aboutsummaryrefslogtreecommitdiffstats
path: root/mm/compaction.c
diff options
context:
space:
mode:
Diffstat (limited to 'mm/compaction.c')
-rw-r--r--mm/compaction.c432
1 files changed, 272 insertions, 160 deletions
diff --git a/mm/compaction.c b/mm/compaction.c
index 74a8c825ff28..2f42d9528539 100644
--- a/mm/compaction.c
+++ b/mm/compaction.c
@@ -16,30 +16,11 @@
16#include <linux/sysfs.h> 16#include <linux/sysfs.h>
17#include "internal.h" 17#include "internal.h"
18 18
19#if defined CONFIG_COMPACTION || defined CONFIG_CMA
20
19#define CREATE_TRACE_POINTS 21#define CREATE_TRACE_POINTS
20#include <trace/events/compaction.h> 22#include <trace/events/compaction.h>
21 23
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 */
36 bool sync; /* Synchronous migration */
37
38 int order; /* order a direct compactor needs */
39 int migratetype; /* MOVABLE, RECLAIMABLE etc */
40 struct zone *zone;
41};
42
43static unsigned long release_freepages(struct list_head *freelist) 24static unsigned long release_freepages(struct list_head *freelist)
44{ 25{
45 struct page *page, *next; 26 struct page *page, *next;
@@ -54,24 +35,35 @@ static unsigned long release_freepages(struct list_head *freelist)
54 return count; 35 return count;
55} 36}
56 37
57/* Isolate free pages onto a private freelist. Must hold zone->lock */ 38static void map_pages(struct list_head *list)
58static unsigned long isolate_freepages_block(struct zone *zone, 39{
59 unsigned long blockpfn, 40 struct page *page;
60 struct list_head *freelist) 41
42 list_for_each_entry(page, list, lru) {
43 arch_alloc_page(page, 0);
44 kernel_map_pages(page, 1, 1);
45 }
46}
47
48static inline bool migrate_async_suitable(int migratetype)
49{
50 return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
51}
52
53/*
54 * Isolate free pages onto a private freelist. Caller must hold zone->lock.
55 * If @strict is true, will abort returning 0 on any invalid PFNs or non-free
56 * pages inside of the pageblock (even though it may still end up isolating
57 * some pages).
58 */
59static unsigned long isolate_freepages_block(unsigned long blockpfn,
60 unsigned long end_pfn,
61 struct list_head *freelist,
62 bool strict)
61{ 63{
62 unsigned long zone_end_pfn, end_pfn;
63 int nr_scanned = 0, total_isolated = 0; 64 int nr_scanned = 0, total_isolated = 0;
64 struct page *cursor; 65 struct page *cursor;
65 66
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); 67 cursor = pfn_to_page(blockpfn);
76 68
77 /* Isolate free pages. This assumes the block is valid */ 69 /* Isolate free pages. This assumes the block is valid */
@@ -79,15 +71,23 @@ static unsigned long isolate_freepages_block(struct zone *zone,
79 int isolated, i; 71 int isolated, i;
80 struct page *page = cursor; 72 struct page *page = cursor;
81 73
82 if (!pfn_valid_within(blockpfn)) 74 if (!pfn_valid_within(blockpfn)) {
75 if (strict)
76 return 0;
83 continue; 77 continue;
78 }
84 nr_scanned++; 79 nr_scanned++;
85 80
86 if (!PageBuddy(page)) 81 if (!PageBuddy(page)) {
82 if (strict)
83 return 0;
87 continue; 84 continue;
85 }
88 86
89 /* Found a free page, break it into order-0 pages */ 87 /* Found a free page, break it into order-0 pages */
90 isolated = split_free_page(page); 88 isolated = split_free_page(page);
89 if (!isolated && strict)
90 return 0;
91 total_isolated += isolated; 91 total_isolated += isolated;
92 for (i = 0; i < isolated; i++) { 92 for (i = 0; i < isolated; i++) {
93 list_add(&page->lru, freelist); 93 list_add(&page->lru, freelist);
@@ -105,114 +105,71 @@ static unsigned long isolate_freepages_block(struct zone *zone,
105 return total_isolated; 105 return total_isolated;
106} 106}
107 107
108/* Returns true if the page is within a block suitable for migration to */ 108/**
109static bool suitable_migration_target(struct page *page) 109 * isolate_freepages_range() - isolate free pages.
110{ 110 * @start_pfn: The first PFN to start isolating.
111 111 * @end_pfn: The one-past-last PFN.
112 int migratetype = get_pageblock_migratetype(page); 112 *
113 113 * Non-free pages, invalid PFNs, or zone boundaries within the
114 /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */ 114 * [start_pfn, end_pfn) range are considered errors, cause function to
115 if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE) 115 * undo its actions and return zero.
116 return false; 116 *
117 117 * Otherwise, function returns one-past-the-last PFN of isolated page
118 /* If the page is a large free page, then allow migration */ 118 * (which may be greater then end_pfn if end fell in a middle of
119 if (PageBuddy(page) && page_order(page) >= pageblock_order) 119 * a free page).
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
130/*
131 * Based on information in the current compact_control, find blocks
132 * suitable for isolating free pages from and then isolate them.
133 */ 120 */
134static void isolate_freepages(struct zone *zone, 121unsigned long
135 struct compact_control *cc) 122isolate_freepages_range(unsigned long start_pfn, unsigned long end_pfn)
136{ 123{
137 struct page *page; 124 unsigned long isolated, pfn, block_end_pfn, flags;
138 unsigned long high_pfn, low_pfn, pfn; 125 struct zone *zone = NULL;
139 unsigned long flags; 126 LIST_HEAD(freelist);
140 int nr_freepages = cc->nr_freepages;
141 struct list_head *freelist = &cc->freepages;
142
143 /*
144 * Initialise the free scanner. The starting point is where we last
145 * scanned from (or the end of the zone if starting). The low point
146 * is the end of the pageblock the migration scanner is using.
147 */
148 pfn = cc->free_pfn;
149 low_pfn = cc->migrate_pfn + pageblock_nr_pages;
150 127
151 /* 128 if (pfn_valid(start_pfn))
152 * Take care that if the migration scanner is at the end of the zone 129 zone = page_zone(pfn_to_page(start_pfn));
153 * that the free scanner does not accidentally move to the next zone
154 * in the next isolation cycle.
155 */
156 high_pfn = min(low_pfn, pfn);
157 130
158 /* 131 for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) {
159 * Isolate free pages until enough are available to migrate the 132 if (!pfn_valid(pfn) || zone != page_zone(pfn_to_page(pfn)))
160 * pages on cc->migratepages. We stop searching if the migrate 133 break;
161 * and free page scanners meet or enough free pages are isolated.
162 */
163 for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
164 pfn -= pageblock_nr_pages) {
165 unsigned long isolated;
166
167 if (!pfn_valid(pfn))
168 continue;
169 134
170 /* 135 /*
171 * Check for overlapping nodes/zones. It's possible on some 136 * On subsequent iterations ALIGN() is actually not needed,
172 * configurations to have a setup like 137 * but we keep it that we not to complicate the code.
173 * node0 node1 node0
174 * i.e. it's possible that all pages within a zones range of
175 * pages do not belong to a single zone.
176 */ 138 */
177 page = pfn_to_page(pfn); 139 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
178 if (page_zone(page) != zone) 140 block_end_pfn = min(block_end_pfn, end_pfn);
179 continue;
180 141
181 /* Check the block is suitable for migration */ 142 spin_lock_irqsave(&zone->lock, flags);
182 if (!suitable_migration_target(page)) 143 isolated = isolate_freepages_block(pfn, block_end_pfn,
183 continue; 144 &freelist, true);
145 spin_unlock_irqrestore(&zone->lock, flags);
184 146
185 /* 147 /*
186 * Found a block suitable for isolating free pages from. Now 148 * In strict mode, isolate_freepages_block() returns 0 if
187 * we disabled interrupts, double check things are ok and 149 * there are any holes in the block (ie. invalid PFNs or
188 * isolate the pages. This is to minimise the time IRQs 150 * non-free pages).
189 * are disabled
190 */ 151 */
191 isolated = 0; 152 if (!isolated)
192 spin_lock_irqsave(&zone->lock, flags); 153 break;
193 if (suitable_migration_target(page)) {
194 isolated = isolate_freepages_block(zone, pfn, freelist);
195 nr_freepages += isolated;
196 }
197 spin_unlock_irqrestore(&zone->lock, flags);
198 154
199 /* 155 /*
200 * Record the highest PFN we isolated pages from. When next 156 * If we managed to isolate pages, it is always (1 << n) *
201 * looking for free pages, the search will restart here as 157 * pageblock_nr_pages for some non-negative n. (Max order
202 * page migration may have returned some pages to the allocator 158 * page may span two pageblocks).
203 */ 159 */
204 if (isolated)
205 high_pfn = max(high_pfn, pfn);
206 } 160 }
207 161
208 /* split_free_page does not map the pages */ 162 /* split_free_page does not map the pages */
209 list_for_each_entry(page, freelist, lru) { 163 map_pages(&freelist);
210 arch_alloc_page(page, 0); 164
211 kernel_map_pages(page, 1, 1); 165 if (pfn < end_pfn) {
166 /* Loop terminated early, cleanup. */
167 release_freepages(&freelist);
168 return 0;
212 } 169 }
213 170
214 cc->free_pfn = high_pfn; 171 /* We don't use freelists for anything. */
215 cc->nr_freepages = nr_freepages; 172 return pfn;
216} 173}
217 174
218/* Update the number of anon and file isolated pages in the zone */ 175/* Update the number of anon and file isolated pages in the zone */
@@ -243,37 +200,34 @@ static bool too_many_isolated(struct zone *zone)
243 return isolated > (inactive + active) / 2; 200 return isolated > (inactive + active) / 2;
244} 201}
245 202
246/* possible outcome of isolate_migratepages */ 203/**
247typedef enum { 204 * isolate_migratepages_range() - isolate all migrate-able pages in range.
248 ISOLATE_ABORT, /* Abort compaction now */ 205 * @zone: Zone pages are in.
249 ISOLATE_NONE, /* No pages isolated, continue scanning */ 206 * @cc: Compaction control structure.
250 ISOLATE_SUCCESS, /* Pages isolated, migrate */ 207 * @low_pfn: The first PFN of the range.
251} isolate_migrate_t; 208 * @end_pfn: The one-past-the-last PFN of the range.
252 209 *
253/* 210 * Isolate all pages that can be migrated from the range specified by
254 * Isolate all pages that can be migrated from the block pointed to by 211 * [low_pfn, end_pfn). Returns zero if there is a fatal signal
255 * the migrate scanner within compact_control. 212 * pending), otherwise PFN of the first page that was not scanned
213 * (which may be both less, equal to or more then end_pfn).
214 *
215 * Assumes that cc->migratepages is empty and cc->nr_migratepages is
216 * zero.
217 *
218 * Apart from cc->migratepages and cc->nr_migratetypes this function
219 * does not modify any cc's fields, in particular it does not modify
220 * (or read for that matter) cc->migrate_pfn.
256 */ 221 */
257static isolate_migrate_t isolate_migratepages(struct zone *zone, 222unsigned long
258 struct compact_control *cc) 223isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
224 unsigned long low_pfn, unsigned long end_pfn)
259{ 225{
260 unsigned long low_pfn, end_pfn;
261 unsigned long last_pageblock_nr = 0, pageblock_nr; 226 unsigned long last_pageblock_nr = 0, pageblock_nr;
262 unsigned long nr_scanned = 0, nr_isolated = 0; 227 unsigned long nr_scanned = 0, nr_isolated = 0;
263 struct list_head *migratelist = &cc->migratepages; 228 struct list_head *migratelist = &cc->migratepages;
264 isolate_mode_t mode = ISOLATE_ACTIVE|ISOLATE_INACTIVE; 229 isolate_mode_t mode = 0;
265 230 struct lruvec *lruvec;
266 /* Do not scan outside zone boundaries */
267 low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
268
269 /* Only scan within a pageblock boundary */
270 end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
271
272 /* Do not cross the free scanner or scan within a memory hole */
273 if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
274 cc->migrate_pfn = end_pfn;
275 return ISOLATE_NONE;
276 }
277 231
278 /* 232 /*
279 * Ensure that there are not too many pages isolated from the LRU 233 * Ensure that there are not too many pages isolated from the LRU
@@ -283,12 +237,12 @@ static isolate_migrate_t isolate_migratepages(struct zone *zone,
283 while (unlikely(too_many_isolated(zone))) { 237 while (unlikely(too_many_isolated(zone))) {
284 /* async migration should just abort */ 238 /* async migration should just abort */
285 if (!cc->sync) 239 if (!cc->sync)
286 return ISOLATE_ABORT; 240 return 0;
287 241
288 congestion_wait(BLK_RW_ASYNC, HZ/10); 242 congestion_wait(BLK_RW_ASYNC, HZ/10);
289 243
290 if (fatal_signal_pending(current)) 244 if (fatal_signal_pending(current))
291 return ISOLATE_ABORT; 245 return 0;
292 } 246 }
293 247
294 /* Time to isolate some pages for migration */ 248 /* Time to isolate some pages for migration */
@@ -351,7 +305,7 @@ static isolate_migrate_t isolate_migratepages(struct zone *zone,
351 */ 305 */
352 pageblock_nr = low_pfn >> pageblock_order; 306 pageblock_nr = low_pfn >> pageblock_order;
353 if (!cc->sync && last_pageblock_nr != pageblock_nr && 307 if (!cc->sync && last_pageblock_nr != pageblock_nr &&
354 get_pageblock_migratetype(page) != MIGRATE_MOVABLE) { 308 !migrate_async_suitable(get_pageblock_migratetype(page))) {
355 low_pfn += pageblock_nr_pages; 309 low_pfn += pageblock_nr_pages;
356 low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1; 310 low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1;
357 last_pageblock_nr = pageblock_nr; 311 last_pageblock_nr = pageblock_nr;
@@ -374,14 +328,16 @@ static isolate_migrate_t isolate_migratepages(struct zone *zone,
374 if (!cc->sync) 328 if (!cc->sync)
375 mode |= ISOLATE_ASYNC_MIGRATE; 329 mode |= ISOLATE_ASYNC_MIGRATE;
376 330
331 lruvec = mem_cgroup_page_lruvec(page, zone);
332
377 /* Try isolate the page */ 333 /* Try isolate the page */
378 if (__isolate_lru_page(page, mode, 0) != 0) 334 if (__isolate_lru_page(page, mode) != 0)
379 continue; 335 continue;
380 336
381 VM_BUG_ON(PageTransCompound(page)); 337 VM_BUG_ON(PageTransCompound(page));
382 338
383 /* Successfully isolated */ 339 /* Successfully isolated */
384 del_page_from_lru_list(zone, page, page_lru(page)); 340 del_page_from_lru_list(page, lruvec, page_lru(page));
385 list_add(&page->lru, migratelist); 341 list_add(&page->lru, migratelist);
386 cc->nr_migratepages++; 342 cc->nr_migratepages++;
387 nr_isolated++; 343 nr_isolated++;
@@ -396,11 +352,124 @@ static isolate_migrate_t isolate_migratepages(struct zone *zone,
396 acct_isolated(zone, cc); 352 acct_isolated(zone, cc);
397 353
398 spin_unlock_irq(&zone->lru_lock); 354 spin_unlock_irq(&zone->lru_lock);
399 cc->migrate_pfn = low_pfn;
400 355
401 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated); 356 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
402 357
403 return ISOLATE_SUCCESS; 358 return low_pfn;
359}
360
361#endif /* CONFIG_COMPACTION || CONFIG_CMA */
362#ifdef CONFIG_COMPACTION
363
364/* Returns true if the page is within a block suitable for migration to */
365static bool suitable_migration_target(struct page *page)
366{
367
368 int migratetype = get_pageblock_migratetype(page);
369
370 /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */
371 if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE)
372 return false;
373
374 /* If the page is a large free page, then allow migration */
375 if (PageBuddy(page) && page_order(page) >= pageblock_order)
376 return true;
377
378 /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
379 if (migrate_async_suitable(migratetype))
380 return true;
381
382 /* Otherwise skip the block */
383 return false;
384}
385
386/*
387 * Based on information in the current compact_control, find blocks
388 * suitable for isolating free pages from and then isolate them.
389 */
390static void isolate_freepages(struct zone *zone,
391 struct compact_control *cc)
392{
393 struct page *page;
394 unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn;
395 unsigned long flags;
396 int nr_freepages = cc->nr_freepages;
397 struct list_head *freelist = &cc->freepages;
398
399 /*
400 * Initialise the free scanner. The starting point is where we last
401 * scanned from (or the end of the zone if starting). The low point
402 * is the end of the pageblock the migration scanner is using.
403 */
404 pfn = cc->free_pfn;
405 low_pfn = cc->migrate_pfn + pageblock_nr_pages;
406
407 /*
408 * Take care that if the migration scanner is at the end of the zone
409 * that the free scanner does not accidentally move to the next zone
410 * in the next isolation cycle.
411 */
412 high_pfn = min(low_pfn, pfn);
413
414 zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
415
416 /*
417 * Isolate free pages until enough are available to migrate the
418 * pages on cc->migratepages. We stop searching if the migrate
419 * and free page scanners meet or enough free pages are isolated.
420 */
421 for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
422 pfn -= pageblock_nr_pages) {
423 unsigned long isolated;
424
425 if (!pfn_valid(pfn))
426 continue;
427
428 /*
429 * Check for overlapping nodes/zones. It's possible on some
430 * configurations to have a setup like
431 * node0 node1 node0
432 * i.e. it's possible that all pages within a zones range of
433 * pages do not belong to a single zone.
434 */
435 page = pfn_to_page(pfn);
436 if (page_zone(page) != zone)
437 continue;
438
439 /* Check the block is suitable for migration */
440 if (!suitable_migration_target(page))
441 continue;
442
443 /*
444 * Found a block suitable for isolating free pages from. Now
445 * we disabled interrupts, double check things are ok and
446 * isolate the pages. This is to minimise the time IRQs
447 * are disabled
448 */
449 isolated = 0;
450 spin_lock_irqsave(&zone->lock, flags);
451 if (suitable_migration_target(page)) {
452 end_pfn = min(pfn + pageblock_nr_pages, zone_end_pfn);
453 isolated = isolate_freepages_block(pfn, end_pfn,
454 freelist, false);
455 nr_freepages += isolated;
456 }
457 spin_unlock_irqrestore(&zone->lock, flags);
458
459 /*
460 * Record the highest PFN we isolated pages from. When next
461 * looking for free pages, the search will restart here as
462 * page migration may have returned some pages to the allocator
463 */
464 if (isolated)
465 high_pfn = max(high_pfn, pfn);
466 }
467
468 /* split_free_page does not map the pages */
469 map_pages(freelist);
470
471 cc->free_pfn = high_pfn;
472 cc->nr_freepages = nr_freepages;
404} 473}
405 474
406/* 475/*
@@ -449,6 +518,44 @@ static void update_nr_listpages(struct compact_control *cc)
449 cc->nr_freepages = nr_freepages; 518 cc->nr_freepages = nr_freepages;
450} 519}
451 520
521/* possible outcome of isolate_migratepages */
522typedef enum {
523 ISOLATE_ABORT, /* Abort compaction now */
524 ISOLATE_NONE, /* No pages isolated, continue scanning */
525 ISOLATE_SUCCESS, /* Pages isolated, migrate */
526} isolate_migrate_t;
527
528/*
529 * Isolate all pages that can be migrated from the block pointed to by
530 * the migrate scanner within compact_control.
531 */
532static isolate_migrate_t isolate_migratepages(struct zone *zone,
533 struct compact_control *cc)
534{
535 unsigned long low_pfn, end_pfn;
536
537 /* Do not scan outside zone boundaries */
538 low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
539
540 /* Only scan within a pageblock boundary */
541 end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
542
543 /* Do not cross the free scanner or scan within a memory hole */
544 if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
545 cc->migrate_pfn = end_pfn;
546 return ISOLATE_NONE;
547 }
548
549 /* Perform the isolation */
550 low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn);
551 if (!low_pfn)
552 return ISOLATE_ABORT;
553
554 cc->migrate_pfn = low_pfn;
555
556 return ISOLATE_SUCCESS;
557}
558
452static int compact_finished(struct zone *zone, 559static int compact_finished(struct zone *zone,
453 struct compact_control *cc) 560 struct compact_control *cc)
454{ 561{
@@ -594,8 +701,11 @@ static int compact_zone(struct zone *zone, struct compact_control *cc)
594 if (err) { 701 if (err) {
595 putback_lru_pages(&cc->migratepages); 702 putback_lru_pages(&cc->migratepages);
596 cc->nr_migratepages = 0; 703 cc->nr_migratepages = 0;
704 if (err == -ENOMEM) {
705 ret = COMPACT_PARTIAL;
706 goto out;
707 }
597 } 708 }
598
599 } 709 }
600 710
601out: 711out:
@@ -795,3 +905,5 @@ void compaction_unregister_node(struct node *node)
795 return device_remove_file(&node->dev, &dev_attr_compact); 905 return device_remove_file(&node->dev, &dev_attr_compact);
796} 906}
797#endif /* CONFIG_SYSFS && CONFIG_NUMA */ 907#endif /* CONFIG_SYSFS && CONFIG_NUMA */
908
909#endif /* CONFIG_COMPACTION */