Received: by 2002:a05:7412:b795:b0:e2:908c:2ebd with SMTP id iv21csp555586rdb; Thu, 2 Nov 2023 11:01:51 -0700 (PDT) X-Google-Smtp-Source: AGHT+IExzZaLd5iGaIvS3LktwjvlxlmyxHyvDBhg1GriYCeVGzi1fI9aAnrvIxP1QHpzK2p9GuNb X-Received: by 2002:a17:902:e812:b0:1cc:332f:9e4b with SMTP id u18-20020a170902e81200b001cc332f9e4bmr677021plg.1.1698948110531; Thu, 02 Nov 2023 11:01:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698948110; cv=none; d=google.com; s=arc-20160816; b=BCSdhjFjW2Zw+xPdcpzRl/TFaw3BkfH9FyA4Fs9LSgr+2slhd/MK+I5ZlV/X217IYC 4JFHPjlVuhqMNdgw2BbwQPrDjJ1JlDOCq9D1YCgaZ5EGqk6PocLQJR3f16jNfveWGRs+ bE1TJKUq7c7TRyXvVvntsjknEzUsf8prd27hnDtyJ/Ly1zBoS3gC93PMyOtbt3s2nSNs Cx9Of1GVakn6ih5LQ4vrg84qrU1uS0VuEioDxllssSRV5Ne0duIe5SekHPAqmb4FR5Zk r7Q9SihtZ0UIfGLr2IyWDc/3TwCD+6njhqKd/AXQiAmNKqLk6eUKn3ODRML+UdyismMT Y6Iw== 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=6PNDEm4kXFYBRRJS/3kBphkCUT3Aht0BTpc1/MnMORo=; fh=z1KcSqUpYQ9oC4uLSeIkhAYTnJ2bvP0QbNW2xqV5NqA=; b=EIo3s+utQmIO52QZjUKl/D5S0JiK9GCMDWxLyqVMQnUp/KHxsP+H0pf1dsx9nC4L3E cMMZBTlsk6hpIy3dIzCKUdhrDk1E7vwA8/3Oo/u+ELo8IU3M089n2DoVUi2HPJYs1Fzm 0CXpb8IgNnIkztKdX2ldVKzxNf6pXOnzqE5s/lhw5pMcvba+M2E30ai1CwwNXljaK0FM Zr/rBiFG5pFNvstMmFhWseRPMhiSRkrAKpIAUYylRdgwLYRFfwHU7ZW5QiQzZ1dttexo 0GtGzvKPCqOATWf4PeUnZwKOaPIE8g5AeOn0TSaY9PVI5W7jDY2MdpmDZ3+5G8pghfjP ULGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=EdRsqPT9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id j4-20020a170903028400b001bb0ba81053si69073plr.50.2023.11.02.11.01.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Nov 2023 11:01:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=EdRsqPT9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (Postfix) with ESMTP id AE591807EBA0; Thu, 2 Nov 2023 11:01:33 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377151AbjKBSAr (ORCPT + 99 others); Thu, 2 Nov 2023 14:00:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53746 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347307AbjKBSAC (ORCPT ); Thu, 2 Nov 2023 14:00:02 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 88B9110E7 for ; Thu, 2 Nov 2023 10:59:08 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-da1aa98ec19so1505562276.2 for ; Thu, 02 Nov 2023 10:59:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698947947; x=1699552747; 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=6PNDEm4kXFYBRRJS/3kBphkCUT3Aht0BTpc1/MnMORo=; b=EdRsqPT9Z6IitF7UNmJ1MqJbQ1LRcbnwMchwXw+Osz9EnJtvp3Sf7wPq4ojGblPMbJ xR6hh3WA1tZFno29T93wu0OVRP9KCFjiMwCXDgYRIGA0oZlL+r4yxmbIaavTwmrnNQHX xVta6TMgfxgYCBa/27InAo+1njsxZNtjMJ83C98y5nTg7WKX66d9N4RmDLWkikbpz/LY VK35XKHVKs1vnxaPpG7BULGRoEBc0ln8n1UNVKV3+zmCvQXM4u57DpYv53gy5QtDd1ic hsjtWMzIr5wUKW6g+ozC4/09rzcAH6vdFr5wIWzFfee7tXKbqQlWw8A6NTSkBlVqIrvp gySQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698947947; x=1699552747; 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=6PNDEm4kXFYBRRJS/3kBphkCUT3Aht0BTpc1/MnMORo=; b=PPXqREMfP0dRFDj3AC6J8RWs0qJhjD1J1rGrd39yfb6Ueiw1cIDXi7U9pwYrxmBH81 hZ+vS4rl7H2JB6ziB3XZMD1qTxlHCBv8y17tF6J3YrQi6qyL6rZTnnQuL+yV0x40m0mQ iAons2ji0928Tcb6XiaFm6QzKRNRdoroXEOogjk5px4IzVLE114RqPEPbsTY0CGqHvXs j0mm6jSxeIOSflGv5PHuK4TB7+Lk31xSpp/TG+439rHvFZRXTX7WdwF4ZjhbaLYiTifh Ttl7CRxoL9IoBbQ6hOeLAxRdT6eU73TMCgar44NrALsmeh+Y1wwxk5p3OvndvsefR4eI mvDA== X-Gm-Message-State: AOJu0YxCKj5lpci7Sdr6qj30hV74E2cavzzAbiqFCIGA2xfncGJsJ14x 7CdFbzI4zLeM+VAiA3o1v+5bEX4bTCFl X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:bb34:df9c:836c:afca]) (user=irogers job=sendgmr) by 2002:a25:9392:0:b0:da0:cbe9:6bac with SMTP id a18-20020a259392000000b00da0cbe96bacmr390511ybm.11.1698947947552; Thu, 02 Nov 2023 10:59:07 -0700 (PDT) Date: Thu, 2 Nov 2023 10:57:13 -0700 In-Reply-To: <20231102175735.2272696-1-irogers@google.com> Message-Id: <20231102175735.2272696-32-irogers@google.com> Mime-Version: 1.0 References: <20231102175735.2272696-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.869.gea05f2083d-goog Subject: [PATCH v4 31/53] 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 Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email 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 (lipwig.vger.email [0.0.0.0]); Thu, 02 Nov 2023 11:01:34 -0700 (PDT) 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 d6b3f84cb935..42d73f00f9c1 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -1758,8 +1758,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 8a8c1f216b86..b3937e734cbf 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -942,7 +942,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.42.0.869.gea05f2083d-goog