blob: c07d21f0d16eebf2308abf9631b77477afdde0da [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
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +010053/*
Mel Gorman0ba33882012-08-21 16:16:17 -070054 * Compaction requires the taking of some coarse locks that are potentially
55 * very heavily contended. Check if the process needs to be scheduled or
56 * if the lock is contended. For async compaction, back out in the event
57 * if contention is severe. For sync compaction, schedule.
58 *
59 * Returns true if the lock is held.
60 * Returns false if the lock is released and compaction should abort
61 */
62static bool compact_checklock_irqsave(spinlock_t *lock, unsigned long *flags,
63 bool locked, struct compact_control *cc)
64{
65 if (need_resched() || spin_is_contended(lock)) {
66 if (locked) {
67 spin_unlock_irqrestore(lock, *flags);
68 locked = false;
69 }
70
71 /* async aborts if taking too long or contended */
72 if (!cc->sync) {
Shaohua Li03dd8fe2012-10-08 16:32:27 -070073 cc->contended = true;
Mel Gorman0ba33882012-08-21 16:16:17 -070074 return false;
75 }
76
77 cond_resched();
78 if (fatal_signal_pending(current))
79 return false;
80 }
81
82 if (!locked)
83 spin_lock_irqsave(lock, *flags);
84 return true;
85}
86
87static inline bool compact_trylock_irqsave(spinlock_t *lock,
88 unsigned long *flags, struct compact_control *cc)
89{
90 return compact_checklock_irqsave(lock, flags, false, cc);
91}
92
Mel Gormanc74068b2012-10-08 16:29:12 -070093static void compact_capture_page(struct compact_control *cc)
94{
95 unsigned long flags;
96 int mtype, mtype_low, mtype_high;
97
98 if (!cc->page || *cc->page)
99 return;
100
101 /*
102 * For MIGRATE_MOVABLE allocations we capture a suitable page ASAP
103 * regardless of the migratetype of the freelist is is captured from.
104 * This is fine because the order for a high-order MIGRATE_MOVABLE
105 * allocation is typically at least a pageblock size and overall
106 * fragmentation is not impaired. Other allocation types must
107 * capture pages from their own migratelist because otherwise they
108 * could pollute other pageblocks like MIGRATE_MOVABLE with
109 * difficult to move pages and making fragmentation worse overall.
110 */
111 if (cc->migratetype == MIGRATE_MOVABLE) {
112 mtype_low = 0;
113 mtype_high = MIGRATE_PCPTYPES;
114 } else {
115 mtype_low = cc->migratetype;
116 mtype_high = cc->migratetype + 1;
117 }
118
119 /* Speculatively examine the free lists without zone lock */
120 for (mtype = mtype_low; mtype < mtype_high; mtype++) {
121 int order;
122 for (order = cc->order; order < MAX_ORDER; order++) {
123 struct page *page;
124 struct free_area *area;
125 area = &(cc->zone->free_area[order]);
126 if (list_empty(&area->free_list[mtype]))
127 continue;
128
129 /* Take the lock and attempt capture of the page */
130 if (!compact_trylock_irqsave(&cc->zone->lock, &flags, cc))
131 return;
132 if (!list_empty(&area->free_list[mtype])) {
133 page = list_entry(area->free_list[mtype].next,
134 struct page, lru);
135 if (capture_free_page(page, cc->order, mtype)) {
136 spin_unlock_irqrestore(&cc->zone->lock,
137 flags);
138 *cc->page = page;
139 return;
140 }
141 }
142 spin_unlock_irqrestore(&cc->zone->lock, flags);
143 }
144 }
145}
146
Mel Gorman0ba33882012-08-21 16:16:17 -0700147/*
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100148 * Isolate free pages onto a private freelist. Caller must hold zone->lock.
149 * If @strict is true, will abort returning 0 on any invalid PFNs or non-free
150 * pages inside of the pageblock (even though it may still end up isolating
151 * some pages).
152 */
153static unsigned long isolate_freepages_block(unsigned long blockpfn,
154 unsigned long end_pfn,
155 struct list_head *freelist,
156 bool strict)
Mel Gorman748446b2010-05-24 14:32:27 -0700157{
Mel Gormanb7aba692011-01-13 15:45:54 -0800158 int nr_scanned = 0, total_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700159 struct page *cursor;
160
Mel Gorman748446b2010-05-24 14:32:27 -0700161 cursor = pfn_to_page(blockpfn);
162
163 /* Isolate free pages. This assumes the block is valid */
164 for (; blockpfn < end_pfn; blockpfn++, cursor++) {
165 int isolated, i;
166 struct page *page = cursor;
167
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100168 if (!pfn_valid_within(blockpfn)) {
169 if (strict)
170 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700171 continue;
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100172 }
Mel Gormanb7aba692011-01-13 15:45:54 -0800173 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700174
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100175 if (!PageBuddy(page)) {
176 if (strict)
177 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700178 continue;
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100179 }
Mel Gorman748446b2010-05-24 14:32:27 -0700180
181 /* Found a free page, break it into order-0 pages */
182 isolated = split_free_page(page);
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100183 if (!isolated && strict)
184 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700185 total_isolated += isolated;
186 for (i = 0; i < isolated; i++) {
187 list_add(&page->lru, freelist);
188 page++;
189 }
190
191 /* If a page was split, advance to the end of it */
192 if (isolated) {
193 blockpfn += isolated - 1;
194 cursor += isolated - 1;
195 }
196 }
197
Mel Gormanb7aba692011-01-13 15:45:54 -0800198 trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
Mel Gorman748446b2010-05-24 14:32:27 -0700199 return total_isolated;
200}
201
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100202/**
203 * isolate_freepages_range() - isolate free pages.
204 * @start_pfn: The first PFN to start isolating.
205 * @end_pfn: The one-past-last PFN.
206 *
207 * Non-free pages, invalid PFNs, or zone boundaries within the
208 * [start_pfn, end_pfn) range are considered errors, cause function to
209 * undo its actions and return zero.
210 *
211 * Otherwise, function returns one-past-the-last PFN of isolated page
212 * (which may be greater then end_pfn if end fell in a middle of
213 * a free page).
214 */
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100215unsigned long
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100216isolate_freepages_range(unsigned long start_pfn, unsigned long end_pfn)
217{
218 unsigned long isolated, pfn, block_end_pfn, flags;
219 struct zone *zone = NULL;
220 LIST_HEAD(freelist);
221
222 if (pfn_valid(start_pfn))
223 zone = page_zone(pfn_to_page(start_pfn));
224
225 for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) {
226 if (!pfn_valid(pfn) || zone != page_zone(pfn_to_page(pfn)))
227 break;
228
229 /*
230 * On subsequent iterations ALIGN() is actually not needed,
231 * but we keep it that we not to complicate the code.
232 */
233 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
234 block_end_pfn = min(block_end_pfn, end_pfn);
235
236 spin_lock_irqsave(&zone->lock, flags);
237 isolated = isolate_freepages_block(pfn, block_end_pfn,
238 &freelist, true);
239 spin_unlock_irqrestore(&zone->lock, flags);
240
241 /*
242 * In strict mode, isolate_freepages_block() returns 0 if
243 * there are any holes in the block (ie. invalid PFNs or
244 * non-free pages).
245 */
246 if (!isolated)
247 break;
248
249 /*
250 * If we managed to isolate pages, it is always (1 << n) *
251 * pageblock_nr_pages for some non-negative n. (Max order
252 * page may span two pageblocks).
253 */
254 }
255
256 /* split_free_page does not map the pages */
257 map_pages(&freelist);
258
259 if (pfn < end_pfn) {
260 /* Loop terminated early, cleanup. */
261 release_freepages(&freelist);
262 return 0;
263 }
264
265 /* We don't use freelists for anything. */
266 return pfn;
267}
268
Mel Gorman748446b2010-05-24 14:32:27 -0700269/* Update the number of anon and file isolated pages in the zone */
Mel Gorman0ba33882012-08-21 16:16:17 -0700270static void acct_isolated(struct zone *zone, bool locked, struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700271{
272 struct page *page;
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700273 unsigned int count[2] = { 0, };
Mel Gorman748446b2010-05-24 14:32:27 -0700274
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700275 list_for_each_entry(page, &cc->migratepages, lru)
276 count[!!page_is_file_cache(page)]++;
Mel Gorman748446b2010-05-24 14:32:27 -0700277
Mel Gorman0ba33882012-08-21 16:16:17 -0700278 /* If locked we can use the interrupt unsafe versions */
279 if (locked) {
280 __mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
281 __mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
282 } else {
283 mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
284 mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
285 }
Mel Gorman748446b2010-05-24 14:32:27 -0700286}
287
288/* Similar to reclaim, but different enough that they don't share logic */
289static bool too_many_isolated(struct zone *zone)
290{
Minchan Kimbc693042010-09-09 16:38:00 -0700291 unsigned long active, inactive, isolated;
Mel Gorman748446b2010-05-24 14:32:27 -0700292
293 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
294 zone_page_state(zone, NR_INACTIVE_ANON);
Minchan Kimbc693042010-09-09 16:38:00 -0700295 active = zone_page_state(zone, NR_ACTIVE_FILE) +
296 zone_page_state(zone, NR_ACTIVE_ANON);
Mel Gorman748446b2010-05-24 14:32:27 -0700297 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
298 zone_page_state(zone, NR_ISOLATED_ANON);
299
Minchan Kimbc693042010-09-09 16:38:00 -0700300 return isolated > (inactive + active) / 2;
Mel Gorman748446b2010-05-24 14:32:27 -0700301}
302
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100303/**
304 * isolate_migratepages_range() - isolate all migrate-able pages in range.
305 * @zone: Zone pages are in.
306 * @cc: Compaction control structure.
307 * @low_pfn: The first PFN of the range.
308 * @end_pfn: The one-past-the-last PFN of the range.
309 *
310 * Isolate all pages that can be migrated from the range specified by
311 * [low_pfn, end_pfn). Returns zero if there is a fatal signal
312 * pending), otherwise PFN of the first page that was not scanned
313 * (which may be both less, equal to or more then end_pfn).
314 *
315 * Assumes that cc->migratepages is empty and cc->nr_migratepages is
316 * zero.
317 *
318 * Apart from cc->migratepages and cc->nr_migratetypes this function
319 * does not modify any cc's fields, in particular it does not modify
320 * (or read for that matter) cc->migrate_pfn.
Mel Gorman748446b2010-05-24 14:32:27 -0700321 */
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100322unsigned long
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100323isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
324 unsigned long low_pfn, unsigned long end_pfn)
Mel Gorman748446b2010-05-24 14:32:27 -0700325{
Mel Gorman9927af742011-01-13 15:45:59 -0800326 unsigned long last_pageblock_nr = 0, pageblock_nr;
Mel Gormanb7aba692011-01-13 15:45:54 -0800327 unsigned long nr_scanned = 0, nr_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700328 struct list_head *migratelist = &cc->migratepages;
Konstantin Khlebnikovfa168092012-05-29 15:06:54 -0700329 isolate_mode_t mode = 0;
Mel Gorman0ba33882012-08-21 16:16:17 -0700330 unsigned long flags;
331 bool locked;
Mel Gorman748446b2010-05-24 14:32:27 -0700332
Mel Gorman748446b2010-05-24 14:32:27 -0700333 /*
334 * Ensure that there are not too many pages isolated from the LRU
335 * list by either parallel reclaimers or compaction. If there are,
336 * delay for some time until fewer pages are isolated
337 */
338 while (unlikely(too_many_isolated(zone))) {
Mel Gormanf9e35b32011-06-15 15:08:52 -0700339 /* async migration should just abort */
340 if (!cc->sync)
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100341 return 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700342
Mel Gorman748446b2010-05-24 14:32:27 -0700343 congestion_wait(BLK_RW_ASYNC, HZ/10);
344
345 if (fatal_signal_pending(current))
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100346 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700347 }
348
349 /* Time to isolate some pages for migration */
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700350 cond_resched();
Mel Gorman0ba33882012-08-21 16:16:17 -0700351 spin_lock_irqsave(&zone->lru_lock, flags);
352 locked = true;
Mel Gorman748446b2010-05-24 14:32:27 -0700353 for (; low_pfn < end_pfn; low_pfn++) {
354 struct page *page;
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700355
356 /* give a chance to irqs before checking need_resched() */
357 if (!((low_pfn+1) % SWAP_CLUSTER_MAX)) {
Mel Gorman0ba33882012-08-21 16:16:17 -0700358 spin_unlock_irqrestore(&zone->lru_lock, flags);
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700359 locked = false;
360 }
Mel Gorman0ba33882012-08-21 16:16:17 -0700361
362 /* Check if it is ok to still hold the lock */
363 locked = compact_checklock_irqsave(&zone->lru_lock, &flags,
364 locked, cc);
365 if (!locked)
366 break;
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700367
Mel Gorman0bf380b2012-02-03 15:37:18 -0800368 /*
369 * migrate_pfn does not necessarily start aligned to a
370 * pageblock. Ensure that pfn_valid is called when moving
371 * into a new MAX_ORDER_NR_PAGES range in case of large
372 * memory holes within the zone
373 */
374 if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) {
375 if (!pfn_valid(low_pfn)) {
376 low_pfn += MAX_ORDER_NR_PAGES - 1;
377 continue;
378 }
379 }
380
Mel Gorman748446b2010-05-24 14:32:27 -0700381 if (!pfn_valid_within(low_pfn))
382 continue;
Mel Gormanb7aba692011-01-13 15:45:54 -0800383 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700384
Mel Gormandc908602012-02-08 17:13:38 -0800385 /*
386 * Get the page and ensure the page is within the same zone.
387 * See the comment in isolate_freepages about overlapping
388 * nodes. It is deliberate that the new zone lock is not taken
389 * as memory compaction should not move pages between nodes.
390 */
Mel Gorman748446b2010-05-24 14:32:27 -0700391 page = pfn_to_page(low_pfn);
Mel Gormandc908602012-02-08 17:13:38 -0800392 if (page_zone(page) != zone)
393 continue;
394
395 /* Skip if free */
Mel Gorman748446b2010-05-24 14:32:27 -0700396 if (PageBuddy(page))
397 continue;
398
Mel Gorman9927af742011-01-13 15:45:59 -0800399 /*
400 * For async migration, also only scan in MOVABLE blocks. Async
401 * migration is optimistic to see if the minimum amount of work
402 * satisfies the allocation
403 */
404 pageblock_nr = low_pfn >> pageblock_order;
405 if (!cc->sync && last_pageblock_nr != pageblock_nr &&
Michal Nazarewiczd4158d22011-12-29 13:09:50 +0100406 !migrate_async_suitable(get_pageblock_migratetype(page))) {
Mel Gorman9927af742011-01-13 15:45:59 -0800407 low_pfn += pageblock_nr_pages;
408 low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1;
409 last_pageblock_nr = pageblock_nr;
410 continue;
411 }
412
Andrea Arcangelibc835012011-01-13 15:47:08 -0800413 if (!PageLRU(page))
414 continue;
415
416 /*
417 * PageLRU is set, and lru_lock excludes isolation,
418 * splitting and collapsing (collapsing has already
419 * happened if PageLRU is set).
420 */
421 if (PageTransHuge(page)) {
422 low_pfn += (1 << compound_order(page)) - 1;
423 continue;
424 }
425
Mel Gormanc8244932012-01-12 17:19:38 -0800426 if (!cc->sync)
427 mode |= ISOLATE_ASYNC_MIGRATE;
428
Mel Gorman748446b2010-05-24 14:32:27 -0700429 /* Try isolate the page */
Konstantin Khlebnikovfa168092012-05-29 15:06:54 -0700430 if (__isolate_lru_page(page, mode) != 0)
Mel Gorman748446b2010-05-24 14:32:27 -0700431 continue;
432
Andrea Arcangelibc835012011-01-13 15:47:08 -0800433 VM_BUG_ON(PageTransCompound(page));
434
Mel Gorman748446b2010-05-24 14:32:27 -0700435 /* Successfully isolated */
436 del_page_from_lru_list(zone, page, page_lru(page));
437 list_add(&page->lru, migratelist);
Mel Gorman748446b2010-05-24 14:32:27 -0700438 cc->nr_migratepages++;
Mel Gormanb7aba692011-01-13 15:45:54 -0800439 nr_isolated++;
Mel Gorman748446b2010-05-24 14:32:27 -0700440
441 /* Avoid isolating too much */
Hillf Danton31b83842012-01-10 15:07:59 -0800442 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
443 ++low_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700444 break;
Hillf Danton31b83842012-01-10 15:07:59 -0800445 }
Mel Gorman748446b2010-05-24 14:32:27 -0700446 }
447
Mel Gorman0ba33882012-08-21 16:16:17 -0700448 acct_isolated(zone, locked, cc);
Mel Gorman748446b2010-05-24 14:32:27 -0700449
Mel Gorman0ba33882012-08-21 16:16:17 -0700450 if (locked)
451 spin_unlock_irqrestore(&zone->lru_lock, flags);
Mel Gorman748446b2010-05-24 14:32:27 -0700452
Mel Gormanb7aba692011-01-13 15:45:54 -0800453 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
454
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100455 return low_pfn;
456}
457
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100458#endif /* CONFIG_COMPACTION || CONFIG_CMA */
459#ifdef CONFIG_COMPACTION
460
461/* Returns true if the page is within a block suitable for migration to */
462static bool suitable_migration_target(struct page *page)
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100463{
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100464
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100465 int migratetype = get_pageblock_migratetype(page);
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100466
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100467 /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */
468 if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE)
469 return false;
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100470
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100471 /* If the page is a large free page, then allow migration */
472 if (PageBuddy(page) && page_order(page) >= pageblock_order)
473 return true;
474
Michal Nazarewiczd4158d22011-12-29 13:09:50 +0100475 /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
476 if (migrate_async_suitable(migratetype))
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100477 return true;
478
479 /* Otherwise skip the block */
480 return false;
481}
482
483/*
484 * Based on information in the current compact_control, find blocks
485 * suitable for isolating free pages from and then isolate them.
486 */
487static void isolate_freepages(struct zone *zone,
488 struct compact_control *cc)
489{
490 struct page *page;
491 unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn;
492 unsigned long flags;
493 int nr_freepages = cc->nr_freepages;
494 struct list_head *freelist = &cc->freepages;
495
496 /*
497 * Initialise the free scanner. The starting point is where we last
498 * scanned from (or the end of the zone if starting). The low point
499 * is the end of the pageblock the migration scanner is using.
500 */
501 pfn = cc->free_pfn;
502 low_pfn = cc->migrate_pfn + pageblock_nr_pages;
503
504 /*
505 * Take care that if the migration scanner is at the end of the zone
506 * that the free scanner does not accidentally move to the next zone
507 * in the next isolation cycle.
508 */
509 high_pfn = min(low_pfn, pfn);
510
511 zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
512
513 /*
514 * Isolate free pages until enough are available to migrate the
515 * pages on cc->migratepages. We stop searching if the migrate
516 * and free page scanners meet or enough free pages are isolated.
517 */
518 for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
519 pfn -= pageblock_nr_pages) {
520 unsigned long isolated;
521
522 if (!pfn_valid(pfn))
523 continue;
524
525 /*
526 * Check for overlapping nodes/zones. It's possible on some
527 * configurations to have a setup like
528 * node0 node1 node0
529 * i.e. it's possible that all pages within a zones range of
530 * pages do not belong to a single zone.
531 */
532 page = pfn_to_page(pfn);
533 if (page_zone(page) != zone)
534 continue;
535
536 /* Check the block is suitable for migration */
537 if (!suitable_migration_target(page))
538 continue;
539
540 /*
541 * Found a block suitable for isolating free pages from. Now
542 * we disabled interrupts, double check things are ok and
543 * isolate the pages. This is to minimise the time IRQs
544 * are disabled
545 */
546 isolated = 0;
Mel Gorman0ba33882012-08-21 16:16:17 -0700547
548 /*
549 * The zone lock must be held to isolate freepages. This
550 * unfortunately this is a very coarse lock and can be
551 * heavily contended if there are parallel allocations
552 * or parallel compactions. For async compaction do not
553 * spin on the lock
554 */
555 if (!compact_trylock_irqsave(&zone->lock, &flags, cc))
556 break;
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100557 if (suitable_migration_target(page)) {
558 end_pfn = min(pfn + pageblock_nr_pages, zone_end_pfn);
559 isolated = isolate_freepages_block(pfn, end_pfn,
560 freelist, false);
561 nr_freepages += isolated;
562 }
563 spin_unlock_irqrestore(&zone->lock, flags);
564
565 /*
566 * Record the highest PFN we isolated pages from. When next
567 * looking for free pages, the search will restart here as
568 * page migration may have returned some pages to the allocator
569 */
570 if (isolated)
571 high_pfn = max(high_pfn, pfn);
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100572 }
573
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100574 /* split_free_page does not map the pages */
575 map_pages(freelist);
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100576
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100577 cc->free_pfn = high_pfn;
578 cc->nr_freepages = nr_freepages;
Mel Gorman748446b2010-05-24 14:32:27 -0700579}
580
581/*
582 * This is a migrate-callback that "allocates" freepages by taking pages
583 * from the isolated freelists in the block we are migrating to.
584 */
585static struct page *compaction_alloc(struct page *migratepage,
586 unsigned long data,
587 int **result)
588{
589 struct compact_control *cc = (struct compact_control *)data;
590 struct page *freepage;
591
592 /* Isolate free pages if necessary */
593 if (list_empty(&cc->freepages)) {
594 isolate_freepages(cc->zone, cc);
595
596 if (list_empty(&cc->freepages))
597 return NULL;
598 }
599
600 freepage = list_entry(cc->freepages.next, struct page, lru);
601 list_del(&freepage->lru);
602 cc->nr_freepages--;
603
604 return freepage;
605}
606
607/*
608 * We cannot control nr_migratepages and nr_freepages fully when migration is
609 * running as migrate_pages() has no knowledge of compact_control. When
610 * migration is complete, we count the number of pages on the lists by hand.
611 */
612static void update_nr_listpages(struct compact_control *cc)
613{
614 int nr_migratepages = 0;
615 int nr_freepages = 0;
616 struct page *page;
617
618 list_for_each_entry(page, &cc->migratepages, lru)
619 nr_migratepages++;
620 list_for_each_entry(page, &cc->freepages, lru)
621 nr_freepages++;
622
623 cc->nr_migratepages = nr_migratepages;
624 cc->nr_freepages = nr_freepages;
625}
626
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100627/* possible outcome of isolate_migratepages */
628typedef enum {
629 ISOLATE_ABORT, /* Abort compaction now */
630 ISOLATE_NONE, /* No pages isolated, continue scanning */
631 ISOLATE_SUCCESS, /* Pages isolated, migrate */
632} isolate_migrate_t;
633
634/*
635 * Isolate all pages that can be migrated from the block pointed to by
636 * the migrate scanner within compact_control.
637 */
638static isolate_migrate_t isolate_migratepages(struct zone *zone,
639 struct compact_control *cc)
640{
641 unsigned long low_pfn, end_pfn;
642
643 /* Do not scan outside zone boundaries */
644 low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
645
646 /* Only scan within a pageblock boundary */
647 end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
648
649 /* Do not cross the free scanner or scan within a memory hole */
650 if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
651 cc->migrate_pfn = end_pfn;
652 return ISOLATE_NONE;
653 }
654
655 /* Perform the isolation */
656 low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn);
Shaohua Li03dd8fe2012-10-08 16:32:27 -0700657 if (!low_pfn || cc->contended)
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100658 return ISOLATE_ABORT;
659
660 cc->migrate_pfn = low_pfn;
661
662 return ISOLATE_SUCCESS;
663}
664
Mel Gorman748446b2010-05-24 14:32:27 -0700665static int compact_finished(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800666 struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700667{
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800668 unsigned long watermark;
Mel Gorman56de7262010-05-24 14:32:30 -0700669
Mel Gorman748446b2010-05-24 14:32:27 -0700670 if (fatal_signal_pending(current))
671 return COMPACT_PARTIAL;
672
673 /* Compaction run completes if the migrate and free scanner meet */
674 if (cc->free_pfn <= cc->migrate_pfn)
675 return COMPACT_COMPLETE;
676
Johannes Weiner82478fb2011-01-20 14:44:21 -0800677 /*
678 * order == -1 is expected when compacting via
679 * /proc/sys/vm/compact_memory
680 */
Mel Gorman56de7262010-05-24 14:32:30 -0700681 if (cc->order == -1)
682 return COMPACT_CONTINUE;
683
Michal Hocko3957c772011-06-15 15:08:25 -0700684 /* Compaction run is not finished if the watermark is not met */
685 watermark = low_wmark_pages(zone);
686 watermark += (1 << cc->order);
687
688 if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
689 return COMPACT_CONTINUE;
690
Mel Gorman56de7262010-05-24 14:32:30 -0700691 /* Direct compactor: Is a suitable page free? */
Mel Gormanc74068b2012-10-08 16:29:12 -0700692 if (cc->page) {
693 /* Was a suitable page captured? */
694 if (*cc->page)
Mel Gorman56de7262010-05-24 14:32:30 -0700695 return COMPACT_PARTIAL;
Mel Gormanc74068b2012-10-08 16:29:12 -0700696 } else {
697 unsigned int order;
698 for (order = cc->order; order < MAX_ORDER; order++) {
699 struct free_area *area = &zone->free_area[cc->order];
700 /* Job done if page is free of the right migratetype */
701 if (!list_empty(&area->free_list[cc->migratetype]))
702 return COMPACT_PARTIAL;
Mel Gorman56de7262010-05-24 14:32:30 -0700703
Mel Gormanc74068b2012-10-08 16:29:12 -0700704 /* Job done if allocation would set block type */
705 if (cc->order >= pageblock_order && area->nr_free)
706 return COMPACT_PARTIAL;
707 }
Mel Gorman56de7262010-05-24 14:32:30 -0700708 }
709
Mel Gorman748446b2010-05-24 14:32:27 -0700710 return COMPACT_CONTINUE;
711}
712
Mel Gorman3e7d3442011-01-13 15:45:56 -0800713/*
714 * compaction_suitable: Is this suitable to run compaction on this zone now?
715 * Returns
716 * COMPACT_SKIPPED - If there are too few free pages for compaction
717 * COMPACT_PARTIAL - If the allocation would succeed without compaction
718 * COMPACT_CONTINUE - If compaction should run now
719 */
720unsigned long compaction_suitable(struct zone *zone, int order)
721{
722 int fragindex;
723 unsigned long watermark;
724
725 /*
Michal Hocko3957c772011-06-15 15:08:25 -0700726 * order == -1 is expected when compacting via
727 * /proc/sys/vm/compact_memory
728 */
729 if (order == -1)
730 return COMPACT_CONTINUE;
731
732 /*
Mel Gorman3e7d3442011-01-13 15:45:56 -0800733 * Watermarks for order-0 must be met for compaction. Note the 2UL.
734 * This is because during migration, copies of pages need to be
735 * allocated and for a short time, the footprint is higher
736 */
737 watermark = low_wmark_pages(zone) + (2UL << order);
738 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
739 return COMPACT_SKIPPED;
740
741 /*
742 * fragmentation index determines if allocation failures are due to
743 * low memory or external fragmentation
744 *
Shaohua Lia582a732011-06-15 15:08:49 -0700745 * index of -1000 implies allocations might succeed depending on
746 * watermarks
Mel Gorman3e7d3442011-01-13 15:45:56 -0800747 * index towards 0 implies failure is due to lack of memory
748 * index towards 1000 implies failure is due to fragmentation
749 *
750 * Only compact if a failure would be due to fragmentation.
751 */
752 fragindex = fragmentation_index(zone, order);
753 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
754 return COMPACT_SKIPPED;
755
Shaohua Lia582a732011-06-15 15:08:49 -0700756 if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
757 0, 0))
Mel Gorman3e7d3442011-01-13 15:45:56 -0800758 return COMPACT_PARTIAL;
759
760 return COMPACT_CONTINUE;
761}
762
Mel Gorman748446b2010-05-24 14:32:27 -0700763static int compact_zone(struct zone *zone, struct compact_control *cc)
764{
765 int ret;
766
Mel Gorman3e7d3442011-01-13 15:45:56 -0800767 ret = compaction_suitable(zone, cc->order);
768 switch (ret) {
769 case COMPACT_PARTIAL:
770 case COMPACT_SKIPPED:
771 /* Compaction is likely to fail */
772 return ret;
773 case COMPACT_CONTINUE:
774 /* Fall through to compaction */
775 ;
776 }
777
Mel Gorman748446b2010-05-24 14:32:27 -0700778 /* Setup to move all movable pages to the end of the zone */
779 cc->migrate_pfn = zone->zone_start_pfn;
780 cc->free_pfn = cc->migrate_pfn + zone->spanned_pages;
781 cc->free_pfn &= ~(pageblock_nr_pages-1);
782
783 migrate_prep_local();
784
785 while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
786 unsigned long nr_migrate, nr_remaining;
Minchan Kim9d502c12011-03-22 16:30:39 -0700787 int err;
Mel Gorman748446b2010-05-24 14:32:27 -0700788
Mel Gormanf9e35b32011-06-15 15:08:52 -0700789 switch (isolate_migratepages(zone, cc)) {
790 case ISOLATE_ABORT:
791 ret = COMPACT_PARTIAL;
Shaohua Li03dd8fe2012-10-08 16:32:27 -0700792 putback_lru_pages(&cc->migratepages);
793 cc->nr_migratepages = 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700794 goto out;
795 case ISOLATE_NONE:
Mel Gorman748446b2010-05-24 14:32:27 -0700796 continue;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700797 case ISOLATE_SUCCESS:
798 ;
799 }
Mel Gorman748446b2010-05-24 14:32:27 -0700800
801 nr_migrate = cc->nr_migratepages;
Minchan Kim9d502c12011-03-22 16:30:39 -0700802 err = migrate_pages(&cc->migratepages, compaction_alloc,
Mel Gorman7f0f2492011-01-13 15:45:58 -0800803 (unsigned long)cc, false,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800804 cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC);
Mel Gorman748446b2010-05-24 14:32:27 -0700805 update_nr_listpages(cc);
806 nr_remaining = cc->nr_migratepages;
807
808 count_vm_event(COMPACTBLOCKS);
809 count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining);
810 if (nr_remaining)
811 count_vm_events(COMPACTPAGEFAILED, nr_remaining);
Mel Gormanb7aba692011-01-13 15:45:54 -0800812 trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
813 nr_remaining);
Mel Gorman748446b2010-05-24 14:32:27 -0700814
815 /* Release LRU pages not migrated */
Minchan Kim9d502c12011-03-22 16:30:39 -0700816 if (err) {
Mel Gorman748446b2010-05-24 14:32:27 -0700817 putback_lru_pages(&cc->migratepages);
818 cc->nr_migratepages = 0;
David Rientjes7a08b442012-07-11 14:02:13 -0700819 if (err == -ENOMEM) {
820 ret = COMPACT_PARTIAL;
821 goto out;
822 }
Mel Gorman748446b2010-05-24 14:32:27 -0700823 }
Mel Gormanc74068b2012-10-08 16:29:12 -0700824
825 /* Capture a page now if it is a suitable size */
826 compact_capture_page(cc);
Mel Gorman748446b2010-05-24 14:32:27 -0700827 }
828
Mel Gormanf9e35b32011-06-15 15:08:52 -0700829out:
Mel Gorman748446b2010-05-24 14:32:27 -0700830 /* Release free pages and check accounting */
831 cc->nr_freepages -= release_freepages(&cc->freepages);
832 VM_BUG_ON(cc->nr_freepages != 0);
833
834 return ret;
835}
Mel Gorman76ab0f52010-05-24 14:32:28 -0700836
Kyungmin Parkd43a87e2011-10-31 17:09:08 -0700837static unsigned long compact_zone_order(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800838 int order, gfp_t gfp_mask,
Mel Gormanc74068b2012-10-08 16:29:12 -0700839 bool sync, bool *contended,
840 struct page **page)
Mel Gorman56de7262010-05-24 14:32:30 -0700841{
Shaohua Li03dd8fe2012-10-08 16:32:27 -0700842 unsigned long ret;
Mel Gorman56de7262010-05-24 14:32:30 -0700843 struct compact_control cc = {
844 .nr_freepages = 0,
845 .nr_migratepages = 0,
846 .order = order,
847 .migratetype = allocflags_to_migratetype(gfp_mask),
848 .zone = zone,
Mel Gorman77f1fe62011-01-13 15:45:57 -0800849 .sync = sync,
Mel Gormanc74068b2012-10-08 16:29:12 -0700850 .page = page,
Mel Gorman56de7262010-05-24 14:32:30 -0700851 };
852 INIT_LIST_HEAD(&cc.freepages);
853 INIT_LIST_HEAD(&cc.migratepages);
854
Shaohua Li03dd8fe2012-10-08 16:32:27 -0700855 ret = compact_zone(zone, &cc);
856
857 VM_BUG_ON(!list_empty(&cc.freepages));
858 VM_BUG_ON(!list_empty(&cc.migratepages));
859
860 *contended = cc.contended;
861 return ret;
Mel Gorman56de7262010-05-24 14:32:30 -0700862}
863
Mel Gorman5e771902010-05-24 14:32:31 -0700864int sysctl_extfrag_threshold = 500;
865
Mel Gorman56de7262010-05-24 14:32:30 -0700866/**
867 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
868 * @zonelist: The zonelist used for the current allocation
869 * @order: The order of the current allocation
870 * @gfp_mask: The GFP mask of the current allocation
871 * @nodemask: The allowed nodes to allocate from
Mel Gorman77f1fe62011-01-13 15:45:57 -0800872 * @sync: Whether migration is synchronous or not
Mel Gorman56de7262010-05-24 14:32:30 -0700873 *
874 * This is the main entry point for direct page compaction.
875 */
876unsigned long try_to_compact_pages(struct zonelist *zonelist,
Mel Gorman77f1fe62011-01-13 15:45:57 -0800877 int order, gfp_t gfp_mask, nodemask_t *nodemask,
Mel Gormanc74068b2012-10-08 16:29:12 -0700878 bool sync, bool *contended, struct page **page)
Mel Gorman56de7262010-05-24 14:32:30 -0700879{
880 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
881 int may_enter_fs = gfp_mask & __GFP_FS;
882 int may_perform_io = gfp_mask & __GFP_IO;
Mel Gorman56de7262010-05-24 14:32:30 -0700883 struct zoneref *z;
884 struct zone *zone;
885 int rc = COMPACT_SKIPPED;
Bartlomiej Zolnierkiewicz850bf192012-10-08 16:32:05 -0700886 int alloc_flags = 0;
Mel Gorman56de7262010-05-24 14:32:30 -0700887
Mel Gormanbc337a92012-10-08 16:29:09 -0700888 /* Check if the GFP flags allow compaction */
Andrea Arcangelic5a73c32011-01-13 15:47:11 -0800889 if (!order || !may_enter_fs || !may_perform_io)
Mel Gorman56de7262010-05-24 14:32:30 -0700890 return rc;
891
892 count_vm_event(COMPACTSTALL);
893
Bartlomiej Zolnierkiewicz850bf192012-10-08 16:32:05 -0700894#ifdef CONFIG_CMA
895 if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
896 alloc_flags |= ALLOC_CMA;
897#endif
Mel Gorman56de7262010-05-24 14:32:30 -0700898 /* Compact each zone in the list */
899 for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
900 nodemask) {
Mel Gorman56de7262010-05-24 14:32:30 -0700901 int status;
902
Mel Gorman0ba33882012-08-21 16:16:17 -0700903 status = compact_zone_order(zone, order, gfp_mask, sync,
Mel Gormanc74068b2012-10-08 16:29:12 -0700904 contended, page);
Mel Gorman56de7262010-05-24 14:32:30 -0700905 rc = max(status, rc);
906
Mel Gorman3e7d3442011-01-13 15:45:56 -0800907 /* If a normal allocation would succeed, stop compacting */
Bartlomiej Zolnierkiewicz850bf192012-10-08 16:32:05 -0700908 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
909 alloc_flags))
Mel Gorman56de7262010-05-24 14:32:30 -0700910 break;
911 }
912
913 return rc;
914}
915
916
Mel Gorman76ab0f52010-05-24 14:32:28 -0700917/* Compact all zones within a node */
Rik van Riel7be62de2012-03-21 16:33:52 -0700918static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
Mel Gorman76ab0f52010-05-24 14:32:28 -0700919{
920 int zoneid;
Mel Gorman76ab0f52010-05-24 14:32:28 -0700921 struct zone *zone;
922
Mel Gorman76ab0f52010-05-24 14:32:28 -0700923 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
Mel Gorman76ab0f52010-05-24 14:32:28 -0700924
925 zone = &pgdat->node_zones[zoneid];
926 if (!populated_zone(zone))
927 continue;
928
Rik van Riel7be62de2012-03-21 16:33:52 -0700929 cc->nr_freepages = 0;
930 cc->nr_migratepages = 0;
931 cc->zone = zone;
932 INIT_LIST_HEAD(&cc->freepages);
933 INIT_LIST_HEAD(&cc->migratepages);
Mel Gorman76ab0f52010-05-24 14:32:28 -0700934
Dan Carpenteraad6ec32012-03-21 16:33:54 -0700935 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
Rik van Riel7be62de2012-03-21 16:33:52 -0700936 compact_zone(zone, cc);
Mel Gorman76ab0f52010-05-24 14:32:28 -0700937
Rik van Rielaff62242012-03-21 16:33:52 -0700938 if (cc->order > 0) {
939 int ok = zone_watermark_ok(zone, cc->order,
940 low_wmark_pages(zone), 0, 0);
Minchan Kim57bb21c2012-08-21 16:16:03 -0700941 if (ok && cc->order >= zone->compact_order_failed)
Rik van Rielaff62242012-03-21 16:33:52 -0700942 zone->compact_order_failed = cc->order + 1;
943 /* Currently async compaction is never deferred. */
944 else if (!ok && cc->sync)
945 defer_compaction(zone, cc->order);
946 }
947
Rik van Riel7be62de2012-03-21 16:33:52 -0700948 VM_BUG_ON(!list_empty(&cc->freepages));
949 VM_BUG_ON(!list_empty(&cc->migratepages));
Mel Gorman76ab0f52010-05-24 14:32:28 -0700950 }
951
952 return 0;
953}
954
Rik van Riel7be62de2012-03-21 16:33:52 -0700955int compact_pgdat(pg_data_t *pgdat, int order)
956{
957 struct compact_control cc = {
958 .order = order,
959 .sync = false,
Mel Gormanc74068b2012-10-08 16:29:12 -0700960 .page = NULL,
Rik van Riel7be62de2012-03-21 16:33:52 -0700961 };
962
963 return __compact_pgdat(pgdat, &cc);
964}
965
966static int compact_node(int nid)
967{
Rik van Riel7be62de2012-03-21 16:33:52 -0700968 struct compact_control cc = {
969 .order = -1,
970 .sync = true,
Mel Gormanc74068b2012-10-08 16:29:12 -0700971 .page = NULL,
Rik van Riel7be62de2012-03-21 16:33:52 -0700972 };
973
Hugh Dickins8575ec22012-03-21 16:33:53 -0700974 return __compact_pgdat(NODE_DATA(nid), &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -0700975}
976
Mel Gorman76ab0f52010-05-24 14:32:28 -0700977/* Compact all nodes in the system */
Jason Liuc0b96522013-01-11 14:31:47 -0800978static void compact_nodes(void)
Mel Gorman76ab0f52010-05-24 14:32:28 -0700979{
980 int nid;
981
Hugh Dickins8575ec22012-03-21 16:33:53 -0700982 /* Flush pending updates to the LRU lists */
983 lru_add_drain_all();
984
Mel Gorman76ab0f52010-05-24 14:32:28 -0700985 for_each_online_node(nid)
986 compact_node(nid);
Mel Gorman76ab0f52010-05-24 14:32:28 -0700987}
988
989/* The written value is actually unused, all memory is compacted */
990int sysctl_compact_memory;
991
992/* This is the entry point for compacting all nodes via /proc/sys/vm */
993int sysctl_compaction_handler(struct ctl_table *table, int write,
994 void __user *buffer, size_t *length, loff_t *ppos)
995{
996 if (write)
Jason Liuc0b96522013-01-11 14:31:47 -0800997 compact_nodes();
Mel Gorman76ab0f52010-05-24 14:32:28 -0700998
999 return 0;
1000}
Mel Gormaned4a6d72010-05-24 14:32:29 -07001001
Mel Gorman5e771902010-05-24 14:32:31 -07001002int sysctl_extfrag_handler(struct ctl_table *table, int write,
1003 void __user *buffer, size_t *length, loff_t *ppos)
1004{
1005 proc_dointvec_minmax(table, write, buffer, length, ppos);
1006
1007 return 0;
1008}
1009
Mel Gormaned4a6d72010-05-24 14:32:29 -07001010#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
Kay Sievers10fbcf42011-12-21 14:48:43 -08001011ssize_t sysfs_compact_node(struct device *dev,
1012 struct device_attribute *attr,
Mel Gormaned4a6d72010-05-24 14:32:29 -07001013 const char *buf, size_t count)
1014{
Hugh Dickins8575ec22012-03-21 16:33:53 -07001015 int nid = dev->id;
1016
1017 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
1018 /* Flush pending updates to the LRU lists */
1019 lru_add_drain_all();
1020
1021 compact_node(nid);
1022 }
Mel Gormaned4a6d72010-05-24 14:32:29 -07001023
1024 return count;
1025}
Kay Sievers10fbcf42011-12-21 14:48:43 -08001026static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001027
1028int compaction_register_node(struct node *node)
1029{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001030 return device_create_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001031}
1032
1033void compaction_unregister_node(struct node *node)
1034{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001035 return device_remove_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001036}
1037#endif /* CONFIG_SYSFS && CONFIG_NUMA */
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +01001038
1039#endif /* CONFIG_COMPACTION */