Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp2751165pxb; Mon, 31 Jan 2022 03:35:35 -0800 (PST) X-Google-Smtp-Source: ABdhPJyVbsvmOQspu2Daktv+hC2cBuAHfRYyxD5Z130X5HjXRxLLpJlQ56vqxCppWt/ENoYWKW5K X-Received: by 2002:a17:90b:3648:: with SMTP id nh8mr24197735pjb.145.1643628935766; Mon, 31 Jan 2022 03:35:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643628935; cv=none; d=google.com; s=arc-20160816; b=CJusbw2dRudcqZy6ejRMT4yrjLltsmjGi5onLklPLmwYUhJO8rFDSRNDfVtVXaJF84 655GpRhD3XmXjzsGE1ceh5mKuVAC7HqNgaZSJJ7Qn4JWTjP7r3FrYhP+LjcR7T3D1yH7 wVMIQhWiII52QQF9ZeZlPRbNTFzDWwf61yLutR5fubNhwMNwhFA28/b5JzIj+ED/w/y6 yTU8qA+tAUQKVXTWpgE3GdYk3LVLfSbUw30Gq7S377fPv/9F+A8S7zUWVgNKOpEBFTfj rIQ0sMzOdHeLgdEVroHSYEB6uMHYYPMb7Jcrsnxv0GHbQHL8qHc1T02ud78GvE9cCZxU YSUg== 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=dnI/5FrhyzWX6PU/pYBptw+7dTMAoBpwqv4IaxTShu8=; b=moEq78vyAwyb/fhwtG6ELu8tfTsVq9RQp797jKhEIsUr8CrGdGoeWTfABFkvszzI09 DKbziR9bn6oSJ3HP8tZRMAraaXy5Zf0h1+tBZ15m6iiJFxj220z+lao6mAjOcl2UkqQZ WmUQjc9CFYbVYHOL3VP1q2j+65FLiU7c9mcJ+++XpjRxLcV9yiox752gCxRl6xzMIPE6 d7EFEWwEfHCsv1+jVriEPksNeqPIJ5E1vS4zL4dpZQKYm7W+DB80Y3hftTK633Y0xZr/ Z5a98d3TzwC8z8h0VMDFV1oZPv4WKp+ZxWFon2RUcAnGELwZztKi1Y94kHIaenxU9chR 01/A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=nAqBN1pF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w10si11434616pla.44.2022.01.31.03.35.25; Mon, 31 Jan 2022 03:35:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=nAqBN1pF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S1351418AbiA1U3x (ORCPT + 99 others); Fri, 28 Jan 2022 15:29:53 -0500 Received: from mga02.intel.com ([134.134.136.20]:61407 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351190AbiA1U3Y (ORCPT ); Fri, 28 Jan 2022 15:29:24 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1643401764; x=1674937764; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=jUmhLCHJPiqhmaaqrTsN4KWS/+AZ+jsPvivdmM8ieFY=; b=nAqBN1pFHjse/39b2n6f6r9bvy79h2qiKtkuEsdfh8d5CpTFp13oHDIK sCkX0hRoElRg1pDbkoMNrgE47FRJM9/RshKIwT1w5qzC/20I4uj3Rqo+d J1u7OTcFML/WcPkMFSLAs1tXmPr/hJf1wSrMvhjsNn91v9u61zrLt65Dk 1SyG2YO/cd8ost5bGCzYtLUpFacbNC/8skIurICFbccy+j4WII+VzHqIv MK/RjE/oM4EM6YuxKYC9lJYFxGvGLWzNoz4Uc2KYx8mbU/s9Y9/XCs9IF VxNxfTE7UTC0rPX+SJig13LMZ4vfZUG0lHB88tdKkvai4NDn8VxvL5NF1 Q==; X-IronPort-AV: E=McAfee;i="6200,9189,10241"; a="234578012" X-IronPort-AV: E=Sophos;i="5.88,324,1635231600"; d="scan'208";a="234578012" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Jan 2022 12:29:23 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.88,324,1635231600"; d="scan'208";a="618827709" Received: from otcwcpicx3.sc.intel.com ([172.25.55.73]) by FMSMGA003.fm.intel.com with ESMTP; 28 Jan 2022 12:29:23 -0800 From: Fenghua Yu To: "Thomas Gleixner" , "Dave Hansen" , "Ingo Molnar" , "Borislav Petkov" , "Peter Zijlstra" , "Andy Lutomirski" , "Tony Luck" , "Lu Baolu" , "Joerg Roedel" , Josh Poimboeuf , "Jacob Pan" , "Ashok Raj" , "Ravi V Shankar" Cc: iommu@lists.linux-foundation.org, "x86" , "linux-kernel" , Fenghua Yu Subject: [PATCH v3 08/11] x86/traps: Demand-populate PASID MSR via #GP Date: Fri, 28 Jan 2022 12:29:02 -0800 Message-Id: <20220128202905.2274672-9-fenghua.yu@intel.com> X-Mailer: git-send-email 2.35.0 In-Reply-To: <20220128202905.2274672-1-fenghua.yu@intel.com> References: <20220128202905.2274672-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 All tasks start with PASID state disabled. This means that the first time they execute an ENQCMD instruction they will take a #GP fault. Modify the #GP fault handler to check if the "mm" for the task has already been allocated a PASID. If so, try to fix the #GP fault by loading the IA32_PASID MSR. Signed-off-by: Fenghua Yu Reviewed-by: Tony Luck --- v2: - Directly write IA32_PASID MSR in fixup while local IRQ is still disabled (Thomas) - Move #ifdef over to CONFIG_IOMMU_SVA since it is what defines mm->pasid and ->pasid_activated (Dave Hansen). - Rename try_fixup_pasid() -> try_fixup_enqcmd_gp(). This code really is highly specific to ENQCMD, not PASIDs (Dave Hansen). - Add lockdep assert and comment about context (Dave Hansen). - Re-flow the if() mess (Dave Hansen). arch/x86/kernel/traps.c | 55 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/arch/x86/kernel/traps.c b/arch/x86/kernel/traps.c index c9d566dcf89a..7ef00dee35be 100644 --- a/arch/x86/kernel/traps.c +++ b/arch/x86/kernel/traps.c @@ -39,6 +39,7 @@ #include #include #include +#include #include #include @@ -559,6 +560,57 @@ static bool fixup_iopl_exception(struct pt_regs *regs) return true; } +/* + * The unprivileged ENQCMD instruction generates #GPs if the + * IA32_PASID MSR has not been populated. If possible, populate + * the MSR from a PASID previously allocated to the mm. + */ +static bool try_fixup_enqcmd_gp(void) +{ +#ifdef CONFIG_IOMMU_SVA + u32 pasid; + + /* + * MSR_IA32_PASID is managed using XSAVE. Directly + * writing to the MSR is only possible when fpregs + * are valid and the fpstate is not. This is + * guaranteed when handling a userspace exception + * in *before* interrupts are re-enabled. + */ + lockdep_assert_irqs_disabled(); + + /* + * Hardware without ENQCMD will not generate + * #GPs that can be fixed up here. + */ + if (!cpu_feature_enabled(X86_FEATURE_ENQCMD)) + return false; + + pasid = current->mm->pasid; + + /* + * If the mm has not been allocated a + * PASID, the #GP can not be fixed up. + */ + if (!pasid_valid(pasid)) + return false; + + /* + * Did this thread already have its PASID activated? + * If so, the #GP must be from something else. + */ + if (current->pasid_activated) + return false; + + wrmsrl(MSR_IA32_PASID, pasid | MSR_IA32_PASID_VALID); + current->pasid_activated = 1; + + return true; +#else + return false; +#endif +} + DEFINE_IDTENTRY_ERRORCODE(exc_general_protection) { char desc[sizeof(GPFSTR) + 50 + 2*sizeof(unsigned long) + 1] = GPFSTR; @@ -567,6 +619,9 @@ DEFINE_IDTENTRY_ERRORCODE(exc_general_protection) unsigned long gp_addr; int ret; + if (user_mode(regs) && try_fixup_enqcmd_gp()) + return; + cond_local_irq_enable(regs); if (static_cpu_has(X86_FEATURE_UMIP)) { -- 2.35.0