Skip to contentSkip to conversation

checkpatch: Add check for array allocator family argument order

PATCH Applied Liao Chang 4 participants 8 messages Nov 4, 2022 – Nov 7, 2022 Checkpatch

lore
Share & tools
lore

Liao Chang

author
#0 lore raw

PATCH v2 1/1 checkpatch: Add check for array allocator family argument order

These array allocator family are sometimes misused with the first and second arguments switchted.

Same issue with calloc, kvcalloc, kvmalloc_array etc.

Bleat if sizeof is the first argument.

---
v2:
1. Acked-by Joe Perches.
2. Use lore links in Link tag.

---
 scripts/checkpatch.pl | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/checkpatch.pl +1 -1
index 1e5e66ae5a52..a9a9dc277cff 100755
@@ -7128,7 +7128,7 @@ sub process {
 		}
 
 # check for alloc argument mismatch
-		if ($line =~ /\b((?:devm_)?(?:kcalloc|kmalloc_array))\s*\(\s*sizeof\b/) {
+		if ($line =~ /\b((?:devm_)?((?:k|kv)?(calloc|malloc_array)(?:_node)?))\s*\(\s*sizeof\b/) {
 			WARN("ALLOC_ARRAY_ARGS",
 			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
 		}
Signature · 1 line
2.17.1
Acked-by on v2 1/1
#1 lore raw
Quoted 3 lines · Liao Chang

These array allocator family are sometimes misused with the first and second arguments switchted.

Just a nit:

s/switchted/switched/

But probably you actually mean "swapped". I think there is a slight difference between the two words, "switched" and "swapped". And here the arguments are swapped. Note: I am also not a native speaker.

For the implementation change:

How many new findings are now identified with this extended check on linux-next? Could you run checkpatch on all files of linux-next and share the new findings? Then we can do a quick scan if some instances should be immediately fixed or some janitor should follow through with such a task.

Lukas

Quoted 33 lines

Same issue with calloc, kvcalloc, kvmalloc_array etc.

Bleat if sizeof is the first argument.

---
v2:
1. Acked-by Joe Perches.
2. Use lore links in Link tag.

---
 scripts/checkpatch.pl | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/checkpatch.pl +1 -2
index 1e5e66ae5a52..a9a9dc277cff 100755
@@ -7128,7 +7128,7 @@ sub process {
                }
 
 # check for alloc argument mismatch
-               if ($line =~ /\b((?:devm_)?(?:kcalloc|kmalloc_array))\s*\(\s*sizeof\b/) {
+               if ($line =~ /\b((?:devm_)?((?:k|kv)?(calloc|malloc_array)(?:_node)?))\s*\(\s*sizeof\b/) {
                        WARN("ALLOC_ARRAY_ARGS",
                             "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
                }
--

2.17.1

maintainer
#2 lore raw
Quoted 15 lines · Lukas Bulwahn
Quoted 3 lines · Liao Chang

These array allocator family are sometimes misused with the first and second arguments switchted.

Just a nit:

s/switchted/switched/

But probably you actually mean "swapped". I think there is a slight difference between the two words, "switched" and "swapped". And here the arguments are swapped. Note: I am also not a native speaker.

For the implementation change:

Quoted 7 lines

How many new findings are now identified with this extended check on linux-next? Could you run checkpatch on all files of linux-next and share the new findings? Then we can do a quick scan if some instances should be immediately fixed or some janitor should follow through with such a task.

There are ~25.
It's fairly trivial Lukas and only cosmetic.

https://lore.kernel.org/lkml/[email protected]/

#3 lore raw
In reply to Lukas Bulwahn
Quoted 15 lines · Lukas Bulwahn
Quoted 3 lines · Liao Chang

These array allocator family are sometimes misused with the first and second arguments switchted.

Just a nit:

s/switchted/switched/

But probably you actually mean "swapped". I think there is a slight difference between the two words, "switched" and "swapped". And here the arguments are swapped. Note: I am also not a native speaker.

For the implementation change:

Quoted 7 lines

How many new findings are now identified with this extended check on linux-next? Could you run checkpatch on all files of linux-next and share the new findings? Then we can do a quick scan if some instances should be immediately fixed or some janitor should follow through with such a task.

The scan result for v6.1-rc3:

./kernel/watch_queue.c:276: pages = kcalloc(sizeof(struct page *), nr_pages, GFP_KERNEL);
./virt/kvm/kvm_main.c:411: mc->objects = kvmalloc_array(sizeof(void *), capacity, gfp);
./tools/objtool/check.c:288: struct cfi_state *cfi = calloc(sizeof(struct cfi_state), 1);
./tools/objtool/check.c:517: file->pv_ops = calloc(sizeof(struct pv_state), nr);
./kernel/bpf/bpf_local_storage.c:623: smap->buckets = kvcalloc(sizeof(*smap->buckets), nbuckets,
./tools/perf/builtin-record.c:4076: rec->switch_output.filenames = calloc(sizeof(char *),
./fs/btrfs/send.c:7913: sctx->clone_roots = kvcalloc(sizeof(*sctx->clone_roots),
./drivers/scsi/bfa/bfad_bsg.c:3247: buf_base = kcalloc(sizeof(struct bfad_buf_info) +
./sound/core/seq/seq_memory.c:379: cellptr = kvmalloc_array(sizeof(struct snd_seq_event_cell), pool->size,
./tools/lib/bpf/libbpf.c:8220: gen = calloc(sizeof(*gen), 1);

./tools/perf/util/metricgroup.c:269:    metric_events = calloc(sizeof(void *), ids_size + 1);
./tools/perf/util/hist.c:492:           he->res_samples = calloc(sizeof(struct res_sample),
./tools/perf/util/bpf-loader.c:574:     priv = calloc(sizeof(*priv), 1);

./tools/perf/util/synthetic-events.c:1040: synthesize_threads = calloc(sizeof(pthread_t), thread_nr);
./tools/perf/util/synthetic-events.c:1044: args = calloc(sizeof(*args), thread_nr);
./tools/perf/util/stat-shadow.c:390: metric_events = calloc(sizeof(struct evsel *),
./drivers/soc/fsl/dpio/dpio-service.c:526: ed = kcalloc(sizeof(struct qbman_eq_desc), 32, GFP_KERNEL);
./drivers/gpu/drm/nouveau/nouveau_svm.c:1000: buffer->fault = kvcalloc(sizeof(*buffer->fault), buffer->entries, GFP_KERNEL);

./tools/testing/selftests/memfd/fuse_test.c:257:        zero = calloc(sizeof(*zero), mfd_def_size);
./tools/testing/selftests/bpf/test_progs.c:1324:        dispatcher_threads = calloc(sizeof(pthread_t), env.workers);
./tools/testing/selftests/bpf/test_progs.c:1325:        data = calloc(sizeof(struct dispatch_data), env.workers);
./tools/testing/selftests/bpf/test_progs.c:1327:        env.worker_current_test = calloc(sizeof(int), env.workers);

./tools/testing/selftests/bpf/test_progs.c:1642: env.worker_pids = calloc(sizeof(__pid_t), env.workers);
./tools/testing/selftests/bpf/test_progs.c:1643: env.worker_socks = calloc(sizeof(int), env.workers);
./tools/testing/selftests/arm64/fp/fp-stress.c:458: children = calloc(sizeof(*children), tests);
./tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c:87: link = calloc(sizeof(struct bpf_link *), prog_cnt);
./tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c:91: prog = calloc(sizeof(struct bpf_program *), prog_cnt);

Quoted 37 lines

Lukas

Quoted 31 lines

Same issue with calloc, kvcalloc, kvmalloc_array etc.

Bleat if sizeof is the first argument.

---
v2:
1. Acked-by Joe Perches.
2. Use lore links in Link tag.

---
 scripts/checkpatch.pl | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/checkpatch.pl +1 -2
index 1e5e66ae5a52..a9a9dc277cff 100755
@@ -7128,7 +7128,7 @@ sub process {
                }
 
 # check for alloc argument mismatch
-               if ($line =~ /\b((?:devm_)?(?:kcalloc|kmalloc_array))\s*\(\s*sizeof\b/) {
+               if ($line =~ /\b((?:devm_)?((?:k|kv)?(calloc|malloc_array)(?:_node)?))\s*\(\s*sizeof\b/) {
                        WARN("ALLOC_ARRAY_ARGS",
                             "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
                }
--
Quoted 2 lines

2.17.1

.

Signature · 2 lines
BR,
Liao, Chang
#4 lore raw
In reply to Liao Chang

Le 04/11/2022 à 08:05, Liao Chang a écrit :

Quoted 24 lines

These array allocator family are sometimes misused with the first and second arguments switchted.

Same issue with calloc, kvcalloc, kvmalloc_array etc.

Bleat if sizeof is the first argument.

---
v2:
1. Acked-by Joe Perches.
2. Use lore links in Link tag.

---
  scripts/checkpatch.pl | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
scripts/checkpatch.pl +0 -0
index 1e5e66ae5a52..a9a9dc277cff 100755

scripts/checkpatch.pl sub process { · line 7134

Quoted 8 lines
scripts/checkpatch.pl +1 -1
@@ -7128,7 +7128,7 @@ sub process {
  		}
 
  # check for alloc argument mismatch
-		if ($line =~ /\b((?:devm_)?(?:kcalloc|kmalloc_array))\s*\(\s*sizeof\b/) {
+		if ($line =~ /\b((?:devm_)?((?:k|kv)?(calloc|malloc_array)(?:_node)?))\s*\(\s*sizeof\b/) {
  			WARN("ALLOC_ARRAY_ARGS",
  			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
  		}

Hi,

Should the devm_ and not devm_ cases be separated?

In the devm_case, sizeof will never be just after the first '('.

CJ

maintainer
#5 lore raw
Quoted 36 lines · Christophe JAILLET

Le 04/11/2022 à 08:05, Liao Chang a écrit :

Quoted 24 lines

These array allocator family are sometimes misused with the first and second arguments switchted.

Same issue with calloc, kvcalloc, kvmalloc_array etc.

Bleat if sizeof is the first argument.

---
v2:
1. Acked-by Joe Perches.
2. Use lore links in Link tag.

---
  scripts/checkpatch.pl | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
scripts/checkpatch.pl +0 -0
index 1e5e66ae5a52..a9a9dc277cff 100755

scripts/checkpatch.pl sub process { · line 7134

Quoted 8 lines
scripts/checkpatch.pl +1 -1
@@ -7128,7 +7128,7 @@ sub process {
  		}
 
  # check for alloc argument mismatch
-		if ($line =~ /\b((?:devm_)?(?:kcalloc|kmalloc_array))\s*\(\s*sizeof\b/) {
+		if ($line =~ /\b((?:devm_)?((?:k|kv)?(calloc|malloc_array)(?:_node)?))\s*\(\s*sizeof\b/) {
  			WARN("ALLOC_ARRAY_ARGS",
  			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
  		}

Hi,

Quoted 3 lines

Should the devm_ and not devm_ cases be separated?

In the devm_case, sizeof will never be just after the first '('.

Right.

Likely this works better:

---
 scripts/checkpatch.pl | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
scripts/checkpatch.pl +8 -2
index 7be93c3df2bcb..7f37976a9f8b5 100755
@@ -7145,12 +7145,18 @@ sub process {
 			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
 		}
 
-# check for alloc argument mismatch
-		if ($line =~ /\b((?:devm_)?(?:kcalloc|kmalloc_array))\s*\(\s*sizeof\b/) {
+# check for alloc argument mismatch in alloc functions
+		if ($line =~ /\b(((?:k|kv)?(calloc|malloc_array)(?:_node)?))\s*\(\s*sizeof\b/) {
 			WARN("ALLOC_ARRAY_ARGS",
 			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
 		}
 
+# check for alloc argument mismatch in devm_ alloc functions
+		if ($line =~ /\b(devm_((?:k|kv)?(calloc|malloc_array)(?:_node)?))\s*\(\s*$FuncArg\s*,\s*sizeof\b/) {
+			WARN("ALLOC_ARRAY_ARGS",
+			     "$1 uses number as second arg, sizeof is generally wrong\n" . $herecurr);
+		}
+
 # check for multiple semicolons
 		if ($line =~ /;\s*;\s*$/) {
 			if (WARN("ONE_SEMICOLON",
#6 lore raw

Le 05/11/2022 à 18:24, Joe Perches a écrit :

Quoted 62 lines · Christophe JAILLET
Quoted 35 lines

Le 04/11/2022 à 08:05, Liao Chang a écrit :

Quoted 24 lines

These array allocator family are sometimes misused with the first and second arguments switchted.

Same issue with calloc, kvcalloc, kvmalloc_array etc.

Bleat if sizeof is the first argument.

---
v2:
1. Acked-by Joe Perches.
2. Use lore links in Link tag.

---
   scripts/checkpatch.pl | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
scripts/checkpatch.pl +0 -0
index 1e5e66ae5a52..a9a9dc277cff 100755

scripts/checkpatch.pl sub process { · line 7134

Quoted 8 lines
scripts/checkpatch.pl +1 -1
@@ -7128,7 +7128,7 @@ sub process {
   		}
 
   # check for alloc argument mismatch
-		if ($line =~ /\b((?:devm_)?(?:kcalloc|kmalloc_array))\s*\(\s*sizeof\b/) {
+		if ($line =~ /\b((?:devm_)?((?:k|kv)?(calloc|malloc_array)(?:_node)?))\s*\(\s*sizeof\b/) {
   			WARN("ALLOC_ARRAY_ARGS",
   			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
   		}

Hi,

Quoted 3 lines

Should the devm_ and not devm_ cases be separated?

In the devm_case, sizeof will never be just after the first '('.

Right.

Likely this works better:

---
  scripts/checkpatch.pl | 10 ++++++++--
  1 file changed, 8 insertions(+), 2 deletions(-)
scripts/checkpatch.pl +2 -2
index 7be93c3df2bcb..7f37976a9f8b5 100755
@@ -7145,12 +7145,18 @@ sub process {
  			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
  		}
 
-# check for alloc argument mismatch
-		if ($line =~ /\b((?:devm_)?(?:kcalloc|kmalloc_array))\s*\(\s*sizeof\b/) {
+# check for alloc argument mismatch in alloc functions
+		if ($line =~ /\b(((?:k|kv)?(calloc|malloc_array)(?:_node)?))\s*\(\s*sizeof\b/) {
  			WARN("ALLOC_ARRAY_ARGS",
  			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
  		}

scripts/checkpatch.pl sub process { · line 7162

Quoted 10 lines
scripts/checkpatch.pl +6 -0
 
+# check for alloc argument mismatch in devm_ alloc functions
+		if ($line =~ /\b(devm_((?:k|kv)?(calloc|malloc_array)(?:_node)?))\s*\(\s*$FuncArg\s*,\s*sizeof\b/) {
+			WARN("ALLOC_ARRAY_ARGS",
+			     "$1 uses number as second arg, sizeof is generally wrong\n" . $herecurr);
+		}
+
  # check for multiple semicolons
  		if ($line =~ /;\s*;\s*$/) {
  			if (WARN("ONE_SEMICOLON",

+1

#7 lore raw
Quoted 64 lines · Christophe JAILLET

Le 05/11/2022 à 18:24, Joe Perches a écrit :

Quoted 62 lines · Christophe JAILLET
Quoted 34 lines

Le 04/11/2022 à 08:05, Liao Chang a écrit :

Quoted 24 lines

These array allocator family are sometimes misused with the first and second arguments switchted.

Same issue with calloc, kvcalloc, kvmalloc_array etc.

Bleat if sizeof is the first argument.

---
v2:
1. Acked-by Joe Perches.
2. Use lore links in Link tag.

---
   scripts/checkpatch.pl | 2 +-

   1 file changed, 1 insertion(+), 1 deletion(-)
scripts/checkpatch.pl +0 -0
index 1e5e66ae5a52..a9a9dc277cff 100755

scripts/checkpatch.pl sub process { · line 7133

Quoted 7 lines
scripts/checkpatch.pl +1 -1
@@ -7128,7 +7128,7 @@ sub process {
            }
       # check for alloc argument mismatch
-        if ($line =~ /\b((?:devm_)?(?:kcalloc|kmalloc_array))\s*\(\s*sizeof\b/) {
+        if ($line =~ /\b((?:devm_)?((?:k|kv)?(calloc|malloc_array)(?:_node)?))\s*\(\s*sizeof\b/) {
                WARN("ALLOC_ARRAY_ARGS",
                     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
            }

Hi,

Quoted 3 lines

Should the devm_ and not devm_ cases be separated?

In the devm_case, sizeof will never be just after the first '('.

Right.

Likely this works better:
---
  scripts/checkpatch.pl | 10 ++++++++--

  1 file changed, 8 insertions(+), 2 deletions(-)
scripts/checkpatch.pl +0 -0
index 7be93c3df2bcb..7f37976a9f8b5 100755
@@ -7145,12 +7145,18 @@ sub process {
                    "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
           }

  -# check for alloc argument mismatch
-        if ($line =~ /\b((?:devm_)?(?:kcalloc|kmalloc_array))\s*\(\s*sizeof\b/) {
+# check for alloc argument mismatch in alloc functions
+        if ($line =~ /\b(((?:k|kv)?(calloc|malloc_array)(?:_node)?))\s*\(\s*sizeof\b/) { WARN("ALLOC_ARRAY_ARGS",
                   "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr); }
  +# check for alloc argument mismatch in devm_ alloc functions
+        if ($line =~ /\b(devm_((?:k|kv)?(calloc|malloc_array)(?:_node)?))\s*\(\s*$FuncArg\s*,\s*sizeof\b/) { +            WARN("ALLOC_ARRAY_ARGS",

Quoted 9 lines
Quoted 7 lines

+                 "$1 uses number as second arg, sizeof is generally wrong\n" . $herecurr); +        }
+

  # check for multiple semicolons
          if ($line =~ /;\s*;\s*$/) {
              if (WARN("ONE_SEMICOLON",

+1

+1, thanks.

BTW, i find the v2 has been added to the -mm mm-nonmm-unstable branch, do i need to submit new version includes above change?

Quoted 2 lines

.

Signature · 2 lines
BR,
Liao, Chang