Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp645529rdb; Thu, 1 Feb 2024 22:16:42 -0800 (PST) X-Google-Smtp-Source: AGHT+IFWW/B+mgfyYby8PsHs5H7onPczLy2uUpPDUUAcUu4+NkF0KwZEwxlXT2/HPAuDCdbrTKXh X-Received: by 2002:a05:620a:22f2:b0:783:2a08:8044 with SMTP id p18-20020a05620a22f200b007832a088044mr1351708qki.57.1706854602178; Thu, 01 Feb 2024 22:16:42 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706854602; cv=pass; d=google.com; s=arc-20160816; b=t58LS+i/LBP81qL7kDSCFUqNNCUODkVVMoy836KxOOCW3JUp7P/3aeppqk4zGjNrBL wo5Kh4w6V0ewFDfJHa9XFo923MR5BZgGiqaFSDHVEj0RY0FyJwAcTCQXpCv8n/aOC7lN B2vW7k/Dp0CxWAQ+xGhr3BopSoRE98EDVQDFYRuMLqs4A8nQG5pZfeYSHiyVe48gMdFy mYP9QMpguQHj2adS4yBIWcPygEr3J6LcrL7ZQOfN1pWGHVTgmBRZdAU8MIJaWOl96io8 trSeU/Uw5AKDw1KgtACuRyvlQFlQIYH2AjuenBj+SaAavsxvdt7CxewSAZ5HI9qS3uJL z7FQ== 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=J1NDzPJDQoypR+FtlykBmPEdE/4vIPaLTqPNnIbkP3o=; fh=hPNnwySdkO/kiPrT3NWV0U6juJuf7ypti8O8Z8r6gfk=; b=nBrKSnNjrm5xtgjq+jxzocDOcM3Dvmgi+9wmj/fxiiT+kUYYDz2x+2P69kxMbSKywZ 6TKQBahSRWjPAynVAZOsaFJMelHyKS+nvMfy8agbCvwC7TfLF+tSYJYaQJa+Yx+RULhM 3l7Whm35cFQjobetMQ3HRskXGSIgVLBrnxaTDLbZGe6K+WdaQbN1Z32LKwpAyvR8P/fp wMHqHcuQb6gc4NJJspXDiVjxFbWNp9lBYQCKK5sr99lR/YxrBMq1oyb2F9EPCcQxk2Bh 956kZ73KTxKetd2wUH0poSNvSkKxA1gsLdzyGsXnjGkaUgKrzyDxmZpaSuTKcviZMoHm 6cLQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=KoIRlQsK; 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-49243-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-49243-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=1; AJvYcCWmme0AZVaIDaFVfisKydakcs9w8p3sFGkq/v8/cKo+zv4lu7zZsffEgBvenF/bdM6tRcDtZpHjkz4JVU+ygnS57rxLjnULtgZxvPpy0Q== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id u5-20020a05620a022500b0078405cc4b14si1284708qkm.604.2024.02.01.22.16.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Feb 2024 22:16:42 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-49243-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=KoIRlQsK; 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-49243-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-49243-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 C57AC1C213E5 for ; Fri, 2 Feb 2024 06:16:41 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EB95F17BBA; Fri, 2 Feb 2024 06:15:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="KoIRlQsK" Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.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 8BE6A1774A for ; Fri, 2 Feb 2024 06:15:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706854558; cv=none; b=FPKN5D51Iu5gX7qoe3R9Rs9dYJfw/k+vvElyU3VS+jfhJyyil9/wu8k860r12AKxLN03aDsFqcDXQ54awgBxPrCjGm6mZuj3jC1tFpsVGxYHcjPYK6a8pm3RMypHEt37z8/qgutQba+9p31J6uKYC+neVvlYVQz9Ncl5dHXccRI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706854558; c=relaxed/simple; bh=DaB8vTdXvQNZKpQ3zmCJbz1kO9aEvxkRfoyVK91CF/8=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=Nb+LC4y33YBd8F+s7IuydGIGmbIwi5Iqv7JP+q1IzIZMED6j4YRHQec5Ayp94mrRKDVQ4kjIms4Nqw0H4GvUzKM5pFI9zauOmZOeL8+s7VHdTrMlW6wcUDvcRdFXPhBGfPIxV+mn83699xReVx2nhY/6h8kJ9mT6DELeA4B7DAU= 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=KoIRlQsK; arc=none smtp.client-ip=209.85.219.202 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-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc6c2643a07so3113978276.3 for ; Thu, 01 Feb 2024 22:15:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706854555; x=1707459355; 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=J1NDzPJDQoypR+FtlykBmPEdE/4vIPaLTqPNnIbkP3o=; b=KoIRlQsKE20SUL48lWyZvfQmqDsCbSlEExODvgMfICbWuHk4PjCORHQecoj3iv8baa l0PxQihrApQi8ZY4jJ8glRfBnTwE1oRdBp4JshMO+mqxvUij2RdFnY5wNOh0mcg95Fje Eza60cebkrHbcIDkOnleMalvGl1aUTsYi0/FNb/s7fV7C2baAP1wFZy+VZJeuL9t+Sbo JCacnOfh3CQIN8AaL9Jr1dERw+sE4g+xWR23qb+e1pHZvqNt5Y1Djz07mF3EQFUkkVl+ sFH025fVuUcVtCdVhIyknPFz+CKJXAbOH25aRSFoKcHTqBt/Jj41h5GEzBhKMAebUige JNAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706854555; x=1707459355; 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=J1NDzPJDQoypR+FtlykBmPEdE/4vIPaLTqPNnIbkP3o=; b=RBOCv1WL7L9uZCdQYN4VfvV1vnVb0Fga5UwQLhE3WWPtaoQDDEIe7wyjBBhEV4V6KB uwAk5asJGNKVGf99weLTREgV30Lg+XDX3qLrEPsZ3H7kJwxcjJHgYeVblEPoKlNejhmK VaNhdn2eBHd4PxJbVWoN6OHkukWv+ENoFOm5clIYIFAii6lAFLw3CtkdTIXNJZdq9A/P t3dVLKzzXwSQR7LCgqo+lFOEpoVLls9PArpJrifgjjoV0OudDKhE6ktMkfuSUNZKThw5 zgUqia/eFI5u1mC4uwCBfjH8MmBz8PkGCPZJBaR7Xc/nrnfZcBHt/Uu+NjPss1fjBd9q c05g== X-Gm-Message-State: AOJu0YwKQrCnXKeDBFqS3ND0W3aKN0bKNYMMf728c8/1kpGdLli3jMur hm4G5TQZhZNNTorqOntq/1g6AY6PEaq2ABidjDh21JS/Q9JbUf5LJApu5QQzkF6Z2qPAzCC9FUv lmxuMHg== X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:a85f:db1d:a66b:7f53]) (user=irogers job=sendgmr) by 2002:a05:6902:f0a:b0:dc6:d3c0:ebe0 with SMTP id et10-20020a0569020f0a00b00dc6d3c0ebe0mr1158215ybb.0.1706854555663; Thu, 01 Feb 2024 22:15:55 -0800 (PST) Date: Thu, 1 Feb 2024 22:15:11 -0800 In-Reply-To: <20240202061532.1939474-1-irogers@google.com> Message-Id: <20240202061532.1939474-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: <20240202061532.1939474-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog Subject: [PATCH v8 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 , "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 , 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 489072bebb5b..75e85c0e04bc 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -956,7 +956,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.594.gd9cf4e227d-goog