Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp977400pxb; Wed, 29 Sep 2021 14:00:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyaKsQcGz0kP+5zGi4a5N6tQHFMgbec4NPWVuWicAiGKLhuoDFi+G9zA2THFXGXjZ3bNzVx X-Received: by 2002:a17:90a:514b:: with SMTP id k11mr2006395pjm.103.1632949250500; Wed, 29 Sep 2021 14:00:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632949250; cv=none; d=google.com; s=arc-20160816; b=kYhIRD0qqs4epuLsuPBiwp9tsL2OLyFm6RMYWmhtPemm+i3QG3XjnGYzpy2DTPakb4 zVWjF9VOqJPvvsVVo9v6sd6HcTv7XB9oEXQBRyv1MjztH4ekvG6piLGy0tzizEUulDrd lPvNh1kSPp9oPdKCRtOoYX4XN4Z+oPwGewzKZuwOanVnqFzKGj83aZWWZZ53GvxOTHWc 364r2AZCGADUhJSwTjbfBhIup6CLzG2Dm4YRcIFvJJaB7L4yASwnx1ZIe2dRExfweJog 8og8bWE97IERZwTnu1RZXMQ22+4XUNMrTeAVOW1wXiQ1Y4FDVQ1rAOoxWBIrb1gj0Om1 Teew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=cRIxOowpW/HglvZKnymIlqMwmv8KFs9+SG3lnBakjQM=; b=PH4NNT2U32D2Pec796HURt7UrpgVWu5eX+83M4NWjrR4KaevOZq39eYXZXgnftcjLO Lc6VqXKazp/N87Ri+SMIJPQlqmC109aaFCKCSIOHLmfET/X9KnYi5n6rNZ9BVsgpUSce iDNsu2hgMmO2/McnpyBBXMZCtzRTFNi8lGI81IZsZ2JZppyZItVRVtImpyjhKf+LtJRd PpV1pMdJYb02npuDMWkgcm/M6+r193Ukp41o3G8F8al/J5TFTahsY97R3Qrnh5QMed22 4bfAduLKtyHvxyxIRdpjxAPl00DYwDB599IDPyr2zP+3AIFuxO4cX0WrNWnc/Ajcp2+B OW3w== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (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 r13si1012795pgv.341.2021.09.29.14.00.36; Wed, 29 Sep 2021 14:00:50 -0700 (PDT) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346192AbhI2SdN (ORCPT + 99 others); Wed, 29 Sep 2021 14:33:13 -0400 Received: from mga02.intel.com ([134.134.136.20]:61107 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346190AbhI2SdK (ORCPT ); Wed, 29 Sep 2021 14:33:10 -0400 X-IronPort-AV: E=McAfee;i="6200,9189,10122"; a="212267247" X-IronPort-AV: E=Sophos;i="5.85,332,1624345200"; d="scan'208";a="212267247" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Sep 2021 11:31:29 -0700 X-IronPort-AV: E=Sophos;i="5.85,332,1624345200"; d="scan'208";a="563481249" Received: from agluck-desk2.sc.intel.com (HELO agluck-desk2.amr.corp.intel.com) ([10.3.52.146]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Sep 2021 11:31:28 -0700 Date: Wed, 29 Sep 2021 11:31:27 -0700 From: "Luck, Tony" To: Fenghua Yu Cc: Thomas Gleixner , Andy Lutomirski , Peter Zijlstra , Ingo Molnar , Borislav Petkov , Dave Hansen , Lu Baolu , Joerg Roedel , Josh Poimboeuf , Dave Jiang , Jacob Jun Pan , Raj Ashok , "Shankar, Ravi V" , iommu@lists.linux-foundation.org, the arch/x86 maintainers , Linux Kernel Mailing List Subject: Re: [PATCH 5/8] x86/mmu: Add mm-based PASID refcounting Message-ID: References: <87o88jfajo.ffs@tglx> <87k0j6dsdn.ffs@tglx> <87r1d78t2e.ffs@tglx> <75e95acc-6730-ddcf-d722-66e575076256@kernel.org> <877dez8fqu.ffs@tglx> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Sep 29, 2021 at 06:07:22PM +0000, Fenghua Yu wrote: > Add > +#ifdef CONFIG_IOMMU_SUPPORT > because mm->pasid and current-pasid_activated are defined only if > CONFIG_IOMMU_SUPPORT is defined. > > > + if (user_mode(regs) && current->mm->pasid && !current->pasid_activated) { > > Maybe need to add "&& cpu_feature_enabled(X86_FEATURE_ENQCMD)" because > the IA32_PASID MSR is only used when ENQCMD is enabled? > > > + current->pasid_activated = 1; > > + wrmsrl(MSR_IA32_PASID, current->mm->pasid | MSR_IA32_PASID_VALID); > > + return; > > + } > > + > > +endif New version that addresses those issues. Has ugly #ifdef in C code :-( If that's unacceptable, then could create some stub functions, or add a call to __try_fixup_pasid() that's in a file in the iommu code that is only built when CONFIG_IOMMU_SUPPORT is set. But either of those move the details far away from the #GP handler so make extra work for anyone trying to follow along with what is happening here. -Tony --- diff --git a/arch/x86/kernel/traps.c b/arch/x86/kernel/traps.c index a58800973aed..5a3c87fd65de 100644 --- a/arch/x86/kernel/traps.c +++ b/arch/x86/kernel/traps.c @@ -528,6 +528,32 @@ static enum kernel_gp_hint get_kernel_gp_address(struct pt_regs *regs, #define GPFSTR "general protection fault" +/* + * When a user executes the ENQCMD instruction it will #GP + * fault if the IA32_PASID MSR has not been set up with a + * valid PASID. + * So if the process has been allocated a PASID (mm->pasid) + * AND the IA32_PASID MSR has not been initialized, try to + * fix this #GP by initializing the IA32_PASID MSR. + * If the #GP was for some other reason, it will trigger + * again, but this routine will return false and the #GP + * will be processed. + */ +static void try_fixup_pasid(void) +{ + if (!cpu_feature_enabled(X86_FEATURE_ENQCMD)) + return false; + +#ifdef CONFIG_IOMMU_SUPPORT + if (current->mm->pasid && !current->pasid_activated) { + current->pasid_activated = 1; + wrmsrl(MSR_IA32_PASID, current->mm->pasid); + return true; + } +#endif + return false; +} + DEFINE_IDTENTRY_ERRORCODE(exc_general_protection) { char desc[sizeof(GPFSTR) + 50 + 2*sizeof(unsigned long) + 1] = GPFSTR; @@ -536,6 +562,9 @@ DEFINE_IDTENTRY_ERRORCODE(exc_general_protection) unsigned long gp_addr; int ret; + if (user_mode(regs) && try_fixup_pasid()) + return; + cond_local_irq_enable(regs); if (static_cpu_has(X86_FEATURE_UMIP)) {