Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

selftests: ntsync: Add some tests for semaphore state.

Wine has tests for its synchronization primitives, but these are more accessible
to kernel developers, and also allow us to test some edge cases that Wine does
not care about.

This patch adds tests for semaphore-specific ioctls NTSYNC_IOC_SEM_POST and
NTSYNC_IOC_SEM_READ, and waiting on semaphores.

Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>
Link: https://lore.kernel.org/r/20241213193511.457338-17-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Elizabeth Figura and committed by
Greg Kroah-Hartman
7f853a25 a138179a

+155
+1
tools/testing/selftests/Makefile
··· 18 18 TARGETS += devices/probe 19 19 TARGETS += dmabuf-heaps 20 20 TARGETS += drivers/dma-buf 21 + TARGETS += drivers/ntsync 21 22 TARGETS += drivers/s390x/uvdevice 22 23 TARGETS += drivers/net 23 24 TARGETS += drivers/net/bonding
+1
tools/testing/selftests/drivers/ntsync/.gitignore
··· 1 + ntsync
+7
tools/testing/selftests/drivers/ntsync/Makefile
··· 1 + # SPDX-LICENSE-IDENTIFIER: GPL-2.0-only 2 + TEST_GEN_PROGS := ntsync 3 + 4 + CFLAGS += $(KHDR_INCLUDES) 5 + LDLIBS += -lpthread 6 + 7 + include ../../lib.mk
+1
tools/testing/selftests/drivers/ntsync/config
··· 1 + CONFIG_WINESYNC=y
+145
tools/testing/selftests/drivers/ntsync/ntsync.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * Various unit tests for the "ntsync" synchronization primitive driver. 4 + * 5 + * Copyright (C) 2021-2022 Elizabeth Figura <zfigura@codeweavers.com> 6 + */ 7 + 8 + #define _GNU_SOURCE 9 + #include <sys/ioctl.h> 10 + #include <sys/stat.h> 11 + #include <fcntl.h> 12 + #include <time.h> 13 + #include <pthread.h> 14 + #include <linux/ntsync.h> 15 + #include "../../kselftest_harness.h" 16 + 17 + static int read_sem_state(int sem, __u32 *count, __u32 *max) 18 + { 19 + struct ntsync_sem_args args; 20 + int ret; 21 + 22 + memset(&args, 0xcc, sizeof(args)); 23 + ret = ioctl(sem, NTSYNC_IOC_SEM_READ, &args); 24 + *count = args.count; 25 + *max = args.max; 26 + return ret; 27 + } 28 + 29 + #define check_sem_state(sem, count, max) \ 30 + ({ \ 31 + __u32 __count, __max; \ 32 + int ret = read_sem_state((sem), &__count, &__max); \ 33 + EXPECT_EQ(0, ret); \ 34 + EXPECT_EQ((count), __count); \ 35 + EXPECT_EQ((max), __max); \ 36 + }) 37 + 38 + static int release_sem(int sem, __u32 *count) 39 + { 40 + return ioctl(sem, NTSYNC_IOC_SEM_RELEASE, count); 41 + } 42 + 43 + static int wait_any(int fd, __u32 count, const int *objs, __u32 owner, __u32 *index) 44 + { 45 + struct ntsync_wait_args args = {0}; 46 + struct timespec timeout; 47 + int ret; 48 + 49 + clock_gettime(CLOCK_MONOTONIC, &timeout); 50 + 51 + args.timeout = timeout.tv_sec * 1000000000 + timeout.tv_nsec; 52 + args.count = count; 53 + args.objs = (uintptr_t)objs; 54 + args.owner = owner; 55 + args.index = 0xdeadbeef; 56 + ret = ioctl(fd, NTSYNC_IOC_WAIT_ANY, &args); 57 + *index = args.index; 58 + return ret; 59 + } 60 + 61 + TEST(semaphore_state) 62 + { 63 + struct ntsync_sem_args sem_args; 64 + struct timespec timeout; 65 + __u32 count, index; 66 + int fd, ret, sem; 67 + 68 + clock_gettime(CLOCK_MONOTONIC, &timeout); 69 + 70 + fd = open("/dev/ntsync", O_CLOEXEC | O_RDONLY); 71 + ASSERT_LE(0, fd); 72 + 73 + sem_args.count = 3; 74 + sem_args.max = 2; 75 + sem = ioctl(fd, NTSYNC_IOC_CREATE_SEM, &sem_args); 76 + EXPECT_EQ(-1, sem); 77 + EXPECT_EQ(EINVAL, errno); 78 + 79 + sem_args.count = 2; 80 + sem_args.max = 2; 81 + sem = ioctl(fd, NTSYNC_IOC_CREATE_SEM, &sem_args); 82 + EXPECT_LE(0, sem); 83 + check_sem_state(sem, 2, 2); 84 + 85 + count = 0; 86 + ret = release_sem(sem, &count); 87 + EXPECT_EQ(0, ret); 88 + EXPECT_EQ(2, count); 89 + check_sem_state(sem, 2, 2); 90 + 91 + count = 1; 92 + ret = release_sem(sem, &count); 93 + EXPECT_EQ(-1, ret); 94 + EXPECT_EQ(EOVERFLOW, errno); 95 + check_sem_state(sem, 2, 2); 96 + 97 + ret = wait_any(fd, 1, &sem, 123, &index); 98 + EXPECT_EQ(0, ret); 99 + EXPECT_EQ(0, index); 100 + check_sem_state(sem, 1, 2); 101 + 102 + ret = wait_any(fd, 1, &sem, 123, &index); 103 + EXPECT_EQ(0, ret); 104 + EXPECT_EQ(0, index); 105 + check_sem_state(sem, 0, 2); 106 + 107 + ret = wait_any(fd, 1, &sem, 123, &index); 108 + EXPECT_EQ(-1, ret); 109 + EXPECT_EQ(ETIMEDOUT, errno); 110 + 111 + count = 3; 112 + ret = release_sem(sem, &count); 113 + EXPECT_EQ(-1, ret); 114 + EXPECT_EQ(EOVERFLOW, errno); 115 + check_sem_state(sem, 0, 2); 116 + 117 + count = 2; 118 + ret = release_sem(sem, &count); 119 + EXPECT_EQ(0, ret); 120 + EXPECT_EQ(0, count); 121 + check_sem_state(sem, 2, 2); 122 + 123 + ret = wait_any(fd, 1, &sem, 123, &index); 124 + EXPECT_EQ(0, ret); 125 + ret = wait_any(fd, 1, &sem, 123, &index); 126 + EXPECT_EQ(0, ret); 127 + 128 + count = 1; 129 + ret = release_sem(sem, &count); 130 + EXPECT_EQ(0, ret); 131 + EXPECT_EQ(0, count); 132 + check_sem_state(sem, 1, 2); 133 + 134 + count = ~0u; 135 + ret = release_sem(sem, &count); 136 + EXPECT_EQ(-1, ret); 137 + EXPECT_EQ(EOVERFLOW, errno); 138 + check_sem_state(sem, 1, 2); 139 + 140 + close(sem); 141 + 142 + close(fd); 143 + } 144 + 145 + TEST_HARNESS_MAIN