All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] kdb: use sizeof(kdb_prompt_str) instead of mismatched CMD_BUFLEN
@ 2026-06-16 16:44 Naveen Kumar Chaudhary
  2026-06-16 20:20 ` David Laight
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Naveen Kumar Chaudhary @ 2026-06-16 16:44 UTC (permalink / raw)
  To: jason.wessel, danielt, dianders; +Cc: kgdb-bugreport, linux-kernel

kdb_main.c defines CMD_BUFLEN as 200 (for command history buffers),
while kdb_io.c defines it as 256 (for kdb_prompt_str). The snprintf()
filling kdb_prompt_str incorrectly used the local CMD_BUFLEN (200),
truncating the prompt unnecessarily. Use sizeof(kdb_prompt_str) to
always match the actual buffer size.

Signed-off-by: Naveen Kumar Chaudhary <naveen.osdev@gmail.com>
---
 kernel/debug/kdb/kdb_main.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/kernel/debug/kdb/kdb_main.c b/kernel/debug/kdb/kdb_main.c
index ddce56b47b25..571e9e61b40e 100644
--- a/kernel/debug/kdb/kdb_main.c
+++ b/kernel/debug/kdb/kdb_main.c
@@ -1265,8 +1265,8 @@ static int kdb_local(kdb_reason_t reason, int error, struct pt_regs *regs,
 
 do_full_getstr:
 		/* PROMPT can only be set if we have MEM_READ permission. */
-		snprintf(kdb_prompt_str, CMD_BUFLEN, kdbgetenv("PROMPT"),
-			 raw_smp_processor_id());
+		snprintf(kdb_prompt_str, sizeof(kdb_prompt_str),
+			 kdbgetenv("PROMPT"), raw_smp_processor_id());
 
 		/*
 		 * Fetch command from keyboard
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH] kdb: use sizeof(kdb_prompt_str) instead of mismatched CMD_BUFLEN
  2026-06-16 16:44 [PATCH] kdb: use sizeof(kdb_prompt_str) instead of mismatched CMD_BUFLEN Naveen Kumar Chaudhary
@ 2026-06-16 20:20 ` David Laight
  2026-06-16 22:06   ` Doug Anderson
  2026-06-16 22:04 ` Doug Anderson
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 9+ messages in thread
From: David Laight @ 2026-06-16 20:20 UTC (permalink / raw)
  To: Naveen Kumar Chaudhary
  Cc: jason.wessel, danielt, dianders, kgdb-bugreport, linux-kernel

On Tue, 16 Jun 2026 22:14:54 +0530
Naveen Kumar Chaudhary <naveen.osdev@gmail.com> wrote:

> kdb_main.c defines CMD_BUFLEN as 200 (for command history buffers),
> while kdb_io.c defines it as 256 (for kdb_prompt_str). The snprintf()
> filling kdb_prompt_str incorrectly used the local CMD_BUFLEN (200),
> truncating the prompt unnecessarily. Use sizeof(kdb_prompt_str) to
> always match the actual buffer size.

As a matter of interest what sets the string that kdbgetenv("PROMPT")
returns?
If it is user settable, using it as a format string doesn't seem wise
(even for kdbg).

	David

> 
> Signed-off-by: Naveen Kumar Chaudhary <naveen.osdev@gmail.com>
> ---
>  kernel/debug/kdb/kdb_main.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/kernel/debug/kdb/kdb_main.c b/kernel/debug/kdb/kdb_main.c
> index ddce56b47b25..571e9e61b40e 100644
> --- a/kernel/debug/kdb/kdb_main.c
> +++ b/kernel/debug/kdb/kdb_main.c
> @@ -1265,8 +1265,8 @@ static int kdb_local(kdb_reason_t reason, int error, struct pt_regs *regs,
>  
>  do_full_getstr:
>  		/* PROMPT can only be set if we have MEM_READ permission. */
> -		snprintf(kdb_prompt_str, CMD_BUFLEN, kdbgetenv("PROMPT"),
> -			 raw_smp_processor_id());
> +		snprintf(kdb_prompt_str, sizeof(kdb_prompt_str),
> +			 kdbgetenv("PROMPT"), raw_smp_processor_id());
>  
>  		/*
>  		 * Fetch command from keyboard


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] kdb: use sizeof(kdb_prompt_str) instead of mismatched CMD_BUFLEN
  2026-06-16 16:44 [PATCH] kdb: use sizeof(kdb_prompt_str) instead of mismatched CMD_BUFLEN Naveen Kumar Chaudhary
  2026-06-16 20:20 ` David Laight
@ 2026-06-16 22:04 ` Doug Anderson
  2026-06-17  2:28   ` [PATCH v2] kdb: unify CMD_BUFLEN definition into kdb_private.h Naveen Kumar Chaudhary
  2026-06-17 10:43 ` [PATCH] kdb: use sizeof(kdb_prompt_str) instead of mismatched CMD_BUFLEN kernel test robot
  2026-06-17 19:49 ` kernel test robot
  3 siblings, 1 reply; 9+ messages in thread
From: Doug Anderson @ 2026-06-16 22:04 UTC (permalink / raw)
  To: Naveen Kumar Chaudhary
  Cc: jason.wessel, danielt, kgdb-bugreport, linux-kernel

Hi,

On Tue, Jun 16, 2026 at 9:45 AM Naveen Kumar Chaudhary
<naveen.osdev@gmail.com> wrote:
>
> kdb_main.c defines CMD_BUFLEN as 200 (for command history buffers),
> while kdb_io.c defines it as 256 (for kdb_prompt_str). The snprintf()
> filling kdb_prompt_str incorrectly used the local CMD_BUFLEN (200),
> truncating the prompt unnecessarily. Use sizeof(kdb_prompt_str) to
> always match the actual buffer size.
>
> Signed-off-by: Naveen Kumar Chaudhary <naveen.osdev@gmail.com>
> ---
>  kernel/debug/kdb/kdb_main.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/kernel/debug/kdb/kdb_main.c b/kernel/debug/kdb/kdb_main.c
> index ddce56b47b25..571e9e61b40e 100644
> --- a/kernel/debug/kdb/kdb_main.c
> +++ b/kernel/debug/kdb/kdb_main.c
> @@ -1265,8 +1265,8 @@ static int kdb_local(kdb_reason_t reason, int error, struct pt_regs *regs,
>
>  do_full_getstr:
>                 /* PROMPT can only be set if we have MEM_READ permission. */
> -               snprintf(kdb_prompt_str, CMD_BUFLEN, kdbgetenv("PROMPT"),
> -                        raw_smp_processor_id());
> +               snprintf(kdb_prompt_str, sizeof(kdb_prompt_str),
> +                        kdbgetenv("PROMPT"), raw_smp_processor_id());

Hmmm, I don't think so. My compiler yells at me for that:

  Invalid application of 'sizeof' to an incomplete type
'char[]'clang(sizeof_alignof_incomplete_or_sizeless_type)

...which makes sense since the variable is defined in a different
source file and the header has:

kernel/debug/kdb/kdb_private.h:extern char kdb_prompt_str[];

So there's a bug to fix, but I don't think your fix is quite right.
Instead, maybe you should have a single #define that's in a header and
used by both files?


-Doug

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] kdb: use sizeof(kdb_prompt_str) instead of mismatched CMD_BUFLEN
  2026-06-16 20:20 ` David Laight
@ 2026-06-16 22:06   ` Doug Anderson
  0 siblings, 0 replies; 9+ messages in thread
