blob: fe95d8d021c30fe067b47ae7544d3070aac47ac8 [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>
Rafael Aquinibf6bddf2012-12-11 16:02:42 -080018#include <linux/balloon_compaction.h>
Minchan Kim194159f2013-02-22 16:33:58 -080019#include <linux/page-isolation.h>
Andrey Ryabininb8c73fc2015-02-13 14:39:28 -080020#include <linux/kasan.h>
Vlastimil Babka698b1b32016-03-17 14:18:08 -070021#include <linux/kthread.h>
22#include <linux/freezer.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{
68 struct page *page;
69
70 list_for_each_entry(page, list, lru) {
71 arch_alloc_page(page, 0);
72 kernel_map_pages(page, 1, 1);
Andrey Ryabininb8c73fc2015-02-13 14:39:28 -080073 kasan_alloc_pages(page, 0);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +010074 }
75}
76
Michal Nazarewicz47118af2011-12-29 13:09:50 +010077static inline bool migrate_async_suitable(int migratetype)
78{
79 return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
80}
81
Mel Gormanbb13ffe2012-10-08 16:32:41 -070082#ifdef CONFIG_COMPACTION
Joonsoo Kim24e27162015-02-11 15:27:09 -080083
Minchan Kimbda807d2016-07-26 15:23:05 -070084int PageMovable(struct page *page)
85{
86 struct address_space *mapping;
87
88 VM_BUG_ON_PAGE(!PageLocked(page), page);
89 if (!__PageMovable(page))
90 return 0;
91
92 mapping = page_mapping(page);
93 if (mapping && mapping->a_ops && mapping->a_ops->isolate_page)
94 return 1;
95
96 return 0;
97}
98EXPORT_SYMBOL(PageMovable);
99
100void __SetPageMovable(struct page *page, struct address_space *mapping)
101{
102 VM_BUG_ON_PAGE(!PageLocked(page), page);
103 VM_BUG_ON_PAGE((unsigned long)mapping & PAGE_MAPPING_MOVABLE, page);
104 page->mapping = (void *)((unsigned long)mapping | PAGE_MAPPING_MOVABLE);
105}
106EXPORT_SYMBOL(__SetPageMovable);
107
108void __ClearPageMovable(struct page *page)
109{
110 VM_BUG_ON_PAGE(!PageLocked(page), page);
111 VM_BUG_ON_PAGE(!PageMovable(page), page);
112 /*
113 * Clear registered address_space val with keeping PAGE_MAPPING_MOVABLE
114 * flag so that VM can catch up released page by driver after isolation.
115 * With it, VM migration doesn't try to put it back.
116 */
117 page->mapping = (void *)((unsigned long)page->mapping &
118 PAGE_MAPPING_MOVABLE);
119}
120EXPORT_SYMBOL(__ClearPageMovable);
121
Joonsoo Kim24e27162015-02-11 15:27:09 -0800122/* Do not skip compaction more than 64 times */
123#define COMPACT_MAX_DEFER_SHIFT 6
124
125/*
126 * Compaction is deferred when compaction fails to result in a page
127 * allocation success. 1 << compact_defer_limit compactions are skipped up
128 * to a limit of 1 << COMPACT_MAX_DEFER_SHIFT
129 */
130void defer_compaction(struct zone *zone, int order)
131{
132 zone->compact_considered = 0;
133 zone->compact_defer_shift++;
134
135 if (order < zone->compact_order_failed)
136 zone->compact_order_failed = order;
137
138 if (zone->compact_defer_shift > COMPACT_MAX_DEFER_SHIFT)
139 zone->compact_defer_shift = COMPACT_MAX_DEFER_SHIFT;
140
141 trace_mm_compaction_defer_compaction(zone, order);
142}
143
144/* Returns true if compaction should be skipped this time */
145bool compaction_deferred(struct zone *zone, int order)
146{
147 unsigned long defer_limit = 1UL << zone->compact_defer_shift;
148
149 if (order < zone->compact_order_failed)
150 return false;
151
152 /* Avoid possible overflow */
153 if (++zone->compact_considered > defer_limit)
154 zone->compact_considered = defer_limit;
155
156 if (zone->compact_considered >= defer_limit)
157 return false;
158
159 trace_mm_compaction_deferred(zone, order);
160
161 return true;
162}
163
164/*
165 * Update defer tracking counters after successful compaction of given order,
166 * which means an allocation either succeeded (alloc_success == true) or is
167 * expected to succeed.
168 */
169void compaction_defer_reset(struct zone *zone, int order,
170 bool alloc_success)
171{
172 if (alloc_success) {
173 zone->compact_considered = 0;
174 zone->compact_defer_shift = 0;
175 }
176 if (order >= zone->compact_order_failed)
177 zone->compact_order_failed = order + 1;
178
179 trace_mm_compaction_defer_reset(zone, order);
180}
181
182/* Returns true if restarting compaction after many failures */
183bool compaction_restarting(struct zone *zone, int order)
184{
185 if (order < zone->compact_order_failed)
186 return false;
187
188 return zone->compact_defer_shift == COMPACT_MAX_DEFER_SHIFT &&
189 zone->compact_considered >= 1UL << zone->compact_defer_shift;
190}
191
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700192/* Returns true if the pageblock should be scanned for pages to isolate. */
193static inline bool isolation_suitable(struct compact_control *cc,
194 struct page *page)
195{
196 if (cc->ignore_skip_hint)
197 return true;
198
199 return !get_pageblock_skip(page);
200}
201
Vlastimil Babka02333642015-09-08 15:02:42 -0700202static void reset_cached_positions(struct zone *zone)
203{
204 zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn;
205 zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn;
Joonsoo Kim623446e2016-03-15 14:57:45 -0700206 zone->compact_cached_free_pfn =
Vlastimil Babka06b66402016-05-19 17:11:48 -0700207 pageblock_start_pfn(zone_end_pfn(zone) - 1);
Vlastimil Babka02333642015-09-08 15:02:42 -0700208}
209
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700210/*
211 * This function is called to clear all cached information on pageblocks that
212 * should be skipped for page isolation when the migrate and free page scanner
213 * meet.
214 */
Mel Gorman62997022012-10-08 16:32:47 -0700215static void __reset_isolation_suitable(struct zone *zone)
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700216{
217 unsigned long start_pfn = zone->zone_start_pfn;
Cody P Schafer108bcc92013-02-22 16:35:23 -0800218 unsigned long end_pfn = zone_end_pfn(zone);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700219 unsigned long pfn;
220
Mel Gorman62997022012-10-08 16:32:47 -0700221 zone->compact_blockskip_flush = false;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700222
223 /* Walk the zone and mark every pageblock as suitable for isolation */
224 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
225 struct page *page;
226
227 cond_resched();
228
229 if (!pfn_valid(pfn))
230 continue;
231
232 page = pfn_to_page(pfn);
233 if (zone != page_zone(page))
234 continue;
235
236 clear_pageblock_skip(page);
237 }
Vlastimil Babka02333642015-09-08 15:02:42 -0700238
239 reset_cached_positions(zone);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700240}
241
Mel Gorman62997022012-10-08 16:32:47 -0700242void reset_isolation_suitable(pg_data_t *pgdat)
243{
244 int zoneid;
245
246 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
247 struct zone *zone = &pgdat->node_zones[zoneid];
248 if (!populated_zone(zone))
249 continue;
250
251 /* Only flush if a full compaction finished recently */
252 if (zone->compact_blockskip_flush)
253 __reset_isolation_suitable(zone);
254 }
255}
256
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700257/*
258 * If no pages were isolated then mark this pageblock to be skipped in the
Mel Gorman62997022012-10-08 16:32:47 -0700259 * future. The information is later cleared by __reset_isolation_suitable().
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700260 */
Mel Gormanc89511a2012-10-08 16:32:45 -0700261static void update_pageblock_skip(struct compact_control *cc,
262 struct page *page, unsigned long nr_isolated,
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700263 bool migrate_scanner)
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700264{
Mel Gormanc89511a2012-10-08 16:32:45 -0700265 struct zone *zone = cc->zone;
David Rientjes35979ef2014-06-04 16:08:27 -0700266 unsigned long pfn;
Joonsoo Kim6815bf32013-12-18 17:08:52 -0800267
268 if (cc->ignore_skip_hint)
269 return;
270
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700271 if (!page)
272 return;
273
David Rientjes35979ef2014-06-04 16:08:27 -0700274 if (nr_isolated)
275 return;
276
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700277 set_pageblock_skip(page);
Mel Gormanc89511a2012-10-08 16:32:45 -0700278
David Rientjes35979ef2014-06-04 16:08:27 -0700279 pfn = page_to_pfn(page);
280
281 /* Update where async and sync compaction should restart */
282 if (migrate_scanner) {
David Rientjes35979ef2014-06-04 16:08:27 -0700283 if (pfn > zone->compact_cached_migrate_pfn[0])
284 zone->compact_cached_migrate_pfn[0] = pfn;
David Rientjese0b9dae2014-06-04 16:08:28 -0700285 if (cc->mode != MIGRATE_ASYNC &&
286 pfn > zone->compact_cached_migrate_pfn[1])
David Rientjes35979ef2014-06-04 16:08:27 -0700287 zone->compact_cached_migrate_pfn[1] = pfn;
288 } else {
David Rientjes35979ef2014-06-04 16:08:27 -0700289 if (pfn < zone->compact_cached_free_pfn)
290 zone->compact_cached_free_pfn = pfn;
Mel Gormanc89511a2012-10-08 16:32:45 -0700291 }
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700292}
293#else
294static inline bool isolation_suitable(struct compact_control *cc,
295 struct page *page)
296{
297 return true;
298}
299
Mel Gormanc89511a2012-10-08 16:32:45 -0700300static void update_pageblock_skip(struct compact_control *cc,
301 struct page *page, unsigned long nr_isolated,
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700302 bool migrate_scanner)
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700303{
304}
305#endif /* CONFIG_COMPACTION */
306
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700307/*
308 * Compaction requires the taking of some coarse locks that are potentially
309 * very heavily contended. For async compaction, back out if the lock cannot
310 * be taken immediately. For sync compaction, spin on the lock if needed.
311 *
312 * Returns true if the lock is held
313 * Returns false if the lock is not held and compaction should abort
314 */
315static bool compact_trylock_irqsave(spinlock_t *lock, unsigned long *flags,
316 struct compact_control *cc)
Mel Gorman2a1402a2012-10-08 16:32:33 -0700317{
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700318 if (cc->mode == MIGRATE_ASYNC) {
319 if (!spin_trylock_irqsave(lock, *flags)) {
320 cc->contended = COMPACT_CONTENDED_LOCK;
321 return false;
322 }
323 } else {
324 spin_lock_irqsave(lock, *flags);
325 }
Vlastimil Babka1f9efde2014-10-09 15:27:14 -0700326
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700327 return true;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700328}
329
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100330/*
Mel Gormanc67fe372012-08-21 16:16:17 -0700331 * Compaction requires the taking of some coarse locks that are potentially
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700332 * very heavily contended. The lock should be periodically unlocked to avoid
333 * having disabled IRQs for a long time, even when there is nobody waiting on
334 * the lock. It might also be that allowing the IRQs will result in
335 * need_resched() becoming true. If scheduling is needed, async compaction
336 * aborts. Sync compaction schedules.
337 * Either compaction type will also abort if a fatal signal is pending.
338 * In either case if the lock was locked, it is dropped and not regained.
Mel Gormanc67fe372012-08-21 16:16:17 -0700339 *
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700340 * Returns true if compaction should abort due to fatal signal pending, or
341 * async compaction due to need_resched()
342 * Returns false when compaction can continue (sync compaction might have
343 * scheduled)
Mel Gormanc67fe372012-08-21 16:16:17 -0700344 */
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700345static bool compact_unlock_should_abort(spinlock_t *lock,
346 unsigned long flags, bool *locked, struct compact_control *cc)
Mel Gormanc67fe372012-08-21 16:16:17 -0700347{
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700348 if (*locked) {
349 spin_unlock_irqrestore(lock, flags);
350 *locked = false;
351 }
Vlastimil Babka1f9efde2014-10-09 15:27:14 -0700352
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700353 if (fatal_signal_pending(current)) {
354 cc->contended = COMPACT_CONTENDED_SCHED;
355 return true;
356 }
Mel Gormanc67fe372012-08-21 16:16:17 -0700357
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700358 if (need_resched()) {
David Rientjese0b9dae2014-06-04 16:08:28 -0700359 if (cc->mode == MIGRATE_ASYNC) {
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700360 cc->contended = COMPACT_CONTENDED_SCHED;
361 return true;
Mel Gormanc67fe372012-08-21 16:16:17 -0700362 }
Mel Gormanc67fe372012-08-21 16:16:17 -0700363 cond_resched();
Mel Gormanc67fe372012-08-21 16:16:17 -0700364 }
365
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700366 return false;
Mel Gormanc67fe372012-08-21 16:16:17 -0700367}
368
Vlastimil Babkabe976572014-06-04 16:10:41 -0700369/*
370 * Aside from avoiding lock contention, compaction also periodically checks
371 * need_resched() and either schedules in sync compaction or aborts async
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700372 * compaction. This is similar to what compact_unlock_should_abort() does, but
Vlastimil Babkabe976572014-06-04 16:10:41 -0700373 * is used where no lock is concerned.
374 *
375 * Returns false when no scheduling was needed, or sync compaction scheduled.
376 * Returns true when async compaction should abort.
377 */
378static inline bool compact_should_abort(struct compact_control *cc)
379{
380 /* async compaction aborts if contended */
381 if (need_resched()) {
382 if (cc->mode == MIGRATE_ASYNC) {
Vlastimil Babka1f9efde2014-10-09 15:27:14 -0700383 cc->contended = COMPACT_CONTENDED_SCHED;
Vlastimil Babkabe976572014-06-04 16:10:41 -0700384 return true;
385 }
386
387 cond_resched();
388 }
389
390 return false;
391}
392
Mel Gormanc67fe372012-08-21 16:16:17 -0700393/*
Jerome Marchand9e4be472013-11-12 15:07:12 -0800394 * Isolate free pages onto a private freelist. If @strict is true, will abort
395 * returning 0 on any invalid PFNs or non-free pages inside of the pageblock
396 * (even though it may still end up isolating some pages).
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100397 */
Mel Gormanf40d1e42012-10-08 16:32:36 -0700398static unsigned long isolate_freepages_block(struct compact_control *cc,
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700399 unsigned long *start_pfn,
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100400 unsigned long end_pfn,
401 struct list_head *freelist,
402 bool strict)
Mel Gorman748446b2010-05-24 14:32:27 -0700403{
Mel Gormanb7aba692011-01-13 15:45:54 -0800404 int nr_scanned = 0, total_isolated = 0;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700405 struct page *cursor, *valid_page = NULL;
Xiubo Lib8b2d822014-10-09 15:28:21 -0700406 unsigned long flags = 0;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700407 bool locked = false;
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700408 unsigned long blockpfn = *start_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700409
Mel Gorman748446b2010-05-24 14:32:27 -0700410 cursor = pfn_to_page(blockpfn);
411
Mel Gormanf40d1e42012-10-08 16:32:36 -0700412 /* Isolate free pages. */
Mel Gorman748446b2010-05-24 14:32:27 -0700413 for (; blockpfn < end_pfn; blockpfn++, cursor++) {
414 int isolated, i;
415 struct page *page = cursor;
416
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700417 /*
418 * Periodically drop the lock (if held) regardless of its
419 * contention, to give chance to IRQs. Abort if fatal signal
420 * pending or async compaction detects need_resched()
421 */
422 if (!(blockpfn % SWAP_CLUSTER_MAX)
423 && compact_unlock_should_abort(&cc->zone->lock, flags,
424 &locked, cc))
425 break;
426
Mel Gormanb7aba692011-01-13 15:45:54 -0800427 nr_scanned++;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700428 if (!pfn_valid_within(blockpfn))
Laura Abbott2af120b2014-03-10 15:49:44 -0700429 goto isolate_fail;
430
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700431 if (!valid_page)
432 valid_page = page;
Vlastimil Babka9fcd6d22015-09-08 15:02:49 -0700433
434 /*
435 * For compound pages such as THP and hugetlbfs, we can save
436 * potentially a lot of iterations if we skip them at once.
437 * The check is racy, but we can consider only valid values
438 * and the only danger is skipping too much.
439 */
440 if (PageCompound(page)) {
441 unsigned int comp_order = compound_order(page);
442
443 if (likely(comp_order < MAX_ORDER)) {
444 blockpfn += (1UL << comp_order) - 1;
445 cursor += (1UL << comp_order) - 1;
446 }
447
448 goto isolate_fail;
449 }
450
Mel Gormanf40d1e42012-10-08 16:32:36 -0700451 if (!PageBuddy(page))
Laura Abbott2af120b2014-03-10 15:49:44 -0700452 goto isolate_fail;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700453
454 /*
Vlastimil Babka69b71892014-10-09 15:27:18 -0700455 * If we already hold the lock, we can skip some rechecking.
456 * Note that if we hold the lock now, checked_pageblock was
457 * already set in some previous iteration (or strict is true),
458 * so it is correct to skip the suitable migration target
459 * recheck as well.
Mel Gormanf40d1e42012-10-08 16:32:36 -0700460 */
Vlastimil Babka69b71892014-10-09 15:27:18 -0700461 if (!locked) {
462 /*
463 * The zone lock must be held to isolate freepages.
464 * Unfortunately this is a very coarse lock and can be
465 * heavily contended if there are parallel allocations
466 * or parallel compactions. For async compaction do not
467 * spin on the lock and we acquire the lock as late as
468 * possible.
469 */
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700470 locked = compact_trylock_irqsave(&cc->zone->lock,
471 &flags, cc);
Vlastimil Babka69b71892014-10-09 15:27:18 -0700472 if (!locked)
473 break;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700474
Vlastimil Babka69b71892014-10-09 15:27:18 -0700475 /* Recheck this is a buddy page under lock */
476 if (!PageBuddy(page))
477 goto isolate_fail;
478 }
Mel Gorman748446b2010-05-24 14:32:27 -0700479
480 /* Found a free page, break it into order-0 pages */
481 isolated = split_free_page(page);
David Rientjesa4f04f22016-06-24 14:50:10 -0700482 if (!isolated)
483 break;
484
Mel Gorman748446b2010-05-24 14:32:27 -0700485 total_isolated += isolated;
David Rientjesa4f04f22016-06-24 14:50:10 -0700486 cc->nr_freepages += isolated;
Mel Gorman748446b2010-05-24 14:32:27 -0700487 for (i = 0; i < isolated; i++) {
488 list_add(&page->lru, freelist);
489 page++;
490 }
David Rientjesa4f04f22016-06-24 14:50:10 -0700491 if (!strict && cc->nr_migratepages <= cc->nr_freepages) {
492 blockpfn += isolated;
493 break;
Mel Gorman748446b2010-05-24 14:32:27 -0700494 }
David Rientjesa4f04f22016-06-24 14:50:10 -0700495 /* Advance to the end of split page */
496 blockpfn += isolated - 1;
497 cursor += isolated - 1;
498 continue;
Laura Abbott2af120b2014-03-10 15:49:44 -0700499
500isolate_fail:
501 if (strict)
502 break;
503 else
504 continue;
505
Mel Gorman748446b2010-05-24 14:32:27 -0700506 }
507
David Rientjesa4f04f22016-06-24 14:50:10 -0700508 if (locked)
509 spin_unlock_irqrestore(&cc->zone->lock, flags);
510
Vlastimil Babka9fcd6d22015-09-08 15:02:49 -0700511 /*
512 * There is a tiny chance that we have read bogus compound_order(),
513 * so be careful to not go outside of the pageblock.
514 */
515 if (unlikely(blockpfn > end_pfn))
516 blockpfn = end_pfn;
517
Joonsoo Kime34d85f2015-02-11 15:27:04 -0800518 trace_mm_compaction_isolate_freepages(*start_pfn, blockpfn,
519 nr_scanned, total_isolated);
520
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700521 /* Record how far we have got within the block */
522 *start_pfn = blockpfn;
523
Mel Gormanf40d1e42012-10-08 16:32:36 -0700524 /*
525 * If strict isolation is requested by CMA then check that all the
526 * pages requested were isolated. If there were any failures, 0 is
527 * returned and CMA will fail.
528 */
Laura Abbott2af120b2014-03-10 15:49:44 -0700529 if (strict && blockpfn < end_pfn)
Mel Gormanf40d1e42012-10-08 16:32:36 -0700530 total_isolated = 0;
531
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700532 /* Update the pageblock-skip if the whole pageblock was scanned */
533 if (blockpfn == end_pfn)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700534 update_pageblock_skip(cc, valid_page, total_isolated, false);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700535
Minchan Kim010fc292012-12-20 15:05:06 -0800536 count_compact_events(COMPACTFREE_SCANNED, nr_scanned);
Mel Gorman397487d2012-10-19 12:00:10 +0100537 if (total_isolated)
Minchan Kim010fc292012-12-20 15:05:06 -0800538 count_compact_events(COMPACTISOLATED, total_isolated);
Mel Gorman748446b2010-05-24 14:32:27 -0700539 return total_isolated;
540}
541
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100542/**
543 * isolate_freepages_range() - isolate free pages.
544 * @start_pfn: The first PFN to start isolating.
545 * @end_pfn: The one-past-last PFN.
546 *
547 * Non-free pages, invalid PFNs, or zone boundaries within the
548 * [start_pfn, end_pfn) range are considered errors, cause function to
549 * undo its actions and return zero.
550 *
551 * Otherwise, function returns one-past-the-last PFN of isolated page
552 * (which may be greater then end_pfn if end fell in a middle of
553 * a free page).
554 */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100555unsigned long
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700556isolate_freepages_range(struct compact_control *cc,
557 unsigned long start_pfn, unsigned long end_pfn)
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100558{
Joonsoo Kime1409c32016-03-15 14:57:48 -0700559 unsigned long isolated, pfn, block_start_pfn, block_end_pfn;
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100560 LIST_HEAD(freelist);
561
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700562 pfn = start_pfn;
Vlastimil Babka06b66402016-05-19 17:11:48 -0700563 block_start_pfn = pageblock_start_pfn(pfn);
Joonsoo Kime1409c32016-03-15 14:57:48 -0700564 if (block_start_pfn < cc->zone->zone_start_pfn)
565 block_start_pfn = cc->zone->zone_start_pfn;
Vlastimil Babka06b66402016-05-19 17:11:48 -0700566 block_end_pfn = pageblock_end_pfn(pfn);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100567
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700568 for (; pfn < end_pfn; pfn += isolated,
Joonsoo Kime1409c32016-03-15 14:57:48 -0700569 block_start_pfn = block_end_pfn,
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700570 block_end_pfn += pageblock_nr_pages) {
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700571 /* Protect pfn from changing by isolate_freepages_block */
572 unsigned long isolate_start_pfn = pfn;
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700573
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100574 block_end_pfn = min(block_end_pfn, end_pfn);
575
Joonsoo Kim58420012014-11-13 15:19:07 -0800576 /*
577 * pfn could pass the block_end_pfn if isolated freepage
578 * is more than pageblock order. In this case, we adjust
579 * scanning range to right one.
580 */
581 if (pfn >= block_end_pfn) {
Vlastimil Babka06b66402016-05-19 17:11:48 -0700582 block_start_pfn = pageblock_start_pfn(pfn);
583 block_end_pfn = pageblock_end_pfn(pfn);
Joonsoo Kim58420012014-11-13 15:19:07 -0800584 block_end_pfn = min(block_end_pfn, end_pfn);
585 }
586
Joonsoo Kime1409c32016-03-15 14:57:48 -0700587 if (!pageblock_pfn_to_page(block_start_pfn,
588 block_end_pfn, cc->zone))
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700589 break;
590
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700591 isolated = isolate_freepages_block(cc, &isolate_start_pfn,
592 block_end_pfn, &freelist, true);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100593
594 /*
595 * In strict mode, isolate_freepages_block() returns 0 if
596 * there are any holes in the block (ie. invalid PFNs or
597 * non-free pages).
598 */
599 if (!isolated)
600 break;
601
602 /*
603 * If we managed to isolate pages, it is always (1 << n) *
604 * pageblock_nr_pages for some non-negative n. (Max order
605 * page may span two pageblocks).
606 */
607 }
608
609 /* split_free_page does not map the pages */
610 map_pages(&freelist);
611
612 if (pfn < end_pfn) {
613 /* Loop terminated early, cleanup. */
614 release_freepages(&freelist);
615 return 0;
616 }
617
618 /* We don't use freelists for anything. */
619 return pfn;
620}
621
Mel Gorman748446b2010-05-24 14:32:27 -0700622/* Update the number of anon and file isolated pages in the zone */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700623static void acct_isolated(struct zone *zone, struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700624{
625 struct page *page;
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700626 unsigned int count[2] = { 0, };
Mel Gorman748446b2010-05-24 14:32:27 -0700627
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700628 if (list_empty(&cc->migratepages))
629 return;
630
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700631 list_for_each_entry(page, &cc->migratepages, lru)
632 count[!!page_is_file_cache(page)]++;
Mel Gorman748446b2010-05-24 14:32:27 -0700633
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700634 mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
635 mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
Mel Gorman748446b2010-05-24 14:32:27 -0700636}
637
638/* Similar to reclaim, but different enough that they don't share logic */
639static bool too_many_isolated(struct zone *zone)
640{
Minchan Kimbc693042010-09-09 16:38:00 -0700641 unsigned long active, inactive, isolated;
Mel Gorman748446b2010-05-24 14:32:27 -0700642
643 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
644 zone_page_state(zone, NR_INACTIVE_ANON);
Minchan Kimbc693042010-09-09 16:38:00 -0700645 active = zone_page_state(zone, NR_ACTIVE_FILE) +
646 zone_page_state(zone, NR_ACTIVE_ANON);
Mel Gorman748446b2010-05-24 14:32:27 -0700647 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
648 zone_page_state(zone, NR_ISOLATED_ANON);
649
Minchan Kimbc693042010-09-09 16:38:00 -0700650 return isolated > (inactive + active) / 2;
Mel Gorman748446b2010-05-24 14:32:27 -0700651}
652
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100653/**
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700654 * isolate_migratepages_block() - isolate all migrate-able pages within
655 * a single pageblock
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100656 * @cc: Compaction control structure.
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700657 * @low_pfn: The first PFN to isolate
658 * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock
659 * @isolate_mode: Isolation mode to be used.
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100660 *
661 * Isolate all pages that can be migrated from the range specified by
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700662 * [low_pfn, end_pfn). The range is expected to be within same pageblock.
663 * Returns zero if there is a fatal signal pending, otherwise PFN of the
664 * first page that was not scanned (which may be both less, equal to or more
665 * than end_pfn).
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100666 *
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700667 * The pages are isolated on cc->migratepages list (not required to be empty),
668 * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field
669 * is neither read nor updated.
Mel Gorman748446b2010-05-24 14:32:27 -0700670 */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700671static unsigned long
672isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
673 unsigned long end_pfn, isolate_mode_t isolate_mode)
Mel Gorman748446b2010-05-24 14:32:27 -0700674{
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700675 struct zone *zone = cc->zone;
Mel Gormanb7aba692011-01-13 15:45:54 -0800676 unsigned long nr_scanned = 0, nr_isolated = 0;
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700677 struct lruvec *lruvec;
Xiubo Lib8b2d822014-10-09 15:28:21 -0700678 unsigned long flags = 0;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700679 bool locked = false;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700680 struct page *page = NULL, *valid_page = NULL;
Joonsoo Kime34d85f2015-02-11 15:27:04 -0800681 unsigned long start_pfn = low_pfn;
Vlastimil Babkafdd048e2016-05-19 17:11:55 -0700682 bool skip_on_failure = false;
683 unsigned long next_skip_pfn = 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700684
Mel Gorman748446b2010-05-24 14:32:27 -0700685 /*
686 * Ensure that there are not too many pages isolated from the LRU
687 * list by either parallel reclaimers or compaction. If there are,
688 * delay for some time until fewer pages are isolated
689 */
690 while (unlikely(too_many_isolated(zone))) {
Mel Gormanf9e35b32011-06-15 15:08:52 -0700691 /* async migration should just abort */
David Rientjese0b9dae2014-06-04 16:08:28 -0700692 if (cc->mode == MIGRATE_ASYNC)
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100693 return 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700694
Mel Gorman748446b2010-05-24 14:32:27 -0700695 congestion_wait(BLK_RW_ASYNC, HZ/10);
696
697 if (fatal_signal_pending(current))
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100698 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700699 }
700
Vlastimil Babkabe976572014-06-04 16:10:41 -0700701 if (compact_should_abort(cc))
702 return 0;
David Rientjesaeef4b82014-06-04 16:08:31 -0700703
Vlastimil Babkafdd048e2016-05-19 17:11:55 -0700704 if (cc->direct_compaction && (cc->mode == MIGRATE_ASYNC)) {
705 skip_on_failure = true;
706 next_skip_pfn = block_end_pfn(low_pfn, cc->order);
707 }
708
Mel Gorman748446b2010-05-24 14:32:27 -0700709 /* Time to isolate some pages for migration */
Mel Gorman748446b2010-05-24 14:32:27 -0700710 for (; low_pfn < end_pfn; low_pfn++) {
Vlastimil Babka29c0dde2015-09-08 15:02:46 -0700711
Vlastimil Babkafdd048e2016-05-19 17:11:55 -0700712 if (skip_on_failure && low_pfn >= next_skip_pfn) {
713 /*
714 * We have isolated all migration candidates in the
715 * previous order-aligned block, and did not skip it due
716 * to failure. We should migrate the pages now and
717 * hopefully succeed compaction.
718 */
719 if (nr_isolated)
720 break;
721
722 /*
723 * We failed to isolate in the previous order-aligned
724 * block. Set the new boundary to the end of the
725 * current block. Note we can't simply increase
726 * next_skip_pfn by 1 << order, as low_pfn might have
727 * been incremented by a higher number due to skipping
728 * a compound or a high-order buddy page in the
729 * previous loop iteration.
730 */
731 next_skip_pfn = block_end_pfn(low_pfn, cc->order);
732 }
733
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700734 /*
735 * Periodically drop the lock (if held) regardless of its
736 * contention, to give chance to IRQs. Abort async compaction
737 * if contended.
738 */
739 if (!(low_pfn % SWAP_CLUSTER_MAX)
740 && compact_unlock_should_abort(&zone->lru_lock, flags,
741 &locked, cc))
742 break;
Mel Gormanc67fe372012-08-21 16:16:17 -0700743
Mel Gorman748446b2010-05-24 14:32:27 -0700744 if (!pfn_valid_within(low_pfn))
Vlastimil Babkafdd048e2016-05-19 17:11:55 -0700745 goto isolate_fail;
Mel Gormanb7aba692011-01-13 15:45:54 -0800746 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700747
Mel Gorman748446b2010-05-24 14:32:27 -0700748 page = pfn_to_page(low_pfn);
Mel Gormandc908602012-02-08 17:13:38 -0800749
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700750 if (!valid_page)
751 valid_page = page;
752
Mel Gorman6c144662014-01-23 15:53:38 -0800753 /*
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700754 * Skip if free. We read page order here without zone lock
755 * which is generally unsafe, but the race window is small and
756 * the worst thing that can happen is that we skip some
757 * potential isolation targets.
Mel Gorman6c144662014-01-23 15:53:38 -0800758 */
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700759 if (PageBuddy(page)) {
760 unsigned long freepage_order = page_order_unsafe(page);
761
762 /*
763 * Without lock, we cannot be sure that what we got is
764 * a valid page order. Consider only values in the
765 * valid order range to prevent low_pfn overflow.
766 */
767 if (freepage_order > 0 && freepage_order < MAX_ORDER)
768 low_pfn += (1UL << freepage_order) - 1;
Mel Gorman748446b2010-05-24 14:32:27 -0700769 continue;
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700770 }
Mel Gorman748446b2010-05-24 14:32:27 -0700771
Mel Gorman9927af742011-01-13 15:45:59 -0800772 /*
Vlastimil Babka29c0dde2015-09-08 15:02:46 -0700773 * Regardless of being on LRU, compound pages such as THP and
774 * hugetlbfs are not to be compacted. We can potentially save
775 * a lot of iterations if we skip them at once. The check is
776 * racy, but we can consider only valid values and the only
777 * danger is skipping too much.
Andrea Arcangelibc835012011-01-13 15:47:08 -0800778 */
Vlastimil Babka29c0dde2015-09-08 15:02:46 -0700779 if (PageCompound(page)) {
780 unsigned int comp_order = compound_order(page);
781
782 if (likely(comp_order < MAX_ORDER))
783 low_pfn += (1UL << comp_order) - 1;
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700784
Vlastimil Babkafdd048e2016-05-19 17:11:55 -0700785 goto isolate_fail;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700786 }
787
Minchan Kimbda807d2016-07-26 15:23:05 -0700788 /*
789 * Check may be lockless but that's ok as we recheck later.
790 * It's possible to migrate LRU and non-lru movable pages.
791 * Skip any other type of page
792 */
793 if (!PageLRU(page)) {
794 if (unlikely(balloon_page_movable(page))) {
795 if (balloon_page_isolate(page)) {
796 /* Successfully isolated */
797 goto isolate_success;
798 }
799 }
800
801 /*
802 * __PageMovable can return false positive so we need
803 * to verify it under page_lock.
804 */
805 if (unlikely(__PageMovable(page)) &&
806 !PageIsolated(page)) {
807 if (locked) {
808 spin_unlock_irqrestore(&zone->lru_lock,
809 flags);
810 locked = false;
811 }
812
813 if (isolate_movable_page(page, isolate_mode))
814 goto isolate_success;
815 }
816
Vlastimil Babkafdd048e2016-05-19 17:11:55 -0700817 goto isolate_fail;
Minchan Kimbda807d2016-07-26 15:23:05 -0700818 }
Vlastimil Babka29c0dde2015-09-08 15:02:46 -0700819
David Rientjes119d6d52014-04-03 14:48:00 -0700820 /*
821 * Migration will fail if an anonymous page is pinned in memory,
822 * so avoid taking lru_lock and isolating it unnecessarily in an
823 * admittedly racy check.
824 */
825 if (!page_mapping(page) &&
826 page_count(page) > page_mapcount(page))
Vlastimil Babkafdd048e2016-05-19 17:11:55 -0700827 goto isolate_fail;
David Rientjes119d6d52014-04-03 14:48:00 -0700828
Vlastimil Babka69b71892014-10-09 15:27:18 -0700829 /* If we already hold the lock, we can skip some rechecking */
830 if (!locked) {
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700831 locked = compact_trylock_irqsave(&zone->lru_lock,
832 &flags, cc);
Vlastimil Babka69b71892014-10-09 15:27:18 -0700833 if (!locked)
834 break;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700835
Vlastimil Babka29c0dde2015-09-08 15:02:46 -0700836 /* Recheck PageLRU and PageCompound under lock */
Vlastimil Babka69b71892014-10-09 15:27:18 -0700837 if (!PageLRU(page))
Vlastimil Babkafdd048e2016-05-19 17:11:55 -0700838 goto isolate_fail;
Vlastimil Babka29c0dde2015-09-08 15:02:46 -0700839
840 /*
841 * Page become compound since the non-locked check,
842 * and it's on LRU. It can only be a THP so the order
843 * is safe to read and it's 0 for tail pages.
844 */
845 if (unlikely(PageCompound(page))) {
846 low_pfn += (1UL << compound_order(page)) - 1;
Vlastimil Babkafdd048e2016-05-19 17:11:55 -0700847 goto isolate_fail;
Vlastimil Babka69b71892014-10-09 15:27:18 -0700848 }
Andrea Arcangelibc835012011-01-13 15:47:08 -0800849 }
850
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700851 lruvec = mem_cgroup_page_lruvec(page, zone);
852
Mel Gorman748446b2010-05-24 14:32:27 -0700853 /* Try isolate the page */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700854 if (__isolate_lru_page(page, isolate_mode) != 0)
Vlastimil Babkafdd048e2016-05-19 17:11:55 -0700855 goto isolate_fail;
Mel Gorman748446b2010-05-24 14:32:27 -0700856
Vlastimil Babka29c0dde2015-09-08 15:02:46 -0700857 VM_BUG_ON_PAGE(PageCompound(page), page);
Andrea Arcangelibc835012011-01-13 15:47:08 -0800858
Mel Gorman748446b2010-05-24 14:32:27 -0700859 /* Successfully isolated */
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700860 del_page_from_lru_list(page, lruvec, page_lru(page));
Joonsoo Kimb6c75012014-04-07 15:37:07 -0700861
862isolate_success:
Vlastimil Babkafdd048e2016-05-19 17:11:55 -0700863 list_add(&page->lru, &cc->migratepages);
Mel Gorman748446b2010-05-24 14:32:27 -0700864 cc->nr_migratepages++;
Mel Gormanb7aba692011-01-13 15:45:54 -0800865 nr_isolated++;
Mel Gorman748446b2010-05-24 14:32:27 -0700866
Vlastimil Babkaa34753d2016-05-19 17:11:51 -0700867 /*
868 * Record where we could have freed pages by migration and not
869 * yet flushed them to buddy allocator.
870 * - this is the lowest page that was isolated and likely be
871 * then freed by migration.
872 */
873 if (!cc->last_migrated_pfn)
874 cc->last_migrated_pfn = low_pfn;
875
Mel Gorman748446b2010-05-24 14:32:27 -0700876 /* Avoid isolating too much */
Hillf Danton31b83842012-01-10 15:07:59 -0800877 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
878 ++low_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700879 break;
Hillf Danton31b83842012-01-10 15:07:59 -0800880 }
Vlastimil Babkafdd048e2016-05-19 17:11:55 -0700881
882 continue;
883isolate_fail:
884 if (!skip_on_failure)
885 continue;
886
887 /*
888 * We have isolated some pages, but then failed. Release them
889 * instead of migrating, as we cannot form the cc->order buddy
890 * page anyway.
891 */
892 if (nr_isolated) {
893 if (locked) {
894 spin_unlock_irqrestore(&zone->lru_lock, flags);
895 locked = false;
896 }
897 acct_isolated(zone, cc);
898 putback_movable_pages(&cc->migratepages);
899 cc->nr_migratepages = 0;
900 cc->last_migrated_pfn = 0;
901 nr_isolated = 0;
902 }
903
904 if (low_pfn < next_skip_pfn) {
905 low_pfn = next_skip_pfn - 1;
906 /*
907 * The check near the loop beginning would have updated
908 * next_skip_pfn too, but this is a bit simpler.
909 */
910 next_skip_pfn += 1UL << cc->order;
911 }
Mel Gorman748446b2010-05-24 14:32:27 -0700912 }
913
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700914 /*
915 * The PageBuddy() check could have potentially brought us outside
916 * the range to be scanned.
917 */
918 if (unlikely(low_pfn > end_pfn))
919 low_pfn = end_pfn;
920
Mel Gormanc67fe372012-08-21 16:16:17 -0700921 if (locked)
922 spin_unlock_irqrestore(&zone->lru_lock, flags);
Mel Gorman748446b2010-05-24 14:32:27 -0700923
Vlastimil Babka50b5b092014-01-21 15:51:10 -0800924 /*
925 * Update the pageblock-skip information and cached scanner pfn,
926 * if the whole pageblock was scanned without isolating any page.
Vlastimil Babka50b5b092014-01-21 15:51:10 -0800927 */
David Rientjes35979ef2014-06-04 16:08:27 -0700928 if (low_pfn == end_pfn)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700929 update_pageblock_skip(cc, valid_page, nr_isolated, true);
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700930
Joonsoo Kime34d85f2015-02-11 15:27:04 -0800931 trace_mm_compaction_isolate_migratepages(start_pfn, low_pfn,
932 nr_scanned, nr_isolated);
Mel Gormanb7aba692011-01-13 15:45:54 -0800933
Minchan Kim010fc292012-12-20 15:05:06 -0800934 count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned);
Mel Gorman397487d2012-10-19 12:00:10 +0100935 if (nr_isolated)
Minchan Kim010fc292012-12-20 15:05:06 -0800936 count_compact_events(COMPACTISOLATED, nr_isolated);
Mel Gorman397487d2012-10-19 12:00:10 +0100937
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100938 return low_pfn;
939}
940
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700941/**
942 * isolate_migratepages_range() - isolate migrate-able pages in a PFN range
943 * @cc: Compaction control structure.
944 * @start_pfn: The first PFN to start isolating.
945 * @end_pfn: The one-past-last PFN.
946 *
947 * Returns zero if isolation fails fatally due to e.g. pending signal.
948 * Otherwise, function returns one-past-the-last PFN of isolated page
949 * (which may be greater than end_pfn if end fell in a middle of a THP page).
950 */
951unsigned long
952isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn,
953 unsigned long end_pfn)
954{
Joonsoo Kime1409c32016-03-15 14:57:48 -0700955 unsigned long pfn, block_start_pfn, block_end_pfn;
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700956
957 /* Scan block by block. First and last block may be incomplete */
958 pfn = start_pfn;
Vlastimil Babka06b66402016-05-19 17:11:48 -0700959 block_start_pfn = pageblock_start_pfn(pfn);
Joonsoo Kime1409c32016-03-15 14:57:48 -0700960 if (block_start_pfn < cc->zone->zone_start_pfn)
961 block_start_pfn = cc->zone->zone_start_pfn;
Vlastimil Babka06b66402016-05-19 17:11:48 -0700962 block_end_pfn = pageblock_end_pfn(pfn);
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700963
964 for (; pfn < end_pfn; pfn = block_end_pfn,
Joonsoo Kime1409c32016-03-15 14:57:48 -0700965 block_start_pfn = block_end_pfn,
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700966 block_end_pfn += pageblock_nr_pages) {
967
968 block_end_pfn = min(block_end_pfn, end_pfn);
969
Joonsoo Kime1409c32016-03-15 14:57:48 -0700970 if (!pageblock_pfn_to_page(block_start_pfn,
971 block_end_pfn, cc->zone))
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700972 continue;
973
974 pfn = isolate_migratepages_block(cc, pfn, block_end_pfn,
975 ISOLATE_UNEVICTABLE);
976
Hugh Dickins14af4a52016-05-05 16:22:15 -0700977 if (!pfn)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700978 break;
Joonsoo Kim6ea41c02014-10-29 14:50:20 -0700979
980 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX)
981 break;
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700982 }
983 acct_isolated(cc->zone, cc);
984
985 return pfn;
986}
987
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100988#endif /* CONFIG_COMPACTION || CONFIG_CMA */
989#ifdef CONFIG_COMPACTION
Andrew Morton018e9a42015-04-15 16:15:20 -0700990
991/* Returns true if the page is within a block suitable for migration to */
992static bool suitable_migration_target(struct page *page)
993{
994 /* If the page is a large free page, then disallow migration */
995 if (PageBuddy(page)) {
996 /*
997 * We are checking page_order without zone->lock taken. But
998 * the only small danger is that we skip a potentially suitable
999 * pageblock, so it's not worth to check order for valid range.
1000 */
1001 if (page_order_unsafe(page) >= pageblock_order)
1002 return false;
1003 }
1004
1005 /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
1006 if (migrate_async_suitable(get_pageblock_migratetype(page)))
1007 return true;
1008
1009 /* Otherwise skip the block */
1010 return false;
1011}
1012
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001013/*
Vlastimil Babkaf2849aa2015-09-08 15:02:36 -07001014 * Test whether the free scanner has reached the same or lower pageblock than
1015 * the migration scanner, and compaction should thus terminate.
1016 */
1017static inline bool compact_scanners_met(struct compact_control *cc)
1018{
1019 return (cc->free_pfn >> pageblock_order)
1020 <= (cc->migrate_pfn >> pageblock_order);
1021}
1022
1023/*
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001024 * Based on information in the current compact_control, find blocks
1025 * suitable for isolating free pages from and then isolate them.
1026 */
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001027static void isolate_freepages(struct compact_control *cc)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001028{
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001029 struct zone *zone = cc->zone;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001030 struct page *page;
Vlastimil Babkac96b9e52014-06-04 16:07:26 -07001031 unsigned long block_start_pfn; /* start of current pageblock */
Vlastimil Babkae14c7202014-10-09 15:27:20 -07001032 unsigned long isolate_start_pfn; /* exact pfn we start at */
Vlastimil Babkac96b9e52014-06-04 16:07:26 -07001033 unsigned long block_end_pfn; /* end of current pageblock */
1034 unsigned long low_pfn; /* lowest pfn scanner is able to scan */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001035 struct list_head *freelist = &cc->freepages;
1036
1037 /*
1038 * Initialise the free scanner. The starting point is where we last
Vlastimil Babka49e068f2014-05-06 12:50:03 -07001039 * successfully isolated from, zone-cached value, or the end of the
Vlastimil Babkae14c7202014-10-09 15:27:20 -07001040 * zone when isolating for the first time. For looping we also need
1041 * this pfn aligned down to the pageblock boundary, because we do
Vlastimil Babkac96b9e52014-06-04 16:07:26 -07001042 * block_start_pfn -= pageblock_nr_pages in the for loop.
1043 * For ending point, take care when isolating in last pageblock of a
1044 * a zone which ends in the middle of a pageblock.
Vlastimil Babka49e068f2014-05-06 12:50:03 -07001045 * The low boundary is the end of the pageblock the migration scanner
1046 * is using.
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001047 */
Vlastimil Babkae14c7202014-10-09 15:27:20 -07001048 isolate_start_pfn = cc->free_pfn;
Vlastimil Babka06b66402016-05-19 17:11:48 -07001049 block_start_pfn = pageblock_start_pfn(cc->free_pfn);
Vlastimil Babkac96b9e52014-06-04 16:07:26 -07001050 block_end_pfn = min(block_start_pfn + pageblock_nr_pages,
1051 zone_end_pfn(zone));
Vlastimil Babka06b66402016-05-19 17:11:48 -07001052 low_pfn = pageblock_end_pfn(cc->migrate_pfn);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001053
1054 /*
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001055 * Isolate free pages until enough are available to migrate the
1056 * pages on cc->migratepages. We stop searching if the migrate
1057 * and free page scanners meet or enough free pages are isolated.
1058 */
Vlastimil Babkaf5f61a32015-09-08 15:02:39 -07001059 for (; block_start_pfn >= low_pfn;
Vlastimil Babkac96b9e52014-06-04 16:07:26 -07001060 block_end_pfn = block_start_pfn,
Vlastimil Babkae14c7202014-10-09 15:27:20 -07001061 block_start_pfn -= pageblock_nr_pages,
1062 isolate_start_pfn = block_start_pfn) {
David Rientjesf6ea3ad2013-09-30 13:45:03 -07001063 /*
1064 * This can iterate a massively long zone without finding any
1065 * suitable migration targets, so periodically check if we need
Vlastimil Babkabe976572014-06-04 16:10:41 -07001066 * to schedule, or even abort async compaction.
David Rientjesf6ea3ad2013-09-30 13:45:03 -07001067 */
Vlastimil Babkabe976572014-06-04 16:10:41 -07001068 if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
1069 && compact_should_abort(cc))
1070 break;
David Rientjesf6ea3ad2013-09-30 13:45:03 -07001071
Vlastimil Babka7d49d882014-10-09 15:27:11 -07001072 page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
1073 zone);
1074 if (!page)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001075 continue;
1076
1077 /* Check the block is suitable for migration */
Linus Torvalds68e3e922012-06-03 20:05:57 -07001078 if (!suitable_migration_target(page))
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001079 continue;
Linus Torvalds68e3e922012-06-03 20:05:57 -07001080
Mel Gormanbb13ffe2012-10-08 16:32:41 -07001081 /* If isolation recently failed, do not retry */
1082 if (!isolation_suitable(cc, page))
1083 continue;
1084
Vlastimil Babkae14c7202014-10-09 15:27:20 -07001085 /* Found a block suitable for isolating free pages from. */
David Rientjesa46cbf32016-07-14 12:06:50 -07001086 isolate_freepages_block(cc, &isolate_start_pfn, block_end_pfn,
1087 freelist, false);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001088
1089 /*
David Rientjesa46cbf32016-07-14 12:06:50 -07001090 * If we isolated enough freepages, or aborted due to lock
1091 * contention, terminate.
Vlastimil Babkae14c7202014-10-09 15:27:20 -07001092 */
Vlastimil Babkaf5f61a32015-09-08 15:02:39 -07001093 if ((cc->nr_freepages >= cc->nr_migratepages)
1094 || cc->contended) {
David Rientjesa46cbf32016-07-14 12:06:50 -07001095 if (isolate_start_pfn >= block_end_pfn) {
1096 /*
1097 * Restart at previous pageblock if more
1098 * freepages can be isolated next time.
1099 */
Vlastimil Babkaf5f61a32015-09-08 15:02:39 -07001100 isolate_start_pfn =
1101 block_start_pfn - pageblock_nr_pages;
David Rientjesa46cbf32016-07-14 12:06:50 -07001102 }
Vlastimil Babkabe976572014-06-04 16:10:41 -07001103 break;
David Rientjesa46cbf32016-07-14 12:06:50 -07001104 } else if (isolate_start_pfn < block_end_pfn) {
Vlastimil Babkaf5f61a32015-09-08 15:02:39 -07001105 /*
David Rientjesa46cbf32016-07-14 12:06:50 -07001106 * If isolation failed early, do not continue
1107 * needlessly.
Vlastimil Babkaf5f61a32015-09-08 15:02:39 -07001108 */
David Rientjesa46cbf32016-07-14 12:06:50 -07001109 break;
Vlastimil Babkaf5f61a32015-09-08 15:02:39 -07001110 }
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +01001111 }
1112
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001113 /* split_free_page does not map the pages */
1114 map_pages(freelist);
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +01001115
Vlastimil Babka7ed695e2014-01-21 15:51:09 -08001116 /*
Vlastimil Babkaf5f61a32015-09-08 15:02:39 -07001117 * Record where the free scanner will restart next time. Either we
1118 * broke from the loop and set isolate_start_pfn based on the last
1119 * call to isolate_freepages_block(), or we met the migration scanner
1120 * and the loop terminated due to isolate_start_pfn < low_pfn
Vlastimil Babka7ed695e2014-01-21 15:51:09 -08001121 */
Vlastimil Babkaf5f61a32015-09-08 15:02:39 -07001122 cc->free_pfn = isolate_start_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -07001123}
1124
1125/*
1126 * This is a migrate-callback that "allocates" freepages by taking pages
1127 * from the isolated freelists in the block we are migrating to.
1128 */
1129static struct page *compaction_alloc(struct page *migratepage,
1130 unsigned long data,
1131 int **result)
1132{
1133 struct compact_control *cc = (struct compact_control *)data;
1134 struct page *freepage;
1135
Vlastimil Babkabe976572014-06-04 16:10:41 -07001136 /*
1137 * Isolate free pages if necessary, and if we are not aborting due to
1138 * contention.
1139 */
Mel Gorman748446b2010-05-24 14:32:27 -07001140 if (list_empty(&cc->freepages)) {
Vlastimil Babkabe976572014-06-04 16:10:41 -07001141 if (!cc->contended)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001142 isolate_freepages(cc);
Mel Gorman748446b2010-05-24 14:32:27 -07001143
1144 if (list_empty(&cc->freepages))
1145 return NULL;
1146 }
1147
1148 freepage = list_entry(cc->freepages.next, struct page, lru);
1149 list_del(&freepage->lru);
1150 cc->nr_freepages--;
1151
1152 return freepage;
1153}
1154
1155/*
David Rientjesd53aea32014-06-04 16:08:26 -07001156 * This is a migrate-callback that "frees" freepages back to the isolated
1157 * freelist. All pages on the freelist are from the same zone, so there is no
1158 * special handling needed for NUMA.
1159 */
1160static void compaction_free(struct page *page, unsigned long data)
1161{
1162 struct compact_control *cc = (struct compact_control *)data;
1163
1164 list_add(&page->lru, &cc->freepages);
1165 cc->nr_freepages++;
1166}
1167
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001168/* possible outcome of isolate_migratepages */
1169typedef enum {
1170 ISOLATE_ABORT, /* Abort compaction now */
1171 ISOLATE_NONE, /* No pages isolated, continue scanning */
1172 ISOLATE_SUCCESS, /* Pages isolated, migrate */
1173} isolate_migrate_t;
1174
1175/*
Eric B Munson5bbe3542015-04-15 16:13:20 -07001176 * Allow userspace to control policy on scanning the unevictable LRU for
1177 * compactable pages.
1178 */
1179int sysctl_compact_unevictable_allowed __read_mostly = 1;
1180
1181/*
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001182 * Isolate all pages that can be migrated from the first suitable block,
1183 * starting at the block pointed to by the migrate scanner pfn within
1184 * compact_control.
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001185 */
1186static isolate_migrate_t isolate_migratepages(struct zone *zone,
1187 struct compact_control *cc)
1188{
Joonsoo Kime1409c32016-03-15 14:57:48 -07001189 unsigned long block_start_pfn;
1190 unsigned long block_end_pfn;
1191 unsigned long low_pfn;
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001192 struct page *page;
1193 const isolate_mode_t isolate_mode =
Eric B Munson5bbe3542015-04-15 16:13:20 -07001194 (sysctl_compact_unevictable_allowed ? ISOLATE_UNEVICTABLE : 0) |
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001195 (cc->mode == MIGRATE_ASYNC ? ISOLATE_ASYNC_MIGRATE : 0);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001196
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001197 /*
1198 * Start at where we last stopped, or beginning of the zone as
1199 * initialized by compact_zone()
1200 */
1201 low_pfn = cc->migrate_pfn;
Vlastimil Babka06b66402016-05-19 17:11:48 -07001202 block_start_pfn = pageblock_start_pfn(low_pfn);
Joonsoo Kime1409c32016-03-15 14:57:48 -07001203 if (block_start_pfn < zone->zone_start_pfn)
1204 block_start_pfn = zone->zone_start_pfn;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001205
1206 /* Only scan within a pageblock boundary */
Vlastimil Babka06b66402016-05-19 17:11:48 -07001207 block_end_pfn = pageblock_end_pfn(low_pfn);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001208
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001209 /*
1210 * Iterate over whole pageblocks until we find the first suitable.
1211 * Do not cross the free scanner.
1212 */
Joonsoo Kime1409c32016-03-15 14:57:48 -07001213 for (; block_end_pfn <= cc->free_pfn;
1214 low_pfn = block_end_pfn,
1215 block_start_pfn = block_end_pfn,
1216 block_end_pfn += pageblock_nr_pages) {
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001217
1218 /*
1219 * This can potentially iterate a massively long zone with
1220 * many pageblocks unsuitable, so periodically check if we
1221 * need to schedule, or even abort async compaction.
1222 */
1223 if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
1224 && compact_should_abort(cc))
1225 break;
1226
Joonsoo Kime1409c32016-03-15 14:57:48 -07001227 page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
1228 zone);
Vlastimil Babka7d49d882014-10-09 15:27:11 -07001229 if (!page)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001230 continue;
1231
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001232 /* If isolation recently failed, do not retry */
1233 if (!isolation_suitable(cc, page))
1234 continue;
1235
1236 /*
1237 * For async compaction, also only scan in MOVABLE blocks.
1238 * Async compaction is optimistic to see if the minimum amount
1239 * of work satisfies the allocation.
1240 */
1241 if (cc->mode == MIGRATE_ASYNC &&
1242 !migrate_async_suitable(get_pageblock_migratetype(page)))
1243 continue;
1244
1245 /* Perform the isolation */
Joonsoo Kime1409c32016-03-15 14:57:48 -07001246 low_pfn = isolate_migratepages_block(cc, low_pfn,
1247 block_end_pfn, isolate_mode);
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001248
Hugh Dickinsff599092015-02-12 15:00:28 -08001249 if (!low_pfn || cc->contended) {
1250 acct_isolated(zone, cc);
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001251 return ISOLATE_ABORT;
Hugh Dickinsff599092015-02-12 15:00:28 -08001252 }
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001253
1254 /*
1255 * Either we isolated something and proceed with migration. Or
1256 * we failed and compact_zone should decide if we should
1257 * continue or not.
1258 */
1259 break;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001260 }
1261
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001262 acct_isolated(zone, cc);
Vlastimil Babkaf2849aa2015-09-08 15:02:36 -07001263 /* Record where migration scanner will be restarted. */
1264 cc->migrate_pfn = low_pfn;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001265
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001266 return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001267}
1268
Yaowei Bai21c527a2015-11-05 18:47:20 -08001269/*
1270 * order == -1 is expected when compacting via
1271 * /proc/sys/vm/compact_memory
1272 */
1273static inline bool is_via_compact_memory(int order)
1274{
1275 return order == -1;
1276}
1277
Michal Hockoea7ab982016-05-20 16:56:38 -07001278static enum compact_result __compact_finished(struct zone *zone, struct compact_control *cc,
David Rientjes6d7ce552014-10-09 15:27:27 -07001279 const int migratetype)
Mel Gorman748446b2010-05-24 14:32:27 -07001280{
Mel Gorman8fb74b92013-01-11 14:32:16 -08001281 unsigned int order;
Andrea Arcangeli5a03b052011-01-13 15:47:11 -08001282 unsigned long watermark;
Mel Gorman56de7262010-05-24 14:32:30 -07001283
Vlastimil Babkabe976572014-06-04 16:10:41 -07001284 if (cc->contended || fatal_signal_pending(current))
Vlastimil Babka2d1e1042015-11-05 18:48:02 -08001285 return COMPACT_CONTENDED;
Mel Gorman748446b2010-05-24 14:32:27 -07001286
Mel Gorman753341a2012-10-08 16:32:40 -07001287 /* Compaction run completes if the migrate and free scanner meet */
Vlastimil Babkaf2849aa2015-09-08 15:02:36 -07001288 if (compact_scanners_met(cc)) {
Vlastimil Babka55b7c4c2014-01-21 15:51:11 -08001289 /* Let the next compaction start anew. */
Vlastimil Babka02333642015-09-08 15:02:42 -07001290 reset_cached_positions(zone);
Vlastimil Babka55b7c4c2014-01-21 15:51:11 -08001291
Mel Gorman62997022012-10-08 16:32:47 -07001292 /*
1293 * Mark that the PG_migrate_skip information should be cleared
Vlastimil Babkaaccf6242016-03-17 14:18:15 -07001294 * by kswapd when it goes to sleep. kcompactd does not set the
Mel Gorman62997022012-10-08 16:32:47 -07001295 * flag itself as the decision to be clear should be directly
1296 * based on an allocation request.
1297 */
Vlastimil Babkaaccf6242016-03-17 14:18:15 -07001298 if (cc->direct_compaction)
Mel Gorman62997022012-10-08 16:32:47 -07001299 zone->compact_blockskip_flush = true;
1300
Michal Hockoc8f7de02016-05-20 16:56:47 -07001301 if (cc->whole_zone)
1302 return COMPACT_COMPLETE;
1303 else
1304 return COMPACT_PARTIAL_SKIPPED;
Mel Gormanbb13ffe2012-10-08 16:32:41 -07001305 }
Mel Gorman748446b2010-05-24 14:32:27 -07001306
Yaowei Bai21c527a2015-11-05 18:47:20 -08001307 if (is_via_compact_memory(cc->order))
Mel Gorman56de7262010-05-24 14:32:30 -07001308 return COMPACT_CONTINUE;
1309
Michal Hocko3957c772011-06-15 15:08:25 -07001310 /* Compaction run is not finished if the watermark is not met */
1311 watermark = low_wmark_pages(zone);
Michal Hocko3957c772011-06-15 15:08:25 -07001312
Vlastimil Babkaebff3982014-12-10 15:43:22 -08001313 if (!zone_watermark_ok(zone, cc->order, watermark, cc->classzone_idx,
1314 cc->alloc_flags))
Michal Hocko3957c772011-06-15 15:08:25 -07001315 return COMPACT_CONTINUE;
1316
Mel Gorman56de7262010-05-24 14:32:30 -07001317 /* Direct compactor: Is a suitable page free? */
Mel Gorman8fb74b92013-01-11 14:32:16 -08001318 for (order = cc->order; order < MAX_ORDER; order++) {
1319 struct free_area *area = &zone->free_area[order];
Joonsoo Kim2149cda2015-04-14 15:45:21 -07001320 bool can_steal;
Mel Gorman56de7262010-05-24 14:32:30 -07001321
Mel Gorman8fb74b92013-01-11 14:32:16 -08001322 /* Job done if page is free of the right migratetype */
David Rientjes6d7ce552014-10-09 15:27:27 -07001323 if (!list_empty(&area->free_list[migratetype]))
Mel Gorman8fb74b92013-01-11 14:32:16 -08001324 return COMPACT_PARTIAL;
1325
Joonsoo Kim2149cda2015-04-14 15:45:21 -07001326#ifdef CONFIG_CMA
1327 /* MIGRATE_MOVABLE can fallback on MIGRATE_CMA */
1328 if (migratetype == MIGRATE_MOVABLE &&
1329 !list_empty(&area->free_list[MIGRATE_CMA]))
1330 return COMPACT_PARTIAL;
1331#endif
1332 /*
1333 * Job done if allocation would steal freepages from
1334 * other migratetype buddy lists.
1335 */
1336 if (find_suitable_fallback(area, order, migratetype,
1337 true, &can_steal) != -1)
Mel Gorman8fb74b92013-01-11 14:32:16 -08001338 return COMPACT_PARTIAL;
Mel Gorman56de7262010-05-24 14:32:30 -07001339 }
1340
Joonsoo Kim837d0262015-02-11 15:27:06 -08001341 return COMPACT_NO_SUITABLE_PAGE;
1342}
1343
Michal Hockoea7ab982016-05-20 16:56:38 -07001344static enum compact_result compact_finished(struct zone *zone,
1345 struct compact_control *cc,
1346 const int migratetype)
Joonsoo Kim837d0262015-02-11 15:27:06 -08001347{
1348 int ret;
1349
1350 ret = __compact_finished(zone, cc, migratetype);
1351 trace_mm_compaction_finished(zone, cc->order, ret);
1352 if (ret == COMPACT_NO_SUITABLE_PAGE)
1353 ret = COMPACT_CONTINUE;
1354
1355 return ret;
Mel Gorman748446b2010-05-24 14:32:27 -07001356}
1357
Mel Gorman3e7d3442011-01-13 15:45:56 -08001358/*
1359 * compaction_suitable: Is this suitable to run compaction on this zone now?
1360 * Returns
1361 * COMPACT_SKIPPED - If there are too few free pages for compaction
1362 * COMPACT_PARTIAL - If the allocation would succeed without compaction
1363 * COMPACT_CONTINUE - If compaction should run now
1364 */
Michal Hockoea7ab982016-05-20 16:56:38 -07001365static enum compact_result __compaction_suitable(struct zone *zone, int order,
Mel Gormanc6038442016-05-19 17:13:38 -07001366 unsigned int alloc_flags,
Michal Hocko86a294a2016-05-20 16:57:12 -07001367 int classzone_idx,
1368 unsigned long wmark_target)
Mel Gorman3e7d3442011-01-13 15:45:56 -08001369{
1370 int fragindex;
1371 unsigned long watermark;
1372
Yaowei Bai21c527a2015-11-05 18:47:20 -08001373 if (is_via_compact_memory(order))
Michal Hocko3957c772011-06-15 15:08:25 -07001374 return COMPACT_CONTINUE;
1375
Vlastimil Babkaebff3982014-12-10 15:43:22 -08001376 watermark = low_wmark_pages(zone);
1377 /*
1378 * If watermarks for high-order allocation are already met, there
1379 * should be no need for compaction at all.
1380 */
1381 if (zone_watermark_ok(zone, order, watermark, classzone_idx,
1382 alloc_flags))
1383 return COMPACT_PARTIAL;
1384
Michal Hocko3957c772011-06-15 15:08:25 -07001385 /*
Mel Gorman3e7d3442011-01-13 15:45:56 -08001386 * Watermarks for order-0 must be met for compaction. Note the 2UL.
1387 * This is because during migration, copies of pages need to be
1388 * allocated and for a short time, the footprint is higher
1389 */
Vlastimil Babkaebff3982014-12-10 15:43:22 -08001390 watermark += (2UL << order);
Michal Hocko86a294a2016-05-20 16:57:12 -07001391 if (!__zone_watermark_ok(zone, 0, watermark, classzone_idx,
1392 alloc_flags, wmark_target))
Mel Gorman3e7d3442011-01-13 15:45:56 -08001393 return COMPACT_SKIPPED;
1394
1395 /*
1396 * fragmentation index determines if allocation failures are due to
1397 * low memory or external fragmentation
1398 *
Vlastimil Babkaebff3982014-12-10 15:43:22 -08001399 * index of -1000 would imply allocations might succeed depending on
1400 * watermarks, but we already failed the high-order watermark check
Mel Gorman3e7d3442011-01-13 15:45:56 -08001401 * index towards 0 implies failure is due to lack of memory
1402 * index towards 1000 implies failure is due to fragmentation
1403 *
1404 * Only compact if a failure would be due to fragmentation.
1405 */
1406 fragindex = fragmentation_index(zone, order);
1407 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
Joonsoo Kim837d0262015-02-11 15:27:06 -08001408 return COMPACT_NOT_SUITABLE_ZONE;
Mel Gorman3e7d3442011-01-13 15:45:56 -08001409
Mel Gorman3e7d3442011-01-13 15:45:56 -08001410 return COMPACT_CONTINUE;
1411}
1412
Michal Hockoea7ab982016-05-20 16:56:38 -07001413enum compact_result compaction_suitable(struct zone *zone, int order,
Mel Gormanc6038442016-05-19 17:13:38 -07001414 unsigned int alloc_flags,
1415 int classzone_idx)
Joonsoo Kim837d0262015-02-11 15:27:06 -08001416{
Michal Hockoea7ab982016-05-20 16:56:38 -07001417 enum compact_result ret;
Joonsoo Kim837d0262015-02-11 15:27:06 -08001418
Michal Hocko86a294a2016-05-20 16:57:12 -07001419 ret = __compaction_suitable(zone, order, alloc_flags, classzone_idx,
1420 zone_page_state(zone, NR_FREE_PAGES));
Joonsoo Kim837d0262015-02-11 15:27:06 -08001421 trace_mm_compaction_suitable(zone, order, ret);
1422 if (ret == COMPACT_NOT_SUITABLE_ZONE)
1423 ret = COMPACT_SKIPPED;
1424
1425 return ret;
1426}
1427
Michal Hocko86a294a2016-05-20 16:57:12 -07001428bool compaction_zonelist_suitable(struct alloc_context *ac, int order,
1429 int alloc_flags)
1430{
1431 struct zone *zone;
1432 struct zoneref *z;
1433
1434 /*
1435 * Make sure at least one zone would pass __compaction_suitable if we continue
1436 * retrying the reclaim.
1437 */
1438 for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx,
1439 ac->nodemask) {
1440 unsigned long available;
1441 enum compact_result compact_result;
1442
1443 /*
1444 * Do not consider all the reclaimable memory because we do not
1445 * want to trash just for a single high order allocation which
1446 * is even not guaranteed to appear even if __compaction_suitable
1447 * is happy about the watermark check.
1448 */
1449 available = zone_reclaimable_pages(zone) / order;
1450 available += zone_page_state_snapshot(zone, NR_FREE_PAGES);
1451 compact_result = __compaction_suitable(zone, order, alloc_flags,
1452 ac_classzone_idx(ac), available);
1453 if (compact_result != COMPACT_SKIPPED &&
1454 compact_result != COMPACT_NOT_SUITABLE_ZONE)
1455 return true;
1456 }
1457
1458 return false;
1459}
1460
Michal Hockoea7ab982016-05-20 16:56:38 -07001461static enum compact_result compact_zone(struct zone *zone, struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -07001462{
Michal Hockoea7ab982016-05-20 16:56:38 -07001463 enum compact_result ret;
Mel Gormanc89511a2012-10-08 16:32:45 -07001464 unsigned long start_pfn = zone->zone_start_pfn;
Cody P Schafer108bcc92013-02-22 16:35:23 -08001465 unsigned long end_pfn = zone_end_pfn(zone);
David Rientjes6d7ce552014-10-09 15:27:27 -07001466 const int migratetype = gfpflags_to_migratetype(cc->gfp_mask);
David Rientjese0b9dae2014-06-04 16:08:28 -07001467 const bool sync = cc->mode != MIGRATE_ASYNC;
Mel Gorman748446b2010-05-24 14:32:27 -07001468
Vlastimil Babkaebff3982014-12-10 15:43:22 -08001469 ret = compaction_suitable(zone, cc->order, cc->alloc_flags,
1470 cc->classzone_idx);
Michal Hockoc46649d2016-05-20 16:56:41 -07001471 /* Compaction is likely to fail */
1472 if (ret == COMPACT_PARTIAL || ret == COMPACT_SKIPPED)
Mel Gorman3e7d3442011-01-13 15:45:56 -08001473 return ret;
Michal Hockoc46649d2016-05-20 16:56:41 -07001474
1475 /* huh, compaction_suitable is returning something unexpected */
1476 VM_BUG_ON(ret != COMPACT_CONTINUE);
Mel Gorman3e7d3442011-01-13 15:45:56 -08001477
Mel Gormanc89511a2012-10-08 16:32:45 -07001478 /*
Vlastimil Babkad3132e42014-01-21 15:51:08 -08001479 * Clear pageblock skip if there were failures recently and compaction
Vlastimil Babkaaccf6242016-03-17 14:18:15 -07001480 * is about to be retried after being deferred.
Vlastimil Babkad3132e42014-01-21 15:51:08 -08001481 */
Vlastimil Babkaaccf6242016-03-17 14:18:15 -07001482 if (compaction_restarting(zone, cc->order))
Vlastimil Babkad3132e42014-01-21 15:51:08 -08001483 __reset_isolation_suitable(zone);
1484
1485 /*
Mel Gormanc89511a2012-10-08 16:32:45 -07001486 * Setup to move all movable pages to the end of the zone. Used cached
1487 * information on where the scanners should start but check that it
1488 * is initialised by ensuring the values are within zone boundaries.
1489 */
David Rientjese0b9dae2014-06-04 16:08:28 -07001490 cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync];
Mel Gormanc89511a2012-10-08 16:32:45 -07001491 cc->free_pfn = zone->compact_cached_free_pfn;
Joonsoo Kim623446e2016-03-15 14:57:45 -07001492 if (cc->free_pfn < start_pfn || cc->free_pfn >= end_pfn) {
Vlastimil Babka06b66402016-05-19 17:11:48 -07001493 cc->free_pfn = pageblock_start_pfn(end_pfn - 1);
Mel Gormanc89511a2012-10-08 16:32:45 -07001494 zone->compact_cached_free_pfn = cc->free_pfn;
1495 }
Joonsoo Kim623446e2016-03-15 14:57:45 -07001496 if (cc->migrate_pfn < start_pfn || cc->migrate_pfn >= end_pfn) {
Mel Gormanc89511a2012-10-08 16:32:45 -07001497 cc->migrate_pfn = start_pfn;
David Rientjes35979ef2014-06-04 16:08:27 -07001498 zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn;
1499 zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn;
Mel Gormanc89511a2012-10-08 16:32:45 -07001500 }
Michal Hockoc8f7de02016-05-20 16:56:47 -07001501
1502 if (cc->migrate_pfn == start_pfn)
1503 cc->whole_zone = true;
1504
Joonsoo Kim1a167182015-09-08 15:03:59 -07001505 cc->last_migrated_pfn = 0;
Mel Gorman748446b2010-05-24 14:32:27 -07001506
Joonsoo Kim16c4a092015-02-11 15:27:01 -08001507 trace_mm_compaction_begin(start_pfn, cc->migrate_pfn,
1508 cc->free_pfn, end_pfn, sync);
Mel Gorman0eb927c2014-01-21 15:51:05 -08001509
Mel Gorman748446b2010-05-24 14:32:27 -07001510 migrate_prep_local();
1511
David Rientjes6d7ce552014-10-09 15:27:27 -07001512 while ((ret = compact_finished(zone, cc, migratetype)) ==
1513 COMPACT_CONTINUE) {
Minchan Kim9d502c12011-03-22 16:30:39 -07001514 int err;
Mel Gorman748446b2010-05-24 14:32:27 -07001515
Mel Gormanf9e35b32011-06-15 15:08:52 -07001516 switch (isolate_migratepages(zone, cc)) {
1517 case ISOLATE_ABORT:
Vlastimil Babka2d1e1042015-11-05 18:48:02 -08001518 ret = COMPACT_CONTENDED;
Rafael Aquini5733c7d2012-12-11 16:02:47 -08001519 putback_movable_pages(&cc->migratepages);
Shaohua Lie64c5232012-10-08 16:32:27 -07001520 cc->nr_migratepages = 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -07001521 goto out;
1522 case ISOLATE_NONE:
Vlastimil Babkafdaf7f52014-12-10 15:43:34 -08001523 /*
1524 * We haven't isolated and migrated anything, but
1525 * there might still be unflushed migrations from
1526 * previous cc->order aligned block.
1527 */
1528 goto check_drain;
Mel Gormanf9e35b32011-06-15 15:08:52 -07001529 case ISOLATE_SUCCESS:
1530 ;
1531 }
Mel Gorman748446b2010-05-24 14:32:27 -07001532
David Rientjesd53aea32014-06-04 16:08:26 -07001533 err = migrate_pages(&cc->migratepages, compaction_alloc,
David Rientjese0b9dae2014-06-04 16:08:28 -07001534 compaction_free, (unsigned long)cc, cc->mode,
Mel Gorman7b2a2d42012-10-19 14:07:31 +01001535 MR_COMPACTION);
Mel Gorman748446b2010-05-24 14:32:27 -07001536
Vlastimil Babkaf8c93012014-06-04 16:08:32 -07001537 trace_mm_compaction_migratepages(cc->nr_migratepages, err,
1538 &cc->migratepages);
Mel Gorman748446b2010-05-24 14:32:27 -07001539
Vlastimil Babkaf8c93012014-06-04 16:08:32 -07001540 /* All pages were either migrated or will be released */
1541 cc->nr_migratepages = 0;
Minchan Kim9d502c12011-03-22 16:30:39 -07001542 if (err) {
Rafael Aquini5733c7d2012-12-11 16:02:47 -08001543 putback_movable_pages(&cc->migratepages);
Vlastimil Babka7ed695e2014-01-21 15:51:09 -08001544 /*
1545 * migrate_pages() may return -ENOMEM when scanners meet
1546 * and we want compact_finished() to detect it
1547 */
Vlastimil Babkaf2849aa2015-09-08 15:02:36 -07001548 if (err == -ENOMEM && !compact_scanners_met(cc)) {
Vlastimil Babka2d1e1042015-11-05 18:48:02 -08001549 ret = COMPACT_CONTENDED;
David Rientjes4bf2bba2012-07-11 14:02:13 -07001550 goto out;
1551 }
Vlastimil Babkafdd048e2016-05-19 17:11:55 -07001552 /*
1553 * We failed to migrate at least one page in the current
1554 * order-aligned block, so skip the rest of it.
1555 */
1556 if (cc->direct_compaction &&
1557 (cc->mode == MIGRATE_ASYNC)) {
1558 cc->migrate_pfn = block_end_pfn(
1559 cc->migrate_pfn - 1, cc->order);
1560 /* Draining pcplists is useless in this case */
1561 cc->last_migrated_pfn = 0;
1562
1563 }
Mel Gorman748446b2010-05-24 14:32:27 -07001564 }
Vlastimil Babkafdaf7f52014-12-10 15:43:34 -08001565
Vlastimil Babkafdaf7f52014-12-10 15:43:34 -08001566check_drain:
1567 /*
1568 * Has the migration scanner moved away from the previous
1569 * cc->order aligned block where we migrated from? If yes,
1570 * flush the pages that were freed, so that they can merge and
1571 * compact_finished() can detect immediately if allocation
1572 * would succeed.
1573 */
Joonsoo Kim1a167182015-09-08 15:03:59 -07001574 if (cc->order > 0 && cc->last_migrated_pfn) {
Vlastimil Babkafdaf7f52014-12-10 15:43:34 -08001575 int cpu;
1576 unsigned long current_block_start =
Vlastimil Babka06b66402016-05-19 17:11:48 -07001577 block_start_pfn(cc->migrate_pfn, cc->order);
Vlastimil Babkafdaf7f52014-12-10 15:43:34 -08001578
Joonsoo Kim1a167182015-09-08 15:03:59 -07001579 if (cc->last_migrated_pfn < current_block_start) {
Vlastimil Babkafdaf7f52014-12-10 15:43:34 -08001580 cpu = get_cpu();
1581 lru_add_drain_cpu(cpu);
1582 drain_local_pages(zone);
1583 put_cpu();
1584 /* No more flushing until we migrate again */
Joonsoo Kim1a167182015-09-08 15:03:59 -07001585 cc->last_migrated_pfn = 0;
Vlastimil Babkafdaf7f52014-12-10 15:43:34 -08001586 }
1587 }
1588
Mel Gorman748446b2010-05-24 14:32:27 -07001589 }
1590
Mel Gormanf9e35b32011-06-15 15:08:52 -07001591out:
Vlastimil Babka6bace092014-12-10 15:43:31 -08001592 /*
1593 * Release free pages and update where the free scanner should restart,
1594 * so we don't leave any returned pages behind in the next attempt.
1595 */
1596 if (cc->nr_freepages > 0) {
1597 unsigned long free_pfn = release_freepages(&cc->freepages);
1598
1599 cc->nr_freepages = 0;
1600 VM_BUG_ON(free_pfn == 0);
1601 /* The cached pfn is always the first in a pageblock */
Vlastimil Babka06b66402016-05-19 17:11:48 -07001602 free_pfn = pageblock_start_pfn(free_pfn);
Vlastimil Babka6bace092014-12-10 15:43:31 -08001603 /*
1604 * Only go back, not forward. The cached pfn might have been
1605 * already reset to zone end in compact_finished()
1606 */
1607 if (free_pfn > zone->compact_cached_free_pfn)
1608 zone->compact_cached_free_pfn = free_pfn;
1609 }
Mel Gorman748446b2010-05-24 14:32:27 -07001610
Joonsoo Kim16c4a092015-02-11 15:27:01 -08001611 trace_mm_compaction_end(start_pfn, cc->migrate_pfn,
1612 cc->free_pfn, end_pfn, sync, ret);
Mel Gorman0eb927c2014-01-21 15:51:05 -08001613
Vlastimil Babka2d1e1042015-11-05 18:48:02 -08001614 if (ret == COMPACT_CONTENDED)
1615 ret = COMPACT_PARTIAL;
1616
Mel Gorman748446b2010-05-24 14:32:27 -07001617 return ret;
1618}
Mel Gorman76ab0f52010-05-24 14:32:28 -07001619
Michal Hockoea7ab982016-05-20 16:56:38 -07001620static enum compact_result compact_zone_order(struct zone *zone, int order,
Vlastimil Babkaebff3982014-12-10 15:43:22 -08001621 gfp_t gfp_mask, enum migrate_mode mode, int *contended,
Mel Gormanc6038442016-05-19 17:13:38 -07001622 unsigned int alloc_flags, int classzone_idx)
Mel Gorman56de7262010-05-24 14:32:30 -07001623{
Michal Hockoea7ab982016-05-20 16:56:38 -07001624 enum compact_result ret;
Mel Gorman56de7262010-05-24 14:32:30 -07001625 struct compact_control cc = {
1626 .nr_freepages = 0,
1627 .nr_migratepages = 0,
1628 .order = order,
David Rientjes6d7ce552014-10-09 15:27:27 -07001629 .gfp_mask = gfp_mask,
Mel Gorman56de7262010-05-24 14:32:30 -07001630 .zone = zone,
David Rientjese0b9dae2014-06-04 16:08:28 -07001631 .mode = mode,
Vlastimil Babkaebff3982014-12-10 15:43:22 -08001632 .alloc_flags = alloc_flags,
1633 .classzone_idx = classzone_idx,
Vlastimil Babkaaccf6242016-03-17 14:18:15 -07001634 .direct_compaction = true,
Mel Gorman56de7262010-05-24 14:32:30 -07001635 };
1636 INIT_LIST_HEAD(&cc.freepages);
1637 INIT_LIST_HEAD(&cc.migratepages);
1638
Shaohua Lie64c5232012-10-08 16:32:27 -07001639 ret = compact_zone(zone, &cc);
1640
1641 VM_BUG_ON(!list_empty(&cc.freepages));
1642 VM_BUG_ON(!list_empty(&cc.migratepages));
1643
1644 *contended = cc.contended;
1645 return ret;
Mel Gorman56de7262010-05-24 14:32:30 -07001646}
1647
Mel Gorman5e771902010-05-24 14:32:31 -07001648int sysctl_extfrag_threshold = 500;
1649
Mel Gorman56de7262010-05-24 14:32:30 -07001650/**
1651 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
Mel Gorman56de7262010-05-24 14:32:30 -07001652 * @gfp_mask: The GFP mask of the current allocation
Vlastimil Babka1a6d53a2015-02-11 15:25:44 -08001653 * @order: The order of the current allocation
1654 * @alloc_flags: The allocation flags of the current allocation
1655 * @ac: The context of current allocation
David Rientjese0b9dae2014-06-04 16:08:28 -07001656 * @mode: The migration mode for async, sync light, or sync migration
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001657 * @contended: Return value that determines if compaction was aborted due to
1658 * need_resched() or lock contention
Mel Gorman56de7262010-05-24 14:32:30 -07001659 *
1660 * This is the main entry point for direct page compaction.
1661 */
Michal Hockoea7ab982016-05-20 16:56:38 -07001662enum compact_result try_to_compact_pages(gfp_t gfp_mask, unsigned int order,
Mel Gormanc6038442016-05-19 17:13:38 -07001663 unsigned int alloc_flags, const struct alloc_context *ac,
1664 enum migrate_mode mode, int *contended)
Mel Gorman56de7262010-05-24 14:32:30 -07001665{
Mel Gorman56de7262010-05-24 14:32:30 -07001666 int may_enter_fs = gfp_mask & __GFP_FS;
1667 int may_perform_io = gfp_mask & __GFP_IO;
Mel Gorman56de7262010-05-24 14:32:30 -07001668 struct zoneref *z;
1669 struct zone *zone;
Michal Hocko1d4746d2016-05-20 16:56:44 -07001670 enum compact_result rc = COMPACT_SKIPPED;
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001671 int all_zones_contended = COMPACT_CONTENDED_LOCK; /* init for &= op */
1672
1673 *contended = COMPACT_CONTENDED_NONE;
Mel Gorman56de7262010-05-24 14:32:30 -07001674
Mel Gorman4ffb6332012-10-08 16:29:09 -07001675 /* Check if the GFP flags allow compaction */
Andrea Arcangelic5a73c32011-01-13 15:47:11 -08001676 if (!order || !may_enter_fs || !may_perform_io)
Vlastimil Babka53853e22014-10-09 15:27:02 -07001677 return COMPACT_SKIPPED;
Mel Gorman56de7262010-05-24 14:32:30 -07001678
Joonsoo Kim837d0262015-02-11 15:27:06 -08001679 trace_mm_compaction_try_to_compact_pages(order, gfp_mask, mode);
1680
Mel Gorman56de7262010-05-24 14:32:30 -07001681 /* Compact each zone in the list */
Vlastimil Babka1a6d53a2015-02-11 15:25:44 -08001682 for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx,
1683 ac->nodemask) {
Michal Hockoea7ab982016-05-20 16:56:38 -07001684 enum compact_result status;
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001685 int zone_contended;
Mel Gorman56de7262010-05-24 14:32:30 -07001686
Michal Hocko1d4746d2016-05-20 16:56:44 -07001687 if (compaction_deferred(zone, order)) {
1688 rc = max_t(enum compact_result, COMPACT_DEFERRED, rc);
Vlastimil Babka53853e22014-10-09 15:27:02 -07001689 continue;
Michal Hocko1d4746d2016-05-20 16:56:44 -07001690 }
Vlastimil Babka53853e22014-10-09 15:27:02 -07001691
David Rientjese0b9dae2014-06-04 16:08:28 -07001692 status = compact_zone_order(zone, order, gfp_mask, mode,
Vlastimil Babka1a6d53a2015-02-11 15:25:44 -08001693 &zone_contended, alloc_flags,
Mel Gorman93ea9962016-05-19 17:14:13 -07001694 ac_classzone_idx(ac));
Mel Gorman56de7262010-05-24 14:32:30 -07001695 rc = max(status, rc);
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001696 /*
1697 * It takes at least one zone that wasn't lock contended
1698 * to clear all_zones_contended.
1699 */
1700 all_zones_contended &= zone_contended;
Mel Gorman56de7262010-05-24 14:32:30 -07001701
Mel Gorman3e7d3442011-01-13 15:45:56 -08001702 /* If a normal allocation would succeed, stop compacting */
Vlastimil Babkaebff3982014-12-10 15:43:22 -08001703 if (zone_watermark_ok(zone, order, low_wmark_pages(zone),
Mel Gorman93ea9962016-05-19 17:14:13 -07001704 ac_classzone_idx(ac), alloc_flags)) {
Vlastimil Babka53853e22014-10-09 15:27:02 -07001705 /*
1706 * We think the allocation will succeed in this zone,
1707 * but it is not certain, hence the false. The caller
1708 * will repeat this with true if allocation indeed
1709 * succeeds in this zone.
1710 */
1711 compaction_defer_reset(zone, order, false);
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001712 /*
1713 * It is possible that async compaction aborted due to
1714 * need_resched() and the watermarks were ok thanks to
1715 * somebody else freeing memory. The allocation can
1716 * however still fail so we better signal the
1717 * need_resched() contention anyway (this will not
1718 * prevent the allocation attempt).
1719 */
1720 if (zone_contended == COMPACT_CONTENDED_SCHED)
1721 *contended = COMPACT_CONTENDED_SCHED;
1722
1723 goto break_loop;
1724 }
1725
Michal Hockoc8f7de02016-05-20 16:56:47 -07001726 if (mode != MIGRATE_ASYNC && (status == COMPACT_COMPLETE ||
1727 status == COMPACT_PARTIAL_SKIPPED)) {
Vlastimil Babka53853e22014-10-09 15:27:02 -07001728 /*
1729 * We think that allocation won't succeed in this zone
1730 * so we defer compaction there. If it ends up
1731 * succeeding after all, it will be reset.
1732 */
1733 defer_compaction(zone, order);
1734 }
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001735
1736 /*
1737 * We might have stopped compacting due to need_resched() in
1738 * async compaction, or due to a fatal signal detected. In that
1739 * case do not try further zones and signal need_resched()
1740 * contention.
1741 */
1742 if ((zone_contended == COMPACT_CONTENDED_SCHED)
1743 || fatal_signal_pending(current)) {
1744 *contended = COMPACT_CONTENDED_SCHED;
1745 goto break_loop;
1746 }
1747
1748 continue;
1749break_loop:
1750 /*
1751 * We might not have tried all the zones, so be conservative
1752 * and assume they are not all lock contended.
1753 */
1754 all_zones_contended = 0;
1755 break;
Mel Gorman56de7262010-05-24 14:32:30 -07001756 }
1757
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001758 /*
1759 * If at least one zone wasn't deferred or skipped, we report if all
1760 * zones that were tried were lock contended.
1761 */
Michal Hocko1d4746d2016-05-20 16:56:44 -07001762 if (rc > COMPACT_INACTIVE && all_zones_contended)
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07001763 *contended = COMPACT_CONTENDED_LOCK;
1764
Mel Gorman56de7262010-05-24 14:32:30 -07001765 return rc;
1766}
1767
1768
Mel Gorman76ab0f52010-05-24 14:32:28 -07001769/* Compact all zones within a node */
Andrew Morton7103f162013-02-22 16:32:33 -08001770static void __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001771{
1772 int zoneid;
Mel Gorman76ab0f52010-05-24 14:32:28 -07001773 struct zone *zone;
1774
Mel Gorman76ab0f52010-05-24 14:32:28 -07001775 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
Mel Gorman76ab0f52010-05-24 14:32:28 -07001776
1777 zone = &pgdat->node_zones[zoneid];
1778 if (!populated_zone(zone))
1779 continue;
1780
Rik van Riel7be62de2012-03-21 16:33:52 -07001781 cc->nr_freepages = 0;
1782 cc->nr_migratepages = 0;
1783 cc->zone = zone;
1784 INIT_LIST_HEAD(&cc->freepages);
1785 INIT_LIST_HEAD(&cc->migratepages);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001786
Gioh Kim195b0c62015-04-15 16:13:33 -07001787 /*
1788 * When called via /proc/sys/vm/compact_memory
1789 * this makes sure we compact the whole zone regardless of
1790 * cached scanner positions.
1791 */
Yaowei Bai21c527a2015-11-05 18:47:20 -08001792 if (is_via_compact_memory(cc->order))
Gioh Kim195b0c62015-04-15 16:13:33 -07001793 __reset_isolation_suitable(zone);
1794
Yaowei Bai21c527a2015-11-05 18:47:20 -08001795 if (is_via_compact_memory(cc->order) ||
1796 !compaction_deferred(zone, cc->order))
Rik van Riel7be62de2012-03-21 16:33:52 -07001797 compact_zone(zone, cc);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001798
Rik van Riel7be62de2012-03-21 16:33:52 -07001799 VM_BUG_ON(!list_empty(&cc->freepages));
1800 VM_BUG_ON(!list_empty(&cc->migratepages));
Joonsoo Kim75469342016-01-14 15:20:48 -08001801
1802 if (is_via_compact_memory(cc->order))
1803 continue;
1804
1805 if (zone_watermark_ok(zone, cc->order,
1806 low_wmark_pages(zone), 0, 0))
1807 compaction_defer_reset(zone, cc->order, false);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001808 }
Mel Gorman76ab0f52010-05-24 14:32:28 -07001809}
1810
Andrew Morton7103f162013-02-22 16:32:33 -08001811void compact_pgdat(pg_data_t *pgdat, int order)
Rik van Riel7be62de2012-03-21 16:33:52 -07001812{
1813 struct compact_control cc = {
1814 .order = order,
David Rientjese0b9dae2014-06-04 16:08:28 -07001815 .mode = MIGRATE_ASYNC,
Rik van Riel7be62de2012-03-21 16:33:52 -07001816 };
1817
Mel Gorman3a7200a2013-09-11 14:22:19 -07001818 if (!order)
1819 return;
1820
Andrew Morton7103f162013-02-22 16:32:33 -08001821 __compact_pgdat(pgdat, &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -07001822}
1823
Andrew Morton7103f162013-02-22 16:32:33 -08001824static void compact_node(int nid)
Rik van Riel7be62de2012-03-21 16:33:52 -07001825{
Rik van Riel7be62de2012-03-21 16:33:52 -07001826 struct compact_control cc = {
1827 .order = -1,
David Rientjese0b9dae2014-06-04 16:08:28 -07001828 .mode = MIGRATE_SYNC,
David Rientjes91ca9182014-04-03 14:47:23 -07001829 .ignore_skip_hint = true,
Rik van Riel7be62de2012-03-21 16:33:52 -07001830 };
1831
Andrew Morton7103f162013-02-22 16:32:33 -08001832 __compact_pgdat(NODE_DATA(nid), &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -07001833}
1834
Mel Gorman76ab0f52010-05-24 14:32:28 -07001835/* Compact all nodes in the system */
Jason Liu7964c062013-01-11 14:31:47 -08001836static void compact_nodes(void)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001837{
1838 int nid;
1839
Hugh Dickins8575ec22012-03-21 16:33:53 -07001840 /* Flush pending updates to the LRU lists */
1841 lru_add_drain_all();
1842
Mel Gorman76ab0f52010-05-24 14:32:28 -07001843 for_each_online_node(nid)
1844 compact_node(nid);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001845}
1846
1847/* The written value is actually unused, all memory is compacted */
1848int sysctl_compact_memory;
1849
Yaowei Baifec4eb22016-01-14 15:20:09 -08001850/*
1851 * This is the entry point for compacting all nodes via
1852 * /proc/sys/vm/compact_memory
1853 */
Mel Gorman76ab0f52010-05-24 14:32:28 -07001854int sysctl_compaction_handler(struct ctl_table *table, int write,
1855 void __user *buffer, size_t *length, loff_t *ppos)
1856{
1857 if (write)
Jason Liu7964c062013-01-11 14:31:47 -08001858 compact_nodes();
Mel Gorman76ab0f52010-05-24 14:32:28 -07001859
1860 return 0;
1861}
Mel Gormaned4a6d72010-05-24 14:32:29 -07001862
Mel Gorman5e771902010-05-24 14:32:31 -07001863int sysctl_extfrag_handler(struct ctl_table *table, int write,
1864 void __user *buffer, size_t *length, loff_t *ppos)
1865{
1866 proc_dointvec_minmax(table, write, buffer, length, ppos);
1867
1868 return 0;
1869}
1870
Mel Gormaned4a6d72010-05-24 14:32:29 -07001871#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
Rashika Kheria74e77fb2014-04-03 14:48:01 -07001872static ssize_t sysfs_compact_node(struct device *dev,
Kay Sievers10fbcf42011-12-21 14:48:43 -08001873 struct device_attribute *attr,
Mel Gormaned4a6d72010-05-24 14:32:29 -07001874 const char *buf, size_t count)
1875{
Hugh Dickins8575ec22012-03-21 16:33:53 -07001876 int nid = dev->id;
1877
1878 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
1879 /* Flush pending updates to the LRU lists */
1880 lru_add_drain_all();
1881
1882 compact_node(nid);
1883 }
Mel Gormaned4a6d72010-05-24 14:32:29 -07001884
1885 return count;
1886}
Kay Sievers10fbcf42011-12-21 14:48:43 -08001887static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001888
1889int compaction_register_node(struct node *node)
1890{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001891 return device_create_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001892}
1893
1894void compaction_unregister_node(struct node *node)
1895{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001896 return device_remove_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001897}
1898#endif /* CONFIG_SYSFS && CONFIG_NUMA */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001899
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001900static inline bool kcompactd_work_requested(pg_data_t *pgdat)
1901{
Vlastimil Babka172400c2016-05-05 16:22:32 -07001902 return pgdat->kcompactd_max_order > 0 || kthread_should_stop();
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001903}
1904
1905static bool kcompactd_node_suitable(pg_data_t *pgdat)
1906{
1907 int zoneid;
1908 struct zone *zone;
1909 enum zone_type classzone_idx = pgdat->kcompactd_classzone_idx;
1910
Chen Feng6cd9dc32016-05-20 16:59:02 -07001911 for (zoneid = 0; zoneid <= classzone_idx; zoneid++) {
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001912 zone = &pgdat->node_zones[zoneid];
1913
1914 if (!populated_zone(zone))
1915 continue;
1916
1917 if (compaction_suitable(zone, pgdat->kcompactd_max_order, 0,
1918 classzone_idx) == COMPACT_CONTINUE)
1919 return true;
1920 }
1921
1922 return false;
1923}
1924
1925static void kcompactd_do_work(pg_data_t *pgdat)
1926{
1927 /*
1928 * With no special task, compact all zones so that a page of requested
1929 * order is allocatable.
1930 */
1931 int zoneid;
1932 struct zone *zone;
1933 struct compact_control cc = {
1934 .order = pgdat->kcompactd_max_order,
1935 .classzone_idx = pgdat->kcompactd_classzone_idx,
1936 .mode = MIGRATE_SYNC_LIGHT,
1937 .ignore_skip_hint = true,
1938
1939 };
1940 bool success = false;
1941
1942 trace_mm_compaction_kcompactd_wake(pgdat->node_id, cc.order,
1943 cc.classzone_idx);
1944 count_vm_event(KCOMPACTD_WAKE);
1945
Chen Feng6cd9dc32016-05-20 16:59:02 -07001946 for (zoneid = 0; zoneid <= cc.classzone_idx; zoneid++) {
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001947 int status;
1948
1949 zone = &pgdat->node_zones[zoneid];
1950 if (!populated_zone(zone))
1951 continue;
1952
1953 if (compaction_deferred(zone, cc.order))
1954 continue;
1955
1956 if (compaction_suitable(zone, cc.order, 0, zoneid) !=
1957 COMPACT_CONTINUE)
1958 continue;
1959
1960 cc.nr_freepages = 0;
1961 cc.nr_migratepages = 0;
1962 cc.zone = zone;
1963 INIT_LIST_HEAD(&cc.freepages);
1964 INIT_LIST_HEAD(&cc.migratepages);
1965
Vlastimil Babka172400c2016-05-05 16:22:32 -07001966 if (kthread_should_stop())
1967 return;
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001968 status = compact_zone(zone, &cc);
1969
1970 if (zone_watermark_ok(zone, cc.order, low_wmark_pages(zone),
1971 cc.classzone_idx, 0)) {
1972 success = true;
1973 compaction_defer_reset(zone, cc.order, false);
Michal Hockoc8f7de02016-05-20 16:56:47 -07001974 } else if (status == COMPACT_PARTIAL_SKIPPED || status == COMPACT_COMPLETE) {
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001975 /*
1976 * We use sync migration mode here, so we defer like
1977 * sync direct compaction does.
1978 */
1979 defer_compaction(zone, cc.order);
1980 }
1981
1982 VM_BUG_ON(!list_empty(&cc.freepages));
1983 VM_BUG_ON(!list_empty(&cc.migratepages));
1984 }
1985
1986 /*
1987 * Regardless of success, we are done until woken up next. But remember
1988 * the requested order/classzone_idx in case it was higher/tighter than
1989 * our current ones
1990 */
1991 if (pgdat->kcompactd_max_order <= cc.order)
1992 pgdat->kcompactd_max_order = 0;
1993 if (pgdat->kcompactd_classzone_idx >= cc.classzone_idx)
1994 pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1;
1995}
1996
1997void wakeup_kcompactd(pg_data_t *pgdat, int order, int classzone_idx)
1998{
1999 if (!order)
2000 return;
2001
2002 if (pgdat->kcompactd_max_order < order)
2003 pgdat->kcompactd_max_order = order;
2004
2005 if (pgdat->kcompactd_classzone_idx > classzone_idx)
2006 pgdat->kcompactd_classzone_idx = classzone_idx;
2007
2008 if (!waitqueue_active(&pgdat->kcompactd_wait))
2009 return;
2010
2011 if (!kcompactd_node_suitable(pgdat))
2012 return;
2013
2014 trace_mm_compaction_wakeup_kcompactd(pgdat->node_id, order,
2015 classzone_idx);
2016 wake_up_interruptible(&pgdat->kcompactd_wait);
2017}
2018
2019/*
2020 * The background compaction daemon, started as a kernel thread
2021 * from the init process.
2022 */
2023static int kcompactd(void *p)
2024{
2025 pg_data_t *pgdat = (pg_data_t*)p;
2026 struct task_struct *tsk = current;
2027
2028 const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
2029
2030 if (!cpumask_empty(cpumask))
2031 set_cpus_allowed_ptr(tsk, cpumask);
2032
2033 set_freezable();
2034
2035 pgdat->kcompactd_max_order = 0;
2036 pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1;
2037
2038 while (!kthread_should_stop()) {
2039 trace_mm_compaction_kcompactd_sleep(pgdat->node_id);
2040 wait_event_freezable(pgdat->kcompactd_wait,
2041 kcompactd_work_requested(pgdat));
2042
2043 kcompactd_do_work(pgdat);
2044 }
2045
2046 return 0;
2047}
2048
2049/*
2050 * This kcompactd start function will be called by init and node-hot-add.
2051 * On node-hot-add, kcompactd will moved to proper cpus if cpus are hot-added.
2052 */
2053int kcompactd_run(int nid)
2054{
2055 pg_data_t *pgdat = NODE_DATA(nid);
2056 int ret = 0;
2057
2058 if (pgdat->kcompactd)
2059 return 0;
2060
2061 pgdat->kcompactd = kthread_run(kcompactd, pgdat, "kcompactd%d", nid);
2062 if (IS_ERR(pgdat->kcompactd)) {
2063 pr_err("Failed to start kcompactd on node %d\n", nid);
2064 ret = PTR_ERR(pgdat->kcompactd);
2065 pgdat->kcompactd = NULL;
2066 }
2067 return ret;
2068}
2069
2070/*
2071 * Called by memory hotplug when all memory in a node is offlined. Caller must
2072 * hold mem_hotplug_begin/end().
2073 */
2074void kcompactd_stop(int nid)
2075{
2076 struct task_struct *kcompactd = NODE_DATA(nid)->kcompactd;
2077
2078 if (kcompactd) {
2079 kthread_stop(kcompactd);
2080 NODE_DATA(nid)->kcompactd = NULL;
2081 }
2082}
2083
2084/*
2085 * It's optimal to keep kcompactd on the same CPUs as their memory, but
2086 * not required for correctness. So if the last cpu in a node goes
2087 * away, we get changed to run anywhere: as the first one comes back,
2088 * restore their cpu bindings.
2089 */
2090static int cpu_callback(struct notifier_block *nfb, unsigned long action,
2091 void *hcpu)
2092{
2093 int nid;
2094
2095 if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) {
2096 for_each_node_state(nid, N_MEMORY) {
2097 pg_data_t *pgdat = NODE_DATA(nid);
2098 const struct cpumask *mask;
2099
2100 mask = cpumask_of_node(pgdat->node_id);
2101
2102 if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
2103 /* One of our CPUs online: restore mask */
2104 set_cpus_allowed_ptr(pgdat->kcompactd, mask);
2105 }
2106 }
2107 return NOTIFY_OK;
2108}
2109
2110static int __init kcompactd_init(void)
2111{
2112 int nid;
2113
2114 for_each_node_state(nid, N_MEMORY)
2115 kcompactd_run(nid);
2116 hotcpu_notifier(cpu_callback, 0);
2117 return 0;
2118}
2119subsys_initcall(kcompactd_init)
2120
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01002121#endif /* CONFIG_COMPACTION */