DPDK patches and discussions
 help / color / mirror / Atom feed
From: Anatoly Burakov <anatoly.burakov@intel.com>
To: dev@dpdk.org
Cc: John McNamara <john.mcnamara@intel.com>,
	Marko Kovacevic <marko.kovacevic@intel.com>,
	przemyslawx.lal@intel.com, kuralamudhan.ramakrishnan@intel.com,
	ivan.coughlan@intel.com, tiwei.bie@intel.com,
	ray.kinsella@intel.com, maxime.coquelin@redhat.com
Subject: [dpdk-dev] [PATCH v2 4/5] mem: use memfd for no-huge mode
Date: Tue, 11 Dec 2018 16:43:31 +0000	[thread overview]
Message-ID: <28e25797ef95a0a74fd264388ab63b9cd980c265.1544546363.git.anatoly.burakov@intel.com> (raw)
In-Reply-To: <cover.1544546363.git.anatoly.burakov@intel.com>
In-Reply-To: <cover.1544546363.git.anatoly.burakov@intel.com>

When running in no-huge mode, we anonymously allocate our memory.
While this works for regular NICs and vdev's, it's not suitable
for memory sharing scenarios such as virtio with vhost_user
backend.

To fix this, allocate no-huge memory using memfd, and register
it with memalloc just like any other memseg fd. This will enable
using rte_memseg_get_fd() API with --no-huge EAL flag.

Signed-off-by: Anatoly Burakov <anatoly.burakov@intel.com>
---

Notes:
    v2:
    - Detect memfd support at compile time
    - Change memfd-related log level to debug

 doc/guides/rel_notes/release_19_02.rst   |  5 +++
 lib/librte_eal/linuxapp/eal/eal_memory.c | 54 +++++++++++++++++++++++-
 2 files changed, 57 insertions(+), 2 deletions(-)

diff --git a/doc/guides/rel_notes/release_19_02.rst b/doc/guides/rel_notes/release_19_02.rst
index 960098582..420d51b5b 100644
--- a/doc/guides/rel_notes/release_19_02.rst
+++ b/doc/guides/rel_notes/release_19_02.rst
@@ -23,6 +23,11 @@ DPDK Release 19.02
 New Features
 ------------
 
+* **Support for using VirtIO without hugepages**
+
+  The --no-huge mode was augmented to use memfd-backed memory (on systems that
+  support memfd), to allow using VirtIO-based NICs without hugepages.
+
 .. This section should contain new features added in this release.
    Sample format:
 
diff --git a/lib/librte_eal/linuxapp/eal/eal_memory.c b/lib/librte_eal/linuxapp/eal/eal_memory.c
index 32feb415d..7d922a965 100644
--- a/lib/librte_eal/linuxapp/eal/eal_memory.c
+++ b/lib/librte_eal/linuxapp/eal/eal_memory.c
@@ -25,6 +25,10 @@
 #include <sys/time.h>
 #include <signal.h>
 #include <setjmp.h>
+#ifdef F_ADD_SEALS /* if file sealing is supported, so is memfd */
+#include <linux/memfd.h>
+#define MEMFD_SUPPORTED
+#endif
 #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
 #include <numa.h>
 #include <numaif.h>
@@ -1341,12 +1345,18 @@ eal_legacy_hugepage_init(void)
 	/* hugetlbfs can be disabled */
 	if (internal_config.no_hugetlbfs) {
 		struct rte_memseg_list *msl;
+		int n_segs, cur_seg, fd, flags;
+#ifdef MEMFD_SUPPORTED
+		int memfd;
+#endif
 		uint64_t page_sz;
-		int n_segs, cur_seg;
 
 		/* nohuge mode is legacy mode */
 		internal_config.legacy_mem = 1;
 
+		/* nohuge mode is single-file segments mode */
+		internal_config.single_file_segments = 1;
+
 		/* create a memseg list */
 		msl = &mcfg->memsegs[0];
 
@@ -1359,8 +1369,38 @@ eal_legacy_hugepage_init(void)
 			return -1;
 		}
 
