From patchwork Fri Jan 23 12:34:34 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Uvarov X-Patchwork-Id: 43635 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wi0-f197.google.com (mail-wi0-f197.google.com [209.85.212.197]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id AAB16240D9 for ; Fri, 23 Jan 2015 12:35:37 +0000 (UTC) Received: by mail-wi0-f197.google.com with SMTP id n3sf1243031wiv.0 for ; Fri, 23 Jan 2015 04:35:37 -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:in-reply-to :references: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=/+AxfTPXiDAu7xqBY4Xx5PCJGk+lcjbhTuIdKbZAaJ4=; b=Xr0sHrS6jBOyIL/A6YSnTCs0fcwNTKNpoAZAu/FzX8C0oz81qulPI2RbbXcvxPk1I3 TtFLwp/5tMJsk1x5yyCGsD/RHe8ROiJzNFfcrinnd4jZdn+bRzj47tWy3Ps0vrPRRKRX zcjSjwWR3Qs4ZCBemeceqegrlaWfvjQinhZySzsNf0cY/yBBXWLheQg/Zr11uv21rS6Y UdB6m0hJlGuB5KE8AcoQwsGIY1RvKdSWrdgZK1TJ+LPbyaAwlj9U1xeg8GFjq9r6i2Yp 0Tt+fgiSNL2zy9c2ramu9D2+jE40fykbXEcsR+QKaknBICUp6dNiciMeZn1eMk+jRpf3 vPNQ== X-Gm-Message-State: ALoCoQlspEcREX73wDuyke8610KzIWC65Y0dB+kO8n37m7uk6CM87hd9DyF1VizNAAljq/G9iXua X-Received: by 10.180.88.66 with SMTP id be2mr259609wib.1.1422016537009; Fri, 23 Jan 2015 04:35:37 -0800 (PST) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.5.74 with SMTP id q10ls282804laq.43.gmail; Fri, 23 Jan 2015 04:35:36 -0800 (PST) X-Received: by 10.112.132.2 with SMTP id oq2mr7071778lbb.11.1422016536592; Fri, 23 Jan 2015 04:35:36 -0800 (PST) Received: from mail-la0-f51.google.com (mail-la0-f51.google.com. [209.85.215.51]) by mx.google.com with ESMTPS id qg8si1193967lbb.139.2015.01.23.04.35.36 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 23 Jan 2015 04:35:36 -0800 (PST) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.51 as permitted sender) client-ip=209.85.215.51; Received: by mail-la0-f51.google.com with SMTP id ge10so7017815lab.10 for ; Fri, 23 Jan 2015 04:35:36 -0800 (PST) X-Received: by 10.112.30.71 with SMTP id q7mr7059707lbh.41.1422016536470; Fri, 23 Jan 2015 04:35:36 -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.9.200 with SMTP id c8csp236727lbb; Fri, 23 Jan 2015 04:35:35 -0800 (PST) X-Received: by 10.140.93.21 with SMTP id c21mr12912653qge.0.1422016535016; Fri, 23 Jan 2015 04:35:35 -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 j3si1734347qge.80.2015.01.23.04.35.33 (version=TLSv1 cipher=RC4-SHA bits=128/128); Fri, 23 Jan 2015 04:35:35 -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 1YEdSW-0006IO-Cv; Fri, 23 Jan 2015 12:35:32 +0000 Received: from mail-la0-f47.google.com ([209.85.215.47]) by ip-10-35-177-41.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1YEdSG-00068f-Oa for lng-odp@lists.linaro.org; Fri, 23 Jan 2015 12:35:16 +0000 Received: by mail-la0-f47.google.com with SMTP id hz20so7014308lab.6 for ; Fri, 23 Jan 2015 04:35:10 -0800 (PST) X-Received: by 10.112.52.229 with SMTP id w5mr7100184lbo.52.1422016510814; Fri, 23 Jan 2015 04:35:10 -0800 (PST) Received: from localhost.localdomain ([188.92.105.210]) by mx.google.com with ESMTPSA id qg5sm410897lbb.42.2015.01.23.04.35.09 (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 23 Jan 2015 04:35:09 -0800 (PST) From: Maxim Uvarov To: lng-odp@lists.linaro.org Date: Fri, 23 Jan 2015 15:34:34 +0300 Message-Id: <1422016474-8676-3-git-send-email-maxim.uvarov@linaro.org> X-Mailer: git-send-email 1.8.5.1.163.gd7aced9 In-Reply-To: <1422016474-8676-1-git-send-email-maxim.uvarov@linaro.org> References: <1422016474-8676-1-git-send-email-maxim.uvarov@linaro.org> X-Topics: patch Subject: [lng-odp] [PATCHv4 3/3] 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.51 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 --- v4: if alloc huge pages failed fall back to normal pages with original size. platform/linux-generic/odp_shared_memory.c | 43 +++++++++++++++++++++--------- test/validation/odp_shm.c | 4 +++ 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/platform/linux-generic/odp_shared_memory.c b/platform/linux-generic/odp_shared_memory.c index 23a9ceb..2004f30 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 alloc_hp_size; uint64_t page_sz, huge_sz; + int need_huge_page = 0; - 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); + if (need_huge_page) { + /* 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; } @@ -235,18 +240,31 @@ odp_shm_t odp_shm_reserve(const char *name, uint64_t size, uint64_t align, #ifdef MAP_HUGETLB /* Try first huge pages */ - if (huge_sz && alloc_size > page_sz) { - addr = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, + if (need_huge_page) { + if (flags & ODP_SHM_PROC) { + if (ftruncate(fd, alloc_hp_size) == -1) + goto normal_pages; + } + addr = mmap(NULL, alloc_hp_size, PROT_READ | PROT_WRITE, map_flag | MAP_HUGETLB, fd, 0); } +normal_pages: #endif - /* Use normal pages for small or failed huge page allocations */ if (addr == MAP_FAILED) { + if (flags & ODP_SHM_PROC) { + if (ftruncate(fd, alloc_size) == -1) { + odp_spinlock_unlock(&odp_shm_tbl->lock); + ODP_DBG("odp_shm_reserve: ftruncate failed\n"); + return ODP_SHM_INVALID; + } + } addr = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, map_flag, fd, 0); + block->alloc_size = alloc_size; block->page_sz = page_sz; } else { + block->alloc_size = alloc_hp_size; block->huge = 1; block->page_sz = huge_sz; } @@ -267,7 +285,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);