checkpatch: Add check for array allocator family argument order
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
@@ -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
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_arrayetc.Bleat if sizeof is the first argument.
Signed-off-by Liao Chang <[email protected]>Acked-by Joe Perches <[email protected]>---
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@@ -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
Joe Perches
maintainerQuoted 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
Acked-by Lukas Bulwahn <[email protected]>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.
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
Acked-by Lukas Bulwahn <[email protected]>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_arrayetc.Bleat if sizeof is the first argument.
Signed-off-by Liao Chang <[email protected]>Acked-by Joe Perches <[email protected]>---
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@@ -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
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_arrayetc.Bleat if sizeof is the first argument.
Signed-off-by Liao Chang <[email protected]>Acked-by Joe Perches <[email protected]>---
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
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
Joe Perches
maintainerQuoted 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_arrayetc.Bleat if sizeof is the first argument.
Signed-off-by Liao Chang <[email protected]>Acked-by Joe Perches <[email protected]>---
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
-0scripts/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 notdevm_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
@@ -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",
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_arrayetc.Bleat if sizeof is the first argument.
Signed-off-by Liao Chang <[email protected]>Acked-by Joe Perches <[email protected]>---
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
-0scripts/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 notdevm_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@@ -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
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_arrayetc.Bleat if sizeof is the first argument.
Signed-off-by Liao Chang <[email protected]>Acked-by Joe Perches <[email protected]>---
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
-0scripts/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 notdevm_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@@ -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 indevm_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