Skip to contentSkip to conversation

softirq: Limit the BH count warning to RT BH lock users

PATCH Superseded Yiyang Chen 3 participants 3 messages Nov 22 – Nov 23 Real-time Linux (PREEMPT_RT)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 softirq: Limit the BH count warning to RT BH lock users

__local_bh_disable_ip() warns if a non-preemptible task first disables BHs while the per-CPU BH count is nonzero. Without the RT BH lock, this check can turn off lock debugging on a valid call sequence.

With CONFIG_PREEMPT_RT_NEEDS_BH_LOCK=n, the following sequence is valid for two tasks on the same CPU.
Both tasks call the BH helpers with IRQs enabled. The per-CPU BH count starts at zero.

Task A Task B
------ ------
local_bh_disable()

    softirq_ctrl.cnt += SOFTIRQ_DISABLE_OFFSET
    current->softirq_disable_cnt += SOFTIRQ_DISABLE_OFFSET

<preempted by B>
preempt_disable()
local_bh_disable()
B's softirq_disable_cnt is zero preemptible() is false
WARN on nonzero softirq_ctrl.cnt softirq_ctrl.cnt += SOFTIRQ_DISABLE_OFFSET current->softirq_disable_cnt += SOFTIRQ_DISABLE_OFFSET
local_bh_enable()
preempt_enable()
<resumes>
local_bh_enable()

The warning relies on the per-CPU BH lock serializing BH-disabled sections on the same CPU. With CONFIG_PREEMPT_RT_NEEDS_BH_LOCK=y, A still holds that lock when B runs. Since B is non-preemptible, it skips the lock acquisition. The nonzero count then exposes B entering the section while A holds the lock.

With CONFIG_PREEMPT_RT_NEEDS_BH_LOCK=n, there is no BH lock for B to bypass. The same nonzero count simply records A's active BH-disabled section, which may legitimately overlap B's.

Guard the warning with CONFIG_PREEMPT_RT_NEEDS_BH_LOCK for as long as the lock is optional. If the lock is removed, as discussed earlier, the check can go away with it.

---
 kernel/softirq.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/softirq.c +1 -1
index 7fc2200392cd..f64f6b594951 100644
@@ -195,7 +195,7 @@ void __local_bh_disable_ip(unsigned long ip, unsigned int cnt)
 
 			/* Required to meet the RCU bottomhalf requirements. */
 			rcu_read_lock();
-		} else {
+		} else if (IS_ENABLED(CONFIG_PREEMPT_RT_NEEDS_BH_LOCK)) {
 			DEBUG_LOCKS_WARN_ON(this_cpu_read(softirq_ctrl.cnt));
 		}
 	}
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] softirq: missing Fixes trailer for false-positive BH count warning fix --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] softirq: missing Fixes trailer for false-positive BH count warning fix
--

commit aff1deb32f25d2d4caa853499ddafd50802def5a
Author: Yiyang Chen <[email protected]>

Quoted 39 lines

softirq: Limit the BH count warning to RT BH lock users

__local_bh_disable_ip() warns if a non-preemptible task first disables BHs while the per-CPU BH count is nonzero. Without the RT BH lock, this check can turn off lock debugging on a valid call sequence.

With CONFIG_PREEMPT_RT_NEEDS_BH_LOCK=n, the following sequence is valid for two tasks on the same CPU.
Both tasks call the BH helpers with IRQs enabled. The per-CPU BH count starts at zero.

Task A Task B
------ ------
local_bh_disable()

    softirq_ctrl.cnt += SOFTIRQ_DISABLE_OFFSET
    current->softirq_disable_cnt += SOFTIRQ_DISABLE_OFFSET

<preempted by B>
preempt_disable()
local_bh_disable()
B's softirq_disable_cnt is zero preemptible() is false
WARN on nonzero softirq_ctrl.cnt softirq_ctrl.cnt += SOFTIRQ_DISABLE_OFFSET current->softirq_disable_cnt += SOFTIRQ_DISABLE_OFFSET
local_bh_enable()
preempt_enable()
<resumes>
local_bh_enable()

