linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Yuan Tan <tanyuan@tinylab.org>
To: axboe@kernel.dk, syzbot+f2aaf773187f5cae54f3@syzkaller.appspotmail.com
Cc: linux-block@vger.kernel.org, akpm@linux-foundation.org,
	linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-mm@kvack.org, syzkaller-bugs@googlegroups.com,
	willy@infradead.org, falcon@tinylab.org, tanyuan@tinylab.org
Subject: [PATCH] block: add lock for safe nrpages access in invalidate_bdev()
Date: Mon, 10 Mar 2025 09:54:00 -0700	[thread overview]
Message-ID: <20250310165400.3166618-1-tanyuan@tinylab.org> (raw)
In-Reply-To: <67ceb38a.050a0220.e1a89.04b1.GAE@google.com>

Syzbot reported a data-race in __filemap_add_folio / invalidate_bdev[1]
due to concurrent access to mapping->nrpages.
Adds a lock around the access to nrpages.

[1] https://syzkaller.appspot.com/bug?extid=f2aaf773187f5cae54f3

Signed-off-by: Yuan Tan <tanyuan@tinylab.org>
Reported-by: syzbot+f2aaf773187f5cae54f3@syzkaller.appspotmail.com
---
 block/bdev.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

I had already completed and tested this patch before Matthew sent the
email. I'm not sure if this solution is correct. If it's not, please
ignore the patch :)

diff --git a/block/bdev.c b/block/bdev.c
index 9d73a8fbf7f9..934043d09068 100644
--- a/block/bdev.c
+++ b/block/bdev.c
@@ -96,7 +96,14 @@ void invalidate_bdev(struct block_device *bdev)
 {
 	struct address_space *mapping = bdev->bd_mapping;
 
-	if (mapping->nrpages) {
+	XA_STATE(xas, &mapping->i_pages, 0);  /* we don't care about the index */
+	unsigned long nrpages;
+
+	xas_lock_irq(&xas);
+	nrpages = mapping->nrpages;
+	xas_unlock_irq(&xas);
+
+	if (nrpages) {
 		invalidate_bh_lrus();
 		lru_add_drain_all();	/* make sure all lru add caches are flushed */
 		invalidate_mapping_pages(mapping, 0, -1);
-- 
2.25.1



  parent reply	other threads:[~2025-03-10 16:56 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-10  9:40 [syzbot] [mm?] [fs?] KCSAN: data-race in __filemap_add_folio / invalidate_bdev (8) syzbot
2025-03-10 15:28 ` Matthew Wilcox
2025-03-10 16:54 ` Yuan Tan [this message]
2025-03-10 17:11   ` [PATCH] block: add lock for safe nrpages access in invalidate_bdev() Matthew Wilcox

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=20250310165400.3166618-1-tanyuan@tinylab.org \
    --to=tanyuan@tinylab.org \
    --cc=akpm@linux-foundation.org \
    --cc=axboe@kernel.dk \
    --cc=falcon@tinylab.org \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=syzbot+f2aaf773187f5cae54f3@syzkaller.appspotmail.com \
    --cc=syzkaller-bugs@googlegroups.com \
    --cc=willy@infradead.org \
    /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