Skip to contentSkip to conversation

block, md: Better handle REQ_OP_FLUSH

PATCH In review Song Liu 4 participants 9 messages Dec 21, 2023

Subsystems (2)
lore
Share & tools
lore

Song Liu

author maintainer
#0 lore raw

cover block, md: Better handle REQ_OP_FLUSH

A recent bug report [1] shows md is handling a flush from bcachefs as read:

bch2_journal_write=>
submit_bio=>

    ...
    md_handle_request =>
      raid5_make_request =>
        chunk_aligned_read =>
          raid5_read_one_chunk =>
	    ...

It appears md code only checks REQ_PREFLUSH for flush requests, which doesn't cover all cases. OTOH, op_is_flush() doesn't check REQ_OP_FLUSH either.

Fix this by:
1) Check REQ_PREFLUSH in op_is_flush();
2) Use op_is_flush() in md code.

Thanks,
Song

[1] https://bugzilla.kernel.org/show_bug.cgi?id=218184

Song Liu (2):
block: Check REQ_OP_FLUSH in op_is_flush()
md: Use op_is_flush() to check flush bio

 drivers/md/raid0.c        | 2 +-
 drivers/md/raid1.c        | 2 +-
 drivers/md/raid10.c       | 2 +-
 drivers/md/raid5.c        | 2 +-
 include/linux/blk_types.h | 3 ++-
 5 files changed, 6 insertions(+), 5 deletions(-)

--
2.34.1

author maintainer
#1 lore raw

PATCH 1/2 block: Check REQ_OP_FLUSH in op_is_flush()

Upper layer (fs, etc.) may issue flush with something like:

  bio_reset(bio, bdev, REQ_OP_FLUSH);
  bio->bi_end_io = xxx;
  submit_bio(bio);

op_is_flush(bio->bi_opf) should return true for this bio.

---
 include/linux/blk_types.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/linux/blk_types.h +2 -1
