Skip to contentSkip to conversation

rcu: update: Check rcu_bh_lock_map state in rcu_read_lock_bh_held

PATCH In review Neeraj Upadhyay 2 participants 6 messages Jun 22, 2021 – Jun 23, 2021 RCU

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rcu: update: Check rcu_bh_lock_map state in rcu_read_lock_bh_held

In addition to irq and softirq state, check rcu_bh_lock_map state, to decide whether RCU bh lock is held.

---
 kernel/rcu/update.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/rcu/update.c +1 -1
index c21b38c..d416f1c 100644
@@ -333,7 +333,7 @@ int rcu_read_lock_bh_held(void)
 
 	if (rcu_read_lock_held_common(&ret))
 		return ret;
-	return in_softirq() || irqs_disabled();
+	return lock_is_held(&rcu_bh_lock_map) || in_softirq() || irqs_disabled();
 }
 EXPORT_SYMBOL_GPL(rcu_read_lock_bh_held);
 
Signature · 2 lines
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, 
hosted by The Linux Foundation
maintainer
#1 lore raw
Quoted 4 lines · Neeraj Upadhyay

In addition to irq and softirq state, check rcu_bh_lock_map state, to decide whether RCU bh lock is held.

My initial reaction was that "in_softirq() || irqs_disabled()" covers it because rcu_read_lock_bh() disables BH. But you are right that it does seem a bit silly to ignore lockdep.

So would it also make sense to have a WARN_ON_ONCE() if lockdep claims we are under rcu_read_lock_bh() protection, but "in_softirq() || irqs_disabled()" think otherwise?

Thanx, Paul

Quoted 21 lines
---
 kernel/rcu/update.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/rcu/update.c +1 -1
index c21b38c..d416f1c 100644
@@ -333,7 +333,7 @@ int rcu_read_lock_bh_held(void)
 
 	if (rcu_read_lock_held_common(&ret))
 		return ret;
-	return in_softirq() || irqs_disabled();
+	return lock_is_held(&rcu_bh_lock_map) || in_softirq() || irqs_disabled();
 }
 EXPORT_SYMBOL_GPL(rcu_read_lock_bh_held);
 
Signature · 2 lines
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, 
hosted by The Linux Foundation
#2 lore raw
Quoted 10 lines · Paul E. McKenney
Quoted 4 lines · Neeraj Upadhyay

In addition to irq and softirq state, check rcu_bh_lock_map state, to decide whether RCU bh lock is held.

My initial reaction was that "in_softirq() || irqs_disabled()" covers it because rcu_read_lock_bh() disables BH. But you are right that it does seem a bit silly to ignore lockdep.

Quoted 6 lines

So would it also make sense to have a WARN_ON_ONCE() if lockdep claims we are under rcu_read_lock_bh() protection, but "in_softirq() || irqs_disabled()" think otherwise?

Thanx, Paul

After thinking more on this, looks like one intention of not
having lockdep check here was to catch scenarios where some code enables bh after doing rcu_read_lock_bh(), as is mentioned in the comment above rcu_read_lock_bh_held():

Note that if someone uses
rcu_read_lock_bh(), but then later enables BH, lockdep (if enabled) will show the situation. This is useful for debug checks in functions that require that they be called within an RCU read-side critical section.

