From patchwork Mon Jan 26 12:13:11 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Uvarov X-Patchwork-Id: 43736 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-we0-f197.google.com (mail-we0-f197.google.com [74.125.82.197]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id C2CC620D5D for ; Mon, 26 Jan 2015 12:13:40 +0000 (UTC) Received: by mail-we0-f197.google.com with SMTP id l61sf4052813wev.0 for ; Mon, 26 Jan 2015 04:13:40 -0800 (PST) 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:content-type :content-transfer-encoding:errors-to:sender:x-original-sender :x-original-authentication-results:mailing-list; bh=ZkuMBWqaR164bzmZyO7I92Fk0knAk1y9FwqYRlymnH0=; b=btjwI9JqvUtFnIpoOk91ZqTiLSfhkeoIJfqwFoskVWdPVNuCYIwIvf2cScfbcnK18V YQMpf3QHYxeno2AcR0p1aLglXUoiov0l8GNITGvgi4eBYzDvysmqKGLc3M4cSlPEnTrO p9aoFXtfnzhEhf7/q1VH32Dy1JjoQ0G7ATaQ6RvPO2B+vEp9eDxT1ATV49BpMVdB2N34 FujIB4GLC4X4cmjhFYGuYM7s6kw+u0aLMu+9N7vQrJ5y/vct+JgClIN3rXPtH7lU9Xnm aNcbRLPlTE7nSUsaIeX0yQZII+ZfG4cYxzKLFavWhsUJygskSsbMUyjZ51irtTcxuto4 l81Q== X-Gm-Message-State: ALoCoQkI2GDUEOGuheyqjsmSPAxHlKCVXk75K9GYNUYDf2of/9vUJbZnj2cXI5DHsV1juUVX+OU1 X-Received: by 10.152.36.168 with SMTP id r8mr2503701laj.2.1422274419962; Mon, 26 Jan 2015 04:13:39 -0800 (PST) X-BeenThere: patchwork-forward@linaro.org Received: by 10.153.11.6 with SMTP id ee6ls519515lad.7.gmail; Mon, 26 Jan 2015 04:13:39 -0800 (PST) X-Received: by 10.152.4.200 with SMTP id m8mr2450477lam.2.1422274419812; Mon, 26 Jan 2015 04:13:39 -0800 (PST) Received: from mail-lb0-f178.google.com (mail-lb0-f178.google.com. [209.85.217.178]) by mx.google.com with ESMTPS id pk3si8858636lbc.70.2015.01.26.04.13.39 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 26 Jan 2015 04:13:39 -0800 (PST) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.178 as permitted sender) client-ip=209.85.217.178; Received: by mail-lb0-f178.google.com with SMTP id u10so7186347lbd.9 for ; Mon, 26 Jan 2015 04:13:39 -0800 (PST) X-Received: by 10.112.164.101 with SMTP id yp5mr12216130lbb.82.1422274419687; Mon, 26 Jan 2015 04:13:39 -0800 (PST) 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.35.133 with SMTP id h5csp760192lbj; Mon, 26 Jan 2015 04:13:38 -0800 (PST) X-Received: by 10.229.172.196 with SMTP id m4mr40571435qcz.19.1422274417833; Mon, 26 Jan 2015 04:13:37 -0800 (PST) Received: from ip-10-35-177-41.ec2.internal (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTPS id x5si8805978qad.71.2015.01.26.04.13.36 (version=TLSv1 cipher=RC4-SHA bits=128/128); Mon, 26 Jan 2015 04:13:37 -0800 (PST) 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 1YFiXt-0007yS-H1; Mon, 26 Jan 2015 12:13:33 +0000 Received: from mail-la0-f44.google.com ([209.85.215.44]) by ip-10-35-177-41.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1YFiXo-0007yL-2c for lng-odp@lists.linaro.org; Mon, 26 Jan 2015 12:13:28 +0000 Received: by mail-la0-f44.google.com with SMTP id s18so7312585lam.3 for ; Mon, 26 Jan 2015 04:13:22 -0800 (PST) X-Received: by 10.112.164.101 with SMTP id yp5mr12214978lbb.82.1422274402052; Mon, 26 Jan 2015 04:13:22 -0800 (PST) Received: from localhost.localdomain (broadband-46-188-125-104.2com.net. [46.188.125.104]) by mx.google.com with ESMTPSA id db7sm2769297lad.16.2015.01.26.04.13.20 (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 26 Jan 2015 04:13:20 -0800 (PST) From: Maxim Uvarov To: lng-odp@lists.linaro.org Date: Mon, 26 Jan 2015 15:13:11 +0300 Message-Id: <1422274391-18907-1-git-send-email-maxim.uvarov@linaro.org> X-Mailer: git-send-email 1.8.5.1.163.gd7aced9 X-Topics: patch Subject: [lng-odp] [PATCHv5] hugepages: align mmap size for hugepages 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.178 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 In case of hugepages munmap requires size aligned to page. Signed-off-by: Maxim Uvarov --- v5: - fix alloc_hp_size define (somehow arm gcc finds that it can be uninitialized; - remove goto and make if cases more readable; platform/linux-generic/odp_shared_memory.c | 73 +++++++++++++++++++----------- test/validation/odp_shm.c | 4 ++ 2 files changed, 50 insertions(+), 27 deletions(-) diff --git a/platform/linux-generic/odp_shared_memory.c b/platform/linux-generic/odp_shared_memory.c index 23a9ceb..4689c5d 100644 --- a/platform/linux-generic/odp_shared_memory.c +++ b/platform/linux-generic/odp_shared_memory.c @@ -179,27 +179,32 @@ odp_shm_t odp_shm_reserve(const char *name, uint64_t size, uint64_t align, 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; + uint64_t alloc_size; uint64_t page_sz, huge_sz; + int ret; +#ifdef MAP_HUGETLB + int need_huge_page = 0; + uint64_t alloc_hp_size; +#endif - huge_sz = odp_sys_huge_page_size(); page_sz = odp_sys_page_size(); + alloc_size = size + align; + +#ifdef MAP_HUGETLB + huge_sz = odp_sys_huge_page_size(); + need_huge_page = (huge_sz && alloc_size > page_sz); + /* munmap for huge pages requires sizes round up by page */ + alloc_hp_size = (size + align + (huge_sz - 1)) & (-huge_sz); +#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 ODP_SHM_INVALID; } - - if (ftruncate(fd, alloc_size) == -1) { - ODP_DBG("odp_shm_reserve: ftruncate failed\n"); - return ODP_SHM_INVALID; - } - } else { map_flag |= MAP_ANONYMOUS; } @@ -230,32 +235,47 @@ odp_shm_t odp_shm_reserve(const char *name, uint64_t size, uint64_t align, block = &odp_shm_tbl->block[i]; block->hdl = to_handle(i); - block->huge = 0; addr = MAP_FAILED; #ifdef MAP_HUGETLB /* Try first huge pages */ - if (huge_sz && alloc_size > page_sz) { - addr = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, - map_flag | MAP_HUGETLB, fd, 0); + if (need_huge_page) { + ret = 0; + if (flags & ODP_SHM_PROC) + ret = ftruncate(fd, alloc_hp_size); + + if (ret == 0) { + addr = mmap(NULL, alloc_hp_size, PROT_READ | PROT_WRITE, + map_flag | MAP_HUGETLB, fd, 0); + if (addr == MAP_FAILED) { + ODP_DBG("odp_shm_reserve: mmap HP failed\n"); + } else { + block->alloc_size = alloc_hp_size; + block->huge = 1; + block->page_sz = huge_sz; + } + } } #endif /* Use normal pages for small or failed huge page allocations */ if (addr == MAP_FAILED) { - addr = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, - map_flag, fd, 0); - block->page_sz = page_sz; - } else { - block->huge = 1; - block->page_sz = huge_sz; - } - - if (addr == MAP_FAILED) { - /* Alloc failed */ - odp_spinlock_unlock(&odp_shm_tbl->lock); - ODP_DBG("odp_shm_reserve: mmap failed\n"); - return ODP_SHM_INVALID; + ret = 0; + if (flags & ODP_SHM_PROC) + ret = ftruncate(fd, alloc_size); + if (ret == 0) { + addr = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, + map_flag, fd, 0); + if (addr == MAP_FAILED) { + odp_spinlock_unlock(&odp_shm_tbl->lock); + ODP_DBG("odp_shm_reserve: mmap failed\n"); + return ODP_SHM_INVALID; + } else { + block->alloc_size = alloc_size; + block->huge = 0; + block->page_sz = page_sz; + } + } } block->addr_orig = addr; @@ -267,7 +287,6 @@ odp_shm_t odp_shm_reserve(const char *name, uint64_t size, uint64_t align, block->name[ODP_SHM_NAME_LEN - 1] = 0; block->size = size; block->align = align; - block->alloc_size = alloc_size; block->flags = flags; block->fd = fd; block->addr = addr; diff --git a/test/validation/odp_shm.c b/test/validation/odp_shm.c index c26925b..4b1a38e 100644 --- a/test/validation/odp_shm.c +++ b/test/validation/odp_shm.c @@ -32,7 +32,11 @@ static void *run_shm_thread(void *arg) CU_ASSERT(0 == info.flags); CU_ASSERT(test_shared_data == info.addr); CU_ASSERT(sizeof(test_shared_data_t) <= info.size); +#ifdef MAP_HUGETLB + CU_ASSERT(odp_sys_huge_page_size() == info.page_size); +#else CU_ASSERT(odp_sys_page_size() == info.page_size); +#endif odp_shm_print_all(); fflush(stdout);