Powered by Linux
[PATCH] check_ida_alloc: Improve check to not warn when min is '1' — Semantic Matching Tool

[PATCH] check_ida_alloc: Improve check to not warn when min is '1'

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This check warns when the max argument of ida_alloc_range is not of the
the form "2^x-1".

It is fine to not warn when min argument is 1 and max argument is a
power of 2.

Before this patch:
drivers/net/ethernet/mellanox/mlx5/core/lib/macsec_fs.c:587 macsec_fs_tx_setup_fte() error: Calling ida_alloc_range() with a 'max' argument which is a power of 2. -1 missing?

After this patch:
Smatch will not warn as the min argument is one.

Signed-off-by: Harshit Mogalapalli <harshit.m.mogalapalli@xxxxxxxxxx>
---
 check_ida_alloc.c | 28 +++++++++++++++++++++++++---
 1 file changed, 25 insertions(+), 3 deletions(-)

diff --git a/check_ida_alloc.c b/check_ida_alloc.c
index 7f1f7463..e29b8ff1 100644
--- a/check_ida_alloc.c
+++ b/check_ida_alloc.c
@@ -58,7 +58,7 @@ static bool is_power_of_two(struct expression *expr)
 	return false;
 }
 
-static void match_ida_alloc(const char *fn, struct expression *expr, void *_arg_nr)
+static void match_ida_alloc_max(const char *fn, struct expression *expr, void *_arg_nr)
 {
 	int arg_nr = PTR_INT(_arg_nr);
 	struct expression *arg_expr;
@@ -71,6 +71,28 @@ static void match_ida_alloc(const char *fn, struct expression *expr, void *_arg_
 			 fn);
 }
 
+static void match_ida_alloc_range(const char *fn, struct expression *expr, void *info)
+{
+	struct expression *arg_expr1, *arg_expr2;
+
+	sval_t sval;
+
+	arg_expr1 = get_argument_from_call_expr(expr->args, 1);
+	arg_expr1 = strip_expr(arg_expr1);
+	arg_expr2 = get_argument_from_call_expr(expr->args, 2);
+	arg_expr2 = strip_expr(arg_expr2);
+
+	if (!get_implied_value(arg_expr1, &sval))
+		return;
+
+	if (sval.uvalue == 1)
+		return;
+
+	if (is_power_of_two(arg_expr2))
+		sm_error("Calling %s() with a 'max' argument which is a power of 2. -1 missing?",
+			  fn);
+}
+
 void check_ida_alloc(int id)
 {
 	if (option_project != PROJ_KERNEL)
@@ -78,6 +100,6 @@ void check_ida_alloc(int id)
 
 	my_id = id;
 
-	add_function_hook("ida_alloc_max", &match_ida_alloc, INT_PTR(1));
-	add_function_hook("ida_alloc_range", &match_ida_alloc, INT_PTR(2));
+	add_function_hook("ida_alloc_max", &match_ida_alloc_max, INT_PTR(1));
+	add_function_hook("ida_alloc_range", &match_ida_alloc_range, NULL);
 }
-- 
2.39.3




[Index of Archives]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Big List of Linux Books]

  Powered by Linux