Le 23/03/2021 à 11:14, Maciej Zdeb a écrit :
Hi Christopher,

Bad news, patches didn't help. Attaching stacktraces, now it looks that spoe that executes lua scripts (free_thread_spue_lua.txt) tried to malloc twice. :(


It is most probably because of compiler optimizations. Some compiler barriers are necessary to avoid instructions reordering. It is the purpose of attached patches. Sorry to ask you it again, but could you make some tests ?

Thanks !
--
Christopher Faulet
>From b4f55500b514e5bfcdaba938cbd2b0ba3cfb2f62 Mon Sep 17 00:00:00 2001
From: Christopher Faulet <cfau...@haproxy.com>
Date: Tue, 23 Mar 2021 15:17:22 +0100
Subject: [PATCH] BUG/MEDIUM: hlua: Use compiler barrier to protect
 hlua_not_dumpable increments

In hlua_alloc() function, the hlua_not_dumpable variable is incremented
before any call to realloc() and decremented just after. To be sure it
really works, we must prevent any instruction reordering. Thus compiler
barriers have been added to do so.

The patch fixes a bug in the commit a61789a1d ("MEDIUM: lua: Use a
per-thread counter to track some non-reentrant parts of lua"). It must be
backported as far as 2.0.
---
 src/hlua.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/src/hlua.c b/src/hlua.c
index 5bef67a48b..9fc30336f1 100644
--- a/src/hlua.c
+++ b/src/hlua.c
@@ -8196,7 +8196,9 @@ static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
 		if (ptr)
 			zone->allocated -= osize;
 		hlua_not_dumpable++;
+		__ha_compiler_barrier();
 		free(ptr);
+		__ha_compiler_barrier();
 		hlua_not_dumpable--;
 		return NULL;
 	}
@@ -8207,7 +8209,9 @@ static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
 			return NULL;
 
 		hlua_not_dumpable++;
+		__ha_compiler_barrier();
 		ptr = malloc(nsize);
+		__ha_compiler_barrier();
 		hlua_not_dumpable--;
 		if (ptr)
 			zone->allocated += nsize;
@@ -8219,7 +8223,9 @@ static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
 		return NULL;
 
 	hlua_not_dumpable++;
+	__ha_compiler_barrier();
 	ptr = realloc(ptr, nsize);
+	__ha_compiler_barrier();
 	hlua_not_dumpable--;
 	if (ptr)
 		zone->allocated += nsize - osize;
-- 
2.30.2

>From 8eee5c4ba6eb92ceece0414b716c075c81d689ab Mon Sep 17 00:00:00 2001
From: Christopher Faulet <cfau...@haproxy.com>
Date: Tue, 23 Mar 2021 15:16:52 +0100
Subject: [PATCH] BUG/MEDIUM: hlua: Use compiler barrier to protect
 hlua_not_dumpable increments

In hlua_alloc() function, the hlua_not_dumpable variable is incremented
before any call to realloc() and decremented just after. To be sure it
really works, we must prevent any instruction reordering. Thus compiler
barriers have been added to do so.

The patch fixes a bug in the commit a61789a1d ("MEDIUM: lua: Use a
per-thread counter to track some non-reentrant parts of lua"). It must be
backported as far as 2.0.
---
 src/hlua.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/src/hlua.c b/src/hlua.c
index 990080b8c..ba7f93a99 100644
--- a/src/hlua.c
+++ b/src/hlua.c
@@ -8245,7 +8245,9 @@ static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
 		if (ptr)
 			zone->allocated -= osize;
 		hlua_not_dumpable++;
+		__ha_compiler_barrier();
 		free(ptr);
+		__ha_compiler_barrier();
 		hlua_not_dumpable--;
 		return NULL;
 	}
@@ -8256,7 +8258,9 @@ static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
 			return NULL;
 
 		hlua_not_dumpable++;
+		__ha_compiler_barrier();
 		ptr = malloc(nsize);
+		__ha_compiler_barrier();
 		hlua_not_dumpable--;
 		if (ptr)
 			zone->allocated += nsize;
@@ -8268,7 +8272,9 @@ static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
 		return NULL;
 
 	hlua_not_dumpable++;
+	__ha_compiler_barrier();
 	ptr = realloc(ptr, nsize);
+	__ha_compiler_barrier();
 	hlua_not_dumpable--;
 	if (ptr)
 		zone->allocated += nsize - osize;
-- 
2.30.2

>From 35c1b30ef2d725a37a82f0a293b2bd0bd7c473e2 Mon Sep 17 00:00:00 2001
From: Christopher Faulet <cfau...@haproxy.com>
Date: Tue, 23 Mar 2021 15:10:46 +0100
Subject: [PATCH] BUG/MEDIUM: hlua: Use compiler barrier to protect
 hlua_not_dumpable increments

In hlua_alloc() function, the hlua_not_dumpable variable is incremented
before any call to realloc() and decremented just after. To be sure it
really works, we must prevent any instruction reordering. Thus compiler
barriers have been added to do so.

The patch fixes a bug in the commit a61789a1d ("MEDIUM: lua: Use a
per-thread counter to track some non-reentrant parts of lua"). It must be
backported as far as 2.0.
---
 src/hlua.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/src/hlua.c b/src/hlua.c
index 962195a60..d47973dcf 100644
--- a/src/hlua.c
+++ b/src/hlua.c
@@ -8639,7 +8639,9 @@ static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
 	 */
 	if (likely(~zone->limit == 0)) {
 		hlua_not_dumpable++;
+		__ha_compiler_barrier();
 		ptr = realloc(ptr, nsize);
+		__ha_compiler_barrier();
 		hlua_not_dumpable--;
 		return ptr;
 	}
@@ -8657,7 +8659,9 @@ static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
 	} while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new));
 
 	hlua_not_dumpable++;
+	__ha_compiler_barrier();
 	ptr = realloc(ptr, nsize);
+	__ha_compiler_barrier();
 	hlua_not_dumpable--;
 
 	if (unlikely(!ptr && nsize)) // failed
-- 
2.30.2

Reply via email to