From: Peter Zijlstra <peterz@infradead.org>
To: Ian Rogers <irogers@google.com>, x86@kernel.org
Cc: Vince Weaver <vincent.weaver@maine.edu>,
"Liang, Kan" <kan.liang@linux.intel.com>,
linux-perf-users@vger.kernel.org
Subject: Re: perf_event: avoiding gpf on rdpmc for top-down-events on hybrid
Date: Wed, 18 Jun 2025 13:55:03 +0200 [thread overview]
Message-ID: <20250618115503.GF1613633@noisy.programming.kicks-ass.net> (raw)
In-Reply-To: <20250618084522.GE1613376@noisy.programming.kicks-ass.net>
On Wed, Jun 18, 2025 at 10:45:22AM +0200, Peter Zijlstra wrote:
> 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;
And alternative might be to return ~0UL in both registers. All 32bit
chips have limited counter width and this will set the high bits. All
64bit chips do no expect the high words of the registers to be set.
That way one could recognise the fail case; if that was so desired.
> + regs->ip += insn.length;
> + return true;
> +}
> +
> static bool fixup_iopl_exception(struct pt_regs *regs)
> {
> struct thread_struct *t = ¤t->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;
>
next prev parent reply other threads:[~2025-06-18 11:55 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 [this message]
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
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=20250618115503.GF1613633@noisy.programming.kicks-ass.net \
--to=peterz@infradead.org \
--cc=irogers@google.com \
--cc=kan.liang@linux.intel.com \
--cc=linux-perf-users@vger.kernel.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.