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