Um, not sure I agree with this implementation. I think a cleaner way would be 
to put the requested_addr in pci_uio_map_resource and pci_vfio_map_resource (or 
rather, put it in include/eal_pci_init.h, like extern void *requested_addr) but 
make actual use of it in pci_uio/vfio_map_resource only (and leave all of this 
out of eal_pci.c at all). That will also rid you of the necessity to pass 
around pointers to pointers.

(in that case I would also rename requested_addr to pci_map_addr or something, 
to make it less vague) 

Thanks,
Anatoly

-----Original Message-----
From: lxu [mailto:liang...@cinfotech.cn] 
Sent: Friday, November 7, 2014 2:58 PM
To: dev at dpdk.org
Cc: Burakov, Anatoly; thomas.monjalon at 6wind.com; De Lara Guarch, Pablo
Subject: [PATCH v7] eal: map uio resources after hugepages.

A multiple process DPDK application must mmap hugepages and pci resources into 
same virtual addresses. By default the virtual addresses chosen by the primary 
process automatically when calling the mmap. But sometime the chosen virtual 
addresses isn't usable at secondary process. Such as the secondary process 
linked with more libraries than primary process. The library has been mapped 
into this virtual address. The command line parameter 'base-virtaddr' has been 
added for this situation. If it's configured, the hugepages will be mapped into 
this base address. But the virtual address of uio resource mapped still does 
not refer to the parameter. In that case "EAL: pci_map_resource(): cannot mmap" 
will be got.

This patch try to map uio resources after hugepages. So the error can be 
resolved by set base-virtaddr into free virtual address space.

Signed-off-by: lxu <liang.xu at cinfotech.cn>
---
 lib/librte_eal/linuxapp/eal/eal_pci.c              | 25 ++++++++++++++++++++--
 lib/librte_eal/linuxapp/eal/eal_pci_uio.c          |  6 ++++--
 lib/librte_eal/linuxapp/eal/eal_pci_vfio.c         |  4 +++-
 lib/librte_eal/linuxapp/eal/include/eal_pci_init.h |  4 ++--
 4 files changed, 32 insertions(+), 7 deletions(-)

diff --git a/lib/librte_eal/linuxapp/eal/eal_pci.c 
b/lib/librte_eal/linuxapp/eal/eal_pci.c
index 5fe3961..aef6f5e 100644
--- a/lib/librte_eal/linuxapp/eal/eal_pci.c
+++ b/lib/librte_eal/linuxapp/eal/eal_pci.c
@@ -483,15 +483,36 @@ pci_config_space_set(struct rte_pci_device *dev)  }  
#endif

