Skip to contentSkip to conversation

selftests/mm: fix deadlock after pthread_create

PATCH Applied Edward Liaw 4 participants 7 messages Oct 3, 2024 – Oct 18, 2024

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover selftests/mm: fix deadlock after pthread_create

On Android arm, pthread_create followed by a fork caused a deadlock in the case where the fork required work to be completed by the created thread.

Updated the synchronization primitive to use pthread_barrier instead of atomic_bool.

Applied the same fix to the wp-fork-with-event test.

Edward Liaw (2):
selftests/mm: replace atomic_bool with pthread_barrier_t selftests/mm: fix deadlock for fork after pthread_create on ARM

 tools/testing/selftests/mm/uffd-common.c     |  5 +++--
 tools/testing/selftests/mm/uffd-common.h     |  3 +--
 tools/testing/selftests/mm/uffd-unit-tests.c | 21 ++++++++++++++------
 3 files changed, 19 insertions(+), 10 deletions(-)

--
2.46.1.824.gd892dcdcdd-goog

#1 lore raw

PATCH 1/2 selftests/mm: replace atomic_bool with pthread_barrier_t

Swaps synchronization primitive with pthread_barrier, so that stdatomic.h does not need to be included.

The synchronization is needed on Android ARM64; we see a deadlock with pthread_create when the parent thread races forward before the child has a chance to start doing work.

