blob: 0f082284f245d3019e27f02839d492d71f382def [file] [log] [blame]
Mel Gorman748446b2010-05-24 14:32:27 -07001/*
2 * linux/mm/compaction.c
3 *
4 * Memory compaction for the reduction of external fragmentation. Note that
5 * this heavily depends upon page migration to do all the real heavy
6 * lifting
7 *
8 * Copyright IBM Corp. 2007-2010 Mel Gorman <mel@csn.ul.ie>
9 */
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>
Mel Gorman76ab0f52010-05-24 14:32:28 -070015#include <linux/sysctl.h>
Mel Gormaned4a6d72010-05-24 14:32:29 -070016#include <linux/sysfs.h>
Mel Gorman748446b2010-05-24 14:32:27 -070017#include "internal.h"
18
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +010019#if defined CONFIG_COMPACTION || defined CONFIG_CMA
20
Mel Gormanb7aba692011-01-13 15:45:54 -080021#define CREATE_TRACE_POINTS
22#include <trace/events/compaction.h>
23
Mel Gorman748446b2010-05-24 14:32:27 -070024static 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
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +010038static 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
Michal Nazarewiczd4158d22011-12-29 13:09:50 +010048static inline bool migrate_async_suitable(int migratetype)
49{
50 return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
51}
52
Mel Gorman6a38cba2012-10-08 16:32:33 -070053static inline bool should_release_lock(spinlock_t *lock)
54{
55 return need_resched() || spin_is_contended(lock);
56}
57
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +010058/*
Mel Gorman0ba33882012-08-21 16:16:17 -070059 * 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 */
67static bool compact_checklock_irqsave(spinlock_t *lock, unsigned long *flags,
68 bool locked, struct compact_control *cc)
69{
Mel Gorman6a38cba2012-10-08 16:32:33 -070070 if (should_release_lock(lock)) {
Mel Gorman0ba33882012-08-21 16:16:17 -070071 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) {
Shaohua Li03dd8fe2012-10-08 16:32:27 -070078 cc->contended = true;
Mel Gorman0ba33882012-08-21 16:16:17 -070079 return false;
80 }
81
82 cond_resched();
Mel Gorman0ba33882012-08-21 16:16:17 -070083 }
84
85 if (!locked)
86 spin_lock_irqsave(lock, *flags);
87 return true;
88}
89
90static 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
Mel Gormand6ed9722012-10-08 16:32:36 -070096/* Returns true if the page is within a block suitable for migration to */
97static 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
Mel Gormanc74068b2012-10-08 16:29:12 -0700117static 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
Mel Gorman0ba33882012-08-21 16:16:17 -0700171/*
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100172 * 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 */
Mel Gormand6ed9722012-10-08 16:32:36 -0700177static unsigned long isolate_freepages_block(struct compact_control *cc,
178 unsigned long blockpfn,
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100179 unsigned long end_pfn,
180 struct list_head *freelist,
181 bool strict)
Mel Gorman748446b2010-05-24 14:32:27 -0700182{
Mel Gormanb7aba692011-01-13 15:45:54 -0800183 int nr_scanned = 0, total_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700184 struct page *cursor;
Mel Gormand6ed9722012-10-08 16:32:36 -0700185 unsigned long nr_strict_required = end_pfn - blockpfn;
186 unsigned long flags;
187 bool locked = false;
Mel Gorman748446b2010-05-24 14:32:27 -0700188
Mel Gorman748446b2010-05-24 14:32:27 -0700189 cursor = pfn_to_page(blockpfn);
190
Mel Gormand6ed9722012-10-08 16:32:36 -0700191 /* Isolate free pages. */
Mel Gorman748446b2010-05-24 14:32:27 -0700192 for (; blockpfn < end_pfn; blockpfn++, cursor++) {
193 int isolated, i;
194 struct page *page = cursor;
195
Mel Gormanb7aba692011-01-13 15:45:54 -0800196 nr_scanned++;
Mel Gormand6ed9722012-10-08 16:32:36 -0700197 if (!pfn_valid_within(blockpfn))
Mel Gorman748446b2010-05-24 14:32:27 -0700198 continue;
Mel Gormand6ed9722012-10-08 16:32:36 -0700199 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;
218
219 /* Recheck this is a buddy page under lock */
220 if (!PageBuddy(page))
221 continue;
Mel Gorman748446b2010-05-24 14:32:27 -0700222
223 /* Found a free page, break it into order-0 pages */
224 isolated = split_free_page(page);
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100225 if (!isolated && strict)
Mel Gormand6ed9722012-10-08 16:32:36 -0700226 break;
Mel Gorman748446b2010-05-24 14:32:27 -0700227 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
Mel Gormanb7aba692011-01-13 15:45:54 -0800240 trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
Mel Gormand6ed9722012-10-08 16:32:36 -0700241
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
Mel Gorman748446b2010-05-24 14:32:27 -0700253 return total_isolated;
254}
255
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100256/**
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 */
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100269unsigned long
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100270isolate_freepages_range(unsigned long start_pfn, unsigned long end_pfn)
271{
Mel Gormand6ed9722012-10-08 16:32:36 -0700272 unsigned long isolated, pfn, block_end_pfn;
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100273 struct zone *zone = NULL;
274 LIST_HEAD(freelist);
275
Mel Gormand6ed9722012-10-08 16:32:36 -0700276 /* cc needed for isolate_freepages_block to acquire zone->lock */
277 struct compact_control cc = {
278 .sync = true,
279 };
280
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100281 if (pfn_valid(start_pfn))
Mel Gormand6ed9722012-10-08 16:32:36 -0700282 cc.zone = zone = page_zone(pfn_to_page(start_pfn));
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100283
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
Mel Gormand6ed9722012-10-08 16:32:36 -0700295 isolated = isolate_freepages_block(&cc, pfn, block_end_pfn,
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100296 &freelist, true);
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100297
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
Mel Gorman748446b2010-05-24 14:32:27 -0700326/* Update the number of anon and file isolated pages in the zone */
Mel Gorman0ba33882012-08-21 16:16:17 -0700327static void acct_isolated(struct zone *zone, bool locked, struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700328{
329 struct page *page;
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700330 unsigned int count[2] = { 0, };
Mel Gorman748446b2010-05-24 14:32:27 -0700331
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700332 list_for_each_entry(page, &cc->migratepages, lru)
333 count[!!page_is_file_cache(page)]++;
Mel Gorman748446b2010-05-24 14:32:27 -0700334
Mel Gorman0ba33882012-08-21 16:16:17 -0700335 /* 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 }
Mel Gorman748446b2010-05-24 14:32:27 -0700343}
344
345/* Similar to reclaim, but different enough that they don't share logic */
346static bool too_many_isolated(struct zone *zone)
347{
Minchan Kimbc693042010-09-09 16:38:00 -0700348 unsigned long active, inactive, isolated;
Mel Gorman748446b2010-05-24 14:32:27 -0700349
350 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
351 zone_page_state(zone, NR_INACTIVE_ANON);
Minchan Kimbc693042010-09-09 16:38:00 -0700352 active = zone_page_state(zone, NR_ACTIVE_FILE) +
353 zone_page_state(zone, NR_ACTIVE_ANON);
Mel Gorman748446b2010-05-24 14:32:27 -0700354 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
355 zone_page_state(zone, NR_ISOLATED_ANON);
356
Minchan Kimbc693042010-09-09 16:38:00 -0700357 return isolated > (inactive + active) / 2;
Mel Gorman748446b2010-05-24 14:32:27 -0700358}
359
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100360/**
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.
Mel Gorman748446b2010-05-24 14:32:27 -0700378 */
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100379unsigned long
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100380isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
381 unsigned long low_pfn, unsigned long end_pfn)
Mel Gorman748446b2010-05-24 14:32:27 -0700382{
Mel Gorman9927af742011-01-13 15:45:59 -0800383 unsigned long last_pageblock_nr = 0, pageblock_nr;
Mel Gormanb7aba692011-01-13 15:45:54 -0800384 unsigned long nr_scanned = 0, nr_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700385 struct list_head *migratelist = &cc->migratepages;
Konstantin Khlebnikovfa168092012-05-29 15:06:54 -0700386 isolate_mode_t mode = 0;
Mel Gorman0ba33882012-08-21 16:16:17 -0700387 unsigned long flags;
Mel Gorman6a38cba2012-10-08 16:32:33 -0700388 bool locked = false;
Mel Gorman748446b2010-05-24 14:32:27 -0700389
Mel Gorman748446b2010-05-24 14:32:27 -0700390 /*
391 * Ensure that there are not too many pages isolated from the LRU
392 * list by either parallel reclaimers or compaction. If there are,
393 * delay for some time until fewer pages are isolated
394 */
395 while (unlikely(too_many_isolated(zone))) {
Mel Gormanf9e35b32011-06-15 15:08:52 -0700396 /* async migration should just abort */
397 if (!cc->sync)
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100398 return 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700399
Mel Gorman748446b2010-05-24 14:32:27 -0700400 congestion_wait(BLK_RW_ASYNC, HZ/10);
401
402 if (fatal_signal_pending(current))
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100403 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700404 }
405
406 /* Time to isolate some pages for migration */
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700407 cond_resched();
Mel Gorman748446b2010-05-24 14:32:27 -0700408 for (; low_pfn < end_pfn; low_pfn++) {
409 struct page *page;
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700410
411 /* give a chance to irqs before checking need_resched() */
Mel Gorman6a38cba2012-10-08 16:32:33 -0700412 if (locked && !((low_pfn+1) % SWAP_CLUSTER_MAX)) {
413 if (should_release_lock(&zone->lru_lock)) {
414 spin_unlock_irqrestore(&zone->lru_lock, flags);
415 locked = false;
416 }
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700417 }
Mel Gorman0ba33882012-08-21 16:16:17 -0700418
Mel Gorman0bf380b2012-02-03 15:37:18 -0800419 /*
420 * migrate_pfn does not necessarily start aligned to a
421 * pageblock. Ensure that pfn_valid is called when moving
422 * into a new MAX_ORDER_NR_PAGES range in case of large
423 * memory holes within the zone
424 */
425 if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) {
426 if (!pfn_valid(low_pfn)) {
427 low_pfn += MAX_ORDER_NR_PAGES - 1;
428 continue;
429 }
430 }
431
Mel Gorman748446b2010-05-24 14:32:27 -0700432 if (!pfn_valid_within(low_pfn))
433 continue;
Mel Gormanb7aba692011-01-13 15:45:54 -0800434 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700435
Mel Gormandc908602012-02-08 17:13:38 -0800436 /*
437 * Get the page and ensure the page is within the same zone.
438 * See the comment in isolate_freepages about overlapping
439 * nodes. It is deliberate that the new zone lock is not taken
440 * as memory compaction should not move pages between nodes.
441 */
Mel Gorman748446b2010-05-24 14:32:27 -0700442 page = pfn_to_page(low_pfn);
Mel Gormandc908602012-02-08 17:13:38 -0800443 if (page_zone(page) != zone)
444 continue;
445
446 /* Skip if free */
Mel Gorman748446b2010-05-24 14:32:27 -0700447 if (PageBuddy(page))
448 continue;
449
Mel Gorman9927af742011-01-13 15:45:59 -0800450 /*
451 * For async migration, also only scan in MOVABLE blocks. Async
452 * migration is optimistic to see if the minimum amount of work
453 * satisfies the allocation
454 */
455 pageblock_nr = low_pfn >> pageblock_order;
456 if (!cc->sync && last_pageblock_nr != pageblock_nr &&
Michal Nazarewiczd4158d22011-12-29 13:09:50 +0100457 !migrate_async_suitable(get_pageblock_migratetype(page))) {
Mel Gorman6a38cba2012-10-08 16:32:33 -0700458 goto next_pageblock;
Mel Gorman9927af742011-01-13 15:45:59 -0800459 }
460
Mel Gorman6a38cba2012-10-08 16:32:33 -0700461 /* Check may be lockless but that's ok as we recheck later */
Andrea Arcangelibc835012011-01-13 15:47:08 -0800462 if (!PageLRU(page))
463 continue;
464
465 /*
Mel Gorman6a38cba2012-10-08 16:32:33 -0700466 * PageLRU is set. lru_lock normally excludes isolation
467 * splitting and collapsing (collapsing has already happened
468 * if PageLRU is set) but the lock is not necessarily taken
469 * here and it is wasteful to take it just to check transhuge.
470 * Check TransHuge without lock and skip the whole pageblock if
471 * it's either a transhuge or hugetlbfs page, as calling
472 * compound_order() without preventing THP from splitting the
473 * page underneath us may return surprising results.
Andrea Arcangelibc835012011-01-13 15:47:08 -0800474 */
475 if (PageTransHuge(page)) {
Mel Gorman6a38cba2012-10-08 16:32:33 -0700476 if (!locked)
477 goto next_pageblock;
478 low_pfn += (1 << compound_order(page)) - 1;
479 continue;
480 }
481
482 /* Check if it is ok to still hold the lock */
483 locked = compact_checklock_irqsave(&zone->lru_lock, &flags,
484 locked, cc);
485 if (!locked || fatal_signal_pending(current))
486 break;
487
488 /* Recheck PageLRU and PageTransHuge under lock */
489 if (!PageLRU(page))
490 continue;
491 if (PageTransHuge(page)) {
Andrea Arcangelibc835012011-01-13 15:47:08 -0800492 low_pfn += (1 << compound_order(page)) - 1;
493 continue;
494 }
495
Mel Gormanc8244932012-01-12 17:19:38 -0800496 if (!cc->sync)
497 mode |= ISOLATE_ASYNC_MIGRATE;
498
Mel Gorman748446b2010-05-24 14:32:27 -0700499 /* Try isolate the page */
Konstantin Khlebnikovfa168092012-05-29 15:06:54 -0700500 if (__isolate_lru_page(page, mode) != 0)
Mel Gorman748446b2010-05-24 14:32:27 -0700501 continue;
502
Andrea Arcangelibc835012011-01-13 15:47:08 -0800503 VM_BUG_ON(PageTransCompound(page));
504
Mel Gorman748446b2010-05-24 14:32:27 -0700505 /* Successfully isolated */
506 del_page_from_lru_list(zone, page, page_lru(page));
507 list_add(&page->lru, migratelist);
Mel Gorman748446b2010-05-24 14:32:27 -0700508 cc->nr_migratepages++;
Mel Gormanb7aba692011-01-13 15:45:54 -0800509 nr_isolated++;
Mel Gorman748446b2010-05-24 14:32:27 -0700510
511 /* Avoid isolating too much */
Hillf Danton31b83842012-01-10 15:07:59 -0800512 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
513 ++low_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700514 break;
Hillf Danton31b83842012-01-10 15:07:59 -0800515 }
Mel Gorman6a38cba2012-10-08 16:32:33 -0700516
517 continue;
518
519next_pageblock:
520 low_pfn += pageblock_nr_pages;
521 low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1;
522 last_pageblock_nr = pageblock_nr;
Mel Gorman748446b2010-05-24 14:32:27 -0700523 }
524
Mel Gorman0ba33882012-08-21 16:16:17 -0700525 acct_isolated(zone, locked, cc);
Mel Gorman748446b2010-05-24 14:32:27 -0700526
Mel Gorman0ba33882012-08-21 16:16:17 -0700527 if (locked)
528 spin_unlock_irqrestore(&zone->lru_lock, flags);
Mel Gorman748446b2010-05-24 14:32:27 -0700529
Mel Gormanb7aba692011-01-13 15:45:54 -0800530 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
531
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100532 return low_pfn;
533}
534
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100535#endif /* CONFIG_COMPACTION || CONFIG_CMA */
536#ifdef CONFIG_COMPACTION
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100537/*
538 * Based on information in the current compact_control, find blocks
539 * suitable for isolating free pages from and then isolate them.
540 */
541static void isolate_freepages(struct zone *zone,
542 struct compact_control *cc)
543{
544 struct page *page;
545 unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn;
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100546 int nr_freepages = cc->nr_freepages;
547 struct list_head *freelist = &cc->freepages;
548
549 /*
550 * Initialise the free scanner. The starting point is where we last
551 * scanned from (or the end of the zone if starting). The low point
552 * is the end of the pageblock the migration scanner is using.
553 */
554 pfn = cc->free_pfn;
555 low_pfn = cc->migrate_pfn + pageblock_nr_pages;
556
557 /*
558 * Take care that if the migration scanner is at the end of the zone
559 * that the free scanner does not accidentally move to the next zone
560 * in the next isolation cycle.
561 */
562 high_pfn = min(low_pfn, pfn);
563
564 zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
565
566 /*
567 * Isolate free pages until enough are available to migrate the
568 * pages on cc->migratepages. We stop searching if the migrate
569 * and free page scanners meet or enough free pages are isolated.
570 */
571 for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
572 pfn -= pageblock_nr_pages) {
573 unsigned long isolated;
574
575 if (!pfn_valid(pfn))
576 continue;
577
578 /*
579 * Check for overlapping nodes/zones. It's possible on some
580 * configurations to have a setup like
581 * node0 node1 node0
582 * i.e. it's possible that all pages within a zones range of
583 * pages do not belong to a single zone.
584 */
585 page = pfn_to_page(pfn);
586 if (page_zone(page) != zone)
587 continue;
588
589 /* Check the block is suitable for migration */
590 if (!suitable_migration_target(page))
591 continue;
592
Mel Gormand6ed9722012-10-08 16:32:36 -0700593 /* Found a block suitable for isolating free pages from */
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100594 isolated = 0;
Mel Gormand6ed9722012-10-08 16:32:36 -0700595 end_pfn = min(pfn + pageblock_nr_pages, zone_end_pfn);
596 isolated = isolate_freepages_block(cc, pfn, end_pfn,
597 freelist, false);
598 nr_freepages += isolated;
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100599
600 /*
601 * Record the highest PFN we isolated pages from. When next
602 * looking for free pages, the search will restart here as
603 * page migration may have returned some pages to the allocator
604 */
605 if (isolated)
606 high_pfn = max(high_pfn, pfn);
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100607 }
608
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100609 /* split_free_page does not map the pages */
610 map_pages(freelist);
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100611
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100612 cc->free_pfn = high_pfn;
613 cc->nr_freepages = nr_freepages;
Mel Gorman748446b2010-05-24 14:32:27 -0700614}
615
616/*
617 * This is a migrate-callback that "allocates" freepages by taking pages
618 * from the isolated freelists in the block we are migrating to.
619 */
620static struct page *compaction_alloc(struct page *migratepage,
621 unsigned long data,
622 int **result)
623{
624 struct compact_control *cc = (struct compact_control *)data;
625 struct page *freepage;
626
627 /* Isolate free pages if necessary */
628 if (list_empty(&cc->freepages)) {
629 isolate_freepages(cc->zone, cc);
630
631 if (list_empty(&cc->freepages))
632 return NULL;
633 }
634
635 freepage = list_entry(cc->freepages.next, struct page, lru);
636 list_del(&freepage->lru);
637 cc->nr_freepages--;
638
639 return freepage;
640}
641
642/*
643 * We cannot control nr_migratepages and nr_freepages fully when migration is
644 * running as migrate_pages() has no knowledge of compact_control. When
645 * migration is complete, we count the number of pages on the lists by hand.
646 */
647static void update_nr_listpages(struct compact_control *cc)
648{
649 int nr_migratepages = 0;
650 int nr_freepages = 0;
651 struct page *page;
652
653 list_for_each_entry(page, &cc->migratepages, lru)
654 nr_migratepages++;
655 list_for_each_entry(page, &cc->freepages, lru)
656 nr_freepages++;
657
658 cc->nr_migratepages = nr_migratepages;
659 cc->nr_freepages = nr_freepages;
660}
661
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100662/* possible outcome of isolate_migratepages */
663typedef enum {
664 ISOLATE_ABORT, /* Abort compaction now */
665 ISOLATE_NONE, /* No pages isolated, continue scanning */
666 ISOLATE_SUCCESS, /* Pages isolated, migrate */
667} isolate_migrate_t;
668
669/*
670 * Isolate all pages that can be migrated from the block pointed to by
671 * the migrate scanner within compact_control.
672 */
673static isolate_migrate_t isolate_migratepages(struct zone *zone,
674 struct compact_control *cc)
675{
676 unsigned long low_pfn, end_pfn;
677
678 /* Do not scan outside zone boundaries */
679 low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
680
681 /* Only scan within a pageblock boundary */
682 end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
683
684 /* Do not cross the free scanner or scan within a memory hole */
685 if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
686 cc->migrate_pfn = end_pfn;
687 return ISOLATE_NONE;
688 }
689
690 /* Perform the isolation */
691 low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn);
Shaohua Li03dd8fe2012-10-08 16:32:27 -0700692 if (!low_pfn || cc->contended)
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100693 return ISOLATE_ABORT;
694
695 cc->migrate_pfn = low_pfn;
696
697 return ISOLATE_SUCCESS;
698}
699
Mel Gorman748446b2010-05-24 14:32:27 -0700700static int compact_finished(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800701 struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700702{
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800703 unsigned long watermark;
Mel Gorman56de7262010-05-24 14:32:30 -0700704
Mel Gorman748446b2010-05-24 14:32:27 -0700705 if (fatal_signal_pending(current))
706 return COMPACT_PARTIAL;
707
708 /* Compaction run completes if the migrate and free scanner meet */
709 if (cc->free_pfn <= cc->migrate_pfn)
710 return COMPACT_COMPLETE;
711
Johannes Weiner82478fb2011-01-20 14:44:21 -0800712 /*
713 * order == -1 is expected when compacting via
714 * /proc/sys/vm/compact_memory
715 */
Mel Gorman56de7262010-05-24 14:32:30 -0700716 if (cc->order == -1)
717 return COMPACT_CONTINUE;
718
Michal Hocko3957c772011-06-15 15:08:25 -0700719 /* Compaction run is not finished if the watermark is not met */
720 watermark = low_wmark_pages(zone);
721 watermark += (1 << cc->order);
722
723 if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
724 return COMPACT_CONTINUE;
725
Mel Gorman56de7262010-05-24 14:32:30 -0700726 /* Direct compactor: Is a suitable page free? */
Mel Gormanc74068b2012-10-08 16:29:12 -0700727 if (cc->page) {
728 /* Was a suitable page captured? */
729 if (*cc->page)
Mel Gorman56de7262010-05-24 14:32:30 -0700730 return COMPACT_PARTIAL;
Mel Gormanc74068b2012-10-08 16:29:12 -0700731 } else {
732 unsigned int order;
733 for (order = cc->order; order < MAX_ORDER; order++) {
734 struct free_area *area = &zone->free_area[cc->order];
735 /* Job done if page is free of the right migratetype */
736 if (!list_empty(&area->free_list[cc->migratetype]))
737 return COMPACT_PARTIAL;
Mel Gorman56de7262010-05-24 14:32:30 -0700738
Mel Gormanc74068b2012-10-08 16:29:12 -0700739 /* Job done if allocation would set block type */
740 if (cc->order >= pageblock_order && area->nr_free)
741 return COMPACT_PARTIAL;
742 }
Mel Gorman56de7262010-05-24 14:32:30 -0700743 }
744
Mel Gorman748446b2010-05-24 14:32:27 -0700745 return COMPACT_CONTINUE;
746}
747
Mel Gorman3e7d3442011-01-13 15:45:56 -0800748/*
749 * compaction_suitable: Is this suitable to run compaction on this zone now?
750 * Returns
751 * COMPACT_SKIPPED - If there are too few free pages for compaction
752 * COMPACT_PARTIAL - If the allocation would succeed without compaction
753 * COMPACT_CONTINUE - If compaction should run now
754 */
755unsigned long compaction_suitable(struct zone *zone, int order)
756{
757 int fragindex;
758 unsigned long watermark;
759
760 /*
Michal Hocko3957c772011-06-15 15:08:25 -0700761 * order == -1 is expected when compacting via
762 * /proc/sys/vm/compact_memory
763 */
764 if (order == -1)
765 return COMPACT_CONTINUE;
766
767 /*
Mel Gorman3e7d3442011-01-13 15:45:56 -0800768 * Watermarks for order-0 must be met for compaction. Note the 2UL.
769 * This is because during migration, copies of pages need to be
770 * allocated and for a short time, the footprint is higher
771 */
772 watermark = low_wmark_pages(zone) + (2UL << order);
773 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
774 return COMPACT_SKIPPED;
775
776 /*
777 * fragmentation index determines if allocation failures are due to
778 * low memory or external fragmentation
779 *
Shaohua Lia582a732011-06-15 15:08:49 -0700780 * index of -1000 implies allocations might succeed depending on
781 * watermarks
Mel Gorman3e7d3442011-01-13 15:45:56 -0800782 * index towards 0 implies failure is due to lack of memory
783 * index towards 1000 implies failure is due to fragmentation
784 *
785 * Only compact if a failure would be due to fragmentation.
786 */
787 fragindex = fragmentation_index(zone, order);
788 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
789 return COMPACT_SKIPPED;
790
Shaohua Lia582a732011-06-15 15:08:49 -0700791 if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
792 0, 0))
Mel Gorman3e7d3442011-01-13 15:45:56 -0800793 return COMPACT_PARTIAL;
794
795 return COMPACT_CONTINUE;
796}
797
Mel Gorman748446b2010-05-24 14:32:27 -0700798static int compact_zone(struct zone *zone, struct compact_control *cc)
799{
800 int ret;
801
Mel Gorman3e7d3442011-01-13 15:45:56 -0800802 ret = compaction_suitable(zone, cc->order);
803 switch (ret) {
804 case COMPACT_PARTIAL:
805 case COMPACT_SKIPPED:
806 /* Compaction is likely to fail */
807 return ret;
808 case COMPACT_CONTINUE:
809 /* Fall through to compaction */
810 ;
811 }
812
Mel Gorman748446b2010-05-24 14:32:27 -0700813 /* Setup to move all movable pages to the end of the zone */
814 cc->migrate_pfn = zone->zone_start_pfn;
815 cc->free_pfn = cc->migrate_pfn + zone->spanned_pages;
816 cc->free_pfn &= ~(pageblock_nr_pages-1);
817
818 migrate_prep_local();
819
820 while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
821 unsigned long nr_migrate, nr_remaining;
Minchan Kim9d502c12011-03-22 16:30:39 -0700822 int err;
Mel Gorman748446b2010-05-24 14:32:27 -0700823
Mel Gormanf9e35b32011-06-15 15:08:52 -0700824 switch (isolate_migratepages(zone, cc)) {
825 case ISOLATE_ABORT:
826 ret = COMPACT_PARTIAL;
Shaohua Li03dd8fe2012-10-08 16:32:27 -0700827 putback_lru_pages(&cc->migratepages);
828 cc->nr_migratepages = 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700829 goto out;
830 case ISOLATE_NONE:
Mel Gorman748446b2010-05-24 14:32:27 -0700831 continue;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700832 case ISOLATE_SUCCESS:
833 ;
834 }
Mel Gorman748446b2010-05-24 14:32:27 -0700835
836 nr_migrate = cc->nr_migratepages;
Minchan Kim9d502c12011-03-22 16:30:39 -0700837 err = migrate_pages(&cc->migratepages, compaction_alloc,
Mel Gorman7f0f2492011-01-13 15:45:58 -0800838 (unsigned long)cc, false,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800839 cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC);
Mel Gorman748446b2010-05-24 14:32:27 -0700840 update_nr_listpages(cc);
841 nr_remaining = cc->nr_migratepages;
842
843 count_vm_event(COMPACTBLOCKS);
844 count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining);
845 if (nr_remaining)
846 count_vm_events(COMPACTPAGEFAILED, nr_remaining);
Mel Gormanb7aba692011-01-13 15:45:54 -0800847 trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
848 nr_remaining);
Mel Gorman748446b2010-05-24 14:32:27 -0700849
850 /* Release LRU pages not migrated */
Minchan Kim9d502c12011-03-22 16:30:39 -0700851 if (err) {
Mel Gorman748446b2010-05-24 14:32:27 -0700852 putback_lru_pages(&cc->migratepages);
853 cc->nr_migratepages = 0;
David Rientjes7a08b442012-07-11 14:02:13 -0700854 if (err == -ENOMEM) {
855 ret = COMPACT_PARTIAL;
856 goto out;
857 }
Mel Gorman748446b2010-05-24 14:32:27 -0700858 }
Mel Gormanc74068b2012-10-08 16:29:12 -0700859
860 /* Capture a page now if it is a suitable size */
861 compact_capture_page(cc);
Mel Gorman748446b2010-05-24 14:32:27 -0700862 }
863
Mel Gormanf9e35b32011-06-15 15:08:52 -0700864out:
Mel Gorman748446b2010-05-24 14:32:27 -0700865 /* Release free pages and check accounting */
866 cc->nr_freepages -= release_freepages(&cc->freepages);
867 VM_BUG_ON(cc->nr_freepages != 0);
868
869 return ret;
870}
Mel Gorman76ab0f52010-05-24 14:32:28 -0700871
Kyungmin Parkd43a87e2011-10-31 17:09:08 -0700872static unsigned long compact_zone_order(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800873 int order, gfp_t gfp_mask,
Mel Gormanc74068b2012-10-08 16:29:12 -0700874 bool sync, bool *contended,
875 struct page **page)
Mel Gorman56de7262010-05-24 14:32:30 -0700876{
Shaohua Li03dd8fe2012-10-08 16:32:27 -0700877 unsigned long ret;
Mel Gorman56de7262010-05-24 14:32:30 -0700878 struct compact_control cc = {
879 .nr_freepages = 0,
880 .nr_migratepages = 0,
881 .order = order,
882 .migratetype = allocflags_to_migratetype(gfp_mask),
883 .zone = zone,
Mel Gorman77f1fe62011-01-13 15:45:57 -0800884 .sync = sync,
Mel Gormanc74068b2012-10-08 16:29:12 -0700885 .page = page,
Mel Gorman56de7262010-05-24 14:32:30 -0700886 };
887 INIT_LIST_HEAD(&cc.freepages);
888 INIT_LIST_HEAD(&cc.migratepages);
889
Shaohua Li03dd8fe2012-10-08 16:32:27 -0700890 ret = compact_zone(zone, &cc);
891
892 VM_BUG_ON(!list_empty(&cc.freepages));
893 VM_BUG_ON(!list_empty(&cc.migratepages));
894
895 *contended = cc.contended;
896 return ret;
Mel Gorman56de7262010-05-24 14:32:30 -0700897}
898
Mel Gorman5e771902010-05-24 14:32:31 -0700899int sysctl_extfrag_threshold = 500;
900
Mel Gorman56de7262010-05-24 14:32:30 -0700901/**
902 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
903 * @zonelist: The zonelist used for the current allocation
904 * @order: The order of the current allocation
905 * @gfp_mask: The GFP mask of the current allocation
906 * @nodemask: The allowed nodes to allocate from
Mel Gorman77f1fe62011-01-13 15:45:57 -0800907 * @sync: Whether migration is synchronous or not
Mel Gorman8a063192012-10-08 16:32:31 -0700908 * @contended: Return value that is true if compaction was aborted due to lock contention
909 * @page: Optionally capture a free page of the requested order during compaction
Mel Gorman56de7262010-05-24 14:32:30 -0700910 *
911 * This is the main entry point for direct page compaction.
912 */
913unsigned long try_to_compact_pages(struct zonelist *zonelist,
Mel Gorman77f1fe62011-01-13 15:45:57 -0800914 int order, gfp_t gfp_mask, nodemask_t *nodemask,
Mel Gormanc74068b2012-10-08 16:29:12 -0700915 bool sync, bool *contended, struct page **page)
Mel Gorman56de7262010-05-24 14:32:30 -0700916{
917 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
918 int may_enter_fs = gfp_mask & __GFP_FS;
919 int may_perform_io = gfp_mask & __GFP_IO;
Mel Gorman56de7262010-05-24 14:32:30 -0700920 struct zoneref *z;
921 struct zone *zone;
922 int rc = COMPACT_SKIPPED;
Bartlomiej Zolnierkiewicz850bf192012-10-08 16:32:05 -0700923 int alloc_flags = 0;
Mel Gorman56de7262010-05-24 14:32:30 -0700924
Mel Gormanbc337a92012-10-08 16:29:09 -0700925 /* Check if the GFP flags allow compaction */
Andrea Arcangelic5a73c32011-01-13 15:47:11 -0800926 if (!order || !may_enter_fs || !may_perform_io)
Mel Gorman56de7262010-05-24 14:32:30 -0700927 return rc;
928
929 count_vm_event(COMPACTSTALL);
930
Bartlomiej Zolnierkiewicz850bf192012-10-08 16:32:05 -0700931#ifdef CONFIG_CMA
932 if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
933 alloc_flags |= ALLOC_CMA;
934#endif
Mel Gorman56de7262010-05-24 14:32:30 -0700935 /* Compact each zone in the list */
936 for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
937 nodemask) {
Mel Gorman56de7262010-05-24 14:32:30 -0700938 int status;
939
Mel Gorman0ba33882012-08-21 16:16:17 -0700940 status = compact_zone_order(zone, order, gfp_mask, sync,
Mel Gormanc74068b2012-10-08 16:29:12 -0700941 contended, page);
Mel Gorman56de7262010-05-24 14:32:30 -0700942 rc = max(status, rc);
943
Mel Gorman3e7d3442011-01-13 15:45:56 -0800944 /* If a normal allocation would succeed, stop compacting */
Bartlomiej Zolnierkiewicz850bf192012-10-08 16:32:05 -0700945 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
946 alloc_flags))
Mel Gorman56de7262010-05-24 14:32:30 -0700947 break;
948 }
949
950 return rc;
951}
952
953
Mel Gorman76ab0f52010-05-24 14:32:28 -0700954/* Compact all zones within a node */
Rik van Riel7be62de2012-03-21 16:33:52 -0700955static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
Mel Gorman76ab0f52010-05-24 14:32:28 -0700956{
957 int zoneid;
Mel Gorman76ab0f52010-05-24 14:32:28 -0700958 struct zone *zone;
959
Mel Gorman76ab0f52010-05-24 14:32:28 -0700960 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
Mel Gorman76ab0f52010-05-24 14:32:28 -0700961
962 zone = &pgdat->node_zones[zoneid];
963 if (!populated_zone(zone))
964 continue;
965
Rik van Riel7be62de2012-03-21 16:33:52 -0700966 cc->nr_freepages = 0;
967 cc->nr_migratepages = 0;
968 cc->zone = zone;
969 INIT_LIST_HEAD(&cc->freepages);
970 INIT_LIST_HEAD(&cc->migratepages);
Mel Gorman76ab0f52010-05-24 14:32:28 -0700971
Dan Carpenteraad6ec32012-03-21 16:33:54 -0700972 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
Rik van Riel7be62de2012-03-21 16:33:52 -0700973 compact_zone(zone, cc);
Mel Gorman76ab0f52010-05-24 14:32:28 -0700974
Rik van Rielaff62242012-03-21 16:33:52 -0700975 if (cc->order > 0) {
976 int ok = zone_watermark_ok(zone, cc->order,
977 low_wmark_pages(zone), 0, 0);
Minchan Kim57bb21c2012-08-21 16:16:03 -0700978 if (ok && cc->order >= zone->compact_order_failed)
Rik van Rielaff62242012-03-21 16:33:52 -0700979 zone->compact_order_failed = cc->order + 1;
980 /* Currently async compaction is never deferred. */
981 else if (!ok && cc->sync)
982 defer_compaction(zone, cc->order);
983 }
984
Rik van Riel7be62de2012-03-21 16:33:52 -0700985 VM_BUG_ON(!list_empty(&cc->freepages));
986 VM_BUG_ON(!list_empty(&cc->migratepages));
Mel Gorman76ab0f52010-05-24 14:32:28 -0700987 }
988
989 return 0;
990}
991
Rik van Riel7be62de2012-03-21 16:33:52 -0700992int compact_pgdat(pg_data_t *pgdat, int order)
993{
994 struct compact_control cc = {
995 .order = order,
996 .sync = false,
Mel Gormanc74068b2012-10-08 16:29:12 -0700997 .page = NULL,
Rik van Riel7be62de2012-03-21 16:33:52 -0700998 };
999
1000 return __compact_pgdat(pgdat, &cc);
1001}
1002
1003static int compact_node(int nid)
1004{
Rik van Riel7be62de2012-03-21 16:33:52 -07001005 struct compact_control cc = {
1006 .order = -1,
1007 .sync = true,
Mel Gormanc74068b2012-10-08 16:29:12 -07001008 .page = NULL,
Rik van Riel7be62de2012-03-21 16:33:52 -07001009 };
1010
Hugh Dickins8575ec22012-03-21 16:33:53 -07001011 return __compact_pgdat(NODE_DATA(nid), &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -07001012}
1013
Mel Gorman76ab0f52010-05-24 14:32:28 -07001014/* Compact all nodes in the system */
Jason Liuc0b96522013-01-11 14:31:47 -08001015static void compact_nodes(void)
Mel Gorman76ab0f52010-05-24 14:32:28 -07001016{
1017 int nid;
1018
Hugh Dickins8575ec22012-03-21 16:33:53 -07001019 /* Flush pending updates to the LRU lists */
1020 lru_add_drain_all();
1021
Mel Gorman76ab0f52010-05-24 14:32:28 -07001022 for_each_online_node(nid)
1023 compact_node(nid);
Mel Gorman76ab0f52010-05-24 14:32:28 -07001024}
1025
1026/* The written value is actually unused, all memory is compacted */
1027int sysctl_compact_memory;
1028
1029/* This is the entry point for compacting all nodes via /proc/sys/vm */
1030int sysctl_compaction_handler(struct ctl_table *table, int write,
1031 void __user *buffer, size_t *length, loff_t *ppos)
1032{
1033 if (write)
Jason Liuc0b96522013-01-11 14:31:47 -08001034 compact_nodes();
Mel Gorman76ab0f52010-05-24 14:32:28 -07001035
1036 return 0;
1037}
Mel Gormaned4a6d72010-05-24 14:32:29 -07001038
Mel Gorman5e771902010-05-24 14:32:31 -07001039int sysctl_extfrag_handler(struct ctl_table *table, int write,
1040 void __user *buffer, size_t *length, loff_t *ppos)
1041{
1042 proc_dointvec_minmax(table, write, buffer, length, ppos);
1043
1044 return 0;
1045}
1046
Mel Gormaned4a6d72010-05-24 14:32:29 -07001047#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
Kay Sievers10fbcf42011-12-21 14:48:43 -08001048ssize_t sysfs_compact_node(struct device *dev,
1049 struct device_attribute *attr,
Mel Gormaned4a6d72010-05-24 14:32:29 -07001050 const char *buf, size_t count)
1051{
Hugh Dickins8575ec22012-03-21 16:33:53 -07001052 int nid = dev->id;
1053
1054 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
1055 /* Flush pending updates to the LRU lists */
1056 lru_add_drain_all();
1057
1058 compact_node(nid);
1059 }
Mel Gormaned4a6d72010-05-24 14:32:29 -07001060
1061 return count;
1062}
Kay Sievers10fbcf42011-12-21 14:48:43 -08001063static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001064
1065int compaction_register_node(struct node *node)
1066{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001067 return device_create_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001068}
1069
1070void compaction_unregister_node(struct node *node)
1071{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001072 return device_remove_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001073}
1074#endif /* CONFIG_SYSFS && CONFIG_NUMA */
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +01001075
1076#endif /* CONFIG_COMPACTION */