[PATCH 02/15] block: blk-integiry: add helper functions for bio_integrity_add_page

From: Jinyoung CHOI
Date: Wed May 03 2023 - 06:02:30 EST


Add functions that use the hardware limit to merge the page with
integrity's bio_vec.
Using the added functions, the physically continuous pages are made of
one bio_vec.

Previously, physically continuous pages were not created as one
bio_vec, but separately created.

Cc: Christoph Hellwig <hch@xxxxxx>
Cc: Martin K. Petersen <martin.petersen@xxxxxxxxxx>

Signed-off-by: Jinyoung Choi <j-young.choi@xxxxxxxxxxx>
---
block/bio-integrity.c | 58 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 58 insertions(+)

diff --git a/block/bio-integrity.c b/block/bio-integrity.c
index 4533eb491661..06b6a2c178d2 100644
--- a/block/bio-integrity.c
+++ b/block/bio-integrity.c
@@ -111,6 +111,64 @@ void bio_integrity_free(struct bio *bio)
bio->bi_opf &= ~REQ_INTEGRITY;
}

+/**
+ * bip_full - check if the bip is full
+ * @bip: bip to check
+ * @len: length of one segment to be added
+ *
+ * Return true if @bip is full and one segment with @len bytes can't be
+ * added to the bip, otherwise return false
+ */
+static inline bool bip_full(struct bio_integrity_payload *bip, unsigned int len)
+{
+ if (bip->bip_vcnt >= bip->bip_max_vcnt)
+ return true;
+ if (bip->bip_iter.bi_size > UINT_MAX - len)
+ return true;
+ return false;
+}
+
+/**
+ * bip_try_merge_hw_seg - try to merge a page into a segment, while
+ * obeying the hardware segment size limit
+ * @q: the target queue
+ * @bip: bip to check
+ * @page: page containing integrity metadata
+ * @len: number of bytes of integrity metadata in page
+ * @offset: start offset within page
+ * @same_page: return if the segment has been merged inside the same page
+ *
+ * Return true if @page is merged to @bip, otherwise return false
+ */
+static bool bip_try_merge_hw_seg(struct request_queue *q,
+ struct bio_integrity_payload *bip,
+ struct page *page, unsigned int len,
+ unsigned int offset, bool *same_page)
+{
+ struct bio_vec *bv = &bip->bip_vec[bip->bip_vcnt - 1];
+ unsigned long mask = queue_segment_boundary(q);
+ phys_addr_t addr1 = page_to_phys(bv->bv_page) + bv->bv_offset;
+ phys_addr_t addr2 = page_to_phys(page) + offset + len - 1;
+
+ if ((addr1 | mask) != (addr2 | mask))
+ return false;
+ if (bv->bv_len + len > queue_max_segment_size(q))
+ return false;
+
+ if (bip->bip_vcnt > 0) {
+ if (bio_page_is_mergeable(bv, page, len, offset, same_page)) {
+ if (bip->bip_iter.bi_size > UINT_MAX - len) {
+ *same_page = false;
+ return false;
+ }
+ bv->bv_len += len;
+ bip->bip_iter.bi_size += len;
+ return true;
+ }
+ }
+ return false;
+}
+
/**
* bio_integrity_add_page - Attach integrity metadata
* @bio: bio to update
--
2.34.1