[v3] mem: fix undefined behavior in NUMA code

Message ID 2f0bea6a3860143bfbcf31142440a0e4db7c7933.1537521888.git.anatoly.burakov@intel.com (mailing list archive)
State Accepted, archived
Delegated to: Thomas Monjalon
Headers
Series [v3] mem: fix undefined behavior in NUMA code |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation success Compilation OK

Commit Message

Anatoly Burakov Sept. 21, 2018, 9:27 a.m. UTC
  When NUMA-aware hugepages config option is set, we rely on
libnuma to tell the kernel to allocate hugepages on a specific
NUMA node. However, we allocate node mask before we check if
NUMA is available in the first place, which, according to
the manpage [1], causes undefined behaviour.

Fix by only using nodemask when we have NUMA available.

[1] https://linux.die.net/man/3/numa_alloc_onnode

Bugzilla ID: 20

Fixes: 1b72605d2416 ("mem: balanced allocation of hugepages")
Cc: i.maximets@samsung.com
Cc: stable@dpdk.org

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

Notes:
    v3:
    - Fix potential memory leak if socket-mem was not specified
    
    v2:
    - Improve readability as per Ilya's comment

 lib/librte_eal/linuxapp/eal/eal_memory.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
  

Comments

Ilya Maximets Sept. 21, 2018, 11:02 a.m. UTC | #1
On 21.09.2018 12:27, Anatoly Burakov wrote:
> When NUMA-aware hugepages config option is set, we rely on
> libnuma to tell the kernel to allocate hugepages on a specific
> NUMA node. However, we allocate node mask before we check if
> NUMA is available in the first place, which, according to
> the manpage [1], causes undefined behaviour.
> 
> Fix by only using nodemask when we have NUMA available.
> 
> [1] https://linux.die.net/man/3/numa_alloc_onnode
> 
> Bugzilla ID: 20
> 
> Fixes: 1b72605d2416 ("mem: balanced allocation of hugepages")
> Cc: i.maximets@samsung.com
> Cc: stable@dpdk.org
> 
> Signed-off-by: Anatoly Burakov <anatoly.burakov@intel.com>
> ---
> 
> Notes:
>     v3:
>     - Fix potential memory leak if socket-mem was not specified
>     
>     v2:
>     - Improve readability as per Ilya's comment
> 
>  lib/librte_eal/linuxapp/eal/eal_memory.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)

LGTM,
Acked-by: Ilya Maximets <i.maximets@samsung.com>
  
Thomas Monjalon Oct. 3, 2018, 10:36 p.m. UTC | #2
21/09/2018 13:02, Ilya Maximets:
> On 21.09.2018 12:27, Anatoly Burakov wrote:
> > When NUMA-aware hugepages config option is set, we rely on
> > libnuma to tell the kernel to allocate hugepages on a specific
> > NUMA node. However, we allocate node mask before we check if
> > NUMA is available in the first place, which, according to
> > the manpage [1], causes undefined behaviour.
> > 
> > Fix by only using nodemask when we have NUMA available.
> > 
> > [1] https://linux.die.net/man/3/numa_alloc_onnode
> > 
> > Bugzilla ID: 20
> > 
> > Fixes: 1b72605d2416 ("mem: balanced allocation of hugepages")
> > Cc: i.maximets@samsung.com
> > Cc: stable@dpdk.org
> > 
> > Signed-off-by: Anatoly Burakov <anatoly.burakov@intel.com>
> > ---
> > 
> > Notes:
> >     v3:
> >     - Fix potential memory leak if socket-mem was not specified
> >     
> >     v2:
> >     - Improve readability as per Ilya's comment
> > 
> >  lib/librte_eal/linuxapp/eal/eal_memory.c | 6 ++++--
> >  1 file changed, 4 insertions(+), 2 deletions(-)
> 
> LGTM,
> Acked-by: Ilya Maximets <i.maximets@samsung.com>

Applied, thanks
  

Patch

diff --git a/lib/librte_eal/linuxapp/eal/eal_memory.c b/lib/librte_eal/linuxapp/eal/eal_memory.c
index dbf19499e..7747ee6df 100644
--- a/lib/librte_eal/linuxapp/eal/eal_memory.c
+++ b/lib/librte_eal/linuxapp/eal/eal_memory.c
@@ -263,7 +263,7 @@  map_all_hugepages(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi,
 	int node_id = -1;
 	int essential_prev = 0;
 	int oldpolicy;
-	struct bitmask *oldmask = numa_allocate_nodemask();
+	struct bitmask *oldmask = NULL;
 	bool have_numa = true;
 	unsigned long maxnode = 0;
 
@@ -275,6 +275,7 @@  map_all_hugepages(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi,
 
 	if (have_numa) {
 		RTE_LOG(DEBUG, EAL, "Trying to obtain current memory policy.\n");
+		oldmask = numa_allocate_nodemask();
 		if (get_mempolicy(&oldpolicy, oldmask->maskp,
 				  oldmask->size + 1, 0, 0) < 0) {
 			RTE_LOG(ERR, EAL,
@@ -402,7 +403,8 @@  map_all_hugepages(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi,
 			numa_set_localalloc();
 		}
 	}
-	numa_free_cpumask(oldmask);
+	if (oldmask != NULL)
+		numa_free_cpumask(oldmask);
 #endif
 	return i;
 }