* [PATCH v2] mm: zswap: fix potential memory corruption on duplicate store
@ 2023-09-25 13:00 Domenico Cerasuolo
0 siblings, 0 replies; only message in thread
From: Domenico Cerasuolo @ 2023-09-25 13:00 UTC (permalink / raw)
To: sjenning, ddstreet, vitaly.wool, akpm, nphamcs, hannes
Cc: linux-mm, linux-kernel, kernel-team, Domenico Cerasuolo
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.
V2:
- Added a warning and a comment to the second duplicates check in
zswap_store function (Johannes).
Based on commit ce9ecca0238b ("Linux 6.6-rc2")
Fixes: 42c06a0e8ebe ("mm: kill frontswap")
Signed-off-by: Domenico Cerasuolo <cerasuolodomenico@gmail.com>
---
mm/zswap.c | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)
diff --git a/mm/zswap.c b/mm/zswap.c
index 412b1409a0d7..083c693602b8 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -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
@@ -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);
}
--
2.34.1
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2023-09-25 13:00 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-25 13:00 [PATCH v2] mm: zswap: fix potential memory corruption on duplicate store Domenico Cerasuolo
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox