mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] erofs: fix erofs_insert_workgroup() lockref usage
@ 2023-10-31  6:05 Gao Xiang
  2023-10-31  6:20 ` Linus Torvalds
  2023-10-31 10:29 ` Chao Yu
  0 siblings, 2 replies; 4+ messages in thread
From: Gao Xiang @ 2023-10-31  6:05 UTC (permalink / raw)
  To: linux-erofs; +Cc: LKML, Linus Torvalds, Gao Xiang

As Linus pointed out [1], lockref_put_return() is fundamentally
designed to be something that can fail.  It behaves as a fastpath-only
thing, and the failure case needs to be handled anyway.

Actually, since the new pcluster was just allocated without being
populated, it won't be accessed by others until it is inserted into
XArray, so lockref helpers are actually unneeded here.

Let's just set the proper reference count on initializing.

[1] https://lore.kernel.org/r/CAHk-=whCga8BeQnJ3ZBh_Hfm9ctba_wpF444LpwRybVNMzO6Dw@mail.gmail.com
Fixes: 7674a42f35ea ("erofs: use struct lockref to replace handcrafted approach")
Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com>
---
 fs/erofs/utils.c | 8 +-------
 fs/erofs/zdata.c | 1 +
 2 files changed, 2 insertions(+), 7 deletions(-)

diff --git a/fs/erofs/utils.c b/fs/erofs/utils.c
index cc6fb9e98899..4256a85719a1 100644
--- a/fs/erofs/utils.c
+++ b/fs/erofs/utils.c
@@ -77,12 +77,7 @@ struct erofs_workgroup *erofs_insert_workgroup(struct super_block *sb,
 	struct erofs_sb_info *const sbi = EROFS_SB(sb);
 	struct erofs_workgroup *pre;
 
-	/*
-	 * Bump up before making this visible to others for the XArray in order
-	 * to avoid potential UAF without serialized by xa_lock.
-	 */
-	lockref_get(&grp->lockref);
-
+	DBG_BUGON(grp->lockref.count < 1);
 repeat:
 	xa_lock(&sbi->managed_pslots);
 	pre = __xa_cmpxchg(&sbi->managed_pslots, grp->index,
@@ -96,7 +91,6 @@ struct erofs_workgroup *erofs_insert_workgroup(struct super_block *sb,
 			cond_resched();
 			goto repeat;
 		}
-		lockref_put_return(&grp->lockref);
 		grp = pre;
 	}
 	xa_unlock(&sbi->managed_pslots);
diff --git a/fs/erofs/zdata.c b/fs/erofs/zdata.c
index 036f610e044b..a7e6847f6f8f 100644
--- a/fs/erofs/zdata.c
+++ b/fs/erofs/zdata.c
@@ -796,6 +796,7 @@ static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe)
 		return PTR_ERR(pcl);
 
 	spin_lock_init(&pcl->obj.lockref.lock);
+	pcl->obj.lockref.count = 1;	/* one ref for this request */
 	pcl->algorithmformat = map->m_algorithmformat;
 	pcl->length = 0;
 	pcl->partial = true;
-- 
2.39.3


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

* Re: [PATCH] erofs: fix erofs_insert_workgroup() lockref usage
  2023-10-31  6:05 [PATCH] erofs: fix erofs_insert_workgroup() lockref usage Gao Xiang
@ 2023-10-31  6:20 ` Linus Torvalds
  2023-10-31  6:26   ` Gao Xiang
  2023-10-31 10:29 ` Chao Yu
  1 sibling, 1 reply; 4+ messages in thread
From: Linus Torvalds @ 2023-10-31  6:20 UTC (permalink / raw)
  To: Gao Xiang; +Cc: linux-erofs, LKML

On Mon, 30 Oct 2023 at 20:08, Gao Xiang <hsiangkao@linux.alibaba.com> wrote:
>
> As Linus pointed out [1], lockref_put_return() is fundamentally
> designed to be something that can fail.  It behaves as a fastpath-only
> thing, and the failure case needs to be handled anyway.
>
> Actually, since the new pcluster was just allocated without being
> populated, it won't be accessed by others until it is inserted into
> XArray, so lockref helpers are actually unneeded here.
>
> Let's just set the proper reference count on initializing.

From a quick superficial look this looks like the right approach.
Thanks for the quick response.

              Linus

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

* Re: [PATCH] erofs: fix erofs_insert_workgroup() lockref usage
  2023-10-31  6:20 ` Linus Torvalds
@ 2023-10-31  6:26   ` Gao Xiang
  0 siblings, 0 replies; 4+ messages in thread
From: Gao Xiang @ 2023-10-31  6:26 UTC (permalink / raw)
  To: Linus Torvalds; +Cc: linux-erofs, LKML



On 2023/10/31 14:20, Linus Torvalds wrote:
> On Mon, 30 Oct 2023 at 20:08, Gao Xiang <hsiangkao@linux.alibaba.com> wrote:
>>
>> As Linus pointed out [1], lockref_put_return() is fundamentally
>> designed to be something that can fail.  It behaves as a fastpath-only
>> thing, and the failure case needs to be handled anyway.
>>
>> Actually, since the new pcluster was just allocated without being
>> populated, it won't be accessed by others until it is inserted into
>> XArray, so lockref helpers are actually unneeded here.
>>
>> Let's just set the proper reference count on initializing.
> 
>  From a quick superficial look this looks like the right approach.
> Thanks for the quick response.

Thanks, I will trigger a stress test for this and it will be included
in this pull request...

Thanks,
Gao Xiang

> 
>                Linus

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

* Re: [PATCH] erofs: fix erofs_insert_workgroup() lockref usage
  2023-10-31  6:05 [PATCH] erofs: fix erofs_insert_workgroup() lockref usage Gao Xiang
  2023-10-31  6:20 ` Linus Torvalds
@ 2023-10-31 10:29 ` Chao Yu
  1 sibling, 0 replies; 4+ messages in thread
From: Chao Yu @ 2023-10-31 10:29 UTC (permalink / raw)
  To: Gao Xiang, linux-erofs; +Cc: LKML

On 2023/10/31 14:05, Gao Xiang wrote:
> As Linus pointed out [1], lockref_put_return() is fundamentally
> designed to be something that can fail.  It behaves as a fastpath-only
> thing, and the failure case needs to be handled anyway.
> 
> Actually, since the new pcluster was just allocated without being
> populated, it won't be accessed by others until it is inserted into
> XArray, so lockref helpers are actually unneeded here.
> 
> Let's just set the proper reference count on initializing.
> 
> [1] https://lore.kernel.org/r/CAHk-=whCga8BeQnJ3ZBh_Hfm9ctba_wpF444LpwRybVNMzO6Dw@mail.gmail.com
> Fixes: 7674a42f35ea ("erofs: use struct lockref to replace handcrafted approach")
> Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com>

Reviewed-by: Chao Yu <chao@kernel.org>

Thanks,

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

end of thread, other threads:[~2023-10-31 10:30 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-31  6:05 [PATCH] erofs: fix erofs_insert_workgroup() lockref usage Gao Xiang
2023-10-31  6:20 ` Linus Torvalds
2023-10-31  6:26   ` Gao Xiang
2023-10-31 10:29 ` Chao Yu

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®