Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp3429020rdh; Mon, 27 Nov 2023 14:13:17 -0800 (PST) X-Google-Smtp-Source: AGHT+IEUobOu+338z/pJPAGsiIamEMu1VDm28Fs/nuhKJxFGbtMkvIuP5JVVJGuA5tg/d5o04hY+ X-Received: by 2002:a17:902:e746:b0:1cf:c401:1d9e with SMTP id p6-20020a170902e74600b001cfc4011d9emr5039685plf.31.1701123197616; Mon, 27 Nov 2023 14:13:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701123197; cv=none; d=google.com; s=arc-20160816; b=K55dSI7AWdybfBBb3Zm+S4WO7GqBpCjIUc0KfYOiIMRhMjRdRvPFslhruAgewrBnzr H42NiyzoiRqi4hReX9iWXrNRjTm+jQU5Z6P71VMa8ay70yFVT164gnP5Eb/EyzwThicw 9ojHh01rzJtLIRCHqOyCDSEnFfcjNfHsGHDPOonvXnwt3u4sACGELJsKEGK5BAQIYZFl oskNNlTfRu+78XmiWuN023NK+kBwEJCV+CWWjxPMXUo3GyXwpjtMuIiJii+eIMP7EyiZ 8dxj45ulnbi4BK11I7qM2gVPiPapB/6lYakTfNOe0evGJ00KulGWkbpdl+Q95B7NEwGP Kz9A== 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=CbcVF6J0KDbkpD4NFjsmD9Sp0o+LDZsZhXDUnXGxxY8=; fh=Io7x3OacpefNRRNKoYAzVE0aWTDwD7FRzyCdD1e3AN4=; b=MIwWkS5nyqsfaFS9EmEZ/EBHUyyrcrgqxFlFu1w+sj58byxpsdc4ha919RX2x6lruk RyQPhD1/9X+1wxmAbTPlxvbpkzp9JELqEQC8e8+NKNq7p8Q3ClXAiMn3Q6a3mMywpAOI Fsc1A8m74xEjitDhp9Fe/ugnQr0UrFEfb1OHy9/Nb3eu+4tDSW+8ydi+fXD/2oEn2g9B YQea0lCGhQgEZjBUNxJMfH0AQ3A+Q4nlnsJFmgHebzXKzIohHNo90XlH2Dx/zwYTqXAe rqj45PdAwqFjsIox2dJ+S715k4niDTRQU86hiyxdo5kOX47vQLKbHz3EjWMX1mpYZ8hw HWFQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=qfn0pr6k; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id v7-20020a170902b7c700b001ca93040adbsi9235037plz.653.2023.11.27.14.13.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 14:13:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=qfn0pr6k; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (Postfix) with ESMTP id 1FC0B805A7FF; Mon, 27 Nov 2023 14:13:10 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233644AbjK0WMc (ORCPT + 99 others); Mon, 27 Nov 2023 17:12:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44002 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234161AbjK0WLk (ORCPT ); Mon, 27 Nov 2023 17:11:40 -0500 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1E7CF12C for ; Mon, 27 Nov 2023 14:10:29 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5c994880223so71192747b3.0 for ; Mon, 27 Nov 2023 14:10:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701123028; x=1701727828; 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=CbcVF6J0KDbkpD4NFjsmD9Sp0o+LDZsZhXDUnXGxxY8=; b=qfn0pr6k8F1baAihRSRQbB96jNImBoDJ0lGgMPqiRsWQKUFUkD/ZLlrxleou5txL4x nwllzS6Yu7Rx5E2mDiDYHvv3UuxAei5JG3iCaXGLZDb4IGratMRif99wP3sKgu2cZjZN aOTHdaiuTEAVezBZI8gVi/S7KEHnNZT+CR/3yKqJa0CHZDFDYKYD6odlat8UfvSEGdN7 FfM0jmdcjwDWedfbcjxUeHyfq07JR3rMqhSBbSVXBjP1s5MFuqyqUuaL4z5sbefgzdNk NL4yKtJfvcm560CsR4G9DXDPFrS5hVZel7FpCWiUc6W4JMnj2TwJ2I5DkUY5U/PkhGol BLXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701123028; x=1701727828; 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=CbcVF6J0KDbkpD4NFjsmD9Sp0o+LDZsZhXDUnXGxxY8=; b=e/Q6FrewnrNLW1e9zFJXt4qcE7d677fF508AHHs/qR4RBBa1HLjrJQP+FgUPBjekre Mqr+pPXrOYOMx4rpXgggP3mZ4cW9pWXuF3o8GHP2SyTAxhzWGmspSrxEbIBRM80UPKxJ e2q5jIY3hUeFDPd6VjmAp+xDFkg6rnp4teyFUaidhqdJSraf29t7Cb4mk+jLrl4JToPg A5ftTXRkiimZqqCMWdwvwd9raBMpsdTjOLlA1NQs9oWwWLzlhRsT18rnddTtJDC9GdRc EEWUL/u3jLZ8jzIzgHcziioIMUYaL3QULlOQEnQz0aSIoiGnUHVSEAyDvIBsVw5PN0jh 88Kg== X-Gm-Message-State: AOJu0Yx9bGHj62WKW2czAxyBHNxKrkXfS2/yENLrmY4BUiYw0m3qdspi d+K8h6lEk1oYFCzTPjfIfKrPoVOkRGMj X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:829:6e77:9093:f39b]) (user=irogers job=sendgmr) by 2002:a05:690c:2e10:b0:5ca:400:f1c with SMTP id et16-20020a05690c2e1000b005ca04000f1cmr376976ywb.8.1701123028227; Mon, 27 Nov 2023 14:10:28 -0800 (PST) Date: Mon, 27 Nov 2023 14:08:40 -0800 In-Reply-To: <20231127220902.1315692-1-irogers@google.com> Message-Id: <20231127220902.1315692-29-irogers@google.com> Mime-Version: 1.0 References: <20231127220902.1315692-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.rc1.413.gea7ed67945-goog Subject: [PATCH v5 28/50] 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" 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 agentk.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 (agentk.vger.email [0.0.0.0]); Mon, 27 Nov 2023 14:13:10 -0800 (PST) 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.43.0.rc1.413.gea7ed67945-goog