[v4] eal: fix memory mapping for 32-bit targets

Message ID 20210511144554.146987-1-lance.richardson@broadcom.com (mailing list archive)
State Rejected, archived
Delegated to: David Marchand
Headers
Series [v4] eal: fix memory mapping for 32-bit targets |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation success Compilation OK
ci/intel-Testing success Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-testing fail Testing issues
ci/iol-abi-testing success Testing PASS
ci/github-robot success github build: passed

Commit Message

Lance Richardson May 11, 2021, 2:45 p.m. UTC
  For 32-bit targets, size_t is normally a 32-bit type and
does not have sufficient range to represent 64-bit offsets
that can are needed when mapping PCI addresses. Use off_t
instead, which is usually a 64-bit type when compiled with
_D_FILE_OFFSET_BITS=64 as is the case for DPDK.

Found when attempting to run 32-bit Linux dpdk-testpmd
using VFIO driver:

    EAL: pci_map_resource(): cannot map resource(63, 0xc0010000, \
    0x200000, 0x20000000000): Invalid argument ((nil))

Fixes: c4b89ecb64ea ("eal: introduce memory management wrappers")
Cc: stable@dpdk.org
Signed-off-by: Lance Richardson <lance.richardson@broadcom.com>
---
 v4: Identical to v1, with <sys/types.h> now included in rte_eal_paging.h
     to (hopefully) make off_t available for Windows builds.
 v3: Use PRIx64 instead of llx ot format offset in log message.
 v2: Use uint64_t instead of off_t (off_t is unknown to Windows).

 lib/eal/include/rte_eal_paging.h |  3 ++-
 lib/eal/unix/eal_unix_memory.c   | 10 +++++-----
 lib/eal/windows/eal_memory.c     |  2 +-
 3 files changed, 8 insertions(+), 7 deletions(-)
  

Comments

Lance Richardson May 11, 2021, 3:56 p.m. UTC | #1
>  v4: Identical to v1, with <sys/types.h> now included in rte_eal_paging.h
>      to (hopefully) make off_t available for Windows builds.

With this version, using off_t is no longer a problem, however based on the
new compilation error it appears that for Windows, off_t is a 32-bit type
while size_t is a 64-bit type:

../lib/eal/windows/eal_memory.c:519:37: error: shift count >= width of
type [-Werror,-Wshift-count-overflow]
DWORD offset_high = (DWORD)(offset >> 32);
^  ~~

So the options seem to be to either use uint64_t for the offset parameter
as in patch v3, or else introduce something like:
     typedef uint64_t rte_off_t;

Thoughts/opinions?
  
Dmitry Kozlyuk May 11, 2021, 4:08 p.m. UTC | #2
2021-05-11 11:56 (UTC-0400), Lance Richardson:
> >  v4: Identical to v1, with <sys/types.h> now included in rte_eal_paging.h
> >      to (hopefully) make off_t available for Windows builds.  
> 
> With this version, using off_t is no longer a problem, however based on the
> new compilation error it appears that for Windows, off_t is a 32-bit type
> while size_t is a 64-bit type:
> 
> ../lib/eal/windows/eal_memory.c:519:37: error: shift count >= width of
> type [-Werror,-Wshift-count-overflow]
> DWORD offset_high = (DWORD)(offset >> 32);
> ^  ~~
> 
> So the options seem to be to either use uint64_t for the offset parameter
> as in patch v3, or else introduce something like:
>      typedef uint64_t rte_off_t;
> 
> Thoughts/opinions?

I'm for uint64_t: it's explicit and will hardly ever change.
  
Thomas Monjalon May 11, 2021, 8:45 p.m. UTC | #3
11/05/2021 18:08, Dmitry Kozlyuk:
> 2021-05-11 11:56 (UTC-0400), Lance Richardson:
> > >  v4: Identical to v1, with <sys/types.h> now included in rte_eal_paging.h
> > >      to (hopefully) make off_t available for Windows builds.  
> > 
> > With this version, using off_t is no longer a problem, however based on the
> > new compilation error it appears that for Windows, off_t is a 32-bit type
> > while size_t is a 64-bit type:
> > 
> > ../lib/eal/windows/eal_memory.c:519:37: error: shift count >= width of
> > type [-Werror,-Wshift-count-overflow]
> > DWORD offset_high = (DWORD)(offset >> 32);
> > ^  ~~
> > 
> > So the options seem to be to either use uint64_t for the offset parameter
> > as in patch v3, or else introduce something like:
> >      typedef uint64_t rte_off_t;
> > 
> > Thoughts/opinions?
> 
> I'm for uint64_t: it's explicit and will hardly ever change.

OK, v3 applied.
  

Patch

diff --git a/lib/eal/include/rte_eal_paging.h b/lib/eal/include/rte_eal_paging.h
index ed98e70e9e..974bf5ee64 100644
--- a/lib/eal/include/rte_eal_paging.h
+++ b/lib/eal/include/rte_eal_paging.h
@@ -3,6 +3,7 @@ 
  */
 
 #include <stdint.h>
+#include <sys/types.h>
 
 #include <rte_compat.h>
 
@@ -61,7 +62,7 @@  enum rte_map_flags {
 __rte_internal
 void *
 rte_mem_map(void *requested_addr, size_t size, int prot, int flags,
-	int fd, size_t offset);
+	int fd, off_t offset);
 
 /**
  * OS-independent implementation of POSIX munmap(3).
diff --git a/lib/eal/unix/eal_unix_memory.c b/lib/eal/unix/eal_unix_memory.c
index ec7156df96..51a42e1a43 100644
--- a/lib/eal/unix/eal_unix_memory.c
+++ b/lib/eal/unix/eal_unix_memory.c
@@ -24,14 +24,14 @@ 
 
 static void *
 mem_map(void *requested_addr, size_t size, int prot, int flags,
-	int fd, size_t offset)
+	int fd, off_t offset)
 {
 	void *virt = mmap(requested_addr, size, prot, flags, fd, offset);
 	if (virt == MAP_FAILED) {
 		RTE_LOG(DEBUG, EAL,
-			"Cannot mmap(%p, 0x%zx, 0x%x, 0x%x, %d, 0x%zx): %s\n",
-			requested_addr, size, prot, flags, fd, offset,
-			strerror(errno));
+			"Cannot mmap(%p, 0x%zx, 0x%x, 0x%x, %d, 0x%llx): %s\n",
+			requested_addr, size, prot, flags, fd,
+			(unsigned long long)offset, strerror(errno));
 		rte_errno = errno;
 		return NULL;
 	}
@@ -106,7 +106,7 @@  mem_rte_to_sys_prot(int prot)
 
 void *
 rte_mem_map(void *requested_addr, size_t size, int prot, int flags,
-	int fd, size_t offset)
+	int fd, off_t offset)
 {
 	int sys_flags = 0;
 	int sys_prot;
diff --git a/lib/eal/windows/eal_memory.c b/lib/eal/windows/eal_memory.c
index 2cf5a5e649..f1c4b03e96 100644
--- a/lib/eal/windows/eal_memory.c
+++ b/lib/eal/windows/eal_memory.c
@@ -508,7 +508,7 @@  eal_mem_set_dump(void *virt, size_t size, bool dump)
 
 void *
 rte_mem_map(void *requested_addr, size_t size, int prot, int flags,
-	int fd, size_t offset)
+	int fd, off_t offset)
 {
 	HANDLE file_handle = INVALID_HANDLE_VALUE;
 	HANDLE mapping_handle = INVALID_HANDLE_VALUE;