Skip to contentSkip to conversation

mm/zswap: invalidate old entry when store fail or !zswap_enabled

PATCH Superseded [email protected] 3 participants 5 messages Feb 7, 2024

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

[email protected]

author reviewer
#0 lore raw

PATCH v3 1/1 mm/zswap: invalidate old entry when store fail or !zswap_enabled

From: Chengming Zhou <[email protected]>

We may encounter duplicate entry in the zswap_store():

1. swap slot that freed to per-cpu swap cache, doesn't invalidate the zswap entry, then got reused. This has been fixed.

2. !exclusive load mode, swapin folio will leave its zswap entry on the tree, then swapout again. This has been removed.

3. one folio can be dirtied again after zswap_store(), so need to zswap_store() again. This should be handled correctly.

So we must invalidate the old duplicate entry before insert the new one, which actually doesn't have to be done at the beginning of zswap_store(). And this is a normal situation, we shouldn't WARN_ON(1) in this case, so delete it. (The WARN_ON(1) seems want to detect swap entry UAF problem? But not very necessary here.)

The good point is that we don't need to lock tree twice in the store success path.

Note we still need to invalidate the old duplicate entry in the store failure path, otherwise the new data in swapfile could be overwrite by the old data in zswap pool when lru writeback.

We have to do this even when !zswap_enabled since zswap can be disabled anytime. If the folio store success before, then got dirtied again but zswap disabled, we won't invalidate the old duplicate entry in the zswap_store(). So later lru writeback may overwrite the new data in swapfile.

---
v3:
- Fix a few grammatical problems in comments, per Yosry.

v2:
- Change the duplicate entry invalidation loop to if, since we hold the lock, we won't find it once we invalidate it, per Yosry.
- Add Fixes tag.

---
 mm/zswap.c | 33 ++++++++++++++++-----------------
 1 file changed, 16 insertions(+), 17 deletions(-)
mm/zswap.c +16 -17
index cd67f7f6b302..d9d8947d6761 100644
@@ -1518,18 +1518,8 @@ bool zswap_store(struct folio *folio)
 		return false;
 
 	if (!zswap_enabled)
-		return false;
+		goto check_old;
 
-	/*
-	 * 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);
-	entry = zswap_rb_search(&tree->rbroot, offset);
-	if (entry)
-		zswap_invalidate_entry(tree, entry);
-	spin_unlock(&tree->lock);
 	objcg = get_obj_cgroup_from_folio(folio);
 	if (objcg && !obj_cgroup_may_zswap(objcg)) {
 		memcg = get_mem_cgroup_from_objcg(objcg);
@@ -1608,14 +1598,12 @@ 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.
+	 * The folio may have been dirtied again, invalidate the
+	 * possibly stale entry before inserting the new entry.
 	 */
