page_reporting.c 10 KB

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