compaction.c 20 KB

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