patches for DPDK stable branches
 help / color / mirror / Atom feed
From: Anatoly Burakov <anatoly.burakov@intel.com>
To: dev@dpdk.org
Cc: Bruce Richardson <bruce.richardson@intel.com>, stable@dpdk.org
Subject: [dpdk-stable] [PATCH 2/2] eal: use base address hint to reserve space for mem config
Date: Mon, 29 Jul 2019 13:42:52 +0100	[thread overview]
Message-ID: <3f4bf9cdf8b472e05e76274f6e8df5be936b01a9.1564398617.git.anatoly.burakov@intel.com> (raw)
In-Reply-To: <09c3f9d74e1e49aa5b3608d4bf4a773d086e83ff.1564398617.git.anatoly.burakov@intel.com>
In-Reply-To: <09c3f9d74e1e49aa5b3608d4bf4a773d086e83ff.1564398617.git.anatoly.burakov@intel.com>

Currently, mem config will be mapped without using the virtual
area reservation infrastructure, which means it will be mapped
at an arbitrary location. This may cause failures to map the
shared config in secondary process due to things like PCI
whitelist arguments allocating memory in a space where the
primary has allocated the shared mem config.

Fix this by using virtual area reservation to reserve space for
the mem config, thereby avoiding the problem and reserving the
shared config (hopefully) far away from any normal memory
allocations.

Cc: stable@dpdk.org

Signed-off-by: Anatoly Burakov <anatoly.burakov@intel.com>
---
 lib/librte_eal/freebsd/eal/eal.c | 27 +++++++++++++++++++++------
 lib/librte_eal/linux/eal/eal.c   | 30 ++++++++++++++++++++++--------
 2 files changed, 43 insertions(+), 14 deletions(-)

diff --git a/lib/librte_eal/freebsd/eal/eal.c b/lib/librte_eal/freebsd/eal/eal.c
index d53f0fe69..fb43c00fd 100644
--- a/lib/librte_eal/freebsd/eal/eal.c
+++ b/lib/librte_eal/freebsd/eal/eal.c
@@ -219,7 +219,8 @@ eal_parse_sysfs_value(const char *filename, unsigned long *val)
 static int
 rte_eal_config_create(void)
 {
-	void *rte_mem_cfg_addr;
+	size_t cfg_len = sizeof(*rte_config.mem_config);
+	void *rte_mem_cfg_addr, *mapped_mem_cfg_addr;
 	int retval;
 
 	const char *pathname = eal_runtime_config_path();
@@ -236,7 +237,7 @@ rte_eal_config_create(void)
 		}
 	}
 
-	retval = ftruncate(mem_cfg_fd, sizeof(*rte_config.mem_config));
+	retval = ftruncate(mem_cfg_fd, cfg_len);
 	if (retval < 0){
 		close(mem_cfg_fd);
 		mem_cfg_fd = -1;
@@ -254,15 +255,29 @@ rte_eal_config_create(void)
 		return -1;
 	}
 
