* [PATCH] btrfs: fix __folio_put with no deref in btrfs_do_encoded_write
@ 2024-07-01 20:52 Boris Burkov
2024-07-01 22:23 ` Qu Wenruo
0 siblings, 1 reply; 2+ messages in thread
From: Boris Burkov @ 2024-07-01 20:52 UTC (permalink / raw)
To: linux-btrfs, kernel-team
The conversion to folios switched __free_page to __folio_put in the
error path in btrfs_do_encoded_write.
However, this gets the page refcounting wrong. If we do hit that error
path (I reproduced by modifying btrfs_do_encoded_write to pretend to
always fail in a way that jumps to out_folios and running the xfstest
btrfs/281), then we always hit the following BUG freeing the folio:
BUG: Bad page state in process btrfs pfn:40ab0b
page: refcount:1 mapcount:0 mapping:0000000000000000 index:0x61be5 pfn:0x40ab0b
flags: 0x5ffff0000000000(node=0|zone=2|lastcpupid=0x1ffff)
raw: 05ffff0000000000 0000000000000000 dead000000000122 0000000000000000
raw: 0000000000061be5 0000000000000000 00000001ffffffff 0000000000000000
page dumped because: nonzero _refcount
Call Trace:
<TASK>
dump_stack_lvl+0x3d/0xe0
bad_page+0xea/0xf0
free_unref_page+0x8e1/0x900
? __mem_cgroup_uncharge+0x69/0x90
__folio_put+0xe6/0x190
btrfs_do_encoded_write+0x445/0x780
? current_time+0x25/0xd0
btrfs_do_write_iter+0x2cc/0x4b0
btrfs_ioctl_encoded_write+0x2b6/0x340
It turns out __free_page dereferenced the page while __folio_put does
not. Switch __folio_put to folio_put which does dereference the page
first.
Fixes: 400b172b8cdc ("btrfs: compression: migrate compression/decompression paths to folios")
Signed-off-by: Boris Burkov <boris@bur.io>
---
fs/btrfs/inode.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 0a11d309ee89..12fb7e8056a1 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -9558,7 +9558,7 @@ ssize_t btrfs_do_encoded_write(struct kiocb *iocb, struct iov_iter *from,
out_folios:
for (i = 0; i < nr_folios; i++) {
if (folios[i])
- __folio_put(folios[i]);
+ folio_put(folios[i]);
}
kvfree(folios);
out:
--
2.45.2
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] btrfs: fix __folio_put with no deref in btrfs_do_encoded_write
2024-07-01 20:52 [PATCH] btrfs: fix __folio_put with no deref in btrfs_do_encoded_write Boris Burkov
@ 2024-07-01 22:23 ` Qu Wenruo
0 siblings, 0 replies; 2+ messages in thread
From: Qu Wenruo @ 2024-07-01 22:23 UTC (permalink / raw)
To: Boris Burkov, linux-btrfs, kernel-team
在 2024/7/2 06:22, Boris Burkov 写道:
> The conversion to folios switched __free_page to __folio_put in the
> error path in btrfs_do_encoded_write.
>
> However, this gets the page refcounting wrong. If we do hit that error
> path (I reproduced by modifying btrfs_do_encoded_write to pretend to
> always fail in a way that jumps to out_folios and running the xfstest
> btrfs/281), then we always hit the following BUG freeing the folio:
>
> BUG: Bad page state in process btrfs pfn:40ab0b
> page: refcount:1 mapcount:0 mapping:0000000000000000 index:0x61be5 pfn:0x40ab0b
> flags: 0x5ffff0000000000(node=0|zone=2|lastcpupid=0x1ffff)
> raw: 05ffff0000000000 0000000000000000 dead000000000122 0000000000000000
> raw: 0000000000061be5 0000000000000000 00000001ffffffff 0000000000000000
> page dumped because: nonzero _refcount
> Call Trace:
> <TASK>
> dump_stack_lvl+0x3d/0xe0
> bad_page+0xea/0xf0
> free_unref_page+0x8e1/0x900
> ? __mem_cgroup_uncharge+0x69/0x90
> __folio_put+0xe6/0x190
> btrfs_do_encoded_write+0x445/0x780
> ? current_time+0x25/0xd0
> btrfs_do_write_iter+0x2cc/0x4b0
> btrfs_ioctl_encoded_write+0x2b6/0x340
>
> It turns out __free_page dereferenced the page while __folio_put does
> not. Switch __folio_put to folio_put which does dereference the page
> first.
>
> Fixes: 400b172b8cdc ("btrfs: compression: migrate compression/decompression paths to folios")
> Signed-off-by: Boris Burkov <boris@bur.io>
My bad, I should go deeper before doing the switch from __free_page() to
__folio_put().
Reviewed-by: Qu Wenruo <wqu@suse.com>
Thanks for catching this!
Qu
> ---
> fs/btrfs/inode.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
> index 0a11d309ee89..12fb7e8056a1 100644
> --- a/fs/btrfs/inode.c
> +++ b/fs/btrfs/inode.c
> @@ -9558,7 +9558,7 @@ ssize_t btrfs_do_encoded_write(struct kiocb *iocb, struct iov_iter *from,
> out_folios:
> for (i = 0; i < nr_folios; i++) {
> if (folios[i])
> - __folio_put(folios[i]);
> + folio_put(folios[i]);
> }
> kvfree(folios);
> out:
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-07-01 22:23 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-07-01 20:52 [PATCH] btrfs: fix __folio_put with no deref in btrfs_do_encoded_write Boris Burkov
2024-07-01 22:23 ` Qu Wenruo
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox