Skip to contentSkip to conversation

kdb: Simplify kdb commands registration

PATCH Superseded Sumit Garg 2 participants 3 messages Jan 19, 2021 – Jan 25, 2021 KGDB / KDB /debug_core

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Sumit Garg

author
#0 lore raw

PATCH 1/1 kdb: Simplify kdb commands registration

Simplify kdb commands registration via using linked list instead of static array for commands storage.

---
 kernel/debug/kdb/kdb_main.c    | 78 ++++++++++--------------------------------
 kernel/debug/kdb/kdb_private.h |  1 +
 2 files changed, 20 insertions(+), 59 deletions(-)
kernel/debug/kdb/kdb_main.c +19 -59
index 930ac1b..93ac0f5 100644
@@ -33,6 +33,7 @@
 #include <linux/kallsyms.h>
 #include <linux/kgdb.h>
 #include <linux/kdb.h>
+#include <linux/list.h>
 #include <linux/notifier.h>
 #include <linux/interrupt.h>
 #include <linux/delay.h>
@@ -84,15 +85,8 @@ static unsigned int kdb_continue_catastrophic =
 static unsigned int kdb_continue_catastrophic;
 #endif
 
-/* kdb_commands describes the available commands. */
-static kdbtab_t *kdb_commands;
-#define KDB_BASE_CMD_MAX 50
-static int kdb_max_commands = KDB_BASE_CMD_MAX;
-static kdbtab_t kdb_base_commands[KDB_BASE_CMD_MAX];
-#define for_each_kdbcmd(cmd, num)					\
-	for ((cmd) = kdb_base_commands, (num) = 0;			\
-	     num < kdb_max_commands;					\
-	     num++, num == KDB_BASE_CMD_MAX ? cmd = kdb_commands : cmd++)
+/* kdb_cmds_head describes the available commands. */
+static LIST_HEAD(kdb_cmds_head);
 
 typedef struct _kdbmsg {
 	int	km_diag;	/* kdb diagnostic */
@@ -921,7 +915,7 @@ int kdb_parse(const char *cmdstr)
 	char *cp;
 	char *cpp, quoted;
 	kdbtab_t *tp;
-	int i, escaped, ignore_errors = 0, check_grep = 0;
+	int escaped, ignore_errors = 0, check_grep = 0;
 
 	/*
 	 * First tokenize the command string.
@@ -1011,7 +1005,7 @@ int kdb_parse(const char *cmdstr)
 		++argv[0];
 	}
 
-	for_each_kdbcmd(tp, i) {
+	list_for_each_entry(tp, &kdb_cmds_head, list_node) {
 		if (tp->cmd_name) {
 			/*
 			 * If this command is allowed to be abbreviated,
@@ -1037,8 +1031,8 @@ int kdb_parse(const char *cmdstr)
 	 * few characters of this match any of the known commands.
 	 * e.g., md1c20 should match md.
 	 */
-	if (i == kdb_max_commands) {
-		for_each_kdbcmd(tp, i) {
+	if (list_entry_is_head(tp, &kdb_cmds_head, list_node)) {
+		list_for_each_entry(tp, &kdb_cmds_head, list_node) {
 			if (tp->cmd_name) {
 				if (strncmp(argv[0],
 					    tp->cmd_name,
@@ -1049,7 +1043,7 @@ int kdb_parse(const char *cmdstr)
 		}
 	}
 
-	if (i < kdb_max_commands) {
+	if (!list_entry_is_head(tp, &kdb_cmds_head, list_node)) {
 		int result;
 
 		if (!kdb_check_flags(tp->cmd_flags, kdb_cmd_enabled, argc <= 1))
@@ -2428,12 +2422,11 @@ static int kdb_kgdb(int argc, const char **argv)
 static int kdb_help(int argc, const char **argv)
 {
 	kdbtab_t *kt;
-	int i;
 
 	kdb_printf("%-15.15s %-20.20s %s\n", "Command", "Usage", "Description");
 	kdb_printf("-----------------------------"
 		   "-----------------------------\n");
-	for_each_kdbcmd(kt, i) {
+	list_for_each_entry(kt, &kdb_cmds_head, list_node) {
 		char *space = "";
 		if (KDB_FLAG(CMD_INTERRUPT))
 			return 0;
@@ -2667,13 +2660,9 @@ int kdb_register_flags(char *cmd,
 		       short minlen,
 		       kdb_cmdflags_t flags)
 {
-	int i;
 	kdbtab_t *kp;
 
-	/*
-	 *  Brute force method to determine duplicates
-	 */
-	for_each_kdbcmd(kp, i) {
+	list_for_each_entry(kp, &kdb_cmds_head, list_node) {
 		if (kp->cmd_name && (strcmp(kp->cmd_name, cmd) == 0)) {
 			kdb_printf("Duplicate kdb command registered: "
 				"%s, func %px help %s\n", cmd, func, help);
@@ -2681,35 +2670,10 @@ int kdb_register_flags(char *cmd,
 		}
 	}
 
-	/*
-	 * Insert command into first available location in table
-	 */
-	for_each_kdbcmd(kp, i) {
-		if (kp->cmd_name == NULL)
-			break;
-	}
-
-	if (i >= kdb_max_commands) {
-		kdbtab_t *new = kmalloc_array(kdb_max_commands -
-						KDB_BASE_CMD_MAX +
-						kdb_command_extend,
-					      sizeof(*new),
-					      GFP_KDB);
-		if (!new) {
-			kdb_printf("Could not allocate new kdb_command "
-				   "table\n");
-			return 1;
-		}
-		if (kdb_commands) {
-			memcpy(new, kdb_commands,
-			  (kdb_max_commands - KDB_BASE_CMD_MAX) * sizeof(*new));
-			kfree(kdb_commands);
-		}
-		memset(new + kdb_max_commands - KDB_BASE_CMD_MAX, 0,
-		       kdb_command_extend * sizeof(*new));
-		kdb_commands = new;
-		kp = kdb_commands + kdb_max_commands - KDB_BASE_CMD_MAX;
-		kdb_max_commands += kdb_command_extend;
+	kp = kmalloc(sizeof(*kp), GFP_KDB);
+	if (!kp) {
+		kdb_printf("Could not allocate new kdb_command table\n");
+		return 1;
 	}
 
 	kp->cmd_name   = cmd;
@@ -2719,6 +2683,8 @@ int kdb_register_flags(char *cmd,
 	kp->cmd_minlen = minlen;
 	kp->cmd_flags  = flags;
 
+	list_add_tail(&kp->list_node, &kdb_cmds_head);
+
 	return 0;
 }
 EXPORT_SYMBOL_GPL(kdb_register_flags);
@@ -2757,15 +2723,15 @@ EXPORT_SYMBOL_GPL(kdb_register);
  */
 int kdb_unregister(char *cmd)
 {
-	int i;
 	kdbtab_t *kp;
 
 	/*
 	 *  find the command.
 	 */
-	for_each_kdbcmd(kp, i) {
+	list_for_each_entry(kp, &kdb_cmds_head, list_node) {
 		if (kp->cmd_name && (strcmp(kp->cmd_name, cmd) == 0)) {
-			kp->cmd_name = NULL;
+			list_del(&kp->list_node);
+			kfree(kp);
 			return 0;
 		}
 	}
@@ -2778,12 +2744,6 @@ EXPORT_SYMBOL_GPL(kdb_unregister);
 /* Initialize the kdb command table. */
 static void __init kdb_inittab(void)
 {
-	int i;
-	kdbtab_t *kp;
-
-	for_each_kdbcmd(kp, i)
-		kp->cmd_name = NULL;
-
 	kdb_register_flags("md", kdb_md, "<vaddr>",
 	  "Display Memory Contents, also mdWcN, e.g. md8c1", 1,
 	  KDB_ENABLE_MEM_READ | KDB_REPEAT_NO_ARGS);
kernel/debug/kdb/kdb_private.h +1 -0
index a4281fb..7a4a181 100644
@@ -174,6 +174,7 @@ typedef struct _kdbtab {
 	short    cmd_minlen;		/* Minimum legal # command
 					 * chars required */
 	kdb_cmdflags_t cmd_flags;	/* Command behaviour flags */
+	struct list_head list_node;
 } kdbtab_t;
 
 extern int kdb_bt(int, const char **);	/* KDB display back trace */
Signature · 1 line
2.7.4
reviewer
#1 lore raw

Hi,

Quoted 9 lines · Sumit Garg

Simplify kdb commands registration via using linked list instead of static array for commands storage.

---
 kernel/debug/kdb/kdb_main.c    | 78 ++++++++++--------------------------------
 kernel/debug/kdb/kdb_private.h |  1 +
 2 files changed, 20 insertions(+), 59 deletions(-)

Wow, nice. It should have been done this way from the start! ;-)

kernel/debug/kdb/kdb_main.c int kdb_parse(const char *cmdstr) · line 1009

Quoted 7 lines
kernel/debug/kdb/kdb_main.c +1 -1
@@ -1011,7 +1005,7 @@ int kdb_parse(const char *cmdstr)
                ++argv[0];
        }
 
-       for_each_kdbcmd(tp, i) {
+       list_for_each_entry(tp, &kdb_cmds_head, list_node) {
                if (tp->cmd_name) {

So I think here (and elsewhere in this file) you can remove this "if (...->cmd_name)" stuff now, right? That was all there because the old "remove" used to just NULL out the name to handle gaps and that's no longer possible. If you are really paranoid you could error-check kdb_register_flags()

kernel/debug/kdb/kdb_private.h typedef struct _kdbtab { · line 177

Quoted 7 lines
kernel/debug/kdb/kdb_private.h +1 -0
@@ -174,6 +174,7 @@ typedef struct _kdbtab {
        short    cmd_minlen;            /* Minimum legal # command
                                         * chars required */
        kdb_cmdflags_t cmd_flags;       /* Command behaviour flags */
+       struct list_head list_node;

nit: every other entry in this struct has a comment but not yours..

Other than those small nits I think this is a great improvement, thanks!

-Doug

author
#2 lore raw

Thanks Doug for your review.

Quoted 21 lines · Doug Anderson

Hi,

Quoted 9 lines · Sumit Garg

Simplify kdb commands registration via using linked list instead of static array for commands storage.

---
 kernel/debug/kdb/kdb_main.c    | 78 ++++++++++--------------------------------
 kernel/debug/kdb/kdb_private.h |  1 +
 2 files changed, 20 insertions(+), 59 deletions(-)

Wow, nice. It should have been done this way from the start! ;-)

Quoted 4 lines
kernel/debug/kdb/kdb_main.c +0 -0
@@ -1011,7 +1005,7 @@ int kdb_parse(const char *cmdstr)
                ++argv[0];
        }
 
Quoted 10 lines

kernel/debug/kdb/kdb_main.c int kdb_parse(const char *cmdstr) · line 1009

Quoted 3 lines
kernel/debug/kdb/kdb_main.c +1 -1
-       for_each_kdbcmd(tp, i) {
+       list_for_each_entry(tp, &kdb_cmds_head, list_node) {
                if (tp->cmd_name) {

So I think here (and elsewhere in this file) you can remove this "if (...->cmd_name)" stuff now, right? That was all there because the old "remove" used to just NULL out the name to handle gaps and that's no longer possible. If you are really paranoid you could error-check kdb_register_flags()

Agree, will get rid of this check.

Quoted 11 lines

kernel/debug/kdb/kdb_private.h typedef struct _kdbtab { · line 177

Quoted 8 lines
kernel/debug/kdb/kdb_private.h +1 -0
@@ -174,6 +174,7 @@ typedef struct _kdbtab {
        short    cmd_minlen;            /* Minimum legal # command
                                         * chars required */
        kdb_cmdflags_t cmd_flags;       /* Command behaviour flags */
+       struct list_head list_node;

nit: every other entry in this struct has a comment but not yours..

Will add a comment here.

Quoted 3 lines

Other than those small nits I think this is a great improvement, thanks!

Thanks,
Sumit

Quoted 1 line

-Doug