10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*10934Ssommerfeld@sun.com * Common Development and Distribution License (the "License").
6*10934Ssommerfeld@sun.com * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*10934Ssommerfeld@sun.com * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
270Sstevel@tonic-gate /* All Rights Reserved */
280Sstevel@tonic-gate
290Sstevel@tonic-gate
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate * UNIX Device Driver Interface functions
320Sstevel@tonic-gate * This file contains the C-versions of putnext() and put().
330Sstevel@tonic-gate * Assembly language versions exist for some architectures.
340Sstevel@tonic-gate */
350Sstevel@tonic-gate
360Sstevel@tonic-gate #include <sys/types.h>
370Sstevel@tonic-gate #include <sys/systm.h>
380Sstevel@tonic-gate #include <sys/cpuvar.h>
390Sstevel@tonic-gate #include <sys/debug.h>
400Sstevel@tonic-gate #include <sys/t_lock.h>
410Sstevel@tonic-gate #include <sys/stream.h>
420Sstevel@tonic-gate #include <sys/thread.h>
430Sstevel@tonic-gate #include <sys/strsubr.h>
440Sstevel@tonic-gate #include <sys/ddi.h>
450Sstevel@tonic-gate #include <sys/vtrace.h>
460Sstevel@tonic-gate #include <sys/cmn_err.h>
470Sstevel@tonic-gate #include <sys/strft.h>
480Sstevel@tonic-gate #include <sys/stack.h>
490Sstevel@tonic-gate #include <sys/archsystm.h>
500Sstevel@tonic-gate
510Sstevel@tonic-gate /*
520Sstevel@tonic-gate * Streams with many modules may create long chains of calls via putnext() which
530Sstevel@tonic-gate * may exhaust stack space. When putnext detects that the stack space left is
540Sstevel@tonic-gate * too small (less then PUT_STACK_NEEDED), the call chain is broken and
550Sstevel@tonic-gate * further processing is delegated to the background thread via call to
560Sstevel@tonic-gate * putnext_tail(). Unfortunately there is no generic solution with fixed stack
570Sstevel@tonic-gate * size, and putnext() is recursive function, so this hack is a necessary evil.
580Sstevel@tonic-gate *
590Sstevel@tonic-gate * The redzone value is chosen dependent on the default stack size which is 8K
600Sstevel@tonic-gate * on 32-bit kernels and on x86 and 16K on 64-bit kernels. The values are chosen
61*10934Ssommerfeld@sun.com * empirically. For 64-bit kernels it is 5000 and for 32-bit kernels it is 3000.
62*10934Ssommerfeld@sun.com * Experiments showed that 2500 is not enough for either 32-bit or 64-bit
63*10934Ssommerfeld@sun.com * kernels.
640Sstevel@tonic-gate *
650Sstevel@tonic-gate * The redzone value is a tuneable rather then a constant to allow adjustments
660Sstevel@tonic-gate * in the field.
670Sstevel@tonic-gate *
680Sstevel@tonic-gate * The check in PUT_STACK_NOTENOUGH is taken from segkp_map_red() function. It
690Sstevel@tonic-gate * is possible to define it as a generic function exported by seg_kp, but
700Sstevel@tonic-gate *
710Sstevel@tonic-gate * a) It may sound like an open invitation to use the facility indiscriminately.
720Sstevel@tonic-gate * b) It adds extra function call in putnext path.
730Sstevel@tonic-gate *
740Sstevel@tonic-gate * We keep a global counter `put_stack_notenough' which keeps track how many
750Sstevel@tonic-gate * times the stack switching hack was used.
760Sstevel@tonic-gate */
770Sstevel@tonic-gate
780Sstevel@tonic-gate static ulong_t put_stack_notenough;
790Sstevel@tonic-gate
800Sstevel@tonic-gate #ifdef _LP64
810Sstevel@tonic-gate #define PUT_STACK_NEEDED 5000
820Sstevel@tonic-gate #else
83*10934Ssommerfeld@sun.com #define PUT_STACK_NEEDED 3000
840Sstevel@tonic-gate #endif
850Sstevel@tonic-gate
860Sstevel@tonic-gate int put_stack_needed = PUT_STACK_NEEDED;
870Sstevel@tonic-gate
880Sstevel@tonic-gate #if defined(STACK_GROWTH_DOWN)
890Sstevel@tonic-gate #define PUT_STACK_NOTENOUGH() \
900Sstevel@tonic-gate (((STACK_BIAS + (uintptr_t)getfp() - \
910Sstevel@tonic-gate (uintptr_t)curthread->t_stkbase) < put_stack_needed) && \
920Sstevel@tonic-gate ++put_stack_notenough)
930Sstevel@tonic-gate #else
940Sstevel@tonic-gate #error "STACK_GROWTH_DOWN undefined"
950Sstevel@tonic-gate #endif
960Sstevel@tonic-gate
970Sstevel@tonic-gate boolean_t UseFastlocks = B_FALSE;
980Sstevel@tonic-gate
990Sstevel@tonic-gate /*
1000Sstevel@tonic-gate * function: putnext()
1010Sstevel@tonic-gate * purpose: call the put routine of the queue linked to qp
1020Sstevel@tonic-gate *
1030Sstevel@tonic-gate * Note: this function is written to perform well on modern computer
1040Sstevel@tonic-gate * architectures by e.g. preloading values into registers and "smearing" out
1050Sstevel@tonic-gate * code.
1060Sstevel@tonic-gate *
1070Sstevel@tonic-gate * A note on the fastput mechanism. The most significant bit of a
1080Sstevel@tonic-gate * putcount is considered the "FASTPUT" bit. If set, then there is
1090Sstevel@tonic-gate * nothing stoping a concurrent put from occuring (note that putcounts
1100Sstevel@tonic-gate * are only allowed on CIPUT perimiters). If, however, it is cleared,
1110Sstevel@tonic-gate * then we need to take the normal lock path by aquiring the SQLOCK.
1120Sstevel@tonic-gate * This is a slowlock. When a thread starts exclusiveness, e.g. wants
1130Sstevel@tonic-gate * writer access, it will clear the FASTPUT bit, causing new threads
1140Sstevel@tonic-gate * to take the slowlock path. This assures that putcounts will not
1150Sstevel@tonic-gate * increase in value, so the want-writer does not need to constantly
1160Sstevel@tonic-gate * aquire the putlocks to sum the putcounts. This does have the
1170Sstevel@tonic-gate * possibility of having the count drop right after reading, but that
1180Sstevel@tonic-gate * is no different than aquiring, reading and then releasing. However,
1190Sstevel@tonic-gate * in this mode, it cannot go up, so eventually they will drop to zero
1200Sstevel@tonic-gate * and the want-writer can proceed.
1210Sstevel@tonic-gate *
1220Sstevel@tonic-gate * If the FASTPUT bit is set, or in the slowlock path we see that there
1230Sstevel@tonic-gate * are no writers or want-writers, we make the choice of calling the
1240Sstevel@tonic-gate * putproc, or a "fast-fill_syncq". The fast-fill is a fill with
1250Sstevel@tonic-gate * immediate intention to drain. This is done because there are
1260Sstevel@tonic-gate * messages already at the queue waiting to drain. To preserve message
1270Sstevel@tonic-gate * ordering, we need to put this message at the end, and pickup the
1280Sstevel@tonic-gate * messages at the beginning. We call the macro that actually
1290Sstevel@tonic-gate * enqueues the message on the queue, and then call qdrain_syncq. If
1300Sstevel@tonic-gate * there is already a drainer, we just return. We could make that
1310Sstevel@tonic-gate * check before calling qdrain_syncq, but it is a little more clear
1320Sstevel@tonic-gate * to have qdrain_syncq do this (we might try the above optimization
1330Sstevel@tonic-gate * as this behavior evolves). qdrain_syncq assumes that SQ_EXCL is set
1340Sstevel@tonic-gate * already if this is a non-CIPUT perimiter, and that an appropriate
1350Sstevel@tonic-gate * claim has been made. So we do all that work before dropping the
1360Sstevel@tonic-gate * SQLOCK with our claim.
1370Sstevel@tonic-gate *
1380Sstevel@tonic-gate * If we cannot proceed with the putproc/fast-fill, we just fall
1390Sstevel@tonic-gate * through to the qfill_syncq, and then tail processing. If state
1400Sstevel@tonic-gate * has changed in that cycle, or wakeups are needed, it will occur
1410Sstevel@tonic-gate * there.
1420Sstevel@tonic-gate */
1430Sstevel@tonic-gate void
putnext(queue_t * qp,mblk_t * mp)1440Sstevel@tonic-gate putnext(queue_t *qp, mblk_t *mp)
1450Sstevel@tonic-gate {
1460Sstevel@tonic-gate queue_t *fqp = qp; /* For strft tracing */
1470Sstevel@tonic-gate syncq_t *sq;
1480Sstevel@tonic-gate uint16_t flags;
1490Sstevel@tonic-gate uint16_t drain_mask;
1500Sstevel@tonic-gate struct qinit *qi;
1510Sstevel@tonic-gate int (*putproc)();
1520Sstevel@tonic-gate struct stdata *stp;
1530Sstevel@tonic-gate int ix;
1540Sstevel@tonic-gate boolean_t queued = B_FALSE;
1550Sstevel@tonic-gate kmutex_t *sdlock = NULL;
1560Sstevel@tonic-gate kmutex_t *sqciplock = NULL;
1570Sstevel@tonic-gate ushort_t *sqcipcount = NULL;
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate TRACE_2(TR_FAC_STREAMS_FR, TR_PUTNEXT_START,
160*10934Ssommerfeld@sun.com "putnext_start:(%p, %p)", qp, mp);
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate ASSERT(mp->b_datap->db_ref != 0);
1630Sstevel@tonic-gate ASSERT(mp->b_next == NULL && mp->b_prev == NULL);
1640Sstevel@tonic-gate stp = STREAM(qp);
1650Sstevel@tonic-gate ASSERT(stp != NULL);
1660Sstevel@tonic-gate if (stp->sd_ciputctrl != NULL) {
1670Sstevel@tonic-gate ix = CPU->cpu_seqid & stp->sd_nciputctrl;
1680Sstevel@tonic-gate sdlock = &stp->sd_ciputctrl[ix].ciputctrl_lock;
1690Sstevel@tonic-gate mutex_enter(sdlock);
1700Sstevel@tonic-gate } else {
1710Sstevel@tonic-gate mutex_enter(sdlock = &stp->sd_lock);
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate qp = qp->q_next;
1740Sstevel@tonic-gate sq = qp->q_syncq;
1750Sstevel@tonic-gate ASSERT(sq != NULL);
1760Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(SQLOCK(sq)));
1770Sstevel@tonic-gate qi = qp->q_qinfo;
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate if (sq->sq_ciputctrl != NULL) {
1800Sstevel@tonic-gate /* fastlock: */
1810Sstevel@tonic-gate ASSERT(sq->sq_flags & SQ_CIPUT);
1820Sstevel@tonic-gate ix = CPU->cpu_seqid & sq->sq_nciputctrl;
1830Sstevel@tonic-gate sqciplock = &sq->sq_ciputctrl[ix].ciputctrl_lock;
1840Sstevel@tonic-gate sqcipcount = &sq->sq_ciputctrl[ix].ciputctrl_count;
1850Sstevel@tonic-gate mutex_enter(sqciplock);
1860Sstevel@tonic-gate if (!((*sqcipcount) & SQ_FASTPUT) ||
1870Sstevel@tonic-gate (sq->sq_flags & (SQ_STAYAWAY|SQ_EXCL|SQ_EVENTS))) {
1880Sstevel@tonic-gate mutex_exit(sqciplock);
1890Sstevel@tonic-gate sqciplock = NULL;
1900Sstevel@tonic-gate goto slowlock;
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate mutex_exit(sdlock);
1930Sstevel@tonic-gate (*sqcipcount)++;
1940Sstevel@tonic-gate ASSERT(*sqcipcount != 0);
1950Sstevel@tonic-gate queued = qp->q_sqflags & Q_SQQUEUED;
1960Sstevel@tonic-gate mutex_exit(sqciplock);
1970Sstevel@tonic-gate } else {
198*10934Ssommerfeld@sun.com slowlock:
1990Sstevel@tonic-gate ASSERT(sqciplock == NULL);
2000Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
2010Sstevel@tonic-gate mutex_exit(sdlock);
2020Sstevel@tonic-gate flags = sq->sq_flags;
2030Sstevel@tonic-gate /*
2040Sstevel@tonic-gate * We are going to drop SQLOCK, so make a claim to prevent syncq
2050Sstevel@tonic-gate * from closing.
2060Sstevel@tonic-gate */
2070Sstevel@tonic-gate sq->sq_count++;
2080Sstevel@tonic-gate ASSERT(sq->sq_count != 0); /* Wraparound */
2090Sstevel@tonic-gate /*
2100Sstevel@tonic-gate * If there are writers or exclusive waiters, there is not much
2110Sstevel@tonic-gate * we can do. Place the message on the syncq and schedule a
2120Sstevel@tonic-gate * background thread to drain it.
2130Sstevel@tonic-gate *
2140Sstevel@tonic-gate * Also if we are approaching end of stack, fill the syncq and
2150Sstevel@tonic-gate * switch processing to a background thread - see comments on
2160Sstevel@tonic-gate * top.
2170Sstevel@tonic-gate */
2180Sstevel@tonic-gate if ((flags & (SQ_STAYAWAY|SQ_EXCL|SQ_EVENTS)) ||
2190Sstevel@tonic-gate (sq->sq_needexcl != 0) || PUT_STACK_NOTENOUGH()) {
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate TRACE_3(TR_FAC_STREAMS_FR, TR_PUTNEXT_END,
2220Sstevel@tonic-gate "putnext_end:(%p, %p, %p) SQ_EXCL fill",
2230Sstevel@tonic-gate qp, mp, sq);
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate /*
2260Sstevel@tonic-gate * NOTE: qfill_syncq will need QLOCK. It is safe to drop
2270Sstevel@tonic-gate * SQLOCK because positive sq_count keeps the syncq from
2280Sstevel@tonic-gate * closing.
2290Sstevel@tonic-gate */
2300Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate qfill_syncq(sq, qp, mp);
2330Sstevel@tonic-gate /*
2340Sstevel@tonic-gate * NOTE: after the call to qfill_syncq() qp may be
2350Sstevel@tonic-gate * closed, both qp and sq should not be referenced at
2360Sstevel@tonic-gate * this point.
2370Sstevel@tonic-gate *
2380Sstevel@tonic-gate * This ASSERT is located here to prevent stack frame
2390Sstevel@tonic-gate * consumption in the DEBUG code.
2400Sstevel@tonic-gate */
2410Sstevel@tonic-gate ASSERT(sqciplock == NULL);
2420Sstevel@tonic-gate return;
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate queued = qp->q_sqflags & Q_SQQUEUED;
2460Sstevel@tonic-gate /*
2470Sstevel@tonic-gate * If not a concurrent perimiter, we need to acquire
2480Sstevel@tonic-gate * it exclusively. It could not have been previously
2490Sstevel@tonic-gate * set since we held the SQLOCK before testing
2500Sstevel@tonic-gate * SQ_GOAWAY above (which includes SQ_EXCL).
2510Sstevel@tonic-gate * We do this here because we hold the SQLOCK, and need
2520Sstevel@tonic-gate * to make this state change BEFORE dropping it.
2530Sstevel@tonic-gate */
2540Sstevel@tonic-gate if (!(flags & SQ_CIPUT)) {
2550Sstevel@tonic-gate ASSERT((sq->sq_flags & SQ_EXCL) == 0);
2560Sstevel@tonic-gate ASSERT(!(sq->sq_type & SQ_CIPUT));
2570Sstevel@tonic-gate sq->sq_flags |= SQ_EXCL;
2580Sstevel@tonic-gate }
2590Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate
2620Sstevel@tonic-gate ASSERT((sq->sq_flags & (SQ_EXCL|SQ_CIPUT)));
2630Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(SQLOCK(sq)));
2640Sstevel@tonic-gate
2650Sstevel@tonic-gate /*
2660Sstevel@tonic-gate * We now have a claim on the syncq, we are either going to
2670Sstevel@tonic-gate * put the message on the syncq and then drain it, or we are
2680Sstevel@tonic-gate * going to call the putproc().
2690Sstevel@tonic-gate */
2700Sstevel@tonic-gate putproc = qi->qi_putp;
2710Sstevel@tonic-gate if (!queued) {
2720Sstevel@tonic-gate STR_FTEVENT_MSG(mp, fqp, FTEV_PUTNEXT, mp->b_rptr -
2730Sstevel@tonic-gate mp->b_datap->db_base);
2740Sstevel@tonic-gate (*putproc)(qp, mp);
2750Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(SQLOCK(sq)));
2760Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(QLOCK(qp)));
2770Sstevel@tonic-gate } else {
2780Sstevel@tonic-gate mutex_enter(QLOCK(qp));
2790Sstevel@tonic-gate /*
2800Sstevel@tonic-gate * If there are no messages in front of us, just call putproc(),
2810Sstevel@tonic-gate * otherwise enqueue the message and drain the queue.
2820Sstevel@tonic-gate */
2830Sstevel@tonic-gate if (qp->q_syncqmsgs == 0) {
2840Sstevel@tonic-gate mutex_exit(QLOCK(qp));
2850Sstevel@tonic-gate STR_FTEVENT_MSG(mp, fqp, FTEV_PUTNEXT, mp->b_rptr -
2860Sstevel@tonic-gate mp->b_datap->db_base);
2870Sstevel@tonic-gate (*putproc)(qp, mp);
2880Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(SQLOCK(sq)));
2890Sstevel@tonic-gate } else {
2900Sstevel@tonic-gate /*
2910Sstevel@tonic-gate * We are doing a fill with the intent to
2920Sstevel@tonic-gate * drain (meaning we are filling because
2930Sstevel@tonic-gate * there are messages in front of us ane we
2940Sstevel@tonic-gate * need to preserve message ordering)
2950Sstevel@tonic-gate * Therefore, put the message on the queue
2960Sstevel@tonic-gate * and call qdrain_syncq (must be done with
2970Sstevel@tonic-gate * the QLOCK held).
2980Sstevel@tonic-gate */
2990Sstevel@tonic-gate STR_FTEVENT_MSG(mp, fqp, FTEV_PUTNEXT,
3000Sstevel@tonic-gate mp->b_rptr - mp->b_datap->db_base);
3010Sstevel@tonic-gate
3020Sstevel@tonic-gate #ifdef DEBUG
3030Sstevel@tonic-gate /*
3040Sstevel@tonic-gate * These two values were in the original code for
3050Sstevel@tonic-gate * all syncq messages. This is unnecessary in
3060Sstevel@tonic-gate * the current implementation, but was retained
3070Sstevel@tonic-gate * in debug mode as it is usefull to know where
3080Sstevel@tonic-gate * problems occur.
3090Sstevel@tonic-gate */
3100Sstevel@tonic-gate mp->b_queue = qp;
3110Sstevel@tonic-gate mp->b_prev = (mblk_t *)putproc;
3120Sstevel@tonic-gate #endif
3130Sstevel@tonic-gate SQPUT_MP(qp, mp);
3140Sstevel@tonic-gate qdrain_syncq(sq, qp);
3150Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(QLOCK(qp)));
3160Sstevel@tonic-gate }
3170Sstevel@tonic-gate }
3180Sstevel@tonic-gate /*
3190Sstevel@tonic-gate * Before we release our claim, we need to see if any
3200Sstevel@tonic-gate * events were posted. If the syncq is SQ_EXCL && SQ_QUEUED,
3210Sstevel@tonic-gate * we were responsible for going exclusive and, therefore,
3220Sstevel@tonic-gate * are resposible for draining.
3230Sstevel@tonic-gate */
3240Sstevel@tonic-gate if (sq->sq_flags & (SQ_EXCL)) {
3250Sstevel@tonic-gate drain_mask = 0;
3260Sstevel@tonic-gate } else {
3270Sstevel@tonic-gate drain_mask = SQ_QUEUED;
3280Sstevel@tonic-gate }
3290Sstevel@tonic-gate
3300Sstevel@tonic-gate if (sqciplock != NULL) {
3310Sstevel@tonic-gate mutex_enter(sqciplock);
3320Sstevel@tonic-gate flags = sq->sq_flags;
3330Sstevel@tonic-gate ASSERT(flags & SQ_CIPUT);
3340Sstevel@tonic-gate /* SQ_EXCL could have been set by qwriter_inner */
3350Sstevel@tonic-gate if ((flags & (SQ_EXCL|SQ_TAIL)) || sq->sq_needexcl) {
3360Sstevel@tonic-gate /*
3370Sstevel@tonic-gate * we need SQLOCK to handle
3380Sstevel@tonic-gate * wakeups/drains/flags change. sqciplock
3390Sstevel@tonic-gate * is needed to decrement sqcipcount.
3400Sstevel@tonic-gate * SQLOCK has to be grabbed before sqciplock
3410Sstevel@tonic-gate * for lock ordering purposes.
3420Sstevel@tonic-gate * after sqcipcount is decremented some lock
3430Sstevel@tonic-gate * still needs to be held to make sure
3440Sstevel@tonic-gate * syncq won't get freed on us.
3450Sstevel@tonic-gate *
3460Sstevel@tonic-gate * To prevent deadlocks we try to grab SQLOCK and if it
3470Sstevel@tonic-gate * is held already we drop sqciplock, acquire SQLOCK and
3480Sstevel@tonic-gate * reacqwire sqciplock again.
3490Sstevel@tonic-gate */
3500Sstevel@tonic-gate if (mutex_tryenter(SQLOCK(sq)) == 0) {
3510Sstevel@tonic-gate mutex_exit(sqciplock);
3520Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
3530Sstevel@tonic-gate mutex_enter(sqciplock);
3540Sstevel@tonic-gate }
3550Sstevel@tonic-gate flags = sq->sq_flags;
3560Sstevel@tonic-gate ASSERT(*sqcipcount != 0);
3570Sstevel@tonic-gate (*sqcipcount)--;
3580Sstevel@tonic-gate mutex_exit(sqciplock);
3590Sstevel@tonic-gate } else {
3600Sstevel@tonic-gate ASSERT(*sqcipcount != 0);
3610Sstevel@tonic-gate (*sqcipcount)--;
3620Sstevel@tonic-gate mutex_exit(sqciplock);
3630Sstevel@tonic-gate TRACE_3(TR_FAC_STREAMS_FR, TR_PUTNEXT_END,
3640Sstevel@tonic-gate "putnext_end:(%p, %p, %p) done", qp, mp, sq);
3650Sstevel@tonic-gate return;
3660Sstevel@tonic-gate }
3670Sstevel@tonic-gate } else {
3680Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
3690Sstevel@tonic-gate flags = sq->sq_flags;
3700Sstevel@tonic-gate ASSERT(sq->sq_count != 0);
3710Sstevel@tonic-gate sq->sq_count--;
3720Sstevel@tonic-gate }
3730Sstevel@tonic-gate if ((flags & (SQ_TAIL)) || sq->sq_needexcl) {
3740Sstevel@tonic-gate putnext_tail(sq, qp, (flags & ~drain_mask));
3750Sstevel@tonic-gate /*
3760Sstevel@tonic-gate * The only purpose of this ASSERT is to preserve calling stack
3770Sstevel@tonic-gate * in DEBUG kernel.
3780Sstevel@tonic-gate */
3790Sstevel@tonic-gate ASSERT(sq != NULL);
3800Sstevel@tonic-gate return;
3810Sstevel@tonic-gate }
3820Sstevel@tonic-gate ASSERT((sq->sq_flags & (SQ_EXCL|SQ_CIPUT)) || queued);
3830Sstevel@tonic-gate ASSERT((flags & (SQ_EXCL|SQ_CIPUT)) || queued);
3840Sstevel@tonic-gate /*
3850Sstevel@tonic-gate * Safe to always drop SQ_EXCL:
3860Sstevel@tonic-gate * Not SQ_CIPUT means we set SQ_EXCL above
3870Sstevel@tonic-gate * For SQ_CIPUT SQ_EXCL will only be set if the put
3880Sstevel@tonic-gate * procedure did a qwriter(INNER) in which case
3890Sstevel@tonic-gate * nobody else is in the inner perimeter and we
3900Sstevel@tonic-gate * are exiting.
3910Sstevel@tonic-gate *
3920Sstevel@tonic-gate * I would like to make the following assertion:
3930Sstevel@tonic-gate *
3940Sstevel@tonic-gate * ASSERT((flags & (SQ_EXCL|SQ_CIPUT)) != (SQ_EXCL|SQ_CIPUT) ||
3950Sstevel@tonic-gate * sq->sq_count == 0);
3960Sstevel@tonic-gate *
3970Sstevel@tonic-gate * which indicates that if we are both putshared and exclusive,
3980Sstevel@tonic-gate * we became exclusive while executing the putproc, and the only
3990Sstevel@tonic-gate * claim on the syncq was the one we dropped a few lines above.
4000Sstevel@tonic-gate * But other threads that enter putnext while the syncq is exclusive
4010Sstevel@tonic-gate * need to make a claim as they may need to drop SQLOCK in the
4020Sstevel@tonic-gate * has_writers case to avoid deadlocks. If these threads are
4030Sstevel@tonic-gate * delayed or preempted, it is possible that the writer thread can
4040Sstevel@tonic-gate * find out that there are other claims making the (sq_count == 0)
4050Sstevel@tonic-gate * test invalid.
4060Sstevel@tonic-gate */
4070Sstevel@tonic-gate
4080Sstevel@tonic-gate sq->sq_flags = flags & ~SQ_EXCL;
4090Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
4100Sstevel@tonic-gate TRACE_3(TR_FAC_STREAMS_FR, TR_PUTNEXT_END,
4110Sstevel@tonic-gate "putnext_end:(%p, %p, %p) done", qp, mp, sq);
4120Sstevel@tonic-gate }
4130Sstevel@tonic-gate
4140Sstevel@tonic-gate
4150Sstevel@tonic-gate /*
4160Sstevel@tonic-gate * wrapper for qi_putp entry in module ops vec.
4170Sstevel@tonic-gate * implements asynchronous putnext().
4180Sstevel@tonic-gate * Note, that unlike putnext(), this routine is NOT optimized for the
4190Sstevel@tonic-gate * fastpath. Calling this routine will grab whatever locks are necessary
4200Sstevel@tonic-gate * to protect the stream head, q_next, and syncq's.
4210Sstevel@tonic-gate * And since it is in the normal locks path, we do not use putlocks if
4220Sstevel@tonic-gate * they exist (though this can be changed by swapping the value of
4230Sstevel@tonic-gate * UseFastlocks).
4240Sstevel@tonic-gate */
4250Sstevel@tonic-gate void
put(queue_t * qp,mblk_t * mp)4260Sstevel@tonic-gate put(queue_t *qp, mblk_t *mp)
4270Sstevel@tonic-gate {
4280Sstevel@tonic-gate queue_t *fqp = qp; /* For strft tracing */
4290Sstevel@tonic-gate syncq_t *sq;
4300Sstevel@tonic-gate uint16_t flags;
4310Sstevel@tonic-gate uint16_t drain_mask;
4320Sstevel@tonic-gate struct qinit *qi;
4330Sstevel@tonic-gate int (*putproc)();
4340Sstevel@tonic-gate int ix;
4350Sstevel@tonic-gate boolean_t queued = B_FALSE;
4360Sstevel@tonic-gate kmutex_t *sqciplock = NULL;
4370Sstevel@tonic-gate ushort_t *sqcipcount = NULL;
4380Sstevel@tonic-gate
4390Sstevel@tonic-gate TRACE_2(TR_FAC_STREAMS_FR, TR_PUT_START,
440*10934Ssommerfeld@sun.com "put:(%X, %X)", qp, mp);
4410Sstevel@tonic-gate ASSERT(mp->b_datap->db_ref != 0);
4420Sstevel@tonic-gate ASSERT(mp->b_next == NULL && mp->b_prev == NULL);
4430Sstevel@tonic-gate
4440Sstevel@tonic-gate sq = qp->q_syncq;
4450Sstevel@tonic-gate ASSERT(sq != NULL);
4460Sstevel@tonic-gate qi = qp->q_qinfo;
4470Sstevel@tonic-gate
4480Sstevel@tonic-gate if (UseFastlocks && sq->sq_ciputctrl != NULL) {
4490Sstevel@tonic-gate /* fastlock: */
4500Sstevel@tonic-gate ASSERT(sq->sq_flags & SQ_CIPUT);
4510Sstevel@tonic-gate ix = CPU->cpu_seqid & sq->sq_nciputctrl;
4520Sstevel@tonic-gate sqciplock = &sq->sq_ciputctrl[ix].ciputctrl_lock;
4530Sstevel@tonic-gate sqcipcount = &sq->sq_ciputctrl[ix].ciputctrl_count;
4540Sstevel@tonic-gate mutex_enter(sqciplock);
4550Sstevel@tonic-gate if (!((*sqcipcount) & SQ_FASTPUT) ||
4560Sstevel@tonic-gate (sq->sq_flags & (SQ_STAYAWAY|SQ_EXCL|SQ_EVENTS))) {
4570Sstevel@tonic-gate mutex_exit(sqciplock);
4580Sstevel@tonic-gate sqciplock = NULL;
4590Sstevel@tonic-gate goto slowlock;
4600Sstevel@tonic-gate }
4610Sstevel@tonic-gate (*sqcipcount)++;
4620Sstevel@tonic-gate ASSERT(*sqcipcount != 0);
4630Sstevel@tonic-gate queued = qp->q_sqflags & Q_SQQUEUED;
4640Sstevel@tonic-gate mutex_exit(sqciplock);
4650Sstevel@tonic-gate } else {
466*10934Ssommerfeld@sun.com slowlock:
4670Sstevel@tonic-gate ASSERT(sqciplock == NULL);
4680Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
4690Sstevel@tonic-gate flags = sq->sq_flags;
4700Sstevel@tonic-gate /*
4710Sstevel@tonic-gate * We are going to drop SQLOCK, so make a claim to prevent syncq
4720Sstevel@tonic-gate * from closing.
4730Sstevel@tonic-gate */
4740Sstevel@tonic-gate sq->sq_count++;
4750Sstevel@tonic-gate ASSERT(sq->sq_count != 0); /* Wraparound */
4760Sstevel@tonic-gate /*
4770Sstevel@tonic-gate * If there are writers or exclusive waiters, there is not much
4780Sstevel@tonic-gate * we can do. Place the message on the syncq and schedule a
4790Sstevel@tonic-gate * background thread to drain it.
4800Sstevel@tonic-gate *
4810Sstevel@tonic-gate * Also if we are approaching end of stack, fill the syncq and
4820Sstevel@tonic-gate * switch processing to a background thread - see comments on
4830Sstevel@tonic-gate * top.
4840Sstevel@tonic-gate */
4850Sstevel@tonic-gate if ((flags & (SQ_STAYAWAY|SQ_EXCL|SQ_EVENTS)) ||
4860Sstevel@tonic-gate (sq->sq_needexcl != 0) || PUT_STACK_NOTENOUGH()) {
4870Sstevel@tonic-gate
4880Sstevel@tonic-gate TRACE_3(TR_FAC_STREAMS_FR, TR_PUTNEXT_END,
4890Sstevel@tonic-gate "putnext_end:(%p, %p, %p) SQ_EXCL fill",
4900Sstevel@tonic-gate qp, mp, sq);
4910Sstevel@tonic-gate
4920Sstevel@tonic-gate /*
4930Sstevel@tonic-gate * NOTE: qfill_syncq will need QLOCK. It is safe to drop
4940Sstevel@tonic-gate * SQLOCK because positive sq_count keeps the syncq from
4950Sstevel@tonic-gate * closing.
4960Sstevel@tonic-gate */
4970Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
4980Sstevel@tonic-gate
4990Sstevel@tonic-gate qfill_syncq(sq, qp, mp);
5000Sstevel@tonic-gate /*
5010Sstevel@tonic-gate * NOTE: after the call to qfill_syncq() qp may be
5020Sstevel@tonic-gate * closed, both qp and sq should not be referenced at
5030Sstevel@tonic-gate * this point.
5040Sstevel@tonic-gate *
5050Sstevel@tonic-gate * This ASSERT is located here to prevent stack frame
5060Sstevel@tonic-gate * consumption in the DEBUG code.
5070Sstevel@tonic-gate */
5080Sstevel@tonic-gate ASSERT(sqciplock == NULL);
5090Sstevel@tonic-gate return;
5100Sstevel@tonic-gate }
5110Sstevel@tonic-gate
5120Sstevel@tonic-gate queued = qp->q_sqflags & Q_SQQUEUED;
5130Sstevel@tonic-gate /*
5140Sstevel@tonic-gate * If not a concurrent perimiter, we need to acquire
5150Sstevel@tonic-gate * it exclusively. It could not have been previously
5160Sstevel@tonic-gate * set since we held the SQLOCK before testing
5170Sstevel@tonic-gate * SQ_GOAWAY above (which includes SQ_EXCL).
5180Sstevel@tonic-gate * We do this here because we hold the SQLOCK, and need
5190Sstevel@tonic-gate * to make this state change BEFORE dropping it.
5200Sstevel@tonic-gate */
5210Sstevel@tonic-gate if (!(flags & SQ_CIPUT)) {
5220Sstevel@tonic-gate ASSERT((sq->sq_flags & SQ_EXCL) == 0);
5230Sstevel@tonic-gate ASSERT(!(sq->sq_type & SQ_CIPUT));
5240Sstevel@tonic-gate sq->sq_flags |= SQ_EXCL;
5250Sstevel@tonic-gate }
5260Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
5270Sstevel@tonic-gate }
5280Sstevel@tonic-gate
5290Sstevel@tonic-gate ASSERT((sq->sq_flags & (SQ_EXCL|SQ_CIPUT)));
5300Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(SQLOCK(sq)));
5310Sstevel@tonic-gate
5320Sstevel@tonic-gate /*
5330Sstevel@tonic-gate * We now have a claim on the syncq, we are either going to
5340Sstevel@tonic-gate * put the message on the syncq and then drain it, or we are
5350Sstevel@tonic-gate * going to call the putproc().
5360Sstevel@tonic-gate */
5370Sstevel@tonic-gate putproc = qi->qi_putp;
5380Sstevel@tonic-gate if (!queued) {
5390Sstevel@tonic-gate STR_FTEVENT_MSG(mp, fqp, FTEV_PUTNEXT, mp->b_rptr -
5400Sstevel@tonic-gate mp->b_datap->db_base);
5410Sstevel@tonic-gate (*putproc)(qp, mp);
5420Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(SQLOCK(sq)));
5430Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(QLOCK(qp)));
5440Sstevel@tonic-gate } else {
5450Sstevel@tonic-gate mutex_enter(QLOCK(qp));
5460Sstevel@tonic-gate /*
5470Sstevel@tonic-gate * If there are no messages in front of us, just call putproc(),
5480Sstevel@tonic-gate * otherwise enqueue the message and drain the queue.
5490Sstevel@tonic-gate */
5500Sstevel@tonic-gate if (qp->q_syncqmsgs == 0) {
5510Sstevel@tonic-gate mutex_exit(QLOCK(qp));
5520Sstevel@tonic-gate STR_FTEVENT_MSG(mp, fqp, FTEV_PUTNEXT, mp->b_rptr -
5530Sstevel@tonic-gate mp->b_datap->db_base);
5540Sstevel@tonic-gate (*putproc)(qp, mp);
5550Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(SQLOCK(sq)));
5560Sstevel@tonic-gate } else {
5570Sstevel@tonic-gate /*
5580Sstevel@tonic-gate * We are doing a fill with the intent to
5590Sstevel@tonic-gate * drain (meaning we are filling because
5600Sstevel@tonic-gate * there are messages in front of us ane we
5610Sstevel@tonic-gate * need to preserve message ordering)
5620Sstevel@tonic-gate * Therefore, put the message on the queue
5630Sstevel@tonic-gate * and call qdrain_syncq (must be done with
5640Sstevel@tonic-gate * the QLOCK held).
5650Sstevel@tonic-gate */
5660Sstevel@tonic-gate STR_FTEVENT_MSG(mp, fqp, FTEV_PUTNEXT,
5670Sstevel@tonic-gate mp->b_rptr - mp->b_datap->db_base);
5680Sstevel@tonic-gate
5690Sstevel@tonic-gate #ifdef DEBUG
5700Sstevel@tonic-gate /*
5710Sstevel@tonic-gate * These two values were in the original code for
5720Sstevel@tonic-gate * all syncq messages. This is unnecessary in
5730Sstevel@tonic-gate * the current implementation, but was retained
5740Sstevel@tonic-gate * in debug mode as it is usefull to know where
5750Sstevel@tonic-gate * problems occur.
5760Sstevel@tonic-gate */
5770Sstevel@tonic-gate mp->b_queue = qp;
5780Sstevel@tonic-gate mp->b_prev = (mblk_t *)putproc;
5790Sstevel@tonic-gate #endif
5800Sstevel@tonic-gate SQPUT_MP(qp, mp);
5810Sstevel@tonic-gate qdrain_syncq(sq, qp);
5820Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(QLOCK(qp)));
5830Sstevel@tonic-gate }
5840Sstevel@tonic-gate }
5850Sstevel@tonic-gate /*
5860Sstevel@tonic-gate * Before we release our claim, we need to see if any
5870Sstevel@tonic-gate * events were posted. If the syncq is SQ_EXCL && SQ_QUEUED,
5880Sstevel@tonic-gate * we were responsible for going exclusive and, therefore,
5890Sstevel@tonic-gate * are resposible for draining.
5900Sstevel@tonic-gate */
5910Sstevel@tonic-gate if (sq->sq_flags & (SQ_EXCL)) {
5920Sstevel@tonic-gate drain_mask = 0;
5930Sstevel@tonic-gate } else {
5940Sstevel@tonic-gate drain_mask = SQ_QUEUED;
5950Sstevel@tonic-gate }
5960Sstevel@tonic-gate
5970Sstevel@tonic-gate if (sqciplock != NULL) {
5980Sstevel@tonic-gate mutex_enter(sqciplock);
5990Sstevel@tonic-gate flags = sq->sq_flags;
6000Sstevel@tonic-gate ASSERT(flags & SQ_CIPUT);
6010Sstevel@tonic-gate /* SQ_EXCL could have been set by qwriter_inner */
6020Sstevel@tonic-gate if ((flags & (SQ_EXCL|SQ_TAIL)) || sq->sq_needexcl) {
6030Sstevel@tonic-gate /*
6040Sstevel@tonic-gate * we need SQLOCK to handle
6050Sstevel@tonic-gate * wakeups/drains/flags change. sqciplock
6060Sstevel@tonic-gate * is needed to decrement sqcipcount.
6070Sstevel@tonic-gate * SQLOCK has to be grabbed before sqciplock
6080Sstevel@tonic-gate * for lock ordering purposes.
6090Sstevel@tonic-gate * after sqcipcount is decremented some lock
6100Sstevel@tonic-gate * still needs to be held to make sure
6110Sstevel@tonic-gate * syncq won't get freed on us.
6120Sstevel@tonic-gate *
6130Sstevel@tonic-gate * To prevent deadlocks we try to grab SQLOCK and if it
6140Sstevel@tonic-gate * is held already we drop sqciplock, acquire SQLOCK and
6150Sstevel@tonic-gate * reacqwire sqciplock again.
6160Sstevel@tonic-gate */
6170Sstevel@tonic-gate if (mutex_tryenter(SQLOCK(sq)) == 0) {
6180Sstevel@tonic-gate mutex_exit(sqciplock);
6190Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
6200Sstevel@tonic-gate mutex_enter(sqciplock);
6210Sstevel@tonic-gate }
6220Sstevel@tonic-gate flags = sq->sq_flags;
6230Sstevel@tonic-gate ASSERT(*sqcipcount != 0);
6240Sstevel@tonic-gate (*sqcipcount)--;
6250Sstevel@tonic-gate mutex_exit(sqciplock);
6260Sstevel@tonic-gate } else {
6270Sstevel@tonic-gate ASSERT(*sqcipcount != 0);
6280Sstevel@tonic-gate (*sqcipcount)--;
6290Sstevel@tonic-gate mutex_exit(sqciplock);
6300Sstevel@tonic-gate TRACE_3(TR_FAC_STREAMS_FR, TR_PUTNEXT_END,
6310Sstevel@tonic-gate "putnext_end:(%p, %p, %p) done", qp, mp, sq);
6320Sstevel@tonic-gate return;
6330Sstevel@tonic-gate }
6340Sstevel@tonic-gate } else {
6350Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
6360Sstevel@tonic-gate flags = sq->sq_flags;
6370Sstevel@tonic-gate ASSERT(sq->sq_count != 0);
6380Sstevel@tonic-gate sq->sq_count--;
6390Sstevel@tonic-gate }
6400Sstevel@tonic-gate if ((flags & (SQ_TAIL)) || sq->sq_needexcl) {
6410Sstevel@tonic-gate putnext_tail(sq, qp, (flags & ~drain_mask));
6420Sstevel@tonic-gate /*
6430Sstevel@tonic-gate * The only purpose of this ASSERT is to preserve calling stack
6440Sstevel@tonic-gate * in DEBUG kernel.
6450Sstevel@tonic-gate */
6460Sstevel@tonic-gate ASSERT(sq != NULL);
6470Sstevel@tonic-gate return;
6480Sstevel@tonic-gate }
6490Sstevel@tonic-gate ASSERT((sq->sq_flags & (SQ_EXCL|SQ_CIPUT)) || queued);
6500Sstevel@tonic-gate ASSERT((flags & (SQ_EXCL|SQ_CIPUT)) || queued);
6510Sstevel@tonic-gate /*
6520Sstevel@tonic-gate * Safe to always drop SQ_EXCL:
6530Sstevel@tonic-gate * Not SQ_CIPUT means we set SQ_EXCL above
6540Sstevel@tonic-gate * For SQ_CIPUT SQ_EXCL will only be set if the put
6550Sstevel@tonic-gate * procedure did a qwriter(INNER) in which case
6560Sstevel@tonic-gate * nobody else is in the inner perimeter and we
6570Sstevel@tonic-gate * are exiting.
6580Sstevel@tonic-gate *
6590Sstevel@tonic-gate * I would like to make the following assertion:
6600Sstevel@tonic-gate *
6610Sstevel@tonic-gate * ASSERT((flags & (SQ_EXCL|SQ_CIPUT)) != (SQ_EXCL|SQ_CIPUT) ||
6620Sstevel@tonic-gate * sq->sq_count == 0);
6630Sstevel@tonic-gate *
6640Sstevel@tonic-gate * which indicates that if we are both putshared and exclusive,
6650Sstevel@tonic-gate * we became exclusive while executing the putproc, and the only
6660Sstevel@tonic-gate * claim on the syncq was the one we dropped a few lines above.
6670Sstevel@tonic-gate * But other threads that enter putnext while the syncq is exclusive
6680Sstevel@tonic-gate * need to make a claim as they may need to drop SQLOCK in the
6690Sstevel@tonic-gate * has_writers case to avoid deadlocks. If these threads are
6700Sstevel@tonic-gate * delayed or preempted, it is possible that the writer thread can
6710Sstevel@tonic-gate * find out that there are other claims making the (sq_count == 0)
6720Sstevel@tonic-gate * test invalid.
6730Sstevel@tonic-gate */
6740Sstevel@tonic-gate
6750Sstevel@tonic-gate sq->sq_flags = flags & ~SQ_EXCL;
6760Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
6770Sstevel@tonic-gate TRACE_3(TR_FAC_STREAMS_FR, TR_PUTNEXT_END,
6780Sstevel@tonic-gate "putnext_end:(%p, %p, %p) done", qp, mp, sq);
6790Sstevel@tonic-gate }
680