Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp371648ybh; Sat, 18 Jul 2020 06:40:32 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzBFJo/c2nTvn6n7SZibblD+f2dfCIbB9TdHZLKdquG8pjc66fUhWohKM/NJhoPaCspltPN X-Received: by 2002:a05:6402:b84:: with SMTP id cf4mr13343625edb.21.1595079632129; Sat, 18 Jul 2020 06:40:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595079632; cv=none; d=google.com; s=arc-20160816; b=vhUtQZXsYn0GGO7ac88m5NSrdI60Rz6/JGnhgcD57BX/s3Bq4/yreO0ecaHhu+82aj m24pY4vNP0/faIbsnvdp/V7q0Ht3qAsAiSk4m3QHbMEzshIRAsX3TLBE/0EktSvFnM+j moCHMBPV13fXU0xbso2Yzojt5NdVcn4kvdN+SPgdQNxxkhAXDzc2eK5JMN2Xsh49pzm/ IrUNmzYLVhqwxlm/oM5+aifXMIsABynBjOX9UQzQ5ppvcXNLUjxuE8NthOTV9cHNdMnf aeOS/EYBm+Q1gCemm2QLdu+IgV42Xh9SLa97tacXeyCDyZYlpGtEMZrzjmZnZlGDdqxp QqPg== 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=80URiCGOEAgO/63+/ISh7h+yxZnKPojhEpNhvm3SzK4=; b=GSrOEpplHCIzbMWJaZjXKwk0VI7QezrorXbKuhx/4ilKdH9URkks3bSiwn/pbbPSp/ GiK7STIkN6sknhMsHZjr0klm45d+vQMYmgWc8F7g7rtV5oqDPrHN7l7WOWgkO8NV1ATf P/IJf8gq4JNItWnoPl03VrwVyw9IDa6RCjrVehU5NpMcAoWuEnC8iP7cmu8McPdAyn3i 6rQMyqTRGmoW+o6igmXe/oh4JqxUp5/qOv/6SXZzWPIEQbXiKKXDDv1jOPbaEnp3vJDa tVweNQ4hz8NDzL6N+lud/edip2RoMZArfW+dlVgfaF8vMAzqv6lPTrJjcqPhVKY4L2mE eEUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="Crmu7c+/"; 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=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t17si7817558ejs.635.2020.07.18.06.40.08; Sat, 18 Jul 2020 06:40:32 -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; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="Crmu7c+/"; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726798AbgGRNjy (ORCPT + 99 others); Sat, 18 Jul 2020 09:39:54 -0400 Received: from us-smtp-1.mimecast.com ([205.139.110.61]:20509 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726574AbgGRNjy (ORCPT ); Sat, 18 Jul 2020 09:39:54 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1595079591; 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=80URiCGOEAgO/63+/ISh7h+yxZnKPojhEpNhvm3SzK4=; b=Crmu7c+/BfeprVX7nRXchYzwHXBFPGTerB51haqcSFrtgZCwJ3P74D6P13jjGN+XBQxCi3 LPdtKdCw5p8/LA7whHlPNx3iMC5bDwuTy722smvBk1GuwgeACka2Pal15L5L6q4T3ksJxI vzSuX7gdC/coPNtaYNr3/qxO8Vhp++o= 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-226-DNr2txDpNa-2ajYGgKTcPA-1; Sat, 18 Jul 2020 09:39:47 -0400 X-MC-Unique: DNr2txDpNa-2ajYGgKTcPA-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id A14491800D42; Sat, 18 Jul 2020 13:39:45 +0000 (UTC) Received: from [10.36.115.54] (ovpn-115-54.ams2.redhat.com [10.36.115.54]) by smtp.corp.redhat.com (Postfix) with ESMTPS id F1CE675565; Sat, 18 Jul 2020 13:39:35 +0000 (UTC) Subject: Re: [PATCH v5 14/15] vfio: Document dual stage control To: Liu Yi L , alex.williamson@redhat.com, baolu.lu@linux.intel.com, joro@8bytes.org Cc: kevin.tian@intel.com, jacob.jun.pan@linux.intel.com, ashok.raj@intel.com, jun.j.tian@intel.com, yi.y.sun@intel.com, jean-philippe@linaro.org, peterx@redhat.com, hao.wu@intel.com, stefanha@gmail.com, iommu@lists.linux-foundation.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org References: <1594552870-55687-1-git-send-email-yi.l.liu@intel.com> <1594552870-55687-15-git-send-email-yi.l.liu@intel.com> From: Auger Eric Message-ID: Date: Sat, 18 Jul 2020 15:39:34 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.5.0 MIME-Version: 1.0 In-Reply-To: <1594552870-55687-15-git-send-email-yi.l.liu@intel.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Yi, On 7/12/20 1:21 PM, Liu Yi L wrote: > From: Eric Auger > > The VFIO API was enhanced to support nested stage control: a bunch of > new iotcls and usage guideline. ioctls > > Let's document the process to follow to set up nested mode. > > Cc: Kevin Tian > CC: Jacob Pan > Cc: Alex Williamson > Cc: Eric Auger > Cc: Jean-Philippe Brucker > Cc: Joerg Roedel > Cc: Lu Baolu > Reviewed-by: Stefan Hajnoczi > Signed-off-by: Eric Auger > Signed-off-by: Liu Yi L > --- > v3 -> v4: > *) add review-by from Stefan Hajnoczi > > v2 -> v3: > *) address comments from Stefan Hajnoczi > > v1 -> v2: > *) new in v2, compared with Eric's original version, pasid table bind > and fault reporting is removed as this series doesn't cover them. > Original version from Eric. > https://lkml.org/lkml/2020/3/20/700 > --- > Documentation/driver-api/vfio.rst | 67 +++++++++++++++++++++++++++++++++++++++ > 1 file changed, 67 insertions(+) > > diff --git a/Documentation/driver-api/vfio.rst b/Documentation/driver-api/vfio.rst > index f1a4d3c..0672c45 100644 > --- a/Documentation/driver-api/vfio.rst > +++ b/Documentation/driver-api/vfio.rst > @@ -239,6 +239,73 @@ group and can access them as follows:: > /* Gratuitous device reset and go... */ > ioctl(device, VFIO_DEVICE_RESET); > > +IOMMU Dual Stage Control > +------------------------ > + > +Some IOMMUs support 2 stages/levels of translation. Stage corresponds to > +the ARM terminology while level corresponds to Intel's VTD terminology. > +In the following text we use either without distinction. > + > +This is useful when the guest is exposed with a virtual IOMMU and some > +devices are assigned to the guest through VFIO. Then the guest OS can use > +stage 1 (GIOVA -> GPA or GVA->GPA), while the hypervisor uses stage 2 for > +VM isolation (GPA -> HPA). > + > +Under dual stage translation, the guest gets ownership of the stage 1 page > +tables and also owns stage 1 configuration structures. The hypervisor owns > +the root configuration structure (for security reason), including stage 2 > +configuration. This works as long as configuration structures and page table > +formats are compatible between the virtual IOMMU and the physical IOMMU. > + > +Assuming the HW supports it, this nested mode is selected by choosing the > +VFIO_TYPE1_NESTING_IOMMU type through: > + > + ioctl(container, VFIO_SET_IOMMU, VFIO_TYPE1_NESTING_IOMMU); > + > +This forces the hypervisor to use the stage 2, leaving stage 1 available > +for guest usage. The guest stage 1 format depends on IOMMU vendor, and > +it is the same with the nesting configuration method. User space should > +check the format and configuration method after setting nesting type by > +using: > + > + ioctl(container->fd, VFIO_IOMMU_GET_INFO, &nesting_info); > + > +Details can be found in Documentation/userspace-api/iommu.rst. For Intel > +VT-d, each stage 1 page table is bound to host by: > + > + nesting_op->flags = VFIO_IOMMU_NESTING_OP_BIND_PGTBL; > + memcpy(&nesting_op->data, &bind_data, sizeof(bind_data)); > + ioctl(container->fd, VFIO_IOMMU_NESTING_OP, nesting_op); > + > +As mentioned above, guest OS may use stage 1 for GIOVA->GPA or GVA->GPA. the guest OS, here and below? > +GVA->GPA page tables are available when PASID (Process Address Space ID) > +is exposed to guest. e.g. guest with PASID-capable devices assigned. For > +such page table binding, the bind_data should include PASID info, which > +is allocated by guest itself or by host. This depends on hardware vendor. > +e.g. Intel VT-d requires to allocate PASID from host. This requirement is > +defined by the Virtual Command Support in VT-d 3.0 spec, guest software > +running on VT-d should allocate PASID from host kernel. because VTD 3.0 requires the unicity of the PASID, system wide, instead of the above repetition. To allocate PASID > +from host, user space should check the IOMMU_NESTING_FEAT_SYSWIDE_PASID > +bit of the nesting info reported from host kernel. VFIO reports the nesting > +info by VFIO_IOMMU_GET_INFO. User space could allocate PASID from host by: if SYSWIDE_PASID requirement is exposed, the userspace *must* allocate ... > + > + req.flags = VFIO_IOMMU_ALLOC_PASID; > + ioctl(container, VFIO_IOMMU_PASID_REQUEST, &req); > + > +With first stage/level page table bound to host, it allows to combine the > +guest stage 1 translation along with the hypervisor stage 2 translation to > +get final address. > + > +When the guest invalidates stage 1 related caches, invalidations must be > +forwarded to the host through > + > + nesting_op->flags = VFIO_IOMMU_NESTING_OP_CACHE_INVLD; > + memcpy(&nesting_op->data, &inv_data, sizeof(inv_data)); > + ioctl(container->fd, VFIO_IOMMU_NESTING_OP, nesting_op); > + > +Those invalidations can happen at various granularity levels, page, context, > +... > + > VFIO User API > ------------------------------------------------------------------------------- I see you dropped the unrecoverable error reporting part of the original contribution. By the way don't you need any error handling for either of the use cases on vtd? > > Thanks Eric