From patchwork Mon Nov 16 11:36:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxime Coquelin X-Patchwork-Id: 84235 X-Patchwork-Delegate: maxime.coquelin@redhat.com Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id B4672A04B5; Mon, 16 Nov 2020 12:37:29 +0100 (CET) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id E43CCC908; Mon, 16 Nov 2020 12:36:37 +0100 (CET) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by dpdk.org (Postfix) with ESMTP id 84212C8F2 for ; Mon, 16 Nov 2020 12:36:35 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1605526594; 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=F5WMdC+K8059Fla0tri3lmWZe7q14vvDB52reNpSVYA=; b=axT14CdZEtl3uc80VG6tqqLDnFYby/TPmawKxH/BLOrqKUD0gT+Uv2r92byRAkaRoOP+3R uNCUh18SGtXKa4Sx+gbmVDubQRlhibG4tsTD15ThkotY8xjE7n6qmnKeofXvhjaGVO2nu0 Y4TWXFe468yQWiz9kZC0blF3kzVSJqc= 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-235-DLyFh5GcM9a-0BmJ12jpvA-1; Mon, 16 Nov 2020 06:36:31 -0500 X-MC-Unique: DLyFh5GcM9a-0BmJ12jpvA-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id BAA541074666; Mon, 16 Nov 2020 11:36:30 +0000 (UTC) Received: from localhost.localdomain (unknown [10.36.110.32]) by smtp.corp.redhat.com (Postfix) with ESMTP id A134E5DA30; Mon, 16 Nov 2020 11:36:29 +0000 (UTC) From: Maxime Coquelin To: dev@dpdk.org, chenbo.xia@intel.com, xuan.ding@intel.com Cc: Maxime Coquelin Date: Mon, 16 Nov 2020 12:36:20 +0100 Message-Id: <20201116113620.587073-4-maxime.coquelin@redhat.com> In-Reply-To: <20201116113620.587073-1-maxime.coquelin@redhat.com> References: <20201116113620.587073-1-maxime.coquelin@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=maxime.coquelin@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [dpdk-dev] [PATCH 21.02 3/3] vhost: refactor memory regions mapping X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" This patch moves memory region mmaping and related preparation in a dedicated function in order to simplify VHOST_USER_SET_MEM_TABLE request handling function. Signed-off-by: Maxime Coquelin Reviewed-by: Chenbo Xia --- lib/librte_vhost/vhost_user.c | 178 ++++++++++++++++++---------------- 1 file changed, 94 insertions(+), 84 deletions(-) diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c index 2ee22ef76d..24109bd4c1 100644 --- a/lib/librte_vhost/vhost_user.c +++ b/lib/librte_vhost/vhost_user.c @@ -1097,6 +1097,96 @@ vhost_user_postcopy_register(struct virtio_net *dev, int main_fd, return 0; } +static int +vhost_user_mmap_region(struct virtio_net *dev, + struct rte_vhost_mem_region *region, + uint64_t mmap_offset) +{ + void *mmap_addr; + uint64_t mmap_size; + uint64_t alignment; + int populate; + + /* Check for memory_size + mmap_offset overflow */ + if (mmap_offset >= -region->size) { + VHOST_LOG_CONFIG(ERR, + "mmap_offset (%#"PRIx64") and memory_size " + "(%#"PRIx64") overflow\n", + mmap_offset, region->size); + return -1; + } + + mmap_size = region->size + mmap_offset; + + /* mmap() without flag of MAP_ANONYMOUS, should be called with length + * argument aligned with hugepagesz at older longterm version Linux, + * like 2.6.32 and 3.2.72, or mmap() will fail with EINVAL. + * + * To avoid failure, make sure in caller to keep length aligned. + */ + alignment = get_blk_size(region->fd); + if (alignment == (uint64_t)-1) { + VHOST_LOG_CONFIG(ERR, + "couldn't get hugepage size through fstat\n"); + return -1; + } + mmap_size = RTE_ALIGN_CEIL(mmap_size, alignment); + if (mmap_size == 0) { + /* + * It could happen if initial mmap_size + alignment overflows + * the sizeof uint64, which could happen if either mmap_size or + * alignment value is wrong. + * + * mmap() kernel implementation would return an error, but + * better catch it before and provide useful info in the logs. + */ + VHOST_LOG_CONFIG(ERR, "mmap size (0x%" PRIx64 ") " + "or alignment (0x%" PRIx64 ") is invalid\n", + region->size + mmap_offset, alignment); + return -1; + } + + populate = dev->async_copy ? MAP_POPULATE : 0; + mmap_addr = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, + MAP_SHARED | populate, region->fd, 0); + + if (mmap_addr == MAP_FAILED) { + VHOST_LOG_CONFIG(ERR, "mmap failed (%s).\n", strerror(errno)); + return -1; + } + + region->mmap_addr = mmap_addr; + region->mmap_size = mmap_size; + region->host_user_addr = (uint64_t)(uintptr_t)mmap_addr + mmap_offset; + + if (dev->async_copy) + if (add_guest_pages(dev, region, alignment) < 0) { + VHOST_LOG_CONFIG(ERR, + "adding guest pages to region failed.\n"); + return -1; + } + + VHOST_LOG_CONFIG(INFO, + "guest memory region size: 0x%" PRIx64 "\n" + "\t guest physical addr: 0x%" PRIx64 "\n" + "\t guest virtual addr: 0x%" PRIx64 "\n" + "\t host virtual addr: 0x%" PRIx64 "\n" + "\t mmap addr : 0x%" PRIx64 "\n" + "\t mmap size : 0x%" PRIx64 "\n" + "\t mmap align: 0x%" PRIx64 "\n" + "\t mmap off : 0x%" PRIx64 "\n", + region->size, + region->guest_phys_addr, + region->guest_user_addr, + region->host_user_addr, + (uint64_t)(uintptr_t)mmap_addr, + mmap_size, + alignment, + mmap_offset); + + return 0; +} + static int vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg, int main_fd) @@ -1104,12 +1194,9 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg, struct virtio_net *dev = *pdev; struct VhostUserMemory *memory = &msg->payload.memory; struct rte_vhost_mem_region *reg; - void *mmap_addr; - uint64_t mmap_size; + uint64_t mmap_offset; - uint64_t alignment; uint32_t i; - int populate; if (validate_msg_fds(msg, memory->nregions) != 0) return RTE_VHOST_MSG_RESULT_ERR; @@ -1171,7 +1258,6 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg, dev->vid); goto free_guest_pages; } - dev->mem->nregions = memory->nregions; for (i = 0; i < memory->nregions; i++) { reg = &dev->mem->regions[i]; @@ -1189,88 +1275,12 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg, mmap_offset = memory->regions[i].mmap_offset; - /* Check for memory_size + mmap_offset overflow */ - if (mmap_offset >= -reg->size) { - VHOST_LOG_CONFIG(ERR, - "mmap_offset (%#"PRIx64") and memory_size " - "(%#"PRIx64") overflow\n", - mmap_offset, reg->size); - goto free_mem_table; - } - - mmap_size = reg->size + mmap_offset; - - /* mmap() without flag of MAP_ANONYMOUS, should be called - * with length argument aligned with hugepagesz at older - * longterm version Linux, like 2.6.32 and 3.2.72, or - * mmap() will fail with EINVAL. - * - * to avoid failure, make sure in caller to keep length - * aligned. - */ - alignment = get_blk_size(reg->fd); - if (alignment == (uint64_t)-1) { - VHOST_LOG_CONFIG(ERR, - "couldn't get hugepage size through fstat\n"); - goto free_mem_table; - } - mmap_size = RTE_ALIGN_CEIL(mmap_size, alignment); - if (mmap_size == 0) { - /* - * It could happen if initial mmap_size + alignment - * overflows the sizeof uint64, which could happen if - * either mmap_size or alignment value is wrong. - * - * mmap() kernel implementation would return an error, - * but better catch it before and provide useful info - * in the logs. - */ - VHOST_LOG_CONFIG(ERR, "mmap size (0x%" PRIx64 ") " - "or alignment (0x%" PRIx64 ") is invalid\n", - reg->size + mmap_offset, alignment); - goto free_mem_table; - } - - populate = dev->async_copy ? MAP_POPULATE : 0; - mmap_addr = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, - MAP_SHARED | populate, reg->fd, 0); - - if (mmap_addr == MAP_FAILED) { - VHOST_LOG_CONFIG(ERR, - "mmap region %u failed.\n", i); + if (vhost_user_mmap_region(dev, reg, mmap_offset) < 0) { + VHOST_LOG_CONFIG(ERR, "Failed to mmap region %u\n", i); goto free_mem_table; } - reg->mmap_addr = mmap_addr; - reg->mmap_size = mmap_size; - reg->host_user_addr = (uint64_t)(uintptr_t)mmap_addr + - mmap_offset; - - if (dev->async_copy) - if (add_guest_pages(dev, reg, alignment) < 0) { - VHOST_LOG_CONFIG(ERR, - "adding guest pages to region %u failed.\n", - i); - goto free_mem_table; - } - - VHOST_LOG_CONFIG(INFO, - "guest memory region %u, size: 0x%" PRIx64 "\n" - "\t guest physical addr: 0x%" PRIx64 "\n" - "\t guest virtual addr: 0x%" PRIx64 "\n" - "\t host virtual addr: 0x%" PRIx64 "\n" - "\t mmap addr : 0x%" PRIx64 "\n" - "\t mmap size : 0x%" PRIx64 "\n" - "\t mmap align: 0x%" PRIx64 "\n" - "\t mmap off : 0x%" PRIx64 "\n", - i, reg->size, - reg->guest_phys_addr, - reg->guest_user_addr, - reg->host_user_addr, - (uint64_t)(uintptr_t)mmap_addr, - mmap_size, - alignment, - mmap_offset); + dev->mem->nregions++; } if (vhost_user_postcopy_register(dev, main_fd, msg) < 0)