From patchwork Mon Jan 26 12:54:31 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Uvarov X-Patchwork-Id: 43739 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-ee0-f70.google.com (mail-ee0-f70.google.com [74.125.83.70]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 5B86423FFF for ; Mon, 26 Jan 2015 12:54:58 +0000 (UTC) Received: by mail-ee0-f70.google.com with SMTP id c13sf4147152eek.1 for ; Mon, 26 Jan 2015 04:54:57 -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=4VObkSvneb3UrBR7UuutuaYmKAY12fqzHRhGzgmjuXo=; b=VFgiHhQxp+CKH/j8xH7vu0H0OVl0YD0QPET8SdN98DrKsz1Hps7TItV7eLB7PXiN6n 4rvB6r8WqV+K+YXv0QrE3pgkDjPuI2g7mrCDIyv4ir7cQbSWjfOleGKopzbNH4hdIeEO 1vV1sqnBY8nXWT48THQ3xO7AKS7yZWwQMVwI5nZAK6P2fUCDoEgD/np4RFxyIR+XSND6 xIER8dziynw/lKsRzspq5oFoAfvZwbYLkAINk7t5nAHxJeJK9zbx+7x9bDin37kZWK2r 6cNLBSSmRmALQVmwsHQ8wo7JqES5ODvH7vSmDTEVMXgckQYllJsbo8IBpA4QEAfzLKeV W3YQ== X-Gm-Message-State: ALoCoQkYRrlyJ/MXt9QiPf5i1S78MdKZ9qYyyW/P2scDJow1jbI47HU2u+Vp6dvzxDMF67rcuTwr X-Received: by 10.194.241.163 with SMTP id wj3mr1759008wjc.5.1422276897588; Mon, 26 Jan 2015 04:54:57 -0800 (PST) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.44.162 with SMTP id f2ls13995lam.5.gmail; Mon, 26 Jan 2015 04:54:57 -0800 (PST) X-Received: by 10.152.4.8 with SMTP id g8mr20731337lag.58.1422276897417; Mon, 26 Jan 2015 04:54:57 -0800 (PST) Received: from mail-la0-f41.google.com (mail-la0-f41.google.com. [209.85.215.41]) by mx.google.com with ESMTPS id ss9si8927145lbb.89.2015.01.26.04.54.57 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 26 Jan 2015 04:54:57 -0800 (PST) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.41 as permitted sender) client-ip=209.85.215.41; Received: by mail-la0-f41.google.com with SMTP id gm9so7495576lab.0 for ; Mon, 26 Jan 2015 04:54:57 -0800 (PST) X-Received: by 10.152.203.230 with SMTP id kt6mr4287814lac.38.1422276896955; Mon, 26 Jan 2015 04:54:56 -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 h5csp770746lbj; Mon, 26 Jan 2015 04:54:56 -0800 (PST) X-Received: by 10.140.41.113 with SMTP id y104mr38373328qgy.51.1422276895610; Mon, 26 Jan 2015 04:54:55 -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 u47si13115196qge.5.2015.01.26.04.54.52 (version=TLSv1 cipher=RC4-SHA bits=128/128); Mon, 26 Jan 2015 04:54:55 -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 1YFjBq-0003Yb-BX; Mon, 26 Jan 2015 12:54:50 +0000 Received: from mail-lb0-f177.google.com ([209.85.217.177]) by ip-10-35-177-41.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1YFjBj-0003WY-Nb for lng-odp@lists.linaro.org; Mon, 26 Jan 2015 12:54:43 +0000 Received: by mail-lb0-f177.google.com with SMTP id p9so7356133lbv.8 for ; Mon, 26 Jan 2015 04:54:37 -0800 (PST) X-Received: by 10.152.3.195 with SMTP id e3mr21113245lae.8.1422276877809; Mon, 26 Jan 2015 04:54:37 -0800 (PST) Received: from localhost.localdomain (broadband-46-188-125-104.2com.net. [46.188.125.104]) by mx.google.com with ESMTPSA id ba3sm2790754lbc.35.2015.01.26.04.54.36 (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 26 Jan 2015 04:54:36 -0800 (PST) From: Maxim Uvarov To: lng-odp@lists.linaro.org Date: Mon, 26 Jan 2015 15:54:31 +0300 Message-Id: <1422276871-9233-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] [PATCHv6] 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.215.41 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 --- v6: error if ftuncate fails in HP case. platform/linux-generic/odp_shared_memory.c | 73 +++++++++++++++++++----------- test/validation/odp_shm.c | 4 ++ 2 files changed, 51 insertions(+), 26 deletions(-) diff --git a/platform/linux-generic/odp_shared_memory.c b/platform/linux-generic/odp_shared_memory.c index 23a9ceb..515a26f 100644 --- a/platform/linux-generic/odp_shared_memory.c +++ b/platform/linux-generic/odp_shared_memory.c @@ -179,27 +179,31 @@ 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; +#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 +234,50 @@ 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) { + if ((flags & ODP_SHM_PROC) && + (ftruncate(fd, alloc_hp_size) == -1)) { + odp_spinlock_unlock(&odp_shm_tbl->lock); + ODP_DBG("odp_shm_reserve: ftruncate HP failed\n"); + return ODP_SHM_INVALID; + } + + 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 ((flags & ODP_SHM_PROC) && + (ftruncate(fd, alloc_size) == -1)) { + odp_spinlock_unlock(&odp_shm_tbl->lock); + ODP_ERR("odp_shm_reserve: ftruncate failed\n"); + return ODP_SHM_INVALID; + } - 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; + 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 +289,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);