mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Bart Van Assche <bvanassche@acm.org>
To: paulmck@kernel.org
Cc: "Peter Zijlstra" <peterz@infradead.org>,
	"Ingo Molnar" <mingo@redhat.com>, "Will Deacon" <will@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>,
	"Waiman Long" <longman@redhat.com>,
	linux-kernel@vger.kernel.org, "Marco Elver" <elver@google.com>,
	"Lai Jiangshan" <jiangshanlai@gmail.com>,
	"Josh Triplett" <josh@joshtriplett.org>,
	"Onur Özkan" <work@onurozkan.dev>,
	"Nathan Chancellor" <nathan@kernel.org>
Subject: Re: [PATCH v3 3/3] srcu: Remove check from srcu_dereference_check()
Date: Tue, 17 Nov 2026 08:53:52 -0800	[thread overview]
Message-ID: <c935da68-bd05-46b1-b837-e62cda95c0ea@acm.org> (raw)
In-Reply-To: <723856b0-e1c9-41c7-a6d3-32a45ef1f88a@paulmck-laptop>

On 11/16/26 4:51 PM, Paul E. McKenney wrote:
 > Should we invoke __srcu_read_lock_must_hold() conditionally, only if the
 > value of c is zero?

Hi Paul,

Thanks for the review. I don't think that would help. The Clang
context analysis is a compile-time analysis. It does not know the
runtime value of 'c', so a conditional call to
__srcu_read_lock_must_hold() only behaves differently from an
unconditional call if the compiler can evaluate 'c' at compile time:

- If 'c' is the constant 0, Clang removes the always-false edge and
   the call is checked. This is what srcu_dereference() does with
   this patch.

- If 'c' is the constant 1, the call is never checked. This is what
   srcu_dereference_notrace() would get. srcu_dereference_notrace()
   calls __srcu_read_lock_must_hold() directly, so it keeps the
   check.

- If 'c' is a real lockdep expression, e.g.
   lockdep_is_held(&foo->mutex), Clang can't evaluate it. The call
   stays reachable and is checked. That brings back the false
   positive for update-side callers that this patch fixes. With
   CONFIG_LOCKDEP=n, lockdep_is_held() becomes
   ({ (void)(lock); 1; }), so the result would also depend on the
   kernel config.

I checked this with a small test program built with clang
-Wthread-safety:

	#define deref_check(c) ({ if (!(c)) must_hold(&s); 0; })

	deref_check(0);                 /* warns, as expected       */
	deref_check(1);                 /* no warning               */
	deref_check(is_held(&m));       /* warns: false positive    */
	deref_check(({ (void)&m; 1; })) /* no warning (lockdep off) */

A conditional call would also evaluate 'c' outside the lockdep
checks, so it would run even when lockdep is disabled. Today 'c' is
only evaluated inside __rcu_dereference_check() when
CONFIG_PROVE_RCU=y.

So the only case where the conditional call helps is a constant 0,
and srcu_dereference() already covers that. Callers that pass a
literal 0 directly to srcu_dereference_check() can switch to
srcu_dereference(). The runtime check is unchanged: lockdep still
checks '(c) || srcu_read_lock_held(ssp)'.

Thanks,

Bart.

      reply	other threads:[~2026-11-17 16:53 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16 22:59 [PATCH 0/3] Fix three Clang context analysis issues Bart Van Assche
2026-11-16 22:59 ` [PATCH v3 1/3] bit_spinlock: Fix the __bitlock() definition Bart Van Assche
2026-11-16 22:59 ` [PATCH v3 2/3] cleanup: Disable context analysis for __free() helpers Bart Van Assche
2026-11-16 22:59 ` [PATCH v3 3/3] srcu: Remove check from srcu_dereference_check() Bart Van Assche
2026-11-17  0:51   ` Paul E. McKenney
2026-11-17 16:53     ` Bart Van Assche [this message]

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=c935da68-bd05-46b1-b837-e62cda95c0ea@acm.org \
    --to=bvanassche@acm.org \
    --cc=boqun@kernel.org \
    --cc=elver@google.com \
    --cc=jiangshanlai@gmail.com \
    --cc=josh@joshtriplett.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=longman@redhat.com \
    --cc=mingo@redhat.com \
    --cc=nathan@kernel.org \
    --cc=paulmck@kernel.org \
    --cc=peterz@infradead.org \
    --cc=will@kernel.org \
    --cc=work@onurozkan.dev \
    /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®