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 10:20:31 -0800	[thread overview]
Message-ID: <d345bdcd-d3ef-42b6-b2d0-a75f1830f1c5@acm.org> (raw)
In-Reply-To: <2d072123-0315-4432-9f5d-74d6071adb8e@paulmck-laptop>

On 11/17/26 9:53 AM, Paul E. McKenney wrote:
 > Would __builtin_constant_p() help here?
 >
 > If we don't have a build-time constant, don't check.
 >
 > If we do have a build-time constant, check only if that constant has
 > the value zero, that is, there is no update-side protection.

Hi Paul,

Yes, that helps. I had missed this option. Something like the
following does what you describe:

	#define srcu_dereference_check(p, ssp, c)			\
	({								\
		if (__builtin_constant_p(c) && !(c))			\
			__srcu_read_lock_must_hold(ssp);		\
		__acquire_shared_ctx_lock(RCU);				\
		...

This also fixes the runtime problem I mentioned: the argument of
__builtin_constant_p() is never evaluated, and '!(c)' is only
evaluated if 'c' is a constant.

I tested this with clang -Wthread-safety at -O0 and -O2:

	c == 0                            -> checked (warns without
	                                     the SRCU read lock)
	c == 1                            -> not checked
	c == lockdep_is_held(&m)          -> not checked
	c == ({ (void)(&m); 1; })         -> not checked
	     (lockdep_is_held() with CONFIG_LOCKDEP=n)

There is one limitation. If 'c' is a plain variable, function
argument or structure member, e.g. srcu_dereference_check(p, ssp,
foo->flag), Clang does not resolve __builtin_constant_p() in the
front end. It leaves that to the optimizer, which may still find a
constant later. The context analysis then treats the call to
__srcu_read_lock_must_hold() as reachable and reports a false
positive. Clang does resolve __builtin_constant_p() to 0 when 'c'
contains a function call, and that covers lockdep_is_held(),
srcu_read_lock_held() and refcount_read(). In the current tree,
every srcu_dereference_check() caller passes either a constant or
an expression built from such calls, so this limitation doesn't
affect any existing caller.

So in v4 I plan to:
- call __srcu_read_lock_must_hold() from srcu_dereference_check()
   only if 'c' is the constant 0, as shown above;
- keep srcu_dereference() defined as
   srcu_dereference_check((p), (ssp), 0), which now gets the check
   automatically;
- keep the explicit __srcu_read_lock_must_hold() call in
   srcu_dereference_notrace(), because it passes 1 for 'c';
- update the comment above __srcu_read_lock_must_hold() and the
   patch description to match.

Does that sound good to you?

Thanks,

Bart.

  reply	other threads:[~2026-11-17 18:20 UTC|newest]

Thread overview: 10+ 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
2026-11-17 17:53       ` Paul E. McKenney
2026-11-17 18:20         ` Bart Van Assche [this message]
2026-11-17 18:44           ` Paul E. McKenney
2026-11-17 20:03             ` Bart Van Assche

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=d345bdcd-d3ef-42b6-b2d0-a75f1830f1c5@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®