Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp1505058ybb; Thu, 9 Apr 2020 03:33:21 -0700 (PDT) X-Google-Smtp-Source: APiQypLFzyGctE93lTYN+NBSRVnFLy+XyJ2qfQnHTGCdl/1hgVO4aSj8N8RvjgticRcDpfGeFTrH X-Received: by 2002:a05:6830:4d1:: with SMTP id s17mr8355455otd.27.1586428401103; Thu, 09 Apr 2020 03:33:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586428401; cv=none; d=google.com; s=arc-20160816; b=Io3FWPbwtOgPU3LGCo2LfO0ESwcmiupr09X2dkWPOV5YKbOd0oe97lR5ieUsBlGT3M 6yT4Bnbi8zQk+IOYXZ7zPxzB6oXKsqDjxGdAcGfCl1i6JfMYGLQ5IZ16rzKl7tdHM7g+ 8uvL5RbV7mekM67MQZVoFLyLQCdxFCYCNObuxFmXaTeyo5HmnO5gvy8N8MPaOe0H/i/1 AUD9XBeeZqhyvyMXmVzWkSu9A1U9y9OIYg4gOA+hOBn8+qY8S5z+0UTT3Os1flf3g1F7 xeqzhOxeZNMqnFWs1coDL0d+3AU2jrEqc5IFHSaePo/Qs8Bw3P++qWP1u123xYwi+mdT ReEQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature; bh=5l4oIm1W9iSBccPrCEaBXIuDP10N0FjMt/RbcFAy1Mo=; b=vkWsX3JScRUiWKHFx1/tTpHttiwzSo8AV5Ud3Z62pHk2aMNKpew+doEmlIOdriqTvD 4Q212DMcCuIA9ENh2t/aJERcsNCSSYItqQNZBub7pP51lXcIC3NrP7dzJkQYkn91r1rN ZwJAA8w5xHA7b98KsOScQB439Mu/BwMttghZCm/GwgfnpKsR/jy6Ei8AcB5u1FJSQ1He HyFynvimmbSFqAwmNxW70icqtXS9drgMY6s/7EBIowQwIK4jIb/iVb82L+kBIvrX0TbS oF9JX/2rWHY3MIVITVFqW6ImOH1p7zNiV0AIxDJ9eduS+1Ymp+qIgLd8hSk9RXBMSSDI 98hA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=He7R8eCI; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f9si4041526oti.77.2020.04.09.03.33.07; Thu, 09 Apr 2020 03:33:21 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=He7R8eCI; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726627AbgDIKbR (ORCPT + 99 others); Thu, 9 Apr 2020 06:31:17 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:21500 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725970AbgDIKbR (ORCPT ); Thu, 9 Apr 2020 06:31:17 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1586428276; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5l4oIm1W9iSBccPrCEaBXIuDP10N0FjMt/RbcFAy1Mo=; b=He7R8eCIUhzDFVSzs11vsRDN1AjlojkWMZ9DrPwJAAhasK1ydoEQtuef7TrTo0IIKGl7MU ASwJlAy9VrS1qcmb29pKciTTCkqzjDx/xKsrEVZi8eGEoiwvVrk1EAGYSeHAlbxtVhjGH3 xz+NUTDuueFRMm3P/KweIesDs0RE4M8= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-482-rsetYb6kPoiWgRizy7nklQ-1; Thu, 09 Apr 2020 06:31:14 -0400 X-MC-Unique: rsetYb6kPoiWgRizy7nklQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 3B875107ACC4; Thu, 9 Apr 2020 10:31:12 +0000 (UTC) Received: from [10.36.115.53] (ovpn-115-53.ams2.redhat.com [10.36.115.53]) by smtp.corp.redhat.com (Postfix) with ESMTPS id CD0079A253; Thu, 9 Apr 2020 10:31:05 +0000 (UTC) Subject: Re: [PATCH v11 10/10] iommu/vt-d: Add custom allocator for IOASID To: Jacob Pan , Lu Baolu , iommu@lists.linux-foundation.org, LKML , Joerg Roedel , David Woodhouse , Jean-Philippe Brucker Cc: Yi Liu , "Tian, Kevin" , Raj Ashok , Alex Williamson , Christoph Hellwig , Jonathan Cameron References: <1585939334-21396-1-git-send-email-jacob.jun.pan@linux.intel.com> <1585939334-21396-11-git-send-email-jacob.jun.pan@linux.intel.com> From: Auger Eric Message-ID: <7a471ea9-b31d-b0a7-f66f-1e23c253fbee@redhat.com> Date: Thu, 9 Apr 2020 12:31:04 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.4.0 MIME-Version: 1.0 In-Reply-To: <1585939334-21396-11-git-send-email-jacob.jun.pan@linux.intel.com> Content-Type: text/plain; charset=windows-1252 Content-Language: en-US Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Jacob, On 4/3/20 8:42 PM, Jacob Pan wrote: > When VT-d driver runs in the guest, PASID allocation must be > performed via virtual command interface. This patch registers a > custom IOASID allocator which takes precedence over the default > XArray based allocator. The resulting IOASID allocation will always > come from the host. This ensures that PASID namespace is system- > wide. > > Signed-off-by: Lu Baolu > Signed-off-by: Liu, Yi L > Signed-off-by: Jacob Pan > --- > drivers/iommu/intel-iommu.c | 84 +++++++++++++++++++++++++++++++++++++++++++++ > include/linux/intel-iommu.h | 2 ++ > 2 files changed, 86 insertions(+) > > diff --git a/drivers/iommu/intel-iommu.c b/drivers/iommu/intel-iommu.c > index 045c5c08d71d..ff3f0386951f 100644 > --- a/drivers/iommu/intel-iommu.c > +++ b/drivers/iommu/intel-iommu.c > @@ -1732,6 +1732,9 @@ static void free_dmar_iommu(struct intel_iommu *iommu) > if (ecap_prs(iommu->ecap)) > intel_svm_finish_prq(iommu); > } > + if (ecap_vcs(iommu->ecap) && vccap_pasid(iommu->vccap)) > + ioasid_unregister_allocator(&iommu->pasid_allocator); > + > #endif > } > > @@ -3266,6 +3269,84 @@ static int copy_translation_tables(struct intel_iommu *iommu) > return ret; > } > > +#ifdef CONFIG_INTEL_IOMMU_SVM > +static ioasid_t intel_vcmd_ioasid_alloc(ioasid_t min, ioasid_t max, void *data) > +{ > + struct intel_iommu *iommu = data; > + ioasid_t ioasid; > + > + if (!iommu) > + return INVALID_IOASID; > + /* > + * VT-d virtual command interface always uses the full 20 bit > + * PASID range. Host can partition guest PASID range based on > + * policies but it is out of guest's control. > + */ > + if (min < PASID_MIN || max > intel_pasid_max_id) > + return INVALID_IOASID; > + > + if (vcmd_alloc_pasid(iommu, &ioasid)) > + return INVALID_IOASID; > + > + return ioasid; > +} > + > +static void intel_vcmd_ioasid_free(ioasid_t ioasid, void *data) > +{ > + struct intel_iommu *iommu = data; > + > + if (!iommu) > + return; > + /* > + * Sanity check the ioasid owner is done at upper layer, e.g. VFIO > + * We can only free the PASID when all the devices are unbound. > + */ > + if (ioasid_find(NULL, ioasid, NULL)) { > + pr_alert("Cannot free active IOASID %d\n", ioasid); > + return; > + } > + vcmd_free_pasid(iommu, ioasid); > +} > + > +static void register_pasid_allocator(struct intel_iommu *iommu) > +{ > + /* > + * If we are running in the host, no need for custom allocator > + * in that PASIDs are allocated from the host system-wide. > + */ > + if (!cap_caching_mode(iommu->cap)) > + return; > + > + if (!sm_supported(iommu)) { > + pr_warn("VT-d Scalable Mode not enabled, no PASID allocation\n"); > + return; > + } > + > + /* > + * Register a custom PASID allocator if we are running in a guest, > + * guest PASID must be obtained via virtual command interface. > + * There can be multiple vIOMMUs in each guest but only one allocator > + * is active. All vIOMMU allocators will eventually be calling the same > + * host allocator. > + */ nit: I prefer if (!ecap_vcs(iommu->ecap) || !vccap_pasid(iommu->vccap)) returns; as it removes indents. > + if (ecap_vcs(iommu->ecap) && vccap_pasid(iommu->vccap)) { > + pr_info("Register custom PASID allocator\n"); > + iommu->pasid_allocator.alloc = intel_vcmd_ioasid_alloc; > + iommu->pasid_allocator.free = intel_vcmd_ioasid_free; > + iommu->pasid_allocator.pdata = (void *)iommu; > + if (ioasid_register_allocator(&iommu->pasid_allocator)) { > + pr_warn("Custom PASID allocator failed, scalable mode disabled\n"); > + /* > + * Disable scalable mode on this IOMMU if there > + * is no custom allocator. Mixing SM capable vIOMMU > + * and non-SM vIOMMU are not supported. > + */ > + intel_iommu_sm = 0; > + } > + } > +} > +#endif > + > static int __init init_dmars(void) > { > struct dmar_drhd_unit *drhd; > @@ -3383,6 +3464,9 @@ static int __init init_dmars(void) > */ > for_each_active_iommu(iommu, drhd) { > iommu_flush_write_buffer(iommu); > +#ifdef CONFIG_INTEL_IOMMU_SVM > + register_pasid_allocator(iommu); > +#endif > iommu_set_root_entry(iommu); > iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL); > iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH); > diff --git a/include/linux/intel-iommu.h b/include/linux/intel-iommu.h > index f652db3198d9..e122cb30388e 100644 > --- a/include/linux/intel-iommu.h > +++ b/include/linux/intel-iommu.h > @@ -19,6 +19,7 @@ > #include > #include > #include > +#include > > #include > #include > @@ -588,6 +589,7 @@ struct intel_iommu { > #ifdef CONFIG_INTEL_IOMMU_SVM > struct page_req_dsc *prq; > unsigned char prq_name[16]; /* Name for PRQ interrupt */ > + struct ioasid_allocator_ops pasid_allocator; /* Custom allocator for PASIDs */ > #endif > struct q_inval *qi; /* Queued invalidation info */ > u32 *iommu_state; /* Store iommu states between suspend and resume.*/ > Besides Reviewed-by: Eric Auger Thanks Eric