Skip to contentSkip to conversation

kdb code refactoring

PATCH Superseded Sumit Garg 2 participants 14 messages Jul 9, 2021 – Jul 12, 2021

Subsystems (2)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Sumit Garg

author
#0 lore raw

v4 cover kdb code refactoring

Some more kdb code refactoring related to:
- Removal of redundant kdb_register_flags().
- Simplification of kdb defcmd macro logic.

Tested with kgdbtest on arm64, doesn't show any regressions.

Changes in v4:
- Split rename of "defcmd_set" to "kdb_macro" as a separate patch.
- Incorporated misc. comments from Doug.
- Added a patch that removes redundant prefix "cmd_" from name of members of struct kdbtab_t.

Changes in v3:
- Split patch into 2 for ease of review.
- Get rid of kdb_register_flags() completely via switching all user to register pre-allocated kdb commands.

Changes in v2:
- Define new structs: kdb_macro_t and kdb_macro_cmd_t instead of modifying existing kdb command struct and struct kdb_subcmd.
- Reword commit message.

Sumit Garg (4):
kdb: Rename struct defcmd_set to struct kdb_macro_t
kdb: Get rid of redundant kdb_register_flags()
kdb: Simplify kdb_defcmd macro logic
kdb: Rename members of struct kdbtab_t

 include/linux/kdb.h            |  27 +-
 kernel/debug/kdb/kdb_bp.c      |  72 ++--
 kernel/debug/kdb/kdb_main.c    | 628 +++++++++++++++------------------
 kernel/debug/kdb/kdb_private.h |  13 -
 kernel/trace/trace_kdb.c       |  12 +-
 samples/kdb/kdb_hello.c        |  20 +-
 6 files changed, 359 insertions(+), 413 deletions(-)
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v4 1/4 kdb: Rename struct defcmd_set to struct kdb_macro_t

as that sounds more appropriate given its purpose.

---
 kernel/debug/kdb/kdb_main.c | 38 ++++++++++++++++++-------------------
 1 file changed, 19 insertions(+), 19 deletions(-)
kernel/debug/kdb/kdb_main.c +19 -19
index d8ee5647b732..d4897fbc9d2e 100644
@@ -654,7 +654,7 @@ static void kdb_cmderror(int diag)
  * Returns:
  *	zero for success, a kdb diagnostic if error
  */
