From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796179178; b=47An3GhkifoMAqCU3B+5Ovf6d2/3lDtvKXY4x7Jc4R2NfVmxrXRdPeuzpFFNrKt1fm3u oyt+XcXNj0TJhneT4K7lM8sFuRev+0Fne82aFyxWFWo8939UuYFQQ6R1lELsEmbLzGxKq NWM1s18zlzP0sP8mxpszzkiNjqXaFRjx/AqniVojF//xhUjxiI5JQV+nCgG0uZG+EBN7k Z3QZV++uq+YZms9XRGt/czC00FtoBxprf+FDS3q9jeA2nxd+uurs7nlsJ5FwE4G40gV9n 6LBxASCpCSCP38jG0KWnwHKrOGNf6h+IZ+l1Pt/7B+rHuzQqmqp6ymHMr2S2nN2GoiQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796179178; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=E442JYo85y3qiFHINqVIiTjIuoKvNijr60QcWJGv6Ro=; b=Qr+MVDIBAkwH345Nz4EREEJsfRLTJC6NZopKptA/TfV5PuiNDs5HR8V44nm33AZ81ghR I+qzkNgzGd4f8iOybAu92KIhBI7gXeTdomSjEMPL/LHOZrC912iljiNC5buEAEaHyjoIW +45HMbj4+wQwlo4SngnAk5+z/Qms+Ti67/0jqGeSCCETSEw7A/wbcyzQ4nNBxB0Kb2QrN bvKTCulwC0/Sypgk950ih/FNyCMbyDDokKpKToAc7TeV4CMuaDJO0xkJI1vMnmaT6OxF9 D9inviN1ee23rd/KPPwC+l0bk8G3PfWjiat1C+8U6eespBV4WmeiVD7ZMsd5VGzuEOw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=nCjwJi67; arc=none smtp.remote-ip=209.85.210.199 Received: from mail-pf1-f199.google.com (mail-pf1-f199.google.com [209.85.210.199]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 20BF3606B7D2 for ; Wed, 2 Dec 2026 02:39:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 20BF3606B7D2 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--jthoughton.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=nCjwJi67 Received: by mail-pf1-f199.google.com with SMTP id d2e1a72fcca58-8a064f294cbso9841683b3a.2 for ; Tue, 01 Dec 2026 18:39:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796179177; x=1796783977; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=E442JYo85y3qiFHINqVIiTjIuoKvNijr60QcWJGv6Ro=; b=nCjwJi67URp+9dF+ejR/Wcb1+pfx2DDmH5XmWwwFNUK4E+XIFJsMVsDz4cJ4NM73ke eG/4ELaBAHeRfH4E6E6V4ioqCtgkJjZe3SowvcvuegTU0W4n+B1fkf8qv0iDEdI2H616 PRmBK6i7Dm6ZeR7JNjPhyC3qep2aWgwkEPjHFRzPrQUwf/BJ6T50yzl7hQ8xCWKC2aQY he9Q7DEyLJvjgWR4eEZY91RwfDifIOuhaQFLNOG60kSukDFSE+wc0Lfn1104Pna1QjUz AhGmd1cLKTpuXzTHIfeEQpxx5U+zbCuOdoSmdgySkH6eutpr8/W6Pr+4YwPt12jOq2Bq x/Mw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796179177; x=1796783977; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=E442JYo85y3qiFHINqVIiTjIuoKvNijr60QcWJGv6Ro=; b=xMZxfjed09yVcg2cK2c4SPVYpswWaCbNKuMVPlr4LGgEAQOBLaZ7R6y8NezeJZP20x pSTiOcbTkkvgBJVHXdOnJ+Mj8xv9VNXl4/xGKfPlemFwXvxwc+iTyFwCPVdJjRrcDyK0 QG9XdTsz97TjhtN5+ApxaNQp5Wcg4HDVjUO5yzK+6XF7wy77DRivDNOrbNaPSpk18xM8 wQEU0KVY+RyPQrXp9Ce7gUJnfNXdUMzPu/6e6Z1Fgq4TiyS5R3usgyInFljzbsxHD7mi 4g5ivsLGhZEOQPCogkICUdyAzqq4/BftQmh6nMpCrOW+PJdakhXRz0z0p96GJer8t4zb lswQ== X-Forwarded-Encrypted: i=1; AL+hErF866NGlHfdj1/jG68UwGCeX5J49t/UtLPHdKFdKRVkQdNBjhNuvHxdmVhBYkEMQ+dgXiaXDIrBBlwVTwtk1b7+@vger.kernel.org X-Gm-Message-State: APuU4Z9Yhk8xVSaWK33gqkN7VUEdgUFVwNZQXLhzv9O7wI0I23vqZPtg EUtWAFkCS72eatJcVToBsWySd6JpjeeNe2zAFaTCvjvFlk+RNaicERzUltZXEQqtvSnPl1pIQsd x49wpABWZj85i5m9njehTWg== X-Received: from pfbld1.prod.google.com ([2002:a05:6a00:4f81:b0:88f:f68e:660b]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:4407:b0:89e:cfbb:e8a2 with SMTP id d2e1a72fcca58-8c8793b7dd6mr36946682b3a.4.1796179177082; Tue, 01 Dec 2026 18:39:37 -0800 (PST) Date: Wed, 2 Dec 2026 02:39:15 +0000 In-Reply-To: <20261202023920.595207-1-jthoughton@google.com> X-Mailing-List: linux-perf-users@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261202023920.595207-1-jthoughton@google.com> X-Mailer: git-send-email 2.56.1.911.g883d8bcb20-goog Message-ID: <20261202023920.595207-9-jthoughton@google.com> Subject: [PATCH v2 08/12] powerpc/perf: Don't read user memory without an mm in BHRB decoding From: James Houghton To: Andrew Morton , Alexander Viro , Arnd Bergmann Cc: Sean Christopherson , Paolo Bonzini , Jim Mattson , Jens Axboe , David Hildenbrand , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, Catalin Marinas , Will Deacon , Paul Walmsley , Palmer Dabbelt , Albert Ou , Madhavan Srinivasan , Chris Zankel , Max Filippov , Dinh Nguyen , Simon Schuster , David Sterba , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , KP Singh , Matt Bobrowski , Peter Zijlstra , Arnaldo Carvalho de Melo , Namhyung Kim , Steven Rostedt , Masami Hiramatsu , linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-block@vger.kernel.org, linux-btrfs@vger.kernel.org, bpf@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-riscv@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, io-uring@vger.kernel.org, James Houghton Content-Type: text/plain; charset="UTF-8" power_pmu_bhrb_to() reads the branch instruction at a user address to compute the branch target of a BHRB entry. It runs from the PMU interrupt. The BHRB is cleared when a task is scheduled in, but not when a task exits, so between exit_mm() and its final schedule, an exiting task can still have its own user branches in the BHRB. On ISA v3.1, which only records user branches, they stay there for the rest of the exit; on older processors, the kernel's own branches push them out after a while. If a CPU-wide event with a branch stack samples the task in that window, e.g. during exit_files() or exit_task_work(), power_pmu_bhrb_to() reads one of those user addresses with current->mm == NULL. (The task's own events are already gone, as perf_event_exit_task() runs before exit_mm().) This doesn't read another task's memory: the mm that is still loaded is the exiting task's own, so the read either faults, if exit_mmap() has already torn it down, or returns the right instruction, if another thread still uses the mm. But a later patch in this series makes the uaccess helpers warn when current->mm is NULL, and this path can be reached by anyone allowed to open CPU-wide events (CAP_PERFMON, or a low enough perf_event_paranoid), so it must not get there. Check for a NULL current->mm first, and treat it like a failed read. Signed-off-by: James Houghton --- arch/powerpc/perf/core-book3s.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/arch/powerpc/perf/core-book3s.c b/arch/powerpc/perf/core-book3s.c index 10ded865583c..88425e40854c 100644 --- a/arch/powerpc/perf/core-book3s.c +++ b/arch/powerpc/perf/core-book3s.c @@ -471,6 +471,13 @@ static __u64 power_pmu_bhrb_to(u64 addr) return branch_target(&instr); } + /* + * The PMU interrupt may hit a task without an mm, e.g. a kernel + * thread, which has no user memory to read. + */ + if (!current->mm) + return 0; + /* Userspace: need copy instruction here then translate it */ if (copy_from_user_nofault(&instr, (unsigned int __user *)addr, sizeof(instr))) -- 2.56.1.911.g883d8bcb20-goog