mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 1/1] bcache: fix use-after-free when waiting for dirty-init threads
@ 2026-11-05 13:19 Chen Fan
  2026-11-12 15:52 ` Coly Li
  2026-11-26 11:35 ` Coly Li
  0 siblings, 2 replies; 7+ messages in thread
From: Chen Fan @ 2026-11-05 13:19 UTC (permalink / raw)
  To: colyli, kent.overstreet; +Cc: linux-bcache, linux-kernel, fan.chen

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
@@ -16,6 +16,7 @@
 #include <linux/kthread.h>
 #include <linux/sched/clock.h>
 #include <trace/events/bcache.h>
+#include <linux/completion.h>
 
 static void update_gc_after_writeback(struct cache_set *c)
 {
@@ -953,12 +954,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 +971,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 +1011,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 +1020,20 @@ 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 =
+		thread =
 			kthread_run(bch_dirty_init_thread, &state.infos[i],
 				    "bch_dirtcnt[%d]", i);
-		if (IS_ERR(state.infos[i].thread)) {
+		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..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;
 };
 
 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];
 };
 
-- 
2.43.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/1] bcache: fix use-after-free when waiting for dirty-init threads
  2026-11-05 13:19 [PATCH 1/1] bcache: fix use-after-free when waiting for dirty-init threads Chen Fan
@ 2026-11-12 15:52 ` Coly Li
  2026-11-26 11:35 ` Coly Li
  1 sibling, 0 replies; 7+ messages in thread
From: Coly Li @ 2026-11-12 15:52 UTC (permalink / raw)
  To: Chen Fan; +Cc: kent.overstreet, linux-bcache, linux-kernel

Hi Chen Fan,

Thanks for the patch. 

> 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>


Recently I am overwhelmed by the CVE flood, let me respond you (and other patches) next week.

Best regards,

Coly Li

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/1] bcache: fix use-after-free when waiting for dirty-init threads
  2026-11-05 13:19 [PATCH 1/1] bcache: fix use-after-free when waiting for dirty-init threads Chen Fan
  2026-11-12 15:52 ` Coly Li
@ 2026-11-26 11:35 ` Coly Li
  2026-11-27  1:40   ` chenfan
  2026-12-01  9:22   ` chenfan
  1 sibling, 2 replies; 7+ messages in thread
From: Coly Li @ 2026-11-26 11:35 UTC (permalink / raw)
  To: Chen Fan; +Cc: kent.overstreet, linux-bcache, linux-kernel

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/1] bcache: fix use-after-free when waiting for dirty-init threads
  2026-11-26 11:35 ` Coly Li
@ 2026-11-27  1:40   ` chenfan
  2026-12-01  9:22   ` chenfan
  1 sibling, 0 replies; 7+ messages in thread
From: chenfan @ 2026-11-27  1:40 UTC (permalink / raw)
  To: Coly Li; +Cc: kent.overstreet, linux-bcache, linux-kernel

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/1] bcache: fix use-after-free when waiting for dirty-init threads
  2026-11-26 11:35 ` Coly Li
  2026-11-27  1:40   ` chenfan
@ 2026-12-01  9:22   ` chenfan
  2026-12-01 10:00     ` Coly Li
  1 sibling, 1 reply; 7+ messages in thread
From: chenfan @ 2026-12-01  9:22 UTC (permalink / raw)
  To: Coly Li; +Cc: kent.overstreet, linux-bcache, linux-kernel

Hi Coly,

please see inline.

在 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];

I notice that in bch_dirty_init_thread, where the wrapper only care 
`state`, so I think we can remove it too.

thanks


>
>
> Thanks.
>
> Coly Li

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/1] bcache: fix use-after-free when waiting for dirty-init threads
  2026-12-01  9:22   ` chenfan
@ 2026-12-01 10:00     ` Coly Li
  2026-12-01 12:37       ` chenfan
  0 siblings, 1 reply; 7+ messages in thread
From: Coly Li @ 2026-12-01 10:00 UTC (permalink / raw)
  To: chenfan; +Cc: kent.overstreet, linux-bcache, linux-kernel

> 2026年12月1日 17:22,chenfan <fan.chen@easystack.cn> 写道:
> 
> Hi Coly,
> 
> please see inline.
> 
> 在 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];
> 
> I notice that in bch_dirty_init_thread, where the wrapper only care `state`, so I think we can remove it too.

Can you describe the idea with more explicit details?

Thanks.

Coly Li

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/1] bcache: fix use-after-free when waiting for dirty-init threads
  2026-12-01 10:00     ` Coly Li
@ 2026-12-01 12:37       ` chenfan
  0 siblings, 0 replies; 7+ messages in thread
From: chenfan @ 2026-12-01 12:37 UTC (permalink / raw)
  To: Coly Li; +Cc: kent.overstreet, linux-bcache, linux-kernel

Hi Coly,

Sorry for the unclear explanation.
I actually want to remove is the `dirty_init_thrd_info` in struct 
`bch_dirty_init_state`.
because for bch_dirty_init_thread, we just care about the struct 
"bch_dirty_init_state"
as params,  so just give the `state`directly to bch_dirty_init_thread is 
enough.
I've sent a V2 patch that should explain it well.

Thanks

在 2026/12/1 18:00, Coly Li 写道:
>> 2026年12月1日 17:22,chenfan <fan.chen@easystack.cn> 写道:
>>
>> Hi Coly,
>>
>> please see inline.
>>
>> 在 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];
>> I notice that in bch_dirty_init_thread, where the wrapper only care `state`, so I think we can remove it too.
> Can you describe the idea with more explicit details?
>
> Thanks.
>
> Coly Li

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-12-01 12:37 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-05 13:19 [PATCH 1/1] bcache: fix use-after-free when waiting for dirty-init threads Chen Fan
2026-11-12 15:52 ` Coly Li
2026-11-26 11:35 ` Coly Li
2026-11-27  1:40   ` chenfan
2026-12-01  9:22   ` chenfan
2026-12-01 10:00     ` Coly Li
2026-12-01 12:37       ` chenfan

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®