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
*);
108 struct vnode
*speclisth
[SPECHSZ
];
110 /* symbolic sleep message strings for devices */
111 char devopn
[] = "devopn";
112 char devio
[] = "devio";
113 char devwait
[] = "devwait";
114 char devin
[] = "devin";
115 char devout
[] = "devout";
116 char devioc
[] = "devioc";
117 char devcls
[] = "devcls";
119 #define VOPFUNC int (*)(void *)
121 int (**spec_vnodeop_p
)(void *);
122 struct vnodeopv_entry_desc spec_vnodeop_entries
[] = {
123 { &vnop_default_desc
, (VOPFUNC
)vn_default_error
},
124 { &vnop_lookup_desc
, (VOPFUNC
)spec_lookup
}, /* lookup */
125 { &vnop_create_desc
, (VOPFUNC
)err_create
}, /* create */
126 { &vnop_mknod_desc
, (VOPFUNC
)err_mknod
}, /* mknod */
127 { &vnop_open_desc
, (VOPFUNC
)spec_open
}, /* open */
128 { &vnop_close_desc
, (VOPFUNC
)spec_close
}, /* close */
129 { &vnop_access_desc
, (VOPFUNC
)spec_access
}, /* access */
130 { &vnop_getattr_desc
, (VOPFUNC
)spec_getattr
}, /* getattr */
131 { &vnop_setattr_desc
, (VOPFUNC
)spec_setattr
}, /* setattr */
132 { &vnop_read_desc
, (VOPFUNC
)spec_read
}, /* read */
133 { &vnop_write_desc
, (VOPFUNC
)spec_write
}, /* write */
134 { &vnop_ioctl_desc
, (VOPFUNC
)spec_ioctl
}, /* ioctl */
135 { &vnop_select_desc
, (VOPFUNC
)spec_select
}, /* select */
136 { &vnop_revoke_desc
, (VOPFUNC
)nop_revoke
}, /* revoke */
137 { &vnop_mmap_desc
, (VOPFUNC
)err_mmap
}, /* mmap */
138 { &vnop_fsync_desc
, (VOPFUNC
)spec_fsync
}, /* fsync */
139 { &vnop_remove_desc
, (VOPFUNC
)err_remove
}, /* remove */
140 { &vnop_link_desc
, (VOPFUNC
)err_link
}, /* link */
141 { &vnop_rename_desc
, (VOPFUNC
)err_rename
}, /* rename */
142 { &vnop_mkdir_desc
, (VOPFUNC
)err_mkdir
}, /* mkdir */
143 { &vnop_rmdir_desc
, (VOPFUNC
)err_rmdir
}, /* rmdir */
144 { &vnop_symlink_desc
, (VOPFUNC
)err_symlink
}, /* symlink */
145 { &vnop_readdir_desc
, (VOPFUNC
)err_readdir
}, /* readdir */
146 { &vnop_readlink_desc
, (VOPFUNC
)err_readlink
}, /* readlink */
147 { &vnop_inactive_desc
, (VOPFUNC
)nop_inactive
}, /* inactive */
148 { &vnop_reclaim_desc
, (VOPFUNC
)nop_reclaim
}, /* reclaim */
149 { &vnop_strategy_desc
, (VOPFUNC
)spec_strategy
}, /* strategy */
150 { &vnop_pathconf_desc
, (VOPFUNC
)spec_pathconf
}, /* pathconf */
151 { &vnop_advlock_desc
, (VOPFUNC
)err_advlock
}, /* advlock */
152 { &vnop_bwrite_desc
, (VOPFUNC
)spec_bwrite
}, /* bwrite */
153 { &vnop_pagein_desc
, (VOPFUNC
)err_pagein
}, /* Pagein */
154 { &vnop_pageout_desc
, (VOPFUNC
)err_pageout
}, /* Pageout */
155 { &vnop_copyfile_desc
, (VOPFUNC
)err_copyfile
}, /* Copyfile */
156 { &vnop_blktooff_desc
, (VOPFUNC
)spec_blktooff
}, /* blktooff */
157 { &vnop_offtoblk_desc
, (VOPFUNC
)spec_offtoblk
}, /* offtoblk */
158 { &vnop_blockmap_desc
, (VOPFUNC
)spec_blockmap
}, /* blockmap */
159 { (struct vnodeop_desc
*)NULL
, (int(*)(void *))NULL
}
161 struct vnodeopv_desc spec_vnodeop_opv_desc
=
162 { &spec_vnodeop_p
, spec_vnodeop_entries
};
165 static void set_blocksize(vnode_t
, dev_t
);
167 #define LOWPRI_TIER1_WINDOW_MSECS 25
168 #define LOWPRI_TIER2_WINDOW_MSECS 100
169 #define LOWPRI_TIER3_WINDOW_MSECS 500
171 #define LOWPRI_TIER1_IO_PERIOD_MSECS 40
172 #define LOWPRI_TIER2_IO_PERIOD_MSECS 85
173 #define LOWPRI_TIER3_IO_PERIOD_MSECS 200
175 #define LOWPRI_TIER1_IO_PERIOD_SSD_MSECS 5
176 #define LOWPRI_TIER2_IO_PERIOD_SSD_MSECS 15
177 #define LOWPRI_TIER3_IO_PERIOD_SSD_MSECS 25
180 int throttle_windows_msecs
[THROTTLE_LEVEL_END
+ 1] = {
182 LOWPRI_TIER1_WINDOW_MSECS
,
183 LOWPRI_TIER2_WINDOW_MSECS
,
184 LOWPRI_TIER3_WINDOW_MSECS
,
187 int throttle_io_period_msecs
[THROTTLE_LEVEL_END
+ 1] = {
189 LOWPRI_TIER1_IO_PERIOD_MSECS
,
190 LOWPRI_TIER2_IO_PERIOD_MSECS
,
191 LOWPRI_TIER3_IO_PERIOD_MSECS
,
194 int throttle_io_period_ssd_msecs
[THROTTLE_LEVEL_END
+ 1] = {
196 LOWPRI_TIER1_IO_PERIOD_SSD_MSECS
,
197 LOWPRI_TIER2_IO_PERIOD_SSD_MSECS
,
198 LOWPRI_TIER3_IO_PERIOD_SSD_MSECS
,
202 int throttled_count
[THROTTLE_LEVEL_END
+ 1];
204 struct _throttle_io_info_t
{
205 lck_mtx_t throttle_lock
;
207 struct timeval throttle_last_write_timestamp
;
208 struct timeval throttle_min_timer_deadline
;
209 struct timeval throttle_window_start_timestamp
[THROTTLE_LEVEL_END
+ 1]; /* window starts at both the beginning and completion of an I/O */
210 struct timeval throttle_last_IO_timestamp
[THROTTLE_LEVEL_END
+ 1];
211 pid_t throttle_last_IO_pid
[THROTTLE_LEVEL_END
+ 1];
212 struct timeval throttle_start_IO_period_timestamp
[THROTTLE_LEVEL_END
+ 1];
213 int32_t throttle_inflight_count
[THROTTLE_LEVEL_END
+ 1];
215 TAILQ_HEAD( , uthread
) throttle_uthlist
[THROTTLE_LEVEL_END
+ 1]; /* Lists of throttled uthreads */
216 int throttle_next_wake_level
;
218 thread_call_t throttle_timer_call
;
219 int32_t throttle_timer_ref
;
220 int32_t throttle_timer_active
;
222 int32_t throttle_io_count
;
223 int32_t throttle_io_count_begin
;
224 int *throttle_io_periods
;
225 uint32_t throttle_io_period_num
;
227 int32_t throttle_refcnt
;
228 int32_t throttle_alloc
;
229 int32_t throttle_disabled
;
230 int32_t throttle_is_fusion_with_priority
;
233 struct _throttle_io_info_t _throttle_io_info
[LOWPRI_MAX_NUM_DEV
];
236 int lowpri_throttle_enabled
= 1;
239 static void throttle_info_end_io_internal(struct _throttle_io_info_t
*info
, int throttle_level
);
240 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
);
241 static int throttle_get_thread_throttle_level(uthread_t ut
);
242 static int throttle_get_thread_throttle_level_internal(uthread_t ut
, int io_tier
);
243 void throttle_info_mount_reset_period(mount_t mp
, int isssd
);
246 * Trivial lookup routine that always fails.
249 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
;
274 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
)
285 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
))
311 switch (vp
->v_type
) {
314 if ((u_int
)maj
>= (u_int
)nchrdev
)
316 if (cred
!= FSCRED
&& (ap
->a_mode
& FWRITE
)) {
318 * When running in very secure mode, do not allow
319 * opens for writing of any disk character devices.
321 if (securelevel
>= 2 && isdisk(dev
, VCHR
))
324 /* Never allow writing to /dev/mem or /dev/kmem */
328 * When running in secure mode, do not allow opens for
329 * writing of character devices whose corresponding block
330 * devices are currently mounted.
332 if (securelevel
>= 1) {
333 if ((bdev
= chrtoblk(dev
)) != NODEV
&& check_mountedon(bdev
, VBLK
, &error
))
338 devsw_lock(dev
, S_IFCHR
);
339 error
= (*cdevsw
[maj
].d_open
)(dev
, ap
->a_mode
, S_IFCHR
, p
);
342 vp
->v_specinfo
->si_opencount
++;
345 devsw_unlock(dev
, S_IFCHR
);
347 if (error
== 0 && cdevsw
[maj
].d_type
== D_DISK
&& !vp
->v_un
.vu_specinfo
->si_initted
) {
349 uint64_t throttle_mask
= 0;
350 uint32_t devbsdunit
= 0;
352 if (VNOP_IOCTL(vp
, DKIOCGETTHROTTLEMASK
, (caddr_t
)&throttle_mask
, 0, NULL
) == 0) {
354 if (throttle_mask
!= 0 &&
355 VNOP_IOCTL(vp
, DKIOCISSOLIDSTATE
, (caddr_t
)&isssd
, 0, ap
->a_context
) == 0) {
357 * as a reasonable approximation, only use the lowest bit of the mask
358 * to generate a disk unit number
360 devbsdunit
= num_trailing_0(throttle_mask
);
364 vp
->v_un
.vu_specinfo
->si_isssd
= isssd
;
365 vp
->v_un
.vu_specinfo
->si_devbsdunit
= devbsdunit
;
366 vp
->v_un
.vu_specinfo
->si_throttle_mask
= throttle_mask
;
367 vp
->v_un
.vu_specinfo
->si_throttleable
= 1;
368 vp
->v_un
.vu_specinfo
->si_initted
= 1;
373 if (vp
->v_un
.vu_specinfo
->si_initted
== 0) {
375 vp
->v_un
.vu_specinfo
->si_initted
= 1;
382 if ((u_int
)maj
>= (u_int
)nblkdev
)
385 * When running in very secure mode, do not allow
386 * opens for writing of any disk block devices.
388 if (securelevel
>= 2 && cred
!= FSCRED
&&
389 (ap
->a_mode
& FWRITE
) && bdevsw
[maj
].d_type
== D_DISK
)
392 * Do not allow opens of block devices that are
395 if ( (error
= vfs_mountedon(vp
)) )
398 devsw_lock(dev
, S_IFBLK
);
399 error
= (*bdevsw
[maj
].d_open
)(dev
, ap
->a_mode
, S_IFBLK
, p
);
401 vp
->v_specinfo
->si_opencount
++;
403 devsw_unlock(dev
, S_IFBLK
);
409 u_int32_t size512
= 512;
412 if (!VNOP_IOCTL(vp
, DKIOCGETBLOCKSIZE
, (caddr_t
)&blksize
, 0, ap
->a_context
)) {
413 /* Switch to 512 byte sectors (temporarily) */
415 if (!VNOP_IOCTL(vp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&size512
, FWRITE
, ap
->a_context
)) {
416 /* Get the number of 512 byte physical blocks. */
417 if (!VNOP_IOCTL(vp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
, 0, ap
->a_context
)) {
421 /* If it doesn't set back, we can't recover */
422 if (VNOP_IOCTL(vp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&blksize
, FWRITE
, ap
->a_context
))
428 set_blocksize(vp
, dev
);
431 * Cache the size in bytes of the block device for later
432 * use by spec_write().
435 vp
->v_specdevsize
= blkcnt
* (u_int64_t
)size512
;
437 vp
->v_specdevsize
= (u_int64_t
)0; /* Default: Can't get */
444 panic("spec_open type");
453 spec_read(struct vnop_read_args
*ap
)
455 struct vnode
*vp
= ap
->a_vp
;
456 struct uio
*uio
= ap
->a_uio
;
458 daddr64_t bn
, nextbn
;
466 if (uio
->uio_rw
!= UIO_READ
)
467 panic("spec_read mode");
468 if (UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
))
469 panic("spec_read proc");
471 if (uio_resid(uio
) == 0)
474 switch (vp
->v_type
) {
478 struct _throttle_io_info_t
*throttle_info
= NULL
;
479 int thread_throttle_level
;
480 if (cdevsw
[major(vp
->v_rdev
)].d_type
== D_DISK
&& vp
->v_un
.vu_specinfo
->si_throttleable
) {
481 throttle_info
= &_throttle_io_info
[vp
->v_un
.vu_specinfo
->si_devbsdunit
];
482 thread_throttle_level
= throttle_info_update_internal(throttle_info
, NULL
, 0, vp
->v_un
.vu_specinfo
->si_isssd
, TRUE
, NULL
);
484 error
= (*cdevsw
[major(vp
->v_rdev
)].d_read
)
485 (vp
->v_rdev
, uio
, ap
->a_ioflag
);
488 throttle_info_end_io_internal(throttle_info
, thread_throttle_level
);
495 if (uio
->uio_offset
< 0)
500 devBlockSize
= vp
->v_specsize
;
502 if (devBlockSize
> PAGE_SIZE
)
505 bscale
= PAGE_SIZE
/ devBlockSize
;
506 bsize
= bscale
* devBlockSize
;
509 on
= uio
->uio_offset
% bsize
;
511 bn
= (daddr64_t
)((uio
->uio_offset
/ devBlockSize
) &~ (bscale
- 1));
513 if (vp
->v_speclastr
+ bscale
== bn
) {
514 nextbn
= bn
+ bscale
;
515 error
= buf_breadn(vp
, bn
, (int)bsize
, &nextbn
,
516 (int *)&bsize
, 1, NOCRED
, &bp
);
518 error
= buf_bread(vp
, bn
, (int)bsize
, NOCRED
, &bp
);
521 vp
->v_speclastr
= bn
;
524 n
= bsize
- buf_resid(bp
);
525 if ((on
> n
) || error
) {
531 n
= min((unsigned)(n
- on
), uio_resid(uio
));
533 error
= uiomove((char *)buf_dataptr(bp
) + on
, n
, uio
);
537 } while (error
== 0 && uio_resid(uio
) > 0 && n
!= 0);
541 panic("spec_read type");
552 spec_write(struct vnop_write_args
*ap
)
554 struct vnode
*vp
= ap
->a_vp
;
555 struct uio
*uio
= ap
->a_uio
;
558 int bsize
, blkmask
, bscale
;
566 if (uio
->uio_rw
!= UIO_WRITE
)
567 panic("spec_write mode");
568 if (UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
))
569 panic("spec_write proc");
572 switch (vp
->v_type
) {
576 struct _throttle_io_info_t
*throttle_info
= NULL
;
577 int thread_throttle_level
;
578 if (cdevsw
[major(vp
->v_rdev
)].d_type
== D_DISK
&& vp
->v_un
.vu_specinfo
->si_throttleable
) {
579 throttle_info
= &_throttle_io_info
[vp
->v_un
.vu_specinfo
->si_devbsdunit
];
581 thread_throttle_level
= throttle_info_update_internal(throttle_info
, NULL
, 0, vp
->v_un
.vu_specinfo
->si_isssd
, TRUE
, NULL
);
583 microuptime(&throttle_info
->throttle_last_write_timestamp
);
585 error
= (*cdevsw
[major(vp
->v_rdev
)].d_write
)
586 (vp
->v_rdev
, uio
, ap
->a_ioflag
);
589 throttle_info_end_io_internal(throttle_info
, thread_throttle_level
);
596 if (uio_resid(uio
) == 0)
598 if (uio
->uio_offset
< 0)
601 io_sync
= (ap
->a_ioflag
& IO_SYNC
);
605 devBlockSize
= vp
->v_specsize
;
606 if (devBlockSize
> PAGE_SIZE
)
609 bscale
= PAGE_SIZE
/ devBlockSize
;
610 blkmask
= bscale
- 1;
611 bsize
= bscale
* devBlockSize
;
615 bn
= (daddr64_t
)((uio
->uio_offset
/ devBlockSize
) &~ blkmask
);
616 on
= uio
->uio_offset
% bsize
;
618 n
= min((unsigned)(bsize
- on
), uio_resid(uio
));
621 * Use buf_getblk() as an optimization IFF:
623 * 1) We are reading exactly a block on a block
625 * 2) We know the size of the device from spec_open
626 * 3) The read doesn't span the end of the device
628 * Otherwise, we fall back on buf_bread().
631 vp
->v_specdevsize
!= (u_int64_t
)0 &&
632 (uio
->uio_offset
+ (u_int64_t
)n
) > vp
->v_specdevsize
) {
633 /* reduce the size of the read to what is there */
634 n
= (uio
->uio_offset
+ (u_int64_t
)n
) - vp
->v_specdevsize
;
638 bp
= buf_getblk(vp
, bn
, bsize
, 0, 0, BLK_WRITE
);
640 error
= (int)buf_bread(vp
, bn
, bsize
, NOCRED
, &bp
);
642 /* Translate downstream error for upstream, if needed */
644 error
= (int)buf_error(bp
);
649 n
= min(n
, bsize
- buf_resid(bp
));
651 error
= uiomove((char *)buf_dataptr(bp
) + on
, n
, uio
);
659 error
= buf_bwrite(bp
);
661 if ((n
+ on
) == bsize
)
662 error
= buf_bawrite(bp
);
664 error
= buf_bdwrite(bp
);
666 } while (error
== 0 && uio_resid(uio
) > 0 && n
!= 0);
670 panic("spec_write type");
678 * Device ioctl operation.
681 spec_ioctl(struct vnop_ioctl_args
*ap
)
683 proc_t p
= vfs_context_proc(ap
->a_context
);
684 dev_t dev
= ap
->a_vp
->v_rdev
;
687 KERNEL_DEBUG_CONSTANT(FSDBG_CODE(DBG_IOCTL
, 0) | DBG_FUNC_START
,
688 dev
, ap
->a_command
, ap
->a_fflag
, ap
->a_vp
->v_type
, 0);
690 switch (ap
->a_vp
->v_type
) {
693 retval
= (*cdevsw
[major(dev
)].d_ioctl
)(dev
, ap
->a_command
, ap
->a_data
,
698 retval
= (*bdevsw
[major(dev
)].d_ioctl
)(dev
, ap
->a_command
, ap
->a_data
, ap
->a_fflag
, p
);
699 if (!retval
&& ap
->a_command
== DKIOCSETBLOCKSIZE
)
700 ap
->a_vp
->v_specsize
= *(uint32_t *)ap
->a_data
;
707 KERNEL_DEBUG_CONSTANT(FSDBG_CODE(DBG_IOCTL
, 0) | DBG_FUNC_END
,
708 dev
, ap
->a_command
, ap
->a_fflag
, retval
, 0);
714 spec_select(struct vnop_select_args
*ap
)
716 proc_t p
= vfs_context_proc(ap
->a_context
);
719 switch (ap
->a_vp
->v_type
) {
722 return (1); /* XXX */
725 dev
= ap
->a_vp
->v_rdev
;
726 return (*cdevsw
[major(dev
)].d_select
)(dev
, ap
->a_which
, ap
->a_wql
, p
);
730 static int filt_specattach(struct knote
*kn
, struct kevent_internal_s
*kev
);
733 spec_kqfilter(vnode_t vp
, struct knote
*kn
, struct kevent_internal_s
*kev
)
737 assert(vnode_ischr(vp
));
739 dev
= vnode_specrdev(vp
);
743 * Try a bpf device, as defined in bsd/net/bpf.c
744 * If it doesn't error out the attach, then it
745 * claimed it. Otherwise, fall through and try
748 int32_t tmp_flags
= kn
->kn_flags
;
749 int64_t tmp_data
= kn
->kn_data
;
752 res
= bpfkqfilter(dev
, kn
);
753 if ((kn
->kn_flags
& EV_ERROR
) == 0) {
756 kn
->kn_flags
= tmp_flags
;
757 kn
->kn_data
= tmp_data
;
760 if (major(dev
) > nchrdev
) {
761 knote_set_error(kn
, ENXIO
);
765 kn
->kn_vnode_kqok
= !!(cdevsw_flags
[major(dev
)] & CDEVSW_SELECT_KQUEUE
);
766 kn
->kn_vnode_use_ofst
= !!(cdevsw_flags
[major(dev
)] & CDEVSW_USE_OFFSET
);
768 if (cdevsw_flags
[major(dev
)] & CDEVSW_IS_PTS
) {
769 kn
->kn_filtid
= EVFILTID_PTSD
;
770 return ptsd_kqfilter(dev
, kn
);
771 } else if (cdevsw
[major(dev
)].d_type
== D_TTY
&&
772 !(cdevsw_flags
[major(dev
)] & CDEVSW_IS_PTC
) &&
775 * TTYs from drivers that use struct ttys use their own filter
776 * routines. The PTC driver doesn't use the tty for character
777 * counts, so it must go through the select fallback.
779 kn
->kn_filtid
= EVFILTID_TTY
;
780 return knote_fops(kn
)->f_attach(kn
, kev
);
783 /* Try to attach to other char special devices */
784 return filt_specattach(kn
, kev
);
788 * Synch buffers associated with a block device
791 spec_fsync_internal(vnode_t vp
, int waitfor
, __unused vfs_context_t context
)
793 if (vp
->v_type
== VCHR
)
796 * Flush all dirty buffers associated with a block device.
798 buf_flushdirtyblks(vp
, (waitfor
== MNT_WAIT
|| waitfor
== MNT_DWAIT
), 0, "spec_fsync");
804 spec_fsync(struct vnop_fsync_args
*ap
)
806 return spec_fsync_internal(ap
->a_vp
, ap
->a_waitfor
, ap
->a_context
);
811 * Just call the device strategy routine
813 void throttle_init(void);
817 #define DEBUG_ALLOC_THROTTLE_INFO(format, debug_info, args...) \
819 if ((debug_info)->alloc) \
820 printf("%s: "format, __FUNCTION__, ## args); \
824 #define DEBUG_ALLOC_THROTTLE_INFO(format, debug_info, args...)
828 SYSCTL_INT(_debug
, OID_AUTO
, lowpri_throttle_tier1_window_msecs
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &throttle_windows_msecs
[THROTTLE_LEVEL_TIER1
], 0, "");
829 SYSCTL_INT(_debug
, OID_AUTO
, lowpri_throttle_tier2_window_msecs
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &throttle_windows_msecs
[THROTTLE_LEVEL_TIER2
], 0, "");
830 SYSCTL_INT(_debug
, OID_AUTO
, lowpri_throttle_tier3_window_msecs
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &throttle_windows_msecs
[THROTTLE_LEVEL_TIER3
], 0, "");
832 SYSCTL_INT(_debug
, OID_AUTO
, lowpri_throttle_tier1_io_period_msecs
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &throttle_io_period_msecs
[THROTTLE_LEVEL_TIER1
], 0, "");
833 SYSCTL_INT(_debug
, OID_AUTO
, lowpri_throttle_tier2_io_period_msecs
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &throttle_io_period_msecs
[THROTTLE_LEVEL_TIER2
], 0, "");
834 SYSCTL_INT(_debug
, OID_AUTO
, lowpri_throttle_tier3_io_period_msecs
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &throttle_io_period_msecs
[THROTTLE_LEVEL_TIER3
], 0, "");
836 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, "");
837 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, "");
838 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, "");
840 SYSCTL_INT(_debug
, OID_AUTO
, lowpri_throttle_enabled
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &lowpri_throttle_enabled
, 0, "");
843 static lck_grp_t
*throttle_lock_grp
;
844 static lck_attr_t
*throttle_lock_attr
;
845 static lck_grp_attr_t
*throttle_lock_grp_attr
;
849 * throttled I/O helper function
850 * convert the index of the lowest set bit to a device index
853 num_trailing_0(uint64_t n
)
856 * since in most cases the number of trailing 0s is very small,
857 * we simply counting sequentially from the lowest bit
860 return sizeof(n
) * 8;
862 while (!ISSET(n
, 1)) {
871 * Release the reference and if the item was allocated and this is the last
872 * reference then free it.
874 * This routine always returns the old value.
877 throttle_info_rel(struct _throttle_io_info_t
*info
)
879 SInt32 oldValue
= OSDecrementAtomic(&info
->throttle_refcnt
);
881 DEBUG_ALLOC_THROTTLE_INFO("refcnt = %d info = %p\n",
882 info
, (int)(oldValue
-1), info
);
884 /* The reference count just went negative, very bad */
886 panic("throttle info ref cnt went negative!");
889 * Once reference count is zero, no one else should be able to take a
892 if ((info
->throttle_refcnt
== 0) && (info
->throttle_alloc
)) {
893 DEBUG_ALLOC_THROTTLE_INFO("Freeing info = %p\n", info
);
895 lck_mtx_destroy(&info
->throttle_lock
, throttle_lock_grp
);
903 * Just take a reference on the throttle info structure.
905 * This routine always returns the old value.
908 throttle_info_ref(struct _throttle_io_info_t
*info
)
910 SInt32 oldValue
= OSIncrementAtomic(&info
->throttle_refcnt
);
912 DEBUG_ALLOC_THROTTLE_INFO("refcnt = %d info = %p\n",
913 info
, (int)(oldValue
-1), info
);
914 /* Allocated items should never have a reference of zero */
915 if (info
->throttle_alloc
&& (oldValue
== 0))
916 panic("Taking a reference without calling create throttle info!\n");
922 * on entry the throttle_lock is held...
923 * this function is responsible for taking
924 * and dropping the reference on the info
925 * structure which will keep it from going
926 * away while the timer is running if it
927 * happens to have been dynamically allocated by
928 * a network fileystem kext which is now trying
932 throttle_timer_start(struct _throttle_io_info_t
*info
, boolean_t update_io_count
, int wakelevel
)
934 struct timeval elapsed
;
936 struct timeval period
;
937 uint64_t elapsed_msecs
;
941 boolean_t throttled
= FALSE
;
942 boolean_t need_timer
= FALSE
;
946 if (update_io_count
== TRUE
) {
947 info
->throttle_io_count_begin
= info
->throttle_io_count
;
948 info
->throttle_io_period_num
++;
950 while (wakelevel
>= THROTTLE_LEVEL_THROTTLED
)
951 info
->throttle_start_IO_period_timestamp
[wakelevel
--] = now
;
953 info
->throttle_min_timer_deadline
= now
;
955 msecs
= info
->throttle_io_periods
[THROTTLE_LEVEL_THROTTLED
];
956 period
.tv_sec
= msecs
/ 1000;
957 period
.tv_usec
= (msecs
% 1000) * 1000;
959 timevaladd(&info
->throttle_min_timer_deadline
, &period
);
961 for (throttle_level
= THROTTLE_LEVEL_START
; throttle_level
< THROTTLE_LEVEL_END
; throttle_level
++) {
964 timevalsub(&elapsed
, &info
->throttle_window_start_timestamp
[throttle_level
]);
965 elapsed_msecs
= (uint64_t)elapsed
.tv_sec
* (uint64_t)1000 + (elapsed
.tv_usec
/ 1000);
967 for (level
= throttle_level
+ 1; level
<= THROTTLE_LEVEL_END
; level
++) {
969 if (!TAILQ_EMPTY(&info
->throttle_uthlist
[level
])) {
971 if (elapsed_msecs
< (uint64_t)throttle_windows_msecs
[level
] || info
->throttle_inflight_count
[throttle_level
]) {
973 * we had an I/O occur at a higher priority tier within
974 * this tier's throttle window
979 * we assume that the windows are the same or longer
980 * as we drop through the throttling tiers... thus
981 * we can stop looking once we run into a tier with
982 * threads to schedule regardless of whether it's
983 * still in its throttling window or not
988 if (throttled
== TRUE
)
991 if (throttled
== TRUE
) {
992 uint64_t deadline
= 0;
993 struct timeval target
;
994 struct timeval min_target
;
997 * we've got at least one tier still in a throttled window
998 * so we need a timer running... compute the next deadline
1001 for (level
= throttle_level
+1; level
<= THROTTLE_LEVEL_END
; level
++) {
1003 if (TAILQ_EMPTY(&info
->throttle_uthlist
[level
]))
1006 target
= info
->throttle_start_IO_period_timestamp
[level
];
1008 msecs
= info
->throttle_io_periods
[level
];
1009 period
.tv_sec
= msecs
/ 1000;
1010 period
.tv_usec
= (msecs
% 1000) * 1000;
1012 timevaladd(&target
, &period
);
1014 if (need_timer
== FALSE
|| timevalcmp(&target
, &min_target
, <)) {
1015 min_target
= target
;
1019 if (timevalcmp(&info
->throttle_min_timer_deadline
, &now
, >)) {
1020 if (timevalcmp(&info
->throttle_min_timer_deadline
, &min_target
, >))
1021 min_target
= info
->throttle_min_timer_deadline
;
1024 if (info
->throttle_timer_active
) {
1025 if (thread_call_cancel(info
->throttle_timer_call
) == FALSE
) {
1027 * couldn't kill the timer because it's already
1028 * been dispatched, so don't try to start a new
1029 * one... once we drop the lock, the timer will
1030 * proceed and eventually re-run this function
1034 info
->throttle_timer_active
= 0;
1036 if (need_timer
== TRUE
) {
1038 * This is defined as an int (32-bit) rather than a 64-bit
1039 * value because it would need a really big period in the
1040 * order of ~500 days to overflow this. So, we let this be
1041 * 32-bit which allows us to use the clock_interval_to_deadline()
1046 if (info
->throttle_timer_ref
== 0) {
1048 * take a reference for the timer
1050 throttle_info_ref(info
);
1052 info
->throttle_timer_ref
= 1;
1054 elapsed
= min_target
;
1055 timevalsub(&elapsed
, &now
);
1056 target_msecs
= elapsed
.tv_sec
* 1000 + elapsed
.tv_usec
/ 1000;
1058 if (target_msecs
<= 0) {
1060 * we may have computed a deadline slightly in the past
1061 * due to various factors... if so, just set the timer
1062 * to go off in the near future (we don't need to be precise)
1066 clock_interval_to_deadline(target_msecs
, 1000000, &deadline
);
1068 thread_call_enter_delayed(info
->throttle_timer_call
, deadline
);
1069 info
->throttle_timer_active
= 1;
1072 return (throttle_level
);
1077 throttle_timer(struct _throttle_io_info_t
*info
)
1079 uthread_t ut
, utlist
;
1080 struct timeval elapsed
;
1082 uint64_t elapsed_msecs
;
1086 caddr_t wake_address
= NULL
;
1087 boolean_t update_io_count
= FALSE
;
1088 boolean_t need_wakeup
= FALSE
;
1089 boolean_t need_release
= FALSE
;
1092 lck_mtx_lock(&info
->throttle_lock
);
1094 info
->throttle_timer_active
= 0;
1098 timevalsub(&elapsed
, &info
->throttle_start_IO_period_timestamp
[THROTTLE_LEVEL_THROTTLED
]);
1099 elapsed_msecs
= (uint64_t)elapsed
.tv_sec
* (uint64_t)1000 + (elapsed
.tv_usec
/ 1000);
1101 if (elapsed_msecs
>= (uint64_t)info
->throttle_io_periods
[THROTTLE_LEVEL_THROTTLED
]) {
1103 wake_level
= info
->throttle_next_wake_level
;
1105 for (level
= THROTTLE_LEVEL_START
; level
< THROTTLE_LEVEL_END
; level
++) {
1108 timevalsub(&elapsed
, &info
->throttle_start_IO_period_timestamp
[wake_level
]);
1109 elapsed_msecs
= (uint64_t)elapsed
.tv_sec
* (uint64_t)1000 + (elapsed
.tv_usec
/ 1000);
1111 if (elapsed_msecs
>= (uint64_t)info
->throttle_io_periods
[wake_level
] && !TAILQ_EMPTY(&info
->throttle_uthlist
[wake_level
])) {
1113 * we're closing out the current IO period...
1114 * if we have a waiting thread, wake it up
1115 * after we have reset the I/O window info
1118 update_io_count
= TRUE
;
1120 info
->throttle_next_wake_level
= wake_level
- 1;
1122 if (info
->throttle_next_wake_level
== THROTTLE_LEVEL_START
)
1123 info
->throttle_next_wake_level
= THROTTLE_LEVEL_END
;
1129 if (wake_level
== THROTTLE_LEVEL_START
)
1130 wake_level
= THROTTLE_LEVEL_END
;
1133 if (need_wakeup
== TRUE
) {
1134 if (!TAILQ_EMPTY(&info
->throttle_uthlist
[wake_level
])) {
1136 ut
= (uthread_t
)TAILQ_FIRST(&info
->throttle_uthlist
[wake_level
]);
1137 TAILQ_REMOVE(&info
->throttle_uthlist
[wake_level
], ut
, uu_throttlelist
);
1138 ut
->uu_on_throttlelist
= THROTTLE_LEVEL_NONE
;
1139 ut
->uu_is_throttled
= FALSE
;
1141 wake_address
= (caddr_t
)&ut
->uu_on_throttlelist
;
1144 wake_level
= THROTTLE_LEVEL_START
;
1146 throttle_level
= throttle_timer_start(info
, update_io_count
, wake_level
);
1148 if (wake_address
!= NULL
)
1149 wakeup(wake_address
);
1151 for (level
= THROTTLE_LEVEL_THROTTLED
; level
<= throttle_level
; level
++) {
1153 TAILQ_FOREACH_SAFE(ut
, &info
->throttle_uthlist
[level
], uu_throttlelist
, utlist
) {
1155 TAILQ_REMOVE(&info
->throttle_uthlist
[level
], ut
, uu_throttlelist
);
1156 ut
->uu_on_throttlelist
= THROTTLE_LEVEL_NONE
;
1157 ut
->uu_is_throttled
= FALSE
;
1159 wakeup(&ut
->uu_on_throttlelist
);
1162 if (info
->throttle_timer_active
== 0 && info
->throttle_timer_ref
) {
1163 info
->throttle_timer_ref
= 0;
1164 need_release
= TRUE
;
1166 lck_mtx_unlock(&info
->throttle_lock
);
1168 if (need_release
== TRUE
)
1169 throttle_info_rel(info
);
1174 throttle_add_to_list(struct _throttle_io_info_t
*info
, uthread_t ut
, int mylevel
, boolean_t insert_tail
)
1176 boolean_t start_timer
= FALSE
;
1177 int level
= THROTTLE_LEVEL_START
;
1179 if (TAILQ_EMPTY(&info
->throttle_uthlist
[mylevel
])) {
1180 info
->throttle_start_IO_period_timestamp
[mylevel
] = info
->throttle_last_IO_timestamp
[mylevel
];
1184 if (insert_tail
== TRUE
)
1185 TAILQ_INSERT_TAIL(&info
->throttle_uthlist
[mylevel
], ut
, uu_throttlelist
);
1187 TAILQ_INSERT_HEAD(&info
->throttle_uthlist
[mylevel
], ut
, uu_throttlelist
);
1189 ut
->uu_on_throttlelist
= mylevel
;
1191 if (start_timer
== TRUE
) {
1192 /* we may need to start or rearm the timer */
1193 level
= throttle_timer_start(info
, FALSE
, THROTTLE_LEVEL_START
);
1195 if (level
== THROTTLE_LEVEL_END
) {
1196 if (ut
->uu_on_throttlelist
>= THROTTLE_LEVEL_THROTTLED
) {
1197 TAILQ_REMOVE(&info
->throttle_uthlist
[ut
->uu_on_throttlelist
], ut
, uu_throttlelist
);
1199 ut
->uu_on_throttlelist
= THROTTLE_LEVEL_NONE
;
1207 throttle_init_throttle_window(void)
1209 int throttle_window_size
;
1212 * The hierarchy of throttle window values is as follows:
1214 * - Device tree properties
1216 * All values are specified in msecs.
1219 /* Override global values with device-tree properties */
1220 if (PE_get_default("kern.io_throttle_window_tier1", &throttle_window_size
, sizeof(throttle_window_size
)))
1221 throttle_windows_msecs
[THROTTLE_LEVEL_TIER1
] = throttle_window_size
;
1223 if (PE_get_default("kern.io_throttle_window_tier2", &throttle_window_size
, sizeof(throttle_window_size
)))
1224 throttle_windows_msecs
[THROTTLE_LEVEL_TIER2
] = throttle_window_size
;
1226 if (PE_get_default("kern.io_throttle_window_tier3", &throttle_window_size
, sizeof(throttle_window_size
)))
1227 throttle_windows_msecs
[THROTTLE_LEVEL_TIER3
] = throttle_window_size
;
1229 /* Override with boot-args */
1230 if (PE_parse_boot_argn("io_throttle_window_tier1", &throttle_window_size
, sizeof(throttle_window_size
)))
1231 throttle_windows_msecs
[THROTTLE_LEVEL_TIER1
] = throttle_window_size
;
1233 if (PE_parse_boot_argn("io_throttle_window_tier2", &throttle_window_size
, sizeof(throttle_window_size
)))
1234 throttle_windows_msecs
[THROTTLE_LEVEL_TIER2
] = throttle_window_size
;
1236 if (PE_parse_boot_argn("io_throttle_window_tier3", &throttle_window_size
, sizeof(throttle_window_size
)))
1237 throttle_windows_msecs
[THROTTLE_LEVEL_TIER3
] = throttle_window_size
;
1241 throttle_init_throttle_period(struct _throttle_io_info_t
*info
, boolean_t isssd
)
1243 int throttle_period_size
;
1246 * The hierarchy of throttle period values is as follows:
1248 * - Device tree properties
1250 * All values are specified in msecs.
1253 /* Assign global defaults */
1254 if ((isssd
== TRUE
) && (info
->throttle_is_fusion_with_priority
== 0))
1255 info
->throttle_io_periods
= &throttle_io_period_ssd_msecs
[0];
1257 info
->throttle_io_periods
= &throttle_io_period_msecs
[0];
1259 /* Override global values with device-tree properties */
1260 if (PE_get_default("kern.io_throttle_period_tier1", &throttle_period_size
, sizeof(throttle_period_size
)))
1261 info
->throttle_io_periods
[THROTTLE_LEVEL_TIER1
] = throttle_period_size
;
1263 if (PE_get_default("kern.io_throttle_period_tier2", &throttle_period_size
, sizeof(throttle_period_size
)))
1264 info
->throttle_io_periods
[THROTTLE_LEVEL_TIER2
] = throttle_period_size
;
1266 if (PE_get_default("kern.io_throttle_period_tier3", &throttle_period_size
, sizeof(throttle_period_size
)))
1267 info
->throttle_io_periods
[THROTTLE_LEVEL_TIER3
] = throttle_period_size
;
1269 /* Override with boot-args */
1270 if (PE_parse_boot_argn("io_throttle_period_tier1", &throttle_period_size
, sizeof(throttle_period_size
)))
1271 info
->throttle_io_periods
[THROTTLE_LEVEL_TIER1
] = throttle_period_size
;
1273 if (PE_parse_boot_argn("io_throttle_period_tier2", &throttle_period_size
, sizeof(throttle_period_size
)))
1274 info
->throttle_io_periods
[THROTTLE_LEVEL_TIER2
] = throttle_period_size
;
1276 if (PE_parse_boot_argn("io_throttle_period_tier3", &throttle_period_size
, sizeof(throttle_period_size
)))
1277 info
->throttle_io_periods
[THROTTLE_LEVEL_TIER3
] = throttle_period_size
;
1282 extern void vm_io_reprioritize_init(void);
1283 int iosched_enabled
= 1;
1289 struct _throttle_io_info_t
*info
;
1296 * allocate lock group attribute and group
1298 throttle_lock_grp_attr
= lck_grp_attr_alloc_init();
1299 throttle_lock_grp
= lck_grp_alloc_init("throttle I/O", throttle_lock_grp_attr
);
1301 /* Update throttle parameters based on device tree configuration */
1302 throttle_init_throttle_window();
1305 * allocate the lock attribute
1307 throttle_lock_attr
= lck_attr_alloc_init();
1309 for (i
= 0; i
< LOWPRI_MAX_NUM_DEV
; i
++) {
1310 info
= &_throttle_io_info
[i
];
1312 lck_mtx_init(&info
->throttle_lock
, throttle_lock_grp
, throttle_lock_attr
);
1313 info
->throttle_timer_call
= thread_call_allocate((thread_call_func_t
)throttle_timer
, (thread_call_param_t
)info
);
1315 for (level
= 0; level
<= THROTTLE_LEVEL_END
; level
++) {
1316 TAILQ_INIT(&info
->throttle_uthlist
[level
]);
1317 info
->throttle_last_IO_pid
[level
] = 0;
1318 info
->throttle_inflight_count
[level
] = 0;
1320 info
->throttle_next_wake_level
= THROTTLE_LEVEL_END
;
1321 info
->throttle_disabled
= 0;
1322 info
->throttle_is_fusion_with_priority
= 0;
1325 if (PE_parse_boot_argn("iosched", &iosched
, sizeof(iosched
))) {
1326 iosched_enabled
= iosched
;
1328 if (iosched_enabled
) {
1329 /* Initialize I/O Reprioritization mechanism */
1330 vm_io_reprioritize_init();
1336 sys_override_io_throttle(int flag
)
1338 if (flag
== THROTTLE_IO_ENABLE
)
1339 lowpri_throttle_enabled
= 1;
1341 if (flag
== THROTTLE_IO_DISABLE
)
1342 lowpri_throttle_enabled
= 0;
1345 int rethrottle_wakeups
= 0;
1348 * the uu_rethrottle_lock is used to synchronize this function
1349 * with "throttle_lowpri_io" which is where a throttled thread
1350 * will block... that function will grab this lock before beginning
1351 * it's decision making process concerning the need to block, and
1352 * hold it through the assert_wait. When that thread is awakened
1353 * for any reason (timer or rethrottle), it will reacquire the
1354 * uu_rethrottle_lock before determining if it really is ok for
1355 * it to now run. This is the point at which the thread could
1356 * enter a different throttling queue and reblock or return from
1357 * the throttle w/o having waited out it's entire throttle if
1358 * the rethrottle has now moved it out of any currently
1359 * active throttle window.
1363 * 1 - This may be called with the task lock held.
1364 * 2 - This may be called with preemption and interrupts disabled
1365 * in the kqueue wakeup path so we can't take the throttle_lock which is a mutex
1366 * 3 - This cannot safely dereference uu_throttle_info, as it may
1367 * get deallocated out from under us
1371 rethrottle_thread(uthread_t ut
)
1374 * If uthread doesn't have throttle state, then there's no chance
1375 * of it needing a rethrottle.
1377 if (ut
->uu_throttle_info
== NULL
)
1380 boolean_t s
= ml_set_interrupts_enabled(FALSE
);
1381 lck_spin_lock(&ut
->uu_rethrottle_lock
);
1383 if (ut
->uu_is_throttled
== FALSE
)
1384 ut
->uu_was_rethrottled
= TRUE
;
1386 int my_new_level
= throttle_get_thread_throttle_level(ut
);
1388 if (my_new_level
!= ut
->uu_on_throttlelist
) {
1390 * ut is currently blocked (as indicated by
1391 * ut->uu_is_throttled == TRUE)
1392 * and we're changing it's throttle level, so
1393 * we need to wake it up.
1395 ut
->uu_is_throttled
= FALSE
;
1396 wakeup(&ut
->uu_on_throttlelist
);
1398 rethrottle_wakeups
++;
1399 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_FSRW
, 102)), thread_tid(ut
->uu_thread
), ut
->uu_on_throttlelist
, my_new_level
, 0, 0);
1402 lck_spin_unlock(&ut
->uu_rethrottle_lock
);
1403 ml_set_interrupts_enabled(s
);
1410 * Create and take a reference on a throttle info structure and return a
1411 * pointer for the file system to use when calling throttle_info_update.
1412 * Calling file system must have a matching release for every create.
1415 throttle_info_create(void)
1417 struct _throttle_io_info_t
*info
;
1420 MALLOC(info
, struct _throttle_io_info_t
*, sizeof(*info
), M_TEMP
, M_ZERO
| M_WAITOK
);
1421 /* Should never happen but just in case */
1424 /* Mark that this one was allocated and needs to be freed */
1425 DEBUG_ALLOC_THROTTLE_INFO("Creating info = %p\n", info
, info
);
1426 info
->throttle_alloc
= TRUE
;
1428 lck_mtx_init(&info
->throttle_lock
, throttle_lock_grp
, throttle_lock_attr
);
1429 info
->throttle_timer_call
= thread_call_allocate((thread_call_func_t
)throttle_timer
, (thread_call_param_t
)info
);
1431 for (level
= 0; level
<= THROTTLE_LEVEL_END
; level
++) {
1432 TAILQ_INIT(&info
->throttle_uthlist
[level
]);
1434 info
->throttle_next_wake_level
= THROTTLE_LEVEL_END
;
1436 /* Take a reference */
1437 OSIncrementAtomic(&info
->throttle_refcnt
);
1444 * Release the throttle info pointer if all the reference are gone. Should be
1445 * called to release reference taken by throttle_info_create
1448 throttle_info_release(void *throttle_info
)
1450 DEBUG_ALLOC_THROTTLE_INFO("Releaseing info = %p\n",
1451 (struct _throttle_io_info_t
*)throttle_info
,
1452 (struct _throttle_io_info_t
*)throttle_info
);
1453 if (throttle_info
) /* Just to be careful */
1454 throttle_info_rel(throttle_info
);
1460 * File Systems that create an info structure, need to call this routine in
1461 * their mount routine (used by cluster code). File Systems that call this in
1462 * their mount routines must call throttle_info_mount_rel in their unmount
1466 throttle_info_mount_ref(mount_t mp
, void *throttle_info
)
1468 if ((throttle_info
== NULL
) || (mp
== NULL
))
1470 throttle_info_ref(throttle_info
);
1473 * We already have a reference release it before adding the new one
1475 if (mp
->mnt_throttle_info
)
1476 throttle_info_rel(mp
->mnt_throttle_info
);
1477 mp
->mnt_throttle_info
= throttle_info
;
1481 * Private KPI routine
1483 * return a handle for accessing throttle_info given a throttle_mask. The
1484 * handle must be released by throttle_info_rel_by_mask
1487 throttle_info_ref_by_mask(uint64_t throttle_mask
, throttle_info_handle_t
*throttle_info_handle
)
1490 struct _throttle_io_info_t
*info
;
1492 if (throttle_info_handle
== NULL
)
1495 dev_index
= num_trailing_0(throttle_mask
);
1496 info
= &_throttle_io_info
[dev_index
];
1497 throttle_info_ref(info
);
1498 *(struct _throttle_io_info_t
**)throttle_info_handle
= info
;
1504 * Private KPI routine
1506 * release the handle obtained by throttle_info_ref_by_mask
1509 throttle_info_rel_by_mask(throttle_info_handle_t throttle_info_handle
)
1512 * for now the handle is just a pointer to _throttle_io_info_t
1514 throttle_info_rel((struct _throttle_io_info_t
*)throttle_info_handle
);
1520 * File Systems that throttle_info_mount_ref, must call this routine in their
1524 throttle_info_mount_rel(mount_t mp
)
1526 if (mp
->mnt_throttle_info
)
1527 throttle_info_rel(mp
->mnt_throttle_info
);
1528 mp
->mnt_throttle_info
= NULL
;
1532 * Reset throttling periods for the given mount point
1534 * private interface used by disk conditioner to reset
1535 * throttling periods when 'is_ssd' status changes
1538 throttle_info_mount_reset_period(mount_t mp
, int isssd
)
1540 struct _throttle_io_info_t
*info
;
1543 info
= &_throttle_io_info
[LOWPRI_MAX_NUM_DEV
- 1];
1544 else if (mp
->mnt_throttle_info
== NULL
)
1545 info
= &_throttle_io_info
[mp
->mnt_devbsdunit
];
1547 info
= mp
->mnt_throttle_info
;
1549 throttle_init_throttle_period(info
, isssd
);
1553 throttle_info_get_last_io_time(mount_t mp
, struct timeval
*tv
)
1555 struct _throttle_io_info_t
*info
;
1558 info
= &_throttle_io_info
[LOWPRI_MAX_NUM_DEV
- 1];
1559 else if (mp
->mnt_throttle_info
== NULL
)
1560 info
= &_throttle_io_info
[mp
->mnt_devbsdunit
];
1562 info
= mp
->mnt_throttle_info
;
1564 *tv
= info
->throttle_last_write_timestamp
;
1568 update_last_io_time(mount_t mp
)
1570 struct _throttle_io_info_t
*info
;
1573 info
= &_throttle_io_info
[LOWPRI_MAX_NUM_DEV
- 1];
1574 else if (mp
->mnt_throttle_info
== NULL
)
1575 info
= &_throttle_io_info
[mp
->mnt_devbsdunit
];
1577 info
= mp
->mnt_throttle_info
;
1579 microuptime(&info
->throttle_last_write_timestamp
);
1581 mp
->mnt_last_write_completed_timestamp
= info
->throttle_last_write_timestamp
;
1585 throttle_get_io_policy(uthread_t
*ut
)
1588 *ut
= get_bsdthread_info(current_thread());
1590 return (proc_get_effective_thread_policy(current_thread(), TASK_POLICY_IO
));
1594 throttle_get_passive_io_policy(uthread_t
*ut
)
1597 *ut
= get_bsdthread_info(current_thread());
1599 return (proc_get_effective_thread_policy(current_thread(), TASK_POLICY_PASSIVE_IO
));
1604 throttle_get_thread_throttle_level(uthread_t ut
)
1606 uthread_t
*ut_p
= (ut
== NULL
) ? &ut
: NULL
;
1607 int io_tier
= throttle_get_io_policy(ut_p
);
1609 return throttle_get_thread_throttle_level_internal(ut
, io_tier
);
1613 * Return a throttle level given an existing I/O tier (such as returned by throttle_get_io_policy)
1616 throttle_get_thread_throttle_level_internal(uthread_t ut
, int io_tier
) {
1617 int thread_throttle_level
= io_tier
;
1618 int user_idle_level
;
1622 /* Bootcache misses should always be throttled */
1623 if (ut
->uu_throttle_bc
== TRUE
)
1624 thread_throttle_level
= THROTTLE_LEVEL_TIER3
;
1627 * Issue tier3 I/O as tier2 when the user is idle
1628 * to allow maintenance tasks to make more progress.
1630 * Assume any positive idle level is enough... for now it's
1631 * only ever 0 or 128 but this is not defined anywhere.
1633 if (thread_throttle_level
>= THROTTLE_LEVEL_TIER3
) {
1634 user_idle_level
= timer_get_user_idle_level();
1635 if (user_idle_level
> 0) {
1636 thread_throttle_level
--;
1640 return (thread_throttle_level
);
1644 * I/O will be throttled if either of the following are true:
1645 * - Higher tiers have in-flight I/O
1646 * - The time delta since the last start/completion of a higher tier is within the throttle window interval
1648 * In-flight I/O is bookended by throttle_info_update_internal/throttle_info_end_io_internal
1651 throttle_io_will_be_throttled_internal(void * throttle_info
, int * mylevel
, int * throttling_level
)
1653 struct _throttle_io_info_t
*info
= throttle_info
;
1654 struct timeval elapsed
;
1656 uint64_t elapsed_msecs
;
1657 int thread_throttle_level
;
1660 if ((thread_throttle_level
= throttle_get_thread_throttle_level(NULL
)) < THROTTLE_LEVEL_THROTTLED
)
1661 return (THROTTLE_DISENGAGED
);
1665 for (throttle_level
= THROTTLE_LEVEL_START
; throttle_level
< thread_throttle_level
; throttle_level
++) {
1666 if (info
->throttle_inflight_count
[throttle_level
]) {
1670 timevalsub(&elapsed
, &info
->throttle_window_start_timestamp
[throttle_level
]);
1671 elapsed_msecs
= (uint64_t)elapsed
.tv_sec
* (uint64_t)1000 + (elapsed
.tv_usec
/ 1000);
1673 if (elapsed_msecs
< (uint64_t)throttle_windows_msecs
[thread_throttle_level
])
1676 if (throttle_level
>= thread_throttle_level
) {
1678 * we're beyond all of the throttle windows
1679 * that affect the throttle level of this thread,
1680 * so go ahead and treat as normal I/O
1682 return (THROTTLE_DISENGAGED
);
1685 *mylevel
= thread_throttle_level
;
1686 if (throttling_level
)
1687 *throttling_level
= throttle_level
;
1689 if (info
->throttle_io_count
!= info
->throttle_io_count_begin
) {
1691 * we've already issued at least one throttleable I/O
1692 * in the current I/O window, so avoid issuing another one
1694 return (THROTTLE_NOW
);
1697 * we're in the throttle window, so
1698 * cut the I/O size back
1700 return (THROTTLE_ENGAGED
);
1704 * If we have a mount point and it has a throttle info pointer then
1705 * use it to do the check, otherwise use the device unit number to find
1706 * the correct throttle info array element.
1709 throttle_io_will_be_throttled(__unused
int lowpri_window_msecs
, mount_t mp
)
1711 struct _throttle_io_info_t
*info
;
1714 * Should we just return zero if no mount point
1717 info
= &_throttle_io_info
[LOWPRI_MAX_NUM_DEV
- 1];
1718 else if (mp
->mnt_throttle_info
== NULL
)
1719 info
= &_throttle_io_info
[mp
->mnt_devbsdunit
];
1721 info
= mp
->mnt_throttle_info
;
1723 if (info
->throttle_is_fusion_with_priority
) {
1724 uthread_t ut
= get_bsdthread_info(current_thread());
1725 if (ut
->uu_lowpri_window
== 0)
1726 return (THROTTLE_DISENGAGED
);
1729 if (info
->throttle_disabled
)
1730 return (THROTTLE_DISENGAGED
);
1732 return throttle_io_will_be_throttled_internal(info
, NULL
, NULL
);
1736 * Routine to increment I/O throttling counters maintained in the proc
1740 throttle_update_proc_stats(pid_t throttling_pid
, int count
)
1742 proc_t throttling_proc
;
1743 proc_t throttled_proc
= current_proc();
1745 /* The throttled_proc is always the current proc; so we are not concerned with refs */
1746 OSAddAtomic64(count
, &(throttled_proc
->was_throttled
));
1748 /* The throttling pid might have exited by now */
1749 throttling_proc
= proc_find(throttling_pid
);
1750 if (throttling_proc
!= PROC_NULL
) {
1751 OSAddAtomic64(count
, &(throttling_proc
->did_throttle
));
1752 proc_rele(throttling_proc
);
1757 * Block until woken up by the throttle timer or by a rethrottle call.
1758 * As long as we hold the throttle_lock while querying the throttle tier, we're
1759 * safe against seeing an old throttle tier after a rethrottle.
1762 throttle_lowpri_io(int sleep_amount
)
1765 struct _throttle_io_info_t
*info
;
1766 int throttle_type
= 0;
1768 int throttling_level
= THROTTLE_LEVEL_NONE
;
1770 uint32_t throttle_io_period_num
= 0;
1771 boolean_t insert_tail
= TRUE
;
1774 ut
= get_bsdthread_info(current_thread());
1776 if (ut
->uu_lowpri_window
== 0)
1779 info
= ut
->uu_throttle_info
;
1782 ut
->uu_throttle_bc
= FALSE
;
1783 ut
->uu_lowpri_window
= 0;
1786 lck_mtx_lock(&info
->throttle_lock
);
1787 assert(ut
->uu_on_throttlelist
< THROTTLE_LEVEL_THROTTLED
);
1789 if (sleep_amount
== 0)
1792 if (sleep_amount
== 1 && ut
->uu_throttle_bc
== FALSE
)
1795 throttle_io_period_num
= info
->throttle_io_period_num
;
1797 ut
->uu_was_rethrottled
= FALSE
;
1799 while ( (throttle_type
= throttle_io_will_be_throttled_internal(info
, &mylevel
, &throttling_level
)) ) {
1801 if (throttle_type
== THROTTLE_ENGAGED
) {
1802 if (sleep_amount
== 0)
1804 if (info
->throttle_io_period_num
< throttle_io_period_num
)
1806 if ((info
->throttle_io_period_num
- throttle_io_period_num
) >= (uint32_t)sleep_amount
)
1810 * keep the same position in the list if "rethrottle_thread" changes our throttle level and
1811 * then puts us back to the original level before we get a chance to run
1813 if (ut
->uu_on_throttlelist
>= THROTTLE_LEVEL_THROTTLED
&& ut
->uu_on_throttlelist
!= mylevel
) {
1815 * must have been awakened via "rethrottle_thread" (the timer pulls us off the list)
1816 * and we've changed our throttling level, so pull ourselves off of the appropriate list
1817 * and make sure we get put on the tail of the new list since we're starting anew w/r to
1818 * the throttling engine
1820 TAILQ_REMOVE(&info
->throttle_uthlist
[ut
->uu_on_throttlelist
], ut
, uu_throttlelist
);
1821 ut
->uu_on_throttlelist
= THROTTLE_LEVEL_NONE
;
1824 if (ut
->uu_on_throttlelist
< THROTTLE_LEVEL_THROTTLED
) {
1825 if (throttle_add_to_list(info
, ut
, mylevel
, insert_tail
) == THROTTLE_LEVEL_END
)
1828 assert(throttling_level
>= THROTTLE_LEVEL_START
&& throttling_level
<= THROTTLE_LEVEL_END
);
1830 s
= ml_set_interrupts_enabled(FALSE
);
1831 lck_spin_lock(&ut
->uu_rethrottle_lock
);
1834 * this is the critical section w/r to our interaction
1835 * with "rethrottle_thread"
1837 if (ut
->uu_was_rethrottled
== TRUE
) {
1839 lck_spin_unlock(&ut
->uu_rethrottle_lock
);
1840 ml_set_interrupts_enabled(s
);
1841 lck_mtx_yield(&info
->throttle_lock
);
1843 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_FSRW
, 103)), thread_tid(ut
->uu_thread
), ut
->uu_on_throttlelist
, 0, 0, 0);
1845 ut
->uu_was_rethrottled
= FALSE
;
1848 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_THROTTLE
, PROCESS_THROTTLED
)) | DBG_FUNC_NONE
,
1849 info
->throttle_last_IO_pid
[throttling_level
], throttling_level
, proc_selfpid(), mylevel
, 0);
1851 if (sleep_cnt
== 0) {
1852 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_FSRW
, 97)) | DBG_FUNC_START
,
1853 throttle_windows_msecs
[mylevel
], info
->throttle_io_periods
[mylevel
], info
->throttle_io_count
, 0, 0);
1854 throttled_count
[mylevel
]++;
1856 ut
->uu_wmesg
= "throttle_lowpri_io";
1858 assert_wait((caddr_t
)&ut
->uu_on_throttlelist
, THREAD_UNINT
);
1860 ut
->uu_is_throttled
= TRUE
;
1861 lck_spin_unlock(&ut
->uu_rethrottle_lock
);
1862 ml_set_interrupts_enabled(s
);
1864 lck_mtx_unlock(&info
->throttle_lock
);
1866 thread_block(THREAD_CONTINUE_NULL
);
1868 ut
->uu_wmesg
= NULL
;
1870 ut
->uu_is_throttled
= FALSE
;
1871 ut
->uu_was_rethrottled
= FALSE
;
1873 lck_mtx_lock(&info
->throttle_lock
);
1877 if (sleep_amount
== 0)
1878 insert_tail
= FALSE
;
1879 else if (info
->throttle_io_period_num
< throttle_io_period_num
||
1880 (info
->throttle_io_period_num
- throttle_io_period_num
) >= (uint32_t)sleep_amount
) {
1881 insert_tail
= FALSE
;
1886 if (ut
->uu_on_throttlelist
>= THROTTLE_LEVEL_THROTTLED
) {
1887 TAILQ_REMOVE(&info
->throttle_uthlist
[ut
->uu_on_throttlelist
], ut
, uu_throttlelist
);
1888 ut
->uu_on_throttlelist
= THROTTLE_LEVEL_NONE
;
1890 lck_mtx_unlock(&info
->throttle_lock
);
1893 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_FSRW
, 97)) | DBG_FUNC_END
,
1894 throttle_windows_msecs
[mylevel
], info
->throttle_io_periods
[mylevel
], info
->throttle_io_count
, 0, 0);
1896 * We update the stats for the last pid which opened a throttle window for the throttled thread.
1897 * This might not be completely accurate since the multiple throttles seen by the lower tier pid
1898 * might have been caused by various higher prio pids. However, updating these stats accurately
1899 * means doing a proc_find while holding the throttle lock which leads to deadlock.
1901 throttle_update_proc_stats(info
->throttle_last_IO_pid
[throttling_level
], sleep_cnt
);
1904 ut
->uu_throttle_info
= NULL
;
1905 ut
->uu_throttle_bc
= FALSE
;
1906 ut
->uu_lowpri_window
= 0;
1908 throttle_info_rel(info
);
1916 * set a kernel thread's IO policy. policy can be:
1917 * IOPOL_NORMAL, IOPOL_THROTTLE, IOPOL_PASSIVE, IOPOL_UTILITY, IOPOL_STANDARD
1919 * explanations about these policies are in the man page of setiopolicy_np
1921 void throttle_set_thread_io_policy(int policy
)
1923 proc_set_thread_policy(current_thread(), TASK_POLICY_INTERNAL
, TASK_POLICY_IOPOL
, policy
);
1926 void throttle_info_reset_window(uthread_t ut
)
1928 struct _throttle_io_info_t
*info
;
1931 ut
= get_bsdthread_info(current_thread());
1933 if ( (info
= ut
->uu_throttle_info
) ) {
1934 throttle_info_rel(info
);
1936 ut
->uu_throttle_info
= NULL
;
1937 ut
->uu_lowpri_window
= 0;
1938 ut
->uu_throttle_bc
= FALSE
;
1943 void throttle_info_set_initial_window(uthread_t ut
, struct _throttle_io_info_t
*info
, boolean_t BC_throttle
, boolean_t isssd
)
1945 if (lowpri_throttle_enabled
== 0 || info
->throttle_disabled
)
1948 if (info
->throttle_io_periods
== 0) {
1949 throttle_init_throttle_period(info
, isssd
);
1951 if (ut
->uu_throttle_info
== NULL
) {
1953 ut
->uu_throttle_info
= info
;
1954 throttle_info_ref(info
);
1955 DEBUG_ALLOC_THROTTLE_INFO("updating info = %p\n", info
, info
);
1957 ut
->uu_lowpri_window
= 1;
1958 ut
->uu_throttle_bc
= BC_throttle
;
1963 * Update inflight IO count and throttling window
1964 * Should be called when an IO is done
1966 * Only affects IO that was sent through spec_strategy
1968 void throttle_info_end_io(buf_t bp
) {
1970 struct bufattr
*bap
;
1971 struct _throttle_io_info_t
*info
;
1975 if (!ISSET(bap
->ba_flags
, BA_STRATEGY_TRACKED_IO
)) {
1978 CLR(bap
->ba_flags
, BA_STRATEGY_TRACKED_IO
);
1980 mp
= buf_vnode(bp
)->v_mount
;
1982 info
= &_throttle_io_info
[mp
->mnt_devbsdunit
];
1984 info
= &_throttle_io_info
[LOWPRI_MAX_NUM_DEV
- 1];
1987 io_tier
= GET_BUFATTR_IO_TIER(bap
);
1988 if (ISSET(bap
->ba_flags
, BA_IO_TIER_UPGRADE
)) {
1992 throttle_info_end_io_internal(info
, io_tier
);
1996 * Decrement inflight count initially incremented by throttle_info_update_internal
1999 void throttle_info_end_io_internal(struct _throttle_io_info_t
*info
, int throttle_level
) {
2000 if (throttle_level
== THROTTLE_LEVEL_NONE
) {
2004 microuptime(&info
->throttle_window_start_timestamp
[throttle_level
]);
2005 OSDecrementAtomic(&info
->throttle_inflight_count
[throttle_level
]);
2006 assert(info
->throttle_inflight_count
[throttle_level
] >= 0);
2010 * If inflight is TRUE and bap is NULL then the caller is responsible for calling
2011 * throttle_info_end_io_internal to avoid leaking in-flight I/O.
2014 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
)
2016 int thread_throttle_level
;
2018 if (lowpri_throttle_enabled
== 0 || info
->throttle_disabled
)
2019 return THROTTLE_LEVEL_NONE
;
2022 ut
= get_bsdthread_info(current_thread());
2024 if (bap
&& inflight
&& !ut
->uu_throttle_bc
) {
2025 thread_throttle_level
= GET_BUFATTR_IO_TIER(bap
);
2026 if (ISSET(bap
->ba_flags
, BA_IO_TIER_UPGRADE
)) {
2027 thread_throttle_level
--;
2030 thread_throttle_level
= throttle_get_thread_throttle_level(ut
);
2033 if (thread_throttle_level
!= THROTTLE_LEVEL_NONE
) {
2034 if(!ISSET(flags
, B_PASSIVE
)) {
2035 info
->throttle_last_IO_pid
[thread_throttle_level
] = proc_selfpid();
2036 if (inflight
&& !ut
->uu_throttle_bc
) {
2038 SET(bap
->ba_flags
, BA_STRATEGY_TRACKED_IO
);
2040 OSIncrementAtomic(&info
->throttle_inflight_count
[thread_throttle_level
]);
2042 microuptime(&info
->throttle_window_start_timestamp
[thread_throttle_level
]);
2044 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_THROTTLE
, OPEN_THROTTLE_WINDOW
)) | DBG_FUNC_NONE
,
2045 current_proc()->p_pid
, thread_throttle_level
, 0, 0, 0);
2047 microuptime(&info
->throttle_last_IO_timestamp
[thread_throttle_level
]);
2051 if (thread_throttle_level
>= THROTTLE_LEVEL_THROTTLED
) {
2053 * I'd really like to do the IOSleep here, but
2054 * we may be holding all kinds of filesystem related locks
2055 * and the pages for this I/O marked 'busy'...
2056 * we don't want to cause a normal task to block on
2057 * one of these locks while we're throttling a task marked
2058 * for low priority I/O... we'll mark the uthread and
2059 * do the delay just before we return from the system
2060 * call that triggered this I/O or from vnode_pagein
2062 OSAddAtomic(1, &info
->throttle_io_count
);
2064 throttle_info_set_initial_window(ut
, info
, FALSE
, isssd
);
2067 return thread_throttle_level
;
2070 void *throttle_info_update_by_mount(mount_t mp
)
2072 struct _throttle_io_info_t
*info
;
2074 boolean_t isssd
= FALSE
;
2076 ut
= get_bsdthread_info(current_thread());
2079 if (disk_conditioner_mount_is_ssd(mp
))
2081 info
= &_throttle_io_info
[mp
->mnt_devbsdunit
];
2083 info
= &_throttle_io_info
[LOWPRI_MAX_NUM_DEV
- 1];
2085 if (!ut
->uu_lowpri_window
)
2086 throttle_info_set_initial_window(ut
, info
, FALSE
, isssd
);
2095 * this is usually called before every I/O, used for throttled I/O
2096 * book keeping. This routine has low overhead and does not sleep
2098 void throttle_info_update(void *throttle_info
, int flags
)
2101 throttle_info_update_internal(throttle_info
, NULL
, flags
, FALSE
, FALSE
, NULL
);
2107 * this is usually called before every I/O, used for throttled I/O
2108 * book keeping. This routine has low overhead and does not sleep
2110 void throttle_info_update_by_mask(void *throttle_info_handle
, int flags
)
2112 void *throttle_info
= throttle_info_handle
;
2115 * for now we only use the lowest bit of the throttle mask, so the
2116 * handle is the same as the throttle_info. Later if we store a
2117 * set of throttle infos in the handle, we will want to loop through
2118 * them and call throttle_info_update in a loop
2120 throttle_info_update(throttle_info
, flags
);
2125 * This routine marks the throttle info as disabled. Used for mount points which
2126 * support I/O scheduling.
2129 void throttle_info_disable_throttle(int devno
, boolean_t isfusion
)
2131 struct _throttle_io_info_t
*info
;
2133 if (devno
< 0 || devno
>= LOWPRI_MAX_NUM_DEV
)
2134 panic("Illegal devno (%d) passed into throttle_info_disable_throttle()", devno
);
2136 info
= &_throttle_io_info
[devno
];
2137 // don't disable software throttling on devices that are part of a fusion device
2138 // and override the software throttle periods to use HDD periods
2140 info
->throttle_is_fusion_with_priority
= isfusion
;
2141 throttle_init_throttle_period(info
, FALSE
);
2143 info
->throttle_disabled
= !info
->throttle_is_fusion_with_priority
;
2149 * KPI routine (private)
2150 * Called to determine if this IO is being throttled to this level so that it can be treated specially
2152 int throttle_info_io_will_be_throttled(void * throttle_info
, int policy
)
2154 struct _throttle_io_info_t
*info
= throttle_info
;
2155 struct timeval elapsed
;
2156 uint64_t elapsed_msecs
;
2158 int thread_throttle_level
;
2162 case IOPOL_THROTTLE
:
2163 thread_throttle_level
= THROTTLE_LEVEL_TIER3
;
2166 thread_throttle_level
= THROTTLE_LEVEL_TIER2
;
2168 case IOPOL_STANDARD
:
2169 thread_throttle_level
= THROTTLE_LEVEL_TIER1
;
2172 thread_throttle_level
= THROTTLE_LEVEL_TIER0
;
2175 for (throttle_level
= THROTTLE_LEVEL_START
; throttle_level
< thread_throttle_level
; throttle_level
++) {
2176 if (info
->throttle_inflight_count
[throttle_level
]) {
2180 microuptime(&elapsed
);
2181 timevalsub(&elapsed
, &info
->throttle_window_start_timestamp
[throttle_level
]);
2182 elapsed_msecs
= (uint64_t)elapsed
.tv_sec
* (uint64_t)1000 + (elapsed
.tv_usec
/ 1000);
2184 if (elapsed_msecs
< (uint64_t)throttle_windows_msecs
[thread_throttle_level
])
2187 if (throttle_level
>= thread_throttle_level
) {
2189 * we're beyond all of the throttle windows
2190 * so go ahead and treat as normal I/O
2192 return (THROTTLE_DISENGAGED
);
2195 * we're in the throttle window
2197 return (THROTTLE_ENGAGED
);
2200 int throttle_lowpri_window(void)
2202 struct uthread
*ut
= get_bsdthread_info(current_thread());
2203 return ut
->uu_lowpri_window
;
2208 int upl_get_cached_tier(void *);
2212 spec_strategy(struct vnop_strategy_args
*ap
)
2221 struct bufattr
*bap
;
2223 struct _throttle_io_info_t
*throttle_info
;
2224 boolean_t isssd
= FALSE
;
2225 boolean_t inflight
= FALSE
;
2226 boolean_t upgrade
= FALSE
;
2229 #if !CONFIG_EMBEDDED
2230 proc_t curproc
= current_proc();
2231 #endif /* !CONFIG_EMBEDDED */
2234 bdev
= buf_device(bp
);
2235 mp
= buf_vnode(bp
)->v_mount
;
2239 if (bp
->b_flags
& B_CLUSTER
) {
2241 io_tier
= upl_get_cached_tier(bp
->b_upl
);
2244 io_tier
= throttle_get_io_policy(&ut
);
2245 #if DEVELOPMENT || DEBUG
2247 int my_io_tier
= throttle_get_io_policy(&ut
);
2249 if (io_tier
!= my_io_tier
)
2250 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);
2254 io_tier
= throttle_get_io_policy(&ut
);
2256 io_tier
= throttle_get_io_policy(&ut
);
2258 passive
= throttle_get_passive_io_policy(&ut
);
2261 * Mark if the I/O was upgraded by throttle_get_thread_throttle_level
2262 * while preserving the original issued tier (throttle_get_io_policy
2263 * does not return upgraded tiers)
2265 if (mp
&& io_tier
> throttle_get_thread_throttle_level_internal(ut
, io_tier
)) {
2267 if (!(mp
->mnt_ioflags
& MNT_IOFLAGS_IOSCHED_SUPPORTED
)) {
2270 #else /* CONFIG_IOSCHED */
2272 #endif /* CONFIG_IOSCHED */
2275 if (bp
->b_flags
& B_META
)
2276 bap
->ba_flags
|= BA_META
;
2280 * For I/O Scheduling, we currently do not have a way to track and expedite metadata I/Os.
2281 * To ensure we dont get into priority inversions due to metadata I/Os, we use the following rules:
2282 * For metadata reads, ceil all I/Os to IOSCHED_METADATA_TIER & mark them passive if the I/O tier was upgraded
2283 * For metadata writes, unconditionally mark them as IOSCHED_METADATA_TIER and passive
2285 if (bap
->ba_flags
& BA_META
) {
2286 if (mp
&& (mp
->mnt_ioflags
& MNT_IOFLAGS_IOSCHED_SUPPORTED
)) {
2287 if (bp
->b_flags
& B_READ
) {
2288 if (io_tier
> IOSCHED_METADATA_TIER
) {
2289 io_tier
= IOSCHED_METADATA_TIER
;
2293 io_tier
= IOSCHED_METADATA_TIER
;
2298 #endif /* CONFIG_IOSCHED */
2300 SET_BUFATTR_IO_TIER(bap
, io_tier
);
2303 bp
->b_flags
|= B_PASSIVE
;
2304 bap
->ba_flags
|= BA_PASSIVE
;
2307 #if !CONFIG_EMBEDDED
2308 if ((curproc
!= NULL
) && ((curproc
->p_flag
& P_DELAYIDLESLEEP
) == P_DELAYIDLESLEEP
))
2309 bap
->ba_flags
|= BA_DELAYIDLESLEEP
;
2310 #endif /* !CONFIG_EMBEDDED */
2312 bflags
= bp
->b_flags
;
2314 if (((bflags
& B_READ
) == 0) && ((bflags
& B_ASYNC
) == 0))
2315 bufattr_markquickcomplete(bap
);
2317 if (bflags
& B_READ
)
2319 if (bflags
& B_ASYNC
)
2322 if (bap
->ba_flags
& BA_META
)
2324 else if (bflags
& B_PAGEIO
)
2325 code
|= DKIO_PAGING
;
2328 code
|= DKIO_THROTTLE
;
2330 code
|= ((io_tier
<< DKIO_TIER_SHIFT
) & DKIO_TIER_MASK
);
2332 if (bflags
& B_PASSIVE
)
2333 code
|= DKIO_PASSIVE
;
2335 if (bap
->ba_flags
& BA_NOCACHE
)
2336 code
|= DKIO_NOCACHE
;
2339 code
|= DKIO_TIER_UPGRADE
;
2340 SET(bap
->ba_flags
, BA_IO_TIER_UPGRADE
);
2343 if (kdebug_enable
) {
2344 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON
, FSDBG_CODE(DBG_DKRW
, code
) | DBG_FUNC_NONE
,
2345 buf_kernel_addrperm_addr(bp
), bdev
, (int)buf_blkno(bp
), buf_count(bp
), 0);
2348 thread_update_io_stats(current_thread(), buf_count(bp
), code
);
2351 if (disk_conditioner_mount_is_ssd(mp
))
2354 * Partially initialized mounts don't have a final devbsdunit and should not be tracked.
2355 * Verify that devbsdunit is initialized (non-zero) or that 0 is the correct initialized value
2356 * (mnt_throttle_mask is initialized and num_trailing_0 would be 0)
2358 if (mp
->mnt_devbsdunit
|| (mp
->mnt_throttle_mask
!= LOWPRI_MAX_NUM_DEV
- 1 && mp
->mnt_throttle_mask
& 0x1)) {
2361 throttle_info
= &_throttle_io_info
[mp
->mnt_devbsdunit
];
2364 throttle_info
= &_throttle_io_info
[LOWPRI_MAX_NUM_DEV
- 1];
2366 throttle_info_update_internal(throttle_info
, ut
, bflags
, isssd
, inflight
, bap
);
2368 if ((bflags
& B_READ
) == 0) {
2369 microuptime(&throttle_info
->throttle_last_write_timestamp
);
2372 mp
->mnt_last_write_issued_timestamp
= throttle_info
->throttle_last_write_timestamp
;
2373 INCR_PENDING_IO(buf_count(bp
), mp
->mnt_pending_write_size
);
2376 INCR_PENDING_IO(buf_count(bp
), mp
->mnt_pending_read_size
);
2379 * The BootCache may give us special information about
2380 * the IO, so it returns special values that we check
2383 * IO_SATISFIED_BY_CACHE
2384 * The read has been satisfied by the boot cache. Don't
2385 * throttle the thread unnecessarily.
2387 * IO_SHOULD_BE_THROTTLED
2388 * The boot cache is playing back a playlist and this IO
2389 * cut through. Throttle it so we're not cutting through
2390 * the boot cache too often.
2392 * Note that typical strategy routines are defined with
2393 * a void return so we'll get garbage here. In the
2394 * unlikely case the garbage matches our special return
2395 * value, it's not a big deal since we're only adjusting
2396 * the throttling delay.
2398 #define IO_SATISFIED_BY_CACHE ((int)0xcafefeed)
2399 #define IO_SHOULD_BE_THROTTLED ((int)0xcafebeef)
2400 typedef int strategy_fcn_ret_t(struct buf
*bp
);
2402 strategy_ret
= (*(strategy_fcn_ret_t
*)bdevsw
[major(bdev
)].d_strategy
)(bp
);
2404 // disk conditioner needs to track when this I/O actually starts
2405 // which means track it after `strategy` which may include delays
2406 // from inflight I/Os
2407 microuptime(&bp
->b_timestamp_tv
);
2409 if (IO_SATISFIED_BY_CACHE
== strategy_ret
) {
2411 * If this was a throttled IO satisfied by the boot cache,
2412 * don't delay the thread.
2414 throttle_info_reset_window(ut
);
2416 } else if (IO_SHOULD_BE_THROTTLED
== strategy_ret
) {
2418 * If the boot cache indicates this IO should be throttled,
2421 throttle_info_set_initial_window(ut
, throttle_info
, TRUE
, isssd
);
2428 * This is a noop, simply returning what one has been given.
2431 spec_blockmap(__unused
struct vnop_blockmap_args
*ap
)
2438 * Device close routine
2441 spec_close(struct vnop_close_args
*ap
)
2443 struct vnode
*vp
= ap
->a_vp
;
2444 dev_t dev
= vp
->v_rdev
;
2446 int flags
= ap
->a_fflag
;
2447 struct proc
*p
= vfs_context_proc(ap
->a_context
);
2448 struct session
*sessp
;
2450 switch (vp
->v_type
) {
2454 * Hack: a tty device that is a controlling terminal
2455 * has a reference from the session structure.
2456 * We cannot easily tell that a character device is
2457 * a controlling terminal, unless it is the closing
2458 * process' controlling terminal. In that case,
2459 * if the reference count is 1 (this is the very
2462 sessp
= proc_session(p
);
2463 devsw_lock(dev
, S_IFCHR
);
2464 if (sessp
!= SESSION_NULL
) {
2465 if (vp
== sessp
->s_ttyvp
&& vcount(vp
) == 1) {
2466 struct tty
*tp
= TTY_NULL
;
2468 devsw_unlock(dev
, S_IFCHR
);
2469 session_lock(sessp
);
2470 if (vp
== sessp
->s_ttyvp
) {
2471 tp
= SESSION_TP(sessp
);
2472 sessp
->s_ttyvp
= NULL
;
2473 sessp
->s_ttyvid
= 0;
2474 sessp
->s_ttyp
= TTY_NULL
;
2475 sessp
->s_ttypgrpid
= NO_PID
;
2477 session_unlock(sessp
);
2479 if (tp
!= TTY_NULL
) {
2481 * We may have won a race with a proc_exit
2482 * of the session leader, the winner
2483 * clears the flag (even if not set)
2491 devsw_lock(dev
, S_IFCHR
);
2493 session_rele(sessp
);
2496 if (--vp
->v_specinfo
->si_opencount
< 0)
2497 panic("negative open count (c, %u, %u)", major(dev
), minor(dev
));
2500 * close on last reference or on vnode revoke call
2502 if (vcount(vp
) == 0 || (flags
& IO_REVOKE
) != 0)
2503 error
= cdevsw
[major(dev
)].d_close(dev
, flags
, S_IFCHR
, p
);
2505 devsw_unlock(dev
, S_IFCHR
);
2510 * If there is more than one outstanding open, don't
2511 * send the close to the device.
2513 devsw_lock(dev
, S_IFBLK
);
2514 if (vcount(vp
) > 1) {
2515 vp
->v_specinfo
->si_opencount
--;
2516 devsw_unlock(dev
, S_IFBLK
);
2519 devsw_unlock(dev
, S_IFBLK
);
2522 * On last close of a block device (that isn't mounted)
2523 * we must invalidate any in core blocks, so that
2524 * we can, for instance, change floppy disks.
2526 if ((error
= spec_fsync_internal(vp
, MNT_WAIT
, ap
->a_context
)))
2529 error
= buf_invalidateblks(vp
, BUF_WRITE_DATA
, 0, 0);
2533 devsw_lock(dev
, S_IFBLK
);
2535 if (--vp
->v_specinfo
->si_opencount
< 0)
2536 panic("negative open count (b, %u, %u)", major(dev
), minor(dev
));
2538 if (vcount(vp
) == 0)
2539 error
= bdevsw
[major(dev
)].d_close(dev
, flags
, S_IFBLK
, p
);
2541 devsw_unlock(dev
, S_IFBLK
);
2545 panic("spec_close: not special");
2553 * Return POSIX pathconf information applicable to special devices.
2556 spec_pathconf(struct vnop_pathconf_args
*ap
)
2559 switch (ap
->a_name
) {
2561 *ap
->a_retval
= LINK_MAX
;
2564 *ap
->a_retval
= MAX_CANON
;
2567 *ap
->a_retval
= MAX_INPUT
;
2570 *ap
->a_retval
= PIPE_BUF
;
2572 case _PC_CHOWN_RESTRICTED
:
2573 *ap
->a_retval
= 200112; /* _POSIX_CHOWN_RESTRICTED */
2576 *ap
->a_retval
= _POSIX_VDISABLE
;
2585 * Special device failed operation
2588 spec_ebadf(__unused
void *dummy
)
2594 /* Blktooff derives file offset from logical block number */
2596 spec_blktooff(struct vnop_blktooff_args
*ap
)
2598 struct vnode
*vp
= ap
->a_vp
;
2600 switch (vp
->v_type
) {
2602 *ap
->a_offset
= (off_t
)-1; /* failure */
2606 printf("spec_blktooff: not implemented for VBLK\n");
2607 *ap
->a_offset
= (off_t
)-1; /* failure */
2611 panic("spec_blktooff type");
2618 /* Offtoblk derives logical block number from file offset */
2620 spec_offtoblk(struct vnop_offtoblk_args
*ap
)
2622 struct vnode
*vp
= ap
->a_vp
;
2624 switch (vp
->v_type
) {
2626 *ap
->a_lblkno
= (daddr64_t
)-1; /* failure */
2630 printf("spec_offtoblk: not implemented for VBLK\n");
2631 *ap
->a_lblkno
= (daddr64_t
)-1; /* failure */
2635 panic("spec_offtoblk type");
2642 static void filt_specdetach(struct knote
*kn
);
2643 static int filt_specevent(struct knote
*kn
, long hint
);
2644 static int filt_spectouch(struct knote
*kn
, struct kevent_internal_s
*kev
);
2645 static int filt_specprocess(struct knote
*kn
, struct filt_process_s
*data
, struct kevent_internal_s
*kev
);
2646 static unsigned filt_specpeek(struct knote
*kn
);
2648 SECURITY_READ_ONLY_EARLY(struct filterops
) spec_filtops
= {
2650 .f_attach
= filt_specattach
,
2651 .f_detach
= filt_specdetach
,
2652 .f_event
= filt_specevent
,
2653 .f_touch
= filt_spectouch
,
2654 .f_process
= filt_specprocess
,
2655 .f_peek
= filt_specpeek
2660 * Given a waitq that is assumed to be embedded within a selinfo structure,
2661 * return the containing selinfo structure. While 'wq' is not really a queue
2662 * element, this macro simply does the offset_of calculation to get back to a
2663 * containing struct given the struct type and member name.
2665 #define selinfo_from_waitq(wq) \
2666 qe_element((wq), struct selinfo, si_waitq)
2669 spec_knote_select_and_link(struct knote
*kn
)
2674 struct waitq_set
*old_wqs
;
2675 uint64_t rsvd
, rsvd_arg
;
2676 uint64_t *rlptr
= NULL
;
2677 struct selinfo
*si
= NULL
;
2680 uth
= get_bsdthread_info(current_thread());
2682 ctx
= vfs_context_current();
2683 vp
= (vnode_t
)kn
->kn_fp
->f_fglob
->fg_data
;
2685 int error
= vnode_getwithvid(vp
, kn
->kn_hookid
);
2687 knote_set_error(kn
, ENOENT
);
2692 * This function may be called many times to link or re-link the
2693 * underlying vnode to the kqueue. If we've already linked the two,
2694 * we will have a valid kn_hook_data which ties us to the underlying
2695 * device's waitq via a the waitq's prepost table object. However,
2696 * devices can abort any select action by calling selthreadclear().
2697 * This is OK because the table object will be invalidated by the
2698 * driver (through a call to selthreadclear), so any attempt to access
2699 * the associated waitq will fail because the table object is invalid.
2701 * Even if we've already registered, we need to pass a pointer
2702 * to a reserved link structure. Otherwise, selrecord() will
2703 * infer that we're in the second pass of select() and won't
2704 * actually do anything!
2706 rsvd
= rsvd_arg
= waitq_link_reserve(NULL
);
2707 rlptr
= (void *)&rsvd_arg
;
2710 * Trick selrecord() into hooking kqueue's wait queue set
2711 * set into device's selinfo wait queue
2713 old_wqs
= uth
->uu_wqset
;
2714 uth
->uu_wqset
= &(knote_get_kq(kn
)->kq_wqs
);
2715 selres
= VNOP_SELECT(vp
, knote_get_seltype(kn
), 0, rlptr
, ctx
);
2716 uth
->uu_wqset
= old_wqs
;
2719 * make sure to cleanup the reserved link - this guards against
2720 * drivers that may not actually call selrecord().
2722 waitq_link_release(rsvd
);
2723 if (rsvd
!= rsvd_arg
) {
2724 /* the driver / handler called selrecord() */
2726 memcpy(&wq
, rlptr
, sizeof(void *));
2729 * The waitq is part of the selinfo structure managed by the
2730 * driver. For certain drivers, we want to hook the knote into
2731 * the selinfo structure's si_note field so selwakeup can call
2734 si
= selinfo_from_waitq(wq
);
2737 * The waitq_get_prepost_id() function will (potentially)
2738 * allocate a prepost table object for the waitq and return
2739 * the table object's ID to us. It will also set the
2740 * waitq_prepost_id field within the waitq structure.
2742 * We can just overwrite kn_hook_data because it's simply a
2743 * table ID used to grab a reference when needed.
2745 * We have a reference on the vnode, so we know that the
2746 * device won't go away while we get this ID.
2748 kn
->kn_hook_data
= waitq_get_prepost_id(wq
);
2750 assert(selres
!= 0);
2758 static void filt_spec_common(struct knote
*kn
, int selres
)
2760 if (kn
->kn_vnode_use_ofst
) {
2761 if (kn
->kn_fp
->f_fglob
->fg_offset
>= (uint32_t)selres
) {
2764 kn
->kn_data
= ((uint32_t)selres
) - kn
->kn_fp
->f_fglob
->fg_offset
;
2767 kn
->kn_data
= selres
;
2772 filt_specattach(struct knote
*kn
, __unused
struct kevent_internal_s
*kev
)
2777 vp
= (vnode_t
)kn
->kn_fp
->f_fglob
->fg_data
; /* Already have iocount, and vnode is alive */
2779 assert(vnode_ischr(vp
));
2781 dev
= vnode_specrdev(vp
);
2784 * For a few special kinds of devices, we can attach knotes with
2785 * no restrictions because their "select" vectors return the amount
2786 * of data available. Others require an explicit NOTE_LOWAT with
2787 * data of 1, indicating that the caller doesn't care about actual
2788 * data counts, just an indication that the device has data.
2790 if (!kn
->kn_vnode_kqok
&&
2791 ((kn
->kn_sfflags
& NOTE_LOWAT
) == 0 || kn
->kn_sdata
!= 1)) {
2792 knote_set_error(kn
, EINVAL
);
2797 * This forces the select fallback to call through VNOP_SELECT and hook
2798 * up selinfo on every filter routine.
2800 * Pseudo-terminal controllers are opted out of native kevent support --
2801 * remove this when they get their own EVFILTID.
2803 if (cdevsw_flags
[major(dev
)] & CDEVSW_IS_PTC
) {
2804 kn
->kn_vnode_kqok
= 0;
2807 kn
->kn_filtid
= EVFILTID_SPEC
;
2808 kn
->kn_hook_data
= 0;
2809 kn
->kn_hookid
= vnode_vid(vp
);
2811 knote_markstayactive(kn
);
2812 return spec_knote_select_and_link(kn
);
2816 filt_specdetach(struct knote
*kn
)
2818 knote_clearstayactive(kn
);
2821 * This is potentially tricky: the device's selinfo waitq that was
2822 * tricked into being part of this knote's waitq set may not be a part
2823 * of any other set, and the device itself may have revoked the memory
2824 * in which the waitq was held. We use the knote's kn_hook_data field
2825 * to keep the ID of the waitq's prepost table object. This
2826 * object keeps a pointer back to the waitq, and gives us a safe way
2827 * to decouple the dereferencing of driver allocated memory: if the
2828 * driver goes away (taking the waitq with it) then the prepost table
2829 * object will be invalidated. The waitq details are handled in the
2830 * waitq API invoked here.
2832 if (kn
->kn_hook_data
) {
2833 waitq_unlink_by_prepost_id(kn
->kn_hook_data
, &(knote_get_kq(kn
)->kq_wqs
));
2834 kn
->kn_hook_data
= 0;
2839 filt_specevent(struct knote
*kn
, __unused
long hint
)
2842 * Nothing should call knote or knote_vanish on this knote.
2844 panic("filt_specevent(%p)", kn
);
2849 filt_spectouch(struct knote
*kn
, struct kevent_internal_s
*kev
)
2851 kn
->kn_sdata
= kev
->data
;
2852 kn
->kn_sfflags
= kev
->fflags
;
2853 if ((kn
->kn_status
& KN_UDATA_SPECIFIC
) == 0)
2854 kn
->kn_udata
= kev
->udata
;
2856 if (kev
->flags
& EV_ENABLE
) {
2857 return spec_knote_select_and_link(kn
);
2864 filt_specprocess(struct knote
*kn
, struct filt_process_s
*data
, struct kevent_internal_s
*kev
)
2866 #pragma unused(data)
2874 uth
= get_bsdthread_info(current_thread());
2875 ctx
= vfs_context_current();
2876 vp
= (vnode_t
)kn
->kn_fp
->f_fglob
->fg_data
;
2878 /* FIXME JMM - locking against touches? */
2880 error
= vnode_getwithvid(vp
, kn
->kn_hookid
);
2882 kn
->kn_flags
|= (EV_EOF
| EV_ONESHOT
);
2883 *kev
= kn
->kn_kevent
;
2887 selres
= spec_knote_select_and_link(kn
);
2888 filt_spec_common(kn
, selres
);
2892 res
= ((kn
->kn_sfflags
& NOTE_LOWAT
) != 0) ?
2893 (kn
->kn_data
>= kn
->kn_sdata
) : kn
->kn_data
;
2896 *kev
= kn
->kn_kevent
;
2897 if (kn
->kn_flags
& EV_CLEAR
) {
2907 filt_specpeek(struct knote
*kn
)
2911 selres
= spec_knote_select_and_link(kn
);
2912 filt_spec_common(kn
, selres
);