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=1796088695; b=GDFN+rW/5fB4kb8rE0j6asWE/AbYHjOIr48++0IHuCgPrjZqaiasXys7XoKGy9MnyflG Lum5eG7zOsj9UidfKpjfCBLNTrmpddRW4tfdY9PA65nvA5afeJgny5rf0tZT6fp9D1hjI hBrlQe3ckFFHfwgOLlSMjXvqch6QvpE1QwEwLpNVqVPN+DCUoYmqz/Tn6TEVsjnC1cBPf BURYFAjsTvocUTHzN62gDGJDTXFVPs6BGj9NPrRibzI+fFOkgu+SWF228niD3+7ug7zcK /efFogVTShZ1ASuucot/nYH+nKCxodHx8bbefSW3l+zuEDwo7hUzPmWID/iA+7hyiAA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796088695; h=Received:DKIM-Signature:Date:To:From:In-Reply-To:References:Subject: Message-Id; bh=gSbe0srY1OeLqkUY+eTUKGR/fh+5DT+KSjad3BHQrio=; b=RTOtKKu5SntP+k07adXsBnqv/Raco7DMe2XBt5CyVgj4+t9Mh+cbwb5JLAgwitWRTk9f lT/vC2MdCmdzF/rCN05PbbjXMPm4I/oP7cy6rVSCLQcp36cburFYQH8xUHlrfaREOJWvl zIJMon/LUSF0nV6sDFsB8JsLRo2NxSstK4pXJHDC3eXQLwXc7PnBFuC/eQc931AtyarsK H86z3zGROUQx6JaU9c5D7VbyanK50mPYY0LK3dX/hKt7YnGDxU5kjaw3uZrqussTj741d NowB7CHf5hugC8vCZWRK4LTP2V7DJbGOrrd1VFcIO5Q3L6kZ0IfIq58HjVhmKxy33NA== 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=wThl0Hbn; 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 82014606A31A for ; Tue, 1 Dec 2026 01:31:35 +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=wThl0Hbn Received: by smtp.kernel.org (Postfix) with ESMTPSA id E4D2F1F000C1; Tue, 1 Dec 2026 01:31:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796088695; bh=gSbe0srY1OeLqkUY+eTUKGR/fh+5DT+KSjad3BHQrio=; h=Date:To:From:In-Reply-To:References:Subject; b=wThl0HbnzCDRxbln320YydFPkMXFHDMNZ55K+0tMwQnB+i3z0hp6R1zezG9O79JA5 me8t9FD8XkVqxise5PWgTBQlpFdVhCsfIGcjJ4iGeJMD5kvczClISpKvtXF9IqHCTl EgyK6rO94pA77jIf0YZA8OJ1ROrg0BsFVskPc+5E= Date: Mon, 30 Nov 2026 17:31:34 -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-2-senozhatsky@chromium.org> References: <20261130045410.68479-2-senozhatsky@chromium.org> Subject: + zram-avoid-rollback-uaf-in-zcomp_cpu_up_prepare.patch added to mm-new branch Message-Id: <20261201013134.E4D2F1F000C1@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: 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 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 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 | 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