Skip to contentSkip to conversation

block: Add should_fail_bio() for bpf error injection

PATCH Superseded Howard McLauchlan 2 participants 2 messages Feb 6, 2018 Block layer

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 block: Add should_fail_bio() for bpf error injection

The classic error injection mechanism, should_fail_request() does not support use cases where more information is required (from the entire struct bio, for example).

To that end, this patch introduces should_fail_bio(), which calls should_fail_request() under the hood but provides a convenient place for kprobes to hook into if they require the entire struct bio. This patch also replaces some existing calls to should_fail_request() with should_fail_bio() with no degradation in performance.

---
V2: rebased on master for 4.15

 block/blk-core.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
block/blk-core.c +8 -1
index d0d104268f1a..560a0260a8dc 100644
@@ -34,6 +34,7 @@
 #include <linux/pm_runtime.h>
 #include <linux/blk-cgroup.h>
 #include <linux/debugfs.h>
+#include <linux/bpf.h>
 
 #define CREATE_TRACE_POINTS
 #include <trace/events/block.h>
@@ -2083,6 +2084,12 @@ static inline bool bio_check_ro(struct bio *bio, struct hd_struct *part)
 	return false;
 }
 
+static noinline bool should_fail_bio(struct bio *bio)
+{
+	return should_fail_request(&bio->bi_disk->part0, bio->bi_iter.bi_size);
+}
+BPF_ALLOW_ERROR_INJECTION(should_fail_bio);
+
 /*
  * Remap block n of partition p to block n+start(p) of the disk.
  */
@@ -2174,7 +2181,7 @@ generic_make_request_checks(struct bio *bio)
 	if ((bio->bi_opf & REQ_NOWAIT) && !queue_is_rq_based(q))
 		goto not_supported;
 
-	if (should_fail_request(&bio->bi_disk->part0, bio->bi_iter.bi_size))
+	if (should_fail_bio(bio))
 		goto end_io;
 
 	if (!bio->bi_partno) {
Signature · 1 line
2.14.1
maintainer
#1 lore raw
Quoted 9 lines · Howard McLauchlan

The classic error injection mechanism, should_fail_request() does not support use cases where more information is required (from the entire struct bio, for example).

To that end, this patch introduces should_fail_bio(), which calls should_fail_request() under the hood but provides a convenient place for kprobes to hook into if they require the entire struct bio. This patch also replaces some existing calls to should_fail_request() with should_fail_bio() with no degradation in performance.

It fails:

block/blk-core.c:2091:1: warning: data definition has no type or storage class
BPF_ALLOW_ERROR_INJECTION(should_fail_bio);
^~~~~~~~~~~~~~~~~~~~~~~~~
block/blk-core.c:2091:1: error: type defaults to ‘int’ in declaration of ‘BPF_ALLOW_ERROR_INJECTION’ [-Werror=implicit-int] block/blk-core.c:2091:1: warning: parameter names (without types) in function declaration
cc1: some warnings being treated as errors

Signature · 1 line
Jens Axboe