kdb: Get rid of custom debug heap allocator
PATCH v3 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 use statically allocated buffer for symbol name as per it's current usage. So do that and hence remove custom debug heap allocator.
Note that this change puts a restriction on kdbnearsym() callers to carefully use shared namebuf such that a caller should consume the symbol returned immediately prior to another call to fetch a different symbol.
This change has been tested using kgdbtest on arm64 which doesn't show any regressions.
---
Changes in v3:
- Rebased to tip of upstream master.
- Updated function header comment for kdbnearsym().
Changes in v2:
- Use single static buffer for symbol name in kdbnearsym() instead of per caller buffers allocated on stack.
kernel/debug/kdb/kdb_debugger.c | 1 - kernel/debug/kdb/kdb_private.h | 5 - kernel/debug/kdb/kdb_support.c | 328 +++----------------------------- 3 files changed, 28 insertions(+), 306 deletions(-)
kernel/debug/kdb/kdb_debugger.c +0 -1
@@ -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_private.h +0 -5
@@ -109,7 +109,6 @@ 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 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 +28 -300
375 lines not on this page · raw message
Signature · 1 line
2.25.1
Hi,
kernel/debug/kdb/kdb_support.c int kdbgetsymval(const char *symname, kdb_symtab_t *symtab) · line 58
Quoted 11 lines · Sumit Garg
kernel/debug/kdb/kdb_support.c +0
-3@@ -233,10 +232,6 @@ extern struct task_struct *kdb_curr_task(int);@@ -52,48 +52,48 @@ 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
nit: This is now kernel-doc, right? So start with "/**" ?
Quoted 40 lines
kernel/debug/kdb/kdb_support.c +23
-16-* less than 'addr'. +* kdbnearsym() - Return the name of the symbol with the nearest address +* less than @addr. +* @addr: Address to check for near symbol +* @symtab: Structure to receive results * -* Parameters: -* addr Address to check for symbol near -* symtab Structure to receive results -* Returns: -* 0 No sections contain this address, symtab zero filled -* 1 Address mapped to module/symbol/section, data in symtab -* Remarks: -* 2.6 kallsyms has a "feature" where it unpacks the name into a -* string. If that string is reused before the caller expects it -* then the caller sees its string change without warning. To -* avoid cluttering up the main kdb code with lots of kdb_strdup, -* tests and kfree calls, kdbnearsym maintains an LRU list of the -* last few unique strings. The list is sized large enough to -* hold active strings, no kdb caller of kdbnearsym makes more -* than ~20 later calls before using a saved value. +* WARNING: This function may return a pointer to a single statically +* allocated buffer (namebuf). kdb's unusual calling context (single +* threaded, all other CPUs halted) provides us sufficient locking for +* this to be safe. The only constraint imposed by the static buffer is +* that the caller must consume any previous reply prior to another call +* to lookup a new symbol. +* +* Note that, strictly speaking, some architectures may re-enter the kdb +* trap if the system turns out to be very badly damaged and this breaks +* the single-threaded assumption above. In these circumstances successful +* continuation and exit from the inner trap is unlikely to work and any +* user attempting this receives a prominent warning before being allowed +* to progress. In these circumstances we remain memory safe because +* namebuf[KSYM_NAME_LEN-1] will never change from '\0' although we do +* tolerate the possibility of garbled symbol display from the outer kdb +* trap. +* +* Return: +* * 0 - No sections contain this address, symtab zero filled
kernel/debug/kdb/kdb_support.c int kdbgetsymval(const char *symname, kdb_symtab_t *symtab) · line 87
Quoted 10 lines
kernel/debug/kdb/kdb_support.c +2
-2+* * 1 - Address mapped to module/symbol/section, data in symtab */ int kdbnearsym (unsigned long addr,kdb_symtab_t *symtab) {int ret =0 ;unsigned long symbolsize =0 ;unsigned long offset =0 ; -#define knt1_size 128/* must be >= kallsyms table size */ -char *knt1 =NULL ; +static char namebuf[KSYM_NAME_LEN ];
I guess this also ends up fixing a bug too, right? My greps show that "KSYM_NAME_LEN" is 512 and the first thing kallsyms_lookup() does it zero out byte 511. Previously we were only allocating 128 bytes so I guess we were writing past the end.
Assuming I understood correctly, maybe mention the bugfix in the commit text?
Quoted 28 lines
kernel/debug/kdb/kdb_support.c +0
-24@@ -102,63 +102,14 @@ int kdbnearsym(unsigned long addr, kdb_symtab_t *symtab) symtab->sym_end = symtab->sym_start + symbolsize; ret = symtab->sym_name !=NULL && *(symtab->sym_name) !='\0' ; -if (ret) { -int i; -/* Another 2.6 kallsyms "feature". Sometimes the sym_name is -* set but the buffer passed into kallsyms_lookup is not used, -* so it contains garbage. The caller has to work out which -* buffer needs to be saved. -* -* What was Rusty smoking when he wrote that code? -*/ -if (symtab->sym_name != knt1) { -strncpy (knt1, symtab->sym_name, knt1_size); - knt1[knt1_size-1 ] ='\0' ; - } -for (i =0 ; i <ARRAY_SIZE (kdb_name_table); ++i) { -if (kdb_name_table[i] && -strcmp (kdb_name_table[i], knt1) ==0 ) -break ; - } -if (i >=ARRAY_SIZE (kdb_name_table)) { -debug_kfree (kdb_name_table[0 ]); -memmove (kdb_name_table, kdb_name_table+1 , -sizeof (kdb_name_table[0 ]) * - (ARRAY_SIZE (kdb_name_table)-1 )); - }else {
kernel/debug/kdb/kdb_support.c int kdbnearsym(unsigned long addr, kdb_symtab_t *symtab) · line 138
Quoted 10 lines
kernel/debug/kdb/kdb_support.c +0
-10-debug_kfree (knt1); - knt1 = kdb_name_table[i]; -memmove (kdb_name_table+i, kdb_name_table+i+1 , -sizeof (kdb_name_table[0 ]) * - (ARRAY_SIZE (kdb_name_table)-i-1 )); - } - i =ARRAY_SIZE (kdb_name_table) -1 ; - kdb_name_table[i] = knt1; - symtab->sym_name = kdb_name_table[i]; - knt1 =NULL ;
I definitely had a hard time following exactly what all the cases were handling and if they were all right, but I agree that we can kill the old code (yay!)
kernel/debug/kdb/kdb_support.c void kdb_symbol_print(unsigned long addr, const kdb_symtab_t *symtab_p, · line 206
Quoted 8 lines
kernel/debug/kdb/kdb_support.c +1
-0@@ -249,6 +200,7 @@ void kdb_symbol_print(unsigned long addr, const kdb_symtab_t *symtab_p, unsigned int punc) {kdb_symtab_t symtab, *symtab_p2; +if (symtab_p) { symtab_p2 = (kdb_symtab_t *)symtab_p; }else {
nit: unrelated whitespace change?
All comments above are nits and not terribly important, so:
Hi Doug,
Thanks for your review.
Quoted 11 lines · Doug Anderson
Hi,
Quoted 7 lines · Sumit Garg
kernel/debug/kdb/kdb_support.c +0
-0@@ -233,10 +232,6 @@ extern struct task_struct *kdb_curr_task(int);@@ -52,48 +52,48 @@ int kdbgetsymval(const char *symname, kdb_symtab_t *symtab) }EXPORT_SYMBOL (kdbgetsymval);
Quoted 7 lines
kernel/debug/kdb/kdb_support.c int kdbgetsymval(const char *symname, kdb_symtab_t *symtab) · line 58
Quoted 4 lines
kernel/debug/kdb/kdb_support.c +0
-3-static char *kdb_name_table[100 ];/* arbitrary size */ -/* -* kdbnearsym - Return the name of the symbol with the nearest address nit: This is now kernel-doc, right? So start with "/**" ?
Ack.
Quoted 51 lines
Quoted 40 lines
kernel/debug/kdb/kdb_support.c +23
-16-* less than 'addr'. +* kdbnearsym() - Return the name of the symbol with the nearest address +* less than @addr. +* @addr: Address to check for near symbol +* @symtab: Structure to receive results * -* Parameters: -* addr Address to check for symbol near -* symtab Structure to receive results -* Returns: -* 0 No sections contain this address, symtab zero filled -* 1 Address mapped to module/symbol/section, data in symtab -* Remarks: -* 2.6 kallsyms has a "feature" where it unpacks the name into a -* string. If that string is reused before the caller expects it -* then the caller sees its string change without warning. To -* avoid cluttering up the main kdb code with lots of kdb_strdup, -* tests and kfree calls, kdbnearsym maintains an LRU list of the -* last few unique strings. The list is sized large enough to -* hold active strings, no kdb caller of kdbnearsym makes more -* than ~20 later calls before using a saved value. +* WARNING: This function may return a pointer to a single statically +* allocated buffer (namebuf). kdb's unusual calling context (single +* threaded, all other CPUs halted) provides us sufficient locking for +* this to be safe. The only constraint imposed by the static buffer is +* that the caller must consume any previous reply prior to another call +* to lookup a new symbol. +* +* Note that, strictly speaking, some architectures may re-enter the kdb +* trap if the system turns out to be very badly damaged and this breaks +* the single-threaded assumption above. In these circumstances successful +* continuation and exit from the inner trap is unlikely to work and any +* user attempting this receives a prominent warning before being allowed +* to progress. In these circumstances we remain memory safe because +* namebuf[KSYM_NAME_LEN-1] will never change from '\0' although we do +* tolerate the possibility of garbled symbol display from the outer kdb +* trap. +* +* Return: +* * 0 - No sections contain this address, symtab zero filled kernel/debug/kdb/kdb_support.c int kdbgetsymval(const char *symname, kdb_symtab_t *symtab) · line 87
Quoted 10 lines
kernel/debug/kdb/kdb_support.c +2
-2+* * 1 - Address mapped to module/symbol/section, data in symtab */ int kdbnearsym (unsigned long addr,kdb_symtab_t *symtab) {int ret =0 ;unsigned long symbolsize =0 ;unsigned long offset =0 ; -#define knt1_size 128/* must be >= kallsyms table size */ -char *knt1 =NULL ; +static char namebuf[KSYM_NAME_LEN ];
Quoted 2 lines
I guess this also ends up fixing a bug too, right? My greps show that "
KSYM_NAME_LEN" is 512
I can see "KSYM_NAME_LEN" defined as 128 here [1]. Are you looking at any other header file?
[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/linux/kallsyms.h#n18
Quoted 51 lines
and the first thing
kallsyms_lookup()does it
zero out byte 511. Previously we were only allocating 128 bytes so I guess we were writing past the end.Assuming I understood correctly, maybe mention the bugfix in the commit text?
Quoted 28 lines
kernel/debug/kdb/kdb_support.c +0
-24@@ -102,63 +102,14 @@ int kdbnearsym(unsigned long addr, kdb_symtab_t *symtab) symtab->sym_end = symtab->sym_start + symbolsize; ret = symtab->sym_name !=NULL && *(symtab->sym_name) !='\0' ; -if (ret) { -int i; -/* Another 2.6 kallsyms "feature". Sometimes the sym_name is -* set but the buffer passed into kallsyms_lookup is not used, -* so it contains garbage. The caller has to work out which -* buffer needs to be saved. -* -* What was Rusty smoking when he wrote that code? -*/ -if (symtab->sym_name != knt1) { -strncpy (knt1, symtab->sym_name, knt1_size); - knt1[knt1_size-1 ] ='\0' ; - } -for (i =0 ; i <ARRAY_SIZE (kdb_name_table); ++i) { -if (kdb_name_table[i] && -strcmp (kdb_name_table[i], knt1) ==0 ) -break ; - } -if (i >=ARRAY_SIZE (kdb_name_table)) { -debug_kfree (kdb_name_table[0 ]); -memmove (kdb_name_table, kdb_name_table+1 , -sizeof (kdb_name_table[0 ]) * - (ARRAY_SIZE (kdb_name_table)-1 )); - }else {kernel/debug/kdb/kdb_support.c int kdbnearsym(unsigned long addr, kdb_symtab_t *symtab) · line 138
Quoted 10 lines
kernel/debug/kdb/kdb_support.c +0
-10-debug_kfree (knt1); - knt1 = kdb_name_table[i]; -memmove (kdb_name_table+i, kdb_name_table+i+1 , -sizeof (kdb_name_table[0 ]) * - (ARRAY_SIZE (kdb_name_table)-i-1 )); - } - i =ARRAY_SIZE (kdb_name_table) -1 ; - kdb_name_table[i] = knt1; - symtab->sym_name = kdb_name_table[i]; - knt1 =NULL ;I definitely had a hard time following exactly what all the cases were handling and if they were all right, but I agree that we can kill the old code (yay!)
Quoted 11 lines
kernel/debug/kdb/kdb_support.c void kdb_symbol_print(unsigned long addr, const kdb_symtab_t *symtab_p, · line 206
Quoted 8 lines
kernel/debug/kdb/kdb_support.c +1
-0@@ -249,6 +200,7 @@ void kdb_symbol_print(unsigned long addr, const kdb_symtab_t *symtab_p, unsigned int punc) {kdb_symtab_t symtab, *symtab_p2; +if (symtab_p) { symtab_p2 = (kdb_symtab_t *)symtab_p; }else {nit: unrelated whitespace change?
Will revert.
Quoted 4 lines
All comments above are nits and not terribly important, so:
Reviewed-by Douglas Anderson <[email protected]>
Thanks,
-Sumit
Doug Anderson
reviewerHi,
Quoted 10 lines · Sumit Garg
Quoted 10 lines
Quoted 9 lines
int
kdbnearsym(unsigned long addr, kdb_symtab_t *symtab){ int ret = 0; unsigned long symbolsize = 0; unsigned long offset = 0;kernel/debug/kdb/kdb_support.c +1
-2-#define knt1_size 128/* must be >= kallsyms table size */ -char *knt1 =NULL ; +static char namebuf[KSYM_NAME_LEN ];
Quoted 7 lines
Quoted 2 lines
I guess this also ends up fixing a bug too, right? My greps show that "
KSYM_NAME_LEN" is 512I can see "
KSYM_NAME_LEN" defined as 128 here [1]. Are you looking at any other header file?[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/linux/kallsyms.h#n18
Ah ha, it's recent! See commit f2f6175186f4 ("kallsyms: increase maximum kernel symbol length to 512")
...I guess this officially "Fixes" that commit then?
-Doug
Quoted 16 lines · Doug Anderson
Hi,
Quoted 13 lines · Sumit Garg
Quoted 12 lines
kernel/debug/kdb/kdb_support.c int kdbgetsymval(const char *symname, kdb_symtab_t *symtab) · line 87
Quoted 9 lines
int
kdbnearsym(unsigned long addr, kdb_symtab_t *symtab){ int ret = 0; unsigned long symbolsize = 0; unsigned long offset = 0;kernel/debug/kdb/kdb_support.c +1
-2-#define knt1_size 128/* must be >= kallsyms table size */ -char *knt1 =NULL ; +static char namebuf[KSYM_NAME_LEN ];I guess this also ends up fixing a bug too, right? My greps show that "
KSYM_NAME_LEN" is 512
Quoted 7 lines
Quoted 4 lines
I can see "
KSYM_NAME_LEN" defined as 128 here [1]. Are you looking at any other header file?[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/linux/kallsyms.h#n18
Ah ha, it's recent! See commit
f2f6175186f4("kallsyms: increase maximum kernel symbol length to 512")
Ineed. So recent that I think it hasn't been merged to mainline yet!
This patch is part of the rust patch set. IIUC it is in linux-next for wider testing but I'd be surprised if it gets merged to mainline anytime soon (and even more amazed if it is merged without being rebased and given a new hash value ;-) ).
Daniel.
Doug Anderson
reviewerHi,
Quoted 26 lines · Daniel Thompson
Quoted 16 lines · Doug Anderson
Hi,
Quoted 13 lines · Sumit Garg
Quoted 12 lines
kernel/debug/kdb/kdb_support.c int kdbgetsymval(const char *symname, kdb_symtab_t *symtab) · line 87
Quoted 9 lines
int
kdbnearsym(unsigned long addr, kdb_symtab_t *symtab){ int ret = 0; unsigned long symbolsize = 0; unsigned long offset = 0;kernel/debug/kdb/kdb_support.c +1
-2-#define knt1_size 128/* must be >= kallsyms table size */ -char *knt1 =NULL ; +static char namebuf[KSYM_NAME_LEN ];I guess this also ends up fixing a bug too, right? My greps show that "
KSYM_NAME_LEN" is 512Quoted 7 lines
Quoted 4 lines
I can see "
KSYM_NAME_LEN" defined as 128 here [1]. Are you looking at any other header file?[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/linux/kallsyms.h#n18
Ah ha, it's recent! See commit
f2f6175186f4("kallsyms: increase maximum kernel symbol length to 512")
Quoted 6 lines
Ineed. So recent that I think it hasn't been merged to mainline yet!
This patch is part of the rust patch set. IIUC it is in linux-next for wider testing but I'd be surprised if it gets merged to mainline anytime soon (and even more amazed if it is merged without being rebased and given a new hash value ;-) ).
Ah, good point. Yeah, I should have mentioned that I was looking at linuxnext. I guess maybe the right compromise is just to mention that we'll be more robust in case that other #define changes. ;-)
-Doug
Quoted 33 lines · Doug Anderson
Hi,
Quoted 28 lines · Daniel Thompson
Quoted 16 lines · Doug Anderson
Hi,
Quoted 13 lines · Sumit Garg
Quoted 12 lines
kernel/debug/kdb/kdb_support.c int kdbgetsymval(const char *symname, kdb_symtab_t *symtab) · line 87
Quoted 9 lines
int
kdbnearsym(unsigned long addr, kdb_symtab_t *symtab){ int ret = 0; unsigned long symbolsize = 0; unsigned long offset = 0;kernel/debug/kdb/kdb_support.c +1
-2-#define knt1_size 128/* must be >= kallsyms table size */ -char *knt1 =NULL ; +static char namebuf[KSYM_NAME_LEN ];I guess this also ends up fixing a bug too, right? My greps show that "
KSYM_NAME_LEN" is 512Quoted 7 lines
Quoted 4 lines
I can see "
KSYM_NAME_LEN" defined as 128 here [1]. Are you looking at any other header file?[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/linux/kallsyms.h#n18
Ah ha, it's recent! See commit
f2f6175186f4("kallsyms: increase maximum kernel symbol length to 512")Ineed. So recent that I think it hasn't been merged to mainline yet!
Quoted 9 lines
Quoted 4 lines
This patch is part of the rust patch set. IIUC it is in linux-next for wider testing but I'd be surprised if it gets merged to mainline anytime soon (and even more amazed if it is merged without being rebased and given a new hash value ;-) ).
Ah, good point. Yeah, I should have mentioned that I was looking at linuxnext. I guess maybe the right compromise is just to mention that we'll be more robust in case that other #define changes. ;-)
Okay, I will update the commit description accordingly.
-Sumit
Quoted 1 line
-Doug