Skip to content

Commit a30d4ff

Browse files
nir9daniel-thompson
authored andcommitted
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>
1 parent 80e54e8 commit a30d4ff

File tree

2 files changed

+9
-41
lines changed

2 files changed

+9
-41
lines changed

include/linux/kdb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ extern int kdb_initial_cpu;
104104
#define KDB_NOENVVALUE (-6)
105105
#define KDB_NOTIMP (-7)
106106
#define KDB_ENVFULL (-8)
107-
#define KDB_ENVBUFFULL (-9)
107+
#define KDB_KMALLOCFAILED (-9)
108108
#define KDB_TOOMANYBPT (-10)
109109
#define KDB_TOOMANYDBREGS (-11)
110110
#define KDB_DUPBPT (-12)

kernel/debug/kdb/kdb_main.c

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ static kdbmsg_t kdbmsgs[] = {
105105
KDBMSG(NOENVVALUE, "Environment variable should have value"),
106106
KDBMSG(NOTIMP, "Command not implemented"),
107107
KDBMSG(ENVFULL, "Environment full"),
108-
KDBMSG(ENVBUFFULL, "Environment buffer full"),
108+
KDBMSG(KMALLOCFAILED, "Failed to allocate memory"),
109109
KDBMSG(TOOMANYBPT, "Too many breakpoints defined"),
110110
#ifdef CONFIG_CPU_XSCALE
111111
KDBMSG(TOOMANYDBREGS, "More breakpoints than ibcr registers defined"),
@@ -130,13 +130,9 @@ static const int __nkdb_err = ARRAY_SIZE(kdbmsgs);
130130

131131

132132
/*
133-
* Initial environment. This is all kept static and local to
134-
* this file. We don't want to rely on the memory allocation
135-
* mechanisms in the kernel, so we use a very limited allocate-only
136-
* heap for new and altered environment variables. The entire
137-
* environment is limited to a fixed number of entries (add more
138-
* to __env[] if required) and a fixed amount of heap (add more to
139-
* KDB_ENVBUFSIZE if required).
133+
* Initial environment. This is all kept static and local to this file.
134+
* The entire environment is limited to a fixed number of entries
135+
* (add more to __env[] if required)
140136
*/
141137

142138
static char *__env[31] = {
@@ -258,35 +254,6 @@ char *kdbgetenv(const char *match)
258254
return NULL;
259255
}
260256

261-
/*
262-
* kdballocenv - This function is used to allocate bytes for
263-
* environment entries.
264-
* Parameters:
265-
* bytes The number of bytes to allocate in the static buffer.
266-
* Returns:
267-
* A pointer to the allocated space in the buffer on success.
268-
* NULL if bytes > size available in the envbuffer.
269-
* Remarks:
270-
* We use a static environment buffer (envbuffer) to hold the values
271-
* of dynamically generated environment variables (see kdb_set). Buffer
272-
* space once allocated is never free'd, so over time, the amount of space
273-
* (currently 512 bytes) will be exhausted if env variables are changed
274-
* frequently.
275-
*/
276-
static char *kdballocenv(size_t bytes)
277-
{
278-
#define KDB_ENVBUFSIZE 512
279-
static char envbuffer[KDB_ENVBUFSIZE];
280-
static int envbufsize;
281-
char *ep = NULL;
282-
283-
if ((KDB_ENVBUFSIZE - envbufsize) >= bytes) {
284-
ep = &envbuffer[envbufsize];
285-
envbufsize += bytes;
286-
}
287-
return ep;
288-
}
289-
290257
/*
291258
* kdbgetulenv - This function will return the value of an unsigned
292259
* long-valued environment variable.
@@ -348,9 +315,9 @@ static int kdb_setenv(const char *var, const char *val)
348315

349316
varlen = strlen(var);
350317
vallen = strlen(val);
351-
ep = kdballocenv(varlen + vallen + 2);
352-
if (ep == (char *)0)
353-
return KDB_ENVBUFFULL;
318+
ep = kmalloc(varlen + vallen + 2, GFP_KDB);
319+
if (!ep)
320+
return KDB_KMALLOCFAILED;
354321

355322
sprintf(ep, "%s=%s", var, val);
356323

@@ -359,6 +326,7 @@ static int kdb_setenv(const char *var, const char *val)
359326
&& ((strncmp(__env[i], var, varlen) == 0)
360327
&& ((__env[i][varlen] == '\0')
361328
|| (__env[i][varlen] == '=')))) {
329+
kfree_const(__env[i]);
362330
__env[i] = ep;
363331
return 0;
364332
}

0 commit comments

Comments
 (0)