All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] zram: fix pCPU stream access vs CPU-hotplug races
@ 2026-11-30  4:43 Sergey Senozhatsky
  2026-11-30  4:43 ` [PATCH 1/2] zram: avoid rollback UAF in zcomp_cpu_up_prepare() Sergey Senozhatsky
  2026-11-30  4:43 ` [PATCH 2/2] zram: publish streams under lock in zcomp_cpu_up_prepare() Sergey Senozhatsky
  0 siblings, 2 replies; 5+ messages in thread
From: Sergey Senozhatsky @ 2026-11-30  4:43 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Suren Baghdasaryan, Brian Geffon, Minchan Kim, Richard Chang,
	linux-kernel, linux-block, linux-mm, Sergey Senozhatsky

Suren Baghdasaryan ran an internal model on zram and found a couple
of pre-existing issues with per-CPU stream access in zram.  Both
are very low risk, related to preemption and CPU-hotplug races,
and have existed since v6.15.  Because zcomp has been redesigned
since v6.15 (split into separate R/W streams), these patches do
not apply to stable trees as-is, thus stable+noautosel tag.

Sergey Senozhatsky (2):
  zram: avoid rollback UAF in zcomp_cpu_up_prepare()
  zram: publish streams under lock in zcomp_cpu_up_prepare()

 drivers/block/zram/zcomp.c | 109 ++++++++++++++++++++++++++-----------
 drivers/block/zram/zcomp.h |   4 +-
 2 files changed, 80 insertions(+), 33 deletions(-)

-- 
2.56.1.839.g22a8290db9-goog


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/2] zram: avoid rollback UAF in zcomp_cpu_up_prepare()
  2026-11-30  4:43 [PATCH 0/2] zram: fix pCPU stream access vs CPU-hotplug races Sergey Senozhatsky
