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 <[email protected]>
---
 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


Reply via email to