Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp1363524rdb; Wed, 6 Dec 2023 17:22:18 -0800 (PST) X-Google-Smtp-Source: AGHT+IEuMUuOLFf/cclUl/pYRcmPP73YzIiEw4gk9IIT0T91ajKgMKjcBOtp+Bbu4pOGUfwsq9D0 X-Received: by 2002:a17:902:900b:b0:1d0:6ffd:9e27 with SMTP id a11-20020a170902900b00b001d06ffd9e27mr1541343plp.121.1701912137981; Wed, 06 Dec 2023 17:22:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701912137; cv=none; d=google.com; s=arc-20160816; b=tvPriD8UqOtp+egXQV3oZlfVBX3TNqcoBXcdHFH2SLKN/sNAzfXOEtnxqQGyYw8nzM yD/7bU8+fZtAYpd0QWEVdsETlvwYFGHYUVreySREuU/eVZOzAa8PqD8fQ/leyNu3pVlA mEcX43Q0Frr08HjRZTQoEaVNaw4rn5QmVKIKN20x0kbmCJvHGco4ztNKGObOkVpmzUrS nqiOsWe2sM5epLbwqgL0BfZkLHvbyT/gtX/HbIxRWqZOJgbW59a1lxXo3VmGDx4bMpt7 +umyp5BfY33UVZmsW+0LX9k6Gj501JEUEEjjnzbkR4r4iJEC/1e2Z5IbocNCv8zp45BE E6Sw== 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=aFxV3Xhm3dKZsR7pRkHrvO8DCjszBbrDjVot/5oMgb4=; fh=Io7x3OacpefNRRNKoYAzVE0aWTDwD7FRzyCdD1e3AN4=; b=Tlm+mIrykZhqkS3iJIMJ4wbUdT+lS7l0767exV5Aj+aXPpKCdSRd89S+vnp/CUWQ9L BMCPQXZInuyaLkjnRm/9Oiw8yPIvBOmH/3caIak31HYA4uMCSYb0ZfrKjD98nkEPppdO tAPx8TO+m+P0OV6rqWU0N3F2XifslFyqn3rq/U09yjo94VcE2WsUZVCKMdlJUe1Zejko 867N0pikM5jhsslMKaG+DgQtIWYRkgghhhF/S6aMlbQPbZ6Sr9RJX5cHB0WmaSL3Oty6 nj4CzHku2Vu9x7J6wn7Xs4bsUtkBk2nJ1mmwxVZ73iYI53lPjdUoLEM4eC3JXJkeDzyC UcUw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=DNsTjXX+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id c10-20020a170903234a00b001cfee3a6708si178243plh.150.2023.12.06.17.22.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 17:22:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=DNsTjXX+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (Postfix) with ESMTP id 507F5808349D; Wed, 6 Dec 2023 17:21:52 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1443113AbjLGBTb (ORCPT + 99 others); Wed, 6 Dec 2023 20:19:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57752 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1442083AbjLGBTB (ORCPT ); Wed, 6 Dec 2023 20:19:01 -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 375DC2136 for ; Wed, 6 Dec 2023 17:18:29 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5d340a9cf07so1747757b3.3 for ; Wed, 06 Dec 2023 17:18:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701911908; x=1702516708; 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=aFxV3Xhm3dKZsR7pRkHrvO8DCjszBbrDjVot/5oMgb4=; b=DNsTjXX+m329jmn5Y7tc9qHuqULRYS0ZYQ5qpMeiFukTBmYufv+mUxwJu4mxdS4ddM I0ZmG9vXZPajBTqv0RylidZ6FQrxE5y9YI9Y0o+1F/wcqqX6ZFKK3V7ogQapEPP198Dq t0BjjB+gfEl+spOo7waFtLbiLrPHfdfL+9xhkmnv+kL3hoTRKvdB4nBroiM5J2zGAdOX p4iGDmyMSN4WtfWOFWSRT/k6u6qvamE14fVklZA/6trtvQwAatwu29UTOeQUxoVF5H6y 5gAHnUA0HimBJ/5/Y+eMbaWfyD4be5PgIFHinLyCD2K5siDi1kN1IUJyRFC94sP0d8MD Tljg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701911908; x=1702516708; 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=aFxV3Xhm3dKZsR7pRkHrvO8DCjszBbrDjVot/5oMgb4=; b=H+XlZizNO8o/benp9CDagUok6SNzzdBtg/ZbxJLAxBIe+LYT1xrVGk2qBdtv+nA7Hw sSn7m7AFTIoALsFpy0JYIDobYsTDusHnRlWLF5m/HD9QS6LavsdDom5XwGpjsRU6YCA/ s/x3l89hnJl6NzWavDdl96P60HPH+mgGrdprUvUJbnH3PgOmCN8k8bHQ11uikOvGH0+x mRHJgscht9b7WKMtd9na55M+nGofEFxQVwXRGhT1nWvTOeG0RLSF+DBjiH5YbXRtdhNk 3qUjE8DDuwSjw5rZfzyhsWyvbLWG8rbykTRhfo4aOJxUEg7FlWY7KS8GaUboEiiWbP+t lRpA== X-Gm-Message-State: AOJu0YwLmwF9iM74qYwWJ6GyhN9Q4xw6M3KqSaci/GgDbuuReTBCZRdg 395iItRI3BG0SVqPMIOyO/0o4/1U5dox X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:35bf:293e:7696:34e9]) (user=irogers job=sendgmr) by 2002:a81:99d0:0:b0:5d4:3013:25d4 with SMTP id q199-20020a8199d0000000b005d4301325d4mr31873ywg.5.1701911907896; Wed, 06 Dec 2023 17:18:27 -0800 (PST) Date: Wed, 6 Dec 2023 17:17:00 -0800 In-Reply-To: <20231207011722.1220634-1-irogers@google.com> Message-Id: <20231207011722.1220634-27-irogers@google.com> Mime-Version: 1.0 References: <20231207011722.1220634-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.rc2.451.g8631bc7472-goog Subject: [PATCH v6 26/47] 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 pete.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 (pete.vger.email [0.0.0.0]); Wed, 06 Dec 2023 17:21:53 -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 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.rc2.451.g8631bc7472-goog