All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Ian Rogers" <irogers@google.com>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH v1 07/10] perf dso: Fix unlocked __dsos__add() in vdso and expand lock annotations
Date: Tue, 13 Oct 2026 18:41:08 +0000	[thread overview]
Message-ID: <sashiko-outbox-170345@kernel.org> (raw)
In-Reply-To: <20261013182458.1234616-8-irogers@google.com>

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] perf dso: AB-BA lock inversion deadlock between dso__data_open_lock and dso->lock during fd eviction
- [High] perf dso: Out-of-bounds list access and crash due to dropped lock desyncing open count
- [Medium] perf vdso: TOCTOU race in machine__findnew_vdso allows duplicate VDSO dso insertion
--

commit 8889b552b9a0b44cdbc90055b9073e8918dcfc00
Author: Ian Rogers <irogers@google.com>

perf dso: Fix unlocked __dsos__add() in vdso and expand lock annotations

This commit expands lock annotations for the struct dsos lock and struct
dso data structures, updating locking primitives to ensure proper 
concurrency tracking. It also switches VDSO addition to use the 
locked dsos__add function.

> diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c
> index ff5b30a3d4240..87a92238d25ba 100644
> --- a/tools/perf/util/dso.c
> +++ b/tools/perf/util/dso.c
[ ... ]
> @@ -552,13 +539,13 @@ static void dso__list_add(struct dso *dso) __must_hold(&_dso__data_open_lock)
>  	dso__data_open_cnt++;
>  }
>  
> -static void dso__list_del(struct dso *dso) __must_hold(&_dso__data_open_lock)
> +static void dso__list_del(struct dso *dso) __must_hold(&dso__data_open_lock)
>  {
>  	list_del_init(&dso__data(dso)->open_entry);
>  #ifdef REFCNT_CHECKING
> -	mutex_unlock(dso__data_open_lock());
> +	mutex_unlock(&dso__data_open_lock);
>  	dso__put(dso__data(dso)->dso);
> -	mutex_lock(dso__data_open_lock());
> +	mutex_lock(&dso__data_open_lock);
>  #endif

[Severity: High]
Does dropping the lock before decrementing the count open a window for an
out-of-bounds access?

In dso__list_del(), the entry is removed from the list via list_del_init()
before dso__data_open_lock is dropped under REFCNT_CHECKING. However,
dso__data_open_cnt is not decremented until after the lock is reacquired.

If the list had exactly one element, it is now empty, but the count remains
positive during the unlocked window.

If another thread calls do_open() and hits EMFILE during this window, it
checks dso__data_open_cnt. Seeing a positive count, it will call
close_first_dso(), which blindly executes list_first_entry() on the empty
dso__data_open list. This results in an invalid pointer and subsequent memory
corruption.

>  	WARN_ONCE(dso__data_open_cnt <= 0,
>  		  "DSO data fd counter out of bounds.");
> @@ -567,7 +554,7 @@ static void dso__list_del(struct dso *dso) __must_hold(&_dso__data_open_lock)
>  
>  static void close_first_dso(void);
>  
> -static int do_open(char *name) __must_hold(&_dso__data_open_lock)
> +static int do_open(char *name) __must_hold(&dso__data_open_lock)
>  {
>  	do {
>  		int fd = open(name, O_RDONLY|O_CLOEXEC);
[ ... ]
> @@ -670,7 +657,7 @@ void dso__put_filename(struct dso *dso __maybe_unused, char *filename, bool deco
>  }
>  
>  static int __open_dso(struct dso *dso, struct machine *machine)
> -	__must_hold(&_dso__data_open_lock)
> +	__must_hold(&dso__data_open_lock)
>  {
>  	int fd = -EINVAL;
>  	char *name;
> @@ -703,7 +690,7 @@ static void check_data_close(void);
>   * list/count of open DSO objects.
>   */
>  static int open_dso(struct dso *dso, struct machine *machine)
> -	__must_hold(&_dso__data_open_lock)
> +	__must_hold(&dso__data_open_lock)

[Severity: High]
Could this sequence lead to an AB-BA lock inversion deadlock between
dso__data_open_lock and dso->lock?

When a thread opens a DSO via __open_dso(), it first acquires dso->lock.
If it calls do_open() and hits the open file limit (EMFILE), do_open() calls
close_first_dso() to evict an older DSO.

This leads to dso__list_del() being called for the evicted DSO. As seen
above, under REFCNT_CHECKING, dso__list_del() temporarily drops the global
dso__data_open_lock and calls dso__put(). At this moment, the current
thread still holds dso->lock for the DSO it was trying to open.

A concurrent thread can now acquire the uncontended dso__data_open_lock,
attempt to open the same DSO the first thread is processing, and block
waiting for dso->lock. When the first thread finishes dso__put(), it will
attempt to reacquire dso__data_open_lock, creating a classic AB-BA deadlock.

[ ... ]
> diff --git a/tools/perf/util/vdso.c b/tools/perf/util/vdso.c
> index c12f5d8c4bf69..862ca172ab59a 100644
> --- a/tools/perf/util/vdso.c
> +++ b/tools/perf/util/vdso.c
> @@ -131,7 +131,7 @@ static struct dso *__machine__addnew_vdso(struct machine *machine, const char *s
>  
>  	dso = dso__new(short_name);
>  	if (dso != NULL) {
> -		__dsos__add(&machine->dsos, dso);
> +		dsos__add(&machine->dsos, dso);
>  		dso__set_long_name(dso, long_name, false);
>  	}
>  

[Severity: Medium]
Does this missing double-check create a TOCTOU race that allows duplicate
VDSO entries to be inserted?

In machine__findnew_vdso(), there is an initial read-locked lookup via
dsos__find(). If the DSO is not found, it calls __machine__addnew_vdso()
which allocates a new DSO and delegates to dsos__add().

Because dsos__add() acquires the write lock and blindly appends the DSO
without verifying if another thread already inserted it, concurrent
threads calling map__new() can race. They can both miss the initial
read-locked lookup, both allocate new DSO objects, and both insert them.

This violates the unique DSO invariant and leaks the memory allocated
for the duplicate VDSO dso.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261013182458.1234616-1-irogers@google.com?part=7

  reply	other threads:[~2026-10-13 18:41 UTC|newest]

Thread overview: 52+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-13 18:24 [PATCH v1 00/10] tools/perf: Adopt kernel compiler-context-analysis.h annotations Ian Rogers
2026-10-13 18:24 ` [PATCH v1 01/10] compiler-context-analysis: Avoid unused parameter warning in _context_unsafe_alias() Ian Rogers
2026-10-13 18:30   ` sashiko-bot
2026-10-13 19:44   ` Bart Van Assche
2026-10-13 22:10     ` Ian Rogers
2026-10-13 22:14       ` Bart Van Assche
2026-10-13 22:26         ` Ian Rogers
2026-10-13 22:47     ` Bart Van Assche
2026-10-13 22:59       ` Ian Rogers
2026-10-13 18:24 ` [PATCH v1 02/10] tools headers compiler: Synchronize compiler_types.h with the kernel sources Ian Rogers
2026-10-13 18:33   ` sashiko-bot
2026-10-13 18:24 ` [PATCH v1 03/10] tools headers: Synchronize linux/compiler-context-analysis.h " Ian Rogers
2026-10-13 18:34   ` sashiko-bot
2026-10-13 18:24 ` [PATCH v1 04/10] perf mutex: Use kernel style compiler-context-analysis.h annotations Ian Rogers
2026-10-13 18:36   ` sashiko-bot
2026-10-13 18:24 ` [PATCH v1 05/10] perf build: Enable additional Clang thread-safety flags Ian Rogers
2026-10-13 18:35   ` sashiko-bot
2026-10-13 18:24 ` [PATCH v1 06/10] perf comm: Add __guarded_by annotations to struct comm_strs Ian Rogers
2026-10-13 18:35   ` sashiko-bot
2026-10-13 18:24 ` [PATCH v1 07/10] perf dso: Fix unlocked __dsos__add() in vdso and expand lock annotations Ian Rogers
2026-10-13 18:41   ` sashiko-bot [this message]
2026-10-13 18:24 ` [PATCH v1 08/10] perf thread: Expand context analysis annotations to threads and namespaces_lock Ian Rogers
2026-10-13 18:35   ` sashiko-bot
2026-10-13 18:24 ` [PATCH v1 09/10] perf config, intel-tpebs, env: Add __guarded_by and __must_hold annotations Ian Rogers
2026-10-13 18:42   ` sashiko-bot
2026-10-13 18:24 ` [PATCH v1 10/10] perf maps: Add context analysis annotations Ian Rogers
2026-10-13 18:43   ` sashiko-bot
2026-10-13 22:26 ` [PATCH v2 00/10] tools/perf: Adopt kernel compiler-context-analysis.h annotations Ian Rogers
2026-10-13 22:26   ` [PATCH v2 01/10] compiler-context-analysis: Avoid unused parameter warning in _context_unsafe_alias() Ian Rogers
2026-10-13 22:30     ` sashiko-bot
2026-10-13 22:26   ` [PATCH v2 02/10] tools headers compiler: Synchronize compiler_types.h with the kernel sources Ian Rogers
2026-10-13 22:36     ` sashiko-bot
2026-10-13 22:26   ` [PATCH v2 03/10] tools headers: Synchronize linux/compiler-context-analysis.h " Ian Rogers
2026-10-13 22:34     ` sashiko-bot
2026-10-13 22:26   ` [PATCH v2 04/10] perf mutex: Use kernel style compiler-context-analysis.h annotations Ian Rogers
2026-10-13 22:42     ` sashiko-bot
2026-10-13 22:26   ` [PATCH v2 05/10] perf build: Enable additional Clang thread-safety flags Ian Rogers
2026-10-13 22:34     ` sashiko-bot
2026-10-13 22:26   ` [PATCH v2 06/10] perf comm: Add __guarded_by annotations to struct comm_strs Ian Rogers
2026-10-13 22:36     ` sashiko-bot
2026-10-13 22:26   ` [PATCH v2 07/10] perf dso: Fix unlocked __dsos__add() in vdso and expand lock annotations Ian Rogers
2026-10-13 22:43     ` sashiko-bot
2026-10-13 22:26   ` [PATCH v2 08/10] perf thread: Expand context analysis annotations to threads and namespaces_lock Ian Rogers
2026-10-13 22:36     ` sashiko-bot
2026-10-13 22:26   ` [PATCH v2 09/10] perf config, intel-tpebs, env: Add __guarded_by and __must_hold annotations Ian Rogers
2026-10-13 22:41     ` sashiko-bot
2026-10-13 22:26   ` [PATCH v2 10/10] perf maps: Add context analysis annotations Ian Rogers
2026-10-13 22:38     ` sashiko-bot
2026-10-13 22:52   ` [PATCH v2 00/10] tools/perf: Adopt kernel compiler-context-analysis.h annotations Bart Van Assche
2026-10-13 23:09     ` Ian Rogers
2026-10-13 23:18       ` Bart Van Assche
2026-10-13 23:26         ` Ian Rogers

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=sashiko-outbox-170345@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=irogers@google.com \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.