Skip to contentSkip to conversation

linuxapp, eal: Fix the memory leak issue of logid

PATCH Superseded Ziye Yang 5 participants 16 messages Sep 5, 2018 – Oct 28, 2018

lore
Share & tools
lore

Ziye Yang

author
#0 lore raw

PATCH v5 1/1 linuxapp, eal: Fix the memory leak issue of logid

From: Ziye Yang <[email protected]>

This patch is used to fix the memory leak issue of logid. We use the ASAN test in SPDK when intergrating DPDK and find this memory leak issue.

---
 lib/librte_eal/linuxapp/eal/eal.c     | 21 ++++++++++++++++++++-
 lib/librte_eal/linuxapp/eal/eal_log.c | 11 ++++++++++-
 2 files changed, 30 insertions(+), 2 deletions(-)
lib/librte_eal/linuxapp/eal/eal.c +20 -1
index e59ac65..e150200 100644
@@ -793,7 +793,7 @@ static void rte_eal_init_alert(const char *msg)
 	int i, fctret, ret;
 	pthread_t thread_id;
 	static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
-	const char *logid;
+	char *logid;
 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
 	char thread_name[RTE_MAX_THREAD_NAME_LEN];
 
@@ -812,6 +812,12 @@ static void rte_eal_init_alert(const char *msg)
 
 	logid = strrchr(argv[0], '/');
 	logid = strdup(logid ? logid + 1: argv[0]);
+	if (!logid) {
+		rte_eal_init_alert("Cannot allocate memory for logid\n");
+		rte_errno = ENOMEM;
+		rte_atomic32_clear(&run_once);
+		return -1;
+	}
 
 	thread_id = pthread_self();
 
@@ -823,6 +829,8 @@ static void rte_eal_init_alert(const char *msg)
 	if (rte_eal_cpu_init() < 0) {
 		rte_eal_init_alert("Cannot detect lcores.");
 		rte_errno = ENOTSUP;
+		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
 
@@ -831,6 +839,7 @@ static void rte_eal_init_alert(const char *msg)
 		rte_eal_init_alert("Invalid 'command line' arguments.");
 		rte_errno = EINVAL;
 		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
 
@@ -838,12 +847,14 @@ static void rte_eal_init_alert(const char *msg)
 		rte_eal_init_alert("Cannot init plugins\n");
 		rte_errno = EINVAL;
 		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
 
 	if (eal_option_device_parse()) {
 		rte_errno = ENODEV;
 		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
 
@@ -851,6 +862,8 @@ static void rte_eal_init_alert(const char *msg)
 
 	if (rte_eal_intr_init() < 0) {
 		rte_eal_init_alert("Cannot init interrupt-handling thread\n");
+		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
 
@@ -861,6 +874,8 @@ static void rte_eal_init_alert(const char *msg)
 		rte_eal_init_alert("failed to init mp channel\n");
 		if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
 			rte_errno = EFAULT;
+			rte_atomic32_clear(&run_once);
+			free(logid);
 			return -1;
 		}
 	}
@@ -869,6 +884,7 @@ static void rte_eal_init_alert(const char *msg)
 		rte_eal_init_alert("Cannot scan the buses for devices\n");
 		rte_errno = ENODEV;
 		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
 
@@ -893,6 +909,7 @@ static void rte_eal_init_alert(const char *msg)
 			rte_eal_init_alert("Cannot get hugepage information.");
 			rte_errno = EACCES;
 			rte_atomic32_clear(&run_once);
+			free(logid);
 			return -1;
 		}
 	}
@@ -919,8 +936,10 @@ static void rte_eal_init_alert(const char *msg)
 		rte_eal_init_alert("Cannot init logging.");
 		rte_errno = ENOMEM;
 		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
