From patchwork Thu Feb 24 13:24:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 12758581 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 12EE2C433EF for ; Thu, 24 Feb 2022 13:25:17 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.278291.475481 (Exim 4.92) (envelope-from ) id 1nNE7G-0007rR-2Z; Thu, 24 Feb 2022 13:25:06 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 278291.475481; Thu, 24 Feb 2022 13:25:06 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nNE7F-0007rK-VX; Thu, 24 Feb 2022 13:25:05 +0000 Received: by outflank-mailman (input) for mailman id 278291; Thu, 24 Feb 2022 13:25:04 +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 1nNE7D-0007q3-UC for xen-devel@lists.xenproject.org; Thu, 24 Feb 2022 13:25:04 +0000 Received: from de-smtp-delivery-102.mimecast.com (de-smtp-delivery-102.mimecast.com [194.104.109.102]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 2c3d5eaa-9575-11ec-8539-5f4723681683; Thu, 24 Feb 2022 14:25:03 +0100 (CET) Received: from EUR01-VE1-obe.outbound.protection.outlook.com (mail-ve1eur01lp2053.outbound.protection.outlook.com [104.47.1.53]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id de-mta-7-gj4dPI3HOky1O1Tkms3XBQ-1; Thu, 24 Feb 2022 14:25:01 +0100 Received: from VI1PR04MB5600.eurprd04.prod.outlook.com (2603:10a6:803:e7::16) by AM0PR04MB5971.eurprd04.prod.outlook.com (2603:10a6:208:114::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5017.23; Thu, 24 Feb 2022 13:25:00 +0000 Received: from VI1PR04MB5600.eurprd04.prod.outlook.com ([fe80::d479:b728:345c:bd65]) by VI1PR04MB5600.eurprd04.prod.outlook.com ([fe80::d479:b728:345c:bd65%6]) with mapi id 15.20.5017.024; Thu, 24 Feb 2022 13:25:00 +0000 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: 2c3d5eaa-9575-11ec-8539-5f4723681683 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=mimecast20200619; t=1645709102; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=hJj7Nd1zQ5C3uR5AotGLWuOQOjAHBSXWP97Y2JLCbGE=; b=aAQfIFptKYIKqGc9PWkCFUJrATvk1gnJ29+7deT9cIYDakXvANus0pRgKTg8iOs4UhysZ1 EsMfpYtsIIM/BG6P9hdxvX/Zx0mIHcTE468rZ5I7D4ZudFs/E01nuKNcwaVgz8x72v1pa0 Gs0RWLX1C60bOkYFQm5bVJoLJs3HQWg= X-MC-Unique: gj4dPI3HOky1O1Tkms3XBQ-1 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BCOgu/EhVO5gB8Z0g8wfEuDIikrY2SN+Yt3tfCwfxziXFKa2RAeeLHoj2FtnDKkvp7mCc0JkcCGNIUMA62PwARkoq8Pc8XzOEf9nyCgLLVBun7EcgFPgirvtqUC3VvSnQyTmoCMQa/10lOkDAW1UZ3O2Uw1QnrE3K3nD+s2OEXI2AiJzDo/VN3Hw3jf62ZBLM0y+duU+Hm1nI4KMAlZhuLwRoFD1ahKkm3DhrYSZagUsZdi8eY50VDQoK2cKcxjLSu4xyRvivbgw1Tj5XovD9AyelMNOKR0sBKRDvK2A/e5YDHCBwrb8Ik/kiDuDsM/xRgSUf0GXnqdC8QxRDPIh2A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=hJj7Nd1zQ5C3uR5AotGLWuOQOjAHBSXWP97Y2JLCbGE=; b=BadaGglrufTEw1s7lHHbwtwPce1gtObUFokR8nAC/P1du50v3FsuN+RfjtjfhyoLRJH7BSXcMYQ/QZxpgmoChXa/S80omn9GQ9PU4QBX/NdFr9JtQ3b7ubyxsEEzzFu1dVtUopd7N8FzFjXGtRQ3vXOc6eF4NSOdYk6oVQBGgkgIo1vxsiYlDQ8nH9OBUVLIBtX7albbmhA/NNkhcyeB+xjxgadDeqk0Ft2zLboHDG1Mi1yIx26kO7IO2oH6HntNkdrXUsyR5TY9CdjacShNYm172Rvtv51aGm2EW5lwLGi0u4gWASmtGH3qwG6jFR5wTQbPCwz/TCtlQXTXLi/RrA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Message-ID: <58f9bf8a-a086-4c23-75d6-f490df2e0718@suse.com> Date: Thu, 24 Feb 2022 14:24:59 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.6.1 Subject: [PATCH v2 2/2] x86: correct fencing around CLFLUSH Content-Language: en-US From: Jan Beulich To: "xen-devel@lists.xenproject.org" Cc: Andrew Cooper , Wei Liu , =?utf-8?q?Roger_Pau_Monn=C3=A9?= References: <265e87fa-0f05-7953-1bce-34999119d3c4@suse.com> In-Reply-To: <265e87fa-0f05-7953-1bce-34999119d3c4@suse.com> X-ClientProxiedBy: AS9PR04CA0077.eurprd04.prod.outlook.com (2603:10a6:20b:48b::13) To VI1PR04MB5600.eurprd04.prod.outlook.com (2603:10a6:803:e7::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: a530dd74-bf7a-48e7-8192-08d9f7990eea X-MS-TrafficTypeDiagnostic: AM0PR04MB5971:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: AFHENVLP144M/g5csPuqyN0DyG93UWW9QiPWDxTWiqJfCCwiXH/lzsWJSJwIwfnMIFMgtUtJptUvCCLXi4wg1nnfJG4kNy+cIEb0s5+7RVXEVsU9eJ5bWc/8xbBy2vP/bX8Xe+BnPgA2SwB/td31OjUsyaeEUOmWtsohgFq7BjZ6LCDk/rRxacbxk86Wi4tmSG5GMHBRd7dOpqBF3tlihOZnQP5u8H1F+V1opkfOLdcDEMw8dLKv1NEO87NjkzqkeDUmrsbYxuCLWEMyPXj+UOyTfsjWsFjulZXMXn5hxtGnP9NjXJA7BvBhEw8ZjH0QK5ifpe/iaGU2hROuDKiMchfjlnFwnPMh6nS76hdYtut2jS/XzLvNuoeUx/ww8ijsDBkIG+o7leKFwrXhbQ/FM/ZYzv4eTQE78gO9zGuH3+an8mopOq2V5AbOZUPmp6A77faNwdkdRiwQlgcXEin69bpf8vyWO2CnXGFwK9KI7MDvIOqyWj5J0OXlbeeACoCgqot6wLvtiWaaNdtIrCr5xuYk7v4WJ6PDOarJEssl0Q/NBef+XCZ5U5va53yrjoWP6p+KRy+KpvcS/N6wjXfAo4MwVxiG8/0bcYB1agRV35FPzY7v0KelZKYQ5gQUENfqrj0hRe/k931oNiFe+j7GMEqoaPiG8mMDkzQYnkqSKqwndIH1UFIsvRxz505LJyWnHuMCWEp+fsKFo1WkModqhNAm0NWU67+D1q5RNkJbbt4= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:VI1PR04MB5600.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(54906003)(31686004)(86362001)(508600001)(36756003)(6506007)(31696002)(6512007)(38100700002)(8676002)(6916009)(186003)(2906002)(5660300002)(66476007)(316002)(8936002)(26005)(4326008)(2616005)(6486002)(83380400001)(66946007)(66556008)(43740500002)(45980500001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?SVAenAmTJ6WMu66nqZyFxqrLs9Ab?= =?utf-8?q?pr+awLqSKphE9Vi4ZPc6fiZ6uHK+IeBNPWZBiLmN6F203LEx5Htv+q1NGtBtcmMjN?= =?utf-8?q?opf3ybDB3//sjyqkIYcx4CIqPyqdLx1OGkIdl2yy97SakFIpv72SqxKu5X8ppA3Mi?= =?utf-8?q?xUpg8sPJO/tFlkeke6RBi1DpF+HuD6PDSYVe+iwjZPfhUm/ETqrCUycW3ICpNEVru?= =?utf-8?q?JuX+Tk/81Ha10SELUuMyctr3X7ijzUJB3+4ej3C+4FK7B1AnPKElXoACTxiwGXVBr?= =?utf-8?q?g7vFNAuA9hT+wPqiO5oRvi59jwCePHshWdLYvEkACQ5hcM9HpkqK8iIAMdYmOsowC?= =?utf-8?q?5n17yXYK0I8TF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Ic0uIV22bJcLHd1x9x7KtDNO1/ue4SCiEE65OQLwrYFt8?= =?utf-8?q?oae24qtmao+g1gptSpXqQpsK1NSotiTgLKCeUYdUDZYcBQDktFkfX/ALwLDB/ZX2A?= =?utf-8?q?sJVwT2+0ZjOXSVUu8kgaTs8zq6saKm1FzOpVv82xQeNnq+8FjUeHuYDm0yQo+zAu+?= =?utf-8?q?Q5u18KwpHIDlQqz34SrHM3JJ3MNKWnAl9QssaujzeBc26HfLzN/QGOT7kZZND1UNw?= =?utf-8?q?8N/bU31DK7b2jmnfbg/FWIVHVd5NOhn7etuIVFX/MXrWmnwYaHK06i5BrEUrqK/Nk?= =?utf-8?q?tD7B3F35nUiCQ2sgpZk/nLSowiwERr+HB/uegxezQzfpEBKHqcNZt+c=3D?= X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: a530dd74-bf7a-48e7-8192-08d9f7990eea X-MS-Exchange-CrossTenant-AuthSource: VI1PR04MB5600.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Feb 2022 13:25:00.3348 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: f7a17af6-1c5c-4a36-aa8b-f5be247aa4ba X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: H+5trbyvSpiWi7KOnqNv7WgLR6c1r78D3tzFDr8uZO14VligxG+lgyQuJiBmmZMZXHuLX3UhFXPf9KMl15/mkQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM0PR04MB5971 As noted in the context of 3330013e6739 ("VT-d / x86: re-arrange cache syncing"): While cache_writeback() has the SFENCE on the correct side of CLFLUSHOPT, flush_area_local() doesn't. While I can't prove it due to lacking a copy of the old SDM version, I can only assume this placement was a result of what had been described there originally. In any event recent versions of the SDM hve been telling us otherwise. For AMD the situation is more complex: MFENCE is needed ahead and/or after CLFLUSH when the CPU doesn't also support CLFLUSHOPT. (It's "and" in the flush_area_local() case, as we cannot know what the caller's needs are. For cache_writeback() fencing ahead of the flush is sufficient.) Fixes: 623c720fc8da3 ("x86: use CLFLUSHOPT when available") Signed-off-by: Jan Beulich --- TBD: I'd be okay with not touching cache_writeback(), for only being used by VT-d right now. --- v2: Mark AMD behavior as a bug rather than a feature. Retain quotes. --- a/xen/arch/x86/cpu/amd.c +++ b/xen/arch/x86/cpu/amd.c @@ -761,6 +761,13 @@ static void __init noinline detect_bugs( */ if (!cpu_has_nscb) setup_force_cpu_cap(X86_BUG_NULL_SEG); + + /* + * AMD CPUs not supporting CLFLUSHOPT require MFENCE to serialize + * CLFLUSH against other memory accesses. + */ + if (!cpu_has_clflushopt) + setup_force_cpu_cap(X86_BUG_CLFLUSH_MFENCE); } static void init_amd(struct cpuinfo_x86 *c) --- a/xen/arch/x86/flushtlb.c +++ b/xen/arch/x86/flushtlb.c @@ -245,12 +245,15 @@ unsigned int flush_area_local(const void c->x86_clflush_size && c->x86_cache_size && sz && ((sz >> 10) < c->x86_cache_size) ) { - alternative("", "sfence", X86_FEATURE_CLFLUSHOPT); + alternative("", "mfence", X86_BUG_CLFLUSH_MFENCE); for ( i = 0; i < sz; i += c->x86_clflush_size ) alternative_input("ds; clflush %0", "data16 clflush %0", /* clflushopt */ X86_FEATURE_CLFLUSHOPT, "m" (((const char *)va)[i])); + alternative_2("", + "sfence", X86_FEATURE_CLFLUSHOPT, + "mfence", X86_BUG_CLFLUSH_MFENCE); flags &= ~FLUSH_CACHE; } else @@ -274,6 +277,8 @@ void cache_writeback(const void *addr, u unsigned int clflush_size = current_cpu_data.x86_clflush_size ?: 16; const void *end = addr + size; + alternative("", "mfence", X86_BUG_CLFLUSH_MFENCE); + addr -= (unsigned long)addr & (clflush_size - 1); for ( ; addr < end; addr += clflush_size ) { --- a/xen/arch/x86/include/asm/cpufeatures.h +++ b/xen/arch/x86/include/asm/cpufeatures.h @@ -47,6 +47,7 @@ XEN_CPUFEATURE(RET_SIMPLE, X86_SY #define X86_BUG_FPU_PTRS X86_BUG( 0) /* (F)X{SAVE,RSTOR} doesn't save/restore FOP/FIP/FDP. */ #define X86_BUG_NULL_SEG X86_BUG( 1) /* NULL-ing a selector preserves the base and limit. */ +#define X86_BUG_CLFLUSH_MFENCE X86_BUG( 2) /* MFENCE needed to serialize CLFLUSH */ /* Total number of capability words, inc synth and bug words. */ #define NCAPINTS (FSCAPINTS + X86_NR_SYNTH + X86_NR_BUG) /* N 32-bit words worth of info */