From patchwork Fri Nov 25 12:58:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiaxi Chen X-Patchwork-Id: 25995 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp3994925wrr; Fri, 25 Nov 2022 04:59:17 -0800 (PST) X-Google-Smtp-Source: AA0mqf45+u+BV04cLOeaSaQWSFqmeX6TjGMPuWb3BW5wFlWWZqLLHR3L0JA+EXajJT4NFqbbz1MR X-Received: by 2002:a17:906:4b04:b0:7b9:a75b:faf9 with SMTP id y4-20020a1709064b0400b007b9a75bfaf9mr11467254eju.473.1669381157514; Fri, 25 Nov 2022 04:59:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669381157; cv=none; d=google.com; s=arc-20160816; b=eb5H9uFRrbmb9h9FYw5tJ8qZZjWmZAFRbrHhly91oR79gqyxMlU1OisTI9uIDvkF/s 8werSG4LvW29sX1VWQFir+fyR7WZtZaevUcU0T9bXmtVaOlwlLbPMtqeHfx1ZxMFnA6r azbcFxi+BqSuoDpaFgLi0nUiZqmLtiscSCQooZo6WSnMiG+uCh0qblSxjSJibd2BSeDD Ffr5oNCXmAHbgYw59X4IVNzH265sWYs5c1wJAdyCD8TMqHu9ph+c2q2uMHh2SakiUWIp POo1PKu+w8r7zC78NuMBZ7rAyhzI7YeVXN7XfSfobT2U53Ke6vvaUZ3gD0qgw6g5LKtZ rRdQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=wlxRWvY1AHg90Gj8hMLtlbNLYi+P8ePKNwc/UDWKJIM=; b=wVz0Q3yPYmbuOWwg1lbqB7/tB7sV5eI6XeI0a0RDANsbXEMXMgAYTeTrHWLPO4zt+N 9LCQFZHl/0JTVnp5TJKrLrpUZZjIcWgxNb6djTo7rEERJtZqimsNxrNGAPWdKCaPhhVk yH/Dztz4NHjVGdo32Hu9Sw54qxljQy91JHAVpKM3HMEBE0vu72YmGR057APf8u5zuxsK Oe3uGDVjoGWwbA9j4Vwo5STjQIZsRypllkLwekn4MQptNtbqJM6VWUJ0rMpQbLY/owBa Gzlbp7S6lIjU7aHfnfBQY+e/SyIlTobfSz9/BBsuWMK+X0ZJ/cIsArbhAu5MFrLqP2/B 3DBw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=BkzJ2uiD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b8-20020a056402350800b00459fc3fcf3csi2374392edd.102.2022.11.25.04.58.53; Fri, 25 Nov 2022 04:59:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=BkzJ2uiD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229686AbiKYM6r (ORCPT + 99 others); Fri, 25 Nov 2022 07:58:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56132 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229618AbiKYM6n (ORCPT ); Fri, 25 Nov 2022 07:58:43 -0500 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 76DC227DF7; Fri, 25 Nov 2022 04:58:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1669381122; x=1700917122; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=q5uru/FJg753cngn4X04rdHO32+oaebGg6gCaJzWBcU=; b=BkzJ2uiD9EKbYtRXxHpve8wq4lfvvfe55mpiJ4gRbh1jWk8JV+MlGsbi 4vzd1SKrfvN2O7DwYnDzBS67x8oeQuqVozo/0cO2c61thQ+h1EWgDXTPP EJWwpC6TW/acPoWjRRq+F19Kiu6u41Cl6JNcLd7rFRC3yIV9pxD/qqZL7 0iM0PDsMfuprV4pe7X71BYa2ugYtEJSH/ybcHOO04mKCbbf120goxq9ca vYgNbkKlAD0vFvq1JGSDGWNGyAfbkvl3+qVJIFu4PjqnAxqFtcIUt36Oy 3PlnioqajmTo59xAgIdIYngdwgxtfWFPg1DEH4pc/oosXiQurrmI8dZIe A==; X-IronPort-AV: E=McAfee;i="6500,9779,10541"; a="314513176" X-IronPort-AV: E=Sophos;i="5.96,193,1665471600"; d="scan'208";a="314513176" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Nov 2022 04:58:42 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10541"; a="706061216" X-IronPort-AV: E=Sophos;i="5.96,193,1665471600"; d="scan'208";a="706061216" Received: from jiaxichen-precision-3650-tower.sh.intel.com ([10.239.159.75]) by fmsmga008.fm.intel.com with ESMTP; 25 Nov 2022 04:58:37 -0800 From: Jiaxi Chen To: kvm@vger.kernel.org Cc: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, seanjc@google.com, pbonzini@redhat.com, ndesaulniers@google.com, alexandre.belloni@bootlin.com, peterz@infradead.org, jpoimboe@kernel.org, chang.seok.bae@intel.com, pawan.kumar.gupta@linux.intel.com, babu.moger@amd.com, jmattson@google.com, sandipan.das@amd.com, tony.luck@intel.com, sathyanarayanan.kuppuswamy@linux.intel.com, fenghua.yu@intel.com, keescook@chromium.org, nathan@kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v5 1/8] KVM: x86: Add BUILD_BUG_ON() to detect bad usage of "scattered" flags Date: Fri, 25 Nov 2022 20:58:38 +0800 Message-Id: <20221125125845.1182922-2-jiaxi.chen@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221125125845.1182922-1-jiaxi.chen@linux.intel.com> References: <20221125125845.1182922-1-jiaxi.chen@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-7.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_HI,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750473016500034619?= X-GMAIL-MSGID: =?utf-8?q?1750473016500034619?= From: Sean Christopherson Add a compile-time assert in the SF() macro to detect improper usage, i.e. to detect passing in an X86_FEATURE_* flag that isn't actually scattered by the kernel. Upcoming feature flags will be 100% KVM-only and will have X86_FEATURE_* macros that point at a kvm_only_cpuid_leafs word, not a kernel-defined word. Using SF() and thus boot_cpu_has() for such feature flags would access memory beyond x86_capability[NCAPINTS] and at best incorrectly hide a feature, and at worst leak kernel state to userspace. Signed-off-by: Sean Christopherson --- arch/x86/kvm/cpuid.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index 62bc7a01cecc..65b103874139 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -65,7 +65,13 @@ u32 xstate_required_size(u64 xstate_bv, bool compacted) #define KVM_X86_FEATURE_PSFD (13*32+28) /* Predictive Store Forwarding Disable */ #define F feature_bit -#define SF(name) (boot_cpu_has(X86_FEATURE_##name) ? F(name) : 0) + +/* Scattered Flag - For features that are scattered by cpufeatures.h. */ +#define SF(name) \ +({ \ + BUILD_BUG_ON(X86_FEATURE_##name >= MAX_CPU_FEATURES); \ + (boot_cpu_has(X86_FEATURE_##name) ? F(name) : 0); \ +}) /* * Magic value used by KVM when querying userspace-provided CPUID entries and