+	free(logid);
 
 #ifdef VFIO_PRESENT
 	if (rte_eal_vfio_setup() < 0) {
lib/librte_eal/linuxapp/eal/eal_log.c +10 -1
index 9d02ddd..076b1b9 100644
@@ -19,6 +19,9 @@
 
 #include "eal_private.h"
 
+#define DEFAULT_LOG_ID_LEN 128
+char g_logid[DEFAULT_LOG_ID_LEN];
+
 /*
  * default log function
  */
@@ -49,12 +52,18 @@
 rte_eal_log_init(const char *id, int facility)
 {
 	FILE *log_stream;
+	int str_len;
 
 	log_stream = fopencookie(NULL, "w+", console_log_func);
 	if (log_stream == NULL)
 		return -1;
 
-	openlog(id, LOG_NDELAY | LOG_PID, facility);
+	str_len = strlen(id);
+	if (str_len >= DEFAULT_LOG_ID_LEN)
+		return -1;
+
+	strncpy(g_logid, id, str_len);
+	openlog(g_logid, LOG_NDELAY | LOG_PID, facility);
 
 	eal_log_set_default(log_stream);
 
Signature · 1 line
1.9.3
#1 lore raw

Hi

Quoted 143 lines

-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Ziye Yang
Sent: Wednesday, September 5, 2018 6:39 AM
To: [email protected]
Cc: Ziye Yang <[email protected]>
Subject: [dpdk-dev] [PATCH v5] linuxapp, eal: Fix the memory leak issue of logid

From: Ziye Yang <[email protected]>

This patch is used to fix the memory leak issue of logid. We use the ASAN test in SPDK when intergrating DPDK and find this memory leak issue.

---
 lib/librte_eal/linuxapp/eal/eal.c     | 21 ++++++++++++++++++++-
 lib/librte_eal/linuxapp/eal/eal_log.c | 11 ++++++++++-
 2 files changed, 30 insertions(+), 2 deletions(-)
lib/librte_eal/linuxapp/eal/eal.c +20 -1
index e59ac65..e150200 100644
@@ -793,7 +793,7 @@ static void rte_eal_init_alert(const char *msg)
 	int i, fctret, ret;
 	pthread_t thread_id;
 	static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
-	const char *logid;
+	char *logid;
 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
 	char thread_name[RTE_MAX_THREAD_NAME_LEN];
 
@@ -812,6 +812,12 @@ static void rte_eal_init_alert(const char *msg)
 
 	logid = strrchr(argv[0], '/');
 	logid = strdup(logid ? logid + 1: argv[0]);
+	if (!logid) {
+		rte_eal_init_alert("Cannot allocate memory for logid\n");
+		rte_errno = ENOMEM;
+		rte_atomic32_clear(&run_once);
+		return -1;
+	}
 
 	thread_id = pthread_self();
 
@@ -823,6 +829,8 @@ static void rte_eal_init_alert(const char *msg)
 	if (rte_eal_cpu_init() < 0) {
 		rte_eal_init_alert("Cannot detect lcores.");
 		rte_errno = ENOTSUP;
+		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
 
@@ -831,6 +839,7 @@ static void rte_eal_init_alert(const char *msg)
 		rte_eal_init_alert("Invalid 'command line' arguments.");
 		rte_errno = EINVAL;
 		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
 
@@ -838,12 +847,14 @@ static void rte_eal_init_alert(const char *msg)
 		rte_eal_init_alert("Cannot init plugins\n");
 		rte_errno = EINVAL;
 		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
 
 	if (eal_option_device_parse()) {
 		rte_errno = ENODEV;
 		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
 
@@ -851,6 +862,8 @@ static void rte_eal_init_alert(const char *msg)
 
 	if (rte_eal_intr_init() < 0) {
 		rte_eal_init_alert("Cannot init interrupt-handling thread\n");
+		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
 
@@ -861,6 +874,8 @@ static void rte_eal_init_alert(const char *msg)
 		rte_eal_init_alert("failed to init mp channel\n");
 		if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
 			rte_errno = EFAULT;
+			rte_atomic32_clear(&run_once);
+			free(logid);
 			return -1;
 		}
 	}
@@ -869,6 +884,7 @@ static void rte_eal_init_alert(const char *msg)
 		rte_eal_init_alert("Cannot scan the buses for devices\n");
 		rte_errno = ENODEV;
 		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
 
@@ -893,6 +909,7 @@ static void rte_eal_init_alert(const char *msg)
 			rte_eal_init_alert("Cannot get hugepage information.");
 			rte_errno = EACCES;
 			rte_atomic32_clear(&run_once);
+			free(logid);
 			return -1;
 		}
 	}
@@ -919,8 +936,10 @@ static void rte_eal_init_alert(const char *msg)
 		rte_eal_init_alert("Cannot init logging.");
 		rte_errno = ENOMEM;
 		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
+	free(logid);
 
 #ifdef VFIO_PRESENT
 	if (rte_eal_vfio_setup() < 0) {
lib/librte_eal/linuxapp/eal/eal_log.c +4 -0
index 9d02ddd..076b1b9 100644
@@ -19,6 +19,9 @@
 
 #include "eal_private.h"
 
+#define DEFAULT_LOG_ID_LEN 128
+char g_logid[DEFAULT_LOG_ID_LEN];
+
 /*
  * default log function
  */
@@ -49,12 +52,18 @@
 rte_eal_log_init(const char *id, int facility)
 {
 	FILE *log_stream;
+	int str_len;
 
 	log_stream = fopencookie(NULL, "w+", console_log_func);

lib/librte_eal/linuxapp/eal/eal_log.c line 66

Quoted 10 lines
lib/librte_eal/linuxapp/eal/eal_log.c +6 -1
 	if (log_stream == NULL)
 		return -1;
 
-	openlog(id, LOG_NDELAY | LOG_PID, facility);
+	str_len = strlen(id);
+	if (str_len >= DEFAULT_LOG_ID_LEN)
+		return -1;
+
+	strncpy(g_logid, id, str_len);
+	openlog(g_logid, LOG_NDELAY | LOG_PID, facility);

Could you explain what is the point here to create a global here and pass pointer to it to openlog()? Konstantin

Quoted 5 lines
lib/librte_eal/linuxapp/eal/eal_log.c +0 -1
 	eal_log_set_default(log_stream);
 
--

1.9.3

author
#2 lore raw

Hi Konstantin,

The global logid is used to prevent the memory leak. If we do not do this, but directly free(logid), this pointer will still be used by openlog, which means that we cannot free the logid anymore. And by adding the global variable, it will not only solve the memory leak but also makes the openlog function still work.

Best Regards
Ziye Yang

-----Original Message-----
From: Ananyev, Konstantin
Sent: Monday, September 10, 2018 7:55 PM
To: Yang, Ziye <[email protected]>; [email protected]
Cc: Ziye Yang <[email protected]>
Subject: RE: [dpdk-dev] [PATCH v5] linuxapp, eal: Fix the memory leak issue of logid

Hi

Quoted 145 lines

-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Ziye Yang
Sent: Wednesday, September 5, 2018 6:39 AM
To: [email protected]
Cc: Ziye Yang <[email protected]>
Subject: [dpdk-dev] [PATCH v5] linuxapp, eal: Fix the memory leak issue of logid

From: Ziye Yang <[email protected]>

This patch is used to fix the memory leak issue of logid. We use the ASAN test in SPDK when intergrating DPDK and find this memory leak issue.

---
 lib/librte_eal/linuxapp/eal/eal.c     | 21 ++++++++++++++++++++-
 lib/librte_eal/linuxapp/eal/eal_log.c | 11 ++++++++++-
 2 files changed, 30 insertions(+), 2 deletions(-)
lib/librte_eal/linuxapp/eal/eal.c +20 -1
index e59ac65..e150200 100644
@@ -793,7 +793,7 @@ static void rte_eal_init_alert(const char *msg)
 	int i, fctret, ret;
 	pthread_t thread_id;
 	static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
-	const char *logid;
+	char *logid;
 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
 	char thread_name[RTE_MAX_THREAD_NAME_LEN];
 
@@ -812,6 +812,12 @@ static void rte_eal_init_alert(const char *msg)
 
 	logid = strrchr(argv[0], '/');
 	logid = strdup(logid ? logid + 1: argv[0]);
+	if (!logid) {
+		rte_eal_init_alert("Cannot allocate memory for logid\n");
+		rte_errno = ENOMEM;
+		rte_atomic32_clear(&run_once);
+		return -1;
+	}
 
 	thread_id = pthread_self();
 
@@ -823,6 +829,8 @@ static void rte_eal_init_alert(const char *msg)
 	if (rte_eal_cpu_init() < 0) {
 		rte_eal_init_alert("Cannot detect lcores.");
 		rte_errno = ENOTSUP;
+		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
 
@@ -831,6 +839,7 @@ static void rte_eal_init_alert(const char *msg)
 		rte_eal_init_alert("Invalid 'command line' arguments.");
 		rte_errno = EINVAL;
 		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
 
@@ -838,12 +847,14 @@ static void rte_eal_init_alert(const char *msg)
 		rte_eal_init_alert("Cannot init plugins\n");
 		rte_errno = EINVAL;
 		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
 
 	if (eal_option_device_parse()) {
 		rte_errno = ENODEV;
 		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
 
@@ -851,6 +862,8 @@ static void rte_eal_init_alert(const char *msg)
 
 	if (rte_eal_intr_init() < 0) {
 		rte_eal_init_alert("Cannot init interrupt-handling thread\n");
+		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
 
@@ -861,6 +874,8 @@ static void rte_eal_init_alert(const char *msg)
 		rte_eal_init_alert("failed to init mp channel\n");
 		if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
 			rte_errno = EFAULT;
+			rte_atomic32_clear(&run_once);
+			free(logid);
 			return -1;
 		}
 	}
@@ -869,6 +884,7 @@ static void rte_eal_init_alert(const char *msg)
 		rte_eal_init_alert("Cannot scan the buses for devices\n");
 		rte_errno = ENODEV;
 		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
 
@@ -893,6 +909,7 @@ static void rte_eal_init_alert(const char *msg)
 			rte_eal_init_alert("Cannot get hugepage information.");
 			rte_errno = EACCES;
 			rte_atomic32_clear(&run_once);
+			free(logid);
 			return -1;
 		}
 	}
@@ -919,8 +936,10 @@ static void rte_eal_init_alert(const char *msg)
 		rte_eal_init_alert("Cannot init logging.");
 		rte_errno = ENOMEM;
 		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
+	free(logid);
 
 #ifdef VFIO_PRESENT
 	if (rte_eal_vfio_setup() < 0) {
lib/librte_eal/linuxapp/eal/eal_log.c +4 -0
index 9d02ddd..076b1b9 100644
@@ -19,6 +19,9 @@
 
 #include "eal_private.h"
 
+#define DEFAULT_LOG_ID_LEN 128
+char g_logid[DEFAULT_LOG_ID_LEN];
+
 /*
  * default log function
  */
@@ -49,12 +52,18 @@
 rte_eal_log_init(const char *id, int facility)  {
 	FILE *log_stream;
+	int str_len;
 
 	log_stream = fopencookie(NULL, "w+", console_log_func);

lib/librte_eal/linuxapp/eal/eal_log.c line 65

Quoted 10 lines
lib/librte_eal/linuxapp/eal/eal_log.c +6 -1
 	if (log_stream == NULL)
 		return -1;
 
-	openlog(id, LOG_NDELAY | LOG_PID, facility);
+	str_len = strlen(id);
+	if (str_len >= DEFAULT_LOG_ID_LEN)
+		return -1;
+
+	strncpy(g_logid, id, str_len);
+	openlog(g_logid, LOG_NDELAY | LOG_PID, facility);

Could you explain what is the point here to create a global here and pass pointer to it to openlog()? Konstantin

Quoted 5 lines
lib/librte_eal/linuxapp/eal/eal_log.c +0 -1
 	eal_log_set_default(log_stream);
 
--

1.9.3

#3 lore raw
Quoted 9 lines

-----Original Message-----
From: Yang, Ziye
Sent: Monday, September 10, 2018 12:58 PM
To: Ananyev, Konstantin <[email protected]>; [email protected]
Cc: Ziye Yang <[email protected]>
Subject: RE: [dpdk-dev] [PATCH v5] linuxapp, eal: Fix the memory leak issue of logid

Quoted 5 lines

Hi Konstantin,

The global logid is used to prevent the memory leak. If we do not do this, but directly free(logid), this pointer will still be used by openlog, which means that we cannot free the logid anymore. And by adding the global variable, it will not only solve the memory leak but also makes the openlog function still work.

But then I suppose you can avoid any dynamic memory allocation at all with something like that:
rte_eal_init(int argc, char **argv)
{
...
static char logid[PATH_MAX];
...
const char *p= strrchr(argv[0], '/');
snprintf(logid, sizeof(logid), "%s", p);
...
rte_eal_log_init(logid, ...);

?
Konstantin

Quoted 179 lines

Best Regards
Ziye Yang

-----Original Message-----
From: Ananyev, Konstantin
Sent: Monday, September 10, 2018 7:55 PM
To: Yang, Ziye <[email protected]>; [email protected]
Cc: Ziye Yang <[email protected]>
Subject: RE: [dpdk-dev] [PATCH v5] linuxapp, eal: Fix the memory leak issue of logid

Hi

Quoted 145 lines

-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Ziye Yang
Sent: Wednesday, September 5, 2018 6:39 AM
To: [email protected]
Cc: Ziye Yang <[email protected]>
Subject: [dpdk-dev] [PATCH v5] linuxapp, eal: Fix the memory leak issue of logid

From: Ziye Yang <[email protected]>

This patch is used to fix the memory leak issue of logid. We use the ASAN test in SPDK when intergrating DPDK and find this memory leak issue.

---
 lib/librte_eal/linuxapp/eal/eal.c     | 21 ++++++++++++++++++++-
 lib/librte_eal/linuxapp/eal/eal_log.c | 11 ++++++++++-
 2 files changed, 30 insertions(+), 2 deletions(-)
lib/librte_eal/linuxapp/eal/eal.c +20 -1
index e59ac65..e150200 100644
@@ -793,7 +793,7 @@ static void rte_eal_init_alert(const char *msg)
 	int i, fctret, ret;
 	pthread_t thread_id;
 	static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
-	const char *logid;
+	char *logid;
 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
 	char thread_name[RTE_MAX_THREAD_NAME_LEN];
 
@@ -812,6 +812,12 @@ static void rte_eal_init_alert(const char *msg)
 
 	logid = strrchr(argv[0], '/');
 	logid = strdup(logid ? logid + 1: argv[0]);
+	if (!logid) {
+		rte_eal_init_alert("Cannot allocate memory for logid\n");
+		rte_errno = ENOMEM;
+		rte_atomic32_clear(&run_once);
+		return -1;
+	}
 
 	thread_id = pthread_self();
 
@@ -823,6 +829,8 @@ static void rte_eal_init_alert(const char *msg)
 	if (rte_eal_cpu_init() < 0) {
 		rte_eal_init_alert("Cannot detect lcores.");
 		rte_errno = ENOTSUP;
+		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
 
@@ -831,6 +839,7 @@ static void rte_eal_init_alert(const char *msg)
 		rte_eal_init_alert("Invalid 'command line' arguments.");
 		rte_errno = EINVAL;
 		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
 
@@ -838,12 +847,14 @@ static void rte_eal_init_alert(const char *msg)
 		rte_eal_init_alert("Cannot init plugins\n");
 		rte_errno = EINVAL;
 		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
 
 	if (eal_option_device_parse()) {
 		rte_errno = ENODEV;
 		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
 
@@ -851,6 +862,8 @@ static void rte_eal_init_alert(const char *msg)
 
 	if (rte_eal_intr_init() < 0) {
 		rte_eal_init_alert("Cannot init interrupt-handling thread\n");
+		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
 
@@ -861,6 +874,8 @@ static void rte_eal_init_alert(const char *msg)
 		rte_eal_init_alert("failed to init mp channel\n");
 		if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
 			rte_errno = EFAULT;
+			rte_atomic32_clear(&run_once);
+			free(logid);
 			return -1;
 		}
 	}
@@ -869,6 +884,7 @@ static void rte_eal_init_alert(const char *msg)
 		rte_eal_init_alert("Cannot scan the buses for devices\n");
 		rte_errno = ENODEV;
 		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
 
@@ -893,6 +909,7 @@ static void rte_eal_init_alert(const char *msg)
 			rte_eal_init_alert("Cannot get hugepage information.");
 			rte_errno = EACCES;
 			rte_atomic32_clear(&run_once);
+			free(logid);
 			return -1;
 		}
 	}
@@ -919,8 +936,10 @@ static void rte_eal_init_alert(const char *msg)
 		rte_eal_init_alert("Cannot init logging.");
 		rte_errno = ENOMEM;
 		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
+	free(logid);
 
 #ifdef VFIO_PRESENT
 	if (rte_eal_vfio_setup() < 0) {
lib/librte_eal/linuxapp/eal/eal_log.c +4 -0
index 9d02ddd..076b1b9 100644
@@ -19,6 +19,9 @@
 
 #include "eal_private.h"
 
+#define DEFAULT_LOG_ID_LEN 128
+char g_logid[DEFAULT_LOG_ID_LEN];
+
 /*
  * default log function
  */
@@ -49,12 +52,18 @@
 rte_eal_log_init(const char *id, int facility)  {
 	FILE *log_stream;
+	int str_len;
 
 	log_stream = fopencookie(NULL, "w+", console_log_func);

lib/librte_eal/linuxapp/eal/eal_log.c line 65

Quoted 10 lines
lib/librte_eal/linuxapp/eal/eal_log.c +6 -1
 	if (log_stream == NULL)
 		return -1;
 
-	openlog(id, LOG_NDELAY | LOG_PID, facility);
+	str_len = strlen(id);
+	if (str_len >= DEFAULT_LOG_ID_LEN)
+		return -1;
+
+	strncpy(g_logid, id, str_len);
+	openlog(g_logid, LOG_NDELAY | LOG_PID, facility);

Could you explain what is the point here to create a global here and pass pointer to it to openlog()? Konstantin

Quoted 5 lines
lib/librte_eal/linuxapp/eal/eal_log.c +0 -1
 	eal_log_set_default(log_stream);
 
--

1.9.3

author
#4 lore raw

Hi Konstantin,

Thanks for your comments. I will revise the patch and submit another patch later.

Best Regards
Ziye Yang

-----Original Message-----
From: Ananyev, Konstantin
Sent: Monday, September 10, 2018 8:19 PM
To: Yang, Ziye <[email protected]>; [email protected]
Cc: Ziye Yang <[email protected]>
Subject: RE: [dpdk-dev] [PATCH v5] linuxapp, eal: Fix the memory leak issue of logid

Quoted 8 lines

-----Original Message-----
From: Yang, Ziye
Sent: Monday, September 10, 2018 12:58 PM
To: Ananyev, Konstantin <[email protected]>; [email protected]
Cc: Ziye Yang <[email protected]>
Subject: RE: [dpdk-dev] [PATCH v5] linuxapp, eal: Fix the memory leak issue of logid

Quoted 5 lines

Hi Konstantin,

The global logid is used to prevent the memory leak. If we do not do
this, but directly free(logid), this pointer will still be used by
openlog, which means that we cannot free the logid anymore. And by adding the global variable, it will not only solve the memory leak but also makes the openlog function still work.

But then I suppose you can avoid any dynamic memory allocation at all with something like that:
rte_eal_init(int argc, char **argv)
{
...
static char logid[PATH_MAX];
...
const char *p= strrchr(argv[0], '/');
snprintf(logid, sizeof(logid), "%s", p);
...
rte_eal_log_init(logid, ...);

?
Konstantin

Quoted 180 lines

Best Regards
Ziye Yang

-----Original Message-----
From: Ananyev, Konstantin
Sent: Monday, September 10, 2018 7:55 PM
To: Yang, Ziye <[email protected]>; [email protected]
Cc: Ziye Yang <[email protected]>
Subject: RE: [dpdk-dev] [PATCH v5] linuxapp, eal: Fix the memory leak issue of logid

Hi

Quoted 145 lines

-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Ziye Yang
Sent: Wednesday, September 5, 2018 6:39 AM
To: [email protected]
Cc: Ziye Yang <[email protected]>
Subject: [dpdk-dev] [PATCH v5] linuxapp, eal: Fix the memory leak issue of logid

From: Ziye Yang <[email protected]>

This patch is used to fix the memory leak issue of logid. We use the ASAN test in SPDK when intergrating DPDK and find this memory leak issue.

---
 lib/librte_eal/linuxapp/eal/eal.c     | 21 ++++++++++++++++++++-
 lib/librte_eal/linuxapp/eal/eal_log.c | 11 ++++++++++-
 2 files changed, 30 insertions(+), 2 deletions(-)
lib/librte_eal/linuxapp/eal/eal.c +20 -1
index e59ac65..e150200 100644
@@ -793,7 +793,7 @@ static void rte_eal_init_alert(const char *msg)
 	int i, fctret, ret;
 	pthread_t thread_id;
 	static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
-	const char *logid;
+	char *logid;
 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
 	char thread_name[RTE_MAX_THREAD_NAME_LEN];
 
@@ -812,6 +812,12 @@ static void rte_eal_init_alert(const char *msg)
 
 	logid = strrchr(argv[0], '/');
 	logid = strdup(logid ? logid + 1: argv[0]);
+	if (!logid) {
+		rte_eal_init_alert("Cannot allocate memory for logid\n");
+		rte_errno = ENOMEM;
+		rte_atomic32_clear(&run_once);
+		return -1;
+	}
 
 	thread_id = pthread_self();
 
@@ -823,6 +829,8 @@ static void rte_eal_init_alert(const char *msg)
 	if (rte_eal_cpu_init() < 0) {
 		rte_eal_init_alert("Cannot detect lcores.");
 		rte_errno = ENOTSUP;
+		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
 
@@ -831,6 +839,7 @@ static void rte_eal_init_alert(const char *msg)
 		rte_eal_init_alert("Invalid 'command line' arguments.");
 		rte_errno = EINVAL;
 		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
 
@@ -838,12 +847,14 @@ static void rte_eal_init_alert(const char *msg)
 		rte_eal_init_alert("Cannot init plugins\n");
 		rte_errno = EINVAL;
 		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
 
 	if (eal_option_device_parse()) {
 		rte_errno = ENODEV;
 		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
 
@@ -851,6 +862,8 @@ static void rte_eal_init_alert(const char *msg)
 
 	if (rte_eal_intr_init() < 0) {
 		rte_eal_init_alert("Cannot init interrupt-handling thread\n");
+		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
 
@@ -861,6 +874,8 @@ static void rte_eal_init_alert(const char *msg)
 		rte_eal_init_alert("failed to init mp channel\n");
 		if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
 			rte_errno = EFAULT;
+			rte_atomic32_clear(&run_once);
+			free(logid);
 			return -1;
 		}
 	}
@@ -869,6 +884,7 @@ static void rte_eal_init_alert(const char *msg)
 		rte_eal_init_alert("Cannot scan the buses for devices\n");
 		rte_errno = ENODEV;
 		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
 
@@ -893,6 +909,7 @@ static void rte_eal_init_alert(const char *msg)
 			rte_eal_init_alert("Cannot get hugepage information.");
 			rte_errno = EACCES;
 			rte_atomic32_clear(&run_once);
+			free(logid);
 			return -1;
 		}
 	}
@@ -919,8 +936,10 @@ static void rte_eal_init_alert(const char *msg)
 		rte_eal_init_alert("Cannot init logging.");
 		rte_errno = ENOMEM;
 		rte_atomic32_clear(&run_once);
+		free(logid);
 		return -1;
 	}
+	free(logid);
 
 #ifdef VFIO_PRESENT
 	if (rte_eal_vfio_setup() < 0) {
lib/librte_eal/linuxapp/eal/eal_log.c +4 -0
index 9d02ddd..076b1b9 100644
@@ -19,6 +19,9 @@
 
 #include "eal_private.h"
 
+#define DEFAULT_LOG_ID_LEN 128
+char g_logid[DEFAULT_LOG_ID_LEN];
+
 /*
  * default log function
  */
@@ -49,12 +52,18 @@
 rte_eal_log_init(const char *id, int facility)  {
 	FILE *log_stream;
+	int str_len;
 
 	log_stream = fopencookie(NULL, "w+", console_log_func);

lib/librte_eal/linuxapp/eal/eal_log.c line 65

Quoted 10 lines
lib/librte_eal/linuxapp/eal/eal_log.c +6 -1
 	if (log_stream == NULL)
 		return -1;
 
-	openlog(id, LOG_NDELAY | LOG_PID, facility);
+	str_len = strlen(id);
+	if (str_len >= DEFAULT_LOG_ID_LEN)
+		return -1;
+
+	strncpy(g_logid, id, str_len);
+	openlog(g_logid, LOG_NDELAY | LOG_PID, facility);

Could you explain what is the point here to create a global here and pass pointer to it to openlog()? Konstantin

Quoted 5 lines
lib/librte_eal/linuxapp/eal/eal_log.c +0 -1
 	eal_log_set_default(log_stream);
 
--

1.9.3

author
#5 lore raw

PATCH v6 1/1 linuxapp, eal: Fix the memory leak issue of logid

In reply to Ziye Yang

From: Ziye Yang <[email protected]>

This patch is used to fix the memory leak issue of logid. We use the ASAN test in SPDK when intergrating DPDK and find this memory leak issue.

By the way, we also fix several missed function call of rte_atomic32_clear.

---
 lib/librte_eal/linuxapp/eal/eal.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
lib/librte_eal/linuxapp/eal/eal.c +7 -4
index e59ac65..a1b218a 100644
@@ -793,7 +793,8 @@ static void rte_eal_init_alert(const char *msg)
 	int i, fctret, ret;
 	pthread_t thread_id;
 	static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
-	const char *logid;
+	const char *p;
+	static char logid[PATH_MAX];
 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
 	char thread_name[RTE_MAX_THREAD_NAME_LEN];
 
@@ -810,9 +811,8 @@ static void rte_eal_init_alert(const char *msg)
 		return -1;
 	}
 
-	logid = strrchr(argv[0], '/');
-	logid = strdup(logid ? logid + 1: argv[0]);
-
+	p = strrchr(argv[0], '/');
+	snprintf(logid, sizeof(logid), "%s", (p ? p + 1: argv[0]));
 	thread_id = pthread_self();
 
 	eal_reset_internal_config(&internal_config);
@@ -823,6 +823,7 @@ static void rte_eal_init_alert(const char *msg)
 	if (rte_eal_cpu_init() < 0) {
 		rte_eal_init_alert("Cannot detect lcores.");
 		rte_errno = ENOTSUP;
+		rte_atomic32_clear(&run_once);
 		return -1;
 	}
 
@@ -851,6 +852,7 @@ static void rte_eal_init_alert(const char *msg)
 
 	if (rte_eal_intr_init() < 0) {
 		rte_eal_init_alert("Cannot init interrupt-handling thread\n");
+		rte_atomic32_clear(&run_once);
 		return -1;
 	}
 
@@ -861,6 +863,7 @@ static void rte_eal_init_alert(const char *msg)
 		rte_eal_init_alert("failed to init mp channel\n");
 		if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
 			rte_errno = EFAULT;
+			rte_atomic32_clear(&run_once);
 			return -1;
 		}
 	}
Signature · 1 line
1.9.3
author
#6 lore raw

PATCH v7 1/1 linuxapp, eal: Fix the memory leak issue of logid

In reply to Ziye Yang

From: Ziye Yang <[email protected]>

This patch is used to fix the memory leak issue of logid. We use the ASAN test in SPDK when intergrating DPDK and find this memory leak issue.

By the way, we also fix several missed function call of rte_atomic32_clear.

---
 lib/librte_eal/linuxapp/eal/eal.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
lib/librte_eal/linuxapp/eal/eal.c +7 -4
index e59ac65..a5129e5 100644
@@ -793,7 +793,8 @@ static void rte_eal_init_alert(const char *msg)
 	int i, fctret, ret;
 	pthread_t thread_id;
 	static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
-	const char *logid;
+	const char *p;
+	static char logid[PATH_MAX];
 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
 	char thread_name[RTE_MAX_THREAD_NAME_LEN];
 
@@ -810,9 +811,8 @@ static void rte_eal_init_alert(const char *msg)
 		return -1;
 	}
 
-	logid = strrchr(argv[0], '/');
-	logid = strdup(logid ? logid + 1: argv[0]);
-
+	p = strrchr(argv[0], '/');
+	snprintf(logid, sizeof(logid), "%s", (p ? p + 1 : argv[0]));
 	thread_id = pthread_self();
 
 	eal_reset_internal_config(&internal_config);
@@ -823,6 +823,7 @@ static void rte_eal_init_alert(const char *msg)
 	if (rte_eal_cpu_init() < 0) {
 		rte_eal_init_alert("Cannot detect lcores.");
 		rte_errno = ENOTSUP;
+		rte_atomic32_clear(&run_once);
 		return -1;
 	}
 
@@ -851,6 +852,7 @@ static void rte_eal_init_alert(const char *msg)
 
 	if (rte_eal_intr_init() < 0) {
 		rte_eal_init_alert("Cannot init interrupt-handling thread\n");
+		rte_atomic32_clear(&run_once);
 		return -1;
 	}
 
@@ -861,6 +863,7 @@ static void rte_eal_init_alert(const char *msg)
 		rte_eal_init_alert("failed to init mp channel\n");
 		if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
 			rte_errno = EFAULT;
+			rte_atomic32_clear(&run_once);
 			return -1;
 		}
 	}
Signature · 1 line
1.9.3
Acked-by on v7 1/1
#7 lore raw
Quoted 66 lines

-----Original Message-----
From: Yang, Ziye
Sent: Tuesday, September 11, 2018 2:28 AM
To: [email protected]
Cc: Ananyev, Konstantin <[email protected]>; Ziye Yang <[email protected]>
Subject: [PATCH v7] linuxapp, eal: Fix the memory leak issue of logid

From: Ziye Yang <[email protected]>

This patch is used to fix the memory leak issue of logid. We use the ASAN test in SPDK when intergrating DPDK and find this memory leak issue.

By the way, we also fix several missed function call of rte_atomic32_clear.

---
 lib/librte_eal/linuxapp/eal/eal.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
lib/librte_eal/linuxapp/eal/eal.c +6 -4
index e59ac65..a5129e5 100644
@@ -793,7 +793,8 @@ static void rte_eal_init_alert(const char *msg)
 	int i, fctret, ret;
 	pthread_t thread_id;
 	static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
-	const char *logid;
+	const char *p;
+	static char logid[PATH_MAX];
 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
 	char thread_name[RTE_MAX_THREAD_NAME_LEN];
 
@@ -810,9 +811,8 @@ static void rte_eal_init_alert(const char *msg)
 		return -1;
 	}
 