@ 2026-11-30  4:43 ` Sergey Senozhatsky
  2026-12-01  1:31   ` + zram-avoid-rollback-uaf-in-zcomp_cpu_up_prepare.patch added to mm-new branch Andrew Morton
  2026-11-30  4:43 ` [PATCH 2/2] zram: publish streams under lock in zcomp_cpu_up_prepare() Sergey Senozhatsky
  1 sibling, 1 reply; 5+ messages in thread
From: Sergey Senozhatsky @ 2026-11-30  4:43 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Suren Baghdasaryan, Brian Geffon, Minchan Kim, Richard Chang,
	linux-kernel, linux-block, linux-mm, Sergey Senozhatsky,
	stable+noautosel

A task on CPU1 can grab CPU1's per-CPU compression stream pointer via
raw_cpu_ptr() and get preempted before taking cstrm->lock.  If CPU1 goes
offline, the task is migrated to another CPU while still holding its
pointer to CPU1's cstrm, and zcomp_cpu_dead() destroys that stream under
its lock.  When CPU1 starts coming back online, the task can resume on
that other CPU and acquire CPU1's cstrm->lock while
zcomp_cpu_up_prepare() is still rebuilding CPU1's streams.

Currently zcomp_cpu_up_prepare() sets cstrm->buffer before calling
zcomp_dstrm_init().  If the resumed writer acquires cstrm->lock, sees
cstrm->buffer != NULL and starts compressing, and zcomp_dstrm_init()
then fails, the rollback calls zcomp_cstrm_free() without cstrm->lock,
freeing ->ctx and ->buffer under the active writer.

Splitting zcomp into separate R/W streams widened this window: before
that, the rollback could only follow a failed ->local_copy allocation in
zcomp_strm_init().

Move the buffer vzalloc() calls out of zcomp_{c,d}strm_init() (renamed
to zcomp_{c,d}strm_ctx_init(), paired with zcomp_{c,d}strm_ctx_free())
into zcomp_cpu_up_prepare() and zcomp_init().  Because ->buffer and
->local_copy remain NULL and untouched on the error paths, nothing reads
->ctx and rollback never races with a resumed writer.

Fixes: 2efa9e9eb4db ("zram: permit preemption with active compression stream")
Cc: <stable+noautosel@kernel.org> # depends on the zram R/W stream split, not in stable trees
Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>
---
 drivers/block/zram/zcomp.c | 83 +++++++++++++++++++++++++-------------
 1 file changed, 56 insertions(+), 27 deletions(-)

diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c
index ad8c7dc73922..365845af465f 100644
--- a/drivers/block/zram/zcomp.c
+++ b/drivers/block/zram/zcomp.c
@@ -45,16 +45,21 @@ static const struct zcomp_ops *backends[] = {
 	NULL
 };
 
-static void zcomp_cstrm_free(struct zcomp *comp, struct zcomp_cstrm *zstrm)
+static void zcomp_cstrm_ctx_free(struct zcomp *comp, struct zcomp_cstrm *zstrm)
 {
 	if (comp->ops->destroy_cctx)
 		comp->ops->destroy_cctx(&zstrm->ctx);
+	zstrm->ctx.context = NULL;
+}
+
+static void zcomp_cstrm_free(struct zcomp *comp, struct zcomp_cstrm *zstrm)
+{
+	zcomp_cstrm_ctx_free(comp, zstrm);
 	vfree(zstrm->buffer);
 	zstrm->buffer = NULL;
-	zstrm->ctx.context = NULL;
 }
 
-static int zcomp_cstrm_init(struct zcomp *comp, struct zcomp_cstrm *zstrm)
+static int zcomp_cstrm_ctx_init(struct zcomp *comp, struct zcomp_cstrm *zstrm)
 {
 	int ret;
 
@@ -67,28 +72,24 @@ static int zcomp_cstrm_init(struct zcomp *comp, struct zcomp_cstrm *zstrm)
 		}
 	}
 
-	/*
-	 * allocate 2 pages. 1 for compressed data, plus 1 extra for the
-	 * case when compressed size is larger than the original one
-	 */
-	zstrm->buffer = vzalloc(2 * PAGE_SIZE);
-	if (!zstrm->buffer) {
-		zcomp_cstrm_free(comp, zstrm);
-		return -ENOMEM;
-	}
 	return 0;
 }
 
-static void zcomp_dstrm_free(struct zcomp *comp, struct zcomp_dstrm *zstrm)
+static void zcomp_dstrm_ctx_free(struct zcomp *comp, struct zcomp_dstrm *zstrm)
 {
 	if (comp->ops->destroy_dctx)
 		comp->ops->destroy_dctx(&zstrm->ctx);
+	zstrm->ctx.context = NULL;
+}
+
+static void zcomp_dstrm_free(struct zcomp *comp, struct zcomp_dstrm *zstrm)
+{
+	zcomp_dstrm_ctx_free(comp, zstrm);
 	vfree(zstrm->local_copy);
 	zstrm->local_copy = NULL;
-	zstrm->ctx.context = NULL;
 }
 
-static int zcomp_dstrm_init(struct zcomp *comp, struct zcomp_dstrm *zstrm)
+static int zcomp_dstrm_ctx_init(struct zcomp *comp, struct zcomp_dstrm *zstrm)
 {
 	int ret;
 
@@ -101,11 +102,6 @@ static int zcomp_dstrm_init(struct zcomp *comp, struct zcomp_dstrm *zstrm)
 		}
 	}
 
-	zstrm->local_copy = vzalloc(PAGE_SIZE);
-	if (!zstrm->local_copy) {
-		zcomp_dstrm_free(comp, zstrm);
-		return -ENOMEM;
-	}
 	return 0;
 }
 
@@ -333,27 +329,55 @@ int zcomp_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
 	struct zcomp *comp = hlist_entry(node, struct zcomp, node);
 	struct zcomp_cstrm *cstrm = NULL;
 	struct zcomp_dstrm *dstrm;
+	void *buffer = NULL, *local_copy;
 	int ret;
 
 	if (comp->cstream) {
 		cstrm = per_cpu_ptr(comp->cstream, cpu);
-		ret = zcomp_cstrm_init(comp, cstrm);
+		ret = zcomp_cstrm_ctx_init(comp, cstrm);
 		if (ret) {
 			pr_err("Can't allocate a compression stream\n");
 			return ret;
 		}
+
+		/*
+		 * allocate 2 pages. 1 for compressed data, plus 1 extra for
+		 * the case when compressed size is larger than the original
+		 * one
+		 */
+		buffer = vzalloc(2 * PAGE_SIZE);
+		if (!buffer) {
+			zcomp_cstrm_ctx_free(comp, cstrm);
+			return -ENOMEM;
+		}
 	}
 
 	dstrm = per_cpu_ptr(comp->dstream, cpu);
-	ret = zcomp_dstrm_init(comp, dstrm);
+	ret = zcomp_dstrm_ctx_init(comp, dstrm);
 	if (ret) {
 		pr_err("Can't allocate a decompression stream\n");
-		if (cstrm)
-			zcomp_cstrm_free(comp, cstrm);
-		return ret;
+		goto err;
+	}
+
+	local_copy = vzalloc(PAGE_SIZE);
+	if (!local_copy) {
+		zcomp_dstrm_ctx_free(comp, dstrm);
+		ret = -ENOMEM;
+		goto err;
 	}
 
+	if (cstrm)
+		cstrm->buffer = buffer;
+	dstrm->local_copy = local_copy;
+
 	return 0;
+
+err:
+	if (cstrm) {
+		vfree(buffer);
+		zcomp_cstrm_ctx_free(comp, cstrm);
+	}
+	return ret;
 }
 
 int zcomp_cpu_dead(unsigned int cpu, struct hlist_node *node)
@@ -394,13 +418,18 @@ static int zcomp_init(struct zcomp *comp, struct zcomp_params *params,
 			goto cleanup;
 		}
 		mutex_init(&comp->recomp_cstream->lock);
-		ret = zcomp_cstrm_init(comp, comp->recomp_cstream);
+		ret = zcomp_cstrm_ctx_init(comp, comp->recomp_cstream);
 		if (ret) {
-			/* zcomp_cstrm_init() cleans up after itself */
+			/* zcomp_cstrm_ctx_init() cleans up after itself */
 			kfree(comp->recomp_cstream);
 			comp->recomp_cstream = NULL;
 			goto cleanup;
 		}
+		comp->recomp_cstream->buffer = vzalloc(2 * PAGE_SIZE);
+		if (!comp->recomp_cstream->buffer) {
+			ret = -ENOMEM;
+			goto cleanup;
+		}
 	} else {
 		comp->cstream = alloc_percpu(struct zcomp_cstrm);
 		if (!comp->cstream) {
-- 
2.56.1.839.g22a8290db9-goog


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/2] zram: publish streams under lock in zcomp_cpu_up_prepare()
  2026-11-30  4:43 [PATCH 0/2] zram: fix pCPU stream access vs CPU-hotplug races Sergey Senozhatsky
  2026-11-30  4:43 ` [PATCH 1/2] zram: avoid rollback UAF in zcomp_cpu_up_prepare() Sergey Senozhatsky