-	rte_mem_cfg_addr = mmap(NULL, sizeof(*rte_config.mem_config),
-				PROT_READ | PROT_WRITE, MAP_SHARED, mem_cfg_fd, 0);
-
-	if (rte_mem_cfg_addr == MAP_FAILED){
+	/* reserve space for config */
+	rte_mem_cfg_addr = eal_get_virtual_area(NULL, &cfg_len,
+			sysconf(_SC_PAGESIZE), EAL_VIRTUAL_AREA_ADDR_IS_HINT,
+			0);
+	if (rte_mem_cfg_addr == NULL) {
 		RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config\n");
 		close(mem_cfg_fd);
 		mem_cfg_fd = -1;
 		return -1;
 	}
+
+	/* remap the actual file into the space we've just reserved */
+	mapped_mem_cfg_addr = mmap(rte_mem_cfg_addr,
+			cfg_len, PROT_READ | PROT_WRITE,
+			MAP_SHARED | MAP_FIXED, mem_cfg_fd, 0);
+	if (mapped_mem_cfg_addr == MAP_FAILED) {
+		RTE_LOG(ERR, EAL, "Cannot remap memory for rte_config\n");
+		munmap(rte_mem_cfg_addr, cfg_len);
+		close(mem_cfg_fd);
+		mem_cfg_fd = -1;
+		return -1;
+	}
+
 	memcpy(rte_mem_cfg_addr, &early_mem_config, sizeof(early_mem_config));
 	rte_config.mem_config = rte_mem_cfg_addr;
 
diff --git a/lib/librte_eal/linux/eal/eal.c b/lib/librte_eal/linux/eal/eal.c
index 34db78753..ed0ebcc8e 100644
--- a/lib/librte_eal/linux/eal/eal.c
+++ b/lib/librte_eal/linux/eal/eal.c
@@ -305,7 +305,8 @@ eal_parse_sysfs_value(const char *filename, unsigned long *val)
 static int
 rte_eal_config_create(void)
 {
-	void *rte_mem_cfg_addr;
+	size_t cfg_len = sizeof(*rte_config.mem_config);
+	void *rte_mem_cfg_addr, *mapped_mem_cfg_addr;
 	int retval;
 
 	const char *pathname = eal_runtime_config_path();
@@ -330,7 +331,7 @@ rte_eal_config_create(void)
 		}
 	}
 
-	retval = ftruncate(mem_cfg_fd, sizeof(*rte_config.mem_config));
+	retval = ftruncate(mem_cfg_fd, cfg_len);
 	if (retval < 0){
 		close(mem_cfg_fd);
 		mem_cfg_fd = -1;
@@ -348,13 +349,26 @@ rte_eal_config_create(void)
 		return -1;
 	}
 
-	rte_mem_cfg_addr = mmap(rte_mem_cfg_addr, sizeof(*rte_config.mem_config),
-				PROT_READ | PROT_WRITE, MAP_SHARED, mem_cfg_fd, 0);
-
-	if (rte_mem_cfg_addr == MAP_FAILED){
-		close(mem_cfg_fd);
-		mem_cfg_fd = -1;
+	/* reserve space for config */
+	rte_mem_cfg_addr = eal_get_virtual_area(rte_mem_cfg_addr, &cfg_len,
+			sysconf(_SC_PAGESIZE), EAL_VIRTUAL_AREA_ADDR_IS_HINT,
+			0);
+	if (rte_mem_cfg_addr == NULL) {
 		RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config\n");
+		close(mem_cfg_fd);
+		mem_cfg_fd = -1;
+		return -1;
+	}
+
+	/* remap the actual file into the space we've just reserved */
+	mapped_mem_cfg_addr = mmap(rte_mem_cfg_addr,
+			cfg_len, PROT_READ | PROT_WRITE,
+			MAP_SHARED | MAP_FIXED, mem_cfg_fd, 0);
+	if (mapped_mem_cfg_addr == MAP_FAILED) {
+		munmap(rte_mem_cfg_addr, cfg_len);
+		close(mem_cfg_fd);
+		mem_cfg_fd = -1;
+		RTE_LOG(ERR, EAL, "Cannot remap memory for rte_config\n");
 		return -1;
 	}
 
-- 
2.17.1

WARNING: multiple messages have this Message-ID
From: Anatoly Burakov <anatoly.burakov@intel.com>
To: dev@dpdk.org
Cc: Bruce Richardson <bruce.richardson@intel.com>, stable@dpdk.org
Subject: [dpdk-stable] [PATCH 2/2] eal: use base address hint to reserve space for mem config
Date: Mon, 29 Jul 2019 12:18:56 +0100	[thread overview]
Message-ID: <3f4bf9cdf8b472e05e76274f6e8df5be936b01a9.1564398617.git.anatoly.burakov@intel.com> (raw)
Message-ID: <20190729111856.oqqsETw3T_XMNRrzUnPkRWnmtIhLudo16f7EPIwxTJA@z> (raw)
In-Reply-To: <09c3f9d74e1e49aa5b3608d4bf4a773d086e83ff.1564398617.git.anatoly.burakov@intel.com>
In-Reply-To: <09c3f9d74e1e49aa5b3608d4bf4a773d086e83ff.1564398617.git.anatoly.burakov@intel.com>

Currently, mem config will be mapped without using the virtual
area reservation infrastructure, which means it will be mapped
at an arbitrary location. This may cause failures to map the
shared config in secondary process due to things like PCI
whitelist arguments allocating memory in a space where the
primary has allocated the shared mem config.

Fix this by using virtual area reservation to reserve space for
the mem config, thereby avoiding the problem and reserving the
shared config (hopefully) far away from any normal memory
allocations.

Cc: stable@dpdk.org

Signed-off-by: Anatoly Burakov <anatoly.burakov@intel.com>
---
 lib/librte_eal/freebsd/eal/eal.c | 27 +++++++++++++++++++++------
 lib/librte_eal/linux/eal/eal.c   | 30 ++++++++++++++++++++++--------
 2 files changed, 43 insertions(+), 14 deletions(-)

diff --git a/lib/librte_eal/freebsd/eal/eal.c b/lib/librte_eal/freebsd/eal/eal.c
index d53f0fe69..fb43c00fd 100644
--- a/lib/librte_eal/freebsd/eal/eal.c
+++ b/lib/librte_eal/freebsd/eal/eal.c
@@ -219,7 +219,8 @@ eal_parse_sysfs_value(const char *filename, unsigned long *val)
 static int
 rte_eal_config_create(void)
 {
-	void *rte_mem_cfg_addr;
+	size_t cfg_len = sizeof(*rte_config.mem_config);
+	void *rte_mem_cfg_addr, *mapped_mem_cfg_addr;
 	int retval;
 
 	const char *pathname = eal_runtime_config_path();
@@ -236,7 +237,7 @@ rte_eal_config_create(void)
 		}
 	}
 
