From nobody Wed Dec 17 12:44:27 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 8F4E4C4167B for ; Mon, 27 Nov 2023 22:12:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234082AbjK0WMM (ORCPT ); Mon, 27 Nov 2023 17:12:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43256 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234249AbjK0WL3 (ORCPT ); Mon, 27 Nov 2023 17:11:29 -0500 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C73A426B3 for ; Mon, 27 Nov 2023 14:10:24 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-da31ec03186so3525907276.1 for ; Mon, 27 Nov 2023 14:10:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701123024; x=1701727824; 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=LI6DQBD9kp75ZZdLQrlvQ72uv7oEtpT8IbPmgcKaY6U=; b=XWHLe+a/ZCBf5LR4WTlwVammEelJAZjEH8lgx5Nq7cjC/ygs/Dh/rcRs9g+UTlewiQ rfhndJlKl82hTeogZhaqtc8mTDNqy6dcW2uDitfY8Bk+4TP7RyHWYVUYvJfXgdam2uJw OwwZztEyPsHnwV/NET8t2GkPiCbMODtGEkkW3HNN6TqWa2hRzLYStYVYA5IBgVauwkXO lEgBo9Elx3LowhrgkARQPPL4g3yYC9JzUQ+QUM64U9MdyM/tGD7rksDRvJr9CaNlTv/z dcc5yntTbqEPxKnO2xl7DFyhZXYtvYdEM4vsJhwvUDzavMau6B/GzPC5TYuCubCrbuS4 qWBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701123024; x=1701727824; 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=LI6DQBD9kp75ZZdLQrlvQ72uv7oEtpT8IbPmgcKaY6U=; b=eQPzHbozMxPGil2liZoU7ZobIZWNlX0lWetgDFzCcBpLc3O5GOhSxGJsvpc+njgGMc rNB80A+lTBlvnp+DxmgJe23FE+a11CIubYb0HtnQkAJy6h+ythTXGEcS8eYyriersaGM YqiX95yehUFDBwsUlJZeRgdSkCSeJ3YvqFR+zjp+zEFi80CP2oiXpqizcV8qKioWg2F0 vI14cGDJ6riS0sAGiZKQDmgqaOtJLpU7HTrbs0dzsgL3rfo2E3qgJacCbiHlPqKVLoa8 OFmHxZQi64yg0jnPC4Xf0EDKfQmkXkTk4FC2uEF2sAleBhnDny9a0ua1foyJQ2RCxJrJ rISA== X-Gm-Message-State: AOJu0Yza8wd/n8HXOrxmvMKBB2X0kNC1p+VC7b/+kKBAd+153QcaJt6B AG6JGG75f/KOAL+ImkPLcxCAGOKW/nsq X-Google-Smtp-Source: AGHT+IGjOkRQiBcNrvZvdwOy2oDjt2WwXB2e+JF2BXJMXAnG6jJoJ2gbECNFUENEPDOW8wpJrt7+gGavK4Cj X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:829:6e77:9093:f39b]) (user=irogers job=sendgmr) by 2002:a25:d656:0:b0:daf:b1e3:26c with SMTP id n83-20020a25d656000000b00dafb1e3026cmr463050ybg.5.1701123023915; Mon, 27 Nov 2023 14:10:23 -0800 (PST) Date: Mon, 27 Nov 2023 14:08:38 -0800 In-Reply-To: <20231127220902.1315692-1-irogers@google.com> Message-Id: <20231127220902.1315692-27-irogers@google.com> Mime-Version: 1.0 References: <20231127220902.1315692-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.rc1.413.gea7ed67945-goog Subject: [PATCH v5 26/50] perf maps: Get map before returning in maps__find 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. Signed-off-by: Ian Rogers --- tools/perf/arch/x86/tests/dwarf-unwind.c | 1 + tools/perf/tests/vmlinux-kallsyms.c | 5 ++--- tools/perf/util/bpf-event.c | 1 + tools/perf/util/event.c | 4 ++-- tools/perf/util/machine.c | 22 ++++++++-------------- tools/perf/util/maps.c | 17 ++++++++++------- tools/perf/util/symbol.c | 3 ++- 7 files changed, 26 insertions(+), 27 deletions(-) diff --git a/tools/perf/arch/x86/tests/dwarf-unwind.c b/tools/perf/arch/x86= /tests/dwarf-unwind.c index 5bfec3345d59..c05c0a85dad4 100644 --- a/tools/perf/arch/x86/tests/dwarf-unwind.c +++ b/tools/perf/arch/x86/tests/dwarf-unwind.c @@ -34,6 +34,7 @@ static int sample_ustack(struct perf_sample *sample, } =20 stack_size =3D map__end(map) - sp; + map__put(map); stack_size =3D stack_size > STACK_SIZE ? STACK_SIZE : stack_size; =20 memcpy(buf, (void *) sp, stack_size); diff --git a/tools/perf/tests/vmlinux-kallsyms.c b/tools/perf/tests/vmlinux= -kallsyms.c index 822f893e67d5..e808e6fc8f76 100644 --- a/tools/perf/tests/vmlinux-kallsyms.c +++ b/tools/perf/tests/vmlinux-kallsyms.c @@ -151,10 +151,8 @@ static int test__vmlinux_matches_kallsyms_cb2(struct m= ap *map, void *data) u64 mem_end =3D map__unmap_ip(args->vmlinux_map, map__end(map)); =20 pair =3D maps__find(args->kallsyms.kmaps, mem_start); - if (pair =3D=3D NULL || map__priv(pair)) - return 0; =20 - if (map__start(pair) =3D=3D mem_start) { + if (pair !=3D NULL && !map__priv(pair) && map__start(pair) =3D=3D mem_sta= rt) { struct dso *dso =3D map__dso(map); =20 if (!args->header_printed) { @@ -170,6 +168,7 @@ static int test__vmlinux_matches_kallsyms_cb2(struct ma= p *map, void *data) pr_info(" %s\n", dso->name); map__set_priv(pair, 1); } + map__put(pair); return 0; } =20 diff --git a/tools/perf/util/bpf-event.c b/tools/perf/util/bpf-event.c index 830711cae30d..d07fd5ffa823 100644 --- a/tools/perf/util/bpf-event.c +++ b/tools/perf/util/bpf-event.c @@ -63,6 +63,7 @@ static int machine__process_bpf_event_load(struct machine= *machine, dso->bpf_prog.id =3D id; dso->bpf_prog.sub_id =3D i; dso->bpf_prog.env =3D env; + map__put(map); } } return 0; diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c index 68f45e9e63b6..198903157f9e 100644 --- a/tools/perf/util/event.c +++ b/tools/perf/util/event.c @@ -511,7 +511,7 @@ size_t perf_event__fprintf_text_poke(union perf_event *= event, struct machine *ma struct addr_location al; =20 addr_location__init(&al); - al.map =3D map__get(maps__find(machine__kernel_maps(machine), tp->addr)); + al.map =3D maps__find(machine__kernel_maps(machine), tp->addr); if (al.map && map__load(al.map) >=3D 0) { al.addr =3D map__map_ip(al.map, tp->addr); al.sym =3D map__find_symbol(al.map, al.addr); @@ -641,7 +641,7 @@ struct map *thread__find_map(struct thread *thread, u8 = cpumode, u64 addr, return NULL; } al->maps =3D maps__get(maps); - al->map =3D map__get(maps__find(maps, al->addr)); + al->map =3D maps__find(maps, al->addr); if (al->map !=3D NULL) { /* * Kernel maps might be changed when loading symbols so loading diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index ab345604f274..1112a9dbb21a 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -897,7 +897,6 @@ static int machine__process_ksymbol_register(struct mac= hine *machine, struct symbol *sym; struct dso *dso; struct map *map =3D maps__find(machine__kernel_maps(machine), event->ksym= bol.addr); - bool put_map =3D false; int err =3D 0; =20 if (!map) { @@ -914,12 +913,6 @@ static int machine__process_ksymbol_register(struct ma= chine *machine, err =3D -ENOMEM; goto out; } - /* - * The inserted map has a get on it, we need to put to release - * the reference count here, but do it after all accesses are - * done. - */ - put_map =3D true; if (event->ksymbol.ksym_type =3D=3D PERF_RECORD_KSYMBOL_TYPE_OOL) { dso->binary_type =3D DSO_BINARY_TYPE__OOL; dso->data.file_size =3D event->ksymbol.len; @@ -953,8 +946,7 @@ static int machine__process_ksymbol_register(struct mac= hine *machine, } dso__insert_symbol(dso, sym); out: - if (put_map) - map__put(map); + map__put(map); return err; } =20 @@ -978,7 +970,7 @@ static int machine__process_ksymbol_unregister(struct m= achine *machine, if (sym) dso__delete_symbol(dso, sym); } - + map__put(map); return 0; } =20 @@ -1006,11 +998,11 @@ int machine__process_text_poke(struct machine *machi= ne, union perf_event *event, perf_event__fprintf_text_poke(event, machine, stdout); =20 if (!event->text_poke.new_len) - return 0; + goto out; =20 if (cpumode !=3D PERF_RECORD_MISC_KERNEL) { pr_debug("%s: unsupported cpumode - ignoring\n", __func__); - return 0; + goto out; } =20 if (dso) { @@ -1033,7 +1025,8 @@ int machine__process_text_poke(struct machine *machin= e, union perf_event *event, pr_debug("Failed to find kernel text poke address map for %#" PRI_lx64 "= \n", event->text_poke.addr); } - +out: + map__put(map); return 0; } =20 @@ -1301,9 +1294,10 @@ static int machine__map_x86_64_entry_trampolines_cb(= struct map *map, void *data) return 0; =20 dest_map =3D maps__find(args->kmaps, map__pgoff(map)); - if (dest_map !=3D map) + if (RC_CHK_ACCESS(dest_map) !=3D RC_CHK_ACCESS(map)) map__set_pgoff(map, map__map_ip(dest_map, map__pgoff(map))); =20 + map__put(dest_map); args->found =3D true; return 0; } diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index 06fdd8a7c2a2..28facfdac1d7 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -487,15 +487,18 @@ void maps__remove_maps(struct maps *maps, bool (*cb)(= struct map *map, void *data struct symbol *maps__find_symbol(struct maps *maps, u64 addr, struct map *= *mapp) { struct map *map =3D maps__find(maps, addr); + struct symbol *result =3D NULL; =20 /* Ensure map is loaded before using map->map_ip */ if (map !=3D NULL && map__load(map) >=3D 0) { - if (mapp !=3D NULL) - *mapp =3D map; // TODO: map_put on else path when find returns a get. - return map__find_symbol(map, map__map_ip(map, addr)); - } + if (mapp) + *mapp =3D map; =20 - return NULL; + result =3D map__find_symbol(map, map__map_ip(map, addr)); + if (!mapp) + map__put(map); + } + return result; } =20 struct maps__find_symbol_by_name_args { @@ -539,7 +542,7 @@ int maps__find_ams(struct maps *maps, struct addr_map_s= ymbol *ams) if (ams->addr < map__start(ams->ms.map) || ams->addr >=3D map__end(ams->m= s.map)) { if (maps =3D=3D NULL) return -1; - ams->ms.map =3D maps__find(maps, ams->addr); // TODO: map_get + ams->ms.map =3D maps__find(maps, ams->addr); if (ams->ms.map =3D=3D NULL) return -1; } @@ -848,7 +851,7 @@ struct map *maps__find(struct maps *maps, u64 ip) sizeof(*mapp), map__addr_cmp); =20 if (mapp) - result =3D *mapp; // map__get(*mapp); + result =3D map__get(*mapp); done =3D true; } up_read(maps__lock(maps)); diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index 30da8a405d11..ad4819a24320 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -757,7 +757,6 @@ static int dso__load_all_kallsyms(struct dso *dso, cons= t char *filename) =20 static int maps__split_kallsyms_for_kcore(struct maps *kmaps, struct dso *= dso) { - struct map *curr_map; struct symbol *pos; int count =3D 0; struct rb_root_cached old_root =3D dso->symbols; @@ -770,6 +769,7 @@ static int maps__split_kallsyms_for_kcore(struct maps *= kmaps, struct dso *dso) *root =3D RB_ROOT_CACHED; =20 while (next) { + struct map *curr_map; struct dso *curr_map_dso; char *module; =20 @@ -796,6 +796,7 @@ static int maps__split_kallsyms_for_kcore(struct maps *= kmaps, struct dso *dso) pos->end -=3D map__start(curr_map) - map__pgoff(curr_map); symbols__insert(&curr_map_dso->symbols, pos); ++count; + map__put(curr_map); } =20 /* Symbols have been adjusted */ --=20 2.43.0.rc1.413.gea7ed67945-goog