From nobody Thu Nov 28 05:33:40 2024 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 805801A0708 for ; Thu, 3 Oct 2024 21:17:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727990253; cv=none; b=SpSUpxM/pIzIQMWPKXqP6h7+X3cajYMM+tyG4peiihqLRQr6KqSXTJHgg5LBcfSyabHiWQKakyOSjtY+HdOuueJA0fpfFLErGIJQroj62RjOhwf+Idb9HA47Uf38WOJRlIMtfIYKgY8qCAW1aDw+hO7WGhv/9WgVEWBmwSQ6OqA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727990253; c=relaxed/simple; bh=EcrxJmJkEY89MEO9l4NQd+g2m2f94zkmGXJcRfAXWCk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=OujFC7zBtHb8RbymYTmEJ4xRPJmec5fVyMcj++5P8y86ZC5HBYaqx+xyQ0BBgQaEOFBRa1kmqZrK4XmohkQjuaV4KI9zducP/QmLOcttCYrD6ZXiddmBskk5FWCC0D0UlxidJNKooGJLDgP4bxxOy2Z7dkOMR5lLvvPkjqDMD14= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--edliaw.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=QAiuja3w; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--edliaw.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="QAiuja3w" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e28694ff555so2485974276.2 for ; Thu, 03 Oct 2024 14:17:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727990249; x=1728595049; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=KkHqwn8Bqw7l/s5v7I5SXTM3ejHc+yrhyoRwQ1tPH9Q=; b=QAiuja3wtjYlcEuCj9YT6CRp/oOsXhxexGf0y9q3l1OfOB4UzHydXoF6Dl1soBBLw7 1RviT5LUm9N87EXwPltJW9dtALXfTmxudhj2pSNUKGyCf0eS2SQP/H22NDx3aMjo4l2T LbPtD/GgnH0PoagejBelcc510ouPmauyawgR2mYD2KXM8TEpJ2SSH7lUCEex1GU0n6l8 RzgJNYIdjpJbVlBZcSdlTh6nmh8Q38Pu39Rk1d51/28UFw1xn2DOgNdeNtzdyuiD6Xwy qrcnW4DyaLjU8B0u2NBKQP8wsfR2jyPeGqlNcigNrdgoxXWQQ6dia6MtImDJulrUGeGx XtYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727990249; x=1728595049; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=KkHqwn8Bqw7l/s5v7I5SXTM3ejHc+yrhyoRwQ1tPH9Q=; b=M4QrwvgyohYrMSTuc6qK4iwPqBuzArM6daWUm9hRsZZ+YQg8SMcjwYAQb/1KddHQuJ PkTVAcYdZaDx38WHSZ2xGcmc/9ObF2/+0savfo8LiUTvN+oFLfubPWwZbTaKAOA3plkV xG+EFuhz/tgojOEFKXrh4jN0WEvPv3Sm3+XIpSLQpdikwnoFxv41IcfhdUW/huUzsMoh ItdPfwzHt8vYy1GmT/47fdi24M0a/jJ/rc20Z5misGmxRrsg3MIpcl6N1t5uin/lVdJl AQ2UiFQrFcF9CJlfNJZTHTlTICFZZOjFecAKsu5fqiJjwd+1rF8KsQliHAQmg72R2iNF KOPg== X-Gm-Message-State: AOJu0YxNhUW0YX7C18OKpxkCBG4OFoxCS7XHo4v3lBDTeQXd9RZecyBo U0vZwdbCqQQA8J9V3NQFNKv7mdWkmxSC9iDRYjgvYQFblTkkS3IN06tvKgZ/9HEF/FUA90vs/aH Y9A== X-Google-Smtp-Source: AGHT+IGgqrbal18cXmCsiQESftRs1395vmYgGSaXxFs9pt/ua0r8DPUXmkAbrXpoVABneX6qbZ8gjH/Q2zw= X-Received: from edliaw.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:305d]) (user=edliaw job=sendgmr) by 2002:a25:684d:0:b0:e24:a06b:1aed with SMTP id 3f1490d57ef6-e28936d26b8mr255276.3.1727990249428; Thu, 03 Oct 2024 14:17:29 -0700 (PDT) Date: Thu, 3 Oct 2024 21:17:10 +0000 In-Reply-To: <20241003211716.371786-1-edliaw@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241003211716.371786-1-edliaw@google.com> X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241003211716.371786-2-edliaw@google.com> Subject: [PATCH 1/2] selftests/mm: replace atomic_bool with pthread_barrier_t From: Edward Liaw To: linux-kselftest@vger.kernel.org, Andrew Morton , Shuah Khan , Lokesh Gidra , Edward Liaw , Peter Xu Cc: linux-kernel@vger.kernel.org, kernel-team@android.com, linux-mm@kvack.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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") Signed-off-by: Edward Liaw Reviewed-by: Muhammad Usama Anjum --- 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(-) diff --git a/tools/testing/selftests/mm/uffd-common.c b/tools/testing/selft= ests/mm/uffd-common.c index 717539eddf98..852e7281026e 100644 --- a/tools/testing/selftests/mm/uffd-common.c +++ b/tools/testing/selftests/mm/uffd-common.c @@ -18,7 +18,7 @@ bool test_uffdio_wp =3D 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; =20 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 =3D pipefd[cpu*2]; pollfd[1].events =3D POLLIN; =20 - ready_for_fork =3D true; + /* Ready for parent thread to fork */ + pthread_barrier_wait(&ready_for_fork); =20 for (;;) { ret =3D poll(pollfd, 2, -1); diff --git a/tools/testing/selftests/mm/uffd-common.h b/tools/testing/selft= ests/mm/uffd-common.h index a70ae10b5f62..3e6228d8e0dc 100644 --- a/tools/testing/selftests/mm/uffd-common.h +++ b/tools/testing/selftests/mm/uffd-common.h @@ -33,7 +33,6 @@ #include #include #include -#include =20 #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; =20 extern uffd_test_ops_t anon_uffd_test_ops; extern uffd_test_ops_t shmem_uffd_test_ops; diff --git a/tools/testing/selftests/mm/uffd-unit-tests.c b/tools/testing/s= elftests/mm/uffd-unit-tests.c index b3d21eed203d..3db2296ac631 100644 --- a/tools/testing/selftests/mm/uffd-unit-tests.c +++ b/tools/testing/selftests/mm/uffd-unit-tests.c @@ -774,7 +774,7 @@ static void uffd_sigbus_test_common(bool wp) char c; struct uffd_args args =3D { 0 }; =20 - ready_for_fork =3D false; + pthread_barrier_init(&ready_for_fork, NULL, 2); =20 fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK); =20 @@ -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"); =20 - 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); =20 pid =3D fork(); if (pid < 0) @@ -833,7 +834,7 @@ static void uffd_events_test_common(bool wp) char c; struct uffd_args args =3D { 0 }; =20 - ready_for_fork =3D false; + pthread_barrier_init(&ready_for_fork, NULL, 2); =20 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"); =20 - 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); =20 pid =3D fork(); if (pid < 0) --=20 2.46.1.824.gd892dcdcdd-goog From nobody Thu Nov 28 05:33:40 2024 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5CBEC1ABEA7 for ; Thu, 3 Oct 2024 21:17:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727990255; cv=none; b=SPL5H61Yq8LVVgQQl9fQkIR9UmL64KC4Kj6FpCl/AAQD9A+/yv93pDXgJVhPNiGA8WJXJ73EBzIpq6ubIICNxmyDcMRLe5IzluU31atSe5rHjfE6kNSCrGeqhGNQ6cJUMO5TQdyvRpsRYadOKDqxQHmIeTJOK1asCpD7Lk19178= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727990255; c=relaxed/simple; bh=IROd3VJkoeniPwTUOiMyty21qBg2Z2tb1n98wFzPBTk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mrFRgq2jPsFFGEoIAurDdIeh/RtAEqr0ruZ59+mC7GDIQOzk9F8XJErVYWLP7SdGBWgtXw0aJ9fd3RAgXQizgglFmzeCzUzGWqvzep9OJX+HgCWMGZhM/8E0Lq1HT6bxh9b4j+q1ivVQcevXub/rkfcMxL4iqv6jAP7jqDoFszk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--edliaw.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=hnbSWH7Y; arc=none smtp.client-ip=209.85.210.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--edliaw.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="hnbSWH7Y" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-71dd55880d3so2381584b3a.0 for ; Thu, 03 Oct 2024 14:17:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727990253; x=1728595053; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=BVeNglwTN7akCXDmRlldGSc/rW6sZdc88WuvUouG7/c=; b=hnbSWH7YlL3X/wYMAgb3TFfF40v7w+qxHnxhifvKLBdUkXB95ZyTucmd6dzSXl1mCk EHO8h7cVX1eBHSUJHgqHFpZxTwWWmsKCLHssen6/n1stpajbl8kL/XOETYBXHzRlVViJ ct6GGlcO6DGUU2xVIpH/H3n/Qp0pM74U79GAuAXeFY48WNkBAC6kkZhg4dRSDvygLAqG DEjp8cg5yYrEsM2i+z8YTCxjjvRPoq/qg7gZessSu4lygobIvFKVhZ0LqLFlSnxJkeBH N6bSQS8daJppIdYazpO91jt72zDLvD+f8Or43dxu65na0PmcXF2Vqbp3pI5ihd7AdIIX f+Bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727990253; x=1728595053; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=BVeNglwTN7akCXDmRlldGSc/rW6sZdc88WuvUouG7/c=; b=BP4a3dvZ883vywzpaOM/X01C4ppqlmrzop13W9OrTYU0VVzPs8P/0Mo9nHWvbUszpP IG47CkoFR5cwF4DrsudTzTCKSbKipwWVpgZUam5IKLpEQ8pgbsnYoBM0GXgM22WyEgVX 5NEoEFo52Vr1lkDCcEzeLKpogqR+a3y5j9jq9g5zh1hlFy0nA1JQDHoN6mq7IiH7s0ga IEsKWgLJPMKEgIXSTx7hbqHuXZwIu/B4VrnNqVKd0GLVx6TS5vhUgH0fIV+Kh3adwapN p4ErDFnFnTiCWvxtaPGeL+q8i9hZgN53AHHJ7N5GeE+lHVuAeblnaIZmGUWTe0vAjR9y HJcA== X-Gm-Message-State: AOJu0YzoBGVfuOE9MWCRJFot996+1Hvkt2k1DVfvWZsvbexJjl102+aF Fg9DltumdpnhGhmRPr5nTmwGrap9/g0OSed5sBhwJzYVWnDKHicu7DVq+7BFFghHK29Fbp/JquU +jg== X-Google-Smtp-Source: AGHT+IF1wAIWxriS35+1+9s9R25YDFII/xuSGjwNB1DYXJzZFTeclXhMlYEUs67ru5vh0bxduTAAmrtUyuM= X-Received: from edliaw.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:305d]) (user=edliaw job=sendgmr) by 2002:a05:6a00:858b:b0:717:8e0f:3e5c with SMTP id d2e1a72fcca58-71de24930e7mr686b3a.5.1727990253229; Thu, 03 Oct 2024 14:17:33 -0700 (PDT) Date: Thu, 3 Oct 2024 21:17:11 +0000 In-Reply-To: <20241003211716.371786-1-edliaw@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241003211716.371786-1-edliaw@google.com> X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241003211716.371786-3-edliaw@google.com> Subject: [PATCH 2/2] selftests/mm: fix deadlock for fork after pthread_create on ARM From: Edward Liaw To: linux-kselftest@vger.kernel.org, Andrew Morton , Shuah Khan , Lokesh Gidra , Edward Liaw , Peter Xu Cc: linux-kernel@vger.kernel.org, kernel-team@android.com, linux-mm@kvack.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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") Signed-off-by: Edward Liaw --- tools/testing/selftests/mm/uffd-unit-tests.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tools/testing/selftests/mm/uffd-unit-tests.c b/tools/testing/s= elftests/mm/uffd-unit-tests.c index 3db2296ac631..c8a3b1c7edff 100644 --- a/tools/testing/selftests/mm/uffd-unit-tests.c +++ b/tools/testing/selftests/mm/uffd-unit-tests.c @@ -241,6 +241,9 @@ static void *fork_event_consumer(void *data) fork_event_args *args =3D data; struct uffd_msg msg =3D { 0 }; =20 + /* 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)); =20 @@ -308,8 +311,12 @@ static int pagemap_test_fork(int uffd, bool with_event= , bool test_pin) =20 /* 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); } =20 child =3D fork(); --=20 2.46.1.824.gd892dcdcdd-goog