@ 2026-11-30  4:43 ` Sergey Senozhatsky
  2026-12-01  1:31   ` + zram-publish-streams-under-lock-in-zcomp_cpu_up_prepare.patch added to mm-new branch Andrew Morton
  1 sibling, 1 reply; 5+ messages in thread
From: Sergey Senozhatsky @ 2026-11-30  4:43 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Suren Baghdasaryan, Brian Geffon, Minchan Kim, Richard Chang,
	linux-kernel, linux-block, linux-mm, Sergey Senozhatsky,
	stable+noautosel

zcomp_cpu_up_prepare() writes ->ctx and assigns cstrm->buffer and
dstrm->local_copy without taking the stream locks.  Because those
unlocked stores do not pair with cstrm->lock or down_read(&dstrm->lock),
a task resuming on another CPU with a stale stream pointer across an
offline/online cycle (via raw_cpu_ptr() or for_each_online_cpu_wrap())
races with zcomp_cpu_up_prepare(): on weakly ordered architectures such
as arm64, it can observe a non-NULL buffer before ->ctx initialization
is visible (missing release-acquire ordering, and a data race).

Publish ->buffer under cstrm->lock and ->local_copy under
down_write(&dstrm->lock).  The GFP_KERNEL allocations must stay outside
the locks: they can enter reclaim, and swap-out to zram takes
cstrm->lock under fs_reclaim, so holding cstrm->lock across them would
create a cstrm->lock -> fs_reclaim -> cstrm->lock dependency (the same
reason zswap moved its prepare-time allocations outside the mutex in
commit 779b9955f643 ("mm: zswap: move allocations during CPU init
outside the lock")).

Fixes: 2efa9e9eb4db ("zram: permit preemption with active compression stream")
Cc: <stable+noautosel@kernel.org> # depends on the zram R/W stream split, not in stable trees
Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>
---
 drivers/block/zram/zcomp.c | 28 +++++++++++++++++++++++-----
 drivers/block/zram/zcomp.h |  4 ++--
 2 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c
index 365845af465f..d05f3cbe6d9d 100644
--- a/drivers/block/zram/zcomp.c
+++ b/drivers/block/zram/zcomp.c
@@ -162,8 +162,9 @@ struct zcomp_cstrm *zcomp_cstrm_get(struct zcomp *comp)
 		 * cpu_dead() from releasing this stream under us, however
 		 * there is still a race window between raw_cpu_ptr() and
 		 * mutex_lock(), during which we could have been migrated
-		 * from a CPU that has already destroyed its stream.  If
-		 * so then unlock and re-try on the current CPU.
+		 * from a CPU that has already destroyed its stream (or is
+		 * still rebuilding it).  If so then unlock and re-try on
+		 * the current CPU.
 		 */
 		mutex_lock(&zstrm->lock);
 		if (zstrm->buffer)
@@ -192,8 +193,8 @@ static bool zcomp_dstrm_excl_trylock(struct zcomp_dstrm *zstrm)
 	if (mutex_is_locked(&zstrm->excl_lock))
 		return false;
 	/*
-	 * Pin the stream.  The write side of ->lock is only taken by
-	 * zcomp_cpu_dead(), which also sets ->local_copy to NULL.
+	 * Pin the stream.  The write side of ->lock is only taken by CPU
+	 * hotplug callbacks, which publish and clear ->local_copy.
 	 */
 	if (!down_read_trylock(&zstrm->lock))
 		return false;
@@ -332,6 +333,12 @@ int zcomp_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
 	void *buffer = NULL, *local_copy;
 	int ret;
 
+	/*
+	 * Allocate outside stream locks: allocations use GFP_KERNEL and
+	 * may recurse into direct reclaim (zram swap), which acquires
+	 * cstrm->lock.  Initializing ->ctx in place is safe while
+	 * ->buffer / ->local_copy remain NULL.
+	 */
 	if (comp->cstream) {
 		cstrm = per_cpu_ptr(comp->cstream, cpu);
 		ret = zcomp_cstrm_ctx_init(comp, cstrm);
@@ -366,9 +373,20 @@ int zcomp_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
 		goto err;
 	}
 
-	if (cstrm)
+	/*
+	 * Publish under the stream locks to serialize with tasks that may
+	 * hold a stale pointer to this CPU's stream across an offline/online
+	 * cycle and to order ->ctx initialization before buffer visibility.
+	 */
+	if (cstrm) {
+		mutex_lock(&cstrm->lock);
 		cstrm->buffer = buffer;
+		mutex_unlock(&cstrm->lock);
+	}
+
+	down_write(&dstrm->lock);
 	dstrm->local_copy = local_copy;
+	up_write(&dstrm->lock);
 
 	return 0;
 
diff --git a/drivers/block/zram/zcomp.h b/drivers/block/zram/zcomp.h
index 39efefc43a13..b9a2b57a649b 100644
--- a/drivers/block/zram/zcomp.h
+++ b/drivers/block/zram/zcomp.h
@@ -57,8 +57,8 @@ struct zcomp_cstrm {
 struct zcomp_dstrm {
 	/*
 	 * Pins the stream: held in read mode by every user, regardless
-	 * of the decompression mode, and in write mode only by
-	 * zcomp_cpu_dead().
+	 * of the decompression mode, and in write mode only by CPU
+	 * hotplug callbacks (zcomp_cpu_up_prepare() and zcomp_cpu_dead()).
 	 */
 	struct rw_semaphore lock;
 	/* Serializes users of the mutable stream state: ->local_copy, ->ctx */
-- 
2.56.1.839.g22a8290db9-goog


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* + zram-avoid-rollback-uaf-in-zcomp_cpu_up_prepare.patch added to mm-new branch
  2026-11-30  4:43 ` [PATCH 1/2] zram: avoid rollback UAF in zcomp_cpu_up_prepare() Sergey Senozhatsky
@ 2026-12-01  1:31   ` Andrew Morton
  0 siblings, 0 replies; 5+ messages in thread
From: Andrew Morton @ 2026-12-01  1:31 UTC (permalink / raw)
  To: mm-commits, surenb, stable+noautosel, richardycc, minchan,
	bgeffon, senozhatsky, akpm


The patch titled
     Subject: zram: avoid rollback UAF in zcomp_cpu_up_prepare()
has been added to the -mm mm-new branch.  Its filename is
     zram-avoid-rollback-uaf-in-zcomp_cpu_up_prepare.patch

This patch will shortly appear at
     https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/zram-avoid-rollback-uaf-in-zcomp_cpu_up_prepare.patch

This patch will later appear in the mm-new branch at
    git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

Note, mm-new is a provisional staging ground for work-in-progress
patches, and acceptance into mm-new is a notification for others take
notice and to finish up reviews.  Please do not hesitate to respond to
review feedback and post updated versions to replace or incrementally
fixup patches in mm-new.

The mm-new branch of mm.git is not included in linux-next

If a few days of testing in mm-new is successful, the patch will me moved
into mm.git's mm-unstable branch, which is included in linux-next

Before you just go and hit "reply", please:
   a) Consider who else should be cc'ed
   b) Prefer to cc a suitable mailing list as well
   c) Ideally: find the original patch on the mailing list and do a
      reply-to-all to that, adding suitable additional cc's

*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***

The -mm tree is included into linux-next via various
branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days

------------------------------------------------------
From: Sergey Senozhatsky <senozhatsky@chromium.org>
Subject: zram: avoid rollback UAF in zcomp_cpu_up_prepare()
Date: Mon, 30 Nov 2026 13:43:38 +0900

Patch series "zram: fix pCPU stream access vs CPU-hotplug races".

Suren Baghdasaryan ran an internal model on zram and found a couple of
pre-existing issues with per-CPU stream access in zram.  Both are very low
risk, related to preemption and CPU-hotplug races, and have existed since
v6.15.  Because zcomp has been redesigned since v6.15 (split into separate
R/W streams), these patches do not apply to stable trees as-is, thus
stable+noautosel tag.


This patch (of 2):

A task on CPU1 can grab CPU1's per-CPU compression stream pointer via
raw_cpu_ptr() and get preempted before taking cstrm->lock.  If CPU1 goes
offline, the task is migrated to another CPU while still holding its
pointer to CPU1's cstrm, and zcomp_cpu_dead() destroys that stream under
its lock.  When CPU1 starts coming back online, the task can resume on
that other CPU and acquire CPU1's cstrm->lock while zcomp_cpu_up_prepare()
is still rebuilding CPU1's streams.

Currently zcomp_cpu_up_prepare() sets cstrm->buffer before calling
zcomp_dstrm_init().  If the resumed writer acquires cstrm->lock, sees
cstrm->buffer != NULL and starts compressing, and zcomp_dstrm_init() then
fails, the rollback calls zcomp_cstrm_free() without cstrm->lock, freeing
->ctx and ->buffer under the active writer.

Splitting zcomp into separate R/W streams widened this window: before
that, the rollback could only follow a failed ->local_copy allocation in
zcomp_strm_init().

Move the buffer vzalloc() calls out of zcomp_{c,d}strm_init() (renamed to
zcomp_{c,d}strm_ctx_init(), paired with zcomp_{c,d}strm_ctx_free()) into
zcomp_cpu_up_prepare() and zcomp_init().  Because ->buffer and
->local_copy remain NULL and untouched on the error paths, nothing reads
->ctx and rollback never races with a resumed writer.

Link: https://lore.kernel.org/20261130045410.68479-1-senozhatsky@chromium.org
Link: https://patch.msgid.link/20261130045410.68479-2-senozhatsky@chromium.org
Fixes: 2efa9e9eb4db ("zram: permit preemption with active compression stream")
Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Cc: Suren Baghdasaryan <surenb@google.com>
Cc: Brian Geffon <bgeffon@google.com>
Cc: Minchan Kim <minchan@kernel.org>
Cc: Richard Chang <richardycc@google.com>
Cc: <stable+noautosel@kernel.org> # depends on the zram R/W stream split, not in stable trees

 drivers/block/zram/zcomp.c |   83 +++++++++++++++++++++++------------
 1 file changed, 56 insertions(+), 27 deletions(-)

--- a/drivers/block/zram/zcomp.c~zram-avoid-rollback-uaf-in-zcomp_cpu_up_prepare
+++ a/drivers/block/zram/zcomp.c
@@ -45,16 +45,21 @@ static const struct zcomp_ops *backends[
 	NULL
 };
 
-static void zcomp_cstrm_free(struct zcomp *comp, struct zcomp_cstrm *zstrm)
+static void zcomp_cstrm_ctx_free(struct zcomp *comp, struct zcomp_cstrm *zstrm)
 {
 	if (comp->ops->destroy_cctx)
 		comp->ops->destroy_cctx(&zstrm->ctx);
+	zstrm->ctx.context = NULL;
+}
+
+static void zcomp_cstrm_free(struct zcomp *comp, struct zcomp_cstrm *zstrm)
+{
+	zcomp_cstrm_ctx_free(comp, zstrm);
 	vfree(zstrm->buffer);
 	zstrm->buffer = NULL;
-	zstrm->ctx.context = NULL;
 }
 
-static int zcomp_cstrm_init(struct zcomp *comp, struct zcomp_cstrm *zstrm)
+static int zcomp_cstrm_ctx_init(struct zcomp *comp, struct zcomp_cstrm *zstrm)
 {
 	int ret;
 
@@ -67,28 +72,24 @@ static int zcomp_cstrm_init(struct zcomp
 		}
 	}
 
-	/*
-	 * allocate 2 pages. 1 for compressed data, plus 1 extra for the
-	 * case when compressed size is larger than the original one
-	 */
-	zstrm->buffer = vzalloc(2 * PAGE_SIZE);
-	if (!zstrm->buffer) {
-		zcomp_cstrm_free(comp, zstrm);
-		return -ENOMEM;
-	}
 	return 0;
 }
 
