Skip to contentSkip to conversation

mm/zsmalloc: fix NULL pointer dereference in get_next_zpdesc

PATCH In review Michael Fara 2 participants 3 messages Feb 9 – Feb 18

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/zsmalloc: fix NULL pointer dereference in get_next_zpdesc

get_next_zpdesc() calls get_zspage() which unconditionally dereferences zpdesc->zspage without a NULL check. This causes a kernel oops when zpdesc->zspage has been set to NULL by reset_zpdesc() during a race between zspage destruction and page compaction/migration.

The race window is documented in a TODO comment in zs_page_migrate():

"nothing prevents a zspage from getting destroyed while it is isolated for migration, as the page lock is temporarily dropped after zs_page_isolate() succeeded"

The sequence is:
1. Compaction calls zs_page_isolate() on a zpdesc, then drops its page lock.
2. Concurrently, async_free_zspage() or free_zspage() destroys the

     zspage, calling reset_zpdesc() which sets zpdesc->zspage = NULL.
  3. A subsequent zs_free() path calls trylock_zspage(), which iterates
     zpdescs via get_next_zpdesc(). get_zspage() dereferences the now-
     NULL backpointer, causing:

       BUG: kernel NULL pointer dereference, address: 0000000000000000
       RIP: 0010:free_zspage+0x26/0x100
       Call Trace:
        zs_free+0xf4/0x110
        zswap_entry_free+0x7e/0x160

The migration side already has a NULL guard (zs_page_migrate line 1675:
"if (!zpdesc->zspage) return 0;"), but get_next_zpdesc() lacks the same protection.

Fix this by reading zpdesc->zspage directly in get_next_zpdesc() instead of going through get_zspage(), and returning NULL when the backpointer is NULL. This stops iteration safely — the caller treats it as the end of the page chain.

---
 mm/zsmalloc.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
