block, md: Better handle REQ_OP_FLUSH
Subsystems (2)
Song Liu
maintainercover 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
Song Liu
maintainerPATCH 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
@@ -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
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).
Song Liu
maintainerRe: [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_FLUSHis 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_FLUSHthan 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
Song Liu
maintainerPATCH 2/2 md: Use op_is_flush() to check flush bio
In reply to Song Liuop_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
@@ -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
@@ -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
@@ -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
@@ -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
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_PREFLUSHfor flush requests, which doesn't cover all cases. OTOH,op_is_flush()doesn't checkREQ_OP_FLUSH
either.Fix this by:
1) CheckREQ_PREFLUSHinop_is_flush();
2) Useop_is_flush()in md code.
Quoted 6 lines
Thanks,
Song
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: CheckREQ_OP_FLUSHinop_is_flush()
md: Useop_is_flush()to check flush biodrivers/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
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_PREFLUSHfor flush requests, which doesn't cover all cases. OTOH,op_is_flush()doesn't checkREQ_OP_FLUSH
either.Fix this by:
1) CheckREQ_PREFLUSHinop_is_flush();
2) Useop_is_flush()in md code.
Quoted 8 lines
REQ_OP_FLUSHis only used by the block layer's flush code, and the filesystem should useREQ_PREFLUSHwith an empty write bio.If we want upper layer to be able to directly send
REQ_OP_FLUSHbio, then we should retrieve allREQ_PREFLUSHto confirm. At least for now, it seems thatREQ_OP_FLUSHwithoutREQ_PREFLUSHinblk_flush_policywill 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...
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 frombcachefs
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_PREFLUSHfor flush requests,which
Quoted 8 lines
Quoted 8 lines
doesn't cover all cases. OTOH,
op_is_flush()doesn't checkREQ_OP_FLUSH
either.Fix this by:
1) CheckREQ_PREFLUSHinop_is_flush();
2) Useop_is_flush()in md code.Quoted 5 lines
Quoted 5 lines
Thanks,
Song[1]
Quoted 6 lines
Quoted 1 line
REQ_OP_FLUSHis only used by the block layer's flush code, and the filesystem should useREQ_PREFLUSHwith an empty write bio.If we want upper layer to be able to directly send
REQ_OP_FLUSHbio,
Quoted 1 line
then we should retrieve all
REQ_PREFLUSHto confirm. At least fornow,
Quoted 8 lines
Quoted 1 line
it seems that
REQ_OP_FLUSHwithoutREQ_PREFLUSHin
blk_flush_policyQuoted 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()ingeneric_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 thereq_opandreq_flag_bitsboth
using (__forceblk_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.
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 frombcachefs
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_PREFLUSHfor flush requests,which
Quoted 8 lines
Quoted 8 lines
doesn't cover all cases. OTOH,
op_is_flush()doesn't checkREQ_OP_FLUSH
either.Fix this by:
1) CheckREQ_PREFLUSHinop_is_flush();
2) Useop_is_flush()in md code.Quoted 5 lines
Quoted 5 lines
Thanks,
Song[1]
Quoted 6 lines
Quoted 1 line
REQ_OP_FLUSHis only used by the block layer's flush code, and the filesystem should useREQ_PREFLUSHwith an empty write bio.If we want upper layer to be able to directly send
REQ_OP_FLUSHbio,
Quoted 1 line
then we should retrieve all
REQ_PREFLUSHto confirm. At least fornow,
Quoted 1 line
it seems that
REQ_OP_FLUSHwithoutREQ_PREFLUSHin
blk_flush_policyQuoted 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()ingeneric_make_request().Please refer to the
writeback_cache_control.rst. Use an empty write bio with theREQ_PREFLUSHflag for an explicit flush, or as commonly practiced by most filesystems, useblkdev_issue_flushfor 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 thereq_opandreq_flag_bitsboth
using (__forceblk_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.