smatch.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com>
To: dan.carpenter@linaro.org, error27@gmail.com
Cc: smatch@vger.kernel.org, christophe.jaillet@wanadoo.fr,
	harshit.m.mogalapalli@oracle.com,
	harshit.m.mogalapalli@gmail.com
Subject: [PATCH] check_ida_alloc: Improve check to not warn when min is '1'
Date: Thu, 26 Oct 2023 05:01:17 -0700	[thread overview]
Message-ID: <20231026120117.1025540-1-harshit.m.mogalapalli@oracle.com> (raw)

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@oracle.com>
---
 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


             reply	other threads:[~2023-10-26 12:01 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-26 12:01 Harshit Mogalapalli [this message]
2023-10-26 12:27 ` [PATCH] check_ida_alloc: Improve check to not warn when min is '1' Dan Carpenter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231026120117.1025540-1-harshit.m.mogalapalli@oracle.com \
    --to=harshit.m.mogalapalli@oracle.com \
    --cc=christophe.jaillet@wanadoo.fr \
    --cc=dan.carpenter@linaro.org \
    --cc=error27@gmail.com \
    --cc=harshit.m.mogalapalli@gmail.com \
    --cc=smatch@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).