-	while (zswap_rb_insert(&tree->rbroot, entry, &dupentry) == -EEXIST) {
-		WARN_ON(1);
+	if (zswap_rb_insert(&tree->rbroot, entry, &dupentry) == -EEXIST) {
 		zswap_invalidate_entry(tree, dupentry);
+		VM_WARN_ON(zswap_rb_insert(&tree->rbroot, entry, &dupentry));
 	}
 	if (entry->length) {
 		INIT_LIST_HEAD(&entry->lru);
@@ -1638,6 +1626,17 @@ bool zswap_store(struct folio *folio)
 reject:
 	if (objcg)
 		obj_cgroup_put(objcg);
+check_old:
+	/*
+	 * If the zswap store fails or zswap is disabled, we must invalidate the
+	 * possibly stale entry which was previously stored at this offset.
+	 * Otherwise, writeback could overwrite the new data in the swapfile.
+	 */
+	spin_lock(&tree->lock);
+	entry = zswap_rb_search(&tree->rbroot, offset);
+	if (entry)
+		zswap_invalidate_entry(tree, entry);
+	spin_unlock(&tree->lock);
 	return false;
 
 shrink:
Signature · 1 line
2.40.1
#1 lore raw
Quoted 5 lines · On Tue, Feb 6, 2024 at 7:39 PM <[email protected]> wrote:

From: Chengming Zhou <[email protected]>

We may encounter duplicate entry in the zswap_store():

Quoted 8 lines

1. swap slot that freed to per-cpu swap cache, doesn't invalidate the zswap entry, then got reused. This has been fixed.

2. !exclusive load mode, swapin folio will leave its zswap entry on the tree, then swapout again. This has been removed.

3. one folio can be dirtied again after zswap_store(), so need to zswap_store() again. This should be handled correctly.

Thanks, I have been wondering about the cause of that for a while.

Quoted 67 lines

So we must invalidate the old duplicate entry before insert the new one, which actually doesn't have to be done at the beginning of zswap_store(). And this is a normal situation, we shouldn't WARN_ON(1) in this case, so delete it. (The WARN_ON(1) seems want to detect swap entry UAF problem? But not very necessary here.)

The good point is that we don't need to lock tree twice in the store success path.

Note we still need to invalidate the old duplicate entry in the store failure path, otherwise the new data in swapfile could be overwrite by the old data in zswap pool when lru writeback.

We have to do this even when !zswap_enabled since zswap can be disabled anytime. If the folio store success before, then got dirtied again but zswap disabled, we won't invalidate the old duplicate entry in the zswap_store(). So later lru writeback may overwrite the new data in swapfile.

---
v3:
- Fix a few grammatical problems in comments, per Yosry.

v2:
- Change the duplicate entry invalidation loop to if, since we hold the lock, we won't find it once we invalidate it, per Yosry.
- Add Fixes tag.

---
 mm/zswap.c | 33 ++++++++++++++++-----------------
 1 file changed, 16 insertions(+), 17 deletions(-)
mm/zswap.c +1 -13
index cd67f7f6b302..d9d8947d6761 100644
@@ -1518,18 +1518,8 @@ bool zswap_store(struct folio *folio)
                return false;
 
        if (!zswap_enabled)
-               return false;
+               goto check_old;
 
-       /*
-        * 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);
-       entry = zswap_rb_search(&tree->rbroot, offset);
-       if (entry)
-               zswap_invalidate_entry(tree, entry);
-       spin_unlock(&tree->lock);
        objcg = get_obj_cgroup_from_folio(folio);
        if (objcg && !obj_cgroup_may_zswap(objcg)) {
                memcg = get_mem_cgroup_from_objcg(objcg);
@@ -1608,14 +1598,12 @@ 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

mm/zswap.c bool zswap_store(struct folio *folio) · line 1606

Quoted 10 lines
mm/zswap.c +4 -4
-        * found again here it means that something went wrong in the swap
-        * cache.
+        * The folio may have been dirtied again, invalidate the
+        * possibly stale entry before inserting the new entry.
         */
-       while (zswap_rb_insert(&tree->rbroot, entry, &dupentry) == -EEXIST) {
-               WARN_ON(1);
+       if (zswap_rb_insert(&tree->rbroot, entry, &dupentry) == -EEXIST) {
                zswap_invalidate_entry(tree, dupentry);
+               VM_WARN_ON(zswap_rb_insert(&tree->rbroot, entry, &dupentry));

It seems there is only one path called zswap_rb_insert() and there is no loop to repeat the insert any more. Can we have the zswap_rb_insert() install the entry and return the dupentry? We can still just call zswap_invalidate_entry() on the duplicate. The mapping of the dupentry has been removed when zswap_rb_insert() returns. That will save a repeat lookup on the duplicate case.
After this change, the zswap_rb_insert() will map to the xarray xa_store() pretty nicely.

Chris

Quoted 25 lines
mm/zswap.c +11 -1
        }
        if (entry->length) {
                INIT_LIST_HEAD(&entry->lru);
@@ -1638,6 +1626,17 @@ bool zswap_store(struct folio *folio)
 reject:
        if (objcg)
                obj_cgroup_put(objcg);
+check_old:
+       /*
+        * If the zswap store fails or zswap is disabled, we must invalidate the
+        * possibly stale entry which was previously stored at this offset.
+        * Otherwise, writeback could overwrite the new data in the swapfile.
+        */
+       spin_lock(&tree->lock);
+       entry = zswap_rb_search(&tree->rbroot, offset);
+       if (entry)
+               zswap_invalidate_entry(tree, entry);
+       spin_unlock(&tree->lock);
        return false;
 
 shrink:
--

2.40.1

#2 lore raw
Quoted 17 lines
Quoted 6 lines
mm/zswap.c +0 -2
@@ -1608,14 +1598,12 @@ 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

mm/zswap.c bool zswap_store(struct folio *folio) · line 1606

Quoted 10 lines
mm/zswap.c +4 -4
-        * found again here it means that something went wrong in the swap
-        * cache.
+        * The folio may have been dirtied again, invalidate the
+        * possibly stale entry before inserting the new entry.
         */
-       while (zswap_rb_insert(&tree->rbroot, entry, &dupentry) == -EEXIST) {
-               WARN_ON(1);
+       if (zswap_rb_insert(&tree->rbroot, entry, &dupentry) == -EEXIST) {
                zswap_invalidate_entry(tree, dupentry);
+               VM_WARN_ON(zswap_rb_insert(&tree->rbroot, entry, &dupentry));
Quoted 8 lines

It seems there is only one path called zswap_rb_insert() and there is no loop to repeat the insert any more. Can we have the zswap_rb_insert() install the entry and return the dupentry? We can still just call zswap_invalidate_entry() on the duplicate. The mapping of the dupentry has been removed when zswap_rb_insert() returns. That will save a repeat lookup on the duplicate case.
After this change, the zswap_rb_insert() will map to the xarray xa_store() pretty nicely.

I brought this up in v1 [1]. We agreed to leave it as-is for now since we expect the xarray conversion soon-ish. No need to update zswap_rb_insert() only to replace it with xa_store() later anyway.

[1] https://lore.kernel.org/lkml/[email protected]/

Acked-by
#3 lore raw
Quoted 27 lines · Yosry Ahmed
Quoted 18 lines
Quoted 7 lines
mm/zswap.c +0 -2
@@ -1608,14 +1598,12 @@ 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

mm/zswap.c bool zswap_store(struct folio *folio) · line 1606

Quoted 10 lines
mm/zswap.c +4 -4
-        * found again here it means that something went wrong in the swap
-        * cache.
+        * The folio may have been dirtied again, invalidate the
+        * possibly stale entry before inserting the new entry.
         */
-       while (zswap_rb_insert(&tree->rbroot, entry, &dupentry) == -EEXIST) {
-               WARN_ON(1);
+       if (zswap_rb_insert(&tree->rbroot, entry, &dupentry) == -EEXIST) {
                zswap_invalidate_entry(tree, dupentry);
+               VM_WARN_ON(zswap_rb_insert(&tree->rbroot, entry, &dupentry));
Quoted 8 lines

It seems there is only one path called zswap_rb_insert() and there is no loop to repeat the insert any more. Can we have the zswap_rb_insert() install the entry and return the dupentry? We can still just call zswap_invalidate_entry() on the duplicate. The mapping of the dupentry has been removed when zswap_rb_insert() returns. That will save a repeat lookup on the duplicate case.
After this change, the zswap_rb_insert() will map to the xarray xa_store() pretty nicely.

Quoted 6 lines

I brought this up in v1 [1]. We agreed to leave it as-is for now since we expect the xarray conversion soon-ish. No need to update zswap_rb_insert() only to replace it with xa_store() later anyway.

[1] https://lore.kernel.org/lkml/[email protected]/

Ah, thanks for the pointer. I miss your earlier reply.

Chris

author reviewer
#4 lore raw
In reply to [email protected]
Quoted 79 lines · On 2024/2/7 11:38, [email protected] wrote:

From: Chengming Zhou <[email protected]>

We may encounter duplicate entry in the zswap_store():

1. swap slot that freed to per-cpu swap cache, doesn't invalidate the zswap entry, then got reused. This has been fixed.

2. !exclusive load mode, swapin folio will leave its zswap entry on the tree, then swapout again. This has been removed.

3. one folio can be dirtied again after zswap_store(), so need to zswap_store() again. This should be handled correctly.

So we must invalidate the old duplicate entry before insert the new one, which actually doesn't have to be done at the beginning of zswap_store(). And this is a normal situation, we shouldn't WARN_ON(1) in this case, so delete it. (The WARN_ON(1) seems want to detect swap entry UAF problem? But not very necessary here.)

The good point is that we don't need to lock tree twice in the store success path.

Note we still need to invalidate the old duplicate entry in the store failure path, otherwise the new data in swapfile could be overwrite by the old data in zswap pool when lru writeback.

We have to do this even when !zswap_enabled since zswap can be disabled anytime. If the folio store success before, then got dirtied again but zswap disabled, we won't invalidate the old duplicate entry in the zswap_store(). So later lru writeback may overwrite the new data in swapfile.

---
v3:
- Fix a few grammatical problems in comments, per Yosry.

v2:
- Change the duplicate entry invalidation loop to if, since we hold the lock, we won't find it once we invalidate it, per Yosry.
- Add Fixes tag.

---
 mm/zswap.c | 33 ++++++++++++++++-----------------
 1 file changed, 16 insertions(+), 17 deletions(-)
mm/zswap.c +1 -13
index cd67f7f6b302..d9d8947d6761 100644
@@ -1518,18 +1518,8 @@ bool zswap_store(struct folio *folio)
 		return false;
 
 	if (!zswap_enabled)
-		return false;
+		goto check_old;
 
-	/*
-	 * 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);
-	entry = zswap_rb_search(&tree->rbroot, offset);
-	if (entry)
-		zswap_invalidate_entry(tree, entry);
-	spin_unlock(&tree->lock);
 	objcg = get_obj_cgroup_from_folio(folio);
 	if (objcg && !obj_cgroup_may_zswap(objcg)) {
 		memcg = get_mem_cgroup_from_objcg(objcg);
@@ -1608,14 +1598,12 @@ 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

mm/zswap.c bool zswap_store(struct folio *folio) · line 1606

Quoted 10 lines
mm/zswap.c +4 -4
-	 * found again here it means that something went wrong in the swap
-	 * cache.
+	 * The folio may have been dirtied again, invalidate the
+	 * possibly stale entry before inserting the new entry.
 	 */
-	while (zswap_rb_insert(&tree->rbroot, entry, &dupentry) == -EEXIST) {
-		WARN_ON(1);
+	if (zswap_rb_insert(&tree->rbroot, entry, &dupentry) == -EEXIST) {
 		zswap_invalidate_entry(tree, dupentry);
+		VM_WARN_ON(zswap_rb_insert(&tree->rbroot, entry, &dupentry));

Oh, I just realized this is empty if !CONFIG_DEBUG_VM, will post v4.

Thanks.

Quoted 21 lines
mm/zswap.c +11 -0
 	}
 	if (entry->length) {
 		INIT_LIST_HEAD(&entry->lru);
@@ -1638,6 +1626,17 @@ bool zswap_store(struct folio *folio)
 reject:
 	if (objcg)
 		obj_cgroup_put(objcg);
+check_old:
+	/*
+	 * If the zswap store fails or zswap is disabled, we must invalidate the
+	 * possibly stale entry which was previously stored at this offset.
+	 * Otherwise, writeback could overwrite the new data in the swapfile.
+	 */
+	spin_lock(&tree->lock);
+	entry = zswap_rb_search(&tree->rbroot, offset);
+	if (entry)
+		zswap_invalidate_entry(tree, entry);
+	spin_unlock(&tree->lock);
 	return false;
 
 shrink: