From patchwork Fri Sep 5 11:29:10 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petri Savolainen X-Patchwork-Id: 36813 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-yk0-f200.google.com (mail-yk0-f200.google.com [209.85.160.200]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 54938206A0 for ; Fri, 5 Sep 2014 11:28:22 +0000 (UTC) Received: by mail-yk0-f200.google.com with SMTP id 9sf35235357ykp.7 for ; Fri, 05 Sep 2014 04:28:22 -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: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=GitybcEKy7XsFKYOhWi/3j+3TN0NEon+Q+OdhftLFA8=; b=gK0vNIRWR0Bd/oGjPekjjj5r/VR7TlbN6IFBpBybMqZuvmw/2DgW9nv/DGGB3HH/oj ITesYcuhDkGWHykwT/6hPyy/ZGss3iSFjBfkKh5VTsRls19YqVFHOfwU+vKFglBjsCkB 2h5pk0+afJ5fC2YXxP++OVBF8U1mevFbekzzx0qZsLfgsRORWZUEsL1YCZ7HHvBpfe0O sI1ZDNVrOMfvygAQ8C6I4fuzfNyFTU9d0vuZ3Z+oiyjCpifadqyH2BoGZLLVP+YYlMaE +4CzADY2CiQrI7VRo0gS74X7hZ+sn2NTBc9K28iWUsEDVnT29ZAGMRN/6CSNrslu5RSz PkQw== X-Gm-Message-State: ALoCoQluUiIoCo2qDSD/APAXC56MR9KW8ok34G9DuoB1dTWRwzvICbOu6yyNwmidPveqFtSAZBz+ X-Received: by 10.236.207.164 with SMTP id n24mr7034237yho.5.1409916502145; Fri, 05 Sep 2014 04:28:22 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.17.47 with SMTP id 44ls695606qgc.11.gmail; Fri, 05 Sep 2014 04:28:22 -0700 (PDT) X-Received: by 10.52.121.74 with SMTP id li10mr8045907vdb.19.1409916502036; Fri, 05 Sep 2014 04:28:22 -0700 (PDT) Received: from mail-vc0-f169.google.com (mail-vc0-f169.google.com [209.85.220.169]) by mx.google.com with ESMTPS id ql18si318879vdb.84.2014.09.05.04.28.22 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 05 Sep 2014 04:28:22 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.169 as permitted sender) client-ip=209.85.220.169; Received: by mail-vc0-f169.google.com with SMTP id hq11so12388673vcb.28 for ; Fri, 05 Sep 2014 04:28:22 -0700 (PDT) X-Received: by 10.221.66.194 with SMTP id xr2mr74185vcb.65.1409916501943; Fri, 05 Sep 2014 04:28:21 -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.45.67 with SMTP id uj3csp67717vcb; Fri, 5 Sep 2014 04:28:21 -0700 (PDT) X-Received: by 10.224.98.207 with SMTP id r15mr7615314qan.23.1409916501066; Fri, 05 Sep 2014 04:28:21 -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 t5si1147560qat.73.2014.09.05.04.28.20 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Fri, 05 Sep 2014 04:28:21 -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 1XPrgg-0007Dj-OM; Fri, 05 Sep 2014 11:28:18 +0000 Received: from mail-qc0-f169.google.com ([209.85.216.169]) by ip-10-141-164-156.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1XPrgZ-0007DW-PO for lng-odp@lists.linaro.org; Fri, 05 Sep 2014 11:28:11 +0000 Received: by mail-qc0-f169.google.com with SMTP id l6so12269815qcy.14 for ; Fri, 05 Sep 2014 04:28:06 -0700 (PDT) X-Received: by 10.224.151.69 with SMTP id b5mr17352225qaw.37.1409916486413; Fri, 05 Sep 2014 04:28:06 -0700 (PDT) Received: from localhost.localdomain (ec2-23-23-178-99.compute-1.amazonaws.com. [23.23.178.99]) by mx.google.com with ESMTPSA id i17sm597119qay.47.2014.09.05.04.28.03 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 05 Sep 2014 04:28:04 -0700 (PDT) From: Petri Savolainen To: lng-odp@lists.linaro.org Date: Fri, 5 Sep 2014 14:29:10 +0300 Message-Id: <1409916551-24327-1-git-send-email-petri.savolainen@linaro.org> X-Mailer: git-send-email 2.1.0 X-Topics: patch Subject: [lng-odp] [PATCH v2 1/2] Shared memory reserve flag 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: petri.savolainen@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.169 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 flags parameter to reserve call. * SW_ONLY: optimize memory usage when HW accelerators are not involved. * PROC: for communication with external (non-ODP) processes in the system Signed-off-by: Petri Savolainen --- .../linux-generic/include/api/odp_shared_memory.h | 15 +++++++- platform/linux-generic/odp_shared_memory.c | 45 +++++++++++++++++----- platform/linux-keystone2/odp_shared_memory.c | 5 ++- 3 files changed, 53 insertions(+), 12 deletions(-) diff --git a/platform/linux-generic/include/api/odp_shared_memory.h b/platform/linux-generic/include/api/odp_shared_memory.h index 8ac8847..7d9fedd 100644 --- a/platform/linux-generic/include/api/odp_shared_memory.h +++ b/platform/linux-generic/include/api/odp_shared_memory.h @@ -21,9 +21,18 @@ extern "C" { #include -/** Maximum shared memory block name lenght in chars */ +/** Maximum shared memory block name length in chars */ #define ODP_SHM_NAME_LEN 32 +/* + * Shared memory flags + */ + +/* Share level */ +#define ODP_SHM_SW_ONLY 0x1 /**< Application SW only, no HW access */ +#define ODP_SHM_PROC 0x2 /**< Share with external processes */ + + /** * Reserve a block of shared memory @@ -31,10 +40,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 flags Shared mem parameter flags (ODP_SHM_*). Default value is 0. * * @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, + uint32_t flags); /** * Lookup for a block of shared memory diff --git a/platform/linux-generic/odp_shared_memory.c b/platform/linux-generic/odp_shared_memory.c index 784f42b..3d1e12a 100644 --- a/platform/linux-generic/odp_shared_memory.c +++ b/platform/linux-generic/odp_shared_memory.c @@ -11,6 +11,7 @@ #include #include +#include #include #include #include @@ -44,8 +45,6 @@ typedef struct { #define MAP_ANONYMOUS MAP_ANON #endif -#define SHM_FLAGS (MAP_SHARED | MAP_ANONYMOUS) - /* Global shared memory table */ static odp_shm_table_t *odp_shm_tbl; @@ -60,7 +59,7 @@ int odp_shm_init_global(void) #endif addr = mmap(NULL, sizeof(odp_shm_table_t), - PROT_READ | PROT_WRITE, SHM_FLAGS, -1, 0); + PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); if (addr == MAP_FAILED) return -1; @@ -95,11 +94,17 @@ 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, + uint32_t flags) { int i; odp_shm_block_t *block; void *addr; + int fd = -1; + int map_flag = MAP_SHARED; + /* If already exists: O_EXCL: error, O_TRUNC: truncate to zero */ + int oflag = O_RDWR | O_CREAT | O_TRUNC; + uint64_t alloc_size = size + align; #ifdef MAP_HUGETLB uint64_t huge_sz, page_sz; @@ -107,11 +112,31 @@ void *odp_shm_reserve(const char *name, uint64_t size, uint64_t align) page_sz = odp_sys_page_size(); #endif + if (flags & ODP_SHM_PROC) { + /* Creates a file to /dev/shm */ + fd = shm_open(name, oflag, + S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + + if (fd == -1) { + ODP_DBG("odp_shm_reserve: shm_open failed\n"); + return NULL; + } + + if (ftruncate(fd, alloc_size) == -1) { + ODP_DBG("odp_shm_reserve: ftruncate failed\n"); + return NULL; + } + + } else { + map_flag |= MAP_ANONYMOUS; + } + odp_spinlock_lock(&odp_shm_tbl->lock); if (find_block(name) >= 0) { /* Found a block with the same name */ odp_spinlock_unlock(&odp_shm_tbl->lock); + ODP_DBG("odp_shm_reserve: name already used\n"); return NULL; } @@ -125,6 +150,7 @@ void *odp_shm_reserve(const char *name, uint64_t size, uint64_t align) if (i > ODP_SHM_NUM_BLOCKS - 1) { /* Table full */ odp_spinlock_unlock(&odp_shm_tbl->lock); + ODP_DBG("odp_shm_reserve: no more blocks\n"); return NULL; } @@ -135,16 +161,16 @@ void *odp_shm_reserve(const char *name, uint64_t size, uint64_t align) #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); + if (huge_sz && alloc_size > page_sz) { + addr = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, + map_flag | MAP_HUGETLB, fd, 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); + addr = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, + map_flag, fd, 0); } else { block->huge = 1; @@ -153,6 +179,7 @@ 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); + ODP_DBG("odp_shm_reserve: mmap failed\n"); return NULL; } diff --git a/platform/linux-keystone2/odp_shared_memory.c b/platform/linux-keystone2/odp_shared_memory.c index e595111..69ac34e 100644 --- a/platform/linux-keystone2/odp_shared_memory.c +++ b/platform/linux-keystone2/odp_shared_memory.c @@ -208,8 +208,11 @@ 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, + uint32_t flags) { + (void)flags; + return _odp_shm_reserve(name, size, align, ODP_SHM_CMA); }