Skip to contentSkip to conversation

mem: get physical address of any pointer

PATCH In review Thomas Monjalon 2 participants 5 messages Jun 12, 2013 4 messages not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mem: get physical address of any pointer

From: Damien Millescamps <[email protected]>

Extract rte_mem_virt2phy() from find_physaddr().

This function permits to obtain the physical address of any virtual address mapped to the current process calling this function. Note that this function is very slow and shouldn't be called after initialization to avoid a performance bottleneck.

---
 lib/librte_eal/common/include/rte_memory.h |    7 +++
 lib/librte_eal/linuxapp/eal/eal_memory.c   |   83 +++++++++++++++-------------
 2 files changed, 52 insertions(+), 38 deletions(-)
lib/librte_eal/common/include/rte_memory.h +7 -0
index bf843dc..eaf4cc9 100644
@@ -70,6 +70,7 @@ enum rte_page_sizes {
 #ifndef __KERNEL__ /* so we can include this header in kernel modules */
 typedef uint64_t phys_addr_t; /**< Physical address definition. */
 #endif
+#define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
 
 /**
  * Physical memory segment descriptor.
@@ -87,6 +88,12 @@ struct rte_memseg {
 	uint32_t nrank;             /**< Number of ranks. */
 } __attribute__((__packed__));
 
+/**
+ * Get physical address of any mapped virtual address in the current process.
+ * It is found by browsing the /proc/self/pagemap special file.
+ * The page won't be swappable anymore.
+ */
+phys_addr_t rte_mem_virt2phy(const void *virt);
 
 /**
  * Get the layout of the available physical memory.
lib/librte_eal/linuxapp/eal/eal_memory.c +45 -38

104 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#2 lore raw
In reply to a message not in the archive

Hi Thomas,

Please find some comments below.

Quoted 6 lines
lib/librte_eal/common/include/rte_memory.h +6 -0
+/**
+ * Get physical address of any mapped virtual address in the current process.
+ * It is found by browsing the /proc/self/pagemap special file.
+ * The page won't be swappable anymore.
+ */
+phys_addr_t rte_mem_virt2phy(const void *virt);

lib/librte_eal/common/include/rte_memory.h struct rte_memseg {

Quoted 10 lines
lib/librte_eal/common/include/rte_memory.h +4 -2
 
[...]
-static int
-find_physaddr(struct hugepage *hugepg_tbl, struct hugepage_info *hpi)
+phys_addr_t
+rte_mem_virt2phy(const void *virt)
  {
[...]
+    /* allocate page in physical memory and prevent from swapping */
+    mlock((void*)aligned, page_size);

Should this function really do the mlock() ?

It's a bit confusing to have a function that looks like a "get" that will change the status of the page.

Another approach that sounds more reasonnable to me is to let the application does the mlock(), and have the rte_mem_virt2phy() return RTE_BAD_PHYS_ADDR if the page is not locked in memory. This info is well known from the /proc/self/pagemap.

Should we also rename virt2phy in virt2phys ?

Regards,
Olivier

#4 lore raw

PATCH 1/1 mem: get physical address of any pointer

In reply to a message not in the archive

From: Damien Millescamps <[email protected]>

Extract rte_mem_virt2phy() from find_physaddr().

rte_mem_virt2phy() permits to obtain the physical address of any virtual address mapped to the current process calling this function. Note that this function is very slow and shouldn't be called after initialization to avoid a performance bottleneck.

The memory must be locked with mlock(). The function rte_mem_lock_page() is a mlock() helper that lock the whole page.

A better name would be rte_mem_virt2phys but rte_mem_virt2phy is more consistent with rte_mempool_virt2phy.

---
 lib/librte_eal/common/include/rte_memory.h |   22 +++++++
 lib/librte_eal/linuxapp/eal/eal_memory.c   |   89 ++++++++++++++++------------
 2 files changed, 73 insertions(+), 38 deletions(-)
lib/librte_eal/common/include/rte_memory.h +22 -0
index bf843dc..d9d368b 100644
@@ -70,6 +70,7 @@ enum rte_page_sizes {
 #ifndef __KERNEL__ /* so we can include this header in kernel modules */
 typedef uint64_t phys_addr_t; /**< Physical address definition. */
 #endif
+#define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
 
 /**
  * Physical memory segment descriptor.
@@ -87,6 +88,27 @@ struct rte_memseg {
 	uint32_t nrank;             /**< Number of ranks. */
 } __attribute__((__packed__));
 
+/**
+ * Lock page in physical memory and prevent from swapping.
+ *
+ * @param virt
+ *   The virtual address.
+ * @return
+ *   0 on success, negative on error.
+ */
+int rte_mem_lock_page(const void *virt);
+
+/**
+ * Get physical address of any mapped virtual address in the current process.
+ * It is found by browsing the /proc/self/pagemap special file.
+ * The page must be locked.
+ *
+ * @param virt
+ *   The virtual address.
+ * @return
+ *   The physical address or RTE_BAD_PHYS_ADDR on error.
+ */
+phys_addr_t rte_mem_virt2phy(const void *virt);
 
 /**
  * Get the layout of the available physical memory.
lib/librte_eal/linuxapp/eal/eal_memory.c +51 -38

111 lines not on this page · raw message

Signature · 1 line
1.7.10.4