Print this page
pbchk
cleanup port_free_event_local() semantics
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/uts/common/fs/portfs/port.c
+++ new/usr/src/uts/common/fs/portfs/port.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
|
↓ open down ↓ |
17 lines elided |
↑ open up ↑ |
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 */
26 26
27 27 /*
28 - * Copyright (c) 2015 Joyent, Inc. All rights reserved.
28 + * Copyright 2020 Joyent, Inc.
29 29 */
30 30
31 31 #include <sys/types.h>
32 32 #include <sys/systm.h>
33 33 #include <sys/cred.h>
34 34 #include <sys/modctl.h>
35 35 #include <sys/vfs.h>
36 36 #include <sys/vfs_opreg.h>
37 37 #include <sys/sysmacros.h>
38 38 #include <sys/cmn_err.h>
39 39 #include <sys/stat.h>
40 40 #include <sys/errno.h>
41 41 #include <sys/kmem.h>
42 42 #include <sys/file.h>
43 43 #include <sys/kstat.h>
44 44 #include <sys/port_impl.h>
45 45 #include <sys/task.h>
46 46 #include <sys/project.h>
47 47
48 48 /*
49 49 * Event Ports can be shared across threads or across processes.
50 50 * Every thread/process can use an own event port or a group of them
51 51 * can use a single port. A major request was also to get the ability
52 52 * to submit user-defined events to a port. The idea of the
53 53 * user-defined events is to use the event ports for communication between
54 54 * threads/processes (like message queues). User defined-events are queued
55 55 * in a port with the same priority as other event types.
56 56 *
57 57 * Events are delivered only once. The thread/process which is waiting
58 58 * for events with the "highest priority" (priority here is related to the
59 59 * internal strategy to wakeup waiting threads) will retrieve the event,
60 60 * all other threads/processes will not be notified. There is also
61 61 * the requirement to have events which should be submitted immediately
62 62 * to all "waiting" threads. That is the main task of the alert event.
63 63 * The alert event is submitted by the application to a port. The port
64 64 * changes from a standard mode to the alert mode. Now all waiting threads
65 65 * will be awaken immediately and they will return with the alert event.
66 66 * Threads trying to retrieve events from a port in alert mode will
67 67 * return immediately with the alert event.
68 68 *
69 69 *
70 70 * An event port is like a kernel queue, which accept events submitted from
71 71 * user level as well as events submitted from kernel sub-systems. Sub-systems
72 72 * able to submit events to a port are the so-called "event sources".
73 73 * Current event sources:
74 74 * PORT_SOURCE_AIO : events submitted per transaction completion from
75 75 * POSIX-I/O framework.
76 76 * PORT_SOURCE_TIMER : events submitted when a timer fires
77 77 * (see timer_create(3RT)).
78 78 * PORT_SOURCE_FD : events submitted per file descriptor (see poll(2)).
79 79 * PORT_SOURCE_ALERT : events submitted from user. This is not really a
80 80 * single event, this is actually a port mode
81 81 * (see port_alert(3c)).
82 82 * PORT_SOURCE_USER : events submitted by applications with
83 83 * port_send(3c) or port_sendn(3c).
84 84 * PORT_SOURCE_FILE : events submitted per file being watched for file
85 85 * change events (see port_create(3c).
86 86 *
87 87 * There is a user API implemented in the libc library as well as a
88 88 * kernel API implemented in port_subr.c in genunix.
89 89 * The available user API functions are:
90 90 * port_create() : create a port as a file descriptor of portfs file system
91 91 * The standard close(2) function closes a port.
92 92 * port_associate() : associate a file descriptor with a port to be able to
93 93 * retrieve events from that file descriptor.
94 94 * port_dissociate(): remove the association of a file descriptor with a port.
95 95 * port_alert() : set/unset a port in alert mode
96 96 * port_send() : send an event of type PORT_SOURCE_USER to a port
97 97 * port_sendn() : send an event of type PORT_SOURCE_USER to a list of ports
98 98 * port_get() : retrieve a single event from a port
99 99 * port_getn() : retrieve a list of events from a port
100 100 *
101 101 * The available kernel API functions are:
102 102 * port_allocate_event(): allocate an event slot/structure of/from a port
103 103 * port_init_event() : set event data in the event structure
104 104 * port_send_event() : send event to a port
105 105 * port_free_event() : deliver allocated slot/structure back to a port
106 106 * port_associate_ksource(): associate a kernel event source with a port
107 107 * port_dissociate_ksource(): dissociate a kernel event source from a port
108 108 *
109 109 * The libc implementation consists of small functions which pass the
110 110 * arguments to the kernel using the "portfs" system call. It means, all the
111 111 * synchronisation work is being done in the kernel. The "portfs" system
112 112 * call loads the portfs file system into the kernel.
113 113 *
114 114 * PORT CREATION
115 115 * The first function to be used is port_create() which internally creates
116 116 * a vnode and a portfs node. The portfs node is represented by the port_t
117 117 * structure, which again includes all the data necessary to control a port.
118 118 * port_create() returns a file descriptor, which needs to be used in almost
119 119 * all other event port functions.
120 120 * The maximum number of ports per system is controlled by the resource
121 121 * control: project:port-max-ids.
122 122 *
123 123 * EVENT GENERATION
124 124 * The second step is the triggering of events, which could be sent to a port.
125 125 * Every event source implements an own method to generate events for a port:
126 126 * PORT_SOURCE_AIO:
127 127 * The sigevent structure of the standard POSIX-IO functions
128 128 * was extended by an additional notification type.
129 129 * Standard notification types:
130 130 * SIGEV_NONE, SIGEV_SIGNAL and SIGEV_THREAD
131 131 * Event ports introduced now SIGEV_PORT.
132 132 * The notification type SIGEV_PORT specifies that a structure
133 133 * of type port_notify_t has to be attached to the sigev_value.
134 134 * The port_notify_t structure contains the event port file
135 135 * descriptor and a user-defined pointer.
136 136 * Internally the AIO implementation will use the kernel API
137 137 * functions to allocate an event port slot per transaction (aiocb)
138 138 * and sent the event to the port as soon as the transaction completes.
139 139 * All the events submitted per transaction are of type
140 140 * PORT_SOURCE_AIO.
141 141 * PORT_SOURCE_TIMER:
142 142 * The timer_create() function uses the same method as the
143 143 * PORT_SOURCE_AIO event source. It also uses the sigevent structure
144 144 * to deliver the port information.
145 145 * Internally the timer code will allocate a single event slot/struct
146 146 * per timer and it will send the timer event as soon as the timer
147 147 * fires. If the timer-fired event is not delivered to the application
148 148 * before the next period elapsed, then an overrun counter will be
149 149 * incremented. The timer event source uses a callback function to
150 150 * detect the delivery of the event to the application. At that time
151 151 * the timer callback function will update the event overrun counter.
152 152 * PORT_SOURCE_FD:
153 153 * This event source uses the port_associate() function to allocate
154 154 * an event slot/struct from a port. The application defines in the
155 155 * events argument of port_associate() the type of events which it is
156 156 * interested on.
157 157 * The internal pollwakeup() function is used by all the file
158 158 * systems --which are supporting the VOP_POLL() interface- to notify
159 159 * the upper layer (poll(2), devpoll(7d) and now event ports) about
160 160 * the event triggered (see valid events in poll(2)).
161 161 * The pollwakeup() function forwards the event to the layer registered
162 162 * to receive the current event.
163 163 * The port_dissociate() function can be used to free the allocated
164 164 * event slot from the port. Anyway, file descriptors deliver events
165 165 * only one time and remain deactivated until the application
166 166 * reactivates the association of a file descriptor with port_associate().
167 167 * If an associated file descriptor is closed then the file descriptor
168 168 * will be dissociated automatically from the port.
169 169 *
170 170 * PORT_SOURCE_ALERT:
171 171 * This event type is generated when the port was previously set in
172 172 * alert mode using the port_alert() function.
173 173 * A single alert event is delivered to every thread which tries to
174 174 * retrieve events from a port.
175 175 * PORT_SOURCE_USER:
176 176 * This type of event is generated from user level using the port_send()
177 177 * function to send a user event to a port or the port_sendn() function
178 178 * to send an event to a list of ports.
179 179 * PORT_SOURCE_FILE:
180 180 * This event source uses the port_associate() interface to register
181 181 * a file to be monitored for changes. The file name that needs to be
182 182 * monitored is specified in the file_obj_t structure, a pointer to which
183 183 * is passed as an argument. The event types to be monitored are specified
184 184 * in the events argument.
185 185 * A file events monitor is represented internal per port per object
186 186 * address(the file_obj_t pointer). Which means there can be multiple
187 187 * watches registered on the same file using different file_obj_t
188 188 * structure pointer. With the help of the FEM(File Event Monitoring)
189 189 * hooks, the file's vnode ops are intercepted and relevant events
190 190 * delivered. The port_dissociate() function is used to de-register a
191 191 * file events monitor on a file. When the specified file is
192 192 * removed/renamed, the file events watch/monitor is automatically
193 193 * removed.
194 194 *
195 195 * EVENT DELIVERY / RETRIEVING EVENTS
196 196 * Events remain in the port queue until:
197 197 * - the application uses port_get() or port_getn() to retrieve events,
198 198 * - the event source cancel the event,
199 199 * - the event port is closed or
200 200 * - the process exits.
201 201 * The maximal number of events in a port queue is the maximal number
202 202 * of event slots/structures which can be allocated by event sources.
203 203 * The allocation of event slots/structures is controlled by the resource
204 204 * control: process.port-max-events.
205 205 * The port_get() function retrieves a single event and the port_getn()
206 206 * function retrieves a list of events.
207 207 * Events are classified as shareable and non-shareable events across processes.
208 208 * Non-shareable events are invisible for the port_get(n)() functions of
209 209 * processes other than the owner of the event.
210 210 * Shareable event types are:
211 211 * PORT_SOURCE_USER events
212 212 * This type of event is unconditionally shareable and without
213 213 * limitations. If the parent process sends a user event and closes
214 214 * the port afterwards, the event remains in the port and the child
215 215 * process will still be able to retrieve the user event.
216 216 * PORT_SOURCE_ALERT events
217 217 * This type of event is shareable between processes.
218 218 * Limitation: The alert mode of the port is removed if the owner
219 219 * (process which set the port in alert mode) of the
220 220 * alert event closes the port.
221 221 * PORT_SOURCE_FD events
222 222 * This type of event is conditional shareable between processes.
223 223 * After fork(2) all forked file descriptors are shareable between
224 224 * the processes. The child process is allowed to retrieve events
225 225 * from the associated file descriptors and it can also re-associate
226 226 * the fd with the port.
227 227 * Limitations: The child process is not allowed to dissociate
228 228 * the file descriptor from the port. Only the
229 229 * owner (process) of the association is allowed to
230 230 * dissociate the file descriptor from the port.
231 231 * If the owner of the association closes the port
232 232 * the association will be removed.
233 233 * PORT_SOURCE_AIO events
234 234 * This type of event is not shareable between processes.
235 235 * PORT_SOURCE_TIMER events
236 236 * This type of event is not shareable between processes.
237 237 * PORT_SOURCE_FILE events
238 238 * This type of event is not shareable between processes.
239 239 *
240 240 * FORK BEHAVIOUR
241 241 * On fork(2) the child process inherits all opened file descriptors from
242 242 * the parent process. This is also valid for port file descriptors.
243 243 * Associated file descriptors with a port maintain the association across the
244 244 * fork(2). It means, the child process gets full access to the port and
245 245 * it can retrieve events from all common associated file descriptors.
246 246 * Events of file descriptors created and associated with a port after the
247 247 * fork(2) are non-shareable and can only be retrieved by the same process.
248 248 *
249 249 * If the parent or the child process closes an exported port (using fork(2)
250 250 * or I_SENDFD) all the file descriptors associated with the port by the
251 251 * process will be dissociated from the port. Events of dissociated file
252 252 * descriptors as well as all non-shareable events will be discarded.
253 253 * The other process can continue working with the port as usual.
254 254 *
255 255 * CLOSING A PORT
256 256 * close(2) has to be used to close a port. See FORK BEHAVIOUR for details.
257 257 *
258 258 * PORT EVENT STRUCTURES
259 259 * The global control structure of the event ports framework is port_control_t.
260 260 * port_control_t keeps track of the number of created ports in the system.
261 261 * The cache of the port event structures is also located in port_control_t.
262 262 *
263 263 * On port_create() the vnode and the portfs node is also created.
264 264 * The portfs node is represented by the port_t structure.
265 265 * The port_t structure manages all port specific tasks:
266 266 * - management of resource control values
267 267 * - port VOP_POLL interface
268 268 * - creation time
269 269 * - uid and gid of the port
270 270 *
271 271 * The port_t structure contains the port_queue_t structure.
272 272 * The port_queue_t structure contains all the data necessary for the
273 273 * queue management:
274 274 * - locking
275 275 * - condition variables
276 276 * - event counters
277 277 * - submitted events (represented by port_kevent_t structures)
278 278 * - threads waiting for event delivery (check portget_t structure)
279 279 * - PORT_SOURCE_FD cache (managed by the port_fdcache_t structure)
280 280 * - event source management (managed by the port_source_t structure)
281 281 * - alert mode management (check port_alert_t structure)
282 282 *
283 283 * EVENT MANAGEMENT
284 284 * The event port file system creates a kmem_cache for internal allocation of
285 285 * event port structures.
286 286 *
287 287 * 1. Event source association with a port:
288 288 * The first step to do for event sources is to get associated with a port
289 289 * using the port_associate_ksource() function or adding an entry to the
290 290 * port_ksource_tab[]. An event source can get dissociated from a port
291 291 * using the port_dissociate_ksource() function. An entry in the
292 292 * port_ksource_tab[] implies that the source will be associated
293 293 * automatically with every new created port.
294 294 * The event source can deliver a callback function, which is used by the
295 295 * port to notify the event source about close(2). The idea is that
296 296 * in such a case the event source should free all allocated resources
297 297 * and it must return to the port all allocated slots/structures.
298 298 * The port_close() function will wait until all allocated event
299 299 * structures/slots are returned to the port.
300 300 * The callback function is not necessary when the event source does not
301 301 * maintain local resources, a second condition is that the event source
302 302 * can guarantee that allocated event slots will be returned without
303 303 * delay to the port (it will not block and sleep somewhere).
304 304 *
305 305 * 2. Reservation of an event slot / event structure
306 306 * The event port reliability is based on the reservation of an event "slot"
307 307 * (allocation of an event structure) by the event source as part of the
308 308 * application call. If the maximal number of event slots is exhausted then
309 309 * the event source can return a corresponding error code to the application.
310 310 *
311 311 * The port_alloc_event() function has to be used by event sources to
312 312 * allocate an event slot (reserve an event structure). The port_alloc_event()
313 313 * doesn not block and it will return a 0 value on success or an error code
314 314 * if it fails.
315 315 * An argument of port_alloc_event() is a flag which determines the behavior
316 316 * of the event after it was delivered to the application:
317 317 * PORT_ALLOC_DEFAULT : event slot becomes free after delivery to the
318 318 * application.
319 319 * PORT_ALLOC_PRIVATE : event slot remains under the control of the event
320 320 * source. This kind of slots can not be used for
321 321 * event delivery and should only be used internally
322 322 * by the event source.
323 323 * PORT_KEV_CACHED : event slot remains under the control of an event
324 324 * port cache. It does not become free after delivery
325 325 * to the application.
326 326 * PORT_ALLOC_SCACHED : event slot remains under the control of the event
327 327 * source. The event source takes the control over
328 328 * the slot after the event is delivered to the
329 329 * application.
330 330 *
331 331 * 3. Delivery of events to the event port
332 332 * Earlier allocated event structure/slot has to be used to deliver
333 333 * event data to the port. Event source has to use the function
334 334 * port_send_event(). The single argument is a pointer to the previously
335 335 * reserved event structure/slot.
336 336 * The portkev_events field of the port_kevent_t structure can be updated/set
337 337 * in two ways:
338 338 * 1. using the port_set_event() function, or
339 339 * 2. updating the portkev_events field out of the callback function:
340 340 * The event source can deliver a callback function to the port as an
341 341 * argument of port_init_event().
342 342 * One of the arguments of the callback function is a pointer to the
343 343 * events field, which will be delivered to the application.
344 344 * (see Delivery of events to the application).
345 345 * Event structures/slots can be delivered to the event port only one time,
346 346 * they remain blocked until the data is delivered to the application and the
347 347 * slot becomes free or it is delivered back to the event source
348 348 * (PORT_ALLOC_SCACHED). The activation of the callback function mentioned above
349 349 * is at the same time the indicator for the event source that the event
350 350 * structure/slot is free for reuse.
351 351 *
352 352 * 4. Delivery of events to the application
353 353 * The events structures/slots delivered by event sources remain in the
354 354 * port queue until they are retrieved by the application or the port
355 355 * is closed (exit(2) also closes all opened file descriptors)..
356 356 * The application uses port_get() or port_getn() to retrieve events from
357 357 * a port. port_get() retrieves a single event structure/slot and port_getn()
358 358 * retrieves a list of event structures/slots.
359 359 * Both functions are able to poll for events and return immediately or they
360 360 * can specify a timeout value.
361 361 * Before the events are delivered to the application they are moved to a
362 362 * second temporary internal queue. The idea is to avoid lock collisions or
363 363 * contentions of the global queue lock.
364 364 * The global queue lock is used every time when an event source delivers
365 365 * new events to the port.
366 366 * The port_get() and port_getn() functions
367 367 * a) retrieve single events from the temporary queue,
368 368 * b) prepare the data to be passed to the application memory,
369 369 * c) activate the callback function of the event sources:
370 370 * - to get the latest event data,
371 371 * - the event source can free all allocated resources associated with the
372 372 * current event,
373 373 * - the event source can re-use the current event slot/structure
374 374 * - the event source can deny the delivery of the event to the application
375 375 * (e.g. because of the wrong process).
376 376 * d) put the event back to the temporary queue if the event delivery was denied
377 377 * e) repeat a) until d) as long as there are events in the queue and
378 378 * there is enough user space available.
379 379 *
380 380 * The loop described above could block for a very long time the global mutex,
381 381 * to avoid that a second mutex was introduced to synchronized concurrent
382 382 * threads accessing the temporary queue.
383 383 */
384 384
385 385 static int64_t portfs(int, uintptr_t, uintptr_t, uintptr_t, uintptr_t,
386 386 uintptr_t);
387 387
388 388 static struct sysent port_sysent = {
389 389 6,
390 390 SE_ARGC | SE_64RVAL | SE_NOUNLOAD,
391 391 (int (*)())(uintptr_t)portfs,
392 392 };
393 393
394 394 static struct modlsys modlsys = {
395 395 &mod_syscallops, "event ports", &port_sysent
396 396 };
397 397
398 398 #ifdef _SYSCALL32_IMPL
399 399
400 400 static int64_t
401 401 portfs32(uint32_t arg1, int32_t arg2, uint32_t arg3, uint32_t arg4,
402 402 uint32_t arg5, uint32_t arg6);
403 403
404 404 static struct sysent port_sysent32 = {
405 405 6,
406 406 SE_ARGC | SE_64RVAL | SE_NOUNLOAD,
407 407 (int (*)())(uintptr_t)portfs32,
408 408 };
409 409
410 410 static struct modlsys modlsys32 = {
411 411 &mod_syscallops32,
412 412 "32-bit event ports syscalls",
413 413 &port_sysent32
414 414 };
415 415 #endif /* _SYSCALL32_IMPL */
416 416
417 417 static struct modlinkage modlinkage = {
418 418 MODREV_1,
419 419 &modlsys,
420 420 #ifdef _SYSCALL32_IMPL
421 421 &modlsys32,
422 422 #endif
423 423 NULL
424 424 };
425 425
426 426 port_kstat_t port_kstat = {
427 427 { "ports", KSTAT_DATA_UINT32 }
428 428 };
429 429
430 430 dev_t portdev;
431 431 struct vnodeops *port_vnodeops;
432 432 struct vfs port_vfs;
433 433
434 434 extern rctl_hndl_t rc_process_portev;
435 435 extern rctl_hndl_t rc_project_portids;
436 436 extern void aio_close_port(void *, int, pid_t, int);
437 437
438 438 /*
439 439 * This table contains a list of event sources which need a static
440 440 * association with a port (every port).
441 441 * The last NULL entry in the table is required to detect "end of table".
442 442 */
443 443 struct port_ksource port_ksource_tab[] = {
444 444 {PORT_SOURCE_AIO, aio_close_port, NULL, NULL},
445 445 {0, NULL, NULL, NULL}
446 446 };
447 447
448 448 /* local functions */
449 449 static int port_getn(port_t *, port_event_t *, uint_t, uint_t *,
450 450 port_gettimer_t *);
451 451 static int port_sendn(int [], int [], uint_t, int, void *, uint_t *);
452 452 static int port_alert(port_t *, int, int, void *);
453 453 static int port_dispatch_event(port_t *, int, int, int, uintptr_t, void *);
454 454 static int port_send(port_t *, int, int, void *);
455 455 static int port_create(int *);
456 456 static int port_get_alert(port_alert_t *, port_event_t *);
457 457 static int port_copy_event(port_event_t *, port_kevent_t *, list_t *);
458 458 static int *port_errorn(int *, int, int, int);
459 459 static int port_noshare(void *, int *, pid_t, int, void *);
460 460 static int port_get_timeout(timespec_t *, timespec_t *, timespec_t **, int *,
461 461 int);
462 462 static void port_init(port_t *);
463 463 static void port_remove_alert(port_queue_t *);
464 464 static void port_add_ksource_local(port_t *, port_ksource_t *);
465 465 static void port_check_return_cond(port_queue_t *);
466 466 static void port_dequeue_thread(port_queue_t *, portget_t *);
467 467 static portget_t *port_queue_thread(port_queue_t *, uint_t);
468 468 static void port_kstat_init(void);
469 469
470 470 #ifdef _SYSCALL32_IMPL
471 471 static int port_copy_event32(port_event32_t *, port_kevent_t *, list_t *);
472 472 #endif
473 473
474 474 int
475 475 _init(void)
476 476 {
477 477 static const fs_operation_def_t port_vfsops_template[] = {
478 478 NULL, NULL
479 479 };
480 480 extern const fs_operation_def_t port_vnodeops_template[];
481 481 vfsops_t *port_vfsops;
482 482 int error;
483 483 major_t major;
484 484
485 485 if ((major = getudev()) == (major_t)-1)
486 486 return (ENXIO);
487 487 portdev = makedevice(major, 0);
488 488
489 489 /* Create a dummy vfs */
490 490 error = vfs_makefsops(port_vfsops_template, &port_vfsops);
491 491 if (error) {
492 492 cmn_err(CE_WARN, "port init: bad vfs ops");
493 493 return (error);
494 494 }
495 495 vfs_setops(&port_vfs, port_vfsops);
496 496 port_vfs.vfs_flag = VFS_RDONLY;
497 497 port_vfs.vfs_dev = portdev;
498 498 vfs_make_fsid(&(port_vfs.vfs_fsid), portdev, 0);
499 499
500 500 error = vn_make_ops("portfs", port_vnodeops_template, &port_vnodeops);
501 501 if (error) {
502 502 vfs_freevfsops(port_vfsops);
503 503 cmn_err(CE_WARN, "port init: bad vnode ops");
504 504 return (error);
505 505 }
506 506
507 507 mutex_init(&port_control.pc_mutex, NULL, MUTEX_DEFAULT, NULL);
508 508 port_control.pc_nents = 0; /* number of active ports */
509 509
510 510 /* create kmem_cache for port event structures */
511 511 port_control.pc_cache = kmem_cache_create("port_cache",
512 512 sizeof (port_kevent_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
513 513
514 514 port_kstat_init(); /* init port kstats */
515 515 return (mod_install(&modlinkage));
516 516 }
517 517
518 518 int
519 519 _info(struct modinfo *modinfop)
520 520 {
521 521 return (mod_info(&modlinkage, modinfop));
522 522 }
523 523
524 524 /*
525 525 * System call wrapper for all port related system calls from 32-bit programs.
526 526 */
527 527 #ifdef _SYSCALL32_IMPL
528 528 static int64_t
529 529 portfs32(uint32_t opcode, int32_t a0, uint32_t a1, uint32_t a2, uint32_t a3,
530 530 uint32_t a4)
531 531 {
532 532 int64_t error;
533 533
534 534 switch (opcode & PORT_CODE_MASK) {
535 535 case PORT_GET:
536 536 error = portfs(PORT_GET, a0, a1, (int)a2, (int)a3, a4);
537 537 break;
538 538 case PORT_SENDN:
539 539 error = portfs(opcode, (uint32_t)a0, a1, a2, a3, a4);
540 540 break;
541 541 default:
542 542 error = portfs(opcode, a0, a1, a2, a3, a4);
543 543 break;
544 544 }
545 545 return (error);
546 546 }
547 547 #endif /* _SYSCALL32_IMPL */
548 548
549 549 /*
550 550 * System entry point for port functions.
551 551 * a0 is a port file descriptor (except for PORT_SENDN and PORT_CREATE).
552 552 * The libc uses PORT_SYS_NOPORT in functions which do not deliver a
553 553 * port file descriptor as first argument.
554 554 */
555 555 static int64_t
556 556 portfs(int opcode, uintptr_t a0, uintptr_t a1, uintptr_t a2, uintptr_t a3,
557 557 uintptr_t a4)
558 558 {
559 559 rval_t r;
560 560 port_t *pp;
561 561 int error = 0;
562 562 uint_t nget;
563 563 file_t *fp;
564 564 port_gettimer_t port_timer;
565 565
566 566 r.r_vals = 0;
567 567 if (opcode & PORT_SYS_NOPORT) {
568 568 opcode &= PORT_CODE_MASK;
569 569 if (opcode == PORT_SENDN) {
570 570 error = port_sendn((int *)a0, (int *)a1, (uint_t)a2,
571 571 (int)a3, (void *)a4, (uint_t *)&r.r_val1);
572 572 if (error && (error != EIO))
573 573 return ((int64_t)set_errno(error));
574 574 return (r.r_vals);
575 575 }
576 576
577 577 if (opcode == PORT_CREATE) {
578 578 error = port_create(&r.r_val1);
579 579 if (error)
580 580 return ((int64_t)set_errno(error));
581 581 return (r.r_vals);
582 582 }
583 583 }
584 584
585 585 /* opcodes using port as first argument (a0) */
586 586
587 587 if ((fp = getf((int)a0)) == NULL)
588 588 return ((uintptr_t)set_errno(EBADF));
589 589
590 590 if (fp->f_vnode->v_type != VPORT) {
591 591 releasef((int)a0);
592 592 return ((uintptr_t)set_errno(EBADFD));
593 593 }
594 594
595 595 pp = VTOEP(fp->f_vnode);
596 596
597 597 switch (opcode & PORT_CODE_MASK) {
598 598 case PORT_GET:
599 599 {
600 600 /* see PORT_GETN description */
601 601 struct timespec timeout;
602 602
603 603 port_timer.pgt_flags = PORTGET_ONE;
604 604 port_timer.pgt_loop = 0;
605 605 port_timer.pgt_rqtp = NULL;
606 606 if (a4 != 0) {
607 607 port_timer.pgt_timeout = &timeout;
608 608 timeout.tv_sec = (time_t)a2;
609 609 timeout.tv_nsec = (long)a3;
610 610 } else {
611 611 port_timer.pgt_timeout = NULL;
612 612 }
613 613 do {
614 614 nget = 1;
615 615 error = port_getn(pp, (port_event_t *)a1, 1,
616 616 (uint_t *)&nget, &port_timer);
617 617 } while (nget == 0 && error == 0 && port_timer.pgt_loop);
618 618 break;
619 619 }
620 620 case PORT_GETN:
621 621 {
622 622 /*
623 623 * port_getn() can only retrieve own or shareable events from
624 624 * other processes. The port_getn() function remains in the
625 625 * kernel until own or shareable events are available or the
626 626 * timeout elapses.
627 627 */
628 628 port_timer.pgt_flags = 0;
629 629 port_timer.pgt_loop = 0;
630 630 port_timer.pgt_rqtp = NULL;
631 631 port_timer.pgt_timeout = (struct timespec *)a4;
632 632 do {
633 633 nget = a3;
634 634 error = port_getn(pp, (port_event_t *)a1, (uint_t)a2,
635 635 (uint_t *)&nget, &port_timer);
636 636 } while (nget == 0 && error == 0 && port_timer.pgt_loop);
637 637 r.r_val1 = nget;
638 638 r.r_val2 = error;
639 639 releasef((int)a0);
640 640 if (error && error != ETIME)
641 641 return ((int64_t)set_errno(error));
642 642 return (r.r_vals);
643 643 }
644 644 case PORT_ASSOCIATE:
645 645 {
646 646 switch ((int)a1) {
647 647 case PORT_SOURCE_FD:
648 648 error = port_associate_fd(pp, (int)a1, (uintptr_t)a2,
649 649 (int)a3, (void *)a4);
650 650 break;
651 651 case PORT_SOURCE_FILE:
652 652 error = port_associate_fop(pp, (int)a1, (uintptr_t)a2,
653 653 (int)a3, (void *)a4);
654 654 break;
655 655 default:
656 656 error = EINVAL;
657 657 break;
658 658 }
659 659 break;
660 660 }
661 661 case PORT_SEND:
662 662 {
663 663 /* user-defined events */
664 664 error = port_send(pp, PORT_SOURCE_USER, (int)a1, (void *)a2);
665 665 break;
666 666 }
667 667 case PORT_DISPATCH:
668 668 {
669 669 /*
670 670 * library events, blocking
671 671 * Only events of type PORT_SOURCE_AIO or PORT_SOURCE_MQ
672 672 * are currently allowed.
673 673 */
674 674 if ((int)a1 != PORT_SOURCE_AIO && (int)a1 != PORT_SOURCE_MQ) {
675 675 error = EINVAL;
676 676 break;
677 677 }
678 678 error = port_dispatch_event(pp, (int)opcode, (int)a1, (int)a2,
679 679 (uintptr_t)a3, (void *)a4);
680 680 break;
681 681 }
682 682 case PORT_DISSOCIATE:
683 683 {
684 684 switch ((int)a1) {
685 685 case PORT_SOURCE_FD:
686 686 error = port_dissociate_fd(pp, (uintptr_t)a2);
687 687 break;
688 688 case PORT_SOURCE_FILE:
689 689 error = port_dissociate_fop(pp, (uintptr_t)a2);
690 690 break;
691 691 default:
692 692 error = EINVAL;
693 693 break;
694 694 }
695 695 break;
696 696 }
697 697 case PORT_ALERT:
698 698 {
699 699 if ((int)a2) /* a2 = events */
700 700 error = port_alert(pp, (int)a1, (int)a2, (void *)a3);
701 701 else
702 702 port_remove_alert(&pp->port_queue);
703 703 break;
704 704 }
705 705 default:
706 706 error = EINVAL;
707 707 break;
708 708 }
709 709
710 710 releasef((int)a0);
711 711 if (error)
712 712 return ((int64_t)set_errno(error));
713 713 return (r.r_vals);
714 714 }
715 715
716 716 /*
717 717 * System call to create a port.
718 718 *
719 719 * The port_create() function creates a vnode of type VPORT per port.
720 720 * The port control data is associated with the vnode as vnode private data.
721 721 * The port_create() function returns an event port file descriptor.
722 722 */
723 723 static int
724 724 port_create(int *fdp)
725 725 {
726 726 port_t *pp;
727 727 vnode_t *vp;
728 728 struct file *fp;
729 729 proc_t *p = curproc;
730 730
731 731 /* initialize vnode and port private data */
732 732 pp = kmem_zalloc(sizeof (port_t), KM_SLEEP);
733 733
734 734 pp->port_vnode = vn_alloc(KM_SLEEP);
735 735 vp = EPTOV(pp);
736 736 vn_setops(vp, port_vnodeops);
737 737 vp->v_type = VPORT;
738 738 vp->v_vfsp = &port_vfs;
739 739 vp->v_data = (caddr_t)pp;
740 740
741 741 mutex_enter(&port_control.pc_mutex);
742 742 /*
743 743 * Retrieve the maximal number of event ports allowed per system from
744 744 * the resource control: project.port-max-ids.
745 745 */
746 746 mutex_enter(&p->p_lock);
747 747 if (rctl_test(rc_project_portids, p->p_task->tk_proj->kpj_rctls, p,
748 748 port_control.pc_nents + 1, RCA_SAFE) & RCT_DENY) {
749 749 mutex_exit(&p->p_lock);
750 750 vn_free(vp);
751 751 kmem_free(pp, sizeof (port_t));
752 752 mutex_exit(&port_control.pc_mutex);
753 753 return (EAGAIN);
754 754 }
755 755
756 756 /*
757 757 * Retrieve the maximal number of events allowed per port from
758 758 * the resource control: process.port-max-events.
759 759 */
760 760 pp->port_max_events = rctl_enforced_value(rc_process_portev,
761 761 p->p_rctls, p);
762 762 mutex_exit(&p->p_lock);
763 763
764 764 /* allocate a new user file descriptor and a file structure */
765 765 if (falloc(vp, 0, &fp, fdp)) {
766 766 /*
767 767 * If the file table is full, free allocated resources.
768 768 */
769 769 vn_free(vp);
770 770 kmem_free(pp, sizeof (port_t));
771 771 mutex_exit(&port_control.pc_mutex);
772 772 return (EMFILE);
773 773 }
774 774
775 775 mutex_exit(&fp->f_tlock);
776 776
777 777 pp->port_fd = *fdp;
778 778 port_control.pc_nents++;
779 779 p->p_portcnt++;
780 780 port_kstat.pks_ports.value.ui32++;
781 781 mutex_exit(&port_control.pc_mutex);
782 782
783 783 /* initializes port private data */
784 784 port_init(pp);
785 785 /* set user file pointer */
786 786 setf(*fdp, fp);
787 787 return (0);
788 788 }
789 789
790 790 /*
791 791 * port_init() initializes event port specific data
792 792 */
793 793 static void
794 794 port_init(port_t *pp)
795 795 {
796 796 port_queue_t *portq;
797 797 port_ksource_t *pks;
798 798
799 799 mutex_init(&pp->port_mutex, NULL, MUTEX_DEFAULT, NULL);
800 800 portq = &pp->port_queue;
801 801 mutex_init(&portq->portq_mutex, NULL, MUTEX_DEFAULT, NULL);
802 802 pp->port_flags |= PORT_INIT;
803 803
804 804 /*
805 805 * If it is not enough memory available to satisfy a user
806 806 * request using a single port_getn() call then port_getn()
807 807 * will reduce the size of the list to PORT_MAX_LIST.
808 808 */
809 809 pp->port_max_list = port_max_list;
810 810
811 811 /* Set timestamp entries required for fstat(2) requests */
812 812 gethrestime(&pp->port_ctime);
813 813 pp->port_uid = crgetuid(curproc->p_cred);
814 814 pp->port_gid = crgetgid(curproc->p_cred);
815 815
816 816 /* initialize port queue structs */
817 817 list_create(&portq->portq_list, sizeof (port_kevent_t),
818 818 offsetof(port_kevent_t, portkev_node));
819 819 list_create(&portq->portq_get_list, sizeof (port_kevent_t),
820 820 offsetof(port_kevent_t, portkev_node));
821 821 portq->portq_flags = 0;
822 822 pp->port_pid = curproc->p_pid;
823 823
824 824 /* Allocate cache skeleton for PORT_SOURCE_FD events */
825 825 portq->portq_pcp = kmem_zalloc(sizeof (port_fdcache_t), KM_SLEEP);
826 826 mutex_init(&portq->portq_pcp->pc_lock, NULL, MUTEX_DEFAULT, NULL);
827 827
828 828 /*
829 829 * Allocate cache skeleton for association of event sources.
830 830 */
831 831 mutex_init(&portq->portq_source_mutex, NULL, MUTEX_DEFAULT, NULL);
832 832 portq->portq_scache = kmem_zalloc(
833 833 PORT_SCACHE_SIZE * sizeof (port_source_t *), KM_SLEEP);
834 834
835 835 /*
836 836 * pre-associate some kernel sources with this port.
837 837 * The pre-association is required to create port_source_t
838 838 * structures for object association.
839 839 * Some sources can not get associated with a port before the first
840 840 * object association is requested. Another reason to pre_associate
841 841 * a particular source with a port is because of performance.
842 842 */
843 843
844 844 for (pks = port_ksource_tab; pks->pks_source != 0; pks++)
845 845 port_add_ksource_local(pp, pks);
846 846 }
847 847
848 848 /*
849 849 * The port_add_ksource_local() function is being used to associate
850 850 * event sources with every new port.
851 851 * The event sources need to be added to port_ksource_tab[].
852 852 */
853 853 static void
854 854 port_add_ksource_local(port_t *pp, port_ksource_t *pks)
855 855 {
856 856 port_source_t *pse;
857 857 port_source_t **ps;
858 858
859 859 mutex_enter(&pp->port_queue.portq_source_mutex);
860 860 ps = &pp->port_queue.portq_scache[PORT_SHASH(pks->pks_source)];
861 861 for (pse = *ps; pse != NULL; pse = pse->portsrc_next) {
862 862 if (pse->portsrc_source == pks->pks_source)
863 863 break;
864 864 }
865 865
866 866 if (pse == NULL) {
867 867 /* associate new source with the port */
868 868 pse = kmem_zalloc(sizeof (port_source_t), KM_SLEEP);
869 869 pse->portsrc_source = pks->pks_source;
870 870 pse->portsrc_close = pks->pks_close;
871 871 pse->portsrc_closearg = pks->pks_closearg;
872 872 pse->portsrc_cnt = 1;
873 873
874 874 pks->pks_portsrc = pse;
875 875 if (*ps != NULL)
876 876 pse->portsrc_next = (*ps)->portsrc_next;
877 877 *ps = pse;
878 878 }
879 879 mutex_exit(&pp->port_queue.portq_source_mutex);
880 880 }
881 881
882 882 /*
883 883 * The port_send() function sends an event of type "source" to a
884 884 * port. This function is non-blocking. An event can be sent to
885 885 * a port as long as the number of events per port does not achieve the
886 886 * maximal allowed number of events. The max. number of events per port is
887 887 * defined by the resource control process.max-port-events.
888 888 * This function is used by the port library function port_send()
889 889 * and port_dispatch(). The port_send(3c) function is part of the
890 890 * event ports API and submits events of type PORT_SOURCE_USER. The
891 891 * port_dispatch() function is project private and it is used by library
892 892 * functions to submit events of other types than PORT_SOURCE_USER
893 893 * (e.g. PORT_SOURCE_AIO).
894 894 */
895 895 static int
896 896 port_send(port_t *pp, int source, int events, void *user)
897 897 {
898 898 port_kevent_t *pev;
899 899 int error;
900 900
901 901 error = port_alloc_event_local(pp, source, PORT_ALLOC_DEFAULT, &pev);
902 902 if (error)
903 903 return (error);
904 904
905 905 pev->portkev_object = 0;
906 906 pev->portkev_events = events;
907 907 pev->portkev_user = user;
908 908 pev->portkev_callback = NULL;
909 909 pev->portkev_arg = NULL;
910 910 pev->portkev_flags = 0;
911 911
912 912 port_send_event(pev);
913 913 return (0);
914 914 }
915 915
916 916 /*
917 917 * The port_noshare() function returns 0 if the current event was generated
918 918 * by the same process. Otherwise is returns a value other than 0 and the
919 919 * event should not be delivered to the current processe.
920 920 * The port_noshare() function is normally used by the port_dispatch()
921 921 * function. The port_dispatch() function is project private and can only be
922 922 * used within the event port project.
923 923 * Currently the libaio uses the port_dispatch() function to deliver events
924 924 * of types PORT_SOURCE_AIO.
925 925 */
926 926 /* ARGSUSED */
927 927 static int
928 928 port_noshare(void *arg, int *events, pid_t pid, int flag, void *evp)
929 929 {
930 930 if (flag == PORT_CALLBACK_DEFAULT && curproc->p_pid != pid)
931 931 return (1);
932 932 return (0);
933 933 }
934 934
935 935 /*
936 936 * The port_dispatch_event() function is project private and it is used by
937 937 * libraries involved in the project to deliver events to the port.
938 938 * port_dispatch will sleep and wait for enough resources to satisfy the
939 939 * request, if necessary.
940 940 * The library can specify if the delivered event is shareable with other
941 941 * processes (see PORT_SYS_NOSHARE flag).
942 942 */
943 943 static int
944 944 port_dispatch_event(port_t *pp, int opcode, int source, int events,
945 945 uintptr_t object, void *user)
946 946 {
947 947 port_kevent_t *pev;
948 948 int error;
949 949
950 950 error = port_alloc_event_block(pp, source, PORT_ALLOC_DEFAULT, &pev);
951 951 if (error)
952 952 return (error);
953 953
954 954 pev->portkev_object = object;
955 955 pev->portkev_events = events;
956 956 pev->portkev_user = user;
957 957 pev->portkev_arg = NULL;
958 958 if (opcode & PORT_SYS_NOSHARE) {
959 959 pev->portkev_flags = PORT_KEV_NOSHARE;
960 960 pev->portkev_callback = port_noshare;
961 961 } else {
962 962 pev->portkev_flags = 0;
963 963 pev->portkev_callback = NULL;
964 964 }
965 965
966 966 port_send_event(pev);
967 967 return (0);
968 968 }
969 969
970 970
971 971 /*
972 972 * The port_sendn() function is the kernel implementation of the event
973 973 * port API function port_sendn(3c).
974 974 * This function is able to send an event to a list of event ports.
975 975 */
976 976 static int
977 977 port_sendn(int ports[], int errors[], uint_t nent, int events, void *user,
978 978 uint_t *nget)
979 979 {
980 980 port_kevent_t *pev;
981 981 int errorcnt = 0;
982 982 int error = 0;
983 983 int count;
984 984 int port;
985 985 int *plist;
986 986 int *elist = NULL;
987 987 file_t *fp;
988 988 port_t *pp;
989 989
990 990 if (nent == 0 || nent > port_max_list)
991 991 return (EINVAL);
992 992
993 993 plist = kmem_alloc(nent * sizeof (int), KM_SLEEP);
994 994 if (copyin((void *)ports, plist, nent * sizeof (int))) {
995 995 kmem_free(plist, nent * sizeof (int));
996 996 return (EFAULT);
997 997 }
998 998
999 999 /*
1000 1000 * Scan the list for event port file descriptors and send the
1001 1001 * attached user event data embedded in a event of type
1002 1002 * PORT_SOURCE_USER to every event port in the list.
1003 1003 * If a list entry is not a valid event port then the corresponding
1004 1004 * error code will be stored in the errors[] list with the same
1005 1005 * list offset as in the ports[] list.
1006 1006 */
1007 1007
1008 1008 for (count = 0; count < nent; count++) {
1009 1009 port = plist[count];
1010 1010 if ((fp = getf(port)) == NULL) {
1011 1011 elist = port_errorn(elist, nent, EBADF, count);
1012 1012 errorcnt++;
1013 1013 continue;
1014 1014 }
1015 1015
1016 1016 pp = VTOEP(fp->f_vnode);
1017 1017 if (fp->f_vnode->v_type != VPORT) {
1018 1018 releasef(port);
1019 1019 elist = port_errorn(elist, nent, EBADFD, count);
1020 1020 errorcnt++;
1021 1021 continue;
1022 1022 }
1023 1023
1024 1024 error = port_alloc_event_local(pp, PORT_SOURCE_USER,
1025 1025 PORT_ALLOC_DEFAULT, &pev);
1026 1026 if (error) {
1027 1027 releasef(port);
1028 1028 elist = port_errorn(elist, nent, error, count);
1029 1029 errorcnt++;
1030 1030 continue;
1031 1031 }
1032 1032
1033 1033 pev->portkev_object = 0;
1034 1034 pev->portkev_events = events;
1035 1035 pev->portkev_user = user;
1036 1036 pev->portkev_callback = NULL;
1037 1037 pev->portkev_arg = NULL;
1038 1038 pev->portkev_flags = 0;
1039 1039
1040 1040 port_send_event(pev);
1041 1041 releasef(port);
1042 1042 }
1043 1043 if (errorcnt) {
1044 1044 error = EIO;
1045 1045 if (copyout(elist, (void *)errors, nent * sizeof (int)))
1046 1046 error = EFAULT;
1047 1047 kmem_free(elist, nent * sizeof (int));
1048 1048 }
1049 1049 *nget = nent - errorcnt;
1050 1050 kmem_free(plist, nent * sizeof (int));
1051 1051 return (error);
1052 1052 }
1053 1053
1054 1054 static int *
1055 1055 port_errorn(int *elist, int nent, int error, int index)
1056 1056 {
1057 1057 if (elist == NULL)
1058 1058 elist = kmem_zalloc(nent * sizeof (int), KM_SLEEP);
1059 1059 elist[index] = error;
1060 1060 return (elist);
1061 1061 }
1062 1062
1063 1063 /*
1064 1064 * port_alert()
1065 1065 * The port_alert() funcion is a high priority event and it is always set
1066 1066 * on top of the queue. It is also delivered as single event.
1067 1067 * flags:
1068 1068 * - SET :overwrite current alert data
1069 1069 * - UPDATE:set alert data or return EBUSY if alert mode is already set
1070 1070 *
1071 1071 * - set the ALERT flag
1072 1072 * - wakeup all sleeping threads
1073 1073 */
1074 1074 static int
1075 1075 port_alert(port_t *pp, int flags, int events, void *user)
1076 1076 {
1077 1077 port_queue_t *portq;
1078 1078 portget_t *pgetp;
1079 1079 port_alert_t *pa;
1080 1080
1081 1081 if ((flags & PORT_ALERT_INVALID) == PORT_ALERT_INVALID)
1082 1082 return (EINVAL);
1083 1083
1084 1084 portq = &pp->port_queue;
1085 1085 pa = &portq->portq_alert;
1086 1086 mutex_enter(&portq->portq_mutex);
1087 1087
1088 1088 /* check alert conditions */
1089 1089 if (flags == PORT_ALERT_UPDATE) {
1090 1090 if (portq->portq_flags & PORTQ_ALERT) {
1091 1091 mutex_exit(&portq->portq_mutex);
1092 1092 return (EBUSY);
1093 1093 }
1094 1094 }
1095 1095
1096 1096 /*
1097 1097 * Store alert data in the port to be delivered to threads
1098 1098 * which are using port_get(n) to retrieve events.
1099 1099 */
1100 1100
1101 1101 portq->portq_flags |= PORTQ_ALERT;
1102 1102 pa->portal_events = events; /* alert info */
1103 1103 pa->portal_pid = curproc->p_pid; /* process owner */
1104 1104 pa->portal_object = 0; /* no object */
1105 1105 pa->portal_user = user; /* user alert data */
1106 1106
1107 1107 /* alert and deliver alert data to waiting threads */
1108 1108 pgetp = portq->portq_thread;
1109 1109 if (pgetp == NULL) {
1110 1110 /* no threads waiting for events */
1111 1111 mutex_exit(&portq->portq_mutex);
1112 1112 return (0);
1113 1113 }
1114 1114
1115 1115 /*
1116 1116 * Set waiting threads in alert mode (PORTGET_ALERT)..
1117 1117 * Every thread waiting for events already allocated a portget_t
1118 1118 * structure to sleep on.
1119 1119 * The port alert arguments are stored in the portget_t structure.
1120 1120 * The PORTGET_ALERT flag is set to indicate the thread to return
1121 1121 * immediately with the alert event.
1122 1122 */
1123 1123 do {
1124 1124 if ((pgetp->portget_state & PORTGET_ALERT) == 0) {
1125 1125 pa = &pgetp->portget_alert;
1126 1126 pa->portal_events = events;
1127 1127 pa->portal_object = 0;
1128 1128 pa->portal_user = user;
1129 1129 pgetp->portget_state |= PORTGET_ALERT;
1130 1130 cv_signal(&pgetp->portget_cv);
1131 1131 }
1132 1132 } while ((pgetp = pgetp->portget_next) != portq->portq_thread);
1133 1133 mutex_exit(&portq->portq_mutex);
1134 1134 return (0);
1135 1135 }
1136 1136
1137 1137 /*
1138 1138 * Clear alert state of the port
1139 1139 */
1140 1140 static void
1141 1141 port_remove_alert(port_queue_t *portq)
1142 1142 {
1143 1143 mutex_enter(&portq->portq_mutex);
1144 1144 portq->portq_flags &= ~PORTQ_ALERT;
1145 1145 mutex_exit(&portq->portq_mutex);
1146 1146 }
1147 1147
1148 1148 /*
1149 1149 * The port_getn() function is used to retrieve events from a port.
1150 1150 *
1151 1151 * The port_getn() function returns immediately if there are enough events
1152 1152 * available in the port to satisfy the request or if the port is in alert
1153 1153 * mode (see port_alert(3c)).
1154 1154 * The timeout argument of port_getn(3c) -which is embedded in the
1155 1155 * port_gettimer_t structure- specifies if the system call should block or if it
1156 1156 * should return immediately depending on the number of events available.
1157 1157 * This function is internally used by port_getn(3c) as well as by
1158 1158 * port_get(3c).
1159 1159 */
1160 1160 static int
1161 1161 port_getn(port_t *pp, port_event_t *uevp, uint_t max, uint_t *nget,
1162 1162 port_gettimer_t *pgt)
1163 1163 {
1164 1164 port_queue_t *portq;
1165 1165 port_kevent_t *pev;
1166 1166 port_kevent_t *lev;
1167 1167 int error = 0;
1168 1168 uint_t nmax;
1169 1169 uint_t nevents;
1170 1170 uint_t eventsz;
1171 1171 port_event_t *kevp;
1172 1172 list_t *glist;
1173 1173 uint_t tnent;
1174 1174 int rval;
1175 1175 int blocking = -1;
1176 1176 int timecheck;
1177 1177 int flag;
1178 1178 timespec_t rqtime;
1179 1179 timespec_t *rqtp = NULL;
1180 1180 portget_t *pgetp;
1181 1181 void *results;
1182 1182 model_t model = get_udatamodel();
1183 1183
1184 1184 flag = pgt->pgt_flags;
1185 1185
1186 1186 if (*nget > max && max > 0)
1187 1187 return (EINVAL);
1188 1188
1189 1189 portq = &pp->port_queue;
1190 1190 mutex_enter(&portq->portq_mutex);
1191 1191 if (max == 0) {
1192 1192 /*
1193 1193 * Return number of objects with events.
1194 1194 * The port_block() call is required to synchronize this
1195 1195 * thread with another possible thread, which could be
1196 1196 * retrieving events from the port queue.
1197 1197 */
1198 1198 port_block(portq);
1199 1199 /*
1200 1200 * Check if a second thread is currently retrieving events
1201 1201 * and it is using the temporary event queue.
1202 1202 */
1203 1203 if (portq->portq_tnent) {
1204 1204 /* put remaining events back to the port queue */
1205 1205 port_push_eventq(portq);
1206 1206 }
1207 1207 *nget = portq->portq_nent;
1208 1208 port_unblock(portq);
1209 1209 mutex_exit(&portq->portq_mutex);
1210 1210 return (0);
1211 1211 }
1212 1212
1213 1213 if (uevp == NULL) {
1214 1214 mutex_exit(&portq->portq_mutex);
1215 1215 return (EFAULT);
1216 1216 }
1217 1217 if (*nget == 0) { /* no events required */
1218 1218 mutex_exit(&portq->portq_mutex);
1219 1219 return (0);
1220 1220 }
1221 1221
1222 1222 /* port is being closed ... */
1223 1223 if (portq->portq_flags & PORTQ_CLOSE) {
1224 1224 mutex_exit(&portq->portq_mutex);
1225 1225 return (EBADFD);
1226 1226 }
1227 1227
1228 1228 /* return immediately if port in alert mode */
1229 1229 if (portq->portq_flags & PORTQ_ALERT) {
1230 1230 error = port_get_alert(&portq->portq_alert, uevp);
1231 1231 if (error == 0)
1232 1232 *nget = 1;
1233 1233 mutex_exit(&portq->portq_mutex);
1234 1234 return (error);
1235 1235 }
1236 1236
1237 1237 portq->portq_thrcnt++;
1238 1238
1239 1239 /*
1240 1240 * Now check if the completed events satisfy the
1241 1241 * "wait" requirements of the current thread:
1242 1242 */
1243 1243
1244 1244 if (pgt->pgt_loop) {
1245 1245 /*
1246 1246 * loop entry of same thread
1247 1247 * pgt_loop is set when the current thread returns
1248 1248 * prematurely from this function. That could happen
1249 1249 * when a port is being shared between processes and
1250 1250 * this thread could not find events to return.
1251 1251 * It is not allowed to a thread to retrieve non-shareable
1252 1252 * events generated in other processes.
1253 1253 * PORTQ_WAIT_EVENTS is set when a thread already
1254 1254 * checked the current event queue and no new events
1255 1255 * are added to the queue.
1256 1256 */
1257 1257 if (((portq->portq_flags & PORTQ_WAIT_EVENTS) == 0) &&
1258 1258 (portq->portq_nent >= *nget)) {
1259 1259 /* some new events arrived ...check them */
1260 1260 goto portnowait;
1261 1261 }
1262 1262 rqtp = pgt->pgt_rqtp;
1263 1263 timecheck = pgt->pgt_timecheck;
1264 1264 pgt->pgt_flags |= PORTGET_WAIT_EVENTS;
1265 1265 } else {
1266 1266 /* check if enough events are available ... */
1267 1267 if (portq->portq_nent >= *nget)
1268 1268 goto portnowait;
1269 1269 /*
1270 1270 * There are not enough events available to satisfy
1271 1271 * the request, check timeout value and wait for
1272 1272 * incoming events.
1273 1273 */
1274 1274 error = port_get_timeout(pgt->pgt_timeout, &rqtime, &rqtp,
1275 1275 &blocking, flag);
1276 1276 if (error) {
1277 1277 port_check_return_cond(portq);
1278 1278 mutex_exit(&portq->portq_mutex);
1279 1279 return (error);
1280 1280 }
1281 1281
1282 1282 if (blocking == 0) /* don't block, check fired events */
1283 1283 goto portnowait;
1284 1284
1285 1285 if (rqtp != NULL) {
1286 1286 timespec_t now;
1287 1287 timecheck = timechanged;
1288 1288 gethrestime(&now);
1289 1289 timespecadd(rqtp, &now);
1290 1290 }
1291 1291 }
1292 1292
1293 1293 /* enqueue thread in the list of waiting threads */
1294 1294 pgetp = port_queue_thread(portq, *nget);
1295 1295
1296 1296
1297 1297 /* Wait here until return conditions met */
1298 1298 for (;;) {
1299 1299 if (pgetp->portget_state & PORTGET_ALERT) {
1300 1300 /* reap alert event and return */
1301 1301 error = port_get_alert(&pgetp->portget_alert, uevp);
1302 1302 if (error)
1303 1303 *nget = 0;
1304 1304 else
1305 1305 *nget = 1;
1306 1306 port_dequeue_thread(&pp->port_queue, pgetp);
1307 1307 portq->portq_thrcnt--;
1308 1308 mutex_exit(&portq->portq_mutex);
1309 1309 return (error);
1310 1310 }
1311 1311
1312 1312 /*
1313 1313 * Check if some other thread is already retrieving
1314 1314 * events (portq_getn > 0).
1315 1315 */
1316 1316
1317 1317 if ((portq->portq_getn == 0) &&
1318 1318 ((portq)->portq_nent >= *nget) &&
1319 1319 (!((pgt)->pgt_flags & PORTGET_WAIT_EVENTS) ||
1320 1320 !((portq)->portq_flags & PORTQ_WAIT_EVENTS)))
1321 1321 break;
1322 1322
1323 1323 if (portq->portq_flags & PORTQ_CLOSE) {
1324 1324 error = EBADFD;
1325 1325 break;
1326 1326 }
1327 1327
1328 1328 rval = cv_waituntil_sig(&pgetp->portget_cv, &portq->portq_mutex,
1329 1329 rqtp, timecheck);
1330 1330
1331 1331 if (rval <= 0) {
1332 1332 error = (rval == 0) ? EINTR : ETIME;
1333 1333 break;
1334 1334 }
1335 1335 }
1336 1336
1337 1337 /* take thread out of the wait queue */
1338 1338 port_dequeue_thread(portq, pgetp);
1339 1339
1340 1340 if (error != 0 && (error == EINTR || error == EBADFD ||
1341 1341 (error == ETIME && flag))) {
1342 1342 /* return without events */
1343 1343 port_check_return_cond(portq);
1344 1344 mutex_exit(&portq->portq_mutex);
1345 1345 return (error);
1346 1346 }
1347 1347
1348 1348 portnowait:
1349 1349 /*
1350 1350 * Move port event queue to a temporary event queue .
1351 1351 * New incoming events will be continue be posted to the event queue
1352 1352 * and they will not be considered by the current thread.
1353 1353 * The idea is to avoid lock contentions or an often locking/unlocking
1354 1354 * of the port queue mutex. The contention and performance degradation
1355 1355 * could happen because:
1356 1356 * a) incoming events use the port queue mutex to enqueue new events and
1357 1357 * b) before the event can be delivered to the application it is
1358 1358 * necessary to notify the event sources about the event delivery.
1359 1359 * Sometimes the event sources can require a long time to return and
1360 1360 * the queue mutex would block incoming events.
1361 1361 * During this time incoming events (port_send_event()) do not need
1362 1362 * to awake threads waiting for events. Before the current thread
1363 1363 * returns it will check the conditions to awake other waiting threads.
1364 1364 */
1365 1365 portq->portq_getn++; /* number of threads retrieving events */
1366 1366 port_block(portq); /* block other threads here */
1367 1367 nmax = max < portq->portq_nent ? max : portq->portq_nent;
1368 1368
1369 1369 if (portq->portq_tnent) {
1370 1370 /*
1371 1371 * Move remaining events from previous thread back to the
1372 1372 * port event queue.
1373 1373 */
1374 1374 port_push_eventq(portq);
1375 1375 }
1376 1376 /* move port event queue to a temporary queue */
1377 1377 list_move_tail(&portq->portq_get_list, &portq->portq_list);
1378 1378 glist = &portq->portq_get_list; /* use temporary event queue */
1379 1379 tnent = portq->portq_nent; /* get current number of events */
1380 1380 portq->portq_nent = 0; /* no events in the port event queue */
1381 1381 portq->portq_flags |= PORTQ_WAIT_EVENTS; /* detect incoming events */
1382 1382 mutex_exit(&portq->portq_mutex); /* event queue can be reused now */
1383 1383
1384 1384 if (model == DATAMODEL_NATIVE) {
1385 1385 eventsz = sizeof (port_event_t);
1386 1386
1387 1387 if (nmax == 0) {
1388 1388 kevp = NULL;
1389 1389 } else {
1390 1390 kevp = kmem_alloc(eventsz * nmax, KM_NOSLEEP);
1391 1391 if (kevp == NULL) {
1392 1392 if (nmax > pp->port_max_list)
1393 1393 nmax = pp->port_max_list;
1394 1394 kevp = kmem_alloc(eventsz * nmax, KM_SLEEP);
1395 1395 }
1396 1396 }
1397 1397
1398 1398 results = kevp;
|
↓ open down ↓ |
1360 lines elided |
↑ open up ↑ |
1399 1399 lev = NULL; /* start with first event in the queue */
1400 1400 for (nevents = 0; nevents < nmax; ) {
1401 1401 pev = port_get_kevent(glist, lev);
1402 1402 if (pev == NULL) /* no more events available */
1403 1403 break;
1404 1404 if (pev->portkev_flags & PORT_KEV_FREE) {
1405 1405 /* Just discard event */
1406 1406 list_remove(glist, pev);
1407 1407 pev->portkev_flags &= ~(PORT_CLEANUP_DONE);
1408 1408 if (PORT_FREE_EVENT(pev))
1409 - port_free_event_local(pev, 0);
1409 + port_free_event_local(pev, B_TRUE);
1410 1410 tnent--;
1411 1411 continue;
1412 1412 }
1413 1413
1414 1414 /* move event data to copyout list */
1415 1415 if (port_copy_event(&kevp[nevents], pev, glist)) {
1416 1416 /*
1417 1417 * Event can not be delivered to the
1418 1418 * current process.
1419 1419 */
1420 1420 if (lev != NULL)
1421 1421 list_insert_after(glist, lev, pev);
1422 1422 else
1423 1423 list_insert_head(glist, pev);
1424 1424 lev = pev; /* last checked event */
1425 1425 } else {
1426 1426 nevents++; /* # of events ready */
1427 1427 }
1428 1428 }
1429 1429 #ifdef _SYSCALL32_IMPL
1430 1430 } else {
1431 1431 port_event32_t *kevp32;
1432 1432
1433 1433 eventsz = sizeof (port_event32_t);
1434 1434
1435 1435 if (nmax == 0) {
1436 1436 kevp32 = NULL;
1437 1437 } else {
1438 1438 kevp32 = kmem_alloc(eventsz * nmax, KM_NOSLEEP);
1439 1439 if (kevp32 == NULL) {
1440 1440 if (nmax > pp->port_max_list)
1441 1441 nmax = pp->port_max_list;
1442 1442 kevp32 = kmem_alloc(eventsz * nmax, KM_SLEEP);
1443 1443 }
1444 1444 }
1445 1445
1446 1446 results = kevp32;
|
↓ open down ↓ |
27 lines elided |
↑ open up ↑ |
1447 1447 lev = NULL; /* start with first event in the queue */
1448 1448 for (nevents = 0; nevents < nmax; ) {
1449 1449 pev = port_get_kevent(glist, lev);
1450 1450 if (pev == NULL) /* no more events available */
1451 1451 break;
1452 1452 if (pev->portkev_flags & PORT_KEV_FREE) {
1453 1453 /* Just discard event */
1454 1454 list_remove(glist, pev);
1455 1455 pev->portkev_flags &= ~(PORT_CLEANUP_DONE);
1456 1456 if (PORT_FREE_EVENT(pev))
1457 - port_free_event_local(pev, 0);
1457 + port_free_event_local(pev, B_TRUE);
1458 1458 tnent--;
1459 1459 continue;
1460 1460 }
1461 1461
1462 1462 /* move event data to copyout list */
1463 1463 if (port_copy_event32(&kevp32[nevents], pev, glist)) {
1464 1464 /*
1465 1465 * Event can not be delivered to the
1466 1466 * current process.
1467 1467 */
1468 1468 if (lev != NULL)
1469 1469 list_insert_after(glist, lev, pev);
1470 1470 else
1471 1471 list_insert_head(glist, pev);
1472 1472 lev = pev; /* last checked event */
1473 1473 } else {
1474 1474 nevents++; /* # of events ready */
1475 1475 }
1476 1476 }
1477 1477 #endif /* _SYSCALL32_IMPL */
1478 1478 }
1479 1479
1480 1480 /*
1481 1481 * Remember number of remaining events in the temporary event queue.
1482 1482 */
1483 1483 portq->portq_tnent = tnent - nevents;
1484 1484
1485 1485 /*
1486 1486 * Work to do before return :
1487 1487 * - push list of remaining events back to the top of the standard
1488 1488 * port queue.
1489 1489 * - if this is the last thread calling port_get(n) then wakeup the
1490 1490 * thread waiting on close(2).
1491 1491 * - check for a deferred cv_signal from port_send_event() and wakeup
1492 1492 * the sleeping thread.
1493 1493 */
1494 1494
1495 1495 mutex_enter(&portq->portq_mutex);
1496 1496 port_unblock(portq);
1497 1497 if (portq->portq_tnent) {
1498 1498 /*
1499 1499 * move remaining events in the temporary event queue back
1500 1500 * to the port event queue
1501 1501 */
1502 1502 port_push_eventq(portq);
1503 1503 }
1504 1504 portq->portq_getn--; /* update # of threads retrieving events */
1505 1505 if (--portq->portq_thrcnt == 0) { /* # of threads waiting ... */
1506 1506 /* Last thread => check close(2) conditions ... */
1507 1507 if (portq->portq_flags & PORTQ_CLOSE) {
1508 1508 cv_signal(&portq->portq_closecv);
1509 1509 mutex_exit(&portq->portq_mutex);
1510 1510 kmem_free(results, eventsz * nmax);
1511 1511 /* do not copyout events */
1512 1512 *nget = 0;
1513 1513 return (EBADFD);
1514 1514 }
1515 1515 } else if (portq->portq_getn == 0) {
1516 1516 /*
1517 1517 * no other threads retrieving events ...
1518 1518 * check wakeup conditions of sleeping threads
1519 1519 */
1520 1520 if ((portq->portq_thread != NULL) &&
1521 1521 (portq->portq_nent >= portq->portq_nget))
1522 1522 cv_signal(&portq->portq_thread->portget_cv);
1523 1523 }
1524 1524
1525 1525 /*
1526 1526 * Check PORTQ_POLLIN here because the current thread set temporarily
1527 1527 * the number of events in the queue to zero.
1528 1528 */
1529 1529 if (portq->portq_flags & PORTQ_POLLIN) {
1530 1530 portq->portq_flags &= ~PORTQ_POLLIN;
1531 1531 mutex_exit(&portq->portq_mutex);
1532 1532 pollwakeup(&pp->port_pollhd, POLLIN);
1533 1533 } else {
1534 1534 mutex_exit(&portq->portq_mutex);
1535 1535 }
1536 1536
1537 1537 /* now copyout list of user event structures to user space */
1538 1538 if (nevents) {
1539 1539 if (copyout(results, uevp, nevents * eventsz))
1540 1540 error = EFAULT;
1541 1541 }
1542 1542 kmem_free(results, eventsz * nmax);
1543 1543
1544 1544 if (nevents == 0 && error == 0 && pgt->pgt_loop == 0 && blocking != 0) {
1545 1545 /* no events retrieved: check loop conditions */
1546 1546 if (blocking == -1) {
1547 1547 /* no timeout checked */
1548 1548 error = port_get_timeout(pgt->pgt_timeout,
1549 1549 &pgt->pgt_rqtime, &rqtp, &blocking, flag);
1550 1550 if (error) {
1551 1551 *nget = nevents;
1552 1552 return (error);
1553 1553 }
1554 1554 if (rqtp != NULL) {
1555 1555 timespec_t now;
1556 1556 pgt->pgt_timecheck = timechanged;
1557 1557 gethrestime(&now);
1558 1558 timespecadd(&pgt->pgt_rqtime, &now);
1559 1559 }
1560 1560 pgt->pgt_rqtp = rqtp;
1561 1561 } else {
1562 1562 /* timeout already checked -> remember values */
1563 1563 pgt->pgt_rqtp = rqtp;
1564 1564 if (rqtp != NULL) {
1565 1565 pgt->pgt_timecheck = timecheck;
1566 1566 pgt->pgt_rqtime = *rqtp;
1567 1567 }
1568 1568 }
1569 1569 if (blocking)
1570 1570 /* timeout remaining */
1571 1571 pgt->pgt_loop = 1;
1572 1572 }
1573 1573
1574 1574 /* set number of user event structures completed */
1575 1575 *nget = nevents;
1576 1576 return (error);
1577 1577 }
1578 1578
1579 1579 /*
1580 1580 * 1. copy kernel event structure to user event structure.
1581 1581 * 2. PORT_KEV_WIRED event structures will be reused by the "source"
1582 1582 * 3. Remove PORT_KEV_DONEQ flag (event removed from the event queue)
1583 1583 * 4. Other types of event structures can be delivered back to the port cache
1584 1584 * (port_free_event_local()).
1585 1585 * 5. The event source callback function is the last opportunity for the
1586 1586 * event source to update events, to free local resources associated with
1587 1587 * the event or to deny the delivery of the event.
1588 1588 */
1589 1589 static int
1590 1590 port_copy_event(port_event_t *puevp, port_kevent_t *pkevp, list_t *list)
1591 1591 {
1592 1592 int free_event = 0;
1593 1593 int flags;
1594 1594 int error;
1595 1595
1596 1596 puevp->portev_source = pkevp->portkev_source;
1597 1597 puevp->portev_object = pkevp->portkev_object;
1598 1598 puevp->portev_user = pkevp->portkev_user;
1599 1599 puevp->portev_events = pkevp->portkev_events;
1600 1600
1601 1601 /* remove event from the queue */
1602 1602 list_remove(list, pkevp);
1603 1603
1604 1604 /*
1605 1605 * Events of type PORT_KEV_WIRED remain allocated by the
1606 1606 * event source.
1607 1607 */
1608 1608 flags = pkevp->portkev_flags;
1609 1609 if (pkevp->portkev_flags & PORT_KEV_WIRED)
1610 1610 pkevp->portkev_flags &= ~PORT_KEV_DONEQ;
1611 1611 else
1612 1612 free_event = 1;
1613 1613
1614 1614 if (pkevp->portkev_callback) {
1615 1615 error = (*pkevp->portkev_callback)(pkevp->portkev_arg,
1616 1616 &puevp->portev_events, pkevp->portkev_pid,
1617 1617 PORT_CALLBACK_DEFAULT, pkevp);
1618 1618
|
↓ open down ↓ |
151 lines elided |
↑ open up ↑ |
1619 1619 if (error) {
1620 1620 /*
1621 1621 * Event can not be delivered.
1622 1622 * Caller must reinsert the event into the queue.
1623 1623 */
1624 1624 pkevp->portkev_flags = flags;
1625 1625 return (error);
1626 1626 }
1627 1627 }
1628 1628 if (free_event)
1629 - port_free_event_local(pkevp, 0);
1629 + port_free_event_local(pkevp, B_TRUE);
1630 1630 return (0);
1631 1631 }
1632 1632
1633 1633 #ifdef _SYSCALL32_IMPL
1634 1634 /*
1635 1635 * 1. copy kernel event structure to user event structure.
1636 1636 * 2. PORT_KEV_WIRED event structures will be reused by the "source"
1637 1637 * 3. Remove PORT_KEV_DONEQ flag (event removed from the event queue)
1638 1638 * 4. Other types of event structures can be delivered back to the port cache
1639 1639 * (port_free_event_local()).
1640 1640 * 5. The event source callback function is the last opportunity for the
1641 1641 * event source to update events, to free local resources associated with
1642 1642 * the event or to deny the delivery of the event.
1643 1643 */
1644 1644 static int
1645 1645 port_copy_event32(port_event32_t *puevp, port_kevent_t *pkevp, list_t *list)
1646 1646 {
1647 1647 int free_event = 0;
1648 1648 int error;
1649 1649 int flags;
1650 1650
1651 1651 puevp->portev_source = pkevp->portkev_source;
1652 1652 puevp->portev_object = (daddr32_t)pkevp->portkev_object;
1653 1653 puevp->portev_user = (caddr32_t)(uintptr_t)pkevp->portkev_user;
1654 1654 puevp->portev_events = pkevp->portkev_events;
1655 1655
1656 1656 /* remove event from the queue */
1657 1657 list_remove(list, pkevp);
1658 1658
1659 1659 /*
1660 1660 * Events if type PORT_KEV_WIRED remain allocated by the
1661 1661 * sub-system (source).
1662 1662 */
1663 1663
1664 1664 flags = pkevp->portkev_flags;
1665 1665 if (pkevp->portkev_flags & PORT_KEV_WIRED)
1666 1666 pkevp->portkev_flags &= ~PORT_KEV_DONEQ;
1667 1667 else
1668 1668 free_event = 1;
1669 1669
1670 1670 if (pkevp->portkev_callback != NULL) {
1671 1671 error = (*pkevp->portkev_callback)(pkevp->portkev_arg,
1672 1672 &puevp->portev_events, pkevp->portkev_pid,
1673 1673 PORT_CALLBACK_DEFAULT, pkevp);
|
↓ open down ↓ |
34 lines elided |
↑ open up ↑ |
1674 1674 if (error) {
1675 1675 /*
1676 1676 * Event can not be delivered.
1677 1677 * Caller must reinsert the event into the queue.
1678 1678 */
1679 1679 pkevp->portkev_flags = flags;
1680 1680 return (error);
1681 1681 }
1682 1682 }
1683 1683 if (free_event)
1684 - port_free_event_local(pkevp, 0);
1684 + port_free_event_local(pkevp, B_TRUE);
1685 1685 return (0);
1686 1686 }
1687 1687 #endif /* _SYSCALL32_IMPL */
1688 1688
1689 1689 /*
1690 1690 * copyout alert event.
1691 1691 */
1692 1692 static int
1693 1693 port_get_alert(port_alert_t *pa, port_event_t *uevp)
1694 1694 {
1695 1695 model_t model = get_udatamodel();
1696 1696
1697 1697 /* copyout alert event structures to user space */
1698 1698 if (model == DATAMODEL_NATIVE) {
1699 1699 port_event_t uev;
1700 1700 uev.portev_source = PORT_SOURCE_ALERT;
1701 1701 uev.portev_object = pa->portal_object;
1702 1702 uev.portev_events = pa->portal_events;
1703 1703 uev.portev_user = pa->portal_user;
1704 1704 if (copyout(&uev, uevp, sizeof (port_event_t)))
1705 1705 return (EFAULT);
1706 1706 #ifdef _SYSCALL32_IMPL
1707 1707 } else {
1708 1708 port_event32_t uev32;
1709 1709 uev32.portev_source = PORT_SOURCE_ALERT;
1710 1710 uev32.portev_object = (daddr32_t)pa->portal_object;
1711 1711 uev32.portev_events = pa->portal_events;
1712 1712 uev32.portev_user = (daddr32_t)(uintptr_t)pa->portal_user;
1713 1713 if (copyout(&uev32, uevp, sizeof (port_event32_t)))
1714 1714 return (EFAULT);
1715 1715 #endif /* _SYSCALL32_IMPL */
1716 1716 }
1717 1717 return (0);
1718 1718 }
1719 1719
1720 1720 /*
1721 1721 * Check return conditions :
1722 1722 * - pending port close(2)
1723 1723 * - threads waiting for events
1724 1724 */
1725 1725 static void
1726 1726 port_check_return_cond(port_queue_t *portq)
1727 1727 {
1728 1728 ASSERT(MUTEX_HELD(&portq->portq_mutex));
1729 1729 portq->portq_thrcnt--;
1730 1730 if (portq->portq_flags & PORTQ_CLOSE) {
1731 1731 if (portq->portq_thrcnt == 0)
1732 1732 cv_signal(&portq->portq_closecv);
1733 1733 else
1734 1734 cv_signal(&portq->portq_thread->portget_cv);
1735 1735 }
1736 1736 }
1737 1737
1738 1738 /*
1739 1739 * The port_get_kevent() function returns
1740 1740 * - the event located at the head of the queue if 'last' pointer is NULL
1741 1741 * - the next event after the event pointed by 'last'
1742 1742 * The caller of this function is responsible for the integrity of the queue
1743 1743 * in use:
1744 1744 * - port_getn() is using a temporary queue protected with port_block().
1745 1745 * - port_close_events() is working on the global event queue and protects
1746 1746 * the queue with portq->portq_mutex.
1747 1747 */
1748 1748 port_kevent_t *
1749 1749 port_get_kevent(list_t *list, port_kevent_t *last)
1750 1750 {
1751 1751 if (last == NULL)
1752 1752 return (list_head(list));
1753 1753 else
1754 1754 return (list_next(list, last));
1755 1755 }
1756 1756
1757 1757 /*
1758 1758 * The port_get_timeout() function gets the timeout data from user space
1759 1759 * and converts that info into a corresponding internal representation.
1760 1760 * The kerneldata flag means that the timeout data is already loaded.
1761 1761 */
1762 1762 static int
1763 1763 port_get_timeout(timespec_t *timeout, timespec_t *rqtime, timespec_t **rqtp,
1764 1764 int *blocking, int kerneldata)
1765 1765 {
1766 1766 model_t model = get_udatamodel();
1767 1767
1768 1768 *rqtp = NULL;
1769 1769 if (timeout == NULL) {
1770 1770 *blocking = 1;
1771 1771 return (0);
1772 1772 }
1773 1773
1774 1774 if (kerneldata) {
1775 1775 *rqtime = *timeout;
1776 1776 } else {
1777 1777 if (model == DATAMODEL_NATIVE) {
1778 1778 if (copyin(timeout, rqtime, sizeof (*rqtime)))
1779 1779 return (EFAULT);
1780 1780 #ifdef _SYSCALL32_IMPL
1781 1781 } else {
1782 1782 timespec32_t wait_time_32;
1783 1783 if (copyin(timeout, &wait_time_32,
1784 1784 sizeof (wait_time_32)))
1785 1785 return (EFAULT);
1786 1786 TIMESPEC32_TO_TIMESPEC(rqtime, &wait_time_32);
1787 1787 #endif /* _SYSCALL32_IMPL */
1788 1788 }
1789 1789 }
1790 1790
1791 1791 if (rqtime->tv_sec == 0 && rqtime->tv_nsec == 0) {
1792 1792 *blocking = 0;
1793 1793 return (0);
1794 1794 }
1795 1795
1796 1796 if (rqtime->tv_sec < 0 ||
1797 1797 rqtime->tv_nsec < 0 || rqtime->tv_nsec >= NANOSEC)
1798 1798 return (EINVAL);
1799 1799
1800 1800 *rqtp = rqtime;
1801 1801 *blocking = 1;
1802 1802 return (0);
1803 1803 }
1804 1804
1805 1805 /*
1806 1806 * port_queue_thread()
1807 1807 * Threads requiring more events than available will be put in a wait queue.
1808 1808 * There is a "thread wait queue" per port.
1809 1809 * Threads requiring less events get a higher priority than others and they
1810 1810 * will be awoken first.
1811 1811 */
1812 1812 static portget_t *
1813 1813 port_queue_thread(port_queue_t *portq, uint_t nget)
1814 1814 {
1815 1815 portget_t *pgetp;
1816 1816 portget_t *ttp;
1817 1817 portget_t *htp;
1818 1818
1819 1819 pgetp = kmem_zalloc(sizeof (portget_t), KM_SLEEP);
1820 1820 pgetp->portget_nget = nget;
1821 1821 pgetp->portget_pid = curproc->p_pid;
1822 1822 if (portq->portq_thread == NULL) {
1823 1823 /* first waiting thread */
1824 1824 portq->portq_thread = pgetp;
1825 1825 portq->portq_nget = nget;
1826 1826 pgetp->portget_prev = pgetp;
1827 1827 pgetp->portget_next = pgetp;
1828 1828 return (pgetp);
1829 1829 }
1830 1830
1831 1831 /*
1832 1832 * thread waiting for less events will be set on top of the queue.
1833 1833 */
1834 1834 ttp = portq->portq_thread;
1835 1835 htp = ttp;
1836 1836 for (;;) {
1837 1837 if (nget <= ttp->portget_nget)
1838 1838 break;
1839 1839 if (htp == ttp->portget_next)
1840 1840 break; /* last event */
1841 1841 ttp = ttp->portget_next;
1842 1842 }
1843 1843
1844 1844 /* add thread to the queue */
1845 1845 pgetp->portget_next = ttp;
1846 1846 pgetp->portget_prev = ttp->portget_prev;
1847 1847 ttp->portget_prev->portget_next = pgetp;
1848 1848 ttp->portget_prev = pgetp;
1849 1849 if (portq->portq_thread == ttp)
1850 1850 portq->portq_thread = pgetp;
1851 1851 portq->portq_nget = portq->portq_thread->portget_nget;
1852 1852 return (pgetp);
1853 1853 }
1854 1854
1855 1855 /*
1856 1856 * Take thread out of the queue.
1857 1857 */
1858 1858 static void
1859 1859 port_dequeue_thread(port_queue_t *portq, portget_t *pgetp)
1860 1860 {
1861 1861 if (pgetp->portget_next == pgetp) {
1862 1862 /* last (single) waiting thread */
1863 1863 portq->portq_thread = NULL;
1864 1864 portq->portq_nget = 0;
1865 1865 } else {
1866 1866 pgetp->portget_prev->portget_next = pgetp->portget_next;
1867 1867 pgetp->portget_next->portget_prev = pgetp->portget_prev;
1868 1868 if (portq->portq_thread == pgetp)
1869 1869 portq->portq_thread = pgetp->portget_next;
1870 1870 portq->portq_nget = portq->portq_thread->portget_nget;
1871 1871 }
1872 1872 kmem_free(pgetp, sizeof (portget_t));
1873 1873 }
1874 1874
1875 1875 /*
1876 1876 * Set up event port kstats.
1877 1877 */
1878 1878 static void
1879 1879 port_kstat_init()
1880 1880 {
1881 1881 kstat_t *ksp;
1882 1882 uint_t ndata;
1883 1883
1884 1884 ndata = sizeof (port_kstat) / sizeof (kstat_named_t);
1885 1885 ksp = kstat_create("portfs", 0, "Event Ports", "misc",
1886 1886 KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_VIRTUAL);
1887 1887 if (ksp) {
1888 1888 ksp->ks_data = &port_kstat;
1889 1889 kstat_install(ksp);
1890 1890 }
1891 1891 }
|
↓ open down ↓ |
197 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX