[dpdk-dev,3/3] memalloc: fix unmapping and marking segments as free

Message ID 88ac531b5b13288971edcee16785c8cc612bdfe0.1525342009.git.anatoly.burakov@intel.com (mailing list archive)
State Accepted, archived
Delegated to: Thomas Monjalon
Headers

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation success Compilation OK

Commit Message

Anatoly Burakov May 3, 2018, 10:11 a.m. UTC
  Currently, page deallocation might fail if allocator cannot get page
fd, which will leave VA space still mapped, and will also not mark
page as free.

Fix page deallocation function to always unmap space before trying
to get rid of the page itself, and always mark page as free even if
page deallocation failed.

Fixes: a5ff05d60fc5 ("mem: support unmapping pages at runtime")
Fixes: 1a7dc2252f28 ("mem: revert to using flock and add per-segment lockfiles")
Cc: anatoly.burakov@intel.com

Signed-off-by: Anatoly Burakov <anatoly.burakov@intel.com>
---
 lib/librte_eal/linuxapp/eal/eal_memalloc.c | 27 ++++++++++++++-------------
 1 file changed, 14 insertions(+), 13 deletions(-)
  

Comments

Ananyev, Konstantin May 10, 2018, 5:03 p.m. UTC | #1
> -----Original Message-----
> From: dev [mailto:dev-bounces@dpdk.org] On Behalf Of Anatoly Burakov
> Sent: Thursday, May 3, 2018 11:11 AM
> To: dev@dpdk.org
> Cc: Liu, Yong <yong.liu@intel.com>; Burakov, Anatoly <anatoly.burakov@intel.com>
> Subject: [dpdk-dev] [PATCH 3/3] memalloc: fix unmapping and marking segments as free
> 
> Currently, page deallocation might fail if allocator cannot get page
> fd, which will leave VA space still mapped, and will also not mark
> page as free.
> 
> Fix page deallocation function to always unmap space before trying
> to get rid of the page itself, and always mark page as free even if
> page deallocation failed.
> 
> Fixes: a5ff05d60fc5 ("mem: support unmapping pages at runtime")
> Fixes: 1a7dc2252f28 ("mem: revert to using flock and add per-segment lockfiles")
> Cc: anatoly.burakov@intel.com
> 
> Signed-off-by: Anatoly Burakov <anatoly.burakov@intel.com>
> ---

Acked-by: Konstantin Ananyev <konstantin.ananyev@intel.com>
  

Patch

diff --git a/lib/librte_eal/linuxapp/eal/eal_memalloc.c b/lib/librte_eal/linuxapp/eal/eal_memalloc.c
index 3282293..c1a0211 100644
--- a/lib/librte_eal/linuxapp/eal/eal_memalloc.c
+++ b/lib/librte_eal/linuxapp/eal/eal_memalloc.c
@@ -610,14 +610,6 @@  free_seg(struct rte_memseg *ms, struct hugepage_info *hi,
 	/* erase page data */
 	memset(ms->addr, 0, ms->len);
 
-	/* if we are not in single file segments mode, we're going to unmap the
-	 * segment and thus drop the lock on original fd, so take out another
-	 * shared lock before we do that.
-	 */
-	fd = get_seg_fd(path, sizeof(path), hi, list_idx, seg_idx);
-	if (fd < 0)
-		return -1;
-
 	if (mmap(ms->addr, ms->len, PROT_READ,
 			MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0) ==
 				MAP_FAILED) {
@@ -625,6 +617,14 @@  free_seg(struct rte_memseg *ms, struct hugepage_info *hi,
 		return -1;
 	}
 
+	/* if we are not in single file segments mode, we're going to unmap the
+	 * segment and thus drop the lock on original fd, but hugepage dir is
+	 * now locked so we can take out another one without races.
+	 */
+	fd = get_seg_fd(path, sizeof(path), hi, list_idx, seg_idx);
+	if (fd < 0)
+		return -1;
+
 	if (internal_config.single_file_segments) {
 		map_offset = seg_idx * ms->len;
 		if (resize_hugefile(fd, path, list_idx, seg_idx, map_offset,
@@ -735,12 +735,13 @@  alloc_seg_walk(const struct rte_memseg_list *msl, void *arg)
 						&cur_msl->memseg_arr;
 
 				tmp = rte_fbarray_get(arr, j);
-				if (free_seg(tmp, wa->hi, msl_idx, j)) {
-					RTE_LOG(ERR, EAL, "Cannot free page\n");
-					continue;
-				}
-
 				rte_fbarray_set_free(arr, j);
+
+				/* free_seg may attempt to create a file, which
+				 * may fail.
+				 */
+				if (free_seg(tmp, wa->hi, msl_idx, j))
+					RTE_LOG(DEBUG, EAL, "Cannot free page\n");
 			}
 			/* clear the list */
 			if (wa->ms)