Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp868812ybb; Fri, 20 Mar 2020 09:22:44 -0700 (PDT) X-Google-Smtp-Source: ADFU+vuUvG+E+bG7ztKu6pLfmnW6qNqzFblvaPHnhZNcfYjQL9Sd0GTc7riOcexSWLQsAzZoiqKe X-Received: by 2002:a05:6830:1251:: with SMTP id s17mr7828292otp.75.1584721363764; Fri, 20 Mar 2020 09:22:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584721363; cv=none; d=google.com; s=arc-20160816; b=avfrbe/1cfOtLdv9y5FPOHOT+fUJ01URqu4D+5O45X0JvUHavJaJ3Ako98VGNMmVMr nNwY4DbFPgeNVpBQSAwSNGEg0rwfrlAvf6JW+w3QhpfcT9j7PVVTYWr3EtHk9Ih+yW0C Juedg4d0OMqyTojxwO+hfKPQk2DdzkD1NefjgEAvalpOv769J7+JsRA/ndCVv6hq/Syo onVZLaaXlZY7gRwUKBuiypoVRZ6dAEBifi6u42WWlDW1EWf0zCikzESi685kiaC3tIRs rwqlfaFzbL/vjihJkW2Wywc0Au9XqHgJ+XKpoK53rGldfiP7jruKoXrYyM9ZAs8hbSwE 3Fag== 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:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=HWsrtV8xi0+PxqqXUFv44KyfDBrCKeGZUGmcPNEwi3s=; b=vLK7I/sCDYT8+jl+ffJQ3Mbq+gbVi3K2oY9LPEDHXkMlHlThNVerG3yP3/uFXqF9k2 TrNmO8NfPYTuU5RAZDUb5vpHP9tkkMO8PR6w0Yy22MB4wyLsGoDmQcbwuyy0CGA18AHo KLSzsOlsBrVTyHcIM3yrDdi90iv3TbKu6tqTYtj8tYvBdhLBfyEpfjQbI5tsg8ZrW7Yc PSEB/asA1gVH6zVfS8c5TW6CsPNU9J8nH9y2hxBS83R4YVgi1vLNjlqeptE9iR9v407N N4K7d5fgWSFJfpQ/Bc1AedoKK1FW2N+TNvgMfQ54nRhCUAN3sdJCrEuyXbhR0HQLWZ/I UYjg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=IDu4mDfg; 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 73si2997906oii.60.2020.03.20.09.22.26; Fri, 20 Mar 2020 09:22:43 -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=IDu4mDfg; 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 S1727529AbgCTQUO (ORCPT + 99 others); Fri, 20 Mar 2020 12:20:14 -0400 Received: from us-smtp-delivery-74.mimecast.com ([216.205.24.74]:28805 "EHLO us-smtp-delivery-74.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727563AbgCTQUD (ORCPT ); Fri, 20 Mar 2020 12:20:03 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1584721202; 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=IDu4mDfgHOExNHY5DCATScNvtBD3Yqxj/tkCnZjrTmADyGZY8WbqCn+zVnr3kwQxc1uuHx OsU4PLt/FGXA4IcJ9Z6VMYmPPtsEhRpuqA5JmIB+PIyx9UX43oux1tMYpI4PHRyo9rdveQ DHU6IPeUYiq6C3RwS77uE6Ujydl6IFA= 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: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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