Skip to contentSkip to conversation

Add str_alloc_free()/str_free_alloc helper and

PATCH Superseded Jiazi Li 2 participants 6 messages Apr 30 – May 1

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Jiazi Li

author
#0 lore raw

v2 cover Add str_alloc_free()/str_free_alloc helper and

Currently finds 4 locations:
./drivers/net/wireless/realtek/rtw89/fw.c:2557:7-12: opportunity for str_alloc_free(valid) ./drivers/net/wireless/realtek/rtw89/fw.c:2693:7-12: opportunity for str_alloc_free(valid) ./drivers/android/tests/binder_alloc_kunit.c:196:6-21: opportunity for str_alloc_free(alloc -> pages [ i ]) ./mm/slub.c:1634:3-8: opportunity for str_alloc_free(alloc)

Jiazi Li (2):
lib/string_choices: Add str_alloc_free() helper
coccinelle: Add rules to find str_alloc_free() replacements

 include/linux/string_choices.h              |   6 +
 scripts/coccinelle/api/string_choices.cocci | 228 ++++++++++++--------
 2 files changed, 139 insertions(+), 95 deletions(-)
Signature · 1 line
2.49.0
author
#1 lore raw

PATCH v2 1/2 lib/string_choices: Add str_alloc_free() helper

Add str_alloc_free() helper to return "alloc" or "free" string literal depending on the boolean argument. Also add the inversed variant str_free_alloc().

---
V2: Sorted function alphabetically.

---
 include/linux/string_choices.h | 6 ++++++
 1 file changed, 6 insertions(+)
include/linux/string_choices.h +6 -0
index ee84087d4b26..65f346db95ba 100644
@@ -17,6 +17,12 @@
 
 #include <linux/types.h>
 