-static void zcomp_dstrm_free(struct zcomp *comp, struct zcomp_dstrm *zstrm)
+static void zcomp_dstrm_ctx_free(struct zcomp *comp, struct zcomp_dstrm *zstrm)
 {
 	if (comp->ops->destroy_dctx)
 		comp->ops->destroy_dctx(&zstrm->ctx);
+	zstrm->ctx.context = NULL;
+}
+
+static void zcomp_dstrm_free(struct zcomp *comp, struct zcomp_dstrm *zstrm)
+{
+	zcomp_dstrm_ctx_free(comp, zstrm);
 	vfree(zstrm->local_copy);
 	zstrm->local_copy = NULL;
-	zstrm->ctx.context = NULL;
 }
 
-static int zcomp_dstrm_init(struct zcomp *comp, struct zcomp_dstrm *zstrm)
+static int zcomp_dstrm_ctx_init(struct zcomp *comp, struct zcomp_dstrm *zstrm)
 {
 	int ret;
 
@@ -101,11 +102,6 @@ static int zcomp_dstrm_init(struct zcomp
 		}
 	}
 
-	zstrm->local_copy = vzalloc(PAGE_SIZE);
-	if (!zstrm->local_copy) {
-		zcomp_dstrm_free(comp, zstrm);
-		return -ENOMEM;
-	}
 	return 0;
 }
 
