Linux Btrfs filesystem development
 help / color / mirror / Atom feed
* [PATCH] btrfs: add __GFP_NOFAIL to the kvmalloc in split_item
@ 2026-12-03 23:36 Boris Burkov
  2026-12-04  0:00 ` Qu Wenruo
  2026-12-04 11:33 ` Harry Yoo
  0 siblings, 2 replies; 4+ messages in thread
From: Boris Burkov @ 2026-12-03 23:36 UTC (permalink / raw)
  To: linux-btrfs, kernel-team; +Cc: Shakeel Butt, Kiryl Shutsemau, Harry Yoo

The recent patch
b2ba99cf6f7b ("btrfs: use kvmalloc() for b-tree split_item()")

improved the behavior of the split_item() allocation for allocations of
order >= 4. However, we have observed that it seems to regress the
allocations of order 2 that are more typical for btrfs with 4k page
size, 16k nodesize and crc32c checksums (split_item gets called from
btrfs_del_csums only currently)

I believe that this is caused by kvmalloc having a path for failing
allocations (regardless of order) when fatal_signal_pending() is true on
the task as checked by vm_area_alloc_pages():

if (!(gfp & __GFP_NOFAIL) && fatal_signal_pending(current))
        break;

reached by the following callchain:
kvmalloc(item_size, GFP_NOFS)
  __kvmalloc_node_noprof()
    __do_kmalloc_node()
    __vmalloc_node_range_noprof()
      __vmalloc_area_node()
        vm_area_alloc_pages()

This results in the same signature of an ENOMEM abort observed in the
original patch:

BTRFS: error (device dm-3 state A) in btrfs_del_csums:1053: errno=-12 Out of memory
Link: https://lore.kernel.org/linux-btrfs/2ab022c5c7e07345a9fc91915836cbd4ab2d25ef.1788819456.git.wqu@suse.com/

I believe we could attempt to also fix this by having a mempool for this
csum deletion temporary item buffer, but save that for a follow-up since
this is a narrow fix for an immediate problem.

Fixes: b2ba99cf6f7b ("btrfs: use kvmalloc() for b-tree split_item()")
Signed-off-by: Boris Burkov <boris@bur.io>
---
 fs/btrfs/ctree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c
