[PATCH v4 12/36] bio: Add bio_for_each_thp_segment_all

From: Matthew Wilcox
Date: Fri May 15 2020 - 09:19:11 EST


From: "Matthew Wilcox (Oracle)" <willy@xxxxxxxxxxxxx>

Iterate once for each THP page instead of once for each base page.

Signed-off-by: Matthew Wilcox (Oracle) <willy@xxxxxxxxxxxxx>
---
include/linux/bio.h | 13 +++++++++++++
include/linux/bvec.h | 23 +++++++++++++++++++++++
2 files changed, 36 insertions(+)

diff --git a/include/linux/bio.h b/include/linux/bio.h
index c1c0f9ea4e63..4cc883fd8d63 100644
--- a/include/linux/bio.h
+++ b/include/linux/bio.h
@@ -131,12 +131,25 @@ static inline bool bio_next_segment(const struct bio *bio,
return true;
}

+static inline bool bio_next_thp_segment(const struct bio *bio,
+ struct bvec_iter_all *iter)
+{
+ if (iter->idx >= bio->bi_vcnt)
+ return false;
+
+ bvec_thp_advance(&bio->bi_io_vec[iter->idx], iter);
+ return true;
+}
+
/*
* drivers should _never_ use the all version - the bio may have been split
* before it got to the driver and the driver won't own all of it
*/
#define bio_for_each_segment_all(bvl, bio, iter) \
for (bvl = bvec_init_iter_all(&iter); bio_next_segment((bio), &iter); )
+#define bio_for_each_thp_segment_all(bvl, bio, iter) \
+ for (bvl = bvec_init_iter_all(&iter); \
+ bio_next_thp_segment((bio), &iter); )

static inline void bio_advance_iter(struct bio *bio, struct bvec_iter *iter,
unsigned bytes)
diff --git a/include/linux/bvec.h b/include/linux/bvec.h
index a81c13ac1972..e08bd192e0ed 100644
--- a/include/linux/bvec.h
+++ b/include/linux/bvec.h
@@ -153,4 +153,27 @@ static inline void bvec_advance(const struct bio_vec *bvec,
}
}

+static inline void bvec_thp_advance(const struct bio_vec *bvec,
+ struct bvec_iter_all *iter_all)
+{
+ struct bio_vec *bv = &iter_all->bv;
+ unsigned int page_size = thp_size(bvec->bv_page);
+
+ if (iter_all->done) {
+ bv->bv_page += hpage_nr_pages(bv->bv_page);
+ bv->bv_offset = 0;
+ } else {
+ BUG_ON(bvec->bv_offset >= page_size);
+ bv->bv_page = bvec->bv_page;
+ bv->bv_offset = bvec->bv_offset & (page_size - 1);
+ }
+ bv->bv_len = min(page_size - bv->bv_offset,
+ bvec->bv_len - iter_all->done);
+ iter_all->done += bv->bv_len;
+
+ if (iter_all->done == bvec->bv_len) {
+ iter_all->idx++;
+ iter_all->done = 0;
+ }
+}
#endif /* __LINUX_BVEC_ITER_H */
--
2.26.2