Skip to contentSkip to conversation

accel/tcg: Fix concurrent pthread_create() and munmap()

PATCH In review Ilya Leoshkevich 3 participants 5 messages Oct 28, 2022

lore
Share & tools
lore
#0 lore raw

cover accel/tcg: Fix concurrent pthread_create() and munmap()

Hi,

This is a fix for the issue reported in [1]. Patch 1 is the fix itself, patch 2 is the test/reproducer.

By the way, I noticed that there is no code to free tb_jmp_cache, and object_finalize(CPUState) is never called - in case of system emulation this is fine, but what about linux-user?

[1] https://lists.gnu.org/archive/html/qemu-devel/2022-10/msg05181.html

Best regards,
Ilya

Ilya Leoshkevich (2):
accel/tcg: Fix concurrent pthread_create() and munmap() tests/tcg/multiarch: Add munmap-pthread.c

 accel/tcg/tb-maint.c                 |  5 ++
 tests/tcg/multiarch/Makefile.target  |  3 ++
 tests/tcg/multiarch/munmap-pthread.c | 71 ++++++++++++++++++++++++++++
 3 files changed, 79 insertions(+)
 create mode 100644 tests/tcg/multiarch/munmap-pthread.c
Signature · 1 line
2.37.2
#1 lore raw

PATCH 1/2 accel/tcg: Fix concurrent pthread_create() and munmap()

munmap() flushes jump cache on all CPUs if the unmapped range contains a translation block. This currently includes new CPUs (i.e. qemu-user threads), for which there is no jump cache yet, which leads to a null pointer dereference.

Fix by skipping new CPUs.

Fixes a976a99a2975 ("include/hw/core: Create struct CPUJumpCache")
---
 accel/tcg/tb-maint.c | 5 +++++
 1 file changed, 5 insertions(+)
accel/tcg/tb-maint.c +5 -0
index c8e921089df..2a063f91aa6 100644
@@ -241,6 +241,11 @@ static void tb_jmp_cache_inval_tb(TranslationBlock *tb)
         CPU_FOREACH(cpu) {
             CPUJumpCache *jc = cpu->tb_jmp_cache;
 
+            if (unlikely(!jc)) {
+                /* This is a new CPU that is not initialized yet. */
+                continue;
+            }
+
             if (qatomic_read(&jc->array[h].tb) == tb) {
                 qatomic_set(&jc->array[h].tb, NULL);
             }
Signature · 1 line
2.37.2
#2 lore raw
Quoted 18 lines · Ilya Leoshkevich

munmap() flushes jump cache on all CPUs if the unmapped range contains a translation block. This currently includes new CPUs (i.e. qemu-user threads), for which there is no jump cache yet, which leads to a null pointer dereference.

Fix by skipping new CPUs.

Fixes a976a99a2975 ("include/hw/core: Create struct CPUJumpCache")
---
  accel/tcg/tb-maint.c | 5 +++++
  1 file changed, 5 insertions(+)
accel/tcg/tb-maint.c +0 -0
index c8e921089df..2a063f91aa6 100644

accel/tcg/tb-maint.c static void tb_jmp_cache_inval_tb(TranslationBlock *tb) · line 248

Quoted 8 lines
accel/tcg/tb-maint.c +5 -0
@@ -241,6 +241,11 @@ static void tb_jmp_cache_inval_tb(TranslationBlock *tb)
          CPU_FOREACH(cpu) {
              CPUJumpCache *jc = cpu->tb_jmp_cache;
 
+            if (unlikely(!jc)) {
+                /* This is a new CPU that is not initialized yet. */
+                continue;
+            }
+

Interesting that the new cpu gets linked before realize. I wonder if cpu_list_add should be moved from the top of cpu_exec_realizefn to the end.

Thanks for the test case. I'll have a closer look.

r~

#3 lore raw

PATCH 2/2 tests/tcg/multiarch: Add munmap-pthread.c

In reply to Ilya Leoshkevich

Add a test to detect races between munmap() and creating new threads.

---
 tests/tcg/multiarch/Makefile.target  |  3 ++
 tests/tcg/multiarch/munmap-pthread.c | 71 ++++++++++++++++++++++++++++
 2 files changed, 74 insertions(+)
 create mode 100644 tests/tcg/multiarch/munmap-pthread.c
tests/tcg/multiarch/Makefile.target +3 -0
index 78104f9bbb7..5f0fee1aadb 100644
@@ -36,6 +36,9 @@ threadcount: LDFLAGS+=-lpthread
 
 signals: LDFLAGS+=-lrt -lpthread
 
+munmap-pthread: CFLAGS+=-pthread
+munmap-pthread: LDFLAGS+=-pthread
+
 # We define the runner for test-mmap after the individual
 # architectures have defined their supported pages sizes. If no
 # additional page sizes are defined we only run the default test.
tests/tcg/multiarch/munmap-pthread.c new file +71 -0

71 lines not on this page · raw message

Signature · 1 line
2.37.2
#4 lore raw
In reply to Ilya Leoshkevich
Quoted 8 lines · Ilya Leoshkevich

Hi,

This is a fix for the issue reported in [1]. Patch 1 is the fix itself, patch 2 is the test/reproducer.

By the way, I noticed that there is no code to free tb_jmp_cache, and object_finalize(CPUState) is never called - in case of system emulation this is fine, but what about linux-user?

We definitely have a memory leak here. The last attempt to properly finalize CPUState failed because we have duplicate pointers to the cpregs structures that end up in generated code. See:

https://lists.gnu.org/archive/html/qemu-devel/2022-08/msg02819.html

Quoted 15 lines

[1] https://lists.gnu.org/archive/html/qemu-devel/2022-10/msg05181.html

Best regards,
Ilya

Ilya Leoshkevich (2):
accel/tcg: Fix concurrent pthread_create() and munmap() tests/tcg/multiarch: Add munmap-pthread.c

 accel/tcg/tb-maint.c                 |  5 ++
 tests/tcg/multiarch/Makefile.target  |  3 ++
 tests/tcg/multiarch/munmap-pthread.c | 71 ++++++++++++++++++++++++++++
 3 files changed, 79 insertions(+)
 create mode 100644 tests/tcg/multiarch/munmap-pthread.c
Signature · 1 line
Alex Bennée