Commit 18d9304b892c9f090e42dca40d041586519951be

Authored by Vlastimil Babka
Committed by Greg Kroah-Hartman
1 parent b36cd20d35

mm, vmscan: prevent kswapd livelock due to pfmemalloc-throttled process being killed

commit 9e5e3661727eaf960d3480213f8e87c8d67b6956 upstream.

Charles Shirron and Paul Cassella from Cray Inc have reported kswapd
stuck in a busy loop with nothing left to balance, but
kswapd_try_to_sleep() failing to sleep.  Their analysis found the cause
to be a combination of several factors:

1. A process is waiting in throttle_direct_reclaim() on pgdat->pfmemalloc_wait

2. The process has been killed (by OOM in this case), but has not yet been
   scheduled to remove itself from the waitqueue and die.

3. kswapd checks for throttled processes in prepare_kswapd_sleep():

        if (waitqueue_active(&pgdat->pfmemalloc_wait)) {
                wake_up(&pgdat->pfmemalloc_wait);
		return false; // kswapd will not go to sleep
	}

   However, for a process that was already killed, wake_up() does not remove
   the process from the waitqueue, since try_to_wake_up() checks its state
   first and returns false when the process is no longer waiting.

4. kswapd is running on the same CPU as the only CPU that the process is
   allowed to run on (through cpus_allowed, or possibly single-cpu system).

5. CONFIG_PREEMPT_NONE=y kernel is used. If there's nothing to balance, kswapd
   encounters no voluntary preemption points and repeatedly fails
   prepare_kswapd_sleep(), blocking the process from running and removing
   itself from the waitqueue, which would let kswapd sleep.

So, the source of the problem is that we prevent kswapd from going to
sleep until there are processes waiting on the pfmemalloc_wait queue,
and a process waiting on a queue is guaranteed to be removed from the
queue only when it gets scheduled.  This was done to make sure that no
process is left sleeping on pfmemalloc_wait when kswapd itself goes to
sleep.

However, it isn't necessary to postpone kswapd sleep until the
pfmemalloc_wait queue actually empties.  To prevent processes from being
left sleeping, it's actually enough to guarantee that all processes
waiting on pfmemalloc_wait queue have been woken up by the time we put
kswapd to sleep.

This patch therefore fixes this issue by substituting 'wake_up' with
'wake_up_all' and removing 'return false' in the code snippet from
prepare_kswapd_sleep() above.  Note that if any process puts itself in
the queue after this waitqueue_active() check, or after the wake up
itself, it means that the process will also wake up kswapd - and since
we are under prepare_to_wait(), the wake up won't be missed.  Also we
update the comment prepare_kswapd_sleep() to hopefully more clearly
describe the races it is preventing.

Fixes: 5515061d22f0 ("mm: throttle direct reclaimers if PF_MEMALLOC reserves are low and swap is backed by network storage")
Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Vladimir Davydov <vdavydov@parallels.com>
Cc: Mel Gorman <mgorman@suse.de>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Acked-by: Michal Hocko <mhocko@suse.cz>
Acked-by: Rik van Riel <riel@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Showing 1 changed file with 13 additions and 11 deletions Side-by-side Diff

... ... @@ -2860,18 +2860,20 @@
2860 2860 return false;
2861 2861  
2862 2862 /*
2863   - * There is a potential race between when kswapd checks its watermarks
2864   - * and a process gets throttled. There is also a potential race if
2865   - * processes get throttled, kswapd wakes, a large process exits therby
2866   - * balancing the zones that causes kswapd to miss a wakeup. If kswapd
2867   - * is going to sleep, no process should be sleeping on pfmemalloc_wait
2868   - * so wake them now if necessary. If necessary, processes will wake
2869   - * kswapd and get throttled again
  2863 + * The throttled processes are normally woken up in balance_pgdat() as
  2864 + * soon as pfmemalloc_watermark_ok() is true. But there is a potential
  2865 + * race between when kswapd checks the watermarks and a process gets
  2866 + * throttled. There is also a potential race if processes get
  2867 + * throttled, kswapd wakes, a large process exits thereby balancing the
  2868 + * zones, which causes kswapd to exit balance_pgdat() before reaching
  2869 + * the wake up checks. If kswapd is going to sleep, no process should
  2870 + * be sleeping on pfmemalloc_wait, so wake them now if necessary. If
  2871 + * the wake up is premature, processes will wake kswapd and get
  2872 + * throttled again. The difference from wake ups in balance_pgdat() is
  2873 + * that here we are under prepare_to_wait().
2870 2874 */
2871   - if (waitqueue_active(&pgdat->pfmemalloc_wait)) {
2872   - wake_up(&pgdat->pfmemalloc_wait);
2873   - return false;
2874   - }
  2875 + if (waitqueue_active(&pgdat->pfmemalloc_wait))
  2876 + wake_up_all(&pgdat->pfmemalloc_wait);
2875 2877  
2876 2878 return pgdat_balanced(pgdat, order, classzone_idx);
2877 2879 }