From patchwork Tue Jan 5 15:36:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Sistare X-Patchwork-Id: 11999659 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=-19.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, UNPARSEABLE_RELAY,USER_AGENT_GIT 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 7E009C433DB for ; Tue, 5 Jan 2021 16:05:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4F14E22C9D for ; Tue, 5 Jan 2021 16:05:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726246AbhAEQEn (ORCPT ); Tue, 5 Jan 2021 11:04:43 -0500 Received: from aserp2130.oracle.com ([141.146.126.79]:40584 "EHLO aserp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725936AbhAEQEn (ORCPT ); Tue, 5 Jan 2021 11:04:43 -0500 Received: from pps.filterd (aserp2130.oracle.com [127.0.0.1]) by aserp2130.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 105Fsji3094294; Tue, 5 Jan 2021 16:04:00 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references; s=corp-2020-01-29; bh=wlo2cVcl+/Th1nEAYhe73iPgHz0zOc5IAxfyoIhiGCE=; b=sdbrlKnvw3bc6Dyoey9JObZRK5QvDk9F1OehWrGc+Ytnv9+AZ9JqXbKeG5ai7nfwAWmW 4HlImScCQPK8iKtsduCzrh2S8EVdHr5QfLtCO+0BFqLqRZH46V2XHCACXkdeKLzqI208 dCdttiuzJpZZDwz8Dd0LLXOPyaNB0P5YvkdocOY1QKt3nor5se13gHcDIv62mvVe5U5f xyzTedyoXTMJUfhjjIdsOcGNI8DjBM1D0WEs4exJZcU0H+xowB0Yuv5Zzrm9J/Z5CfYm vFttUF7NmwBBvOtWn7MjzNHWvF2j/7GQUMHY2paF1/AXD3XgxOFpwP1VlmUWJOUdQIgm nA== Received: from userp3030.oracle.com (userp3030.oracle.com [156.151.31.80]) by aserp2130.oracle.com with ESMTP id 35tebasj32-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Tue, 05 Jan 2021 16:03:59 +0000 Received: from pps.filterd (userp3030.oracle.com [127.0.0.1]) by userp3030.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 105FtL0a175884; Tue, 5 Jan 2021 16:03:59 GMT Received: from userv0121.oracle.com (userv0121.oracle.com [156.151.31.72]) by userp3030.oracle.com with ESMTP id 35vct61n8k-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 05 Jan 2021 16:03:59 +0000 Received: from abhmp0016.oracle.com (abhmp0016.oracle.com [141.146.116.22]) by userv0121.oracle.com (8.14.4/8.13.8) with ESMTP id 105G3wQc015343; Tue, 5 Jan 2021 16:03:58 GMT Received: from ca-dev63.us.oracle.com (/10.211.8.221) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 05 Jan 2021 16:03:58 +0000 From: Steve Sistare To: kvm@vger.kernel.org Cc: Alex Williamson , Cornelia Huck , Kirti Wankhede , Steve Sistare Subject: [PATCH V1 1/5] vfio: maintain dma_list order Date: Tue, 5 Jan 2021 07:36:49 -0800 Message-Id: <1609861013-129801-2-git-send-email-steven.sistare@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1609861013-129801-1-git-send-email-steven.sistare@oracle.com> References: <1609861013-129801-1-git-send-email-steven.sistare@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9855 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 malwarescore=0 phishscore=0 bulkscore=0 mlxlogscore=999 suspectscore=0 mlxscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2101050099 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9855 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 phishscore=0 priorityscore=1501 spamscore=0 mlxscore=0 clxscore=1011 bulkscore=0 lowpriorityscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2101050099 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Keep entries properly sorted in the dma_list rb_tree so that iterating over multiple entries across a range with gaps works, without requiring one to delete each visited entry as in vfio_dma_do_unmap. Signed-off-by: Steve Sistare Reported-by: kernel test robot --- drivers/vfio/vfio_iommu_type1.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 5fbf0c1..02228d0 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -157,20 +157,24 @@ static struct vfio_group *vfio_iommu_find_iommu_group(struct vfio_iommu *iommu, static struct vfio_dma *vfio_find_dma(struct vfio_iommu *iommu, dma_addr_t start, size_t size) { + struct vfio_dma *res = 0; struct rb_node *node = iommu->dma_list.rb_node; while (node) { struct vfio_dma *dma = rb_entry(node, struct vfio_dma, node); - if (start + size <= dma->iova) + if (start < dma->iova + dma->size) { + res = dma; + if (start >= dma->iova) + break; node = node->rb_left; - else if (start >= dma->iova + dma->size) + } else { node = node->rb_right; - else - return dma; + } } - - return NULL; + if (res && size && res->iova >= start + size) + res = 0; + return res; } static void vfio_link_dma(struct vfio_iommu *iommu, struct vfio_dma *new) @@ -182,7 +186,7 @@ static void vfio_link_dma(struct vfio_iommu *iommu, struct vfio_dma *new) parent = *link; dma = rb_entry(parent, struct vfio_dma, node); - if (new->iova + new->size <= dma->iova) + if (new->iova < dma->iova) link = &(*link)->rb_left; else link = &(*link)->rb_right; From patchwork Tue Jan 5 15:36:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Sistare X-Patchwork-Id: 11999665 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=-19.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, UNPARSEABLE_RELAY,USER_AGENT_GIT 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 BB49AC43381 for ; Tue, 5 Jan 2021 16:05:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 98E0E22C9D for ; Tue, 5 Jan 2021 16:05:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728294AbhAEQEq (ORCPT ); Tue, 5 Jan 2021 11:04:46 -0500 Received: from aserp2120.oracle.com ([141.146.126.78]:49614 "EHLO aserp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726749AbhAEQEp (ORCPT ); Tue, 5 Jan 2021 11:04:45 -0500 Received: from pps.filterd (aserp2120.oracle.com [127.0.0.1]) by aserp2120.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 105FssY4031176; Tue, 5 Jan 2021 16:04:00 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references; s=corp-2020-01-29; bh=4/H3lHUMJl6zxVO4zaYyVALKN7VwtyrDABjdtel12JY=; b=jkkC44nPHAaX8c1BiKWB5ybkXtYMsolvEjMU4edXDMLAWn0MODcFLW0r0hRUegoSyYLR Dgf6COkro4L1hP5R0Do889PLP97yLYND6V/nUiZGa6t/RWYF3sgV94yuV6TEYY+g/blc YbZWGRR7irREce3erRGVoCXpnpKY6gYeNs6ZB1xY/NqfKfEAhSO3wHDcHhU2qBX7U7gF YFZgvbLJ81b2aFr9kW4aP7RcFJ01kUAi6Qou0/vNd7pia65AWBVEkwN8KbaXuiNX+1zV JzD8YxBAiGFlEjt8UNQ8u2pkOivCLd9MU4Bbcuorn+DjCGG+d05EPmhmywtOHYtsAjuj SA== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by aserp2120.oracle.com with ESMTP id 35tgsksfan-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Tue, 05 Jan 2021 16:04:00 +0000 Received: from pps.filterd (aserp3020.oracle.com [127.0.0.1]) by aserp3020.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 105FukTG187413; Tue, 5 Jan 2021 16:03:59 GMT Received: from userv0121.oracle.com (userv0121.oracle.com [156.151.31.72]) by aserp3020.oracle.com with ESMTP id 35v1f8see1-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 05 Jan 2021 16:03:59 +0000 Received: from abhmp0016.oracle.com (abhmp0016.oracle.com [141.146.116.22]) by userv0121.oracle.com (8.14.4/8.13.8) with ESMTP id 105G3woQ015365; Tue, 5 Jan 2021 16:03:58 GMT Received: from ca-dev63.us.oracle.com (/10.211.8.221) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 05 Jan 2021 16:03:58 +0000 From: Steve Sistare To: kvm@vger.kernel.org Cc: Alex Williamson , Cornelia Huck , Kirti Wankhede , Steve Sistare Subject: [PATCH V1 2/5] vfio: option to unmap all Date: Tue, 5 Jan 2021 07:36:50 -0800 Message-Id: <1609861013-129801-3-git-send-email-steven.sistare@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1609861013-129801-1-git-send-email-steven.sistare@oracle.com> References: <1609861013-129801-1-git-send-email-steven.sistare@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9855 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 phishscore=0 suspectscore=0 spamscore=0 bulkscore=0 adultscore=0 mlxscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2101050099 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9855 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxscore=0 spamscore=0 malwarescore=0 phishscore=0 impostorscore=0 bulkscore=0 clxscore=1015 priorityscore=1501 lowpriorityscore=0 adultscore=0 suspectscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2101050099 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org For VFIO_IOMMU_UNMAP_DMA, delete all mappings if iova=0 and size=0. Signed-off-by: Steve Sistare --- drivers/vfio/vfio_iommu_type1.c | 11 ++++++++--- include/uapi/linux/vfio.h | 3 ++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 02228d0..3dc501d 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -1079,6 +1079,8 @@ static int vfio_dma_do_unmap(struct vfio_iommu *iommu, size_t unmapped = 0, pgsize; int ret = 0, retries = 0; unsigned long pgshift; + dma_addr_t iova; + unsigned long size; mutex_lock(&iommu->lock); @@ -1090,7 +1092,7 @@ static int vfio_dma_do_unmap(struct vfio_iommu *iommu, goto unlock; } - if (!unmap->size || unmap->size & (pgsize - 1)) { + if ((!unmap->size && unmap->iova) || unmap->size & (pgsize - 1)) { ret = -EINVAL; goto unlock; } @@ -1154,8 +1156,11 @@ static int vfio_dma_do_unmap(struct vfio_iommu *iommu, } } - while ((dma = vfio_find_dma(iommu, unmap->iova, unmap->size))) { - if (!iommu->v2 && unmap->iova > dma->iova) + iova = unmap->iova; + size = unmap->size ? unmap->size : SIZE_MAX; + + while ((dma = vfio_find_dma(iommu, iova, size))) { + if (!iommu->v2 && iova > dma->iova) break; /* * Task with same address space who mapped this iova range is diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h index 9204705..896e527 100644 --- a/include/uapi/linux/vfio.h +++ b/include/uapi/linux/vfio.h @@ -1073,7 +1073,8 @@ struct vfio_bitmap { * Caller sets argsz. The actual unmapped size is returned in the size * field. No guarantee is made to the user that arbitrary unmaps of iova * or size different from those used in the original mapping call will - * succeed. + * succeed. If iova=0 and size=0, all addresses are unmapped. + * * VFIO_DMA_UNMAP_FLAG_GET_DIRTY_BITMAP should be set to get the dirty bitmap * before unmapping IO virtual addresses. When this flag is set, the user must * provide a struct vfio_bitmap in data[]. User must provide zero-allocated From patchwork Tue Jan 5 15:36:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Sistare X-Patchwork-Id: 11999663 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=-19.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, UNPARSEABLE_RELAY,USER_AGENT_GIT 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 B25D3C433E9 for ; Tue, 5 Jan 2021 16:05:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 81DA222CA0 for ; Tue, 5 Jan 2021 16:05:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726920AbhAEQEp (ORCPT ); Tue, 5 Jan 2021 11:04:45 -0500 Received: from userp2130.oracle.com ([156.151.31.86]:45490 "EHLO userp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726655AbhAEQEo (ORCPT ); Tue, 5 Jan 2021 11:04:44 -0500 Received: from pps.filterd (userp2130.oracle.com [127.0.0.1]) by userp2130.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 105FtMF0137935; Tue, 5 Jan 2021 16:04:00 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references; s=corp-2020-01-29; bh=Ap/ouEGpUYNvnLEDenrFGt8SjrbOaQOwLg59rXOkBvA=; b=VWUUz670qgUe7xAphxfKJ3kUsffFOsm0egDol2u5IxBJPippZWjkQzcT1h7EZlC47kyC cbLl676rQydW3y0Uw1ZgC9mkCgk7ioXfkfUMfQR52f/MkLDgeJIQphKDXHDwu8ONhesS 7VwzWI/G61c+IxPU66jTLVKX7seE2kpv+FuTeOhYK1QB3AzSz7jAVDuObpNEhFrBLHEJ 2Sbx8iuiGCR69qa86uR+3kp86+1plEtRglFMIAgmypjBcQXjN1zPHLUrwyIZZ/UxsJIT J+C2BeRbJIKa6RLy6xt0q/C8/VcMi4vW9GH07fCxRVUQkyR130/vzivEg0t3fjT9+7xu xw== Received: from userp3030.oracle.com (userp3030.oracle.com [156.151.31.80]) by userp2130.oracle.com with ESMTP id 35tg8r1g6b-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Tue, 05 Jan 2021 16:04:00 +0000 Received: from pps.filterd (userp3030.oracle.com [127.0.0.1]) by userp3030.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 105FtLPk176029; Tue, 5 Jan 2021 16:04:00 GMT Received: from aserv0122.oracle.com (aserv0122.oracle.com [141.146.126.236]) by userp3030.oracle.com with ESMTP id 35vct61n95-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 05 Jan 2021 16:03:59 +0000 Received: from abhmp0016.oracle.com (abhmp0016.oracle.com [141.146.116.22]) by aserv0122.oracle.com (8.14.4/8.14.4) with ESMTP id 105G3wMw031362; Tue, 5 Jan 2021 16:03:58 GMT Received: from ca-dev63.us.oracle.com (/10.211.8.221) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 05 Jan 2021 16:03:58 +0000 From: Steve Sistare To: kvm@vger.kernel.org Cc: Alex Williamson , Cornelia Huck , Kirti Wankhede , Steve Sistare Subject: [PATCH V1 3/5] vfio: detect closed container Date: Tue, 5 Jan 2021 07:36:51 -0800 Message-Id: <1609861013-129801-4-git-send-email-steven.sistare@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1609861013-129801-1-git-send-email-steven.sistare@oracle.com> References: <1609861013-129801-1-git-send-email-steven.sistare@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9855 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 malwarescore=0 phishscore=0 bulkscore=0 mlxlogscore=999 suspectscore=0 mlxscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2101050099 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9855 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 clxscore=1015 phishscore=0 bulkscore=0 spamscore=0 impostorscore=0 suspectscore=0 adultscore=0 mlxlogscore=999 mlxscore=0 malwarescore=0 lowpriorityscore=0 priorityscore=1501 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2101050099 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Add a function that detects if an iommu_group has a valid container. Signed-off-by: Steve Sistare --- drivers/vfio/vfio.c | 12 ++++++++++++ include/linux/vfio.h | 1 + 2 files changed, 13 insertions(+) diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c index 262ab0e..f89ab80 100644 --- a/drivers/vfio/vfio.c +++ b/drivers/vfio/vfio.c @@ -61,6 +61,7 @@ struct vfio_container { struct vfio_iommu_driver *iommu_driver; void *iommu_data; bool noiommu; + bool closed; }; struct vfio_unbound_dev { @@ -1223,6 +1224,7 @@ static int vfio_fops_release(struct inode *inode, struct file *filep) filep->private_data = NULL; + container->closed = true; vfio_container_put(container); return 0; @@ -2216,6 +2218,16 @@ void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm) } EXPORT_SYMBOL_GPL(vfio_group_set_kvm); +bool vfio_iommu_group_contained(struct iommu_group *iommu_group) +{ + struct vfio_group *group = vfio_group_get_from_iommu(iommu_group); + bool ret = group && group->container && !group->container->closed; + + vfio_group_put(group); + return ret; +} +EXPORT_SYMBOL_GPL(vfio_iommu_group_contained); + static int vfio_register_group_notifier(struct vfio_group *group, unsigned long *events, struct notifier_block *nb) diff --git a/include/linux/vfio.h b/include/linux/vfio.h index 38d3c6a..b2724e7 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -148,6 +148,7 @@ extern int vfio_unregister_notifier(struct device *dev, struct kvm; extern void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm); +extern bool vfio_iommu_group_contained(struct iommu_group *group); /* * Sub-module helpers From patchwork Tue Jan 5 15:36:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Sistare X-Patchwork-Id: 11999667 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=-19.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, UNPARSEABLE_RELAY,USER_AGENT_GIT 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 E4805C433E0 for ; Tue, 5 Jan 2021 16:05:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B7F8822C9F for ; Tue, 5 Jan 2021 16:05:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728098AbhAEQEq (ORCPT ); Tue, 5 Jan 2021 11:04:46 -0500 Received: from userp2130.oracle.com ([156.151.31.86]:45500 "EHLO userp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726711AbhAEQEp (ORCPT ); Tue, 5 Jan 2021 11:04:45 -0500 Received: from pps.filterd (userp2130.oracle.com [127.0.0.1]) by userp2130.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 105Ftgdj138082; Tue, 5 Jan 2021 16:04:00 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references; s=corp-2020-01-29; bh=2nFsFxZoKMVelZhOQbExkZ51PmPGycR3uHNdk+4xWgw=; b=bNNwEKjXx5LZGVkNkPCVKhzMUuHn9mxFVBEYt4BFt+0w9pUOnebUGOK3bsxVQlsJSrHz qHVbn9hmF/F6B8MuCAA9PpsFjMlshshyh3JHD6soywp/gXMxlsBlVIVP0sGDO5fYyTIp C86l74J+JKEQV+0hEp3AuGr0wwXhAXlHuxmuqZrfBeFuzSVuehtUEcgzdfbF4eDsYgkD XTMreDDUcFObQUjNmohHwMga15m3B4IZ1/JK6KvukTFl8AbPHnSfqY4nvQLuR5V2z/bk N+MDiI4jkjWRm/ryPP1tIzaFVDMBp2SwXytcozKXXhWwmoSa1xSikj5nvqTjBc2LCOMt qw== Received: from aserp3030.oracle.com (aserp3030.oracle.com [141.146.126.71]) by userp2130.oracle.com with ESMTP id 35tg8r1g6d-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Tue, 05 Jan 2021 16:04:00 +0000 Received: from pps.filterd (aserp3030.oracle.com [127.0.0.1]) by aserp3030.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 105FuR3Q026734; Tue, 5 Jan 2021 16:04:00 GMT Received: from aserv0121.oracle.com (aserv0121.oracle.com [141.146.126.235]) by aserp3030.oracle.com with ESMTP id 35v4rbj5xk-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 05 Jan 2021 16:03:59 +0000 Received: from abhmp0016.oracle.com (abhmp0016.oracle.com [141.146.116.22]) by aserv0121.oracle.com (8.14.4/8.13.8) with ESMTP id 105G3xR5015510; Tue, 5 Jan 2021 16:03:59 GMT Received: from ca-dev63.us.oracle.com (/10.211.8.221) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 05 Jan 2021 16:03:59 +0000 From: Steve Sistare To: kvm@vger.kernel.org Cc: Alex Williamson , Cornelia Huck , Kirti Wankhede , Steve Sistare Subject: [PATCH V1 4/5] vfio: VA suspend interface Date: Tue, 5 Jan 2021 07:36:52 -0800 Message-Id: <1609861013-129801-5-git-send-email-steven.sistare@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1609861013-129801-1-git-send-email-steven.sistare@oracle.com> References: <1609861013-129801-1-git-send-email-steven.sistare@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9855 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 mlxscore=0 bulkscore=0 suspectscore=0 spamscore=0 adultscore=0 malwarescore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2101050099 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9855 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 clxscore=1015 phishscore=0 bulkscore=0 spamscore=0 impostorscore=0 suspectscore=0 adultscore=0 mlxlogscore=999 mlxscore=0 malwarescore=0 lowpriorityscore=0 priorityscore=1501 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2101050099 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Add interfaces that allow the underlying memory object of an iova range to be mapped to a new host virtual address in the host process: - VFIO_DMA_UNMAP_FLAG_SUSPEND for VFIO_IOMMU_UNMAP_DMA - VFIO_DMA_MAP_FLAG_RESUME flag for VFIO_IOMMU_MAP_DMA - VFIO_SUSPEND extension for VFIO_CHECK_EXTENSION The suspend interface blocks vfio translation of host virtual addresses in a range, but DMA to already-mapped pages continues. The resume interface records the new base VA and resumes translation. See comments in uapi/linux/vfio.h for more details. This is a partial implementation. Blocking is added in the next patch. Signed-off-by: Steve Sistare --- drivers/vfio/vfio_iommu_type1.c | 47 +++++++++++++++++++++++++++++++++++------ include/uapi/linux/vfio.h | 16 ++++++++++++++ 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 3dc501d..2c164a6 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -92,6 +92,7 @@ struct vfio_dma { int prot; /* IOMMU_READ/WRITE */ bool iommu_mapped; bool lock_cap; /* capable(CAP_IPC_LOCK) */ + bool suspended; struct task_struct *task; struct rb_root pfn_list; /* Ex-user pinned pfn list */ unsigned long *bitmap; @@ -1080,7 +1081,7 @@ static int vfio_dma_do_unmap(struct vfio_iommu *iommu, int ret = 0, retries = 0; unsigned long pgshift; dma_addr_t iova; - unsigned long size; + unsigned long size, consumed; mutex_lock(&iommu->lock); @@ -1169,6 +1170,21 @@ static int vfio_dma_do_unmap(struct vfio_iommu *iommu, if (dma->task->mm != current->mm) break; + if (unmap->flags & VFIO_DMA_UNMAP_FLAG_SUSPEND) { + if (dma->suspended) { + ret = -EINVAL; + goto unlock; + } + dma->suspended = true; + consumed = dma->iova + dma->size - iova; + if (consumed >= size) + break; + iova += consumed; + size -= consumed; + unmapped += dma->size; + continue; + } + if (!RB_EMPTY_ROOT(&dma->pfn_list)) { struct vfio_iommu_type1_dma_unmap nb_unmap; @@ -1307,6 +1323,7 @@ static bool vfio_iommu_iova_dma_valid(struct vfio_iommu *iommu, static int vfio_dma_do_map(struct vfio_iommu *iommu, struct vfio_iommu_type1_dma_map *map) { + bool resume = map->flags & VFIO_DMA_MAP_FLAG_RESUME; dma_addr_t iova = map->iova; unsigned long vaddr = map->vaddr; size_t size = map->size; @@ -1324,13 +1341,16 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu, if (map->flags & VFIO_DMA_MAP_FLAG_READ) prot |= IOMMU_READ; + if ((prot && resume) || (!prot && !resume)) + return -EINVAL; + mutex_lock(&iommu->lock); pgsize = (size_t)1 << __ffs(iommu->pgsize_bitmap); WARN_ON((pgsize - 1) & PAGE_MASK); - if (!prot || !size || (size | iova | vaddr) & (pgsize - 1)) { + if (!size || (size | iova | vaddr) & (pgsize - 1)) { ret = -EINVAL; goto out_unlock; } @@ -1341,7 +1361,19 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu, goto out_unlock; } - if (vfio_find_dma(iommu, iova, size)) { + dma = vfio_find_dma(iommu, iova, size); + if (resume) { + if (!dma) { + ret = -ENOENT; + } else if (!dma->suspended || dma->iova != iova || + dma->size != size) { + ret = -EINVAL; + } else { + dma->vaddr = vaddr; + dma->suspended = false; + } + goto out_unlock; + } else if (dma) { ret = -EEXIST; goto out_unlock; } @@ -2532,6 +2564,7 @@ static int vfio_iommu_type1_check_extension(struct vfio_iommu *iommu, case VFIO_TYPE1_IOMMU: case VFIO_TYPE1v2_IOMMU: case VFIO_TYPE1_NESTING_IOMMU: + case VFIO_SUSPEND: return 1; case VFIO_DMA_CC_IOMMU: if (!iommu) @@ -2686,7 +2719,8 @@ static int vfio_iommu_type1_map_dma(struct vfio_iommu *iommu, { struct vfio_iommu_type1_dma_map map; unsigned long minsz; - uint32_t mask = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE; + uint32_t mask = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE | + VFIO_DMA_MAP_FLAG_RESUME; minsz = offsetofend(struct vfio_iommu_type1_dma_map, size); @@ -2704,6 +2738,8 @@ static int vfio_iommu_type1_unmap_dma(struct vfio_iommu *iommu, { struct vfio_iommu_type1_dma_unmap unmap; struct vfio_bitmap bitmap = { 0 }; + uint32_t mask = VFIO_DMA_UNMAP_FLAG_GET_DIRTY_BITMAP | + VFIO_DMA_UNMAP_FLAG_SUSPEND; unsigned long minsz; int ret; @@ -2712,8 +2748,7 @@ static int vfio_iommu_type1_unmap_dma(struct vfio_iommu *iommu, if (copy_from_user(&unmap, (void __user *)arg, minsz)) return -EFAULT; - if (unmap.argsz < minsz || - unmap.flags & ~VFIO_DMA_UNMAP_FLAG_GET_DIRTY_BITMAP) + if (unmap.argsz < minsz || unmap.flags & ~mask || unmap.flags == mask) return -EINVAL; if (unmap.flags & VFIO_DMA_UNMAP_FLAG_GET_DIRTY_BITMAP) { diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h index 896e527..fcf7b56 100644 --- a/include/uapi/linux/vfio.h +++ b/include/uapi/linux/vfio.h @@ -46,6 +46,9 @@ */ #define VFIO_NOIOMMU_IOMMU 8 +/* Supports VFIO DMA suspend and resume */ +#define VFIO_SUSPEND 9 + /* * The IOCTL interface is designed for extensibility by embedding the * structure length (argsz) and flags into structures passed between @@ -1046,12 +1049,19 @@ struct vfio_iommu_type1_info_cap_migration { * * Map process virtual addresses to IO virtual addresses using the * provided struct vfio_dma_map. Caller sets argsz. READ &/ WRITE required. + * + * If flags & VFIO_DMA_MAP_FLAG_RESUME, record the new base vaddr for iova, and + * resume translation of host virtual addresses in the iova range. The new + * vaddr must point to the same memory object as the old vaddr, but this is not + * verified. iova and size must match those in the original MAP_DMA call. + * Protection is not changed, and the READ & WRITE flags must be 0. */ struct vfio_iommu_type1_dma_map { __u32 argsz; __u32 flags; #define VFIO_DMA_MAP_FLAG_READ (1 << 0) /* readable from device */ #define VFIO_DMA_MAP_FLAG_WRITE (1 << 1) /* writable from device */ +#define VFIO_DMA_MAP_FLAG_RESUME (1 << 2) __u64 vaddr; /* Process virtual address */ __u64 iova; /* IO virtual address */ __u64 size; /* Size of mapping (bytes) */ @@ -1084,11 +1094,17 @@ struct vfio_bitmap { * indicates that the page at that offset from iova is dirty. A Bitmap of the * pages in the range of unmapped size is returned in the user-provided * vfio_bitmap.data. + * + * If flags & VFIO_DMA_UNMAP_FLAG_SUSPEND, do not unmap, but suspend vfio + * translation of host virtual addresses in the iova range. During suspension, + * kernel threads that attempt to translate will block. DMA to already-mapped + * pages continues. */ struct vfio_iommu_type1_dma_unmap { __u32 argsz; __u32 flags; #define VFIO_DMA_UNMAP_FLAG_GET_DIRTY_BITMAP (1 << 0) +#define VFIO_DMA_UNMAP_FLAG_SUSPEND (1 << 1) __u64 iova; /* IO virtual address */ __u64 size; /* Size of mapping (bytes) */ __u8 data[]; From patchwork Tue Jan 5 15:36:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Sistare X-Patchwork-Id: 11999669 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=-19.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, UNPARSEABLE_RELAY,USER_AGENT_GIT 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 0C388C4332D for ; Tue, 5 Jan 2021 16:05:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CBC7922C9D for ; Tue, 5 Jan 2021 16:05:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728329AbhAEQEr (ORCPT ); Tue, 5 Jan 2021 11:04:47 -0500 Received: from userp2130.oracle.com ([156.151.31.86]:45698 "EHLO userp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728287AbhAEQEr (ORCPT ); Tue, 5 Jan 2021 11:04:47 -0500 Received: from pps.filterd (userp2130.oracle.com [127.0.0.1]) by userp2130.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 105Fu1f0139639; Tue, 5 Jan 2021 16:04:01 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references; s=corp-2020-01-29; bh=RKOrHkYm9qF1Xiebh709uEbj8RGTJ9WzcPbpIiekVWU=; b=DLQYUGpn+pzDoPebRXgcHNGolavWXcThn9H9/xlchtr0Ww0D6txS0lUI69J/m0NvdDx6 KD68F8SC30VWJ2qKTjMSAaeAIQ+p9ZQQVtwaU3tfNHMg4klYXCqDYTAU0lkhtjiApkgB fYf7S3+256CAkk90+pMdgPHHuzqzJrQXmktgveAO8294ZarbJ9Tn+vyI7/azC/zmqbGy kfNl+S+8hV/2yR4u4J1EAudWsqCakysUzsbsxM/tw0lgxdN3+L9d4BXd+qzU1pS8rBnw TeBAohMPfgMTtL+/rLuRPz13miMX1IZWGnKOWMch0oGjfpsbybyEnSYp2Sj+2+Zs8Ljl Pg== Received: from aserp3030.oracle.com (aserp3030.oracle.com [141.146.126.71]) by userp2130.oracle.com with ESMTP id 35tg8r1g6k-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Tue, 05 Jan 2021 16:04:01 +0000 Received: from pps.filterd (aserp3030.oracle.com [127.0.0.1]) by aserp3030.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 105FuRY7026699; Tue, 5 Jan 2021 16:04:01 GMT Received: from userv0122.oracle.com (userv0122.oracle.com [156.151.31.75]) by aserp3030.oracle.com with ESMTP id 35v4rbj5yp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 05 Jan 2021 16:04:01 +0000 Received: from abhmp0016.oracle.com (abhmp0016.oracle.com [141.146.116.22]) by userv0122.oracle.com (8.14.4/8.14.4) with ESMTP id 105G3x1S017635; Tue, 5 Jan 2021 16:03:59 GMT Received: from ca-dev63.us.oracle.com (/10.211.8.221) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 05 Jan 2021 16:03:59 +0000 From: Steve Sistare To: kvm@vger.kernel.org Cc: Alex Williamson , Cornelia Huck , Kirti Wankhede , Steve Sistare Subject: [PATCH V1 5/5] vfio: block during VA suspend Date: Tue, 5 Jan 2021 07:36:53 -0800 Message-Id: <1609861013-129801-6-git-send-email-steven.sistare@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1609861013-129801-1-git-send-email-steven.sistare@oracle.com> References: <1609861013-129801-1-git-send-email-steven.sistare@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9855 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 mlxscore=0 bulkscore=0 suspectscore=0 spamscore=0 adultscore=0 malwarescore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2101050099 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9855 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 clxscore=1015 phishscore=0 bulkscore=0 spamscore=0 impostorscore=0 suspectscore=0 adultscore=0 mlxlogscore=999 mlxscore=0 malwarescore=0 lowpriorityscore=0 priorityscore=1501 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2101050099 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Block translation of host virtual address while an iova range is suspended. Signed-off-by: Steve Sistare Reported-by: kernel test robot Reported-by: kernel test robot --- drivers/vfio/vfio_iommu_type1.c | 48 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 2c164a6..8035b9a 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -31,6 +31,8 @@ #include #include #include +#include +#include #include #include #include @@ -484,6 +486,34 @@ static int vaddr_get_pfn(struct mm_struct *mm, unsigned long vaddr, return ret; } +static bool vfio_iommu_contained(struct vfio_iommu *iommu) +{ + struct vfio_domain *domain = iommu->external_domain; + struct vfio_group *group; + + if (!domain) + domain = list_first_entry(&iommu->domain_list, + struct vfio_domain, next); + + group = list_first_entry(&domain->group_list, struct vfio_group, next); + return vfio_iommu_group_contained(group->iommu_group); +} + + +bool vfio_vaddr_valid(struct vfio_iommu *iommu, struct vfio_dma *dma) +{ + while (dma->suspended) { + mutex_unlock(&iommu->lock); + msleep_interruptible(10); + mutex_lock(&iommu->lock); + if (kthread_should_stop() || !vfio_iommu_contained(iommu) || + fatal_signal_pending(current)) { + return false; + } + } + return true; +} + /* * Attempt to pin pages. We really don't want to track all the pfns and * the iommu can only map chunks of consecutive pfns anyway, so get the @@ -690,6 +720,11 @@ static int vfio_iommu_type1_pin_pages(void *iommu_data, continue; } + if (!vfio_vaddr_valid(iommu, dma)) { + ret = -EFAULT; + goto pin_unwind; + } + remote_vaddr = dma->vaddr + (iova - dma->iova); ret = vfio_pin_page_external(dma, remote_vaddr, &phys_pfn[i], do_accounting); @@ -1514,12 +1549,16 @@ static int vfio_iommu_replay(struct vfio_iommu *iommu, i += PAGE_SIZE; } } else { - unsigned long pfn; - unsigned long vaddr = dma->vaddr + - (iova - dma->iova); + unsigned long pfn, vaddr; size_t n = dma->iova + dma->size - iova; long npage; + if (!vfio_vaddr_valid(iommu, dma)) { + ret = -EFAULT; + goto unwind; + } + vaddr = dma->vaddr + (iova - dma->iova); + npage = vfio_pin_pages_remote(dma, vaddr, n >> PAGE_SHIFT, &pfn, limit); @@ -2965,6 +3004,9 @@ static int vfio_iommu_type1_dma_rw_chunk(struct vfio_iommu *iommu, if (count > dma->size - offset) count = dma->size - offset; + if (!vfio_vaddr_valid(iommu, dma)) + goto out; + vaddr = dma->vaddr + offset; if (write) {