-	logid = strrchr(argv[0], '/');
-	logid = strdup(logid ? logid + 1: argv[0]);
-
+	p = strrchr(argv[0], '/');
+	snprintf(logid, sizeof(logid), "%s", (p ? p + 1 : argv[0]));
 	thread_id = pthread_self();
 
 	eal_reset_internal_config(&internal_config);
@@ -823,6 +823,7 @@ static void rte_eal_init_alert(const char *msg)
 	if (rte_eal_cpu_init() < 0) {
 		rte_eal_init_alert("Cannot detect lcores.");
 		rte_errno = ENOTSUP;
+		rte_atomic32_clear(&run_once);
 		return -1;
 	}
 
@@ -851,6 +852,7 @@ static void rte_eal_init_alert(const char *msg)
 
 	if (rte_eal_intr_init() < 0) {
 		rte_eal_init_alert("Cannot init interrupt-handling thread\n");
+		rte_atomic32_clear(&run_once);
 		return -1;
 	}
 

lib/librte_eal/linuxapp/eal/eal.c static void rte_eal_init_alert(const char *msg)

Quoted 8 lines
lib/librte_eal/linuxapp/eal/eal.c +1 -1
@@ -861,6 +863,7 @@ static void rte_eal_init_alert(const char *msg)
 		rte_eal_init_alert("failed to init mp channel\n");
 		if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
 			rte_errno = EFAULT;
