Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp2186171rda; Tue, 24 Oct 2023 15:27:47 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG2VxF/i8odTB9wo7nhd89E1p+5DCdI4My8nFz6FCP+glRZRp0DrkM00F7cP13CBQmsaRha X-Received: by 2002:a05:6a00:4c86:b0:6bf:15fb:4b32 with SMTP id eb6-20020a056a004c8600b006bf15fb4b32mr20222570pfb.8.1698186467477; Tue, 24 Oct 2023 15:27:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186467; cv=none; d=google.com; s=arc-20160816; b=XVlZ16S4d1IN9XMgRHRNrh5LBd+8vhOD9Muq3NpN33TnwIIr5hPuD8syEYU2Fw9Mun kXyDAINijopr6ilkQuqRXsZyWAklK9E3eaLvtvdZj2vGt7ssBsmvJIJSWo16pDP2UlPy mYe0lT1qFExt7rqhuaNdHW/0/cBaOBEezAz1DREaWYyvmjh9SRSI5GLwiZYJj5JOiFP5 XkBq9DzFIFru7sDNXkcKvA2Ruaej+I4v3XwlHQRZ5hQ/lsbTDkXV4jhGqhyVORFRc/L2 IpiT4acGU+FHE0EVLNuuFfyCfSXWFxLNBsdaCs9nQE4kGLQDcQqaDAb8i6GznXaQqUcT hiGw== 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=RtLWFp/Ay9yY6/sZZC/VcUQilprFkB+Z9rXtdAZQ2Hk=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=GQq1MQBT34d2urQ0dWD4c3W1tV6KrDG4SWz6U8DLjgKq2TL4tHjwZLBuniIi2vKoTm Yn5TaHTMo1KYnABR3NMgUMgE7SgqiDUrZ3PvFSZ8YTL/SRByLPRpbWaNI+pqceZBXpWc GN1itG7HwYeV0cDbwPdPdQRPLkw8p+OglpqQkC1Hwz4SDMcPHm9NP9yE4kh6qt3ySbFP g81LfIrL4/gNqbABA8KRKbp+oo2Rkv32r5QfBIY7QILxiugO/zJJMki/qsdvls/cgY4O o1rPl6vGv5qoYVRPNkJuL0mrCX9ZU+EnleazDa5uqs0SidBk37e1m1cUGeWKohST6jca 6d7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=vPvtXrnG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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 morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id r194-20020a632bcb000000b005b8f38f9976si1273586pgr.765.2023.10.24.15.27.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:27:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=vPvtXrnG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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 morse.vger.email (Postfix) with ESMTP id EB26A809B9CC; Tue, 24 Oct 2023 15:27:12 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344575AbjJXW05 (ORCPT + 99 others); Tue, 24 Oct 2023 18:26:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41204 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344618AbjJXW0X (ORCPT ); Tue, 24 Oct 2023 18:26:23 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 035A71BDF for ; Tue, 24 Oct 2023 15:25:43 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d9a3a38b96cso5574231276.0 for ; Tue, 24 Oct 2023 15:25:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186343; x=1698791143; 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=RtLWFp/Ay9yY6/sZZC/VcUQilprFkB+Z9rXtdAZQ2Hk=; b=vPvtXrnG8y52U1DnsKfD3FlNUA/UY/g3ZmVg0L5CYQShxdXUf8wRf3pZB+jRG8gEmb sJ/8waadxrtY6OZHqiBYN7W7/0CejexF/FSHZJ5XkVbnO5Txt67fZLgV24iwCDBxm+Bb C7p47VBoIvJNXtDN/QboNm6yziMnKhwmtyVo8SvDLVcfWkWtB4hups1Jd77XqvOgu9a0 7uIho2d+zfBA2E8bAOqf9WFdFWtyorfkBNUcDYdinqWyIi8UHqWfq9ID0ZHHgDaqkN/5 Lpv+xLEdSbEh8cmKnuWm5zjYWHWWkVXrJSXtIRX8YHejpeMNJcu1yZqF+cEc899WtyS2 dgrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186343; x=1698791143; 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=RtLWFp/Ay9yY6/sZZC/VcUQilprFkB+Z9rXtdAZQ2Hk=; b=i9gISZUFIFjW9rHM1GwA+9taO5F3yXjNgPIiZlFxGzMlmSCDBbER2mmr9ZJikKh0gW 6CPAf0GcGt/8Ak6PyIrCTfDeCsoeARNJOxQ2unS1M/VXCJuD96n8TWrHTFid4mHWe1f0 YSAHQk0JKTcG4pB5HiVHzo62G7+Kh3ClDqNZkVfFeU9AULzUkA3TvP8iDPzAHuM9QlfD dVd2iJP0WaGgbTCBFpgfS53zy0UT5iU25ImA5G7MesamaeYYjhVr/C7SJV794IAMeP6n d+z7UQfJ8neZy7R5quUJ9MjPUc7KupUF+l/AsfdOVkFyeVGFW2946NgPYY0EdaXaDvHH oDXw== X-Gm-Message-State: AOJu0YwCfHK6K2tghZ1emHyTHLG3Gq4qcWTZ+lhZgIPRaFj+3VVygGti cuNIL2LXsfkZa6MjKRdvNeTNbJTwkr0U X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a05:6902:1888:b0:d9a:520f:1988 with SMTP id cj8-20020a056902188800b00d9a520f1988mr364100ybb.4.1698186343062; Tue, 24 Oct 2023 15:25:43 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:43 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-41-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 40/50] 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 , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,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 morse.vger.email 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 (morse.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:27:13 -0700 (PDT) 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/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 map *map, void *data) struct map *pair = maps__find_by_name(args->kallsyms.kmaps, (dso->kernel ? dso->short_name : dso->name)); - 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 = true; diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 1112a9dbb21a..d6b3f84cb935 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -1538,8 +1538,10 @@ static int maps__set_module_path(struct maps *maps, const char *path, struct kmo return 0; long_name = strdup(path); - if (long_name == NULL) + if (long_name == NULL) { + map__put(map); return -ENOMEM; + } dso = map__dso(map); dso__set_long_name(dso, long_name, true); @@ -1553,7 +1555,7 @@ static int maps__set_module_path(struct maps *maps, const char *path, struct kmo dso->symtab_type++; dso->comp = m->comp; } - + map__put(map); return 0; } diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index 28facfdac1d7..8a8c1f216b86 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -885,7 +885,7 @@ struct map *maps__find_by_name(struct maps *maps, const char *name) struct dso *dso = map__dso(maps__maps_by_name(maps)[i]); if (dso && strcmp(dso->short_name, name) == 0) { - result = maps__maps_by_name(maps)[i]; // TODO: map__get + result = map__get(maps__maps_by_name(maps)[i]); done = true; } } @@ -897,7 +897,7 @@ struct map *maps__find_by_name(struct maps *maps, const char *name) sizeof(*mapp), map__strcmp_name); if (mapp) { - result = *mapp; // TODO: map__get + result = map__get(*mapp); i = mapp - maps__maps_by_name(maps); RC_CHK_ACCESS(maps)->last_search_by_name_idx = i; } @@ -922,7 +922,7 @@ struct map *maps__find_by_name(struct maps *maps, const char *name) struct dso *dso = map__dso(pos); if (dso && strcmp(dso->short_name, name) == 0) { - result = pos; // TODO: map__get + result = 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, struct dso **pdso) map = maps__find_by_name(machine__kernel_maps(host_machine), module_name); if (map) { dso = 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 *dso, struct map *map, dso__set_loaded(curr_dso); *curr_mapp = curr_map; *curr_dsop = curr_dso; - } else + } else { *curr_dsop = map__dso(curr_map); + map__put(curr_map); + } return 0; } diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index ad4819a24320..3f31e868d883 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, struct dso *dso, u64 delta, struct map *initial_map) { struct machine *machine; - struct map *curr_map = initial_map; + struct map *curr_map = map__get(initial_map); struct symbol *pos; int count = 0, moved = 0; struct rb_root_cached *root = &dso->symbols; @@ -858,13 +858,14 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta, dso__set_loaded(curr_map_dso); } + map__zput(curr_map); curr_map = maps__find_by_name(kmaps, module); if (curr_map == NULL) { pr_debug("%s/proc/{kallsyms,modules} " "inconsistency while looking " "for \"%s\" module!\n", machine->root_dir, module); - curr_map = initial_map; + curr_map = map__get(initial_map); goto discard_symbol; } curr_map_dso = map__dso(curr_map); @@ -888,7 +889,7 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta, * symbols at this point. */ goto discard_symbol; - } else if (curr_map != initial_map) { + } else if (RC_CHK_ACCESS(curr_map) != RC_CHK_ACCESS(initial_map)) { char dso_name[PATH_MAX]; struct dso *ndso; @@ -899,7 +900,8 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta, } if (count == 0) { - curr_map = initial_map; + map__zput(curr_map); + curr_map = map__get(initial_map); goto add_symbol; } @@ -913,6 +915,7 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta, kernel_range++); ndso = dso__new(dso_name); + map__zput(curr_map); if (ndso == NULL) return -1; @@ -926,6 +929,7 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta, 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, struct dso *dso, u64 delta, pos->end -= delta; } add_symbol: - if (curr_map != initial_map) { + if (RC_CHK_ACCESS(curr_map) != RC_CHK_ACCESS(initial_map)) { struct dso *curr_map_dso = map__dso(curr_map); rb_erase_cached(&pos->rb_node, root); @@ -951,12 +955,12 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta, symbol__delete(pos); } - if (curr_map != initial_map && + if (RC_CHK_ACCESS(curr_map) != RC_CHK_ACCESS(initial_map) && dso->kernel == 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; } -- 2.42.0.758.gaed0368e0e-goog