]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob
34c43a54e69e420e38dd10830a42cd03aeed81cc
[thirdparty/kernel/stable-queue.git] /
1 From cbab0e4eec299e9059199ebe6daf48730be46d2b Mon Sep 17 00:00:00 2001
2 From: Rafael Aquini <aquini@redhat.com>
3 Date: Wed, 12 Jun 2013 14:04:49 -0700
4 Subject: swap: avoid read_swap_cache_async() race to deadlock while waiting on discard I/O completion
5
6 From: Rafael Aquini <aquini@redhat.com>
7
8 commit cbab0e4eec299e9059199ebe6daf48730be46d2b upstream.
9
10 read_swap_cache_async() can race against get_swap_page(), and stumble
11 across a SWAP_HAS_CACHE entry in the swap map whose page wasn't brought
12 into the swapcache yet.
13
14 This transient swap_map state is expected to be transitory, but the
15 actual placement of discard at scan_swap_map() inserts a wait for I/O
16 completion thus making the thread at read_swap_cache_async() to loop
17 around its -EEXIST case, while the other end at get_swap_page() is
18 scheduled away at scan_swap_map(). This can leave the system deadlocked
19 if the I/O completion happens to be waiting on the CPU waitqueue where
20 read_swap_cache_async() is busy looping and !CONFIG_PREEMPT.
21
22 This patch introduces a cond_resched() call to make the aforementioned
23 read_swap_cache_async() busy loop condition to bail out when necessary,
24 thus avoiding the subtle race window.
25
26 Signed-off-by: Rafael Aquini <aquini@redhat.com>
27 Acked-by: Johannes Weiner <hannes@cmpxchg.org>
28 Acked-by: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
29 Acked-by: Hugh Dickins <hughd@google.com>
30 Cc: Shaohua Li <shli@kernel.org>
31 Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
32 Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
33 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
34
35 ---
36 mm/swap_state.c | 18 +++++++++++++++++-
37 1 file changed, 17 insertions(+), 1 deletion(-)
38
39 --- a/mm/swap_state.c
40 +++ b/mm/swap_state.c
41 @@ -315,8 +315,24 @@ struct page *read_swap_cache_async(swp_e
42 * Swap entry may have been freed since our caller observed it.
43 */
44 err = swapcache_prepare(entry);
45 - if (err == -EEXIST) { /* seems racy */
46 + if (err == -EEXIST) {
47 radix_tree_preload_end();
48 + /*
49 + * We might race against get_swap_page() and stumble
50 + * across a SWAP_HAS_CACHE swap_map entry whose page
51 + * has not been brought into the swapcache yet, while
52 + * the other end is scheduled away waiting on discard
53 + * I/O completion at scan_swap_map().
54 + *
55 + * In order to avoid turning this transitory state
56 + * into a permanent loop around this -EEXIST case
57 + * if !CONFIG_PREEMPT and the I/O completion happens
58 + * to be waiting on the CPU waitqueue where we are now
59 + * busy looping, we just conditionally invoke the
60 + * scheduler here, if there are some more important
61 + * tasks to run.
62 + */
63 + cond_resched();
64 continue;
65 }
66 if (err) { /* swp entry is obsolete ? */