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=1796179179; b=PXDB6PsQs7wePy8i7LouZuY257ryiHy9iILUbT9ob1I/YUtIB1oEbWoOr2go48x77L7R RnL8EXfsgSB3WoLCBIh53zUr0sKK0yNDKWxAmv1jOxCydEReHZEW/RhZH8npR8e3Zd+2E zC/2TG757xi/Clkx8hS7wWDS+QxVLUba8ZkRnDRKNnZ0hp6/2Tzo1W1XsebIeMMwg6M/Y W0rB5Iyla06d/gOOsffKmggTrE8BJ6257HMNQJli36/mIauvceWw+1BmeDqNE33PWOTwB NjZD9V8h5E4QnY3a6mg+ZiB2dLEQz4R5i8InTuYni3y1PoeUhxCCILrojbBR4gyPXSQ== 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=UzS0bL8yeJ0kdDdGCDedVBYjxNb7/upkzj5qdeaQEuOFHaP0FskQbHWp6aAo30cbDMVX uJFTkZl659r0qELkCYbhDA4ioScxE1k/fNN6EEeayZUpiaUUywYkIKpyWOncy1Cn6CwvL Uoo5kyvduzlxZs6Xb2DLRDeLc8RFtWVjsRdIimXEOflpUsHibOWe9o8rJvor3DHQiz0qO BBrnfoHqwXpnvQtufu6kBF5uSF8Zhrx+ulSC/rSSwIap132jq7oA7F4oPYdE2ykpknWtf kFt7jUDDfUU8RCZ2Q0z18lGGgB4quZr0kaZlwgqzZttLCXQhe1otMscmY5q8cF2C1JQ== 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 20BCB606B7D1 for ; Wed, 2 Dec 2026 02:39:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 20BCB606B7D1 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-8d3a6306dadso3251916b3a.3 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=efnVBK43jyhFinopVnlBCtmjSKCoePvUuUp/v+TSPeZ7VZLtKt68S+QI8eP7knd2+A YhAAOaaIxQXwkAXc1VP+69tfYc29hyc1PG5cZrdAuglIbI7izGCu6IFQMzNUtgLSwEvn z8qd/lvUTltEW7LHnrxVcvb0kSdLvnNYu+63h8AfTT4AEyphYg9LGb746qzLe2+Ggwj8 1x2Z8rW4xR6GktXYJYMtBdxr+RKiTGXSjMzEmlLQ7/jf7xr/2jPPIAsv60dxJmC0emRD fBHvIco5J/U6VTkFrAyjzXliv4AiLsvP+0OapXQ1kOUDE3tJRqJepdFum903YeWgbkRf XZ0g== X-Forwarded-Encrypted: i=1; AL+hErEMuQyPcZj5asRr4XEUyv2D/4fPuZdzj1781LIBdXjLGzJ/nfg20Qbml053SLLK7v8Fy0o=@vger.kernel.org X-Gm-Message-State: APuU4Z8bYnaTpYNM7r04xKoM6k3ILUPUT5QA7fCvVTK7ywLuqqvwzLRH Atvli8Ffl0iatW79SMUxA+p8ldMmTzlQtkT49mPl9zoSxwWybXyzPioG0Z8w3XVUCA+pZaq0ZSq 2pfNfb/SAYi3VEZm9e2GZdQ== 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: kvm@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