mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] ubi: Fix deadlock caused by recursively holding work_sem
@ 2023-03-04  1:41 ZhaoLong Wang
  2023-03-04  2:32 ` Zhihao Cheng
  0 siblings, 1 reply; 3+ messages in thread
From: ZhaoLong Wang @ 2023-03-04  1:41 UTC (permalink / raw)
  To: richard, miquel.raynal, vigneshr
  Cc: linux-mtd, linux-kernel, wangzhaolong1, yi.zhang

During the processing of the bgt, if the sync_erase() return -EBUSY
or some other error code in __erase_worker(),schedule_erase() called
again lead to the down_read(ubi->work_sem) hold twice and may get
block by down_write(ubi->work_sem) in ubi_update_fastmap(),
which cause deadlock.

          ubi bgt                        other task
 do_work
  down_read(&ubi->work_sem)          ubi_update_fastmap
  erase_worker                         # Blocked by down_read
   __erase_worker                      down_write(&ubi->work_sem)
    schedule_erase
     schedule_ubi_work
      down_read(&ubi->work_sem)

Fix this by changing input parameter @nested of the schedule_erase() to
'true' to avoid recursively acquiring the down_read(&ubi->work_sem).

Also, fix the incorrect comment about @nested parameter of the
schedule_erase() because when down_write(ubi->work_sem) is held, the
@nested is also need be true.

Link: https://bugzilla.kernel.org/show_bug.cgi?id=217093
Fixes: 2e8f08deabbc ("ubi: Fix races around ubi_refill_pools()")
Signed-off-by: ZhaoLong Wang <wangzhaolong1@huawei.com>
---
 drivers/mtd/ubi/wl.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/mtd/ubi/wl.c b/drivers/mtd/ubi/wl.c
index 40f39e5d6dfc..26a214f016c1 100644
--- a/drivers/mtd/ubi/wl.c
+++ b/drivers/mtd/ubi/wl.c
@@ -575,7 +575,7 @@ static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
  * @vol_id: the volume ID that last used this PEB
  * @lnum: the last used logical eraseblock number for the PEB
  * @torture: if the physical eraseblock has to be tortured
- * @nested: denotes whether the work_sem is already held in read mode
+ * @nested: denotes whether the work_sem is already held
  *
  * This function returns zero in case of success and a %-ENOMEM in case of
  * failure.
@@ -1131,7 +1131,7 @@ static int __erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk)
 		int err1;
 
 		/* Re-schedule the LEB for erasure */
-		err1 = schedule_erase(ubi, e, vol_id, lnum, 0, false);
+		err1 = schedule_erase(ubi, e, vol_id, lnum, 0, true);
 		if (err1) {
 			spin_lock(&ubi->wl_lock);
 			wl_entry_destroy(ubi, e);
-- 
2.31.1


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

* Re: [PATCH] ubi: Fix deadlock caused by recursively holding work_sem
  2023-03-04  1:41 [PATCH] ubi: Fix deadlock caused by recursively holding work_sem ZhaoLong Wang
@ 2023-03-04  2:32 ` Zhihao Cheng
  2023-03-04 16:59   ` Richard Weinberger
  0 siblings, 1 reply; 3+ messages in thread
From: Zhihao Cheng @ 2023-03-04  2:32 UTC (permalink / raw)
  To: ZhaoLong Wang, richard, miquel.raynal, vigneshr
  Cc: linux-mtd, linux-kernel, yi.zhang

> During the processing of the bgt, if the sync_erase() return -EBUSY
> or some other error code in __erase_worker(),schedule_erase() called
> again lead to the down_read(ubi->work_sem) hold twice and may get
> block by down_write(ubi->work_sem) in ubi_update_fastmap(),
> which cause deadlock.
> 
>            ubi bgt                        other task
>   do_work
>    down_read(&ubi->work_sem)          ubi_update_fastmap
>    erase_worker                         # Blocked by down_read
>     __erase_worker                      down_write(&ubi->work_sem)
>      schedule_erase
>       schedule_ubi_work
>        down_read(&ubi->work_sem)
> 
> Fix this by changing input parameter @nested of the schedule_erase() to
> 'true' to avoid recursively acquiring the down_read(&ubi->work_sem).
> 
> Also, fix the incorrect comment about @nested parameter of the
> schedule_erase() because when down_write(ubi->work_sem) is held, the
> @nested is also need be true.
> 
> Link: https://bugzilla.kernel.org/show_bug.cgi?id=217093
> Fixes: 2e8f08deabbc ("ubi: Fix races around ubi_refill_pools()")
> Signed-off-by: ZhaoLong Wang <wangzhaolong1@huawei.com>
> ---
>   drivers/mtd/ubi/wl.c | 4 ++--
>   1 file changed, 2 insertions(+), 2 deletions(-)

Reviewed-by: Zhihao Cheng <chengzhihao1@huawei.com>
> 
> diff --git a/drivers/mtd/ubi/wl.c b/drivers/mtd/ubi/wl.c
> index 40f39e5d6dfc..26a214f016c1 100644
> --- a/drivers/mtd/ubi/wl.c
> +++ b/drivers/mtd/ubi/wl.c
> @@ -575,7 +575,7 @@ static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
>    * @vol_id: the volume ID that last used this PEB
>    * @lnum: the last used logical eraseblock number for the PEB
>    * @torture: if the physical eraseblock has to be tortured
> - * @nested: denotes whether the work_sem is already held in read mode
> + * @nested: denotes whether the work_sem is already held
>    *
>    * This function returns zero in case of success and a %-ENOMEM in case of
>    * failure.
> @@ -1131,7 +1131,7 @@ static int __erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk)
>   		int err1;
>   
>   		/* Re-schedule the LEB for erasure */
> -		err1 = schedule_erase(ubi, e, vol_id, lnum, 0, false);
> +		err1 = schedule_erase(ubi, e, vol_id, lnum, 0, true);
>   		if (err1) {
>   			spin_lock(&ubi->wl_lock);
>   			wl_entry_destroy(ubi, e);
> 


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

* Re: [PATCH] ubi: Fix deadlock caused by recursively holding work_sem
  2023-03-04  2:32 ` Zhihao Cheng