@@ -333,27 +329,55 @@ int zcomp_cpu_up_prepare(unsigned int cp
 	struct zcomp *comp = hlist_entry(node, struct zcomp, node);
 	struct zcomp_cstrm *cstrm = NULL;
 	struct zcomp_dstrm *dstrm;
+	void *buffer = NULL, *local_copy;
 	int ret;
 
 	if (comp->cstream) {
 		cstrm = per_cpu_ptr(comp->cstream, cpu);
-		ret = zcomp_cstrm_init(comp, cstrm);
+		ret = zcomp_cstrm_ctx_init(comp, cstrm);
 		if (ret) {
 			pr_err("Can't allocate a compression stream\n");
 			return ret;
 		}
+
+		/*
+		 * allocate 2 pages. 1 for compressed data, plus 1 extra for
+		 * the case when compressed size is larger than the original
+		 * one
+		 */
+		buffer = vzalloc(2 * PAGE_SIZE);
+		if (!buffer) {
+			zcomp_cstrm_ctx_free(comp, cstrm);
+			return -ENOMEM;
+		}
 	}
 
 	dstrm = per_cpu_ptr(comp->dstream, cpu);
-	ret = zcomp_dstrm_init(comp, dstrm);
+	ret = zcomp_dstrm_ctx_init(comp, dstrm);
 	if (ret) {
 		pr_err("Can't allocate a decompression stream\n");
-		if (cstrm)
-			zcomp_cstrm_free(comp, cstrm);
-		return ret;
+		goto err;
+	}
+
+	local_copy = vzalloc(PAGE_SIZE);
+	if (!local_copy) {
+		zcomp_dstrm_ctx_free(comp, dstrm);
+		ret = -ENOMEM;
+		goto err;
 	}
 
+	if (cstrm)
+		cstrm->buffer = buffer;
+	dstrm->local_copy = local_copy;
+
 	return 0;
+
+err:
+	if (cstrm) {
+		vfree(buffer);
+		zcomp_cstrm_ctx_free(comp, cstrm);
+	}
+	return ret;
 }
 
 int zcomp_cpu_dead(unsigned int cpu, struct hlist_node *node)
