mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Daniel Yang <danielyangkang@gmail.com>
To: "Theodore Ts'o" <tytso@mit.edu>,
	Andreas Dilger <adilger.kernel@dilger.ca>,
	linux-ext4@vger.kernel.org (open list:EXT4 FILE SYSTEM),
	linux-kernel@vger.kernel.org (open list)
Cc: Daniel Yang <danielyangkang@gmail.com>,
	syzbot+a388a53633c9a4e9b22e@syzkaller.appspotmail.com
Subject: [PATCH] fix: KCSAN data-race in ext4_buffered_write_iter()
Date: Sun,  3 Nov 2024 22:48:57 -0800	[thread overview]
Message-ID: <20241104064857.295818-1-danielyangkang@gmail.com> (raw)

In ext4_buffered_write_iter(), generic_write_sync() is being called at
the same time by two different CPUs. This causes a data-race for
inode->i_state. To prevent this, make generic_write_sync() a critical
section in ext4_buffered_write_iter(). Use mutex to allow preemption so
other CPU is not blocked while waiting.

Signed-off-by: Daniel Yang <danielyangkang@gmail.com>
Reported-by: syzbot+a388a53633c9a4e9b22e@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=a388a53633c9a4e9b22e
---
 fs/ext4/file.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/fs/ext4/file.c b/fs/ext4/file.c
index f14aed14b..ce1251d3b 100644
--- a/fs/ext4/file.c
+++ b/fs/ext4/file.c
@@ -19,6 +19,7 @@
  *	(jj@sunsite.ms.mff.cuni.cz)
  */
 
+#include "linux/mutex.h"
 #include <linux/time.h>
 #include <linux/fs.h>
 #include <linux/iomap.h>
@@ -282,6 +283,9 @@ static ssize_t ext4_write_checks(struct kiocb *iocb, struct iov_iter *from)
 	return count;
 }
 
+/* lock for critical section of generic_write_sync */
+static DEFINE_MUTEX(write_sync_lock);
+
 static ssize_t ext4_buffered_write_iter(struct kiocb *iocb,
 					struct iov_iter *from)
 {
@@ -302,7 +306,13 @@ static ssize_t ext4_buffered_write_iter(struct kiocb *iocb,
 	inode_unlock(inode);
 	if (unlikely(ret <= 0))
 		return ret;
-	return generic_write_sync(iocb, ret);
+
+	/* prevent read-write data race */
+	mutex_lock(&write_sync_lock);
+	ret = generic_write_sync(iocb, ret);
+	mutex_unlock(&write_sync_lock);
+
+	return ret;
 }
 
 static ssize_t ext4_handle_inode_extension(struct inode *inode, loff_t offset,
-- 
2.39.2


                 reply	other threads:[~2024-11-04  6:50 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20241104064857.295818-1-danielyangkang@gmail.com \
    --to=danielyangkang@gmail.com \
    --cc=adilger.kernel@dilger.ca \
    --cc=linux-ext4@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=syzbot+a388a53633c9a4e9b22e@syzkaller.appspotmail.com \
    --cc=tytso@mit.edu \
    /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

all inboxes | Powered by JetHome®