Skip to contentSkip to conversation

kdb: Get rid of custom debug heap allocator

PATCH Superseded Sumit Garg 2 participants 7 messages Feb 26, 2021 – Mar 23, 2021 KGDB / KDB /debug_core

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Sumit Garg

author
#0 lore raw

PATCH v2 1/1 kdb: Get rid of custom debug heap allocator

Currently the only user for debug heap is kdbnearsym() which can be modified to rather ask the caller to supply a buffer for symbol name. So do that and modify kdbnearsym() callers to pass a symbol name buffer allocated statically and hence remove custom debug heap allocator.

This change has been tested using kgdbtest on arm64 which doesn't show any regressions.

---

Changes in v2:
- Allocate namebuf statically instead of stack to maintain debugger robustness.

 kernel/debug/kdb/kdb_debugger.c |   1 -
 kernel/debug/kdb/kdb_main.c     |   6 +-
 kernel/debug/kdb/kdb_private.h  |   7 +-
 kernel/debug/kdb/kdb_support.c  | 294 +-------------------------------
 4 files changed, 11 insertions(+), 297 deletions(-)
kernel/debug/kdb/kdb_debugger.c +0 -1
index 0220afda3200..e91fc3e4edd5 100644
@@ -140,7 +140,6 @@ int kdb_stub(struct kgdb_state *ks)
 	 */
 	kdb_common_deinit_state();
 	KDB_STATE_CLEAR(PAGER);
