Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp2186484rda; Tue, 24 Oct 2023 15:28:39 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGGBsc0lJKK4f3uXjoleKwhorquFJpX9SKJo0UHl9Dtu+fc9vfxO8la/XLzagq0NwsxieOz X-Received: by 2002:a05:6a20:748b:b0:17a:f1eb:e143 with SMTP id p11-20020a056a20748b00b0017af1ebe143mr4601413pzd.26.1698186519559; Tue, 24 Oct 2023 15:28:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186519; cv=none; d=google.com; s=arc-20160816; b=tXKc1xVRW/LuIbCqJo9TFpz+M/xhC/PFgPeqwTjOfknWaq1w2QYnTL2E4QXaW4v/zb ly17JVuycQgmkqz31FpUbWJJga8Km+tBQ4T6V8OEZx64z0gyUQJMqJHPbBkG+kLbbpqY bhtVICgaSggeu4RlUbkQum4cFVzK6WnRznEmtl82oNViWb8oDullfoeiU/hFCZn3isdY p7BIoUm/fORSZYxmYLoYSMmdaF6bp7fQVlH9I+JVzYEM2PXQqQwS1+p6/S9eKGh+3JvT nxh7CRtKJdWA5mCfHn8yYQA7/EPpJQKLlS9X1Tf2HAsMTYeQDq2NB4LFkZiNZ62/Kg1v /GJg== 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=f/kNgXH1Uh+lkDKn4udeU01EF+mxZdpavmj5nGxyL2A=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=uqG8+d0T/FBYUOS0Q90oMVKHl5gkDNxdzWIZxAS0REe0KxqO3l3rKKvBs4HQh/q6rF qels1OnCmtr16SS7PIBZAN1hZjXkRJfslJfgO5zu6F5yiLS9+YrcIo8+pLwXMD1NNh1r 6jzBDgIP1FGZN0fY9yCdXZKs2qCXeXxMJqtwW7qyFncg2HkkLZDtYBnywj+BDn7pX7b1 P5YCOlIGojJWtelZN9QcLYE5Vy+uJZfRPjvstveFkmSeBAl3JYi8pwn3V2Z14WJ79u0k ZuUCm4tlNI4iXAr5XGqJw7pfBTbHtCC2j/O9vPETcmTukn14txGcmHwoKQAtBFHQPYjL fvAQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=XvJXOZLy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 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. [23.128.96.36]) by mx.google.com with ESMTPS id s1-20020a17090aad8100b0027d0c3507eesi11643205pjq.32.2023.10.24.15.28.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:28:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=XvJXOZLy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 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 1C3CD809C116; Tue, 24 Oct 2023 15:28:24 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344654AbjJXW2A (ORCPT + 99 others); Tue, 24 Oct 2023 18:28:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59896 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344660AbjJXW1f (ORCPT ); Tue, 24 Oct 2023 18:27:35 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F163510F4 for ; Tue, 24 Oct 2023 15:25:45 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5a8ee6a1801so63570507b3.3 for ; Tue, 24 Oct 2023 15:25:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186345; x=1698791145; 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=f/kNgXH1Uh+lkDKn4udeU01EF+mxZdpavmj5nGxyL2A=; b=XvJXOZLyXI3pgqCPjcPyjhB/+6CS50YS/z6g04SRTQdp3wZ+Ct5TwGaohC3c+kvMFW VxPrRlzdeSmaqV44r0kHCXNm5yBNk3UfUCVXUMmtcJwc650v8sdh1I0WVDFB/OwZ3x2o 3KbU1FRFVi+WAxU9u3xo4sTczU97ySsf7Y544dtcz/lbS6TLSCRoYhnlpBwHOeeZH5Cy sSOdnBbDOrhzdbRlwSBujoXqW/mk8bw7/x3yElFW+MPDWEHiAtvtx3Zis5Hih/3T1Z1L jTPQTU0iBufLjZTqaBXalfjhpwfGuG8Bi+Pl+jeCDD7kk19uxzHogOYK8hlwrgZL6BLU wvFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186345; x=1698791145; 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=f/kNgXH1Uh+lkDKn4udeU01EF+mxZdpavmj5nGxyL2A=; b=cH+RXUP7ixVClAqPRGmJvwT+sxHy31FFqPBOZRjJMPIEEs0BUwkcbDn5xz6vYxYt1G w6VFUsb/I74kia+TPArsQ3ZEM7M+5c16FPZm5c/QNNnlm8BlgNg7LusIhwfzqXA8lqH5 ma2VLOGOC0srJO4iWG7c8wgJCFF5jfZ0CJAM5SE9zC7gdLAH8zqjjMGa879gg96+7uLS ciPtCgZICvD9cTxrOSZzxQlYJQELQGYkrAJzPSm3tr34R0/Gsvdr1T/7KPXw8z1aEI/W gZzm1eZjw+Z+TtDijsQBByGUDBzgCNXoInYI3yGee6SmXwclk6aLz/VOrAUIaKZqQU9H 9b/A== X-Gm-Message-State: AOJu0YwtuKPwK+L0N7JN0XmCoBuuFO7TS26s7Pxn3b9LLO7Zm0ww5lio Yg+VpqIc9npp/rUSOzc84YROZGvmGvVW X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a25:aa45:0:b0:da0:3e46:8ba5 with SMTP id s63-20020a25aa45000000b00da03e468ba5mr57420ybi.8.1698186345124; Tue, 24 Oct 2023 15:25:45 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:44 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-42-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 41/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 , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , 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,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]); Tue, 24 Oct 2023 15:28:25 -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.758.gaed0368e0e-goog