index d5c5e59ddbd2..338423da84ca 100644
@@ -487,7 +487,8 @@ static inline bool op_is_write(blk_opf_t op)
  */
 static inline bool op_is_flush(blk_opf_t op)
 {
-	return op & (REQ_FUA | REQ_PREFLUSH);
+	return op & (REQ_FUA | REQ_PREFLUSH) ||
+		(op & REQ_OP_MASK) == REQ_OP_FLUSH;
 }
 
 /*
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 1/2] block: Check REQ_OP_FLUSH in op_is_flush()

Quoted 5 lines · Song Liu

Upper layer (fs, etc.) may issue flush with something like:

  bio_reset(bio, bdev, REQ_OP_FLUSH);
  bio->bi_end_io = xxx;
  submit_bio(bio);

No, they can't. REQ_OP_FLUSH is currently only used by request based drivers and only generated by the flush state machine.

(Not that I particularly like this wart, I'd much prefer file systems to submit a REQ_OP_FLUSH than an empty write with a preflush flag, but that's not a trivial change).

author maintainer
#3 lore raw

Re: [PATCH 1/2] block: Check REQ_OP_FLUSH in op_is_flush()

Quoted 10 lines · Christoph Hellwig
Quoted 5 lines · Song Liu

Upper layer (fs, etc.) may issue flush with something like:

  bio_reset(bio, bdev, REQ_OP_FLUSH);
  bio->bi_end_io = xxx;
  submit_bio(bio);

No, they can't. REQ_OP_FLUSH is currently only used by request based drivers and only generated by the flush state machine.

Hmm... Then this call trace from bcachefs is the exception here:

bch2_journal_write=>
submit_bio=>

    ...
    md_handle_request =>
      raid5_make_request =>
        chunk_aligned_read =>
          raid5_read_one_chunk =>
Quoted 3 lines

(Not that I particularly like this wart, I'd much prefer file systems to submit a REQ_OP_FLUSH than an empty write with a preflush flag, but that's not a trivial change).

Kent, I think we need to update how bcachefs issue flushes.

Thanks,
Song

author maintainer
#4 lore raw

PATCH 2/2 md: Use op_is_flush() to check flush bio

In reply to Song Liu

op_is_flush() covers different ways to request flush. Use it instead of simply checking against REQ_PREFLUSH.

---
 drivers/md/raid0.c  | 2 +-
 drivers/md/raid1.c  | 2 +-
 drivers/md/raid10.c | 2 +-
 drivers/md/raid5.c  | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)
drivers/md/raid0.c +1 -1
index c50a7abda744..20283dc5208a 100644
@@ -592,7 +592,7 @@ static bool raid0_make_request(struct mddev *mddev, struct bio *bio)
 	unsigned chunk_sects;
 	unsigned sectors;
 
-	if (unlikely(bio->bi_opf & REQ_PREFLUSH)
+	if (unlikely(op_is_flush(bio->bi_opf))
 	    && md_flush_request(mddev, bio))
 		return true;
 
drivers/md/raid1.c +1 -1
index aaa434f0c175..5c1dadd7fbb6 100644
@@ -1581,7 +1581,7 @@ static bool raid1_make_request(struct mddev *mddev, struct bio *bio)
 {
 	sector_t sectors;
 
-	if (unlikely(bio->bi_opf & REQ_PREFLUSH)
+	if (unlikely(op_is_flush(bio->bi_opf))
 	    && md_flush_request(mddev, bio))
 		return true;
 
drivers/md/raid10.c +1 -1
index 7412066ea22c..5c6e0a8635f2 100644
@@ -1857,7 +1857,7 @@ static bool raid10_make_request(struct mddev *mddev, struct bio *bio)
 	int chunk_sects = chunk_mask + 1;
 	int sectors = bio_sectors(bio);
 
-	if (unlikely(bio->bi_opf & REQ_PREFLUSH)
+	if (unlikely(op_is_flush(bio->bi_opf))
 	    && md_flush_request(mddev, bio))
 		return true;
 
drivers/md/raid5.c +1 -1
index e57deb1c6138..1bcf96b490a7 100644
@@ -6070,7 +6070,7 @@ static bool raid5_make_request(struct mddev *mddev, struct bio * bi)
 	enum stripe_result res;
 	int s, stripe_cnt;
 
-	if (unlikely(bi->bi_opf & REQ_PREFLUSH)) {
+	if (unlikely(op_is_flush(bi->bi_opf))) {
 		int ret = log_handle_flush_request(conf, bi);
 
 		if (ret == 0)
Signature · 1 line
2.34.1
#5 lore raw
In reply to Song Liu
Quoted 24 lines · Song Liu

External email : Please do not click links or open attachments until you have verified the sender or the content.
A recent bug report [1] shows md is handling a flush from bcachefs
as read:

bch2_journal_write=>
submit_bio=>

    ...
    md_handle_request =>
      raid5_make_request =>
        chunk_aligned_read =>
          raid5_read_one_chunk =>
	    ...

It appears md code only checks REQ_PREFLUSH for flush requests, which doesn't cover all cases. OTOH, op_is_flush() doesn't check REQ_OP_FLUSH
either.

Fix this by:
1) Check REQ_PREFLUSH in op_is_flush();
2) Use op_is_flush() in md code.

Quoted 6 lines

Thanks,
Song

[1]
https://urldefense.com/v3/__https://bugzilla.kernel.org/show_bug.cgi?id=218184__;!!CTRNKA9wMg0ARbw!gQbjtS_f5d3Du2prpIT8zUM4mkZf7qDleyaAuEfG8j5tMrDvw7cfJUB04VWl0uVAL4BJ4YWbVopp$

REQ_OP_FLUSH is only used by the block layer's flush code, and the filesystem should use REQ_PREFLUSH with an empty write bio.

If we want upper layer to be able to directly send REQ_OP_FLUSH bio, then we should retrieve all REQ_PREFLUSH to confirm. At least for now, it seems that REQ_OP_FLUSH without REQ_PREFLUSH in blk_flush_policy will directly return 0 and no flush operation will be sent to the driver.

Quoted 15 lines

Song Liu (2):
block: Check REQ_OP_FLUSH in op_is_flush()
md: Use op_is_flush() to check flush bio

 drivers/md/raid0.c        | 2 +-
 drivers/md/raid1.c        | 2 +-
 drivers/md/raid10.c       | 2 +-
 drivers/md/raid5.c        | 2 +-
 include/linux/blk_types.h | 3 ++-
 5 files changed, 6 insertions(+), 5 deletions(-)

--
2.34.1

#6 lore raw
Quoted 32 lines · Ed Tsai (蔡宗軒)
Quoted 24 lines · Song Liu

External email : Please do not click links or open attachments until you have verified the sender or the content.
A recent bug report [1] shows md is handling a flush from bcachefs
as read:

bch2_journal_write=>
submit_bio=>

    ...
    md_handle_request =>
      raid5_make_request =>
        chunk_aligned_read =>
          raid5_read_one_chunk =>
	    ...

It appears md code only checks REQ_PREFLUSH for flush requests, which doesn't cover all cases. OTOH, op_is_flush() doesn't check REQ_OP_FLUSH
either.

Fix this by:
1) Check REQ_PREFLUSH in op_is_flush();
2) Use op_is_flush() in md code.

Quoted 6 lines

Thanks,
Song

[1]
https://urldefense.com/v3/__https://bugzilla.kernel.org/show_bug.cgi?id=218184__;!!CTRNKA9wMg0ARbw!gQbjtS_f5d3Du2prpIT8zUM4mkZf7qDleyaAuEfG8j5tMrDvw7cfJUB04VWl0uVAL4BJ4YWbVopp$

Quoted 8 lines

REQ_OP_FLUSH is only used by the block layer's flush code, and the filesystem should use REQ_PREFLUSH with an empty write bio.

If we want upper layer to be able to directly send REQ_OP_FLUSH bio, then we should retrieve all REQ_PREFLUSH to confirm. At least for now, it seems that REQ_OP_FLUSH without REQ_PREFLUSH in blk_flush_policy will directly return 0 and no flush operation will be sent to the driver.

If that's the case, then it should be documented and there should be a WARN_ON() in generic_make_request().

Also, glancing at blk_types.h, we have the req_op and req_flag_bits both using (__force blk_opf_t), but using the same bit range - what the hell? That's seriously broken...

#7 lore raw
Quoted 41 lines · Kent Overstreet
Quoted 3 lines · Ed Tsai (蔡宗軒)
Quoted 2 lines · Song Liu

you have verified the sender or the content.
A recent bug report [1] shows md is handling a flush from

bcachefs

Quoted 11 lines
Quoted 11 lines

as read:

bch2_journal_write=>
submit_bio=>

    ...
    md_handle_request =>
      raid5_make_request =>
        chunk_aligned_read =>
          raid5_read_one_chunk =>
    ...
Quoted 1 line
Quoted 1 line

It appears md code only checks REQ_PREFLUSH for flush requests,

which

Quoted 8 lines
Quoted 8 lines

doesn't cover all cases. OTOH, op_is_flush() doesn't check REQ_OP_FLUSH
either.

Fix this by:
1) Check REQ_PREFLUSH in op_is_flush();
2) Use op_is_flush() in md code.

Quoted 5 lines
Quoted 5 lines

Thanks,
Song

[1]

https://urldefense.com/v3/__https://bugzilla.kernel.org/show_bug.cgi?id=218184__;!!CTRNKA9wMg0ARbw!gQbjtS_f5d3Du2prpIT8zUM4mkZf7qDleyaAuEfG8j5tMrDvw7cfJUB04VWl0uVAL4BJ4YWbVopp$

Quoted 6 lines
Quoted 1 line

REQ_OP_FLUSH is only used by the block layer's flush code, and the filesystem should use REQ_PREFLUSH with an empty write bio.

If we want upper layer to be able to directly send REQ_OP_FLUSH

bio,

Quoted 1 line

then we should retrieve all REQ_PREFLUSH to confirm. At least for

now,

Quoted 8 lines
Quoted 1 line

it seems that REQ_OP_FLUSH without REQ_PREFLUSH in

blk_flush_policy

Quoted 2 lines

will directly return 0 and no flush operation will be sent to the driver.

If that's the case, then it should be documented and there should be a
WARN_ON() in generic_make_request().

Please refer to the writeback_cache_control.rst. Use an empty write bio with the REQ_PREFLUSH flag for an explicit flush, or as commonly practiced by most filesystems, use blkdev_issue_flush for a pure flush.

Quoted 6 lines

Also, glancing at blk_types.h, we have the req_op and req_flag_bits both
using (__force blk_opf_t), but using the same bit range - what the hell?
That's seriously broken...

No, read the comment before req_op. We do not need to use the entire 32 bits to represent OP; only 8 bits for OP, while the remaning 24 bits is used for FLAG.

#8 lore raw
Quoted 47 lines · Ed Tsai (蔡宗軒)
Quoted 46 lines · Kent Overstreet
Quoted 3 lines · Ed Tsai (蔡宗軒)
Quoted 2 lines · Song Liu

you have verified the sender or the content.
A recent bug report [1] shows md is handling a flush from

bcachefs

Quoted 11 lines
Quoted 11 lines

as read:

bch2_journal_write=>
submit_bio=>

    ...
    md_handle_request =>
      raid5_make_request =>
        chunk_aligned_read =>
          raid5_read_one_chunk =>
    ...
Quoted 1 line
Quoted 1 line

It appears md code only checks REQ_PREFLUSH for flush requests,

which

Quoted 8 lines
Quoted 8 lines

doesn't cover all cases. OTOH, op_is_flush() doesn't check REQ_OP_FLUSH
either.

Fix this by:
1) Check REQ_PREFLUSH in op_is_flush();
2) Use op_is_flush() in md code.

Quoted 5 lines
Quoted 5 lines

Thanks,
Song

[1]

https://urldefense.com/v3/__https://bugzilla.kernel.org/show_bug.cgi?id=218184__;!!CTRNKA9wMg0ARbw!gQbjtS_f5d3Du2prpIT8zUM4mkZf7qDleyaAuEfG8j5tMrDvw7cfJUB04VWl0uVAL4BJ4YWbVopp$

Quoted 6 lines
Quoted 1 line

REQ_OP_FLUSH is only used by the block layer's flush code, and the filesystem should use REQ_PREFLUSH with an empty write bio.

If we want upper layer to be able to directly send REQ_OP_FLUSH

bio,

Quoted 1 line

then we should retrieve all REQ_PREFLUSH to confirm. At least for

now,

Quoted 1 line

it seems that REQ_OP_FLUSH without REQ_PREFLUSH in

blk_flush_policy

Quoted 2 lines

will directly return 0 and no flush operation will be sent to the driver.

Quoted 7 lines
Quoted 3 lines

If that's the case, then it should be documented and there should be a
WARN_ON() in generic_make_request().

Please refer to the writeback_cache_control.rst. Use an empty write bio with the REQ_PREFLUSH flag for an explicit flush, or as commonly practiced by most filesystems, use blkdev_issue_flush for a pure flush.

That's not a substitute for a proper comment in the code.

Quoted 11 lines
Quoted 7 lines

Also, glancing at blk_types.h, we have the req_op and req_flag_bits both
using (__force blk_opf_t), but using the same bit range - what the hell?
That's seriously broken...

No, read the comment before req_op. We do not need to use the entire 32 bits to represent OP; only 8 bits for OP, while the remaning 24 bits is used for FLAG.

No, this is just broken; it's using the same bitwise enum for two different enums.

bitwise exists for a reason - C enums are not natively type safe, and mixing up enums/bitflags and using them in the wrong context is a serious source of bugs. If it would be incorrect to or the two different flags together, you can't use the same bitwise type.