Skip to contentSkip to conversation

srcutree: Add WARN_ON_ONCE() for atomic SRCU in srcu_gp_end()

PATCH In review Kunwu Chan 2 participants 4 messages Sep 10 – Sep 11

Subsystems (2)
lore
Share & tools
lore

Kunwu Chan

author
#0 lore raw

PATCH v2 1/1 srcutree: Add WARN_ON_ONCE() for atomic SRCU in srcu_gp_end()

Atomic SRCU remain in the small size state, so add a WARN_ON_ONCE() before the transition check to catch any future code path that might violate this invariant.

---
Changes since v1:
- Drop the !is_atomic guard from v1 and add WARN_ON_ONCE() instead, per Paul McKenney's suggestion.
- v1: https://lore.kernel.org/rcu/[email protected]/

---
 kernel/rcu/srcutree.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/rcu/srcutree.c +2 -0
index 84c022ec8e09..68441c32cebf 100644
@@ -1074,6 +1074,8 @@ static void srcu_gp_end(struct srcu_struct *ssp, bool is_atomic)
 	}
 
 	/* Transition to big if needed. */
+	WARN_ON_ONCE(ssp->srcu_reader_flavor == SRCU_READ_FLAVOR_ATOMIC &&
+			ss_state != SRCU_SIZE_SMALL);
 	if (ss_state != SRCU_SIZE_SMALL && ss_state != SRCU_SIZE_BIG) {
 		if (ss_state == SRCU_SIZE_ALLOC)
 			init_srcu_struct_nodes(ssp, GFP_KERNEL);
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 19 lines · Kunwu Chan

Atomic SRCU remain in the small size state, so add a WARN_ON_ONCE() before the transition check to catch any future code path that might violate this invariant.

---
Changes since v1:
- Drop the !is_atomic guard from v1 and add WARN_ON_ONCE() instead, per Paul McKenney's suggestion.
- v1: https://lore.kernel.org/rcu/[email protected]/

---
 kernel/rcu/srcutree.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/rcu/srcutree.c +0 -0
index 84c022ec8e09..68441c32cebf 100644

kernel/rcu/srcutree.c static void srcu_gp_end(struct srcu_struct *ssp, bool is_atomic) · line 1078

Quoted 5 lines
kernel/rcu/srcutree.c +2 -0
@@ -1074,6 +1074,8 @@ static void srcu_gp_end(struct srcu_struct *ssp, bool is_atomic)
 	}
 
 	/* Transition to big if needed. */
+	WARN_ON_ONCE(ssp->srcu_reader_flavor == SRCU_READ_FLAVOR_ATOMIC &&
+			ss_state != SRCU_SIZE_SMALL);

