[Devel] [PATCH VZ10 02/11] block/bio: add bio_issue_elapsed_ns()

Andrey Zhadchenko andrey.zhadchenko at virtuozzo.com
Mon Jul 13 03:36:35 MSK 2026


If blkcgroup is present, each bio is stamped at the issue time.
Expose this via bio_issue_elapsed_ns() so we can accurately
calculate total time spent in the block layer.

https://virtuozzo.atlassian.net/browse/VSTOR-103846
Signed-off-by: Andrey Zhadchenko <andrey.zhadchenko at virtuozzo.com>
---
 block/bio.c         | 23 +++++++++++++++++++++++
 include/linux/bio.h |  2 ++
 2 files changed, 25 insertions(+)

diff --git a/block/bio.c b/block/bio.c
index 3c0a558c90f52..c26babfc5e1ce 100644
--- a/block/bio.c
+++ b/block/bio.c
@@ -1827,6 +1827,29 @@ int bioset_init(struct bio_set *bs,
 }
 EXPORT_SYMBOL(bioset_init);
 
+/*
+ * bio_issue_elapsed_ns - calculate elapsed time since issue
+ * @bio:	bio
+ *
+ * Return the elapsed nanoseconds since bio issue until now,
+ * handling the 51-bit wraparound.
+ */
+u64 bio_issue_elapsed_ns(struct bio *bio)
+{
+#ifdef CONFIG_BLK_CGROUP
+	u64 now, elapsed;
+
+	/* Mask both operands to 51 bits before the modular subtraction. */
+	now = __bio_issue_time(blk_time_get_ns());
+	elapsed = now - bio_issue_time(&bio->bi_issue);
+
+	return elapsed & BIO_ISSUE_TIME_MASK;
+#else
+	return 0;
+#endif
+}
+EXPORT_SYMBOL_GPL(bio_issue_elapsed_ns);
+
 static int __init init_bio(void)
 {
 	int i;
diff --git a/include/linux/bio.h b/include/linux/bio.h
index 46ffac5caab78..00c116207a25a 100644
--- a/include/linux/bio.h
+++ b/include/linux/bio.h
@@ -714,4 +714,6 @@ struct bio *bio_chain_and_submit(struct bio *prev, struct bio *new);
 struct bio *blk_alloc_discard_bio(struct block_device *bdev,
 		sector_t *sector, sector_t *nr_sects, gfp_t gfp_mask);
 
+u64 bio_issue_elapsed_ns(struct bio *bio);
+
 #endif /* __LINUX_BIO_H */
-- 
2.43.5



More information about the Devel mailing list