From patchwork Tue Feb 6 03:33:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 197161 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp1299907dyb; Mon, 5 Feb 2024 19:36:21 -0800 (PST) X-Google-Smtp-Source: AGHT+IESjGDxqWT5gsmctyYStuVZsAbh0OCtZSEFNRTUo+nwyjpiyhIdPN8DblupLiAcitBwEtge X-Received: by 2002:a05:6830:921:b0:6e0:be61:850d with SMTP id v33-20020a056830092100b006e0be61850dmr1590696ott.1.1707190581212; Mon, 05 Feb 2024 19:36:21 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707190581; cv=pass; d=google.com; s=arc-20160816; b=FMmrGXXY+wRv/lQzA332KAFl4wMNbUL7ACm3kCBXelMduisDmQiMAfCNWmXlrB/Ktl cBHdhYIlSg/LrcblYJ8IzaNv3LW5C+UrtJHwZSQuj4ka7+BlvAi4ltfuIJSO9N5xJKli q6KyBnSS3NGT4Sr1+H5EbZ3Nw5GhmG6Xzaf+uJmtolDBIC+Apw0fxZ932czFjlGAMEdR 3W7hk8THiUZ7SOCYdc6LqZMX8qigcfq0Dor7rMUswRglKgFwaIBN9wWMvGUlm4Kb7P/d 3lnp1/iJqmBzID4GlJrLyAW2BpZyShp7haldKXW0LqOjg06so1kg0NbuZHjQ8ZARWC8g DRtg== 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=Ay5pESXQDW6zysG3DCALcAImBiyx5FS+UI74K+BRnd8=; fh=RR0heUoXZixhzeTNHAo+pZQS+3DNOBG04h9fEonTkaE=; b=up8BiW9PTlBmk5wdPqOrCGgIMjmqjPvGEpxtxJyeMcaUJs+O4+4t4XoslY9qxUHeZD /UhEbj/zqX5e1WwOlswfBvzhQ2pJTgrVzhrPlRL+PP5UnPK4uza1Z0Xw+efWvWFtrIYZ HLz6Qaol0HqjIN7HFywwwQ4JArFE1OEZa7FwK5TJwrXJy+4QtDr3DJeqVqoeAN2uGEY5 PGseb8Oj5in63Qj24sI9de2XMsugGH8ofIlh6TttwiWayNGt3or1zoLUJUxcBlL6krr/ tvkF7YV3VT3AR0pLBwqc14eppdtN4TjBqy0oaTRWHxc39U+TLSYYq6VBCSfWTZfEXa/m gRCQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=DZ7AdCqI; 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-54262-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-54262-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=1; AJvYcCWxW1Iol0jDzQ94Uv+Pwzs5CK2wLTdMoZReYum36Yx4xvSbGEMaAZZLVvWSVr0gKjDVmHm9glSDqC8Xv3XjcKNRk1Au+w== Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id r13-20020a63e50d000000b005dc0af6cdbdsi918440pgh.298.2024.02.05.19.36.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 19:36:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-54262-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=DZ7AdCqI; 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-54262-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-54262-ouuuleilei=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 sy.mirrors.kernel.org (Postfix) with ESMTPS id F0F2CB240DB for ; Tue, 6 Feb 2024 03:35:13 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CBD367995B; Tue, 6 Feb 2024 03:33:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="DZ7AdCqI" Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (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 7F7D2762F3 for ; Tue, 6 Feb 2024 03:33:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707190417; cv=none; b=KGGClSMl2nW140ckJcJHoMu5gJxewitLOZNbry8czV8BwKtth9Lf/QmVSReBaVDoD/0yXE/2L0oHGhxgLlGb7NFQUAHmMr5Bdh4j9Y4gyv5v2ejP+ZTBaJGI66XOXHDRGpira3H6JAwUURGMhfVjGvv+GBSOzescNKTLk51dHkg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707190417; c=relaxed/simple; bh=5X1qsUi3skYX0+XJLWX/k8pq64P7f+cfj6l98r2VoJ8=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=BlBh4MWTKM0eLOQHbnNDEJK20tdY90NPDYxUjoSF1DB7KbZ0bg3tjfjB0KyvQ2KeTes7KliNqXFjMUSJ6JcGmdLjcHLbzcuCvnr1Umm7Daq+Fcq6EyGedX9kVonuF5sa4XKSHbug/HI6ztdolmkQgERWzly4TCey/LpwCEduq0k= 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=DZ7AdCqI; arc=none smtp.client-ip=209.85.219.201 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-f201.google.com with SMTP id 3f1490d57ef6-dc6b26845cdso8644669276.3 for ; Mon, 05 Feb 2024 19:33:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707190413; x=1707795213; 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=Ay5pESXQDW6zysG3DCALcAImBiyx5FS+UI74K+BRnd8=; b=DZ7AdCqIW4SN2p0fFMDtzJZsQtPSR2SW8mD/U+JILSftqL8rvTaRyvTFmtXRpk77+0 usH3CaZILv3zi/4R5iZVfOOhJxmPYOKS/vbTefJmT1DdLWWLfXGUHHoB1CuUUjgqM61D SqywKLyd2ja6OT/OzJHMBpUJBWGAmniBKa+5Na0NvS9OwH2jnDHH2pnR5GasxDGYA3EX uJZ/HkhrRS5HhPQ4xQbY3xzjSYbds2vSuM2b5+3S06KwcCTjh2iK0uKKquTq5yme3OA2 Iw9Mm5nnfyj2nQ+4PXEmMhcnAZuDuABnUeURZj+EaZYfO7sf33krKCNFTxPrEg+RrVV6 Wj0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707190413; x=1707795213; 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=Ay5pESXQDW6zysG3DCALcAImBiyx5FS+UI74K+BRnd8=; b=Aurhn2Zrc4pNdzDqo97I1vpk7UnyFRwzp823ECSvQpBud7Pp1SglEaz3NOTbn2Pzgv B8H2cWR9UZVI/agXud2noPQcDPxLECQ5f0WNfXuU57xjj96Rgu7Nb7h6l2Y8diZBLy9p bWwknQsZfYu/rGV5y4+D0DoKCzS1Bk3zksGEHf2FbCBHlk5M3LGTwAT/wFUETXI7atOC kvTnATKVBaFO35oxuL6q8J593rKGQVKpcquLTSUzHAn+Am4Yrz4PunKRxQl7JKob3Vur hAxu2TafAVqDLJTubE1Y/TFCmjBzXm9CpiGz24RDX7X2UIJx7maIIASFlnqLOwxD5tLL eCdA== X-Gm-Message-State: AOJu0YyzsnU/d14e+yjO5RALLjrZkHiHOEC3kk+ZJOcu6hBmzvNiK7d4 pkL7UY01FUSv1QqJYsP+S0KsZBw2w85hpV27RA9dfrjqD2qZHznN6/FIl9dqta43xk1/fqf26HJ fbpUMYw== X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:326b:71bb:e465:6f39]) (user=irogers job=sendgmr) by 2002:a05:6902:2785:b0:dc6:dfc6:4207 with SMTP id eb5-20020a056902278500b00dc6dfc64207mr113515ybb.10.1707190413407; Mon, 05 Feb 2024 19:33:33 -0800 (PST) Date: Mon, 5 Feb 2024 19:33:18 -0800 In-Reply-To: <20240206033320.2657716-1-irogers@google.com> Message-Id: <20240206033320.2657716-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: <20240206033320.2657716-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog Subject: [PATCH v1 4/6] perf maps: Get map before returning in maps__find_next_entry From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Song Liu , Miguel Ojeda , Liam Howlett , Colin Ian King , K Prateek Nayak , Artem Savkov , Changbin Du , Masami Hiramatsu , Athira Rajeev , Yang Jihong , Tiezhu Yang , James Clark , liuwenyu , Leo Yan , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790119070882982021 X-GMAIL-MSGID: 1790119070882982021 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 Acked-by: Namhyung Kim --- 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 f4855e2bfd6e..e577909456be 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;