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
)
258 set_blocksize(struct vnode
*vp
, dev_t dev
)
263 if ((major(dev
) < nblkdev
) && (size
= bdevsw
[major(dev
)].d_psize
)) {
264 rsize
= (*size
)(dev
);
265 if (rsize
<= 0) /* did size fail? */
266 vp
->v_specsize
= DEV_BSIZE
;
268 vp
->v_specsize
= rsize
;
271 vp
->v_specsize
= DEV_BSIZE
;
275 set_fsblocksize(struct vnode
*vp
)
278 if (vp
->v_type
== VBLK
) {
279 dev_t dev
= (dev_t
)vp
->v_rdev
;
280 int maj
= major(dev
);
282 if ((u_int
)maj
>= (u_int
)nblkdev
)
286 set_blocksize(vp
, dev
);
294 * Open a special file.
297 spec_open(struct vnop_open_args
*ap
)
299 struct proc
*p
= vfs_context_proc(ap
->a_context
);
300 kauth_cred_t cred
= vfs_context_ucred(ap
->a_context
);
301 struct vnode
*vp
= ap
->a_vp
;
302 dev_t bdev
, dev
= (dev_t
)vp
->v_rdev
;
303 int maj
= major(dev
);
307 * Don't allow open if fs is mounted -nodev.
309 if (vp
->v_mount
&& (vp
->v_mount
->mnt_flag
& MNT_NODEV
))
312 switch (vp
->v_type
) {
315 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
))
325 /* Never allow writing to /dev/mem or /dev/kmem */
329 * When running in secure mode, do not allow opens for
330 * writing of character devices whose corresponding block
331 * devices are currently mounted.
333 if (securelevel
>= 1) {
334 if ((bdev
= chrtoblk(dev
)) != NODEV
&& check_mountedon(bdev
, VBLK
, &error
))
339 devsw_lock(dev
, S_IFCHR
);
340 error
= (*cdevsw
[maj
].d_open
)(dev
, ap
->a_mode
, S_IFCHR
, p
);
343 vp
->v_specinfo
->si_opencount
++;
346 devsw_unlock(dev
, S_IFCHR
);
348 if (error
== 0 && cdevsw
[maj
].d_type
== D_DISK
&& !vp
->v_un
.vu_specinfo
->si_initted
) {
350 uint64_t throttle_mask
= 0;
351 uint32_t devbsdunit
= 0;
353 if (VNOP_IOCTL(vp
, DKIOCGETTHROTTLEMASK
, (caddr_t
)&throttle_mask
, 0, NULL
) == 0) {
355 if (throttle_mask
!= 0 &&
356 VNOP_IOCTL(vp
, DKIOCISSOLIDSTATE
, (caddr_t
)&isssd
, 0, ap
->a_context
) == 0) {
358 * as a reasonable approximation, only use the lowest bit of the mask
359 * to generate a disk unit number
361 devbsdunit
= num_trailing_0(throttle_mask
);
365 vp
->v_un
.vu_specinfo
->si_isssd
= isssd
;
366 vp
->v_un
.vu_specinfo
->si_devbsdunit
= devbsdunit
;
367 vp
->v_un
.vu_specinfo
->si_throttle_mask
= throttle_mask
;
368 vp
->v_un
.vu_specinfo
->si_throttleable
= 1;
369 vp
->v_un
.vu_specinfo
->si_initted
= 1;
374 if (vp
->v_un
.vu_specinfo
->si_initted
== 0) {
376 vp
->v_un
.vu_specinfo
->si_initted
= 1;
383 if ((u_int
)maj
>= (u_int
)nblkdev
)
386 * When running in very secure mode, do not allow
387 * opens for writing of any disk block devices.
389 if (securelevel
>= 2 && cred
!= FSCRED
&&
390 (ap
->a_mode
& FWRITE
) && bdevsw
[maj
].d_type
== D_DISK
)
393 * Do not allow opens of block devices that are
396 if ( (error
= vfs_mountedon(vp
)) )
399 devsw_lock(dev
, S_IFBLK
);
400 error
= (*bdevsw
[maj
].d_open
)(dev
, ap
->a_mode
, S_IFBLK
, p
);
402 vp
->v_specinfo
->si_opencount
++;
404 devsw_unlock(dev
, S_IFBLK
);
410 u_int32_t size512
= 512;
413 if (!VNOP_IOCTL(vp
, DKIOCGETBLOCKSIZE
, (caddr_t
)&blksize
, 0, ap
->a_context
)) {
414 /* Switch to 512 byte sectors (temporarily) */
416 if (!VNOP_IOCTL(vp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&size512
, FWRITE
, ap
->a_context
)) {
417 /* Get the number of 512 byte physical blocks. */
418 if (!VNOP_IOCTL(vp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
, 0, ap
->a_context
)) {
422 /* If it doesn't set back, we can't recover */
423 if (VNOP_IOCTL(vp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&blksize
, FWRITE
, ap
->a_context
))
429 set_blocksize(vp
, dev
);
432 * Cache the size in bytes of the block device for later
433 * use by spec_write().
436 vp
->v_specdevsize
= blkcnt
* (u_int64_t
)size512
;
438 vp
->v_specdevsize
= (u_int64_t
)0; /* Default: Can't get */
445 panic("spec_open type");
454 spec_read(struct vnop_read_args
*ap
)
456 struct vnode
*vp
= ap
->a_vp
;
457 struct uio
*uio
= ap
->a_uio
;
459 daddr64_t bn
, nextbn
;
467 if (uio
->uio_rw
!= UIO_READ
)
468 panic("spec_read mode");
469 if (UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
))
470 panic("spec_read proc");
472 if (uio_resid(uio
) == 0)
475 switch (vp
->v_type
) {
479 struct _throttle_io_info_t
*throttle_info
= NULL
;
480 int thread_throttle_level
;
481 if (cdevsw
[major(vp
->v_rdev
)].d_type
== D_DISK
&& vp
->v_un
.vu_specinfo
->si_throttleable
) {
482 throttle_info
= &_throttle_io_info
[vp
->v_un
.vu_specinfo
->si_devbsdunit
];
483 thread_throttle_level
= throttle_info_update_internal(throttle_info
, NULL
, 0, vp
->v_un
.vu_specinfo
->si_isssd
, TRUE
, NULL
);
485 error
= (*cdevsw
[major(vp
->v_rdev
)].d_read
)
486 (vp
->v_rdev
, uio
, ap
->a_ioflag
);
489 throttle_info_end_io_internal(throttle_info
, thread_throttle_level
);
496 if (uio
->uio_offset
< 0)
501 devBlockSize
= vp
->v_specsize
;
503 if (devBlockSize
> PAGE_SIZE
)
506 bscale
= PAGE_SIZE
/ devBlockSize
;
507 bsize
= bscale
* devBlockSize
;
510 on
= uio
->uio_offset
% bsize
;
512 bn
= (daddr64_t
)((uio
->uio_offset
/ devBlockSize
) &~ (bscale
- 1));
514 if (vp
->v_speclastr
+ bscale
== bn
) {
515 nextbn
= bn
+ bscale
;
516 error
= buf_breadn(vp
, bn
, (int)bsize
, &nextbn
,
517 (int *)&bsize
, 1, NOCRED
, &bp
);
519 error
= buf_bread(vp
, bn
, (int)bsize
, NOCRED
, &bp
);
522 vp
->v_speclastr
= bn
;
525 n
= bsize
- buf_resid(bp
);
526 if ((on
> n
) || error
) {
532 n
= min((unsigned)(n
- on
), uio_resid(uio
));
534 error
= uiomove((char *)buf_dataptr(bp
) + on
, n
, uio
);
538 } while (error
== 0 && uio_resid(uio
) > 0 && n
!= 0);
542 panic("spec_read type");
553 spec_write(struct vnop_write_args
*ap
)
555 struct vnode
*vp
= ap
->a_vp
;
556 struct uio
*uio
= ap
->a_uio
;
559 int bsize
, blkmask
, bscale
;
567 if (uio
->uio_rw
!= UIO_WRITE
)
568 panic("spec_write mode");
569 if (UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
))
570 panic("spec_write proc");
573 switch (vp
->v_type
) {
577 struct _throttle_io_info_t
*throttle_info
= NULL
;
578 int thread_throttle_level
;
579 if (cdevsw
[major(vp
->v_rdev
)].d_type
== D_DISK
&& vp
->v_un
.vu_specinfo
->si_throttleable
) {
580 throttle_info
= &_throttle_io_info
[vp
->v_un
.vu_specinfo
->si_devbsdunit
];
582 thread_throttle_level
= throttle_info_update_internal(throttle_info
, NULL
, 0, vp
->v_un
.vu_specinfo
->si_isssd
, TRUE
, NULL
);
584 microuptime(&throttle_info
->throttle_last_write_timestamp
);
586 error
= (*cdevsw
[major(vp
->v_rdev
)].d_write
)
587 (vp
->v_rdev
, uio
, ap
->a_ioflag
);
590 throttle_info_end_io_internal(throttle_info
, thread_throttle_level
);
597 if (uio_resid(uio
) == 0)
599 if (uio
->uio_offset
< 0)
602 io_sync
= (ap
->a_ioflag
& IO_SYNC
);
606 devBlockSize
= vp
->v_specsize
;
607 if (devBlockSize
> PAGE_SIZE
)
610 bscale
= PAGE_SIZE
/ devBlockSize
;
611 blkmask
= bscale
- 1;
612 bsize
= bscale
* devBlockSize
;
616 bn
= (daddr64_t
)((uio
->uio_offset
/ devBlockSize
) &~ blkmask
);
617 on
= uio
->uio_offset
% bsize
;
619 n
= min((unsigned)(bsize
- on
), uio_resid(uio
));
622 * Use buf_getblk() as an optimization IFF:
624 * 1) We are reading exactly a block on a block
626 * 2) We know the size of the device from spec_open
627 * 3) The read doesn't span the end of the device
629 * Otherwise, we fall back on buf_bread().
632 vp
->v_specdevsize
!= (u_int64_t
)0 &&
633 (uio
->uio_offset
+ (u_int64_t
)n
) > vp
->v_specdevsize
) {
634 /* reduce the size of the read to what is there */
635 n
= (uio
->uio_offset
+ (u_int64_t
)n
) - vp
->v_specdevsize
;
639 bp
= buf_getblk(vp
, bn
, bsize
, 0, 0, BLK_WRITE
);
641 error
= (int)buf_bread(vp
, bn
, bsize
, NOCRED
, &bp
);
643 /* Translate downstream error for upstream, if needed */
645 error
= (int)buf_error(bp
);
650 n
= min(n
, bsize
- buf_resid(bp
));
652 error
= uiomove((char *)buf_dataptr(bp
) + on
, n
, uio
);
660 error
= buf_bwrite(bp
);
662 if ((n
+ on
) == bsize
)
663 error
= buf_bawrite(bp
);
665 error
= buf_bdwrite(bp
);
667 } while (error
== 0 && uio_resid(uio
) > 0 && n
!= 0);
671 panic("spec_write type");
679 * Device ioctl operation.
682 spec_ioctl(struct vnop_ioctl_args
*ap
)
684 proc_t p
= vfs_context_proc(ap
->a_context
);
685 dev_t dev
= ap
->a_vp
->v_rdev
;
688 KERNEL_DEBUG_CONSTANT(FSDBG_CODE(DBG_IOCTL
, 0) | DBG_FUNC_START
,
689 dev
, ap
->a_command
, ap
->a_fflag
, ap
->a_vp
->v_type
, 0);
691 switch (ap
->a_vp
->v_type
) {
694 retval
= (*cdevsw
[major(dev
)].d_ioctl
)(dev
, ap
->a_command
, ap
->a_data
,
699 retval
= (*bdevsw
[major(dev
)].d_ioctl
)(dev
, ap
->a_command
, ap
->a_data
, ap
->a_fflag
, p
);
700 if (!retval
&& ap
->a_command
== DKIOCSETBLOCKSIZE
)
701 ap
->a_vp
->v_specsize
= *(uint32_t *)ap
->a_data
;
708 KERNEL_DEBUG_CONSTANT(FSDBG_CODE(DBG_IOCTL
, 0) | DBG_FUNC_END
,
709 dev
, ap
->a_command
, ap
->a_fflag
, retval
, 0);
715 spec_select(struct vnop_select_args
*ap
)
717 proc_t p
= vfs_context_proc(ap
->a_context
);
720 switch (ap
->a_vp
->v_type
) {
723 return (1); /* XXX */
726 dev
= ap
->a_vp
->v_rdev
;
727 return (*cdevsw
[major(dev
)].d_select
)(dev
, ap
->a_which
, ap
->a_wql
, p
);
731 static int filt_specattach(struct knote
*kn
, struct kevent_internal_s
*kev
);
734 spec_kqfilter(vnode_t vp
, struct knote
*kn
, struct kevent_internal_s
*kev
)
738 assert(vnode_ischr(vp
));
740 dev
= vnode_specrdev(vp
);
744 * Try a bpf device, as defined in bsd/net/bpf.c
745 * If it doesn't error out the attach, then it
746 * claimed it. Otherwise, fall through and try
749 int32_t tmp_flags
= kn
->kn_flags
;
750 int64_t tmp_data
= kn
->kn_data
;
753 res
= bpfkqfilter(dev
, kn
);
754 if ((kn
->kn_flags
& EV_ERROR
) == 0) {
757 kn
->kn_flags
= tmp_flags
;
758 kn
->kn_data
= tmp_data
;
761 if (major(dev
) > nchrdev
) {
762 knote_set_error(kn
, ENXIO
);
766 kn
->kn_vnode_kqok
= !!(cdevsw_flags
[major(dev
)] & CDEVSW_SELECT_KQUEUE
);
767 kn
->kn_vnode_use_ofst
= !!(cdevsw_flags
[major(dev
)] & CDEVSW_USE_OFFSET
);
769 if (cdevsw_flags
[major(dev
)] & CDEVSW_IS_PTS
) {
770 kn
->kn_filtid
= EVFILTID_PTSD
;
771 return ptsd_kqfilter(dev
, kn
);
772 } else if (cdevsw_flags
[major(dev
)] & CDEVSW_IS_PTC
) {
773 kn
->kn_filtid
= EVFILTID_PTMX
;
774 return ptmx_kqfilter(dev
, kn
);
775 } else if (cdevsw
[major(dev
)].d_type
== D_TTY
&& kn
->kn_vnode_kqok
) {
777 * TTYs from drivers that use struct ttys use their own filter
778 * routines. The PTC driver doesn't use the tty for character
779 * counts, so it must go through the select fallback.
781 kn
->kn_filtid
= EVFILTID_TTY
;
782 return knote_fops(kn
)->f_attach(kn
, kev
);
785 /* Try to attach to other char special devices */
786 return filt_specattach(kn
, kev
);
790 * Synch buffers associated with a block device
793 spec_fsync_internal(vnode_t vp
, int waitfor
, __unused vfs_context_t context
)
795 if (vp
->v_type
== VCHR
)
798 * Flush all dirty buffers associated with a block device.
800 buf_flushdirtyblks(vp
, (waitfor
== MNT_WAIT
|| waitfor
== MNT_DWAIT
), 0, "spec_fsync");
806 spec_fsync(struct vnop_fsync_args
*ap
)
808 return spec_fsync_internal(ap
->a_vp
, ap
->a_waitfor
, ap
->a_context
);
813 * Just call the device strategy routine
815 void throttle_init(void);
819 #define DEBUG_ALLOC_THROTTLE_INFO(format, debug_info, args...) \
821 if ((debug_info)->alloc) \
822 printf("%s: "format, __FUNCTION__, ## args); \
826 #define DEBUG_ALLOC_THROTTLE_INFO(format, debug_info, args...)
830 SYSCTL_INT(_debug
, OID_AUTO
, lowpri_throttle_tier1_window_msecs
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &throttle_windows_msecs
[THROTTLE_LEVEL_TIER1
], 0, "");
831 SYSCTL_INT(_debug
, OID_AUTO
, lowpri_throttle_tier2_window_msecs
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &throttle_windows_msecs
[THROTTLE_LEVEL_TIER2
], 0, "");
832 SYSCTL_INT(_debug
, OID_AUTO
, lowpri_throttle_tier3_window_msecs
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &throttle_windows_msecs
[THROTTLE_LEVEL_TIER3
], 0, "");
834 SYSCTL_INT(_debug
, OID_AUTO
, lowpri_throttle_tier1_io_period_msecs
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &throttle_io_period_msecs
[THROTTLE_LEVEL_TIER1
], 0, "");
835 SYSCTL_INT(_debug
, OID_AUTO
, lowpri_throttle_tier2_io_period_msecs
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &throttle_io_period_msecs
[THROTTLE_LEVEL_TIER2
], 0, "");
836 SYSCTL_INT(_debug
, OID_AUTO
, lowpri_throttle_tier3_io_period_msecs
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &throttle_io_period_msecs
[THROTTLE_LEVEL_TIER3
], 0, "");
838 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, "");
839 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, "");
840 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, "");
842 SYSCTL_INT(_debug
, OID_AUTO
, lowpri_throttle_enabled
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &lowpri_throttle_enabled
, 0, "");
845 static lck_grp_t
*throttle_lock_grp
;
846 static lck_attr_t
*throttle_lock_attr
;
847 static lck_grp_attr_t
*throttle_lock_grp_attr
;
851 * throttled I/O helper function
852 * convert the index of the lowest set bit to a device index
855 num_trailing_0(uint64_t n
)
858 * since in most cases the number of trailing 0s is very small,
859 * we simply counting sequentially from the lowest bit
862 return sizeof(n
) * 8;
864 while (!ISSET(n
, 1)) {
873 * Release the reference and if the item was allocated and this is the last
874 * reference then free it.
876 * This routine always returns the old value.
879 throttle_info_rel(struct _throttle_io_info_t
*info
)
881 SInt32 oldValue
= OSDecrementAtomic(&info
->throttle_refcnt
);
883 DEBUG_ALLOC_THROTTLE_INFO("refcnt = %d info = %p\n",
884 info
, (int)(oldValue
-1), info
);
886 /* The reference count just went negative, very bad */
888 panic("throttle info ref cnt went negative!");
891 * Once reference count is zero, no one else should be able to take a
894 if ((info
->throttle_refcnt
== 0) && (info
->throttle_alloc
)) {
895 DEBUG_ALLOC_THROTTLE_INFO("Freeing info = %p\n", info
);
897 lck_mtx_destroy(&info
->throttle_lock
, throttle_lock_grp
);
905 * Just take a reference on the throttle info structure.
907 * This routine always returns the old value.
910 throttle_info_ref(struct _throttle_io_info_t
*info
)
912 SInt32 oldValue
= OSIncrementAtomic(&info
->throttle_refcnt
);
914 DEBUG_ALLOC_THROTTLE_INFO("refcnt = %d info = %p\n",
915 info
, (int)(oldValue
-1), info
);
916 /* Allocated items should never have a reference of zero */
917 if (info
->throttle_alloc
&& (oldValue
== 0))
918 panic("Taking a reference without calling create throttle info!\n");
924 * on entry the throttle_lock is held...
925 * this function is responsible for taking
926 * and dropping the reference on the info
927 * structure which will keep it from going
928 * away while the timer is running if it
929 * happens to have been dynamically allocated by
930 * a network fileystem kext which is now trying
934 throttle_timer_start(struct _throttle_io_info_t
*info
, boolean_t update_io_count
, int wakelevel
)
936 struct timeval elapsed
;
938 struct timeval period
;
939 uint64_t elapsed_msecs
;
943 boolean_t throttled
= FALSE
;
944 boolean_t need_timer
= FALSE
;
948 if (update_io_count
== TRUE
) {
949 info
->throttle_io_count_begin
= info
->throttle_io_count
;
950 info
->throttle_io_period_num
++;
952 while (wakelevel
>= THROTTLE_LEVEL_THROTTLED
)
953 info
->throttle_start_IO_period_timestamp
[wakelevel
--] = now
;
955 info
->throttle_min_timer_deadline
= now
;
957 msecs
= info
->throttle_io_periods
[THROTTLE_LEVEL_THROTTLED
];
958 period
.tv_sec
= msecs
/ 1000;
959 period
.tv_usec
= (msecs
% 1000) * 1000;
961 timevaladd(&info
->throttle_min_timer_deadline
, &period
);
963 for (throttle_level
= THROTTLE_LEVEL_START
; throttle_level
< THROTTLE_LEVEL_END
; throttle_level
++) {
966 timevalsub(&elapsed
, &info
->throttle_window_start_timestamp
[throttle_level
]);
967 elapsed_msecs
= (uint64_t)elapsed
.tv_sec
* (uint64_t)1000 + (elapsed
.tv_usec
/ 1000);
969 for (level
= throttle_level
+ 1; level
<= THROTTLE_LEVEL_END
; level
++) {
971 if (!TAILQ_EMPTY(&info
->throttle_uthlist
[level
])) {
973 if (elapsed_msecs
< (uint64_t)throttle_windows_msecs
[level
] || info
->throttle_inflight_count
[throttle_level
]) {
975 * we had an I/O occur at a higher priority tier within
976 * this tier's throttle window
981 * we assume that the windows are the same or longer
982 * as we drop through the throttling tiers... thus
983 * we can stop looking once we run into a tier with
984 * threads to schedule regardless of whether it's
985 * still in its throttling window or not
990 if (throttled
== TRUE
)
993 if (throttled
== TRUE
) {
994 uint64_t deadline
= 0;
995 struct timeval target
;
996 struct timeval min_target
;
999 * we've got at least one tier still in a throttled window
1000 * so we need a timer running... compute the next deadline
1003 for (level
= throttle_level
+1; level
<= THROTTLE_LEVEL_END
; level
++) {
1005 if (TAILQ_EMPTY(&info
->throttle_uthlist
[level
]))
1008 target
= info
->throttle_start_IO_period_timestamp
[level
];
1010 msecs
= info
->throttle_io_periods
[level
];
1011 period
.tv_sec
= msecs
/ 1000;
1012 period
.tv_usec
= (msecs
% 1000) * 1000;
1014 timevaladd(&target
, &period
);
1016 if (need_timer
== FALSE
|| timevalcmp(&target
, &min_target
, <)) {
1017 min_target
= target
;
1021 if (timevalcmp(&info
->throttle_min_timer_deadline
, &now
, >)) {
1022 if (timevalcmp(&info
->throttle_min_timer_deadline
, &min_target
, >))
1023 min_target
= info
->throttle_min_timer_deadline
;
1026 if (info
->throttle_timer_active
) {
1027 if (thread_call_cancel(info
->throttle_timer_call
) == FALSE
) {
1029 * couldn't kill the timer because it's already
1030 * been dispatched, so don't try to start a new
1031 * one... once we drop the lock, the timer will
1032 * proceed and eventually re-run this function
1036 info
->throttle_timer_active
= 0;
1038 if (need_timer
== TRUE
) {
1040 * This is defined as an int (32-bit) rather than a 64-bit
1041 * value because it would need a really big period in the
1042 * order of ~500 days to overflow this. So, we let this be
1043 * 32-bit which allows us to use the clock_interval_to_deadline()
1048 if (info
->throttle_timer_ref
== 0) {
1050 * take a reference for the timer
1052 throttle_info_ref(info
);
1054 info
->throttle_timer_ref
= 1;
1056 elapsed
= min_target
;
1057 timevalsub(&elapsed
, &now
);
1058 target_msecs
= elapsed
.tv_sec
* 1000 + elapsed
.tv_usec
/ 1000;
1060 if (target_msecs
<= 0) {
1062 * we may have computed a deadline slightly in the past
1063 * due to various factors... if so, just set the timer
1064 * to go off in the near future (we don't need to be precise)
1068 clock_interval_to_deadline(target_msecs
, 1000000, &deadline
);
1070 thread_call_enter_delayed(info
->throttle_timer_call
, deadline
);
1071 info
->throttle_timer_active
= 1;
1074 return (throttle_level
);
1079 throttle_timer(struct _throttle_io_info_t
*info
)
1081 uthread_t ut
, utlist
;
1082 struct timeval elapsed
;
1084 uint64_t elapsed_msecs
;
1088 caddr_t wake_address
= NULL
;
1089 boolean_t update_io_count
= FALSE
;
1090 boolean_t need_wakeup
= FALSE
;
1091 boolean_t need_release
= FALSE
;
1094 lck_mtx_lock(&info
->throttle_lock
);
1096 info
->throttle_timer_active
= 0;
1100 timevalsub(&elapsed
, &info
->throttle_start_IO_period_timestamp
[THROTTLE_LEVEL_THROTTLED
]);
1101 elapsed_msecs
= (uint64_t)elapsed
.tv_sec
* (uint64_t)1000 + (elapsed
.tv_usec
/ 1000);
1103 if (elapsed_msecs
>= (uint64_t)info
->throttle_io_periods
[THROTTLE_LEVEL_THROTTLED
]) {
1105 wake_level
= info
->throttle_next_wake_level
;
1107 for (level
= THROTTLE_LEVEL_START
; level
< THROTTLE_LEVEL_END
; level
++) {
1110 timevalsub(&elapsed
, &info
->throttle_start_IO_period_timestamp
[wake_level
]);
1111 elapsed_msecs
= (uint64_t)elapsed
.tv_sec
* (uint64_t)1000 + (elapsed
.tv_usec
/ 1000);
1113 if (elapsed_msecs
>= (uint64_t)info
->throttle_io_periods
[wake_level
] && !TAILQ_EMPTY(&info
->throttle_uthlist
[wake_level
])) {
1115 * we're closing out the current IO period...
1116 * if we have a waiting thread, wake it up
1117 * after we have reset the I/O window info
1120 update_io_count
= TRUE
;
1122 info
->throttle_next_wake_level
= wake_level
- 1;
1124 if (info
->throttle_next_wake_level
== THROTTLE_LEVEL_START
)
1125 info
->throttle_next_wake_level
= THROTTLE_LEVEL_END
;
1131 if (wake_level
== THROTTLE_LEVEL_START
)
1132 wake_level
= THROTTLE_LEVEL_END
;
1135 if (need_wakeup
== TRUE
) {
1136 if (!TAILQ_EMPTY(&info
->throttle_uthlist
[wake_level
])) {
1138 ut
= (uthread_t
)TAILQ_FIRST(&info
->throttle_uthlist
[wake_level
]);
1139 TAILQ_REMOVE(&info
->throttle_uthlist
[wake_level
], ut
, uu_throttlelist
);
1140 ut
->uu_on_throttlelist
= THROTTLE_LEVEL_NONE
;
1141 ut
->uu_is_throttled
= FALSE
;
1143 wake_address
= (caddr_t
)&ut
->uu_on_throttlelist
;
1146 wake_level
= THROTTLE_LEVEL_START
;
1148 throttle_level
= throttle_timer_start(info
, update_io_count
, wake_level
);
1150 if (wake_address
!= NULL
)
1151 wakeup(wake_address
);
1153 for (level
= THROTTLE_LEVEL_THROTTLED
; level
<= throttle_level
; level
++) {
1155 TAILQ_FOREACH_SAFE(ut
, &info
->throttle_uthlist
[level
], uu_throttlelist
, utlist
) {
1157 TAILQ_REMOVE(&info
->throttle_uthlist
[level
], ut
, uu_throttlelist
);
1158 ut
->uu_on_throttlelist
= THROTTLE_LEVEL_NONE
;
1159 ut
->uu_is_throttled
= FALSE
;
1161 wakeup(&ut
->uu_on_throttlelist
);
1164 if (info
->throttle_timer_active
== 0 && info
->throttle_timer_ref
) {
1165 info
->throttle_timer_ref
= 0;
1166 need_release
= TRUE
;
1168 lck_mtx_unlock(&info
->throttle_lock
);
1170 if (need_release
== TRUE
)
1171 throttle_info_rel(info
);
1176 throttle_add_to_list(struct _throttle_io_info_t
*info
, uthread_t ut
, int mylevel
, boolean_t insert_tail
)
1178 boolean_t start_timer
= FALSE
;
1179 int level
= THROTTLE_LEVEL_START
;
1181 if (TAILQ_EMPTY(&info
->throttle_uthlist
[mylevel
])) {
1182 info
->throttle_start_IO_period_timestamp
[mylevel
] = info
->throttle_last_IO_timestamp
[mylevel
];
1186 if (insert_tail
== TRUE
)
1187 TAILQ_INSERT_TAIL(&info
->throttle_uthlist
[mylevel
], ut
, uu_throttlelist
);
1189 TAILQ_INSERT_HEAD(&info
->throttle_uthlist
[mylevel
], ut
, uu_throttlelist
);
1191 ut
->uu_on_throttlelist
= mylevel
;
1193 if (start_timer
== TRUE
) {
1194 /* we may need to start or rearm the timer */
1195 level
= throttle_timer_start(info
, FALSE
, THROTTLE_LEVEL_START
);
1197 if (level
== THROTTLE_LEVEL_END
) {
1198 if (ut
->uu_on_throttlelist
>= THROTTLE_LEVEL_THROTTLED
) {
1199 TAILQ_REMOVE(&info
->throttle_uthlist
[ut
->uu_on_throttlelist
], ut
, uu_throttlelist
);
1201 ut
->uu_on_throttlelist
= THROTTLE_LEVEL_NONE
;
1209 throttle_init_throttle_window(void)
1211 int throttle_window_size
;
1214 * The hierarchy of throttle window values is as follows:
1216 * - Device tree properties
1218 * All values are specified in msecs.
1221 /* Override global values with device-tree properties */
1222 if (PE_get_default("kern.io_throttle_window_tier1", &throttle_window_size
, sizeof(throttle_window_size
)))
1223 throttle_windows_msecs
[THROTTLE_LEVEL_TIER1
] = throttle_window_size
;
1225 if (PE_get_default("kern.io_throttle_window_tier2", &throttle_window_size
, sizeof(throttle_window_size
)))
1226 throttle_windows_msecs
[THROTTLE_LEVEL_TIER2
] = throttle_window_size
;
1228 if (PE_get_default("kern.io_throttle_window_tier3", &throttle_window_size
, sizeof(throttle_window_size
)))
1229 throttle_windows_msecs
[THROTTLE_LEVEL_TIER3
] = throttle_window_size
;
1231 /* Override with boot-args */
1232 if (PE_parse_boot_argn("io_throttle_window_tier1", &throttle_window_size
, sizeof(throttle_window_size
)))
1233 throttle_windows_msecs
[THROTTLE_LEVEL_TIER1
] = throttle_window_size
;
1235 if (PE_parse_boot_argn("io_throttle_window_tier2", &throttle_window_size
, sizeof(throttle_window_size
)))
1236 throttle_windows_msecs
[THROTTLE_LEVEL_TIER2
] = throttle_window_size
;
1238 if (PE_parse_boot_argn("io_throttle_window_tier3", &throttle_window_size
, sizeof(throttle_window_size
)))
1239 throttle_windows_msecs
[THROTTLE_LEVEL_TIER3
] = throttle_window_size
;
1243 throttle_init_throttle_period(struct _throttle_io_info_t
*info
, boolean_t isssd
)
1245 int throttle_period_size
;
1248 * The hierarchy of throttle period values is as follows:
1250 * - Device tree properties
1252 * All values are specified in msecs.
1255 /* Assign global defaults */
1256 if ((isssd
== TRUE
) && (info
->throttle_is_fusion_with_priority
== 0))
1257 info
->throttle_io_periods
= &throttle_io_period_ssd_msecs
[0];
1259 info
->throttle_io_periods
= &throttle_io_period_msecs
[0];
1261 /* Override global values with device-tree properties */
1262 if (PE_get_default("kern.io_throttle_period_tier1", &throttle_period_size
, sizeof(throttle_period_size
)))
1263 info
->throttle_io_periods
[THROTTLE_LEVEL_TIER1
] = throttle_period_size
;
1265 if (PE_get_default("kern.io_throttle_period_tier2", &throttle_period_size
, sizeof(throttle_period_size
)))
1266 info
->throttle_io_periods
[THROTTLE_LEVEL_TIER2
] = throttle_period_size
;
1268 if (PE_get_default("kern.io_throttle_period_tier3", &throttle_period_size
, sizeof(throttle_period_size
)))
1269 info
->throttle_io_periods
[THROTTLE_LEVEL_TIER3
] = throttle_period_size
;
1271 /* Override with boot-args */
1272 if (PE_parse_boot_argn("io_throttle_period_tier1", &throttle_period_size
, sizeof(throttle_period_size
)))
1273 info
->throttle_io_periods
[THROTTLE_LEVEL_TIER1
] = throttle_period_size
;
1275 if (PE_parse_boot_argn("io_throttle_period_tier2", &throttle_period_size
, sizeof(throttle_period_size
)))
1276 info
->throttle_io_periods
[THROTTLE_LEVEL_TIER2
] = throttle_period_size
;
1278 if (PE_parse_boot_argn("io_throttle_period_tier3", &throttle_period_size
, sizeof(throttle_period_size
)))
1279 info
->throttle_io_periods
[THROTTLE_LEVEL_TIER3
] = throttle_period_size
;
1284 extern void vm_io_reprioritize_init(void);
1285 int iosched_enabled
= 1;
1291 struct _throttle_io_info_t
*info
;
1298 * allocate lock group attribute and group
1300 throttle_lock_grp_attr
= lck_grp_attr_alloc_init();
1301 throttle_lock_grp
= lck_grp_alloc_init("throttle I/O", throttle_lock_grp_attr
);
1303 /* Update throttle parameters based on device tree configuration */
1304 throttle_init_throttle_window();
1307 * allocate the lock attribute
1309 throttle_lock_attr
= lck_attr_alloc_init();
1311 for (i
= 0; i
< LOWPRI_MAX_NUM_DEV
; i
++) {
1312 info
= &_throttle_io_info
[i
];
1314 lck_mtx_init(&info
->throttle_lock
, throttle_lock_grp
, throttle_lock_attr
);
1315 info
->throttle_timer_call
= thread_call_allocate((thread_call_func_t
)throttle_timer
, (thread_call_param_t
)info
);
1317 for (level
= 0; level
<= THROTTLE_LEVEL_END
; level
++) {
1318 TAILQ_INIT(&info
->throttle_uthlist
[level
]);
1319 info
->throttle_last_IO_pid
[level
] = 0;
1320 info
->throttle_inflight_count
[level
] = 0;
1322 info
->throttle_next_wake_level
= THROTTLE_LEVEL_END
;
1323 info
->throttle_disabled
= 0;
1324 info
->throttle_is_fusion_with_priority
= 0;
1327 if (PE_parse_boot_argn("iosched", &iosched
, sizeof(iosched
))) {
1328 iosched_enabled
= iosched
;
1330 if (iosched_enabled
) {
1331 /* Initialize I/O Reprioritization mechanism */
1332 vm_io_reprioritize_init();
1338 sys_override_io_throttle(int flag
)
1340 if (flag
== THROTTLE_IO_ENABLE
)
1341 lowpri_throttle_enabled
= 1;
1343 if (flag
== THROTTLE_IO_DISABLE
)
1344 lowpri_throttle_enabled
= 0;
1347 int rethrottle_wakeups
= 0;
1350 * the uu_rethrottle_lock is used to synchronize this function
1351 * with "throttle_lowpri_io" which is where a throttled thread
1352 * will block... that function will grab this lock before beginning
1353 * it's decision making process concerning the need to block, and
1354 * hold it through the assert_wait. When that thread is awakened
1355 * for any reason (timer or rethrottle), it will reacquire the
1356 * uu_rethrottle_lock before determining if it really is ok for
1357 * it to now run. This is the point at which the thread could
1358 * enter a different throttling queue and reblock or return from
1359 * the throttle w/o having waited out it's entire throttle if
1360 * the rethrottle has now moved it out of any currently
1361 * active throttle window.
1365 * 1 - This may be called with the task lock held.
1366 * 2 - This may be called with preemption and interrupts disabled
1367 * in the kqueue wakeup path so we can't take the throttle_lock which is a mutex
1368 * 3 - This cannot safely dereference uu_throttle_info, as it may
1369 * get deallocated out from under us
1373 rethrottle_thread(uthread_t ut
)
1376 * If uthread doesn't have throttle state, then there's no chance
1377 * of it needing a rethrottle.
1379 if (ut
->uu_throttle_info
== NULL
)
1382 boolean_t s
= ml_set_interrupts_enabled(FALSE
);
1383 lck_spin_lock(&ut
->uu_rethrottle_lock
);
1385 if (ut
->uu_is_throttled
== FALSE
)
1386 ut
->uu_was_rethrottled
= TRUE
;
1388 int my_new_level
= throttle_get_thread_throttle_level(ut
);
1390 if (my_new_level
!= ut
->uu_on_throttlelist
) {
1392 * ut is currently blocked (as indicated by
1393 * ut->uu_is_throttled == TRUE)
1394 * and we're changing it's throttle level, so
1395 * we need to wake it up.
1397 ut
->uu_is_throttled
= FALSE
;
1398 wakeup(&ut
->uu_on_throttlelist
);
1400 rethrottle_wakeups
++;
1401 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_FSRW
, 102)), thread_tid(ut
->uu_thread
), ut
->uu_on_throttlelist
, my_new_level
, 0, 0);
1404 lck_spin_unlock(&ut
->uu_rethrottle_lock
);
1405 ml_set_interrupts_enabled(s
);
1412 * Create and take a reference on a throttle info structure and return a
1413 * pointer for the file system to use when calling throttle_info_update.
1414 * Calling file system must have a matching release for every create.
1417 throttle_info_create(void)
1419 struct _throttle_io_info_t
*info
;
1422 MALLOC(info
, struct _throttle_io_info_t
*, sizeof(*info
), M_TEMP
, M_ZERO
| M_WAITOK
);
1423 /* Should never happen but just in case */
1426 /* Mark that this one was allocated and needs to be freed */
1427 DEBUG_ALLOC_THROTTLE_INFO("Creating info = %p\n", info
, info
);
1428 info
->throttle_alloc
= TRUE
;
1430 lck_mtx_init(&info
->throttle_lock
, throttle_lock_grp
, throttle_lock_attr
);
1431 info
->throttle_timer_call
= thread_call_allocate((thread_call_func_t
)throttle_timer
, (thread_call_param_t
)info
);
1433 for (level
= 0; level
<= THROTTLE_LEVEL_END
; level
++) {
1434 TAILQ_INIT(&info
->throttle_uthlist
[level
]);
1436 info
->throttle_next_wake_level
= THROTTLE_LEVEL_END
;
1438 /* Take a reference */
1439 OSIncrementAtomic(&info
->throttle_refcnt
);
1446 * Release the throttle info pointer if all the reference are gone. Should be
1447 * called to release reference taken by throttle_info_create
1450 throttle_info_release(void *throttle_info
)
1452 DEBUG_ALLOC_THROTTLE_INFO("Releaseing info = %p\n",
1453 (struct _throttle_io_info_t
*)throttle_info
,
1454 (struct _throttle_io_info_t
*)throttle_info
);
1455 if (throttle_info
) /* Just to be careful */
1456 throttle_info_rel(throttle_info
);
1462 * File Systems that create an info structure, need to call this routine in
1463 * their mount routine (used by cluster code). File Systems that call this in
1464 * their mount routines must call throttle_info_mount_rel in their unmount
1468 throttle_info_mount_ref(mount_t mp
, void *throttle_info
)
1470 if ((throttle_info
== NULL
) || (mp
== NULL
))
1472 throttle_info_ref(throttle_info
);
1475 * We already have a reference release it before adding the new one
1477 if (mp
->mnt_throttle_info
)
1478 throttle_info_rel(mp
->mnt_throttle_info
);
1479 mp
->mnt_throttle_info
= throttle_info
;
1483 * Private KPI routine
1485 * return a handle for accessing throttle_info given a throttle_mask. The
1486 * handle must be released by throttle_info_rel_by_mask
1489 throttle_info_ref_by_mask(uint64_t throttle_mask
, throttle_info_handle_t
*throttle_info_handle
)
1492 struct _throttle_io_info_t
*info
;
1494 if (throttle_info_handle
== NULL
)
1497 dev_index
= num_trailing_0(throttle_mask
);
1498 info
= &_throttle_io_info
[dev_index
];
1499 throttle_info_ref(info
);
1500 *(struct _throttle_io_info_t
**)throttle_info_handle
= info
;
1506 * Private KPI routine
1508 * release the handle obtained by throttle_info_ref_by_mask
1511 throttle_info_rel_by_mask(throttle_info_handle_t throttle_info_handle
)
1514 * for now the handle is just a pointer to _throttle_io_info_t
1516 throttle_info_rel((struct _throttle_io_info_t
*)throttle_info_handle
);
1522 * File Systems that throttle_info_mount_ref, must call this routine in their
1526 throttle_info_mount_rel(mount_t mp
)
1528 if (mp
->mnt_throttle_info
)
1529 throttle_info_rel(mp
->mnt_throttle_info
);
1530 mp
->mnt_throttle_info
= NULL
;
1534 * Reset throttling periods for the given mount point
1536 * private interface used by disk conditioner to reset
1537 * throttling periods when 'is_ssd' status changes
1540 throttle_info_mount_reset_period(mount_t mp
, int isssd
)
1542 struct _throttle_io_info_t
*info
;
1545 info
= &_throttle_io_info
[LOWPRI_MAX_NUM_DEV
- 1];
1546 else if (mp
->mnt_throttle_info
== NULL
)
1547 info
= &_throttle_io_info
[mp
->mnt_devbsdunit
];
1549 info
= mp
->mnt_throttle_info
;
1551 throttle_init_throttle_period(info
, isssd
);
1555 throttle_info_get_last_io_time(mount_t mp
, struct timeval
*tv
)
1557 struct _throttle_io_info_t
*info
;
1560 info
= &_throttle_io_info
[LOWPRI_MAX_NUM_DEV
- 1];
1561 else if (mp
->mnt_throttle_info
== NULL
)
1562 info
= &_throttle_io_info
[mp
->mnt_devbsdunit
];
1564 info
= mp
->mnt_throttle_info
;
1566 *tv
= info
->throttle_last_write_timestamp
;
1570 update_last_io_time(mount_t mp
)
1572 struct _throttle_io_info_t
*info
;
1575 info
= &_throttle_io_info
[LOWPRI_MAX_NUM_DEV
- 1];
1576 else if (mp
->mnt_throttle_info
== NULL
)
1577 info
= &_throttle_io_info
[mp
->mnt_devbsdunit
];
1579 info
= mp
->mnt_throttle_info
;
1581 microuptime(&info
->throttle_last_write_timestamp
);
1583 mp
->mnt_last_write_completed_timestamp
= info
->throttle_last_write_timestamp
;
1587 throttle_get_io_policy(uthread_t
*ut
)
1590 *ut
= get_bsdthread_info(current_thread());
1592 return (proc_get_effective_thread_policy(current_thread(), TASK_POLICY_IO
));
1596 throttle_get_passive_io_policy(uthread_t
*ut
)
1599 *ut
= get_bsdthread_info(current_thread());
1601 return (proc_get_effective_thread_policy(current_thread(), TASK_POLICY_PASSIVE_IO
));
1606 throttle_get_thread_throttle_level(uthread_t ut
)
1608 uthread_t
*ut_p
= (ut
== NULL
) ? &ut
: NULL
;
1609 int io_tier
= throttle_get_io_policy(ut_p
);
1611 return throttle_get_thread_throttle_level_internal(ut
, io_tier
);
1615 * Return a throttle level given an existing I/O tier (such as returned by throttle_get_io_policy)
1618 throttle_get_thread_throttle_level_internal(uthread_t ut
, int io_tier
) {
1619 int thread_throttle_level
= io_tier
;
1620 int user_idle_level
;
1624 /* Bootcache misses should always be throttled */
1625 if (ut
->uu_throttle_bc
== TRUE
)
1626 thread_throttle_level
= THROTTLE_LEVEL_TIER3
;
1629 * Issue tier3 I/O as tier2 when the user is idle
1630 * to allow maintenance tasks to make more progress.
1632 * Assume any positive idle level is enough... for now it's
1633 * only ever 0 or 128 but this is not defined anywhere.
1635 if (thread_throttle_level
>= THROTTLE_LEVEL_TIER3
) {
1636 user_idle_level
= timer_get_user_idle_level();
1637 if (user_idle_level
> 0) {
1638 thread_throttle_level
--;
1642 return (thread_throttle_level
);
1646 * I/O will be throttled if either of the following are true:
1647 * - Higher tiers have in-flight I/O
1648 * - The time delta since the last start/completion of a higher tier is within the throttle window interval
1650 * In-flight I/O is bookended by throttle_info_update_internal/throttle_info_end_io_internal
1653 throttle_io_will_be_throttled_internal(void * throttle_info
, int * mylevel
, int * throttling_level
)
1655 struct _throttle_io_info_t
*info
= throttle_info
;
1656 struct timeval elapsed
;
1658 uint64_t elapsed_msecs
;
1659 int thread_throttle_level
;
1662 if ((thread_throttle_level
= throttle_get_thread_throttle_level(NULL
)) < THROTTLE_LEVEL_THROTTLED
)
1663 return (THROTTLE_DISENGAGED
);
1667 for (throttle_level
= THROTTLE_LEVEL_START
; throttle_level
< thread_throttle_level
; throttle_level
++) {
1668 if (info
->throttle_inflight_count
[throttle_level
]) {
1672 timevalsub(&elapsed
, &info
->throttle_window_start_timestamp
[throttle_level
]);
1673 elapsed_msecs
= (uint64_t)elapsed
.tv_sec
* (uint64_t)1000 + (elapsed
.tv_usec
/ 1000);
1675 if (elapsed_msecs
< (uint64_t)throttle_windows_msecs
[thread_throttle_level
])
1678 if (throttle_level
>= thread_throttle_level
) {
1680 * we're beyond all of the throttle windows
1681 * that affect the throttle level of this thread,
1682 * so go ahead and treat as normal I/O
1684 return (THROTTLE_DISENGAGED
);
1687 *mylevel
= thread_throttle_level
;
1688 if (throttling_level
)
1689 *throttling_level
= throttle_level
;
1691 if (info
->throttle_io_count
!= info
->throttle_io_count_begin
) {
1693 * we've already issued at least one throttleable I/O
1694 * in the current I/O window, so avoid issuing another one
1696 return (THROTTLE_NOW
);
1699 * we're in the throttle window, so
1700 * cut the I/O size back
1702 return (THROTTLE_ENGAGED
);
1706 * If we have a mount point and it has a throttle info pointer then
1707 * use it to do the check, otherwise use the device unit number to find
1708 * the correct throttle info array element.
1711 throttle_io_will_be_throttled(__unused
int lowpri_window_msecs
, mount_t mp
)
1713 struct _throttle_io_info_t
*info
;
1716 * Should we just return zero if no mount point
1719 info
= &_throttle_io_info
[LOWPRI_MAX_NUM_DEV
- 1];
1720 else if (mp
->mnt_throttle_info
== NULL
)
1721 info
= &_throttle_io_info
[mp
->mnt_devbsdunit
];
1723 info
= mp
->mnt_throttle_info
;
1725 if (info
->throttle_is_fusion_with_priority
) {
1726 uthread_t ut
= get_bsdthread_info(current_thread());
1727 if (ut
->uu_lowpri_window
== 0)
1728 return (THROTTLE_DISENGAGED
);
1731 if (info
->throttle_disabled
)
1732 return (THROTTLE_DISENGAGED
);
1734 return throttle_io_will_be_throttled_internal(info
, NULL
, NULL
);
1738 * Routine to increment I/O throttling counters maintained in the proc
1742 throttle_update_proc_stats(pid_t throttling_pid
, int count
)
1744 proc_t throttling_proc
;
1745 proc_t throttled_proc
= current_proc();
1747 /* The throttled_proc is always the current proc; so we are not concerned with refs */
1748 OSAddAtomic64(count
, &(throttled_proc
->was_throttled
));
1750 /* The throttling pid might have exited by now */
1751 throttling_proc
= proc_find(throttling_pid
);
1752 if (throttling_proc
!= PROC_NULL
) {
1753 OSAddAtomic64(count
, &(throttling_proc
->did_throttle
));
1754 proc_rele(throttling_proc
);
1759 * Block until woken up by the throttle timer or by a rethrottle call.
1760 * As long as we hold the throttle_lock while querying the throttle tier, we're
1761 * safe against seeing an old throttle tier after a rethrottle.
1764 throttle_lowpri_io(int sleep_amount
)
1767 struct _throttle_io_info_t
*info
;
1768 int throttle_type
= 0;
1770 int throttling_level
= THROTTLE_LEVEL_NONE
;
1772 uint32_t throttle_io_period_num
= 0;
1773 boolean_t insert_tail
= TRUE
;
1776 ut
= get_bsdthread_info(current_thread());
1778 if (ut
->uu_lowpri_window
== 0)
1781 info
= ut
->uu_throttle_info
;
1784 ut
->uu_throttle_bc
= FALSE
;
1785 ut
->uu_lowpri_window
= 0;
1788 lck_mtx_lock(&info
->throttle_lock
);
1789 assert(ut
->uu_on_throttlelist
< THROTTLE_LEVEL_THROTTLED
);
1791 if (sleep_amount
== 0)
1794 if (sleep_amount
== 1 && ut
->uu_throttle_bc
== FALSE
)
1797 throttle_io_period_num
= info
->throttle_io_period_num
;
1799 ut
->uu_was_rethrottled
= FALSE
;
1801 while ( (throttle_type
= throttle_io_will_be_throttled_internal(info
, &mylevel
, &throttling_level
)) ) {
1803 if (throttle_type
== THROTTLE_ENGAGED
) {
1804 if (sleep_amount
== 0)
1806 if (info
->throttle_io_period_num
< throttle_io_period_num
)
1808 if ((info
->throttle_io_period_num
- throttle_io_period_num
) >= (uint32_t)sleep_amount
)
1812 * keep the same position in the list if "rethrottle_thread" changes our throttle level and
1813 * then puts us back to the original level before we get a chance to run
1815 if (ut
->uu_on_throttlelist
>= THROTTLE_LEVEL_THROTTLED
&& ut
->uu_on_throttlelist
!= mylevel
) {
1817 * must have been awakened via "rethrottle_thread" (the timer pulls us off the list)
1818 * and we've changed our throttling level, so pull ourselves off of the appropriate list
1819 * and make sure we get put on the tail of the new list since we're starting anew w/r to
1820 * the throttling engine
1822 TAILQ_REMOVE(&info
->throttle_uthlist
[ut
->uu_on_throttlelist
], ut
, uu_throttlelist
);
1823 ut
->uu_on_throttlelist
= THROTTLE_LEVEL_NONE
;
1826 if (ut
->uu_on_throttlelist
< THROTTLE_LEVEL_THROTTLED
) {
1827 if (throttle_add_to_list(info
, ut
, mylevel
, insert_tail
) == THROTTLE_LEVEL_END
)
1830 assert(throttling_level
>= THROTTLE_LEVEL_START
&& throttling_level
<= THROTTLE_LEVEL_END
);
1832 s
= ml_set_interrupts_enabled(FALSE
);
1833 lck_spin_lock(&ut
->uu_rethrottle_lock
);
1836 * this is the critical section w/r to our interaction
1837 * with "rethrottle_thread"
1839 if (ut
->uu_was_rethrottled
== TRUE
) {
1841 lck_spin_unlock(&ut
->uu_rethrottle_lock
);
1842 ml_set_interrupts_enabled(s
);
1843 lck_mtx_yield(&info
->throttle_lock
);
1845 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_FSRW
, 103)), thread_tid(ut
->uu_thread
), ut
->uu_on_throttlelist
, 0, 0, 0);
1847 ut
->uu_was_rethrottled
= FALSE
;
1850 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_THROTTLE
, PROCESS_THROTTLED
)) | DBG_FUNC_NONE
,
1851 info
->throttle_last_IO_pid
[throttling_level
], throttling_level
, proc_selfpid(), mylevel
, 0);
1853 if (sleep_cnt
== 0) {
1854 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_FSRW
, 97)) | DBG_FUNC_START
,
1855 throttle_windows_msecs
[mylevel
], info
->throttle_io_periods
[mylevel
], info
->throttle_io_count
, 0, 0);
1856 throttled_count
[mylevel
]++;
1858 ut
->uu_wmesg
= "throttle_lowpri_io";
1860 assert_wait((caddr_t
)&ut
->uu_on_throttlelist
, THREAD_UNINT
);
1862 ut
->uu_is_throttled
= TRUE
;
1863 lck_spin_unlock(&ut
->uu_rethrottle_lock
);
1864 ml_set_interrupts_enabled(s
);
1866 lck_mtx_unlock(&info
->throttle_lock
);
1868 thread_block(THREAD_CONTINUE_NULL
);
1870 ut
->uu_wmesg
= NULL
;
1872 ut
->uu_is_throttled
= FALSE
;
1873 ut
->uu_was_rethrottled
= FALSE
;
1875 lck_mtx_lock(&info
->throttle_lock
);
1879 if (sleep_amount
== 0)
1880 insert_tail
= FALSE
;
1881 else if (info
->throttle_io_period_num
< throttle_io_period_num
||
1882 (info
->throttle_io_period_num
- throttle_io_period_num
) >= (uint32_t)sleep_amount
) {
1883 insert_tail
= FALSE
;
1888 if (ut
->uu_on_throttlelist
>= THROTTLE_LEVEL_THROTTLED
) {
1889 TAILQ_REMOVE(&info
->throttle_uthlist
[ut
->uu_on_throttlelist
], ut
, uu_throttlelist
);
1890 ut
->uu_on_throttlelist
= THROTTLE_LEVEL_NONE
;
1892 lck_mtx_unlock(&info
->throttle_lock
);
1895 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_FSRW
, 97)) | DBG_FUNC_END
,
1896 throttle_windows_msecs
[mylevel
], info
->throttle_io_periods
[mylevel
], info
->throttle_io_count
, 0, 0);
1898 * We update the stats for the last pid which opened a throttle window for the throttled thread.
1899 * This might not be completely accurate since the multiple throttles seen by the lower tier pid
1900 * might have been caused by various higher prio pids. However, updating these stats accurately
1901 * means doing a proc_find while holding the throttle lock which leads to deadlock.
1903 throttle_update_proc_stats(info
->throttle_last_IO_pid
[throttling_level
], sleep_cnt
);
1906 ut
->uu_throttle_info
= NULL
;
1907 ut
->uu_throttle_bc
= FALSE
;
1908 ut
->uu_lowpri_window
= 0;
1910 throttle_info_rel(info
);
1918 * set a kernel thread's IO policy. policy can be:
1919 * IOPOL_NORMAL, IOPOL_THROTTLE, IOPOL_PASSIVE, IOPOL_UTILITY, IOPOL_STANDARD
1921 * explanations about these policies are in the man page of setiopolicy_np
1923 void throttle_set_thread_io_policy(int policy
)
1925 proc_set_thread_policy(current_thread(), TASK_POLICY_INTERNAL
, TASK_POLICY_IOPOL
, policy
);
1928 int throttle_get_thread_effective_io_policy()
1930 return proc_get_effective_thread_policy(current_thread(), TASK_POLICY_IO
);
1933 void throttle_info_reset_window(uthread_t ut
)
1935 struct _throttle_io_info_t
*info
;
1938 ut
= get_bsdthread_info(current_thread());
1940 if ( (info
= ut
->uu_throttle_info
) ) {
1941 throttle_info_rel(info
);
1943 ut
->uu_throttle_info
= NULL
;
1944 ut
->uu_lowpri_window
= 0;
1945 ut
->uu_throttle_bc
= FALSE
;
1950 void throttle_info_set_initial_window(uthread_t ut
, struct _throttle_io_info_t
*info
, boolean_t BC_throttle
, boolean_t isssd
)
1952 if (lowpri_throttle_enabled
== 0 || info
->throttle_disabled
)
1955 if (info
->throttle_io_periods
== 0) {
1956 throttle_init_throttle_period(info
, isssd
);
1958 if (ut
->uu_throttle_info
== NULL
) {
1960 ut
->uu_throttle_info
= info
;
1961 throttle_info_ref(info
);
1962 DEBUG_ALLOC_THROTTLE_INFO("updating info = %p\n", info
, info
);
1964 ut
->uu_lowpri_window
= 1;
1965 ut
->uu_throttle_bc
= BC_throttle
;
1970 * Update inflight IO count and throttling window
1971 * Should be called when an IO is done
1973 * Only affects IO that was sent through spec_strategy
1975 void throttle_info_end_io(buf_t bp
) {
1977 struct bufattr
*bap
;
1978 struct _throttle_io_info_t
*info
;
1982 if (!ISSET(bap
->ba_flags
, BA_STRATEGY_TRACKED_IO
)) {
1985 CLR(bap
->ba_flags
, BA_STRATEGY_TRACKED_IO
);
1987 mp
= buf_vnode(bp
)->v_mount
;
1989 info
= &_throttle_io_info
[mp
->mnt_devbsdunit
];
1991 info
= &_throttle_io_info
[LOWPRI_MAX_NUM_DEV
- 1];
1994 io_tier
= GET_BUFATTR_IO_TIER(bap
);
1995 if (ISSET(bap
->ba_flags
, BA_IO_TIER_UPGRADE
)) {
1999 throttle_info_end_io_internal(info
, io_tier
);
2003 * Decrement inflight count initially incremented by throttle_info_update_internal
2006 void throttle_info_end_io_internal(struct _throttle_io_info_t
*info
, int throttle_level
) {
2007 if (throttle_level
== THROTTLE_LEVEL_NONE
) {
2011 microuptime(&info
->throttle_window_start_timestamp
[throttle_level
]);
2012 OSDecrementAtomic(&info
->throttle_inflight_count
[throttle_level
]);
2013 assert(info
->throttle_inflight_count
[throttle_level
] >= 0);
2017 * If inflight is TRUE and bap is NULL then the caller is responsible for calling
2018 * throttle_info_end_io_internal to avoid leaking in-flight I/O.
2021 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
)
2023 int thread_throttle_level
;
2025 if (lowpri_throttle_enabled
== 0 || info
->throttle_disabled
)
2026 return THROTTLE_LEVEL_NONE
;
2029 ut
= get_bsdthread_info(current_thread());
2031 if (bap
&& inflight
&& !ut
->uu_throttle_bc
) {
2032 thread_throttle_level
= GET_BUFATTR_IO_TIER(bap
);
2033 if (ISSET(bap
->ba_flags
, BA_IO_TIER_UPGRADE
)) {
2034 thread_throttle_level
--;
2037 thread_throttle_level
= throttle_get_thread_throttle_level(ut
);
2040 if (thread_throttle_level
!= THROTTLE_LEVEL_NONE
) {
2041 if(!ISSET(flags
, B_PASSIVE
)) {
2042 info
->throttle_last_IO_pid
[thread_throttle_level
] = proc_selfpid();
2043 if (inflight
&& !ut
->uu_throttle_bc
) {
2045 SET(bap
->ba_flags
, BA_STRATEGY_TRACKED_IO
);
2047 OSIncrementAtomic(&info
->throttle_inflight_count
[thread_throttle_level
]);
2049 microuptime(&info
->throttle_window_start_timestamp
[thread_throttle_level
]);
2051 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_THROTTLE
, OPEN_THROTTLE_WINDOW
)) | DBG_FUNC_NONE
,
2052 current_proc()->p_pid
, thread_throttle_level
, 0, 0, 0);
2054 microuptime(&info
->throttle_last_IO_timestamp
[thread_throttle_level
]);
2058 if (thread_throttle_level
>= THROTTLE_LEVEL_THROTTLED
) {
2060 * I'd really like to do the IOSleep here, but
2061 * we may be holding all kinds of filesystem related locks
2062 * and the pages for this I/O marked 'busy'...
2063 * we don't want to cause a normal task to block on
2064 * one of these locks while we're throttling a task marked
2065 * for low priority I/O... we'll mark the uthread and
2066 * do the delay just before we return from the system
2067 * call that triggered this I/O or from vnode_pagein
2069 OSAddAtomic(1, &info
->throttle_io_count
);
2071 throttle_info_set_initial_window(ut
, info
, FALSE
, isssd
);
2074 return thread_throttle_level
;
2077 void *throttle_info_update_by_mount(mount_t mp
)
2079 struct _throttle_io_info_t
*info
;
2081 boolean_t isssd
= FALSE
;
2083 ut
= get_bsdthread_info(current_thread());
2086 if (disk_conditioner_mount_is_ssd(mp
))
2088 info
= &_throttle_io_info
[mp
->mnt_devbsdunit
];
2090 info
= &_throttle_io_info
[LOWPRI_MAX_NUM_DEV
- 1];
2092 if (!ut
->uu_lowpri_window
)
2093 throttle_info_set_initial_window(ut
, info
, FALSE
, isssd
);
2102 * this is usually called before every I/O, used for throttled I/O
2103 * book keeping. This routine has low overhead and does not sleep
2105 void throttle_info_update(void *throttle_info
, int flags
)
2108 throttle_info_update_internal(throttle_info
, NULL
, flags
, FALSE
, FALSE
, NULL
);
2114 * this is usually called before every I/O, used for throttled I/O
2115 * book keeping. This routine has low overhead and does not sleep
2117 void throttle_info_update_by_mask(void *throttle_info_handle
, int flags
)
2119 void *throttle_info
= throttle_info_handle
;
2122 * for now we only use the lowest bit of the throttle mask, so the
2123 * handle is the same as the throttle_info. Later if we store a
2124 * set of throttle infos in the handle, we will want to loop through
2125 * them and call throttle_info_update in a loop
2127 throttle_info_update(throttle_info
, flags
);
2132 * This routine marks the throttle info as disabled. Used for mount points which
2133 * support I/O scheduling.
2136 void throttle_info_disable_throttle(int devno
, boolean_t isfusion
)
2138 struct _throttle_io_info_t
*info
;
2140 if (devno
< 0 || devno
>= LOWPRI_MAX_NUM_DEV
)
2141 panic("Illegal devno (%d) passed into throttle_info_disable_throttle()", devno
);
2143 info
= &_throttle_io_info
[devno
];
2144 // don't disable software throttling on devices that are part of a fusion device
2145 // and override the software throttle periods to use HDD periods
2147 info
->throttle_is_fusion_with_priority
= isfusion
;
2148 throttle_init_throttle_period(info
, FALSE
);
2150 info
->throttle_disabled
= !info
->throttle_is_fusion_with_priority
;
2156 * KPI routine (private)
2157 * Called to determine if this IO is being throttled to this level so that it can be treated specially
2159 int throttle_info_io_will_be_throttled(void * throttle_info
, int policy
)
2161 struct _throttle_io_info_t
*info
= throttle_info
;
2162 struct timeval elapsed
;
2163 uint64_t elapsed_msecs
;
2165 int thread_throttle_level
;
2169 case IOPOL_THROTTLE
:
2170 thread_throttle_level
= THROTTLE_LEVEL_TIER3
;
2173 thread_throttle_level
= THROTTLE_LEVEL_TIER2
;
2175 case IOPOL_STANDARD
:
2176 thread_throttle_level
= THROTTLE_LEVEL_TIER1
;
2179 thread_throttle_level
= THROTTLE_LEVEL_TIER0
;
2182 for (throttle_level
= THROTTLE_LEVEL_START
; throttle_level
< thread_throttle_level
; throttle_level
++) {
2183 if (info
->throttle_inflight_count
[throttle_level
]) {
2187 microuptime(&elapsed
);
2188 timevalsub(&elapsed
, &info
->throttle_window_start_timestamp
[throttle_level
]);
2189 elapsed_msecs
= (uint64_t)elapsed
.tv_sec
* (uint64_t)1000 + (elapsed
.tv_usec
/ 1000);
2191 if (elapsed_msecs
< (uint64_t)throttle_windows_msecs
[thread_throttle_level
])
2194 if (throttle_level
>= thread_throttle_level
) {
2196 * we're beyond all of the throttle windows
2197 * so go ahead and treat as normal I/O
2199 return (THROTTLE_DISENGAGED
);
2202 * we're in the throttle window
2204 return (THROTTLE_ENGAGED
);
2207 int throttle_lowpri_window(void)
2209 struct uthread
*ut
= get_bsdthread_info(current_thread());
2210 return ut
->uu_lowpri_window
;
2215 int upl_get_cached_tier(void *);
2219 spec_strategy(struct vnop_strategy_args
*ap
)
2228 struct bufattr
*bap
;
2230 struct _throttle_io_info_t
*throttle_info
;
2231 boolean_t isssd
= FALSE
;
2232 boolean_t inflight
= FALSE
;
2233 boolean_t upgrade
= FALSE
;
2236 #if !CONFIG_EMBEDDED
2237 proc_t curproc
= current_proc();
2238 #endif /* !CONFIG_EMBEDDED */
2241 bdev
= buf_device(bp
);
2242 mp
= buf_vnode(bp
)->v_mount
;
2246 if (bp
->b_flags
& B_CLUSTER
) {
2248 io_tier
= upl_get_cached_tier(bp
->b_upl
);
2251 io_tier
= throttle_get_io_policy(&ut
);
2252 #if DEVELOPMENT || DEBUG
2254 int my_io_tier
= throttle_get_io_policy(&ut
);
2256 if (io_tier
!= my_io_tier
)
2257 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);
2261 io_tier
= throttle_get_io_policy(&ut
);
2263 io_tier
= throttle_get_io_policy(&ut
);
2265 passive
= throttle_get_passive_io_policy(&ut
);
2268 * Mark if the I/O was upgraded by throttle_get_thread_throttle_level
2269 * while preserving the original issued tier (throttle_get_io_policy
2270 * does not return upgraded tiers)
2272 if (mp
&& io_tier
> throttle_get_thread_throttle_level_internal(ut
, io_tier
)) {
2274 if (!(mp
->mnt_ioflags
& MNT_IOFLAGS_IOSCHED_SUPPORTED
)) {
2277 #else /* CONFIG_IOSCHED */
2279 #endif /* CONFIG_IOSCHED */
2282 if (bp
->b_flags
& B_META
)
2283 bap
->ba_flags
|= BA_META
;
2287 * For I/O Scheduling, we currently do not have a way to track and expedite metadata I/Os.
2288 * To ensure we dont get into priority inversions due to metadata I/Os, we use the following rules:
2289 * For metadata reads, ceil all I/Os to IOSCHED_METADATA_TIER & mark them passive if the I/O tier was upgraded
2290 * For metadata writes, unconditionally mark them as IOSCHED_METADATA_TIER and passive
2292 if (bap
->ba_flags
& BA_META
) {
2293 if (mp
&& (mp
->mnt_ioflags
& MNT_IOFLAGS_IOSCHED_SUPPORTED
)) {
2294 if (bp
->b_flags
& B_READ
) {
2295 if (io_tier
> IOSCHED_METADATA_TIER
) {
2296 io_tier
= IOSCHED_METADATA_TIER
;
2300 io_tier
= IOSCHED_METADATA_TIER
;
2305 #endif /* CONFIG_IOSCHED */
2307 SET_BUFATTR_IO_TIER(bap
, io_tier
);
2310 bp
->b_flags
|= B_PASSIVE
;
2311 bap
->ba_flags
|= BA_PASSIVE
;
2314 #if !CONFIG_EMBEDDED
2315 if ((curproc
!= NULL
) && ((curproc
->p_flag
& P_DELAYIDLESLEEP
) == P_DELAYIDLESLEEP
))
2316 bap
->ba_flags
|= BA_DELAYIDLESLEEP
;
2317 #endif /* !CONFIG_EMBEDDED */
2319 bflags
= bp
->b_flags
;
2321 if (((bflags
& B_READ
) == 0) && ((bflags
& B_ASYNC
) == 0))
2322 bufattr_markquickcomplete(bap
);
2324 if (bflags
& B_READ
)
2326 if (bflags
& B_ASYNC
)
2329 if (bap
->ba_flags
& BA_META
)
2331 else if (bflags
& B_PAGEIO
)
2332 code
|= DKIO_PAGING
;
2335 code
|= DKIO_THROTTLE
;
2337 code
|= ((io_tier
<< DKIO_TIER_SHIFT
) & DKIO_TIER_MASK
);
2339 if (bflags
& B_PASSIVE
)
2340 code
|= DKIO_PASSIVE
;
2342 if (bap
->ba_flags
& BA_NOCACHE
)
2343 code
|= DKIO_NOCACHE
;
2346 code
|= DKIO_TIER_UPGRADE
;
2347 SET(bap
->ba_flags
, BA_IO_TIER_UPGRADE
);
2350 if (kdebug_enable
) {
2351 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON
, FSDBG_CODE(DBG_DKRW
, code
) | DBG_FUNC_NONE
,
2352 buf_kernel_addrperm_addr(bp
), bdev
, (int)buf_blkno(bp
), buf_count(bp
), 0);
2355 thread_update_io_stats(current_thread(), buf_count(bp
), code
);
2358 if (disk_conditioner_mount_is_ssd(mp
))
2361 * Partially initialized mounts don't have a final devbsdunit and should not be tracked.
2362 * Verify that devbsdunit is initialized (non-zero) or that 0 is the correct initialized value
2363 * (mnt_throttle_mask is initialized and num_trailing_0 would be 0)
2365 if (mp
->mnt_devbsdunit
|| (mp
->mnt_throttle_mask
!= LOWPRI_MAX_NUM_DEV
- 1 && mp
->mnt_throttle_mask
& 0x1)) {
2368 throttle_info
= &_throttle_io_info
[mp
->mnt_devbsdunit
];
2371 throttle_info
= &_throttle_io_info
[LOWPRI_MAX_NUM_DEV
- 1];
2373 throttle_info_update_internal(throttle_info
, ut
, bflags
, isssd
, inflight
, bap
);
2375 if ((bflags
& B_READ
) == 0) {
2376 microuptime(&throttle_info
->throttle_last_write_timestamp
);
2379 mp
->mnt_last_write_issued_timestamp
= throttle_info
->throttle_last_write_timestamp
;
2380 INCR_PENDING_IO(buf_count(bp
), mp
->mnt_pending_write_size
);
2383 INCR_PENDING_IO(buf_count(bp
), mp
->mnt_pending_read_size
);
2386 * The BootCache may give us special information about
2387 * the IO, so it returns special values that we check
2390 * IO_SATISFIED_BY_CACHE
2391 * The read has been satisfied by the boot cache. Don't
2392 * throttle the thread unnecessarily.
2394 * IO_SHOULD_BE_THROTTLED
2395 * The boot cache is playing back a playlist and this IO
2396 * cut through. Throttle it so we're not cutting through
2397 * the boot cache too often.
2399 * Note that typical strategy routines are defined with
2400 * a void return so we'll get garbage here. In the
2401 * unlikely case the garbage matches our special return
2402 * value, it's not a big deal since we're only adjusting
2403 * the throttling delay.
2405 #define IO_SATISFIED_BY_CACHE ((int)0xcafefeed)
2406 #define IO_SHOULD_BE_THROTTLED ((int)0xcafebeef)
2407 typedef int strategy_fcn_ret_t(struct buf
*bp
);
2409 strategy_ret
= (*(strategy_fcn_ret_t
*)bdevsw
[major(bdev
)].d_strategy
)(bp
);
2411 // disk conditioner needs to track when this I/O actually starts
2412 // which means track it after `strategy` which may include delays
2413 // from inflight I/Os
2414 microuptime(&bp
->b_timestamp_tv
);
2416 if (IO_SATISFIED_BY_CACHE
== strategy_ret
) {
2418 * If this was a throttled IO satisfied by the boot cache,
2419 * don't delay the thread.
2421 throttle_info_reset_window(ut
);
2423 } else if (IO_SHOULD_BE_THROTTLED
== strategy_ret
) {
2425 * If the boot cache indicates this IO should be throttled,
2428 throttle_info_set_initial_window(ut
, throttle_info
, TRUE
, isssd
);
2435 * This is a noop, simply returning what one has been given.
2438 spec_blockmap(__unused
struct vnop_blockmap_args
*ap
)
2445 * Device close routine
2448 spec_close(struct vnop_close_args
*ap
)
2450 struct vnode
*vp
= ap
->a_vp
;
2451 dev_t dev
= vp
->v_rdev
;
2453 int flags
= ap
->a_fflag
;
2454 struct proc
*p
= vfs_context_proc(ap
->a_context
);
2455 struct session
*sessp
;
2457 switch (vp
->v_type
) {
2461 * Hack: a tty device that is a controlling terminal
2462 * has a reference from the session structure.
2463 * We cannot easily tell that a character device is
2464 * a controlling terminal, unless it is the closing
2465 * process' controlling terminal. In that case,
2466 * if the reference count is 1 (this is the very
2469 sessp
= proc_session(p
);
2470 devsw_lock(dev
, S_IFCHR
);
2471 if (sessp
!= SESSION_NULL
) {
2472 if (vp
== sessp
->s_ttyvp
&& vcount(vp
) == 1) {
2473 struct tty
*tp
= TTY_NULL
;
2475 devsw_unlock(dev
, S_IFCHR
);
2476 session_lock(sessp
);
2477 if (vp
== sessp
->s_ttyvp
) {
2478 tp
= SESSION_TP(sessp
);
2479 sessp
->s_ttyvp
= NULL
;
2480 sessp
->s_ttyvid
= 0;
2481 sessp
->s_ttyp
= TTY_NULL
;
2482 sessp
->s_ttypgrpid
= NO_PID
;
2484 session_unlock(sessp
);
2486 if (tp
!= TTY_NULL
) {
2488 * We may have won a race with a proc_exit
2489 * of the session leader, the winner
2490 * clears the flag (even if not set)
2498 devsw_lock(dev
, S_IFCHR
);
2500 session_rele(sessp
);
2503 if (--vp
->v_specinfo
->si_opencount
< 0)
2504 panic("negative open count (c, %u, %u)", major(dev
), minor(dev
));
2507 * close on last reference or on vnode revoke call
2509 if (vcount(vp
) == 0 || (flags
& IO_REVOKE
) != 0)
2510 error
= cdevsw
[major(dev
)].d_close(dev
, flags
, S_IFCHR
, p
);
2512 devsw_unlock(dev
, S_IFCHR
);
2517 * If there is more than one outstanding open, don't
2518 * send the close to the device.
2520 devsw_lock(dev
, S_IFBLK
);
2521 if (vcount(vp
) > 1) {
2522 vp
->v_specinfo
->si_opencount
--;
2523 devsw_unlock(dev
, S_IFBLK
);
2526 devsw_unlock(dev
, S_IFBLK
);
2529 * On last close of a block device (that isn't mounted)
2530 * we must invalidate any in core blocks, so that
2531 * we can, for instance, change floppy disks.
2533 if ((error
= spec_fsync_internal(vp
, MNT_WAIT
, ap
->a_context
)))
2536 error
= buf_invalidateblks(vp
, BUF_WRITE_DATA
, 0, 0);
2540 devsw_lock(dev
, S_IFBLK
);
2542 if (--vp
->v_specinfo
->si_opencount
< 0)
2543 panic("negative open count (b, %u, %u)", major(dev
), minor(dev
));
2545 if (vcount(vp
) == 0)
2546 error
= bdevsw
[major(dev
)].d_close(dev
, flags
, S_IFBLK
, p
);
2548 devsw_unlock(dev
, S_IFBLK
);
2552 panic("spec_close: not special");
2560 * Return POSIX pathconf information applicable to special devices.
2563 spec_pathconf(struct vnop_pathconf_args
*ap
)
2566 switch (ap
->a_name
) {
2568 *ap
->a_retval
= LINK_MAX
;
2571 *ap
->a_retval
= MAX_CANON
;
2574 *ap
->a_retval
= MAX_INPUT
;
2577 *ap
->a_retval
= PIPE_BUF
;
2579 case _PC_CHOWN_RESTRICTED
:
2580 *ap
->a_retval
= 200112; /* _POSIX_CHOWN_RESTRICTED */
2583 *ap
->a_retval
= _POSIX_VDISABLE
;
2592 * Special device failed operation
2595 spec_ebadf(__unused
void *dummy
)
2601 /* Blktooff derives file offset from logical block number */
2603 spec_blktooff(struct vnop_blktooff_args
*ap
)
2605 struct vnode
*vp
= ap
->a_vp
;
2607 switch (vp
->v_type
) {
2609 *ap
->a_offset
= (off_t
)-1; /* failure */
2613 printf("spec_blktooff: not implemented for VBLK\n");
2614 *ap
->a_offset
= (off_t
)-1; /* failure */
2618 panic("spec_blktooff type");
2625 /* Offtoblk derives logical block number from file offset */
2627 spec_offtoblk(struct vnop_offtoblk_args
*ap
)
2629 struct vnode
*vp
= ap
->a_vp
;
2631 switch (vp
->v_type
) {
2633 *ap
->a_lblkno
= (daddr64_t
)-1; /* failure */
2637 printf("spec_offtoblk: not implemented for VBLK\n");
2638 *ap
->a_lblkno
= (daddr64_t
)-1; /* failure */
2642 panic("spec_offtoblk type");
2649 static void filt_specdetach(struct knote
*kn
);
2650 static int filt_specevent(struct knote
*kn
, long hint
);
2651 static int filt_spectouch(struct knote
*kn
, struct kevent_internal_s
*kev
);
2652 static int filt_specprocess(struct knote
*kn
, struct filt_process_s
*data
, struct kevent_internal_s
*kev
);
2653 static unsigned filt_specpeek(struct knote
*kn
);
2655 SECURITY_READ_ONLY_EARLY(struct filterops
) spec_filtops
= {
2657 .f_attach
= filt_specattach
,
2658 .f_detach
= filt_specdetach
,
2659 .f_event
= filt_specevent
,
2660 .f_touch
= filt_spectouch
,
2661 .f_process
= filt_specprocess
,
2662 .f_peek
= filt_specpeek
2667 * Given a waitq that is assumed to be embedded within a selinfo structure,
2668 * return the containing selinfo structure. While 'wq' is not really a queue
2669 * element, this macro simply does the offset_of calculation to get back to a
2670 * containing struct given the struct type and member name.
2672 #define selinfo_from_waitq(wq) \
2673 qe_element((wq), struct selinfo, si_waitq)
2676 spec_knote_select_and_link(struct knote
*kn
)
2681 struct waitq_set
*old_wqs
;
2682 uint64_t rsvd
, rsvd_arg
;
2683 uint64_t *rlptr
= NULL
;
2684 struct selinfo
*si
= NULL
;
2687 uth
= get_bsdthread_info(current_thread());
2689 ctx
= vfs_context_current();
2690 vp
= (vnode_t
)kn
->kn_fp
->f_fglob
->fg_data
;
2692 int error
= vnode_getwithvid(vp
, kn
->kn_hookid
);
2694 knote_set_error(kn
, ENOENT
);
2699 * This function may be called many times to link or re-link the
2700 * underlying vnode to the kqueue. If we've already linked the two,
2701 * we will have a valid kn_hook_data which ties us to the underlying
2702 * device's waitq via a the waitq's prepost table object. However,
2703 * devices can abort any select action by calling selthreadclear().
2704 * This is OK because the table object will be invalidated by the
2705 * driver (through a call to selthreadclear), so any attempt to access
2706 * the associated waitq will fail because the table object is invalid.
2708 * Even if we've already registered, we need to pass a pointer
2709 * to a reserved link structure. Otherwise, selrecord() will
2710 * infer that we're in the second pass of select() and won't
2711 * actually do anything!
2713 rsvd
= rsvd_arg
= waitq_link_reserve(NULL
);
2714 rlptr
= (void *)&rsvd_arg
;
2717 * Trick selrecord() into hooking kqueue's wait queue set into the device's
2718 * selinfo wait queue.
2720 old_wqs
= uth
->uu_wqset
;
2721 uth
->uu_wqset
= &(knote_get_kq(kn
)->kq_wqs
);
2723 * Now these are the laws of VNOP_SELECT, as old and as true as the sky,
2724 * And the device that shall keep it may prosper, but the device that shall
2725 * break it must receive ENODEV:
2727 * 1. Take a lock to protect against other selects on the same vnode.
2728 * 2. Return 1 if data is ready to be read.
2729 * 3. Return 0 and call `selrecord` on a handy `selinfo` structure if there
2731 * 4. Call `selwakeup` when the vnode has an active `selrecord` and data
2732 * can be read or written (depending on the seltype).
2733 * 5. If there's a `selrecord` and no corresponding `selwakeup`, but the
2734 * vnode is going away, call `selthreadclear`.
2736 selres
= VNOP_SELECT(vp
, knote_get_seltype(kn
), 0, rlptr
, ctx
);
2737 uth
->uu_wqset
= old_wqs
;
2740 * Make sure to cleanup the reserved link - this guards against
2741 * drivers that may not actually call selrecord().
2743 waitq_link_release(rsvd
);
2744 if (rsvd
!= rsvd_arg
) {
2745 /* The driver / handler called selrecord() */
2747 memcpy(&wq
, rlptr
, sizeof(void *));
2750 * The waitq is part of the selinfo structure managed by the
2751 * driver. For certain drivers, we want to hook the knote into
2752 * the selinfo structure's si_note field so selwakeup can call
2755 si
= selinfo_from_waitq(wq
);
2758 * The waitq_get_prepost_id() function will (potentially)
2759 * allocate a prepost table object for the waitq and return
2760 * the table object's ID to us. It will also set the
2761 * waitq_prepost_id field within the waitq structure.
2763 * We can just overwrite kn_hook_data because it's simply a
2764 * table ID used to grab a reference when needed.
2766 * We have a reference on the vnode, so we know that the
2767 * device won't go away while we get this ID.
2769 kn
->kn_hook_data
= waitq_get_prepost_id(wq
);
2770 } else if (selres
== 0) {
2772 * The device indicated that there's no data to read, but didn't call
2773 * `selrecord`. Nothing will be notified of changes to this vnode, so
2774 * return an error back to user space, to make it clear that the knote
2777 knote_set_error(kn
, ENODEV
);
2785 static void filt_spec_common(struct knote
*kn
, int selres
)
2787 if (kn
->kn_vnode_use_ofst
) {
2788 if (kn
->kn_fp
->f_fglob
->fg_offset
>= (uint32_t)selres
) {
2791 kn
->kn_data
= ((uint32_t)selres
) - kn
->kn_fp
->f_fglob
->fg_offset
;
2794 kn
->kn_data
= selres
;
2799 filt_specattach(struct knote
*kn
, __unused
struct kevent_internal_s
*kev
)
2804 vp
= (vnode_t
)kn
->kn_fp
->f_fglob
->fg_data
; /* Already have iocount, and vnode is alive */
2806 assert(vnode_ischr(vp
));
2808 dev
= vnode_specrdev(vp
);
2811 * For a few special kinds of devices, we can attach knotes with
2812 * no restrictions because their "select" vectors return the amount
2813 * of data available. Others require an explicit NOTE_LOWAT with
2814 * data of 1, indicating that the caller doesn't care about actual
2815 * data counts, just an indication that the device has data.
2817 if (!kn
->kn_vnode_kqok
&&
2818 ((kn
->kn_sfflags
& NOTE_LOWAT
) == 0 || kn
->kn_sdata
!= 1)) {
2819 knote_set_error(kn
, EINVAL
);
2824 * This forces the select fallback to call through VNOP_SELECT and hook
2825 * up selinfo on every filter routine.
2827 * Pseudo-terminal controllers are opted out of native kevent support --
2828 * remove this when they get their own EVFILTID.
2830 if (cdevsw_flags
[major(dev
)] & CDEVSW_IS_PTC
) {
2831 kn
->kn_vnode_kqok
= 0;
2834 kn
->kn_filtid
= EVFILTID_SPEC
;
2835 kn
->kn_hook_data
= 0;
2836 kn
->kn_hookid
= vnode_vid(vp
);
2838 knote_markstayactive(kn
);
2839 return spec_knote_select_and_link(kn
);
2843 filt_specdetach(struct knote
*kn
)
2845 knote_clearstayactive(kn
);
2848 * This is potentially tricky: the device's selinfo waitq that was
2849 * tricked into being part of this knote's waitq set may not be a part
2850 * of any other set, and the device itself may have revoked the memory
2851 * in which the waitq was held. We use the knote's kn_hook_data field
2852 * to keep the ID of the waitq's prepost table object. This
2853 * object keeps a pointer back to the waitq, and gives us a safe way
2854 * to decouple the dereferencing of driver allocated memory: if the
2855 * driver goes away (taking the waitq with it) then the prepost table
2856 * object will be invalidated. The waitq details are handled in the
2857 * waitq API invoked here.
2859 if (kn
->kn_hook_data
) {
2860 waitq_unlink_by_prepost_id(kn
->kn_hook_data
, &(knote_get_kq(kn
)->kq_wqs
));
2861 kn
->kn_hook_data
= 0;
2866 filt_specevent(struct knote
*kn
, __unused
long hint
)
2869 * Nothing should call knote or knote_vanish on this knote.
2871 panic("filt_specevent(%p)", kn
);
2876 filt_spectouch(struct knote
*kn
, struct kevent_internal_s
*kev
)
2878 kn
->kn_sdata
= kev
->data
;
2879 kn
->kn_sfflags
= kev
->fflags
;
2880 if ((kn
->kn_status
& KN_UDATA_SPECIFIC
) == 0)
2881 kn
->kn_udata
= kev
->udata
;
2883 if (kev
->flags
& EV_ENABLE
) {
2884 return spec_knote_select_and_link(kn
);
2891 filt_specprocess(struct knote
*kn
, struct filt_process_s
*data
, struct kevent_internal_s
*kev
)
2893 #pragma unused(data)
2901 uth
= get_bsdthread_info(current_thread());
2902 ctx
= vfs_context_current();
2903 vp
= (vnode_t
)kn
->kn_fp
->f_fglob
->fg_data
;
2905 /* FIXME JMM - locking against touches? */
2907 error
= vnode_getwithvid(vp
, kn
->kn_hookid
);
2909 kn
->kn_flags
|= (EV_EOF
| EV_ONESHOT
);
2910 *kev
= kn
->kn_kevent
;
2914 selres
= spec_knote_select_and_link(kn
);
2915 filt_spec_common(kn
, selres
);
2919 res
= ((kn
->kn_sfflags
& NOTE_LOWAT
) != 0) ?
2920 (kn
->kn_data
>= kn
->kn_sdata
) : kn
->kn_data
;
2923 *kev
= kn
->kn_kevent
;
2924 if (kn
->kn_flags
& EV_CLEAR
) {
2934 filt_specpeek(struct knote
*kn
)
2938 selres
= spec_knote_select_and_link(kn
);
2939 filt_spec_common(kn
, selres
);