Skip to contentSkip to conversation

coccinelle: Add some rules for string_chioces helpers.

PATCH In review Hongbo Li 3 participants 18 messages Sep 11, 2024 – Sep 28, 2024

Subsystems (2)
lore
Share & tools
lore

Hongbo Li

author
#0 lore raw

v3 cover coccinelle: Add some rules for string_chioces helpers.

We found that many of the detection rules for helpers in string_choices.h are missing. This series of patches is intended to complete these rules. We have verified in the latest kernel tree that these rules can detect many places where the string choices helper can be used as a replacement.

Here are some of the examples:

```
drivers/gpio/gpio-mvebu.c:917:6-18: opportunity for str_lo_hi(in_pol & msk)
drivers/pinctrl/nomadik/pinctrl-nomadik.c:1109:16-19: opportunity for str_high_low(val)
drivers/pinctrl/nomadik/pinctrl-nomadik.c:1116:13-16: opportunity for str_high_low(val
drivers/gpio/gpio-wcove.c:396:6-16: opportunity for str_hi_lo(ctli & 0x1)
drivers/pci/hotplug/pnv_php.c:367:10-41: opportunity for str_on_off(state == OPAL_PCI_SLOT_POWER_ON)
drivers/mtd/nand/raw/brcmnand/brcmnand.c:1844:40-63: opportunity for str_read_write(edu_cmd == EDU_CMD_READ)
drivers/mtd/nand/raw/brcmnand/brcmnand.c:1437:33-35: opportunity for str_on_off(wp)
drivers/mtd/nand/raw/brcmnand/brcmnand.c:1467:9-11: opportunity for str_on_off(wp)
drivers/greybus/svc.c:76:9-37: opportunity for str_enabled_disabled(gb_svc_watchdog_enabled ( svc ))
drivers/thermal/thermal_sysfs.c:59:29-36: opportunity for str_enabled_disabled(enabled)
drivers/dma-buf/st-dma-fence.c:508:10-35: opportunity for str_yes_no(dma_fence_is_signaled ( f2 ))
drivers/mmc/host/omap_hsmmc.c:1644:7-43: opportunity for str_enabled_disabled(host -> flags & HSMMC_SDIO_IRQ_ENABLED)
drivers/input/mouse/elan_i2c_core.c:202:2-4: opportunity for str_on_off(on)
drivers/macintosh/via-pmu.c:626:10-28: opportunity for str_enabled_disabled(option_server_mode)
drivers/usb/mtu3/mtu3_debugfs.c:482:5-39: opportunity for str_on_off(regulator_is_enabled ( otg_sx -> vbus ))
drivers/net/wireless/intel/iwlwifi/mvm/tt.c:355:4-10: opportunity for str_enable_disable(enable)
drivers/nfc/pn544/i2c.c:215:7-20: opportunity for str_low_high(polarity == 0)
drivers/media/tuners/tda18250.c:110:3-12: opportunity for str_true_false(triggered)
drivers/mtd/tests/torturetest.c:191:30-35: opportunity for str_enabled_disabled(check)
drivers/bus/intel-ixp4xx-eb.c:225:5-8: opportunity for str_enabled_disabled(val)
drivers/net/ethernet/huawei/hinic/hinic_port.c:1133:3-9: opportunity for str_enable_disable(enable)
drivers/net/ethernet/huawei/hinic/hinic_port.c:1245:3-9: opportunity for str_enable_disable(pfc_en)
drivers/media/pci/cx23885/cimax2.c:227:4-8: opportunity for str_read_write(read)
drivers/media/usb/em28xx/em28xx-video.c:941:4-9: opportunity for str_enabled_disabled(flags)
```

Changes since v2: https://patchwork.kernel.org/project/linux-hardening/cover/[email protected]/
- Remove unnecessary outer parentheses for new rules as suggested by julia, thanks.
- Remove unnecessary outer parentheses for old rules.

Changes since v1: https://patchwork.kernel.org/project/linux-hardening/cover/[email protected]/
- Add more rules for helpers in string_choices.h.

Thanks,
Hongbo.

Hongbo Li (10):
coccinelle: Add rules to find str_true_false() replacements
coccinelle: Add rules to find str_false_true() replacements
coccinelle: Add rules to find str_hi{gh}_lo{w}() replacements
coccinelle: Add rules to find str_lo{w}_hi{gh}() replacements
coccinelle: Add rules to find str_enable{d}_disable{d}() replacements
coccinelle: Add rules to find str_read_write() replacements
coccinelle: Add rules to find str_write_read() replacements
coccinelle: Add rules to find str_on_off() replacements
coccinelle: Add rules to find str_yes_no() replacements
coccinelle: Remove unnecessary parentheses for only one possible
change.

 scripts/coccinelle/api/string_choices.cocci | 240 +++++++++++++++++++-
 1 file changed, 232 insertions(+), 8 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v3 1/10 coccinelle: Add rules to find str_true_false() replacements

