All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] smatch_allocations: Record the function that allocates memory
@ 2022-08-15 16:20 Christophe JAILLET
  2022-08-15 16:21 ` [PATCH 2/2] check_freeing_devm: Take advantage of the new add_allocation hook Christophe JAILLET
  2022-08-16  8:10 ` [PATCH 1/2] smatch_allocations: Record the function that allocates memory Dan Carpenter
  0 siblings, 2 replies; 4+ messages in thread
From: Christophe JAILLET @ 2022-08-15 16:20 UTC (permalink / raw)
  To: smatch; +Cc: Christophe JAILLET

Keep track of the name of the function that did the memory allocation.

Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
Not sure if it is the best way to save this information. Maybe it can
already be retrieved another way (from expr directly?)

An example of why it could be useful is in patch 2/2.
---
 smatch.h             | 1 +
 smatch_allocations.c | 1 +
 2 files changed, 2 insertions(+)

diff --git a/smatch.h b/smatch.h
index 36ae3497d8de..00a7563185e6 100644
--- a/smatch.h
+++ b/smatch.h
@@ -174,6 +174,7 @@ void call_name_sym_fns(struct name_sym_fn_list *list, struct expression *expr, c
 void call_string_hooks(struct string_hook_list *list, struct expression *expr, const char *str);
 
 struct allocation_info {
+	const char *fn_name;
 	const char *size_str;
 	struct expression *total_size;
 	struct expression *nr_elems;
diff --git a/smatch_allocations.c b/smatch_allocations.c
index 599f195a4c31..08957f3e0902 100644
--- a/smatch_allocations.c
+++ b/smatch_allocations.c
@@ -98,6 +98,7 @@ static void match_alloc(struct expression *expr, const char *name, struct symbol
 	struct allocation_info data = { };
 	alloc_hook *fn;
 
+	data.fn_name = info->name;
 	data.size_str = info->size;
 	data.zeroed = info->zeroed;
 
-- 
2.34.1

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] check_freeing_devm: Take advantage of the new add_allocation hook.
  2022-08-15 16:20 [PATCH 1/2] smatch_allocations: Record the function that allocates memory Christophe JAILLET
@ 2022-08-15 16:21 ` Christophe JAILLET
  2022-08-16  8:12   ` Dan Carpenter
  2022-08-16  8:10 ` [PATCH 1/2] smatch_allocations: Record the function that allocates memory Dan Carpenter
  1 sibling, 1 reply; 4+ messages in thread
From: Christophe JAILLET @ 2022-08-15 16:21 UTC (permalink / raw)
  To: smatch; +Cc: Christophe JAILLET

add_allocation_hook() already register many memory allocators. Use this
list and filter function that starts with devm_ to avoid duplicating part
of the list.

Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
Not sure it is a really elegant solution or an improvement.
---
 check_freeing_devm.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)

diff --git a/check_freeing_devm.c b/check_freeing_devm.c
index 51fa990f1b4a..b77cdd25769f 100644
--- a/check_freeing_devm.c
+++ b/check_freeing_devm.c
@@ -26,6 +26,17 @@ static void match_assign(const char *fn, struct expression *expr, void *unused)
 	set_state_expr(my_id, expr->left, &devm);
 }
 
+static void match_allocation(struct expression *expr,
+			     const char *name, struct symbol *sym,
+			     struct allocation_info *info)
+{
+	/*
+	 * Only handle devm_ memory allocator
+	 */
+	if (strncmp(info->fn_name, "devm_", 5) == 0)
+		match_assign(name, expr, NULL);
+}
+
 /*
  * This hook deals with things like:
  * ptr = devm_kmalloc(...);
@@ -96,12 +107,12 @@ void check_freeing_devm(int id)
 
 	my_id = id;
 
-	add_function_assign_hook("devm_kmalloc", &match_assign, NULL);
-	add_function_assign_hook("devm_kzalloc", &match_assign, NULL);
-	add_function_assign_hook("devm_kcalloc", &match_assign, NULL);
-	add_function_assign_hook("devm_kmalloc_array", &match_assign, NULL);
+	/*
+	 * We register all allocation functions, but only devm_ will be handled
+	 * in match_allocation()
+	 */
+	add_allocation_hook(&match_allocation);
 
-	add_function_assign_hook("devm_kmemdup", &match_assign, NULL);
 	add_function_assign_hook("devm_kstrdup", &match_assign, NULL);
 	add_function_assign_hook("devm_kasprintf", &match_assign, NULL);
 	add_function_assign_hook("devm_kvasprintf", &match_assign, NULL);
-- 
2.34.1

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/2] smatch_allocations: Record the function that allocates memory
  2022-08-15 16:20 [PATCH 1/2] smatch_allocations: Record the function that allocates memory Christophe JAILLET
  2022-08-15 16:21 ` [PATCH 2/2] check_freeing_devm: Take advantage of the new add_allocation hook Christophe JAILLET
@ 2022-08-16  8:10 ` Dan Carpenter
  1 sibling, 0 replies; 4+ messages in thread
From: Dan Carpenter @ 2022-08-16  8:10 UTC (permalink / raw)
  To: Christophe JAILLET; +Cc: smatch

On Mon, Aug 15, 2022 at 06:20:55PM +0200, Christophe JAILLET wrote:
> Keep track of the name of the function that did the memory allocation.
> 
> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
> ---
> Not sure if it is the best way to save this information. Maybe it can
> already be retrieved another way (from expr directly?)
> 

You could get it from the expr.  The expr is an assign expr

	foo = kmalloc(sizeof(*foo), GFP_KERNEL);

So you could do:

	while (expr->type == EXPR_ASSIGNMENT)
		expr = strip_expr(expr->right);

	if (expr->type != EXPR_CALL)
		return;

	name = expr_to_str(expr->fn);

At the same time putting it in the allocation_info feels like it might
be the right thing and it's basically no cost so I'm going to apply this.

regards,
dan carpenter

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/2] check_freeing_devm: Take advantage of the new add_allocation hook.
  2022-08-15 16:21 ` [PATCH 2/2] check_freeing_devm: Take advantage of the new add_allocation hook Christophe JAILLET
@ 2022-08-16  8:12   ` Dan Carpenter
  0 siblings, 0 replies; 4+ messages in thread
From: Dan Carpenter @ 2022-08-16  8:12 UTC (permalink / raw)
  To: Christophe JAILLET; +Cc: smatch

On Mon, Aug 15, 2022 at 06:21:01PM +0200, Christophe JAILLET wrote:
> add_allocation_hook() already register many memory allocators. Use this
> list and filter function that starts with devm_ to avoid duplicating part
> of the list.
> 
> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
> ---
> Not sure it is a really elegant solution or an improvement.

It's definitely where I want things to go.  Ideally the other string
allocation functions would get added to the allocation function list
but I'm not sure the right format for that.

Anyway, I've applied all three patches.  Thanks!

regards,
dan carpenter

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-08-16 10:25 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-15 16:20 [PATCH 1/2] smatch_allocations: Record the function that allocates memory Christophe JAILLET
2022-08-15 16:21 ` [PATCH 2/2] check_freeing_devm: Take advantage of the new add_allocation hook Christophe JAILLET
2022-08-16  8:12   ` Dan Carpenter
2022-08-16  8:10 ` [PATCH 1/2] smatch_allocations: Record the function that allocates memory Dan Carpenter

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.