Client users seem to be doing lockdep checks on returned value:
drivers/net/wireguard/peer.c
RCU_LOCKDEP_WARN(!rcu_read_lock_bh_held(),

Similarly, any rcu_dereference_check(..., rcu_read_lock_bh_held()) usage also triggers warning, if bh is enabled, inside rcu_read_lock_bh() section.

So, using 'in_softirq() || irqs_disabled()' condition looks to be sufficient condition, to mark all read lock bh regions and adding '|| lock_is_held(&rcu_bh_lock_map)' to this condition does not seem to fit well with the RCU_LOCKDEP_WARN(!rcu_read_lock_bh_held()) and rcu_dereference_check(..., rcu_read_lock_bh_held()) calls, if we hit
the scenario, where bh lockmap state (shows bh lock acquired) conflicts with the softirq/irq state .

Thanks
Neeraj

Quoted 21 lines
Quoted 21 lines
---
  kernel/rcu/update.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
kernel/rcu/update.c +1 -1
index c21b38c..d416f1c 100644
@@ -333,7 +333,7 @@ int rcu_read_lock_bh_held(void)
 
  	if (rcu_read_lock_held_common(&ret))
  		return ret;
-	return in_softirq() || irqs_disabled();
+	return lock_is_held(&rcu_bh_lock_map) || in_softirq() || irqs_disabled();
  }
  EXPORT_SYMBOL_GPL(rcu_read_lock_bh_held);
 
Signature · 2 lines
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
hosted by The Linux Foundation
Signature · 1 line
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, hosted by The Linux Foundation
maintainer
#3 lore raw
Quoted 36 lines · Neeraj Upadhyay
Quoted 6 lines · Paul E. McKenney
Quoted 4 lines · Neeraj Upadhyay

In addition to irq and softirq state, check rcu_bh_lock_map state, to decide whether RCU bh lock is held.

Quoted 7 lines

My initial reaction was that "in_softirq() || irqs_disabled()" covers it because rcu_read_lock_bh() disables BH. But you are right that it does seem a bit silly to ignore lockdep.

So would it also make sense to have a WARN_ON_ONCE() if lockdep claims we are under rcu_read_lock_bh() protection, but "in_softirq() || irqs_disabled()" think otherwise?

After thinking more on this, looks like one intention of not having lockdep check here was to catch scenarios where some code enables bh after doing rcu_read_lock_bh(), as is mentioned in the comment above rcu_read_lock_bh_held():

Note that if someone uses
rcu_read_lock_bh(), but then later enables BH, lockdep (if enabled) will show the situation. This is useful for debug checks in functions that require that they be called within an RCU read-side critical section.

Client users seem to be doing lockdep checks on returned value:
drivers/net/wireguard/peer.c
RCU_LOCKDEP_WARN(!rcu_read_lock_bh_held(),

Similarly, any rcu_dereference_check(..., rcu_read_lock_bh_held()) usage also triggers warning, if bh is enabled, inside rcu_read_lock_bh() section.

Quoted 7 lines

So, using 'in_softirq() || irqs_disabled()' condition looks to be sufficient condition, to mark all read lock bh regions and adding '|| lock_is_held(&rcu_bh_lock_map)' to this condition does not seem to fit well with the RCU_LOCKDEP_WARN(!rcu_read_lock_bh_held()) and rcu_dereference_check(..., rcu_read_lock_bh_held()) calls, if we hit the scenario, where bh lockmap state (shows bh lock acquired) conflicts with the softirq/irq state .

That makes sense to me!

But should there be checks somewhere for something like "lock_is_held(&rcu_bh_lock_map) && !in_softirq() && !irqs_disabled()"?

Thanx, Paul

Quoted 26 lines

Thanks
Neeraj

Quoted 19 lines
Quoted 19 lines
---
  kernel/rcu/update.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
kernel/rcu/update.c +1 -2
index c21b38c..d416f1c 100644
@@ -333,7 +333,7 @@ int rcu_read_lock_bh_held(void)
  	if (rcu_read_lock_held_common(&ret))
  		return ret;
-	return in_softirq() || irqs_disabled();
+	return lock_is_held(&rcu_bh_lock_map) || in_softirq() || irqs_disabled();
  }
  EXPORT_SYMBOL_GPL(rcu_read_lock_bh_held);
-- 
 QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,

hosted by The Linux Foundation

Signature · 2 lines
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member of
the Code Aurora Forum, hosted by The Linux Foundation
#4 lore raw
Quoted 45 lines · Paul E. McKenney
Quoted 36 lines · Neeraj Upadhyay
Quoted 6 lines · Paul E. McKenney
Quoted 4 lines · Neeraj Upadhyay

