checkpatch: don't flag ACQUIRE_ERR() assignments in if conditions
PATCH v2 1/1 checkpatch: don't flag ACQUIRE_ERR() assignments in if conditions
ACQUIRE_ERR() and its wrappers, PM_RUNTIME_ACQUIRE_ERR() and IIO_DEV_ACQUIRE_FAILED(), report whether a conditional cleanup.h guard was acquired, and drivers consume the result directly in an if condition:
if ((rc = ACQUIRE_ERR(mutex_intr, &lock))) return rc;
That combined form is the established style at the 49 in-tree call sites under drivers/cxl and drivers/pci/tsm.c, so ASSIGN_IN_IF fires there only as a false positive, and every patch touching those lines carries noise that reviewers have to wave off manually.
Skip the check only when every assignment in the condition assigns the result of such a call, matched by the *_ACQUIRE_ERR() / *_ACQUIRE_FAILED() naming convention of its wrappers. Plain assignments, mixed conditions and near-miss identifiers still get flagged.
---
Changes since v1 [1]:
- Check each assignment in the condition instead of the condition as a whole, so a mixed condition such as
"if ((rc = regular_function()) || (ret = ACQUIRE_ERR(...)))" keeps getting flagged (Alison Schofield).
- Match the assigned-to expression with $Lval, as Joe Perches suggested on Alison's earlier attempt at this exception [2].
- Cc linux-cxl, where most users of this form live.
Two deviations from [2]: the assigned expression capture excludes '=', so an '==' comparison in a mixed condition is not mistaken for a bad assignment, and the check requires an assignment the $Lval match can see, so compound assignments like '+=', which it cannot see, are not silently allowed.
Testing: ran checkpatch on a test file with the allowed forms (bare macro and both wrappers, member and array element lhs, multiple ACQUIRE family assignments in one condition, an '==' comparison alongside, no spaces around '=') and with 14 cases that must still trigger (plain assignments, near-miss identifiers, arithmetic on the call result, ACQUIRE_ERR in a comment, indirect assignment, mixed conditions in both orders). All allowed forms pass, all 14 still trigger. The 49 in-tree call sites under drivers/cxl and drivers/pci/tsm.c no longer report ASSIGN_IN_IF, and checkpatch output is unchanged for fs/namei.c, kernel/sched/core.c, mm/vmscan.c and net/core/dev.c.
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/linux-cxl/[email protected]/
--- scripts/checkpatch.pl | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
scripts/checkpatch.pl +14 -0
@@ -5777,7 +5777,21 @@ sub process { my ($s, $c) = ($stat, $cond); my $fixed_assign_in_if = 0; + # ACQUIRE_ERR() and its wrappers, e.g. PM_RUNTIME_ACQUIRE_ERR() + # and IIO_DEV_ACQUIRE_FAILED(), are meant to be evaluated in an + # if condition, with the error assigned in the condition: + # if ((rc = ACQUIRE_ERR(name, &lock))) + # Allow that only when every assignment in the condition assigns + # the result of such a call, so that a mixed condition keeps + # getting flagged: + # if ((rc = regular_function()) || (ret = ACQUIRE_ERR(name, &lock))) + my $assign_in_if = 0; if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { + my $has_assignment = $c =~ /\b$Lval\s*=\s*[^,)&|=]+/; + my $has_other_assignment = $c =~ /\b$Lval\s*=\s*(?!\s*\w*ACQUIRE_(?:ERR|FAILED)\s*\()[^,)&|=]+/; + $assign_in_if = !$has_assignment || $has_other_assignment; + } + if ($assign_in_if) { if (ERROR("ASSIGN_IN_IF", "do not use assignment in if condition\n" . $herecurr) && $fix && $perl_version_ok) {
Signature · 1 line
2.43.7
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] checkpatch: trailing arithmetic after ACQUIRE_ERR bypasses ASSIGN_IN_IF - [Medium] checkpatch…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] checkpatch: trailing arithmetic after ACQUIRE_ERR bypasses ASSIGN_IN_IF
- [Medium] checkpatch: mixed compound assignment and ACQUIRE_ERR bypasses check --
commit 82d84894b0a64b92c72367ea66d4270c5cb9cc86
Author: Guixin Liu <[email protected]>
checkpatch: don't flag ACQUIRE_ERR() assignments in if conditions
This commit updates checkpatch to skip the ASSIGN_IN_IF warning when the assignments inside the condition are strictly from the ACQUIRE_ERR family of macros. It attempts to ensure that plain assignments, compound assignments, and mixed conditions still trigger the warning.
Quoted 4 lines
scripts/checkpatch.pl +0
-0
[ ... ]
Quoted 6 lines
scripts/checkpatch.pl +2
-0@@ -5787,7 +5787,21 @@ sub process { my ($s, $c) = ($stat, $cond); my $fixed_assign_in_if = 0; + # ACQUIRE_ERR() and its wrappers, e.g. PM_RUNTIME_ACQUIRE_ERR() + # and IIO_DEV_ACQUIRE_FAILED(), are meant to be evaluated in an
scripts/checkpatch.pl sub process { · line 5801
Quoted 10 lines
scripts/checkpatch.pl +9
-0+ # if condition, with the error assigned in the condition: + # if ((rc = ACQUIRE_ERR(name, &lock))) + # Allow that only when every assignment in the condition assigns + # the result of such a call, so that a mixed condition keeps + # getting flagged: + # if ((rc = regular_function()) || (ret = ACQUIRE_ERR(name, &lock))) + my $assign_in_if = 0; if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { + my $has_assignment = $c =~ /\b$Lval\s*=\s*[^,)&|=]+/; + my $has_other_assignment = $c =~ /\b$Lval\s*=\s*(?!\s*\w*ACQUIRE_(?:ERR|FAILED)\s*\()[^,)&|=]+/;
[Severity: Medium]
Does this regex allow trailing arithmetic to bypass the check?
Because the regex only checks the prefix of the right-hand side of the assignment, a condition like if ((rc = ACQUIRE_ERR(...) + 1)) seems to start with the allowed macro. This causes the negative lookahead to fail, preventing it from being flagged as an other assignment.
This appears to contradict the commit message's claim about arithmetic on the call result still triggering the check.
scripts/checkpatch.pl sub process { · line 5792
Quoted 1 line
scripts/checkpatch.pl +1
-0+ $assign_in_if = !$has_assignment || $has_other_assignment;
[Severity: Medium]
Does this logic silently allow compound assignments when they are mixed with a valid ACQUIRE_ERR() assignment?
An expression like if ((rc = ACQUIRE_ERR(...)) || (a += 1)) satisfies the $has_assignment check, which disables the fail-safe. However, the compound assignment does not trigger the $has_other_assignment check because it does not match the strict equals sign in the regex.
This seems to contradict the commit message's statement that compound assignments are not silently allowed.
Quoted 5 lines
scripts/checkpatch.pl +2
-0+ } + if ($assign_in_if) { if (ERROR("ASSIGN_IN_IF", "do not use assignment in if condition\n" . $herecurr) && $fix && $perl_version_ok) {
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 19 lines · Guixin Liu
ACQUIRE_ERR()and its wrappers,PM_RUNTIME_ACQUIRE_ERR()andIIO_DEV_ACQUIRE_FAILED(), report whether a conditionalcleanup.hguard was acquired, and drivers consume the result directly in an if condition:if ((rc = ACQUIRE_ERR(mutex_intr, &lock))) return rc;That combined form is the established style at the 49 in-tree call sites under
drivers/cxlanddrivers/pci/tsm.c, soASSIGN_IN_IFfires there only as a false positive, and every patch touching those lines carries noise that reviewers have to wave off manually.Skip the check only when every assignment in the condition assigns the result of such a call, matched by the
*_ACQUIRE_ERR()/*_ACQUIRE_FAILED()naming convention of its wrappers. Plain assignments, mixed conditions and near-miss identifiers still get flagged.
Quoted 4 lines
Cc [[email protected]](mailto:[email protected])Assisted-by LLM
Quoted 63 lines
---
Changes since v1 [1]:- Check each assignment in the condition instead of the condition as a whole, so a mixed condition such as
"if ((rc =regular_function()) || (ret =ACQUIRE_ERR(...)))" keeps getting flagged (Alison Schofield).
- Match the assigned-to expression with $Lval, as Joe Perches suggested on Alison's earlier attempt at this exception [2].
- Cc linux-cxl, where most users of this form live.Two deviations from [2]: the assigned expression capture excludes '=', so an '==' comparison in a mixed condition is not mistaken for a bad assignment, and the check requires an assignment the $Lval match can see, so compound assignments like '+=', which it cannot see, are not silently allowed.
Testing: ran checkpatch on a test file with the allowed forms (bare macro and both wrappers, member and array element lhs, multiple ACQUIRE family assignments in one condition, an '==' comparison alongside, no spaces around '=') and with 14 cases that must still trigger (plain assignments, near-miss identifiers, arithmetic on the call result,
ACQUIRE_ERRin a comment, indirect assignment, mixed conditions in both orders). All allowed forms pass, all 14 still trigger. The 49 in-tree call sites underdrivers/cxlanddrivers/pci/tsm.cno longer reportASSIGN_IN_IF, and checkpatch output is unchanged forfs/namei.c,kernel/sched/core.c,mm/vmscan.candnet/core/dev.c.[1] [https://lore.kernel.org/all/[email protected]/](https://lore.kernel.org/all/[email protected]/)
[2] [https://lore.kernel.org/linux-cxl/[email protected]/](https://lore.kernel.org/linux-cxl/[email protected]/)--- scripts/checkpatch.pl | 14 ++++++++++++++ 1 file changed, 14 insertions(+)scripts/checkpatch.pl +14
-0@@ -5777,7 +5777,21 @@ sub process { my ($s, $c) = ($stat, $cond); my $fixed_assign_in_if = 0; + # ACQUIRE_ERR() and its wrappers, e.g. PM_RUNTIME_ACQUIRE_ERR() + # and IIO_DEV_ACQUIRE_FAILED(), are meant to be evaluated in an + # if condition, with the error assigned in the condition: + # if ((rc = ACQUIRE_ERR(name, &lock))) + # Allow that only when every assignment in the condition assigns + # the result of such a call, so that a mixed condition keeps + # getting flagged: + # if ((rc = regular_function()) || (ret = ACQUIRE_ERR(name, &lock))) + my $assign_in_if = 0; if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { + my $has_assignment = $c =~ /\b$Lval\s*=\s*[^,)&|=]+/; + my $has_other_assignment = $c =~ /\b$Lval\s*=\s*(?!\s*\w*ACQUIRE_(?:ERR|FAILED)\s*\()[^,)&|=]+/; + $assign_in_if = !$has_assignment || $has_other_assignment; + } + if ($assign_in_if) { if (ERROR("ASSIGN_IN_IF", "do not use assignment in if condition\n" . $herecurr) && $fix && $perl_version_ok) {
Hi Joe,
Could you please apply this patch to 7.4?
Best Regards,
Guixin Liu
Quoted 88 lines · Joe Perches
Quoted 19 lines · Guixin Liu
ACQUIRE_ERR()and its wrappers,PM_RUNTIME_ACQUIRE_ERR()andIIO_DEV_ACQUIRE_FAILED(), report whether a conditionalcleanup.hguard was acquired, and drivers consume the result directly in an if condition:if ((rc = ACQUIRE_ERR(mutex_intr, &lock))) return rc;That combined form is the established style at the 49 in-tree call sites under
drivers/cxlanddrivers/pci/tsm.c, soASSIGN_IN_IFfires there only as a false positive, and every patch touching those lines carries noise that reviewers have to wave off manually.Skip the check only when every assignment in the condition assigns the result of such a call, matched by the
*_ACQUIRE_ERR()/*_ACQUIRE_FAILED()naming convention of its wrappers. Plain assignments, mixed conditions and near-miss identifiers still get flagged.Quoted 4 lines
Cc [[email protected]](mailto:[email protected])Assisted-by LLMAcked-by Joe Perches <[email protected]>Quoted 62 lines
---
Changes since v1 [1]:- Check each assignment in the condition instead of the condition as a whole, so a mixed condition such as
"if ((rc =regular_function()) || (ret =ACQUIRE_ERR(...)))" keeps getting flagged (Alison Schofield).
- Match the assigned-to expression with $Lval, as Joe Perches suggested on Alison's earlier attempt at this exception [2].
- Cc linux-cxl, where most users of this form live.Two deviations from [2]: the assigned expression capture excludes '=', so an '==' comparison in a mixed condition is not mistaken for a bad assignment, and the check requires an assignment the $Lval match can see, so compound assignments like '+=', which it cannot see, are not silently allowed.
Testing: ran checkpatch on a test file with the allowed forms (bare macro and both wrappers, member and array element lhs, multiple ACQUIRE family assignments in one condition, an '==' comparison alongside, no spaces around '=') and with 14 cases that must still trigger (plain assignments, near-miss identifiers, arithmetic on the call result,
ACQUIRE_ERRin a comment, indirect assignment, mixed conditions in both orders). All allowed forms pass, all 14 still trigger. The 49 in-tree call sites underdrivers/cxlanddrivers/pci/tsm.cno longer reportASSIGN_IN_IF, and checkpatch output is unchanged forfs/namei.c,kernel/sched/core.c,mm/vmscan.candnet/core/dev.c.[1] [https://lore.kernel.org/all/[email protected]/](https://lore.kernel.org/all/[email protected]/)
[2] [https://lore.kernel.org/linux-cxl/[email protected]/](https://lore.kernel.org/linux-cxl/[email protected]/)--- scripts/checkpatch.pl | 14 ++++++++++++++ 1 file changed, 14 insertions(+)scripts/checkpatch.pl +14
-0@@ -5777,7 +5777,21 @@ sub process { my ($s, $c) = ($stat, $cond); my $fixed_assign_in_if = 0; + # ACQUIRE_ERR() and its wrappers, e.g. PM_RUNTIME_ACQUIRE_ERR() + # and IIO_DEV_ACQUIRE_FAILED(), are meant to be evaluated in an + # if condition, with the error assigned in the condition: + # if ((rc = ACQUIRE_ERR(name, &lock))) + # Allow that only when every assignment in the condition assigns + # the result of such a call, so that a mixed condition keeps + # getting flagged: + # if ((rc = regular_function()) || (ret = ACQUIRE_ERR(name, &lock))) + my $assign_in_if = 0; if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { + my $has_assignment = $c =~ /\b$Lval\s*=\s*[^,)&|=]+/; + my $has_other_assignment = $c =~ /\b$Lval\s*=\s*(?!\s*\w*ACQUIRE_(?:ERR|FAILED)\s*\()[^,)&|=]+/; + $assign_in_if = !$has_assignment || $has_other_assignment; + } + if ($assign_in_if) { if (ERROR("ASSIGN_IN_IF", "do not use assignment in if condition\n" . $herecurr) && $fix && $perl_version_ok) {
Quoted 5 lines · Guixin Liu
Hi Joe,
Could you please apply this patch to 7.4?Best Regards,
Guixin Liu
Already applied by Andrew :)
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=d25f15a76c7a7f0e20a2b4b36f5108ef115a6d1f
Quoted 91 lines · Joe Perches
Quoted 88 lines · Guixin Liu
Quoted 19 lines
ACQUIRE_ERR()and its wrappers,PM_RUNTIME_ACQUIRE_ERR()andIIO_DEV_ACQUIRE_FAILED(), report whether a conditionalcleanup.hguard was acquired, and drivers consume the result directly in an if condition:if ((rc = ACQUIRE_ERR(mutex_intr, &lock))) return rc;That combined form is the established style at the 49 in-tree call sites under
drivers/cxlanddrivers/pci/tsm.c, soASSIGN_IN_IFfires there only as a false positive, and every patch touching those lines carries noise that reviewers have to wave off manually.Skip the check only when every assignment in the condition assigns the result of such a call, matched by the
*_ACQUIRE_ERR()/*_ACQUIRE_FAILED()naming convention of its wrappers. Plain assignments, mixed conditions and near-miss identifiers still get flagged.Quoted 4 lines
Cc [[email protected]](mailto:[email protected])Assisted-by LLMAcked-by Joe Perches <[email protected]>Quoted 62 lines
---
Changes since v1 [1]:- Check each assignment in the condition instead of the condition as a whole, so a mixed condition such as
"if ((rc =regular_function()) || (ret =ACQUIRE_ERR(...)))" keeps getting flagged (Alison Schofield).
- Match the assigned-to expression with $Lval, as Joe Perches suggested on Alison's earlier attempt at this exception [2].
- Cc linux-cxl, where most users of this form live.Two deviations from [2]: the assigned expression capture excludes '=', so an '==' comparison in a mixed condition is not mistaken for a bad assignment, and the check requires an assignment the $Lval match can see, so compound assignments like '+=', which it cannot see, are not silently allowed.
Testing: ran checkpatch on a test file with the allowed forms (bare macro and both wrappers, member and array element lhs, multiple ACQUIRE family assignments in one condition, an '==' comparison alongside, no spaces around '=') and with 14 cases that must still trigger (plain assignments, near-miss identifiers, arithmetic on the call result,
ACQUIRE_ERRin a comment, indirect assignment, mixed conditions in both orders). All allowed forms pass, all 14 still trigger. The 49 in-tree call sites underdrivers/cxlanddrivers/pci/tsm.cno longer reportASSIGN_IN_IF, and checkpatch output is unchanged forfs/namei.c,kernel/sched/core.c,mm/vmscan.candnet/core/dev.c.[1] [https://lore.kernel.org/all/[email protected]/](https://lore.kernel.org/all/[email protected]/)
[2] [https://lore.kernel.org/linux-cxl/[email protected]/](https://lore.kernel.org/linux-cxl/[email protected]/)--- scripts/checkpatch.pl | 14 ++++++++++++++ 1 file changed, 14 insertions(+)scripts/checkpatch.pl +14
-0@@ -5777,7 +5777,21 @@ sub process { my ($s, $c) = ($stat, $cond); my $fixed_assign_in_if = 0; + # ACQUIRE_ERR() and its wrappers, e.g. PM_RUNTIME_ACQUIRE_ERR() + # and IIO_DEV_ACQUIRE_FAILED(), are meant to be evaluated in an + # if condition, with the error assigned in the condition: + # if ((rc = ACQUIRE_ERR(name, &lock))) + # Allow that only when every assignment in the condition assigns + # the result of such a call, so that a mixed condition keeps + # getting flagged: + # if ((rc = regular_function()) || (ret = ACQUIRE_ERR(name, &lock))) + my $assign_in_if = 0; if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { + my $has_assignment = $c =~ /\b$Lval\s*=\s*[^,)&|=]+/; + my $has_other_assignment = $c =~ /\b$Lval\s*=\s*(?!\s*\w*ACQUIRE_(?:ERR|FAILED)\s*\()[^,)&|=]+/; + $assign_in_if = !$has_assignment || $has_other_assignment; + } + if ($assign_in_if) { if (ERROR("ASSIGN_IN_IF", "do not use assignment in if condition\n" . $herecurr) && $fix && $perl_version_ok) {
Quoted 19 lines · Guixin Liu
ACQUIRE_ERR()and its wrappers,PM_RUNTIME_ACQUIRE_ERR()andIIO_DEV_ACQUIRE_FAILED(), report whether a conditionalcleanup.hguard was acquired, and drivers consume the result directly in an if condition:if ((rc = ACQUIRE_ERR(mutex_intr, &lock))) return rc;That combined form is the established style at the 49 in-tree call sites under
drivers/cxlanddrivers/pci/tsm.c, soASSIGN_IN_IFfires there only as a false positive, and every patch touching those lines carries noise that reviewers have to wave off manually.Skip the check only when every assignment in the condition assigns the result of such a call, matched by the
*_ACQUIRE_ERR()/*_ACQUIRE_FAILED()naming convention of its wrappers. Plain assignments, mixed conditions and near-miss identifiers still get flagged.
Quoted 4 lines
Suggested-by Alison Schofield <[email protected]>Assisted-by LLMSigned-off-by Guixin Liu <[email protected]>
Quoted 19 lines · On Thu, 24 Sep 2026 11:39:23 +0800 Guixin Liu <[email protected]> wrote:
ACQUIRE_ERR()and its wrappers,PM_RUNTIME_ACQUIRE_ERR()andIIO_DEV_ACQUIRE_FAILED(), report whether a conditionalcleanup.hguard was acquired, and drivers consume the result directly in an if condition:if ((rc = ACQUIRE_ERR(mutex_intr, &lock))) return rc;That combined form is the established style at the 49 in-tree call sites under
drivers/cxlanddrivers/pci/tsm.c, soASSIGN_IN_IFfires there only as a false positive, and every patch touching those lines carries noise that reviewers have to wave off manually.Skip the check only when every assignment in the condition assigns the result of such a call, matched by the
*_ACQUIRE_ERR()/*_ACQUIRE_FAILED()naming convention of its wrappers. Plain assignments, mixed conditions and near-miss identifiers still get flagged.
Quoted 5 lines
Suggested-by Alison Schofield <[email protected]>Assisted-by LLMSigned-off-by Guixin Liu <[email protected]>
I'm thoroughly in favour but seeing as I'd end up using an LLM just to figure out what the actual code does no tags from me!
Jonathan