After str_true_false() has been introduced in the tree, we can add rules for finding places where str_true_false() can be used. A simple test can find over 10 locations.

---
 scripts/coccinelle/api/string_choices.cocci | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
scripts/coccinelle/api/string_choices.cocci +19 -0
index 5e729f187f22..6942ad7c4224 100644
@@ -85,3 +85,22 @@ 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@
+expression E;
+@@
+-      ((E) ? "true" : "false")
++      str_true_false(E)
+
+@str_true_false_r depends on !patch exists@
+expression E;
+position P;
+@@
+*      ((E@P) ? "true" : "false")
+
+@script:python depends on report@
+p << str_true_false_r.P;
+e << str_true_false_r.E;
+@@
+
+coccilib.report.print_report(p[0], "opportunity for str_true_false(%s)" % e)
Signature · 1 line
2.34.1
maintainer
#2 lore raw

Re: [cocci] [PATCH -next v3 01/10] coccinelle: Add rules to find str_true_false() replacements

Quoted 19 lines · Hongbo Li

After str_true_false() has been introduced in the tree, we can add rules for finding places where str_true_false() can be used. A simple test can find over 10 locations.

---
 scripts/coccinelle/api/string_choices.cocci | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
scripts/coccinelle/api/string_choices.cocci +2 -0
index 5e729f187f22..6942ad7c4224 100644
@@ -85,3 +85,22 @@ 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@

scripts/coccinelle/api/string_choices.cocci e << str_down_up_r.E; · line 99

Quoted 10 lines
scripts/coccinelle/api/string_choices.cocci +10 -0
+expression E;
+@@
+-      ((E) ? "true" : "false")
++      str_true_false(E)
+
+@str_true_false_r depends on !patch exists@
+expression E;
+position P;
+@@
+*      ((E@P) ? "true" : "false")

Hello,

The semantic patch is quite slow. Actually it tests a large number of cases, eg where the parentheses are present and where they are not.

A small optimization is possible in the non-patch case. The outer parentheses are not needed, because you will already get the same information whether they are there or not.

In contrast, for the patch case, the outer parentheses are needed, because if they are there we want to remove them, since they are not needed for the function call.

Could you update the depends on !patch cases to remove the outer parentheses?

Also, just one patch would be fine. There are many changes, but they are all sort of the same, so it would be easier just to see them all at once.

thanks,
julia

Quoted 11 lines
scripts/coccinelle/api/string_choices.cocci +7 -1
+
+@script:python depends on report@
+p << str_true_false_r.P;
+e << str_true_false_r.E;
+@@
+
+coccilib.report.print_report(p[0], "opportunity for str_true_false(%s)" % e)
--

2.34.1

#3 lore raw

Re: [cocci] [v3 01/10] coccinelle: Add rules to find str_true_false() replacements

Quoted 2 lines

The semantic patch is quite slow. Actually it tests a large number of cases, eg where the parentheses are present and where they are not.

Can such development concerns trigger any adjustments for further coccicheck configurations and provided SmPL scripts?

Regards,
Markus

author
#4 lore raw

Re: [PATCH -next v3 01/10] coccinelle: Add rules to find str_true_false() replacements

In reply to Julia Lawall
Quoted 43 lines · Julia Lawall
Quoted 19 lines · Hongbo Li

After str_true_false() has been introduced in the tree, we can add rules for finding places where str_true_false() can be used. A simple test can find over 10 locations.

---
  scripts/coccinelle/api/string_choices.cocci | 19 +++++++++++++++++++
  1 file changed, 19 insertions(+)
scripts/coccinelle/api/string_choices.cocci +2 -0
index 5e729f187f22..6942ad7c4224 100644
@@ -85,3 +85,22 @@ 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@

scripts/coccinelle/api/string_choices.cocci e << str_down_up_r.E; · line 99

Quoted 10 lines
scripts/coccinelle/api/string_choices.cocci +10 -0
+expression E;
+@@
+-      ((E) ? "true" : "false")
++      str_true_false(E)
+
+@str_true_false_r depends on !patch exists@
+expression E;
+position P;
+@@
+*      ((E@P) ? "true" : "false")

Hello,

The semantic patch is quite slow. Actually it tests a large number of cases, eg where the parentheses are present and where they are not.

