Blame SOURCES/0010-Enhance-dev-d-D-options-to-support-blk-mq-sbitmap.patch

d67611
From 7095c8fd029e3a33117e3b67de73f504686ebfe2 Mon Sep 17 00:00:00 2001
d67611
From: Lianbo Jiang <lijiang@redhat.com>
d67611
Date: Thu, 2 Jun 2022 20:12:55 +0800
d67611
Subject: [PATCH 10/18] Enhance "dev -d|-D" options to support blk-mq sbitmap
d67611
d67611
Since Linux 5.16-rc1, which kernel commit 9a14d6ce4135 ("block: remove
d67611
debugfs blk_mq_ctx dispatched/merged/completed attributes") removed the
d67611
members from struct blk_mq_ctx, crash has not displayed disk I/O statistics
d67611
for multiqueue (blk-mq) devices.
d67611
d67611
Let's parse the sbitmap in blk-mq layer to support it.
d67611
d67611
Signed-off-by: Lianbo Jiang <lijiang@redhat.com>
d67611
Signed-off-by: Kazuhito Hagio <k-hagio-ab@nec.com>
d67611
---
d67611
 defs.h    |  11 +++
d67611
 dev.c     | 244 +++++++++++++++++++++++++++++++++++++++++++++---------
d67611
 symbols.c |  22 +++++
d67611
 3 files changed, 238 insertions(+), 39 deletions(-)
d67611
d67611
diff --git a/defs.h b/defs.h
d67611
index c8444b4e54eb..2681586a33dc 100644
d67611
--- a/defs.h
d67611
+++ b/defs.h
d67611
@@ -2170,6 +2170,16 @@ struct offset_table {                    /* stash of commonly-used offsets */
d67611
 	long sbq_wait_state_wait;
d67611
 	long sbitmap_alloc_hint;
d67611
 	long sbitmap_round_robin;
d67611
+	long request_cmd_flags;
d67611
+	long request_q;
d67611
+	long request_state;
d67611
+	long request_queue_queue_hw_ctx;
d67611
+	long request_queue_nr_hw_queues;
d67611
+	long blk_mq_hw_ctx_tags;
d67611
+	long blk_mq_tags_bitmap_tags;
d67611
+	long blk_mq_tags_breserved_tags;
d67611
+	long blk_mq_tags_nr_reserved_tags;
d67611
+	long blk_mq_tags_rqs;
d67611
 };
d67611
 
d67611
 struct size_table {         /* stash of commonly-used sizes */
d67611
@@ -2339,6 +2349,7 @@ struct size_table {         /* stash of commonly-used sizes */
d67611
 	long sbitmap;
d67611
 	long sbitmap_queue;
d67611
 	long sbq_wait_state;
d67611
+	long blk_mq_tags;
d67611
 };
d67611
 
d67611
 struct array_table {
d67611
diff --git a/dev.c b/dev.c
d67611
index a493e51ac95c..4be4c96df8b0 100644
d67611
--- a/dev.c
d67611
+++ b/dev.c
d67611
@@ -4238,19 +4238,176 @@ get_one_mctx_diskio(unsigned long mctx, struct diskio *io)
d67611
 	io->write = (dispatch[1] - comp[1]);
d67611
 }
d67611
 
d67611
+typedef bool (busy_tag_iter_fn)(ulong rq, void *data);
d67611
+
d67611
+struct mq_inflight {
d67611
+	ulong q;
d67611
+	struct diskio *dio;
d67611
+};
d67611
+
d67611
+struct bt_iter_data {
d67611
+	ulong tags;
d67611
+	uint reserved;
d67611
+	uint nr_reserved_tags;
d67611
+	busy_tag_iter_fn *fn;
d67611
+	void *data;
d67611
+};
d67611
+
d67611
+/*
d67611
+ * See the include/linux/blk_types.h and include/linux/blk-mq.h
d67611
+ */
d67611
+#define MQ_RQ_IN_FLIGHT 1
d67611
+#define REQ_OP_BITS     8
d67611
+#define REQ_OP_MASK     ((1 << REQ_OP_BITS) - 1)
d67611
+
d67611
+static uint op_is_write(uint op)
d67611
+{
d67611
+	return (op & REQ_OP_MASK) & 1;
d67611
+}
d67611
+
d67611
+static bool mq_check_inflight(ulong rq, void *data)
d67611
+{
d67611
+	uint cmd_flags = 0, state = 0;
d67611
+	ulong addr = 0, queue = 0;
d67611
+	struct mq_inflight *mi = data;
d67611
+
d67611
+	if (!IS_KVADDR(rq))
d67611
+		return TRUE;
d67611
+
d67611
+	addr = rq + OFFSET(request_q);
d67611
+	if (!readmem(addr, KVADDR, &queue, sizeof(ulong), "request.q", RETURN_ON_ERROR))
d67611
+		return FALSE;
d67611
+
d67611
+	addr = rq + OFFSET(request_cmd_flags);
d67611
+	if (!readmem(addr, KVADDR, &cmd_flags, sizeof(uint), "request.cmd_flags", RETURN_ON_ERROR))
d67611
+		return FALSE;
d67611
+
d67611
+	addr = rq + OFFSET(request_state);
d67611
+	if (!readmem(addr, KVADDR, &state, sizeof(uint), "request.state", RETURN_ON_ERROR))
d67611
+		return FALSE;
d67611
+
d67611
+	if (queue == mi->q && state == MQ_RQ_IN_FLIGHT) {
d67611
+		if (op_is_write(cmd_flags))
d67611
+			mi->dio->write++;
d67611
+		else
d67611
+			mi->dio->read++;
d67611
+	}
d67611
+
d67611
+	return TRUE;
d67611
+}
d67611
+
d67611
+static bool bt_iter(uint bitnr, void *data)
d67611
+{
d67611
+	ulong addr = 0, rqs_addr = 0, rq = 0;
d67611
+	struct bt_iter_data *iter_data = data;
d67611
+	ulong tag = iter_data->tags;
d67611
+
d67611
+	if (!iter_data->reserved)
d67611
+		bitnr += iter_data->nr_reserved_tags;
d67611
+
d67611
+	/* rqs */
d67611
+	addr = tag + OFFSET(blk_mq_tags_rqs);
d67611
+	if (!readmem(addr, KVADDR, &rqs_addr, sizeof(void *), "blk_mq_tags.rqs", RETURN_ON_ERROR))
d67611
+		return FALSE;
d67611
+
d67611
+	addr = rqs_addr + bitnr * sizeof(ulong); /* rqs[bitnr] */
d67611
+	if (!readmem(addr, KVADDR, &rq, sizeof(ulong), "blk_mq_tags.rqs[]", RETURN_ON_ERROR))
d67611
+		return FALSE;
d67611
+
d67611
+	return iter_data->fn(rq, iter_data->data);
d67611
+}
d67611
+
d67611
+static void bt_for_each(ulong q, ulong tags, ulong sbq, uint reserved, uint nr_resvd_tags, struct diskio *dio)
d67611
+{
d67611
+	struct sbitmap_context sc = {0};
d67611
+	struct mq_inflight mi = {
d67611
+		.q = q,
d67611
+		.dio = dio,
d67611
+	};
d67611
+	struct bt_iter_data iter_data = {
d67611
+		.tags = tags,
d67611
+		.reserved = reserved,
d67611
+		.nr_reserved_tags = nr_resvd_tags,
d67611
+		.fn = mq_check_inflight,
d67611
+		.data = &mi,
d67611
+	};
d67611
+
d67611
+	sbitmap_context_load(sbq + OFFSET(sbitmap_queue_sb), &sc);
d67611
+	sbitmap_for_each_set(&sc, bt_iter, &iter_data);
d67611
+}
d67611
+
d67611
+static void queue_for_each_hw_ctx(ulong q, ulong *hctx, uint cnt, struct diskio *dio)
d67611
+{
d67611
+	uint i;
d67611
+
d67611
+	for (i = 0; i < cnt; i++) {
d67611
+		ulong addr = 0, tags = 0;
d67611
+		uint nr_reserved_tags = 0;
d67611
+
d67611
+		/* Tags owned by the block driver */
d67611
+		addr = hctx[i] + OFFSET(blk_mq_hw_ctx_tags);
d67611
+		if (!readmem(addr, KVADDR, &tags, sizeof(ulong),
d67611
+				"blk_mq_hw_ctx.tags", RETURN_ON_ERROR))
d67611
+			break;
d67611
+
d67611
+		addr = tags + OFFSET(blk_mq_tags_nr_reserved_tags);
d67611
+		if (!readmem(addr, KVADDR, &nr_reserved_tags, sizeof(uint),
d67611
+				"blk_mq_tags_nr_reserved_tags", RETURN_ON_ERROR))
d67611
+			break;
d67611
+
d67611
+		if (nr_reserved_tags) {
d67611
+			addr = tags + OFFSET(blk_mq_tags_breserved_tags);
d67611
+			bt_for_each(q, tags, addr, 1, nr_reserved_tags, dio);
d67611
+		}
d67611
+		addr = tags + OFFSET(blk_mq_tags_bitmap_tags);
d67611
+		bt_for_each(q, tags, addr, 0, nr_reserved_tags, dio);
d67611
+	}
d67611
+}
d67611
+
d67611
+static void get_mq_diskio_from_hw_queues(ulong q, struct diskio *dio)
d67611
+{
d67611
+	uint cnt = 0;
d67611
+	ulong addr = 0, hctx_addr = 0;
d67611
+	ulong *hctx_array = NULL;
d67611
+
d67611
+	addr = q + OFFSET(request_queue_nr_hw_queues);
d67611
+	readmem(addr, KVADDR, &cnt, sizeof(uint),
d67611
+		"request_queue.nr_hw_queues", FAULT_ON_ERROR);
d67611
+
d67611
+	addr = q + OFFSET(request_queue_queue_hw_ctx);
d67611
+	readmem(addr, KVADDR, &hctx_addr, sizeof(void *),
d67611
+		"request_queue.queue_hw_ctx", FAULT_ON_ERROR);
d67611
+
d67611
+	hctx_array = (ulong *)GETBUF(sizeof(void *) * cnt);
d67611
+	if (!hctx_array)
d67611
+		error(FATAL, "fail to get memory for the hctx_array\n");
d67611
+
d67611
+	if (!readmem(hctx_addr, KVADDR, hctx_array, sizeof(void *) * cnt,
d67611
+			"request_queue.queue_hw_ctx[]", RETURN_ON_ERROR)) {
d67611
+		FREEBUF(hctx_array);
d67611
+		return;
d67611
+	}
d67611
+
d67611
+	queue_for_each_hw_ctx(q, hctx_array, cnt, dio);
d67611
+
d67611
+	FREEBUF(hctx_array);
d67611
+}
d67611
+
d67611
 static void
d67611
 get_mq_diskio(unsigned long q, unsigned long *mq_count)
d67611
 {
d67611
 	int cpu;
d67611
 	unsigned long queue_ctx;
d67611
 	unsigned long mctx_addr;
d67611
-	struct diskio tmp;
d67611
+	struct diskio tmp = {0};
d67611
 
d67611
 	if (INVALID_MEMBER(blk_mq_ctx_rq_dispatched) ||
d67611
-	    INVALID_MEMBER(blk_mq_ctx_rq_completed))
d67611
+	    INVALID_MEMBER(blk_mq_ctx_rq_completed)) {
d67611
+		get_mq_diskio_from_hw_queues(q, &tmp);
d67611
+		mq_count[0] = tmp.read;
d67611
+		mq_count[1] = tmp.write;
d67611
 		return;
d67611
-
d67611
-	memset(&tmp, 0x00, sizeof(struct diskio));
d67611
+	}
d67611
 
d67611
 	readmem(q + OFFSET(request_queue_queue_ctx), KVADDR, &queue_ctx,
d67611
 		sizeof(ulong), "request_queue.queue_ctx",
d67611
@@ -4479,41 +4636,24 @@ display_one_diskio(struct iter *i, unsigned long gendisk, ulong flags)
d67611
 		&& (io.read + io.write == 0))
d67611
 		return;
d67611
 
d67611
-	if (use_mq_interface(queue_addr) &&
d67611
-	    (INVALID_MEMBER(blk_mq_ctx_rq_dispatched) ||
d67611
-	     INVALID_MEMBER(blk_mq_ctx_rq_completed)))
d67611
-		fprintf(fp, "%s%s%s  %s%s%s%s  %s%s%s",
d67611
-			mkstring(buf0, 5, RJUST|INT_DEC, (char *)(unsigned long)major),
d67611
-			space(MINSPACE),
d67611
-			mkstring(buf1, VADDR_PRLEN, LJUST|LONG_HEX, (char *)gendisk),
d67611
-			space(MINSPACE),
d67611
-			mkstring(buf2, 10, LJUST, disk_name),
d67611
-			space(MINSPACE),
d67611
-			mkstring(buf3, VADDR_PRLEN <= 11 ? 11 : VADDR_PRLEN,
d67611
-				 LJUST|LONG_HEX, (char *)queue_addr),
d67611
-			space(MINSPACE),
d67611
-			mkstring(buf4, 17, RJUST, "(not supported)"),
d67611
-			space(MINSPACE));
d67611
-
d67611
-	else
d67611
-		fprintf(fp, "%s%s%s  %s%s%s%s  %s%5d%s%s%s%s%s",
d67611
-			mkstring(buf0, 5, RJUST|INT_DEC, (char *)(unsigned long)major),
d67611
-			space(MINSPACE),
d67611
-			mkstring(buf1, VADDR_PRLEN, LJUST|LONG_HEX, (char *)gendisk),
d67611
-			space(MINSPACE),
d67611
-			mkstring(buf2, 10, LJUST, disk_name),
d67611
-			space(MINSPACE),
d67611
-			mkstring(buf3, VADDR_PRLEN <= 11 ? 11 : VADDR_PRLEN,
d67611
-				 LJUST|LONG_HEX, (char *)queue_addr),
d67611
-			space(MINSPACE),
d67611
-			io.read + io.write,
d67611
-			space(MINSPACE),
d67611
-			mkstring(buf4, 5, RJUST|INT_DEC,
d67611
-				(char *)(unsigned long)io.read),
d67611
-			space(MINSPACE),
d67611
-			mkstring(buf5, 5, RJUST|INT_DEC,
d67611
-				(char *)(unsigned long)io.write),
d67611
-			space(MINSPACE));
d67611
+	fprintf(fp, "%s%s%s  %s%s%s%s  %s%5d%s%s%s%s%s",
d67611
+		mkstring(buf0, 5, RJUST|INT_DEC, (char *)(unsigned long)major),
d67611
+		space(MINSPACE),
d67611
+		mkstring(buf1, VADDR_PRLEN, LJUST|LONG_HEX, (char *)gendisk),
d67611
+		space(MINSPACE),
d67611
+		mkstring(buf2, 10, LJUST, disk_name),
d67611
+		space(MINSPACE),
d67611
+		mkstring(buf3, VADDR_PRLEN <= 11 ? 11 : VADDR_PRLEN,
d67611
+			 LJUST|LONG_HEX, (char *)queue_addr),
d67611
+		space(MINSPACE),
d67611
+		io.read + io.write,
d67611
+		space(MINSPACE),
d67611
+		mkstring(buf4, 5, RJUST|INT_DEC,
d67611
+			(char *)(unsigned long)io.read),
d67611
+		space(MINSPACE),
d67611
+		mkstring(buf5, 5, RJUST|INT_DEC,
d67611
+			(char *)(unsigned long)io.write),
d67611
+		space(MINSPACE));
d67611
 
d67611
 	if (VALID_MEMBER(request_queue_in_flight)) {
d67611
 		if (!use_mq_interface(queue_addr)) {
d67611
@@ -4597,6 +4737,9 @@ void diskio_init(void)
d67611
 	MEMBER_OFFSET_INIT(kobject_entry, "kobject", "entry");
d67611
 	MEMBER_OFFSET_INIT(kset_list, "kset", "list");
d67611
 	MEMBER_OFFSET_INIT(request_list_count, "request_list", "count");
d67611
+	MEMBER_OFFSET_INIT(request_cmd_flags, "request", "cmd_flags");
d67611
+	MEMBER_OFFSET_INIT(request_q, "request", "q");
d67611
+	MEMBER_OFFSET_INIT(request_state, "request", "state");
d67611
 	MEMBER_OFFSET_INIT(request_queue_in_flight, "request_queue",
d67611
 		"in_flight");
d67611
 	if (MEMBER_EXISTS("request_queue", "rq"))
d67611
@@ -4608,10 +4751,33 @@ void diskio_init(void)
d67611
 			"mq_ops");
d67611
 		ANON_MEMBER_OFFSET_INIT(request_queue_queue_ctx,
d67611
 			"request_queue", "queue_ctx");
d67611
+		MEMBER_OFFSET_INIT(request_queue_queue_hw_ctx,
d67611
+			"request_queue", "queue_hw_ctx");
d67611
+		MEMBER_OFFSET_INIT(request_queue_nr_hw_queues,
d67611
+			"request_queue", "nr_hw_queues");
d67611
 		MEMBER_OFFSET_INIT(blk_mq_ctx_rq_dispatched, "blk_mq_ctx",
d67611
 			"rq_dispatched");
d67611
 		MEMBER_OFFSET_INIT(blk_mq_ctx_rq_completed, "blk_mq_ctx",
d67611
 			"rq_completed");
d67611
+		MEMBER_OFFSET_INIT(blk_mq_hw_ctx_tags, "blk_mq_hw_ctx", "tags");
d67611
+		MEMBER_OFFSET_INIT(blk_mq_tags_bitmap_tags, "blk_mq_tags",
d67611
+			"bitmap_tags");
d67611
+		MEMBER_OFFSET_INIT(blk_mq_tags_breserved_tags, "blk_mq_tags",
d67611
+			"breserved_tags");
d67611
+		MEMBER_OFFSET_INIT(blk_mq_tags_nr_reserved_tags, "blk_mq_tags",
d67611
+			"nr_reserved_tags");
d67611
+		MEMBER_OFFSET_INIT(blk_mq_tags_rqs, "blk_mq_tags", "rqs");
d67611
+		STRUCT_SIZE_INIT(blk_mq_tags, "blk_mq_tags");
d67611
+		STRUCT_SIZE_INIT(sbitmap, "sbitmap");
d67611
+		STRUCT_SIZE_INIT(sbitmap_word, "sbitmap_word");
d67611
+		MEMBER_OFFSET_INIT(sbitmap_word_word, "sbitmap_word", "word");
d67611
+		MEMBER_OFFSET_INIT(sbitmap_word_cleared, "sbitmap_word", "cleared");
d67611
+		MEMBER_OFFSET_INIT(sbitmap_depth, "sbitmap", "depth");
d67611
+		MEMBER_OFFSET_INIT(sbitmap_shift, "sbitmap", "shift");
d67611
+		MEMBER_OFFSET_INIT(sbitmap_map_nr, "sbitmap", "map_nr");
d67611
+		MEMBER_OFFSET_INIT(sbitmap_map, "sbitmap", "map");
d67611
+		MEMBER_OFFSET_INIT(sbitmap_queue_sb, "sbitmap_queue", "sb");
d67611
+
d67611
 	}
d67611
 	MEMBER_OFFSET_INIT(subsys_private_klist_devices, "subsys_private",
d67611
 		"klist_devices");
d67611
diff --git a/symbols.c b/symbols.c
d67611
index 5d12a021c769..c1f09556d710 100644
d67611
--- a/symbols.c
d67611
+++ b/symbols.c
d67611
@@ -10385,6 +10385,12 @@ dump_offset_table(char *spec, ulong makestruct)
d67611
 		OFFSET(kset_list));
d67611
 	fprintf(fp, "            request_list_count: %ld\n",
d67611
 		OFFSET(request_list_count));
d67611
+	fprintf(fp, "             request_cmd_flags: %ld\n",
d67611
+		OFFSET(request_cmd_flags));
d67611
+	fprintf(fp, "                     request_q: %ld\n",
d67611
+		OFFSET(request_q));
d67611
+	fprintf(fp, "                 request_state: %ld\n",
d67611
+		OFFSET(request_state));
d67611
 	fprintf(fp, "       request_queue_in_flight: %ld\n",
d67611
 		OFFSET(request_queue_in_flight));
d67611
 	fprintf(fp, "              request_queue_rq: %ld\n",
d67611
@@ -10393,10 +10399,25 @@ dump_offset_table(char *spec, ulong makestruct)
d67611
 		OFFSET(request_queue_mq_ops));
d67611
 	fprintf(fp, "       request_queue_queue_ctx: %ld\n",
d67611
 		OFFSET(request_queue_queue_ctx));
d67611
+	fprintf(fp, "    request_queue_queue_hw_ctx: %ld\n",
d67611
+		OFFSET(request_queue_queue_hw_ctx));
d67611
+	fprintf(fp, "    request_queue_nr_hw_queues: %ld\n",
d67611
+		OFFSET(request_queue_nr_hw_queues));
d67611
 	fprintf(fp, "      blk_mq_ctx_rq_dispatched: %ld\n",
d67611
 		OFFSET(blk_mq_ctx_rq_dispatched));
d67611
 	fprintf(fp, "       blk_mq_ctx_rq_completed: %ld\n",
d67611
 		OFFSET(blk_mq_ctx_rq_completed));
d67611
+	fprintf(fp, "            blk_mq_hw_ctx_tags: %ld\n",
d67611
+		OFFSET(blk_mq_hw_ctx_tags));
d67611
+	fprintf(fp, "       blk_mq_tags_bitmap_tags: %ld\n",
d67611
+		OFFSET(blk_mq_tags_bitmap_tags));
d67611
+	fprintf(fp, "    blk_mq_tags_breserved_tags: %ld\n",
d67611
+		OFFSET(blk_mq_tags_breserved_tags));
d67611
+	fprintf(fp, "  blk_mq_tags_nr_reserved_tags: %ld\n",
d67611
+		OFFSET(blk_mq_tags_nr_reserved_tags));
d67611
+	fprintf(fp, "               blk_mq_tags_rqs: %ld\n",
d67611
+		OFFSET(blk_mq_tags_rqs));
d67611
+
d67611
 	fprintf(fp, "  subsys_private_klist_devices: %ld\n",
d67611
 		OFFSET(subsys_private_klist_devices));
d67611
 	fprintf(fp, "                subsystem_kset: %ld\n",
d67611
@@ -11003,6 +11024,7 @@ dump_offset_table(char *spec, ulong makestruct)
d67611
 	fprintf(fp, "                       sbitmap: %ld\n", SIZE(sbitmap));
d67611
 	fprintf(fp, "                 sbitmap_queue: %ld\n", SIZE(sbitmap_queue));
d67611
 	fprintf(fp, "                sbq_wait_state: %ld\n", SIZE(sbq_wait_state));
d67611
+	fprintf(fp, "                   blk_mq_tags: %ld\n", SIZE(blk_mq_tags));
d67611
 
d67611
         fprintf(fp, "\n                   array_table:\n");
d67611
 	/*
d67611
-- 
d67611
2.30.2
d67611