Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp3428602rdh; Mon, 27 Nov 2023 14:12:30 -0800 (PST) X-Google-Smtp-Source: AGHT+IFWWK8d1u8RZ7aDmSxE/zQvLPQ9XfdQgS6Ch/14sGeIqNgnPeEiYdx2sryfMYgaBGqd/8+D X-Received: by 2002:a17:90b:80b:b0:285:8673:450d with SMTP id bk11-20020a17090b080b00b002858673450dmr13323500pjb.40.1701123150311; Mon, 27 Nov 2023 14:12:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701123150; cv=none; d=google.com; s=arc-20160816; b=K1DsW78eeQRlWTun5N3hs6KguDMGkOvGWnoh73ECEDRbiKCgb96hDyozkS6/EVE2qG 9+qsp0m8Owh2+CBLVL069Tp/r0fYO3xQVG3P14Vupm26NqSCBARZC5MyHhIO/3LrNBoj RmfItu2Ww1PzExM+fv1LeZPcXkEC6zZqfV/LgUbOYVvyX0rQpZ3TJZJ9vigUK2doSQ3+ RZbIjUKOb7VMmu4lB3bukNVa6Bk8wzJ+DOoaIPEmzcF17Z4rfW1axNaGE6u0i0/6eMsO pIie/Ez5m1nLAlS9M6qDog9rjPVEwC/QaZtOrPYaLaxYdVatSL4AlEdW+SDwZfVuYvk0 66MA== 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=hLbcZOEFyBsqn7FE5MjAHdUMX2GXl7FRr743DLVQTfo=; fh=Io7x3OacpefNRRNKoYAzVE0aWTDwD7FRzyCdD1e3AN4=; b=wsnTAhpmcXPOlkXxZBiyRyBF/49cqV4l2dCU4iE+P2cXn6whgm3Y0i9XplZMHFcpj5 UvRjvEeAcsfOfQo2taBHIHttzVwOsDkQbg6uu9Blkn9prPnmBs5urVxW36InDk5Kk5II p+ybLN1GlXyeK05kDmuwdMPxmrIQJmpvgUDtxY78aJGSEyzHs08hr+eQ98hSennusJ5Y QJ9BmSSL1Bupa/5uQDCuYBKbDfwmz2EgTJMFVNWid5jQ+InWDjpXp96r+iiD0K/cFG4e YMMUe6QbHg0/PvQ8n+rCWUZiWRFnp1ipiFCaqU8sv1eEtsSe/48OO2mlylnTXn3eNKvb 1ohg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="lq/SkcIT"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id b4-20020a17090acc0400b0028588a74a86si8798570pju.83.2023.11.27.14.12.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 14:12:30 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="lq/SkcIT"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 0FED480A5FAD; Mon, 27 Nov 2023 14:12:29 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234278AbjK0WMN (ORCPT + 99 others); Mon, 27 Nov 2023 17:12:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43018 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234104AbjK0WLc (ORCPT ); Mon, 27 Nov 2023 17:11:32 -0500 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 49BD72701 for ; Mon, 27 Nov 2023 14:10:26 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5ce16bc121aso45610677b3.1 for ; Mon, 27 Nov 2023 14:10:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701123026; x=1701727826; 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=hLbcZOEFyBsqn7FE5MjAHdUMX2GXl7FRr743DLVQTfo=; b=lq/SkcITb2oizRESBlTGkQ/zB8NkgsVyiKuYdKMusDlnOz2OqrwVN+y6WZaPi+cVdz 63z7Y6y6bRGJ7X8YTZaIqZGzhcR4Nst7cN9x5qk1GgsTa+WjBHh2h7oj3Txe6g5SBLjF JpcVkrSTucdtCGzvnQ8Bo7abDr2sssnV9i1vLurA6afEYLmDu+VWal/XqJrZ1NERrtow 1/7gbDimLleOGS+08kgvdn46Qh0OHd+zHUkacMab1Wb7eyH5/YZ+vY4XqXU56uGhlEX0 MWfnPi823bo6NevOGsskjs7kcaZnQ1PGtZSRxCcUzf0gb0C4LQkZbEM/dcYfJvKX/fHr i6Mg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701123026; x=1701727826; 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=hLbcZOEFyBsqn7FE5MjAHdUMX2GXl7FRr743DLVQTfo=; b=C7nOuyMzJNA+EASCmTdDkB+88TT5JCqHuBBlLI4XeYJtZzkTho2OtAUcCTtVnt8rMj pyz3TTpkYydkuu1eiKl3/OtCJotDc7DerxazIKMXRKxLEriRu6vbdQLm3e9cDNCWfeBW 9dyKY93yF8SrJ2a45wiixFO/wOZyeki3wgxL99PyrsJedaKhrkPEx1r+ZvVadJJB6NZ3 8Iizz5P2R989xr0Fcd5SLiWO9c0FaM11vz0pwLCOosVAfhQsocshsVJd4R/wJHBvcGXF UIJZmqmu/aTqzrrs4eMJhDR5RolMaRAKg+mmuG+orxyM2HziOAcTsSlQ2OMkYDWYVww2 eVLQ== X-Gm-Message-State: AOJu0Yz7/o2mla6UDwn02d1bQW8HLJyF+Y9FYVxroU6utWt63/umfzyU v1TFpMJJi1FRMRKAum8mnWOmEiTbguRV X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:829:6e77:9093:f39b]) (user=irogers job=sendgmr) by 2002:a05:6902:e91:b0:db4:5e66:98fe with SMTP id dg17-20020a0569020e9100b00db45e6698femr358483ybb.11.1701123026128; Mon, 27 Nov 2023 14:10:26 -0800 (PST) Date: Mon, 27 Nov 2023 14:08:39 -0800 In-Reply-To: <20231127220902.1315692-1-irogers@google.com> Message-Id: <20231127220902.1315692-28-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 27/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 , 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 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=ham 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]); Mon, 27 Nov 2023 14:12:29 -0800 (PST) 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 | 20 ++++++++++++-------- 6 files changed, 26 insertions(+), 16 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..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, 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_EQUAL(curr_map, 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_EQUAL(curr_map, 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_EQUAL(curr_map, 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; } @@ -1248,7 +1252,7 @@ static bool remove_old_maps(struct map *map, void *data) * We need to preserve eBPF maps even if they are covered by kcore, * because we need to access eBPF dso for source data. */ - return RC_CHK_ACCESS(map) != RC_CHK_ACCESS(map_to_save) && !__map__is_bpf_prog(map); + return !RC_CHK_EQUAL(map, map_to_save) && !__map__is_bpf_prog(map); } static int dso__load_kcore(struct dso *dso, struct map *map, -- 2.43.0.rc1.413.gea7ed67945-goog