Re: [RFC[ Alloc in vsprintf
From: Linus Torvalds
Date: Sun Jun 26 2022 - 16:19:46 EST
On Sun, Jun 26, 2022 at 12:53 PM Joe Perches <joe@xxxxxxxxxxx> wrote:
>
> In a reply to the printbufs thread, I wrote a proposal to use an
> alloc to reduce stack in vsprintf when CONFIG_KALLSYMS is enabled.
>
> No one has replied to this but I think it's somewhat sensible.
I think that's a bad idea.
Those things are *literally* called from panic situations, which may
be while holding core memory allocation locks, or similar.
The last thing we want to do is make a hard-to-debug panic be even
*harder* to debug because you get a deadlock when oopsing.
(And yes, I realize that the symbol name lookup can have problems too,
but thats' kind of fundamental to %pS, while a kzmalloc isn't.
Now, you are correct that the stack buffer is annoying. But I think
the proper way to fix that is to say "we already *have* the target
buffer, let's use it".
That does require teaching the sprint_symbol() functions that they
need to take a "length of buffer" and return how much they used, but
that would seem to be a sensible thing anyway, and what the code
should always have done?
It's bad policy to just pass in a buffer without length, and I think
it was always broken. Nasty. That KSYM_SYMBOL_LEN is magically taking
care of it all, but it's ugly as heck, wouldn't you say?
NOTE! The attached patch is completely broken. I did not do that
interface change to the kallsyms code. The patch is literally meant to
be just an explanation of what I mean, not a working patch.
Linus
lib/vsprintf.c | 18 +++++++++++-------
1 file changed, 11 insertions(+), 7 deletions(-)
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 3c1853a9d1c0..032fa8bc5752 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -981,7 +981,7 @@ char *symbol_string(char *buf, char *end, void *ptr,
{
unsigned long value;
#ifdef CONFIG_KALLSYMS
- char sym[KSYM_SYMBOL_LEN];
+ unsigned long maxlen;
#endif
if (fmt[1] == 'R')
@@ -989,18 +989,22 @@ char *symbol_string(char *buf, char *end, void *ptr,
value = (unsigned long)ptr;
#ifdef CONFIG_KALLSYMS
+ maxlen = end - buf;
+ if (maxlen > spec.precision)
+ maxlen = spec.precision;
+
if (*fmt == 'B' && fmt[1] == 'b')
- sprint_backtrace_build_id(sym, value);
+ maxlen = sprint_backtrace_build_id(buf, maxlen, value);
else if (*fmt == 'B')
- sprint_backtrace(sym, value);
+ maxlen = sprint_backtrace(buf, maxlen, value);
else if (*fmt == 'S' && (fmt[1] == 'b' || (fmt[1] == 'R' && fmt[2] == 'b')))
- sprint_symbol_build_id(sym, value);
+ maxlen = sprint_symbol_build_id(buf, maxlen, value);
else if (*fmt != 's')
- sprint_symbol(sym, value);
+ maxlen = sprint_symbol(buf, maxlen, value);
else
- sprint_symbol_no_offset(sym, value);
+ maxlen = sprint_symbol_no_offset(buf, maxlen, value);
- return string_nocheck(buf, end, sym, spec);
+ return widen_string(buf, maxlen, end, spec);
#else
return special_hex_number(buf, end, value, sizeof(void *));
#endif