From: chenfan <fan.chen@easystack.cn>
To: Coly Li <colyli@fygo.io>
Cc: kent.overstreet@linux.dev, linux-bcache@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/1] bcache: fix use-after-free when waiting for dirty-init threads
Date: Fri, 27 Nov 2026 09:40:58 +0800 [thread overview]
Message-ID: <0810cc96-ad0b-4718-acf8-c2bbf43e388b@easystack.cn> (raw)
In-Reply-To: <A7BF631D-A5E8-47D0-B265-73C220200424@fygo.io>
Hi Coly,
Thanks for your advice, I will refactor it in the next few days.
在 2026/11/26 19:35, Coly Li 写道:
> Hi Chen Fan,
>
> Nice catch!
>
>> 2026年11月5日 21:19,Chen Fan <fan.chen@easystack.cn> 写道:
>>
>> 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 ]---
>>
>> Signed-off-by: Chen Fan <fan.chen@easystack.cn>
>> ---
>> drivers/md/bcache/writeback.c | 31 ++++++++++++-------------------
>> drivers/md/bcache/writeback.h | 4 +---
>> 2 files changed, 13 insertions(+), 22 deletions(-)
>>
>> diff --git a/drivers/md/bcache/writeback.c b/drivers/md/bcache/writeback.c
>> index 4b237074f453..e74457a8c952 100644
>> --- a/drivers/md/bcache/writeback.c
>> +++ b/drivers/md/bcache/writeback.c
> [snipped]
>
>> diff --git a/drivers/md/bcache/writeback.h b/drivers/md/bcache/writeback.h
>> index 31df716951f6..e6c74be20fc6 100644
>> --- a/drivers/md/bcache/writeback.h
>> +++ b/drivers/md/bcache/writeback.h
>> @@ -31,7 +31,6 @@
>> struct bch_dirty_init_state;
>> struct dirty_init_thrd_info {
>> struct bch_dirty_init_state *state;
>> - struct task_struct *thread;
>> };
>>
> After this change, struct dirty_init_thrd_info has only one member state,
> I feel you can totally remove struct dirty_init_thrd_info, and move state
> directly into struct bch_dirty_init_stat.
>
>
>> struct bch_dirty_init_state {
>> @@ -40,9 +39,8 @@ struct bch_dirty_init_state {
>> int total_threads;
>> int key_idx;
>> spinlock_t idx_lock;
>> - atomic_t started;
>> atomic_t enough;
>> - wait_queue_head_t wait;
>> + struct completion done;
>> struct dirty_init_thrd_info infos[BCH_DIRTY_INIT_THRD_MAX];
> You may think of changing the above line into
> struct bch_dirty_init_state state[BCH_DIRTY_INIT_THRD_MAX];
>
>
> Thanks.
>
> Coly Li
prev parent reply other threads:[~2026-11-27 1:41 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-05 13:19 Chen Fan
2026-11-12 15:52 ` Coly Li
2026-11-26 11:35 ` Coly Li
2026-11-27 1:40 ` chenfan [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=0810cc96-ad0b-4718-acf8-c2bbf43e388b@easystack.cn \
--to=fan.chen@easystack.cn \
--cc=colyli@fygo.io \
--cc=kent.overstreet@linux.dev \
--cc=linux-bcache@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®