Linux Perf Users
 help / color / mirror / Atom feed
From: "Mi, Dapeng" <dapeng1.mi@linux.intel.com>
To: Ian Rogers <irogers@google.com>
Cc: Peter Zijlstra <peterz@infradead.org>,
	x86@kernel.org, Vince Weaver <vincent.weaver@maine.edu>,
	"Liang, Kan" <kan.liang@linux.intel.com>,
	linux-perf-users@vger.kernel.org,
	Stephane Eranian <eranian@google.com>
Subject: Re: perf_event: avoiding gpf on rdpmc for top-down-events on hybrid
Date: Tue, 1 Dec 2026 18:19:09 +0800	[thread overview]
Message-ID: <92339199-2330-4898-a8ef-d6416ef89e53@linux.intel.com> (raw)
In-Reply-To: <CAP-5=fXeQD6diq5c-+M5JB10JLi9_if9CcnBxOFX4VyoFcC9BQ@mail.gmail.com>


On 12/1/2026 2:38 PM, Ian Rogers wrote:
> On Mon, Nov 30, 2026 at 9:22 PM Mi, Dapeng <dapeng1.mi@linux.intel.com> wrote:
>>
>> On 12/1/2026 2:39 AM, Ian Rogers wrote:
>>> On Wed, Nov 4, 2026 at 10:17 PM Mi, Dapeng <dapeng1.mi@linux.intel.com> wrote:
>>>> On 6/18/2025 4:45 PM, Peter Zijlstra wrote:
>>>>> On Tue, Jun 17, 2025 at 01:36:40PM -0700, Ian Rogers wrote:
>>>>>
>>>>>> Thanks Peter, out of curiosity, how does the RDPMC fail? If an event
>>>>>> were rescheduled (even with RSEQ assuming the CPU the thread is
>>>>>> running on doesn't change) could the RDPMC with the incorrect index
>>>>>> also fail?
>>>>> So the problem with RDPMC is that it will #GP when used with an invalid
>>>>> index.
>>>>>
>>>>> Normally, when counting using fixed or general purpose events, this is
>>>>> not a problem, because every CPU in the machine has those. So while the
>>>>> index might not be the event you were after, the instruction doesn't
>>>>> trap, we observe the sequence changed and retry the loop. No harm done.
>>>>>
>>>>> But for the top-down thingies, the P-cores will have a RDPMC idx that
>>>>> the E-cores do not support. Using this index on an E-core will #GP.
>>>>>
>>>>> So the following situation:
>>>>>
>>>>>       CPU-P                           CPU-E
>>>>>
>>>>>       do {
>>>>>         seq = pc->lock;
>>>>>         barrier();
>>>>>
>>>>>         index = pc->index;
>>>>>         count = pc->count;
>>>>>         if (index) {
>>>>>           width = pm->pmc_width;
>>>>>
>>>>>                       <migrate task to CPU-E>
>>>>>
>>>>>                                           rdpmc(index - 1); <-- #GP
>>>>>                                         }
>>>>>
>>>>>                                         barrier();
>>>>>                                       } while (pc->lock != seq);
>>>>>
>>>>> Where the task is migrated from a P to an E core after reading the
>>>>> index but before doing RDPMC, turns fatal.
>>>>>
>>>>> When we use RSEQ, the migration will abort the sequence and the RDPMC
>>>>> will not be executed.
>>>>>
>>>>>
>>>>> Aand... while writing this I wondered why we don't simply fix up the
>>>>> fault in kernel space. Something like so...
>>>> If we silently drop the #GP for invalid rdpmc index, it would make the
>>>> kernel's behavior inconsistent with what SDM describes, "Specifying an
>>>> unsupported PMC encoding will cause a general protection exception #GP(0)."
>>>> It may break users' default assumption and cause some issues.
>>> It could, and users could catch the signal and deal with the issue
>>> themselves. That would be very messy and inherently racy. Since there
>>> are no known users of the exception can we add the cleaner fix for
>>> userspace?
>> The difficulty is that kernel has no way to know if the invalid rdpmc index
>> is intentionally set by user or not. Some test cases may intentionally set
>> an invalid index and validate if kernel returns a #GP.
> Why do we care about these tests? Can you provide an example of one?
> What do the tests hope to prove by causing the fault?

Currently KVM has some selftests to validate if the behavior of rpdmc
follows the SDM, like the guest_test_rdpmc() helper in
tools/testing/selftests/kvm/x86/pmu_counters_test.c.

When rdpmc is in pass-through mode, the rdpmc instruction in guest would
directly execute on the hardware and could need to trigger #GP. Although
currently all guest code including the rdpmc instruction in KVM selftests
run in kernel space, we can't exclude the test cases are extended to user
space in the future.


>
>> I don't see there is a good way to fix or work around the issue from kernel
>> space. We could still have to fix the issue from user space. An way to fix
>> this issue (what AI suggests) could be to register an rseq critical section
>> around the index load and the rdpmc instruction, then if migration happens
>> in the critical section, then rdpmc index would be retrieved from the new cpu.
> Requiring rseq/librseq on top of rdpmc is kind of frustrating. Do we
> plan to add this to the rdpmc documentation in perf_event.h? We use
> affinities to avoid this in the perf tool, but calling a system call
> to set/restore affinities per RDPMC defeats the RDPMC purpose.

I agree that adding rseq protection for rdpmc is frustrating, especially
considering the complexity that rseq introduces (rseq protection needs to
be implemented with aseembly code). I'm not sure if adding rseq is the
preferable way to fix this issue. If it is, we must need to update the
rdpmc documentation in perf_event.h. CPU affinity is another way to work
around this issue, but it introduce the side effect on migration.


>
> Thanks,
> Ian
>
>>> Thanks,
>>> Ian
>>>
>>>>> ---
>>>>> diff --git a/arch/x86/kernel/traps.c b/arch/x86/kernel/traps.c
>>>>> index 9f88b8a78e50..90391526acf7 100644
>>>>> --- a/arch/x86/kernel/traps.c
>>>>> +++ b/arch/x86/kernel/traps.c
>>>>> @@ -677,6 +677,43 @@ static enum kernel_gp_hint get_kernel_gp_address(struct pt_regs *regs,
>>>>>
>>>>>  #define GPFSTR "general protection fault"
>>>>>
>>>>> +static bool fixup_rdpmc_exception(struct pt_regs *regs)
>>>>> +{
>>>>> +     struct mm_struct *mm = current->mm;
>>>>> +     u8 buf[MAX_INSN_SIZE];
>>>>> +     struct insn insn;
>>>>> +     int len;
>>>>> +
>>>>> +     len = insn_fetch_from_user(regs, buf);
>>>>> +     if (len <= 0)
>>>>> +             return false;
>>>>> +
>>>>> +     if (!insn_decode_from_regs(&insn, regs, buf, len))
>>>>> +             return false;
>>>>> +
>>>>> +     /* RDPMC */
>>>>> +     if (insn.opcode.bytes[0] != 0x0f || insn.opcode.bytes[1] != 0x33)
>>>>> +             return false;
>>>>> +
>>>>> +     if (!atomic_read(&mm->context.perf_rdpmc_allowed))
>>>>> +             return false;
>>>>> +
>>>>> +     /*
>>>>> +      * So userspace RDPMC is allowed and took #GP.
>>>>> +      *
>>>>> +      * This means they got the index wrong. But per the ABI described in
>>>>> +      * struct perf_event_mmap_page; this means they'll also fail the
>>>>> +      * sequence lock and will retry the operation after re-reading the
>>>>> +      * index.
>>>>> +      *
>>>>> +      * Fake out the RDPMC by returning all zeros and continue.
>>>>> +      */
>>>>> +     regs->ax = 0;
>>>>> +     regs->dx = 0;
>>>>> +     regs->ip += insn.length;
>>>>> +     return true;
>>>>> +}
>>>>> +
>>>>>  static bool fixup_iopl_exception(struct pt_regs *regs)
>>>>>  {
>>>>>       struct thread_struct *t = &current->thread;
>>>>> @@ -812,6 +849,9 @@ DEFINE_IDTENTRY_ERRORCODE(exc_general_protection)
>>>>>       }
>>>>>
>>>>>       if (user_mode(regs)) {
>>>>> +             if (fixup_rdpmc_exception(regs))
>>>>> +                     goto exit;
>>>>> +
>>>>>               if (fixup_iopl_exception(regs))
>>>>>                       goto exit;
>>>>>

      reply	other threads:[~2026-12-01 10:19 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-04 21:20 perf_event: avoiding gpf on rdpmc for top-down-events on hybrid Vince Weaver
2024-12-05  0:57 ` Ian Rogers
2025-06-15 23:23   ` Ian Rogers
2025-06-16 19:34     ` Vince Weaver
2025-06-17  4:36       ` Ian Rogers
2025-06-17 16:37         ` Ian Rogers
2025-06-17 18:17           ` Peter Zijlstra
2025-06-17 20:36             ` Ian Rogers
2025-06-18  8:45               ` Peter Zijlstra
2025-06-18 11:55                 ` Peter Zijlstra
2025-06-18 13:57                 ` Vince Weaver
2026-11-05  6:17                 ` Mi, Dapeng
2026-11-30 18:39                   ` Ian Rogers
2026-12-01  5:22                     ` Mi, Dapeng
2026-12-01  6:38                       ` Ian Rogers
2026-12-01 10:19                         ` Mi, Dapeng [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=92339199-2330-4898-a8ef-d6416ef89e53@linux.intel.com \
    --to=dapeng1.mi@linux.intel.com \
    --cc=eranian@google.com \
    --cc=irogers@google.com \
    --cc=kan.liang@linux.intel.com \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=peterz@infradead.org \
    --cc=vincent.weaver@maine.edu \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox