2e409b
commit f294197b5511537e6b14d5e1db324f4fc4fdd3f8
2e409b
Author: Dave Anderson <anderson@redhat.com>
2e409b
Date:   Fri Jul 6 10:57:50 2018 -0400
2e409b
2e409b
    Support for the "bpf" command on RHEL 3.10.0-913.el7 and later
2e409b
    3.10-based RHEL7 kernels, which contain a backport of the upstream
2e409b
    eBPF code, but still use the older, pre-4.11, IDR facility that does
2e409b
    not use radix trees for linking the active bpf_prog and bpf_map
2e409b
    structures.  Without the patch, the command indicates "bpf: command
2e409b
    not supported or applicable on this architecture or kernel".
2e409b
    (anderson@redhat.com)
2e409b
2e409b
diff --git a/bpf.c b/bpf.c
2e409b
index 427263d..8871b76 100644
2e409b
--- a/bpf.c
2e409b
+++ b/bpf.c
2e409b
@@ -45,6 +45,10 @@ static void bpf_prog_gpl_compatible(char *, ulong);
2e409b
 static void dump_xlated_plain(void *, unsigned int, int);
2e409b
 static void print_boot_time(unsigned long long, char *, unsigned int);
2e409b
 
2e409b
+static int do_old_idr(int, ulong, struct radix_tree_pair *);
2e409b
+#define OLD_IDR_INIT   (1)
2e409b
+#define OLD_IDR_COUNT  (2)
2e409b
+#define OLD_IDR_GATHER (3)
2e409b
 
2e409b
 #define PROG_ID        (0x1)
2e409b
 #define MAP_ID         (0x2)
2e409b
@@ -167,7 +171,6 @@ bpf_init(struct bpf_info *bpf)
2e409b
 		    !VALID_STRUCT(bpf_map) ||
2e409b
 		    !VALID_STRUCT(bpf_insn) ||
2e409b
 		    INVALID_MEMBER(bpf_prog_aux) ||
2e409b
-		    INVALID_MEMBER(idr_idr_rt) ||
2e409b
 		    INVALID_MEMBER(bpf_prog_type) ||
2e409b
 		    INVALID_MEMBER(bpf_prog_tag) ||
2e409b
 		    INVALID_MEMBER(bpf_prog_jited_len) ||
2e409b
@@ -210,6 +213,9 @@ bpf_init(struct bpf_info *bpf)
2e409b
 			mkstring(buf2, VADDR_PRLEN, CENTER|LJUST, "BPF_MAP"),
2e409b
 			mkstring(buf3, bpf->bpf_map_map_type_size, CENTER|LJUST, "BPF_MAP_TYPE"));
2e409b
 
2e409b
+		if (INVALID_MEMBER(idr_idr_rt))
2e409b
+			do_old_idr(OLD_IDR_INIT, 0, NULL);
2e409b
+
2e409b
 		bpf->status = TRUE;
2e409b
 		break;
2e409b
 
2e409b
@@ -220,24 +226,38 @@ bpf_init(struct bpf_info *bpf)
2e409b
 		command_not_supported();
2e409b
 	}
2e409b
 
2e409b
-	bpf->progs = do_radix_tree(symbol_value("prog_idr") + OFFSET(idr_idr_rt),
2e409b
-		RADIX_TREE_COUNT, NULL);
2e409b
+	if (VALID_MEMBER(idr_idr_rt))
2e409b
+		bpf->progs = do_radix_tree(symbol_value("prog_idr") + OFFSET(idr_idr_rt),
2e409b
+			RADIX_TREE_COUNT, NULL);
2e409b
+	else 
2e409b
+		bpf->progs = do_old_idr(OLD_IDR_COUNT, symbol_value("prog_idr"), NULL);
2e409b
+
2e409b
 	if (bpf->progs) {
2e409b
 		len = sizeof(struct radix_tree_pair) * (bpf->progs+1);
2e409b
 		bpf->proglist = (struct radix_tree_pair *)GETBUF(len);
2e409b
 		bpf->proglist[0].index = bpf->progs;
2e409b
-		bpf->progs = do_radix_tree(symbol_value("prog_idr") + OFFSET(idr_idr_rt),
2e409b
-			RADIX_TREE_GATHER, bpf->proglist);
2e409b
+		if (VALID_MEMBER(idr_idr_rt))
2e409b
+			bpf->progs = do_radix_tree(symbol_value("prog_idr") + OFFSET(idr_idr_rt),
2e409b
+				RADIX_TREE_GATHER, bpf->proglist);
2e409b
+		else
2e409b
+			bpf->progs = do_old_idr(OLD_IDR_GATHER, symbol_value("prog_idr"), bpf->proglist);
2e409b
 	}
2e409b
 
2e409b
-	bpf->maps = do_radix_tree(symbol_value("map_idr") + OFFSET(idr_idr_rt), 
2e409b
-		RADIX_TREE_COUNT, NULL);
2e409b
+	if (VALID_MEMBER(idr_idr_rt))
2e409b
+		bpf->maps = do_radix_tree(symbol_value("map_idr") + OFFSET(idr_idr_rt), 
2e409b
+			RADIX_TREE_COUNT, NULL);
2e409b
+	else
2e409b
+		bpf->maps = do_old_idr(OLD_IDR_COUNT, symbol_value("map_idr"), NULL);
2e409b
+
2e409b
 	if (bpf->maps) {
2e409b
 		len = sizeof(struct radix_tree_pair) * (bpf->maps+1);
2e409b
 		bpf->maplist = (struct radix_tree_pair *)GETBUF(len);
2e409b
 		bpf->maplist[0].index = bpf->maps;
2e409b
-		bpf->maps = do_radix_tree(symbol_value("map_idr") + OFFSET(idr_idr_rt),
2e409b
-			RADIX_TREE_GATHER, bpf->maplist);
2e409b
+		if (VALID_MEMBER(idr_idr_rt))
2e409b
+			bpf->maps = do_radix_tree(symbol_value("map_idr") + OFFSET(idr_idr_rt),
2e409b
+				RADIX_TREE_GATHER, bpf->maplist);
2e409b
+		else
2e409b
+			bpf->maps = do_old_idr(OLD_IDR_GATHER, symbol_value("map_idr"), bpf->maplist);
2e409b
 	}
2e409b
 
2e409b
 	bpf->bpf_prog_buf = GETBUF(SIZE(bpf_prog));
2e409b
@@ -538,8 +558,10 @@ do_map_only:
2e409b
 	}
2e409b
 
2e409b
 bailout:
2e409b
-	FREEBUF(bpf->proglist);
2e409b
-	FREEBUF(bpf->maplist);
2e409b
+	if (bpf->proglist)
2e409b
+		FREEBUF(bpf->proglist);
2e409b
+	if (bpf->maplist)
2e409b
+		FREEBUF(bpf->maplist);
2e409b
 	FREEBUF(bpf->bpf_prog_buf);
2e409b
 	FREEBUF(bpf->bpf_prog_aux_buf);
2e409b
 	FREEBUF(bpf->bpf_map_buf);
2e409b
@@ -1255,3 +1277,50 @@ print_boot_time(unsigned long long nsecs, char *buf, unsigned int size)
2e409b
 	sprintf(buf, "(unknown)");
2e409b
 #endif
2e409b
 }
2e409b
+
2e409b
+/*
2e409b
+ *  Borrow the old (pre-radix_tree) IDR facility code used by
2e409b
+ *  the ipcs command.
2e409b
+ */
2e409b
+static int
2e409b
+do_old_idr(int cmd, ulong idr, struct radix_tree_pair *rtp)
2e409b
+{
2e409b
+	int i, max, cur, next_id, total = 0;
2e409b
+	ulong entry;
2e409b
+
2e409b
+	switch (cmd)
2e409b
+	{
2e409b
+	case OLD_IDR_INIT:
2e409b
+		ipcs_init();
2e409b
+		break;
2e409b
+
2e409b
+	case OLD_IDR_COUNT:
2e409b
+		readmem(idr + OFFSET(idr_cur), KVADDR, &cur, 
2e409b
+			sizeof(int), "idr.cur", FAULT_ON_ERROR);
2e409b
+		for (total = next_id = 0; next_id < cur; next_id++) {
2e409b
+			entry = idr_find(idr, next_id);
2e409b
+			if (entry == 0)
2e409b
+				continue;
2e409b
+			total++;
2e409b
+		}
2e409b
+		break;
2e409b
+
2e409b
+	case OLD_IDR_GATHER:
2e409b
+		max = rtp[0].index;
2e409b
+		readmem(idr + OFFSET(idr_cur), KVADDR, &cur, 
2e409b
+			sizeof(int), "idr.cur", FAULT_ON_ERROR);
2e409b
+		for (i = total = next_id = 0; next_id < cur; next_id++) {
2e409b
+			entry = idr_find(idr, next_id);
2e409b
+			if (entry == 0)
2e409b
+				continue;
2e409b
+			total++;
2e409b
+			rtp[i].index = next_id;
2e409b
+			rtp[i].value = (void *)entry;
2e409b
+			if (++i == max)
2e409b
+				break;
2e409b
+		}
2e409b
+		break;
2e409b
+	}
2e409b
+
2e409b
+	return total;
2e409b
+}
2e409b
diff --git a/defs.h b/defs.h
2e409b
index e6e3850..b05aecc 100644
2e409b
--- a/defs.h
2e409b
+++ b/defs.h
2e409b
@@ -2031,6 +2031,7 @@ struct offset_table {                    /* stash of commonly-used offsets */
2e409b
 	long bpf_prog_aux_load_time;
2e409b
 	long bpf_prog_aux_user;
2e409b
 	long user_struct_uid;
2e409b
+	long idr_cur;
2e409b
 };
