From patchwork Sat Mar 20 00:06:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 42727 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A1C0B3842431; Sat, 20 Mar 2021 00:07:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A1C0B3842431 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1616198830; bh=3MXGB7lhy1+f9WnQk4Br8xdHCEUsUNVPGKjJSSRJv4k=; h=References:In-Reply-To:Date:Subject:To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=akDZRD7yIXkmZtzblQTwsSHTCJC0IUMfLeFEYAQ20Geu9KJA+tpAe/9MPMEeWuZlG m67nziVlhBnLtYuZAiZ9T/brBDVptnYxz/RIT/vwDhiaOIETVEq6amVyhJVweaoKiL PqMqj55fXn9ychrPBQnXN4Bee5x7MOC8rIJVbpH4= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-ot1-x32f.google.com (mail-ot1-x32f.google.com [IPv6:2607:f8b0:4864:20::32f]) by sourceware.org (Postfix) with ESMTPS id 8B25D386F42F for ; Sat, 20 Mar 2021 00:07:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 8B25D386F42F Received: by mail-ot1-x32f.google.com with SMTP id m21-20020a9d7ad50000b02901b83efc84a0so10149420otn.10 for ; Fri, 19 Mar 2021 17:07:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=3MXGB7lhy1+f9WnQk4Br8xdHCEUsUNVPGKjJSSRJv4k=; b=WKGgmkcsoOhdkk3oovhBEdQVWWRw14LAuXuUpAXClYyBLQMOVvUEN9w+RrhrUAhIPN 6BlKsLR6EjNftVr/6pZCeej1wlpnQMftsgegKTcIs/xxUyZQ4GTzLPmb6qPH+u6XaJhD rmrLg7UVmKkO6nJ8DlNa9W0mCmTb3HIX2D3HXlUpA0HraFHONP9pvFvR2/bqb3ufXBvB Nn/yUWeZAYC0ZnehbtH3s8wsPkMVhmykyk7kwy1+SfYohtzdi+9pS2bEOjbMUd8wZUhE 5AnwkCb//tVaWZRAc6eY/lp8rPiCLMxvuU10lA+e/XspI76S/2W/h4BZGIXDke8KtgTx v8bA== X-Gm-Message-State: AOAM5312hVvx7JRG0LoJcBXnus2MoVgYm8+HbLwd7yYQow9+yrNGnmCj EFJxXwRzdRGWRsCC2McUHQkp2yNDHbuxMa6Ru3A= X-Google-Smtp-Source: ABdhPJxn/aVL+oeltHEGyrDt1BN/kMlKrhfDSm6eqtSU4aRg6Z/YnJi9eEx1oQ4AegTxrOGnmvJNcSuoBc20RSRoR4E= X-Received: by 2002:a05:6830:1515:: with SMTP id k21mr2985045otp.269.1616198826927; Fri, 19 Mar 2021 17:07:06 -0700 (PDT) MIME-Version: 1.0 References: <20210319132751.3845006-1-hjl.tools@gmail.com> In-Reply-To: Date: Fri, 19 Mar 2021 17:06:30 -0700 Message-ID: Subject: [PATCH v3] x86: Properly disable XSAVE related features [BZ #27605] To: DJ Delorie X-Spam-Status: No, score=-3036.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: "H.J. Lu via Libc-alpha" From: "H.J. Lu" Reply-To: "H.J. Lu" Cc: GNU C Library Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" On Fri, Mar 19, 2021 at 3:22 PM H.J. Lu wrote: > > On Fri, Mar 19, 2021 at 2:42 PM DJ Delorie wrote: > > > > "H.J. Lu" writes: > > > If OSXSAVE is disabled by glibc tunables, disable features which depend > > > on OSXSAVE. > > > > This one seems to ignore -XSAVE, both on an 11th gen (-XSAVE,-XSAVEC -> > > xsave()) and on 4th (just -XSAVE). Is this patch supposed to be used > > with the previous patch, or standalone? > > > > Fixed. Here is the v2 patch. OK for master? > Here is the v3 patch to disable all XSAVE related features if both XSAVE and XSAVEC aren't usable. From 7ed30c76a7e20f77eb1b65f9b38c905819ad1880 Mon Sep 17 00:00:00 2001 From: "H.J. Lu" Date: Fri, 19 Mar 2021 06:15:37 -0700 Subject: [PATCH v3] x86: Properly disable XSAVE related features [BZ #27605] 1. Support GLIBC_TUNABLES=glibc.cpu.hwcaps=-XSAVE. 2. Disable all features which depend on XSAVE: a. If OSXSAVE is disabled by glibc tunables. Or b. If both XSAVE and XSAVEC aren't usable. --- sysdeps/x86/cpu-features.c | 55 ++++++++++++++++++++++++++++++++++++++ sysdeps/x86/cpu-tunables.c | 1 + 2 files changed, 56 insertions(+) diff --git a/sysdeps/x86/cpu-features.c b/sysdeps/x86/cpu-features.c index d7248cbb45..e99e67f34d 100644 --- a/sysdeps/x86/cpu-features.c +++ b/sysdeps/x86/cpu-features.c @@ -654,6 +654,61 @@ no_cpuid: #if HAVE_TUNABLES TUNABLE_GET (hwcaps, tunable_val_t *, TUNABLE_CALLBACK (set_hwcaps)); + + bool disable_xsave_features = false; + + if (!CPU_FEATURE_USABLE_P (cpu_features, OSXSAVE)) + { + /* These features are usable only if OSXSAVE is usable. */ + CPU_FEATURE_UNSET (cpu_features, XSAVE); + CPU_FEATURE_UNSET (cpu_features, XSAVEOPT); + CPU_FEATURE_UNSET (cpu_features, XSAVEC); + CPU_FEATURE_UNSET (cpu_features, XGETBV_ECX_1); + CPU_FEATURE_UNSET (cpu_features, XFD); + + disable_xsave_features = true; + } + + if (disable_xsave_features + || (!CPU_FEATURE_USABLE_P (cpu_features, XSAVE) + && !CPU_FEATURE_USABLE_P (cpu_features, XSAVEC))) + { + /* Clear xsave_state_size if both XSAVE and XSAVEC aren't usable. */ + cpu_features->xsave_state_size = 0; + + CPU_FEATURE_UNSET (cpu_features, AVX); + CPU_FEATURE_UNSET (cpu_features, AVX2); + CPU_FEATURE_UNSET (cpu_features, AVX_VNNI); + CPU_FEATURE_UNSET (cpu_features, FMA); + CPU_FEATURE_UNSET (cpu_features, VAES); + CPU_FEATURE_UNSET (cpu_features, VPCLMULQDQ); + CPU_FEATURE_UNSET (cpu_features, XOP); + CPU_FEATURE_UNSET (cpu_features, F16C); + CPU_FEATURE_UNSET (cpu_features, AVX512F); + CPU_FEATURE_UNSET (cpu_features, AVX512CD); + CPU_FEATURE_UNSET (cpu_features, AVX512ER); + CPU_FEATURE_UNSET (cpu_features, AVX512PF); + CPU_FEATURE_UNSET (cpu_features, AVX512VL); + CPU_FEATURE_UNSET (cpu_features, AVX512DQ); + CPU_FEATURE_UNSET (cpu_features, AVX512BW); + CPU_FEATURE_UNSET (cpu_features, AVX512_4FMAPS); + CPU_FEATURE_UNSET (cpu_features, AVX512_4VNNIW); + CPU_FEATURE_UNSET (cpu_features, AVX512_BITALG); + CPU_FEATURE_UNSET (cpu_features, AVX512_IFMA); + CPU_FEATURE_UNSET (cpu_features, AVX512_VBMI); + CPU_FEATURE_UNSET (cpu_features, AVX512_VBMI2); + CPU_FEATURE_UNSET (cpu_features, AVX512_VNNI); + CPU_FEATURE_UNSET (cpu_features, AVX512_VPOPCNTDQ); + CPU_FEATURE_UNSET (cpu_features, AVX512_VP2INTERSECT); + CPU_FEATURE_UNSET (cpu_features, AVX512_BF16); + CPU_FEATURE_UNSET (cpu_features, AVX512_FP16); + CPU_FEATURE_UNSET (cpu_features, AMX_BF16); + CPU_FEATURE_UNSET (cpu_features, AMX_TILE); + CPU_FEATURE_UNSET (cpu_features, AMX_INT8); + + CPU_FEATURE_UNSET (cpu_features, FMA4); + } + #elif defined SHARED /* Reuse dl_platform, dl_hwcap and dl_hwcap_mask for x86. The glibc.cpu.hwcap_mask tunable is initialized already, so no diff --git a/sysdeps/x86/cpu-tunables.c b/sysdeps/x86/cpu-tunables.c index 126896f41b..61b05e5b1d 100644 --- a/sysdeps/x86/cpu-tunables.c +++ b/sysdeps/x86/cpu-tunables.c @@ -168,6 +168,7 @@ TUNABLE_CALLBACK (set_hwcaps) (tunable_val_t *valp) CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, MOVBE, 5); CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, SHSTK, 5); CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, SSSE3, 5); + CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, XSAVE, 5); } break; case 6: -- 2.30.2