The warning relies on the per-CPU BH lock serializing BH-disabled sections on the same CPU. With CONFIG_PREEMPT_RT_NEEDS_BH_LOCK=y, A still holds that lock when B runs. Since B is non-preemptible, it skips the lock acquisition. The nonzero count then exposes B entering the section while A holds the lock.

With CONFIG_PREEMPT_RT_NEEDS_BH_LOCK=n, there is no BH lock for B to bypass. The same nonzero count simply records A's active BH-disabled section, which may legitimately overlap B's.

Quoted 6 lines

Guard the warning with CONFIG_PREEMPT_RT_NEEDS_BH_LOCK for as long as the lock is optional. If the lock is removed, as discussed earlier, the check can go away with it.

[Severity: Low]
This fixes the warning left unguarded when CONFIG_PREEMPT_RT_NEEDS_BH_LOCK was introduced. Should this include:

Fixes 3253cb49cbad ("softirq: Allow to drop the softirq-BKL lock on PREEMPT_RT")
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/d9554bee441d2c763ef8d61e966a7a471915bbee.1795372382.git.cyyzero16@gmail.com?part=1
#2 lore raw
In reply to Yiyang Chen
Quoted 8 lines · Yiyang Chen

__local_bh_disable_ip() warns if a non-preemptible task first disables BHs while the per-CPU BH count is nonzero. Without the RT BH lock, this check can turn off lock debugging on a valid call sequence.

With CONFIG_PREEMPT_RT_NEEDS_BH_LOCK=n, the following sequence is valid for two tasks on the same CPU.
Both tasks call the BH helpers with IRQs enabled. The per-CPU BH count starts at zero.

This is slightly confusing the diagram below. The key point is that the first task is preemptible, disables BH and second task is not preemtible and does does the same.

Quoted 24 lines

Task A Task B
------ ------
local_bh_disable()

    softirq_ctrl.cnt += SOFTIRQ_DISABLE_OFFSET
    current->softirq_disable_cnt += SOFTIRQ_DISABLE_OFFSET

<preempted by B>
preempt_disable()
local_bh_disable()
B's softirq_disable_cnt is zero preemptible() is false
WARN on nonzero softirq_ctrl.cnt softirq_ctrl.cnt += SOFTIRQ_DISABLE_OFFSET current->softirq_disable_cnt += SOFTIRQ_DISABLE_OFFSET
local_bh_enable()
preempt_enable()
<resumes>
local_bh_enable()

The warning relies on the per-CPU BH lock serializing BH-disabled sections on the same CPU. With CONFIG_PREEMPT_RT_NEEDS_BH_LOCK=y, A still holds that lock when B runs. Since B is non-preemptible, it skips the lock acquisition. The nonzero count then exposes B entering the section while A holds the lock.

Quoted 3 lines

With CONFIG_PREEMPT_RT_NEEDS_BH_LOCK=n, there is no BH lock for B to bypass. The same nonzero count simply records A's active BH-disabled section, which may legitimately overlap B's.

Correct. It used ensure that the lock is not taken therefore no "protected resource" is in use. With CONFIG_PREEMPT_RT_NEEDS_BH_LOCK=n this is no longer the case.

Quoted 6 lines

Guard the warning with CONFIG_PREEMPT_RT_NEEDS_BH_LOCK for as long as the lock is optional. If the lock is removed, as discussed earlier, the check can go away with it.

I agree in general. A fixes line would probably be a good idea.

Quoted 9 lines
---
 kernel/softirq.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/softirq.c +0 -0
index 7fc2200392cd..f64f6b594951 100644

kernel/softirq.c void __local_bh_disable_ip(unsigned long ip, unsigned int cnt) · line 201

Quoted 8 lines
kernel/softirq.c +1 -1
@@ -195,7 +195,7 @@ void __local_bh_disable_ip(unsigned long ip, unsigned int cnt)
 
 			/* Required to meet the RCU bottomhalf requirements. */
 			rcu_read_lock();
-		} else {
+		} else if (IS_ENABLED(CONFIG_PREEMPT_RT_NEEDS_BH_LOCK)) {
 			DEBUG_LOCKS_WARN_ON(this_cpu_read(softirq_ctrl.cnt));
 		}
 	}

Sebastian