Skip to contentSkip to conversation

linux-user: Fix locking order in fork_start()

PATCH Applied Peter Maydell 4 participants 4 messages Dec 4, 2017 – Jan 19, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user: Fix locking order in fork_start()

Our locking order is that the tb lock should be taken inside the mmap_lock, but fork_start() grabs locks the other way around. This means that if a heavily multithreaded guest process (such as Java) calls fork() it can deadlock, with the thread that called fork() stuck in fork_start() with the tb lock and waiting for the mmap lock, but some other thread in tb_find() with the mmap lock and waiting for the tb lock. The cpu_list_lock() should also always be taken last, not first.

Fix this by making fork_start() grab the locks in the right order. The order in which we drop locks doesn't matter, so we leave fork_end() the way it is.

---
 linux-user/main.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
linux-user/main.c +2 -2
index 6286661..146ee3e 100644
@@ -128,9 +128,9 @@ int cpu_get_pic_interrupt(CPUX86State *env)
 /* Make sure everything is in a consistent state for calling fork().  */
 void fork_start(void)
 {
-    cpu_list_lock();
-    qemu_mutex_lock(&tb_ctx.tb_lock);
     mmap_fork_start();
+    qemu_mutex_lock(&tb_ctx.tb_lock);
+    cpu_list_lock();
 }
 
 void fork_end(int child)
Signature · 1 line
2.7.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 27 lines · Peter Maydell

Our locking order is that the tb lock should be taken inside the mmap_lock, but fork_start() grabs locks the other way around. This means that if a heavily multithreaded guest process (such as Java) calls fork() it can deadlock, with the thread that called fork() stuck in fork_start() with the tb lock and waiting for the mmap lock, but some other thread in tb_find() with the mmap lock and waiting for the tb lock. The cpu_list_lock() should also always be taken last, not first.

Fix this by making fork_start() grab the locks in the right order. The order in which we drop locks doesn't matter, so we leave fork_end() the way it is.

---
 linux-user/main.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
linux-user/main.c +0 -0
index 6286661..146ee3e 100644
@@ -128,9 +128,9 @@ int cpu_get_pic_interrupt(CPUX86State *env)
 /* Make sure everything is in a consistent state for calling fork().  */

linux-user/main.c int cpu_get_pic_interrupt(CPUX86State *env) · line 136

Quoted 10 lines
linux-user/main.c +2 -2
 void fork_start(void)
 {
-    cpu_list_lock();
-    qemu_mutex_lock(&tb_ctx.tb_lock);
     mmap_fork_start();
+    qemu_mutex_lock(&tb_ctx.tb_lock);
+    cpu_list_lock();
 }
 
 void fork_end(int child)
Reviewed-by on v1 1/1
#2 lore raw
In reply to Peter Maydell
Quoted 26 lines · Peter Maydell

Our locking order is that the tb lock should be taken inside the mmap_lock, but fork_start() grabs locks the other way around. This means that if a heavily multithreaded guest process (such as Java) calls fork() it can deadlock, with the thread that called fork() stuck in fork_start() with the tb lock and waiting for the mmap lock, but some other thread in tb_find() with the mmap lock and waiting for the tb lock. The cpu_list_lock() should also always be taken last, not first.

Fix this by making fork_start() grab the locks in the right order. The order in which we drop locks doesn't matter, so we leave fork_end() the way it is.

---
 linux-user/main.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
linux-user/main.c +0 -0
index 6286661..146ee3e 100644
@@ -128,9 +128,9 @@ int cpu_get_pic_interrupt(CPUX86State *env)
 /* Make sure everything is in a consistent state for calling fork().  */

linux-user/main.c int cpu_get_pic_interrupt(CPUX86State *env) · line 136

Quoted 10 lines
linux-user/main.c +2 -2
 void fork_start(void)
 {
-    cpu_list_lock();
-    qemu_mutex_lock(&tb_ctx.tb_lock);
     mmap_fork_start();
+    qemu_mutex_lock(&tb_ctx.tb_lock);
+    cpu_list_lock();
 }
 
 void fork_end(int child)

--
Alex Bennée

#3 lore raw
In reply to Peter Maydell

Le 04/12/2017 à 15:22, Peter Maydell a écrit :

Quoted 27 lines

Our locking order is that the tb lock should be taken inside the mmap_lock, but fork_start() grabs locks the other way around. This means that if a heavily multithreaded guest process (such as Java) calls fork() it can deadlock, with the thread that called fork() stuck in fork_start() with the tb lock and waiting for the mmap lock, but some other thread in tb_find() with the mmap lock and waiting for the tb lock. The cpu_list_lock() should also always be taken last, not first.

Fix this by making fork_start() grab the locks in the right order. The order in which we drop locks doesn't matter, so we leave fork_end() the way it is.

---
 linux-user/main.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
linux-user/main.c +0 -0
index 6286661..146ee3e 100644
@@ -128,9 +128,9 @@ int cpu_get_pic_interrupt(CPUX86State *env)
 /* Make sure everything is in a consistent state for calling fork().  */

linux-user/main.c int cpu_get_pic_interrupt(CPUX86State *env) · line 136

Quoted 10 lines
linux-user/main.c +2 -2
 void fork_start(void)
 {
-    cpu_list_lock();
-    qemu_mutex_lock(&tb_ctx.tb_lock);
     mmap_fork_start();
+    qemu_mutex_lock(&tb_ctx.tb_lock);
+    cpu_list_lock();
 }
 
 void fork_end(int child)

Applied to my linux-user branch.

Thanks,
Laurent