Commit | Line | Data |
---|---|---|
748446bb MG |
1 | /* |
2 | * linux/mm/compaction.c | |
3 | * | |
4 | * Memory compaction for the reduction of external fragmentation. Note that | |
5 | * this heavily depends upon page migration to do all the real heavy | |
6 | * lifting | |
7 | * | |
8 | * Copyright IBM Corp. 2007-2010 Mel Gorman <mel@csn.ul.ie> | |
9 | */ | |
10 | #include <linux/swap.h> | |
11 | #include <linux/migrate.h> | |
12 | #include <linux/compaction.h> | |
13 | #include <linux/mm_inline.h> | |
14 | #include <linux/backing-dev.h> | |
76ab0f53 | 15 | #include <linux/sysctl.h> |
ed4a6d7f | 16 | #include <linux/sysfs.h> |
748446bb MG |
17 | #include "internal.h" |
18 | ||
ff9543fd MN |
19 | #if defined CONFIG_COMPACTION || defined CONFIG_CMA |
20 | ||
b7aba698 MG |
21 | #define CREATE_TRACE_POINTS |
22 | #include <trace/events/compaction.h> | |
23 | ||
748446bb MG |
24 | static unsigned long release_freepages(struct list_head *freelist) |
25 | { | |
26 | struct page *page, *next; | |
27 | unsigned long count = 0; | |
28 | ||
29 | list_for_each_entry_safe(page, next, freelist, lru) { | |
30 | list_del(&page->lru); | |
31 | __free_page(page); | |
32 | count++; | |
33 | } | |
34 | ||
35 | return count; | |
36 | } | |
37 | ||
ff9543fd MN |
38 | static void map_pages(struct list_head *list) |
39 | { | |
40 | struct page *page; | |
41 | ||
42 | list_for_each_entry(page, list, lru) { | |
43 | arch_alloc_page(page, 0); | |
44 | kernel_map_pages(page, 1, 1); | |
45 | } | |
46 | } | |
47 | ||
47118af0 MN |
48 | static inline bool migrate_async_suitable(int migratetype) |
49 | { | |
50 | return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE; | |
51 | } | |
52 | ||
bb13ffeb MG |
53 | #ifdef CONFIG_COMPACTION |
54 | /* Returns true if the pageblock should be scanned for pages to isolate. */ | |
55 | static inline bool isolation_suitable(struct compact_control *cc, | |
56 | struct page *page) | |
57 | { | |
58 | if (cc->ignore_skip_hint) | |
59 | return true; | |
60 | ||
61 | return !get_pageblock_skip(page); | |
62 | } | |
63 | ||
64 | /* | |
65 | * This function is called to clear all cached information on pageblocks that | |
66 | * should be skipped for page isolation when the migrate and free page scanner | |
67 | * meet. | |
68 | */ | |
62997027 | 69 | static void __reset_isolation_suitable(struct zone *zone) |
bb13ffeb MG |
70 | { |
71 | unsigned long start_pfn = zone->zone_start_pfn; | |
72 | unsigned long end_pfn = zone->zone_start_pfn + zone->spanned_pages; | |
73 | unsigned long pfn; | |
74 | ||
c89511ab MG |
75 | zone->compact_cached_migrate_pfn = start_pfn; |
76 | zone->compact_cached_free_pfn = end_pfn; | |
62997027 | 77 | zone->compact_blockskip_flush = false; |
bb13ffeb MG |
78 | |
79 | /* Walk the zone and mark every pageblock as suitable for isolation */ | |
80 | for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) { | |
81 | struct page *page; | |
82 | ||
83 | cond_resched(); | |
84 | ||
85 | if (!pfn_valid(pfn)) | |
86 | continue; | |
87 | ||
88 | page = pfn_to_page(pfn); | |
89 | if (zone != page_zone(page)) | |
90 | continue; | |
91 | ||
92 | clear_pageblock_skip(page); | |
93 | } | |
94 | } | |
95 | ||
62997027 MG |
96 | void reset_isolation_suitable(pg_data_t *pgdat) |
97 | { | |
98 | int zoneid; | |
99 | ||
100 | for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { | |
101 | struct zone *zone = &pgdat->node_zones[zoneid]; | |
102 | if (!populated_zone(zone)) | |
103 | continue; | |
104 | ||
105 | /* Only flush if a full compaction finished recently */ | |
106 | if (zone->compact_blockskip_flush) | |
107 | __reset_isolation_suitable(zone); | |
108 | } | |
109 | } | |
110 | ||
bb13ffeb MG |
111 | /* |
112 | * If no pages were isolated then mark this pageblock to be skipped in the | |
62997027 | 113 | * future. The information is later cleared by __reset_isolation_suitable(). |
bb13ffeb | 114 | */ |
c89511ab MG |
115 | static void update_pageblock_skip(struct compact_control *cc, |
116 | struct page *page, unsigned long nr_isolated, | |
117 | bool migrate_scanner) | |
bb13ffeb | 118 | { |
c89511ab | 119 | struct zone *zone = cc->zone; |
bb13ffeb MG |
120 | if (!page) |
121 | return; | |
122 | ||
c89511ab MG |
123 | if (!nr_isolated) { |
124 | unsigned long pfn = page_to_pfn(page); | |
bb13ffeb | 125 | set_pageblock_skip(page); |
c89511ab MG |
126 | |
127 | /* Update where compaction should restart */ | |
128 | if (migrate_scanner) { | |
129 | if (!cc->finished_update_migrate && | |
130 | pfn > zone->compact_cached_migrate_pfn) | |
131 | zone->compact_cached_migrate_pfn = pfn; | |
132 | } else { | |
133 | if (!cc->finished_update_free && | |
134 | pfn < zone->compact_cached_free_pfn) | |
135 | zone->compact_cached_free_pfn = pfn; | |
136 | } | |
137 | } | |
bb13ffeb MG |
138 | } |
139 | #else | |
140 | static inline bool isolation_suitable(struct compact_control *cc, | |
141 | struct page *page) | |
142 | { | |
143 | return true; | |
144 | } | |
145 | ||
c89511ab MG |
146 | static void update_pageblock_skip(struct compact_control *cc, |
147 | struct page *page, unsigned long nr_isolated, | |
148 | bool migrate_scanner) | |
bb13ffeb MG |
149 | { |
150 | } | |
151 | #endif /* CONFIG_COMPACTION */ | |
152 | ||
2a1402aa MG |
153 | static inline bool should_release_lock(spinlock_t *lock) |
154 | { | |
155 | return need_resched() || spin_is_contended(lock); | |
156 | } | |
157 | ||
c67fe375 MG |
158 | /* |
159 | * Compaction requires the taking of some coarse locks that are potentially | |
160 | * very heavily contended. Check if the process needs to be scheduled or | |
161 | * if the lock is contended. For async compaction, back out in the event | |
162 | * if contention is severe. For sync compaction, schedule. | |
163 | * | |
164 | * Returns true if the lock is held. | |
165 | * Returns false if the lock is released and compaction should abort | |
166 | */ | |
167 | static bool compact_checklock_irqsave(spinlock_t *lock, unsigned long *flags, | |
168 | bool locked, struct compact_control *cc) | |
169 | { | |
2a1402aa | 170 | if (should_release_lock(lock)) { |
c67fe375 MG |
171 | if (locked) { |
172 | spin_unlock_irqrestore(lock, *flags); | |
173 | locked = false; | |
174 | } | |
175 | ||
176 | /* async aborts if taking too long or contended */ | |
177 | if (!cc->sync) { | |
e64c5237 | 178 | cc->contended = true; |
c67fe375 MG |
179 | return false; |
180 | } | |
181 | ||
182 | cond_resched(); | |
c67fe375 MG |
183 | } |
184 | ||
185 | if (!locked) | |
186 | spin_lock_irqsave(lock, *flags); | |
187 | return true; | |
188 | } | |
189 | ||
190 | static inline bool compact_trylock_irqsave(spinlock_t *lock, | |
191 | unsigned long *flags, struct compact_control *cc) | |
192 | { | |
193 | return compact_checklock_irqsave(lock, flags, false, cc); | |
194 | } | |
195 | ||
f40d1e42 MG |
196 | /* Returns true if the page is within a block suitable for migration to */ |
197 | static bool suitable_migration_target(struct page *page) | |
198 | { | |
199 | int migratetype = get_pageblock_migratetype(page); | |
200 | ||
201 | /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */ | |
202 | if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE) | |
203 | return false; | |
204 | ||
205 | /* If the page is a large free page, then allow migration */ | |
206 | if (PageBuddy(page) && page_order(page) >= pageblock_order) | |
207 | return true; | |
208 | ||
209 | /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */ | |
210 | if (migrate_async_suitable(migratetype)) | |
211 | return true; | |
212 | ||
213 | /* Otherwise skip the block */ | |
214 | return false; | |
215 | } | |
216 | ||
1fb3f8ca MG |
217 | static void compact_capture_page(struct compact_control *cc) |
218 | { | |
219 | unsigned long flags; | |
220 | int mtype, mtype_low, mtype_high; | |
221 | ||
222 | if (!cc->page || *cc->page) | |
223 | return; | |
224 | ||
225 | /* | |
226 | * For MIGRATE_MOVABLE allocations we capture a suitable page ASAP | |
227 | * regardless of the migratetype of the freelist is is captured from. | |
228 | * This is fine because the order for a high-order MIGRATE_MOVABLE | |
229 | * allocation is typically at least a pageblock size and overall | |
230 | * fragmentation is not impaired. Other allocation types must | |
231 | * capture pages from their own migratelist because otherwise they | |
232 | * could pollute other pageblocks like MIGRATE_MOVABLE with | |
233 | * difficult to move pages and making fragmentation worse overall. | |
234 | */ | |
235 | if (cc->migratetype == MIGRATE_MOVABLE) { | |
236 | mtype_low = 0; | |
237 | mtype_high = MIGRATE_PCPTYPES; | |
238 | } else { | |
239 | mtype_low = cc->migratetype; | |
240 | mtype_high = cc->migratetype + 1; | |
241 | } | |
242 | ||
243 | /* Speculatively examine the free lists without zone lock */ | |
244 | for (mtype = mtype_low; mtype < mtype_high; mtype++) { | |
245 | int order; | |
246 | for (order = cc->order; order < MAX_ORDER; order++) { | |
247 | struct page *page; | |
248 | struct free_area *area; | |
249 | area = &(cc->zone->free_area[order]); | |
250 | if (list_empty(&area->free_list[mtype])) | |
251 | continue; | |
252 | ||
253 | /* Take the lock and attempt capture of the page */ | |
254 | if (!compact_trylock_irqsave(&cc->zone->lock, &flags, cc)) | |
255 | return; | |
256 | if (!list_empty(&area->free_list[mtype])) { | |
257 | page = list_entry(area->free_list[mtype].next, | |
258 | struct page, lru); | |
259 | if (capture_free_page(page, cc->order, mtype)) { | |
260 | spin_unlock_irqrestore(&cc->zone->lock, | |
261 | flags); | |
262 | *cc->page = page; | |
263 | return; | |
264 | } | |
265 | } | |
266 | spin_unlock_irqrestore(&cc->zone->lock, flags); | |
267 | } | |
268 | } | |
269 | } | |
270 | ||
85aa125f MN |
271 | /* |
272 | * Isolate free pages onto a private freelist. Caller must hold zone->lock. | |
273 | * If @strict is true, will abort returning 0 on any invalid PFNs or non-free | |
274 | * pages inside of the pageblock (even though it may still end up isolating | |
275 | * some pages). | |
276 | */ | |
f40d1e42 MG |
277 | static unsigned long isolate_freepages_block(struct compact_control *cc, |
278 | unsigned long blockpfn, | |
85aa125f MN |
279 | unsigned long end_pfn, |
280 | struct list_head *freelist, | |
281 | bool strict) | |
748446bb | 282 | { |
b7aba698 | 283 | int nr_scanned = 0, total_isolated = 0; |
bb13ffeb | 284 | struct page *cursor, *valid_page = NULL; |
f40d1e42 MG |
285 | unsigned long nr_strict_required = end_pfn - blockpfn; |
286 | unsigned long flags; | |
287 | bool locked = false; | |
748446bb | 288 | |
748446bb MG |
289 | cursor = pfn_to_page(blockpfn); |
290 | ||
f40d1e42 | 291 | /* Isolate free pages. */ |
748446bb MG |
292 | for (; blockpfn < end_pfn; blockpfn++, cursor++) { |
293 | int isolated, i; | |
294 | struct page *page = cursor; | |
295 | ||
b7aba698 | 296 | nr_scanned++; |
f40d1e42 MG |
297 | if (!pfn_valid_within(blockpfn)) |
298 | continue; | |
bb13ffeb MG |
299 | if (!valid_page) |
300 | valid_page = page; | |
f40d1e42 MG |
301 | if (!PageBuddy(page)) |
302 | continue; | |
303 | ||
304 | /* | |
305 | * The zone lock must be held to isolate freepages. | |
306 | * Unfortunately this is a very coarse lock and can be | |
307 | * heavily contended if there are parallel allocations | |
308 | * or parallel compactions. For async compaction do not | |
309 | * spin on the lock and we acquire the lock as late as | |
310 | * possible. | |
311 | */ | |
312 | locked = compact_checklock_irqsave(&cc->zone->lock, &flags, | |
313 | locked, cc); | |
314 | if (!locked) | |
315 | break; | |
316 | ||
317 | /* Recheck this is a suitable migration target under lock */ | |
318 | if (!strict && !suitable_migration_target(page)) | |
319 | break; | |
748446bb | 320 | |
f40d1e42 MG |
321 | /* Recheck this is a buddy page under lock */ |
322 | if (!PageBuddy(page)) | |
748446bb MG |
323 | continue; |
324 | ||
325 | /* Found a free page, break it into order-0 pages */ | |
326 | isolated = split_free_page(page); | |
85aa125f | 327 | if (!isolated && strict) |
f40d1e42 | 328 | break; |
748446bb MG |
329 | total_isolated += isolated; |
330 | for (i = 0; i < isolated; i++) { | |
331 | list_add(&page->lru, freelist); | |
332 | page++; | |
333 | } | |
334 | ||
335 | /* If a page was split, advance to the end of it */ | |
336 | if (isolated) { | |
337 | blockpfn += isolated - 1; | |
338 | cursor += isolated - 1; | |
339 | } | |
340 | } | |
341 | ||
b7aba698 | 342 | trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated); |
f40d1e42 MG |
343 | |
344 | /* | |
345 | * If strict isolation is requested by CMA then check that all the | |
346 | * pages requested were isolated. If there were any failures, 0 is | |
347 | * returned and CMA will fail. | |
348 | */ | |
349 | if (strict && nr_strict_required != total_isolated) | |
350 | total_isolated = 0; | |
351 | ||
352 | if (locked) | |
353 | spin_unlock_irqrestore(&cc->zone->lock, flags); | |
354 | ||
bb13ffeb MG |
355 | /* Update the pageblock-skip if the whole pageblock was scanned */ |
356 | if (blockpfn == end_pfn) | |
c89511ab | 357 | update_pageblock_skip(cc, valid_page, total_isolated, false); |
bb13ffeb | 358 | |
748446bb MG |
359 | return total_isolated; |
360 | } | |
361 | ||
85aa125f MN |
362 | /** |
363 | * isolate_freepages_range() - isolate free pages. | |
364 | * @start_pfn: The first PFN to start isolating. | |
365 | * @end_pfn: The one-past-last PFN. | |
366 | * | |
367 | * Non-free pages, invalid PFNs, or zone boundaries within the | |
368 | * [start_pfn, end_pfn) range are considered errors, cause function to | |
369 | * undo its actions and return zero. | |
370 | * | |
371 | * Otherwise, function returns one-past-the-last PFN of isolated page | |
372 | * (which may be greater then end_pfn if end fell in a middle of | |
373 | * a free page). | |
374 | */ | |
ff9543fd | 375 | unsigned long |
bb13ffeb MG |
376 | isolate_freepages_range(struct compact_control *cc, |
377 | unsigned long start_pfn, unsigned long end_pfn) | |
85aa125f | 378 | { |
f40d1e42 | 379 | unsigned long isolated, pfn, block_end_pfn; |
85aa125f MN |
380 | LIST_HEAD(freelist); |
381 | ||
85aa125f | 382 | for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) { |
bb13ffeb | 383 | if (!pfn_valid(pfn) || cc->zone != page_zone(pfn_to_page(pfn))) |
85aa125f MN |
384 | break; |
385 | ||
386 | /* | |
387 | * On subsequent iterations ALIGN() is actually not needed, | |
388 | * but we keep it that we not to complicate the code. | |
389 | */ | |
390 | block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages); | |
391 | block_end_pfn = min(block_end_pfn, end_pfn); | |
392 | ||
bb13ffeb | 393 | isolated = isolate_freepages_block(cc, pfn, block_end_pfn, |
85aa125f | 394 | &freelist, true); |
85aa125f MN |
395 | |
396 | /* | |
397 | * In strict mode, isolate_freepages_block() returns 0 if | |
398 | * there are any holes in the block (ie. invalid PFNs or | |
399 | * non-free pages). | |
400 | */ | |
401 | if (!isolated) | |
402 | break; | |
403 | ||
404 | /* | |
405 | * If we managed to isolate pages, it is always (1 << n) * | |
406 | * pageblock_nr_pages for some non-negative n. (Max order | |
407 | * page may span two pageblocks). | |
408 | */ | |
409 | } | |
410 | ||
411 | /* split_free_page does not map the pages */ | |
412 | map_pages(&freelist); | |
413 | ||
414 | if (pfn < end_pfn) { | |
415 | /* Loop terminated early, cleanup. */ | |
416 | release_freepages(&freelist); | |
417 | return 0; | |
418 | } | |
419 | ||
420 | /* We don't use freelists for anything. */ | |
421 | return pfn; | |
422 | } | |
423 | ||
748446bb | 424 | /* Update the number of anon and file isolated pages in the zone */ |
c67fe375 | 425 | static void acct_isolated(struct zone *zone, bool locked, struct compact_control *cc) |
748446bb MG |
426 | { |
427 | struct page *page; | |
b9e84ac1 | 428 | unsigned int count[2] = { 0, }; |
748446bb | 429 | |
b9e84ac1 MK |
430 | list_for_each_entry(page, &cc->migratepages, lru) |
431 | count[!!page_is_file_cache(page)]++; | |
748446bb | 432 | |
c67fe375 MG |
433 | /* If locked we can use the interrupt unsafe versions */ |
434 | if (locked) { | |
435 | __mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]); | |
436 | __mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]); | |
437 | } else { | |
438 | mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]); | |
439 | mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]); | |
440 | } | |
748446bb MG |
441 | } |
442 | ||
443 | /* Similar to reclaim, but different enough that they don't share logic */ | |
444 | static bool too_many_isolated(struct zone *zone) | |
445 | { | |
bc693045 | 446 | unsigned long active, inactive, isolated; |
748446bb MG |
447 | |
448 | inactive = zone_page_state(zone, NR_INACTIVE_FILE) + | |
449 | zone_page_state(zone, NR_INACTIVE_ANON); | |
bc693045 MK |
450 | active = zone_page_state(zone, NR_ACTIVE_FILE) + |
451 | zone_page_state(zone, NR_ACTIVE_ANON); | |
748446bb MG |
452 | isolated = zone_page_state(zone, NR_ISOLATED_FILE) + |
453 | zone_page_state(zone, NR_ISOLATED_ANON); | |
454 | ||
bc693045 | 455 | return isolated > (inactive + active) / 2; |
748446bb MG |
456 | } |
457 | ||
2fe86e00 MN |
458 | /** |
459 | * isolate_migratepages_range() - isolate all migrate-able pages in range. | |
460 | * @zone: Zone pages are in. | |
461 | * @cc: Compaction control structure. | |
462 | * @low_pfn: The first PFN of the range. | |
463 | * @end_pfn: The one-past-the-last PFN of the range. | |
464 | * | |
465 | * Isolate all pages that can be migrated from the range specified by | |
466 | * [low_pfn, end_pfn). Returns zero if there is a fatal signal | |
467 | * pending), otherwise PFN of the first page that was not scanned | |
468 | * (which may be both less, equal to or more then end_pfn). | |
469 | * | |
470 | * Assumes that cc->migratepages is empty and cc->nr_migratepages is | |
471 | * zero. | |
472 | * | |
473 | * Apart from cc->migratepages and cc->nr_migratetypes this function | |
474 | * does not modify any cc's fields, in particular it does not modify | |
475 | * (or read for that matter) cc->migrate_pfn. | |
748446bb | 476 | */ |
ff9543fd | 477 | unsigned long |
2fe86e00 MN |
478 | isolate_migratepages_range(struct zone *zone, struct compact_control *cc, |
479 | unsigned long low_pfn, unsigned long end_pfn) | |
748446bb | 480 | { |
9927af74 | 481 | unsigned long last_pageblock_nr = 0, pageblock_nr; |
b7aba698 | 482 | unsigned long nr_scanned = 0, nr_isolated = 0; |
748446bb | 483 | struct list_head *migratelist = &cc->migratepages; |
f3fd4a61 | 484 | isolate_mode_t mode = 0; |
fa9add64 | 485 | struct lruvec *lruvec; |
c67fe375 | 486 | unsigned long flags; |
2a1402aa | 487 | bool locked = false; |
bb13ffeb | 488 | struct page *page = NULL, *valid_page = NULL; |
748446bb | 489 | |
748446bb MG |
490 | /* |
491 | * Ensure that there are not too many pages isolated from the LRU | |
492 | * list by either parallel reclaimers or compaction. If there are, | |
493 | * delay for some time until fewer pages are isolated | |
494 | */ | |
495 | while (unlikely(too_many_isolated(zone))) { | |
f9e35b3b | 496 | /* async migration should just abort */ |
68e3e926 | 497 | if (!cc->sync) |
2fe86e00 | 498 | return 0; |
f9e35b3b | 499 | |
748446bb MG |
500 | congestion_wait(BLK_RW_ASYNC, HZ/10); |
501 | ||
502 | if (fatal_signal_pending(current)) | |
2fe86e00 | 503 | return 0; |
748446bb MG |
504 | } |
505 | ||
506 | /* Time to isolate some pages for migration */ | |
b2eef8c0 | 507 | cond_resched(); |
748446bb | 508 | for (; low_pfn < end_pfn; low_pfn++) { |
b2eef8c0 | 509 | /* give a chance to irqs before checking need_resched() */ |
2a1402aa MG |
510 | if (locked && !((low_pfn+1) % SWAP_CLUSTER_MAX)) { |
511 | if (should_release_lock(&zone->lru_lock)) { | |
512 | spin_unlock_irqrestore(&zone->lru_lock, flags); | |
513 | locked = false; | |
514 | } | |
b2eef8c0 | 515 | } |
c67fe375 | 516 | |
0bf380bc MG |
517 | /* |
518 | * migrate_pfn does not necessarily start aligned to a | |
519 | * pageblock. Ensure that pfn_valid is called when moving | |
520 | * into a new MAX_ORDER_NR_PAGES range in case of large | |
521 | * memory holes within the zone | |
522 | */ | |
523 | if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) { | |
524 | if (!pfn_valid(low_pfn)) { | |
525 | low_pfn += MAX_ORDER_NR_PAGES - 1; | |
526 | continue; | |
527 | } | |
528 | } | |
529 | ||
748446bb MG |
530 | if (!pfn_valid_within(low_pfn)) |
531 | continue; | |
b7aba698 | 532 | nr_scanned++; |
748446bb | 533 | |
dc908600 MG |
534 | /* |
535 | * Get the page and ensure the page is within the same zone. | |
536 | * See the comment in isolate_freepages about overlapping | |
537 | * nodes. It is deliberate that the new zone lock is not taken | |
538 | * as memory compaction should not move pages between nodes. | |
539 | */ | |
748446bb | 540 | page = pfn_to_page(low_pfn); |
dc908600 MG |
541 | if (page_zone(page) != zone) |
542 | continue; | |
543 | ||
bb13ffeb MG |
544 | if (!valid_page) |
545 | valid_page = page; | |
546 | ||
547 | /* If isolation recently failed, do not retry */ | |
548 | pageblock_nr = low_pfn >> pageblock_order; | |
549 | if (!isolation_suitable(cc, page)) | |
550 | goto next_pageblock; | |
551 | ||
dc908600 | 552 | /* Skip if free */ |
748446bb MG |
553 | if (PageBuddy(page)) |
554 | continue; | |
555 | ||
9927af74 MG |
556 | /* |
557 | * For async migration, also only scan in MOVABLE blocks. Async | |
558 | * migration is optimistic to see if the minimum amount of work | |
559 | * satisfies the allocation | |
560 | */ | |
68e3e926 | 561 | if (!cc->sync && last_pageblock_nr != pageblock_nr && |
47118af0 | 562 | !migrate_async_suitable(get_pageblock_migratetype(page))) { |
c89511ab | 563 | cc->finished_update_migrate = true; |
2a1402aa | 564 | goto next_pageblock; |
9927af74 MG |
565 | } |
566 | ||
2a1402aa | 567 | /* Check may be lockless but that's ok as we recheck later */ |
bc835011 AA |
568 | if (!PageLRU(page)) |
569 | continue; | |
570 | ||
571 | /* | |
2a1402aa MG |
572 | * PageLRU is set. lru_lock normally excludes isolation |
573 | * splitting and collapsing (collapsing has already happened | |
574 | * if PageLRU is set) but the lock is not necessarily taken | |
575 | * here and it is wasteful to take it just to check transhuge. | |
576 | * Check TransHuge without lock and skip the whole pageblock if | |
577 | * it's either a transhuge or hugetlbfs page, as calling | |
578 | * compound_order() without preventing THP from splitting the | |
579 | * page underneath us may return surprising results. | |
bc835011 | 580 | */ |
2a1402aa MG |
581 | if (PageTransHuge(page)) { |
582 | if (!locked) | |
583 | goto next_pageblock; | |
584 | low_pfn += (1 << compound_order(page)) - 1; | |
585 | continue; | |
586 | } | |
587 | ||
588 | /* Check if it is ok to still hold the lock */ | |
589 | locked = compact_checklock_irqsave(&zone->lru_lock, &flags, | |
590 | locked, cc); | |
591 | if (!locked || fatal_signal_pending(current)) | |
592 | break; | |
593 | ||
594 | /* Recheck PageLRU and PageTransHuge under lock */ | |
595 | if (!PageLRU(page)) | |
596 | continue; | |
bc835011 AA |
597 | if (PageTransHuge(page)) { |
598 | low_pfn += (1 << compound_order(page)) - 1; | |
599 | continue; | |
600 | } | |
601 | ||
68e3e926 | 602 | if (!cc->sync) |
c8244935 MG |
603 | mode |= ISOLATE_ASYNC_MIGRATE; |
604 | ||
fa9add64 HD |
605 | lruvec = mem_cgroup_page_lruvec(page, zone); |
606 | ||
748446bb | 607 | /* Try isolate the page */ |
f3fd4a61 | 608 | if (__isolate_lru_page(page, mode) != 0) |
748446bb MG |
609 | continue; |
610 | ||
bc835011 AA |
611 | VM_BUG_ON(PageTransCompound(page)); |
612 | ||
748446bb | 613 | /* Successfully isolated */ |
c89511ab | 614 | cc->finished_update_migrate = true; |
fa9add64 | 615 | del_page_from_lru_list(page, lruvec, page_lru(page)); |
748446bb | 616 | list_add(&page->lru, migratelist); |
748446bb | 617 | cc->nr_migratepages++; |
b7aba698 | 618 | nr_isolated++; |
748446bb MG |
619 | |
620 | /* Avoid isolating too much */ | |
31b8384a HD |
621 | if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) { |
622 | ++low_pfn; | |
748446bb | 623 | break; |
31b8384a | 624 | } |
2a1402aa MG |
625 | |
626 | continue; | |
627 | ||
628 | next_pageblock: | |
629 | low_pfn += pageblock_nr_pages; | |
630 | low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1; | |
631 | last_pageblock_nr = pageblock_nr; | |
748446bb MG |
632 | } |
633 | ||
c67fe375 | 634 | acct_isolated(zone, locked, cc); |
748446bb | 635 | |
c67fe375 MG |
636 | if (locked) |
637 | spin_unlock_irqrestore(&zone->lru_lock, flags); | |
748446bb | 638 | |
bb13ffeb MG |
639 | /* Update the pageblock-skip if the whole pageblock was scanned */ |
640 | if (low_pfn == end_pfn) | |
c89511ab | 641 | update_pageblock_skip(cc, valid_page, nr_isolated, true); |
bb13ffeb | 642 | |
b7aba698 MG |
643 | trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated); |
644 | ||
2fe86e00 MN |
645 | return low_pfn; |
646 | } | |
647 | ||
ff9543fd MN |
648 | #endif /* CONFIG_COMPACTION || CONFIG_CMA */ |
649 | #ifdef CONFIG_COMPACTION | |
2fe86e00 | 650 | /* |
ff9543fd MN |
651 | * Based on information in the current compact_control, find blocks |
652 | * suitable for isolating free pages from and then isolate them. | |
2fe86e00 | 653 | */ |
ff9543fd MN |
654 | static void isolate_freepages(struct zone *zone, |
655 | struct compact_control *cc) | |
2fe86e00 | 656 | { |
ff9543fd MN |
657 | struct page *page; |
658 | unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn; | |
ff9543fd MN |
659 | int nr_freepages = cc->nr_freepages; |
660 | struct list_head *freelist = &cc->freepages; | |
2fe86e00 | 661 | |
ff9543fd MN |
662 | /* |
663 | * Initialise the free scanner. The starting point is where we last | |
664 | * scanned from (or the end of the zone if starting). The low point | |
665 | * is the end of the pageblock the migration scanner is using. | |
666 | */ | |
667 | pfn = cc->free_pfn; | |
668 | low_pfn = cc->migrate_pfn + pageblock_nr_pages; | |
2fe86e00 | 669 | |
ff9543fd MN |
670 | /* |
671 | * Take care that if the migration scanner is at the end of the zone | |
672 | * that the free scanner does not accidentally move to the next zone | |
673 | * in the next isolation cycle. | |
674 | */ | |
675 | high_pfn = min(low_pfn, pfn); | |
2fe86e00 | 676 | |
ff9543fd | 677 | zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages; |
2fe86e00 | 678 | |
ff9543fd MN |
679 | /* |
680 | * Isolate free pages until enough are available to migrate the | |
681 | * pages on cc->migratepages. We stop searching if the migrate | |
682 | * and free page scanners meet or enough free pages are isolated. | |
683 | */ | |
684 | for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages; | |
685 | pfn -= pageblock_nr_pages) { | |
686 | unsigned long isolated; | |
2fe86e00 | 687 | |
ff9543fd MN |
688 | if (!pfn_valid(pfn)) |
689 | continue; | |
2fe86e00 | 690 | |
ff9543fd MN |
691 | /* |
692 | * Check for overlapping nodes/zones. It's possible on some | |
693 | * configurations to have a setup like | |
694 | * node0 node1 node0 | |
695 | * i.e. it's possible that all pages within a zones range of | |
696 | * pages do not belong to a single zone. | |
697 | */ | |
698 | page = pfn_to_page(pfn); | |
699 | if (page_zone(page) != zone) | |
700 | continue; | |
701 | ||
702 | /* Check the block is suitable for migration */ | |
68e3e926 | 703 | if (!suitable_migration_target(page)) |
ff9543fd | 704 | continue; |
68e3e926 | 705 | |
bb13ffeb MG |
706 | /* If isolation recently failed, do not retry */ |
707 | if (!isolation_suitable(cc, page)) | |
708 | continue; | |
709 | ||
f40d1e42 | 710 | /* Found a block suitable for isolating free pages from */ |
ff9543fd | 711 | isolated = 0; |
f40d1e42 MG |
712 | end_pfn = min(pfn + pageblock_nr_pages, zone_end_pfn); |
713 | isolated = isolate_freepages_block(cc, pfn, end_pfn, | |
714 | freelist, false); | |
715 | nr_freepages += isolated; | |
ff9543fd MN |
716 | |
717 | /* | |
718 | * Record the highest PFN we isolated pages from. When next | |
719 | * looking for free pages, the search will restart here as | |
720 | * page migration may have returned some pages to the allocator | |
721 | */ | |
c89511ab MG |
722 | if (isolated) { |
723 | cc->finished_update_free = true; | |
ff9543fd | 724 | high_pfn = max(high_pfn, pfn); |
c89511ab | 725 | } |
ff9543fd MN |
726 | } |
727 | ||
728 | /* split_free_page does not map the pages */ | |
729 | map_pages(freelist); | |
730 | ||
731 | cc->free_pfn = high_pfn; | |
732 | cc->nr_freepages = nr_freepages; | |
748446bb MG |
733 | } |
734 | ||
735 | /* | |
736 | * This is a migrate-callback that "allocates" freepages by taking pages | |
737 | * from the isolated freelists in the block we are migrating to. | |
738 | */ | |
739 | static struct page *compaction_alloc(struct page *migratepage, | |
740 | unsigned long data, | |
741 | int **result) | |
742 | { | |
743 | struct compact_control *cc = (struct compact_control *)data; | |
744 | struct page *freepage; | |
745 | ||
746 | /* Isolate free pages if necessary */ | |
747 | if (list_empty(&cc->freepages)) { | |
748 | isolate_freepages(cc->zone, cc); | |
749 | ||
750 | if (list_empty(&cc->freepages)) | |
751 | return NULL; | |
752 | } | |
753 | ||
754 | freepage = list_entry(cc->freepages.next, struct page, lru); | |
755 | list_del(&freepage->lru); | |
756 | cc->nr_freepages--; | |
757 | ||
758 | return freepage; | |
759 | } | |
760 | ||
761 | /* | |
762 | * We cannot control nr_migratepages and nr_freepages fully when migration is | |
763 | * running as migrate_pages() has no knowledge of compact_control. When | |
764 | * migration is complete, we count the number of pages on the lists by hand. | |
765 | */ | |
766 | static void update_nr_listpages(struct compact_control *cc) | |
767 | { | |
768 | int nr_migratepages = 0; | |
769 | int nr_freepages = 0; | |
770 | struct page *page; | |
771 | ||
772 | list_for_each_entry(page, &cc->migratepages, lru) | |
773 | nr_migratepages++; | |
774 | list_for_each_entry(page, &cc->freepages, lru) | |
775 | nr_freepages++; | |
776 | ||
777 | cc->nr_migratepages = nr_migratepages; | |
778 | cc->nr_freepages = nr_freepages; | |
779 | } | |
780 | ||
ff9543fd MN |
781 | /* possible outcome of isolate_migratepages */ |
782 | typedef enum { | |
783 | ISOLATE_ABORT, /* Abort compaction now */ | |
784 | ISOLATE_NONE, /* No pages isolated, continue scanning */ | |
785 | ISOLATE_SUCCESS, /* Pages isolated, migrate */ | |
786 | } isolate_migrate_t; | |
787 | ||
788 | /* | |
789 | * Isolate all pages that can be migrated from the block pointed to by | |
790 | * the migrate scanner within compact_control. | |
791 | */ | |
792 | static isolate_migrate_t isolate_migratepages(struct zone *zone, | |
793 | struct compact_control *cc) | |
794 | { | |
795 | unsigned long low_pfn, end_pfn; | |
796 | ||
797 | /* Do not scan outside zone boundaries */ | |
798 | low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn); | |
799 | ||
800 | /* Only scan within a pageblock boundary */ | |
801 | end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages); | |
802 | ||
803 | /* Do not cross the free scanner or scan within a memory hole */ | |
804 | if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) { | |
805 | cc->migrate_pfn = end_pfn; | |
806 | return ISOLATE_NONE; | |
807 | } | |
808 | ||
809 | /* Perform the isolation */ | |
810 | low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn); | |
e64c5237 | 811 | if (!low_pfn || cc->contended) |
ff9543fd MN |
812 | return ISOLATE_ABORT; |
813 | ||
814 | cc->migrate_pfn = low_pfn; | |
815 | ||
816 | return ISOLATE_SUCCESS; | |
817 | } | |
818 | ||
748446bb | 819 | static int compact_finished(struct zone *zone, |
5a03b051 | 820 | struct compact_control *cc) |
748446bb | 821 | { |
5a03b051 | 822 | unsigned long watermark; |
56de7263 | 823 | |
748446bb MG |
824 | if (fatal_signal_pending(current)) |
825 | return COMPACT_PARTIAL; | |
826 | ||
753341a4 | 827 | /* Compaction run completes if the migrate and free scanner meet */ |
bb13ffeb | 828 | if (cc->free_pfn <= cc->migrate_pfn) { |
62997027 MG |
829 | /* |
830 | * Mark that the PG_migrate_skip information should be cleared | |
831 | * by kswapd when it goes to sleep. kswapd does not set the | |
832 | * flag itself as the decision to be clear should be directly | |
833 | * based on an allocation request. | |
834 | */ | |
835 | if (!current_is_kswapd()) | |
836 | zone->compact_blockskip_flush = true; | |
837 | ||
748446bb | 838 | return COMPACT_COMPLETE; |
bb13ffeb | 839 | } |
748446bb | 840 | |
82478fb7 JW |
841 | /* |
842 | * order == -1 is expected when compacting via | |
843 | * /proc/sys/vm/compact_memory | |
844 | */ | |
56de7263 MG |
845 | if (cc->order == -1) |
846 | return COMPACT_CONTINUE; | |
847 | ||
3957c776 MH |
848 | /* Compaction run is not finished if the watermark is not met */ |
849 | watermark = low_wmark_pages(zone); | |
850 | watermark += (1 << cc->order); | |
851 | ||
852 | if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0)) | |
853 | return COMPACT_CONTINUE; | |
854 | ||
56de7263 | 855 | /* Direct compactor: Is a suitable page free? */ |
1fb3f8ca MG |
856 | if (cc->page) { |
857 | /* Was a suitable page captured? */ | |
858 | if (*cc->page) | |
56de7263 | 859 | return COMPACT_PARTIAL; |
1fb3f8ca MG |
860 | } else { |
861 | unsigned int order; | |
862 | for (order = cc->order; order < MAX_ORDER; order++) { | |
863 | struct free_area *area = &zone->free_area[cc->order]; | |
864 | /* Job done if page is free of the right migratetype */ | |
865 | if (!list_empty(&area->free_list[cc->migratetype])) | |
866 | return COMPACT_PARTIAL; | |
867 | ||
868 | /* Job done if allocation would set block type */ | |
869 | if (cc->order >= pageblock_order && area->nr_free) | |
870 | return COMPACT_PARTIAL; | |
871 | } | |
56de7263 MG |
872 | } |
873 | ||
748446bb MG |
874 | return COMPACT_CONTINUE; |
875 | } | |
876 | ||
3e7d3449 MG |
877 | /* |
878 | * compaction_suitable: Is this suitable to run compaction on this zone now? | |
879 | * Returns | |
880 | * COMPACT_SKIPPED - If there are too few free pages for compaction | |
881 | * COMPACT_PARTIAL - If the allocation would succeed without compaction | |
882 | * COMPACT_CONTINUE - If compaction should run now | |
883 | */ | |
884 | unsigned long compaction_suitable(struct zone *zone, int order) | |
885 | { | |
886 | int fragindex; | |
887 | unsigned long watermark; | |
888 | ||
3957c776 MH |
889 | /* |
890 | * order == -1 is expected when compacting via | |
891 | * /proc/sys/vm/compact_memory | |
892 | */ | |
893 | if (order == -1) | |
894 | return COMPACT_CONTINUE; | |
895 | ||
3e7d3449 MG |
896 | /* |
897 | * Watermarks for order-0 must be met for compaction. Note the 2UL. | |
898 | * This is because during migration, copies of pages need to be | |
899 | * allocated and for a short time, the footprint is higher | |
900 | */ | |
901 | watermark = low_wmark_pages(zone) + (2UL << order); | |
902 | if (!zone_watermark_ok(zone, 0, watermark, 0, 0)) | |
903 | return COMPACT_SKIPPED; | |
904 | ||
905 | /* | |
906 | * fragmentation index determines if allocation failures are due to | |
907 | * low memory or external fragmentation | |
908 | * | |
a582a738 SL |
909 | * index of -1000 implies allocations might succeed depending on |
910 | * watermarks | |
3e7d3449 MG |
911 | * index towards 0 implies failure is due to lack of memory |
912 | * index towards 1000 implies failure is due to fragmentation | |
913 | * | |
914 | * Only compact if a failure would be due to fragmentation. | |
915 | */ | |
916 | fragindex = fragmentation_index(zone, order); | |
917 | if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold) | |
918 | return COMPACT_SKIPPED; | |
919 | ||
a582a738 SL |
920 | if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark, |
921 | 0, 0)) | |
3e7d3449 MG |
922 | return COMPACT_PARTIAL; |
923 | ||
924 | return COMPACT_CONTINUE; | |
925 | } | |
926 | ||
748446bb MG |
927 | static int compact_zone(struct zone *zone, struct compact_control *cc) |
928 | { | |
929 | int ret; | |
c89511ab MG |
930 | unsigned long start_pfn = zone->zone_start_pfn; |
931 | unsigned long end_pfn = zone->zone_start_pfn + zone->spanned_pages; | |
748446bb | 932 | |
3e7d3449 MG |
933 | ret = compaction_suitable(zone, cc->order); |
934 | switch (ret) { | |
935 | case COMPACT_PARTIAL: | |
936 | case COMPACT_SKIPPED: | |
937 | /* Compaction is likely to fail */ | |
938 | return ret; | |
939 | case COMPACT_CONTINUE: | |
940 | /* Fall through to compaction */ | |
941 | ; | |
942 | } | |
943 | ||
c89511ab MG |
944 | /* |
945 | * Setup to move all movable pages to the end of the zone. Used cached | |
946 | * information on where the scanners should start but check that it | |
947 | * is initialised by ensuring the values are within zone boundaries. | |
948 | */ | |
949 | cc->migrate_pfn = zone->compact_cached_migrate_pfn; | |
950 | cc->free_pfn = zone->compact_cached_free_pfn; | |
951 | if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) { | |
952 | cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1); | |
953 | zone->compact_cached_free_pfn = cc->free_pfn; | |
954 | } | |
955 | if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) { | |
956 | cc->migrate_pfn = start_pfn; | |
957 | zone->compact_cached_migrate_pfn = cc->migrate_pfn; | |
958 | } | |
748446bb | 959 | |
62997027 MG |
960 | /* |
961 | * Clear pageblock skip if there were failures recently and compaction | |
962 | * is about to be retried after being deferred. kswapd does not do | |
963 | * this reset as it'll reset the cached information when going to sleep. | |
964 | */ | |
965 | if (compaction_restarting(zone, cc->order) && !current_is_kswapd()) | |
966 | __reset_isolation_suitable(zone); | |
bb13ffeb | 967 | |
748446bb MG |
968 | migrate_prep_local(); |
969 | ||
970 | while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) { | |
971 | unsigned long nr_migrate, nr_remaining; | |
9d502c1c | 972 | int err; |
748446bb | 973 | |
f9e35b3b MG |
974 | switch (isolate_migratepages(zone, cc)) { |
975 | case ISOLATE_ABORT: | |
976 | ret = COMPACT_PARTIAL; | |
e64c5237 SL |
977 | putback_lru_pages(&cc->migratepages); |
978 | cc->nr_migratepages = 0; | |
f9e35b3b MG |
979 | goto out; |
980 | case ISOLATE_NONE: | |
748446bb | 981 | continue; |
f9e35b3b MG |
982 | case ISOLATE_SUCCESS: |
983 | ; | |
984 | } | |
748446bb MG |
985 | |
986 | nr_migrate = cc->nr_migratepages; | |
9d502c1c | 987 | err = migrate_pages(&cc->migratepages, compaction_alloc, |
68e3e926 LT |
988 | (unsigned long)cc, false, |
989 | cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC); | |
748446bb MG |
990 | update_nr_listpages(cc); |
991 | nr_remaining = cc->nr_migratepages; | |
992 | ||
993 | count_vm_event(COMPACTBLOCKS); | |
994 | count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining); | |
995 | if (nr_remaining) | |
996 | count_vm_events(COMPACTPAGEFAILED, nr_remaining); | |
b7aba698 MG |
997 | trace_mm_compaction_migratepages(nr_migrate - nr_remaining, |
998 | nr_remaining); | |
748446bb MG |
999 | |
1000 | /* Release LRU pages not migrated */ | |
9d502c1c | 1001 | if (err) { |
748446bb MG |
1002 | putback_lru_pages(&cc->migratepages); |
1003 | cc->nr_migratepages = 0; | |
4bf2bba3 DR |
1004 | if (err == -ENOMEM) { |
1005 | ret = COMPACT_PARTIAL; | |
1006 | goto out; | |
1007 | } | |
748446bb | 1008 | } |
1fb3f8ca MG |
1009 | |
1010 | /* Capture a page now if it is a suitable size */ | |
1011 | compact_capture_page(cc); | |
748446bb MG |
1012 | } |
1013 | ||
f9e35b3b | 1014 | out: |
748446bb MG |
1015 | /* Release free pages and check accounting */ |
1016 | cc->nr_freepages -= release_freepages(&cc->freepages); | |
1017 | VM_BUG_ON(cc->nr_freepages != 0); | |
1018 | ||
1019 | return ret; | |
1020 | } | |
76ab0f53 | 1021 | |
d43a87e6 | 1022 | static unsigned long compact_zone_order(struct zone *zone, |
5a03b051 | 1023 | int order, gfp_t gfp_mask, |
1fb3f8ca MG |
1024 | bool sync, bool *contended, |
1025 | struct page **page) | |
56de7263 | 1026 | { |
e64c5237 | 1027 | unsigned long ret; |
56de7263 MG |
1028 | struct compact_control cc = { |
1029 | .nr_freepages = 0, | |
1030 | .nr_migratepages = 0, | |
1031 | .order = order, | |
1032 | .migratetype = allocflags_to_migratetype(gfp_mask), | |
1033 | .zone = zone, | |
68e3e926 | 1034 | .sync = sync, |
1fb3f8ca | 1035 | .page = page, |
56de7263 MG |
1036 | }; |
1037 | INIT_LIST_HEAD(&cc.freepages); | |
1038 | INIT_LIST_HEAD(&cc.migratepages); | |
1039 | ||
e64c5237 SL |
1040 | ret = compact_zone(zone, &cc); |
1041 | ||
1042 | VM_BUG_ON(!list_empty(&cc.freepages)); | |
1043 | VM_BUG_ON(!list_empty(&cc.migratepages)); | |
1044 | ||
1045 | *contended = cc.contended; | |
1046 | return ret; | |
56de7263 MG |
1047 | } |
1048 | ||
5e771905 MG |
1049 | int sysctl_extfrag_threshold = 500; |
1050 | ||
56de7263 MG |
1051 | /** |
1052 | * try_to_compact_pages - Direct compact to satisfy a high-order allocation | |
1053 | * @zonelist: The zonelist used for the current allocation | |
1054 | * @order: The order of the current allocation | |
1055 | * @gfp_mask: The GFP mask of the current allocation | |
1056 | * @nodemask: The allowed nodes to allocate from | |
77f1fe6b | 1057 | * @sync: Whether migration is synchronous or not |
661c4cb9 MG |
1058 | * @contended: Return value that is true if compaction was aborted due to lock contention |
1059 | * @page: Optionally capture a free page of the requested order during compaction | |
56de7263 MG |
1060 | * |
1061 | * This is the main entry point for direct page compaction. | |
1062 | */ | |
1063 | unsigned long try_to_compact_pages(struct zonelist *zonelist, | |
77f1fe6b | 1064 | int order, gfp_t gfp_mask, nodemask_t *nodemask, |
1fb3f8ca | 1065 | bool sync, bool *contended, struct page **page) |
56de7263 MG |
1066 | { |
1067 | enum zone_type high_zoneidx = gfp_zone(gfp_mask); | |
1068 | int may_enter_fs = gfp_mask & __GFP_FS; | |
1069 | int may_perform_io = gfp_mask & __GFP_IO; | |
56de7263 MG |
1070 | struct zoneref *z; |
1071 | struct zone *zone; | |
1072 | int rc = COMPACT_SKIPPED; | |
d95ea5d1 | 1073 | int alloc_flags = 0; |
56de7263 | 1074 | |
4ffb6335 | 1075 | /* Check if the GFP flags allow compaction */ |
c5a73c3d | 1076 | if (!order || !may_enter_fs || !may_perform_io) |
56de7263 MG |
1077 | return rc; |
1078 | ||
1079 | count_vm_event(COMPACTSTALL); | |
1080 | ||
d95ea5d1 BZ |
1081 | #ifdef CONFIG_CMA |
1082 | if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE) | |
1083 | alloc_flags |= ALLOC_CMA; | |
1084 | #endif | |
56de7263 MG |
1085 | /* Compact each zone in the list */ |
1086 | for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx, | |
1087 | nodemask) { | |
56de7263 MG |
1088 | int status; |
1089 | ||
c67fe375 | 1090 | status = compact_zone_order(zone, order, gfp_mask, sync, |
1fb3f8ca | 1091 | contended, page); |
56de7263 MG |
1092 | rc = max(status, rc); |
1093 | ||
3e7d3449 | 1094 | /* If a normal allocation would succeed, stop compacting */ |
d95ea5d1 BZ |
1095 | if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0, |
1096 | alloc_flags)) | |
56de7263 MG |
1097 | break; |
1098 | } | |
1099 | ||
1100 | return rc; | |
1101 | } | |
1102 | ||
1103 | ||
76ab0f53 | 1104 | /* Compact all zones within a node */ |
7be62de9 | 1105 | static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc) |
76ab0f53 MG |
1106 | { |
1107 | int zoneid; | |
76ab0f53 MG |
1108 | struct zone *zone; |
1109 | ||
76ab0f53 | 1110 | for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { |
76ab0f53 MG |
1111 | |
1112 | zone = &pgdat->node_zones[zoneid]; | |
1113 | if (!populated_zone(zone)) | |
1114 | continue; | |
1115 | ||
7be62de9 RR |
1116 | cc->nr_freepages = 0; |
1117 | cc->nr_migratepages = 0; | |
1118 | cc->zone = zone; | |
1119 | INIT_LIST_HEAD(&cc->freepages); | |
1120 | INIT_LIST_HEAD(&cc->migratepages); | |
76ab0f53 | 1121 | |
aad6ec37 | 1122 | if (cc->order == -1 || !compaction_deferred(zone, cc->order)) |
7be62de9 | 1123 | compact_zone(zone, cc); |
76ab0f53 | 1124 | |
aff62249 RR |
1125 | if (cc->order > 0) { |
1126 | int ok = zone_watermark_ok(zone, cc->order, | |
1127 | low_wmark_pages(zone), 0, 0); | |
c81758fb | 1128 | if (ok && cc->order >= zone->compact_order_failed) |
aff62249 RR |
1129 | zone->compact_order_failed = cc->order + 1; |
1130 | /* Currently async compaction is never deferred. */ | |
68e3e926 | 1131 | else if (!ok && cc->sync) |
aff62249 RR |
1132 | defer_compaction(zone, cc->order); |
1133 | } | |
1134 | ||
7be62de9 RR |
1135 | VM_BUG_ON(!list_empty(&cc->freepages)); |
1136 | VM_BUG_ON(!list_empty(&cc->migratepages)); | |
76ab0f53 MG |
1137 | } |
1138 | ||
1139 | return 0; | |
1140 | } | |
1141 | ||
7be62de9 RR |
1142 | int compact_pgdat(pg_data_t *pgdat, int order) |
1143 | { | |
1144 | struct compact_control cc = { | |
1145 | .order = order, | |
68e3e926 | 1146 | .sync = false, |
1fb3f8ca | 1147 | .page = NULL, |
7be62de9 RR |
1148 | }; |
1149 | ||
1150 | return __compact_pgdat(pgdat, &cc); | |
1151 | } | |
1152 | ||
1153 | static int compact_node(int nid) | |
1154 | { | |
7be62de9 RR |
1155 | struct compact_control cc = { |
1156 | .order = -1, | |
68e3e926 | 1157 | .sync = true, |
1fb3f8ca | 1158 | .page = NULL, |
7be62de9 RR |
1159 | }; |
1160 | ||
8575ec29 | 1161 | return __compact_pgdat(NODE_DATA(nid), &cc); |
7be62de9 RR |
1162 | } |
1163 | ||
76ab0f53 MG |
1164 | /* Compact all nodes in the system */ |
1165 | static int compact_nodes(void) | |
1166 | { | |
1167 | int nid; | |
1168 | ||
8575ec29 HD |
1169 | /* Flush pending updates to the LRU lists */ |
1170 | lru_add_drain_all(); | |
1171 | ||
76ab0f53 MG |
1172 | for_each_online_node(nid) |
1173 | compact_node(nid); | |
1174 | ||
1175 | return COMPACT_COMPLETE; | |
1176 | } | |
1177 | ||
1178 | /* The written value is actually unused, all memory is compacted */ | |
1179 | int sysctl_compact_memory; | |
1180 | ||
1181 | /* This is the entry point for compacting all nodes via /proc/sys/vm */ | |
1182 | int sysctl_compaction_handler(struct ctl_table *table, int write, | |
1183 | void __user *buffer, size_t *length, loff_t *ppos) | |
1184 | { | |
1185 | if (write) | |
1186 | return compact_nodes(); | |
1187 | ||
1188 | return 0; | |
1189 | } | |
ed4a6d7f | 1190 | |
5e771905 MG |
1191 | int sysctl_extfrag_handler(struct ctl_table *table, int write, |
1192 | void __user *buffer, size_t *length, loff_t *ppos) | |
1193 | { | |
1194 | proc_dointvec_minmax(table, write, buffer, length, ppos); | |
1195 | ||
1196 | return 0; | |
1197 | } | |
1198 | ||
ed4a6d7f | 1199 | #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA) |
10fbcf4c KS |
1200 | ssize_t sysfs_compact_node(struct device *dev, |
1201 | struct device_attribute *attr, | |
ed4a6d7f MG |
1202 | const char *buf, size_t count) |
1203 | { | |
8575ec29 HD |
1204 | int nid = dev->id; |
1205 | ||
1206 | if (nid >= 0 && nid < nr_node_ids && node_online(nid)) { | |
1207 | /* Flush pending updates to the LRU lists */ | |
1208 | lru_add_drain_all(); | |
1209 | ||
1210 | compact_node(nid); | |
1211 | } | |
ed4a6d7f MG |
1212 | |
1213 | return count; | |
1214 | } | |
10fbcf4c | 1215 | static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node); |
ed4a6d7f MG |
1216 | |
1217 | int compaction_register_node(struct node *node) | |
1218 | { | |
10fbcf4c | 1219 | return device_create_file(&node->dev, &dev_attr_compact); |
ed4a6d7f MG |
1220 | } |
1221 | ||
1222 | void compaction_unregister_node(struct node *node) | |
1223 | { | |
10fbcf4c | 1224 | return device_remove_file(&node->dev, &dev_attr_compact); |
ed4a6d7f MG |
1225 | } |
1226 | #endif /* CONFIG_SYSFS && CONFIG_NUMA */ | |
ff9543fd MN |
1227 | |
1228 | #endif /* CONFIG_COMPACTION */ |