-	retval = ftruncate(mem_cfg_fd, sizeof(*rte_config.mem_config));
+	retval = ftruncate(mem_cfg_fd, cfg_len);
 	if (retval < 0){
 		close(mem_cfg_fd);
 		mem_cfg_fd = -1;
@@ -254,15 +255,29 @@ rte_eal_config_create(void)
 		return -1;
 	}
 
-	rte_mem_cfg_addr = mmap(NULL, sizeof(*rte_config.mem_config),
-				PROT_READ | PROT_WRITE, MAP_SHARED, mem_cfg_fd, 0);
-
-	if (rte_mem_cfg_addr == MAP_FAILED){
+	/* reserve space for config */
+	rte_mem_cfg_addr = eal_get_virtual_area(NULL, &cfg_len,
+			sysconf(_SC_PAGESIZE), EAL_VIRTUAL_AREA_ADDR_IS_HINT,
+			0);
+	if (rte_mem_cfg_addr == NULL) {
 		RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config\n");
 		close(mem_cfg_fd);
 		mem_cfg_fd = -1;
 		return -1;
 	}
+
+	/* remap the actual file into the space we've just reserved */
+	mapped_mem_cfg_addr = mmap(rte_mem_cfg_addr,
+			cfg_len, PROT_READ | PROT_WRITE,
+			MAP_SHARED | MAP_FIXED, mem_cfg_fd, 0);
+	if (mapped_mem_cfg_addr == MAP_FAILED) {
+		RTE_LOG(ERR, EAL, "Cannot remap memory for rte_config\n");
+		munmap(rte_mem_cfg_addr, cfg_len);
+		close(mem_cfg_fd);
+		mem_cfg_fd = -1;
+		return -1;
+	}
+
 	memcpy(rte_mem_cfg_addr, &early_mem_config, sizeof(early_mem_config));
 	rte_config.mem_config = rte_mem_cfg_addr;
 
diff --git a/lib/librte_eal/linux/eal/eal.c b/lib/librte_eal/linux/eal/eal.c
index 34db78753..ed0ebcc8e 100644
--- a/lib/librte_eal/linux/eal/eal.c
+++ b/lib/librte_eal/linux/eal/eal.c
@@ -305,7 +305,8 @@ eal_parse_sysfs_value(const char *filename, unsigned long *val)
 static int
 rte_eal_config_create(void)
 {
-	void *rte_mem_cfg_addr;
+	size_t cfg_len = sizeof(*rte_config.mem_config);
+	void *rte_mem_cfg_addr, *mapped_mem_cfg_addr;
 	int retval;
 
 	const char *pathname = eal_runtime_config_path();
@@ -330,7 +331,7 @@ rte_eal_config_create(void)
 		}
 	}
 
