Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
748446bb MG |
2 | /* |
3 | * linux/mm/compaction.c | |
4 | * | |
5 | * Memory compaction for the reduction of external fragmentation. Note that | |
6 | * this heavily depends upon page migration to do all the real heavy | |
7 | * lifting | |
8 | * | |
9 | * Copyright IBM Corp. 2007-2010 Mel Gorman <mel@csn.ul.ie> | |
10 | */ | |
698b1b30 | 11 | #include <linux/cpu.h> |
748446bb MG |
12 | #include <linux/swap.h> |
13 | #include <linux/migrate.h> | |
14 | #include <linux/compaction.h> | |
15 | #include <linux/mm_inline.h> | |
174cd4b1 | 16 | #include <linux/sched/signal.h> |
748446bb | 17 | #include <linux/backing-dev.h> |
76ab0f53 | 18 | #include <linux/sysctl.h> |
ed4a6d7f | 19 | #include <linux/sysfs.h> |
194159fb | 20 | #include <linux/page-isolation.h> |
b8c73fc2 | 21 | #include <linux/kasan.h> |
698b1b30 VB |
22 | #include <linux/kthread.h> |
23 | #include <linux/freezer.h> | |
83358ece | 24 | #include <linux/page_owner.h> |
eb414681 | 25 | #include <linux/psi.h> |
435b3894 | 26 | #include <linux/cpuset.h> |
748446bb MG |
27 | #include "internal.h" |
28 | ||
010fc29a | 29 | #ifdef CONFIG_COMPACTION |
31ca72fa CTK |
30 | /* |
31 | * Fragmentation score check interval for proactive compaction purposes. | |
32 | */ | |
33 | #define HPAGE_FRAG_CHECK_INTERVAL_MSEC (500) | |
34 | ||
010fc29a MK |
35 | static inline void count_compact_event(enum vm_event_item item) |
36 | { | |
37 | count_vm_event(item); | |
38 | } | |
39 | ||
40 | static inline void count_compact_events(enum vm_event_item item, long delta) | |
41 | { | |
42 | count_vm_events(item, delta); | |
43 | } | |
ee6f62fd ZY |
44 | |
45 | /* | |
46 | * order == -1 is expected when compacting proactively via | |
47 | * 1. /proc/sys/vm/compact_memory | |
48 | * 2. /sys/devices/system/node/nodex/compact | |
49 | * 3. /proc/sys/vm/compaction_proactiveness | |
50 | */ | |
51 | static inline bool is_via_compact_memory(int order) | |
52 | { | |
53 | return order == -1; | |
54 | } | |
55 | ||
010fc29a MK |
56 | #else |
57 | #define count_compact_event(item) do { } while (0) | |
58 | #define count_compact_events(item, delta) do { } while (0) | |
ee6f62fd | 59 | static inline bool is_via_compact_memory(int order) { return false; } |
010fc29a MK |
60 | #endif |
61 | ||
ff9543fd MN |
62 | #if defined CONFIG_COMPACTION || defined CONFIG_CMA |
63 | ||
b7aba698 MG |
64 | #define CREATE_TRACE_POINTS |
65 | #include <trace/events/compaction.h> | |
66 | ||
06b6640a VB |
67 | #define block_start_pfn(pfn, order) round_down(pfn, 1UL << (order)) |
68 | #define block_end_pfn(pfn, order) ALIGN((pfn) + 1, 1UL << (order)) | |
06b6640a | 69 | |
facdaa91 NG |
70 | /* |
71 | * Page order with-respect-to which proactive compaction | |
72 | * calculates external fragmentation, which is used as | |
73 | * the "fragmentation score" of a node/zone. | |
74 | */ | |
75 | #if defined CONFIG_TRANSPARENT_HUGEPAGE | |
76 | #define COMPACTION_HPAGE_ORDER HPAGE_PMD_ORDER | |
25788738 | 77 | #elif defined CONFIG_HUGETLBFS |
facdaa91 NG |
78 | #define COMPACTION_HPAGE_ORDER HUGETLB_PAGE_ORDER |
79 | #else | |
80 | #define COMPACTION_HPAGE_ORDER (PMD_SHIFT - PAGE_SHIFT) | |
81 | #endif | |
82 | ||
34a023dc SB |
83 | static struct page *mark_allocated_noprof(struct page *page, unsigned int order, gfp_t gfp_flags) |
84 | { | |
85 | post_alloc_hook(page, order, __GFP_MOVABLE); | |
8fd10a89 | 86 | set_page_refcounted(page); |
34a023dc SB |
87 | return page; |
88 | } | |
89 | #define mark_allocated(...) alloc_hooks(mark_allocated_noprof(__VA_ARGS__)) | |
90 | ||
733aea0b | 91 | static unsigned long release_free_list(struct list_head *freepages) |
ff9543fd | 92 | { |
733aea0b ZY |
93 | int order; |
94 | unsigned long high_pfn = 0; | |
66c64223 | 95 | |
733aea0b ZY |
96 | for (order = 0; order < NR_PAGE_ORDERS; order++) { |
97 | struct page *page, *next; | |
66c64223 | 98 | |
733aea0b ZY |
99 | list_for_each_entry_safe(page, next, &freepages[order], lru) { |
100 | unsigned long pfn = page_to_pfn(page); | |
ff9543fd | 101 | |
733aea0b ZY |
102 | list_del(&page->lru); |
103 | /* | |
104 | * Convert free pages into post allocation pages, so | |
105 | * that we can free them via __free_page. | |
106 | */ | |
34a023dc | 107 | mark_allocated(page, order, __GFP_MOVABLE); |
733aea0b ZY |
108 | __free_pages(page, order); |
109 | if (pfn > high_pfn) | |
110 | high_pfn = pfn; | |
66c64223 | 111 | } |
ff9543fd | 112 | } |
733aea0b | 113 | return high_pfn; |
ff9543fd MN |
114 | } |
115 | ||
bb13ffeb | 116 | #ifdef CONFIG_COMPACTION |
68f2736a | 117 | bool PageMovable(struct page *page) |
bda807d4 | 118 | { |
68f2736a | 119 | const struct movable_operations *mops; |
bda807d4 MK |
120 | |
121 | VM_BUG_ON_PAGE(!PageLocked(page), page); | |
122 | if (!__PageMovable(page)) | |
68f2736a | 123 | return false; |
bda807d4 | 124 | |
68f2736a MWO |
125 | mops = page_movable_ops(page); |
126 | if (mops) | |
127 | return true; | |
bda807d4 | 128 | |
68f2736a | 129 | return false; |
bda807d4 | 130 | } |
bda807d4 | 131 | |
68f2736a | 132 | void __SetPageMovable(struct page *page, const struct movable_operations *mops) |
bda807d4 MK |
133 | { |
134 | VM_BUG_ON_PAGE(!PageLocked(page), page); | |
68f2736a MWO |
135 | VM_BUG_ON_PAGE((unsigned long)mops & PAGE_MAPPING_MOVABLE, page); |
136 | page->mapping = (void *)((unsigned long)mops | PAGE_MAPPING_MOVABLE); | |
bda807d4 MK |
137 | } |
138 | EXPORT_SYMBOL(__SetPageMovable); | |
139 | ||
140 | void __ClearPageMovable(struct page *page) | |
141 | { | |
bda807d4 MK |
142 | VM_BUG_ON_PAGE(!PageMovable(page), page); |
143 | /* | |
68f2736a MWO |
144 | * This page still has the type of a movable page, but it's |
145 | * actually not movable any more. | |
bda807d4 | 146 | */ |
68f2736a | 147 | page->mapping = (void *)PAGE_MAPPING_MOVABLE; |
bda807d4 MK |
148 | } |
149 | EXPORT_SYMBOL(__ClearPageMovable); | |
150 | ||
24e2716f JK |
151 | /* Do not skip compaction more than 64 times */ |
152 | #define COMPACT_MAX_DEFER_SHIFT 6 | |
153 | ||
154 | /* | |
155 | * Compaction is deferred when compaction fails to result in a page | |
860b3272 | 156 | * allocation success. 1 << compact_defer_shift, compactions are skipped up |
24e2716f JK |
157 | * to a limit of 1 << COMPACT_MAX_DEFER_SHIFT |
158 | */ | |
2271b016 | 159 | static void defer_compaction(struct zone *zone, int order) |
24e2716f JK |
160 | { |
161 | zone->compact_considered = 0; | |
162 | zone->compact_defer_shift++; | |
163 | ||
164 | if (order < zone->compact_order_failed) | |
165 | zone->compact_order_failed = order; | |
166 | ||
167 | if (zone->compact_defer_shift > COMPACT_MAX_DEFER_SHIFT) | |
168 | zone->compact_defer_shift = COMPACT_MAX_DEFER_SHIFT; | |
169 | ||
170 | trace_mm_compaction_defer_compaction(zone, order); | |
171 | } | |
172 | ||
173 | /* Returns true if compaction should be skipped this time */ | |
2271b016 | 174 | static bool compaction_deferred(struct zone *zone, int order) |
24e2716f JK |
175 | { |
176 | unsigned long defer_limit = 1UL << zone->compact_defer_shift; | |
177 | ||
178 | if (order < zone->compact_order_failed) | |
179 | return false; | |
180 | ||
181 | /* Avoid possible overflow */ | |
62b35fe0 | 182 | if (++zone->compact_considered >= defer_limit) { |
24e2716f | 183 | zone->compact_considered = defer_limit; |
24e2716f | 184 | return false; |
62b35fe0 | 185 | } |
24e2716f JK |
186 | |
187 | trace_mm_compaction_deferred(zone, order); | |
188 | ||
189 | return true; | |
190 | } | |
191 | ||
192 | /* | |
193 | * Update defer tracking counters after successful compaction of given order, | |
194 | * which means an allocation either succeeded (alloc_success == true) or is | |
195 | * expected to succeed. | |
196 | */ | |
197 | void compaction_defer_reset(struct zone *zone, int order, | |
198 | bool alloc_success) | |
199 | { | |
200 | if (alloc_success) { | |
201 | zone->compact_considered = 0; | |
202 | zone->compact_defer_shift = 0; | |
203 | } | |
204 | if (order >= zone->compact_order_failed) | |
205 | zone->compact_order_failed = order + 1; | |
206 | ||
207 | trace_mm_compaction_defer_reset(zone, order); | |
208 | } | |
209 | ||
210 | /* Returns true if restarting compaction after many failures */ | |
2271b016 | 211 | static bool compaction_restarting(struct zone *zone, int order) |
24e2716f JK |
212 | { |
213 | if (order < zone->compact_order_failed) | |
214 | return false; | |
215 | ||
216 | return zone->compact_defer_shift == COMPACT_MAX_DEFER_SHIFT && | |
217 | zone->compact_considered >= 1UL << zone->compact_defer_shift; | |
218 | } | |
219 | ||
bb13ffeb MG |
220 | /* Returns true if the pageblock should be scanned for pages to isolate. */ |
221 | static inline bool isolation_suitable(struct compact_control *cc, | |
222 | struct page *page) | |
223 | { | |
224 | if (cc->ignore_skip_hint) | |
225 | return true; | |
226 | ||
227 | return !get_pageblock_skip(page); | |
228 | } | |
229 | ||
02333641 VB |
230 | static void reset_cached_positions(struct zone *zone) |
231 | { | |
232 | zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn; | |
233 | zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn; | |
623446e4 | 234 | zone->compact_cached_free_pfn = |
06b6640a | 235 | pageblock_start_pfn(zone_end_pfn(zone) - 1); |
02333641 VB |
236 | } |
237 | ||
9721fd82 BW |
238 | #ifdef CONFIG_SPARSEMEM |
239 | /* | |
240 | * If the PFN falls into an offline section, return the start PFN of the | |
241 | * next online section. If the PFN falls into an online section or if | |
242 | * there is no next online section, return 0. | |
243 | */ | |
244 | static unsigned long skip_offline_sections(unsigned long start_pfn) | |
245 | { | |
246 | unsigned long start_nr = pfn_to_section_nr(start_pfn); | |
247 | ||
248 | if (online_section_nr(start_nr)) | |
249 | return 0; | |
250 | ||
251 | while (++start_nr <= __highest_present_section_nr) { | |
252 | if (online_section_nr(start_nr)) | |
253 | return section_nr_to_pfn(start_nr); | |
254 | } | |
255 | ||
256 | return 0; | |
257 | } | |
e6e0c767 BW |
258 | |
259 | /* | |
260 | * If the PFN falls into an offline section, return the end PFN of the | |
261 | * next online section in reverse. If the PFN falls into an online section | |
262 | * or if there is no next online section in reverse, return 0. | |
263 | */ | |
264 | static unsigned long skip_offline_sections_reverse(unsigned long start_pfn) | |
265 | { | |
266 | unsigned long start_nr = pfn_to_section_nr(start_pfn); | |
267 | ||
268 | if (!start_nr || online_section_nr(start_nr)) | |
269 | return 0; | |
270 | ||
271 | while (start_nr-- > 0) { | |
272 | if (online_section_nr(start_nr)) | |
273 | return section_nr_to_pfn(start_nr) + PAGES_PER_SECTION; | |
274 | } | |
275 | ||
276 | return 0; | |
277 | } | |
9721fd82 BW |
278 | #else |
279 | static unsigned long skip_offline_sections(unsigned long start_pfn) | |
280 | { | |
281 | return 0; | |
282 | } | |
e6e0c767 BW |
283 | |
284 | static unsigned long skip_offline_sections_reverse(unsigned long start_pfn) | |
285 | { | |
286 | return 0; | |
287 | } | |
9721fd82 BW |
288 | #endif |
289 | ||
21dc7e02 | 290 | /* |
2271b016 | 291 | * Compound pages of >= pageblock_order should consistently be skipped until |
b527cfe5 VB |
292 | * released. It is always pointless to compact pages of such order (if they are |
293 | * migratable), and the pageblocks they occupy cannot contain any free pages. | |
21dc7e02 | 294 | */ |
b527cfe5 | 295 | static bool pageblock_skip_persistent(struct page *page) |
21dc7e02 | 296 | { |
b527cfe5 | 297 | if (!PageCompound(page)) |
21dc7e02 | 298 | return false; |
b527cfe5 VB |
299 | |
300 | page = compound_head(page); | |
301 | ||
302 | if (compound_order(page) >= pageblock_order) | |
303 | return true; | |
304 | ||
305 | return false; | |
21dc7e02 DR |
306 | } |
307 | ||
e332f741 MG |
308 | static bool |
309 | __reset_isolation_pfn(struct zone *zone, unsigned long pfn, bool check_source, | |
310 | bool check_target) | |
311 | { | |
312 | struct page *page = pfn_to_online_page(pfn); | |
6b0868c8 | 313 | struct page *block_page; |
e332f741 MG |
314 | struct page *end_page; |
315 | unsigned long block_pfn; | |
316 | ||
317 | if (!page) | |
318 | return false; | |
319 | if (zone != page_zone(page)) | |
320 | return false; | |
321 | if (pageblock_skip_persistent(page)) | |
322 | return false; | |
323 | ||
324 | /* | |
325 | * If skip is already cleared do no further checking once the | |
326 | * restart points have been set. | |
327 | */ | |
328 | if (check_source && check_target && !get_pageblock_skip(page)) | |
329 | return true; | |
330 | ||
331 | /* | |
332 | * If clearing skip for the target scanner, do not select a | |
333 | * non-movable pageblock as the starting point. | |
334 | */ | |
335 | if (!check_source && check_target && | |
336 | get_pageblock_migratetype(page) != MIGRATE_MOVABLE) | |
337 | return false; | |
338 | ||
6b0868c8 MG |
339 | /* Ensure the start of the pageblock or zone is online and valid */ |
340 | block_pfn = pageblock_start_pfn(pfn); | |
a2e9a5af VB |
341 | block_pfn = max(block_pfn, zone->zone_start_pfn); |
342 | block_page = pfn_to_online_page(block_pfn); | |
6b0868c8 MG |
343 | if (block_page) { |
344 | page = block_page; | |
345 | pfn = block_pfn; | |
346 | } | |
347 | ||
348 | /* Ensure the end of the pageblock or zone is online and valid */ | |
a2e9a5af | 349 | block_pfn = pageblock_end_pfn(pfn) - 1; |
6b0868c8 MG |
350 | block_pfn = min(block_pfn, zone_end_pfn(zone) - 1); |
351 | end_page = pfn_to_online_page(block_pfn); | |
352 | if (!end_page) | |
353 | return false; | |
354 | ||
e332f741 MG |
355 | /* |
356 | * Only clear the hint if a sample indicates there is either a | |
357 | * free page or an LRU page in the block. One or other condition | |
358 | * is necessary for the block to be a migration source/target. | |
359 | */ | |
e332f741 | 360 | do { |
859a85dd MR |
361 | if (check_source && PageLRU(page)) { |
362 | clear_pageblock_skip(page); | |
363 | return true; | |
364 | } | |
e332f741 | 365 | |
859a85dd MR |
366 | if (check_target && PageBuddy(page)) { |
367 | clear_pageblock_skip(page); | |
368 | return true; | |
e332f741 MG |
369 | } |
370 | ||
371 | page += (1 << PAGE_ALLOC_COSTLY_ORDER); | |
a2e9a5af | 372 | } while (page <= end_page); |
e332f741 MG |
373 | |
374 | return false; | |
375 | } | |
376 | ||
bb13ffeb MG |
377 | /* |
378 | * This function is called to clear all cached information on pageblocks that | |
379 | * should be skipped for page isolation when the migrate and free page scanner | |
380 | * meet. | |
381 | */ | |
62997027 | 382 | static void __reset_isolation_suitable(struct zone *zone) |
bb13ffeb | 383 | { |
e332f741 | 384 | unsigned long migrate_pfn = zone->zone_start_pfn; |
6b0868c8 | 385 | unsigned long free_pfn = zone_end_pfn(zone) - 1; |
e332f741 MG |
386 | unsigned long reset_migrate = free_pfn; |
387 | unsigned long reset_free = migrate_pfn; | |
388 | bool source_set = false; | |
389 | bool free_set = false; | |
390 | ||
8df4e28c | 391 | /* Only flush if a full compaction finished recently */ |
e332f741 MG |
392 | if (!zone->compact_blockskip_flush) |
393 | return; | |
bb13ffeb | 394 | |
62997027 | 395 | zone->compact_blockskip_flush = false; |
bb13ffeb | 396 | |
e332f741 MG |
397 | /* |
398 | * Walk the zone and update pageblock skip information. Source looks | |
399 | * for PageLRU while target looks for PageBuddy. When the scanner | |
400 | * is found, both PageBuddy and PageLRU are checked as the pageblock | |
401 | * is suitable as both source and target. | |
402 | */ | |
403 | for (; migrate_pfn < free_pfn; migrate_pfn += pageblock_nr_pages, | |
404 | free_pfn -= pageblock_nr_pages) { | |
bb13ffeb MG |
405 | cond_resched(); |
406 | ||
e332f741 MG |
407 | /* Update the migrate PFN */ |
408 | if (__reset_isolation_pfn(zone, migrate_pfn, true, source_set) && | |
409 | migrate_pfn < reset_migrate) { | |
410 | source_set = true; | |
411 | reset_migrate = migrate_pfn; | |
412 | zone->compact_init_migrate_pfn = reset_migrate; | |
413 | zone->compact_cached_migrate_pfn[0] = reset_migrate; | |
414 | zone->compact_cached_migrate_pfn[1] = reset_migrate; | |
415 | } | |
bb13ffeb | 416 | |
e332f741 MG |
417 | /* Update the free PFN */ |
418 | if (__reset_isolation_pfn(zone, free_pfn, free_set, true) && | |
419 | free_pfn > reset_free) { | |
420 | free_set = true; | |
421 | reset_free = free_pfn; | |
422 | zone->compact_init_free_pfn = reset_free; | |
423 | zone->compact_cached_free_pfn = reset_free; | |
424 | } | |
bb13ffeb | 425 | } |
02333641 | 426 | |
e332f741 MG |
427 | /* Leave no distance if no suitable block was reset */ |
428 | if (reset_migrate >= reset_free) { | |
429 | zone->compact_cached_migrate_pfn[0] = migrate_pfn; | |
430 | zone->compact_cached_migrate_pfn[1] = migrate_pfn; | |
431 | zone->compact_cached_free_pfn = free_pfn; | |
432 | } | |
bb13ffeb MG |
433 | } |
434 | ||
62997027 MG |
435 | void reset_isolation_suitable(pg_data_t *pgdat) |
436 | { | |
437 | int zoneid; | |
438 | ||
439 | for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { | |
440 | struct zone *zone = &pgdat->node_zones[zoneid]; | |
441 | if (!populated_zone(zone)) | |
442 | continue; | |
443 | ||
8df4e28c | 444 | __reset_isolation_suitable(zone); |
62997027 MG |
445 | } |
446 | } | |
447 | ||
e380bebe MG |
448 | /* |
449 | * Sets the pageblock skip bit if it was clear. Note that this is a hint as | |
450 | * locks are not required for read/writers. Returns true if it was already set. | |
451 | */ | |
590ccea8 | 452 | static bool test_and_set_skip(struct compact_control *cc, struct page *page) |
e380bebe MG |
453 | { |
454 | bool skip; | |
455 | ||
590ccea8 | 456 | /* Do not update if skip hint is being ignored */ |
e380bebe MG |
457 | if (cc->ignore_skip_hint) |
458 | return false; | |
459 | ||
e380bebe MG |
460 | skip = get_pageblock_skip(page); |
461 | if (!skip && !cc->no_set_skip_hint) | |
462 | set_pageblock_skip(page); | |
463 | ||
464 | return skip; | |
465 | } | |
466 | ||
467 | static void update_cached_migrate(struct compact_control *cc, unsigned long pfn) | |
468 | { | |
469 | struct zone *zone = cc->zone; | |
470 | ||
e380bebe MG |
471 | /* Set for isolation rather than compaction */ |
472 | if (cc->no_set_skip_hint) | |
473 | return; | |
474 | ||
3c099a2b KS |
475 | pfn = pageblock_end_pfn(pfn); |
476 | ||
cf043a00 | 477 | /* Update where async and sync compaction should restart */ |
e380bebe MG |
478 | if (pfn > zone->compact_cached_migrate_pfn[0]) |
479 | zone->compact_cached_migrate_pfn[0] = pfn; | |
480 | if (cc->mode != MIGRATE_ASYNC && | |
481 | pfn > zone->compact_cached_migrate_pfn[1]) | |
482 | zone->compact_cached_migrate_pfn[1] = pfn; | |
483 | } | |
484 | ||
bb13ffeb MG |
485 | /* |
486 | * If no pages were isolated then mark this pageblock to be skipped in the | |
62997027 | 487 | * future. The information is later cleared by __reset_isolation_suitable(). |
bb13ffeb | 488 | */ |
c89511ab | 489 | static void update_pageblock_skip(struct compact_control *cc, |
d097a6f6 | 490 | struct page *page, unsigned long pfn) |
bb13ffeb | 491 | { |
c89511ab | 492 | struct zone *zone = cc->zone; |
6815bf3f | 493 | |
2583d671 | 494 | if (cc->no_set_skip_hint) |
6815bf3f JK |
495 | return; |
496 | ||
edc2ca61 | 497 | set_pageblock_skip(page); |
c89511ab | 498 | |
e380bebe MG |
499 | if (pfn < zone->compact_cached_free_pfn) |
500 | zone->compact_cached_free_pfn = pfn; | |
bb13ffeb MG |
501 | } |
502 | #else | |
503 | static inline bool isolation_suitable(struct compact_control *cc, | |
504 | struct page *page) | |
505 | { | |
506 | return true; | |
507 | } | |
508 | ||
b527cfe5 | 509 | static inline bool pageblock_skip_persistent(struct page *page) |
21dc7e02 DR |
510 | { |
511 | return false; | |
512 | } | |
513 | ||
514 | static inline void update_pageblock_skip(struct compact_control *cc, | |
d097a6f6 | 515 | struct page *page, unsigned long pfn) |
bb13ffeb MG |
516 | { |
517 | } | |
e380bebe MG |
518 | |
519 | static void update_cached_migrate(struct compact_control *cc, unsigned long pfn) | |
520 | { | |
521 | } | |
522 | ||
590ccea8 | 523 | static bool test_and_set_skip(struct compact_control *cc, struct page *page) |
e380bebe MG |
524 | { |
525 | return false; | |
526 | } | |
bb13ffeb MG |
527 | #endif /* CONFIG_COMPACTION */ |
528 | ||
8b44d279 VB |
529 | /* |
530 | * Compaction requires the taking of some coarse locks that are potentially | |
cb2dcaf0 MG |
531 | * very heavily contended. For async compaction, trylock and record if the |
532 | * lock is contended. The lock will still be acquired but compaction will | |
533 | * abort when the current block is finished regardless of success rate. | |
534 | * Sync compaction acquires the lock. | |
8b44d279 | 535 | * |
cb2dcaf0 | 536 | * Always returns true which makes it easier to track lock state in callers. |
8b44d279 | 537 | */ |
cb2dcaf0 | 538 | static bool compact_lock_irqsave(spinlock_t *lock, unsigned long *flags, |
8b44d279 | 539 | struct compact_control *cc) |
77337ede | 540 | __acquires(lock) |
2a1402aa | 541 | { |
cb2dcaf0 MG |
542 | /* Track if the lock is contended in async mode */ |
543 | if (cc->mode == MIGRATE_ASYNC && !cc->contended) { | |
544 | if (spin_trylock_irqsave(lock, *flags)) | |
545 | return true; | |
546 | ||
547 | cc->contended = true; | |
8b44d279 | 548 | } |
1f9efdef | 549 | |
cb2dcaf0 | 550 | spin_lock_irqsave(lock, *flags); |
8b44d279 | 551 | return true; |
2a1402aa MG |
552 | } |
553 | ||
c67fe375 MG |
554 | /* |
555 | * Compaction requires the taking of some coarse locks that are potentially | |
8b44d279 VB |
556 | * very heavily contended. The lock should be periodically unlocked to avoid |
557 | * having disabled IRQs for a long time, even when there is nobody waiting on | |
558 | * the lock. It might also be that allowing the IRQs will result in | |
d56c1584 | 559 | * need_resched() becoming true. If scheduling is needed, compaction schedules. |
8b44d279 VB |
560 | * Either compaction type will also abort if a fatal signal is pending. |
561 | * In either case if the lock was locked, it is dropped and not regained. | |
c67fe375 | 562 | * |
d56c1584 ML |
563 | * Returns true if compaction should abort due to fatal signal pending. |
564 | * Returns false when compaction can continue. | |
c67fe375 | 565 | */ |
8b44d279 VB |
566 | static bool compact_unlock_should_abort(spinlock_t *lock, |
567 | unsigned long flags, bool *locked, struct compact_control *cc) | |
c67fe375 | 568 | { |
8b44d279 VB |
569 | if (*locked) { |
570 | spin_unlock_irqrestore(lock, flags); | |
571 | *locked = false; | |
572 | } | |
1f9efdef | 573 | |
8b44d279 | 574 | if (fatal_signal_pending(current)) { |
c3486f53 | 575 | cc->contended = true; |
8b44d279 VB |
576 | return true; |
577 | } | |
c67fe375 | 578 | |
cf66f070 | 579 | cond_resched(); |
be976572 VB |
580 | |
581 | return false; | |
582 | } | |
583 | ||
85aa125f | 584 | /* |
9e4be470 JM |
585 | * Isolate free pages onto a private freelist. If @strict is true, will abort |
586 | * returning 0 on any invalid PFNs or non-free pages inside of the pageblock | |
587 | * (even though it may still end up isolating some pages). | |
85aa125f | 588 | */ |
f40d1e42 | 589 | static unsigned long isolate_freepages_block(struct compact_control *cc, |
e14c720e | 590 | unsigned long *start_pfn, |
85aa125f MN |
591 | unsigned long end_pfn, |
592 | struct list_head *freelist, | |
4fca9730 | 593 | unsigned int stride, |
85aa125f | 594 | bool strict) |
748446bb | 595 | { |
b7aba698 | 596 | int nr_scanned = 0, total_isolated = 0; |
dc13292c | 597 | struct page *page; |
b8b2d825 | 598 | unsigned long flags = 0; |
f40d1e42 | 599 | bool locked = false; |
e14c720e | 600 | unsigned long blockpfn = *start_pfn; |
66c64223 | 601 | unsigned int order; |
748446bb | 602 | |
4fca9730 MG |
603 | /* Strict mode is for isolation, speed is secondary */ |
604 | if (strict) | |
605 | stride = 1; | |
606 | ||
dc13292c | 607 | page = pfn_to_page(blockpfn); |
748446bb | 608 | |
f40d1e42 | 609 | /* Isolate free pages. */ |
dc13292c | 610 | for (; blockpfn < end_pfn; blockpfn += stride, page += stride) { |
66c64223 | 611 | int isolated; |
748446bb | 612 | |
8b44d279 VB |
613 | /* |
614 | * Periodically drop the lock (if held) regardless of its | |
615 | * contention, to give chance to IRQs. Abort if fatal signal | |
d56c1584 | 616 | * pending. |
8b44d279 | 617 | */ |
c036ddff | 618 | if (!(blockpfn % COMPACT_CLUSTER_MAX) |
8b44d279 VB |
619 | && compact_unlock_should_abort(&cc->zone->lock, flags, |
620 | &locked, cc)) | |
621 | break; | |
622 | ||
b7aba698 | 623 | nr_scanned++; |
2af120bc | 624 | |
9fcd6d2e VB |
625 | /* |
626 | * For compound pages such as THP and hugetlbfs, we can save | |
627 | * potentially a lot of iterations if we skip them at once. | |
628 | * The check is racy, but we can consider only valid values | |
629 | * and the only danger is skipping too much. | |
630 | */ | |
631 | if (PageCompound(page)) { | |
21dc7e02 DR |
632 | const unsigned int order = compound_order(page); |
633 | ||
d1366e74 LS |
634 | if ((order <= MAX_PAGE_ORDER) && |
635 | (blockpfn + (1UL << order) <= end_pfn)) { | |
21dc7e02 | 636 | blockpfn += (1UL << order) - 1; |
dc13292c | 637 | page += (1UL << order) - 1; |
56d48d8d | 638 | nr_scanned += (1UL << order) - 1; |
9fcd6d2e | 639 | } |
3da0272a | 640 | |
9fcd6d2e VB |
641 | goto isolate_fail; |
642 | } | |
643 | ||
f40d1e42 | 644 | if (!PageBuddy(page)) |
2af120bc | 645 | goto isolate_fail; |
f40d1e42 | 646 | |
85f73e6d | 647 | /* If we already hold the lock, we can skip some rechecking. */ |
69b7189f | 648 | if (!locked) { |
cb2dcaf0 | 649 | locked = compact_lock_irqsave(&cc->zone->lock, |
8b44d279 | 650 | &flags, cc); |
f40d1e42 | 651 | |
69b7189f VB |
652 | /* Recheck this is a buddy page under lock */ |
653 | if (!PageBuddy(page)) | |
654 | goto isolate_fail; | |
655 | } | |
748446bb | 656 | |
66c64223 | 657 | /* Found a free page, will break it into order-0 pages */ |
ab130f91 | 658 | order = buddy_order(page); |
66c64223 | 659 | isolated = __isolate_free_page(page, order); |
a4f04f2c DR |
660 | if (!isolated) |
661 | break; | |
66c64223 | 662 | set_page_private(page, order); |
a4f04f2c | 663 | |
b717d6b9 | 664 | nr_scanned += isolated - 1; |
748446bb | 665 | total_isolated += isolated; |
a4f04f2c | 666 | cc->nr_freepages += isolated; |
733aea0b | 667 | list_add_tail(&page->lru, &freelist[order]); |
66c64223 | 668 | |
a4f04f2c DR |
669 | if (!strict && cc->nr_migratepages <= cc->nr_freepages) { |
670 | blockpfn += isolated; | |
671 | break; | |
748446bb | 672 | } |
a4f04f2c DR |
673 | /* Advance to the end of split page */ |
674 | blockpfn += isolated - 1; | |
dc13292c | 675 | page += isolated - 1; |
a4f04f2c | 676 | continue; |
2af120bc LA |
677 | |
678 | isolate_fail: | |
679 | if (strict) | |
680 | break; | |
2af120bc | 681 | |
748446bb MG |
682 | } |
683 | ||
a4f04f2c DR |
684 | if (locked) |
685 | spin_unlock_irqrestore(&cc->zone->lock, flags); | |
686 | ||
9fcd6d2e | 687 | /* |
3da0272a | 688 | * Be careful to not go outside of the pageblock. |
9fcd6d2e VB |
689 | */ |
690 | if (unlikely(blockpfn > end_pfn)) | |
691 | blockpfn = end_pfn; | |
692 | ||
e34d85f0 JK |
693 | trace_mm_compaction_isolate_freepages(*start_pfn, blockpfn, |
694 | nr_scanned, total_isolated); | |
695 | ||
e14c720e VB |
696 | /* Record how far we have got within the block */ |
697 | *start_pfn = blockpfn; | |
698 | ||
f40d1e42 MG |
699 | /* |
700 | * If strict isolation is requested by CMA then check that all the | |
701 | * pages requested were isolated. If there were any failures, 0 is | |
702 | * returned and CMA will fail. | |
703 | */ | |
2af120bc | 704 | if (strict && blockpfn < end_pfn) |
f40d1e42 MG |
705 | total_isolated = 0; |
706 | ||
7f354a54 | 707 | cc->total_free_scanned += nr_scanned; |
397487db | 708 | if (total_isolated) |
010fc29a | 709 | count_compact_events(COMPACTISOLATED, total_isolated); |
748446bb MG |
710 | return total_isolated; |
711 | } | |
712 | ||
85aa125f MN |
713 | /** |
714 | * isolate_freepages_range() - isolate free pages. | |
e8b098fc | 715 | * @cc: Compaction control structure. |
85aa125f MN |
716 | * @start_pfn: The first PFN to start isolating. |
717 | * @end_pfn: The one-past-last PFN. | |
718 | * | |
719 | * Non-free pages, invalid PFNs, or zone boundaries within the | |
720 | * [start_pfn, end_pfn) range are considered errors, cause function to | |
e98337d1 | 721 | * undo its actions and return zero. cc->freepages[] are empty. |
85aa125f MN |
722 | * |
723 | * Otherwise, function returns one-past-the-last PFN of isolated page | |
724 | * (which may be greater then end_pfn if end fell in a middle of | |
e98337d1 | 725 | * a free page). cc->freepages[] contain free pages isolated. |
85aa125f | 726 | */ |
ff9543fd | 727 | unsigned long |
bb13ffeb MG |
728 | isolate_freepages_range(struct compact_control *cc, |
729 | unsigned long start_pfn, unsigned long end_pfn) | |
85aa125f | 730 | { |
e1409c32 | 731 | unsigned long isolated, pfn, block_start_pfn, block_end_pfn; |
733aea0b | 732 | int order; |
733aea0b ZY |
733 | |
734 | for (order = 0; order < NR_PAGE_ORDERS; order++) | |
e98337d1 | 735 | INIT_LIST_HEAD(&cc->freepages[order]); |
85aa125f | 736 | |
7d49d886 | 737 | pfn = start_pfn; |
06b6640a | 738 | block_start_pfn = pageblock_start_pfn(pfn); |
e1409c32 JK |
739 | if (block_start_pfn < cc->zone->zone_start_pfn) |
740 | block_start_pfn = cc->zone->zone_start_pfn; | |
06b6640a | 741 | block_end_pfn = pageblock_end_pfn(pfn); |
7d49d886 VB |
742 | |
743 | for (; pfn < end_pfn; pfn += isolated, | |
e1409c32 | 744 | block_start_pfn = block_end_pfn, |
7d49d886 | 745 | block_end_pfn += pageblock_nr_pages) { |
e14c720e VB |
746 | /* Protect pfn from changing by isolate_freepages_block */ |
747 | unsigned long isolate_start_pfn = pfn; | |
85aa125f | 748 | |
58420016 JK |
749 | /* |
750 | * pfn could pass the block_end_pfn if isolated freepage | |
751 | * is more than pageblock order. In this case, we adjust | |
752 | * scanning range to right one. | |
753 | */ | |
754 | if (pfn >= block_end_pfn) { | |
06b6640a VB |
755 | block_start_pfn = pageblock_start_pfn(pfn); |
756 | block_end_pfn = pageblock_end_pfn(pfn); | |
58420016 JK |
757 | } |
758 | ||
a2864a67 KS |
759 | block_end_pfn = min(block_end_pfn, end_pfn); |
760 | ||
e1409c32 JK |
761 | if (!pageblock_pfn_to_page(block_start_pfn, |
762 | block_end_pfn, cc->zone)) | |
7d49d886 VB |
763 | break; |
764 | ||
e14c720e | 765 | isolated = isolate_freepages_block(cc, &isolate_start_pfn, |
e98337d1 | 766 | block_end_pfn, cc->freepages, 0, true); |
85aa125f MN |
767 | |
768 | /* | |
769 | * In strict mode, isolate_freepages_block() returns 0 if | |
770 | * there are any holes in the block (ie. invalid PFNs or | |
771 | * non-free pages). | |
772 | */ | |
773 | if (!isolated) | |
774 | break; | |
775 | ||
776 | /* | |
777 | * If we managed to isolate pages, it is always (1 << n) * | |
778 | * pageblock_nr_pages for some non-negative n. (Max order | |
779 | * page may span two pageblocks). | |
780 | */ | |
781 | } | |
782 | ||
85aa125f MN |
783 | if (pfn < end_pfn) { |
784 | /* Loop terminated early, cleanup. */ | |
e98337d1 | 785 | release_free_list(cc->freepages); |
85aa125f MN |
786 | return 0; |
787 | } | |
788 | ||
789 | /* We don't use freelists for anything. */ | |
790 | return pfn; | |
791 | } | |
792 | ||
748446bb | 793 | /* Similar to reclaim, but different enough that they don't share logic */ |
4fbbb3fd | 794 | static bool too_many_isolated(struct compact_control *cc) |
748446bb | 795 | { |
4fbbb3fd | 796 | pg_data_t *pgdat = cc->zone->zone_pgdat; |
d818fca1 MG |
797 | bool too_many; |
798 | ||
bc693045 | 799 | unsigned long active, inactive, isolated; |
748446bb | 800 | |
5f438eee AR |
801 | inactive = node_page_state(pgdat, NR_INACTIVE_FILE) + |
802 | node_page_state(pgdat, NR_INACTIVE_ANON); | |
803 | active = node_page_state(pgdat, NR_ACTIVE_FILE) + | |
804 | node_page_state(pgdat, NR_ACTIVE_ANON); | |
805 | isolated = node_page_state(pgdat, NR_ISOLATED_FILE) + | |
806 | node_page_state(pgdat, NR_ISOLATED_ANON); | |
748446bb | 807 | |
4fbbb3fd JW |
808 | /* |
809 | * Allow GFP_NOFS to isolate past the limit set for regular | |
810 | * compaction runs. This prevents an ABBA deadlock when other | |
811 | * compactors have already isolated to the limit, but are | |
812 | * blocked on filesystem locks held by the GFP_NOFS thread. | |
813 | */ | |
814 | if (cc->gfp_mask & __GFP_FS) { | |
815 | inactive >>= 3; | |
816 | active >>= 3; | |
817 | } | |
818 | ||
d818fca1 MG |
819 | too_many = isolated > (inactive + active) / 2; |
820 | if (!too_many) | |
821 | wake_throttle_isolated(pgdat); | |
822 | ||
823 | return too_many; | |
748446bb MG |
824 | } |
825 | ||
ee6f62fd ZY |
826 | /** |
827 | * skip_isolation_on_order() - determine when to skip folio isolation based on | |
828 | * folio order and compaction target order | |
829 | * @order: to-be-isolated folio order | |
830 | * @target_order: compaction target order | |
831 | * | |
832 | * This avoids unnecessary folio isolations during compaction. | |
833 | */ | |
834 | static bool skip_isolation_on_order(int order, int target_order) | |
835 | { | |
836 | /* | |
837 | * Unless we are performing global compaction (i.e., | |
838 | * is_via_compact_memory), skip any folios that are larger than the | |
839 | * target order: we wouldn't be here if we'd have a free folio with | |
840 | * the desired target_order, so migrating this folio would likely fail | |
841 | * later. | |
842 | */ | |
843 | if (!is_via_compact_memory(target_order) && order >= target_order) | |
844 | return true; | |
845 | /* | |
846 | * We limit memory compaction to pageblocks and won't try | |
847 | * creating free blocks of memory that are larger than that. | |
848 | */ | |
849 | return order >= pageblock_order; | |
850 | } | |
851 | ||
2fe86e00 | 852 | /** |
edc2ca61 VB |
853 | * isolate_migratepages_block() - isolate all migrate-able pages within |
854 | * a single pageblock | |
2fe86e00 | 855 | * @cc: Compaction control structure. |
edc2ca61 VB |
856 | * @low_pfn: The first PFN to isolate |
857 | * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock | |
89f6c88a | 858 | * @mode: Isolation mode to be used. |
2fe86e00 MN |
859 | * |
860 | * Isolate all pages that can be migrated from the range specified by | |
edc2ca61 | 861 | * [low_pfn, end_pfn). The range is expected to be within same pageblock. |
c2ad7a1f | 862 | * Returns errno, like -EAGAIN or -EINTR in case e.g signal pending or congestion, |
369fa227 | 863 | * -ENOMEM in case we could not allocate a page, or 0. |
c2ad7a1f | 864 | * cc->migrate_pfn will contain the next pfn to scan. |
2fe86e00 | 865 | * |
edc2ca61 | 866 | * The pages are isolated on cc->migratepages list (not required to be empty), |
c2ad7a1f | 867 | * and cc->nr_migratepages is updated accordingly. |
748446bb | 868 | */ |
c2ad7a1f | 869 | static int |
edc2ca61 | 870 | isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, |
89f6c88a | 871 | unsigned long end_pfn, isolate_mode_t mode) |
748446bb | 872 | { |
5f438eee | 873 | pg_data_t *pgdat = cc->zone->zone_pgdat; |
b7aba698 | 874 | unsigned long nr_scanned = 0, nr_isolated = 0; |
fa9add64 | 875 | struct lruvec *lruvec; |
b8b2d825 | 876 | unsigned long flags = 0; |
6168d0da | 877 | struct lruvec *locked = NULL; |
56ae0bb3 | 878 | struct folio *folio = NULL; |
bb13ffeb | 879 | struct page *page = NULL, *valid_page = NULL; |
89f6c88a | 880 | struct address_space *mapping; |
e34d85f0 | 881 | unsigned long start_pfn = low_pfn; |
fdd048e1 VB |
882 | bool skip_on_failure = false; |
883 | unsigned long next_skip_pfn = 0; | |
e380bebe | 884 | bool skip_updated = false; |
c2ad7a1f OS |
885 | int ret = 0; |
886 | ||
887 | cc->migrate_pfn = low_pfn; | |
748446bb | 888 | |
748446bb MG |
889 | /* |
890 | * Ensure that there are not too many pages isolated from the LRU | |
891 | * list by either parallel reclaimers or compaction. If there are, | |
892 | * delay for some time until fewer pages are isolated | |
893 | */ | |
4fbbb3fd | 894 | while (unlikely(too_many_isolated(cc))) { |
d20bdd57 ZY |
895 | /* stop isolation if there are still pages not migrated */ |
896 | if (cc->nr_migratepages) | |
c2ad7a1f | 897 | return -EAGAIN; |
d20bdd57 | 898 | |
f9e35b3b | 899 | /* async migration should just abort */ |
e0b9daeb | 900 | if (cc->mode == MIGRATE_ASYNC) |
c2ad7a1f | 901 | return -EAGAIN; |
f9e35b3b | 902 | |
c3f4a9a2 | 903 | reclaim_throttle(pgdat, VMSCAN_THROTTLE_ISOLATED); |
748446bb MG |
904 | |
905 | if (fatal_signal_pending(current)) | |
c2ad7a1f | 906 | return -EINTR; |
748446bb MG |
907 | } |
908 | ||
cf66f070 | 909 | cond_resched(); |
aeef4b83 | 910 | |
fdd048e1 VB |
911 | if (cc->direct_compaction && (cc->mode == MIGRATE_ASYNC)) { |
912 | skip_on_failure = true; | |
913 | next_skip_pfn = block_end_pfn(low_pfn, cc->order); | |
914 | } | |
915 | ||
748446bb | 916 | /* Time to isolate some pages for migration */ |
748446bb | 917 | for (; low_pfn < end_pfn; low_pfn++) { |
0003e2a4 | 918 | bool is_dirty, is_unevictable; |
29c0dde8 | 919 | |
fdd048e1 VB |
920 | if (skip_on_failure && low_pfn >= next_skip_pfn) { |
921 | /* | |
922 | * We have isolated all migration candidates in the | |
923 | * previous order-aligned block, and did not skip it due | |
924 | * to failure. We should migrate the pages now and | |
925 | * hopefully succeed compaction. | |
926 | */ | |
927 | if (nr_isolated) | |
928 | break; | |
929 | ||
930 | /* | |
931 | * We failed to isolate in the previous order-aligned | |
932 | * block. Set the new boundary to the end of the | |
933 | * current block. Note we can't simply increase | |
934 | * next_skip_pfn by 1 << order, as low_pfn might have | |
935 | * been incremented by a higher number due to skipping | |
936 | * a compound or a high-order buddy page in the | |
937 | * previous loop iteration. | |
938 | */ | |
939 | next_skip_pfn = block_end_pfn(low_pfn, cc->order); | |
940 | } | |
941 | ||
8b44d279 VB |
942 | /* |
943 | * Periodically drop the lock (if held) regardless of its | |
670105a2 MG |
944 | * contention, to give chance to IRQs. Abort completely if |
945 | * a fatal signal is pending. | |
8b44d279 | 946 | */ |
c036ddff | 947 | if (!(low_pfn % COMPACT_CLUSTER_MAX)) { |
6168d0da AS |
948 | if (locked) { |
949 | unlock_page_lruvec_irqrestore(locked, flags); | |
950 | locked = NULL; | |
951 | } | |
952 | ||
953 | if (fatal_signal_pending(current)) { | |
954 | cc->contended = true; | |
c2ad7a1f | 955 | ret = -EINTR; |
6168d0da | 956 | |
6168d0da AS |
957 | goto fatal_pending; |
958 | } | |
959 | ||
960 | cond_resched(); | |
670105a2 | 961 | } |
c67fe375 | 962 | |
b7aba698 | 963 | nr_scanned++; |
748446bb | 964 | |
748446bb | 965 | page = pfn_to_page(low_pfn); |
dc908600 | 966 | |
e380bebe MG |
967 | /* |
968 | * Check if the pageblock has already been marked skipped. | |
493614da | 969 | * Only the first PFN is checked as the caller isolates |
e380bebe MG |
970 | * COMPACT_CLUSTER_MAX at a time so the second call must |
971 | * not falsely conclude that the block should be skipped. | |
972 | */ | |
493614da JW |
973 | if (!valid_page && (pageblock_aligned(low_pfn) || |
974 | low_pfn == cc->zone->zone_start_pfn)) { | |
4af12d04 | 975 | if (!isolation_suitable(cc, page)) { |
e380bebe | 976 | low_pfn = end_pfn; |
56ae0bb3 | 977 | folio = NULL; |
e380bebe MG |
978 | goto isolate_abort; |
979 | } | |
bb13ffeb | 980 | valid_page = page; |
e380bebe | 981 | } |
bb13ffeb | 982 | |
ee6f62fd | 983 | if (PageHuge(page)) { |
a84edd52 | 984 | const unsigned int order = compound_order(page); |
ee6f62fd ZY |
985 | /* |
986 | * skip hugetlbfs if we are not compacting for pages | |
987 | * bigger than its order. THPs and other compound pages | |
988 | * are handled below. | |
989 | */ | |
990 | if (!cc->alloc_contig) { | |
ee6f62fd ZY |
991 | |
992 | if (order <= MAX_PAGE_ORDER) { | |
993 | low_pfn += (1UL << order) - 1; | |
994 | nr_scanned += (1UL << order) - 1; | |
995 | } | |
996 | goto isolate_fail; | |
997 | } | |
998 | /* for alloc_contig case */ | |
1c06b6a5 BW |
999 | if (locked) { |
1000 | unlock_page_lruvec_irqrestore(locked, flags); | |
1001 | locked = NULL; | |
1002 | } | |
1003 | ||
b4c829fa VMO |
1004 | folio = page_folio(page); |
1005 | ret = isolate_or_dissolve_huge_folio(folio, &cc->migratepages); | |
369fa227 OS |
1006 | |
1007 | /* | |
b4c829fa | 1008 | * Fail isolation in case isolate_or_dissolve_huge_folio() |
369fa227 OS |
1009 | * reports an error. In case of -ENOMEM, abort right away. |
1010 | */ | |
1011 | if (ret < 0) { | |
1012 | /* Do not report -EBUSY down the chain */ | |
1013 | if (ret == -EBUSY) | |
1014 | ret = 0; | |
a84edd52 VMO |
1015 | low_pfn += (1UL << order) - 1; |
1016 | nr_scanned += (1UL << order) - 1; | |
369fa227 OS |
1017 | goto isolate_fail; |
1018 | } | |
1019 | ||
b4c829fa | 1020 | if (folio_test_hugetlb(folio)) { |
ae37c7ff OS |
1021 | /* |
1022 | * Hugepage was successfully isolated and placed | |
1023 | * on the cc->migratepages list. | |
1024 | */ | |
56ae0bb3 | 1025 | low_pfn += folio_nr_pages(folio) - 1; |
ae37c7ff OS |
1026 | goto isolate_success_no_list; |
1027 | } | |
1028 | ||
369fa227 OS |
1029 | /* |
1030 | * Ok, the hugepage was dissolved. Now these pages are | |
1031 | * Buddy and cannot be re-allocated because they are | |
1032 | * isolated. Fall-through as the check below handles | |
1033 | * Buddy pages. | |
1034 | */ | |
1035 | } | |
1036 | ||
6c14466c | 1037 | /* |
99c0fd5e VB |
1038 | * Skip if free. We read page order here without zone lock |
1039 | * which is generally unsafe, but the race window is small and | |
1040 | * the worst thing that can happen is that we skip some | |
1041 | * potential isolation targets. | |
6c14466c | 1042 | */ |
99c0fd5e | 1043 | if (PageBuddy(page)) { |
ab130f91 | 1044 | unsigned long freepage_order = buddy_order_unsafe(page); |
99c0fd5e VB |
1045 | |
1046 | /* | |
1047 | * Without lock, we cannot be sure that what we got is | |
1048 | * a valid page order. Consider only values in the | |
1049 | * valid order range to prevent low_pfn overflow. | |
1050 | */ | |
5e0a760b | 1051 | if (freepage_order > 0 && freepage_order <= MAX_PAGE_ORDER) { |
99c0fd5e | 1052 | low_pfn += (1UL << freepage_order) - 1; |
56d48d8d BW |
1053 | nr_scanned += (1UL << freepage_order) - 1; |
1054 | } | |
748446bb | 1055 | continue; |
99c0fd5e | 1056 | } |
748446bb | 1057 | |
bc835011 | 1058 | /* |
ee6f62fd ZY |
1059 | * Regardless of being on LRU, compound pages such as THP |
1060 | * (hugetlbfs is handled above) are not to be compacted unless | |
1061 | * we are attempting an allocation larger than the compound | |
1062 | * page size. We can potentially save a lot of iterations if we | |
1063 | * skip them at once. The check is racy, but we can consider | |
1064 | * only valid values and the only danger is skipping too much. | |
bc835011 | 1065 | */ |
1da2f328 | 1066 | if (PageCompound(page) && !cc->alloc_contig) { |
21dc7e02 | 1067 | const unsigned int order = compound_order(page); |
edc2ca61 | 1068 | |
ee6f62fd ZY |
1069 | /* Skip based on page order and compaction target order. */ |
1070 | if (skip_isolation_on_order(order, cc->order)) { | |
1071 | if (order <= MAX_PAGE_ORDER) { | |
1072 | low_pfn += (1UL << order) - 1; | |
1073 | nr_scanned += (1UL << order) - 1; | |
1074 | } | |
1075 | goto isolate_fail; | |
56d48d8d | 1076 | } |
2a1402aa MG |
1077 | } |
1078 | ||
bda807d4 MK |
1079 | /* |
1080 | * Check may be lockless but that's ok as we recheck later. | |
1081 | * It's possible to migrate LRU and non-lru movable pages. | |
1082 | * Skip any other type of page | |
1083 | */ | |
1084 | if (!PageLRU(page)) { | |
bda807d4 MK |
1085 | /* |
1086 | * __PageMovable can return false positive so we need | |
1087 | * to verify it under page_lock. | |
1088 | */ | |
1089 | if (unlikely(__PageMovable(page)) && | |
1090 | !PageIsolated(page)) { | |
1091 | if (locked) { | |
6168d0da AS |
1092 | unlock_page_lruvec_irqrestore(locked, flags); |
1093 | locked = NULL; | |
bda807d4 MK |
1094 | } |
1095 | ||
56ae0bb3 KW |
1096 | if (isolate_movable_page(page, mode)) { |
1097 | folio = page_folio(page); | |
bda807d4 | 1098 | goto isolate_success; |
56ae0bb3 | 1099 | } |
bda807d4 MK |
1100 | } |
1101 | ||
fdd048e1 | 1102 | goto isolate_fail; |
bda807d4 | 1103 | } |
29c0dde8 | 1104 | |
829ae0f8 GS |
1105 | /* |
1106 | * Be careful not to clear PageLRU until after we're | |
1107 | * sure the page is not being freed elsewhere -- the | |
1108 | * page release code relies on it. | |
1109 | */ | |
56ae0bb3 KW |
1110 | folio = folio_get_nontail_page(page); |
1111 | if (unlikely(!folio)) | |
829ae0f8 GS |
1112 | goto isolate_fail; |
1113 | ||
119d6d59 DR |
1114 | /* |
1115 | * Migration will fail if an anonymous page is pinned in memory, | |
1116 | * so avoid taking lru_lock and isolating it unnecessarily in an | |
1117 | * admittedly racy check. | |
1118 | */ | |
56ae0bb3 KW |
1119 | mapping = folio_mapping(folio); |
1120 | if (!mapping && (folio_ref_count(folio) - 1) > folio_mapcount(folio)) | |
829ae0f8 | 1121 | goto isolate_fail_put; |
119d6d59 | 1122 | |
73e64c51 MH |
1123 | /* |
1124 | * Only allow to migrate anonymous pages in GFP_NOFS context | |
1125 | * because those do not depend on fs locks. | |
1126 | */ | |
89f6c88a | 1127 | if (!(cc->gfp_mask & __GFP_FS) && mapping) |
829ae0f8 | 1128 | goto isolate_fail_put; |
9df41314 | 1129 | |
89f6c88a | 1130 | /* Only take pages on LRU: a check now makes later tests safe */ |
56ae0bb3 | 1131 | if (!folio_test_lru(folio)) |
89f6c88a HD |
1132 | goto isolate_fail_put; |
1133 | ||
0003e2a4 SC |
1134 | is_unevictable = folio_test_unevictable(folio); |
1135 | ||
89f6c88a | 1136 | /* Compaction might skip unevictable pages but CMA takes them */ |
0003e2a4 | 1137 | if (!(mode & ISOLATE_UNEVICTABLE) && is_unevictable) |
89f6c88a HD |
1138 | goto isolate_fail_put; |
1139 | ||
1140 | /* | |
1141 | * To minimise LRU disruption, the caller can indicate with | |
1142 | * ISOLATE_ASYNC_MIGRATE that it only wants to isolate pages | |
1143 | * it will be able to migrate without blocking - clean pages | |
1144 | * for the most part. PageWriteback would require blocking. | |
1145 | */ | |
56ae0bb3 | 1146 | if ((mode & ISOLATE_ASYNC_MIGRATE) && folio_test_writeback(folio)) |
9df41314 AS |
1147 | goto isolate_fail_put; |
1148 | ||
0003e2a4 SC |
1149 | is_dirty = folio_test_dirty(folio); |
1150 | ||
1151 | if (((mode & ISOLATE_ASYNC_MIGRATE) && is_dirty) || | |
1152 | (mapping && is_unevictable)) { | |
1153 | bool migrate_dirty = true; | |
27e6a24a | 1154 | bool is_inaccessible; |
89f6c88a HD |
1155 | |
1156 | /* | |
866ff801 | 1157 | * Only folios without mappings or that have |
0003e2a4 SC |
1158 | * a ->migrate_folio callback are possible to migrate |
1159 | * without blocking. | |
1160 | * | |
27e6a24a | 1161 | * Folios from inaccessible mappings are not migratable. |
0003e2a4 SC |
1162 | * |
1163 | * However, we can be racing with truncation, which can | |
1164 | * free the mapping that we need to check. Truncation | |
1165 | * holds the folio lock until after the folio is removed | |
1166 | * from the page so holding it ourselves is sufficient. | |
1167 | * | |
27e6a24a PB |
1168 | * To avoid locking the folio just to check inaccessible, |
1169 | * assume every inaccessible folio is also unevictable, | |
0003e2a4 SC |
1170 | * which is a cheaper test. If our assumption goes |
1171 | * wrong, it's not a correctness bug, just potentially | |
1172 | * wasted cycles. | |
89f6c88a | 1173 | */ |
56ae0bb3 | 1174 | if (!folio_trylock(folio)) |
89f6c88a HD |
1175 | goto isolate_fail_put; |
1176 | ||
56ae0bb3 | 1177 | mapping = folio_mapping(folio); |
0003e2a4 SC |
1178 | if ((mode & ISOLATE_ASYNC_MIGRATE) && is_dirty) { |
1179 | migrate_dirty = !mapping || | |
1180 | mapping->a_ops->migrate_folio; | |
1181 | } | |
27e6a24a | 1182 | is_inaccessible = mapping && mapping_inaccessible(mapping); |
56ae0bb3 | 1183 | folio_unlock(folio); |
27e6a24a | 1184 | if (!migrate_dirty || is_inaccessible) |
89f6c88a HD |
1185 | goto isolate_fail_put; |
1186 | } | |
1187 | ||
56ae0bb3 KW |
1188 | /* Try isolate the folio */ |
1189 | if (!folio_test_clear_lru(folio)) | |
9df41314 AS |
1190 | goto isolate_fail_put; |
1191 | ||
56ae0bb3 | 1192 | lruvec = folio_lruvec(folio); |
6168d0da | 1193 | |
69b7189f | 1194 | /* If we already hold the lock, we can skip some rechecking */ |
6168d0da AS |
1195 | if (lruvec != locked) { |
1196 | if (locked) | |
1197 | unlock_page_lruvec_irqrestore(locked, flags); | |
1198 | ||
1199 | compact_lock_irqsave(&lruvec->lru_lock, &flags, cc); | |
1200 | locked = lruvec; | |
6168d0da | 1201 | |
56ae0bb3 | 1202 | lruvec_memcg_debug(lruvec, folio); |
e380bebe | 1203 | |
590ccea8 MG |
1204 | /* |
1205 | * Try get exclusive access under lock. If marked for | |
1206 | * skip, the scan is aborted unless the current context | |
1207 | * is a rescan to reach the end of the pageblock. | |
1208 | */ | |
1209 | if (!skip_updated && valid_page) { | |
e380bebe | 1210 | skip_updated = true; |
590ccea8 MG |
1211 | if (test_and_set_skip(cc, valid_page) && |
1212 | !cc->finish_pageblock) { | |
7545e2f2 | 1213 | low_pfn = end_pfn; |
e380bebe | 1214 | goto isolate_abort; |
590ccea8 | 1215 | } |
e380bebe | 1216 | } |
2a1402aa | 1217 | |
29c0dde8 | 1218 | /* |
ee6f62fd | 1219 | * Check LRU folio order under the lock |
29c0dde8 | 1220 | */ |
ee6f62fd ZY |
1221 | if (unlikely(skip_isolation_on_order(folio_order(folio), |
1222 | cc->order) && | |
1223 | !cc->alloc_contig)) { | |
56ae0bb3 KW |
1224 | low_pfn += folio_nr_pages(folio) - 1; |
1225 | nr_scanned += folio_nr_pages(folio) - 1; | |
1226 | folio_set_lru(folio); | |
9df41314 | 1227 | goto isolate_fail_put; |
69b7189f | 1228 | } |
d99fd5fe | 1229 | } |
fa9add64 | 1230 | |
56ae0bb3 KW |
1231 | /* The folio is taken off the LRU */ |
1232 | if (folio_test_large(folio)) | |
1233 | low_pfn += folio_nr_pages(folio) - 1; | |
bc835011 | 1234 | |
748446bb | 1235 | /* Successfully isolated */ |
56ae0bb3 KW |
1236 | lruvec_del_folio(lruvec, folio); |
1237 | node_stat_mod_folio(folio, | |
1238 | NR_ISOLATED_ANON + folio_is_file_lru(folio), | |
1239 | folio_nr_pages(folio)); | |
b6c75016 JK |
1240 | |
1241 | isolate_success: | |
56ae0bb3 | 1242 | list_add(&folio->lru, &cc->migratepages); |
ae37c7ff | 1243 | isolate_success_no_list: |
56ae0bb3 KW |
1244 | cc->nr_migratepages += folio_nr_pages(folio); |
1245 | nr_isolated += folio_nr_pages(folio); | |
1246 | nr_scanned += folio_nr_pages(folio) - 1; | |
748446bb | 1247 | |
804d3121 MG |
1248 | /* |
1249 | * Avoid isolating too much unless this block is being | |
48731c84 | 1250 | * fully scanned (e.g. dirty/writeback pages, parallel allocation) |
cb2dcaf0 MG |
1251 | * or a lock is contended. For contention, isolate quickly to |
1252 | * potentially remove one source of contention. | |
804d3121 | 1253 | */ |
38935861 | 1254 | if (cc->nr_migratepages >= COMPACT_CLUSTER_MAX && |
48731c84 | 1255 | !cc->finish_pageblock && !cc->contended) { |
31b8384a | 1256 | ++low_pfn; |
748446bb | 1257 | break; |
31b8384a | 1258 | } |
fdd048e1 VB |
1259 | |
1260 | continue; | |
9df41314 AS |
1261 | |
1262 | isolate_fail_put: | |
1263 | /* Avoid potential deadlock in freeing page under lru_lock */ | |
1264 | if (locked) { | |
6168d0da AS |
1265 | unlock_page_lruvec_irqrestore(locked, flags); |
1266 | locked = NULL; | |
9df41314 | 1267 | } |
56ae0bb3 | 1268 | folio_put(folio); |
9df41314 | 1269 | |
fdd048e1 | 1270 | isolate_fail: |
369fa227 | 1271 | if (!skip_on_failure && ret != -ENOMEM) |
fdd048e1 VB |
1272 | continue; |
1273 | ||
1274 | /* | |
1275 | * We have isolated some pages, but then failed. Release them | |
1276 | * instead of migrating, as we cannot form the cc->order buddy | |
1277 | * page anyway. | |
1278 | */ | |
1279 | if (nr_isolated) { | |
1280 | if (locked) { | |
6168d0da AS |
1281 | unlock_page_lruvec_irqrestore(locked, flags); |
1282 | locked = NULL; | |
fdd048e1 | 1283 | } |
fdd048e1 VB |
1284 | putback_movable_pages(&cc->migratepages); |
1285 | cc->nr_migratepages = 0; | |
fdd048e1 VB |
1286 | nr_isolated = 0; |
1287 | } | |
1288 | ||
1289 | if (low_pfn < next_skip_pfn) { | |
1290 | low_pfn = next_skip_pfn - 1; | |
1291 | /* | |
1292 | * The check near the loop beginning would have updated | |
1293 | * next_skip_pfn too, but this is a bit simpler. | |
1294 | */ | |
1295 | next_skip_pfn += 1UL << cc->order; | |
1296 | } | |
369fa227 OS |
1297 | |
1298 | if (ret == -ENOMEM) | |
1299 | break; | |
748446bb MG |
1300 | } |
1301 | ||
99c0fd5e VB |
1302 | /* |
1303 | * The PageBuddy() check could have potentially brought us outside | |
1304 | * the range to be scanned. | |
1305 | */ | |
1306 | if (unlikely(low_pfn > end_pfn)) | |
1307 | low_pfn = end_pfn; | |
1308 | ||
56ae0bb3 | 1309 | folio = NULL; |
9df41314 | 1310 | |
e380bebe | 1311 | isolate_abort: |
c67fe375 | 1312 | if (locked) |
6168d0da | 1313 | unlock_page_lruvec_irqrestore(locked, flags); |
56ae0bb3 KW |
1314 | if (folio) { |
1315 | folio_set_lru(folio); | |
1316 | folio_put(folio); | |
9df41314 | 1317 | } |
748446bb | 1318 | |
50b5b094 | 1319 | /* |
48731c84 | 1320 | * Update the cached scanner pfn once the pageblock has been scanned. |
804d3121 MG |
1321 | * Pages will either be migrated in which case there is no point |
1322 | * scanning in the near future or migration failed in which case the | |
1323 | * failure reason may persist. The block is marked for skipping if | |
1324 | * there were no pages isolated in the block or if the block is | |
1325 | * rescanned twice in a row. | |
50b5b094 | 1326 | */ |
48731c84 | 1327 | if (low_pfn == end_pfn && (!nr_isolated || cc->finish_pageblock)) { |
8b71b499 | 1328 | if (!cc->no_set_skip_hint && valid_page && !skip_updated) |
e380bebe MG |
1329 | set_pageblock_skip(valid_page); |
1330 | update_cached_migrate(cc, low_pfn); | |
1331 | } | |
bb13ffeb | 1332 | |
e34d85f0 JK |
1333 | trace_mm_compaction_isolate_migratepages(start_pfn, low_pfn, |
1334 | nr_scanned, nr_isolated); | |
b7aba698 | 1335 | |
670105a2 | 1336 | fatal_pending: |
7f354a54 | 1337 | cc->total_migrate_scanned += nr_scanned; |
397487db | 1338 | if (nr_isolated) |
010fc29a | 1339 | count_compact_events(COMPACTISOLATED, nr_isolated); |
397487db | 1340 | |
c2ad7a1f OS |
1341 | cc->migrate_pfn = low_pfn; |
1342 | ||
1343 | return ret; | |
2fe86e00 MN |
1344 | } |
1345 | ||
edc2ca61 VB |
1346 | /** |
1347 | * isolate_migratepages_range() - isolate migrate-able pages in a PFN range | |
1348 | * @cc: Compaction control structure. | |
1349 | * @start_pfn: The first PFN to start isolating. | |
1350 | * @end_pfn: The one-past-last PFN. | |
1351 | * | |
369fa227 OS |
1352 | * Returns -EAGAIN when contented, -EINTR in case of a signal pending, -ENOMEM |
1353 | * in case we could not allocate a page, or 0. | |
edc2ca61 | 1354 | */ |
c2ad7a1f | 1355 | int |
edc2ca61 VB |
1356 | isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn, |
1357 | unsigned long end_pfn) | |
1358 | { | |
e1409c32 | 1359 | unsigned long pfn, block_start_pfn, block_end_pfn; |
c2ad7a1f | 1360 | int ret = 0; |
edc2ca61 VB |
1361 | |
1362 | /* Scan block by block. First and last block may be incomplete */ | |
1363 | pfn = start_pfn; | |
06b6640a | 1364 | block_start_pfn = pageblock_start_pfn(pfn); |
e1409c32 JK |
1365 | if (block_start_pfn < cc->zone->zone_start_pfn) |
1366 | block_start_pfn = cc->zone->zone_start_pfn; | |
06b6640a | 1367 | block_end_pfn = pageblock_end_pfn(pfn); |
edc2ca61 VB |
1368 | |
1369 | for (; pfn < end_pfn; pfn = block_end_pfn, | |
e1409c32 | 1370 | block_start_pfn = block_end_pfn, |
edc2ca61 VB |
1371 | block_end_pfn += pageblock_nr_pages) { |
1372 | ||
1373 | block_end_pfn = min(block_end_pfn, end_pfn); | |
1374 | ||
e1409c32 JK |
1375 | if (!pageblock_pfn_to_page(block_start_pfn, |
1376 | block_end_pfn, cc->zone)) | |
edc2ca61 VB |
1377 | continue; |
1378 | ||
c2ad7a1f OS |
1379 | ret = isolate_migratepages_block(cc, pfn, block_end_pfn, |
1380 | ISOLATE_UNEVICTABLE); | |
edc2ca61 | 1381 | |
c2ad7a1f | 1382 | if (ret) |
edc2ca61 | 1383 | break; |
6ea41c0c | 1384 | |
38935861 | 1385 | if (cc->nr_migratepages >= COMPACT_CLUSTER_MAX) |
6ea41c0c | 1386 | break; |
edc2ca61 | 1387 | } |
edc2ca61 | 1388 | |
c2ad7a1f | 1389 | return ret; |
edc2ca61 VB |
1390 | } |
1391 | ||
ff9543fd MN |
1392 | #endif /* CONFIG_COMPACTION || CONFIG_CMA */ |
1393 | #ifdef CONFIG_COMPACTION | |
018e9a49 | 1394 | |
b682debd VB |
1395 | static bool suitable_migration_source(struct compact_control *cc, |
1396 | struct page *page) | |
1397 | { | |
282722b0 VB |
1398 | int block_mt; |
1399 | ||
9bebefd5 MG |
1400 | if (pageblock_skip_persistent(page)) |
1401 | return false; | |
1402 | ||
282722b0 | 1403 | if ((cc->mode != MIGRATE_ASYNC) || !cc->direct_compaction) |
b682debd VB |
1404 | return true; |
1405 | ||
282722b0 VB |
1406 | block_mt = get_pageblock_migratetype(page); |
1407 | ||
1408 | if (cc->migratetype == MIGRATE_MOVABLE) | |
1409 | return is_migrate_movable(block_mt); | |
1410 | else | |
1411 | return block_mt == cc->migratetype; | |
b682debd VB |
1412 | } |
1413 | ||
018e9a49 | 1414 | /* Returns true if the page is within a block suitable for migration to */ |
9f7e3387 VB |
1415 | static bool suitable_migration_target(struct compact_control *cc, |
1416 | struct page *page) | |
018e9a49 AM |
1417 | { |
1418 | /* If the page is a large free page, then disallow migration */ | |
1419 | if (PageBuddy(page)) { | |
1883e8ac BW |
1420 | int order = cc->order > 0 ? cc->order : pageblock_order; |
1421 | ||
018e9a49 AM |
1422 | /* |
1423 | * We are checking page_order without zone->lock taken. But | |
1424 | * the only small danger is that we skip a potentially suitable | |
1425 | * pageblock, so it's not worth to check order for valid range. | |
1426 | */ | |
1883e8ac | 1427 | if (buddy_order_unsafe(page) >= order) |
018e9a49 AM |
1428 | return false; |
1429 | } | |
1430 | ||
1ef36db2 YX |
1431 | if (cc->ignore_block_suitable) |
1432 | return true; | |
1433 | ||
018e9a49 | 1434 | /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */ |
b682debd | 1435 | if (is_migrate_movable(get_pageblock_migratetype(page))) |
018e9a49 AM |
1436 | return true; |
1437 | ||
1438 | /* Otherwise skip the block */ | |
1439 | return false; | |
1440 | } | |
1441 | ||
70b44595 MG |
1442 | static inline unsigned int |
1443 | freelist_scan_limit(struct compact_control *cc) | |
1444 | { | |
dd7ef7bd QC |
1445 | unsigned short shift = BITS_PER_LONG - 1; |
1446 | ||
1447 | return (COMPACT_CLUSTER_MAX >> min(shift, cc->fast_search_fail)) + 1; | |
70b44595 MG |
1448 | } |
1449 | ||
f2849aa0 VB |
1450 | /* |
1451 | * Test whether the free scanner has reached the same or lower pageblock than | |
1452 | * the migration scanner, and compaction should thus terminate. | |
1453 | */ | |
1454 | static inline bool compact_scanners_met(struct compact_control *cc) | |
1455 | { | |
1456 | return (cc->free_pfn >> pageblock_order) | |
1457 | <= (cc->migrate_pfn >> pageblock_order); | |
1458 | } | |
1459 | ||
5a811889 MG |
1460 | /* |
1461 | * Used when scanning for a suitable migration target which scans freelists | |
1462 | * in reverse. Reorders the list such as the unscanned pages are scanned | |
1463 | * first on the next iteration of the free scanner | |
1464 | */ | |
1465 | static void | |
1466 | move_freelist_head(struct list_head *freelist, struct page *freepage) | |
1467 | { | |
1468 | LIST_HEAD(sublist); | |
1469 | ||
4c179891 | 1470 | if (!list_is_first(&freepage->buddy_list, freelist)) { |
bbefa0fc | 1471 | list_cut_before(&sublist, freelist, &freepage->buddy_list); |
d2155fe5 | 1472 | list_splice_tail(&sublist, freelist); |
5a811889 MG |
1473 | } |
1474 | } | |
1475 | ||
1476 | /* | |
1477 | * Similar to move_freelist_head except used by the migration scanner | |
1478 | * when scanning forward. It's possible for these list operations to | |
1479 | * move against each other if they search the free list exactly in | |
1480 | * lockstep. | |
1481 | */ | |
70b44595 MG |
1482 | static void |
1483 | move_freelist_tail(struct list_head *freelist, struct page *freepage) | |
1484 | { | |
1485 | LIST_HEAD(sublist); | |
1486 | ||
4c179891 | 1487 | if (!list_is_last(&freepage->buddy_list, freelist)) { |
bbefa0fc | 1488 | list_cut_position(&sublist, freelist, &freepage->buddy_list); |
d2155fe5 | 1489 | list_splice_tail(&sublist, freelist); |
70b44595 MG |
1490 | } |
1491 | } | |
1492 | ||
5a811889 | 1493 | static void |
be21b32a | 1494 | fast_isolate_around(struct compact_control *cc, unsigned long pfn) |
5a811889 MG |
1495 | { |
1496 | unsigned long start_pfn, end_pfn; | |
6e2b7044 | 1497 | struct page *page; |
5a811889 MG |
1498 | |
1499 | /* Do not search around if there are enough pages already */ | |
1500 | if (cc->nr_freepages >= cc->nr_migratepages) | |
1501 | return; | |
1502 | ||
1503 | /* Minimise scanning during async compaction */ | |
1504 | if (cc->direct_compaction && cc->mode == MIGRATE_ASYNC) | |
1505 | return; | |
1506 | ||
1507 | /* Pageblock boundaries */ | |
6e2b7044 VB |
1508 | start_pfn = max(pageblock_start_pfn(pfn), cc->zone->zone_start_pfn); |
1509 | end_pfn = min(pageblock_end_pfn(pfn), zone_end_pfn(cc->zone)); | |
1510 | ||
1511 | page = pageblock_pfn_to_page(start_pfn, end_pfn, cc->zone); | |
1512 | if (!page) | |
1513 | return; | |
5a811889 | 1514 | |
733aea0b | 1515 | isolate_freepages_block(cc, &start_pfn, end_pfn, cc->freepages, 1, false); |
5a811889 MG |
1516 | |
1517 | /* Skip this pageblock in the future as it's full or nearly full */ | |
18c59d58 | 1518 | if (start_pfn == end_pfn && !cc->no_set_skip_hint) |
5a811889 MG |
1519 | set_pageblock_skip(page); |
1520 | } | |
1521 | ||
dbe2d4e4 MG |
1522 | /* Search orders in round-robin fashion */ |
1523 | static int next_search_order(struct compact_control *cc, int order) | |
1524 | { | |
1525 | order--; | |
1526 | if (order < 0) | |
1527 | order = cc->order - 1; | |
1528 | ||
1529 | /* Search wrapped around? */ | |
1530 | if (order == cc->search_order) { | |
1531 | cc->search_order--; | |
1532 | if (cc->search_order < 0) | |
1533 | cc->search_order = cc->order - 1; | |
1534 | return -1; | |
1535 | } | |
1536 | ||
1537 | return order; | |
1538 | } | |
1539 | ||
2dbd9005 | 1540 | static void fast_isolate_freepages(struct compact_control *cc) |
5a811889 | 1541 | { |
b55ca526 | 1542 | unsigned int limit = max(1U, freelist_scan_limit(cc) >> 1); |
447ba886 | 1543 | unsigned int nr_scanned = 0, total_isolated = 0; |
74e21484 | 1544 | unsigned long low_pfn, min_pfn, highest = 0; |
5a811889 MG |
1545 | unsigned long nr_isolated = 0; |
1546 | unsigned long distance; | |
1547 | struct page *page = NULL; | |
1548 | bool scan_start = false; | |
1549 | int order; | |
1550 | ||
1551 | /* Full compaction passes in a negative order */ | |
1552 | if (cc->order <= 0) | |
2dbd9005 | 1553 | return; |
5a811889 MG |
1554 | |
1555 | /* | |
1556 | * If starting the scan, use a deeper search and use the highest | |
1557 | * PFN found if a suitable one is not found. | |
1558 | */ | |
e332f741 | 1559 | if (cc->free_pfn >= cc->zone->compact_init_free_pfn) { |
5a811889 MG |
1560 | limit = pageblock_nr_pages >> 1; |
1561 | scan_start = true; | |
1562 | } | |
1563 | ||
1564 | /* | |
1565 | * Preferred point is in the top quarter of the scan space but take | |
1566 | * a pfn from the top half if the search is problematic. | |
1567 | */ | |
1568 | distance = (cc->free_pfn - cc->migrate_pfn); | |
1569 | low_pfn = pageblock_start_pfn(cc->free_pfn - (distance >> 2)); | |
1570 | min_pfn = pageblock_start_pfn(cc->free_pfn - (distance >> 1)); | |
1571 | ||
1572 | if (WARN_ON_ONCE(min_pfn > low_pfn)) | |
1573 | low_pfn = min_pfn; | |
1574 | ||
dbe2d4e4 MG |
1575 | /* |
1576 | * Search starts from the last successful isolation order or the next | |
1577 | * order to search after a previous failure | |
1578 | */ | |
1579 | cc->search_order = min_t(unsigned int, cc->order - 1, cc->search_order); | |
1580 | ||
1581 | for (order = cc->search_order; | |
1582 | !page && order >= 0; | |
1583 | order = next_search_order(cc, order)) { | |
5a811889 MG |
1584 | struct free_area *area = &cc->zone->free_area[order]; |
1585 | struct list_head *freelist; | |
1586 | struct page *freepage; | |
1587 | unsigned long flags; | |
1588 | unsigned int order_scanned = 0; | |
74e21484 | 1589 | unsigned long high_pfn = 0; |
5a811889 MG |
1590 | |
1591 | if (!area->nr_free) | |
1592 | continue; | |
1593 | ||
1594 | spin_lock_irqsave(&cc->zone->lock, flags); | |
1595 | freelist = &area->free_list[MIGRATE_MOVABLE]; | |
94ec2003 | 1596 | list_for_each_entry_reverse(freepage, freelist, buddy_list) { |
5a811889 MG |
1597 | unsigned long pfn; |
1598 | ||
1599 | order_scanned++; | |
1600 | nr_scanned++; | |
1601 | pfn = page_to_pfn(freepage); | |
1602 | ||
1603 | if (pfn >= highest) | |
6e2b7044 VB |
1604 | highest = max(pageblock_start_pfn(pfn), |
1605 | cc->zone->zone_start_pfn); | |
5a811889 MG |
1606 | |
1607 | if (pfn >= low_pfn) { | |
1608 | cc->fast_search_fail = 0; | |
dbe2d4e4 | 1609 | cc->search_order = order; |
5a811889 MG |
1610 | page = freepage; |
1611 | break; | |
1612 | } | |
1613 | ||
1614 | if (pfn >= min_pfn && pfn > high_pfn) { | |
1615 | high_pfn = pfn; | |
1616 | ||
1617 | /* Shorten the scan if a candidate is found */ | |
1618 | limit >>= 1; | |
1619 | } | |
1620 | ||
1621 | if (order_scanned >= limit) | |
1622 | break; | |
1623 | } | |
1624 | ||
e6bd14ec | 1625 | /* Use a maximum candidate pfn if a preferred one was not found */ |
5a811889 MG |
1626 | if (!page && high_pfn) { |
1627 | page = pfn_to_page(high_pfn); | |
1628 | ||
1629 | /* Update freepage for the list reorder below */ | |
1630 | freepage = page; | |
1631 | } | |
1632 | ||
1633 | /* Reorder to so a future search skips recent pages */ | |
1634 | move_freelist_head(freelist, freepage); | |
1635 | ||
1636 | /* Isolate the page if available */ | |
1637 | if (page) { | |
1638 | if (__isolate_free_page(page, order)) { | |
1639 | set_page_private(page, order); | |
1640 | nr_isolated = 1 << order; | |
b717d6b9 | 1641 | nr_scanned += nr_isolated - 1; |
447ba886 | 1642 | total_isolated += nr_isolated; |
5a811889 | 1643 | cc->nr_freepages += nr_isolated; |
733aea0b | 1644 | list_add_tail(&page->lru, &cc->freepages[order]); |
5a811889 MG |
1645 | count_compact_events(COMPACTISOLATED, nr_isolated); |
1646 | } else { | |
1647 | /* If isolation fails, abort the search */ | |
5b56d996 | 1648 | order = cc->search_order + 1; |
5a811889 MG |
1649 | page = NULL; |
1650 | } | |
1651 | } | |
1652 | ||
1653 | spin_unlock_irqrestore(&cc->zone->lock, flags); | |
1654 | ||
a8d13355 BW |
1655 | /* Skip fast search if enough freepages isolated */ |
1656 | if (cc->nr_freepages >= cc->nr_migratepages) | |
1657 | break; | |
1658 | ||
5a811889 | 1659 | /* |
b55ca526 | 1660 | * Smaller scan on next order so the total scan is related |
5a811889 MG |
1661 | * to freelist_scan_limit. |
1662 | */ | |
1663 | if (order_scanned >= limit) | |
b55ca526 | 1664 | limit = max(1U, limit >> 1); |
5a811889 MG |
1665 | } |
1666 | ||
447ba886 BW |
1667 | trace_mm_compaction_fast_isolate_freepages(min_pfn, cc->free_pfn, |
1668 | nr_scanned, total_isolated); | |
1669 | ||
5a811889 MG |
1670 | if (!page) { |
1671 | cc->fast_search_fail++; | |
1672 | if (scan_start) { | |
1673 | /* | |
1674 | * Use the highest PFN found above min. If one was | |
f3867755 | 1675 | * not found, be pessimistic for direct compaction |
5a811889 MG |
1676 | * and use the min mark. |
1677 | */ | |
ca2864e5 | 1678 | if (highest >= min_pfn) { |
5a811889 MG |
1679 | page = pfn_to_page(highest); |
1680 | cc->free_pfn = highest; | |
1681 | } else { | |
e577c8b6 | 1682 | if (cc->direct_compaction && pfn_valid(min_pfn)) { |
73a6e474 | 1683 | page = pageblock_pfn_to_page(min_pfn, |
6e2b7044 VB |
1684 | min(pageblock_end_pfn(min_pfn), |
1685 | zone_end_pfn(cc->zone)), | |
73a6e474 | 1686 | cc->zone); |
d19b1a17 BS |
1687 | if (page && !suitable_migration_target(cc, page)) |
1688 | page = NULL; | |
1689 | ||
5a811889 MG |
1690 | cc->free_pfn = min_pfn; |
1691 | } | |
1692 | } | |
1693 | } | |
1694 | } | |
1695 | ||
d097a6f6 MG |
1696 | if (highest && highest >= cc->zone->compact_cached_free_pfn) { |
1697 | highest -= pageblock_nr_pages; | |
5a811889 | 1698 | cc->zone->compact_cached_free_pfn = highest; |
d097a6f6 | 1699 | } |
5a811889 MG |
1700 | |
1701 | cc->total_free_scanned += nr_scanned; | |
1702 | if (!page) | |
2dbd9005 | 1703 | return; |
5a811889 MG |
1704 | |
1705 | low_pfn = page_to_pfn(page); | |
be21b32a | 1706 | fast_isolate_around(cc, low_pfn); |
5a811889 MG |
1707 | } |
1708 | ||
2fe86e00 | 1709 | /* |
ff9543fd MN |
1710 | * Based on information in the current compact_control, find blocks |
1711 | * suitable for isolating free pages from and then isolate them. | |
2fe86e00 | 1712 | */ |
edc2ca61 | 1713 | static void isolate_freepages(struct compact_control *cc) |
2fe86e00 | 1714 | { |
edc2ca61 | 1715 | struct zone *zone = cc->zone; |
ff9543fd | 1716 | struct page *page; |
c96b9e50 | 1717 | unsigned long block_start_pfn; /* start of current pageblock */ |
e14c720e | 1718 | unsigned long isolate_start_pfn; /* exact pfn we start at */ |
c96b9e50 VB |
1719 | unsigned long block_end_pfn; /* end of current pageblock */ |
1720 | unsigned long low_pfn; /* lowest pfn scanner is able to scan */ | |
4fca9730 | 1721 | unsigned int stride; |
2fe86e00 | 1722 | |
5a811889 | 1723 | /* Try a small search of the free lists for a candidate */ |
00bc102f | 1724 | fast_isolate_freepages(cc); |
5a811889 | 1725 | if (cc->nr_freepages) |
733aea0b | 1726 | return; |
5a811889 | 1727 | |
ff9543fd MN |
1728 | /* |
1729 | * Initialise the free scanner. The starting point is where we last | |
49e068f0 | 1730 | * successfully isolated from, zone-cached value, or the end of the |
e14c720e VB |
1731 | * zone when isolating for the first time. For looping we also need |
1732 | * this pfn aligned down to the pageblock boundary, because we do | |
c96b9e50 VB |
1733 | * block_start_pfn -= pageblock_nr_pages in the for loop. |
1734 | * For ending point, take care when isolating in last pageblock of a | |
a1c1dbeb | 1735 | * zone which ends in the middle of a pageblock. |
49e068f0 VB |
1736 | * The low boundary is the end of the pageblock the migration scanner |
1737 | * is using. | |
ff9543fd | 1738 | */ |
e14c720e | 1739 | isolate_start_pfn = cc->free_pfn; |
5a811889 | 1740 | block_start_pfn = pageblock_start_pfn(isolate_start_pfn); |
c96b9e50 VB |
1741 | block_end_pfn = min(block_start_pfn + pageblock_nr_pages, |
1742 | zone_end_pfn(zone)); | |
06b6640a | 1743 | low_pfn = pageblock_end_pfn(cc->migrate_pfn); |
4fca9730 | 1744 | stride = cc->mode == MIGRATE_ASYNC ? COMPACT_CLUSTER_MAX : 1; |
2fe86e00 | 1745 | |
ff9543fd MN |
1746 | /* |
1747 | * Isolate free pages until enough are available to migrate the | |
1748 | * pages on cc->migratepages. We stop searching if the migrate | |
1749 | * and free page scanners meet or enough free pages are isolated. | |
1750 | */ | |
f5f61a32 | 1751 | for (; block_start_pfn >= low_pfn; |
c96b9e50 | 1752 | block_end_pfn = block_start_pfn, |
e14c720e VB |
1753 | block_start_pfn -= pageblock_nr_pages, |
1754 | isolate_start_pfn = block_start_pfn) { | |
4fca9730 MG |
1755 | unsigned long nr_isolated; |
1756 | ||
f6ea3adb DR |
1757 | /* |
1758 | * This can iterate a massively long zone without finding any | |
cb810ad2 | 1759 | * suitable migration targets, so periodically check resched. |
f6ea3adb | 1760 | */ |
c036ddff | 1761 | if (!(block_start_pfn % (COMPACT_CLUSTER_MAX * pageblock_nr_pages))) |
cf66f070 | 1762 | cond_resched(); |
f6ea3adb | 1763 | |
7d49d886 VB |
1764 | page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn, |
1765 | zone); | |
e6e0c767 BW |
1766 | if (!page) { |
1767 | unsigned long next_pfn; | |
1768 | ||
1769 | next_pfn = skip_offline_sections_reverse(block_start_pfn); | |
1770 | if (next_pfn) | |
1771 | block_start_pfn = max(next_pfn, low_pfn); | |
1772 | ||
ff9543fd | 1773 | continue; |
e6e0c767 | 1774 | } |
ff9543fd MN |
1775 | |
1776 | /* Check the block is suitable for migration */ | |
9f7e3387 | 1777 | if (!suitable_migration_target(cc, page)) |
ff9543fd | 1778 | continue; |
68e3e926 | 1779 | |
bb13ffeb MG |
1780 | /* If isolation recently failed, do not retry */ |
1781 | if (!isolation_suitable(cc, page)) | |
1782 | continue; | |
1783 | ||
e14c720e | 1784 | /* Found a block suitable for isolating free pages from. */ |
4fca9730 | 1785 | nr_isolated = isolate_freepages_block(cc, &isolate_start_pfn, |
733aea0b | 1786 | block_end_pfn, cc->freepages, stride, false); |
ff9543fd | 1787 | |
d097a6f6 MG |
1788 | /* Update the skip hint if the full pageblock was scanned */ |
1789 | if (isolate_start_pfn == block_end_pfn) | |
16951789 KS |
1790 | update_pageblock_skip(cc, page, block_start_pfn - |
1791 | pageblock_nr_pages); | |
d097a6f6 | 1792 | |
cb2dcaf0 MG |
1793 | /* Are enough freepages isolated? */ |
1794 | if (cc->nr_freepages >= cc->nr_migratepages) { | |
a46cbf3b DR |
1795 | if (isolate_start_pfn >= block_end_pfn) { |
1796 | /* | |
1797 | * Restart at previous pageblock if more | |
1798 | * freepages can be isolated next time. | |
1799 | */ | |
f5f61a32 VB |
1800 | isolate_start_pfn = |
1801 | block_start_pfn - pageblock_nr_pages; | |
a46cbf3b | 1802 | } |
be976572 | 1803 | break; |
a46cbf3b | 1804 | } else if (isolate_start_pfn < block_end_pfn) { |
f5f61a32 | 1805 | /* |
a46cbf3b DR |
1806 | * If isolation failed early, do not continue |
1807 | * needlessly. | |
f5f61a32 | 1808 | */ |
a46cbf3b | 1809 | break; |
f5f61a32 | 1810 | } |
4fca9730 MG |
1811 | |
1812 | /* Adjust stride depending on isolation */ | |
1813 | if (nr_isolated) { | |
1814 | stride = 1; | |
1815 | continue; | |
1816 | } | |
1817 | stride = min_t(unsigned int, COMPACT_CLUSTER_MAX, stride << 1); | |
ff9543fd MN |
1818 | } |
1819 | ||
7ed695e0 | 1820 | /* |
f5f61a32 VB |
1821 | * Record where the free scanner will restart next time. Either we |
1822 | * broke from the loop and set isolate_start_pfn based on the last | |
1823 | * call to isolate_freepages_block(), or we met the migration scanner | |
1824 | * and the loop terminated due to isolate_start_pfn < low_pfn | |
7ed695e0 | 1825 | */ |
f5f61a32 | 1826 | cc->free_pfn = isolate_start_pfn; |
748446bb MG |
1827 | } |
1828 | ||
1829 | /* | |
1830 | * This is a migrate-callback that "allocates" freepages by taking pages | |
1831 | * from the isolated freelists in the block we are migrating to. | |
1832 | */ | |
b951aaff | 1833 | static struct folio *compaction_alloc_noprof(struct folio *src, unsigned long data) |
748446bb MG |
1834 | { |
1835 | struct compact_control *cc = (struct compact_control *)data; | |
4e096ae1 | 1836 | struct folio *dst; |
733aea0b | 1837 | int order = folio_order(src); |
73318e2c ZY |
1838 | bool has_isolated_pages = false; |
1839 | int start_order; | |
1840 | struct page *freepage; | |
1841 | unsigned long size; | |
1842 | ||
1843 | again: | |
1844 | for (start_order = order; start_order < NR_PAGE_ORDERS; start_order++) | |
1845 | if (!list_empty(&cc->freepages[start_order])) | |
1846 | break; | |
748446bb | 1847 | |
73318e2c ZY |
1848 | /* no free pages in the list */ |
1849 | if (start_order == NR_PAGE_ORDERS) { | |
1850 | if (has_isolated_pages) | |
748446bb | 1851 | return NULL; |
73318e2c ZY |
1852 | isolate_freepages(cc); |
1853 | has_isolated_pages = true; | |
1854 | goto again; | |
1855 | } | |
1856 | ||
1857 | freepage = list_first_entry(&cc->freepages[start_order], struct page, | |
1858 | lru); | |
1859 | size = 1 << start_order; | |
1860 | ||
1861 | list_del(&freepage->lru); | |
1862 | ||
1863 | while (start_order > order) { | |
1864 | start_order--; | |
1865 | size >>= 1; | |
1866 | ||
1867 | list_add(&freepage[size].lru, &cc->freepages[start_order]); | |
1868 | set_page_private(&freepage[size], start_order); | |
748446bb | 1869 | } |
73318e2c | 1870 | dst = (struct folio *)freepage; |
748446bb | 1871 | |
733aea0b | 1872 | post_alloc_hook(&dst->page, order, __GFP_MOVABLE); |
8fd10a89 | 1873 | set_page_refcounted(&dst->page); |
733aea0b ZY |
1874 | if (order) |
1875 | prep_compound_page(&dst->page, order); | |
1876 | cc->nr_freepages -= 1 << order; | |
1877 | cc->nr_migratepages -= 1 << order; | |
1878 | return page_rmappable_folio(&dst->page); | |
748446bb MG |
1879 | } |
1880 | ||
b951aaff SB |
1881 | static struct folio *compaction_alloc(struct folio *src, unsigned long data) |
1882 | { | |
1883 | return alloc_hooks(compaction_alloc_noprof(src, data)); | |
1884 | } | |
1885 | ||
748446bb | 1886 | /* |
d53aea3d DR |
1887 | * This is a migrate-callback that "frees" freepages back to the isolated |
1888 | * freelist. All pages on the freelist are from the same zone, so there is no | |
1889 | * special handling needed for NUMA. | |
1890 | */ | |
4e096ae1 | 1891 | static void compaction_free(struct folio *dst, unsigned long data) |
d53aea3d DR |
1892 | { |
1893 | struct compact_control *cc = (struct compact_control *)data; | |
733aea0b ZY |
1894 | int order = folio_order(dst); |
1895 | struct page *page = &dst->page; | |
d53aea3d | 1896 | |
733aea0b ZY |
1897 | if (folio_put_testzero(dst)) { |
1898 | free_pages_prepare(page, order); | |
1899 | list_add(&dst->lru, &cc->freepages[order]); | |
1900 | cc->nr_freepages += 1 << order; | |
1901 | } | |
1902 | cc->nr_migratepages += 1 << order; | |
1903 | /* | |
1904 | * someone else has referenced the page, we cannot take it back to our | |
1905 | * free list. | |
1906 | */ | |
d53aea3d DR |
1907 | } |
1908 | ||
ff9543fd MN |
1909 | /* possible outcome of isolate_migratepages */ |
1910 | typedef enum { | |
1911 | ISOLATE_ABORT, /* Abort compaction now */ | |
1912 | ISOLATE_NONE, /* No pages isolated, continue scanning */ | |
1913 | ISOLATE_SUCCESS, /* Pages isolated, migrate */ | |
1914 | } isolate_migrate_t; | |
1915 | ||
5bbe3547 EM |
1916 | /* |
1917 | * Allow userspace to control policy on scanning the unevictable LRU for | |
1918 | * compactable pages. | |
1919 | */ | |
48fe8ab8 MC |
1920 | static int sysctl_compact_unevictable_allowed __read_mostly = CONFIG_COMPACT_UNEVICTABLE_DEFAULT; |
1921 | /* | |
1922 | * Tunable for proactive compaction. It determines how | |
1923 | * aggressively the kernel should compact memory in the | |
1924 | * background. It takes values in the range [0, 100]. | |
1925 | */ | |
1926 | static unsigned int __read_mostly sysctl_compaction_proactiveness = 20; | |
1927 | static int sysctl_extfrag_threshold = 500; | |
8b9167cd | 1928 | static int __read_mostly sysctl_compact_memory; |
5bbe3547 | 1929 | |
70b44595 MG |
1930 | static inline void |
1931 | update_fast_start_pfn(struct compact_control *cc, unsigned long pfn) | |
1932 | { | |
1933 | if (cc->fast_start_pfn == ULONG_MAX) | |
1934 | return; | |
1935 | ||
1936 | if (!cc->fast_start_pfn) | |
1937 | cc->fast_start_pfn = pfn; | |
1938 | ||
1939 | cc->fast_start_pfn = min(cc->fast_start_pfn, pfn); | |
1940 | } | |
1941 | ||
1942 | static inline unsigned long | |
1943 | reinit_migrate_pfn(struct compact_control *cc) | |
1944 | { | |
1945 | if (!cc->fast_start_pfn || cc->fast_start_pfn == ULONG_MAX) | |
1946 | return cc->migrate_pfn; | |
1947 | ||
1948 | cc->migrate_pfn = cc->fast_start_pfn; | |
1949 | cc->fast_start_pfn = ULONG_MAX; | |
1950 | ||
1951 | return cc->migrate_pfn; | |
1952 | } | |
1953 | ||
1954 | /* | |
1955 | * Briefly search the free lists for a migration source that already has | |
1956 | * some free pages to reduce the number of pages that need migration | |
1957 | * before a pageblock is free. | |
1958 | */ | |
1959 | static unsigned long fast_find_migrateblock(struct compact_control *cc) | |
1960 | { | |
1961 | unsigned int limit = freelist_scan_limit(cc); | |
1962 | unsigned int nr_scanned = 0; | |
1963 | unsigned long distance; | |
1964 | unsigned long pfn = cc->migrate_pfn; | |
1965 | unsigned long high_pfn; | |
1966 | int order; | |
15d28d0d | 1967 | bool found_block = false; |
70b44595 MG |
1968 | |
1969 | /* Skip hints are relied on to avoid repeats on the fast search */ | |
1970 | if (cc->ignore_skip_hint) | |
1971 | return pfn; | |
1972 | ||
f9d7fc1a MG |
1973 | /* |
1974 | * If the pageblock should be finished then do not select a different | |
1975 | * pageblock. | |
1976 | */ | |
1977 | if (cc->finish_pageblock) | |
1978 | return pfn; | |
1979 | ||
70b44595 MG |
1980 | /* |
1981 | * If the migrate_pfn is not at the start of a zone or the start | |
1982 | * of a pageblock then assume this is a continuation of a previous | |
1983 | * scan restarted due to COMPACT_CLUSTER_MAX. | |
1984 | */ | |
1985 | if (pfn != cc->zone->zone_start_pfn && pfn != pageblock_start_pfn(pfn)) | |
1986 | return pfn; | |
1987 | ||
1988 | /* | |
1989 | * For smaller orders, just linearly scan as the number of pages | |
1990 | * to migrate should be relatively small and does not necessarily | |
1991 | * justify freeing up a large block for a small allocation. | |
1992 | */ | |
1993 | if (cc->order <= PAGE_ALLOC_COSTLY_ORDER) | |
1994 | return pfn; | |
1995 | ||
1996 | /* | |
1997 | * Only allow kcompactd and direct requests for movable pages to | |
1998 | * quickly clear out a MOVABLE pageblock for allocation. This | |
1999 | * reduces the risk that a large movable pageblock is freed for | |
2000 | * an unmovable/reclaimable small allocation. | |
2001 | */ | |
2002 | if (cc->direct_compaction && cc->migratetype != MIGRATE_MOVABLE) | |
2003 | return pfn; | |
2004 | ||
2005 | /* | |
2006 | * When starting the migration scanner, pick any pageblock within the | |
2007 | * first half of the search space. Otherwise try and pick a pageblock | |
2008 | * within the first eighth to reduce the chances that a migration | |
2009 | * target later becomes a source. | |
2010 | */ | |
2011 | distance = (cc->free_pfn - cc->migrate_pfn) >> 1; | |
2012 | if (cc->migrate_pfn != cc->zone->zone_start_pfn) | |
2013 | distance >>= 2; | |
2014 | high_pfn = pageblock_start_pfn(cc->migrate_pfn + distance); | |
2015 | ||
2016 | for (order = cc->order - 1; | |
15d28d0d | 2017 | order >= PAGE_ALLOC_COSTLY_ORDER && !found_block && nr_scanned < limit; |
70b44595 MG |
2018 | order--) { |
2019 | struct free_area *area = &cc->zone->free_area[order]; | |
2020 | struct list_head *freelist; | |
2021 | unsigned long flags; | |
2022 | struct page *freepage; | |
2023 | ||
2024 | if (!area->nr_free) | |
2025 | continue; | |
2026 | ||
2027 | spin_lock_irqsave(&cc->zone->lock, flags); | |
2028 | freelist = &area->free_list[MIGRATE_MOVABLE]; | |
94ec2003 | 2029 | list_for_each_entry(freepage, freelist, buddy_list) { |
70b44595 MG |
2030 | unsigned long free_pfn; |
2031 | ||
15d28d0d WY |
2032 | if (nr_scanned++ >= limit) { |
2033 | move_freelist_tail(freelist, freepage); | |
2034 | break; | |
2035 | } | |
2036 | ||
70b44595 MG |
2037 | free_pfn = page_to_pfn(freepage); |
2038 | if (free_pfn < high_pfn) { | |
70b44595 MG |
2039 | /* |
2040 | * Avoid if skipped recently. Ideally it would | |
2041 | * move to the tail but even safe iteration of | |
2042 | * the list assumes an entry is deleted, not | |
2043 | * reordered. | |
2044 | */ | |
15d28d0d | 2045 | if (get_pageblock_skip(freepage)) |
70b44595 | 2046 | continue; |
70b44595 MG |
2047 | |
2048 | /* Reorder to so a future search skips recent pages */ | |
2049 | move_freelist_tail(freelist, freepage); | |
2050 | ||
e380bebe | 2051 | update_fast_start_pfn(cc, free_pfn); |
70b44595 | 2052 | pfn = pageblock_start_pfn(free_pfn); |
bbe832b9 RY |
2053 | if (pfn < cc->zone->zone_start_pfn) |
2054 | pfn = cc->zone->zone_start_pfn; | |
70b44595 | 2055 | cc->fast_search_fail = 0; |
15d28d0d | 2056 | found_block = true; |
70b44595 MG |
2057 | break; |
2058 | } | |
70b44595 MG |
2059 | } |
2060 | spin_unlock_irqrestore(&cc->zone->lock, flags); | |
2061 | } | |
2062 | ||
2063 | cc->total_migrate_scanned += nr_scanned; | |
2064 | ||
2065 | /* | |
2066 | * If fast scanning failed then use a cached entry for a page block | |
2067 | * that had free pages as the basis for starting a linear scan. | |
2068 | */ | |
15d28d0d WY |
2069 | if (!found_block) { |
2070 | cc->fast_search_fail++; | |
70b44595 | 2071 | pfn = reinit_migrate_pfn(cc); |
15d28d0d | 2072 | } |
70b44595 MG |
2073 | return pfn; |
2074 | } | |
2075 | ||
ff9543fd | 2076 | /* |
edc2ca61 VB |
2077 | * Isolate all pages that can be migrated from the first suitable block, |
2078 | * starting at the block pointed to by the migrate scanner pfn within | |
2079 | * compact_control. | |
ff9543fd | 2080 | */ |
32aaf055 | 2081 | static isolate_migrate_t isolate_migratepages(struct compact_control *cc) |
ff9543fd | 2082 | { |
e1409c32 JK |
2083 | unsigned long block_start_pfn; |
2084 | unsigned long block_end_pfn; | |
2085 | unsigned long low_pfn; | |
edc2ca61 VB |
2086 | struct page *page; |
2087 | const isolate_mode_t isolate_mode = | |
5bbe3547 | 2088 | (sysctl_compact_unevictable_allowed ? ISOLATE_UNEVICTABLE : 0) | |
1d2047fe | 2089 | (cc->mode != MIGRATE_SYNC ? ISOLATE_ASYNC_MIGRATE : 0); |
70b44595 | 2090 | bool fast_find_block; |
ff9543fd | 2091 | |
edc2ca61 VB |
2092 | /* |
2093 | * Start at where we last stopped, or beginning of the zone as | |
70b44595 MG |
2094 | * initialized by compact_zone(). The first failure will use |
2095 | * the lowest PFN as the starting point for linear scanning. | |
edc2ca61 | 2096 | */ |
70b44595 | 2097 | low_pfn = fast_find_migrateblock(cc); |
06b6640a | 2098 | block_start_pfn = pageblock_start_pfn(low_pfn); |
32aaf055 PL |
2099 | if (block_start_pfn < cc->zone->zone_start_pfn) |
2100 | block_start_pfn = cc->zone->zone_start_pfn; | |
ff9543fd | 2101 | |
70b44595 | 2102 | /* |
0aa8ea3c KS |
2103 | * fast_find_migrateblock() has already ensured the pageblock is not |
2104 | * set with a skipped flag, so to avoid the isolation_suitable check | |
2105 | * below again, check whether the fast search was successful. | |
70b44595 MG |
2106 | */ |
2107 | fast_find_block = low_pfn != cc->migrate_pfn && !cc->fast_search_fail; | |
2108 | ||
ff9543fd | 2109 | /* Only scan within a pageblock boundary */ |
06b6640a | 2110 | block_end_pfn = pageblock_end_pfn(low_pfn); |
ff9543fd | 2111 | |
edc2ca61 VB |
2112 | /* |
2113 | * Iterate over whole pageblocks until we find the first suitable. | |
2114 | * Do not cross the free scanner. | |
2115 | */ | |
e1409c32 | 2116 | for (; block_end_pfn <= cc->free_pfn; |
70b44595 | 2117 | fast_find_block = false, |
c2ad7a1f | 2118 | cc->migrate_pfn = low_pfn = block_end_pfn, |
e1409c32 JK |
2119 | block_start_pfn = block_end_pfn, |
2120 | block_end_pfn += pageblock_nr_pages) { | |
ff9543fd | 2121 | |
edc2ca61 VB |
2122 | /* |
2123 | * This can potentially iterate a massively long zone with | |
2124 | * many pageblocks unsuitable, so periodically check if we | |
cb810ad2 | 2125 | * need to schedule. |
edc2ca61 | 2126 | */ |
c036ddff | 2127 | if (!(low_pfn % (COMPACT_CLUSTER_MAX * pageblock_nr_pages))) |
cf66f070 | 2128 | cond_resched(); |
ff9543fd | 2129 | |
32aaf055 PL |
2130 | page = pageblock_pfn_to_page(block_start_pfn, |
2131 | block_end_pfn, cc->zone); | |
9721fd82 BW |
2132 | if (!page) { |
2133 | unsigned long next_pfn; | |
2134 | ||
2135 | next_pfn = skip_offline_sections(block_start_pfn); | |
2136 | if (next_pfn) | |
2137 | block_end_pfn = min(next_pfn, cc->free_pfn); | |
edc2ca61 | 2138 | continue; |
9721fd82 | 2139 | } |
edc2ca61 | 2140 | |
e380bebe MG |
2141 | /* |
2142 | * If isolation recently failed, do not retry. Only check the | |
2143 | * pageblock once. COMPACT_CLUSTER_MAX causes a pageblock | |
2144 | * to be visited multiple times. Assume skip was checked | |
2145 | * before making it "skip" so other compaction instances do | |
2146 | * not scan the same block. | |
2147 | */ | |
493614da JW |
2148 | if ((pageblock_aligned(low_pfn) || |
2149 | low_pfn == cc->zone->zone_start_pfn) && | |
e380bebe | 2150 | !fast_find_block && !isolation_suitable(cc, page)) |
edc2ca61 VB |
2151 | continue; |
2152 | ||
2153 | /* | |
556162bf ML |
2154 | * For async direct compaction, only scan the pageblocks of the |
2155 | * same migratetype without huge pages. Async direct compaction | |
2156 | * is optimistic to see if the minimum amount of work satisfies | |
2157 | * the allocation. The cached PFN is updated as it's possible | |
2158 | * that all remaining blocks between source and target are | |
2159 | * unsuitable and the compaction scanners fail to meet. | |
edc2ca61 | 2160 | */ |
9bebefd5 MG |
2161 | if (!suitable_migration_source(cc, page)) { |
2162 | update_cached_migrate(cc, block_end_pfn); | |
edc2ca61 | 2163 | continue; |
9bebefd5 | 2164 | } |
edc2ca61 VB |
2165 | |
2166 | /* Perform the isolation */ | |
c2ad7a1f OS |
2167 | if (isolate_migratepages_block(cc, low_pfn, block_end_pfn, |
2168 | isolate_mode)) | |
edc2ca61 VB |
2169 | return ISOLATE_ABORT; |
2170 | ||
2171 | /* | |
2172 | * Either we isolated something and proceed with migration. Or | |
2173 | * we failed and compact_zone should decide if we should | |
2174 | * continue or not. | |
2175 | */ | |
2176 | break; | |
2177 | } | |
2178 | ||
edc2ca61 | 2179 | return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE; |
ff9543fd MN |
2180 | } |
2181 | ||
b4a0215e KW |
2182 | /* |
2183 | * Determine whether kswapd is (or recently was!) running on this node. | |
2184 | * | |
2185 | * pgdat_kswapd_lock() pins pgdat->kswapd, so a concurrent kswapd_stop() can't | |
2186 | * zero it. | |
2187 | */ | |
facdaa91 NG |
2188 | static bool kswapd_is_running(pg_data_t *pgdat) |
2189 | { | |
b4a0215e KW |
2190 | bool running; |
2191 | ||
2192 | pgdat_kswapd_lock(pgdat); | |
2193 | running = pgdat->kswapd && task_is_running(pgdat->kswapd); | |
2194 | pgdat_kswapd_unlock(pgdat); | |
2195 | ||
2196 | return running; | |
facdaa91 NG |
2197 | } |
2198 | ||
2199 | /* | |
2200 | * A zone's fragmentation score is the external fragmentation wrt to the | |
40d7e203 CTR |
2201 | * COMPACTION_HPAGE_ORDER. It returns a value in the range [0, 100]. |
2202 | */ | |
2203 | static unsigned int fragmentation_score_zone(struct zone *zone) | |
2204 | { | |
2205 | return extfrag_for_order(zone, COMPACTION_HPAGE_ORDER); | |
2206 | } | |
2207 | ||
2208 | /* | |
2209 | * A weighted zone's fragmentation score is the external fragmentation | |
2210 | * wrt to the COMPACTION_HPAGE_ORDER scaled by the zone's size. It | |
2211 | * returns a value in the range [0, 100]. | |
facdaa91 NG |
2212 | * |
2213 | * The scaling factor ensures that proactive compaction focuses on larger | |
2214 | * zones like ZONE_NORMAL, rather than smaller, specialized zones like | |
2215 | * ZONE_DMA32. For smaller zones, the score value remains close to zero, | |
2216 | * and thus never exceeds the high threshold for proactive compaction. | |
2217 | */ | |
40d7e203 | 2218 | static unsigned int fragmentation_score_zone_weighted(struct zone *zone) |
facdaa91 NG |
2219 | { |
2220 | unsigned long score; | |
2221 | ||
40d7e203 | 2222 | score = zone->present_pages * fragmentation_score_zone(zone); |
facdaa91 NG |
2223 | return div64_ul(score, zone->zone_pgdat->node_present_pages + 1); |
2224 | } | |
2225 | ||
2226 | /* | |
2227 | * The per-node proactive (background) compaction process is started by its | |
2228 | * corresponding kcompactd thread when the node's fragmentation score | |
2229 | * exceeds the high threshold. The compaction process remains active till | |
2230 | * the node's score falls below the low threshold, or one of the back-off | |
2231 | * conditions is met. | |
2232 | */ | |
d34c0a75 | 2233 | static unsigned int fragmentation_score_node(pg_data_t *pgdat) |
facdaa91 | 2234 | { |
d34c0a75 | 2235 | unsigned int score = 0; |
facdaa91 NG |
2236 | int zoneid; |
2237 | ||
2238 | for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { | |
2239 | struct zone *zone; | |
2240 | ||
2241 | zone = &pgdat->node_zones[zoneid]; | |
9e552271 BW |
2242 | if (!populated_zone(zone)) |
2243 | continue; | |
40d7e203 | 2244 | score += fragmentation_score_zone_weighted(zone); |
facdaa91 NG |
2245 | } |
2246 | ||
2247 | return score; | |
2248 | } | |
2249 | ||
8fbb92bd | 2250 | static unsigned int fragmentation_score_wmark(bool low) |
facdaa91 | 2251 | { |
98c93890 | 2252 | unsigned int wmark_low, leeway; |
facdaa91 | 2253 | |
bb317f00 | 2254 | wmark_low = 100U - sysctl_compaction_proactiveness; |
98c93890 MC |
2255 | leeway = min(10U, wmark_low / 2); |
2256 | return low ? wmark_low : min(wmark_low + leeway, 100U); | |
facdaa91 NG |
2257 | } |
2258 | ||
2259 | static bool should_proactive_compact_node(pg_data_t *pgdat) | |
2260 | { | |
2261 | int wmark_high; | |
2262 | ||
2263 | if (!sysctl_compaction_proactiveness || kswapd_is_running(pgdat)) | |
2264 | return false; | |
2265 | ||
8fbb92bd | 2266 | wmark_high = fragmentation_score_wmark(false); |
facdaa91 NG |
2267 | return fragmentation_score_node(pgdat) > wmark_high; |
2268 | } | |
2269 | ||
40cacbcb | 2270 | static enum compact_result __compact_finished(struct compact_control *cc) |
748446bb | 2271 | { |
8fb74b9f | 2272 | unsigned int order; |
d39773a0 | 2273 | const int migratetype = cc->migratetype; |
cb2dcaf0 | 2274 | int ret; |
748446bb | 2275 | |
753341a4 | 2276 | /* Compaction run completes if the migrate and free scanner meet */ |
f2849aa0 | 2277 | if (compact_scanners_met(cc)) { |
55b7c4c9 | 2278 | /* Let the next compaction start anew. */ |
40cacbcb | 2279 | reset_cached_positions(cc->zone); |
55b7c4c9 | 2280 | |
62997027 MG |
2281 | /* |
2282 | * Mark that the PG_migrate_skip information should be cleared | |
accf6242 | 2283 | * by kswapd when it goes to sleep. kcompactd does not set the |
62997027 MG |
2284 | * flag itself as the decision to be clear should be directly |
2285 | * based on an allocation request. | |
2286 | */ | |
accf6242 | 2287 | if (cc->direct_compaction) |
40cacbcb | 2288 | cc->zone->compact_blockskip_flush = true; |
62997027 | 2289 | |
c8f7de0b MH |
2290 | if (cc->whole_zone) |
2291 | return COMPACT_COMPLETE; | |
2292 | else | |
2293 | return COMPACT_PARTIAL_SKIPPED; | |
bb13ffeb | 2294 | } |
748446bb | 2295 | |
facdaa91 NG |
2296 | if (cc->proactive_compaction) { |
2297 | int score, wmark_low; | |
2298 | pg_data_t *pgdat; | |
2299 | ||
2300 | pgdat = cc->zone->zone_pgdat; | |
2301 | if (kswapd_is_running(pgdat)) | |
2302 | return COMPACT_PARTIAL_SKIPPED; | |
2303 | ||
2304 | score = fragmentation_score_zone(cc->zone); | |
8fbb92bd | 2305 | wmark_low = fragmentation_score_wmark(true); |
facdaa91 NG |
2306 | |
2307 | if (score > wmark_low) | |
2308 | ret = COMPACT_CONTINUE; | |
2309 | else | |
2310 | ret = COMPACT_SUCCESS; | |
2311 | ||
2312 | goto out; | |
2313 | } | |
2314 | ||
21c527a3 | 2315 | if (is_via_compact_memory(cc->order)) |
56de7263 MG |
2316 | return COMPACT_CONTINUE; |
2317 | ||
efe771c7 MG |
2318 | /* |
2319 | * Always finish scanning a pageblock to reduce the possibility of | |
2320 | * fallbacks in the future. This is particularly important when | |
2321 | * migration source is unmovable/reclaimable but it's not worth | |
2322 | * special casing. | |
2323 | */ | |
ee0913c4 | 2324 | if (!pageblock_aligned(cc->migrate_pfn)) |
efe771c7 | 2325 | return COMPACT_CONTINUE; |
baf6a9a1 | 2326 | |
a211c655 JW |
2327 | /* |
2328 | * When defrag_mode is enabled, make kcompactd target | |
2329 | * watermarks in whole pageblocks. Because they can be stolen | |
2330 | * without polluting, no further fallback checks are needed. | |
2331 | */ | |
2332 | if (defrag_mode && !cc->direct_compaction) { | |
2333 | if (__zone_watermark_ok(cc->zone, cc->order, | |
2334 | high_wmark_pages(cc->zone), | |
2335 | cc->highest_zoneidx, cc->alloc_flags, | |
2336 | zone_page_state(cc->zone, | |
2337 | NR_FREE_PAGES_BLOCKS))) | |
2338 | return COMPACT_SUCCESS; | |
2339 | ||
2340 | return COMPACT_CONTINUE; | |
2341 | } | |
2342 | ||
56de7263 | 2343 | /* Direct compactor: Is a suitable page free? */ |
cb2dcaf0 | 2344 | ret = COMPACT_NO_SUITABLE_PAGE; |
fd377218 | 2345 | for (order = cc->order; order < NR_PAGE_ORDERS; order++) { |
40cacbcb | 2346 | struct free_area *area = &cc->zone->free_area[order]; |
8fb74b9f MG |
2347 | |
2348 | /* Job done if page is free of the right migratetype */ | |
b03641af | 2349 | if (!free_area_empty(area, migratetype)) |
cf378319 | 2350 | return COMPACT_SUCCESS; |
8fb74b9f | 2351 | |
2149cdae JK |
2352 | #ifdef CONFIG_CMA |
2353 | /* MIGRATE_MOVABLE can fallback on MIGRATE_CMA */ | |
2354 | if (migratetype == MIGRATE_MOVABLE && | |
b03641af | 2355 | !free_area_empty(area, MIGRATE_CMA)) |
cf378319 | 2356 | return COMPACT_SUCCESS; |
2149cdae JK |
2357 | #endif |
2358 | /* | |
2359 | * Job done if allocation would steal freepages from | |
2360 | * other migratetype buddy lists. | |
2361 | */ | |
ee414bd9 | 2362 | if (find_suitable_fallback(area, order, migratetype, true) >= 0) |
baf6a9a1 | 2363 | /* |
fa599c44 ML |
2364 | * Movable pages are OK in any pageblock. If we are |
2365 | * stealing for a non-movable allocation, make sure | |
2366 | * we finish compacting the current pageblock first | |
2367 | * (which is assured by the above migrate_pfn align | |
2368 | * check) so it is as free as possible and we won't | |
2369 | * have to steal another one soon. | |
baf6a9a1 | 2370 | */ |
fa599c44 | 2371 | return COMPACT_SUCCESS; |
56de7263 MG |
2372 | } |
2373 | ||
facdaa91 | 2374 | out: |
cb2dcaf0 MG |
2375 | if (cc->contended || fatal_signal_pending(current)) |
2376 | ret = COMPACT_CONTENDED; | |
2377 | ||
2378 | return ret; | |
837d026d JK |
2379 | } |
2380 | ||
40cacbcb | 2381 | static enum compact_result compact_finished(struct compact_control *cc) |
837d026d JK |
2382 | { |
2383 | int ret; | |
2384 | ||
40cacbcb MG |
2385 | ret = __compact_finished(cc); |
2386 | trace_mm_compaction_finished(cc->zone, cc->order, ret); | |
837d026d JK |
2387 | if (ret == COMPACT_NO_SUITABLE_PAGE) |
2388 | ret = COMPACT_CONTINUE; | |
2389 | ||
2390 | return ret; | |
748446bb MG |
2391 | } |
2392 | ||
3cf04937 | 2393 | static bool __compaction_suitable(struct zone *zone, int order, |
67914ac0 JW |
2394 | unsigned long watermark, int highest_zoneidx, |
2395 | unsigned long free_pages) | |
3e7d3449 | 2396 | { |
3e7d3449 | 2397 | /* |
9861a62c | 2398 | * Watermarks for order-0 must be met for compaction to be able to |
984fdba6 | 2399 | * isolate free pages for migration targets. This means that the |
67914ac0 JW |
2400 | * watermark have to match, or be more pessimistic than the check in |
2401 | * __isolate_free_page(). | |
2402 | * | |
2403 | * For costly orders, we require a higher watermark for compaction to | |
2404 | * proceed to increase its chances. | |
2405 | * | |
2406 | * We use the direct compactor's highest_zoneidx to skip over zones | |
2407 | * where lowmem reserves would prevent allocation even if compaction | |
2408 | * succeeds. | |
2409 | * | |
d883c6cf | 2410 | * ALLOC_CMA is used, as pages in CMA pageblocks are considered |
67914ac0 | 2411 | * suitable migration targets. |
3e7d3449 | 2412 | */ |
8348faf9 | 2413 | watermark += compact_gap(order); |
67914ac0 JW |
2414 | if (order > PAGE_ALLOC_COSTLY_ORDER) |
2415 | watermark += low_wmark_pages(zone) - min_wmark_pages(zone); | |
3cf04937 | 2416 | return __zone_watermark_ok(zone, 0, watermark, highest_zoneidx, |
67914ac0 | 2417 | ALLOC_CMA, free_pages); |
cc5c9f09 VB |
2418 | } |
2419 | ||
2b1a20c3 HS |
2420 | /* |
2421 | * compaction_suitable: Is this suitable to run compaction on this zone now? | |
2b1a20c3 | 2422 | */ |
67914ac0 JW |
2423 | bool compaction_suitable(struct zone *zone, int order, unsigned long watermark, |
2424 | int highest_zoneidx) | |
cc5c9f09 | 2425 | { |
3cf04937 JW |
2426 | enum compact_result compact_result; |
2427 | bool suitable; | |
cc5c9f09 | 2428 | |
67914ac0 | 2429 | suitable = __compaction_suitable(zone, order, watermark, highest_zoneidx, |
3cf04937 | 2430 | zone_page_state(zone, NR_FREE_PAGES)); |
3e7d3449 MG |
2431 | /* |
2432 | * fragmentation index determines if allocation failures are due to | |
2433 | * low memory or external fragmentation | |
2434 | * | |
ebff3980 VB |
2435 | * index of -1000 would imply allocations might succeed depending on |
2436 | * watermarks, but we already failed the high-order watermark check | |
3e7d3449 MG |
2437 | * index towards 0 implies failure is due to lack of memory |
2438 | * index towards 1000 implies failure is due to fragmentation | |
2439 | * | |
20311420 VB |
2440 | * Only compact if a failure would be due to fragmentation. Also |
2441 | * ignore fragindex for non-costly orders where the alternative to | |
2442 | * a successful reclaim/compaction is OOM. Fragindex and the | |
2443 | * vm.extfrag_threshold sysctl is meant as a heuristic to prevent | |
2444 | * excessive compaction for costly orders, but it should not be at the | |
2445 | * expense of system stability. | |
3e7d3449 | 2446 | */ |
3cf04937 JW |
2447 | if (suitable) { |
2448 | compact_result = COMPACT_CONTINUE; | |
2449 | if (order > PAGE_ALLOC_COSTLY_ORDER) { | |
2450 | int fragindex = fragmentation_index(zone, order); | |
2451 | ||
2452 | if (fragindex >= 0 && | |
2453 | fragindex <= sysctl_extfrag_threshold) { | |
2454 | suitable = false; | |
2455 | compact_result = COMPACT_NOT_SUITABLE_ZONE; | |
2456 | } | |
2457 | } | |
2458 | } else { | |
2459 | compact_result = COMPACT_SKIPPED; | |
cc5c9f09 | 2460 | } |
837d026d | 2461 | |
3cf04937 | 2462 | trace_mm_compaction_suitable(zone, order, compact_result); |
837d026d | 2463 | |
3cf04937 | 2464 | return suitable; |
837d026d JK |
2465 | } |
2466 | ||
67914ac0 | 2467 | /* Used by direct reclaimers */ |
86a294a8 MH |
2468 | bool compaction_zonelist_suitable(struct alloc_context *ac, int order, |
2469 | int alloc_flags) | |
2470 | { | |
2471 | struct zone *zone; | |
2472 | struct zoneref *z; | |
2473 | ||
2474 | /* | |
2475 | * Make sure at least one zone would pass __compaction_suitable if we continue | |
2476 | * retrying the reclaim. | |
2477 | */ | |
97a225e6 JK |
2478 | for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, |
2479 | ac->highest_zoneidx, ac->nodemask) { | |
86a294a8 | 2480 | unsigned long available; |
86a294a8 MH |
2481 | |
2482 | /* | |
2483 | * Do not consider all the reclaimable memory because we do not | |
2484 | * want to trash just for a single high order allocation which | |
2485 | * is even not guaranteed to appear even if __compaction_suitable | |
2486 | * is happy about the watermark check. | |
2487 | */ | |
5a1c84b4 | 2488 | available = zone_reclaimable_pages(zone) / order; |
86a294a8 | 2489 | available += zone_page_state_snapshot(zone, NR_FREE_PAGES); |
67914ac0 JW |
2490 | if (__compaction_suitable(zone, order, min_wmark_pages(zone), |
2491 | ac->highest_zoneidx, available)) | |
86a294a8 MH |
2492 | return true; |
2493 | } | |
2494 | ||
2495 | return false; | |
2496 | } | |
2497 | ||
e19a3f59 KS |
2498 | /* |
2499 | * Should we do compaction for target allocation order. | |
2500 | * Return COMPACT_SUCCESS if allocation for target order can be already | |
2501 | * satisfied | |
2502 | * Return COMPACT_SKIPPED if compaction for target order is likely to fail | |
2503 | * Return COMPACT_CONTINUE if compaction for target order should be ran | |
2504 | */ | |
2505 | static enum compact_result | |
2506 | compaction_suit_allocation_order(struct zone *zone, unsigned int order, | |
6268f0a1 | 2507 | int highest_zoneidx, unsigned int alloc_flags, |
a211c655 | 2508 | bool async, bool kcompactd) |
e19a3f59 | 2509 | { |
a211c655 | 2510 | unsigned long free_pages; |
e19a3f59 KS |
2511 | unsigned long watermark; |
2512 | ||
a211c655 JW |
2513 | if (kcompactd && defrag_mode) |
2514 | free_pages = zone_page_state(zone, NR_FREE_PAGES_BLOCKS); | |
2515 | else | |
2516 | free_pages = zone_page_state(zone, NR_FREE_PAGES); | |
2517 | ||
e19a3f59 | 2518 | watermark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK); |
a211c655 JW |
2519 | if (__zone_watermark_ok(zone, order, watermark, highest_zoneidx, |
2520 | alloc_flags, free_pages)) | |
e19a3f59 KS |
2521 | return COMPACT_SUCCESS; |
2522 | ||
6268f0a1 | 2523 | /* |
2524 | * For unmovable allocations (without ALLOC_CMA), check if there is enough | |
2525 | * free memory in the non-CMA pageblocks. Otherwise compaction could form | |
2526 | * the high-order page in CMA pageblocks, which would not help the | |
2527 | * allocation to succeed. However, limit the check to costly order async | |
2528 | * compaction (such as opportunistic THP attempts) because there is the | |
2529 | * possibility that compaction would migrate pages from non-CMA to CMA | |
2530 | * pageblock. | |
2531 | */ | |
2532 | if (order > PAGE_ALLOC_COSTLY_ORDER && async && | |
2533 | !(alloc_flags & ALLOC_CMA)) { | |
67914ac0 JW |
2534 | if (!__zone_watermark_ok(zone, 0, watermark + compact_gap(order), |
2535 | highest_zoneidx, 0, | |
2536 | zone_page_state(zone, NR_FREE_PAGES))) | |
6268f0a1 | 2537 | return COMPACT_SKIPPED; |
2538 | } | |
2539 | ||
67914ac0 | 2540 | if (!compaction_suitable(zone, order, watermark, highest_zoneidx)) |
e19a3f59 KS |
2541 | return COMPACT_SKIPPED; |
2542 | ||
2543 | return COMPACT_CONTINUE; | |
2544 | } | |
2545 | ||
5e1f0f09 MG |
2546 | static enum compact_result |
2547 | compact_zone(struct compact_control *cc, struct capture_control *capc) | |
748446bb | 2548 | { |
ea7ab982 | 2549 | enum compact_result ret; |
40cacbcb MG |
2550 | unsigned long start_pfn = cc->zone->zone_start_pfn; |
2551 | unsigned long end_pfn = zone_end_pfn(cc->zone); | |
566e54e1 | 2552 | unsigned long last_migrated_pfn; |
e0b9daeb | 2553 | const bool sync = cc->mode != MIGRATE_ASYNC; |
8854c55f | 2554 | bool update_cached; |
ab755bf4 | 2555 | unsigned int nr_succeeded = 0, nr_migratepages; |
733aea0b | 2556 | int order; |
748446bb | 2557 | |
a94b5252 YS |
2558 | /* |
2559 | * These counters track activities during zone compaction. Initialize | |
2560 | * them before compacting a new zone. | |
2561 | */ | |
2562 | cc->total_migrate_scanned = 0; | |
2563 | cc->total_free_scanned = 0; | |
2564 | cc->nr_migratepages = 0; | |
2565 | cc->nr_freepages = 0; | |
733aea0b ZY |
2566 | for (order = 0; order < NR_PAGE_ORDERS; order++) |
2567 | INIT_LIST_HEAD(&cc->freepages[order]); | |
a94b5252 YS |
2568 | INIT_LIST_HEAD(&cc->migratepages); |
2569 | ||
01c0bfe0 | 2570 | cc->migratetype = gfp_migratetype(cc->gfp_mask); |
e8606320 JW |
2571 | |
2572 | if (!is_via_compact_memory(cc->order)) { | |
e19a3f59 KS |
2573 | ret = compaction_suit_allocation_order(cc->zone, cc->order, |
2574 | cc->highest_zoneidx, | |
6268f0a1 | 2575 | cc->alloc_flags, |
a211c655 JW |
2576 | cc->mode == MIGRATE_ASYNC, |
2577 | !cc->direct_compaction); | |
e19a3f59 KS |
2578 | if (ret != COMPACT_CONTINUE) |
2579 | return ret; | |
e8606320 | 2580 | } |
c46649de | 2581 | |
d3132e4b VB |
2582 | /* |
2583 | * Clear pageblock skip if there were failures recently and compaction | |
accf6242 | 2584 | * is about to be retried after being deferred. |
d3132e4b | 2585 | */ |
40cacbcb MG |
2586 | if (compaction_restarting(cc->zone, cc->order)) |
2587 | __reset_isolation_suitable(cc->zone); | |
d3132e4b | 2588 | |
c89511ab MG |
2589 | /* |
2590 | * Setup to move all movable pages to the end of the zone. Used cached | |
06ed2998 VB |
2591 | * information on where the scanners should start (unless we explicitly |
2592 | * want to compact the whole zone), but check that it is initialised | |
2593 | * by ensuring the values are within zone boundaries. | |
c89511ab | 2594 | */ |
70b44595 | 2595 | cc->fast_start_pfn = 0; |
06ed2998 | 2596 | if (cc->whole_zone) { |
c89511ab | 2597 | cc->migrate_pfn = start_pfn; |
06ed2998 VB |
2598 | cc->free_pfn = pageblock_start_pfn(end_pfn - 1); |
2599 | } else { | |
40cacbcb MG |
2600 | cc->migrate_pfn = cc->zone->compact_cached_migrate_pfn[sync]; |
2601 | cc->free_pfn = cc->zone->compact_cached_free_pfn; | |
06ed2998 VB |
2602 | if (cc->free_pfn < start_pfn || cc->free_pfn >= end_pfn) { |
2603 | cc->free_pfn = pageblock_start_pfn(end_pfn - 1); | |
40cacbcb | 2604 | cc->zone->compact_cached_free_pfn = cc->free_pfn; |
06ed2998 VB |
2605 | } |
2606 | if (cc->migrate_pfn < start_pfn || cc->migrate_pfn >= end_pfn) { | |
2607 | cc->migrate_pfn = start_pfn; | |
40cacbcb MG |
2608 | cc->zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn; |
2609 | cc->zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn; | |
06ed2998 | 2610 | } |
c8f7de0b | 2611 | |
e332f741 | 2612 | if (cc->migrate_pfn <= cc->zone->compact_init_migrate_pfn) |
06ed2998 VB |
2613 | cc->whole_zone = true; |
2614 | } | |
c8f7de0b | 2615 | |
566e54e1 | 2616 | last_migrated_pfn = 0; |
748446bb | 2617 | |
8854c55f MG |
2618 | /* |
2619 | * Migrate has separate cached PFNs for ASYNC and SYNC* migration on | |
2620 | * the basis that some migrations will fail in ASYNC mode. However, | |
2621 | * if the cached PFNs match and pageblocks are skipped due to having | |
2622 | * no isolation candidates, then the sync state does not matter. | |
2623 | * Until a pageblock with isolation candidates is found, keep the | |
2624 | * cached PFNs in sync to avoid revisiting the same blocks. | |
2625 | */ | |
2626 | update_cached = !sync && | |
2627 | cc->zone->compact_cached_migrate_pfn[0] == cc->zone->compact_cached_migrate_pfn[1]; | |
2628 | ||
abd4349f | 2629 | trace_mm_compaction_begin(cc, start_pfn, end_pfn, sync); |
0eb927c0 | 2630 | |
361a2a22 MK |
2631 | /* lru_add_drain_all could be expensive with involving other CPUs */ |
2632 | lru_add_drain(); | |
748446bb | 2633 | |
40cacbcb | 2634 | while ((ret = compact_finished(cc)) == COMPACT_CONTINUE) { |
9d502c1c | 2635 | int err; |
19d3cf9d | 2636 | unsigned long iteration_start_pfn = cc->migrate_pfn; |
748446bb | 2637 | |
804d3121 | 2638 | /* |
48731c84 MG |
2639 | * Avoid multiple rescans of the same pageblock which can |
2640 | * happen if a page cannot be isolated (dirty/writeback in | |
2641 | * async mode) or if the migrated pages are being allocated | |
2642 | * before the pageblock is cleared. The first rescan will | |
2643 | * capture the entire pageblock for migration. If it fails, | |
2644 | * it'll be marked skip and scanning will proceed as normal. | |
804d3121 | 2645 | */ |
48731c84 | 2646 | cc->finish_pageblock = false; |
804d3121 | 2647 | if (pageblock_start_pfn(last_migrated_pfn) == |
19d3cf9d | 2648 | pageblock_start_pfn(iteration_start_pfn)) { |
48731c84 | 2649 | cc->finish_pageblock = true; |
804d3121 MG |
2650 | } |
2651 | ||
cfccd2e6 | 2652 | rescan: |
32aaf055 | 2653 | switch (isolate_migratepages(cc)) { |
f9e35b3b | 2654 | case ISOLATE_ABORT: |
2d1e1041 | 2655 | ret = COMPACT_CONTENDED; |
5733c7d1 | 2656 | putback_movable_pages(&cc->migratepages); |
e64c5237 | 2657 | cc->nr_migratepages = 0; |
f9e35b3b MG |
2658 | goto out; |
2659 | case ISOLATE_NONE: | |
8854c55f MG |
2660 | if (update_cached) { |
2661 | cc->zone->compact_cached_migrate_pfn[1] = | |
2662 | cc->zone->compact_cached_migrate_pfn[0]; | |
2663 | } | |
2664 | ||
fdaf7f5c VB |
2665 | /* |
2666 | * We haven't isolated and migrated anything, but | |
2667 | * there might still be unflushed migrations from | |
2668 | * previous cc->order aligned block. | |
2669 | */ | |
2670 | goto check_drain; | |
f9e35b3b | 2671 | case ISOLATE_SUCCESS: |
8854c55f | 2672 | update_cached = false; |
7c0a84bd KS |
2673 | last_migrated_pfn = max(cc->zone->zone_start_pfn, |
2674 | pageblock_start_pfn(cc->migrate_pfn - 1)); | |
f9e35b3b | 2675 | } |
748446bb | 2676 | |
ab755bf4 BW |
2677 | /* |
2678 | * Record the number of pages to migrate since the | |
2679 | * compaction_alloc/free() will update cc->nr_migratepages | |
2680 | * properly. | |
2681 | */ | |
2682 | nr_migratepages = cc->nr_migratepages; | |
d53aea3d | 2683 | err = migrate_pages(&cc->migratepages, compaction_alloc, |
e0b9daeb | 2684 | compaction_free, (unsigned long)cc, cc->mode, |
84b328aa | 2685 | MR_COMPACTION, &nr_succeeded); |
748446bb | 2686 | |
ab755bf4 | 2687 | trace_mm_compaction_migratepages(nr_migratepages, nr_succeeded); |
748446bb | 2688 | |
f8c9301f VB |
2689 | /* All pages were either migrated or will be released */ |
2690 | cc->nr_migratepages = 0; | |
9d502c1c | 2691 | if (err) { |
5733c7d1 | 2692 | putback_movable_pages(&cc->migratepages); |
7ed695e0 VB |
2693 | /* |
2694 | * migrate_pages() may return -ENOMEM when scanners meet | |
2695 | * and we want compact_finished() to detect it | |
2696 | */ | |
f2849aa0 | 2697 | if (err == -ENOMEM && !compact_scanners_met(cc)) { |
2d1e1041 | 2698 | ret = COMPACT_CONTENDED; |
4bf2bba3 DR |
2699 | goto out; |
2700 | } | |
fdd048e1 | 2701 | /* |
cfccd2e6 | 2702 | * If an ASYNC or SYNC_LIGHT fails to migrate a page |
c3750cc7 | 2703 | * within the pageblock_order-aligned block and |
9ecc5fc5 | 2704 | * fast_find_migrateblock may be used then scan the |
cfccd2e6 MG |
2705 | * remainder of the pageblock. This will mark the |
2706 | * pageblock "skip" to avoid rescanning in the near | |
2707 | * future. This will isolate more pages than necessary | |
2708 | * for the request but avoid loops due to | |
2709 | * fast_find_migrateblock revisiting blocks that were | |
2710 | * recently partially scanned. | |
fdd048e1 | 2711 | */ |
539aa041 | 2712 | if (!pageblock_aligned(cc->migrate_pfn) && |
9ecc5fc5 | 2713 | !cc->ignore_skip_hint && !cc->finish_pageblock && |
539aa041 | 2714 | (cc->mode < MIGRATE_SYNC)) { |
cfccd2e6 MG |
2715 | cc->finish_pageblock = true; |
2716 | ||
2717 | /* | |
2718 | * Draining pcplists does not help THP if | |
2719 | * any page failed to migrate. Even after | |
2720 | * drain, the pageblock will not be free. | |
2721 | */ | |
2722 | if (cc->order == COMPACTION_HPAGE_ORDER) | |
2723 | last_migrated_pfn = 0; | |
2724 | ||
2725 | goto rescan; | |
fdd048e1 | 2726 | } |
748446bb | 2727 | } |
fdaf7f5c | 2728 | |
16b3be40 MG |
2729 | /* Stop if a page has been captured */ |
2730 | if (capc && capc->page) { | |
2731 | ret = COMPACT_SUCCESS; | |
2732 | break; | |
2733 | } | |
2734 | ||
fdaf7f5c VB |
2735 | check_drain: |
2736 | /* | |
2737 | * Has the migration scanner moved away from the previous | |
2738 | * cc->order aligned block where we migrated from? If yes, | |
2739 | * flush the pages that were freed, so that they can merge and | |
2740 | * compact_finished() can detect immediately if allocation | |
2741 | * would succeed. | |
2742 | */ | |
566e54e1 | 2743 | if (cc->order > 0 && last_migrated_pfn) { |
fdaf7f5c | 2744 | unsigned long current_block_start = |
06b6640a | 2745 | block_start_pfn(cc->migrate_pfn, cc->order); |
fdaf7f5c | 2746 | |
566e54e1 | 2747 | if (last_migrated_pfn < current_block_start) { |
b01b2141 | 2748 | lru_add_drain_cpu_zone(cc->zone); |
fdaf7f5c | 2749 | /* No more flushing until we migrate again */ |
566e54e1 | 2750 | last_migrated_pfn = 0; |
fdaf7f5c VB |
2751 | } |
2752 | } | |
748446bb MG |
2753 | } |
2754 | ||
f9e35b3b | 2755 | out: |
6bace090 VB |
2756 | /* |
2757 | * Release free pages and update where the free scanner should restart, | |
2758 | * so we don't leave any returned pages behind in the next attempt. | |
2759 | */ | |
2760 | if (cc->nr_freepages > 0) { | |
733aea0b | 2761 | unsigned long free_pfn = release_free_list(cc->freepages); |
6bace090 VB |
2762 | |
2763 | cc->nr_freepages = 0; | |
2764 | VM_BUG_ON(free_pfn == 0); | |
2765 | /* The cached pfn is always the first in a pageblock */ | |
06b6640a | 2766 | free_pfn = pageblock_start_pfn(free_pfn); |
6bace090 VB |
2767 | /* |
2768 | * Only go back, not forward. The cached pfn might have been | |
2769 | * already reset to zone end in compact_finished() | |
2770 | */ | |
40cacbcb MG |
2771 | if (free_pfn > cc->zone->compact_cached_free_pfn) |
2772 | cc->zone->compact_cached_free_pfn = free_pfn; | |
6bace090 | 2773 | } |
748446bb | 2774 | |
7f354a54 DR |
2775 | count_compact_events(COMPACTMIGRATE_SCANNED, cc->total_migrate_scanned); |
2776 | count_compact_events(COMPACTFREE_SCANNED, cc->total_free_scanned); | |
2777 | ||
abd4349f | 2778 | trace_mm_compaction_end(cc, start_pfn, end_pfn, sync, ret); |
0eb927c0 | 2779 | |
753ec50d BW |
2780 | VM_BUG_ON(!list_empty(&cc->migratepages)); |
2781 | ||
748446bb MG |
2782 | return ret; |
2783 | } | |
76ab0f53 | 2784 | |
ea7ab982 | 2785 | static enum compact_result compact_zone_order(struct zone *zone, int order, |
c3486f53 | 2786 | gfp_t gfp_mask, enum compact_priority prio, |
97a225e6 | 2787 | unsigned int alloc_flags, int highest_zoneidx, |
5e1f0f09 | 2788 | struct page **capture) |
56de7263 | 2789 | { |
ea7ab982 | 2790 | enum compact_result ret; |
56de7263 | 2791 | struct compact_control cc = { |
56de7263 | 2792 | .order = order, |
dbe2d4e4 | 2793 | .search_order = order, |
6d7ce559 | 2794 | .gfp_mask = gfp_mask, |
56de7263 | 2795 | .zone = zone, |
a5508cd8 VB |
2796 | .mode = (prio == COMPACT_PRIO_ASYNC) ? |
2797 | MIGRATE_ASYNC : MIGRATE_SYNC_LIGHT, | |
ebff3980 | 2798 | .alloc_flags = alloc_flags, |
97a225e6 | 2799 | .highest_zoneidx = highest_zoneidx, |
accf6242 | 2800 | .direct_compaction = true, |
a8e025e5 | 2801 | .whole_zone = (prio == MIN_COMPACT_PRIORITY), |
9f7e3387 VB |
2802 | .ignore_skip_hint = (prio == MIN_COMPACT_PRIORITY), |
2803 | .ignore_block_suitable = (prio == MIN_COMPACT_PRIORITY) | |
56de7263 | 2804 | }; |
5e1f0f09 MG |
2805 | struct capture_control capc = { |
2806 | .cc = &cc, | |
2807 | .page = NULL, | |
2808 | }; | |
2809 | ||
b9e20f0d VB |
2810 | /* |
2811 | * Make sure the structs are really initialized before we expose the | |
2812 | * capture control, in case we are interrupted and the interrupt handler | |
2813 | * frees a page. | |
2814 | */ | |
2815 | barrier(); | |
2816 | WRITE_ONCE(current->capture_control, &capc); | |
56de7263 | 2817 | |
5e1f0f09 | 2818 | ret = compact_zone(&cc, &capc); |
e64c5237 | 2819 | |
b9e20f0d VB |
2820 | /* |
2821 | * Make sure we hide capture control first before we read the captured | |
2822 | * page pointer, otherwise an interrupt could free and capture a page | |
2823 | * and we would leak it. | |
2824 | */ | |
2825 | WRITE_ONCE(current->capture_control, NULL); | |
2826 | *capture = READ_ONCE(capc.page); | |
06dac2f4 CTR |
2827 | /* |
2828 | * Technically, it is also possible that compaction is skipped but | |
2829 | * the page is still captured out of luck(IRQ came and freed the page). | |
2830 | * Returning COMPACT_SUCCESS in such cases helps in properly accounting | |
2831 | * the COMPACT[STALL|FAIL] when compaction is skipped. | |
2832 | */ | |
2833 | if (*capture) | |
2834 | ret = COMPACT_SUCCESS; | |
5e1f0f09 | 2835 | |
e64c5237 | 2836 | return ret; |
56de7263 MG |
2837 | } |
2838 | ||
2839 | /** | |
2840 | * try_to_compact_pages - Direct compact to satisfy a high-order allocation | |
56de7263 | 2841 | * @gfp_mask: The GFP mask of the current allocation |
1a6d53a1 VB |
2842 | * @order: The order of the current allocation |
2843 | * @alloc_flags: The allocation flags of the current allocation | |
2844 | * @ac: The context of current allocation | |
112d2d29 | 2845 | * @prio: Determines how hard direct compaction should try to succeed |
6467552c | 2846 | * @capture: Pointer to free page created by compaction will be stored here |
56de7263 MG |
2847 | * |
2848 | * This is the main entry point for direct page compaction. | |
2849 | */ | |
ea7ab982 | 2850 | enum compact_result try_to_compact_pages(gfp_t gfp_mask, unsigned int order, |
c603844b | 2851 | unsigned int alloc_flags, const struct alloc_context *ac, |
5e1f0f09 | 2852 | enum compact_priority prio, struct page **capture) |
56de7263 | 2853 | { |
56de7263 MG |
2854 | struct zoneref *z; |
2855 | struct zone *zone; | |
1d4746d3 | 2856 | enum compact_result rc = COMPACT_SKIPPED; |
56de7263 | 2857 | |
803de900 | 2858 | if (!gfp_compaction_allowed(gfp_mask)) |
53853e2d | 2859 | return COMPACT_SKIPPED; |
56de7263 | 2860 | |
a5508cd8 | 2861 | trace_mm_compaction_try_to_compact_pages(order, gfp_mask, prio); |
837d026d | 2862 | |
56de7263 | 2863 | /* Compact each zone in the list */ |
97a225e6 JK |
2864 | for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, |
2865 | ac->highest_zoneidx, ac->nodemask) { | |
ea7ab982 | 2866 | enum compact_result status; |
56de7263 | 2867 | |
435b3894 ZH |
2868 | if (cpusets_enabled() && |
2869 | (alloc_flags & ALLOC_CPUSET) && | |
2870 | !__cpuset_zone_allowed(zone, gfp_mask)) | |
2871 | continue; | |
2872 | ||
a8e025e5 VB |
2873 | if (prio > MIN_COMPACT_PRIORITY |
2874 | && compaction_deferred(zone, order)) { | |
1d4746d3 | 2875 | rc = max_t(enum compact_result, COMPACT_DEFERRED, rc); |
53853e2d | 2876 | continue; |
1d4746d3 | 2877 | } |
53853e2d | 2878 | |
a5508cd8 | 2879 | status = compact_zone_order(zone, order, gfp_mask, prio, |
97a225e6 | 2880 | alloc_flags, ac->highest_zoneidx, capture); |
56de7263 MG |
2881 | rc = max(status, rc); |
2882 | ||
7ceb009a VB |
2883 | /* The allocation should succeed, stop compacting */ |
2884 | if (status == COMPACT_SUCCESS) { | |
53853e2d VB |
2885 | /* |
2886 | * We think the allocation will succeed in this zone, | |
2887 | * but it is not certain, hence the false. The caller | |
2888 | * will repeat this with true if allocation indeed | |
2889 | * succeeds in this zone. | |
2890 | */ | |
2891 | compaction_defer_reset(zone, order, false); | |
1f9efdef | 2892 | |
c3486f53 | 2893 | break; |
1f9efdef VB |
2894 | } |
2895 | ||
a5508cd8 | 2896 | if (prio != COMPACT_PRIO_ASYNC && (status == COMPACT_COMPLETE || |
c3486f53 | 2897 | status == COMPACT_PARTIAL_SKIPPED)) |
53853e2d VB |
2898 | /* |
2899 | * We think that allocation won't succeed in this zone | |
2900 | * so we defer compaction there. If it ends up | |
2901 | * succeeding after all, it will be reset. | |
2902 | */ | |
2903 | defer_compaction(zone, order); | |
1f9efdef VB |
2904 | |
2905 | /* | |
2906 | * We might have stopped compacting due to need_resched() in | |
2907 | * async compaction, or due to a fatal signal detected. In that | |
c3486f53 | 2908 | * case do not try further zones |
1f9efdef | 2909 | */ |
c3486f53 VB |
2910 | if ((prio == COMPACT_PRIO_ASYNC && need_resched()) |
2911 | || fatal_signal_pending(current)) | |
2912 | break; | |
56de7263 MG |
2913 | } |
2914 | ||
2915 | return rc; | |
2916 | } | |
2917 | ||
facdaa91 | 2918 | /* |
3e40b3f4 KW |
2919 | * compact_node() - compact all zones within a node |
2920 | * @pgdat: The node page data | |
2921 | * @proactive: Whether the compaction is proactive | |
facdaa91 | 2922 | * |
3e40b3f4 KW |
2923 | * For proactive compaction, compact till each zone's fragmentation score |
2924 | * reaches within proactive compaction thresholds (as determined by the | |
2925 | * proactiveness tunable), it is possible that the function returns before | |
2926 | * reaching score targets due to various back-off conditions, such as, | |
2927 | * contention on per-node or per-zone locks. | |
facdaa91 | 2928 | */ |
f6f3f275 | 2929 | static int compact_node(pg_data_t *pgdat, bool proactive) |
facdaa91 NG |
2930 | { |
2931 | int zoneid; | |
2932 | struct zone *zone; | |
2933 | struct compact_control cc = { | |
2934 | .order = -1, | |
3e40b3f4 | 2935 | .mode = proactive ? MIGRATE_SYNC_LIGHT : MIGRATE_SYNC, |
facdaa91 NG |
2936 | .ignore_skip_hint = true, |
2937 | .whole_zone = true, | |
2938 | .gfp_mask = GFP_KERNEL, | |
3e40b3f4 | 2939 | .proactive_compaction = proactive, |
facdaa91 NG |
2940 | }; |
2941 | ||
2942 | for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { | |
2943 | zone = &pgdat->node_zones[zoneid]; | |
2944 | if (!populated_zone(zone)) | |
2945 | continue; | |
2946 | ||
f6f3f275 KW |
2947 | if (fatal_signal_pending(current)) |
2948 | return -EINTR; | |
2949 | ||
facdaa91 NG |
2950 | cc.zone = zone; |
2951 | ||
2952 | compact_zone(&cc, NULL); | |
2953 | ||
3e40b3f4 KW |
2954 | if (proactive) { |
2955 | count_compact_events(KCOMPACTD_MIGRATE_SCANNED, | |
2956 | cc.total_migrate_scanned); | |
2957 | count_compact_events(KCOMPACTD_FREE_SCANNED, | |
2958 | cc.total_free_scanned); | |
2959 | } | |
76ab0f53 | 2960 | } |
f6f3f275 KW |
2961 | |
2962 | return 0; | |
76ab0f53 MG |
2963 | } |
2964 | ||
3e40b3f4 | 2965 | /* Compact all zones of all nodes in the system */ |
f6f3f275 | 2966 | static int compact_nodes(void) |
76ab0f53 | 2967 | { |
f6f3f275 | 2968 | int ret, nid; |
76ab0f53 | 2969 | |
8575ec29 HD |
2970 | /* Flush pending updates to the LRU lists */ |
2971 | lru_add_drain_all(); | |
2972 | ||
f6f3f275 KW |
2973 | for_each_online_node(nid) { |
2974 | ret = compact_node(NODE_DATA(nid), false); | |
2975 | if (ret) | |
2976 | return ret; | |
2977 | } | |
2978 | ||
2979 | return 0; | |
76ab0f53 MG |
2980 | } |
2981 | ||
78eb4ea2 | 2982 | static int compaction_proactiveness_sysctl_handler(const struct ctl_table *table, int write, |
65d759c8 CTR |
2983 | void *buffer, size_t *length, loff_t *ppos) |
2984 | { | |
2985 | int rc, nid; | |
2986 | ||
2987 | rc = proc_dointvec_minmax(table, write, buffer, length, ppos); | |
2988 | if (rc) | |
2989 | return rc; | |
2990 | ||
2991 | if (write && sysctl_compaction_proactiveness) { | |
2992 | for_each_online_node(nid) { | |
2993 | pg_data_t *pgdat = NODE_DATA(nid); | |
2994 | ||
2995 | if (pgdat->proactive_compact_trigger) | |
2996 | continue; | |
2997 | ||
2998 | pgdat->proactive_compact_trigger = true; | |
8fff8b6f BW |
2999 | trace_mm_compaction_wakeup_kcompactd(pgdat->node_id, -1, |
3000 | pgdat->nr_zones - 1); | |
65d759c8 CTR |
3001 | wake_up_interruptible(&pgdat->kcompactd_wait); |
3002 | } | |
3003 | } | |
3004 | ||
3005 | return 0; | |
3006 | } | |
3007 | ||
fec4eb2c YB |
3008 | /* |
3009 | * This is the entry point for compacting all nodes via | |
3010 | * /proc/sys/vm/compact_memory | |
3011 | */ | |
78eb4ea2 | 3012 | static int sysctl_compaction_handler(const struct ctl_table *table, int write, |
32927393 | 3013 | void *buffer, size_t *length, loff_t *ppos) |
76ab0f53 | 3014 | { |
8b9167cd WY |
3015 | int ret; |
3016 | ||
3017 | ret = proc_dointvec(table, write, buffer, length, ppos); | |
3018 | if (ret) | |
3019 | return ret; | |
3020 | ||
3021 | if (sysctl_compact_memory != 1) | |
3022 | return -EINVAL; | |
3023 | ||
76ab0f53 | 3024 | if (write) |
f6f3f275 | 3025 | ret = compact_nodes(); |
76ab0f53 | 3026 | |
f6f3f275 | 3027 | return ret; |
76ab0f53 | 3028 | } |
ed4a6d7f MG |
3029 | |
3030 | #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA) | |
17adb230 Y |
3031 | static ssize_t compact_store(struct device *dev, |
3032 | struct device_attribute *attr, | |
3033 | const char *buf, size_t count) | |
ed4a6d7f | 3034 | { |
8575ec29 HD |
3035 | int nid = dev->id; |
3036 | ||
3037 | if (nid >= 0 && nid < nr_node_ids && node_online(nid)) { | |
3038 | /* Flush pending updates to the LRU lists */ | |
3039 | lru_add_drain_all(); | |
3040 | ||
3e40b3f4 | 3041 | compact_node(NODE_DATA(nid), false); |
8575ec29 | 3042 | } |
ed4a6d7f MG |
3043 | |
3044 | return count; | |
3045 | } | |
17adb230 | 3046 | static DEVICE_ATTR_WO(compact); |
ed4a6d7f MG |
3047 | |
3048 | int compaction_register_node(struct node *node) | |
3049 | { | |
10fbcf4c | 3050 | return device_create_file(&node->dev, &dev_attr_compact); |
ed4a6d7f MG |
3051 | } |
3052 | ||
3053 | void compaction_unregister_node(struct node *node) | |
3054 | { | |
f82024cb | 3055 | device_remove_file(&node->dev, &dev_attr_compact); |
ed4a6d7f MG |
3056 | } |
3057 | #endif /* CONFIG_SYSFS && CONFIG_NUMA */ | |
ff9543fd | 3058 | |
698b1b30 VB |
3059 | static inline bool kcompactd_work_requested(pg_data_t *pgdat) |
3060 | { | |
65d759c8 CTR |
3061 | return pgdat->kcompactd_max_order > 0 || kthread_should_stop() || |
3062 | pgdat->proactive_compact_trigger; | |
698b1b30 VB |
3063 | } |
3064 | ||
3065 | static bool kcompactd_node_suitable(pg_data_t *pgdat) | |
3066 | { | |
3067 | int zoneid; | |
3068 | struct zone *zone; | |
97a225e6 | 3069 | enum zone_type highest_zoneidx = pgdat->kcompactd_highest_zoneidx; |
e19a3f59 | 3070 | enum compact_result ret; |
a211c655 JW |
3071 | unsigned int alloc_flags = defrag_mode ? |
3072 | ALLOC_WMARK_HIGH : ALLOC_WMARK_MIN; | |
698b1b30 | 3073 | |
97a225e6 | 3074 | for (zoneid = 0; zoneid <= highest_zoneidx; zoneid++) { |
698b1b30 VB |
3075 | zone = &pgdat->node_zones[zoneid]; |
3076 | ||
3077 | if (!populated_zone(zone)) | |
3078 | continue; | |
3079 | ||
e19a3f59 KS |
3080 | ret = compaction_suit_allocation_order(zone, |
3081 | pgdat->kcompactd_max_order, | |
a211c655 JW |
3082 | highest_zoneidx, alloc_flags, |
3083 | false, true); | |
e19a3f59 | 3084 | if (ret == COMPACT_CONTINUE) |
698b1b30 VB |
3085 | return true; |
3086 | } | |
3087 | ||
3088 | return false; | |
3089 | } | |
3090 | ||
3091 | static void kcompactd_do_work(pg_data_t *pgdat) | |
3092 | { | |
3093 | /* | |
3094 | * With no special task, compact all zones so that a page of requested | |
3095 | * order is allocatable. | |
3096 | */ | |
3097 | int zoneid; | |
3098 | struct zone *zone; | |
3099 | struct compact_control cc = { | |
3100 | .order = pgdat->kcompactd_max_order, | |
dbe2d4e4 | 3101 | .search_order = pgdat->kcompactd_max_order, |
97a225e6 | 3102 | .highest_zoneidx = pgdat->kcompactd_highest_zoneidx, |
698b1b30 | 3103 | .mode = MIGRATE_SYNC_LIGHT, |
a0647dc9 | 3104 | .ignore_skip_hint = false, |
73e64c51 | 3105 | .gfp_mask = GFP_KERNEL, |
a211c655 | 3106 | .alloc_flags = defrag_mode ? ALLOC_WMARK_HIGH : ALLOC_WMARK_MIN, |
698b1b30 | 3107 | }; |
e19a3f59 KS |
3108 | enum compact_result ret; |
3109 | ||
698b1b30 | 3110 | trace_mm_compaction_kcompactd_wake(pgdat->node_id, cc.order, |
97a225e6 | 3111 | cc.highest_zoneidx); |
7f354a54 | 3112 | count_compact_event(KCOMPACTD_WAKE); |
698b1b30 | 3113 | |
97a225e6 | 3114 | for (zoneid = 0; zoneid <= cc.highest_zoneidx; zoneid++) { |
698b1b30 VB |
3115 | int status; |
3116 | ||
3117 | zone = &pgdat->node_zones[zoneid]; | |
3118 | if (!populated_zone(zone)) | |
3119 | continue; | |
3120 | ||
3121 | if (compaction_deferred(zone, cc.order)) | |
3122 | continue; | |
3123 | ||
e19a3f59 | 3124 | ret = compaction_suit_allocation_order(zone, |
67914ac0 | 3125 | cc.order, zoneid, cc.alloc_flags, |
a211c655 | 3126 | false, true); |
e19a3f59 | 3127 | if (ret != COMPACT_CONTINUE) |
e8606320 | 3128 | continue; |
f98a497e | 3129 | |
172400c6 VB |
3130 | if (kthread_should_stop()) |
3131 | return; | |
a94b5252 YS |
3132 | |
3133 | cc.zone = zone; | |
5e1f0f09 | 3134 | status = compact_zone(&cc, NULL); |
698b1b30 | 3135 | |
7ceb009a | 3136 | if (status == COMPACT_SUCCESS) { |
698b1b30 | 3137 | compaction_defer_reset(zone, cc.order, false); |
c8f7de0b | 3138 | } else if (status == COMPACT_PARTIAL_SKIPPED || status == COMPACT_COMPLETE) { |
bc3106b2 DR |
3139 | /* |
3140 | * Buddy pages may become stranded on pcps that could | |
3141 | * otherwise coalesce on the zone's free area for | |
3142 | * order >= cc.order. This is ratelimited by the | |
3143 | * upcoming deferral. | |
3144 | */ | |
3145 | drain_all_pages(zone); | |
3146 | ||
698b1b30 VB |
3147 | /* |
3148 | * We use sync migration mode here, so we defer like | |
3149 | * sync direct compaction does. | |
3150 | */ | |
3151 | defer_compaction(zone, cc.order); | |
3152 | } | |
3153 | ||
7f354a54 DR |
3154 | count_compact_events(KCOMPACTD_MIGRATE_SCANNED, |
3155 | cc.total_migrate_scanned); | |
3156 | count_compact_events(KCOMPACTD_FREE_SCANNED, | |
3157 | cc.total_free_scanned); | |
698b1b30 VB |
3158 | } |
3159 | ||
3160 | /* | |
3161 | * Regardless of success, we are done until woken up next. But remember | |
97a225e6 JK |
3162 | * the requested order/highest_zoneidx in case it was higher/tighter |
3163 | * than our current ones | |
698b1b30 VB |
3164 | */ |
3165 | if (pgdat->kcompactd_max_order <= cc.order) | |
3166 | pgdat->kcompactd_max_order = 0; | |
97a225e6 JK |
3167 | if (pgdat->kcompactd_highest_zoneidx >= cc.highest_zoneidx) |
3168 | pgdat->kcompactd_highest_zoneidx = pgdat->nr_zones - 1; | |
698b1b30 VB |
3169 | } |
3170 | ||
97a225e6 | 3171 | void wakeup_kcompactd(pg_data_t *pgdat, int order, int highest_zoneidx) |
698b1b30 VB |
3172 | { |
3173 | if (!order) | |
3174 | return; | |
3175 | ||
3176 | if (pgdat->kcompactd_max_order < order) | |
3177 | pgdat->kcompactd_max_order = order; | |
3178 | ||
97a225e6 JK |
3179 | if (pgdat->kcompactd_highest_zoneidx > highest_zoneidx) |
3180 | pgdat->kcompactd_highest_zoneidx = highest_zoneidx; | |
698b1b30 | 3181 | |
6818600f DB |
3182 | /* |
3183 | * Pairs with implicit barrier in wait_event_freezable() | |
3184 | * such that wakeups are not missed. | |
3185 | */ | |
3186 | if (!wq_has_sleeper(&pgdat->kcompactd_wait)) | |
698b1b30 VB |
3187 | return; |
3188 | ||
3189 | if (!kcompactd_node_suitable(pgdat)) | |
3190 | return; | |
3191 | ||
3192 | trace_mm_compaction_wakeup_kcompactd(pgdat->node_id, order, | |
97a225e6 | 3193 | highest_zoneidx); |
698b1b30 VB |
3194 | wake_up_interruptible(&pgdat->kcompactd_wait); |
3195 | } | |
3196 | ||
3197 | /* | |
3198 | * The background compaction daemon, started as a kernel thread | |
3199 | * from the init process. | |
3200 | */ | |
3201 | static int kcompactd(void *p) | |
3202 | { | |
68d68ff6 | 3203 | pg_data_t *pgdat = (pg_data_t *)p; |
e1e92bfa CTR |
3204 | long default_timeout = msecs_to_jiffies(HPAGE_FRAG_CHECK_INTERVAL_MSEC); |
3205 | long timeout = default_timeout; | |
698b1b30 | 3206 | |
ce6d9c1c | 3207 | current->flags |= PF_KCOMPACTD; |
698b1b30 VB |
3208 | set_freezable(); |
3209 | ||
3210 | pgdat->kcompactd_max_order = 0; | |
97a225e6 | 3211 | pgdat->kcompactd_highest_zoneidx = pgdat->nr_zones - 1; |
698b1b30 VB |
3212 | |
3213 | while (!kthread_should_stop()) { | |
eb414681 JW |
3214 | unsigned long pflags; |
3215 | ||
65d759c8 CTR |
3216 | /* |
3217 | * Avoid the unnecessary wakeup for proactive compaction | |
3218 | * when it is disabled. | |
3219 | */ | |
3220 | if (!sysctl_compaction_proactiveness) | |
3221 | timeout = MAX_SCHEDULE_TIMEOUT; | |
698b1b30 | 3222 | trace_mm_compaction_kcompactd_sleep(pgdat->node_id); |
facdaa91 | 3223 | if (wait_event_freezable_timeout(pgdat->kcompactd_wait, |
65d759c8 CTR |
3224 | kcompactd_work_requested(pgdat), timeout) && |
3225 | !pgdat->proactive_compact_trigger) { | |
facdaa91 NG |
3226 | |
3227 | psi_memstall_enter(&pflags); | |
3228 | kcompactd_do_work(pgdat); | |
3229 | psi_memstall_leave(&pflags); | |
e1e92bfa CTR |
3230 | /* |
3231 | * Reset the timeout value. The defer timeout from | |
3232 | * proactive compaction is lost here but that is fine | |
3233 | * as the condition of the zone changing substantionally | |
3234 | * then carrying on with the previous defer interval is | |
3235 | * not useful. | |
3236 | */ | |
3237 | timeout = default_timeout; | |
facdaa91 NG |
3238 | continue; |
3239 | } | |
698b1b30 | 3240 | |
e1e92bfa CTR |
3241 | /* |
3242 | * Start the proactive work with default timeout. Based | |
3243 | * on the fragmentation score, this timeout is updated. | |
3244 | */ | |
3245 | timeout = default_timeout; | |
facdaa91 NG |
3246 | if (should_proactive_compact_node(pgdat)) { |
3247 | unsigned int prev_score, score; | |
3248 | ||
facdaa91 | 3249 | prev_score = fragmentation_score_node(pgdat); |
3e40b3f4 | 3250 | compact_node(pgdat, true); |
facdaa91 NG |
3251 | score = fragmentation_score_node(pgdat); |
3252 | /* | |
3253 | * Defer proactive compaction if the fragmentation | |
3254 | * score did not go down i.e. no progress made. | |
3255 | */ | |
e1e92bfa CTR |
3256 | if (unlikely(score >= prev_score)) |
3257 | timeout = | |
3258 | default_timeout << COMPACT_MAX_DEFER_SHIFT; | |
facdaa91 | 3259 | } |
65d759c8 CTR |
3260 | if (unlikely(pgdat->proactive_compact_trigger)) |
3261 | pgdat->proactive_compact_trigger = false; | |
698b1b30 VB |
3262 | } |
3263 | ||
ce6d9c1c MS |
3264 | current->flags &= ~PF_KCOMPACTD; |
3265 | ||
698b1b30 VB |
3266 | return 0; |
3267 | } | |
3268 | ||
3269 | /* | |
3270 | * This kcompactd start function will be called by init and node-hot-add. | |
3271 | * On node-hot-add, kcompactd will moved to proper cpus if cpus are hot-added. | |
3272 | */ | |
833dfc00 | 3273 | void __meminit kcompactd_run(int nid) |
698b1b30 VB |
3274 | { |
3275 | pg_data_t *pgdat = NODE_DATA(nid); | |
698b1b30 VB |
3276 | |
3277 | if (pgdat->kcompactd) | |
024c61ea | 3278 | return; |
698b1b30 | 3279 | |
54880b5a | 3280 | pgdat->kcompactd = kthread_create_on_node(kcompactd, pgdat, nid, "kcompactd%d", nid); |
698b1b30 VB |
3281 | if (IS_ERR(pgdat->kcompactd)) { |
3282 | pr_err("Failed to start kcompactd on node %d\n", nid); | |
698b1b30 | 3283 | pgdat->kcompactd = NULL; |
54880b5a FW |
3284 | } else { |
3285 | wake_up_process(pgdat->kcompactd); | |
698b1b30 | 3286 | } |
698b1b30 VB |
3287 | } |
3288 | ||
3289 | /* | |
3290 | * Called by memory hotplug when all memory in a node is offlined. Caller must | |
e8da368a | 3291 | * be holding mem_hotplug_begin/done(). |
698b1b30 | 3292 | */ |
833dfc00 | 3293 | void __meminit kcompactd_stop(int nid) |
698b1b30 VB |
3294 | { |
3295 | struct task_struct *kcompactd = NODE_DATA(nid)->kcompactd; | |
3296 | ||
3297 | if (kcompactd) { | |
3298 | kthread_stop(kcompactd); | |
3299 | NODE_DATA(nid)->kcompactd = NULL; | |
3300 | } | |
3301 | } | |
3302 | ||
78eb4ea2 | 3303 | static int proc_dointvec_minmax_warn_RT_change(const struct ctl_table *table, |
48fe8ab8 MC |
3304 | int write, void *buffer, size_t *lenp, loff_t *ppos) |
3305 | { | |
3306 | int ret, old; | |
3307 | ||
3308 | if (!IS_ENABLED(CONFIG_PREEMPT_RT) || !write) | |
3309 | return proc_dointvec_minmax(table, write, buffer, lenp, ppos); | |
3310 | ||
3311 | old = *(int *)table->data; | |
3312 | ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); | |
3313 | if (ret) | |
3314 | return ret; | |
3315 | if (old != *(int *)table->data) | |
3316 | pr_warn_once("sysctl attribute %s changed by %s[%d]\n", | |
3317 | table->procname, current->comm, | |
3318 | task_pid_nr(current)); | |
3319 | return ret; | |
3320 | } | |
3321 | ||
1751f872 | 3322 | static const struct ctl_table vm_compaction[] = { |
48fe8ab8 MC |
3323 | { |
3324 | .procname = "compact_memory", | |
8b9167cd | 3325 | .data = &sysctl_compact_memory, |
48fe8ab8 MC |
3326 | .maxlen = sizeof(int), |
3327 | .mode = 0200, | |
3328 | .proc_handler = sysctl_compaction_handler, | |
3329 | }, | |
3330 | { | |
3331 | .procname = "compaction_proactiveness", | |
3332 | .data = &sysctl_compaction_proactiveness, | |
3333 | .maxlen = sizeof(sysctl_compaction_proactiveness), | |
3334 | .mode = 0644, | |
3335 | .proc_handler = compaction_proactiveness_sysctl_handler, | |
3336 | .extra1 = SYSCTL_ZERO, | |
3337 | .extra2 = SYSCTL_ONE_HUNDRED, | |
3338 | }, | |
3339 | { | |
3340 | .procname = "extfrag_threshold", | |
3341 | .data = &sysctl_extfrag_threshold, | |
3342 | .maxlen = sizeof(int), | |
3343 | .mode = 0644, | |
3344 | .proc_handler = proc_dointvec_minmax, | |
3345 | .extra1 = SYSCTL_ZERO, | |
3346 | .extra2 = SYSCTL_ONE_THOUSAND, | |
3347 | }, | |
3348 | { | |
3349 | .procname = "compact_unevictable_allowed", | |
3350 | .data = &sysctl_compact_unevictable_allowed, | |
3351 | .maxlen = sizeof(int), | |
3352 | .mode = 0644, | |
3353 | .proc_handler = proc_dointvec_minmax_warn_RT_change, | |
3354 | .extra1 = SYSCTL_ZERO, | |
3355 | .extra2 = SYSCTL_ONE, | |
3356 | }, | |
48fe8ab8 | 3357 | }; |
48fe8ab8 | 3358 | |
698b1b30 VB |
3359 | static int __init kcompactd_init(void) |
3360 | { | |
3361 | int nid; | |
3362 | ||
3363 | for_each_node_state(nid, N_MEMORY) | |
3364 | kcompactd_run(nid); | |
48fe8ab8 | 3365 | register_sysctl_init("vm", vm_compaction); |
698b1b30 VB |
3366 | return 0; |
3367 | } | |
3368 | subsys_initcall(kcompactd_init) | |
3369 | ||
ff9543fd | 3370 | #endif /* CONFIG_COMPACTION */ |