summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHuang Ying <ying.huang@intel.com>2018-04-23 08:20:28 +1000
committerStephen Rothwell <sfr@canb.auug.org.au>2018-04-23 08:23:48 +1000
commit4536719200134075e89315ca0733f3b1b54b298b (patch)
tree5092e17262e68b9766ce7909e1ee5bc2777d3b94
parenta07fb3c26ee02e8811e8f1a626036b5cd010c7b2 (diff)
downloadlinux-4536719200134075e89315ca0733f3b1b54b298b.tar.gz
linux-4536719200134075e89315ca0733f3b1b54b298b.tar.xz
mm, swap: fix race between swapoff and some swap operations
- Add more comments to get_swap_device() to make it more clear about possible swapoff or swapoff+swapon. Link: http://lkml.kernel.org/r/20180223060010.954-1-ying.huang@intel.com Signed-off-by: "Huang, Ying" <ying.huang@intel.com> Cc: Hugh Dickins <hughd@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Stephen Rothwell <sfr@canb.auug.org.au>
-rw-r--r--mm/memory.c2
-rw-r--r--mm/swapfile.c23
2 files changed, 24 insertions, 1 deletions
diff --git a/mm/memory.c b/mm/memory.c
index 658a9da803c3..3b4e98f80b39 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -2922,7 +2922,7 @@ int do_swap_page(struct vm_fault *vmf)
struct swap_info_struct *si = swp_swap_info(entry);
if (si->flags & SWP_SYNCHRONOUS_IO &&
- __swap_count(entry) == 1) {
+ __swap_count(entry) == 1) {
/* skip swapcache */
page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma,
vmf->address);
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 5b9bb760532f..254a511c51d0 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -1113,6 +1113,29 @@ static struct swap_info_struct *swap_info_get_cont(swp_entry_t entry,
* return pointer to swap_info_struct, and keep the swap entry valid
* via preventing the swap device from being swapoff, until
* put_swap_device() is called. Otherwise return NULL.
+ *
+ * Notice that swapoff or swapoff+swapon can still happen before the
+ * preempt_disable() in get_swap_device() or after the
+ * preempt_enable() in put_swap_device() if there isn't any other way
+ * to prevent swapoff, such as page lock, page table lock, etc. The
+ * caller must be prepared for that. For example, the following
+ * situation is possible.
+ *
+ * CPU1 CPU2
+ * do_swap_page()
+ * ... swapoff+swapon
+ * __read_swap_cache_async()
+ * swapcache_prepare()
+ * __swap_duplicate()
+ * // check swap_map
+ * // verify PTE not changed
+ *
+ * In __swap_duplicate(), the swap_map need to be checked before
+ * changing partly because the specified swap entry may be for another
+ * swap device which has been swapoff. And in do_swap_page(), after
+ * the page is read from the swap device, the PTE is verified not
+ * changed with the page table locked to check whether the swap device
+ * has been swapoff or swapoff+swapon.
*/
struct swap_info_struct *get_swap_device(swp_entry_t entry)
{