+			rte_atomic32_clear(&run_once);
 			return -1;
 		}
 	}
--
Quoted 1 line

1.9.3

#8 lore raw
In reply to Ziye Yang
Quoted 8 lines · Ziye Yang

From: Ziye Yang <[email protected]>

This patch is used to fix the memory leak issue of logid. We use the ASAN test in SPDK when intergrating DPDK and find this memory leak issue.

By the way, we also fix several missed function call of rte_atomic32_clear.

This part I don't understand. It should be a separate proposal.

Quoted 10 lines
---
 lib/librte_eal/linuxapp/eal/eal.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
lib/librte_eal/linuxapp/eal/eal.c +0 -0
index e59ac65..a5129e5 100644

lib/librte_eal/linuxapp/eal/eal.c static void rte_eal_init_alert(const char *msg) · line 797

Quoted 6 lines
lib/librte_eal/linuxapp/eal/eal.c +2 -1
@@ -793,7 +793,8 @@ static void rte_eal_init_alert(const char *msg)
 	int i, fctret, ret;
 	pthread_t thread_id;
 	static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
-	const char *logid;
+	const char *p;
+	static char logid[PATH_MAX];

On a linux system, PATH_MAX is 4096, but an argument may be MAX_ARG_STRLEN which is significantly higher.

