From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E46A2C433F5 for ; Mon, 20 Sep 2021 20:06:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CC69C61244 for ; Mon, 20 Sep 2021 20:06:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238017AbhITUHp (ORCPT ); Mon, 20 Sep 2021 16:07:45 -0400 Received: from mga05.intel.com ([192.55.52.43]:60376 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231153AbhITUFn (ORCPT ); Mon, 20 Sep 2021 16:05:43 -0400 X-IronPort-AV: E=McAfee;i="6200,9189,10113"; a="308775437" X-IronPort-AV: E=Sophos;i="5.85,309,1624345200"; d="scan'208";a="308775437" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Sep 2021 13:02:16 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.85,309,1624345200"; d="scan'208";a="473779504" Received: from otcwcpicx3.sc.intel.com ([172.25.55.73]) by fmsmga007.fm.intel.com with ESMTP; 20 Sep 2021 13:02:16 -0700 From: Fenghua Yu To: "Thomas Gleixner" , "Ingo Molnar" , "Borislav Petkov" , "Peter Zijlstra" , "Andy Lutomirski" , "Dave Hansen" , "Tony Luck" , "Lu Baolu" , "Joerg Roedel" , Josh Poimboeuf , "Dave Jiang" , "Jacob Jun Pan" , "Ashok Raj" , "Ravi V Shankar" Cc: iommu@lists.linux-foundation.org, "x86" , "linux-kernel" , Fenghua Yu Subject: [PATCH 7/8] tools/objtool: Check for use of the ENQCMD instruction in the kernel Date: Mon, 20 Sep 2021 19:23:48 +0000 Message-Id: <20210920192349.2602141-8-fenghua.yu@intel.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210920192349.2602141-1-fenghua.yu@intel.com> References: <20210920192349.2602141-1-fenghua.yu@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The ENQCMD implicitly accesses the PASID_MSR to fill in the pasid field of the descriptor being submitted to an accelerator. But there is no precise (and stable across kernel changes) point at which the PASID_MSR is updated from the value for one task to the next. Kernel code that uses accelerators must always use the ENQCMDS instruction which does not access the PASID_MSR. Check for use of the ENQCMD instruction in the kernel and warn on its usage. Checking the invalid instruction is a relatively new use of objtool and I'm open to feedback about the approach. Signed-off-by: Fenghua Yu Reviewed-by: Tony Luck --- tools/objtool/arch/x86/decode.c | 10 +++++++++- tools/objtool/check.c | 20 ++++++++++++++++++++ tools/objtool/include/objtool/arch.h | 1 + 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/tools/objtool/arch/x86/decode.c b/tools/objtool/arch/x86/decode.c index bc821056aba9..e63b44ba3bd4 100644 --- a/tools/objtool/arch/x86/decode.c +++ b/tools/objtool/arch/x86/decode.c @@ -110,7 +110,7 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec, { struct insn insn; int x86_64, ret; - unsigned char op1, op2, + unsigned char op1, op2, op3, rex = 0, rex_b = 0, rex_r = 0, rex_w = 0, rex_x = 0, modrm = 0, modrm_mod = 0, modrm_rm = 0, modrm_reg = 0, sib = 0, /* sib_scale = 0, */ sib_index = 0, sib_base = 0; @@ -137,6 +137,7 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec, op1 = insn.opcode.bytes[0]; op2 = insn.opcode.bytes[1]; + op3 = insn.opcode.bytes[2]; if (insn.rex_prefix.nbytes) { rex = insn.rex_prefix.bytes[0]; @@ -489,6 +490,13 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec, /* nopl/nopw */ *type = INSN_NOP; + } else if (op2 == 0x38 && op3 == 0xf8) { + if (insn.prefixes.nbytes == 1 && + insn.prefixes.bytes[0] == 0xf2) { + /* enqcmd */ + *type = INSN_ENQCMD; + } + } else if (op2 == 0xa0 || op2 == 0xa8) { /* push fs/gs */ diff --git a/tools/objtool/check.c b/tools/objtool/check.c index e5947fbb9e7a..91d13521d9d6 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -3133,6 +3133,21 @@ static int validate_reachable_instructions(struct objtool_file *file) return 0; } +static int validate_enqcmd(struct objtool_file *file) +{ + struct instruction *insn; + + for_each_insn(file, insn) { + if (insn->type == INSN_ENQCMD) { + WARN_FUNC("enqcmd instruction", insn->sec, + insn->offset); + return 1; + } + } + + return 0; +} + int check(struct objtool_file *file) { int ret, warnings = 0; @@ -3147,6 +3162,11 @@ int check(struct objtool_file *file) if (list_empty(&file->insn_list)) goto out; + ret = validate_enqcmd(file); + if (ret < 0) + goto out; + warnings += ret; + if (vmlinux && !validate_dup) { ret = validate_vmlinux_functions(file); if (ret < 0) diff --git a/tools/objtool/include/objtool/arch.h b/tools/objtool/include/objtool/arch.h index 062bb6e9b865..5147c0762b49 100644 --- a/tools/objtool/include/objtool/arch.h +++ b/tools/objtool/include/objtool/arch.h @@ -26,6 +26,7 @@ enum insn_type { INSN_CLAC, INSN_STD, INSN_CLD, + INSN_ENQCMD, INSN_OTHER, }; -- 2.33.0