From patchwork Mon Aug 4 14:48:02 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Uvarov X-Patchwork-Id: 34853 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-oa0-f69.google.com (mail-oa0-f69.google.com [209.85.219.69]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 2DE4820F2E for ; Mon, 4 Aug 2014 14:48:58 +0000 (UTC) Received: by mail-oa0-f69.google.com with SMTP id i7sf40608269oag.8 for ; Mon, 04 Aug 2014 07:48:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:date:message-id:in-reply-to :references:subject:precedence:list-id:list-unsubscribe:list-archive :list-post:list-help:list-subscribe:mime-version:errors-to:sender :x-original-sender:x-original-authentication-results:mailing-list :content-type:content-transfer-encoding; bh=qIYkUJP47GyY7Jm0YUYOK//3xX9hG4dr12AiMVkkgrk=; b=bMIiDOJsYZKKH2I6eFcsqfLC8oyzcP7Ji9yYQKn5JzweFi7zXgFHkfsylbffcIFZ+s DnYlUwTKUqvBh0t1F3PeIRPQ7DoZXk0BYt6u1dp9CLsCMuPwtVsvcYnVHyNVu9bdbaNC EOIzip0orXkmlwAOlU6yijA/VQxttKFnk15Vc1eyoxFDQqxhHIKGnCf9LIdFk9qDG6Kd 8lb/0upSwfOkt8nrUB8g5b+vKluOU1QxjLRtwzdAdaod84/cfGd7RxRGFYqtqmCIimcd fjpSUXdfHk+5/zgoFN0AUBC0R1ynOutvPpecXiqkkBsgNNNV+s7yfIuJd02KPZ8OxW0c 4QNg== X-Gm-Message-State: ALoCoQmfX4bqKInSNhqkQlBmyso2P82SndfoZVW8QZuVOSZ3Ks2EMANQ1uoWD4Afl/98QvKH/rot X-Received: by 10.42.16.69 with SMTP id o5mr35304429ica.17.1407163736645; Mon, 04 Aug 2014 07:48:56 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.47.174 with SMTP id m43ls2258338qga.84.gmail; Mon, 04 Aug 2014 07:48:56 -0700 (PDT) X-Received: by 10.220.116.196 with SMTP id n4mr23955573vcq.6.1407163736558; Mon, 04 Aug 2014 07:48:56 -0700 (PDT) Received: from mail-vc0-f179.google.com (mail-vc0-f179.google.com [209.85.220.179]) by mx.google.com with ESMTPS id e4si12055084vdw.49.2014.08.04.07.48.56 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 04 Aug 2014 07:48:56 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.179 as permitted sender) client-ip=209.85.220.179; Received: by mail-vc0-f179.google.com with SMTP id hq11so11261600vcb.10 for ; Mon, 04 Aug 2014 07:48:56 -0700 (PDT) X-Received: by 10.52.129.165 with SMTP id nx5mr19391540vdb.25.1407163736389; Mon, 04 Aug 2014 07:48:56 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.221.37.5 with SMTP id tc5csp310308vcb; Mon, 4 Aug 2014 07:48:55 -0700 (PDT) X-Received: by 10.180.210.172 with SMTP id mv12mr29971203wic.14.1407163734803; Mon, 04 Aug 2014 07:48:54 -0700 (PDT) Received: from ip-10-141-164-156.ec2.internal (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTPS id ls14si21273956wic.32.2014.08.04.07.48.42 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Mon, 04 Aug 2014 07:48:44 -0700 (PDT) Received-SPF: none (google.com: lng-odp-bounces@lists.linaro.org does not designate permitted sender hosts) client-ip=54.225.227.206; Received: from localhost ([127.0.0.1] helo=ip-10-141-164-156.ec2.internal) by ip-10-141-164-156.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1XEJYs-00059Y-NQ; Mon, 04 Aug 2014 14:48:30 +0000 Received: from mail-la0-f42.google.com ([209.85.215.42]) by ip-10-141-164-156.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1XEJYg-00058k-Dw for lng-odp@lists.linaro.org; Mon, 04 Aug 2014 14:48:18 +0000 Received: by mail-la0-f42.google.com with SMTP id pv20so5499376lab.1 for ; Mon, 04 Aug 2014 07:48:12 -0700 (PDT) X-Received: by 10.112.137.97 with SMTP id qh1mr2800722lbb.57.1407163692050; Mon, 04 Aug 2014 07:48:12 -0700 (PDT) Received: from localhost.localdomain (ppp91-76-29-79.pppoe.mtu-net.ru. [91.76.29.79]) by mx.google.com with ESMTPSA id dk4sm22342450lbb.38.2014.08.04.07.48.10 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 04 Aug 2014 07:48:11 -0700 (PDT) From: Maxim Uvarov To: lng-odp@lists.linaro.org Date: Mon, 4 Aug 2014 18:48:02 +0400 Message-Id: <1407163684-27844-2-git-send-email-maxim.uvarov@linaro.org> X-Mailer: git-send-email 1.8.5.1.163.gd7aced9 In-Reply-To: <1407163684-27844-1-git-send-email-maxim.uvarov@linaro.org> References: <1407163684-27844-1-git-send-email-maxim.uvarov@linaro.org> X-Topics: patch Subject: [lng-odp] [PATCHv3 1/3] odp shm shared between processes X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: , List-Help: , List-Subscribe: , MIME-Version: 1.0 Errors-To: lng-odp-bounces@lists.linaro.org Sender: lng-odp-bounces@lists.linaro.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: maxim.uvarov@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.179 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 Add flag to odp_shm_reserve() to select if allocated memory is visible by thread or it's visible by other processes. Signed-off-by: Maxim Uvarov --- example/generator/odp_generator.c | 6 +++-- example/l2fwd/odp_l2fwd.c | 6 +++-- example/odp_example/odp_example.c | 3 ++- example/packet/odp_pktio.c | 6 +++-- example/timer/odp_timer_test.c | 3 ++- include/odp_shared_memory.h | 11 +++++++- platform/linux-generic/odp_buffer_pool.c | 3 ++- platform/linux-generic/odp_packet_io.c | 3 ++- platform/linux-generic/odp_queue.c | 3 ++- platform/linux-generic/odp_ring.c | 3 ++- platform/linux-generic/odp_schedule.c | 6 +++-- platform/linux-generic/odp_shared_memory.c | 40 ++++++++++++++++++++++++++---- test/api_test/odp_shm_test.c | 3 ++- test/api_test/odp_timer_ping.c | 3 ++- 14 files changed, 77 insertions(+), 22 deletions(-) diff --git a/example/generator/odp_generator.c b/example/generator/odp_generator.c index e4a72fa..b1be789 100644 --- a/example/generator/odp_generator.c +++ b/example/generator/odp_generator.c @@ -542,7 +542,8 @@ int main(int argc, char *argv[]) odp_atomic_init_u64(&counters.icmp); /* Reserve memory for args from shared mem */ - args = odp_shm_reserve("shm_args", sizeof(args_t), ODP_CACHE_LINE_SIZE); + args = odp_shm_reserve("shm_args", sizeof(args_t), ODP_CACHE_LINE_SIZE, + ODP_SHM_THREAD); if (args == NULL) { ODP_ERR("Error: shared mem alloc failed.\n"); exit(EXIT_FAILURE); @@ -587,7 +588,8 @@ int main(int argc, char *argv[]) /* Create packet pool */ pool_base = odp_shm_reserve("shm_packet_pool", - SHM_PKT_POOL_SIZE, ODP_CACHE_LINE_SIZE); + SHM_PKT_POOL_SIZE, ODP_CACHE_LINE_SIZE, + ODP_SHM_THREAD); if (pool_base == NULL) { ODP_ERR("Error: packet pool mem alloc failed.\n"); exit(EXIT_FAILURE); diff --git a/example/l2fwd/odp_l2fwd.c b/example/l2fwd/odp_l2fwd.c index e331ff2..c5f3725 100644 --- a/example/l2fwd/odp_l2fwd.c +++ b/example/l2fwd/odp_l2fwd.c @@ -294,7 +294,8 @@ int main(int argc, char *argv[]) } /* Reserve memory for args from shared mem */ - gbl_args = odp_shm_reserve("shm_args", sizeof(args_t), ODP_CACHE_LINE_SIZE); + gbl_args = odp_shm_reserve("shm_args", sizeof(args_t), + ODP_CACHE_LINE_SIZE, ODP_SHM_THREAD); if (gbl_args == NULL) { ODP_ERR("Error: shared mem alloc failed.\n"); exit(EXIT_FAILURE); @@ -345,7 +346,8 @@ int main(int argc, char *argv[]) /* Create packet pool */ pool_base = odp_shm_reserve("shm_packet_pool", - SHM_PKT_POOL_SIZE, ODP_CACHE_LINE_SIZE); + SHM_PKT_POOL_SIZE, ODP_CACHE_LINE_SIZE, + ODP_SHM_THREAD); if (pool_base == NULL) { ODP_ERR("Error: packet pool mem alloc failed.\n"); exit(EXIT_FAILURE); diff --git a/example/odp_example/odp_example.c b/example/odp_example/odp_example.c index be96093..6d075b2 100644 --- a/example/odp_example/odp_example.c +++ b/example/odp_example/odp_example.c @@ -987,7 +987,8 @@ int main(int argc, char *argv[]) * Create message pool */ pool_base = odp_shm_reserve("msg_pool", - MSG_POOL_SIZE, ODP_CACHE_LINE_SIZE); + MSG_POOL_SIZE, ODP_CACHE_LINE_SIZE, + ODP_SHM_THREAD); pool = odp_buffer_pool_create("msg_pool", pool_base, MSG_POOL_SIZE, sizeof(test_message_t), diff --git a/example/packet/odp_pktio.c b/example/packet/odp_pktio.c index edf8cfd..f5694c3 100644 --- a/example/packet/odp_pktio.c +++ b/example/packet/odp_pktio.c @@ -291,7 +291,8 @@ int main(int argc, char *argv[]) } /* Reserve memory for args from shared mem */ - args = odp_shm_reserve("shm_args", sizeof(args_t), ODP_CACHE_LINE_SIZE); + args = odp_shm_reserve("shm_args", sizeof(args_t), ODP_CACHE_LINE_SIZE, + ODP_SHM_THREAD); if (args == NULL) { ODP_ERR("Error: shared mem alloc failed.\n"); exit(EXIT_FAILURE); @@ -332,7 +333,8 @@ int main(int argc, char *argv[]) /* Create packet pool */ pool_base = odp_shm_reserve("shm_packet_pool", - SHM_PKT_POOL_SIZE, ODP_CACHE_LINE_SIZE); + SHM_PKT_POOL_SIZE, ODP_CACHE_LINE_SIZE, + ODP_SHM_THREAD); if (pool_base == NULL) { ODP_ERR("Error: packet pool mem alloc failed.\n"); exit(EXIT_FAILURE); diff --git a/example/timer/odp_timer_test.c b/example/timer/odp_timer_test.c index dbe0e5b..113200b 100644 --- a/example/timer/odp_timer_test.c +++ b/example/timer/odp_timer_test.c @@ -260,7 +260,8 @@ int main(int argc, char *argv[]) * Create message pool */ pool_base = odp_shm_reserve("msg_pool", - MSG_POOL_SIZE, ODP_CACHE_LINE_SIZE); + MSG_POOL_SIZE, ODP_CACHE_LINE_SIZE, + ODP_SHM_THREAD); pool = odp_buffer_pool_create("msg_pool", pool_base, MSG_POOL_SIZE, 0, diff --git a/include/odp_shared_memory.h b/include/odp_shared_memory.h index 8ac8847..f1947a7 100644 --- a/include/odp_shared_memory.h +++ b/include/odp_shared_memory.h @@ -24,6 +24,13 @@ extern "C" { /** Maximum shared memory block name lenght in chars */ #define ODP_SHM_NAME_LEN 32 +typedef enum { + ODP_SHM_THREAD, /**< Memory accessible by threads. */ + ODP_SHM_PROC, /**< Memory accessible by processes. + Will be created if not exist. */ + ODP_SHM_PROC_NOCREAT, /**< Memory accessible by processes. + Has to be created before usage.*/ +} odp_shm_e; /** * Reserve a block of shared memory @@ -31,10 +38,12 @@ extern "C" { * @param name Name of the block (maximum ODP_SHM_NAME_LEN - 1 chars) * @param size Block size in bytes * @param align Block alignment in bytes + * @param flag Flags for shared memory creation * * @return Pointer to the reserved block, or NULL */ -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); /** * Lookup for a block of shared memory diff --git a/platform/linux-generic/odp_buffer_pool.c b/platform/linux-generic/odp_buffer_pool.c index a48781a..9157994 100644 --- a/platform/linux-generic/odp_buffer_pool.c +++ b/platform/linux-generic/odp_buffer_pool.c @@ -102,7 +102,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-generic/odp_packet_io.c b/platform/linux-generic/odp_packet_io.c index 33ade10..050ade4 100644 --- a/platform/linux-generic/odp_packet_io.c +++ b/platform/linux-generic/odp_packet_io.c @@ -55,7 +55,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-generic/odp_queue.c b/platform/linux-generic/odp_queue.c index c637bdf..507331f 100644 --- a/platform/linux-generic/odp_queue.c +++ b/platform/linux-generic/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-generic/odp_ring.c b/platform/linux-generic/odp_ring.c index 25ff66a..ee8175e 100644 --- a/platform/linux-generic/odp_ring.c +++ b/platform/linux-generic/odp_ring.c @@ -171,7 +171,8 @@ odp_ring_create(const char *name, unsigned count, unsigned flags) odp_rwlock_write_lock(&qlock); /* reserve a memory zone for this ring.*/ - r = odp_shm_reserve(ring_name, ring_size, ODP_CACHE_LINE_SIZE); + r = odp_shm_reserve(ring_name, ring_size, ODP_CACHE_LINE_SIZE, + ODP_SHM_THREAD); if (r != NULL) { /* init the ring structure */ diff --git a/platform/linux-generic/odp_schedule.c b/platform/linux-generic/odp_schedule.c index 9e399f1..9e76692 100644 --- a/platform/linux-generic/odp_schedule.c +++ b/platform/linux-generic/odp_schedule.c @@ -89,7 +89,8 @@ int odp_schedule_init_global(void) sched = odp_shm_reserve("odp_scheduler", sizeof(sched_t), - ODP_CACHE_LINE_SIZE); + ODP_CACHE_LINE_SIZE, + ODP_SHM_THREAD); if (sched == NULL) { ODP_ERR("Schedule init: Shm reserve failed.\n"); @@ -98,7 +99,8 @@ int odp_schedule_init_global(void) pool_base = odp_shm_reserve("odp_sched_pool", - SCHED_POOL_SIZE, ODP_CACHE_LINE_SIZE); + SCHED_POOL_SIZE, ODP_CACHE_LINE_SIZE, + ODP_SHM_THREAD); pool = odp_buffer_pool_create("odp_sched_pool", pool_base, SCHED_POOL_SIZE, sizeof(queue_desc_t), diff --git a/platform/linux-generic/odp_shared_memory.c b/platform/linux-generic/odp_shared_memory.c index 784f42b..0217b8a 100644 --- a/platform/linux-generic/odp_shared_memory.c +++ b/platform/linux-generic/odp_shared_memory.c @@ -14,6 +14,8 @@ #include #include #include +#include +#include #include #include @@ -95,9 +97,12 @@ static int find_block(const char *name) } -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) { - int i; + int i, ret, shm_open_flags; + int shm = -1; + int mmap_flags = MAP_SHARED; odp_shm_block_t *block; void *addr; #ifdef MAP_HUGETLB @@ -133,19 +138,42 @@ void *odp_shm_reserve(const char *name, uint64_t size, uint64_t align) 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; } @@ -153,6 +181,8 @@ void *odp_shm_reserve(const char *name, uint64_t size, uint64_t align) if (addr == MAP_FAILED) { /* Alloc failed */ odp_spinlock_unlock(&odp_shm_tbl->lock); + if (flag != ODP_SHM_PROC_NOCREAT) + shm_unlink(name); return NULL; } diff --git a/test/api_test/odp_shm_test.c b/test/api_test/odp_shm_test.c index 318d662..fc448a4 100644 --- a/test/api_test/odp_shm_test.c +++ b/test/api_test/odp_shm_test.c @@ -47,7 +47,8 @@ int main(int argc ODP_UNUSED, char *argv[] ODP_UNUSED) odp_print_system_info(); test_shared_data = odp_shm_reserve("test_shared_data", - sizeof(test_shared_data_t), 128); + sizeof(test_shared_data_t), 128, + ODP_SHM_THREAD); memset(test_shared_data, 0, sizeof(test_shared_data_t)); printf("test shared data at %p\n\n", test_shared_data); diff --git a/test/api_test/odp_timer_ping.c b/test/api_test/odp_timer_ping.c index cd67e0d..308e88c 100644 --- a/test/api_test/odp_timer_ping.c +++ b/test/api_test/odp_timer_ping.c @@ -324,7 +324,8 @@ int main(int argc ODP_UNUSED, char *argv[] ODP_UNUSED) * Create message pool */ pool_base = odp_shm_reserve("msg_pool", - MSG_POOL_SIZE, ODP_CACHE_LINE_SIZE); + MSG_POOL_SIZE, ODP_CACHE_LINE_SIZE, + ODP_SHM_THREAD); pool = odp_buffer_pool_create("msg_pool", pool_base, MSG_POOL_SIZE, BUF_SIZE,