Also much better, but we also need to avoid doing the transition in this case, because doing that transition breaks atomic SRCU. On way to make this happen is something like this:

	if (ssp->srcu_reader_flavor == SRCU_READ_FLAVOR_ATOMIC && ss_state != SRCU_SIZE_SMALL) {
		WARN_ON_ONCE(1);
	} else if (ss_state != SRCU_SIZE_SMALL && ss_state != SRCU_SIZE_BIG) {
		if (ss_state == SRCU_SIZE_ALLOC)
			init_srcu_struct_nodes(ssp, GFP_KERNEL);

Other approaches might take advantage of the fact that WARN_ON_ONCE() returns the value of its argument, allowing WARN_ON_ONCE() to be used as a condition in an "if" statement.

Thanx, Paul

Quoted 6 lines
kernel/rcu/srcutree.c +0 -0
 	if (ss_state != SRCU_SIZE_SMALL && ss_state != SRCU_SIZE_BIG) {
 		if (ss_state == SRCU_SIZE_ALLOC)
 			init_srcu_struct_nodes(ssp, GFP_KERNEL);
Signature · 1 line
2.43.0
author
#2 lore raw
Quoted 31 lines · Paul E. McKenney
Quoted 19 lines · Kunwu Chan

Atomic SRCU remain in the small size state, so add a WARN_ON_ONCE() before the transition check to catch any future code path that might violate this invariant.

---
Changes since v1:
- Drop the !is_atomic guard from v1 and add WARN_ON_ONCE() instead, per Paul McKenney's suggestion.
- v1: https://lore.kernel.org/rcu/[email protected]/

---
 kernel/rcu/srcutree.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/rcu/srcutree.c +0 -0
index 84c022ec8e09..68441c32cebf 100644

kernel/rcu/srcutree.c static void srcu_gp_end(struct srcu_struct *ssp, bool is_atomic) · line 1078

Quoted 5 lines
kernel/rcu/srcutree.c +2 -0
@@ -1074,6 +1074,8 @@ static void srcu_gp_end(struct srcu_struct *ssp, bool is_atomic)
      }
 
      /* Transition to big if needed. */
+     WARN_ON_ONCE(ssp->srcu_reader_flavor == SRCU_READ_FLAVOR_ATOMIC &&
+                     ss_state != SRCU_SIZE_SMALL);

Also much better, but we also need to avoid doing the transition in this case, because doing that transition breaks atomic SRCU. On way to make this happen is something like this:

Quoted 6 lines
        if (ssp->srcu_reader_flavor == SRCU_READ_FLAVOR_ATOMIC && ss_state != SRCU_SIZE_SMALL) {
                WARN_ON_ONCE(1);
        } else if (ss_state != SRCU_SIZE_SMALL && ss_state != SRCU_SIZE_BIG) {
                if (ss_state == SRCU_SIZE_ALLOC)
                        init_srcu_struct_nodes(ssp, GFP_KERNEL);

Thanks, Paul. Good point. The original !is_atomic guard in v1 already prevented the transition for atomic SRCU. I was mainly thinking of the WARN as a diagnostic, since this state should not occur with the current code. I agree that retaining the protection against the transition is safer if the invariant is ever violated.

Quoted 4 lines

Other approaches might take advantage of the fact that WARN_ON_ONCE() returns the value of its argument, allowing WARN_ON_ONCE() to be used as a condition in an "if" statement.

I prefer the first approach. Using !WARN_ON_ONCE() directly in the existing condition would make it rather long and less readable, while the explicit if/else keeps the control flow clear.

Thanks,
Kunwu

Quoted 8 lines

Thanx, Paul

Quoted 6 lines
kernel/rcu/srcutree.c +0 -1
      if (ss_state != SRCU_SIZE_SMALL && ss_state != SRCU_SIZE_BIG) {
              if (ss_state == SRCU_SIZE_ALLOC)
                      init_srcu_struct_nodes(ssp, GFP_KERNEL);
--

2.43.0

maintainer
#3 lore raw
Quoted 45 lines · KunWu Chan
Quoted 31 lines · Paul E. McKenney
Quoted 19 lines · Kunwu Chan

Atomic SRCU remain in the small size state, so add a WARN_ON_ONCE() before the transition check to catch any future code path that might violate this invariant.

---
Changes since v1:
- Drop the !is_atomic guard from v1 and add WARN_ON_ONCE() instead, per Paul McKenney's suggestion.
- v1: https://lore.kernel.org/rcu/[email protected]/

---
 kernel/rcu/srcutree.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/rcu/srcutree.c +0 -0
index 84c022ec8e09..68441c32cebf 100644

kernel/rcu/srcutree.c static void srcu_gp_end(struct srcu_struct *ssp, bool is_atomic) · line 1078

Quoted 5 lines
kernel/rcu/srcutree.c +2 -0
@@ -1074,6 +1074,8 @@ static void srcu_gp_end(struct srcu_struct *ssp, bool is_atomic)
      }
 
      /* Transition to big if needed. */
+     WARN_ON_ONCE(ssp->srcu_reader_flavor == SRCU_READ_FLAVOR_ATOMIC &&
+                     ss_state != SRCU_SIZE_SMALL);

Also much better, but we also need to avoid doing the transition in this case, because doing that transition breaks atomic SRCU. On way to make this happen is something like this:

Quoted 6 lines
        if (ssp->srcu_reader_flavor == SRCU_READ_FLAVOR_ATOMIC && ss_state != SRCU_SIZE_SMALL) {
                WARN_ON_ONCE(1);
        } else if (ss_state != SRCU_SIZE_SMALL && ss_state != SRCU_SIZE_BIG) {
                if (ss_state == SRCU_SIZE_ALLOC)
                        init_srcu_struct_nodes(ssp, GFP_KERNEL);

Thanks, Paul. Good point. The original !is_atomic guard in v1 already prevented the transition for atomic SRCU. I was mainly thinking of the WARN as a diagnostic, since this state should not occur with the current code. I agree that retaining the protection against the transition is safer if the invariant is ever violated.

Quoted 8 lines
Quoted 4 lines

Other approaches might take advantage of the fact that WARN_ON_ONCE() returns the value of its argument, allowing WARN_ON_ONCE() to be used as a condition in an "if" statement.

I prefer the first approach. Using !WARN_ON_ONCE() directly in the existing condition would make it rather long and less readable, while the explicit if/else keeps the control flow clear.

Very good, looking forward to seeing what you come up with.

Thanx, Paul

Quoted 11 lines

Thanks,
Kunwu

Quoted 8 lines

Thanx, Paul

Quoted 6 lines
kernel/rcu/srcutree.c +0 -1
      if (ss_state != SRCU_SIZE_SMALL && ss_state != SRCU_SIZE_BIG) {
              if (ss_state == SRCU_SIZE_ALLOC)
                      init_srcu_struct_nodes(ssp, GFP_KERNEL);
--

2.43.0