page_reporting.c 12 KB

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