blob: be7729b706af573237c767e5bf8b329c930d6b61 [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>
Andrey Ryabinin93f051b2015-02-13 14:39:28 -080019#include <linux/kasan.h>
Mel Gorman748446b2010-05-24 14:32:27 -070020#include "internal.h"
21
Minchan Kim010fc292012-12-20 15:05:06 -080022#ifdef CONFIG_COMPACTION
23static inline void count_compact_event(enum vm_event_item item)
24{
25 count_vm_event(item);
26}
27
28static inline void count_compact_events(enum vm_event_item item, long delta)
29{
30 count_vm_events(item, delta);
31}
32#else
33#define count_compact_event(item) do { } while (0)
34#define count_compact_events(item, delta) do { } while (0)
35#endif
36
Michal Nazarewiczff9543f2011-12-29 13:09:50 +010037#if defined CONFIG_COMPACTION || defined CONFIG_CMA
38
Mel Gormanb7aba692011-01-13 15:45:54 -080039#define CREATE_TRACE_POINTS
40#include <trace/events/compaction.h>
41
Mel Gorman748446b2010-05-24 14:32:27 -070042static unsigned long release_freepages(struct list_head *freelist)
43{
44 struct page *page, *next;
45 unsigned long count = 0;
46
47 list_for_each_entry_safe(page, next, freelist, lru) {
48 list_del(&page->lru);
49 __free_page(page);
50 count++;
51 }
52
53 return count;
54}
55
Michal Nazarewiczff9543f2011-12-29 13:09:50 +010056static void map_pages(struct list_head *list)
57{
58 struct page *page;
59
60 list_for_each_entry(page, list, lru) {
61 arch_alloc_page(page, 0);
62 kernel_map_pages(page, 1, 1);
Andrey Ryabinin93f051b2015-02-13 14:39:28 -080063 kasan_alloc_pages(page, 0);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +010064 }
65}
66
Michal Nazarewicz47118af2011-12-29 13:09:50 +010067static inline bool migrate_async_suitable(int migratetype)
68{
69 return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
70}
71
Vlastimil Babka7d49d882014-10-09 15:27:11 -070072/*
73 * Check that the whole (or subset of) a pageblock given by the interval of
74 * [start_pfn, end_pfn) is valid and within the same zone, before scanning it
75 * with the migration of free compaction scanner. The scanners then need to
76 * use only pfn_valid_within() check for arches that allow holes within
77 * pageblocks.
78 *
79 * Return struct page pointer of start_pfn, or NULL if checks were not passed.
80 *
81 * It's possible on some configurations to have a setup like node0 node1 node0
82 * i.e. it's possible that all pages within a zones range of pages do not
83 * belong to a single zone. We assume that a border between node0 and node1
84 * can occur within a single pageblock, but not a node0 node1 node0
85 * interleaving within a single pageblock. It is therefore sufficient to check
86 * the first and last page of a pageblock and avoid checking each individual
87 * page in a pageblock.
88 */
89static struct page *pageblock_pfn_to_page(unsigned long start_pfn,
90 unsigned long end_pfn, struct zone *zone)
91{
92 struct page *start_page;
93 struct page *end_page;
94
95 /* end_pfn is one past the range we are checking */
96 end_pfn--;
97
98 if (!pfn_valid(start_pfn) || !pfn_valid(end_pfn))
99 return NULL;
100
101 start_page = pfn_to_page(start_pfn);
102
103 if (page_zone(start_page) != zone)
104 return NULL;
105
106 end_page = pfn_to_page(end_pfn);
107
108 /* This gives a shorter code than deriving page_zone(end_page) */
109 if (page_zone_id(start_page) != page_zone_id(end_page))
110 return NULL;
111
112 return start_page;
113}
114
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700115#ifdef CONFIG_COMPACTION
116/* Returns true if the pageblock should be scanned for pages to isolate. */
117static inline bool isolation_suitable(struct compact_control *cc,
118 struct page *page)
119{
120 if (cc->ignore_skip_hint)
121 return true;
122
123 return !get_pageblock_skip(page);
124}
125
126/*
127 * This function is called to clear all cached information on pageblocks that
128 * should be skipped for page isolation when the migrate and free page scanner
129 * meet.
130 */
Mel Gorman62997022012-10-08 16:32:47 -0700131static void __reset_isolation_suitable(struct zone *zone)
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700132{
133 unsigned long start_pfn = zone->zone_start_pfn;
Cody P Schafer108bcc92013-02-22 16:35:23 -0800134 unsigned long end_pfn = zone_end_pfn(zone);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700135 unsigned long pfn;
136
David Rientjes35979ef2014-06-04 16:08:27 -0700137 zone->compact_cached_migrate_pfn[0] = start_pfn;
138 zone->compact_cached_migrate_pfn[1] = start_pfn;
Mel Gormanc89511a2012-10-08 16:32:45 -0700139 zone->compact_cached_free_pfn = end_pfn;
Mel Gorman62997022012-10-08 16:32:47 -0700140 zone->compact_blockskip_flush = false;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700141
142 /* Walk the zone and mark every pageblock as suitable for isolation */
143 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
144 struct page *page;
145
146 cond_resched();
147
148 if (!pfn_valid(pfn))
149 continue;
150
151 page = pfn_to_page(pfn);
152 if (zone != page_zone(page))
153 continue;
154
155 clear_pageblock_skip(page);
156 }
157}
158
Mel Gorman62997022012-10-08 16:32:47 -0700159void reset_isolation_suitable(pg_data_t *pgdat)
160{
161 int zoneid;
162
163 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
164 struct zone *zone = &pgdat->node_zones[zoneid];
165 if (!populated_zone(zone))
166 continue;
167
168 /* Only flush if a full compaction finished recently */
169 if (zone->compact_blockskip_flush)
170 __reset_isolation_suitable(zone);
171 }
172}
173
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700174/*
175 * If no pages were isolated then mark this pageblock to be skipped in the
Mel Gorman62997022012-10-08 16:32:47 -0700176 * future. The information is later cleared by __reset_isolation_suitable().
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700177 */
Mel Gormanc89511a2012-10-08 16:32:45 -0700178static void update_pageblock_skip(struct compact_control *cc,
179 struct page *page, unsigned long nr_isolated,
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700180 bool migrate_scanner)
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700181{
Mel Gormanc89511a2012-10-08 16:32:45 -0700182 struct zone *zone = cc->zone;
David Rientjes35979ef2014-06-04 16:08:27 -0700183 unsigned long pfn;
Joonsoo Kim6815bf32013-12-18 17:08:52 -0800184
185 if (cc->ignore_skip_hint)
186 return;
187
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700188 if (!page)
189 return;
190
David Rientjes35979ef2014-06-04 16:08:27 -0700191 if (nr_isolated)
192 return;
193
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700194 set_pageblock_skip(page);
Mel Gormanc89511a2012-10-08 16:32:45 -0700195
David Rientjes35979ef2014-06-04 16:08:27 -0700196 pfn = page_to_pfn(page);
197
198 /* Update where async and sync compaction should restart */
199 if (migrate_scanner) {
200 if (cc->finished_update_migrate)
201 return;
202 if (pfn > zone->compact_cached_migrate_pfn[0])
203 zone->compact_cached_migrate_pfn[0] = pfn;
David Rientjese0b9dae2014-06-04 16:08:28 -0700204 if (cc->mode != MIGRATE_ASYNC &&
205 pfn > zone->compact_cached_migrate_pfn[1])
David Rientjes35979ef2014-06-04 16:08:27 -0700206 zone->compact_cached_migrate_pfn[1] = pfn;
207 } else {
208 if (cc->finished_update_free)
209 return;
210 if (pfn < zone->compact_cached_free_pfn)
211 zone->compact_cached_free_pfn = pfn;
Mel Gormanc89511a2012-10-08 16:32:45 -0700212 }
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700213}
214#else
215static inline bool isolation_suitable(struct compact_control *cc,
216 struct page *page)
217{
218 return true;
219}
220
Mel Gormanc89511a2012-10-08 16:32:45 -0700221static void update_pageblock_skip(struct compact_control *cc,
222 struct page *page, unsigned long nr_isolated,
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700223 bool migrate_scanner)
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700224{
225}
226#endif /* CONFIG_COMPACTION */
227
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700228/*
229 * Compaction requires the taking of some coarse locks that are potentially
230 * very heavily contended. For async compaction, back out if the lock cannot
231 * be taken immediately. For sync compaction, spin on the lock if needed.
232 *
233 * Returns true if the lock is held
234 * Returns false if the lock is not held and compaction should abort
235 */
236static bool compact_trylock_irqsave(spinlock_t *lock, unsigned long *flags,
237 struct compact_control *cc)
Mel Gorman2a1402a2012-10-08 16:32:33 -0700238{
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700239 if (cc->mode == MIGRATE_ASYNC) {
240 if (!spin_trylock_irqsave(lock, *flags)) {
241 cc->contended = COMPACT_CONTENDED_LOCK;
242 return false;
243 }
244 } else {
245 spin_lock_irqsave(lock, *flags);
246 }
Vlastimil Babka1f9efde2014-10-09 15:27:14 -0700247
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700248 return true;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700249}
250
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100251/*
Mel Gormanc67fe372012-08-21 16:16:17 -0700252 * Compaction requires the taking of some coarse locks that are potentially
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700253 * very heavily contended. The lock should be periodically unlocked to avoid
254 * having disabled IRQs for a long time, even when there is nobody waiting on
255 * the lock. It might also be that allowing the IRQs will result in
256 * need_resched() becoming true. If scheduling is needed, async compaction
257 * aborts. Sync compaction schedules.
258 * Either compaction type will also abort if a fatal signal is pending.
259 * In either case if the lock was locked, it is dropped and not regained.
Mel Gormanc67fe372012-08-21 16:16:17 -0700260 *
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700261 * Returns true if compaction should abort due to fatal signal pending, or
262 * async compaction due to need_resched()
263 * Returns false when compaction can continue (sync compaction might have
264 * scheduled)
Mel Gormanc67fe372012-08-21 16:16:17 -0700265 */
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700266static bool compact_unlock_should_abort(spinlock_t *lock,
267 unsigned long flags, bool *locked, struct compact_control *cc)
Mel Gormanc67fe372012-08-21 16:16:17 -0700268{
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700269 if (*locked) {
270 spin_unlock_irqrestore(lock, flags);
271 *locked = false;
272 }
Vlastimil Babka1f9efde2014-10-09 15:27:14 -0700273
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700274 if (fatal_signal_pending(current)) {
275 cc->contended = COMPACT_CONTENDED_SCHED;
276 return true;
277 }
Mel Gormanc67fe372012-08-21 16:16:17 -0700278
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700279 if (need_resched()) {
David Rientjese0b9dae2014-06-04 16:08:28 -0700280 if (cc->mode == MIGRATE_ASYNC) {
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700281 cc->contended = COMPACT_CONTENDED_SCHED;
282 return true;
Mel Gormanc67fe372012-08-21 16:16:17 -0700283 }
Mel Gormanc67fe372012-08-21 16:16:17 -0700284 cond_resched();
Mel Gormanc67fe372012-08-21 16:16:17 -0700285 }
286
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700287 return false;
Mel Gormanc67fe372012-08-21 16:16:17 -0700288}
289
Vlastimil Babkabe976572014-06-04 16:10:41 -0700290/*
291 * Aside from avoiding lock contention, compaction also periodically checks
292 * need_resched() and either schedules in sync compaction or aborts async
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700293 * compaction. This is similar to what compact_unlock_should_abort() does, but
Vlastimil Babkabe976572014-06-04 16:10:41 -0700294 * is used where no lock is concerned.
295 *
296 * Returns false when no scheduling was needed, or sync compaction scheduled.
297 * Returns true when async compaction should abort.
298 */
299static inline bool compact_should_abort(struct compact_control *cc)
300{
301 /* async compaction aborts if contended */
302 if (need_resched()) {
303 if (cc->mode == MIGRATE_ASYNC) {
Vlastimil Babka1f9efde2014-10-09 15:27:14 -0700304 cc->contended = COMPACT_CONTENDED_SCHED;
Vlastimil Babkabe976572014-06-04 16:10:41 -0700305 return true;
306 }
307
308 cond_resched();
309 }
310
311 return false;
312}
313
Mel Gormanf40d1e42012-10-08 16:32:36 -0700314/* Returns true if the page is within a block suitable for migration to */
315static bool suitable_migration_target(struct page *page)
316{
Joonsoo Kim7d348b92014-04-07 15:37:03 -0700317 /* If the page is a large free page, then disallow migration */
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700318 if (PageBuddy(page)) {
319 /*
320 * We are checking page_order without zone->lock taken. But
321 * the only small danger is that we skip a potentially suitable
322 * pageblock, so it's not worth to check order for valid range.
323 */
324 if (page_order_unsafe(page) >= pageblock_order)
325 return false;
326 }
Mel Gormanf40d1e42012-10-08 16:32:36 -0700327
328 /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
Joonsoo Kim7d348b92014-04-07 15:37:03 -0700329 if (migrate_async_suitable(get_pageblock_migratetype(page)))
Mel Gormanf40d1e42012-10-08 16:32:36 -0700330 return true;
331
332 /* Otherwise skip the block */
333 return false;
334}
335
Mel Gormanc67fe372012-08-21 16:16:17 -0700336/*
Jerome Marchand9e4be472013-11-12 15:07:12 -0800337 * Isolate free pages onto a private freelist. If @strict is true, will abort
338 * returning 0 on any invalid PFNs or non-free pages inside of the pageblock
339 * (even though it may still end up isolating some pages).
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100340 */
Mel Gormanf40d1e42012-10-08 16:32:36 -0700341static unsigned long isolate_freepages_block(struct compact_control *cc,
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700342 unsigned long *start_pfn,
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100343 unsigned long end_pfn,
344 struct list_head *freelist,
345 bool strict)
Mel Gorman748446b2010-05-24 14:32:27 -0700346{
Mel Gormanb7aba692011-01-13 15:45:54 -0800347 int nr_scanned = 0, total_isolated = 0;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700348 struct page *cursor, *valid_page = NULL;
Xiubo Lib8b2d822014-10-09 15:28:21 -0700349 unsigned long flags = 0;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700350 bool locked = false;
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700351 unsigned long blockpfn = *start_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700352
Mel Gorman748446b2010-05-24 14:32:27 -0700353 cursor = pfn_to_page(blockpfn);
354
Mel Gormanf40d1e42012-10-08 16:32:36 -0700355 /* Isolate free pages. */
Mel Gorman748446b2010-05-24 14:32:27 -0700356 for (; blockpfn < end_pfn; blockpfn++, cursor++) {
357 int isolated, i;
358 struct page *page = cursor;
359
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700360 /*
361 * Periodically drop the lock (if held) regardless of its
362 * contention, to give chance to IRQs. Abort if fatal signal
363 * pending or async compaction detects need_resched()
364 */
365 if (!(blockpfn % SWAP_CLUSTER_MAX)
366 && compact_unlock_should_abort(&cc->zone->lock, flags,
367 &locked, cc))
368 break;
369
Mel Gormanb7aba692011-01-13 15:45:54 -0800370 nr_scanned++;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700371 if (!pfn_valid_within(blockpfn))
Laura Abbott2af120b2014-03-10 15:49:44 -0700372 goto isolate_fail;
373
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700374 if (!valid_page)
375 valid_page = page;
Vlastimil Babkaf1f702e2015-09-08 15:02:49 -0700376
377 /*
378 * For compound pages such as THP and hugetlbfs, we can save
379 * potentially a lot of iterations if we skip them at once.
380 * The check is racy, but we can consider only valid values
381 * and the only danger is skipping too much.
382 */
383 if (PageCompound(page)) {
384 unsigned int comp_order = compound_order(page);
385
386 if (likely(comp_order < MAX_ORDER)) {
387 blockpfn += (1UL << comp_order) - 1;
388 cursor += (1UL << comp_order) - 1;
389 }
390
391 goto isolate_fail;
392 }
393
Mel Gormanf40d1e42012-10-08 16:32:36 -0700394 if (!PageBuddy(page))
Laura Abbott2af120b2014-03-10 15:49:44 -0700395 goto isolate_fail;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700396
397 /*
Vlastimil Babka69b71892014-10-09 15:27:18 -0700398 * If we already hold the lock, we can skip some rechecking.
399 * Note that if we hold the lock now, checked_pageblock was
400 * already set in some previous iteration (or strict is true),
401 * so it is correct to skip the suitable migration target
402 * recheck as well.
Mel Gormanf40d1e42012-10-08 16:32:36 -0700403 */
Vlastimil Babka69b71892014-10-09 15:27:18 -0700404 if (!locked) {
405 /*
406 * The zone lock must be held to isolate freepages.
407 * Unfortunately this is a very coarse lock and can be
408 * heavily contended if there are parallel allocations
409 * or parallel compactions. For async compaction do not
410 * spin on the lock and we acquire the lock as late as
411 * possible.
412 */
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700413 locked = compact_trylock_irqsave(&cc->zone->lock,
414 &flags, cc);
Vlastimil Babka69b71892014-10-09 15:27:18 -0700415 if (!locked)
416 break;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700417
Vlastimil Babka69b71892014-10-09 15:27:18 -0700418 /* Recheck this is a buddy page under lock */
419 if (!PageBuddy(page))
420 goto isolate_fail;
421 }
Mel Gorman748446b2010-05-24 14:32:27 -0700422
423 /* Found a free page, break it into order-0 pages */
424 isolated = split_free_page(page);
David Rientjes3c767522016-06-24 14:50:10 -0700425 if (!isolated)
426 break;
427
Mel Gorman748446b2010-05-24 14:32:27 -0700428 total_isolated += isolated;
David Rientjes3c767522016-06-24 14:50:10 -0700429 cc->nr_freepages += isolated;
Mel Gorman748446b2010-05-24 14:32:27 -0700430 for (i = 0; i < isolated; i++) {
431 list_add(&page->lru, freelist);
432 page++;
433 }
David Rientjes3c767522016-06-24 14:50:10 -0700434 if (!strict && cc->nr_migratepages <= cc->nr_freepages) {
435 blockpfn += isolated;
436 break;
Mel Gorman748446b2010-05-24 14:32:27 -0700437 }
David Rientjes3c767522016-06-24 14:50:10 -0700438 /* Advance to the end of split page */
439 blockpfn += isolated - 1;
440 cursor += isolated - 1;
441 continue;
Laura Abbott2af120b2014-03-10 15:49:44 -0700442
443isolate_fail:
444 if (strict)
445 break;
446 else
447 continue;
448
Mel Gorman748446b2010-05-24 14:32:27 -0700449 }
450
David Rientjes3c767522016-06-24 14:50:10 -0700451 if (locked)
452 spin_unlock_irqrestore(&cc->zone->lock, flags);
453
Vlastimil Babkaf1f702e2015-09-08 15:02:49 -0700454 /*
455 * There is a tiny chance that we have read bogus compound_order(),
456 * so be careful to not go outside of the pageblock.
457 */
458 if (unlikely(blockpfn > end_pfn))
459 blockpfn = end_pfn;
460
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700461 /* Record how far we have got within the block */
462 *start_pfn = blockpfn;
463
Mel Gormanb7aba692011-01-13 15:45:54 -0800464 trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
Mel Gormanf40d1e42012-10-08 16:32:36 -0700465
466 /*
467 * If strict isolation is requested by CMA then check that all the
468 * pages requested were isolated. If there were any failures, 0 is
469 * returned and CMA will fail.
470 */
Laura Abbott2af120b2014-03-10 15:49:44 -0700471 if (strict && blockpfn < end_pfn)
Mel Gormanf40d1e42012-10-08 16:32:36 -0700472 total_isolated = 0;
473
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700474 /* Update the pageblock-skip if the whole pageblock was scanned */
475 if (blockpfn == end_pfn)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700476 update_pageblock_skip(cc, valid_page, total_isolated, false);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700477
Minchan Kim010fc292012-12-20 15:05:06 -0800478 count_compact_events(COMPACTFREE_SCANNED, nr_scanned);
Mel Gorman397487d2012-10-19 12:00:10 +0100479 if (total_isolated)
Minchan Kim010fc292012-12-20 15:05:06 -0800480 count_compact_events(COMPACTISOLATED, total_isolated);
Mel Gorman748446b2010-05-24 14:32:27 -0700481 return total_isolated;
482}
483
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100484/**
485 * isolate_freepages_range() - isolate free pages.
486 * @start_pfn: The first PFN to start isolating.
487 * @end_pfn: The one-past-last PFN.
488 *
489 * Non-free pages, invalid PFNs, or zone boundaries within the
490 * [start_pfn, end_pfn) range are considered errors, cause function to
491 * undo its actions and return zero.
492 *
493 * Otherwise, function returns one-past-the-last PFN of isolated page
494 * (which may be greater then end_pfn if end fell in a middle of
495 * a free page).
496 */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100497unsigned long
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700498isolate_freepages_range(struct compact_control *cc,
499 unsigned long start_pfn, unsigned long end_pfn)
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100500{
Mel Gormanf40d1e42012-10-08 16:32:36 -0700501 unsigned long isolated, pfn, block_end_pfn;
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100502 LIST_HEAD(freelist);
503
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700504 pfn = start_pfn;
505 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100506
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700507 for (; pfn < end_pfn; pfn += isolated,
508 block_end_pfn += pageblock_nr_pages) {
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700509 /* Protect pfn from changing by isolate_freepages_block */
510 unsigned long isolate_start_pfn = pfn;
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700511
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100512 block_end_pfn = min(block_end_pfn, end_pfn);
513
Joonsoo Kim58420012014-11-13 15:19:07 -0800514 /*
515 * pfn could pass the block_end_pfn if isolated freepage
516 * is more than pageblock order. In this case, we adjust
517 * scanning range to right one.
518 */
519 if (pfn >= block_end_pfn) {
520 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
521 block_end_pfn = min(block_end_pfn, end_pfn);
522 }
523
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700524 if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone))
525 break;
526
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700527 isolated = isolate_freepages_block(cc, &isolate_start_pfn,
528 block_end_pfn, &freelist, true);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100529
530 /*
531 * In strict mode, isolate_freepages_block() returns 0 if
532 * there are any holes in the block (ie. invalid PFNs or
533 * non-free pages).
534 */
535 if (!isolated)
536 break;
537
538 /*
539 * If we managed to isolate pages, it is always (1 << n) *
540 * pageblock_nr_pages for some non-negative n. (Max order
541 * page may span two pageblocks).
542 */
543 }
544
545 /* split_free_page does not map the pages */
546 map_pages(&freelist);
547
548 if (pfn < end_pfn) {
549 /* Loop terminated early, cleanup. */
550 release_freepages(&freelist);
551 return 0;
552 }
553
554 /* We don't use freelists for anything. */
555 return pfn;
556}
557
Mel Gorman748446b2010-05-24 14:32:27 -0700558/* Update the number of anon and file isolated pages in the zone */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700559static void acct_isolated(struct zone *zone, struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700560{
561 struct page *page;
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700562 unsigned int count[2] = { 0, };
Mel Gorman748446b2010-05-24 14:32:27 -0700563
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700564 if (list_empty(&cc->migratepages))
565 return;
566
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700567 list_for_each_entry(page, &cc->migratepages, lru)
568 count[!!page_is_file_cache(page)]++;
Mel Gorman748446b2010-05-24 14:32:27 -0700569
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700570 mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
571 mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
Mel Gorman748446b2010-05-24 14:32:27 -0700572}
573
574/* Similar to reclaim, but different enough that they don't share logic */
575static bool too_many_isolated(struct zone *zone)
576{
Minchan Kimbc693042010-09-09 16:38:00 -0700577 unsigned long active, inactive, isolated;
Mel Gorman748446b2010-05-24 14:32:27 -0700578
579 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
580 zone_page_state(zone, NR_INACTIVE_ANON);
Minchan Kimbc693042010-09-09 16:38:00 -0700581 active = zone_page_state(zone, NR_ACTIVE_FILE) +
582 zone_page_state(zone, NR_ACTIVE_ANON);
Mel Gorman748446b2010-05-24 14:32:27 -0700583 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
584 zone_page_state(zone, NR_ISOLATED_ANON);
585
Minchan Kimbc693042010-09-09 16:38:00 -0700586 return isolated > (inactive + active) / 2;
Mel Gorman748446b2010-05-24 14:32:27 -0700587}
588
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100589/**
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700590 * isolate_migratepages_block() - isolate all migrate-able pages within
591 * a single pageblock
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100592 * @cc: Compaction control structure.
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700593 * @low_pfn: The first PFN to isolate
594 * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock
595 * @isolate_mode: Isolation mode to be used.
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100596 *
597 * Isolate all pages that can be migrated from the range specified by
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700598 * [low_pfn, end_pfn). The range is expected to be within same pageblock.
599 * Returns zero if there is a fatal signal pending, otherwise PFN of the
600 * first page that was not scanned (which may be both less, equal to or more
601 * than end_pfn).
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100602 *
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700603 * The pages are isolated on cc->migratepages list (not required to be empty),
604 * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field
605 * is neither read nor updated.
Mel Gorman748446b2010-05-24 14:32:27 -0700606 */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700607static unsigned long
608isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
609 unsigned long end_pfn, isolate_mode_t isolate_mode)
Mel Gorman748446b2010-05-24 14:32:27 -0700610{
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700611 struct zone *zone = cc->zone;
Mel Gormanb7aba692011-01-13 15:45:54 -0800612 unsigned long nr_scanned = 0, nr_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700613 struct list_head *migratelist = &cc->migratepages;
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700614 struct lruvec *lruvec;
Xiubo Lib8b2d822014-10-09 15:28:21 -0700615 unsigned long flags = 0;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700616 bool locked = false;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700617 struct page *page = NULL, *valid_page = NULL;
Mel Gorman748446b2010-05-24 14:32:27 -0700618
Mel Gorman748446b2010-05-24 14:32:27 -0700619 /*
620 * Ensure that there are not too many pages isolated from the LRU
621 * list by either parallel reclaimers or compaction. If there are,
622 * delay for some time until fewer pages are isolated
623 */
624 while (unlikely(too_many_isolated(zone))) {
Mel Gormanf9e35b32011-06-15 15:08:52 -0700625 /* async migration should just abort */
David Rientjese0b9dae2014-06-04 16:08:28 -0700626 if (cc->mode == MIGRATE_ASYNC)
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100627 return 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700628
Mel Gorman748446b2010-05-24 14:32:27 -0700629 congestion_wait(BLK_RW_ASYNC, HZ/10);
630
631 if (fatal_signal_pending(current))
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100632 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700633 }
634
Vlastimil Babkabe976572014-06-04 16:10:41 -0700635 if (compact_should_abort(cc))
636 return 0;
David Rientjesaeef4b82014-06-04 16:08:31 -0700637
Mel Gorman748446b2010-05-24 14:32:27 -0700638 /* Time to isolate some pages for migration */
Mel Gorman748446b2010-05-24 14:32:27 -0700639 for (; low_pfn < end_pfn; low_pfn++) {
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700640 /*
641 * Periodically drop the lock (if held) regardless of its
642 * contention, to give chance to IRQs. Abort async compaction
643 * if contended.
644 */
645 if (!(low_pfn % SWAP_CLUSTER_MAX)
646 && compact_unlock_should_abort(&zone->lru_lock, flags,
647 &locked, cc))
648 break;
Mel Gormanc67fe372012-08-21 16:16:17 -0700649
Mel Gorman748446b2010-05-24 14:32:27 -0700650 if (!pfn_valid_within(low_pfn))
651 continue;
Mel Gormanb7aba692011-01-13 15:45:54 -0800652 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700653
Mel Gorman748446b2010-05-24 14:32:27 -0700654 page = pfn_to_page(low_pfn);
Mel Gormandc908602012-02-08 17:13:38 -0800655
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700656 if (!valid_page)
657 valid_page = page;
658
Mel Gorman6c144662014-01-23 15:53:38 -0800659 /*
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700660 * Skip if free. We read page order here without zone lock
661 * which is generally unsafe, but the race window is small and
662 * the worst thing that can happen is that we skip some
663 * potential isolation targets.
Mel Gorman6c144662014-01-23 15:53:38 -0800664 */
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700665 if (PageBuddy(page)) {
666 unsigned long freepage_order = page_order_unsafe(page);
667
668 /*
669 * Without lock, we cannot be sure that what we got is
670 * a valid page order. Consider only values in the
671 * valid order range to prevent low_pfn overflow.
672 */
673 if (freepage_order > 0 && freepage_order < MAX_ORDER)
674 low_pfn += (1UL << freepage_order) - 1;
Mel Gorman748446b2010-05-24 14:32:27 -0700675 continue;
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700676 }
Mel Gorman748446b2010-05-24 14:32:27 -0700677
Mel Gorman9927af742011-01-13 15:45:59 -0800678 /*
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800679 * Check may be lockless but that's ok as we recheck later.
680 * It's possible to migrate LRU pages and balloon pages
681 * Skip any other type of page
682 */
683 if (!PageLRU(page)) {
684 if (unlikely(balloon_page_movable(page))) {
Konstantin Khlebnikovd6d86c02014-10-09 15:29:27 -0700685 if (balloon_page_isolate(page)) {
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800686 /* Successfully isolated */
Joonsoo Kimb6c75012014-04-07 15:37:07 -0700687 goto isolate_success;
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800688 }
689 }
Andrea Arcangelibc835012011-01-13 15:47:08 -0800690 continue;
Rafael Aquinibf6bddf2012-12-11 16:02:42 -0800691 }
Andrea Arcangelibc835012011-01-13 15:47:08 -0800692
693 /*
Mel Gorman2a1402a2012-10-08 16:32:33 -0700694 * PageLRU is set. lru_lock normally excludes isolation
695 * splitting and collapsing (collapsing has already happened
696 * if PageLRU is set) but the lock is not necessarily taken
697 * here and it is wasteful to take it just to check transhuge.
698 * Check TransHuge without lock and skip the whole pageblock if
699 * it's either a transhuge or hugetlbfs page, as calling
700 * compound_order() without preventing THP from splitting the
701 * page underneath us may return surprising results.
Andrea Arcangelibc835012011-01-13 15:47:08 -0800702 */
703 if (PageTransHuge(page)) {
Mel Gorman2a1402a2012-10-08 16:32:33 -0700704 if (!locked)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700705 low_pfn = ALIGN(low_pfn + 1,
706 pageblock_nr_pages) - 1;
707 else
708 low_pfn += (1 << compound_order(page)) - 1;
709
Mel Gorman2a1402a2012-10-08 16:32:33 -0700710 continue;
711 }
712
David Rientjes119d6d52014-04-03 14:48:00 -0700713 /*
714 * Migration will fail if an anonymous page is pinned in memory,
715 * so avoid taking lru_lock and isolating it unnecessarily in an
716 * admittedly racy check.
717 */
718 if (!page_mapping(page) &&
719 page_count(page) > page_mapcount(page))
720 continue;
721
Vlastimil Babka69b71892014-10-09 15:27:18 -0700722 /* If we already hold the lock, we can skip some rechecking */
723 if (!locked) {
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700724 locked = compact_trylock_irqsave(&zone->lru_lock,
725 &flags, cc);
Vlastimil Babka69b71892014-10-09 15:27:18 -0700726 if (!locked)
727 break;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700728
Vlastimil Babka69b71892014-10-09 15:27:18 -0700729 /* Recheck PageLRU and PageTransHuge under lock */
730 if (!PageLRU(page))
731 continue;
732 if (PageTransHuge(page)) {
733 low_pfn += (1 << compound_order(page)) - 1;
734 continue;
735 }
Andrea Arcangelibc835012011-01-13 15:47:08 -0800736 }
737
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700738 lruvec = mem_cgroup_page_lruvec(page, zone);
739
Mel Gorman748446b2010-05-24 14:32:27 -0700740 /* Try isolate the page */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700741 if (__isolate_lru_page(page, isolate_mode) != 0)
Mel Gorman748446b2010-05-24 14:32:27 -0700742 continue;
743
Sasha Levin309381fea2014-01-23 15:52:54 -0800744 VM_BUG_ON_PAGE(PageTransCompound(page), page);
Andrea Arcangelibc835012011-01-13 15:47:08 -0800745
Mel Gorman748446b2010-05-24 14:32:27 -0700746 /* Successfully isolated */
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700747 del_page_from_lru_list(page, lruvec, page_lru(page));
Joonsoo Kimb6c75012014-04-07 15:37:07 -0700748
749isolate_success:
750 cc->finished_update_migrate = true;
Mel Gorman748446b2010-05-24 14:32:27 -0700751 list_add(&page->lru, migratelist);
Mel Gorman748446b2010-05-24 14:32:27 -0700752 cc->nr_migratepages++;
Mel Gormanb7aba692011-01-13 15:45:54 -0800753 nr_isolated++;
Mel Gorman748446b2010-05-24 14:32:27 -0700754
755 /* Avoid isolating too much */
Hillf Danton31b83842012-01-10 15:07:59 -0800756 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
757 ++low_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700758 break;
Hillf Danton31b83842012-01-10 15:07:59 -0800759 }
Mel Gorman748446b2010-05-24 14:32:27 -0700760 }
761
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700762 /*
763 * The PageBuddy() check could have potentially brought us outside
764 * the range to be scanned.
765 */
766 if (unlikely(low_pfn > end_pfn))
767 low_pfn = end_pfn;
768
Mel Gormanc67fe372012-08-21 16:16:17 -0700769 if (locked)
770 spin_unlock_irqrestore(&zone->lru_lock, flags);
Mel Gorman748446b2010-05-24 14:32:27 -0700771
Vlastimil Babka50b5b092014-01-21 15:51:10 -0800772 /*
773 * Update the pageblock-skip information and cached scanner pfn,
774 * if the whole pageblock was scanned without isolating any page.
Vlastimil Babka50b5b092014-01-21 15:51:10 -0800775 */
David Rientjes35979ef2014-06-04 16:08:27 -0700776 if (low_pfn == end_pfn)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700777 update_pageblock_skip(cc, valid_page, nr_isolated, true);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700778
Mel Gormanb7aba692011-01-13 15:45:54 -0800779 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
780
Minchan Kim010fc292012-12-20 15:05:06 -0800781 count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned);
Mel Gorman397487d2012-10-19 12:00:10 +0100782 if (nr_isolated)
Minchan Kim010fc292012-12-20 15:05:06 -0800783 count_compact_events(COMPACTISOLATED, nr_isolated);
Mel Gorman397487d2012-10-19 12:00:10 +0100784
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100785 return low_pfn;
786}
787
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700788/**
789 * isolate_migratepages_range() - isolate migrate-able pages in a PFN range
790 * @cc: Compaction control structure.
791 * @start_pfn: The first PFN to start isolating.
792 * @end_pfn: The one-past-last PFN.
793 *
794 * Returns zero if isolation fails fatally due to e.g. pending signal.
795 * Otherwise, function returns one-past-the-last PFN of isolated page
796 * (which may be greater than end_pfn if end fell in a middle of a THP page).
797 */
798unsigned long
799isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn,
800 unsigned long end_pfn)
801{
802 unsigned long pfn, block_end_pfn;
803
804 /* Scan block by block. First and last block may be incomplete */
805 pfn = start_pfn;
806 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
807
808 for (; pfn < end_pfn; pfn = block_end_pfn,
809 block_end_pfn += pageblock_nr_pages) {
810
811 block_end_pfn = min(block_end_pfn, end_pfn);
812
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700813 if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone))
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700814 continue;
815
816 pfn = isolate_migratepages_block(cc, pfn, block_end_pfn,
817 ISOLATE_UNEVICTABLE);
818
Hugh Dickinsd6926182016-05-05 16:22:15 -0700819 if (!pfn)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700820 break;
Joonsoo Kim6ea41c02014-10-29 14:50:20 -0700821
822 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX)
823 break;
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700824 }
825 acct_isolated(cc->zone, cc);
826
827 return pfn;
828}
829
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100830#endif /* CONFIG_COMPACTION || CONFIG_CMA */
831#ifdef CONFIG_COMPACTION
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100832/*
833 * Based on information in the current compact_control, find blocks
834 * suitable for isolating free pages from and then isolate them.
835 */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700836static void isolate_freepages(struct compact_control *cc)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100837{
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700838 struct zone *zone = cc->zone;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100839 struct page *page;
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700840 unsigned long block_start_pfn; /* start of current pageblock */
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700841 unsigned long isolate_start_pfn; /* exact pfn we start at */
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700842 unsigned long block_end_pfn; /* end of current pageblock */
843 unsigned long low_pfn; /* lowest pfn scanner is able to scan */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100844 int nr_freepages = cc->nr_freepages;
845 struct list_head *freelist = &cc->freepages;
846
847 /*
848 * Initialise the free scanner. The starting point is where we last
Vlastimil Babka49e068f2014-05-06 12:50:03 -0700849 * successfully isolated from, zone-cached value, or the end of the
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700850 * zone when isolating for the first time. For looping we also need
851 * this pfn aligned down to the pageblock boundary, because we do
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700852 * block_start_pfn -= pageblock_nr_pages in the for loop.
853 * For ending point, take care when isolating in last pageblock of a
854 * a zone which ends in the middle of a pageblock.
Vlastimil Babka49e068f2014-05-06 12:50:03 -0700855 * The low boundary is the end of the pageblock the migration scanner
856 * is using.
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100857 */
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700858 isolate_start_pfn = cc->free_pfn;
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700859 block_start_pfn = cc->free_pfn & ~(pageblock_nr_pages-1);
860 block_end_pfn = min(block_start_pfn + pageblock_nr_pages,
861 zone_end_pfn(zone));
Vlastimil Babka7ed695e02014-01-21 15:51:09 -0800862 low_pfn = ALIGN(cc->migrate_pfn + 1, pageblock_nr_pages);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100863
864 /*
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100865 * Isolate free pages until enough are available to migrate the
866 * pages on cc->migratepages. We stop searching if the migrate
867 * and free page scanners meet or enough free pages are isolated.
868 */
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700869 for (; block_start_pfn >= low_pfn && cc->nr_migratepages > nr_freepages;
870 block_end_pfn = block_start_pfn,
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700871 block_start_pfn -= pageblock_nr_pages,
872 isolate_start_pfn = block_start_pfn) {
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100873 unsigned long isolated;
874
David Rientjesf6ea3ad2013-09-30 13:45:03 -0700875 /*
876 * This can iterate a massively long zone without finding any
877 * suitable migration targets, so periodically check if we need
Vlastimil Babkabe976572014-06-04 16:10:41 -0700878 * to schedule, or even abort async compaction.
David Rientjesf6ea3ad2013-09-30 13:45:03 -0700879 */
Vlastimil Babkabe976572014-06-04 16:10:41 -0700880 if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
881 && compact_should_abort(cc))
882 break;
David Rientjesf6ea3ad2013-09-30 13:45:03 -0700883
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700884 page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
885 zone);
886 if (!page)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100887 continue;
888
889 /* Check the block is suitable for migration */
Linus Torvalds68e3e922012-06-03 20:05:57 -0700890 if (!suitable_migration_target(page))
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100891 continue;
Linus Torvalds68e3e922012-06-03 20:05:57 -0700892
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700893 /* If isolation recently failed, do not retry */
894 if (!isolation_suitable(cc, page))
895 continue;
896
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700897 /* Found a block suitable for isolating free pages from. */
898 isolated = isolate_freepages_block(cc, &isolate_start_pfn,
David Rientjes3c767522016-06-24 14:50:10 -0700899 block_end_pfn, freelist, false);
900 /* If isolation failed early, do not continue needlessly */
901 if (!isolated && isolate_start_pfn < block_end_pfn &&
902 cc->nr_migratepages > cc->nr_freepages)
903 break;
904
Mel Gormanf40d1e42012-10-08 16:32:36 -0700905 nr_freepages += isolated;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100906
907 /*
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700908 * Remember where the free scanner should restart next time,
909 * which is where isolate_freepages_block() left off.
910 * But if it scanned the whole pageblock, isolate_start_pfn
911 * now points at block_end_pfn, which is the start of the next
912 * pageblock.
913 * In that case we will however want to restart at the start
914 * of the previous pageblock.
915 */
916 cc->free_pfn = (isolate_start_pfn < block_end_pfn) ?
917 isolate_start_pfn :
918 block_start_pfn - pageblock_nr_pages;
919
920 /*
Vlastimil Babkae9ade562014-06-04 16:08:34 -0700921 * Set a flag that we successfully isolated in this pageblock.
922 * In the next loop iteration, zone->compact_cached_free_pfn
923 * will not be updated and thus it will effectively contain the
924 * highest pageblock we isolated pages from.
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100925 */
Vlastimil Babkae9ade562014-06-04 16:08:34 -0700926 if (isolated)
Mel Gormanc89511a2012-10-08 16:32:45 -0700927 cc->finished_update_free = true;
Vlastimil Babkabe976572014-06-04 16:10:41 -0700928
929 /*
930 * isolate_freepages_block() might have aborted due to async
931 * compaction being contended
932 */
933 if (cc->contended)
934 break;
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100935 }
936
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100937 /* split_free_page does not map the pages */
938 map_pages(freelist);
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100939
Vlastimil Babka7ed695e02014-01-21 15:51:09 -0800940 /*
941 * If we crossed the migrate scanner, we want to keep it that way
942 * so that compact_finished() may detect this
943 */
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700944 if (block_start_pfn < low_pfn)
Vlastimil Babkae9ade562014-06-04 16:08:34 -0700945 cc->free_pfn = cc->migrate_pfn;
Vlastimil Babkac96b9e52014-06-04 16:07:26 -0700946
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100947 cc->nr_freepages = nr_freepages;
Mel Gorman748446b2010-05-24 14:32:27 -0700948}
949
950/*
951 * This is a migrate-callback that "allocates" freepages by taking pages
952 * from the isolated freelists in the block we are migrating to.
953 */
954static struct page *compaction_alloc(struct page *migratepage,
955 unsigned long data,
956 int **result)
957{
958 struct compact_control *cc = (struct compact_control *)data;
959 struct page *freepage;
960
Vlastimil Babkabe976572014-06-04 16:10:41 -0700961 /*
962 * Isolate free pages if necessary, and if we are not aborting due to
963 * contention.
964 */
Mel Gorman748446b2010-05-24 14:32:27 -0700965 if (list_empty(&cc->freepages)) {
Vlastimil Babkabe976572014-06-04 16:10:41 -0700966 if (!cc->contended)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700967 isolate_freepages(cc);
Mel Gorman748446b2010-05-24 14:32:27 -0700968
969 if (list_empty(&cc->freepages))
970 return NULL;
971 }
972
973 freepage = list_entry(cc->freepages.next, struct page, lru);
974 list_del(&freepage->lru);
975 cc->nr_freepages--;
976
977 return freepage;
978}
979
980/*
David Rientjesd53aea32014-06-04 16:08:26 -0700981 * This is a migrate-callback that "frees" freepages back to the isolated
982 * freelist. All pages on the freelist are from the same zone, so there is no
983 * special handling needed for NUMA.
984 */
985static void compaction_free(struct page *page, unsigned long data)
986{
987 struct compact_control *cc = (struct compact_control *)data;
988
989 list_add(&page->lru, &cc->freepages);
990 cc->nr_freepages++;
991}
992
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100993/* possible outcome of isolate_migratepages */
994typedef enum {
995 ISOLATE_ABORT, /* Abort compaction now */
996 ISOLATE_NONE, /* No pages isolated, continue scanning */
997 ISOLATE_SUCCESS, /* Pages isolated, migrate */
998} isolate_migrate_t;
999
1000/*
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001001 * Isolate all pages that can be migrated from the first suitable block,
1002 * starting at the block pointed to by the migrate scanner pfn within
1003 * compact_control.
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001004 */
1005static isolate_migrate_t isolate_migratepages(struct zone *zone,
1006 struct compact_control *cc)
1007{
1008 unsigned long low_pfn, end_pfn;
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001009 struct page *page;
1010 const isolate_mode_t isolate_mode =
1011 (cc->mode == MIGRATE_ASYNC ? ISOLATE_ASYNC_MIGRATE : 0);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001012
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001013 /*
1014 * Start at where we last stopped, or beginning of the zone as
1015 * initialized by compact_zone()
1016 */
1017 low_pfn = cc->migrate_pfn;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001018
1019 /* Only scan within a pageblock boundary */
Mel Gormana9aacbc2013-02-22 16:32:25 -08001020 end_pfn = ALIGN(low_pfn + 1, pageblock_nr_pages);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001021
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001022 /*
1023 * Iterate over whole pageblocks until we find the first suitable.
1024 * Do not cross the free scanner.
1025 */
1026 for (; end_pfn <= cc->free_pfn;
1027 low_pfn = end_pfn, end_pfn += pageblock_nr_pages) {
1028
1029 /*
1030 * This can potentially iterate a massively long zone with
1031 * many pageblocks unsuitable, so periodically check if we
1032 * need to schedule, or even abort async compaction.
1033 */
1034 if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
1035 && compact_should_abort(cc))
1036 break;
1037
Vlastimil Babka7d49d882014-10-09 15:27:11 -07001038 page = pageblock_pfn_to_page(low_pfn, end_pfn, zone);
1039 if (!page)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001040 continue;
1041
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001042 /* If isolation recently failed, do not retry */
1043 if (!isolation_suitable(cc, page))
1044 continue;
1045
1046 /*
1047 * For async compaction, also only scan in MOVABLE blocks.
1048 * Async compaction is optimistic to see if the minimum amount
1049 * of work satisfies the allocation.
1050 */
1051 if (cc->mode == MIGRATE_ASYNC &&
1052 !migrate_async_suitable(get_pageblock_migratetype(page)))
1053 continue;
1054
1055 /* Perform the isolation */
1056 low_pfn = isolate_migratepages_block(cc, low_pfn, end_pfn,
1057 isolate_mode);
1058
Hugh Dickinsb7c386c2015-02-12 15:00:28 -08001059 if (!low_pfn || cc->contended) {
1060 acct_isolated(zone, cc);
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001061 return ISOLATE_ABORT;
Hugh Dickinsb7c386c2015-02-12 15:00:28 -08001062 }
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001063
1064 /*
1065 * Either we isolated something and proceed with migration. Or
1066 * we failed and compact_zone should decide if we should
1067 * continue or not.
1068 */
1069 break;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001070 }
1071
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001072 acct_isolated(zone, cc);
Vlastimil Babka1d5bfe12014-11-13 15:19:30 -08001073 /*
1074 * Record where migration scanner will be restarted. If we end up in
1075 * the same pageblock as the free scanner, make the scanners fully
1076 * meet so that compact_finished() terminates compaction.
1077 */
1078 cc->migrate_pfn = (end_pfn <= cc->free_pfn) ? low_pfn : cc->free_pfn;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001079
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001080 return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001081}
1082
David Rientjes6d7ce552014-10-09 15:27:27 -07001083static int compact_finished(struct zone *zone, struct compact_control *cc,
1084 const int migratetype)
Mel Gorman748446b2010-05-24 14:32:27 -07001085{
Mel Gorman8fb74b92013-01-11 14:32:16 -08001086 unsigned int order;
Andrea Arcangeli5a03b052011-01-13 15:47:11 -08001087 unsigned long watermark;
Mel Gorman56de7262010-05-24 14:32:30 -07001088
Vlastimil Babkabe976572014-06-04 16:10:41 -07001089 if (cc->contended || fatal_signal_pending(current))
Mel Gorman748446b2010-05-24 14:32:27 -07001090 return COMPACT_PARTIAL;
1091
Mel Gorman753341a2012-10-08 16:32:40 -07001092 /* Compaction run completes if the migrate and free scanner meet */
Mel Gormanbb13ffe2012-10-08 16:32:41 -07001093 if (cc->free_pfn <= cc->migrate_pfn) {
Vlastimil Babka55b7c4c2014-01-21 15:51:11 -08001094 /* Let the next compaction start anew. */
David Rientjes35979ef2014-06-04 16:08:27 -07001095 zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn;
1096 zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn;
Vlastimil Babka55b7c4c2014-01-21 15:51:11 -08001097 zone->compact_cached_free_pfn = zone_end_pfn(zone);
1098
Mel Gorman62997022012-10-08 16:32:47 -07001099 /*
1100 * Mark that the PG_migrate_skip information should be cleared
1101 * by kswapd when it goes to sleep. kswapd does not set the
1102 * flag itself as the decision to be clear should be directly
1103 * based on an allocation request.
1104 */
1105 if (!current_is_kswapd())
1106 zone->compact_blockskip_flush = true;
1107
Mel Gorman748446b2010-05-24 14:32:27 -07001108 return COMPACT_COMPLETE;
Mel Gormanbb13ffe2012-10-08 16:32:41 -07001109 }
Mel Gorman748446b2010-05-24 14:32:27 -07001110
Johannes Weiner82478fb2011-01-20 14:44:21 -08001111 /*
1112 * order == -1 is expected when compacting via
1113 * /proc/sys/vm/compact_memory
1114 */
Mel Gorman56de7262010-05-24 14:32:30 -07001115 if (cc->order == -1)
1116 return COMPACT_CONTINUE;
1117
Michal Hocko3957c772011-06-15 15:08:25 -07001118 /* Compaction run is not finished if the watermark is not met */
1119 watermark = low_wmark_pages(zone);
1120 watermark += (1 << cc->order);
1121
1122 if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
1123 return COMPACT_CONTINUE;
1124
Mel Gorman56de7262010-05-24 14:32:30 -07001125 /* Direct compactor: Is a suitable page free? */
Mel Gorman8fb74b92013-01-11 14:32:16 -08001126 for (order = cc->order; order < MAX_ORDER; order++) {
1127 struct free_area *area = &zone->free_area[order];
Mel Gorman56de7262010-05-24 14:32:30 -07001128
Mel Gorman8fb74b92013-01-11 14:32:16 -08001129 /* Job done if page is free of the right migratetype */
David Rientjes6d7ce552014-10-09 15:27:27 -07001130 if (!list_empty(&area->free_list[migratetype]))
Mel Gorman8fb74b92013-01-11 14:32:16 -08001131 return COMPACT_PARTIAL;
1132
1133 /* Job done if allocation would set block type */
Joonsoo Kim42af81d2015-02-12 14:59:50 -08001134 if (order >= pageblock_order && area->nr_free)
Mel Gorman8fb74b92013-01-11 14:32:16 -08001135 return COMPACT_PARTIAL;
Mel Gorman56de7262010-05-24 14:32:30 -07001136 }
1137
Mel Gorman748446b2010-05-24 14:32:27 -07001138 return COMPACT_CONTINUE;
1139}
1140
Mel Gorman3e7d3442011-01-13 15:45:56 -08001141/*
1142 * compaction_suitable: Is this suitable to run compaction on this zone now?
1143 * Returns
1144 * COMPACT_SKIPPED - If there are too few free pages for compaction
1145 * COMPACT_PARTIAL - If the allocation would succeed without compaction
1146 * COMPACT_CONTINUE - If compaction should run now
1147 */
1148unsigned long compaction_suitable(struct zone *zone, int order)
1149{
1150 int fragindex;
1151 unsigned long watermark;
1152
1153 /*
Michal Hocko3957c772011-06-15 15:08:25 -07001154 * order == -1 is expected when compacting via
1155 * /proc/sys/vm/compact_memory
1156 */
1157 if (order == -1)
1158 return COMPACT_CONTINUE;
1159
1160 /*
Mel Gorman3e7d3442011-01-13 15:45:56 -08001161 * Watermarks for order-0 must be met for compaction. Note the 2UL.
1162 * This is because during migration, copies of pages need to be
1163 * allocated and for a short time, the footprint is higher
1164 */
1165 watermark = low_wmark_pages(zone) + (2UL << order);
1166 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
1167 return COMPACT_SKIPPED;
1168
1169 /*
1170 * fragmentation index determines if allocation failures are due to
1171 * low memory or external fragmentation
1172 *
Shaohua Lia582a732011-06-15 15:08:49 -07001173 * index of -1000 implies allocations might succeed depending on
1174 * watermarks
Mel Gorman3e7d3442011-01-13 15:45:56 -08001175 * index towards 0 implies failure is due to lack of memory
1176 * index towards 1000 implies failure is due to fragmentation
1177 *
1178 * Only compact if a failure would be due to fragmentation.
1179 */
1180 fragindex = fragmentation_index(zone, order);
1181 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
1182 return COMPACT_SKIPPED;
1183
Shaohua Lia582a732011-06-15 15:08:49 -07001184 if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
1185 0, 0))
Mel Gorman3e7d3442011-01-13 15:45:56 -08001186 return COMPACT_PARTIAL;
1187
1188 return COMPACT_CONTINUE;
1189}
1190
Mel Gorman748446b2010-05-24 14:32:27 -07001191static int compact_zone(struct zone *zone, struct compact_control *cc)
1192{
1193 int ret;
Mel Gormanc89511a2012-10-08 16:32:45 -07001194 unsigned long start_pfn = zone->zone_start_pfn;
Cody P Schafer108bcc92013-02-22 16:35:23 -08001195 unsigned long end_pfn = zone_end_pfn(zone);
David Rientjes6d7ce552014-10-09 15:27:27 -07001196 const int migratetype = gfpflags_to_migratetype(cc->gfp_mask);
David Rientjese0b9dae2014-06-04 16:08:28 -07001197 const bool sync = cc->mode != MIGRATE_ASYNC;
Mel Gorman748446b2010-05-24 14:32:27 -07001198
Mel Gorman3e7d3442011-01-13 15:45:56 -08001199 ret = compaction_suitable(zone, cc->order);
1200 switch (ret) {
1201 case COMPACT_PARTIAL:
1202 case COMPACT_SKIPPED:
1203 /* Compaction is likely to fail */
1204 return ret;
1205 case COMPACT_CONTINUE:
1206 /* Fall through to compaction */
1207 ;
1208 }
1209
Mel Gormanc89511a2012-10-08 16:32:45 -07001210 /*
Vlastimil Babkad3132e42014-01-21 15:51:08 -08001211 * Clear pageblock skip if there were failures recently and compaction
1212 * is about to be retried after being deferred. kswapd does not do
1213 * this reset as it'll reset the cached information when going to sleep.
1214 */
1215 if (compaction_restarting(zone, cc->order) && !current_is_kswapd())
1216 __reset_isolation_suitable(zone);
1217
1218 /*
Mel Gormanc89511a2012-10-08 16:32:45 -07001219 * Setup to move all movable pages to the end of the zone. Used cached
1220 * information on where the scanners should start but check that it
1221 * is initialised by ensuring the values are within zone boundaries.
1222 */
David Rientjese0b9dae2014-06-04 16:08:28 -07001223 cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync];
Mel Gormanc89511a2012-10-08 16:32:45 -07001224 cc->free_pfn = zone->compact_cached_free_pfn;
1225 if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) {
1226 cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1);
1227 zone->compact_cached_free_pfn = cc->free_pfn;
1228 }
1229 if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) {
1230 cc->migrate_pfn = start_pfn;
David Rientjes35979ef2014-06-04 16:08:27 -07001231 zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn;
1232 zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn;
Mel Gormanc89511a2012-10-08 16:32:45 -07001233 }
Mel Gorman748446b2010-05-24 14:32:27 -07001234
Mel Gorman0eb927c2014-01-21 15:51:05 -08001235 trace_mm_compaction_begin(start_pfn, cc->migrate_pfn, cc->free_pfn, end_pfn);
1236
Mel Gorman748446b2010-05-24 14:32:27 -07001237 migrate_prep_local();
1238
David Rientjes6d7ce552014-10-09 15:27:27 -07001239 while ((ret = compact_finished(zone, cc, migratetype)) ==
1240 COMPACT_CONTINUE) {
Minchan Kim9d502c12011-03-22 16:30:39 -07001241 int err;
Mel Gorman748446b2010-05-24 14:32:27 -07001242
Mel Gormanf9e35b32011-06-15 15:08:52 -07001243 switch (isolate_migratepages(zone, cc)) {
1244 case ISOLATE_ABORT:
1245 ret = COMPACT_PARTIAL;
Rafael Aquini5733c7d2012-12-11 16:02:47 -08001246 putback_movable_pages(&cc->migratepages);
Shaohua Lie64c5232012-10-08 16:32:27 -07001247 cc->nr_migratepages = 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -07001248 goto out;
1249 case ISOLATE_NONE:
Mel Gorman748446b2010-05-24 14:32:27 -07001250 continue;
Mel Gormanf9e35b32011-06-15 15:08:52 -07001251 case ISOLATE_SUCCESS:
1252 ;
1253 }
Mel Gorman748446b2010-05-24 14:32:27 -07001254
David Rientjesd53aea32014-06-04 16:08:26 -07001255 err = migrate_pages(&cc->migratepages, compaction_alloc,
David Rientjese0b9dae2014-06-04 16:08:28 -07001256 compaction_free, (unsigned long)cc, cc->mode,
Mel Gorman7b2a2d42012-10-19 14:07:31 +01001257 MR_COMPACTION);
Mel Gorman748446b2010-05-24 14:32:27 -07001258
Vlastimil Babkaf8c93012014-06-04 16:08:32 -07001259 trace_mm_compaction_migratepages(cc->nr_migratepages, err,
1260 &cc->migratepages);
Mel Gorman748446b2010-05-24 14:32:27 -07001261
Vlastimil Babkaf8c93012014-06-04 16:08:32 -07001262 /* All pages were either migrated or will be released */
1263 cc->nr_migratepages = 0;
Minchan Kim9d502c12011-03-22 16:30:39 -07001264 if (err) {
Rafael Aquini5733c7d2012-12-11 16:02:47 -08001265 putback_movable_pages(&cc->migratepages);
Vlastimil Babka7ed695e02014-01-21 15:51:09 -08001266 /*
1267 * migrate_pages() may return -ENOMEM when scanners meet
1268 * and we want compact_finished() to detect it
1269 */
1270 if (err == -ENOMEM && cc->free_pfn > cc->migrate_pfn) {
David Rientjes4bf2bba2012-07-11 14:02:13 -07001271 ret = COMPACT_PARTIAL;
1272 goto out;
1273 }
Mel Gorman748446b2010-05-24 14:32:27 -07001274 }
Mel Gorman748446b2010-05-24 14:32:27 -07001275 }
1276
Mel Gormanf9e35b32011-06-15 15:08:52 -07001277out:
Mel Gorman748446b2010-05-24 14:32:27 -07001278 /* Release free pages and check accounting */
1279 cc->nr_freepages -= release_freepages(&cc->freepages);
1280 VM_BUG_ON(cc->nr_freepages != 0);
1281
Mel Gorman0eb927c2014-01-21 15:51:05 -08001282 trace_mm_compaction_end(ret);
1283
Mel Gorman748446b2010-05-24 14:32:27 -07001284 return ret;
1285}
Mel Gorman76ab0f52010-05-24 14:32:28 -07001286
David Rientjese0b9dae2014-06-04 16:08:28 -07001287static unsigned long compact_zone_order(struct zone *zone, int order,
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001288 gfp_t gfp_mask, enum migrate_mode mode, int *contended)
Mel Gorman56de7262010-05-24 14:32:30 -07001289{
Shaohua Lie64c5232012-10-08 16:32:27 -07001290 unsigned long ret;
Mel Gorman56de7262010-05-24 14:32:30 -07001291 struct compact_control cc = {
1292 .nr_freepages = 0,
1293 .nr_migratepages = 0,
1294 .order = order,
David Rientjes6d7ce552014-10-09 15:27:27 -07001295 .gfp_mask = gfp_mask,
Mel Gorman56de7262010-05-24 14:32:30 -07001296 .zone = zone,
David Rientjese0b9dae2014-06-04 16:08:28 -07001297 .mode = mode,
Mel Gorman56de7262010-05-24 14:32:30 -07001298 };
1299 INIT_LIST_HEAD(&cc.freepages);
1300 INIT_LIST_HEAD(&cc.migratepages);
1301
Shaohua Lie64c5232012-10-08 16:32:27 -07001302 ret = compact_zone(zone, &cc);
1303
1304 VM_BUG_ON(!list_empty(&cc.freepages));
1305 VM_BUG_ON(!list_empty(&cc.migratepages));
1306
1307 *contended = cc.contended;
1308 return ret;
Mel Gorman56de7262010-05-24 14:32:30 -07001309}
1310
Mel Gorman5e771902010-05-24 14:32:31 -07001311int sysctl_extfrag_threshold = 500;
1312
Mel Gorman56de7262010-05-24 14:32:30 -07001313/**
1314 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
1315 * @zonelist: The zonelist used for the current allocation
1316 * @order: The order of the current allocation
1317 * @gfp_mask: The GFP mask of the current allocation
1318 * @nodemask: The allowed nodes to allocate from
David Rientjese0b9dae2014-06-04 16:08:28 -07001319 * @mode: The migration mode for async, sync light, or sync migration
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001320 * @contended: Return value that determines if compaction was aborted due to
1321 * need_resched() or lock contention
Vlastimil Babka53853e22014-10-09 15:27:02 -07001322 * @candidate_zone: Return the zone where we think allocation should succeed
Mel Gorman56de7262010-05-24 14:32:30 -07001323 *
1324 * This is the main entry point for direct page compaction.
1325 */
1326unsigned long try_to_compact_pages(struct zonelist *zonelist,
Mel Gorman77f1fe62011-01-13 15:45:57 -08001327 int order, gfp_t gfp_mask, nodemask_t *nodemask,
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001328 enum migrate_mode mode, int *contended,
Vlastimil Babka53853e22014-10-09 15:27:02 -07001329 struct zone **candidate_zone)
Mel Gorman56de7262010-05-24 14:32:30 -07001330{
1331 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
1332 int may_enter_fs = gfp_mask & __GFP_FS;
1333 int may_perform_io = gfp_mask & __GFP_IO;
Mel Gorman56de7262010-05-24 14:32:30 -07001334 struct zoneref *z;
1335 struct zone *zone;
Vlastimil Babka53853e22014-10-09 15:27:02 -07001336 int rc = COMPACT_DEFERRED;
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001337 int alloc_flags = 0;
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001338 int all_zones_contended = COMPACT_CONTENDED_LOCK; /* init for &= op */
1339
1340 *contended = COMPACT_CONTENDED_NONE;
Mel Gorman56de7262010-05-24 14:32:30 -07001341
Mel Gorman4ffb6332012-10-08 16:29:09 -07001342 /* Check if the GFP flags allow compaction */
Andrea Arcangelic5a73c32011-01-13 15:47:11 -08001343 if (!order || !may_enter_fs || !may_perform_io)
Vlastimil Babka53853e22014-10-09 15:27:02 -07001344 return COMPACT_SKIPPED;
Mel Gorman56de7262010-05-24 14:32:30 -07001345
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001346#ifdef CONFIG_CMA
David Rientjes43e7a342014-10-09 15:27:25 -07001347 if (gfpflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001348 alloc_flags |= ALLOC_CMA;
1349#endif
Mel Gorman56de7262010-05-24 14:32:30 -07001350 /* Compact each zone in the list */
1351 for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
1352 nodemask) {
Mel Gorman56de7262010-05-24 14:32:30 -07001353 int status;
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001354 int zone_contended;
Mel Gorman56de7262010-05-24 14:32:30 -07001355
Vlastimil Babka53853e22014-10-09 15:27:02 -07001356 if (compaction_deferred(zone, order))
1357 continue;
1358
David Rientjese0b9dae2014-06-04 16:08:28 -07001359 status = compact_zone_order(zone, order, gfp_mask, mode,
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001360 &zone_contended);
Mel Gorman56de7262010-05-24 14:32:30 -07001361 rc = max(status, rc);
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001362 /*
1363 * It takes at least one zone that wasn't lock contended
1364 * to clear all_zones_contended.
1365 */
1366 all_zones_contended &= zone_contended;
Mel Gorman56de7262010-05-24 14:32:30 -07001367
Mel Gorman3e7d3442011-01-13 15:45:56 -08001368 /* If a normal allocation would succeed, stop compacting */
Bartlomiej Zolnierkiewiczd95ea5d2012-10-08 16:32:05 -07001369 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
Vlastimil Babka53853e22014-10-09 15:27:02 -07001370 alloc_flags)) {
1371 *candidate_zone = zone;
1372 /*
1373 * We think the allocation will succeed in this zone,
1374 * but it is not certain, hence the false. The caller
1375 * will repeat this with true if allocation indeed
1376 * succeeds in this zone.
1377 */
1378 compaction_defer_reset(zone, order, false);
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001379 /*
1380 * It is possible that async compaction aborted due to
1381 * need_resched() and the watermarks were ok thanks to
1382 * somebody else freeing memory. The allocation can
1383 * however still fail so we better signal the
1384 * need_resched() contention anyway (this will not
1385 * prevent the allocation attempt).
1386 */
1387 if (zone_contended == COMPACT_CONTENDED_SCHED)
1388 *contended = COMPACT_CONTENDED_SCHED;
1389
1390 goto break_loop;
1391 }
1392
1393 if (mode != MIGRATE_ASYNC) {
Vlastimil Babka53853e22014-10-09 15:27:02 -07001394 /*
1395 * We think that allocation won't succeed in this zone
1396 * so we defer compaction there. If it ends up
1397 * succeeding after all, it will be reset.
1398 */
1399 defer_compaction(zone, order);
1400 }
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001401
1402 /*
1403 * We might have stopped compacting due to need_resched() in
1404 * async compaction, or due to a fatal signal detected. In that
1405 * case do not try further zones and signal need_resched()
1406 * contention.
1407 */
1408 if ((zone_contended == COMPACT_CONTENDED_SCHED)
1409 || fatal_signal_pending(current)) {
1410 *contended = COMPACT_CONTENDED_SCHED;
1411 goto break_loop;
1412 }
1413
1414 continue;
1415break_loop:
1416 /*
1417 * We might not have tried all the zones, so be conservative
1418 * and assume they are not all lock contended.
1419 */
1420 all_zones_contended = 0;
1421 break;
Mel Gorman56de7262010-05-24 14:32:30 -07001422 }
1423
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001424 /*
1425 * If at least one zone wasn't deferred or skipped, we report if all
1426 * zones that were tried were lock contended.
1427 */
1428 if (rc > COMPACT_SKIPPED && all_zones_contended)
1429 *contended = COMPACT_CONTENDED_LOCK;
1430
Mel Gorman56de7262010-05-24 14:32:30 -07001431 return rc;
1432}
1433
1434
Mel Gorman76ab0f52010-05-24 14:32:28 -07001435/* Compact all zones within a node */
Andrew Morton7103f162013-02-22 16:32:33 -08001436static void __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001437{
1438 int zoneid;
Mel Gorman76ab0f52010-05-24 14:32:28 -07001439 struct zone *zone;
1440
Mel Gorman76ab0f52010-05-24 14:32:28 -07001441 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
Mel Gorman76ab0f52010-05-24 14:32:28 -07001442
1443 zone = &pgdat->node_zones[zoneid];
1444 if (!populated_zone(zone))
1445 continue;
1446
Rik van Riel7be62de2012-03-21 16:33:52 -07001447 cc->nr_freepages = 0;
1448 cc->nr_migratepages = 0;
1449 cc->zone = zone;
1450 INIT_LIST_HEAD(&cc->freepages);
1451 INIT_LIST_HEAD(&cc->migratepages);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001452
Dan Carpenteraad6ec32012-03-21 16:33:54 -07001453 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
Rik van Riel7be62de2012-03-21 16:33:52 -07001454 compact_zone(zone, cc);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001455
Rik van Rielaff62242012-03-21 16:33:52 -07001456 if (cc->order > 0) {
Vlastimil Babkade6c60a2014-01-21 15:51:07 -08001457 if (zone_watermark_ok(zone, cc->order,
1458 low_wmark_pages(zone), 0, 0))
1459 compaction_defer_reset(zone, cc->order, false);
Rik van Rielaff62242012-03-21 16:33:52 -07001460 }
1461
Rik van Riel7be62de2012-03-21 16:33:52 -07001462 VM_BUG_ON(!list_empty(&cc->freepages));
1463 VM_BUG_ON(!list_empty(&cc->migratepages));
Mel Gorman76ab0f52010-05-24 14:32:28 -07001464 }
Mel Gorman76ab0f52010-05-24 14:32:28 -07001465}
1466
Andrew Morton7103f162013-02-22 16:32:33 -08001467void compact_pgdat(pg_data_t *pgdat, int order)
Rik van Riel7be62de2012-03-21 16:33:52 -07001468{
1469 struct compact_control cc = {
1470 .order = order,
David Rientjese0b9dae2014-06-04 16:08:28 -07001471 .mode = MIGRATE_ASYNC,
Rik van Riel7be62de2012-03-21 16:33:52 -07001472 };
1473
Mel Gorman3a7200a2013-09-11 14:22:19 -07001474 if (!order)
1475 return;
1476
Andrew Morton7103f162013-02-22 16:32:33 -08001477 __compact_pgdat(pgdat, &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -07001478}
1479
Andrew Morton7103f162013-02-22 16:32:33 -08001480static void compact_node(int nid)
Rik van Riel7be62de2012-03-21 16:33:52 -07001481{
Rik van Riel7be62de2012-03-21 16:33:52 -07001482 struct compact_control cc = {
1483 .order = -1,
David Rientjese0b9dae2014-06-04 16:08:28 -07001484 .mode = MIGRATE_SYNC,
David Rientjes91ca9182014-04-03 14:47:23 -07001485 .ignore_skip_hint = true,
Rik van Riel7be62de2012-03-21 16:33:52 -07001486 };
1487
Andrew Morton7103f162013-02-22 16:32:33 -08001488 __compact_pgdat(NODE_DATA(nid), &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -07001489}
1490
Mel Gorman76ab0f52010-05-24 14:32:28 -07001491/* Compact all nodes in the system */
Jason Liu7964c062013-01-11 14:31:47 -08001492static void compact_nodes(void)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001493{
1494 int nid;
1495
Hugh Dickins8575ec22012-03-21 16:33:53 -07001496 /* Flush pending updates to the LRU lists */
1497 lru_add_drain_all();
1498
Mel Gorman76ab0f52010-05-24 14:32:28 -07001499 for_each_online_node(nid)
1500 compact_node(nid);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001501}
1502
1503/* The written value is actually unused, all memory is compacted */
1504int sysctl_compact_memory;
1505
1506/* This is the entry point for compacting all nodes via /proc/sys/vm */
1507int sysctl_compaction_handler(struct ctl_table *table, int write,
1508 void __user *buffer, size_t *length, loff_t *ppos)
1509{
1510 if (write)
Jason Liu7964c062013-01-11 14:31:47 -08001511 compact_nodes();
Mel Gorman76ab0f52010-05-24 14:32:28 -07001512
1513 return 0;
1514}
Mel Gormaned4a6d72010-05-24 14:32:29 -07001515
Mel Gorman5e771902010-05-24 14:32:31 -07001516int sysctl_extfrag_handler(struct ctl_table *table, int write,
1517 void __user *buffer, size_t *length, loff_t *ppos)
1518{
1519 proc_dointvec_minmax(table, write, buffer, length, ppos);
1520
1521 return 0;
1522}
1523
Mel Gormaned4a6d72010-05-24 14:32:29 -07001524#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
Rashika Kheria74e77fb2014-04-03 14:48:01 -07001525static ssize_t sysfs_compact_node(struct device *dev,
Kay Sievers10fbcf42011-12-21 14:48:43 -08001526 struct device_attribute *attr,
Mel Gormaned4a6d72010-05-24 14:32:29 -07001527 const char *buf, size_t count)
1528{
Hugh Dickins8575ec22012-03-21 16:33:53 -07001529 int nid = dev->id;
1530
1531 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
1532 /* Flush pending updates to the LRU lists */
1533 lru_add_drain_all();
1534
1535 compact_node(nid);
1536 }
Mel Gormaned4a6d72010-05-24 14:32:29 -07001537
1538 return count;
1539}
Kay Sievers10fbcf42011-12-21 14:48:43 -08001540static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001541
1542int compaction_register_node(struct node *node)
1543{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001544 return device_create_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001545}
1546
1547void compaction_unregister_node(struct node *node)
1548{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001549 return device_remove_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001550}
1551#endif /* CONFIG_SYSFS && CONFIG_NUMA */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001552
1553#endif /* CONFIG_COMPACTION */