mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Paul E. McKenney" <paulmck@kernel.org>
To: rcu@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, kernel-team@fb.com,
	rostedt@goodmis.org, "Paul E. McKenney" <paulmck@kernel.org>,
	Neeraj Upadhyay <quic_neeraju@quicinc.com>
Subject: [PATCH rcu 13/21] srcu: Avoid NULL dereference in srcu_torture_stats_print()
Date: Mon, 18 Apr 2022 17:03:14 -0700	[thread overview]
Message-ID: <20220419000322.3948903-13-paulmck@kernel.org> (raw)
In-Reply-To: <20220419000315.GA3948789@paulmck-ThinkPad-P17-Gen-1>

You really shouldn't invoke srcu_torture_stats_print() after invoking
cleanup_srcu_struct(), but there is really no reason to get a
compiler-obfuscated per-CPU-variable NULL pointer dereference as the
diagnostic.  This commit therefore checks for NULL ->sda and makes a
more polite console-message complaint in that case.

Co-developed-by: Neeraj Upadhyay <quic_neeraju@quicinc.com>
Signed-off-by: Neeraj Upadhyay <quic_neeraju@quicinc.com>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
---
 kernel/rcu/srcutree.c | 62 ++++++++++++++++++++++++-------------------
 1 file changed, 34 insertions(+), 28 deletions(-)

diff --git a/kernel/rcu/srcutree.c b/kernel/rcu/srcutree.c
index 9dd048989027..b7138dbe1a2d 100644
--- a/kernel/rcu/srcutree.c
+++ b/kernel/rcu/srcutree.c
@@ -1477,37 +1477,43 @@ void srcu_torture_stats_print(struct srcu_struct *ssp, char *tt, char *tf)
 	idx = ssp->srcu_idx & 0x1;
 	if (ss_state < 0 || ss_state >= ARRAY_SIZE(srcu_size_state_name))
 		ss_state_idx = ARRAY_SIZE(srcu_size_state_name) - 1;
-	pr_alert("%s%s Tree SRCU g%ld state %d (%s) per-CPU(idx=%d):",
+	pr_alert("%s%s Tree SRCU g%ld state %d (%s)",
 		 tt, tf, rcu_seq_current(&ssp->srcu_gp_seq), ss_state,
-		 srcu_size_state_name[ss_state_idx], idx);
-	for_each_possible_cpu(cpu) {
-		unsigned long l0, l1;
-		unsigned long u0, u1;
-		long c0, c1;
-		struct srcu_data *sdp;
-
-		sdp = per_cpu_ptr(ssp->sda, cpu);
-		u0 = data_race(sdp->srcu_unlock_count[!idx]);
-		u1 = data_race(sdp->srcu_unlock_count[idx]);
+		 srcu_size_state_name[ss_state_idx]);
+	if (!ssp->sda) {
+		// Called after cleanup_srcu_struct(), perhaps.
+		pr_cont(" No per-CPU srcu_data structures (->sda == NULL).\n");
+	} else {
+		pr_cont(" per-CPU(idx=%d):", idx);
+		for_each_possible_cpu(cpu) {
+			unsigned long l0, l1;
+			unsigned long u0, u1;
+			long c0, c1;
+			struct srcu_data *sdp;
 
-		/*
-		 * Make sure that a lock is always counted if the corresponding
-		 * unlock is counted.
-		 */
-		smp_rmb();
-
-		l0 = data_race(sdp->srcu_lock_count[!idx]);
-		l1 = data_race(sdp->srcu_lock_count[idx]);
-
-		c0 = l0 - u0;
-		c1 = l1 - u1;
-		pr_cont(" %d(%ld,%ld %c)",
-			cpu, c0, c1,
-			"C."[rcu_segcblist_empty(&sdp->srcu_cblist)]);
-		s0 += c0;
-		s1 += c1;
+			sdp = per_cpu_ptr(ssp->sda, cpu);
+			u0 = data_race(sdp->srcu_unlock_count[!idx]);
+			u1 = data_race(sdp->srcu_unlock_count[idx]);
+
+			/*
+			 * Make sure that a lock is always counted if the corresponding
+			 * unlock is counted.
+			 */
+			smp_rmb();
+
+			l0 = data_race(sdp->srcu_lock_count[!idx]);
+			l1 = data_race(sdp->srcu_lock_count[idx]);
+
+			c0 = l0 - u0;
+			c1 = l1 - u1;
+			pr_cont(" %d(%ld,%ld %c)",
+				cpu, c0, c1,
+				"C."[rcu_segcblist_empty(&sdp->srcu_cblist)]);
+			s0 += c0;
+			s1 += c1;
+		}
+		pr_cont(" T(%ld,%ld)\n", s0, s1);
 	}
-	pr_cont(" T(%ld,%ld)\n", s0, s1);
 	if (READ_ONCE(ssp->srcu_size_state) == SRCU_SIZE_SMALL && convert_to_big == 2)
 		WRITE_ONCE(ssp->srcu_size_state, SRCU_SIZE_ALLOC);
 }
