Blob Blame History Raw
From 50480835adf15a389267393674504551b68987a2 Mon Sep 17 00:00:00 2001
From: xingfeng2510 <xingfeng25100@163.com>
Date: Wed, 30 Mar 2022 16:10:51 +0800
Subject: [PATCH 2/2] libbpf-tools: Fix dropped request->rq_disk for kernel
 5.17+

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
---
 libbpf-tools/biolatency.bpf.c | 20 ++++++++++++++++++--
 libbpf-tools/biosnoop.bpf.c   | 12 +++++++++++-
 libbpf-tools/biostacks.bpf.c  | 12 +++++++++++-
 libbpf-tools/bitesize.bpf.c   | 12 +++++++++++-
 4 files changed, 51 insertions(+), 5 deletions(-)

diff --git a/libbpf-tools/biolatency.bpf.c b/libbpf-tools/biolatency.bpf.c
index 648dda78..8f325046 100644
--- a/libbpf-tools/biolatency.bpf.c
+++ b/libbpf-tools/biolatency.bpf.c
@@ -19,6 +19,10 @@ const volatile bool targ_ms = false;
 const volatile bool filter_dev = false;
 const volatile __u32 targ_dev = 0;
 
+struct request_queue___x {
+	struct gendisk *disk;
+} __attribute__((preserve_access_index));
+
 struct {
 	__uint(type, BPF_MAP_TYPE_CGROUP_ARRAY);
 	__type(key, u32);
@@ -53,9 +57,15 @@ int trace_rq_start(struct request *rq, int issue)
 	u64 ts = bpf_ktime_get_ns();
 
 	if (filter_dev) {
-		struct gendisk *disk = BPF_CORE_READ(rq, rq_disk);
+		struct request_queue___x *q = (void *)BPF_CORE_READ(rq, q);
+		struct gendisk *disk;
 		u32 dev;
 
+		if (bpf_core_field_exists(q->disk))
+			disk = BPF_CORE_READ(q, disk);
+		else
+			disk = BPF_CORE_READ(rq, rq_disk);
+
 		dev = disk ? MKDEV(BPF_CORE_READ(disk, major),
 				BPF_CORE_READ(disk, first_minor)) : 0;
 		if (targ_dev != dev)
@@ -119,7 +129,13 @@ int BPF_PROG(block_rq_complete, struct request *rq, int error,
 		goto cleanup;
 
 	if (targ_per_disk) {
-		struct gendisk *disk = BPF_CORE_READ(rq, rq_disk);
+		struct request_queue___x *q = (void *)BPF_CORE_READ(rq, q);
+		struct gendisk *disk;
+
+		if (bpf_core_field_exists(q->disk))
+			disk = BPF_CORE_READ(q, disk);
+		else
+			disk = BPF_CORE_READ(rq, rq_disk);
 
 		hkey.dev = disk ? MKDEV(BPF_CORE_READ(disk, major),
 					BPF_CORE_READ(disk, first_minor)) : 0;
diff --git a/libbpf-tools/biosnoop.bpf.c b/libbpf-tools/biosnoop.bpf.c
index 54226e43..05903473 100644
--- a/libbpf-tools/biosnoop.bpf.c
+++ b/libbpf-tools/biosnoop.bpf.c
@@ -15,6 +15,10 @@ const volatile __u32 targ_dev = 0;
 
 extern __u32 LINUX_KERNEL_VERSION __kconfig;
 
+struct request_queue___x {
+	struct gendisk *disk;
+} __attribute__((preserve_access_index));
+
 struct {
 	__uint(type, BPF_MAP_TYPE_CGROUP_ARRAY);
 	__type(key, u32);
@@ -92,7 +96,13 @@ int trace_rq_start(struct request *rq, bool insert)
 
 	stagep = bpf_map_lookup_elem(&start, &rq);
 	if (!stagep) {
-		struct gendisk *disk = BPF_CORE_READ(rq, rq_disk);
+		struct request_queue___x *q = (void *)BPF_CORE_READ(rq, q);
+		struct gendisk *disk;
+
+		if (bpf_core_field_exists(q->disk))
+			disk = BPF_CORE_READ(q, disk);
+		else
+			disk = BPF_CORE_READ(rq, rq_disk);
 
 		stage.dev = disk ? MKDEV(BPF_CORE_READ(disk, major),
 				BPF_CORE_READ(disk, first_minor)) : 0;
diff --git a/libbpf-tools/biostacks.bpf.c b/libbpf-tools/biostacks.bpf.c
index 01993737..c13975fa 100644
--- a/libbpf-tools/biostacks.bpf.c
+++ b/libbpf-tools/biostacks.bpf.c
@@ -14,6 +14,10 @@ const volatile bool targ_ms = false;
 const volatile bool filter_dev = false;
 const volatile __u32 targ_dev = -1;
 
+struct request_queue___x {
+	struct gendisk *disk;
+} __attribute__((preserve_access_index));
+
 struct internal_rqinfo {
 	u64 start_ts;
 	struct rqinfo rqinfo;
@@ -41,9 +45,15 @@ static __always_inline
 int trace_start(void *ctx, struct request *rq, bool merge_bio)
 {
 	struct internal_rqinfo *i_rqinfop = NULL, i_rqinfo = {};
-	struct gendisk *disk = BPF_CORE_READ(rq, rq_disk);
+	struct request_queue___x *q = (void *)BPF_CORE_READ(rq, q);
+	struct gendisk *disk;
 	u32 dev;
 
+	if (bpf_core_field_exists(q->disk))
+		disk = BPF_CORE_READ(q, disk);
+	else
+		disk = BPF_CORE_READ(rq, rq_disk);
+
 	dev = disk ? MKDEV(BPF_CORE_READ(disk, major),
 			BPF_CORE_READ(disk, first_minor)) : 0;
 	if (filter_dev && targ_dev != dev)
diff --git a/libbpf-tools/bitesize.bpf.c b/libbpf-tools/bitesize.bpf.c
index 80672c9b..5066ca33 100644
--- a/libbpf-tools/bitesize.bpf.c
+++ b/libbpf-tools/bitesize.bpf.c
@@ -13,6 +13,10 @@ const volatile __u32 targ_dev = 0;
 
 extern __u32 LINUX_KERNEL_VERSION __kconfig;
 
+struct request_queue___x {
+	struct gendisk *disk;
+} __attribute__((preserve_access_index));
+
 struct {
 	__uint(type, BPF_MAP_TYPE_HASH);
 	__uint(max_entries, 10240);
@@ -41,9 +45,15 @@ static int trace_rq_issue(struct request *rq)
 	u64 slot;
 
 	if (filter_dev) {
-		struct gendisk *disk = BPF_CORE_READ(rq, rq_disk);
+		struct request_queue___x *q = (void *)BPF_CORE_READ(rq, q);
+		struct gendisk *disk;
 		u32 dev;
 
+		if (bpf_core_field_exists(q->disk))
+			disk = BPF_CORE_READ(q, disk);
+		else
+			disk = BPF_CORE_READ(rq, rq_disk);
+
 		dev = disk ? MKDEV(BPF_CORE_READ(disk, major),
 				BPF_CORE_READ(disk, first_minor)) : 0;
 		if (targ_dev != dev)
-- 
2.35.1