@ 2023-03-04 16:59   ` Richard Weinberger
  0 siblings, 0 replies; 3+ messages in thread
From: Richard Weinberger @ 2023-03-04 16:59 UTC (permalink / raw)
  To: chengzhihao1
  Cc: ZhaoLong Wang, Miquel Raynal, Vignesh Raghavendra, linux-mtd,
	linux-kernel, yi zhang

----- Ursprüngliche Mail -----
> Von: "chengzhihao1" <chengzhihao1@huawei.com>
>> During the processing of the bgt, if the sync_erase() return -EBUSY
>> or some other error code in __erase_worker(),schedule_erase() called
>> again lead to the down_read(ubi->work_sem) hold twice and may get
>> block by down_write(ubi->work_sem) in ubi_update_fastmap(),
>> which cause deadlock.
>> 
>>            ubi bgt                        other task
>>   do_work
>>    down_read(&ubi->work_sem)          ubi_update_fastmap
>>    erase_worker                         # Blocked by down_read
>>     __erase_worker                      down_write(&ubi->work_sem)
>>      schedule_erase
>>       schedule_ubi_work
>>        down_read(&ubi->work_sem)
>> 
>> Fix this by changing input parameter @nested of the schedule_erase() to
>> 'true' to avoid recursively acquiring the down_read(&ubi->work_sem).
>> 
>> Also, fix the incorrect comment about @nested parameter of the
>> schedule_erase() because when down_write(ubi->work_sem) is held, the
>> @nested is also need be true.
>> 
>> Link: https://bugzilla.kernel.org/show_bug.cgi?id=217093
>> Fixes: 2e8f08deabbc ("ubi: Fix races around ubi_refill_pools()")
>> Signed-off-by: ZhaoLong Wang <wangzhaolong1@huawei.com>
>> ---
>>   drivers/mtd/ubi/wl.c | 4 ++--
>>   1 file changed, 2 insertions(+), 2 deletions(-)
> 
> Reviewed-by: Zhihao Cheng <chengzhihao1@huawei.com>

Applied to -next. Thanks everyone!

Thanks,
//richard

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

end of thread, other threads:[~2023-03-04 16:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-04  1:41 [PATCH] ubi: Fix deadlock caused by recursively holding work_sem ZhaoLong Wang
2023-03-04  2:32 ` Zhihao Cheng
2023-03-04 16:59   ` Richard Weinberger

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®