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/cdefs.h>
29__FBSDID("$FreeBSD$");
30
31#include <sys/types.h>
32#include <sys/umtx.h>
33
34#include <pthread.h>
35
36#include <atf-c.h>
37
38/*
39 * This is an implementation detail of _umtx_op(2), pulled from
40 * sys/kern/kern_umtx.c.  The relevant bug observed that requests above the
41 * batch side would not function as intended, so it's important that this
42 * reflects the BATCH_SIZE configured there.
43 */
44#define	UMTX_OP_BATCH_SIZE	128
45#define THREAD_COUNT		((UMTX_OP_BATCH_SIZE * 3) / 2)
46
47static pthread_mutex_t static_mutex = PTHREAD_MUTEX_INITIALIZER;
48
49static int batched_waiting;
50
51static void *
52batching_threadfunc(void *arg)
53{
54
55	pthread_mutex_lock(&static_mutex);
56	++batched_waiting;
57	pthread_mutex_unlock(&static_mutex);
58	_umtx_op(arg, UMTX_OP_WAIT_UINT_PRIVATE, 0, NULL, NULL);
59
60	return (NULL);
61}
62
63ATF_TC(batching);
64ATF_TC_HEAD(batching, tc)
65{
66	atf_tc_set_md_var(tc, "descr",
67	    "Checks batching of UMTX_OP_NWAKE_PRIVATE");
68}
69ATF_TC_BODY(batching, tc)
70{
71	uintptr_t addrs[THREAD_COUNT];
72	uint32_t vals[THREAD_COUNT];
73	pthread_t threads[THREAD_COUNT];
74
75	for (int i = 0; i < THREAD_COUNT; i++) {
76		addrs[i] = (uintptr_t)&vals[i];
77		vals[i] = 0;
78		pthread_create(&threads[i], NULL, batching_threadfunc,
79		    &vals[i]);
80	}
81
82	pthread_mutex_lock(&static_mutex);
83	while (batched_waiting != THREAD_COUNT) {
84		pthread_mutex_unlock(&static_mutex);
85		pthread_yield();
86		pthread_mutex_lock(&static_mutex);
87	}
88
89	/*
90	 * Spin for another .50 seconds to make sure they're all safely in the
91	 * kernel.
92	 */
93	usleep(500000);
94
95	pthread_mutex_unlock(&static_mutex);
96	_umtx_op(addrs, UMTX_OP_NWAKE_PRIVATE, THREAD_COUNT, NULL, NULL);
97
98	for (int i = 0; i < THREAD_COUNT; i++) {
99		ATF_REQUIRE_EQ(0, pthread_join(threads[i], NULL));
100	}
101}
102
103ATF_TP_ADD_TCS(tp)
104{
105
106	ATF_TP_ADD_TC(tp, batching);
107	return (atf_no_error());
108}
109