Skip to contentSkip to conversation

eal: fix rte_zalloc_socket to zero memory

PATCH Superseded David Harton 5 participants 10 messages Dec 7, 2018 – Dec 10, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 eal: fix rte_zalloc_socket to zero memory

The zalloc and calloc functions do not actually zero the memory. Added memset to rte_zmalloc_socket() so allocated memory is cleared.

---
 lib/librte_eal/common/rte_malloc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
lib/librte_eal/common/rte_malloc.c +3 -1
index 0da5ad5e8..be382e534 100644
@@ -74,7 +74,9 @@ rte_malloc(const char *type, size_t size, unsigned align)
 void *
 rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket)
 {
-	return rte_malloc_socket(type, size, align, socket);
+	void *new_ptr = rte_malloc_socket(type, size, align, socket);
+	if (new_ptr) memset(new_ptr, 0, size);
+	return new_ptr;
 }
 
 /*
Signature · 1 line
2.19.1
#1 lore raw
Quoted 20 lines · David Harton

The zalloc and calloc functions do not actually zero the memory. Added memset to rte_zmalloc_socket() so allocated memory is cleared.

---
lib/librte_eal/common/rte_malloc.c | 4 +++-

1 file changed, 3 insertions(+), 1 deletion(-)
lib/librte_eal/common/rte_malloc.c +0 -0
index 0da5ad5e8..be382e534 100644
@@ -74,7 +74,9 @@ rte_malloc(const char *type, size_t size, unsigned align)

void *
rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket) {

lib/librte_eal/common/rte_malloc.c rte_malloc(const char *type, size_t size, unsigned align) · line 78

Quoted 3 lines
lib/librte_eal/common/rte_malloc.c +2 -1
-	return rte_malloc_socket(type, size, align, socket);
+	void *new_ptr = rte_malloc_socket(type, size, align, socket);
+	if (new_ptr) memset(new_ptr, 0, size);

Someone will hate me, but the memset() line should be on the next line not on the ‘if’ line. It does not explicitly state in the coding style, but do not see any example in the coding style on having the one line statement on the line of the ‘if’.

What is the ruling here, I would suggest it be on the next line?

Quoted 7 lines
lib/librte_eal/common/rte_malloc.c +1 -0
+	return new_ptr;

}

/*

Signature · 1 line
2.19.1

Regards,
Keith

#2 lore raw
Quoted 38 lines

-----Original Message-----
From: Wiles, Keith <[email protected]>
Sent: Friday, December 07, 2018 6:41 PM
To: David Harton (dharton) <[email protected]>
Cc: [email protected]; Burakov, Anatoly <[email protected]>
Subject: Re: [dpdk-dev] [PATCH] eal: fix rte_zalloc_socket to zero memory

Quoted 16 lines · David Harton

The zalloc and calloc functions do not actually zero the memory. Added memset to rte_zmalloc_socket() so allocated memory is cleared.

---
lib/librte_eal/common/rte_malloc.c | 4 +++-

1 file changed, 3 insertions(+), 1 deletion(-)
lib/librte_eal/common/rte_malloc.c +0 -0
index 0da5ad5e8..be382e534 100644

lib/librte_eal/common/rte_malloc.c rte_malloc(const char *type, size_t size, unsigned · line 77

Quoted 5 lines
lib/librte_eal/common/rte_malloc.c +2 -1
@@ -74,7 +74,9 @@ rte_malloc(const char *type, size_t size, unsigned
 align) void * rte_zmalloc_socket(const char *type, size_t size,
 unsigned align, int socket) {
-	return rte_malloc_socket(type, size, align, socket);
+	void *new_ptr = rte_malloc_socket(type, size, align, socket);
+	if (new_ptr) memset(new_ptr, 0, size);

Someone will hate me, but the memset() line should be on the next line not on the ‘if’ line. It does not explicitly state in the coding style, but do not see any example in the coding style on having the one line statement on the line of the ‘if’.

Quoted 1 line

What is the ruling here, I would suggest it be on the next line?

FWIW, I copied the pattern from rte_free() but I it is the only use in the file.

I have no problems changing it and fixing rte_free() too if that is the desire.

Quoted 11 lines
Quoted 8 lines
lib/librte_eal/common/rte_malloc.c +1 -1
+	return new_ptr;
 }
 
 /*
--

2.19.1

Regards,
Keith

#3 lore raw

On Dec 7, 2018, at 4:47 PM, David Harton (dharton) <[email protected]<mailto:[email protected]>> wrote:

-----Original Message-----
From: Wiles, Keith <[email protected]<mailto:[email protected]>>
Sent: Friday, December 07, 2018 6:41 PM
To: David Harton (dharton) <[email protected]<mailto:[email protected]>>
Cc: [email protected]<mailto:[email protected]>; Burakov, Anatoly <[email protected]<mailto:[email protected]>>
Subject: Re: [dpdk-dev] [PATCH] eal: fix rte_zalloc_socket to zero memory

On Dec 7, 2018, at 3:24 PM, David Harton <[email protected]<mailto:[email protected]>> wrote:

The zalloc and calloc functions do not actually zero the memory. Added memset to rte_zmalloc_socket() so allocated memory is cleared.

---
lib/librte_eal/common/rte_malloc.c | 4 +++-

1 file changed, 3 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/lib/librte_eal/common/rte_malloc.c
index 0da5ad5e8..be382e534 100644

lib/librte_eal/common/rte_malloc.c +0 -0
@@ -74,7 +74,9 @@ rte_malloc(const char *type, size_t size, unsigned

align) void * rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket) {
- return rte_malloc_socket(type, size, align, socket);
+ void *new_ptr = rte_malloc_socket(type, size, align, socket);
+ if (new_ptr) memset(new_ptr, 0, size);

Someone will hate me, but the memset() line should be on the next line not on the ‘if’ line. It does not explicitly state in the coding style, but do not see any example in the coding style on having the one line statement on the line of the ‘if’.

What is the ruling here, I would suggest it be on the next line?

FWIW, I copied the pattern from rte_free() but I it is the only use in the file.

I have no problems changing it and fixing rte_free() too if that is the desire.

Lets wait for the big guns to decide what is the correct method and updated the coding style.

This also points to a problem as we need a tool to run and fix up the code, like uncrustify or similar tool, this way I can stop being the code style police :-)

+ return new_ptr;
}

/*
--
2.19.1

Regards,
Keith

Regards,
Keith

#4 lore raw
In reply to David Harton (dharton)

fix up my emailer issue, sending out in non-plain text. :-(

Quoted 40 lines · David Harton (dharton)
Quoted 39 lines

-----Original Message-----
From: Wiles, Keith <[email protected]>
Sent: Friday, December 07, 2018 6:41 PM
To: David Harton (dharton) <[email protected]>
Cc: [email protected]; Burakov, Anatoly <[email protected]>
Subject: Re: [dpdk-dev] [PATCH] eal: fix rte_zalloc_socket to zero memory

Quoted 16 lines · David Harton

The zalloc and calloc functions do not actually zero the memory. Added memset to rte_zmalloc_socket() so allocated memory is cleared.

---
lib/librte_eal/common/rte_malloc.c | 4 +++-

1 file changed, 3 insertions(+), 1 deletion(-)
lib/librte_eal/common/rte_malloc.c +0 -0
index 0da5ad5e8..be382e534 100644

lib/librte_eal/common/rte_malloc.c rte_malloc(const char *type, size_t size, unsigned · line 77

Quoted 5 lines
lib/librte_eal/common/rte_malloc.c +2 -1
@@ -74,7 +74,9 @@ rte_malloc(const char *type, size_t size, unsigned
 align) void * rte_zmalloc_socket(const char *type, size_t size,
 unsigned align, int socket) {
-	return rte_malloc_socket(type, size, align, socket);
+	void *new_ptr = rte_malloc_socket(type, size, align, socket);
+	if (new_ptr) memset(new_ptr, 0, size);

Someone will hate me, but the memset() line should be on the next line not on the ‘if’ line. It does not explicitly state in the coding style, but do not see any example in the coding style on having the one line statement on the line of the ‘if’.

Quoted 6 lines
Quoted 1 line

What is the ruling here, I would suggest it be on the next line?

FWIW, I copied the pattern from rte_free() but I it is the only use in the file.

I have no problems changing it and fixing rte_free() too if that is the desire.

Let's wait for the big guns to decide what is the correct method and updated the coding style.

This also points to a problem as we need a tool to run and fix up the code, like uncrustify or similar tool, this way I can stop being the code style police :-)

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
lib/librte_eal/common/rte_malloc.c +1 -0
+	return new_ptr;
 }
 
Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
lib/librte_eal/common/rte_malloc.c +0 -1
 /*
--

2.19.1

Regards,
Keith

Regards,
Keith

#5 lore raw
In reply to David Harton
Quoted 13 lines · David Harton

The zalloc and calloc functions do not actually zero the memory. Added memset to rte_zmalloc_socket() so allocated memory is cleared.

---
  lib/librte_eal/common/rte_malloc.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
lib/librte_eal/common/rte_malloc.c +0 -0
index 0da5ad5e8..be382e534 100644

lib/librte_eal/common/rte_malloc.c rte_malloc(const char *type, size_t size, unsigned align) · line 78

Quoted 6 lines
lib/librte_eal/common/rte_malloc.c +2 -1
@@ -74,7 +74,9 @@ rte_malloc(const char *type, size_t size, unsigned align)
  void *
  rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket)
  {
-	return rte_malloc_socket(type, size, align, socket);
+	void *new_ptr = rte_malloc_socket(type, size, align, socket);
+	if (new_ptr) memset(new_ptr, 0, size);

Maybe it would be worth to have a likely() here.

Quoted 5 lines
lib/librte_eal/common/rte_malloc.c +1 -0
+	return new_ptr;
  }
 
  /*
#6 lore raw

PATCH v2 1/1 eal: fix rte_zalloc_socket to zero memory

In reply to David Harton

The zalloc and calloc functions do not actually zero the memory. Added memset to rte_zmalloc_socket() so allocated memory is cleared.

---

v2 Indented if clause

 lib/librte_eal/common/rte_malloc.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
lib/librte_eal/common/rte_malloc.c +4 -1
index 0da5ad5e8..40a5384ff 100644
@@ -74,7 +74,10 @@ rte_malloc(const char *type, size_t size, unsigned align)
 void *
 rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket)
 {
-	return rte_malloc_socket(type, size, align, socket);
+	void *new_ptr = rte_malloc_socket(type, size, align, socket);
+	if (new_ptr)
+		memset(new_ptr, 0, size);
+	return new_ptr;
 }
 
 /*
Signature · 1 line
2.19.1
#7 lore raw
Quoted 18 lines · David Harton

The zalloc and calloc functions do not actually zero the memory. Added memset to rte_zmalloc_socket() so allocated memory is cleared.

---

v2 Indented if clause

  lib/librte_eal/common/rte_malloc.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
lib/librte_eal/common/rte_malloc.c +0 -0
index 0da5ad5e8..40a5384ff 100644
@@ -74,7 +74,10 @@ rte_malloc(const char *type, size_t size, unsigned align)
  void *

lib/librte_eal/common/rte_malloc.c rte_malloc(const char *type, size_t size, unsigned align) · line 83

Quoted 10 lines
lib/librte_eal/common/rte_malloc.c +4 -1
  rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket)
  {
-	return rte_malloc_socket(type, size, align, socket);
+	void *new_ptr = rte_malloc_socket(type, size, align, socket);
+	if (new_ptr)
+		memset(new_ptr, 0, size);
+	return new_ptr;
  }
 
  /*

NAK, see comments to v1.

TL;DR this is not needed, rte_free does memset(0). If there are circumstances where some memory is not erased, this is a bug and should be fixed at the source.

Signature · 2 lines
Thanks,
Anatoly
#8 lore raw
In reply to David Harton
Quoted 13 lines · David Harton

The zalloc and calloc functions do not actually zero the memory. Added memset to rte_zmalloc_socket() so allocated memory is cleared.

---
 lib/librte_eal/common/rte_malloc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
lib/librte_eal/common/rte_malloc.c +0 -0
index 0da5ad5e8..be382e534 100644

lib/librte_eal/common/rte_malloc.c rte_malloc(const char *type, size_t size, unsigned align) · line 80

Quoted 8 lines
lib/librte_eal/common/rte_malloc.c +3 -1
@@ -74,7 +74,9 @@ rte_malloc(const char *type, size_t size, unsigned align)
 void *
 rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket)
 {
-	return rte_malloc_socket(type, size, align, socket);
+	void *new_ptr = rte_malloc_socket(type, size, align, socket);
+	if (new_ptr) memset(new_ptr, 0, size);
+	return new_ptr;
 }

While this is functionally correct, I believe this memset should not actually be needed. A few years back we changed the behaviour in DPDK to always zero memory on free, rather than zeroing on allocate. This worked fine because the kernel always gave us zeroed hugepages and zeroing them a second time was a waste of cycles. The percentage of memory that was subsequently freed and reallocated was small so zeroing on free saved quite a bit of processing time, especially at app startup.

If, following all the memory rework in recent releases, this scheme of zeroing on free no longer works, I'd rather see that fixed than go back to the scheme of zeroing on allocation. [Assuming we do fix it, a comment explaining the missing memset would also be good to avoid future patches here]

Regards,
/Bruce

#9 lore raw
Quoted 31 lines · Bruce Richardson
Quoted 13 lines · David Harton

The zalloc and calloc functions do not actually zero the memory. Added memset to rte_zmalloc_socket() so allocated memory is cleared.

---
  lib/librte_eal/common/rte_malloc.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
lib/librte_eal/common/rte_malloc.c +0 -0
index 0da5ad5e8..be382e534 100644

lib/librte_eal/common/rte_malloc.c rte_malloc(const char *type, size_t size, unsigned align) · line 80

Quoted 8 lines
lib/librte_eal/common/rte_malloc.c +3 -1
@@ -74,7 +74,9 @@ rte_malloc(const char *type, size_t size, unsigned align)
  void *
  rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket)
  {
-	return rte_malloc_socket(type, size, align, socket);
+	void *new_ptr = rte_malloc_socket(type, size, align, socket);
+	if (new_ptr) memset(new_ptr, 0, size);
+	return new_ptr;
  }

While this is functionally correct, I believe this memset should not actually be needed. A few years back we changed the behaviour in DPDK to always zero memory on free, rather than zeroing on allocate. This worked fine because the kernel always gave us zeroed hugepages and zeroing them a second time was a waste of cycles. The percentage of memory that was subsequently freed and reallocated was small so zeroing on free saved quite a bit of processing time, especially at app startup.

Quoted 5 lines

If, following all the memory rework in recent releases, this scheme of zeroing on free no longer works, I'd rather see that fixed than go back to the scheme of zeroing on allocation. [Assuming we do fix it, a comment explaining the missing memset would also be good to avoid future patches here]

Bruce is correct. Memory is zeroed-out on free, and we get zeroed-out pages from the kernel, so memory from rte_malloc and rte_zmalloc are (or should be) functionally equivalent.

If there are any circumstances where memory is not being freed (and there were a few bugs like that), then i'd rather fix those too.

Quoted 4 lines

Regards,
/Bruce

Signature · 2 lines
Thanks,
Anatoly