From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.127.231.50 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796394920; b=tQtrUPzEyDb8hBksF8c/Sv3qtEi1vuY4Thsp32WTEbGAmTarCqaTTq+T+bwNIZmp2UDO ReC6aEeCQhxYdJ5uBI6NrMRhTXQzeFsgZ8DCeLw5O//IadZqq06sHYekMpKCJaXjtmVeD t39tzTBv/hiaWOHBJP3VBmOIa4O/nB3igAznED7Hk5NtTZWz9naQPlpLrGVeYyYLGtKmc 8quEDonXq+NEhJoTm4KHjX0WKEj+u6KplvIHW7Gg1bttF6h50x+pWfnl/IA+NLTyZShd4 ruXXgRnssW6Oji16yHoezAg8fQTHZkNVGRVsXWCL1vBncJxZpzfGBz9lHenHBDApXVA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796394920; h=DMARC-Filter:DKIM-Signature:References:Subject:Date:Mime-Version: X-Original-From:Cc:From:Content-Disposition:In-Reply-To: X-Lms-Return-Path:To:Message-Id:Content-Transfer-Encoding:Received: Content-Type; bh=fnYeBNRD8mB7Kzd24HPNNJudjrB6205MOMYeyiUqzbA=; b=xOl+tkBCCbZRbh5Z3JzGvsiX/VRy4610sHz4rJb9lRWBq1/XPbdArGwvc6e+ETOpiiQR 2LTvXkVO6f6HZi8dZe7dbzTiUmwCjwJy7OAwcCAG1beijbmWL+DU4rR62JojOtZhNgjr5 wDrP9d30ltWN7P77lGaPH0jqp4dyv6j9S7Mz8LRzDuqf6Qk0pSc+ERuaY6bLeJMjnAf/p jJt6NUVnD5Vvn3VTYQpmjZPVnC0VjMtHY39gb7ajYYQgJ8eUZ5hA4l58uA86BQh44RRPD JRFRFPK4gyBV8gWnzdmYq4B06gwLSdnE5SnZAb5qLtq5bkmnGRDShaR8+/yPsy26FkA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=fygo.io; spf=pass smtp.mailfrom=fygo.io; dkim=pass header.d=fygo-io.20200929.dkim.larksuite.com header.i=@fygo-io.20200929.dkim.larksuite.com header.a=rsa-sha256 header.s=s1 header.b=QJkqLQou; arc=none smtp.remote-ip=209.127.231.50 Received: from va-2-50.ptr.blmpb.com (va-2-50.ptr.blmpb.com [209.127.231.50]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E0A94606A322 for ; Fri, 4 Dec 2026 14:35:18 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E0A94606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=fygo.io Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=fygo.io Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=fygo-io.20200929.dkim.larksuite.com header.i=@fygo-io.20200929.dkim.larksuite.com header.a=rsa-sha256 header.s=s1 header.b=QJkqLQou DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=s1; d=fygo-io.20200929.dkim.larksuite.com; t=1796394914; h=from:subject:mime-version:from:date:message-id:subject:to:cc: reply-to:content-type:mime-version:in-reply-to:message-id; bh=fnYeBNRD8mB7Kzd24HPNNJudjrB6205MOMYeyiUqzbA=; b=QJkqLQouBthM0vx8yawA/XBG1cRIzA3mzxm7J4/p+uY4jOPRWZh9ihUrjkqsmQXiurypsz q//FTTh8CPAX1wDeJRraCu7kjX8yZf0idSM7EWKzOz0RNCVEhZxouvZg99qVvbh2mhvDXD xzhWmBEBqwrL6Vs0Et2KJYVDT1DwrVPhMXu/5Z3JsvCmzyQGjQWo0DBnIPLhhjVoxOZNfx oArqQXwJwGn1EraqCNnjGiRkLp6q4VPdhe1Z8YOp6OjYjHLjocgjq8LXm4Exj17JD5ZUM1 UkZICwJa8J1XSpy54FPOweVHvM3HWlImC4z/j2oWhyGdDxPzaKgupgHlsDi2DA== References: <20261201091602.3003132-1-fan.chen@easystack.cn> Subject: Re: [PATCH v2 1/1] bcache: fix use-after-free when waiting for dirty-init threads Date: Fri, 4 Dec 2026 22:35:10 +0800 X-Mailing-List: linux-bcache@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 X-Original-From: Coly Li Cc: , From: "Coly Li" Content-Disposition: inline In-Reply-To: <20261201091602.3003132-1-fan.chen@easystack.cn> X-Lms-Return-Path: To: "Chen Fan" Message-Id: Content-Transfer-Encoding: 7bit Received: from studio.local ([120.245.64.92]) by smtp.larksuite.com with ESMTPS; Fri, 04 Dec 2026 14:35:12 +0000 Content-Type: text/plain; charset=UTF-8 On Tue, Dec 01, 2026 at 05:16:02PM +0800, Chen Fan wrote: > We use multiple threads to count dirty sectors in parallel (read-only). > bch_sectors_dirty_init() waits for the last dirty-init thread to set > state.started to 0. > > However, after the last thread sets state.started to 0 but before it calls > wake_up(&state->wait), bch_sectors_dirty_init() may observe started == 0, > return from wait_event() directly, and free local variable state. > The subsequent wake_up(&state->wait) then accesses freed memory, because > bch_sectors_dirty_init() has already exited. > > Fix this by using a completion to wait for all dirty-init threads, so that > no dirty-init thread can still access state after the wait completes. > > This issue was triggered in our environment with the following stack: > ------------[ cut here ]------------ > [30433.391386] Internal error: Oops: 0000000096000047 [#1] SMP > [30433.585804] CPU: 54 PID: 48242 Comm: bch_dirtcnt[1] Kdump: loaded Tainted: G 6.6.0-98.0.0.90.aarch64 #1 > [30433.718972] Call trace: > [30433.722722] native_queued_spin_lock_slowpath+0x33c/0x420 > [30433.729401] __wake_up_common_lock+0xbc/0x118 > [30433.735050] __wake_up+0x20/0x38 > [30433.739575] bch_dirty_init_thread+0x1c0/0x1f8 [bcache] > [30433.746169] kthread+0xf0/0x108 > [30433.750583] ret_from_fork+0x10/0x20 > [30433.755425] Code: 91002020 8b040284 f862d8a5 aa0403e2 (f8256841) > ---[ end trace ]--- > Fixes: b144e45fc576 ("bcache: make bch_sectors_dirty_init() to be multithreaded") Cc: # 5.7+ > Signed-off-by: Chen Fan The patch is good for me, I add the Fixes and CC stable in the above commit log. This patch is added into my for-next. Thanks. Coly Li > --- > drivers/md/bcache/writeback.c | 38 +++++++++++++---------------------- > drivers/md/bcache/writeback.h | 10 +-------- > 2 files changed, 15 insertions(+), 33 deletions(-) > > diff --git a/drivers/md/bcache/writeback.c b/drivers/md/bcache/writeback.c > index 4b237074f453..450685115670 100644 > --- a/drivers/md/bcache/writeback.c > +++ b/drivers/md/bcache/writeback.c > @@ -12,6 +12,7 @@ > #include "debug.h" > #include "writeback.h" > > +#include > #include > #include > #include > @@ -904,8 +905,7 @@ static int bch_root_node_dirty_init(struct cache_set *c, > > static int bch_dirty_init_thread(void *arg) > { > - struct dirty_init_thrd_info *info = arg; > - struct bch_dirty_init_state *state = info->state; > + struct bch_dirty_init_state *state = arg; > struct cache_set *c = state->c; > struct btree_iter_stack iter; > struct bkey *k, *p; > @@ -953,12 +953,7 @@ static int bch_dirty_init_thread(void *arg) > } > > out: > - /* In order to wake up state->wait in time */ > - smp_mb__before_atomic(); > - if (atomic_dec_and_test(&state->started)) > - wake_up(&state->wait); > - > - return 0; > + kthread_complete_and_exit(&state->done, 0); > } > > static int bch_btre_dirty_init_thread_nr(void) > @@ -975,8 +970,9 @@ static int bch_btre_dirty_init_thread_nr(void) > > void bch_sectors_dirty_init(struct bcache_device *d) > { > - int i; > + int i, launched = 0; > struct btree *b = NULL; > + struct task_struct *thread; > struct bkey *k = NULL; > struct btree_iter_stack iter; > struct sectors_dirty_init op; > @@ -1014,9 +1010,8 @@ void bch_sectors_dirty_init(struct bcache_device *d) > state.total_threads = bch_btre_dirty_init_thread_nr(); > state.key_idx = 0; > spin_lock_init(&state.idx_lock); > - atomic_set(&state.started, 0); > atomic_set(&state.enough, 0); > - init_waitqueue_head(&state.wait); > + init_completion(&state.done); > > for (i = 0; i < state.total_threads; i++) { > /* Fetch latest state.enough earlier */ > @@ -1024,23 +1019,18 @@ void bch_sectors_dirty_init(struct bcache_device *d) > if (atomic_read(&state.enough)) > break; > > - atomic_inc(&state.started); > - state.infos[i].state = &state; > - state.infos[i].thread = > - kthread_run(bch_dirty_init_thread, &state.infos[i], > - "bch_dirtcnt[%d]", i); > - if (IS_ERR(state.infos[i].thread)) { > + thread = kthread_run(bch_dirty_init_thread, &state, > + "bch_dirtcnt[%d]", i); > + if (IS_ERR(thread)) { > pr_err("fails to run thread bch_dirty_init[%d]\n", i); > - atomic_dec(&state.started); > - for (--i; i >= 0; i--) > - kthread_stop(state.infos[i].thread); > - goto out; > + break; > } > + launched++; > } > > -out: > - /* Must wait for all threads to stop. */ > - wait_event(state.wait, atomic_read(&state.started) == 0); > + /* Wait for all threads to finish accessing state. */ > + for (i = 0; i < launched; i++) > + wait_for_completion(&state.done); > rw_unlock(0, b); > } > > diff --git a/drivers/md/bcache/writeback.h b/drivers/md/bcache/writeback.h > index 31df716951f6..1ebd7e26ed25 100644 > --- a/drivers/md/bcache/writeback.h > +++ b/drivers/md/bcache/writeback.h > @@ -28,22 +28,14 @@ > */ > #define WRITEBACK_SHARE_SHIFT 14 > > -struct bch_dirty_init_state; > -struct dirty_init_thrd_info { > - struct bch_dirty_init_state *state; > - struct task_struct *thread; > -}; > - > struct bch_dirty_init_state { > struct cache_set *c; > struct bcache_device *d; > int total_threads; > int key_idx; > spinlock_t idx_lock; > - atomic_t started; > atomic_t enough; > - wait_queue_head_t wait; > - struct dirty_init_thrd_info infos[BCH_DIRTY_INIT_THRD_MAX]; > + struct completion done; > }; > > static inline uint64_t bcache_dev_sectors_dirty(struct bcache_device *d) > -- > 2.43.0