Re: [Patch v2 7/7] perf/x86/intel: Add support for rdpmc user disable feature

From: Mi, Dapeng

Date: Mon Jan 12 2026 - 21:29:53 EST



On 1/12/2026 6:57 PM, Peter Zijlstra wrote:
> On Mon, Jan 12, 2026 at 01:16:49PM +0800, Dapeng Mi wrote:
>
>> +static void intel_pmu_update_rdpmc_user_disable(struct perf_event *event)
>> +{
>> + /*
>> + * Counter scope's user-space rdpmc is disabled by default
>> + * except two cases.
>> + * a. rdpmc = 2 (user space rdpmc enabled unconditionally)
>> + * b. rdpmc = 1 and the event is not a system-wide event.
>> + * The count of non-system-wide events would be cleared when
>> + * context switches, so no count data is leaked.
>> + */
>> + if (x86_pmu_has_rdpmc_user_disable(event->pmu)) {
>> + if (x86_pmu.attr_rdpmc == X86_USER_RDPMC_ALWAYS_ENABLE ||
>> + (x86_pmu.attr_rdpmc == X86_USER_RDPMC_CONDITIONAL_ENABLE &&
>> + event->ctx->task))
>> + event->hw.config &= ~ARCH_PERFMON_EVENTSEL_RDPMC_USER_DISABLE;
>> + else
>> + event->hw.config |= ARCH_PERFMON_EVENTSEL_RDPMC_USER_DISABLE;
>> + }
>> +}
> Is it not simpler to invert that condition?
>
> if (x86_pmu.attr_rdpmc == X86_USER_RDPMC_ALWAYS_NEVER ||
> !event->ctx->task)
> event->hw.config |= ARCH_PERFMON_EVENTSEL_RDPMC_USER_DISABLE;
> else
> event->hw.config &= ~ARCH_PERFMON_EVENTSEL_RDPMC_USER_DISABLE;

Hmm, it seems the logic changes along with the code change. The system-wide
event should be allowed to read with rdpmc from user space when attr_rdpmc
== X86_USER_RDPMC_ALWAYS_ENABLE, but it doesn't with the new code.

If we don't change the logic and just invert the condition, the code
simplicity has no much change.

if (x86_pmu.attr_rdpmc == X86_USER_RDPMC_ALWAYS_NEVER ||
(x86_pmu.attr_rdpmc == X86_USER_RDPMC_CONDITIONAL_ENABLE &&
!event->ctx->task))
event->hw.config |= ARCH_PERFMON_EVENTSEL_RDPMC_USER_DISABLE;
else
event->hw.config &= ~ARCH_PERFMON_EVENTSEL_RDPMC_USER_DISABLE;