From: Doug Anderson @ 2026-06-16 22:06 UTC (permalink / raw)
  To: David Laight
  Cc: Naveen Kumar Chaudhary, jason.wessel, danielt, kgdb-bugreport,
	linux-kernel

Hi,

On Tue, Jun 16, 2026 at 1:20 PM David Laight
<david.laight.linux@gmail.com> wrote:
>
> On Tue, 16 Jun 2026 22:14:54 +0530
> Naveen Kumar Chaudhary <naveen.osdev@gmail.com> wrote:
>
> > kdb_main.c defines CMD_BUFLEN as 200 (for command history buffers),
> > while kdb_io.c defines it as 256 (for kdb_prompt_str). The snprintf()
> > filling kdb_prompt_str incorrectly used the local CMD_BUFLEN (200),
> > truncating the prompt unnecessarily. Use sizeof(kdb_prompt_str) to
> > always match the actual buffer size.
>
> As a matter of interest what sets the string that kdbgetenv("PROMPT")
> returns?
> If it is user settable, using it as a format string doesn't seem wise
> (even for kdbg).

For some history, see commit ad99b5105c08 ("kdb: Censor attempts to
set PROMPT without ENABLE_MEM_READ").

I have no idea how truly useful it is to be able to mess with your
prompt like this to begin with, but at least the "safety" of it has
been considered a little.

-Doug

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v2] kdb: unify CMD_BUFLEN definition into kdb_private.h
  2026-06-16 22:04 ` Doug Anderson
@ 2026-06-17  2:28   ` Naveen Kumar Chaudhary
  2026-06-17  3:00     ` Naveen Kumar Chaudhary
  2026-06-17 21:16     ` Doug Anderson
  0 siblings, 2 replies; 9+ messages in thread
From: Naveen Kumar Chaudhary @ 2026-06-17  2:28 UTC (permalink / raw)
  To: dianders, david.laight.linux
  Cc: jason.wessel, danielt, kgdb-bugreport, linux-kernel

CMD_BUFLEN was defined separately in kdb_io.c (256) and kdb_main.c
(200), causing kdb_main.c to use the wrong size when formatting the
prompt string into kdb_prompt_str (which is 256 bytes).

Move CMD_BUFLEN (256) into kdb_private.h so all users share a single
consistent definition, and remove the local definitions from both
files.

Fixes: 5d5314d6795f ("kdb: core for kgdb back end (1 of 2)")
Signed-off-by: Naveen Kumar Chaudhary <naveen.osdev@gmail.com>
---
 kernel/debug/kdb/kdb_io.c      | 1 -
 kernel/debug/kdb/kdb_main.c    | 6 ++----
 kernel/debug/kdb/kdb_private.h | 3 ++-
 3 files changed, 4 insertions(+), 6 deletions(-)

diff --git a/kernel/debug/kdb/kdb_io.c b/kernel/debug/kdb/kdb_io.c
index c399f11740ef..f5b1b7d4c9c8 100644
--- a/kernel/debug/kdb/kdb_io.c
+++ b/kernel/debug/kdb/kdb_io.c
@@ -22,7 +22,6 @@
 #include <linux/kallsyms.h>
 #include "kdb_private.h"
 
-#define CMD_BUFLEN 256
 char kdb_prompt_str[CMD_BUFLEN];
 
 int kdb_trap_printk;
diff --git a/kernel/debug/kdb/kdb_main.c b/kernel/debug/kdb/kdb_main.c
index ddce56b47b25..ca0126db9850 100644
--- a/kernel/debug/kdb/kdb_main.c
+++ b/kernel/debug/kdb/kdb_main.c
@@ -783,8 +783,6 @@ static int kdb_exec_defcmd(int argc, const char **argv)
 
 /* Command history */
 #define KDB_CMD_HISTORY_COUNT	32
