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=1796085981; b=jxna7DgcfQuZy9mMbBBvjR6CUr3XUK+ksPoYxJB+ol903nDCiLzerDu7wm5saW+KR1JC WQDXBzbvNeYLKIq2B51hHQGyfHkll85WILt74cFvn5mbEYHdNn0oVhXndup6iwTTPIG3w fkKqISQxYyIRKQFX3R/5Pp9oEB9kTxBF0nDiCChHxftNw5A1OPBjXSpEoNRRZA67r7BOd rfQ64Ux+bzZnB+kV1BacpFrbdt6o756w3Mnqs6aM0/RafGvissHtwQz71Ljoa/YD1e5h2 d8uSAaRZ/lDY3h8K/9j3uI44jcf9SlYvKUElA3/DW97UhdvzzkgPqONLHFoOwXH7rVg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796085981; 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:From:To:Cc:Subject: Date:Message-Id:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=8IHn/9Hdk3ZjU6oKYcm9edy3POiTOVeO64Bt+txQ5fQ=; b=2tBFy+tnglDGwMpCGUPoH/gPcx9G+5DLXAv9/jeTPL6PuTHOAsmNYObkEuKvROIER8SA DgdDEbbRukONLJ6FHfUQ5u7CWHlOD0rI8MS8gvYyBNfW1tEMl1e6JRwhfXD0Q6boCuHr8 b7qEdS21iNsZBBrd2WpYqdktVZpHaV9jx6piZ/ZBlPLNaRUf4GKgyqs1XCR3IMtggo8pa Th7mFvsAa4MFCuKDSRxPoINNLJeXK8woHJ38VGXY74xR8KV0JbLr636fhnMMdjjsav0Zr udbN000WqBFkFPH7cYwQtb1fayp6EnhrzxkMNy+krHmKRXSP6DDbkpBTReS7TkNv63g== 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=IWbez+8J; 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 2A9DE606A320; Tue, 1 Dec 2026 00:46:20 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2A9DE606A320 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=IWbez+8J DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796085982; x=1827621982; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=8IHn/9Hdk3ZjU6oKYcm9edy3POiTOVeO64Bt+txQ5fQ=; b=IWbez+8J2hnrTRIY8NLSlyP0q5HXQDy4lQWN5DoYGI4H01E4SJqY1gzB rSD0WuTw09cvXtjB5mm1aqU7z70IeZoE0vd/toB3vx4/6NT/sraEe/oKa giBsS/CYboX0Ho91RLclP00QZ3rHSSTmWUWa7tKfJofd1xRpmF+lUuQXt 20d/jl1AH5RAmCoYBzhiNynxtCtiq1WxZhqvt0mv+Pv+eVm4xQF3Ncirx rXCeuUgjQ5w1vNDehKgd+9stC0XrWCqsGS1DzSdGh8Qd+/e18RJTqnREy tij/7jsI+NkkITsKHXDJfET5ODJI7U1PNET/l9wdoGpc3Gv3ELY5G6JJi g==; X-CSE-ConnectionGUID: kHnPkcgRR5GRotF2bLwSfQ== X-CSE-MsgGUID: 9HvPJw/nRD6TxLC8KXLoSw== X-IronPort-AV: E=McAfee;i="6800,10657,11982"; a="4846021" X-IronPort-AV: E=Sophos;i="6.27,243,1787036400"; d="scan'208";a="4846021" Received: from fmviesa010.fm.intel.com ([10.60.135.150]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Nov 2026 16:46:21 -0800 X-CSE-ConnectionGUID: ldprJTZwQaCoxSD4Me+bJw== X-CSE-MsgGUID: Mfi2W8bCSOui5i1e1tuPjw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,243,1787036400"; d="scan'208";a="13678784" Received: from spr.sh.intel.com ([10.112.229.196]) by fmviesa010.fm.intel.com with ESMTP; 30 Nov 2026 16:46:14 -0800 From: Dapeng Mi To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Ian Rogers , Adrian Hunter , Alexander Shishkin , Andi Kleen , Eranian Stephane Cc: linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Dapeng Mi , Zide Chen , Falcon Thomas , Xudong Hao , Dapeng Mi , Sashiko Subject: [Patch v2 10/16] perf/x86/intel: Don't inject fake PEBS overflow for non-guest PEBS event Date: Tue, 1 Dec 2026 08:37:02 +0800 Message-Id: <20261201003708.1806748-11-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261201003708.1806748-1-dapeng1.mi@linux.intel.com> References: <20261201003708.1806748-1-dapeng1.mi@linux.intel.com> 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 Content-Transfer-Encoding: 8bit The x86_pmu_handle_guest_pebs() function injects a fake overflow event to notify KVM of guest PEBS overflows. However, the existing check to determine if an event belongs to a KVM guest PEBS counter is insufficient. If a host PEBS event with 'exclude_guest == 0' is active when a PMI arrives while perf_guest_state() is active, it can be mistaken for a guest PEBS event. If such a host event samples extended or SIMD registers with PERF_SAMPLE_REGS_USER, perf_prepare_sample() falls back to perf_get_regs_user(). This points data->regs_user.regs at the per-CPU nmi_user_regs, which is a bare 'struct pt_regs' and not embedded inside a 'struct x86_perf_regs'. Subsequent calls to perf_reg_value() and perf_simd_reg_value() apply container_of() to it, reading past the end of nmi_user_regs and resulting in an out-of-bounds memory access. Enhance the guest PEBS check by ensuring that the fake overflow injection is skipped if the event's overflow handler is the default handler, or if the event samples registers (via PERF_SAMPLE_REGS_USER or PERF_SAMPLE_REGS_INTR). A genuine guest PEBS event uses a custom overflow handler and never configures these register sample types. Additionally, refine the logic for retrieving the guest PEBS indices and introduce a NULL pointer check prior to accessing the guest PEBS event. This excludes non-PEBS indices (such as PEBS_OUTPUT_PT) and guarantees that only valid guest PEBS events are iterated. Fixes: edd9aec51213 ("perf/x86: Enable XMM register sampling for REGS_USER case") Reported-by: Sashiko Closes: https://sashiko.dev/#/bug/linux-4c1692d7-7f30-48a3-a8d6-481928159d11 Signed-off-by: Dapeng Mi --- arch/x86/events/intel/core.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c index ee7ace7be3c6..3762f4a7ca2f 100644 --- a/arch/x86/events/intel/core.c +++ b/arch/x86/events/intel/core.c @@ -3788,20 +3788,40 @@ static int x86_pmu_handle_guest_pebs(struct pt_regs *regs, struct perf_sample_data *data) { struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); - u64 guest_pebs_idxs = cpuc->pebs_enabled & ~cpuc->intel_ctrl_exclude_guest_mask; struct perf_event *event = NULL; + u64 pebs_idxs, guest_pebs_idxs; int bit; if (!unlikely(perf_guest_state())) return 0; + pebs_idxs = hybrid(cpuc->pmu, intel_ctrl) & + cpuc->pebs_enabled & x86_pmu.pebs_capable; + guest_pebs_idxs = pebs_idxs & + ~cpuc->intel_ctrl_exclude_guest_mask & + cpuc->intel_ctrl_exclude_host_mask; + if (!x86_pmu.pebs_ept || !x86_pmu.pebs_active || !guest_pebs_idxs) return 0; for_each_set_bit(bit, (unsigned long *)&guest_pebs_idxs, X86_PMC_IDX_MAX) { event = cpuc->events[bit]; - if (!event->attr.precise_ip) + if (!event || !event->attr.precise_ip) + continue; + + if (is_default_overflow_handler(event)) + continue; + + /* + * Guest PEBS events never sample registers. However, a + * host PEBS event may be mistaken for a guest PEBS event + * here if its PMI arrives while perf_guest_state() is set. + * + * Skip the event if it samples registers. + */ + if (event->attr.sample_type & + (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_REGS_INTR)) continue; perf_sample_data_init(data, 0, event->hw.last_period); -- 2.34.1