From patchwork Wed Sep 17 23:05:54 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Uvarov X-Patchwork-Id: 37552 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wg0-f72.google.com (mail-wg0-f72.google.com [74.125.82.72]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 97E672054D for ; Wed, 17 Sep 2014 23:06:33 +0000 (UTC) Received: by mail-wg0-f72.google.com with SMTP id m15sf24854wgh.7 for ; Wed, 17 Sep 2014 16:06:32 -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=+knha2WnizAb3dYM8mr0VsmqZj+GiERFsbUBGOTMBOk=; b=eZiICgTEx0Ru07anKpKh5FRB5I2aH/3MbJFgVyyNpXb80Oc6EO2YKI1BoOqxIAtI7V rykiEdcAOCsXkhod4YYaXTLWRChhCZnHU2MAlfrOyqTVokfOf4BvTtjAgZ8OwSFjMYdR +JmRxd+JKDsK0Kv0PNQxeTCcy9sDved/br4WiY4E9VACojaXXduARnDFhNDAPtZ8qGUr bQ+Egzw3MbvQKy/swhaH8cv5Od8DwtEdOYR+cpyvpEw7POczDakcD6M7UftHa5ssyEhK cbfWIG/KssnZlYVbEUjqRKsigRaHwX/1mYsQV6GkojfmsNDQs2Gpz+AdTitN3nyGFQoy 3AIw== X-Gm-Message-State: ALoCoQkTKCpNUzE7I741K8hE0bdQE3bbscwwqM7YOqyYWhowwdmk5Gru4m9XuhcORYevy5HF9PQQ X-Received: by 10.180.221.169 with SMTP id qf9mr238628wic.5.1410995192834; Wed, 17 Sep 2014 16:06:32 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.23.200 with SMTP id o8ls175795laf.89.gmail; Wed, 17 Sep 2014 16:06:32 -0700 (PDT) X-Received: by 10.152.4.39 with SMTP id h7mr689225lah.49.1410995192580; Wed, 17 Sep 2014 16:06:32 -0700 (PDT) Received: from mail-lb0-f176.google.com (mail-lb0-f176.google.com [209.85.217.176]) by mx.google.com with ESMTPS id jp6si1271396lbc.37.2014.09.17.16.06.32 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 17 Sep 2014 16:06:32 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.176 as permitted sender) client-ip=209.85.217.176; Received: by mail-lb0-f176.google.com with SMTP id z11so61240lbi.21 for ; Wed, 17 Sep 2014 16:06:32 -0700 (PDT) X-Received: by 10.152.10.74 with SMTP id g10mr696101lab.41.1410995192508; Wed, 17 Sep 2014 16:06:32 -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.112.130.169 with SMTP id of9csp668011lbb; Wed, 17 Sep 2014 16:06:31 -0700 (PDT) X-Received: by 10.224.128.9 with SMTP id i9mr1190658qas.50.1410995190630; Wed, 17 Sep 2014 16:06:30 -0700 (PDT) Received: from ip-10-35-177-41.ec2.internal (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTPS id s9si24175279qan.84.2014.09.17.16.06.30 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Wed, 17 Sep 2014 16:06:30 -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-35-177-41.ec2.internal) by ip-10-35-177-41.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1XUOIv-0005gA-3h; Wed, 17 Sep 2014 23:06:29 +0000 Received: from mail-pa0-f43.google.com ([209.85.220.43]) by ip-10-35-177-41.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1XUOIi-0005fP-2r for lng-odp@lists.linaro.org; Wed, 17 Sep 2014 23:06:16 +0000 Received: by mail-pa0-f43.google.com with SMTP id fa1so79868pad.16 for ; Wed, 17 Sep 2014 16:06:10 -0700 (PDT) X-Received: by 10.66.124.202 with SMTP id mk10mr489026pab.1.1410995170216; Wed, 17 Sep 2014 16:06:10 -0700 (PDT) Received: from localhost.localdomain (70-35-38-154.static.wiline.com. [70.35.38.154]) by mx.google.com with ESMTPSA id f1sm17894333pdn.62.2014.09.17.16.06.09 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 17 Sep 2014 16:06:09 -0700 (PDT) From: Maxim Uvarov To: lng-odp@lists.linaro.org Date: Thu, 18 Sep 2014 03:05:54 +0400 Message-Id: <1410995157-6905-2-git-send-email-maxim.uvarov@linaro.org> X-Mailer: git-send-email 1.8.5.1.163.gd7aced9 In-Reply-To: <1410995157-6905-1-git-send-email-maxim.uvarov@linaro.org> References: <1410995157-6905-1-git-send-email-maxim.uvarov@linaro.org> X-Topics: patch Subject: [lng-odp] [PATCH 1/4] 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: 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.217.176 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 From: Petri Savolainen 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 Signed-off-by: Maxim Uvarov Acked-by: Petri Savolainen --- .../linux-generic/include/api/odp_shared_memory.h | 15 +++++++- platform/linux-generic/odp_shared_memory.c | 45 +++++++++++++++++----- 2 files changed, 49 insertions(+), 11 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; }