mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Bart Van Assche <bvanassche@acm.org>
To: Marco Elver <elver@google.com>
Cc: Julia Lawall <Julia.Lawall@inria.fr>,
	Nicolas Palix <nicolas.palix@imag.fr>,
	linux-kernel@vger.kernel.org, cocci@inria.fr,
	kasan-dev@googlegroups.com
Subject: Re: [PATCH] coccinelle: locks: Add must_hold.cocci
Date: Thu, 19 Nov 2026 13:31:47 -0800	[thread overview]
Message-ID: <591a87fb-042e-417c-b9bd-32e3b9cb645f@acm.org> (raw)
In-Reply-To: <20261119210807.211041-1-elver@google.com>

On 11/19/26 1:07 PM, Marco Elver wrote:
> Clang's context analysis treats lockdep_assert_held*() as an assumption
> that the lock is held. A function that asserts instead of being
> annotated with __must_hold() therefore passes the analysis, but its
> callers are never checked.
Hi Marco,

The LLM I asked for help with reviewing this patch provided the
following feedback (not sure all of this is correct):

 >   drivers/nvme/host/fabrics.c:650:1-20: nvmf_lookup_transport() 
asserts &nvmf_transports_rwsem is held but lacks 
__must_hold(&nvmf_transports_rwsem)

This suggestion is wrong. The only caller of nvmf_lookup_transport()
holds nvmf_transports_rwsem as a reader:

	down_read(&nvmf_transports_rwsem);
	ops = nvmf_lookup_transport(opts);

Hence, the correct annotation is __must_hold_shared() and adding
__must_hold() would trigger a new context analysis warning in the
caller. lockdep_assert_held() succeeds if a lock is held in any mode, so
for that macro the script cannot know which of the two annotations is
appropriate. How about reporting "lacks __must_hold() or
__must_hold_shared()" for lockdep_assert_held() and keeping the specific
suggestion for the _write() and _read() variants only?

 > +	# Attributes on a prototype in the same file.
 > +	for d in re.finditer(r'\b%s\s*\([^;{}]*\)([^;{}]*);' % re.escape(f),
 > +			     ''.join(lines)):
 > +		if '__must_hold' in d.group(1):
 > +			return None

It seems to me that this test never succeeds. '[^;{}]*\)' is greedy and
hence matches up to the last ')' before the semicolon. That is the
closing parenthesis of the attribute itself, so group 1 is empty:

 >>> import re
 >>> s = 'static int foo(struct x *x) __must_hold(&x->lock);'
 >>> re.search(r'\bfoo\s*\([^;{}]*\)([^;{}]*);', s).group(1)
''

The result is the same if the attribute occurs on the next line. I think
that this will cause functions that are only annotated on a forward
declaration to be reported. Testing d.group(0) instead of d.group(1)
would fix this. Additionally, should this test use the same attribute
list as the test above it (__releases*(), __context_unsafe(),
__no_context_analysis)?

 > +@r@
 > +identifier f;
 > +parameter list ps;
 > +expression E;
 > +position pf, p;
 > +@@
 > +
 > +f@pf(ps)
 > +{
 > +	...
 > +(
 > +	lockdep_assert_held@p(E);
 > +|
 > +	lockdep_assert_held_write@p(E);
 > +|
 > +	lockdep_assert_held_read@p(E);
 > +)
 > +	...
 > +}

Is my understanding correct that the default for a rule without
'-', '+' or '*' annotations is "exists" instead of "forall"? If so, this
rule also matches conditional assertions, e.g. this code from
fs/btrfs/backref.c:

	if (!current->journal_info)
		lockdep_assert_held(&fs_info->commit_root_sem);

__must_hold() would be wrong for such functions, and the above does not
match "assert on all paths" from the patch description nor
"unconditionally assert" from the comment at the top of the script.
Please consider specifying "forall" or "exists" explicitly.

Additionally, this rule matches functions that acquire E themselves
before asserting that it is held. Can these be excluded, e.g. with a
"when" constraint on the first "..."?

 > +	m = re.match(r'[&(]*(\w+)', e)
 > +	if not m:
 > +		return None
 > +	params = re.findall(r'(\w+)\s*(?:\[[^\]]*\]\s*)*(?:,|$)', str(ps))
 > +	if m.group(1) not in params and not re.fullmatch(r'&\w+', e):
 > +		return None	# Local alias: cannot be named in an attribute.

Only the first identifier of E is checked. Expressions like
&tbl->locks[idx] with 'tbl' a parameter and 'idx' a local variable are
reported although these cannot be used as an attribute argument. Would
it be possible to verify that all identifiers that occur in E are either
parameters or struct member names?

The 'params' regular expression does not extract the correct name for
all parameter declarations:

'void (*cb)(struct foo *), int n'    -> ['n']
'int (*fn)(void *data, int x)'       -> ['data']
'struct task_struct *p __maybe_unused' -> ['__maybe_unused']

Is there a way to let Coccinelle provide the parameter names instead of
extracting these with a regular expression?

 > +	static = 'static' in ''.join(lines[max(first - 3, 0):first])

This is a substring search across three lines, so e.g. a
"static int counter;" declaration two lines above a non-static function
suppresses the "check prototype" hint. Can 'static' be matched in the
SmPL rule instead?

Some further remarks:
- lockdep_assert_held_once() and assert_spin_locked() are not handled
   (71 respectively 256 occurrences in .c files in my tree), nor are
   wrappers like lockdep_assert_wiphy(). Please either add these or
   mention this in the "Limitations" section.
- Global locks are only recognized if E has the form "&name".
   Expressions like &init_mm.mmap_lock are skipped silently. That's fine
   with me but it is not mentioned in the comment at the top.
- The patch description mentions that functions with a __must_hold*()
   or __releases*() annotation are skipped. The script also skips
   functions annotated with __context_unsafe() or __no_context_analysis.

Thanks,

Bart.


      reply	other threads:[~2026-11-19 21:31 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 21:07 Marco Elver
2026-11-19 21:31 ` 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=591a87fb-042e-417c-b9bd-32e3b9cb645f@acm.org \
    --to=bvanassche@acm.org \
    --cc=Julia.Lawall@inria.fr \
    --cc=cocci@inria.fr \
    --cc=elver@google.com \
    --cc=kasan-dev@googlegroups.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nicolas.palix@imag.fr \
    /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®