-#define CMD_BUFLEN		200	/* kdb_printf: max printline
-					 * size == 256 */
 static unsigned int cmd_head, cmd_tail;
 static unsigned int cmdptr;
 static char cmd_hist[KDB_CMD_HISTORY_COUNT][CMD_BUFLEN];
@@ -1265,8 +1263,8 @@ static int kdb_local(kdb_reason_t reason, int error, struct pt_regs *regs,
 
 do_full_getstr:
 		/* PROMPT can only be set if we have MEM_READ permission. */
-		snprintf(kdb_prompt_str, CMD_BUFLEN, kdbgetenv("PROMPT"),
-			 raw_smp_processor_id());
+		snprintf(kdb_prompt_str, CMD_BUFLEN,
+			 kdbgetenv("PROMPT"), raw_smp_processor_id());
 
 		/*
 		 * Fetch command from keyboard
diff --git a/kernel/debug/kdb/kdb_private.h b/kernel/debug/kdb/kdb_private.h
index 92a28b8ab604..722e8aa50724 100644
--- a/kernel/debug/kdb/kdb_private.h
+++ b/kernel/debug/kdb/kdb_private.h
@@ -225,7 +225,8 @@ extern void kdb_kbd_cleanup_state(void);
 #define kdb_kbd_cleanup_state()
 #endif /* ! CONFIG_KDB_KEYBOARD */
 
-extern char kdb_prompt_str[];
+#define CMD_BUFLEN 256
+extern char kdb_prompt_str[CMD_BUFLEN];
 
 #define	KDB_WORD_SIZE	((int)sizeof(unsigned long))
 
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH v2] kdb: unify CMD_BUFLEN definition into kdb_private.h
  2026-06-17  2:28   ` [PATCH v2] kdb: unify CMD_BUFLEN definition into kdb_private.h Naveen Kumar Chaudhary
@ 2026-06-17  3:00     ` Naveen Kumar Chaudhary
  2026-06-17 21:16     ` Doug Anderson
  1 sibling, 0 replies; 9+ messages in thread
From: Naveen Kumar Chaudhary @ 2026-06-17  3:00 UTC (permalink / raw)
  To: dianders, david.laight.linux
  Cc: jason.wessel, danielt, kgdb-bugreport, linux-kernel

Thanks Doug for the review. Apologies, I missed to realize that my
config for kgdb_kdb was not enabled and hence missed that compilation
error. Have taken care this time with this new patch.

One concern I should mention about `cmd_hist[32][200]` earlier which was
32 entries × 200 = 6,400 bytes of static storage. Bumping to 256 would
make it 8,192 bytes — a ~28% increase in static memory. Though this
should be ok for a debugger. Alternatively, I was thinking to rename
these two differently so that they don't clash, plus we won't have this
memory bump. But it has its own quirks.

Assuming that 1.8KB is meaningless for an optional debugger and a single
definition is impossible to get wrong, the unified approach is the better
long-term choice. Please correct me in case I am wrong.

Regards,
Naveen

On Wed 17 Jun 07:58 AM, Naveen Kumar Chaudhary wrote:
> CMD_BUFLEN was defined separately in kdb_io.c (256) and kdb_main.c
> (200), causing kdb_main.c to use the wrong size when formatting the
> prompt string into kdb_prompt_str (which is 256 bytes).
> 
> Move CMD_BUFLEN (256) into kdb_private.h so all users share a single
> consistent definition, and remove the local definitions from both
> files.
> 
> Fixes: 5d5314d6795f ("kdb: core for kgdb back end (1 of 2)")
> Signed-off-by: Naveen Kumar Chaudhary <naveen.osdev@gmail.com>
> ---
>  kernel/debug/kdb/kdb_io.c      | 1 -
>  kernel/debug/kdb/kdb_main.c    | 6 ++----
>  kernel/debug/kdb/kdb_private.h | 3 ++-
>  3 files changed, 4 insertions(+), 6 deletions(-)
> 
> diff --git a/kernel/debug/kdb/kdb_io.c b/kernel/debug/kdb/kdb_io.c
> index c399f11740ef..f5b1b7d4c9c8 100644
> --- a/kernel/debug/kdb/kdb_io.c
> +++ b/kernel/debug/kdb/kdb_io.c
> @@ -22,7 +22,6 @@
>  #include <linux/kallsyms.h>
>  #include "kdb_private.h"
>  
> -#define CMD_BUFLEN 256
>  char kdb_prompt_str[CMD_BUFLEN];
>  
>  int kdb_trap_printk;
> diff --git a/kernel/debug/kdb/kdb_main.c b/kernel/debug/kdb/kdb_main.c
> index ddce56b47b25..ca0126db9850 100644
> --- a/kernel/debug/kdb/kdb_main.c
> +++ b/kernel/debug/kdb/kdb_main.c
> @@ -783,8 +783,6 @@ static int kdb_exec_defcmd(int argc, const char **argv)
>  
>  /* Command history */
>  #define KDB_CMD_HISTORY_COUNT	32
> -#define CMD_BUFLEN		200	/* kdb_printf: max printline
> -					 * size == 256 */
>  static unsigned int cmd_head, cmd_tail;
>  static unsigned int cmdptr;
>  static char cmd_hist[KDB_CMD_HISTORY_COUNT][CMD_BUFLEN];
> @@ -1265,8 +1263,8 @@ static int kdb_local(kdb_reason_t reason, int error, struct pt_regs *regs,
>  
>  do_full_getstr:
>  		/* PROMPT can only be set if we have MEM_READ permission. */
> -		snprintf(kdb_prompt_str, CMD_BUFLEN, kdbgetenv("PROMPT"),
> -			 raw_smp_processor_id());
> +		snprintf(kdb_prompt_str, CMD_BUFLEN,
> +			 kdbgetenv("PROMPT"), raw_smp_processor_id());
>  
>  		/*
>  		 * Fetch command from keyboard
> diff --git a/kernel/debug/kdb/kdb_private.h b/kernel/debug/kdb/kdb_private.h
> index 92a28b8ab604..722e8aa50724 100644
> --- a/kernel/debug/kdb/kdb_private.h
> +++ b/kernel/debug/kdb/kdb_private.h
> @@ -225,7 +225,8 @@ extern void kdb_kbd_cleanup_state(void);
>  #define kdb_kbd_cleanup_state()
>  #endif /* ! CONFIG_KDB_KEYBOARD */
>  
> -extern char kdb_prompt_str[];
> +#define CMD_BUFLEN 256
> +extern char kdb_prompt_str[CMD_BUFLEN];
>  
>  #define	KDB_WORD_SIZE	((int)sizeof(unsigned long))
>  
> -- 
> 2.43.0
> 

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] kdb: use sizeof(kdb_prompt_str) instead of mismatched CMD_BUFLEN
  2026-06-16 16:44 [PATCH] kdb: use sizeof(kdb_prompt_str) instead of mismatched CMD_BUFLEN Naveen Kumar Chaudhary
  2026-06-16 20:20 ` David Laight
  2026-06-16 22:04 ` Doug Anderson
@ 2026-06-17 10:43 ` kernel test robot
  2026-06-17 19:49 ` kernel test robot
  3 siblings, 0 replies; 9+ messages in thread
From: kernel test robot @ 2026-06-17 10:43 UTC (permalink / raw)
  To: Naveen Kumar Chaudhary, jason.wessel, danielt, dianders
  Cc: llvm, oe-kbuild-all, kgdb-bugreport, linux-kernel

Hi Naveen,

kernel test robot noticed the following build errors:

[auto build test ERROR on v7.1]
[also build test ERROR on linus/master next-20260616]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Naveen-Kumar-Chaudhary/kdb-use-sizeof-kdb_prompt_str-instead-of-mismatched-CMD_BUFLEN/20260617-055312
base:   v7.1
patch link:    https://lore.kernel.org/r/uqjlxgcu6y6ukayda7jka7ji73ctkj4f3632rejud6cqqayfwx%40kuyf2f2lvett
patch subject: [PATCH] kdb: use sizeof(kdb_prompt_str) instead of mismatched CMD_BUFLEN
config: hexagon-randconfig-001-20260617 (https://download.01.org/0day-ci/archive/20260617/202606171818.9AqxpkQ1-lkp@intel.com/config)
compiler: clang version 22.1.3 (https://github.com/llvm/llvm-project e9846648fd6183ee6d8cbdb4502213fcf902a211)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260617/202606171818.9AqxpkQ1-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202606171818.9AqxpkQ1-lkp@intel.com/

All errors (new ones prefixed by >>):

>> kernel/debug/kdb/kdb_main.c:1268:34: error: invalid application of 'sizeof' to an incomplete type 'char[]'
    1268 |                 snprintf(kdb_prompt_str, sizeof(kdb_prompt_str),
         |                                                ^~~~~~~~~~~~~~~~
   1 error generated.


vim +1268 kernel/debug/kdb/kdb_main.c

  1124	
  1125	/*
  1126	 * kdb_local - The main code for kdb.  This routine is invoked on a
  1127	 *	specific processor, it is not global.  The main kdb() routine
  1128	 *	ensures that only one processor at a time is in this routine.
  1129	 *	This code is called with the real reason code on the first
  1130	 *	entry to a kdb session, thereafter it is called with reason
  1131	 *	SWITCH, even if the user goes back to the original cpu.
  1132	 * Inputs:
  1133	 *	reason		The reason KDB was invoked
  1134	 *	error		The hardware-defined error code
  1135	 *	regs		The exception frame at time of fault/breakpoint.
  1136	 *	db_result	Result code from the break or debug point.
  1137	 * Returns:
  1138	 *	0	KDB was invoked for an event which it wasn't responsible
  1139	 *	1	KDB handled the event for which it was invoked.
  1140	 *	KDB_CMD_GO	User typed 'go'.
  1141	 *	KDB_CMD_CPU	User switched to another cpu.
  1142	 *	KDB_CMD_SS	Single step.
  1143	 */
  1144	static int kdb_local(kdb_reason_t reason, int error, struct pt_regs *regs,
  1145			     kdb_dbtrap_t db_result)
  1146	{
  1147		char *cmdbuf;
  1148		int diag;
  1149		struct task_struct *kdb_current =
  1150			curr_task(raw_smp_processor_id());
  1151	
  1152		KDB_DEBUG_STATE("kdb_local 1", reason);
  1153	
  1154		kdb_check_for_lockdown();
  1155	
  1156		kdb_go_count = 0;
  1157		if (reason == KDB_REASON_DEBUG) {
  1158			/* special case below */
  1159		} else {
  1160			kdb_printf("\nEntering kdb (current=0x%px, pid %d) ",
  1161				   kdb_current, kdb_current ? kdb_current->pid : 0);
  1162	#if defined(CONFIG_SMP)
  1163			kdb_printf("on processor %d ", raw_smp_processor_id());
  1164	#endif
  1165		}
  1166	
  1167		switch (reason) {
  1168		case KDB_REASON_DEBUG:
  1169		{
  1170			/*
  1171			 * If re-entering kdb after a single step
  1172			 * command, don't print the message.
  1173			 */
  1174			switch (db_result) {
  1175			case KDB_DB_BPT:
  1176				kdb_printf("\nEntering kdb (0x%px, pid %d) ",
  1177					   kdb_current, kdb_current->pid);
  1178	#if defined(CONFIG_SMP)
  1179				kdb_printf("on processor %d ", raw_smp_processor_id());
  1180	#endif
  1181				kdb_printf("due to Debug @ " kdb_machreg_fmt "\n",
  1182					   instruction_pointer(regs));
  1183				break;
  1184			case KDB_DB_SS:
  1185				break;
  1186			case KDB_DB_SSBPT:
  1187				KDB_DEBUG_STATE("kdb_local 4", reason);
  1188				return 1;	/* kdba_db_trap did the work */
  1189			default:
  1190				kdb_printf("kdb: Bad result from kdba_db_trap: %d\n",
  1191					   db_result);
  1192				break;
  1193			}
  1194	
  1195		}
  1196			break;
  1197		case KDB_REASON_ENTER:
  1198			if (KDB_STATE(KEYBOARD))
  1199				kdb_printf("due to Keyboard Entry\n");
  1200			else
  1201				kdb_printf("due to KDB_ENTER()\n");
  1202			break;
  1203		case KDB_REASON_KEYBOARD:
  1204			KDB_STATE_SET(KEYBOARD);
  1205			kdb_printf("due to Keyboard Entry\n");
  1206			break;
  1207		case KDB_REASON_ENTER_SLAVE:
  1208			/* drop through, slaves only get released via cpu switch */
  1209		case KDB_REASON_SWITCH:
  1210			kdb_printf("due to cpu switch\n");
  1211			break;
  1212		case KDB_REASON_OOPS:
  1213			kdb_printf("Oops: %s\n", kdb_diemsg);
  1214			kdb_printf("due to oops @ " kdb_machreg_fmt "\n",
  1215				   instruction_pointer(regs));
  1216			kdb_dumpregs(regs);
  1217			break;
  1218		case KDB_REASON_SYSTEM_NMI:
  1219			kdb_printf("due to System NonMaskable Interrupt\n");
  1220			break;
  1221		case KDB_REASON_NMI:
  1222			kdb_printf("due to NonMaskable Interrupt @ "
  1223				   kdb_machreg_fmt "\n",
  1224				   instruction_pointer(regs));
  1225			break;
  1226		case KDB_REASON_SSTEP:
  1227		case KDB_REASON_BREAK:
  1228			kdb_printf("due to %s @ " kdb_machreg_fmt "\n",
  1229				   reason == KDB_REASON_BREAK ?
  1230				   "Breakpoint" : "SS trap", instruction_pointer(regs));
  1231			/*
  1232			 * Determine if this breakpoint is one that we
  1233			 * are interested in.
  1234			 */
  1235			if (db_result != KDB_DB_BPT) {
  1236				kdb_printf("kdb: error return from kdba_bp_trap: %d\n",
  1237					   db_result);
  1238				KDB_DEBUG_STATE("kdb_local 6", reason);
  1239				return 0;	/* Not for us, dismiss it */
  1240			}
  1241			break;
  1242		case KDB_REASON_RECURSE:
  1243			kdb_printf("due to Recursion @ " kdb_machreg_fmt "\n",
  1244				   instruction_pointer(regs));
  1245			break;
  1246		default:
  1247			kdb_printf("kdb: unexpected reason code: %d\n", reason);
  1248			KDB_DEBUG_STATE("kdb_local 8", reason);
  1249			return 0;	/* Not for us, dismiss it */
  1250		}
  1251	
  1252		while (1) {
  1253			/*
  1254			 * Initialize pager context.
  1255			 */
  1256			kdb_nextline = 1;
  1257			KDB_STATE_CLEAR(SUPPRESS);
  1258			kdb_grepping_flag = 0;
  1259			/* ensure the old search does not leak into '/' commands */
  1260			kdb_grep_string[0] = '\0';
  1261	
  1262			cmdbuf = cmd_cur;
  1263			*cmdbuf = '\0';
  1264			*(cmd_hist[cmd_head]) = '\0';
  1265	
  1266	do_full_getstr:
  1267			/* PROMPT can only be set if we have MEM_READ permission. */
> 1268			snprintf(kdb_prompt_str, sizeof(kdb_prompt_str),
  1269				 kdbgetenv("PROMPT"), raw_smp_processor_id());
  1270	
  1271			/*
  1272			 * Fetch command from keyboard
  1273			 */
  1274			cmdbuf = kdb_getstr(cmdbuf, CMD_BUFLEN, kdb_prompt_str);
  1275			if (*cmdbuf != '\n') {
  1276				if (*cmdbuf < 32) {
  1277					if (cmdptr == cmd_head) {
  1278						strscpy(cmd_hist[cmd_head], cmd_cur,
  1279							CMD_BUFLEN);
  1280						*(cmd_hist[cmd_head] +
  1281						  strlen(cmd_hist[cmd_head])-1) = '\0';
  1282					}
  1283					if (!handle_ctrl_cmd(cmdbuf))
  1284						*(cmd_cur+strlen(cmd_cur)-1) = '\0';
  1285					cmdbuf = cmd_cur;
  1286					goto do_full_getstr;
  1287				} else {
  1288					strscpy(cmd_hist[cmd_head], cmd_cur,
  1289						CMD_BUFLEN);
  1290				}
  1291	
  1292				cmd_head = (cmd_head+1) % KDB_CMD_HISTORY_COUNT;
  1293				if (cmd_head == cmd_tail)
  1294					cmd_tail = (cmd_tail+1) % KDB_CMD_HISTORY_COUNT;
  1295			}
  1296	
  1297			cmdptr = cmd_head;
  1298			diag = kdb_parse(cmdbuf);
  1299			if (diag == KDB_NOTFOUND) {
  1300				drop_newline(cmdbuf);
  1301				kdb_printf("Unknown kdb command: '%s'\n", cmdbuf);
  1302				diag = 0;
  1303			}
  1304			if (diag == KDB_CMD_GO
  1305			 || diag == KDB_CMD_CPU
  1306			 || diag == KDB_CMD_SS
  1307			 || diag == KDB_CMD_KGDB)
  1308				break;
  1309	
  1310			if (diag)
  1311				kdb_cmderror(diag);
  1312		}
  1313		KDB_DEBUG_STATE("kdb_local 9", diag);
  1314		return diag;
  1315	}
  1316	

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] kdb: use sizeof(kdb_prompt_str) instead of mismatched CMD_BUFLEN
  2026-06-16 16:44 [PATCH] kdb: use sizeof(kdb_prompt_str) instead of mismatched CMD_BUFLEN Naveen Kumar Chaudhary
                   ` (2 preceding siblings ...)
  2026-06-17 10:43 ` [PATCH] kdb: use sizeof(kdb_prompt_str) instead of mismatched CMD_BUFLEN kernel test robot
@ 2026-06-17 19:49 ` kernel test robot
  3 siblings, 0 replies; 9+ messages in thread
