All of lore.kernel.org
 help / color / mirror / Atom feed
From: Niels Dossche <dossche.niels@gmail.com>
To: linux-fsdevel@vger.kernel.org
Cc: Alexander Viro <viro@zeniv.linux.org.uk>,
	Niels Dossche <dossche.niels@gmail.com>
Subject: [PATCH] direct-io: prevent possible race condition on bio_list
Date: Sat, 26 Feb 2022 23:17:48 +0100	[thread overview]
Message-ID: <20220226221748.197800-1-dossche.niels@gmail.com> (raw)

Prevent bio_list from changing in the while loop condition such that the
body of the loop won't execute with a potentially NULL pointer for
bio_list, which causes a NULL dereference later on.

Signed-off-by: Niels Dossche <dossche.niels@gmail.com>
---
 fs/direct-io.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/fs/direct-io.c b/fs/direct-io.c
index 654443558047..806f05407019 100644
--- a/fs/direct-io.c
+++ b/fs/direct-io.c
@@ -545,19 +545,22 @@ static inline int dio_bio_reap(struct dio *dio, struct dio_submit *sdio)
 	int ret = 0;
 
 	if (sdio->reap_counter++ >= 64) {
+		unsigned long flags;
+
+		spin_lock_irqsave(&dio->bio_lock, flags);
 		while (dio->bio_list) {
-			unsigned long flags;
 			struct bio *bio;
 			int ret2;
 
-			spin_lock_irqsave(&dio->bio_lock, flags);
 			bio = dio->bio_list;
 			dio->bio_list = bio->bi_private;
 			spin_unlock_irqrestore(&dio->bio_lock, flags);
 			ret2 = blk_status_to_errno(dio_bio_complete(dio, bio));
 			if (ret == 0)
 				ret = ret2;
+			spin_lock_irqsave(&dio->bio_lock, flags);
 		}
+		spin_unlock_irqrestore(&dio->bio_lock, flags);
 		sdio->reap_counter = 0;
 	}
 	return ret;
-- 
2.35.1


             reply	other threads:[~2022-02-26 22:18 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-26 22:17 Niels Dossche [this message]
2022-02-26 22:25 ` [PATCH] direct-io: prevent possible race condition on bio_list Matthew Wilcox
2022-02-26 22:29   ` Niels Dossche
2022-04-28 15:00     ` Jan Kara

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=20220226221748.197800-1-dossche.niels@gmail.com \
    --to=dossche.niels@gmail.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.