blob: 26bb20ef853dbe7039e2cdca309f5a34426a3a5f [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>
Rafael Aquinibf6bddf2012-12-11 16:02:42 -080017#include <linux/balloon_compaction.h>
Minchan Kim194159f2013-02-22 16:33:58 -080018#include <linux/page-isolation.h>
Mel Gorman748446b2010-05-24 14:32:27 -070019#include "internal.h"
20
Minchan Kim010fc292012-12-20 15:05:06 -080021#ifdef CONFIG_COMPACTION
22static inline void count_compact_event(enum vm_event_item item)
23{
24 count_vm_event(item);
25}
26
27static inline void count_compact_events(enum vm_event_item item, long delta)
28{
29 count_vm_events(item, delta);
30}
31#else
32#define count_compact_event(item) do { } while (0)
33#define count_compact_events(item, delta) do { } while (0)
34#endif
35
Michal Nazarewiczff9543f2011-12-29 13:09:50 +010036#if defined CONFIG_COMPACTION || defined CONFIG_CMA
37
Mel Gormanb7aba692011-01-13 15:45:54 -080038#define CREATE_TRACE_POINTS
39#include <trace/events/compaction.h>
40
Mel Gorman748446b2010-05-24 14:32:27 -070041static unsigned long release_freepages(struct list_head *freelist)
42{
43 struct page *page, *next;
44 unsigned long count = 0;
45
46 list_for_each_entry_safe(page, next, freelist, lru) {
47 list_del(&page->lru);
48 __free_page(page);
49 count++;
50 }
51
52 return count;
53}
54
Michal Nazarewiczff9543f2011-12-29 13:09:50 +010055static void map_pages(struct list_head *list)
56{
57 struct page *page;
58
59 list_for_each_entry(page, list, lru) {
60 arch_alloc_page(page, 0);
61 kernel_map_pages(page, 1, 1);
62 }
63}
64
Michal Nazarewicz47118af2011-12-29 13:09:50 +010065static inline bool migrate_async_suitable(int migratetype)
66{
67 return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
68}
69
Vlastimil Babka7d49d882014-10-09 15:27:11 -070070/*
71 * Check that the whole (or subset of) a pageblock given by the interval of
72 * [start_pfn, end_pfn) is valid and within the same zone, before scanning it
73 * with the migration of free compaction scanner. The scanners then need to
74 * use only pfn_valid_within() check for arches that allow holes within
75 * pageblocks.
76 *
77 * Return struct page pointer of start_pfn, or NULL if checks were not passed.
78 *
79 * It's possible on some configurations to have a setup like node0 node1 node0
80 * i.e. it's possible that all pages within a zones range of pages do not
81 * belong to a single zone. We assume that a border between node0 and node1
82 * can occur within a single pageblock, but not a node0 node1 node0
83 * interleaving within a single pageblock. It is therefore sufficient to check
84 * the first and last page of a pageblock and avoid checking each individual
85 * page in a pageblock.
86 */
87static struct page *pageblock_pfn_to_page(unsigned long start_pfn,
88 unsigned long end_pfn, struct zone *zone)
89{
90 struct page *start_page;
91 struct page *end_page;
92
93 /* end_pfn is one past the range we are checking */
94 end_pfn--;
95
96 if (!pfn_valid(start_pfn) || !pfn_valid(end_pfn))
97 return NULL;
98
99 start_page = pfn_to_page(start_pfn);
100
101 if (page_zone(start_page) != zone)
102 return NULL;
103
104 end_page = pfn_to_page(end_pfn);
105
106 /* This gives a shorter code than deriving page_zone(end_page) */
107 if (page_zone_id(start_page) != page_zone_id(end_page))
108 return NULL;
109
110 return start_page;
111}
112
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700113#ifdef CONFIG_COMPACTION
114/* Returns true if the pageblock should be scanned for pages to isolate. */
115static inline bool isolation_suitable(struct compact_control *cc,
116 struct page *page)
117{
118 if (cc->ignore_skip_hint)
119 return true;
120
121 return !get_pageblock_skip(page);
122}
123
124/*
125 * This function is called to clear all cached information on pageblocks that
126 * should be skipped for page isolation when the migrate and free page scanner
127 * meet.
128 */
Mel Gorman62997022012-10-08 16:32:47 -0700129static void __reset_isolation_suitable(struct zone *zone)
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700130{
131 unsigned long start_pfn = zone->zone_start_pfn;
Cody P Schafer108bcc92013-02-22 16:35:23 -0800132 unsigned long end_pfn = zone_end_pfn(zone);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700133 unsigned long pfn;
134
David Rientjes35979ef2014-06-04 16:08:27 -0700135 zone->compact_cached_migrate_pfn[0] = start_pfn;
136 zone->compact_cached_migrate_pfn[1] = start_pfn;
Mel Gormanc89511a2012-10-08 16:32:45 -0700137 zone->compact_cached_free_pfn = end_pfn;
Mel Gorman62997022012-10-08 16:32:47 -0700138 zone->compact_blockskip_flush = false;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700139
140 /* Walk the zone and mark every pageblock as suitable for isolation */
141 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
142 struct page *page;
143
144 cond_resched();
145
146 if (!pfn_valid(pfn))
147 continue;
148
149 page = pfn_to_page(pfn);
150 if (zone != page_zone(page))
151 continue;
152
153 clear_pageblock_skip(page);
154 }
155}
156
Mel Gorman62997022012-10-08 16:32:47 -0700157void reset_isolation_suitable(pg_data_t *pgdat)
158{
159 int zoneid;
160
161 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
162 struct zone *zone = &pgdat->node_zones[zoneid];
163 if (!populated_zone(zone))
164 continue;
165
166 /* Only flush if a full compaction finished recently */
167 if (zone->compact_blockskip_flush)
168 __reset_isolation_suitable(zone);
169 }
170}
171
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700172/*
173 * If no pages were isolated then mark this pageblock to be skipped in the
Mel Gorman62997022012-10-08 16:32:47 -0700174 * future. The information is later cleared by __reset_isolation_suitable().
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700175 */
Mel Gormanc89511a2012-10-08 16:32:45 -0700176static void update_pageblock_skip(struct compact_control *cc,
177 struct page *page, unsigned long nr_isolated,
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700178 bool migrate_scanner)
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700179{
Mel Gormanc89511a2012-10-08 16:32:45 -0700180 struct zone *zone = cc->zone;
David Rientjes35979ef2014-06-04 16:08:27 -0700181 unsigned long pfn;
Joonsoo Kim6815bf32013-12-18 17:08:52 -0800182
183 if (cc->ignore_skip_hint)
184 return;
185
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700186 if (!page)
187 return;
188
David Rientjes35979ef2014-06-04 16:08:27 -0700189 if (nr_isolated)
190 return;
191
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700192 set_pageblock_skip(page);
Mel Gormanc89511a2012-10-08 16:32:45 -0700193
David Rientjes35979ef2014-06-04 16:08:27 -0700194 pfn = page_to_pfn(page);
195
196 /* Update where async and sync compaction should restart */
197 if (migrate_scanner) {
198 if (cc->finished_update_migrate)
199 return;
200 if (pfn > zone->compact_cached_migrate_pfn[0])
201 zone->compact_cached_migrate_pfn[0] = pfn;
David Rientjese0b9dae2014-06-04 16:08:28 -0700202 if (cc->mode != MIGRATE_ASYNC &&
203 pfn > zone->compact_cached_migrate_pfn[1])
David Rientjes35979ef2014-06-04 16:08:27 -0700204 zone->compact_cached_migrate_pfn[1] = pfn;
205 } else {
206 if (cc->finished_update_free)
207 return;
208 if (pfn < zone->compact_cached_free_pfn)
209 zone->compact_cached_free_pfn = pfn;
Mel Gormanc89511a2012-10-08 16:32:45 -0700210 }
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700211}
212#else
213static inline bool isolation_suitable(struct compact_control *cc,
214 struct page *page)
215{
216 return true;
217}
218
Mel Gormanc89511a2012-10-08 16:32:45 -0700219static void update_pageblock_skip(struct compact_control *cc,
220 struct page *page, unsigned long nr_isolated,
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700221 bool migrate_scanner)
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700222{
223}
224#endif /* CONFIG_COMPACTION */
225
Vlastimil Babka1f9efde2014-10-09 15:27:14 -0700226static int should_release_lock(spinlock_t *lock)
Mel Gorman2a1402a2012-10-08 16:32:33 -0700227{
Vlastimil Babka1f9efde2014-10-09 15:27:14 -0700228 /*
229 * Sched contention has higher priority here as we may potentially
230 * have to abort whole compaction ASAP. Returning with lock contention
231 * means we will try another zone, and further decisions are
232 * influenced only when all zones are lock contended. That means
233 * potentially missing a lock contention is less critical.
234 */
235 if (need_resched())
236 return COMPACT_CONTENDED_SCHED;
237 else if (spin_is_contended(lock))
238 return COMPACT_CONTENDED_LOCK;
239
240 return COMPACT_CONTENDED_NONE;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700241}
242
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100243/*
Mel Gormanc67fe372012-08-21 16:16:17 -0700244 * Compaction requires the taking of some coarse locks that are potentially
245 * very heavily contended. Check if the process needs to be scheduled or
246 * if the lock is contended. For async compaction, back out in the event
247 * if contention is severe. For sync compaction, schedule.
248 *
249 * Returns true if the lock is held.
250 * Returns false if the lock is released and compaction should abort
251 */
252static bool compact_checklock_irqsave(spinlock_t *lock, unsigned long *flags,
253 bool locked, struct compact_control *cc)
254{
Vlastimil Babka1f9efde2014-10-09 15:27:14 -0700255 int contended = should_release_lock(lock);
256
257 if (contended) {
Mel Gormanc67fe372012-08-21 16:16:17 -0700258 if (locked) {
259 spin_unlock_irqrestore(lock, *flags);
260 locked = false;
261 }
262
263 /* async aborts if taking too long or contended */
David Rientjese0b9dae2014-06-04 16:08:28 -0700264 if (cc->mode == MIGRATE_ASYNC) {
Vlastimil Babka1f9efde2014-10-09 15:27:14 -0700265 cc->contended = contended;
Mel Gormanc67fe372012-08-21 16:16:17 -0700266 return false;
267 }
268
269 cond_resched();
Mel Gormanc67fe372012-08-21 16:16:17 -0700270 }
271
272 if (!locked)
273 spin_lock_irqsave(lock, *flags);
274 return true;
275}
276
Vlastimil Babkabe976572014-06-04 16:10:41 -0700277/*
278 * Aside from avoiding lock contention, compaction also periodically checks
279 * need_resched() and either schedules in sync compaction or aborts async
280 * compaction. This is similar to what compact_checklock_irqsave() does, but
281 * is used where no lock is concerned.
282 *
283 * Returns false when no scheduling was needed, or sync compaction scheduled.
284 * Returns true when async compaction should abort.
285 */
286static inline bool compact_should_abort(struct compact_control *cc)
287{
288 /* async compaction aborts if contended */
289 if (need_resched()) {
290 if (cc->mode == MIGRATE_ASYNC) {
Vlastimil Babka1f9efde2014-10-09 15:27:14 -0700291 cc->contended = COMPACT_CONTENDED_SCHED;
Vlastimil Babkabe976572014-06-04 16:10:41 -0700292 return true;
293 }
294
295 cond_resched();
296 }
297
298 return false;
299}
300
Mel Gormanf40d1e42012-10-08 16:32:36 -0700301/* Returns true if the page is within a block suitable for migration to */
302static bool suitable_migration_target(struct page *page)
303{
Joonsoo Kim7d348b92014-04-07 15:37:03 -0700304 /* If the page is a large free page, then disallow migration */
Mel Gormanf40d1e42012-10-08 16:32:36 -0700305 if (PageBuddy(page) && page_order(page) >= pageblock_order)
Joonsoo Kim7d348b92014-04-07 15:37:03 -0700306 return false;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700307
308 /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
Joonsoo Kim7d348b92014-04-07 15:37:03 -0700309 if (migrate_async_suitable(get_pageblock_migratetype(page)))
Mel Gormanf40d1e42012-10-08 16:32:36 -0700310 return true;
311
312 /* Otherwise skip the block */
313 return false;
314}
315
Mel Gormanc67fe372012-08-21 16:16:17 -0700316/*
Jerome Marchand9e4be472013-11-12 15:07:12 -0800317 * Isolate free pages onto a private freelist. If @strict is true, will abort
318 * returning 0 on any invalid PFNs or non-free pages inside of the pageblock
319 * (even though it may still end up isolating some pages).
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100320 */
Mel Gormanf40d1e42012-10-08 16:32:36 -0700321static unsigned long isolate_freepages_block(struct compact_control *cc,
322 unsigned long blockpfn,
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100323 unsigned long end_pfn,
324 struct list_head *freelist,
325 bool strict)
Mel Gorman748446b2010-05-24 14:32:27 -0700326{
Mel Gormanb7aba692011-01-13 15:45:54 -0800327 int nr_scanned = 0, total_isolated = 0;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700328 struct page *cursor, *valid_page = NULL;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700329 unsigned long flags;
330 bool locked = false;
Mel Gorman748446b2010-05-24 14:32:27 -0700331
Mel Gorman748446b2010-05-24 14:32:27 -0700332 cursor = pfn_to_page(blockpfn);
333
Mel Gormanf40d1e42012-10-08 16:32:36 -0700334 /* Isolate free pages. */
Mel Gorman748446b2010-05-24 14:32:27 -0700335 for (; blockpfn < end_pfn; blockpfn++, cursor++) {
336 int isolated, i;
337 struct page *page = cursor;
338
Mel Gormanb7aba692011-01-13 15:45:54 -0800339 nr_scanned++;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700340 if (!pfn_valid_within(blockpfn))
Laura Abbott2af120b2014-03-10 15:49:44 -0700341 goto isolate_fail;
342
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700343 if (!valid_page)
344 valid_page = page;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700345 if (!PageBuddy(page))
Laura Abbott2af120b2014-03-10 15:49:44 -0700346 goto isolate_fail;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700347
348 /*
349 * The zone lock must be held to isolate freepages.
350 * Unfortunately this is a very coarse lock and can be
351 * heavily contended if there are parallel allocations
352 * or parallel compactions. For async compaction do not
353 * spin on the lock and we acquire the lock as late as
354 * possible.
355 */
356 locked = compact_checklock_irqsave(&cc->zone->lock, &flags,
357 locked, cc);
358 if (!locked)
359 break;
360
Mel Gormanf40d1e42012-10-08 16:32:36 -0700361 /* Recheck this is a buddy page under lock */
362 if (!PageBuddy(page))
Laura Abbott2af120b2014-03-10 15:49:44 -0700363 goto isolate_fail;
Mel Gorman748446b2010-05-24 14:32:27 -0700364
365 /* Found a free page, break it into order-0 pages */
366 isolated = split_free_page(page);
367 total_isolated += isolated;
368 for (i = 0; i < isolated; i++) {
369 list_add(&page->lru, freelist);
370 page++;
371 }
372
373 /* If a page was split, advance to the end of it */
374 if (isolated) {
375 blockpfn += isolated - 1;
376 cursor += isolated - 1;
Laura Abbott2af120b2014-03-10 15:49:44 -0700377 continue;
Mel Gorman748446b2010-05-24 14:32:27 -0700378 }
Laura Abbott2af120b2014-03-10 15:49:44 -0700379
380isolate_fail:
381 if (strict)
382 break;
383 else
384 continue;
385
Mel Gorman748446b2010-05-24 14:32:27 -0700386 }
387
Mel Gormanb7aba692011-01-13 15:45:54 -0800388 trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
Mel Gormanf40d1e42012-10-08 16:32:36 -0700389
390 /*
391 * If strict isolation is requested by CMA then check that all the
392 * pages requested were isolated. If there were any failures, 0 is
393 * returned and CMA will fail.
394 */
Laura Abbott2af120b2014-03-10 15:49:44 -0700395 if (strict && blockpfn < end_pfn)
Mel Gormanf40d1e42012-10-08 16:32:36 -0700396 total_isolated = 0;
397
398 if (locked)
399 spin_unlock_irqrestore(&cc->zone->lock, flags);
400
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700401 /* Update the pageblock-skip if the whole pageblock was scanned */
402 if (blockpfn == end_pfn)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700403 update_pageblock_skip(cc, valid_page, total_isolated, false);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700404
Minchan Kim010fc292012-12-20 15:05:06 -0800405 count_compact_events(COMPACTFREE_SCANNED, nr_scanned);
Mel Gorman397487d2012-10-19 12:00:10 +0100406 if (total_isolated)
Minchan Kim010fc292012-12-20 15:05:06 -0800407 count_compact_events(COMPACTISOLATED, total_isolated);
Mel Gorman748446b2010-05-24 14:32:27 -0700408 return total_isolated;
409}
410
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100411/**
412 * isolate_freepages_range() - isolate free pages.
413 * @start_pfn: The first PFN to start isolating.
414 * @end_pfn: The one-past-last PFN.
415 *
416 * Non-free pages, invalid PFNs, or zone boundaries within the
417 * [start_pfn, end_pfn) range are considered errors, cause function to
418 * undo its actions and return zero.
419 *
420 * Otherwise, function returns one-past-the-last PFN of isolated page
421 * (which may be greater then end_pfn if end fell in a middle of
422 * a free page).
423 */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100424unsigned long
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700425isolate_freepages_range(struct compact_control *cc,
426 unsigned long start_pfn, unsigned long end_pfn)
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100427{
Mel Gormanf40d1e42012-10-08 16:32:36 -0700428 unsigned long isolated, pfn, block_end_pfn;
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100429 LIST_HEAD(freelist);
430
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700431 pfn = start_pfn;
432 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100433
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700434 for (; pfn < end_pfn; pfn += isolated,
435 block_end_pfn += pageblock_nr_pages) {
436
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100437 block_end_pfn = min(block_end_pfn, end_pfn);
438
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700439 if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone))
440 break;
441
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700442 isolated = isolate_freepages_block(cc, pfn, block_end_pfn,
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100443 &freelist, true);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100444
445 /*
446 * In strict mode, isolate_freepages_block() returns 0 if
447 * there are any holes in the block (ie. invalid PFNs or
448 * non-free pages).
449 */
450 if (!isolated)
451 break;
452
453 /*
454 * If we managed to isolate pages, it is always (1 << n) *
455 * pageblock_nr_pages for some non-negative n. (Max order
456 * page may span two pageblocks).
457 */
458 }
459
460 /* split_free_page does not map the pages */
461 map_pages(&freelist);
462
463 if (pfn < end_pfn) {
464 /* Loop terminated early, cleanup. */
465 release_freepages(&freelist);
466 return 0;
467 }
468
469 /* We don't use freelists for anything. */
470 return pfn;
471}
472
Mel Gorman748446b2010-05-24 14:32:27 -0700473/* Update the number of anon and file isolated pages in the zone */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700474static void acct_isolated(struct zone *zone, struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700475{
476 struct page *page;
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700477 unsigned int count[2] = { 0, };
Mel Gorman748446b2010-05-24 14:32:27 -0700478
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700479 if (list_empty(&cc->migratepages))
480 return;
481
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700482 list_for_each_entry(page, &cc->migratepages, lru)
483 count[!!page_is_file_cache(page)]++;
Mel Gorman748446b2010-05-24 14:32:27 -0700484
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700485 mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
486 mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
Mel Gorman748446b2010-05-24 14:32:27 -0700487}
488
489/* Similar to reclaim, but different enough that they don't share logic */
490static bool too_many_isolated(struct zone *zone)
491{
Minchan Kimbc693042010-09-09 16:38:00 -0700492 unsigned long active, inactive, isolated;
Mel Gorman748446b2010-05-24 14:32:27 -0700493
494 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
495 zone_page_state(zone, NR_INACTIVE_ANON);
Minchan Kimbc693042010-09-09 16:38:00 -0700496 active = zone_page_state(zone, NR_ACTIVE_FILE) +
497 zone_page_state(zone, NR_ACTIVE_ANON);
Mel Gorman748446b2010-05-24 14:32:27 -0700498 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
499 zone_page_state(zone, NR_ISOLATED_ANON);
500
Minchan Kimbc693042010-09-09 16:38:00 -0700501 return isolated > (inactive + active) / 2;
Mel Gorman748446b2010-05-24 14:32:27 -0700502}
503
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100504/**
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700505 * isolate_migratepages_block() - isolate all migrate-able pages within
506 * a single pageblock
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100507 * @cc: Compaction control structure.
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700508 * @low_pfn: The first PFN to isolate
509 * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock
510 * @isolate_mode: Isolation mode to be used.
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100511 *
512 * Isolate all pages that can be migrated from the range specified by
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700513 * [low_pfn, end_pfn). The range is expected to be within same pageblock.
514 * Returns zero if there is a fatal signal pending, otherwise PFN of the
515 * first page that was not scanned (which may be both less, equal to or more
516 * than end_pfn).
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100517 *
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700518 * The pages are isolated on cc->migratepages list (not required to be empty),
519 * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field
520 * is neither read nor updated.
Mel Gorman748446b2010-05-24 14:32:27 -0700521 */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700522static unsigned long
523isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
524 unsigned long end_pfn, isolate_mode_t isolate_mode)
Mel Gorman748446b2010-05-24 14:32:27 -0700525{
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700526 struct zone *zone = cc->zone;
Mel Gormanb7aba692011-01-13 15:45:54 -0800527 unsigned long nr_scanned = 0, nr_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700528 struct list_head *migratelist = &cc->migratepages;
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700529 struct lruvec *lruvec;
Mel Gormanc67fe372012-08-21 16:16:17 -0700530 unsigned long flags;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700531 bool locked = false;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700532 struct page *page = NULL, *valid_page = NULL;
Mel Gorman748446b2010-05-24 14:32:27 -0700533
Mel Gorman748446b2010-05-24 14:32:27 -0700534 /*
535 * Ensure that there are not too many pages isolated from the LRU
536 * list by either parallel reclaimers or compaction. If there are,
537 * delay for some time until fewer pages are isolated
538 */
539 while (unlikely(too_many_isolated(zone))) {
Mel Gormanf9e35b32011-06-15 15:08:52 -0700540 /* async migration should just abort */
David Rientjese0b9dae2014-06-04 16:08:28 -0700541 if (cc->mode == MIGRATE_ASYNC)
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100542 return 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700543
Mel Gorman748446b2010-05-24 14:32:27 -0700544 congestion_wait(BLK_RW_ASYNC, HZ/10);
545
546 if (fatal_signal_pending(current))
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100547 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700548 }
549
Vlastimil Babkabe976572014-06-04 16:10:41 -0700550 if (compact_should_abort(cc))
551 return 0;
David Rientjesaeef4b82014-06-04 16:08:31 -0700552
Mel Gorman748446b2010-05-24 14:32:27 -0700553 /* Time to isolate some pages for migration */
Mel Gorman748446b2010-05-24 14:32:27 -0700554 for (; low_pfn < end_pfn; low_pfn++) {
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700555 /* give a chance to irqs before checking need_resched() */
Joonsoo Kimbe1aa032014-04-07 15:37:05 -0700556 if (locked && !(low_pfn % SWAP_CLUSTER_MAX)) {
Mel Gorman2a1402a2012-10-08 16:32:33 -0700557 if (should_release_lock(&zone->lru_lock)) {
558 spin_unlock_irqrestore(&zone->lru_lock, flags);
559 locked = false;
560 }
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700561 }
Mel Gormanc67fe372012-08-21 16:16:17 -0700562
Mel Gorman748446b2010-05-24 14:32:27 -0700563 if (!pfn_valid_within(low_pfn))
564 continue;
Mel Gormanb7aba692011-01-13 15:45:54 -0800565 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700566
Mel Gorman748446b2010-05-24 14:32:27 -0700567 page = pfn_to_page(low_pfn);
Mel Gormandc908602012-02-08 17:13:38 -0800568
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700569 if (!valid_page)
570 valid_page = page;
571
Mel Gorman6c144662014-01-23 15:53:38 -0800572 /*
573 * Skip if free. page_order cannot be used without zone->lock
574 * as nothing prevents parallel allocations or buddy merging.
575 */
Mel Gorman748446b2010-05-24 14:32:27 -0700576 if (PageBuddy(page))
577 continue;
578
Mel Gorman9927af742011-01-13 15:45:59 -0800579 /*
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800580 * Check may be lockless but that's ok as we recheck later.
581 * It's possible to migrate LRU pages and balloon pages
582 * Skip any other type of page
583 */
584 if (!PageLRU(page)) {
585 if (unlikely(balloon_page_movable(page))) {
586 if (locked && balloon_page_isolate(page)) {
587 /* Successfully isolated */
Joonsoo Kimb6c75012014-04-07 15:37:07 -0700588 goto isolate_success;
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800589 }
590 }
Andrea Arcangelibc835012011-01-13 15:47:08 -0800591 continue;
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800592 }
Andrea Arcangelibc835012011-01-13 15:47:08 -0800593
594 /*
Mel Gorman2a1402a2012-10-08 16:32:33 -0700595 * PageLRU is set. lru_lock normally excludes isolation
596 * splitting and collapsing (collapsing has already happened
597 * if PageLRU is set) but the lock is not necessarily taken
598 * here and it is wasteful to take it just to check transhuge.
599 * Check TransHuge without lock and skip the whole pageblock if
600 * it's either a transhuge or hugetlbfs page, as calling
601 * compound_order() without preventing THP from splitting the
602 * page underneath us may return surprising results.
Andrea Arcangelibc835012011-01-13 15:47:08 -0800603 */
604 if (PageTransHuge(page)) {
Mel Gorman2a1402a2012-10-08 16:32:33 -0700605 if (!locked)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700606 low_pfn = ALIGN(low_pfn + 1,
607 pageblock_nr_pages) - 1;
608 else
609 low_pfn += (1 << compound_order(page)) - 1;
610
Mel Gorman2a1402a2012-10-08 16:32:33 -0700611 continue;
612 }
613
David Rientjes119d6d52014-04-03 14:48:00 -0700614 /*
615 * Migration will fail if an anonymous page is pinned in memory,
616 * so avoid taking lru_lock and isolating it unnecessarily in an
617 * admittedly racy check.
618 */
619 if (!page_mapping(page) &&
620 page_count(page) > page_mapcount(page))
621 continue;
622
Mel Gorman2a1402a2012-10-08 16:32:33 -0700623 /* Check if it is ok to still hold the lock */
624 locked = compact_checklock_irqsave(&zone->lru_lock, &flags,
625 locked, cc);
626 if (!locked || fatal_signal_pending(current))
627 break;
628
629 /* Recheck PageLRU and PageTransHuge under lock */
630 if (!PageLRU(page))
631 continue;
632 if (PageTransHuge(page)) {
Andrea Arcangelibc835012011-01-13 15:47:08 -0800633 low_pfn += (1 << compound_order(page)) - 1;
634 continue;
635 }
636
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700637 lruvec = mem_cgroup_page_lruvec(page, zone);
638
Mel Gorman748446b2010-05-24 14:32:27 -0700639 /* Try isolate the page */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700640 if (__isolate_lru_page(page, isolate_mode) != 0)
Mel Gorman748446b2010-05-24 14:32:27 -0700641 continue;
642
Sasha Levin309381fea2014-01-23 15:52:54 -0800643 VM_BUG_ON_PAGE(PageTransCompound(page), page);
Andrea Arcangelibc835012011-01-13 15:47:08 -0800644
Mel Gorman748446b2010-05-24 14:32:27 -0700645 /* Successfully isolated */
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700646 del_page_from_lru_list(page, lruvec, page_lru(page));
Joonsoo Kimb6c75012014-04-07 15:37:07 -0700647
648isolate_success:
649 cc->finished_update_migrate = true;
Mel Gorman748446b2010-05-24 14:32:27 -0700650 list_add(&page->lru, migratelist);
Mel Gorman748446b2010-05-24 14:32:27 -0700651 cc->nr_migratepages++;
Mel Gormanb7aba692011-01-13 15:45:54 -0800652 nr_isolated++;
Mel Gorman748446b2010-05-24 14:32:27 -0700653
654 /* Avoid isolating too much */
Hillf Danton31b83842012-01-10 15:07:59 -0800655 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
656 ++low_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700657 break;
Hillf Danton31b83842012-01-10 15:07:59 -0800658 }
Mel Gorman748446b2010-05-24 14:32:27 -0700659 }
660
Mel Gormanc67fe372012-08-21 16:16:17 -0700661 if (locked)
662 spin_unlock_irqrestore(&zone->lru_lock, flags);
Mel Gorman748446b2010-05-24 14:32:27 -0700663
Vlastimil Babka50b5b092014-01-21 15:51:10 -0800664 /*
665 * Update the pageblock-skip information and cached scanner pfn,
666 * if the whole pageblock was scanned without isolating any page.
Vlastimil Babka50b5b092014-01-21 15:51:10 -0800667 */
David Rientjes35979ef2014-06-04 16:08:27 -0700668 if (low_pfn == end_pfn)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700669 update_pageblock_skip(cc, valid_page, nr_isolated, true);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700670
Mel Gormanb7aba692011-01-13 15:45:54 -0800671 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
672
Minchan Kim010fc292012-12-20 15:05:06 -0800673 count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned);
Mel Gorman397487d2012-10-19 12:00:10 +0100674 if (nr_isolated)
Minchan Kim010fc292012-12-20 15:05:06 -0800675 count_compact_events(COMPACTISOLATED, nr_isolated);
Mel Gorman397487d2012-10-19 12:00:10 +0100676
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100677 return low_pfn;
678}
679
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700680/**
681 * isolate_migratepages_range() - isolate migrate-able pages in a PFN range
682 * @cc: Compaction control structure.
683 * @start_pfn: The first PFN to start isolating.
684 * @end_pfn: The one-past-last PFN.
685 *
686 * Returns zero if isolation fails fatally due to e.g. pending signal.
687 * Otherwise, function returns one-past-the-last PFN of isolated page
688 * (which may be greater than end_pfn if end fell in a middle of a THP page).
689 */
690unsigned long
691isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn,
692 unsigned long end_pfn)
693{
694 unsigned long pfn, block_end_pfn;
695
696 /* Scan block by block. First and last block may be incomplete */
697 pfn = start_pfn;
698 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
699
700 for (; pfn < end_pfn; pfn = block_end_pfn,
701 block_end_pfn += pageblock_nr_pages) {
702
703 block_end_pfn = min(block_end_pfn, end_pfn);
704
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700705 if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone))
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700706 continue;
707
708 pfn = isolate_migratepages_block(cc, pfn, block_end_pfn,
709 ISOLATE_UNEVICTABLE);
710
711 /*
712 * In case of fatal failure, release everything that might
713 * have been isolated in the previous iteration, and signal
714 * the failure back to caller.
715 */
716 if (!pfn) {
717 putback_movable_pages(&cc->migratepages);
718 cc->nr_migratepages = 0;
719 break;
720 }
721 }
722 acct_isolated(cc->zone, cc);
723
724 return pfn;
725}
726
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100727#endif /* CONFIG_COMPACTION || CONFIG_CMA */
728#ifdef CONFIG_COMPACTION
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100729/*
730 * Based on information in the current compact_control, find blocks
731 * suitable for isolating free pages from and then isolate them.
732 */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700733static void isolate_freepages(struct compact_control *cc)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100734{
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700735 struct zone *zone = cc->zone;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100736 struct page *page;
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700737 unsigned long block_start_pfn; /* start of current pageblock */
738 unsigned long block_end_pfn; /* end of current pageblock */
739 unsigned long low_pfn; /* lowest pfn scanner is able to scan */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100740 int nr_freepages = cc->nr_freepages;
741 struct list_head *freelist = &cc->freepages;
742
743 /*
744 * Initialise the free scanner. The starting point is where we last
Vlastimil Babka49e068f2014-05-06 12:50:03 -0700745 * successfully isolated from, zone-cached value, or the end of the
746 * zone when isolating for the first time. We need this aligned to
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700747 * the pageblock boundary, because we do
748 * block_start_pfn -= pageblock_nr_pages in the for loop.
749 * For ending point, take care when isolating in last pageblock of a
750 * a zone which ends in the middle of a pageblock.
Vlastimil Babka49e068f2014-05-06 12:50:03 -0700751 * The low boundary is the end of the pageblock the migration scanner
752 * is using.
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100753 */
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700754 block_start_pfn = cc->free_pfn & ~(pageblock_nr_pages-1);
755 block_end_pfn = min(block_start_pfn + pageblock_nr_pages,
756 zone_end_pfn(zone));
Vlastimil Babka7ed695e2014-01-21 15:51:09 -0800757 low_pfn = ALIGN(cc->migrate_pfn + 1, pageblock_nr_pages);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100758
759 /*
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100760 * Isolate free pages until enough are available to migrate the
761 * pages on cc->migratepages. We stop searching if the migrate
762 * and free page scanners meet or enough free pages are isolated.
763 */
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700764 for (; block_start_pfn >= low_pfn && cc->nr_migratepages > nr_freepages;
765 block_end_pfn = block_start_pfn,
766 block_start_pfn -= pageblock_nr_pages) {
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100767 unsigned long isolated;
768
David Rientjesf6ea3ad2013-09-30 13:45:03 -0700769 /*
770 * This can iterate a massively long zone without finding any
771 * suitable migration targets, so periodically check if we need
Vlastimil Babkabe976572014-06-04 16:10:41 -0700772 * to schedule, or even abort async compaction.
David Rientjesf6ea3ad2013-09-30 13:45:03 -0700773 */
Vlastimil Babkabe976572014-06-04 16:10:41 -0700774 if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
775 && compact_should_abort(cc))
776 break;
David Rientjesf6ea3ad2013-09-30 13:45:03 -0700777
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700778 page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
779 zone);
780 if (!page)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100781 continue;
782
783 /* Check the block is suitable for migration */
Linus Torvalds68e3e922012-06-03 20:05:57 -0700784 if (!suitable_migration_target(page))
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100785 continue;
Linus Torvalds68e3e922012-06-03 20:05:57 -0700786
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700787 /* If isolation recently failed, do not retry */
788 if (!isolation_suitable(cc, page))
789 continue;
790
Mel Gormanf40d1e42012-10-08 16:32:36 -0700791 /* Found a block suitable for isolating free pages from */
Vlastimil Babkae9ade562014-06-04 16:08:34 -0700792 cc->free_pfn = block_start_pfn;
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700793 isolated = isolate_freepages_block(cc, block_start_pfn,
794 block_end_pfn, freelist, false);
Mel Gormanf40d1e42012-10-08 16:32:36 -0700795 nr_freepages += isolated;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100796
797 /*
Vlastimil Babkae9ade562014-06-04 16:08:34 -0700798 * Set a flag that we successfully isolated in this pageblock.
799 * In the next loop iteration, zone->compact_cached_free_pfn
800 * will not be updated and thus it will effectively contain the
801 * highest pageblock we isolated pages from.
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100802 */
Vlastimil Babkae9ade562014-06-04 16:08:34 -0700803 if (isolated)
Mel Gormanc89511a2012-10-08 16:32:45 -0700804 cc->finished_update_free = true;
Vlastimil Babkabe976572014-06-04 16:10:41 -0700805
806 /*
807 * isolate_freepages_block() might have aborted due to async
808 * compaction being contended
809 */
810 if (cc->contended)
811 break;
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100812 }
813
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100814 /* split_free_page does not map the pages */
815 map_pages(freelist);
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100816
Vlastimil Babka7ed695e2014-01-21 15:51:09 -0800817 /*
818 * If we crossed the migrate scanner, we want to keep it that way
819 * so that compact_finished() may detect this
820 */
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700821 if (block_start_pfn < low_pfn)
Vlastimil Babkae9ade562014-06-04 16:08:34 -0700822 cc->free_pfn = cc->migrate_pfn;
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700823
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100824 cc->nr_freepages = nr_freepages;
Mel Gorman748446b2010-05-24 14:32:27 -0700825}
826
827/*
828 * This is a migrate-callback that "allocates" freepages by taking pages
829 * from the isolated freelists in the block we are migrating to.
830 */
831static struct page *compaction_alloc(struct page *migratepage,
832 unsigned long data,
833 int **result)
834{
835 struct compact_control *cc = (struct compact_control *)data;
836 struct page *freepage;
837
Vlastimil Babkabe976572014-06-04 16:10:41 -0700838 /*
839 * Isolate free pages if necessary, and if we are not aborting due to
840 * contention.
841 */
Mel Gorman748446b2010-05-24 14:32:27 -0700842 if (list_empty(&cc->freepages)) {
Vlastimil Babkabe976572014-06-04 16:10:41 -0700843 if (!cc->contended)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700844 isolate_freepages(cc);
Mel Gorman748446b2010-05-24 14:32:27 -0700845
846 if (list_empty(&cc->freepages))
847 return NULL;
848 }
849
850 freepage = list_entry(cc->freepages.next, struct page, lru);
851 list_del(&freepage->lru);
852 cc->nr_freepages--;
853
854 return freepage;
855}
856
857/*
David Rientjesd53aea32014-06-04 16:08:26 -0700858 * This is a migrate-callback that "frees" freepages back to the isolated
859 * freelist. All pages on the freelist are from the same zone, so there is no
860 * special handling needed for NUMA.
861 */
862static void compaction_free(struct page *page, unsigned long data)
863{
864 struct compact_control *cc = (struct compact_control *)data;
865
866 list_add(&page->lru, &cc->freepages);
867 cc->nr_freepages++;
868}
869
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100870/* possible outcome of isolate_migratepages */
871typedef enum {
872 ISOLATE_ABORT, /* Abort compaction now */
873 ISOLATE_NONE, /* No pages isolated, continue scanning */
874 ISOLATE_SUCCESS, /* Pages isolated, migrate */
875} isolate_migrate_t;
876
877/*
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700878 * Isolate all pages that can be migrated from the first suitable block,
879 * starting at the block pointed to by the migrate scanner pfn within
880 * compact_control.
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100881 */
882static isolate_migrate_t isolate_migratepages(struct zone *zone,
883 struct compact_control *cc)
884{
885 unsigned long low_pfn, end_pfn;
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700886 struct page *page;
887 const isolate_mode_t isolate_mode =
888 (cc->mode == MIGRATE_ASYNC ? ISOLATE_ASYNC_MIGRATE : 0);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100889
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700890 /*
891 * Start at where we last stopped, or beginning of the zone as
892 * initialized by compact_zone()
893 */
894 low_pfn = cc->migrate_pfn;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100895
896 /* Only scan within a pageblock boundary */
Mel Gormana9aacbc2013-02-22 16:32:25 -0800897 end_pfn = ALIGN(low_pfn + 1, pageblock_nr_pages);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100898
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700899 /*
900 * Iterate over whole pageblocks until we find the first suitable.
901 * Do not cross the free scanner.
902 */
903 for (; end_pfn <= cc->free_pfn;
904 low_pfn = end_pfn, end_pfn += pageblock_nr_pages) {
905
906 /*
907 * This can potentially iterate a massively long zone with
908 * many pageblocks unsuitable, so periodically check if we
909 * need to schedule, or even abort async compaction.
910 */
911 if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
912 && compact_should_abort(cc))
913 break;
914
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700915 page = pageblock_pfn_to_page(low_pfn, end_pfn, zone);
916 if (!page)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700917 continue;
918
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700919 /* If isolation recently failed, do not retry */
920 if (!isolation_suitable(cc, page))
921 continue;
922
923 /*
924 * For async compaction, also only scan in MOVABLE blocks.
925 * Async compaction is optimistic to see if the minimum amount
926 * of work satisfies the allocation.
927 */
928 if (cc->mode == MIGRATE_ASYNC &&
929 !migrate_async_suitable(get_pageblock_migratetype(page)))
930 continue;
931
932 /* Perform the isolation */
933 low_pfn = isolate_migratepages_block(cc, low_pfn, end_pfn,
934 isolate_mode);
935
936 if (!low_pfn || cc->contended)
937 return ISOLATE_ABORT;
938
939 /*
940 * Either we isolated something and proceed with migration. Or
941 * we failed and compact_zone should decide if we should
942 * continue or not.
943 */
944 break;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100945 }
946
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700947 acct_isolated(zone, cc);
948 /* Record where migration scanner will be restarted */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100949 cc->migrate_pfn = low_pfn;
950
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700951 return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100952}
953
Mel Gorman748446b2010-05-24 14:32:27 -0700954static int compact_finished(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800955 struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700956{
Mel Gorman8fb74b92013-01-11 14:32:16 -0800957 unsigned int order;
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800958 unsigned long watermark;
Mel Gorman56de7262010-05-24 14:32:30 -0700959
Vlastimil Babkabe976572014-06-04 16:10:41 -0700960 if (cc->contended || fatal_signal_pending(current))
Mel Gorman748446b2010-05-24 14:32:27 -0700961 return COMPACT_PARTIAL;
962
Mel Gorman753341a2012-10-08 16:32:40 -0700963 /* Compaction run completes if the migrate and free scanner meet */
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700964 if (cc->free_pfn <= cc->migrate_pfn) {
Vlastimil Babka55b7c4c2014-01-21 15:51:11 -0800965 /* Let the next compaction start anew. */
David Rientjes35979ef2014-06-04 16:08:27 -0700966 zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn;
967 zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn;
Vlastimil Babka55b7c4c2014-01-21 15:51:11 -0800968 zone->compact_cached_free_pfn = zone_end_pfn(zone);
969
Mel Gorman62997022012-10-08 16:32:47 -0700970 /*
971 * Mark that the PG_migrate_skip information should be cleared
972 * by kswapd when it goes to sleep. kswapd does not set the
973 * flag itself as the decision to be clear should be directly
974 * based on an allocation request.
975 */
976 if (!current_is_kswapd())
977 zone->compact_blockskip_flush = true;
978
Mel Gorman748446b2010-05-24 14:32:27 -0700979 return COMPACT_COMPLETE;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700980 }
Mel Gorman748446b2010-05-24 14:32:27 -0700981
Johannes Weiner82478fb2011-01-20 14:44:21 -0800982 /*
983 * order == -1 is expected when compacting via
984 * /proc/sys/vm/compact_memory
985 */
Mel Gorman56de7262010-05-24 14:32:30 -0700986 if (cc->order == -1)
987 return COMPACT_CONTINUE;
988
Michal Hocko3957c772011-06-15 15:08:25 -0700989 /* Compaction run is not finished if the watermark is not met */
990 watermark = low_wmark_pages(zone);
991 watermark += (1 << cc->order);
992
993 if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
994 return COMPACT_CONTINUE;
995
Mel Gorman56de7262010-05-24 14:32:30 -0700996 /* Direct compactor: Is a suitable page free? */
Mel Gorman8fb74b92013-01-11 14:32:16 -0800997 for (order = cc->order; order < MAX_ORDER; order++) {
998 struct free_area *area = &zone->free_area[order];
Mel Gorman56de7262010-05-24 14:32:30 -0700999
Mel Gorman8fb74b92013-01-11 14:32:16 -08001000 /* Job done if page is free of the right migratetype */
1001 if (!list_empty(&area->free_list[cc->migratetype]))
1002 return COMPACT_PARTIAL;
1003
1004 /* Job done if allocation would set block type */
1005 if (cc->order >= pageblock_order && area->nr_free)
1006 return COMPACT_PARTIAL;
Mel Gorman56de7262010-05-24 14:32:30 -07001007 }
1008
Mel Gorman748446b2010-05-24 14:32:27 -07001009 return COMPACT_CONTINUE;
1010}
1011
Mel Gorman3e7d3442011-01-13 15:45:56 -08001012/*
1013 * compaction_suitable: Is this suitable to run compaction on this zone now?
1014 * Returns
1015 * COMPACT_SKIPPED - If there are too few free pages for compaction
1016 * COMPACT_PARTIAL - If the allocation would succeed without compaction
1017 * COMPACT_CONTINUE - If compaction should run now
1018 */
1019unsigned long compaction_suitable(struct zone *zone, int order)
1020{
1021 int fragindex;
1022 unsigned long watermark;
1023
1024 /*
Michal Hocko3957c772011-06-15 15:08:25 -07001025 * order == -1 is expected when compacting via
1026 * /proc/sys/vm/compact_memory
1027 */
1028 if (order == -1)
1029 return COMPACT_CONTINUE;
1030
1031 /*
Mel Gorman3e7d3442011-01-13 15:45:56 -08001032 * Watermarks for order-0 must be met for compaction. Note the 2UL.
1033 * This is because during migration, copies of pages need to be
1034 * allocated and for a short time, the footprint is higher
1035 */
1036 watermark = low_wmark_pages(zone) + (2UL << order);
1037 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
1038 return COMPACT_SKIPPED;
1039
1040 /*
1041 * fragmentation index determines if allocation failures are due to
1042 * low memory or external fragmentation
1043 *
Shaohua Lia582a732011-06-15 15:08:49 -07001044 * index of -1000 implies allocations might succeed depending on
1045 * watermarks
Mel Gorman3e7d3442011-01-13 15:45:56 -08001046 * index towards 0 implies failure is due to lack of memory
1047 * index towards 1000 implies failure is due to fragmentation
1048 *
1049 * Only compact if a failure would be due to fragmentation.
1050 */
1051 fragindex = fragmentation_index(zone, order);
1052 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
1053 return COMPACT_SKIPPED;
1054
Shaohua Lia582a732011-06-15 15:08:49 -07001055 if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
1056 0, 0))
Mel Gorman3e7d3442011-01-13 15:45:56 -08001057 return COMPACT_PARTIAL;
1058
1059 return COMPACT_CONTINUE;
1060}
1061
Mel Gorman748446b2010-05-24 14:32:27 -07001062static int compact_zone(struct zone *zone, struct compact_control *cc)
1063{
1064 int ret;
Mel Gormanc89511a2012-10-08 16:32:45 -07001065 unsigned long start_pfn = zone->zone_start_pfn;
Cody P Schafer108bcc92013-02-22 16:35:23 -08001066 unsigned long end_pfn = zone_end_pfn(zone);
David Rientjese0b9dae2014-06-04 16:08:28 -07001067 const bool sync = cc->mode != MIGRATE_ASYNC;
Mel Gorman748446b2010-05-24 14:32:27 -07001068
Mel Gorman3e7d3442011-01-13 15:45:56 -08001069 ret = compaction_suitable(zone, cc->order);
1070 switch (ret) {
1071 case COMPACT_PARTIAL:
1072 case COMPACT_SKIPPED:
1073 /* Compaction is likely to fail */
1074 return ret;
1075 case COMPACT_CONTINUE:
1076 /* Fall through to compaction */
1077 ;
1078 }
1079
Mel Gormanc89511a2012-10-08 16:32:45 -07001080 /*
Vlastimil Babkad3132e42014-01-21 15:51:08 -08001081 * Clear pageblock skip if there were failures recently and compaction
1082 * is about to be retried after being deferred. kswapd does not do
1083 * this reset as it'll reset the cached information when going to sleep.
1084 */
1085 if (compaction_restarting(zone, cc->order) && !current_is_kswapd())
1086 __reset_isolation_suitable(zone);
1087
1088 /*
Mel Gormanc89511a2012-10-08 16:32:45 -07001089 * Setup to move all movable pages to the end of the zone. Used cached
1090 * information on where the scanners should start but check that it
1091 * is initialised by ensuring the values are within zone boundaries.
1092 */
David Rientjese0b9dae2014-06-04 16:08:28 -07001093 cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync];
Mel Gormanc89511a2012-10-08 16:32:45 -07001094 cc->free_pfn = zone->compact_cached_free_pfn;
1095 if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) {
1096 cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1);
1097 zone->compact_cached_free_pfn = cc->free_pfn;
1098 }
1099 if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) {
1100 cc->migrate_pfn = start_pfn;
David Rientjes35979ef2014-06-04 16:08:27 -07001101 zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn;
1102 zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn;
Mel Gormanc89511a2012-10-08 16:32:45 -07001103 }
Mel Gorman748446b2010-05-24 14:32:27 -07001104
Mel Gorman0eb927c2014-01-21 15:51:05 -08001105 trace_mm_compaction_begin(start_pfn, cc->migrate_pfn, cc->free_pfn, end_pfn);
1106
Mel Gorman748446b2010-05-24 14:32:27 -07001107 migrate_prep_local();
1108
1109 while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
Minchan Kim9d502c12011-03-22 16:30:39 -07001110 int err;
Mel Gorman748446b2010-05-24 14:32:27 -07001111
Mel Gormanf9e35b32011-06-15 15:08:52 -07001112 switch (isolate_migratepages(zone, cc)) {
1113 case ISOLATE_ABORT:
1114 ret = COMPACT_PARTIAL;
Rafael Aquini5733c7d2012-12-11 16:02:47 -08001115 putback_movable_pages(&cc->migratepages);
Shaohua Lie64c5232012-10-08 16:32:27 -07001116 cc->nr_migratepages = 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -07001117 goto out;
1118 case ISOLATE_NONE:
Mel Gorman748446b2010-05-24 14:32:27 -07001119 continue;
Mel Gormanf9e35b32011-06-15 15:08:52 -07001120 case ISOLATE_SUCCESS:
1121 ;
1122 }
Mel Gorman748446b2010-05-24 14:32:27 -07001123
David Rientjesd53aea32014-06-04 16:08:26 -07001124 err = migrate_pages(&cc->migratepages, compaction_alloc,
David Rientjese0b9dae2014-06-04 16:08:28 -07001125 compaction_free, (unsigned long)cc, cc->mode,
Mel Gorman7b2a2d42012-10-19 14:07:31 +01001126 MR_COMPACTION);
Mel Gorman748446b2010-05-24 14:32:27 -07001127
Vlastimil Babkaf8c93012014-06-04 16:08:32 -07001128 trace_mm_compaction_migratepages(cc->nr_migratepages, err,
1129 &cc->migratepages);
Mel Gorman748446b2010-05-24 14:32:27 -07001130
Vlastimil Babkaf8c93012014-06-04 16:08:32 -07001131 /* All pages were either migrated or will be released */
1132 cc->nr_migratepages = 0;
Minchan Kim9d502c12011-03-22 16:30:39 -07001133 if (err) {
Rafael Aquini5733c7d2012-12-11 16:02:47 -08001134 putback_movable_pages(&cc->migratepages);
Vlastimil Babka7ed695e2014-01-21 15:51:09 -08001135 /*
1136 * migrate_pages() may return -ENOMEM when scanners meet
1137 * and we want compact_finished() to detect it
1138 */
1139 if (err == -ENOMEM && cc->free_pfn > cc->migrate_pfn) {
David Rientjes4bf2bba2012-07-11 14:02:13 -07001140 ret = COMPACT_PARTIAL;
1141 goto out;
1142 }
Mel Gorman748446b2010-05-24 14:32:27 -07001143 }
Mel Gorman748446b2010-05-24 14:32:27 -07001144 }
1145
Mel Gormanf9e35b32011-06-15 15:08:52 -07001146out:
Mel Gorman748446b2010-05-24 14:32:27 -07001147 /* Release free pages and check accounting */
1148 cc->nr_freepages -= release_freepages(&cc->freepages);
1149 VM_BUG_ON(cc->nr_freepages != 0);
1150
Mel Gorman0eb927c2014-01-21 15:51:05 -08001151 trace_mm_compaction_end(ret);
1152
Mel Gorman748446b2010-05-24 14:32:27 -07001153 return ret;
1154}
Mel Gorman76ab0f52010-05-24 14:32:28 -07001155
David Rientjese0b9dae2014-06-04 16:08:28 -07001156static unsigned long compact_zone_order(struct zone *zone, int order,
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001157 gfp_t gfp_mask, enum migrate_mode mode, int *contended)
Mel Gorman56de7262010-05-24 14:32:30 -07001158{
Shaohua Lie64c5232012-10-08 16:32:27 -07001159 unsigned long ret;
Mel Gorman56de7262010-05-24 14:32:30 -07001160 struct compact_control cc = {
1161 .nr_freepages = 0,
1162 .nr_migratepages = 0,
1163 .order = order,
1164 .migratetype = allocflags_to_migratetype(gfp_mask),
1165 .zone = zone,
David Rientjese0b9dae2014-06-04 16:08:28 -07001166 .mode = mode,
Mel Gorman56de7262010-05-24 14:32:30 -07001167 };
1168 INIT_LIST_HEAD(&cc.freepages);
1169 INIT_LIST_HEAD(&cc.migratepages);
1170
Shaohua Lie64c5232012-10-08 16:32:27 -07001171 ret = compact_zone(zone, &cc);
1172
1173 VM_BUG_ON(!list_empty(&cc.freepages));
1174 VM_BUG_ON(!list_empty(&cc.migratepages));
1175
1176 *contended = cc.contended;
1177 return ret;
Mel Gorman56de7262010-05-24 14:32:30 -07001178}
1179
Mel Gorman5e771902010-05-24 14:32:31 -07001180int sysctl_extfrag_threshold = 500;
1181
Mel Gorman56de7262010-05-24 14:32:30 -07001182/**
1183 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
1184 * @zonelist: The zonelist used for the current allocation
1185 * @order: The order of the current allocation
1186 * @gfp_mask: The GFP mask of the current allocation
1187 * @nodemask: The allowed nodes to allocate from
David Rientjese0b9dae2014-06-04 16:08:28 -07001188 * @mode: The migration mode for async, sync light, or sync migration
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001189 * @contended: Return value that determines if compaction was aborted due to
1190 * need_resched() or lock contention
Vlastimil Babka53853e22014-10-09 15:27:02 -07001191 * @candidate_zone: Return the zone where we think allocation should succeed
Mel Gorman56de7262010-05-24 14:32:30 -07001192 *
1193 * This is the main entry point for direct page compaction.
1194 */
1195unsigned long try_to_compact_pages(struct zonelist *zonelist,
Mel Gorman77f1fe62011-01-13 15:45:57 -08001196 int order, gfp_t gfp_mask, nodemask_t *nodemask,
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001197 enum migrate_mode mode, int *contended,
Vlastimil Babka53853e22014-10-09 15:27:02 -07001198 struct zone **candidate_zone)
Mel Gorman56de7262010-05-24 14:32:30 -07001199{
1200 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
1201 int may_enter_fs = gfp_mask & __GFP_FS;
1202 int may_perform_io = gfp_mask & __GFP_IO;
Mel Gorman56de7262010-05-24 14:32:30 -07001203 struct zoneref *z;
1204 struct zone *zone;
Vlastimil Babka53853e22014-10-09 15:27:02 -07001205 int rc = COMPACT_DEFERRED;
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001206 int alloc_flags = 0;
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001207 int all_zones_contended = COMPACT_CONTENDED_LOCK; /* init for &= op */
1208
1209 *contended = COMPACT_CONTENDED_NONE;
Mel Gorman56de7262010-05-24 14:32:30 -07001210
Mel Gorman4ffb6332012-10-08 16:29:09 -07001211 /* Check if the GFP flags allow compaction */
Andrea Arcangelic5a73c32011-01-13 15:47:11 -08001212 if (!order || !may_enter_fs || !may_perform_io)
Vlastimil Babka53853e22014-10-09 15:27:02 -07001213 return COMPACT_SKIPPED;
Mel Gorman56de7262010-05-24 14:32:30 -07001214
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001215#ifdef CONFIG_CMA
1216 if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
1217 alloc_flags |= ALLOC_CMA;
1218#endif
Mel Gorman56de7262010-05-24 14:32:30 -07001219 /* Compact each zone in the list */
1220 for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
1221 nodemask) {
Mel Gorman56de7262010-05-24 14:32:30 -07001222 int status;
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001223 int zone_contended;
Mel Gorman56de7262010-05-24 14:32:30 -07001224
Vlastimil Babka53853e22014-10-09 15:27:02 -07001225 if (compaction_deferred(zone, order))
1226 continue;
1227
David Rientjese0b9dae2014-06-04 16:08:28 -07001228 status = compact_zone_order(zone, order, gfp_mask, mode,
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001229 &zone_contended);
Mel Gorman56de7262010-05-24 14:32:30 -07001230 rc = max(status, rc);
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001231 /*
1232 * It takes at least one zone that wasn't lock contended
1233 * to clear all_zones_contended.
1234 */
1235 all_zones_contended &= zone_contended;
Mel Gorman56de7262010-05-24 14:32:30 -07001236
Mel Gorman3e7d3442011-01-13 15:45:56 -08001237 /* If a normal allocation would succeed, stop compacting */
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001238 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
Vlastimil Babka53853e22014-10-09 15:27:02 -07001239 alloc_flags)) {
1240 *candidate_zone = zone;
1241 /*
1242 * We think the allocation will succeed in this zone,
1243 * but it is not certain, hence the false. The caller
1244 * will repeat this with true if allocation indeed
1245 * succeeds in this zone.
1246 */
1247 compaction_defer_reset(zone, order, false);
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001248 /*
1249 * It is possible that async compaction aborted due to
1250 * need_resched() and the watermarks were ok thanks to
1251 * somebody else freeing memory. The allocation can
1252 * however still fail so we better signal the
1253 * need_resched() contention anyway (this will not
1254 * prevent the allocation attempt).
1255 */
1256 if (zone_contended == COMPACT_CONTENDED_SCHED)
1257 *contended = COMPACT_CONTENDED_SCHED;
1258
1259 goto break_loop;
1260 }
1261
1262 if (mode != MIGRATE_ASYNC) {
Vlastimil Babka53853e22014-10-09 15:27:02 -07001263 /*
1264 * We think that allocation won't succeed in this zone
1265 * so we defer compaction there. If it ends up
1266 * succeeding after all, it will be reset.
1267 */
1268 defer_compaction(zone, order);
1269 }
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001270
1271 /*
1272 * We might have stopped compacting due to need_resched() in
1273 * async compaction, or due to a fatal signal detected. In that
1274 * case do not try further zones and signal need_resched()
1275 * contention.
1276 */
1277 if ((zone_contended == COMPACT_CONTENDED_SCHED)
1278 || fatal_signal_pending(current)) {
1279 *contended = COMPACT_CONTENDED_SCHED;
1280 goto break_loop;
1281 }
1282
1283 continue;
1284break_loop:
1285 /*
1286 * We might not have tried all the zones, so be conservative
1287 * and assume they are not all lock contended.
1288 */
1289 all_zones_contended = 0;
1290 break;
Mel Gorman56de7262010-05-24 14:32:30 -07001291 }
1292
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001293 /*
1294 * If at least one zone wasn't deferred or skipped, we report if all
1295 * zones that were tried were lock contended.
1296 */
1297 if (rc > COMPACT_SKIPPED && all_zones_contended)
1298 *contended = COMPACT_CONTENDED_LOCK;
1299
Mel Gorman56de7262010-05-24 14:32:30 -07001300 return rc;
1301}
1302
1303
Mel Gorman76ab0f52010-05-24 14:32:28 -07001304/* Compact all zones within a node */
Andrew Morton7103f162013-02-22 16:32:33 -08001305static void __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001306{
1307 int zoneid;
Mel Gorman76ab0f52010-05-24 14:32:28 -07001308 struct zone *zone;
1309
Mel Gorman76ab0f52010-05-24 14:32:28 -07001310 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
Mel Gorman76ab0f52010-05-24 14:32:28 -07001311
1312 zone = &pgdat->node_zones[zoneid];
1313 if (!populated_zone(zone))
1314 continue;
1315
Rik van Riel7be62de2012-03-21 16:33:52 -07001316 cc->nr_freepages = 0;
1317 cc->nr_migratepages = 0;
1318 cc->zone = zone;
1319 INIT_LIST_HEAD(&cc->freepages);
1320 INIT_LIST_HEAD(&cc->migratepages);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001321
Dan Carpenteraad6ec32012-03-21 16:33:54 -07001322 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
Rik van Riel7be62de2012-03-21 16:33:52 -07001323 compact_zone(zone, cc);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001324
Rik van Rielaff62242012-03-21 16:33:52 -07001325 if (cc->order > 0) {
Vlastimil Babkade6c60a2014-01-21 15:51:07 -08001326 if (zone_watermark_ok(zone, cc->order,
1327 low_wmark_pages(zone), 0, 0))
1328 compaction_defer_reset(zone, cc->order, false);
Rik van Rielaff62242012-03-21 16:33:52 -07001329 }
1330
Rik van Riel7be62de2012-03-21 16:33:52 -07001331 VM_BUG_ON(!list_empty(&cc->freepages));
1332 VM_BUG_ON(!list_empty(&cc->migratepages));
Mel Gorman76ab0f52010-05-24 14:32:28 -07001333 }
Mel Gorman76ab0f52010-05-24 14:32:28 -07001334}
1335
Andrew Morton7103f162013-02-22 16:32:33 -08001336void compact_pgdat(pg_data_t *pgdat, int order)
Rik van Riel7be62de2012-03-21 16:33:52 -07001337{
1338 struct compact_control cc = {
1339 .order = order,
David Rientjese0b9dae2014-06-04 16:08:28 -07001340 .mode = MIGRATE_ASYNC,
Rik van Riel7be62de2012-03-21 16:33:52 -07001341 };
1342
Mel Gorman3a7200a2013-09-11 14:22:19 -07001343 if (!order)
1344 return;
1345
Andrew Morton7103f162013-02-22 16:32:33 -08001346 __compact_pgdat(pgdat, &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -07001347}
1348
Andrew Morton7103f162013-02-22 16:32:33 -08001349static void compact_node(int nid)
Rik van Riel7be62de2012-03-21 16:33:52 -07001350{
Rik van Riel7be62de2012-03-21 16:33:52 -07001351 struct compact_control cc = {
1352 .order = -1,
David Rientjese0b9dae2014-06-04 16:08:28 -07001353 .mode = MIGRATE_SYNC,
David Rientjes91ca9182014-04-03 14:47:23 -07001354 .ignore_skip_hint = true,
Rik van Riel7be62de2012-03-21 16:33:52 -07001355 };
1356
Andrew Morton7103f162013-02-22 16:32:33 -08001357 __compact_pgdat(NODE_DATA(nid), &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -07001358}
1359
Mel Gorman76ab0f52010-05-24 14:32:28 -07001360/* Compact all nodes in the system */
Jason Liu7964c062013-01-11 14:31:47 -08001361static void compact_nodes(void)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001362{
1363 int nid;
1364
Hugh Dickins8575ec22012-03-21 16:33:53 -07001365 /* Flush pending updates to the LRU lists */
1366 lru_add_drain_all();
1367
Mel Gorman76ab0f52010-05-24 14:32:28 -07001368 for_each_online_node(nid)
1369 compact_node(nid);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001370}
1371
1372/* The written value is actually unused, all memory is compacted */
1373int sysctl_compact_memory;
1374
1375/* This is the entry point for compacting all nodes via /proc/sys/vm */
1376int sysctl_compaction_handler(struct ctl_table *table, int write,
1377 void __user *buffer, size_t *length, loff_t *ppos)
1378{
1379 if (write)
Jason Liu7964c062013-01-11 14:31:47 -08001380 compact_nodes();
Mel Gorman76ab0f52010-05-24 14:32:28 -07001381
1382 return 0;
1383}
Mel Gormaned4a6d72010-05-24 14:32:29 -07001384
Mel Gorman5e771902010-05-24 14:32:31 -07001385int sysctl_extfrag_handler(struct ctl_table *table, int write,
1386 void __user *buffer, size_t *length, loff_t *ppos)
1387{
1388 proc_dointvec_minmax(table, write, buffer, length, ppos);
1389
1390 return 0;
1391}
1392
Mel Gormaned4a6d72010-05-24 14:32:29 -07001393#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
Rashika Kheria74e77fb2014-04-03 14:48:01 -07001394static ssize_t sysfs_compact_node(struct device *dev,
Kay Sievers10fbcf42011-12-21 14:48:43 -08001395 struct device_attribute *attr,
Mel Gormaned4a6d72010-05-24 14:32:29 -07001396 const char *buf, size_t count)
1397{
Hugh Dickins8575ec22012-03-21 16:33:53 -07001398 int nid = dev->id;
1399
1400 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
1401 /* Flush pending updates to the LRU lists */
1402 lru_add_drain_all();
1403
1404 compact_node(nid);
1405 }
Mel Gormaned4a6d72010-05-24 14:32:29 -07001406
1407 return count;
1408}
Kay Sievers10fbcf42011-12-21 14:48:43 -08001409static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001410
1411int compaction_register_node(struct node *node)
1412{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001413 return device_create_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001414}
1415
1416void compaction_unregister_node(struct node *node)
1417{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001418 return device_remove_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001419}
1420#endif /* CONFIG_SYSFS && CONFIG_NUMA */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001421
1422#endif /* CONFIG_COMPACTION */