-struct defcmd_set {
+struct kdb_macro_t {
 	int count;
 	bool usable;
 	char *name;
@@ -662,8 +662,8 @@ struct defcmd_set {
 	char *help;
 	char **command;
 };
-static struct defcmd_set *defcmd_set;
-static int defcmd_set_count;
+static struct kdb_macro_t *kdb_macro;
+static int kdb_macro_count;
 static bool defcmd_in_progress;
 
 /* Forward references */
@@ -671,7 +671,7 @@ static int kdb_exec_defcmd(int argc, const char **argv);
 
 static int kdb_defcmd2(const char *cmdstr, const char *argv0)
 {
-	struct defcmd_set *s = defcmd_set + defcmd_set_count - 1;
+	struct kdb_macro_t *s = kdb_macro + kdb_macro_count - 1;
 	char **save_command = s->command;
 	if (strcmp(argv0, "endefcmd") == 0) {
 		defcmd_in_progress = false;
@@ -704,7 +704,7 @@ static int kdb_defcmd2(const char *cmdstr, const char *argv0)
 
 static int kdb_defcmd(int argc, const char **argv)
 {
-	struct defcmd_set *save_defcmd_set = defcmd_set, *s;
+	struct kdb_macro_t *save_kdb_macro = kdb_macro, *s;
 	if (defcmd_in_progress) {
 		kdb_printf("kdb: nested defcmd detected, assuming missing "
 			   "endefcmd\n");
@@ -712,7 +712,7 @@ static int kdb_defcmd(int argc, const char **argv)
 	}
 	if (argc == 0) {
 		int i;
-		for (s = defcmd_set; s < defcmd_set + defcmd_set_count; ++s) {
+		for (s = kdb_macro; s < kdb_macro + kdb_macro_count; ++s) {
 			kdb_printf("defcmd %s \"%s\" \"%s\"\n", s->name,
 				   s->usage, s->help);
 			for (i = 0; i < s->count; ++i)
@@ -727,13 +727,13 @@ static int kdb_defcmd(int argc, const char **argv)
 		kdb_printf("Command only available during kdb_init()\n");
 		return KDB_NOTIMP;
 	}
-	defcmd_set = kmalloc_array(defcmd_set_count + 1, sizeof(*defcmd_set),
+	kdb_macro = kmalloc_array(kdb_macro_count + 1, sizeof(*kdb_macro),
 				   GFP_KDB);
-	if (!defcmd_set)
+	if (!kdb_macro)
 		goto fail_defcmd;
-	memcpy(defcmd_set, save_defcmd_set,
-	       defcmd_set_count * sizeof(*defcmd_set));
-	s = defcmd_set + defcmd_set_count;
+	memcpy(kdb_macro, save_kdb_macro,
+	       kdb_macro_count * sizeof(*kdb_macro));
+	s = kdb_macro + kdb_macro_count;
 	memset(s, 0, sizeof(*s));
 	s->usable = true;
 	s->name = kdb_strdup(argv[1], GFP_KDB);
@@ -753,19 +753,19 @@ static int kdb_defcmd(int argc, const char **argv)
 		strcpy(s->help, argv[3]+1);
 		s->help[strlen(s->help)-1] = '\0';
 	}
-	++defcmd_set_count;
+	++kdb_macro_count;
 	defcmd_in_progress = true;
-	kfree(save_defcmd_set);
+	kfree(save_kdb_macro);
 	return 0;
 fail_help:
 	kfree(s->usage);
 fail_usage:
 	kfree(s->name);
 fail_name:
-	kfree(defcmd_set);
+	kfree(kdb_macro);
 fail_defcmd:
-	kdb_printf("Could not allocate new defcmd_set entry for %s\n", argv[1]);
-	defcmd_set = save_defcmd_set;
+	kdb_printf("Could not allocate new kdb_macro entry for %s\n", argv[1]);
+	kdb_macro = save_kdb_macro;
 	return KDB_NOTIMP;
 }
 
@@ -781,14 +781,14 @@ static int kdb_defcmd(int argc, const char **argv)
 static int kdb_exec_defcmd(int argc, const char **argv)
 {
 	int i, ret;
-	struct defcmd_set *s;
+	struct kdb_macro_t *s;
 	if (argc != 0)
 		return KDB_ARGCOUNT;
-	for (s = defcmd_set, i = 0; i < defcmd_set_count; ++i, ++s) {
+	for (s = kdb_macro, i = 0; i < kdb_macro_count; ++i, ++s) {
 		if (strcmp(s->name, argv[0]) == 0)
 			break;
 	}
-	if (i == defcmd_set_count) {
+	if (i == kdb_macro_count) {
 		kdb_printf("kdb_exec_defcmd: could not find commands for %s\n",
 			   argv[0]);
 		return KDB_NOTIMP;
Signature · 1 line
2.25.1
reviewer
#2 lore raw

Re: [PATCH v4 1/4] kdb: Rename struct defcmd_set to struct kdb_macro_t

Hi,

Quoted 2 lines · Sumit Garg

as that sounds more appropriate given its purpose.

nit: Personally I prefer this to be a whole sentence that isn't just a continuation of the patch subject.

Quoted 10 lines
---
 kernel/debug/kdb/kdb_main.c | 38 ++++++++++++++++++-------------------
 1 file changed, 19 insertions(+), 19 deletions(-)
kernel/debug/kdb/kdb_main.c +0 -0
index d8ee5647b732..d4897fbc9d2e 100644

kernel/debug/kdb/kdb_main.c static void kdb_cmderror(int diag) · line 657

Quoted 5 lines
kernel/debug/kdb/kdb_main.c +1 -1
@@ -654,7 +654,7 @@ static void kdb_cmderror(int diag)
  * Returns:
  *     zero for success, a kdb diagnostic if error
  */
-struct defcmd_set {
+struct kdb_macro_t {

Why the "_t" at the end? To me that implies that this is a typedef, so you'd declare a variable of this as:

kdb_macro_t my_macro;

...instead of:

struct kdb_macro_t my_macro;

Not that I'm suggesting adding a typedef for this structure. It seems kernel convention is not to do typedefs for structures unless there's a strong reason to. Rather, I'm just suggesting removing the "_t" from the end.

kernel/debug/kdb/kdb_main.c static int kdb_exec_defcmd(int argc, const char **argv); · line 674

Quoted 6 lines
kernel/debug/kdb/kdb_main.c +1 -1
@@ -671,7 +671,7 @@ static int kdb_exec_defcmd(int argc, const char **argv);
 
 static int kdb_defcmd2(const char *cmdstr, const char *argv0)
 {
-       struct defcmd_set *s = defcmd_set + defcmd_set_count - 1;
+       struct kdb_macro_t *s = kdb_macro + kdb_macro_count - 1;

I guess the variable "s" was short for "set". Should it change to "m" ? Here and in other places below.

kernel/debug/kdb/kdb_main.c static int kdb_defcmd(int argc, const char **argv) · line 731

Quoted 7 lines
kernel/debug/kdb/kdb_main.c +1 -1
@@ -727,13 +727,13 @@ static int kdb_defcmd(int argc, const char **argv)
                kdb_printf("Command only available during kdb_init()\n");
                return KDB_NOTIMP;
        }
-       defcmd_set = kmalloc_array(defcmd_set_count + 1, sizeof(*defcmd_set),
+       kdb_macro = kmalloc_array(kdb_macro_count + 1, sizeof(*kdb_macro),
                                   GFP_KDB);

nit: the indentation is now off for the GFP_KDB since you changed the length of the previous line.

Sorry for not noticing those things in the previous version. I guess having this part of the change split out really did make it easier to review! ;-) Those are all pretty much just nits, so if they're fixed feel free to add my Reviewed-by tag.

author
#3 lore raw

Re: [PATCH v4 1/4] kdb: Rename struct defcmd_set to struct kdb_macro_t

Hi Doug,

Thanks for your review.

Quoted 10 lines · Doug Anderson

Hi,

Quoted 2 lines · Sumit Garg

as that sounds more appropriate given its purpose.

nit: Personally I prefer this to be a whole sentence that isn't just a continuation of the patch subject.

Okay I will use the whole sentence instead.

Quoted 22 lines
Quoted 11 lines
---
 kernel/debug/kdb/kdb_main.c | 38 ++++++++++++++++++-------------------
 1 file changed, 19 insertions(+), 19 deletions(-)
kernel/debug/kdb/kdb_main.c +0 -0
index d8ee5647b732..d4897fbc9d2e 100644

kernel/debug/kdb/kdb_main.c static void kdb_cmderror(int diag) · line 657

Quoted 5 lines
kernel/debug/kdb/kdb_main.c +1 -1
@@ -654,7 +654,7 @@ static void kdb_cmderror(int diag)
  * Returns:
  *     zero for success, a kdb diagnostic if error
  */
-struct defcmd_set {
+struct kdb_macro_t {

Why the "_t" at the end? To me that implies that this is a typedef, so you'd declare a variable of this as:

kdb_macro_t my_macro;

Quoted 9 lines

...instead of:

struct kdb_macro_t my_macro;

Not that I'm suggesting adding a typedef for this structure. It seems kernel convention is not to do typedefs for structures unless there's a strong reason to. Rather, I'm just suggesting removing the "_t" from the end.

Ack.

Quoted 11 lines

kernel/debug/kdb/kdb_main.c static int kdb_exec_defcmd(int argc, const char **argv); · line 674

Quoted 7 lines
kernel/debug/kdb/kdb_main.c +1 -1
@@ -671,7 +671,7 @@ static int kdb_exec_defcmd(int argc, const char **argv);
 
 static int kdb_defcmd2(const char *cmdstr, const char *argv0)
 {
-       struct defcmd_set *s = defcmd_set + defcmd_set_count - 1;
+       struct kdb_macro_t *s = kdb_macro + kdb_macro_count - 1;

I guess the variable "s" was short for "set". Should it change to "m" ? Here and in other places below.

This variable is dropped in patch #3. Can we bear this variable name until that patch?

Quoted 11 lines

kernel/debug/kdb/kdb_main.c static int kdb_defcmd(int argc, const char **argv) · line 731

Quoted 8 lines
kernel/debug/kdb/kdb_main.c +1 -1
@@ -727,13 +727,13 @@ static int kdb_defcmd(int argc, const char **argv)
                kdb_printf("Command only available during kdb_init()\n");
                return KDB_NOTIMP;
        }
-       defcmd_set = kmalloc_array(defcmd_set_count + 1, sizeof(*defcmd_set),
+       kdb_macro = kmalloc_array(kdb_macro_count + 1, sizeof(*kdb_macro),
                                   GFP_KDB);

nit: the indentation is now off for the GFP_KDB since you changed the length of the previous line.

Okay, I will correct it.

Quoted 5 lines

Sorry for not noticing those things in the previous version. I guess having this part of the change split out really did make it easier to review! ;-) Those are all pretty much just nits, so if they're fixed feel free to add my Reviewed-by tag.

Thanks.
-Sumit

reviewer
#4 lore raw

Re: [PATCH v4 1/4] kdb: Rename struct defcmd_set to struct kdb_macro_t

Hi,

Quoted 12 lines · Sumit Garg
Quoted 11 lines

kernel/debug/kdb/kdb_main.c static int kdb_exec_defcmd(int argc, const char **argv); · line 674

Quoted 7 lines
kernel/debug/kdb/kdb_main.c +1 -1
@@ -671,7 +671,7 @@ static int kdb_exec_defcmd(int argc, const char **argv);
 
 static int kdb_defcmd2(const char *cmdstr, const char *argv0)
 {
-       struct defcmd_set *s = defcmd_set + defcmd_set_count - 1;
+       struct kdb_macro_t *s = kdb_macro + kdb_macro_count - 1;

I guess the variable "s" was short for "set". Should it change to "m" ? Here and in other places below.

Quoted 2 lines

This variable is dropped in patch #3. Can we bear this variable name until that patch?

Sure.

-Doug

author
#5 lore raw

PATCH v4 2/4 kdb: Get rid of redundant kdb_register_flags()

In reply to Sumit Garg

Commit e4f291b3f7bb ("kdb: Simplify kdb commands registration") allowed registration of pre-allocated kdb commands with pointer to struct kdbtab_t. Lets switch other users as well to register pre- allocated kdb commands via:
- Changing prototype for kdb_register() to pass a pointer to struct kdbtab_t instead.
- Embed kdbtab_t structure in kdb_macro_t rather than individual params.

With these changes kdb_register_flags() becomes redundant and hence removed. Also, since we have switched all users to register pre-allocated commands, "is_dynamic" flag in struct kdbtab_t becomes redundant and hence removed as well.

---
 include/linux/kdb.h            |  27 ++++--
 kernel/debug/kdb/kdb_main.c    | 167 +++++++++++----------------------
 kernel/debug/kdb/kdb_private.h |  13 ---
 kernel/trace/trace_kdb.c       |  12 ++-
 samples/kdb/kdb_hello.c        |  20 ++--
 5 files changed, 88 insertions(+), 151 deletions(-)
include/linux/kdb.h +17 -10
index 0125a677b67f..de858edfb3b8 100644
@@ -13,6 +13,8 @@
  * Copyright (C) 2009 Jason Wessel <[email protected]>
  */
 
+#include <linux/list.h>
+
 /* Shifted versions of the command enable bits are be used if the command
  * has no arguments (see kdb_check_flags). This allows commands, such as
  * go, to have different permissions depending upon whether it is called
@@ -64,6 +66,17 @@ typedef enum {
 
 typedef int (*kdb_func_t)(int, const char **);
 
+/* The KDB shell command table */
+typedef struct _kdbtab {
+	char    *cmd_name;		/* Command name */
+	kdb_func_t cmd_func;		/* Function to execute command */
+	char    *cmd_usage;		/* Usage String for this command */
+	char    *cmd_help;		/* Help message for this command */
+	short    cmd_minlen;		/* Minimum legal # cmd chars required */
+	kdb_cmdflags_t cmd_flags;	/* Command behaviour flags */
+	struct list_head list_node;	/* Command list */
+} kdbtab_t;
+
 #ifdef	CONFIG_KGDB_KDB
 #include <linux/init.h>
 #include <linux/sched.h>
@@ -193,19 +206,13 @@ static inline const char *kdb_walk_kallsyms(loff_t *pos)
 #endif /* ! CONFIG_KALLSYMS */
 
 /* Dynamic kdb shell command registration */
-extern int kdb_register(char *, kdb_func_t, char *, char *, short);
-extern int kdb_register_flags(char *, kdb_func_t, char *, char *,
-			      short, kdb_cmdflags_t);
-extern int kdb_unregister(char *);
+extern int kdb_register(kdbtab_t *cmd);
+extern void kdb_unregister(kdbtab_t *cmd);
 #else /* ! CONFIG_KGDB_KDB */
 static inline __printf(1, 2) int kdb_printf(const char *fmt, ...) { return 0; }
 static inline void kdb_init(int level) {}
-static inline int kdb_register(char *cmd, kdb_func_t func, char *usage,
-			       char *help, short minlen) { return 0; }
-static inline int kdb_register_flags(char *cmd, kdb_func_t func, char *usage,
-				     char *help, short minlen,
-				     kdb_cmdflags_t flags) { return 0; }
-static inline int kdb_unregister(char *cmd) { return 0; }
+static inline int kdb_register(kdbtab_t *cmd) { return 0; }
+static inline void kdb_unregister(kdbtab_t *cmd) {}
 #endif	/* CONFIG_KGDB_KDB */
 enum {
 	KDB_NOT_INITIALIZED,
kernel/debug/kdb/kdb_main.c +53 -114

258 lines not on this page · raw message

kernel/debug/kdb/kdb_private.h +0 -13
index 8dbc840113c9..629590084a0d 100644
@@ -164,19 +164,6 @@ typedef struct _kdb_bp {
 #ifdef CONFIG_KGDB_KDB
 extern kdb_bp_t kdb_breakpoints[/* KDB_MAXBPT */];
 
-/* The KDB shell command table */
-typedef struct _kdbtab {
-	char    *cmd_name;		/* Command name */
-	kdb_func_t cmd_func;		/* Function to execute command */
-	char    *cmd_usage;		/* Usage String for this command */
-	char    *cmd_help;		/* Help message for this command */
-	short    cmd_minlen;		/* Minimum legal # command
-					 * chars required */
-	kdb_cmdflags_t cmd_flags;	/* Command behaviour flags */
-	struct list_head list_node;	/* Command list */
-	bool    is_dynamic;		/* Command table allocation type */
-} kdbtab_t;
-
 extern void kdb_register_table(kdbtab_t *kp, size_t len);
 extern int kdb_bt(int, const char **);	/* KDB display back trace */
 
kernel/trace/trace_kdb.c +9 -3
index 9da76104f7a2..6c4f92c79e43 100644
@@ -147,11 +147,17 @@ static int kdb_ftdump(int argc, const char **argv)
 	return 0;
 }
 
+static kdbtab_t ftdump_cmd = {
+	.cmd_name = "ftdump",
+	.cmd_func = kdb_ftdump,
+	.cmd_usage = "[skip_#entries] [cpu]",
+	.cmd_help = "Dump ftrace log; -skip dumps last #entries",
+	.cmd_flags = KDB_ENABLE_ALWAYS_SAFE,
+};
+
 static __init int kdb_ftrace_register(void)
 {
-	kdb_register_flags("ftdump", kdb_ftdump, "[skip_#entries] [cpu]",
-			    "Dump ftrace log; -skip dumps last #entries", 0,
-			    KDB_ENABLE_ALWAYS_SAFE);
+	kdb_register(&ftdump_cmd);
 	return 0;
 }
 
samples/kdb/kdb_hello.c +9 -11
index c1c2fa0f62c2..9ad514a6648b 100644
@@ -28,28 +28,26 @@ static int kdb_hello_cmd(int argc, const char **argv)
 	return 0;
 }
 
+static kdbtab_t hello_cmd = {
+	.cmd_name = "hello",
+	.cmd_func = kdb_hello_cmd,
+	.cmd_usage = "[string]",
+	.cmd_help = "Say Hello World or Hello [string]",
+};
 
 static int __init kdb_hello_cmd_init(void)
 {
 	/*
 	 * Registration of a dynamically added kdb command is done with
-	 * kdb_register() with the arguments being:
-	 *   1: The name of the shell command
-	 *   2: The function that processes the command
-	 *   3: Description of the usage of any arguments
-	 *   4: Descriptive text when you run help
-	 *   5: Number of characters to complete the command
-	 *      0 == type the whole command
-	 *      1 == match both "g" and "go" for example
+	 * kdb_register().
 	 */
-	kdb_register("hello", kdb_hello_cmd, "[string]",
-		     "Say Hello World or Hello [string]", 0);
+	kdb_register(&hello_cmd);
 	return 0;
 }
 
 static void __exit kdb_hello_cmd_exit(void)
 {
-	kdb_unregister("hello");
+	kdb_unregister(&hello_cmd);
 }
 
 module_init(kdb_hello_cmd_init);
Signature · 1 line
2.25.1
reviewer Reviewed-by on v4 2/4
#6 lore raw

Re: [PATCH v4 2/4] kdb: Get rid of redundant kdb_register_flags()

Hi,

Quoted 9 lines · Sumit Garg

Commit e4f291b3f7bb ("kdb: Simplify kdb commands registration") allowed registration of pre-allocated kdb commands with pointer to struct kdbtab_t. Lets switch other users as well to register pre- allocated kdb commands via:
- Changing prototype for kdb_register() to pass a pointer to struct kdbtab_t instead.
- Embed kdbtab_t structure in kdb_macro_t rather than individual params.

Quoted 7 lines

With these changes kdb_register_flags() becomes redundant and hence removed. Also, since we have switched all users to register pre-allocated commands, "is_dynamic" flag in struct kdbtab_t becomes redundant and hence removed as well.

Looking at v3 responses [1], seems like you forgot to add again:

This also looks good to me now.

author
#7 lore raw

Re: [PATCH v4 2/4] kdb: Get rid of redundant kdb_register_flags()

Quoted 18 lines · Doug Anderson

Hi,

Quoted 14 lines · Sumit Garg

Commit e4f291b3f7bb ("kdb: Simplify kdb commands registration") allowed registration of pre-allocated kdb commands with pointer to struct kdbtab_t. Lets switch other users as well to register pre- allocated kdb commands via:
- Changing prototype for kdb_register() to pass a pointer to struct kdbtab_t instead.
- Embed kdbtab_t structure in kdb_macro_t rather than individual params.

With these changes kdb_register_flags() becomes redundant and hence removed. Also, since we have switched all users to register pre-allocated commands, "is_dynamic" flag in struct kdbtab_t becomes redundant and hence removed as well.

Quoted 7 lines
Quoted 2 lines

Looking at v3 responses [1], seems like you forgot to add again:

Ah, it was mistakenly dropped, I will add it again.

Quoted 3 lines

This also looks good to me now.

Thanks,
-Sumit

author
#8 lore raw

PATCH v4 3/4 kdb: Simplify kdb_defcmd macro logic

In reply to Sumit Garg

Switch to use a linked list instead of dynamic array which makes allocation of kdb macro and traversing the kdb macro commands list simpler.

---
 kernel/debug/kdb/kdb_main.c | 107 +++++++++++++++++++-----------------
 1 file changed, 58 insertions(+), 49 deletions(-)
kernel/debug/kdb/kdb_main.c +58 -49
index 6d9ff4048e7d..371983c03223 100644
@@ -654,13 +654,16 @@ static void kdb_cmderror(int diag)
  *	zero for success, a kdb diagnostic if error
  */
 struct kdb_macro_t {
-	int count;
-	bool usable;
-	kdbtab_t cmd;
-	char **command;
+	kdbtab_t cmd;			/* Macro command */
+	struct list_head statements;	/* Associated statement list */
 };
+
+struct kdb_macro_statement_t {
+	char *statement;		/* Statement name */
+	struct list_head list_node;	/* Statement list node */
+};
+
 static struct kdb_macro_t *kdb_macro;
-static int kdb_macro_count;
 static bool defcmd_in_progress;
 
 /* Forward references */
@@ -668,34 +671,33 @@ static int kdb_exec_defcmd(int argc, const char **argv);
 
 static int kdb_defcmd2(const char *cmdstr, const char *argv0)
 {
-	struct kdb_macro_t *s = kdb_macro + kdb_macro_count - 1;
-	char **save_command = s->command;
+	struct kdb_macro_statement_t *kmc;
+
+	if (!kdb_macro)
+		return KDB_NOTIMP;
+
 	if (strcmp(argv0, "endefcmd") == 0) {
 		defcmd_in_progress = false;
-		if (!s->count)
-			s->usable = false;
-		if (s->usable)
-			kdb_register(&s->cmd);
+		if (!list_empty(&kdb_macro->statements))
+			kdb_register(&kdb_macro->cmd);
 		return 0;
 	}
-	if (!s->usable)
-		return KDB_NOTIMP;
-	s->command = kcalloc(s->count + 1, sizeof(*(s->command)), GFP_KDB);
-	if (!s->command) {
-		kdb_printf("Could not allocate new kdb_defcmd table for %s\n",
+
+	kmc = kmalloc(sizeof(*kmc), GFP_KDB);
+	if (!kmc) {
+		kdb_printf("Could not allocate new kdb macro command: %s\n",
 			   cmdstr);
-		s->usable = false;
 		return KDB_NOTIMP;
 	}
-	memcpy(s->command, save_command, s->count * sizeof(*(s->command)));
-	s->command[s->count++] = kdb_strdup(cmdstr, GFP_KDB);
-	kfree(save_command);
+
+	kmc->statement = kdb_strdup(cmdstr, GFP_KDB);
+	list_add_tail(&kmc->list_node, &kdb_macro->statements);
+
 	return 0;
 }
 
 static int kdb_defcmd(int argc, const char **argv)
 {
-	struct kdb_macro_t *save_kdb_macro = kdb_macro, *s;
 	kdbtab_t *mp;
 
 	if (defcmd_in_progress) {
@@ -704,13 +706,21 @@ static int kdb_defcmd(int argc, const char **argv)
 		kdb_defcmd2("endefcmd", "endefcmd");
 	}
 	if (argc == 0) {
-		int i;
-		for (s = kdb_macro; s < kdb_macro + kdb_macro_count; ++s) {
-			kdb_printf("defcmd %s \"%s\" \"%s\"\n", s->cmd.cmd_name,
-				   s->cmd.cmd_usage, s->cmd.cmd_help);
-			for (i = 0; i < s->count; ++i)
-				kdb_printf("%s", s->command[i]);
-			kdb_printf("endefcmd\n");
+		kdbtab_t *kp;
+		struct kdb_macro_t *kmp;
+		struct kdb_macro_statement_t *kmc;
+
+		list_for_each_entry(kp, &kdb_cmds_head, list_node) {
+			if (kp->cmd_func == kdb_exec_defcmd) {
+				kdb_printf("defcmd %s \"%s\" \"%s\"\n",
+					   kp->cmd_name, kp->cmd_usage,
+					   kp->cmd_help);
+				kmp = container_of(kp, struct kdb_macro_t, cmd);
+				list_for_each_entry(kmc, &kmp->statements,
+						    list_node)
+					kdb_printf("%s", kmc->statement);
+				kdb_printf("endefcmd\n");
+			}
 		}
 		return 0;
 	}
@@ -720,17 +730,11 @@ static int kdb_defcmd(int argc, const char **argv)
 		kdb_printf("Command only available during kdb_init()\n");
 		return KDB_NOTIMP;
 	}
-	kdb_macro = kmalloc_array(kdb_macro_count + 1, sizeof(*kdb_macro),
-				   GFP_KDB);
+	kdb_macro = kzalloc(sizeof(*kdb_macro), GFP_KDB);
 	if (!kdb_macro)
 		goto fail_defcmd;
-	memcpy(kdb_macro, save_kdb_macro,
-	       kdb_macro_count * sizeof(*kdb_macro));
-	s = kdb_macro + kdb_macro_count;
-	memset(s, 0, sizeof(*s));
-	s->usable = true;
 
-	mp = &s->cmd;
+	mp = &kdb_macro->cmd;
 	mp->cmd_func = kdb_exec_defcmd;
 	mp->cmd_minlen = 0;
 	mp->cmd_flags = KDB_ENABLE_ALWAYS_SAFE;
@@ -751,9 +755,9 @@ static int kdb_defcmd(int argc, const char **argv)
 		strcpy(mp->cmd_help, argv[3]+1);
 		mp->cmd_help[strlen(mp->cmd_help)-1] = '\0';
 	}
-	++kdb_macro_count;
+
+	INIT_LIST_HEAD(&kdb_macro->statements);
 	defcmd_in_progress = true;
-	kfree(save_kdb_macro);
 	return 0;
 fail_help:
 	kfree(mp->cmd_usage);
@@ -763,7 +767,6 @@ static int kdb_defcmd(int argc, const char **argv)
 	kfree(kdb_macro);
 fail_defcmd:
 	kdb_printf("Could not allocate new kdb_macro entry for %s\n", argv[1]);
-	kdb_macro = save_kdb_macro;
 	return KDB_NOTIMP;
 }
 
@@ -778,25 +781,31 @@ static int kdb_defcmd(int argc, const char **argv)
  */
 static int kdb_exec_defcmd(int argc, const char **argv)
 {
-	int i, ret;
-	struct kdb_macro_t *s;
+	int ret;
+	kdbtab_t *kp;
+	struct kdb_macro_t *kmp;
+	struct kdb_macro_statement_t *kmc;
+
 	if (argc != 0)
 		return KDB_ARGCOUNT;
-	for (s = kdb_macro, i = 0; i < kdb_macro_count; ++i, ++s) {
-		if (strcmp(s->cmd.cmd_name, argv[0]) == 0)
+
+	list_for_each_entry(kp, &kdb_cmds_head, list_node) {
+		if (strcmp(kp->cmd_name, argv[0]) == 0)
 			break;
 	}
-	if (i == kdb_macro_count) {
+	if (list_entry_is_head(kp, &kdb_cmds_head, list_node)) {
 		kdb_printf("kdb_exec_defcmd: could not find commands for %s\n",
 			   argv[0]);
 		return KDB_NOTIMP;
 	}
-	for (i = 0; i < s->count; ++i) {
-		/* Recursive use of kdb_parse, do not use argv after
-		 * this point */
+	kmp = container_of(kp, struct kdb_macro_t, cmd);
+	list_for_each_entry(kmc, &kmp->statements, list_node) {
+		/*
+		 * Recursive use of kdb_parse, do not use argv after this point.
+		 */
 		argv = NULL;
-		kdb_printf("[%s]kdb> %s\n", s->cmd.cmd_name, s->command[i]);
-		ret = kdb_parse(s->command[i]);
+		kdb_printf("[%s]kdb> %s\n", kmp->cmd.cmd_name, kmc->statement);
+		ret = kdb_parse(kmc->statement);
 		if (ret)
 			return ret;
 	}
Signature · 1 line
2.25.1
reviewer Reviewed-by on v4 3/4
#9 lore raw

Re: [PATCH v4 3/4] kdb: Simplify kdb_defcmd macro logic

Hi,

Quoted 19 lines · Sumit Garg

Switch to use a linked list instead of dynamic array which makes allocation of kdb macro and traversing the kdb macro commands list simpler.

---
 kernel/debug/kdb/kdb_main.c | 107 +++++++++++++++++++-----------------
 1 file changed, 58 insertions(+), 49 deletions(-)
kernel/debug/kdb/kdb_main.c +0 -0
index 6d9ff4048e7d..371983c03223 100644
@@ -654,13 +654,16 @@ static void kdb_cmderror(int diag)
  *     zero for success, a kdb diagnostic if error
  */
 struct kdb_macro_t {

kernel/debug/kdb/kdb_main.c static void kdb_cmderror(int diag) · line 662

Quoted 10 lines
kernel/debug/kdb/kdb_main.c +5 -4
-       int count;
-       bool usable;
-       kdbtab_t cmd;
-       char **command;
+       kdbtab_t cmd;                   /* Macro command */
+       struct list_head statements;    /* Associated statement list */
 };
+
+struct kdb_macro_statement_t {
+       char *statement;                /* Statement name */

This is still not really the name. This is the actual statement, right? Like it might contain "ftdump -1", right? It seems really weird to call that the "name". You could drop the word "name", or change this to "Statement text", or just totally drop the comment.

Other than that this looks good to me.

author
#10 lore raw

Re: [PATCH v4 3/4] kdb: Simplify kdb_defcmd macro logic

Quoted 30 lines · Doug Anderson

Hi,

Quoted 26 lines · Sumit Garg

Switch to use a linked list instead of dynamic array which makes allocation of kdb macro and traversing the kdb macro commands list simpler.

---
 kernel/debug/kdb/kdb_main.c | 107 +++++++++++++++++++-----------------
 1 file changed, 58 insertions(+), 49 deletions(-)
kernel/debug/kdb/kdb_main.c +2 -4
index 6d9ff4048e7d..371983c03223 100644
@@ -654,13 +654,16 @@ static void kdb_cmderror(int diag)
  *     zero for success, a kdb diagnostic if error
  */
 struct kdb_macro_t {
-       int count;
-       bool usable;
-       kdbtab_t cmd;
-       char **command;
+       kdbtab_t cmd;                   /* Macro command */
+       struct list_head statements;    /* Associated statement list */
 };
Quoted 9 lines

kernel/debug/kdb/kdb_main.c static void kdb_cmderror(int diag) · line 662

Quoted 3 lines
kernel/debug/kdb/kdb_main.c +3 -0
+
+struct kdb_macro_statement_t {
+       char *statement;                /* Statement name */

This is still not really the name. This is the actual statement, right? Like it might contain "ftdump -1", right? It seems really weird to call that the "name". You could drop the word "name", or change this to "Statement text", or just totally drop the comment.

Let me use "Statement text" instead.

Quoted 3 lines

Other than that this looks good to me.

Thanks,
-Sumit

author
#11 lore raw

PATCH v4 4/4 kdb: Rename members of struct kdbtab_t

In reply to Sumit Garg

Remove redundant prefix "cmd_" from name of members in struct kdbtab_t for better readibility.

---
 include/linux/kdb.h         |  12 +-
 kernel/debug/kdb/kdb_bp.c   |  72 +++----
 kernel/debug/kdb/kdb_main.c | 406 ++++++++++++++++++------------------
 kernel/trace/trace_kdb.c    |  10 +-
 samples/kdb/kdb_hello.c     |   8 +-
 5 files changed, 254 insertions(+), 254 deletions(-)
include/linux/kdb.h +6 -6
index de858edfb3b8..ea0f5e580fac 100644
@@ -68,12 +68,12 @@ typedef int (*kdb_func_t)(int, const char **);
 
 /* The KDB shell command table */
 typedef struct _kdbtab {
-	char    *cmd_name;		/* Command name */
-	kdb_func_t cmd_func;		/* Function to execute command */
-	char    *cmd_usage;		/* Usage String for this command */
-	char    *cmd_help;		/* Help message for this command */
-	short    cmd_minlen;		/* Minimum legal # cmd chars required */
-	kdb_cmdflags_t cmd_flags;	/* Command behaviour flags */
+	char    *name;			/* Command name */
+	kdb_func_t func;		/* Function to execute command */
+	char    *usage;			/* Usage String for this command */
+	char    *help;			/* Help message for this command */
+	short    minlen;		/* Minimum legal # cmd chars required */
+	kdb_cmdflags_t flags;		/* Command behaviour flags */
 	struct list_head list_node;	/* Command list */
 } kdbtab_t;
 
kernel/debug/kdb/kdb_bp.c +36 -36

87 lines not on this page · raw message

kernel/debug/kdb/kdb_main.c +203 -203

538 lines not on this page · raw message

kernel/trace/trace_kdb.c +5 -5
index 6c4f92c79e43..59857a1ee44c 100644
@@ -148,11 +148,11 @@ static int kdb_ftdump(int argc, const char **argv)
 }
 
 static kdbtab_t ftdump_cmd = {
-	.cmd_name = "ftdump",
-	.cmd_func = kdb_ftdump,
-	.cmd_usage = "[skip_#entries] [cpu]",
-	.cmd_help = "Dump ftrace log; -skip dumps last #entries",
-	.cmd_flags = KDB_ENABLE_ALWAYS_SAFE,
+	.name = "ftdump",
+	.func = kdb_ftdump,
+	.usage = "[skip_#entries] [cpu]",
+	.help = "Dump ftrace log; -skip dumps last #entries",
+	.flags = KDB_ENABLE_ALWAYS_SAFE,
 };
 
 static __init int kdb_ftrace_register(void)
samples/kdb/kdb_hello.c +4 -4
index 9ad514a6648b..82736e5a5e32 100644
@@ -29,10 +29,10 @@ static int kdb_hello_cmd(int argc, const char **argv)
 }
 
 static kdbtab_t hello_cmd = {
-	.cmd_name = "hello",
-	.cmd_func = kdb_hello_cmd,
-	.cmd_usage = "[string]",
-	.cmd_help = "Say Hello World or Hello [string]",
+	.name = "hello",
+	.func = kdb_hello_cmd,
+	.usage = "[string]",
+	.help = "Say Hello World or Hello [string]",
 };
 
 static int __init kdb_hello_cmd_init(void)
Signature · 1 line
2.25.1
reviewer Reviewed-by on v4 4/4
#12 lore raw

Re: [PATCH v4 4/4] kdb: Rename members of struct kdbtab_t

Hi,

kernel/debug/kdb/kdb_main.c static int kdb_defcmd(int argc, const char **argv) · line 717

Quoted 12 lines · Sumit Garg
kernel/debug/kdb/kdb_main.c +3 -3
@@ -711,10 +711,10 @@ static int kdb_defcmd(int argc, const char **argv)
                struct kdb_macro_statement_t *kmc;
 
                list_for_each_entry(kp, &kdb_cmds_head, list_node) {
-                       if (kp->cmd_func == kdb_exec_defcmd) {
+                       if (kp->func == kdb_exec_defcmd) {
                                kdb_printf("defcmd %s \"%s\" \"%s\"\n",
-                                          kp->cmd_name, kp->cmd_usage,
-                                          kp->cmd_help);
+                                          kp->name, kp->usage,
+                                          kp->help);

The call could probably be squashed down to 2 lines instead of 3 now.

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

Quoted 8 lines
kernel/debug/kdb/kdb_main.c +2 -2
@@ -1031,8 +1031,8 @@ int kdb_parse(const char *cmdstr)
         */
        if (list_entry_is_head(tp, &kdb_cmds_head, list_node)) {
                list_for_each_entry(tp, &kdb_cmds_head, list_node) {
-                       if (strncmp(argv[0], tp->cmd_name,
-                                   strlen(tp->cmd_name)) == 0)
+                       if (strncmp(argv[0], tp->name,
+                                   strlen(tp->name)) == 0)

Squash down to one line now that it's shorter.

The word wrapping isn't really a huge deal to me, though, so:

author
#13 lore raw

Re: [PATCH v4 4/4] kdb: Rename members of struct kdbtab_t

Quoted 8 lines · Doug Anderson

Hi,

Quoted 4 lines · Sumit Garg
kernel/debug/kdb/kdb_main.c +0 -0
@@ -711,10 +711,10 @@ static int kdb_defcmd(int argc, const char **argv)
                struct kdb_macro_statement_t *kmc;
 
Quoted 11 lines

kernel/debug/kdb/kdb_main.c static int kdb_defcmd(int argc, const char **argv) · line 717

Quoted 8 lines
kernel/debug/kdb/kdb_main.c +3 -3
                list_for_each_entry(kp, &kdb_cmds_head, list_node) {
-                       if (kp->cmd_func == kdb_exec_defcmd) {
+                       if (kp->func == kdb_exec_defcmd) {
                                kdb_printf("defcmd %s \"%s\" \"%s\"\n",
-                                          kp->cmd_name, kp->cmd_usage,
-                                          kp->cmd_help);
+                                          kp->name, kp->usage,
+                                          kp->help);

The call could probably be squashed down to 2 lines instead of 3 now.

Ack.

Quoted 12 lines

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

Quoted 9 lines
kernel/debug/kdb/kdb_main.c +2 -2
@@ -1031,8 +1031,8 @@ int kdb_parse(const char *cmdstr)
         */
        if (list_entry_is_head(tp, &kdb_cmds_head, list_node)) {
                list_for_each_entry(tp, &kdb_cmds_head, list_node) {
-                       if (strncmp(argv[0], tp->cmd_name,
-                                   strlen(tp->cmd_name)) == 0)
+                       if (strncmp(argv[0], tp->name,
+                                   strlen(tp->name)) == 0)

Squash down to one line now that it's shorter.

Ack.

Quoted 4 lines

The word wrapping isn't really a huge deal to me, though, so:

Thanks,
-Sumit