From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-dy1-f197.google.com (mail-dy1-f197.google.com [74.125.82.197]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B005156260A for ; Tue, 13 Oct 2026 22:26:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.197 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791930420; cv=none; b=B9KUTrKlL0uQ1O71IzM7c2Ixr0cmJEb3Ph9YqpXxpsDhaD/NZSqQqvg62/DsoAg1wXZPb76c9ziAb33KsWFraiosKLgFLCdw9K4Eu0dACYHvh95NS8DT/JKNLpH7YGEZaUyHd9ptdKW0YLyiPF/5u+uz+sPv//hDWw4xQStNrQA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791930420; c=relaxed/simple; bh=7US1K91tc/xAY0auUwgzNKlEZEip4CvIibxK9/lCxAI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=oYL2ca1FUMGGUcSdzuSuLkp7GdvaK6DUiNoPYaMUF2gSWXfJzsj6Je5ReQ3rT1uWzM/DesVDp3vE3yjgbM0C1jz5YT0KwaxDcVETGWaepGyN5+3T059IAwA98GCN7qwtQpx5UZ3MtfIGKyeeqX1Sk1cgXiwNmgTIye+aBRf0vkM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=W6aTDBaZ; arc=none smtp.client-ip=74.125.82.197 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="W6aTDBaZ" Received: by mail-dy1-f197.google.com with SMTP id 5a478bee46e88-33c35f5ca6cso7811517eec.1 for ; Tue, 13 Oct 2026 15:26:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1791930418; x=1792535218; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=Eg1RxKBzAbpEvTU/GlXM2uZlQTXEQK/Sj0ELzD775os=; b=W6aTDBaZnl5AM+ENm4Lt1ess0GszMWNjlfMHqAE35pLDnZmf+loG7XLJEB7q80EHYo +BH4p/lZWlg9xa2MiD6Cu/WYLFjECAyBiSD5OvIgs8olOdFe0p+6gPdI8X63KNJzTqub cjsGLzzw8+pKIyKhJ4BZLbrtbZTchaG99UEYYz6UHN4tJ84N5KAAONrmBtCmTTApWKPU Jxx8EIWB3dyriIPl6uJJ2/vTApXhFqswQ4Y22Uq0ueWD8Oyv9z+uDSRJe2OkEaE8Chzf QId4/p2kQfnCZodefBtChXPNDcibMOh8whj5RJkMZiuAv4pavrkj38JDABcMU2NdPN0s 1zMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1791930418; x=1792535218; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=Eg1RxKBzAbpEvTU/GlXM2uZlQTXEQK/Sj0ELzD775os=; b=iT2DSxIKEeEU6EdGtORsF0/iNW4SXQPhrHUNeWErBPr0lnqVVHB2pz5uquLLUz+aBW /dagyWwZj1QaOMSG6xfpYYUZwRzyZfkB4oWNUmP4jxVm30hDC460L6Qm4nJkEBAGU5cS uahwt9MxElN4JTb0jkTSPF6NTER995G2dGRnyvoXuk/MZOCzwUxMVy1nTvudUq/qGpjv 7gMzQbWpinwfi/Rm1OyH/sBS0pb8oepY4ZDUH0+CTMo3pZ7cyvgzEKXUWYaIn+AGXAsx FoGu9ax0D8tEf2eKNRUAPVVFHudGn7OQ8DNwAn7ExylbFVCHZMGhL0Cs6knbM0VLH2NB zXDA== X-Forwarded-Encrypted: i=1; AKwUvByog3kNxFjNHnjD2JY4wh2AUsA7Vwlf06MSXoYiyQ6K9cVfCI4m1ZpT5HD5ZqqElxi5gpi89xinswVhDlW4Rdy4@vger.kernel.org X-Gm-Message-State: AFq9FYJiX+Ej0OOwpaoYKedchFvnu+tbNul+6MIldf5opNfMQBo+f8Pb h1P7ntvL14lFXk0Tc25leqaV2dci/71peuluaJJNx6qu/MTtxztqesZy2JI1HNyucTxpIewsEKV Mve9leTFuMw== X-Received: from dyr7.prod.google.com ([2002:a05:693c:4007:b0:359:72f9:2aab]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7301:1a3:b0:35b:7dcd:dd57 with SMTP id 5a478bee46e88-35b7ddd267emr6892890eec.10.1791930416351; Tue, 13 Oct 2026 15:26:56 -0700 (PDT) Date: Tue, 13 Oct 2026 15:26:20 -0700 In-Reply-To: <20261013222622.1822619-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-perf-users@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20261013182458.1234616-1-irogers@google.com> <20261013222622.1822619-1-irogers@google.com> X-Mailer: git-send-email 2.56.0.385.gd3acb90ef8-goog Message-ID: <20261013222622.1822619-9-irogers@google.com> Subject: [PATCH v2 08/10] perf thread: Expand context analysis annotations to threads and namespaces_lock From: Ian Rogers To: irogers@google.com, acme@kernel.org, elver@google.com, namhyung@kernel.org Cc: adrian.hunter@intel.com, bvanassche@acm.org, james.clark@linaro.org, jolsa@kernel.org, justinstitt@google.com, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, ljs@kernel.org, llvm@lists.linux.dev, mingo@redhat.com, morbo@google.com, nathan@kernel.org, ndesaulniers@google.com, peterz@infradead.org Content-Type: text/plain; charset="UTF-8" Annotate shard and last_match in struct threads_table_entry with __guarded_by(&lock) and add __must_hold_shared()/__must_hold() to the last_match helpers in util/threads.c. In util/thread.h and util/thread.c, add __returns_ctx_lock() to thread__namespaces_lock() and thread__comm_lock(), annotate namespaces_list and comm_list with __guarded_by(), and add __must_hold_shared()/__must_hold() to the namespaces_list helpers. Assisted-by: Antigravity:gemini-3.1-pro Signed-off-by: Ian Rogers --- tools/perf/util/thread.c | 2 ++ tools/perf/util/thread.h | 28 +++++++++++++++++----------- tools/perf/util/threads.c | 4 ++++ tools/perf/util/threads.h | 6 +++--- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c index 616d5bf87baf..af761b1fc5d5 100644 --- a/tools/perf/util/thread.c +++ b/tools/perf/util/thread.c @@ -149,6 +149,7 @@ void thread__put(struct thread *thread) } static struct namespaces *__thread__namespaces(struct thread *thread) + __must_hold_shared(thread__namespaces_lock(thread)) { if (list_empty(thread__namespaces_list(thread))) return NULL; @@ -169,6 +170,7 @@ struct namespaces *thread__namespaces(struct thread *thread) static int __thread__set_namespaces(struct thread *thread, u64 timestamp, struct perf_record_namespaces *event) + __must_hold(thread__namespaces_lock(thread)) { struct namespaces *new, *curr = __thread__namespaces(thread); diff --git a/tools/perf/util/thread.h b/tools/perf/util/thread.h index 9d0ce7742c06..a7850eebce4e 100644 --- a/tools/perf/util/thread.h +++ b/tools/perf/util/thread.h @@ -48,10 +48,10 @@ DECLARE_RC_STRUCT(thread) { bool exited; bool comm_set; int comm_len; - struct list_head namespaces_list; struct rw_semaphore namespaces_lock; - struct list_head comm_list; + struct list_head namespaces_list __guarded_by(&namespaces_lock); struct rw_semaphore comm_lock; + struct list_head comm_list __guarded_by(&comm_lock); u64 db_id; void *priv; @@ -230,30 +230,36 @@ static inline void thread__set_comm_len(struct thread *thread, int len) RC_CHK_ACCESS(thread)->comm_len = len; } -static inline struct list_head *thread__namespaces_list(struct thread *thread) +static inline struct rw_semaphore *thread__namespaces_lock(const struct thread *thread) + __returns_ctx_lock(&RC_CHK_ACCESS(thread)->namespaces_lock) { - return &RC_CHK_ACCESS(thread)->namespaces_list; + return (struct rw_semaphore *)&RC_CHK_ACCESS(thread)->namespaces_lock; } -static inline int thread__namespaces_list_empty(const struct thread *thread) +static inline struct list_head *thread__namespaces_list(struct thread *thread) + __must_hold_shared(thread__namespaces_lock(thread)) { - return list_empty(&RC_CHK_ACCESS(thread)->namespaces_list); + /* Readers hold the shared lock; writers hold the exclusive lock. */ + return context_unsafe(&RC_CHK_ACCESS(thread)->namespaces_list); } -static inline struct rw_semaphore *thread__namespaces_lock(struct thread *thread) +static inline int thread__namespaces_list_empty(const struct thread *thread) + __must_hold_shared(thread__namespaces_lock(thread)) { - return &RC_CHK_ACCESS(thread)->namespaces_lock; + return list_empty(&RC_CHK_ACCESS(thread)->namespaces_list); } -static inline struct rw_semaphore *thread__comm_lock(struct thread *thread) +static inline struct rw_semaphore *thread__comm_lock(const struct thread *thread) + __returns_ctx_lock(&RC_CHK_ACCESS(thread)->comm_lock) { - return &RC_CHK_ACCESS(thread)->comm_lock; + return (struct rw_semaphore *)&RC_CHK_ACCESS(thread)->comm_lock; } static inline struct list_head *thread__comm_list(struct thread *thread) __must_hold_shared(thread__comm_lock(thread)) { - return &RC_CHK_ACCESS(thread)->comm_list; + /* Readers hold the shared lock; writers hold the exclusive lock. */ + return context_unsafe(&RC_CHK_ACCESS(thread)->comm_list); } static inline u64 thread__db_id(const struct thread *thread) diff --git a/tools/perf/util/threads.c b/tools/perf/util/threads.c index 6ca0b178fb6c..39b3c420635a 100644 --- a/tools/perf/util/threads.c +++ b/tools/perf/util/threads.c @@ -21,6 +21,7 @@ static bool key_equal(long key1, long key2, void *ctx __maybe_unused) } void threads__init(struct threads *threads) + __context_unsafe(/* initialization */) { for (int i = 0; i < THREADS__TABLE_SIZE; i++) { struct threads_table_entry *table = &threads->table[i]; @@ -32,6 +33,7 @@ void threads__init(struct threads *threads) } void threads__exit(struct threads *threads) + __context_unsafe(/* teardown */) { threads__remove_all_threads(threads); for (int i = 0; i < THREADS__TABLE_SIZE; i++) { @@ -63,6 +65,7 @@ size_t threads__nr(struct threads *threads) */ static struct thread *__threads_table_entry__get_last_match(struct threads_table_entry *table, pid_t tid) + __must_hold_shared(&table->lock) { struct thread *th, *res = NULL; @@ -76,6 +79,7 @@ static struct thread *__threads_table_entry__get_last_match(struct threads_table static void __threads_table_entry__set_last_match(struct threads_table_entry *table, struct thread *th) + __must_hold(&table->lock) { thread__put(table->last_match); table->last_match = thread__get(th); diff --git a/tools/perf/util/threads.h b/tools/perf/util/threads.h index da68d2223f18..54fba16657db 100644 --- a/tools/perf/util/threads.h +++ b/tools/perf/util/threads.h @@ -11,10 +11,10 @@ struct thread; #define THREADS__TABLE_SIZE (1 << THREADS__TABLE_BITS) struct threads_table_entry { - /* Key is tid, value is struct thread. */ - struct hashmap shard; struct rw_semaphore lock; - struct thread *last_match; + /* Key is tid, value is struct thread. */ + struct hashmap shard __guarded_by(&lock); + struct thread *last_match __guarded_by(&lock); }; struct threads { -- 2.56.0.385.gd3acb90ef8-goog