[PATCH v2 5/5] block: CONFIG_BLK_DEBUG

From: Kent Overstreet
Date: Mon Jun 05 2023 - 17:28:13 EST


This adds a new kconfig option to enable some extra debug mode
assertions. Currently this is just used for some bio iterator
assertions, but hopefully more will be added in the future.

Signed-off-by: Kent Overstreet <kent.overstreet@xxxxxxxxx>
---
block/Kconfig | 7 +++++++
include/linux/bio.h | 5 +++++
include/linux/bvec.h | 8 ++++++++
3 files changed, 20 insertions(+)

diff --git a/block/Kconfig b/block/Kconfig
index 86122e459f..611dddb59c 100644
--- a/block/Kconfig
+++ b/block/Kconfig
@@ -165,6 +165,13 @@ config BLK_CGROUP_IOPRIO
scheduler and block devices process requests. Only some I/O schedulers
and some block devices support I/O priorities.

+config BLK_DEBUG
+ bool "Extra block layer assertions"
+ depends on DEBUG_KERNEL
+ help
+ Enable extra assertions in the block layer. Currently this is just the
+ bio iterator code, but may be expanded.
+
config BLK_DEBUG_FS
bool "Block layer debugging information in debugfs"
default y
diff --git a/include/linux/bio.h b/include/linux/bio.h
index f599bcf15e..cece57c54d 100644
--- a/include/linux/bio.h
+++ b/include/linux/bio.h
@@ -81,6 +81,8 @@ static inline void *bio_data(struct bio *bio)
static inline struct bio_vec bio_iter_all_peek(const struct bio *bio,
struct bvec_iter_all *iter)
{
+ BLK_BUG_ON(iter->idx >= bio->bi_vcnt);
+
return bvec_iter_all_peek(bio->bi_io_vec, iter);
}

@@ -89,6 +91,9 @@ static inline void bio_iter_all_advance(const struct bio *bio,
unsigned bytes)
{
bvec_iter_all_advance(bio->bi_io_vec, iter, bytes);
+
+ BLK_BUG_ON(iter->idx > bio->bi_vcnt ||
+ (iter->idx == bio->bi_vcnt && iter->done));
}

#define bio_for_each_segment_all_continue(bvl, bio, iter) \
diff --git a/include/linux/bvec.h b/include/linux/bvec.h
index ee5965fedf..2467245da2 100644
--- a/include/linux/bvec.h
+++ b/include/linux/bvec.h
@@ -16,6 +16,12 @@

struct page;

+#ifdef CONFIG_BLK_DEBUG
+#define BLK_BUG_ON(cond) BUG_ON(cond)
+#else
+#define BLK_BUG_ON(cond) BUILD_BUG_ON_INVALID(cond)
+#endif
+
/**
* struct bio_vec - a contiguous range of physical memory addresses
* @bv_page: First page associated with the address range.
@@ -210,6 +216,8 @@ static inline struct bio_vec __bvec_iter_all_peek(const struct bio_vec *bvec,
{
struct bio_vec bv = bvec[iter->idx];

+ BLK_BUG_ON(iter->done >= bv.bv_len);
+
bv.bv_offset += iter->done;
bv.bv_len -= iter->done;

--
2.40.1