diff mbox

[2/3] linux-keystone2: pass shm arg to odp_shm_reserve()

Message ID 1407393782-6189-3-git-send-email-maxim.uvarov@linaro.org
State New
Headers show

Commit Message

Maxim Uvarov Aug. 7, 2014, 6:43 a.m. UTC
Signed-off-by: Maxim Uvarov <maxim.uvarov@linaro.org>
---
 .../include/odp_shared_memory_internal.h           |  2 +-
 platform/linux-keystone2/odp_buffer_pool.c         |  3 +-
 platform/linux-keystone2/odp_packet_io.c           |  3 +-
 platform/linux-keystone2/odp_queue.c               |  3 +-
 platform/linux-keystone2/odp_shared_memory.c       | 45 +++++++++++++++++++---
 5 files changed, 46 insertions(+), 10 deletions(-)

Comments

Taras Kondratiuk Aug. 7, 2014, 10:26 a.m. UTC | #1
On 08/07/2014 09:43 AM, Maxim Uvarov wrote:
> Signed-off-by: Maxim Uvarov <maxim.uvarov@linaro.org>
> ---
>   .../include/odp_shared_memory_internal.h           |  2 +-
>   platform/linux-keystone2/odp_buffer_pool.c         |  3 +-
>   platform/linux-keystone2/odp_packet_io.c           |  3 +-
>   platform/linux-keystone2/odp_queue.c               |  3 +-
>   platform/linux-keystone2/odp_shared_memory.c       | 45 +++++++++++++++++++---
>   5 files changed, 46 insertions(+), 10 deletions(-)

[...]

> diff --git a/platform/linux-keystone2/odp_shared_memory.c b/platform/linux-keystone2/odp_shared_memory.c
> index e595111..bef7d40 100644

[...]

> @@ -161,18 +169,42 @@ void *_odp_shm_reserve(const char *name, uint64_t size, uint64_t align,
>   		void *addr = MAP_FAILED;
>   		block->huge = 0;
>
> +		if (flag != ODP_SHM_THREAD) {
> +			shm_open_flags = O_RDWR;
> +			if (flag == ODP_SHM_PROC)
> +				shm_open_flags |= O_CREAT;
> +
> +			shm = shm_open(name, shm_open_flags, S_IRUSR | S_IWUSR);
> +			if (shm == -1) {
> +				odp_spinlock_unlock(&odp_shm_tbl->lock);
> +				ODP_ERR("shm_open failed");
> +				return NULL;
> +			}
> +
> +			ret = ftruncate(shm, size + align);
> +			if (ret == -1) {
> +				odp_spinlock_unlock(&odp_shm_tbl->lock);
> +				if (flag != ODP_SHM_PROC_NOCREAT)
> +					shm_unlink(name);
> +				ODP_ERR("ftruncate failed");
> +				return NULL;
> +			}
> +		} else {
> +			mmap_flags |= MAP_ANONYMOUS;
> +		}
> +

This branch is accessible only inside of implementation.
Application will go via ODP_SHM_CMA part, so it doesn't make sense.
Maxim Uvarov Aug. 7, 2014, 11:20 a.m. UTC | #2
On 08/07/2014 02:26 PM, Taras Kondratiuk wrote:
> On 08/07/2014 09:43 AM, Maxim Uvarov wrote:
>
> This branch is accessible only inside of implementation.
> Application will go via ODP_SHM_CMA part, so it doesn't make sense.

Ok, let me send you the other patch with that for both  CMA  and mmap 
branches.
Looks like it's easy.

Maxim.
diff mbox

Patch

diff --git a/platform/linux-keystone2/include/odp_shared_memory_internal.h b/platform/linux-keystone2/include/odp_shared_memory_internal.h
index 833091e..fedec78 100644
--- a/platform/linux-keystone2/include/odp_shared_memory_internal.h
+++ b/platform/linux-keystone2/include/odp_shared_memory_internal.h
@@ -19,7 +19,7 @@  extern "C" {
 #endif
 
 void *_odp_shm_reserve(const char *name, uint64_t size, uint64_t align,
-		       int type);
+		       int type, odp_shm_e flag);
 uintptr_t _odp_shm_get_paddr(void *vaddr);
 
 #ifdef __cplusplus
diff --git a/platform/linux-keystone2/odp_buffer_pool.c b/platform/linux-keystone2/odp_buffer_pool.c
index 6ce02d4..f3e6f01 100644
--- a/platform/linux-keystone2/odp_buffer_pool.c
+++ b/platform/linux-keystone2/odp_buffer_pool.c
@@ -96,7 +96,8 @@  int odp_buffer_pool_init_global(void)
 
 	pool_tbl = odp_shm_reserve("odp_buffer_pools",
 				   sizeof(pool_table_t),
-				   sizeof(pool_entry_t));
+				   sizeof(pool_entry_t),
+				   ODP_SHM_THREAD);
 
 	if (pool_tbl == NULL)
 		return -1;