A small optimization is possible in the non-patch case. The outer parentheses are not needed, because you will already get the same information whether they are there or not.

Quoted 7 lines

In contrast, for the patch case, the outer parentheses are needed, because if they are there we want to remove them, since they are not needed for the function call.

Could you update the depends on !patch cases to remove the outer parentheses?

You mean in non-patch case, we could just write like the following?:

+@str_true_false_r depends on !patch exists@
+expression E;
+position P;
+@@
+* (E@P) ? "true" : "false"

I have tested in my machine. The impact of this parenthesis on performance is very minimal.

With parentheses, the time on driver/ costs:

real 1m41.696s
user 85m24.069s
sys 1m8.891s

Without parentheses, the time on driver/ costs:

real 1m40.438s
user 85m53.987s
sys 1m7.981s

Thanks,
Hongbo

Quoted 18 lines

Also, just one patch would be fine. There are many changes, but they are all sort of the same, so it would be easier just to see them all at once.

thanks,
julia

Quoted 11 lines
scripts/coccinelle/api/string_choices.cocci +7 -1
+
+@script:python depends on report@
+p << str_true_false_r.P;
+e << str_true_false_r.E;
+@@
+
+coccilib.report.print_report(p[0], "opportunity for str_true_false(%s)" % e)
--

2.34.1

maintainer
#5 lore raw

Re: [cocci] [PATCH -next v3 01/10] coccinelle: Add rules to find str_true_false() replacements

Thanks for testing it. I will see if there is some other way to improve the performance. Sent from my iPhone

Quoted 88 lines · Hongbo Li



Quoted 34 lines · Julia Lawall
Quoted 20 lines · Hongbo Li

After str_true_false() has been introduced in the tree, we can add rules for finding places where str_true_false() can be used. A simple test can find over 10 locations.

---
 scripts/coccinelle/api/string_choices.cocci | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
scripts/coccinelle/api/string_choices.cocci +2 -0
index 5e729f187f22..6942ad7c4224 100644
@@ -85,3 +85,22 @@ 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@

scripts/coccinelle/api/string_choices.cocci e << str_down_up_r.E; · line 99

Quoted 10 lines
scripts/coccinelle/api/string_choices.cocci +10 -0
+expression E;
+@@
+-      ((E) ? "true" : "false")
++      str_true_false(E)
+
+@str_true_false_r depends on !patch exists@
+expression E;
+position P;
+@@
+*      ((E@P) ? "true" : "false")

Hello,
The semantic patch is quite slow. Actually it tests a large number of cases, eg where the parentheses are present and where they are not.

Quoted 8 lines

A small optimization is possible in the non-patch case. The outer parentheses are not needed, because you will already get the same information whether they are there or not.
In contrast, for the patch case, the outer parentheses are needed, because if they are there we want to remove them, since they are not needed for the function call.
Could you update the depends on !patch cases to remove the outer parentheses?

You mean in non-patch case, we could just write like the following?:

+@str_true_false_r depends on !patch exists@

scripts/coccinelle/api/string_choices.cocci +4 -0
+expression E;
+position P;
+@@
+*      (E@P) ? "true" : "false"

I have tested in my machine. The impact of this parenthesis on performance is very minimal.

With parentheses, the time on driver/ costs:

real 1m41.696s
user 85m24.069s
sys 1m8.891s

Without parentheses, the time on driver/ costs:

real 1m40.438s
user 85m53.987s
sys 1m7.981s

Thanks,
Hongbo

Quoted 15 lines

Also, just one patch would be fine. There are many changes, but they are all sort of the same, so it would be easier just to see them all at once. thanks,
julia

Quoted 11 lines
scripts/coccinelle/api/string_choices.cocci +7 -1
+
+@script:python depends on report@
+p << str_true_false_r.P;
+e << str_true_false_r.E;
+@@
+
+coccilib.report.print_report(p[0], "opportunity for str_true_false(%s)" % e)
--

2.34.1

author
#6 lore raw

Re: [PATCH -next v3 01/10] coccinelle: Add rules to find str_true_false() replacements

Quoted 1 line · Julia Lawall

Thanks for testing it. I will see if there is some other way to improve the performance.

May be every rules in the same file are executed sequentially cost a lot?

Thanks,
Hongbo

Quoted 91 lines

Sent from my iPhone

Quoted 88 lines · Hongbo Li



Quoted 34 lines · Julia Lawall
Quoted 20 lines · Hongbo Li

After str_true_false() has been introduced in the tree, we can add rules for finding places where str_true_false() can be used. A simple test can find over 10 locations.

