From nobody Wed Dec 17 12:12:45 2025 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (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 D9D6319ABAB for ; Tue, 18 Mar 2025 04:31:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742272319; cv=none; b=G/M9V3hYRY5qrh9hclllT8SNOdiH+kDTLIxSVI1XtytpxF/cLIOYhxE3pPq6OHdqFNWAWb8xQGafbBFJluzzMNWThEounDWktHc5X9vSv7KoKnz2YICZA76luNYoAMsHop+SbkKsDt8uwGiRnwRZuRwmtyNR6CZK5YBKp+FLUtk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742272319; c=relaxed/simple; bh=R9Xg6/Ngqi7cKsMGCB9U1IMDEdObQAmq0QYpQz3ckv0=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=qSBD5wG2XXVgBa6VVZKhfvbdzNgmyBq/AX9ij/9fjblft931u1XCoaE+WMa/8FTt7E0y2aM6iQrm56Up1ee1vO7O/rzDMSwxdzxwAaz6SIKT9eHrJ4RzFHQoTIG1J5TN+CDwWxqszQvIlWWVHR+nKaac3H06hSykLKsMtcwIXwQ= 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=sjg+fDHv; arc=none smtp.client-ip=209.85.219.201 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="sjg+fDHv" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e63458181eeso8195286276.0 for ; Mon, 17 Mar 2025 21:31:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1742272317; x=1742877117; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=IsSrnV0KmyUDFf76MOHDYiCKaXGAeH1pZZ/Ur+aDjTE=; b=sjg+fDHvjvQIY9HiBYOWJjK7dS2bM7G2F/oQkfE4fItu+N2Xdq1LljzC2z0gFDC67U oI1LcRMyFEFSeAYa3UGsSSTmWgW4uKrkzAPA1AIgupQVdoTNFMMDHsJmWclZfK7urxU6 F3XT5+6T0ICX/nyDyUqL21cKOuugxakQ/RefYbcP2xRB48zS1eAqFuheXI+rlKBoZabj B3u1Gh/4tqnFwP2x/ASXDja05auhmts/PxXLxCwrV0j4Ud5Uw+gtRaDN7oZRgBo5XTBu SLKlMTN1byTjP/CN49EY7yFlyYKWlNmeJ3fgXpPWBkA8k7hIFukSyLfgrmptAuzvJHwU 3G4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742272317; x=1742877117; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=IsSrnV0KmyUDFf76MOHDYiCKaXGAeH1pZZ/Ur+aDjTE=; b=FdgVoyovEdxeCHWPmn6BupVG9P38SyTtH2SkSfeVxXcJ4hTkUC34jrhK3mI6xIXt33 9pWlhs05jSMZVq7/aUwGxnfLbtsDlmlf3Ij5SSYNjCb6FlnSp04heb5nup3yXihe6P4Q KA40sPuAcThODZNQMpL4zOKAdKCEMklwJ+Oz1CiCmF2dTz5jVW4Ti9wPd/6blSCFN7/R m4NCdD7FxH05jy2gJebRPegHU7HVrKGe6262d6Bk2DLD7e/uPJ5eEYYNGq/pmgoEz5ts CXI2rfjHRRt6rvLuy3InDujUIY66QQSs5L+wTv53KWrwSq+hdf4sTnUJf5RaR1xdcWpb Ay0w== X-Forwarded-Encrypted: i=1; AJvYcCW9LrTQYCnDDntHSsxs4XtSaTeZJANZuVzvIbNKdGIjGuRpQ5Wj9UD0XIpoSLE2D0gRHZrlzhY5ghCM5a4=@vger.kernel.org X-Gm-Message-State: AOJu0YwdUfFLRN6yFhVHEfRtsoCmnzRuIrqHTurAYj2DyA3udPBaidSA 21gOYDrmkh6Tk54bZDyXB7SkClXhi7NKbPCfYsumbiH2jK1LBj5iCz+TpYJuH/CbPM669aPSsnz nHuxKGQ== X-Google-Smtp-Source: AGHT+IE9RLBPYYi01/nJ6WpnF2viapxrPRVS7wY4MLSGpo6dfqlkOjr5inF2G5LwZ0a2dZ+mLAaiBH+9FSni X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:ef29:15ce:a42a:b69f]) (user=irogers job=sendgmr) by 2002:a25:ad4b:0:b0:e60:a0b5:e57 with SMTP id 3f1490d57ef6-e63f64e8581mr7989276.3.1742272316696; Mon, 17 Mar 2025 21:31:56 -0700 (PDT) Date: Mon, 17 Mar 2025 21:31:49 -0700 In-Reply-To: <20250318043151.137973-1-irogers@google.com> Message-Id: <20250318043151.137973-2-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250318043151.137973-1-irogers@google.com> X-Mailer: git-send-email 2.49.0.rc1.451.g8f38331e32-goog Subject: [PATCH v2 1/3] perf mutex: Add annotations for LOCKS_EXCLUDED and LOCKS_RETURNED From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , Athira Rajeev , Stephen Brennan , James Clark , Yunseong Kim , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Used to annotate when locks shouldn't be held for a function or if a function returns a lock that's used by later mutex lock unlock operations. Signed-off-by: Ian Rogers --- tools/perf/util/mutex.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tools/perf/util/mutex.h b/tools/perf/util/mutex.h index 40661120cacc..62d258c71ded 100644 --- a/tools/perf/util/mutex.h +++ b/tools/perf/util/mutex.h @@ -33,6 +33,12 @@ /* Documents if a type is a lockable type. */ #define LOCKABLE __attribute__((lockable)) =20 +/* Documents a function that expects a lock not to be held prior to entry.= */ +#define LOCKS_EXCLUDED(...) __attribute__((locks_excluded(__VA_ARGS__))) + +/* Documents a function that returns a lock. */ +#define LOCK_RETURNED(x) __attribute__((lock_returned(x))) + /* Documents functions that acquire a lock in the body of a function, and = do not release it. */ #define EXCLUSIVE_LOCK_FUNCTION(...) __attribute__((exclusive_lock_functi= on(__VA_ARGS__))) =20 @@ -57,6 +63,8 @@ #define GUARDED_BY(x) #define PT_GUARDED_BY(x) #define LOCKABLE +#define LOCKS_EXCLUDED(...) +#define LOCK_RETURNED(x) #define EXCLUSIVE_LOCK_FUNCTION(...) #define UNLOCK_FUNCTION(...) #define EXCLUSIVE_TRYLOCK_FUNCTION(...) --=20 2.49.0.rc1.451.g8f38331e32-goog From nobody Wed Dec 17 12:12:45 2025 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (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 1739A1C07C4 for ; Tue, 18 Mar 2025 04:31:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742272321; cv=none; b=cT6FwnN4jH7QOzHmfDjh/mxJJdqEprRbH3+nxBV+UUnlARG3iylO7M4G6PQWj93gS+5kSjzfMRlcl2+T8xvIVrlszSTqq0QAd7Vzb8/gztvazlxViQMfxi90EEHfJRRAfVpNTKvZhJH8e5tCfzswH+IC+wVji1LBJT4xJo63Q60= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742272321; c=relaxed/simple; bh=Lo4LNHyksZC8Nvy8Ez4rvuVgU4Jy5CO7M+jBaZReBy4=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=BWZo9VEopKdIBW0g8MmyTpQlN/SxnSY3xu0QbO6Po9FnE9VJ9QDWebHwVmN+EkXt/lOjojH2IG0NM4Xbo4QyZww6XdQ3Io84fccypVzkiAWY4xGOLn/yBhzMDZfT9mNOm9HNS8IJlMK7afh7GCwbaTj+BTdPmzwVzYXorxVp72E= 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=sjA1IO82; arc=none smtp.client-ip=209.85.128.201 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="sjA1IO82" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6fee461837cso73613997b3.1 for ; Mon, 17 Mar 2025 21:31:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1742272319; x=1742877119; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=buUfiIeN4odzNdnVDdtT1UchpiIGiz11qbZtpIeUEUI=; b=sjA1IO82cCusKxODlBsm3dl3Ta/CyqwbimsfhqKVovfkh6W+pf6mzVUc6So5YkpwIY A/oz5F8hauUYGc28qYclJTWoF3tNrmzH53vLbd3NrU7m4iyTICSXgny12/Fa0c7Q6DMC dWJwy4kW315htZwmxzNJ2c0cOfls9uFXR+pnDOwApn9dP2nD75TP7fg+Mm0leaCUZjBf z+1LMmA7WXB1dX5iHd/c/Qhyr+pnT9bjadzAsp623J5EBWM8SjrPsFbl/AuTvlDQi/DC qY3qPU3N3Hu2R4gMR36013HQ86qEMTooo92FekRNQVTV+u0lq5myBg6Q7aqqTD/3+Uxo cIag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742272319; x=1742877119; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=buUfiIeN4odzNdnVDdtT1UchpiIGiz11qbZtpIeUEUI=; b=iWbKGs4fWCqtEaQ5ka9ZiRdwOpdSCFGre2/vJADEJbeOYEum9EScf07ZTDnEmKwktJ E7hO4mC0gYIVPbAdY72a12nY2BvneSxhTuFx4Ym1ae4xi2RJZA/8QqdC+IM57bl8CyPv sJ+ySDyfwVFxU0DVWuMubXxZCUDT7cpkKwIokDDK3YwiX1jqxAKgxTv/L16e10iDifDx EUf6v17vb1E01SE4vVuY2X/FcXQUqzl47EHY+xfCsnogGfcUrnKba93vAd5zrqKbOq+P iR7h05hkBPyefXHxpHU//feX73EOmzGCmTY4Bpkj8f4PRbutn+wjqky98Z8XK4MRVey0 KFXA== X-Forwarded-Encrypted: i=1; AJvYcCUtztV/kwMvcO1gn5U/jHrYiMo9ZLk2Ra5yYRr8rKx1CRsk2x7ehGsB+46Ol9cNb7YqyMBNb0qgY7rKuMA=@vger.kernel.org X-Gm-Message-State: AOJu0Yx1JPTQbeV2tZ3SmKYVmNNQN2alyW31q4I6WH/1ICLkSrVaem0M 0TtUw0l0TKMoI7FbJ5RgZE6TTB4ofR4x0N0UAAU2Fj7anCrPOnsaesF4/kaV7XDYRCtZIyKIH27 f8vpRFg== X-Google-Smtp-Source: AGHT+IHt0q0AMpyCBE1YLeztKe3QCXP+N1dVHHSbhX67GXrd/iWlfxYQPvguy3gsTKVEfi2oC9xOb5X5Y1Pp X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:ef29:15ce:a42a:b69f]) (user=irogers job=sendgmr) by 2002:a05:690c:2c8b:b0:6f9:98c3:a191 with SMTP id 00721157ae682-6ff45edf246mr71587b3.2.1742272319011; Mon, 17 Mar 2025 21:31:59 -0700 (PDT) Date: Mon, 17 Mar 2025 21:31:50 -0700 In-Reply-To: <20250318043151.137973-1-irogers@google.com> Message-Id: <20250318043151.137973-3-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250318043151.137973-1-irogers@google.com> X-Mailer: git-send-email 2.49.0.rc1.451.g8f38331e32-goog Subject: [PATCH v2 2/3] perf dso: Use lock annotations to fix asan deadlock From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , Athira Rajeev , Stephen Brennan , James Clark , Yunseong Kim , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" dso__list_del with address sanitizer and/or reference count checking will call dso__put that can call dso__data_close reentrantly trying to lock the dso__data_open_lock and deadlocking. Switch from pthread mutexes to perf's mutex so that lock checking is performed in debug builds. Add lock annotations that diagnosed the problem. Release the dso__data_open_lock around the dso__put to avoid the deadlock. Change the declaration of dso__data_get_fd to return a boolean, indicating the fd is valid and the lock is held, to make it compatible with the thread safety annotations as a try lock. Signed-off-by: Ian Rogers --- tools/perf/tests/dso-data.c | 4 +- tools/perf/util/dso.c | 74 +++++++++++++++--------- tools/perf/util/dso.h | 15 +++-- tools/perf/util/unwind-libunwind-local.c | 16 ++--- 4 files changed, 66 insertions(+), 43 deletions(-) diff --git a/tools/perf/tests/dso-data.c b/tools/perf/tests/dso-data.c index 5286ae8bd2d7..06be7c5d8495 100644 --- a/tools/perf/tests/dso-data.c +++ b/tools/perf/tests/dso-data.c @@ -106,9 +106,9 @@ struct test_data_offset offsets[] =3D { /* move it from util/dso.c for compatibility */ static int dso__data_fd(struct dso *dso, struct machine *machine) { - int fd =3D dso__data_get_fd(dso, machine); + int fd =3D -1; =20 - if (fd >=3D 0) + if (dso__data_get_fd(dso, machine, &fd)) dso__data_put_fd(dso); =20 return fd; diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c index 7576e8e24838..e0111049f6b0 100644 --- a/tools/perf/util/dso.c +++ b/tools/perf/util/dso.c @@ -493,11 +493,25 @@ void dso__set_module_info(struct dso *dso, struct kmo= d_path *m, /* * Global list of open DSOs and the counter. */ +struct mutex _dso__data_open_lock; static LIST_HEAD(dso__data_open); -static long dso__data_open_cnt; -static pthread_mutex_t dso__data_open_lock =3D PTHREAD_MUTEX_INITIALIZER; +static long dso__data_open_cnt GUARDED_BY(_dso__data_open_lock); =20 -static void dso__list_add(struct dso *dso) +static void dso__data_open_lock_init(void) +{ + mutex_init(&_dso__data_open_lock); +} + +static struct mutex *dso__data_open_lock(void) LOCK_RETURNED(_dso__data_op= en_lock) +{ + static pthread_once_t data_open_lock_once =3D PTHREAD_ONCE_INIT; + + pthread_once(&data_open_lock_once, dso__data_open_lock_init); + + return &_dso__data_open_lock; +} + +static void dso__list_add(struct dso *dso) EXCLUSIVE_LOCKS_REQUIRED(_dso__= data_open_lock) { list_add_tail(&dso__data(dso)->open_entry, &dso__data_open); #ifdef REFCNT_CHECKING @@ -508,11 +522,13 @@ static void dso__list_add(struct dso *dso) dso__data_open_cnt++; } =20 -static void dso__list_del(struct dso *dso) +static void dso__list_del(struct dso *dso) EXCLUSIVE_LOCKS_REQUIRED(_dso__= data_open_lock) { list_del_init(&dso__data(dso)->open_entry); #ifdef REFCNT_CHECKING + mutex_unlock(dso__data_open_lock()); dso__put(dso__data(dso)->dso); + mutex_lock(dso__data_open_lock()); #endif WARN_ONCE(dso__data_open_cnt <=3D 0, "DSO data fd counter out of bounds."); @@ -521,7 +537,7 @@ static void dso__list_del(struct dso *dso) =20 static void close_first_dso(void); =20 -static int do_open(char *name) +static int do_open(char *name) EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lo= ck) { int fd; char sbuf[STRERR_BUFSIZE]; @@ -548,6 +564,7 @@ char *dso__filename_with_chroot(const struct dso *dso, = const char *filename) } =20 static int __open_dso(struct dso *dso, struct machine *machine) + EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock) { int fd =3D -EINVAL; char *root_dir =3D (char *)""; @@ -613,6 +630,7 @@ static void check_data_close(void); * list/count of open DSO objects. */ static int open_dso(struct dso *dso, struct machine *machine) + EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock) { int fd; struct nscookie nsc; @@ -638,7 +656,7 @@ static int open_dso(struct dso *dso, struct machine *ma= chine) return fd; } =20 -static void close_data_fd(struct dso *dso) +static void close_data_fd(struct dso *dso) EXCLUSIVE_LOCKS_REQUIRED(_dso__= data_open_lock) { if (dso__data(dso)->fd >=3D 0) { close(dso__data(dso)->fd); @@ -655,12 +673,12 @@ static void close_data_fd(struct dso *dso) * Close @dso's data file descriptor and updates * list/count of open DSO objects. */ -static void close_dso(struct dso *dso) +static void close_dso(struct dso *dso) EXCLUSIVE_LOCKS_REQUIRED(_dso__data= _open_lock) { close_data_fd(dso); } =20 -static void close_first_dso(void) +static void close_first_dso(void) EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open= _lock) { struct dso_data *dso_data; struct dso *dso; @@ -705,7 +723,7 @@ void reset_fd_limit(void) fd_limit =3D 0; } =20 -static bool may_cache_fd(void) +static bool may_cache_fd(void) EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lo= ck) { if (!fd_limit) fd_limit =3D get_fd_limit(); @@ -721,7 +739,7 @@ static bool may_cache_fd(void) * for opened dso file descriptors. The limit is half * of the RLIMIT_NOFILE files opened. */ -static void check_data_close(void) +static void check_data_close(void) EXCLUSIVE_LOCKS_REQUIRED(_dso__data_ope= n_lock) { bool cache_fd =3D may_cache_fd(); =20 @@ -737,12 +755,13 @@ static void check_data_close(void) */ void dso__data_close(struct dso *dso) { - pthread_mutex_lock(&dso__data_open_lock); + mutex_lock(dso__data_open_lock()); close_dso(dso); - pthread_mutex_unlock(&dso__data_open_lock); + mutex_unlock(dso__data_open_lock()); } =20 static void try_to_open_dso(struct dso *dso, struct machine *machine) + EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock) { enum dso_binary_type binary_type_data[] =3D { DSO_BINARY_TYPE__BUILD_ID_CACHE, @@ -784,25 +803,27 @@ static void try_to_open_dso(struct dso *dso, struct m= achine *machine) * returns file descriptor. It should be paired with * dso__data_put_fd() if it returns non-negative value. */ -int dso__data_get_fd(struct dso *dso, struct machine *machine) +bool dso__data_get_fd(struct dso *dso, struct machine *machine, int *fd) { + *fd =3D -1; if (dso__data(dso)->status =3D=3D DSO_DATA_STATUS_ERROR) - return -1; + return false; =20 - if (pthread_mutex_lock(&dso__data_open_lock) < 0) - return -1; + mutex_lock(dso__data_open_lock()); =20 try_to_open_dso(dso, machine); =20 - if (dso__data(dso)->fd < 0) - pthread_mutex_unlock(&dso__data_open_lock); + *fd =3D dso__data(dso)->fd; + if (*fd >=3D 0) + return true; =20 - return dso__data(dso)->fd; + mutex_unlock(dso__data_open_lock()); + return false; } =20 void dso__data_put_fd(struct dso *dso __maybe_unused) { - pthread_mutex_unlock(&dso__data_open_lock); + mutex_unlock(dso__data_open_lock()); } =20 bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by) @@ -954,7 +975,7 @@ static ssize_t file_read(struct dso *dso, struct machin= e *machine, { ssize_t ret; =20 - pthread_mutex_lock(&dso__data_open_lock); + mutex_lock(dso__data_open_lock()); =20 /* * dso__data(dso)->fd might be closed if other thread opened another @@ -970,7 +991,7 @@ static ssize_t file_read(struct dso *dso, struct machin= e *machine, =20 ret =3D pread(dso__data(dso)->fd, data, DSO__DATA_CACHE_SIZE, offset); out: - pthread_mutex_unlock(&dso__data_open_lock); + mutex_unlock(dso__data_open_lock()); return ret; } =20 @@ -1078,7 +1099,7 @@ static int file_size(struct dso *dso, struct machine = *machine) struct stat st; char sbuf[STRERR_BUFSIZE]; =20 - pthread_mutex_lock(&dso__data_open_lock); + mutex_lock(dso__data_open_lock()); =20 /* * dso__data(dso)->fd might be closed if other thread opened another @@ -1102,7 +1123,7 @@ static int file_size(struct dso *dso, struct machine = *machine) dso__data(dso)->file_size =3D st.st_size; =20 out: - pthread_mutex_unlock(&dso__data_open_lock); + mutex_unlock(dso__data_open_lock()); return ret; } =20 @@ -1611,11 +1632,10 @@ size_t dso__fprintf(struct dso *dso, FILE *fp) =20 enum dso_type dso__type(struct dso *dso, struct machine *machine) { - int fd; + int fd =3D -1; enum dso_type type =3D DSO__TYPE_UNKNOWN; =20 - fd =3D dso__data_get_fd(dso, machine); - if (fd >=3D 0) { + if (dso__data_get_fd(dso, machine, &fd)) { type =3D dso__type_fd(fd); dso__data_put_fd(dso); } diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h index 84d5aac666aa..846b74510038 100644 --- a/tools/perf/util/dso.h +++ b/tools/perf/util/dso.h @@ -232,6 +232,8 @@ DECLARE_RC_STRUCT(dso) { char name[]; }; =20 +extern struct mutex _dso__data_open_lock; + /* dso__for_each_symbol - iterate over the symbols of given type * * @dso: the 'struct dso *' in which symbols are iterated @@ -653,7 +655,7 @@ void __dso__inject_id(struct dso *dso, const struct dso= _id *id); int dso__name_len(const struct dso *dso); =20 struct dso *dso__get(struct dso *dso); -void dso__put(struct dso *dso); +void dso__put(struct dso *dso) LOCKS_EXCLUDED(_dso__data_open_lock); =20 static inline void __dso__zput(struct dso **dso) { @@ -733,8 +735,8 @@ void dso__set_module_info(struct dso *dso, struct kmod_= path *m, * The current usage of the dso__data_* interface is as follows: * * Get DSO's fd: - * int fd =3D dso__data_get_fd(dso, machine); - * if (fd >=3D 0) { + * int fd; + * if (dso__data_get_fd(dso, machine, &fd)) { * USE 'fd' SOMEHOW * dso__data_put_fd(dso); * } @@ -756,9 +758,10 @@ void dso__set_module_info(struct dso *dso, struct kmod= _path *m, * * TODO */ -int dso__data_get_fd(struct dso *dso, struct machine *machine); -void dso__data_put_fd(struct dso *dso); -void dso__data_close(struct dso *dso); +bool dso__data_get_fd(struct dso *dso, struct machine *machine, int *fd) + EXCLUSIVE_TRYLOCK_FUNCTION(true, _dso__data_open_lock); +void dso__data_put_fd(struct dso *dso) UNLOCK_FUNCTION(_dso__data_open_loc= k); +void dso__data_close(struct dso *dso) LOCKS_EXCLUDED(_dso__data_open_lock); =20 int dso__data_file_size(struct dso *dso, struct machine *machine); off_t dso__data_size(struct dso *dso, struct machine *machine); diff --git a/tools/perf/util/unwind-libunwind-local.c b/tools/perf/util/unw= ind-libunwind-local.c index 5f4387e2423a..9fb2c1343c7f 100644 --- a/tools/perf/util/unwind-libunwind-local.c +++ b/tools/perf/util/unwind-libunwind-local.c @@ -330,8 +330,7 @@ static int read_unwind_spec_eh_frame(struct dso *dso, s= truct unwind_info *ui, int ret, fd; =20 if (dso__data(dso)->eh_frame_hdr_offset =3D=3D 0) { - fd =3D dso__data_get_fd(dso, ui->machine); - if (fd < 0) + if (!dso__data_get_fd(dso, ui->machine, &fd)) return -EINVAL; =20 /* Check the .eh_frame section for unwinding info */ @@ -372,8 +371,7 @@ static int read_unwind_spec_debug_frame(struct dso *dso, * has to be pointed by symsrc_filename */ if (ofs =3D=3D 0) { - fd =3D dso__data_get_fd(dso, machine); - if (fd >=3D 0) { + if (dso__data_get_fd(dso, machine, &fd) { ofs =3D elf_section_offset(fd, ".debug_frame"); dso__data_put_fd(dso); } @@ -485,14 +483,16 @@ find_proc_info(unw_addr_space_t as, unw_word_t ip, un= w_proc_info_t *pi, /* Check the .debug_frame section for unwinding info */ if (ret < 0 && !read_unwind_spec_debug_frame(dso, ui->machine, &segbase)) { - int fd =3D dso__data_get_fd(dso, ui->machine); - int is_exec =3D elf_is_exec(fd, dso__name(dso)); + int fd; u64 start =3D map__start(map); - unw_word_t base =3D is_exec ? 0 : start; + unw_word_t base =3D start; const char *symfile; =20 - if (fd >=3D 0) + if (dso__data_get_fd(dso, ui->machine, &fd)) { + if (elf_is_exec(fd, dso__name(dso))) + base =3D 0; dso__data_put_fd(dso); + } =20 symfile =3D dso__symsrc_filename(dso) ?: dso__name(dso); =20 --=20 2.49.0.rc1.451.g8f38331e32-goog From nobody Wed Dec 17 12:12:45 2025 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (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 B3DFF19ABAB for ; Tue, 18 Mar 2025 04:32:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742272324; cv=none; b=RoYxlabGcyvfB2hqpMLtmp6u5XFr/olZ2xu+Z+icQbRNfj378KCmH/bu0CY0MLvGaMo3ITr18TiLX9rDAuyn36ajv/HV9+hKr20n2cNhLx04pKfWCKhM3Cc88A2dm83wt95tfqpObEgVB6fL6eLdOCj09J3Lt8NQR93+MIztBF8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742272324; c=relaxed/simple; bh=d5/SPH6Xszn1gnN5qqSdZHDkkc8zw3ZG8Oun46aesZk=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=XuLMhwDmntlM4b94gXy/vlDtALEZ4WuzRlejrFrUkkHAXgbQ6PWfPQKdiYce5XXZEX/RNsrK4c36qH4MV+DgKy6w0LektAlkbEsUSB1ctE7nybNtYxUl+xpEiVf+w5OE9ZGHKyHAd4YDVBfyioTbOC5hIBiq89GoHLV6pEDy2Ys= 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=0IWX/TnC; arc=none smtp.client-ip=209.85.128.202 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="0IWX/TnC" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6fec6c91630so73288677b3.2 for ; Mon, 17 Mar 2025 21:32:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1742272321; x=1742877121; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=EUkAIINDzOsP+PuNVteX8o8oIwqX+sSmcLnHXIaoPro=; b=0IWX/TnCQZK7vslD49s0JHa2D3hZRsB5MJ7Tl0priZ97uaPaMOt3X33rExPJZuLg9N cUbvLed6Dd71o2LIgMbR/4JxPmlnt+L6DKv7WigLqAH4GyHFahZbLO1esvyWCHefxxMv Tegpjb8NQJAQLj4RJSOtOqSl4RtdY7Ot2zwwfaEBDSMednD/DFyKJIfQJn3+tO6BF97b gj6ieM04ttXT5gAfoU/XO8lP/Dq59k9hG4WN2dJf6auXr0jXsdvc8T7lU/kRIxcaaGwn cRQPWZ8fnYfjPjNgqLwGsb81qZUkdcxhysEtHYrE57/6HAVSJ9AfJ2SyTYwWiSp4yO8H ctWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742272321; x=1742877121; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=EUkAIINDzOsP+PuNVteX8o8oIwqX+sSmcLnHXIaoPro=; b=dtJgfPfuqokXD0L4srswGIY6D3759L7Ty6aTHWQwNd0NfEWFzx2vdrqyi4VbjJbwip wcPr7iFujUzgC0gP+4EsmSu/B8MgfGdqp0OUiOkFjdu+CAfXbpkwFfZJ3SfSdCIS/4Tm dKHcmN5ZnlMkGkiwYuxBgSHZHYznXzpufu76bsvNMvy4sC0rh/OcAuqT61lJiyVu9wRb sWRGKRSC4uddIkU+UZjPrmx120cGBCjteG4CLC2xv38iMJDCu7TKi2dDjz/a53ol/p7w c+vVBY2+ivUqYrokSUcWvE+MBeL26RRv1He178sf7gDEORFyd2jBsm/fmJP/cOELwUfA Kr1A== X-Forwarded-Encrypted: i=1; AJvYcCUoAjHVgXtEXg8M/8BEJQBFxrduldpiYavlXs0SxuONA9gsAW0TrTGvuo+0CqZ6aI7GZ6S/e5ZmyTEQ4vw=@vger.kernel.org X-Gm-Message-State: AOJu0YwCzgNlSV0ggJXaBkvKkkQKk15nO4/wng50cUZ+zs87pquSzwfz OTkW6cVKb+A9DDrG9dkxf+aeUEWktynp+Pf1QqY4G9KnsDn6U7k2K0V2zpWpzzl44uc2ZpB4VaC uYp8jNg== X-Google-Smtp-Source: AGHT+IEcfXSAohVmxzPknUUIX08jwASTAN/GzdnD1c7IYI76WLUVAzrBv5lIoiUiC2EvtM1IH2Qkcs87+T4e X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:ef29:15ce:a42a:b69f]) (user=irogers job=sendgmr) by 2002:a05:690c:2c05:b0:6fe:c5cc:e1ab with SMTP id 00721157ae682-6ff46024ae2mr176667b3.6.1742272321267; Mon, 17 Mar 2025 21:32:01 -0700 (PDT) Date: Mon, 17 Mar 2025 21:31:51 -0700 In-Reply-To: <20250318043151.137973-1-irogers@google.com> Message-Id: <20250318043151.137973-4-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250318043151.137973-1-irogers@google.com> X-Mailer: git-send-email 2.49.0.rc1.451.g8f38331e32-goog Subject: [PATCH v2 3/3] perf test dso-data: Correctly free test file in read test From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , Athira Rajeev , Stephen Brennan , James Clark , Yunseong Kim , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The DSO data read test opens a file but as dsos__exit is used the test file isn't closed. This causes the subsequent subtests in don't fork (-F) mode to fail as one more than expected file descriptor is open. Signed-off-by: Ian Rogers --- tools/perf/tests/dso-data.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/tools/perf/tests/dso-data.c b/tools/perf/tests/dso-data.c index 06be7c5d8495..a1fff4203b75 100644 --- a/tools/perf/tests/dso-data.c +++ b/tools/perf/tests/dso-data.c @@ -114,6 +114,17 @@ static int dso__data_fd(struct dso *dso, struct machin= e *machine) return fd; } =20 +static void dsos__delete(struct dsos *dsos) +{ + for (unsigned int i =3D 0; i < dsos->cnt; i++) { + struct dso *dso =3D dsos->dsos[i]; + + dso__data_close(dso); + unlink(dso__name(dso)); + } + dsos__exit(dsos); +} + static int test__dso_data(struct test_suite *test __maybe_unused, int subt= est __maybe_unused) { struct machine machine; @@ -172,7 +183,7 @@ static int test__dso_data(struct test_suite *test __may= be_unused, int subtest __ } =20 dso__put(dso); - dsos__exit(&machine.dsos); + dsos__delete(&machine.dsos); unlink(file); return 0; } @@ -222,17 +233,6 @@ static int dsos__create(int cnt, int size, struct dsos= *dsos) return 0; } =20 -static void dsos__delete(struct dsos *dsos) -{ - for (unsigned int i =3D 0; i < dsos->cnt; i++) { - struct dso *dso =3D dsos->dsos[i]; - - dso__data_close(dso); - unlink(dso__name(dso)); - } - dsos__exit(dsos); -} - static int set_fd_limit(int n) { struct rlimit rlim; --=20 2.49.0.rc1.451.g8f38331e32-goog