Received: by 2002:a05:7412:b795:b0:e2:908c:2ebd with SMTP id iv21csp559335rdb; Thu, 2 Nov 2023 11:06:43 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEYwegsOMOW7ZUrQqgkI//PDAHPsiu2gDcqTULtb7lch3u35Nex1cnce+LofavE6pwQZxFm X-Received: by 2002:a05:6300:8085:b0:180:d45e:727c with SMTP id ap5-20020a056300808500b00180d45e727cmr9939748pzc.46.1698948403256; Thu, 02 Nov 2023 11:06:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698948403; cv=none; d=google.com; s=arc-20160816; b=Cja8u7vnX2s2D5speFachbhFpowwRIYbVk/MxWAFx1XQY7+AufrV6IzLj3Ko/dsLFd 0B037Bd0hpb79i0UzzJ4bl23qxQrfvkCbK0hDpGAKYy4t7oEgT5E1dxYXKxDeBMpbpof 71KCjL7mxHi2RI/Ts0nr0KS+U3pY4Cn/2/zJNlDPeYMtW7ciDnZhZouhOUqFCjObHrY3 3Qp+lurGIvI/BPzBX3nWTk55Gj0AaouxILP/DN5LXFIZ7Ah/Qyof72iYZOeQxtkrdukr CwW1wWUGbOjUEk/xftORya7BLqhNSBR7VWa4iVrDrrtqCbqXAO9C5xljw2MAvZmFc8VT /cYA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=YYGdo15y2jW5NBV57rBqS7hVRUDLmlDHwihGlduFalw=; fh=z1KcSqUpYQ9oC4uLSeIkhAYTnJ2bvP0QbNW2xqV5NqA=; b=tiYo8ZEt5zEkMbeJYh9qRGn0s+wf5wR/9/KqbJwf9XWKenYxBRWBNJanV3EPlBWTym FDjLfkBegFVp4+upnyeg7YatOaG1VYh8EuNADFpyzKGHFarMc2iy/3nMcE1s464ZEqDL s8C4r0yZmYwydIb1NVwbLj8T4GGz4g5ZxiEpjG2jjkGTQIWAv7uDTJbt7RKNndLBV74M 1oXHF2K5l3RBZV8MpQDpeU2b+Dl00cO9aALVRN7zZybf52McZVA6Q4Uozo8ZdRQLLIB2 kKpOH9m6dgb//D/1bOBmytfG+UZ9tv30thkEfCdOz6Qv0TV1UdevwG20sdRLTze9VNZ/ ozEA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=xP6I2j1k; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id cl23-20020a056a02099700b005b91192c61dsi75602pgb.217.2023.11.02.11.06.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Nov 2023 11:06:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=xP6I2j1k; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 39BF8809220F; Thu, 2 Nov 2023 11:05:28 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235595AbjKBSFA (ORCPT + 99 others); Thu, 2 Nov 2023 14:05:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45382 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234522AbjKBSEe (ORCPT ); Thu, 2 Nov 2023 14:04:34 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CC2622101 for ; Thu, 2 Nov 2023 10:59:35 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5ae5b12227fso16766257b3.0 for ; Thu, 02 Nov 2023 10:59:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698947975; x=1699552775; 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=YYGdo15y2jW5NBV57rBqS7hVRUDLmlDHwihGlduFalw=; b=xP6I2j1kTLptZwC3Qge5qr3AZNq38fjpXKvj3f8l/vJXlCnQY2WWFwI9rWjYifsGSI G3tKrqnKvN5IeWR8NISiVyHsxSh1srdGZVK5gvKpAArW3cmKOYX6VH42sSe9NiKQWjRG zLDc/hnBuKocTqGDrh6tw5HAkdn3NuDxi5G6eGe9Foo83QEc+PRM5U2InwcAo7fouAuX cc3E8AzAGOPAxRap5tRbgGZ8h/VZYyOHIWUSEVaapfjfYEUffFsT5Atifs6gzkr61tJU l8iNa6pRVhTiwy7yf2IHZy5KA66YZ1N1DpcR5Gtt1Avx8UZw2A11ZZM2xCzrB1N5+boo EhuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698947975; x=1699552775; 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=YYGdo15y2jW5NBV57rBqS7hVRUDLmlDHwihGlduFalw=; b=nNcLUyGOzG1M9914+yCaUWCKQCTJ4SkQjHMPOD2qSHO9X1UYHO+bVPAPZ7FKetM2Z4 VEF6GCF8PsVvuw972ys5yYq5llEqsAF5SRT50X+uQIU8VehhnLPhwQCRv+T60H8lEc03 wyGUPhYpaYghNTop4TrIWOEBhGVDpgIaIQHOsCdJHYCK/H9OKPKP1hUxp5ePDeXztsMr qC7xGc+ciA58m7tjXXpswHUr9Q0WDjEWXKmCGdZrnhYxXC6Af+Jg8r8EkxKft4ewjqaT jhxrGCFXQ7lUM/cPTKqzhlWijP7Bq3xeQmi0LYTVMrUnNskJjqHBOXKT7X1k4ScA11JE d+Xw== X-Gm-Message-State: AOJu0YzCLATl4XrXBeAH7JE8oFDRktHTAHlsubcB/Y9t/aXN/1+N83Zz 2HsjPhU5UlEkLD7CH3o06gOOvMcEx8NY X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:bb34:df9c:836c:afca]) (user=irogers job=sendgmr) by 2002:a81:6d8a:0:b0:5a1:d0cf:b65d with SMTP id i132-20020a816d8a000000b005a1d0cfb65dmr8475ywc.5.1698947974891; Thu, 02 Nov 2023 10:59:34 -0700 (PDT) Date: Thu, 2 Nov 2023 10:57:25 -0700 In-Reply-To: <20231102175735.2272696-1-irogers@google.com> Message-Id: <20231102175735.2272696-44-irogers@google.com> Mime-Version: 1.0 References: <20231102175735.2272696-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.869.gea05f2083d-goog Subject: [PATCH v4 43/53] perf dsos: Add dsos__for_each_dso 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 , Vincent Whitchurch , "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 , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Thu, 02 Nov 2023 11:05:28 -0700 (PDT) To better abstract the dsos internals, add dsos__for_each_dso that does a callback on each dso. This also means the read lock can be correctly held. Signed-off-by: Ian Rogers --- tools/perf/builtin-inject.c | 25 +++++++----- tools/perf/util/build-id.c | 76 ++++++++++++++++++++----------------- tools/perf/util/dsos.c | 16 ++++++++ tools/perf/util/dsos.h | 8 +--- tools/perf/util/machine.c | 40 +++++++++++-------- 5 files changed, 100 insertions(+), 65 deletions(-) diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c index ef73317e6ae7..ce5e28eaad90 100644 --- a/tools/perf/builtin-inject.c +++ b/tools/perf/builtin-inject.c @@ -1187,23 +1187,28 @@ static int synthesize_build_id(struct perf_inject *inject, struct dso *dso, pid_ process_build_id, machine); } +static int guest_session__add_build_ids_cb(struct dso *dso, void *data) +{ + struct guest_session *gs = data; + struct perf_inject *inject = container_of(gs, struct perf_inject, guest_session); + + if (!dso->has_build_id) + return 0; + + return synthesize_build_id(inject, dso, gs->machine_pid); + +} + static int guest_session__add_build_ids(struct guest_session *gs) { struct perf_inject *inject = container_of(gs, struct perf_inject, guest_session); - struct machine *machine = &gs->session->machines.host; - struct dso *dso; - int ret; /* Build IDs will be put in the Build ID feature section */ perf_header__set_feat(&inject->session->header, HEADER_BUILD_ID); - dsos__for_each_with_build_id(dso, &machine->dsos.head) { - ret = synthesize_build_id(inject, dso, gs->machine_pid); - if (ret) - return ret; - } - - return 0; + return dsos__for_each_dso(&gs->session->machines.host.dsos, + guest_session__add_build_ids_cb, + gs); } static int guest_session__ksymbol_event(struct perf_tool *tool, diff --git a/tools/perf/util/build-id.c b/tools/perf/util/build-id.c index a617b1917e6b..a6d3c253f19f 100644 --- a/tools/perf/util/build-id.c +++ b/tools/perf/util/build-id.c @@ -327,48 +327,56 @@ static int write_buildid(const char *name, size_t name_len, struct build_id *bid return write_padded(fd, name, name_len + 1, len); } -static int machine__write_buildid_table(struct machine *machine, - struct feat_fd *fd) +struct machine__write_buildid_table_cb_args { + struct machine *machine; + struct feat_fd *fd; + u16 kmisc, umisc; +}; + +static int machine__write_buildid_table_cb(struct dso *dso, void *data) { - int err = 0; - struct dso *pos; - u16 kmisc = PERF_RECORD_MISC_KERNEL, - umisc = PERF_RECORD_MISC_USER; + struct machine__write_buildid_table_cb_args *args = data; + const char *name; + size_t name_len; + bool in_kernel = false; - if (!machine__is_host(machine)) { - kmisc = PERF_RECORD_MISC_GUEST_KERNEL; - umisc = PERF_RECORD_MISC_GUEST_USER; - } + if (!dso->has_build_id) + return 0; - dsos__for_each_with_build_id(pos, &machine->dsos.head) { - const char *name; - size_t name_len; - bool in_kernel = false; + if (!dso->hit && !dso__is_vdso(dso)) + return 0; - if (!pos->hit && !dso__is_vdso(pos)) - continue; + if (dso__is_vdso(dso)) { + name = dso->short_name; + name_len = dso->short_name_len; + } else if (dso__is_kcore(dso)) { + name = args->machine->mmap_name; + name_len = strlen(name); + } else { + name = dso->long_name; + name_len = dso->long_name_len; + } - if (dso__is_vdso(pos)) { - name = pos->short_name; - name_len = pos->short_name_len; - } else if (dso__is_kcore(pos)) { - name = machine->mmap_name; - name_len = strlen(name); - } else { - name = pos->long_name; - name_len = pos->long_name_len; - } + in_kernel = dso->kernel || is_kernel_module(name, PERF_RECORD_MISC_CPUMODE_UNKNOWN); + return write_buildid(name, name_len, &dso->bid, args->machine->pid, + in_kernel ? args->kmisc : args->umisc, args->fd); +} - in_kernel = pos->kernel || - is_kernel_module(name, - PERF_RECORD_MISC_CPUMODE_UNKNOWN); - err = write_buildid(name, name_len, &pos->bid, machine->pid, - in_kernel ? kmisc : umisc, fd); - if (err) - break; +static int machine__write_buildid_table(struct machine *machine, struct feat_fd *fd) +{ + struct machine__write_buildid_table_cb_args args = { + .machine = machine, + .fd = fd, + .kmisc = PERF_RECORD_MISC_KERNEL, + .umisc = PERF_RECORD_MISC_USER, + }; + + if (!machine__is_host(machine)) { + args.kmisc = PERF_RECORD_MISC_GUEST_KERNEL; + args.umisc = PERF_RECORD_MISC_GUEST_USER; } - return err; + return dsos__for_each_dso(&machine->dsos, machine__write_buildid_table_cb, &args); } int perf_session__write_buildid_table(struct perf_session *session, diff --git a/tools/perf/util/dsos.c b/tools/perf/util/dsos.c index d269e09005a7..d43f64939b12 100644 --- a/tools/perf/util/dsos.c +++ b/tools/perf/util/dsos.c @@ -433,3 +433,19 @@ struct dso *dsos__find_kernel_dso(struct dsos *dsos) up_read(&dsos->lock); return res; } + +int dsos__for_each_dso(struct dsos *dsos, int (*cb)(struct dso *dso, void *data), void *data) +{ + struct dso *dso; + + down_read(&dsos->lock); + list_for_each_entry(dso, &dsos->head, node) { + int err; + + err = cb(dso, data); + if (err) + return err; + } + up_read(&dsos->lock); + return 0; +} diff --git a/tools/perf/util/dsos.h b/tools/perf/util/dsos.h index a7c7f723c5ff..317a263f0e37 100644 --- a/tools/perf/util/dsos.h +++ b/tools/perf/util/dsos.h @@ -23,12 +23,6 @@ struct dsos { struct rw_semaphore lock; }; -#define dsos__for_each_with_build_id(pos, head) \ - list_for_each_entry(pos, head, node) \ - if (!pos->has_build_id) \ - continue; \ - else - void dsos__init(struct dsos *dsos); void dsos__exit(struct dsos *dsos); @@ -55,4 +49,6 @@ struct dso *dsos__findnew_module_dso(struct dsos *dsos, struct machine *machine, struct dso *dsos__find_kernel_dso(struct dsos *dsos); +int dsos__for_each_dso(struct dsos *dsos, int (*cb)(struct dso *dso, void *data), void *data); + #endif /* __PERF_DSOS */ diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 06026a1b2d1a..6505f8c2cecc 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -1558,16 +1558,14 @@ int machine__create_kernel_maps(struct machine *machine) return ret; } -static bool machine__uses_kcore(struct machine *machine) +static int machine__uses_kcore_cb(struct dso *dso, void *data __maybe_unused) { - struct dso *dso; - - list_for_each_entry(dso, &machine->dsos.head, node) { - if (dso__is_kcore(dso)) - return true; - } + return dso__is_kcore(dso) ? 1 : 0; +} - return false; +static bool machine__uses_kcore(struct machine *machine) +{ + return dsos__for_each_dso(&machine->dsos, machine__uses_kcore_cb, NULL) != 0 ? true : false; } static bool perf_event__is_extra_kernel_mmap(struct machine *machine, @@ -3133,16 +3131,28 @@ char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, ch return sym->name; } +struct machine__for_each_dso_cb_args { + struct machine *machine; + machine__dso_t fn; + void *priv; +}; + +static int machine__for_each_dso_cb(struct dso *dso, void *data) +{ + struct machine__for_each_dso_cb_args *args = data; + + return args->fn(dso, args->machine, args->priv); +} + int machine__for_each_dso(struct machine *machine, machine__dso_t fn, void *priv) { - struct dso *pos; - int err = 0; + struct machine__for_each_dso_cb_args args = { + .machine = machine, + .fn = fn, + .priv = priv, + }; - list_for_each_entry(pos, &machine->dsos.head, node) { - if (fn(pos, machine, priv)) - err = -1; - } - return err; + return dsos__for_each_dso(&machine->dsos, machine__for_each_dso_cb, &args); } int machine__for_each_kernel_map(struct machine *machine, machine__map_t fn, void *priv) -- 2.42.0.869.gea05f2083d-goog