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