+		/* set up parameters for anonymous mmap */
+		fd = -1;
+		flags = MAP_PRIVATE | MAP_ANONYMOUS;
+
+#ifdef MEMFD_SUPPORTED
+		/* create a memfd and store it in the segment fd table */
+		memfd = memfd_create("nohuge", 0);
+		if (memfd < 0) {
+			RTE_LOG(DEBUG, EAL, "Cannot create memfd: %s\n",
+					strerror(errno));
+			RTE_LOG(DEBUG, EAL, "Falling back to anonymous map\n");
+		} else {
+			/* we got an fd - now resize it */
+			if (ftruncate(memfd, internal_config.memory) < 0) {
+				RTE_LOG(ERR, EAL, "Cannot resize memfd: %s\n",
+						strerror(errno));
+				RTE_LOG(ERR, EAL, "Falling back to anonymous map\n");
+				close(memfd);
+			} else {
+				/* creating memfd-backed file was successful.
+				 * we want changes to memfd to be visible to
+				 * other processes (such as vhost backend), so
+				 * map it as shared memory.
+				 */
+				RTE_LOG(DEBUG, EAL, "Using memfd for anonymous memory\n");
+				fd = memfd;
+				flags = MAP_SHARED;
+			}
+		}
+#endif
 		addr = mmap(NULL, internal_config.memory, PROT_READ | PROT_WRITE,
-				MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+				flags, fd, 0);
 		if (addr == MAP_FAILED) {
 			RTE_LOG(ERR, EAL, "%s: mmap() failed: %s\n", __func__,
 					strerror(errno));
@@ -1371,6 +1411,16 @@ eal_legacy_hugepage_init(void)
 		msl->socket_id = 0;
 		msl->len = internal_config.memory;
 
+		/* we're in single-file segments mode, so only the segment list
+		 * fd needs to be set up.
+		 */
+		if (fd != -1) {
+			if (eal_memalloc_set_seg_list_fd(0, fd) < 0) {
+				RTE_LOG(ERR, EAL, "Cannot set up segment list fd\n");
+				/* not a serious error, proceed */
+			}
+		}
+
 		/* populate memsegs. each memseg is one page long */
 		for (cur_seg = 0; cur_seg < n_segs; cur_seg++) {
 			arr = &msl->memseg_arr;
-- 
2.17.1

  parent reply	other threads:[~2018-12-11 16:43 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-11-13 17:54 [dpdk-dev] [PATCH 19.02 0/2] Allow using virtio without hugepages Anatoly Burakov
2018-11-13 17:54 ` [dpdk-dev] [PATCH 19.02 1/2] memalloc: allow setting up segment list fd's Anatoly Burakov
2018-11-13 17:54 ` [dpdk-dev] [PATCH 19.02 2/2] mem: use memfd for no-huge mode Anatoly Burakov
2018-11-28  4:57   ` Tiwei Bie
2018-11-28  9:11     ` Burakov, Anatoly
2018-12-11 16:43 ` [dpdk-dev] [PATCH v2 0/5] Allow using virtio without hugepages Anatoly Burakov
2018-12-13  4:53   ` Tiwei Bie
2018-12-13 11:43   ` [dpdk-dev] [PATCH v3 0/5] Allow using virtio-user " Anatoly Burakov
2018-12-20 22:01     ` Thomas Monjalon
2018-12-13 11:43   ` [dpdk-dev] [PATCH v3 1/5] mem: fix error code for segment fd API for external segs Anatoly Burakov
2018-12-14  9:15     ` Maxime Coquelin
2018-12-13 11:43   ` [dpdk-dev] [PATCH v3 2/5] memalloc: check for memfd support in segment fd API Anatoly Burakov
2018-12-14  9:19     ` Maxime Coquelin
2018-12-13 11:43   ` [dpdk-dev] [PATCH v3 3/5] memalloc: allow setting up segment list fd's Anatoly Burakov
2018-12-14 10:03     ` Maxime Coquelin
2018-12-13 11:43   ` [dpdk-dev] [PATCH v3 4/5] mem: use memfd for no-huge mode Anatoly Burakov
2018-12-13 11:59     ` Burakov, Anatoly
2018-12-14 10:06     ` Maxime Coquelin
2018-12-13 11:43   ` [dpdk-dev] [PATCH v3 5/5] test: add segment fd API test Anatoly Burakov
2018-12-14 10:09     ` Maxime Coquelin
2018-12-11 16:43 ` [dpdk-dev] [PATCH v2 1/5] mem: fix error code for segment fd API for external segs Anatoly Burakov
2018-12-11 16:43 ` [dpdk-dev] [PATCH v2 2/5] memalloc: check for memfd support in segment fd API Anatoly Burakov
2018-12-11 16:43 ` [dpdk-dev] [PATCH v2 3/5] memalloc: allow setting up segment list fd's Anatoly Burakov
2018-12-11 16:43 ` Anatoly Burakov [this message]
2018-12-13  4:59   ` [dpdk-dev] [PATCH v2 4/5] mem: use memfd for no-huge mode Tiwei Bie
2018-12-13 11:36     ` Burakov, Anatoly
2018-12-11 16:43 ` [dpdk-dev] [PATCH v2 5/5] test: add segment fd API test Anatoly Burakov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=28e25797ef95a0a74fd264388ab63b9cd980c265.1544546363.git.anatoly.burakov@intel.com \
    --to=anatoly.burakov@intel.com \
    --cc=dev@dpdk.org \
    --cc=ivan.coughlan@intel.com \
    --cc=john.mcnamara@intel.com \
    --cc=kuralamudhan.ramakrishnan@intel.com \
    --cc=marko.kovacevic@intel.com \
    --cc=maxime.coquelin@redhat.com \
    --cc=przemyslawx.lal@intel.com \
    --cc=ray.kinsella@intel.com \
    --cc=tiwei.bie@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).