Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp569186lqp; Thu, 21 Mar 2024 09:06:42 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWluc76rd6q99Jpu0VdJ42T22MWf4N4b0+GFyzuHf6kqzGZNM2GmXzEvEFPpobYPEyjyaSwuIChAXRmz4XjVgYM+Sc+o8IzaaeZHpPlfw== X-Google-Smtp-Source: AGHT+IFcT2+3o3liZ7jKnnqjX//zQwsrwifpJ4y6VXcMefkKn1UfiTeaswGZ0ZDZaR6sgv9gOw2S X-Received: by 2002:a17:906:468f:b0:a46:d591:873c with SMTP id a15-20020a170906468f00b00a46d591873cmr3654527ejr.18.1711037202690; Thu, 21 Mar 2024 09:06:42 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711037202; cv=pass; d=google.com; s=arc-20160816; b=XbvU9SR8NJYw3UrhH0w3WvfF83rmbPYGTz7z5Gpxs5DPt4wBo/nbwEQ9RvhNYH46sZ YQ+n9QTHxR4eNPnFBYjg8Bl4Fx+P5Yk3EcilRsEJIVOYISiuE1v2BqzFAgcakyWyyFp8 b90E8TSxRM4/iEvB2REdm5PzssX6hBOk7ji1flKu0XkgwFBbQHJcnp1TROrJQb/HKCIY 5KOi/W+ETp2gcpKo5E9sfDKup3dk0hw/lV3zR1kGsWxKJpR5PItxBP5HbzfU8q6OvnH5 9gkXJufIlUyQhnsYDUnx3c62asZaG3U8A9vAIJL9ETPHx8jCjQkaHVgmdj/u3m0fXxgV 7JqA== 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=3Gaj42JKJGpbBMXevo1XN/rsx9c1VGwA8xTiPugnM4A=; fh=WdKYwT77N8mJK+R9AT/BaTF/QPTZ1LsZeO28qa88Rd0=; b=YTw/zHiy18IkjfI047oDzklsQbZmUMNYD7WGy8NmtsHZA1nDXxhmeeZFNshlZigGZk bY8Jn1MKdj4vSHbiJz3jkuAF8ujGgHvcr+GF2pemEXFpBU42dynr1nDUaZZt1nuCSk0n Rv2MvuhUSyF1vp+PDC6ZXo5xg7Vf5o6b0ZVBVq0/jh8YbFzS+j9rnRg5kKVrzInq8SnL GhjGeLDvn91JQG6I/9vjGY7fKknJnsd3LSzZkV+5e71cQb/OfEixAKg8LnfvHrDv3rjV bBeaTvq9H8cRu5HLKPythBnjODazu/hKxYctX86LHMLEM9k48p7QVqeerdNSlcNTDzsm MjOA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Qx1O0F+b; 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-110307-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-110307-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. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id jg31-20020a170907971f00b00a466643297asi51522ejc.547.2024.03.21.09.06.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Mar 2024 09:06:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-110307-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Qx1O0F+b; 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-110307-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-110307-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 427F61F21E8E for ; Thu, 21 Mar 2024 16:06:42 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5D76F12E1D5; Thu, 21 Mar 2024 16:03:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Qx1O0F+b" 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 E750B12DD9B for ; Thu, 21 Mar 2024 16:03:40 +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=1711037024; cv=none; b=Vk88t8b0h3NQZUKKjcBYDekom/efuNFUXw8UgZCYplVaGpRRe2uCEWK7J5A9I6M6o3yyWQCU7Xs4Tj5sZ9XWDuseJ077lHC4S+EP0d8BcPX7kiGKm/Nfy5Sd2CSrm6qc69U6wL1M3j2R42tHwfVRgUgbOJclZws+9tR6MFGDWEg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711037024; c=relaxed/simple; bh=hSMuUa1ZbCYjg1HblkWJi89sX+KRofSvSmdTL9QiRM4=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=Wex5WBo0OmnbJwvOpW+tFiM+b1dvwmvcmaXQucUcrCZ2Kr5NJGAoWKS69/pGzAb4M1HJCIYZs5WH8e5Zwgwu5lEDil0Wlt/1O4VLrC/ULUpboMcSKjGfjXA62ymhd//wWjudXZwuDUkngCRPDeZyRX2JfO2VF/+vGsnCV/KU0rw= 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=Qx1O0F+b; 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-dcbee93a3e1so1794359276.3 for ; Thu, 21 Mar 2024 09:03:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1711037020; x=1711641820; 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=3Gaj42JKJGpbBMXevo1XN/rsx9c1VGwA8xTiPugnM4A=; b=Qx1O0F+bFc65A6y+UoQN7tTSy5W2CwqZwvtVGOLE454j84ZrwV/t9wLMxQnUI9vpPC g+9vqRHPr+vq8CaIDJCqbaBOL1zm7pn/VTiNcN13ntQDXMGMzxdV7rSbnTL9oHM9Z9nG 17BDb/kTDenVTvLZBwkoJP8XcXOYUPKOe9NqkIjvIPR6qjqmI9ED8cQLcnl2viPim6K/ gZRFfLkC0zU5kjo6C1IzkdvbE/nvjt1sXFcufYmiBharm0LITvSzE5HTPD6pYTfZcL+l 4zoq2aQSCdnA94Xr2p+Sxd/nbeFtwzgno/Df+kw9sSFrqfGgMzPG5DPcJ1jadN2xhIGn +vqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711037020; x=1711641820; 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=3Gaj42JKJGpbBMXevo1XN/rsx9c1VGwA8xTiPugnM4A=; b=AoM1LFD2lxdMOUCqE9XMolKkB2/yCz4sgIPOu+LaT7/6FPgUsaK4TJASXpV3qWCoIf LZcvqw0bjVLUW/4f4hcEd552xVJOIX48hQCq+9Sw0gtMugvOA0oKmRQBab/M7vlUY91/ TlE+9kXK8BNrmNz8u4p6z9cEyYRfzsg/EUPxctC3k+IUzDAE6/bcMWGQzpJ7fU3LyELc 7c/1TtWCZt2ft9Afu0O/wLyzgL9s5sAaV7ZvIxttOOW+Wjb7HFFIyEO4uFmWv2XF0vV3 KGL4H6el/CnURImibkiIdm9hoHOB3aH3TFkwKmAxNb2k8SIJmIrZV/fp2UWEswHxqkyY bD9A== X-Forwarded-Encrypted: i=1; AJvYcCWSLAzitAUgcemwkmWUlLxTTao+0E7MskbMSdyFdRnaHbFOlyGhcZPXCVBNYulroIsNCIdcFER3URIMLvZ1NYeF6n/kYd1wKVpyCV6X X-Gm-Message-State: AOJu0Yy1fZdxEePGhQUuGUoheipapjB6nt8g+FxgEzW0WcetGnkuQ/+g ZHYFg38fJyC1HEzULeY8FzmXeAfqIOf92ATZl5ZFTviIRAk8IkOJqbot/+i7ZJqhTmFFNL2jInQ thlDCfA== X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:7f3:cf74:42c4:c0a]) (user=irogers job=sendgmr) by 2002:a05:6902:188f:b0:dcf:f526:4cc6 with SMTP id cj15-20020a056902188f00b00dcff5264cc6mr1128689ybb.11.1711037019952; Thu, 21 Mar 2024 09:03:39 -0700 (PDT) Date: Thu, 21 Mar 2024 09:03:00 -0700 In-Reply-To: <20240321160300.1635121-1-irogers@google.com> Message-Id: <20240321160300.1635121-14-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240321160300.1635121-1-irogers@google.com> X-Mailer: git-send-email 2.44.0.396.g6e790dbe36-goog Subject: [PATCH v2 13/13] 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 , James Clark , Athira Rajeev , Colin Ian King , "=?UTF-8?q?Ahelenia=20Ziemia=C5=84ska?=" , Leo Yan , Song Liu , Miguel Ojeda , Liam Howlett , Ilkka Koskinen , Ben Gainey , K Prateek Nayak , Kan Liang , Yanteng Si , Ravi Bangoria , Sun Haiyong , Changbin Du , Masami Hiramatsu , zhaimingbing , Paran Lee , Li Dong , elfring@users.sourceforge.net, Andi Kleen , Markus Elfring , Chengen Du , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@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.396.g6e790dbe36-goog