From 19fcae43a3acefe6e24a2097dddce158298b6966 Mon Sep 17 00:00:00 2001 From: Christophe JAILLET <679652+tititiou36@users.noreply.github.com> Date: Thu, 19 May 2022 20:48:37 +0200 Subject: [PATCH 1/3] Refactor and add new functions tested by check_kvmalloc_array_zero() Use a table of searched functions that use __GFP_ZERO and to proposed alternatives. This makes the code easier to expand. While at it, scan for new functions. Signed-off-by: Christophe JAILLET <679652+tititiou36@users.noreply.github.com> --- check_kvmalloc_array_zero.c | 51 +++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/check_kvmalloc_array_zero.c b/check_kvmalloc_array_zero.c index 9aeab928..fc29c556 100644 --- a/check_kvmalloc_array_zero.c +++ b/check_kvmalloc_array_zero.c @@ -16,18 +16,41 @@ */ /* - * This checks complains when we have kvmalloc_array used with - * __GFP_ZERO flag, we can instead use kvcalloc which is designed - * for it. - * Similarly kmalloc_array + __GPF_ZERO = kcalloc. + * This check complains when we have a function used with __GFP_ZERO flag and + * we can use a less verbose alternative. + * + * Example: kmalloc_array + __GPF_ZERO = kcalloc */ #include "smatch.h" static int my_id; +struct match_alloc_struct { + const char *function; + const char *alternative; + int flag_pos; +}; + +struct match_alloc_struct match_alloc_functions[] = { + { "kmalloc", "kzalloc", 1 }, + { "kmalloc_node", "kzalloc_node", 1 }, + + { "kmalloc_array", "kcalloc", 2 }, + { "kmalloc_array_node", "kcalloc_node", 2 }, + + { "kvmalloc", "kvzalloc", 1 }, + { "kvmalloc_node", "kvzalloc_node", 1 }, + + { "kvmalloc_array", "kvcalloc", 2 }, + + { "kmem_cache_alloc", "kmem_cache_zalloc", 1 }, + { NULL, NULL, 0 } +}; + static void match_alloc(const char *fn, struct expression *expr, void *_arg) { + struct match_alloc_struct *entry = match_alloc_functions; unsigned long gfp; int arg_nr = PTR_INT(_arg); struct expression *arg_expr; @@ -41,20 +64,28 @@ static void match_alloc(const char *fn, struct expression *expr, void *_arg) return; if (sval.uvalue & gfp) { - if (strcmp(fn,"kvmalloc_array") == 0) - sm_warning("Please consider using kvcalloc instead"); - else - sm_warning("Please consider using kcalloc instead"); + while (entry->function) { + if (strcmp(fn, entry->function) == 0) { + sm_warning("Please consider using %s instead of %s", + entry->alternative, function->function); + break; + } + entry++; + } } } void check_kvmalloc_array_zero(int id) { + struct match_alloc_struct *entry = match_alloc_functions; + if (option_project != PROJ_KERNEL) return; my_id = id; - add_function_hook("kvmalloc_array", &match_alloc, INT_PTR(2)); - add_function_hook("kmalloc_array", &match_alloc, INT_PTR(2)); + while (entry->function) { + add_function_hook(entry->function, &match_alloc, INT_PTR(entry->flag_pos)); + entry++; + } } From 723c7197ae990675ba6cbdfe8eb908621e475e2e Mon Sep 17 00:00:00 2001 From: Christophe Jaillet <679652+tititiou36@users.noreply.github.com> Date: Thu, 19 May 2022 21:59:40 +0200 Subject: [PATCH 2/3] Update check_kvmalloc_array_zero.c Fix a Copy/Paste error --- check_kvmalloc_array_zero.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/check_kvmalloc_array_zero.c b/check_kvmalloc_array_zero.c index fc29c556..b1960a0f 100644 --- a/check_kvmalloc_array_zero.c +++ b/check_kvmalloc_array_zero.c @@ -67,7 +67,7 @@ static void match_alloc(const char *fn, struct expression *expr, void *_arg) while (entry->function) { if (strcmp(fn, entry->function) == 0) { sm_warning("Please consider using %s instead of %s", - entry->alternative, function->function); + entry->alternative, entry->function); break; } entry++; From 983618cadb9d097bdf2809932efcb6370fb74167 Mon Sep 17 00:00:00 2001 From: Christophe Jaillet <679652+tititiou36@users.noreply.github.com> Date: Sat, 21 May 2022 09:05:43 +0200 Subject: [PATCH 3/3] Report the correct function name that triggered the warning The warning message has a hard-coded 'test_bit' but several other function can trigger this warning. So report the correct function name to be less puzzling. --- check_test_bit.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/check_test_bit.c b/check_test_bit.c index a26a12ab..b09139aa 100644 --- a/check_test_bit.c +++ b/check_test_bit.c @@ -35,7 +35,7 @@ static void match_test_bit(const char *fn, struct expression *expr, void *data) macro = get_macro_name(arg->pos); if (macro && strstr(macro, "cpu_has")) return; - sm_warning("test_bit() takes a bit number"); + sm_warning("%s() takes a bit number", fn); } void check_test_bit(int id)