From nobody Sun Dec 28 17:30:29 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 A56E3C4167B for ; Thu, 7 Dec 2023 01:19:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1441997AbjLGBTU (ORCPT ); Wed, 6 Dec 2023 20:19:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47756 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1443050AbjLGBSu (ORCPT ); Wed, 6 Dec 2023 20:18:50 -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 0264610CC for ; Wed, 6 Dec 2023 17:18:26 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5d3f951af5aso2108547b3.0 for ; Wed, 06 Dec 2023 17:18:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701911905; x=1702516705; 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=rAO+eOAptizS+gjlcY4fuyNy/rOmCC4yvFDJwj5PiGs=; b=3wATpWulWRPFSvLKqC91++mEnyoMV0QkfxK5pYINRFiC+b5h2S7c9+Ds1u5WnAJ81e 0shicjMLr9vRsTiVUZdsN+DH0cSEjzfvwTCKGzbpRzBrsmICFeO+nxbct7+GTao8GbJJ ib3+UVWp3m85wasMCErgkdSUtX4QjbNeTumerpLoNVOnLc9HyixZ2pe2UpApjDlmPPz5 pfkl+F1wiTJZZ0DEtGCaXmN8B1TZzmzm5oItDDvQA1uuvZ1uTvY8xcVnC9Ad2dAttVQb WFLOocyO/4sNUO7fUOT53+EUVnLeBsi13aRLdQbs11C9iGVtPIvSuN3Aficz6MbUipB7 +6ng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701911905; x=1702516705; 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=rAO+eOAptizS+gjlcY4fuyNy/rOmCC4yvFDJwj5PiGs=; b=N/56OTnQcN3kfWfWho+X2LoYtmIeIiD+6xPKA47vqZzI0X32u1pSvz+wNH2vA7lRr1 SUfJkt9juNX5S4vGypyJE5HpUa+tDE+jcLsEPPuoADNU6b4TAH3qcFQgqEWrC3WpzrD2 jrtlVgpdsUNy/cV08vA5194w69Vx8hpVXWo8AMOjSek+xtpJVBtKhxPXW1pw0/NQfe7o a+zC8ThJEW1rp07wDHba5xITa7YHJnJLJy+pj3QEzJUccbVHFfYu0jca2nb2tM8JCXBm 26dI6YAEblhwYo8apaeUCPKW3RsBgkhAAOXpsi3ayX4N2s+aYEzX1XmLXSOskPJg+tIt uvJw== X-Gm-Message-State: AOJu0YwC0ST58up751vxuVo/scKW4jqEnwUD/ba/h2qovSnA6OsxKIjU gjJ6tO1vAS7VSVNiE09ru+NwhxWPlaH7 X-Google-Smtp-Source: AGHT+IHY1cG5ydWTlnAxz6hUcg9NjthxHS+I0UoE6tM6dc+N1PyHvb8AizjLkQSBoKJeCUMTD79Eghqhh1R2 X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:35bf:293e:7696:34e9]) (user=irogers job=sendgmr) by 2002:a05:690c:408d:b0:5d4:ce2:e90b with SMTP id gb13-20020a05690c408d00b005d40ce2e90bmr24930ywb.7.1701911905420; Wed, 06 Dec 2023 17:18:25 -0800 (PST) Date: Wed, 6 Dec 2023 17:16:59 -0800 In-Reply-To: <20231207011722.1220634-1-irogers@google.com> Message-Id: <20231207011722.1220634-26-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 25/47] perf maps: Get map before returning in maps__find_by_name 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" Finding a map is done under a lock, returning the map without a reference count means it can be removed without notice and causing uses after free. Grab a reference count to the map within the lock region and return this. Fix up locations that need a map__put following this. Also fix some reference counted pointer comparisons. Signed-off-by: Ian Rogers --- tools/perf/tests/vmlinux-kallsyms.c | 5 +++-- tools/perf/util/machine.c | 6 ++++-- tools/perf/util/maps.c | 6 +++--- tools/perf/util/probe-event.c | 1 + tools/perf/util/symbol-elf.c | 4 +++- tools/perf/util/symbol.c | 18 +++++++++++------- 6 files changed, 25 insertions(+), 15 deletions(-) diff --git a/tools/perf/tests/vmlinux-kallsyms.c b/tools/perf/tests/vmlinux= -kallsyms.c index e808e6fc8f76..fecbf851bb2e 100644 --- a/tools/perf/tests/vmlinux-kallsyms.c +++ b/tools/perf/tests/vmlinux-kallsyms.c @@ -131,9 +131,10 @@ static int test__vmlinux_matches_kallsyms_cb1(struct m= ap *map, void *data) struct map *pair =3D maps__find_by_name(args->kallsyms.kmaps, (dso->kernel ? dso->short_name : dso->name)); =20 - if (pair) + if (pair) { map__set_priv(pair, 1); - else { + map__put(pair); + } else { if (!args->header_printed) { pr_info("WARN: Maps only in vmlinux:\n"); args->header_printed =3D true; diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index e8eb9f0b073f..7031f6fddcae 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -1537,8 +1537,10 @@ static int maps__set_module_path(struct maps *maps, = const char *path, struct kmo return 0; =20 long_name =3D strdup(path); - if (long_name =3D=3D NULL) + if (long_name =3D=3D NULL) { + map__put(map); return -ENOMEM; + } =20 dso =3D map__dso(map); dso__set_long_name(dso, long_name, true); @@ -1552,7 +1554,7 @@ static int maps__set_module_path(struct maps *maps, c= onst char *path, struct kmo dso->symtab_type++; dso->comp =3D m->comp; } - + map__put(map); return 0; } =20 diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index 17aa894721a7..b85147cc8723 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -886,7 +886,7 @@ struct map *maps__find_by_name(struct maps *maps, const= char *name) struct dso *dso =3D map__dso(maps__maps_by_name(maps)[i]); =20 if (dso && strcmp(dso->short_name, name) =3D=3D 0) { - result =3D maps__maps_by_name(maps)[i]; // TODO: map__get + result =3D map__get(maps__maps_by_name(maps)[i]); done =3D true; } } @@ -898,7 +898,7 @@ struct map *maps__find_by_name(struct maps *maps, const= char *name) sizeof(*mapp), map__strcmp_name); =20 if (mapp) { - result =3D *mapp; // TODO: map__get + result =3D map__get(*mapp); i =3D mapp - maps__maps_by_name(maps); RC_CHK_ACCESS(maps)->last_search_by_name_idx =3D i; } @@ -923,7 +923,7 @@ struct map *maps__find_by_name(struct maps *maps, const= char *name) struct dso *dso =3D map__dso(pos); =20 if (dso && strcmp(dso->short_name, name) =3D=3D 0) { - result =3D pos; // TODO: map__get + result =3D map__get(pos); break; } } diff --git a/tools/perf/util/probe-event.c b/tools/perf/util/probe-event.c index a1a796043691..be71abe8b9b0 100644 --- a/tools/perf/util/probe-event.c +++ b/tools/perf/util/probe-event.c @@ -358,6 +358,7 @@ static int kernel_get_module_dso(const char *module, st= ruct dso **pdso) map =3D maps__find_by_name(machine__kernel_maps(host_machine), module_na= me); if (map) { dso =3D map__dso(map); + map__put(map); goto found; } pr_debug("Failed to find module %s.\n", module); diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c index 4b934ed3bfd1..5990e3fabdb5 100644 --- a/tools/perf/util/symbol-elf.c +++ b/tools/perf/util/symbol-elf.c @@ -1470,8 +1470,10 @@ static int dso__process_kernel_symbol(struct dso *ds= o, struct map *map, dso__set_loaded(curr_dso); *curr_mapp =3D curr_map; *curr_dsop =3D curr_dso; - } else + } else { *curr_dsop =3D map__dso(curr_map); + map__put(curr_map); + } =20 return 0; } diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index 1710b89e207c..0785a54e832e 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -814,7 +814,7 @@ static int maps__split_kallsyms(struct maps *kmaps, str= uct dso *dso, u64 delta, struct map *initial_map) { struct machine *machine; - struct map *curr_map =3D initial_map; + struct map *curr_map =3D map__get(initial_map); struct symbol *pos; int count =3D 0, moved =3D 0; struct rb_root_cached *root =3D &dso->symbols; @@ -858,13 +858,14 @@ static int maps__split_kallsyms(struct maps *kmaps, s= truct dso *dso, u64 delta, dso__set_loaded(curr_map_dso); } =20 + map__zput(curr_map); curr_map =3D maps__find_by_name(kmaps, module); if (curr_map =3D=3D NULL) { pr_debug("%s/proc/{kallsyms,modules} " "inconsistency while looking " "for \"%s\" module!\n", machine->root_dir, module); - curr_map =3D initial_map; + curr_map =3D map__get(initial_map); goto discard_symbol; } curr_map_dso =3D map__dso(curr_map); @@ -888,7 +889,7 @@ static int maps__split_kallsyms(struct maps *kmaps, str= uct dso *dso, u64 delta, * symbols at this point. */ goto discard_symbol; - } else if (curr_map !=3D initial_map) { + } else if (!RC_CHK_EQUAL(curr_map, initial_map)) { char dso_name[PATH_MAX]; struct dso *ndso; =20 @@ -899,7 +900,8 @@ static int maps__split_kallsyms(struct maps *kmaps, str= uct dso *dso, u64 delta, } =20 if (count =3D=3D 0) { - curr_map =3D initial_map; + map__zput(curr_map); + curr_map =3D map__get(initial_map); goto add_symbol; } =20 @@ -913,6 +915,7 @@ static int maps__split_kallsyms(struct maps *kmaps, str= uct dso *dso, u64 delta, kernel_range++); =20 ndso =3D dso__new(dso_name); + map__zput(curr_map); if (ndso =3D=3D NULL) return -1; =20 @@ -926,6 +929,7 @@ static int maps__split_kallsyms(struct maps *kmaps, str= uct dso *dso, u64 delta, =20 map__set_mapping_type(curr_map, MAPPING_TYPE__IDENTITY); if (maps__insert(kmaps, curr_map)) { + map__zput(curr_map); dso__put(ndso); return -1; } @@ -936,7 +940,7 @@ static int maps__split_kallsyms(struct maps *kmaps, str= uct dso *dso, u64 delta, pos->end -=3D delta; } add_symbol: - if (curr_map !=3D initial_map) { + if (!RC_CHK_EQUAL(curr_map, initial_map)) { struct dso *curr_map_dso =3D map__dso(curr_map); =20 rb_erase_cached(&pos->rb_node, root); @@ -951,12 +955,12 @@ static int maps__split_kallsyms(struct maps *kmaps, s= truct dso *dso, u64 delta, symbol__delete(pos); } =20 - if (curr_map !=3D initial_map && + if (!RC_CHK_EQUAL(curr_map, initial_map) && dso->kernel =3D=3D DSO_SPACE__KERNEL_GUEST && machine__is_default_guest(maps__machine(kmaps))) { dso__set_loaded(map__dso(curr_map)); } - + map__put(curr_map); return count + moved; } =20 --=20 2.43.0.rc2.451.g8631bc7472-goog