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

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

Checks

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

Commit Message

Lance Richardson May 8, 2021, 2:27 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 uint64_t
instead.

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>
---
v3: Use PRIx64 instead of llx to 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 |  2 +-
 lib/eal/unix/eal_unix_memory.c   | 11 ++++++-----
 lib/eal/windows/eal_memory.c     |  2 +-
 3 files changed, 8 insertions(+), 7 deletions(-)
  

Comments

Thomas Monjalon May 10, 2021, 12:42 p.m. UTC | #1
+Cc Anatoly

08/05/2021 16:27, Lance Richardson:
> 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 uint64_t
> instead.
> 
> 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>
> ---
> v3: Use PRIx64 instead of llx to 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 |  2 +-
>  lib/eal/unix/eal_unix_memory.c   | 11 ++++++-----
>  lib/eal/windows/eal_memory.c     |  2 +-
>  3 files changed, 8 insertions(+), 7 deletions(-)
> 
> diff --git a/lib/eal/include/rte_eal_paging.h b/lib/eal/include/rte_eal_paging.h
> index ed98e70e9e..c60317d0f5 100644
> --- a/lib/eal/include/rte_eal_paging.h
> +++ b/lib/eal/include/rte_eal_paging.h
> @@ -61,7 +61,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, uint64_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..68ae93bd6e 100644
> --- a/lib/eal/unix/eal_unix_memory.c
> +++ b/lib/eal/unix/eal_unix_memory.c
> @@ -5,6 +5,7 @@
>  #include <string.h>
>  #include <sys/mman.h>
>  #include <unistd.h>
> +#include <inttypes.h>
>  
>  #include <rte_eal_paging.h>
>  #include <rte_errno.h>
> @@ -24,14 +25,14 @@
>  
>  static void *
>  mem_map(void *requested_addr, size_t size, int prot, int flags,
> -	int fd, size_t offset)
> +	int fd, uint64_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%"PRIx64"): %s\n",
> +		    requested_addr, size, prot, flags, fd, offset,
> +		    strerror(errno));
>  		rte_errno = errno;
>  		return NULL;
>  	}
> @@ -106,7 +107,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, uint64_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..4db048ccb5 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, uint64_t offset)
>  {
>  	HANDLE file_handle = INVALID_HANDLE_VALUE;
>  	HANDLE mapping_handle = INVALID_HANDLE_VALUE;
>
  
Burakov, Anatoly May 10, 2021, 3:31 p.m. UTC | #2
On 10-May-21 1:42 PM, Thomas Monjalon wrote:
> +Cc Anatoly
> 
> 08/05/2021 16:27, Lance Richardson:
>> 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 uint64_t
>> instead.
>>
>> 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>
>> ---

LGTM, although the Fixes: tag is probably wrong because IIRC previous 
versions did have the same issue.

Acked-by: Anatoly Burakov <anatoly.burakov@intel.com>
  
Thomas Monjalon May 10, 2021, 3:55 p.m. UTC | #3
10/05/2021 17:31, Burakov, Anatoly:
> On 10-May-21 1:42 PM, Thomas Monjalon wrote:
> > +Cc Anatoly
> > 
> > 08/05/2021 16:27, Lance Richardson:
> >> 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 uint64_t
> >> instead.
> >>
> >> 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>
> >> ---
> 
> LGTM, although the Fixes: tag is probably wrong because IIRC previous 
> versions did have the same issue.

What should be the right Fixes tag Anatoly?
  
Lance Richardson May 10, 2021, 4:04 p.m. UTC | #4
On Mon, May 10, 2021 at 11:55 AM Thomas Monjalon <thomas@monjalon.net>
wrote:

> 10/05/2021 17:31, Burakov, Anatoly:
> > On 10-May-21 1:42 PM, Thomas Monjalon wrote:
> > > +Cc Anatoly
> > >
> > > 08/05/2021 16:27, Lance Richardson:
> > >> 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 uint64_t
> > >> instead.
> > >>
> > >> 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>
> > >> ---
> >
> > LGTM, although the Fixes: tag is probably wrong because IIRC previous
> > versions did have the same issue.
>
> What should be the right Fixes tag Anatoly?
>

I think this would be more appropriate (prior to this commit,
pci_map_resource()
called mmap() directly with off_t offset, with this commit offset was
narrowed
to size_t before calling mmap()):

Fixes: 2fd3567e5425 ("pci: use OS generic memory mapping functions")
  
Thomas Monjalon May 11, 2021, 7:55 a.m. UTC | #5
10/05/2021 18:04, Lance Richardson:
> On Mon, May 10, 2021 at 11:55 AM Thomas Monjalon <thomas@monjalon.net>
> wrote:
> 
> > 10/05/2021 17:31, Burakov, Anatoly:
> > > On 10-May-21 1:42 PM, Thomas Monjalon wrote:
> > > > +Cc Anatoly
> > > >
> > > > 08/05/2021 16:27, Lance Richardson:
> > > >> 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 uint64_t
> > > >> instead.
> > > >>
> > > >> 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>
> > > >> ---
> > >
> > > LGTM, although the Fixes: tag is probably wrong because IIRC previous
> > > versions did have the same issue.
> >
> > What should be the right Fixes tag Anatoly?
> >
> 
> I think this would be more appropriate (prior to this commit,
> pci_map_resource()
> called mmap() directly with off_t offset, with this commit offset was
> narrowed
> to size_t before calling mmap()):
> 
> Fixes: 2fd3567e5425 ("pci: use OS generic memory mapping functions")

That's only one usage of rte_mem_map.
I prefer the original Fixes: line.
  
Thomas Monjalon May 11, 2021, 8:17 a.m. UTC | #6
08/05/2021 16:27, Lance Richardson:
> 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 uint64_t
> instead.
> 
> 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>
> ---
> v3: Use PRIx64 instead of llx to format offset in log message.
> v2: Use uint64_t instead of off_t (off_t is unknown to Windows).

off_t is referenced in drivers/bus/pci/windows/pci.c
(for unused parameter) so it should be fine?
If not, we could add it?
  
Lance Richardson May 11, 2021, 1:33 p.m. UTC | #7
On Tue, May 11, 2021 at 4:18 AM Thomas Monjalon <thomas@monjalon.net> wrote:
>
> 08/05/2021 16:27, Lance Richardson:
> > 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 uint64_t
> > instead.
> >
> > 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>
> > ---
> > v3: Use PRIx64 instead of llx to format offset in log message.
> > v2: Use uint64_t instead of off_t (off_t is unknown to Windows).
>
> off_t is referenced in drivers/bus/pci/windows/pci.c
> (for unused parameter) so it should be fine?
> If not, we could add it?

 v1 of the patch had the following build error in CI:

   *Build Failed #1:
   OS: WIN10-64
   Target: x86_64-windows-clang
   FAILED: lib/librte_eal.a.p/eal_common_eal_common_fbarray.c.obj
   ....
   In file included from ../lib/eal/common/eal_common_fbarray.c:14:
   ..\lib\eal\include\rte_eal_paging.h:64:10: error: unknown type name 'off_t'
           int fd, off_t offset);
  
