Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp6935178rdb; Tue, 2 Jan 2024 21:08:14 -0800 (PST) X-Google-Smtp-Source: AGHT+IHG/60n03Ld7wwTjSydJ/OA+Swvy4lXtPCo9LphTrzVzzdJcazWE13eYCdY0j3sqob5Wp2F X-Received: by 2002:a05:6214:48d:b0:680:b7ee:d378 with SMTP id pt13-20020a056214048d00b00680b7eed378mr5414552qvb.115.1704258494452; Tue, 02 Jan 2024 21:08:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704258494; cv=none; d=google.com; s=arc-20160816; b=rIbMYZFyfjpLvBAUSsE9CPdxe3us+MuWq26nLOhykFTM2MJOvPQjl3pD9HnfvE+Si5 n37ak11BZGCdr43dlryoiWJB1ehsT6R+pvILzmNi6IDusEbsAtA+1wcI7ws/M3noEync zQE1Xj4j9dABKsU6c2xuYxx/tz7NwrM3GIs+42aPBXdSMDAncwYwdV88Q/CRVXlRTLSP BBvwGoKeDstXF2TIl5umhwx57AXVygUfALx+w82+eFHmkIJbUsVpYsq5w0UfKpr645zf alZFJmsvp5O3fzMZv3dSSz8ye7FqnWPsRiQRyiGeZ6hRxCrfvmTYemszapNFPomsbbMw 1N9g== ARC-Message-Signature: i=1; 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=QDc8U8CcTfXk6/xIXhbcXcxIlZrCEy1yfrH0rSN6Nbc=; fh=Io7x3OacpefNRRNKoYAzVE0aWTDwD7FRzyCdD1e3AN4=; b=SAQZCuxEkfe+mYFnw+5XUJlrjzGBNXEAGxtu7TvuChHglu7Z1lKXQ4EEj5jLPNe0uP nOyydU7IknRS504aIKuWMpaFUuiLglj+YTUjzSYyyF/uOLjKmr5O90EXmuqDZk5PIys3 W1GcYmbHQmIOjG3vIvSv2OZBCvb/oEErK9W2NX2fWhmFKFGVBi+PUcPerPVsXQsRT+CW C9G4nkA5hmHNyEtkl5fpw758wK9yd5AJWiOD2GnNPUQisuZfMaMjxooF0omwEtlERQRV C+9Sps1FBMKetdF3zeaVafhQYktI4G5uvvWWiAWTwa9rw3+IUWqWVOYNRs9KCIRAtXct dPVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Gtgb+4Q9; spf=pass (google.com: domain of linux-kernel+bounces-15163-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-15163-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id o17-20020a0ce411000000b0067f92e3d944si26592549qvl.398.2024.01.02.21.08.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jan 2024 21:08:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-15163-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=Gtgb+4Q9; spf=pass (google.com: domain of linux-kernel+bounces-15163-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-15163-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 3A14D1C22E66 for ; Wed, 3 Jan 2024 05:08:14 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EC644182DD; Wed, 3 Jan 2024 05:06:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Gtgb+4Q9" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (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 DE67D182A3 for ; Wed, 3 Jan 2024 05:06:56 +0000 (UTC) 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-f202.google.com with SMTP id 00721157ae682-5e744f7ca3bso151045387b3.2 for ; Tue, 02 Jan 2024 21:06:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1704258416; x=1704863216; 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=QDc8U8CcTfXk6/xIXhbcXcxIlZrCEy1yfrH0rSN6Nbc=; b=Gtgb+4Q9xwIphF8/8mfvogKyMwRh0Di3byJfqsGDfslLmpiC9vheU0Cg0LyZn+FWGq k3ztS05mrQiyz4fVwnVitZ6N9CQ7MVV8VGwuYblBWa5oCLHQ3D94Ki2jMI0MTsaY3FSI LKTx4U4oFZAuSVHT7pPH0JQRfqU5rMWZ4Ec9CxsH1FikfV+4D2t//YH4DoLTOgIsHQZ1 229yQfUQ3KdwUkIHTdL2m8mI2xV5UIEkUAYutz7NmmXknsDtgJnslG0yd9PbKB/kApPm V5PMWW+FtEdBVds/azg7XYqGRKVgyvYdFc1Kw2xxJpZOsWhhOaxxJncv7iI52zXIda51 Sweg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704258416; x=1704863216; 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=QDc8U8CcTfXk6/xIXhbcXcxIlZrCEy1yfrH0rSN6Nbc=; b=oxI0lWcJp+XlMfnRBo9IgSwFdOM4uWjfL/6Iq06GhyfwtqaNDrMjzWZz6ZAT01VukL vpQw0tc3IfXPfa18o1l4NB4LZ8tAvuxamNno+iXcQYYBYRirvc75R67yWd5yZjUes1mW 4Xq/CRKDvfc16Fkr80gfIH2ca0Gbj/V2wCz0NhNgZF5GE/bitklmSD8/xTaJmPIdEUt8 Lw5eYJRRutBuA70xJuM090t7u7bo6/hhVsh/HIqCH3YUGFXNx7vuWl/2nC4DWyKRigR2 KJCTkhBRfwvPQeIP2O2lMBciOYjnis/aDFC91GV6XzAnPT1a66kx7CuvgOFOxsPAnMWN 4gQQ== X-Gm-Message-State: AOJu0YywePRygVXmTenM4+aJl2a0JlBOsRWFnAKqozlt49Z0BkyGgRt7 h5satVOEyAtmui03AHjysAO6G7qLf24Z+SbBi3c= X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:e2bd:f1f6:8ea6:8d6c]) (user=irogers job=sendgmr) by 2002:a05:690c:b83:b0:5ca:5fcd:7063 with SMTP id ck3-20020a05690c0b8300b005ca5fcd7063mr8363427ywb.3.1704258416086; Tue, 02 Jan 2024 21:06:56 -0800 (PST) Date: Tue, 2 Jan 2024 21:06:14 -0800 In-Reply-To: <20240103050635.391888-1-irogers@google.com> Message-Id: <20240103050635.391888-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: <20240103050635.391888-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Subject: [PATCH v7 04/25] perf maps: Get map before returning in maps__find_next_entry 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" 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/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 b85147cc8723..0438c417ee44 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -943,7 +943,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.472.g3155946c3a-goog