Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp1325259rdb; Fri, 9 Feb 2024 19:18:55 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVc0/IlTtwBcQ4FpV9I4SmmPo4iYpMGY3u0YBaASxZOnHDp650e1XA1bZS2n2pyTNeyG9pc+NwdVB0fzfZvIAxo4QW8/PG6zy5k8x0xfg== X-Google-Smtp-Source: AGHT+IFqMuIGZ+iwNZVAQI120CHUTTX9gkBxUpURg2kApe6rjuOKriP9fRqiTph7Ilhw4XxQAgQp X-Received: by 2002:a05:620a:4153:b0:783:e9ba:7d4b with SMTP id k19-20020a05620a415300b00783e9ba7d4bmr1205998qko.49.1707535135388; Fri, 09 Feb 2024 19:18:55 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707535135; cv=pass; d=google.com; s=arc-20160816; b=RK8o7B2SWFKXcAWh2Z3BesdZL6M4OjsD2CH0C26nCRAxY8Dx+PQbkgzyT9oJG7Jq4y CWH0UcBeCheJCMo5jn9DWtddiMRS4gSuZAltDNAZGYgRth1jg5Itz1LUFlku61dd+01/ KXGhzqYBPhknmYpWsNKt6AmZDmZ3/O1jkcnMgOwseFWbH+vB0gZ0eBQthmVxwbvjx5cx PrE5WgbAZmNvP+PJq1LZAMIVIjcbwRlxcz/0nU+1LXIHvW7nxCBIT7hJURQjBsMiGMXE UK7zfpH9EgqGsf05ASfBurBF2bnLUW3z7H+rwR5upMWremIIoxi7SNaVPdy0vwyX5jhb cHjQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=to:from:subject:references:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:in-reply-to:date :dkim-signature; bh=WbHwthVXSI7C3XB81guo41V7Q64jaj+9t0IxZ+P82yY=; fh=xyZuzcnEoqbaPRyeGw0zynuMxh9/f8VNPPHtqyIWv4c=; b=gYNLWLg1KamV4UhWDhz9zddybERFNjLGj0c4mPkldAU3dZRiFNa+mChji+PtIu5CBz yrvu09ml/aDrhNGnMGMe9PzNXwJXcHElp8YfxnEi+9n5IVesLZd7LCpaW5uGc/WyUY5y GFWD6pMvaVA6pARwKNZ1xOBIWDywR32tk5aAmiqLN+uTU6iKUHerz6CUb5QhTpOaECfZ 6HtIZuFqEdDu5EBm/eO5iL1+qrpfYlnkJyOiLTOQuugDaecp7ditN/7lhpZ8te48Ic/7 YzUl57JZq6hvWpMxiwgGtqkhuTSFdBTkY7EEv/csJ3fXkMJClnpDYGoCcHfa335PQITU Ja2Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=bcuVmUdv; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-60266-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-60266-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCV3IJLb2eSm0LoOEjiamd8I7CDSp3Ga8pPuXe+/ee/C7/bp5MY/WE6Q9XfpcSdxp/y766a8k8WOUIVJwb7e/Q29WEWz8HZ2t0dccn35uA== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id t27-20020a05620a035b00b00783feb9410esi877498qkm.705.2024.02.09.19.18.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 19:18:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-60266-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=bcuVmUdv; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-60266-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-60266-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 18E451C247D6 for ; Sat, 10 Feb 2024 03:18:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E1307171B1; Sat, 10 Feb 2024 03:18:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="bcuVmUdv" Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0465C171D0 for ; Sat, 10 Feb 2024 03:18:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707535085; cv=none; b=SpNBV5B6P2KzHTTgVwzNiPnWjTQM2ANjhUJYozOqhjFgeYmpW0HfPF+FES3m5mI9YW7u7Umnb+ma0xH10GIPVTMTWZi6VUQ5kxf8uW+hfPES+pfEexUQ9NTdbf6bAhGK5N6I0k7QVAQ2HQIvZKezO9eKxYp1Yo5ooroYL7+oOTs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707535085; c=relaxed/simple; bh=fCSN5N4coy81LhtSHeyCVX7q+9jL53OOSOyC3avUo8c=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=iQpzQVuxTpDfCoIrU9ih5GSYM+4MyF5BzSWM4tqLdkPVbE2iTpz94LJg1S0DyhseGCHG+TNX74U0u5V4KHc3p2+eyK1nvFnAhX1Fzt/aQyb5UUVAhU0HUXAwdUUPxvZFB05QsItGc2YKXey4U4MtWjlCsv7NY9W7doqGanwCJSo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=bcuVmUdv; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-604a52664d9so31935167b3.1 for ; Fri, 09 Feb 2024 19:18:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707535083; x=1708139883; 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=WbHwthVXSI7C3XB81guo41V7Q64jaj+9t0IxZ+P82yY=; b=bcuVmUdvwhlFFBVOoiVP+6DYUaC8nEg7IRS2jhyfWmDkpShyy2h8Qkgolf3DbB0L45 eKHH3ftX1Q5QruJHDZW0rkUA8w2J6bZc0+BAJL0GqzkRCoFkDbtwdqN+vYEM/XTKdWof Jek9SFPi5HAtU+JzHvtmWcu+mD0DaUJwNTB9WG56AFVAol+z6neoVD4WdXTjSV6JhnqS QTfN3+K4vDDgUZf2UkLRzHSKG0U8kvNLE7Oe6/0iDx3nt7U2hk+FC7YFOYtzuH2Kxwsy HOq7Eo1u6GH2B6VPbBxVnRLGcch4G2f1bIjRozXIM1T07Merux7R4P0ma2Lt/A7RCDEv Nsqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707535083; x=1708139883; 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=WbHwthVXSI7C3XB81guo41V7Q64jaj+9t0IxZ+P82yY=; b=lXxvuBwlDjsuupjAwiPFrgkjMctIyCaEAf9Me42Anxrv9Y8dqS+uSzo2lfHi5GfXm5 /NFsPCAt32qK8n3kQz2gOIz725EXO/ii6o6jarugT0DGuHjnDCSBir1GZa20+vKqOIkL DXyc+3lA5/iHEKydbiyl+hfUD8QqSmpZd20Pb6dzy1s9H/Wao4OO9tKF3LEapyowdehu aI1OMSpK79D5gmX+Oju8FYIEUcKUnxNpqAOt3WpRe7cJsN60mTHSJgz1MX6x2P0ZDUOq USMCpK28Or6E/CA/I2/V19KpOIUCJxnBcQoOsN+mFDz1iVmlYpnbCYjyNTsSf3D4WXhx hnnw== X-Forwarded-Encrypted: i=1; AJvYcCVZGhNa3Bj8MU9mJbBfLyOoAz4GH9UPG35V8hPIMZtbwIuuwJcj02T6dmToQy3J7wm+C8bb1mbheVh5hQ6RcLuxm3iYLR6Y5s9JEbhB X-Gm-Message-State: AOJu0YwtCovACsRKBeWDtpLjvwam2NzOl9h327Qxqpi1Y5SpLyP5ri+V rn/V45mTpxZeobNCeEN3SCASq4DquLiSP3jPHarX+FYg0KxWljNsfBQ4DoZmfQtxPlYdkasRdSh 0IEcRlQ== X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:877:241d:8c35:1c5b]) (user=irogers job=sendgmr) by 2002:a0d:e844:0:b0:604:a67c:7f8d with SMTP id r65-20020a0de844000000b00604a67c7f8dmr280224ywe.5.1707535083146; Fri, 09 Feb 2024 19:18:03 -0800 (PST) Date: Fri, 9 Feb 2024 19:17:43 -0800 In-Reply-To: <20240210031746.4057262-1-irogers@google.com> Message-Id: <20240210031746.4057262-4-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240210031746.4057262-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Subject: [PATCH v3 3/6] perf maps: Get map before returning in maps__find_by_name From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Song Liu , Colin Ian King , Liam Howlett , K Prateek Nayak , Artem Savkov , Changbin Du , Masami Hiramatsu , Athira Rajeev , Alexey Dobriyan , James Clark , Vincent Whitchurch , Leo Yan , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org 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 Acked-by: Namhyung Kim --- 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 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; 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); @@ -1552,7 +1554,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 2547c9074b3a..ea8fa684e8c6 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -905,7 +905,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; } } @@ -917,7 +917,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; } @@ -942,7 +942,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 95773c32796d..0b91f813c4fa 100644 --- a/tools/perf/util/symbol-elf.c +++ b/tools/perf/util/symbol-elf.c @@ -1535,8 +1535,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 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, 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; } -- 2.43.0.687.g38aa6559b0-goog