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.
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.
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]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
31 * UNIX Device Driver Interface functions
32 * This file contains the C-versions of putnext() and put().
33 * Assembly language versions exist for some architectures.
36 #include <sys/types.h>
37 #include <sys/systm.h>
38 #include <sys/cpuvar.h>
39 #include <sys/debug.h>
40 #include <sys/t_lock.h>
41 #include <sys/stream.h>
42 #include <sys/thread.h>
43 #include <sys/strsubr.h>
45 #include <sys/vtrace.h>
46 #include <sys/cmn_err.h>
47 #include <sys/strft.h>
48 #include <sys/stack.h>
49 #include <sys/archsystm.h>
52 * Streams with many modules may create long chains of calls via putnext() which
53 * may exhaust stack space. When putnext detects that the stack space left is
54 * too small (less then PUT_STACK_NEEDED), the call chain is broken and
55 * further processing is delegated to the background thread via call to
56 * putnext_tail(). Unfortunately there is no generic solution with fixed stack
57 * size, and putnext() is recursive function, so this hack is a necessary evil.
59 * The redzone value is chosen dependent on the default stack size which is 8K
60 * on 32-bit kernels and on x86 and 16K on 64-bit kernels. The values are chosen
61 * empirically. For 64-bit kernels it is 5000 and for 32-bit kernels it is 3000.
62 * Experiments showed that 2500 is not enough for either 32-bit or 64-bit
65 * The redzone value is a tuneable rather then a constant to allow adjustments
68 * The check in PUT_STACK_NOTENOUGH is taken from segkp_map_red() function. It
69 * is possible to define it as a generic function exported by seg_kp, but
71 * a) It may sound like an open invitation to use the facility indiscriminately.
72 * b) It adds extra function call in putnext path.
74 * We keep a global counter `put_stack_notenough' which keeps track how many
75 * times the stack switching hack was used.
78 static ulong_t put_stack_notenough
;
81 #define PUT_STACK_NEEDED 5000
83 #define PUT_STACK_NEEDED 3000
86 int put_stack_needed
= PUT_STACK_NEEDED
;
88 #if defined(STACK_GROWTH_DOWN)
89 #define PUT_STACK_NOTENOUGH() \
90 (((STACK_BIAS + (uintptr_t)getfp() - \
91 (uintptr_t)curthread->t_stkbase) < put_stack_needed) && \
92 ++put_stack_notenough)
94 #error "STACK_GROWTH_DOWN undefined"
97 boolean_t UseFastlocks
= B_FALSE
;
100 * function: putnext()
101 * purpose: call the put routine of the queue linked to qp
103 * Note: this function is written to perform well on modern computer
104 * architectures by e.g. preloading values into registers and "smearing" out
107 * A note on the fastput mechanism. The most significant bit of a
108 * putcount is considered the "FASTPUT" bit. If set, then there is
109 * nothing stoping a concurrent put from occuring (note that putcounts
110 * are only allowed on CIPUT perimiters). If, however, it is cleared,
111 * then we need to take the normal lock path by aquiring the SQLOCK.
112 * This is a slowlock. When a thread starts exclusiveness, e.g. wants
113 * writer access, it will clear the FASTPUT bit, causing new threads
114 * to take the slowlock path. This assures that putcounts will not
115 * increase in value, so the want-writer does not need to constantly
116 * aquire the putlocks to sum the putcounts. This does have the
117 * possibility of having the count drop right after reading, but that
118 * is no different than aquiring, reading and then releasing. However,
119 * in this mode, it cannot go up, so eventually they will drop to zero
120 * and the want-writer can proceed.
122 * If the FASTPUT bit is set, or in the slowlock path we see that there
123 * are no writers or want-writers, we make the choice of calling the
124 * putproc, or a "fast-fill_syncq". The fast-fill is a fill with
125 * immediate intention to drain. This is done because there are
126 * messages already at the queue waiting to drain. To preserve message
127 * ordering, we need to put this message at the end, and pickup the
128 * messages at the beginning. We call the macro that actually
129 * enqueues the message on the queue, and then call qdrain_syncq. If
130 * there is already a drainer, we just return. We could make that
131 * check before calling qdrain_syncq, but it is a little more clear
132 * to have qdrain_syncq do this (we might try the above optimization
133 * as this behavior evolves). qdrain_syncq assumes that SQ_EXCL is set
134 * already if this is a non-CIPUT perimiter, and that an appropriate
135 * claim has been made. So we do all that work before dropping the
136 * SQLOCK with our claim.
138 * If we cannot proceed with the putproc/fast-fill, we just fall
139 * through to the qfill_syncq, and then tail processing. If state
140 * has changed in that cycle, or wakeups are needed, it will occur
144 putnext(queue_t
*qp
, mblk_t
*mp
)
146 queue_t
*fqp
= qp
; /* For strft tracing */
154 boolean_t queued
= B_FALSE
;
155 kmutex_t
*sdlock
= NULL
;
156 kmutex_t
*sqciplock
= NULL
;
157 ushort_t
*sqcipcount
= NULL
;
159 TRACE_2(TR_FAC_STREAMS_FR
, TR_PUTNEXT_START
,
160 "putnext_start:(%p, %p)", qp
, mp
);
162 ASSERT(mp
->b_datap
->db_ref
!= 0);
163 ASSERT(mp
->b_next
== NULL
&& mp
->b_prev
== NULL
);
166 if (stp
->sd_ciputctrl
!= NULL
) {
167 ix
= CPU
->cpu_seqid
& stp
->sd_nciputctrl
;
168 sdlock
= &stp
->sd_ciputctrl
[ix
].ciputctrl_lock
;
171 mutex_enter(sdlock
= &stp
->sd_lock
);
176 ASSERT(MUTEX_NOT_HELD(SQLOCK(sq
)));
179 if (sq
->sq_ciputctrl
!= NULL
) {
181 ASSERT(sq
->sq_flags
& SQ_CIPUT
);
182 ix
= CPU
->cpu_seqid
& sq
->sq_nciputctrl
;
183 sqciplock
= &sq
->sq_ciputctrl
[ix
].ciputctrl_lock
;
184 sqcipcount
= &sq
->sq_ciputctrl
[ix
].ciputctrl_count
;
185 mutex_enter(sqciplock
);
186 if (!((*sqcipcount
) & SQ_FASTPUT
) ||
187 (sq
->sq_flags
& (SQ_STAYAWAY
|SQ_EXCL
|SQ_EVENTS
))) {
188 mutex_exit(sqciplock
);
194 ASSERT(*sqcipcount
!= 0);
195 queued
= qp
->q_sqflags
& Q_SQQUEUED
;
196 mutex_exit(sqciplock
);
199 ASSERT(sqciplock
== NULL
);
200 mutex_enter(SQLOCK(sq
));
202 flags
= sq
->sq_flags
;
204 * We are going to drop SQLOCK, so make a claim to prevent syncq
208 ASSERT(sq
->sq_count
!= 0); /* Wraparound */
210 * If there are writers or exclusive waiters, there is not much
211 * we can do. Place the message on the syncq and schedule a
212 * background thread to drain it.
214 * Also if we are approaching end of stack, fill the syncq and
215 * switch processing to a background thread - see comments on
218 if ((flags
& (SQ_STAYAWAY
|SQ_EXCL
|SQ_EVENTS
)) ||
219 (sq
->sq_needexcl
!= 0) || PUT_STACK_NOTENOUGH()) {
221 TRACE_3(TR_FAC_STREAMS_FR
, TR_PUTNEXT_END
,
222 "putnext_end:(%p, %p, %p) SQ_EXCL fill",
226 * NOTE: qfill_syncq will need QLOCK. It is safe to drop
227 * SQLOCK because positive sq_count keeps the syncq from
230 mutex_exit(SQLOCK(sq
));
232 qfill_syncq(sq
, qp
, mp
);
234 * NOTE: after the call to qfill_syncq() qp may be
235 * closed, both qp and sq should not be referenced at
238 * This ASSERT is located here to prevent stack frame
239 * consumption in the DEBUG code.
241 ASSERT(sqciplock
== NULL
);
245 queued
= qp
->q_sqflags
& Q_SQQUEUED
;
247 * If not a concurrent perimiter, we need to acquire
248 * it exclusively. It could not have been previously
249 * set since we held the SQLOCK before testing
250 * SQ_GOAWAY above (which includes SQ_EXCL).
251 * We do this here because we hold the SQLOCK, and need
252 * to make this state change BEFORE dropping it.
254 if (!(flags
& SQ_CIPUT
)) {
255 ASSERT((sq
->sq_flags
& SQ_EXCL
) == 0);
256 ASSERT(!(sq
->sq_type
& SQ_CIPUT
));
257 sq
->sq_flags
|= SQ_EXCL
;
259 mutex_exit(SQLOCK(sq
));
262 ASSERT((sq
->sq_flags
& (SQ_EXCL
|SQ_CIPUT
)));
263 ASSERT(MUTEX_NOT_HELD(SQLOCK(sq
)));
266 * We now have a claim on the syncq, we are either going to
267 * put the message on the syncq and then drain it, or we are
268 * going to call the putproc().
270 putproc
= qi
->qi_putp
;
272 STR_FTEVENT_MSG(mp
, fqp
, FTEV_PUTNEXT
, mp
->b_rptr
-
273 mp
->b_datap
->db_base
);
275 ASSERT(MUTEX_NOT_HELD(SQLOCK(sq
)));
276 ASSERT(MUTEX_NOT_HELD(QLOCK(qp
)));
278 mutex_enter(QLOCK(qp
));
280 * If there are no messages in front of us, just call putproc(),
281 * otherwise enqueue the message and drain the queue.
283 if (qp
->q_syncqmsgs
== 0) {
284 mutex_exit(QLOCK(qp
));
285 STR_FTEVENT_MSG(mp
, fqp
, FTEV_PUTNEXT
, mp
->b_rptr
-
286 mp
->b_datap
->db_base
);
288 ASSERT(MUTEX_NOT_HELD(SQLOCK(sq
)));
291 * We are doing a fill with the intent to
292 * drain (meaning we are filling because
293 * there are messages in front of us ane we
294 * need to preserve message ordering)
295 * Therefore, put the message on the queue
296 * and call qdrain_syncq (must be done with
299 STR_FTEVENT_MSG(mp
, fqp
, FTEV_PUTNEXT
,
300 mp
->b_rptr
- mp
->b_datap
->db_base
);
304 * These two values were in the original code for
305 * all syncq messages. This is unnecessary in
306 * the current implementation, but was retained
307 * in debug mode as it is usefull to know where
311 mp
->b_prev
= (mblk_t
*)putproc
;
314 qdrain_syncq(sq
, qp
);
315 ASSERT(MUTEX_NOT_HELD(QLOCK(qp
)));
319 * Before we release our claim, we need to see if any
320 * events were posted. If the syncq is SQ_EXCL && SQ_QUEUED,
321 * we were responsible for going exclusive and, therefore,
322 * are resposible for draining.
324 if (sq
->sq_flags
& (SQ_EXCL
)) {
327 drain_mask
= SQ_QUEUED
;
330 if (sqciplock
!= NULL
) {
331 mutex_enter(sqciplock
);
332 flags
= sq
->sq_flags
;
333 ASSERT(flags
& SQ_CIPUT
);
334 /* SQ_EXCL could have been set by qwriter_inner */
335 if ((flags
& (SQ_EXCL
|SQ_TAIL
)) || sq
->sq_needexcl
) {
337 * we need SQLOCK to handle
338 * wakeups/drains/flags change. sqciplock
339 * is needed to decrement sqcipcount.
340 * SQLOCK has to be grabbed before sqciplock
341 * for lock ordering purposes.
342 * after sqcipcount is decremented some lock
343 * still needs to be held to make sure
344 * syncq won't get freed on us.
346 * To prevent deadlocks we try to grab SQLOCK and if it
347 * is held already we drop sqciplock, acquire SQLOCK and
348 * reacqwire sqciplock again.
350 if (mutex_tryenter(SQLOCK(sq
)) == 0) {
351 mutex_exit(sqciplock
);
352 mutex_enter(SQLOCK(sq
));
353 mutex_enter(sqciplock
);
355 flags
= sq
->sq_flags
;
356 ASSERT(*sqcipcount
!= 0);
358 mutex_exit(sqciplock
);
360 ASSERT(*sqcipcount
!= 0);
362 mutex_exit(sqciplock
);
363 TRACE_3(TR_FAC_STREAMS_FR
, TR_PUTNEXT_END
,
364 "putnext_end:(%p, %p, %p) done", qp
, mp
, sq
);
368 mutex_enter(SQLOCK(sq
));
369 flags
= sq
->sq_flags
;
370 ASSERT(sq
->sq_count
!= 0);
373 if ((flags
& (SQ_TAIL
)) || sq
->sq_needexcl
) {
374 putnext_tail(sq
, qp
, (flags
& ~drain_mask
));
376 * The only purpose of this ASSERT is to preserve calling stack
382 ASSERT((sq
->sq_flags
& (SQ_EXCL
|SQ_CIPUT
)) || queued
);
383 ASSERT((flags
& (SQ_EXCL
|SQ_CIPUT
)) || queued
);
385 * Safe to always drop SQ_EXCL:
386 * Not SQ_CIPUT means we set SQ_EXCL above
387 * For SQ_CIPUT SQ_EXCL will only be set if the put
388 * procedure did a qwriter(INNER) in which case
389 * nobody else is in the inner perimeter and we
392 * I would like to make the following assertion:
394 * ASSERT((flags & (SQ_EXCL|SQ_CIPUT)) != (SQ_EXCL|SQ_CIPUT) ||
395 * sq->sq_count == 0);
397 * which indicates that if we are both putshared and exclusive,
398 * we became exclusive while executing the putproc, and the only
399 * claim on the syncq was the one we dropped a few lines above.
400 * But other threads that enter putnext while the syncq is exclusive
401 * need to make a claim as they may need to drop SQLOCK in the
402 * has_writers case to avoid deadlocks. If these threads are
403 * delayed or preempted, it is possible that the writer thread can
404 * find out that there are other claims making the (sq_count == 0)
408 sq
->sq_flags
= flags
& ~SQ_EXCL
;
409 mutex_exit(SQLOCK(sq
));
410 TRACE_3(TR_FAC_STREAMS_FR
, TR_PUTNEXT_END
,
411 "putnext_end:(%p, %p, %p) done", qp
, mp
, sq
);
416 * wrapper for qi_putp entry in module ops vec.
417 * implements asynchronous putnext().
418 * Note, that unlike putnext(), this routine is NOT optimized for the
419 * fastpath. Calling this routine will grab whatever locks are necessary
420 * to protect the stream head, q_next, and syncq's.
421 * And since it is in the normal locks path, we do not use putlocks if
422 * they exist (though this can be changed by swapping the value of
426 put(queue_t
*qp
, mblk_t
*mp
)
428 queue_t
*fqp
= qp
; /* For strft tracing */
435 boolean_t queued
= B_FALSE
;
436 kmutex_t
*sqciplock
= NULL
;
437 ushort_t
*sqcipcount
= NULL
;
439 TRACE_2(TR_FAC_STREAMS_FR
, TR_PUT_START
,
440 "put:(%X, %X)", qp
, mp
);
441 ASSERT(mp
->b_datap
->db_ref
!= 0);
442 ASSERT(mp
->b_next
== NULL
&& mp
->b_prev
== NULL
);
448 if (UseFastlocks
&& sq
->sq_ciputctrl
!= NULL
) {
450 ASSERT(sq
->sq_flags
& SQ_CIPUT
);
451 ix
= CPU
->cpu_seqid
& sq
->sq_nciputctrl
;
452 sqciplock
= &sq
->sq_ciputctrl
[ix
].ciputctrl_lock
;
453 sqcipcount
= &sq
->sq_ciputctrl
[ix
].ciputctrl_count
;
454 mutex_enter(sqciplock
);
455 if (!((*sqcipcount
) & SQ_FASTPUT
) ||
456 (sq
->sq_flags
& (SQ_STAYAWAY
|SQ_EXCL
|SQ_EVENTS
))) {
457 mutex_exit(sqciplock
);
462 ASSERT(*sqcipcount
!= 0);
463 queued
= qp
->q_sqflags
& Q_SQQUEUED
;
464 mutex_exit(sqciplock
);
467 ASSERT(sqciplock
== NULL
);
468 mutex_enter(SQLOCK(sq
));
469 flags
= sq
->sq_flags
;
471 * We are going to drop SQLOCK, so make a claim to prevent syncq
475 ASSERT(sq
->sq_count
!= 0); /* Wraparound */
477 * If there are writers or exclusive waiters, there is not much
478 * we can do. Place the message on the syncq and schedule a
479 * background thread to drain it.
481 * Also if we are approaching end of stack, fill the syncq and
482 * switch processing to a background thread - see comments on
485 if ((flags
& (SQ_STAYAWAY
|SQ_EXCL
|SQ_EVENTS
)) ||
486 (sq
->sq_needexcl
!= 0) || PUT_STACK_NOTENOUGH()) {
488 TRACE_3(TR_FAC_STREAMS_FR
, TR_PUTNEXT_END
,
489 "putnext_end:(%p, %p, %p) SQ_EXCL fill",
493 * NOTE: qfill_syncq will need QLOCK. It is safe to drop
494 * SQLOCK because positive sq_count keeps the syncq from
497 mutex_exit(SQLOCK(sq
));
499 qfill_syncq(sq
, qp
, mp
);
501 * NOTE: after the call to qfill_syncq() qp may be
502 * closed, both qp and sq should not be referenced at
505 * This ASSERT is located here to prevent stack frame
506 * consumption in the DEBUG code.
508 ASSERT(sqciplock
== NULL
);
512 queued
= qp
->q_sqflags
& Q_SQQUEUED
;
514 * If not a concurrent perimiter, we need to acquire
515 * it exclusively. It could not have been previously
516 * set since we held the SQLOCK before testing
517 * SQ_GOAWAY above (which includes SQ_EXCL).
518 * We do this here because we hold the SQLOCK, and need
519 * to make this state change BEFORE dropping it.
521 if (!(flags
& SQ_CIPUT
)) {
522 ASSERT((sq
->sq_flags
& SQ_EXCL
) == 0);
523 ASSERT(!(sq
->sq_type
& SQ_CIPUT
));
524 sq
->sq_flags
|= SQ_EXCL
;
526 mutex_exit(SQLOCK(sq
));
529 ASSERT((sq
->sq_flags
& (SQ_EXCL
|SQ_CIPUT
)));
530 ASSERT(MUTEX_NOT_HELD(SQLOCK(sq
)));
533 * We now have a claim on the syncq, we are either going to
534 * put the message on the syncq and then drain it, or we are
535 * going to call the putproc().
537 putproc
= qi
->qi_putp
;
539 STR_FTEVENT_MSG(mp
, fqp
, FTEV_PUTNEXT
, mp
->b_rptr
-
540 mp
->b_datap
->db_base
);
542 ASSERT(MUTEX_NOT_HELD(SQLOCK(sq
)));
543 ASSERT(MUTEX_NOT_HELD(QLOCK(qp
)));
545 mutex_enter(QLOCK(qp
));
547 * If there are no messages in front of us, just call putproc(),
548 * otherwise enqueue the message and drain the queue.
550 if (qp
->q_syncqmsgs
== 0) {
551 mutex_exit(QLOCK(qp
));
552 STR_FTEVENT_MSG(mp
, fqp
, FTEV_PUTNEXT
, mp
->b_rptr
-
553 mp
->b_datap
->db_base
);
555 ASSERT(MUTEX_NOT_HELD(SQLOCK(sq
)));
558 * We are doing a fill with the intent to
559 * drain (meaning we are filling because
560 * there are messages in front of us ane we
561 * need to preserve message ordering)
562 * Therefore, put the message on the queue
563 * and call qdrain_syncq (must be done with
566 STR_FTEVENT_MSG(mp
, fqp
, FTEV_PUTNEXT
,
567 mp
->b_rptr
- mp
->b_datap
->db_base
);
571 * These two values were in the original code for
572 * all syncq messages. This is unnecessary in
573 * the current implementation, but was retained
574 * in debug mode as it is usefull to know where
578 mp
->b_prev
= (mblk_t
*)putproc
;
581 qdrain_syncq(sq
, qp
);
582 ASSERT(MUTEX_NOT_HELD(QLOCK(qp
)));
586 * Before we release our claim, we need to see if any
587 * events were posted. If the syncq is SQ_EXCL && SQ_QUEUED,
588 * we were responsible for going exclusive and, therefore,
589 * are resposible for draining.
591 if (sq
->sq_flags
& (SQ_EXCL
)) {
594 drain_mask
= SQ_QUEUED
;
597 if (sqciplock
!= NULL
) {
598 mutex_enter(sqciplock
);
599 flags
= sq
->sq_flags
;
600 ASSERT(flags
& SQ_CIPUT
);
601 /* SQ_EXCL could have been set by qwriter_inner */
602 if ((flags
& (SQ_EXCL
|SQ_TAIL
)) || sq
->sq_needexcl
) {
604 * we need SQLOCK to handle
605 * wakeups/drains/flags change. sqciplock
606 * is needed to decrement sqcipcount.
607 * SQLOCK has to be grabbed before sqciplock
608 * for lock ordering purposes.
609 * after sqcipcount is decremented some lock
610 * still needs to be held to make sure
611 * syncq won't get freed on us.
613 * To prevent deadlocks we try to grab SQLOCK and if it
614 * is held already we drop sqciplock, acquire SQLOCK and
615 * reacqwire sqciplock again.
617 if (mutex_tryenter(SQLOCK(sq
)) == 0) {
618 mutex_exit(sqciplock
);
619 mutex_enter(SQLOCK(sq
));
620 mutex_enter(sqciplock
);
622 flags
= sq
->sq_flags
;
623 ASSERT(*sqcipcount
!= 0);
625 mutex_exit(sqciplock
);
627 ASSERT(*sqcipcount
!= 0);
629 mutex_exit(sqciplock
);
630 TRACE_3(TR_FAC_STREAMS_FR
, TR_PUTNEXT_END
,
631 "putnext_end:(%p, %p, %p) done", qp
, mp
, sq
);
635 mutex_enter(SQLOCK(sq
));
636 flags
= sq
->sq_flags
;
637 ASSERT(sq
->sq_count
!= 0);
640 if ((flags
& (SQ_TAIL
)) || sq
->sq_needexcl
) {
641 putnext_tail(sq
, qp
, (flags
& ~drain_mask
));
643 * The only purpose of this ASSERT is to preserve calling stack
649 ASSERT((sq
->sq_flags
& (SQ_EXCL
|SQ_CIPUT
)) || queued
);
650 ASSERT((flags
& (SQ_EXCL
|SQ_CIPUT
)) || queued
);
652 * Safe to always drop SQ_EXCL:
653 * Not SQ_CIPUT means we set SQ_EXCL above
654 * For SQ_CIPUT SQ_EXCL will only be set if the put
655 * procedure did a qwriter(INNER) in which case
656 * nobody else is in the inner perimeter and we
659 * I would like to make the following assertion:
661 * ASSERT((flags & (SQ_EXCL|SQ_CIPUT)) != (SQ_EXCL|SQ_CIPUT) ||
662 * sq->sq_count == 0);
664 * which indicates that if we are both putshared and exclusive,
665 * we became exclusive while executing the putproc, and the only
666 * claim on the syncq was the one we dropped a few lines above.
667 * But other threads that enter putnext while the syncq is exclusive
668 * need to make a claim as they may need to drop SQLOCK in the
669 * has_writers case to avoid deadlocks. If these threads are
670 * delayed or preempted, it is possible that the writer thread can
671 * find out that there are other claims making the (sq_count == 0)
675 sq
->sq_flags
= flags
& ~SQ_EXCL
;
676 mutex_exit(SQLOCK(sq
));
677 TRACE_3(TR_FAC_STREAMS_FR
, TR_PUTNEXT_END
,
678 "putnext_end:(%p, %p, %p) done", qp
, mp
, sq
);