mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] btrfs: sysfs: restore NOFS context on early return in btrfs_sysfs_add_block_group_type()
@ 2026-10-15  6:53 guolingxing
  2026-10-15  8:52 ` Qu Wenruo
  0 siblings, 1 reply; 2+ messages in thread
From: guolingxing @ 2026-10-15  6:53 UTC (permalink / raw)
  To: David Sterba, Chris Mason, Josef Bacik
  Cc: linux-btrfs, linux-kernel, guolingxing, stable

btrfs_sysfs_add_block_group_type() sets up a NOFS context with
memalloc_nofs_save() because kobject_add() performs GFP_KERNEL
allocations deep in its call chain, and the function may be called
while holding a transaction handle or a lock required for a
transaction commit, where reclaim could deadlock.

Every other return path of this function calls memalloc_nofs_restore()
except the early return taken when another thread has already created
the kobject for the same raid index (this can happen when restriping).
Taking that path leaves PF_MEMALLOC_NOFS set on the calling task, so
all subsequent memory allocations performed by that task remain
restricted to the NOFS scope, which may lead to unexpected allocation
failures or deadlocks later on.

Restore the NOFS context before returning from that path.

Fixes: 49ea112da0e6 ("btrfs: do not create raid sysfs entries under any locks")
Cc: stable@vger.kernel.org
Signed-off-by: guolingxing <guolingxing@tju.edu.cn>
---
 fs/btrfs/sysfs.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c
index 39cb01ee441a..71b1ec6c0643 100644
--- a/fs/btrfs/sysfs.c
+++ b/fs/btrfs/sysfs.c
@@ -1857,6 +1857,7 @@ void btrfs_sysfs_add_block_group_type(struct btrfs_block_group *cache)
 	if (space_info->block_group_kobjs[index]) {
 		spin_unlock(&space_info->lock);
 		kobject_put(&rkobj->kobj);
+		memalloc_nofs_restore(nofs_flag);
 		return;
 	} else {
 		space_info->block_group_kobjs[index] = &rkobj->kobj;
-- 
2.34.1


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

* Re: [PATCH] btrfs: sysfs: restore NOFS context on early return in btrfs_sysfs_add_block_group_type()
  2026-10-15  6:53 [PATCH] btrfs: sysfs: restore NOFS context on early return in btrfs_sysfs_add_block_group_type() guolingxing
@ 2026-10-15  8:52 ` Qu Wenruo
  0 siblings, 0 replies; 2+ messages in thread
From: Qu Wenruo @ 2026-10-15  8:52 UTC (permalink / raw)
  To: guolingxing, David Sterba, Chris Mason, Josef Bacik
  Cc: linux-btrfs, linux-kernel, stable



在 2026/10/15 17:23, guolingxing 写道:
> btrfs_sysfs_add_block_group_type() sets up a NOFS context with
> memalloc_nofs_save() because kobject_add() performs GFP_KERNEL
> allocations deep in its call chain, and the function may be called
> while holding a transaction handle or a lock required for a
> transaction commit, where reclaim could deadlock.
> 
> Every other return path of this function calls memalloc_nofs_restore()
> except the early return taken when another thread has already created
> the kobject for the same raid index (this can happen when restriping).
> Taking that path leaves PF_MEMALLOC_NOFS set on the calling task, so
> all subsequent memory allocations performed by that task remain
> restricted to the NOFS scope, which may lead to unexpected allocation
> failures or deadlocks later on.
> 
> Restore the NOFS context before returning from that path.
> 
> Fixes: 49ea112da0e6 ("btrfs: do not create raid sysfs entries under any locks")
> Cc: stable@vger.kernel.org
> Signed-off-by: guolingxing <guolingxing@tju.edu.cn>

Reviewed-by: Qu Wenruo <wqu@suse.com>

> ---
>   fs/btrfs/sysfs.c | 1 +
>   1 file changed, 1 insertion(+)
> 
> diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c
> index 39cb01ee441a..71b1ec6c0643 100644
> --- a/fs/btrfs/sysfs.c
> +++ b/fs/btrfs/sysfs.c
> @@ -1857,6 +1857,7 @@ void btrfs_sysfs_add_block_group_type(struct btrfs_block_group *cache)
>   	if (space_info->block_group_kobjs[index]) {
>   		spin_unlock(&space_info->lock);
>   		kobject_put(&rkobj->kobj);
> +		memalloc_nofs_restore(nofs_flag);
>   		return;
>   	} else {
>   		space_info->block_group_kobjs[index] = &rkobj->kobj;


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

end of thread, other threads:[~2026-10-15  8:53 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-15  6:53 [PATCH] btrfs: sysfs: restore NOFS context on early return in btrfs_sysfs_add_block_group_type() guolingxing
2026-10-15  8:52 ` Qu Wenruo

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®