selftests: ntsync: Add a stress test for contended waits.

Test a more realistic usage pattern, and one with heavy contention, in order to
actually exercise ntsync's internal synchronization.

This test has several threads in a tight loop acquiring a mutex, modifying some
shared data, and then releasing the mutex. At the end we check if the data is
consistent.

Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>
Link: https://lore.kernel.org/r/20241213193511.457338-28-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
Elizabeth Figura
2025-01-08 13:18:12 +01:00
committed by Greg Kroah-Hartman
parent c52b9cb13f
commit a22860e57b
@@ -1268,4 +1268,76 @@ TEST(alert_all)
close(fd);
}
#define STRESS_LOOPS 10000
#define STRESS_THREADS 4
static unsigned int stress_counter;
static int stress_device, stress_start_event, stress_mutex;
static void *stress_thread(void *arg)
{
struct ntsync_wait_args wait_args = {0};
__u32 index, count, i;
int ret;
wait_args.timeout = UINT64_MAX;
wait_args.count = 1;
wait_args.objs = (uintptr_t)&stress_start_event;
wait_args.owner = gettid();
wait_args.index = 0xdeadbeef;
ioctl(stress_device, NTSYNC_IOC_WAIT_ANY, &wait_args);
wait_args.objs = (uintptr_t)&stress_mutex;
for (i = 0; i < STRESS_LOOPS; ++i) {
ioctl(stress_device, NTSYNC_IOC_WAIT_ANY, &wait_args);
++stress_counter;
unlock_mutex(stress_mutex, wait_args.owner, &count);
}
return NULL;
}
TEST(stress_wait)
{
struct ntsync_event_args event_args;
struct ntsync_mutex_args mutex_args;
pthread_t threads[STRESS_THREADS];
__u32 signaled, i;
int ret;
stress_device = open("/dev/ntsync", O_CLOEXEC | O_RDONLY);
ASSERT_LE(0, stress_device);
mutex_args.owner = 0;
mutex_args.count = 0;
stress_mutex = ioctl(stress_device, NTSYNC_IOC_CREATE_MUTEX, &mutex_args);
EXPECT_LE(0, stress_mutex);
event_args.manual = 1;
event_args.signaled = 0;
stress_start_event = ioctl(stress_device, NTSYNC_IOC_CREATE_EVENT, &event_args);
EXPECT_LE(0, stress_start_event);
for (i = 0; i < STRESS_THREADS; ++i)
pthread_create(&threads[i], NULL, stress_thread, NULL);
ret = ioctl(stress_start_event, NTSYNC_IOC_EVENT_SET, &signaled);
EXPECT_EQ(0, ret);
for (i = 0; i < STRESS_THREADS; ++i) {
ret = pthread_join(threads[i], NULL);
EXPECT_EQ(0, ret);
}
EXPECT_EQ(STRESS_LOOPS * STRESS_THREADS, stress_counter);
close(stress_start_event);
close(stress_mutex);
close(stress_device);
}
TEST_HARNESS_MAIN