xref: /linux/mm/page_reporting.c (revision cf85f810f911234a06a4ef2439e8694b93b717fc)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/mm.h>
3 #include <linux/mmzone.h>
4 #include <linux/page_reporting.h>
5 #include <linux/gfp.h>
6 #include <linux/export.h>
7 #include <linux/module.h>
8 #include <linux/delay.h>
9 #include <linux/scatterlist.h>
10 
11 #include "page_reporting.h"
12 #include "internal.h"
13 
14 /* Initialize to an unsupported value */
15 unsigned int page_reporting_order = PAGE_REPORTING_ORDER_UNSPECIFIED;
16 
17 static int page_order_update_notify(const char *val, const struct kernel_param *kp)
18 {
19 	/*
20 	 * If param is set beyond this limit, order is set to default
21 	 * pageblock_order value
22 	 */
23 	return  param_set_uint_minmax(val, kp, 0, MAX_PAGE_ORDER);
24 }
25 
26 static const struct kernel_param_ops page_reporting_param_ops = {
27 	.set = &page_order_update_notify,
28 	/*
29 	 * For the get op, use param_get_int instead of param_get_uint.
30 	 * This is to make sure that when unset the initialized value of
31 	 * -1 is shown correctly
32 	 */
33 	.get = &param_get_int,
34 };
35 
36 module_param_cb(page_reporting_order, &page_reporting_param_ops,
37 			&page_reporting_order, 0644);
38 MODULE_PARM_DESC(page_reporting_order, "Set page reporting order");
39 
40 /*
41  * This symbol is also a kernel parameter. Export the page_reporting_order
42  * symbol so that other drivers can access it to control order values without
43  * having to introduce another configurable parameter. Only one driver can
44  * register with the page_reporting driver for the service, so we have just
45  * one control parameter for the use case(which can be accessed in both
46  * drivers)
47  */
48 EXPORT_SYMBOL_GPL(page_reporting_order);
49 
50 #define PAGE_REPORTING_DELAY	(2 * HZ)
51 static struct page_reporting_dev_info __rcu *pr_dev_info __read_mostly;
52 
53 enum {
54 	PAGE_REPORTING_IDLE = 0,
55 	PAGE_REPORTING_REQUESTED,
56 	PAGE_REPORTING_ACTIVE
57 };
58 
59 /* request page reporting */
60 static void
61 __page_reporting_request(struct page_reporting_dev_info *prdev)
62 {
63 	unsigned int state;
64 
65 	/* Check to see if we are in desired state */
66 	state = atomic_read(&prdev->state);
67 	if (state == PAGE_REPORTING_REQUESTED)
68 		return;
69 
70 	/*
71 	 * If reporting is already active there is nothing we need to do.
72 	 * Test against 0 as that represents PAGE_REPORTING_IDLE.
73 	 */
74 	state = atomic_xchg(&prdev->state, PAGE_REPORTING_REQUESTED);
75 	if (state != PAGE_REPORTING_IDLE)
76 		return;
77 
78 	/*
79 	 * Delay the start of work to allow a sizable queue to build. For
80 	 * now we are limiting this to running no more than once every
81 	 * couple of seconds.
82 	 */
83 	queue_delayed_work(system_freezable_wq, &prdev->work,
84 			   PAGE_REPORTING_DELAY);
85 }
86 
87 /* notify prdev of free page reporting request */
88 void __page_reporting_notify(void)
89 {
90 	struct page_reporting_dev_info *prdev;
91 
92 	/*
93 	 * We use RCU to protect the pr_dev_info pointer. In almost all
94 	 * cases this should be present, however in the unlikely case of
95 	 * a shutdown this will be NULL and we should exit.
96 	 */
97 	rcu_read_lock();
98 	prdev = rcu_dereference(pr_dev_info);
99 	if (likely(prdev))
100 		__page_reporting_request(prdev);
101 
102 	rcu_read_unlock();
103 }
104 
105 static void
106 page_reporting_drain(struct page_reporting_dev_info *prdev,
107 		     struct scatterlist *sgl, unsigned int nents, bool reported)
108 {
109 	struct scatterlist *sg = sgl;
110 
111 	/*
112 	 * Drain the now reported pages back into their respective
113 	 * free lists/areas. We assume at least one page is populated.
114 	 */
115 	do {
116 		struct page *page = sg_page(sg);
117 		int mt = get_pageblock_migratetype(page);
118 		unsigned int order = get_order(sg->length);
119 
120 		__putback_isolated_page(page, order, mt);
121 
122 		/* If the pages were not reported due to error skip flagging */
123 		if (!reported)
124 			continue;
125 
126 		/*
127 		 * If page was not commingled with another page we can
128 		 * consider the result to be "reported" since the page
129 		 * hasn't been modified, otherwise we will need to
130 		 * report on the new larger page when we make our way
131 		 * up to that higher order.
132 		 */
133 		if (PageBuddy(page) && buddy_order(page) == order)
134 			__SetPageReported(page);
135 	} while ((sg = sg_next(sg)));
136 
137 	/* reinitialize scatterlist now that it is empty */
138 	sg_init_table(sgl, nents);
139 }
140 
141 /*
142  * The page reporting cycle consists of 4 stages, fill, report, drain, and
143  * idle. We will cycle through the first 3 stages until we cannot obtain a
144  * full scatterlist of pages, in that case we will switch to idle.
145  */
146 static int
147 page_reporting_cycle(struct page_reporting_dev_info *prdev, struct zone *zone,
148 		     unsigned int order, unsigned int mt,
149 		     struct scatterlist *sgl, unsigned int *offset)
150 {
151 	struct free_area *area = &zone->free_area[order];
152 	struct list_head *list = &area->free_list[mt];
153 	unsigned int page_len = PAGE_SIZE << order;
154 	struct page *page, *next;
155 	long budget;
156 	int err = 0;
157 
158 	/*
159 	 * Perform early check, if free area is empty there is
160 	 * nothing to process so we can skip this free_list.
161 	 */
162 	if (list_empty(list))
163 		return err;
164 
165 	spin_lock_irq(&zone->lock);
166 
167 	/*
168 	 * Limit how many calls we will be making to the page reporting
169 	 * device for this list. By doing this we avoid processing any
170 	 * given list for too long.
171 	 *
172 	 * The current value used allows us enough calls to process over a
173 	 * sixteenth of the current list plus one additional call to handle
174 	 * any pages that may have already been present from the previous
175 	 * list processed. This should result in us reporting all pages on
176 	 * an idle system in about 30 seconds.
177 	 */
178 	budget = DIV_ROUND_UP(area->nr_free, prdev->capacity * 16);
179 
180 	/* loop through free list adding unreported pages to sg list */
181 	list_for_each_entry_safe(page, next, list, lru) {
182 		/* We are going to skip over the reported pages. */
183 		if (PageReported(page))
184 			continue;
185 
186 		/*
187 		 * If we fully consumed our budget then update our
188 		 * state to indicate that we are requesting additional
189 		 * processing and exit this list.
190 		 */
191 		if (budget < 0) {
192 			atomic_set(&prdev->state, PAGE_REPORTING_REQUESTED);
193 			next = page;
194 			break;
195 		}
196 
197 		/* Attempt to pull page from list and place in scatterlist */
198 		if (*offset) {
199 			if (!__isolate_free_page(page, order)) {
200 				next = page;
201 				break;
202 			}
203 
204 			/* Add page to scatter list */
205 			--(*offset);
206 			sg_set_page(&sgl[*offset], page, page_len, 0);
207 
208 			continue;
209 		}
210 
211 		/*
212 		 * Make the first non-reported page in the free list
213 		 * the new head of the free list before we release the
214 		 * zone lock.
215 		 */
216 		if (!list_is_first(&page->lru, list))
217 			list_rotate_to_front(&page->lru, list);
218 
219 		/* release lock before waiting on report processing */
220 		spin_unlock_irq(&zone->lock);
221 
222 		/* begin processing pages in local list */
223 		err = prdev->report(prdev, sgl, prdev->capacity);
224 
225 		/* reset offset since the full list was reported */
226 		*offset = prdev->capacity;
227 
228 		/* update budget to reflect call to report function */
229 		budget--;
230 
231 		/* reacquire zone lock and resume processing */
232 		spin_lock_irq(&zone->lock);
233 
234 		/* flush reported pages from the sg list */
235 		page_reporting_drain(prdev, sgl, prdev->capacity, !err);
236 
237 		/*
238 		 * Reset next to first entry, the old next isn't valid
239 		 * since we dropped the lock to report the pages
240 		 */
241 		next = list_first_entry(list, struct page, lru);
242 
243 		/* exit on error */
244 		if (err)
245 			break;
246 	}
247 
248 	/* Rotate any leftover pages to the head of the freelist */
249 	if (!list_entry_is_head(next, list, lru) && !list_is_first(&next->lru, list))
250 		list_rotate_to_front(&next->lru, list);
251 
252 	spin_unlock_irq(&zone->lock);
253 
254 	return err;
255 }
256 
257 static int
258 page_reporting_process_zone(struct page_reporting_dev_info *prdev,
259 			    struct scatterlist *sgl, struct zone *zone)
260 {
261 	unsigned int order, mt, leftover, offset = prdev->capacity;
262 	unsigned long watermark;
263 	int err = 0;
264 
265 	/* Generate minimum watermark to be able to guarantee progress */
266 	watermark = low_wmark_pages(zone) +
267 		    (prdev->capacity << page_reporting_order);
268 
269 	/*
270 	 * Cancel request if insufficient free memory or if we failed
271 	 * to allocate page reporting statistics for the zone.
272 	 */
273 	if (!zone_watermark_ok(zone, 0, watermark, 0, ALLOC_CMA))
274 		return err;
275 
276 	/* Process each free list starting from lowest order/mt */
277 	for (order = page_reporting_order; order < NR_PAGE_ORDERS; order++) {
278 		for (mt = 0; mt < MIGRATE_TYPES; mt++) {
279 			/* We do not pull pages from the isolate free list */
280 			if (is_migrate_isolate(mt))
281 				continue;
282 
283 			err = page_reporting_cycle(prdev, zone, order, mt,
284 						   sgl, &offset);
285 			if (err)
286 				return err;
287 		}
288 	}
289 
290 	/* report the leftover pages before going idle */
291 	leftover = prdev->capacity - offset;
292 	if (leftover) {
293 		sgl = &sgl[offset];
294 		err = prdev->report(prdev, sgl, leftover);
295 
296 		/* flush any remaining pages out from the last report */
297 		spin_lock_irq(&zone->lock);
298 		page_reporting_drain(prdev, sgl, leftover, !err);
299 		spin_unlock_irq(&zone->lock);
300 	}
301 
302 	return err;
303 }
304 
305 static void page_reporting_process(struct work_struct *work)
306 {
307 	struct delayed_work *d_work = to_delayed_work(work);
308 	struct page_reporting_dev_info *prdev =
309 		container_of(d_work, struct page_reporting_dev_info, work);
310 	int err = 0, state = PAGE_REPORTING_ACTIVE;
311 	struct scatterlist *sgl;
312 	struct zone *zone;
313 
314 	/*
315 	 * Change the state to "Active" so that we can track if there is
316 	 * anyone requests page reporting after we complete our pass. If
317 	 * the state is not altered by the end of the pass we will switch
318 	 * to idle and quit scheduling reporting runs.
319 	 */
320 	atomic_set(&prdev->state, state);
321 
322 	/* allocate scatterlist to store pages being reported on */
323 	sgl = kmalloc_objs(*sgl, prdev->capacity);
324 	if (!sgl)
325 		goto err_out;
326 
327 	sg_init_table(sgl, prdev->capacity);
328 
329 	for_each_zone(zone) {
330 		err = page_reporting_process_zone(prdev, sgl, zone);
331 		if (err)
332 			break;
333 	}
334 
335 	kfree(sgl);
336 err_out:
337 	/*
338 	 * If the state has reverted back to requested then there may be
339 	 * additional pages to be processed. We will defer for 2s to allow
340 	 * more pages to accumulate.
341 	 */
342 	state = atomic_cmpxchg(&prdev->state, state, PAGE_REPORTING_IDLE);
343 	if (state == PAGE_REPORTING_REQUESTED)
344 		queue_delayed_work(system_freezable_wq, &prdev->work,
345 				   PAGE_REPORTING_DELAY);
346 }
347 
348 static DEFINE_MUTEX(page_reporting_mutex);
349 DEFINE_STATIC_KEY_FALSE(page_reporting_enabled);
350 
351 int page_reporting_register(struct page_reporting_dev_info *prdev)
352 {
353 	int err = 0;
354 
355 	mutex_lock(&page_reporting_mutex);
356 
357 	/* nothing to do if already in use */
358 	if (rcu_dereference_protected(pr_dev_info,
359 				lockdep_is_held(&page_reporting_mutex))) {
360 		err = -EBUSY;
361 		goto err_out;
362 	}
363 
364 	/*
365 	 * If the page_reporting_order value is not set, we check if
366 	 * an order is provided from the driver that is performing the
367 	 * registration. If that is not provided either, we default to
368 	 * pageblock_order.
369 	 */
370 
371 	if (page_reporting_order == PAGE_REPORTING_ORDER_UNSPECIFIED) {
372 		if (prdev->order != PAGE_REPORTING_ORDER_UNSPECIFIED &&
373 		    prdev->order <= MAX_PAGE_ORDER)
374 			page_reporting_order = prdev->order;
375 		else
376 			page_reporting_order = pageblock_order;
377 	}
378 
379 	if (!prdev->capacity || prdev->capacity > PAGE_REPORTING_CAPACITY)
380 		prdev->capacity = PAGE_REPORTING_CAPACITY;
381 
382 	/* initialize state and work structures */
383 	atomic_set(&prdev->state, PAGE_REPORTING_IDLE);
384 	INIT_DELAYED_WORK(&prdev->work, &page_reporting_process);
385 
386 	/* Begin initial flush of zones */
387 	__page_reporting_request(prdev);
388 
389 	/* Assign device to allow notifications */
390 	rcu_assign_pointer(pr_dev_info, prdev);
391 
392 	/* enable page reporting notification */
393 	if (!static_key_enabled(&page_reporting_enabled)) {
394 		static_branch_enable(&page_reporting_enabled);
395 		pr_info("Free page reporting enabled\n");
396 	}
397 err_out:
398 	mutex_unlock(&page_reporting_mutex);
399 
400 	return err;
401 }
402 EXPORT_SYMBOL_GPL(page_reporting_register);
403 
404 void page_reporting_unregister(struct page_reporting_dev_info *prdev)
405 {
406 	mutex_lock(&page_reporting_mutex);
407 
408 	if (prdev == rcu_dereference_protected(pr_dev_info,
409 				lockdep_is_held(&page_reporting_mutex))) {
410 		/* Disable page reporting notification */
411 		RCU_INIT_POINTER(pr_dev_info, NULL);
412 		synchronize_rcu();
413 
414 		/* Flush any existing work, and lock it out */
415 		cancel_delayed_work_sync(&prdev->work);
416 	}
417 
418 	mutex_unlock(&page_reporting_mutex);
419 }
420 EXPORT_SYMBOL_GPL(page_reporting_unregister);
421