Skip to contentSkip to conversation

tracing/eprobe: Replace kzalloc with kmalloc

PATCH In review Quanfa Fu 2 participants 3 messages Jan 7, 2023 – Jan 9, 2023 Tracing

lore
Share & tools
lore

Quanfa Fu

author
#0 lore raw

PATCH 1/1 tracing/eprobe: Replace kzalloc with kmalloc

Since this memory will be filled soon below, I feel that there is no need for a memory of all zeros here. 'snprintf' does not return negative num according to ISO C99, so I feel that no judgment is needed here.

No functional change intended.

---
 kernel/trace/trace_eprobe.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
kernel/trace/trace_eprobe.c +1 -3
index 352b65e2b910..cd1d271a74e7 100644
@@ -917,15 +917,13 @@ static int trace_eprobe_parse_filter(struct trace_eprobe *ep, int argc, const ch
 	for (i = 0; i < argc; i++)
 		len += strlen(argv[i]) + 1;
 
-	ep->filter_str = kzalloc(len, GFP_KERNEL);
+	ep->filter_str = kmalloc(len, GFP_KERNEL);
 	if (!ep->filter_str)
 		return -ENOMEM;
 
 	p = ep->filter_str;
 	for (i = 0; i < argc; i++) {
 		ret = snprintf(p, len, "%s ", argv[i]);
-		if (ret < 0)
-			goto error;
 		if (ret > len) {
 			ret = -E2BIG;
 			goto error;
Signature · 1 line
2.31.1
maintainer
#1 lore raw
Quoted 1 line · On Sat, 7 Jan 2023 11:43:35 +0800 Quanfa Fu <[email protected]> wrote:

Since this memory will be filled soon below, I feel that there is

kzalloc() is also used as a safety measure to make sure nothing is accidentally exposed. I rather keep it for safety. Just because it doesn't need to be here doesn't mean it should be removed. There is no benefit to making this kmalloc(), as this is far from a fast path.

Quoted 3 lines

no need for a memory of all zeros here. 'snprintf' does not return negative num according to ISO C99, so I feel that no judgment is needed here.

Also, it's best to remove "feelings" from change logs. Code updates are not made due to how one feels about something (at least it shouldn't be), but about having technical reasons for doing so. I do agree there's no reason to check snprintf() from returning negative, as looking at its implementation, there is no negative return. Thus, the change log should be:

"No need to check for negative return value from snprintf() as the code does not return negative values."

Quoted 2 lines

No functional change intended.

And this does have functional changes. If the output of a compiler is different for a function, then that's a functional change. What we consider non functional changes is if functions get moved around, or possibly code in a function is moved into a helper function where the compiler *should* end up with the same assembly.

Changing what malloc is called is definitely a functional change.

Quoted 12 lines
---
 kernel/trace/trace_eprobe.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
kernel/trace/trace_eprobe.c +0 -0
index 352b65e2b910..cd1d271a74e7 100644
@@ -917,15 +917,13 @@ static int trace_eprobe_parse_filter(struct trace_eprobe *ep, int argc, const ch
 	for (i = 0; i < argc; i++)

kernel/trace/trace_eprobe.c static int trace_eprobe_parse_filter(struct trace_eprobe *ep, int argc, const ch · line 926

Quoted 10 lines
kernel/trace/trace_eprobe.c +1 -1
 		len += strlen(argv[i]) + 1;
 
-	ep->filter_str = kzalloc(len, GFP_KERNEL);
+	ep->filter_str = kmalloc(len, GFP_KERNEL);
 	if (!ep->filter_str)
 		return -ENOMEM;
 
 	p = ep->filter_str;
 	for (i = 0; i < argc; i++) {
 		ret = snprintf(p, len, "%s ", argv[i]);

I wonder if this should be a strncat() instead?

kernel/trace/trace_eprobe.c static int trace_eprobe_parse_filter(struct trace_eprobe *ep, int argc, const ch · line 929

Quoted 5 lines
kernel/trace/trace_eprobe.c +0 -2
-		if (ret < 0)
-			goto error;
 		if (ret > len) {
 			ret = -E2BIG;
 			goto error;
	for (i = 0; i < arcc, i++)
		strncat(ep->filter_str, argv[i], len);

I mean, before this code we have that loop already determining what len is, do we really need to check if it is going to be -E2BIG?

-- Steve

author
#2 lore raw

Thanks a lot. Learned a lot from here.

I replaced snprintf with memcpy in Patchv2

Quoted 74 lines · Steven Rostedt
Quoted 1 line · On Sat, 7 Jan 2023 11:43:35 +0800 Quanfa Fu <[email protected]> wrote:

Since this memory will be filled soon below, I feel that there is

kzalloc() is also used as a safety measure to make sure nothing is accidentally exposed. I rather keep it for safety. Just because it doesn't need to be here doesn't mean it should be removed. There is no benefit to making this kmalloc(), as this is far from a fast path.

Quoted 3 lines

no need for a memory of all zeros here. 'snprintf' does not return negative num according to ISO C99, so I feel that no judgment is needed here.

Also, it's best to remove "feelings" from change logs. Code updates are not made due to how one feels about something (at least it shouldn't be), but about having technical reasons for doing so. I do agree there's no reason to check snprintf() from returning negative, as looking at its implementation, there is no negative return. Thus, the change log should be:

"No need to check for negative return value from snprintf() as the code does not return negative values."

Quoted 2 lines

No functional change intended.

And this does have functional changes. If the output of a compiler is different for a function, then that's a functional change. What we consider non functional changes is if functions get moved around, or possibly code in a function is moved into a helper function where the compiler *should* end up with the same assembly.

Changing what malloc is called is definitely a functional change.

Quoted 12 lines
---
 kernel/trace/trace_eprobe.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
kernel/trace/trace_eprobe.c +0 -0
index 352b65e2b910..cd1d271a74e7 100644
@@ -917,15 +917,13 @@ static int trace_eprobe_parse_filter(struct trace_eprobe *ep, int argc, const ch
      for (i = 0; i < argc; i++)

kernel/trace/trace_eprobe.c static int trace_eprobe_parse_filter(struct trace_eprobe *ep, int argc, const ch · line 926

Quoted 10 lines
kernel/trace/trace_eprobe.c +1 -1
              len += strlen(argv[i]) + 1;
 
-     ep->filter_str = kzalloc(len, GFP_KERNEL);
+     ep->filter_str = kmalloc(len, GFP_KERNEL);
      if (!ep->filter_str)
              return -ENOMEM;
 
      p = ep->filter_str;
      for (i = 0; i < argc; i++) {
              ret = snprintf(p, len, "%s ", argv[i]);

I wonder if this should be a strncat() instead?

kernel/trace/trace_eprobe.c static int trace_eprobe_parse_filter(struct trace_eprobe *ep, int argc, const ch · line 929

Quoted 5 lines
kernel/trace/trace_eprobe.c +0 -2
-             if (ret < 0)
-                     goto error;
              if (ret > len) {
                      ret = -E2BIG;
                      goto error;
        for (i = 0; i < arcc, i++)
                strncat(ep->filter_str, argv[i], len);

I mean, before this code we have that loop already determining what len is, do we really need to check if it is going to be -E2BIG?

-- Steve