From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.71 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1792700140; b=XCu32heowdgNeAdudYPjHGLlkWcsjp9apTZ0W9khS0UmOC9xIu0RB5nGe5q+2qDPVYEX JxEz6FRDxFiXmPnmm+S67xxMVLtbuTgQ7pMf1VC3r+NmR5bxh4bku16PSnUBsUeDO9daG 7fz2PAMB1ynKKcdoOW+vX8o3/xpMVKUIkTstcH84V2ybIRgjs/d5N6T5/g90l9+2Co1VE jwrng30p8HoxwBN6sNaLkY69kvOIXb35UjT3B8K4NXMvtkoQtlDRL5rq/Ib1ZY3A85qQ5 6HZs+rZC4pb0ygH4CkL4B19AD8z1W4Yj/m3qheX2qgzHTs+oTaRi8jKlqP6f/pI2Hlw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1792700140; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=e89dsJnrgTqWNie6z3l9SkAKLW3jt2Md0h+tKjeIm1s=; b=eIrpLJW/S9Y8PgmfJR13z67DW7IL0fKTtsjhTp3nsBy6rwHmLKr1J2iNDVVzHuPqHDkX rB3GIWEDmMh8AyJz906825h5Vh6s/gTo+Psgc1WC5Wv/spNoYxEdXMqJ+b0Qh5UcwkqVi KVt0eyTeHv/SzK2TMWikWdsonbli11zlCF5/n7J9kqpbpFlcBujmjfscuskPgH2Mc5TRs 4PAVWe0hnjhCHvNjbnblMSqIe9ENrrxRN7ahDabmlB4OsMc8sCgLkUN+0EdhEw6haLz7A nmGNNNUxI1WTQA29s9T/gmm3U88+YOEcOUNB/4wDQfzZOpqjcSIF7epEBZGKjiZ4MKA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20251104 header.b=iBVxrXYv; arc=none smtp.remote-ip=74.125.82.71 Received: from mail-dl1-f71.google.com (mail-dl1-f71.google.com [74.125.82.71]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 218FA60A380B for ; Thu, 22 Oct 2026 20:15:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 218FA60A380B 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, unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20251104 header.b=iBVxrXYv Received: by mail-dl1-f71.google.com with SMTP id a92af1059eb24-14373747468so134717c88.1 for ; Thu, 22 Oct 2026 13:15:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1792700139; x=1793304939; darn=lists.linux.dev; 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=e89dsJnrgTqWNie6z3l9SkAKLW3jt2Md0h+tKjeIm1s=; b=iBVxrXYvY5o/HiwU825HZo9bUzPGBq82EelTLCIpxopP8TryWxXWi4ckUXu5yAPGbL v+5GZ1WtVQDiKKIxWWtEgxG2h9hh4lPNZm3oJvlu3f3/VrljPv0WmJ8gAj/nn4Mp56JI NwFXTjzNl3kexg2/ZX9RbwXx5bJjre3Bm9Nd0cvNqX3f8rhySrd77zFICyEJBvZjHy4w r4Yfat53z5vGR4oGvOWkt8dXh79A9ah39tvCLNIHSSKAz3bxK4rwkHaBfPUe+rDFD1R/ nw67vQvBLjkVHo6WaILhagnATDxVlQZEC0bLD4ZV4kJo7Gx5iD85uYB063hh2Y5mZnAo hWbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1792700139; x=1793304939; 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=e89dsJnrgTqWNie6z3l9SkAKLW3jt2Md0h+tKjeIm1s=; b=WRBVLn7DxRqcJmzoZqn+WqvDkO/x0qmmghRPk5SbcD1H5r/BJWozR7iaSHw9VID/Wh DSwds21bg2cW41kmwHHT8bMell2Ggi270Z7IF3bD+8SQNNFCfZJ5HHjofys8Xg4HzoJr yJvHa9RpmjpaCCfcHw8rGGkSV+gxcy6aIZI7CHMEbAOpzoBj+WF3NfJAveK08JLZWu4p 7+dZkcdJI8c363o/rMfW++su6M7qcbupA0BSUFKqFDXT00EFbkaI1stUqQZhTk1XZWtC Y21KGqIumtUcNefGA4+s9cNr4QpOEjT+7GJfFnWibyp+X59Nwu7rPkCbCmxXbLNXfEnD CEvg== X-Forwarded-Encrypted: i=1; AKwUvBwlis6aTbZGwZ3YS7G0Yaow3BuWuPc6wxyWjCONJNWqzif5XV20IuNajmcKdCJnQ18YTxyX@lists.linux.dev X-Gm-Message-State: AFq9FYIKEn5LIBBNc3aPvlkJQm/icYnzowx79bFGclLO/PZmMuKGEtEN //yPr6Ne/I37kYF45WRkxq9n8Hj2oQb5CkYjMk4H7HLheWhGfM286StO9l6FgPFjwX9i04/bO/c js41Mz9nmlg== X-Received: from dlbvv8.prod.google.com ([2002:a05:7022:5f08:b0:191:3c0c:1269]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:220b:b0:187:eb7f:b8f with SMTP id a92af1059eb24-195c4d60aadmr425813c88.13.1792700138931; Thu, 22 Oct 2026 13:15:38 -0700 (PDT) Date: Thu, 22 Oct 2026 13:14:46 -0700 In-Reply-To: <20261022201453.1717523-1-irogers@google.com> X-Mailing-List: llvm@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261021223905.715051-1-irogers@google.com> <20261022201453.1717523-1-irogers@google.com> X-Mailer: git-send-email 2.56.0.307.g5fe7bd680c-goog Message-ID: <20261022201453.1717523-14-irogers@google.com> Subject: [PATCH v8 13/19] perf dso: Expand context analysis annotations From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org Cc: adrian.hunter@intel.com, elver@google.com, 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 struct dsos fields with __guarded_by(&lock), annotate dso__data_open and fd_limit in util/dso.c with __guarded_by(&dso__data_open_lock), and tighten __dsos__findnew_id() and __dsos__addnew_id() from shared to exclusive lock requirements. Signed-off-by: Ian Rogers --- tools/perf/tests/dso-data.c | 4 ++++ tools/perf/util/dso.c | 14 +++++++++----- tools/perf/util/dsos.c | 10 ++++++++-- tools/perf/util/dsos.h | 13 +++++++------ tools/perf/util/vdso.c | 1 + 5 files changed, 29 insertions(+), 13 deletions(-) diff --git a/tools/perf/tests/dso-data.c b/tools/perf/tests/dso-data.c index 18b7052a2dc5..6ef59929e6c0 100644 --- a/tools/perf/tests/dso-data.c +++ b/tools/perf/tests/dso-data.c @@ -116,12 +116,14 @@ static int dso__data_fd(struct dso *dso, struct machine *machine) static void dsos__delete(struct dsos *dsos) { + down_write(&dsos->lock); for (unsigned int i = 0; i < dsos->cnt; i++) { struct dso *dso = dsos->dsos[i]; dso__data_close(dso); unlink(dso__name(dso)); } + up_write(&dsos->lock); dsos__exit(dsos); } @@ -254,6 +256,7 @@ static int set_fd_limit(int n) } static int test__dso_data_cache(struct test_suite *test __maybe_unused, int subtest __maybe_unused) + __context_unsafe(/* single-threaded test */) { struct machine machine; long nr_end, nr = open_files_cnt(); @@ -324,6 +327,7 @@ static long new_limit(int count) } static int test__dso_data_reopen(struct test_suite *test __maybe_unused, int subtest __maybe_unused) + __context_unsafe(/* single-threaded test */) { struct machine machine; long nr_end, nr = open_files_cnt(), lim = new_limit(3); diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c index 4dc518f7cb7d..5f4b4cd9a089 100644 --- a/tools/perf/util/dso.c +++ b/tools/perf/util/dso.c @@ -525,7 +525,8 @@ void dso__set_module_info(struct dso *dso, struct kmod_path *m, * Global list of open DSOs and the counter. */ DEFINE_MUTEX(dso__data_open_lock); -static LIST_HEAD(dso__data_open); +static struct list_head dso__data_open __guarded_by(&dso__data_open_lock) = + LIST_HEAD_INIT(dso__data_open); static long dso__data_open_cnt __guarded_by(&dso__data_open_lock); static void dso__list_add(struct dso *dso) __must_hold(&dso__data_open_lock) @@ -757,7 +758,7 @@ static rlim_t get_fd_limit(void) return limit; } -static rlim_t fd_limit; +static rlim_t fd_limit __guarded_by(&dso__data_open_lock); /* * Used only by tests/dso-data.c to reset the environment @@ -766,7 +767,8 @@ static rlim_t fd_limit; */ void reset_fd_limit(void) { - fd_limit = 0; + /* Only called by single-threaded test setup. */ + context_unsafe(fd_limit = 0); } static bool may_cache_fd(void) __must_hold(&dso__data_open_lock) @@ -1723,8 +1725,10 @@ void __dso__improve_id(struct dso *dso, const struct dso_id *id) dso_id->build_id = id->build_id; changed = true; } - if (changed && dsos) - dsos->sorted = false; + if (changed && dsos) { + /* dsos write lock held by caller. */ + context_unsafe(dsos->sorted = false); + } } int dso_id__cmp(const struct dso_id *a, const struct dso_id *b) diff --git a/tools/perf/util/dsos.c b/tools/perf/util/dsos.c index ff58ac2566cf..7f3a6dabf8a7 100644 --- a/tools/perf/util/dsos.c +++ b/tools/perf/util/dsos.c @@ -12,6 +12,7 @@ #include void dsos__init(struct dsos *dsos) + __context_unsafe(/* initialization */) { init_rwsem(&dsos->lock); @@ -50,6 +51,7 @@ void dsos__exit(struct dsos *dsos) static int __dsos__for_each_dso(struct dsos *dsos, int (*cb)(struct dso *dso, void *data), void *data) + __must_hold_shared(&dsos->lock) { for (unsigned int i = 0; i < dsos->cnt; i++) { struct dso *dso = dsos->dsos[i]; @@ -182,7 +184,8 @@ static struct dso *__dsos__find_by_longname_id(struct dsos *dsos, } qsort(dsos->dsos, dsos->cnt, sizeof(struct dso *), dsos__cmp_long_name_id_short_name); - dsos->sorted = true; + /* write_locked is true, so dsos->lock is held exclusively. */ + context_unsafe(dsos->sorted = true); } res = bsearch(&key, dsos->dsos, dsos->cnt, sizeof(struct dso *), @@ -194,6 +197,7 @@ static struct dso *__dsos__find_by_longname_id(struct dsos *dsos, } int __dsos__add(struct dsos *dsos, struct dso *dso) + __must_hold(&dsos->lock) { if (!dso) return -EINVAL; @@ -285,6 +289,7 @@ static struct dso *__dsos__find_id(struct dsos *dsos, const char *name, const st } struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short, bool write_locked) + __must_hold_shared(&dsos->lock) { return __dsos__find_id(dsos, name, &dso_id_empty, cmp_short, write_locked); } @@ -319,6 +324,7 @@ static void dso__set_basename(struct dso *dso) } static struct dso *__dsos__addnew_id(struct dsos *dsos, const char *name, const struct dso_id *id) + __must_hold(&dsos->lock) { struct dso *dso = dso__new_id(name, id); @@ -335,7 +341,7 @@ static struct dso *__dsos__addnew_id(struct dsos *dsos, const char *name, const } static struct dso *__dsos__findnew_id(struct dsos *dsos, const char *name, const struct dso_id *id) - __must_hold_shared(&dsos->lock) + __must_hold(&dsos->lock) { struct dso *dso = __dsos__find_id(dsos, name, id, false, /*write_locked=*/true); diff --git a/tools/perf/util/dsos.h b/tools/perf/util/dsos.h index 857a79fd8805..cc7e3a42f9cb 100644 --- a/tools/perf/util/dsos.h +++ b/tools/perf/util/dsos.h @@ -19,18 +19,19 @@ struct machine; */ struct dsos { struct rw_semaphore lock; - struct dso **dsos; - unsigned int cnt; - unsigned int allocated; - bool sorted; + struct dso **dsos __pt_guarded_by(&lock) __guarded_by(&lock); + unsigned int cnt __guarded_by(&lock); + unsigned int allocated __guarded_by(&lock); + bool sorted __guarded_by(&lock); }; void dsos__init(struct dsos *dsos); void dsos__exit(struct dsos *dsos); -int __dsos__add(struct dsos *dsos, struct dso *dso); +int __dsos__add(struct dsos *dsos, struct dso *dso) __must_hold(&dsos->lock); int dsos__add(struct dsos *dsos, struct dso *dso); -struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short, bool write_locked); +struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short, bool write_locked) + __must_hold_shared(&dsos->lock); struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short); struct dso *dsos__findnew_id(struct dsos *dsos, const char *name, const struct dso_id *id); diff --git a/tools/perf/util/vdso.c b/tools/perf/util/vdso.c index 93828e660672..fc75c0cf372f 100644 --- a/tools/perf/util/vdso.c +++ b/tools/perf/util/vdso.c @@ -126,6 +126,7 @@ void machine__exit_vdso(struct machine *machine) static struct dso *__machine__addnew_vdso(struct machine *machine, const char *short_name, const char *long_name) + __must_hold(&machine->dsos.lock) { struct dso *dso; -- 2.56.0.307.g5fe7bd680c-goog