Have you thought about an alternative where you keep the strdup and add an atexit() handler to do the free? Otherwise, you'll need to add code to check the string length as well and enforce some kind of size restriction.

Quoted 16 lines
lib/librte_eal/linuxapp/eal/eal.c +2 -3
 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
 	char thread_name[RTE_MAX_THREAD_NAME_LEN];
 
@@ -810,9 +811,8 @@ static void rte_eal_init_alert(const char *msg)
 		return -1;
 	}
 
-	logid = strrchr(argv[0], '/');
-	logid = strdup(logid ? logid + 1: argv[0]);
-
+	p = strrchr(argv[0], '/');
+	snprintf(logid, sizeof(logid), "%s", (p ? p + 1 : argv[0]));
 	thread_id = pthread_self();
 
 	eal_reset_internal_config(&internal_config);

lib/librte_eal/linuxapp/eal/eal.c static void rte_eal_init_alert(const char *msg) · line 826

Quoted 4 lines
lib/librte_eal/linuxapp/eal/eal.c +1 -0
@@ -823,6 +823,7 @@ static void rte_eal_init_alert(const char *msg)
 	if (rte_eal_cpu_init() < 0) {
 		rte_eal_init_alert("Cannot detect lcores.");
 		rte_errno = ENOTSUP;
+		rte_atomic32_clear(&run_once);

This is not recoverable. No amount of retry will allow the user to re-init the eal - the hardware isn't supported. Why clear the run_once flag?

lib/librte_eal/linuxapp/eal/eal.c static void rte_eal_init_alert(const char *msg) · line 855

Quoted 8 lines
lib/librte_eal/linuxapp/eal/eal.c +1 -0
 		return -1;
 	}
 
