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