---
  scripts/coccinelle/api/string_choices.cocci | 19 +++++++++++++++++++
  1 file changed, 19 insertions(+)
scripts/coccinelle/api/string_choices.cocci +2 -0
index 5e729f187f22..6942ad7c4224 100644
@@ -85,3 +85,22 @@ 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@

scripts/coccinelle/api/string_choices.cocci e << str_down_up_r.E; · line 99

Quoted 10 lines
scripts/coccinelle/api/string_choices.cocci +10 -0
+expression E;
+@@
+-      ((E) ? "true" : "false")
++      str_true_false(E)
+
+@str_true_false_r depends on !patch exists@
+expression E;
+position P;
+@@
+*      ((E@P) ? "true" : "false")

Hello,
The semantic patch is quite slow. Actually it tests a large number of cases, eg where the parentheses are present and where they are not.

Quoted 8 lines

A small optimization is possible in the non-patch case. The outer parentheses are not needed, because you will already get the same information whether they are there or not.
In contrast, for the patch case, the outer parentheses are needed, because if they are there we want to remove them, since they are not needed for the function call.
Could you update the depends on !patch cases to remove the outer parentheses?

You mean in non-patch case, we could just write like the following?:

+@str_true_false_r depends on !patch exists@

scripts/coccinelle/api/string_choices.cocci +4 -0
+expression E;
+position P;
+@@
+*      (E@P) ? "true" : "false"

I have tested in my machine. The impact of this parenthesis on performance is very minimal.

With parentheses, the time on driver/ costs:

real 1m41.696s
user 85m24.069s
sys 1m8.891s

Without parentheses, the time on driver/ costs:

real 1m40.438s
user 85m53.987s
sys 1m7.981s

Thanks,
Hongbo

Quoted 15 lines

Also, just one patch would be fine. There are many changes, but they are all sort of the same, so it would be easier just to see them all at once. thanks,
julia

Quoted 11 lines
scripts/coccinelle/api/string_choices.cocci +7 -1
+
+@script:python depends on report@
+p << str_true_false_r.P;
+e << str_true_false_r.E;
+@@
+
+coccilib.report.print_report(p[0], "opportunity for str_true_false(%s)" % e)
--

2.34.1

author
#7 lore raw

PATCH v3 2/10 coccinelle: Add rules to find str_false_true() replacements

In reply to Hongbo Li

As done with str_true_false(), add checks for str_false_true() opportunities. A simple test can find over 9 cases currently exist in the tree.

---
 scripts/coccinelle/api/string_choices.cocci | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
scripts/coccinelle/api/string_choices.cocci +19 -0
index 6942ad7c4224..c3c5bc94fab0 100644
@@ -104,3 +104,22 @@ e << str_true_false_r.E;
 @@
 
 coccilib.report.print_report(p[0], "opportunity for str_true_false(%s)" % e)
+
+@str_false_true depends on patch@
+expression E;
+@@
+-      ((E) ? "false" : "true")
++      str_false_true(E)
+
+@str_false_true_r depends on !patch exists@
+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)
Signature · 1 line
2.34.1
author
#8 lore raw

PATCH v3 3/10 coccinelle: Add rules to find str_hi{gh}_lo{w}() replacements

In reply to Hongbo Li

As other rules done, we add rules for str_hi{gh}_lo{w}() to check the relative opportunities.

---
 scripts/coccinelle/api/string_choices.cocci | 42 +++++++++++++++++++++
 1 file changed, 42 insertions(+)
scripts/coccinelle/api/string_choices.cocci +42 -0
index c3c5bc94fab0..7c631fd8abe2 100644
@@ -123,3 +123,45 @@ e << str_false_true_r.E;
 @@
 
 coccilib.report.print_report(p[0], "opportunity for str_false_true(%s)" % e)
+
+@str_hi_lo depends on patch@
+expression E;
+@@
+(
+-      ((E) ? "hi" : "lo")
++      str_hi_lo(E)
+)
+
+@str_hi_lo_r depends on !patch exists@
+expression E;
+position P;
+@@
+(
+*      ((E@P) ? "hi" : "lo")
+)
+
+@script:python depends on report@
+p << str_hi_lo_r.P;
+e << str_hi_lo_r.E;
+@@
+
+coccilib.report.print_report(p[0], "opportunity for str_hi_lo(%s)" % e)
+
+@str_high_low depends on patch@
+expression E;
+@@
+-      ((E) ? "high" : "low")
++      str_high_low(E)
+
+@str_high_low_r depends on !patch exists@
+expression E;
+position P;
+@@
+*      ((E@P) ? "high" : "low")
+
+@script:python depends on report@
+p << str_high_low_r.P;
+e << str_high_low_r.E;
+@@
+
+coccilib.report.print_report(p[0], "opportunity for str_high_low(%s)" % e)
Signature · 1 line
2.34.1
author
#9 lore raw

