compaction.c 19 KB

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