@@ -851,6 +852,7 @@ static void rte_eal_init_alert(const char *msg)
 
 	if (rte_eal_intr_init() < 0) {
 		rte_eal_init_alert("Cannot init interrupt-handling thread\n");
+		rte_atomic32_clear(&run_once);

Arguable whether or not this is recoverable. IIRC, the eal_intr_init spawns a thread - if it fails to spawn the likelihood is the process won't be able to continue.

lib/librte_eal/linuxapp/eal/eal.c static void rte_eal_init_alert(const char *msg) · line 866

Quoted 8 lines
lib/librte_eal/linuxapp/eal/eal.c +1 -0
 		return -1;
 	}
 
@@ -861,6 +863,7 @@ static void rte_eal_init_alert(const char *msg)
 		rte_eal_init_alert("failed to init mp channel\n");
 		if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
 			rte_errno = EFAULT;
+			rte_atomic32_clear(&run_once);

This is also not recoverable. Why clear the run_once flag?

Quoted 3 lines
lib/librte_eal/linuxapp/eal/eal.c +0 -0
 			return -1;
 		}
 	}
#9 lore raw
Quoted 27 lines

-----Original Message-----
From: Aaron Conole [mailto:[email protected]]
Sent: Tuesday, September 11, 2018 2:47 PM
To: Yang, Ziye <[email protected]>
Cc: [email protected]; Ananyev, Konstantin <[email protected]>; Ziye Yang <[email protected]>
Subject: Re: [dpdk-dev] [PATCH v7] linuxapp, eal: Fix the memory leak issue of logid

Quoted 8 lines · Ziye Yang

From: Ziye Yang <[email protected]>

This patch is used to fix the memory leak issue of logid. We use the ASAN test in SPDK when intergrating DPDK and find this memory leak issue.

By the way, we also fix several missed function call of rte_atomic32_clear.

This part I don't understand. It should be a separate proposal.

Quoted 5 lines
---
 lib/librte_eal/linuxapp/eal/eal.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
Quoted 14 lines

lib/librte_eal/linuxapp/eal/eal.c static void rte_eal_init_alert(const char *msg) · line 797

Quoted 11 lines
lib/librte_eal/linuxapp/eal/eal.c +2 -1
index e59ac65..a5129e5 100644
@@ -793,7 +793,8 @@ static void rte_eal_init_alert(const char *msg)
 	int i, fctret, ret;
 	pthread_t thread_id;
 	static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
-	const char *logid;
+	const char *p;
+	static char logid[PATH_MAX];

On a linux system, PATH_MAX is 4096, but an argument may be MAX_ARG_STRLEN which is significantly higher.

But we only interested here in 'basename(argv[0])'.
Surely it shouldn't be bigger than PATH_MAX unless something is terribly wrong here.

Quoted 5 lines

Have you thought about an alternative where you keep the strdup and add an atexit() handler to do the free? Otherwise, you'll need to add code to check the string length as well and enforce some kind of size restriction.

snprintf() below will do a safe truncation for us.

Quoted 53 lines
Quoted 17 lines
lib/librte_eal/linuxapp/eal/eal.c +2 -3
 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
 	char thread_name[RTE_MAX_THREAD_NAME_LEN];
 
@@ -810,9 +811,8 @@ static void rte_eal_init_alert(const char *msg)
 		return -1;
 	}
 
-	logid = strrchr(argv[0], '/');
-	logid = strdup(logid ? logid + 1: argv[0]);
-
+	p = strrchr(argv[0], '/');
+	snprintf(logid, sizeof(logid), "%s", (p ? p + 1 : argv[0]));
 	thread_id = pthread_self();
 
 	eal_reset_internal_config(&internal_config);

lib/librte_eal/linuxapp/eal/eal.c static void rte_eal_init_alert(const char *msg) · line 826

Quoted 4 lines
lib/librte_eal/linuxapp/eal/eal.c +1 -0
@@ -823,6 +823,7 @@ static void rte_eal_init_alert(const char *msg)
 	if (rte_eal_cpu_init() < 0) {
 		rte_eal_init_alert("Cannot detect lcores.");
 		rte_errno = ENOTSUP;
+		rte_atomic32_clear(&run_once);

This is not recoverable. No amount of retry will allow the user to re-init the eal - the hardware isn't supported. Why clear the run_once flag?

lib/librte_eal/linuxapp/eal/eal.c static void rte_eal_init_alert(const char *msg) · line 855

Quoted 8 lines
lib/librte_eal/linuxapp/eal/eal.c +1 -0
 		return -1;
 	}
 
