From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796088697; b=zcHvYRHw4ZsKKSaSvVIqo8vN9jqB5P6iq9BQiA5TG98dFEM3wM3qIlEoG0bfdLaZC5MI tpelDOjlNvk51FAsG0CaeXTI8es/Y7tM4RP9Nwi96m8VNROkfigZiNb5Q2JllOGMJt1rD Nsa3CxKp0kE3C1U/SdTUTVp02orroX/tZsXGgT456096bMCPw32B0IKEI++OYWehSR4or NNnBagA1FDrZqkAb6pUHAAE0hAFd/V+O/J4pmg0DZAVU+uXHYMdFsn+cSdvh+WAY0oVtI 2jey7tODCgduEQYxGxPJ+DHx3PVxsrBpXt3eiwuBa5ZMlJSryVX9iy5ALEqYFA77A7Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796088697; h=Received:DKIM-Signature:Date:To:From:In-Reply-To:References:Subject: Message-Id; bh=NeElQ9WoUvXA5QB2j5lgZQG3ygb95Khelx57y8bfGNE=; b=IyNJ/r3HhwNs2E9t9IUeiKB1VbOyG0/sJChOMBesgkby+MnUFnbtUytD0vzVR/1qPhWg ZL5sJ0pP85ajzPWjcZpzUZTF3SpTSZF6o3aRHqQ2hqAYBxNE70sh8sjqvs35d4FbWmvOr lPk6F03IXH+rRCbsvyCPrixp8KMDI2EQ+Y19/3WSfKFjCMZDyKLaG6CqeEaCkS9bVmxBe K9knV8Xb5QmRDur/GEh0m8psiVrO8maRRb5gRzbiCDp5ZB/j0PHDXCTIno9dgZKMxW6pS knEH9moUJK1rbrFBnSZkC4k9QKx2q9lw8I5NagOBzDiN+FMAu577d5ZXLTpNlwegZbg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=x2967nPH; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 893AD606A320 for ; Tue, 1 Dec 2026 01:31:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=x2967nPH Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4385B1F000C1; Tue, 1 Dec 2026 01:31:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796088697; bh=NeElQ9WoUvXA5QB2j5lgZQG3ygb95Khelx57y8bfGNE=; h=Date:To:From:In-Reply-To:References:Subject; b=x2967nPHRyUM745ZBcLG5AxJhitDLDeitCXtUjui4lysGKzsblSsN/m5yHOHXQeUq kuf6gI99Ha79BQY8uI508b1Ewo39wPPPyogPt3YquzyEzDzFmpyD27YuBtUOEy9VxB xPna0VjCsLYawNceggu546PaVWY1KrpFUsOoB6QY= Date: Mon, 30 Nov 2026 17:31:36 -0800 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 From: Andrew Morton In-Reply-To: <20261130045410.68479-3-senozhatsky@chromium.org> References: <20261130045410.68479-3-senozhatsky@chromium.org> Subject: + zram-publish-streams-under-lock-in-zcomp_cpu_up_prepare.patch added to mm-new branch Message-Id: <20261201013137.4385B1F000C1@smtp.kernel.org> X-Mailing-List: mm-commits@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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 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 Signed-off-by: Andrew Morton Cc: Suren Baghdasaryan Cc: Brian Geffon Cc: Minchan Kim Cc: Richard Chang Cc: # 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