Received: by 2002:ab2:7b86:0:b0:1f7:5705:b850 with SMTP id q6csp605255lqh; Sat, 4 May 2024 14:39:37 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVFasEzZT8t/7Tv1QtChJjkjSj327vYK1u8c/IAGGb+TtprunNcj8v0dl+SuZnncB6Z/zug9OlgyT3GLQJYNRdLCUOv2v3aJ3HHe/U1Ug== X-Google-Smtp-Source: AGHT+IG7ZDUBpaE6JRsfGOCRy8RDjlSjqDQZKvwq21lkhFP5XYOTX4WUzgsnK1D0FMrzKVZLXE/V X-Received: by 2002:a17:903:240a:b0:1e6:40f1:93be with SMTP id e10-20020a170903240a00b001e640f193bemr7823828plo.27.1714858777584; Sat, 04 May 2024 14:39:37 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714858777; cv=pass; d=google.com; s=arc-20160816; b=cRlmRvrG5uJIq8cAawEo1gnt2RbWuFnjbxwFTjbOOVkUnBrlrE9u22ehVzgQMo203a E+NzOehdNb/Ai9yiTcB2BGZRZsd1wTNkGGvSAQI2V55yUpL3ZoEugFS4GPYOeOMWYZXU 86jQtYq3ru+uPRbjDX2F6z9zVGSps5tS9W9CQzm3PCmHWXazmg+iZ8hJBvurnJi9I4IE /RAUPW3eeGzTzumspNWaSeyntwDDInuw+zHqThl2Shq2CL1MoqZRhY/R8qdUBHVWm/9i Uk8KxSpIxccUjyrk0JILZ5QpbHyYWfy+nMIiqv3r9tkBzeDfSo8+QiBhvUkI6rS+h1AM i0gg== 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=q472qqxF5ihtJmhy47OpIos2OiFBFiIxQhyS5d/9W5Q=; fh=QZH9+gOGrkN0YdCz4A9Iil93nGznwZW7urfDzVozcBc=; b=G681pqY94ukNMLkyYtBGCMkprDD5uLY38C89wLDM3irQP6d++S9A+vRP6jRUzk5EHq w3ziIDqV80sNJ+yIfz/vDm85CggQq8TwF9m5gq5nGR3H4zpg6odLyNgUmGBJI4Uqs3C3 5iVePfvX/FNp1U8QyP2M5V+vmP6958ez55bkl4nAHKWZ8jDctY9bYf/mf6vTavA4PKCZ X1Sg57YSjgMUSyqPNl4hXeRYXqxA7Pdf1ZhRYbdLYgf5FFfEyBcjt8v9E3HJo8J5Xd3f 3yte1pvh4o6uVcKGEdk3zwEZ8DpwGqk1cofr+eknSJw/4n3Vp7C+tRoVoiLMULngoiSm Sblw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=0+gO+xQE; 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-168827-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-168827-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id d11-20020a170902cecb00b001eb7a959ed9si5744795plg.125.2024.05.04.14.39.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 04 May 2024 14:39:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-168827-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=0+gO+xQE; 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-168827-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-168827-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 2A9A52821BB for ; Sat, 4 May 2024 21:39:37 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 091D685C65; Sat, 4 May 2024 21:38:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="0+gO+xQE" 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 31D7685937 for ; Sat, 4 May 2024 21:38:28 +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=1714858709; cv=none; b=VHvp+xzMoQIkAfBC0IWCUCG86UexAvi2RYhR7ojsP+5bM1QXrUgzsoWfB31YCoQTMAeMojcsW3Od8Zc4PhZbUM3oj592bTXQNxPjGCDsOD0xY1eEdF1+K3H68WNou36Q20ZNOSYAjga4RQqdv8hIZl+/+so1WhrzzI3VGB36c1U= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714858709; c=relaxed/simple; bh=EnPZ72Jy6PLP4dTT506sUjost8vtZko8wVVrrNdjs5c=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=qDVGPsxU1ueIz90Dg4VihafakR6BkP5qL5CBYyLfKLymGg7XGzzEptSGDtAMLOy+x9lkAUnyxkfialMtrQA7L6fMWwaNh+u3eWBcL9qw4xYbOzDDOl7bleo43Go8ewiRBvATAjnzLXWxe5J7UI80z9wHgOBJqs1f/kUgBFsbfcI= 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=0+gO+xQE; 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-de54ccab44aso1847951276.3 for ; Sat, 04 May 2024 14:38:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1714858707; x=1715463507; 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=q472qqxF5ihtJmhy47OpIos2OiFBFiIxQhyS5d/9W5Q=; b=0+gO+xQExGKZaU9vGhD+m8WC+xOhhgiUOX9pRRcyhLU2XX9gC3iCz1FMvg+oZS8BDQ NytP5GoUnvgri8MWUzlm6Zgha7TIuDuHUTRnCATYEP3SlEh55tl41cMpvoPWCdFih68M PWW2M4hN19hIpJwhaVX/66fHrt5T/s4Bz9wtYOvYqKNvITnXzIiCVMiafz1byOpL/AyU ignRsHtCHwRvrxvyLLX0j6ca0pe9hnRqtuwHOpBC4wrxmNe5QkGs5d+UkLXV+GLGMiZk bcemTsXIHGmT0tHWL/tS3WjiYAID6Pyb0PS4cZIg6S6Y2o+Yp4jd7n2kvxJ3uCcVuiq/ Jcag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714858707; x=1715463507; 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=q472qqxF5ihtJmhy47OpIos2OiFBFiIxQhyS5d/9W5Q=; b=K3XOcskv6JjTBJ6h5SsRe1f4T188fxCjU8gk5N80wBVlcTqL9jehyI4L79HrCtHPOs Zn6JJ7JWmK7QC8eFAvlIecp4IINCWRlLoVKjoWRFo1QgqB9ZSGehaD/ap3YPxRqDO6Av 4okU4MUNBH/crKvHu2OqRJJu/kWE4+IdJS0V5DdFtuA6UJp3VW2zF3Z4ZOuK2ZMP6Adm 1BBdzuXPL88Pm500y2D32+HApTyWOWHMJa8J71vWGXFYH8Lc0i2BZL/SN8ebU1bQC740 n3549xBm+/N7fSqtKq4zh0DFY5pLVu7T7hMt3hcFEG4uanYZtfyDFnxJ73rdMJUfAbDO +dBQ== X-Forwarded-Encrypted: i=1; AJvYcCU5B/n6pgVUDhgZ5JwvpMDAkadiDzrpX65Cj1nkweeb+oL4hsxH/hxL5cPdlvvrVU18U8fu5xLpbVGghhbfY3Rkk5VJeGJANonuV252 X-Gm-Message-State: AOJu0YweWjyN7aBqZEzD9hHHLPNgIGoVS4agc5de4XtWYcpkzjMQcEz4 dBC0yp0JtNT/SYXB+Jx0ibCkpcgLyPoQi09kN25Tw0xTPKaYqmQCswRdvIWu5Bvcmmg9alRPJkH e7hXJuw== X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:566d:1f52:f5f5:c8ec]) (user=irogers job=sendgmr) by 2002:a25:c708:0:b0:dc9:c54e:c5eb with SMTP id w8-20020a25c708000000b00dc9c54ec5ebmr1895377ybe.7.1714858707184; Sat, 04 May 2024 14:38:27 -0700 (PDT) Date: Sat, 4 May 2024 14:38:03 -0700 In-Reply-To: <20240504213803.218974-1-irogers@google.com> Message-Id: <20240504213803.218974-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: <20240504213803.218974-1-irogers@google.com> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog Subject: [PATCH v6 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.45.0.rc1.225.g2a3ae87e7f-goog