From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id 06F17A052A; Fri, 10 Jul 2020 22:40:47 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id A6DBC1DE49; Fri, 10 Jul 2020 22:40:46 +0200 (CEST) Received: from new1-smtp.messagingengine.com (new1-smtp.messagingengine.com [66.111.4.221]) by dpdk.org (Postfix) with ESMTP id 51B051DE21 for ; Fri, 10 Jul 2020 22:40:43 +0200 (CEST) Received: from compute7.internal (compute7.nyi.internal [10.202.2.47]) by mailnew.nyi.internal (Postfix) with ESMTP id E4D48580473; Fri, 10 Jul 2020 16:40:42 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute7.internal (MEProxy); Fri, 10 Jul 2020 16:40:42 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=monjalon.net; h= from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; s=fm1; bh=sx0iGa5Aacvbv tvuj6hc7JFBY2C+er9WRyiR9gRNtlU=; b=wVZ1ItsJ25UYqoF94x/v44aHs3Gzm MqD8x289sknt9xW39lySVFJN1VK/isBogdUVWsjRbvf6Y9rF4ZDgcak/ByliMwF/ so4W5blgWaC+qOPi4DRF84ee+JtxfDtIFf7FnrUYm2ZDpJZYk7VLJMHBBUmnkcY8 PgT14YnmF8RIaqVCmPS5Qeu+1f7ywhq5ORD6EQJwnaMw1CgoJQBTnzUKZrZEICGg 6fgRCu2gyDDYIJtvabBLC9cJw2LU9EH48IJqXyb64JI9QmbB3okq8Wnuw6YfAr+V K3GpapOdRpbRJ6DTLCfvZfAT+dolVwIdEU6KwyOY7OXMHjZ/pIxZb3LUw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:date:from :in-reply-to:message-id:mime-version:references:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; bh=sx0iGa5Aacvbvtvuj6hc7JFBY2C+er9WRyiR9gRNtlU=; b=OC6JzCum P/52mx3NjIaFtifug36k3VsPd5sKqEd1hUNDHY8GP3aRYUXdOHQ3xs9PMQxGmukB 0+tj9gHZfRBVXrB46182z5AXFG3QHEvKTAFlq4qTHxo9My4QsTJx0I0ZiPngqWg7 ts3Lh2wuj2jf+YxsfTlFNDhC2mOF/pa1yep/DgIxchIzDqmDyMHd2DpGQ2FR1yBX eBlYAxzcLOVrFmuzK+rq6coAN1tRRHEKXWyAVSkFSBc+bpnHqxmZfkarK49mkWzW CHSmXjNkN09oURlGFaAKJRnGMgqOPXxwc+322Ugzpox+QcIvTCNUQZRw+IhcTVXL esSe0hZFOB4ivw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduiedrvddugdduheejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefhvffufffkofgjfhgggfestdekre dtredttdenucfhrhhomhepvfhhohhmrghsucfoohhnjhgrlhhonhcuoehthhhomhgrshes mhhonhhjrghlohhnrdhnvghtqeenucggtffrrghtthgvrhhnpedvhefgiedvjeegtdevhe efhfetleefgfeivefgffevfeejgedtgfeutdehtdegveenucfkphepjeejrddufeegrddv tdefrddukeegnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrh homhepthhhohhmrghssehmohhnjhgrlhhonhdrnhgvth X-ME-Proxy: Received: from xps.monjalon.net (184.203.134.77.rev.sfr.net [77.134.203.184]) by mail.messagingengine.com (Postfix) with ESMTPA id BFE6B30600A6; Fri, 10 Jul 2020 16:40:40 -0400 (EDT) From: Thomas Monjalon To: dev@dpdk.org Cc: david.marchand@redhat.com, ferruh.yigit@intel.com, grive@u256.net, alvinx.zhang@intel.com, beilei.xing@intel.com, jia.guo@intel.com, anatoly.burakov@intel.com, bruce.richardson@intel.com, dmitry.kozliuk@gmail.com, navasile@linux.microsoft.com, dmitrym@microsoft.com, pallavi.kadam@intel.com, talshn@mellanox.com Date: Fri, 10 Jul 2020 22:40:34 +0200 Message-Id: <20200710204034.4158330-1-thomas@monjalon.net> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200710115324.3902559-1-thomas@monjalon.net> References: <20200710115324.3902559-1-thomas@monjalon.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [dpdk-dev] [PATCH v2] pci: keep API compatibility with mmap values 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" The function pci_map_resource() returns MAP_FAILED in case of error. When replacing the call to mmap() by rte_mem_map(), the error code became NULL, breaking the API. This function is probably not used outside of DPDK, but it is still a problem for two reasons: - the deprecation process was not followed - the Linux function pci_vfio_mmap_bar() is broken for i40e The error code is reverted to the Unix value MAP_FAILED. Windows needs to define this special value (-1 as in Unix). After proper deprecation process, the API could be changed again if really needed. Because of the switch from mmap() to rte_mem_map(), another part of the API was changed: "int additional_flags" are defined as "additional flags for the mapping range" without mentioning it was directly used in mmap(). Currently it is directly used in rte_mem_map(), that's why the values rte_map_flags must be mapped (sic) on the mmap ones in case of Unix OS. These are side effects of a badly defined API using Unix values. Bugzilla ID: 503 Fixes: 2fd3567e5425 ("pci: use OS generic memory mapping functions") Cc: talshn@mellanox.com Reported-by: David Marchand Signed-off-by: Thomas Monjalon --- v2: move MAP_FAILED from sys/mman.h to rte_os.h --- drivers/bus/pci/bsd/pci.c | 2 +- drivers/bus/pci/linux/pci_uio.c | 2 +- drivers/bus/pci/linux/pci_vfio.c | 4 ++-- drivers/bus/pci/pci_common_uio.c | 2 +- lib/librte_eal/include/rte_eal_paging.h | 10 ++++++++++ lib/librte_eal/windows/include/rte_os.h | 6 ++++++ lib/librte_pci/rte_pci.c | 1 + lib/librte_pci/rte_pci.h | 2 +- 8 files changed, 23 insertions(+), 6 deletions(-) diff --git a/drivers/bus/pci/bsd/pci.c b/drivers/bus/pci/bsd/pci.c index 8bc473eb9a..6ec27b4b5b 100644 --- a/drivers/bus/pci/bsd/pci.c +++ b/drivers/bus/pci/bsd/pci.c @@ -192,7 +192,7 @@ pci_uio_map_resource_by_index(struct rte_pci_device *dev, int res_idx, mapaddr = pci_map_resource(NULL, fd, (off_t)offset, (size_t)dev->mem_resource[res_idx].len, 0); close(fd); - if (mapaddr == NULL) + if (mapaddr == MAP_FAILED) goto error; maps[map_idx].phaddr = dev->mem_resource[res_idx].phys_addr; diff --git a/drivers/bus/pci/linux/pci_uio.c b/drivers/bus/pci/linux/pci_uio.c index b622001539..097dc19225 100644 --- a/drivers/bus/pci/linux/pci_uio.c +++ b/drivers/bus/pci/linux/pci_uio.c @@ -345,7 +345,7 @@ pci_uio_map_resource_by_index(struct rte_pci_device *dev, int res_idx, mapaddr = pci_map_resource(pci_map_addr, fd, 0, (size_t)dev->mem_resource[res_idx].len, 0); close(fd); - if (mapaddr == NULL) + if (mapaddr == MAP_FAILED) goto error; pci_map_addr = RTE_PTR_ADD(mapaddr, diff --git a/drivers/bus/pci/linux/pci_vfio.c b/drivers/bus/pci/linux/pci_vfio.c index fdeb9a8caf..07e072e13f 100644 --- a/drivers/bus/pci/linux/pci_vfio.c +++ b/drivers/bus/pci/linux/pci_vfio.c @@ -566,7 +566,7 @@ pci_vfio_mmap_bar(int vfio_dev_fd, struct mapped_pci_resource *vfio_res, } /* if there's a second part, try to map it */ - if (map_addr != NULL + if (map_addr != MAP_FAILED && memreg[1].offset && memreg[1].size) { void *second_addr = RTE_PTR_ADD(bar_addr, (uintptr_t)(memreg[1].offset - @@ -578,7 +578,7 @@ pci_vfio_mmap_bar(int vfio_dev_fd, struct mapped_pci_resource *vfio_res, RTE_MAP_FORCE_ADDRESS); } - if (map_addr == NULL) { + if (map_addr == NULL || map_addr == MAP_FAILED) { munmap(bar_addr, bar->size); bar_addr = MAP_FAILED; RTE_LOG(ERR, EAL, "Failed to map pci BAR%d\n", diff --git a/drivers/bus/pci/pci_common_uio.c b/drivers/bus/pci/pci_common_uio.c index 793dfd0a7c..f4dca9da91 100644 --- a/drivers/bus/pci/pci_common_uio.c +++ b/drivers/bus/pci/pci_common_uio.c @@ -58,7 +58,7 @@ pci_uio_map_secondary(struct rte_pci_device *dev) "Cannot mmap device resource file %s to address: %p\n", uio_res->maps[i].path, uio_res->maps[i].addr); - if (mapaddr != NULL) { + if (mapaddr != MAP_FAILED) { /* unmap addrs correctly mapped */ for (j = 0; j < i; j++) pci_unmap_resource( diff --git a/lib/librte_eal/include/rte_eal_paging.h b/lib/librte_eal/include/rte_eal_paging.h index ed98e70e9e..429f896c8c 100644 --- a/lib/librte_eal/include/rte_eal_paging.h +++ b/lib/librte_eal/include/rte_eal_paging.h @@ -3,6 +3,9 @@ */ #include +#ifndef RTE_EXEC_ENV_WINDOWS +#include +#endif #include @@ -22,6 +25,7 @@ enum rte_mem_prot { /** Additional flags for memory mapping. */ enum rte_map_flags { +#ifdef RTE_EXEC_ENV_WINDOWS /** Changes to the mapped memory are visible to other processes. */ RTE_MAP_SHARED = 1 << 0, /** Mapping is not backed by a regular file. */ @@ -35,6 +39,12 @@ enum rte_map_flags { * it is not required to do so, thus mapping with this flag may fail. */ RTE_MAP_FORCE_ADDRESS = 1 << 3 +#else /* map mmap flags because they are exposed in pci_map_resource() API */ + RTE_MAP_SHARED = MAP_SHARED, + RTE_MAP_ANONYMOUS = MAP_ANONYMOUS, + RTE_MAP_PRIVATE = MAP_PRIVATE, + RTE_MAP_FORCE_ADDRESS = MAP_FIXED, +#endif }; /** diff --git a/lib/librte_eal/windows/include/rte_os.h b/lib/librte_eal/windows/include/rte_os.h index 569ed92d51..2881bf2224 100644 --- a/lib/librte_eal/windows/include/rte_os.h +++ b/lib/librte_eal/windows/include/rte_os.h @@ -25,6 +25,12 @@ extern "C" { #define PATH_MAX _MAX_PATH #endif +/* sys/mman.h + * The syscall mmap does not exist on Windows, + * but this error code is used in a badly defined DPDK API for PCI mapping. + */ +#define MAP_FAILED ((void *) -1) + #define sleep(x) Sleep(1000 * (x)) #define strerror_r(a, b, c) strerror_s(b, c, a) diff --git a/lib/librte_pci/rte_pci.c b/lib/librte_pci/rte_pci.c index d8272b9076..1d1cbc75ac 100644 --- a/lib/librte_pci/rte_pci.c +++ b/lib/librte_pci/rte_pci.c @@ -163,6 +163,7 @@ pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size, __func__, fd, requested_addr, size, (unsigned long long)offset, rte_strerror(rte_errno), mapaddr); + mapaddr = MAP_FAILED; /* API uses mmap error code */ } else RTE_LOG(DEBUG, EAL, " PCI memory mapped at %p\n", mapaddr); diff --git a/lib/librte_pci/rte_pci.h b/lib/librte_pci/rte_pci.h index 104b2bb858..a03235da1f 100644 --- a/lib/librte_pci/rte_pci.h +++ b/lib/librte_pci/rte_pci.h @@ -160,7 +160,7 @@ int rte_pci_addr_parse(const char *str, struct rte_pci_addr *addr); * The additional flags for the mapping range. * @return * - On success, the function returns a pointer to the mapped area. - * - On error, NULL is returned. + * - On error, MAP_FAILED is returned. */ void *pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size, int additional_flags); -- 2.27.0