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