Thomas Monjalon May 11, 2021, 8:45 p.m. UTC | #8
10/05/2021 17:31, Burakov, Anatoly:
> On 10-May-21 1:42 PM, Thomas Monjalon wrote:
> > +Cc Anatoly
> > 
> > 08/05/2021 16:27, Lance Richardson:
> >> 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 uint64_t
> >> instead.
> >>
> >> 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>
> >> ---
> 
> LGTM, although the Fixes: tag is probably wrong because IIRC previous 
> versions did have the same issue.
> 
> Acked-by: Anatoly Burakov <anatoly.burakov@intel.com>

v3 applied, thanks
  

Patch

diff --git a/lib/eal/include/rte_eal_paging.h b/lib/eal/include/rte_eal_paging.h
index ed98e70e9e..c60317d0f5 100644
--- a/lib/eal/include/rte_eal_paging.h
+++ b/lib/eal/include/rte_eal_paging.h
@@ -61,7 +61,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, uint64_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..68ae93bd6e 100644
--- a/lib/eal/unix/eal_unix_memory.c
+++ b/lib/eal/unix/eal_unix_memory.c
@@ -5,6 +5,7 @@ 
 #include <string.h>
 #include <sys/mman.h>
 #include <unistd.h>
+#include <inttypes.h>
 
 #include <rte_eal_paging.h>
 #include <rte_errno.h>
@@ -24,14 +25,14 @@ 
 
 static void *
 mem_map(void *requested_addr, size_t size, int prot, int flags,
-	int fd, size_t offset)
+	int fd, uint64_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%"PRIx64"): %s\n",
+		    requested_addr, size, prot, flags, fd, offset,
+		    strerror(errno));
 		rte_errno = errno;
 		return NULL;
 	}
@@ -106,7 +107,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, uint64_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..4db048ccb5 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, uint64_t offset)
 {
 	HANDLE file_handle = INVALID_HANDLE_VALUE;
 	HANDLE mapping_handle = INVALID_HANDLE_VALUE;