-	retval = ftruncate(mem_cfg_fd, sizeof(*rte_config.mem_config));
+	retval = ftruncate(mem_cfg_fd, cfg_len);
 	if (retval < 0){
 		close(mem_cfg_fd);
 		mem_cfg_fd = -1;
@@ -348,13 +349,26 @@ rte_eal_config_create(void)
 		return -1;
 	}
 
-	rte_mem_cfg_addr = mmap(rte_mem_cfg_addr, sizeof(*rte_config.mem_config),
-				PROT_READ | PROT_WRITE, MAP_SHARED, mem_cfg_fd, 0);
-
-	if (rte_mem_cfg_addr == MAP_FAILED){
-		close(mem_cfg_fd);
-		mem_cfg_fd = -1;
+	/* reserve space for config */
+	rte_mem_cfg_addr = eal_get_virtual_area(rte_mem_cfg_addr, &cfg_len,
+			sysconf(_SC_PAGESIZE), EAL_VIRTUAL_AREA_ADDR_IS_HINT,
+			0);
+	if (rte_mem_cfg_addr == NULL) {
 		RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config\n");
+		close(mem_cfg_fd);
+		mem_cfg_fd = -1;
+		return -1;
+	}
+
+	/* remap the actual file into the space we've just reserved */
+	mapped_mem_cfg_addr = mmap(rte_mem_cfg_addr,
+			cfg_len, PROT_READ | PROT_WRITE,
+			MAP_SHARED | MAP_FIXED, mem_cfg_fd, 0);
+	if (mapped_mem_cfg_addr == MAP_FAILED) {
+		munmap(rte_mem_cfg_addr, cfg_len);
+		close(mem_cfg_fd);
+		mem_cfg_fd = -1;
+		RTE_LOG(ERR, EAL, "Cannot remap memory for rte_config\n");
 		return -1;
 	}
 
-- 
2.17.1

WARNING: multiple messages have this Message-ID
From: Anatoly Burakov <anatoly.burakov@intel.com>
To: dev@dpdk.org
Cc: Bruce Richardson <bruce.richardson@intel.com>, stable@dpdk.org
Subject: [dpdk-stable] [PATCH 2/2] eal: use base address hint to reserve space for mem config
Date: Mon, 29 Jul 2019 12:10:23 +0100	[thread overview]
Message-ID: <3f4bf9cdf8b472e05e76274f6e8df5be936b01a9.1564398617.git.anatoly.burakov@intel.com> (raw)
Message-ID: <20190729111023.Wr7JGfenk5M2j8QvOvIyj5VHdHhfgkIc2hPo8uJbyQg@z> (raw)
In-Reply-To: <09c3f9d74e1e49aa5b3608d4bf4a773d086e83ff.1564398617.git.anatoly.burakov@intel.com>
In-Reply-To: <09c3f9d74e1e49aa5b3608d4bf4a773d086e83ff.1564398617.git.anatoly.burakov@intel.com>

Currently, mem config will be mapped without using the virtual
area reservation infrastructure, which means it will be mapped
at an arbitrary location. This may cause failures to map the
shared config in secondary process due to things like PCI
whitelist arguments allocating memory in a space where the
primary has allocated the shared mem config.

Fix this by using virtual area reservation to reserve space for
the mem config, thereby avoiding the problem and reserving the
shared config (hopefully) far away from any normal memory
allocations.

Cc: stable@dpdk.org

Signed-off-by: Anatoly Burakov <anatoly.burakov@intel.com>
---
 lib/librte_eal/freebsd/eal/eal.c | 27 +++++++++++++++++++++------
 lib/librte_eal/linux/eal/eal.c   | 30 ++++++++++++++++++++++--------
 2 files changed, 43 insertions(+), 14 deletions(-)

diff --git a/lib/librte_eal/freebsd/eal/eal.c b/lib/librte_eal/freebsd/eal/eal.c
index d53f0fe69..fb43c00fd 100644
--- a/lib/librte_eal/freebsd/eal/eal.c
+++ b/lib/librte_eal/freebsd/eal/eal.c
@@ -219,7 +219,8 @@ eal_parse_sysfs_value(const char *filename, unsigned long *val)
 static int
 rte_eal_config_create(void)
 {
-	void *rte_mem_cfg_addr;
+	size_t cfg_len = sizeof(*rte_config.mem_config);
+	void *rte_mem_cfg_addr, *mapped_mem_cfg_addr;
 	int retval;
 
 	const char *pathname = eal_runtime_config_path();
@@ -236,7 +237,7 @@ rte_eal_config_create(void)
 		}
 	}
 
