Skip to content

Commit

Permalink
mm: zswap: fix potential memory corruption on duplicate store
Browse files Browse the repository at this point in the history
While stress-testing zswap a memory corruption was happening when writing
back pages.  __frontswap_store used to check for duplicate entries before
attempting to store a page in zswap, this was because if the store fails
the old entry isn't removed from the tree.  This change removes duplicate
entries in zswap_store before the actual attempt.

[[email protected]: add a warning and a comment, per Johannes]
  Link: https://lkml.kernel.org/r/[email protected]
Link: https://lkml.kernel.org/r/[email protected]
Fixes: 42c06a0 ("mm: kill frontswap")
Signed-off-by: Domenico Cerasuolo <[email protected]>
Acked-by: Johannes Weiner <[email protected]>
Acked-by: Nhat Pham <[email protected]>
Cc: Dan Streetman <[email protected]>
Cc: Domenico Cerasuolo <[email protected]>
Cc: Seth Jennings <[email protected]>
Cc: Vitaly Wool <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
  • Loading branch information
lelloman authored and akpm00 committed Sep 30, 2023
1 parent 6f1bace commit ca56489
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions mm/zswap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1218,6 +1218,19 @@ bool zswap_store(struct folio *folio)
if (!zswap_enabled || !tree)
return false;

/*
* If this is a duplicate, it must be removed before attempting to store
* it, otherwise, if the store fails the old page won't be removed from
* the tree, and it might be written back overriding the new data.
*/
spin_lock(&tree->lock);
dupentry = zswap_rb_search(&tree->rbroot, offset);
if (dupentry) {
zswap_duplicate_entry++;
zswap_invalidate_entry(tree, dupentry);
}
spin_unlock(&tree->lock);

/*
* XXX: zswap reclaim does not work with cgroups yet. Without a
* cgroup-aware entry LRU, we will push out entries system-wide based on
Expand Down Expand Up @@ -1333,7 +1346,14 @@ bool zswap_store(struct folio *folio)

/* map */
spin_lock(&tree->lock);
/*
* A duplicate entry should have been removed at the beginning of this
* function. Since the swap entry should be pinned, if a duplicate is
* found again here it means that something went wrong in the swap
* cache.
*/
while (zswap_rb_insert(&tree->rbroot, entry, &dupentry) == -EEXIST) {
WARN_ON(1);
zswap_duplicate_entry++;
zswap_invalidate_entry(tree, dupentry);
}
Expand Down

0 comments on commit ca56489

Please sign in to comment.