mm/zsmalloc.c +13 -1
@@ -735,7 +735,19 @@ static struct zspage *get_zspage(struct zpdesc *zpdesc)
 
 static struct zpdesc *get_next_zpdesc(struct zpdesc *zpdesc)
 {
-	struct zspage *zspage = get_zspage(zpdesc);
+	struct zspage *zspage = zpdesc->zspage;
+
+	/*
+	 * If the backpointer is NULL, this zpdesc was already freed via
+	 * reset_zpdesc() by a racing async_free_zspage() while isolated
+	 * for compaction. See the TODO comment in zs_page_migrate().
+	 */
+	if (unlikely(!zspage)) {
+		WARN_ON_ONCE(1);
+		return NULL;
+	}
+
+	BUG_ON(zspage->magic != ZSPAGE_MAGIC);
 
 	if (unlikely(ZsHugePage(zspage)))
 		return NULL;

--
2.39.0

#1 lore raw

Cc-ing Minchan and Brian.

Quoted 30 lines · Michael Fara

get_next_zpdesc() calls get_zspage() which unconditionally dereferences zpdesc->zspage without a NULL check. This causes a kernel oops when zpdesc->zspage has been set to NULL by reset_zpdesc() during a race between zspage destruction and page compaction/migration.

The race window is documented in a TODO comment in zs_page_migrate():

"nothing prevents a zspage from getting destroyed while it is isolated for migration, as the page lock is temporarily dropped after zs_page_isolate() succeeded"

The sequence is:
1. Compaction calls zs_page_isolate() on a zpdesc, then drops its page lock.
2. Concurrently, async_free_zspage() or free_zspage() destroys the

     zspage, calling reset_zpdesc() which sets zpdesc->zspage = NULL.
  3. A subsequent zs_free() path calls trylock_zspage(), which iterates
     zpdescs via get_next_zpdesc(). get_zspage() dereferences the now-
     NULL backpointer, causing:

       BUG: kernel NULL pointer dereference, address: 0000000000000000
       RIP: 0010:free_zspage+0x26/0x100
       Call Trace:
        zs_free+0xf4/0x110
        zswap_entry_free+0x7e/0x160

The migration side already has a NULL guard (zs_page_migrate line 1675:
"if (!zpdesc->zspage) return 0;"), but get_next_zpdesc() lacks the same protection.

Quoted 6 lines

Fix this by reading zpdesc->zspage directly in get_next_zpdesc() instead of going through get_zspage(), and returning NULL when the backpointer is NULL. This stops iteration safely — the caller treats it as the end of the page chain.

JFI: all of your emails ended up in the SPAM folder, somehow. Recovered.

[..]

Quoted 4 lines
mm/zsmalloc.c +0 -0
@@ -735,7 +735,19 @@ static struct zspage *get_zspage(struct zpdesc *zpdesc)
 
 static struct zpdesc *get_next_zpdesc(struct zpdesc *zpdesc)
 {

mm/zsmalloc.c static struct zspage *get_zspage(struct zpdesc *zpdesc) · line 746

Quoted 10 lines
mm/zsmalloc.c +9 -1
-	struct zspage *zspage = get_zspage(zpdesc);
+	struct zspage *zspage = zpdesc->zspage;
+
+	/*
+	 * If the backpointer is NULL, this zpdesc was already freed via
+	 * reset_zpdesc() by a racing async_free_zspage() while isolated
+	 * for compaction. See the TODO comment in zs_page_migrate().
+	 */
+	if (unlikely(!zspage)) {
+		WARN_ON_ONCE(1);

What is the purpose of this WARN_ON_ONCE()?

mm/zsmalloc.c static struct zspage *get_zspage(struct zpdesc *zpdesc) · line 750

Quoted 4 lines
mm/zsmalloc.c +4 -0
+		return NULL;
+	}
+
+	BUG_ON(zspage->magic != ZSPAGE_MAGIC);

We can't add new BUG_ON().

Quoted 4 lines
mm/zsmalloc.c +0 -1
 	if (unlikely(ZsHugePage(zspage)))
 		return NULL;
--

2.39.0

#2 lore raw
In reply to Michael Fara

On (26/02/09 19:37), Michael Fara wrote:
[..]

Quoted 38 lines

The sequence is:
1. Compaction calls zs_page_isolate() on a zpdesc, then drops its page lock.
2. Concurrently, async_free_zspage() or free_zspage() destroys the

     zspage, calling reset_zpdesc() which sets zpdesc->zspage = NULL.
  3. A subsequent zs_free() path calls trylock_zspage(), which iterates
     zpdescs via get_next_zpdesc(). get_zspage() dereferences the now-
     NULL backpointer, causing:

       BUG: kernel NULL pointer dereference, address: 0000000000000000
       RIP: 0010:free_zspage+0x26/0x100
       Call Trace:
        zs_free+0xf4/0x110
        zswap_entry_free+0x7e/0x160

The migration side already has a NULL guard (zs_page_migrate line 1675:
"if (!zpdesc->zspage) return 0;"), but get_next_zpdesc() lacks the same protection.

Fix this by reading zpdesc->zspage directly in get_next_zpdesc() instead of going through get_zspage(), and returning NULL when the backpointer is NULL. This stops iteration safely — the caller treats it as the end of the page chain.

---
 mm/zsmalloc.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
mm/zsmalloc.c +1 -1
@@ -735,7 +735,19 @@ static struct zspage *get_zspage(struct zpdesc *zpdesc)
 
 static struct zpdesc *get_next_zpdesc(struct zpdesc *zpdesc)
 {
-	struct zspage *zspage = get_zspage(zpdesc);
+	struct zspage *zspage = zpdesc->zspage;

mm/zsmalloc.c static struct zspage *get_zspage(struct zpdesc *zpdesc) · line 748

Quoted 10 lines
mm/zsmalloc.c +10 -0
+
+	/*
+	 * If the backpointer is NULL, this zpdesc was already freed via
+	 * reset_zpdesc() by a racing async_free_zspage() while isolated
+	 * for compaction. See the TODO comment in zs_page_migrate().
+	 */
+	if (unlikely(!zspage)) {
+		WARN_ON_ONCE(1);
+		return NULL;
+	}

I need to look closer, but the quick glance suggests that this is a problematic approach. We can't just return NULL from get_next_zpdesc() because this can potentially cause issues in the callers. E.g. trylock_zspage() will treat NULL as the end of the page chain and return success, which is clearly wrong. We also have a bunch of other callers that never expect NULL from get_next_zpdesc().