Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 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> |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 15 | #include <linux/sysctl.h> |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 16 | #include <linux/sysfs.h> |
Rafael Aquini | bf6bddf | 2012-12-11 16:02:42 -0800 | [diff] [blame] | 17 | #include <linux/balloon_compaction.h> |
Minchan Kim | 194159f | 2013-02-22 16:33:58 -0800 | [diff] [blame] | 18 | #include <linux/page-isolation.h> |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 19 | #include "internal.h" |
| 20 | |
Minchan Kim | 010fc29 | 2012-12-20 15:05:06 -0800 | [diff] [blame] | 21 | #ifdef CONFIG_COMPACTION |
| 22 | static inline void count_compact_event(enum vm_event_item item) |
| 23 | { |
| 24 | count_vm_event(item); |
| 25 | } |
| 26 | |
| 27 | static inline void count_compact_events(enum vm_event_item item, long delta) |
| 28 | { |
| 29 | count_vm_events(item, delta); |
| 30 | } |
| 31 | #else |
| 32 | #define count_compact_event(item) do { } while (0) |
| 33 | #define count_compact_events(item, delta) do { } while (0) |
| 34 | #endif |
| 35 | |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 36 | #if defined CONFIG_COMPACTION || defined CONFIG_CMA |
Joonsoo Kim | 16c4a09 | 2015-02-11 15:27:01 -0800 | [diff] [blame^] | 37 | #ifdef CONFIG_TRACEPOINTS |
| 38 | static const char *const compaction_status_string[] = { |
| 39 | "deferred", |
| 40 | "skipped", |
| 41 | "continue", |
| 42 | "partial", |
| 43 | "complete", |
| 44 | }; |
| 45 | #endif |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 46 | |
Mel Gorman | b7aba69 | 2011-01-13 15:45:54 -0800 | [diff] [blame] | 47 | #define CREATE_TRACE_POINTS |
| 48 | #include <trace/events/compaction.h> |
| 49 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 50 | static unsigned long release_freepages(struct list_head *freelist) |
| 51 | { |
| 52 | struct page *page, *next; |
Vlastimil Babka | 6bace09 | 2014-12-10 15:43:31 -0800 | [diff] [blame] | 53 | unsigned long high_pfn = 0; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 54 | |
| 55 | list_for_each_entry_safe(page, next, freelist, lru) { |
Vlastimil Babka | 6bace09 | 2014-12-10 15:43:31 -0800 | [diff] [blame] | 56 | unsigned long pfn = page_to_pfn(page); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 57 | list_del(&page->lru); |
| 58 | __free_page(page); |
Vlastimil Babka | 6bace09 | 2014-12-10 15:43:31 -0800 | [diff] [blame] | 59 | if (pfn > high_pfn) |
| 60 | high_pfn = pfn; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Vlastimil Babka | 6bace09 | 2014-12-10 15:43:31 -0800 | [diff] [blame] | 63 | return high_pfn; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 66 | static void map_pages(struct list_head *list) |
| 67 | { |
| 68 | struct page *page; |
| 69 | |
| 70 | list_for_each_entry(page, list, lru) { |
| 71 | arch_alloc_page(page, 0); |
| 72 | kernel_map_pages(page, 1, 1); |
| 73 | } |
| 74 | } |
| 75 | |
Michal Nazarewicz | 47118af | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 76 | static inline bool migrate_async_suitable(int migratetype) |
| 77 | { |
| 78 | return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE; |
| 79 | } |
| 80 | |
Vlastimil Babka | 7d49d88 | 2014-10-09 15:27:11 -0700 | [diff] [blame] | 81 | /* |
| 82 | * Check that the whole (or subset of) a pageblock given by the interval of |
| 83 | * [start_pfn, end_pfn) is valid and within the same zone, before scanning it |
| 84 | * with the migration of free compaction scanner. The scanners then need to |
| 85 | * use only pfn_valid_within() check for arches that allow holes within |
| 86 | * pageblocks. |
| 87 | * |
| 88 | * Return struct page pointer of start_pfn, or NULL if checks were not passed. |
| 89 | * |
| 90 | * It's possible on some configurations to have a setup like node0 node1 node0 |
| 91 | * i.e. it's possible that all pages within a zones range of pages do not |
| 92 | * belong to a single zone. We assume that a border between node0 and node1 |
| 93 | * can occur within a single pageblock, but not a node0 node1 node0 |
| 94 | * interleaving within a single pageblock. It is therefore sufficient to check |
| 95 | * the first and last page of a pageblock and avoid checking each individual |
| 96 | * page in a pageblock. |
| 97 | */ |
| 98 | static struct page *pageblock_pfn_to_page(unsigned long start_pfn, |
| 99 | unsigned long end_pfn, struct zone *zone) |
| 100 | { |
| 101 | struct page *start_page; |
| 102 | struct page *end_page; |
| 103 | |
| 104 | /* end_pfn is one past the range we are checking */ |
| 105 | end_pfn--; |
| 106 | |
| 107 | if (!pfn_valid(start_pfn) || !pfn_valid(end_pfn)) |
| 108 | return NULL; |
| 109 | |
| 110 | start_page = pfn_to_page(start_pfn); |
| 111 | |
| 112 | if (page_zone(start_page) != zone) |
| 113 | return NULL; |
| 114 | |
| 115 | end_page = pfn_to_page(end_pfn); |
| 116 | |
| 117 | /* This gives a shorter code than deriving page_zone(end_page) */ |
| 118 | if (page_zone_id(start_page) != page_zone_id(end_page)) |
| 119 | return NULL; |
| 120 | |
| 121 | return start_page; |
| 122 | } |
| 123 | |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 124 | #ifdef CONFIG_COMPACTION |
| 125 | /* Returns true if the pageblock should be scanned for pages to isolate. */ |
| 126 | static inline bool isolation_suitable(struct compact_control *cc, |
| 127 | struct page *page) |
| 128 | { |
| 129 | if (cc->ignore_skip_hint) |
| 130 | return true; |
| 131 | |
| 132 | return !get_pageblock_skip(page); |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * This function is called to clear all cached information on pageblocks that |
| 137 | * should be skipped for page isolation when the migrate and free page scanner |
| 138 | * meet. |
| 139 | */ |
Mel Gorman | 6299702 | 2012-10-08 16:32:47 -0700 | [diff] [blame] | 140 | static void __reset_isolation_suitable(struct zone *zone) |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 141 | { |
| 142 | unsigned long start_pfn = zone->zone_start_pfn; |
Cody P Schafer | 108bcc9 | 2013-02-22 16:35:23 -0800 | [diff] [blame] | 143 | unsigned long end_pfn = zone_end_pfn(zone); |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 144 | unsigned long pfn; |
| 145 | |
David Rientjes | 35979ef | 2014-06-04 16:08:27 -0700 | [diff] [blame] | 146 | zone->compact_cached_migrate_pfn[0] = start_pfn; |
| 147 | zone->compact_cached_migrate_pfn[1] = start_pfn; |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 148 | zone->compact_cached_free_pfn = end_pfn; |
Mel Gorman | 6299702 | 2012-10-08 16:32:47 -0700 | [diff] [blame] | 149 | zone->compact_blockskip_flush = false; |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 150 | |
| 151 | /* Walk the zone and mark every pageblock as suitable for isolation */ |
| 152 | for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) { |
| 153 | struct page *page; |
| 154 | |
| 155 | cond_resched(); |
| 156 | |
| 157 | if (!pfn_valid(pfn)) |
| 158 | continue; |
| 159 | |
| 160 | page = pfn_to_page(pfn); |
| 161 | if (zone != page_zone(page)) |
| 162 | continue; |
| 163 | |
| 164 | clear_pageblock_skip(page); |
| 165 | } |
| 166 | } |
| 167 | |
Mel Gorman | 6299702 | 2012-10-08 16:32:47 -0700 | [diff] [blame] | 168 | void reset_isolation_suitable(pg_data_t *pgdat) |
| 169 | { |
| 170 | int zoneid; |
| 171 | |
| 172 | for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { |
| 173 | struct zone *zone = &pgdat->node_zones[zoneid]; |
| 174 | if (!populated_zone(zone)) |
| 175 | continue; |
| 176 | |
| 177 | /* Only flush if a full compaction finished recently */ |
| 178 | if (zone->compact_blockskip_flush) |
| 179 | __reset_isolation_suitable(zone); |
| 180 | } |
| 181 | } |
| 182 | |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 183 | /* |
| 184 | * If no pages were isolated then mark this pageblock to be skipped in the |
Mel Gorman | 6299702 | 2012-10-08 16:32:47 -0700 | [diff] [blame] | 185 | * future. The information is later cleared by __reset_isolation_suitable(). |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 186 | */ |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 187 | static void update_pageblock_skip(struct compact_control *cc, |
| 188 | struct page *page, unsigned long nr_isolated, |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 189 | bool migrate_scanner) |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 190 | { |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 191 | struct zone *zone = cc->zone; |
David Rientjes | 35979ef | 2014-06-04 16:08:27 -0700 | [diff] [blame] | 192 | unsigned long pfn; |
Joonsoo Kim | 6815bf3 | 2013-12-18 17:08:52 -0800 | [diff] [blame] | 193 | |
| 194 | if (cc->ignore_skip_hint) |
| 195 | return; |
| 196 | |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 197 | if (!page) |
| 198 | return; |
| 199 | |
David Rientjes | 35979ef | 2014-06-04 16:08:27 -0700 | [diff] [blame] | 200 | if (nr_isolated) |
| 201 | return; |
| 202 | |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 203 | set_pageblock_skip(page); |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 204 | |
David Rientjes | 35979ef | 2014-06-04 16:08:27 -0700 | [diff] [blame] | 205 | pfn = page_to_pfn(page); |
| 206 | |
| 207 | /* Update where async and sync compaction should restart */ |
| 208 | if (migrate_scanner) { |
David Rientjes | 35979ef | 2014-06-04 16:08:27 -0700 | [diff] [blame] | 209 | if (pfn > zone->compact_cached_migrate_pfn[0]) |
| 210 | zone->compact_cached_migrate_pfn[0] = pfn; |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 211 | if (cc->mode != MIGRATE_ASYNC && |
| 212 | pfn > zone->compact_cached_migrate_pfn[1]) |
David Rientjes | 35979ef | 2014-06-04 16:08:27 -0700 | [diff] [blame] | 213 | zone->compact_cached_migrate_pfn[1] = pfn; |
| 214 | } else { |
David Rientjes | 35979ef | 2014-06-04 16:08:27 -0700 | [diff] [blame] | 215 | if (pfn < zone->compact_cached_free_pfn) |
| 216 | zone->compact_cached_free_pfn = pfn; |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 217 | } |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 218 | } |
| 219 | #else |
| 220 | static inline bool isolation_suitable(struct compact_control *cc, |
| 221 | struct page *page) |
| 222 | { |
| 223 | return true; |
| 224 | } |
| 225 | |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 226 | static void update_pageblock_skip(struct compact_control *cc, |
| 227 | struct page *page, unsigned long nr_isolated, |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 228 | bool migrate_scanner) |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 229 | { |
| 230 | } |
| 231 | #endif /* CONFIG_COMPACTION */ |
| 232 | |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 233 | /* |
| 234 | * Compaction requires the taking of some coarse locks that are potentially |
| 235 | * very heavily contended. For async compaction, back out if the lock cannot |
| 236 | * be taken immediately. For sync compaction, spin on the lock if needed. |
| 237 | * |
| 238 | * Returns true if the lock is held |
| 239 | * Returns false if the lock is not held and compaction should abort |
| 240 | */ |
| 241 | static bool compact_trylock_irqsave(spinlock_t *lock, unsigned long *flags, |
| 242 | struct compact_control *cc) |
Mel Gorman | 2a1402a | 2012-10-08 16:32:33 -0700 | [diff] [blame] | 243 | { |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 244 | if (cc->mode == MIGRATE_ASYNC) { |
| 245 | if (!spin_trylock_irqsave(lock, *flags)) { |
| 246 | cc->contended = COMPACT_CONTENDED_LOCK; |
| 247 | return false; |
| 248 | } |
| 249 | } else { |
| 250 | spin_lock_irqsave(lock, *flags); |
| 251 | } |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame] | 252 | |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 253 | return true; |
Mel Gorman | 2a1402a | 2012-10-08 16:32:33 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 256 | /* |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 257 | * Compaction requires the taking of some coarse locks that are potentially |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 258 | * very heavily contended. The lock should be periodically unlocked to avoid |
| 259 | * having disabled IRQs for a long time, even when there is nobody waiting on |
| 260 | * the lock. It might also be that allowing the IRQs will result in |
| 261 | * need_resched() becoming true. If scheduling is needed, async compaction |
| 262 | * aborts. Sync compaction schedules. |
| 263 | * Either compaction type will also abort if a fatal signal is pending. |
| 264 | * In either case if the lock was locked, it is dropped and not regained. |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 265 | * |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 266 | * Returns true if compaction should abort due to fatal signal pending, or |
| 267 | * async compaction due to need_resched() |
| 268 | * Returns false when compaction can continue (sync compaction might have |
| 269 | * scheduled) |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 270 | */ |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 271 | static bool compact_unlock_should_abort(spinlock_t *lock, |
| 272 | unsigned long flags, bool *locked, struct compact_control *cc) |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 273 | { |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 274 | if (*locked) { |
| 275 | spin_unlock_irqrestore(lock, flags); |
| 276 | *locked = false; |
| 277 | } |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame] | 278 | |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 279 | if (fatal_signal_pending(current)) { |
| 280 | cc->contended = COMPACT_CONTENDED_SCHED; |
| 281 | return true; |
| 282 | } |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 283 | |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 284 | if (need_resched()) { |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 285 | if (cc->mode == MIGRATE_ASYNC) { |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 286 | cc->contended = COMPACT_CONTENDED_SCHED; |
| 287 | return true; |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 288 | } |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 289 | cond_resched(); |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 292 | return false; |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Vlastimil Babka | be97657 | 2014-06-04 16:10:41 -0700 | [diff] [blame] | 295 | /* |
| 296 | * Aside from avoiding lock contention, compaction also periodically checks |
| 297 | * need_resched() and either schedules in sync compaction or aborts async |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 298 | * compaction. This is similar to what compact_unlock_should_abort() does, but |
Vlastimil Babka | be97657 | 2014-06-04 16:10:41 -0700 | [diff] [blame] | 299 | * is used where no lock is concerned. |
| 300 | * |
| 301 | * Returns false when no scheduling was needed, or sync compaction scheduled. |
| 302 | * Returns true when async compaction should abort. |
| 303 | */ |
| 304 | static inline bool compact_should_abort(struct compact_control *cc) |
| 305 | { |
| 306 | /* async compaction aborts if contended */ |
| 307 | if (need_resched()) { |
| 308 | if (cc->mode == MIGRATE_ASYNC) { |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame] | 309 | cc->contended = COMPACT_CONTENDED_SCHED; |
Vlastimil Babka | be97657 | 2014-06-04 16:10:41 -0700 | [diff] [blame] | 310 | return true; |
| 311 | } |
| 312 | |
| 313 | cond_resched(); |
| 314 | } |
| 315 | |
| 316 | return false; |
| 317 | } |
| 318 | |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 319 | /* Returns true if the page is within a block suitable for migration to */ |
| 320 | static bool suitable_migration_target(struct page *page) |
| 321 | { |
Joonsoo Kim | 7d348b9 | 2014-04-07 15:37:03 -0700 | [diff] [blame] | 322 | /* If the page is a large free page, then disallow migration */ |
Vlastimil Babka | 99c0fd5 | 2014-10-09 15:27:23 -0700 | [diff] [blame] | 323 | if (PageBuddy(page)) { |
| 324 | /* |
| 325 | * We are checking page_order without zone->lock taken. But |
| 326 | * the only small danger is that we skip a potentially suitable |
| 327 | * pageblock, so it's not worth to check order for valid range. |
| 328 | */ |
| 329 | if (page_order_unsafe(page) >= pageblock_order) |
| 330 | return false; |
| 331 | } |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 332 | |
| 333 | /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */ |
Joonsoo Kim | 7d348b9 | 2014-04-07 15:37:03 -0700 | [diff] [blame] | 334 | if (migrate_async_suitable(get_pageblock_migratetype(page))) |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 335 | return true; |
| 336 | |
| 337 | /* Otherwise skip the block */ |
| 338 | return false; |
| 339 | } |
| 340 | |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 341 | /* |
Jerome Marchand | 9e4be47 | 2013-11-12 15:07:12 -0800 | [diff] [blame] | 342 | * Isolate free pages onto a private freelist. If @strict is true, will abort |
| 343 | * returning 0 on any invalid PFNs or non-free pages inside of the pageblock |
| 344 | * (even though it may still end up isolating some pages). |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 345 | */ |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 346 | static unsigned long isolate_freepages_block(struct compact_control *cc, |
Vlastimil Babka | e14c720 | 2014-10-09 15:27:20 -0700 | [diff] [blame] | 347 | unsigned long *start_pfn, |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 348 | unsigned long end_pfn, |
| 349 | struct list_head *freelist, |
| 350 | bool strict) |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 351 | { |
Mel Gorman | b7aba69 | 2011-01-13 15:45:54 -0800 | [diff] [blame] | 352 | int nr_scanned = 0, total_isolated = 0; |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 353 | struct page *cursor, *valid_page = NULL; |
Xiubo Li | b8b2d82 | 2014-10-09 15:28:21 -0700 | [diff] [blame] | 354 | unsigned long flags = 0; |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 355 | bool locked = false; |
Vlastimil Babka | e14c720 | 2014-10-09 15:27:20 -0700 | [diff] [blame] | 356 | unsigned long blockpfn = *start_pfn; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 357 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 358 | cursor = pfn_to_page(blockpfn); |
| 359 | |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 360 | /* Isolate free pages. */ |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 361 | for (; blockpfn < end_pfn; blockpfn++, cursor++) { |
| 362 | int isolated, i; |
| 363 | struct page *page = cursor; |
| 364 | |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 365 | /* |
| 366 | * Periodically drop the lock (if held) regardless of its |
| 367 | * contention, to give chance to IRQs. Abort if fatal signal |
| 368 | * pending or async compaction detects need_resched() |
| 369 | */ |
| 370 | if (!(blockpfn % SWAP_CLUSTER_MAX) |
| 371 | && compact_unlock_should_abort(&cc->zone->lock, flags, |
| 372 | &locked, cc)) |
| 373 | break; |
| 374 | |
Mel Gorman | b7aba69 | 2011-01-13 15:45:54 -0800 | [diff] [blame] | 375 | nr_scanned++; |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 376 | if (!pfn_valid_within(blockpfn)) |
Laura Abbott | 2af120b | 2014-03-10 15:49:44 -0700 | [diff] [blame] | 377 | goto isolate_fail; |
| 378 | |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 379 | if (!valid_page) |
| 380 | valid_page = page; |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 381 | if (!PageBuddy(page)) |
Laura Abbott | 2af120b | 2014-03-10 15:49:44 -0700 | [diff] [blame] | 382 | goto isolate_fail; |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 383 | |
| 384 | /* |
Vlastimil Babka | 69b7189 | 2014-10-09 15:27:18 -0700 | [diff] [blame] | 385 | * If we already hold the lock, we can skip some rechecking. |
| 386 | * Note that if we hold the lock now, checked_pageblock was |
| 387 | * already set in some previous iteration (or strict is true), |
| 388 | * so it is correct to skip the suitable migration target |
| 389 | * recheck as well. |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 390 | */ |
Vlastimil Babka | 69b7189 | 2014-10-09 15:27:18 -0700 | [diff] [blame] | 391 | if (!locked) { |
| 392 | /* |
| 393 | * The zone lock must be held to isolate freepages. |
| 394 | * Unfortunately this is a very coarse lock and can be |
| 395 | * heavily contended if there are parallel allocations |
| 396 | * or parallel compactions. For async compaction do not |
| 397 | * spin on the lock and we acquire the lock as late as |
| 398 | * possible. |
| 399 | */ |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 400 | locked = compact_trylock_irqsave(&cc->zone->lock, |
| 401 | &flags, cc); |
Vlastimil Babka | 69b7189 | 2014-10-09 15:27:18 -0700 | [diff] [blame] | 402 | if (!locked) |
| 403 | break; |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 404 | |
Vlastimil Babka | 69b7189 | 2014-10-09 15:27:18 -0700 | [diff] [blame] | 405 | /* Recheck this is a buddy page under lock */ |
| 406 | if (!PageBuddy(page)) |
| 407 | goto isolate_fail; |
| 408 | } |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 409 | |
| 410 | /* Found a free page, break it into order-0 pages */ |
| 411 | isolated = split_free_page(page); |
| 412 | total_isolated += isolated; |
| 413 | for (i = 0; i < isolated; i++) { |
| 414 | list_add(&page->lru, freelist); |
| 415 | page++; |
| 416 | } |
| 417 | |
| 418 | /* If a page was split, advance to the end of it */ |
| 419 | if (isolated) { |
| 420 | blockpfn += isolated - 1; |
| 421 | cursor += isolated - 1; |
Laura Abbott | 2af120b | 2014-03-10 15:49:44 -0700 | [diff] [blame] | 422 | continue; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 423 | } |
Laura Abbott | 2af120b | 2014-03-10 15:49:44 -0700 | [diff] [blame] | 424 | |
| 425 | isolate_fail: |
| 426 | if (strict) |
| 427 | break; |
| 428 | else |
| 429 | continue; |
| 430 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Vlastimil Babka | e14c720 | 2014-10-09 15:27:20 -0700 | [diff] [blame] | 433 | /* Record how far we have got within the block */ |
| 434 | *start_pfn = blockpfn; |
| 435 | |
Mel Gorman | b7aba69 | 2011-01-13 15:45:54 -0800 | [diff] [blame] | 436 | trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated); |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 437 | |
| 438 | /* |
| 439 | * If strict isolation is requested by CMA then check that all the |
| 440 | * pages requested were isolated. If there were any failures, 0 is |
| 441 | * returned and CMA will fail. |
| 442 | */ |
Laura Abbott | 2af120b | 2014-03-10 15:49:44 -0700 | [diff] [blame] | 443 | if (strict && blockpfn < end_pfn) |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 444 | total_isolated = 0; |
| 445 | |
| 446 | if (locked) |
| 447 | spin_unlock_irqrestore(&cc->zone->lock, flags); |
| 448 | |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 449 | /* Update the pageblock-skip if the whole pageblock was scanned */ |
| 450 | if (blockpfn == end_pfn) |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 451 | update_pageblock_skip(cc, valid_page, total_isolated, false); |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 452 | |
Minchan Kim | 010fc29 | 2012-12-20 15:05:06 -0800 | [diff] [blame] | 453 | count_compact_events(COMPACTFREE_SCANNED, nr_scanned); |
Mel Gorman | 397487d | 2012-10-19 12:00:10 +0100 | [diff] [blame] | 454 | if (total_isolated) |
Minchan Kim | 010fc29 | 2012-12-20 15:05:06 -0800 | [diff] [blame] | 455 | count_compact_events(COMPACTISOLATED, total_isolated); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 456 | return total_isolated; |
| 457 | } |
| 458 | |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 459 | /** |
| 460 | * isolate_freepages_range() - isolate free pages. |
| 461 | * @start_pfn: The first PFN to start isolating. |
| 462 | * @end_pfn: The one-past-last PFN. |
| 463 | * |
| 464 | * Non-free pages, invalid PFNs, or zone boundaries within the |
| 465 | * [start_pfn, end_pfn) range are considered errors, cause function to |
| 466 | * undo its actions and return zero. |
| 467 | * |
| 468 | * Otherwise, function returns one-past-the-last PFN of isolated page |
| 469 | * (which may be greater then end_pfn if end fell in a middle of |
| 470 | * a free page). |
| 471 | */ |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 472 | unsigned long |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 473 | isolate_freepages_range(struct compact_control *cc, |
| 474 | unsigned long start_pfn, unsigned long end_pfn) |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 475 | { |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 476 | unsigned long isolated, pfn, block_end_pfn; |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 477 | LIST_HEAD(freelist); |
| 478 | |
Vlastimil Babka | 7d49d88 | 2014-10-09 15:27:11 -0700 | [diff] [blame] | 479 | pfn = start_pfn; |
| 480 | block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages); |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 481 | |
Vlastimil Babka | 7d49d88 | 2014-10-09 15:27:11 -0700 | [diff] [blame] | 482 | for (; pfn < end_pfn; pfn += isolated, |
| 483 | block_end_pfn += pageblock_nr_pages) { |
Vlastimil Babka | e14c720 | 2014-10-09 15:27:20 -0700 | [diff] [blame] | 484 | /* Protect pfn from changing by isolate_freepages_block */ |
| 485 | unsigned long isolate_start_pfn = pfn; |
Vlastimil Babka | 7d49d88 | 2014-10-09 15:27:11 -0700 | [diff] [blame] | 486 | |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 487 | block_end_pfn = min(block_end_pfn, end_pfn); |
| 488 | |
Joonsoo Kim | 5842001 | 2014-11-13 15:19:07 -0800 | [diff] [blame] | 489 | /* |
| 490 | * pfn could pass the block_end_pfn if isolated freepage |
| 491 | * is more than pageblock order. In this case, we adjust |
| 492 | * scanning range to right one. |
| 493 | */ |
| 494 | if (pfn >= block_end_pfn) { |
| 495 | block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages); |
| 496 | block_end_pfn = min(block_end_pfn, end_pfn); |
| 497 | } |
| 498 | |
Vlastimil Babka | 7d49d88 | 2014-10-09 15:27:11 -0700 | [diff] [blame] | 499 | if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone)) |
| 500 | break; |
| 501 | |
Vlastimil Babka | e14c720 | 2014-10-09 15:27:20 -0700 | [diff] [blame] | 502 | isolated = isolate_freepages_block(cc, &isolate_start_pfn, |
| 503 | block_end_pfn, &freelist, true); |
Michal Nazarewicz | 85aa125 | 2012-01-30 13:24:03 +0100 | [diff] [blame] | 504 | |
| 505 | /* |
| 506 | * In strict mode, isolate_freepages_block() returns 0 if |
| 507 | * there are any holes in the block (ie. invalid PFNs or |
| 508 | * non-free pages). |
| 509 | */ |
| 510 | if (!isolated) |
| 511 | break; |
| 512 | |
| 513 | /* |
| 514 | * If we managed to isolate pages, it is always (1 << n) * |
| 515 | * pageblock_nr_pages for some non-negative n. (Max order |
| 516 | * page may span two pageblocks). |
| 517 | */ |
| 518 | } |
| 519 | |
| 520 | /* split_free_page does not map the pages */ |
| 521 | map_pages(&freelist); |
| 522 | |
| 523 | if (pfn < end_pfn) { |
| 524 | /* Loop terminated early, cleanup. */ |
| 525 | release_freepages(&freelist); |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | /* We don't use freelists for anything. */ |
| 530 | return pfn; |
| 531 | } |
| 532 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 533 | /* Update the number of anon and file isolated pages in the zone */ |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 534 | static void acct_isolated(struct zone *zone, struct compact_control *cc) |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 535 | { |
| 536 | struct page *page; |
Minchan Kim | b9e84ac | 2011-10-31 17:06:44 -0700 | [diff] [blame] | 537 | unsigned int count[2] = { 0, }; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 538 | |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 539 | if (list_empty(&cc->migratepages)) |
| 540 | return; |
| 541 | |
Minchan Kim | b9e84ac | 2011-10-31 17:06:44 -0700 | [diff] [blame] | 542 | list_for_each_entry(page, &cc->migratepages, lru) |
| 543 | count[!!page_is_file_cache(page)]++; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 544 | |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 545 | mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]); |
| 546 | mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | /* Similar to reclaim, but different enough that they don't share logic */ |
| 550 | static bool too_many_isolated(struct zone *zone) |
| 551 | { |
Minchan Kim | bc69304 | 2010-09-09 16:38:00 -0700 | [diff] [blame] | 552 | unsigned long active, inactive, isolated; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 553 | |
| 554 | inactive = zone_page_state(zone, NR_INACTIVE_FILE) + |
| 555 | zone_page_state(zone, NR_INACTIVE_ANON); |
Minchan Kim | bc69304 | 2010-09-09 16:38:00 -0700 | [diff] [blame] | 556 | active = zone_page_state(zone, NR_ACTIVE_FILE) + |
| 557 | zone_page_state(zone, NR_ACTIVE_ANON); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 558 | isolated = zone_page_state(zone, NR_ISOLATED_FILE) + |
| 559 | zone_page_state(zone, NR_ISOLATED_ANON); |
| 560 | |
Minchan Kim | bc69304 | 2010-09-09 16:38:00 -0700 | [diff] [blame] | 561 | return isolated > (inactive + active) / 2; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 562 | } |
| 563 | |
Michal Nazarewicz | 2fe86e0 | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 564 | /** |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 565 | * isolate_migratepages_block() - isolate all migrate-able pages within |
| 566 | * a single pageblock |
Michal Nazarewicz | 2fe86e0 | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 567 | * @cc: Compaction control structure. |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 568 | * @low_pfn: The first PFN to isolate |
| 569 | * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock |
| 570 | * @isolate_mode: Isolation mode to be used. |
Michal Nazarewicz | 2fe86e0 | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 571 | * |
| 572 | * Isolate all pages that can be migrated from the range specified by |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 573 | * [low_pfn, end_pfn). The range is expected to be within same pageblock. |
| 574 | * Returns zero if there is a fatal signal pending, otherwise PFN of the |
| 575 | * first page that was not scanned (which may be both less, equal to or more |
| 576 | * than end_pfn). |
Michal Nazarewicz | 2fe86e0 | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 577 | * |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 578 | * The pages are isolated on cc->migratepages list (not required to be empty), |
| 579 | * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field |
| 580 | * is neither read nor updated. |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 581 | */ |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 582 | static unsigned long |
| 583 | isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, |
| 584 | unsigned long end_pfn, isolate_mode_t isolate_mode) |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 585 | { |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 586 | struct zone *zone = cc->zone; |
Mel Gorman | b7aba69 | 2011-01-13 15:45:54 -0800 | [diff] [blame] | 587 | unsigned long nr_scanned = 0, nr_isolated = 0; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 588 | struct list_head *migratelist = &cc->migratepages; |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 589 | struct lruvec *lruvec; |
Xiubo Li | b8b2d82 | 2014-10-09 15:28:21 -0700 | [diff] [blame] | 590 | unsigned long flags = 0; |
Mel Gorman | 2a1402a | 2012-10-08 16:32:33 -0700 | [diff] [blame] | 591 | bool locked = false; |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 592 | struct page *page = NULL, *valid_page = NULL; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 593 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 594 | /* |
| 595 | * Ensure that there are not too many pages isolated from the LRU |
| 596 | * list by either parallel reclaimers or compaction. If there are, |
| 597 | * delay for some time until fewer pages are isolated |
| 598 | */ |
| 599 | while (unlikely(too_many_isolated(zone))) { |
Mel Gorman | f9e35b3 | 2011-06-15 15:08:52 -0700 | [diff] [blame] | 600 | /* async migration should just abort */ |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 601 | if (cc->mode == MIGRATE_ASYNC) |
Michal Nazarewicz | 2fe86e0 | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 602 | return 0; |
Mel Gorman | f9e35b3 | 2011-06-15 15:08:52 -0700 | [diff] [blame] | 603 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 604 | congestion_wait(BLK_RW_ASYNC, HZ/10); |
| 605 | |
| 606 | if (fatal_signal_pending(current)) |
Michal Nazarewicz | 2fe86e0 | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 607 | return 0; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 608 | } |
| 609 | |
Vlastimil Babka | be97657 | 2014-06-04 16:10:41 -0700 | [diff] [blame] | 610 | if (compact_should_abort(cc)) |
| 611 | return 0; |
David Rientjes | aeef4b8 | 2014-06-04 16:08:31 -0700 | [diff] [blame] | 612 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 613 | /* Time to isolate some pages for migration */ |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 614 | for (; low_pfn < end_pfn; low_pfn++) { |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 615 | /* |
| 616 | * Periodically drop the lock (if held) regardless of its |
| 617 | * contention, to give chance to IRQs. Abort async compaction |
| 618 | * if contended. |
| 619 | */ |
| 620 | if (!(low_pfn % SWAP_CLUSTER_MAX) |
| 621 | && compact_unlock_should_abort(&zone->lru_lock, flags, |
| 622 | &locked, cc)) |
| 623 | break; |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 624 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 625 | if (!pfn_valid_within(low_pfn)) |
| 626 | continue; |
Mel Gorman | b7aba69 | 2011-01-13 15:45:54 -0800 | [diff] [blame] | 627 | nr_scanned++; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 628 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 629 | page = pfn_to_page(low_pfn); |
Mel Gorman | dc90860 | 2012-02-08 17:13:38 -0800 | [diff] [blame] | 630 | |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 631 | if (!valid_page) |
| 632 | valid_page = page; |
| 633 | |
Mel Gorman | 6c14466 | 2014-01-23 15:53:38 -0800 | [diff] [blame] | 634 | /* |
Vlastimil Babka | 99c0fd5 | 2014-10-09 15:27:23 -0700 | [diff] [blame] | 635 | * Skip if free. We read page order here without zone lock |
| 636 | * which is generally unsafe, but the race window is small and |
| 637 | * the worst thing that can happen is that we skip some |
| 638 | * potential isolation targets. |
Mel Gorman | 6c14466 | 2014-01-23 15:53:38 -0800 | [diff] [blame] | 639 | */ |
Vlastimil Babka | 99c0fd5 | 2014-10-09 15:27:23 -0700 | [diff] [blame] | 640 | if (PageBuddy(page)) { |
| 641 | unsigned long freepage_order = page_order_unsafe(page); |
| 642 | |
| 643 | /* |
| 644 | * Without lock, we cannot be sure that what we got is |
| 645 | * a valid page order. Consider only values in the |
| 646 | * valid order range to prevent low_pfn overflow. |
| 647 | */ |
| 648 | if (freepage_order > 0 && freepage_order < MAX_ORDER) |
| 649 | low_pfn += (1UL << freepage_order) - 1; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 650 | continue; |
Vlastimil Babka | 99c0fd5 | 2014-10-09 15:27:23 -0700 | [diff] [blame] | 651 | } |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 652 | |
Mel Gorman | 9927af74 | 2011-01-13 15:45:59 -0800 | [diff] [blame] | 653 | /* |
Rafael Aquini | bf6bddf | 2012-12-11 16:02:42 -0800 | [diff] [blame] | 654 | * Check may be lockless but that's ok as we recheck later. |
| 655 | * It's possible to migrate LRU pages and balloon pages |
| 656 | * Skip any other type of page |
| 657 | */ |
| 658 | if (!PageLRU(page)) { |
| 659 | if (unlikely(balloon_page_movable(page))) { |
Konstantin Khlebnikov | d6d86c0 | 2014-10-09 15:29:27 -0700 | [diff] [blame] | 660 | if (balloon_page_isolate(page)) { |
Rafael Aquini | bf6bddf | 2012-12-11 16:02:42 -0800 | [diff] [blame] | 661 | /* Successfully isolated */ |
Joonsoo Kim | b6c7501 | 2014-04-07 15:37:07 -0700 | [diff] [blame] | 662 | goto isolate_success; |
Rafael Aquini | bf6bddf | 2012-12-11 16:02:42 -0800 | [diff] [blame] | 663 | } |
| 664 | } |
Andrea Arcangeli | bc83501 | 2011-01-13 15:47:08 -0800 | [diff] [blame] | 665 | continue; |
Rafael Aquini | bf6bddf | 2012-12-11 16:02:42 -0800 | [diff] [blame] | 666 | } |
Andrea Arcangeli | bc83501 | 2011-01-13 15:47:08 -0800 | [diff] [blame] | 667 | |
| 668 | /* |
Mel Gorman | 2a1402a | 2012-10-08 16:32:33 -0700 | [diff] [blame] | 669 | * PageLRU is set. lru_lock normally excludes isolation |
| 670 | * splitting and collapsing (collapsing has already happened |
| 671 | * if PageLRU is set) but the lock is not necessarily taken |
| 672 | * here and it is wasteful to take it just to check transhuge. |
| 673 | * Check TransHuge without lock and skip the whole pageblock if |
| 674 | * it's either a transhuge or hugetlbfs page, as calling |
| 675 | * compound_order() without preventing THP from splitting the |
| 676 | * page underneath us may return surprising results. |
Andrea Arcangeli | bc83501 | 2011-01-13 15:47:08 -0800 | [diff] [blame] | 677 | */ |
| 678 | if (PageTransHuge(page)) { |
Mel Gorman | 2a1402a | 2012-10-08 16:32:33 -0700 | [diff] [blame] | 679 | if (!locked) |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 680 | low_pfn = ALIGN(low_pfn + 1, |
| 681 | pageblock_nr_pages) - 1; |
| 682 | else |
| 683 | low_pfn += (1 << compound_order(page)) - 1; |
| 684 | |
Mel Gorman | 2a1402a | 2012-10-08 16:32:33 -0700 | [diff] [blame] | 685 | continue; |
| 686 | } |
| 687 | |
David Rientjes | 119d6d5 | 2014-04-03 14:48:00 -0700 | [diff] [blame] | 688 | /* |
| 689 | * Migration will fail if an anonymous page is pinned in memory, |
| 690 | * so avoid taking lru_lock and isolating it unnecessarily in an |
| 691 | * admittedly racy check. |
| 692 | */ |
| 693 | if (!page_mapping(page) && |
| 694 | page_count(page) > page_mapcount(page)) |
| 695 | continue; |
| 696 | |
Vlastimil Babka | 69b7189 | 2014-10-09 15:27:18 -0700 | [diff] [blame] | 697 | /* If we already hold the lock, we can skip some rechecking */ |
| 698 | if (!locked) { |
Vlastimil Babka | 8b44d27 | 2014-10-09 15:27:16 -0700 | [diff] [blame] | 699 | locked = compact_trylock_irqsave(&zone->lru_lock, |
| 700 | &flags, cc); |
Vlastimil Babka | 69b7189 | 2014-10-09 15:27:18 -0700 | [diff] [blame] | 701 | if (!locked) |
| 702 | break; |
Mel Gorman | 2a1402a | 2012-10-08 16:32:33 -0700 | [diff] [blame] | 703 | |
Vlastimil Babka | 69b7189 | 2014-10-09 15:27:18 -0700 | [diff] [blame] | 704 | /* Recheck PageLRU and PageTransHuge under lock */ |
| 705 | if (!PageLRU(page)) |
| 706 | continue; |
| 707 | if (PageTransHuge(page)) { |
| 708 | low_pfn += (1 << compound_order(page)) - 1; |
| 709 | continue; |
| 710 | } |
Andrea Arcangeli | bc83501 | 2011-01-13 15:47:08 -0800 | [diff] [blame] | 711 | } |
| 712 | |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 713 | lruvec = mem_cgroup_page_lruvec(page, zone); |
| 714 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 715 | /* Try isolate the page */ |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 716 | if (__isolate_lru_page(page, isolate_mode) != 0) |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 717 | continue; |
| 718 | |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 719 | VM_BUG_ON_PAGE(PageTransCompound(page), page); |
Andrea Arcangeli | bc83501 | 2011-01-13 15:47:08 -0800 | [diff] [blame] | 720 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 721 | /* Successfully isolated */ |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 722 | del_page_from_lru_list(page, lruvec, page_lru(page)); |
Joonsoo Kim | b6c7501 | 2014-04-07 15:37:07 -0700 | [diff] [blame] | 723 | |
| 724 | isolate_success: |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 725 | list_add(&page->lru, migratelist); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 726 | cc->nr_migratepages++; |
Mel Gorman | b7aba69 | 2011-01-13 15:45:54 -0800 | [diff] [blame] | 727 | nr_isolated++; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 728 | |
| 729 | /* Avoid isolating too much */ |
Hillf Danton | 31b8384 | 2012-01-10 15:07:59 -0800 | [diff] [blame] | 730 | if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) { |
| 731 | ++low_pfn; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 732 | break; |
Hillf Danton | 31b8384 | 2012-01-10 15:07:59 -0800 | [diff] [blame] | 733 | } |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 734 | } |
| 735 | |
Vlastimil Babka | 99c0fd5 | 2014-10-09 15:27:23 -0700 | [diff] [blame] | 736 | /* |
| 737 | * The PageBuddy() check could have potentially brought us outside |
| 738 | * the range to be scanned. |
| 739 | */ |
| 740 | if (unlikely(low_pfn > end_pfn)) |
| 741 | low_pfn = end_pfn; |
| 742 | |
Mel Gorman | c67fe37 | 2012-08-21 16:16:17 -0700 | [diff] [blame] | 743 | if (locked) |
| 744 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 745 | |
Vlastimil Babka | 50b5b09 | 2014-01-21 15:51:10 -0800 | [diff] [blame] | 746 | /* |
| 747 | * Update the pageblock-skip information and cached scanner pfn, |
| 748 | * if the whole pageblock was scanned without isolating any page. |
Vlastimil Babka | 50b5b09 | 2014-01-21 15:51:10 -0800 | [diff] [blame] | 749 | */ |
David Rientjes | 35979ef | 2014-06-04 16:08:27 -0700 | [diff] [blame] | 750 | if (low_pfn == end_pfn) |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 751 | update_pageblock_skip(cc, valid_page, nr_isolated, true); |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 752 | |
Mel Gorman | b7aba69 | 2011-01-13 15:45:54 -0800 | [diff] [blame] | 753 | trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated); |
| 754 | |
Minchan Kim | 010fc29 | 2012-12-20 15:05:06 -0800 | [diff] [blame] | 755 | count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned); |
Mel Gorman | 397487d | 2012-10-19 12:00:10 +0100 | [diff] [blame] | 756 | if (nr_isolated) |
Minchan Kim | 010fc29 | 2012-12-20 15:05:06 -0800 | [diff] [blame] | 757 | count_compact_events(COMPACTISOLATED, nr_isolated); |
Mel Gorman | 397487d | 2012-10-19 12:00:10 +0100 | [diff] [blame] | 758 | |
Michal Nazarewicz | 2fe86e0 | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 759 | return low_pfn; |
| 760 | } |
| 761 | |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 762 | /** |
| 763 | * isolate_migratepages_range() - isolate migrate-able pages in a PFN range |
| 764 | * @cc: Compaction control structure. |
| 765 | * @start_pfn: The first PFN to start isolating. |
| 766 | * @end_pfn: The one-past-last PFN. |
| 767 | * |
| 768 | * Returns zero if isolation fails fatally due to e.g. pending signal. |
| 769 | * Otherwise, function returns one-past-the-last PFN of isolated page |
| 770 | * (which may be greater than end_pfn if end fell in a middle of a THP page). |
| 771 | */ |
| 772 | unsigned long |
| 773 | isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn, |
| 774 | unsigned long end_pfn) |
| 775 | { |
| 776 | unsigned long pfn, block_end_pfn; |
| 777 | |
| 778 | /* Scan block by block. First and last block may be incomplete */ |
| 779 | pfn = start_pfn; |
| 780 | block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages); |
| 781 | |
| 782 | for (; pfn < end_pfn; pfn = block_end_pfn, |
| 783 | block_end_pfn += pageblock_nr_pages) { |
| 784 | |
| 785 | block_end_pfn = min(block_end_pfn, end_pfn); |
| 786 | |
Vlastimil Babka | 7d49d88 | 2014-10-09 15:27:11 -0700 | [diff] [blame] | 787 | if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone)) |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 788 | continue; |
| 789 | |
| 790 | pfn = isolate_migratepages_block(cc, pfn, block_end_pfn, |
| 791 | ISOLATE_UNEVICTABLE); |
| 792 | |
| 793 | /* |
| 794 | * In case of fatal failure, release everything that might |
| 795 | * have been isolated in the previous iteration, and signal |
| 796 | * the failure back to caller. |
| 797 | */ |
| 798 | if (!pfn) { |
| 799 | putback_movable_pages(&cc->migratepages); |
| 800 | cc->nr_migratepages = 0; |
| 801 | break; |
| 802 | } |
Joonsoo Kim | 6ea41c0 | 2014-10-29 14:50:20 -0700 | [diff] [blame] | 803 | |
| 804 | if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) |
| 805 | break; |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 806 | } |
| 807 | acct_isolated(cc->zone, cc); |
| 808 | |
| 809 | return pfn; |
| 810 | } |
| 811 | |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 812 | #endif /* CONFIG_COMPACTION || CONFIG_CMA */ |
| 813 | #ifdef CONFIG_COMPACTION |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 814 | /* |
| 815 | * Based on information in the current compact_control, find blocks |
| 816 | * suitable for isolating free pages from and then isolate them. |
| 817 | */ |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 818 | static void isolate_freepages(struct compact_control *cc) |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 819 | { |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 820 | struct zone *zone = cc->zone; |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 821 | struct page *page; |
Vlastimil Babka | c96b9e5 | 2014-06-04 16:07:26 -0700 | [diff] [blame] | 822 | unsigned long block_start_pfn; /* start of current pageblock */ |
Vlastimil Babka | e14c720 | 2014-10-09 15:27:20 -0700 | [diff] [blame] | 823 | unsigned long isolate_start_pfn; /* exact pfn we start at */ |
Vlastimil Babka | c96b9e5 | 2014-06-04 16:07:26 -0700 | [diff] [blame] | 824 | unsigned long block_end_pfn; /* end of current pageblock */ |
| 825 | unsigned long low_pfn; /* lowest pfn scanner is able to scan */ |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 826 | int nr_freepages = cc->nr_freepages; |
| 827 | struct list_head *freelist = &cc->freepages; |
| 828 | |
| 829 | /* |
| 830 | * Initialise the free scanner. The starting point is where we last |
Vlastimil Babka | 49e068f | 2014-05-06 12:50:03 -0700 | [diff] [blame] | 831 | * successfully isolated from, zone-cached value, or the end of the |
Vlastimil Babka | e14c720 | 2014-10-09 15:27:20 -0700 | [diff] [blame] | 832 | * zone when isolating for the first time. For looping we also need |
| 833 | * this pfn aligned down to the pageblock boundary, because we do |
Vlastimil Babka | c96b9e5 | 2014-06-04 16:07:26 -0700 | [diff] [blame] | 834 | * block_start_pfn -= pageblock_nr_pages in the for loop. |
| 835 | * For ending point, take care when isolating in last pageblock of a |
| 836 | * a zone which ends in the middle of a pageblock. |
Vlastimil Babka | 49e068f | 2014-05-06 12:50:03 -0700 | [diff] [blame] | 837 | * The low boundary is the end of the pageblock the migration scanner |
| 838 | * is using. |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 839 | */ |
Vlastimil Babka | e14c720 | 2014-10-09 15:27:20 -0700 | [diff] [blame] | 840 | isolate_start_pfn = cc->free_pfn; |
Vlastimil Babka | c96b9e5 | 2014-06-04 16:07:26 -0700 | [diff] [blame] | 841 | block_start_pfn = cc->free_pfn & ~(pageblock_nr_pages-1); |
| 842 | block_end_pfn = min(block_start_pfn + pageblock_nr_pages, |
| 843 | zone_end_pfn(zone)); |
Vlastimil Babka | 7ed695e | 2014-01-21 15:51:09 -0800 | [diff] [blame] | 844 | low_pfn = ALIGN(cc->migrate_pfn + 1, pageblock_nr_pages); |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 845 | |
| 846 | /* |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 847 | * Isolate free pages until enough are available to migrate the |
| 848 | * pages on cc->migratepages. We stop searching if the migrate |
| 849 | * and free page scanners meet or enough free pages are isolated. |
| 850 | */ |
Vlastimil Babka | c96b9e5 | 2014-06-04 16:07:26 -0700 | [diff] [blame] | 851 | for (; block_start_pfn >= low_pfn && cc->nr_migratepages > nr_freepages; |
| 852 | block_end_pfn = block_start_pfn, |
Vlastimil Babka | e14c720 | 2014-10-09 15:27:20 -0700 | [diff] [blame] | 853 | block_start_pfn -= pageblock_nr_pages, |
| 854 | isolate_start_pfn = block_start_pfn) { |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 855 | unsigned long isolated; |
| 856 | |
David Rientjes | f6ea3ad | 2013-09-30 13:45:03 -0700 | [diff] [blame] | 857 | /* |
| 858 | * This can iterate a massively long zone without finding any |
| 859 | * suitable migration targets, so periodically check if we need |
Vlastimil Babka | be97657 | 2014-06-04 16:10:41 -0700 | [diff] [blame] | 860 | * to schedule, or even abort async compaction. |
David Rientjes | f6ea3ad | 2013-09-30 13:45:03 -0700 | [diff] [blame] | 861 | */ |
Vlastimil Babka | be97657 | 2014-06-04 16:10:41 -0700 | [diff] [blame] | 862 | if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)) |
| 863 | && compact_should_abort(cc)) |
| 864 | break; |
David Rientjes | f6ea3ad | 2013-09-30 13:45:03 -0700 | [diff] [blame] | 865 | |
Vlastimil Babka | 7d49d88 | 2014-10-09 15:27:11 -0700 | [diff] [blame] | 866 | page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn, |
| 867 | zone); |
| 868 | if (!page) |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 869 | continue; |
| 870 | |
| 871 | /* Check the block is suitable for migration */ |
Linus Torvalds | 68e3e92 | 2012-06-03 20:05:57 -0700 | [diff] [blame] | 872 | if (!suitable_migration_target(page)) |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 873 | continue; |
Linus Torvalds | 68e3e92 | 2012-06-03 20:05:57 -0700 | [diff] [blame] | 874 | |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 875 | /* If isolation recently failed, do not retry */ |
| 876 | if (!isolation_suitable(cc, page)) |
| 877 | continue; |
| 878 | |
Vlastimil Babka | e14c720 | 2014-10-09 15:27:20 -0700 | [diff] [blame] | 879 | /* Found a block suitable for isolating free pages from. */ |
| 880 | isolated = isolate_freepages_block(cc, &isolate_start_pfn, |
Vlastimil Babka | c96b9e5 | 2014-06-04 16:07:26 -0700 | [diff] [blame] | 881 | block_end_pfn, freelist, false); |
Mel Gorman | f40d1e4 | 2012-10-08 16:32:36 -0700 | [diff] [blame] | 882 | nr_freepages += isolated; |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 883 | |
| 884 | /* |
Vlastimil Babka | e14c720 | 2014-10-09 15:27:20 -0700 | [diff] [blame] | 885 | * Remember where the free scanner should restart next time, |
| 886 | * which is where isolate_freepages_block() left off. |
| 887 | * But if it scanned the whole pageblock, isolate_start_pfn |
| 888 | * now points at block_end_pfn, which is the start of the next |
| 889 | * pageblock. |
| 890 | * In that case we will however want to restart at the start |
| 891 | * of the previous pageblock. |
| 892 | */ |
| 893 | cc->free_pfn = (isolate_start_pfn < block_end_pfn) ? |
| 894 | isolate_start_pfn : |
| 895 | block_start_pfn - pageblock_nr_pages; |
| 896 | |
| 897 | /* |
Vlastimil Babka | be97657 | 2014-06-04 16:10:41 -0700 | [diff] [blame] | 898 | * isolate_freepages_block() might have aborted due to async |
| 899 | * compaction being contended |
| 900 | */ |
| 901 | if (cc->contended) |
| 902 | break; |
Michal Nazarewicz | 2fe86e0 | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 903 | } |
| 904 | |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 905 | /* split_free_page does not map the pages */ |
| 906 | map_pages(freelist); |
Michal Nazarewicz | 2fe86e0 | 2012-01-30 13:16:26 +0100 | [diff] [blame] | 907 | |
Vlastimil Babka | 7ed695e | 2014-01-21 15:51:09 -0800 | [diff] [blame] | 908 | /* |
| 909 | * If we crossed the migrate scanner, we want to keep it that way |
| 910 | * so that compact_finished() may detect this |
| 911 | */ |
Vlastimil Babka | c96b9e5 | 2014-06-04 16:07:26 -0700 | [diff] [blame] | 912 | if (block_start_pfn < low_pfn) |
Vlastimil Babka | e9ade56 | 2014-06-04 16:08:34 -0700 | [diff] [blame] | 913 | cc->free_pfn = cc->migrate_pfn; |
Vlastimil Babka | c96b9e5 | 2014-06-04 16:07:26 -0700 | [diff] [blame] | 914 | |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 915 | cc->nr_freepages = nr_freepages; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | /* |
| 919 | * This is a migrate-callback that "allocates" freepages by taking pages |
| 920 | * from the isolated freelists in the block we are migrating to. |
| 921 | */ |
| 922 | static struct page *compaction_alloc(struct page *migratepage, |
| 923 | unsigned long data, |
| 924 | int **result) |
| 925 | { |
| 926 | struct compact_control *cc = (struct compact_control *)data; |
| 927 | struct page *freepage; |
| 928 | |
Vlastimil Babka | be97657 | 2014-06-04 16:10:41 -0700 | [diff] [blame] | 929 | /* |
| 930 | * Isolate free pages if necessary, and if we are not aborting due to |
| 931 | * contention. |
| 932 | */ |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 933 | if (list_empty(&cc->freepages)) { |
Vlastimil Babka | be97657 | 2014-06-04 16:10:41 -0700 | [diff] [blame] | 934 | if (!cc->contended) |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 935 | isolate_freepages(cc); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 936 | |
| 937 | if (list_empty(&cc->freepages)) |
| 938 | return NULL; |
| 939 | } |
| 940 | |
| 941 | freepage = list_entry(cc->freepages.next, struct page, lru); |
| 942 | list_del(&freepage->lru); |
| 943 | cc->nr_freepages--; |
| 944 | |
| 945 | return freepage; |
| 946 | } |
| 947 | |
| 948 | /* |
David Rientjes | d53aea3 | 2014-06-04 16:08:26 -0700 | [diff] [blame] | 949 | * This is a migrate-callback that "frees" freepages back to the isolated |
| 950 | * freelist. All pages on the freelist are from the same zone, so there is no |
| 951 | * special handling needed for NUMA. |
| 952 | */ |
| 953 | static void compaction_free(struct page *page, unsigned long data) |
| 954 | { |
| 955 | struct compact_control *cc = (struct compact_control *)data; |
| 956 | |
| 957 | list_add(&page->lru, &cc->freepages); |
| 958 | cc->nr_freepages++; |
| 959 | } |
| 960 | |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 961 | /* possible outcome of isolate_migratepages */ |
| 962 | typedef enum { |
| 963 | ISOLATE_ABORT, /* Abort compaction now */ |
| 964 | ISOLATE_NONE, /* No pages isolated, continue scanning */ |
| 965 | ISOLATE_SUCCESS, /* Pages isolated, migrate */ |
| 966 | } isolate_migrate_t; |
| 967 | |
| 968 | /* |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 969 | * Isolate all pages that can be migrated from the first suitable block, |
| 970 | * starting at the block pointed to by the migrate scanner pfn within |
| 971 | * compact_control. |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 972 | */ |
| 973 | static isolate_migrate_t isolate_migratepages(struct zone *zone, |
| 974 | struct compact_control *cc) |
| 975 | { |
| 976 | unsigned long low_pfn, end_pfn; |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 977 | struct page *page; |
| 978 | const isolate_mode_t isolate_mode = |
| 979 | (cc->mode == MIGRATE_ASYNC ? ISOLATE_ASYNC_MIGRATE : 0); |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 980 | |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 981 | /* |
| 982 | * Start at where we last stopped, or beginning of the zone as |
| 983 | * initialized by compact_zone() |
| 984 | */ |
| 985 | low_pfn = cc->migrate_pfn; |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 986 | |
| 987 | /* Only scan within a pageblock boundary */ |
Mel Gorman | a9aacbc | 2013-02-22 16:32:25 -0800 | [diff] [blame] | 988 | end_pfn = ALIGN(low_pfn + 1, pageblock_nr_pages); |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 989 | |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 990 | /* |
| 991 | * Iterate over whole pageblocks until we find the first suitable. |
| 992 | * Do not cross the free scanner. |
| 993 | */ |
| 994 | for (; end_pfn <= cc->free_pfn; |
| 995 | low_pfn = end_pfn, end_pfn += pageblock_nr_pages) { |
| 996 | |
| 997 | /* |
| 998 | * This can potentially iterate a massively long zone with |
| 999 | * many pageblocks unsuitable, so periodically check if we |
| 1000 | * need to schedule, or even abort async compaction. |
| 1001 | */ |
| 1002 | if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)) |
| 1003 | && compact_should_abort(cc)) |
| 1004 | break; |
| 1005 | |
Vlastimil Babka | 7d49d88 | 2014-10-09 15:27:11 -0700 | [diff] [blame] | 1006 | page = pageblock_pfn_to_page(low_pfn, end_pfn, zone); |
| 1007 | if (!page) |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 1008 | continue; |
| 1009 | |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 1010 | /* If isolation recently failed, do not retry */ |
| 1011 | if (!isolation_suitable(cc, page)) |
| 1012 | continue; |
| 1013 | |
| 1014 | /* |
| 1015 | * For async compaction, also only scan in MOVABLE blocks. |
| 1016 | * Async compaction is optimistic to see if the minimum amount |
| 1017 | * of work satisfies the allocation. |
| 1018 | */ |
| 1019 | if (cc->mode == MIGRATE_ASYNC && |
| 1020 | !migrate_async_suitable(get_pageblock_migratetype(page))) |
| 1021 | continue; |
| 1022 | |
| 1023 | /* Perform the isolation */ |
| 1024 | low_pfn = isolate_migratepages_block(cc, low_pfn, end_pfn, |
| 1025 | isolate_mode); |
| 1026 | |
| 1027 | if (!low_pfn || cc->contended) |
| 1028 | return ISOLATE_ABORT; |
| 1029 | |
| 1030 | /* |
| 1031 | * Either we isolated something and proceed with migration. Or |
| 1032 | * we failed and compact_zone should decide if we should |
| 1033 | * continue or not. |
| 1034 | */ |
| 1035 | break; |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1036 | } |
| 1037 | |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 1038 | acct_isolated(zone, cc); |
Vlastimil Babka | 1d5bfe1 | 2014-11-13 15:19:30 -0800 | [diff] [blame] | 1039 | /* |
| 1040 | * Record where migration scanner will be restarted. If we end up in |
| 1041 | * the same pageblock as the free scanner, make the scanners fully |
| 1042 | * meet so that compact_finished() terminates compaction. |
| 1043 | */ |
| 1044 | cc->migrate_pfn = (end_pfn <= cc->free_pfn) ? low_pfn : cc->free_pfn; |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1045 | |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 1046 | return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE; |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1047 | } |
| 1048 | |
David Rientjes | 6d7ce55 | 2014-10-09 15:27:27 -0700 | [diff] [blame] | 1049 | static int compact_finished(struct zone *zone, struct compact_control *cc, |
| 1050 | const int migratetype) |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1051 | { |
Mel Gorman | 8fb74b9 | 2013-01-11 14:32:16 -0800 | [diff] [blame] | 1052 | unsigned int order; |
Andrea Arcangeli | 5a03b05 | 2011-01-13 15:47:11 -0800 | [diff] [blame] | 1053 | unsigned long watermark; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1054 | |
Vlastimil Babka | be97657 | 2014-06-04 16:10:41 -0700 | [diff] [blame] | 1055 | if (cc->contended || fatal_signal_pending(current)) |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1056 | return COMPACT_PARTIAL; |
| 1057 | |
Mel Gorman | 753341a | 2012-10-08 16:32:40 -0700 | [diff] [blame] | 1058 | /* Compaction run completes if the migrate and free scanner meet */ |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 1059 | if (cc->free_pfn <= cc->migrate_pfn) { |
Vlastimil Babka | 55b7c4c | 2014-01-21 15:51:11 -0800 | [diff] [blame] | 1060 | /* Let the next compaction start anew. */ |
David Rientjes | 35979ef | 2014-06-04 16:08:27 -0700 | [diff] [blame] | 1061 | zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn; |
| 1062 | zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn; |
Vlastimil Babka | 55b7c4c | 2014-01-21 15:51:11 -0800 | [diff] [blame] | 1063 | zone->compact_cached_free_pfn = zone_end_pfn(zone); |
| 1064 | |
Mel Gorman | 6299702 | 2012-10-08 16:32:47 -0700 | [diff] [blame] | 1065 | /* |
| 1066 | * Mark that the PG_migrate_skip information should be cleared |
| 1067 | * by kswapd when it goes to sleep. kswapd does not set the |
| 1068 | * flag itself as the decision to be clear should be directly |
| 1069 | * based on an allocation request. |
| 1070 | */ |
| 1071 | if (!current_is_kswapd()) |
| 1072 | zone->compact_blockskip_flush = true; |
| 1073 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1074 | return COMPACT_COMPLETE; |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 1075 | } |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1076 | |
Johannes Weiner | 82478fb | 2011-01-20 14:44:21 -0800 | [diff] [blame] | 1077 | /* |
| 1078 | * order == -1 is expected when compacting via |
| 1079 | * /proc/sys/vm/compact_memory |
| 1080 | */ |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1081 | if (cc->order == -1) |
| 1082 | return COMPACT_CONTINUE; |
| 1083 | |
Michal Hocko | 3957c77 | 2011-06-15 15:08:25 -0700 | [diff] [blame] | 1084 | /* Compaction run is not finished if the watermark is not met */ |
| 1085 | watermark = low_wmark_pages(zone); |
Michal Hocko | 3957c77 | 2011-06-15 15:08:25 -0700 | [diff] [blame] | 1086 | |
Vlastimil Babka | ebff398 | 2014-12-10 15:43:22 -0800 | [diff] [blame] | 1087 | if (!zone_watermark_ok(zone, cc->order, watermark, cc->classzone_idx, |
| 1088 | cc->alloc_flags)) |
Michal Hocko | 3957c77 | 2011-06-15 15:08:25 -0700 | [diff] [blame] | 1089 | return COMPACT_CONTINUE; |
| 1090 | |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1091 | /* Direct compactor: Is a suitable page free? */ |
Mel Gorman | 8fb74b9 | 2013-01-11 14:32:16 -0800 | [diff] [blame] | 1092 | for (order = cc->order; order < MAX_ORDER; order++) { |
| 1093 | struct free_area *area = &zone->free_area[order]; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1094 | |
Mel Gorman | 8fb74b9 | 2013-01-11 14:32:16 -0800 | [diff] [blame] | 1095 | /* Job done if page is free of the right migratetype */ |
David Rientjes | 6d7ce55 | 2014-10-09 15:27:27 -0700 | [diff] [blame] | 1096 | if (!list_empty(&area->free_list[migratetype])) |
Mel Gorman | 8fb74b9 | 2013-01-11 14:32:16 -0800 | [diff] [blame] | 1097 | return COMPACT_PARTIAL; |
| 1098 | |
| 1099 | /* Job done if allocation would set block type */ |
| 1100 | if (cc->order >= pageblock_order && area->nr_free) |
| 1101 | return COMPACT_PARTIAL; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1102 | } |
| 1103 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1104 | return COMPACT_CONTINUE; |
| 1105 | } |
| 1106 | |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 1107 | /* |
| 1108 | * compaction_suitable: Is this suitable to run compaction on this zone now? |
| 1109 | * Returns |
| 1110 | * COMPACT_SKIPPED - If there are too few free pages for compaction |
| 1111 | * COMPACT_PARTIAL - If the allocation would succeed without compaction |
| 1112 | * COMPACT_CONTINUE - If compaction should run now |
| 1113 | */ |
Vlastimil Babka | ebff398 | 2014-12-10 15:43:22 -0800 | [diff] [blame] | 1114 | unsigned long compaction_suitable(struct zone *zone, int order, |
| 1115 | int alloc_flags, int classzone_idx) |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 1116 | { |
| 1117 | int fragindex; |
| 1118 | unsigned long watermark; |
| 1119 | |
| 1120 | /* |
Michal Hocko | 3957c77 | 2011-06-15 15:08:25 -0700 | [diff] [blame] | 1121 | * order == -1 is expected when compacting via |
| 1122 | * /proc/sys/vm/compact_memory |
| 1123 | */ |
| 1124 | if (order == -1) |
| 1125 | return COMPACT_CONTINUE; |
| 1126 | |
Vlastimil Babka | ebff398 | 2014-12-10 15:43:22 -0800 | [diff] [blame] | 1127 | watermark = low_wmark_pages(zone); |
| 1128 | /* |
| 1129 | * If watermarks for high-order allocation are already met, there |
| 1130 | * should be no need for compaction at all. |
| 1131 | */ |
| 1132 | if (zone_watermark_ok(zone, order, watermark, classzone_idx, |
| 1133 | alloc_flags)) |
| 1134 | return COMPACT_PARTIAL; |
| 1135 | |
Michal Hocko | 3957c77 | 2011-06-15 15:08:25 -0700 | [diff] [blame] | 1136 | /* |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 1137 | * Watermarks for order-0 must be met for compaction. Note the 2UL. |
| 1138 | * This is because during migration, copies of pages need to be |
| 1139 | * allocated and for a short time, the footprint is higher |
| 1140 | */ |
Vlastimil Babka | ebff398 | 2014-12-10 15:43:22 -0800 | [diff] [blame] | 1141 | watermark += (2UL << order); |
| 1142 | if (!zone_watermark_ok(zone, 0, watermark, classzone_idx, alloc_flags)) |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 1143 | return COMPACT_SKIPPED; |
| 1144 | |
| 1145 | /* |
| 1146 | * fragmentation index determines if allocation failures are due to |
| 1147 | * low memory or external fragmentation |
| 1148 | * |
Vlastimil Babka | ebff398 | 2014-12-10 15:43:22 -0800 | [diff] [blame] | 1149 | * index of -1000 would imply allocations might succeed depending on |
| 1150 | * watermarks, but we already failed the high-order watermark check |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 1151 | * index towards 0 implies failure is due to lack of memory |
| 1152 | * index towards 1000 implies failure is due to fragmentation |
| 1153 | * |
| 1154 | * Only compact if a failure would be due to fragmentation. |
| 1155 | */ |
| 1156 | fragindex = fragmentation_index(zone, order); |
| 1157 | if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold) |
| 1158 | return COMPACT_SKIPPED; |
| 1159 | |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 1160 | return COMPACT_CONTINUE; |
| 1161 | } |
| 1162 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1163 | static int compact_zone(struct zone *zone, struct compact_control *cc) |
| 1164 | { |
| 1165 | int ret; |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 1166 | unsigned long start_pfn = zone->zone_start_pfn; |
Cody P Schafer | 108bcc9 | 2013-02-22 16:35:23 -0800 | [diff] [blame] | 1167 | unsigned long end_pfn = zone_end_pfn(zone); |
David Rientjes | 6d7ce55 | 2014-10-09 15:27:27 -0700 | [diff] [blame] | 1168 | const int migratetype = gfpflags_to_migratetype(cc->gfp_mask); |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 1169 | const bool sync = cc->mode != MIGRATE_ASYNC; |
Vlastimil Babka | fdaf7f5 | 2014-12-10 15:43:34 -0800 | [diff] [blame] | 1170 | unsigned long last_migrated_pfn = 0; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1171 | |
Vlastimil Babka | ebff398 | 2014-12-10 15:43:22 -0800 | [diff] [blame] | 1172 | ret = compaction_suitable(zone, cc->order, cc->alloc_flags, |
| 1173 | cc->classzone_idx); |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 1174 | switch (ret) { |
| 1175 | case COMPACT_PARTIAL: |
| 1176 | case COMPACT_SKIPPED: |
| 1177 | /* Compaction is likely to fail */ |
| 1178 | return ret; |
| 1179 | case COMPACT_CONTINUE: |
| 1180 | /* Fall through to compaction */ |
| 1181 | ; |
| 1182 | } |
| 1183 | |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 1184 | /* |
Vlastimil Babka | d3132e4 | 2014-01-21 15:51:08 -0800 | [diff] [blame] | 1185 | * Clear pageblock skip if there were failures recently and compaction |
| 1186 | * is about to be retried after being deferred. kswapd does not do |
| 1187 | * this reset as it'll reset the cached information when going to sleep. |
| 1188 | */ |
| 1189 | if (compaction_restarting(zone, cc->order) && !current_is_kswapd()) |
| 1190 | __reset_isolation_suitable(zone); |
| 1191 | |
| 1192 | /* |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 1193 | * Setup to move all movable pages to the end of the zone. Used cached |
| 1194 | * information on where the scanners should start but check that it |
| 1195 | * is initialised by ensuring the values are within zone boundaries. |
| 1196 | */ |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 1197 | cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync]; |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 1198 | cc->free_pfn = zone->compact_cached_free_pfn; |
| 1199 | if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) { |
| 1200 | cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1); |
| 1201 | zone->compact_cached_free_pfn = cc->free_pfn; |
| 1202 | } |
| 1203 | if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) { |
| 1204 | cc->migrate_pfn = start_pfn; |
David Rientjes | 35979ef | 2014-06-04 16:08:27 -0700 | [diff] [blame] | 1205 | zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn; |
| 1206 | zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn; |
Mel Gorman | c89511a | 2012-10-08 16:32:45 -0700 | [diff] [blame] | 1207 | } |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1208 | |
Joonsoo Kim | 16c4a09 | 2015-02-11 15:27:01 -0800 | [diff] [blame^] | 1209 | trace_mm_compaction_begin(start_pfn, cc->migrate_pfn, |
| 1210 | cc->free_pfn, end_pfn, sync); |
Mel Gorman | 0eb927c | 2014-01-21 15:51:05 -0800 | [diff] [blame] | 1211 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1212 | migrate_prep_local(); |
| 1213 | |
David Rientjes | 6d7ce55 | 2014-10-09 15:27:27 -0700 | [diff] [blame] | 1214 | while ((ret = compact_finished(zone, cc, migratetype)) == |
| 1215 | COMPACT_CONTINUE) { |
Minchan Kim | 9d502c1 | 2011-03-22 16:30:39 -0700 | [diff] [blame] | 1216 | int err; |
Vlastimil Babka | fdaf7f5 | 2014-12-10 15:43:34 -0800 | [diff] [blame] | 1217 | unsigned long isolate_start_pfn = cc->migrate_pfn; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1218 | |
Mel Gorman | f9e35b3 | 2011-06-15 15:08:52 -0700 | [diff] [blame] | 1219 | switch (isolate_migratepages(zone, cc)) { |
| 1220 | case ISOLATE_ABORT: |
| 1221 | ret = COMPACT_PARTIAL; |
Rafael Aquini | 5733c7d | 2012-12-11 16:02:47 -0800 | [diff] [blame] | 1222 | putback_movable_pages(&cc->migratepages); |
Shaohua Li | e64c523 | 2012-10-08 16:32:27 -0700 | [diff] [blame] | 1223 | cc->nr_migratepages = 0; |
Mel Gorman | f9e35b3 | 2011-06-15 15:08:52 -0700 | [diff] [blame] | 1224 | goto out; |
| 1225 | case ISOLATE_NONE: |
Vlastimil Babka | fdaf7f5 | 2014-12-10 15:43:34 -0800 | [diff] [blame] | 1226 | /* |
| 1227 | * We haven't isolated and migrated anything, but |
| 1228 | * there might still be unflushed migrations from |
| 1229 | * previous cc->order aligned block. |
| 1230 | */ |
| 1231 | goto check_drain; |
Mel Gorman | f9e35b3 | 2011-06-15 15:08:52 -0700 | [diff] [blame] | 1232 | case ISOLATE_SUCCESS: |
| 1233 | ; |
| 1234 | } |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1235 | |
David Rientjes | d53aea3 | 2014-06-04 16:08:26 -0700 | [diff] [blame] | 1236 | err = migrate_pages(&cc->migratepages, compaction_alloc, |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 1237 | compaction_free, (unsigned long)cc, cc->mode, |
Mel Gorman | 7b2a2d4 | 2012-10-19 14:07:31 +0100 | [diff] [blame] | 1238 | MR_COMPACTION); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1239 | |
Vlastimil Babka | f8c9301 | 2014-06-04 16:08:32 -0700 | [diff] [blame] | 1240 | trace_mm_compaction_migratepages(cc->nr_migratepages, err, |
| 1241 | &cc->migratepages); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1242 | |
Vlastimil Babka | f8c9301 | 2014-06-04 16:08:32 -0700 | [diff] [blame] | 1243 | /* All pages were either migrated or will be released */ |
| 1244 | cc->nr_migratepages = 0; |
Minchan Kim | 9d502c1 | 2011-03-22 16:30:39 -0700 | [diff] [blame] | 1245 | if (err) { |
Rafael Aquini | 5733c7d | 2012-12-11 16:02:47 -0800 | [diff] [blame] | 1246 | putback_movable_pages(&cc->migratepages); |
Vlastimil Babka | 7ed695e | 2014-01-21 15:51:09 -0800 | [diff] [blame] | 1247 | /* |
| 1248 | * migrate_pages() may return -ENOMEM when scanners meet |
| 1249 | * and we want compact_finished() to detect it |
| 1250 | */ |
| 1251 | if (err == -ENOMEM && cc->free_pfn > cc->migrate_pfn) { |
David Rientjes | 4bf2bba | 2012-07-11 14:02:13 -0700 | [diff] [blame] | 1252 | ret = COMPACT_PARTIAL; |
| 1253 | goto out; |
| 1254 | } |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1255 | } |
Vlastimil Babka | fdaf7f5 | 2014-12-10 15:43:34 -0800 | [diff] [blame] | 1256 | |
| 1257 | /* |
| 1258 | * Record where we could have freed pages by migration and not |
| 1259 | * yet flushed them to buddy allocator. We use the pfn that |
| 1260 | * isolate_migratepages() started from in this loop iteration |
| 1261 | * - this is the lowest page that could have been isolated and |
| 1262 | * then freed by migration. |
| 1263 | */ |
| 1264 | if (!last_migrated_pfn) |
| 1265 | last_migrated_pfn = isolate_start_pfn; |
| 1266 | |
| 1267 | check_drain: |
| 1268 | /* |
| 1269 | * Has the migration scanner moved away from the previous |
| 1270 | * cc->order aligned block where we migrated from? If yes, |
| 1271 | * flush the pages that were freed, so that they can merge and |
| 1272 | * compact_finished() can detect immediately if allocation |
| 1273 | * would succeed. |
| 1274 | */ |
| 1275 | if (cc->order > 0 && last_migrated_pfn) { |
| 1276 | int cpu; |
| 1277 | unsigned long current_block_start = |
| 1278 | cc->migrate_pfn & ~((1UL << cc->order) - 1); |
| 1279 | |
| 1280 | if (last_migrated_pfn < current_block_start) { |
| 1281 | cpu = get_cpu(); |
| 1282 | lru_add_drain_cpu(cpu); |
| 1283 | drain_local_pages(zone); |
| 1284 | put_cpu(); |
| 1285 | /* No more flushing until we migrate again */ |
| 1286 | last_migrated_pfn = 0; |
| 1287 | } |
| 1288 | } |
| 1289 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1290 | } |
| 1291 | |
Mel Gorman | f9e35b3 | 2011-06-15 15:08:52 -0700 | [diff] [blame] | 1292 | out: |
Vlastimil Babka | 6bace09 | 2014-12-10 15:43:31 -0800 | [diff] [blame] | 1293 | /* |
| 1294 | * Release free pages and update where the free scanner should restart, |
| 1295 | * so we don't leave any returned pages behind in the next attempt. |
| 1296 | */ |
| 1297 | if (cc->nr_freepages > 0) { |
| 1298 | unsigned long free_pfn = release_freepages(&cc->freepages); |
| 1299 | |
| 1300 | cc->nr_freepages = 0; |
| 1301 | VM_BUG_ON(free_pfn == 0); |
| 1302 | /* The cached pfn is always the first in a pageblock */ |
| 1303 | free_pfn &= ~(pageblock_nr_pages-1); |
| 1304 | /* |
| 1305 | * Only go back, not forward. The cached pfn might have been |
| 1306 | * already reset to zone end in compact_finished() |
| 1307 | */ |
| 1308 | if (free_pfn > zone->compact_cached_free_pfn) |
| 1309 | zone->compact_cached_free_pfn = free_pfn; |
| 1310 | } |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1311 | |
Joonsoo Kim | 16c4a09 | 2015-02-11 15:27:01 -0800 | [diff] [blame^] | 1312 | trace_mm_compaction_end(start_pfn, cc->migrate_pfn, |
| 1313 | cc->free_pfn, end_pfn, sync, ret); |
Mel Gorman | 0eb927c | 2014-01-21 15:51:05 -0800 | [diff] [blame] | 1314 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 1315 | return ret; |
| 1316 | } |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1317 | |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 1318 | static unsigned long compact_zone_order(struct zone *zone, int order, |
Vlastimil Babka | ebff398 | 2014-12-10 15:43:22 -0800 | [diff] [blame] | 1319 | gfp_t gfp_mask, enum migrate_mode mode, int *contended, |
| 1320 | int alloc_flags, int classzone_idx) |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1321 | { |
Shaohua Li | e64c523 | 2012-10-08 16:32:27 -0700 | [diff] [blame] | 1322 | unsigned long ret; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1323 | struct compact_control cc = { |
| 1324 | .nr_freepages = 0, |
| 1325 | .nr_migratepages = 0, |
| 1326 | .order = order, |
David Rientjes | 6d7ce55 | 2014-10-09 15:27:27 -0700 | [diff] [blame] | 1327 | .gfp_mask = gfp_mask, |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1328 | .zone = zone, |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 1329 | .mode = mode, |
Vlastimil Babka | ebff398 | 2014-12-10 15:43:22 -0800 | [diff] [blame] | 1330 | .alloc_flags = alloc_flags, |
| 1331 | .classzone_idx = classzone_idx, |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1332 | }; |
| 1333 | INIT_LIST_HEAD(&cc.freepages); |
| 1334 | INIT_LIST_HEAD(&cc.migratepages); |
| 1335 | |
Shaohua Li | e64c523 | 2012-10-08 16:32:27 -0700 | [diff] [blame] | 1336 | ret = compact_zone(zone, &cc); |
| 1337 | |
| 1338 | VM_BUG_ON(!list_empty(&cc.freepages)); |
| 1339 | VM_BUG_ON(!list_empty(&cc.migratepages)); |
| 1340 | |
| 1341 | *contended = cc.contended; |
| 1342 | return ret; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1343 | } |
| 1344 | |
Mel Gorman | 5e77190 | 2010-05-24 14:32:31 -0700 | [diff] [blame] | 1345 | int sysctl_extfrag_threshold = 500; |
| 1346 | |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1347 | /** |
| 1348 | * try_to_compact_pages - Direct compact to satisfy a high-order allocation |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1349 | * @gfp_mask: The GFP mask of the current allocation |
Vlastimil Babka | 1a6d53a | 2015-02-11 15:25:44 -0800 | [diff] [blame] | 1350 | * @order: The order of the current allocation |
| 1351 | * @alloc_flags: The allocation flags of the current allocation |
| 1352 | * @ac: The context of current allocation |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 1353 | * @mode: The migration mode for async, sync light, or sync migration |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame] | 1354 | * @contended: Return value that determines if compaction was aborted due to |
| 1355 | * need_resched() or lock contention |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1356 | * |
| 1357 | * This is the main entry point for direct page compaction. |
| 1358 | */ |
Vlastimil Babka | 1a6d53a | 2015-02-11 15:25:44 -0800 | [diff] [blame] | 1359 | unsigned long try_to_compact_pages(gfp_t gfp_mask, unsigned int order, |
| 1360 | int alloc_flags, const struct alloc_context *ac, |
| 1361 | enum migrate_mode mode, int *contended) |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1362 | { |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1363 | int may_enter_fs = gfp_mask & __GFP_FS; |
| 1364 | int may_perform_io = gfp_mask & __GFP_IO; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1365 | struct zoneref *z; |
| 1366 | struct zone *zone; |
Vlastimil Babka | 53853e2 | 2014-10-09 15:27:02 -0700 | [diff] [blame] | 1367 | int rc = COMPACT_DEFERRED; |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame] | 1368 | int all_zones_contended = COMPACT_CONTENDED_LOCK; /* init for &= op */ |
| 1369 | |
| 1370 | *contended = COMPACT_CONTENDED_NONE; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1371 | |
Mel Gorman | 4ffb633 | 2012-10-08 16:29:09 -0700 | [diff] [blame] | 1372 | /* Check if the GFP flags allow compaction */ |
Andrea Arcangeli | c5a73c3 | 2011-01-13 15:47:11 -0800 | [diff] [blame] | 1373 | if (!order || !may_enter_fs || !may_perform_io) |
Vlastimil Babka | 53853e2 | 2014-10-09 15:27:02 -0700 | [diff] [blame] | 1374 | return COMPACT_SKIPPED; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1375 | |
| 1376 | /* Compact each zone in the list */ |
Vlastimil Babka | 1a6d53a | 2015-02-11 15:25:44 -0800 | [diff] [blame] | 1377 | for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx, |
| 1378 | ac->nodemask) { |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1379 | int status; |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame] | 1380 | int zone_contended; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1381 | |
Vlastimil Babka | 53853e2 | 2014-10-09 15:27:02 -0700 | [diff] [blame] | 1382 | if (compaction_deferred(zone, order)) |
| 1383 | continue; |
| 1384 | |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 1385 | status = compact_zone_order(zone, order, gfp_mask, mode, |
Vlastimil Babka | 1a6d53a | 2015-02-11 15:25:44 -0800 | [diff] [blame] | 1386 | &zone_contended, alloc_flags, |
| 1387 | ac->classzone_idx); |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1388 | rc = max(status, rc); |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame] | 1389 | /* |
| 1390 | * It takes at least one zone that wasn't lock contended |
| 1391 | * to clear all_zones_contended. |
| 1392 | */ |
| 1393 | all_zones_contended &= zone_contended; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1394 | |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 1395 | /* If a normal allocation would succeed, stop compacting */ |
Vlastimil Babka | ebff398 | 2014-12-10 15:43:22 -0800 | [diff] [blame] | 1396 | if (zone_watermark_ok(zone, order, low_wmark_pages(zone), |
Vlastimil Babka | 1a6d53a | 2015-02-11 15:25:44 -0800 | [diff] [blame] | 1397 | ac->classzone_idx, alloc_flags)) { |
Vlastimil Babka | 53853e2 | 2014-10-09 15:27:02 -0700 | [diff] [blame] | 1398 | /* |
| 1399 | * We think the allocation will succeed in this zone, |
| 1400 | * but it is not certain, hence the false. The caller |
| 1401 | * will repeat this with true if allocation indeed |
| 1402 | * succeeds in this zone. |
| 1403 | */ |
| 1404 | compaction_defer_reset(zone, order, false); |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame] | 1405 | /* |
| 1406 | * It is possible that async compaction aborted due to |
| 1407 | * need_resched() and the watermarks were ok thanks to |
| 1408 | * somebody else freeing memory. The allocation can |
| 1409 | * however still fail so we better signal the |
| 1410 | * need_resched() contention anyway (this will not |
| 1411 | * prevent the allocation attempt). |
| 1412 | */ |
| 1413 | if (zone_contended == COMPACT_CONTENDED_SCHED) |
| 1414 | *contended = COMPACT_CONTENDED_SCHED; |
| 1415 | |
| 1416 | goto break_loop; |
| 1417 | } |
| 1418 | |
Vlastimil Babka | f866979 | 2014-12-10 15:43:28 -0800 | [diff] [blame] | 1419 | if (mode != MIGRATE_ASYNC && status == COMPACT_COMPLETE) { |
Vlastimil Babka | 53853e2 | 2014-10-09 15:27:02 -0700 | [diff] [blame] | 1420 | /* |
| 1421 | * We think that allocation won't succeed in this zone |
| 1422 | * so we defer compaction there. If it ends up |
| 1423 | * succeeding after all, it will be reset. |
| 1424 | */ |
| 1425 | defer_compaction(zone, order); |
| 1426 | } |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame] | 1427 | |
| 1428 | /* |
| 1429 | * We might have stopped compacting due to need_resched() in |
| 1430 | * async compaction, or due to a fatal signal detected. In that |
| 1431 | * case do not try further zones and signal need_resched() |
| 1432 | * contention. |
| 1433 | */ |
| 1434 | if ((zone_contended == COMPACT_CONTENDED_SCHED) |
| 1435 | || fatal_signal_pending(current)) { |
| 1436 | *contended = COMPACT_CONTENDED_SCHED; |
| 1437 | goto break_loop; |
| 1438 | } |
| 1439 | |
| 1440 | continue; |
| 1441 | break_loop: |
| 1442 | /* |
| 1443 | * We might not have tried all the zones, so be conservative |
| 1444 | * and assume they are not all lock contended. |
| 1445 | */ |
| 1446 | all_zones_contended = 0; |
| 1447 | break; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1448 | } |
| 1449 | |
Vlastimil Babka | 1f9efde | 2014-10-09 15:27:14 -0700 | [diff] [blame] | 1450 | /* |
| 1451 | * If at least one zone wasn't deferred or skipped, we report if all |
| 1452 | * zones that were tried were lock contended. |
| 1453 | */ |
| 1454 | if (rc > COMPACT_SKIPPED && all_zones_contended) |
| 1455 | *contended = COMPACT_CONTENDED_LOCK; |
| 1456 | |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 1457 | return rc; |
| 1458 | } |
| 1459 | |
| 1460 | |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1461 | /* Compact all zones within a node */ |
Andrew Morton | 7103f16 | 2013-02-22 16:32:33 -0800 | [diff] [blame] | 1462 | static void __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc) |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1463 | { |
| 1464 | int zoneid; |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1465 | struct zone *zone; |
| 1466 | |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1467 | for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1468 | |
| 1469 | zone = &pgdat->node_zones[zoneid]; |
| 1470 | if (!populated_zone(zone)) |
| 1471 | continue; |
| 1472 | |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 1473 | cc->nr_freepages = 0; |
| 1474 | cc->nr_migratepages = 0; |
| 1475 | cc->zone = zone; |
| 1476 | INIT_LIST_HEAD(&cc->freepages); |
| 1477 | INIT_LIST_HEAD(&cc->migratepages); |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1478 | |
Dan Carpenter | aad6ec3 | 2012-03-21 16:33:54 -0700 | [diff] [blame] | 1479 | if (cc->order == -1 || !compaction_deferred(zone, cc->order)) |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 1480 | compact_zone(zone, cc); |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1481 | |
Rik van Riel | aff6224 | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 1482 | if (cc->order > 0) { |
Vlastimil Babka | de6c60a | 2014-01-21 15:51:07 -0800 | [diff] [blame] | 1483 | if (zone_watermark_ok(zone, cc->order, |
| 1484 | low_wmark_pages(zone), 0, 0)) |
| 1485 | compaction_defer_reset(zone, cc->order, false); |
Rik van Riel | aff6224 | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 1486 | } |
| 1487 | |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 1488 | VM_BUG_ON(!list_empty(&cc->freepages)); |
| 1489 | VM_BUG_ON(!list_empty(&cc->migratepages)); |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1490 | } |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1491 | } |
| 1492 | |
Andrew Morton | 7103f16 | 2013-02-22 16:32:33 -0800 | [diff] [blame] | 1493 | void compact_pgdat(pg_data_t *pgdat, int order) |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 1494 | { |
| 1495 | struct compact_control cc = { |
| 1496 | .order = order, |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 1497 | .mode = MIGRATE_ASYNC, |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 1498 | }; |
| 1499 | |
Mel Gorman | 3a7200a | 2013-09-11 14:22:19 -0700 | [diff] [blame] | 1500 | if (!order) |
| 1501 | return; |
| 1502 | |
Andrew Morton | 7103f16 | 2013-02-22 16:32:33 -0800 | [diff] [blame] | 1503 | __compact_pgdat(pgdat, &cc); |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 1504 | } |
| 1505 | |
Andrew Morton | 7103f16 | 2013-02-22 16:32:33 -0800 | [diff] [blame] | 1506 | static void compact_node(int nid) |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 1507 | { |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 1508 | struct compact_control cc = { |
| 1509 | .order = -1, |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 1510 | .mode = MIGRATE_SYNC, |
David Rientjes | 91ca918 | 2014-04-03 14:47:23 -0700 | [diff] [blame] | 1511 | .ignore_skip_hint = true, |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 1512 | }; |
| 1513 | |
Andrew Morton | 7103f16 | 2013-02-22 16:32:33 -0800 | [diff] [blame] | 1514 | __compact_pgdat(NODE_DATA(nid), &cc); |
Rik van Riel | 7be62de | 2012-03-21 16:33:52 -0700 | [diff] [blame] | 1515 | } |
| 1516 | |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1517 | /* Compact all nodes in the system */ |
Jason Liu | 7964c06 | 2013-01-11 14:31:47 -0800 | [diff] [blame] | 1518 | static void compact_nodes(void) |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1519 | { |
| 1520 | int nid; |
| 1521 | |
Hugh Dickins | 8575ec2 | 2012-03-21 16:33:53 -0700 | [diff] [blame] | 1522 | /* Flush pending updates to the LRU lists */ |
| 1523 | lru_add_drain_all(); |
| 1524 | |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1525 | for_each_online_node(nid) |
| 1526 | compact_node(nid); |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | /* The written value is actually unused, all memory is compacted */ |
| 1530 | int sysctl_compact_memory; |
| 1531 | |
| 1532 | /* This is the entry point for compacting all nodes via /proc/sys/vm */ |
| 1533 | int sysctl_compaction_handler(struct ctl_table *table, int write, |
| 1534 | void __user *buffer, size_t *length, loff_t *ppos) |
| 1535 | { |
| 1536 | if (write) |
Jason Liu | 7964c06 | 2013-01-11 14:31:47 -0800 | [diff] [blame] | 1537 | compact_nodes(); |
Mel Gorman | 76ab0f5 | 2010-05-24 14:32:28 -0700 | [diff] [blame] | 1538 | |
| 1539 | return 0; |
| 1540 | } |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 1541 | |
Mel Gorman | 5e77190 | 2010-05-24 14:32:31 -0700 | [diff] [blame] | 1542 | int sysctl_extfrag_handler(struct ctl_table *table, int write, |
| 1543 | void __user *buffer, size_t *length, loff_t *ppos) |
| 1544 | { |
| 1545 | proc_dointvec_minmax(table, write, buffer, length, ppos); |
| 1546 | |
| 1547 | return 0; |
| 1548 | } |
| 1549 | |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 1550 | #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA) |
Rashika Kheria | 74e77fb | 2014-04-03 14:48:01 -0700 | [diff] [blame] | 1551 | static ssize_t sysfs_compact_node(struct device *dev, |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 1552 | struct device_attribute *attr, |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 1553 | const char *buf, size_t count) |
| 1554 | { |
Hugh Dickins | 8575ec2 | 2012-03-21 16:33:53 -0700 | [diff] [blame] | 1555 | int nid = dev->id; |
| 1556 | |
| 1557 | if (nid >= 0 && nid < nr_node_ids && node_online(nid)) { |
| 1558 | /* Flush pending updates to the LRU lists */ |
| 1559 | lru_add_drain_all(); |
| 1560 | |
| 1561 | compact_node(nid); |
| 1562 | } |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 1563 | |
| 1564 | return count; |
| 1565 | } |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 1566 | static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node); |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 1567 | |
| 1568 | int compaction_register_node(struct node *node) |
| 1569 | { |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 1570 | return device_create_file(&node->dev, &dev_attr_compact); |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 1571 | } |
| 1572 | |
| 1573 | void compaction_unregister_node(struct node *node) |
| 1574 | { |
Kay Sievers | 10fbcf4 | 2011-12-21 14:48:43 -0800 | [diff] [blame] | 1575 | return device_remove_file(&node->dev, &dev_attr_compact); |
Mel Gorman | ed4a6d7 | 2010-05-24 14:32:29 -0700 | [diff] [blame] | 1576 | } |
| 1577 | #endif /* CONFIG_SYSFS && CONFIG_NUMA */ |
Michal Nazarewicz | ff9543f | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1578 | |
| 1579 | #endif /* CONFIG_COMPACTION */ |