linux_wait_bit.c revision 1.3 1 1.3 riastrad /* $NetBSD: linux_wait_bit.c,v 1.3 2021/12/19 01:42:02 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.1 riastrad * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * This code is derived from software contributed to The NetBSD Foundation
8 1.1 riastrad * by Taylor R. Campbell.
9 1.1 riastrad *
10 1.1 riastrad * Redistribution and use in source and binary forms, with or without
11 1.1 riastrad * modification, are permitted provided that the following conditions
12 1.1 riastrad * are met:
13 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
14 1.1 riastrad * notice, this list of conditions and the following disclaimer.
15 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
17 1.1 riastrad * documentation and/or other materials provided with the distribution.
18 1.1 riastrad *
19 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
30 1.1 riastrad */
31 1.1 riastrad
32 1.1 riastrad #include <sys/cdefs.h>
33 1.3 riastrad __KERNEL_RCSID(0, "$NetBSD: linux_wait_bit.c,v 1.3 2021/12/19 01:42:02 riastradh Exp $");
34 1.1 riastrad
35 1.1 riastrad #include <sys/param.h>
36 1.1 riastrad #include <sys/types.h>
37 1.1 riastrad #include <sys/bitops.h>
38 1.1 riastrad #include <sys/condvar.h>
39 1.1 riastrad #include <sys/mutex.h>
40 1.1 riastrad #include <sys/systm.h>
41 1.1 riastrad
42 1.1 riastrad #include <linux/bitops.h>
43 1.1 riastrad #include <linux/sched.h>
44 1.1 riastrad #include <linux/wait_bit.h>
45 1.1 riastrad
46 1.1 riastrad static struct {
47 1.1 riastrad struct waitbitentry {
48 1.1 riastrad kmutex_t lock;
49 1.1 riastrad kcondvar_t cv;
50 1.1 riastrad } ent;
51 1.1 riastrad char pad[CACHE_LINE_SIZE - sizeof(struct waitbitentry)];
52 1.1 riastrad } waitbittab[PAGE_SIZE/CACHE_LINE_SIZE] __cacheline_aligned;
53 1.1 riastrad CTASSERT(sizeof(waitbittab) == PAGE_SIZE);
54 1.1 riastrad CTASSERT(sizeof(waitbittab[0]) == CACHE_LINE_SIZE);
55 1.1 riastrad
56 1.1 riastrad int
57 1.1 riastrad linux_wait_bit_init(void)
58 1.1 riastrad {
59 1.1 riastrad size_t i;
60 1.1 riastrad
61 1.1 riastrad for (i = 0; i < __arraycount(waitbittab); i++) {
62 1.1 riastrad mutex_init(&waitbittab[i].ent.lock, MUTEX_DEFAULT, IPL_VM);
63 1.1 riastrad cv_init(&waitbittab[i].ent.cv, "waitbit");
64 1.1 riastrad }
65 1.1 riastrad
66 1.1 riastrad return 0;
67 1.1 riastrad }
68 1.1 riastrad
69 1.1 riastrad void
70 1.1 riastrad linux_wait_bit_fini(void)
71 1.1 riastrad {
72 1.1 riastrad size_t i;
73 1.1 riastrad
74 1.1 riastrad for (i = 0; i < __arraycount(waitbittab); i++) {
75 1.1 riastrad cv_destroy(&waitbittab[i].ent.cv);
76 1.1 riastrad mutex_destroy(&waitbittab[i].ent.lock);
77 1.1 riastrad }
78 1.1 riastrad }
79 1.1 riastrad
80 1.1 riastrad static inline size_t
81 1.1 riastrad wait_bit_hash(const volatile unsigned long *bitmap, unsigned bit)
82 1.1 riastrad {
83 1.1 riastrad /* Try to avoid cache line collisions. */
84 1.1 riastrad const volatile unsigned long *word = bitmap + bit/(NBBY*sizeof(*word));
85 1.1 riastrad
86 1.1 riastrad return ((uintptr_t)word >> ilog2(CACHE_LINE_SIZE)) %
87 1.1 riastrad __arraycount(waitbittab);
88 1.1 riastrad }
89 1.1 riastrad
90 1.1 riastrad static struct waitbitentry *
91 1.1 riastrad wait_bit_enter(const volatile unsigned long *bitmap, unsigned bit)
92 1.1 riastrad {
93 1.1 riastrad struct waitbitentry *wbe = &waitbittab[wait_bit_hash(bitmap, bit)].ent;
94 1.1 riastrad
95 1.1 riastrad mutex_enter(&wbe->lock);
96 1.1 riastrad
97 1.1 riastrad return wbe;
98 1.1 riastrad }
99 1.1 riastrad
100 1.1 riastrad static void
101 1.1 riastrad wait_bit_exit(struct waitbitentry *wbe)
102 1.1 riastrad {
103 1.1 riastrad
104 1.1 riastrad mutex_exit(&wbe->lock);
105 1.1 riastrad }
106 1.1 riastrad
107 1.1 riastrad void
108 1.1 riastrad wake_up_bit(const volatile unsigned long *bitmap, unsigned bit)
109 1.1 riastrad {
110 1.1 riastrad struct waitbitentry *wbe;
111 1.1 riastrad
112 1.1 riastrad wbe = wait_bit_enter(bitmap, bit);
113 1.1 riastrad cv_broadcast(&wbe->cv);
114 1.1 riastrad wait_bit_exit(wbe);
115 1.1 riastrad }
116 1.1 riastrad
117 1.1 riastrad int
118 1.3 riastrad wait_on_bit(const volatile unsigned long *bitmap, unsigned bit, int flags)
119 1.3 riastrad {
120 1.3 riastrad struct waitbitentry *wbe;
121 1.3 riastrad int error, ret;
122 1.3 riastrad
123 1.3 riastrad if (test_bit(bit, bitmap))
124 1.3 riastrad return 0;
125 1.3 riastrad
126 1.3 riastrad wbe = wait_bit_enter(bitmap, bit);
127 1.3 riastrad
128 1.3 riastrad while (!test_bit(bit, bitmap)) {
129 1.3 riastrad if (flags & TASK_UNINTERRUPTIBLE) {
130 1.3 riastrad cv_wait(&wbe->cv, &wbe->lock);
131 1.3 riastrad } else {
132 1.3 riastrad error = cv_wait_sig(&wbe->cv, &wbe->lock);
133 1.3 riastrad if (error == EINTR || error == ERESTART)
134 1.3 riastrad ret = -ERESTARTSYS;
135 1.3 riastrad else if (error != 0)
136 1.3 riastrad ret = -error;
137 1.3 riastrad if (ret)
138 1.3 riastrad goto out;
139 1.3 riastrad }
140 1.3 riastrad }
141 1.3 riastrad
142 1.3 riastrad /* Bit is set. Return zero on success. */
143 1.3 riastrad ret = 0;
144 1.3 riastrad
145 1.3 riastrad out: wait_bit_exit(wbe);
146 1.3 riastrad return ret;
147 1.3 riastrad }
148 1.3 riastrad
149 1.3 riastrad int
150 1.1 riastrad wait_on_bit_timeout(const volatile unsigned long *bitmap, unsigned bit,
151 1.1 riastrad int flags, unsigned long timeout)
152 1.1 riastrad {
153 1.1 riastrad struct waitbitentry *wbe;
154 1.1 riastrad int error, ret;
155 1.1 riastrad
156 1.1 riastrad if (test_bit(bit, bitmap))
157 1.1 riastrad return timeout;
158 1.1 riastrad
159 1.1 riastrad wbe = wait_bit_enter(bitmap, bit);
160 1.1 riastrad
161 1.1 riastrad while (!test_bit(bit, bitmap)) {
162 1.1 riastrad unsigned starttime, endtime;
163 1.1 riastrad
164 1.1 riastrad starttime = hardclock_ticks;
165 1.1 riastrad if (flags & TASK_UNINTERRUPTIBLE) {
166 1.1 riastrad error = cv_timedwait(&wbe->cv, &wbe->lock,
167 1.1 riastrad MIN(INT_MAX, timeout));
168 1.1 riastrad } else {
169 1.1 riastrad error = cv_timedwait_sig(&wbe->cv, &wbe->lock,
170 1.1 riastrad MIN(INT_MAX, timeout));
171 1.1 riastrad }
172 1.1 riastrad endtime = hardclock_ticks;
173 1.1 riastrad
174 1.1 riastrad /* If we timed out, return zero time left. */
175 1.1 riastrad if (error == EWOULDBLOCK || endtime - starttime < timeout) {
176 1.1 riastrad ret = 0;
177 1.1 riastrad goto out;
178 1.1 riastrad }
179 1.1 riastrad
180 1.1 riastrad /* If we were interrupted, return -ERESTARTSYS. */
181 1.2 riastrad if (error == EINTR || error == ERESTART) {
182 1.1 riastrad ret = -ERESTARTSYS;
183 1.1 riastrad goto out;
184 1.1 riastrad }
185 1.1 riastrad
186 1.1 riastrad /* Otherwise, debit the time spent. */
187 1.1 riastrad timeout -= (endtime - starttime);
188 1.1 riastrad }
189 1.1 riastrad /* Bit is set. Return the time left. */
190 1.1 riastrad ret = timeout;
191 1.1 riastrad
192 1.1 riastrad out: wait_bit_exit(wbe);
193 1.1 riastrad return ret;
194 1.1 riastrad }
195