-	kdbnearsym_cleanup();
 	if (error == KDB_CMD_KGDB) {
 		if (KDB_STATE(DOING_KGDB))
 			KDB_STATE_CLEAR(DOING_KGDB);
kernel/debug/kdb/kdb_main.c +4 -2
index 9d69169582c6..6efe9ec53906 100644
@@ -526,6 +526,7 @@ int kdbgetaddrarg(int argc, const char **argv, int *nextarg,
 	char symbol = '\0';
 	char *cp;
 	kdb_symtab_t symtab;
+	static char namebuf[KSYM_NAME_LEN];
 
 	/*
 	 * If the enable flags prohibit both arbitrary memory access
@@ -585,7 +586,7 @@ int kdbgetaddrarg(int argc, const char **argv, int *nextarg,
 	}
 
 	if (!found)
-		found = kdbnearsym(addr, &symtab);
+		found = kdbnearsym(addr, &symtab, namebuf);
 
 	(*nextarg)++;
 
@@ -1503,6 +1504,7 @@ static void kdb_md_line(const char *fmtstr, unsigned long addr,
 	int i;
 	int j;
 	unsigned long word;
+	static char namebuf[KSYM_NAME_LEN];
 
 	memset(cbuf, '\0', sizeof(cbuf));
 	if (phys)
@@ -1518,7 +1520,7 @@ static void kdb_md_line(const char *fmtstr, unsigned long addr,
 			break;
 		kdb_printf(fmtstr, word);
 		if (symbolic)
-			kdbnearsym(word, &symtab);
+			kdbnearsym(word, &symtab, namebuf);
 		else
 			memset(&symtab, 0, sizeof(symtab));
 		if (symtab.sym_name) {
kernel/debug/kdb/kdb_private.h +1 -6
index b857a84de3b5..1707eeebc59a 100644
@@ -108,8 +108,7 @@ extern char *kdbgetenv(const char *);
 extern int kdbgetaddrarg(int, const char **, int*, unsigned long *,
 			 long *, char **);
 extern int kdbgetsymval(const char *, kdb_symtab_t *);
-extern int kdbnearsym(unsigned long, kdb_symtab_t *);
-extern void kdbnearsym_cleanup(void);
+extern int kdbnearsym(unsigned long addr, kdb_symtab_t *symtab, char *namebuf);
 extern char *kdb_strdup(const char *str, gfp_t type);
 extern void kdb_symbol_print(unsigned long, const kdb_symtab_t *, unsigned int);
 
@@ -233,10 +232,6 @@ extern struct task_struct *kdb_curr_task(int);
 
 #define GFP_KDB (in_dbg_master() ? GFP_ATOMIC : GFP_KERNEL)
 
-extern void *debug_kmalloc(size_t size, gfp_t flags);
-extern void debug_kfree(void *);
-extern void debug_kusage(void);
-
 extern struct task_struct *kdb_current_task;
 extern struct pt_regs *kdb_current_regs;
 
kernel/debug/kdb/kdb_support.c +6 -288

349 lines not on this page · raw message

Signature · 1 line
2.25.1
#1 lore raw
Quoted 4 lines · Sumit Garg

Currently the only user for debug heap is kdbnearsym() which can be modified to rather ask the caller to supply a buffer for symbol name. So do that and modify kdbnearsym() callers to pass a symbol name buffer allocated statically and hence remove custom debug heap allocator.

Why make the callers do this?

The LRU buffers were managed inside kdbnearsym() why does switching to an approach with a single buffer require us to push that buffer out to the callers?

kernel/debug/kdb/kdb_main.c int kdbgetaddrarg(int argc, const char **argv, int *nextarg,

Quoted 5 lines
kernel/debug/kdb/kdb_main.c +0 -0
index 9d69169582c6..6efe9ec53906 100644
@@ -526,6 +526,7 @@ int kdbgetaddrarg(int argc, const char **argv, int *nextarg,

The documentation comment for this function has not been updated to describe the new contract on callers of this function (e.g. if they consume the symbol name they must do so before calling kdbgetaddrarg() (and maybe kdbnearsym() again).

Quoted 12 lines
kernel/debug/kdb/kdb_main.c +1 -0
 	char symbol = '\0';
 	char *cp;
 	kdb_symtab_t symtab;
+	static char namebuf[KSYM_NAME_LEN];
 
 	/*
 	 * If the enable flags prohibit both arbitrary memory access
kernel/debug/kdb/kdb_support.c +0 -0
index b59aad1f0b55..9b907a84f2db 100644

kernel/debug/kdb/kdb_support.c int kdbgetsymval(const char *symname, kdb_symtab_t *symtab) · line 62

Quoted 8 lines
kernel/debug/kdb/kdb_support.c +0 -2
@@ -57,8 +57,6 @@ int kdbgetsymval(const char *symname, kdb_symtab_t *symtab)
 }
 EXPORT_SYMBOL(kdbgetsymval);
 
-static char *kdb_name_table[100];	/* arbitrary size */
-
 /*
  * kdbnearsym -	Return the name of the symbol with the nearest address
  *	less than 'addr'.

Again the documentation comment has not been updated and, in this case, is now misleading.

If we move the static buffer here then the remarks section on this function is a really good place to describe what the callers must do to manage the static buffer safely as well as a convenient place to mention that we tolerate the reuse of the static buffer if kdb is re-entered becase a) kdb is broken if that happens and b) we are crash resilient if if does.

kernel/debug/kdb/kdb_support.c static char *kdb_name_table[100]; /* arbitrary size */ · line 80

Quoted 6 lines
kernel/debug/kdb/kdb_support.c +1 -1
@@ -79,13 +77,11 @@ static char *kdb_name_table[100];	/* arbitrary size */
  *	hold active strings, no kdb caller of kdbnearsym makes more
  *	than ~20 later calls before using a saved value.
  */
-int kdbnearsym(unsigned long addr, kdb_symtab_t *symtab)
+int kdbnearsym(unsigned long addr, kdb_symtab_t *symtab, char *namebuf)

As above, I don't understand why we need to add namebuf here. I think the prototype can remain the same.

Think of it simple that we have reduce the cache from having 100 entries to having just 1 ;-) .

Daniel.

author
#2 lore raw
Quoted 7 lines · Daniel Thompson
Quoted 4 lines · Sumit Garg

Currently the only user for debug heap is kdbnearsym() which can be modified to rather ask the caller to supply a buffer for symbol name. So do that and modify kdbnearsym() callers to pass a symbol name buffer allocated statically and hence remove custom debug heap allocator.

Quoted 6 lines

Why make the callers do this?

The LRU buffers were managed inside kdbnearsym() why does switching to an approach with a single buffer require us to push that buffer out to the callers?

Earlier the LRU buffers managed namebuf uniqueness per caller (upto 100 callers) but if we switch to single entry in kdbnearsym() then all callers need to share common buffer which will lead to incorrect results from following simple sequence:

kdbnearsym(word, &symtab1);
kdbnearsym(word, &symtab2);
kdb_symbol_print(word, &symtab1, 0);
kdb_symbol_print(word, &symtab2, 0);

But if we change to a unique static namebuf per caller then the following sequence will work:

kdbnearsym(word, &symtab1, namebuf1);
kdbnearsym(word, &symtab2, namebuf2);
kdb_symbol_print(word, &symtab1, 0);
kdb_symbol_print(word, &symtab2, 0);

Quoted 12 lines

kernel/debug/kdb/kdb_main.c int kdbgetaddrarg(int argc, const char **argv, int *nextarg,

Quoted 6 lines
kernel/debug/kdb/kdb_main.c +0 -0
index 9d69169582c6..6efe9ec53906 100644
@@ -526,6 +526,7 @@ int kdbgetaddrarg(int argc, const char **argv, int *nextarg,

The documentation comment for this function has not been updated to describe the new contract on callers of this function (e.g. if they consume the symbol name they must do so before calling kdbgetaddrarg() (and maybe kdbnearsym() again).

I am not sure if I follow you here. If we have a unique static buffer per caller then why do we need this new contract?

Quoted 16 lines
Quoted 16 lines
kernel/debug/kdb/kdb_main.c +1 -0
      char symbol = '\0';
      char *cp;
      kdb_symtab_t symtab;
+     static char namebuf[KSYM_NAME_LEN];
 
      /*
       * If the enable flags prohibit both arbitrary memory access
kernel/debug/kdb/kdb_support.c +0 -0
index b59aad1f0b55..9b907a84f2db 100644
@@ -57,8 +57,6 @@ int kdbgetsymval(const char *symname, kdb_symtab_t *symtab)
 }
 EXPORT_SYMBOL(kdbgetsymval);
 
Quoted 8 lines

kernel/debug/kdb/kdb_support.c int kdbgetsymval(const char *symname, kdb_symtab_t *symtab) · line 62

Quoted 5 lines
kernel/debug/kdb/kdb_support.c +0 -2
-static char *kdb_name_table[100];    /* arbitrary size */
-
 /*
  * kdbnearsym -      Return the name of the symbol with the nearest address
  *   less than 'addr'.

Again the documentation comment has not been updated and, in this case, is now misleading.

Okay, I will fix it.

Quoted 19 lines

If we move the static buffer here then the remarks section on this function is a really good place to describe what the callers must do to manage the static buffer safely as well as a convenient place to mention that we tolerate the reuse of the static buffer if kdb is re-entered becase a) kdb is broken if that happens and b) we are crash resilient if if does.

kernel/debug/kdb/kdb_support.c static char *kdb_name_table[100]; /* arbitrary size */ · line 80

Quoted 6 lines
kernel/debug/kdb/kdb_support.c +1 -1
@@ -79,13 +77,11 @@ static char *kdb_name_table[100]; /* arbitrary size */
  *   hold active strings, no kdb caller of kdbnearsym makes more
  *   than ~20 later calls before using a saved value.
  */
-int kdbnearsym(unsigned long addr, kdb_symtab_t *symtab)
+int kdbnearsym(unsigned long addr, kdb_symtab_t *symtab, char *namebuf)

As above, I don't understand why we need to add namebuf here. I think the prototype can remain the same.

Quoted 2 lines

Think of it simple that we have reduce the cache from having 100 entries to having just 1 ;-) .

Please see my response above.

-Sumit

Quoted 3 lines

Daniel.

#3 lore raw
Quoted 11 lines · Sumit Garg
Quoted 9 lines · Daniel Thompson
Quoted 4 lines · Sumit Garg

Currently the only user for debug heap is kdbnearsym() which can be modified to rather ask the caller to supply a buffer for symbol name. So do that and modify kdbnearsym() callers to pass a symbol name buffer allocated statically and hence remove custom debug heap allocator.

Why make the callers do this?

Quoted 7 lines
Quoted 4 lines

The LRU buffers were managed inside kdbnearsym() why does switching to an approach with a single buffer require us to push that buffer out to the callers?

Earlier the LRU buffers managed namebuf uniqueness per caller (upto 100 callers)

The uniqueness is per symbol, not per caller.

Quoted 9 lines

but if we switch to single entry in kdbnearsym() then all callers need to share common buffer which will lead to incorrect results from following simple sequence:

kdbnearsym(word, &symtab1);
kdbnearsym(word, &symtab2);
kdb_symbol_print(word, &symtab1, 0);
kdb_symbol_print(word, &symtab2, 0);

Quoted 7 lines

But if we change to a unique static namebuf per caller then the following sequence will work:

kdbnearsym(word, &symtab1, namebuf1);
kdbnearsym(word, &symtab2, namebuf2);
kdb_symbol_print(word, &symtab1, 0);
kdb_symbol_print(word, &symtab2, 0);

This is true but do any of the callers of kdbnearsym ever do this? The main reaason that heap stuck out as redundant was that I've only ever seen the output of kdbnearsym() consumed almost immediately by a print.

I wrote an early version of a patch like this that just shrunk the LRU cache down to 2 and avoided any heap usage... but I threw it away when I realized we never carry cached values outside the function that obtained them.

Quoted 15 lines
Quoted 14 lines

kernel/debug/kdb/kdb_main.c int kdbgetaddrarg(int argc, const char **argv, int *nextarg,

Quoted 8 lines
(quoted diff) +0 -0
@@ -526,6 +526,7 @@ int kdbgetaddrarg(int argc, const char **argv, int *nextarg,
 
 
kernel/debug/kdb/kdb_main.c +0 -0
index 9d69169582c6..6efe9ec53906 100644
@@ -526,6 +526,7 @@ int kdbgetaddrarg(int argc, const char **argv, int *nextarg,

The documentation comment for this function has not been updated to describe the new contract on callers of this function (e.g. if they consume the symbol name they must do so before calling kdbgetaddrarg() (and maybe kdbnearsym() again).

Quoted 2 lines

I am not sure if I follow you here. If we have a unique static buffer per caller then why do we need this new contract?

I traced the code wrong. I thought it shared symtab->sym_name with its own caller... but it doesn't it shares synname with its caller and that's totally different...

Daniel.

Quoted 57 lines
Quoted 17 lines
Quoted 17 lines
kernel/debug/kdb/kdb_main.c +1 -0
      char symbol = '\0';
      char *cp;
      kdb_symtab_t symtab;
+     static char namebuf[KSYM_NAME_LEN];
 
      /*
       * If the enable flags prohibit both arbitrary memory access
kernel/debug/kdb/kdb_support.c +0 -0
index b59aad1f0b55..9b907a84f2db 100644
@@ -57,8 +57,6 @@ int kdbgetsymval(const char *symname, kdb_symtab_t *symtab)
 }
 EXPORT_SYMBOL(kdbgetsymval);
 
Quoted 8 lines

kernel/debug/kdb/kdb_support.c int kdbgetsymval(const char *symname, kdb_symtab_t *symtab) · line 62

Quoted 5 lines
kernel/debug/kdb/kdb_support.c +0 -2
-static char *kdb_name_table[100];    /* arbitrary size */
-
 /*
  * kdbnearsym -      Return the name of the symbol with the nearest address
  *   less than 'addr'.

Again the documentation comment has not been updated and, in this case, is now misleading.

Okay, I will fix it.

Quoted 19 lines

If we move the static buffer here then the remarks section on this function is a really good place to describe what the callers must do to manage the static buffer safely as well as a convenient place to mention that we tolerate the reuse of the static buffer if kdb is re-entered becase a) kdb is broken if that happens and b) we are crash resilient if if does.

kernel/debug/kdb/kdb_support.c static char *kdb_name_table[100]; /* arbitrary size */ · line 80

Quoted 6 lines
kernel/debug/kdb/kdb_support.c +1 -1
@@ -79,13 +77,11 @@ static char *kdb_name_table[100]; /* arbitrary size */
  *   hold active strings, no kdb caller of kdbnearsym makes more
  *   than ~20 later calls before using a saved value.
  */
-int kdbnearsym(unsigned long addr, kdb_symtab_t *symtab)
+int kdbnearsym(unsigned long addr, kdb_symtab_t *symtab, char *namebuf)

As above, I don't understand why we need to add namebuf here. I think the prototype can remain the same.

Quoted 2 lines

Think of it simple that we have reduce the cache from having 100 entries to having just 1 ;-) .

Please see my response above.

-Sumit

Quoted 3 lines

Daniel.

author
#4 lore raw
Quoted 18 lines · Daniel Thompson
Quoted 16 lines · Sumit Garg
Quoted 13 lines · Daniel Thompson
Quoted 4 lines · Sumit Garg

Currently the only user for debug heap is kdbnearsym() which can be modified to rather ask the caller to supply a buffer for symbol name. So do that and modify kdbnearsym() callers to pass a symbol name buffer allocated statically and hence remove custom debug heap allocator.

Why make the callers do this?

The LRU buffers were managed inside kdbnearsym() why does switching to an approach with a single buffer require us to push that buffer out to the callers?

Quoted 5 lines
Quoted 2 lines

Earlier the LRU buffers managed namebuf uniqueness per caller (upto 100 callers)

The uniqueness is per symbol, not per caller.

Agree.

Quoted 12 lines
Quoted 12 lines

but if we switch to single entry in kdbnearsym() then all callers need to share common buffer which will lead to incorrect results from following simple sequence:

kdbnearsym(word, &symtab1);
kdbnearsym(word, &symtab2);
kdb_symbol_print(word, &symtab1, 0);
kdb_symbol_print(word, &symtab2, 0);

But if we change to a unique static namebuf per caller then the following sequence will work:

Quoted 6 lines
Quoted 4 lines

kdbnearsym(word, &symtab1, namebuf1);
kdbnearsym(word, &symtab2, namebuf2);
kdb_symbol_print(word, &symtab1, 0);
kdb_symbol_print(word, &symtab2, 0);

This is true but do any of the callers of kdbnearsym ever do this?

No, but any of prospective callers may need this.

Quoted 4 lines

The
main reaason that heap stuck out as redundant was that I've only ever seen the output of kdbnearsym() consumed almost immediately by a print.

Yeah but I think the alternative proposed in this patch isn't as burdensome as the heap and tries to somewhat match existing functionality.

Quoted 5 lines

I wrote an early version of a patch like this that just shrunk the LRU cache down to 2 and avoided any heap usage... but I threw it away when I realized we never carry cached values outside the function that obtained them.

Okay, so if you still think that having a single static buffer inside kdbnearsym() is an appropriate approach for time being then I will switch to use that instead.

-Sumit

Quoted 83 lines
Quoted 16 lines
Quoted 4 lines
Quoted 2 lines
(quoted diff) +0 -0
@@ -526,6 +526,7 @@ int kdbgetaddrarg(int argc, const char **argv, int *nextarg,
Quoted 11 lines

kernel/debug/kdb/kdb_main.c int kdbgetaddrarg(int argc, const char **argv, int *nextarg,

Quoted 5 lines
kernel/debug/kdb/kdb_main.c +0 -0
index 9d69169582c6..6efe9ec53906 100644
@@ -526,6 +526,7 @@ int kdbgetaddrarg(int argc, const char **argv, int *nextarg,

The documentation comment for this function has not been updated to describe the new contract on callers of this function (e.g. if they consume the symbol name they must do so before calling kdbgetaddrarg() (and maybe kdbnearsym() again).

Quoted 2 lines

I am not sure if I follow you here. If we have a unique static buffer per caller then why do we need this new contract?

I traced the code wrong. I thought it shared symtab->sym_name with its own caller... but it doesn't it shares synname with its caller and that's totally different...

Daniel.

Quoted 57 lines
Quoted 17 lines
Quoted 17 lines
kernel/debug/kdb/kdb_main.c +1 -0
      char symbol = '\0';
      char *cp;
      kdb_symtab_t symtab;
+     static char namebuf[KSYM_NAME_LEN];
 
      /*
       * If the enable flags prohibit both arbitrary memory access
kernel/debug/kdb/kdb_support.c +0 -0
index b59aad1f0b55..9b907a84f2db 100644
@@ -57,8 +57,6 @@ int kdbgetsymval(const char *symname, kdb_symtab_t *symtab)
 }
 EXPORT_SYMBOL(kdbgetsymval);
 
Quoted 8 lines

kernel/debug/kdb/kdb_support.c int kdbgetsymval(const char *symname, kdb_symtab_t *symtab) · line 62

Quoted 5 lines
kernel/debug/kdb/kdb_support.c +0 -2
-static char *kdb_name_table[100];    /* arbitrary size */
-
 /*
  * kdbnearsym -      Return the name of the symbol with the nearest address
  *   less than 'addr'.

Again the documentation comment has not been updated and, in this case, is now misleading.

Okay, I will fix it.

Quoted 19 lines

If we move the static buffer here then the remarks section on this function is a really good place to describe what the callers must do to manage the static buffer safely as well as a convenient place to mention that we tolerate the reuse of the static buffer if kdb is re-entered becase a) kdb is broken if that happens and b) we are crash resilient if if does.

kernel/debug/kdb/kdb_support.c static char *kdb_name_table[100]; /* arbitrary size */ · line 80

Quoted 6 lines
kernel/debug/kdb/kdb_support.c +1 -1
@@ -79,13 +77,11 @@ static char *kdb_name_table[100]; /* arbitrary size */
  *   hold active strings, no kdb caller of kdbnearsym makes more
  *   than ~20 later calls before using a saved value.
  */
-int kdbnearsym(unsigned long addr, kdb_symtab_t *symtab)
+int kdbnearsym(unsigned long addr, kdb_symtab_t *symtab, char *namebuf)

As above, I don't understand why we need to add namebuf here. I think the prototype can remain the same.

Quoted 2 lines

Think of it simple that we have reduce the cache from having 100 entries to having just 1 ;-) .

Please see my response above.

-Sumit

Quoted 3 lines

Daniel.

#5 lore raw
Quoted 64 lines · Sumit Garg
Quoted 18 lines · Daniel Thompson
Quoted 16 lines · Sumit Garg
Quoted 13 lines · Daniel Thompson
Quoted 4 lines · Sumit Garg

Currently the only user for debug heap is kdbnearsym() which can be modified to rather ask the caller to supply a buffer for symbol name. So do that and modify kdbnearsym() callers to pass a symbol name buffer allocated statically and hence remove custom debug heap allocator.

Why make the callers do this?

The LRU buffers were managed inside kdbnearsym() why does switching to an approach with a single buffer require us to push that buffer out to the callers?

Quoted 5 lines
Quoted 2 lines

Earlier the LRU buffers managed namebuf uniqueness per caller (upto 100 callers)

The uniqueness is per symbol, not per caller.

Agree.

Quoted 12 lines
Quoted 12 lines

but if we switch to single entry in kdbnearsym() then all callers need to share common buffer which will lead to incorrect results from following simple sequence:

kdbnearsym(word, &symtab1);
kdbnearsym(word, &symtab2);
kdb_symbol_print(word, &symtab1, 0);
kdb_symbol_print(word, &symtab2, 0);

But if we change to a unique static namebuf per caller then the following sequence will work:

Quoted 6 lines
Quoted 4 lines

kdbnearsym(word, &symtab1, namebuf1);
kdbnearsym(word, &symtab2, namebuf2);
kdb_symbol_print(word, &symtab1, 0);
kdb_symbol_print(word, &symtab2, 0);

This is true but do any of the callers of kdbnearsym ever do this?

No, but any of prospective callers may need this.

Quoted 4 lines

The
main reaason that heap stuck out as redundant was that I've only ever seen the output of kdbnearsym() consumed almost immediately by a print.

Yeah but I think the alternative proposed in this patch isn't as burdensome as the heap and tries to somewhat match existing functionality.

Quoted 5 lines

I wrote an early version of a patch like this that just shrunk the LRU cache down to 2 and avoided any heap usage... but I threw it away when I realized we never carry cached values outside the function that obtained them.

Quoted 3 lines

Okay, so if you still think that having a single static buffer inside kdbnearsym() is an appropriate approach for time being then I will switch to use that instead.

Sorry to drop this thread for so long.

On reflection I still have a few concerns about the current code. To be clear this is not really about wasting 128 bytes of RAM (your patch saves 256K after all).

It's more that the current static buffers "look weird". They are static so any competent OS programmer reads them and thinks "but what about concurrency/reentrancy"). With the static buffers scattered through the code they don't have a single place to find the answer.

I originally proposed handling this by the static buffer horror in kdbnearsym() and describing how it all works in the header comment! As much as anything this was to centralize the commentary in the contract for calling kdbnearsym(). Hence nobody should write the theoretic bug you describe because they read the contract!

You are welcome to counter propose but you must ensure that there are equivalent comments so our "competent OS programmer" from the paragraph above can figure out how the static buffer works without having to run git blame` and digging out the patch history.

Daniel.

Quoted 86 lines

-Sumit

Quoted 83 lines
Quoted 16 lines
Quoted 4 lines
Quoted 2 lines
(quoted diff) +0 -0
@@ -526,6 +526,7 @@ int kdbgetaddrarg(int argc, const char **argv, int *nextarg,
Quoted 11 lines

kernel/debug/kdb/kdb_main.c int kdbgetaddrarg(int argc, const char **argv, int *nextarg,

Quoted 5 lines
kernel/debug/kdb/kdb_main.c +0 -0
index 9d69169582c6..6efe9ec53906 100644
@@ -526,6 +526,7 @@ int kdbgetaddrarg(int argc, const char **argv, int *nextarg,

The documentation comment for this function has not been updated to describe the new contract on callers of this function (e.g. if they consume the symbol name they must do so before calling kdbgetaddrarg() (and maybe kdbnearsym() again).

Quoted 2 lines

I am not sure if I follow you here. If we have a unique static buffer per caller then why do we need this new contract?

I traced the code wrong. I thought it shared symtab->sym_name with its own caller... but it doesn't it shares synname with its caller and that's totally different...

Daniel.

Quoted 57 lines
Quoted 17 lines
Quoted 17 lines
kernel/debug/kdb/kdb_main.c +1 -0
      char symbol = '\0';
      char *cp;
      kdb_symtab_t symtab;
+     static char namebuf[KSYM_NAME_LEN];
 
      /*
       * If the enable flags prohibit both arbitrary memory access
kernel/debug/kdb/kdb_support.c +0 -0
index b59aad1f0b55..9b907a84f2db 100644
@@ -57,8 +57,6 @@ int kdbgetsymval(const char *symname, kdb_symtab_t *symtab)
 }
 EXPORT_SYMBOL(kdbgetsymval);
 
Quoted 8 lines

kernel/debug/kdb/kdb_support.c int kdbgetsymval(const char *symname, kdb_symtab_t *symtab) · line 62

Quoted 5 lines
kernel/debug/kdb/kdb_support.c +0 -2
-static char *kdb_name_table[100];    /* arbitrary size */
-
 /*
  * kdbnearsym -      Return the name of the symbol with the nearest address
  *   less than 'addr'.

Again the documentation comment has not been updated and, in this case, is now misleading.

Okay, I will fix it.

Quoted 19 lines

If we move the static buffer here then the remarks section on this function is a really good place to describe what the callers must do to manage the static buffer safely as well as a convenient place to mention that we tolerate the reuse of the static buffer if kdb is re-entered becase a) kdb is broken if that happens and b) we are crash resilient if if does.

kernel/debug/kdb/kdb_support.c static char *kdb_name_table[100]; /* arbitrary size */ · line 80

Quoted 6 lines
kernel/debug/kdb/kdb_support.c +1 -1
@@ -79,13 +77,11 @@ static char *kdb_name_table[100]; /* arbitrary size */
  *   hold active strings, no kdb caller of kdbnearsym makes more
  *   than ~20 later calls before using a saved value.
  */
-int kdbnearsym(unsigned long addr, kdb_symtab_t *symtab)
+int kdbnearsym(unsigned long addr, kdb_symtab_t *symtab, char *namebuf)

As above, I don't understand why we need to add namebuf here. I think the prototype can remain the same.

Quoted 2 lines

Think of it simple that we have reduce the cache from having 100 entries to having just 1 ;-) .

Please see my response above.

-Sumit

Quoted 3 lines

Daniel.

author
#6 lore raw
Quoted 87 lines · Daniel Thompson
Quoted 64 lines · Sumit Garg
Quoted 18 lines · Daniel Thompson
Quoted 16 lines · Sumit Garg
Quoted 13 lines · Daniel Thompson
Quoted 4 lines · Sumit Garg

Currently the only user for debug heap is kdbnearsym() which can be modified to rather ask the caller to supply a buffer for symbol name. So do that and modify kdbnearsym() callers to pass a symbol name buffer allocated statically and hence remove custom debug heap allocator.

Why make the callers do this?

The LRU buffers were managed inside kdbnearsym() why does switching to an approach with a single buffer require us to push that buffer out to the callers?

Quoted 5 lines
Quoted 2 lines

Earlier the LRU buffers managed namebuf uniqueness per caller (upto 100 callers)

The uniqueness is per symbol, not per caller.

Agree.

Quoted 12 lines
Quoted 12 lines

but if we switch to single entry in kdbnearsym() then all callers need to share common buffer which will lead to incorrect results from following simple sequence:

kdbnearsym(word, &symtab1);
kdbnearsym(word, &symtab2);
kdb_symbol_print(word, &symtab1, 0);
kdb_symbol_print(word, &symtab2, 0);

But if we change to a unique static namebuf per caller then the following sequence will work:

Quoted 6 lines
Quoted 4 lines

kdbnearsym(word, &symtab1, namebuf1);
kdbnearsym(word, &symtab2, namebuf2);
kdb_symbol_print(word, &symtab1, 0);
kdb_symbol_print(word, &symtab2, 0);

This is true but do any of the callers of kdbnearsym ever do this?

No, but any of prospective callers may need this.

Quoted 4 lines

The
main reaason that heap stuck out as redundant was that I've only ever seen the output of kdbnearsym() consumed almost immediately by a print.

Yeah but I think the alternative proposed in this patch isn't as burdensome as the heap and tries to somewhat match existing functionality.

Quoted 5 lines

I wrote an early version of a patch like this that just shrunk the LRU cache down to 2 and avoided any heap usage... but I threw it away when I realized we never carry cached values outside the function that obtained them.

Quoted 3 lines

Okay, so if you still think that having a single static buffer inside kdbnearsym() is an appropriate approach for time being then I will switch to use that instead.

Sorry to drop this thread for so long.

On reflection I still have a few concerns about the current code. To be clear this is not really about wasting 128 bytes of RAM (your patch saves 256K after all).

It's more that the current static buffers "look weird". They are static so any competent OS programmer reads them and thinks "but what about concurrency/reentrancy"). With the static buffers scattered through the code they don't have a single place to find the answer.

I originally proposed handling this by the static buffer horror in kdbnearsym() and describing how it all works in the header comment! As much as anything this was to centralize the commentary in the contract for calling kdbnearsym(). Hence nobody should write the theoretic bug you describe because they read the contract!

Quoted 5 lines

You are welcome to counter propose but you must ensure that there are equivalent comments so our "competent OS programmer" from the paragraph above can figure out how the static buffer works without having to run git blame` and digging out the patch history.

Okay, I understand your point here. Let me go ahead with a single static buffer in kdbnearsym() with a proper header comment.

-Sumit

Quoted 91 lines

Daniel.

Quoted 86 lines

-Sumit

Quoted 83 lines
Quoted 16 lines
Quoted 4 lines
Quoted 2 lines
(quoted diff) +0 -0
@@ -526,6 +526,7 @@ int kdbgetaddrarg(int argc, const char **argv, int *nextarg,
Quoted 11 lines

kernel/debug/kdb/kdb_main.c int kdbgetaddrarg(int argc, const char **argv, int *nextarg,

Quoted 5 lines
kernel/debug/kdb/kdb_main.c +0 -0
index 9d69169582c6..6efe9ec53906 100644
@@ -526,6 +526,7 @@ int kdbgetaddrarg(int argc, const char **argv, int *nextarg,

The documentation comment for this function has not been updated to describe the new contract on callers of this function (e.g. if they consume the symbol name they must do so before calling kdbgetaddrarg() (and maybe kdbnearsym() again).

Quoted 2 lines

I am not sure if I follow you here. If we have a unique static buffer per caller then why do we need this new contract?

I traced the code wrong. I thought it shared symtab->sym_name with its own caller... but it doesn't it shares synname with its caller and that's totally different...

Daniel.

Quoted 57 lines
Quoted 17 lines
Quoted 17 lines
kernel/debug/kdb/kdb_main.c +1 -0
      char symbol = '\0';
      char *cp;
      kdb_symtab_t symtab;
+     static char namebuf[KSYM_NAME_LEN];
 
      /*
       * If the enable flags prohibit both arbitrary memory access
kernel/debug/kdb/kdb_support.c +0 -0
index b59aad1f0b55..9b907a84f2db 100644
@@ -57,8 +57,6 @@ int kdbgetsymval(const char *symname, kdb_symtab_t *symtab)
 }
 EXPORT_SYMBOL(kdbgetsymval);
 
Quoted 8 lines

kernel/debug/kdb/kdb_support.c int kdbgetsymval(const char *symname, kdb_symtab_t *symtab) · line 62

Quoted 5 lines
kernel/debug/kdb/kdb_support.c +0 -2
-static char *kdb_name_table[100];    /* arbitrary size */
-
 /*
  * kdbnearsym -      Return the name of the symbol with the nearest address
  *   less than 'addr'.

Again the documentation comment has not been updated and, in this case, is now misleading.

Okay, I will fix it.

Quoted 19 lines

If we move the static buffer here then the remarks section on this function is a really good place to describe what the callers must do to manage the static buffer safely as well as a convenient place to mention that we tolerate the reuse of the static buffer if kdb is re-entered becase a) kdb is broken if that happens and b) we are crash resilient if if does.

kernel/debug/kdb/kdb_support.c static char *kdb_name_table[100]; /* arbitrary size */ · line 80

Quoted 6 lines
kernel/debug/kdb/kdb_support.c +1 -1
@@ -79,13 +77,11 @@ static char *kdb_name_table[100]; /* arbitrary size */
  *   hold active strings, no kdb caller of kdbnearsym makes more
  *   than ~20 later calls before using a saved value.
  */
-int kdbnearsym(unsigned long addr, kdb_symtab_t *symtab)
+int kdbnearsym(unsigned long addr, kdb_symtab_t *symtab, char *namebuf)

As above, I don't understand why we need to add namebuf here. I think the prototype can remain the same.

Quoted 2 lines

Think of it simple that we have reduce the cache from having 100 entries to having just 1 ;-) .

Please see my response above.

-Sumit

Quoted 3 lines

Daniel.