Received: by 2002:ab2:7855:0:b0:1f9:5764:f03e with SMTP id m21csp963154lqp; Thu, 23 May 2024 05:34:19 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVhqrex0Tq8JUPVFKHmm5YZt5LzComTljdU8xxiw1dFNvcncpZa08SAv31aRlzlxF3DO80eLMPVQKRtV3zN6HwqFOYqW3pCVTcaQp6xMA== X-Google-Smtp-Source: AGHT+IGAa1FeSzg0ugRSiSmSverru/x6ZdajipQqXqPRnc/B6hSFsvQrx9fuEIbIKcWbzoucGA3c X-Received: by 2002:a17:906:1d09:b0:a59:a05c:b559 with SMTP id a640c23a62f3a-a62281c7ea5mr281427966b.70.1716467659246; Thu, 23 May 2024 05:34:19 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716467659; cv=pass; d=google.com; s=arc-20160816; b=AsR7Tn5jIMCJQLv4Nrz9qk7S5BaGBZjio9kvLbryfijt3Xq1O8a9RYExnvoa/p7I4z loor5tqOAHQe7W3B5qpz555UvkFAoJr7gBjRNEzXsdendDwFIjqeElQFiuzwDYe4+h69 ilQizJMdgs8qxUQvb+n1oaQnwWBaK0XtmMa1S/9i2ZA03bUtEJU4O8VwyM6Y1PgZ6GQg hSbL8qjXwLExs4rzC7l3pGSvCeBEHYxcAYR5JfjtPvwo3CiNLEWnd3fFeH+xlxJT8TrJ raAAp8XnWk+UtfKuc0mRSif+LJ0Wd/U/m0GvRyvy8hfqcFEoEd+XJNpn3/xiYAxdoabL ABgA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=TG9RpsRGFzzFo4NixL4rHOJgwqr4kHslcW8q4P+LGVM=; fh=B+pj/7Zlt6omm0ZsgnD4tfyfvcaJjCyGKUn2xGstYtI=; b=KT2feOlaesLL6oivo64XJ55vQEVgCIh/xzq5rIcMbj+3wZA4OyklqrGactD770PN07 HGnEmSmixHBy2SDvcv0X/QlOohABdXzaBLKIAYl2Lmtlb2XiD2Uc916rwCRKBXRbQKg2 mixIGpL3cTKPs8FBOr/bL9XeDG9aV1HF/21BrQEQxYTKTGWwMhyegvhMLzU3s2Nl5Enq O+GAwqpU30mwuc4nqdNfX7oUyXDhwx0p0Zfgny3KiSRK5TFl6b49BMfa33WBKxKLJ5JT scIRPbmvzVOCzRDNNw5QlSWkaI5xx8l1ndDF/mmExgGP0b1IJDYiMFvAmGZzI4wCQZfH i3vQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2023-11-20 header.b="Qt/z69XK"; arc=pass (i=1 spf=pass spfdomain=oracle.com dkim=pass dkdomain=oracle.com dmarc=pass fromdomain=oracle.com); spf=pass (google.com: domain of linux-kernel+bounces-187476-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-187476-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=oracle.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id a640c23a62f3a-a5ce1d071c3si845155966b.949.2024.05.23.05.34.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 May 2024 05:34:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-187476-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2023-11-20 header.b="Qt/z69XK"; arc=pass (i=1 spf=pass spfdomain=oracle.com dkim=pass dkdomain=oracle.com dmarc=pass fromdomain=oracle.com); spf=pass (google.com: domain of linux-kernel+bounces-187476-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-187476-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=oracle.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id C86CF1F227B1 for ; Thu, 23 May 2024 12:34:18 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BE0291E4A0; Thu, 23 May 2024 12:33:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="Qt/z69XK" Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 20FB7148824; Thu, 23 May 2024 12:33:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.177.32 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716467635; cv=none; b=d4h+cgqlyrfjSm+m0ezkbBM8r7ywH4gLr4U6BD7prrDtTUfI/M2NajJoOkVpWnDejcF/JlMP39uKJlcb0PSUs+lMRdCggGRM4Qg1eyP8TlPkL1Zh09OMNvJtfmXm2b8EX1CuxLktWNUdZVSxC3NhMuvTFEQ6gQVGeKY7+2o9//M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716467635; c=relaxed/simple; bh=smrF2fUmUHA7xyyomoTkTTinVqiDpVSE9fy64YqQQIw=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=ADwMS0kk6/gkUQEPEWE0neV/58Ja9NsmFd9+FeeJDK/wbVYx8sJXWimIS5GlA5LrkOiP2YQ51GSZkicx24f8H+8jDZeYdYBhFE3qkAnEq0qj7bnNzTfzsKVqB3z17PdyaOePoOmDLKkHaWsppxzWEk1YbeCHlkFoE8NgcUbOT5o= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=oracle.com; spf=pass smtp.mailfrom=oracle.com; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b=Qt/z69XK; arc=none smtp.client-ip=205.220.177.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 44N4xBDA032362; Thu, 23 May 2024 12:33:27 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : mime-version : content-transfer-encoding; s=corp-2023-11-20; bh=TG9RpsRGFzzFo4NixL4rHOJgwqr4kHslcW8q4P+LGVM=; b=Qt/z69XKEHsvrxyNZ+5fmHvUp118mNkGvV5fLuAKclnXE3MxnaMOg+gkfZFos4YJtAoU hA98aC0FtfmRYMvRgADAw8zbRUPEz+X+nX5mxyis5SoEKGHaNdyAt/9gftEouPgHCWM1 RCob7mwm8HQrurZqD7/8TXBqHLk1yLtqWhDzFDZp3StI1BEUCDWD3ScHe65nTH/NEt+5 8jSbI3/OkDDDxGWrg7LpoO8d3koMxMx+Gf2A9ftHwzGMQfAIOUDEgcqX+8jw2p9PA2jr oC6rhj5uvxTyHZi7bL6Zo5e5pCBWI9hG2rnhOeLC81Bj3Gphm8A6GQmExN5gpDqs76IY Kw== Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3y6kxva1g8-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 23 May 2024 12:33:26 +0000 Received: from pps.filterd (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 44NCC6o5019515; Thu, 23 May 2024 12:33:26 GMT Received: from pps.reinject (localhost [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3y6jsahjg1-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 23 May 2024 12:33:26 +0000 Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 44NCXP6F016719; Thu, 23 May 2024 12:33:25 GMT Received: from laptop-dell-latitude7430.nl.oracle.com (dhcp-10-175-24-91.vpn.oracle.com [10.175.24.91]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTP id 3y6jsahje8-1; Thu, 23 May 2024 12:33:25 +0000 From: Alexandre Chartre To: x86@kernel.org, kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org, daniel.sneddon@linux.intel.com, pawan.kumar.gupta@linux.intel.com, tglx@linutronix.de, konrad.wilk@oracle.com, peterz@infradead.org, gregkh@linuxfoundation.org, seanjc@google.com, andrew.cooper3@citrix.com, dave.hansen@linux.intel.com, nik.borisov@suse.com, kpsingh@kernel.org, longman@redhat.com, bp@alien8.de, pbonzini@redhat.com, alexandre.chartre@oracle.com Subject: [PATCH] x86/bhi: BHI mitigation can trigger warning in #DB handler Date: Thu, 23 May 2024 14:33:22 +0200 Message-Id: <20240523123322.3326690-1-alexandre.chartre@oracle.com> X-Mailer: git-send-email 2.39.3 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.650,FMLib:17.12.28.16 definitions=2024-05-23_07,2024-05-23_01,2024-05-17_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 mlxscore=0 spamscore=0 adultscore=0 mlxlogscore=999 malwarescore=0 suspectscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2405010000 definitions=main-2405230086 X-Proofpoint-GUID: dVpr_7H5SXUK8Sw610ASeh_7ItQhnjVO X-Proofpoint-ORIG-GUID: dVpr_7H5SXUK8Sw610ASeh_7ItQhnjVO When BHI mitigation is enabled, if sysenter is invoked with the TF flag set then entry_SYSENTER_compat uses CLEAR_BRANCH_HISTORY and calls the clear_bhb_loop() before the TF flag is cleared. This causes the #DB handler (exc_debug_kernel) to issue a warning because single-step is used outside the entry_SYSENTER_compat function. To address this issue, entry_SYSENTER_compat() should use CLEAR_BRANCH_HISTORY after making sure flag the TF flag is cleared. The problem can be reproduced with the following sequence: $ cat sysenter_step.c int main() { asm("pushf; pop %ax; bts $8,%ax; push %ax; popf; sysenter"); } $ gcc -o sysenter_step sysenter_step.c $ ./sysenter_step Segmentation fault (core dumped) The program is expected to crash, and the #DB handler will issue a warning. Kernel log: WARNING: CPU: 27 PID: 7000 at arch/x86/kernel/traps.c:1009 exc_debug_kernel+0xd2/0x160 ... RIP: 0010:exc_debug_kernel+0xd2/0x160 ... Call Trace: <#DB> ? show_regs+0x68/0x80 ? __warn+0x8c/0x140 ? exc_debug_kernel+0xd2/0x160 ? report_bug+0x175/0x1a0 ? handle_bug+0x44/0x90 ? exc_invalid_op+0x1c/0x70 ? asm_exc_invalid_op+0x1f/0x30 ? exc_debug_kernel+0xd2/0x160 exc_debug+0x43/0x50 asm_exc_debug+0x1e/0x40 RIP: 0010:clear_bhb_loop+0x0/0xb0 ... ? entry_SYSENTER_compat_after_hwframe+0x6e/0x8d Fixes: 7390db8aea0d ("x86/bhi: Add support for clearing branch history at syscall entry") Reported-by: Suman Maity Signed-off-by: Alexandre Chartre --- arch/x86/entry/entry_64_compat.S | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/arch/x86/entry/entry_64_compat.S b/arch/x86/entry/entry_64_compat.S index 11c9b8efdc4c..7fa04edc87e9 100644 --- a/arch/x86/entry/entry_64_compat.S +++ b/arch/x86/entry/entry_64_compat.S @@ -91,7 +91,6 @@ SYM_INNER_LABEL(entry_SYSENTER_compat_after_hwframe, SYM_L_GLOBAL) IBRS_ENTER UNTRAIN_RET - CLEAR_BRANCH_HISTORY /* * SYSENTER doesn't filter flags, so we need to clear NT and AC @@ -116,6 +115,12 @@ SYM_INNER_LABEL(entry_SYSENTER_compat_after_hwframe, SYM_L_GLOBAL) jnz .Lsysenter_fix_flags .Lsysenter_flags_fixed: + /* + * CLEAR_BRANCH_HISTORY can call other functions. It should be invoked + * after making sure TF is cleared because single-step is ignored only + * for instructions inside the entry_SYSENTER_compat function. + */ + CLEAR_BRANCH_HISTORY movq %rsp, %rdi call do_SYSENTER_32 jmp sysret32_from_system_call -- 2.39.3