@@ -394,13 +418,18 @@ static int zcomp_init(struct zcomp *comp
 			goto cleanup;
 		}
 		mutex_init(&comp->recomp_cstream->lock);
-		ret = zcomp_cstrm_init(comp, comp->recomp_cstream);
+		ret = zcomp_cstrm_ctx_init(comp, comp->recomp_cstream);
 		if (ret) {
-			/* zcomp_cstrm_init() cleans up after itself */
+			/* zcomp_cstrm_ctx_init() cleans up after itself */
 			kfree(comp->recomp_cstream);
 			comp->recomp_cstream = NULL;
 			goto cleanup;
 		}
+		comp->recomp_cstream->buffer = vzalloc(2 * PAGE_SIZE);
+		if (!comp->recomp_cstream->buffer) {
+			ret = -ENOMEM;
+			goto cleanup;
+		}
 	} else {
 		comp->cstream = alloc_percpu(struct zcomp_cstrm);
 		if (!comp->cstream) {
_

Patches currently in -mm which might be from senozhatsky@chromium.org are

zram-prefix-backends-printk-s.patch
zram-remove-debugfs-entry-on-init-failure.patch
zram-reset-writeback-state-in-zram_reset_device.patch
zram-move-mark_slot_accessed-under-existing-slot-lock-sections.patch
zram-remove-stale-warn_on-from-zram_read_page.patch
zram-zstd-do-not-allocate-empty-c-d-dictionaries.patch
zram-lz4hc-pre-initialize-dictionary-stream-in-setup_params.patch
zram-lz4-use-lz4_decompress_safe_usingdict.patch
zram-lz4hc-use-lz4_decompress_safe_usingdict.patch
zram-zstd-do-not-use-kvzalloc-for-zstd-allocations.patch
zram-split-zcomp-into-separate-r-w-streams.patch
zram-introduce-shared-decompression-streams.patch
zram-allocate-bounce-buffer-for-spanning-objects.patch
zram-borrow-idle-decompression-streams-in-exclusive-mode.patch
zram-use-a-singleton-compression-context-for-recompression.patch
zram-drop-stale-comment-in-write_incompressible_page.patch
zram-drop-get_slot_handle-and-set_slot_handle.patch
zsmalloc-return-handle-from-obj_allocated.patch
zsmalloc-switch-handle-type-to-struct-zs_handle.patch
zram-check-mem_limit-before-allocating-zsmalloc-object.patch
zram-allocate-zsmalloc-object-under-slot-lock.patch
zswap-do-not-free-the-entry-if-its-pool-is-null.patch
zsmalloc-encode-1-based-obj_idx-so-valid-obj-is-never-0.patch
zsmalloc-embed-struct-zs_handle-into-zram-and-zswap-entries.patch
zsmalloc-fold-zs_obj_write-into-zs_store.patch
zsmalloc-populate-new-zspage-outside-class-lock-in-zs_store.patch
zram-fix-quadratic-writeback-bitmap-search.patch
zsmalloc-free-a-zspages-pages-in-one-batch.patch
zram-keep-the-hot-statistics-per-cpu.patch
zram-do-not-modify-cont-sysfs-store-buffers.patch
zram-avoid-rollback-uaf-in-zcomp_cpu_up_prepare.patch
zram-publish-streams-under-lock-in-zcomp_cpu_up_prepare.patch
zsmalloc-fix-spdx-license-identifier.patch
zram-use-spdx-license-identifier.patch
maintainers-add-mm-and-block-lists-to-zram-entry.patch


^ permalink raw reply	[flat|nested] 5+ messages in thread

* + zram-publish-streams-under-lock-in-zcomp_cpu_up_prepare.patch added to mm-new branch
  2026-11-30  4:43 ` [PATCH 2/2] zram: publish streams under lock in zcomp_cpu_up_prepare() Sergey Senozhatsky
@ 2026-12-01  1:31   ` Andrew Morton
  0 siblings, 0 replies; 5+ messages in thread
From: Andrew Morton @ 2026-12-01  1:31 UTC (permalink / raw)
  To: mm-commits, surenb, stable+noautosel, richardycc, minchan,
	bgeffon, senozhatsky, akpm


The patch titled
     Subject: zram: publish streams under lock in zcomp_cpu_up_prepare()
has been added to the -mm mm-new branch.  Its filename is
     zram-publish-streams-under-lock-in-zcomp_cpu_up_prepare.patch

This patch will shortly appear at
     https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/zram-publish-streams-under-lock-in-zcomp_cpu_up_prepare.patch

This patch will later appear in the mm-new branch at
    git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

Note, mm-new is a provisional staging ground for work-in-progress
patches, and acceptance into mm-new is a notification for others take
notice and to finish up reviews.  Please do not hesitate to respond to
review feedback and post updated versions to replace or incrementally
fixup patches in mm-new.

The mm-new branch of mm.git is not included in linux-next

If a few days of testing in mm-new is successful, the patch will me moved
into mm.git's mm-unstable branch, which is included in linux-next

Before you just go and hit "reply", please:
   a) Consider who else should be cc'ed
   b) Prefer to cc a suitable mailing list as well
   c) Ideally: find the original patch on the mailing list and do a
      reply-to-all to that, adding suitable additional cc's

