From patchwork Mon Sep 1 11:53:04 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petri Savolainen X-Patchwork-Id: 36349 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-ig0-f200.google.com (mail-ig0-f200.google.com [209.85.213.200]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 229A420792 for ; Mon, 1 Sep 2014 11:52:23 +0000 (UTC) Received: by mail-ig0-f200.google.com with SMTP id uq10sf29755826igb.3 for ; Mon, 01 Sep 2014 04:52: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=SBLsrSoZ7DohbSYNS36z196IYMNfPYX5JJslVZgf2eY=; b=jYvJXGHqbOfALqo+Osii6p8hGUJntzrgry7C36N9eyMflJ33H5zIqPb5NRfarG/SAv DBHHCQOmBY1XaYpnDuFPt196m9aNfjaprKX9x5v2Kv8bagxADmtfmGM32eYDjME4jOLL k+U8zWyh5LMHuLtEHFXHmNFJZJTtI5IU1Q273Hn6qDl0hRM5FW+b7xJQWEDKieH+eKYj 9NL5XRAZ6MuOOT///9sd8DAG/jDHQsVU/sjGVEPieBH0JHWxK0QyGwlhJcP2nmfczLQN t2HUcluQ3w0AB3gs/kUPgFtY/afskysse9brrbtc0OaCLkXd4oviiGRtjTkuUzS08esa PxjQ== X-Gm-Message-State: ALoCoQnsMvw1PBlsKQvK/+Rr8DIM3I7kczpZcSA3IJaCLJ41XYDMqh/DkN/EVAgkFkz6MAPAnJYr X-Received: by 10.50.73.162 with SMTP id m2mr15678502igv.1.1409572342746; Mon, 01 Sep 2014 04:52:22 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.34.99 with SMTP id k90ls1963946qgk.78.gmail; Mon, 01 Sep 2014 04:52:22 -0700 (PDT) X-Received: by 10.220.200.195 with SMTP id ex3mr85787vcb.63.1409572342660; Mon, 01 Sep 2014 04:52: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 vm1si216385vdc.29.2014.09.01.04.52.22 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 01 Sep 2014 04:52: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 hq11so5369802vcb.14 for ; Mon, 01 Sep 2014 04:52:22 -0700 (PDT) X-Received: by 10.52.162.74 with SMTP id xy10mr87135vdb.51.1409572342560; Mon, 01 Sep 2014 04:52:22 -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 uj3csp375771vcb; Mon, 1 Sep 2014 04:52:22 -0700 (PDT) X-Received: by 10.140.97.131 with SMTP id m3mr40701566qge.80.1409572341972; Mon, 01 Sep 2014 04:52: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 b7si657000qan.20.2014.09.01.04.52.21 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Mon, 01 Sep 2014 04:52: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 1XOQ9i-00041E-Hu; Mon, 01 Sep 2014 11:52:18 +0000 Received: from mail-qc0-f179.google.com ([209.85.216.179]) by ip-10-141-164-156.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1XOQ9b-0003yN-Uh for lng-odp@lists.linaro.org; Mon, 01 Sep 2014 11:52:11 +0000 Received: by mail-qc0-f179.google.com with SMTP id l6so5197971qcy.24 for ; Mon, 01 Sep 2014 04:52:06 -0700 (PDT) X-Received: by 10.140.94.163 with SMTP id g32mr41968512qge.77.1409572326730; Mon, 01 Sep 2014 04:52: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 b8sm1538663qat.21.2014.09.01.04.52.04 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 01 Sep 2014 04:52:05 -0700 (PDT) From: Petri Savolainen To: lng-odp@lists.linaro.org Date: Mon, 1 Sep 2014 14:53:04 +0300 Message-Id: <1409572385-6169-1-git-send-email-petri.savolainen@linaro.org> X-Mailer: git-send-email 2.1.0 X-Topics: patch Subject: [lng-odp] [PATCH 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 other (non-ODP) processes in the system * KERNEL: for communication with a kernel (OS) driver Signed-off-by: Petri Savolainen --- .../linux-generic/include/api/odp_shared_memory.h | 13 ++++++- platform/linux-generic/odp_shared_memory.c | 45 +++++++++++++++++----- 2 files changed, 48 insertions(+), 10 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..814acdc 100644 --- a/platform/linux-generic/include/api/odp_shared_memory.h +++ b/platform/linux-generic/include/api/odp_shared_memory.h @@ -24,6 +24,15 @@ extern "C" { /** Maximum shared memory block name lenght in chars */ #define ODP_SHM_NAME_LEN 32 +/* + * Shared memory flags + */ + +/* Share level */ +#define ODP_SHM_SW_ONLY 0x1 /**< Internal to application SW, no HW access */ +#define ODP_SHM_PROC 0x2 /**< Share with external processes */ +#define ODP_SHM_KERNEL 0x4 /**< Share with kernel */ + /** * 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 Block parameter flags * * @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..4cb82b5 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; + /* O_EXCL: error, O_TRUNC: truncate to zero, if already exists */ + 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; }