* [PATCH] eal: handle sysconf() negative return value
@ 2025-06-10 13:13 Morten Brørup
0 siblings, 0 replies; only message in thread
From: Morten Brørup @ 2025-06-10 13:13 UTC (permalink / raw)
To: Tyler Retzlaff, Anatoly Burakov, Bruce Richardson,
Maxime Coquelin, Chenbo Xia, dev
Cc: Morten Brørup
Coverity reports some defects, where the root cause seems to be negative
return value from sysconf() not being handled.
rte_mem_page_size() has been updated to handle negative return value from
sysconf(_SC_PAGESIZE).
All other functions calling sysconf(_SC_PAGESIZE) directly have been
updated to call rte_mem_page_size() instead.
At this time, no other calls to sysconf() have been found in DPDK.
Also, an unrelated drive-by minor fix in eal_mem_set_dump():
When madvise() failed, an incorrect reason was logged.
Signed-off-by: Morten Brørup <mb@smartsharesystems.com>
---
lib/eal/freebsd/eal.c | 3 ++-
lib/eal/freebsd/eal_memory.c | 7 +++----
lib/eal/linux/eal.c | 3 ++-
lib/eal/unix/eal_unix_memory.c | 16 ++++++++++++++--
lib/vhost/vduse.c | 3 ++-
5 files changed, 23 insertions(+), 9 deletions(-)
diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index d6fffa2170..ce7f5c3d65 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -26,6 +26,7 @@
#include <rte_launch.h>
#include <rte_eal.h>
#include <rte_eal_memconfig.h>
+#include <rte_eal_paging.h>
#include <rte_errno.h>
#include <rte_per_lcore.h>
#include <rte_lcore.h>
@@ -98,7 +99,7 @@ rte_eal_config_create(void)
struct rte_config *config = rte_eal_get_configuration();
const struct internal_config *internal_conf =
eal_get_internal_configuration();
- size_t page_sz = sysconf(_SC_PAGE_SIZE);
+ size_t page_sz = rte_mem_page_size();
size_t cfg_len = sizeof(struct rte_mem_config);
size_t cfg_len_aligned = RTE_ALIGN(cfg_len, page_sz);
void *rte_mem_cfg_addr, *mapped_mem_cfg_addr;
diff --git a/lib/eal/freebsd/eal_memory.c b/lib/eal/freebsd/eal_memory.c
index 3b72e13506..6d3d46a390 100644
--- a/lib/eal/freebsd/eal_memory.c
+++ b/lib/eal/freebsd/eal_memory.c
@@ -11,6 +11,7 @@
#include <fcntl.h>
#include <rte_eal.h>
+#include <rte_eal_paging.h>
#include <rte_errno.h>
#include <rte_log.h>
#include <rte_string_fns.h>
@@ -22,8 +23,6 @@
#include "eal_memcfg.h"
#include "eal_options.h"
-#define EAL_PAGE_SIZE (sysconf(_SC_PAGESIZE))
-
uint64_t eal_get_baseaddr(void)
{
/*
@@ -191,7 +190,7 @@ rte_eal_hugepage_init(void)
addr = mmap(addr, page_sz, PROT_READ|PROT_WRITE,
MAP_SHARED | MAP_FIXED,
hpi->lock_descriptor,
- j * EAL_PAGE_SIZE);
+ j * rte_mem_page_size());
if (addr == MAP_FAILED) {
EAL_LOG(ERR, "Failed to mmap buffer %u from %s",
j, hpi->hugedir);
@@ -243,7 +242,7 @@ attach_segment(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
addr = mmap(ms->addr, ms->len, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_FIXED, wa->fd_hugepage,
- wa->seg_idx * EAL_PAGE_SIZE);
+ wa->seg_idx * rte_mem_page_size());
if (addr == MAP_FAILED || addr != ms->addr)
return -1;
wa->seg_idx++;
diff --git a/lib/eal/linux/eal.c b/lib/eal/linux/eal.c
index 20f777b8b0..ad6de9b7ee 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -31,6 +31,7 @@
#include <rte_launch.h>
#include <rte_eal.h>
#include <rte_eal_memconfig.h>
+#include <rte_eal_paging.h>
#include <rte_errno.h>
#include <rte_lcore.h>
#include <rte_service_component.h>
@@ -179,7 +180,7 @@ static int
rte_eal_config_create(void)
{
struct rte_config *config = rte_eal_get_configuration();
- size_t page_sz = sysconf(_SC_PAGE_SIZE);
+ size_t page_sz = rte_mem_page_size();
size_t cfg_len = sizeof(*config->mem_config);
size_t cfg_len_aligned = RTE_ALIGN(cfg_len, page_sz);
void *rte_mem_cfg_addr, *mapped_mem_cfg_addr;
diff --git a/lib/eal/unix/eal_unix_memory.c b/lib/eal/unix/eal_unix_memory.c
index c540f1e838..51de6780f2 100644
--- a/lib/eal/unix/eal_unix_memory.c
+++ b/lib/eal/unix/eal_unix_memory.c
@@ -85,7 +85,7 @@ eal_mem_set_dump(void *virt, size_t size, bool dump)
int ret = madvise(virt, size, flags);
if (ret) {
EAL_LOG(DEBUG, "madvise(%p, %#zx, %d) failed: %s",
- virt, size, flags, strerror(rte_errno));
+ virt, size, flags, strerror(errno));
rte_errno = errno;
}
return ret;
@@ -141,8 +141,20 @@ rte_mem_page_size(void)
{
static size_t page_size;
- if (!page_size)
+ if (page_size == 0) {
+ /*
+ * When the sysconf value cannot be determined, sysconf()
+ * returns -1 without setting errno.
+ * To distinguish an indeterminate value from an error,
+ * clear errno before calling sysconf(), and check whether
+ * errno has been set if sysconf() returns -1.
+ */
+ errno = 0;
page_size = sysconf(_SC_PAGESIZE);
+ if ((ssize_t)page_size < 0)
+ rte_panic("sysconf(_SC_PAGESIZE) failed: %s",
+ errno == 0 ? "Indeterminate" : strerror(errno));
+ }
return page_size;
}
diff --git a/lib/vhost/vduse.c b/lib/vhost/vduse.c
index a2a7d73388..9de7f04a4f 100644
--- a/lib/vhost/vduse.c
+++ b/lib/vhost/vduse.c
@@ -16,6 +16,7 @@
#include <sys/stat.h>
#include <rte_common.h>
+#include <rte_eal_paging.h>
#include <rte_thread.h>
#include "fd_man.h"
@@ -690,7 +691,7 @@ vduse_device_create(const char *path, bool compliant_ol_flags)
dev_config->vendor_id = 0;
dev_config->features = features;
dev_config->vq_num = total_queues;
- dev_config->vq_align = sysconf(_SC_PAGE_SIZE);
+ dev_config->vq_align = rte_mem_page_size();
dev_config->config_size = sizeof(struct virtio_net_config);
memcpy(dev_config->config, &vnet_config, sizeof(vnet_config));
--
2.43.0
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2025-06-10 13:13 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-06-10 13:13 [PATCH] eal: handle sysconf() negative return value Morten Brørup
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).