From patchwork Thu Nov 30 11:34:38 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pierre Morel X-Patchwork-Id: 10084657 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 651766035E for ; Thu, 30 Nov 2017 11:34:59 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 61ADF29B98 for ; Thu, 30 Nov 2017 11:34:59 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5690029D17; Thu, 30 Nov 2017 11:34:59 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0860129B98 for ; Thu, 30 Nov 2017 11:34:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751964AbdK3Leq (ORCPT ); Thu, 30 Nov 2017 06:34:46 -0500 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:51936 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1750984AbdK3Leo (ORCPT ); Thu, 30 Nov 2017 06:34:44 -0500 Received: from pps.filterd (m0098421.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.21/8.16.0.21) with SMTP id vAUBYVsi113616 for ; Thu, 30 Nov 2017 06:34:44 -0500 Received: from e06smtp15.uk.ibm.com (e06smtp15.uk.ibm.com [195.75.94.111]) by mx0a-001b2d01.pphosted.com with ESMTP id 2ejh7p8153-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Thu, 30 Nov 2017 06:34:43 -0500 Received: from localhost by e06smtp15.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 30 Nov 2017 11:34:42 -0000 Received: from b06cxnps4076.portsmouth.uk.ibm.com (9.149.109.198) by e06smtp15.uk.ibm.com (192.168.101.145) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Thu, 30 Nov 2017 11:34:39 -0000 Received: from d06av25.portsmouth.uk.ibm.com (d06av25.portsmouth.uk.ibm.com [9.149.105.61]) by b06cxnps4076.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id vAUBYdQP25755766; Thu, 30 Nov 2017 11:34:39 GMT Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id D87ED11C054; Thu, 30 Nov 2017 11:29:11 +0000 (GMT) Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 8C22611C04C; Thu, 30 Nov 2017 11:29:11 +0000 (GMT) Received: from morel-ThinkPad-W530.boeblingen.de.ibm.com (unknown [9.145.49.184]) by d06av25.portsmouth.uk.ibm.com (Postfix) with ESMTP; Thu, 30 Nov 2017 11:29:11 +0000 (GMT) From: Pierre Morel To: alex.williamson@redhat.com Cc: cohuck@redhat.com, borntraeger@de.ibm.com, zyimin@linux.vnet.ibm.com, pasic@linux.vnet.ibm.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] vfio/iommu_type1: report the IOMMU aperture info Date: Thu, 30 Nov 2017 12:34:38 +0100 X-Mailer: git-send-email 2.7.4 X-TM-AS-GCONF: 00 x-cbid: 17113011-0020-0000-0000-000003D25756 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17113011-0021-0000-0000-00004267C7A1 Message-Id: <1512041678-4563-1-git-send-email-pmorel@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-11-30_03:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0 clxscore=1011 lowpriorityscore=0 impostorscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1709140000 definitions=main-1711300154 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When userland VFIO defines a new IOMMU for a guest it may want to specify to the guest the physical limits of the underlying host IOMMU to avoid access to forbidden memory ranges. Currently, the vfio_iommu_type1 driver does not report this information to userland. Let's extend the vfio_iommu_type1_info structure reported by the ioctl VFIO_IOMMU_GET_INFO command to report the IOMMU limits as new uint64_t entries aperture_start and aperture_end. Let's also extend the flags bit map to add a flag specifying if this extension of the info structure is reported or not. Signed-off-by: Pierre Morel --- drivers/vfio/vfio_iommu_type1.c | 42 +++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/vfio.h | 3 +++ 2 files changed, 45 insertions(+) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 8549cb1..7da5fe0 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -1526,6 +1526,40 @@ static int vfio_domains_have_iommu_cache(struct vfio_iommu *iommu) return ret; } +/** + * vfio_get_aperture - report minimal aperture of a vfio_iommu + * @iommu: the current vfio_iommu + * @start: a pointer to the aperture start + * @end : a pointer to the aperture end + * + * This function iterate on the domains using the given vfio_iommu + * and restrict the aperture to the minimal aperture common + * to all domains sharing this vfio_iommu. + */ +static void vfio_get_aperture(struct vfio_iommu *iommu, uint64_t *start, + uint64_t *end) +{ + struct iommu_domain_geometry geometry; + struct vfio_domain *domain; + + *start = 0; + *end = U64_MAX; + + mutex_lock(&iommu->lock); + /* loop on all domains using this vfio_iommu */ + list_for_each_entry(domain, &iommu->domain_list, next) { + iommu_domain_get_attr(domain->domain, DOMAIN_ATTR_GEOMETRY, + &geometry); + if (geometry.force_aperture) { + if (geometry.aperture_start > *start) + *start = geometry.aperture_start; + if (geometry.aperture_end < *end) + *end = geometry.aperture_end; + } + } + mutex_unlock(&iommu->lock); +} + static long vfio_iommu_type1_ioctl(void *iommu_data, unsigned int cmd, unsigned long arg) { @@ -1560,6 +1594,14 @@ static long vfio_iommu_type1_ioctl(void *iommu_data, info.iova_pgsizes = vfio_pgsize_bitmap(iommu); + minsz = min_t(size_t, info.argsz, sizeof(info)); + if (minsz >= offsetofend(struct vfio_iommu_type1_info, + aperture_end)) { + info.flags |= VFIO_IOMMU_INFO_APERTURE; + vfio_get_aperture(iommu, &info.aperture_start, + &info.aperture_end); + } + return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0; diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h index 0fb25fb..780d909 100644 --- a/include/uapi/linux/vfio.h +++ b/include/uapi/linux/vfio.h @@ -519,6 +519,9 @@ struct vfio_iommu_type1_info { __u32 flags; #define VFIO_IOMMU_INFO_PGSIZES (1 << 0) /* supported page sizes info */ __u64 iova_pgsizes; /* Bitmap of supported page sizes */ +#define VFIO_IOMMU_INFO_APERTURE (1 << 1) /* supported aperture info */ + __u64 aperture_start; /* start of DMA aperture */ + __u64 aperture_end; /* end of DMA aperture */ }; #define VFIO_IOMMU_GET_INFO _IO(VFIO_TYPE, VFIO_BASE + 12)