From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by dpdk.org (Postfix) with ESMTP id 71BC6A498 for ; Fri, 27 Apr 2018 18:38:49 +0200 (CEST) X-Amp-Result: UNSCANNABLE X-Amp-File-Uploaded: False Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 27 Apr 2018 09:38:46 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.49,335,1520924400"; d="scan'208";a="41148978" Received: from kpaliswi-mobl.ger.corp.intel.com ([10.252.7.38]) by fmsmga002.fm.intel.com with SMTP; 27 Apr 2018 09:38:44 -0700 Received: by (sSMTP sendmail emulation); Fri, 27 Apr 2018 17:38:43 +0100 Date: Fri, 27 Apr 2018 17:38:42 +0100 From: Bruce Richardson To: Anatoly Burakov Cc: dev@dpdk.org Message-ID: <20180427163842.GB96380@bricha3-MOBL.ger.corp.intel.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Organization: Intel Research and Development Ireland Ltd. User-Agent: Mutt/1.9.4 (2018-02-28) Subject: Re: [dpdk-dev] [PATCH] mem: unmap unneeded space X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 27 Apr 2018 16:38:50 -0000 On Thu, Apr 19, 2018 at 05:35:25PM +0100, Anatoly Burakov wrote: > When we ask to reserve virtual areas, we usually include > alignment in the mapping size, and that memory ends up > being wasted. Wasting a gigabyte of VA space while trying to > reserve one gigabyte is pretty expensive on 32-bit, so after > we're done mapping, unmap unneeded space. > > Signed-off-by: Anatoly Burakov > --- > lib/librte_eal/common/eal_common_memory.c | 23 +++++++++++++++++++++-- > 1 file changed, 21 insertions(+), 2 deletions(-) > > diff --git a/lib/librte_eal/common/eal_common_memory.c b/lib/librte_eal/common/eal_common_memory.c > index 24a9ed5..8dd026a 100644 > --- a/lib/librte_eal/common/eal_common_memory.c > +++ b/lib/librte_eal/common/eal_common_memory.c > @@ -75,8 +75,13 @@ eal_get_virtual_area(void *requested_addr, size_t *size, > > do { > map_sz = no_align ? *size : *size + page_sz; > + if (map_sz > SIZE_MAX) { > + RTE_LOG(ERR, EAL, "Map size too big\n"); > + rte_errno = E2BIG; > + return NULL; > + } > > - mapped_addr = mmap(requested_addr, map_sz, PROT_READ, > + mapped_addr = mmap(requested_addr, (size_t)map_sz, PROT_READ, > mmap_flags, -1, 0); > if (mapped_addr == MAP_FAILED && allow_shrink) > *size -= page_sz; > @@ -113,8 +118,22 @@ eal_get_virtual_area(void *requested_addr, size_t *size, > RTE_LOG(WARNING, EAL, " This may cause issues with mapping memory into secondary processes\n"); > } > > - if (unmap) > + if (unmap) { > munmap(mapped_addr, map_sz); > + } else if (!no_align) { > + void *unmap_pre, *unmap_post, *map_end; > + size_t pre_len, post_len; > + > + /* unmap all of the extra space */ > + unmap_pre = mapped_addr; > + pre_len = RTE_PTR_DIFF(aligned_addr, mapped_addr); > + map_end = RTE_PTR_ADD(mapped_addr, (size_t)map_sz); > + unmap_post = RTE_PTR_ADD(aligned_addr, *size); > + post_len = RTE_PTR_DIFF(map_end, unmap_post); > + > + munmap(unmap_pre, pre_len); > + munmap(unmap_post, post_len); I suggest you take parts of the commit log and add them as a comment into the code just after the else if. Also, for readability, I think it would improve things to put the munmap(unmap_pre, pre_len) immediately after the calculation of pre_len, then a black line and then the rest of the code for clearing the post_len block. I found it harder to decipher trying to keep track of all the variables simultaneously in the two blocks. Splitting them into a pre-blocka and a post-block should help here. /Bruce