+static void *
+pci_find_max_end_va(void)
+{
+       const struct rte_memseg * seg = rte_eal_get_physmem_layout();
+       const struct rte_memseg * last = seg;
+       unsigned i = 0;
+
+       for (i=0; i<RTE_MAX_MEMSEG; i++, seg++) {
+               if (seg->addr == NULL)
+                       break;
+
+               if(seg->addr > last->addr)
+                       last = seg;
+
+       }
+       return RTE_PTR_ADD(last->addr, last->len); }
+
 static int
 pci_map_device(struct rte_pci_device *dev)  {
        int ret, mapped = 0;
+       static void * requested_addr;
+       if(NULL == requested_addr)
+               requested_addr = pci_find_max_end_va();

        /* try mapping the NIC resources using VFIO if it exists */  #ifdef 
VFIO_PRESENT
        if (pci_vfio_is_enabled()) {
-               ret = pci_vfio_map_resource(dev);
+               ret = pci_vfio_map_resource(dev, &requested_addr);
                if (ret == 0)
                        mapped = 1;
                else if (ret < 0)
@@ -500,7 +521,7 @@ pci_map_device(struct rte_pci_device *dev)  #endif
        /* map resources for devices that use igb_uio */
        if (!mapped) {
-               ret = pci_uio_map_resource(dev);
+               ret = pci_uio_map_resource(dev, &requested_addr);
                if (ret != 0)
                        return ret;
        }
diff --git a/lib/librte_eal/linuxapp/eal/eal_pci_uio.c 
b/lib/librte_eal/linuxapp/eal/eal_pci_uio.c
index 7e62266..e92124e 100644
--- a/lib/librte_eal/linuxapp/eal/eal_pci_uio.c
+++ b/lib/librte_eal/linuxapp/eal/eal_pci_uio.c
@@ -275,7 +275,7 @@ pci_get_uio_dev(struct rte_pci_device *dev, char *dstbuf,

 /* map the PCI resource of a PCI device in virtual memory */  int 
-pci_uio_map_resource(struct rte_pci_device *dev)
+pci_uio_map_resource(struct rte_pci_device *dev, void **requested_addr)
 {
        int i, j;
        char dirname[PATH_MAX];
@@ -371,10 +371,12 @@ pci_uio_map_resource(struct rte_pci_device *dev)
                        if (maps[j].addr != NULL)
                                fail = 1;
                        else {
-                               mapaddr = pci_map_resource(NULL, fd, 
(off_t)offset,
+                               mapaddr = pci_map_resource(*requested_addr, fd, 
(off_t)offset,
                                                (size_t)maps[j].size);
                                if (mapaddr == NULL)
                                        fail = 1;
+                               else
+                                       *requested_addr = RTE_PTR_ADD(mapaddr, 
maps[j].size);
                        }

                        if (fail) {
diff --git a/lib/librte_eal/linuxapp/eal/eal_pci_vfio.c 
b/lib/librte_eal/linuxapp/eal/eal_pci_vfio.c
index c776ddc..2102adf 100644
--- a/lib/librte_eal/linuxapp/eal/eal_pci_vfio.c
+++ b/lib/librte_eal/linuxapp/eal/eal_pci_vfio.c
@@ -515,7 +515,7 @@ clear_current_group(void)
  * primary and secondary processes follow almost exactly the same path
  */
 int
-pci_vfio_map_resource(struct rte_pci_device *dev)
+pci_vfio_map_resource(struct rte_pci_device *dev, void 
+**requested_addr)
 {
        struct vfio_group_status group_status = {
                        .argsz = sizeof(group_status)
@@ -720,6 +720,7 @@ pci_vfio_map_resource(struct rte_pci_device *dev)
                if (i == msix_bar)
                        continue;

+               maps[i].addr = *requested_addr;
                bar_addr = pci_map_resource(maps[i].addr, vfio_dev_fd, 
reg.offset,
                                reg.size);

@@ -732,6 +733,7 @@ pci_vfio_map_resource(struct rte_pci_device *dev)
                        return -1;
                }

+               *requested_addr = bar_addr;
                maps[i].addr = bar_addr;
                maps[i].offset = reg.offset;
                maps[i].size = reg.size;
diff --git a/lib/librte_eal/linuxapp/eal/include/eal_pci_init.h 
b/lib/librte_eal/linuxapp/eal/include/eal_pci_init.h
index d758bee..e14fa36 100644
--- a/lib/librte_eal/linuxapp/eal/include/eal_pci_init.h
+++ b/lib/librte_eal/linuxapp/eal/include/eal_pci_init.h
@@ -63,7 +63,7 @@ void *pci_map_resource(void *requested_addr, int fd, off_t 
offset,
                size_t size);

 /* map IGB_UIO resource prototype */
-int pci_uio_map_resource(struct rte_pci_device *dev);
+int pci_uio_map_resource(struct rte_pci_device *dev, void 
+**requested_addr);

 #ifdef VFIO_PRESENT

@@ -74,7 +74,7 @@ int pci_vfio_is_enabled(void);  int 
pci_vfio_mp_sync_setup(void);

 /* map VFIO resource prototype */
-int pci_vfio_map_resource(struct rte_pci_device *dev);
+int pci_vfio_map_resource(struct rte_pci_device *dev, void 
+**requested_addr);
 int pci_vfio_get_group_fd(int iommu_group_fd);  int 
pci_vfio_get_container_fd(void);

--
1.9.1

Reply via email to