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