PATCH v3 4/10 coccinelle: Add rules to find str_lo{w}_hi{gh}() replacements

In reply to Hongbo Li

As other rules done, we add rules for str_lo{w}_hi{gh}() to check the relative opportunities.

---
 scripts/coccinelle/api/string_choices.cocci | 38 +++++++++++++++++++++
 1 file changed, 38 insertions(+)
scripts/coccinelle/api/string_choices.cocci +38 -0
index 7c631fd8abe2..e91d4eb30161 100644
@@ -165,3 +165,41 @@ e << str_high_low_r.E;
 @@
 
 coccilib.report.print_report(p[0], "opportunity for str_high_low(%s)" % e)
+
+@str_lo_hi depends on patch@
+expression E;
+@@
+-      ((E) ? "lo" : "hi")
++      str_lo_hi(E)
+
+@str_lo_hi_r depends on !patch exists@
+expression E;
+position P;
+@@
+*      ((E@P) ? "lo" : "hi")
+
+@script:python depends on report@
+p << str_lo_hi_r.P;
+e << str_lo_hi_r.E;
+@@
+
+coccilib.report.print_report(p[0], "opportunity for str_lo_hi(%s)" % e)
+
+@str_low_high depends on patch@
+expression E;
+@@
+-      ((E) ? "low" : "high")
++      str_low_high(E)
+
+@str_low_high_r depends on !patch exists@
+expression E;
+position P;
+@@
+*      ((E@P) ? "low" : "high")
+
+@script:python depends on report@
+p << str_low_high_r.P;
+e << str_low_high_r.E;
+@@
+
+coccilib.report.print_report(p[0], "opportunity for str_low_high(%s)" % e)
Signature · 1 line
2.34.1
author
#10 lore raw

PATCH v3 5/10 coccinelle: Add rules to find str_enable{d}_disable{d}() replacements

In reply to Hongbo Li

As other rules done, we add rules for str_enable{d}_ disable{d}() to check the relative opportunities.

---
 scripts/coccinelle/api/string_choices.cocci | 38 +++++++++++++++++++++
 1 file changed, 38 insertions(+)
scripts/coccinelle/api/string_choices.cocci +38 -0
index e91d4eb30161..bb6b851ee2aa 100644
@@ -203,3 +203,41 @@ 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 exists@
+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@
+expression E;
+@@
+-      ((E) ? "enabled" : "disabled")
++      str_enabled_disabled(E)
+
+@str_enabled_disabled_r depends on !patch exists@
+expression E;
+position P;
+@@
+*      ((E@P) ? "enabled" : "disabled")
+
+@script:python depends on report@
+p << str_enabled_disabled_r.P;
+e << str_enabled_disabled_r.E;
+@@
+
+coccilib.report.print_report(p[0], "opportunity for str_enabled_disabled(%s)" % e)
Signature · 1 line
2.34.1
author
#11 lore raw

PATCH v3 6/10 coccinelle: Add rules to find str_read_write() replacements

In reply to Hongbo Li

As other rules done, we add rules for str_read_write() to check the relative opportunities.

---
 scripts/coccinelle/api/string_choices.cocci | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
scripts/coccinelle/api/string_choices.cocci +19 -0
index bb6b851ee2aa..29c507d86ffd 100644
@@ -241,3 +241,22 @@ e << str_enabled_disabled_r.E;
 @@
 
 coccilib.report.print_report(p[0], "opportunity for str_enabled_disabled(%s)" % e)
+
+@str_read_write depends on patch@
+expression E;
+@@
+-      ((E) ? "read" : "write")
++      str_read_write(E)
+
+@str_read_write_r depends on !patch exists@
+expression E;
+position P;
+@@
+*      ((E@P) ? "read" : "write")
+
+@script:python depends on report@
+p << str_read_write_r.P;
+e << str_read_write_r.E;
+@@
+
+coccilib.report.print_report(p[0], "opportunity for str_read_write(%s)" % e)
Signature · 1 line
2.34.1
author
#12 lore raw

PATCH v3 7/10 coccinelle: Add rules to find str_write_read() replacements

In reply to Hongbo Li

As other rules done, we add rules for str_write_read() to check the relative opportunities.

---
 scripts/coccinelle/api/string_choices.cocci | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
