From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.198 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796179179; b=HFbYg2jAxa0L9HyiIkr+qtaPzrfVjGlDvZ8WnXCPG4O1K7CXvy1vGymNOrcmAyCIqww8 b3wG4KIS9pWbnXge8E/RdrgJYozwO+uUkp6r3mq1+xr7UzFu2SnOT88229tjToCiueLML rw0gXqfM2tn7BigyoVjxwwOdPlEtUOKFRthH94xvBBmWTvJY4pWUW3Bf5MgpcUTI4TsXF uloqm/FLlt1Qp+/2D381JJQYAUzZJhQekjRQxx0dUcnvs55xaAzAJa8w9msaaRJaQ5dul Pom2iFrojG7erdt7/bRmfI+8D0zS3tZNh29CyAXIGfq2e4X/a6KjugOfFVHzANyoUug== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796179179; 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=k0wnTKmovEy9PtfMx6Nw5kIOicUCZwpq8/6ZPwx2ljLmlWn9F/XDAltWKT3ho+2uy8Sx lhKGOyjTwGXK7fG210rYUs63VUDUhbUICU5fZC61139n+lMFPbKmArTVdTfqBzQ4/FLBE uRnLj43G7yDTlNeXitV6xF55puv3rtYHm5/iAuoFnVUO2OcwY/pGsGcTXUiG7saRUVJm0 SV+x+pRT97n9hVu/kwuyBbzSwAl4hMWQ+95VmZro3ZqKZKOpYSBAldEidlE1fAiK6vDQ/ PWA4+1JvcE+Gt5QNIYQUqJt9hcu2zoihTRQOZQn6UeZ3e+JHUmnukqNmNhyE+exmRHw== 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.198 Received: from mail-pf1-f198.google.com (mail-pf1-f198.google.com [209.85.210.198]) (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 27224606B7D5 for ; Wed, 2 Dec 2026 02:39:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 27224606B7D5 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-f198.google.com with SMTP id d2e1a72fcca58-8a064f294cbso9841689b3a.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=jcPSjQ+5dV3bTSTVp+Xr5j7CLwOkuA1JfKCWmzzWpa29zrCJW2qv51YbT6C75rJh64 8K6BV2olkhDWHqDdWlaBRwxGCydzodIMjOkftyxFb/vs9DwoTmqswKbE8lZzgtxHmjw8 /8Uns/7n86OzsfSYzm4hr84ihxQc+tSkIAZYspFy+ArV9J6jsqYWlpjxOgr1Idqcx6+A Pzu5SIZ9u/FvsYIr8zaPH3srPc/He3B1YzrQRc1lUz4wYmhEXhL+ed5A5M1OB6Os+ooc jHT1hnl8mKTkupMEItgpY+H4F2bz7B9y6rEi9PKfk2Vz1u+Vv2fOifoxH8Ndvo8xoB5i 2h2w== X-Forwarded-Encrypted: i=1; AL+hErF0/V8lgmFbUdwpfCRI950JVbpS0J0J8QpLBZ9C9zwYyGKYcZNXwpNfda1yqgGhdWoZMiw=@vger.kernel.org X-Gm-Message-State: APuU4Z9AThnzEWDt6OU7NEbbSdsyyACkBq7k/fzck/WhtTrmRicEDZhj 4dyjUqzPaHRbTY68d2HRtZ9C+RXRNHCRmtHP2wG1DIyhU7LQ+jZqKRl/YKE6evXaHebNfffNc4m Gxzn8edKF1uxwzpLCu3UCNQ== 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: bpf@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