From patchwork Tue Jul 30 15:53:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Roger_Pau_Monn=C3=A9?= X-Patchwork-Id: 13747555 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B6B38C3DA70 for ; Tue, 30 Jul 2024 15:53:33 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.767943.1178645 (Exim 4.92) (envelope-from ) id 1sYpA6-00068u-Ll; Tue, 30 Jul 2024 15:53:18 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 767943.1178645; Tue, 30 Jul 2024 15:53:18 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sYpA6-00068n-J5; Tue, 30 Jul 2024 15:53:18 +0000 Received: by outflank-mailman (input) for mailman id 767943; Tue, 30 Jul 2024 15:53:16 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sYpA4-00068f-U4 for xen-devel@lists.xenproject.org; Tue, 30 Jul 2024 15:53:16 +0000 Received: from mail-qk1-x731.google.com (mail-qk1-x731.google.com [2607:f8b0:4864:20::731]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id d45a0a4d-4e8b-11ef-8776-851b0ebba9a2; Tue, 30 Jul 2024 17:53:14 +0200 (CEST) Received: by mail-qk1-x731.google.com with SMTP id af79cd13be357-7a1dac7f0b7so294669085a.0 for ; Tue, 30 Jul 2024 08:53:14 -0700 (PDT) Received: from localhost ([213.195.124.163]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7a1d73f02c3sm650988685a.65.2024.07.30.08.53.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jul 2024 08:53:12 -0700 (PDT) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: d45a0a4d-4e8b-11ef-8776-851b0ebba9a2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1722354793; x=1722959593; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=x26zs6CcCUyr+tPboAGAeAUd3pUjBBOwMMI7r116+WM=; b=PoSjQwE/5MPbE4n+dB85GaoVk+xWdlRzDSCwFXGo30kQkghUTeFSyWHVKv8ZeYXFvs rf7N/scWGPhYi9D41PbFmmlYg1S7XGxJKbe6bh7NemL9qEQGG6F95hCX8FLL/R1n6Aob edDrPqS4xVZZBPOoBO+Da4O9PB/Kbi8EF9UGY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722354793; x=1722959593; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=x26zs6CcCUyr+tPboAGAeAUd3pUjBBOwMMI7r116+WM=; b=WVECc7Ofzy8c8Xkn8plDIpHAXfZlkYUhbmwZMXshgIuGtpc/xqYMTcKRLzKyhYoXOZ IG+TP50IOI9kBaFiZOz2p++wDXFc+CbeShlB0RulFEFUwkpeDCDxoZOYmhTZPNPZQLiJ 7/otAADVoQXFV9OZ0VyLBMCJHfdErDmmoXVlg8d7rw8CwJadBW4d3bWLYcVXQ17gs6k/ mZjUqTn2lDM46T4G4lv3xBsGAxn3AL+ywnQy4ysihB2SvWxzidSXh0lETZLFUUDLnOk1 zRJ2wSdukpYgASG+auJiF9OO+4DNsCDFrF/scPjDozQa8D3HKiZXn9ZQLGAS/TeT/C5k bBfw== X-Gm-Message-State: AOJu0YydD9jG6GnrmfXJeVeJnRXUf2SoQssRLgbGkfeCEc/nMFwi0Evy cRLPKMH2WYUeK4BeCkAC9A/bkfPKObnN96ZI6GBXfFCMjQIG39MpKCf0YOuZP2vLEHGutqCdxdx t X-Google-Smtp-Source: AGHT+IHubYDM1jWP1FzmnMgVKszWkQ06DP81wrJB5eS3p+W/05GelQVrROvze/bdNo0U5zN2GtHEdw== X-Received: by 2002:a05:620a:4721:b0:79f:1f6:ab9f with SMTP id af79cd13be357-7a1e525e6bamr1102989485a.29.1722354793040; Tue, 30 Jul 2024 08:53:13 -0700 (PDT) From: Roger Pau Monne To: xen-devel@lists.xenproject.org Cc: Roger Pau Monne , Jan Beulich , Andrew Cooper , Alejandro Vallejo Subject: [PATCH v3] x86/altcall: further refine clang workaround Date: Tue, 30 Jul 2024 17:53:05 +0200 Message-ID: <20240730155305.49172-1-roger.pau@citrix.com> X-Mailer: git-send-email 2.45.2 MIME-Version: 1.0 The current code in ALT_CALL_ARG() won't successfully workaround the clang code-generation issue if the arg parameter has a size that's not a power of 2. While there are no such sized parameters at the moment, improve the workaround to also be effective when such sizes are used. Instead of using a union with a long use an unsigned long that's first initialized to 0 and afterwards set to the argument value. Reported-by: Alejandro Vallejo Suggested-by: Alejandro Vallejo Signed-off-by: Roger Pau Monné Reviewed-by: Jan Beulich --- Changes since v2: - Fix indentation and style issues. - Adjust comment to match the new workaround. --- xen/arch/x86/include/asm/alternative.h | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/xen/arch/x86/include/asm/alternative.h b/xen/arch/x86/include/asm/alternative.h index e63b45927643..c5fa242e76b3 100644 --- a/xen/arch/x86/include/asm/alternative.h +++ b/xen/arch/x86/include/asm/alternative.h @@ -169,27 +169,25 @@ extern void alternative_branches(void); #ifdef CONFIG_CC_IS_CLANG /* - * Use a union with an unsigned long in order to prevent clang from - * skipping a possible truncation of the value. By using the union any - * truncation is carried before the call instruction, in turn covering - * for ABI-non-compliance in that the necessary clipping / extension of - * the value is supposed to be carried out in the callee. + * Clang doesn't follow the psABI and doesn't truncate parameter values at the + * callee. This can lead to bad code being generated when using alternative + * calls. * - * Note this behavior is not mandated by the standard, and hence could - * stop being a viable workaround, or worse, could cause a different set - * of code-generation issues in future clang versions. + * Workaround it by using a temporary intermediate variable that's zeroed + * before being assigned the parameter value, as that forces clang to zero the + * register at the caller. * * This has been reported upstream: * https://github.com/llvm/llvm-project/issues/12579 * https://github.com/llvm/llvm-project/issues/82598 */ #define ALT_CALL_ARG(arg, n) \ - register union { \ - typeof(arg) e[sizeof(long) / sizeof(arg)]; \ - unsigned long r; \ - } a ## n ## _ asm ( ALT_CALL_arg ## n ) = { \ - .e[0] = ({ BUILD_BUG_ON(sizeof(arg) > sizeof(void *)); (arg); })\ - } + register unsigned long a ## n ## _ asm ( ALT_CALL_arg ## n ) = ({ \ + unsigned long tmp = 0; \ + *(typeof(arg) *)&tmp = (arg); \ + BUILD_BUG_ON(sizeof(arg) > sizeof(unsigned long)); \ + tmp; \ + }) #else #define ALT_CALL_ARG(arg, n) \ register typeof(arg) a ## n ## _ asm ( ALT_CALL_arg ## n ) = \