Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp1325410rdb; Fri, 9 Feb 2024 19:19:26 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUYiAErNzMYfSMyoG6GdtMUUwKG788r4V1HUnQfMFtSOUfnOI022lESSbGl9++hOimV3qJLUSw8Wm8u3w2MYS5TbWZB4ThQgIp0UF+wew== X-Google-Smtp-Source: AGHT+IH6tATU9OJwgm7zrxS4NwSmNOo6tEaP4+CG1eMisgd++3CUkzsM9qFKE4U/DtKokU+Y7/ti X-Received: by 2002:a05:620a:90b:b0:784:da2:5799 with SMTP id v11-20020a05620a090b00b007840da25799mr916743qkv.45.1707535166458; Fri, 09 Feb 2024 19:19:26 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707535166; cv=pass; d=google.com; s=arc-20160816; b=ondyJdJ4F5Pom9xZyQfCNwYeCaNvLfyOJbbj/1/xsXyikQLwGWYB6ILyzntAny8Zgm HSXVX56ZfNjlcHsUuAN+wR6vxKRbQmcNTAlkyEgJzIlevuiA57adB9K1T487gxp2HRKC hlmjO/NrsFXJF5Jvxcymz4dsmU0q9jsIOPt62cS2zcOjuEvJuSZzG5/oTYcvEZ0F7/N4 NES4IqRyV89ZtrvB202LRe99TKFaGUTMuobdxCRfK2HlALvmhDOhTgpwzFC2R1+JaUsh zSzVBqP6OZM+RX0FoImz4eZUi6rx6P2LBg7d+4cWT1GK0nANMgbVxfWZR5ePLNoOdsre 61YQ== 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=cCmA44GmuSEQ6KR355fhhypmUscD3id2lD2Aj801jbU=; fh=HiZooOeJ8skgzgGgfFjpupnN+jja65cU9i4gvfNHy3k=; b=daBZTY4tt9aqio61maGKDLhx4NrQfjoeu/K/LdhQt+XWjYytYRMgFirliTi9asoC3E HfHzC8baMrTwlBhBsnOAdIKgiJIZFS1ZP2QB/eXRLE8tZCXYRkUr/5E5QtMGFRHX43I9 wIGMKmuoQx51+mEQPJZ697dNeEePX6vb2wLR0XeI9lq+behXioQbMeTcLl4KYjEmeR+u /8yNJ03+dsgqg5RBrtIEM+/uaHmJwaA5TKbVz/tnEmFluUWcHyo+ajPxU9cno+T2YDF3 Grev1g3WWuUODW33CJAdfGy3FLar4fdqGa2HD2M59QzEfNlwDO342LK/U449i6JdElaD vuAA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ZnYdjP3H; 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-60267-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-60267-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; AJvYcCWuYqqZffBIiJcGkPdZ13hYuJ4tTPdY52pAby4ltIYIscmEP2WqoWmrAxrDpGSMzKZQeBEwXmxbHSmp1EBcThbKY0KsTFkVyM9kzqMH2w== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id f13-20020a05620a12ed00b00785ae603d88si896964qkl.88.2024.02.09.19.19.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 19:19:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-60267-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=ZnYdjP3H; 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-60267-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-60267-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 2C1D71C248D6 for ; Sat, 10 Feb 2024 03:19:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7817A8493; Sat, 10 Feb 2024 03:18:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ZnYdjP3H" 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 B0D932134B for ; Sat, 10 Feb 2024 03:18:06 +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=1707535089; cv=none; b=nTkEtvwOn0szViifTSwDtL/nZKYY7LLy+6mY5dOv1Gr5FbvNj9p4iUAc5Ho2aElMe1HDbxXcrNQKilBGBy1eBqmgyPGb7tSahNpnb6x5boUOG+ZcA2ll9iYHOkx4KcEkmtOqV+ks8DLau4Tuql1ovAomC05RUDWADtjgrnChA28= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707535089; c=relaxed/simple; bh=NlbGPHUjvYPN2VcnjTROsgfVOvtX5wfc2jhlMdVGruM=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=mCbibVHKMI42wukzGXomVR9kksJ10jjdc6QROUMsIPOIq8TNin/tJfshrb/HUogpuIZC7/nlhcFDKvSRtiq35zg0YxrMudFmZybn6tPl+mznHU/MeSKUGCy0DF30mlAjtyEJbfns5KxqKsGCva/+a8sdHJN/K0CYm4u/7o6oEcg= 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=ZnYdjP3H; 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-5efe82b835fso40458267b3.0 for ; Fri, 09 Feb 2024 19:18:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707535085; x=1708139885; 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=cCmA44GmuSEQ6KR355fhhypmUscD3id2lD2Aj801jbU=; b=ZnYdjP3HtTkPU+JzmqOwUWgjDDX8DA/CozgrchtAixilebiDQGVKBnXI9Ji17tUQ2r g/SQFMyiDWjZaXi6VHnsIBKSWiy4Oj9aw5/FgDgfAVbDl3HkJ5mfZuNBogE1lb6Idg2+ 9jjfqLMhIzaeT4bgKjpXe9Hx+KL1B9DB6tplU1O+9OqZOABB/Fc6AZxpeJ/+/PJBWtjY tSa4i77a/+wplygoZ9OQuJ64g/QA1j4fYIdtUJioTkML3VUYHMTXkgky8DuBNoiUXJXV EMYlSal2aiF+SxqLzadbqLo6eQPvsIMigVgWUr3sItk9OJy6HI2xM0YCG7HP8TQnbes/ tdLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707535085; x=1708139885; 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=cCmA44GmuSEQ6KR355fhhypmUscD3id2lD2Aj801jbU=; b=d7AXglc7Ei2U2d9BnBlxLb5jRE1JBg01V4GtLcUBZzPWsSvcV/1ejJmr1oeJ0GI/Eh MuKZi2hZglRgQibZCsT4y5kOwQ6pvyg1xn5Oyn2ZNeXnHDnG0aLK9d3+JQ6REkTNK0s+ xtUIvdlm9PtPVlFIW9/N7deXMRVCF7+WQPz+GtqV6AxDoagEd4O9GNapXYgoSK2Ao65g Ouc5RPpdZAzNxzgGzp6mNkT5qEk8TuEDAwBz6R0i8FM8eGA10b3ffIak3uJO8ocYpkz9 4PHt/+BfX4cy1SECgV2FPQiM3G3xHHJyl/aup4MeQGqBz3+sB+uFNBhc+HVlczTaYQKV tKng== X-Forwarded-Encrypted: i=1; AJvYcCXPad2SJlDbcvXUJcReid9D+jPqdr/aoOV7nhmSTwrEhV/Y/Irq91LmYikUBaUwOxwKlcs5fQSMvHEwv5iwUqauB3eIIptpkdj/VvBV X-Gm-Message-State: AOJu0YxgUaSEy8Tz9oyK/lpQb/GAnhBRZCPfbNKh4lfEgpCzM88NhvWM LlekIkGnCipncC5lKgdXO8EGrUDux50Sx1LWl0kXh6c62akNLsvxbf+bi0XbhTW2SAaa9WKaNfS UWxdUBg== X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:877:241d:8c35:1c5b]) (user=irogers job=sendgmr) by 2002:a25:ce05:0:b0:dc7:31f3:9f98 with SMTP id x5-20020a25ce05000000b00dc731f39f98mr241172ybe.9.1707535085769; Fri, 09 Feb 2024 19:18:05 -0800 (PST) Date: Fri, 9 Feb 2024 19:17:44 -0800 In-Reply-To: <20240210031746.4057262-1-irogers@google.com> Message-Id: <20240210031746.4057262-5-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 4/6] perf maps: Get map before returning in maps__find_next_entry 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. Signed-off-by: Ian Rogers Acked-by: Namhyung Kim --- tools/perf/util/machine.c | 4 +++- tools/perf/util/maps.c | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 7031f6fddcae..4911734411b5 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -1761,8 +1761,10 @@ int machine__create_kernel_maps(struct machine *machine) struct map *next = maps__find_next_entry(machine__kernel_maps(machine), machine__kernel_map(machine)); - if (next) + if (next) { machine__set_kernel_mmap(machine, start, map__start(next)); + map__put(next); + } } out_put: diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index ea8fa684e8c6..df0c8041899e 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -962,7 +962,7 @@ struct map *maps__find_next_entry(struct maps *maps, struct map *map) down_read(maps__lock(maps)); i = maps__by_address_index(maps, map); if (i < maps__nr_maps(maps)) - result = maps__maps_by_address(maps)[i]; // TODO: map__get + result = map__get(maps__maps_by_address(maps)[i]); up_read(maps__lock(maps)); return result; -- 2.43.0.687.g38aa6559b0-goog