1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2020 Kyle Evans <kevans@FreeBSD.org>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28#include <sys/types.h>
29#include <sys/umtx.h>
30
31#include <pthread.h>
32
33#include <atf-c.h>
34
35/*
36 * This is an implementation detail of _umtx_op(2), pulled from
37 * sys/kern/kern_umtx.c.  The relevant bug observed that requests above the
38 * batch side would not function as intended, so it's important that this
39 * reflects the BATCH_SIZE configured there.
40 */
41#define	UMTX_OP_BATCH_SIZE	128
42#define THREAD_COUNT		((UMTX_OP_BATCH_SIZE * 3) / 2)
43
44static pthread_mutex_t static_mutex = PTHREAD_MUTEX_INITIALIZER;
45
46static int batched_waiting;
47
48static void *
49batching_threadfunc(void *arg)
50{
51
52	pthread_mutex_lock(&static_mutex);
53	++batched_waiting;
54	pthread_mutex_unlock(&static_mutex);
55	_umtx_op(arg, UMTX_OP_WAIT_UINT_PRIVATE, 0, NULL, NULL);
56
57	return (NULL);
58}
59
60ATF_TC(batching);
61ATF_TC_HEAD(batching, tc)
62{
63	atf_tc_set_md_var(tc, "descr",
64	    "Checks batching of UMTX_OP_NWAKE_PRIVATE");
65}
66ATF_TC_BODY(batching, tc)
67{
68	uintptr_t addrs[THREAD_COUNT];
69	uint32_t vals[THREAD_COUNT];
70	pthread_t threads[THREAD_COUNT];
71
72	for (int i = 0; i < THREAD_COUNT; i++) {
73		addrs[i] = (uintptr_t)&vals[i];
74		vals[i] = 0;
75		pthread_create(&threads[i], NULL, batching_threadfunc,
76		    &vals[i]);
77	}
78
79	pthread_mutex_lock(&static_mutex);
80	while (batched_waiting != THREAD_COUNT) {
81		pthread_mutex_unlock(&static_mutex);
82		pthread_yield();
83		pthread_mutex_lock(&static_mutex);
84	}
85
86	/*
87	 * Spin for another .50 seconds to make sure they're all safely in the
88	 * kernel.
89	 */
90	usleep(500000);
91
92	pthread_mutex_unlock(&static_mutex);
93	_umtx_op(addrs, UMTX_OP_NWAKE_PRIVATE, THREAD_COUNT, NULL, NULL);
94
95	for (int i = 0; i < THREAD_COUNT; i++) {
96		ATF_REQUIRE_EQ(0, pthread_join(threads[i], NULL));
97	}
98}
99
100ATF_TP_ADD_TCS(tp)
101{
102
103	ATF_TP_ADD_TC(tp, batching);
104	return (atf_no_error());
105}
106