blob: 6885e74c23670f294cba0ff2845accabc58d6db4 [file] [log] [blame]
Alexander Duyck36e66c52020-04-06 20:04:56 -07001// 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/delay.h>
8#include <linux/scatterlist.h>
9
10#include "page_reporting.h"
11#include "internal.h"
12
13#define PAGE_REPORTING_DELAY (2 * HZ)
14static struct page_reporting_dev_info __rcu *pr_dev_info __read_mostly;
15
16enum {
17 PAGE_REPORTING_IDLE = 0,
18 PAGE_REPORTING_REQUESTED,
19 PAGE_REPORTING_ACTIVE
20};
21
22/* request page reporting */
23static void
24__page_reporting_request(struct page_reporting_dev_info *prdev)
25{
26 unsigned int state;
27
28 /* Check to see if we are in desired state */
29 state = atomic_read(&prdev->state);
30 if (state == PAGE_REPORTING_REQUESTED)
31 return;
32
33 /*
34 * If reporting is already active there is nothing we need to do.
35 * Test against 0 as that represents PAGE_REPORTING_IDLE.
36 */
37 state = atomic_xchg(&prdev->state, PAGE_REPORTING_REQUESTED);
38 if (state != PAGE_REPORTING_IDLE)
39 return;
40
41 /*
42 * Delay the start of work to allow a sizable queue to build. For
43 * now we are limiting this to running no more than once every
44 * couple of seconds.
45 */
46 schedule_delayed_work(&prdev->work, PAGE_REPORTING_DELAY);
47}
48
49/* notify prdev of free page reporting request */
50void __page_reporting_notify(void)
51{
52 struct page_reporting_dev_info *prdev;
53
54 /*
55 * We use RCU to protect the pr_dev_info pointer. In almost all
56 * cases this should be present, however in the unlikely case of
57 * a shutdown this will be NULL and we should exit.
58 */
59 rcu_read_lock();
60 prdev = rcu_dereference(pr_dev_info);
61 if (likely(prdev))
62 __page_reporting_request(prdev);
63
64 rcu_read_unlock();
65}
66
67static void
68page_reporting_drain(struct page_reporting_dev_info *prdev,
69 struct scatterlist *sgl, unsigned int nents, bool reported)
70{
71 struct scatterlist *sg = sgl;
72
73 /*
74 * Drain the now reported pages back into their respective
75 * free lists/areas. We assume at least one page is populated.
76 */
77 do {
78 struct page *page = sg_page(sg);
79 int mt = get_pageblock_migratetype(page);
80 unsigned int order = get_order(sg->length);
81
82 __putback_isolated_page(page, order, mt);
83
84 /* If the pages were not reported due to error skip flagging */
85 if (!reported)
86 continue;
87
88 /*
89 * If page was not comingled with another page we can
90 * consider the result to be "reported" since the page
91 * hasn't been modified, otherwise we will need to
92 * report on the new larger page when we make our way
93 * up to that higher order.
94 */
95 if (PageBuddy(page) && page_order(page) == order)
96 __SetPageReported(page);
97 } while ((sg = sg_next(sg)));
98
99 /* reinitialize scatterlist now that it is empty */
100 sg_init_table(sgl, nents);
101}
102
103/*
104 * The page reporting cycle consists of 4 stages, fill, report, drain, and
105 * idle. We will cycle through the first 3 stages until we cannot obtain a
106 * full scatterlist of pages, in that case we will switch to idle.
107 */
108static int
109page_reporting_cycle(struct page_reporting_dev_info *prdev, struct zone *zone,
110 unsigned int order, unsigned int mt,
111 struct scatterlist *sgl, unsigned int *offset)
112{
113 struct free_area *area = &zone->free_area[order];
114 struct list_head *list = &area->free_list[mt];
115 unsigned int page_len = PAGE_SIZE << order;
116 struct page *page, *next;
117 int err = 0;
118
119 /*
120 * Perform early check, if free area is empty there is
121 * nothing to process so we can skip this free_list.
122 */
123 if (list_empty(list))
124 return err;
125
126 spin_lock_irq(&zone->lock);
127
128 /* loop through free list adding unreported pages to sg list */
129 list_for_each_entry_safe(page, next, list, lru) {
130 /* We are going to skip over the reported pages. */
131 if (PageReported(page))
132 continue;
133
Alexander Duyck02cf8712020-04-06 20:05:10 -0700134 /* Attempt to pull page from list and place in scatterlist */
135 if (*offset) {
136 if (!__isolate_free_page(page, order)) {
137 next = page;
138 break;
139 }
Alexander Duyck36e66c52020-04-06 20:04:56 -0700140
Alexander Duyck02cf8712020-04-06 20:05:10 -0700141 /* Add page to scatter list */
142 --(*offset);
143 sg_set_page(&sgl[*offset], page, page_len, 0);
Alexander Duyck36e66c52020-04-06 20:04:56 -0700144
Alexander Duyck36e66c52020-04-06 20:04:56 -0700145 continue;
Alexander Duyck02cf8712020-04-06 20:05:10 -0700146 }
147
148 /*
149 * Make the first non-processed page in the free list
150 * the new head of the free list before we release the
151 * zone lock.
152 */
153 if (&page->lru != list && !list_is_first(&page->lru, list))
154 list_rotate_to_front(&page->lru, list);
Alexander Duyck36e66c52020-04-06 20:04:56 -0700155
156 /* release lock before waiting on report processing */
157 spin_unlock_irq(&zone->lock);
158
159 /* begin processing pages in local list */
160 err = prdev->report(prdev, sgl, PAGE_REPORTING_CAPACITY);
161
162 /* reset offset since the full list was reported */
163 *offset = PAGE_REPORTING_CAPACITY;
164
165 /* reacquire zone lock and resume processing */
166 spin_lock_irq(&zone->lock);
167
168 /* flush reported pages from the sg list */
169 page_reporting_drain(prdev, sgl, PAGE_REPORTING_CAPACITY, !err);
170
171 /*
172 * Reset next to first entry, the old next isn't valid
173 * since we dropped the lock to report the pages
174 */
175 next = list_first_entry(list, struct page, lru);
176
177 /* exit on error */
178 if (err)
179 break;
180 }
181
Alexander Duyck02cf8712020-04-06 20:05:10 -0700182 /* Rotate any leftover pages to the head of the freelist */
183 if (&next->lru != list && !list_is_first(&next->lru, list))
184 list_rotate_to_front(&next->lru, list);
185
Alexander Duyck36e66c52020-04-06 20:04:56 -0700186 spin_unlock_irq(&zone->lock);
187
188 return err;
189}
190
191static int
192page_reporting_process_zone(struct page_reporting_dev_info *prdev,
193 struct scatterlist *sgl, struct zone *zone)
194{
195 unsigned int order, mt, leftover, offset = PAGE_REPORTING_CAPACITY;
196 unsigned long watermark;
197 int err = 0;
198
199 /* Generate minimum watermark to be able to guarantee progress */
200 watermark = low_wmark_pages(zone) +
201 (PAGE_REPORTING_CAPACITY << PAGE_REPORTING_MIN_ORDER);
202
203 /*
204 * Cancel request if insufficient free memory or if we failed
205 * to allocate page reporting statistics for the zone.
206 */
207 if (!zone_watermark_ok(zone, 0, watermark, 0, ALLOC_CMA))
208 return err;
209
210 /* Process each free list starting from lowest order/mt */
211 for (order = PAGE_REPORTING_MIN_ORDER; order < MAX_ORDER; order++) {
212 for (mt = 0; mt < MIGRATE_TYPES; mt++) {
213 /* We do not pull pages from the isolate free list */
214 if (is_migrate_isolate(mt))
215 continue;
216
217 err = page_reporting_cycle(prdev, zone, order, mt,
218 sgl, &offset);
219 if (err)
220 return err;
221 }
222 }
223
224 /* report the leftover pages before going idle */
225 leftover = PAGE_REPORTING_CAPACITY - offset;
226 if (leftover) {
227 sgl = &sgl[offset];
228 err = prdev->report(prdev, sgl, leftover);
229
230 /* flush any remaining pages out from the last report */
231 spin_lock_irq(&zone->lock);
232 page_reporting_drain(prdev, sgl, leftover, !err);
233 spin_unlock_irq(&zone->lock);
234 }
235
236 return err;
237}
238
239static void page_reporting_process(struct work_struct *work)
240{
241 struct delayed_work *d_work = to_delayed_work(work);
242 struct page_reporting_dev_info *prdev =
243 container_of(d_work, struct page_reporting_dev_info, work);
244 int err = 0, state = PAGE_REPORTING_ACTIVE;
245 struct scatterlist *sgl;
246 struct zone *zone;
247
248 /*
249 * Change the state to "Active" so that we can track if there is
250 * anyone requests page reporting after we complete our pass. If
251 * the state is not altered by the end of the pass we will switch
252 * to idle and quit scheduling reporting runs.
253 */
254 atomic_set(&prdev->state, state);
255
256 /* allocate scatterlist to store pages being reported on */
257 sgl = kmalloc_array(PAGE_REPORTING_CAPACITY, sizeof(*sgl), GFP_KERNEL);
258 if (!sgl)
259 goto err_out;
260
261 sg_init_table(sgl, PAGE_REPORTING_CAPACITY);
262
263 for_each_zone(zone) {
264 err = page_reporting_process_zone(prdev, sgl, zone);
265 if (err)
266 break;
267 }
268
269 kfree(sgl);
270err_out:
271 /*
272 * If the state has reverted back to requested then there may be
273 * additional pages to be processed. We will defer for 2s to allow
274 * more pages to accumulate.
275 */
276 state = atomic_cmpxchg(&prdev->state, state, PAGE_REPORTING_IDLE);
277 if (state == PAGE_REPORTING_REQUESTED)
278 schedule_delayed_work(&prdev->work, PAGE_REPORTING_DELAY);
279}
280
281static DEFINE_MUTEX(page_reporting_mutex);
282DEFINE_STATIC_KEY_FALSE(page_reporting_enabled);
283
284int page_reporting_register(struct page_reporting_dev_info *prdev)
285{
286 int err = 0;
287
288 mutex_lock(&page_reporting_mutex);
289
290 /* nothing to do if already in use */
291 if (rcu_access_pointer(pr_dev_info)) {
292 err = -EBUSY;
293 goto err_out;
294 }
295
296 /* initialize state and work structures */
297 atomic_set(&prdev->state, PAGE_REPORTING_IDLE);
298 INIT_DELAYED_WORK(&prdev->work, &page_reporting_process);
299
300 /* Begin initial flush of zones */
301 __page_reporting_request(prdev);
302
303 /* Assign device to allow notifications */
304 rcu_assign_pointer(pr_dev_info, prdev);
305
306 /* enable page reporting notification */
307 if (!static_key_enabled(&page_reporting_enabled)) {
308 static_branch_enable(&page_reporting_enabled);
309 pr_info("Free page reporting enabled\n");
310 }
311err_out:
312 mutex_unlock(&page_reporting_mutex);
313
314 return err;
315}
316EXPORT_SYMBOL_GPL(page_reporting_register);
317
318void page_reporting_unregister(struct page_reporting_dev_info *prdev)
319{
320 mutex_lock(&page_reporting_mutex);
321
322 if (rcu_access_pointer(pr_dev_info) == prdev) {
323 /* Disable page reporting notification */
324 RCU_INIT_POINTER(pr_dev_info, NULL);
325 synchronize_rcu();
326
327 /* Flush any existing work, and lock it out */
328 cancel_delayed_work_sync(&prdev->work);
329 }
330
331 mutex_unlock(&page_reporting_mutex);
332}
333EXPORT_SYMBOL_GPL(page_reporting_unregister);