Patch "perf/x86/amd: Fix crash due to race between amd_pmu_enable_all, perf NMI and throttling" has been added to the 6.0-stable tree

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This is a note to let you know that I've just added the patch titled

    perf/x86/amd: Fix crash due to race between amd_pmu_enable_all, perf NMI and throttling

to the 6.0-stable tree which can be found at:
    http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary

The filename of the patch is:
     perf-x86-amd-fix-crash-due-to-race-between-amd_pmu_e.patch
and it can be found in the queue-6.0 subdirectory.

If you, or anyone else, feels it should not be added to the stable tree,
please let <stable@xxxxxxxxxxxxxxx> know about it.



commit d9d9ea383fd81ec7c6c99102c6253a102c83a146
Author: Ravi Bangoria <ravi.bangoria@xxxxxxx>
Date:   Mon Nov 14 10:10:29 2022 +0530

    perf/x86/amd: Fix crash due to race between amd_pmu_enable_all, perf NMI and throttling
    
    [ Upstream commit baa014b9543c8e5e94f5d15b66abfe60750b8284 ]
    
    amd_pmu_enable_all() does:
    
          if (!test_bit(idx, cpuc->active_mask))
                  continue;
    
          amd_pmu_enable_event(cpuc->events[idx]);
    
    A perf NMI of another event can come between these two steps. Perf NMI
    handler internally disables and enables _all_ events, including the one
    which nmi-intercepted amd_pmu_enable_all() was in process of enabling.
    If that unintentionally enabled event has very low sampling period and
    causes immediate successive NMI, causing the event to be throttled,
    cpuc->events[idx] and cpuc->active_mask gets cleared by x86_pmu_stop().
    This will result in amd_pmu_enable_event() getting called with event=NULL
    when amd_pmu_enable_all() resumes after handling the NMIs. This causes a
    kernel crash:
    
      BUG: kernel NULL pointer dereference, address: 0000000000000198
      #PF: supervisor read access in kernel mode
      #PF: error_code(0x0000) - not-present page
      [...]
      Call Trace:
       <TASK>
       amd_pmu_enable_all+0x68/0xb0
       ctx_resched+0xd9/0x150
       event_function+0xb8/0x130
       ? hrtimer_start_range_ns+0x141/0x4a0
       ? perf_duration_warn+0x30/0x30
       remote_function+0x4d/0x60
       __flush_smp_call_function_queue+0xc4/0x500
       flush_smp_call_function_queue+0x11d/0x1b0
       do_idle+0x18f/0x2d0
       cpu_startup_entry+0x19/0x20
       start_secondary+0x121/0x160
       secondary_startup_64_no_verify+0xe5/0xeb
       </TASK>
    
    amd_pmu_disable_all()/amd_pmu_enable_all() calls inside perf NMI handler
    were recently added as part of BRS enablement but I'm not sure whether
    we really need them. We can just disable BRS in the beginning and enable
    it back while returning from NMI. This will solve the issue by not
    enabling those events whose active_masks are set but are not yet enabled
    in hw pmu.
    
    Fixes: ada543459cab ("perf/x86/amd: Add AMD Fam19h Branch Sampling support")
    Reported-by: Linux Kernel Functional Testing <lkft@xxxxxxxxxx>
    Signed-off-by: Ravi Bangoria <ravi.bangoria@xxxxxxx>
    Signed-off-by: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx>
    Link: https://lkml.kernel.org/r/20221114044029.373-1-ravi.bangoria@xxxxxxx
    Signed-off-by: Sasha Levin <sashal@xxxxxxxxxx>

diff --git a/arch/x86/events/amd/core.c b/arch/x86/events/amd/core.c
index 9ac3718410ce..7e39c47d7759 100644
--- a/arch/x86/events/amd/core.c
+++ b/arch/x86/events/amd/core.c
@@ -896,8 +896,7 @@ static int amd_pmu_handle_irq(struct pt_regs *regs)
 	pmu_enabled = cpuc->enabled;
 	cpuc->enabled = 0;
 
-	/* stop everything (includes BRS) */
-	amd_pmu_disable_all();
+	amd_brs_disable_all();
 
 	/* Drain BRS is in use (could be inactive) */
 	if (cpuc->lbr_users)
@@ -908,7 +907,7 @@ static int amd_pmu_handle_irq(struct pt_regs *regs)
 
 	cpuc->enabled = pmu_enabled;
 	if (pmu_enabled)
-		amd_pmu_enable_all(0);
+		amd_brs_enable_all();
 
 	return amd_pmu_adjust_nmi_window(handled);
 }



[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux