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