index a70860bfd109..b05fc297ebea 100644
--- a/fs/btrfs/ctree.c
+++ b/fs/btrfs/ctree.c
@@ -3943,7 +3943,7 @@ static noinline int split_item(struct btrfs_trans_handle *trans,
 	orig_offset = btrfs_item_offset(leaf, path->slots[0]);
 	item_size = btrfs_item_size(leaf, path->slots[0]);
 
-	buf = kvmalloc(item_size, GFP_NOFS);
+	buf = kvmalloc(item_size, GFP_NOFS | __GFP_NOFAIL);
 	if (!buf)
 		return -ENOMEM;
 
-- 
2.56.0


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

* Re: [PATCH] btrfs: add __GFP_NOFAIL to the kvmalloc in split_item
  2026-12-03 23:36 [PATCH] btrfs: add __GFP_NOFAIL to the kvmalloc in split_item Boris Burkov
@ 2026-12-04  0:00 ` Qu Wenruo
  2026-12-04  4:41   ` Qu Wenruo
  2026-12-04 11:33 ` Harry Yoo
  1 sibling, 1 reply; 4+ messages in thread
From: Qu Wenruo @ 2026-12-04  0:00 UTC (permalink / raw)
  To: Boris Burkov, linux-btrfs, kernel-team
  Cc: Shakeel Butt, Kiryl Shutsemau, Harry Yoo



在 2026/12/4 10:06, Boris Burkov 写道:
> The recent patch
> b2ba99cf6f7b ("btrfs: use kvmalloc() for b-tree split_item()")
> 
> improved the behavior of the split_item() allocation for allocations of
> order >= 4. However, we have observed that it seems to regress the
> allocations of order 2 that are more typical for btrfs with 4k page
> size, 16k nodesize and crc32c checksums (split_item gets called from
> btrfs_del_csums only currently)
> 
> I believe that this is caused by kvmalloc having a path for failing
> allocations (regardless of order) when fatal_signal_pending() is true on
> the task as checked by vm_area_alloc_pages():
> 
> if (!(gfp & __GFP_NOFAIL) && fatal_signal_pending(current))
>          break;

I agree with the analyze. Indeed this can lead to vmalloc failure 
unexpectedly, and this NOFAIL flag is the only way to address it so far.

> 
> reached by the following callchain:
> kvmalloc(item_size, GFP_NOFS)
>    __kvmalloc_node_noprof()
>      __do_kmalloc_node()
>      __vmalloc_node_range_noprof()
>        __vmalloc_area_node()
>          vm_area_alloc_pages()
> 
> This results in the same signature of an ENOMEM abort observed in the
> original patch:
> 
> BTRFS: error (device dm-3 state A) in btrfs_del_csums:1053: errno=-12 Out of memory
> Link: https://lore.kernel.org/linux-btrfs/2ab022c5c7e07345a9fc91915836cbd4ab2d25ef.1788819456.git.wqu@suse.com/

But I believe it's better to provide your own call trace.

The original call trace is from a workqueue, which should not got any 
signal IIRC.

Otherwise looks good to me.

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

Thanks,
Qu

> 
> I believe we could attempt to also fix this by having a mempool for this
> csum deletion temporary item buffer, but save that for a follow-up since
> this is a narrow fix for an immediate problem.
> 
> Fixes: b2ba99cf6f7b ("btrfs: use kvmalloc() for b-tree split_item()")
> Signed-off-by: Boris Burkov <boris@bur.io>
> ---
>   fs/btrfs/ctree.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c
> index a70860bfd109..b05fc297ebea 100644
> --- a/fs/btrfs/ctree.c
> +++ b/fs/btrfs/ctree.c
> @@ -3943,7 +3943,7 @@ static noinline int split_item(struct btrfs_trans_handle *trans,
>   	orig_offset = btrfs_item_offset(leaf, path->slots[0]);
>   	item_size = btrfs_item_size(leaf, path->slots[0]);
>   
> -	buf = kvmalloc(item_size, GFP_NOFS);
> +	buf = kvmalloc(item_size, GFP_NOFS | __GFP_NOFAIL);
>   	if (!buf)
>   		return -ENOMEM;
>   


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

* Re: [PATCH] btrfs: add __GFP_NOFAIL to the kvmalloc in split_item
  2026-12-04  0:00 ` Qu Wenruo
@ 2026-12-04  4:41   ` Qu Wenruo
  0 siblings, 0 replies; 4+ messages in thread
From: Qu Wenruo @ 2026-12-04  4:41 UTC (permalink / raw)
  To: Boris Burkov, linux-btrfs, kernel-team
  Cc: Shakeel Butt, Kiryl Shutsemau, Harry Yoo



在 2026/12/4 10:30, Qu Wenruo 写道:
> 
> 
> 在 2026/12/4 10:06, Boris Burkov 写道:
>> The recent patch
>> b2ba99cf6f7b ("btrfs: use kvmalloc() for b-tree split_item()")
>>
>> improved the behavior of the split_item() allocation for allocations of
>> order >= 4. However, we have observed that it seems to regress the
>> allocations of order 2 that are more typical for btrfs with 4k page
>> size, 16k nodesize and crc32c checksums (split_item gets called from
>> btrfs_del_csums only currently)
>>
>> I believe that this is caused by kvmalloc having a path for failing
>> allocations (regardless of order) when fatal_signal_pending() is true on
>> the task as checked by vm_area_alloc_pages():
>>
>> if (!(gfp & __GFP_NOFAIL) && fatal_signal_pending(current))
>>          break;
> 
> I agree with the analyze. Indeed this can lead to vmalloc failure 
> unexpectedly, and this NOFAIL flag is the only way to address it so far.
> 
>>
>> reached by the following callchain:
>> kvmalloc(item_size, GFP_NOFS)
>>    __kvmalloc_node_noprof()
>>      __do_kmalloc_node()
>>      __vmalloc_node_range_noprof()
>>        __vmalloc_area_node()
>>          vm_area_alloc_pages()
>>
>> This results in the same signature of an ENOMEM abort observed in the
>> original patch:
>>
>> BTRFS: error (device dm-3 state A) in btrfs_del_csums:1053: errno=-12 
>> Out of memory
>> Link: https://lore.kernel.org/linux- 
>> btrfs/2ab022c5c7e07345a9fc91915836cbd4ab2d25ef.1788819456.git.wqu@suse.com/
> 
> But I believe it's better to provide your own call trace.
> 
> The original call trace is from a workqueue, which should not got any 
> signal IIRC.
> 
> Otherwise looks good to me.
> 
> Reviewed-by: Qu Wenruo <wqu@suse.com>
> 
> Thanks,
> Qu
> 
>>
>> I believe we could attempt to also fix this by having a mempool for this
>> csum deletion temporary item buffer, but save that for a follow-up since
>> this is a narrow fix for an immediate problem.

BTW, there can be a better solution, without extra memory allocation.
(The real memory cost can even be handled by on-stack memory)

The main point of memory usage in split_item() is a typical memory swap:


Old leaf:

            /====================---------------------------\
           /                     \                           \
       |Item X|                  |<- Part A ->|<-- Part B --->|

New leaf:
          /======================================-----------\
         /                                       \           \
       |Item X|Item X + 1|       |<-- Part B --->|<- Part A ->|
                   \             /               /
                    \============---------------/

So the real work is to swap memory with the eb's page based layout.

We only need a small buffer, e.g. 16 bytes for each part, then can do 
the swap buffer by buffer.

The cost is the extra eb accesses, as we no longer do a large copy, but 
tons of small copies.

There may be some extra corner cases, but I'll explore this solution to 
completely get rid of the memory allocation.

Thanks,
Qu

>>
>> Fixes: b2ba99cf6f7b ("btrfs: use kvmalloc() for b-tree split_item()")
>> Signed-off-by: Boris Burkov <boris@bur.io>
>> ---
>>   fs/btrfs/ctree.c | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c
>> index a70860bfd109..b05fc297ebea 100644
>> --- a/fs/btrfs/ctree.c
>> +++ b/fs/btrfs/ctree.c
>> @@ -3943,7 +3943,7 @@ static noinline int split_item(struct 
>> btrfs_trans_handle *trans,
>>       orig_offset = btrfs_item_offset(leaf, path->slots[0]);
>>       item_size = btrfs_item_size(leaf, path->slots[0]);
>> -    buf = kvmalloc(item_size, GFP_NOFS);
>> +    buf = kvmalloc(item_size, GFP_NOFS | __GFP_NOFAIL);
>>       if (!buf)
>>           return -ENOMEM;
> 


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

* Re: [PATCH] btrfs: add __GFP_NOFAIL to the kvmalloc in split_item
  2026-12-03 23:36 [PATCH] btrfs: add __GFP_NOFAIL to the kvmalloc in split_item Boris Burkov
  2026-12-04  0:00 ` Qu Wenruo
@ 2026-12-04 11:33 ` Harry Yoo
  1 sibling, 0 replies; 4+ messages in thread
From: Harry Yoo @ 2026-12-04 11:33 UTC (permalink / raw)
  To: Boris Burkov
  Cc: linux-btrfs, kernel-team, Shakeel Butt, Kiryl Shutsemau, linux-mm

On Thu, Dec 03, 2026 at 03:36:53PM -0800, Boris Burkov wrote:
> The recent patch
> b2ba99cf6f7b ("btrfs: use kvmalloc() for b-tree split_item()")
> 
> improved the behavior of the split_item() allocation for allocations of
> order >= 4. However, we have observed that it seems to regress the
> allocations of order 2 that are more typical for btrfs with 4k page
> size, 16k nodesize and crc32c checksums (split_item gets called from
> btrfs_del_csums only currently)
> 
> I believe that this is caused by kvmalloc having a path for failing
> allocations (regardless of order) when fatal_signal_pending() is true on
> the task as checked by vm_area_alloc_pages():
> 
> if (!(gfp & __GFP_NOFAIL) && fatal_signal_pending(current))
>         break;

Right.

> reached by the following callchain:
> kvmalloc(item_size, GFP_NOFS)
>   __kvmalloc_node_noprof()
>     __do_kmalloc_node()
>     __vmalloc_node_range_noprof()
>       __vmalloc_area_node()
>         vm_area_alloc_pages()
> 
> This results in the same signature of an ENOMEM abort observed in the
> original patch:
> 
> BTRFS: error (device dm-3 state A) in btrfs_del_csums:1053: errno=-12 Out of memory
> Link: https://lore.kernel.org/linux-btrfs/2ab022c5c7e07345a9fc91915836cbd4ab2d25ef.1788819456.git.wqu@suse.com/

In this case I think it's fair to specify __GFP_NOFAIL as failing to
allocate it leads to a disruption and there's no reasonable fallback.

https://lore.kernel.org/linux-mm/20211122153233.9924-3-mhocko@kernel.org

> I believe we could attempt to also fix this by having a mempool for this
> csum deletion temporary item buffer, but save that for a follow-up since
> this is a narrow fix for an immediate problem.
> 
> Fixes: b2ba99cf6f7b ("btrfs: use kvmalloc() for b-tree split_item()")
> Signed-off-by: Boris Burkov <boris@bur.io>
> ---

For the kvmalloc() part, looks good to me, so:
Reviewed-by: Harry Yoo (Meta) <harry@kernel.org>

-- 
Cheers,
Harry / Hyeonggon

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

end of thread, other threads:[~2026-12-04 11:33 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 23:36 [PATCH] btrfs: add __GFP_NOFAIL to the kvmalloc in split_item Boris Burkov
2026-12-04  0:00 ` Qu Wenruo
2026-12-04  4:41   ` Qu Wenruo
2026-12-04 11:33 ` Harry Yoo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox