From nobody Sat Dec 28 03:34:46 2024 Received: from mail.codeweavers.com (mail.codeweavers.com [4.36.192.163]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DB10314E2CC; Mon, 9 Dec 2024 18:59:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=4.36.192.163 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733770776; cv=none; b=Tn5OqR9dYXMnektpu6g9sU//R1RT/2mEpQLMSVQIEbg5WbF2tmA/xhEnneN7Gl2ESNpu562TJfJfY8lH9EbyJxJrRCLLQIrVxv8GnZGdRVGIi6nDH4BUdfMo88Qyo2QQgrXVZR6tZthr9N18VYqZWhDpE7Xhf2L2cPbm92DW+EU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733770776; c=relaxed/simple; bh=CAgcnAWVndXCc5CdhXm4Yv0C2toEM5VQmYN5+fMdcjo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VQVVN1j16rF65fnzxMIOrXJtHyA6UQ96+U8xJMntpKrN6GT8EhVFVyJ8VOkrdbMIhpHX5NMF7GRTLxmmlmM0S/ULXXj9Lsc85oHj3qjjpbgJK7KiBEMSnoTLjcj/3CdPSzIlCHY0QBYrD+Vs2ebuGQwL3f0Bx05WjQQiNSeMV7c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=codeweavers.com; spf=pass smtp.mailfrom=codeweavers.com; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b=NntiBX5l; arc=none smtp.client-ip=4.36.192.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=codeweavers.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=codeweavers.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="NntiBX5l" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=codeweavers.com; s=s1; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=zwZaONPTRv1TAeaJhWHoQ0+5xaoskz2R0vBPXkeiQgY=; b=NntiBX5lgSFwK/t9vKf9Xfmn2i AlhZ+bg49mbeOqB5/GSlv43olfzgrulic2EJswn+ExbboGgs7z9DnNvd0ARUoIwrgkPP7ZZydUY9N 5ZwYyWTnQJHmpWIYbZD2jhn6lclTeAi2ahe+t5euotUfrYTymiDz/Kv+aPOBSx7eD+0YdAJj5bNzs NFrdfHDzo4h8oOmn5jzgKrhF8eWmCAsZZHKrDroebGV4vB5898YMLJh/eTmkTMGdggXOYY9H0m38+ 6xkSUxTvyjuUe2CXOMPBZXfffyApu4j4ZnAEhHesfuZWoI6Ykg5mJJFq9cGGxcEl4IhFlIsOeIOyS UifLSzDw==; Received: from cw137ip160.mn.codeweavers.com ([10.69.137.160] helo=camazotz.mn.codeweavers.com) by mail.codeweavers.com with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tKiye-001Gd5-0N; Mon, 09 Dec 2024 12:59:28 -0600 From: Elizabeth Figura To: Arnd Bergmann , Greg Kroah-Hartman , Jonathan Corbet , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, wine-devel@winehq.org, =?UTF-8?q?Andr=C3=A9=20Almeida?= , Wolfram Sang , Arkadiusz Hiler , Peter Zijlstra , Andy Lutomirski , linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, Randy Dunlap , Ingo Molnar , Will Deacon , Waiman Long , Boqun Feng , Elizabeth Figura Subject: [PATCH v6 18/28] selftests: ntsync: Add some tests for wakeup signaling with WINESYNC_IOC_WAIT_ANY. Date: Mon, 9 Dec 2024 12:58:54 -0600 Message-ID: <20241209185904.507350-19-zfigura@codeweavers.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241209185904.507350-1-zfigura@codeweavers.com> References: <20241209185904.507350-1-zfigura@codeweavers.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Test contended "wait-for-any" waits, to make sure that scheduling and wakeup logic works correctly. Signed-off-by: Elizabeth Figura --- .../testing/selftests/drivers/ntsync/ntsync.c | 150 ++++++++++++++++++ 1 file changed, 150 insertions(+) diff --git a/tools/testing/selftests/drivers/ntsync/ntsync.c b/tools/testin= g/selftests/drivers/ntsync/ntsync.c index c0f372167557..993f5db23768 100644 --- a/tools/testing/selftests/drivers/ntsync/ntsync.c +++ b/tools/testing/selftests/drivers/ntsync/ntsync.c @@ -556,4 +556,154 @@ TEST(test_wait_all) close(fd); } =20 +struct wake_args { + int fd; + int obj; +}; + +struct wait_args { + int fd; + unsigned long request; + struct ntsync_wait_args *args; + int ret; + int err; +}; + +static void *wait_thread(void *arg) +{ + struct wait_args *args =3D arg; + + args->ret =3D ioctl(args->fd, args->request, args->args); + args->err =3D errno; + return NULL; +} + +static __u64 get_abs_timeout(unsigned int ms) +{ + struct timespec timeout; + clock_gettime(CLOCK_MONOTONIC, &timeout); + return (timeout.tv_sec * 1000000000) + timeout.tv_nsec + (ms * 1000000); +} + +static int wait_for_thread(pthread_t thread, unsigned int ms) +{ + struct timespec timeout; + + clock_gettime(CLOCK_REALTIME, &timeout); + timeout.tv_nsec +=3D ms * 1000000; + timeout.tv_sec +=3D (timeout.tv_nsec / 1000000000); + timeout.tv_nsec %=3D 1000000000; + return pthread_timedjoin_np(thread, NULL, &timeout); +} + +TEST(wake_any) +{ + struct ntsync_mutex_args mutex_args =3D {0}; + struct ntsync_wait_args wait_args =3D {0}; + struct ntsync_sem_args sem_args =3D {0}; + struct wait_args thread_args; + int objs[2], fd, ret; + __u32 count, index; + pthread_t thread; + + fd =3D open("/dev/ntsync", O_CLOEXEC | O_RDONLY); + ASSERT_LE(0, fd); + + sem_args.count =3D 0; + sem_args.max =3D 3; + sem_args.sem =3D 0xdeadbeef; + ret =3D ioctl(fd, NTSYNC_IOC_CREATE_SEM, &sem_args); + EXPECT_EQ(0, ret); + EXPECT_NE(0xdeadbeef, sem_args.sem); + + mutex_args.owner =3D 123; + mutex_args.count =3D 1; + mutex_args.mutex =3D 0xdeadbeef; + ret =3D ioctl(fd, NTSYNC_IOC_CREATE_MUTEX, &mutex_args); + EXPECT_EQ(0, ret); + EXPECT_NE(0xdeadbeef, mutex_args.mutex); + + objs[0] =3D sem_args.sem; + objs[1] =3D mutex_args.mutex; + + /* test waking the semaphore */ + + wait_args.timeout =3D get_abs_timeout(1000); + wait_args.objs =3D (uintptr_t)objs; + wait_args.count =3D 2; + wait_args.owner =3D 456; + wait_args.index =3D 0xdeadbeef; + thread_args.fd =3D fd; + thread_args.args =3D &wait_args; + thread_args.request =3D NTSYNC_IOC_WAIT_ANY; + ret =3D pthread_create(&thread, NULL, wait_thread, &thread_args); + EXPECT_EQ(0, ret); + + ret =3D wait_for_thread(thread, 100); + EXPECT_EQ(ETIMEDOUT, ret); + + count =3D 1; + ret =3D post_sem(sem_args.sem, &count); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, count); + check_sem_state(sem_args.sem, 0, 3); + + ret =3D wait_for_thread(thread, 100); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, thread_args.ret); + EXPECT_EQ(0, wait_args.index); + + /* test waking the mutex */ + + /* first grab it again for owner 123 */ + ret =3D wait_any(fd, 1, &mutex_args.mutex, 123, &index); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, index); + + wait_args.timeout =3D get_abs_timeout(1000); + wait_args.owner =3D 456; + ret =3D pthread_create(&thread, NULL, wait_thread, &thread_args); + EXPECT_EQ(0, ret); + + ret =3D wait_for_thread(thread, 100); + EXPECT_EQ(ETIMEDOUT, ret); + + ret =3D unlock_mutex(mutex_args.mutex, 123, &count); + EXPECT_EQ(0, ret); + EXPECT_EQ(2, count); + + ret =3D pthread_tryjoin_np(thread, NULL); + EXPECT_EQ(EBUSY, ret); + + ret =3D unlock_mutex(mutex_args.mutex, 123, &count); + EXPECT_EQ(0, ret); + EXPECT_EQ(1, mutex_args.count); + check_mutex_state(mutex_args.mutex, 1, 456); + + ret =3D wait_for_thread(thread, 100); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, thread_args.ret); + EXPECT_EQ(1, wait_args.index); + + /* delete an object while it's being waited on */ + + wait_args.timeout =3D get_abs_timeout(200); + wait_args.owner =3D 123; + ret =3D pthread_create(&thread, NULL, wait_thread, &thread_args); + EXPECT_EQ(0, ret); + + ret =3D wait_for_thread(thread, 100); + EXPECT_EQ(ETIMEDOUT, ret); + + close(sem_args.sem); + close(mutex_args.mutex); + + ret =3D wait_for_thread(thread, 200); + EXPECT_EQ(0, ret); + EXPECT_EQ(-1, thread_args.ret); + EXPECT_EQ(ETIMEDOUT, thread_args.err); + + close(fd); +} + TEST_HARNESS_MAIN --=20 2.45.2