Fixes 8c864371b2a1 ("selftests/mm: fix ARM related issue with fork after

pthread_create")

---
 tools/testing/selftests/mm/uffd-common.c     |  5 +++--
 tools/testing/selftests/mm/uffd-common.h     |  3 +--
 tools/testing/selftests/mm/uffd-unit-tests.c | 14 ++++++++------
 3 files changed, 12 insertions(+), 10 deletions(-)
tools/testing/selftests/mm/uffd-common.c +3 -2
index 717539eddf98..852e7281026e 100644
@@ -18,7 +18,7 @@ bool test_uffdio_wp = true;
 unsigned long long *count_verify;
 uffd_test_ops_t *uffd_test_ops;
 uffd_test_case_ops_t *uffd_test_case_ops;
-atomic_bool ready_for_fork;
+pthread_barrier_t ready_for_fork;
 
 static int uffd_mem_fd_create(off_t mem_size, bool hugetlb)
 {
@@ -519,7 +519,8 @@ void *uffd_poll_thread(void *arg)
 	pollfd[1].fd = pipefd[cpu*2];
 	pollfd[1].events = POLLIN;
 
-	ready_for_fork = true;
+	/* Ready for parent thread to fork */
+	pthread_barrier_wait(&ready_for_fork);
 
 	for (;;) {
 		ret = poll(pollfd, 2, -1);
tools/testing/selftests/mm/uffd-common.h +1 -2
index a70ae10b5f62..3e6228d8e0dc 100644
@@ -33,7 +33,6 @@
 #include <inttypes.h>
 #include <stdint.h>
 #include <sys/random.h>
-#include <stdatomic.h>
 
 #include "../kselftest.h"
 #include "vm_util.h"
@@ -105,7 +104,7 @@ extern bool map_shared;
 extern bool test_uffdio_wp;
 extern unsigned long long *count_verify;
 extern volatile bool test_uffdio_copy_eexist;
-extern atomic_bool ready_for_fork;
+extern pthread_barrier_t ready_for_fork;
 
 extern uffd_test_ops_t anon_uffd_test_ops;
 extern uffd_test_ops_t shmem_uffd_test_ops;
tools/testing/selftests/mm/uffd-unit-tests.c +8 -6
index b3d21eed203d..3db2296ac631 100644
@@ -774,7 +774,7 @@ static void uffd_sigbus_test_common(bool wp)
 	char c;
 	struct uffd_args args = { 0 };
 
-	ready_for_fork = false;
+	pthread_barrier_init(&ready_for_fork, NULL, 2);
 
 	fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
 
@@ -791,8 +791,9 @@ static void uffd_sigbus_test_common(bool wp)
 	if (pthread_create(&uffd_mon, NULL, uffd_poll_thread, &args))
 		err("uffd_poll_thread create");
 
-	while (!ready_for_fork)
-		; /* Wait for the poll_thread to start executing before forking */
+	/* Wait for child thread to start before forking */
+	pthread_barrier_wait(&ready_for_fork);
+	pthread_barrier_destroy(&ready_for_fork);
 
 	pid = fork();
 	if (pid < 0)
@@ -833,7 +834,7 @@ static void uffd_events_test_common(bool wp)
 	char c;
 	struct uffd_args args = { 0 };
 
-	ready_for_fork = false;
+	pthread_barrier_init(&ready_for_fork, NULL, 2);
 
 	fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
 	if (uffd_register(uffd, area_dst, nr_pages * page_size,
@@ -844,8 +845,9 @@ static void uffd_events_test_common(bool wp)
 	if (pthread_create(&uffd_mon, NULL, uffd_poll_thread, &args))
 		err("uffd_poll_thread create");
 
-	while (!ready_for_fork)
-		; /* Wait for the poll_thread to start executing before forking */
+	/* Wait for child thread to start before forking */
+	pthread_barrier_wait(&ready_for_fork);
+	pthread_barrier_destroy(&ready_for_fork);
 
 	pid = fork();
 	if (pid < 0)
Signature · 1 line
2.46.1.824.gd892dcdcdd-goog
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] selftests/mm: replace atomic_bool with pthread_barrier_t

Quoted 10 lines · Edward Liaw

Swaps synchronization primitive with pthread_barrier, so that stdatomic.h does not need to be included.

The synchronization is needed on Android ARM64; we see a deadlock with pthread_create when the parent thread races forward before the child has a chance to start doing work.

Fixes 8c864371b2a1 ("selftests/mm: fix ARM related issue with fork after

pthread_create")

Quoted 96 lines
---
 tools/testing/selftests/mm/uffd-common.c     |  5 +++--
 tools/testing/selftests/mm/uffd-common.h     |  3 +--
 tools/testing/selftests/mm/uffd-unit-tests.c | 14 ++++++++------
 3 files changed, 12 insertions(+), 10 deletions(-)
tools/testing/selftests/mm/uffd-common.c +3 -2
index 717539eddf98..852e7281026e 100644
@@ -18,7 +18,7 @@ bool test_uffdio_wp = true;
 unsigned long long *count_verify;
 uffd_test_ops_t *uffd_test_ops;
 uffd_test_case_ops_t *uffd_test_case_ops;
-atomic_bool ready_for_fork;
+pthread_barrier_t ready_for_fork;
 
 static int uffd_mem_fd_create(off_t mem_size, bool hugetlb)
 {
@@ -519,7 +519,8 @@ void *uffd_poll_thread(void *arg)
 	pollfd[1].fd = pipefd[cpu*2];
 	pollfd[1].events = POLLIN;
 
-	ready_for_fork = true;
+	/* Ready for parent thread to fork */
+	pthread_barrier_wait(&ready_for_fork);
 
 	for (;;) {
 		ret = poll(pollfd, 2, -1);
tools/testing/selftests/mm/uffd-common.h +1 -2
index a70ae10b5f62..3e6228d8e0dc 100644
@@ -33,7 +33,6 @@
 #include <inttypes.h>
 #include <stdint.h>
 #include <sys/random.h>
-#include <stdatomic.h>
 
 #include "../kselftest.h"
 #include "vm_util.h"
@@ -105,7 +104,7 @@ extern bool map_shared;
 extern bool test_uffdio_wp;
 extern unsigned long long *count_verify;
 extern volatile bool test_uffdio_copy_eexist;
-extern atomic_bool ready_for_fork;
+extern pthread_barrier_t ready_for_fork;
 
 extern uffd_test_ops_t anon_uffd_test_ops;
 extern uffd_test_ops_t shmem_uffd_test_ops;
tools/testing/selftests/mm/uffd-unit-tests.c +8 -6
index b3d21eed203d..3db2296ac631 100644
@@ -774,7 +774,7 @@ static void uffd_sigbus_test_common(bool wp)
 	char c;
 	struct uffd_args args = { 0 };
 
-	ready_for_fork = false;
+	pthread_barrier_init(&ready_for_fork, NULL, 2);
 
 	fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
 
@@ -791,8 +791,9 @@ static void uffd_sigbus_test_common(bool wp)
 	if (pthread_create(&uffd_mon, NULL, uffd_poll_thread, &args))
 		err("uffd_poll_thread create");
 
-	while (!ready_for_fork)
-		; /* Wait for the poll_thread to start executing before forking */
+	/* Wait for child thread to start before forking */
+	pthread_barrier_wait(&ready_for_fork);
+	pthread_barrier_destroy(&ready_for_fork);
 
 	pid = fork();
 	if (pid < 0)
@@ -833,7 +834,7 @@ static void uffd_events_test_common(bool wp)
 	char c;
 	struct uffd_args args = { 0 };
 
-	ready_for_fork = false;
+	pthread_barrier_init(&ready_for_fork, NULL, 2);
 
 	fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
 	if (uffd_register(uffd, area_dst, nr_pages * page_size,
@@ -844,8 +845,9 @@ static void uffd_events_test_common(bool wp)
 	if (pthread_create(&uffd_mon, NULL, uffd_poll_thread, &args))
 		err("uffd_poll_thread create");
 
-	while (!ready_for_fork)
-		; /* Wait for the poll_thread to start executing before forking */
+	/* Wait for child thread to start before forking */
+	pthread_barrier_wait(&ready_for_fork);
+	pthread_barrier_destroy(&ready_for_fork);
 
 	pid = fork();
 	if (pid < 0)
Signature · 2 lines
BR,
Muhammad Usama Anjum
#3 lore raw

Re: [PATCH 1/2] selftests/mm: replace atomic_bool with pthread_barrier_t

In reply to Edward Liaw
Quoted 30 lines · Edward Liaw

Swaps synchronization primitive with pthread_barrier, so that stdatomic.h does not need to be included.

The synchronization is needed on Android ARM64; we see a deadlock with pthread_create when the parent thread races forward before the child has a chance to start doing work.

Fixes 8c864371b2a1 ("selftests/mm: fix ARM related issue with fork after

pthread_create")

---
 tools/testing/selftests/mm/uffd-common.c     |  5 +++--
 tools/testing/selftests/mm/uffd-common.h     |  3 +--
 tools/testing/selftests/mm/uffd-unit-tests.c | 14 ++++++++------
 3 files changed, 12 insertions(+), 10 deletions(-)
tools/testing/selftests/mm/uffd-common.c +1 -1
index 717539eddf98..852e7281026e 100644
@@ -18,7 +18,7 @@ bool test_uffdio_wp = true;
 unsigned long long *count_verify;
 uffd_test_ops_t *uffd_test_ops;
 uffd_test_case_ops_t *uffd_test_case_ops;
-atomic_bool ready_for_fork;
+pthread_barrier_t ready_for_fork;
 
 static int uffd_mem_fd_create(off_t mem_size, bool hugetlb)
 {

tools/testing/selftests/mm/uffd-common.c void *uffd_poll_thread(void *arg) · line 523

Quoted 6 lines
tools/testing/selftests/mm/uffd-common.c +2 -1
@@ -519,7 +519,8 @@ void *uffd_poll_thread(void *arg)
 	pollfd[1].fd = pipefd[cpu*2];
 	pollfd[1].events = POLLIN;
 
-	ready_for_fork = true;
+	/* Ready for parent thread to fork */
+	pthread_barrier_wait(&ready_for_fork);

Hi Edward,

This change is causing uffd-unit-tests to hang on arm64 when running the "move on anon" test. It's happening because this wait is never returning for this case. And that happens because ready_for_fork was never initialized for this test. It looks like there are other places where a thread is created for uffd_poll_thread() where ready_for_fork is not initialized too.

I added this change and it solves the problem, although it's pretty hacky.

This is blocking our arm64 testing on linux-next so would appreciate either a quick fix or removing the change until a fix is ready.

Thanks,
Ryan

----8<-----

tools/testing/selftests/mm/uffd-common.c +3 -1
index 852e7281026e..a05eb705be02 100644
@@ -19,6 +19,7 @@ unsigned long long *count_verify;
 uffd_test_ops_t *uffd_test_ops;
 uffd_test_case_ops_t *uffd_test_case_ops;
 pthread_barrier_t ready_for_fork;
+bool wait_ready_for_fork;
 
 static int uffd_mem_fd_create(off_t mem_size, bool hugetlb)
 {
@@ -520,7 +521,8 @@ void *uffd_poll_thread(void *arg)
        pollfd[1].events = POLLIN;
 
        /* Ready for parent thread to fork */
-       pthread_barrier_wait(&ready_for_fork);
+       if (wait_ready_for_fork)
+               pthread_barrier_wait(&ready_for_fork);
 
        for (;;) {
                ret = poll(pollfd, 2, -1);
tools/testing/selftests/mm/uffd-common.h +1 -0
index 3e6228d8e0dc..e94329a39b34 100644
@@ -105,6 +105,7 @@ extern bool test_uffdio_wp;
 extern unsigned long long *count_verify;
 extern volatile bool test_uffdio_copy_eexist;
 extern pthread_barrier_t ready_for_fork;
+extern bool wait_ready_for_fork;
 
 extern uffd_test_ops_t anon_uffd_test_ops;
 extern uffd_test_ops_t shmem_uffd_test_ops;
tools/testing/selftests/mm/uffd-unit-tests.c +4 -0
index 3db2296ac631..d1fc4cd6a948 100644
@@ -775,6 +775,7 @@ static void uffd_sigbus_test_common(bool wp)
        struct uffd_args args = { 0 };
 
        pthread_barrier_init(&ready_for_fork, NULL, 2);
+       wait_ready_for_fork = true;
 
        fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
 
@@ -794,6 +795,7 @@ static void uffd_sigbus_test_common(bool wp)
        /* Wait for child thread to start before forking */
        pthread_barrier_wait(&ready_for_fork);
        pthread_barrier_destroy(&ready_for_fork);
+       wait_ready_for_fork = false;
 
        pid = fork();
        if (pid < 0)
@@ -835,6 +837,7 @@ static void uffd_events_test_common(bool wp)
        struct uffd_args args = { 0 };
 
        pthread_barrier_init(&ready_for_fork, NULL, 2);
+       wait_ready_for_fork = true;
 
        fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
        if (uffd_register(uffd, area_dst, nr_pages * page_size,
@@ -848,6 +851,7 @@ static void uffd_events_test_common(bool wp)
        /* Wait for child thread to start before forking */
        pthread_barrier_wait(&ready_for_fork);
        pthread_barrier_destroy(&ready_for_fork);
+       wait_ready_for_fork = false;
 
        pid = fork();
        if (pid < 0)

----8<-----

Quoted 70 lines
tools/testing/selftests/mm/uffd-common.c +0 -0
 	for (;;) {
 		ret = poll(pollfd, 2, -1);
tools/testing/selftests/mm/uffd-common.h +1 -2
index a70ae10b5f62..3e6228d8e0dc 100644
@@ -33,7 +33,6 @@
 #include <inttypes.h>
 #include <stdint.h>
 #include <sys/random.h>
-#include <stdatomic.h>
 
 #include "../kselftest.h"
 #include "vm_util.h"
@@ -105,7 +104,7 @@ extern bool map_shared;
 extern bool test_uffdio_wp;
 extern unsigned long long *count_verify;
 extern volatile bool test_uffdio_copy_eexist;
-extern atomic_bool ready_for_fork;
+extern pthread_barrier_t ready_for_fork;
 
 extern uffd_test_ops_t anon_uffd_test_ops;
 extern uffd_test_ops_t shmem_uffd_test_ops;
tools/testing/selftests/mm/uffd-unit-tests.c +8 -6
index b3d21eed203d..3db2296ac631 100644
@@ -774,7 +774,7 @@ static void uffd_sigbus_test_common(bool wp)
 	char c;
 	struct uffd_args args = { 0 };
 
-	ready_for_fork = false;
+	pthread_barrier_init(&ready_for_fork, NULL, 2);
 
 	fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
 
@@ -791,8 +791,9 @@ static void uffd_sigbus_test_common(bool wp)
 	if (pthread_create(&uffd_mon, NULL, uffd_poll_thread, &args))
 		err("uffd_poll_thread create");
 
-	while (!ready_for_fork)
-		; /* Wait for the poll_thread to start executing before forking */
+	/* Wait for child thread to start before forking */
+	pthread_barrier_wait(&ready_for_fork);
+	pthread_barrier_destroy(&ready_for_fork);
 
 	pid = fork();
 	if (pid < 0)
@@ -833,7 +834,7 @@ static void uffd_events_test_common(bool wp)
 	char c;
 	struct uffd_args args = { 0 };
 
-	ready_for_fork = false;
+	pthread_barrier_init(&ready_for_fork, NULL, 2);
 
 	fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
 	if (uffd_register(uffd, area_dst, nr_pages * page_size,
@@ -844,8 +845,9 @@ static void uffd_events_test_common(bool wp)
 	if (pthread_create(&uffd_mon, NULL, uffd_poll_thread, &args))
 		err("uffd_poll_thread create");
 
-	while (!ready_for_fork)
-		; /* Wait for the poll_thread to start executing before forking */
+	/* Wait for child thread to start before forking */
+	pthread_barrier_wait(&ready_for_fork);
+	pthread_barrier_destroy(&ready_for_fork);
 
 	pid = fork();
 	if (pid < 0)
#4 lore raw

Re: [PATCH 1/2] selftests/mm: replace atomic_bool with pthread_barrier_t

Quoted 41 lines · Ryan Roberts
Quoted 30 lines · Edward Liaw

Swaps synchronization primitive with pthread_barrier, so that stdatomic.h does not need to be included.

The synchronization is needed on Android ARM64; we see a deadlock with pthread_create when the parent thread races forward before the child has a chance to start doing work.

Fixes 8c864371b2a1 ("selftests/mm: fix ARM related issue with fork after

pthread_create")

---
 tools/testing/selftests/mm/uffd-common.c     |  5 +++--
 tools/testing/selftests/mm/uffd-common.h     |  3 +--
 tools/testing/selftests/mm/uffd-unit-tests.c | 14 ++++++++------
 3 files changed, 12 insertions(+), 10 deletions(-)
tools/testing/selftests/mm/uffd-common.c +1 -1
index 717539eddf98..852e7281026e 100644
@@ -18,7 +18,7 @@ bool test_uffdio_wp = true;
 unsigned long long *count_verify;
 uffd_test_ops_t *uffd_test_ops;
 uffd_test_case_ops_t *uffd_test_case_ops;
-atomic_bool ready_for_fork;
+pthread_barrier_t ready_for_fork;
 
 static int uffd_mem_fd_create(off_t mem_size, bool hugetlb)
 {

tools/testing/selftests/mm/uffd-common.c void *uffd_poll_thread(void *arg) · line 523

Quoted 6 lines
tools/testing/selftests/mm/uffd-common.c +2 -1
@@ -519,7 +519,8 @@ void *uffd_poll_thread(void *arg)
      pollfd[1].fd = pipefd[cpu*2];
      pollfd[1].events = POLLIN;
 
-     ready_for_fork = true;
+     /* Ready for parent thread to fork */
+     pthread_barrier_wait(&ready_for_fork);

Hi Edward,

Quoted 5 lines

This change is causing uffd-unit-tests to hang on arm64 when running the "move on anon" test. It's happening because this wait is never returning for this case. And that happens because ready_for_fork was never initialized for this test. It looks like there are other places where a thread is created for uffd_poll_thread() where ready_for_fork is not initialized too.

I added this change and it solves the problem, although it's pretty hacky.

This is blocking our arm64 testing on linux-next so would appreciate either a quick fix or removing the change until a fix is ready.

Ah, I should not have changed it to a pthread_barrier, since that depends on the parent creating the barrier; I'm sending a revert and subsequent fix.

Quoted 149 lines

Thanks,
Ryan

----8<-----

tools/testing/selftests/mm/uffd-common.c +3 -1
index 852e7281026e..a05eb705be02 100644
@@ -19,6 +19,7 @@ unsigned long long *count_verify;
 uffd_test_ops_t *uffd_test_ops;
 uffd_test_case_ops_t *uffd_test_case_ops;
 pthread_barrier_t ready_for_fork;
+bool wait_ready_for_fork;
 
 static int uffd_mem_fd_create(off_t mem_size, bool hugetlb)
 {
@@ -520,7 +521,8 @@ void *uffd_poll_thread(void *arg)
        pollfd[1].events = POLLIN;
 
        /* Ready for parent thread to fork */
-       pthread_barrier_wait(&ready_for_fork);
+       if (wait_ready_for_fork)
+               pthread_barrier_wait(&ready_for_fork);
 
        for (;;) {
                ret = poll(pollfd, 2, -1);
tools/testing/selftests/mm/uffd-common.h +1 -0
index 3e6228d8e0dc..e94329a39b34 100644
@@ -105,6 +105,7 @@ extern bool test_uffdio_wp;
 extern unsigned long long *count_verify;
 extern volatile bool test_uffdio_copy_eexist;
 extern pthread_barrier_t ready_for_fork;
+extern bool wait_ready_for_fork;
 
 extern uffd_test_ops_t anon_uffd_test_ops;
 extern uffd_test_ops_t shmem_uffd_test_ops;
tools/testing/selftests/mm/uffd-unit-tests.c +4 -1
index 3db2296ac631..d1fc4cd6a948 100644
@@ -775,6 +775,7 @@ static void uffd_sigbus_test_common(bool wp)
        struct uffd_args args = { 0 };
 
        pthread_barrier_init(&ready_for_fork, NULL, 2);
+       wait_ready_for_fork = true;
 
        fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
 
@@ -794,6 +795,7 @@ static void uffd_sigbus_test_common(bool wp)
        /* Wait for child thread to start before forking */
        pthread_barrier_wait(&ready_for_fork);
        pthread_barrier_destroy(&ready_for_fork);
+       wait_ready_for_fork = false;
 
        pid = fork();
        if (pid < 0)
@@ -835,6 +837,7 @@ static void uffd_events_test_common(bool wp)
        struct uffd_args args = { 0 };
 
        pthread_barrier_init(&ready_for_fork, NULL, 2);
+       wait_ready_for_fork = true;
 
        fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
        if (uffd_register(uffd, area_dst, nr_pages * page_size,
@@ -848,6 +851,7 @@ static void uffd_events_test_common(bool wp)
        /* Wait for child thread to start before forking */
        pthread_barrier_wait(&ready_for_fork);
        pthread_barrier_destroy(&ready_for_fork);
+       wait_ready_for_fork = false;
 
        pid = fork();
        if (pid < 0)
----8<-----
Quoted 72 lines
tools/testing/selftests/mm/uffd-common.c +0 -0
      for (;;) {
              ret = poll(pollfd, 2, -1);
tools/testing/selftests/mm/uffd-common.h +1 -2
index a70ae10b5f62..3e6228d8e0dc 100644
@@ -33,7 +33,6 @@
 #include <inttypes.h>
 #include <stdint.h>
 #include <sys/random.h>
-#include <stdatomic.h>
 
 #include "../kselftest.h"
 #include "vm_util.h"
@@ -105,7 +104,7 @@ extern bool map_shared;
 extern bool test_uffdio_wp;
 extern unsigned long long *count_verify;
 extern volatile bool test_uffdio_copy_eexist;
-extern atomic_bool ready_for_fork;
+extern pthread_barrier_t ready_for_fork;
 
 extern uffd_test_ops_t anon_uffd_test_ops;
 extern uffd_test_ops_t shmem_uffd_test_ops;
tools/testing/selftests/mm/uffd-unit-tests.c +8 -6
index b3d21eed203d..3db2296ac631 100644
@@ -774,7 +774,7 @@ static void uffd_sigbus_test_common(bool wp)
      char c;
      struct uffd_args args = { 0 };
 
-     ready_for_fork = false;
+     pthread_barrier_init(&ready_for_fork, NULL, 2);
 
      fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
 
@@ -791,8 +791,9 @@ static void uffd_sigbus_test_common(bool wp)
      if (pthread_create(&uffd_mon, NULL, uffd_poll_thread, &args))
              err("uffd_poll_thread create");
 
-     while (!ready_for_fork)
-             ; /* Wait for the poll_thread to start executing before forking */
+     /* Wait for child thread to start before forking */
+     pthread_barrier_wait(&ready_for_fork);
+     pthread_barrier_destroy(&ready_for_fork);
 
      pid = fork();
      if (pid < 0)
@@ -833,7 +834,7 @@ static void uffd_events_test_common(bool wp)
      char c;
      struct uffd_args args = { 0 };
 
-     ready_for_fork = false;
+     pthread_barrier_init(&ready_for_fork, NULL, 2);
 
      fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
      if (uffd_register(uffd, area_dst, nr_pages * page_size,
@@ -844,8 +845,9 @@ static void uffd_events_test_common(bool wp)
      if (pthread_create(&uffd_mon, NULL, uffd_poll_thread, &args))
              err("uffd_poll_thread create");
 
-     while (!ready_for_fork)
-             ; /* Wait for the poll_thread to start executing before forking */
+     /* Wait for child thread to start before forking */
+     pthread_barrier_wait(&ready_for_fork);
+     pthread_barrier_destroy(&ready_for_fork);
 
      pid = fork();
      if (pid < 0)
#5 lore raw

PATCH 2/2 selftests/mm: fix deadlock for fork after pthread_create on ARM

In reply to Edward Liaw

On Android with arm, there is some synchronization needed to avoid a deadlock when forking after pthread_create.

Fixes cff294582798 ("selftests/mm: extend and rename uffd pagemap test")
---
 tools/testing/selftests/mm/uffd-unit-tests.c | 7 +++++++
 1 file changed, 7 insertions(+)
tools/testing/selftests/mm/uffd-unit-tests.c +7 -0
index 3db2296ac631..c8a3b1c7edff 100644
@@ -241,6 +241,9 @@ static void *fork_event_consumer(void *data)
 	fork_event_args *args = data;
 	struct uffd_msg msg = { 0 };
 
+	/* Ready for parent thread to fork */
+	pthread_barrier_wait(&ready_for_fork);
+
 	/* Read until a full msg received */
 	while (uffd_read_msg(args->parent_uffd, &msg));
 
@@ -308,8 +311,12 @@ static int pagemap_test_fork(int uffd, bool with_event, bool test_pin)
 
 	/* Prepare a thread to resolve EVENT_FORK */
 	if (with_event) {
+		pthread_barrier_init(&ready_for_fork, NULL, 2);
 		if (pthread_create(&thread, NULL, fork_event_consumer, &args))
 			err("pthread_create()");
+		/* Wait for child thread to start before forking */
+		pthread_barrier_wait(&ready_for_fork);
+		pthread_barrier_destroy(&ready_for_fork);
 	}
 
 	child = fork();
Signature · 1 line
2.46.1.824.gd892dcdcdd-goog
maintainer
#6 lore raw
In reply to Edward Liaw
Quoted 4 lines · On Thu, 3 Oct 2024 21:17:09 +0000 Edward Liaw <[email protected]> wrote:

On Android arm, pthread_create followed by a fork caused a deadlock in the case where the fork required work to be completed by the created thread.

Quoted 8 lines

Updated the synchronization primitive to use pthread_barrier instead of atomic_bool.

Applied the same fix to the wp-fork-with-event test.

Edward Liaw (2):
selftests/mm: replace atomic_bool with pthread_barrier_t selftests/mm: fix deadlock for fork after pthread_create on ARM

These fixes have different Fixes: targets, which might cause backporting issues - some kernels might end up with one patch and not the other. Was this intended? Is it OK?