2e409b
 
2e409b
 struct size_table {         /* stash of commonly-used sizes */
2e409b
@@ -5590,6 +5591,12 @@ enum {
2e409b
 void dev_init(void);
2e409b
 void dump_dev_table(void);
2e409b
 
2e409b
+/*
2e409b
+ *  ipcs.c
2e409b
+ */
2e409b
+void ipcs_init(void);
2e409b
+ulong idr_find(ulong, int);
2e409b
+
2e409b
 #ifdef ARM
2e409b
 void arm_init(int);
2e409b
 void arm_dump_machdep_table(ulong);
2e409b
diff --git a/ipcs.c b/ipcs.c
2e409b
index ef51fdd..80f78e4 100644
2e409b
--- a/ipcs.c
2e409b
+++ b/ipcs.c
2e409b
@@ -79,13 +79,11 @@ struct ipcs_table {
2e409b
  * function declaration
2e409b
  */
2e409b
 
2e409b
-static void ipcs_init(void);
2e409b
 static int dump_shared_memory(int, ulong, int, ulong);
2e409b
 static int dump_semaphore_arrays(int, ulong, int, ulong);
2e409b
 static int dump_message_queues(int, ulong, int, ulong);
2e409b
 static int ipc_search_idr(ulong, int, ulong, int (*)(ulong, int, ulong, int, int), int);
2e409b
 static int ipc_search_array(ulong, int, ulong, int (*)(ulong, int, ulong, int, int), int);
2e409b
-static ulong idr_find(ulong, int);
2e409b
 static int dump_shm_info(ulong, int, ulong, int, int);
2e409b
 static int dump_sem_info(ulong, int, ulong, int, int);
2e409b
 static int dump_msg_info(ulong, int, ulong, int, int);
2e409b
@@ -101,7 +99,7 @@ static void gather_radix_tree_entries(ulong);
2e409b
  */
2e409b
 static struct ipcs_table ipcs_table = { 0 };
2e409b
 
2e409b
-static void
2e409b
+void
2e409b
 ipcs_init(void)
2e409b
 {
2e409b
 	if (ipcs_table.init_flags & IPCS_INIT) {
2e409b
@@ -119,6 +117,7 @@ ipcs_init(void)
2e409b
 	MEMBER_OFFSET_INIT(idr_layer_layer, "idr_layer", "layer");
2e409b
 	MEMBER_OFFSET_INIT(idr_layer_ary, "idr_layer", "ary");
2e409b
 	MEMBER_OFFSET_INIT(idr_top, "idr", "top");
2e409b
+	MEMBER_OFFSET_INIT(idr_cur, "idr", "cur");
2e409b
 	MEMBER_OFFSET_INIT(ipc_id_ary_p, "ipc_id_ary", "p");
2e409b
 	MEMBER_OFFSET_INIT(ipc_ids_entries, "ipc_ids", "entries");
2e409b
 	MEMBER_OFFSET_INIT(ipc_ids_max_id, "ipc_ids", "max_id");
2e409b
@@ -188,7 +187,10 @@ ipcs_init(void)
2e409b
 		ipcs_table.shm_f_op_huge_addr = -1;
2e409b
 	}
2e409b
 
2e409b
-	if (BITS32())
2e409b
+	if (VALID_MEMBER(idr_layer_ary) && 
2e409b
+	    get_array_length("idr_layer.ary", NULL, 0) > 64)
2e409b
+		ipcs_table.idr_bits = 8;
2e409b
+	else if (BITS32())
2e409b
 		ipcs_table.idr_bits = 5;
2e409b
 	else if (BITS64())
2e409b
 		ipcs_table.idr_bits = 6;
2e409b
@@ -635,7 +637,7 @@ ipc_search_idr(ulong ipc_ids_p, int specified, ulong specified_value, int (*fn)(
2e409b
 /*
2e409b
  * search every idr_layer
2e409b
  */
2e409b
-static ulong
2e409b
+ulong
2e409b
 idr_find(ulong idp, int id)
2e409b
 {
2e409b
 	ulong idr_layer_p;
2e409b
diff --git a/symbols.c b/symbols.c
2e409b
index bf55319..8ff1430 100644
2e409b
--- a/symbols.c
2e409b
+++ b/symbols.c
2e409b
@@ -10041,6 +10041,8 @@ dump_offset_table(char *spec, ulong makestruct)
2e409b
 		OFFSET(idr_layers));
2e409b
 	fprintf(fp, "                       idr_top: %ld\n",
2e409b
 		OFFSET(idr_top));
2e409b
+	fprintf(fp, "                       idr_cur: %ld\n",
2e409b
+		OFFSET(idr_cur));
2e409b
 	fprintf(fp, "                  ipc_id_ary_p: %ld\n",
2e409b
 		OFFSET(ipc_id_ary_p));
2e409b
 	fprintf(fp, "               ipc_ids_entries: %ld\n",