2 * Copyright (c) 2000-2016 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1989, 1993, 1995
31 * The Regents of the University of California. All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)spec_vnops.c 8.14 (Berkeley) 5/21/95
64 #include <sys/param.h>
65 #include <sys/proc_internal.h>
66 #include <sys/kauth.h>
67 #include <sys/systm.h>
68 #include <sys/kernel.h>
70 #include <sys/buf_internal.h>
71 #include <sys/mount_internal.h>
72 #include <sys/vnode_internal.h>
73 #include <sys/file_internal.h>
74 #include <sys/namei.h>
76 #include <sys/errno.h>
77 #include <sys/ioctl.h>
80 #include <sys/malloc.h>
82 #include <sys/uio_internal.h>
83 #include <sys/resource.h>
84 #include <machine/machine_routines.h>
85 #include <miscfs/specfs/specdev.h>
86 #include <vfs/vfs_support.h>
87 #include <vfs/vfs_disk_conditioner.h>
89 #include <kern/assert.h>
90 #include <kern/task.h>
91 #include <kern/sched_prim.h>
92 #include <kern/thread.h>
93 #include <kern/policy_internal.h>
94 #include <kern/timer_call.h>
95 #include <kern/waitq.h>
97 #include <pexpert/pexpert.h>
99 #include <sys/kdebug.h>
100 #include <libkern/section_keywords.h>
102 /* XXX following three prototypes should be in a header file somewhere */
103 extern dev_t
chrtoblk(dev_t dev
);
104 extern boolean_t
iskmemdev(dev_t dev
);
105 extern int bpfkqfilter(dev_t dev
, struct knote
*kn
);
106 extern int ptsd_kqfilter(dev_t
, struct knote
*);
107 extern int ptmx_kqfilter(dev_t
, struct knote
*);
109 struct vnode
*speclisth
[SPECHSZ
];
111 /* symbolic sleep message strings for devices */
112 char devopn
[] = "devopn";
113 char devio
[] = "devio";
114 char devwait
[] = "devwait";
115 char devin
[] = "devin";
116 char devout
[] = "devout";
117 char devioc
[] = "devioc";
118 char devcls
[] = "devcls";
120 #define VOPFUNC int (*)(void *)
122 int(**spec_vnodeop_p
)(void *);
123 struct vnodeopv_entry_desc spec_vnodeop_entries
[] = {
124 { &vnop_default_desc
, (VOPFUNC
)vn_default_error
},
125 { &vnop_lookup_desc
, (VOPFUNC
)spec_lookup
}, /* lookup */
126 { &vnop_create_desc
, (VOPFUNC
)err_create
}, /* create */
127 { &vnop_mknod_desc
, (VOPFUNC
)err_mknod
}, /* mknod */
128 { &vnop_open_desc
, (VOPFUNC
)spec_open
}, /* open */
129 { &vnop_close_desc
, (VOPFUNC
)spec_close
}, /* close */
130 { &vnop_access_desc
, (VOPFUNC
)spec_access
}, /* access */
131 { &vnop_getattr_desc
, (VOPFUNC
)spec_getattr
}, /* getattr */
132 { &vnop_setattr_desc
, (VOPFUNC
)spec_setattr
}, /* setattr */
133 { &vnop_read_desc
, (VOPFUNC
)spec_read
}, /* read */
134 { &vnop_write_desc
, (VOPFUNC
)spec_write
}, /* write */
135 { &vnop_ioctl_desc
, (VOPFUNC
)spec_ioctl
}, /* ioctl */
136 { &vnop_select_desc
, (VOPFUNC
)spec_select
}, /* select */
137 { &vnop_revoke_desc
, (VOPFUNC
)nop_revoke
}, /* revoke */
138 { &vnop_mmap_desc
, (VOPFUNC
)err_mmap
}, /* mmap */
139 { &vnop_fsync_desc
, (VOPFUNC
)spec_fsync
}, /* fsync */
140 { &vnop_remove_desc
, (VOPFUNC
)err_remove
}, /* remove */
141 { &vnop_link_desc
, (VOPFUNC
)err_link
}, /* link */
142 { &vnop_rename_desc
, (VOPFUNC
)err_rename
}, /* rename */
143 { &vnop_mkdir_desc
, (VOPFUNC
)err_mkdir
}, /* mkdir */
144 { &vnop_rmdir_desc
, (VOPFUNC
)err_rmdir
}, /* rmdir */
145 { &vnop_symlink_desc
, (VOPFUNC
)err_symlink
}, /* symlink */
146 { &vnop_readdir_desc
, (VOPFUNC
)err_readdir
}, /* readdir */
147 { &vnop_readlink_desc
, (VOPFUNC
)err_readlink
}, /* readlink */
148 { &vnop_inactive_desc
, (VOPFUNC
)nop_inactive
}, /* inactive */
149 { &vnop_reclaim_desc
, (VOPFUNC
)nop_reclaim
}, /* reclaim */
150 { &vnop_strategy_desc
, (VOPFUNC
)spec_strategy
}, /* strategy */
151 { &vnop_pathconf_desc
, (VOPFUNC
)spec_pathconf
}, /* pathconf */
152 { &vnop_advlock_desc
, (VOPFUNC
)err_advlock
}, /* advlock */
153 { &vnop_bwrite_desc
, (VOPFUNC
)spec_bwrite
}, /* bwrite */
154 { &vnop_pagein_desc
, (VOPFUNC
)err_pagein
}, /* Pagein */
155 { &vnop_pageout_desc
, (VOPFUNC
)err_pageout
}, /* Pageout */
156 { &vnop_copyfile_desc
, (VOPFUNC
)err_copyfile
}, /* Copyfile */
157 { &vnop_blktooff_desc
, (VOPFUNC
)spec_blktooff
}, /* blktooff */
158 { &vnop_offtoblk_desc
, (VOPFUNC
)spec_offtoblk
}, /* offtoblk */
159 { &vnop_blockmap_desc
, (VOPFUNC
)spec_blockmap
}, /* blockmap */
160 { (struct vnodeop_desc
*)NULL
, (int (*)(void *))NULL
}
162 struct vnodeopv_desc spec_vnodeop_opv_desc
=
163 { &spec_vnodeop_p
, spec_vnodeop_entries
};
166 static void set_blocksize(vnode_t
, dev_t
);
168 #define LOWPRI_TIER1_WINDOW_MSECS 25
169 #define LOWPRI_TIER2_WINDOW_MSECS 100
170 #define LOWPRI_TIER3_WINDOW_MSECS 500
172 #define LOWPRI_TIER1_IO_PERIOD_MSECS 40
173 #define LOWPRI_TIER2_IO_PERIOD_MSECS 85
174 #define LOWPRI_TIER3_IO_PERIOD_MSECS 200
176 #define LOWPRI_TIER1_IO_PERIOD_SSD_MSECS 5
177 #define LOWPRI_TIER2_IO_PERIOD_SSD_MSECS 15
178 #define LOWPRI_TIER3_IO_PERIOD_SSD_MSECS 25
181 int throttle_windows_msecs
[THROTTLE_LEVEL_END
+ 1] = {
183 LOWPRI_TIER1_WINDOW_MSECS
,
184 LOWPRI_TIER2_WINDOW_MSECS
,
185 LOWPRI_TIER3_WINDOW_MSECS
,
188 int throttle_io_period_msecs
[THROTTLE_LEVEL_END
+ 1] = {
190 LOWPRI_TIER1_IO_PERIOD_MSECS
,
191 LOWPRI_TIER2_IO_PERIOD_MSECS
,
192 LOWPRI_TIER3_IO_PERIOD_MSECS
,
195 int throttle_io_period_ssd_msecs
[THROTTLE_LEVEL_END
+ 1] = {
197 LOWPRI_TIER1_IO_PERIOD_SSD_MSECS
,
198 LOWPRI_TIER2_IO_PERIOD_SSD_MSECS
,
199 LOWPRI_TIER3_IO_PERIOD_SSD_MSECS
,
203 int throttled_count
[THROTTLE_LEVEL_END
+ 1];
205 struct _throttle_io_info_t
{
206 lck_mtx_t throttle_lock
;
208 struct timeval throttle_last_write_timestamp
;
209 struct timeval throttle_min_timer_deadline
;
210 struct timeval throttle_window_start_timestamp
[THROTTLE_LEVEL_END
+ 1]; /* window starts at both the beginning and completion of an I/O */
211 struct timeval throttle_last_IO_timestamp
[THROTTLE_LEVEL_END
+ 1];
212 pid_t throttle_last_IO_pid
[THROTTLE_LEVEL_END
+ 1];
213 struct timeval throttle_start_IO_period_timestamp
[THROTTLE_LEVEL_END
+ 1];
214 int32_t throttle_inflight_count
[THROTTLE_LEVEL_END
+ 1];
216 TAILQ_HEAD(, uthread
) throttle_uthlist
[THROTTLE_LEVEL_END
+ 1]; /* Lists of throttled uthreads */
217 int throttle_next_wake_level
;
219 thread_call_t throttle_timer_call
;
220 int32_t throttle_timer_ref
;
221 int32_t throttle_timer_active
;
223 int32_t throttle_io_count
;
224 int32_t throttle_io_count_begin
;
225 int *throttle_io_periods
;
226 uint32_t throttle_io_period_num
;
228 int32_t throttle_refcnt
;
229 int32_t throttle_alloc
;
230 int32_t throttle_disabled
;
231 int32_t throttle_is_fusion_with_priority
;
234 struct _throttle_io_info_t _throttle_io_info
[LOWPRI_MAX_NUM_DEV
];
237 int lowpri_throttle_enabled
= 1;
240 static void throttle_info_end_io_internal(struct _throttle_io_info_t
*info
, int throttle_level
);
241 static int throttle_info_update_internal(struct _throttle_io_info_t
*info
, uthread_t ut
, int flags
, boolean_t isssd
, boolean_t inflight
, struct bufattr
*bap
);
242 static int throttle_get_thread_throttle_level(uthread_t ut
);
243 static int throttle_get_thread_throttle_level_internal(uthread_t ut
, int io_tier
);
244 void throttle_info_mount_reset_period(mount_t mp
, int isssd
);
247 * Trivial lookup routine that always fails.
250 spec_lookup(struct vnop_lookup_args
*ap
)
257 set_blocksize(struct vnode
*vp
, dev_t dev
)
262 if ((major(dev
) < nblkdev
) && (size
= bdevsw
[major(dev
)].d_psize
)) {
263 rsize
= (*size
)(dev
);
264 if (rsize
<= 0) { /* did size fail? */
265 vp
->v_specsize
= DEV_BSIZE
;
267 vp
->v_specsize
= rsize
;
270 vp
->v_specsize
= DEV_BSIZE
;
275 set_fsblocksize(struct vnode
*vp
)
277 if (vp
->v_type
== VBLK
) {
278 dev_t dev
= (dev_t
)vp
->v_rdev
;
279 int maj
= major(dev
);
281 if ((u_int
)maj
>= (u_int
)nblkdev
) {
286 set_blocksize(vp
, dev
);
293 * Open a special file.
296 spec_open(struct vnop_open_args
*ap
)
298 struct proc
*p
= vfs_context_proc(ap
->a_context
);
299 kauth_cred_t cred
= vfs_context_ucred(ap
->a_context
);
300 struct vnode
*vp
= ap
->a_vp
;
301 dev_t bdev
, dev
= (dev_t
)vp
->v_rdev
;
302 int maj
= major(dev
);
306 * Don't allow open if fs is mounted -nodev.
308 if (vp
->v_mount
&& (vp
->v_mount
->mnt_flag
& MNT_NODEV
)) {
312 switch (vp
->v_type
) {
314 if ((u_int
)maj
>= (u_int
)nchrdev
) {
317 if (cred
!= FSCRED
&& (ap
->a_mode
& FWRITE
)) {
319 * When running in very secure mode, do not allow
320 * opens for writing of any disk character devices.
322 if (securelevel
>= 2 && isdisk(dev
, VCHR
)) {
326 /* Never allow writing to /dev/mem or /dev/kmem */
327 if (iskmemdev(dev
)) {
331 * When running in secure mode, do not allow opens for
332 * writing of character devices whose corresponding block
333 * devices are currently mounted.
335 if (securelevel
>= 1) {
336 if ((bdev
= chrtoblk(dev
)) != NODEV
&& check_mountedon(bdev
, VBLK
, &error
)) {
342 devsw_lock(dev
, S_IFCHR
);
343 error
= (*cdevsw
[maj
].d_open
)(dev
, ap
->a_mode
, S_IFCHR
, p
);
346 vp
->v_specinfo
->si_opencount
++;
349 devsw_unlock(dev
, S_IFCHR
);
351 if (error
== 0 && cdevsw
[maj
].d_type
== D_DISK
&& !vp
->v_un
.vu_specinfo
->si_initted
) {
353 uint64_t throttle_mask
= 0;
354 uint32_t devbsdunit
= 0;
356 if (VNOP_IOCTL(vp
, DKIOCGETTHROTTLEMASK
, (caddr_t
)&throttle_mask
, 0, NULL
) == 0) {
357 if (throttle_mask
!= 0 &&
358 VNOP_IOCTL(vp
, DKIOCISSOLIDSTATE
, (caddr_t
)&isssd
, 0, ap
->a_context
) == 0) {
360 * as a reasonable approximation, only use the lowest bit of the mask
361 * to generate a disk unit number
363 devbsdunit
= num_trailing_0(throttle_mask
);
367 vp
->v_un
.vu_specinfo
->si_isssd
= isssd
;
368 vp
->v_un
.vu_specinfo
->si_devbsdunit
= devbsdunit
;
369 vp
->v_un
.vu_specinfo
->si_throttle_mask
= throttle_mask
;
370 vp
->v_un
.vu_specinfo
->si_throttleable
= 1;
371 vp
->v_un
.vu_specinfo
->si_initted
= 1;
376 if (vp
->v_un
.vu_specinfo
->si_initted
== 0) {
378 vp
->v_un
.vu_specinfo
->si_initted
= 1;
385 if ((u_int
)maj
>= (u_int
)nblkdev
) {
389 * When running in very secure mode, do not allow
390 * opens for writing of any disk block devices.
392 if (securelevel
>= 2 && cred
!= FSCRED
&&
393 (ap
->a_mode
& FWRITE
) && bdevsw
[maj
].d_type
== D_DISK
) {
397 * Do not allow opens of block devices that are
400 if ((error
= vfs_mountedon(vp
))) {
404 devsw_lock(dev
, S_IFBLK
);
405 error
= (*bdevsw
[maj
].d_open
)(dev
, ap
->a_mode
, S_IFBLK
, p
);
407 vp
->v_specinfo
->si_opencount
++;
409 devsw_unlock(dev
, S_IFBLK
);
415 u_int32_t size512
= 512;
418 if (!VNOP_IOCTL(vp
, DKIOCGETBLOCKSIZE
, (caddr_t
)&blksize
, 0, ap
->a_context
)) {
419 /* Switch to 512 byte sectors (temporarily) */
421 if (!VNOP_IOCTL(vp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&size512
, FWRITE
, ap
->a_context
)) {
422 /* Get the number of 512 byte physical blocks. */
423 if (!VNOP_IOCTL(vp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
, 0, ap
->a_context
)) {
427 /* If it doesn't set back, we can't recover */
428 if (VNOP_IOCTL(vp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&blksize
, FWRITE
, ap
->a_context
)) {
435 set_blocksize(vp
, dev
);
438 * Cache the size in bytes of the block device for later
439 * use by spec_write().
442 vp
->v_specdevsize
= blkcnt
* (u_int64_t
)size512
;
444 vp
->v_specdevsize
= (u_int64_t
)0; /* Default: Can't get */
450 panic("spec_open type");
459 spec_read(struct vnop_read_args
*ap
)
461 struct vnode
*vp
= ap
->a_vp
;
462 struct uio
*uio
= ap
->a_uio
;
464 daddr64_t bn
, nextbn
;
466 int devBlockSize
= 0;
472 if (uio
->uio_rw
!= UIO_READ
) {
473 panic("spec_read mode");
475 if (UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
)) {
476 panic("spec_read proc");
479 if (uio_resid(uio
) == 0) {
483 switch (vp
->v_type
) {
486 struct _throttle_io_info_t
*throttle_info
= NULL
;
487 int thread_throttle_level
;
488 if (cdevsw
[major(vp
->v_rdev
)].d_type
== D_DISK
&& vp
->v_un
.vu_specinfo
->si_throttleable
) {
489 throttle_info
= &_throttle_io_info
[vp
->v_un
.vu_specinfo
->si_devbsdunit
];
490 thread_throttle_level
= throttle_info_update_internal(throttle_info
, NULL
, 0, vp
->v_un
.vu_specinfo
->si_isssd
, TRUE
, NULL
);
492 error
= (*cdevsw
[major(vp
->v_rdev
)].d_read
)
493 (vp
->v_rdev
, uio
, ap
->a_ioflag
);
496 throttle_info_end_io_internal(throttle_info
, thread_throttle_level
);
503 if (uio
->uio_offset
< 0) {
509 devBlockSize
= vp
->v_specsize
;
511 if (devBlockSize
> PAGE_SIZE
) {
515 bscale
= PAGE_SIZE
/ devBlockSize
;
516 bsize
= bscale
* devBlockSize
;
519 on
= uio
->uio_offset
% bsize
;
521 bn
= (daddr64_t
)((uio
->uio_offset
/ devBlockSize
) & ~(bscale
- 1));
523 if (vp
->v_speclastr
+ bscale
== bn
) {
524 nextbn
= bn
+ bscale
;
525 error
= buf_breadn(vp
, bn
, (int)bsize
, &nextbn
,
526 (int *)&bsize
, 1, NOCRED
, &bp
);
528 error
= buf_bread(vp
, bn
, (int)bsize
, NOCRED
, &bp
);
532 vp
->v_speclastr
= bn
;
535 n
= bsize
- buf_resid(bp
);
536 if ((on
> n
) || error
) {
543 n
= min((unsigned)(n
- on
), uio_resid(uio
));
545 error
= uiomove((char *)buf_dataptr(bp
) + on
, n
, uio
);
546 if (n
+ on
== bsize
) {
550 } while (error
== 0 && uio_resid(uio
) > 0 && n
!= 0);
554 panic("spec_read type");
565 spec_write(struct vnop_write_args
*ap
)
567 struct vnode
*vp
= ap
->a_vp
;
568 struct uio
*uio
= ap
->a_uio
;
571 int bsize
, blkmask
, bscale
;
573 int devBlockSize
= 0;
579 if (uio
->uio_rw
!= UIO_WRITE
) {
580 panic("spec_write mode");
582 if (UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
)) {
583 panic("spec_write proc");
587 switch (vp
->v_type
) {
590 struct _throttle_io_info_t
*throttle_info
= NULL
;
591 int thread_throttle_level
;
592 if (cdevsw
[major(vp
->v_rdev
)].d_type
== D_DISK
&& vp
->v_un
.vu_specinfo
->si_throttleable
) {
593 throttle_info
= &_throttle_io_info
[vp
->v_un
.vu_specinfo
->si_devbsdunit
];
595 thread_throttle_level
= throttle_info_update_internal(throttle_info
, NULL
, 0, vp
->v_un
.vu_specinfo
->si_isssd
, TRUE
, NULL
);
597 microuptime(&throttle_info
->throttle_last_write_timestamp
);
599 error
= (*cdevsw
[major(vp
->v_rdev
)].d_write
)
600 (vp
->v_rdev
, uio
, ap
->a_ioflag
);
603 throttle_info_end_io_internal(throttle_info
, thread_throttle_level
);
610 if (uio_resid(uio
) == 0) {
613 if (uio
->uio_offset
< 0) {
617 io_sync
= (ap
->a_ioflag
& IO_SYNC
);
621 devBlockSize
= vp
->v_specsize
;
622 if (devBlockSize
> PAGE_SIZE
) {
626 bscale
= PAGE_SIZE
/ devBlockSize
;
627 blkmask
= bscale
- 1;
628 bsize
= bscale
* devBlockSize
;
632 bn
= (daddr64_t
)((uio
->uio_offset
/ devBlockSize
) & ~blkmask
);
633 on
= uio
->uio_offset
% bsize
;
635 n
= min((unsigned)(bsize
- on
), uio_resid(uio
));
638 * Use buf_getblk() as an optimization IFF:
640 * 1) We are reading exactly a block on a block
642 * 2) We know the size of the device from spec_open
643 * 3) The read doesn't span the end of the device
645 * Otherwise, we fall back on buf_bread().
648 vp
->v_specdevsize
!= (u_int64_t
)0 &&
649 (uio
->uio_offset
+ (u_int64_t
)n
) > vp
->v_specdevsize
) {
650 /* reduce the size of the read to what is there */
651 n
= (uio
->uio_offset
+ (u_int64_t
)n
) - vp
->v_specdevsize
;
655 bp
= buf_getblk(vp
, bn
, bsize
, 0, 0, BLK_WRITE
);
657 error
= (int)buf_bread(vp
, bn
, bsize
, NOCRED
, &bp
);
660 /* Translate downstream error for upstream, if needed */
662 error
= (int)buf_error(bp
);
668 n
= min(n
, bsize
- buf_resid(bp
));
670 error
= uiomove((char *)buf_dataptr(bp
) + on
, n
, uio
);
678 error
= buf_bwrite(bp
);
680 if ((n
+ on
) == bsize
) {
681 error
= buf_bawrite(bp
);
683 error
= buf_bdwrite(bp
);
686 } while (error
== 0 && uio_resid(uio
) > 0 && n
!= 0);
690 panic("spec_write type");
698 * Device ioctl operation.
701 spec_ioctl(struct vnop_ioctl_args
*ap
)
703 proc_t p
= vfs_context_proc(ap
->a_context
);
704 dev_t dev
= ap
->a_vp
->v_rdev
;
707 KERNEL_DEBUG_CONSTANT(FSDBG_CODE(DBG_IOCTL
, 0) | DBG_FUNC_START
,
708 dev
, ap
->a_command
, ap
->a_fflag
, ap
->a_vp
->v_type
, 0);
710 switch (ap
->a_vp
->v_type
) {
712 retval
= (*cdevsw
[major(dev
)].d_ioctl
)(dev
, ap
->a_command
, ap
->a_data
,
717 retval
= (*bdevsw
[major(dev
)].d_ioctl
)(dev
, ap
->a_command
, ap
->a_data
, ap
->a_fflag
, p
);
718 if (!retval
&& ap
->a_command
== DKIOCSETBLOCKSIZE
) {
719 ap
->a_vp
->v_specsize
= *(uint32_t *)ap
->a_data
;
727 KERNEL_DEBUG_CONSTANT(FSDBG_CODE(DBG_IOCTL
, 0) | DBG_FUNC_END
,
728 dev
, ap
->a_command
, ap
->a_fflag
, retval
, 0);
734 spec_select(struct vnop_select_args
*ap
)
736 proc_t p
= vfs_context_proc(ap
->a_context
);
739 switch (ap
->a_vp
->v_type
) {
744 dev
= ap
->a_vp
->v_rdev
;
745 return (*cdevsw
[major(dev
)].d_select
)(dev
, ap
->a_which
, ap
->a_wql
, p
);
749 static int filt_specattach(struct knote
*kn
, struct kevent_internal_s
*kev
);
752 spec_kqfilter(vnode_t vp
, struct knote
*kn
, struct kevent_internal_s
*kev
)
756 assert(vnode_ischr(vp
));
758 dev
= vnode_specrdev(vp
);
762 * Try a bpf device, as defined in bsd/net/bpf.c
763 * If it doesn't error out the attach, then it
764 * claimed it. Otherwise, fall through and try
767 int32_t tmp_flags
= kn
->kn_flags
;
768 int64_t tmp_data
= kn
->kn_data
;
771 res
= bpfkqfilter(dev
, kn
);
772 if ((kn
->kn_flags
& EV_ERROR
) == 0) {
775 kn
->kn_flags
= tmp_flags
;
776 kn
->kn_data
= tmp_data
;
779 if (major(dev
) > nchrdev
) {
780 knote_set_error(kn
, ENXIO
);
784 kn
->kn_vnode_kqok
= !!(cdevsw_flags
[major(dev
)] & CDEVSW_SELECT_KQUEUE
);
785 kn
->kn_vnode_use_ofst
= !!(cdevsw_flags
[major(dev
)] & CDEVSW_USE_OFFSET
);
787 if (cdevsw_flags
[major(dev
)] & CDEVSW_IS_PTS
) {
788 kn
->kn_filtid
= EVFILTID_PTSD
;
789 return ptsd_kqfilter(dev
, kn
);
790 } else if (cdevsw_flags
[major(dev
)] & CDEVSW_IS_PTC
) {
791 kn
->kn_filtid
= EVFILTID_PTMX
;
792 return ptmx_kqfilter(dev
, kn
);
793 } else if (cdevsw
[major(dev
)].d_type
== D_TTY
&& kn
->kn_vnode_kqok
) {
795 * TTYs from drivers that use struct ttys use their own filter
796 * routines. The PTC driver doesn't use the tty for character
797 * counts, so it must go through the select fallback.
799 kn
->kn_filtid
= EVFILTID_TTY
;
800 return knote_fops(kn
)->f_attach(kn
, kev
);
803 /* Try to attach to other char special devices */
804 return filt_specattach(kn
, kev
);
808 * Synch buffers associated with a block device
811 spec_fsync_internal(vnode_t vp
, int waitfor
, __unused vfs_context_t context
)
813 if (vp
->v_type
== VCHR
) {
817 * Flush all dirty buffers associated with a block device.
819 buf_flushdirtyblks(vp
, (waitfor
== MNT_WAIT
|| waitfor
== MNT_DWAIT
), 0, "spec_fsync");
825 spec_fsync(struct vnop_fsync_args
*ap
)
827 return spec_fsync_internal(ap
->a_vp
, ap
->a_waitfor
, ap
->a_context
);
832 * Just call the device strategy routine
834 void throttle_init(void);
838 #define DEBUG_ALLOC_THROTTLE_INFO(format, debug_info, args...) \
840 if ((debug_info)->alloc) \
841 printf("%s: "format, __FUNCTION__, ## args); \
845 #define DEBUG_ALLOC_THROTTLE_INFO(format, debug_info, args...)
849 SYSCTL_INT(_debug
, OID_AUTO
, lowpri_throttle_tier1_window_msecs
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &throttle_windows_msecs
[THROTTLE_LEVEL_TIER1
], 0, "");
850 SYSCTL_INT(_debug
, OID_AUTO
, lowpri_throttle_tier2_window_msecs
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &throttle_windows_msecs
[THROTTLE_LEVEL_TIER2
], 0, "");
851 SYSCTL_INT(_debug
, OID_AUTO
, lowpri_throttle_tier3_window_msecs
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &throttle_windows_msecs
[THROTTLE_LEVEL_TIER3
], 0, "");
853 SYSCTL_INT(_debug
, OID_AUTO
, lowpri_throttle_tier1_io_period_msecs
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &throttle_io_period_msecs
[THROTTLE_LEVEL_TIER1
], 0, "");
854 SYSCTL_INT(_debug
, OID_AUTO
, lowpri_throttle_tier2_io_period_msecs
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &throttle_io_period_msecs
[THROTTLE_LEVEL_TIER2
], 0, "");
855 SYSCTL_INT(_debug
, OID_AUTO
, lowpri_throttle_tier3_io_period_msecs
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &throttle_io_period_msecs
[THROTTLE_LEVEL_TIER3
], 0, "");
857 SYSCTL_INT(_debug
, OID_AUTO
, lowpri_throttle_tier1_io_period_ssd_msecs
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &throttle_io_period_ssd_msecs
[THROTTLE_LEVEL_TIER1
], 0, "");
858 SYSCTL_INT(_debug
, OID_AUTO
, lowpri_throttle_tier2_io_period_ssd_msecs
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &throttle_io_period_ssd_msecs
[THROTTLE_LEVEL_TIER2
], 0, "");
859 SYSCTL_INT(_debug
, OID_AUTO
, lowpri_throttle_tier3_io_period_ssd_msecs
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &throttle_io_period_ssd_msecs
[THROTTLE_LEVEL_TIER3
], 0, "");
861 SYSCTL_INT(_debug
, OID_AUTO
, lowpri_throttle_enabled
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &lowpri_throttle_enabled
, 0, "");
864 static lck_grp_t
*throttle_lock_grp
;
865 static lck_attr_t
*throttle_lock_attr
;
866 static lck_grp_attr_t
*throttle_lock_grp_attr
;
870 * throttled I/O helper function
871 * convert the index of the lowest set bit to a device index
874 num_trailing_0(uint64_t n
)
877 * since in most cases the number of trailing 0s is very small,
878 * we simply counting sequentially from the lowest bit
881 return sizeof(n
) * 8;
884 while (!ISSET(n
, 1)) {
893 * Release the reference and if the item was allocated and this is the last
894 * reference then free it.
896 * This routine always returns the old value.
899 throttle_info_rel(struct _throttle_io_info_t
*info
)
901 SInt32 oldValue
= OSDecrementAtomic(&info
->throttle_refcnt
);
903 DEBUG_ALLOC_THROTTLE_INFO("refcnt = %d info = %p\n",
904 info
, (int)(oldValue
- 1), info
);
906 /* The reference count just went negative, very bad */
908 panic("throttle info ref cnt went negative!");
912 * Once reference count is zero, no one else should be able to take a
915 if ((info
->throttle_refcnt
== 0) && (info
->throttle_alloc
)) {
916 DEBUG_ALLOC_THROTTLE_INFO("Freeing info = %p\n", info
);
918 lck_mtx_destroy(&info
->throttle_lock
, throttle_lock_grp
);
926 * Just take a reference on the throttle info structure.
928 * This routine always returns the old value.
931 throttle_info_ref(struct _throttle_io_info_t
*info
)
933 SInt32 oldValue
= OSIncrementAtomic(&info
->throttle_refcnt
);
935 DEBUG_ALLOC_THROTTLE_INFO("refcnt = %d info = %p\n",
936 info
, (int)(oldValue
- 1), info
);
937 /* Allocated items should never have a reference of zero */
938 if (info
->throttle_alloc
&& (oldValue
== 0)) {
939 panic("Taking a reference without calling create throttle info!\n");
946 * on entry the throttle_lock is held...
947 * this function is responsible for taking
948 * and dropping the reference on the info
949 * structure which will keep it from going
950 * away while the timer is running if it
951 * happens to have been dynamically allocated by
952 * a network fileystem kext which is now trying
956 throttle_timer_start(struct _throttle_io_info_t
*info
, boolean_t update_io_count
, int wakelevel
)
958 struct timeval elapsed
;
960 struct timeval period
;
961 uint64_t elapsed_msecs
;
965 boolean_t throttled
= FALSE
;
966 boolean_t need_timer
= FALSE
;
970 if (update_io_count
== TRUE
) {
971 info
->throttle_io_count_begin
= info
->throttle_io_count
;
972 info
->throttle_io_period_num
++;
974 while (wakelevel
>= THROTTLE_LEVEL_THROTTLED
) {
975 info
->throttle_start_IO_period_timestamp
[wakelevel
--] = now
;
978 info
->throttle_min_timer_deadline
= now
;
980 msecs
= info
->throttle_io_periods
[THROTTLE_LEVEL_THROTTLED
];
981 period
.tv_sec
= msecs
/ 1000;
982 period
.tv_usec
= (msecs
% 1000) * 1000;
984 timevaladd(&info
->throttle_min_timer_deadline
, &period
);
986 for (throttle_level
= THROTTLE_LEVEL_START
; throttle_level
< THROTTLE_LEVEL_END
; throttle_level
++) {
988 timevalsub(&elapsed
, &info
->throttle_window_start_timestamp
[throttle_level
]);
989 elapsed_msecs
= (uint64_t)elapsed
.tv_sec
* (uint64_t)1000 + (elapsed
.tv_usec
/ 1000);
991 for (level
= throttle_level
+ 1; level
<= THROTTLE_LEVEL_END
; level
++) {
992 if (!TAILQ_EMPTY(&info
->throttle_uthlist
[level
])) {
993 if (elapsed_msecs
< (uint64_t)throttle_windows_msecs
[level
] || info
->throttle_inflight_count
[throttle_level
]) {
995 * we had an I/O occur at a higher priority tier within
996 * this tier's throttle window
1001 * we assume that the windows are the same or longer
1002 * as we drop through the throttling tiers... thus
1003 * we can stop looking once we run into a tier with
1004 * threads to schedule regardless of whether it's
1005 * still in its throttling window or not
1010 if (throttled
== TRUE
) {
1014 if (throttled
== TRUE
) {
1015 uint64_t deadline
= 0;
1016 struct timeval target
;
1017 struct timeval min_target
;
1020 * we've got at least one tier still in a throttled window
1021 * so we need a timer running... compute the next deadline
1024 for (level
= throttle_level
+ 1; level
<= THROTTLE_LEVEL_END
; level
++) {
1025 if (TAILQ_EMPTY(&info
->throttle_uthlist
[level
])) {
1029 target
= info
->throttle_start_IO_period_timestamp
[level
];
1031 msecs
= info
->throttle_io_periods
[level
];
1032 period
.tv_sec
= msecs
/ 1000;
1033 period
.tv_usec
= (msecs
% 1000) * 1000;
1035 timevaladd(&target
, &period
);
1037 if (need_timer
== FALSE
|| timevalcmp(&target
, &min_target
, <)) {
1038 min_target
= target
;
1042 if (timevalcmp(&info
->throttle_min_timer_deadline
, &now
, >)) {
1043 if (timevalcmp(&info
->throttle_min_timer_deadline
, &min_target
, >)) {
1044 min_target
= info
->throttle_min_timer_deadline
;
1048 if (info
->throttle_timer_active
) {
1049 if (thread_call_cancel(info
->throttle_timer_call
) == FALSE
) {
1051 * couldn't kill the timer because it's already
1052 * been dispatched, so don't try to start a new
1053 * one... once we drop the lock, the timer will
1054 * proceed and eventually re-run this function
1058 info
->throttle_timer_active
= 0;
1061 if (need_timer
== TRUE
) {
1063 * This is defined as an int (32-bit) rather than a 64-bit
1064 * value because it would need a really big period in the
1065 * order of ~500 days to overflow this. So, we let this be
1066 * 32-bit which allows us to use the clock_interval_to_deadline()
1071 if (info
->throttle_timer_ref
== 0) {
1073 * take a reference for the timer
1075 throttle_info_ref(info
);
1077 info
->throttle_timer_ref
= 1;
1079 elapsed
= min_target
;
1080 timevalsub(&elapsed
, &now
);
1081 target_msecs
= elapsed
.tv_sec
* 1000 + elapsed
.tv_usec
/ 1000;
1083 if (target_msecs
<= 0) {
1085 * we may have computed a deadline slightly in the past
1086 * due to various factors... if so, just set the timer
1087 * to go off in the near future (we don't need to be precise)
1091 clock_interval_to_deadline(target_msecs
, 1000000, &deadline
);
1093 thread_call_enter_delayed(info
->throttle_timer_call
, deadline
);
1094 info
->throttle_timer_active
= 1;
1097 return throttle_level
;
1102 throttle_timer(struct _throttle_io_info_t
*info
)
1104 uthread_t ut
, utlist
;
1105 struct timeval elapsed
;
1107 uint64_t elapsed_msecs
;
1111 caddr_t wake_address
= NULL
;
1112 boolean_t update_io_count
= FALSE
;
1113 boolean_t need_wakeup
= FALSE
;
1114 boolean_t need_release
= FALSE
;
1117 lck_mtx_lock(&info
->throttle_lock
);
1119 info
->throttle_timer_active
= 0;
1123 timevalsub(&elapsed
, &info
->throttle_start_IO_period_timestamp
[THROTTLE_LEVEL_THROTTLED
]);
1124 elapsed_msecs
= (uint64_t)elapsed
.tv_sec
* (uint64_t)1000 + (elapsed
.tv_usec
/ 1000);
1126 if (elapsed_msecs
>= (uint64_t)info
->throttle_io_periods
[THROTTLE_LEVEL_THROTTLED
]) {
1127 wake_level
= info
->throttle_next_wake_level
;
1129 for (level
= THROTTLE_LEVEL_START
; level
< THROTTLE_LEVEL_END
; level
++) {
1131 timevalsub(&elapsed
, &info
->throttle_start_IO_period_timestamp
[wake_level
]);
1132 elapsed_msecs
= (uint64_t)elapsed
.tv_sec
* (uint64_t)1000 + (elapsed
.tv_usec
/ 1000);
1134 if (elapsed_msecs
>= (uint64_t)info
->throttle_io_periods
[wake_level
] && !TAILQ_EMPTY(&info
->throttle_uthlist
[wake_level
])) {
1136 * we're closing out the current IO period...
1137 * if we have a waiting thread, wake it up
1138 * after we have reset the I/O window info
1141 update_io_count
= TRUE
;
1143 info
->throttle_next_wake_level
= wake_level
- 1;
1145 if (info
->throttle_next_wake_level
== THROTTLE_LEVEL_START
) {
1146 info
->throttle_next_wake_level
= THROTTLE_LEVEL_END
;
1153 if (wake_level
== THROTTLE_LEVEL_START
) {
1154 wake_level
= THROTTLE_LEVEL_END
;
1158 if (need_wakeup
== TRUE
) {
1159 if (!TAILQ_EMPTY(&info
->throttle_uthlist
[wake_level
])) {
1160 ut
= (uthread_t
)TAILQ_FIRST(&info
->throttle_uthlist
[wake_level
]);
1161 TAILQ_REMOVE(&info
->throttle_uthlist
[wake_level
], ut
, uu_throttlelist
);
1162 ut
->uu_on_throttlelist
= THROTTLE_LEVEL_NONE
;
1163 ut
->uu_is_throttled
= false;
1165 wake_address
= (caddr_t
)&ut
->uu_on_throttlelist
;
1168 wake_level
= THROTTLE_LEVEL_START
;
1171 throttle_level
= throttle_timer_start(info
, update_io_count
, wake_level
);
1173 if (wake_address
!= NULL
) {
1174 wakeup(wake_address
);
1177 for (level
= THROTTLE_LEVEL_THROTTLED
; level
<= throttle_level
; level
++) {
1178 TAILQ_FOREACH_SAFE(ut
, &info
->throttle_uthlist
[level
], uu_throttlelist
, utlist
) {
1179 TAILQ_REMOVE(&info
->throttle_uthlist
[level
], ut
, uu_throttlelist
);
1180 ut
->uu_on_throttlelist
= THROTTLE_LEVEL_NONE
;
1181 ut
->uu_is_throttled
= false;
1183 wakeup(&ut
->uu_on_throttlelist
);
1186 if (info
->throttle_timer_active
== 0 && info
->throttle_timer_ref
) {
1187 info
->throttle_timer_ref
= 0;
1188 need_release
= TRUE
;
1190 lck_mtx_unlock(&info
->throttle_lock
);
1192 if (need_release
== TRUE
) {
1193 throttle_info_rel(info
);
1199 throttle_add_to_list(struct _throttle_io_info_t
*info
, uthread_t ut
, int mylevel
, boolean_t insert_tail
)
1201 boolean_t start_timer
= FALSE
;
1202 int level
= THROTTLE_LEVEL_START
;
1204 if (TAILQ_EMPTY(&info
->throttle_uthlist
[mylevel
])) {
1205 info
->throttle_start_IO_period_timestamp
[mylevel
] = info
->throttle_last_IO_timestamp
[mylevel
];
1209 if (insert_tail
== TRUE
) {
1210 TAILQ_INSERT_TAIL(&info
->throttle_uthlist
[mylevel
], ut
, uu_throttlelist
);
1212 TAILQ_INSERT_HEAD(&info
->throttle_uthlist
[mylevel
], ut
, uu_throttlelist
);
1215 ut
->uu_on_throttlelist
= mylevel
;
1217 if (start_timer
== TRUE
) {
1218 /* we may need to start or rearm the timer */
1219 level
= throttle_timer_start(info
, FALSE
, THROTTLE_LEVEL_START
);
1221 if (level
== THROTTLE_LEVEL_END
) {
1222 if (ut
->uu_on_throttlelist
>= THROTTLE_LEVEL_THROTTLED
) {
1223 TAILQ_REMOVE(&info
->throttle_uthlist
[ut
->uu_on_throttlelist
], ut
, uu_throttlelist
);
1225 ut
->uu_on_throttlelist
= THROTTLE_LEVEL_NONE
;
1233 throttle_init_throttle_window(void)
1235 int throttle_window_size
;
1238 * The hierarchy of throttle window values is as follows:
1240 * - Device tree properties
1242 * All values are specified in msecs.
1245 /* Override global values with device-tree properties */
1246 if (PE_get_default("kern.io_throttle_window_tier1", &throttle_window_size
, sizeof(throttle_window_size
))) {
1247 throttle_windows_msecs
[THROTTLE_LEVEL_TIER1
] = throttle_window_size
;
1250 if (PE_get_default("kern.io_throttle_window_tier2", &throttle_window_size
, sizeof(throttle_window_size
))) {
1251 throttle_windows_msecs
[THROTTLE_LEVEL_TIER2
] = throttle_window_size
;
1254 if (PE_get_default("kern.io_throttle_window_tier3", &throttle_window_size
, sizeof(throttle_window_size
))) {
1255 throttle_windows_msecs
[THROTTLE_LEVEL_TIER3
] = throttle_window_size
;
1258 /* Override with boot-args */
1259 if (PE_parse_boot_argn("io_throttle_window_tier1", &throttle_window_size
, sizeof(throttle_window_size
))) {
1260 throttle_windows_msecs
[THROTTLE_LEVEL_TIER1
] = throttle_window_size
;
1263 if (PE_parse_boot_argn("io_throttle_window_tier2", &throttle_window_size
, sizeof(throttle_window_size
))) {
1264 throttle_windows_msecs
[THROTTLE_LEVEL_TIER2
] = throttle_window_size
;
1267 if (PE_parse_boot_argn("io_throttle_window_tier3", &throttle_window_size
, sizeof(throttle_window_size
))) {
1268 throttle_windows_msecs
[THROTTLE_LEVEL_TIER3
] = throttle_window_size
;
1273 throttle_init_throttle_period(struct _throttle_io_info_t
*info
, boolean_t isssd
)
1275 int throttle_period_size
;
1278 * The hierarchy of throttle period values is as follows:
1280 * - Device tree properties
1282 * All values are specified in msecs.
1285 /* Assign global defaults */
1286 if ((isssd
== TRUE
) && (info
->throttle_is_fusion_with_priority
== 0)) {
1287 info
->throttle_io_periods
= &throttle_io_period_ssd_msecs
[0];
1289 info
->throttle_io_periods
= &throttle_io_period_msecs
[0];
1292 /* Override global values with device-tree properties */
1293 if (PE_get_default("kern.io_throttle_period_tier1", &throttle_period_size
, sizeof(throttle_period_size
))) {
1294 info
->throttle_io_periods
[THROTTLE_LEVEL_TIER1
] = throttle_period_size
;
1297 if (PE_get_default("kern.io_throttle_period_tier2", &throttle_period_size
, sizeof(throttle_period_size
))) {
1298 info
->throttle_io_periods
[THROTTLE_LEVEL_TIER2
] = throttle_period_size
;
1301 if (PE_get_default("kern.io_throttle_period_tier3", &throttle_period_size
, sizeof(throttle_period_size
))) {
1302 info
->throttle_io_periods
[THROTTLE_LEVEL_TIER3
] = throttle_period_size
;
1305 /* Override with boot-args */
1306 if (PE_parse_boot_argn("io_throttle_period_tier1", &throttle_period_size
, sizeof(throttle_period_size
))) {
1307 info
->throttle_io_periods
[THROTTLE_LEVEL_TIER1
] = throttle_period_size
;
1310 if (PE_parse_boot_argn("io_throttle_period_tier2", &throttle_period_size
, sizeof(throttle_period_size
))) {
1311 info
->throttle_io_periods
[THROTTLE_LEVEL_TIER2
] = throttle_period_size
;
1314 if (PE_parse_boot_argn("io_throttle_period_tier3", &throttle_period_size
, sizeof(throttle_period_size
))) {
1315 info
->throttle_io_periods
[THROTTLE_LEVEL_TIER3
] = throttle_period_size
;
1320 extern void vm_io_reprioritize_init(void);
1321 int iosched_enabled
= 1;
1327 struct _throttle_io_info_t
*info
;
1334 * allocate lock group attribute and group
1336 throttle_lock_grp_attr
= lck_grp_attr_alloc_init();
1337 throttle_lock_grp
= lck_grp_alloc_init("throttle I/O", throttle_lock_grp_attr
);
1339 /* Update throttle parameters based on device tree configuration */
1340 throttle_init_throttle_window();
1343 * allocate the lock attribute
1345 throttle_lock_attr
= lck_attr_alloc_init();
1347 for (i
= 0; i
< LOWPRI_MAX_NUM_DEV
; i
++) {
1348 info
= &_throttle_io_info
[i
];
1350 lck_mtx_init(&info
->throttle_lock
, throttle_lock_grp
, throttle_lock_attr
);
1351 info
->throttle_timer_call
= thread_call_allocate((thread_call_func_t
)throttle_timer
, (thread_call_param_t
)info
);
1353 for (level
= 0; level
<= THROTTLE_LEVEL_END
; level
++) {
1354 TAILQ_INIT(&info
->throttle_uthlist
[level
]);
1355 info
->throttle_last_IO_pid
[level
] = 0;
1356 info
->throttle_inflight_count
[level
] = 0;
1358 info
->throttle_next_wake_level
= THROTTLE_LEVEL_END
;
1359 info
->throttle_disabled
= 0;
1360 info
->throttle_is_fusion_with_priority
= 0;
1363 if (PE_parse_boot_argn("iosched", &iosched
, sizeof(iosched
))) {
1364 iosched_enabled
= iosched
;
1366 if (iosched_enabled
) {
1367 /* Initialize I/O Reprioritization mechanism */
1368 vm_io_reprioritize_init();
1374 sys_override_io_throttle(boolean_t enable_override
)
1376 if (enable_override
) {
1377 lowpri_throttle_enabled
= 0;
1379 lowpri_throttle_enabled
= 1;
1383 int rethrottle_wakeups
= 0;
1386 * the uu_rethrottle_lock is used to synchronize this function
1387 * with "throttle_lowpri_io" which is where a throttled thread
1388 * will block... that function will grab this lock before beginning
1389 * it's decision making process concerning the need to block, and
1390 * hold it through the assert_wait. When that thread is awakened
1391 * for any reason (timer or rethrottle), it will reacquire the
1392 * uu_rethrottle_lock before determining if it really is ok for
1393 * it to now run. This is the point at which the thread could
1394 * enter a different throttling queue and reblock or return from
1395 * the throttle w/o having waited out it's entire throttle if
1396 * the rethrottle has now moved it out of any currently
1397 * active throttle window.
1401 * 1 - This may be called with the task lock held.
1402 * 2 - This may be called with preemption and interrupts disabled
1403 * in the kqueue wakeup path so we can't take the throttle_lock which is a mutex
1404 * 3 - This cannot safely dereference uu_throttle_info, as it may
1405 * get deallocated out from under us
1409 rethrottle_thread(uthread_t ut
)
1412 * If uthread doesn't have throttle state, then there's no chance
1413 * of it needing a rethrottle.
1415 if (ut
->uu_throttle_info
== NULL
) {
1419 boolean_t s
= ml_set_interrupts_enabled(FALSE
);
1420 lck_spin_lock(&ut
->uu_rethrottle_lock
);
1422 if (!ut
->uu_is_throttled
) {
1423 ut
->uu_was_rethrottled
= true;
1425 int my_new_level
= throttle_get_thread_throttle_level(ut
);
1427 if (my_new_level
!= ut
->uu_on_throttlelist
) {
1429 * ut is currently blocked (as indicated by
1430 * ut->uu_is_throttled == true)
1431 * and we're changing it's throttle level, so
1432 * we need to wake it up.
1434 ut
->uu_is_throttled
= false;
1435 wakeup(&ut
->uu_on_throttlelist
);
1437 rethrottle_wakeups
++;
1438 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_FSRW
, 102)), thread_tid(ut
->uu_thread
), ut
->uu_on_throttlelist
, my_new_level
, 0, 0);
1441 lck_spin_unlock(&ut
->uu_rethrottle_lock
);
1442 ml_set_interrupts_enabled(s
);
1449 * Create and take a reference on a throttle info structure and return a
1450 * pointer for the file system to use when calling throttle_info_update.
1451 * Calling file system must have a matching release for every create.
1454 throttle_info_create(void)
1456 struct _throttle_io_info_t
*info
;
1459 MALLOC(info
, struct _throttle_io_info_t
*, sizeof(*info
), M_TEMP
, M_ZERO
| M_WAITOK
);
1460 /* Should never happen but just in case */
1464 /* Mark that this one was allocated and needs to be freed */
1465 DEBUG_ALLOC_THROTTLE_INFO("Creating info = %p\n", info
, info
);
1466 info
->throttle_alloc
= TRUE
;
1468 lck_mtx_init(&info
->throttle_lock
, throttle_lock_grp
, throttle_lock_attr
);
1469 info
->throttle_timer_call
= thread_call_allocate((thread_call_func_t
)throttle_timer
, (thread_call_param_t
)info
);
1471 for (level
= 0; level
<= THROTTLE_LEVEL_END
; level
++) {
1472 TAILQ_INIT(&info
->throttle_uthlist
[level
]);
1474 info
->throttle_next_wake_level
= THROTTLE_LEVEL_END
;
1476 /* Take a reference */
1477 OSIncrementAtomic(&info
->throttle_refcnt
);
1484 * Release the throttle info pointer if all the reference are gone. Should be
1485 * called to release reference taken by throttle_info_create
1488 throttle_info_release(void *throttle_info
)
1490 DEBUG_ALLOC_THROTTLE_INFO("Releaseing info = %p\n",
1491 (struct _throttle_io_info_t
*)throttle_info
,
1492 (struct _throttle_io_info_t
*)throttle_info
);
1493 if (throttle_info
) { /* Just to be careful */
1494 throttle_info_rel(throttle_info
);
1501 * File Systems that create an info structure, need to call this routine in
1502 * their mount routine (used by cluster code). File Systems that call this in
1503 * their mount routines must call throttle_info_mount_rel in their unmount
1507 throttle_info_mount_ref(mount_t mp
, void *throttle_info
)
1509 if ((throttle_info
== NULL
) || (mp
== NULL
)) {
1512 throttle_info_ref(throttle_info
);
1515 * We already have a reference release it before adding the new one
1517 if (mp
->mnt_throttle_info
) {
1518 throttle_info_rel(mp
->mnt_throttle_info
);
1520 mp
->mnt_throttle_info
= throttle_info
;
1524 * Private KPI routine
1526 * return a handle for accessing throttle_info given a throttle_mask. The
1527 * handle must be released by throttle_info_rel_by_mask
1530 throttle_info_ref_by_mask(uint64_t throttle_mask
, throttle_info_handle_t
*throttle_info_handle
)
1533 struct _throttle_io_info_t
*info
;
1535 if (throttle_info_handle
== NULL
) {
1539 dev_index
= num_trailing_0(throttle_mask
);
1540 info
= &_throttle_io_info
[dev_index
];
1541 throttle_info_ref(info
);
1542 *(struct _throttle_io_info_t
**)throttle_info_handle
= info
;
1548 * Private KPI routine
1550 * release the handle obtained by throttle_info_ref_by_mask
1553 throttle_info_rel_by_mask(throttle_info_handle_t throttle_info_handle
)
1556 * for now the handle is just a pointer to _throttle_io_info_t
1558 throttle_info_rel((struct _throttle_io_info_t
*)throttle_info_handle
);
1564 * File Systems that throttle_info_mount_ref, must call this routine in their
1568 throttle_info_mount_rel(mount_t mp
)
1570 if (mp
->mnt_throttle_info
) {
1571 throttle_info_rel(mp
->mnt_throttle_info
);
1573 mp
->mnt_throttle_info
= NULL
;
1577 * Reset throttling periods for the given mount point
1579 * private interface used by disk conditioner to reset
1580 * throttling periods when 'is_ssd' status changes
1583 throttle_info_mount_reset_period(mount_t mp
, int isssd
)
1585 struct _throttle_io_info_t
*info
;
1588 info
= &_throttle_io_info
[LOWPRI_MAX_NUM_DEV
- 1];
1589 } else if (mp
->mnt_throttle_info
== NULL
) {
1590 info
= &_throttle_io_info
[mp
->mnt_devbsdunit
];
1592 info
= mp
->mnt_throttle_info
;
1595 throttle_init_throttle_period(info
, isssd
);
1599 throttle_info_get_last_io_time(mount_t mp
, struct timeval
*tv
)
1601 struct _throttle_io_info_t
*info
;
1604 info
= &_throttle_io_info
[LOWPRI_MAX_NUM_DEV
- 1];
1605 } else if (mp
->mnt_throttle_info
== NULL
) {
1606 info
= &_throttle_io_info
[mp
->mnt_devbsdunit
];
1608 info
= mp
->mnt_throttle_info
;
1611 *tv
= info
->throttle_last_write_timestamp
;
1615 update_last_io_time(mount_t mp
)
1617 struct _throttle_io_info_t
*info
;
1620 info
= &_throttle_io_info
[LOWPRI_MAX_NUM_DEV
- 1];
1621 } else if (mp
->mnt_throttle_info
== NULL
) {
1622 info
= &_throttle_io_info
[mp
->mnt_devbsdunit
];
1624 info
= mp
->mnt_throttle_info
;
1627 microuptime(&info
->throttle_last_write_timestamp
);
1629 mp
->mnt_last_write_completed_timestamp
= info
->throttle_last_write_timestamp
;
1634 throttle_get_io_policy(uthread_t
*ut
)
1637 *ut
= get_bsdthread_info(current_thread());
1640 return proc_get_effective_thread_policy(current_thread(), TASK_POLICY_IO
);
1644 throttle_get_passive_io_policy(uthread_t
*ut
)
1647 *ut
= get_bsdthread_info(current_thread());
1650 return proc_get_effective_thread_policy(current_thread(), TASK_POLICY_PASSIVE_IO
);
1655 throttle_get_thread_throttle_level(uthread_t ut
)
1657 uthread_t
*ut_p
= (ut
== NULL
) ? &ut
: NULL
;
1658 int io_tier
= throttle_get_io_policy(ut_p
);
1660 return throttle_get_thread_throttle_level_internal(ut
, io_tier
);
1664 * Return a throttle level given an existing I/O tier (such as returned by throttle_get_io_policy)
1667 throttle_get_thread_throttle_level_internal(uthread_t ut
, int io_tier
)
1669 int thread_throttle_level
= io_tier
;
1670 int user_idle_level
;
1674 /* Bootcache misses should always be throttled */
1675 if (ut
->uu_throttle_bc
) {
1676 thread_throttle_level
= THROTTLE_LEVEL_TIER3
;
1680 * Issue tier3 I/O as tier2 when the user is idle
1681 * to allow maintenance tasks to make more progress.
1683 * Assume any positive idle level is enough... for now it's
1684 * only ever 0 or 128 but this is not defined anywhere.
1686 if (thread_throttle_level
>= THROTTLE_LEVEL_TIER3
) {
1687 user_idle_level
= timer_get_user_idle_level();
1688 if (user_idle_level
> 0) {
1689 thread_throttle_level
--;
1693 return thread_throttle_level
;
1697 * I/O will be throttled if either of the following are true:
1698 * - Higher tiers have in-flight I/O
1699 * - The time delta since the last start/completion of a higher tier is within the throttle window interval
1701 * In-flight I/O is bookended by throttle_info_update_internal/throttle_info_end_io_internal
1704 throttle_io_will_be_throttled_internal(void * throttle_info
, int * mylevel
, int * throttling_level
)
1706 struct _throttle_io_info_t
*info
= throttle_info
;
1707 struct timeval elapsed
;
1709 uint64_t elapsed_msecs
;
1710 int thread_throttle_level
;
1713 if ((thread_throttle_level
= throttle_get_thread_throttle_level(NULL
)) < THROTTLE_LEVEL_THROTTLED
) {
1714 return THROTTLE_DISENGAGED
;
1719 for (throttle_level
= THROTTLE_LEVEL_START
; throttle_level
< thread_throttle_level
; throttle_level
++) {
1720 if (info
->throttle_inflight_count
[throttle_level
]) {
1724 timevalsub(&elapsed
, &info
->throttle_window_start_timestamp
[throttle_level
]);
1725 elapsed_msecs
= (uint64_t)elapsed
.tv_sec
* (uint64_t)1000 + (elapsed
.tv_usec
/ 1000);
1727 if (elapsed_msecs
< (uint64_t)throttle_windows_msecs
[thread_throttle_level
]) {
1731 if (throttle_level
>= thread_throttle_level
) {
1733 * we're beyond all of the throttle windows
1734 * that affect the throttle level of this thread,
1735 * so go ahead and treat as normal I/O
1737 return THROTTLE_DISENGAGED
;
1740 *mylevel
= thread_throttle_level
;
1742 if (throttling_level
) {
1743 *throttling_level
= throttle_level
;
1746 if (info
->throttle_io_count
!= info
->throttle_io_count_begin
) {
1748 * we've already issued at least one throttleable I/O
1749 * in the current I/O window, so avoid issuing another one
1751 return THROTTLE_NOW
;
1754 * we're in the throttle window, so
1755 * cut the I/O size back
1757 return THROTTLE_ENGAGED
;
1761 * If we have a mount point and it has a throttle info pointer then
1762 * use it to do the check, otherwise use the device unit number to find
1763 * the correct throttle info array element.
1766 throttle_io_will_be_throttled(__unused
int lowpri_window_msecs
, mount_t mp
)
1768 struct _throttle_io_info_t
*info
;
1771 * Should we just return zero if no mount point
1774 info
= &_throttle_io_info
[LOWPRI_MAX_NUM_DEV
- 1];
1775 } else if (mp
->mnt_throttle_info
== NULL
) {
1776 info
= &_throttle_io_info
[mp
->mnt_devbsdunit
];
1778 info
= mp
->mnt_throttle_info
;
1781 if (info
->throttle_is_fusion_with_priority
) {
1782 uthread_t ut
= get_bsdthread_info(current_thread());
1783 if (ut
->uu_lowpri_window
== 0) {
1784 return THROTTLE_DISENGAGED
;
1788 if (info
->throttle_disabled
) {
1789 return THROTTLE_DISENGAGED
;
1791 return throttle_io_will_be_throttled_internal(info
, NULL
, NULL
);
1796 * Routine to increment I/O throttling counters maintained in the proc
1800 throttle_update_proc_stats(pid_t throttling_pid
, int count
)
1802 proc_t throttling_proc
;
1803 proc_t throttled_proc
= current_proc();
1805 /* The throttled_proc is always the current proc; so we are not concerned with refs */
1806 OSAddAtomic64(count
, &(throttled_proc
->was_throttled
));
1808 /* The throttling pid might have exited by now */
1809 throttling_proc
= proc_find(throttling_pid
);
1810 if (throttling_proc
!= PROC_NULL
) {
1811 OSAddAtomic64(count
, &(throttling_proc
->did_throttle
));
1812 proc_rele(throttling_proc
);
1817 * Block until woken up by the throttle timer or by a rethrottle call.
1818 * As long as we hold the throttle_lock while querying the throttle tier, we're
1819 * safe against seeing an old throttle tier after a rethrottle.
1822 throttle_lowpri_io(int sleep_amount
)
1825 struct _throttle_io_info_t
*info
;
1826 int throttle_type
= 0;
1828 int throttling_level
= THROTTLE_LEVEL_NONE
;
1830 uint32_t throttle_io_period_num
= 0;
1831 boolean_t insert_tail
= TRUE
;
1834 ut
= get_bsdthread_info(current_thread());
1836 if (ut
->uu_lowpri_window
== 0) {
1840 info
= ut
->uu_throttle_info
;
1843 ut
->uu_throttle_bc
= false;
1844 ut
->uu_lowpri_window
= 0;
1847 lck_mtx_lock(&info
->throttle_lock
);
1848 assert(ut
->uu_on_throttlelist
< THROTTLE_LEVEL_THROTTLED
);
1850 if (sleep_amount
== 0) {
1854 if (sleep_amount
== 1 && !ut
->uu_throttle_bc
) {
1858 throttle_io_period_num
= info
->throttle_io_period_num
;
1860 ut
->uu_was_rethrottled
= false;
1862 while ((throttle_type
= throttle_io_will_be_throttled_internal(info
, &mylevel
, &throttling_level
))) {
1863 if (throttle_type
== THROTTLE_ENGAGED
) {
1864 if (sleep_amount
== 0) {
1867 if (info
->throttle_io_period_num
< throttle_io_period_num
) {
1870 if ((info
->throttle_io_period_num
- throttle_io_period_num
) >= (uint32_t)sleep_amount
) {
1875 * keep the same position in the list if "rethrottle_thread" changes our throttle level and
1876 * then puts us back to the original level before we get a chance to run
1878 if (ut
->uu_on_throttlelist
>= THROTTLE_LEVEL_THROTTLED
&& ut
->uu_on_throttlelist
!= mylevel
) {
1880 * must have been awakened via "rethrottle_thread" (the timer pulls us off the list)
1881 * and we've changed our throttling level, so pull ourselves off of the appropriate list
1882 * and make sure we get put on the tail of the new list since we're starting anew w/r to
1883 * the throttling engine
1885 TAILQ_REMOVE(&info
->throttle_uthlist
[ut
->uu_on_throttlelist
], ut
, uu_throttlelist
);
1886 ut
->uu_on_throttlelist
= THROTTLE_LEVEL_NONE
;
1889 if (ut
->uu_on_throttlelist
< THROTTLE_LEVEL_THROTTLED
) {
1890 if (throttle_add_to_list(info
, ut
, mylevel
, insert_tail
) == THROTTLE_LEVEL_END
) {
1894 assert(throttling_level
>= THROTTLE_LEVEL_START
&& throttling_level
<= THROTTLE_LEVEL_END
);
1896 s
= ml_set_interrupts_enabled(FALSE
);
1897 lck_spin_lock(&ut
->uu_rethrottle_lock
);
1900 * this is the critical section w/r to our interaction
1901 * with "rethrottle_thread"
1903 if (ut
->uu_was_rethrottled
) {
1904 lck_spin_unlock(&ut
->uu_rethrottle_lock
);
1905 ml_set_interrupts_enabled(s
);
1906 lck_mtx_yield(&info
->throttle_lock
);
1908 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_FSRW
, 103)), thread_tid(ut
->uu_thread
), ut
->uu_on_throttlelist
, 0, 0, 0);
1910 ut
->uu_was_rethrottled
= false;
1913 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_THROTTLE
, PROCESS_THROTTLED
)) | DBG_FUNC_NONE
,
1914 info
->throttle_last_IO_pid
[throttling_level
], throttling_level
, proc_selfpid(), mylevel
, 0);
1916 if (sleep_cnt
== 0) {
1917 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_FSRW
, 97)) | DBG_FUNC_START
,
1918 throttle_windows_msecs
[mylevel
], info
->throttle_io_periods
[mylevel
], info
->throttle_io_count
, 0, 0);
1919 throttled_count
[mylevel
]++;
1921 ut
->uu_wmesg
= "throttle_lowpri_io";
1923 assert_wait((caddr_t
)&ut
->uu_on_throttlelist
, THREAD_UNINT
);
1925 ut
->uu_is_throttled
= true;
1926 lck_spin_unlock(&ut
->uu_rethrottle_lock
);
1927 ml_set_interrupts_enabled(s
);
1929 lck_mtx_unlock(&info
->throttle_lock
);
1931 thread_block(THREAD_CONTINUE_NULL
);
1933 ut
->uu_wmesg
= NULL
;
1935 ut
->uu_is_throttled
= false;
1936 ut
->uu_was_rethrottled
= false;
1938 lck_mtx_lock(&info
->throttle_lock
);
1942 if (sleep_amount
== 0) {
1943 insert_tail
= FALSE
;
1944 } else if (info
->throttle_io_period_num
< throttle_io_period_num
||
1945 (info
->throttle_io_period_num
- throttle_io_period_num
) >= (uint32_t)sleep_amount
) {
1946 insert_tail
= FALSE
;
1951 if (ut
->uu_on_throttlelist
>= THROTTLE_LEVEL_THROTTLED
) {
1952 TAILQ_REMOVE(&info
->throttle_uthlist
[ut
->uu_on_throttlelist
], ut
, uu_throttlelist
);
1953 ut
->uu_on_throttlelist
= THROTTLE_LEVEL_NONE
;
1955 lck_mtx_unlock(&info
->throttle_lock
);
1958 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_FSRW
, 97)) | DBG_FUNC_END
,
1959 throttle_windows_msecs
[mylevel
], info
->throttle_io_periods
[mylevel
], info
->throttle_io_count
, 0, 0);
1961 * We update the stats for the last pid which opened a throttle window for the throttled thread.
1962 * This might not be completely accurate since the multiple throttles seen by the lower tier pid
1963 * might have been caused by various higher prio pids. However, updating these stats accurately
1964 * means doing a proc_find while holding the throttle lock which leads to deadlock.
1966 throttle_update_proc_stats(info
->throttle_last_IO_pid
[throttling_level
], sleep_cnt
);
1969 ut
->uu_throttle_info
= NULL
;
1970 ut
->uu_throttle_bc
= false;
1971 ut
->uu_lowpri_window
= 0;
1973 throttle_info_rel(info
);
1981 * set a kernel thread's IO policy. policy can be:
1982 * IOPOL_NORMAL, IOPOL_THROTTLE, IOPOL_PASSIVE, IOPOL_UTILITY, IOPOL_STANDARD
1984 * explanations about these policies are in the man page of setiopolicy_np
1987 throttle_set_thread_io_policy(int policy
)
1989 proc_set_thread_policy(current_thread(), TASK_POLICY_INTERNAL
, TASK_POLICY_IOPOL
, policy
);
1993 throttle_get_thread_effective_io_policy()
1995 return proc_get_effective_thread_policy(current_thread(), TASK_POLICY_IO
);
1999 throttle_info_reset_window(uthread_t ut
)
2001 struct _throttle_io_info_t
*info
;
2004 ut
= get_bsdthread_info(current_thread());
2007 if ((info
= ut
->uu_throttle_info
)) {
2008 throttle_info_rel(info
);
2010 ut
->uu_throttle_info
= NULL
;
2011 ut
->uu_lowpri_window
= 0;
2012 ut
->uu_throttle_bc
= false;
2018 throttle_info_set_initial_window(uthread_t ut
, struct _throttle_io_info_t
*info
, boolean_t BC_throttle
, boolean_t isssd
)
2020 if (lowpri_throttle_enabled
== 0 || info
->throttle_disabled
) {
2024 if (info
->throttle_io_periods
== 0) {
2025 throttle_init_throttle_period(info
, isssd
);
2027 if (ut
->uu_throttle_info
== NULL
) {
2028 ut
->uu_throttle_info
= info
;
2029 throttle_info_ref(info
);
2030 DEBUG_ALLOC_THROTTLE_INFO("updating info = %p\n", info
, info
);
2032 ut
->uu_lowpri_window
= 1;
2033 ut
->uu_throttle_bc
= BC_throttle
;
2038 * Update inflight IO count and throttling window
2039 * Should be called when an IO is done
2041 * Only affects IO that was sent through spec_strategy
2044 throttle_info_end_io(buf_t bp
)
2047 struct bufattr
*bap
;
2048 struct _throttle_io_info_t
*info
;
2052 if (!ISSET(bap
->ba_flags
, BA_STRATEGY_TRACKED_IO
)) {
2055 CLR(bap
->ba_flags
, BA_STRATEGY_TRACKED_IO
);
2057 mp
= buf_vnode(bp
)->v_mount
;
2059 info
= &_throttle_io_info
[mp
->mnt_devbsdunit
];
2061 info
= &_throttle_io_info
[LOWPRI_MAX_NUM_DEV
- 1];
2064 io_tier
= GET_BUFATTR_IO_TIER(bap
);
2065 if (ISSET(bap
->ba_flags
, BA_IO_TIER_UPGRADE
)) {
2069 throttle_info_end_io_internal(info
, io_tier
);
2073 * Decrement inflight count initially incremented by throttle_info_update_internal
2077 throttle_info_end_io_internal(struct _throttle_io_info_t
*info
, int throttle_level
)
2079 if (throttle_level
== THROTTLE_LEVEL_NONE
) {
2083 microuptime(&info
->throttle_window_start_timestamp
[throttle_level
]);
2084 OSDecrementAtomic(&info
->throttle_inflight_count
[throttle_level
]);
2085 assert(info
->throttle_inflight_count
[throttle_level
] >= 0);
2089 * If inflight is TRUE and bap is NULL then the caller is responsible for calling
2090 * throttle_info_end_io_internal to avoid leaking in-flight I/O.
2094 throttle_info_update_internal(struct _throttle_io_info_t
*info
, uthread_t ut
, int flags
, boolean_t isssd
, boolean_t inflight
, struct bufattr
*bap
)
2096 int thread_throttle_level
;
2098 if (lowpri_throttle_enabled
== 0 || info
->throttle_disabled
) {
2099 return THROTTLE_LEVEL_NONE
;
2103 ut
= get_bsdthread_info(current_thread());
2106 if (bap
&& inflight
&& !ut
->uu_throttle_bc
) {
2107 thread_throttle_level
= GET_BUFATTR_IO_TIER(bap
);
2108 if (ISSET(bap
->ba_flags
, BA_IO_TIER_UPGRADE
)) {
2109 thread_throttle_level
--;
2112 thread_throttle_level
= throttle_get_thread_throttle_level(ut
);
2115 if (thread_throttle_level
!= THROTTLE_LEVEL_NONE
) {
2116 if (!ISSET(flags
, B_PASSIVE
)) {
2117 info
->throttle_last_IO_pid
[thread_throttle_level
] = proc_selfpid();
2118 if (inflight
&& !ut
->uu_throttle_bc
) {
2120 SET(bap
->ba_flags
, BA_STRATEGY_TRACKED_IO
);
2122 OSIncrementAtomic(&info
->throttle_inflight_count
[thread_throttle_level
]);
2124 microuptime(&info
->throttle_window_start_timestamp
[thread_throttle_level
]);
2126 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_THROTTLE
, OPEN_THROTTLE_WINDOW
)) | DBG_FUNC_NONE
,
2127 current_proc()->p_pid
, thread_throttle_level
, 0, 0, 0);
2129 microuptime(&info
->throttle_last_IO_timestamp
[thread_throttle_level
]);
2133 if (thread_throttle_level
>= THROTTLE_LEVEL_THROTTLED
) {
2135 * I'd really like to do the IOSleep here, but
2136 * we may be holding all kinds of filesystem related locks
2137 * and the pages for this I/O marked 'busy'...
2138 * we don't want to cause a normal task to block on
2139 * one of these locks while we're throttling a task marked
2140 * for low priority I/O... we'll mark the uthread and
2141 * do the delay just before we return from the system
2142 * call that triggered this I/O or from vnode_pagein
2144 OSAddAtomic(1, &info
->throttle_io_count
);
2146 throttle_info_set_initial_window(ut
, info
, FALSE
, isssd
);
2149 return thread_throttle_level
;
2153 throttle_info_update_by_mount(mount_t mp
)
2155 struct _throttle_io_info_t
*info
;
2157 boolean_t isssd
= FALSE
;
2159 ut
= get_bsdthread_info(current_thread());
2162 if (disk_conditioner_mount_is_ssd(mp
)) {
2165 info
= &_throttle_io_info
[mp
->mnt_devbsdunit
];
2167 info
= &_throttle_io_info
[LOWPRI_MAX_NUM_DEV
- 1];
2170 if (!ut
->uu_lowpri_window
) {
2171 throttle_info_set_initial_window(ut
, info
, FALSE
, isssd
);
2181 * this is usually called before every I/O, used for throttled I/O
2182 * book keeping. This routine has low overhead and does not sleep
2185 throttle_info_update(void *throttle_info
, int flags
)
2187 if (throttle_info
) {
2188 throttle_info_update_internal(throttle_info
, NULL
, flags
, FALSE
, FALSE
, NULL
);
2195 * this is usually called before every I/O, used for throttled I/O
2196 * book keeping. This routine has low overhead and does not sleep
2199 throttle_info_update_by_mask(void *throttle_info_handle
, int flags
)
2201 void *throttle_info
= throttle_info_handle
;
2204 * for now we only use the lowest bit of the throttle mask, so the
2205 * handle is the same as the throttle_info. Later if we store a
2206 * set of throttle infos in the handle, we will want to loop through
2207 * them and call throttle_info_update in a loop
2209 throttle_info_update(throttle_info
, flags
);
2214 * This routine marks the throttle info as disabled. Used for mount points which
2215 * support I/O scheduling.
2219 throttle_info_disable_throttle(int devno
, boolean_t isfusion
)
2221 struct _throttle_io_info_t
*info
;
2223 if (devno
< 0 || devno
>= LOWPRI_MAX_NUM_DEV
) {
2224 panic("Illegal devno (%d) passed into throttle_info_disable_throttle()", devno
);
2227 info
= &_throttle_io_info
[devno
];
2228 // don't disable software throttling on devices that are part of a fusion device
2229 // and override the software throttle periods to use HDD periods
2231 info
->throttle_is_fusion_with_priority
= isfusion
;
2232 throttle_init_throttle_period(info
, FALSE
);
2234 info
->throttle_disabled
= !info
->throttle_is_fusion_with_priority
;
2240 * KPI routine (private)
2241 * Called to determine if this IO is being throttled to this level so that it can be treated specially
2244 throttle_info_io_will_be_throttled(void * throttle_info
, int policy
)
2246 struct _throttle_io_info_t
*info
= throttle_info
;
2247 struct timeval elapsed
;
2248 uint64_t elapsed_msecs
;
2250 int thread_throttle_level
;
2253 case IOPOL_THROTTLE
:
2254 thread_throttle_level
= THROTTLE_LEVEL_TIER3
;
2257 thread_throttle_level
= THROTTLE_LEVEL_TIER2
;
2259 case IOPOL_STANDARD
:
2260 thread_throttle_level
= THROTTLE_LEVEL_TIER1
;
2263 thread_throttle_level
= THROTTLE_LEVEL_TIER0
;
2266 for (throttle_level
= THROTTLE_LEVEL_START
; throttle_level
< thread_throttle_level
; throttle_level
++) {
2267 if (info
->throttle_inflight_count
[throttle_level
]) {
2271 microuptime(&elapsed
);
2272 timevalsub(&elapsed
, &info
->throttle_window_start_timestamp
[throttle_level
]);
2273 elapsed_msecs
= (uint64_t)elapsed
.tv_sec
* (uint64_t)1000 + (elapsed
.tv_usec
/ 1000);
2275 if (elapsed_msecs
< (uint64_t)throttle_windows_msecs
[thread_throttle_level
]) {
2279 if (throttle_level
>= thread_throttle_level
) {
2281 * we're beyond all of the throttle windows
2282 * so go ahead and treat as normal I/O
2284 return THROTTLE_DISENGAGED
;
2287 * we're in the throttle window
2289 return THROTTLE_ENGAGED
;
2293 throttle_lowpri_window(void)
2295 struct uthread
*ut
= get_bsdthread_info(current_thread());
2296 return ut
->uu_lowpri_window
;
2301 int upl_get_cached_tier(void *);
2305 spec_strategy(struct vnop_strategy_args
*ap
)
2314 struct bufattr
*bap
;
2316 struct _throttle_io_info_t
*throttle_info
;
2317 boolean_t isssd
= FALSE
;
2318 boolean_t inflight
= FALSE
;
2319 boolean_t upgrade
= FALSE
;
2322 #if !CONFIG_EMBEDDED
2323 proc_t curproc
= current_proc();
2324 #endif /* !CONFIG_EMBEDDED */
2327 bdev
= buf_device(bp
);
2328 mp
= buf_vnode(bp
)->v_mount
;
2332 if (bp
->b_flags
& B_CLUSTER
) {
2333 io_tier
= upl_get_cached_tier(bp
->b_upl
);
2335 if (io_tier
== -1) {
2336 io_tier
= throttle_get_io_policy(&ut
);
2338 #if DEVELOPMENT || DEBUG
2340 int my_io_tier
= throttle_get_io_policy(&ut
);
2342 if (io_tier
!= my_io_tier
) {
2343 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_THROTTLE
, IO_TIER_UPL_MISMATCH
)) | DBG_FUNC_NONE
, buf_kernel_addrperm_addr(bp
), my_io_tier
, io_tier
, 0, 0);
2348 io_tier
= throttle_get_io_policy(&ut
);
2351 io_tier
= throttle_get_io_policy(&ut
);
2353 passive
= throttle_get_passive_io_policy(&ut
);
2356 * Mark if the I/O was upgraded by throttle_get_thread_throttle_level
2357 * while preserving the original issued tier (throttle_get_io_policy
2358 * does not return upgraded tiers)
2360 if (mp
&& io_tier
> throttle_get_thread_throttle_level_internal(ut
, io_tier
)) {
2362 if (!(mp
->mnt_ioflags
& MNT_IOFLAGS_IOSCHED_SUPPORTED
)) {
2365 #else /* CONFIG_IOSCHED */
2367 #endif /* CONFIG_IOSCHED */
2370 if (bp
->b_flags
& B_META
) {
2371 bap
->ba_flags
|= BA_META
;
2376 * For I/O Scheduling, we currently do not have a way to track and expedite metadata I/Os.
2377 * To ensure we dont get into priority inversions due to metadata I/Os, we use the following rules:
2378 * For metadata reads, ceil all I/Os to IOSCHED_METADATA_TIER & mark them passive if the I/O tier was upgraded
2379 * For metadata writes, unconditionally mark them as IOSCHED_METADATA_TIER and passive
2381 if (bap
->ba_flags
& BA_META
) {
2382 if (mp
&& (mp
->mnt_ioflags
& MNT_IOFLAGS_IOSCHED_SUPPORTED
)) {
2383 if (bp
->b_flags
& B_READ
) {
2384 if (io_tier
> IOSCHED_METADATA_TIER
) {
2385 io_tier
= IOSCHED_METADATA_TIER
;
2389 io_tier
= IOSCHED_METADATA_TIER
;
2394 #endif /* CONFIG_IOSCHED */
2396 SET_BUFATTR_IO_TIER(bap
, io_tier
);
2399 bp
->b_flags
|= B_PASSIVE
;
2400 bap
->ba_flags
|= BA_PASSIVE
;
2403 #if !CONFIG_EMBEDDED
2404 if ((curproc
!= NULL
) && ((curproc
->p_flag
& P_DELAYIDLESLEEP
) == P_DELAYIDLESLEEP
)) {
2405 bap
->ba_flags
|= BA_DELAYIDLESLEEP
;
2407 #endif /* !CONFIG_EMBEDDED */
2409 bflags
= bp
->b_flags
;
2411 if (((bflags
& B_READ
) == 0) && ((bflags
& B_ASYNC
) == 0)) {
2412 bufattr_markquickcomplete(bap
);
2415 if (bflags
& B_READ
) {
2418 if (bflags
& B_ASYNC
) {
2422 if (bap
->ba_flags
& BA_META
) {
2424 } else if (bflags
& B_PAGEIO
) {
2425 code
|= DKIO_PAGING
;
2429 code
|= DKIO_THROTTLE
;
2432 code
|= ((io_tier
<< DKIO_TIER_SHIFT
) & DKIO_TIER_MASK
);
2434 if (bflags
& B_PASSIVE
) {
2435 code
|= DKIO_PASSIVE
;
2438 if (bap
->ba_flags
& BA_NOCACHE
) {
2439 code
|= DKIO_NOCACHE
;
2443 code
|= DKIO_TIER_UPGRADE
;
2444 SET(bap
->ba_flags
, BA_IO_TIER_UPGRADE
);
2447 if (kdebug_enable
) {
2448 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON
, FSDBG_CODE(DBG_DKRW
, code
) | DBG_FUNC_NONE
,
2449 buf_kernel_addrperm_addr(bp
), bdev
, buf_blkno(bp
), buf_count(bp
), 0);
2452 thread_update_io_stats(current_thread(), buf_count(bp
), code
);
2455 if (disk_conditioner_mount_is_ssd(mp
)) {
2459 * Partially initialized mounts don't have a final devbsdunit and should not be tracked.
2460 * Verify that devbsdunit is initialized (non-zero) or that 0 is the correct initialized value
2461 * (mnt_throttle_mask is initialized and num_trailing_0 would be 0)
2463 if (mp
->mnt_devbsdunit
|| (mp
->mnt_throttle_mask
!= LOWPRI_MAX_NUM_DEV
- 1 && mp
->mnt_throttle_mask
& 0x1)) {
2466 throttle_info
= &_throttle_io_info
[mp
->mnt_devbsdunit
];
2468 throttle_info
= &_throttle_io_info
[LOWPRI_MAX_NUM_DEV
- 1];
2471 throttle_info_update_internal(throttle_info
, ut
, bflags
, isssd
, inflight
, bap
);
2473 if ((bflags
& B_READ
) == 0) {
2474 microuptime(&throttle_info
->throttle_last_write_timestamp
);
2477 mp
->mnt_last_write_issued_timestamp
= throttle_info
->throttle_last_write_timestamp
;
2478 INCR_PENDING_IO(buf_count(bp
), mp
->mnt_pending_write_size
);
2481 INCR_PENDING_IO(buf_count(bp
), mp
->mnt_pending_read_size
);
2484 * The BootCache may give us special information about
2485 * the IO, so it returns special values that we check
2488 * IO_SATISFIED_BY_CACHE
2489 * The read has been satisfied by the boot cache. Don't
2490 * throttle the thread unnecessarily.
2492 * IO_SHOULD_BE_THROTTLED
2493 * The boot cache is playing back a playlist and this IO
2494 * cut through. Throttle it so we're not cutting through
2495 * the boot cache too often.
2497 * Note that typical strategy routines are defined with
2498 * a void return so we'll get garbage here. In the
2499 * unlikely case the garbage matches our special return
2500 * value, it's not a big deal since we're only adjusting
2501 * the throttling delay.
2503 #define IO_SATISFIED_BY_CACHE ((int)0xcafefeed)
2504 #define IO_SHOULD_BE_THROTTLED ((int)0xcafebeef)
2505 typedef int strategy_fcn_ret_t(struct buf
*bp
);
2507 strategy_ret
= (*(strategy_fcn_ret_t
*)bdevsw
[major(bdev
)].d_strategy
)(bp
);
2509 // disk conditioner needs to track when this I/O actually starts
2510 // which means track it after `strategy` which may include delays
2511 // from inflight I/Os
2512 microuptime(&bp
->b_timestamp_tv
);
2514 if (IO_SATISFIED_BY_CACHE
== strategy_ret
) {
2516 * If this was a throttled IO satisfied by the boot cache,
2517 * don't delay the thread.
2519 throttle_info_reset_window(ut
);
2520 } else if (IO_SHOULD_BE_THROTTLED
== strategy_ret
) {
2522 * If the boot cache indicates this IO should be throttled,
2525 throttle_info_set_initial_window(ut
, throttle_info
, TRUE
, isssd
);
2532 * This is a noop, simply returning what one has been given.
2535 spec_blockmap(__unused
struct vnop_blockmap_args
*ap
)
2542 * Device close routine
2545 spec_close(struct vnop_close_args
*ap
)
2547 struct vnode
*vp
= ap
->a_vp
;
2548 dev_t dev
= vp
->v_rdev
;
2550 int flags
= ap
->a_fflag
;
2551 struct proc
*p
= vfs_context_proc(ap
->a_context
);
2552 struct session
*sessp
;
2554 switch (vp
->v_type
) {
2557 * Hack: a tty device that is a controlling terminal
2558 * has a reference from the session structure.
2559 * We cannot easily tell that a character device is
2560 * a controlling terminal, unless it is the closing
2561 * process' controlling terminal. In that case,
2562 * if the reference count is 1 (this is the very
2565 sessp
= proc_session(p
);
2566 devsw_lock(dev
, S_IFCHR
);
2567 if (sessp
!= SESSION_NULL
) {
2568 if (vp
== sessp
->s_ttyvp
&& vcount(vp
) == 1) {
2569 struct tty
*tp
= TTY_NULL
;
2571 devsw_unlock(dev
, S_IFCHR
);
2572 session_lock(sessp
);
2573 if (vp
== sessp
->s_ttyvp
) {
2574 tp
= SESSION_TP(sessp
);
2575 sessp
->s_ttyvp
= NULL
;
2576 sessp
->s_ttyvid
= 0;
2577 sessp
->s_ttyp
= TTY_NULL
;
2578 sessp
->s_ttypgrpid
= NO_PID
;
2580 session_unlock(sessp
);
2582 if (tp
!= TTY_NULL
) {
2584 * We may have won a race with a proc_exit
2585 * of the session leader, the winner
2586 * clears the flag (even if not set)
2594 devsw_lock(dev
, S_IFCHR
);
2596 session_rele(sessp
);
2599 if (--vp
->v_specinfo
->si_opencount
< 0) {
2600 panic("negative open count (c, %u, %u)", major(dev
), minor(dev
));
2604 * close on last reference or on vnode revoke call
2606 if (vcount(vp
) == 0 || (flags
& IO_REVOKE
) != 0) {
2607 error
= cdevsw
[major(dev
)].d_close(dev
, flags
, S_IFCHR
, p
);
2610 devsw_unlock(dev
, S_IFCHR
);
2615 * If there is more than one outstanding open, don't
2616 * send the close to the device.
2618 devsw_lock(dev
, S_IFBLK
);
2619 if (vcount(vp
) > 1) {
2620 vp
->v_specinfo
->si_opencount
--;
2621 devsw_unlock(dev
, S_IFBLK
);
2624 devsw_unlock(dev
, S_IFBLK
);
2627 * On last close of a block device (that isn't mounted)
2628 * we must invalidate any in core blocks, so that
2629 * we can, for instance, change floppy disks.
2631 if ((error
= spec_fsync_internal(vp
, MNT_WAIT
, ap
->a_context
))) {
2635 error
= buf_invalidateblks(vp
, BUF_WRITE_DATA
, 0, 0);
2640 devsw_lock(dev
, S_IFBLK
);
2642 if (--vp
->v_specinfo
->si_opencount
< 0) {
2643 panic("negative open count (b, %u, %u)", major(dev
), minor(dev
));
2646 if (vcount(vp
) == 0) {
2647 error
= bdevsw
[major(dev
)].d_close(dev
, flags
, S_IFBLK
, p
);
2650 devsw_unlock(dev
, S_IFBLK
);
2654 panic("spec_close: not special");
2662 * Return POSIX pathconf information applicable to special devices.
2665 spec_pathconf(struct vnop_pathconf_args
*ap
)
2667 switch (ap
->a_name
) {
2669 *ap
->a_retval
= LINK_MAX
;
2672 *ap
->a_retval
= MAX_CANON
;
2675 *ap
->a_retval
= MAX_INPUT
;
2678 *ap
->a_retval
= PIPE_BUF
;
2680 case _PC_CHOWN_RESTRICTED
:
2681 *ap
->a_retval
= 200112; /* _POSIX_CHOWN_RESTRICTED */
2684 *ap
->a_retval
= _POSIX_VDISABLE
;
2693 * Special device failed operation
2696 spec_ebadf(__unused
void *dummy
)
2701 /* Blktooff derives file offset from logical block number */
2703 spec_blktooff(struct vnop_blktooff_args
*ap
)
2705 struct vnode
*vp
= ap
->a_vp
;
2707 switch (vp
->v_type
) {
2709 *ap
->a_offset
= (off_t
)-1; /* failure */
2713 printf("spec_blktooff: not implemented for VBLK\n");
2714 *ap
->a_offset
= (off_t
)-1; /* failure */
2718 panic("spec_blktooff type");
2725 /* Offtoblk derives logical block number from file offset */
2727 spec_offtoblk(struct vnop_offtoblk_args
*ap
)
2729 struct vnode
*vp
= ap
->a_vp
;
2731 switch (vp
->v_type
) {
2733 *ap
->a_lblkno
= (daddr64_t
)-1; /* failure */
2737 printf("spec_offtoblk: not implemented for VBLK\n");
2738 *ap
->a_lblkno
= (daddr64_t
)-1; /* failure */
2742 panic("spec_offtoblk type");
2749 static void filt_specdetach(struct knote
*kn
);
2750 static int filt_specevent(struct knote
*kn
, long hint
);
2751 static int filt_spectouch(struct knote
*kn
, struct kevent_internal_s
*kev
);
2752 static int filt_specprocess(struct knote
*kn
, struct filt_process_s
*data
, struct kevent_internal_s
*kev
);
2753 static int filt_specpeek(struct knote
*kn
);
2755 SECURITY_READ_ONLY_EARLY(struct filterops
) spec_filtops
= {
2757 .f_attach
= filt_specattach
,
2758 .f_detach
= filt_specdetach
,
2759 .f_event
= filt_specevent
,
2760 .f_touch
= filt_spectouch
,
2761 .f_process
= filt_specprocess
,
2762 .f_peek
= filt_specpeek
2767 * Given a waitq that is assumed to be embedded within a selinfo structure,
2768 * return the containing selinfo structure. While 'wq' is not really a queue
2769 * element, this macro simply does the offset_of calculation to get back to a
2770 * containing struct given the struct type and member name.
2772 #define selinfo_from_waitq(wq) \
2773 qe_element((wq), struct selinfo, si_waitq)
2776 spec_knote_select_and_link(struct knote
*kn
)
2781 struct waitq_set
*old_wqs
;
2782 uint64_t rsvd
, rsvd_arg
;
2783 uint64_t *rlptr
= NULL
;
2784 struct selinfo
*si
= NULL
;
2787 uth
= get_bsdthread_info(current_thread());
2789 ctx
= vfs_context_current();
2790 vp
= (vnode_t
)kn
->kn_fp
->f_fglob
->fg_data
;
2792 int error
= vnode_getwithvid(vp
, kn
->kn_hookid
);
2794 knote_set_error(kn
, ENOENT
);
2799 * This function may be called many times to link or re-link the
2800 * underlying vnode to the kqueue. If we've already linked the two,
2801 * we will have a valid kn_hook_data which ties us to the underlying
2802 * device's waitq via a the waitq's prepost table object. However,
2803 * devices can abort any select action by calling selthreadclear().
2804 * This is OK because the table object will be invalidated by the
2805 * driver (through a call to selthreadclear), so any attempt to access
2806 * the associated waitq will fail because the table object is invalid.
2808 * Even if we've already registered, we need to pass a pointer
2809 * to a reserved link structure. Otherwise, selrecord() will
2810 * infer that we're in the second pass of select() and won't
2811 * actually do anything!
2813 rsvd
= rsvd_arg
= waitq_link_reserve(NULL
);
2814 rlptr
= (void *)&rsvd_arg
;
2817 * Trick selrecord() into hooking kqueue's wait queue set into the device's
2818 * selinfo wait queue.
2820 old_wqs
= uth
->uu_wqset
;
2821 uth
->uu_wqset
= &(knote_get_kq(kn
)->kq_wqs
);
2824 * Be sure that the waitq set is linked
2825 * before calling select to avoid possible
2826 * allocation under spinlocks.
2828 waitq_set_lazy_init_link(uth
->uu_wqset
);
2831 * Now these are the laws of VNOP_SELECT, as old and as true as the sky,
2832 * And the device that shall keep it may prosper, but the device that shall
2833 * break it must receive ENODEV:
2835 * 1. Take a lock to protect against other selects on the same vnode.
2836 * 2. Return 1 if data is ready to be read.
2837 * 3. Return 0 and call `selrecord` on a handy `selinfo` structure if there
2839 * 4. Call `selwakeup` when the vnode has an active `selrecord` and data
2840 * can be read or written (depending on the seltype).
2841 * 5. If there's a `selrecord` and no corresponding `selwakeup`, but the
2842 * vnode is going away, call `selthreadclear`.
2844 selres
= VNOP_SELECT(vp
, knote_get_seltype(kn
), 0, rlptr
, ctx
);
2845 uth
->uu_wqset
= old_wqs
;
2848 * Make sure to cleanup the reserved link - this guards against
2849 * drivers that may not actually call selrecord().
2851 waitq_link_release(rsvd
);
2852 if (rsvd
!= rsvd_arg
) {
2853 /* The driver / handler called selrecord() */
2855 memcpy(&wq
, rlptr
, sizeof(void *));
2858 * The waitq is part of the selinfo structure managed by the
2859 * driver. For certain drivers, we want to hook the knote into
2860 * the selinfo structure's si_note field so selwakeup can call
2863 si
= selinfo_from_waitq(wq
);
2866 * The waitq_get_prepost_id() function will (potentially)
2867 * allocate a prepost table object for the waitq and return
2868 * the table object's ID to us. It will also set the
2869 * waitq_prepost_id field within the waitq structure.
2871 * We can just overwrite kn_hook_data because it's simply a
2872 * table ID used to grab a reference when needed.
2874 * We have a reference on the vnode, so we know that the
2875 * device won't go away while we get this ID.
2877 kn
->kn_hook_data
= waitq_get_prepost_id(wq
);
2878 } else if (selres
== 0) {
2880 * The device indicated that there's no data to read, but didn't call
2881 * `selrecord`. Nothing will be notified of changes to this vnode, so
2882 * return an error back to user space, to make it clear that the knote
2885 knote_set_error(kn
, ENODEV
);
2894 filt_spec_common(struct knote
*kn
, int selres
)
2896 if (kn
->kn_vnode_use_ofst
) {
2897 if (kn
->kn_fp
->f_fglob
->fg_offset
>= (uint32_t)selres
) {
2900 kn
->kn_data
= ((uint32_t)selres
) - kn
->kn_fp
->f_fglob
->fg_offset
;
2903 kn
->kn_data
= selres
;
2908 filt_specattach(struct knote
*kn
, __unused
struct kevent_internal_s
*kev
)
2913 vp
= (vnode_t
)kn
->kn_fp
->f_fglob
->fg_data
; /* Already have iocount, and vnode is alive */
2915 assert(vnode_ischr(vp
));
2917 dev
= vnode_specrdev(vp
);
2920 * For a few special kinds of devices, we can attach knotes with
2921 * no restrictions because their "select" vectors return the amount
2922 * of data available. Others require an explicit NOTE_LOWAT with
2923 * data of 1, indicating that the caller doesn't care about actual
2924 * data counts, just an indication that the device has data.
2926 if (!kn
->kn_vnode_kqok
&&
2927 ((kn
->kn_sfflags
& NOTE_LOWAT
) == 0 || kn
->kn_sdata
!= 1)) {
2928 knote_set_error(kn
, EINVAL
);
2933 * This forces the select fallback to call through VNOP_SELECT and hook
2934 * up selinfo on every filter routine.
2936 * Pseudo-terminal controllers are opted out of native kevent support --
2937 * remove this when they get their own EVFILTID.
2939 if (cdevsw_flags
[major(dev
)] & CDEVSW_IS_PTC
) {
2940 kn
->kn_vnode_kqok
= 0;
2943 kn
->kn_filtid
= EVFILTID_SPEC
;
2944 kn
->kn_hook_data
= 0;
2945 kn
->kn_hookid
= vnode_vid(vp
);
2947 knote_markstayactive(kn
);
2948 return spec_knote_select_and_link(kn
);
2952 filt_specdetach(struct knote
*kn
)
2954 knote_clearstayactive(kn
);
2957 * This is potentially tricky: the device's selinfo waitq that was
2958 * tricked into being part of this knote's waitq set may not be a part
2959 * of any other set, and the device itself may have revoked the memory
2960 * in which the waitq was held. We use the knote's kn_hook_data field
2961 * to keep the ID of the waitq's prepost table object. This
2962 * object keeps a pointer back to the waitq, and gives us a safe way
2963 * to decouple the dereferencing of driver allocated memory: if the
2964 * driver goes away (taking the waitq with it) then the prepost table
2965 * object will be invalidated. The waitq details are handled in the
2966 * waitq API invoked here.
2968 if (kn
->kn_hook_data
) {
2969 waitq_unlink_by_prepost_id(kn
->kn_hook_data
, &(knote_get_kq(kn
)->kq_wqs
));
2970 kn
->kn_hook_data
= 0;
2975 filt_specevent(struct knote
*kn
, __unused
long hint
)
2978 * Nothing should call knote or knote_vanish on this knote.
2980 panic("filt_specevent(%p)", kn
);
2985 filt_spectouch(struct knote
*kn
, struct kevent_internal_s
*kev
)
2987 kn
->kn_sdata
= kev
->data
;
2988 kn
->kn_sfflags
= kev
->fflags
;
2990 if (kev
->flags
& EV_ENABLE
) {
2991 return spec_knote_select_and_link(kn
);
2998 filt_specprocess(struct knote
*kn
, struct filt_process_s
*data
, struct kevent_internal_s
*kev
)
3000 #pragma unused(data)
3008 uth
= get_bsdthread_info(current_thread());
3009 ctx
= vfs_context_current();
3010 vp
= (vnode_t
)kn
->kn_fp
->f_fglob
->fg_data
;
3012 error
= vnode_getwithvid(vp
, kn
->kn_hookid
);
3014 kn
->kn_flags
|= (EV_EOF
| EV_ONESHOT
);
3015 *kev
= kn
->kn_kevent
;
3019 selres
= spec_knote_select_and_link(kn
);
3020 filt_spec_common(kn
, selres
);
3024 res
= ((kn
->kn_sfflags
& NOTE_LOWAT
) != 0) ?
3025 (kn
->kn_data
>= kn
->kn_sdata
) : kn
->kn_data
;
3028 *kev
= kn
->kn_kevent
;
3029 if (kn
->kn_flags
& EV_CLEAR
) {
3039 filt_specpeek(struct knote
*kn
)
3043 selres
= spec_knote_select_and_link(kn
);
3044 filt_spec_common(kn
, selres
);
3046 return kn
->kn_data
!= 0;