@@ -851,6 +852,7 @@ static void rte_eal_init_alert(const char *msg)
 
 	if (rte_eal_intr_init() < 0) {
 		rte_eal_init_alert("Cannot init interrupt-handling thread\n");
+		rte_atomic32_clear(&run_once);

Arguable whether or not this is recoverable. IIRC, the eal_intr_init spawns a thread - if it fails to spawn the likelihood is the process won't be able to continue.

lib/librte_eal/linuxapp/eal/eal.c static void rte_eal_init_alert(const char *msg) · line 866

Quoted 8 lines
lib/librte_eal/linuxapp/eal/eal.c +1 -0
 		return -1;
 	}
 
@@ -861,6 +863,7 @@ static void rte_eal_init_alert(const char *msg)
 		rte_eal_init_alert("failed to init mp channel\n");
 		if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
 			rte_errno = EFAULT;
+			rte_atomic32_clear(&run_once);

This is also not recoverable. Why clear the run_once flag?

Quoted 3 lines
lib/librte_eal/linuxapp/eal/eal.c +0 -0
 			return -1;
 		}
 	}
#10 lore raw
Quoted 41 lines · Konstantin
Quoted 26 lines

-----Original Message-----
From: Aaron Conole [mailto:[email protected]]
Sent: Tuesday, September 11, 2018 2:47 PM
To: Yang, Ziye <[email protected]>
Cc: [email protected]; Ananyev, Konstantin
<[email protected]>; Ziye Yang <[email protected]>
Subject: Re: [dpdk-dev] [PATCH v7] linuxapp, eal: Fix the memory leak issue of logid

Quoted 8 lines · Ziye Yang

From: Ziye Yang <[email protected]>

This patch is used to fix the memory leak issue of logid. We use the ASAN test in SPDK when intergrating DPDK and find this memory leak issue.

By the way, we also fix several missed function call of rte_atomic32_clear.

This part I don't understand. It should be a separate proposal.

Quoted 5 lines
---
 lib/librte_eal/linuxapp/eal/eal.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
Quoted 14 lines

lib/librte_eal/linuxapp/eal/eal.c static void rte_eal_init_alert(const char *msg) · line 797

Quoted 11 lines
lib/librte_eal/linuxapp/eal/eal.c +2 -1
index e59ac65..a5129e5 100644
@@ -793,7 +793,8 @@ static void rte_eal_init_alert(const char *msg)
 	int i, fctret, ret;
 	pthread_t thread_id;
 	static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
-	const char *logid;
+	const char *p;
+	static char logid[PATH_MAX];

On a linux system, PATH_MAX is 4096, but an argument may be MAX_ARG_STRLEN which is significantly higher.

Quoted 2 lines

But we only interested here in 'basename(argv[0])'.
Surely it shouldn't be bigger than PATH_MAX unless something is terribly wrong here.

The application has full control of what it passes into EAL, but does it sanitize and scrub the arguments? We make an assumption that the argv,argc are direct from cmdline and are calls by the user at a shell. But nothing forces this to be true.

Quoted 7 lines
Quoted 5 lines

Have you thought about an alternative where you keep the strdup and add an atexit() handler to do the free? Otherwise, you'll need to add code to check the string length as well and enforce some kind of size restriction.

snprintf() below will do a safe truncation for us.

Anyway, yes. I completely glossed over it.

Quoted 53 lines
Quoted 53 lines
Quoted 17 lines
lib/librte_eal/linuxapp/eal/eal.c +2 -3
 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
 	char thread_name[RTE_MAX_THREAD_NAME_LEN];
 
@@ -810,9 +811,8 @@ static void rte_eal_init_alert(const char *msg)
 		return -1;
 	}
 
-	logid = strrchr(argv[0], '/');
-	logid = strdup(logid ? logid + 1: argv[0]);
-
+	p = strrchr(argv[0], '/');
+	snprintf(logid, sizeof(logid), "%s", (p ? p + 1 : argv[0]));
 	thread_id = pthread_self();
 
 	eal_reset_internal_config(&internal_config);

lib/librte_eal/linuxapp/eal/eal.c static void rte_eal_init_alert(const char *msg) · line 826

Quoted 4 lines
lib/librte_eal/linuxapp/eal/eal.c +1 -0
@@ -823,6 +823,7 @@ static void rte_eal_init_alert(const char *msg)
 	if (rte_eal_cpu_init() < 0) {
 		rte_eal_init_alert("Cannot detect lcores.");
 		rte_errno = ENOTSUP;
+		rte_atomic32_clear(&run_once);

This is not recoverable. No amount of retry will allow the user to re-init the eal - the hardware isn't supported. Why clear the run_once flag?

lib/librte_eal/linuxapp/eal/eal.c static void rte_eal_init_alert(const char *msg) · line 855

Quoted 8 lines
lib/librte_eal/linuxapp/eal/eal.c +1 -0
 		return -1;
 	}
 
@@ -851,6 +852,7 @@ static void rte_eal_init_alert(const char *msg)
 
 	if (rte_eal_intr_init() < 0) {
 		rte_eal_init_alert("Cannot init interrupt-handling thread\n");
+		rte_atomic32_clear(&run_once);

Arguable whether or not this is recoverable. IIRC, the eal_intr_init spawns a thread - if it fails to spawn the likelihood is the process won't be able to continue.

lib/librte_eal/linuxapp/eal/eal.c static void rte_eal_init_alert(const char *msg) · line 866

Quoted 8 lines
lib/librte_eal/linuxapp/eal/eal.c +1 -0
 		return -1;
 	}
 
@@ -861,6 +863,7 @@ static void rte_eal_init_alert(const char *msg)
 		rte_eal_init_alert("failed to init mp channel\n");
 		if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
 			rte_errno = EFAULT;
+			rte_atomic32_clear(&run_once);

This is also not recoverable. Why clear the run_once flag?

Quoted 3 lines
lib/librte_eal/linuxapp/eal/eal.c +0 -0
 			return -1;
 		}
 	}
#11 lore raw
In reply to Ananyev, Konstantin

On second thought - please just fold in the patch I proposed back in July that I was told would be merged:

http://mails.dpdk.org/archives/dev/2018-July/108445.html
http://mails.dpdk.org/archives/dev/2018-August/109177.html

It doesn't include the issues calling rte_atomic32_clear (that I have concerns about) and preserves an arbitrary length value being passed through argv/argc.

:-)

-Aaron

Quoted 107 lines · Konstantin
Quoted 27 lines

-----Original Message-----
From: Aaron Conole [mailto:[email protected]]
Sent: Tuesday, September 11, 2018 2:47 PM
To: Yang, Ziye <[email protected]>
Cc: [email protected]; Ananyev, Konstantin <[email protected]>; Ziye Yang <[email protected]>
Subject: Re: [dpdk-dev] [PATCH v7] linuxapp, eal: Fix the memory leak issue of logid

Quoted 8 lines · Ziye Yang

From: Ziye Yang <[email protected]>

This patch is used to fix the memory leak issue of logid. We use the ASAN test in SPDK when intergrating DPDK and find this memory leak issue.

By the way, we also fix several missed function call of rte_atomic32_clear.

This part I don't understand. It should be a separate proposal.

Quoted 5 lines
---
 lib/librte_eal/linuxapp/eal/eal.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