scripts/coccinelle/api/string_choices.cocci +19 -0
index 29c507d86ffd..64f04da2e9f4 100644
@@ -260,3 +260,22 @@ e << str_read_write_r.E;
 @@
 
 coccilib.report.print_report(p[0], "opportunity for str_read_write(%s)" % e)
+
+@str_write_read depends on patch@
+expression E;
+@@
+-      ((E) ? "write" : "read")
++      str_write_read(E)
+
+@str_write_read_r depends on !patch exists@
+expression E;
+position P;
+@@
+*      ((E@P) ? "write" : "read")
+
+@script:python depends on report@
+p << str_write_read_r.P;
+e << str_write_read_r.E;
+@@
+
+coccilib.report.print_report(p[0], "opportunity for str_write_read(%s)" % e)
Signature · 1 line
2.34.1
author
#13 lore raw

PATCH v3 8/10 coccinelle: Add rules to find str_on_off() replacements

In reply to Hongbo Li

As other rules done, we add rules for str_on_off() to check the relative opportunities.

---
 scripts/coccinelle/api/string_choices.cocci | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
scripts/coccinelle/api/string_choices.cocci +19 -0
index 64f04da2e9f4..34ff8f48965c 100644
@@ -279,3 +279,22 @@ 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@
+expression E;
+@@
+-      ((E) ? "on" : "off")
++      str_on_off(E)
+
+@str_on_off_r depends on !patch exists@
+expression E;
+position P;
+@@
+*      ((E@P) ? "on" : "off")
+
+@script:python depends on report@
+p << str_on_off_r.P;
+e << str_on_off_r.E;
+@@
+
+coccilib.report.print_report(p[0], "opportunity for str_on_off(%s)" % e)
Signature · 1 line
2.34.1
author
#14 lore raw

PATCH v3 9/10 coccinelle: Add rules to find str_yes_no() replacements

In reply to Hongbo Li

As other rules done, we add rules for str_yes_no() to check the relative opportunities.

---
 scripts/coccinelle/api/string_choices.cocci | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
scripts/coccinelle/api/string_choices.cocci +19 -0
index 34ff8f48965c..96dc7090395d 100644
@@ -298,3 +298,22 @@ e << str_on_off_r.E;
 @@
 
 coccilib.report.print_report(p[0], "opportunity for str_on_off(%s)" % e)
+
+@str_yes_no depends on patch@
+expression E;
+@@
+-      ((E) ? "yes" : "no")
++      str_yes_no(E)
+
+@str_yes_no_r depends on !patch exists@
+expression E;
+position P;
+@@
+*      ((E@P) ? "yes" : "no")
+
+@script:python depends on report@
+p << str_yes_no_r.P;
+e << str_yes_no_r.E;
+@@
+
+coccilib.report.print_report(p[0], "opportunity for str_yes_no(%s)" % e)
Signature · 1 line
2.34.1
author
#15 lore raw

PATCH v3 10/10 coccinelle: Remove unnecessary parentheses for only one possible change.

In reply to Hongbo Li

The parentheses are only needed if there is a disjunction, ie a set of possible changes. If there is only one pattern, we can remove these parentheses. Just like the format:

- x
+ y

not:

(
- x
+ y
)

---
 scripts/coccinelle/api/string_choices.cocci | 8 --------
 1 file changed, 8 deletions(-)
scripts/coccinelle/api/string_choices.cocci +0 -8
index 96dc7090395d..95e9a3b31f86 100644
@@ -43,18 +43,14 @@ coccilib.report.print_report(p[0], "opportunity for str_plural(%s)" % e)
 @str_up_down depends on patch@
 expression E;
 @@
-(
 -	((E) ? "up" : "down")
 +	str_up_down(E)
-)
 
 @str_up_down_r depends on !patch exists@
 expression E;
 position P;
 @@
-(
 *	((E@P) ? "up" : "down")
-)
 
 @script:python depends on report@
 p << str_up_down_r.P;
@@ -66,18 +62,14 @@ coccilib.report.print_report(p[0], "opportunity for str_up_down(%s)" % e)
 @str_down_up depends on patch@
 expression E;
 @@
-(
 -      ((E) ? "down" : "up")
 +      str_down_up(E)
-)
 
 @str_down_up_r depends on !patch exists@
 expression E;
 position P;
 @@
-(
 *      ((E@P) ? "down" : "up")
-)
 
 @script:python depends on report@
 p << str_down_up_r.P;
Signature · 1 line
2.34.1
author
#16 lore raw
In reply to Hongbo Li

Hi,

Gently ping for this.

Thanks,
Hongbo

Quoted 62 lines · Hongbo Li

