From patchwork Wed Jan 31 02:13:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 194443 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2087:b0:106:209c:c626 with SMTP id gs7csp1626446dyb; Tue, 30 Jan 2024 18:20:54 -0800 (PST) X-Google-Smtp-Source: AGHT+IGAiWasXD0VfSoDRRtQwaaOZZYJymMwEaBh8iP0ywyMnjuZtHA0TEAgotWPGTuftIuieJvf X-Received: by 2002:a05:6358:5d87:b0:175:f9b8:89bf with SMTP id s7-20020a0563585d8700b00175f9b889bfmr131785rwm.22.1706667654052; Tue, 30 Jan 2024 18:20:54 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706667654; cv=pass; d=google.com; s=arc-20160816; b=rQwslc5BFZG8Vt0pTTI/rYRmKgmyawUSrHhGlwF8V1rogm8qYMD0hW8VfqPoc7W8AC P5Ap8EgXJ133ZtW0hLI6xYUjvkUxoISTWITeaJB2pV+6DxoUKLqwLfSURw/6lTRYKsQp rAXUdizYfmjVfk7Qp+zAas7dQWrg2+SygZH7Jp341wP6PYcO6mlZu7nfnuZfe2EFrjR8 LXP/rYP0dmXDYxSbsUKGaWI3ryW7Cgi1Y9Z6ml5FPozhp8NeAphzGqZGNgINEvGfbRwF wxAdv7xiTDvKjto9qmFZs2vJ7HiHWDdGyExruQfGiQ3gdBOx+Cw6v5lBWFQ9sGWx+XWf vSGQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=hP8Bz/7TLAf/d75xqJERzPNlxl/Lxor2EgGxfs602cE=; fh=/YqlKDjwZSggOLNtRB2WGBfyy1fjDC4aThAnNi3jbZE=; b=I6AZtLp0/kMyG8R0SWKdmOcVhGNiqW3bbcYK+nVXSeUjh4mCG4VCPyVf5Xghv21bws i4JICcNrWRxD6F2uSPGxPSmXoBz16J8V+4FfHGnJt00rW0kefwEz+PaciCEj+a8v4ofn 8UqVgL/7p4DMclKJzcQY39wNqeNE7UccHjm0Er4a3pBGU4zZEKVZ+4jLQeL30aSf87WH tNxW6I1ACRYuO+K8ZLBbIyWMVmUW4008yTs0AEqPSfoAN34qDoOTxLe82HMcnzndROU2 aCBG0SLDuUZoWB7Rtxdg8U3msOUV7xFn9AmO0Y6iq+vqjKrf2wxd0Ato1p06rTsG2/Hy tAVA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=k8hElhhe; arc=pass (i=1 spf=pass spfdomain=codeweavers.com dkim=pass dkdomain=codeweavers.com dmarc=pass fromdomain=codeweavers.com); spf=pass (google.com: domain of linux-kernel+bounces-45643-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-45643-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=codeweavers.com X-Forwarded-Encrypted: i=1; AJvYcCXu+puYqba9rETrZ4VQbnJBRuXp1bKaD90Zzl9pxXjAT1a6/bMdQrfYvXdsAHA2IPzdcWDLQPGH5PgFyQk9BAI6Z8qU+w== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id a24-20020a63e418000000b005d8c1b5a3bdsi5544703pgi.216.2024.01.30.18.20.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jan 2024 18:20:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-45643-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=k8hElhhe; arc=pass (i=1 spf=pass spfdomain=codeweavers.com dkim=pass dkdomain=codeweavers.com dmarc=pass fromdomain=codeweavers.com); spf=pass (google.com: domain of linux-kernel+bounces-45643-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-45643-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=codeweavers.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id BE92828A697 for ; Wed, 31 Jan 2024 02:20:13 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B4BFA3A8C0; Wed, 31 Jan 2024 02:16:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="k8hElhhe" 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 9666F5677; Wed, 31 Jan 2024 02:16:03 +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=1706667367; cv=none; b=WYe9JCEtADSlcQyM4+6igryAhwHe4z1RgAzsY4WPvGdszAgXi0neK2f4wAT+acPKdRsWQGu62vxnacNY5PDskb4Iv1CR7ep1IeH90nByBaE0B8t2d5E7LKS8whHh4TZpho3P+rIiYKL9FufzT8d/1ALqNrw3HYm2rxv7R4Au76I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706667367; c=relaxed/simple; bh=oGzttK4t0knChdTtyUyMOTbKygxUXc7pM2yjHNoCLmQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=k/YEoNzCb6kxHMO/P1dylrf0FXdfkfTgV9JcqgQYWNQDrV9sOqt3NZh1GiYjn+hq6ZcY1gDdcdkEeYChH3S/WpUb5stzp1RidFJ363bzQ27EwDsZO4ylmiSQ8kuZz2ubi4QPNrZioL4uUGND7cg8eGgJy7RVNOvlfhjfVm8hW7A= 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=k8hElhhe; 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 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=codeweavers.com; s=s1; h=Message-ID:Date:Subject:Cc:To:From:Sender; bh=hP8Bz/7TLAf/d75xqJERzPNlxl/Lxor2EgGxfs602cE=; b=k8hElhheWnkvX0v1/DvZauysNO AekF6tSv/6tQ5zF03mAlKgQwy4r2CkYyViT5B2DbnjDCSidQzDXBCQJVVor/Nn8FLKP6BxXjuV1pi I/7G5/yBJv3KGYWkAqdutwkDUOqkqOkp0/AsvjOhZNieey+AKJGnimd5bSEYHriMNjJQzWVXc8++t +7Phzllr4f4NOdjnbZJ85F9lEh7oXaIFWnl7tdbKv/BlNNLTd/xla7I3q662w1HQFaXt8h0sfI1Xv z01BnDpFMu22ymA1hifEin9oecRi34qsGb3c48EX+PlIdv2uKM7zmRt5DQJ4el6lVjM/+L9qvK9El Dq8gQYPg==; 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 1rV08p-0038Kv-38; Tue, 30 Jan 2024 20:15:56 -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_Almeida?= , Wolfram Sang , Arkadiusz Hiler , Peter Zijlstra , Andy Lutomirski , linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, Elizabeth Figura Subject: [RFC PATCH v2 21/29] selftests: ntsync: Add some tests for wakeup signaling with WINESYNC_IOC_WAIT_ANY. Date: Tue, 30 Jan 2024 20:13:48 -0600 Message-ID: <20240131021356.10322-22-zfigura@codeweavers.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240131021356.10322-1-zfigura@codeweavers.com> References: <20240131021356.10322-1-zfigura@codeweavers.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789570741868485769 X-GMAIL-MSGID: 1789570741868485769 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 | 152 ++++++++++++++++++ 1 file changed, 152 insertions(+) diff --git a/tools/testing/selftests/drivers/ntsync/ntsync.c b/tools/testing/selftests/drivers/ntsync/ntsync.c index 77f1b7e42d76..96a866ef235f 100644 --- a/tools/testing/selftests/drivers/ntsync/ntsync.c +++ b/tools/testing/selftests/drivers/ntsync/ntsync.c @@ -521,4 +521,156 @@ TEST(test_wait_all) close(fd); } +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 = arg; + + args->ret = ioctl(args->fd, args->request, args->args); + args->err = 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 += ms * 1000000; + timeout.tv_sec += (timeout.tv_nsec / 1000000000); + timeout.tv_nsec %= 1000000000; + return pthread_timedjoin_np(thread, NULL, &timeout); +} + +TEST(wake_any) +{ + struct ntsync_mutex_args mutex_args = {0}; + struct ntsync_wait_args wait_args = {0}; + struct ntsync_sem_args sem_args = {0}; + struct wait_args thread_args; + int objs[2], fd, ret; + __u32 count, index; + pthread_t thread; + + fd = open("/dev/ntsync", O_CLOEXEC | O_RDONLY); + ASSERT_LE(0, fd); + + sem_args.count = 0; + sem_args.max = 3; + sem_args.sem = 0xdeadbeef; + ret = ioctl(fd, NTSYNC_IOC_CREATE_SEM, &sem_args); + EXPECT_EQ(0, ret); + EXPECT_NE(0xdeadbeef, sem_args.sem); + + mutex_args.owner = 123; + mutex_args.count = 1; + mutex_args.mutex = 0xdeadbeef; + ret = ioctl(fd, NTSYNC_IOC_CREATE_MUTEX, &mutex_args); + EXPECT_EQ(0, ret); + EXPECT_NE(0xdeadbeef, mutex_args.mutex); + + objs[0] = sem_args.sem; + objs[1] = mutex_args.mutex; + + /* test waking the semaphore */ + + wait_args.timeout = get_abs_timeout(1000); + wait_args.objs = (uintptr_t)objs; + wait_args.count = 2; + wait_args.owner = 456; + wait_args.index = 0xdeadbeef; + thread_args.fd = fd; + thread_args.args = &wait_args; + thread_args.request = NTSYNC_IOC_WAIT_ANY; + ret = pthread_create(&thread, NULL, wait_thread, &thread_args); + EXPECT_EQ(0, ret); + + ret = wait_for_thread(thread, 100); + EXPECT_EQ(ETIMEDOUT, ret); + + count = 1; + ret = post_sem(sem_args.sem, &count); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, count); + check_sem_state(sem_args.sem, 0, 3); + + ret = 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 = wait_any(fd, 1, &mutex_args.mutex, 123, &index); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, index); + + wait_args.timeout = get_abs_timeout(1000); + wait_args.owner = 456; + ret = pthread_create(&thread, NULL, wait_thread, &thread_args); + EXPECT_EQ(0, ret); + + ret = wait_for_thread(thread, 100); + EXPECT_EQ(ETIMEDOUT, ret); + + ret = unlock_mutex(mutex_args.mutex, 123, &count); + EXPECT_EQ(0, ret); + EXPECT_EQ(2, count); + + ret = pthread_tryjoin_np(thread, NULL); + EXPECT_EQ(EBUSY, ret); + + ret = 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 = 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 = get_abs_timeout(200); + wait_args.owner = 123; + ret = pthread_create(&thread, NULL, wait_thread, &thread_args); + EXPECT_EQ(0, ret); + + ret = wait_for_thread(thread, 100); + EXPECT_EQ(ETIMEDOUT, ret); + + close(sem_args.sem); + close(mutex_args.mutex); + + ret = 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