diff --git a/platform/linux-keystone2/odp_packet_io.c b/platform/linux-keystone2/odp_packet_io.c
index 1ded021..18efecd 100644
--- a/platform/linux-keystone2/odp_packet_io.c
+++ b/platform/linux-keystone2/odp_packet_io.c
@@ -71,7 +71,8 @@  int odp_pktio_init_global(void)
 
 	pktio_tbl = odp_shm_reserve("odp_pktio_entries",
 				    sizeof(pktio_table_t),
-				    sizeof(pktio_entry_t));
+				    sizeof(pktio_entry_t),
+				    ODP_SHM_THREAD);
 	if (pktio_tbl == NULL)
 		return -1;
 
diff --git a/platform/linux-keystone2/odp_queue.c b/platform/linux-keystone2/odp_queue.c
index 031eeff..8963d6a 100644
--- a/platform/linux-keystone2/odp_queue.c
+++ b/platform/linux-keystone2/odp_queue.c
@@ -99,7 +99,8 @@  int odp_queue_init_global(void)
 
 	queue_tbl = odp_shm_reserve("odp_queues",
 				    sizeof(queue_table_t),
-				    sizeof(queue_entry_t));
+				    sizeof(queue_entry_t),
+				    ODP_SHM_THREAD);
 
 	if (queue_tbl == NULL)
 		return -1;
diff --git a/platform/linux-keystone2/odp_shared_memory.c b/platform/linux-keystone2/odp_shared_memory.c
index e595111..bef7d40 100644
--- a/platform/linux-keystone2/odp_shared_memory.c
+++ b/platform/linux-keystone2/odp_shared_memory.c
@@ -99,9 +99,11 @@  enum {
 };
 
 void *_odp_shm_reserve(const char *name, uint64_t size, uint64_t align,
-		       int type)
+		       int type, odp_shm_e flag)
 {
-	int i;
+	int i, ret, shm_open_flags;
+	int shm = -1;
+	int mmap_flags = MAP_SHARED;
 	odp_shm_block_t *block;
 #ifdef MAP_HUGETLB
 	uint64_t huge_sz, page_sz;
@@ -144,6 +146,12 @@  void *_odp_shm_reserve(const char *name, uint64_t size, uint64_t align,
 	mem_config.mapping_id = -1;
 
 	if (type == ODP_SHM_CMA) {
+		if (flag != ODP_SHM_THREAD) {
+			ODP_ERR("shm_open with ODP_SHM_CMA is not implemented\n");
+			odp_spinlock_unlock(&odp_shm_tbl->lock);
+			return NULL;
+		}
+
 		ti_em_rh_alloc_map_cma(&mem_config);
 
 		if (!mem_config.vaddr) {
@@ -161,18 +169,42 @@  void *_odp_shm_reserve(const char *name, uint64_t size, uint64_t align,
 		void *addr = MAP_FAILED;
 		block->huge = 0;
 
+		if (flag != ODP_SHM_THREAD) {
+			shm_open_flags = O_RDWR;
+			if (flag == ODP_SHM_PROC)
+				shm_open_flags |= O_CREAT;
+
+			shm = shm_open(name, shm_open_flags, S_IRUSR | S_IWUSR);
+			if (shm == -1) {
+				odp_spinlock_unlock(&odp_shm_tbl->lock);
+				ODP_ERR("shm_open failed");
+				return NULL;
+			}
+
+			ret = ftruncate(shm, size + align);
+			if (ret == -1) {
+				odp_spinlock_unlock(&odp_shm_tbl->lock);
+				if (flag != ODP_SHM_PROC_NOCREAT)
+					shm_unlink(name);
+				ODP_ERR("ftruncate failed");
+				return NULL;
+			}
+		} else {
+			mmap_flags |= MAP_ANONYMOUS;
+		}
+
 #ifdef MAP_HUGETLB
 		/* Try first huge pages */
 		if (huge_sz && (size + align) > page_sz) {
 			addr = mmap(NULL, size + align, PROT_READ | PROT_WRITE,
-				    SHM_FLAGS | MAP_HUGETLB, -1, 0);
+				    mmap_flags | MAP_HUGETLB, shm, 0);
 		}
 #endif
 
 		/* Use normal pages for small or failed huge page allocations */
 		if (addr == MAP_FAILED) {
 			addr = mmap(NULL, size + align, PROT_READ | PROT_WRITE,
-				    SHM_FLAGS, -1, 0);
+				    mmap_flags, shm, 0);
 		} else {
 			block->huge = 1;
 		}
@@ -208,9 +240,10 @@  void *_odp_shm_reserve(const char *name, uint64_t size, uint64_t align,
 	return block->addr;
 }
 
-void *odp_shm_reserve(const char *name, uint64_t size, uint64_t align)
+void *odp_shm_reserve(const char *name, uint64_t size, uint64_t align,
+		      odp_shm_e flag)
 {
-	return _odp_shm_reserve(name, size, align, ODP_SHM_CMA);
+	return _odp_shm_reserve(name, size, align, ODP_SHM_CMA, flag);
 }
 
 uintptr_t _odp_shm_get_paddr(void *vaddr)