public inbox for llvm@lists.linux.dev
 help / color / mirror / Atom feed
From: Hawkins Jiawei <yin31149@gmail.com>
To: yin31149@gmail.com, Chris Mason <clm@fb.com>,
	Josef Bacik <josef@toxicpanda.com>,
	David Sterba <dsterba@suse.com>
Cc: 18801353760@163.com, linux-btrfs@vger.kernel.org,
	linux-kernel@vger.kernel.org, llvm@lists.linux.dev,
	nathan@kernel.org, ndesaulniers@google.com,
	syzbot+dde7e853812ed57835ea@syzkaller.appspotmail.com,
	syzkaller-bugs@googlegroups.com, trix@redhat.com
Subject: [PATCH] btrfs: update bytes_may_use in btrfs_free_reserved_bytes()
Date: Mon, 31 Oct 2022 00:22:24 +0800	[thread overview]
Message-ID: <20221030162223.25970-1-yin31149@gmail.com> (raw)

Syzkaller reports warning as follows:
=====================================
WARNING: CPU: 0 PID: 3612 at fs/btrfs/space-info.h:122
  btrfs_space_info_free_bytes_may_use fs/btrfs/space-info.h:154 [inline]
WARNING: CPU: 0 PID: 3612 at fs/btrfs/space-info.h:122
  block_rsv_release_bytes fs/btrfs/block-rsv.c:151 [inline]
WARNING: CPU: 0 PID: 3612 at fs/btrfs/space-info.h:122
  btrfs_block_rsv_release+0x5d1/0x730 fs/btrfs/block-rsv.c:295
Modules linked in:
[...]
RIP: 0010:btrfs_space_info_update_bytes_may_use
  fs/btrfs/space-info.h:122 [inline]
RIP: 0010:btrfs_space_info_free_bytes_may_use
  fs/btrfs/space-info.h:154 [inline]
RIP: 0010:block_rsv_release_bytes
  fs/btrfs/block-rsv.c:151 [inline]
RIP: 0010:btrfs_block_rsv_release+0x5d1/0x730
  fs/btrfs/block-rsv.c:295
[...]
Call Trace:
 <TASK>
 btrfs_release_global_block_rsv+0x2f/0x250 fs/btrfs/block-rsv.c:463
 btrfs_free_block_groups+0xb67/0xfd0 fs/btrfs/block-group.c:4053
 close_ctree+0x6c5/0xbde fs/btrfs/disk-io.c:4710
 generic_shutdown_super+0x130/0x310 fs/super.c:491
 kill_anon_super+0x36/0x60 fs/super.c:1085
 btrfs_kill_super+0x3d/0x50 fs/btrfs/super.c:2441
 deactivate_locked_super+0xa7/0xf0 fs/super.c:331
 cleanup_mnt+0x4ce/0x560 fs/namespace.c:1186
 task_work_run+0x146/0x1c0 kernel/task_work.c:177
 ptrace_notify+0x29a/0x340 kernel/signal.c:2354
 ptrace_report_syscall include/linux/ptrace.h:420 [inline]
 ptrace_report_syscall_exit include/linux/ptrace.h:482 [inline]
 syscall_exit_work+0x8c/0xe0 kernel/entry/common.c:249
 syscall_exit_to_user_mode_prepare+0x63/0xc0 kernel/entry/common.c:276
 __syscall_exit_to_user_mode_work kernel/entry/common.c:281 [inline]
 syscall_exit_to_user_mode+0xa/0x60 kernel/entry/common.c:294
 do_syscall_64+0x49/0xb0 arch/x86/entry/common.c:86
 entry_SYSCALL_64_after_hwframe+0x63/0xcd
 [...]
 </TASK>
=====================================

In btrfs_new_extent_direct(), kernel will reserves space for extent
by btrfs_reserve_extent(), and frees those space by
btrfs_free_reserved_extent() if btrfs_create_dio_extent() fails.

Yet the problem is that, it may not update the space
info correctly. To be more specific, kernel will
converts space from ->bytes_may_use to ->bytes_reserved, in
btrfs_add_reserved_bytes() when reserving space.
But when freeing those space in btrfs_free_reserved_bytes(),
kernel does not convert space from ->bytes_reserved back to
->bytes_may_use, which triggers the above warning.

This patch solves it by converting space from ->bytes_reserved
back to ->bytes_may_use in btrfs_free_reserved_bytes().

Link: https://lore.kernel.org/all/0000000000002a909705eb841dda@google.com
Reported-by: syzbot+dde7e853812ed57835ea@syzkaller.appspotmail.com
Tested-by: syzbot+dde7e853812ed57835ea@syzkaller.appspotmail.com
Signed-off-by: Hawkins Jiawei <yin31149@gmail.com>
---
 fs/btrfs/block-group.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
index deebc8ddbd93..1b573ab5514b 100644
--- a/fs/btrfs/block-group.c
+++ b/fs/btrfs/block-group.c
@@ -3414,6 +3414,10 @@ void btrfs_free_reserved_bytes(struct btrfs_block_group *cache,
 	cache->reserved -= num_bytes;
 	space_info->bytes_reserved -= num_bytes;
 	space_info->max_extent_size = 0;
+	trace_btrfs_space_reservation(cache->fs_info, "space_info",
+				      space_info->flags, -num_bytes, 1);
+	btrfs_space_info_update_bytes_may_use(cache->fs_info,
+					      space_info, num_bytes);
 
 	if (delalloc)
 		cache->delalloc_bytes -= num_bytes;
-- 
2.25.1


             reply	other threads:[~2022-10-30 16:22 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-30 16:22 Hawkins Jiawei [this message]
2022-10-31 15:11 ` [PATCH] btrfs: update bytes_may_use in btrfs_free_reserved_bytes() Josef Bacik
2022-10-31 15:44   ` Hawkins Jiawei

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221030162223.25970-1-yin31149@gmail.com \
    --to=yin31149@gmail.com \
    --cc=18801353760@163.com \
    --cc=clm@fb.com \
    --cc=dsterba@suse.com \
    --cc=josef@toxicpanda.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=syzbot+dde7e853812ed57835ea@syzkaller.appspotmail.com \
    --cc=syzkaller-bugs@googlegroups.com \
    --cc=trix@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox