Skip to contentSkip to conversation

kdb: remove usage of static environment buffer

PATCH Superseded Nir Lichtman 2 participants 3 messages Feb 3, 2025 – Feb 4, 2025 KGDB / KDB /debug_core

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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.

---
 include/linux/kdb.h         |  2 +-
 kernel/debug/kdb/kdb_main.c | 51 ++++++++-----------------------------
 2 files changed, 12 insertions(+), 41 deletions(-)
include/linux/kdb.h +1 -1
index 905a2e2f45f6..ecbf819deeca 100644
@@ -104,7 +104,7 @@ extern int kdb_initial_cpu;
 #define KDB_NOENVVALUE	(-6)
 #define KDB_NOTIMP	(-7)
 #define KDB_ENVFULL	(-8)
-#define KDB_ENVBUFFULL	(-9)
+#define KDB_KMALLOCFAILED	(-9)
 #define KDB_TOOMANYBPT	(-10)
 #define KDB_TOOMANYDBREGS (-11)
 #define KDB_DUPBPT	(-12)
kernel/debug/kdb/kdb_main.c +11 -40

81 lines not on this page · raw message

Signature · 1 line
2.39.5
reviewer
#1 lore raw

Hi,

kernel/debug/kdb/kdb_main.c static int kdb_setenv(const char *var, const char *val) · line 321

Quoted 12 lines · Nir Lichtman
kernel/debug/kdb/kdb_main.c +4 -3
@@ -348,9 +315,12 @@ static int kdb_setenv(const char *var, const char *val)
 
        varlen = strlen(var);
        vallen = strlen(val);
-       ep = kdballocenv(varlen + vallen + 2);
-       if (ep == (char *)0)
-               return KDB_ENVBUFFULL;
+       ep = kmalloc(varlen + vallen + 2, GFP_KDB);
+       if (!ep) {
+               kdb_printf("Could not allocate space for the env var: %s\n",
+                               var);

You don't need the printout. kmalloc is already very shouty if memory allocations fail.

kernel/debug/kdb/kdb_main.c static int kdb_setenv(const char *var, const char *val) · line 332

Quoted 10 lines
kernel/debug/kdb/kdb_main.c +3 -0
+               return KDB_KMALLOCFAILED;
+       }
 
        sprintf(ep, "%s=%s", var, val);
 
@@ -359,6 +329,7 @@ static int kdb_setenv(const char *var, const char *val)
                 && ((strncmp(__env[i], var, varlen) == 0)
                   && ((__env[i][varlen] == '\0')
                    || (__env[i][varlen] == '=')))) {
+                       kfree(__env[i]);

I haven't tested it, but shouldn't the above be kfree_const() instead of kfree()? Otherwise won't you be trying to kfree() the initial strings? ...or am I missing something?

-Doug

#2 lore raw
Quoted 6 lines · Doug Anderson

Hi,

Quoted 3 lines · Nir Lichtman
(quoted diff) +0 -0
@@ -348,9 +315,12 @@ static int kdb_setenv(const char *var, const char *val)
 

[..]

Quoted 8 lines

kernel/debug/kdb/kdb_main.c static int kdb_setenv(const char *var, const char *val) · line 321

Quoted 2 lines
kernel/debug/kdb/kdb_main.c +2 -0
+               kdb_printf("Could not allocate space for the env var: %s\n",
+                               var);

You don't need the printout. kmalloc is already very shouty if memory allocations fail.

Quoted 1 line

+ return KDB_KMALLOCFAILED;

[..]

Quoted 5 lines
Quoted 1 line

+ kfree(__env[i]);

I haven't tested it, but shouldn't the above be kfree_const() instead of kfree()? Otherwise won't you be trying to kfree() the initial strings? ...or am I missing something?

Hi Doug,

Thanks for the review, you're right, my bad, I'll fix to use kfree_const.

Sending shortly a v2 with the fixes.

Nir

Quoted 3 lines

-Doug