From patchwork Fri Feb 2 06:15:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 195606 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp239578dyc; Thu, 1 Feb 2024 22:22:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IGBJfJPNj4g+NqdkKaZRQdYB2QlB03RPvnhf3WYr1dRhzv5/uGFKXj8VUTbU+3dUng+ovGb X-Received: by 2002:a05:620a:4094:b0:785:546b:7dc3 with SMTP id f20-20020a05620a409400b00785546b7dc3mr1251503qko.9.1706854927806; Thu, 01 Feb 2024 22:22:07 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706854927; cv=pass; d=google.com; s=arc-20160816; b=wzFlP7pcSDkrk2idWC4CeUL40rrxTqVnIgvPlrUlB5rMGsUBKIqYU3T+lzQIX4wkxP 2KwNkMuNQzymqqH/SDIaQohKYiwwF6gePm7urJLAdO6wZHzynJ1b3cSXUdU3avkspLXk ATok7/h/FHNfoj1s4slz8IJ/Che4/ANUjvjJacIu2pHmfICn+5huHnPIi+gNuyR+AY9n W7vLqbaGpdH15EuXBy6XhqiNjryR8BvZNhtQqMKEw/hMbiXH2FqvNmUf78Ma/rEgD/6M F3Y7ntsfQ9ID4+bEwrq/gYs3bSuPzDkplXrxSltZKIXmsNnXcfGlxUeUdM8KTjKtFmUH 9ztg== 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=O7s4uPo6a6IUX6P+LNwsbu+H+dBYWvGrbbrG302CchI=; fh=SvPPW/vyDAo4/CD/0DyoCri17fIjahFLHzmwgaYn+bc=; b=ufa5Y/kLSaJRCGZa72XHfGijcD0W5Yarnb0/Q79nCWmGKCqY2XUMtPK2h8dED3Bkec hd4KVjmyuR8xKlbYSuAwCJVW3k9gajFGwKBUT8Zk16bBplqldg2hkR3mrMglu365Ov9C 9x6FSiJO/8NFdONiG6PdskmvBv1RlNOs3YYQgFzd9tbt00/QHI2xGJAb7u/xEUFfxeVk E9MPlMQQSPGpaaOFSRcRrlwdy+IJkw8h6YDJ0+T46svSYnRq9f4Tqa5STv7BD3ZBg4Rz xm25voPtwlSkzKcvYKmf+r1Pv8rgdwX+axMkhqLTGgH7KGq5ZRa3r1bJYHe+CF3fPcB2 hmwg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=B+NNC7IR; 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-49263-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-49263-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=1; AJvYcCUHzq2dbXMWhcKCljJxWm8tbMdBBdsZsX5BfYzDRGm8F3iLfN3p/vkqc+S4hCbBmamg7pijnkYIW3x8dCcVrN47a8He1w== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id b21-20020a05620a127500b00783ab5f86adsi1327537qkl.265.2024.02.01.22.22.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Feb 2024 22:22:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-49263-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=B+NNC7IR; 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-49263-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-49263-ouuuleilei=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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 88BCC1C241E1 for ; Fri, 2 Feb 2024 06:22:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3101748CFF; Fri, 2 Feb 2024 06:16:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="B+NNC7IR" 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 D933E17BC4 for ; Fri, 2 Feb 2024 06:16:45 +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=1706854607; cv=none; b=PbA2p7NFq5IF8QODzfhI6bzhsJxBsu2B42B9dhXniDBv72BCGr28cwPmcyQTvj6DolPA6bUDtVaMh71aq8DyLGjdTUe1q/3FZPwQMxwy4tn+pjK16/VDeUcLBdQndsAgQ6toYdnV3wc1mPqc+rooIC5ED0tJ9ZiP5V8GQtlfGIE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706854607; c=relaxed/simple; bh=xA5Ts8mXmjH0ByX7hN5Pia0888YuBQgn/eJu9XRxSZo=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=sA9g5ngg1yIbLdz4Fhh7tFksJfNI71P1qRXb1LBDG4Tpla0lQUto9xqYsDt5mdxDcJ4f7GTKu+8bMrLumvMU2ekZxdL74i+bfPNcrPMKn7FKeC98RXNqxlMgx4PtQiEYpWjvVN4aNtU1WC8X+48FjlSS1OP1hePO4BkKfOBo4e8= 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=B+NNC7IR; 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 Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dc6c24a4ce1so2745778276.2 for ; Thu, 01 Feb 2024 22:16:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706854605; x=1707459405; 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=O7s4uPo6a6IUX6P+LNwsbu+H+dBYWvGrbbrG302CchI=; b=B+NNC7IRggfOAoQQn6QX+RtqILSsG3TRD9+yeuh6C+nF7PgUm066kctM9Y6cveXI6Z D5yX/Iur8MGVQxBzYR/RNm9joUZiiLi2kMJe+a78k8hR9u4xLVBPw+x5gjS6nf99iPct y9dsq8GFVrc8y9YxiC2+7v9vf13zz6nKt8T1oD8dIWACkQVqGNpA4GpmLE+Ef3nLsTcK flCCoIqj0ROisNakwM2T1YcQ7ZTm10BtzccOJ1rhGNUAKE2PBn/FbDRU0paPPnEUHBB4 6z1HctWLDW7M9flrkBdeiWUb/DwNYwq2Az0mhNJPmlVM20pk+kltn3MVSMgjrCtas2lG ShgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706854605; x=1707459405; 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=O7s4uPo6a6IUX6P+LNwsbu+H+dBYWvGrbbrG302CchI=; b=BsvMJG7Fp9PTg6h4UJgP2poDQZUm9r+mRFI2B+F/s9mfgpGN+mMUpYmcPyFT93KkgB GBgp+h7/e7OG4bRrLNFK0F8UMSCYQZIXJROYl3EZSDHS17KWDU+IqJvMmvnMQS6mVAQy m/+mWRmimMhsCrQrwwIEfDGP5/289XB6pWKLa76+ZEzXzK5BfSw7Wu+gZ5zWuZ2zOyHT 2SEEADhJIFzr7oI2FiVht3bWpmFMAu2vU42GvHsK98UvLmkRDEFI0PZQUBpvaCmPDuGl pszByKePn5a3H8STqXWDOsIj6f0MHgws/egeFNS3991d3JaBB8ore9MZMR7QeKSqxIi8 6HNA== X-Gm-Message-State: AOJu0Yzf5Fbfbq5AcMhWgoorXux/L65H+9J9Oll32f78If0S0OrM/IT1 n6hy5qQqtsJSCtb+GVYC/Bq0ut966qe9LtBth2lBaJ8trPX+U/Fdszm8qqr7JyBeya7W1h3Wycc wsZ7rzA== X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:a85f:db1d:a66b:7f53]) (user=irogers job=sendgmr) by 2002:a05:6902:240c:b0:dc2:4009:b35e with SMTP id dr12-20020a056902240c00b00dc24009b35emr1783465ybb.9.1706854604950; Thu, 01 Feb 2024 22:16:44 -0800 (PST) Date: Thu, 1 Feb 2024 22:15:32 -0800 In-Reply-To: <20240202061532.1939474-1-irogers@google.com> Message-Id: <20240202061532.1939474-26-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240202061532.1939474-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog Subject: [PATCH v8 25/25] perf dso: Use container_of to avoid a pointer in dso_data From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Kajol Jain , Athira Rajeev , Huacai Chen , Masami Hiramatsu , "Steinar H. Gunderson" , Liam Howlett , Miguel Ojeda , Colin Ian King , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , Ming Wang , James Clark , K Prateek Nayak , Sean Christopherson , Leo Yan , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Li Dong , Sandipan Das , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Guilherme Amadio X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789767112542063957 X-GMAIL-MSGID: 1789767112542063957 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 ddf58f594df0..83de99e52141 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) @@ -1448,7 +1460,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 3e27f93898f2..3311c1740840 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;