We found that many of the detection rules for helpers in string_choices.h are missing. This series of patches is intended to complete these rules. We have verified in the latest kernel tree that these rules can detect many places where the string choices helper can be used as a replacement.

Here are some of the examples:

```
drivers/gpio/gpio-mvebu.c:917:6-18: opportunity for str_lo_hi(in_pol & msk)
drivers/pinctrl/nomadik/pinctrl-nomadik.c:1109:16-19: opportunity for str_high_low(val)
drivers/pinctrl/nomadik/pinctrl-nomadik.c:1116:13-16: opportunity for str_high_low(val
drivers/gpio/gpio-wcove.c:396:6-16: opportunity for str_hi_lo(ctli & 0x1)
drivers/pci/hotplug/pnv_php.c:367:10-41: opportunity for str_on_off(state == OPAL_PCI_SLOT_POWER_ON)
drivers/mtd/nand/raw/brcmnand/brcmnand.c:1844:40-63: opportunity for str_read_write(edu_cmd == EDU_CMD_READ)
drivers/mtd/nand/raw/brcmnand/brcmnand.c:1437:33-35: opportunity for str_on_off(wp)
drivers/mtd/nand/raw/brcmnand/brcmnand.c:1467:9-11: opportunity for str_on_off(wp)
drivers/greybus/svc.c:76:9-37: opportunity for str_enabled_disabled(gb_svc_watchdog_enabled ( svc ))
drivers/thermal/thermal_sysfs.c:59:29-36: opportunity for str_enabled_disabled(enabled)
drivers/dma-buf/st-dma-fence.c:508:10-35: opportunity for str_yes_no(dma_fence_is_signaled ( f2 ))
drivers/mmc/host/omap_hsmmc.c:1644:7-43: opportunity for str_enabled_disabled(host -> flags & HSMMC_SDIO_IRQ_ENABLED)
drivers/input/mouse/elan_i2c_core.c:202:2-4: opportunity for str_on_off(on)
drivers/macintosh/via-pmu.c:626:10-28: opportunity for str_enabled_disabled(option_server_mode)
drivers/usb/mtu3/mtu3_debugfs.c:482:5-39: opportunity for str_on_off(regulator_is_enabled ( otg_sx -> vbus ))
drivers/net/wireless/intel/iwlwifi/mvm/tt.c:355:4-10: opportunity for str_enable_disable(enable)
drivers/nfc/pn544/i2c.c:215:7-20: opportunity for str_low_high(polarity == 0)
drivers/media/tuners/tda18250.c:110:3-12: opportunity for str_true_false(triggered)
drivers/mtd/tests/torturetest.c:191:30-35: opportunity for str_enabled_disabled(check)
drivers/bus/intel-ixp4xx-eb.c:225:5-8: opportunity for str_enabled_disabled(val)
drivers/net/ethernet/huawei/hinic/hinic_port.c:1133:3-9: opportunity for str_enable_disable(enable)
drivers/net/ethernet/huawei/hinic/hinic_port.c:1245:3-9: opportunity for str_enable_disable(pfc_en)
drivers/media/pci/cx23885/cimax2.c:227:4-8: opportunity for str_read_write(read)
drivers/media/usb/em28xx/em28xx-video.c:941:4-9: opportunity for str_enabled_disabled(flags)
```

Changes since v2: https://patchwork.kernel.org/project/linux-hardening/cover/[email protected]/
- Remove unnecessary outer parentheses for new rules as suggested by julia, thanks.
- Remove unnecessary outer parentheses for old rules.

Changes since v1: https://patchwork.kernel.org/project/linux-hardening/cover/[email protected]/
- Add more rules for helpers in string_choices.h.

Thanks,
Hongbo.

Hongbo Li (10):
coccinelle: Add rules to find str_true_false() replacements
coccinelle: Add rules to find str_false_true() replacements
coccinelle: Add rules to find str_hi{gh}_lo{w}() replacements
coccinelle: Add rules to find str_lo{w}_hi{gh}() replacements
coccinelle: Add rules to find str_enable{d}_disable{d}() replacements
coccinelle: Add rules to find str_read_write() replacements
coccinelle: Add rules to find str_write_read() replacements
coccinelle: Add rules to find str_on_off() replacements
coccinelle: Add rules to find str_yes_no() replacements
coccinelle: Remove unnecessary parentheses for only one possible
change.

  scripts/coccinelle/api/string_choices.cocci | 240 +++++++++++++++++++-
  1 file changed, 232 insertions(+), 8 deletions(-)
maintainer
#17 lore raw
In reply to Hongbo Li
Quoted 6 lines · Hongbo Li

