blob: f461b37ab1c19edd6511af3f84cf0c6fef0c76fc [file] [log] [blame]
Mel Gorman748446b2010-05-24 14:32:27 -07001/*
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>
Mel Gorman76ab0f52010-05-24 14:32:28 -070015#include <linux/sysctl.h>
Mel Gormaned4a6d72010-05-24 14:32:29 -070016#include <linux/sysfs.h>
Mel Gorman748446b2010-05-24 14:32:27 -070017#include "internal.h"
18
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +010019#if defined CONFIG_COMPACTION || defined CONFIG_CMA
20
Mel Gormanb7aba692011-01-13 15:45:54 -080021#define CREATE_TRACE_POINTS
22#include <trace/events/compaction.h>
23
Mel Gorman748446b2010-05-24 14:32:27 -070024static unsigned long release_freepages(struct list_head *freelist)
25{
26 struct page *page, *next;
27 unsigned long count = 0;
28
29 list_for_each_entry_safe(page, next, freelist, lru) {
30 list_del(&page->lru);
31 __free_page(page);
32 count++;
33 }
34
35 return count;
36}
37
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +010038static void map_pages(struct list_head *list)
39{
40 struct page *page;
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
Michal Nazarewiczd4158d22011-12-29 13:09:50 +010048static inline bool migrate_async_suitable(int migratetype)
49{
50 return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
51}
52
Mel Gormancc5a88a2012-10-08 16:32:41 -070053#ifdef CONFIG_COMPACTION
54/* Returns true if the pageblock should be scanned for pages to isolate. */
55static inline bool isolation_suitable(struct compact_control *cc,
56 struct page *page)
57{
58 if (cc->ignore_skip_hint)
59 return true;
60
61 return !get_pageblock_skip(page);
62}
63
64/*
65 * This function is called to clear all cached information on pageblocks that
66 * should be skipped for page isolation when the migrate and free page scanner
67 * meet.
68 */
Mel Gormanb71127a2012-10-08 16:32:47 -070069static void __reset_isolation_suitable(struct zone *zone)
Mel Gormancc5a88a2012-10-08 16:32:41 -070070{
71 unsigned long start_pfn = zone->zone_start_pfn;
72 unsigned long end_pfn = zone->zone_start_pfn + zone->spanned_pages;
73 unsigned long pfn;
74
Mel Gormanf57bc462012-10-08 16:32:45 -070075 zone->compact_cached_migrate_pfn = start_pfn;
76 zone->compact_cached_free_pfn = end_pfn;
Mel Gormanb71127a2012-10-08 16:32:47 -070077 zone->compact_blockskip_flush = false;
Mel Gormancc5a88a2012-10-08 16:32:41 -070078
79 /* Walk the zone and mark every pageblock as suitable for isolation */
80 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
81 struct page *page;
82
83 cond_resched();
84
85 if (!pfn_valid(pfn))
86 continue;
87
88 page = pfn_to_page(pfn);
89 if (zone != page_zone(page))
90 continue;
91
92 clear_pageblock_skip(page);
93 }
94}
95
Mel Gormanb71127a2012-10-08 16:32:47 -070096void reset_isolation_suitable(pg_data_t *pgdat)
97{
98 int zoneid;
99
100 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
101 struct zone *zone = &pgdat->node_zones[zoneid];
102 if (!populated_zone(zone))
103 continue;
104
105 /* Only flush if a full compaction finished recently */
106 if (zone->compact_blockskip_flush)
107 __reset_isolation_suitable(zone);
108 }
109}
110
Mel Gormancc5a88a2012-10-08 16:32:41 -0700111/*
112 * If no pages were isolated then mark this pageblock to be skipped in the
Mel Gormanb71127a2012-10-08 16:32:47 -0700113 * future. The information is later cleared by __reset_isolation_suitable().
Mel Gormancc5a88a2012-10-08 16:32:41 -0700114 */
Mel Gormanf57bc462012-10-08 16:32:45 -0700115static void update_pageblock_skip(struct compact_control *cc,
116 struct page *page, unsigned long nr_isolated,
117 bool migrate_scanner)
Mel Gormancc5a88a2012-10-08 16:32:41 -0700118{
Mel Gormanf57bc462012-10-08 16:32:45 -0700119 struct zone *zone = cc->zone;
Mel Gormancc5a88a2012-10-08 16:32:41 -0700120 if (!page)
121 return;
122
Mel Gormanf57bc462012-10-08 16:32:45 -0700123 if (!nr_isolated) {
124 unsigned long pfn = page_to_pfn(page);
Mel Gormancc5a88a2012-10-08 16:32:41 -0700125 set_pageblock_skip(page);
Mel Gormanf57bc462012-10-08 16:32:45 -0700126
127 /* Update where compaction should restart */
128 if (migrate_scanner) {
129 if (!cc->finished_update_migrate &&
130 pfn > zone->compact_cached_migrate_pfn)
131 zone->compact_cached_migrate_pfn = pfn;
132 } else {
133 if (!cc->finished_update_free &&
134 pfn < zone->compact_cached_free_pfn)
135 zone->compact_cached_free_pfn = pfn;
136 }
137 }
Mel Gormancc5a88a2012-10-08 16:32:41 -0700138}
139#else
140static inline bool isolation_suitable(struct compact_control *cc,
141 struct page *page)
142{
143 return true;
144}
145
Mel Gormanf57bc462012-10-08 16:32:45 -0700146static void update_pageblock_skip(struct compact_control *cc,
147 struct page *page, unsigned long nr_isolated,
148 bool migrate_scanner)
Mel Gormancc5a88a2012-10-08 16:32:41 -0700149{
150}
151#endif /* CONFIG_COMPACTION */
152
Mel Gorman6a38cba2012-10-08 16:32:33 -0700153static inline bool should_release_lock(spinlock_t *lock)
154{
155 return need_resched() || spin_is_contended(lock);
156}
157
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100158/*
Mel Gorman0ba33882012-08-21 16:16:17 -0700159 * Compaction requires the taking of some coarse locks that are potentially
160 * very heavily contended. Check if the process needs to be scheduled or
161 * if the lock is contended. For async compaction, back out in the event
162 * if contention is severe. For sync compaction, schedule.
163 *
164 * Returns true if the lock is held.
165 * Returns false if the lock is released and compaction should abort
166 */
167static bool compact_checklock_irqsave(spinlock_t *lock, unsigned long *flags,
168 bool locked, struct compact_control *cc)
169{
Mel Gorman6a38cba2012-10-08 16:32:33 -0700170 if (should_release_lock(lock)) {
Mel Gorman0ba33882012-08-21 16:16:17 -0700171 if (locked) {
172 spin_unlock_irqrestore(lock, *flags);
173 locked = false;
174 }
175
176 /* async aborts if taking too long or contended */
177 if (!cc->sync) {
Shaohua Li03dd8fe2012-10-08 16:32:27 -0700178 cc->contended = true;
Mel Gorman0ba33882012-08-21 16:16:17 -0700179 return false;
180 }
181
182 cond_resched();
Mel Gorman0ba33882012-08-21 16:16:17 -0700183 }
184
185 if (!locked)
186 spin_lock_irqsave(lock, *flags);
187 return true;
188}
189
190static inline bool compact_trylock_irqsave(spinlock_t *lock,
191 unsigned long *flags, struct compact_control *cc)
192{
193 return compact_checklock_irqsave(lock, flags, false, cc);
194}
195
Mel Gormand6ed9722012-10-08 16:32:36 -0700196/* Returns true if the page is within a block suitable for migration to */
197static bool suitable_migration_target(struct page *page)
198{
199 int migratetype = get_pageblock_migratetype(page);
200
201 /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */
202 if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE)
203 return false;
204
205 /* If the page is a large free page, then allow migration */
206 if (PageBuddy(page) && page_order(page) >= pageblock_order)
207 return true;
208
209 /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
210 if (migrate_async_suitable(migratetype))
211 return true;
212
213 /* Otherwise skip the block */
214 return false;
215}
216
Mel Gorman0ba33882012-08-21 16:16:17 -0700217/*
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100218 * Isolate free pages onto a private freelist. Caller must hold zone->lock.
219 * If @strict is true, will abort returning 0 on any invalid PFNs or non-free
220 * pages inside of the pageblock (even though it may still end up isolating
221 * some pages).
222 */
Mel Gormand6ed9722012-10-08 16:32:36 -0700223static unsigned long isolate_freepages_block(struct compact_control *cc,
224 unsigned long blockpfn,
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100225 unsigned long end_pfn,
226 struct list_head *freelist,
227 bool strict)
Mel Gorman748446b2010-05-24 14:32:27 -0700228{
Mel Gormanb7aba692011-01-13 15:45:54 -0800229 int nr_scanned = 0, total_isolated = 0;
Mel Gormancc5a88a2012-10-08 16:32:41 -0700230 struct page *cursor, *valid_page = NULL;
Mel Gormand6ed9722012-10-08 16:32:36 -0700231 unsigned long nr_strict_required = end_pfn - blockpfn;
232 unsigned long flags;
233 bool locked = false;
Mel Gorman748446b2010-05-24 14:32:27 -0700234
Mel Gorman748446b2010-05-24 14:32:27 -0700235 cursor = pfn_to_page(blockpfn);
236
Mel Gormand6ed9722012-10-08 16:32:36 -0700237 /* Isolate free pages. */
Mel Gorman748446b2010-05-24 14:32:27 -0700238 for (; blockpfn < end_pfn; blockpfn++, cursor++) {
239 int isolated, i;
240 struct page *page = cursor;
241
Mel Gormanb7aba692011-01-13 15:45:54 -0800242 nr_scanned++;
Mel Gormand6ed9722012-10-08 16:32:36 -0700243 if (!pfn_valid_within(blockpfn))
Mel Gorman748446b2010-05-24 14:32:27 -0700244 continue;
Mel Gormancc5a88a2012-10-08 16:32:41 -0700245 if (!valid_page)
246 valid_page = page;
Mel Gormand6ed9722012-10-08 16:32:36 -0700247 if (!PageBuddy(page))
248 continue;
249
250 /*
251 * The zone lock must be held to isolate freepages.
252 * Unfortunately this is a very coarse lock and can be
253 * heavily contended if there are parallel allocations
254 * or parallel compactions. For async compaction do not
255 * spin on the lock and we acquire the lock as late as
256 * possible.
257 */
258 locked = compact_checklock_irqsave(&cc->zone->lock, &flags,
259 locked, cc);
260 if (!locked)
261 break;
262
263 /* Recheck this is a suitable migration target under lock */
264 if (!strict && !suitable_migration_target(page))
265 break;
266
267 /* Recheck this is a buddy page under lock */
268 if (!PageBuddy(page))
269 continue;
Mel Gorman748446b2010-05-24 14:32:27 -0700270
271 /* Found a free page, break it into order-0 pages */
272 isolated = split_free_page(page);
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100273 if (!isolated && strict)
Mel Gormand6ed9722012-10-08 16:32:36 -0700274 break;
Mel Gorman748446b2010-05-24 14:32:27 -0700275 total_isolated += isolated;
276 for (i = 0; i < isolated; i++) {
277 list_add(&page->lru, freelist);
278 page++;
279 }
280
281 /* If a page was split, advance to the end of it */
282 if (isolated) {
283 blockpfn += isolated - 1;
284 cursor += isolated - 1;
285 }
286 }
287
Mel Gormanb7aba692011-01-13 15:45:54 -0800288 trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
Mel Gormand6ed9722012-10-08 16:32:36 -0700289
290 /*
291 * If strict isolation is requested by CMA then check that all the
292 * pages requested were isolated. If there were any failures, 0 is
293 * returned and CMA will fail.
294 */
Mel Gorman2a1e4f42012-10-19 13:56:57 -0700295 if (strict && nr_strict_required > total_isolated)
Mel Gormand6ed9722012-10-08 16:32:36 -0700296 total_isolated = 0;
297
298 if (locked)
299 spin_unlock_irqrestore(&cc->zone->lock, flags);
300
Mel Gormancc5a88a2012-10-08 16:32:41 -0700301 /* Update the pageblock-skip if the whole pageblock was scanned */
302 if (blockpfn == end_pfn)
Mel Gormanf57bc462012-10-08 16:32:45 -0700303 update_pageblock_skip(cc, valid_page, total_isolated, false);
Mel Gormancc5a88a2012-10-08 16:32:41 -0700304
Mel Gorman70e51922012-10-19 12:00:10 +0100305 count_vm_events(COMPACTFREE_SCANNED, nr_scanned);
306 if (total_isolated)
307 count_vm_events(COMPACTISOLATED, total_isolated);
308
Mel Gorman748446b2010-05-24 14:32:27 -0700309 return total_isolated;
310}
311
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100312/**
313 * isolate_freepages_range() - isolate free pages.
314 * @start_pfn: The first PFN to start isolating.
315 * @end_pfn: The one-past-last PFN.
316 *
317 * Non-free pages, invalid PFNs, or zone boundaries within the
318 * [start_pfn, end_pfn) range are considered errors, cause function to
319 * undo its actions and return zero.
320 *
321 * Otherwise, function returns one-past-the-last PFN of isolated page
322 * (which may be greater then end_pfn if end fell in a middle of
323 * a free page).
324 */
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100325unsigned long
Mel Gormancc5a88a2012-10-08 16:32:41 -0700326isolate_freepages_range(struct compact_control *cc,
327 unsigned long start_pfn, unsigned long end_pfn)
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100328{
Mel Gormand6ed9722012-10-08 16:32:36 -0700329 unsigned long isolated, pfn, block_end_pfn;
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100330 LIST_HEAD(freelist);
331
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100332 for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) {
Mel Gormancc5a88a2012-10-08 16:32:41 -0700333 if (!pfn_valid(pfn) || cc->zone != page_zone(pfn_to_page(pfn)))
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100334 break;
335
336 /*
337 * On subsequent iterations ALIGN() is actually not needed,
338 * but we keep it that we not to complicate the code.
339 */
340 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
341 block_end_pfn = min(block_end_pfn, end_pfn);
342
Mel Gormancc5a88a2012-10-08 16:32:41 -0700343 isolated = isolate_freepages_block(cc, pfn, block_end_pfn,
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100344 &freelist, true);
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100345
346 /*
347 * In strict mode, isolate_freepages_block() returns 0 if
348 * there are any holes in the block (ie. invalid PFNs or
349 * non-free pages).
350 */
351 if (!isolated)
352 break;
353
354 /*
355 * If we managed to isolate pages, it is always (1 << n) *
356 * pageblock_nr_pages for some non-negative n. (Max order
357 * page may span two pageblocks).
358 */
359 }
360
361 /* split_free_page does not map the pages */
362 map_pages(&freelist);
363
364 if (pfn < end_pfn) {
365 /* Loop terminated early, cleanup. */
366 release_freepages(&freelist);
367 return 0;
368 }
369
370 /* We don't use freelists for anything. */
371 return pfn;
372}
373
Mel Gorman748446b2010-05-24 14:32:27 -0700374/* Update the number of anon and file isolated pages in the zone */
Mel Gorman0ba33882012-08-21 16:16:17 -0700375static void acct_isolated(struct zone *zone, bool locked, struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700376{
377 struct page *page;
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700378 unsigned int count[2] = { 0, };
Mel Gorman748446b2010-05-24 14:32:27 -0700379
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700380 list_for_each_entry(page, &cc->migratepages, lru)
381 count[!!page_is_file_cache(page)]++;
Mel Gorman748446b2010-05-24 14:32:27 -0700382
Mel Gorman0ba33882012-08-21 16:16:17 -0700383 /* If locked we can use the interrupt unsafe versions */
384 if (locked) {
385 __mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
386 __mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
387 } else {
388 mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
389 mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
390 }
Mel Gorman748446b2010-05-24 14:32:27 -0700391}
392
393/* Similar to reclaim, but different enough that they don't share logic */
394static bool too_many_isolated(struct zone *zone)
395{
Minchan Kimbc693042010-09-09 16:38:00 -0700396 unsigned long active, inactive, isolated;
Mel Gorman748446b2010-05-24 14:32:27 -0700397
398 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
399 zone_page_state(zone, NR_INACTIVE_ANON);
Minchan Kimbc693042010-09-09 16:38:00 -0700400 active = zone_page_state(zone, NR_ACTIVE_FILE) +
401 zone_page_state(zone, NR_ACTIVE_ANON);
Mel Gorman748446b2010-05-24 14:32:27 -0700402 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
403 zone_page_state(zone, NR_ISOLATED_ANON);
404
Minchan Kimbc693042010-09-09 16:38:00 -0700405 return isolated > (inactive + active) / 2;
Mel Gorman748446b2010-05-24 14:32:27 -0700406}
407
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100408/**
409 * isolate_migratepages_range() - isolate all migrate-able pages in range.
410 * @zone: Zone pages are in.
411 * @cc: Compaction control structure.
412 * @low_pfn: The first PFN of the range.
413 * @end_pfn: The one-past-the-last PFN of the range.
Minchan Kimf8194dc2012-10-08 16:33:48 -0700414 * @unevictable: true if it allows to isolate unevictable pages
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100415 *
416 * Isolate all pages that can be migrated from the range specified by
417 * [low_pfn, end_pfn). Returns zero if there is a fatal signal
418 * pending), otherwise PFN of the first page that was not scanned
419 * (which may be both less, equal to or more then end_pfn).
420 *
421 * Assumes that cc->migratepages is empty and cc->nr_migratepages is
422 * zero.
423 *
424 * Apart from cc->migratepages and cc->nr_migratetypes this function
425 * does not modify any cc's fields, in particular it does not modify
426 * (or read for that matter) cc->migrate_pfn.
Mel Gorman748446b2010-05-24 14:32:27 -0700427 */
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100428unsigned long
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100429isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
Minchan Kimf8194dc2012-10-08 16:33:48 -0700430 unsigned long low_pfn, unsigned long end_pfn, bool unevictable)
Mel Gorman748446b2010-05-24 14:32:27 -0700431{
Mel Gorman9927af742011-01-13 15:45:59 -0800432 unsigned long last_pageblock_nr = 0, pageblock_nr;
Mel Gormanb7aba692011-01-13 15:45:54 -0800433 unsigned long nr_scanned = 0, nr_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700434 struct list_head *migratelist = &cc->migratepages;
Konstantin Khlebnikovfa168092012-05-29 15:06:54 -0700435 isolate_mode_t mode = 0;
Mel Gorman0ba33882012-08-21 16:16:17 -0700436 unsigned long flags;
Mel Gorman6a38cba2012-10-08 16:32:33 -0700437 bool locked = false;
Mel Gormancc5a88a2012-10-08 16:32:41 -0700438 struct page *page = NULL, *valid_page = NULL;
Mel Gorman748446b2010-05-24 14:32:27 -0700439
Mel Gorman748446b2010-05-24 14:32:27 -0700440 /*
441 * Ensure that there are not too many pages isolated from the LRU
442 * list by either parallel reclaimers or compaction. If there are,
443 * delay for some time until fewer pages are isolated
444 */
445 while (unlikely(too_many_isolated(zone))) {
Mel Gormanf9e35b32011-06-15 15:08:52 -0700446 /* async migration should just abort */
447 if (!cc->sync)
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100448 return 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700449
Mel Gorman748446b2010-05-24 14:32:27 -0700450 congestion_wait(BLK_RW_ASYNC, HZ/10);
451
452 if (fatal_signal_pending(current))
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100453 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700454 }
455
456 /* Time to isolate some pages for migration */
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700457 cond_resched();
Mel Gorman748446b2010-05-24 14:32:27 -0700458 for (; low_pfn < end_pfn; low_pfn++) {
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700459 /* give a chance to irqs before checking need_resched() */
Mel Gorman6a38cba2012-10-08 16:32:33 -0700460 if (locked && !((low_pfn+1) % SWAP_CLUSTER_MAX)) {
461 if (should_release_lock(&zone->lru_lock)) {
462 spin_unlock_irqrestore(&zone->lru_lock, flags);
463 locked = false;
464 }
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700465 }
Mel Gorman0ba33882012-08-21 16:16:17 -0700466
Mel Gorman0bf380b2012-02-03 15:37:18 -0800467 /*
468 * migrate_pfn does not necessarily start aligned to a
469 * pageblock. Ensure that pfn_valid is called when moving
470 * into a new MAX_ORDER_NR_PAGES range in case of large
471 * memory holes within the zone
472 */
473 if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) {
474 if (!pfn_valid(low_pfn)) {
475 low_pfn += MAX_ORDER_NR_PAGES - 1;
476 continue;
477 }
478 }
479
Mel Gorman748446b2010-05-24 14:32:27 -0700480 if (!pfn_valid_within(low_pfn))
481 continue;
Mel Gormanb7aba692011-01-13 15:45:54 -0800482 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700483
Mel Gormandc908602012-02-08 17:13:38 -0800484 /*
485 * Get the page and ensure the page is within the same zone.
486 * See the comment in isolate_freepages about overlapping
487 * nodes. It is deliberate that the new zone lock is not taken
488 * as memory compaction should not move pages between nodes.
489 */
Mel Gorman748446b2010-05-24 14:32:27 -0700490 page = pfn_to_page(low_pfn);
Mel Gormandc908602012-02-08 17:13:38 -0800491 if (page_zone(page) != zone)
492 continue;
493
Mel Gormancc5a88a2012-10-08 16:32:41 -0700494 if (!valid_page)
495 valid_page = page;
496
497 /* If isolation recently failed, do not retry */
498 pageblock_nr = low_pfn >> pageblock_order;
499 if (!isolation_suitable(cc, page))
500 goto next_pageblock;
501
Mel Gormandc908602012-02-08 17:13:38 -0800502 /* Skip if free */
Mel Gorman748446b2010-05-24 14:32:27 -0700503 if (PageBuddy(page))
504 continue;
505
Mel Gorman9927af742011-01-13 15:45:59 -0800506 /*
507 * For async migration, also only scan in MOVABLE blocks. Async
508 * migration is optimistic to see if the minimum amount of work
509 * satisfies the allocation
510 */
Mel Gorman9927af742011-01-13 15:45:59 -0800511 if (!cc->sync && last_pageblock_nr != pageblock_nr &&
Michal Nazarewiczd4158d22011-12-29 13:09:50 +0100512 !migrate_async_suitable(get_pageblock_migratetype(page))) {
Mel Gormanf57bc462012-10-08 16:32:45 -0700513 cc->finished_update_migrate = true;
Mel Gorman6a38cba2012-10-08 16:32:33 -0700514 goto next_pageblock;
Mel Gorman9927af742011-01-13 15:45:59 -0800515 }
516
Mel Gorman6a38cba2012-10-08 16:32:33 -0700517 /* Check may be lockless but that's ok as we recheck later */
Andrea Arcangelibc835012011-01-13 15:47:08 -0800518 if (!PageLRU(page))
519 continue;
520
521 /*
Mel Gorman6a38cba2012-10-08 16:32:33 -0700522 * PageLRU is set. lru_lock normally excludes isolation
523 * splitting and collapsing (collapsing has already happened
524 * if PageLRU is set) but the lock is not necessarily taken
525 * here and it is wasteful to take it just to check transhuge.
526 * Check TransHuge without lock and skip the whole pageblock if
527 * it's either a transhuge or hugetlbfs page, as calling
528 * compound_order() without preventing THP from splitting the
529 * page underneath us may return surprising results.
Andrea Arcangelibc835012011-01-13 15:47:08 -0800530 */
531 if (PageTransHuge(page)) {
Mel Gorman6a38cba2012-10-08 16:32:33 -0700532 if (!locked)
533 goto next_pageblock;
534 low_pfn += (1 << compound_order(page)) - 1;
535 continue;
536 }
537
538 /* Check if it is ok to still hold the lock */
539 locked = compact_checklock_irqsave(&zone->lru_lock, &flags,
540 locked, cc);
541 if (!locked || fatal_signal_pending(current))
542 break;
543
544 /* Recheck PageLRU and PageTransHuge under lock */
545 if (!PageLRU(page))
546 continue;
547 if (PageTransHuge(page)) {
Andrea Arcangelibc835012011-01-13 15:47:08 -0800548 low_pfn += (1 << compound_order(page)) - 1;
549 continue;
550 }
551
Mel Gormanc8244932012-01-12 17:19:38 -0800552 if (!cc->sync)
553 mode |= ISOLATE_ASYNC_MIGRATE;
554
Minchan Kimf8194dc2012-10-08 16:33:48 -0700555 if (unevictable)
556 mode |= ISOLATE_UNEVICTABLE;
557
Mel Gorman748446b2010-05-24 14:32:27 -0700558 /* Try isolate the page */
Konstantin Khlebnikovfa168092012-05-29 15:06:54 -0700559 if (__isolate_lru_page(page, mode) != 0)
Mel Gorman748446b2010-05-24 14:32:27 -0700560 continue;
561
Andrea Arcangelibc835012011-01-13 15:47:08 -0800562 VM_BUG_ON(PageTransCompound(page));
563
Mel Gorman748446b2010-05-24 14:32:27 -0700564 /* Successfully isolated */
Mel Gormanf57bc462012-10-08 16:32:45 -0700565 cc->finished_update_migrate = true;
Mel Gorman748446b2010-05-24 14:32:27 -0700566 del_page_from_lru_list(zone, page, page_lru(page));
567 list_add(&page->lru, migratelist);
Mel Gorman748446b2010-05-24 14:32:27 -0700568 cc->nr_migratepages++;
Mel Gormanb7aba692011-01-13 15:45:54 -0800569 nr_isolated++;
Mel Gorman748446b2010-05-24 14:32:27 -0700570
571 /* Avoid isolating too much */
Hillf Danton31b83842012-01-10 15:07:59 -0800572 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
573 ++low_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700574 break;
Hillf Danton31b83842012-01-10 15:07:59 -0800575 }
Mel Gorman6a38cba2012-10-08 16:32:33 -0700576
577 continue;
578
579next_pageblock:
580 low_pfn += pageblock_nr_pages;
581 low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1;
582 last_pageblock_nr = pageblock_nr;
Mel Gorman748446b2010-05-24 14:32:27 -0700583 }
584
Mel Gorman0ba33882012-08-21 16:16:17 -0700585 acct_isolated(zone, locked, cc);
Mel Gorman748446b2010-05-24 14:32:27 -0700586
Mel Gorman0ba33882012-08-21 16:16:17 -0700587 if (locked)
588 spin_unlock_irqrestore(&zone->lru_lock, flags);
Mel Gorman748446b2010-05-24 14:32:27 -0700589
Mel Gormancc5a88a2012-10-08 16:32:41 -0700590 /* Update the pageblock-skip if the whole pageblock was scanned */
591 if (low_pfn == end_pfn)
Mel Gormanf57bc462012-10-08 16:32:45 -0700592 update_pageblock_skip(cc, valid_page, nr_isolated, true);
Mel Gormancc5a88a2012-10-08 16:32:41 -0700593
Mel Gormanb7aba692011-01-13 15:45:54 -0800594 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
595
Mel Gorman70e51922012-10-19 12:00:10 +0100596 count_vm_events(COMPACTMIGRATE_SCANNED, nr_scanned);
597 if (nr_isolated)
598 count_vm_events(COMPACTISOLATED, nr_isolated);
599
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100600 return low_pfn;
601}
602
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100603#endif /* CONFIG_COMPACTION || CONFIG_CMA */
604#ifdef CONFIG_COMPACTION
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100605/*
606 * Based on information in the current compact_control, find blocks
607 * suitable for isolating free pages from and then isolate them.
608 */
609static void isolate_freepages(struct zone *zone,
610 struct compact_control *cc)
611{
612 struct page *page;
613 unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn;
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100614 int nr_freepages = cc->nr_freepages;
615 struct list_head *freelist = &cc->freepages;
616
617 /*
618 * Initialise the free scanner. The starting point is where we last
619 * scanned from (or the end of the zone if starting). The low point
620 * is the end of the pageblock the migration scanner is using.
621 */
622 pfn = cc->free_pfn;
623 low_pfn = cc->migrate_pfn + pageblock_nr_pages;
624
625 /*
626 * Take care that if the migration scanner is at the end of the zone
627 * that the free scanner does not accidentally move to the next zone
628 * in the next isolation cycle.
629 */
630 high_pfn = min(low_pfn, pfn);
631
632 zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
633
634 /*
635 * Isolate free pages until enough are available to migrate the
636 * pages on cc->migratepages. We stop searching if the migrate
637 * and free page scanners meet or enough free pages are isolated.
638 */
639 for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
640 pfn -= pageblock_nr_pages) {
641 unsigned long isolated;
642
643 if (!pfn_valid(pfn))
644 continue;
645
646 /*
647 * Check for overlapping nodes/zones. It's possible on some
648 * configurations to have a setup like
649 * node0 node1 node0
650 * i.e. it's possible that all pages within a zones range of
651 * pages do not belong to a single zone.
652 */
653 page = pfn_to_page(pfn);
654 if (page_zone(page) != zone)
655 continue;
656
657 /* Check the block is suitable for migration */
658 if (!suitable_migration_target(page))
659 continue;
660
Mel Gormancc5a88a2012-10-08 16:32:41 -0700661 /* If isolation recently failed, do not retry */
662 if (!isolation_suitable(cc, page))
663 continue;
664
Mel Gormand6ed9722012-10-08 16:32:36 -0700665 /* Found a block suitable for isolating free pages from */
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100666 isolated = 0;
Mel Gorman76e36c32012-12-06 19:01:14 +0000667
668 /*
669 * As pfn may not start aligned, pfn+pageblock_nr_page
670 * may cross a MAX_ORDER_NR_PAGES boundary and miss
671 * a pfn_valid check. Ensure isolate_freepages_block()
672 * only scans within a pageblock
673 */
674 end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
675 end_pfn = min(end_pfn, zone_end_pfn);
Mel Gormand6ed9722012-10-08 16:32:36 -0700676 isolated = isolate_freepages_block(cc, pfn, end_pfn,
677 freelist, false);
678 nr_freepages += isolated;
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100679
680 /*
681 * Record the highest PFN we isolated pages from. When next
682 * looking for free pages, the search will restart here as
683 * page migration may have returned some pages to the allocator
684 */
Mel Gormanf57bc462012-10-08 16:32:45 -0700685 if (isolated) {
686 cc->finished_update_free = true;
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100687 high_pfn = max(high_pfn, pfn);
Mel Gormanf57bc462012-10-08 16:32:45 -0700688 }
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100689 }
690
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100691 /* split_free_page does not map the pages */
692 map_pages(freelist);
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100693
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100694 cc->free_pfn = high_pfn;
695 cc->nr_freepages = nr_freepages;
Mel Gorman748446b2010-05-24 14:32:27 -0700696}
697
698/*
699 * This is a migrate-callback that "allocates" freepages by taking pages
700 * from the isolated freelists in the block we are migrating to.
701 */
702static struct page *compaction_alloc(struct page *migratepage,
703 unsigned long data,
704 int **result)
705{
706 struct compact_control *cc = (struct compact_control *)data;
707 struct page *freepage;
708
709 /* Isolate free pages if necessary */
710 if (list_empty(&cc->freepages)) {
711 isolate_freepages(cc->zone, cc);
712
713 if (list_empty(&cc->freepages))
714 return NULL;
715 }
716
717 freepage = list_entry(cc->freepages.next, struct page, lru);
718 list_del(&freepage->lru);
719 cc->nr_freepages--;
720
721 return freepage;
722}
723
724/*
725 * We cannot control nr_migratepages and nr_freepages fully when migration is
726 * running as migrate_pages() has no knowledge of compact_control. When
727 * migration is complete, we count the number of pages on the lists by hand.
728 */
729static void update_nr_listpages(struct compact_control *cc)
730{
731 int nr_migratepages = 0;
732 int nr_freepages = 0;
733 struct page *page;
734
735 list_for_each_entry(page, &cc->migratepages, lru)
736 nr_migratepages++;
737 list_for_each_entry(page, &cc->freepages, lru)
738 nr_freepages++;
739
740 cc->nr_migratepages = nr_migratepages;
741 cc->nr_freepages = nr_freepages;
742}
743
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100744/* possible outcome of isolate_migratepages */
745typedef enum {
746 ISOLATE_ABORT, /* Abort compaction now */
747 ISOLATE_NONE, /* No pages isolated, continue scanning */
748 ISOLATE_SUCCESS, /* Pages isolated, migrate */
749} isolate_migrate_t;
750
751/*
752 * Isolate all pages that can be migrated from the block pointed to by
753 * the migrate scanner within compact_control.
754 */
755static isolate_migrate_t isolate_migratepages(struct zone *zone,
756 struct compact_control *cc)
757{
758 unsigned long low_pfn, end_pfn;
759
760 /* Do not scan outside zone boundaries */
761 low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
762
763 /* Only scan within a pageblock boundary */
764 end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
765
766 /* Do not cross the free scanner or scan within a memory hole */
767 if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
768 cc->migrate_pfn = end_pfn;
769 return ISOLATE_NONE;
770 }
771
772 /* Perform the isolation */
Minchan Kimf8194dc2012-10-08 16:33:48 -0700773 low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn, false);
Shaohua Li03dd8fe2012-10-08 16:32:27 -0700774 if (!low_pfn || cc->contended)
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100775 return ISOLATE_ABORT;
776
777 cc->migrate_pfn = low_pfn;
778
779 return ISOLATE_SUCCESS;
780}
781
Mel Gorman748446b2010-05-24 14:32:27 -0700782static int compact_finished(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800783 struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700784{
Mel Gormane7c3c142013-01-11 14:32:16 -0800785 unsigned int order;
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800786 unsigned long watermark;
Mel Gorman56de7262010-05-24 14:32:30 -0700787
Mel Gorman748446b2010-05-24 14:32:27 -0700788 if (fatal_signal_pending(current))
789 return COMPACT_PARTIAL;
790
791 /* Compaction run completes if the migrate and free scanner meet */
Mel Gormancc5a88a2012-10-08 16:32:41 -0700792 if (cc->free_pfn <= cc->migrate_pfn) {
Mel Gormanb71127a2012-10-08 16:32:47 -0700793 /*
794 * Mark that the PG_migrate_skip information should be cleared
795 * by kswapd when it goes to sleep. kswapd does not set the
796 * flag itself as the decision to be clear should be directly
797 * based on an allocation request.
798 */
799 if (!current_is_kswapd())
800 zone->compact_blockskip_flush = true;
801
Mel Gorman748446b2010-05-24 14:32:27 -0700802 return COMPACT_COMPLETE;
Mel Gormancc5a88a2012-10-08 16:32:41 -0700803 }
Mel Gorman748446b2010-05-24 14:32:27 -0700804
Johannes Weiner82478fb2011-01-20 14:44:21 -0800805 /*
806 * order == -1 is expected when compacting via
807 * /proc/sys/vm/compact_memory
808 */
Mel Gorman56de7262010-05-24 14:32:30 -0700809 if (cc->order == -1)
810 return COMPACT_CONTINUE;
811
Michal Hocko3957c772011-06-15 15:08:25 -0700812 /* Compaction run is not finished if the watermark is not met */
813 watermark = low_wmark_pages(zone);
814 watermark += (1 << cc->order);
815
816 if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
817 return COMPACT_CONTINUE;
818
Mel Gorman56de7262010-05-24 14:32:30 -0700819 /* Direct compactor: Is a suitable page free? */
Mel Gormane7c3c142013-01-11 14:32:16 -0800820 for (order = cc->order; order < MAX_ORDER; order++) {
821 struct free_area *area = &zone->free_area[order];
Mel Gorman56de7262010-05-24 14:32:30 -0700822
Mel Gormane7c3c142013-01-11 14:32:16 -0800823 /* Job done if page is free of the right migratetype */
824 if (!list_empty(&area->free_list[cc->migratetype]))
825 return COMPACT_PARTIAL;
826
827 /* Job done if allocation would set block type */
828 if (cc->order >= pageblock_order && area->nr_free)
829 return COMPACT_PARTIAL;
Mel Gorman56de7262010-05-24 14:32:30 -0700830 }
831
Mel Gorman748446b2010-05-24 14:32:27 -0700832 return COMPACT_CONTINUE;
833}
834
Mel Gorman3e7d3442011-01-13 15:45:56 -0800835/*
836 * compaction_suitable: Is this suitable to run compaction on this zone now?
837 * Returns
838 * COMPACT_SKIPPED - If there are too few free pages for compaction
839 * COMPACT_PARTIAL - If the allocation would succeed without compaction
840 * COMPACT_CONTINUE - If compaction should run now
841 */
842unsigned long compaction_suitable(struct zone *zone, int order)
843{
844 int fragindex;
845 unsigned long watermark;
846
847 /*
Michal Hocko3957c772011-06-15 15:08:25 -0700848 * order == -1 is expected when compacting via
849 * /proc/sys/vm/compact_memory
850 */
851 if (order == -1)
852 return COMPACT_CONTINUE;
853
854 /*
Mel Gorman3e7d3442011-01-13 15:45:56 -0800855 * Watermarks for order-0 must be met for compaction. Note the 2UL.
856 * This is because during migration, copies of pages need to be
857 * allocated and for a short time, the footprint is higher
858 */
859 watermark = low_wmark_pages(zone) + (2UL << order);
860 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
861 return COMPACT_SKIPPED;
862
863 /*
864 * fragmentation index determines if allocation failures are due to
865 * low memory or external fragmentation
866 *
Shaohua Lia582a732011-06-15 15:08:49 -0700867 * index of -1000 implies allocations might succeed depending on
868 * watermarks
Mel Gorman3e7d3442011-01-13 15:45:56 -0800869 * index towards 0 implies failure is due to lack of memory
870 * index towards 1000 implies failure is due to fragmentation
871 *
872 * Only compact if a failure would be due to fragmentation.
873 */
874 fragindex = fragmentation_index(zone, order);
875 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
876 return COMPACT_SKIPPED;
877
Shaohua Lia582a732011-06-15 15:08:49 -0700878 if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
879 0, 0))
Mel Gorman3e7d3442011-01-13 15:45:56 -0800880 return COMPACT_PARTIAL;
881
882 return COMPACT_CONTINUE;
883}
884
Mel Gorman748446b2010-05-24 14:32:27 -0700885static int compact_zone(struct zone *zone, struct compact_control *cc)
886{
887 int ret;
Mel Gormanf57bc462012-10-08 16:32:45 -0700888 unsigned long start_pfn = zone->zone_start_pfn;
889 unsigned long end_pfn = zone->zone_start_pfn + zone->spanned_pages;
Mel Gorman748446b2010-05-24 14:32:27 -0700890
Mel Gorman3e7d3442011-01-13 15:45:56 -0800891 ret = compaction_suitable(zone, cc->order);
892 switch (ret) {
893 case COMPACT_PARTIAL:
894 case COMPACT_SKIPPED:
895 /* Compaction is likely to fail */
896 return ret;
897 case COMPACT_CONTINUE:
898 /* Fall through to compaction */
899 ;
900 }
901
Mel Gormanf57bc462012-10-08 16:32:45 -0700902 /*
903 * Setup to move all movable pages to the end of the zone. Used cached
904 * information on where the scanners should start but check that it
905 * is initialised by ensuring the values are within zone boundaries.
906 */
907 cc->migrate_pfn = zone->compact_cached_migrate_pfn;
908 cc->free_pfn = zone->compact_cached_free_pfn;
909 if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) {
910 cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1);
911 zone->compact_cached_free_pfn = cc->free_pfn;
912 }
913 if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) {
914 cc->migrate_pfn = start_pfn;
915 zone->compact_cached_migrate_pfn = cc->migrate_pfn;
916 }
Mel Gorman748446b2010-05-24 14:32:27 -0700917
Mel Gormanb71127a2012-10-08 16:32:47 -0700918 /*
919 * Clear pageblock skip if there were failures recently and compaction
920 * is about to be retried after being deferred. kswapd does not do
921 * this reset as it'll reset the cached information when going to sleep.
922 */
923 if (compaction_restarting(zone, cc->order) && !current_is_kswapd())
924 __reset_isolation_suitable(zone);
Mel Gormancc5a88a2012-10-08 16:32:41 -0700925
Mel Gorman748446b2010-05-24 14:32:27 -0700926 migrate_prep_local();
927
928 while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
929 unsigned long nr_migrate, nr_remaining;
Minchan Kim9d502c12011-03-22 16:30:39 -0700930 int err;
Mel Gorman748446b2010-05-24 14:32:27 -0700931
Mel Gormanf9e35b32011-06-15 15:08:52 -0700932 switch (isolate_migratepages(zone, cc)) {
933 case ISOLATE_ABORT:
934 ret = COMPACT_PARTIAL;
Shaohua Li03dd8fe2012-10-08 16:32:27 -0700935 putback_lru_pages(&cc->migratepages);
936 cc->nr_migratepages = 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700937 goto out;
938 case ISOLATE_NONE:
Mel Gorman748446b2010-05-24 14:32:27 -0700939 continue;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700940 case ISOLATE_SUCCESS:
941 ;
942 }
Mel Gorman748446b2010-05-24 14:32:27 -0700943
944 nr_migrate = cc->nr_migratepages;
Minchan Kim9d502c12011-03-22 16:30:39 -0700945 err = migrate_pages(&cc->migratepages, compaction_alloc,
Mel Gorman7f0f2492011-01-13 15:45:58 -0800946 (unsigned long)cc, false,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800947 cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC);
Mel Gorman748446b2010-05-24 14:32:27 -0700948 update_nr_listpages(cc);
949 nr_remaining = cc->nr_migratepages;
950
Mel Gormanb7aba692011-01-13 15:45:54 -0800951 trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
952 nr_remaining);
Mel Gorman748446b2010-05-24 14:32:27 -0700953
954 /* Release LRU pages not migrated */
Minchan Kim9d502c12011-03-22 16:30:39 -0700955 if (err) {
Mel Gorman748446b2010-05-24 14:32:27 -0700956 putback_lru_pages(&cc->migratepages);
957 cc->nr_migratepages = 0;
David Rientjes7a08b442012-07-11 14:02:13 -0700958 if (err == -ENOMEM) {
959 ret = COMPACT_PARTIAL;
960 goto out;
961 }
Mel Gorman748446b2010-05-24 14:32:27 -0700962 }
Mel Gorman748446b2010-05-24 14:32:27 -0700963 }
964
Mel Gormanf9e35b32011-06-15 15:08:52 -0700965out:
Mel Gorman748446b2010-05-24 14:32:27 -0700966 /* Release free pages and check accounting */
967 cc->nr_freepages -= release_freepages(&cc->freepages);
968 VM_BUG_ON(cc->nr_freepages != 0);
969
970 return ret;
971}
Mel Gorman76ab0f52010-05-24 14:32:28 -0700972
Kyungmin Parkd43a87e2011-10-31 17:09:08 -0700973static unsigned long compact_zone_order(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800974 int order, gfp_t gfp_mask,
Mel Gormane7c3c142013-01-11 14:32:16 -0800975 bool sync, bool *contended)
Mel Gorman56de7262010-05-24 14:32:30 -0700976{
Shaohua Li03dd8fe2012-10-08 16:32:27 -0700977 unsigned long ret;
Mel Gorman56de7262010-05-24 14:32:30 -0700978 struct compact_control cc = {
979 .nr_freepages = 0,
980 .nr_migratepages = 0,
981 .order = order,
982 .migratetype = allocflags_to_migratetype(gfp_mask),
983 .zone = zone,
Mel Gorman77f1fe62011-01-13 15:45:57 -0800984 .sync = sync,
Mel Gorman56de7262010-05-24 14:32:30 -0700985 };
986 INIT_LIST_HEAD(&cc.freepages);
987 INIT_LIST_HEAD(&cc.migratepages);
988
Shaohua Li03dd8fe2012-10-08 16:32:27 -0700989 ret = compact_zone(zone, &cc);
990
991 VM_BUG_ON(!list_empty(&cc.freepages));
992 VM_BUG_ON(!list_empty(&cc.migratepages));
993
994 *contended = cc.contended;
995 return ret;
Mel Gorman56de7262010-05-24 14:32:30 -0700996}
997
Mel Gorman5e771902010-05-24 14:32:31 -0700998int sysctl_extfrag_threshold = 500;
999
Mel Gorman56de7262010-05-24 14:32:30 -07001000/**
1001 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
1002 * @zonelist: The zonelist used for the current allocation
1003 * @order: The order of the current allocation
1004 * @gfp_mask: The GFP mask of the current allocation
1005 * @nodemask: The allowed nodes to allocate from
Mel Gorman77f1fe62011-01-13 15:45:57 -08001006 * @sync: Whether migration is synchronous or not
Mel Gorman8a063192012-10-08 16:32:31 -07001007 * @contended: Return value that is true if compaction was aborted due to lock contention
1008 * @page: Optionally capture a free page of the requested order during compaction
Mel Gorman56de7262010-05-24 14:32:30 -07001009 *
1010 * This is the main entry point for direct page compaction.
1011 */
1012unsigned long try_to_compact_pages(struct zonelist *zonelist,
Mel Gorman77f1fe62011-01-13 15:45:57 -08001013 int order, gfp_t gfp_mask, nodemask_t *nodemask,
Mel Gormane7c3c142013-01-11 14:32:16 -08001014 bool sync, bool *contended)
Mel Gorman56de7262010-05-24 14:32:30 -07001015{
1016 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
1017 int may_enter_fs = gfp_mask & __GFP_FS;
1018 int may_perform_io = gfp_mask & __GFP_IO;
Mel Gorman56de7262010-05-24 14:32:30 -07001019 struct zoneref *z;
1020 struct zone *zone;
1021 int rc = COMPACT_SKIPPED;
Bartlomiej Zolnierkiewicz850bf192012-10-08 16:32:05 -07001022 int alloc_flags = 0;
Mel Gorman56de7262010-05-24 14:32:30 -07001023
Mel Gormanbc337a92012-10-08 16:29:09 -07001024 /* Check if the GFP flags allow compaction */
Andrea Arcangelic5a73c32011-01-13 15:47:11 -08001025 if (!order || !may_enter_fs || !may_perform_io)
Mel Gorman56de7262010-05-24 14:32:30 -07001026 return rc;
1027
1028 count_vm_event(COMPACTSTALL);
1029
Bartlomiej Zolnierkiewicz850bf192012-10-08 16:32:05 -07001030#ifdef CONFIG_CMA
1031 if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
1032 alloc_flags |= ALLOC_CMA;
1033#endif
Mel Gorman56de7262010-05-24 14:32:30 -07001034 /* Compact each zone in the list */
1035 for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
1036 nodemask) {
Mel Gorman56de7262010-05-24 14:32:30 -07001037 int status;
1038
Mel Gorman0ba33882012-08-21 16:16:17 -07001039 status = compact_zone_order(zone, order, gfp_mask, sync,
Mel Gormane7c3c142013-01-11 14:32:16 -08001040 contended);
Mel Gorman56de7262010-05-24 14:32:30 -07001041 rc = max(status, rc);
1042
Mel Gorman3e7d3442011-01-13 15:45:56 -08001043 /* If a normal allocation would succeed, stop compacting */
Bartlomiej Zolnierkiewicz850bf192012-10-08 16:32:05 -07001044 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
1045 alloc_flags))
Mel Gorman56de7262010-05-24 14:32:30 -07001046 break;
1047 }
1048
1049 return rc;
1050}
1051
1052
Mel Gorman76ab0f52010-05-24 14:32:28 -07001053/* Compact all zones within a node */
Rik van Riel7be62de2012-03-21 16:33:52 -07001054static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001055{
1056 int zoneid;
Mel Gorman76ab0f52010-05-24 14:32:28 -07001057 struct zone *zone;
1058
Mel Gorman76ab0f52010-05-24 14:32:28 -07001059 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
Mel Gorman76ab0f52010-05-24 14:32:28 -07001060
1061 zone = &pgdat->node_zones[zoneid];
1062 if (!populated_zone(zone))
1063 continue;
1064
Rik van Riel7be62de2012-03-21 16:33:52 -07001065 cc->nr_freepages = 0;
1066 cc->nr_migratepages = 0;
1067 cc->zone = zone;
1068 INIT_LIST_HEAD(&cc->freepages);
1069 INIT_LIST_HEAD(&cc->migratepages);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001070
Dan Carpenteraad6ec32012-03-21 16:33:54 -07001071 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
Rik van Riel7be62de2012-03-21 16:33:52 -07001072 compact_zone(zone, cc);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001073
Rik van Rielaff62242012-03-21 16:33:52 -07001074 if (cc->order > 0) {
1075 int ok = zone_watermark_ok(zone, cc->order,
1076 low_wmark_pages(zone), 0, 0);
Minchan Kim57bb21c2012-08-21 16:16:03 -07001077 if (ok && cc->order >= zone->compact_order_failed)
Rik van Rielaff62242012-03-21 16:33:52 -07001078 zone->compact_order_failed = cc->order + 1;
1079 /* Currently async compaction is never deferred. */
1080 else if (!ok && cc->sync)
1081 defer_compaction(zone, cc->order);
1082 }
1083
Rik van Riel7be62de2012-03-21 16:33:52 -07001084 VM_BUG_ON(!list_empty(&cc->freepages));
1085 VM_BUG_ON(!list_empty(&cc->migratepages));
Mel Gorman76ab0f52010-05-24 14:32:28 -07001086 }
1087
1088 return 0;
1089}
1090
Rik van Riel7be62de2012-03-21 16:33:52 -07001091int compact_pgdat(pg_data_t *pgdat, int order)
1092{
1093 struct compact_control cc = {
1094 .order = order,
1095 .sync = false,
1096 };
1097
1098 return __compact_pgdat(pgdat, &cc);
1099}
1100
1101static int compact_node(int nid)
1102{
Rik van Riel7be62de2012-03-21 16:33:52 -07001103 struct compact_control cc = {
1104 .order = -1,
1105 .sync = true,
1106 };
1107
Hugh Dickins8575ec22012-03-21 16:33:53 -07001108 return __compact_pgdat(NODE_DATA(nid), &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -07001109}
1110
Mel Gorman76ab0f52010-05-24 14:32:28 -07001111/* Compact all nodes in the system */
Jason Liuc0b96522013-01-11 14:31:47 -08001112static void compact_nodes(void)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001113{
1114 int nid;
1115
Hugh Dickins8575ec22012-03-21 16:33:53 -07001116 /* Flush pending updates to the LRU lists */
1117 lru_add_drain_all();
1118
Mel Gorman76ab0f52010-05-24 14:32:28 -07001119 for_each_online_node(nid)
1120 compact_node(nid);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001121}
1122
1123/* The written value is actually unused, all memory is compacted */
1124int sysctl_compact_memory;
1125
1126/* This is the entry point for compacting all nodes via /proc/sys/vm */
1127int sysctl_compaction_handler(struct ctl_table *table, int write,
1128 void __user *buffer, size_t *length, loff_t *ppos)
1129{
1130 if (write)
Jason Liuc0b96522013-01-11 14:31:47 -08001131 compact_nodes();
Mel Gorman76ab0f52010-05-24 14:32:28 -07001132
1133 return 0;
1134}
Mel Gormaned4a6d72010-05-24 14:32:29 -07001135
Mel Gorman5e771902010-05-24 14:32:31 -07001136int sysctl_extfrag_handler(struct ctl_table *table, int write,
1137 void __user *buffer, size_t *length, loff_t *ppos)
1138{
1139 proc_dointvec_minmax(table, write, buffer, length, ppos);
1140
1141 return 0;
1142}
1143
Mel Gormaned4a6d72010-05-24 14:32:29 -07001144#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
Kay Sievers10fbcf42011-12-21 14:48:43 -08001145ssize_t sysfs_compact_node(struct device *dev,
1146 struct device_attribute *attr,
Mel Gormaned4a6d72010-05-24 14:32:29 -07001147 const char *buf, size_t count)
1148{
Hugh Dickins8575ec22012-03-21 16:33:53 -07001149 int nid = dev->id;
1150
1151 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
1152 /* Flush pending updates to the LRU lists */
1153 lru_add_drain_all();
1154
1155 compact_node(nid);
1156 }
Mel Gormaned4a6d72010-05-24 14:32:29 -07001157
1158 return count;
1159}
Kay Sievers10fbcf42011-12-21 14:48:43 -08001160static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001161
1162int compaction_register_node(struct node *node)
1163{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001164 return device_create_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001165}
1166
1167void compaction_unregister_node(struct node *node)
1168{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001169 return device_remove_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001170}
1171#endif /* CONFIG_SYSFS && CONFIG_NUMA */
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +01001172
1173#endif /* CONFIG_COMPACTION */