+static inline const char *str_alloc_free(bool v)
+{
+	return v ? "alloc" : "free";
+}
+#define str_free_alloc(v)		str_alloc_free(!(v))
+
 static inline const char *str_assert_deassert(bool v)
 {
 	return v ? "assert" : "deassert";
Signature · 1 line
2.49.0
#2 lore raw

Re: [PATCH v2 1/2] lib/string_choices: Add str_alloc_free() helper

Quoted 6 lines · Jiazi Li

Add str_alloc_free() helper to return "alloc" or "free" string literal depending on the boolean argument. Also add the inversed variant str_free_alloc().

You are not supposed to randomly put some tags without approval. I didn't suggest anything to this patch, I *reviewed* the first version and commented on it, but I have *not* given any tag (neither Suggested-by, nor Reviewed-by, nor anything else).

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author
#3 lore raw

PATCH v2 2/2 coccinelle: Add rules to find str_alloc_free() replacements

In reply to Jiazi Li

Add rules for finding places where str_alloc_free()/str_free_alloc can be used.
This currently finds 4 locations.

Adjust the position of some coccinelle rules, so they sorted in the same order as they appear in the header.

---
V2: Keep coccinelle stances sorted in the same order as they appear in the header.

---
 scripts/coccinelle/api/string_choices.cocci | 228 ++++++++++++--------
 1 file changed, 133 insertions(+), 95 deletions(-)
scripts/coccinelle/api/string_choices.cocci +133 -95
index 375045086912..516b30828380 100644
@@ -7,109 +7,81 @@ virtual patch
 virtual context
 virtual report
 
-@str_plural depends on patch@
+@str_alloc_free depends on patch disable neg_if_exp@
 expression E;
 @@
-(
--	((E == 1) ? "" : "s")
-+	str_plural(E)
-|
--	((E > 1) ? "s" : "")
-+	str_plural(E)
-)
+-      ((E) ? "alloc" : "free")
++      str_alloc_free(E)
 
-@str_plural_r depends on !patch@
+@str_alloc_free_r depends on !patch disable neg_if_exp@
 expression E;
 position P;
 @@
-(
-*	(E@P == 1) ? "" : "s"
-|
-*	(E@P > 1) ? "s" : ""
-)
+*      E@P ? "alloc" : "free"
 
 @script:python depends on report@
-p << str_plural_r.P;
-e << str_plural_r.E;
+p << str_alloc_free_r.P;
+e << str_alloc_free_r.E;
 @@
 
-coccilib.report.print_report(p[0], "opportunity for str_plural(%s)" % e)
+coccilib.report.print_report(p[0], "opportunity for str_alloc_free(%s)" % e)
 
-@str_up_down depends on patch disable neg_if_exp@
+@str_free_alloc depends on patch disable neg_if_exp@
 expression E;
 @@
--	((E) ? "up" : "down")
-+	str_up_down(E)
+-      ((E) ? "free" : "alloc")
++      str_free_alloc(E)
 
-@str_up_down_r depends on !patch disable neg_if_exp@
+@str_free_alloc_r depends on !patch disable neg_if_exp@
 expression E;
 position P;
 @@
-*	E@P ? "up" : "down"
+*      E@P ? "free" : "alloc"
 
 @script:python depends on report@
-p << str_up_down_r.P;
-e << str_up_down_r.E;
+p << str_free_alloc_r.P;
+e << str_free_alloc_r.E;
 @@
 
-coccilib.report.print_report(p[0], "opportunity for str_up_down(%s)" % e)
+coccilib.report.print_report(p[0], "opportunity for str_free_alloc(%s)" % e)
 
-@str_down_up depends on patch disable neg_if_exp@
-expression E;
-@@
--      ((E) ? "down" : "up")
-+      str_down_up(E)
-
-@str_down_up_r depends on !patch disable neg_if_exp@
-expression E;
-position P;
-@@
-*      E@P ? "down" : "up"
-
-@script:python depends on report@
-p << str_down_up_r.P;
-e << str_down_up_r.E;
-@@
-
-coccilib.report.print_report(p[0], "opportunity for str_down_up(%s)" % e)
-
-@str_true_false depends on patch disable neg_if_exp@
+@str_enable_disable depends on patch@
 expression E;
 @@
--      ((E) ? "true" : "false")
-+      str_true_false(E)
+-      ((E) ? "enable" : "disable")
++      str_enable_disable(E)
 
-@str_true_false_r depends on !patch disable neg_if_exp@
+@str_enable_disable_r depends on !patch@
 expression E;
 position P;
 @@
-*      E@P ? "true" : "false"
+*      E@P ? "enable" : "disable"
 
 @script:python depends on report@
-p << str_true_false_r.P;
-e << str_true_false_r.E;
+p << str_enable_disable_r.P;
+e << str_enable_disable_r.E;
 @@
 
-coccilib.report.print_report(p[0], "opportunity for str_true_false(%s)" % e)
+coccilib.report.print_report(p[0], "opportunity for str_enable_disable(%s)" % e)
 
-@str_false_true depends on patch disable neg_if_exp@
+@str_enabled_disabled depends on patch@
 expression E;
 @@
--      ((E) ? "false" : "true")
-+      str_false_true(E)
+-      ((E) ? "enabled" : "disabled")
++      str_enabled_disabled(E)
 
-@str_false_true_r depends on !patch disable neg_if_exp@
+@str_enabled_disabled_r depends on !patch@
 expression E;
 position P;
 @@
-*      E@P ? "false" : "true"
+*      E@P ? "enabled" : "disabled"
 
 @script:python depends on report@
-p << str_false_true_r.P;
-e << str_false_true_r.E;
+p << str_enabled_disabled_r.P;
+e << str_enabled_disabled_r.E;
 @@
 
-coccilib.report.print_report(p[0], "opportunity for str_false_true(%s)" % e)
+coccilib.report.print_report(p[0], "opportunity for str_enabled_disabled(%s)" % e)
 
 @str_hi_lo depends on patch disable neg_if_exp@
 expression E;
@@ -187,43 +159,24 @@ e << str_low_high_r.E;
 
 coccilib.report.print_report(p[0], "opportunity for str_low_high(%s)" % e)
 
-@str_enable_disable depends on patch@
-expression E;
-@@
--      ((E) ? "enable" : "disable")
-+      str_enable_disable(E)
-
-@str_enable_disable_r depends on !patch@
-expression E;
-position P;
-@@
-*      E@P ? "enable" : "disable"
-
-@script:python depends on report@
-p << str_enable_disable_r.P;
-e << str_enable_disable_r.E;
-@@
-
-coccilib.report.print_report(p[0], "opportunity for str_enable_disable(%s)" % e)
-
-@str_enabled_disabled depends on patch@
+@str_on_off depends on patch@
 expression E;
 @@
--      ((E) ? "enabled" : "disabled")
-+      str_enabled_disabled(E)
+-      ((E) ? "on" : "off")
++      str_on_off(E)
 
-@str_enabled_disabled_r depends on !patch@
+@str_on_off_r depends on !patch@
 expression E;
 position P;
 @@
-*      E@P ? "enabled" : "disabled"
+*      E@P ? "on" : "off"
 
 @script:python depends on report@
-p << str_enabled_disabled_r.P;
-e << str_enabled_disabled_r.E;
+p << str_on_off_r.P;
+e << str_on_off_r.E;
 @@
 
-coccilib.report.print_report(p[0], "opportunity for str_enabled_disabled(%s)" % e)
+coccilib.report.print_report(p[0], "opportunity for str_on_off(%s)" % e)
 
 @str_read_write depends on patch disable neg_if_exp@
 expression E;
@@ -263,24 +216,81 @@ e << str_write_read_r.E;
 
 coccilib.report.print_report(p[0], "opportunity for str_write_read(%s)" % e)
 
-@str_on_off depends on patch@
+@str_true_false depends on patch disable neg_if_exp@
 expression E;
 @@
--      ((E) ? "on" : "off")
-+      str_on_off(E)
+-      ((E) ? "true" : "false")
++      str_true_false(E)
 
-@str_on_off_r depends on !patch@
+@str_true_false_r depends on !patch disable neg_if_exp@
 expression E;
 position P;
 @@
-*      E@P ? "on" : "off"
+*      E@P ? "true" : "false"
 
 @script:python depends on report@
-p << str_on_off_r.P;
-e << str_on_off_r.E;
+p << str_true_false_r.P;
+e << str_true_false_r.E;
 @@
 
-coccilib.report.print_report(p[0], "opportunity for str_on_off(%s)" % e)
+coccilib.report.print_report(p[0], "opportunity for str_true_false(%s)" % e)
+
+@str_false_true depends on patch disable neg_if_exp@
+expression E;
+@@
+-      ((E) ? "false" : "true")
++      str_false_true(E)
+
+@str_false_true_r depends on !patch disable neg_if_exp@
+expression E;
+position P;
+@@
+*      E@P ? "false" : "true"
+
+@script:python depends on report@
+p << str_false_true_r.P;
+e << str_false_true_r.E;
+@@
+
+coccilib.report.print_report(p[0], "opportunity for str_false_true(%s)" % e)
+
+@str_up_down depends on patch disable neg_if_exp@
+expression E;
+@@
+-	((E) ? "up" : "down")
++	str_up_down(E)
+
+@str_up_down_r depends on !patch disable neg_if_exp@
+expression E;
+position P;
+@@
+*	E@P ? "up" : "down"
+
+@script:python depends on report@
+p << str_up_down_r.P;
+e << str_up_down_r.E;
+@@
+
+coccilib.report.print_report(p[0], "opportunity for str_up_down(%s)" % e)
+
+@str_down_up depends on patch disable neg_if_exp@
+expression E;
+@@
+-      ((E) ? "down" : "up")
++      str_down_up(E)
+
+@str_down_up_r depends on !patch disable neg_if_exp@
+expression E;
+position P;
+@@
+*      E@P ? "down" : "up"
+
+@script:python depends on report@
+p << str_down_up_r.P;
+e << str_down_up_r.E;
+@@
+
+coccilib.report.print_report(p[0], "opportunity for str_down_up(%s)" % e)
 
 @str_yes_no depends on patch@
 expression E;
@@ -300,3 +310,31 @@ e << str_yes_no_r.E;
 @@
 
 coccilib.report.print_report(p[0], "opportunity for str_yes_no(%s)" % e)
+
+@str_plural depends on patch@
+expression E;
+@@
+(
+-	((E == 1) ? "" : "s")
++	str_plural(E)
+|
+-	((E > 1) ? "s" : "")
++	str_plural(E)
+)
+
+@str_plural_r depends on !patch@
+expression E;
+position P;
+@@
+(
+*	(E@P == 1) ? "" : "s"
+|
+*	(E@P > 1) ? "s" : ""
+)
+
+@script:python depends on report@
+p << str_plural_r.P;
+e << str_plural_r.E;
+@@
+
+coccilib.report.print_report(p[0], "opportunity for str_plural(%s)" % e)
Signature · 1 line
2.49.0
#4 lore raw

Re: [PATCH v2 2/2] coccinelle: Add rules to find str_alloc_free() replacements

Quoted 7 lines · Jiazi Li

Add rules for finding places where str_alloc_free()/str_free_alloc can be used.
This currently finds 4 locations.

Adjust the position of some coccinelle rules, so they sorted in the same order as they appear in the header.

...

Quoted 1 line

I indeed suggested sorting the coccinelle script to follow the order of appearance of the functions in the C file. However, this patch has two changes in one, and the second one I have not suggested. So, split these to two (the first one is sorting one, which may have my Suggested-by tag, and the second one is adding your new helper).

Quoted 2 lines

V2: Keep coccinelle stances sorted in the same order as they appear in the header.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw
In reply to Jiazi Li
Quoted 6 lines · Jiazi Li

Currently finds 4 locations:
./drivers/net/wireless/realtek/rtw89/fw.c:2557:7-12: opportunity for str_alloc_free(valid) ./drivers/net/wireless/realtek/rtw89/fw.c:2693:7-12: opportunity for str_alloc_free(valid) ./drivers/android/tests/binder_alloc_kunit.c:196:6-21: opportunity for str_alloc_free(alloc -> pages [ i ]) ./mm/slub.c:1634:3-8: opportunity for str_alloc_free(alloc)

So, add a first user at least to make sure you haven't added a dead code, probably rtw89 is the best choice as it has more callsites.

Signature · 2 lines
With Best Regards,
Andy Shevchenko