[PATCH 2/2] zram: publish streams under lock in zcomp_cpu_up_prepare()
From: Sergey Senozhatsky
Date: Sun Nov 29 2026 - 23:54:37 EST
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@xxxxxxxxxx> # depends on the zram R/W stream split, not in stable trees
Signed-off-by: Sergey Senozhatsky <senozhatsky@xxxxxxxxxxxx>
---
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