From: Andreas Gruenbacher <agruenba@redhat.com>
To: Christoph Hellwig <hch@infradead.org>,
Jens Axboe <axboe@kernel.dk>, Chris Mason <clm@fb.com>,
David Sterba <dsterba@suse.com>,
Satya Tangirala <satyat@google.com>
Cc: Andreas Gruenbacher <agruenba@redhat.com>,
linux-block@vger.kernel.org, linux-btrfs@vger.kernel.org,
linux-raid@vger.kernel.org, dm-devel@lists.linux.dev,
linux-kernel@vger.kernel.org
Subject: [RFC v2 06/17] bio: do not check bio->bi_status before assigning to it
Date: Sun, 21 Dec 2025 03:52:21 +0100 [thread overview]
Message-ID: <20251221025233.87087-7-agruenba@redhat.com> (raw)
In-Reply-To: <20251221025233.87087-1-agruenba@redhat.com>
In a few places, we check if to->bi_status is 0 before setting it. This
creates the impression of preserving the first error code that occurs,
but that is not actually the case because bio->bi_status is not updated
atomically. (And even updating bio->bi_status atomically wouldn't
preserve the first error in bio chains longer than two.) We don't
actually need to preserve the first error code, though; and all that
matters is that when an error occurs, one of the error codes is
preserved.
Checking bio->bi_status before assigning to it might also have been
meant as an optimization. We are on an error path and the potential
benefit is miniscule, though.
So, don't check bio->bi_status before assigning to it.
Created with Coccinelle using the following semantic patch:
@@
expression status;
struct bio *bio;
@@
- if (status && !bio->bi_status)
- bio->bi_status = status;
+ bio_set_status(bio, status);
@@
expression status;
struct bio *bio;
@@
- if (status != BLK_STS_OK && bio->bi_status == BLK_STS_OK)
- bio->bi_status = status;
+ bio_set_status(bio, status);
@@
expression status;
struct bio *bio;
@@
- if (unlikely(status) && !bio->bi_status)
- bio->bi_status = status;
+ bio_set_status(bio, status);
@@
expression status;
struct bio bio;
@@
- if (status && !bio.bi_status)
- bio.bi_status = status;
+ bio_set_status(&bio, status);
---
block/bio.c | 3 +--
block/fops.c | 3 +--
drivers/md/dm-integrity.c | 3 +--
drivers/md/dm-zoned-target.c | 3 +--
drivers/md/md.c | 6 ++----
5 files changed, 6 insertions(+), 12 deletions(-)
diff --git a/block/bio.c b/block/bio.c
index 3f408e1ba13f..5389321872f0 100644
--- a/block/bio.c
+++ b/block/bio.c
@@ -314,8 +314,7 @@ static struct bio *__bio_chain_endio(struct bio *bio)
{
struct bio *parent = bio->bi_private;
- if (bio->bi_status && !parent->bi_status)
- parent->bi_status = bio->bi_status;
+ bio_set_status(parent, bio->bi_status);
bio_put(bio);
return parent;
}
diff --git a/block/fops.c b/block/fops.c
index b4f911273289..a4a6972cbfbf 100644
--- a/block/fops.c
+++ b/block/fops.c
@@ -135,8 +135,7 @@ static void blkdev_bio_end_io(struct bio *bio)
bool should_dirty = dio->flags & DIO_SHOULD_DIRTY;
bool is_sync = dio->flags & DIO_IS_SYNC;
- if (bio->bi_status && !dio->bio.bi_status)
- dio->bio.bi_status = bio->bi_status;
+ bio_set_status(&dio->bio, bio->bi_status);
if (bio_integrity(bio))
bio_integrity_unmap_user(bio);
diff --git a/drivers/md/dm-integrity.c b/drivers/md/dm-integrity.c
index e380ed7b2a7b..6d04b067060d 100644
--- a/drivers/md/dm-integrity.c
+++ b/drivers/md/dm-integrity.c
@@ -1616,8 +1616,7 @@ static void dec_in_flight(struct dm_integrity_io *dio)
schedule_autocommit(ic);
bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
- if (unlikely(dio->bi_status) && !bio->bi_status)
- bio->bi_status = dio->bi_status;
+ bio_set_status(bio, dio->bi_status);
if (likely(!bio->bi_status) && unlikely(bio_sectors(bio) != dio->range.n_sectors)) {
dio->range.logical_sector += dio->range.n_sectors;
bio_advance(bio, dio->range.n_sectors << SECTOR_SHIFT);
diff --git a/drivers/md/dm-zoned-target.c b/drivers/md/dm-zoned-target.c
index 9da329078ea4..efb960eca8ea 100644
--- a/drivers/md/dm-zoned-target.c
+++ b/drivers/md/dm-zoned-target.c
@@ -77,8 +77,7 @@ static inline void dmz_bio_endio(struct bio *bio, blk_status_t status)
struct dmz_bioctx *bioctx =
dm_per_bio_data(bio, sizeof(struct dmz_bioctx));
- if (status != BLK_STS_OK && bio->bi_status == BLK_STS_OK)
- bio->bi_status = status;
+ bio_set_status(bio, status);
if (bioctx->dev && bio->bi_status != BLK_STS_OK)
bioctx->dev->flags |= DMZ_CHECK_BDEV;
diff --git a/drivers/md/md.c b/drivers/md/md.c
index cbca792fa380..5afc6d63aa7b 100644
--- a/drivers/md/md.c
+++ b/drivers/md/md.c
@@ -9084,8 +9084,7 @@ static void md_end_clone_io(struct bio *bio)
if (bio_data_dir(orig_bio) == WRITE && md_bitmap_enabled(mddev, false))
md_bitmap_end(mddev, md_io_clone);
- if (bio->bi_status && !orig_bio->bi_status)
- orig_bio->bi_status = bio->bi_status;
+ bio_set_status(orig_bio, bio->bi_status);
if (md_io_clone->start_time)
bio_end_io_acct(orig_bio, md_io_clone->start_time);
@@ -9136,8 +9135,7 @@ void md_free_cloned_bio(struct bio *bio)
if (bio_data_dir(orig_bio) == WRITE && md_bitmap_enabled(mddev, false))
md_bitmap_end(mddev, md_io_clone);
- if (bio->bi_status && !orig_bio->bi_status)
- orig_bio->bi_status = bio->bi_status;
+ bio_set_status(orig_bio, bio->bi_status);
if (md_io_clone->start_time)
bio_end_io_acct(orig_bio, md_io_clone->start_time);
--
2.52.0
next prev parent reply other threads:[~2025-12-21 2:53 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-21 2:52 [RFC v2 00/17] bio cleanups Andreas Gruenbacher
2025-12-21 2:52 ` [RFC v2 01/17] xfs: don't clobber bi_status in xfs_zone_alloc_and_submit Andreas Gruenbacher
2025-12-21 15:39 ` Andreas Gruenbacher
2025-12-21 2:52 ` [RFC v2 02/17] bio: rename bio_chain arguments Andreas Gruenbacher
2025-12-21 2:52 ` [RFC v2 03/17] bio: use bio_io_error more often Andreas Gruenbacher
2025-12-21 2:52 ` [RFC v2 04/17] bio: add bio_set_status Andreas Gruenbacher
2025-12-21 2:52 ` [RFC v2 05/17] bio: use bio_set_status for BLK_STS_* status codes Andreas Gruenbacher
2025-12-21 2:52 ` Andreas Gruenbacher [this message]
2025-12-21 2:52 ` [RFC v2 07/17] block: consecutive blk_status_t error codes Andreas Gruenbacher
2025-12-21 2:52 ` [RFC v2 08/17] block: fix blk_status_to_{errno,str} inconsistency Andreas Gruenbacher
2025-12-21 2:52 ` [RFC v2 09/17] block: turn blk_errors array into a macro Andreas Gruenbacher
2025-12-21 2:52 ` [RFC v2 10/17] block: optimize blk_status <=> errno conversion Andreas Gruenbacher
2025-12-21 2:52 ` [RFC v2 11/17] bio: bio_set_status from non-zero errno Andreas Gruenbacher
2025-12-21 2:52 ` [RFC v2 12/17] bio: do not check bio->bi_status before assigning to it (part 2) Andreas Gruenbacher
2025-12-21 2:52 ` [RFC v2 13/17] xfs: use bio_set_status in xfs_zone_alloc_and_submit Andreas Gruenbacher
2025-12-21 2:52 ` [RFC v2 14/17] bio: switch to bio_set_status in submit_bio_noacct Andreas Gruenbacher
2025-12-21 2:52 ` [RFC v2 15/17] bio: never set bi_status to BLK_STS_OK during completion Andreas Gruenbacher
2025-12-21 2:52 ` [RFC v2 16/17] bio: never set bi_status to BLK_STS_OK during completion (part 2) Andreas Gruenbacher
2025-12-21 2:52 ` [RFC v2 17/17] bio: add bio_endio_status Andreas Gruenbacher
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=20251221025233.87087-7-agruenba@redhat.com \
--to=agruenba@redhat.com \
--cc=axboe@kernel.dk \
--cc=clm@fb.com \
--cc=dm-devel@lists.linux.dev \
--cc=dsterba@suse.com \
--cc=hch@infradead.org \
--cc=linux-block@vger.kernel.org \
--cc=linux-btrfs@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-raid@vger.kernel.org \
--cc=satyat@google.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
all inboxes | Powered by JetHome®