From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=198.175.65.19 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796102542; b=BUQJvYSVW5Y0k2F1+Xl3YefIwyaxB8/Pi7MT9SuqRHAM1ITaPVAQAapZHpA4V8zm/3ZQ O8f3Ho2Fd47QzAcUsrK+cEu8VqlJs1w1vy6Rrs0nHQ9vEqtkMH+JP9+K6Ode1O35n7Nt+ A8JobCSMXygNODOL3QnxZIwGcuGWVRAXs7j04YdAJzidQ26HdiaCL0wpuxZ7oKNplq2Tk vZAMm33oBAyRpZ9sXw13dgthnjwHoCbgbzMdPAUE4aj6+7tksiaUllgP8WmxO0G1ZjsTX Lf72xxEPYLE4ByLD/l6zY4L2ZCC82t6FCJ7s65moi/reKaKWFXNcvWtCTu3Hgc1Satw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796102542; 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=XxDNlB203UavRtlcKdm6NjzM9i183tWyDMk6ebdl2iw=; b=d0rbVRDA8IadCxzns/+XpcAmg7sYZcQpc8MjsTuCGq5BWrBnvMjVeS/YOoUfoFq+q1Ds g5840vQyodRqAFX6fohtpME3jGM6T9EAsD+NMgNL3dtGBHwFneW+Rh/rZ4CxTsMK9Whh/ CC7qyalunyJoSU7XYLJvbEgQTiDRYb3z4n3wvf3IJzUUnu8AdyywxmpwYCHRU3bQF3ruj p8EkEPs/LgstOLxwC24vKmsbNUbfCRWwiR32Add99wt+eun3k5M+bQ6Pq5P4f7ST4+cj5 JJPkFU0d/mBjiAmIYYqNl62XxH9NcYbVOiY3s/qgOtfK6mfbsODxvQmZ4xVCq3elJag== 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=lSOuXvWD; arc=none smtp.remote-ip=198.175.65.19 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.19]) (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 D2CBD606A325 for ; Tue, 1 Dec 2026 05:22:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D2CBD606A325 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=lSOuXvWD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796102542; x=1827638542; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=XxDNlB203UavRtlcKdm6NjzM9i183tWyDMk6ebdl2iw=; b=lSOuXvWD7BUalBUS/0ri8e1C0dg2JxFcCCyXGm82p/6Om70aF59I8Hfg 98/v4Yvbo6SVftx/AnrWrSpkgHKjEzCtaBF4Ipwd7j9N+9LfR64NrrKvP q2YrZnhkRzsVdZ3i0c5ULxI8QZwX5L0FD4Z3MH4/tSdcYoPmUKApUrWMk cFH4BHELxUwr8wIN9WPDc11f+k2LmFg9QRErgCIA9e4smc9Ydz5wYH59p bmNrJBodh19lKlWbncL0JINh8wYvNE73r0Adr3ksYDD5oYKfrKuXATt/2 aODA5TV1yYUT9WjmhxdGh4rsfUuEg+5KgM5wjn9Sifa5OZqUExpr8U/l6 A==; X-CSE-ConnectionGUID: YOUc9m2FSLqr6CB18qz6Ww== X-CSE-MsgGUID: ZXYPzV1ySLSyllbR0x8TRg== X-IronPort-AV: E=McAfee;i="6800,10657,11982"; a="5027912" X-IronPort-AV: E=Sophos;i="6.27,244,1787036400"; d="scan'208";a="5027912" Received: from fmviesa007.fm.intel.com ([10.60.135.147]) by orvoesa111.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Nov 2026 21:22:22 -0800 X-CSE-ConnectionGUID: KAfPeqHOQj+0uAwhYyqcXg== X-CSE-MsgGUID: /Dgdt7b6TImKMhu01t2SXQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,244,1787036400"; d="scan'208";a="13447790" Received: from unknown (HELO [10.238.2.182]) ([10.238.2.182]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Nov 2026 21:22:19 -0800 Message-ID: <756915ef-0bf3-4142-8add-cc89d0b4c9b7@linux.intel.com> Date: Tue, 1 Dec 2026 13:22:17 +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> 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: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. 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. > > 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; >>>