1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 22 /* All Rights Reserved */ 23 24 25 /* 26 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 27 * Use is subject to license terms. 28 */ 29 30 #include <sys/types.h> 31 #include <sys/sysmacros.h> 32 #include <sys/param.h> 33 #include <sys/errno.h> 34 #include <sys/signal.h> 35 #include <sys/proc.h> 36 #include <sys/conf.h> 37 #include <sys/cred.h> 38 #include <sys/user.h> 39 #include <sys/vnode.h> 40 #include <sys/file.h> 41 #include <sys/session.h> 42 #include <sys/stream.h> 43 #include <sys/strsubr.h> 44 #include <sys/stropts.h> 45 #include <sys/poll.h> 46 #include <sys/systm.h> 47 #include <sys/cpuvar.h> 48 #include <sys/uio.h> 49 #include <sys/cmn_err.h> 50 #include <sys/priocntl.h> 51 #include <sys/procset.h> 52 #include <sys/vmem.h> 53 #include <sys/bitmap.h> 54 #include <sys/kmem.h> 55 #include <sys/siginfo.h> 56 #include <sys/vtrace.h> 57 #include <sys/callb.h> 58 #include <sys/debug.h> 59 #include <sys/modctl.h> 60 #include <sys/vmsystm.h> 61 #include <vm/page.h> 62 #include <sys/atomic.h> 63 #include <sys/suntpi.h> 64 #include <sys/strlog.h> 65 #include <sys/promif.h> 66 #include <sys/project.h> 67 #include <sys/vm.h> 68 #include <sys/taskq.h> 69 #include <sys/sunddi.h> 70 #include <sys/sunldi_impl.h> 71 #include <sys/strsun.h> 72 #include <sys/isa_defs.h> 73 #include <sys/multidata.h> 74 #include <sys/pattr.h> 75 #include <sys/strft.h> 76 #include <sys/fs/snode.h> 77 #include <sys/zone.h> 78 #include <sys/open.h> 79 #include <sys/sunldi.h> 80 #include <sys/sad.h> 81 #include <sys/netstack.h> 82 83 #define O_SAMESTR(q) (((q)->q_next) && \ 84 (((q)->q_flag & QREADR) == ((q)->q_next->q_flag & QREADR))) 85 86 /* 87 * WARNING: 88 * The variables and routines in this file are private, belonging 89 * to the STREAMS subsystem. These should not be used by modules 90 * or drivers. Compatibility will not be guaranteed. 91 */ 92 93 /* 94 * Id value used to distinguish between different multiplexor links. 95 */ 96 static int32_t lnk_id = 0; 97 98 #define STREAMS_LOPRI MINCLSYSPRI 99 static pri_t streams_lopri = STREAMS_LOPRI; 100 101 #define STRSTAT(x) (str_statistics.x.value.ui64++) 102 typedef struct str_stat { 103 kstat_named_t sqenables; 104 kstat_named_t stenables; 105 kstat_named_t syncqservice; 106 kstat_named_t freebs; 107 kstat_named_t qwr_outer; 108 kstat_named_t rservice; 109 kstat_named_t strwaits; 110 kstat_named_t taskqfails; 111 kstat_named_t bufcalls; 112 kstat_named_t qhelps; 113 kstat_named_t qremoved; 114 kstat_named_t sqremoved; 115 kstat_named_t bcwaits; 116 kstat_named_t sqtoomany; 117 } str_stat_t; 118 119 static str_stat_t str_statistics = { 120 { "sqenables", KSTAT_DATA_UINT64 }, 121 { "stenables", KSTAT_DATA_UINT64 }, 122 { "syncqservice", KSTAT_DATA_UINT64 }, 123 { "freebs", KSTAT_DATA_UINT64 }, 124 { "qwr_outer", KSTAT_DATA_UINT64 }, 125 { "rservice", KSTAT_DATA_UINT64 }, 126 { "strwaits", KSTAT_DATA_UINT64 }, 127 { "taskqfails", KSTAT_DATA_UINT64 }, 128 { "bufcalls", KSTAT_DATA_UINT64 }, 129 { "qhelps", KSTAT_DATA_UINT64 }, 130 { "qremoved", KSTAT_DATA_UINT64 }, 131 { "sqremoved", KSTAT_DATA_UINT64 }, 132 { "bcwaits", KSTAT_DATA_UINT64 }, 133 { "sqtoomany", KSTAT_DATA_UINT64 }, 134 }; 135 136 static kstat_t *str_kstat; 137 138 /* 139 * qrunflag was used previously to control background scheduling of queues. It 140 * is not used anymore, but kept here in case some module still wants to access 141 * it via qready() and setqsched macros. 142 */ 143 char qrunflag; /* Unused */ 144 145 /* 146 * Most of the streams scheduling is done via task queues. Task queues may fail 147 * for non-sleep dispatches, so there are two backup threads servicing failed 148 * requests for queues and syncqs. Both of these threads also service failed 149 * dispatches freebs requests. Queues are put in the list specified by `qhead' 150 * and `qtail' pointers, syncqs use `sqhead' and `sqtail' pointers and freebs 151 * requests are put into `freebs_list' which has no tail pointer. All three 152 * lists are protected by a single `service_queue' lock and use 153 * `services_to_run' condition variable for signaling background threads. Use of 154 * a single lock should not be a problem because it is only used under heavy 155 * loads when task queues start to fail and at that time it may be a good idea 156 * to throttle scheduling requests. 157 * 158 * NOTE: queues and syncqs should be scheduled by two separate threads because 159 * queue servicing may be blocked waiting for a syncq which may be also 160 * scheduled for background execution. This may create a deadlock when only one 161 * thread is used for both. 162 */ 163 164 static taskq_t *streams_taskq; /* Used for most STREAMS scheduling */ 165 166 static kmutex_t service_queue; /* protects all of servicing vars */ 167 static kcondvar_t services_to_run; /* wake up background service thread */ 168 static kcondvar_t syncqs_to_run; /* wake up background service thread */ 169 170 /* 171 * List of queues scheduled for background processing dueue to lack of resources 172 * in the task queues. Protected by service_queue lock; 173 */ 174 static struct queue *qhead; 175 static struct queue *qtail; 176 177 /* 178 * Same list for syncqs 179 */ 180 static syncq_t *sqhead; 181 static syncq_t *sqtail; 182 183 static mblk_t *freebs_list; /* list of buffers to free */ 184 185 /* 186 * Backup threads for servicing queues and syncqs 187 */ 188 kthread_t *streams_qbkgrnd_thread; 189 kthread_t *streams_sqbkgrnd_thread; 190 191 /* 192 * Bufcalls related variables. 193 */ 194 struct bclist strbcalls; /* list of waiting bufcalls */ 195 kmutex_t strbcall_lock; /* protects bufcall list (strbcalls) */ 196 kcondvar_t strbcall_cv; /* Signaling when a bufcall is added */ 197 kmutex_t bcall_monitor; /* sleep/wakeup style monitor */ 198 kcondvar_t bcall_cv; /* wait 'till executing bufcall completes */ 199 kthread_t *bc_bkgrnd_thread; /* Thread to service bufcall requests */ 200 201 kmutex_t strresources; /* protects global resources */ 202 kmutex_t muxifier; /* single-threads multiplexor creation */ 203 204 static void *str_stack_init(netstackid_t stackid, netstack_t *ns); 205 static void str_stack_shutdown(netstackid_t stackid, void *arg); 206 static void str_stack_fini(netstackid_t stackid, void *arg); 207 208 extern void time_to_wait(clock_t *, clock_t); 209 210 /* 211 * run_queues is no longer used, but is kept in case some 3-d party 212 * module/driver decides to use it. 213 */ 214 int run_queues = 0; 215 216 /* 217 * sq_max_size is the depth of the syncq (in number of messages) before 218 * qfill_syncq() starts QFULL'ing destination queues. As its primary 219 * consumer - IP is no longer D_MTPERMOD, but there may be other 220 * modules/drivers depend on this syncq flow control, we prefer to 221 * choose a large number as the default value. For potential 222 * performance gain, this value is tunable in /etc/system. 223 */ 224 int sq_max_size = 10000; 225 226 /* 227 * the number of ciputctrl structures per syncq and stream we create when 228 * needed. 229 */ 230 int n_ciputctrl; 231 int max_n_ciputctrl = 16; 232 /* 233 * if n_ciputctrl is < min_n_ciputctrl don't even create ciputctrl_cache. 234 */ 235 int min_n_ciputctrl = 2; 236 237 /* 238 * Per-driver/module syncqs 239 * ======================== 240 * 241 * For drivers/modules that use PERMOD or outer syncqs we keep a list of 242 * perdm structures, new entries being added (and new syncqs allocated) when 243 * setq() encounters a module/driver with a streamtab that it hasn't seen 244 * before. 245 * The reason for this mechanism is that some modules and drivers share a 246 * common streamtab and it is necessary for those modules and drivers to also 247 * share a common PERMOD syncq. 248 * 249 * perdm_list --> dm_str == streamtab_1 250 * dm_sq == syncq_1 251 * dm_ref 252 * dm_next --> dm_str == streamtab_2 253 * dm_sq == syncq_2 254 * dm_ref 255 * dm_next --> ... NULL 256 * 257 * The dm_ref field is incremented for each new driver/module that takes 258 * a reference to the perdm structure and hence shares the syncq. 259 * References are held in the fmodsw_impl_t structure for each STREAMS module 260 * or the dev_impl array (indexed by device major number) for each driver. 261 * 262 * perdm_list -> [dm_ref == 1] -> [dm_ref == 2] -> [dm_ref == 1] -> NULL 263 * ^ ^ ^ ^ 264 * | ______________/ | | 265 * | / | | 266 * dev_impl: ...|x|y|... module A module B 267 * 268 * When a module/driver is unloaded the reference count is decremented and, 269 * when it falls to zero, the perdm structure is removed from the list and 270 * the syncq is freed (see rele_dm()). 271 */ 272 perdm_t *perdm_list = NULL; 273 static krwlock_t perdm_rwlock; 274 cdevsw_impl_t *devimpl; 275 276 extern struct qinit strdata; 277 extern struct qinit stwdata; 278 279 static void runservice(queue_t *); 280 static void streams_bufcall_service(void); 281 static void streams_qbkgrnd_service(void); 282 static void streams_sqbkgrnd_service(void); 283 static syncq_t *new_syncq(void); 284 static void free_syncq(syncq_t *); 285 static void outer_insert(syncq_t *, syncq_t *); 286 static void outer_remove(syncq_t *, syncq_t *); 287 static void write_now(syncq_t *); 288 static void clr_qfull(queue_t *); 289 static void enable_svc(queue_t *); 290 static void runbufcalls(void); 291 static void sqenable(syncq_t *); 292 static void sqfill_events(syncq_t *, queue_t *, mblk_t *, void (*)()); 293 static void wait_q_syncq(queue_t *); 294 static void backenable_insertedq(queue_t *); 295 296 static void queue_service(queue_t *); 297 static void stream_service(stdata_t *); 298 static void syncq_service(syncq_t *); 299 static void qwriter_outer_service(syncq_t *); 300 static void mblk_free(mblk_t *); 301 #ifdef DEBUG 302 static int qprocsareon(queue_t *); 303 #endif 304 305 static void set_nfsrv_ptr(queue_t *, queue_t *, queue_t *, queue_t *); 306 static void reset_nfsrv_ptr(queue_t *, queue_t *); 307 void set_qfull(queue_t *); 308 309 static void sq_run_events(syncq_t *); 310 static int propagate_syncq(queue_t *); 311 312 static void blocksq(syncq_t *, ushort_t, int); 313 static void unblocksq(syncq_t *, ushort_t, int); 314 static int dropsq(syncq_t *, uint16_t); 315 static void emptysq(syncq_t *); 316 static sqlist_t *sqlist_alloc(struct stdata *, int); 317 static void sqlist_free(sqlist_t *); 318 static sqlist_t *sqlist_build(queue_t *, struct stdata *, boolean_t); 319 static void sqlist_insert(sqlist_t *, syncq_t *); 320 static void sqlist_insertall(sqlist_t *, queue_t *); 321 322 static void strsetuio(stdata_t *); 323 324 struct kmem_cache *stream_head_cache; 325 struct kmem_cache *queue_cache; 326 struct kmem_cache *syncq_cache; 327 struct kmem_cache *qband_cache; 328 struct kmem_cache *linkinfo_cache; 329 struct kmem_cache *ciputctrl_cache = NULL; 330 331 static linkinfo_t *linkinfo_list; 332 333 /* global esballoc throttling queue */ 334 static esb_queue_t system_esbq; 335 336 /* 337 * esballoc tunable parameters. 338 */ 339 int esbq_max_qlen = 0x16; /* throttled queue length */ 340 clock_t esbq_timeout = 0x8; /* timeout to process esb queue */ 341 342 /* 343 * routines to handle esballoc queuing. 344 */ 345 static void esballoc_process_queue(esb_queue_t *); 346 static void esballoc_enqueue_mblk(mblk_t *); 347 static void esballoc_timer(void *); 348 static void esballoc_set_timer(esb_queue_t *, clock_t); 349 static void esballoc_mblk_free(mblk_t *); 350 351 /* 352 * Qinit structure and Module_info structures 353 * for passthru read and write queues 354 */ 355 356 static void pass_wput(queue_t *, mblk_t *); 357 static queue_t *link_addpassthru(stdata_t *); 358 static void link_rempassthru(queue_t *); 359 360 struct module_info passthru_info = { 361 0, 362 "passthru", 363 0, 364 INFPSZ, 365 STRHIGH, 366 STRLOW 367 }; 368 369 struct qinit passthru_rinit = { 370 (int (*)())putnext, 371 NULL, 372 NULL, 373 NULL, 374 NULL, 375 &passthru_info, 376 NULL 377 }; 378 379 struct qinit passthru_winit = { 380 (int (*)()) pass_wput, 381 NULL, 382 NULL, 383 NULL, 384 NULL, 385 &passthru_info, 386 NULL 387 }; 388 389 /* 390 * Special form of assertion: verify that X implies Y i.e. when X is true Y 391 * should also be true. 392 */ 393 #define IMPLY(X, Y) ASSERT(!(X) || (Y)) 394 395 /* 396 * Logical equivalence. Verify that both X and Y are either TRUE or FALSE. 397 */ 398 #define EQUIV(X, Y) { IMPLY(X, Y); IMPLY(Y, X); } 399 400 /* 401 * Verify correctness of list head/tail pointers. 402 */ 403 #define LISTCHECK(head, tail, link) { \ 404 EQUIV(head, tail); \ 405 IMPLY(tail != NULL, tail->link == NULL); \ 406 } 407 408 /* 409 * Enqueue a list element `el' in the end of a list denoted by `head' and `tail' 410 * using a `link' field. 411 */ 412 #define ENQUEUE(el, head, tail, link) { \ 413 ASSERT(el->link == NULL); \ 414 LISTCHECK(head, tail, link); \ 415 if (head == NULL) \ 416 head = el; \ 417 else \ 418 tail->link = el; \ 419 tail = el; \ 420 } 421 422 /* 423 * Dequeue the first element of the list denoted by `head' and `tail' pointers 424 * using a `link' field and put result into `el'. 425 */ 426 #define DQ(el, head, tail, link) { \ 427 LISTCHECK(head, tail, link); \ 428 el = head; \ 429 if (head != NULL) { \ 430 head = head->link; \ 431 if (head == NULL) \ 432 tail = NULL; \ 433 el->link = NULL; \ 434 } \ 435 } 436 437 /* 438 * Remove `el' from the list using `chase' and `curr' pointers and return result 439 * in `succeed'. 440 */ 441 #define RMQ(el, head, tail, link, chase, curr, succeed) { \ 442 LISTCHECK(head, tail, link); \ 443 chase = NULL; \ 444 succeed = 0; \ 445 for (curr = head; (curr != el) && (curr != NULL); curr = curr->link) \ 446 chase = curr; \ 447 if (curr != NULL) { \ 448 succeed = 1; \ 449 ASSERT(curr == el); \ 450 if (chase != NULL) \ 451 chase->link = curr->link; \ 452 else \ 453 head = curr->link; \ 454 curr->link = NULL; \ 455 if (curr == tail) \ 456 tail = chase; \ 457 } \ 458 LISTCHECK(head, tail, link); \ 459 } 460 461 /* Handling of delayed messages on the inner syncq. */ 462 463 /* 464 * DEBUG versions should use function versions (to simplify tracing) and 465 * non-DEBUG kernels should use macro versions. 466 */ 467 468 /* 469 * Put a queue on the syncq list of queues. 470 * Assumes SQLOCK held. 471 */ 472 #define SQPUT_Q(sq, qp) \ 473 { \ 474 ASSERT(MUTEX_HELD(SQLOCK(sq))); \ 475 if (!(qp->q_sqflags & Q_SQQUEUED)) { \ 476 /* The queue should not be linked anywhere */ \ 477 ASSERT((qp->q_sqprev == NULL) && (qp->q_sqnext == NULL)); \ 478 /* Head and tail may only be NULL simultaneously */ \ 479 EQUIV(sq->sq_head, sq->sq_tail); \ 480 /* Queue may be only enqueyed on its syncq */ \ 481 ASSERT(sq == qp->q_syncq); \ 482 /* Check the correctness of SQ_MESSAGES flag */ \ 483 EQUIV(sq->sq_head, (sq->sq_flags & SQ_MESSAGES)); \ 484 /* Sanity check first/last elements of the list */ \ 485 IMPLY(sq->sq_head != NULL, sq->sq_head->q_sqprev == NULL);\ 486 IMPLY(sq->sq_tail != NULL, sq->sq_tail->q_sqnext == NULL);\ 487 /* \ 488 * Sanity check of priority field: empty queue should \ 489 * have zero priority \ 490 * and nqueues equal to zero. \ 491 */ \ 492 IMPLY(sq->sq_head == NULL, sq->sq_pri == 0); \ 493 /* Sanity check of sq_nqueues field */ \ 494 EQUIV(sq->sq_head, sq->sq_nqueues); \ 495 if (sq->sq_head == NULL) { \ 496 sq->sq_head = sq->sq_tail = qp; \ 497 sq->sq_flags |= SQ_MESSAGES; \ 498 } else if (qp->q_spri == 0) { \ 499 qp->q_sqprev = sq->sq_tail; \ 500 sq->sq_tail->q_sqnext = qp; \ 501 sq->sq_tail = qp; \ 502 } else { \ 503 /* \ 504 * Put this queue in priority order: higher \ 505 * priority gets closer to the head. \ 506 */ \ 507 queue_t **qpp = &sq->sq_tail; \ 508 queue_t *qnext = NULL; \ 509 \ 510 while (*qpp != NULL && qp->q_spri > (*qpp)->q_spri) { \ 511 qnext = *qpp; \ 512 qpp = &(*qpp)->q_sqprev; \ 513 } \ 514 qp->q_sqnext = qnext; \ 515 qp->q_sqprev = *qpp; \ 516 if (*qpp != NULL) { \ 517 (*qpp)->q_sqnext = qp; \ 518 } else { \ 519 sq->sq_head = qp; \ 520 sq->sq_pri = sq->sq_head->q_spri; \ 521 } \ 522 *qpp = qp; \ 523 } \ 524 qp->q_sqflags |= Q_SQQUEUED; \ 525 qp->q_sqtstamp = lbolt; \ 526 sq->sq_nqueues++; \ 527 } \ 528 } 529 530 /* 531 * Remove a queue from the syncq list 532 * Assumes SQLOCK held. 533 */ 534 #define SQRM_Q(sq, qp) \ 535 { \ 536 ASSERT(MUTEX_HELD(SQLOCK(sq))); \ 537 ASSERT(qp->q_sqflags & Q_SQQUEUED); \ 538 ASSERT(sq->sq_head != NULL && sq->sq_tail != NULL); \ 539 ASSERT((sq->sq_flags & SQ_MESSAGES) != 0); \ 540 /* Check that the queue is actually in the list */ \ 541 ASSERT(qp->q_sqnext != NULL || sq->sq_tail == qp); \ 542 ASSERT(qp->q_sqprev != NULL || sq->sq_head == qp); \ 543 ASSERT(sq->sq_nqueues != 0); \ 544 if (qp->q_sqprev == NULL) { \ 545 /* First queue on list, make head q_sqnext */ \ 546 sq->sq_head = qp->q_sqnext; \ 547 } else { \ 548 /* Make prev->next == next */ \ 549 qp->q_sqprev->q_sqnext = qp->q_sqnext; \ 550 } \ 551 if (qp->q_sqnext == NULL) { \ 552 /* Last queue on list, make tail sqprev */ \ 553 sq->sq_tail = qp->q_sqprev; \ 554 } else { \ 555 /* Make next->prev == prev */ \ 556 qp->q_sqnext->q_sqprev = qp->q_sqprev; \ 557 } \ 558 /* clear out references on this queue */ \ 559 qp->q_sqprev = qp->q_sqnext = NULL; \ 560 qp->q_sqflags &= ~Q_SQQUEUED; \ 561 /* If there is nothing queued, clear SQ_MESSAGES */ \ 562 if (sq->sq_head != NULL) { \ 563 sq->sq_pri = sq->sq_head->q_spri; \ 564 } else { \ 565 sq->sq_flags &= ~SQ_MESSAGES; \ 566 sq->sq_pri = 0; \ 567 } \ 568 sq->sq_nqueues--; \ 569 ASSERT(sq->sq_head != NULL || sq->sq_evhead != NULL || \ 570 (sq->sq_flags & SQ_QUEUED) == 0); \ 571 } 572 573 /* Hide the definition from the header file. */ 574 #ifdef SQPUT_MP 575 #undef SQPUT_MP 576 #endif 577 578 /* 579 * Put a message on the queue syncq. 580 * Assumes QLOCK held. 581 */ 582 #define SQPUT_MP(qp, mp) \ 583 { \ 584 ASSERT(MUTEX_HELD(QLOCK(qp))); \ 585 ASSERT(qp->q_sqhead == NULL || \ 586 (qp->q_sqtail != NULL && \ 587 qp->q_sqtail->b_next == NULL)); \ 588 qp->q_syncqmsgs++; \ 589 ASSERT(qp->q_syncqmsgs != 0); /* Wraparound */ \ 590 if (qp->q_sqhead == NULL) { \ 591 qp->q_sqhead = qp->q_sqtail = mp; \ 592 } else { \ 593 qp->q_sqtail->b_next = mp; \ 594 qp->q_sqtail = mp; \ 595 } \ 596 ASSERT(qp->q_syncqmsgs > 0); \ 597 set_qfull(qp); \ 598 } 599 600 #define SQ_PUTCOUNT_SETFAST_LOCKED(sq) { \ 601 ASSERT(MUTEX_HELD(SQLOCK(sq))); \ 602 if ((sq)->sq_ciputctrl != NULL) { \ 603 int i; \ 604 int nlocks = (sq)->sq_nciputctrl; \ 605 ciputctrl_t *cip = (sq)->sq_ciputctrl; \ 606 ASSERT((sq)->sq_type & SQ_CIPUT); \ 607 for (i = 0; i <= nlocks; i++) { \ 608 ASSERT(MUTEX_HELD(&cip[i].ciputctrl_lock)); \ 609 cip[i].ciputctrl_count |= SQ_FASTPUT; \ 610 } \ 611 } \ 612 } 613 614 615 #define SQ_PUTCOUNT_CLRFAST_LOCKED(sq) { \ 616 ASSERT(MUTEX_HELD(SQLOCK(sq))); \ 617 if ((sq)->sq_ciputctrl != NULL) { \ 618 int i; \ 619 int nlocks = (sq)->sq_nciputctrl; \ 620 ciputctrl_t *cip = (sq)->sq_ciputctrl; \ 621 ASSERT((sq)->sq_type & SQ_CIPUT); \ 622 for (i = 0; i <= nlocks; i++) { \ 623 ASSERT(MUTEX_HELD(&cip[i].ciputctrl_lock)); \ 624 cip[i].ciputctrl_count &= ~SQ_FASTPUT; \ 625 } \ 626 } \ 627 } 628 629 /* 630 * Run service procedures for all queues in the stream head. 631 */ 632 #define STR_SERVICE(stp, q) { \ 633 ASSERT(MUTEX_HELD(&stp->sd_qlock)); \ 634 while (stp->sd_qhead != NULL) { \ 635 DQ(q, stp->sd_qhead, stp->sd_qtail, q_link); \ 636 ASSERT(stp->sd_nqueues > 0); \ 637 stp->sd_nqueues--; \ 638 ASSERT(!(q->q_flag & QINSERVICE)); \ 639 mutex_exit(&stp->sd_qlock); \ 640 queue_service(q); \ 641 mutex_enter(&stp->sd_qlock); \ 642 } \ 643 ASSERT(stp->sd_nqueues == 0); \ 644 ASSERT((stp->sd_qhead == NULL) && (stp->sd_qtail == NULL)); \ 645 } 646 647 /* 648 * constructor/destructor routines for the stream head cache 649 */ 650 /* ARGSUSED */ 651 static int 652 stream_head_constructor(void *buf, void *cdrarg, int kmflags) 653 { 654 stdata_t *stp = buf; 655 656 mutex_init(&stp->sd_lock, NULL, MUTEX_DEFAULT, NULL); 657 mutex_init(&stp->sd_reflock, NULL, MUTEX_DEFAULT, NULL); 658 mutex_init(&stp->sd_qlock, NULL, MUTEX_DEFAULT, NULL); 659 cv_init(&stp->sd_monitor, NULL, CV_DEFAULT, NULL); 660 cv_init(&stp->sd_iocmonitor, NULL, CV_DEFAULT, NULL); 661 cv_init(&stp->sd_refmonitor, NULL, CV_DEFAULT, NULL); 662 cv_init(&stp->sd_qcv, NULL, CV_DEFAULT, NULL); 663 cv_init(&stp->sd_zcopy_wait, NULL, CV_DEFAULT, NULL); 664 stp->sd_wrq = NULL; 665 666 return (0); 667 } 668 669 /* ARGSUSED */ 670 static void 671 stream_head_destructor(void *buf, void *cdrarg) 672 { 673 stdata_t *stp = buf; 674 675 mutex_destroy(&stp->sd_lock); 676 mutex_destroy(&stp->sd_reflock); 677 mutex_destroy(&stp->sd_qlock); 678 cv_destroy(&stp->sd_monitor); 679 cv_destroy(&stp->sd_iocmonitor); 680 cv_destroy(&stp->sd_refmonitor); 681 cv_destroy(&stp->sd_qcv); 682 cv_destroy(&stp->sd_zcopy_wait); 683 } 684 685 /* 686 * constructor/destructor routines for the queue cache 687 */ 688 /* ARGSUSED */ 689 static int 690 queue_constructor(void *buf, void *cdrarg, int kmflags) 691 { 692 queinfo_t *qip = buf; 693 queue_t *qp = &qip->qu_rqueue; 694 queue_t *wqp = &qip->qu_wqueue; 695 syncq_t *sq = &qip->qu_syncq; 696 697 qp->q_first = NULL; 698 qp->q_link = NULL; 699 qp->q_count = 0; 700 qp->q_mblkcnt = 0; 701 qp->q_sqhead = NULL; 702 qp->q_sqtail = NULL; 703 qp->q_sqnext = NULL; 704 qp->q_sqprev = NULL; 705 qp->q_sqflags = 0; 706 qp->q_rwcnt = 0; 707 qp->q_spri = 0; 708 709 mutex_init(QLOCK(qp), NULL, MUTEX_DEFAULT, NULL); 710 cv_init(&qp->q_wait, NULL, CV_DEFAULT, NULL); 711 712 wqp->q_first = NULL; 713 wqp->q_link = NULL; 714 wqp->q_count = 0; 715 wqp->q_mblkcnt = 0; 716 wqp->q_sqhead = NULL; 717 wqp->q_sqtail = NULL; 718 wqp->q_sqnext = NULL; 719 wqp->q_sqprev = NULL; 720 wqp->q_sqflags = 0; 721 wqp->q_rwcnt = 0; 722 wqp->q_spri = 0; 723 724 mutex_init(QLOCK(wqp), NULL, MUTEX_DEFAULT, NULL); 725 cv_init(&wqp->q_wait, NULL, CV_DEFAULT, NULL); 726 727 sq->sq_head = NULL; 728 sq->sq_tail = NULL; 729 sq->sq_evhead = NULL; 730 sq->sq_evtail = NULL; 731 sq->sq_callbpend = NULL; 732 sq->sq_outer = NULL; 733 sq->sq_onext = NULL; 734 sq->sq_oprev = NULL; 735 sq->sq_next = NULL; 736 sq->sq_svcflags = 0; 737 sq->sq_servcount = 0; 738 sq->sq_needexcl = 0; 739 sq->sq_nqueues = 0; 740 sq->sq_pri = 0; 741 742 mutex_init(&sq->sq_lock, NULL, MUTEX_DEFAULT, NULL); 743 cv_init(&sq->sq_wait, NULL, CV_DEFAULT, NULL); 744 cv_init(&sq->sq_exitwait, NULL, CV_DEFAULT, NULL); 745 746 return (0); 747 } 748 749 /* ARGSUSED */ 750 static void 751 queue_destructor(void *buf, void *cdrarg) 752 { 753 queinfo_t *qip = buf; 754 queue_t *qp = &qip->qu_rqueue; 755 queue_t *wqp = &qip->qu_wqueue; 756 syncq_t *sq = &qip->qu_syncq; 757 758 ASSERT(qp->q_sqhead == NULL); 759 ASSERT(wqp->q_sqhead == NULL); 760 ASSERT(qp->q_sqnext == NULL); 761 ASSERT(wqp->q_sqnext == NULL); 762 ASSERT(qp->q_rwcnt == 0); 763 ASSERT(wqp->q_rwcnt == 0); 764 765 mutex_destroy(&qp->q_lock); 766 cv_destroy(&qp->q_wait); 767 768 mutex_destroy(&wqp->q_lock); 769 cv_destroy(&wqp->q_wait); 770 771 mutex_destroy(&sq->sq_lock); 772 cv_destroy(&sq->sq_wait); 773 cv_destroy(&sq->sq_exitwait); 774 } 775 776 /* 777 * constructor/destructor routines for the syncq cache 778 */ 779 /* ARGSUSED */ 780 static int 781 syncq_constructor(void *buf, void *cdrarg, int kmflags) 782 { 783 syncq_t *sq = buf; 784 785 bzero(buf, sizeof (syncq_t)); 786 787 mutex_init(&sq->sq_lock, NULL, MUTEX_DEFAULT, NULL); 788 cv_init(&sq->sq_wait, NULL, CV_DEFAULT, NULL); 789 cv_init(&sq->sq_exitwait, NULL, CV_DEFAULT, NULL); 790 791 return (0); 792 } 793 794 /* ARGSUSED */ 795 static void 796 syncq_destructor(void *buf, void *cdrarg) 797 { 798 syncq_t *sq = buf; 799 800 ASSERT(sq->sq_head == NULL); 801 ASSERT(sq->sq_tail == NULL); 802 ASSERT(sq->sq_evhead == NULL); 803 ASSERT(sq->sq_evtail == NULL); 804 ASSERT(sq->sq_callbpend == NULL); 805 ASSERT(sq->sq_callbflags == 0); 806 ASSERT(sq->sq_outer == NULL); 807 ASSERT(sq->sq_onext == NULL); 808 ASSERT(sq->sq_oprev == NULL); 809 ASSERT(sq->sq_next == NULL); 810 ASSERT(sq->sq_needexcl == 0); 811 ASSERT(sq->sq_svcflags == 0); 812 ASSERT(sq->sq_servcount == 0); 813 ASSERT(sq->sq_nqueues == 0); 814 ASSERT(sq->sq_pri == 0); 815 ASSERT(sq->sq_count == 0); 816 ASSERT(sq->sq_rmqcount == 0); 817 ASSERT(sq->sq_cancelid == 0); 818 ASSERT(sq->sq_ciputctrl == NULL); 819 ASSERT(sq->sq_nciputctrl == 0); 820 ASSERT(sq->sq_type == 0); 821 ASSERT(sq->sq_flags == 0); 822 823 mutex_destroy(&sq->sq_lock); 824 cv_destroy(&sq->sq_wait); 825 cv_destroy(&sq->sq_exitwait); 826 } 827 828 /* ARGSUSED */ 829 static int 830 ciputctrl_constructor(void *buf, void *cdrarg, int kmflags) 831 { 832 ciputctrl_t *cip = buf; 833 int i; 834 835 for (i = 0; i < n_ciputctrl; i++) { 836 cip[i].ciputctrl_count = SQ_FASTPUT; 837 mutex_init(&cip[i].ciputctrl_lock, NULL, MUTEX_DEFAULT, NULL); 838 } 839 840 return (0); 841 } 842 843 /* ARGSUSED */ 844 static void 845 ciputctrl_destructor(void *buf, void *cdrarg) 846 { 847 ciputctrl_t *cip = buf; 848 int i; 849 850 for (i = 0; i < n_ciputctrl; i++) { 851 ASSERT(cip[i].ciputctrl_count & SQ_FASTPUT); 852 mutex_destroy(&cip[i].ciputctrl_lock); 853 } 854 } 855 856 /* 857 * Init routine run from main at boot time. 858 */ 859 void 860 strinit(void) 861 { 862 int ncpus = ((boot_max_ncpus == -1) ? max_ncpus : boot_max_ncpus); 863 864 stream_head_cache = kmem_cache_create("stream_head_cache", 865 sizeof (stdata_t), 0, 866 stream_head_constructor, stream_head_destructor, NULL, 867 NULL, NULL, 0); 868 869 queue_cache = kmem_cache_create("queue_cache", sizeof (queinfo_t), 0, 870 queue_constructor, queue_destructor, NULL, NULL, NULL, 0); 871 872 syncq_cache = kmem_cache_create("syncq_cache", sizeof (syncq_t), 0, 873 syncq_constructor, syncq_destructor, NULL, NULL, NULL, 0); 874 875 qband_cache = kmem_cache_create("qband_cache", 876 sizeof (qband_t), 0, NULL, NULL, NULL, NULL, NULL, 0); 877 878 linkinfo_cache = kmem_cache_create("linkinfo_cache", 879 sizeof (linkinfo_t), 0, NULL, NULL, NULL, NULL, NULL, 0); 880 881 n_ciputctrl = ncpus; 882 n_ciputctrl = 1 << highbit(n_ciputctrl - 1); 883 ASSERT(n_ciputctrl >= 1); 884 n_ciputctrl = MIN(n_ciputctrl, max_n_ciputctrl); 885 if (n_ciputctrl >= min_n_ciputctrl) { 886 ciputctrl_cache = kmem_cache_create("ciputctrl_cache", 887 sizeof (ciputctrl_t) * n_ciputctrl, 888 sizeof (ciputctrl_t), ciputctrl_constructor, 889 ciputctrl_destructor, NULL, NULL, NULL, 0); 890 } 891 892 streams_taskq = system_taskq; 893 894 if (streams_taskq == NULL) 895 panic("strinit: no memory for streams taskq!"); 896 897 bc_bkgrnd_thread = thread_create(NULL, 0, 898 streams_bufcall_service, NULL, 0, &p0, TS_RUN, streams_lopri); 899 900 streams_qbkgrnd_thread = thread_create(NULL, 0, 901 streams_qbkgrnd_service, NULL, 0, &p0, TS_RUN, streams_lopri); 902 903 streams_sqbkgrnd_thread = thread_create(NULL, 0, 904 streams_sqbkgrnd_service, NULL, 0, &p0, TS_RUN, streams_lopri); 905 906 /* 907 * Create STREAMS kstats. 908 */ 909 str_kstat = kstat_create("streams", 0, "strstat", 910 "net", KSTAT_TYPE_NAMED, 911 sizeof (str_statistics) / sizeof (kstat_named_t), 912 KSTAT_FLAG_VIRTUAL); 913 914 if (str_kstat != NULL) { 915 str_kstat->ks_data = &str_statistics; 916 kstat_install(str_kstat); 917 } 918 919 /* 920 * TPI support routine initialisation. 921 */ 922 tpi_init(); 923 924 /* 925 * Handle to have autopush and persistent link information per 926 * zone. 927 * Note: uses shutdown hook instead of destroy hook so that the 928 * persistent links can be torn down before the destroy hooks 929 * in the TCP/IP stack are called. 930 */ 931 netstack_register(NS_STR, str_stack_init, str_stack_shutdown, 932 str_stack_fini); 933 } 934 935 void 936 str_sendsig(vnode_t *vp, int event, uchar_t band, int error) 937 { 938 struct stdata *stp; 939 940 ASSERT(vp->v_stream); 941 stp = vp->v_stream; 942 /* Have to hold sd_lock to prevent siglist from changing */ 943 mutex_enter(&stp->sd_lock); 944 if (stp->sd_sigflags & event) 945 strsendsig(stp->sd_siglist, event, band, error); 946 mutex_exit(&stp->sd_lock); 947 } 948 949 /* 950 * Send the "sevent" set of signals to a process. 951 * This might send more than one signal if the process is registered 952 * for multiple events. The caller should pass in an sevent that only 953 * includes the events for which the process has registered. 954 */ 955 static void 956 dosendsig(proc_t *proc, int events, int sevent, k_siginfo_t *info, 957 uchar_t band, int error) 958 { 959 ASSERT(MUTEX_HELD(&proc->p_lock)); 960 961 info->si_band = 0; 962 info->si_errno = 0; 963 964 if (sevent & S_ERROR) { 965 sevent &= ~S_ERROR; 966 info->si_code = POLL_ERR; 967 info->si_errno = error; 968 TRACE_2(TR_FAC_STREAMS_FR, TR_STRSENDSIG, 969 "strsendsig:proc %p info %p", proc, info); 970 sigaddq(proc, NULL, info, KM_NOSLEEP); 971 info->si_errno = 0; 972 } 973 if (sevent & S_HANGUP) { 974 sevent &= ~S_HANGUP; 975 info->si_code = POLL_HUP; 976 TRACE_2(TR_FAC_STREAMS_FR, TR_STRSENDSIG, 977 "strsendsig:proc %p info %p", proc, info); 978 sigaddq(proc, NULL, info, KM_NOSLEEP); 979 } 980 if (sevent & S_HIPRI) { 981 sevent &= ~S_HIPRI; 982 info->si_code = POLL_PRI; 983 TRACE_2(TR_FAC_STREAMS_FR, TR_STRSENDSIG, 984 "strsendsig:proc %p info %p", proc, info); 985 sigaddq(proc, NULL, info, KM_NOSLEEP); 986 } 987 if (sevent & S_RDBAND) { 988 sevent &= ~S_RDBAND; 989 if (events & S_BANDURG) 990 sigtoproc(proc, NULL, SIGURG); 991 else 992 sigtoproc(proc, NULL, SIGPOLL); 993 } 994 if (sevent & S_WRBAND) { 995 sevent &= ~S_WRBAND; 996 sigtoproc(proc, NULL, SIGPOLL); 997 } 998 if (sevent & S_INPUT) { 999 sevent &= ~S_INPUT; 1000 info->si_code = POLL_IN; 1001 info->si_band = band; 1002 TRACE_2(TR_FAC_STREAMS_FR, TR_STRSENDSIG, 1003 "strsendsig:proc %p info %p", proc, info); 1004 sigaddq(proc, NULL, info, KM_NOSLEEP); 1005 info->si_band = 0; 1006 } 1007 if (sevent & S_OUTPUT) { 1008 sevent &= ~S_OUTPUT; 1009 info->si_code = POLL_OUT; 1010 info->si_band = band; 1011 TRACE_2(TR_FAC_STREAMS_FR, TR_STRSENDSIG, 1012 "strsendsig:proc %p info %p", proc, info); 1013 sigaddq(proc, NULL, info, KM_NOSLEEP); 1014 info->si_band = 0; 1015 } 1016 if (sevent & S_MSG) { 1017 sevent &= ~S_MSG; 1018 info->si_code = POLL_MSG; 1019 info->si_band = band; 1020 TRACE_2(TR_FAC_STREAMS_FR, TR_STRSENDSIG, 1021 "strsendsig:proc %p info %p", proc, info); 1022 sigaddq(proc, NULL, info, KM_NOSLEEP); 1023 info->si_band = 0; 1024 } 1025 if (sevent & S_RDNORM) { 1026 sevent &= ~S_RDNORM; 1027 sigtoproc(proc, NULL, SIGPOLL); 1028 } 1029 if (sevent != 0) { 1030 panic("strsendsig: unknown event(s) %x", sevent); 1031 } 1032 } 1033 1034 /* 1035 * Send SIGPOLL/SIGURG signal to all processes and process groups 1036 * registered on the given signal list that want a signal for at 1037 * least one of the specified events. 1038 * 1039 * Must be called with exclusive access to siglist (caller holding sd_lock). 1040 * 1041 * strioctl(I_SETSIG/I_ESETSIG) will only change siglist when holding 1042 * sd_lock and the ioctl code maintains a PID_HOLD on the pid structure 1043 * while it is in the siglist. 1044 * 1045 * For performance reasons (MP scalability) the code drops pidlock 1046 * when sending signals to a single process. 1047 * When sending to a process group the code holds 1048 * pidlock to prevent the membership in the process group from changing 1049 * while walking the p_pglink list. 1050 */ 1051 void 1052 strsendsig(strsig_t *siglist, int event, uchar_t band, int error) 1053 { 1054 strsig_t *ssp; 1055 k_siginfo_t info; 1056 struct pid *pidp; 1057 proc_t *proc; 1058 1059 info.si_signo = SIGPOLL; 1060 info.si_errno = 0; 1061 for (ssp = siglist; ssp; ssp = ssp->ss_next) { 1062 int sevent; 1063 1064 sevent = ssp->ss_events & event; 1065 if (sevent == 0) 1066 continue; 1067 1068 if ((pidp = ssp->ss_pidp) == NULL) { 1069 /* pid was released but still on event list */ 1070 continue; 1071 } 1072 1073 1074 if (ssp->ss_pid > 0) { 1075 /* 1076 * XXX This unfortunately still generates 1077 * a signal when a fd is closed but 1078 * the proc is active. 1079 */ 1080 ASSERT(ssp->ss_pid == pidp->pid_id); 1081 1082 mutex_enter(&pidlock); 1083 proc = prfind_zone(pidp->pid_id, ALL_ZONES); 1084 if (proc == NULL) { 1085 mutex_exit(&pidlock); 1086 continue; 1087 } 1088 mutex_enter(&proc->p_lock); 1089 mutex_exit(&pidlock); 1090 dosendsig(proc, ssp->ss_events, sevent, &info, 1091 band, error); 1092 mutex_exit(&proc->p_lock); 1093 } else { 1094 /* 1095 * Send to process group. Hold pidlock across 1096 * calls to dosendsig(). 1097 */ 1098 pid_t pgrp = -ssp->ss_pid; 1099 1100 mutex_enter(&pidlock); 1101 proc = pgfind_zone(pgrp, ALL_ZONES); 1102 while (proc != NULL) { 1103 mutex_enter(&proc->p_lock); 1104 dosendsig(proc, ssp->ss_events, sevent, 1105 &info, band, error); 1106 mutex_exit(&proc->p_lock); 1107 proc = proc->p_pglink; 1108 } 1109 mutex_exit(&pidlock); 1110 } 1111 } 1112 } 1113 1114 /* 1115 * Attach a stream device or module. 1116 * qp is a read queue; the new queue goes in so its next 1117 * read ptr is the argument, and the write queue corresponding 1118 * to the argument points to this queue. Return 0 on success, 1119 * or a non-zero errno on failure. 1120 */ 1121 int 1122 qattach(queue_t *qp, dev_t *devp, int oflag, cred_t *crp, fmodsw_impl_t *fp, 1123 boolean_t is_insert) 1124 { 1125 major_t major; 1126 cdevsw_impl_t *dp; 1127 struct streamtab *str; 1128 queue_t *rq; 1129 queue_t *wrq; 1130 uint32_t qflag; 1131 uint32_t sqtype; 1132 perdm_t *dmp; 1133 int error; 1134 int sflag; 1135 1136 rq = allocq(); 1137 wrq = _WR(rq); 1138 STREAM(rq) = STREAM(wrq) = STREAM(qp); 1139 1140 if (fp != NULL) { 1141 str = fp->f_str; 1142 qflag = fp->f_qflag; 1143 sqtype = fp->f_sqtype; 1144 dmp = fp->f_dmp; 1145 IMPLY((qflag & (QPERMOD | QMTOUTPERIM)), dmp != NULL); 1146 sflag = MODOPEN; 1147 1148 /* 1149 * stash away a pointer to the module structure so we can 1150 * unref it in qdetach. 1151 */ 1152 rq->q_fp = fp; 1153 } else { 1154 ASSERT(!is_insert); 1155 1156 major = getmajor(*devp); 1157 dp = &devimpl[major]; 1158 1159 str = dp->d_str; 1160 ASSERT(str == STREAMSTAB(major)); 1161 1162 qflag = dp->d_qflag; 1163 ASSERT(qflag & QISDRV); 1164 sqtype = dp->d_sqtype; 1165 1166 /* create perdm_t if needed */ 1167 if (NEED_DM(dp->d_dmp, qflag)) 1168 dp->d_dmp = hold_dm(str, qflag, sqtype); 1169 1170 dmp = dp->d_dmp; 1171 sflag = 0; 1172 } 1173 1174 TRACE_2(TR_FAC_STREAMS_FR, TR_QATTACH_FLAGS, 1175 "qattach:qflag == %X(%X)", qflag, *devp); 1176 1177 /* setq might sleep in allocator - avoid holding locks. */ 1178 setq(rq, str->st_rdinit, str->st_wrinit, dmp, qflag, sqtype, B_FALSE); 1179 1180 /* 1181 * Before calling the module's open routine, set up the q_next 1182 * pointer for inserting a module in the middle of a stream. 1183 * 1184 * Note that we can always set _QINSERTING and set up q_next 1185 * pointer for both inserting and pushing a module. Then there 1186 * is no need for the is_insert parameter. In insertq(), called 1187 * by qprocson(), assume that q_next of the new module always points 1188 * to the correct queue and use it for insertion. Everything should 1189 * work out fine. But in the first release of _I_INSERT, we 1190 * distinguish between inserting and pushing to make sure that 1191 * pushing a module follows the same code path as before. 1192 */ 1193 if (is_insert) { 1194 rq->q_flag |= _QINSERTING; 1195 rq->q_next = qp; 1196 } 1197 1198 /* 1199 * If there is an outer perimeter get exclusive access during 1200 * the open procedure. Bump up the reference count on the queue. 1201 */ 1202 entersq(rq->q_syncq, SQ_OPENCLOSE); 1203 error = (*rq->q_qinfo->qi_qopen)(rq, devp, oflag, sflag, crp); 1204 if (error != 0) 1205 goto failed; 1206 leavesq(rq->q_syncq, SQ_OPENCLOSE); 1207 ASSERT(qprocsareon(rq)); 1208 return (0); 1209 1210 failed: 1211 rq->q_flag &= ~_QINSERTING; 1212 if (backq(wrq) != NULL && backq(wrq)->q_next == wrq) 1213 qprocsoff(rq); 1214 leavesq(rq->q_syncq, SQ_OPENCLOSE); 1215 rq->q_next = wrq->q_next = NULL; 1216 qdetach(rq, 0, 0, crp, B_FALSE); 1217 return (error); 1218 } 1219 1220 /* 1221 * Handle second open of stream. For modules, set the 1222 * last argument to MODOPEN and do not pass any open flags. 1223 * Ignore dummydev since this is not the first open. 1224 */ 1225 int 1226 qreopen(queue_t *qp, dev_t *devp, int flag, cred_t *crp) 1227 { 1228 int error; 1229 dev_t dummydev; 1230 queue_t *wqp = _WR(qp); 1231 1232 ASSERT(qp->q_flag & QREADR); 1233 entersq(qp->q_syncq, SQ_OPENCLOSE); 1234 1235 dummydev = *devp; 1236 if (error = ((*qp->q_qinfo->qi_qopen)(qp, &dummydev, 1237 (wqp->q_next ? 0 : flag), (wqp->q_next ? MODOPEN : 0), crp))) { 1238 leavesq(qp->q_syncq, SQ_OPENCLOSE); 1239 mutex_enter(&STREAM(qp)->sd_lock); 1240 qp->q_stream->sd_flag |= STREOPENFAIL; 1241 mutex_exit(&STREAM(qp)->sd_lock); 1242 return (error); 1243 } 1244 leavesq(qp->q_syncq, SQ_OPENCLOSE); 1245 1246 /* 1247 * successful open should have done qprocson() 1248 */ 1249 ASSERT(qprocsareon(_RD(qp))); 1250 return (0); 1251 } 1252 1253 /* 1254 * Detach a stream module or device. 1255 * If clmode == 1 then the module or driver was opened and its 1256 * close routine must be called. If clmode == 0, the module 1257 * or driver was never opened or the open failed, and so its close 1258 * should not be called. 1259 */ 1260 void 1261 qdetach(queue_t *qp, int clmode, int flag, cred_t *crp, boolean_t is_remove) 1262 { 1263 queue_t *wqp = _WR(qp); 1264 ASSERT(STREAM(qp)->sd_flag & (STRCLOSE|STWOPEN|STRPLUMB)); 1265 1266 if (STREAM_NEEDSERVICE(STREAM(qp))) 1267 stream_runservice(STREAM(qp)); 1268 1269 if (clmode) { 1270 /* 1271 * Make sure that all the messages on the write side syncq are 1272 * processed and nothing is left. Since we are closing, no new 1273 * messages may appear there. 1274 */ 1275 wait_q_syncq(wqp); 1276 1277 entersq(qp->q_syncq, SQ_OPENCLOSE); 1278 if (is_remove) { 1279 mutex_enter(QLOCK(qp)); 1280 qp->q_flag |= _QREMOVING; 1281 mutex_exit(QLOCK(qp)); 1282 } 1283 (*qp->q_qinfo->qi_qclose)(qp, flag, crp); 1284 /* 1285 * Check that qprocsoff() was actually called. 1286 */ 1287 ASSERT((qp->q_flag & QWCLOSE) && (wqp->q_flag & QWCLOSE)); 1288 1289 leavesq(qp->q_syncq, SQ_OPENCLOSE); 1290 } else { 1291 disable_svc(qp); 1292 } 1293 1294 /* 1295 * Allow any threads blocked in entersq to proceed and discover 1296 * the QWCLOSE is set. 1297 * Note: This assumes that all users of entersq check QWCLOSE. 1298 * Currently runservice is the only entersq that can happen 1299 * after removeq has finished. 1300 * Removeq will have discarded all messages destined to the closing 1301 * pair of queues from the syncq. 1302 * NOTE: Calling a function inside an assert is unconventional. 1303 * However, it does not cause any problem since flush_syncq() does 1304 * not change any state except when it returns non-zero i.e. 1305 * when the assert will trigger. 1306 */ 1307 ASSERT(flush_syncq(qp->q_syncq, qp) == 0); 1308 ASSERT(flush_syncq(wqp->q_syncq, wqp) == 0); 1309 ASSERT((qp->q_flag & QPERMOD) || 1310 ((qp->q_syncq->sq_head == NULL) && 1311 (wqp->q_syncq->sq_head == NULL))); 1312 1313 /* release any fmodsw_impl_t structure held on behalf of the queue */ 1314 ASSERT(qp->q_fp != NULL || qp->q_flag & QISDRV); 1315 if (qp->q_fp != NULL) 1316 fmodsw_rele(qp->q_fp); 1317 1318 /* freeq removes us from the outer perimeter if any */ 1319 freeq(qp); 1320 } 1321 1322 /* Prevent service procedures from being called */ 1323 void 1324 disable_svc(queue_t *qp) 1325 { 1326 queue_t *wqp = _WR(qp); 1327 1328 ASSERT(qp->q_flag & QREADR); 1329 mutex_enter(QLOCK(qp)); 1330 qp->q_flag |= QWCLOSE; 1331 mutex_exit(QLOCK(qp)); 1332 mutex_enter(QLOCK(wqp)); 1333 wqp->q_flag |= QWCLOSE; 1334 mutex_exit(QLOCK(wqp)); 1335 } 1336 1337 /* allow service procedures to be called again */ 1338 void 1339 enable_svc(queue_t *qp) 1340 { 1341 queue_t *wqp = _WR(qp); 1342 1343 ASSERT(qp->q_flag & QREADR); 1344 mutex_enter(QLOCK(qp)); 1345 qp->q_flag &= ~QWCLOSE; 1346 mutex_exit(QLOCK(qp)); 1347 mutex_enter(QLOCK(wqp)); 1348 wqp->q_flag &= ~QWCLOSE; 1349 mutex_exit(QLOCK(wqp)); 1350 } 1351 1352 /* 1353 * Remove queue from qhead/qtail if it is enabled. 1354 * Only reset QENAB if the queue was removed from the runlist. 1355 * A queue goes through 3 stages: 1356 * It is on the service list and QENAB is set. 1357 * It is removed from the service list but QENAB is still set. 1358 * QENAB gets changed to QINSERVICE. 1359 * QINSERVICE is reset (when the service procedure is done) 1360 * Thus we can not reset QENAB unless we actually removed it from the service 1361 * queue. 1362 */ 1363 void 1364 remove_runlist(queue_t *qp) 1365 { 1366 if (qp->q_flag & QENAB && qhead != NULL) { 1367 queue_t *q_chase; 1368 queue_t *q_curr; 1369 int removed; 1370 1371 mutex_enter(&service_queue); 1372 RMQ(qp, qhead, qtail, q_link, q_chase, q_curr, removed); 1373 mutex_exit(&service_queue); 1374 if (removed) { 1375 STRSTAT(qremoved); 1376 qp->q_flag &= ~QENAB; 1377 } 1378 } 1379 } 1380 1381 1382 /* 1383 * wait for any pending service processing to complete. 1384 * The removal of queues from the runlist is not atomic with the 1385 * clearing of the QENABLED flag and setting the INSERVICE flag. 1386 * consequently it is possible for remove_runlist in strclose 1387 * to not find the queue on the runlist but for it to be QENABLED 1388 * and not yet INSERVICE -> hence wait_svc needs to check QENABLED 1389 * as well as INSERVICE. 1390 */ 1391 void 1392 wait_svc(queue_t *qp) 1393 { 1394 queue_t *wqp = _WR(qp); 1395 1396 ASSERT(qp->q_flag & QREADR); 1397 1398 /* 1399 * Try to remove queues from qhead/qtail list. 1400 */ 1401 if (qhead != NULL) { 1402 remove_runlist(qp); 1403 remove_runlist(wqp); 1404 } 1405 /* 1406 * Wait till the syncqs associated with the queue 1407 * will dissapear from background processing list. 1408 * This only needs to be done for non-PERMOD perimeters since 1409 * for PERMOD perimeters the syncq may be shared and will only be freed 1410 * when the last module/driver is unloaded. 1411 * If for PERMOD perimeters queue was on the syncq list, removeq() 1412 * should call propagate_syncq() or drain_syncq() for it. Both of these 1413 * function remove the queue from its syncq list, so sqthread will not 1414 * try to access the queue. 1415 */ 1416 if (!(qp->q_flag & QPERMOD)) { 1417 syncq_t *rsq = qp->q_syncq; 1418 syncq_t *wsq = wqp->q_syncq; 1419 1420 /* 1421 * Disable rsq and wsq and wait for any background processing of 1422 * syncq to complete. 1423 */ 1424 wait_sq_svc(rsq); 1425 if (wsq != rsq) 1426 wait_sq_svc(wsq); 1427 } 1428 1429 mutex_enter(QLOCK(qp)); 1430 while (qp->q_flag & (QINSERVICE|QENAB)) 1431 cv_wait(&qp->q_wait, QLOCK(qp)); 1432 mutex_exit(QLOCK(qp)); 1433 mutex_enter(QLOCK(wqp)); 1434 while (wqp->q_flag & (QINSERVICE|QENAB)) 1435 cv_wait(&wqp->q_wait, QLOCK(wqp)); 1436 mutex_exit(QLOCK(wqp)); 1437 } 1438 1439 /* 1440 * Put ioctl data from userland buffer `arg' into the mblk chain `bp'. 1441 * `flag' must always contain either K_TO_K or U_TO_K; STR_NOSIG may 1442 * also be set, and is passed through to allocb_cred_wait(). 1443 * 1444 * Returns errno on failure, zero on success. 1445 */ 1446 int 1447 putiocd(mblk_t *bp, char *arg, int flag, cred_t *cr) 1448 { 1449 mblk_t *tmp; 1450 ssize_t count; 1451 int error = 0; 1452 1453 ASSERT((flag & (U_TO_K | K_TO_K)) == U_TO_K || 1454 (flag & (U_TO_K | K_TO_K)) == K_TO_K); 1455 1456 if (bp->b_datap->db_type == M_IOCTL) { 1457 count = ((struct iocblk *)bp->b_rptr)->ioc_count; 1458 } else { 1459 ASSERT(bp->b_datap->db_type == M_COPYIN); 1460 count = ((struct copyreq *)bp->b_rptr)->cq_size; 1461 } 1462 /* 1463 * strdoioctl validates ioc_count, so if this assert fails it 1464 * cannot be due to user error. 1465 */ 1466 ASSERT(count >= 0); 1467 1468 if ((tmp = allocb_cred_wait(count, (flag & STR_NOSIG), &error, cr)) == 1469 NULL) { 1470 return (error); 1471 } 1472 error = strcopyin(arg, tmp->b_wptr, count, flag & (U_TO_K|K_TO_K)); 1473 if (error != 0) { 1474 freeb(tmp); 1475 return (error); 1476 } 1477 DB_CPID(tmp) = curproc->p_pid; 1478 tmp->b_wptr += count; 1479 bp->b_cont = tmp; 1480 1481 return (0); 1482 } 1483 1484 /* 1485 * Copy ioctl data to user-land. Return non-zero errno on failure, 1486 * 0 for success. 1487 */ 1488 int 1489 getiocd(mblk_t *bp, char *arg, int copymode) 1490 { 1491 ssize_t count; 1492 size_t n; 1493 int error; 1494 1495 if (bp->b_datap->db_type == M_IOCACK) 1496 count = ((struct iocblk *)bp->b_rptr)->ioc_count; 1497 else { 1498 ASSERT(bp->b_datap->db_type == M_COPYOUT); 1499 count = ((struct copyreq *)bp->b_rptr)->cq_size; 1500 } 1501 ASSERT(count >= 0); 1502 1503 for (bp = bp->b_cont; bp && count; 1504 count -= n, bp = bp->b_cont, arg += n) { 1505 n = MIN(count, bp->b_wptr - bp->b_rptr); 1506 error = strcopyout(bp->b_rptr, arg, n, copymode); 1507 if (error) 1508 return (error); 1509 } 1510 ASSERT(count == 0); 1511 return (0); 1512 } 1513 1514 /* 1515 * Allocate a linkinfo entry given the write queue of the 1516 * bottom module of the top stream and the write queue of the 1517 * stream head of the bottom stream. 1518 */ 1519 linkinfo_t * 1520 alloclink(queue_t *qup, queue_t *qdown, file_t *fpdown) 1521 { 1522 linkinfo_t *linkp; 1523 1524 linkp = kmem_cache_alloc(linkinfo_cache, KM_SLEEP); 1525 1526 linkp->li_lblk.l_qtop = qup; 1527 linkp->li_lblk.l_qbot = qdown; 1528 linkp->li_fpdown = fpdown; 1529 1530 mutex_enter(&strresources); 1531 linkp->li_next = linkinfo_list; 1532 linkp->li_prev = NULL; 1533 if (linkp->li_next) 1534 linkp->li_next->li_prev = linkp; 1535 linkinfo_list = linkp; 1536 linkp->li_lblk.l_index = ++lnk_id; 1537 ASSERT(lnk_id != 0); /* this should never wrap in practice */ 1538 mutex_exit(&strresources); 1539 1540 return (linkp); 1541 } 1542 1543 /* 1544 * Free a linkinfo entry. 1545 */ 1546 void 1547 lbfree(linkinfo_t *linkp) 1548 { 1549 mutex_enter(&strresources); 1550 if (linkp->li_next) 1551 linkp->li_next->li_prev = linkp->li_prev; 1552 if (linkp->li_prev) 1553 linkp->li_prev->li_next = linkp->li_next; 1554 else 1555 linkinfo_list = linkp->li_next; 1556 mutex_exit(&strresources); 1557 1558 kmem_cache_free(linkinfo_cache, linkp); 1559 } 1560 1561 /* 1562 * Check for a potential linking cycle. 1563 * Return 1 if a link will result in a cycle, 1564 * and 0 otherwise. 1565 */ 1566 int 1567 linkcycle(stdata_t *upstp, stdata_t *lostp, str_stack_t *ss) 1568 { 1569 struct mux_node *np; 1570 struct mux_edge *ep; 1571 int i; 1572 major_t lomaj; 1573 major_t upmaj; 1574 /* 1575 * if the lower stream is a pipe/FIFO, return, since link 1576 * cycles can not happen on pipes/FIFOs 1577 */ 1578 if (lostp->sd_vnode->v_type == VFIFO) 1579 return (0); 1580 1581 for (i = 0; i < ss->ss_devcnt; i++) { 1582 np = &ss->ss_mux_nodes[i]; 1583 MUX_CLEAR(np); 1584 } 1585 lomaj = getmajor(lostp->sd_vnode->v_rdev); 1586 upmaj = getmajor(upstp->sd_vnode->v_rdev); 1587 np = &ss->ss_mux_nodes[lomaj]; 1588 for (;;) { 1589 if (!MUX_DIDVISIT(np)) { 1590 if (np->mn_imaj == upmaj) 1591 return (1); 1592 if (np->mn_outp == NULL) { 1593 MUX_VISIT(np); 1594 if (np->mn_originp == NULL) 1595 return (0); 1596 np = np->mn_originp; 1597 continue; 1598 } 1599 MUX_VISIT(np); 1600 np->mn_startp = np->mn_outp; 1601 } else { 1602 if (np->mn_startp == NULL) { 1603 if (np->mn_originp == NULL) 1604 return (0); 1605 else { 1606 np = np->mn_originp; 1607 continue; 1608 } 1609 } 1610 /* 1611 * If ep->me_nodep is a FIFO (me_nodep == NULL), 1612 * ignore the edge and move on. ep->me_nodep gets 1613 * set to NULL in mux_addedge() if it is a FIFO. 1614 * 1615 */ 1616 ep = np->mn_startp; 1617 np->mn_startp = ep->me_nextp; 1618 if (ep->me_nodep == NULL) 1619 continue; 1620 ep->me_nodep->mn_originp = np; 1621 np = ep->me_nodep; 1622 } 1623 } 1624 } 1625 1626 /* 1627 * Find linkinfo entry corresponding to the parameters. 1628 */ 1629 linkinfo_t * 1630 findlinks(stdata_t *stp, int index, int type, str_stack_t *ss) 1631 { 1632 linkinfo_t *linkp; 1633 struct mux_edge *mep; 1634 struct mux_node *mnp; 1635 queue_t *qup; 1636 1637 mutex_enter(&strresources); 1638 if ((type & LINKTYPEMASK) == LINKNORMAL) { 1639 qup = getendq(stp->sd_wrq); 1640 for (linkp = linkinfo_list; linkp; linkp = linkp->li_next) { 1641 if ((qup == linkp->li_lblk.l_qtop) && 1642 (!index || (index == linkp->li_lblk.l_index))) { 1643 mutex_exit(&strresources); 1644 return (linkp); 1645 } 1646 } 1647 } else { 1648 ASSERT((type & LINKTYPEMASK) == LINKPERSIST); 1649 mnp = &ss->ss_mux_nodes[getmajor(stp->sd_vnode->v_rdev)]; 1650 mep = mnp->mn_outp; 1651 while (mep) { 1652 if ((index == 0) || (index == mep->me_muxid)) 1653 break; 1654 mep = mep->me_nextp; 1655 } 1656 if (!mep) { 1657 mutex_exit(&strresources); 1658 return (NULL); 1659 } 1660 for (linkp = linkinfo_list; linkp; linkp = linkp->li_next) { 1661 if ((!linkp->li_lblk.l_qtop) && 1662 (mep->me_muxid == linkp->li_lblk.l_index)) { 1663 mutex_exit(&strresources); 1664 return (linkp); 1665 } 1666 } 1667 } 1668 mutex_exit(&strresources); 1669 return (NULL); 1670 } 1671 1672 /* 1673 * Given a queue ptr, follow the chain of q_next pointers until you reach the 1674 * last queue on the chain and return it. 1675 */ 1676 queue_t * 1677 getendq(queue_t *q) 1678 { 1679 ASSERT(q != NULL); 1680 while (_SAMESTR(q)) 1681 q = q->q_next; 1682 return (q); 1683 } 1684 1685 /* 1686 * wait for the syncq count to drop to zero. 1687 * sq could be either outer or inner. 1688 */ 1689 1690 static void 1691 wait_syncq(syncq_t *sq) 1692 { 1693 uint16_t count; 1694 1695 mutex_enter(SQLOCK(sq)); 1696 count = sq->sq_count; 1697 SQ_PUTLOCKS_ENTER(sq); 1698 SUM_SQ_PUTCOUNTS(sq, count); 1699 while (count != 0) { 1700 sq->sq_flags |= SQ_WANTWAKEUP; 1701 SQ_PUTLOCKS_EXIT(sq); 1702 cv_wait(&sq->sq_wait, SQLOCK(sq)); 1703 count = sq->sq_count; 1704 SQ_PUTLOCKS_ENTER(sq); 1705 SUM_SQ_PUTCOUNTS(sq, count); 1706 } 1707 SQ_PUTLOCKS_EXIT(sq); 1708 mutex_exit(SQLOCK(sq)); 1709 } 1710 1711 /* 1712 * Wait while there are any messages for the queue in its syncq. 1713 */ 1714 static void 1715 wait_q_syncq(queue_t *q) 1716 { 1717 if ((q->q_sqflags & Q_SQQUEUED) || (q->q_syncqmsgs > 0)) { 1718 syncq_t *sq = q->q_syncq; 1719 1720 mutex_enter(SQLOCK(sq)); 1721 while ((q->q_sqflags & Q_SQQUEUED) || (q->q_syncqmsgs > 0)) { 1722 sq->sq_flags |= SQ_WANTWAKEUP; 1723 cv_wait(&sq->sq_wait, SQLOCK(sq)); 1724 } 1725 mutex_exit(SQLOCK(sq)); 1726 } 1727 } 1728 1729 1730 int 1731 mlink_file(vnode_t *vp, int cmd, struct file *fpdown, cred_t *crp, int *rvalp, 1732 int lhlink) 1733 { 1734 struct stdata *stp; 1735 struct strioctl strioc; 1736 struct linkinfo *linkp; 1737 struct stdata *stpdown; 1738 struct streamtab *str; 1739 queue_t *passq; 1740 syncq_t *passyncq; 1741 queue_t *rq; 1742 cdevsw_impl_t *dp; 1743 uint32_t qflag; 1744 uint32_t sqtype; 1745 perdm_t *dmp; 1746 int error = 0; 1747 netstack_t *ns; 1748 str_stack_t *ss; 1749 1750 stp = vp->v_stream; 1751 TRACE_1(TR_FAC_STREAMS_FR, 1752 TR_I_LINK, "I_LINK/I_PLINK:stp %p", stp); 1753 /* 1754 * Test for invalid upper stream 1755 */ 1756 if (stp->sd_flag & STRHUP) { 1757 return (ENXIO); 1758 } 1759 if (vp->v_type == VFIFO) { 1760 return (EINVAL); 1761 } 1762 if (stp->sd_strtab == NULL) { 1763 return (EINVAL); 1764 } 1765 if (!stp->sd_strtab->st_muxwinit) { 1766 return (EINVAL); 1767 } 1768 if (fpdown == NULL) { 1769 return (EBADF); 1770 } 1771 ns = netstack_find_by_cred(crp); 1772 ASSERT(ns != NULL); 1773 ss = ns->netstack_str; 1774 ASSERT(ss != NULL); 1775 1776 if (getmajor(stp->sd_vnode->v_rdev) >= ss->ss_devcnt) { 1777 netstack_rele(ss->ss_netstack); 1778 return (EINVAL); 1779 } 1780 mutex_enter(&muxifier); 1781 if (stp->sd_flag & STPLEX) { 1782 mutex_exit(&muxifier); 1783 netstack_rele(ss->ss_netstack); 1784 return (ENXIO); 1785 } 1786 1787 /* 1788 * Test for invalid lower stream. 1789 * The check for the v_type != VFIFO and having a major 1790 * number not >= devcnt is done to avoid problems with 1791 * adding mux_node entry past the end of mux_nodes[]. 1792 * For FIFO's we don't add an entry so this isn't a 1793 * problem. 1794 */ 1795 if (((stpdown = fpdown->f_vnode->v_stream) == NULL) || 1796 (stpdown == stp) || (stpdown->sd_flag & 1797 (STPLEX|STRHUP|STRDERR|STWRERR|IOCWAIT|STRPLUMB)) || 1798 ((stpdown->sd_vnode->v_type != VFIFO) && 1799 (getmajor(stpdown->sd_vnode->v_rdev) >= ss->ss_devcnt)) || 1800 linkcycle(stp, stpdown, ss)) { 1801 mutex_exit(&muxifier); 1802 netstack_rele(ss->ss_netstack); 1803 return (EINVAL); 1804 } 1805 TRACE_1(TR_FAC_STREAMS_FR, 1806 TR_STPDOWN, "stpdown:%p", stpdown); 1807 rq = getendq(stp->sd_wrq); 1808 if (cmd == I_PLINK) 1809 rq = NULL; 1810 1811 linkp = alloclink(rq, stpdown->sd_wrq, fpdown); 1812 1813 strioc.ic_cmd = cmd; 1814 strioc.ic_timout = INFTIM; 1815 strioc.ic_len = sizeof (struct linkblk); 1816 strioc.ic_dp = (char *)&linkp->li_lblk; 1817 1818 /* 1819 * STRPLUMB protects plumbing changes and should be set before 1820 * link_addpassthru()/link_rempassthru() are called, so it is set here 1821 * and cleared in the end of mlink when passthru queue is removed. 1822 * Setting of STRPLUMB prevents reopens of the stream while passthru 1823 * queue is in-place (it is not a proper module and doesn't have open 1824 * entry point). 1825 * 1826 * STPLEX prevents any threads from entering the stream from above. It 1827 * can't be set before the call to link_addpassthru() because putnext 1828 * from below may cause stream head I/O routines to be called and these 1829 * routines assert that STPLEX is not set. After link_addpassthru() 1830 * nothing may come from below since the pass queue syncq is blocked. 1831 * Note also that STPLEX should be cleared before the call to 1832 * link_remmpassthru() since when messages start flowing to the stream 1833 * head (e.g. because of message propagation from the pass queue) stream 1834 * head I/O routines may be called with STPLEX flag set. 1835 * 1836 * When STPLEX is set, nothing may come into the stream from above and 1837 * it is safe to do a setq which will change stream head. So, the 1838 * correct sequence of actions is: 1839 * 1840 * 1) Set STRPLUMB 1841 * 2) Call link_addpassthru() 1842 * 3) Set STPLEX 1843 * 4) Call setq and update the stream state 1844 * 5) Clear STPLEX 1845 * 6) Call link_rempassthru() 1846 * 7) Clear STRPLUMB 1847 * 1848 * The same sequence applies to munlink() code. 1849 */ 1850 mutex_enter(&stpdown->sd_lock); 1851 stpdown->sd_flag |= STRPLUMB; 1852 mutex_exit(&stpdown->sd_lock); 1853 /* 1854 * Add passthru queue below lower mux. This will block 1855 * syncqs of lower muxs read queue during I_LINK/I_UNLINK. 1856 */ 1857 passq = link_addpassthru(stpdown); 1858 1859 mutex_enter(&stpdown->sd_lock); 1860 stpdown->sd_flag |= STPLEX; 1861 mutex_exit(&stpdown->sd_lock); 1862 1863 rq = _RD(stpdown->sd_wrq); 1864 /* 1865 * There may be messages in the streamhead's syncq due to messages 1866 * that arrived before link_addpassthru() was done. To avoid 1867 * background processing of the syncq happening simultaneous with 1868 * setq processing, we disable the streamhead syncq and wait until 1869 * existing background thread finishes working on it. 1870 */ 1871 wait_sq_svc(rq->q_syncq); 1872 passyncq = passq->q_syncq; 1873 if (!(passyncq->sq_flags & SQ_BLOCKED)) 1874 blocksq(passyncq, SQ_BLOCKED, 0); 1875 1876 ASSERT((rq->q_flag & QMT_TYPEMASK) == QMTSAFE); 1877 ASSERT(rq->q_syncq == SQ(rq) && _WR(rq)->q_syncq == SQ(rq)); 1878 rq->q_ptr = _WR(rq)->q_ptr = NULL; 1879 1880 /* setq might sleep in allocator - avoid holding locks. */ 1881 /* Note: we are holding muxifier here. */ 1882 1883 str = stp->sd_strtab; 1884 dp = &devimpl[getmajor(vp->v_rdev)]; 1885 ASSERT(dp->d_str == str); 1886 1887 qflag = dp->d_qflag; 1888 sqtype = dp->d_sqtype; 1889 1890 /* create perdm_t if needed */ 1891 if (NEED_DM(dp->d_dmp, qflag)) 1892 dp->d_dmp = hold_dm(str, qflag, sqtype); 1893 1894 dmp = dp->d_dmp; 1895 1896 setq(rq, str->st_muxrinit, str->st_muxwinit, dmp, qflag, sqtype, 1897 B_TRUE); 1898 1899 /* 1900 * XXX Remove any "odd" messages from the queue. 1901 * Keep only M_DATA, M_PROTO, M_PCPROTO. 1902 */ 1903 error = strdoioctl(stp, &strioc, FNATIVE, 1904 K_TO_K | STR_NOERROR | STR_NOSIG, crp, rvalp); 1905 if (error != 0) { 1906 lbfree(linkp); 1907 1908 if (!(passyncq->sq_flags & SQ_BLOCKED)) 1909 blocksq(passyncq, SQ_BLOCKED, 0); 1910 /* 1911 * Restore the stream head queue and then remove 1912 * the passq. Turn off STPLEX before we turn on 1913 * the stream by removing the passq. 1914 */ 1915 rq->q_ptr = _WR(rq)->q_ptr = stpdown; 1916 setq(rq, &strdata, &stwdata, NULL, QMTSAFE, SQ_CI|SQ_CO, 1917 B_TRUE); 1918 1919 mutex_enter(&stpdown->sd_lock); 1920 stpdown->sd_flag &= ~STPLEX; 1921 mutex_exit(&stpdown->sd_lock); 1922 1923 link_rempassthru(passq); 1924 1925 mutex_enter(&stpdown->sd_lock); 1926 stpdown->sd_flag &= ~STRPLUMB; 1927 /* Wakeup anyone waiting for STRPLUMB to clear. */ 1928 cv_broadcast(&stpdown->sd_monitor); 1929 mutex_exit(&stpdown->sd_lock); 1930 1931 mutex_exit(&muxifier); 1932 netstack_rele(ss->ss_netstack); 1933 return (error); 1934 } 1935 mutex_enter(&fpdown->f_tlock); 1936 fpdown->f_count++; 1937 mutex_exit(&fpdown->f_tlock); 1938 1939 /* 1940 * if we've made it here the linkage is all set up so we should also 1941 * set up the layered driver linkages 1942 */ 1943 1944 ASSERT((cmd == I_LINK) || (cmd == I_PLINK)); 1945 if (cmd == I_LINK) { 1946 ldi_mlink_fp(stp, fpdown, lhlink, LINKNORMAL); 1947 } else { 1948 ldi_mlink_fp(stp, fpdown, lhlink, LINKPERSIST); 1949 } 1950 1951 link_rempassthru(passq); 1952 1953 mux_addedge(stp, stpdown, linkp->li_lblk.l_index, ss); 1954 1955 /* 1956 * Mark the upper stream as having dependent links 1957 * so that strclose can clean it up. 1958 */ 1959 if (cmd == I_LINK) { 1960 mutex_enter(&stp->sd_lock); 1961 stp->sd_flag |= STRHASLINKS; 1962 mutex_exit(&stp->sd_lock); 1963 } 1964 /* 1965 * Wake up any other processes that may have been 1966 * waiting on the lower stream. These will all 1967 * error out. 1968 */ 1969 mutex_enter(&stpdown->sd_lock); 1970 /* The passthru module is removed so we may release STRPLUMB */ 1971 stpdown->sd_flag &= ~STRPLUMB; 1972 cv_broadcast(&rq->q_wait); 1973 cv_broadcast(&_WR(rq)->q_wait); 1974 cv_broadcast(&stpdown->sd_monitor); 1975 mutex_exit(&stpdown->sd_lock); 1976 mutex_exit(&muxifier); 1977 *rvalp = linkp->li_lblk.l_index; 1978 netstack_rele(ss->ss_netstack); 1979 return (0); 1980 } 1981 1982 int 1983 mlink(vnode_t *vp, int cmd, int arg, cred_t *crp, int *rvalp, int lhlink) 1984 { 1985 int ret; 1986 struct file *fpdown; 1987 1988 fpdown = getf(arg); 1989 ret = mlink_file(vp, cmd, fpdown, crp, rvalp, lhlink); 1990 if (fpdown != NULL) 1991 releasef(arg); 1992 return (ret); 1993 } 1994 1995 /* 1996 * Unlink a multiplexor link. Stp is the controlling stream for the 1997 * link, and linkp points to the link's entry in the linkinfo list. 1998 * The muxifier lock must be held on entry and is dropped on exit. 1999 * 2000 * NOTE : Currently it is assumed that mux would process all the messages 2001 * sitting on it's queue before ACKing the UNLINK. It is the responsibility 2002 * of the mux to handle all the messages that arrive before UNLINK. 2003 * If the mux has to send down messages on its lower stream before 2004 * ACKing I_UNLINK, then it *should* know to handle messages even 2005 * after the UNLINK is acked (actually it should be able to handle till we 2006 * re-block the read side of the pass queue here). If the mux does not 2007 * open up the lower stream, any messages that arrive during UNLINK 2008 * will be put in the stream head. In the case of lower stream opening 2009 * up, some messages might land in the stream head depending on when 2010 * the message arrived and when the read side of the pass queue was 2011 * re-blocked. 2012 */ 2013 int 2014 munlink(stdata_t *stp, linkinfo_t *linkp, int flag, cred_t *crp, int *rvalp, 2015 str_stack_t *ss) 2016 { 2017 struct strioctl strioc; 2018 struct stdata *stpdown; 2019 queue_t *rq, *wrq; 2020 queue_t *passq; 2021 syncq_t *passyncq; 2022 int error = 0; 2023 file_t *fpdown; 2024 2025 ASSERT(MUTEX_HELD(&muxifier)); 2026 2027 stpdown = linkp->li_fpdown->f_vnode->v_stream; 2028 2029 /* 2030 * See the comment in mlink() concerning STRPLUMB/STPLEX flags. 2031 */ 2032 mutex_enter(&stpdown->sd_lock); 2033 stpdown->sd_flag |= STRPLUMB; 2034 mutex_exit(&stpdown->sd_lock); 2035 2036 /* 2037 * Add passthru queue below lower mux. This will block 2038 * syncqs of lower muxs read queue during I_LINK/I_UNLINK. 2039 */ 2040 passq = link_addpassthru(stpdown); 2041 2042 if ((flag & LINKTYPEMASK) == LINKNORMAL) 2043 strioc.ic_cmd = I_UNLINK; 2044 else 2045 strioc.ic_cmd = I_PUNLINK; 2046 strioc.ic_timout = INFTIM; 2047 strioc.ic_len = sizeof (struct linkblk); 2048 strioc.ic_dp = (char *)&linkp->li_lblk; 2049 2050 error = strdoioctl(stp, &strioc, FNATIVE, 2051 K_TO_K | STR_NOERROR | STR_NOSIG, crp, rvalp); 2052 2053 /* 2054 * If there was an error and this is not called via strclose, 2055 * return to the user. Otherwise, pretend there was no error 2056 * and close the link. 2057 */ 2058 if (error) { 2059 if (flag & LINKCLOSE) { 2060 cmn_err(CE_WARN, "KERNEL: munlink: could not perform " 2061 "unlink ioctl, closing anyway (%d)\n", error); 2062 } else { 2063 link_rempassthru(passq); 2064 mutex_enter(&stpdown->sd_lock); 2065 stpdown->sd_flag &= ~STRPLUMB; 2066 cv_broadcast(&stpdown->sd_monitor); 2067 mutex_exit(&stpdown->sd_lock); 2068 mutex_exit(&muxifier); 2069 return (error); 2070 } 2071 } 2072 2073 mux_rmvedge(stp, linkp->li_lblk.l_index, ss); 2074 fpdown = linkp->li_fpdown; 2075 lbfree(linkp); 2076 2077 /* 2078 * We go ahead and drop muxifier here--it's a nasty global lock that 2079 * can slow others down. It's okay to since attempts to mlink() this 2080 * stream will be stopped because STPLEX is still set in the stdata 2081 * structure, and munlink() is stopped because mux_rmvedge() and 2082 * lbfree() have removed it from mux_nodes[] and linkinfo_list, 2083 * respectively. Note that we defer the closef() of fpdown until 2084 * after we drop muxifier since strclose() can call munlinkall(). 2085 */ 2086 mutex_exit(&muxifier); 2087 2088 wrq = stpdown->sd_wrq; 2089 rq = _RD(wrq); 2090 2091 /* 2092 * Get rid of outstanding service procedure runs, before we make 2093 * it a stream head, since a stream head doesn't have any service 2094 * procedure. 2095 */ 2096 disable_svc(rq); 2097 wait_svc(rq); 2098 2099 /* 2100 * Since we don't disable the syncq for QPERMOD, we wait for whatever 2101 * is queued up to be finished. mux should take care that nothing is 2102 * send down to this queue. We should do it now as we're going to block 2103 * passyncq if it was unblocked. 2104 */ 2105 if (wrq->q_flag & QPERMOD) { 2106 syncq_t *sq = wrq->q_syncq; 2107 2108 mutex_enter(SQLOCK(sq)); 2109 while (wrq->q_sqflags & Q_SQQUEUED) { 2110 sq->sq_flags |= SQ_WANTWAKEUP; 2111 cv_wait(&sq->sq_wait, SQLOCK(sq)); 2112 } 2113 mutex_exit(SQLOCK(sq)); 2114 } 2115 passyncq = passq->q_syncq; 2116 if (!(passyncq->sq_flags & SQ_BLOCKED)) { 2117 2118 syncq_t *sq, *outer; 2119 2120 /* 2121 * Messages could be flowing from underneath. We will 2122 * block the read side of the passq. This would be 2123 * sufficient for QPAIR and QPERQ muxes to ensure 2124 * that no data is flowing up into this queue 2125 * and hence no thread active in this instance of 2126 * lower mux. But for QPERMOD and QMTOUTPERIM there 2127 * could be messages on the inner and outer/inner 2128 * syncqs respectively. We will wait for them to drain. 2129 * Because passq is blocked messages end up in the syncq 2130 * And qfill_syncq could possibly end up setting QFULL 2131 * which will access the rq->q_flag. Hence, we have to 2132 * acquire the QLOCK in setq. 2133 * 2134 * XXX Messages can also flow from top into this 2135 * queue though the unlink is over (Ex. some instance 2136 * in putnext() called from top that has still not 2137 * accessed this queue. And also putq(lowerq) ?). 2138 * Solution : How about blocking the l_qtop queue ? 2139 * Do we really care about such pure D_MP muxes ? 2140 */ 2141 2142 blocksq(passyncq, SQ_BLOCKED, 0); 2143 2144 sq = rq->q_syncq; 2145 if ((outer = sq->sq_outer) != NULL) { 2146 2147 /* 2148 * We have to just wait for the outer sq_count 2149 * drop to zero. As this does not prevent new 2150 * messages to enter the outer perimeter, this 2151 * is subject to starvation. 2152 * 2153 * NOTE :Because of blocksq above, messages could 2154 * be in the inner syncq only because of some 2155 * thread holding the outer perimeter exclusively. 2156 * Hence it would be sufficient to wait for the 2157 * exclusive holder of the outer perimeter to drain 2158 * the inner and outer syncqs. But we will not depend 2159 * on this feature and hence check the inner syncqs 2160 * separately. 2161 */ 2162 wait_syncq(outer); 2163 } 2164 2165 2166 /* 2167 * There could be messages destined for 2168 * this queue. Let the exclusive holder 2169 * drain it. 2170 */ 2171 2172 wait_syncq(sq); 2173 ASSERT((rq->q_flag & QPERMOD) || 2174 ((rq->q_syncq->sq_head == NULL) && 2175 (_WR(rq)->q_syncq->sq_head == NULL))); 2176 } 2177 2178 /* 2179 * We haven't taken care of QPERMOD case yet. QPERMOD is a special 2180 * case as we don't disable its syncq or remove it off the syncq 2181 * service list. 2182 */ 2183 if (rq->q_flag & QPERMOD) { 2184 syncq_t *sq = rq->q_syncq; 2185 2186 mutex_enter(SQLOCK(sq)); 2187 while (rq->q_sqflags & Q_SQQUEUED) { 2188 sq->sq_flags |= SQ_WANTWAKEUP; 2189 cv_wait(&sq->sq_wait, SQLOCK(sq)); 2190 } 2191 mutex_exit(SQLOCK(sq)); 2192 } 2193 2194 /* 2195 * flush_syncq changes states only when there is some messages to 2196 * free. ie when it returns non-zero value to return. 2197 */ 2198 ASSERT(flush_syncq(rq->q_syncq, rq) == 0); 2199 ASSERT(flush_syncq(wrq->q_syncq, wrq) == 0); 2200 2201 /* 2202 * No body else should know about this queue now. 2203 * If the mux did not process the messages before 2204 * acking the I_UNLINK, free them now. 2205 */ 2206 2207 flushq(rq, FLUSHALL); 2208 flushq(_WR(rq), FLUSHALL); 2209 2210 /* 2211 * Convert the mux lower queue into a stream head queue. 2212 * Turn off STPLEX before we turn on the stream by removing the passq. 2213 */ 2214 rq->q_ptr = wrq->q_ptr = stpdown; 2215 setq(rq, &strdata, &stwdata, NULL, QMTSAFE, SQ_CI|SQ_CO, B_TRUE); 2216 2217 ASSERT((rq->q_flag & QMT_TYPEMASK) == QMTSAFE); 2218 ASSERT(rq->q_syncq == SQ(rq) && _WR(rq)->q_syncq == SQ(rq)); 2219 2220 enable_svc(rq); 2221 2222 /* 2223 * Now it is a proper stream, so STPLEX is cleared. But STRPLUMB still 2224 * needs to be set to prevent reopen() of the stream - such reopen may 2225 * try to call non-existent pass queue open routine and panic. 2226 */ 2227 mutex_enter(&stpdown->sd_lock); 2228 stpdown->sd_flag &= ~STPLEX; 2229 mutex_exit(&stpdown->sd_lock); 2230 2231 ASSERT(((flag & LINKTYPEMASK) == LINKNORMAL) || 2232 ((flag & LINKTYPEMASK) == LINKPERSIST)); 2233 2234 /* clean up the layered driver linkages */ 2235 if ((flag & LINKTYPEMASK) == LINKNORMAL) { 2236 ldi_munlink_fp(stp, fpdown, LINKNORMAL); 2237 } else { 2238 ldi_munlink_fp(stp, fpdown, LINKPERSIST); 2239 } 2240 2241 link_rempassthru(passq); 2242 2243 /* 2244 * Now all plumbing changes are finished and STRPLUMB is no 2245 * longer needed. 2246 */ 2247 mutex_enter(&stpdown->sd_lock); 2248 stpdown->sd_flag &= ~STRPLUMB; 2249 cv_broadcast(&stpdown->sd_monitor); 2250 mutex_exit(&stpdown->sd_lock); 2251 2252 (void) closef(fpdown); 2253 return (0); 2254 } 2255 2256 /* 2257 * Unlink all multiplexor links for which stp is the controlling stream. 2258 * Return 0, or a non-zero errno on failure. 2259 */ 2260 int 2261 munlinkall(stdata_t *stp, int flag, cred_t *crp, int *rvalp, str_stack_t *ss) 2262 { 2263 linkinfo_t *linkp; 2264 int error = 0; 2265 2266 mutex_enter(&muxifier); 2267 while (linkp = findlinks(stp, 0, flag, ss)) { 2268 /* 2269 * munlink() releases the muxifier lock. 2270 */ 2271 if (error = munlink(stp, linkp, flag, crp, rvalp, ss)) 2272 return (error); 2273 mutex_enter(&muxifier); 2274 } 2275 mutex_exit(&muxifier); 2276 return (0); 2277 } 2278 2279 /* 2280 * A multiplexor link has been made. Add an 2281 * edge to the directed graph. 2282 */ 2283 void 2284 mux_addedge(stdata_t *upstp, stdata_t *lostp, int muxid, str_stack_t *ss) 2285 { 2286 struct mux_node *np; 2287 struct mux_edge *ep; 2288 major_t upmaj; 2289 major_t lomaj; 2290 2291 upmaj = getmajor(upstp->sd_vnode->v_rdev); 2292 lomaj = getmajor(lostp->sd_vnode->v_rdev); 2293 np = &ss->ss_mux_nodes[upmaj]; 2294 if (np->mn_outp) { 2295 ep = np->mn_outp; 2296 while (ep->me_nextp) 2297 ep = ep->me_nextp; 2298 ep->me_nextp = kmem_alloc(sizeof (struct mux_edge), KM_SLEEP); 2299 ep = ep->me_nextp; 2300 } else { 2301 np->mn_outp = kmem_alloc(sizeof (struct mux_edge), KM_SLEEP); 2302 ep = np->mn_outp; 2303 } 2304 ep->me_nextp = NULL; 2305 ep->me_muxid = muxid; 2306 /* 2307 * Save the dev_t for the purposes of str_stack_shutdown. 2308 * str_stack_shutdown assumes that the device allows reopen, since 2309 * this dev_t is the one after any cloning by xx_open(). 2310 * Would prefer finding the dev_t from before any cloning, 2311 * but specfs doesn't retain that. 2312 */ 2313 ep->me_dev = upstp->sd_vnode->v_rdev; 2314 if (lostp->sd_vnode->v_type == VFIFO) 2315 ep->me_nodep = NULL; 2316 else 2317 ep->me_nodep = &ss->ss_mux_nodes[lomaj]; 2318 } 2319 2320 /* 2321 * A multiplexor link has been removed. Remove the 2322 * edge in the directed graph. 2323 */ 2324 void 2325 mux_rmvedge(stdata_t *upstp, int muxid, str_stack_t *ss) 2326 { 2327 struct mux_node *np; 2328 struct mux_edge *ep; 2329 struct mux_edge *pep = NULL; 2330 major_t upmaj; 2331 2332 upmaj = getmajor(upstp->sd_vnode->v_rdev); 2333 np = &ss->ss_mux_nodes[upmaj]; 2334 ASSERT(np->mn_outp != NULL); 2335 ep = np->mn_outp; 2336 while (ep) { 2337 if (ep->me_muxid == muxid) { 2338 if (pep) 2339 pep->me_nextp = ep->me_nextp; 2340 else 2341 np->mn_outp = ep->me_nextp; 2342 kmem_free(ep, sizeof (struct mux_edge)); 2343 return; 2344 } 2345 pep = ep; 2346 ep = ep->me_nextp; 2347 } 2348 ASSERT(0); /* should not reach here */ 2349 } 2350 2351 /* 2352 * Translate the device flags (from conf.h) to the corresponding 2353 * qflag and sq_flag (type) values. 2354 */ 2355 int 2356 devflg_to_qflag(struct streamtab *stp, uint32_t devflag, uint32_t *qflagp, 2357 uint32_t *sqtypep) 2358 { 2359 uint32_t qflag = 0; 2360 uint32_t sqtype = 0; 2361 2362 if (devflag & _D_OLD) 2363 goto bad; 2364 2365 /* Inner perimeter presence and scope */ 2366 switch (devflag & D_MTINNER_MASK) { 2367 case D_MP: 2368 qflag |= QMTSAFE; 2369 sqtype |= SQ_CI; 2370 break; 2371 case D_MTPERQ|D_MP: 2372 qflag |= QPERQ; 2373 break; 2374 case D_MTQPAIR|D_MP: 2375 qflag |= QPAIR; 2376 break; 2377 case D_MTPERMOD|D_MP: 2378 qflag |= QPERMOD; 2379 break; 2380 default: 2381 goto bad; 2382 } 2383 2384 /* Outer perimeter */ 2385 if (devflag & D_MTOUTPERIM) { 2386 switch (devflag & D_MTINNER_MASK) { 2387 case D_MP: 2388 case D_MTPERQ|D_MP: 2389 case D_MTQPAIR|D_MP: 2390 break; 2391 default: 2392 goto bad; 2393 } 2394 qflag |= QMTOUTPERIM; 2395 } 2396 2397 /* Inner perimeter modifiers */ 2398 if (devflag & D_MTINNER_MOD) { 2399 switch (devflag & D_MTINNER_MASK) { 2400 case D_MP: 2401 goto bad; 2402 default: 2403 break; 2404 } 2405 if (devflag & D_MTPUTSHARED) 2406 sqtype |= SQ_CIPUT; 2407 if (devflag & _D_MTOCSHARED) { 2408 /* 2409 * The code in putnext assumes that it has the 2410 * highest concurrency by not checking sq_count. 2411 * Thus _D_MTOCSHARED can only be supported when 2412 * D_MTPUTSHARED is set. 2413 */ 2414 if (!(devflag & D_MTPUTSHARED)) 2415 goto bad; 2416 sqtype |= SQ_CIOC; 2417 } 2418 if (devflag & _D_MTCBSHARED) { 2419 /* 2420 * The code in putnext assumes that it has the 2421 * highest concurrency by not checking sq_count. 2422 * Thus _D_MTCBSHARED can only be supported when 2423 * D_MTPUTSHARED is set. 2424 */ 2425 if (!(devflag & D_MTPUTSHARED)) 2426 goto bad; 2427 sqtype |= SQ_CICB; 2428 } 2429 if (devflag & _D_MTSVCSHARED) { 2430 /* 2431 * The code in putnext assumes that it has the 2432 * highest concurrency by not checking sq_count. 2433 * Thus _D_MTSVCSHARED can only be supported when 2434 * D_MTPUTSHARED is set. Also _D_MTSVCSHARED is 2435 * supported only for QPERMOD. 2436 */ 2437 if (!(devflag & D_MTPUTSHARED) || !(qflag & QPERMOD)) 2438 goto bad; 2439 sqtype |= SQ_CISVC; 2440 } 2441 } 2442 2443 /* Default outer perimeter concurrency */ 2444 sqtype |= SQ_CO; 2445 2446 /* Outer perimeter modifiers */ 2447 if (devflag & D_MTOCEXCL) { 2448 if (!(devflag & D_MTOUTPERIM)) { 2449 /* No outer perimeter */ 2450 goto bad; 2451 } 2452 sqtype &= ~SQ_COOC; 2453 } 2454 2455 /* Synchronous Streams extended qinit structure */ 2456 if (devflag & D_SYNCSTR) 2457 qflag |= QSYNCSTR; 2458 2459 /* 2460 * Private flag used by a transport module to indicate 2461 * to sockfs that it supports direct-access mode without 2462 * having to go through STREAMS or the transport can use 2463 * sodirect_t sharing to bypass STREAMS for receive-side 2464 * M_DATA processing. 2465 */ 2466 if (devflag & (_D_DIRECT|_D_SODIRECT)) { 2467 /* Reject unless the module is fully-MT (no perimeter) */ 2468 if ((qflag & QMT_TYPEMASK) != QMTSAFE) 2469 goto bad; 2470 if (devflag & _D_DIRECT) 2471 qflag |= _QDIRECT; 2472 if (devflag & _D_SODIRECT) 2473 qflag |= _QSODIRECT; 2474 } 2475 2476 *qflagp = qflag; 2477 *sqtypep = sqtype; 2478 return (0); 2479 2480 bad: 2481 cmn_err(CE_WARN, 2482 "stropen: bad MT flags (0x%x) in driver '%s'", 2483 (int)(qflag & D_MTSAFETY_MASK), 2484 stp->st_rdinit->qi_minfo->mi_idname); 2485 2486 return (EINVAL); 2487 } 2488 2489 /* 2490 * Set the interface values for a pair of queues (qinit structure, 2491 * packet sizes, water marks). 2492 * setq assumes that the caller does not have a claim (entersq or claimq) 2493 * on the queue. 2494 */ 2495 void 2496 setq(queue_t *rq, struct qinit *rinit, struct qinit *winit, 2497 perdm_t *dmp, uint32_t qflag, uint32_t sqtype, boolean_t lock_needed) 2498 { 2499 queue_t *wq; 2500 syncq_t *sq, *outer; 2501 2502 ASSERT(rq->q_flag & QREADR); 2503 ASSERT((qflag & QMT_TYPEMASK) != 0); 2504 IMPLY((qflag & (QPERMOD | QMTOUTPERIM)), dmp != NULL); 2505 2506 wq = _WR(rq); 2507 rq->q_qinfo = rinit; 2508 rq->q_hiwat = rinit->qi_minfo->mi_hiwat; 2509 rq->q_lowat = rinit->qi_minfo->mi_lowat; 2510 rq->q_minpsz = rinit->qi_minfo->mi_minpsz; 2511 rq->q_maxpsz = rinit->qi_minfo->mi_maxpsz; 2512 wq->q_qinfo = winit; 2513 wq->q_hiwat = winit->qi_minfo->mi_hiwat; 2514 wq->q_lowat = winit->qi_minfo->mi_lowat; 2515 wq->q_minpsz = winit->qi_minfo->mi_minpsz; 2516 wq->q_maxpsz = winit->qi_minfo->mi_maxpsz; 2517 2518 /* Remove old syncqs */ 2519 sq = rq->q_syncq; 2520 outer = sq->sq_outer; 2521 if (outer != NULL) { 2522 ASSERT(wq->q_syncq->sq_outer == outer); 2523 outer_remove(outer, rq->q_syncq); 2524 if (wq->q_syncq != rq->q_syncq) 2525 outer_remove(outer, wq->q_syncq); 2526 } 2527 ASSERT(sq->sq_outer == NULL); 2528 ASSERT(sq->sq_onext == NULL && sq->sq_oprev == NULL); 2529 2530 if (sq != SQ(rq)) { 2531 if (!(rq->q_flag & QPERMOD)) 2532 free_syncq(sq); 2533 if (wq->q_syncq == rq->q_syncq) 2534 wq->q_syncq = NULL; 2535 rq->q_syncq = NULL; 2536 } 2537 if (wq->q_syncq != NULL && wq->q_syncq != sq && 2538 wq->q_syncq != SQ(rq)) { 2539 free_syncq(wq->q_syncq); 2540 wq->q_syncq = NULL; 2541 } 2542 ASSERT(rq->q_syncq == NULL || (rq->q_syncq->sq_head == NULL && 2543 rq->q_syncq->sq_tail == NULL)); 2544 ASSERT(wq->q_syncq == NULL || (wq->q_syncq->sq_head == NULL && 2545 wq->q_syncq->sq_tail == NULL)); 2546 2547 if (!(rq->q_flag & QPERMOD) && 2548 rq->q_syncq != NULL && rq->q_syncq->sq_ciputctrl != NULL) { 2549 ASSERT(rq->q_syncq->sq_nciputctrl == n_ciputctrl - 1); 2550 SUMCHECK_CIPUTCTRL_COUNTS(rq->q_syncq->sq_ciputctrl, 2551 rq->q_syncq->sq_nciputctrl, 0); 2552 ASSERT(ciputctrl_cache != NULL); 2553 kmem_cache_free(ciputctrl_cache, rq->q_syncq->sq_ciputctrl); 2554 rq->q_syncq->sq_ciputctrl = NULL; 2555 rq->q_syncq->sq_nciputctrl = 0; 2556 } 2557 2558 if (!(wq->q_flag & QPERMOD) && 2559 wq->q_syncq != NULL && wq->q_syncq->sq_ciputctrl != NULL) { 2560 ASSERT(wq->q_syncq->sq_nciputctrl == n_ciputctrl - 1); 2561 SUMCHECK_CIPUTCTRL_COUNTS(wq->q_syncq->sq_ciputctrl, 2562 wq->q_syncq->sq_nciputctrl, 0); 2563 ASSERT(ciputctrl_cache != NULL); 2564 kmem_cache_free(ciputctrl_cache, wq->q_syncq->sq_ciputctrl); 2565 wq->q_syncq->sq_ciputctrl = NULL; 2566 wq->q_syncq->sq_nciputctrl = 0; 2567 } 2568 2569 sq = SQ(rq); 2570 ASSERT(sq->sq_head == NULL && sq->sq_tail == NULL); 2571 ASSERT(sq->sq_outer == NULL); 2572 ASSERT(sq->sq_onext == NULL && sq->sq_oprev == NULL); 2573 2574 /* 2575 * Create syncqs based on qflag and sqtype. Set the SQ_TYPES_IN_FLAGS 2576 * bits in sq_flag based on the sqtype. 2577 */ 2578 ASSERT((sq->sq_flags & ~SQ_TYPES_IN_FLAGS) == 0); 2579 2580 rq->q_syncq = wq->q_syncq = sq; 2581 sq->sq_type = sqtype; 2582 sq->sq_flags = (sqtype & SQ_TYPES_IN_FLAGS); 2583 2584 /* 2585 * We are making sq_svcflags zero, 2586 * resetting SQ_DISABLED in case it was set by 2587 * wait_svc() in the munlink path. 2588 * 2589 */ 2590 ASSERT((sq->sq_svcflags & SQ_SERVICE) == 0); 2591 sq->sq_svcflags = 0; 2592 2593 /* 2594 * We need to acquire the lock here for the mlink and munlink case, 2595 * where canputnext, backenable, etc can access the q_flag. 2596 */ 2597 if (lock_needed) { 2598 mutex_enter(QLOCK(rq)); 2599 rq->q_flag = (rq->q_flag & ~QMT_TYPEMASK) | QWANTR | qflag; 2600 mutex_exit(QLOCK(rq)); 2601 mutex_enter(QLOCK(wq)); 2602 wq->q_flag = (wq->q_flag & ~QMT_TYPEMASK) | QWANTR | qflag; 2603 mutex_exit(QLOCK(wq)); 2604 } else { 2605 rq->q_flag = (rq->q_flag & ~QMT_TYPEMASK) | QWANTR | qflag; 2606 wq->q_flag = (wq->q_flag & ~QMT_TYPEMASK) | QWANTR | qflag; 2607 } 2608 2609 if (qflag & QPERQ) { 2610 /* Allocate a separate syncq for the write side */ 2611 sq = new_syncq(); 2612 sq->sq_type = rq->q_syncq->sq_type; 2613 sq->sq_flags = rq->q_syncq->sq_flags; 2614 ASSERT(sq->sq_outer == NULL && sq->sq_onext == NULL && 2615 sq->sq_oprev == NULL); 2616 wq->q_syncq = sq; 2617 } 2618 if (qflag & QPERMOD) { 2619 sq = dmp->dm_sq; 2620 2621 /* 2622 * Assert that we do have an inner perimeter syncq and that it 2623 * does not have an outer perimeter associated with it. 2624 */ 2625 ASSERT(sq->sq_outer == NULL && sq->sq_onext == NULL && 2626 sq->sq_oprev == NULL); 2627 rq->q_syncq = wq->q_syncq = sq; 2628 } 2629 if (qflag & QMTOUTPERIM) { 2630 outer = dmp->dm_sq; 2631 2632 ASSERT(outer->sq_outer == NULL); 2633 outer_insert(outer, rq->q_syncq); 2634 if (wq->q_syncq != rq->q_syncq) 2635 outer_insert(outer, wq->q_syncq); 2636 } 2637 ASSERT((rq->q_syncq->sq_flags & SQ_TYPES_IN_FLAGS) == 2638 (rq->q_syncq->sq_type & SQ_TYPES_IN_FLAGS)); 2639 ASSERT((wq->q_syncq->sq_flags & SQ_TYPES_IN_FLAGS) == 2640 (wq->q_syncq->sq_type & SQ_TYPES_IN_FLAGS)); 2641 ASSERT((rq->q_flag & QMT_TYPEMASK) == (qflag & QMT_TYPEMASK)); 2642 2643 /* 2644 * Initialize struio() types. 2645 */ 2646 rq->q_struiot = 2647 (rq->q_flag & QSYNCSTR) ? rinit->qi_struiot : STRUIOT_NONE; 2648 wq->q_struiot = 2649 (wq->q_flag & QSYNCSTR) ? winit->qi_struiot : STRUIOT_NONE; 2650 } 2651 2652 perdm_t * 2653 hold_dm(struct streamtab *str, uint32_t qflag, uint32_t sqtype) 2654 { 2655 syncq_t *sq; 2656 perdm_t **pp; 2657 perdm_t *p; 2658 perdm_t *dmp; 2659 2660 ASSERT(str != NULL); 2661 ASSERT(qflag & (QPERMOD | QMTOUTPERIM)); 2662 2663 rw_enter(&perdm_rwlock, RW_READER); 2664 for (p = perdm_list; p != NULL; p = p->dm_next) { 2665 if (p->dm_str == str) { /* found one */ 2666 atomic_add_32(&(p->dm_ref), 1); 2667 rw_exit(&perdm_rwlock); 2668 return (p); 2669 } 2670 } 2671 rw_exit(&perdm_rwlock); 2672 2673 sq = new_syncq(); 2674 if (qflag & QPERMOD) { 2675 sq->sq_type = sqtype | SQ_PERMOD; 2676 sq->sq_flags = sqtype & SQ_TYPES_IN_FLAGS; 2677 } else { 2678 ASSERT(qflag & QMTOUTPERIM); 2679 sq->sq_onext = sq->sq_oprev = sq; 2680 } 2681 2682 dmp = kmem_alloc(sizeof (perdm_t), KM_SLEEP); 2683 dmp->dm_sq = sq; 2684 dmp->dm_str = str; 2685 dmp->dm_ref = 1; 2686 dmp->dm_next = NULL; 2687 2688 rw_enter(&perdm_rwlock, RW_WRITER); 2689 for (pp = &perdm_list; (p = *pp) != NULL; pp = &(p->dm_next)) { 2690 if (p->dm_str == str) { /* already present */ 2691 p->dm_ref++; 2692 rw_exit(&perdm_rwlock); 2693 free_syncq(sq); 2694 kmem_free(dmp, sizeof (perdm_t)); 2695 return (p); 2696 } 2697 } 2698 2699 *pp = dmp; 2700 rw_exit(&perdm_rwlock); 2701 return (dmp); 2702 } 2703 2704 void 2705 rele_dm(perdm_t *dmp) 2706 { 2707 perdm_t **pp; 2708 perdm_t *p; 2709 2710 rw_enter(&perdm_rwlock, RW_WRITER); 2711 ASSERT(dmp->dm_ref > 0); 2712 2713 if (--dmp->dm_ref > 0) { 2714 rw_exit(&perdm_rwlock); 2715 return; 2716 } 2717 2718 for (pp = &perdm_list; (p = *pp) != NULL; pp = &(p->dm_next)) 2719 if (p == dmp) 2720 break; 2721 ASSERT(p == dmp); 2722 *pp = p->dm_next; 2723 rw_exit(&perdm_rwlock); 2724 2725 /* 2726 * Wait for any background processing that relies on the 2727 * syncq to complete before it is freed. 2728 */ 2729 wait_sq_svc(p->dm_sq); 2730 free_syncq(p->dm_sq); 2731 kmem_free(p, sizeof (perdm_t)); 2732 } 2733 2734 /* 2735 * Make a protocol message given control and data buffers. 2736 * n.b., this can block; be careful of what locks you hold when calling it. 2737 * 2738 * If sd_maxblk is less than *iosize this routine can fail part way through 2739 * (due to an allocation failure). In this case on return *iosize will contain 2740 * the amount that was consumed. Otherwise *iosize will not be modified 2741 * i.e. it will contain the amount that was consumed. 2742 */ 2743 int 2744 strmakemsg( 2745 struct strbuf *mctl, 2746 ssize_t *iosize, 2747 struct uio *uiop, 2748 stdata_t *stp, 2749 int32_t flag, 2750 mblk_t **mpp) 2751 { 2752 mblk_t *mpctl = NULL; 2753 mblk_t *mpdata = NULL; 2754 int error; 2755 2756 ASSERT(uiop != NULL); 2757 2758 *mpp = NULL; 2759 /* Create control part, if any */ 2760 if ((mctl != NULL) && (mctl->len >= 0)) { 2761 error = strmakectl(mctl, flag, uiop->uio_fmode, &mpctl); 2762 if (error) 2763 return (error); 2764 } 2765 /* Create data part, if any */ 2766 if (*iosize >= 0) { 2767 error = strmakedata(iosize, uiop, stp, flag, &mpdata); 2768 if (error) { 2769 freemsg(mpctl); 2770 return (error); 2771 } 2772 } 2773 if (mpctl != NULL) { 2774 if (mpdata != NULL) 2775 linkb(mpctl, mpdata); 2776 *mpp = mpctl; 2777 } else { 2778 *mpp = mpdata; 2779 } 2780 return (0); 2781 } 2782 2783 /* 2784 * Make the control part of a protocol message given a control buffer. 2785 * n.b., this can block; be careful of what locks you hold when calling it. 2786 */ 2787 int 2788 strmakectl( 2789 struct strbuf *mctl, 2790 int32_t flag, 2791 int32_t fflag, 2792 mblk_t **mpp) 2793 { 2794 mblk_t *bp = NULL; 2795 unsigned char msgtype; 2796 int error = 0; 2797 2798 *mpp = NULL; 2799 /* 2800 * Create control part of message, if any. 2801 */ 2802 if ((mctl != NULL) && (mctl->len >= 0)) { 2803 caddr_t base; 2804 int ctlcount; 2805 int allocsz; 2806 2807 if (flag & RS_HIPRI) 2808 msgtype = M_PCPROTO; 2809 else 2810 msgtype = M_PROTO; 2811 2812 ctlcount = mctl->len; 2813 base = mctl->buf; 2814 2815 /* 2816 * Give modules a better chance to reuse M_PROTO/M_PCPROTO 2817 * blocks by increasing the size to something more usable. 2818 */ 2819 allocsz = MAX(ctlcount, 64); 2820 2821 /* 2822 * Range checking has already been done; simply try 2823 * to allocate a message block for the ctl part. 2824 */ 2825 while (!(bp = allocb(allocsz, BPRI_MED))) { 2826 if (fflag & (FNDELAY|FNONBLOCK)) 2827 return (EAGAIN); 2828 if (error = strwaitbuf(allocsz, BPRI_MED)) 2829 return (error); 2830 } 2831 2832 bp->b_datap->db_type = msgtype; 2833 if (copyin(base, bp->b_wptr, ctlcount)) { 2834 freeb(bp); 2835 return (EFAULT); 2836 } 2837 bp->b_wptr += ctlcount; 2838 } 2839 *mpp = bp; 2840 return (0); 2841 } 2842 2843 /* 2844 * Make a protocol message given data buffers. 2845 * n.b., this can block; be careful of what locks you hold when calling it. 2846 * 2847 * If sd_maxblk is less than *iosize this routine can fail part way through 2848 * (due to an allocation failure). In this case on return *iosize will contain 2849 * the amount that was consumed. Otherwise *iosize will not be modified 2850 * i.e. it will contain the amount that was consumed. 2851 */ 2852 int 2853 strmakedata( 2854 ssize_t *iosize, 2855 struct uio *uiop, 2856 stdata_t *stp, 2857 int32_t flag, 2858 mblk_t **mpp) 2859 { 2860 mblk_t *mp = NULL; 2861 mblk_t *bp; 2862 int wroff = (int)stp->sd_wroff; 2863 int tail_len = (int)stp->sd_tail; 2864 int extra = wroff + tail_len; 2865 int error = 0; 2866 ssize_t maxblk; 2867 ssize_t count = *iosize; 2868 cred_t *cr = CRED(); 2869 2870 *mpp = NULL; 2871 if (count < 0) 2872 return (0); 2873 2874 maxblk = stp->sd_maxblk; 2875 if (maxblk == INFPSZ) 2876 maxblk = count; 2877 2878 /* 2879 * Create data part of message, if any. 2880 */ 2881 do { 2882 ssize_t size; 2883 dblk_t *dp; 2884 2885 ASSERT(uiop); 2886 2887 size = MIN(count, maxblk); 2888 2889 while ((bp = allocb_cred(size + extra, cr)) == NULL) { 2890 error = EAGAIN; 2891 if ((uiop->uio_fmode & (FNDELAY|FNONBLOCK)) || 2892 (error = strwaitbuf(size + extra, BPRI_MED)) != 0) { 2893 if (count == *iosize) { 2894 freemsg(mp); 2895 return (error); 2896 } else { 2897 *iosize -= count; 2898 *mpp = mp; 2899 return (0); 2900 } 2901 } 2902 } 2903 dp = bp->b_datap; 2904 dp->db_cpid = curproc->p_pid; 2905 ASSERT(wroff <= dp->db_lim - bp->b_wptr); 2906 bp->b_wptr = bp->b_rptr = bp->b_rptr + wroff; 2907 2908 if (flag & STRUIO_POSTPONE) { 2909 /* 2910 * Setup the stream uio portion of the 2911 * dblk for subsequent use by struioget(). 2912 */ 2913 dp->db_struioflag = STRUIO_SPEC; 2914 dp->db_cksumstart = 0; 2915 dp->db_cksumstuff = 0; 2916 dp->db_cksumend = size; 2917 *(long long *)dp->db_struioun.data = 0ll; 2918 bp->b_wptr += size; 2919 } else { 2920 if (stp->sd_copyflag & STRCOPYCACHED) 2921 uiop->uio_extflg |= UIO_COPY_CACHED; 2922 2923 if (size != 0) { 2924 error = uiomove(bp->b_wptr, size, UIO_WRITE, 2925 uiop); 2926 if (error != 0) { 2927 freeb(bp); 2928 freemsg(mp); 2929 return (error); 2930 } 2931 } 2932 bp->b_wptr += size; 2933 2934 if (stp->sd_wputdatafunc != NULL) { 2935 mblk_t *newbp; 2936 2937 newbp = (stp->sd_wputdatafunc)(stp->sd_vnode, 2938 bp, NULL, NULL, NULL, NULL); 2939 if (newbp == NULL) { 2940 freeb(bp); 2941 freemsg(mp); 2942 return (ECOMM); 2943 } 2944 bp = newbp; 2945 } 2946 } 2947 2948 count -= size; 2949 2950 if (mp == NULL) 2951 mp = bp; 2952 else 2953 linkb(mp, bp); 2954 } while (count > 0); 2955 2956 *mpp = mp; 2957 return (0); 2958 } 2959 2960 /* 2961 * Wait for a buffer to become available. Return non-zero errno 2962 * if not able to wait, 0 if buffer is probably there. 2963 */ 2964 int 2965 strwaitbuf(size_t size, int pri) 2966 { 2967 bufcall_id_t id; 2968 2969 mutex_enter(&bcall_monitor); 2970 if ((id = bufcall(size, pri, (void (*)(void *))cv_broadcast, 2971 &ttoproc(curthread)->p_flag_cv)) == 0) { 2972 mutex_exit(&bcall_monitor); 2973 return (ENOSR); 2974 } 2975 if (!cv_wait_sig(&(ttoproc(curthread)->p_flag_cv), &bcall_monitor)) { 2976 unbufcall(id); 2977 mutex_exit(&bcall_monitor); 2978 return (EINTR); 2979 } 2980 unbufcall(id); 2981 mutex_exit(&bcall_monitor); 2982 return (0); 2983 } 2984 2985 /* 2986 * This function waits for a read or write event to happen on a stream. 2987 * fmode can specify FNDELAY and/or FNONBLOCK. 2988 * The timeout is in ms with -1 meaning infinite. 2989 * The flag values work as follows: 2990 * READWAIT Check for read side errors, send M_READ 2991 * GETWAIT Check for read side errors, no M_READ 2992 * WRITEWAIT Check for write side errors. 2993 * NOINTR Do not return error if nonblocking or timeout. 2994 * STR_NOERROR Ignore all errors except STPLEX. 2995 * STR_NOSIG Ignore/hold signals during the duration of the call. 2996 * STR_PEEK Pass through the strgeterr(). 2997 */ 2998 int 2999 strwaitq(stdata_t *stp, int flag, ssize_t count, int fmode, clock_t timout, 3000 int *done) 3001 { 3002 int slpflg, errs; 3003 int error; 3004 kcondvar_t *sleepon; 3005 mblk_t *mp; 3006 ssize_t *rd_count; 3007 clock_t rval; 3008 3009 ASSERT(MUTEX_HELD(&stp->sd_lock)); 3010 if ((flag & READWAIT) || (flag & GETWAIT)) { 3011 slpflg = RSLEEP; 3012 sleepon = &_RD(stp->sd_wrq)->q_wait; 3013 errs = STRDERR|STPLEX; 3014 } else { 3015 slpflg = WSLEEP; 3016 sleepon = &stp->sd_wrq->q_wait; 3017 errs = STWRERR|STRHUP|STPLEX; 3018 } 3019 if (flag & STR_NOERROR) 3020 errs = STPLEX; 3021 3022 if (stp->sd_wakeq & slpflg) { 3023 /* 3024 * A strwakeq() is pending, no need to sleep. 3025 */ 3026 stp->sd_wakeq &= ~slpflg; 3027 *done = 0; 3028 return (0); 3029 } 3030 3031 if (fmode & (FNDELAY|FNONBLOCK)) { 3032 if (!(flag & NOINTR)) 3033 error = EAGAIN; 3034 else 3035 error = 0; 3036 *done = 1; 3037 return (error); 3038 } 3039 3040 if (stp->sd_flag & errs) { 3041 /* 3042 * Check for errors before going to sleep since the 3043 * caller might not have checked this while holding 3044 * sd_lock. 3045 */ 3046 error = strgeterr(stp, errs, (flag & STR_PEEK)); 3047 if (error != 0) { 3048 *done = 1; 3049 return (error); 3050 } 3051 } 3052 3053 /* 3054 * If any module downstream has requested read notification 3055 * by setting SNDMREAD flag using M_SETOPTS, send a message 3056 * down stream. 3057 */ 3058 if ((flag & READWAIT) && (stp->sd_flag & SNDMREAD)) { 3059 mutex_exit(&stp->sd_lock); 3060 if (!(mp = allocb_wait(sizeof (ssize_t), BPRI_MED, 3061 (flag & STR_NOSIG), &error))) { 3062 mutex_enter(&stp->sd_lock); 3063 *done = 1; 3064 return (error); 3065 } 3066 mp->b_datap->db_type = M_READ; 3067 rd_count = (ssize_t *)mp->b_wptr; 3068 *rd_count = count; 3069 mp->b_wptr += sizeof (ssize_t); 3070 /* 3071 * Send the number of bytes requested by the 3072 * read as the argument to M_READ. 3073 */ 3074 stream_willservice(stp); 3075 putnext(stp->sd_wrq, mp); 3076 stream_runservice(stp); 3077 mutex_enter(&stp->sd_lock); 3078 3079 /* 3080 * If any data arrived due to inline processing 3081 * of putnext(), don't sleep. 3082 */ 3083 if (_RD(stp->sd_wrq)->q_first != NULL) { 3084 *done = 0; 3085 return (0); 3086 } 3087 } 3088 3089 stp->sd_flag |= slpflg; 3090 TRACE_5(TR_FAC_STREAMS_FR, TR_STRWAITQ_WAIT2, 3091 "strwaitq sleeps (2):%p, %X, %lX, %X, %p", 3092 stp, flag, count, fmode, done); 3093 3094 rval = str_cv_wait(sleepon, &stp->sd_lock, timout, flag & STR_NOSIG); 3095 if (rval > 0) { 3096 /* EMPTY */ 3097 TRACE_5(TR_FAC_STREAMS_FR, TR_STRWAITQ_WAKE2, 3098 "strwaitq awakes(2):%X, %X, %X, %X, %X", 3099 stp, flag, count, fmode, done); 3100 } else if (rval == 0) { 3101 TRACE_5(TR_FAC_STREAMS_FR, TR_STRWAITQ_INTR2, 3102 "strwaitq interrupt #2:%p, %X, %lX, %X, %p", 3103 stp, flag, count, fmode, done); 3104 stp->sd_flag &= ~slpflg; 3105 cv_broadcast(sleepon); 3106 if (!(flag & NOINTR)) 3107 error = EINTR; 3108 else 3109 error = 0; 3110 *done = 1; 3111 return (error); 3112 } else { 3113 /* timeout */ 3114 TRACE_5(TR_FAC_STREAMS_FR, TR_STRWAITQ_TIME, 3115 "strwaitq timeout:%p, %X, %lX, %X, %p", 3116 stp, flag, count, fmode, done); 3117 *done = 1; 3118 if (!(flag & NOINTR)) 3119 return (ETIME); 3120 else 3121 return (0); 3122 } 3123 /* 3124 * If the caller implements delayed errors (i.e. queued after data) 3125 * we can not check for errors here since data as well as an 3126 * error might have arrived at the stream head. We return to 3127 * have the caller check the read queue before checking for errors. 3128 */ 3129 if ((stp->sd_flag & errs) && !(flag & STR_DELAYERR)) { 3130 error = strgeterr(stp, errs, (flag & STR_PEEK)); 3131 if (error != 0) { 3132 *done = 1; 3133 return (error); 3134 } 3135 } 3136 *done = 0; 3137 return (0); 3138 } 3139 3140 /* 3141 * Perform job control discipline access checks. 3142 * Return 0 for success and the errno for failure. 3143 */ 3144 3145 #define cantsend(p, t, sig) \ 3146 (sigismember(&(p)->p_ignore, sig) || signal_is_blocked((t), sig)) 3147 3148 int 3149 straccess(struct stdata *stp, enum jcaccess mode) 3150 { 3151 extern kcondvar_t lbolt_cv; /* XXX: should be in a header file */ 3152 kthread_t *t = curthread; 3153 proc_t *p = ttoproc(t); 3154 sess_t *sp; 3155 3156 ASSERT(mutex_owned(&stp->sd_lock)); 3157 3158 if (stp->sd_sidp == NULL || stp->sd_vnode->v_type == VFIFO) 3159 return (0); 3160 3161 mutex_enter(&p->p_lock); /* protects p_pgidp */ 3162 3163 for (;;) { 3164 mutex_enter(&p->p_splock); /* protects p->p_sessp */ 3165 sp = p->p_sessp; 3166 mutex_enter(&sp->s_lock); /* protects sp->* */ 3167 3168 /* 3169 * If this is not the calling process's controlling terminal 3170 * or if the calling process is already in the foreground 3171 * then allow access. 3172 */ 3173 if (sp->s_dev != stp->sd_vnode->v_rdev || 3174 p->p_pgidp == stp->sd_pgidp) { 3175 mutex_exit(&sp->s_lock); 3176 mutex_exit(&p->p_splock); 3177 mutex_exit(&p->p_lock); 3178 return (0); 3179 } 3180 3181 /* 3182 * Check to see if controlling terminal has been deallocated. 3183 */ 3184 if (sp->s_vp == NULL) { 3185 if (!cantsend(p, t, SIGHUP)) 3186 sigtoproc(p, t, SIGHUP); 3187 mutex_exit(&sp->s_lock); 3188 mutex_exit(&p->p_splock); 3189 mutex_exit(&p->p_lock); 3190 return (EIO); 3191 } 3192 3193 mutex_exit(&sp->s_lock); 3194 mutex_exit(&p->p_splock); 3195 3196 if (mode == JCGETP) { 3197 mutex_exit(&p->p_lock); 3198 return (0); 3199 } 3200 3201 if (mode == JCREAD) { 3202 if (p->p_detached || cantsend(p, t, SIGTTIN)) { 3203 mutex_exit(&p->p_lock); 3204 return (EIO); 3205 } 3206 mutex_exit(&p->p_lock); 3207 mutex_exit(&stp->sd_lock); 3208 pgsignal(p->p_pgidp, SIGTTIN); 3209 mutex_enter(&stp->sd_lock); 3210 mutex_enter(&p->p_lock); 3211 } else { /* mode == JCWRITE or JCSETP */ 3212 if ((mode == JCWRITE && !(stp->sd_flag & STRTOSTOP)) || 3213 cantsend(p, t, SIGTTOU)) { 3214 mutex_exit(&p->p_lock); 3215 return (0); 3216 } 3217 if (p->p_detached) { 3218 mutex_exit(&p->p_lock); 3219 return (EIO); 3220 } 3221 mutex_exit(&p->p_lock); 3222 mutex_exit(&stp->sd_lock); 3223 pgsignal(p->p_pgidp, SIGTTOU); 3224 mutex_enter(&stp->sd_lock); 3225 mutex_enter(&p->p_lock); 3226 } 3227 3228 /* 3229 * We call cv_wait_sig_swap() to cause the appropriate 3230 * action for the jobcontrol signal to take place. 3231 * If the signal is being caught, we will take the 3232 * EINTR error return. Otherwise, the default action 3233 * of causing the process to stop will take place. 3234 * In this case, we rely on the periodic cv_broadcast() on 3235 * &lbolt_cv to wake us up to loop around and test again. 3236 * We can't get here if the signal is ignored or 3237 * if the current thread is blocking the signal. 3238 */ 3239 mutex_exit(&stp->sd_lock); 3240 if (!cv_wait_sig_swap(&lbolt_cv, &p->p_lock)) { 3241 mutex_exit(&p->p_lock); 3242 mutex_enter(&stp->sd_lock); 3243 return (EINTR); 3244 } 3245 mutex_exit(&p->p_lock); 3246 mutex_enter(&stp->sd_lock); 3247 mutex_enter(&p->p_lock); 3248 } 3249 } 3250 3251 /* 3252 * Return size of message of block type (bp->b_datap->db_type) 3253 */ 3254 size_t 3255 xmsgsize(mblk_t *bp) 3256 { 3257 unsigned char type; 3258 size_t count = 0; 3259 3260 type = bp->b_datap->db_type; 3261 3262 for (; bp; bp = bp->b_cont) { 3263 if (type != bp->b_datap->db_type) 3264 break; 3265 ASSERT(bp->b_wptr >= bp->b_rptr); 3266 count += bp->b_wptr - bp->b_rptr; 3267 } 3268 return (count); 3269 } 3270 3271 /* 3272 * Allocate a stream head. 3273 */ 3274 struct stdata * 3275 shalloc(queue_t *qp) 3276 { 3277 stdata_t *stp; 3278 3279 stp = kmem_cache_alloc(stream_head_cache, KM_SLEEP); 3280 3281 stp->sd_wrq = _WR(qp); 3282 stp->sd_strtab = NULL; 3283 stp->sd_iocid = 0; 3284 stp->sd_mate = NULL; 3285 stp->sd_freezer = NULL; 3286 stp->sd_refcnt = 0; 3287 stp->sd_wakeq = 0; 3288 stp->sd_anchor = 0; 3289 stp->sd_struiowrq = NULL; 3290 stp->sd_struiordq = NULL; 3291 stp->sd_struiodnak = 0; 3292 stp->sd_struionak = NULL; 3293 stp->sd_t_audit_data = NULL; 3294 stp->sd_rput_opt = 0; 3295 stp->sd_wput_opt = 0; 3296 stp->sd_read_opt = 0; 3297 stp->sd_rprotofunc = strrput_proto; 3298 stp->sd_rmiscfunc = strrput_misc; 3299 stp->sd_rderrfunc = stp->sd_wrerrfunc = NULL; 3300 stp->sd_rputdatafunc = stp->sd_wputdatafunc = NULL; 3301 stp->sd_ciputctrl = NULL; 3302 stp->sd_nciputctrl = 0; 3303 stp->sd_qhead = NULL; 3304 stp->sd_qtail = NULL; 3305 stp->sd_servid = NULL; 3306 stp->sd_nqueues = 0; 3307 stp->sd_svcflags = 0; 3308 stp->sd_copyflag = 0; 3309 3310 return (stp); 3311 } 3312 3313 /* 3314 * Free a stream head. 3315 */ 3316 void 3317 shfree(stdata_t *stp) 3318 { 3319 ASSERT(MUTEX_NOT_HELD(&stp->sd_lock)); 3320 3321 stp->sd_wrq = NULL; 3322 3323 mutex_enter(&stp->sd_qlock); 3324 while (stp->sd_svcflags & STRS_SCHEDULED) { 3325 STRSTAT(strwaits); 3326 cv_wait(&stp->sd_qcv, &stp->sd_qlock); 3327 } 3328 mutex_exit(&stp->sd_qlock); 3329 3330 if (stp->sd_ciputctrl != NULL) { 3331 ASSERT(stp->sd_nciputctrl == n_ciputctrl - 1); 3332 SUMCHECK_CIPUTCTRL_COUNTS(stp->sd_ciputctrl, 3333 stp->sd_nciputctrl, 0); 3334 ASSERT(ciputctrl_cache != NULL); 3335 kmem_cache_free(ciputctrl_cache, stp->sd_ciputctrl); 3336 stp->sd_ciputctrl = NULL; 3337 stp->sd_nciputctrl = 0; 3338 } 3339 ASSERT(stp->sd_qhead == NULL); 3340 ASSERT(stp->sd_qtail == NULL); 3341 ASSERT(stp->sd_nqueues == 0); 3342 kmem_cache_free(stream_head_cache, stp); 3343 } 3344 3345 /* 3346 * Allocate a pair of queues and a syncq for the pair 3347 */ 3348 queue_t * 3349 allocq(void) 3350 { 3351 queinfo_t *qip; 3352 queue_t *qp, *wqp; 3353 syncq_t *sq; 3354 3355 qip = kmem_cache_alloc(queue_cache, KM_SLEEP); 3356 3357 qp = &qip->qu_rqueue; 3358 wqp = &qip->qu_wqueue; 3359 sq = &qip->qu_syncq; 3360 3361 qp->q_last = NULL; 3362 qp->q_next = NULL; 3363 qp->q_ptr = NULL; 3364 qp->q_flag = QUSE | QREADR; 3365 qp->q_bandp = NULL; 3366 qp->q_stream = NULL; 3367 qp->q_syncq = sq; 3368 qp->q_nband = 0; 3369 qp->q_nfsrv = NULL; 3370 qp->q_draining = 0; 3371 qp->q_syncqmsgs = 0; 3372 qp->q_spri = 0; 3373 qp->q_qtstamp = 0; 3374 qp->q_sqtstamp = 0; 3375 qp->q_fp = NULL; 3376 3377 wqp->q_last = NULL; 3378 wqp->q_next = NULL; 3379 wqp->q_ptr = NULL; 3380 wqp->q_flag = QUSE; 3381 wqp->q_bandp = NULL; 3382 wqp->q_stream = NULL; 3383 wqp->q_syncq = sq; 3384 wqp->q_nband = 0; 3385 wqp->q_nfsrv = NULL; 3386 wqp->q_draining = 0; 3387 wqp->q_syncqmsgs = 0; 3388 wqp->q_qtstamp = 0; 3389 wqp->q_sqtstamp = 0; 3390 wqp->q_spri = 0; 3391 3392 sq->sq_count = 0; 3393 sq->sq_rmqcount = 0; 3394 sq->sq_flags = 0; 3395 sq->sq_type = 0; 3396 sq->sq_callbflags = 0; 3397 sq->sq_cancelid = 0; 3398 sq->sq_ciputctrl = NULL; 3399 sq->sq_nciputctrl = 0; 3400 sq->sq_needexcl = 0; 3401 sq->sq_svcflags = 0; 3402 3403 return (qp); 3404 } 3405 3406 /* 3407 * Free a pair of queues and the "attached" syncq. 3408 * Discard any messages left on the syncq(s), remove the syncq(s) from the 3409 * outer perimeter, and free the syncq(s) if they are not the "attached" syncq. 3410 */ 3411 void 3412 freeq(queue_t *qp) 3413 { 3414 qband_t *qbp, *nqbp; 3415 syncq_t *sq, *outer; 3416 queue_t *wqp = _WR(qp); 3417 3418 ASSERT(qp->q_flag & QREADR); 3419 3420 /* 3421 * If a previously dispatched taskq job is scheduled to run 3422 * sync_service() or a service routine is scheduled for the 3423 * queues about to be freed, wait here until all service is 3424 * done on the queue and all associated queues and syncqs. 3425 */ 3426 wait_svc(qp); 3427 3428 (void) flush_syncq(qp->q_syncq, qp); 3429 (void) flush_syncq(wqp->q_syncq, wqp); 3430 ASSERT(qp->q_syncqmsgs == 0 && wqp->q_syncqmsgs == 0); 3431 3432 /* 3433 * Flush the queues before q_next is set to NULL This is needed 3434 * in order to backenable any downstream queue before we go away. 3435 * Note: we are already removed from the stream so that the 3436 * backenabling will not cause any messages to be delivered to our 3437 * put procedures. 3438 */ 3439 flushq(qp, FLUSHALL); 3440 flushq(wqp, FLUSHALL); 3441 3442 /* Tidy up - removeq only does a half-remove from stream */ 3443 qp->q_next = wqp->q_next = NULL; 3444 ASSERT(!(qp->q_flag & QENAB)); 3445 ASSERT(!(wqp->q_flag & QENAB)); 3446 3447 outer = qp->q_syncq->sq_outer; 3448 if (outer != NULL) { 3449 outer_remove(outer, qp->q_syncq); 3450 if (wqp->q_syncq != qp->q_syncq) 3451 outer_remove(outer, wqp->q_syncq); 3452 } 3453 /* 3454 * Free any syncqs that are outside what allocq returned. 3455 */ 3456 if (qp->q_syncq != SQ(qp) && !(qp->q_flag & QPERMOD)) 3457 free_syncq(qp->q_syncq); 3458 if (qp->q_syncq != wqp->q_syncq && wqp->q_syncq != SQ(qp)) 3459 free_syncq(wqp->q_syncq); 3460 3461 ASSERT((qp->q_sqflags & (Q_SQQUEUED | Q_SQDRAINING)) == 0); 3462 ASSERT((wqp->q_sqflags & (Q_SQQUEUED | Q_SQDRAINING)) == 0); 3463 ASSERT(MUTEX_NOT_HELD(QLOCK(qp))); 3464 ASSERT(MUTEX_NOT_HELD(QLOCK(wqp))); 3465 sq = SQ(qp); 3466 ASSERT(MUTEX_NOT_HELD(SQLOCK(sq))); 3467 ASSERT(sq->sq_head == NULL && sq->sq_tail == NULL); 3468 ASSERT(sq->sq_outer == NULL); 3469 ASSERT(sq->sq_onext == NULL && sq->sq_oprev == NULL); 3470 ASSERT(sq->sq_callbpend == NULL); 3471 ASSERT(sq->sq_needexcl == 0); 3472 3473 if (sq->sq_ciputctrl != NULL) { 3474 ASSERT(sq->sq_nciputctrl == n_ciputctrl - 1); 3475 SUMCHECK_CIPUTCTRL_COUNTS(sq->sq_ciputctrl, 3476 sq->sq_nciputctrl, 0); 3477 ASSERT(ciputctrl_cache != NULL); 3478 kmem_cache_free(ciputctrl_cache, sq->sq_ciputctrl); 3479 sq->sq_ciputctrl = NULL; 3480 sq->sq_nciputctrl = 0; 3481 } 3482 3483 ASSERT(qp->q_first == NULL && wqp->q_first == NULL); 3484 ASSERT(qp->q_count == 0 && wqp->q_count == 0); 3485 ASSERT(qp->q_mblkcnt == 0 && wqp->q_mblkcnt == 0); 3486 3487 qp->q_flag &= ~QUSE; 3488 wqp->q_flag &= ~QUSE; 3489 3490 /* NOTE: Uncomment the assert below once bugid 1159635 is fixed. */ 3491 /* ASSERT((qp->q_flag & QWANTW) == 0 && (wqp->q_flag & QWANTW) == 0); */ 3492 3493 qbp = qp->q_bandp; 3494 while (qbp) { 3495 nqbp = qbp->qb_next; 3496 freeband(qbp); 3497 qbp = nqbp; 3498 } 3499 qbp = wqp->q_bandp; 3500 while (qbp) { 3501 nqbp = qbp->qb_next; 3502 freeband(qbp); 3503 qbp = nqbp; 3504 } 3505 kmem_cache_free(queue_cache, qp); 3506 } 3507 3508 /* 3509 * Allocate a qband structure. 3510 */ 3511 qband_t * 3512 allocband(void) 3513 { 3514 qband_t *qbp; 3515 3516 qbp = kmem_cache_alloc(qband_cache, KM_NOSLEEP); 3517 if (qbp == NULL) 3518 return (NULL); 3519 3520 qbp->qb_next = NULL; 3521 qbp->qb_count = 0; 3522 qbp->qb_mblkcnt = 0; 3523 qbp->qb_first = NULL; 3524 qbp->qb_last = NULL; 3525 qbp->qb_flag = 0; 3526 3527 return (qbp); 3528 } 3529 3530 /* 3531 * Free a qband structure. 3532 */ 3533 void 3534 freeband(qband_t *qbp) 3535 { 3536 kmem_cache_free(qband_cache, qbp); 3537 } 3538 3539 /* 3540 * Just like putnextctl(9F), except that allocb_wait() is used. 3541 * 3542 * Consolidation Private, and of course only callable from the stream head or 3543 * routines that may block. 3544 */ 3545 int 3546 putnextctl_wait(queue_t *q, int type) 3547 { 3548 mblk_t *bp; 3549 int error; 3550 3551 if ((datamsg(type) && (type != M_DELAY)) || 3552 (bp = allocb_wait(0, BPRI_HI, 0, &error)) == NULL) 3553 return (0); 3554 3555 bp->b_datap->db_type = (unsigned char)type; 3556 putnext(q, bp); 3557 return (1); 3558 } 3559 3560 /* 3561 * run any possible bufcalls. 3562 */ 3563 void 3564 runbufcalls(void) 3565 { 3566 strbufcall_t *bcp; 3567 3568 mutex_enter(&bcall_monitor); 3569 mutex_enter(&strbcall_lock); 3570 3571 if (strbcalls.bc_head) { 3572 size_t count; 3573 int nevent; 3574 3575 /* 3576 * count how many events are on the list 3577 * now so we can check to avoid looping 3578 * in low memory situations 3579 */ 3580 nevent = 0; 3581 for (bcp = strbcalls.bc_head; bcp; bcp = bcp->bc_next) 3582 nevent++; 3583 3584 /* 3585 * get estimate of available memory from kmem_avail(). 3586 * awake all bufcall functions waiting for 3587 * memory whose request could be satisfied 3588 * by 'count' memory and let 'em fight for it. 3589 */ 3590 count = kmem_avail(); 3591 while ((bcp = strbcalls.bc_head) != NULL && nevent) { 3592 STRSTAT(bufcalls); 3593 --nevent; 3594 if (bcp->bc_size <= count) { 3595 bcp->bc_executor = curthread; 3596 mutex_exit(&strbcall_lock); 3597 (*bcp->bc_func)(bcp->bc_arg); 3598 mutex_enter(&strbcall_lock); 3599 bcp->bc_executor = NULL; 3600 cv_broadcast(&bcall_cv); 3601 strbcalls.bc_head = bcp->bc_next; 3602 kmem_free(bcp, sizeof (strbufcall_t)); 3603 } else { 3604 /* 3605 * too big, try again later - note 3606 * that nevent was decremented above 3607 * so we won't retry this one on this 3608 * iteration of the loop 3609 */ 3610 if (bcp->bc_next != NULL) { 3611 strbcalls.bc_head = bcp->bc_next; 3612 bcp->bc_next = NULL; 3613 strbcalls.bc_tail->bc_next = bcp; 3614 strbcalls.bc_tail = bcp; 3615 } 3616 } 3617 } 3618 if (strbcalls.bc_head == NULL) 3619 strbcalls.bc_tail = NULL; 3620 } 3621 3622 mutex_exit(&strbcall_lock); 3623 mutex_exit(&bcall_monitor); 3624 } 3625 3626 3627 /* 3628 * actually run queue's service routine. 3629 */ 3630 static void 3631 runservice(queue_t *q) 3632 { 3633 qband_t *qbp; 3634 3635 ASSERT(q->q_qinfo->qi_srvp); 3636 again: 3637 entersq(q->q_syncq, SQ_SVC); 3638 TRACE_1(TR_FAC_STREAMS_FR, TR_QRUNSERVICE_START, 3639 "runservice starts:%p", q); 3640 3641 if (!(q->q_flag & QWCLOSE)) 3642 (*q->q_qinfo->qi_srvp)(q); 3643 3644 TRACE_1(TR_FAC_STREAMS_FR, TR_QRUNSERVICE_END, 3645 "runservice ends:(%p)", q); 3646 3647 leavesq(q->q_syncq, SQ_SVC); 3648 3649 mutex_enter(QLOCK(q)); 3650 if (q->q_flag & QENAB) { 3651 q->q_flag &= ~QENAB; 3652 mutex_exit(QLOCK(q)); 3653 goto again; 3654 } 3655 q->q_flag &= ~QINSERVICE; 3656 q->q_flag &= ~QBACK; 3657 for (qbp = q->q_bandp; qbp; qbp = qbp->qb_next) 3658 qbp->qb_flag &= ~QB_BACK; 3659 /* 3660 * Wakeup thread waiting for the service procedure 3661 * to be run (strclose and qdetach). 3662 */ 3663 cv_broadcast(&q->q_wait); 3664 3665 mutex_exit(QLOCK(q)); 3666 } 3667 3668 /* 3669 * Background processing of bufcalls. 3670 */ 3671 void 3672 streams_bufcall_service(void) 3673 { 3674 callb_cpr_t cprinfo; 3675 3676 CALLB_CPR_INIT(&cprinfo, &strbcall_lock, callb_generic_cpr, 3677 "streams_bufcall_service"); 3678 3679 mutex_enter(&strbcall_lock); 3680 3681 for (;;) { 3682 if (strbcalls.bc_head != NULL && kmem_avail() > 0) { 3683 mutex_exit(&strbcall_lock); 3684 runbufcalls(); 3685 mutex_enter(&strbcall_lock); 3686 } 3687 if (strbcalls.bc_head != NULL) { 3688 clock_t wt, tick; 3689 3690 STRSTAT(bcwaits); 3691 /* Wait for memory to become available */ 3692 CALLB_CPR_SAFE_BEGIN(&cprinfo); 3693 tick = SEC_TO_TICK(60); 3694 time_to_wait(&wt, tick); 3695 (void) cv_timedwait(&memavail_cv, &strbcall_lock, wt); 3696 CALLB_CPR_SAFE_END(&cprinfo, &strbcall_lock); 3697 } 3698 3699 /* Wait for new work to arrive */ 3700 if (strbcalls.bc_head == NULL) { 3701 CALLB_CPR_SAFE_BEGIN(&cprinfo); 3702 cv_wait(&strbcall_cv, &strbcall_lock); 3703 CALLB_CPR_SAFE_END(&cprinfo, &strbcall_lock); 3704 } 3705 } 3706 } 3707 3708 /* 3709 * Background processing of streams background tasks which failed 3710 * taskq_dispatch. 3711 */ 3712 static void 3713 streams_qbkgrnd_service(void) 3714 { 3715 callb_cpr_t cprinfo; 3716 queue_t *q; 3717 3718 CALLB_CPR_INIT(&cprinfo, &service_queue, callb_generic_cpr, 3719 "streams_bkgrnd_service"); 3720 3721 mutex_enter(&service_queue); 3722 3723 for (;;) { 3724 /* 3725 * Wait for work to arrive. 3726 */ 3727 while ((freebs_list == NULL) && (qhead == NULL)) { 3728 CALLB_CPR_SAFE_BEGIN(&cprinfo); 3729 cv_wait(&services_to_run, &service_queue); 3730 CALLB_CPR_SAFE_END(&cprinfo, &service_queue); 3731 } 3732 /* 3733 * Handle all pending freebs requests to free memory. 3734 */ 3735 while (freebs_list != NULL) { 3736 mblk_t *mp = freebs_list; 3737 freebs_list = mp->b_next; 3738 mutex_exit(&service_queue); 3739 mblk_free(mp); 3740 mutex_enter(&service_queue); 3741 } 3742 /* 3743 * Run pending queues. 3744 */ 3745 while (qhead != NULL) { 3746 DQ(q, qhead, qtail, q_link); 3747 ASSERT(q != NULL); 3748 mutex_exit(&service_queue); 3749 queue_service(q); 3750 mutex_enter(&service_queue); 3751 } 3752 ASSERT(qhead == NULL && qtail == NULL); 3753 } 3754 } 3755 3756 /* 3757 * Background processing of streams background tasks which failed 3758 * taskq_dispatch. 3759 */ 3760 static void 3761 streams_sqbkgrnd_service(void) 3762 { 3763 callb_cpr_t cprinfo; 3764 syncq_t *sq; 3765 3766 CALLB_CPR_INIT(&cprinfo, &service_queue, callb_generic_cpr, 3767 "streams_sqbkgrnd_service"); 3768 3769 mutex_enter(&service_queue); 3770 3771 for (;;) { 3772 /* 3773 * Wait for work to arrive. 3774 */ 3775 while (sqhead == NULL) { 3776 CALLB_CPR_SAFE_BEGIN(&cprinfo); 3777 cv_wait(&syncqs_to_run, &service_queue); 3778 CALLB_CPR_SAFE_END(&cprinfo, &service_queue); 3779 } 3780 3781 /* 3782 * Run pending syncqs. 3783 */ 3784 while (sqhead != NULL) { 3785 DQ(sq, sqhead, sqtail, sq_next); 3786 ASSERT(sq != NULL); 3787 ASSERT(sq->sq_svcflags & SQ_BGTHREAD); 3788 mutex_exit(&service_queue); 3789 syncq_service(sq); 3790 mutex_enter(&service_queue); 3791 } 3792 } 3793 } 3794 3795 /* 3796 * Disable the syncq and wait for background syncq processing to complete. 3797 * If the syncq is placed on the sqhead/sqtail queue, try to remove it from the 3798 * list. 3799 */ 3800 void 3801 wait_sq_svc(syncq_t *sq) 3802 { 3803 mutex_enter(SQLOCK(sq)); 3804 sq->sq_svcflags |= SQ_DISABLED; 3805 if (sq->sq_svcflags & SQ_BGTHREAD) { 3806 syncq_t *sq_chase; 3807 syncq_t *sq_curr; 3808 int removed; 3809 3810 ASSERT(sq->sq_servcount == 1); 3811 mutex_enter(&service_queue); 3812 RMQ(sq, sqhead, sqtail, sq_next, sq_chase, sq_curr, removed); 3813 mutex_exit(&service_queue); 3814 if (removed) { 3815 sq->sq_svcflags &= ~SQ_BGTHREAD; 3816 sq->sq_servcount = 0; 3817 STRSTAT(sqremoved); 3818 goto done; 3819 } 3820 } 3821 while (sq->sq_servcount != 0) { 3822 sq->sq_flags |= SQ_WANTWAKEUP; 3823 cv_wait(&sq->sq_wait, SQLOCK(sq)); 3824 } 3825 done: 3826 mutex_exit(SQLOCK(sq)); 3827 } 3828 3829 /* 3830 * Put a syncq on the list of syncq's to be serviced by the sqthread. 3831 * Add the argument to the end of the sqhead list and set the flag 3832 * indicating this syncq has been enabled. If it has already been 3833 * enabled, don't do anything. 3834 * This routine assumes that SQLOCK is held. 3835 * NOTE that the lock order is to have the SQLOCK first, 3836 * so if the service_syncq lock is held, we need to release it 3837 * before aquiring the SQLOCK (mostly relevant for the background 3838 * thread, and this seems to be common among the STREAMS global locks). 3839 * Note the the sq_svcflags are protected by the SQLOCK. 3840 */ 3841 void 3842 sqenable(syncq_t *sq) 3843 { 3844 /* 3845 * This is probably not important except for where I believe it 3846 * is being called. At that point, it should be held (and it 3847 * is a pain to release it just for this routine, so don't do 3848 * it). 3849 */ 3850 ASSERT(MUTEX_HELD(SQLOCK(sq))); 3851 3852 IMPLY(sq->sq_servcount == 0, sq->sq_next == NULL); 3853 IMPLY(sq->sq_next != NULL, sq->sq_svcflags & SQ_BGTHREAD); 3854 3855 /* 3856 * Do not put on list if background thread is scheduled or 3857 * syncq is disabled. 3858 */ 3859 if (sq->sq_svcflags & (SQ_DISABLED | SQ_BGTHREAD)) 3860 return; 3861 3862 /* 3863 * Check whether we should enable sq at all. 3864 * Non PERMOD syncqs may be drained by at most one thread. 3865 * PERMOD syncqs may be drained by several threads but we limit the 3866 * total amount to the lesser of 3867 * Number of queues on the squeue and 3868 * Number of CPUs. 3869 */ 3870 if (sq->sq_servcount != 0) { 3871 if (((sq->sq_type & SQ_PERMOD) == 0) || 3872 (sq->sq_servcount >= MIN(sq->sq_nqueues, ncpus_online))) { 3873 STRSTAT(sqtoomany); 3874 return; 3875 } 3876 } 3877 3878 sq->sq_tstamp = lbolt; 3879 STRSTAT(sqenables); 3880 3881 /* Attempt a taskq dispatch */ 3882 sq->sq_servid = (void *)taskq_dispatch(streams_taskq, 3883 (task_func_t *)syncq_service, sq, TQ_NOSLEEP | TQ_NOQUEUE); 3884 if (sq->sq_servid != NULL) { 3885 sq->sq_servcount++; 3886 return; 3887 } 3888 3889 /* 3890 * This taskq dispatch failed, but a previous one may have succeeded. 3891 * Don't try to schedule on the background thread whilst there is 3892 * outstanding taskq processing. 3893 */ 3894 if (sq->sq_servcount != 0) 3895 return; 3896 3897 /* 3898 * System is low on resources and can't perform a non-sleeping 3899 * dispatch. Schedule the syncq for a background thread and mark the 3900 * syncq to avoid any further taskq dispatch attempts. 3901 */ 3902 mutex_enter(&service_queue); 3903 STRSTAT(taskqfails); 3904 ENQUEUE(sq, sqhead, sqtail, sq_next); 3905 sq->sq_svcflags |= SQ_BGTHREAD; 3906 sq->sq_servcount = 1; 3907 cv_signal(&syncqs_to_run); 3908 mutex_exit(&service_queue); 3909 } 3910 3911 /* 3912 * Note: fifo_close() depends on the mblk_t on the queue being freed 3913 * asynchronously. The asynchronous freeing of messages breaks the 3914 * recursive call chain of fifo_close() while there are I_SENDFD type of 3915 * messages refering other file pointers on the queue. Then when 3916 * closing pipes it can avoid stack overflow in case of daisy-chained 3917 * pipes, and also avoid deadlock in case of fifonode_t pairs (which 3918 * share the same fifolock_t). 3919 */ 3920 3921 void 3922 freebs_enqueue(mblk_t *mp, dblk_t *dbp) 3923 { 3924 esb_queue_t *eqp = &system_esbq; 3925 3926 ASSERT(dbp->db_mblk == mp); 3927 3928 /* 3929 * Check data sanity. The dblock should have non-empty free function. 3930 * It is better to panic here then later when the dblock is freed 3931 * asynchronously when the context is lost. 3932 */ 3933 if (dbp->db_frtnp->free_func == NULL) { 3934 panic("freebs_enqueue: dblock %p has a NULL free callback", 3935 (void *)dbp); 3936 } 3937 3938 mutex_enter(&eqp->eq_lock); 3939 /* queue the new mblk on the esballoc queue */ 3940 if (eqp->eq_head == NULL) { 3941 eqp->eq_head = eqp->eq_tail = mp; 3942 } else { 3943 eqp->eq_tail->b_next = mp; 3944 eqp->eq_tail = mp; 3945 } 3946 eqp->eq_len++; 3947 3948 /* If we're the first thread to reach the threshold, process */ 3949 if (eqp->eq_len >= esbq_max_qlen && 3950 !(eqp->eq_flags & ESBQ_PROCESSING)) 3951 esballoc_process_queue(eqp); 3952 3953 esballoc_set_timer(eqp, esbq_timeout); 3954 mutex_exit(&eqp->eq_lock); 3955 } 3956 3957 static void 3958 esballoc_process_queue(esb_queue_t *eqp) 3959 { 3960 mblk_t *mp; 3961 3962 ASSERT(MUTEX_HELD(&eqp->eq_lock)); 3963 3964 eqp->eq_flags |= ESBQ_PROCESSING; 3965 3966 do { 3967 /* 3968 * Detach the message chain for processing. 3969 */ 3970 mp = eqp->eq_head; 3971 eqp->eq_tail->b_next = NULL; 3972 eqp->eq_head = eqp->eq_tail = NULL; 3973 eqp->eq_len = 0; 3974 mutex_exit(&eqp->eq_lock); 3975 3976 /* 3977 * Process the message chain. 3978 */ 3979 esballoc_enqueue_mblk(mp); 3980 mutex_enter(&eqp->eq_lock); 3981 } while ((eqp->eq_len >= esbq_max_qlen) && (eqp->eq_len > 0)); 3982 3983 eqp->eq_flags &= ~ESBQ_PROCESSING; 3984 } 3985 3986 /* 3987 * taskq callback routine to free esballoced mblk's 3988 */ 3989 static void 3990 esballoc_mblk_free(mblk_t *mp) 3991 { 3992 mblk_t *nextmp; 3993 3994 for (; mp != NULL; mp = nextmp) { 3995 nextmp = mp->b_next; 3996 mp->b_next = NULL; 3997 mblk_free(mp); 3998 } 3999 } 4000 4001 static void 4002 esballoc_enqueue_mblk(mblk_t *mp) 4003 { 4004 4005 if (taskq_dispatch(system_taskq, (task_func_t *)esballoc_mblk_free, mp, 4006 TQ_NOSLEEP) == NULL) { 4007 mblk_t *first_mp = mp; 4008 /* 4009 * System is low on resources and can't perform a non-sleeping 4010 * dispatch. Schedule for a background thread. 4011 */ 4012 mutex_enter(&service_queue); 4013 STRSTAT(taskqfails); 4014 4015 while (mp->b_next != NULL) 4016 mp = mp->b_next; 4017 4018 mp->b_next = freebs_list; 4019 freebs_list = first_mp; 4020 cv_signal(&services_to_run); 4021 mutex_exit(&service_queue); 4022 } 4023 } 4024 4025 static void 4026 esballoc_timer(void *arg) 4027 { 4028 esb_queue_t *eqp = arg; 4029 4030 mutex_enter(&eqp->eq_lock); 4031 eqp->eq_flags &= ~ESBQ_TIMER; 4032 4033 if (!(eqp->eq_flags & ESBQ_PROCESSING) && 4034 eqp->eq_len > 0) 4035 esballoc_process_queue(eqp); 4036 4037 esballoc_set_timer(eqp, esbq_timeout); 4038 mutex_exit(&eqp->eq_lock); 4039 } 4040 4041 static void 4042 esballoc_set_timer(esb_queue_t *eqp, clock_t eq_timeout) 4043 { 4044 ASSERT(MUTEX_HELD(&eqp->eq_lock)); 4045 4046 if (eqp->eq_len > 0 && !(eqp->eq_flags & ESBQ_TIMER)) { 4047 (void) timeout(esballoc_timer, eqp, eq_timeout); 4048 eqp->eq_flags |= ESBQ_TIMER; 4049 } 4050 } 4051 4052 void 4053 esballoc_queue_init(void) 4054 { 4055 system_esbq.eq_len = 0; 4056 system_esbq.eq_head = system_esbq.eq_tail = NULL; 4057 system_esbq.eq_flags = 0; 4058 } 4059 4060 /* 4061 * Set the QBACK or QB_BACK flag in the given queue for 4062 * the given priority band. 4063 */ 4064 void 4065 setqback(queue_t *q, unsigned char pri) 4066 { 4067 int i; 4068 qband_t *qbp; 4069 qband_t **qbpp; 4070 4071 ASSERT(MUTEX_HELD(QLOCK(q))); 4072 if (pri != 0) { 4073 if (pri > q->q_nband) { 4074 qbpp = &q->q_bandp; 4075 while (*qbpp) 4076 qbpp = &(*qbpp)->qb_next; 4077 while (pri > q->q_nband) { 4078 if ((*qbpp = allocband()) == NULL) { 4079 cmn_err(CE_WARN, 4080 "setqback: can't allocate qband\n"); 4081 return; 4082 } 4083 (*qbpp)->qb_hiwat = q->q_hiwat; 4084 (*qbpp)->qb_lowat = q->q_lowat; 4085 q->q_nband++; 4086 qbpp = &(*qbpp)->qb_next; 4087 } 4088 } 4089 qbp = q->q_bandp; 4090 i = pri; 4091 while (--i) 4092 qbp = qbp->qb_next; 4093 qbp->qb_flag |= QB_BACK; 4094 } else { 4095 q->q_flag |= QBACK; 4096 } 4097 } 4098 4099 int 4100 strcopyin(void *from, void *to, size_t len, int copyflag) 4101 { 4102 if (copyflag & U_TO_K) { 4103 ASSERT((copyflag & K_TO_K) == 0); 4104 if (copyin(from, to, len)) 4105 return (EFAULT); 4106 } else { 4107 ASSERT(copyflag & K_TO_K); 4108 bcopy(from, to, len); 4109 } 4110 return (0); 4111 } 4112 4113 int 4114 strcopyout(void *from, void *to, size_t len, int copyflag) 4115 { 4116 if (copyflag & U_TO_K) { 4117 if (copyout(from, to, len)) 4118 return (EFAULT); 4119 } else { 4120 ASSERT(copyflag & K_TO_K); 4121 bcopy(from, to, len); 4122 } 4123 return (0); 4124 } 4125 4126 /* 4127 * strsignal_nolock() posts a signal to the process(es) at the stream head. 4128 * It assumes that the stream head lock is already held, whereas strsignal() 4129 * acquires the lock first. This routine was created because a few callers 4130 * release the stream head lock before calling only to re-acquire it after 4131 * it returns. 4132 */ 4133 void 4134 strsignal_nolock(stdata_t *stp, int sig, int32_t band) 4135 { 4136 ASSERT(MUTEX_HELD(&stp->sd_lock)); 4137 switch (sig) { 4138 case SIGPOLL: 4139 if (stp->sd_sigflags & S_MSG) 4140 strsendsig(stp->sd_siglist, S_MSG, (uchar_t)band, 0); 4141 break; 4142 4143 default: 4144 if (stp->sd_pgidp) { 4145 pgsignal(stp->sd_pgidp, sig); 4146 } 4147 break; 4148 } 4149 } 4150 4151 void 4152 strsignal(stdata_t *stp, int sig, int32_t band) 4153 { 4154 TRACE_3(TR_FAC_STREAMS_FR, TR_SENDSIG, 4155 "strsignal:%p, %X, %X", stp, sig, band); 4156 4157 mutex_enter(&stp->sd_lock); 4158 switch (sig) { 4159 case SIGPOLL: 4160 if (stp->sd_sigflags & S_MSG) 4161 strsendsig(stp->sd_siglist, S_MSG, (uchar_t)band, 0); 4162 break; 4163 4164 default: 4165 if (stp->sd_pgidp) { 4166 pgsignal(stp->sd_pgidp, sig); 4167 } 4168 break; 4169 } 4170 mutex_exit(&stp->sd_lock); 4171 } 4172 4173 void 4174 strhup(stdata_t *stp) 4175 { 4176 ASSERT(mutex_owned(&stp->sd_lock)); 4177 pollwakeup(&stp->sd_pollist, POLLHUP); 4178 if (stp->sd_sigflags & S_HANGUP) 4179 strsendsig(stp->sd_siglist, S_HANGUP, 0, 0); 4180 } 4181 4182 /* 4183 * Backenable the first queue upstream from `q' with a service procedure. 4184 */ 4185 void 4186 backenable(queue_t *q, uchar_t pri) 4187 { 4188 queue_t *nq; 4189 4190 /* 4191 * our presence might not prevent other modules in our own 4192 * stream from popping/pushing since the caller of getq might not 4193 * have a claim on the queue (some drivers do a getq on somebody 4194 * else's queue - they know that the queue itself is not going away 4195 * but the framework has to guarantee q_next in that stream.) 4196 */ 4197 claimstr(q); 4198 4199 /* find nearest back queue with service proc */ 4200 for (nq = backq(q); nq && !nq->q_qinfo->qi_srvp; nq = backq(nq)) { 4201 ASSERT(STRMATED(q->q_stream) || STREAM(q) == STREAM(nq)); 4202 } 4203 4204 if (nq) { 4205 kthread_t *freezer; 4206 /* 4207 * backenable can be called either with no locks held 4208 * or with the stream frozen (the latter occurs when a module 4209 * calls rmvq with the stream frozen.) If the stream is frozen 4210 * by the caller the caller will hold all qlocks in the stream. 4211 * Note that a frozen stream doesn't freeze a mated stream, 4212 * so we explicitly check for that. 4213 */ 4214 freezer = STREAM(q)->sd_freezer; 4215 if (freezer != curthread || STREAM(q) != STREAM(nq)) { 4216 mutex_enter(QLOCK(nq)); 4217 } 4218 #ifdef DEBUG 4219 else { 4220 ASSERT(frozenstr(q)); 4221 ASSERT(MUTEX_HELD(QLOCK(q))); 4222 ASSERT(MUTEX_HELD(QLOCK(nq))); 4223 } 4224 #endif 4225 setqback(nq, pri); 4226 qenable_locked(nq); 4227 if (freezer != curthread || STREAM(q) != STREAM(nq)) 4228 mutex_exit(QLOCK(nq)); 4229 } 4230 releasestr(q); 4231 } 4232 4233 /* 4234 * Return the appropriate errno when one of flags_to_check is set 4235 * in sd_flags. Uses the exported error routines if they are set. 4236 * Will return 0 if non error is set (or if the exported error routines 4237 * do not return an error). 4238 * 4239 * If there is both a read and write error to check we prefer the read error. 4240 * Also, give preference to recorded errno's over the error functions. 4241 * The flags that are handled are: 4242 * STPLEX return EINVAL 4243 * STRDERR return sd_rerror (and clear if STRDERRNONPERSIST) 4244 * STWRERR return sd_werror (and clear if STWRERRNONPERSIST) 4245 * STRHUP return sd_werror 4246 * 4247 * If the caller indicates that the operation is a peek a nonpersistent error 4248 * is not cleared. 4249 */ 4250 int 4251 strgeterr(stdata_t *stp, int32_t flags_to_check, int ispeek) 4252 { 4253 int32_t sd_flag = stp->sd_flag & flags_to_check; 4254 int error = 0; 4255 4256 ASSERT(MUTEX_HELD(&stp->sd_lock)); 4257 ASSERT((flags_to_check & ~(STRDERR|STWRERR|STRHUP|STPLEX)) == 0); 4258 if (sd_flag & STPLEX) 4259 error = EINVAL; 4260 else if (sd_flag & STRDERR) { 4261 error = stp->sd_rerror; 4262 if ((stp->sd_flag & STRDERRNONPERSIST) && !ispeek) { 4263 /* 4264 * Read errors are non-persistent i.e. discarded once 4265 * returned to a non-peeking caller, 4266 */ 4267 stp->sd_rerror = 0; 4268 stp->sd_flag &= ~STRDERR; 4269 } 4270 if (error == 0 && stp->sd_rderrfunc != NULL) { 4271 int clearerr = 0; 4272 4273 error = (*stp->sd_rderrfunc)(stp->sd_vnode, ispeek, 4274 &clearerr); 4275 if (clearerr) { 4276 stp->sd_flag &= ~STRDERR; 4277 stp->sd_rderrfunc = NULL; 4278 } 4279 } 4280 } else if (sd_flag & STWRERR) { 4281 error = stp->sd_werror; 4282 if ((stp->sd_flag & STWRERRNONPERSIST) && !ispeek) { 4283 /* 4284 * Write errors are non-persistent i.e. discarded once 4285 * returned to a non-peeking caller, 4286 */ 4287 stp->sd_werror = 0; 4288 stp->sd_flag &= ~STWRERR; 4289 } 4290 if (error == 0 && stp->sd_wrerrfunc != NULL) { 4291 int clearerr = 0; 4292 4293 error = (*stp->sd_wrerrfunc)(stp->sd_vnode, ispeek, 4294 &clearerr); 4295 if (clearerr) { 4296 stp->sd_flag &= ~STWRERR; 4297 stp->sd_wrerrfunc = NULL; 4298 } 4299 } 4300 } else if (sd_flag & STRHUP) { 4301 /* sd_werror set when STRHUP */ 4302 error = stp->sd_werror; 4303 } 4304 return (error); 4305 } 4306 4307 4308 /* 4309 * single-thread open/close/push/pop 4310 * for twisted streams also 4311 */ 4312 int 4313 strstartplumb(stdata_t *stp, int flag, int cmd) 4314 { 4315 int waited = 1; 4316 int error = 0; 4317 4318 if (STRMATED(stp)) { 4319 struct stdata *stmatep = stp->sd_mate; 4320 4321 STRLOCKMATES(stp); 4322 while (waited) { 4323 waited = 0; 4324 while (stmatep->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) { 4325 if ((cmd == I_POP) && 4326 (flag & (FNDELAY|FNONBLOCK))) { 4327 STRUNLOCKMATES(stp); 4328 return (EAGAIN); 4329 } 4330 waited = 1; 4331 mutex_exit(&stp->sd_lock); 4332 if (!cv_wait_sig(&stmatep->sd_monitor, 4333 &stmatep->sd_lock)) { 4334 mutex_exit(&stmatep->sd_lock); 4335 return (EINTR); 4336 } 4337 mutex_exit(&stmatep->sd_lock); 4338 STRLOCKMATES(stp); 4339 } 4340 while (stp->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) { 4341 if ((cmd == I_POP) && 4342 (flag & (FNDELAY|FNONBLOCK))) { 4343 STRUNLOCKMATES(stp); 4344 return (EAGAIN); 4345 } 4346 waited = 1; 4347 mutex_exit(&stmatep->sd_lock); 4348 if (!cv_wait_sig(&stp->sd_monitor, 4349 &stp->sd_lock)) { 4350 mutex_exit(&stp->sd_lock); 4351 return (EINTR); 4352 } 4353 mutex_exit(&stp->sd_lock); 4354 STRLOCKMATES(stp); 4355 } 4356 if (stp->sd_flag & (STRDERR|STWRERR|STRHUP|STPLEX)) { 4357 error = strgeterr(stp, 4358 STRDERR|STWRERR|STRHUP|STPLEX, 0); 4359 if (error != 0) { 4360 STRUNLOCKMATES(stp); 4361 return (error); 4362 } 4363 } 4364 } 4365 stp->sd_flag |= STRPLUMB; 4366 STRUNLOCKMATES(stp); 4367 } else { 4368 mutex_enter(&stp->sd_lock); 4369 while (stp->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) { 4370 if (((cmd == I_POP) || (cmd == _I_REMOVE)) && 4371 (flag & (FNDELAY|FNONBLOCK))) { 4372 mutex_exit(&stp->sd_lock); 4373 return (EAGAIN); 4374 } 4375 if (!cv_wait_sig(&stp->sd_monitor, &stp->sd_lock)) { 4376 mutex_exit(&stp->sd_lock); 4377 return (EINTR); 4378 } 4379 if (stp->sd_flag & (STRDERR|STWRERR|STRHUP|STPLEX)) { 4380 error = strgeterr(stp, 4381 STRDERR|STWRERR|STRHUP|STPLEX, 0); 4382 if (error != 0) { 4383 mutex_exit(&stp->sd_lock); 4384 return (error); 4385 } 4386 } 4387 } 4388 stp->sd_flag |= STRPLUMB; 4389 mutex_exit(&stp->sd_lock); 4390 } 4391 return (0); 4392 } 4393 4394 /* 4395 * Complete the plumbing operation associated with stream `stp'. 4396 */ 4397 void 4398 strendplumb(stdata_t *stp) 4399 { 4400 ASSERT(MUTEX_HELD(&stp->sd_lock)); 4401 ASSERT(stp->sd_flag & STRPLUMB); 4402 stp->sd_flag &= ~STRPLUMB; 4403 cv_broadcast(&stp->sd_monitor); 4404 } 4405 4406 /* 4407 * This describes how the STREAMS framework handles synchronization 4408 * during open/push and close/pop. 4409 * The key interfaces for open and close are qprocson and qprocsoff, 4410 * respectively. While the close case in general is harder both open 4411 * have close have significant similarities. 4412 * 4413 * During close the STREAMS framework has to both ensure that there 4414 * are no stale references to the queue pair (and syncq) that 4415 * are being closed and also provide the guarantees that are documented 4416 * in qprocsoff(9F). 4417 * If there are stale references to the queue that is closing it can 4418 * result in kernel memory corruption or kernel panics. 4419 * 4420 * Note that is it up to the module/driver to ensure that it itself 4421 * does not have any stale references to the closing queues once its close 4422 * routine returns. This includes: 4423 * - Cancelling any timeout/bufcall/qtimeout/qbufcall callback routines 4424 * associated with the queues. For timeout and bufcall callbacks the 4425 * module/driver also has to ensure (or wait for) any callbacks that 4426 * are in progress. 4427 * - If the module/driver is using esballoc it has to ensure that any 4428 * esballoc free functions do not refer to a queue that has closed. 4429 * (Note that in general the close routine can not wait for the esballoc'ed 4430 * messages to be freed since that can cause a deadlock.) 4431 * - Cancelling any interrupts that refer to the closing queues and 4432 * also ensuring that there are no interrupts in progress that will 4433 * refer to the closing queues once the close routine returns. 4434 * - For multiplexors removing any driver global state that refers to 4435 * the closing queue and also ensuring that there are no threads in 4436 * the multiplexor that has picked up a queue pointer but not yet 4437 * finished using it. 4438 * 4439 * In addition, a driver/module can only reference the q_next pointer 4440 * in its open, close, put, or service procedures or in a 4441 * qtimeout/qbufcall callback procedure executing "on" the correct 4442 * stream. Thus it can not reference the q_next pointer in an interrupt 4443 * routine or a timeout, bufcall or esballoc callback routine. Likewise 4444 * it can not reference q_next of a different queue e.g. in a mux that 4445 * passes messages from one queues put/service procedure to another queue. 4446 * In all the cases when the driver/module can not access the q_next 4447 * field it must use the *next* versions e.g. canputnext instead of 4448 * canput(q->q_next) and putnextctl instead of putctl(q->q_next, ...). 4449 * 4450 * 4451 * Assuming that the driver/module conforms to the above constraints 4452 * the STREAMS framework has to avoid stale references to q_next for all 4453 * the framework internal cases which include (but are not limited to): 4454 * - Threads in canput/canputnext/backenable and elsewhere that are 4455 * walking q_next. 4456 * - Messages on a syncq that have a reference to the queue through b_queue. 4457 * - Messages on an outer perimeter (syncq) that have a reference to the 4458 * queue through b_queue. 4459 * - Threads that use q_nfsrv (e.g. canput) to find a queue. 4460 * Note that only canput and bcanput use q_nfsrv without any locking. 4461 * 4462 * The STREAMS framework providing the qprocsoff(9F) guarantees means that 4463 * after qprocsoff returns, the framework has to ensure that no threads can 4464 * enter the put or service routines for the closing read or write-side queue. 4465 * In addition to preventing "direct" entry into the put procedures 4466 * the framework also has to prevent messages being drained from 4467 * the syncq or the outer perimeter. 4468 * XXX Note that currently qdetach does relies on D_MTOCEXCL as the only 4469 * mechanism to prevent qwriter(PERIM_OUTER) from running after 4470 * qprocsoff has returned. 4471 * Note that if a module/driver uses put(9F) on one of its own queues 4472 * it is up to the module/driver to ensure that the put() doesn't 4473 * get called when the queue is closing. 4474 * 4475 * 4476 * The framework aspects of the above "contract" is implemented by 4477 * qprocsoff, removeq, and strlock: 4478 * - qprocsoff (disable_svc) sets QWCLOSE to prevent runservice from 4479 * entering the service procedures. 4480 * - strlock acquires the sd_lock and sd_reflock to prevent putnext, 4481 * canputnext, backenable etc from dereferencing the q_next that will 4482 * soon change. 4483 * - strlock waits for sd_refcnt to be zero to wait for e.g. any canputnext 4484 * or other q_next walker that uses claimstr/releasestr to finish. 4485 * - optionally for every syncq in the stream strlock acquires all the 4486 * sq_lock's and waits for all sq_counts to drop to a value that indicates 4487 * that no thread executes in the put or service procedures and that no 4488 * thread is draining into the module/driver. This ensures that no 4489 * open, close, put, service, or qtimeout/qbufcall callback procedure is 4490 * currently executing hence no such thread can end up with the old stale 4491 * q_next value and no canput/backenable can have the old stale 4492 * q_nfsrv/q_next. 4493 * - qdetach (wait_svc) makes sure that any scheduled or running threads 4494 * have either finished or observed the QWCLOSE flag and gone away. 4495 */ 4496 4497 4498 /* 4499 * Get all the locks necessary to change q_next. 4500 * 4501 * Wait for sd_refcnt to reach 0 and, if sqlist is present, wait for the 4502 * sq_count of each syncq in the list to drop to sq_rmqcount, indicating that 4503 * the only threads inside the sqncq are threads currently calling removeq(). 4504 * Since threads calling removeq() are in the process of removing their queues 4505 * from the stream, we do not need to worry about them accessing a stale q_next 4506 * pointer and thus we do not need to wait for them to exit (in fact, waiting 4507 * for them can cause deadlock). 4508 * 4509 * This routine is subject to starvation since it does not set any flag to 4510 * prevent threads from entering a module in the stream(i.e. sq_count can 4511 * increase on some syncq while it is waiting on some other syncq.) 4512 * 4513 * Assumes that only one thread attempts to call strlock for a given 4514 * stream. If this is not the case the two threads would deadlock. 4515 * This assumption is guaranteed since strlock is only called by insertq 4516 * and removeq and streams plumbing changes are single-threaded for 4517 * a given stream using the STWOPEN, STRCLOSE, and STRPLUMB flags. 4518 * 4519 * For pipes, it is not difficult to atomically designate a pair of streams 4520 * to be mated. Once mated atomically by the framework the twisted pair remain 4521 * configured that way until dismantled atomically by the framework. 4522 * When plumbing takes place on a twisted stream it is necessary to ensure that 4523 * this operation is done exclusively on the twisted stream since two such 4524 * operations, each initiated on different ends of the pipe will deadlock 4525 * waiting for each other to complete. 4526 * 4527 * On entry, no locks should be held. 4528 * The locks acquired and held by strlock depends on a few factors. 4529 * - If sqlist is non-NULL all the syncq locks in the sqlist will be acquired 4530 * and held on exit and all sq_count are at an acceptable level. 4531 * - In all cases, sd_lock and sd_reflock are acquired and held on exit with 4532 * sd_refcnt being zero. 4533 */ 4534 4535 static void 4536 strlock(struct stdata *stp, sqlist_t *sqlist) 4537 { 4538 syncql_t *sql, *sql2; 4539 retry: 4540 /* 4541 * Wait for any claimstr to go away. 4542 */ 4543 if (STRMATED(stp)) { 4544 struct stdata *stp1, *stp2; 4545 4546 STRLOCKMATES(stp); 4547 /* 4548 * Note that the selection of locking order is not 4549 * important, just that they are always aquired in 4550 * the same order. To assure this, we choose this 4551 * order based on the value of the pointer, and since 4552 * the pointer will not change for the life of this 4553 * pair, we will always grab the locks in the same 4554 * order (and hence, prevent deadlocks). 4555 */ 4556 if (&(stp->sd_lock) > &((stp->sd_mate)->sd_lock)) { 4557 stp1 = stp; 4558 stp2 = stp->sd_mate; 4559 } else { 4560 stp2 = stp; 4561 stp1 = stp->sd_mate; 4562 } 4563 mutex_enter(&stp1->sd_reflock); 4564 if (stp1->sd_refcnt > 0) { 4565 STRUNLOCKMATES(stp); 4566 cv_wait(&stp1->sd_refmonitor, &stp1->sd_reflock); 4567 mutex_exit(&stp1->sd_reflock); 4568 goto retry; 4569 } 4570 mutex_enter(&stp2->sd_reflock); 4571 if (stp2->sd_refcnt > 0) { 4572 STRUNLOCKMATES(stp); 4573 mutex_exit(&stp1->sd_reflock); 4574 cv_wait(&stp2->sd_refmonitor, &stp2->sd_reflock); 4575 mutex_exit(&stp2->sd_reflock); 4576 goto retry; 4577 } 4578 STREAM_PUTLOCKS_ENTER(stp1); 4579 STREAM_PUTLOCKS_ENTER(stp2); 4580 } else { 4581 mutex_enter(&stp->sd_lock); 4582 mutex_enter(&stp->sd_reflock); 4583 while (stp->sd_refcnt > 0) { 4584 mutex_exit(&stp->sd_lock); 4585 cv_wait(&stp->sd_refmonitor, &stp->sd_reflock); 4586 if (mutex_tryenter(&stp->sd_lock) == 0) { 4587 mutex_exit(&stp->sd_reflock); 4588 mutex_enter(&stp->sd_lock); 4589 mutex_enter(&stp->sd_reflock); 4590 } 4591 } 4592 STREAM_PUTLOCKS_ENTER(stp); 4593 } 4594 4595 if (sqlist == NULL) 4596 return; 4597 4598 for (sql = sqlist->sqlist_head; sql; sql = sql->sql_next) { 4599 syncq_t *sq = sql->sql_sq; 4600 uint16_t count; 4601 4602 mutex_enter(SQLOCK(sq)); 4603 count = sq->sq_count; 4604 ASSERT(sq->sq_rmqcount <= count); 4605 SQ_PUTLOCKS_ENTER(sq); 4606 SUM_SQ_PUTCOUNTS(sq, count); 4607 if (count == sq->sq_rmqcount) 4608 continue; 4609 4610 /* Failed - drop all locks that we have acquired so far */ 4611 if (STRMATED(stp)) { 4612 STREAM_PUTLOCKS_EXIT(stp); 4613 STREAM_PUTLOCKS_EXIT(stp->sd_mate); 4614 STRUNLOCKMATES(stp); 4615 mutex_exit(&stp->sd_reflock); 4616 mutex_exit(&stp->sd_mate->sd_reflock); 4617 } else { 4618 STREAM_PUTLOCKS_EXIT(stp); 4619 mutex_exit(&stp->sd_lock); 4620 mutex_exit(&stp->sd_reflock); 4621 } 4622 for (sql2 = sqlist->sqlist_head; sql2 != sql; 4623 sql2 = sql2->sql_next) { 4624 SQ_PUTLOCKS_EXIT(sql2->sql_sq); 4625 mutex_exit(SQLOCK(sql2->sql_sq)); 4626 } 4627 4628 /* 4629 * The wait loop below may starve when there are many threads 4630 * claiming the syncq. This is especially a problem with permod 4631 * syncqs (IP). To lessen the impact of the problem we increment 4632 * sq_needexcl and clear fastbits so that putnexts will slow 4633 * down and call sqenable instead of draining right away. 4634 */ 4635 sq->sq_needexcl++; 4636 SQ_PUTCOUNT_CLRFAST_LOCKED(sq); 4637 while (count > sq->sq_rmqcount) { 4638 sq->sq_flags |= SQ_WANTWAKEUP; 4639 SQ_PUTLOCKS_EXIT(sq); 4640 cv_wait(&sq->sq_wait, SQLOCK(sq)); 4641 count = sq->sq_count; 4642 SQ_PUTLOCKS_ENTER(sq); 4643 SUM_SQ_PUTCOUNTS(sq, count); 4644 } 4645 sq->sq_needexcl--; 4646 if (sq->sq_needexcl == 0) 4647 SQ_PUTCOUNT_SETFAST_LOCKED(sq); 4648 SQ_PUTLOCKS_EXIT(sq); 4649 ASSERT(count == sq->sq_rmqcount); 4650 mutex_exit(SQLOCK(sq)); 4651 goto retry; 4652 } 4653 } 4654 4655 /* 4656 * Drop all the locks that strlock acquired. 4657 */ 4658 static void 4659 strunlock(struct stdata *stp, sqlist_t *sqlist) 4660 { 4661 syncql_t *sql; 4662 4663 if (STRMATED(stp)) { 4664 STREAM_PUTLOCKS_EXIT(stp); 4665 STREAM_PUTLOCKS_EXIT(stp->sd_mate); 4666 STRUNLOCKMATES(stp); 4667 mutex_exit(&stp->sd_reflock); 4668 mutex_exit(&stp->sd_mate->sd_reflock); 4669 } else { 4670 STREAM_PUTLOCKS_EXIT(stp); 4671 mutex_exit(&stp->sd_lock); 4672 mutex_exit(&stp->sd_reflock); 4673 } 4674 4675 if (sqlist == NULL) 4676 return; 4677 4678 for (sql = sqlist->sqlist_head; sql; sql = sql->sql_next) { 4679 SQ_PUTLOCKS_EXIT(sql->sql_sq); 4680 mutex_exit(SQLOCK(sql->sql_sq)); 4681 } 4682 } 4683 4684 /* 4685 * When the module has service procedure, we need check if the next 4686 * module which has service procedure is in flow control to trigger 4687 * the backenable. 4688 */ 4689 static void 4690 backenable_insertedq(queue_t *q) 4691 { 4692 qband_t *qbp; 4693 4694 claimstr(q); 4695 if (q->q_qinfo->qi_srvp != NULL && q->q_next != NULL) { 4696 if (q->q_next->q_nfsrv->q_flag & QWANTW) 4697 backenable(q, 0); 4698 4699 qbp = q->q_next->q_nfsrv->q_bandp; 4700 for (; qbp != NULL; qbp = qbp->qb_next) 4701 if ((qbp->qb_flag & QB_WANTW) && qbp->qb_first != NULL) 4702 backenable(q, qbp->qb_first->b_band); 4703 } 4704 releasestr(q); 4705 } 4706 4707 /* 4708 * Given two read queues, insert a new single one after another. 4709 * 4710 * This routine acquires all the necessary locks in order to change 4711 * q_next and related pointer using strlock(). 4712 * It depends on the stream head ensuring that there are no concurrent 4713 * insertq or removeq on the same stream. The stream head ensures this 4714 * using the flags STWOPEN, STRCLOSE, and STRPLUMB. 4715 * 4716 * Note that no syncq locks are held during the q_next change. This is 4717 * applied to all streams since, unlike removeq, there is no problem of stale 4718 * pointers when adding a module to the stream. Thus drivers/modules that do a 4719 * canput(rq->q_next) would never get a closed/freed queue pointer even if we 4720 * applied this optimization to all streams. 4721 */ 4722 void 4723 insertq(struct stdata *stp, queue_t *new) 4724 { 4725 queue_t *after; 4726 queue_t *wafter; 4727 queue_t *wnew = _WR(new); 4728 boolean_t have_fifo = B_FALSE; 4729 4730 if (new->q_flag & _QINSERTING) { 4731 ASSERT(stp->sd_vnode->v_type != VFIFO); 4732 after = new->q_next; 4733 wafter = _WR(new->q_next); 4734 } else { 4735 after = _RD(stp->sd_wrq); 4736 wafter = stp->sd_wrq; 4737 } 4738 4739 TRACE_2(TR_FAC_STREAMS_FR, TR_INSERTQ, 4740 "insertq:%p, %p", after, new); 4741 ASSERT(after->q_flag & QREADR); 4742 ASSERT(new->q_flag & QREADR); 4743 4744 strlock(stp, NULL); 4745 4746 /* Do we have a FIFO? */ 4747 if (wafter->q_next == after) { 4748 have_fifo = B_TRUE; 4749 wnew->q_next = new; 4750 } else { 4751 wnew->q_next = wafter->q_next; 4752 } 4753 new->q_next = after; 4754 4755 set_nfsrv_ptr(new, wnew, after, wafter); 4756 /* 4757 * set_nfsrv_ptr() needs to know if this is an insertion or not, 4758 * so only reset this flag after calling it. 4759 */ 4760 new->q_flag &= ~_QINSERTING; 4761 4762 if (have_fifo) { 4763 wafter->q_next = wnew; 4764 } else { 4765 if (wafter->q_next) 4766 _OTHERQ(wafter->q_next)->q_next = new; 4767 wafter->q_next = wnew; 4768 } 4769 4770 set_qend(new); 4771 /* The QEND flag might have to be updated for the upstream guy */ 4772 set_qend(after); 4773 4774 ASSERT(_SAMESTR(new) == O_SAMESTR(new)); 4775 ASSERT(_SAMESTR(wnew) == O_SAMESTR(wnew)); 4776 ASSERT(_SAMESTR(after) == O_SAMESTR(after)); 4777 ASSERT(_SAMESTR(wafter) == O_SAMESTR(wafter)); 4778 strsetuio(stp); 4779 4780 /* 4781 * If this was a module insertion, bump the push count. 4782 */ 4783 if (!(new->q_flag & QISDRV)) 4784 stp->sd_pushcnt++; 4785 4786 strunlock(stp, NULL); 4787 4788 /* check if the write Q needs backenable */ 4789 backenable_insertedq(wnew); 4790 4791 /* check if the read Q needs backenable */ 4792 backenable_insertedq(new); 4793 } 4794 4795 /* 4796 * Given a read queue, unlink it from any neighbors. 4797 * 4798 * This routine acquires all the necessary locks in order to 4799 * change q_next and related pointers and also guard against 4800 * stale references (e.g. through q_next) to the queue that 4801 * is being removed. It also plays part of the role in ensuring 4802 * that the module's/driver's put procedure doesn't get called 4803 * after qprocsoff returns. 4804 * 4805 * Removeq depends on the stream head ensuring that there are 4806 * no concurrent insertq or removeq on the same stream. The 4807 * stream head ensures this using the flags STWOPEN, STRCLOSE and 4808 * STRPLUMB. 4809 * 4810 * The set of locks needed to remove the queue is different in 4811 * different cases: 4812 * 4813 * Acquire sd_lock, sd_reflock, and all the syncq locks in the stream after 4814 * waiting for the syncq reference count to drop to 0 indicating that no 4815 * non-close threads are present anywhere in the stream. This ensures that any 4816 * module/driver can reference q_next in its open, close, put, or service 4817 * procedures. 4818 * 4819 * The sq_rmqcount counter tracks the number of threads inside removeq(). 4820 * strlock() ensures that there is either no threads executing inside perimeter 4821 * or there is only a thread calling qprocsoff(). 4822 * 4823 * strlock() compares the value of sq_count with the number of threads inside 4824 * removeq() and waits until sq_count is equal to sq_rmqcount. We need to wakeup 4825 * any threads waiting in strlock() when the sq_rmqcount increases. 4826 */ 4827 4828 void 4829 removeq(queue_t *qp) 4830 { 4831 queue_t *wqp = _WR(qp); 4832 struct stdata *stp = STREAM(qp); 4833 sqlist_t *sqlist = NULL; 4834 boolean_t isdriver; 4835 int moved; 4836 syncq_t *sq = qp->q_syncq; 4837 syncq_t *wsq = wqp->q_syncq; 4838 4839 ASSERT(stp); 4840 4841 TRACE_2(TR_FAC_STREAMS_FR, TR_REMOVEQ, 4842 "removeq:%p %p", qp, wqp); 4843 ASSERT(qp->q_flag&QREADR); 4844 4845 /* 4846 * For queues using Synchronous streams, we must wait for all threads in 4847 * rwnext() to drain out before proceeding. 4848 */ 4849 if (qp->q_flag & QSYNCSTR) { 4850 /* First, we need wakeup any threads blocked in rwnext() */ 4851 mutex_enter(SQLOCK(sq)); 4852 if (sq->sq_flags & SQ_WANTWAKEUP) { 4853 sq->sq_flags &= ~SQ_WANTWAKEUP; 4854 cv_broadcast(&sq->sq_wait); 4855 } 4856 mutex_exit(SQLOCK(sq)); 4857 4858 if (wsq != sq) { 4859 mutex_enter(SQLOCK(wsq)); 4860 if (wsq->sq_flags & SQ_WANTWAKEUP) { 4861 wsq->sq_flags &= ~SQ_WANTWAKEUP; 4862 cv_broadcast(&wsq->sq_wait); 4863 } 4864 mutex_exit(SQLOCK(wsq)); 4865 } 4866 4867 mutex_enter(QLOCK(qp)); 4868 while (qp->q_rwcnt > 0) { 4869 qp->q_flag |= QWANTRMQSYNC; 4870 cv_wait(&qp->q_wait, QLOCK(qp)); 4871 } 4872 mutex_exit(QLOCK(qp)); 4873 4874 mutex_enter(QLOCK(wqp)); 4875 while (wqp->q_rwcnt > 0) { 4876 wqp->q_flag |= QWANTRMQSYNC; 4877 cv_wait(&wqp->q_wait, QLOCK(wqp)); 4878 } 4879 mutex_exit(QLOCK(wqp)); 4880 } 4881 4882 mutex_enter(SQLOCK(sq)); 4883 sq->sq_rmqcount++; 4884 if (sq->sq_flags & SQ_WANTWAKEUP) { 4885 sq->sq_flags &= ~SQ_WANTWAKEUP; 4886 cv_broadcast(&sq->sq_wait); 4887 } 4888 mutex_exit(SQLOCK(sq)); 4889 4890 isdriver = (qp->q_flag & QISDRV); 4891 4892 sqlist = sqlist_build(qp, stp, STRMATED(stp)); 4893 strlock(stp, sqlist); 4894 4895 reset_nfsrv_ptr(qp, wqp); 4896 4897 ASSERT(wqp->q_next == NULL || backq(qp)->q_next == qp); 4898 ASSERT(qp->q_next == NULL || backq(wqp)->q_next == wqp); 4899 /* Do we have a FIFO? */ 4900 if (wqp->q_next == qp) { 4901 stp->sd_wrq->q_next = _RD(stp->sd_wrq); 4902 } else { 4903 if (wqp->q_next) 4904 backq(qp)->q_next = qp->q_next; 4905 if (qp->q_next) 4906 backq(wqp)->q_next = wqp->q_next; 4907 } 4908 4909 /* The QEND flag might have to be updated for the upstream guy */ 4910 if (qp->q_next) 4911 set_qend(qp->q_next); 4912 4913 ASSERT(_SAMESTR(stp->sd_wrq) == O_SAMESTR(stp->sd_wrq)); 4914 ASSERT(_SAMESTR(_RD(stp->sd_wrq)) == O_SAMESTR(_RD(stp->sd_wrq))); 4915 4916 /* 4917 * Move any messages destined for the put procedures to the next 4918 * syncq in line. Otherwise free them. 4919 */ 4920 moved = 0; 4921 /* 4922 * Quick check to see whether there are any messages or events. 4923 */ 4924 if (qp->q_syncqmsgs != 0 || (qp->q_syncq->sq_flags & SQ_EVENTS)) 4925 moved += propagate_syncq(qp); 4926 if (wqp->q_syncqmsgs != 0 || 4927 (wqp->q_syncq->sq_flags & SQ_EVENTS)) 4928 moved += propagate_syncq(wqp); 4929 4930 strsetuio(stp); 4931 4932 /* 4933 * If this was a module removal, decrement the push count. 4934 */ 4935 if (!isdriver) 4936 stp->sd_pushcnt--; 4937 4938 strunlock(stp, sqlist); 4939 sqlist_free(sqlist); 4940 4941 /* 4942 * Make sure any messages that were propagated are drained. 4943 * Also clear any QFULL bit caused by messages that were propagated. 4944 */ 4945 4946 if (qp->q_next != NULL) { 4947 clr_qfull(qp); 4948 /* 4949 * For the driver calling qprocsoff, propagate_syncq 4950 * frees all the messages instead of putting it in 4951 * the stream head 4952 */ 4953 if (!isdriver && (moved > 0)) 4954 emptysq(qp->q_next->q_syncq); 4955 } 4956 if (wqp->q_next != NULL) { 4957 clr_qfull(wqp); 4958 /* 4959 * We come here for any pop of a module except for the 4960 * case of driver being removed. We don't call emptysq 4961 * if we did not move any messages. This will avoid holding 4962 * PERMOD syncq locks in emptysq 4963 */ 4964 if (moved > 0) 4965 emptysq(wqp->q_next->q_syncq); 4966 } 4967 4968 mutex_enter(SQLOCK(sq)); 4969 sq->sq_rmqcount--; 4970 mutex_exit(SQLOCK(sq)); 4971 } 4972 4973 /* 4974 * Prevent further entry by setting a flag (like SQ_FROZEN, SQ_BLOCKED or 4975 * SQ_WRITER) on a syncq. 4976 * If maxcnt is not -1 it assumes that caller has "maxcnt" claim(s) on the 4977 * sync queue and waits until sq_count reaches maxcnt. 4978 * 4979 * if maxcnt is -1 there's no need to grab sq_putlocks since the caller 4980 * does not care about putnext threads that are in the middle of calling put 4981 * entry points. 4982 * 4983 * This routine is used for both inner and outer syncqs. 4984 */ 4985 static void 4986 blocksq(syncq_t *sq, ushort_t flag, int maxcnt) 4987 { 4988 uint16_t count = 0; 4989 4990 mutex_enter(SQLOCK(sq)); 4991 /* 4992 * Wait for SQ_FROZEN/SQ_BLOCKED to be reset. 4993 * SQ_FROZEN will be set if there is a frozen stream that has a 4994 * queue which also refers to this "shared" syncq. 4995 * SQ_BLOCKED will be set if there is "off" queue which also 4996 * refers to this "shared" syncq. 4997 */ 4998 if (maxcnt != -1) { 4999 count = sq->sq_count; 5000 SQ_PUTLOCKS_ENTER(sq); 5001 SQ_PUTCOUNT_CLRFAST_LOCKED(sq); 5002 SUM_SQ_PUTCOUNTS(sq, count); 5003 } 5004 sq->sq_needexcl++; 5005 ASSERT(sq->sq_needexcl != 0); /* wraparound */ 5006 5007 while ((sq->sq_flags & flag) || 5008 (maxcnt != -1 && count > (unsigned)maxcnt)) { 5009 sq->sq_flags |= SQ_WANTWAKEUP; 5010 if (maxcnt != -1) { 5011 SQ_PUTLOCKS_EXIT(sq); 5012 } 5013 cv_wait(&sq->sq_wait, SQLOCK(sq)); 5014 if (maxcnt != -1) { 5015 count = sq->sq_count; 5016 SQ_PUTLOCKS_ENTER(sq); 5017 SUM_SQ_PUTCOUNTS(sq, count); 5018 } 5019 } 5020 sq->sq_needexcl--; 5021 sq->sq_flags |= flag; 5022 ASSERT(maxcnt == -1 || count == maxcnt); 5023 if (maxcnt != -1) { 5024 if (sq->sq_needexcl == 0) { 5025 SQ_PUTCOUNT_SETFAST_LOCKED(sq); 5026 } 5027 SQ_PUTLOCKS_EXIT(sq); 5028 } else if (sq->sq_needexcl == 0) { 5029 SQ_PUTCOUNT_SETFAST(sq); 5030 } 5031 5032 mutex_exit(SQLOCK(sq)); 5033 } 5034 5035 /* 5036 * Reset a flag that was set with blocksq. 5037 * 5038 * Can not use this routine to reset SQ_WRITER. 5039 * 5040 * If "isouter" is set then the syncq is assumed to be an outer perimeter 5041 * and drain_syncq is not called. Instead we rely on the qwriter_outer thread 5042 * to handle the queued qwriter operations. 5043 * 5044 * no need to grab sq_putlocks here. See comment in strsubr.h that explains when 5045 * sq_putlocks are used. 5046 */ 5047 static void 5048 unblocksq(syncq_t *sq, uint16_t resetflag, int isouter) 5049 { 5050 uint16_t flags; 5051 5052 mutex_enter(SQLOCK(sq)); 5053 ASSERT(resetflag != SQ_WRITER); 5054 ASSERT(sq->sq_flags & resetflag); 5055 flags = sq->sq_flags & ~resetflag; 5056 sq->sq_flags = flags; 5057 if (flags & (SQ_QUEUED | SQ_WANTWAKEUP)) { 5058 if (flags & SQ_WANTWAKEUP) { 5059 flags &= ~SQ_WANTWAKEUP; 5060 cv_broadcast(&sq->sq_wait); 5061 } 5062 sq->sq_flags = flags; 5063 if ((flags & SQ_QUEUED) && !(flags & (SQ_STAYAWAY|SQ_EXCL))) { 5064 if (!isouter) { 5065 /* drain_syncq drops SQLOCK */ 5066 drain_syncq(sq); 5067 return; 5068 } 5069 } 5070 } 5071 mutex_exit(SQLOCK(sq)); 5072 } 5073 5074 /* 5075 * Reset a flag that was set with blocksq. 5076 * Does not drain the syncq. Use emptysq() for that. 5077 * Returns 1 if SQ_QUEUED is set. Otherwise 0. 5078 * 5079 * no need to grab sq_putlocks here. See comment in strsubr.h that explains when 5080 * sq_putlocks are used. 5081 */ 5082 static int 5083 dropsq(syncq_t *sq, uint16_t resetflag) 5084 { 5085 uint16_t flags; 5086 5087 mutex_enter(SQLOCK(sq)); 5088 ASSERT(sq->sq_flags & resetflag); 5089 flags = sq->sq_flags & ~resetflag; 5090 if (flags & SQ_WANTWAKEUP) { 5091 flags &= ~SQ_WANTWAKEUP; 5092 cv_broadcast(&sq->sq_wait); 5093 } 5094 sq->sq_flags = flags; 5095 mutex_exit(SQLOCK(sq)); 5096 if (flags & SQ_QUEUED) 5097 return (1); 5098 return (0); 5099 } 5100 5101 /* 5102 * Empty all the messages on a syncq. 5103 * 5104 * no need to grab sq_putlocks here. See comment in strsubr.h that explains when 5105 * sq_putlocks are used. 5106 */ 5107 static void 5108 emptysq(syncq_t *sq) 5109 { 5110 uint16_t flags; 5111 5112 mutex_enter(SQLOCK(sq)); 5113 flags = sq->sq_flags; 5114 if ((flags & SQ_QUEUED) && !(flags & (SQ_STAYAWAY|SQ_EXCL))) { 5115 /* 5116 * To prevent potential recursive invocation of drain_syncq we 5117 * do not call drain_syncq if count is non-zero. 5118 */ 5119 if (sq->sq_count == 0) { 5120 /* drain_syncq() drops SQLOCK */ 5121 drain_syncq(sq); 5122 return; 5123 } else 5124 sqenable(sq); 5125 } 5126 mutex_exit(SQLOCK(sq)); 5127 } 5128 5129 /* 5130 * Ordered insert while removing duplicates. 5131 */ 5132 static void 5133 sqlist_insert(sqlist_t *sqlist, syncq_t *sqp) 5134 { 5135 syncql_t *sqlp, **prev_sqlpp, *new_sqlp; 5136 5137 prev_sqlpp = &sqlist->sqlist_head; 5138 while ((sqlp = *prev_sqlpp) != NULL) { 5139 if (sqlp->sql_sq >= sqp) { 5140 if (sqlp->sql_sq == sqp) /* duplicate */ 5141 return; 5142 break; 5143 } 5144 prev_sqlpp = &sqlp->sql_next; 5145 } 5146 new_sqlp = &sqlist->sqlist_array[sqlist->sqlist_index++]; 5147 ASSERT((char *)new_sqlp < (char *)sqlist + sqlist->sqlist_size); 5148 new_sqlp->sql_next = sqlp; 5149 new_sqlp->sql_sq = sqp; 5150 *prev_sqlpp = new_sqlp; 5151 } 5152 5153 /* 5154 * Walk the write side queues until we hit either the driver 5155 * or a twist in the stream (_SAMESTR will return false in both 5156 * these cases) then turn around and walk the read side queues 5157 * back up to the stream head. 5158 */ 5159 static void 5160 sqlist_insertall(sqlist_t *sqlist, queue_t *q) 5161 { 5162 while (q != NULL) { 5163 sqlist_insert(sqlist, q->q_syncq); 5164 5165 if (_SAMESTR(q)) 5166 q = q->q_next; 5167 else if (!(q->q_flag & QREADR)) 5168 q = _RD(q); 5169 else 5170 q = NULL; 5171 } 5172 } 5173 5174 /* 5175 * Allocate and build a list of all syncqs in a stream and the syncq(s) 5176 * associated with the "q" parameter. The resulting list is sorted in a 5177 * canonical order and is free of duplicates. 5178 * Assumes the passed queue is a _RD(q). 5179 */ 5180 static sqlist_t * 5181 sqlist_build(queue_t *q, struct stdata *stp, boolean_t do_twist) 5182 { 5183 sqlist_t *sqlist = sqlist_alloc(stp, KM_SLEEP); 5184 5185 /* 5186 * start with the current queue/qpair 5187 */ 5188 ASSERT(q->q_flag & QREADR); 5189 5190 sqlist_insert(sqlist, q->q_syncq); 5191 sqlist_insert(sqlist, _WR(q)->q_syncq); 5192 5193 sqlist_insertall(sqlist, stp->sd_wrq); 5194 if (do_twist) 5195 sqlist_insertall(sqlist, stp->sd_mate->sd_wrq); 5196 5197 return (sqlist); 5198 } 5199 5200 static sqlist_t * 5201 sqlist_alloc(struct stdata *stp, int kmflag) 5202 { 5203 size_t sqlist_size; 5204 sqlist_t *sqlist; 5205 5206 /* 5207 * Allocate 2 syncql_t's for each pushed module. Note that 5208 * the sqlist_t structure already has 4 syncql_t's built in: 5209 * 2 for the stream head, and 2 for the driver/other stream head. 5210 */ 5211 sqlist_size = 2 * sizeof (syncql_t) * stp->sd_pushcnt + 5212 sizeof (sqlist_t); 5213 if (STRMATED(stp)) 5214 sqlist_size += 2 * sizeof (syncql_t) * stp->sd_mate->sd_pushcnt; 5215 sqlist = kmem_alloc(sqlist_size, kmflag); 5216 5217 sqlist->sqlist_head = NULL; 5218 sqlist->sqlist_size = sqlist_size; 5219 sqlist->sqlist_index = 0; 5220 5221 return (sqlist); 5222 } 5223 5224 /* 5225 * Free the list created by sqlist_alloc() 5226 */ 5227 static void 5228 sqlist_free(sqlist_t *sqlist) 5229 { 5230 kmem_free(sqlist, sqlist->sqlist_size); 5231 } 5232 5233 /* 5234 * Prevent any new entries into any syncq in this stream. 5235 * Used by freezestr. 5236 */ 5237 void 5238 strblock(queue_t *q) 5239 { 5240 struct stdata *stp; 5241 syncql_t *sql; 5242 sqlist_t *sqlist; 5243 5244 q = _RD(q); 5245 5246 stp = STREAM(q); 5247 ASSERT(stp != NULL); 5248 5249 /* 5250 * Get a sorted list with all the duplicates removed containing 5251 * all the syncqs referenced by this stream. 5252 */ 5253 sqlist = sqlist_build(q, stp, B_FALSE); 5254 for (sql = sqlist->sqlist_head; sql != NULL; sql = sql->sql_next) 5255 blocksq(sql->sql_sq, SQ_FROZEN, -1); 5256 sqlist_free(sqlist); 5257 } 5258 5259 /* 5260 * Release the block on new entries into this stream 5261 */ 5262 void 5263 strunblock(queue_t *q) 5264 { 5265 struct stdata *stp; 5266 syncql_t *sql; 5267 sqlist_t *sqlist; 5268 int drain_needed; 5269 5270 q = _RD(q); 5271 5272 /* 5273 * Get a sorted list with all the duplicates removed containing 5274 * all the syncqs referenced by this stream. 5275 * Have to drop the SQ_FROZEN flag on all the syncqs before 5276 * starting to drain them; otherwise the draining might 5277 * cause a freezestr in some module on the stream (which 5278 * would deadlock.) 5279 */ 5280 stp = STREAM(q); 5281 ASSERT(stp != NULL); 5282 sqlist = sqlist_build(q, stp, B_FALSE); 5283 drain_needed = 0; 5284 for (sql = sqlist->sqlist_head; sql != NULL; sql = sql->sql_next) 5285 drain_needed += dropsq(sql->sql_sq, SQ_FROZEN); 5286 if (drain_needed) { 5287 for (sql = sqlist->sqlist_head; sql != NULL; 5288 sql = sql->sql_next) 5289 emptysq(sql->sql_sq); 5290 } 5291 sqlist_free(sqlist); 5292 } 5293 5294 #ifdef DEBUG 5295 static int 5296 qprocsareon(queue_t *rq) 5297 { 5298 if (rq->q_next == NULL) 5299 return (0); 5300 return (_WR(rq->q_next)->q_next == _WR(rq)); 5301 } 5302 5303 int 5304 qclaimed(queue_t *q) 5305 { 5306 uint_t count; 5307 5308 count = q->q_syncq->sq_count; 5309 SUM_SQ_PUTCOUNTS(q->q_syncq, count); 5310 return (count != 0); 5311 } 5312 5313 /* 5314 * Check if anyone has frozen this stream with freezestr 5315 */ 5316 int 5317 frozenstr(queue_t *q) 5318 { 5319 return ((q->q_syncq->sq_flags & SQ_FROZEN) != 0); 5320 } 5321 #endif /* DEBUG */ 5322 5323 /* 5324 * Enter a queue. 5325 * Obsoleted interface. Should not be used. 5326 */ 5327 void 5328 enterq(queue_t *q) 5329 { 5330 entersq(q->q_syncq, SQ_CALLBACK); 5331 } 5332 5333 void 5334 leaveq(queue_t *q) 5335 { 5336 leavesq(q->q_syncq, SQ_CALLBACK); 5337 } 5338 5339 /* 5340 * Enter a perimeter. c_inner and c_outer specifies which concurrency bits 5341 * to check. 5342 * Wait if SQ_QUEUED is set to preserve ordering between messages and qwriter 5343 * calls and the running of open, close and service procedures. 5344 * 5345 * if c_inner bit is set no need to grab sq_putlocks since we don't care 5346 * if other threads have entered or are entering put entry point. 5347 * 5348 * if c_inner bit is set it might have been posible to use 5349 * sq_putlocks/sq_putcounts instead of SQLOCK/sq_count (e.g. to optimize 5350 * open/close path for IP) but since the count may need to be decremented in 5351 * qwait() we wouldn't know which counter to decrement. Currently counter is 5352 * selected by current cpu_seqid and current CPU can change at any moment. XXX 5353 * in the future we might use curthread id bits to select the counter and this 5354 * would stay constant across routine calls. 5355 */ 5356 void 5357 entersq(syncq_t *sq, int entrypoint) 5358 { 5359 uint16_t count = 0; 5360 uint16_t flags; 5361 uint16_t waitflags = SQ_STAYAWAY | SQ_EVENTS | SQ_EXCL; 5362 uint16_t type; 5363 uint_t c_inner = entrypoint & SQ_CI; 5364 uint_t c_outer = entrypoint & SQ_CO; 5365 5366 /* 5367 * Increment ref count to keep closes out of this queue. 5368 */ 5369 ASSERT(sq); 5370 ASSERT(c_inner && c_outer); 5371 mutex_enter(SQLOCK(sq)); 5372 flags = sq->sq_flags; 5373 type = sq->sq_type; 5374 if (!(type & c_inner)) { 5375 /* Make sure all putcounts now use slowlock. */ 5376 count = sq->sq_count; 5377 SQ_PUTLOCKS_ENTER(sq); 5378 SQ_PUTCOUNT_CLRFAST_LOCKED(sq); 5379 SUM_SQ_PUTCOUNTS(sq, count); 5380 sq->sq_needexcl++; 5381 ASSERT(sq->sq_needexcl != 0); /* wraparound */ 5382 waitflags |= SQ_MESSAGES; 5383 } 5384 /* 5385 * Wait until we can enter the inner perimeter. 5386 * If we want exclusive access we wait until sq_count is 0. 5387 * We have to do this before entering the outer perimeter in order 5388 * to preserve put/close message ordering. 5389 */ 5390 while ((flags & waitflags) || (!(type & c_inner) && count != 0)) { 5391 sq->sq_flags = flags | SQ_WANTWAKEUP; 5392 if (!(type & c_inner)) { 5393 SQ_PUTLOCKS_EXIT(sq); 5394 } 5395 cv_wait(&sq->sq_wait, SQLOCK(sq)); 5396 if (!(type & c_inner)) { 5397 count = sq->sq_count; 5398 SQ_PUTLOCKS_ENTER(sq); 5399 SUM_SQ_PUTCOUNTS(sq, count); 5400 } 5401 flags = sq->sq_flags; 5402 } 5403 5404 if (!(type & c_inner)) { 5405 ASSERT(sq->sq_needexcl > 0); 5406 sq->sq_needexcl--; 5407 if (sq->sq_needexcl == 0) { 5408 SQ_PUTCOUNT_SETFAST_LOCKED(sq); 5409 } 5410 } 5411 5412 /* Check if we need to enter the outer perimeter */ 5413 if (!(type & c_outer)) { 5414 /* 5415 * We have to enter the outer perimeter exclusively before 5416 * we can increment sq_count to avoid deadlock. This implies 5417 * that we have to re-check sq_flags and sq_count. 5418 * 5419 * is it possible to have c_inner set when c_outer is not set? 5420 */ 5421 if (!(type & c_inner)) { 5422 SQ_PUTLOCKS_EXIT(sq); 5423 } 5424 mutex_exit(SQLOCK(sq)); 5425 outer_enter(sq->sq_outer, SQ_GOAWAY); 5426 mutex_enter(SQLOCK(sq)); 5427 flags = sq->sq_flags; 5428 /* 5429 * there should be no need to recheck sq_putcounts 5430 * because outer_enter() has already waited for them to clear 5431 * after setting SQ_WRITER. 5432 */ 5433 count = sq->sq_count; 5434 #ifdef DEBUG 5435 /* 5436 * SUMCHECK_SQ_PUTCOUNTS should return the sum instead 5437 * of doing an ASSERT internally. Others should do 5438 * something like 5439 * ASSERT(SUMCHECK_SQ_PUTCOUNTS(sq) == 0); 5440 * without the need to #ifdef DEBUG it. 5441 */ 5442 SUMCHECK_SQ_PUTCOUNTS(sq, 0); 5443 #endif 5444 while ((flags & (SQ_EXCL|SQ_BLOCKED|SQ_FROZEN)) || 5445 (!(type & c_inner) && count != 0)) { 5446 sq->sq_flags = flags | SQ_WANTWAKEUP; 5447 cv_wait(&sq->sq_wait, SQLOCK(sq)); 5448 count = sq->sq_count; 5449 flags = sq->sq_flags; 5450 } 5451 } 5452 5453 sq->sq_count++; 5454 ASSERT(sq->sq_count != 0); /* Wraparound */ 5455 if (!(type & c_inner)) { 5456 /* Exclusive entry */ 5457 ASSERT(sq->sq_count == 1); 5458 sq->sq_flags |= SQ_EXCL; 5459 if (type & c_outer) { 5460 SQ_PUTLOCKS_EXIT(sq); 5461 } 5462 } 5463 mutex_exit(SQLOCK(sq)); 5464 } 5465 5466 /* 5467 * leave a syncq. announce to framework that closes may proceed. 5468 * c_inner and c_outer specifies which concurrency bits 5469 * to check. 5470 * 5471 * must never be called from driver or module put entry point. 5472 * 5473 * no need to grab sq_putlocks here. See comment in strsubr.h that explains when 5474 * sq_putlocks are used. 5475 */ 5476 void 5477 leavesq(syncq_t *sq, int entrypoint) 5478 { 5479 uint16_t flags; 5480 uint16_t type; 5481 uint_t c_outer = entrypoint & SQ_CO; 5482 #ifdef DEBUG 5483 uint_t c_inner = entrypoint & SQ_CI; 5484 #endif 5485 5486 /* 5487 * decrement ref count, drain the syncq if possible, and wake up 5488 * any waiting close. 5489 */ 5490 ASSERT(sq); 5491 ASSERT(c_inner && c_outer); 5492 mutex_enter(SQLOCK(sq)); 5493 flags = sq->sq_flags; 5494 type = sq->sq_type; 5495 if (flags & (SQ_QUEUED|SQ_WANTWAKEUP|SQ_WANTEXWAKEUP)) { 5496 5497 if (flags & SQ_WANTWAKEUP) { 5498 flags &= ~SQ_WANTWAKEUP; 5499 cv_broadcast(&sq->sq_wait); 5500 } 5501 if (flags & SQ_WANTEXWAKEUP) { 5502 flags &= ~SQ_WANTEXWAKEUP; 5503 cv_broadcast(&sq->sq_exitwait); 5504 } 5505 5506 if ((flags & SQ_QUEUED) && !(flags & SQ_STAYAWAY)) { 5507 /* 5508 * The syncq needs to be drained. "Exit" the syncq 5509 * before calling drain_syncq. 5510 */ 5511 ASSERT(sq->sq_count != 0); 5512 sq->sq_count--; 5513 ASSERT((flags & SQ_EXCL) || (type & c_inner)); 5514 sq->sq_flags = flags & ~SQ_EXCL; 5515 drain_syncq(sq); 5516 ASSERT(MUTEX_NOT_HELD(SQLOCK(sq))); 5517 /* Check if we need to exit the outer perimeter */ 5518 /* XXX will this ever be true? */ 5519 if (!(type & c_outer)) 5520 outer_exit(sq->sq_outer); 5521 return; 5522 } 5523 } 5524 ASSERT(sq->sq_count != 0); 5525 sq->sq_count--; 5526 ASSERT((flags & SQ_EXCL) || (type & c_inner)); 5527 sq->sq_flags = flags & ~SQ_EXCL; 5528 mutex_exit(SQLOCK(sq)); 5529 5530 /* Check if we need to exit the outer perimeter */ 5531 if (!(sq->sq_type & c_outer)) 5532 outer_exit(sq->sq_outer); 5533 } 5534 5535 /* 5536 * Prevent q_next from changing in this stream by incrementing sq_count. 5537 * 5538 * no need to grab sq_putlocks here. See comment in strsubr.h that explains when 5539 * sq_putlocks are used. 5540 */ 5541 void 5542 claimq(queue_t *qp) 5543 { 5544 syncq_t *sq = qp->q_syncq; 5545 5546 mutex_enter(SQLOCK(sq)); 5547 sq->sq_count++; 5548 ASSERT(sq->sq_count != 0); /* Wraparound */ 5549 mutex_exit(SQLOCK(sq)); 5550 } 5551 5552 /* 5553 * Undo claimq. 5554 * 5555 * no need to grab sq_putlocks here. See comment in strsubr.h that explains when 5556 * sq_putlocks are used. 5557 */ 5558 void 5559 releaseq(queue_t *qp) 5560 { 5561 syncq_t *sq = qp->q_syncq; 5562 uint16_t flags; 5563 5564 mutex_enter(SQLOCK(sq)); 5565 ASSERT(sq->sq_count > 0); 5566 sq->sq_count--; 5567 5568 flags = sq->sq_flags; 5569 if (flags & (SQ_WANTWAKEUP|SQ_QUEUED)) { 5570 if (flags & SQ_WANTWAKEUP) { 5571 flags &= ~SQ_WANTWAKEUP; 5572 cv_broadcast(&sq->sq_wait); 5573 } 5574 sq->sq_flags = flags; 5575 if ((flags & SQ_QUEUED) && !(flags & (SQ_STAYAWAY|SQ_EXCL))) { 5576 /* 5577 * To prevent potential recursive invocation of 5578 * drain_syncq we do not call drain_syncq if count is 5579 * non-zero. 5580 */ 5581 if (sq->sq_count == 0) { 5582 drain_syncq(sq); 5583 return; 5584 } else 5585 sqenable(sq); 5586 } 5587 } 5588 mutex_exit(SQLOCK(sq)); 5589 } 5590 5591 /* 5592 * Prevent q_next from changing in this stream by incrementing sd_refcnt. 5593 */ 5594 void 5595 claimstr(queue_t *qp) 5596 { 5597 struct stdata *stp = STREAM(qp); 5598 5599 mutex_enter(&stp->sd_reflock); 5600 stp->sd_refcnt++; 5601 ASSERT(stp->sd_refcnt != 0); /* Wraparound */ 5602 mutex_exit(&stp->sd_reflock); 5603 } 5604 5605 /* 5606 * Undo claimstr. 5607 */ 5608 void 5609 releasestr(queue_t *qp) 5610 { 5611 struct stdata *stp = STREAM(qp); 5612 5613 mutex_enter(&stp->sd_reflock); 5614 ASSERT(stp->sd_refcnt != 0); 5615 if (--stp->sd_refcnt == 0) 5616 cv_broadcast(&stp->sd_refmonitor); 5617 mutex_exit(&stp->sd_reflock); 5618 } 5619 5620 static syncq_t * 5621 new_syncq(void) 5622 { 5623 return (kmem_cache_alloc(syncq_cache, KM_SLEEP)); 5624 } 5625 5626 static void 5627 free_syncq(syncq_t *sq) 5628 { 5629 ASSERT(sq->sq_head == NULL); 5630 ASSERT(sq->sq_outer == NULL); 5631 ASSERT(sq->sq_callbpend == NULL); 5632 ASSERT((sq->sq_onext == NULL && sq->sq_oprev == NULL) || 5633 (sq->sq_onext == sq && sq->sq_oprev == sq)); 5634 5635 if (sq->sq_ciputctrl != NULL) { 5636 ASSERT(sq->sq_nciputctrl == n_ciputctrl - 1); 5637 SUMCHECK_CIPUTCTRL_COUNTS(sq->sq_ciputctrl, 5638 sq->sq_nciputctrl, 0); 5639 ASSERT(ciputctrl_cache != NULL); 5640 kmem_cache_free(ciputctrl_cache, sq->sq_ciputctrl); 5641 } 5642 5643 sq->sq_tail = NULL; 5644 sq->sq_evhead = NULL; 5645 sq->sq_evtail = NULL; 5646 sq->sq_ciputctrl = NULL; 5647 sq->sq_nciputctrl = 0; 5648 sq->sq_count = 0; 5649 sq->sq_rmqcount = 0; 5650 sq->sq_callbflags = 0; 5651 sq->sq_cancelid = 0; 5652 sq->sq_next = NULL; 5653 sq->sq_needexcl = 0; 5654 sq->sq_svcflags = 0; 5655 sq->sq_nqueues = 0; 5656 sq->sq_pri = 0; 5657 sq->sq_onext = NULL; 5658 sq->sq_oprev = NULL; 5659 sq->sq_flags = 0; 5660 sq->sq_type = 0; 5661 sq->sq_servcount = 0; 5662 5663 kmem_cache_free(syncq_cache, sq); 5664 } 5665 5666 /* Outer perimeter code */ 5667 5668 /* 5669 * The outer syncq uses the fields and flags in the syncq slightly 5670 * differently from the inner syncqs. 5671 * sq_count Incremented when there are pending or running 5672 * writers at the outer perimeter to prevent the set of 5673 * inner syncqs that belong to the outer perimeter from 5674 * changing. 5675 * sq_head/tail List of deferred qwriter(OUTER) operations. 5676 * 5677 * SQ_BLOCKED Set to prevent traversing of sq_next,sq_prev while 5678 * inner syncqs are added to or removed from the 5679 * outer perimeter. 5680 * SQ_QUEUED sq_head/tail has messages or eventsqueued. 5681 * 5682 * SQ_WRITER A thread is currently traversing all the inner syncqs 5683 * setting the SQ_WRITER flag. 5684 */ 5685 5686 /* 5687 * Get write access at the outer perimeter. 5688 * Note that read access is done by entersq, putnext, and put by simply 5689 * incrementing sq_count in the inner syncq. 5690 * 5691 * Waits until "flags" is no longer set in the outer to prevent multiple 5692 * threads from having write access at the same time. SQ_WRITER has to be part 5693 * of "flags". 5694 * 5695 * Increases sq_count on the outer syncq to keep away outer_insert/remove 5696 * until the outer_exit is finished. 5697 * 5698 * outer_enter is vulnerable to starvation since it does not prevent new 5699 * threads from entering the inner syncqs while it is waiting for sq_count to 5700 * go to zero. 5701 */ 5702 void 5703 outer_enter(syncq_t *outer, uint16_t flags) 5704 { 5705 syncq_t *sq; 5706 int wait_needed; 5707 uint16_t count; 5708 5709 ASSERT(outer->sq_outer == NULL && outer->sq_onext != NULL && 5710 outer->sq_oprev != NULL); 5711 ASSERT(flags & SQ_WRITER); 5712 5713 retry: 5714 mutex_enter(SQLOCK(outer)); 5715 while (outer->sq_flags & flags) { 5716 outer->sq_flags |= SQ_WANTWAKEUP; 5717 cv_wait(&outer->sq_wait, SQLOCK(outer)); 5718 } 5719 5720 ASSERT(!(outer->sq_flags & SQ_WRITER)); 5721 outer->sq_flags |= SQ_WRITER; 5722 outer->sq_count++; 5723 ASSERT(outer->sq_count != 0); /* wraparound */ 5724 wait_needed = 0; 5725 /* 5726 * Set SQ_WRITER on all the inner syncqs while holding 5727 * the SQLOCK on the outer syncq. This ensures that the changing 5728 * of SQ_WRITER is atomic under the outer SQLOCK. 5729 */ 5730 for (sq = outer->sq_onext; sq != outer; sq = sq->sq_onext) { 5731 mutex_enter(SQLOCK(sq)); 5732 count = sq->sq_count; 5733 SQ_PUTLOCKS_ENTER(sq); 5734 sq->sq_flags |= SQ_WRITER; 5735 SUM_SQ_PUTCOUNTS(sq, count); 5736 if (count != 0) 5737 wait_needed = 1; 5738 SQ_PUTLOCKS_EXIT(sq); 5739 mutex_exit(SQLOCK(sq)); 5740 } 5741 mutex_exit(SQLOCK(outer)); 5742 5743 /* 5744 * Get everybody out of the syncqs sequentially. 5745 * Note that we don't actually need to aqiure the PUTLOCKS, since 5746 * we have already cleared the fastbit, and set QWRITER. By 5747 * definition, the count can not increase since putnext will 5748 * take the slowlock path (and the purpose of aquiring the 5749 * putlocks was to make sure it didn't increase while we were 5750 * waiting). 5751 * 5752 * Note that we still aquire the PUTLOCKS to be safe. 5753 */ 5754 if (wait_needed) { 5755 for (sq = outer->sq_onext; sq != outer; sq = sq->sq_onext) { 5756 mutex_enter(SQLOCK(sq)); 5757 count = sq->sq_count; 5758 SQ_PUTLOCKS_ENTER(sq); 5759 SUM_SQ_PUTCOUNTS(sq, count); 5760 while (count != 0) { 5761 sq->sq_flags |= SQ_WANTWAKEUP; 5762 SQ_PUTLOCKS_EXIT(sq); 5763 cv_wait(&sq->sq_wait, SQLOCK(sq)); 5764 count = sq->sq_count; 5765 SQ_PUTLOCKS_ENTER(sq); 5766 SUM_SQ_PUTCOUNTS(sq, count); 5767 } 5768 SQ_PUTLOCKS_EXIT(sq); 5769 mutex_exit(SQLOCK(sq)); 5770 } 5771 /* 5772 * Verify that none of the flags got set while we 5773 * were waiting for the sq_counts to drop. 5774 * If this happens we exit and retry entering the 5775 * outer perimeter. 5776 */ 5777 mutex_enter(SQLOCK(outer)); 5778 if (outer->sq_flags & (flags & ~SQ_WRITER)) { 5779 mutex_exit(SQLOCK(outer)); 5780 outer_exit(outer); 5781 goto retry; 5782 } 5783 mutex_exit(SQLOCK(outer)); 5784 } 5785 } 5786 5787 /* 5788 * Drop the write access at the outer perimeter. 5789 * Read access is dropped implicitly (by putnext, put, and leavesq) by 5790 * decrementing sq_count. 5791 */ 5792 void 5793 outer_exit(syncq_t *outer) 5794 { 5795 syncq_t *sq; 5796 int drain_needed; 5797 uint16_t flags; 5798 5799 ASSERT(outer->sq_outer == NULL && outer->sq_onext != NULL && 5800 outer->sq_oprev != NULL); 5801 ASSERT(MUTEX_NOT_HELD(SQLOCK(outer))); 5802 5803 /* 5804 * Atomically (from the perspective of threads calling become_writer) 5805 * drop the write access at the outer perimeter by holding 5806 * SQLOCK(outer) across all the dropsq calls and the resetting of 5807 * SQ_WRITER. 5808 * This defines a locking order between the outer perimeter 5809 * SQLOCK and the inner perimeter SQLOCKs. 5810 */ 5811 mutex_enter(SQLOCK(outer)); 5812 flags = outer->sq_flags; 5813 ASSERT(outer->sq_flags & SQ_WRITER); 5814 if (flags & SQ_QUEUED) { 5815 write_now(outer); 5816 flags = outer->sq_flags; 5817 } 5818 5819 /* 5820 * sq_onext is stable since sq_count has not yet been decreased. 5821 * Reset the SQ_WRITER flags in all syncqs. 5822 * After dropping SQ_WRITER on the outer syncq we empty all the 5823 * inner syncqs. 5824 */ 5825 drain_needed = 0; 5826 for (sq = outer->sq_onext; sq != outer; sq = sq->sq_onext) 5827 drain_needed += dropsq(sq, SQ_WRITER); 5828 ASSERT(!(outer->sq_flags & SQ_QUEUED)); 5829 flags &= ~SQ_WRITER; 5830 if (drain_needed) { 5831 outer->sq_flags = flags; 5832 mutex_exit(SQLOCK(outer)); 5833 for (sq = outer->sq_onext; sq != outer; sq = sq->sq_onext) 5834 emptysq(sq); 5835 mutex_enter(SQLOCK(outer)); 5836 flags = outer->sq_flags; 5837 } 5838 if (flags & SQ_WANTWAKEUP) { 5839 flags &= ~SQ_WANTWAKEUP; 5840 cv_broadcast(&outer->sq_wait); 5841 } 5842 outer->sq_flags = flags; 5843 ASSERT(outer->sq_count > 0); 5844 outer->sq_count--; 5845 mutex_exit(SQLOCK(outer)); 5846 } 5847 5848 /* 5849 * Add another syncq to an outer perimeter. 5850 * Block out all other access to the outer perimeter while it is being 5851 * changed using blocksq. 5852 * Assumes that the caller has *not* done an outer_enter. 5853 * 5854 * Vulnerable to starvation in blocksq. 5855 */ 5856 static void 5857 outer_insert(syncq_t *outer, syncq_t *sq) 5858 { 5859 ASSERT(outer->sq_outer == NULL && outer->sq_onext != NULL && 5860 outer->sq_oprev != NULL); 5861 ASSERT(sq->sq_outer == NULL && sq->sq_onext == NULL && 5862 sq->sq_oprev == NULL); /* Can't be in an outer perimeter */ 5863 5864 /* Get exclusive access to the outer perimeter list */ 5865 blocksq(outer, SQ_BLOCKED, 0); 5866 ASSERT(outer->sq_flags & SQ_BLOCKED); 5867 ASSERT(!(outer->sq_flags & SQ_WRITER)); 5868 5869 mutex_enter(SQLOCK(sq)); 5870 sq->sq_outer = outer; 5871 outer->sq_onext->sq_oprev = sq; 5872 sq->sq_onext = outer->sq_onext; 5873 outer->sq_onext = sq; 5874 sq->sq_oprev = outer; 5875 mutex_exit(SQLOCK(sq)); 5876 unblocksq(outer, SQ_BLOCKED, 1); 5877 } 5878 5879 /* 5880 * Remove a syncq from an outer perimeter. 5881 * Block out all other access to the outer perimeter while it is being 5882 * changed using blocksq. 5883 * Assumes that the caller has *not* done an outer_enter. 5884 * 5885 * Vulnerable to starvation in blocksq. 5886 */ 5887 static void 5888 outer_remove(syncq_t *outer, syncq_t *sq) 5889 { 5890 ASSERT(outer->sq_outer == NULL && outer->sq_onext != NULL && 5891 outer->sq_oprev != NULL); 5892 ASSERT(sq->sq_outer == outer); 5893 5894 /* Get exclusive access to the outer perimeter list */ 5895 blocksq(outer, SQ_BLOCKED, 0); 5896 ASSERT(outer->sq_flags & SQ_BLOCKED); 5897 ASSERT(!(outer->sq_flags & SQ_WRITER)); 5898 5899 mutex_enter(SQLOCK(sq)); 5900 sq->sq_outer = NULL; 5901 sq->sq_onext->sq_oprev = sq->sq_oprev; 5902 sq->sq_oprev->sq_onext = sq->sq_onext; 5903 sq->sq_oprev = sq->sq_onext = NULL; 5904 mutex_exit(SQLOCK(sq)); 5905 unblocksq(outer, SQ_BLOCKED, 1); 5906 } 5907 5908 /* 5909 * Queue a deferred qwriter(OUTER) callback for this outer perimeter. 5910 * If this is the first callback for this outer perimeter then add 5911 * this outer perimeter to the list of outer perimeters that 5912 * the qwriter_outer_thread will process. 5913 * 5914 * Increments sq_count in the outer syncq to prevent the membership 5915 * of the outer perimeter (in terms of inner syncqs) to change while 5916 * the callback is pending. 5917 */ 5918 static void 5919 queue_writer(syncq_t *outer, void (*func)(), queue_t *q, mblk_t *mp) 5920 { 5921 ASSERT(MUTEX_HELD(SQLOCK(outer))); 5922 5923 mp->b_prev = (mblk_t *)func; 5924 mp->b_queue = q; 5925 mp->b_next = NULL; 5926 outer->sq_count++; /* Decremented when dequeued */ 5927 ASSERT(outer->sq_count != 0); /* Wraparound */ 5928 if (outer->sq_evhead == NULL) { 5929 /* First message. */ 5930 outer->sq_evhead = outer->sq_evtail = mp; 5931 outer->sq_flags |= SQ_EVENTS; 5932 mutex_exit(SQLOCK(outer)); 5933 STRSTAT(qwr_outer); 5934 (void) taskq_dispatch(streams_taskq, 5935 (task_func_t *)qwriter_outer_service, outer, TQ_SLEEP); 5936 } else { 5937 ASSERT(outer->sq_flags & SQ_EVENTS); 5938 outer->sq_evtail->b_next = mp; 5939 outer->sq_evtail = mp; 5940 mutex_exit(SQLOCK(outer)); 5941 } 5942 } 5943 5944 /* 5945 * Try and upgrade to write access at the outer perimeter. If this can 5946 * not be done without blocking then queue the callback to be done 5947 * by the qwriter_outer_thread. 5948 * 5949 * This routine can only be called from put or service procedures plus 5950 * asynchronous callback routines that have properly entered to 5951 * queue (with entersq.) Thus qwriter(OUTER) assumes the caller has one claim 5952 * on the syncq associated with q. 5953 */ 5954 void 5955 qwriter_outer(queue_t *q, mblk_t *mp, void (*func)()) 5956 { 5957 syncq_t *osq, *sq, *outer; 5958 int failed; 5959 uint16_t flags; 5960 5961 osq = q->q_syncq; 5962 outer = osq->sq_outer; 5963 if (outer == NULL) 5964 panic("qwriter(PERIM_OUTER): no outer perimeter"); 5965 ASSERT(outer->sq_outer == NULL && outer->sq_onext != NULL && 5966 outer->sq_oprev != NULL); 5967 5968 mutex_enter(SQLOCK(outer)); 5969 flags = outer->sq_flags; 5970 /* 5971 * If some thread is traversing sq_next, or if we are blocked by 5972 * outer_insert or outer_remove, or if the we already have queued 5973 * callbacks, then queue this callback for later processing. 5974 * 5975 * Also queue the qwriter for an interrupt thread in order 5976 * to reduce the time spent running at high IPL. 5977 * to identify there are events. 5978 */ 5979 if ((flags & SQ_GOAWAY) || (curthread->t_pri >= kpreemptpri)) { 5980 /* 5981 * Queue the become_writer request. 5982 * The queueing is atomic under SQLOCK(outer) in order 5983 * to synchronize with outer_exit. 5984 * queue_writer will drop the outer SQLOCK 5985 */ 5986 if (flags & SQ_BLOCKED) { 5987 /* Must set SQ_WRITER on inner perimeter */ 5988 mutex_enter(SQLOCK(osq)); 5989 osq->sq_flags |= SQ_WRITER; 5990 mutex_exit(SQLOCK(osq)); 5991 } else { 5992 if (!(flags & SQ_WRITER)) { 5993 /* 5994 * The outer could have been SQ_BLOCKED thus 5995 * SQ_WRITER might not be set on the inner. 5996 */ 5997 mutex_enter(SQLOCK(osq)); 5998 osq->sq_flags |= SQ_WRITER; 5999 mutex_exit(SQLOCK(osq)); 6000 } 6001 ASSERT(osq->sq_flags & SQ_WRITER); 6002 } 6003 queue_writer(outer, func, q, mp); 6004 return; 6005 } 6006 /* 6007 * We are half-way to exclusive access to the outer perimeter. 6008 * Prevent any outer_enter, qwriter(OUTER), or outer_insert/remove 6009 * while the inner syncqs are traversed. 6010 */ 6011 outer->sq_count++; 6012 ASSERT(outer->sq_count != 0); /* wraparound */ 6013 flags |= SQ_WRITER; 6014 /* 6015 * Check if we can run the function immediately. Mark all 6016 * syncqs with the writer flag to prevent new entries into 6017 * put and service procedures. 6018 * 6019 * Set SQ_WRITER on all the inner syncqs while holding 6020 * the SQLOCK on the outer syncq. This ensures that the changing 6021 * of SQ_WRITER is atomic under the outer SQLOCK. 6022 */ 6023 failed = 0; 6024 for (sq = outer->sq_onext; sq != outer; sq = sq->sq_onext) { 6025 uint16_t count; 6026 uint_t maxcnt = (sq == osq) ? 1 : 0; 6027 6028 mutex_enter(SQLOCK(sq)); 6029 count = sq->sq_count; 6030 SQ_PUTLOCKS_ENTER(sq); 6031 SUM_SQ_PUTCOUNTS(sq, count); 6032 if (sq->sq_count > maxcnt) 6033 failed = 1; 6034 sq->sq_flags |= SQ_WRITER; 6035 SQ_PUTLOCKS_EXIT(sq); 6036 mutex_exit(SQLOCK(sq)); 6037 } 6038 if (failed) { 6039 /* 6040 * Some other thread has a read claim on the outer perimeter. 6041 * Queue the callback for deferred processing. 6042 * 6043 * queue_writer will set SQ_QUEUED before we drop SQ_WRITER 6044 * so that other qwriter(OUTER) calls will queue their 6045 * callbacks as well. queue_writer increments sq_count so we 6046 * decrement to compensate for the our increment. 6047 * 6048 * Dropping SQ_WRITER enables the writer thread to work 6049 * on this outer perimeter. 6050 */ 6051 outer->sq_flags = flags; 6052 queue_writer(outer, func, q, mp); 6053 /* queue_writer dropper the lock */ 6054 mutex_enter(SQLOCK(outer)); 6055 ASSERT(outer->sq_count > 0); 6056 outer->sq_count--; 6057 ASSERT(outer->sq_flags & SQ_WRITER); 6058 flags = outer->sq_flags; 6059 flags &= ~SQ_WRITER; 6060 if (flags & SQ_WANTWAKEUP) { 6061 flags &= ~SQ_WANTWAKEUP; 6062 cv_broadcast(&outer->sq_wait); 6063 } 6064 outer->sq_flags = flags; 6065 mutex_exit(SQLOCK(outer)); 6066 return; 6067 } else { 6068 outer->sq_flags = flags; 6069 mutex_exit(SQLOCK(outer)); 6070 } 6071 6072 /* Can run it immediately */ 6073 (*func)(q, mp); 6074 6075 outer_exit(outer); 6076 } 6077 6078 /* 6079 * Dequeue all writer callbacks from the outer perimeter and run them. 6080 */ 6081 static void 6082 write_now(syncq_t *outer) 6083 { 6084 mblk_t *mp; 6085 queue_t *q; 6086 void (*func)(); 6087 6088 ASSERT(MUTEX_HELD(SQLOCK(outer))); 6089 ASSERT(outer->sq_outer == NULL && outer->sq_onext != NULL && 6090 outer->sq_oprev != NULL); 6091 while ((mp = outer->sq_evhead) != NULL) { 6092 /* 6093 * queues cannot be placed on the queuelist on the outer 6094 * perimiter. 6095 */ 6096 ASSERT(!(outer->sq_flags & SQ_MESSAGES)); 6097 ASSERT((outer->sq_flags & SQ_EVENTS)); 6098 6099 outer->sq_evhead = mp->b_next; 6100 if (outer->sq_evhead == NULL) { 6101 outer->sq_evtail = NULL; 6102 outer->sq_flags &= ~SQ_EVENTS; 6103 } 6104 ASSERT(outer->sq_count != 0); 6105 outer->sq_count--; /* Incremented when enqueued. */ 6106 mutex_exit(SQLOCK(outer)); 6107 /* 6108 * Drop the message if the queue is closing. 6109 * Make sure that the queue is "claimed" when the callback 6110 * is run in order to satisfy various ASSERTs. 6111 */ 6112 q = mp->b_queue; 6113 func = (void (*)())mp->b_prev; 6114 ASSERT(func != NULL); 6115 mp->b_next = mp->b_prev = NULL; 6116 if (q->q_flag & QWCLOSE) { 6117 freemsg(mp); 6118 } else { 6119 claimq(q); 6120 (*func)(q, mp); 6121 releaseq(q); 6122 } 6123 mutex_enter(SQLOCK(outer)); 6124 } 6125 ASSERT(MUTEX_HELD(SQLOCK(outer))); 6126 } 6127 6128 /* 6129 * The list of messages on the inner syncq is effectively hashed 6130 * by destination queue. These destination queues are doubly 6131 * linked lists (hopefully) in priority order. Messages are then 6132 * put on the queue referenced by the q_sqhead/q_sqtail elements. 6133 * Additional messages are linked together by the b_next/b_prev 6134 * elements in the mblk, with (similar to putq()) the first message 6135 * having a NULL b_prev and the last message having a NULL b_next. 6136 * 6137 * Events, such as qwriter callbacks, are put onto a list in FIFO 6138 * order referenced by sq_evhead, and sq_evtail. This is a singly 6139 * linked list, and messages here MUST be processed in the order queued. 6140 */ 6141 6142 /* 6143 * Run the events on the syncq event list (sq_evhead). 6144 * Assumes there is only one claim on the syncq, it is 6145 * already exclusive (SQ_EXCL set), and the SQLOCK held. 6146 * Messages here are processed in order, with the SQ_EXCL bit 6147 * held all the way through till the last message is processed. 6148 */ 6149 void 6150 sq_run_events(syncq_t *sq) 6151 { 6152 mblk_t *bp; 6153 queue_t *qp; 6154 uint16_t flags = sq->sq_flags; 6155 void (*func)(); 6156 6157 ASSERT(MUTEX_HELD(SQLOCK(sq))); 6158 ASSERT((sq->sq_outer == NULL && sq->sq_onext == NULL && 6159 sq->sq_oprev == NULL) || 6160 (sq->sq_outer != NULL && sq->sq_onext != NULL && 6161 sq->sq_oprev != NULL)); 6162 6163 ASSERT(flags & SQ_EXCL); 6164 ASSERT(sq->sq_count == 1); 6165 6166 /* 6167 * We need to process all of the events on this list. It 6168 * is possible that new events will be added while we are 6169 * away processing a callback, so on every loop, we start 6170 * back at the beginning of the list. 6171 */ 6172 /* 6173 * We have to reaccess sq_evhead since there is a 6174 * possibility of a new entry while we were running 6175 * the callback. 6176 */ 6177 for (bp = sq->sq_evhead; bp != NULL; bp = sq->sq_evhead) { 6178 ASSERT(bp->b_queue->q_syncq == sq); 6179 ASSERT(sq->sq_flags & SQ_EVENTS); 6180 6181 qp = bp->b_queue; 6182 func = (void (*)())bp->b_prev; 6183 ASSERT(func != NULL); 6184 6185 /* 6186 * Messages from the event queue must be taken off in 6187 * FIFO order. 6188 */ 6189 ASSERT(sq->sq_evhead == bp); 6190 sq->sq_evhead = bp->b_next; 6191 6192 if (bp->b_next == NULL) { 6193 /* Deleting last */ 6194 ASSERT(sq->sq_evtail == bp); 6195 sq->sq_evtail = NULL; 6196 sq->sq_flags &= ~SQ_EVENTS; 6197 } 6198 bp->b_prev = bp->b_next = NULL; 6199 ASSERT(bp->b_datap->db_ref != 0); 6200 6201 mutex_exit(SQLOCK(sq)); 6202 6203 (*func)(qp, bp); 6204 6205 mutex_enter(SQLOCK(sq)); 6206 /* 6207 * re-read the flags, since they could have changed. 6208 */ 6209 flags = sq->sq_flags; 6210 ASSERT(flags & SQ_EXCL); 6211 } 6212 ASSERT(sq->sq_evhead == NULL && sq->sq_evtail == NULL); 6213 ASSERT(!(sq->sq_flags & SQ_EVENTS)); 6214 6215 if (flags & SQ_WANTWAKEUP) { 6216 flags &= ~SQ_WANTWAKEUP; 6217 cv_broadcast(&sq->sq_wait); 6218 } 6219 if (flags & SQ_WANTEXWAKEUP) { 6220 flags &= ~SQ_WANTEXWAKEUP; 6221 cv_broadcast(&sq->sq_exitwait); 6222 } 6223 sq->sq_flags = flags; 6224 } 6225 6226 /* 6227 * Put messages on the event list. 6228 * If we can go exclusive now, do so and process the event list, otherwise 6229 * let the last claim service this list (or wake the sqthread). 6230 * This procedure assumes SQLOCK is held. To run the event list, it 6231 * must be called with no claims. 6232 */ 6233 static void 6234 sqfill_events(syncq_t *sq, queue_t *q, mblk_t *mp, void (*func)()) 6235 { 6236 uint16_t count; 6237 6238 ASSERT(MUTEX_HELD(SQLOCK(sq))); 6239 ASSERT(func != NULL); 6240 6241 /* 6242 * This is a callback. Add it to the list of callbacks 6243 * and see about upgrading. 6244 */ 6245 mp->b_prev = (mblk_t *)func; 6246 mp->b_queue = q; 6247 mp->b_next = NULL; 6248 if (sq->sq_evhead == NULL) { 6249 sq->sq_evhead = sq->sq_evtail = mp; 6250 sq->sq_flags |= SQ_EVENTS; 6251 } else { 6252 ASSERT(sq->sq_evtail != NULL); 6253 ASSERT(sq->sq_evtail->b_next == NULL); 6254 ASSERT(sq->sq_flags & SQ_EVENTS); 6255 sq->sq_evtail->b_next = mp; 6256 sq->sq_evtail = mp; 6257 } 6258 /* 6259 * We have set SQ_EVENTS, so threads will have to 6260 * unwind out of the perimiter, and new entries will 6261 * not grab a putlock. But we still need to know 6262 * how many threads have already made a claim to the 6263 * syncq, so grab the putlocks, and sum the counts. 6264 * If there are no claims on the syncq, we can upgrade 6265 * to exclusive, and run the event list. 6266 * NOTE: We hold the SQLOCK, so we can just grab the 6267 * putlocks. 6268 */ 6269 count = sq->sq_count; 6270 SQ_PUTLOCKS_ENTER(sq); 6271 SUM_SQ_PUTCOUNTS(sq, count); 6272 /* 6273 * We have no claim, so we need to check if there 6274 * are no others, then we can upgrade. 6275 */ 6276 /* 6277 * There are currently no claims on 6278 * the syncq by this thread (at least on this entry). The thread who has 6279 * the claim should drain syncq. 6280 */ 6281 if (count > 0) { 6282 /* 6283 * Can't upgrade - other threads inside. 6284 */ 6285 SQ_PUTLOCKS_EXIT(sq); 6286 mutex_exit(SQLOCK(sq)); 6287 return; 6288 } 6289 /* 6290 * Need to set SQ_EXCL and make a claim on the syncq. 6291 */ 6292 ASSERT((sq->sq_flags & SQ_EXCL) == 0); 6293 sq->sq_flags |= SQ_EXCL; 6294 ASSERT(sq->sq_count == 0); 6295 sq->sq_count++; 6296 SQ_PUTLOCKS_EXIT(sq); 6297 6298 /* Process the events list */ 6299 sq_run_events(sq); 6300 6301 /* 6302 * Release our claim... 6303 */ 6304 sq->sq_count--; 6305 6306 /* 6307 * And release SQ_EXCL. 6308 * We don't need to acquire the putlocks to release 6309 * SQ_EXCL, since we are exclusive, and hold the SQLOCK. 6310 */ 6311 sq->sq_flags &= ~SQ_EXCL; 6312 6313 /* 6314 * sq_run_events should have released SQ_EXCL 6315 */ 6316 ASSERT(!(sq->sq_flags & SQ_EXCL)); 6317 6318 /* 6319 * If anything happened while we were running the 6320 * events (or was there before), we need to process 6321 * them now. We shouldn't be exclusive sine we 6322 * released the perimiter above (plus, we asserted 6323 * for it). 6324 */ 6325 if (!(sq->sq_flags & SQ_STAYAWAY) && (sq->sq_flags & SQ_QUEUED)) 6326 drain_syncq(sq); 6327 else 6328 mutex_exit(SQLOCK(sq)); 6329 } 6330 6331 /* 6332 * Perform delayed processing. The caller has to make sure that it is safe 6333 * to enter the syncq (e.g. by checking that none of the SQ_STAYAWAY bits are 6334 * set.) 6335 * 6336 * Assume that the caller has NO claims on the syncq. However, a claim 6337 * on the syncq does not indicate that a thread is draining the syncq. 6338 * There may be more claims on the syncq than there are threads draining 6339 * (i.e. #_threads_draining <= sq_count) 6340 * 6341 * drain_syncq has to terminate when one of the SQ_STAYAWAY bits gets set 6342 * in order to preserve qwriter(OUTER) ordering constraints. 6343 * 6344 * sq_putcount only needs to be checked when dispatching the queued 6345 * writer call for CIPUT sync queue, but this is handled in sq_run_events. 6346 */ 6347 void 6348 drain_syncq(syncq_t *sq) 6349 { 6350 queue_t *qp; 6351 uint16_t count; 6352 uint16_t type = sq->sq_type; 6353 uint16_t flags = sq->sq_flags; 6354 boolean_t bg_service = sq->sq_svcflags & SQ_SERVICE; 6355 6356 TRACE_1(TR_FAC_STREAMS_FR, TR_DRAIN_SYNCQ_START, 6357 "drain_syncq start:%p", sq); 6358 ASSERT(MUTEX_HELD(SQLOCK(sq))); 6359 ASSERT((sq->sq_outer == NULL && sq->sq_onext == NULL && 6360 sq->sq_oprev == NULL) || 6361 (sq->sq_outer != NULL && sq->sq_onext != NULL && 6362 sq->sq_oprev != NULL)); 6363 6364 /* 6365 * Drop SQ_SERVICE flag. 6366 */ 6367 if (bg_service) 6368 sq->sq_svcflags &= ~SQ_SERVICE; 6369 6370 /* 6371 * If SQ_EXCL is set, someone else is processing this syncq - let him 6372 * finish the job. 6373 */ 6374 if (flags & SQ_EXCL) { 6375 if (bg_service) { 6376 ASSERT(sq->sq_servcount != 0); 6377 sq->sq_servcount--; 6378 } 6379 mutex_exit(SQLOCK(sq)); 6380 return; 6381 } 6382 6383 /* 6384 * This routine can be called by a background thread if 6385 * it was scheduled by a hi-priority thread. SO, if there are 6386 * NOT messages queued, return (remember, we have the SQLOCK, 6387 * and it cannot change until we release it). Wakeup any waiters also. 6388 */ 6389 if (!(flags & SQ_QUEUED)) { 6390 if (flags & SQ_WANTWAKEUP) { 6391 flags &= ~SQ_WANTWAKEUP; 6392 cv_broadcast(&sq->sq_wait); 6393 } 6394 if (flags & SQ_WANTEXWAKEUP) { 6395 flags &= ~SQ_WANTEXWAKEUP; 6396 cv_broadcast(&sq->sq_exitwait); 6397 } 6398 sq->sq_flags = flags; 6399 if (bg_service) { 6400 ASSERT(sq->sq_servcount != 0); 6401 sq->sq_servcount--; 6402 } 6403 mutex_exit(SQLOCK(sq)); 6404 return; 6405 } 6406 6407 /* 6408 * If this is not a concurrent put perimiter, we need to 6409 * become exclusive to drain. Also, if not CIPUT, we would 6410 * not have acquired a putlock, so we don't need to check 6411 * the putcounts. If not entering with a claim, we test 6412 * for sq_count == 0. 6413 */ 6414 type = sq->sq_type; 6415 if (!(type & SQ_CIPUT)) { 6416 if (sq->sq_count > 1) { 6417 if (bg_service) { 6418 ASSERT(sq->sq_servcount != 0); 6419 sq->sq_servcount--; 6420 } 6421 mutex_exit(SQLOCK(sq)); 6422 return; 6423 } 6424 sq->sq_flags |= SQ_EXCL; 6425 } 6426 6427 /* 6428 * This is where we make a claim to the syncq. 6429 * This can either be done by incrementing a putlock, or 6430 * the sq_count. But since we already have the SQLOCK 6431 * here, we just bump the sq_count. 6432 * 6433 * Note that after we make a claim, we need to let the code 6434 * fall through to the end of this routine to clean itself 6435 * up. A return in the while loop will put the syncq in a 6436 * very bad state. 6437 */ 6438 sq->sq_count++; 6439 ASSERT(sq->sq_count != 0); /* wraparound */ 6440 6441 while ((flags = sq->sq_flags) & SQ_QUEUED) { 6442 /* 6443 * If we are told to stayaway or went exclusive, 6444 * we are done. 6445 */ 6446 if (flags & (SQ_STAYAWAY)) { 6447 break; 6448 } 6449 6450 /* 6451 * If there are events to run, do so. 6452 * We have one claim to the syncq, so if there are 6453 * more than one, other threads are running. 6454 */ 6455 if (sq->sq_evhead != NULL) { 6456 ASSERT(sq->sq_flags & SQ_EVENTS); 6457 6458 count = sq->sq_count; 6459 SQ_PUTLOCKS_ENTER(sq); 6460 SUM_SQ_PUTCOUNTS(sq, count); 6461 if (count > 1) { 6462 SQ_PUTLOCKS_EXIT(sq); 6463 /* Can't upgrade - other threads inside */ 6464 break; 6465 } 6466 ASSERT((flags & SQ_EXCL) == 0); 6467 sq->sq_flags = flags | SQ_EXCL; 6468 SQ_PUTLOCKS_EXIT(sq); 6469 /* 6470 * we have the only claim, run the events, 6471 * sq_run_events will clear the SQ_EXCL flag. 6472 */ 6473 sq_run_events(sq); 6474 6475 /* 6476 * If this is a CIPUT perimiter, we need 6477 * to drop the SQ_EXCL flag so we can properly 6478 * continue draining the syncq. 6479 */ 6480 if (type & SQ_CIPUT) { 6481 ASSERT(sq->sq_flags & SQ_EXCL); 6482 sq->sq_flags &= ~SQ_EXCL; 6483 } 6484 6485 /* 6486 * And go back to the beginning just in case 6487 * anything changed while we were away. 6488 */ 6489 ASSERT((sq->sq_flags & SQ_EXCL) || (type & SQ_CIPUT)); 6490 continue; 6491 } 6492 6493 ASSERT(sq->sq_evhead == NULL); 6494 ASSERT(!(sq->sq_flags & SQ_EVENTS)); 6495 6496 /* 6497 * Find the queue that is not draining. 6498 * 6499 * q_draining is protected by QLOCK which we do not hold. 6500 * But if it was set, then a thread was draining, and if it gets 6501 * cleared, then it was because the thread has successfully 6502 * drained the syncq, or a GOAWAY state occured. For the GOAWAY 6503 * state to happen, a thread needs the SQLOCK which we hold, and 6504 * if there was such a flag, we whould have already seen it. 6505 */ 6506 6507 for (qp = sq->sq_head; 6508 qp != NULL && (qp->q_draining || 6509 (qp->q_sqflags & Q_SQDRAINING)); 6510 qp = qp->q_sqnext) 6511 ; 6512 6513 if (qp == NULL) 6514 break; 6515 6516 /* 6517 * We have a queue to work on, and we hold the 6518 * SQLOCK and one claim, call qdrain_syncq. 6519 * This means we need to release the SQLOCK and 6520 * aquire the QLOCK (OK since we have a claim). 6521 * Note that qdrain_syncq will actually dequeue 6522 * this queue from the sq_head list when it is 6523 * convinced all the work is done and release 6524 * the QLOCK before returning. 6525 */ 6526 qp->q_sqflags |= Q_SQDRAINING; 6527 mutex_exit(SQLOCK(sq)); 6528 mutex_enter(QLOCK(qp)); 6529 qdrain_syncq(sq, qp); 6530 mutex_enter(SQLOCK(sq)); 6531 6532 /* The queue is drained */ 6533 ASSERT(qp->q_sqflags & Q_SQDRAINING); 6534 qp->q_sqflags &= ~Q_SQDRAINING; 6535 /* 6536 * NOTE: After this point qp should not be used since it may be 6537 * closed. 6538 */ 6539 } 6540 6541 ASSERT(MUTEX_HELD(SQLOCK(sq))); 6542 flags = sq->sq_flags; 6543 6544 /* 6545 * sq->sq_head cannot change because we hold the 6546 * sqlock. However, a thread CAN decide that it is no longer 6547 * going to drain that queue. However, this should be due to 6548 * a GOAWAY state, and we should see that here. 6549 * 6550 * This loop is not very efficient. One solution may be adding a second 6551 * pointer to the "draining" queue, but it is difficult to do when 6552 * queues are inserted in the middle due to priority ordering. Another 6553 * possibility is to yank the queue out of the sq list and put it onto 6554 * the "draining list" and then put it back if it can't be drained. 6555 */ 6556 6557 ASSERT((sq->sq_head == NULL) || (flags & SQ_GOAWAY) || 6558 (type & SQ_CI) || sq->sq_head->q_draining); 6559 6560 /* Drop SQ_EXCL for non-CIPUT perimiters */ 6561 if (!(type & SQ_CIPUT)) 6562 flags &= ~SQ_EXCL; 6563 ASSERT((flags & SQ_EXCL) == 0); 6564 6565 /* Wake up any waiters. */ 6566 if (flags & SQ_WANTWAKEUP) { 6567 flags &= ~SQ_WANTWAKEUP; 6568 cv_broadcast(&sq->sq_wait); 6569 } 6570 if (flags & SQ_WANTEXWAKEUP) { 6571 flags &= ~SQ_WANTEXWAKEUP; 6572 cv_broadcast(&sq->sq_exitwait); 6573 } 6574 sq->sq_flags = flags; 6575 6576 ASSERT(sq->sq_count != 0); 6577 /* Release our claim. */ 6578 sq->sq_count--; 6579 6580 if (bg_service) { 6581 ASSERT(sq->sq_servcount != 0); 6582 sq->sq_servcount--; 6583 } 6584 6585 mutex_exit(SQLOCK(sq)); 6586 6587 TRACE_1(TR_FAC_STREAMS_FR, TR_DRAIN_SYNCQ_END, 6588 "drain_syncq end:%p", sq); 6589 } 6590 6591 6592 /* 6593 * 6594 * qdrain_syncq can be called (currently) from only one of two places: 6595 * drain_syncq 6596 * putnext (or some variation of it). 6597 * and eventually 6598 * qwait(_sig) 6599 * 6600 * If called from drain_syncq, we found it in the list 6601 * of queue's needing service, so there is work to be done (or it 6602 * wouldn't be on the list). 6603 * 6604 * If called from some putnext variation, it was because the 6605 * perimiter is open, but messages are blocking a putnext and 6606 * there is not a thread working on it. Now a thread could start 6607 * working on it while we are getting ready to do so ourself, but 6608 * the thread would set the q_draining flag, and we can spin out. 6609 * 6610 * As for qwait(_sig), I think I shall let it continue to call 6611 * drain_syncq directly (after all, it will get here eventually). 6612 * 6613 * qdrain_syncq has to terminate when: 6614 * - one of the SQ_STAYAWAY bits gets set to preserve qwriter(OUTER) ordering 6615 * - SQ_EVENTS gets set to preserve qwriter(INNER) ordering 6616 * 6617 * ASSUMES: 6618 * One claim 6619 * QLOCK held 6620 * SQLOCK not held 6621 * Will release QLOCK before returning 6622 */ 6623 void 6624 qdrain_syncq(syncq_t *sq, queue_t *q) 6625 { 6626 mblk_t *bp; 6627 boolean_t do_clr; 6628 #ifdef DEBUG 6629 uint16_t count; 6630 #endif 6631 6632 TRACE_1(TR_FAC_STREAMS_FR, TR_DRAIN_SYNCQ_START, 6633 "drain_syncq start:%p", sq); 6634 ASSERT(q->q_syncq == sq); 6635 ASSERT(MUTEX_HELD(QLOCK(q))); 6636 ASSERT(MUTEX_NOT_HELD(SQLOCK(sq))); 6637 /* 6638 * For non-CIPUT perimiters, we should be called with the 6639 * exclusive bit set already. For non-CIPUT perimiters we 6640 * will be doing a concurrent drain, so it better not be set. 6641 */ 6642 ASSERT((sq->sq_flags & (SQ_EXCL|SQ_CIPUT))); 6643 ASSERT(!((sq->sq_type & SQ_CIPUT) && (sq->sq_flags & SQ_EXCL))); 6644 ASSERT((sq->sq_type & SQ_CIPUT) || (sq->sq_flags & SQ_EXCL)); 6645 /* 6646 * All outer pointers are set, or none of them are 6647 */ 6648 ASSERT((sq->sq_outer == NULL && sq->sq_onext == NULL && 6649 sq->sq_oprev == NULL) || 6650 (sq->sq_outer != NULL && sq->sq_onext != NULL && 6651 sq->sq_oprev != NULL)); 6652 #ifdef DEBUG 6653 count = sq->sq_count; 6654 /* 6655 * This is OK without the putlocks, because we have one 6656 * claim either from the sq_count, or a putcount. We could 6657 * get an erroneous value from other counts, but ours won't 6658 * change, so one way or another, we will have at least a 6659 * value of one. 6660 */ 6661 SUM_SQ_PUTCOUNTS(sq, count); 6662 ASSERT(count >= 1); 6663 #endif /* DEBUG */ 6664 6665 /* 6666 * The first thing to do here, is find out if a thread is already 6667 * draining this queue or the queue is closing. If so, we are done, 6668 * just return. Also, if there are no messages, we are done as well. 6669 * Note that we check the q_sqhead since there is s window of 6670 * opportunity for us to enter here because Q_SQQUEUED was set, but is 6671 * not anymore. 6672 */ 6673 if (q->q_draining || (q->q_sqhead == NULL)) { 6674 mutex_exit(QLOCK(q)); 6675 return; 6676 } 6677 6678 /* 6679 * If the perimiter is exclusive, there is nothing we can 6680 * do right now, go away. 6681 * Note that there is nothing to prevent this case from changing 6682 * right after this check, but the spin-out will catch it. 6683 */ 6684 6685 /* Tell other threads that we are draining this queue */ 6686 q->q_draining = 1; /* Protected by QLOCK */ 6687 6688 for (bp = q->q_sqhead; bp != NULL; bp = q->q_sqhead) { 6689 6690 /* 6691 * Because we can enter this routine just because 6692 * a putnext is blocked, we need to spin out if 6693 * the perimiter wants to go exclusive as well 6694 * as just blocked. We need to spin out also if 6695 * events are queued on the syncq. 6696 * Don't check for SQ_EXCL, because non-CIPUT 6697 * perimiters would set it, and it can't become 6698 * exclusive while we hold a claim. 6699 */ 6700 if (sq->sq_flags & (SQ_STAYAWAY | SQ_EVENTS)) { 6701 break; 6702 } 6703 6704 #ifdef DEBUG 6705 /* 6706 * Since we are in qdrain_syncq, we already know the queue, 6707 * but for sanity, we want to check this against the qp that 6708 * was passed in by bp->b_queue. 6709 */ 6710 6711 ASSERT(bp->b_queue == q); 6712 ASSERT(bp->b_queue->q_syncq == sq); 6713 bp->b_queue = NULL; 6714 6715 /* 6716 * We would have the following check in the DEBUG code: 6717 * 6718 * if (bp->b_prev != NULL) { 6719 * ASSERT(bp->b_prev == (void (*)())q->q_qinfo->qi_putp); 6720 * } 6721 * 6722 * This can't be done, however, since IP modifies qinfo 6723 * structure at run-time (switching between IPv4 qinfo and IPv6 6724 * qinfo), invalidating the check. 6725 * So the assignment to func is left here, but the ASSERT itself 6726 * is removed until the whole issue is resolved. 6727 */ 6728 #endif 6729 ASSERT(q->q_sqhead == bp); 6730 q->q_sqhead = bp->b_next; 6731 bp->b_prev = bp->b_next = NULL; 6732 ASSERT(q->q_syncqmsgs > 0); 6733 mutex_exit(QLOCK(q)); 6734 6735 ASSERT(bp->b_datap->db_ref != 0); 6736 6737 (void) (*q->q_qinfo->qi_putp)(q, bp); 6738 6739 mutex_enter(QLOCK(q)); 6740 /* 6741 * We should decrement q_syncqmsgs only after executing the 6742 * put procedure to avoid a possible race with putnext(). 6743 * In putnext() though it sees Q_SQQUEUED is set, there is 6744 * an optimization which allows putnext to call the put 6745 * procedure directly if (q_syncqmsgs == 0) and thus 6746 * a message reodering could otherwise occur. 6747 */ 6748 q->q_syncqmsgs--; 6749 6750 /* 6751 * Clear QFULL in the next service procedure queue if 6752 * this is the last message destined to that queue. 6753 * 6754 * It would make better sense to have some sort of 6755 * tunable for the low water mark, but these symantics 6756 * are not yet defined. So, alas, we use a constant. 6757 */ 6758 do_clr = (q->q_syncqmsgs == 0); 6759 mutex_exit(QLOCK(q)); 6760 6761 if (do_clr) 6762 clr_qfull(q); 6763 6764 mutex_enter(QLOCK(q)); 6765 /* 6766 * Always clear SQ_EXCL when CIPUT in order to handle 6767 * qwriter(INNER). 6768 */ 6769 /* 6770 * The putp() can call qwriter and get exclusive access 6771 * IFF this is the only claim. So, we need to test for 6772 * this possibility so we can aquire the mutex and clear 6773 * the bit. 6774 */ 6775 if ((sq->sq_type & SQ_CIPUT) && (sq->sq_flags & SQ_EXCL)) { 6776 mutex_enter(SQLOCK(sq)); 6777 sq->sq_flags &= ~SQ_EXCL; 6778 mutex_exit(SQLOCK(sq)); 6779 } 6780 } 6781 6782 /* 6783 * We should either have no queues on the syncq, or we were 6784 * told to goaway by a waiter (which we will wake up at the 6785 * end of this function). 6786 */ 6787 ASSERT((q->q_sqhead == NULL) || 6788 (sq->sq_flags & (SQ_STAYAWAY | SQ_EVENTS))); 6789 6790 ASSERT(MUTEX_HELD(QLOCK(q))); 6791 ASSERT(MUTEX_NOT_HELD(SQLOCK(sq))); 6792 6793 /* 6794 * Remove the q from the syncq list if all the messages are 6795 * drained. 6796 */ 6797 if (q->q_sqhead == NULL) { 6798 mutex_enter(SQLOCK(sq)); 6799 if (q->q_sqflags & Q_SQQUEUED) 6800 SQRM_Q(sq, q); 6801 mutex_exit(SQLOCK(sq)); 6802 /* 6803 * Since the queue is removed from the list, reset its priority. 6804 */ 6805 q->q_spri = 0; 6806 } 6807 6808 /* 6809 * Remember, the q_draining flag is used to let another 6810 * thread know that there is a thread currently draining 6811 * the messages for a queue. Since we are now done with 6812 * this queue (even if there may be messages still there), 6813 * we need to clear this flag so some thread will work 6814 * on it if needed. 6815 */ 6816 ASSERT(q->q_draining); 6817 q->q_draining = 0; 6818 6819 /* called with a claim, so OK to drop all locks. */ 6820 mutex_exit(QLOCK(q)); 6821 6822 TRACE_1(TR_FAC_STREAMS_FR, TR_DRAIN_SYNCQ_END, 6823 "drain_syncq end:%p", sq); 6824 } 6825 /* END OF QDRAIN_SYNCQ */ 6826 6827 6828 /* 6829 * This is the mate to qdrain_syncq, except that it is putting the 6830 * message onto the the queue instead draining. Since the 6831 * message is destined for the queue that is selected, there is 6832 * no need to identify the function because the message is 6833 * intended for the put routine for the queue. But this 6834 * routine will do it anyway just in case (but only for debug kernels). 6835 * 6836 * After the message is enqueued on the syncq, it calls putnext_tail() 6837 * which will schedule a background thread to actually process the message. 6838 * 6839 * Assumes that there is a claim on the syncq (sq->sq_count > 0) and 6840 * SQLOCK(sq) and QLOCK(q) are not held. 6841 */ 6842 void 6843 qfill_syncq(syncq_t *sq, queue_t *q, mblk_t *mp) 6844 { 6845 ASSERT(MUTEX_NOT_HELD(SQLOCK(sq))); 6846 ASSERT(MUTEX_NOT_HELD(QLOCK(q))); 6847 ASSERT(sq->sq_count > 0); 6848 ASSERT(q->q_syncq == sq); 6849 ASSERT((sq->sq_outer == NULL && sq->sq_onext == NULL && 6850 sq->sq_oprev == NULL) || 6851 (sq->sq_outer != NULL && sq->sq_onext != NULL && 6852 sq->sq_oprev != NULL)); 6853 6854 mutex_enter(QLOCK(q)); 6855 6856 #ifdef DEBUG 6857 /* 6858 * This is used for debug in the qfill_syncq/qdrain_syncq case 6859 * to trace the queue that the message is intended for. Note 6860 * that the original use was to identify the queue and function 6861 * to call on the drain. In the new syncq, we have the context 6862 * of the queue that we are draining, so call it's putproc and 6863 * don't rely on the saved values. But for debug this is still 6864 * usefull information. 6865 */ 6866 mp->b_prev = (mblk_t *)q->q_qinfo->qi_putp; 6867 mp->b_queue = q; 6868 mp->b_next = NULL; 6869 #endif 6870 ASSERT(q->q_syncq == sq); 6871 /* 6872 * Enqueue the message on the list. 6873 * SQPUT_MP() accesses q_syncqmsgs. We are already holding QLOCK to 6874 * protect it. So its ok to acquire SQLOCK after SQPUT_MP(). 6875 */ 6876 SQPUT_MP(q, mp); 6877 mutex_enter(SQLOCK(sq)); 6878 6879 /* 6880 * And queue on syncq for scheduling, if not already queued. 6881 * Note that we need the SQLOCK for this, and for testing flags 6882 * at the end to see if we will drain. So grab it now, and 6883 * release it before we call qdrain_syncq or return. 6884 */ 6885 if (!(q->q_sqflags & Q_SQQUEUED)) { 6886 q->q_spri = curthread->t_pri; 6887 SQPUT_Q(sq, q); 6888 } 6889 #ifdef DEBUG 6890 else { 6891 /* 6892 * All of these conditions MUST be true! 6893 */ 6894 ASSERT(sq->sq_tail != NULL); 6895 if (sq->sq_tail == sq->sq_head) { 6896 ASSERT((q->q_sqprev == NULL) && 6897 (q->q_sqnext == NULL)); 6898 } else { 6899 ASSERT((q->q_sqprev != NULL) || 6900 (q->q_sqnext != NULL)); 6901 } 6902 ASSERT(sq->sq_flags & SQ_QUEUED); 6903 ASSERT(q->q_syncqmsgs != 0); 6904 ASSERT(q->q_sqflags & Q_SQQUEUED); 6905 } 6906 #endif 6907 mutex_exit(QLOCK(q)); 6908 /* 6909 * SQLOCK is still held, so sq_count can be safely decremented. 6910 */ 6911 sq->sq_count--; 6912 6913 putnext_tail(sq, q, 0); 6914 /* Should not reference sq or q after this point. */ 6915 } 6916 6917 /* End of qfill_syncq */ 6918 6919 /* 6920 * Remove all messages from a syncq (if qp is NULL) or remove all messages 6921 * that would be put into qp by drain_syncq. 6922 * Used when deleting the syncq (qp == NULL) or when detaching 6923 * a queue (qp != NULL). 6924 * Return non-zero if one or more messages were freed. 6925 * 6926 * no need to grab sq_putlocks here. See comment in strsubr.h that explains when 6927 * sq_putlocks are used. 6928 * 6929 * NOTE: This function assumes that it is called from the close() context and 6930 * that all the queues in the syncq are going aay. For this reason it doesn't 6931 * acquire QLOCK for modifying q_sqhead/q_sqtail fields. This assumption is 6932 * currently valid, but it is useful to rethink this function to behave properly 6933 * in other cases. 6934 */ 6935 int 6936 flush_syncq(syncq_t *sq, queue_t *qp) 6937 { 6938 mblk_t *bp, *mp_head, *mp_next, *mp_prev; 6939 queue_t *q; 6940 int ret = 0; 6941 6942 mutex_enter(SQLOCK(sq)); 6943 6944 /* 6945 * Before we leave, we need to make sure there are no 6946 * events listed for this queue. All events for this queue 6947 * will just be freed. 6948 */ 6949 if (qp != NULL && sq->sq_evhead != NULL) { 6950 ASSERT(sq->sq_flags & SQ_EVENTS); 6951 6952 mp_prev = NULL; 6953 for (bp = sq->sq_evhead; bp != NULL; bp = mp_next) { 6954 mp_next = bp->b_next; 6955 if (bp->b_queue == qp) { 6956 /* Delete this message */ 6957 if (mp_prev != NULL) { 6958 mp_prev->b_next = mp_next; 6959 /* 6960 * Update sq_evtail if the last element 6961 * is removed. 6962 */ 6963 if (bp == sq->sq_evtail) { 6964 ASSERT(mp_next == NULL); 6965 sq->sq_evtail = mp_prev; 6966 } 6967 } else 6968 sq->sq_evhead = mp_next; 6969 if (sq->sq_evhead == NULL) 6970 sq->sq_flags &= ~SQ_EVENTS; 6971 bp->b_prev = bp->b_next = NULL; 6972 freemsg(bp); 6973 ret++; 6974 } else { 6975 mp_prev = bp; 6976 } 6977 } 6978 } 6979 6980 /* 6981 * Walk sq_head and: 6982 * - match qp if qp is set, remove it's messages 6983 * - all if qp is not set 6984 */ 6985 q = sq->sq_head; 6986 while (q != NULL) { 6987 ASSERT(q->q_syncq == sq); 6988 if ((qp == NULL) || (qp == q)) { 6989 /* 6990 * Yank the messages as a list off the queue 6991 */ 6992 mp_head = q->q_sqhead; 6993 /* 6994 * We do not have QLOCK(q) here (which is safe due to 6995 * assumptions mentioned above). To obtain the lock we 6996 * need to release SQLOCK which may allow lots of things 6997 * to change upon us. This place requires more analysis. 6998 */ 6999 q->q_sqhead = q->q_sqtail = NULL; 7000 ASSERT(mp_head->b_queue && 7001 mp_head->b_queue->q_syncq == sq); 7002 7003 /* 7004 * Free each of the messages. 7005 */ 7006 for (bp = mp_head; bp != NULL; bp = mp_next) { 7007 mp_next = bp->b_next; 7008 bp->b_prev = bp->b_next = NULL; 7009 freemsg(bp); 7010 ret++; 7011 } 7012 /* 7013 * Now remove the queue from the syncq. 7014 */ 7015 ASSERT(q->q_sqflags & Q_SQQUEUED); 7016 SQRM_Q(sq, q); 7017 q->q_spri = 0; 7018 q->q_syncqmsgs = 0; 7019 7020 /* 7021 * If qp was specified, we are done with it and are 7022 * going to drop SQLOCK(sq) and return. We wakeup syncq 7023 * waiters while we still have the SQLOCK. 7024 */ 7025 if ((qp != NULL) && (sq->sq_flags & SQ_WANTWAKEUP)) { 7026 sq->sq_flags &= ~SQ_WANTWAKEUP; 7027 cv_broadcast(&sq->sq_wait); 7028 } 7029 /* Drop SQLOCK across clr_qfull */ 7030 mutex_exit(SQLOCK(sq)); 7031 7032 /* 7033 * We avoid doing the test that drain_syncq does and 7034 * unconditionally clear qfull for every flushed 7035 * message. Since flush_syncq is only called during 7036 * close this should not be a problem. 7037 */ 7038 clr_qfull(q); 7039 if (qp != NULL) { 7040 return (ret); 7041 } else { 7042 mutex_enter(SQLOCK(sq)); 7043 /* 7044 * The head was removed by SQRM_Q above. 7045 * reread the new head and flush it. 7046 */ 7047 q = sq->sq_head; 7048 } 7049 } else { 7050 q = q->q_sqnext; 7051 } 7052 ASSERT(MUTEX_HELD(SQLOCK(sq))); 7053 } 7054 7055 if (sq->sq_flags & SQ_WANTWAKEUP) { 7056 sq->sq_flags &= ~SQ_WANTWAKEUP; 7057 cv_broadcast(&sq->sq_wait); 7058 } 7059 7060 mutex_exit(SQLOCK(sq)); 7061 return (ret); 7062 } 7063 7064 /* 7065 * Propagate all messages from a syncq to the next syncq that are associated 7066 * with the specified queue. If the queue is attached to a driver or if the 7067 * messages have been added due to a qwriter(PERIM_INNER), free the messages. 7068 * 7069 * Assumes that the stream is strlock()'ed. We don't come here if there 7070 * are no messages to propagate. 7071 * 7072 * NOTE : If the queue is attached to a driver, all the messages are freed 7073 * as there is no point in propagating the messages from the driver syncq 7074 * to the closing stream head which will in turn get freed later. 7075 */ 7076 static int 7077 propagate_syncq(queue_t *qp) 7078 { 7079 mblk_t *bp, *head, *tail, *prev, *next; 7080 syncq_t *sq; 7081 queue_t *nqp; 7082 syncq_t *nsq; 7083 boolean_t isdriver; 7084 int moved = 0; 7085 uint16_t flags; 7086 pri_t priority = curthread->t_pri; 7087 #ifdef DEBUG 7088 void (*func)(); 7089 #endif 7090 7091 sq = qp->q_syncq; 7092 ASSERT(MUTEX_HELD(SQLOCK(sq))); 7093 /* debug macro */ 7094 SQ_PUTLOCKS_HELD(sq); 7095 /* 7096 * As entersq() does not increment the sq_count for 7097 * the write side, check sq_count for non-QPERQ 7098 * perimeters alone. 7099 */ 7100 ASSERT((qp->q_flag & QPERQ) || (sq->sq_count >= 1)); 7101 7102 /* 7103 * propagate_syncq() can be called because of either messages on the 7104 * queue syncq or because on events on the queue syncq. Do actual 7105 * message propagations if there are any messages. 7106 */ 7107 if (qp->q_syncqmsgs) { 7108 isdriver = (qp->q_flag & QISDRV); 7109 7110 if (!isdriver) { 7111 nqp = qp->q_next; 7112 nsq = nqp->q_syncq; 7113 ASSERT(MUTEX_HELD(SQLOCK(nsq))); 7114 /* debug macro */ 7115 SQ_PUTLOCKS_HELD(nsq); 7116 #ifdef DEBUG 7117 func = (void (*)())nqp->q_qinfo->qi_putp; 7118 #endif 7119 } 7120 7121 SQRM_Q(sq, qp); 7122 priority = MAX(qp->q_spri, priority); 7123 qp->q_spri = 0; 7124 head = qp->q_sqhead; 7125 tail = qp->q_sqtail; 7126 qp->q_sqhead = qp->q_sqtail = NULL; 7127 qp->q_syncqmsgs = 0; 7128 7129 /* 7130 * Walk the list of messages, and free them if this is a driver, 7131 * otherwise reset the b_prev and b_queue value to the new putp. 7132 * Afterward, we will just add the head to the end of the next 7133 * syncq, and point the tail to the end of this one. 7134 */ 7135 7136 for (bp = head; bp != NULL; bp = next) { 7137 next = bp->b_next; 7138 if (isdriver) { 7139 bp->b_prev = bp->b_next = NULL; 7140 freemsg(bp); 7141 continue; 7142 } 7143 /* Change the q values for this message */ 7144 bp->b_queue = nqp; 7145 #ifdef DEBUG 7146 bp->b_prev = (mblk_t *)func; 7147 #endif 7148 moved++; 7149 } 7150 /* 7151 * Attach list of messages to the end of the new queue (if there 7152 * is a list of messages). 7153 */ 7154 7155 if (!isdriver && head != NULL) { 7156 ASSERT(tail != NULL); 7157 if (nqp->q_sqhead == NULL) { 7158 nqp->q_sqhead = head; 7159 } else { 7160 ASSERT(nqp->q_sqtail != NULL); 7161 nqp->q_sqtail->b_next = head; 7162 } 7163 nqp->q_sqtail = tail; 7164 /* 7165 * When messages are moved from high priority queue to 7166 * another queue, the destination queue priority is 7167 * upgraded. 7168 */ 7169 7170 if (priority > nqp->q_spri) 7171 nqp->q_spri = priority; 7172 7173 SQPUT_Q(nsq, nqp); 7174 7175 nqp->q_syncqmsgs += moved; 7176 ASSERT(nqp->q_syncqmsgs != 0); 7177 } 7178 } 7179 7180 /* 7181 * Before we leave, we need to make sure there are no 7182 * events listed for this queue. All events for this queue 7183 * will just be freed. 7184 */ 7185 if (sq->sq_evhead != NULL) { 7186 ASSERT(sq->sq_flags & SQ_EVENTS); 7187 prev = NULL; 7188 for (bp = sq->sq_evhead; bp != NULL; bp = next) { 7189 next = bp->b_next; 7190 if (bp->b_queue == qp) { 7191 /* Delete this message */ 7192 if (prev != NULL) { 7193 prev->b_next = next; 7194 /* 7195 * Update sq_evtail if the last element 7196 * is removed. 7197 */ 7198 if (bp == sq->sq_evtail) { 7199 ASSERT(next == NULL); 7200 sq->sq_evtail = prev; 7201 } 7202 } else 7203 sq->sq_evhead = next; 7204 if (sq->sq_evhead == NULL) 7205 sq->sq_flags &= ~SQ_EVENTS; 7206 bp->b_prev = bp->b_next = NULL; 7207 freemsg(bp); 7208 } else { 7209 prev = bp; 7210 } 7211 } 7212 } 7213 7214 flags = sq->sq_flags; 7215 7216 /* Wake up any waiter before leaving. */ 7217 if (flags & SQ_WANTWAKEUP) { 7218 flags &= ~SQ_WANTWAKEUP; 7219 cv_broadcast(&sq->sq_wait); 7220 } 7221 sq->sq_flags = flags; 7222 7223 return (moved); 7224 } 7225 7226 /* 7227 * Try and upgrade to exclusive access at the inner perimeter. If this can 7228 * not be done without blocking then request will be queued on the syncq 7229 * and drain_syncq will run it later. 7230 * 7231 * This routine can only be called from put or service procedures plus 7232 * asynchronous callback routines that have properly entered to 7233 * queue (with entersq.) Thus qwriter_inner assumes the caller has one claim 7234 * on the syncq associated with q. 7235 */ 7236 void 7237 qwriter_inner(queue_t *q, mblk_t *mp, void (*func)()) 7238 { 7239 syncq_t *sq = q->q_syncq; 7240 uint16_t count; 7241 7242 mutex_enter(SQLOCK(sq)); 7243 count = sq->sq_count; 7244 SQ_PUTLOCKS_ENTER(sq); 7245 SUM_SQ_PUTCOUNTS(sq, count); 7246 ASSERT(count >= 1); 7247 ASSERT(sq->sq_type & (SQ_CIPUT|SQ_CISVC)); 7248 7249 if (count == 1) { 7250 /* 7251 * Can upgrade. This case also handles nested qwriter calls 7252 * (when the qwriter callback function calls qwriter). In that 7253 * case SQ_EXCL is already set. 7254 */ 7255 sq->sq_flags |= SQ_EXCL; 7256 SQ_PUTLOCKS_EXIT(sq); 7257 mutex_exit(SQLOCK(sq)); 7258 (*func)(q, mp); 7259 /* 7260 * Assumes that leavesq, putnext, and drain_syncq will reset 7261 * SQ_EXCL for SQ_CIPUT/SQ_CISVC queues. We leave SQ_EXCL on 7262 * until putnext, leavesq, or drain_syncq drops it. 7263 * That way we handle nested qwriter(INNER) without dropping 7264 * SQ_EXCL until the outermost qwriter callback routine is 7265 * done. 7266 */ 7267 return; 7268 } 7269 SQ_PUTLOCKS_EXIT(sq); 7270 sqfill_events(sq, q, mp, func); 7271 } 7272 7273 /* 7274 * Synchronous callback support functions 7275 */ 7276 7277 /* 7278 * Allocate a callback parameter structure. 7279 * Assumes that caller initializes the flags and the id. 7280 * Acquires SQLOCK(sq) if non-NULL is returned. 7281 */ 7282 callbparams_t * 7283 callbparams_alloc(syncq_t *sq, void (*func)(void *), void *arg, int kmflags) 7284 { 7285 callbparams_t *cbp; 7286 size_t size = sizeof (callbparams_t); 7287 7288 cbp = kmem_alloc(size, kmflags & ~KM_PANIC); 7289 7290 /* 7291 * Only try tryhard allocation if the caller is ready to panic. 7292 * Otherwise just fail. 7293 */ 7294 if (cbp == NULL) { 7295 if (kmflags & KM_PANIC) 7296 cbp = kmem_alloc_tryhard(sizeof (callbparams_t), 7297 &size, kmflags); 7298 else 7299 return (NULL); 7300 } 7301 7302 ASSERT(size >= sizeof (callbparams_t)); 7303 cbp->cbp_size = size; 7304 cbp->cbp_sq = sq; 7305 cbp->cbp_func = func; 7306 cbp->cbp_arg = arg; 7307 mutex_enter(SQLOCK(sq)); 7308 cbp->cbp_next = sq->sq_callbpend; 7309 sq->sq_callbpend = cbp; 7310 return (cbp); 7311 } 7312 7313 void 7314 callbparams_free(syncq_t *sq, callbparams_t *cbp) 7315 { 7316 callbparams_t **pp, *p; 7317 7318 ASSERT(MUTEX_HELD(SQLOCK(sq))); 7319 7320 for (pp = &sq->sq_callbpend; (p = *pp) != NULL; pp = &p->cbp_next) { 7321 if (p == cbp) { 7322 *pp = p->cbp_next; 7323 kmem_free(p, p->cbp_size); 7324 return; 7325 } 7326 } 7327 (void) (STRLOG(0, 0, 0, SL_CONSOLE, 7328 "callbparams_free: not found\n")); 7329 } 7330 7331 void 7332 callbparams_free_id(syncq_t *sq, callbparams_id_t id, int32_t flag) 7333 { 7334 callbparams_t **pp, *p; 7335 7336 ASSERT(MUTEX_HELD(SQLOCK(sq))); 7337 7338 for (pp = &sq->sq_callbpend; (p = *pp) != NULL; pp = &p->cbp_next) { 7339 if (p->cbp_id == id && p->cbp_flags == flag) { 7340 *pp = p->cbp_next; 7341 kmem_free(p, p->cbp_size); 7342 return; 7343 } 7344 } 7345 (void) (STRLOG(0, 0, 0, SL_CONSOLE, 7346 "callbparams_free_id: not found\n")); 7347 } 7348 7349 /* 7350 * Callback wrapper function used by once-only callbacks that can be 7351 * cancelled (qtimeout and qbufcall) 7352 * Contains inline version of entersq(sq, SQ_CALLBACK) that can be 7353 * cancelled by the qun* functions. 7354 */ 7355 void 7356 qcallbwrapper(void *arg) 7357 { 7358 callbparams_t *cbp = arg; 7359 syncq_t *sq; 7360 uint16_t count = 0; 7361 uint16_t waitflags = SQ_STAYAWAY | SQ_EVENTS | SQ_EXCL; 7362 uint16_t type; 7363 7364 sq = cbp->cbp_sq; 7365 mutex_enter(SQLOCK(sq)); 7366 type = sq->sq_type; 7367 if (!(type & SQ_CICB)) { 7368 count = sq->sq_count; 7369 SQ_PUTLOCKS_ENTER(sq); 7370 SQ_PUTCOUNT_CLRFAST_LOCKED(sq); 7371 SUM_SQ_PUTCOUNTS(sq, count); 7372 sq->sq_needexcl++; 7373 ASSERT(sq->sq_needexcl != 0); /* wraparound */ 7374 waitflags |= SQ_MESSAGES; 7375 } 7376 /* Can not handle exlusive entry at outer perimeter */ 7377 ASSERT(type & SQ_COCB); 7378 7379 while ((sq->sq_flags & waitflags) || (!(type & SQ_CICB) &&count != 0)) { 7380 if ((sq->sq_callbflags & cbp->cbp_flags) && 7381 (sq->sq_cancelid == cbp->cbp_id)) { 7382 /* timeout has been cancelled */ 7383 sq->sq_callbflags |= SQ_CALLB_BYPASSED; 7384 callbparams_free(sq, cbp); 7385 if (!(type & SQ_CICB)) { 7386 ASSERT(sq->sq_needexcl > 0); 7387 sq->sq_needexcl--; 7388 if (sq->sq_needexcl == 0) { 7389 SQ_PUTCOUNT_SETFAST_LOCKED(sq); 7390 } 7391 SQ_PUTLOCKS_EXIT(sq); 7392 } 7393 mutex_exit(SQLOCK(sq)); 7394 return; 7395 } 7396 sq->sq_flags |= SQ_WANTWAKEUP; 7397 if (!(type & SQ_CICB)) { 7398 SQ_PUTLOCKS_EXIT(sq); 7399 } 7400 cv_wait(&sq->sq_wait, SQLOCK(sq)); 7401 if (!(type & SQ_CICB)) { 7402 count = sq->sq_count; 7403 SQ_PUTLOCKS_ENTER(sq); 7404 SUM_SQ_PUTCOUNTS(sq, count); 7405 } 7406 } 7407 7408 sq->sq_count++; 7409 ASSERT(sq->sq_count != 0); /* Wraparound */ 7410 if (!(type & SQ_CICB)) { 7411 ASSERT(count == 0); 7412 sq->sq_flags |= SQ_EXCL; 7413 ASSERT(sq->sq_needexcl > 0); 7414 sq->sq_needexcl--; 7415 if (sq->sq_needexcl == 0) { 7416 SQ_PUTCOUNT_SETFAST_LOCKED(sq); 7417 } 7418 SQ_PUTLOCKS_EXIT(sq); 7419 } 7420 7421 mutex_exit(SQLOCK(sq)); 7422 7423 cbp->cbp_func(cbp->cbp_arg); 7424 7425 /* 7426 * We drop the lock only for leavesq to re-acquire it. 7427 * Possible optimization is inline of leavesq. 7428 */ 7429 mutex_enter(SQLOCK(sq)); 7430 callbparams_free(sq, cbp); 7431 mutex_exit(SQLOCK(sq)); 7432 leavesq(sq, SQ_CALLBACK); 7433 } 7434 7435 /* 7436 * no need to grab sq_putlocks here. See comment in strsubr.h that 7437 * explains when sq_putlocks are used. 7438 * 7439 * sq_count (or one of the sq_putcounts) has already been 7440 * decremented by the caller, and if SQ_QUEUED, we need to call 7441 * drain_syncq (the global syncq drain). 7442 * If putnext_tail is called with the SQ_EXCL bit set, we are in 7443 * one of two states, non-CIPUT perimiter, and we need to clear 7444 * it, or we went exclusive in the put procedure. In any case, 7445 * we want to clear the bit now, and it is probably easier to do 7446 * this at the beginning of this function (remember, we hold 7447 * the SQLOCK). Lastly, if there are other messages queued 7448 * on the syncq (and not for our destination), enable the syncq 7449 * for background work. 7450 */ 7451 7452 /* ARGSUSED */ 7453 void 7454 putnext_tail(syncq_t *sq, queue_t *qp, uint32_t passflags) 7455 { 7456 uint16_t flags = sq->sq_flags; 7457 7458 ASSERT(MUTEX_HELD(SQLOCK(sq))); 7459 ASSERT(MUTEX_NOT_HELD(QLOCK(qp))); 7460 7461 /* Clear SQ_EXCL if set in passflags */ 7462 if (passflags & SQ_EXCL) { 7463 flags &= ~SQ_EXCL; 7464 } 7465 if (flags & SQ_WANTWAKEUP) { 7466 flags &= ~SQ_WANTWAKEUP; 7467 cv_broadcast(&sq->sq_wait); 7468 } 7469 if (flags & SQ_WANTEXWAKEUP) { 7470 flags &= ~SQ_WANTEXWAKEUP; 7471 cv_broadcast(&sq->sq_exitwait); 7472 } 7473 sq->sq_flags = flags; 7474 7475 /* 7476 * We have cleared SQ_EXCL if we were asked to, and started 7477 * the wakeup process for waiters. If there are no writers 7478 * then we need to drain the syncq if we were told to, or 7479 * enable the background thread to do it. 7480 */ 7481 if (!(flags & (SQ_STAYAWAY|SQ_EXCL))) { 7482 if ((passflags & SQ_QUEUED) || 7483 (sq->sq_svcflags & SQ_DISABLED)) { 7484 /* drain_syncq will take care of events in the list */ 7485 drain_syncq(sq); 7486 return; 7487 } else if (flags & SQ_QUEUED) { 7488 sqenable(sq); 7489 } 7490 } 7491 /* Drop the SQLOCK on exit */ 7492 mutex_exit(SQLOCK(sq)); 7493 TRACE_3(TR_FAC_STREAMS_FR, TR_PUTNEXT_END, 7494 "putnext_end:(%p, %p, %p) done", NULL, qp, sq); 7495 } 7496 7497 void 7498 set_qend(queue_t *q) 7499 { 7500 mutex_enter(QLOCK(q)); 7501 if (!O_SAMESTR(q)) 7502 q->q_flag |= QEND; 7503 else 7504 q->q_flag &= ~QEND; 7505 mutex_exit(QLOCK(q)); 7506 q = _OTHERQ(q); 7507 mutex_enter(QLOCK(q)); 7508 if (!O_SAMESTR(q)) 7509 q->q_flag |= QEND; 7510 else 7511 q->q_flag &= ~QEND; 7512 mutex_exit(QLOCK(q)); 7513 } 7514 7515 /* 7516 * Set QFULL in next service procedure queue (that cares) if not already 7517 * set and if there are already more messages on the syncq than 7518 * sq_max_size. If sq_max_size is 0, no flow control will be asserted on 7519 * any syncq. 7520 * 7521 * The fq here is the next queue with a service procedure. This is where 7522 * we would fail canputnext, so this is where we need to set QFULL. 7523 * In the case when fq != q we need to take QLOCK(fq) to set QFULL flag. 7524 * 7525 * We already have QLOCK at this point. To avoid cross-locks with 7526 * freezestr() which grabs all QLOCKs and with strlock() which grabs both 7527 * SQLOCK and sd_reflock, we need to drop respective locks first. 7528 */ 7529 void 7530 set_qfull(queue_t *q) 7531 { 7532 queue_t *fq = NULL; 7533 7534 ASSERT(MUTEX_HELD(QLOCK(q))); 7535 if ((sq_max_size != 0) && (!(q->q_nfsrv->q_flag & QFULL)) && 7536 (q->q_syncqmsgs > sq_max_size)) { 7537 if ((fq = q->q_nfsrv) == q) { 7538 fq->q_flag |= QFULL; 7539 } else { 7540 mutex_exit(QLOCK(q)); 7541 mutex_enter(QLOCK(fq)); 7542 fq->q_flag |= QFULL; 7543 mutex_exit(QLOCK(fq)); 7544 mutex_enter(QLOCK(q)); 7545 } 7546 } 7547 } 7548 7549 void 7550 clr_qfull(queue_t *q) 7551 { 7552 queue_t *oq = q; 7553 7554 q = q->q_nfsrv; 7555 /* Fast check if there is any work to do before getting the lock. */ 7556 if ((q->q_flag & (QFULL|QWANTW)) == 0) { 7557 return; 7558 } 7559 7560 /* 7561 * Do not reset QFULL (and backenable) if the q_count is the reason 7562 * for QFULL being set. 7563 */ 7564 mutex_enter(QLOCK(q)); 7565 /* 7566 * If queue is empty i.e q_mblkcnt is zero, queue can not be full. 7567 * Hence clear the QFULL. 7568 * If both q_count and q_mblkcnt are less than the hiwat mark, 7569 * clear the QFULL. 7570 */ 7571 if (q->q_mblkcnt == 0 || ((q->q_count < q->q_hiwat) && 7572 (q->q_mblkcnt < q->q_hiwat))) { 7573 q->q_flag &= ~QFULL; 7574 /* 7575 * A little more confusing, how about this way: 7576 * if someone wants to write, 7577 * AND 7578 * both counts are less than the lowat mark 7579 * OR 7580 * the lowat mark is zero 7581 * THEN 7582 * backenable 7583 */ 7584 if ((q->q_flag & QWANTW) && 7585 (((q->q_count < q->q_lowat) && 7586 (q->q_mblkcnt < q->q_lowat)) || q->q_lowat == 0)) { 7587 q->q_flag &= ~QWANTW; 7588 mutex_exit(QLOCK(q)); 7589 backenable(oq, 0); 7590 } else 7591 mutex_exit(QLOCK(q)); 7592 } else 7593 mutex_exit(QLOCK(q)); 7594 } 7595 7596 /* 7597 * Set the forward service procedure pointer. 7598 * 7599 * Called at insert-time to cache a queue's next forward service procedure in 7600 * q_nfsrv; used by canput() and canputnext(). If the queue to be inserted 7601 * has a service procedure then q_nfsrv points to itself. If the queue to be 7602 * inserted does not have a service procedure, then q_nfsrv points to the next 7603 * queue forward that has a service procedure. If the queue is at the logical 7604 * end of the stream (driver for write side, stream head for the read side) 7605 * and does not have a service procedure, then q_nfsrv also points to itself. 7606 */ 7607 void 7608 set_nfsrv_ptr( 7609 queue_t *rnew, /* read queue pointer to new module */ 7610 queue_t *wnew, /* write queue pointer to new module */ 7611 queue_t *prev_rq, /* read queue pointer to the module above */ 7612 queue_t *prev_wq) /* write queue pointer to the module above */ 7613 { 7614 queue_t *qp; 7615 7616 if (prev_wq->q_next == NULL) { 7617 /* 7618 * Insert the driver, initialize the driver and stream head. 7619 * In this case, prev_rq/prev_wq should be the stream head. 7620 * _I_INSERT does not allow inserting a driver. Make sure 7621 * that it is not an insertion. 7622 */ 7623 ASSERT(!(rnew->q_flag & _QINSERTING)); 7624 wnew->q_nfsrv = wnew; 7625 if (rnew->q_qinfo->qi_srvp) 7626 rnew->q_nfsrv = rnew; 7627 else 7628 rnew->q_nfsrv = prev_rq; 7629 prev_rq->q_nfsrv = prev_rq; 7630 prev_wq->q_nfsrv = prev_wq; 7631 } else { 7632 /* 7633 * set up read side q_nfsrv pointer. This MUST be done 7634 * before setting the write side, because the setting of 7635 * the write side for a fifo may depend on it. 7636 * 7637 * Suppose we have a fifo that only has pipemod pushed. 7638 * pipemod has no read or write service procedures, so 7639 * nfsrv for both pipemod queues points to prev_rq (the 7640 * stream read head). Now push bufmod (which has only a 7641 * read service procedure). Doing the write side first, 7642 * wnew->q_nfsrv is set to pipemod's writeq nfsrv, which 7643 * is WRONG; the next queue forward from wnew with a 7644 * service procedure will be rnew, not the stream read head. 7645 * Since the downstream queue (which in the case of a fifo 7646 * is the read queue rnew) can affect upstream queues, it 7647 * needs to be done first. Setting up the read side first 7648 * sets nfsrv for both pipemod queues to rnew and then 7649 * when the write side is set up, wnew-q_nfsrv will also 7650 * point to rnew. 7651 */ 7652 if (rnew->q_qinfo->qi_srvp) { 7653 /* 7654 * use _OTHERQ() because, if this is a pipe, next 7655 * module may have been pushed from other end and 7656 * q_next could be a read queue. 7657 */ 7658 qp = _OTHERQ(prev_wq->q_next); 7659 while (qp && qp->q_nfsrv != qp) { 7660 qp->q_nfsrv = rnew; 7661 qp = backq(qp); 7662 } 7663 rnew->q_nfsrv = rnew; 7664 } else 7665 rnew->q_nfsrv = prev_rq->q_nfsrv; 7666 7667 /* set up write side q_nfsrv pointer */ 7668 if (wnew->q_qinfo->qi_srvp) { 7669 wnew->q_nfsrv = wnew; 7670 7671 /* 7672 * For insertion, need to update nfsrv of the modules 7673 * above which do not have a service routine. 7674 */ 7675 if (rnew->q_flag & _QINSERTING) { 7676 for (qp = prev_wq; 7677 qp != NULL && qp->q_nfsrv != qp; 7678 qp = backq(qp)) { 7679 qp->q_nfsrv = wnew->q_nfsrv; 7680 } 7681 } 7682 } else { 7683 if (prev_wq->q_next == prev_rq) 7684 /* 7685 * Since prev_wq/prev_rq are the middle of a 7686 * fifo, wnew/rnew will also be the middle of 7687 * a fifo and wnew's nfsrv is same as rnew's. 7688 */ 7689 wnew->q_nfsrv = rnew->q_nfsrv; 7690 else 7691 wnew->q_nfsrv = prev_wq->q_next->q_nfsrv; 7692 } 7693 } 7694 } 7695 7696 /* 7697 * Reset the forward service procedure pointer; called at remove-time. 7698 */ 7699 void 7700 reset_nfsrv_ptr(queue_t *rqp, queue_t *wqp) 7701 { 7702 queue_t *tmp_qp; 7703 7704 /* Reset the write side q_nfsrv pointer for _I_REMOVE */ 7705 if ((rqp->q_flag & _QREMOVING) && (wqp->q_qinfo->qi_srvp != NULL)) { 7706 for (tmp_qp = backq(wqp); 7707 tmp_qp != NULL && tmp_qp->q_nfsrv == wqp; 7708 tmp_qp = backq(tmp_qp)) { 7709 tmp_qp->q_nfsrv = wqp->q_nfsrv; 7710 } 7711 } 7712 7713 /* reset the read side q_nfsrv pointer */ 7714 if (rqp->q_qinfo->qi_srvp) { 7715 if (wqp->q_next) { /* non-driver case */ 7716 tmp_qp = _OTHERQ(wqp->q_next); 7717 while (tmp_qp && tmp_qp->q_nfsrv == rqp) { 7718 /* Note that rqp->q_next cannot be NULL */ 7719 ASSERT(rqp->q_next != NULL); 7720 tmp_qp->q_nfsrv = rqp->q_next->q_nfsrv; 7721 tmp_qp = backq(tmp_qp); 7722 } 7723 } 7724 } 7725 } 7726 7727 /* 7728 * This routine should be called after all stream geometry changes to update 7729 * the stream head cached struio() rd/wr queue pointers. Note must be called 7730 * with the streamlock()ed. 7731 * 7732 * Note: only enables Synchronous STREAMS for a side of a Stream which has 7733 * an explicit synchronous barrier module queue. That is, a queue that 7734 * has specified a struio() type. 7735 */ 7736 static void 7737 strsetuio(stdata_t *stp) 7738 { 7739 queue_t *wrq; 7740 7741 if (stp->sd_flag & STPLEX) { 7742 /* 7743 * Not stremahead, but a mux, so no Synchronous STREAMS. 7744 */ 7745 stp->sd_struiowrq = NULL; 7746 stp->sd_struiordq = NULL; 7747 return; 7748 } 7749 /* 7750 * Scan the write queue(s) while synchronous 7751 * until we find a qinfo uio type specified. 7752 */ 7753 wrq = stp->sd_wrq->q_next; 7754 while (wrq) { 7755 if (wrq->q_struiot == STRUIOT_NONE) { 7756 wrq = 0; 7757 break; 7758 } 7759 if (wrq->q_struiot != STRUIOT_DONTCARE) 7760 break; 7761 if (! _SAMESTR(wrq)) { 7762 wrq = 0; 7763 break; 7764 } 7765 wrq = wrq->q_next; 7766 } 7767 stp->sd_struiowrq = wrq; 7768 /* 7769 * Scan the read queue(s) while synchronous 7770 * until we find a qinfo uio type specified. 7771 */ 7772 wrq = stp->sd_wrq->q_next; 7773 while (wrq) { 7774 if (_RD(wrq)->q_struiot == STRUIOT_NONE) { 7775 wrq = 0; 7776 break; 7777 } 7778 if (_RD(wrq)->q_struiot != STRUIOT_DONTCARE) 7779 break; 7780 if (! _SAMESTR(wrq)) { 7781 wrq = 0; 7782 break; 7783 } 7784 wrq = wrq->q_next; 7785 } 7786 stp->sd_struiordq = wrq ? _RD(wrq) : 0; 7787 } 7788 7789 /* 7790 * pass_wput, unblocks the passthru queues, so that 7791 * messages can arrive at muxs lower read queue, before 7792 * I_LINK/I_UNLINK is acked/nacked. 7793 */ 7794 static void 7795 pass_wput(queue_t *q, mblk_t *mp) 7796 { 7797 syncq_t *sq; 7798 7799 sq = _RD(q)->q_syncq; 7800 if (sq->sq_flags & SQ_BLOCKED) 7801 unblocksq(sq, SQ_BLOCKED, 0); 7802 putnext(q, mp); 7803 } 7804 7805 /* 7806 * Set up queues for the link/unlink. 7807 * Create a new queue and block it and then insert it 7808 * below the stream head on the lower stream. 7809 * This prevents any messages from arriving during the setq 7810 * as well as while the mux is processing the LINK/I_UNLINK. 7811 * The blocked passq is unblocked once the LINK/I_UNLINK has 7812 * been acked or nacked or if a message is generated and sent 7813 * down muxs write put procedure. 7814 * see pass_wput(). 7815 * 7816 * After the new queue is inserted, all messages coming from below are 7817 * blocked. The call to strlock will ensure that all activity in the stream head 7818 * read queue syncq is stopped (sq_count drops to zero). 7819 */ 7820 static queue_t * 7821 link_addpassthru(stdata_t *stpdown) 7822 { 7823 queue_t *passq; 7824 sqlist_t sqlist; 7825 7826 passq = allocq(); 7827 STREAM(passq) = STREAM(_WR(passq)) = stpdown; 7828 /* setq might sleep in allocator - avoid holding locks. */ 7829 setq(passq, &passthru_rinit, &passthru_winit, NULL, QPERQ, 7830 SQ_CI|SQ_CO, B_FALSE); 7831 claimq(passq); 7832 blocksq(passq->q_syncq, SQ_BLOCKED, 1); 7833 insertq(STREAM(passq), passq); 7834 7835 /* 7836 * Use strlock() to wait for the stream head sq_count to drop to zero 7837 * since we are going to change q_ptr in the stream head. Note that 7838 * insertq() doesn't wait for any syncq counts to drop to zero. 7839 */ 7840 sqlist.sqlist_head = NULL; 7841 sqlist.sqlist_index = 0; 7842 sqlist.sqlist_size = sizeof (sqlist_t); 7843 sqlist_insert(&sqlist, _RD(stpdown->sd_wrq)->q_syncq); 7844 strlock(stpdown, &sqlist); 7845 strunlock(stpdown, &sqlist); 7846 7847 releaseq(passq); 7848 return (passq); 7849 } 7850 7851 /* 7852 * Let messages flow up into the mux by removing 7853 * the passq. 7854 */ 7855 static void 7856 link_rempassthru(queue_t *passq) 7857 { 7858 claimq(passq); 7859 removeq(passq); 7860 releaseq(passq); 7861 freeq(passq); 7862 } 7863 7864 /* 7865 * Wait for the condition variable pointed to by `cvp' to be signaled, 7866 * or for `tim' milliseconds to elapse, whichever comes first. If `tim' 7867 * is negative, then there is no time limit. If `nosigs' is non-zero, 7868 * then the wait will be non-interruptible. 7869 * 7870 * Returns >0 if signaled, 0 if interrupted, or -1 upon timeout. 7871 */ 7872 clock_t 7873 str_cv_wait(kcondvar_t *cvp, kmutex_t *mp, clock_t tim, int nosigs) 7874 { 7875 clock_t ret, now, tick; 7876 7877 if (tim < 0) { 7878 if (nosigs) { 7879 cv_wait(cvp, mp); 7880 ret = 1; 7881 } else { 7882 ret = cv_wait_sig(cvp, mp); 7883 } 7884 } else if (tim > 0) { 7885 /* 7886 * convert milliseconds to clock ticks 7887 */ 7888 tick = MSEC_TO_TICK_ROUNDUP(tim); 7889 time_to_wait(&now, tick); 7890 if (nosigs) { 7891 ret = cv_timedwait(cvp, mp, now); 7892 } else { 7893 ret = cv_timedwait_sig(cvp, mp, now); 7894 } 7895 } else { 7896 ret = -1; 7897 } 7898 return (ret); 7899 } 7900 7901 /* 7902 * Wait until the stream head can determine if it is at the mark but 7903 * don't wait forever to prevent a race condition between the "mark" state 7904 * in the stream head and any mark state in the caller/user of this routine. 7905 * 7906 * This is used by sockets and for a socket it would be incorrect 7907 * to return a failure for SIOCATMARK when there is no data in the receive 7908 * queue and the marked urgent data is traveling up the stream. 7909 * 7910 * This routine waits until the mark is known by waiting for one of these 7911 * three events: 7912 * The stream head read queue becoming non-empty (including an EOF) 7913 * The STRATMARK flag being set. (Due to a MSGMARKNEXT message.) 7914 * The STRNOTATMARK flag being set (which indicates that the transport 7915 * has sent a MSGNOTMARKNEXT message to indicate that it is not at 7916 * the mark). 7917 * 7918 * The routine returns 1 if the stream is at the mark; 0 if it can 7919 * be determined that the stream is not at the mark. 7920 * If the wait times out and it can't determine 7921 * whether or not the stream might be at the mark the routine will return -1. 7922 * 7923 * Note: This routine should only be used when a mark is pending i.e., 7924 * in the socket case the SIGURG has been posted. 7925 * Note2: This can not wakeup just because synchronous streams indicate 7926 * that data is available since it is not possible to use the synchronous 7927 * streams interfaces to determine the b_flag value for the data queued below 7928 * the stream head. 7929 */ 7930 int 7931 strwaitmark(vnode_t *vp) 7932 { 7933 struct stdata *stp = vp->v_stream; 7934 queue_t *rq = _RD(stp->sd_wrq); 7935 int mark; 7936 7937 mutex_enter(&stp->sd_lock); 7938 while (rq->q_first == NULL && 7939 !(stp->sd_flag & (STRATMARK|STRNOTATMARK|STREOF))) { 7940 stp->sd_flag |= RSLEEP; 7941 7942 /* Wait for 100 milliseconds for any state change. */ 7943 if (str_cv_wait(&rq->q_wait, &stp->sd_lock, 100, 1) == -1) { 7944 mutex_exit(&stp->sd_lock); 7945 return (-1); 7946 } 7947 } 7948 if (stp->sd_flag & STRATMARK) 7949 mark = 1; 7950 else if (rq->q_first != NULL && (rq->q_first->b_flag & MSGMARK)) 7951 mark = 1; 7952 else 7953 mark = 0; 7954 7955 mutex_exit(&stp->sd_lock); 7956 return (mark); 7957 } 7958 7959 /* 7960 * Set a read side error. If persist is set change the socket error 7961 * to persistent. If errfunc is set install the function as the exported 7962 * error handler. 7963 */ 7964 void 7965 strsetrerror(vnode_t *vp, int error, int persist, errfunc_t errfunc) 7966 { 7967 struct stdata *stp = vp->v_stream; 7968 7969 mutex_enter(&stp->sd_lock); 7970 stp->sd_rerror = error; 7971 if (error == 0 && errfunc == NULL) 7972 stp->sd_flag &= ~STRDERR; 7973 else 7974 stp->sd_flag |= STRDERR; 7975 if (persist) { 7976 stp->sd_flag &= ~STRDERRNONPERSIST; 7977 } else { 7978 stp->sd_flag |= STRDERRNONPERSIST; 7979 } 7980 stp->sd_rderrfunc = errfunc; 7981 if (error != 0 || errfunc != NULL) { 7982 cv_broadcast(&_RD(stp->sd_wrq)->q_wait); /* readers */ 7983 cv_broadcast(&stp->sd_wrq->q_wait); /* writers */ 7984 cv_broadcast(&stp->sd_monitor); /* ioctllers */ 7985 7986 mutex_exit(&stp->sd_lock); 7987 pollwakeup(&stp->sd_pollist, POLLERR); 7988 mutex_enter(&stp->sd_lock); 7989 7990 if (stp->sd_sigflags & S_ERROR) 7991 strsendsig(stp->sd_siglist, S_ERROR, 0, error); 7992 } 7993 mutex_exit(&stp->sd_lock); 7994 } 7995 7996 /* 7997 * Set a write side error. If persist is set change the socket error 7998 * to persistent. 7999 */ 8000 void 8001 strsetwerror(vnode_t *vp, int error, int persist, errfunc_t errfunc) 8002 { 8003 struct stdata *stp = vp->v_stream; 8004 8005 mutex_enter(&stp->sd_lock); 8006 stp->sd_werror = error; 8007 if (error == 0 && errfunc == NULL) 8008 stp->sd_flag &= ~STWRERR; 8009 else 8010 stp->sd_flag |= STWRERR; 8011 if (persist) { 8012 stp->sd_flag &= ~STWRERRNONPERSIST; 8013 } else { 8014 stp->sd_flag |= STWRERRNONPERSIST; 8015 } 8016 stp->sd_wrerrfunc = errfunc; 8017 if (error != 0 || errfunc != NULL) { 8018 cv_broadcast(&_RD(stp->sd_wrq)->q_wait); /* readers */ 8019 cv_broadcast(&stp->sd_wrq->q_wait); /* writers */ 8020 cv_broadcast(&stp->sd_monitor); /* ioctllers */ 8021 8022 mutex_exit(&stp->sd_lock); 8023 pollwakeup(&stp->sd_pollist, POLLERR); 8024 mutex_enter(&stp->sd_lock); 8025 8026 if (stp->sd_sigflags & S_ERROR) 8027 strsendsig(stp->sd_siglist, S_ERROR, 0, error); 8028 } 8029 mutex_exit(&stp->sd_lock); 8030 } 8031 8032 /* 8033 * Make the stream return 0 (EOF) when all data has been read. 8034 * No effect on write side. 8035 */ 8036 void 8037 strseteof(vnode_t *vp, int eof) 8038 { 8039 struct stdata *stp = vp->v_stream; 8040 8041 mutex_enter(&stp->sd_lock); 8042 if (!eof) { 8043 stp->sd_flag &= ~STREOF; 8044 mutex_exit(&stp->sd_lock); 8045 return; 8046 } 8047 stp->sd_flag |= STREOF; 8048 if (stp->sd_flag & RSLEEP) { 8049 stp->sd_flag &= ~RSLEEP; 8050 cv_broadcast(&_RD(stp->sd_wrq)->q_wait); 8051 } 8052 8053 mutex_exit(&stp->sd_lock); 8054 pollwakeup(&stp->sd_pollist, POLLIN|POLLRDNORM); 8055 mutex_enter(&stp->sd_lock); 8056 8057 if (stp->sd_sigflags & (S_INPUT|S_RDNORM)) 8058 strsendsig(stp->sd_siglist, S_INPUT|S_RDNORM, 0, 0); 8059 mutex_exit(&stp->sd_lock); 8060 } 8061 8062 void 8063 strflushrq(vnode_t *vp, int flag) 8064 { 8065 struct stdata *stp = vp->v_stream; 8066 8067 mutex_enter(&stp->sd_lock); 8068 flushq(_RD(stp->sd_wrq), flag); 8069 mutex_exit(&stp->sd_lock); 8070 } 8071 8072 void 8073 strsetrputhooks(vnode_t *vp, uint_t flags, 8074 msgfunc_t protofunc, msgfunc_t miscfunc) 8075 { 8076 struct stdata *stp = vp->v_stream; 8077 8078 mutex_enter(&stp->sd_lock); 8079 8080 if (protofunc == NULL) 8081 stp->sd_rprotofunc = strrput_proto; 8082 else 8083 stp->sd_rprotofunc = protofunc; 8084 8085 if (miscfunc == NULL) 8086 stp->sd_rmiscfunc = strrput_misc; 8087 else 8088 stp->sd_rmiscfunc = miscfunc; 8089 8090 if (flags & SH_CONSOL_DATA) 8091 stp->sd_rput_opt |= SR_CONSOL_DATA; 8092 else 8093 stp->sd_rput_opt &= ~SR_CONSOL_DATA; 8094 8095 if (flags & SH_SIGALLDATA) 8096 stp->sd_rput_opt |= SR_SIGALLDATA; 8097 else 8098 stp->sd_rput_opt &= ~SR_SIGALLDATA; 8099 8100 if (flags & SH_IGN_ZEROLEN) 8101 stp->sd_rput_opt |= SR_IGN_ZEROLEN; 8102 else 8103 stp->sd_rput_opt &= ~SR_IGN_ZEROLEN; 8104 8105 mutex_exit(&stp->sd_lock); 8106 } 8107 8108 void 8109 strsetwputhooks(vnode_t *vp, uint_t flags, clock_t closetime) 8110 { 8111 struct stdata *stp = vp->v_stream; 8112 8113 mutex_enter(&stp->sd_lock); 8114 stp->sd_closetime = closetime; 8115 8116 if (flags & SH_SIGPIPE) 8117 stp->sd_wput_opt |= SW_SIGPIPE; 8118 else 8119 stp->sd_wput_opt &= ~SW_SIGPIPE; 8120 if (flags & SH_RECHECK_ERR) 8121 stp->sd_wput_opt |= SW_RECHECK_ERR; 8122 else 8123 stp->sd_wput_opt &= ~SW_RECHECK_ERR; 8124 8125 mutex_exit(&stp->sd_lock); 8126 } 8127 8128 void 8129 strsetrwputdatahooks(vnode_t *vp, msgfunc_t rdatafunc, msgfunc_t wdatafunc) 8130 { 8131 struct stdata *stp = vp->v_stream; 8132 8133 mutex_enter(&stp->sd_lock); 8134 8135 stp->sd_rputdatafunc = rdatafunc; 8136 stp->sd_wputdatafunc = wdatafunc; 8137 8138 mutex_exit(&stp->sd_lock); 8139 } 8140 8141 /* Used within framework when the queue is already locked */ 8142 void 8143 qenable_locked(queue_t *q) 8144 { 8145 stdata_t *stp = STREAM(q); 8146 8147 ASSERT(MUTEX_HELD(QLOCK(q))); 8148 8149 if (!q->q_qinfo->qi_srvp) 8150 return; 8151 8152 /* 8153 * Do not place on run queue if already enabled or closing. 8154 */ 8155 if (q->q_flag & (QWCLOSE|QENAB)) 8156 return; 8157 8158 /* 8159 * mark queue enabled and place on run list if it is not already being 8160 * serviced. If it is serviced, the runservice() function will detect 8161 * that QENAB is set and call service procedure before clearing 8162 * QINSERVICE flag. 8163 */ 8164 q->q_flag |= QENAB; 8165 if (q->q_flag & QINSERVICE) 8166 return; 8167 8168 /* Record the time of qenable */ 8169 q->q_qtstamp = lbolt; 8170 8171 /* 8172 * Put the queue in the stp list and schedule it for background 8173 * processing if it is not already scheduled or if stream head does not 8174 * intent to process it in the foreground later by setting 8175 * STRS_WILLSERVICE flag. 8176 */ 8177 mutex_enter(&stp->sd_qlock); 8178 /* 8179 * If there are already something on the list, stp flags should show 8180 * intention to drain it. 8181 */ 8182 IMPLY(STREAM_NEEDSERVICE(stp), 8183 (stp->sd_svcflags & (STRS_WILLSERVICE | STRS_SCHEDULED))); 8184 8185 ENQUEUE(q, stp->sd_qhead, stp->sd_qtail, q_link); 8186 stp->sd_nqueues++; 8187 8188 /* 8189 * If no one will drain this stream we are the first producer and 8190 * need to schedule it for background thread. 8191 */ 8192 if (!(stp->sd_svcflags & (STRS_WILLSERVICE | STRS_SCHEDULED))) { 8193 /* 8194 * No one will service this stream later, so we have to 8195 * schedule it now. 8196 */ 8197 STRSTAT(stenables); 8198 stp->sd_svcflags |= STRS_SCHEDULED; 8199 stp->sd_servid = (void *)taskq_dispatch(streams_taskq, 8200 (task_func_t *)stream_service, stp, TQ_NOSLEEP|TQ_NOQUEUE); 8201 8202 if (stp->sd_servid == NULL) { 8203 /* 8204 * Task queue failed so fail over to the backup 8205 * servicing thread. 8206 */ 8207 STRSTAT(taskqfails); 8208 /* 8209 * It is safe to clear STRS_SCHEDULED flag because it 8210 * was set by this thread above. 8211 */ 8212 stp->sd_svcflags &= ~STRS_SCHEDULED; 8213 8214 /* 8215 * Failover scheduling is protected by service_queue 8216 * lock. 8217 */ 8218 mutex_enter(&service_queue); 8219 ASSERT((stp->sd_qhead == q) && (stp->sd_qtail == q)); 8220 ASSERT(q->q_link == NULL); 8221 /* 8222 * Append the queue to qhead/qtail list. 8223 */ 8224 if (qhead == NULL) 8225 qhead = q; 8226 else 8227 qtail->q_link = q; 8228 qtail = q; 8229 /* 8230 * Clear stp queue list. 8231 */ 8232 stp->sd_qhead = stp->sd_qtail = NULL; 8233 stp->sd_nqueues = 0; 8234 /* 8235 * Wakeup background queue processing thread. 8236 */ 8237 cv_signal(&services_to_run); 8238 mutex_exit(&service_queue); 8239 } 8240 } 8241 mutex_exit(&stp->sd_qlock); 8242 } 8243 8244 static void 8245 queue_service(queue_t *q) 8246 { 8247 /* 8248 * The queue in the list should have 8249 * QENAB flag set and should not have 8250 * QINSERVICE flag set. QINSERVICE is 8251 * set when the queue is dequeued and 8252 * qenable_locked doesn't enqueue a 8253 * queue with QINSERVICE set. 8254 */ 8255 8256 ASSERT(!(q->q_flag & QINSERVICE)); 8257 ASSERT((q->q_flag & QENAB)); 8258 mutex_enter(QLOCK(q)); 8259 q->q_flag &= ~QENAB; 8260 q->q_flag |= QINSERVICE; 8261 mutex_exit(QLOCK(q)); 8262 runservice(q); 8263 } 8264 8265 static void 8266 syncq_service(syncq_t *sq) 8267 { 8268 STRSTAT(syncqservice); 8269 mutex_enter(SQLOCK(sq)); 8270 ASSERT(!(sq->sq_svcflags & SQ_SERVICE)); 8271 ASSERT(sq->sq_servcount != 0); 8272 ASSERT(sq->sq_next == NULL); 8273 8274 /* if we came here from the background thread, clear the flag */ 8275 if (sq->sq_svcflags & SQ_BGTHREAD) 8276 sq->sq_svcflags &= ~SQ_BGTHREAD; 8277 8278 /* let drain_syncq know that it's being called in the background */ 8279 sq->sq_svcflags |= SQ_SERVICE; 8280 drain_syncq(sq); 8281 } 8282 8283 static void 8284 qwriter_outer_service(syncq_t *outer) 8285 { 8286 /* 8287 * Note that SQ_WRITER is used on the outer perimeter 8288 * to signal that a qwriter(OUTER) is either investigating 8289 * running or that it is actually running a function. 8290 */ 8291 outer_enter(outer, SQ_BLOCKED|SQ_WRITER); 8292 8293 /* 8294 * All inner syncq are empty and have SQ_WRITER set 8295 * to block entering the outer perimeter. 8296 * 8297 * We do not need to explicitly call write_now since 8298 * outer_exit does it for us. 8299 */ 8300 outer_exit(outer); 8301 } 8302 8303 static void 8304 mblk_free(mblk_t *mp) 8305 { 8306 dblk_t *dbp = mp->b_datap; 8307 frtn_t *frp = dbp->db_frtnp; 8308 8309 mp->b_next = NULL; 8310 if (dbp->db_fthdr != NULL) 8311 str_ftfree(dbp); 8312 8313 ASSERT(dbp->db_fthdr == NULL); 8314 frp->free_func(frp->free_arg); 8315 ASSERT(dbp->db_mblk == mp); 8316 8317 if (dbp->db_credp != NULL) { 8318 crfree(dbp->db_credp); 8319 dbp->db_credp = NULL; 8320 } 8321 dbp->db_cpid = -1; 8322 dbp->db_struioflag = 0; 8323 dbp->db_struioun.cksum.flags = 0; 8324 8325 kmem_cache_free(dbp->db_cache, dbp); 8326 } 8327 8328 /* 8329 * Background processing of the stream queue list. 8330 */ 8331 static void 8332 stream_service(stdata_t *stp) 8333 { 8334 queue_t *q; 8335 8336 mutex_enter(&stp->sd_qlock); 8337 8338 STR_SERVICE(stp, q); 8339 8340 stp->sd_svcflags &= ~STRS_SCHEDULED; 8341 stp->sd_servid = NULL; 8342 cv_signal(&stp->sd_qcv); 8343 mutex_exit(&stp->sd_qlock); 8344 } 8345 8346 /* 8347 * Foreground processing of the stream queue list. 8348 */ 8349 void 8350 stream_runservice(stdata_t *stp) 8351 { 8352 queue_t *q; 8353 8354 mutex_enter(&stp->sd_qlock); 8355 STRSTAT(rservice); 8356 /* 8357 * We are going to drain this stream queue list, so qenable_locked will 8358 * not schedule it until we finish. 8359 */ 8360 stp->sd_svcflags |= STRS_WILLSERVICE; 8361 8362 STR_SERVICE(stp, q); 8363 8364 stp->sd_svcflags &= ~STRS_WILLSERVICE; 8365 mutex_exit(&stp->sd_qlock); 8366 /* 8367 * Help backup background thread to drain the qhead/qtail list. 8368 */ 8369 while (qhead != NULL) { 8370 STRSTAT(qhelps); 8371 mutex_enter(&service_queue); 8372 DQ(q, qhead, qtail, q_link); 8373 mutex_exit(&service_queue); 8374 if (q != NULL) 8375 queue_service(q); 8376 } 8377 } 8378 8379 void 8380 stream_willservice(stdata_t *stp) 8381 { 8382 mutex_enter(&stp->sd_qlock); 8383 stp->sd_svcflags |= STRS_WILLSERVICE; 8384 mutex_exit(&stp->sd_qlock); 8385 } 8386 8387 /* 8388 * Replace the cred currently in the mblk with a different one. 8389 */ 8390 void 8391 mblk_setcred(mblk_t *mp, cred_t *cr) 8392 { 8393 cred_t *ocr = DB_CRED(mp); 8394 8395 ASSERT(cr != NULL); 8396 8397 if (cr != ocr) { 8398 crhold(mp->b_datap->db_credp = cr); 8399 if (ocr != NULL) 8400 crfree(ocr); 8401 } 8402 } 8403 8404 int 8405 hcksum_assoc(mblk_t *mp, multidata_t *mmd, pdesc_t *pd, 8406 uint32_t start, uint32_t stuff, uint32_t end, uint32_t value, 8407 uint32_t flags, int km_flags) 8408 { 8409 int rc = 0; 8410 8411 ASSERT(DB_TYPE(mp) == M_DATA || DB_TYPE(mp) == M_MULTIDATA); 8412 if (mp->b_datap->db_type == M_DATA) { 8413 /* Associate values for M_DATA type */ 8414 DB_CKSUMSTART(mp) = (intptr_t)start; 8415 DB_CKSUMSTUFF(mp) = (intptr_t)stuff; 8416 DB_CKSUMEND(mp) = (intptr_t)end; 8417 DB_CKSUMFLAGS(mp) = flags; 8418 DB_CKSUM16(mp) = (uint16_t)value; 8419 8420 } else { 8421 pattrinfo_t pa_info; 8422 8423 ASSERT(mmd != NULL); 8424 8425 pa_info.type = PATTR_HCKSUM; 8426 pa_info.len = sizeof (pattr_hcksum_t); 8427 8428 if (mmd_addpattr(mmd, pd, &pa_info, B_TRUE, km_flags) != NULL) { 8429 pattr_hcksum_t *hck = (pattr_hcksum_t *)pa_info.buf; 8430 8431 hck->hcksum_start_offset = start; 8432 hck->hcksum_stuff_offset = stuff; 8433 hck->hcksum_end_offset = end; 8434 hck->hcksum_cksum_val.inet_cksum = (uint16_t)value; 8435 hck->hcksum_flags = flags; 8436 } else { 8437 rc = -1; 8438 } 8439 } 8440 return (rc); 8441 } 8442 8443 void 8444 hcksum_retrieve(mblk_t *mp, multidata_t *mmd, pdesc_t *pd, 8445 uint32_t *start, uint32_t *stuff, uint32_t *end, 8446 uint32_t *value, uint32_t *flags) 8447 { 8448 ASSERT(DB_TYPE(mp) == M_DATA || DB_TYPE(mp) == M_MULTIDATA); 8449 if (mp->b_datap->db_type == M_DATA) { 8450 if (flags != NULL) { 8451 *flags = DB_CKSUMFLAGS(mp) & (HCK_IPV4_HDRCKSUM | 8452 HCK_PARTIALCKSUM | HCK_FULLCKSUM | 8453 HCK_FULLCKSUM_OK); 8454 if ((*flags & (HCK_PARTIALCKSUM | 8455 HCK_FULLCKSUM)) != 0) { 8456 if (value != NULL) 8457 *value = (uint32_t)DB_CKSUM16(mp); 8458 if ((*flags & HCK_PARTIALCKSUM) != 0) { 8459 if (start != NULL) 8460 *start = 8461 (uint32_t)DB_CKSUMSTART(mp); 8462 if (stuff != NULL) 8463 *stuff = 8464 (uint32_t)DB_CKSUMSTUFF(mp); 8465 if (end != NULL) 8466 *end = 8467 (uint32_t)DB_CKSUMEND(mp); 8468 } 8469 } 8470 } 8471 } else { 8472 pattrinfo_t hck_attr = {PATTR_HCKSUM}; 8473 8474 ASSERT(mmd != NULL); 8475 8476 /* get hardware checksum attribute */ 8477 if (mmd_getpattr(mmd, pd, &hck_attr) != NULL) { 8478 pattr_hcksum_t *hck = (pattr_hcksum_t *)hck_attr.buf; 8479 8480 ASSERT(hck_attr.len >= sizeof (pattr_hcksum_t)); 8481 if (flags != NULL) 8482 *flags = hck->hcksum_flags; 8483 if (start != NULL) 8484 *start = hck->hcksum_start_offset; 8485 if (stuff != NULL) 8486 *stuff = hck->hcksum_stuff_offset; 8487 if (end != NULL) 8488 *end = hck->hcksum_end_offset; 8489 if (value != NULL) 8490 *value = (uint32_t) 8491 hck->hcksum_cksum_val.inet_cksum; 8492 } 8493 } 8494 } 8495 8496 void 8497 lso_info_set(mblk_t *mp, uint32_t mss, uint32_t flags) 8498 { 8499 ASSERT(DB_TYPE(mp) == M_DATA); 8500 8501 /* Set the flags */ 8502 DB_LSOFLAGS(mp) |= flags; 8503 DB_LSOMSS(mp) = mss; 8504 } 8505 8506 void 8507 lso_info_get(mblk_t *mp, uint32_t *mss, uint32_t *flags) 8508 { 8509 ASSERT(DB_TYPE(mp) == M_DATA); 8510 8511 if (flags != NULL) { 8512 *flags = DB_CKSUMFLAGS(mp) & HW_LSO; 8513 if ((*flags != 0) && (mss != NULL)) 8514 *mss = (uint32_t)DB_LSOMSS(mp); 8515 } 8516 } 8517 8518 /* 8519 * Checksum buffer *bp for len bytes with psum partial checksum, 8520 * or 0 if none, and return the 16 bit partial checksum. 8521 */ 8522 unsigned 8523 bcksum(uchar_t *bp, int len, unsigned int psum) 8524 { 8525 int odd = len & 1; 8526 extern unsigned int ip_ocsum(); 8527 8528 if (((intptr_t)bp & 1) == 0 && !odd) { 8529 /* 8530 * Bp is 16 bit aligned and len is multiple of 16 bit word. 8531 */ 8532 return (ip_ocsum((ushort_t *)bp, len >> 1, psum)); 8533 } 8534 if (((intptr_t)bp & 1) != 0) { 8535 /* 8536 * Bp isn't 16 bit aligned. 8537 */ 8538 unsigned int tsum; 8539 8540 #ifdef _LITTLE_ENDIAN 8541 psum += *bp; 8542 #else 8543 psum += *bp << 8; 8544 #endif 8545 len--; 8546 bp++; 8547 tsum = ip_ocsum((ushort_t *)bp, len >> 1, 0); 8548 psum += (tsum << 8) & 0xffff | (tsum >> 8); 8549 if (len & 1) { 8550 bp += len - 1; 8551 #ifdef _LITTLE_ENDIAN 8552 psum += *bp << 8; 8553 #else 8554 psum += *bp; 8555 #endif 8556 } 8557 } else { 8558 /* 8559 * Bp is 16 bit aligned. 8560 */ 8561 psum = ip_ocsum((ushort_t *)bp, len >> 1, psum); 8562 if (odd) { 8563 bp += len - 1; 8564 #ifdef _LITTLE_ENDIAN 8565 psum += *bp; 8566 #else 8567 psum += *bp << 8; 8568 #endif 8569 } 8570 } 8571 /* 8572 * Normalize psum to 16 bits before returning the new partial 8573 * checksum. The max psum value before normalization is 0x3FDFE. 8574 */ 8575 return ((psum >> 16) + (psum & 0xFFFF)); 8576 } 8577 8578 boolean_t 8579 is_vmloaned_mblk(mblk_t *mp, multidata_t *mmd, pdesc_t *pd) 8580 { 8581 boolean_t rc; 8582 8583 ASSERT(DB_TYPE(mp) == M_DATA || DB_TYPE(mp) == M_MULTIDATA); 8584 if (DB_TYPE(mp) == M_DATA) { 8585 rc = (((mp)->b_datap->db_struioflag & STRUIO_ZC) != 0); 8586 } else { 8587 pattrinfo_t zcopy_attr = {PATTR_ZCOPY}; 8588 8589 ASSERT(mmd != NULL); 8590 rc = (mmd_getpattr(mmd, pd, &zcopy_attr) != NULL); 8591 } 8592 return (rc); 8593 } 8594 8595 void 8596 freemsgchain(mblk_t *mp) 8597 { 8598 mblk_t *next; 8599 8600 while (mp != NULL) { 8601 next = mp->b_next; 8602 mp->b_next = NULL; 8603 8604 freemsg(mp); 8605 mp = next; 8606 } 8607 } 8608 8609 mblk_t * 8610 copymsgchain(mblk_t *mp) 8611 { 8612 mblk_t *nmp = NULL; 8613 mblk_t **nmpp = &nmp; 8614 8615 for (; mp != NULL; mp = mp->b_next) { 8616 if ((*nmpp = copymsg(mp)) == NULL) { 8617 freemsgchain(nmp); 8618 return (NULL); 8619 } 8620 8621 nmpp = &((*nmpp)->b_next); 8622 } 8623 8624 return (nmp); 8625 } 8626 8627 /* NOTE: Do not add code after this point. */ 8628 #undef QLOCK 8629 8630 /* 8631 * replacement for QLOCK macro for those that can't use it. 8632 */ 8633 kmutex_t * 8634 QLOCK(queue_t *q) 8635 { 8636 return (&(q)->q_lock); 8637 } 8638 8639 /* 8640 * Dummy runqueues/queuerun functions functions for backwards compatibility. 8641 */ 8642 #undef runqueues 8643 void 8644 runqueues(void) 8645 { 8646 } 8647 8648 #undef queuerun 8649 void 8650 queuerun(void) 8651 { 8652 } 8653 8654 /* 8655 * Initialize the STR stack instance, which tracks autopush and persistent 8656 * links. 8657 */ 8658 /* ARGSUSED */ 8659 static void * 8660 str_stack_init(netstackid_t stackid, netstack_t *ns) 8661 { 8662 str_stack_t *ss; 8663 int i; 8664 8665 ss = (str_stack_t *)kmem_zalloc(sizeof (*ss), KM_SLEEP); 8666 ss->ss_netstack = ns; 8667 8668 /* 8669 * set up autopush 8670 */ 8671 sad_initspace(ss); 8672 8673 /* 8674 * set up mux_node structures. 8675 */ 8676 ss->ss_devcnt = devcnt; /* In case it should change before free */ 8677 ss->ss_mux_nodes = kmem_zalloc((sizeof (struct mux_node) * 8678 ss->ss_devcnt), KM_SLEEP); 8679 for (i = 0; i < ss->ss_devcnt; i++) 8680 ss->ss_mux_nodes[i].mn_imaj = i; 8681 return (ss); 8682 } 8683 8684 /* 8685 * Note: run at zone shutdown and not destroy so that the PLINKs are 8686 * gone by the time other cleanup happens from the destroy callbacks. 8687 */ 8688 static void 8689 str_stack_shutdown(netstackid_t stackid, void *arg) 8690 { 8691 str_stack_t *ss = (str_stack_t *)arg; 8692 int i; 8693 cred_t *cr; 8694 8695 cr = zone_get_kcred(netstackid_to_zoneid(stackid)); 8696 ASSERT(cr != NULL); 8697 8698 /* Undo all the I_PLINKs for this zone */ 8699 for (i = 0; i < ss->ss_devcnt; i++) { 8700 struct mux_edge *ep; 8701 ldi_handle_t lh; 8702 ldi_ident_t li; 8703 int ret; 8704 int rval; 8705 dev_t rdev; 8706 8707 ep = ss->ss_mux_nodes[i].mn_outp; 8708 if (ep == NULL) 8709 continue; 8710 ret = ldi_ident_from_major((major_t)i, &li); 8711 if (ret != 0) { 8712 continue; 8713 } 8714 rdev = ep->me_dev; 8715 ret = ldi_open_by_dev(&rdev, OTYP_CHR, FREAD|FWRITE, 8716 cr, &lh, li); 8717 if (ret != 0) { 8718 ldi_ident_release(li); 8719 continue; 8720 } 8721 8722 ret = ldi_ioctl(lh, I_PUNLINK, (intptr_t)MUXID_ALL, FKIOCTL, 8723 cr, &rval); 8724 if (ret) { 8725 (void) ldi_close(lh, FREAD|FWRITE, cr); 8726 ldi_ident_release(li); 8727 continue; 8728 } 8729 (void) ldi_close(lh, FREAD|FWRITE, cr); 8730 8731 /* Close layered handles */ 8732 ldi_ident_release(li); 8733 } 8734 crfree(cr); 8735 8736 sad_freespace(ss); 8737 8738 kmem_free(ss->ss_mux_nodes, sizeof (struct mux_node) * ss->ss_devcnt); 8739 ss->ss_mux_nodes = NULL; 8740 } 8741 8742 /* 8743 * Free the structure; str_stack_shutdown did the other cleanup work. 8744 */ 8745 /* ARGSUSED */ 8746 static void 8747 str_stack_fini(netstackid_t stackid, void *arg) 8748 { 8749 str_stack_t *ss = (str_stack_t *)arg; 8750 8751 kmem_free(ss, sizeof (*ss)); 8752 } 8753