-	retval = ftruncate(mem_cfg_fd, sizeof(*rte_config.mem_config));
+	retval = ftruncate(mem_cfg_fd, cfg_len);
 	if (retval < 0){
 		close(mem_cfg_fd);
 		mem_cfg_fd = -1;
@@ -254,15 +255,29 @@ rte_eal_config_create(void)
 		return -1;
 	}
 
-	rte_mem_cfg_addr = mmap(NULL, sizeof(*rte_config.mem_config),
-				PROT_READ | PROT_WRITE, MAP_SHARED, mem_cfg_fd, 0);
-
-	if (rte_mem_cfg_addr == MAP_FAILED){
+	/* reserve space for config */
+	rte_mem_cfg_addr = eal_get_virtual_area(NULL, &cfg_len,
+			sysconf(_SC_PAGESIZE), EAL_VIRTUAL_AREA_ADDR_IS_HINT,
+			0);
+	if (rte_mem_cfg_addr == NULL) {
 		RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config\n");
 		close(mem_cfg_fd);
 		mem_cfg_fd = -1;
 		return -1;
 	}
+
+	/* remap the actual file into the space we've just reserved */
+	mapped_mem_cfg_addr = mmap(rte_mem_cfg_addr,
+			cfg_len, PROT_READ | PROT_WRITE,
+			MAP_SHARED | MAP_FIXED, mem_cfg_fd, 0);
+	if (mapped_mem_cfg_addr == MAP_FAILED) {
+		RTE_LOG(ERR, EAL, "Cannot remap memory for rte_config\n");
+		munmap(rte_mem_cfg_addr, cfg_len);
+		close(mem_cfg_fd);
+		mem_cfg_fd = -1;
+		return -1;
+	}
+
 	memcpy(rte_mem_cfg_addr, &early_mem_config, sizeof(early_mem_config));
 	rte_config.mem_config = rte_mem_cfg_addr;
 
diff --git a/lib/librte_eal/linux/eal/eal.c b/lib/librte_eal/linux/eal/eal.c
index 34db78753..ed0ebcc8e 100644
--- a/lib/librte_eal/linux/eal/eal.c
+++ b/lib/librte_eal/linux/eal/eal.c
@@ -305,7 +305,8 @@ eal_parse_sysfs_value(const char *filename, unsigned long *val)
 static int
 rte_eal_config_create(void)
 {
-	void *rte_mem_cfg_addr;
+	size_t cfg_len = sizeof(*rte_config.mem_config);
+	void *rte_mem_cfg_addr, *mapped_mem_cfg_addr;
 	int retval;
 
 	const char *pathname = eal_runtime_config_path();
@@ -330,7 +331,7 @@ rte_eal_config_create(void)
 		}
 	}
 