Quoted 14 lines

lib/librte_eal/linuxapp/eal/eal.c static void rte_eal_init_alert(const char *msg) · line 797

Quoted 11 lines
lib/librte_eal/linuxapp/eal/eal.c +2 -1
index e59ac65..a5129e5 100644
@@ -793,7 +793,8 @@ static void rte_eal_init_alert(const char *msg)
 	int i, fctret, ret;
 	pthread_t thread_id;
 	static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
-	const char *logid;
+	const char *p;
+	static char logid[PATH_MAX];

On a linux system, PATH_MAX is 4096, but an argument may be MAX_ARG_STRLEN which is significantly higher.

But we only interested here in 'basename(argv[0])'. Surely it shouldn't be bigger than PATH_MAX unless something is terribly wrong here.

Quoted 5 lines

Have you thought about an alternative where you keep the strdup and add an atexit() handler to do the free? Otherwise, you'll need to add code to check the string length as well and enforce some kind of size restriction.

snprintf() below will do a safe truncation for us.

Quoted 53 lines
Quoted 17 lines
lib/librte_eal/linuxapp/eal/eal.c +2 -3
 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
 	char thread_name[RTE_MAX_THREAD_NAME_LEN];
 
@@ -810,9 +811,8 @@ static void rte_eal_init_alert(const char *msg)
 		return -1;
 	}
 
-	logid = strrchr(argv[0], '/');
-	logid = strdup(logid ? logid + 1: argv[0]);
-
+	p = strrchr(argv[0], '/');
+	snprintf(logid, sizeof(logid), "%s", (p ? p + 1 : argv[0]));
 	thread_id = pthread_self();
 
 	eal_reset_internal_config(&internal_config);

lib/librte_eal/linuxapp/eal/eal.c static void rte_eal_init_alert(const char *msg) · line 826

Quoted 4 lines
lib/librte_eal/linuxapp/eal/eal.c +1 -0
@@ -823,6 +823,7 @@ static void rte_eal_init_alert(const char *msg)
 	if (rte_eal_cpu_init() < 0) {
 		rte_eal_init_alert("Cannot detect lcores.");
 		rte_errno = ENOTSUP;
+		rte_atomic32_clear(&run_once);

This is not recoverable. No amount of retry will allow the user to re-init the eal - the hardware isn't supported. Why clear the run_once flag?

lib/librte_eal/linuxapp/eal/eal.c static void rte_eal_init_alert(const char *msg) · line 855

Quoted 8 lines
lib/librte_eal/linuxapp/eal/eal.c +1 -0
 		return -1;
 	}
 
@@ -851,6 +852,7 @@ static void rte_eal_init_alert(const char *msg)
 
 	if (rte_eal_intr_init() < 0) {
 		rte_eal_init_alert("Cannot init interrupt-handling thread\n");
+		rte_atomic32_clear(&run_once);

Arguable whether or not this is recoverable. IIRC, the eal_intr_init spawns a thread - if it fails to spawn the likelihood is the process won't be able to continue.

lib/librte_eal/linuxapp/eal/eal.c static void rte_eal_init_alert(const char *msg) · line 866

Quoted 8 lines
lib/librte_eal/linuxapp/eal/eal.c +1 -0
 		return -1;
 	}
 
@@ -861,6 +863,7 @@ static void rte_eal_init_alert(const char *msg)
 		rte_eal_init_alert("failed to init mp channel\n");
 		if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
 			rte_errno = EFAULT;
+			rte_atomic32_clear(&run_once);

This is also not recoverable. Why clear the run_once flag?

Quoted 3 lines
lib/librte_eal/linuxapp/eal/eal.c +0 -0
 			return -1;
 		}
 	}
author
#12 lore raw

PATCH v8 1/1 linuxapp, eal: Fix the memory leak issue of logid

In reply to Ziye Yang

From: Ziye Yang <[email protected]>

This patch is used to fix the memory leak issue of logid. We use the ASAN test in SPDK when intergrating DPDK and find this memory leak issue.

---
 lib/librte_eal/linuxapp/eal/eal.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
lib/librte_eal/linuxapp/eal/eal.c +4 -4
index e59ac65..46494f9 100644
@@ -793,7 +793,8 @@ static void rte_eal_init_alert(const char *msg)
 	int i, fctret, ret;
 	pthread_t thread_id;
 	static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
-	const char *logid;
+	const char *p;
+	static char logid[PATH_MAX];
 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
 	char thread_name[RTE_MAX_THREAD_NAME_LEN];
 
@@ -810,9 +811,8 @@ static void rte_eal_init_alert(const char *msg)
 		return -1;
 	}
 
-	logid = strrchr(argv[0], '/');
-	logid = strdup(logid ? logid + 1: argv[0]);
-
+	p = strrchr(argv[0], '/');
+	snprintf(logid, sizeof(logid), "%s", (p ? p + 1 : argv[0]));
 	thread_id = pthread_self();
 
 	eal_reset_internal_config(&internal_config);
Signature · 1 line
1.9.3
Reviewed-by on v8 1/1
#13 lore raw
Quoted 7 lines · Ziye Yang

From: Ziye Yang <[email protected]>

This patch is used to fix the memory leak issue of logid. We use the ASAN test in SPDK when intergrating DPDK and find this memory leak issue.

check-git-log.sh complains about commit log, it can be:
eal/linux: fix memory leak of logid

Also needs:

Fixes d8a2bc71dfc2 ("log: remove app path from syslog id")
#14 lore raw
In reply to Ziye Yang

12/09/2018 03:31, Ziye Yang:

lib/librte_eal/linuxapp/eal/eal.c static void rte_eal_init_alert(const char *msg) · line 815

Quoted 13 lines

From: Ziye Yang <[email protected]>

This patch is used to fix the memory leak issue of logid. We use the ASAN test in SPDK when intergrating DPDK and find this memory leak issue.

---

lib/librte_eal/linuxapp/eal/eal.c +2 -3
-	logid = strrchr(argv[0], '/');
-	logid = strdup(logid ? logid + 1: argv[0]);
-
+	p = strrchr(argv[0], '/');
+	snprintf(logid, sizeof(logid), "%s", (p ? p + 1 : argv[0]));

Shouldn't it be strlcpy instead of snprintf?

#15 lore raw

22/10/2018 10:00, Thomas Monjalon:

Quoted 7 lines

12/09/2018 03:31, Ziye Yang:

Quoted 6 lines

From: Ziye Yang <[email protected]>

This patch is used to fix the memory leak issue of logid. We use the ASAN test in SPDK when intergrating DPDK and find this memory leak issue.

Quoted 9 lines

lib/librte_eal/linuxapp/eal/eal.c static void rte_eal_init_alert(const char *msg) · line 815

Quoted 7 lines

---

lib/librte_eal/linuxapp/eal/eal.c +2 -3
-	logid = strrchr(argv[0], '/');
-	logid = strdup(logid ? logid + 1: argv[0]);
-
+	p = strrchr(argv[0], '/');
+	snprintf(logid, sizeof(logid), "%s", (p ? p + 1 : argv[0]));

Shouldn't it be strlcpy instead of snprintf?

Applied with suggested replacement:

- snprintf(logid, sizeof(logid), "%s", (p ? p + 1 : argv[0]));
+ strlcpy(logid, p ? p + 1 : argv[0], sizeof(logid));