7b7091
From 93867c59b0bb29470873a427dc7f06ebaf305221 Mon Sep 17 00:00:00 2001
7b7091
From: Pingfan Liu <piliu@redhat.com>
7b7091
Date: Mon, 29 Mar 2021 17:33:06 +0800
7b7091
Subject: [PATCH] libnuma: make numa_police_memory() free of race
7b7091
7b7091
When numa_police_memory() read-write to memory, it can risk the race of
7b7091
another thread write to the same area.
7b7091
7b7091
Using atomic function to protect the read-write operation
7b7091
7b7091
Signed-off-by: Pingfan Liu <piliu@redhat.com>
7b7091
---
7b7091
 libnuma.c | 6 ++++--
7b7091
 1 file changed, 4 insertions(+), 2 deletions(-)
7b7091
7b7091
diff --git a/libnuma.c b/libnuma.c
7b7091
index f073c50..7b8290c 100644
7b7091
--- a/libnuma.c
7b7091
+++ b/libnuma.c
7b7091
@@ -864,8 +864,10 @@ void numa_police_memory(void *mem, size_t size)
7b7091
 {
7b7091
 	int pagesize = numa_pagesize_int();
7b7091
 	unsigned long i;
7b7091
-	for (i = 0; i < size; i += pagesize)
7b7091
-        ((volatile char*)mem)[i] = ((volatile char*)mem)[i];
7b7091
+	char *p = mem;
7b7091
+	for (i = 0; i < size; i += pagesize, p += pagesize)
7b7091
+		__atomic_and_fetch(p, 0xff, __ATOMIC_RELAXED);
7b7091
+
7b7091
 }
7b7091
 
7b7091
 make_internal_alias(numa_police_memory);
7b7091
-- 
7b7091
2.29.2
7b7091