1 | /* $Id: waitqueue-r0drv-linux.h 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Linux Ring-0 Driver Helpers for Abstracting Wait Queues,
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 | #ifndef IPRT_INCLUDED_SRC_r0drv_linux_waitqueue_r0drv_linux_h
|
---|
38 | #define IPRT_INCLUDED_SRC_r0drv_linux_waitqueue_r0drv_linux_h
|
---|
39 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
40 | # pragma once
|
---|
41 | #endif
|
---|
42 |
|
---|
43 | #include "the-linux-kernel.h"
|
---|
44 |
|
---|
45 | #include <iprt/asm-math.h>
|
---|
46 | #include <iprt/err.h>
|
---|
47 | #include <iprt/string.h>
|
---|
48 | #include <iprt/time.h>
|
---|
49 |
|
---|
50 | /** The resolution (nanoseconds) specified when using
|
---|
51 | * schedule_hrtimeout_range. */
|
---|
52 | #define RTR0SEMLNXWAIT_RESOLUTION 50000
|
---|
53 |
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Kernel mode Linux wait state structure.
|
---|
57 | */
|
---|
58 | typedef struct RTR0SEMLNXWAIT
|
---|
59 | {
|
---|
60 | /** The wait queue entry. */
|
---|
61 | #if RTLNX_VER_MIN(4,13,0) || RTLNX_SUSE_MAJ_PREREQ(12, 4) || RTLNX_SUSE_MAJ_PREREQ(15, 0)
|
---|
62 | wait_queue_entry_t WaitQE;
|
---|
63 | #else
|
---|
64 | wait_queue_t WaitQE;
|
---|
65 | #endif
|
---|
66 | /** The absolute timeout given as nano seconds since the start of the
|
---|
67 | * monotonic clock. */
|
---|
68 | uint64_t uNsAbsTimeout;
|
---|
69 | /** The timeout in nano seconds relative to the start of the wait. */
|
---|
70 | uint64_t cNsRelTimeout;
|
---|
71 | /** The native timeout value. */
|
---|
72 | union
|
---|
73 | {
|
---|
74 | #ifdef IPRT_LINUX_HAS_HRTIMER
|
---|
75 | /** The timeout when fHighRes is true. Absolute, so no updating. */
|
---|
76 | ktime_t KtTimeout;
|
---|
77 | #endif
|
---|
78 | /** The timeout when fHighRes is false. Updated after waiting. */
|
---|
79 | long lTimeout;
|
---|
80 | } u;
|
---|
81 | /** Set if we use high resolution timeouts. */
|
---|
82 | bool fHighRes;
|
---|
83 | /** Set if it's an indefinite wait. */
|
---|
84 | bool fIndefinite;
|
---|
85 | /** Set if we've already timed out.
|
---|
86 | * Set by rtR0SemLnxWaitDoIt and read by rtR0SemLnxWaitHasTimedOut. */
|
---|
87 | bool fTimedOut;
|
---|
88 | /** TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE. */
|
---|
89 | int iWaitState;
|
---|
90 | /** The wait queue. */
|
---|
91 | wait_queue_head_t *pWaitQueue;
|
---|
92 | } RTR0SEMLNXWAIT;
|
---|
93 | /** Pointer to a linux wait state. */
|
---|
94 | typedef RTR0SEMLNXWAIT *PRTR0SEMLNXWAIT;
|
---|
95 |
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Initializes a wait.
|
---|
99 | *
|
---|
100 | * The caller MUST check the wait condition BEFORE calling this function or the
|
---|
101 | * timeout logic will be flawed.
|
---|
102 | *
|
---|
103 | * @returns VINF_SUCCESS or VERR_TIMEOUT.
|
---|
104 | * @param pWait The wait structure.
|
---|
105 | * @param fFlags The wait flags.
|
---|
106 | * @param uTimeout The timeout.
|
---|
107 | * @param pWaitQueue The wait queue head.
|
---|
108 | */
|
---|
109 | DECLINLINE(int) rtR0SemLnxWaitInit(PRTR0SEMLNXWAIT pWait, uint32_t fFlags, uint64_t uTimeout,
|
---|
110 | wait_queue_head_t *pWaitQueue)
|
---|
111 | {
|
---|
112 | /*
|
---|
113 | * Process the flags and timeout.
|
---|
114 | */
|
---|
115 | if (!(fFlags & RTSEMWAIT_FLAGS_INDEFINITE))
|
---|
116 | {
|
---|
117 | /** @todo optimize: millisecs -> nanosecs -> millisec -> jiffies */
|
---|
118 | if (fFlags & RTSEMWAIT_FLAGS_MILLISECS)
|
---|
119 | uTimeout = uTimeout < UINT64_MAX / RT_US_1SEC * RT_US_1SEC
|
---|
120 | ? uTimeout * RT_US_1SEC
|
---|
121 | : UINT64_MAX;
|
---|
122 | if (uTimeout == UINT64_MAX)
|
---|
123 | fFlags |= RTSEMWAIT_FLAGS_INDEFINITE;
|
---|
124 | else
|
---|
125 | {
|
---|
126 | uint64_t u64Now;
|
---|
127 | if (fFlags & RTSEMWAIT_FLAGS_RELATIVE)
|
---|
128 | {
|
---|
129 | if (uTimeout == 0)
|
---|
130 | return VERR_TIMEOUT;
|
---|
131 |
|
---|
132 | u64Now = RTTimeSystemNanoTS();
|
---|
133 | pWait->cNsRelTimeout = uTimeout;
|
---|
134 | pWait->uNsAbsTimeout = u64Now + uTimeout;
|
---|
135 | if (pWait->uNsAbsTimeout < u64Now) /* overflow */
|
---|
136 | fFlags |= RTSEMWAIT_FLAGS_INDEFINITE;
|
---|
137 | }
|
---|
138 | else
|
---|
139 | {
|
---|
140 | u64Now = RTTimeSystemNanoTS();
|
---|
141 | if (u64Now >= uTimeout)
|
---|
142 | return VERR_TIMEOUT;
|
---|
143 |
|
---|
144 | pWait->cNsRelTimeout = uTimeout - u64Now;
|
---|
145 | pWait->uNsAbsTimeout = uTimeout;
|
---|
146 | }
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | if (!(fFlags & RTSEMWAIT_FLAGS_INDEFINITE))
|
---|
151 | {
|
---|
152 | pWait->fIndefinite = false;
|
---|
153 | #ifdef IPRT_LINUX_HAS_HRTIMER
|
---|
154 | if ( (fFlags & (RTSEMWAIT_FLAGS_NANOSECS | RTSEMWAIT_FLAGS_ABSOLUTE))
|
---|
155 | || pWait->cNsRelTimeout < RT_NS_1SEC / HZ * 4)
|
---|
156 | {
|
---|
157 | pWait->fHighRes = true;
|
---|
158 | # if BITS_PER_LONG < 64
|
---|
159 | if ( KTIME_SEC_MAX <= LONG_MAX
|
---|
160 | && pWait->uNsAbsTimeout >= KTIME_SEC_MAX * RT_NS_1SEC_64 + (RT_NS_1SEC - 1))
|
---|
161 | fFlags |= RTSEMWAIT_FLAGS_INDEFINITE;
|
---|
162 | else
|
---|
163 | # endif
|
---|
164 | pWait->u.KtTimeout = ns_to_ktime(pWait->uNsAbsTimeout);
|
---|
165 | }
|
---|
166 | else
|
---|
167 | #endif
|
---|
168 | {
|
---|
169 | uint64_t cJiffies = ASMMultU64ByU32DivByU32(pWait->cNsRelTimeout, HZ, RT_NS_1SEC);
|
---|
170 | if (cJiffies >= MAX_JIFFY_OFFSET)
|
---|
171 | fFlags |= RTSEMWAIT_FLAGS_INDEFINITE;
|
---|
172 | else
|
---|
173 | {
|
---|
174 | pWait->u.lTimeout = (long)cJiffies;
|
---|
175 | pWait->fHighRes = false;
|
---|
176 | }
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | if (fFlags & RTSEMWAIT_FLAGS_INDEFINITE)
|
---|
181 | {
|
---|
182 | pWait->fIndefinite = true;
|
---|
183 | pWait->fHighRes = false;
|
---|
184 | pWait->uNsAbsTimeout = UINT64_MAX;
|
---|
185 | pWait->cNsRelTimeout = UINT64_MAX;
|
---|
186 | pWait->u.lTimeout = LONG_MAX;
|
---|
187 | }
|
---|
188 |
|
---|
189 | pWait->fTimedOut = false;
|
---|
190 |
|
---|
191 | /*
|
---|
192 | * Initialize the wait queue related bits.
|
---|
193 | */
|
---|
194 | #if RTLNX_VER_MIN(2,5,39)
|
---|
195 | init_wait((&pWait->WaitQE));
|
---|
196 | #else
|
---|
197 | RT_ZERO(pWait->WaitQE);
|
---|
198 | init_waitqueue_entry((&pWait->WaitQE), current);
|
---|
199 | #endif
|
---|
200 | pWait->pWaitQueue = pWaitQueue;
|
---|
201 | pWait->iWaitState = fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE
|
---|
202 | ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
|
---|
203 |
|
---|
204 | return VINF_SUCCESS;
|
---|
205 | }
|
---|
206 |
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * Prepares the next wait.
|
---|
210 | *
|
---|
211 | * This must be called before rtR0SemLnxWaitDoIt, and the caller should check
|
---|
212 | * the exit conditions in-between the two calls.
|
---|
213 | *
|
---|
214 | * @param pWait The wait structure.
|
---|
215 | */
|
---|
216 | DECLINLINE(void) rtR0SemLnxWaitPrepare(PRTR0SEMLNXWAIT pWait)
|
---|
217 | {
|
---|
218 | /* Make everything thru schedule*() atomic scheduling wise. (Is this correct?) */
|
---|
219 | prepare_to_wait(pWait->pWaitQueue, &pWait->WaitQE, pWait->iWaitState);
|
---|
220 | }
|
---|
221 |
|
---|
222 |
|
---|
223 | /**
|
---|
224 | * Do the actual wait.
|
---|
225 | *
|
---|
226 | * @param pWait The wait structure.
|
---|
227 | */
|
---|
228 | DECLINLINE(void) rtR0SemLnxWaitDoIt(PRTR0SEMLNXWAIT pWait)
|
---|
229 | {
|
---|
230 | if (pWait->fIndefinite)
|
---|
231 | schedule();
|
---|
232 | #ifdef IPRT_LINUX_HAS_HRTIMER
|
---|
233 | else if (pWait->fHighRes)
|
---|
234 | {
|
---|
235 | int rc = schedule_hrtimeout_range(&pWait->u.KtTimeout, HRTIMER_MODE_ABS, RTR0SEMLNXWAIT_RESOLUTION);
|
---|
236 | if (!rc)
|
---|
237 | pWait->fTimedOut = true;
|
---|
238 | }
|
---|
239 | #endif
|
---|
240 | else
|
---|
241 | {
|
---|
242 | pWait->u.lTimeout = schedule_timeout(pWait->u.lTimeout);
|
---|
243 | if (pWait->u.lTimeout <= 0)
|
---|
244 | pWait->fTimedOut = true;
|
---|
245 | }
|
---|
246 | after_wait((&pWait->WaitQE));
|
---|
247 | }
|
---|
248 |
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * Checks if a linux wait was interrupted.
|
---|
252 | *
|
---|
253 | * @returns true / false
|
---|
254 | * @param pWait The wait structure.
|
---|
255 | * @remarks This shall be called before the first rtR0SemLnxWaitDoIt().
|
---|
256 | */
|
---|
257 | DECLINLINE(bool) rtR0SemLnxWaitWasInterrupted(PRTR0SEMLNXWAIT pWait)
|
---|
258 | {
|
---|
259 | return pWait->iWaitState == TASK_INTERRUPTIBLE
|
---|
260 | && signal_pending(current);
|
---|
261 | }
|
---|
262 |
|
---|
263 |
|
---|
264 | /**
|
---|
265 | * Checks if a linux wait has timed out.
|
---|
266 | *
|
---|
267 | * @returns true / false
|
---|
268 | * @param pWait The wait structure.
|
---|
269 | */
|
---|
270 | DECLINLINE(bool) rtR0SemLnxWaitHasTimedOut(PRTR0SEMLNXWAIT pWait)
|
---|
271 | {
|
---|
272 | return pWait->fTimedOut;
|
---|
273 | }
|
---|
274 |
|
---|
275 |
|
---|
276 | /**
|
---|
277 | * Deletes a linux wait.
|
---|
278 | *
|
---|
279 | * @param pWait The wait structure.
|
---|
280 | */
|
---|
281 | DECLINLINE(void) rtR0SemLnxWaitDelete(PRTR0SEMLNXWAIT pWait)
|
---|
282 | {
|
---|
283 | finish_wait(pWait->pWaitQueue, &pWait->WaitQE);
|
---|
284 | }
|
---|
285 |
|
---|
286 |
|
---|
287 | /**
|
---|
288 | * Gets the max resolution of the timeout machinery.
|
---|
289 | *
|
---|
290 | * @returns Resolution specified in nanoseconds.
|
---|
291 | */
|
---|
292 | DECLINLINE(uint32_t) rtR0SemLnxWaitGetResolution(void)
|
---|
293 | {
|
---|
294 | #ifdef IPRT_LINUX_HAS_HRTIMER
|
---|
295 | return RTR0SEMLNXWAIT_RESOLUTION;
|
---|
296 | #else
|
---|
297 | return RT_NS_1SEC / HZ; /* ns */
|
---|
298 | #endif
|
---|
299 | }
|
---|
300 |
|
---|
301 | #endif /* !IPRT_INCLUDED_SRC_r0drv_linux_waitqueue_r0drv_linux_h */
|
---|
302 |
|
---|