-	retval = ftruncate(mem_cfg_fd, sizeof(*rte_config.mem_config));
+	retval = ftruncate(mem_cfg_fd, cfg_len);
 	if (retval < 0){
 		close(mem_cfg_fd);
 		mem_cfg_fd = -1;
@@ -348,13 +349,26 @@ rte_eal_config_create(void)
 		return -1;
 	}
 
-	rte_mem_cfg_addr = mmap(rte_mem_cfg_addr, sizeof(*rte_config.mem_config),
-				PROT_READ | PROT_WRITE, MAP_SHARED, mem_cfg_fd, 0);
-
-	if (rte_mem_cfg_addr == MAP_FAILED){
-		close(mem_cfg_fd);
-		mem_cfg_fd = -1;
+	/* reserve space for config */
+	rte_mem_cfg_addr = eal_get_virtual_area(rte_mem_cfg_addr, &cfg_len,
+			sysconf(_SC_PAGESIZE), EAL_VIRTUAL_AREA_ADDR_IS_HINT,
+			0);
+	if (rte_mem_cfg_addr == NULL) {
 		RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config\n");
+		close(mem_cfg_fd);
+		mem_cfg_fd = -1;
+		return -1;
+	}
+
+	/* remap the actual file into the space we've just reserved */
+	mapped_mem_cfg_addr = mmap(rte_mem_cfg_addr,
+			cfg_len, PROT_READ | PROT_WRITE,
+			MAP_SHARED | MAP_FIXED, mem_cfg_fd, 0);
+	if (mapped_mem_cfg_addr == MAP_FAILED) {
+		munmap(rte_mem_cfg_addr, cfg_len);
+		close(mem_cfg_fd);
+		mem_cfg_fd = -1;
+		RTE_LOG(ERR, EAL, "Cannot remap memory for rte_config\n");
 		return -1;
 	}
 
-- 
2.17.1

  parent reply	other threads:[~2019-07-29 12:43 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-29 12:42 [dpdk-stable] [PATCH 1/2] eal: make base address hint OS-specific Anatoly Burakov
2019-07-29 11:10 ` Anatoly Burakov
2019-07-29 11:18 ` Anatoly Burakov
2019-07-29 12:42 ` Anatoly Burakov [this message]
2019-07-29 11:10   ` [dpdk-stable] [PATCH 2/2] eal: use base address hint to reserve space for mem config Anatoly Burakov
2019-07-29 11:18   ` Anatoly Burakov
2019-07-29 13:13   ` [dpdk-stable] [dpdk-dev] " Burakov, Anatoly
2019-07-30 15:37 ` [dpdk-stable] [PATCH v2 1/2] eal: make base address hint OS-specific Anatoly Burakov
2019-07-30 15:51   ` [dpdk-stable] [PATCH v3 " Anatoly Burakov
2019-07-31 12:47     ` [dpdk-stable] [PATCH v4 " Anatoly Burakov
2019-10-02 11:41       ` [dpdk-stable] [dpdk-dev] " David Marchand
2019-10-02 14:42         ` Stephen Hemminger
2019-10-24 12:32           ` Burakov, Anatoly
2019-10-24 12:32         ` Burakov, Anatoly
2019-10-24 12:36       ` [dpdk-stable] [PATCH v5 " Anatoly Burakov
2019-10-26 16:02         ` David Marchand
2019-10-24 12:36       ` [dpdk-stable] [PATCH v5 2/2] eal: use base address hint to reserve space for mem config Anatoly Burakov
2019-10-26 16:02         ` David Marchand
2019-07-31 12:47     ` [dpdk-stable] [PATCH v4 " Anatoly Burakov
2019-07-30 15:51   ` [dpdk-stable] [PATCH v3 " Anatoly Burakov
2019-07-30 15:37 ` [dpdk-stable] [PATCH v2 " 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=3f4bf9cdf8b472e05e76274f6e8df5be936b01a9.1564398617.git.anatoly.burakov@intel.com \
    --to=anatoly.burakov@intel.com \
    --cc=bruce.richardson@intel.com \
    --cc=dev@dpdk.org \
    --cc=stable@dpdk.org \
    /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).