Linux Btrfs filesystem development
 help / color / mirror / Atom feed
From: Yang Xiuwei <yangxiuwei@kylinos.cn>
To: Chris Mason <clm@fb.com>, David Sterba <dsterba@suse.com>
Cc: linux-btrfs@vger.kernel.org, Mark Harmstone <maharmstone@fb.com>,
	Yang Xiuwei <yangxiuwei@kylinos.cn>
Subject: [PATCH 1/3] btrfs: unlock inode and extent on pages alloc failure in uring read
Date: Wed,  5 Aug 2026 10:11:33 +0800	[thread overview]
Message-ID: <20260805021135.1896609-2-yangxiuwei@kylinos.cn> (raw)
In-Reply-To: <20260805021135.1896609-1-yangxiuwei@kylinos.cn>

btrfs_uring_read_extent() is only reached after btrfs_encoded_read()
has taken the inode shared lock and the extent lock.  If allocating
the pages array fails it returned -ENOMEM directly and left both
locks held.

Fixes: 34310c442e17 ("btrfs: add io_uring command for encoded reads (ENCODED_READ ioctl)")
Signed-off-by: Yang Xiuwei <yangxiuwei@kylinos.cn>
---
 fs/btrfs/ioctl.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index 68b33f365fda..c953e4409d5b 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -4626,8 +4626,10 @@ static int btrfs_uring_read_extent(struct kiocb *iocb, struct iov_iter *iter,
 
 	nr_pages = DIV_ROUND_UP(disk_io_size, PAGE_SIZE);
 	pages = kzalloc_objs(struct page *, nr_pages, GFP_NOFS);
-	if (!pages)
-		return -ENOMEM;
+	if (!pages) {
+		ret = -ENOMEM;
+		goto out_fail;
+	}
 	ret = btrfs_alloc_page_array(nr_pages, pages, GFP_NOFS);
 	if (ret) {
 		ret = -ENOMEM;
@@ -4677,9 +4679,11 @@ static int btrfs_uring_read_extent(struct kiocb *iocb, struct iov_iter *iter,
 	btrfs_unlock_extent(io_tree, start, lockend, &cached_state);
 	btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
 	kfree(priv);
-	for (int i = 0; i < nr_pages; i++) {
-		if (pages[i])
-			__free_page(pages[i]);
+	if (pages) {
+		for (int i = 0; i < nr_pages; i++) {
+			if (pages[i])
+				__free_page(pages[i]);
+		}
 	}
 	kfree(pages);
 	return ret;
-- 
2.25.1


  reply	other threads:[~2026-08-05  2:12 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-05  2:11 [PATCH 0/3] btrfs: fix io_uring encoded read cleanup paths Yang Xiuwei
2026-08-05  2:11 ` Yang Xiuwei [this message]
2026-08-05  3:19   ` [PATCH 1/3] btrfs: unlock inode and extent on pages alloc failure in uring read Qu Wenruo
2026-08-05  4:18     ` Yang Xiuwei
2026-08-05  2:11 ` [PATCH 2/3] btrfs: always return -EIOCBQUEUED after btrfs_uring_read_extent_endio Yang Xiuwei
2026-08-05  3:21   ` Qu Wenruo
2026-08-05  4:18     ` Yang Xiuwei
2026-08-05  2:11 ` [PATCH 3/3] btrfs: free iov when btrfs_uring_read_extent fails Yang Xiuwei

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=20260805021135.1896609-2-yangxiuwei@kylinos.cn \
    --to=yangxiuwei@kylinos.cn \
    --cc=clm@fb.com \
    --cc=dsterba@suse.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=maharmstone@fb.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