In addition to irq and softirq state, check rcu_bh_lock_map state, to decide whether RCU bh lock is held.

Quoted 7 lines

My initial reaction was that "in_softirq() || irqs_disabled()" covers it because rcu_read_lock_bh() disables BH. But you are right that it does seem a bit silly to ignore lockdep.

So would it also make sense to have a WARN_ON_ONCE() if lockdep claims we are under rcu_read_lock_bh() protection, but "in_softirq() || irqs_disabled()" think otherwise?

After thinking more on this, looks like one intention of not having lockdep check here was to catch scenarios where some code enables bh after doing rcu_read_lock_bh(), as is mentioned in the comment above rcu_read_lock_bh_held():

Note that if someone uses
rcu_read_lock_bh(), but then later enables BH, lockdep (if enabled) will show the situation. This is useful for debug checks in functions that require that they be called within an RCU read-side critical section.

Client users seem to be doing lockdep checks on returned value:
drivers/net/wireguard/peer.c

    RCU_LOCKDEP_WARN(!rcu_read_lock_bh_held(),

Similarly, any rcu_dereference_check(..., rcu_read_lock_bh_held()) usage also triggers warning, if bh is enabled, inside rcu_read_lock_bh() section.

Quoted 7 lines

So, using 'in_softirq() || irqs_disabled()' condition looks to be sufficient condition, to mark all read lock bh regions and adding '|| lock_is_held(&rcu_bh_lock_map)' to this condition does not seem to fit well with the RCU_LOCKDEP_WARN(!rcu_read_lock_bh_held()) and rcu_dereference_check(..., rcu_read_lock_bh_held()) calls, if we hit the scenario, where bh lockmap state (shows bh lock acquired) conflicts with the softirq/irq state .

Quoted 7 lines

That makes sense to me!

But should there be checks somewhere for something like "lock_is_held(&rcu_bh_lock_map) && !in_softirq() && !irqs_disabled()"?

Thanx, Paul

I think this check is good to have inside rcu_read_lock_bh_held(), to highlight this scenario explicitly; I am thinking, if it makes sense to have lock_is_held(&rcu_bh_lock_map) check in rcu_softirq_qs() ?

Also, I think this check is more important for rcu_read_lock_sched_held(), where lockdep state is used as a sufficient condition, for marking a RCU sched region. One more api is rcu_read_lock_any_held(), where we can
warn on conflicting cases.

  int rcu_read_lock_sched_held(void)
  {
        bool ret;
        if (rcu_read_lock_held_common(&ret))
                return ret;
        return lock_is_held(&rcu_sched_lock_map) || !preemptible();

}

Thanks
Neeraj

Quoted 26 lines
Quoted 26 lines

Thanks
Neeraj

Quoted 19 lines
Quoted 19 lines
---
   kernel/rcu/update.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
kernel/rcu/update.c +1 -2
index c21b38c..d416f1c 100644
@@ -333,7 +333,7 @@ int rcu_read_lock_bh_held(void)
   	if (rcu_read_lock_held_common(&ret))
   		return ret;
-	return in_softirq() || irqs_disabled();
+	return lock_is_held(&rcu_bh_lock_map) || in_softirq() || irqs_disabled();
   }
   EXPORT_SYMBOL_GPL(rcu_read_lock_bh_held);
-- 
 QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,

hosted by The Linux Foundation

Signature · 2 lines
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member of
the Code Aurora Forum, hosted by The Linux Foundation
Signature · 1 line
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, hosted by The Linux Foundation
maintainer
#5 lore raw
Quoted 61 lines · Neeraj Upadhyay
Quoted 43 lines · Paul E. McKenney
Quoted 34 lines · Neeraj Upadhyay
Quoted 6 lines · Paul E. McKenney
Quoted 4 lines · Neeraj Upadhyay

In addition to irq and softirq state, check rcu_bh_lock_map state, to decide whether RCU bh lock is held.

Quoted 7 lines

