From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=198.175.65.9 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796120358; b=Mv3IKVaAU4GlQxdz9IgJ7Iungozbmv/rvJrYwl6CsAvsxaVGdwfy18D35wk3vR1sS16T jV/jahHBzt+V9ZvaZApnAVjfzYjs8nf68UbFIlUb9s6Q63/+jIFum7uGGc7inkX3A80Q4 0z8mQRck48GcTntdMygp/2R+hd4h6aGhYw+ygjdlqmmGN6aGGTZb1gmQSbXW4eF+m4owS DfScbuenAiqO+UjTZNv5i2IawNIC8eLZFai+nnI4KqQbTZKXUpv2p60J6MloqZ7YC2hf3 MIt1rGqAgcX7Dpk/EObKcP/2Bdhhm/7Gc2UcuSw3y6am7+807Lr3l6egq9WVAM+BZng== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796120358; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:X-IronPort-AV:Received:X-CSE-ConnectionGUID: X-CSE-MsgGUID:X-ExtLoop1:X-IronPort-AV:Received:Message-ID:Date: MIME-Version:User-Agent:Subject:To:Cc:References:Content-Language: From:In-Reply-To:Content-Type:Content-Transfer-Encoding; bh=IW8vHCCwUWnrLPOx8udCunkPzeSY5dwiTa2qnkKMJlY=; b=LhYb0TEYZq+5R5I/jOK+etTnXncduGZ0fzEaCrBNefevnAxRRt0B2+mGHrhIkmPIWrtw AmQkNHVfh8Yi/X3Z+1Uq0YPe9LOSgVtD82Ed5wfOspShP0LBNCjyQKsqDIA0FjVndjrPK apbBgRJ0fv1/cf9gpkPJyDo/+XBWJIfA3kKv8j+jFaAMUANsDc96MghPb+jraH1tYDTAQ 4xBnlq0zicu34Hl/TBWQOQSrYV2PrkQfJn3zKQbKILwr/BSVH+3OQPnjSQttnmUugaQzp XvxewjlmK1J0aIV8ShxRzZWpVVzLzZ8OX7AEsyFNbkDNqm3/bRBewF8OhVp+fs4dNcQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=DEw076O+; arc=none smtp.remote-ip=198.175.65.9 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 80351606A31A for ; Tue, 1 Dec 2026 10:19:17 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 80351606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=DEw076O+ DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796120357; x=1827656357; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=IW8vHCCwUWnrLPOx8udCunkPzeSY5dwiTa2qnkKMJlY=; b=DEw076O+z2LjdxL9Y2CXI8Ksto0aJpBtKhYd0RymXURJD0NciF0G04Fv xoFlKEaH5eN7GMmv8yT4BZY0PGI7RtiapWwAimHBeI7ZYVSWxIKsC8zE3 22LwE7is30BpajX2Z/szmO7PUxN5F7+sHyQG+n6NbA9U7FM30AjpRWF4b BVIdRekHsxtckjQbf3MkhE8SrfgLWpBQDFMD4dxjSrRo+aisWwUt0aIvD hZ0lD+7qIgnNj+DFBF45iKVqR07iS58ObeuZ04Vvf39vZ5Enqh0fi6kE+ 7Vv46s0Cwet+Qjo5IKdiWZj9yf6B3e4nTW87lrhSMR8DIOizGiJpmRtUP A==; X-CSE-ConnectionGUID: 6kYvryAOS2KvA7b+mAb+lw== X-CSE-MsgGUID: v6FBZXYdSvGHPUT9MtgdWg== X-IronPort-AV: E=McAfee;i="6800,10657,11982"; a="4896040" X-IronPort-AV: E=Sophos;i="6.27,244,1787036400"; d="scan'208";a="4896040" Received: from fmviesa010.fm.intel.com ([10.60.135.150]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Dec 2026 02:19:17 -0800 X-CSE-ConnectionGUID: sR39gSkKRau1i1vBUIyNnQ== X-CSE-MsgGUID: yL4TCq/ZSF2D4f07ql/ikw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,244,1787036400"; d="scan'208";a="13777990" Received: from dapengmi-mobl1.ccr.corp.intel.com (HELO [10.124.241.239]) ([10.124.241.239]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Dec 2026 02:19:14 -0800 Message-ID: <92339199-2330-4898-a8ef-d6416ef89e53@linux.intel.com> Date: Tue, 1 Dec 2026 18:19:09 +0800 X-Mailing-List: linux-perf-users@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: perf_event: avoiding gpf on rdpmc for top-down-events on hybrid To: Ian Rogers Cc: Peter Zijlstra , x86@kernel.org, Vince Weaver , "Liang, Kan" , linux-perf-users@vger.kernel.org, Stephane Eranian References: <2c759cfb-41b2-6f50-e893-1545a965d106@maine.edu> <20250617181738.GD1613376@noisy.programming.kicks-ass.net> <20250618084522.GE1613376@noisy.programming.kicks-ass.net> <569b3882-5862-4f6f-96b7-302910e2cf4e@linux.intel.com> <756915ef-0bf3-4142-8add-cc89d0b4c9b7@linux.intel.com> Content-Language: en-US From: "Mi, Dapeng" In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On 12/1/2026 2:38 PM, Ian Rogers wrote: > On Mon, Nov 30, 2026 at 9:22 PM Mi, Dapeng wrote: >> >> On 12/1/2026 2:39 AM, Ian Rogers wrote: >>> On Wed, Nov 4, 2026 at 10:17 PM Mi, Dapeng 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; >>>>> >>>>> >>>>> >>>>> 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 = ¤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; >>>>>