]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - mm/compaction.c
mm: compaction: introduce map_pages()
[mirror_ubuntu-artful-kernel.git] / mm / compaction.c
CommitLineData
748446bb
MG
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>
76ab0f53 15#include <linux/sysctl.h>
ed4a6d7f 16#include <linux/sysfs.h>
748446bb
MG
17#include "internal.h"
18
b7aba698
MG
19#define CREATE_TRACE_POINTS
20#include <trace/events/compaction.h>
21
748446bb
MG
22/*
23 * compact_control is used to track pages being migrated and the free pages
24 * they are being migrated to during memory compaction. The free_pfn starts
25 * at the end of a zone and migrate_pfn begins at the start. Movable pages
26 * are moved to the end of a zone during a compaction run and the run
27 * completes when free_pfn <= migrate_pfn
28 */
29struct compact_control {
30 struct list_head freepages; /* List of free pages to migrate to */
31 struct list_head migratepages; /* List of pages being migrated */
32 unsigned long nr_freepages; /* Number of isolated free pages */
33 unsigned long nr_migratepages; /* Number of pages to migrate */
34 unsigned long free_pfn; /* isolate_freepages search base */
35 unsigned long migrate_pfn; /* isolate_migratepages search base */
77f1fe6b 36 bool sync; /* Synchronous migration */
748446bb 37
aad6ec37 38 int order; /* order a direct compactor needs */
56de7263 39 int migratetype; /* MOVABLE, RECLAIMABLE etc */
748446bb
MG
40 struct zone *zone;
41};
42
43static unsigned long release_freepages(struct list_head *freelist)
44{
45 struct page *page, *next;
46 unsigned long count = 0;
47
48 list_for_each_entry_safe(page, next, freelist, lru) {
49 list_del(&page->lru);
50 __free_page(page);
51 count++;
52 }
53
54 return count;
55}
56
57/* Isolate free pages onto a private freelist. Must hold zone->lock */
58static unsigned long isolate_freepages_block(struct zone *zone,
59 unsigned long blockpfn,
60 struct list_head *freelist)
61{
62 unsigned long zone_end_pfn, end_pfn;
b7aba698 63 int nr_scanned = 0, total_isolated = 0;
748446bb
MG
64 struct page *cursor;
65
66 /* Get the last PFN we should scan for free pages at */
67 zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
68 end_pfn = min(blockpfn + pageblock_nr_pages, zone_end_pfn);
69
70 /* Find the first usable PFN in the block to initialse page cursor */
71 for (; blockpfn < end_pfn; blockpfn++) {
72 if (pfn_valid_within(blockpfn))
73 break;
74 }
75 cursor = pfn_to_page(blockpfn);
76
77 /* Isolate free pages. This assumes the block is valid */
78 for (; blockpfn < end_pfn; blockpfn++, cursor++) {
79 int isolated, i;
80 struct page *page = cursor;
81
82 if (!pfn_valid_within(blockpfn))
83 continue;
b7aba698 84 nr_scanned++;
748446bb
MG
85
86 if (!PageBuddy(page))
87 continue;
88
89 /* Found a free page, break it into order-0 pages */
90 isolated = split_free_page(page);
91 total_isolated += isolated;
92 for (i = 0; i < isolated; i++) {
93 list_add(&page->lru, freelist);
94 page++;
95 }
96
97 /* If a page was split, advance to the end of it */
98 if (isolated) {
99 blockpfn += isolated - 1;
100 cursor += isolated - 1;
101 }
102 }
103
b7aba698 104 trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
748446bb
MG
105 return total_isolated;
106}
107
108/* Returns true if the page is within a block suitable for migration to */
109static bool suitable_migration_target(struct page *page)
110{
111
112 int migratetype = get_pageblock_migratetype(page);
113
114 /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */
115 if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE)
116 return false;
117
118 /* If the page is a large free page, then allow migration */
119 if (PageBuddy(page) && page_order(page) >= pageblock_order)
120 return true;
121
122 /* If the block is MIGRATE_MOVABLE, allow migration */
123 if (migratetype == MIGRATE_MOVABLE)
124 return true;
125
126 /* Otherwise skip the block */
127 return false;
128}
129
03d44192
MN
130static void map_pages(struct list_head *list)
131{
132 struct page *page;
133
134 list_for_each_entry(page, list, lru) {
135 arch_alloc_page(page, 0);
136 kernel_map_pages(page, 1, 1);
137 }
138}
139
748446bb
MG
140/*
141 * Based on information in the current compact_control, find blocks
142 * suitable for isolating free pages from and then isolate them.
143 */
144static void isolate_freepages(struct zone *zone,
145 struct compact_control *cc)
146{
147 struct page *page;
148 unsigned long high_pfn, low_pfn, pfn;
149 unsigned long flags;
150 int nr_freepages = cc->nr_freepages;
151 struct list_head *freelist = &cc->freepages;
152
7454f4ba
MG
153 /*
154 * Initialise the free scanner. The starting point is where we last
155 * scanned from (or the end of the zone if starting). The low point
156 * is the end of the pageblock the migration scanner is using.
157 */
748446bb
MG
158 pfn = cc->free_pfn;
159 low_pfn = cc->migrate_pfn + pageblock_nr_pages;
7454f4ba
MG
160
161 /*
162 * Take care that if the migration scanner is at the end of the zone
163 * that the free scanner does not accidentally move to the next zone
164 * in the next isolation cycle.
165 */
166 high_pfn = min(low_pfn, pfn);
748446bb
MG
167
168 /*
169 * Isolate free pages until enough are available to migrate the
170 * pages on cc->migratepages. We stop searching if the migrate
171 * and free page scanners meet or enough free pages are isolated.
172 */
748446bb
MG
173 for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
174 pfn -= pageblock_nr_pages) {
175 unsigned long isolated;
176
177 if (!pfn_valid(pfn))
178 continue;
179
180 /*
181 * Check for overlapping nodes/zones. It's possible on some
182 * configurations to have a setup like
183 * node0 node1 node0
184 * i.e. it's possible that all pages within a zones range of
185 * pages do not belong to a single zone.
186 */
187 page = pfn_to_page(pfn);
188 if (page_zone(page) != zone)
189 continue;
190
191 /* Check the block is suitable for migration */
192 if (!suitable_migration_target(page))
193 continue;
194
602605a4
MG
195 /*
196 * Found a block suitable for isolating free pages from. Now
197 * we disabled interrupts, double check things are ok and
198 * isolate the pages. This is to minimise the time IRQs
199 * are disabled
200 */
201 isolated = 0;
202 spin_lock_irqsave(&zone->lock, flags);
203 if (suitable_migration_target(page)) {
204 isolated = isolate_freepages_block(zone, pfn, freelist);
205 nr_freepages += isolated;
206 }
207 spin_unlock_irqrestore(&zone->lock, flags);
748446bb
MG
208
209 /*
210 * Record the highest PFN we isolated pages from. When next
211 * looking for free pages, the search will restart here as
212 * page migration may have returned some pages to the allocator
213 */
214 if (isolated)
215 high_pfn = max(high_pfn, pfn);
216 }
748446bb
MG
217
218 /* split_free_page does not map the pages */
03d44192 219 map_pages(freelist);
748446bb
MG
220
221 cc->free_pfn = high_pfn;
222 cc->nr_freepages = nr_freepages;
223}
224
225/* Update the number of anon and file isolated pages in the zone */
226static void acct_isolated(struct zone *zone, struct compact_control *cc)
227{
228 struct page *page;
b9e84ac1 229 unsigned int count[2] = { 0, };
748446bb 230
b9e84ac1
MK
231 list_for_each_entry(page, &cc->migratepages, lru)
232 count[!!page_is_file_cache(page)]++;
748446bb 233
b9e84ac1
MK
234 __mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
235 __mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
748446bb
MG
236}
237
238/* Similar to reclaim, but different enough that they don't share logic */
239static bool too_many_isolated(struct zone *zone)
240{
bc693045 241 unsigned long active, inactive, isolated;
748446bb
MG
242
243 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
244 zone_page_state(zone, NR_INACTIVE_ANON);
bc693045
MK
245 active = zone_page_state(zone, NR_ACTIVE_FILE) +
246 zone_page_state(zone, NR_ACTIVE_ANON);
748446bb
MG
247 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
248 zone_page_state(zone, NR_ISOLATED_ANON);
249
bc693045 250 return isolated > (inactive + active) / 2;
748446bb
MG
251}
252
f9e35b3b
MG
253/* possible outcome of isolate_migratepages */
254typedef enum {
255 ISOLATE_ABORT, /* Abort compaction now */
256 ISOLATE_NONE, /* No pages isolated, continue scanning */
257 ISOLATE_SUCCESS, /* Pages isolated, migrate */
258} isolate_migrate_t;
259
2fe86e00
MN
260/**
261 * isolate_migratepages_range() - isolate all migrate-able pages in range.
262 * @zone: Zone pages are in.
263 * @cc: Compaction control structure.
264 * @low_pfn: The first PFN of the range.
265 * @end_pfn: The one-past-the-last PFN of the range.
266 *
267 * Isolate all pages that can be migrated from the range specified by
268 * [low_pfn, end_pfn). Returns zero if there is a fatal signal
269 * pending), otherwise PFN of the first page that was not scanned
270 * (which may be both less, equal to or more then end_pfn).
271 *
272 * Assumes that cc->migratepages is empty and cc->nr_migratepages is
273 * zero.
274 *
275 * Apart from cc->migratepages and cc->nr_migratetypes this function
276 * does not modify any cc's fields, in particular it does not modify
277 * (or read for that matter) cc->migrate_pfn.
748446bb 278 */
2fe86e00
MN
279static unsigned long
280isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
281 unsigned long low_pfn, unsigned long end_pfn)
748446bb 282{
9927af74 283 unsigned long last_pageblock_nr = 0, pageblock_nr;
b7aba698 284 unsigned long nr_scanned = 0, nr_isolated = 0;
748446bb 285 struct list_head *migratelist = &cc->migratepages;
39deaf85 286 isolate_mode_t mode = ISOLATE_ACTIVE|ISOLATE_INACTIVE;
748446bb 287
748446bb
MG
288 /*
289 * Ensure that there are not too many pages isolated from the LRU
290 * list by either parallel reclaimers or compaction. If there are,
291 * delay for some time until fewer pages are isolated
292 */
293 while (unlikely(too_many_isolated(zone))) {
f9e35b3b
MG
294 /* async migration should just abort */
295 if (!cc->sync)
2fe86e00 296 return 0;
f9e35b3b 297
748446bb
MG
298 congestion_wait(BLK_RW_ASYNC, HZ/10);
299
300 if (fatal_signal_pending(current))
2fe86e00 301 return 0;
748446bb
MG
302 }
303
304 /* Time to isolate some pages for migration */
b2eef8c0 305 cond_resched();
748446bb
MG
306 spin_lock_irq(&zone->lru_lock);
307 for (; low_pfn < end_pfn; low_pfn++) {
308 struct page *page;
b2eef8c0
AA
309 bool locked = true;
310
311 /* give a chance to irqs before checking need_resched() */
312 if (!((low_pfn+1) % SWAP_CLUSTER_MAX)) {
313 spin_unlock_irq(&zone->lru_lock);
314 locked = false;
315 }
316 if (need_resched() || spin_is_contended(&zone->lru_lock)) {
317 if (locked)
318 spin_unlock_irq(&zone->lru_lock);
319 cond_resched();
320 spin_lock_irq(&zone->lru_lock);
321 if (fatal_signal_pending(current))
322 break;
323 } else if (!locked)
324 spin_lock_irq(&zone->lru_lock);
325
0bf380bc
MG
326 /*
327 * migrate_pfn does not necessarily start aligned to a
328 * pageblock. Ensure that pfn_valid is called when moving
329 * into a new MAX_ORDER_NR_PAGES range in case of large
330 * memory holes within the zone
331 */
332 if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) {
333 if (!pfn_valid(low_pfn)) {
334 low_pfn += MAX_ORDER_NR_PAGES - 1;
335 continue;
336 }
337 }
338
748446bb
MG
339 if (!pfn_valid_within(low_pfn))
340 continue;
b7aba698 341 nr_scanned++;
748446bb 342
dc908600
MG
343 /*
344 * Get the page and ensure the page is within the same zone.
345 * See the comment in isolate_freepages about overlapping
346 * nodes. It is deliberate that the new zone lock is not taken
347 * as memory compaction should not move pages between nodes.
348 */
748446bb 349 page = pfn_to_page(low_pfn);
dc908600
MG
350 if (page_zone(page) != zone)
351 continue;
352
353 /* Skip if free */
748446bb
MG
354 if (PageBuddy(page))
355 continue;
356
9927af74
MG
357 /*
358 * For async migration, also only scan in MOVABLE blocks. Async
359 * migration is optimistic to see if the minimum amount of work
360 * satisfies the allocation
361 */
362 pageblock_nr = low_pfn >> pageblock_order;
363 if (!cc->sync && last_pageblock_nr != pageblock_nr &&
364 get_pageblock_migratetype(page) != MIGRATE_MOVABLE) {
365 low_pfn += pageblock_nr_pages;
366 low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1;
367 last_pageblock_nr = pageblock_nr;
368 continue;
369 }
370
bc835011
AA
371 if (!PageLRU(page))
372 continue;
373
374 /*
375 * PageLRU is set, and lru_lock excludes isolation,
376 * splitting and collapsing (collapsing has already
377 * happened if PageLRU is set).
378 */
379 if (PageTransHuge(page)) {
380 low_pfn += (1 << compound_order(page)) - 1;
381 continue;
382 }
383
c8244935
MG
384 if (!cc->sync)
385 mode |= ISOLATE_ASYNC_MIGRATE;
386
748446bb 387 /* Try isolate the page */
39deaf85 388 if (__isolate_lru_page(page, mode, 0) != 0)
748446bb
MG
389 continue;
390
bc835011
AA
391 VM_BUG_ON(PageTransCompound(page));
392
748446bb
MG
393 /* Successfully isolated */
394 del_page_from_lru_list(zone, page, page_lru(page));
395 list_add(&page->lru, migratelist);
748446bb 396 cc->nr_migratepages++;
b7aba698 397 nr_isolated++;
748446bb
MG
398
399 /* Avoid isolating too much */
31b8384a
HD
400 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
401 ++low_pfn;
748446bb 402 break;
31b8384a 403 }
748446bb
MG
404 }
405
406 acct_isolated(zone, cc);
407
408 spin_unlock_irq(&zone->lru_lock);
748446bb 409
b7aba698
MG
410 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
411
2fe86e00
MN
412 return low_pfn;
413}
414
415/*
416 * Isolate all pages that can be migrated from the block pointed to by
417 * the migrate scanner within compact_control.
418 */
419static isolate_migrate_t isolate_migratepages(struct zone *zone,
420 struct compact_control *cc)
421{
422 unsigned long low_pfn, end_pfn;
423
424 /* Do not scan outside zone boundaries */
425 low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
426
427 /* Only scan within a pageblock boundary */
428 end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
429
430 /* Do not cross the free scanner or scan within a memory hole */
431 if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
432 cc->migrate_pfn = end_pfn;
433 return ISOLATE_NONE;
434 }
435
436 /* Perform the isolation */
437 low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn);
438 if (!low_pfn)
439 return ISOLATE_ABORT;
440
441 cc->migrate_pfn = low_pfn;
442
f9e35b3b 443 return ISOLATE_SUCCESS;
748446bb
MG
444}
445
446/*
447 * This is a migrate-callback that "allocates" freepages by taking pages
448 * from the isolated freelists in the block we are migrating to.
449 */
450static struct page *compaction_alloc(struct page *migratepage,
451 unsigned long data,
452 int **result)
453{
454 struct compact_control *cc = (struct compact_control *)data;
455 struct page *freepage;
456
457 /* Isolate free pages if necessary */
458 if (list_empty(&cc->freepages)) {
459 isolate_freepages(cc->zone, cc);
460
461 if (list_empty(&cc->freepages))
462 return NULL;
463 }
464
465 freepage = list_entry(cc->freepages.next, struct page, lru);
466 list_del(&freepage->lru);
467 cc->nr_freepages--;
468
469 return freepage;
470}
471
472/*
473 * We cannot control nr_migratepages and nr_freepages fully when migration is
474 * running as migrate_pages() has no knowledge of compact_control. When
475 * migration is complete, we count the number of pages on the lists by hand.
476 */
477static void update_nr_listpages(struct compact_control *cc)
478{
479 int nr_migratepages = 0;
480 int nr_freepages = 0;
481 struct page *page;
482
483 list_for_each_entry(page, &cc->migratepages, lru)
484 nr_migratepages++;
485 list_for_each_entry(page, &cc->freepages, lru)
486 nr_freepages++;
487
488 cc->nr_migratepages = nr_migratepages;
489 cc->nr_freepages = nr_freepages;
490}
491
492static int compact_finished(struct zone *zone,
5a03b051 493 struct compact_control *cc)
748446bb 494{
56de7263 495 unsigned int order;
5a03b051 496 unsigned long watermark;
56de7263 497
748446bb
MG
498 if (fatal_signal_pending(current))
499 return COMPACT_PARTIAL;
500
501 /* Compaction run completes if the migrate and free scanner meet */
502 if (cc->free_pfn <= cc->migrate_pfn)
503 return COMPACT_COMPLETE;
504
82478fb7
JW
505 /*
506 * order == -1 is expected when compacting via
507 * /proc/sys/vm/compact_memory
508 */
56de7263
MG
509 if (cc->order == -1)
510 return COMPACT_CONTINUE;
511
3957c776
MH
512 /* Compaction run is not finished if the watermark is not met */
513 watermark = low_wmark_pages(zone);
514 watermark += (1 << cc->order);
515
516 if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
517 return COMPACT_CONTINUE;
518
56de7263
MG
519 /* Direct compactor: Is a suitable page free? */
520 for (order = cc->order; order < MAX_ORDER; order++) {
521 /* Job done if page is free of the right migratetype */
522 if (!list_empty(&zone->free_area[order].free_list[cc->migratetype]))
523 return COMPACT_PARTIAL;
524
525 /* Job done if allocation would set block type */
526 if (order >= pageblock_order && zone->free_area[order].nr_free)
527 return COMPACT_PARTIAL;
528 }
529
748446bb
MG
530 return COMPACT_CONTINUE;
531}
532
3e7d3449
MG
533/*
534 * compaction_suitable: Is this suitable to run compaction on this zone now?
535 * Returns
536 * COMPACT_SKIPPED - If there are too few free pages for compaction
537 * COMPACT_PARTIAL - If the allocation would succeed without compaction
538 * COMPACT_CONTINUE - If compaction should run now
539 */
540unsigned long compaction_suitable(struct zone *zone, int order)
541{
542 int fragindex;
543 unsigned long watermark;
544
3957c776
MH
545 /*
546 * order == -1 is expected when compacting via
547 * /proc/sys/vm/compact_memory
548 */
549 if (order == -1)
550 return COMPACT_CONTINUE;
551
3e7d3449
MG
552 /*
553 * Watermarks for order-0 must be met for compaction. Note the 2UL.
554 * This is because during migration, copies of pages need to be
555 * allocated and for a short time, the footprint is higher
556 */
557 watermark = low_wmark_pages(zone) + (2UL << order);
558 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
559 return COMPACT_SKIPPED;
560
561 /*
562 * fragmentation index determines if allocation failures are due to
563 * low memory or external fragmentation
564 *
a582a738
SL
565 * index of -1000 implies allocations might succeed depending on
566 * watermarks
3e7d3449
MG
567 * index towards 0 implies failure is due to lack of memory
568 * index towards 1000 implies failure is due to fragmentation
569 *
570 * Only compact if a failure would be due to fragmentation.
571 */
572 fragindex = fragmentation_index(zone, order);
573 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
574 return COMPACT_SKIPPED;
575
a582a738
SL
576 if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
577 0, 0))
3e7d3449
MG
578 return COMPACT_PARTIAL;
579
580 return COMPACT_CONTINUE;
581}
582
748446bb
MG
583static int compact_zone(struct zone *zone, struct compact_control *cc)
584{
585 int ret;
586
3e7d3449
MG
587 ret = compaction_suitable(zone, cc->order);
588 switch (ret) {
589 case COMPACT_PARTIAL:
590 case COMPACT_SKIPPED:
591 /* Compaction is likely to fail */
592 return ret;
593 case COMPACT_CONTINUE:
594 /* Fall through to compaction */
595 ;
596 }
597
748446bb
MG
598 /* Setup to move all movable pages to the end of the zone */
599 cc->migrate_pfn = zone->zone_start_pfn;
600 cc->free_pfn = cc->migrate_pfn + zone->spanned_pages;
601 cc->free_pfn &= ~(pageblock_nr_pages-1);
602
603 migrate_prep_local();
604
605 while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
606 unsigned long nr_migrate, nr_remaining;
9d502c1c 607 int err;
748446bb 608
f9e35b3b
MG
609 switch (isolate_migratepages(zone, cc)) {
610 case ISOLATE_ABORT:
611 ret = COMPACT_PARTIAL;
612 goto out;
613 case ISOLATE_NONE:
748446bb 614 continue;
f9e35b3b
MG
615 case ISOLATE_SUCCESS:
616 ;
617 }
748446bb
MG
618
619 nr_migrate = cc->nr_migratepages;
9d502c1c 620 err = migrate_pages(&cc->migratepages, compaction_alloc,
7f0f2496 621 (unsigned long)cc, false,
a6bc32b8 622 cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC);
748446bb
MG
623 update_nr_listpages(cc);
624 nr_remaining = cc->nr_migratepages;
625
626 count_vm_event(COMPACTBLOCKS);
627 count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining);
628 if (nr_remaining)
629 count_vm_events(COMPACTPAGEFAILED, nr_remaining);
b7aba698
MG
630 trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
631 nr_remaining);
748446bb
MG
632
633 /* Release LRU pages not migrated */
9d502c1c 634 if (err) {
748446bb
MG
635 putback_lru_pages(&cc->migratepages);
636 cc->nr_migratepages = 0;
637 }
638
639 }
640
f9e35b3b 641out:
748446bb
MG
642 /* Release free pages and check accounting */
643 cc->nr_freepages -= release_freepages(&cc->freepages);
644 VM_BUG_ON(cc->nr_freepages != 0);
645
646 return ret;
647}
76ab0f53 648
d43a87e6 649static unsigned long compact_zone_order(struct zone *zone,
5a03b051 650 int order, gfp_t gfp_mask,
d527caf2 651 bool sync)
56de7263
MG
652{
653 struct compact_control cc = {
654 .nr_freepages = 0,
655 .nr_migratepages = 0,
656 .order = order,
657 .migratetype = allocflags_to_migratetype(gfp_mask),
658 .zone = zone,
77f1fe6b 659 .sync = sync,
56de7263
MG
660 };
661 INIT_LIST_HEAD(&cc.freepages);
662 INIT_LIST_HEAD(&cc.migratepages);
663
664 return compact_zone(zone, &cc);
665}
666
5e771905
MG
667int sysctl_extfrag_threshold = 500;
668
56de7263
MG
669/**
670 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
671 * @zonelist: The zonelist used for the current allocation
672 * @order: The order of the current allocation
673 * @gfp_mask: The GFP mask of the current allocation
674 * @nodemask: The allowed nodes to allocate from
77f1fe6b 675 * @sync: Whether migration is synchronous or not
56de7263
MG
676 *
677 * This is the main entry point for direct page compaction.
678 */
679unsigned long try_to_compact_pages(struct zonelist *zonelist,
77f1fe6b
MG
680 int order, gfp_t gfp_mask, nodemask_t *nodemask,
681 bool sync)
56de7263
MG
682{
683 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
684 int may_enter_fs = gfp_mask & __GFP_FS;
685 int may_perform_io = gfp_mask & __GFP_IO;
56de7263
MG
686 struct zoneref *z;
687 struct zone *zone;
688 int rc = COMPACT_SKIPPED;
689
690 /*
691 * Check whether it is worth even starting compaction. The order check is
692 * made because an assumption is made that the page allocator can satisfy
693 * the "cheaper" orders without taking special steps
694 */
c5a73c3d 695 if (!order || !may_enter_fs || !may_perform_io)
56de7263
MG
696 return rc;
697
698 count_vm_event(COMPACTSTALL);
699
700 /* Compact each zone in the list */
701 for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
702 nodemask) {
56de7263
MG
703 int status;
704
d527caf2 705 status = compact_zone_order(zone, order, gfp_mask, sync);
56de7263
MG
706 rc = max(status, rc);
707
3e7d3449
MG
708 /* If a normal allocation would succeed, stop compacting */
709 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0, 0))
56de7263
MG
710 break;
711 }
712
713 return rc;
714}
715
716
76ab0f53 717/* Compact all zones within a node */
7be62de9 718static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
76ab0f53
MG
719{
720 int zoneid;
76ab0f53
MG
721 struct zone *zone;
722
76ab0f53 723 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
76ab0f53
MG
724
725 zone = &pgdat->node_zones[zoneid];
726 if (!populated_zone(zone))
727 continue;
728
7be62de9
RR
729 cc->nr_freepages = 0;
730 cc->nr_migratepages = 0;
731 cc->zone = zone;
732 INIT_LIST_HEAD(&cc->freepages);
733 INIT_LIST_HEAD(&cc->migratepages);
76ab0f53 734
aad6ec37 735 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
7be62de9 736 compact_zone(zone, cc);
76ab0f53 737
aff62249
RR
738 if (cc->order > 0) {
739 int ok = zone_watermark_ok(zone, cc->order,
740 low_wmark_pages(zone), 0, 0);
741 if (ok && cc->order > zone->compact_order_failed)
742 zone->compact_order_failed = cc->order + 1;
743 /* Currently async compaction is never deferred. */
744 else if (!ok && cc->sync)
745 defer_compaction(zone, cc->order);
746 }
747
7be62de9
RR
748 VM_BUG_ON(!list_empty(&cc->freepages));
749 VM_BUG_ON(!list_empty(&cc->migratepages));
76ab0f53
MG
750 }
751
752 return 0;
753}
754
7be62de9
RR
755int compact_pgdat(pg_data_t *pgdat, int order)
756{
757 struct compact_control cc = {
758 .order = order,
759 .sync = false,
760 };
761
762 return __compact_pgdat(pgdat, &cc);
763}
764
765static int compact_node(int nid)
766{
7be62de9
RR
767 struct compact_control cc = {
768 .order = -1,
769 .sync = true,
770 };
771
8575ec29 772 return __compact_pgdat(NODE_DATA(nid), &cc);
7be62de9
RR
773}
774
76ab0f53
MG
775/* Compact all nodes in the system */
776static int compact_nodes(void)
777{
778 int nid;
779
8575ec29
HD
780 /* Flush pending updates to the LRU lists */
781 lru_add_drain_all();
782
76ab0f53
MG
783 for_each_online_node(nid)
784 compact_node(nid);
785
786 return COMPACT_COMPLETE;
787}
788
789/* The written value is actually unused, all memory is compacted */
790int sysctl_compact_memory;
791
792/* This is the entry point for compacting all nodes via /proc/sys/vm */
793int sysctl_compaction_handler(struct ctl_table *table, int write,
794 void __user *buffer, size_t *length, loff_t *ppos)
795{
796 if (write)
797 return compact_nodes();
798
799 return 0;
800}
ed4a6d7f 801
5e771905
MG
802int sysctl_extfrag_handler(struct ctl_table *table, int write,
803 void __user *buffer, size_t *length, loff_t *ppos)
804{
805 proc_dointvec_minmax(table, write, buffer, length, ppos);
806
807 return 0;
808}
809
ed4a6d7f 810#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
10fbcf4c
KS
811ssize_t sysfs_compact_node(struct device *dev,
812 struct device_attribute *attr,
ed4a6d7f
MG
813 const char *buf, size_t count)
814{
8575ec29
HD
815 int nid = dev->id;
816
817 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
818 /* Flush pending updates to the LRU lists */
819 lru_add_drain_all();
820
821 compact_node(nid);
822 }
ed4a6d7f
MG
823
824 return count;
825}
10fbcf4c 826static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
ed4a6d7f
MG
827
828int compaction_register_node(struct node *node)
829{
10fbcf4c 830 return device_create_file(&node->dev, &dev_attr_compact);
ed4a6d7f
MG
831}
832
833void compaction_unregister_node(struct node *node)
834{
10fbcf4c 835 return device_remove_file(&node->dev, &dev_attr_compact);
ed4a6d7f
MG
836}
837#endif /* CONFIG_SYSFS && CONFIG_NUMA */