*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***

The -mm tree is included into linux-next via various
branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days

------------------------------------------------------
From: Sergey Senozhatsky <senozhatsky@chromium.org>
Subject: zram: publish streams under lock in zcomp_cpu_up_prepare()
Date: Mon, 30 Nov 2026 13:43:39 +0900

zcomp_cpu_up_prepare() writes ->ctx and assigns cstrm->buffer and
dstrm->local_copy without taking the stream locks.  Because those unlocked
stores do not pair with cstrm->lock or down_read(&dstrm->lock), a task
resuming on another CPU with a stale stream pointer across an
offline/online cycle (via raw_cpu_ptr() or for_each_online_cpu_wrap())
races with zcomp_cpu_up_prepare(): on weakly ordered architectures such as
arm64, it can observe a non-NULL buffer before ->ctx initialization is
visible (missing release-acquire ordering, and a data race).

Publish ->buffer under cstrm->lock and ->local_copy under
down_write(&dstrm->lock).  The GFP_KERNEL allocations must stay outside
the locks: they can enter reclaim, and swap-out to zram takes cstrm->lock
under fs_reclaim, so holding cstrm->lock across them would create a
cstrm->lock -> fs_reclaim -> cstrm->lock dependency (the same reason zswap
moved its prepare-time allocations outside the mutex in commit
779b9955f643 ("mm: zswap: move allocations during CPU init outside the
lock")).

Link: https://patch.msgid.link/20261130045410.68479-3-senozhatsky@chromium.org
Fixes: 2efa9e9eb4db ("zram: permit preemption with active compression stream")
Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Cc: Suren Baghdasaryan <surenb@google.com>
Cc: Brian Geffon <bgeffon@google.com>
Cc: Minchan Kim <minchan@kernel.org>
Cc: Richard Chang <richardycc@google.com>
Cc: <stable+noautosel@kernel.org> # depends on the zram R/W stream split, not in stable trees

 drivers/block/zram/zcomp.c |   28 +++++++++++++++++++++++-----
 drivers/block/zram/zcomp.h |    4 ++--
 2 files changed, 25 insertions(+), 7 deletions(-)

--- a/drivers/block/zram/zcomp.c~zram-publish-streams-under-lock-in-zcomp_cpu_up_prepare
+++ a/drivers/block/zram/zcomp.c
@@ -162,8 +162,9 @@ struct zcomp_cstrm *zcomp_cstrm_get(stru
 		 * cpu_dead() from releasing this stream under us, however
 		 * there is still a race window between raw_cpu_ptr() and
 		 * mutex_lock(), during which we could have been migrated
-		 * from a CPU that has already destroyed its stream.  If
-		 * so then unlock and re-try on the current CPU.
+		 * from a CPU that has already destroyed its stream (or is
+		 * still rebuilding it).  If so then unlock and re-try on
+		 * the current CPU.
 		 */
 		mutex_lock(&zstrm->lock);
 		if (zstrm->buffer)
@@ -192,8 +193,8 @@ static bool zcomp_dstrm_excl_trylock(str
 	if (mutex_is_locked(&zstrm->excl_lock))
 		return false;
 	/*
-	 * Pin the stream.  The write side of ->lock is only taken by
-	 * zcomp_cpu_dead(), which also sets ->local_copy to NULL.
+	 * Pin the stream.  The write side of ->lock is only taken by CPU
+	 * hotplug callbacks, which publish and clear ->local_copy.
 	 */
 	if (!down_read_trylock(&zstrm->lock))
 		return false;
@@ -332,6 +333,12 @@ int zcomp_cpu_up_prepare(unsigned int cp
 	void *buffer = NULL, *local_copy;
 	int ret;
 
+	/*
+	 * Allocate outside stream locks: allocations use GFP_KERNEL and
+	 * may recurse into direct reclaim (zram swap), which acquires
+	 * cstrm->lock.  Initializing ->ctx in place is safe while
+	 * ->buffer / ->local_copy remain NULL.
+	 */
 	if (comp->cstream) {
 		cstrm = per_cpu_ptr(comp->cstream, cpu);
 		ret = zcomp_cstrm_ctx_init(comp, cstrm);
@@ -366,9 +373,20 @@ int zcomp_cpu_up_prepare(unsigned int cp
 		goto err;
 	}
 
-	if (cstrm)
+	/*
+	 * Publish under the stream locks to serialize with tasks that may
+	 * hold a stale pointer to this CPU's stream across an offline/online
+	 * cycle and to order ->ctx initialization before buffer visibility.
+	 */
+	if (cstrm) {
+		mutex_lock(&cstrm->lock);
 		cstrm->buffer = buffer;
+		mutex_unlock(&cstrm->lock);
+	}
+
+	down_write(&dstrm->lock);
 	dstrm->local_copy = local_copy;
+	up_write(&dstrm->lock);
 
 	return 0;
 
--- a/drivers/block/zram/zcomp.h~zram-publish-streams-under-lock-in-zcomp_cpu_up_prepare
+++ a/drivers/block/zram/zcomp.h
@@ -57,8 +57,8 @@ struct zcomp_cstrm {
 struct zcomp_dstrm {
 	/*
 	 * Pins the stream: held in read mode by every user, regardless
-	 * of the decompression mode, and in write mode only by
-	 * zcomp_cpu_dead().
+	 * of the decompression mode, and in write mode only by CPU
+	 * hotplug callbacks (zcomp_cpu_up_prepare() and zcomp_cpu_dead()).
 	 */
 	struct rw_semaphore lock;
 	/* Serializes users of the mutable stream state: ->local_copy, ->ctx */
_

Patches currently in -mm which might be from senozhatsky@chromium.org are

zram-prefix-backends-printk-s.patch
zram-remove-debugfs-entry-on-init-failure.patch
zram-reset-writeback-state-in-zram_reset_device.patch
zram-move-mark_slot_accessed-under-existing-slot-lock-sections.patch
zram-remove-stale-warn_on-from-zram_read_page.patch
zram-zstd-do-not-allocate-empty-c-d-dictionaries.patch
zram-lz4hc-pre-initialize-dictionary-stream-in-setup_params.patch
zram-lz4-use-lz4_decompress_safe_usingdict.patch
zram-lz4hc-use-lz4_decompress_safe_usingdict.patch
zram-zstd-do-not-use-kvzalloc-for-zstd-allocations.patch
zram-split-zcomp-into-separate-r-w-streams.patch
zram-introduce-shared-decompression-streams.patch
zram-allocate-bounce-buffer-for-spanning-objects.patch
zram-borrow-idle-decompression-streams-in-exclusive-mode.patch
zram-use-a-singleton-compression-context-for-recompression.patch
zram-drop-stale-comment-in-write_incompressible_page.patch
zram-drop-get_slot_handle-and-set_slot_handle.patch
zsmalloc-return-handle-from-obj_allocated.patch
zsmalloc-switch-handle-type-to-struct-zs_handle.patch
zram-check-mem_limit-before-allocating-zsmalloc-object.patch
zram-allocate-zsmalloc-object-under-slot-lock.patch
zswap-do-not-free-the-entry-if-its-pool-is-null.patch
zsmalloc-encode-1-based-obj_idx-so-valid-obj-is-never-0.patch
zsmalloc-embed-struct-zs_handle-into-zram-and-zswap-entries.patch
zsmalloc-fold-zs_obj_write-into-zs_store.patch
zsmalloc-populate-new-zspage-outside-class-lock-in-zs_store.patch
zram-fix-quadratic-writeback-bitmap-search.patch
zsmalloc-free-a-zspages-pages-in-one-batch.patch
zram-keep-the-hot-statistics-per-cpu.patch
zram-do-not-modify-cont-sysfs-store-buffers.patch
zram-avoid-rollback-uaf-in-zcomp_cpu_up_prepare.patch
zram-publish-streams-under-lock-in-zcomp_cpu_up_prepare.patch
zsmalloc-fix-spdx-license-identifier.patch
zram-use-spdx-license-identifier.patch
maintainers-add-mm-and-block-lists-to-zram-entry.patch


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-12-01  1:31 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30  4:43 [PATCH 0/2] zram: fix pCPU stream access vs CPU-hotplug races Sergey Senozhatsky
2026-11-30  4:43 ` [PATCH 1/2] zram: avoid rollback UAF in zcomp_cpu_up_prepare() Sergey Senozhatsky
2026-12-01  1:31   ` + zram-avoid-rollback-uaf-in-zcomp_cpu_up_prepare.patch added to mm-new branch Andrew Morton
2026-11-30  4:43 ` [PATCH 2/2] zram: publish streams under lock in zcomp_cpu_up_prepare() Sergey Senozhatsky
2026-12-01  1:31   ` + zram-publish-streams-under-lock-in-zcomp_cpu_up_prepare.patch added to mm-new branch Andrew Morton

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.