My initial reaction was that "in_softirq() || irqs_disabled()" covers it because rcu_read_lock_bh() disables BH. But you are right that it does seem a bit silly to ignore lockdep.

So would it also make sense to have a WARN_ON_ONCE() if lockdep claims we are under rcu_read_lock_bh() protection, but "in_softirq() || irqs_disabled()" think otherwise?

After thinking more on this, looks like one intention of not having lockdep check here was to catch scenarios where some code enables bh after doing rcu_read_lock_bh(), as is mentioned in the comment above rcu_read_lock_bh_held():

Note that if someone uses
rcu_read_lock_bh(), but then later enables BH, lockdep (if enabled) will show the situation. This is useful for debug checks in functions that require that they be called within an RCU read-side critical section.

Client users seem to be doing lockdep checks on returned value:
drivers/net/wireguard/peer.c

    RCU_LOCKDEP_WARN(!rcu_read_lock_bh_held(),

Similarly, any rcu_dereference_check(..., rcu_read_lock_bh_held()) usage also triggers warning, if bh is enabled, inside rcu_read_lock_bh() section.

Quoted 7 lines

So, using 'in_softirq() || irqs_disabled()' condition looks to be sufficient condition, to mark all read lock bh regions and adding '|| lock_is_held(&rcu_bh_lock_map)' to this condition does not seem to fit well with the RCU_LOCKDEP_WARN(!rcu_read_lock_bh_held()) and rcu_dereference_check(..., rcu_read_lock_bh_held()) calls, if we hit the scenario, where bh lockmap state (shows bh lock acquired) conflicts with the softirq/irq state .

Quoted 7 lines

That makes sense to me!

But should there be checks somewhere for something like "lock_is_held(&rcu_bh_lock_map) && !in_softirq() && !irqs_disabled()"?

Thanx, Paul

I think this check is good to have inside rcu_read_lock_bh_held(), to highlight this scenario explicitly; I am thinking, if it makes sense to have lock_is_held(&rcu_bh_lock_map) check in rcu_softirq_qs() ?

Also, I think this check is more important for rcu_read_lock_sched_held(), where lockdep state is used as a sufficient condition, for marking a RCU sched region. One more api is rcu_read_lock_any_held(), where we can warn on conflicting cases.

Quoted 8 lines
  int rcu_read_lock_sched_held(void)
  {
        bool ret;
        if (rcu_read_lock_held_common(&ret))
                return ret;
        return lock_is_held(&rcu_sched_lock_map) || !preemptible();

}

Another option would be to check lock_is_held(&rcu_sched_lock_map) anywhere preemption is enabled and lock_is_held(&rcu_bh_lock_map) anywhere BH is enabled. This would (in theory, anyway) catch more bugs.

But it is necessary to be careful because these checks must be suppressed if lockdep has been disabled due to a prior lockdep splat. Otherwise, the first lockdep splat is followed by an inundation of false-positive complaints.

But the real question is "Which important bugs are missed today, and what change would catch them more reliably?"

Thoughts?

Thanx, Paul

Quoted 33 lines

Thanks
Neeraj

Quoted 26 lines
Quoted 26 lines

Thanks
Neeraj

Quoted 19 lines
Quoted 19 lines
---
   kernel/rcu/update.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
kernel/rcu/update.c +1 -2
index c21b38c..d416f1c 100644
@@ -333,7 +333,7 @@ int rcu_read_lock_bh_held(void)
   	if (rcu_read_lock_held_common(&ret))
   		return ret;
-	return in_softirq() || irqs_disabled();
+	return lock_is_held(&rcu_bh_lock_map) || in_softirq() || irqs_disabled();
   }
   EXPORT_SYMBOL_GPL(rcu_read_lock_bh_held);
-- 
 QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,

hosted by The Linux Foundation

Signature · 2 lines
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member of
the Code Aurora Forum, hosted by The Linux Foundation
Signature · 2 lines
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member of
the Code Aurora Forum, hosted by The Linux Foundation