[PATCH 06/15] block: blk-merge: fix to add the number of integrity segments to the request twice

Jinyoung CHOI
2023-05-03T10:12:17Z

blk_integrity_merge_bio() not only performs conditional tests, but also
updates the integrity segment information of request.
It can be called twice when merging the bio into an existing request.

bio_attempt_bio_merge() or blk_mq_sched_try_merge()
blk_rq_merge_ok()
blk_integrity_merge_bio() - 1
bio_attemp_{back|front}_merge()
ll_{back|front}_merge_fn()
ll_new_hw_segments()
blk_integrity_merge_bio() - 2

The part of checking the conditions and the code to update the
information of the actual request were separated. At this time, the
ll_back_merge_fn was called by passth-path, so the condition check was
called by all the separated functions.

And after success in blk_integrity_merge_bio(), the information of the
request may be wrong if it is impossible to merge due to other
conditional tests. Thus, it was changed to be called immediately before
merging the bio's segments.

Cc: Christoph Hellwig <hch@lst.de>
Cc: Martin K. Petersen <martin.petersen@oracle.com>

Signed-off-by: Jinyoung Choi <j-young.choi@samsung.com>
---
block/blk-integrity.c | 34 +++++++++++++++++++++++++++++-----
block/blk-merge.c | 9 +++++----
block/blk.h | 7 +++++++
3 files changed, 41 insertions(+), 9 deletions(-)

diff --git a/block/blk-integrity.c b/block/blk-integrity.c
index d4e9b4556d14..03a85e1f6d2e 100644
--- a/block/blk-integrity.c
+++ b/block/blk-integrity.c
@@ -184,19 +184,43 @@ bool blk_integrity_merge_rq(struct request_queue *q, struct request *req,
return true;
}

+static inline bool blk_integrity_bypass_check(struct request *req,
+ struct bio *bio)
+{
+ return blk_integrity_rq(req) == 0 && bio_integrity(bio) == NULL;
+}
+
+static bool __blk_integrity_mergeable(struct request_queue *q,
+ struct request *req, struct bio *bio)
+{
+ if (blk_integrity_rq(req) == 0 || bio_integrity(bio) == NULL)
+ return false;
+
+ if (bio_integrity(req->bio)->bip_flags != bio_integrity(bio)->bip_flags)
+ return false;
+
+ return true;
+}
+
+bool blk_integrity_mergeable(struct request_queue *q, struct request *req,
+ struct bio *bio)
+{
+ if (blk_integrity_bypass_check(req, bio))
+ return true;
+
+ return __blk_integrity_mergeable(q, req, bio);
+}
+
bool blk_integrity_merge_bio(struct request_queue *q, struct request *req,
struct bio *bio)
{
int nr_integrity_segs;
struct bio *next = bio->bi_next;

- if (blk_integrity_rq(req) == 0 && bio_integrity(bio) == NULL)
+ if (blk_integrity_bypass_check(req, bio))
return true;

- if (blk_integrity_rq(req) == 0 || bio_integrity(bio) == NULL)
- return false;
-
- if (bio_integrity(req->bio)->bip_flags != bio_integrity(bio)->bip_flags)
+ if (!__blk_integrity_mergeable(q, req, bio))
return false;

bio->bi_next = NULL;
diff --git a/block/blk-merge.c b/block/blk-merge.c
index 65e75efa9bd3..8509f468d6d4 100644
--- a/block/blk-merge.c
+++ b/block/blk-merge.c
@@ -611,9 +611,6 @@ static inline int ll_new_hw_segment(struct request *req, struct bio *bio,
if (!blk_cgroup_mergeable(req, bio))
goto no_merge;

- if (blk_integrity_merge_bio(req->q, req, bio) == false)
- goto no_merge;
-
/* discard request merge won't add new segment */
if (req_op(req) == REQ_OP_DISCARD)
return 1;
@@ -621,6 +618,10 @@ static inline int ll_new_hw_segment(struct request *req, struct bio *bio,
if (req->nr_phys_segments + nr_phys_segs > blk_rq_get_max_segments(req))
goto no_merge;

+ /* This will merge integrity segments */
+ if (!blk_integrity_merge_bio(req->q, req, bio))
+ goto no_merge;
+
/*
* This will form the start of a new hw segment. Bump both
* counters.
@@ -934,7 +935,7 @@ bool blk_rq_merge_ok(struct request *rq, struct bio *bio)
return false;

/* only merge integrity protected bio into ditto rq */
- if (blk_integrity_merge_bio(rq->q, rq, bio) == false)
+ if (!blk_integrity_mergeable(rq->q, rq, bio))
return false;

/* Only merge if the crypt contexts are compatible */
diff --git a/block/blk.h b/block/blk.h
index 45547bcf1119..5923d2190d91 100644
--- a/block/blk.h
+++ b/block/blk.h
@@ -189,6 +189,8 @@ static inline bool bio_integrity_endio(struct bio *bio)

bool blk_integrity_merge_rq(struct request_queue *, struct request *,
struct request *);
+bool blk_integrity_mergeable(struct request_queue *rq, struct request *r,
+ struct bio *b);
bool blk_integrity_merge_bio(struct request_queue *, struct request *,
struct bio *);

@@ -221,6 +223,11 @@ static inline bool blk_integrity_merge_rq(struct request_queue *rq,
{
return true;
}
+static inline bool blk_integrity_mergeable(struct request_queue *rq,
+ struct request *r, struct bio *b)
+{
+ return true;
+}
static inline bool blk_integrity_merge_bio(struct request_queue *rq,
struct request *r, struct bio *b)
{
--
2.34.1