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=-6.9 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS autolearn=ham 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 6C693C4332B for ; Fri, 20 Mar 2020 16:20:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 340DD20739 for ; Fri, 20 Mar 2020 16:20:02 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="jKeqs3E4" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727543AbgCTQUB (ORCPT ); Fri, 20 Mar 2020 12:20:01 -0400 Received: from us-smtp-delivery-74.mimecast.com ([216.205.24.74]:24326 "EHLO us-smtp-delivery-74.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727563AbgCTQUA (ORCPT ); Fri, 20 Mar 2020 12:20:00 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1584721199; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=HWsrtV8xi0+PxqqXUFv44KyfDBrCKeGZUGmcPNEwi3s=; b=jKeqs3E4Cxyhcjeak3nVEa4ihtCOVNzfPDyu1ZggLf67C13Asl2nrlSBuURSyhKh0nZwvT LJOOapJtkat+/siWH/07DkssXQoyuUNWbb6HcQ9ZBVIqnN6wZ5DC57neOwg+7RX0tSb4d9 omtE82psvTXYuTZn2SdyWd1QzfTIkg4= 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-151-4S9-rW7oMim-tephJXjksA-1; Fri, 20 Mar 2020 12:19:57 -0400 X-MC-Unique: 4S9-rW7oMim-tephJXjksA-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 818BA107ACCC; Fri, 20 Mar 2020 16:19:36 +0000 (UTC) Received: from laptop.redhat.com (ovpn-113-142.ams2.redhat.com [10.36.113.142]) by smtp.corp.redhat.com (Postfix) with ESMTP id BC38260BFB; Fri, 20 Mar 2020 16:19:32 +0000 (UTC) From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu, joro@8bytes.org, alex.williamson@redhat.com, jacob.jun.pan@linux.intel.com, yi.l.liu@intel.com, jean-philippe.brucker@arm.com, will.deacon@arm.com, robin.murphy@arm.com Cc: marc.zyngier@arm.com, peter.maydell@linaro.org, zhangfei.gao@gmail.com Subject: [PATCH v10 02/11] vfio: VFIO_IOMMU_CACHE_INVALIDATE Date: Fri, 20 Mar 2020 17:19:02 +0100 Message-Id: <20200320161911.27494-3-eric.auger@redhat.com> In-Reply-To: <20200320161911.27494-1-eric.auger@redhat.com> References: <20200320161911.27494-1-eric.auger@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Content-Transfer-Encoding: quoted-printable Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: "Liu, Yi L" When the guest "owns" the stage 1 translation structures, the host IOMMU driver has no knowledge of caching structure updates unless the guest invalidation requests are trapped and passed down to the host. This patch adds the VFIO_IOMMU_CACHE_INVALIDATE ioctl with aims at propagating guest stage1 IOMMU cache invalidations to the host. Signed-off-by: Liu, Yi L Signed-off-by: Eric Auger --- v8 -> v9: - change the ioctl ID v6 -> v7: - Use iommu_capsule struct - renamed vfio_iommu_for_each_dev into vfio_iommu_lookup_dev due to checkpatch error related to for_each_dev suffix v2 -> v3: - introduce vfio_iommu_for_each_dev back in this patch v1 -> v2: - s/TLB/CACHE - remove vfio_iommu_task usage - commit message rewording --- drivers/vfio/vfio_iommu_type1.c | 55 +++++++++++++++++++++++++++++++++ include/uapi/linux/vfio.h | 13 ++++++++ 2 files changed, 68 insertions(+) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_ty= pe1.c index bfacbd876ee1..04c6625098bb 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -124,6 +124,34 @@ struct vfio_regions { #define IS_IOMMU_CAP_DOMAIN_IN_CONTAINER(iommu) \ (!list_empty(&iommu->domain_list)) =20 +struct domain_capsule { + struct iommu_domain *domain; + void *data; +}; + +/* iommu->lock must be held */ +static int +vfio_iommu_lookup_dev(struct vfio_iommu *iommu, + int (*fn)(struct device *dev, void *data), + void *data) +{ + struct domain_capsule dc =3D {.data =3D data}; + struct vfio_domain *d; + struct vfio_group *g; + int ret =3D 0; + + list_for_each_entry(d, &iommu->domain_list, next) { + dc.domain =3D d->domain; + list_for_each_entry(g, &d->group_list, next) { + ret =3D iommu_group_for_each_dev(g->iommu_group, + &dc, fn); + if (ret) + break; + } + } + return ret; +} + static int put_pfn(unsigned long pfn, int prot); =20 /* @@ -2209,6 +2237,15 @@ vfio_attach_pasid_table(struct vfio_iommu *iommu, return ret; } =20 +static int vfio_cache_inv_fn(struct device *dev, void *data) +{ + struct domain_capsule *dc =3D (struct domain_capsule *)data; + struct vfio_iommu_type1_cache_invalidate *ustruct =3D + (struct vfio_iommu_type1_cache_invalidate *)dc->data; + + return iommu_cache_invalidate(dc->domain, dev, &ustruct->info); +} + static long vfio_iommu_type1_ioctl(void *iommu_data, unsigned int cmd, unsigned long arg) { @@ -2332,6 +2369,24 @@ static long vfio_iommu_type1_ioctl(void *iommu_dat= a, return 0; } else return -EINVAL; + } else if (cmd =3D=3D VFIO_IOMMU_CACHE_INVALIDATE) { + struct vfio_iommu_type1_cache_invalidate ustruct; + int ret; + + minsz =3D offsetofend(struct vfio_iommu_type1_cache_invalidate, + info); + + if (copy_from_user(&ustruct, (void __user *)arg, minsz)) + return -EFAULT; + + if (ustruct.argsz < minsz || ustruct.flags) + return -EINVAL; + + mutex_lock(&iommu->lock); + ret =3D vfio_iommu_lookup_dev(iommu, vfio_cache_inv_fn, + &ustruct); + mutex_unlock(&iommu->lock); + return ret; } =20 return -ENOTTY; diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h index e032a1fe6ed9..98212c1711e7 100644 --- a/include/uapi/linux/vfio.h +++ b/include/uapi/linux/vfio.h @@ -813,6 +813,19 @@ struct vfio_iommu_type1_set_pasid_table { =20 #define VFIO_IOMMU_SET_PASID_TABLE _IO(VFIO_TYPE, VFIO_BASE + 22) =20 +/** + * VFIO_IOMMU_CACHE_INVALIDATE - _IOWR(VFIO_TYPE, VFIO_BASE + 23, + * struct vfio_iommu_type1_cache_invalidate) + * + * Propagate guest IOMMU cache invalidation to the host. + */ +struct vfio_iommu_type1_cache_invalidate { + __u32 argsz; + __u32 flags; + struct iommu_cache_invalidate_info info; +}; +#define VFIO_IOMMU_CACHE_INVALIDATE _IO(VFIO_TYPE, VFIO_BASE + 23) + /* -------- Additional API for SPAPR TCE (Server POWERPC) IOMMU --------= */ =20 /* --=20 2.20.1