kdb: remove usage of static environment buffer

Problem: The set environment variable logic uses a static "heap" like
buffer to store the values of the variables, and they are never freed,
on top of that this is redundant since the kernel supplies allocation
facilities which are even used also in this file.

Solution: Remove the weird static buffer logic and use kmalloc instead,
call kfree when overriding an existing variable.

Signed-off-by: Nir Lichtman <nir@lichtman.org>
Reviewed-by: Douglas Anderson <dianders@chromium.org>
Link: https://lore.kernel.org/r/20250204054741.GB1219827@lichtman.org
Signed-off-by: Daniel Thompson <daniel@riscstar.com>
This commit is contained in:
Nir Lichtman 2025-02-04 05:47:41 +00:00 committed by Daniel Thompson
parent 80e54e8491
commit a30d4ff819
2 changed files with 9 additions and 41 deletions

View file

@ -104,7 +104,7 @@ extern int kdb_initial_cpu;
#define KDB_NOENVVALUE (-6) #define KDB_NOENVVALUE (-6)
#define KDB_NOTIMP (-7) #define KDB_NOTIMP (-7)
#define KDB_ENVFULL (-8) #define KDB_ENVFULL (-8)
#define KDB_ENVBUFFULL (-9) #define KDB_KMALLOCFAILED (-9)
#define KDB_TOOMANYBPT (-10) #define KDB_TOOMANYBPT (-10)
#define KDB_TOOMANYDBREGS (-11) #define KDB_TOOMANYDBREGS (-11)
#define KDB_DUPBPT (-12) #define KDB_DUPBPT (-12)

View file

@ -105,7 +105,7 @@ static kdbmsg_t kdbmsgs[] = {
KDBMSG(NOENVVALUE, "Environment variable should have value"), KDBMSG(NOENVVALUE, "Environment variable should have value"),
KDBMSG(NOTIMP, "Command not implemented"), KDBMSG(NOTIMP, "Command not implemented"),
KDBMSG(ENVFULL, "Environment full"), KDBMSG(ENVFULL, "Environment full"),
KDBMSG(ENVBUFFULL, "Environment buffer full"), KDBMSG(KMALLOCFAILED, "Failed to allocate memory"),
KDBMSG(TOOMANYBPT, "Too many breakpoints defined"), KDBMSG(TOOMANYBPT, "Too many breakpoints defined"),
#ifdef CONFIG_CPU_XSCALE #ifdef CONFIG_CPU_XSCALE
KDBMSG(TOOMANYDBREGS, "More breakpoints than ibcr registers defined"), KDBMSG(TOOMANYDBREGS, "More breakpoints than ibcr registers defined"),
@ -130,13 +130,9 @@ static const int __nkdb_err = ARRAY_SIZE(kdbmsgs);
/* /*
* Initial environment. This is all kept static and local to * Initial environment. This is all kept static and local to this file.
* this file. We don't want to rely on the memory allocation * The entire environment is limited to a fixed number of entries
* mechanisms in the kernel, so we use a very limited allocate-only * (add more to __env[] if required)
* heap for new and altered environment variables. The entire
* environment is limited to a fixed number of entries (add more
* to __env[] if required) and a fixed amount of heap (add more to
* KDB_ENVBUFSIZE if required).
*/ */
static char *__env[31] = { static char *__env[31] = {
@ -258,35 +254,6 @@ char *kdbgetenv(const char *match)
return NULL; return NULL;
} }
/*
* kdballocenv - This function is used to allocate bytes for
* environment entries.
* Parameters:
* bytes The number of bytes to allocate in the static buffer.
* Returns:
* A pointer to the allocated space in the buffer on success.
* NULL if bytes > size available in the envbuffer.
* Remarks:
* We use a static environment buffer (envbuffer) to hold the values
* of dynamically generated environment variables (see kdb_set). Buffer
* space once allocated is never free'd, so over time, the amount of space
* (currently 512 bytes) will be exhausted if env variables are changed
* frequently.
*/
static char *kdballocenv(size_t bytes)
{
#define KDB_ENVBUFSIZE 512
static char envbuffer[KDB_ENVBUFSIZE];
static int envbufsize;
char *ep = NULL;
if ((KDB_ENVBUFSIZE - envbufsize) >= bytes) {
ep = &envbuffer[envbufsize];
envbufsize += bytes;
}
return ep;
}
/* /*
* kdbgetulenv - This function will return the value of an unsigned * kdbgetulenv - This function will return the value of an unsigned
* long-valued environment variable. * long-valued environment variable.
@ -348,9 +315,9 @@ static int kdb_setenv(const char *var, const char *val)
varlen = strlen(var); varlen = strlen(var);
vallen = strlen(val); vallen = strlen(val);
ep = kdballocenv(varlen + vallen + 2); ep = kmalloc(varlen + vallen + 2, GFP_KDB);
if (ep == (char *)0) if (!ep)
return KDB_ENVBUFFULL; return KDB_KMALLOCFAILED;
sprintf(ep, "%s=%s", var, val); sprintf(ep, "%s=%s", var, val);
@ -359,6 +326,7 @@ static int kdb_setenv(const char *var, const char *val)
&& ((strncmp(__env[i], var, varlen) == 0) && ((strncmp(__env[i], var, varlen) == 0)
&& ((__env[i][varlen] == '\0') && ((__env[i][varlen] == '\0')
|| (__env[i][varlen] == '=')))) { || (__env[i][varlen] == '=')))) {
kfree_const(__env[i]);
__env[i] = ep; __env[i] = ep;
return 0; return 0;
} }