From nobody Sun Dec 28 17:29:11 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F0A47C4167B for ; Thu, 7 Dec 2023 01:20:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1442983AbjLGBU0 (ORCPT ); Wed, 6 Dec 2023 20:20:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46482 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1443120AbjLGBTd (ORCPT ); Wed, 6 Dec 2023 20:19:33 -0500 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C6F0D270A for ; Wed, 6 Dec 2023 17:18:54 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5ca61d84dc3so1504187b3.0 for ; Wed, 06 Dec 2023 17:18:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701911933; x=1702516733; 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=apWGIZ6GCbU4S5kS4f8lVvIQ3hBhTPwuIopNv/YOhEw=; b=LJwf7YLLrCxK+SgqkFN8FpI/d23HuLpRMuqYmbZS0AWL8EqQjwXQSyHbgbRuK6Swm9 DKzhRY49lOI9fTc8tikLCKXddWJGQnv7lXdr2TLHkGW716dPxo3QyDempqvU91dqvS7y DKO+uWuoHzp8YYbY45bpWdda63xK/3CZhUOOmVvnxQfGLKD/8KZWH9A3V/4W8nPpCVDb iNtCYeEErZmHoY08OwV4vQ/mo7qN5oGIlPhjeljz0N8dCX1U2qRs6RzwELeGuo+85wak 5HHHvgsvKd7hc1ShJ4U25j0/TAcuZ6Qli7iXG2vjtXyuPqsH1+z6dsBAxnmKO6atQbOw CpLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701911933; x=1702516733; 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=apWGIZ6GCbU4S5kS4f8lVvIQ3hBhTPwuIopNv/YOhEw=; b=NGqQsksnZm6/IY9PJowcL2B/YJ5mCpBkAzCvPb47PMH/Yf/pd6VEGIvcpbJmDMOJBW +dVFSyDoidoUyS1H8FoQTuR4xlBP3Ay2xcr+ZikYAKc9fc4xbb+2ZTLWeDO9AyL2j4oM dExDgSW9XhnGg2uwRQ31gtUZaqqPkF9D74gAICzCFQ5ZTRbLJTICGllOiHnaGbw99hrk 8DawWdFrrD4FUAV/NvhLiijhQk8fqjh0WLxhZNKg19SkjD8KAh6PzuhLUf86vv+D+q77 4fQuRFLCjfYBCbYpU7LuP5Ns7RWifsaiEU1PqzwoXlfAvorZ850OouY8dVVl8bfhWutb G1yw== X-Gm-Message-State: AOJu0Yx3yLmjlc24acUlQyIab47a85v2n3d5Rzqp5SxvcHiRPiG4N2M0 YFwGUelr6laG9+SO/gvzDecJm1zPFaD6 X-Google-Smtp-Source: AGHT+IFFnA7lXf+8YLJPd9JVbiLW+oM3d2aFlG3/277d2WAA78PMjI5HCex2lUx+v7Qj2YAPIoqGPy852z0a X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:35bf:293e:7696:34e9]) (user=irogers job=sendgmr) by 2002:a25:acc8:0:b0:dbc:1b48:b06f with SMTP id x8-20020a25acc8000000b00dbc1b48b06fmr27246ybd.2.1701911933210; Wed, 06 Dec 2023 17:18:53 -0800 (PST) Date: Wed, 6 Dec 2023 17:17:11 -0800 In-Reply-To: <20231207011722.1220634-1-irogers@google.com> Message-Id: <20231207011722.1220634-38-irogers@google.com> Mime-Version: 1.0 References: <20231207011722.1220634-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.rc2.451.g8631bc7472-goog Subject: [PATCH v6 37/47] perf dsos: Tidy reference counting and locking 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, Guilherme Amadio Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move more functionality into dsos.c generally from machine, renaming functions to match their new usage. The find function is made to always "get" before returning a dso. Reduce the scope of locks in vdso to match the locking paradigm. Signed-off-by: Ian Rogers --- tools/perf/util/dsos.c | 73 +++++++++++++++++++++++++++++++++++---- tools/perf/util/dsos.h | 9 ++++- tools/perf/util/machine.c | 62 ++------------------------------- tools/perf/util/map.c | 4 +-- tools/perf/util/vdso.c | 48 +++++++++++-------------- 5 files changed, 97 insertions(+), 99 deletions(-) diff --git a/tools/perf/util/dsos.c b/tools/perf/util/dsos.c index e65ef6762bed..d269e09005a7 100644 --- a/tools/perf/util/dsos.c +++ b/tools/perf/util/dsos.c @@ -181,7 +181,7 @@ struct dso *__dsos__findnew_link_by_longname_id(struct = rb_root *root, struct dso * at the end of the list of duplicates. */ if (!dso || (dso =3D=3D this)) - return this; /* Find matching dso */ + return dso__get(this); /* Find matching dso */ /* * The core kernel DSOs may have duplicated long name. * In this case, the short name should be different. @@ -253,15 +253,20 @@ static struct dso *__dsos__find_id(struct dsos *dsos,= const char *name, struct d if (cmp_short) { list_for_each_entry(pos, &dsos->head, node) if (__dso__cmp_short_name(name, id, pos) =3D=3D 0) - return pos; + return dso__get(pos); return NULL; } return __dsos__findnew_by_longname_id(&dsos->root, name, id); } =20 -struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_sho= rt) +struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short) { - return __dsos__find_id(dsos, name, NULL, cmp_short); + struct dso *res; + + down_read(&dsos->lock); + res =3D __dsos__find_id(dsos, name, NULL, cmp_short); + up_read(&dsos->lock); + return res; } =20 static void dso__set_basename(struct dso *dso) @@ -303,8 +308,6 @@ static struct dso *__dsos__addnew_id(struct dsos *dsos,= const char *name, struct if (dso !=3D NULL) { __dsos__add(dsos, dso); dso__set_basename(dso); - /* Put dso here because __dsos_add already got it */ - dso__put(dso); } return dso; } @@ -328,7 +331,7 @@ struct dso *dsos__findnew_id(struct dsos *dsos, const c= har *name, struct dso_id { struct dso *dso; down_write(&dsos->lock); - dso =3D dso__get(__dsos__findnew_id(dsos, name, id)); + dso =3D __dsos__findnew_id(dsos, name, id); up_write(&dsos->lock); return dso; } @@ -374,3 +377,59 @@ int __dsos__hit_all(struct dsos *dsos) =20 return 0; } + +struct dso *dsos__findnew_module_dso(struct dsos *dsos, + struct machine *machine, + struct kmod_path *m, + const char *filename) +{ + struct dso *dso; + + down_write(&dsos->lock); + + dso =3D __dsos__find_id(dsos, m->name, NULL, /*cmp_short=3D*/true); + if (!dso) { + dso =3D __dsos__addnew(dsos, m->name); + if (dso =3D=3D NULL) + goto out_unlock; + + dso__set_module_info(dso, m, machine); + dso__set_long_name(dso, strdup(filename), true); + dso->kernel =3D DSO_SPACE__KERNEL; + } + +out_unlock: + up_write(&dsos->lock); + return dso; +} + +struct dso *dsos__find_kernel_dso(struct dsos *dsos) +{ + struct dso *dso, *res =3D NULL; + + down_read(&dsos->lock); + list_for_each_entry(dso, &dsos->head, node) { + /* + * The cpumode passed to is_kernel_module is not the cpumode of + * *this* event. If we insist on passing correct cpumode to + * is_kernel_module, we should record the cpumode when we adding + * this dso to the linked list. + * + * However we don't really need passing correct cpumode. We + * know the correct cpumode must be kernel mode (if not, we + * should not link it onto kernel_dsos list). + * + * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN. + * is_kernel_module() treats it as a kernel cpumode. + */ + if (!dso->kernel || + is_kernel_module(dso->long_name, + PERF_RECORD_MISC_CPUMODE_UNKNOWN)) + continue; + + res =3D dso__get(dso); + break; + } + up_read(&dsos->lock); + return res; +} diff --git a/tools/perf/util/dsos.h b/tools/perf/util/dsos.h index 1c81ddf07f8f..a7c7f723c5ff 100644 --- a/tools/perf/util/dsos.h +++ b/tools/perf/util/dsos.h @@ -10,6 +10,8 @@ =20 struct dso; struct dso_id; +struct kmod_path; +struct machine; =20 /* * DSOs are put into both a list for fast iteration and rbtree for fast @@ -33,7 +35,7 @@ void dsos__exit(struct dsos *dsos); void __dsos__add(struct dsos *dsos, struct dso *dso); void dsos__add(struct dsos *dsos, struct dso *dso); struct dso *__dsos__addnew(struct dsos *dsos, const char *name); -struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_sho= rt); +struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short= ); =20 struct dso *dsos__findnew_id(struct dsos *dsos, const char *name, struct d= so_id *id); =20 @@ -48,4 +50,9 @@ size_t __dsos__fprintf(struct dsos *dsos, FILE *fp); =20 int __dsos__hit_all(struct dsos *dsos); =20 +struct dso *dsos__findnew_module_dso(struct dsos *dsos, struct machine *ma= chine, + struct kmod_path *m, const char *filename); + +struct dso *dsos__find_kernel_dso(struct dsos *dsos); + #endif /* __PERF_DSOS */ diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index d235d65fb35b..8d0ea17e432a 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -645,31 +645,6 @@ int machine__process_lost_samples_event(struct machine= *machine __maybe_unused, return 0; } =20 -static struct dso *machine__findnew_module_dso(struct machine *machine, - struct kmod_path *m, - const char *filename) -{ - struct dso *dso; - - down_write(&machine->dsos.lock); - - dso =3D __dsos__find(&machine->dsos, m->name, true); - if (!dso) { - dso =3D __dsos__addnew(&machine->dsos, m->name); - if (dso =3D=3D NULL) - goto out_unlock; - - dso__set_module_info(dso, m, machine); - dso__set_long_name(dso, strdup(filename), true); - dso->kernel =3D DSO_SPACE__KERNEL; - } - - dso__get(dso); -out_unlock: - up_write(&machine->dsos.lock); - return dso; -} - int machine__process_aux_event(struct machine *machine __maybe_unused, union perf_event *event) { @@ -853,7 +828,7 @@ static struct map *machine__addnew_module_map(struct ma= chine *machine, u64 start if (kmod_path__parse_name(&m, filename)) return NULL; =20 - dso =3D machine__findnew_module_dso(machine, &m, filename); + dso =3D dsos__findnew_module_dso(&machine->dsos, machine, &m, filename); if (dso =3D=3D NULL) goto out; =20 @@ -1662,40 +1637,7 @@ static int machine__process_kernel_mmap_event(struct= machine *machine, * Should be there already, from the build-id table in * the header. */ - struct dso *kernel =3D NULL; - struct dso *dso; - - down_read(&machine->dsos.lock); - - list_for_each_entry(dso, &machine->dsos.head, node) { - - /* - * The cpumode passed to is_kernel_module is not the - * cpumode of *this* event. If we insist on passing - * correct cpumode to is_kernel_module, we should - * record the cpumode when we adding this dso to the - * linked list. - * - * However we don't really need passing correct - * cpumode. We know the correct cpumode must be kernel - * mode (if not, we should not link it onto kernel_dsos - * list). - * - * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN. - * is_kernel_module() treats it as a kernel cpumode. - */ - - if (!dso->kernel || - is_kernel_module(dso->long_name, - PERF_RECORD_MISC_CPUMODE_UNKNOWN)) - continue; - - - kernel =3D dso__get(dso); - break; - } - - up_read(&machine->dsos.lock); + struct dso *kernel =3D dsos__find_kernel_dso(&machine->dsos); =20 if (kernel =3D=3D NULL) kernel =3D machine__findnew_dso(machine, machine->mmap_name); diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c index cf5a15db3a1f..7c1fff9e413d 100644 --- a/tools/perf/util/map.c +++ b/tools/perf/util/map.c @@ -196,9 +196,7 @@ struct map *map__new(struct machine *machine, u64 start= , u64 len, * reading the header will have the build ID set and all future mmaps w= ill * have it missing. */ - down_read(&machine->dsos.lock); - header_bid_dso =3D __dsos__find(&machine->dsos, filename, false); - up_read(&machine->dsos.lock); + header_bid_dso =3D dsos__find(&machine->dsos, filename, false); if (header_bid_dso && header_bid_dso->header_build_id) { dso__set_build_id(dso, &header_bid_dso->bid); dso->header_build_id =3D 1; diff --git a/tools/perf/util/vdso.c b/tools/perf/util/vdso.c index df8963796187..35532dcbff74 100644 --- a/tools/perf/util/vdso.c +++ b/tools/perf/util/vdso.c @@ -133,8 +133,6 @@ static struct dso *__machine__addnew_vdso(struct machin= e *machine, const char *s if (dso !=3D NULL) { __dsos__add(&machine->dsos, dso); dso__set_long_name(dso, long_name, false); - /* Put dso here because __dsos_add already got it */ - dso__put(dso); } =20 return dso; @@ -252,17 +250,15 @@ static struct dso *__machine__findnew_compat(struct m= achine *machine, const char *file_name; struct dso *dso; =20 - dso =3D __dsos__find(&machine->dsos, vdso_file->dso_name, true); + dso =3D dsos__find(&machine->dsos, vdso_file->dso_name, true); if (dso) - goto out; + return dso; =20 file_name =3D vdso__get_compat_file(vdso_file); if (!file_name) - goto out; + return NULL; =20 - dso =3D __machine__addnew_vdso(machine, vdso_file->dso_name, file_name); -out: - return dso; + return __machine__addnew_vdso(machine, vdso_file->dso_name, file_name); } =20 static int __machine__findnew_vdso_compat(struct machine *machine, @@ -308,21 +304,21 @@ static struct dso *machine__find_vdso(struct machine = *machine, dso_type =3D machine__thread_dso_type(machine, thread); switch (dso_type) { case DSO__TYPE_32BIT: - dso =3D __dsos__find(&machine->dsos, DSO__NAME_VDSO32, true); + dso =3D dsos__find(&machine->dsos, DSO__NAME_VDSO32, true); if (!dso) { - dso =3D __dsos__find(&machine->dsos, DSO__NAME_VDSO, - true); + dso =3D dsos__find(&machine->dsos, DSO__NAME_VDSO, + true); if (dso && dso_type !=3D dso__type(dso, machine)) dso =3D NULL; } break; case DSO__TYPE_X32BIT: - dso =3D __dsos__find(&machine->dsos, DSO__NAME_VDSOX32, true); + dso =3D dsos__find(&machine->dsos, DSO__NAME_VDSOX32, true); break; case DSO__TYPE_64BIT: case DSO__TYPE_UNKNOWN: default: - dso =3D __dsos__find(&machine->dsos, DSO__NAME_VDSO, true); + dso =3D dsos__find(&machine->dsos, DSO__NAME_VDSO, true); break; } =20 @@ -334,37 +330,33 @@ struct dso *machine__findnew_vdso(struct machine *mac= hine, { struct vdso_info *vdso_info; struct dso *dso =3D NULL; + char *file; =20 - down_write(&machine->dsos.lock); if (!machine->vdso_info) machine->vdso_info =3D vdso_info__new(); =20 vdso_info =3D machine->vdso_info; if (!vdso_info) - goto out_unlock; + return NULL; =20 dso =3D machine__find_vdso(machine, thread); if (dso) - goto out_unlock; + return dso; =20 #if BITS_PER_LONG =3D=3D 64 if (__machine__findnew_vdso_compat(machine, thread, vdso_info, &dso)) - goto out_unlock; + return dso; #endif =20 - dso =3D __dsos__find(&machine->dsos, DSO__NAME_VDSO, true); - if (!dso) { - char *file; + dso =3D dsos__find(&machine->dsos, DSO__NAME_VDSO, true); + if (dso) + return dso; =20 - file =3D get_file(&vdso_info->vdso); - if (file) - dso =3D __machine__addnew_vdso(machine, DSO__NAME_VDSO, file); - } + file =3D get_file(&vdso_info->vdso); + if (!file) + return NULL; =20 -out_unlock: - dso__get(dso); - up_write(&machine->dsos.lock); - return dso; + return __machine__addnew_vdso(machine, DSO__NAME_VDSO, file); } =20 bool dso__is_vdso(struct dso *dso) --=20 2.43.0.rc2.451.g8631bc7472-goog