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