From: kernel test robot @ 2026-06-17 19:49 UTC (permalink / raw)
  To: Naveen Kumar Chaudhary, jason.wessel, danielt, dianders
  Cc: oe-kbuild-all, kgdb-bugreport, linux-kernel

Hi Naveen,

kernel test robot noticed the following build errors:

[auto build test ERROR on v7.1]
[also build test ERROR on linus/master next-20260616]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Naveen-Kumar-Chaudhary/kdb-use-sizeof-kdb_prompt_str-instead-of-mismatched-CMD_BUFLEN/20260617-055312
base:   v7.1
patch link:    https://lore.kernel.org/r/uqjlxgcu6y6ukayda7jka7ji73ctkj4f3632rejud6cqqayfwx%40kuyf2f2lvett
patch subject: [PATCH] kdb: use sizeof(kdb_prompt_str) instead of mismatched CMD_BUFLEN
config: sh-allyesconfig (https://download.01.org/0day-ci/archive/20260618/202606180316.efxdStG5-lkp@intel.com/config)
compiler: sh4-linux-gcc (GCC) 16.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260618/202606180316.efxdStG5-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202606180316.efxdStG5-lkp@intel.com/

All errors (new ones prefixed by >>):

   kernel/debug/kdb/kdb_main.c: In function 'kdb_local':
>> kernel/debug/kdb/kdb_main.c:1268:48: error: invalid application of 'sizeof' to incomplete type 'char[]'
    1268 |                 snprintf(kdb_prompt_str, sizeof(kdb_prompt_str),
         |                                                ^


vim +1268 kernel/debug/kdb/kdb_main.c

  1124	
  1125	/*
  1126	 * kdb_local - The main code for kdb.  This routine is invoked on a
  1127	 *	specific processor, it is not global.  The main kdb() routine
  1128	 *	ensures that only one processor at a time is in this routine.
  1129	 *	This code is called with the real reason code on the first
  1130	 *	entry to a kdb session, thereafter it is called with reason
  1131	 *	SWITCH, even if the user goes back to the original cpu.
  1132	 * Inputs:
  1133	 *	reason		The reason KDB was invoked
  1134	 *	error		The hardware-defined error code
  1135	 *	regs		The exception frame at time of fault/breakpoint.
  1136	 *	db_result	Result code from the break or debug point.
  1137	 * Returns:
  1138	 *	0	KDB was invoked for an event which it wasn't responsible
  1139	 *	1	KDB handled the event for which it was invoked.
  1140	 *	KDB_CMD_GO	User typed 'go'.
  1141	 *	KDB_CMD_CPU	User switched to another cpu.
  1142	 *	KDB_CMD_SS	Single step.
  1143	 */
  1144	static int kdb_local(kdb_reason_t reason, int error, struct pt_regs *regs,
  1145			     kdb_dbtrap_t db_result)
  1146	{
  1147		char *cmdbuf;
  1148		int diag;
  1149		struct task_struct *kdb_current =
  1150			curr_task(raw_smp_processor_id());
  1151	
  1152		KDB_DEBUG_STATE("kdb_local 1", reason);
  1153	
  1154		kdb_check_for_lockdown();
  1155	
  1156		kdb_go_count = 0;
  1157		if (reason == KDB_REASON_DEBUG) {
  1158			/* special case below */
  1159		} else {
  1160			kdb_printf("\nEntering kdb (current=0x%px, pid %d) ",
  1161				   kdb_current, kdb_current ? kdb_current->pid : 0);
  1162	#if defined(CONFIG_SMP)
  1163			kdb_printf("on processor %d ", raw_smp_processor_id());
  1164	#endif
  1165		}
  1166	
  1167		switch (reason) {
  1168		case KDB_REASON_DEBUG:
  1169		{
  1170			/*
  1171			 * If re-entering kdb after a single step
  1172			 * command, don't print the message.
  1173			 */
  1174			switch (db_result) {
  1175			case KDB_DB_BPT:
  1176				kdb_printf("\nEntering kdb (0x%px, pid %d) ",
  1177					   kdb_current, kdb_current->pid);
  1178	#if defined(CONFIG_SMP)
  1179				kdb_printf("on processor %d ", raw_smp_processor_id());
  1180	#endif
  1181				kdb_printf("due to Debug @ " kdb_machreg_fmt "\n",
  1182					   instruction_pointer(regs));
  1183				break;
  1184			case KDB_DB_SS:
  1185				break;
  1186			case KDB_DB_SSBPT:
  1187				KDB_DEBUG_STATE("kdb_local 4", reason);
  1188				return 1;	/* kdba_db_trap did the work */
  1189			default:
  1190				kdb_printf("kdb: Bad result from kdba_db_trap: %d\n",
  1191					   db_result);
  1192				break;
  1193			}
  1194	
  1195		}
  1196			break;
  1197		case KDB_REASON_ENTER:
  1198			if (KDB_STATE(KEYBOARD))
  1199				kdb_printf("due to Keyboard Entry\n");
  1200			else
  1201				kdb_printf("due to KDB_ENTER()\n");
  1202			break;
  1203		case KDB_REASON_KEYBOARD:
  1204			KDB_STATE_SET(KEYBOARD);
  1205			kdb_printf("due to Keyboard Entry\n");
  1206			break;
  1207		case KDB_REASON_ENTER_SLAVE:
  1208			/* drop through, slaves only get released via cpu switch */
  1209		case KDB_REASON_SWITCH:
  1210			kdb_printf("due to cpu switch\n");
  1211			break;
  1212		case KDB_REASON_OOPS:
  1213			kdb_printf("Oops: %s\n", kdb_diemsg);
  1214			kdb_printf("due to oops @ " kdb_machreg_fmt "\n",
  1215				   instruction_pointer(regs));
  1216			kdb_dumpregs(regs);
  1217			break;
  1218		case KDB_REASON_SYSTEM_NMI:
  1219			kdb_printf("due to System NonMaskable Interrupt\n");
  1220			break;
  1221		case KDB_REASON_NMI:
  1222			kdb_printf("due to NonMaskable Interrupt @ "
  1223				   kdb_machreg_fmt "\n",
  1224				   instruction_pointer(regs));
  1225			break;
  1226		case KDB_REASON_SSTEP:
  1227		case KDB_REASON_BREAK:
  1228			kdb_printf("due to %s @ " kdb_machreg_fmt "\n",
  1229				   reason == KDB_REASON_BREAK ?
  1230				   "Breakpoint" : "SS trap", instruction_pointer(regs));
  1231			/*
  1232			 * Determine if this breakpoint is one that we
  1233			 * are interested in.
  1234			 */
  1235			if (db_result != KDB_DB_BPT) {
  1236				kdb_printf("kdb: error return from kdba_bp_trap: %d\n",
  1237					   db_result);
  1238				KDB_DEBUG_STATE("kdb_local 6", reason);
  1239				return 0;	/* Not for us, dismiss it */
  1240			}
  1241			break;
  1242		case KDB_REASON_RECURSE:
  1243			kdb_printf("due to Recursion @ " kdb_machreg_fmt "\n",
  1244				   instruction_pointer(regs));
  1245			break;
  1246		default:
  1247			kdb_printf("kdb: unexpected reason code: %d\n", reason);
  1248			KDB_DEBUG_STATE("kdb_local 8", reason);
  1249			return 0;	/* Not for us, dismiss it */
  1250		}
  1251	
  1252		while (1) {
  1253			/*
  1254			 * Initialize pager context.
  1255			 */
  1256			kdb_nextline = 1;
  1257			KDB_STATE_CLEAR(SUPPRESS);
  1258			kdb_grepping_flag = 0;
  1259			/* ensure the old search does not leak into '/' commands */
  1260			kdb_grep_string[0] = '\0';
  1261	
  1262			cmdbuf = cmd_cur;
  1263			*cmdbuf = '\0';
  1264			*(cmd_hist[cmd_head]) = '\0';
  1265	
  1266	do_full_getstr:
  1267			/* PROMPT can only be set if we have MEM_READ permission. */
> 1268			snprintf(kdb_prompt_str, sizeof(kdb_prompt_str),
  1269				 kdbgetenv("PROMPT"), raw_smp_processor_id());
  1270	
  1271			/*
  1272			 * Fetch command from keyboard
  1273			 */
  1274			cmdbuf = kdb_getstr(cmdbuf, CMD_BUFLEN, kdb_prompt_str);
  1275			if (*cmdbuf != '\n') {
  1276				if (*cmdbuf < 32) {
  1277					if (cmdptr == cmd_head) {
  1278						strscpy(cmd_hist[cmd_head], cmd_cur,
  1279							CMD_BUFLEN);
  1280						*(cmd_hist[cmd_head] +
  1281						  strlen(cmd_hist[cmd_head])-1) = '\0';
  1282					}
  1283					if (!handle_ctrl_cmd(cmdbuf))
  1284						*(cmd_cur+strlen(cmd_cur)-1) = '\0';
  1285					cmdbuf = cmd_cur;
  1286					goto do_full_getstr;
  1287				} else {
  1288					strscpy(cmd_hist[cmd_head], cmd_cur,
  1289						CMD_BUFLEN);
  1290				}
  1291	
  1292				cmd_head = (cmd_head+1) % KDB_CMD_HISTORY_COUNT;
  1293				if (cmd_head == cmd_tail)
  1294					cmd_tail = (cmd_tail+1) % KDB_CMD_HISTORY_COUNT;
  1295			}
  1296	
  1297			cmdptr = cmd_head;
  1298			diag = kdb_parse(cmdbuf);
  1299			if (diag == KDB_NOTFOUND) {
  1300				drop_newline(cmdbuf);
  1301				kdb_printf("Unknown kdb command: '%s'\n", cmdbuf);
  1302				diag = 0;
  1303			}
  1304			if (diag == KDB_CMD_GO
  1305			 || diag == KDB_CMD_CPU
  1306			 || diag == KDB_CMD_SS
  1307			 || diag == KDB_CMD_KGDB)
  1308				break;
  1309	
  1310			if (diag)
  1311				kdb_cmderror(diag);
  1312		}
  1313		KDB_DEBUG_STATE("kdb_local 9", diag);
  1314		return diag;
  1315	}
  1316	

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2] kdb: unify CMD_BUFLEN definition into kdb_private.h
  2026-06-17  2:28   ` [PATCH v2] kdb: unify CMD_BUFLEN definition into kdb_private.h Naveen Kumar Chaudhary
  2026-06-17  3:00     ` Naveen Kumar Chaudhary
@ 2026-06-17 21:16     ` Doug Anderson
  1 sibling, 0 replies; 9+ messages in thread
From: Doug Anderson @ 2026-06-17 21:16 UTC (permalink / raw)
  To: Naveen Kumar Chaudhary
  Cc: david.laight.linux, jason.wessel, danielt, kgdb-bugreport,
	linux-kernel

Hi,

On Tue, Jun 16, 2026 at 7:28 PM Naveen Kumar Chaudhary
<naveen.osdev@gmail.com> wrote:
>
> CMD_BUFLEN was defined separately in kdb_io.c (256) and kdb_main.c
> (200), causing kdb_main.c to use the wrong size when formatting the
> prompt string into kdb_prompt_str (which is 256 bytes).
>
> Move CMD_BUFLEN (256) into kdb_private.h so all users share a single
> consistent definition, and remove the local definitions from both
> files.
>
> Fixes: 5d5314d6795f ("kdb: core for kgdb back end (1 of 2)")
> Signed-off-by: Naveen Kumar Chaudhary <naveen.osdev@gmail.com>
> ---
>  kernel/debug/kdb/kdb_io.c      | 1 -
>  kernel/debug/kdb/kdb_main.c    | 6 ++----
>  kernel/debug/kdb/kdb_private.h | 3 ++-
>  3 files changed, 4 insertions(+), 6 deletions(-)
>
> diff --git a/kernel/debug/kdb/kdb_io.c b/kernel/debug/kdb/kdb_io.c
> index c399f11740ef..f5b1b7d4c9c8 100644
> --- a/kernel/debug/kdb/kdb_io.c
> +++ b/kernel/debug/kdb/kdb_io.c
> @@ -22,7 +22,6 @@
>  #include <linux/kallsyms.h>
>  #include "kdb_private.h"
>
> -#define CMD_BUFLEN 256
>  char kdb_prompt_str[CMD_BUFLEN];
>
>  int kdb_trap_printk;
> diff --git a/kernel/debug/kdb/kdb_main.c b/kernel/debug/kdb/kdb_main.c
> index ddce56b47b25..ca0126db9850 100644
> --- a/kernel/debug/kdb/kdb_main.c
> +++ b/kernel/debug/kdb/kdb_main.c
> @@ -783,8 +783,6 @@ static int kdb_exec_defcmd(int argc, const char **argv)
>
>  /* Command history */
>  #define KDB_CMD_HISTORY_COUNT  32
> -#define CMD_BUFLEN             200     /* kdb_printf: max printline
> -                                        * size == 256 */

Maybe Daniel will know more; otherwise, I need to spend more time
digging. ...but the comment above (that you're deleting) makes me
believe that 200 was purposely chosen to be a number that was under
256. It sounds as if maybe they're keeping some buffers at 200 so that
there'e enough extra space to print the buffer plus some extra stuff?

Maybe safer to keep the number at 200?


>  static unsigned int cmd_head, cmd_tail;
>  static unsigned int cmdptr;
>  static char cmd_hist[KDB_CMD_HISTORY_COUNT][CMD_BUFLEN];
> @@ -1265,8 +1263,8 @@ static int kdb_local(kdb_reason_t reason, int error, struct pt_regs *regs,
>
>  do_full_getstr:
>                 /* PROMPT can only be set if we have MEM_READ permission. */
> -               snprintf(kdb_prompt_str, CMD_BUFLEN, kdbgetenv("PROMPT"),
> -                        raw_smp_processor_id());
> +               snprintf(kdb_prompt_str, CMD_BUFLEN,
> +                        kdbgetenv("PROMPT"), raw_smp_processor_id());

Unrelated whitespace change. Drop from your patch.


>                 /*
>                  * Fetch command from keyboard
> diff --git a/kernel/debug/kdb/kdb_private.h b/kernel/debug/kdb/kdb_private.h
> index 92a28b8ab604..722e8aa50724 100644
> --- a/kernel/debug/kdb/kdb_private.h
> +++ b/kernel/debug/kdb/kdb_private.h
> @@ -225,7 +225,8 @@ extern void kdb_kbd_cleanup_state(void);
>  #define kdb_kbd_cleanup_state()
>  #endif /* ! CONFIG_KDB_KEYBOARD */
>
> -extern char kdb_prompt_str[];
> +#define CMD_BUFLEN 256
> +extern char kdb_prompt_str[CMD_BUFLEN];

Now that this is in a header file, a slightly less generic name would
be good. Maybe rename to KDB_BUFLEN"

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2026-06-17 21:16 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-16 16:44 [PATCH] kdb: use sizeof(kdb_prompt_str) instead of mismatched CMD_BUFLEN Naveen Kumar Chaudhary
2026-06-16 20:20 ` David Laight
2026-06-16 22:06   ` Doug Anderson
2026-06-16 22:04 ` Doug Anderson
2026-06-17  2:28   ` [PATCH v2] kdb: unify CMD_BUFLEN definition into kdb_private.h Naveen Kumar Chaudhary
2026-06-17  3:00     ` Naveen Kumar Chaudhary
2026-06-17 21:16     ` Doug Anderson
2026-06-17 10:43 ` [PATCH] kdb: use sizeof(kdb_prompt_str) instead of mismatched CMD_BUFLEN kernel test robot
2026-06-17 19:49 ` kernel test robot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.