We found that many of the detection rules for helpers in string_choices.h are missing. This series of patches is intended to complete these rules. We have verified in the latest kernel tree that these rules can detect many places where the string choices helper can be used as a replacement.

Thanks. Applied.

julia

Quoted 60 lines

Here are some of the examples:

```
drivers/gpio/gpio-mvebu.c:917:6-18: opportunity for str_lo_hi(in_pol & msk)
drivers/pinctrl/nomadik/pinctrl-nomadik.c:1109:16-19: opportunity for str_high_low(val)
drivers/pinctrl/nomadik/pinctrl-nomadik.c:1116:13-16: opportunity for str_high_low(val
drivers/gpio/gpio-wcove.c:396:6-16: opportunity for str_hi_lo(ctli & 0x1)
drivers/pci/hotplug/pnv_php.c:367:10-41: opportunity for str_on_off(state == OPAL_PCI_SLOT_POWER_ON)
drivers/mtd/nand/raw/brcmnand/brcmnand.c:1844:40-63: opportunity for str_read_write(edu_cmd == EDU_CMD_READ)
drivers/mtd/nand/raw/brcmnand/brcmnand.c:1437:33-35: opportunity for str_on_off(wp)
drivers/mtd/nand/raw/brcmnand/brcmnand.c:1467:9-11: opportunity for str_on_off(wp)
drivers/greybus/svc.c:76:9-37: opportunity for str_enabled_disabled(gb_svc_watchdog_enabled ( svc ))
drivers/thermal/thermal_sysfs.c:59:29-36: opportunity for str_enabled_disabled(enabled)
drivers/dma-buf/st-dma-fence.c:508:10-35: opportunity for str_yes_no(dma_fence_is_signaled ( f2 ))
drivers/mmc/host/omap_hsmmc.c:1644:7-43: opportunity for str_enabled_disabled(host -> flags & HSMMC_SDIO_IRQ_ENABLED)
drivers/input/mouse/elan_i2c_core.c:202:2-4: opportunity for str_on_off(on)
drivers/macintosh/via-pmu.c:626:10-28: opportunity for str_enabled_disabled(option_server_mode)
drivers/usb/mtu3/mtu3_debugfs.c:482:5-39: opportunity for str_on_off(regulator_is_enabled ( otg_sx -> vbus ))
drivers/net/wireless/intel/iwlwifi/mvm/tt.c:355:4-10: opportunity for str_enable_disable(enable)
drivers/nfc/pn544/i2c.c:215:7-20: opportunity for str_low_high(polarity == 0)
drivers/media/tuners/tda18250.c:110:3-12: opportunity for str_true_false(triggered)
drivers/mtd/tests/torturetest.c:191:30-35: opportunity for str_enabled_disabled(check)
drivers/bus/intel-ixp4xx-eb.c:225:5-8: opportunity for str_enabled_disabled(val)
drivers/net/ethernet/huawei/hinic/hinic_port.c:1133:3-9: opportunity for str_enable_disable(enable)
drivers/net/ethernet/huawei/hinic/hinic_port.c:1245:3-9: opportunity for str_enable_disable(pfc_en)
drivers/media/pci/cx23885/cimax2.c:227:4-8: opportunity for str_read_write(read)
drivers/media/usb/em28xx/em28xx-video.c:941:4-9: opportunity for str_enabled_disabled(flags)
```

Changes since v2: https://patchwork.kernel.org/project/linux-hardening/cover/[email protected]/
- Remove unnecessary outer parentheses for new rules as suggested by julia, thanks.
- Remove unnecessary outer parentheses for old rules.

Changes since v1: https://patchwork.kernel.org/project/linux-hardening/cover/[email protected]/
- Add more rules for helpers in string_choices.h.

Thanks,
Hongbo.

Hongbo Li (10):
coccinelle: Add rules to find str_true_false() replacements
coccinelle: Add rules to find str_false_true() replacements
coccinelle: Add rules to find str_hi{gh}_lo{w}() replacements
coccinelle: Add rules to find str_lo{w}_hi{gh}() replacements
coccinelle: Add rules to find str_enable{d}_disable{d}() replacements
coccinelle: Add rules to find str_read_write() replacements
coccinelle: Add rules to find str_write_read() replacements
coccinelle: Add rules to find str_on_off() replacements
coccinelle: Add rules to find str_yes_no() replacements
coccinelle: Remove unnecessary parentheses for only one possible
change.

 scripts/coccinelle/api/string_choices.cocci | 240 +++++++++++++++++++-
 1 file changed, 232 insertions(+), 8 deletions(-)

--
2.34.1