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