All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrew Morton <akpm@linux-foundation.org>
To: mm-commits@vger.kernel.org,surenb@google.com,stable+noautosel@kernel.org,
	richardycc@google.com,minchan@kernel.org,bgeffon@google.com,
	senozhatsky@chromium.org,akpm@linux-foundation.org
Subject: + zram-publish-streams-under-lock-in-zcomp_cpu_up_prepare.patch added to mm-new branch
Date: Mon, 30 Nov 2026 17:31:36 -0800	[thread overview]
Message-ID: <20261201013137.4385B1F000C1@smtp.kernel.org> (raw)
In-Reply-To: <20261130045410.68479-3-senozhatsky@chromium.org>


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


      reply	other threads:[~2026-12-01  1:31 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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   ` Andrew Morton [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201013137.4385B1F000C1@smtp.kernel.org \
    --to=akpm@linux-foundation.org \
    --cc=bgeffon@google.com \
    --cc=minchan@kernel.org \
    --cc=mm-commits@vger.kernel.org \
    --cc=richardycc@google.com \
    --cc=senozhatsky@chromium.org \
    --cc=stable+noautosel@kernel.org \
    --cc=surenb@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.