compaction.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. /*
  2. * linux/mm/compaction.c
  3. *
  4. * Memory compaction for the reduction of external fragmentation. Note that
  5. * this heavily depends upon page migration to do all the real heavy
  6. * lifting
  7. *
  8. * Copyright IBM Corp. 2007-2010 Mel Gorman <mel@csn.ul.ie>
  9. */
  10. #include <linux/swap.h>
  11. #include <linux/migrate.h>
  12. #include <linux/compaction.h>
  13. #include <linux/mm_inline.h>
  14. #include <linux/backing-dev.h>
  15. #include <linux/sysctl.h>
  16. #include <linux/sysfs.h>
  17. #include "internal.h"
  18. #define CREATE_TRACE_POINTS
  19. #include <trace/events/compaction.h>
  20. /*
  21. * compact_control is used to track pages being migrated and the free pages
  22. * they are being migrated to during memory compaction. The free_pfn starts
  23. * at the end of a zone and migrate_pfn begins at the start. Movable pages
  24. * are moved to the end of a zone during a compaction run and the run
  25. * completes when free_pfn <= migrate_pfn
  26. */
  27. struct compact_control {
  28. struct list_head freepages; /* List of free pages to migrate to */
  29. struct list_head migratepages; /* List of pages being migrated */
  30. unsigned long nr_freepages; /* Number of isolated free pages */
  31. unsigned long nr_migratepages; /* Number of pages to migrate */
  32. unsigned long free_pfn; /* isolate_freepages search base */
  33. unsigned long migrate_pfn; /* isolate_migratepages search base */
  34. bool sync; /* Synchronous migration */
  35. /* Account for isolated anon and file pages */
  36. unsigned long nr_anon;
  37. unsigned long nr_file;
  38. unsigned int order; /* order a direct compactor needs */
  39. int migratetype; /* MOVABLE, RECLAIMABLE etc */
  40. struct zone *zone;
  41. int compact_mode;
  42. };
  43. static unsigned long release_freepages(struct list_head *freelist)
  44. {
  45. struct page *page, *next;
  46. unsigned long count = 0;
  47. list_for_each_entry_safe(page, next, freelist, lru) {
  48. list_del(&page->lru);
  49. __free_page(page);
  50. count++;
  51. }
  52. return count;
  53. }
  54. /* Isolate free pages onto a private freelist. Must hold zone->lock */
  55. static unsigned long isolate_freepages_block(struct zone *zone,
  56. unsigned long blockpfn,
  57. struct list_head *freelist)
  58. {
  59. unsigned long zone_end_pfn, end_pfn;
  60. int nr_scanned = 0, total_isolated = 0;
  61. struct page *cursor;
  62. /* Get the last PFN we should scan for free pages at */
  63. zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
  64. end_pfn = min(blockpfn + pageblock_nr_pages, zone_end_pfn);
  65. /* Find the first usable PFN in the block to initialse page cursor */
  66. for (; blockpfn < end_pfn; blockpfn++) {
  67. if (pfn_valid_within(blockpfn))
  68. break;
  69. }
  70. cursor = pfn_to_page(blockpfn);
  71. /* Isolate free pages. This assumes the block is valid */
  72. for (; blockpfn < end_pfn; blockpfn++, cursor++) {
  73. int isolated, i;
  74. struct page *page = cursor;
  75. if (!pfn_valid_within(blockpfn))
  76. continue;
  77. nr_scanned++;
  78. if (!PageBuddy(page))
  79. continue;
  80. /* Found a free page, break it into order-0 pages */
  81. isolated = split_free_page(page);
  82. total_isolated += isolated;
  83. for (i = 0; i < isolated; i++) {
  84. list_add(&page->lru, freelist);
  85. page++;
  86. }
  87. /* If a page was split, advance to the end of it */
  88. if (isolated) {
  89. blockpfn += isolated - 1;
  90. cursor += isolated - 1;
  91. }
  92. }
  93. trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
  94. return total_isolated;
  95. }
  96. /* Returns true if the page is within a block suitable for migration to */
  97. static bool suitable_migration_target(struct page *page)
  98. {
  99. int migratetype = get_pageblock_migratetype(page);
  100. /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */
  101. if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE)
  102. return false;
  103. /* If the page is a large free page, then allow migration */
  104. if (PageBuddy(page) && page_order(page) >= pageblock_order)
  105. return true;
  106. /* If the block is MIGRATE_MOVABLE, allow migration */
  107. if (migratetype == MIGRATE_MOVABLE)
  108. return true;
  109. /* Otherwise skip the block */
  110. return false;
  111. }
  112. /*
  113. * Based on information in the current compact_control, find blocks
  114. * suitable for isolating free pages from and then isolate them.
  115. */
  116. static void isolate_freepages(struct zone *zone,
  117. struct compact_control *cc)
  118. {
  119. struct page *page;
  120. unsigned long high_pfn, low_pfn, pfn;
  121. unsigned long flags;
  122. int nr_freepages = cc->nr_freepages;
  123. struct list_head *freelist = &cc->freepages;
  124. pfn = cc->free_pfn;
  125. low_pfn = cc->migrate_pfn + pageblock_nr_pages;
  126. high_pfn = low_pfn;
  127. /*
  128. * Isolate free pages until enough are available to migrate the
  129. * pages on cc->migratepages. We stop searching if the migrate
  130. * and free page scanners meet or enough free pages are isolated.
  131. */
  132. spin_lock_irqsave(&zone->lock, flags);
  133. for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
  134. pfn -= pageblock_nr_pages) {
  135. unsigned long isolated;
  136. if (!pfn_valid(pfn))
  137. continue;
  138. /*
  139. * Check for overlapping nodes/zones. It's possible on some
  140. * configurations to have a setup like
  141. * node0 node1 node0
  142. * i.e. it's possible that all pages within a zones range of
  143. * pages do not belong to a single zone.
  144. */
  145. page = pfn_to_page(pfn);
  146. if (page_zone(page) != zone)
  147. continue;
  148. /* Check the block is suitable for migration */
  149. if (!suitable_migration_target(page))
  150. continue;
  151. /* Found a block suitable for isolating free pages from */
  152. isolated = isolate_freepages_block(zone, pfn, freelist);
  153. nr_freepages += isolated;
  154. /*
  155. * Record the highest PFN we isolated pages from. When next
  156. * looking for free pages, the search will restart here as
  157. * page migration may have returned some pages to the allocator
  158. */
  159. if (isolated)
  160. high_pfn = max(high_pfn, pfn);
  161. }
  162. spin_unlock_irqrestore(&zone->lock, flags);
  163. /* split_free_page does not map the pages */
  164. list_for_each_entry(page, freelist, lru) {
  165. arch_alloc_page(page, 0);
  166. kernel_map_pages(page, 1, 1);
  167. }
  168. cc->free_pfn = high_pfn;
  169. cc->nr_freepages = nr_freepages;
  170. }
  171. /* Update the number of anon and file isolated pages in the zone */
  172. static void acct_isolated(struct zone *zone, struct compact_control *cc)
  173. {
  174. struct page *page;
  175. unsigned int count[NR_LRU_LISTS] = { 0, };
  176. list_for_each_entry(page, &cc->migratepages, lru) {
  177. int lru = page_lru_base_type(page);
  178. count[lru]++;
  179. }
  180. cc->nr_anon = count[LRU_ACTIVE_ANON] + count[LRU_INACTIVE_ANON];
  181. cc->nr_file = count[LRU_ACTIVE_FILE] + count[LRU_INACTIVE_FILE];
  182. __mod_zone_page_state(zone, NR_ISOLATED_ANON, cc->nr_anon);
  183. __mod_zone_page_state(zone, NR_ISOLATED_FILE, cc->nr_file);
  184. }
  185. /* Similar to reclaim, but different enough that they don't share logic */
  186. static bool too_many_isolated(struct zone *zone)
  187. {
  188. unsigned long active, inactive, isolated;
  189. inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
  190. zone_page_state(zone, NR_INACTIVE_ANON);
  191. active = zone_page_state(zone, NR_ACTIVE_FILE) +
  192. zone_page_state(zone, NR_ACTIVE_ANON);
  193. isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
  194. zone_page_state(zone, NR_ISOLATED_ANON);
  195. return isolated > (inactive + active) / 2;
  196. }
  197. /*
  198. * Isolate all pages that can be migrated from the block pointed to by
  199. * the migrate scanner within compact_control.
  200. */
  201. static unsigned long isolate_migratepages(struct zone *zone,
  202. struct compact_control *cc)
  203. {
  204. unsigned long low_pfn, end_pfn;
  205. unsigned long last_pageblock_nr = 0, pageblock_nr;
  206. unsigned long nr_scanned = 0, nr_isolated = 0;
  207. struct list_head *migratelist = &cc->migratepages;
  208. /* Do not scan outside zone boundaries */
  209. low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
  210. /* Only scan within a pageblock boundary */
  211. end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
  212. /* Do not cross the free scanner or scan within a memory hole */
  213. if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
  214. cc->migrate_pfn = end_pfn;
  215. return 0;
  216. }
  217. /*
  218. * Ensure that there are not too many pages isolated from the LRU
  219. * list by either parallel reclaimers or compaction. If there are,
  220. * delay for some time until fewer pages are isolated
  221. */
  222. while (unlikely(too_many_isolated(zone))) {
  223. congestion_wait(BLK_RW_ASYNC, HZ/10);
  224. if (fatal_signal_pending(current))
  225. return 0;
  226. }
  227. /* Time to isolate some pages for migration */
  228. spin_lock_irq(&zone->lru_lock);
  229. for (; low_pfn < end_pfn; low_pfn++) {
  230. struct page *page;
  231. if (!pfn_valid_within(low_pfn))
  232. continue;
  233. nr_scanned++;
  234. /* Get the page and skip if free */
  235. page = pfn_to_page(low_pfn);
  236. if (PageBuddy(page))
  237. continue;
  238. /*
  239. * For async migration, also only scan in MOVABLE blocks. Async
  240. * migration is optimistic to see if the minimum amount of work
  241. * satisfies the allocation
  242. */
  243. pageblock_nr = low_pfn >> pageblock_order;
  244. if (!cc->sync && last_pageblock_nr != pageblock_nr &&
  245. get_pageblock_migratetype(page) != MIGRATE_MOVABLE) {
  246. low_pfn += pageblock_nr_pages;
  247. low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1;
  248. last_pageblock_nr = pageblock_nr;
  249. continue;
  250. }
  251. if (!PageLRU(page))
  252. continue;
  253. /*
  254. * PageLRU is set, and lru_lock excludes isolation,
  255. * splitting and collapsing (collapsing has already
  256. * happened if PageLRU is set).
  257. */
  258. if (PageTransHuge(page)) {
  259. low_pfn += (1 << compound_order(page)) - 1;
  260. continue;
  261. }
  262. /* Try isolate the page */
  263. if (__isolate_lru_page(page, ISOLATE_BOTH, 0) != 0)
  264. continue;
  265. VM_BUG_ON(PageTransCompound(page));
  266. /* Successfully isolated */
  267. del_page_from_lru_list(zone, page, page_lru(page));
  268. list_add(&page->lru, migratelist);
  269. cc->nr_migratepages++;
  270. nr_isolated++;
  271. /* Avoid isolating too much */
  272. if (cc->nr_migratepages == COMPACT_CLUSTER_MAX)
  273. break;
  274. }
  275. acct_isolated(zone, cc);
  276. spin_unlock_irq(&zone->lru_lock);
  277. cc->migrate_pfn = low_pfn;
  278. trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
  279. return cc->nr_migratepages;
  280. }
  281. /*
  282. * This is a migrate-callback that "allocates" freepages by taking pages
  283. * from the isolated freelists in the block we are migrating to.
  284. */
  285. static struct page *compaction_alloc(struct page *migratepage,
  286. unsigned long data,
  287. int **result)
  288. {
  289. struct compact_control *cc = (struct compact_control *)data;
  290. struct page *freepage;
  291. /* Isolate free pages if necessary */
  292. if (list_empty(&cc->freepages)) {
  293. isolate_freepages(cc->zone, cc);
  294. if (list_empty(&cc->freepages))
  295. return NULL;
  296. }
  297. freepage = list_entry(cc->freepages.next, struct page, lru);
  298. list_del(&freepage->lru);
  299. cc->nr_freepages--;
  300. return freepage;
  301. }
  302. /*
  303. * We cannot control nr_migratepages and nr_freepages fully when migration is
  304. * running as migrate_pages() has no knowledge of compact_control. When
  305. * migration is complete, we count the number of pages on the lists by hand.
  306. */
  307. static void update_nr_listpages(struct compact_control *cc)
  308. {
  309. int nr_migratepages = 0;
  310. int nr_freepages = 0;
  311. struct page *page;
  312. list_for_each_entry(page, &cc->migratepages, lru)
  313. nr_migratepages++;
  314. list_for_each_entry(page, &cc->freepages, lru)
  315. nr_freepages++;
  316. cc->nr_migratepages = nr_migratepages;
  317. cc->nr_freepages = nr_freepages;
  318. }
  319. static int compact_finished(struct zone *zone,
  320. struct compact_control *cc)
  321. {
  322. unsigned int order;
  323. unsigned long watermark;
  324. if (fatal_signal_pending(current))
  325. return COMPACT_PARTIAL;
  326. /* Compaction run completes if the migrate and free scanner meet */
  327. if (cc->free_pfn <= cc->migrate_pfn)
  328. return COMPACT_COMPLETE;
  329. /* Compaction run is not finished if the watermark is not met */
  330. if (cc->compact_mode != COMPACT_MODE_KSWAPD)
  331. watermark = low_wmark_pages(zone);
  332. else
  333. watermark = high_wmark_pages(zone);
  334. watermark += (1 << cc->order);
  335. if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
  336. return COMPACT_CONTINUE;
  337. /*
  338. * order == -1 is expected when compacting via
  339. * /proc/sys/vm/compact_memory
  340. */
  341. if (cc->order == -1)
  342. return COMPACT_CONTINUE;
  343. /*
  344. * Generating only one page of the right order is not enough
  345. * for kswapd, we must continue until we're above the high
  346. * watermark as a pool for high order GFP_ATOMIC allocations
  347. * too.
  348. */
  349. if (cc->compact_mode == COMPACT_MODE_KSWAPD)
  350. return COMPACT_CONTINUE;
  351. /* Direct compactor: Is a suitable page free? */
  352. for (order = cc->order; order < MAX_ORDER; order++) {
  353. /* Job done if page is free of the right migratetype */
  354. if (!list_empty(&zone->free_area[order].free_list[cc->migratetype]))
  355. return COMPACT_PARTIAL;
  356. /* Job done if allocation would set block type */
  357. if (order >= pageblock_order && zone->free_area[order].nr_free)
  358. return COMPACT_PARTIAL;
  359. }
  360. return COMPACT_CONTINUE;
  361. }
  362. /*
  363. * compaction_suitable: Is this suitable to run compaction on this zone now?
  364. * Returns
  365. * COMPACT_SKIPPED - If there are too few free pages for compaction
  366. * COMPACT_PARTIAL - If the allocation would succeed without compaction
  367. * COMPACT_CONTINUE - If compaction should run now
  368. */
  369. unsigned long compaction_suitable(struct zone *zone, int order)
  370. {
  371. int fragindex;
  372. unsigned long watermark;
  373. /*
  374. * Watermarks for order-0 must be met for compaction. Note the 2UL.
  375. * This is because during migration, copies of pages need to be
  376. * allocated and for a short time, the footprint is higher
  377. */
  378. watermark = low_wmark_pages(zone) + (2UL << order);
  379. if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
  380. return COMPACT_SKIPPED;
  381. /*
  382. * order == -1 is expected when compacting via
  383. * /proc/sys/vm/compact_memory
  384. */
  385. if (order == -1)
  386. return COMPACT_CONTINUE;
  387. /*
  388. * fragmentation index determines if allocation failures are due to
  389. * low memory or external fragmentation
  390. *
  391. * index of -1 implies allocations might succeed dependingon watermarks
  392. * index towards 0 implies failure is due to lack of memory
  393. * index towards 1000 implies failure is due to fragmentation
  394. *
  395. * Only compact if a failure would be due to fragmentation.
  396. */
  397. fragindex = fragmentation_index(zone, order);
  398. if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
  399. return COMPACT_SKIPPED;
  400. if (fragindex == -1 && zone_watermark_ok(zone, order, watermark, 0, 0))
  401. return COMPACT_PARTIAL;
  402. return COMPACT_CONTINUE;
  403. }
  404. static int compact_zone(struct zone *zone, struct compact_control *cc)
  405. {
  406. int ret;
  407. ret = compaction_suitable(zone, cc->order);
  408. switch (ret) {
  409. case COMPACT_PARTIAL:
  410. case COMPACT_SKIPPED:
  411. /* Compaction is likely to fail */
  412. return ret;
  413. case COMPACT_CONTINUE:
  414. /* Fall through to compaction */
  415. ;
  416. }
  417. /* Setup to move all movable pages to the end of the zone */
  418. cc->migrate_pfn = zone->zone_start_pfn;
  419. cc->free_pfn = cc->migrate_pfn + zone->spanned_pages;
  420. cc->free_pfn &= ~(pageblock_nr_pages-1);
  421. migrate_prep_local();
  422. while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
  423. unsigned long nr_migrate, nr_remaining;
  424. if (!isolate_migratepages(zone, cc))
  425. continue;
  426. nr_migrate = cc->nr_migratepages;
  427. migrate_pages(&cc->migratepages, compaction_alloc,
  428. (unsigned long)cc, false,
  429. cc->sync);
  430. update_nr_listpages(cc);
  431. nr_remaining = cc->nr_migratepages;
  432. count_vm_event(COMPACTBLOCKS);
  433. count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining);
  434. if (nr_remaining)
  435. count_vm_events(COMPACTPAGEFAILED, nr_remaining);
  436. trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
  437. nr_remaining);
  438. /* Release LRU pages not migrated */
  439. if (!list_empty(&cc->migratepages)) {
  440. putback_lru_pages(&cc->migratepages);
  441. cc->nr_migratepages = 0;
  442. }
  443. }
  444. /* Release free pages and check accounting */
  445. cc->nr_freepages -= release_freepages(&cc->freepages);
  446. VM_BUG_ON(cc->nr_freepages != 0);
  447. return ret;
  448. }
  449. unsigned long compact_zone_order(struct zone *zone,
  450. int order, gfp_t gfp_mask,
  451. bool sync,
  452. int compact_mode)
  453. {
  454. struct compact_control cc = {
  455. .nr_freepages = 0,
  456. .nr_migratepages = 0,
  457. .order = order,
  458. .migratetype = allocflags_to_migratetype(gfp_mask),
  459. .zone = zone,
  460. .sync = sync,
  461. .compact_mode = compact_mode,
  462. };
  463. INIT_LIST_HEAD(&cc.freepages);
  464. INIT_LIST_HEAD(&cc.migratepages);
  465. return compact_zone(zone, &cc);
  466. }
  467. int sysctl_extfrag_threshold = 500;
  468. /**
  469. * try_to_compact_pages - Direct compact to satisfy a high-order allocation
  470. * @zonelist: The zonelist used for the current allocation
  471. * @order: The order of the current allocation
  472. * @gfp_mask: The GFP mask of the current allocation
  473. * @nodemask: The allowed nodes to allocate from
  474. * @sync: Whether migration is synchronous or not
  475. *
  476. * This is the main entry point for direct page compaction.
  477. */
  478. unsigned long try_to_compact_pages(struct zonelist *zonelist,
  479. int order, gfp_t gfp_mask, nodemask_t *nodemask,
  480. bool sync)
  481. {
  482. enum zone_type high_zoneidx = gfp_zone(gfp_mask);
  483. int may_enter_fs = gfp_mask & __GFP_FS;
  484. int may_perform_io = gfp_mask & __GFP_IO;
  485. struct zoneref *z;
  486. struct zone *zone;
  487. int rc = COMPACT_SKIPPED;
  488. /*
  489. * Check whether it is worth even starting compaction. The order check is
  490. * made because an assumption is made that the page allocator can satisfy
  491. * the "cheaper" orders without taking special steps
  492. */
  493. if (!order || !may_enter_fs || !may_perform_io)
  494. return rc;
  495. count_vm_event(COMPACTSTALL);
  496. /* Compact each zone in the list */
  497. for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
  498. nodemask) {
  499. int status;
  500. status = compact_zone_order(zone, order, gfp_mask, sync,
  501. COMPACT_MODE_DIRECT_RECLAIM);
  502. rc = max(status, rc);
  503. /* If a normal allocation would succeed, stop compacting */
  504. if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0, 0))
  505. break;
  506. }
  507. return rc;
  508. }
  509. /* Compact all zones within a node */
  510. static int compact_node(int nid)
  511. {
  512. int zoneid;
  513. pg_data_t *pgdat;
  514. struct zone *zone;
  515. if (nid < 0 || nid >= nr_node_ids || !node_online(nid))
  516. return -EINVAL;
  517. pgdat = NODE_DATA(nid);
  518. /* Flush pending updates to the LRU lists */
  519. lru_add_drain_all();
  520. for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
  521. struct compact_control cc = {
  522. .nr_freepages = 0,
  523. .nr_migratepages = 0,
  524. .order = -1,
  525. .compact_mode = COMPACT_MODE_DIRECT_RECLAIM,
  526. };
  527. zone = &pgdat->node_zones[zoneid];
  528. if (!populated_zone(zone))
  529. continue;
  530. cc.zone = zone;
  531. INIT_LIST_HEAD(&cc.freepages);
  532. INIT_LIST_HEAD(&cc.migratepages);
  533. compact_zone(zone, &cc);
  534. VM_BUG_ON(!list_empty(&cc.freepages));
  535. VM_BUG_ON(!list_empty(&cc.migratepages));
  536. }
  537. return 0;
  538. }
  539. /* Compact all nodes in the system */
  540. static int compact_nodes(void)
  541. {
  542. int nid;
  543. for_each_online_node(nid)
  544. compact_node(nid);
  545. return COMPACT_COMPLETE;
  546. }
  547. /* The written value is actually unused, all memory is compacted */
  548. int sysctl_compact_memory;
  549. /* This is the entry point for compacting all nodes via /proc/sys/vm */
  550. int sysctl_compaction_handler(struct ctl_table *table, int write,
  551. void __user *buffer, size_t *length, loff_t *ppos)
  552. {
  553. if (write)
  554. return compact_nodes();
  555. return 0;
  556. }
  557. int sysctl_extfrag_handler(struct ctl_table *table, int write,
  558. void __user *buffer, size_t *length, loff_t *ppos)
  559. {
  560. proc_dointvec_minmax(table, write, buffer, length, ppos);
  561. return 0;
  562. }
  563. #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
  564. ssize_t sysfs_compact_node(struct sys_device *dev,
  565. struct sysdev_attribute *attr,
  566. const char *buf, size_t count)
  567. {
  568. compact_node(dev->id);
  569. return count;
  570. }
  571. static SYSDEV_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
  572. int compaction_register_node(struct node *node)
  573. {
  574. return sysdev_create_file(&node->sysdev, &attr_compact);
  575. }
  576. void compaction_unregister_node(struct node *node)
  577. {
  578. return sysdev_remove_file(&node->sysdev, &attr_compact);
  579. }
  580. #endif /* CONFIG_SYSFS && CONFIG_NUMA */