Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp454980pxa; Tue, 4 Aug 2020 09:25:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy7OKw1m2TOUy5NAj4wvOgSgdSxwCT1+a8GYxzeabgChpwPhnW1A4SGuggHCE01HN1VT3iK X-Received: by 2002:a17:906:444e:: with SMTP id i14mr21448655ejp.418.1596558313675; Tue, 04 Aug 2020 09:25:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1596558313; cv=none; d=google.com; s=arc-20160816; b=qawrfYJw752LQiY0fMpwOzTAVJnYjlOnictm0A3LHlS9LZx3nOcQ9eUW5az75vNLkw jAz7geQjOt2hCUbiHrfh/f3QW1ccYAnphARLE7kBoQ7lq30vs2do55di0R9/yDesTLHT Pr7rJ3UhsW3bKDCZWtnZBUF/wqq/L4ZRw1kkS7Wrpix0TkGHxESEfeT8X0nYUWuU9qsT URl8/G1XSj4o3EbyBm0uRYq+rZb3SXfyYuN0EkHwXMx3YT0bMPVtedO8Sikn/ryIiBtY NV5hIYjol6zGP5O3GffVtITPjFpmQQ4D1jq8p0VW7FJS7gZW69DqjSfS1vCpXMvFDa0U tpBg== 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; bh=8p6tdy2NxeOKlQvNvnDm4Euao5aBAOJactQ5LW0lXFA=; b=AKvIEtav/utbFQjGT7F4ZM7Z8LryKOHgE/DQ61uhLwB9aTwhbs4GnEbUc4BvGcfawZ 6Zz8Cge17iS5dpHJIltK6iNtI4DLyhXy8C9cmn+taaZ9DYhc7iT4iUxoetNfEL5jYeOb CX+bQvHCx5hn++wwCchYhd0I5Bp1lDNxcbBqdWqzlEqh2tDmiuGtAb76q6KQep9Xb1ix imxFhhGUwU2ubZ+VYMK1W3kKJfSFR9J7kQDAaki/pc47pnRi3cJONa08P3L244SVISe8 93g58PbyuMDcfBDp82ohjc8v9looCoMO1teuY/j6JElt/2/TWu9nWuTRSKO1s94PlTRp 24zQ== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=mellanox.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n9si7813011ejh.45.2020.08.04.09.24.46; Tue, 04 Aug 2020 09:25:13 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=mellanox.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729838AbgHDQX4 (ORCPT + 99 others); Tue, 4 Aug 2020 12:23:56 -0400 Received: from mail-il-dmz.mellanox.com ([193.47.165.129]:43881 "EHLO mellanox.co.il" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1729615AbgHDQU7 (ORCPT ); Tue, 4 Aug 2020 12:20:59 -0400 Received: from Internal Mail-Server by MTLPINE1 (envelope-from eli@mellanox.com) with SMTP; 4 Aug 2020 19:20:52 +0300 Received: from nps-server-21.mtl.labs.mlnx (nps-server-21.mtl.labs.mlnx [10.237.240.120]) by labmailer.mlnx (8.13.8/8.13.8) with ESMTP id 074GKqCp024004; Tue, 4 Aug 2020 19:20:52 +0300 Received: from nps-server-21.mtl.labs.mlnx (localhost [127.0.0.1]) by nps-server-21.mtl.labs.mlnx (8.14.7/8.14.7) with ESMTP id 074GKqrp022641; Tue, 4 Aug 2020 19:20:52 +0300 Received: (from eli@localhost) by nps-server-21.mtl.labs.mlnx (8.14.7/8.14.7/Submit) id 074GKqZ0022640; Tue, 4 Aug 2020 19:20:52 +0300 From: Eli Cohen To: mst@redhat.com, jasowang@redhat.com, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org Cc: shahafs@mellanox.com, saeedm@mellanox.com, parav@mellanox.com Subject: [PATCH V4 linux-next 04/12] vhost-vdpa: support IOTLB batching hints Date: Tue, 4 Aug 2020 19:20:40 +0300 Message-Id: <20200804162048.22587-5-eli@mellanox.com> X-Mailer: git-send-email 2.26.0 In-Reply-To: <20200804162048.22587-1-eli@mellanox.com> References: <20200804162048.22587-1-eli@mellanox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jason Wang This patches extend the vhost IOTLB API to accept batch updating hints form userspace. When userspace wants update the device IOTLB in a batch, it may do: 1) Write vhost_iotlb_msg with VHOST_IOTLB_BATCH_BEGIN flag 2) Perform a batch of IOTLB updating via VHOST_IOTLB_UPDATE/INVALIDATE 3) Write vhost_iotlb_msg with VHOST_IOTLB_BATCH_END flag Vhost-vdpa may decide to batch the IOMMU/IOTLB updating in step 3 when vDPA device support set_map() ops. This is useful for the vDPA device that want to know all the mappings to tweak their own DMA translation logic. For vDPA device that doesn't require set_map(), no behavior changes. This capability is advertised via VHOST_BACKEND_F_IOTLB_BATCH capability. Signed-off-by: Jason Wang --- drivers/vhost/vdpa.c | 36 ++++++++++++++++++++++++-------- include/uapi/linux/vhost.h | 2 ++ include/uapi/linux/vhost_types.h | 11 ++++++++++ 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c index 2f0310d1b914..0a143615e69d 100644 --- a/drivers/vhost/vdpa.c +++ b/drivers/vhost/vdpa.c @@ -27,7 +27,9 @@ #include "vhost.h" enum { - VHOST_VDPA_BACKEND_FEATURES = (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2) + VHOST_VDPA_BACKEND_FEATURES = + (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2) | + (1ULL << VHOST_BACKEND_F_IOTLB_BATCH), }; /* Currently, only network backend w/o multiqueue is supported. */ @@ -48,6 +50,7 @@ struct vhost_vdpa { int virtio_id; int minor; struct eventfd_ctx *config_ctx; + int in_batch; }; static DEFINE_IDA(vhost_vdpa_ida); @@ -140,6 +143,7 @@ static void vhost_vdpa_reset(struct vhost_vdpa *v) struct vdpa_device *vdpa = v->vdpa; vdpa_reset(vdpa); + v->in_batch = 0; } static long vhost_vdpa_get_device_id(struct vhost_vdpa *v, u8 __user *argp) @@ -563,13 +567,15 @@ static int vhost_vdpa_map(struct vhost_vdpa *v, if (r) return r; - if (ops->dma_map) + if (ops->dma_map) { r = ops->dma_map(vdpa, iova, size, pa, perm); - else if (ops->set_map) - r = ops->set_map(vdpa, dev->iotlb); - else + } else if (ops->set_map) { + if (!v->in_batch) + r = ops->set_map(vdpa, dev->iotlb); + } else { r = iommu_map(v->domain, iova, pa, size, perm_to_iommu_flags(perm)); + } return r; } @@ -582,12 +588,14 @@ static void vhost_vdpa_unmap(struct vhost_vdpa *v, u64 iova, u64 size) vhost_vdpa_iotlb_unmap(v, iova, iova + size - 1); - if (ops->dma_map) + if (ops->dma_map) { ops->dma_unmap(vdpa, iova, size); - else if (ops->set_map) - ops->set_map(vdpa, dev->iotlb); - else + } else if (ops->set_map) { + if (!v->in_batch) + ops->set_map(vdpa, dev->iotlb); + } else { iommu_unmap(v->domain, iova, size); + } } static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v, @@ -680,6 +688,8 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev, struct vhost_iotlb_msg *msg) { struct vhost_vdpa *v = container_of(dev, struct vhost_vdpa, vdev); + struct vdpa_device *vdpa = v->vdpa; + const struct vdpa_config_ops *ops = vdpa->config; int r = 0; r = vhost_dev_check_owner(dev); @@ -693,6 +703,14 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev, case VHOST_IOTLB_INVALIDATE: vhost_vdpa_unmap(v, msg->iova, msg->size); break; + case VHOST_IOTLB_BATCH_BEGIN: + v->in_batch = true; + break; + case VHOST_IOTLB_BATCH_END: + if (v->in_batch && ops->set_map) + ops->set_map(vdpa, dev->iotlb); + v->in_batch = false; + break; default: r = -EINVAL; break; diff --git a/include/uapi/linux/vhost.h b/include/uapi/linux/vhost.h index 5d9254e2a6b6..11a4948b6216 100644 --- a/include/uapi/linux/vhost.h +++ b/include/uapi/linux/vhost.h @@ -91,6 +91,8 @@ /* Use message type V2 */ #define VHOST_BACKEND_F_IOTLB_MSG_V2 0x1 +/* IOTLB can accept batching hints */ +#define VHOST_BACKEND_F_IOTLB_BATCH 0x2 #define VHOST_SET_BACKEND_FEATURES _IOW(VHOST_VIRTIO, 0x25, __u64) #define VHOST_GET_BACKEND_FEATURES _IOR(VHOST_VIRTIO, 0x26, __u64) diff --git a/include/uapi/linux/vhost_types.h b/include/uapi/linux/vhost_types.h index 669457ce5c48..9a269a88a6ff 100644 --- a/include/uapi/linux/vhost_types.h +++ b/include/uapi/linux/vhost_types.h @@ -60,6 +60,17 @@ struct vhost_iotlb_msg { #define VHOST_IOTLB_UPDATE 2 #define VHOST_IOTLB_INVALIDATE 3 #define VHOST_IOTLB_ACCESS_FAIL 4 +/* + * VHOST_IOTLB_BATCH_BEGIN and VHOST_IOTLB_BATCH_END allow modifying + * multiple mappings in one go: beginning with + * VHOST_IOTLB_BATCH_BEGIN, followed by any number of + * VHOST_IOTLB_UPDATE messages, and ending with VHOST_IOTLB_BATCH_END. + * When one of these two values is used as the message type, the rest + * of the fields in the message are ignored. There's no guarantee that + * these changes take place automatically in the device. + */ +#define VHOST_IOTLB_BATCH_BEGIN 5 +#define VHOST_IOTLB_BATCH_END 6 __u8 type; }; -- 2.26.0