Miaohe Lin 2799e77529 swap: fix do_swap_page() race with swapoff
When I was investigating the swap code, I found the below possible race
window:

CPU 1                                   	CPU 2
-----                                   	-----
do_swap_page
  if (data_race(si->flags & SWP_SYNCHRONOUS_IO)
  swap_readpage
    if (data_race(sis->flags & SWP_FS_OPS)) {
                                        	swapoff
					  	  ..
					  	  p->swap_file = NULL;
					  	  ..
    struct file *swap_file = sis->swap_file;
    struct address_space *mapping = swap_file->f_mapping;[oops!]

Note that for the pages that are swapped in through swap cache, this isn't
an issue. Because the page is locked, and the swap entry will be marked
with SWAP_HAS_CACHE, so swapoff() can not proceed until the page has been
unlocked.

Fix this race by using get/put_swap_device() to guard against concurrent
swapoff.

Link: https://lkml.kernel.org/r/20210426123316.806267-3-linmiaohe@huawei.com
Fixes: 0bcac06f27d7 ("mm,swap: skip swapcache for swapin of synchronous device")
Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
Reviewed-by: "Huang, Ying" <ying.huang@intel.com>
Cc: Alex Shi <alexs@kernel.org>
Cc: David Hildenbrand <david@redhat.com>
Cc: Dennis Zhou <dennis@kernel.org>
Cc: Hugh Dickins <hughd@google.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Minchan Kim <minchan@kernel.org>
Cc: Tim Chen <tim.c.chen@linux.intel.com>
Cc: Wei Yang <richard.weiyang@gmail.com>
Cc: Yang Shi <shy828301@gmail.com>
Cc: Yu Zhao <yuzhao@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2021-06-29 10:53:49 -07:00
..
2021-04-26 16:32:11 -07:00
2021-05-05 12:39:29 -07:00
2021-02-02 00:16:57 +01:00
2021-06-08 09:41:16 -07:00
2021-01-23 14:57:21 +01:00
2021-05-18 17:24:52 +01:00
2021-05-06 19:24:11 -07:00
2021-04-14 16:30:30 +03:00
2021-05-08 21:49:48 -06:00
2021-05-15 08:52:30 -07:00
2021-04-29 11:57:23 -07:00
2021-01-24 14:27:17 +01:00
2021-02-26 09:41:03 -08:00
2021-04-08 16:04:20 -07:00
2021-05-24 12:45:26 -04:00
2021-05-24 12:45:26 -04:00
2021-05-01 10:14:08 -07:00
2021-04-08 16:04:20 -07:00
2021-04-08 16:04:20 -07:00
2021-01-21 14:06:00 -07:00
2021-05-04 11:24:46 -07:00
2021-05-06 19:24:11 -07:00
2021-04-19 13:13:26 -04:00
2021-01-18 14:26:51 +01:00
2021-03-22 03:57:39 +01:00
2021-02-08 12:28:07 +01:00
2021-04-12 15:04:23 +02:00
2021-03-30 22:37:06 +02:00
2021-06-29 10:53:48 -07:00
2021-04-28 17:22:10 -07:00
2021-02-11 13:24:44 -08:00
2021-02-23 13:39:45 -08:00
2021-04-30 11:20:40 -07:00
2021-06-29 10:53:48 -07:00
2021-04-26 15:03:23 -07:00
2021-04-26 10:25:03 -07:00
2021-03-22 04:23:14 +01:00
2021-05-05 12:39:29 -07:00
2021-04-05 22:39:38 -04:00
2021-01-21 16:16:10 +00:00
2021-02-26 09:41:02 -08:00
2021-02-26 09:41:03 -08:00
2021-04-28 12:37:53 -07:00
2021-04-08 16:04:20 -07:00
2021-03-29 15:56:48 +02:00
2021-05-07 11:23:41 -07:00
2021-04-06 08:34:39 +02:00
2021-02-17 13:17:49 -08:00
2021-04-21 10:24:41 -05:00
2021-02-26 09:40:59 -08:00
2021-02-03 19:05:50 +01:00
2021-05-01 10:14:08 -07:00
2021-01-24 14:27:17 +01:00
2021-01-24 14:27:20 +01:00
2021-03-06 12:40:22 +01:00
2021-02-26 09:41:03 -08:00
2021-04-28 12:00:13 -07:00
2021-05-05 13:24:11 -07:00
2021-03-17 14:16:15 -05:00
2021-01-16 23:19:26 +01:00
2021-03-22 02:45:52 +01:00
2021-03-18 12:58:27 -04:00
2021-03-19 04:58:25 +00:00
2021-03-25 17:39:33 -07:00
2021-03-10 09:34:06 +01:00
2021-04-15 10:32:17 +02:00
2021-02-13 17:17:53 +01:00
2021-03-30 17:06:49 -07:00
2021-04-26 22:55:12 +01:00
2021-04-09 16:00:00 +02:00
\n
2021-04-29 11:06:13 -07:00
2021-01-21 16:16:10 +00:00
2021-03-22 15:02:14 +01:00
2021-06-24 19:40:53 -07:00
2021-02-09 12:15:07 +01:00
2021-01-21 14:06:00 -07:00
2021-04-16 15:31:02 -07:00
2021-03-30 13:42:33 -04:00
2021-01-24 14:27:17 +01:00
2021-02-08 22:58:55 +01:00