Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp2709478lqt; Mon, 22 Apr 2024 21:28:09 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWaG5ZjxvecLt2zV96GyXLZ2h6bj4R3dHJg7a1ErBVdCnfgtHI5U3EBsMlO7owp43Ogr9OkGYNsozgUbKx4n2PhIMI2pmJnIWGbJB0fiw== X-Google-Smtp-Source: AGHT+IET/+rKsv2T85lh39eZ6d2khxO6B0tUNrzEeYQvgAIjzrjCnHh8/beQoQtaLAAZk2cxG6Sf X-Received: by 2002:a17:906:300a:b0:a52:30d3:41dd with SMTP id 10-20020a170906300a00b00a5230d341ddmr8190831ejz.41.1713846489311; Mon, 22 Apr 2024 21:28:09 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713846489; cv=pass; d=google.com; s=arc-20160816; b=nFG22ZKZvvgc7bD2z/xCc+iB1/F/+hLpzpGCwWPEuvtAsWAXifeHUw5+yn4vuwxqet EvhAZwMNEloaZaK6JtOwK2pB6DjEI0feyf6H2jVtRVPVt91Ff90qtEfeFtLk17UzHDHN 9L1nx9X1wzzqsifWrpRs8+D9BE6esrzSiLvK4QFj+cic8XlolNAQ5rqHyB1F4eZxTD/z YMYd44U5PNnklpJm4VCjnt7c/QtjMRJwtIC2SaIJ7u9ADbbmcfcubiB4Y8DUmsWOgj5I lGqbbYUNEVFCByE46ayCH/vMCcbzNK0rPnkkdWT6Z1USdf2d7MViXtAehYxYDPCb7nft XSzQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=to:from:subject:references:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:in-reply-to:date :dkim-signature; bh=HJdwWYWTyzE2O801RQGxgyFCeT+SeHyEmr9sJLF5pAo=; fh=HHbKRPlNXmj/32amF4Z+HCqmbprYqoVR2KorxeIwE2Y=; b=x3elj1U2aKvSvmhFVHypxtslk6PXHvuxw+FiigIIhSX/f/dzGoUpSCDAQJ9nejbZqk 9rS6Il9AGS2v0Zl5FLPnG/nXZQpMxb4trFjHuxLxZwc/rHk+6w6usfc/hR8WRLUkhJMe y3IW9liD1+wjZqeBZY2+1gcT7N+UIz1d+tGX0mySYtZ3bON92uDD5UWHG3vcOeLgOY9L IkfF2uJer8XLhuqy7tNMzI6mjUiAXs7SpjA5Obd4sWOuH5XsRb/peaRWVDwa8UGW2Ee6 yNPdq+LQFmrB1YL439bXKB7E5mVBuHcnU5WGF84w2n6z6mcLAjzi8PUdMfnRKwPqX2Zx kExw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=LX3CFdZq; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-154501-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-154501-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id ch18-20020a170906c2d200b00a5876da1ca3si671692ejb.328.2024.04.22.21.28.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Apr 2024 21:28:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-154501-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=LX3CFdZq; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-154501-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-154501-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id DD31B1F21544 for ; Tue, 23 Apr 2024 04:28:08 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 96EB637143; Tue, 23 Apr 2024 04:26:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="LX3CFdZq" Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.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 44F881F934 for ; Tue, 23 Apr 2024 04:26:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713846399; cv=none; b=JhlOEFlly58RxifNKR4FqBhxxWjGWUI2Q6EUDC3CmvxVoBgEUfp/UWkKcYInAVTphMEVjOyA0btD7s2gBvznEFy3pJnLErbIvhaVQOuu1fT7/TJNNwKi84Y3Eo5D86LyB0Y8mIQA8CrgrYpm6lMrroYxhI9j/ivRzqE594ONRA8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713846399; c=relaxed/simple; bh=EdPl+cF+Uacqv5Nfnm9cA4AOJEVckbieQ5lajSHXHVg=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=LPFNClwqkGaruKbaTEi9MQCcnDyvsjQtELSi5FGvUp1EN82Q5xwm8WL79xyQ1vS/pg1QThzAP4rjaz3sGiRQdW4Hy80ocH8bjN+Ac4dfk268v7Z/ddsJ/KO6um8SNDDLmsOzs5Q+QBbQU/aKGfwsVXL1pTz9hxutrKPMicLP/Rc= 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=LX3CFdZq; arc=none smtp.client-ip=209.85.219.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 Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-de45daf49deso11579775276.2 for ; Mon, 22 Apr 2024 21:26:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1713846396; x=1714451196; 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=HJdwWYWTyzE2O801RQGxgyFCeT+SeHyEmr9sJLF5pAo=; b=LX3CFdZq9HiS4fzWUgU1ssTdXBfEf1OZSbcI5tD9AMAyv/dsIJ0PcHhaPPwRkMXaki L/h2DeGsfEk5GU56baGva72m+ReeDW3dzNNe4ntSvVRRNw9fdmYlKUIsMAqee9/wIQN1 Zi1kbHlfWU2RfUDCL7pkSrclHZv9baNJUEWj1JyPqEMbigmzyBTiSK5gypBpkob0ut99 0y+5YUophTaemu2GuFWDoNkng1kRDwGGq1esxoYI8DWG9zpcAn8D7l/+xRQW4ybii/JX 4VKXXgE0U/NMx8ILU0QCDuJyx5R0rGGVUgPfI/8T43j72GisMoqboLSTqlGejalWv7Zd jnew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713846396; x=1714451196; 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=HJdwWYWTyzE2O801RQGxgyFCeT+SeHyEmr9sJLF5pAo=; b=XX3s209CSg5dwzLK9rEJqf8qfmeTEvRzfnueK97Vyu3nqPaJZ1s/ejZ1EHscj9MFke tLj9oreECWbbahBaLRxRQG58gc+F3d9fmKBIrxrb6jb0e+7SS7BlowydQfmgaDotSGIC Y6feGSL39DW0h2XeGVRGr7o8u8nSjDWeMGWeCCyQissj6FVh+gD/8cyJoIgMgDvWpsYg LD7V1A2NZRkNzmm550qsTque26hm/ETnZExkjqL0b8Ym1WSYbnfdpndYGfLEuDuboJDr 1+xwxsJpP/VzYS4kUfMKepcFtGWv+yhRnZ/tAfSL3rCTAcdpnQx4bDSImiThuw3YJ89B nJGw== X-Forwarded-Encrypted: i=1; AJvYcCXjsSq6unu0tEokimmm//ZKdf7uGtA+EPzrqJywj+ERjvPDz3XAr7hJnXmIgDHm8oLLLxLDsVD7idiJENNnnWN67wk6ZWFqfnS59meJ X-Gm-Message-State: AOJu0YwwAFuWVanqZVlSpTAiLeE6/EyOjOwBTLavoke5dbS+Wo33vVG3 LFIsLACNohgKd0qB3WAMSS0wztQMtazqHEjPKf25CsIvcuwuJqexWesnZl0DyDFhKpie/aZNDdS ofJj3mQ== X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:4ca4:4d48:a7fe:d70c]) (user=irogers job=sendgmr) by 2002:a25:ad8d:0:b0:dc6:e5d3:5f03 with SMTP id z13-20020a25ad8d000000b00dc6e5d35f03mr3460642ybi.4.1713846396125; Mon, 22 Apr 2024 21:26:36 -0700 (PDT) Date: Mon, 22 Apr 2024 21:26:01 -0700 In-Reply-To: <20240423042601.2009469-1-irogers@google.com> Message-Id: <20240423042601.2009469-8-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240423042601.2009469-1-irogers@google.com> X-Mailer: git-send-email 2.44.0.769.g3c40516874-goog Subject: [PATCH v4 7/7] perf dso: Use container_of to avoid a pointer in dso_data 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 , James Clark , Athira Rajeev , Colin Ian King , nabijaczleweli@nabijaczleweli.xyz, Leo Yan , Song Liu , Ilkka Koskinen , Ben Gainey , K Prateek Nayak , Yanteng Si , Sun Haiyong , Changbin Du , Andi Kleen , Thomas Richter , Masami Hiramatsu , Dima Kogan , zhaimingbing , Paran Lee , Li Dong , Tiezhu Yang , Chengen Du , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" The dso pointer in dso_data is necessary for reference count checking to account for the dso_data forming a global list of open dso's with references to the dso. The dso pointer also allows for the indirection that reference count checking needs. Outside of reference count checking the indirection isn't needed and container_of is more efficient and saves space. The reference count won't be increased by placing items onto the global list, matching how things were before the reference count checking change, but we assert the dso is in dsos holding it live (and that the set of open dsos is a subset of all dsos for the machine). Update the DSO data tests so that they use a dsos struct to make the invariant true. Signed-off-by: Ian Rogers --- tools/perf/tests/dso-data.c | 60 ++++++++++++++++++------------------- tools/perf/util/dso.c | 16 +++++++++- tools/perf/util/dso.h | 2 ++ 3 files changed, 46 insertions(+), 32 deletions(-) diff --git a/tools/perf/tests/dso-data.c b/tools/perf/tests/dso-data.c index fde4eca84b6f..5286ae8bd2d7 100644 --- a/tools/perf/tests/dso-data.c +++ b/tools/perf/tests/dso-data.c @@ -10,6 +10,7 @@ #include #include #include "dso.h" +#include "dsos.h" #include "machine.h" #include "symbol.h" #include "tests.h" @@ -123,9 +124,10 @@ static int test__dso_data(struct test_suite *test __maybe_unused, int subtest __ TEST_ASSERT_VAL("No test file", file); memset(&machine, 0, sizeof(machine)); + dsos__init(&machine.dsos); - dso = dso__new((const char *)file); - + dso = dso__new(file); + TEST_ASSERT_VAL("Failed to add dso", !dsos__add(&machine.dsos, dso)); TEST_ASSERT_VAL("Failed to access to dso", dso__data_fd(dso, &machine) >= 0); @@ -170,6 +172,7 @@ static int test__dso_data(struct test_suite *test __maybe_unused, int subtest __ } dso__put(dso); + dsos__exit(&machine.dsos); unlink(file); return 0; } @@ -199,41 +202,35 @@ static long open_files_cnt(void) return nr - 1; } -static struct dso **dsos; - -static int dsos__create(int cnt, int size) +static int dsos__create(int cnt, int size, struct dsos *dsos) { int i; - dsos = malloc(sizeof(*dsos) * cnt); - TEST_ASSERT_VAL("failed to alloc dsos array", dsos); + dsos__init(dsos); for (i = 0; i < cnt; i++) { - char *file; + struct dso *dso; + char *file = test_file(size); - file = test_file(size); TEST_ASSERT_VAL("failed to get dso file", file); - - dsos[i] = dso__new(file); - TEST_ASSERT_VAL("failed to get dso", dsos[i]); + dso = dso__new(file); + TEST_ASSERT_VAL("failed to get dso", dso); + TEST_ASSERT_VAL("failed to add dso", !dsos__add(dsos, dso)); + dso__put(dso); } return 0; } -static void dsos__delete(int cnt) +static void dsos__delete(struct dsos *dsos) { - int i; - - for (i = 0; i < cnt; i++) { - struct dso *dso = dsos[i]; + for (unsigned int i = 0; i < dsos->cnt; i++) { + struct dso *dso = dsos->dsos[i]; dso__data_close(dso); unlink(dso__name(dso)); - dso__put(dso); } - - free(dsos); + dsos__exit(dsos); } static int set_fd_limit(int n) @@ -267,10 +264,10 @@ static int test__dso_data_cache(struct test_suite *test __maybe_unused, int subt /* and this is now our dso open FDs limit */ dso_cnt = limit / 2; TEST_ASSERT_VAL("failed to create dsos\n", - !dsos__create(dso_cnt, TEST_FILE_SIZE)); + !dsos__create(dso_cnt, TEST_FILE_SIZE, &machine.dsos)); for (i = 0; i < (dso_cnt - 1); i++) { - struct dso *dso = dsos[i]; + struct dso *dso = machine.dsos.dsos[i]; /* * Open dsos via dso__data_fd(), it opens the data @@ -290,17 +287,17 @@ static int test__dso_data_cache(struct test_suite *test __maybe_unused, int subt } /* verify the first one is already open */ - TEST_ASSERT_VAL("dsos[0] is not open", dso__data(dsos[0])->fd != -1); + TEST_ASSERT_VAL("dsos[0] is not open", dso__data(machine.dsos.dsos[0])->fd != -1); /* open +1 dso to reach the allowed limit */ - fd = dso__data_fd(dsos[i], &machine); + fd = dso__data_fd(machine.dsos.dsos[i], &machine); TEST_ASSERT_VAL("failed to get fd", fd > 0); /* should force the first one to be closed */ - TEST_ASSERT_VAL("failed to close dsos[0]", dso__data(dsos[0])->fd == -1); + TEST_ASSERT_VAL("failed to close dsos[0]", dso__data(machine.dsos.dsos[0])->fd == -1); /* cleanup everything */ - dsos__delete(dso_cnt); + dsos__delete(&machine.dsos); /* Make sure we did not leak any file descriptor. */ nr_end = open_files_cnt(); @@ -325,9 +322,9 @@ static int test__dso_data_reopen(struct test_suite *test __maybe_unused, int sub long nr_end, nr = open_files_cnt(), lim = new_limit(3); int fd, fd_extra; -#define dso_0 (dsos[0]) -#define dso_1 (dsos[1]) -#define dso_2 (dsos[2]) +#define dso_0 (machine.dsos.dsos[0]) +#define dso_1 (machine.dsos.dsos[1]) +#define dso_2 (machine.dsos.dsos[2]) /* Rest the internal dso open counter limit. */ reset_fd_limit(); @@ -347,7 +344,8 @@ static int test__dso_data_reopen(struct test_suite *test __maybe_unused, int sub TEST_ASSERT_VAL("failed to set file limit", !set_fd_limit((lim))); - TEST_ASSERT_VAL("failed to create dsos\n", !dsos__create(3, TEST_FILE_SIZE)); + TEST_ASSERT_VAL("failed to create dsos\n", + !dsos__create(3, TEST_FILE_SIZE, &machine.dsos)); /* open dso_0 */ fd = dso__data_fd(dso_0, &machine); @@ -386,7 +384,7 @@ static int test__dso_data_reopen(struct test_suite *test __maybe_unused, int sub /* cleanup everything */ close(fd_extra); - dsos__delete(3); + dsos__delete(&machine.dsos); /* Make sure we did not leak any file descriptor. */ nr_end = open_files_cnt(); diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c index 27db65e96e04..dde706b71da7 100644 --- a/tools/perf/util/dso.c +++ b/tools/perf/util/dso.c @@ -497,14 +497,20 @@ static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER; static void dso__list_add(struct dso *dso) { list_add_tail(&dso__data(dso)->open_entry, &dso__data_open); +#ifdef REFCNT_CHECKING dso__data(dso)->dso = dso__get(dso); +#endif + /* Assume the dso is part of dsos, hence the optional reference count above. */ + assert(dso__dsos(dso)); dso__data_open_cnt++; } static void dso__list_del(struct dso *dso) { list_del_init(&dso__data(dso)->open_entry); +#ifdef REFCNT_CHECKING dso__put(dso__data(dso)->dso); +#endif WARN_ONCE(dso__data_open_cnt <= 0, "DSO data fd counter out of bounds."); dso__data_open_cnt--; @@ -654,9 +660,15 @@ static void close_dso(struct dso *dso) static void close_first_dso(void) { struct dso_data *dso_data; + struct dso *dso; dso_data = list_first_entry(&dso__data_open, struct dso_data, open_entry); - close_dso(dso_data->dso); +#ifdef REFCNT_CHECKING + dso = dso_data->dso; +#else + dso = container_of(dso_data, struct dso, data); +#endif + close_dso(dso); } static rlim_t get_fd_limit(void) @@ -1449,7 +1461,9 @@ struct dso *dso__new_id(const char *name, struct dso_id *id) data->fd = -1; data->status = DSO_DATA_STATUS_UNKNOWN; INIT_LIST_HEAD(&data->open_entry); +#ifdef REFCNT_CHECKING data->dso = NULL; /* Set when on the open_entry list. */ +#endif } return res; } diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h index f9689dd60de3..df2c98402af3 100644 --- a/tools/perf/util/dso.h +++ b/tools/perf/util/dso.h @@ -147,7 +147,9 @@ struct dso_cache { struct dso_data { struct rb_root cache; struct list_head open_entry; +#ifdef REFCNT_CHECKING struct dso *dso; +#endif int fd; int status; u32 status_seen; -- 2.44.0.769.g3c40516874-goog