blob: 4ae1294068a8652df2a365aefb15cb748d7d40a6 [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 */
Vlastimil Babka698b1b32016-03-17 14:18:08 -070010#include <linux/cpu.h>
Mel Gorman748446b2010-05-24 14:32:27 -070011#include <linux/swap.h>
12#include <linux/migrate.h>
13#include <linux/compaction.h>
14#include <linux/mm_inline.h>
15#include <linux/backing-dev.h>
Mel Gorman76ab0f52010-05-24 14:32:28 -070016#include <linux/sysctl.h>
Mel Gormaned4a6d72010-05-24 14:32:29 -070017#include <linux/sysfs.h>
Minchan Kim194159f2013-02-22 16:33:58 -080018#include <linux/page-isolation.h>
Andrey Ryabininb8c73fc2015-02-13 14:39:28 -080019#include <linux/kasan.h>
Vlastimil Babka698b1b32016-03-17 14:18:08 -070020#include <linux/kthread.h>
21#include <linux/freezer.h>
Joonsoo Kim83358ec2016-07-26 15:23:43 -070022#include <linux/page_owner.h>
Mel Gorman748446b2010-05-24 14:32:27 -070023#include "internal.h"
24
Minchan Kim010fc292012-12-20 15:05:06 -080025#ifdef CONFIG_COMPACTION
26static inline void count_compact_event(enum vm_event_item item)
27{
28 count_vm_event(item);
29}
30
31static inline void count_compact_events(enum vm_event_item item, long delta)
32{
33 count_vm_events(item, delta);
34}
35#else
36#define count_compact_event(item) do { } while (0)
37#define count_compact_events(item, delta) do { } while (0)
38#endif
39
Michal Nazarewiczff9543f2011-12-29 13:09:50 +010040#if defined CONFIG_COMPACTION || defined CONFIG_CMA
41
Mel Gormanb7aba692011-01-13 15:45:54 -080042#define CREATE_TRACE_POINTS
43#include <trace/events/compaction.h>
44
Vlastimil Babka06b66402016-05-19 17:11:48 -070045#define block_start_pfn(pfn, order) round_down(pfn, 1UL << (order))
46#define block_end_pfn(pfn, order) ALIGN((pfn) + 1, 1UL << (order))
47#define pageblock_start_pfn(pfn) block_start_pfn(pfn, pageblock_order)
48#define pageblock_end_pfn(pfn) block_end_pfn(pfn, pageblock_order)
49
Mel Gorman748446b2010-05-24 14:32:27 -070050static unsigned long release_freepages(struct list_head *freelist)
51{
52 struct page *page, *next;
Vlastimil Babka6bace092014-12-10 15:43:31 -080053 unsigned long high_pfn = 0;
Mel Gorman748446b2010-05-24 14:32:27 -070054
55 list_for_each_entry_safe(page, next, freelist, lru) {
Vlastimil Babka6bace092014-12-10 15:43:31 -080056 unsigned long pfn = page_to_pfn(page);
Mel Gorman748446b2010-05-24 14:32:27 -070057 list_del(&page->lru);
58 __free_page(page);
Vlastimil Babka6bace092014-12-10 15:43:31 -080059 if (pfn > high_pfn)
60 high_pfn = pfn;
Mel Gorman748446b2010-05-24 14:32:27 -070061 }
62
Vlastimil Babka6bace092014-12-10 15:43:31 -080063 return high_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -070064}
65
Michal Nazarewiczff9543f2011-12-29 13:09:50 +010066static void map_pages(struct list_head *list)
67{
Joonsoo Kim66c64222016-07-26 15:23:40 -070068 unsigned int i, order, nr_pages;
69 struct page *page, *next;
70 LIST_HEAD(tmp_list);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +010071
Joonsoo Kim66c64222016-07-26 15:23:40 -070072 list_for_each_entry_safe(page, next, list, lru) {
73 list_del(&page->lru);
74
75 order = page_private(page);
76 nr_pages = 1 << order;
77 set_page_private(page, 0);
78 set_page_refcounted(page);
79
80 arch_alloc_page(page, order);
81 kernel_map_pages(page, nr_pages, 1);
82 kasan_alloc_pages(page, order);
Joonsoo Kim83358ec2016-07-26 15:23:43 -070083
84 set_page_owner(page, order, __GFP_MOVABLE);
Joonsoo Kim66c64222016-07-26 15:23:40 -070085 if (order)
86 split_page(page, order);
87
88 for (i = 0; i < nr_pages; i++) {
89 list_add(&page->lru, &tmp_list);
90 page++;
91 }
Michal Nazarewiczff9543f2011-12-29 13:09:50 +010092 }
Joonsoo Kim66c64222016-07-26 15:23:40 -070093
94 list_splice(&tmp_list, list);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +010095}
96
Michal Nazarewicz47118af2011-12-29 13:09:50 +010097static inline bool migrate_async_suitable(int migratetype)
98{
99 return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
100}
101
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700102#ifdef CONFIG_COMPACTION
Joonsoo Kim24e27162015-02-11 15:27:09 -0800103
Minchan Kimbda807d2016-07-26 15:23:05 -0700104int PageMovable(struct page *page)
105{
106 struct address_space *mapping;
107
108 VM_BUG_ON_PAGE(!PageLocked(page), page);
109 if (!__PageMovable(page))
110 return 0;
111
112 mapping = page_mapping(page);
113 if (mapping && mapping->a_ops && mapping->a_ops->isolate_page)
114 return 1;
115
116 return 0;
117}
118EXPORT_SYMBOL(PageMovable);
119
120void __SetPageMovable(struct page *page, struct address_space *mapping)
121{
122 VM_BUG_ON_PAGE(!PageLocked(page), page);
123 VM_BUG_ON_PAGE((unsigned long)mapping & PAGE_MAPPING_MOVABLE, page);
124 page->mapping = (void *)((unsigned long)mapping | PAGE_MAPPING_MOVABLE);
125}
126EXPORT_SYMBOL(__SetPageMovable);
127
128void __ClearPageMovable(struct page *page)
129{
130 VM_BUG_ON_PAGE(!PageLocked(page), page);
131 VM_BUG_ON_PAGE(!PageMovable(page), page);
132 /*
133 * Clear registered address_space val with keeping PAGE_MAPPING_MOVABLE
134 * flag so that VM can catch up released page by driver after isolation.
135 * With it, VM migration doesn't try to put it back.
136 */
137 page->mapping = (void *)((unsigned long)page->mapping &
138 PAGE_MAPPING_MOVABLE);
139}
140EXPORT_SYMBOL(__ClearPageMovable);
141
Joonsoo Kim24e27162015-02-11 15:27:09 -0800142/* Do not skip compaction more than 64 times */
143#define COMPACT_MAX_DEFER_SHIFT 6
144
145/*
146 * Compaction is deferred when compaction fails to result in a page
147 * allocation success. 1 << compact_defer_limit compactions are skipped up
148 * to a limit of 1 << COMPACT_MAX_DEFER_SHIFT
149 */
150void defer_compaction(struct zone *zone, int order)
151{
152 zone->compact_considered = 0;
153 zone->compact_defer_shift++;
154
155 if (order < zone->compact_order_failed)
156 zone->compact_order_failed = order;
157
158 if (zone->compact_defer_shift > COMPACT_MAX_DEFER_SHIFT)
159 zone->compact_defer_shift = COMPACT_MAX_DEFER_SHIFT;
160
161 trace_mm_compaction_defer_compaction(zone, order);
162}
163
164/* Returns true if compaction should be skipped this time */
165bool compaction_deferred(struct zone *zone, int order)
166{
167 unsigned long defer_limit = 1UL << zone->compact_defer_shift;
168
169 if (order < zone->compact_order_failed)
170 return false;
171
172 /* Avoid possible overflow */
173 if (++zone->compact_considered > defer_limit)
174 zone->compact_considered = defer_limit;
175
176 if (zone->compact_considered >= defer_limit)
177 return false;
178
179 trace_mm_compaction_deferred(zone, order);
180
181 return true;
182}
183
184/*
185 * Update defer tracking counters after successful compaction of given order,
186 * which means an allocation either succeeded (alloc_success == true) or is
187 * expected to succeed.
188 */
189void compaction_defer_reset(struct zone *zone, int order,
190 bool alloc_success)
191{
192 if (alloc_success) {
193 zone->compact_considered = 0;
194 zone->compact_defer_shift = 0;
195 }
196 if (order >= zone->compact_order_failed)
197 zone->compact_order_failed = order + 1;
198
199 trace_mm_compaction_defer_reset(zone, order);
200}
201
202/* Returns true if restarting compaction after many failures */
203bool compaction_restarting(struct zone *zone, int order)
204{
205 if (order < zone->compact_order_failed)
206 return false;
207
208 return zone->compact_defer_shift == COMPACT_MAX_DEFER_SHIFT &&
209 zone->compact_considered >= 1UL << zone->compact_defer_shift;
210}
211
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700212/* Returns true if the pageblock should be scanned for pages to isolate. */
213static inline bool isolation_suitable(struct compact_control *cc,
214 struct page *page)
215{
216 if (cc->ignore_skip_hint)
217 return true;
218
219 return !get_pageblock_skip(page);
220}
221
Vlastimil Babka02333642015-09-08 15:02:42 -0700222static void reset_cached_positions(struct zone *zone)
223{
224 zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn;
225 zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn;
Joonsoo Kim623446e2016-03-15 14:57:45 -0700226 zone->compact_cached_free_pfn =
Vlastimil Babka06b66402016-05-19 17:11:48 -0700227 pageblock_start_pfn(zone_end_pfn(zone) - 1);
Vlastimil Babka02333642015-09-08 15:02:42 -0700228}
229
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700230/*
231 * This function is called to clear all cached information on pageblocks that
232 * should be skipped for page isolation when the migrate and free page scanner
233 * meet.
234 */
Mel Gorman62997022012-10-08 16:32:47 -0700235static void __reset_isolation_suitable(struct zone *zone)
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700236{
237 unsigned long start_pfn = zone->zone_start_pfn;
Cody P Schafer108bcc92013-02-22 16:35:23 -0800238 unsigned long end_pfn = zone_end_pfn(zone);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700239 unsigned long pfn;
240
Mel Gorman62997022012-10-08 16:32:47 -0700241 zone->compact_blockskip_flush = false;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700242
243 /* Walk the zone and mark every pageblock as suitable for isolation */
244 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
245 struct page *page;
246
247 cond_resched();
248
249 if (!pfn_valid(pfn))
250 continue;
251
252 page = pfn_to_page(pfn);
253 if (zone != page_zone(page))
254 continue;
255
256 clear_pageblock_skip(page);
257 }
Vlastimil Babka02333642015-09-08 15:02:42 -0700258
259 reset_cached_positions(zone);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700260}
261
Mel Gorman62997022012-10-08 16:32:47 -0700262void reset_isolation_suitable(pg_data_t *pgdat)
263{
264 int zoneid;
265
266 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
267 struct zone *zone = &pgdat->node_zones[zoneid];
268 if (!populated_zone(zone))
269 continue;
270
271 /* Only flush if a full compaction finished recently */
272 if (zone->compact_blockskip_flush)
273 __reset_isolation_suitable(zone);
274 }
275}
276
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700277/*
278 * If no pages were isolated then mark this pageblock to be skipped in the
Mel Gorman62997022012-10-08 16:32:47 -0700279 * future. The information is later cleared by __reset_isolation_suitable().
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700280 */
Mel Gormanc89511a2012-10-08 16:32:45 -0700281static void update_pageblock_skip(struct compact_control *cc,
282 struct page *page, unsigned long nr_isolated,
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700283 bool migrate_scanner)
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700284{
Mel Gormanc89511a2012-10-08 16:32:45 -0700285 struct zone *zone = cc->zone;
David Rientjes35979ef2014-06-04 16:08:27 -0700286 unsigned long pfn;
Joonsoo Kim6815bf32013-12-18 17:08:52 -0800287
288 if (cc->ignore_skip_hint)
289 return;
290
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700291 if (!page)
292 return;
293
David Rientjes35979ef2014-06-04 16:08:27 -0700294 if (nr_isolated)
295 return;
296
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700297 set_pageblock_skip(page);
Mel Gormanc89511a2012-10-08 16:32:45 -0700298
David Rientjes35979ef2014-06-04 16:08:27 -0700299 pfn = page_to_pfn(page);
300
301 /* Update where async and sync compaction should restart */
302 if (migrate_scanner) {
David Rientjes35979ef2014-06-04 16:08:27 -0700303 if (pfn > zone->compact_cached_migrate_pfn[0])
304 zone->compact_cached_migrate_pfn[0] = pfn;
David Rientjese0b9dae2014-06-04 16:08:28 -0700305 if (cc->mode != MIGRATE_ASYNC &&
306 pfn > zone->compact_cached_migrate_pfn[1])
David Rientjes35979ef2014-06-04 16:08:27 -0700307 zone->compact_cached_migrate_pfn[1] = pfn;
308 } else {
David Rientjes35979ef2014-06-04 16:08:27 -0700309 if (pfn < zone->compact_cached_free_pfn)
310 zone->compact_cached_free_pfn = pfn;
Mel Gormanc89511a2012-10-08 16:32:45 -0700311 }
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700312}
313#else
314static inline bool isolation_suitable(struct compact_control *cc,
315 struct page *page)
316{
317 return true;
318}
319
Mel Gormanc89511a2012-10-08 16:32:45 -0700320static void update_pageblock_skip(struct compact_control *cc,
321 struct page *page, unsigned long nr_isolated,
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700322 bool migrate_scanner)
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700323{
324}
325#endif /* CONFIG_COMPACTION */
326
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700327/*
328 * Compaction requires the taking of some coarse locks that are potentially
329 * very heavily contended. For async compaction, back out if the lock cannot
330 * be taken immediately. For sync compaction, spin on the lock if needed.
331 *
332 * Returns true if the lock is held
333 * Returns false if the lock is not held and compaction should abort
334 */
335static bool compact_trylock_irqsave(spinlock_t *lock, unsigned long *flags,
336 struct compact_control *cc)
Mel Gorman2a1402a2012-10-08 16:32:33 -0700337{
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700338 if (cc->mode == MIGRATE_ASYNC) {
339 if (!spin_trylock_irqsave(lock, *flags)) {
340 cc->contended = COMPACT_CONTENDED_LOCK;
341 return false;
342 }
343 } else {
344 spin_lock_irqsave(lock, *flags);
345 }
Vlastimil Babka1f9efde2014-10-09 15:27:14 -0700346
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700347 return true;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700348}
349
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100350/*
Mel Gormanc67fe372012-08-21 16:16:17 -0700351 * Compaction requires the taking of some coarse locks that are potentially
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700352 * very heavily contended. The lock should be periodically unlocked to avoid
353 * having disabled IRQs for a long time, even when there is nobody waiting on
354 * the lock. It might also be that allowing the IRQs will result in
355 * need_resched() becoming true. If scheduling is needed, async compaction
356 * aborts. Sync compaction schedules.
357 * Either compaction type will also abort if a fatal signal is pending.
358 * In either case if the lock was locked, it is dropped and not regained.
Mel Gormanc67fe372012-08-21 16:16:17 -0700359 *
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700360 * Returns true if compaction should abort due to fatal signal pending, or
361 * async compaction due to need_resched()
362 * Returns false when compaction can continue (sync compaction might have
363 * scheduled)
Mel Gormanc67fe372012-08-21 16:16:17 -0700364 */
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700365static bool compact_unlock_should_abort(spinlock_t *lock,
366 unsigned long flags, bool *locked, struct compact_control *cc)
Mel Gormanc67fe372012-08-21 16:16:17 -0700367{
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700368 if (*locked) {
369 spin_unlock_irqrestore(lock, flags);
370 *locked = false;
371 }
Vlastimil Babka1f9efde2014-10-09 15:27:14 -0700372
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700373 if (fatal_signal_pending(current)) {
374 cc->contended = COMPACT_CONTENDED_SCHED;
375 return true;
376 }
Mel Gormanc67fe372012-08-21 16:16:17 -0700377
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700378 if (need_resched()) {
David Rientjese0b9dae2014-06-04 16:08:28 -0700379 if (cc->mode == MIGRATE_ASYNC) {
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700380 cc->contended = COMPACT_CONTENDED_SCHED;
381 return true;
Mel Gormanc67fe372012-08-21 16:16:17 -0700382 }
Mel Gormanc67fe372012-08-21 16:16:17 -0700383 cond_resched();
Mel Gormanc67fe372012-08-21 16:16:17 -0700384 }
385
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700386 return false;
Mel Gormanc67fe372012-08-21 16:16:17 -0700387}
388
Vlastimil Babkabe976572014-06-04 16:10:41 -0700389/*
390 * Aside from avoiding lock contention, compaction also periodically checks
391 * need_resched() and either schedules in sync compaction or aborts async
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700392 * compaction. This is similar to what compact_unlock_should_abort() does, but
Vlastimil Babkabe976572014-06-04 16:10:41 -0700393 * is used where no lock is concerned.
394 *
395 * Returns false when no scheduling was needed, or sync compaction scheduled.
396 * Returns true when async compaction should abort.
397 */
398static inline bool compact_should_abort(struct compact_control *cc)
399{
400 /* async compaction aborts if contended */
401 if (need_resched()) {
402 if (cc->mode == MIGRATE_ASYNC) {
Vlastimil Babka1f9efde2014-10-09 15:27:14 -0700403 cc->contended = COMPACT_CONTENDED_SCHED;
Vlastimil Babkabe976572014-06-04 16:10:41 -0700404 return true;
405 }
406
407 cond_resched();
408 }
409
410 return false;
411}
412
Mel Gormanc67fe372012-08-21 16:16:17 -0700413/*
Jerome Marchand9e4be472013-11-12 15:07:12 -0800414 * Isolate free pages onto a private freelist. If @strict is true, will abort
415 * returning 0 on any invalid PFNs or non-free pages inside of the pageblock
416 * (even though it may still end up isolating some pages).
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100417 */
Mel Gormanf40d1e42012-10-08 16:32:36 -0700418static unsigned long isolate_freepages_block(struct compact_control *cc,
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700419 unsigned long *start_pfn,
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100420 unsigned long end_pfn,
421 struct list_head *freelist,
422 bool strict)
Mel Gorman748446b2010-05-24 14:32:27 -0700423{
Mel Gormanb7aba692011-01-13 15:45:54 -0800424 int nr_scanned = 0, total_isolated = 0;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700425 struct page *cursor, *valid_page = NULL;
Xiubo Lib8b2d822014-10-09 15:28:21 -0700426 unsigned long flags = 0;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700427 bool locked = false;
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700428 unsigned long blockpfn = *start_pfn;
Joonsoo Kim66c64222016-07-26 15:23:40 -0700429 unsigned int order;
Mel Gorman748446b2010-05-24 14:32:27 -0700430
Mel Gorman748446b2010-05-24 14:32:27 -0700431 cursor = pfn_to_page(blockpfn);
432
Mel Gormanf40d1e42012-10-08 16:32:36 -0700433 /* Isolate free pages. */
Mel Gorman748446b2010-05-24 14:32:27 -0700434 for (; blockpfn < end_pfn; blockpfn++, cursor++) {
Joonsoo Kim66c64222016-07-26 15:23:40 -0700435 int isolated;
Mel Gorman748446b2010-05-24 14:32:27 -0700436 struct page *page = cursor;
437
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700438 /*
439 * Periodically drop the lock (if held) regardless of its
440 * contention, to give chance to IRQs. Abort if fatal signal
441 * pending or async compaction detects need_resched()
442 */
443 if (!(blockpfn % SWAP_CLUSTER_MAX)
444 && compact_unlock_should_abort(&cc->zone->lock, flags,
445 &locked, cc))
446 break;
447
Mel Gormanb7aba692011-01-13 15:45:54 -0800448 nr_scanned++;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700449 if (!pfn_valid_within(blockpfn))
Laura Abbott2af120b2014-03-10 15:49:44 -0700450 goto isolate_fail;
451
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700452 if (!valid_page)
453 valid_page = page;
Vlastimil Babka9fcd6d22015-09-08 15:02:49 -0700454
455 /*
456 * For compound pages such as THP and hugetlbfs, we can save
457 * potentially a lot of iterations if we skip them at once.
458 * The check is racy, but we can consider only valid values
459 * and the only danger is skipping too much.
460 */
461 if (PageCompound(page)) {
462 unsigned int comp_order = compound_order(page);
463
464 if (likely(comp_order < MAX_ORDER)) {
465 blockpfn += (1UL << comp_order) - 1;
466 cursor += (1UL << comp_order) - 1;
467 }
468
469 goto isolate_fail;
470 }
471
Mel Gormanf40d1e42012-10-08 16:32:36 -0700472 if (!PageBuddy(page))
Laura Abbott2af120b2014-03-10 15:49:44 -0700473 goto isolate_fail;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700474
475 /*
Vlastimil Babka69b71892014-10-09 15:27:18 -0700476 * If we already hold the lock, we can skip some rechecking.
477 * Note that if we hold the lock now, checked_pageblock was
478 * already set in some previous iteration (or strict is true),
479 * so it is correct to skip the suitable migration target
480 * recheck as well.
Mel Gormanf40d1e42012-10-08 16:32:36 -0700481 */
Vlastimil Babka69b71892014-10-09 15:27:18 -0700482 if (!locked) {
483 /*
484 * The zone lock must be held to isolate freepages.
485 * Unfortunately this is a very coarse lock and can be
486 * heavily contended if there are parallel allocations
487 * or parallel compactions. For async compaction do not
488 * spin on the lock and we acquire the lock as late as
489 * possible.
490 */
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700491 locked = compact_trylock_irqsave(&cc->zone->lock,
492 &flags, cc);
Vlastimil Babka69b71892014-10-09 15:27:18 -0700493 if (!locked)
494 break;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700495
Vlastimil Babka69b71892014-10-09 15:27:18 -0700496 /* Recheck this is a buddy page under lock */
497 if (!PageBuddy(page))
498 goto isolate_fail;
499 }
Mel Gorman748446b2010-05-24 14:32:27 -0700500
Joonsoo Kim66c64222016-07-26 15:23:40 -0700501 /* Found a free page, will break it into order-0 pages */
502 order = page_order(page);
503 isolated = __isolate_free_page(page, order);
David Rientjesa4f04f22016-06-24 14:50:10 -0700504 if (!isolated)
505 break;
Joonsoo Kim66c64222016-07-26 15:23:40 -0700506 set_page_private(page, order);
David Rientjesa4f04f22016-06-24 14:50:10 -0700507
Mel Gorman748446b2010-05-24 14:32:27 -0700508 total_isolated += isolated;
David Rientjesa4f04f22016-06-24 14:50:10 -0700509 cc->nr_freepages += isolated;
Joonsoo Kim66c64222016-07-26 15:23:40 -0700510 list_add_tail(&page->lru, freelist);
511
David Rientjesa4f04f22016-06-24 14:50:10 -0700512 if (!strict && cc->nr_migratepages <= cc->nr_freepages) {
513 blockpfn += isolated;
514 break;
Mel Gorman748446b2010-05-24 14:32:27 -0700515 }
David Rientjesa4f04f22016-06-24 14:50:10 -0700516 /* Advance to the end of split page */
517 blockpfn += isolated - 1;
518 cursor += isolated - 1;
519 continue;
Laura Abbott2af120b2014-03-10 15:49:44 -0700520
521isolate_fail:
522 if (strict)
523 break;
524 else
525 continue;
526
Mel Gorman748446b2010-05-24 14:32:27 -0700527 }
528
David Rientjesa4f04f22016-06-24 14:50:10 -0700529 if (locked)
530 spin_unlock_irqrestore(&cc->zone->lock, flags);
531
Vlastimil Babka9fcd6d22015-09-08 15:02:49 -0700532 /*
533 * There is a tiny chance that we have read bogus compound_order(),
534 * so be careful to not go outside of the pageblock.
535 */
536 if (unlikely(blockpfn > end_pfn))
537 blockpfn = end_pfn;
538
Joonsoo Kime34d85f2015-02-11 15:27:04 -0800539 trace_mm_compaction_isolate_freepages(*start_pfn, blockpfn,
540 nr_scanned, total_isolated);
541
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700542 /* Record how far we have got within the block */
543 *start_pfn = blockpfn;
544
Mel Gormanf40d1e42012-10-08 16:32:36 -0700545 /*
546 * If strict isolation is requested by CMA then check that all the
547 * pages requested were isolated. If there were any failures, 0 is
548 * returned and CMA will fail.
549 */
Laura Abbott2af120b2014-03-10 15:49:44 -0700550 if (strict && blockpfn < end_pfn)
Mel Gormanf40d1e42012-10-08 16:32:36 -0700551 total_isolated = 0;
552
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700553 /* Update the pageblock-skip if the whole pageblock was scanned */
554 if (blockpfn == end_pfn)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700555 update_pageblock_skip(cc, valid_page, total_isolated, false);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700556
Minchan Kim010fc292012-12-20 15:05:06 -0800557 count_compact_events(COMPACTFREE_SCANNED, nr_scanned);
Mel Gorman397487d2012-10-19 12:00:10 +0100558 if (total_isolated)
Minchan Kim010fc292012-12-20 15:05:06 -0800559 count_compact_events(COMPACTISOLATED, total_isolated);
Mel Gorman748446b2010-05-24 14:32:27 -0700560 return total_isolated;
561}
562
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100563/**
564 * isolate_freepages_range() - isolate free pages.
565 * @start_pfn: The first PFN to start isolating.
566 * @end_pfn: The one-past-last PFN.
567 *
568 * Non-free pages, invalid PFNs, or zone boundaries within the
569 * [start_pfn, end_pfn) range are considered errors, cause function to
570 * undo its actions and return zero.
571 *
572 * Otherwise, function returns one-past-the-last PFN of isolated page
573 * (which may be greater then end_pfn if end fell in a middle of
574 * a free page).
575 */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100576unsigned long
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700577isolate_freepages_range(struct compact_control *cc,
578 unsigned long start_pfn, unsigned long end_pfn)
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100579{
Joonsoo Kime1409c32016-03-15 14:57:48 -0700580 unsigned long isolated, pfn, block_start_pfn, block_end_pfn;
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100581 LIST_HEAD(freelist);
582
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700583 pfn = start_pfn;
Vlastimil Babka06b66402016-05-19 17:11:48 -0700584 block_start_pfn = pageblock_start_pfn(pfn);
Joonsoo Kime1409c32016-03-15 14:57:48 -0700585 if (block_start_pfn < cc->zone->zone_start_pfn)
586 block_start_pfn = cc->zone->zone_start_pfn;
Vlastimil Babka06b66402016-05-19 17:11:48 -0700587 block_end_pfn = pageblock_end_pfn(pfn);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100588
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700589 for (; pfn < end_pfn; pfn += isolated,
Joonsoo Kime1409c32016-03-15 14:57:48 -0700590 block_start_pfn = block_end_pfn,
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700591 block_end_pfn += pageblock_nr_pages) {
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700592 /* Protect pfn from changing by isolate_freepages_block */
593 unsigned long isolate_start_pfn = pfn;
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700594
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100595 block_end_pfn = min(block_end_pfn, end_pfn);
596
Joonsoo Kim58420012014-11-13 15:19:07 -0800597 /*
598 * pfn could pass the block_end_pfn if isolated freepage
599 * is more than pageblock order. In this case, we adjust
600 * scanning range to right one.
601 */
602 if (pfn >= block_end_pfn) {
Vlastimil Babka06b66402016-05-19 17:11:48 -0700603 block_start_pfn = pageblock_start_pfn(pfn);
604 block_end_pfn = pageblock_end_pfn(pfn);
Joonsoo Kim58420012014-11-13 15:19:07 -0800605 block_end_pfn = min(block_end_pfn, end_pfn);
606 }
607
Joonsoo Kime1409c32016-03-15 14:57:48 -0700608 if (!pageblock_pfn_to_page(block_start_pfn,
609 block_end_pfn, cc->zone))
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700610 break;
611
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700612 isolated = isolate_freepages_block(cc, &isolate_start_pfn,
613 block_end_pfn, &freelist, true);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100614
615 /*
616 * In strict mode, isolate_freepages_block() returns 0 if
617 * there are any holes in the block (ie. invalid PFNs or
618 * non-free pages).
619 */
620 if (!isolated)
621 break;
622
623 /*
624 * If we managed to isolate pages, it is always (1 << n) *
625 * pageblock_nr_pages for some non-negative n. (Max order
626 * page may span two pageblocks).
627 */
628 }
629
Joonsoo Kim66c64222016-07-26 15:23:40 -0700630 /* __isolate_free_page() does not map the pages */
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100631 map_pages(&freelist);
632
633 if (pfn < end_pfn) {
634 /* Loop terminated early, cleanup. */
635 release_freepages(&freelist);
636 return 0;
637 }
638
639 /* We don't use freelists for anything. */
640 return pfn;
641}
642
Mel Gorman748446b2010-05-24 14:32:27 -0700643/* Update the number of anon and file isolated pages in the zone */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700644static void acct_isolated(struct zone *zone, struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700645{
646 struct page *page;
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700647 unsigned int count[2] = { 0, };
Mel Gorman748446b2010-05-24 14:32:27 -0700648
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700649 if (list_empty(&cc->migratepages))
650 return;
651
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700652 list_for_each_entry(page, &cc->migratepages, lru)
653 count[!!page_is_file_cache(page)]++;
Mel Gorman748446b2010-05-24 14:32:27 -0700654
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700655 mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
656 mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
Mel Gorman748446b2010-05-24 14:32:27 -0700657}
658
659/* Similar to reclaim, but different enough that they don't share logic */
660static bool too_many_isolated(struct zone *zone)
661{
Minchan Kimbc693042010-09-09 16:38:00 -0700662 unsigned long active, inactive, isolated;
Mel Gorman748446b2010-05-24 14:32:27 -0700663
664 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
665 zone_page_state(zone, NR_INACTIVE_ANON);
Minchan Kimbc693042010-09-09 16:38:00 -0700666 active = zone_page_state(zone, NR_ACTIVE_FILE) +
667 zone_page_state(zone, NR_ACTIVE_ANON);
Mel Gorman748446b2010-05-24 14:32:27 -0700668 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
669 zone_page_state(zone, NR_ISOLATED_ANON);
670
Minchan Kimbc693042010-09-09 16:38:00 -0700671 return isolated > (inactive + active) / 2;
Mel Gorman748446b2010-05-24 14:32:27 -0700672}
673
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100674/**
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700675 * isolate_migratepages_block() - isolate all migrate-able pages within
676 * a single pageblock
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100677 * @cc: Compaction control structure.
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700678 * @low_pfn: The first PFN to isolate
679 * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock
680 * @isolate_mode: Isolation mode to be used.
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100681 *
682 * Isolate all pages that can be migrated from the range specified by
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700683 * [low_pfn, end_pfn). The range is expected to be within same pageblock.
684 * Returns zero if there is a fatal signal pending, otherwise PFN of the
685 * first page that was not scanned (which may be both less, equal to or more
686 * than end_pfn).
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100687 *
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700688 * The pages are isolated on cc->migratepages list (not required to be empty),
689 * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field
690 * is neither read nor updated.
Mel Gorman748446b2010-05-24 14:32:27 -0700691 */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700692static unsigned long
693isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
694 unsigned long end_pfn, isolate_mode_t isolate_mode)
Mel Gorman748446b2010-05-24 14:32:27 -0700695{
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700696 struct zone *zone = cc->zone;
Mel Gormanb7aba692011-01-13 15:45:54 -0800697 unsigned long nr_scanned = 0, nr_isolated = 0;
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700698 struct lruvec *lruvec;
Xiubo Lib8b2d822014-10-09 15:28:21 -0700699 unsigned long flags = 0;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700700 bool locked = false;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700701 struct page *page = NULL, *valid_page = NULL;
Joonsoo Kime34d85f2015-02-11 15:27:04 -0800702 unsigned long start_pfn = low_pfn;
Vlastimil Babkafdd048e2016-05-19 17:11:55 -0700703 bool skip_on_failure = false;
704 unsigned long next_skip_pfn = 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700705
Mel Gorman748446b2010-05-24 14:32:27 -0700706 /*
707 * Ensure that there are not too many pages isolated from the LRU
708 * list by either parallel reclaimers or compaction. If there are,
709 * delay for some time until fewer pages are isolated
710 */
711 while (unlikely(too_many_isolated(zone))) {
Mel Gormanf9e35b32011-06-15 15:08:52 -0700712 /* async migration should just abort */
David Rientjese0b9dae2014-06-04 16:08:28 -0700713 if (cc->mode == MIGRATE_ASYNC)
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100714 return 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700715
Mel Gorman748446b2010-05-24 14:32:27 -0700716 congestion_wait(BLK_RW_ASYNC, HZ/10);
717
718 if (fatal_signal_pending(current))
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100719 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700720 }
721
Vlastimil Babkabe976572014-06-04 16:10:41 -0700722 if (compact_should_abort(cc))
723 return 0;
David Rientjesaeef4b82014-06-04 16:08:31 -0700724
Vlastimil Babkafdd048e2016-05-19 17:11:55 -0700725 if (cc->direct_compaction && (cc->mode == MIGRATE_ASYNC)) {
726 skip_on_failure = true;
727 next_skip_pfn = block_end_pfn(low_pfn, cc->order);
728 }
729
Mel Gorman748446b2010-05-24 14:32:27 -0700730 /* Time to isolate some pages for migration */
Mel Gorman748446b2010-05-24 14:32:27 -0700731 for (; low_pfn < end_pfn; low_pfn++) {
Vlastimil Babka29c0dde2015-09-08 15:02:46 -0700732
Vlastimil Babkafdd048e2016-05-19 17:11:55 -0700733 if (skip_on_failure && low_pfn >= next_skip_pfn) {
734 /*
735 * We have isolated all migration candidates in the
736 * previous order-aligned block, and did not skip it due
737 * to failure. We should migrate the pages now and
738 * hopefully succeed compaction.
739 */
740 if (nr_isolated)
741 break;
742
743 /*
744 * We failed to isolate in the previous order-aligned
745 * block. Set the new boundary to the end of the
746 * current block. Note we can't simply increase
747 * next_skip_pfn by 1 << order, as low_pfn might have
748 * been incremented by a higher number due to skipping
749 * a compound or a high-order buddy page in the
750 * previous loop iteration.
751 */
752 next_skip_pfn = block_end_pfn(low_pfn, cc->order);
753 }
754
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700755 /*
756 * Periodically drop the lock (if held) regardless of its
757 * contention, to give chance to IRQs. Abort async compaction
758 * if contended.
759 */
760 if (!(low_pfn % SWAP_CLUSTER_MAX)
761 && compact_unlock_should_abort(&zone->lru_lock, flags,
762 &locked, cc))
763 break;
Mel Gormanc67fe372012-08-21 16:16:17 -0700764
Mel Gorman748446b2010-05-24 14:32:27 -0700765 if (!pfn_valid_within(low_pfn))
Vlastimil Babkafdd048e2016-05-19 17:11:55 -0700766 goto isolate_fail;
Mel Gormanb7aba692011-01-13 15:45:54 -0800767 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700768
Mel Gorman748446b2010-05-24 14:32:27 -0700769 page = pfn_to_page(low_pfn);
Mel Gormandc908602012-02-08 17:13:38 -0800770
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700771 if (!valid_page)
772 valid_page = page;
773
Mel Gorman6c144662014-01-23 15:53:38 -0800774 /*
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700775 * Skip if free. We read page order here without zone lock
776 * which is generally unsafe, but the race window is small and
777 * the worst thing that can happen is that we skip some
778 * potential isolation targets.
Mel Gorman6c144662014-01-23 15:53:38 -0800779 */
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700780 if (PageBuddy(page)) {
781 unsigned long freepage_order = page_order_unsafe(page);
782
783 /*
784 * Without lock, we cannot be sure that what we got is
785 * a valid page order. Consider only values in the
786 * valid order range to prevent low_pfn overflow.
787 */
788 if (freepage_order > 0 && freepage_order < MAX_ORDER)
789 low_pfn += (1UL << freepage_order) - 1;
Mel Gorman748446b2010-05-24 14:32:27 -0700790 continue;
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700791 }
Mel Gorman748446b2010-05-24 14:32:27 -0700792
Mel Gorman9927af742011-01-13 15:45:59 -0800793 /*
Vlastimil Babka29c0dde2015-09-08 15:02:46 -0700794 * Regardless of being on LRU, compound pages such as THP and
795 * hugetlbfs are not to be compacted. We can potentially save
796 * a lot of iterations if we skip them at once. The check is
797 * racy, but we can consider only valid values and the only
798 * danger is skipping too much.
Andrea Arcangelibc835012011-01-13 15:47:08 -0800799 */
Vlastimil Babka29c0dde2015-09-08 15:02:46 -0700800 if (PageCompound(page)) {
801 unsigned int comp_order = compound_order(page);
802
803 if (likely(comp_order < MAX_ORDER))
804 low_pfn += (1UL << comp_order) - 1;
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700805
Vlastimil Babkafdd048e2016-05-19 17:11:55 -0700806 goto isolate_fail;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700807 }
808
Minchan Kimbda807d2016-07-26 15:23:05 -0700809 /*
810 * Check may be lockless but that's ok as we recheck later.
811 * It's possible to migrate LRU and non-lru movable pages.
812 * Skip any other type of page
813 */
814 if (!PageLRU(page)) {
Minchan Kimbda807d2016-07-26 15:23:05 -0700815 /*
816 * __PageMovable can return false positive so we need
817 * to verify it under page_lock.
818 */
819 if (unlikely(__PageMovable(page)) &&
820 !PageIsolated(page)) {
821 if (locked) {
822 spin_unlock_irqrestore(&zone->lru_lock,
823 flags);
824 locked = false;
825 }
826
827 if (isolate_movable_page(page, isolate_mode))
828 goto isolate_success;
829 }
830
Vlastimil Babkafdd048e2016-05-19 17:11:55 -0700831 goto isolate_fail;
Minchan Kimbda807d2016-07-26 15:23:05 -0700832 }
Vlastimil Babka29c0dde2015-09-08 15:02:46 -0700833
David Rientjes119d6d52014-04-03 14:48:00 -0700834 /*
835 * Migration will fail if an anonymous page is pinned in memory,
836 * so avoid taking lru_lock and isolating it unnecessarily in an
837 * admittedly racy check.
838 */
839 if (!page_mapping(page) &&
840 page_count(page) > page_mapcount(page))
Vlastimil Babkafdd048e2016-05-19 17:11:55 -0700841 goto isolate_fail;
David Rientjes119d6d52014-04-03 14:48:00 -0700842
Vlastimil Babka69b71892014-10-09 15:27:18 -0700843 /* If we already hold the lock, we can skip some rechecking */
844 if (!locked) {
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700845 locked = compact_trylock_irqsave(&zone->lru_lock,
846 &flags, cc);
Vlastimil Babka69b71892014-10-09 15:27:18 -0700847 if (!locked)
848 break;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700849
Vlastimil Babka29c0dde2015-09-08 15:02:46 -0700850 /* Recheck PageLRU and PageCompound under lock */
Vlastimil Babka69b71892014-10-09 15:27:18 -0700851 if (!PageLRU(page))
Vlastimil Babkafdd048e2016-05-19 17:11:55 -0700852 goto isolate_fail;
Vlastimil Babka29c0dde2015-09-08 15:02:46 -0700853
854 /*
855 * Page become compound since the non-locked check,
856 * and it's on LRU. It can only be a THP so the order
857 * is safe to read and it's 0 for tail pages.
858 */
859 if (unlikely(PageCompound(page))) {
860 low_pfn += (1UL << compound_order(page)) - 1;
Vlastimil Babkafdd048e2016-05-19 17:11:55 -0700861 goto isolate_fail;
Vlastimil Babka69b71892014-10-09 15:27:18 -0700862 }
Andrea Arcangelibc835012011-01-13 15:47:08 -0800863 }
864
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700865 lruvec = mem_cgroup_page_lruvec(page, zone);
866
Mel Gorman748446b2010-05-24 14:32:27 -0700867 /* Try isolate the page */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700868 if (__isolate_lru_page(page, isolate_mode) != 0)
Vlastimil Babkafdd048e2016-05-19 17:11:55 -0700869 goto isolate_fail;
Mel Gorman748446b2010-05-24 14:32:27 -0700870
Vlastimil Babka29c0dde2015-09-08 15:02:46 -0700871 VM_BUG_ON_PAGE(PageCompound(page), page);
Andrea Arcangelibc835012011-01-13 15:47:08 -0800872
Mel Gorman748446b2010-05-24 14:32:27 -0700873 /* Successfully isolated */
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700874 del_page_from_lru_list(page, lruvec, page_lru(page));
Joonsoo Kimb6c75012014-04-07 15:37:07 -0700875
876isolate_success:
Vlastimil Babkafdd048e2016-05-19 17:11:55 -0700877 list_add(&page->lru, &cc->migratepages);
Mel Gorman748446b2010-05-24 14:32:27 -0700878 cc->nr_migratepages++;
Mel Gormanb7aba692011-01-13 15:45:54 -0800879 nr_isolated++;
Mel Gorman748446b2010-05-24 14:32:27 -0700880
Vlastimil Babkaa34753d2016-05-19 17:11:51 -0700881 /*
882 * Record where we could have freed pages by migration and not
883 * yet flushed them to buddy allocator.
884 * - this is the lowest page that was isolated and likely be
885 * then freed by migration.
886 */
887 if (!cc->last_migrated_pfn)
888 cc->last_migrated_pfn = low_pfn;
889
Mel Gorman748446b2010-05-24 14:32:27 -0700890 /* Avoid isolating too much */
Hillf Danton31b83842012-01-10 15:07:59 -0800891 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
892 ++low_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700893 break;
Hillf Danton31b83842012-01-10 15:07:59 -0800894 }
Vlastimil Babkafdd048e2016-05-19 17:11:55 -0700895
896 continue;
897isolate_fail:
898 if (!skip_on_failure)
899 continue;
900
901 /*
902 * We have isolated some pages, but then failed. Release them
903 * instead of migrating, as we cannot form the cc->order buddy
904 * page anyway.
905 */
906 if (nr_isolated) {
907 if (locked) {
908 spin_unlock_irqrestore(&zone->lru_lock, flags);
909 locked = false;
910 }
911 acct_isolated(zone, cc);
912 putback_movable_pages(&cc->migratepages);
913 cc->nr_migratepages = 0;
914 cc->last_migrated_pfn = 0;
915 nr_isolated = 0;
916 }
917
918 if (low_pfn < next_skip_pfn) {
919 low_pfn = next_skip_pfn - 1;
920 /*
921 * The check near the loop beginning would have updated
922 * next_skip_pfn too, but this is a bit simpler.
923 */
924 next_skip_pfn += 1UL << cc->order;
925 }
Mel Gorman748446b2010-05-24 14:32:27 -0700926 }
927
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700928 /*
929 * The PageBuddy() check could have potentially brought us outside
930 * the range to be scanned.
931 */
932 if (unlikely(low_pfn > end_pfn))
933 low_pfn = end_pfn;
934
Mel Gormanc67fe372012-08-21 16:16:17 -0700935 if (locked)
936 spin_unlock_irqrestore(&zone->lru_lock, flags);
Mel Gorman748446b2010-05-24 14:32:27 -0700937
Vlastimil Babka50b5b092014-01-21 15:51:10 -0800938 /*
939 * Update the pageblock-skip information and cached scanner pfn,
940 * if the whole pageblock was scanned without isolating any page.
Vlastimil Babka50b5b092014-01-21 15:51:10 -0800941 */
David Rientjes35979ef2014-06-04 16:08:27 -0700942 if (low_pfn == end_pfn)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700943 update_pageblock_skip(cc, valid_page, nr_isolated, true);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700944
Joonsoo Kime34d85f2015-02-11 15:27:04 -0800945 trace_mm_compaction_isolate_migratepages(start_pfn, low_pfn,
946 nr_scanned, nr_isolated);
Mel Gormanb7aba692011-01-13 15:45:54 -0800947
Minchan Kim010fc292012-12-20 15:05:06 -0800948 count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned);
Mel Gorman397487d2012-10-19 12:00:10 +0100949 if (nr_isolated)
Minchan Kim010fc292012-12-20 15:05:06 -0800950 count_compact_events(COMPACTISOLATED, nr_isolated);
Mel Gorman397487d2012-10-19 12:00:10 +0100951
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100952 return low_pfn;
953}
954
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700955/**
956 * isolate_migratepages_range() - isolate migrate-able pages in a PFN range
957 * @cc: Compaction control structure.
958 * @start_pfn: The first PFN to start isolating.
959 * @end_pfn: The one-past-last PFN.
960 *
961 * Returns zero if isolation fails fatally due to e.g. pending signal.
962 * Otherwise, function returns one-past-the-last PFN of isolated page
963 * (which may be greater than end_pfn if end fell in a middle of a THP page).
964 */
965unsigned long
966isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn,
967 unsigned long end_pfn)
968{
Joonsoo Kime1409c32016-03-15 14:57:48 -0700969 unsigned long pfn, block_start_pfn, block_end_pfn;
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700970
971 /* Scan block by block. First and last block may be incomplete */
972 pfn = start_pfn;
Vlastimil Babka06b66402016-05-19 17:11:48 -0700973 block_start_pfn = pageblock_start_pfn(pfn);
Joonsoo Kime1409c32016-03-15 14:57:48 -0700974 if (block_start_pfn < cc->zone->zone_start_pfn)
975 block_start_pfn = cc->zone->zone_start_pfn;
Vlastimil Babka06b66402016-05-19 17:11:48 -0700976 block_end_pfn = pageblock_end_pfn(pfn);
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700977
978 for (; pfn < end_pfn; pfn = block_end_pfn,
Joonsoo Kime1409c32016-03-15 14:57:48 -0700979 block_start_pfn = block_end_pfn,
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700980 block_end_pfn += pageblock_nr_pages) {
981
982 block_end_pfn = min(block_end_pfn, end_pfn);
983
Joonsoo Kime1409c32016-03-15 14:57:48 -0700984 if (!pageblock_pfn_to_page(block_start_pfn,
985 block_end_pfn, cc->zone))
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700986 continue;
987
988 pfn = isolate_migratepages_block(cc, pfn, block_end_pfn,
989 ISOLATE_UNEVICTABLE);
990
Hugh Dickins14af4a52016-05-05 16:22:15 -0700991 if (!pfn)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700992 break;
Joonsoo Kim6ea41c02014-10-29 14:50:20 -0700993
994 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX)
995 break;
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700996 }
997 acct_isolated(cc->zone, cc);
998
999 return pfn;
1000}
1001
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001002#endif /* CONFIG_COMPACTION || CONFIG_CMA */
1003#ifdef CONFIG_COMPACTION
Andrew Morton018e9a42015-04-15 16:15:20 -07001004
1005/* Returns true if the page is within a block suitable for migration to */
1006static bool suitable_migration_target(struct page *page)
1007{
1008 /* If the page is a large free page, then disallow migration */
1009 if (PageBuddy(page)) {
1010 /*
1011 * We are checking page_order without zone->lock taken. But
1012 * the only small danger is that we skip a potentially suitable
1013 * pageblock, so it's not worth to check order for valid range.
1014 */
1015 if (page_order_unsafe(page) >= pageblock_order)
1016 return false;
1017 }
1018
1019 /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
1020 if (migrate_async_suitable(get_pageblock_migratetype(page)))
1021 return true;
1022
1023 /* Otherwise skip the block */
1024 return false;
1025}
1026
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001027/*
Vlastimil Babkaf2849aa2015-09-08 15:02:36 -07001028 * Test whether the free scanner has reached the same or lower pageblock than
1029 * the migration scanner, and compaction should thus terminate.
1030 */
1031static inline bool compact_scanners_met(struct compact_control *cc)
1032{
1033 return (cc->free_pfn >> pageblock_order)
1034 <= (cc->migrate_pfn >> pageblock_order);
1035}
1036
1037/*
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001038 * Based on information in the current compact_control, find blocks
1039 * suitable for isolating free pages from and then isolate them.
1040 */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001041static void isolate_freepages(struct compact_control *cc)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001042{
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001043 struct zone *zone = cc->zone;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001044 struct page *page;
Vlastimil Babkac96b9e52014-06-04 16:07:26 -07001045 unsigned long block_start_pfn; /* start of current pageblock */
Vlastimil Babkae14c7202014-10-09 15:27:20 -07001046 unsigned long isolate_start_pfn; /* exact pfn we start at */
Vlastimil Babkac96b9e52014-06-04 16:07:26 -07001047 unsigned long block_end_pfn; /* end of current pageblock */
1048 unsigned long low_pfn; /* lowest pfn scanner is able to scan */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001049 struct list_head *freelist = &cc->freepages;
1050
1051 /*
1052 * Initialise the free scanner. The starting point is where we last
Vlastimil Babka49e068f2014-05-06 12:50:03 -07001053 * successfully isolated from, zone-cached value, or the end of the
Vlastimil Babkae14c7202014-10-09 15:27:20 -07001054 * zone when isolating for the first time. For looping we also need
1055 * this pfn aligned down to the pageblock boundary, because we do
Vlastimil Babkac96b9e52014-06-04 16:07:26 -07001056 * block_start_pfn -= pageblock_nr_pages in the for loop.
1057 * For ending point, take care when isolating in last pageblock of a
1058 * a zone which ends in the middle of a pageblock.
Vlastimil Babka49e068f2014-05-06 12:50:03 -07001059 * The low boundary is the end of the pageblock the migration scanner
1060 * is using.
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001061 */
Vlastimil Babkae14c7202014-10-09 15:27:20 -07001062 isolate_start_pfn = cc->free_pfn;
Vlastimil Babka06b66402016-05-19 17:11:48 -07001063 block_start_pfn = pageblock_start_pfn(cc->free_pfn);
Vlastimil Babkac96b9e52014-06-04 16:07:26 -07001064 block_end_pfn = min(block_start_pfn + pageblock_nr_pages,
1065 zone_end_pfn(zone));
Vlastimil Babka06b66402016-05-19 17:11:48 -07001066 low_pfn = pageblock_end_pfn(cc->migrate_pfn);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001067
1068 /*
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001069 * Isolate free pages until enough are available to migrate the
1070 * pages on cc->migratepages. We stop searching if the migrate
1071 * and free page scanners meet or enough free pages are isolated.
1072 */
Vlastimil Babkaf5f61a32015-09-08 15:02:39 -07001073 for (; block_start_pfn >= low_pfn;
Vlastimil Babkac96b9e52014-06-04 16:07:26 -07001074 block_end_pfn = block_start_pfn,
Vlastimil Babkae14c7202014-10-09 15:27:20 -07001075 block_start_pfn -= pageblock_nr_pages,
1076 isolate_start_pfn = block_start_pfn) {
David Rientjesf6ea3ad2013-09-30 13:45:03 -07001077 /*
1078 * This can iterate a massively long zone without finding any
1079 * suitable migration targets, so periodically check if we need
Vlastimil Babkabe976572014-06-04 16:10:41 -07001080 * to schedule, or even abort async compaction.
David Rientjesf6ea3ad2013-09-30 13:45:03 -07001081 */
Vlastimil Babkabe976572014-06-04 16:10:41 -07001082 if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
1083 && compact_should_abort(cc))
1084 break;
David Rientjesf6ea3ad2013-09-30 13:45:03 -07001085
Vlastimil Babka7d49d882014-10-09 15:27:11 -07001086 page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
1087 zone);
1088 if (!page)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001089 continue;
1090
1091 /* Check the block is suitable for migration */
Linus Torvalds68e3e922012-06-03 20:05:57 -07001092 if (!suitable_migration_target(page))
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001093 continue;
Linus Torvalds68e3e922012-06-03 20:05:57 -07001094
Mel Gormanbb13ffe2012-10-08 16:32:41 -07001095 /* If isolation recently failed, do not retry */
1096 if (!isolation_suitable(cc, page))
1097 continue;
1098
Vlastimil Babkae14c7202014-10-09 15:27:20 -07001099 /* Found a block suitable for isolating free pages from. */
David Rientjesa46cbf32016-07-14 12:06:50 -07001100 isolate_freepages_block(cc, &isolate_start_pfn, block_end_pfn,
1101 freelist, false);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001102
1103 /*
David Rientjesa46cbf32016-07-14 12:06:50 -07001104 * If we isolated enough freepages, or aborted due to lock
1105 * contention, terminate.
Vlastimil Babkae14c7202014-10-09 15:27:20 -07001106 */
Vlastimil Babkaf5f61a32015-09-08 15:02:39 -07001107 if ((cc->nr_freepages >= cc->nr_migratepages)
1108 || cc->contended) {
David Rientjesa46cbf32016-07-14 12:06:50 -07001109 if (isolate_start_pfn >= block_end_pfn) {
1110 /*
1111 * Restart at previous pageblock if more
1112 * freepages can be isolated next time.
1113 */
Vlastimil Babkaf5f61a32015-09-08 15:02:39 -07001114 isolate_start_pfn =
1115 block_start_pfn - pageblock_nr_pages;
David Rientjesa46cbf32016-07-14 12:06:50 -07001116 }
Vlastimil Babkabe976572014-06-04 16:10:41 -07001117 break;
David Rientjesa46cbf32016-07-14 12:06:50 -07001118 } else if (isolate_start_pfn < block_end_pfn) {
Vlastimil Babkaf5f61a32015-09-08 15:02:39 -07001119 /*
David Rientjesa46cbf32016-07-14 12:06:50 -07001120 * If isolation failed early, do not continue
1121 * needlessly.
Vlastimil Babkaf5f61a32015-09-08 15:02:39 -07001122 */
David Rientjesa46cbf32016-07-14 12:06:50 -07001123 break;
Vlastimil Babkaf5f61a32015-09-08 15:02:39 -07001124 }
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +01001125 }
1126
Joonsoo Kim66c64222016-07-26 15:23:40 -07001127 /* __isolate_free_page() does not map the pages */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001128 map_pages(freelist);
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +01001129
Vlastimil Babka7ed695e2014-01-21 15:51:09 -08001130 /*
Vlastimil Babkaf5f61a32015-09-08 15:02:39 -07001131 * Record where the free scanner will restart next time. Either we
1132 * broke from the loop and set isolate_start_pfn based on the last
1133 * call to isolate_freepages_block(), or we met the migration scanner
1134 * and the loop terminated due to isolate_start_pfn < low_pfn
Vlastimil Babka7ed695e2014-01-21 15:51:09 -08001135 */
Vlastimil Babkaf5f61a32015-09-08 15:02:39 -07001136 cc->free_pfn = isolate_start_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -07001137}
1138
1139/*
1140 * This is a migrate-callback that "allocates" freepages by taking pages
1141 * from the isolated freelists in the block we are migrating to.
1142 */
1143static struct page *compaction_alloc(struct page *migratepage,
1144 unsigned long data,
1145 int **result)
1146{
1147 struct compact_control *cc = (struct compact_control *)data;
1148 struct page *freepage;
1149
Vlastimil Babkabe976572014-06-04 16:10:41 -07001150 /*
1151 * Isolate free pages if necessary, and if we are not aborting due to
1152 * contention.
1153 */
Mel Gorman748446b2010-05-24 14:32:27 -07001154 if (list_empty(&cc->freepages)) {
Vlastimil Babkabe976572014-06-04 16:10:41 -07001155 if (!cc->contended)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001156 isolate_freepages(cc);
Mel Gorman748446b2010-05-24 14:32:27 -07001157
1158 if (list_empty(&cc->freepages))
1159 return NULL;
1160 }
1161
1162 freepage = list_entry(cc->freepages.next, struct page, lru);
1163 list_del(&freepage->lru);
1164 cc->nr_freepages--;
1165
1166 return freepage;
1167}
1168
1169/*
David Rientjesd53aea32014-06-04 16:08:26 -07001170 * This is a migrate-callback that "frees" freepages back to the isolated
1171 * freelist. All pages on the freelist are from the same zone, so there is no
1172 * special handling needed for NUMA.
1173 */
1174static void compaction_free(struct page *page, unsigned long data)
1175{
1176 struct compact_control *cc = (struct compact_control *)data;
1177
1178 list_add(&page->lru, &cc->freepages);
1179 cc->nr_freepages++;
1180}
1181
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001182/* possible outcome of isolate_migratepages */
1183typedef enum {
1184 ISOLATE_ABORT, /* Abort compaction now */
1185 ISOLATE_NONE, /* No pages isolated, continue scanning */
1186 ISOLATE_SUCCESS, /* Pages isolated, migrate */
1187} isolate_migrate_t;
1188
1189/*
Eric B Munson5bbe3542015-04-15 16:13:20 -07001190 * Allow userspace to control policy on scanning the unevictable LRU for
1191 * compactable pages.
1192 */
1193int sysctl_compact_unevictable_allowed __read_mostly = 1;
1194
1195/*
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001196 * Isolate all pages that can be migrated from the first suitable block,
1197 * starting at the block pointed to by the migrate scanner pfn within
1198 * compact_control.
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001199 */
1200static isolate_migrate_t isolate_migratepages(struct zone *zone,
1201 struct compact_control *cc)
1202{
Joonsoo Kime1409c32016-03-15 14:57:48 -07001203 unsigned long block_start_pfn;
1204 unsigned long block_end_pfn;
1205 unsigned long low_pfn;
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001206 struct page *page;
1207 const isolate_mode_t isolate_mode =
Eric B Munson5bbe3542015-04-15 16:13:20 -07001208 (sysctl_compact_unevictable_allowed ? ISOLATE_UNEVICTABLE : 0) |
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001209 (cc->mode == MIGRATE_ASYNC ? ISOLATE_ASYNC_MIGRATE : 0);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001210
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001211 /*
1212 * Start at where we last stopped, or beginning of the zone as
1213 * initialized by compact_zone()
1214 */
1215 low_pfn = cc->migrate_pfn;
Vlastimil Babka06b66402016-05-19 17:11:48 -07001216 block_start_pfn = pageblock_start_pfn(low_pfn);
Joonsoo Kime1409c32016-03-15 14:57:48 -07001217 if (block_start_pfn < zone->zone_start_pfn)
1218 block_start_pfn = zone->zone_start_pfn;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001219
1220 /* Only scan within a pageblock boundary */
Vlastimil Babka06b66402016-05-19 17:11:48 -07001221 block_end_pfn = pageblock_end_pfn(low_pfn);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001222
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001223 /*
1224 * Iterate over whole pageblocks until we find the first suitable.
1225 * Do not cross the free scanner.
1226 */
Joonsoo Kime1409c32016-03-15 14:57:48 -07001227 for (; block_end_pfn <= cc->free_pfn;
1228 low_pfn = block_end_pfn,
1229 block_start_pfn = block_end_pfn,
1230 block_end_pfn += pageblock_nr_pages) {
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001231
1232 /*
1233 * This can potentially iterate a massively long zone with
1234 * many pageblocks unsuitable, so periodically check if we
1235 * need to schedule, or even abort async compaction.
1236 */
1237 if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
1238 && compact_should_abort(cc))
1239 break;
1240
Joonsoo Kime1409c32016-03-15 14:57:48 -07001241 page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
1242 zone);
Vlastimil Babka7d49d882014-10-09 15:27:11 -07001243 if (!page)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001244 continue;
1245
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001246 /* If isolation recently failed, do not retry */
1247 if (!isolation_suitable(cc, page))
1248 continue;
1249
1250 /*
1251 * For async compaction, also only scan in MOVABLE blocks.
1252 * Async compaction is optimistic to see if the minimum amount
1253 * of work satisfies the allocation.
1254 */
1255 if (cc->mode == MIGRATE_ASYNC &&
1256 !migrate_async_suitable(get_pageblock_migratetype(page)))
1257 continue;
1258
1259 /* Perform the isolation */
Joonsoo Kime1409c32016-03-15 14:57:48 -07001260 low_pfn = isolate_migratepages_block(cc, low_pfn,
1261 block_end_pfn, isolate_mode);
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001262
Hugh Dickinsff599092015-02-12 15:00:28 -08001263 if (!low_pfn || cc->contended) {
1264 acct_isolated(zone, cc);
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001265 return ISOLATE_ABORT;
Hugh Dickinsff599092015-02-12 15:00:28 -08001266 }
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001267
1268 /*
1269 * Either we isolated something and proceed with migration. Or
1270 * we failed and compact_zone should decide if we should
1271 * continue or not.
1272 */
1273 break;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001274 }
1275
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001276 acct_isolated(zone, cc);
Vlastimil Babkaf2849aa2015-09-08 15:02:36 -07001277 /* Record where migration scanner will be restarted. */
1278 cc->migrate_pfn = low_pfn;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001279
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001280 return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001281}
1282
Yaowei Bai21c527a2015-11-05 18:47:20 -08001283/*
1284 * order == -1 is expected when compacting via
1285 * /proc/sys/vm/compact_memory
1286 */
1287static inline bool is_via_compact_memory(int order)
1288{
1289 return order == -1;
1290}
1291
Michal Hockoea7ab982016-05-20 16:56:38 -07001292static enum compact_result __compact_finished(struct zone *zone, struct compact_control *cc,
David Rientjes6d7ce552014-10-09 15:27:27 -07001293 const int migratetype)
Mel Gorman748446b2010-05-24 14:32:27 -07001294{
Mel Gorman8fb74b92013-01-11 14:32:16 -08001295 unsigned int order;
Andrea Arcangeli5a03b052011-01-13 15:47:11 -08001296 unsigned long watermark;
Mel Gorman56de7262010-05-24 14:32:30 -07001297
Vlastimil Babkabe976572014-06-04 16:10:41 -07001298 if (cc->contended || fatal_signal_pending(current))
Vlastimil Babka2d1e1042015-11-05 18:48:02 -08001299 return COMPACT_CONTENDED;
Mel Gorman748446b2010-05-24 14:32:27 -07001300
Mel Gorman753341a2012-10-08 16:32:40 -07001301 /* Compaction run completes if the migrate and free scanner meet */
Vlastimil Babkaf2849aa2015-09-08 15:02:36 -07001302 if (compact_scanners_met(cc)) {
Vlastimil Babka55b7c4c2014-01-21 15:51:11 -08001303 /* Let the next compaction start anew. */
Vlastimil Babka02333642015-09-08 15:02:42 -07001304 reset_cached_positions(zone);
Vlastimil Babka55b7c4c2014-01-21 15:51:11 -08001305
Mel Gorman62997022012-10-08 16:32:47 -07001306 /*
1307 * Mark that the PG_migrate_skip information should be cleared
Vlastimil Babkaaccf6242016-03-17 14:18:15 -07001308 * by kswapd when it goes to sleep. kcompactd does not set the
Mel Gorman62997022012-10-08 16:32:47 -07001309 * flag itself as the decision to be clear should be directly
1310 * based on an allocation request.
1311 */
Vlastimil Babkaaccf6242016-03-17 14:18:15 -07001312 if (cc->direct_compaction)
Mel Gorman62997022012-10-08 16:32:47 -07001313 zone->compact_blockskip_flush = true;
1314
Michal Hockoc8f7de02016-05-20 16:56:47 -07001315 if (cc->whole_zone)
1316 return COMPACT_COMPLETE;
1317 else
1318 return COMPACT_PARTIAL_SKIPPED;
Mel Gormanbb13ffe2012-10-08 16:32:41 -07001319 }
Mel Gorman748446b2010-05-24 14:32:27 -07001320
Yaowei Bai21c527a2015-11-05 18:47:20 -08001321 if (is_via_compact_memory(cc->order))
Mel Gorman56de7262010-05-24 14:32:30 -07001322 return COMPACT_CONTINUE;
1323
Michal Hocko3957c772011-06-15 15:08:25 -07001324 /* Compaction run is not finished if the watermark is not met */
1325 watermark = low_wmark_pages(zone);
Michal Hocko3957c772011-06-15 15:08:25 -07001326
Vlastimil Babkaebff3982014-12-10 15:43:22 -08001327 if (!zone_watermark_ok(zone, cc->order, watermark, cc->classzone_idx,
1328 cc->alloc_flags))
Michal Hocko3957c772011-06-15 15:08:25 -07001329 return COMPACT_CONTINUE;
1330
Mel Gorman56de7262010-05-24 14:32:30 -07001331 /* Direct compactor: Is a suitable page free? */
Mel Gorman8fb74b92013-01-11 14:32:16 -08001332 for (order = cc->order; order < MAX_ORDER; order++) {
1333 struct free_area *area = &zone->free_area[order];
Joonsoo Kim2149cda2015-04-14 15:45:21 -07001334 bool can_steal;
Mel Gorman56de7262010-05-24 14:32:30 -07001335
Mel Gorman8fb74b92013-01-11 14:32:16 -08001336 /* Job done if page is free of the right migratetype */
David Rientjes6d7ce552014-10-09 15:27:27 -07001337 if (!list_empty(&area->free_list[migratetype]))
Mel Gorman8fb74b92013-01-11 14:32:16 -08001338 return COMPACT_PARTIAL;
1339
Joonsoo Kim2149cda2015-04-14 15:45:21 -07001340#ifdef CONFIG_CMA
1341 /* MIGRATE_MOVABLE can fallback on MIGRATE_CMA */
1342 if (migratetype == MIGRATE_MOVABLE &&
1343 !list_empty(&area->free_list[MIGRATE_CMA]))
1344 return COMPACT_PARTIAL;
1345#endif
1346 /*
1347 * Job done if allocation would steal freepages from
1348 * other migratetype buddy lists.
1349 */
1350 if (find_suitable_fallback(area, order, migratetype,
1351 true, &can_steal) != -1)
Mel Gorman8fb74b92013-01-11 14:32:16 -08001352 return COMPACT_PARTIAL;
Mel Gorman56de7262010-05-24 14:32:30 -07001353 }
1354
Joonsoo Kim837d0262015-02-11 15:27:06 -08001355 return COMPACT_NO_SUITABLE_PAGE;
1356}
1357
Michal Hockoea7ab982016-05-20 16:56:38 -07001358static enum compact_result compact_finished(struct zone *zone,
1359 struct compact_control *cc,
1360 const int migratetype)
Joonsoo Kim837d0262015-02-11 15:27:06 -08001361{
1362 int ret;
1363
1364 ret = __compact_finished(zone, cc, migratetype);
1365 trace_mm_compaction_finished(zone, cc->order, ret);
1366 if (ret == COMPACT_NO_SUITABLE_PAGE)
1367 ret = COMPACT_CONTINUE;
1368
1369 return ret;
Mel Gorman748446b2010-05-24 14:32:27 -07001370}
1371
Mel Gorman3e7d3442011-01-13 15:45:56 -08001372/*
1373 * compaction_suitable: Is this suitable to run compaction on this zone now?
1374 * Returns
1375 * COMPACT_SKIPPED - If there are too few free pages for compaction
1376 * COMPACT_PARTIAL - If the allocation would succeed without compaction
1377 * COMPACT_CONTINUE - If compaction should run now
1378 */
Michal Hockoea7ab982016-05-20 16:56:38 -07001379static enum compact_result __compaction_suitable(struct zone *zone, int order,
Mel Gormanc6038442016-05-19 17:13:38 -07001380 unsigned int alloc_flags,
Michal Hocko86a294a2016-05-20 16:57:12 -07001381 int classzone_idx,
1382 unsigned long wmark_target)
Mel Gorman3e7d3442011-01-13 15:45:56 -08001383{
1384 int fragindex;
1385 unsigned long watermark;
1386
Yaowei Bai21c527a2015-11-05 18:47:20 -08001387 if (is_via_compact_memory(order))
Michal Hocko3957c772011-06-15 15:08:25 -07001388 return COMPACT_CONTINUE;
1389
Vlastimil Babkaebff3982014-12-10 15:43:22 -08001390 watermark = low_wmark_pages(zone);
1391 /*
1392 * If watermarks for high-order allocation are already met, there
1393 * should be no need for compaction at all.
1394 */
1395 if (zone_watermark_ok(zone, order, watermark, classzone_idx,
1396 alloc_flags))
1397 return COMPACT_PARTIAL;
1398
Michal Hocko3957c772011-06-15 15:08:25 -07001399 /*
Mel Gorman3e7d3442011-01-13 15:45:56 -08001400 * Watermarks for order-0 must be met for compaction. Note the 2UL.
1401 * This is because during migration, copies of pages need to be
1402 * allocated and for a short time, the footprint is higher
1403 */
Vlastimil Babkaebff3982014-12-10 15:43:22 -08001404 watermark += (2UL << order);
Michal Hocko86a294a2016-05-20 16:57:12 -07001405 if (!__zone_watermark_ok(zone, 0, watermark, classzone_idx,
1406 alloc_flags, wmark_target))
Mel Gorman3e7d3442011-01-13 15:45:56 -08001407 return COMPACT_SKIPPED;
1408
1409 /*
1410 * fragmentation index determines if allocation failures are due to
1411 * low memory or external fragmentation
1412 *
Vlastimil Babkaebff3982014-12-10 15:43:22 -08001413 * index of -1000 would imply allocations might succeed depending on
1414 * watermarks, but we already failed the high-order watermark check
Mel Gorman3e7d3442011-01-13 15:45:56 -08001415 * index towards 0 implies failure is due to lack of memory
1416 * index towards 1000 implies failure is due to fragmentation
1417 *
1418 * Only compact if a failure would be due to fragmentation.
1419 */
1420 fragindex = fragmentation_index(zone, order);
1421 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
Joonsoo Kim837d0262015-02-11 15:27:06 -08001422 return COMPACT_NOT_SUITABLE_ZONE;
Mel Gorman3e7d3442011-01-13 15:45:56 -08001423
Mel Gorman3e7d3442011-01-13 15:45:56 -08001424 return COMPACT_CONTINUE;
1425}
1426
Michal Hockoea7ab982016-05-20 16:56:38 -07001427enum compact_result compaction_suitable(struct zone *zone, int order,
Mel Gormanc6038442016-05-19 17:13:38 -07001428 unsigned int alloc_flags,
1429 int classzone_idx)
Joonsoo Kim837d0262015-02-11 15:27:06 -08001430{
Michal Hockoea7ab982016-05-20 16:56:38 -07001431 enum compact_result ret;
Joonsoo Kim837d0262015-02-11 15:27:06 -08001432
Michal Hocko86a294a2016-05-20 16:57:12 -07001433 ret = __compaction_suitable(zone, order, alloc_flags, classzone_idx,
1434 zone_page_state(zone, NR_FREE_PAGES));
Joonsoo Kim837d0262015-02-11 15:27:06 -08001435 trace_mm_compaction_suitable(zone, order, ret);
1436 if (ret == COMPACT_NOT_SUITABLE_ZONE)
1437 ret = COMPACT_SKIPPED;
1438
1439 return ret;
1440}
1441
Michal Hocko86a294a2016-05-20 16:57:12 -07001442bool compaction_zonelist_suitable(struct alloc_context *ac, int order,
1443 int alloc_flags)
1444{
1445 struct zone *zone;
1446 struct zoneref *z;
1447
1448 /*
1449 * Make sure at least one zone would pass __compaction_suitable if we continue
1450 * retrying the reclaim.
1451 */
1452 for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx,
1453 ac->nodemask) {
1454 unsigned long available;
1455 enum compact_result compact_result;
1456
1457 /*
1458 * Do not consider all the reclaimable memory because we do not
1459 * want to trash just for a single high order allocation which
1460 * is even not guaranteed to appear even if __compaction_suitable
1461 * is happy about the watermark check.
1462 */
1463 available = zone_reclaimable_pages(zone) / order;
1464 available += zone_page_state_snapshot(zone, NR_FREE_PAGES);
1465 compact_result = __compaction_suitable(zone, order, alloc_flags,
1466 ac_classzone_idx(ac), available);
1467 if (compact_result != COMPACT_SKIPPED &&
1468 compact_result != COMPACT_NOT_SUITABLE_ZONE)
1469 return true;
1470 }
1471
1472 return false;
1473}
1474
Michal Hockoea7ab982016-05-20 16:56:38 -07001475static enum compact_result compact_zone(struct zone *zone, struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -07001476{
Michal Hockoea7ab982016-05-20 16:56:38 -07001477 enum compact_result ret;
Mel Gormanc89511a2012-10-08 16:32:45 -07001478 unsigned long start_pfn = zone->zone_start_pfn;
Cody P Schafer108bcc92013-02-22 16:35:23 -08001479 unsigned long end_pfn = zone_end_pfn(zone);
David Rientjes6d7ce552014-10-09 15:27:27 -07001480 const int migratetype = gfpflags_to_migratetype(cc->gfp_mask);
David Rientjese0b9dae2014-06-04 16:08:28 -07001481 const bool sync = cc->mode != MIGRATE_ASYNC;
Mel Gorman748446b2010-05-24 14:32:27 -07001482
Vlastimil Babkaebff3982014-12-10 15:43:22 -08001483 ret = compaction_suitable(zone, cc->order, cc->alloc_flags,
1484 cc->classzone_idx);
Michal Hockoc46649d2016-05-20 16:56:41 -07001485 /* Compaction is likely to fail */
1486 if (ret == COMPACT_PARTIAL || ret == COMPACT_SKIPPED)
Mel Gorman3e7d3442011-01-13 15:45:56 -08001487 return ret;
Michal Hockoc46649d2016-05-20 16:56:41 -07001488
1489 /* huh, compaction_suitable is returning something unexpected */
1490 VM_BUG_ON(ret != COMPACT_CONTINUE);
Mel Gorman3e7d3442011-01-13 15:45:56 -08001491
Mel Gormanc89511a2012-10-08 16:32:45 -07001492 /*
Vlastimil Babkad3132e42014-01-21 15:51:08 -08001493 * Clear pageblock skip if there were failures recently and compaction
Vlastimil Babkaaccf6242016-03-17 14:18:15 -07001494 * is about to be retried after being deferred.
Vlastimil Babkad3132e42014-01-21 15:51:08 -08001495 */
Vlastimil Babkaaccf6242016-03-17 14:18:15 -07001496 if (compaction_restarting(zone, cc->order))
Vlastimil Babkad3132e42014-01-21 15:51:08 -08001497 __reset_isolation_suitable(zone);
1498
1499 /*
Mel Gormanc89511a2012-10-08 16:32:45 -07001500 * Setup to move all movable pages to the end of the zone. Used cached
1501 * information on where the scanners should start but check that it
1502 * is initialised by ensuring the values are within zone boundaries.
1503 */
David Rientjese0b9dae2014-06-04 16:08:28 -07001504 cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync];
Mel Gormanc89511a2012-10-08 16:32:45 -07001505 cc->free_pfn = zone->compact_cached_free_pfn;
Joonsoo Kim623446e2016-03-15 14:57:45 -07001506 if (cc->free_pfn < start_pfn || cc->free_pfn >= end_pfn) {
Vlastimil Babka06b66402016-05-19 17:11:48 -07001507 cc->free_pfn = pageblock_start_pfn(end_pfn - 1);
Mel Gormanc89511a2012-10-08 16:32:45 -07001508 zone->compact_cached_free_pfn = cc->free_pfn;
1509 }
Joonsoo Kim623446e2016-03-15 14:57:45 -07001510 if (cc->migrate_pfn < start_pfn || cc->migrate_pfn >= end_pfn) {
Mel Gormanc89511a2012-10-08 16:32:45 -07001511 cc->migrate_pfn = start_pfn;
David Rientjes35979ef2014-06-04 16:08:27 -07001512 zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn;
1513 zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn;
Mel Gormanc89511a2012-10-08 16:32:45 -07001514 }
Michal Hockoc8f7de02016-05-20 16:56:47 -07001515
1516 if (cc->migrate_pfn == start_pfn)
1517 cc->whole_zone = true;
1518
Joonsoo Kim1a167182015-09-08 15:03:59 -07001519 cc->last_migrated_pfn = 0;
Mel Gorman748446b2010-05-24 14:32:27 -07001520
Joonsoo Kim16c4a092015-02-11 15:27:01 -08001521 trace_mm_compaction_begin(start_pfn, cc->migrate_pfn,
1522 cc->free_pfn, end_pfn, sync);
Mel Gorman0eb927c2014-01-21 15:51:05 -08001523
Mel Gorman748446b2010-05-24 14:32:27 -07001524 migrate_prep_local();
1525
David Rientjes6d7ce552014-10-09 15:27:27 -07001526 while ((ret = compact_finished(zone, cc, migratetype)) ==
1527 COMPACT_CONTINUE) {
Minchan Kim9d502c12011-03-22 16:30:39 -07001528 int err;
Mel Gorman748446b2010-05-24 14:32:27 -07001529
Mel Gormanf9e35b32011-06-15 15:08:52 -07001530 switch (isolate_migratepages(zone, cc)) {
1531 case ISOLATE_ABORT:
Vlastimil Babka2d1e1042015-11-05 18:48:02 -08001532 ret = COMPACT_CONTENDED;
Rafael Aquini5733c7d2012-12-11 16:02:47 -08001533 putback_movable_pages(&cc->migratepages);
Shaohua Lie64c5232012-10-08 16:32:27 -07001534 cc->nr_migratepages = 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -07001535 goto out;
1536 case ISOLATE_NONE:
Vlastimil Babkafdaf7f52014-12-10 15:43:34 -08001537 /*
1538 * We haven't isolated and migrated anything, but
1539 * there might still be unflushed migrations from
1540 * previous cc->order aligned block.
1541 */
1542 goto check_drain;
Mel Gormanf9e35b32011-06-15 15:08:52 -07001543 case ISOLATE_SUCCESS:
1544 ;
1545 }
Mel Gorman748446b2010-05-24 14:32:27 -07001546
David Rientjesd53aea32014-06-04 16:08:26 -07001547 err = migrate_pages(&cc->migratepages, compaction_alloc,
David Rientjese0b9dae2014-06-04 16:08:28 -07001548 compaction_free, (unsigned long)cc, cc->mode,
Mel Gorman7b2a2d42012-10-19 14:07:31 +01001549 MR_COMPACTION);
Mel Gorman748446b2010-05-24 14:32:27 -07001550
Vlastimil Babkaf8c93012014-06-04 16:08:32 -07001551 trace_mm_compaction_migratepages(cc->nr_migratepages, err,
1552 &cc->migratepages);
Mel Gorman748446b2010-05-24 14:32:27 -07001553
Vlastimil Babkaf8c93012014-06-04 16:08:32 -07001554 /* All pages were either migrated or will be released */
1555 cc->nr_migratepages = 0;
Minchan Kim9d502c12011-03-22 16:30:39 -07001556 if (err) {
Rafael Aquini5733c7d2012-12-11 16:02:47 -08001557 putback_movable_pages(&cc->migratepages);
Vlastimil Babka7ed695e2014-01-21 15:51:09 -08001558 /*
1559 * migrate_pages() may return -ENOMEM when scanners meet
1560 * and we want compact_finished() to detect it
1561 */
Vlastimil Babkaf2849aa2015-09-08 15:02:36 -07001562 if (err == -ENOMEM && !compact_scanners_met(cc)) {
Vlastimil Babka2d1e1042015-11-05 18:48:02 -08001563 ret = COMPACT_CONTENDED;
David Rientjes4bf2bba2012-07-11 14:02:13 -07001564 goto out;
1565 }
Vlastimil Babkafdd048e2016-05-19 17:11:55 -07001566 /*
1567 * We failed to migrate at least one page in the current
1568 * order-aligned block, so skip the rest of it.
1569 */
1570 if (cc->direct_compaction &&
1571 (cc->mode == MIGRATE_ASYNC)) {
1572 cc->migrate_pfn = block_end_pfn(
1573 cc->migrate_pfn - 1, cc->order);
1574 /* Draining pcplists is useless in this case */
1575 cc->last_migrated_pfn = 0;
1576
1577 }
Mel Gorman748446b2010-05-24 14:32:27 -07001578 }
Vlastimil Babkafdaf7f52014-12-10 15:43:34 -08001579
Vlastimil Babkafdaf7f52014-12-10 15:43:34 -08001580check_drain:
1581 /*
1582 * Has the migration scanner moved away from the previous
1583 * cc->order aligned block where we migrated from? If yes,
1584 * flush the pages that were freed, so that they can merge and
1585 * compact_finished() can detect immediately if allocation
1586 * would succeed.
1587 */
Joonsoo Kim1a167182015-09-08 15:03:59 -07001588 if (cc->order > 0 && cc->last_migrated_pfn) {
Vlastimil Babkafdaf7f52014-12-10 15:43:34 -08001589 int cpu;
1590 unsigned long current_block_start =
Vlastimil Babka06b66402016-05-19 17:11:48 -07001591 block_start_pfn(cc->migrate_pfn, cc->order);
Vlastimil Babkafdaf7f52014-12-10 15:43:34 -08001592
Joonsoo Kim1a167182015-09-08 15:03:59 -07001593 if (cc->last_migrated_pfn < current_block_start) {
Vlastimil Babkafdaf7f52014-12-10 15:43:34 -08001594 cpu = get_cpu();
1595 lru_add_drain_cpu(cpu);
1596 drain_local_pages(zone);
1597 put_cpu();
1598 /* No more flushing until we migrate again */
Joonsoo Kim1a167182015-09-08 15:03:59 -07001599 cc->last_migrated_pfn = 0;
Vlastimil Babkafdaf7f52014-12-10 15:43:34 -08001600 }
1601 }
1602
Mel Gorman748446b2010-05-24 14:32:27 -07001603 }
1604
Mel Gormanf9e35b32011-06-15 15:08:52 -07001605out:
Vlastimil Babka6bace092014-12-10 15:43:31 -08001606 /*
1607 * Release free pages and update where the free scanner should restart,
1608 * so we don't leave any returned pages behind in the next attempt.
1609 */
1610 if (cc->nr_freepages > 0) {
1611 unsigned long free_pfn = release_freepages(&cc->freepages);
1612
1613 cc->nr_freepages = 0;
1614 VM_BUG_ON(free_pfn == 0);
1615 /* The cached pfn is always the first in a pageblock */
Vlastimil Babka06b66402016-05-19 17:11:48 -07001616 free_pfn = pageblock_start_pfn(free_pfn);
Vlastimil Babka6bace092014-12-10 15:43:31 -08001617 /*
1618 * Only go back, not forward. The cached pfn might have been
1619 * already reset to zone end in compact_finished()
1620 */
1621 if (free_pfn > zone->compact_cached_free_pfn)
1622 zone->compact_cached_free_pfn = free_pfn;
1623 }
Mel Gorman748446b2010-05-24 14:32:27 -07001624
Joonsoo Kim16c4a092015-02-11 15:27:01 -08001625 trace_mm_compaction_end(start_pfn, cc->migrate_pfn,
1626 cc->free_pfn, end_pfn, sync, ret);
Mel Gorman0eb927c2014-01-21 15:51:05 -08001627
Vlastimil Babka2d1e1042015-11-05 18:48:02 -08001628 if (ret == COMPACT_CONTENDED)
1629 ret = COMPACT_PARTIAL;
1630
Mel Gorman748446b2010-05-24 14:32:27 -07001631 return ret;
1632}
Mel Gorman76ab0f52010-05-24 14:32:28 -07001633
Michal Hockoea7ab982016-05-20 16:56:38 -07001634static enum compact_result compact_zone_order(struct zone *zone, int order,
Vlastimil Babkaebff3982014-12-10 15:43:22 -08001635 gfp_t gfp_mask, enum migrate_mode mode, int *contended,
Mel Gormanc6038442016-05-19 17:13:38 -07001636 unsigned int alloc_flags, int classzone_idx)
Mel Gorman56de7262010-05-24 14:32:30 -07001637{
Michal Hockoea7ab982016-05-20 16:56:38 -07001638 enum compact_result ret;
Mel Gorman56de7262010-05-24 14:32:30 -07001639 struct compact_control cc = {
1640 .nr_freepages = 0,
1641 .nr_migratepages = 0,
1642 .order = order,
David Rientjes6d7ce552014-10-09 15:27:27 -07001643 .gfp_mask = gfp_mask,
Mel Gorman56de7262010-05-24 14:32:30 -07001644 .zone = zone,
David Rientjese0b9dae2014-06-04 16:08:28 -07001645 .mode = mode,
Vlastimil Babkaebff3982014-12-10 15:43:22 -08001646 .alloc_flags = alloc_flags,
1647 .classzone_idx = classzone_idx,
Vlastimil Babkaaccf6242016-03-17 14:18:15 -07001648 .direct_compaction = true,
Mel Gorman56de7262010-05-24 14:32:30 -07001649 };
1650 INIT_LIST_HEAD(&cc.freepages);
1651 INIT_LIST_HEAD(&cc.migratepages);
1652
Shaohua Lie64c5232012-10-08 16:32:27 -07001653 ret = compact_zone(zone, &cc);
1654
1655 VM_BUG_ON(!list_empty(&cc.freepages));
1656 VM_BUG_ON(!list_empty(&cc.migratepages));
1657
1658 *contended = cc.contended;
1659 return ret;
Mel Gorman56de7262010-05-24 14:32:30 -07001660}
1661
Mel Gorman5e771902010-05-24 14:32:31 -07001662int sysctl_extfrag_threshold = 500;
1663
Mel Gorman56de7262010-05-24 14:32:30 -07001664/**
1665 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
Mel Gorman56de7262010-05-24 14:32:30 -07001666 * @gfp_mask: The GFP mask of the current allocation
Vlastimil Babka1a6d53a2015-02-11 15:25:44 -08001667 * @order: The order of the current allocation
1668 * @alloc_flags: The allocation flags of the current allocation
1669 * @ac: The context of current allocation
David Rientjese0b9dae2014-06-04 16:08:28 -07001670 * @mode: The migration mode for async, sync light, or sync migration
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001671 * @contended: Return value that determines if compaction was aborted due to
1672 * need_resched() or lock contention
Mel Gorman56de7262010-05-24 14:32:30 -07001673 *
1674 * This is the main entry point for direct page compaction.
1675 */
Michal Hockoea7ab982016-05-20 16:56:38 -07001676enum compact_result try_to_compact_pages(gfp_t gfp_mask, unsigned int order,
Mel Gormanc6038442016-05-19 17:13:38 -07001677 unsigned int alloc_flags, const struct alloc_context *ac,
1678 enum migrate_mode mode, int *contended)
Mel Gorman56de7262010-05-24 14:32:30 -07001679{
Mel Gorman56de7262010-05-24 14:32:30 -07001680 int may_enter_fs = gfp_mask & __GFP_FS;
1681 int may_perform_io = gfp_mask & __GFP_IO;
Mel Gorman56de7262010-05-24 14:32:30 -07001682 struct zoneref *z;
1683 struct zone *zone;
Michal Hocko1d4746d2016-05-20 16:56:44 -07001684 enum compact_result rc = COMPACT_SKIPPED;
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001685 int all_zones_contended = COMPACT_CONTENDED_LOCK; /* init for &= op */
1686
1687 *contended = COMPACT_CONTENDED_NONE;
Mel Gorman56de7262010-05-24 14:32:30 -07001688
Mel Gorman4ffb6332012-10-08 16:29:09 -07001689 /* Check if the GFP flags allow compaction */
Andrea Arcangelic5a73c32011-01-13 15:47:11 -08001690 if (!order || !may_enter_fs || !may_perform_io)
Vlastimil Babka53853e22014-10-09 15:27:02 -07001691 return COMPACT_SKIPPED;
Mel Gorman56de7262010-05-24 14:32:30 -07001692
Joonsoo Kim837d0262015-02-11 15:27:06 -08001693 trace_mm_compaction_try_to_compact_pages(order, gfp_mask, mode);
1694
Mel Gorman56de7262010-05-24 14:32:30 -07001695 /* Compact each zone in the list */
Vlastimil Babka1a6d53a2015-02-11 15:25:44 -08001696 for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx,
1697 ac->nodemask) {
Michal Hockoea7ab982016-05-20 16:56:38 -07001698 enum compact_result status;
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001699 int zone_contended;
Mel Gorman56de7262010-05-24 14:32:30 -07001700
Michal Hocko1d4746d2016-05-20 16:56:44 -07001701 if (compaction_deferred(zone, order)) {
1702 rc = max_t(enum compact_result, COMPACT_DEFERRED, rc);
Vlastimil Babka53853e22014-10-09 15:27:02 -07001703 continue;
Michal Hocko1d4746d2016-05-20 16:56:44 -07001704 }
Vlastimil Babka53853e22014-10-09 15:27:02 -07001705
David Rientjese0b9dae2014-06-04 16:08:28 -07001706 status = compact_zone_order(zone, order, gfp_mask, mode,
Vlastimil Babka1a6d53a2015-02-11 15:25:44 -08001707 &zone_contended, alloc_flags,
Mel Gorman93ea9962016-05-19 17:14:13 -07001708 ac_classzone_idx(ac));
Mel Gorman56de7262010-05-24 14:32:30 -07001709 rc = max(status, rc);
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001710 /*
1711 * It takes at least one zone that wasn't lock contended
1712 * to clear all_zones_contended.
1713 */
1714 all_zones_contended &= zone_contended;
Mel Gorman56de7262010-05-24 14:32:30 -07001715
Mel Gorman3e7d3442011-01-13 15:45:56 -08001716 /* If a normal allocation would succeed, stop compacting */
Vlastimil Babkaebff3982014-12-10 15:43:22 -08001717 if (zone_watermark_ok(zone, order, low_wmark_pages(zone),
Mel Gorman93ea9962016-05-19 17:14:13 -07001718 ac_classzone_idx(ac), alloc_flags)) {
Vlastimil Babka53853e22014-10-09 15:27:02 -07001719 /*
1720 * We think the allocation will succeed in this zone,
1721 * but it is not certain, hence the false. The caller
1722 * will repeat this with true if allocation indeed
1723 * succeeds in this zone.
1724 */
1725 compaction_defer_reset(zone, order, false);
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001726 /*
1727 * It is possible that async compaction aborted due to
1728 * need_resched() and the watermarks were ok thanks to
1729 * somebody else freeing memory. The allocation can
1730 * however still fail so we better signal the
1731 * need_resched() contention anyway (this will not
1732 * prevent the allocation attempt).
1733 */
1734 if (zone_contended == COMPACT_CONTENDED_SCHED)
1735 *contended = COMPACT_CONTENDED_SCHED;
1736
1737 goto break_loop;
1738 }
1739
Michal Hockoc8f7de02016-05-20 16:56:47 -07001740 if (mode != MIGRATE_ASYNC && (status == COMPACT_COMPLETE ||
1741 status == COMPACT_PARTIAL_SKIPPED)) {
Vlastimil Babka53853e22014-10-09 15:27:02 -07001742 /*
1743 * We think that allocation won't succeed in this zone
1744 * so we defer compaction there. If it ends up
1745 * succeeding after all, it will be reset.
1746 */
1747 defer_compaction(zone, order);
1748 }
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001749
1750 /*
1751 * We might have stopped compacting due to need_resched() in
1752 * async compaction, or due to a fatal signal detected. In that
1753 * case do not try further zones and signal need_resched()
1754 * contention.
1755 */
1756 if ((zone_contended == COMPACT_CONTENDED_SCHED)
1757 || fatal_signal_pending(current)) {
1758 *contended = COMPACT_CONTENDED_SCHED;
1759 goto break_loop;
1760 }
1761
1762 continue;
1763break_loop:
1764 /*
1765 * We might not have tried all the zones, so be conservative
1766 * and assume they are not all lock contended.
1767 */
1768 all_zones_contended = 0;
1769 break;
Mel Gorman56de7262010-05-24 14:32:30 -07001770 }
1771
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001772 /*
1773 * If at least one zone wasn't deferred or skipped, we report if all
1774 * zones that were tried were lock contended.
1775 */
Michal Hocko1d4746d2016-05-20 16:56:44 -07001776 if (rc > COMPACT_INACTIVE && all_zones_contended)
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001777 *contended = COMPACT_CONTENDED_LOCK;
1778
Mel Gorman56de7262010-05-24 14:32:30 -07001779 return rc;
1780}
1781
1782
Mel Gorman76ab0f52010-05-24 14:32:28 -07001783/* Compact all zones within a node */
Andrew Morton7103f162013-02-22 16:32:33 -08001784static void __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001785{
1786 int zoneid;
Mel Gorman76ab0f52010-05-24 14:32:28 -07001787 struct zone *zone;
1788
Mel Gorman76ab0f52010-05-24 14:32:28 -07001789 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
Mel Gorman76ab0f52010-05-24 14:32:28 -07001790
1791 zone = &pgdat->node_zones[zoneid];
1792 if (!populated_zone(zone))
1793 continue;
1794
Rik van Riel7be62de2012-03-21 16:33:52 -07001795 cc->nr_freepages = 0;
1796 cc->nr_migratepages = 0;
1797 cc->zone = zone;
1798 INIT_LIST_HEAD(&cc->freepages);
1799 INIT_LIST_HEAD(&cc->migratepages);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001800
Gioh Kim195b0c62015-04-15 16:13:33 -07001801 /*
1802 * When called via /proc/sys/vm/compact_memory
1803 * this makes sure we compact the whole zone regardless of
1804 * cached scanner positions.
1805 */
Yaowei Bai21c527a2015-11-05 18:47:20 -08001806 if (is_via_compact_memory(cc->order))
Gioh Kim195b0c62015-04-15 16:13:33 -07001807 __reset_isolation_suitable(zone);
1808
Yaowei Bai21c527a2015-11-05 18:47:20 -08001809 if (is_via_compact_memory(cc->order) ||
1810 !compaction_deferred(zone, cc->order))
Rik van Riel7be62de2012-03-21 16:33:52 -07001811 compact_zone(zone, cc);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001812
Rik van Riel7be62de2012-03-21 16:33:52 -07001813 VM_BUG_ON(!list_empty(&cc->freepages));
1814 VM_BUG_ON(!list_empty(&cc->migratepages));
Joonsoo Kim75469342016-01-14 15:20:48 -08001815
1816 if (is_via_compact_memory(cc->order))
1817 continue;
1818
1819 if (zone_watermark_ok(zone, cc->order,
1820 low_wmark_pages(zone), 0, 0))
1821 compaction_defer_reset(zone, cc->order, false);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001822 }
Mel Gorman76ab0f52010-05-24 14:32:28 -07001823}
1824
Andrew Morton7103f162013-02-22 16:32:33 -08001825void compact_pgdat(pg_data_t *pgdat, int order)
Rik van Riel7be62de2012-03-21 16:33:52 -07001826{
1827 struct compact_control cc = {
1828 .order = order,
David Rientjese0b9dae2014-06-04 16:08:28 -07001829 .mode = MIGRATE_ASYNC,
Rik van Riel7be62de2012-03-21 16:33:52 -07001830 };
1831
Mel Gorman3a7200a2013-09-11 14:22:19 -07001832 if (!order)
1833 return;
1834
Andrew Morton7103f162013-02-22 16:32:33 -08001835 __compact_pgdat(pgdat, &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -07001836}
1837
Andrew Morton7103f162013-02-22 16:32:33 -08001838static void compact_node(int nid)
Rik van Riel7be62de2012-03-21 16:33:52 -07001839{
Rik van Riel7be62de2012-03-21 16:33:52 -07001840 struct compact_control cc = {
1841 .order = -1,
David Rientjese0b9dae2014-06-04 16:08:28 -07001842 .mode = MIGRATE_SYNC,
David Rientjes91ca9182014-04-03 14:47:23 -07001843 .ignore_skip_hint = true,
Rik van Riel7be62de2012-03-21 16:33:52 -07001844 };
1845
Andrew Morton7103f162013-02-22 16:32:33 -08001846 __compact_pgdat(NODE_DATA(nid), &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -07001847}
1848
Mel Gorman76ab0f52010-05-24 14:32:28 -07001849/* Compact all nodes in the system */
Jason Liu7964c062013-01-11 14:31:47 -08001850static void compact_nodes(void)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001851{
1852 int nid;
1853
Hugh Dickins8575ec22012-03-21 16:33:53 -07001854 /* Flush pending updates to the LRU lists */
1855 lru_add_drain_all();
1856
Mel Gorman76ab0f52010-05-24 14:32:28 -07001857 for_each_online_node(nid)
1858 compact_node(nid);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001859}
1860
1861/* The written value is actually unused, all memory is compacted */
1862int sysctl_compact_memory;
1863
Yaowei Baifec4eb22016-01-14 15:20:09 -08001864/*
1865 * This is the entry point for compacting all nodes via
1866 * /proc/sys/vm/compact_memory
1867 */
Mel Gorman76ab0f52010-05-24 14:32:28 -07001868int sysctl_compaction_handler(struct ctl_table *table, int write,
1869 void __user *buffer, size_t *length, loff_t *ppos)
1870{
1871 if (write)
Jason Liu7964c062013-01-11 14:31:47 -08001872 compact_nodes();
Mel Gorman76ab0f52010-05-24 14:32:28 -07001873
1874 return 0;
1875}
Mel Gormaned4a6d72010-05-24 14:32:29 -07001876
Mel Gorman5e771902010-05-24 14:32:31 -07001877int sysctl_extfrag_handler(struct ctl_table *table, int write,
1878 void __user *buffer, size_t *length, loff_t *ppos)
1879{
1880 proc_dointvec_minmax(table, write, buffer, length, ppos);
1881
1882 return 0;
1883}
1884
Mel Gormaned4a6d72010-05-24 14:32:29 -07001885#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
Rashika Kheria74e77fb2014-04-03 14:48:01 -07001886static ssize_t sysfs_compact_node(struct device *dev,
Kay Sievers10fbcf42011-12-21 14:48:43 -08001887 struct device_attribute *attr,
Mel Gormaned4a6d72010-05-24 14:32:29 -07001888 const char *buf, size_t count)
1889{
Hugh Dickins8575ec22012-03-21 16:33:53 -07001890 int nid = dev->id;
1891
1892 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
1893 /* Flush pending updates to the LRU lists */
1894 lru_add_drain_all();
1895
1896 compact_node(nid);
1897 }
Mel Gormaned4a6d72010-05-24 14:32:29 -07001898
1899 return count;
1900}
Kay Sievers10fbcf42011-12-21 14:48:43 -08001901static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001902
1903int compaction_register_node(struct node *node)
1904{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001905 return device_create_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001906}
1907
1908void compaction_unregister_node(struct node *node)
1909{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001910 return device_remove_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001911}
1912#endif /* CONFIG_SYSFS && CONFIG_NUMA */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001913
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001914static inline bool kcompactd_work_requested(pg_data_t *pgdat)
1915{
Vlastimil Babka172400c2016-05-05 16:22:32 -07001916 return pgdat->kcompactd_max_order > 0 || kthread_should_stop();
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001917}
1918
1919static bool kcompactd_node_suitable(pg_data_t *pgdat)
1920{
1921 int zoneid;
1922 struct zone *zone;
1923 enum zone_type classzone_idx = pgdat->kcompactd_classzone_idx;
1924
Chen Feng6cd9dc32016-05-20 16:59:02 -07001925 for (zoneid = 0; zoneid <= classzone_idx; zoneid++) {
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001926 zone = &pgdat->node_zones[zoneid];
1927
1928 if (!populated_zone(zone))
1929 continue;
1930
1931 if (compaction_suitable(zone, pgdat->kcompactd_max_order, 0,
1932 classzone_idx) == COMPACT_CONTINUE)
1933 return true;
1934 }
1935
1936 return false;
1937}
1938
1939static void kcompactd_do_work(pg_data_t *pgdat)
1940{
1941 /*
1942 * With no special task, compact all zones so that a page of requested
1943 * order is allocatable.
1944 */
1945 int zoneid;
1946 struct zone *zone;
1947 struct compact_control cc = {
1948 .order = pgdat->kcompactd_max_order,
1949 .classzone_idx = pgdat->kcompactd_classzone_idx,
1950 .mode = MIGRATE_SYNC_LIGHT,
1951 .ignore_skip_hint = true,
1952
1953 };
1954 bool success = false;
1955
1956 trace_mm_compaction_kcompactd_wake(pgdat->node_id, cc.order,
1957 cc.classzone_idx);
1958 count_vm_event(KCOMPACTD_WAKE);
1959
Chen Feng6cd9dc32016-05-20 16:59:02 -07001960 for (zoneid = 0; zoneid <= cc.classzone_idx; zoneid++) {
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001961 int status;
1962
1963 zone = &pgdat->node_zones[zoneid];
1964 if (!populated_zone(zone))
1965 continue;
1966
1967 if (compaction_deferred(zone, cc.order))
1968 continue;
1969
1970 if (compaction_suitable(zone, cc.order, 0, zoneid) !=
1971 COMPACT_CONTINUE)
1972 continue;
1973
1974 cc.nr_freepages = 0;
1975 cc.nr_migratepages = 0;
1976 cc.zone = zone;
1977 INIT_LIST_HEAD(&cc.freepages);
1978 INIT_LIST_HEAD(&cc.migratepages);
1979
Vlastimil Babka172400c2016-05-05 16:22:32 -07001980 if (kthread_should_stop())
1981 return;
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001982 status = compact_zone(zone, &cc);
1983
1984 if (zone_watermark_ok(zone, cc.order, low_wmark_pages(zone),
1985 cc.classzone_idx, 0)) {
1986 success = true;
1987 compaction_defer_reset(zone, cc.order, false);
Michal Hockoc8f7de02016-05-20 16:56:47 -07001988 } else if (status == COMPACT_PARTIAL_SKIPPED || status == COMPACT_COMPLETE) {
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001989 /*
1990 * We use sync migration mode here, so we defer like
1991 * sync direct compaction does.
1992 */
1993 defer_compaction(zone, cc.order);
1994 }
1995
1996 VM_BUG_ON(!list_empty(&cc.freepages));
1997 VM_BUG_ON(!list_empty(&cc.migratepages));
1998 }
1999
2000 /*
2001 * Regardless of success, we are done until woken up next. But remember
2002 * the requested order/classzone_idx in case it was higher/tighter than
2003 * our current ones
2004 */
2005 if (pgdat->kcompactd_max_order <= cc.order)
2006 pgdat->kcompactd_max_order = 0;
2007 if (pgdat->kcompactd_classzone_idx >= cc.classzone_idx)
2008 pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1;
2009}
2010
2011void wakeup_kcompactd(pg_data_t *pgdat, int order, int classzone_idx)
2012{
2013 if (!order)
2014 return;
2015
2016 if (pgdat->kcompactd_max_order < order)
2017 pgdat->kcompactd_max_order = order;
2018
2019 if (pgdat->kcompactd_classzone_idx > classzone_idx)
2020 pgdat->kcompactd_classzone_idx = classzone_idx;
2021
2022 if (!waitqueue_active(&pgdat->kcompactd_wait))
2023 return;
2024
2025 if (!kcompactd_node_suitable(pgdat))
2026 return;
2027
2028 trace_mm_compaction_wakeup_kcompactd(pgdat->node_id, order,
2029 classzone_idx);
2030 wake_up_interruptible(&pgdat->kcompactd_wait);
2031}
2032
2033/*
2034 * The background compaction daemon, started as a kernel thread
2035 * from the init process.
2036 */
2037static int kcompactd(void *p)
2038{
2039 pg_data_t *pgdat = (pg_data_t*)p;
2040 struct task_struct *tsk = current;
2041
2042 const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
2043
2044 if (!cpumask_empty(cpumask))
2045 set_cpus_allowed_ptr(tsk, cpumask);
2046
2047 set_freezable();
2048
2049 pgdat->kcompactd_max_order = 0;
2050 pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1;
2051
2052 while (!kthread_should_stop()) {
2053 trace_mm_compaction_kcompactd_sleep(pgdat->node_id);
2054 wait_event_freezable(pgdat->kcompactd_wait,
2055 kcompactd_work_requested(pgdat));
2056
2057 kcompactd_do_work(pgdat);
2058 }
2059
2060 return 0;
2061}
2062
2063/*
2064 * This kcompactd start function will be called by init and node-hot-add.
2065 * On node-hot-add, kcompactd will moved to proper cpus if cpus are hot-added.
2066 */
2067int kcompactd_run(int nid)
2068{
2069 pg_data_t *pgdat = NODE_DATA(nid);
2070 int ret = 0;
2071
2072 if (pgdat->kcompactd)
2073 return 0;
2074
2075 pgdat->kcompactd = kthread_run(kcompactd, pgdat, "kcompactd%d", nid);
2076 if (IS_ERR(pgdat->kcompactd)) {
2077 pr_err("Failed to start kcompactd on node %d\n", nid);
2078 ret = PTR_ERR(pgdat->kcompactd);
2079 pgdat->kcompactd = NULL;
2080 }
2081 return ret;
2082}
2083
2084/*
2085 * Called by memory hotplug when all memory in a node is offlined. Caller must
2086 * hold mem_hotplug_begin/end().
2087 */
2088void kcompactd_stop(int nid)
2089{
2090 struct task_struct *kcompactd = NODE_DATA(nid)->kcompactd;
2091
2092 if (kcompactd) {
2093 kthread_stop(kcompactd);
2094 NODE_DATA(nid)->kcompactd = NULL;
2095 }
2096}
2097
2098/*
2099 * It's optimal to keep kcompactd on the same CPUs as their memory, but
2100 * not required for correctness. So if the last cpu in a node goes
2101 * away, we get changed to run anywhere: as the first one comes back,
2102 * restore their cpu bindings.
2103 */
2104static int cpu_callback(struct notifier_block *nfb, unsigned long action,
2105 void *hcpu)
2106{
2107 int nid;
2108
2109 if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) {
2110 for_each_node_state(nid, N_MEMORY) {
2111 pg_data_t *pgdat = NODE_DATA(nid);
2112 const struct cpumask *mask;
2113
2114 mask = cpumask_of_node(pgdat->node_id);
2115
2116 if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
2117 /* One of our CPUs online: restore mask */
2118 set_cpus_allowed_ptr(pgdat->kcompactd, mask);
2119 }
2120 }
2121 return NOTIFY_OK;
2122}
2123
2124static int __init kcompactd_init(void)
2125{
2126 int nid;
2127
2128 for_each_node_state(nid, N_MEMORY)
2129 kcompactd_run(nid);
2130 hotcpu_notifier(cpu_callback, 0);
2131 return 0;
2132}
2133subsys_initcall(kcompactd_init)
2134
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01002135#endif /* CONFIG_COMPACTION */