summaryrefslogtreecommitdiffstats
path: root/mm/compaction.c
diff options
context:
space:
mode:
authorVlastimil Babka <vbabka@suse.cz>2014-10-09 18:27:16 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2014-10-09 22:25:54 -0400
commit8b44d2791f912566a7ef58c71a7f9cbd16c3eeae (patch)
tree7ce0e365cfc5acfd8ef737c1acec46c1b3ddc0a8 /mm/compaction.c
parent1f9efdef4f3f1d2a073e524113fd0038af636f2b (diff)
mm, compaction: periodically drop lock and restore IRQs in scanners
Compaction scanners regularly check for lock contention and need_resched() through the compact_checklock_irqsave() function. However, if there is no contention, the lock can be held and IRQ disabled for potentially long time. This has been addressed by commit b2eef8c0d091 ("mm: compaction: minimise the time IRQs are disabled while isolating pages for migration") for the migration scanner. However, the refactoring done by commit 2a1402aa044b ("mm: compaction: acquire the zone->lru_lock as late as possible") has changed the conditions so that the lock is dropped only when there's contention on the lock or need_resched() is true. Also, need_resched() is checked only when the lock is already held. The comment "give a chance to irqs before checking need_resched" is therefore misleading, as IRQs remain disabled when the check is done. This patch restores the behavior intended by commit b2eef8c0d091 and also tries to better balance and make more deterministic the time spent by checking for contention vs the time the scanners might run between the checks. It also avoids situations where checking has not been done often enough before. The result should be avoiding both too frequent and too infrequent contention checking, and especially the potentially long-running scans with IRQs disabled and no checking of need_resched() or for fatal signal pending, which can happen when many consecutive pages or pageblocks fail the preliminary tests and do not reach the later call site to compact_checklock_irqsave(), as explained below. Before the patch: In the migration scanner, compact_checklock_irqsave() was called each loop, if reached. If not reached, some lower-frequency checking could still be done if the lock was already held, but this would not result in aborting contended async compaction until reaching compact_checklock_irqsave() or end of pageblock. In the free scanner, it was similar but completely without the periodical checking, so lock can be potentially held until reaching the end of pageblock. After the patch, in both scanners: The periodical check is done as the first thing in the loop on each SWAP_CLUSTER_MAX aligned pfn, using the new compact_unlock_should_abort() function, which always unlocks the lock (if locked) and aborts async compaction if scheduling is needed. It also aborts any type of compaction when a fatal signal is pending. The compact_checklock_irqsave() function is replaced with a slightly different compact_trylock_irqsave(). The biggest difference is that the function is not called at all if the lock is already held. The periodical need_resched() checking is left solely to compact_unlock_should_abort(). The lock contention avoidance for async compaction is achieved by the periodical unlock by compact_unlock_should_abort() and by using trylock in compact_trylock_irqsave() and aborting when trylock fails. Sync compaction does not use trylock. Signed-off-by: Vlastimil Babka <vbabka@suse.cz> Reviewed-by: Zhang Yanfei <zhangyanfei@cn.fujitsu.com> Acked-by: Minchan Kim <minchan@kernel.org> Acked-by: Mel Gorman <mgorman@suse.de> Cc: Michal Nazarewicz <mina86@mina86.com> Cc: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com> Cc: Christoph Lameter <cl@linux.com> Cc: Rik van Riel <riel@redhat.com> Acked-by: David Rientjes <rientjes@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'mm/compaction.c')
-rw-r--r--mm/compaction.c119
1 files changed, 72 insertions, 47 deletions
diff --git a/mm/compaction.c b/mm/compaction.c
index 26bb20ef853d..74770e40cfe5 100644
--- a/mm/compaction.c
+++ b/mm/compaction.c
@@ -223,61 +223,72 @@ static void update_pageblock_skip(struct compact_control *cc,
223} 223}
224#endif /* CONFIG_COMPACTION */ 224#endif /* CONFIG_COMPACTION */
225 225
226static int should_release_lock(spinlock_t *lock) 226/*
227 * Compaction requires the taking of some coarse locks that are potentially
228 * very heavily contended. For async compaction, back out if the lock cannot
229 * be taken immediately. For sync compaction, spin on the lock if needed.
230 *
231 * Returns true if the lock is held
232 * Returns false if the lock is not held and compaction should abort
233 */
234static bool compact_trylock_irqsave(spinlock_t *lock, unsigned long *flags,
235 struct compact_control *cc)
227{ 236{
228 /* 237 if (cc->mode == MIGRATE_ASYNC) {
229 * Sched contention has higher priority here as we may potentially 238 if (!spin_trylock_irqsave(lock, *flags)) {
230 * have to abort whole compaction ASAP. Returning with lock contention 239 cc->contended = COMPACT_CONTENDED_LOCK;
231 * means we will try another zone, and further decisions are 240 return false;
232 * influenced only when all zones are lock contended. That means 241 }
233 * potentially missing a lock contention is less critical. 242 } else {
234 */ 243 spin_lock_irqsave(lock, *flags);
235 if (need_resched()) 244 }
236 return COMPACT_CONTENDED_SCHED;
237 else if (spin_is_contended(lock))
238 return COMPACT_CONTENDED_LOCK;
239 245
240 return COMPACT_CONTENDED_NONE; 246 return true;
241} 247}
242 248
243/* 249/*
244 * Compaction requires the taking of some coarse locks that are potentially 250 * Compaction requires the taking of some coarse locks that are potentially
245 * very heavily contended. Check if the process needs to be scheduled or 251 * very heavily contended. The lock should be periodically unlocked to avoid
246 * if the lock is contended. For async compaction, back out in the event 252 * having disabled IRQs for a long time, even when there is nobody waiting on
247 * if contention is severe. For sync compaction, schedule. 253 * the lock. It might also be that allowing the IRQs will result in
254 * need_resched() becoming true. If scheduling is needed, async compaction
255 * aborts. Sync compaction schedules.
256 * Either compaction type will also abort if a fatal signal is pending.
257 * In either case if the lock was locked, it is dropped and not regained.
248 * 258 *
249 * Returns true if the lock is held. 259 * Returns true if compaction should abort due to fatal signal pending, or
250 * Returns false if the lock is released and compaction should abort 260 * async compaction due to need_resched()
261 * Returns false when compaction can continue (sync compaction might have
262 * scheduled)
251 */ 263 */
252static bool compact_checklock_irqsave(spinlock_t *lock, unsigned long *flags, 264static bool compact_unlock_should_abort(spinlock_t *lock,
253 bool locked, struct compact_control *cc) 265 unsigned long flags, bool *locked, struct compact_control *cc)
254{ 266{
255 int contended = should_release_lock(lock); 267 if (*locked) {
268 spin_unlock_irqrestore(lock, flags);
269 *locked = false;
270 }
256 271
257 if (contended) { 272 if (fatal_signal_pending(current)) {
258 if (locked) { 273 cc->contended = COMPACT_CONTENDED_SCHED;
259 spin_unlock_irqrestore(lock, *flags); 274 return true;
260 locked = false; 275 }
261 }
262 276
263 /* async aborts if taking too long or contended */ 277 if (need_resched()) {
264 if (cc->mode == MIGRATE_ASYNC) { 278 if (cc->mode == MIGRATE_ASYNC) {
265 cc->contended = contended; 279 cc->contended = COMPACT_CONTENDED_SCHED;
266 return false; 280 return true;
267 } 281 }
268
269 cond_resched(); 282 cond_resched();
270 } 283 }
271 284
272 if (!locked) 285 return false;
273 spin_lock_irqsave(lock, *flags);
274 return true;
275} 286}
276 287
277/* 288/*
278 * Aside from avoiding lock contention, compaction also periodically checks 289 * Aside from avoiding lock contention, compaction also periodically checks
279 * need_resched() and either schedules in sync compaction or aborts async 290 * need_resched() and either schedules in sync compaction or aborts async
280 * compaction. This is similar to what compact_checklock_irqsave() does, but 291 * compaction. This is similar to what compact_unlock_should_abort() does, but
281 * is used where no lock is concerned. 292 * is used where no lock is concerned.
282 * 293 *
283 * Returns false when no scheduling was needed, or sync compaction scheduled. 294 * Returns false when no scheduling was needed, or sync compaction scheduled.
@@ -336,6 +347,16 @@ static unsigned long isolate_freepages_block(struct compact_control *cc,
336 int isolated, i; 347 int isolated, i;
337 struct page *page = cursor; 348 struct page *page = cursor;
338 349
350 /*
351 * Periodically drop the lock (if held) regardless of its
352 * contention, to give chance to IRQs. Abort if fatal signal
353 * pending or async compaction detects need_resched()
354 */
355 if (!(blockpfn % SWAP_CLUSTER_MAX)
356 && compact_unlock_should_abort(&cc->zone->lock, flags,
357 &locked, cc))
358 break;
359
339 nr_scanned++; 360 nr_scanned++;
340 if (!pfn_valid_within(blockpfn)) 361 if (!pfn_valid_within(blockpfn))
341 goto isolate_fail; 362 goto isolate_fail;
@@ -353,8 +374,9 @@ static unsigned long isolate_freepages_block(struct compact_control *cc,
353 * spin on the lock and we acquire the lock as late as 374 * spin on the lock and we acquire the lock as late as
354 * possible. 375 * possible.
355 */ 376 */
356 locked = compact_checklock_irqsave(&cc->zone->lock, &flags, 377 if (!locked)
357 locked, cc); 378 locked = compact_trylock_irqsave(&cc->zone->lock,
379 &flags, cc);
358 if (!locked) 380 if (!locked)
359 break; 381 break;
360 382
@@ -552,13 +574,15 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
552 574
553 /* Time to isolate some pages for migration */ 575 /* Time to isolate some pages for migration */
554 for (; low_pfn < end_pfn; low_pfn++) { 576 for (; low_pfn < end_pfn; low_pfn++) {
555 /* give a chance to irqs before checking need_resched() */ 577 /*
556 if (locked && !(low_pfn % SWAP_CLUSTER_MAX)) { 578 * Periodically drop the lock (if held) regardless of its
557 if (should_release_lock(&zone->lru_lock)) { 579 * contention, to give chance to IRQs. Abort async compaction
558 spin_unlock_irqrestore(&zone->lru_lock, flags); 580 * if contended.
559 locked = false; 581 */
560 } 582 if (!(low_pfn % SWAP_CLUSTER_MAX)
561 } 583 && compact_unlock_should_abort(&zone->lru_lock, flags,
584 &locked, cc))
585 break;
562 586
563 if (!pfn_valid_within(low_pfn)) 587 if (!pfn_valid_within(low_pfn))
564 continue; 588 continue;
@@ -620,10 +644,11 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
620 page_count(page) > page_mapcount(page)) 644 page_count(page) > page_mapcount(page))
621 continue; 645 continue;
622 646
623 /* Check if it is ok to still hold the lock */ 647 /* If the lock is not held, try to take it */
624 locked = compact_checklock_irqsave(&zone->lru_lock, &flags, 648 if (!locked)
625 locked, cc); 649 locked = compact_trylock_irqsave(&zone->lru_lock,
626 if (!locked || fatal_signal_pending(current)) 650 &flags, cc);
651 if (!locked)
627 break; 652 break;
628 653
629 /* Recheck PageLRU and PageTransHuge under lock */ 654 /* Recheck PageLRU and PageTransHuge under lock */