-- 
2.31.1.189.g2e36527f23


  parent reply	other threads:[~2022-04-19  0:03 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-19  0:03 [PATCH rcu 0/21] SRCU updates for v5.19 Paul E. McKenney
2022-04-19  0:03 ` [PATCH rcu 01/21] srcu: Tighten cleanup_srcu_struct() GP checks Paul E. McKenney
2022-04-19  0:03 ` [PATCH rcu 02/21] srcu: Fix s/is/if/ typo in srcu_node comment Paul E. McKenney
2022-04-19  0:03 ` [PATCH rcu 03/21] srcu: Make srcu_funnel_gp_start() cache ->mynode in snp_leaf Paul E. McKenney
2022-04-19  0:03 ` [PATCH rcu 04/21] srcu: Make Tree SRCU able to operate without snp_node array Paul E. McKenney
2022-04-19  0:03 ` [PATCH rcu 05/21] srcu: Dynamically allocate srcu_node array Paul E. McKenney
2022-04-19  0:03 ` [PATCH rcu 06/21] srcu: Add size-state transitioning code Paul E. McKenney
2022-04-19  0:03 ` [PATCH rcu 07/21] srcu: Make rcutorture dump the SRCU size state Paul E. McKenney
2022-04-19  0:03 ` [PATCH rcu 08/21] srcu: Compute snp_seq earlier in srcu_funnel_gp_start() Paul E. McKenney
2022-04-19  0:03 ` [PATCH rcu 09/21] srcu: Use invalid initial value for srcu_node GP sequence numbers Paul E. McKenney
2022-04-19  0:03 ` [PATCH rcu 10/21] srcu: Ensure snp nodes tree is fully initialized before traversal Paul E. McKenney
2022-04-19  0:03 ` [PATCH rcu 11/21] srcu: Add boot-time control over srcu_node array allocation Paul E. McKenney
2022-04-19  0:03 ` [PATCH rcu 12/21] srcu: Use export for srcu_struct defined by DEFINE_STATIC_SRCU() Paul E. McKenney
2022-04-19  0:03 ` Paul E. McKenney [this message]
2022-04-19  0:03 ` [PATCH rcu 14/21] srcu: Prevent cleanup_srcu_struct() from freeing non-dynamic ->sda Paul E. McKenney
2022-04-19  0:03 ` [PATCH rcu 15/21] srcu: Explain srcu_funnel_gp_start() call to list_add() is safe Paul E. McKenney
2022-04-19  0:03 ` [PATCH rcu 16/21] srcu: Create concurrency-safe helper for initiating size transition Paul E. McKenney
2022-04-19  0:03 ` [PATCH rcu 17/21] srcu: Add contention-triggered addition of srcu_node tree Paul E. McKenney
2022-04-19  0:03 ` [PATCH rcu 18/21] srcu: Automatically determine size-transition strategy at boot Paul E. McKenney
2022-04-19  0:03 ` [PATCH rcu 19/21] srcu: Add contention check to call_srcu() srcu_data ->lock acquisition Paul E. McKenney
2022-04-19  0:03 ` [PATCH rcu 20/21] srcu: Prevent expedited GPs and blocking readers from consuming CPU Paul E. McKenney
2022-04-19  0:03 ` [PATCH rcu 21/21] srcu: Drop needless initialization of sdp in srcu_gp_start() Paul E. McKenney

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220419000322.3948903-13-paulmck@kernel.org \
    --to=paulmck@kernel.org \
    --cc=kernel-team@fb.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=quic_neeraju@quicinc.com \
    --cc=rcu@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®