]> Git Repo - linux.git/commitdiff
mm: zswap: fix data loss on SWP_SYNCHRONOUS_IO devices
authorJohannes Weiner <[email protected]>
Sun, 24 Mar 2024 21:04:47 +0000 (17:04 -0400)
committerAndrew Morton <[email protected]>
Tue, 26 Mar 2024 18:14:12 +0000 (11:14 -0700)
Zhongkun He reports data corruption when combining zswap with zram.

The issue is the exclusive loads we're doing in zswap. They assume
that all reads are going into the swapcache, which can assume
authoritative ownership of the data and so the zswap copy can go.

However, zram files are marked SWP_SYNCHRONOUS_IO, and faults will try to
bypass the swapcache.  This results in an optimistic read of the swap data
into a page that will be dismissed if the fault fails due to races.  In
this case, zswap mustn't drop its authoritative copy.

Link: https://lore.kernel.org/all/CACSyD1N+dUvsu8=zV9P691B9bVq33erwOXNTmEaUbi9DrDeJzw@mail.gmail.com/
Fixes: b9c91c43412f ("mm: zswap: support exclusive loads")
Link: https://lkml.kernel.org/r/[email protected]
Signed-off-by: Johannes Weiner <[email protected]>
Reported-by: Zhongkun He <[email protected]>
Tested-by: Zhongkun He <[email protected]>
Acked-by: Yosry Ahmed <[email protected]>
Acked-by: Barry Song <[email protected]>
Reviewed-by: Chengming Zhou <[email protected]>
Reviewed-by: Nhat Pham <[email protected]>
Acked-by: Chris Li <[email protected]>
Cc: <[email protected]> [6.5+]
Signed-off-by: Andrew Morton <[email protected]>
mm/zswap.c

index 36612f34b5d720f53279b1bf9cf9f25867e79703..caed028945b046cf4caa4c842abfc900ef0a45fb 100644 (file)
@@ -1636,6 +1636,7 @@ bool zswap_load(struct folio *folio)
        swp_entry_t swp = folio->swap;
        pgoff_t offset = swp_offset(swp);
        struct page *page = &folio->page;
+       bool swapcache = folio_test_swapcache(folio);
        struct zswap_tree *tree = swap_zswap_tree(swp);
        struct zswap_entry *entry;
        u8 *dst;
@@ -1648,7 +1649,20 @@ bool zswap_load(struct folio *folio)
                spin_unlock(&tree->lock);
                return false;
        }
-       zswap_rb_erase(&tree->rbroot, entry);
+       /*
+        * When reading into the swapcache, invalidate our entry. The
+        * swapcache can be the authoritative owner of the page and
+        * its mappings, and the pressure that results from having two
+        * in-memory copies outweighs any benefits of caching the
+        * compression work.
+        *
+        * (Most swapins go through the swapcache. The notable
+        * exception is the singleton fault on SWP_SYNCHRONOUS_IO
+        * files, which reads into a private page and may free it if
+        * the fault fails. We remain the primary owner of the entry.)
+        */
+       if (swapcache)
+               zswap_rb_erase(&tree->rbroot, entry);
        spin_unlock(&tree->lock);
 
        if (entry->length)
@@ -1663,9 +1677,10 @@ bool zswap_load(struct folio *folio)
        if (entry->objcg)
                count_objcg_event(entry->objcg, ZSWPIN);
 
-       zswap_entry_free(entry);
-
-       folio_mark_dirty(folio);
+       if (swapcache) {
+               zswap_entry_free(entry);
+               folio_mark_dirty(folio);
+       }
 
        return true;
 }
This page took 0.058662 seconds and 4 git commands to generate.