]> git.saurik.com Git - apple/xnu.git/blame - bsd/miscfs/specfs/spec_vnops.c
xnu-6153.141.1.tar.gz
[apple/xnu.git] / bsd / miscfs / specfs / spec_vnops.c
CommitLineData
1c79356b 1/*
cb323159 2 * Copyright (c) 2000-2019 Apple Computer, Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
2d21ac55
A
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.
0a7de745 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
0a7de745 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29/*
30 * Copyright (c) 1989, 1993, 1995
31 * The Regents of the University of California. All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
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.
48 *
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
59 * SUCH DAMAGE.
60 *
61 * @(#)spec_vnops.c 8.14 (Berkeley) 5/21/95
62 */
63
64#include <sys/param.h>
91447636
A
65#include <sys/proc_internal.h>
66#include <sys/kauth.h>
1c79356b
A
67#include <sys/systm.h>
68#include <sys/kernel.h>
69#include <sys/conf.h>
91447636
A
70#include <sys/buf_internal.h>
71#include <sys/mount_internal.h>
91447636 72#include <sys/vnode_internal.h>
6d2010ae
A
73#include <sys/file_internal.h>
74#include <sys/namei.h>
1c79356b
A
75#include <sys/stat.h>
76#include <sys/errno.h>
77#include <sys/ioctl.h>
78#include <sys/file.h>
91447636 79#include <sys/user.h>
1c79356b 80#include <sys/malloc.h>
55e303ae 81#include <sys/disk.h>
91447636 82#include <sys/uio_internal.h>
2d21ac55 83#include <sys/resource.h>
39037602 84#include <machine/machine_routines.h>
1c79356b
A
85#include <miscfs/specfs/specdev.h>
86#include <vfs/vfs_support.h>
5ba3f43e 87#include <vfs/vfs_disk_conditioner.h>
39037602 88
6d2010ae
A
89#include <kern/assert.h>
90#include <kern/task.h>
39037602
A
91#include <kern/sched_prim.h>
92#include <kern/thread.h>
93#include <kern/policy_internal.h>
d190cdc3 94#include <kern/timer_call.h>
5ba3f43e 95#include <kern/waitq.h>
39037602 96
39236c6e 97#include <pexpert/pexpert.h>
1c79356b 98
9bccf70c 99#include <sys/kdebug.h>
5ba3f43e 100#include <libkern/section_keywords.h>
1c79356b 101
2d21ac55 102/* XXX following three prototypes should be in a header file somewhere */
0a7de745
A
103extern dev_t chrtoblk(dev_t dev);
104extern boolean_t iskmemdev(dev_t dev);
cb323159 105extern int bpfkqfilter(dev_t dev, struct knote *kn);
5ba3f43e 106extern int ptsd_kqfilter(dev_t, struct knote *);
5c9f4661 107extern int ptmx_kqfilter(dev_t, struct knote *);
316670eb 108
1c79356b
A
109struct vnode *speclisth[SPECHSZ];
110
111/* symbolic sleep message strings for devices */
0a7de745
A
112char devopn[] = "devopn";
113char devio[] = "devio";
114char devwait[] = "devwait";
115char devin[] = "devin";
116char devout[] = "devout";
117char devioc[] = "devioc";
118char devcls[] = "devcls";
1c79356b
A
119
120#define VOPFUNC int (*)(void *)
121
0a7de745 122int(**spec_vnodeop_p)(void *);
cb323159
A
123const struct vnodeopv_entry_desc spec_vnodeop_entries[] = {
124 { .opve_op = &vnop_default_desc, .opve_impl = (VOPFUNC)vn_default_error },
125 { .opve_op = &vnop_lookup_desc, .opve_impl = (VOPFUNC)spec_lookup }, /* lookup */
126 { .opve_op = &vnop_create_desc, .opve_impl = (VOPFUNC)err_create }, /* create */
127 { .opve_op = &vnop_mknod_desc, .opve_impl = (VOPFUNC)err_mknod }, /* mknod */
128 { .opve_op = &vnop_open_desc, .opve_impl = (VOPFUNC)spec_open }, /* open */
129 { .opve_op = &vnop_close_desc, .opve_impl = (VOPFUNC)spec_close }, /* close */
130 { .opve_op = &vnop_access_desc, .opve_impl = (VOPFUNC)spec_access }, /* access */
131 { .opve_op = &vnop_getattr_desc, .opve_impl = (VOPFUNC)spec_getattr }, /* getattr */
132 { .opve_op = &vnop_setattr_desc, .opve_impl = (VOPFUNC)spec_setattr }, /* setattr */
133 { .opve_op = &vnop_read_desc, .opve_impl = (VOPFUNC)spec_read }, /* read */
134 { .opve_op = &vnop_write_desc, .opve_impl = (VOPFUNC)spec_write }, /* write */
135 { .opve_op = &vnop_ioctl_desc, .opve_impl = (VOPFUNC)spec_ioctl }, /* ioctl */
136 { .opve_op = &vnop_select_desc, .opve_impl = (VOPFUNC)spec_select }, /* select */
137 { .opve_op = &vnop_revoke_desc, .opve_impl = (VOPFUNC)nop_revoke }, /* revoke */
138 { .opve_op = &vnop_mmap_desc, .opve_impl = (VOPFUNC)err_mmap }, /* mmap */
139 { .opve_op = &vnop_fsync_desc, .opve_impl = (VOPFUNC)spec_fsync }, /* fsync */
140 { .opve_op = &vnop_remove_desc, .opve_impl = (VOPFUNC)err_remove }, /* remove */
141 { .opve_op = &vnop_link_desc, .opve_impl = (VOPFUNC)err_link }, /* link */
142 { .opve_op = &vnop_rename_desc, .opve_impl = (VOPFUNC)err_rename }, /* rename */
143 { .opve_op = &vnop_mkdir_desc, .opve_impl = (VOPFUNC)err_mkdir }, /* mkdir */
144 { .opve_op = &vnop_rmdir_desc, .opve_impl = (VOPFUNC)err_rmdir }, /* rmdir */
145 { .opve_op = &vnop_symlink_desc, .opve_impl = (VOPFUNC)err_symlink }, /* symlink */
146 { .opve_op = &vnop_readdir_desc, .opve_impl = (VOPFUNC)err_readdir }, /* readdir */
147 { .opve_op = &vnop_readlink_desc, .opve_impl = (VOPFUNC)err_readlink }, /* readlink */
148 { .opve_op = &vnop_inactive_desc, .opve_impl = (VOPFUNC)nop_inactive }, /* inactive */
149 { .opve_op = &vnop_reclaim_desc, .opve_impl = (VOPFUNC)nop_reclaim }, /* reclaim */
150 { .opve_op = &vnop_strategy_desc, .opve_impl = (VOPFUNC)spec_strategy }, /* strategy */
151 { .opve_op = &vnop_pathconf_desc, .opve_impl = (VOPFUNC)spec_pathconf }, /* pathconf */
152 { .opve_op = &vnop_advlock_desc, .opve_impl = (VOPFUNC)err_advlock }, /* advlock */
153 { .opve_op = &vnop_bwrite_desc, .opve_impl = (VOPFUNC)spec_bwrite }, /* bwrite */
154 { .opve_op = &vnop_pagein_desc, .opve_impl = (VOPFUNC)err_pagein }, /* Pagein */
155 { .opve_op = &vnop_pageout_desc, .opve_impl = (VOPFUNC)err_pageout }, /* Pageout */
156 { .opve_op = &vnop_copyfile_desc, .opve_impl = (VOPFUNC)err_copyfile }, /* Copyfile */
157 { .opve_op = &vnop_blktooff_desc, .opve_impl = (VOPFUNC)spec_blktooff }, /* blktooff */
158 { .opve_op = &vnop_offtoblk_desc, .opve_impl = (VOPFUNC)spec_offtoblk }, /* offtoblk */
159 { .opve_op = &vnop_blockmap_desc, .opve_impl = (VOPFUNC)spec_blockmap }, /* blockmap */
160 { .opve_op = (struct vnodeop_desc*)NULL, .opve_impl = (int (*)(void *))NULL }
1c79356b 161};
cb323159
A
162const struct vnodeopv_desc spec_vnodeop_opv_desc =
163{ .opv_desc_vector_p = &spec_vnodeop_p, .opv_desc_ops = spec_vnodeop_entries };
1c79356b 164
91447636
A
165
166static void set_blocksize(vnode_t, dev_t);
167
0a7de745
A
168#define LOWPRI_TIER1_WINDOW_MSECS 25
169#define LOWPRI_TIER2_WINDOW_MSECS 100
170#define LOWPRI_TIER3_WINDOW_MSECS 500
91447636 171
0a7de745
A
172#define LOWPRI_TIER1_IO_PERIOD_MSECS 40
173#define LOWPRI_TIER2_IO_PERIOD_MSECS 85
174#define LOWPRI_TIER3_IO_PERIOD_MSECS 200
316670eb 175
39236c6e
A
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
316670eb 179
316670eb 180
0a7de745 181int throttle_windows_msecs[THROTTLE_LEVEL_END + 1] = {
39236c6e
A
182 0,
183 LOWPRI_TIER1_WINDOW_MSECS,
184 LOWPRI_TIER2_WINDOW_MSECS,
185 LOWPRI_TIER3_WINDOW_MSECS,
186};
187
0a7de745 188int throttle_io_period_msecs[THROTTLE_LEVEL_END + 1] = {
39236c6e
A
189 0,
190 LOWPRI_TIER1_IO_PERIOD_MSECS,
191 LOWPRI_TIER2_IO_PERIOD_MSECS,
192 LOWPRI_TIER3_IO_PERIOD_MSECS,
193};
194
0a7de745 195int throttle_io_period_ssd_msecs[THROTTLE_LEVEL_END + 1] = {
39236c6e
A
196 0,
197 LOWPRI_TIER1_IO_PERIOD_SSD_MSECS,
198 LOWPRI_TIER2_IO_PERIOD_SSD_MSECS,
199 LOWPRI_TIER3_IO_PERIOD_SSD_MSECS,
200};
201
202
0a7de745 203int throttled_count[THROTTLE_LEVEL_END + 1];
316670eb 204
7ddcb079 205struct _throttle_io_info_t {
0a7de745
A
206 lck_mtx_t throttle_lock;
207
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];
39037602 214 int32_t throttle_inflight_count[THROTTLE_LEVEL_END + 1];
316670eb 215
0a7de745
A
216 TAILQ_HEAD(, uthread) throttle_uthlist[THROTTLE_LEVEL_END + 1]; /* Lists of throttled uthreads */
217 int throttle_next_wake_level;
316670eb 218
0a7de745
A
219 thread_call_t throttle_timer_call;
220 int32_t throttle_timer_ref;
221 int32_t throttle_timer_active;
39236c6e 222
0a7de745
A
223 int32_t throttle_io_count;
224 int32_t throttle_io_count_begin;
225 int *throttle_io_periods;
316670eb 226 uint32_t throttle_io_period_num;
39236c6e 227
316670eb
A
228 int32_t throttle_refcnt;
229 int32_t throttle_alloc;
fe8ab488 230 int32_t throttle_disabled;
3e170ce0 231 int32_t throttle_is_fusion_with_priority;
7ddcb079
A
232};
233
234struct _throttle_io_info_t _throttle_io_info[LOWPRI_MAX_NUM_DEV];
235
7ddcb079 236
0a7de745 237int lowpri_throttle_enabled = 1;
39236c6e
A
238
239
39037602
A
240static void throttle_info_end_io_internal(struct _throttle_io_info_t *info, int throttle_level);
241static 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);
39236c6e 242static int throttle_get_thread_throttle_level(uthread_t ut);
d190cdc3 243static int throttle_get_thread_throttle_level_internal(uthread_t ut, int io_tier);
5ba3f43e 244void throttle_info_mount_reset_period(mount_t mp, int isssd);
7ddcb079 245
1c79356b
A
246/*
247 * Trivial lookup routine that always fails.
248 */
249int
2d21ac55 250spec_lookup(struct vnop_lookup_args *ap)
1c79356b 251{
1c79356b 252 *ap->a_vpp = NULL;
0a7de745 253 return ENOTDIR;
1c79356b
A
254}
255
91447636 256static void
1c79356b
A
257set_blocksize(struct vnode *vp, dev_t dev)
258{
0a7de745
A
259 int (*size)(dev_t);
260 int rsize;
1c79356b 261
0a7de745
A
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;
266 } else {
267 vp->v_specsize = rsize;
268 }
269 } else {
270 vp->v_specsize = DEV_BSIZE;
271 }
1c79356b
A
272}
273
274void
275set_fsblocksize(struct vnode *vp)
276{
1c79356b
A
277 if (vp->v_type == VBLK) {
278 dev_t dev = (dev_t)vp->v_rdev;
279 int maj = major(dev);
280
0a7de745 281 if ((u_int)maj >= (u_int)nblkdev) {
1c79356b 282 return;
0a7de745 283 }
1c79356b 284
91447636 285 vnode_lock(vp);
1c79356b 286 set_blocksize(vp, dev);
91447636 287 vnode_unlock(vp);
1c79356b 288 }
1c79356b
A
289}
290
291
292/*
293 * Open a special file.
294 */
91447636 295int
2d21ac55 296spec_open(struct vnop_open_args *ap)
1c79356b 297{
91447636
A
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;
1c79356b
A
301 dev_t bdev, dev = (dev_t)vp->v_rdev;
302 int maj = major(dev);
303 int error;
304
305 /*
306 * Don't allow open if fs is mounted -nodev.
307 */
0a7de745
A
308 if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_NODEV)) {
309 return ENXIO;
310 }
1c79356b
A
311
312 switch (vp->v_type) {
1c79356b 313 case VCHR:
0a7de745
A
314 if ((u_int)maj >= (u_int)nchrdev) {
315 return ENXIO;
316 }
91447636 317 if (cred != FSCRED && (ap->a_mode & FWRITE)) {
cb323159 318#if 0
1c79356b
A
319 /*
320 * When running in very secure mode, do not allow
321 * opens for writing of any disk character devices.
322 */
0a7de745
A
323 if (securelevel >= 2 && isdisk(dev, VCHR)) {
324 return EPERM;
325 }
cb323159 326#endif
fe8ab488
A
327
328 /* Never allow writing to /dev/mem or /dev/kmem */
0a7de745
A
329 if (iskmemdev(dev)) {
330 return EPERM;
331 }
1c79356b 332 /*
fe8ab488
A
333 * When running in secure mode, do not allow opens for
334 * writing of character devices whose corresponding block
335 * devices are currently mounted.
1c79356b
A
336 */
337 if (securelevel >= 1) {
0a7de745
A
338 if ((bdev = chrtoblk(dev)) != NODEV && check_mountedon(bdev, VBLK, &error)) {
339 return error;
340 }
1c79356b
A
341 }
342 }
316670eb 343
6d2010ae 344 devsw_lock(dev, S_IFCHR);
1c79356b 345 error = (*cdevsw[maj].d_open)(dev, ap->a_mode, S_IFCHR, p);
6d2010ae
A
346
347 if (error == 0) {
348 vp->v_specinfo->si_opencount++;
349 }
350
351 devsw_unlock(dev, S_IFCHR);
7ddcb079 352
39236c6e 353 if (error == 0 && cdevsw[maj].d_type == D_DISK && !vp->v_un.vu_specinfo->si_initted) {
0a7de745 354 int isssd = 0;
7ddcb079
A
355 uint64_t throttle_mask = 0;
356 uint32_t devbsdunit = 0;
357
358 if (VNOP_IOCTL(vp, DKIOCGETTHROTTLEMASK, (caddr_t)&throttle_mask, 0, NULL) == 0) {
316670eb
A
359 if (throttle_mask != 0 &&
360 VNOP_IOCTL(vp, DKIOCISSOLIDSTATE, (caddr_t)&isssd, 0, ap->a_context) == 0) {
7ddcb079
A
361 /*
362 * as a reasonable approximation, only use the lowest bit of the mask
363 * to generate a disk unit number
364 */
365 devbsdunit = num_trailing_0(throttle_mask);
366
367 vnode_lock(vp);
0a7de745 368
7ddcb079
A
369 vp->v_un.vu_specinfo->si_isssd = isssd;
370 vp->v_un.vu_specinfo->si_devbsdunit = devbsdunit;
371 vp->v_un.vu_specinfo->si_throttle_mask = throttle_mask;
372 vp->v_un.vu_specinfo->si_throttleable = 1;
373 vp->v_un.vu_specinfo->si_initted = 1;
374
375 vnode_unlock(vp);
376 }
377 }
378 if (vp->v_un.vu_specinfo->si_initted == 0) {
379 vnode_lock(vp);
380 vp->v_un.vu_specinfo->si_initted = 1;
381 vnode_unlock(vp);
382 }
383 }
0a7de745 384 return error;
1c79356b
A
385
386 case VBLK:
0a7de745
A
387 if ((u_int)maj >= (u_int)nblkdev) {
388 return ENXIO;
389 }
1c79356b
A
390 /*
391 * When running in very secure mode, do not allow
392 * opens for writing of any disk block devices.
393 */
91447636 394 if (securelevel >= 2 && cred != FSCRED &&
0a7de745
A
395 (ap->a_mode & FWRITE) && bdevsw[maj].d_type == D_DISK) {
396 return EPERM;
397 }
1c79356b
A
398 /*
399 * Do not allow opens of block devices that are
400 * currently mounted.
401 */
0a7de745
A
402 if ((error = vfs_mountedon(vp))) {
403 return error;
404 }
6d2010ae
A
405
406 devsw_lock(dev, S_IFBLK);
1c79356b 407 error = (*bdevsw[maj].d_open)(dev, ap->a_mode, S_IFBLK, p);
6d2010ae
A
408 if (!error) {
409 vp->v_specinfo->si_opencount++;
410 }
411 devsw_unlock(dev, S_IFBLK);
412
1c79356b 413 if (!error) {
0a7de745
A
414 u_int64_t blkcnt;
415 u_int32_t blksize;
91447636
A
416 int setsize = 0;
417 u_int32_t size512 = 512;
418
419
0a7de745 420 if (!VNOP_IOCTL(vp, DKIOCGETBLOCKSIZE, (caddr_t)&blksize, 0, ap->a_context)) {
91447636 421 /* Switch to 512 byte sectors (temporarily) */
55e303ae 422
91447636 423 if (!VNOP_IOCTL(vp, DKIOCSETBLOCKSIZE, (caddr_t)&size512, FWRITE, ap->a_context)) {
0a7de745
A
424 /* Get the number of 512 byte physical blocks. */
425 if (!VNOP_IOCTL(vp, DKIOCGETBLOCKCOUNT, (caddr_t)&blkcnt, 0, ap->a_context)) {
91447636 426 setsize = 1;
0a7de745 427 }
91447636
A
428 }
429 /* If it doesn't set back, we can't recover */
0a7de745
A
430 if (VNOP_IOCTL(vp, DKIOCSETBLOCKSIZE, (caddr_t)&blksize, FWRITE, ap->a_context)) {
431 error = ENXIO;
432 }
433 }
91447636
A
434
435
436 vnode_lock(vp);
0a7de745 437 set_blocksize(vp, dev);
55e303ae 438
0a7de745
A
439 /*
440 * Cache the size in bytes of the block device for later
441 * use by spec_write().
442 */
443 if (setsize) {
55e303ae 444 vp->v_specdevsize = blkcnt * (u_int64_t)size512;
0a7de745
A
445 } else {
446 vp->v_specdevsize = (u_int64_t)0; /* Default: Can't get */
447 }
91447636 448 vnode_unlock(vp);
1c79356b 449 }
0a7de745 450 return error;
91447636 451 default:
0a7de745 452 panic("spec_open type");
1c79356b 453 }
0a7de745 454 return 0;
1c79356b
A
455}
456
457/*
458 * Vnode op for read
459 */
91447636 460int
2d21ac55 461spec_read(struct vnop_read_args *ap)
1c79356b 462{
2d21ac55
A
463 struct vnode *vp = ap->a_vp;
464 struct uio *uio = ap->a_uio;
1c79356b 465 struct buf *bp;
91447636 466 daddr64_t bn, nextbn;
1c79356b 467 long bsize, bscale;
0a7de745 468 int devBlockSize = 0;
91447636 469 int n, on;
1c79356b
A
470 int error = 0;
471 dev_t dev;
472
473#if DIAGNOSTIC
0a7de745 474 if (uio->uio_rw != UIO_READ) {
1c79356b 475 panic("spec_read mode");
0a7de745
A
476 }
477 if (UIO_SEG_IS_USER_SPACE(uio->uio_segflg)) {
1c79356b 478 panic("spec_read proc");
0a7de745 479 }
1c79356b 480#endif
0a7de745
A
481 if (uio_resid(uio) == 0) {
482 return 0;
483 }
1c79356b
A
484
485 switch (vp->v_type) {
1c79356b 486 case VCHR:
0a7de745
A
487 {
488 struct _throttle_io_info_t *throttle_info = NULL;
489 int thread_throttle_level;
cb323159
A
490 uint64_t blkno = 0;
491 uint32_t iolen = 0;
492 int ddisk = 0;
493 int ktrace_code = DKIO_READ;
494 devBlockSize = vp->v_specsize;
495 uintptr_t our_id;
496
497 if (cdevsw[major(vp->v_rdev)].d_type == D_DISK) {
498 ddisk = 1;
499 }
500
501 if (ddisk && vp->v_un.vu_specinfo->si_throttleable) {
7ddcb079 502 throttle_info = &_throttle_io_info[vp->v_un.vu_specinfo->si_devbsdunit];
0a7de745
A
503 thread_throttle_level = throttle_info_update_internal(throttle_info, NULL, 0, vp->v_un.vu_specinfo->si_isssd, TRUE, NULL);
504 }
cb323159
A
505
506 if (kdebug_enable && ddisk) {
507 if (devBlockSize == 0) {
508 devBlockSize = 512; // default sector size
509 }
510
511 if (uio_offset(uio) && devBlockSize) {
512 blkno = ((uint64_t) uio_offset(uio) / ((uint64_t)devBlockSize));
513 }
514 iolen = (int) uio_resid(uio);
515 our_id = (uintptr_t)thread_tid(current_thread());
516 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON,
517 (FSDBG_CODE(DBG_DKRW, ktrace_code)) | DBG_FUNC_NONE, our_id,
518 vp->v_rdev, blkno, iolen, 0);
519 }
520
1c79356b 521 error = (*cdevsw[major(vp->v_rdev)].d_read)
0a7de745 522 (vp->v_rdev, uio, ap->a_ioflag);
7ddcb079 523
cb323159
A
524
525 if (kdebug_enable && ddisk) {
526 uint32_t residual = (uint32_t)uio_resid(uio);
527 ktrace_code |= DKIO_DONE;
528 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON,
529 (FSDBG_CODE(DBG_DKRW, ktrace_code)) | DBG_FUNC_NONE, our_id,
530 (uintptr_t)VM_KERNEL_ADDRPERM(vp), residual, error, 0);
531 }
532
0a7de745
A
533 if (throttle_info) {
534 throttle_info_end_io_internal(throttle_info, thread_throttle_level);
39037602 535 }
1c79356b 536
0a7de745
A
537 return error;
538 }
539
1c79356b 540 case VBLK:
0a7de745
A
541 if (uio->uio_offset < 0) {
542 return EINVAL;
543 }
1c79356b
A
544
545 dev = vp->v_rdev;
546
547 devBlockSize = vp->v_specsize;
548
0a7de745
A
549 if (devBlockSize > PAGE_SIZE) {
550 return EINVAL;
551 }
1c79356b 552
0a7de745 553 bscale = PAGE_SIZE / devBlockSize;
1c79356b
A
554 bsize = bscale * devBlockSize;
555
556 do {
557 on = uio->uio_offset % bsize;
558
0a7de745
A
559 bn = (daddr64_t)((uio->uio_offset / devBlockSize) & ~(bscale - 1));
560
91447636 561 if (vp->v_speclastr + bscale == bn) {
0a7de745 562 nextbn = bn + bscale;
91447636 563 error = buf_breadn(vp, bn, (int)bsize, &nextbn,
0a7de745
A
564 (int *)&bsize, 1, NOCRED, &bp);
565 } else {
566 error = buf_bread(vp, bn, (int)bsize, NOCRED, &bp);
567 }
91447636
A
568
569 vnode_lock(vp);
570 vp->v_speclastr = bn;
571 vnode_unlock(vp);
1c79356b 572
91447636 573 n = bsize - buf_resid(bp);
1c79356b 574 if ((on > n) || error) {
0a7de745
A
575 if (!error) {
576 error = EINVAL;
577 }
91447636 578 buf_brelse(bp);
0a7de745 579 return error;
1c79356b 580 }
91447636 581 n = min((unsigned)(n - on), uio_resid(uio));
1c79356b 582
6d2010ae 583 error = uiomove((char *)buf_dataptr(bp) + on, n, uio);
0a7de745 584 if (n + on == bsize) {
91447636 585 buf_markaged(bp);
0a7de745 586 }
91447636
A
587 buf_brelse(bp);
588 } while (error == 0 && uio_resid(uio) > 0 && n != 0);
0a7de745 589 return error;
1c79356b
A
590
591 default:
592 panic("spec_read type");
593 }
594 /* NOTREACHED */
91447636 595
0a7de745 596 return 0;
1c79356b
A
597}
598
599/*
600 * Vnode op for write
601 */
91447636 602int
2d21ac55 603spec_write(struct vnop_write_args *ap)
1c79356b 604{
2d21ac55
A
605 struct vnode *vp = ap->a_vp;
606 struct uio *uio = ap->a_uio;
1c79356b 607 struct buf *bp;
91447636 608 daddr64_t bn;
1c79356b 609 int bsize, blkmask, bscale;
2d21ac55 610 int io_sync;
0a7de745 611 int devBlockSize = 0;
2d21ac55 612 int n, on;
1c79356b
A
613 int error = 0;
614 dev_t dev;
615
616#if DIAGNOSTIC
0a7de745 617 if (uio->uio_rw != UIO_WRITE) {
1c79356b 618 panic("spec_write mode");
0a7de745
A
619 }
620 if (UIO_SEG_IS_USER_SPACE(uio->uio_segflg)) {
1c79356b 621 panic("spec_write proc");
0a7de745 622 }
1c79356b
A
623#endif
624
625 switch (vp->v_type) {
1c79356b 626 case VCHR:
0a7de745
A
627 {
628 struct _throttle_io_info_t *throttle_info = NULL;
629 int thread_throttle_level;
cb323159
A
630 dev = vp->v_rdev;
631 devBlockSize = vp->v_specsize;
632 uint32_t iolen = 0;
633 uint64_t blkno = 0;
634 int ddisk = 0;
635 int ktrace_code = 0; // write is implied; read must be OR'd in.
636 uintptr_t our_id;
637
638 if (cdevsw[major(dev)].d_type == D_DISK) {
639 ddisk = 1;
640 }
641
642 if (ddisk && vp->v_un.vu_specinfo->si_throttleable) {
7ddcb079
A
643 throttle_info = &_throttle_io_info[vp->v_un.vu_specinfo->si_devbsdunit];
644
0a7de745 645 thread_throttle_level = throttle_info_update_internal(throttle_info, NULL, 0, vp->v_un.vu_specinfo->si_isssd, TRUE, NULL);
7ddcb079 646
316670eb 647 microuptime(&throttle_info->throttle_last_write_timestamp);
0a7de745 648 }
cb323159
A
649
650 if (kdebug_enable && ddisk) {
651 if (devBlockSize == 0) {
652 devBlockSize = 512; // default sector size
653 }
654 if ((uio_offset(uio) != 0) && devBlockSize) {
655 blkno = ((uint64_t)uio_offset(uio)) / ((uint64_t)devBlockSize);
656 }
657 iolen = (int)uio_resid(uio);
658 our_id = (uintptr_t)thread_tid(current_thread());
659 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON,
660 (FSDBG_CODE(DBG_DKRW, ktrace_code)) | DBG_FUNC_NONE, our_id,
661 vp->v_rdev, blkno, iolen, 0);
662 }
1c79356b 663 error = (*cdevsw[major(vp->v_rdev)].d_write)
0a7de745 664 (vp->v_rdev, uio, ap->a_ioflag);
39037602 665
cb323159
A
666 if (kdebug_enable && ddisk) {
667 //emit the I/O completion
668 uint32_t residual = (uint32_t)uio_resid(uio);
669 ktrace_code |= DKIO_DONE;
670 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON,
671 (FSDBG_CODE(DBG_DKRW, ktrace_code)) | DBG_FUNC_NONE, our_id,
672 (uintptr_t)VM_KERNEL_ADDRPERM(vp), residual, error, 0);
673 }
674
0a7de745
A
675 if (throttle_info) {
676 throttle_info_end_io_internal(throttle_info, thread_throttle_level);
39037602 677 }
1c79356b 678
0a7de745
A
679 return error;
680 }
681
1c79356b 682 case VBLK:
0a7de745
A
683 if (uio_resid(uio) == 0) {
684 return 0;
685 }
686 if (uio->uio_offset < 0) {
687 return EINVAL;
688 }
1c79356b
A
689
690 io_sync = (ap->a_ioflag & IO_SYNC);
1c79356b
A
691
692 dev = (vp->v_rdev);
693
694 devBlockSize = vp->v_specsize;
0a7de745
A
695 if (devBlockSize > PAGE_SIZE) {
696 return EINVAL;
697 }
1c79356b 698
0a7de745 699 bscale = PAGE_SIZE / devBlockSize;
1c79356b
A
700 blkmask = bscale - 1;
701 bsize = bscale * devBlockSize;
0a7de745 702
1c79356b
A
703
704 do {
0a7de745 705 bn = (daddr64_t)((uio->uio_offset / devBlockSize) & ~blkmask);
1c79356b
A
706 on = uio->uio_offset % bsize;
707
91447636 708 n = min((unsigned)(bsize - on), uio_resid(uio));
1c79356b 709
55e303ae 710 /*
91447636 711 * Use buf_getblk() as an optimization IFF:
55e303ae
A
712 *
713 * 1) We are reading exactly a block on a block
714 * aligned boundary
715 * 2) We know the size of the device from spec_open
716 * 3) The read doesn't span the end of the device
717 *
91447636 718 * Otherwise, we fall back on buf_bread().
55e303ae
A
719 */
720 if (n == bsize &&
721 vp->v_specdevsize != (u_int64_t)0 &&
722 (uio->uio_offset + (u_int64_t)n) > vp->v_specdevsize) {
0a7de745
A
723 /* reduce the size of the read to what is there */
724 n = (uio->uio_offset + (u_int64_t)n) - vp->v_specdevsize;
55e303ae
A
725 }
726
0a7de745
A
727 if (n == bsize) {
728 bp = buf_getblk(vp, bn, bsize, 0, 0, BLK_WRITE);
729 } else {
730 error = (int)buf_bread(vp, bn, bsize, NOCRED, &bp);
731 }
1c79356b 732
55e303ae 733 /* Translate downstream error for upstream, if needed */
0a7de745 734 if (!error) {
91447636 735 error = (int)buf_error(bp);
0a7de745 736 }
1c79356b 737 if (error) {
91447636 738 buf_brelse(bp);
0a7de745 739 return error;
1c79356b 740 }
91447636 741 n = min(n, bsize - buf_resid(bp));
1c79356b 742
6d2010ae 743 error = uiomove((char *)buf_dataptr(bp) + on, n, uio);
91447636
A
744 if (error) {
745 buf_brelse(bp);
0a7de745 746 return error;
91447636
A
747 }
748 buf_markaged(bp);
1c79356b 749
0a7de745
A
750 if (io_sync) {
751 error = buf_bwrite(bp);
752 } else {
753 if ((n + on) == bsize) {
754 error = buf_bawrite(bp);
755 } else {
756 error = buf_bdwrite(bp);
757 }
1c79356b 758 }
91447636 759 } while (error == 0 && uio_resid(uio) > 0 && n != 0);
0a7de745 760 return error;
1c79356b
A
761
762 default:
763 panic("spec_write type");
764 }
765 /* NOTREACHED */
91447636 766
0a7de745 767 return 0;
1c79356b
A
768}
769
770/*
771 * Device ioctl operation.
772 */
91447636 773int
2d21ac55 774spec_ioctl(struct vnop_ioctl_args *ap)
1c79356b 775{
91447636 776 proc_t p = vfs_context_proc(ap->a_context);
1c79356b 777 dev_t dev = ap->a_vp->v_rdev;
0a7de745 778 int retval = 0;
b0d623f7
A
779
780 KERNEL_DEBUG_CONSTANT(FSDBG_CODE(DBG_IOCTL, 0) | DBG_FUNC_START,
0a7de745 781 dev, ap->a_command, ap->a_fflag, ap->a_vp->v_type, 0);
1c79356b
A
782
783 switch (ap->a_vp->v_type) {
1c79356b 784 case VCHR:
b0d623f7 785 retval = (*cdevsw[major(dev)].d_ioctl)(dev, ap->a_command, ap->a_data,
0a7de745 786 ap->a_fflag, p);
b0d623f7 787 break;
1c79356b
A
788
789 case VBLK:
316670eb 790 retval = (*bdevsw[major(dev)].d_ioctl)(dev, ap->a_command, ap->a_data, ap->a_fflag, p);
0a7de745 791 if (!retval && ap->a_command == DKIOCSETBLOCKSIZE) {
39037602 792 ap->a_vp->v_specsize = *(uint32_t *)ap->a_data;
0a7de745 793 }
b0d623f7 794 break;
1c79356b
A
795
796 default:
797 panic("spec_ioctl");
798 /* NOTREACHED */
799 }
b0d623f7 800 KERNEL_DEBUG_CONSTANT(FSDBG_CODE(DBG_IOCTL, 0) | DBG_FUNC_END,
0a7de745 801 dev, ap->a_command, ap->a_fflag, retval, 0);
b0d623f7 802
0a7de745 803 return retval;
1c79356b
A
804}
805
91447636 806int
2d21ac55 807spec_select(struct vnop_select_args *ap)
1c79356b 808{
91447636 809 proc_t p = vfs_context_proc(ap->a_context);
2d21ac55 810 dev_t dev;
1c79356b
A
811
812 switch (ap->a_vp->v_type) {
1c79356b 813 default:
0a7de745 814 return 1; /* XXX */
1c79356b
A
815
816 case VCHR:
817 dev = ap->a_vp->v_rdev;
91447636 818 return (*cdevsw[major(dev)].d_select)(dev, ap->a_which, ap->a_wql, p);
1c79356b
A
819 }
820}
91447636 821
cb323159 822static int filt_specattach(struct knote *kn, struct kevent_qos_s *kev);
6d2010ae 823
b0d623f7 824int
cb323159 825spec_kqfilter(vnode_t vp, struct knote *kn, struct kevent_qos_s *kev)
b0d623f7
A
826{
827 dev_t dev;
3e170ce0
A
828
829 assert(vnode_ischr(vp));
b0d623f7 830
b0d623f7
A
831 dev = vnode_specrdev(vp);
832
39236c6e 833#if NETWORKING
39037602
A
834 /*
835 * Try a bpf device, as defined in bsd/net/bpf.c
836 * If it doesn't error out the attach, then it
837 * claimed it. Otherwise, fall through and try
5ba3f43e 838 * other attaches.
39037602
A
839 */
840 int32_t tmp_flags = kn->kn_flags;
cb323159 841 int64_t tmp_sdata = kn->kn_sdata;
39037602
A
842 int res;
843
844 res = bpfkqfilter(dev, kn);
845 if ((kn->kn_flags & EV_ERROR) == 0) {
846 return res;
b0d623f7 847 }
39037602 848 kn->kn_flags = tmp_flags;
cb323159 849 kn->kn_sdata = tmp_sdata;
3e170ce0 850#endif
b0d623f7 851
5ba3f43e
A
852 if (major(dev) > nchrdev) {
853 knote_set_error(kn, ENXIO);
854 return 0;
855 }
856
857 kn->kn_vnode_kqok = !!(cdevsw_flags[major(dev)] & CDEVSW_SELECT_KQUEUE);
858 kn->kn_vnode_use_ofst = !!(cdevsw_flags[major(dev)] & CDEVSW_USE_OFFSET);
859
860 if (cdevsw_flags[major(dev)] & CDEVSW_IS_PTS) {
861 kn->kn_filtid = EVFILTID_PTSD;
862 return ptsd_kqfilter(dev, kn);
5c9f4661
A
863 } else if (cdevsw_flags[major(dev)] & CDEVSW_IS_PTC) {
864 kn->kn_filtid = EVFILTID_PTMX;
865 return ptmx_kqfilter(dev, kn);
866 } else if (cdevsw[major(dev)].d_type == D_TTY && kn->kn_vnode_kqok) {
5ba3f43e
A
867 /*
868 * TTYs from drivers that use struct ttys use their own filter
869 * routines. The PTC driver doesn't use the tty for character
870 * counts, so it must go through the select fallback.
871 */
872 kn->kn_filtid = EVFILTID_TTY;
873 return knote_fops(kn)->f_attach(kn, kev);
874 }
875
39037602 876 /* Try to attach to other char special devices */
5ba3f43e 877 return filt_specattach(kn, kev);
b0d623f7
A
878}
879
1c79356b
A
880/*
881 * Synch buffers associated with a block device
882 */
1c79356b 883int
91447636 884spec_fsync_internal(vnode_t vp, int waitfor, __unused vfs_context_t context)
1c79356b 885{
0a7de745
A
886 if (vp->v_type == VCHR) {
887 return 0;
888 }
1c79356b
A
889 /*
890 * Flush all dirty buffers associated with a block device.
891 */
b0d623f7 892 buf_flushdirtyblks(vp, (waitfor == MNT_WAIT || waitfor == MNT_DWAIT), 0, "spec_fsync");
91447636 893
0a7de745 894 return 0;
1c79356b
A
895}
896
91447636 897int
2d21ac55 898spec_fsync(struct vnop_fsync_args *ap)
91447636
A
899{
900 return spec_fsync_internal(ap->a_vp, ap->a_waitfor, ap->a_context);
901}
902
316670eb 903
1c79356b
A
904/*
905 * Just call the device strategy routine
906 */
316670eb
A
907void throttle_init(void);
908
2d21ac55 909
0a7de745
A
910#if 0
911#define DEBUG_ALLOC_THROTTLE_INFO(format, debug_info, args...) \
912 do { \
913 if ((debug_info)->alloc) \
914 printf("%s: "format, __FUNCTION__, ## args); \
b0d623f7
A
915 } while(0)
916
0a7de745 917#else
b0d623f7
A
918#define DEBUG_ALLOC_THROTTLE_INFO(format, debug_info, args...)
919#endif
920
39236c6e
A
921
922SYSCTL_INT(_debug, OID_AUTO, lowpri_throttle_tier1_window_msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &throttle_windows_msecs[THROTTLE_LEVEL_TIER1], 0, "");
923SYSCTL_INT(_debug, OID_AUTO, lowpri_throttle_tier2_window_msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &throttle_windows_msecs[THROTTLE_LEVEL_TIER2], 0, "");
924SYSCTL_INT(_debug, OID_AUTO, lowpri_throttle_tier3_window_msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &throttle_windows_msecs[THROTTLE_LEVEL_TIER3], 0, "");
925
926SYSCTL_INT(_debug, OID_AUTO, lowpri_throttle_tier1_io_period_msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &throttle_io_period_msecs[THROTTLE_LEVEL_TIER1], 0, "");
927SYSCTL_INT(_debug, OID_AUTO, lowpri_throttle_tier2_io_period_msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &throttle_io_period_msecs[THROTTLE_LEVEL_TIER2], 0, "");
928SYSCTL_INT(_debug, OID_AUTO, lowpri_throttle_tier3_io_period_msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &throttle_io_period_msecs[THROTTLE_LEVEL_TIER3], 0, "");
929
930SYSCTL_INT(_debug, OID_AUTO, lowpri_throttle_tier1_io_period_ssd_msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &throttle_io_period_ssd_msecs[THROTTLE_LEVEL_TIER1], 0, "");
931SYSCTL_INT(_debug, OID_AUTO, lowpri_throttle_tier2_io_period_ssd_msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &throttle_io_period_ssd_msecs[THROTTLE_LEVEL_TIER2], 0, "");
932SYSCTL_INT(_debug, OID_AUTO, lowpri_throttle_tier3_io_period_ssd_msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &throttle_io_period_ssd_msecs[THROTTLE_LEVEL_TIER3], 0, "");
933
934SYSCTL_INT(_debug, OID_AUTO, lowpri_throttle_enabled, CTLFLAG_RW | CTLFLAG_LOCKED, &lowpri_throttle_enabled, 0, "");
935
316670eb 936
39037602
A
937static lck_grp_t *throttle_lock_grp;
938static lck_attr_t *throttle_lock_attr;
939static lck_grp_attr_t *throttle_lock_grp_attr;
316670eb 940
6d2010ae
A
941
942/*
943 * throttled I/O helper function
944 * convert the index of the lowest set bit to a device index
945 */
946int
947num_trailing_0(uint64_t n)
948{
949 /*
950 * since in most cases the number of trailing 0s is very small,
316670eb 951 * we simply counting sequentially from the lowest bit
6d2010ae 952 */
0a7de745 953 if (n == 0) {
6d2010ae 954 return sizeof(n) * 8;
0a7de745 955 }
6d2010ae
A
956 int count = 0;
957 while (!ISSET(n, 1)) {
958 n >>= 1;
959 ++count;
960 }
961 return count;
962}
2d21ac55 963
316670eb 964
b0d623f7
A
965/*
966 * Release the reference and if the item was allocated and this is the last
967 * reference then free it.
968 *
969 * This routine always returns the old value.
970 */
971static int
972throttle_info_rel(struct _throttle_io_info_t *info)
973{
316670eb 974 SInt32 oldValue = OSDecrementAtomic(&info->throttle_refcnt);
b0d623f7 975
0a7de745
A
976 DEBUG_ALLOC_THROTTLE_INFO("refcnt = %d info = %p\n",
977 info, (int)(oldValue - 1), info );
b0d623f7
A
978
979 /* The reference count just went negative, very bad */
0a7de745 980 if (oldValue == 0) {
b0d623f7 981 panic("throttle info ref cnt went negative!");
0a7de745 982 }
b0d623f7 983
0a7de745
A
984 /*
985 * Once reference count is zero, no one else should be able to take a
986 * reference
b0d623f7 987 */
316670eb
A
988 if ((info->throttle_refcnt == 0) && (info->throttle_alloc)) {
989 DEBUG_ALLOC_THROTTLE_INFO("Freeing info = %p\n", info);
0a7de745 990
39037602 991 lck_mtx_destroy(&info->throttle_lock, throttle_lock_grp);
0a7de745 992 FREE(info, M_TEMP);
b0d623f7
A
993 }
994 return oldValue;
995}
996
316670eb 997
b0d623f7
A
998/*
999 * Just take a reference on the throttle info structure.
1000 *
1001 * This routine always returns the old value.
1002 */
1003static SInt32
1004throttle_info_ref(struct _throttle_io_info_t *info)
1005{
316670eb 1006 SInt32 oldValue = OSIncrementAtomic(&info->throttle_refcnt);
b0d623f7 1007
0a7de745
A
1008 DEBUG_ALLOC_THROTTLE_INFO("refcnt = %d info = %p\n",
1009 info, (int)(oldValue - 1), info );
b0d623f7 1010 /* Allocated items should never have a reference of zero */
0a7de745 1011 if (info->throttle_alloc && (oldValue == 0)) {
b0d623f7 1012 panic("Taking a reference without calling create throttle info!\n");
0a7de745 1013 }
b0d623f7
A
1014
1015 return oldValue;
1016}
1017
316670eb
A
1018/*
1019 * on entry the throttle_lock is held...
1020 * this function is responsible for taking
1021 * and dropping the reference on the info
1022 * structure which will keep it from going
1023 * away while the timer is running if it
1024 * happens to have been dynamically allocated by
1025 * a network fileystem kext which is now trying
1026 * to free it
1027 */
1028static uint32_t
39236c6e 1029throttle_timer_start(struct _throttle_io_info_t *info, boolean_t update_io_count, int wakelevel)
0a7de745 1030{
316670eb 1031 struct timeval elapsed;
39236c6e
A
1032 struct timeval now;
1033 struct timeval period;
0a7de745
A
1034 uint64_t elapsed_msecs;
1035 int throttle_level;
1036 int level;
1037 int msecs;
1038 boolean_t throttled = FALSE;
1039 boolean_t need_timer = FALSE;
39236c6e
A
1040
1041 microuptime(&now);
316670eb
A
1042
1043 if (update_io_count == TRUE) {
1044 info->throttle_io_count_begin = info->throttle_io_count;
1045 info->throttle_io_period_num++;
1046
0a7de745 1047 while (wakelevel >= THROTTLE_LEVEL_THROTTLED) {
39236c6e 1048 info->throttle_start_IO_period_timestamp[wakelevel--] = now;
0a7de745 1049 }
39236c6e
A
1050
1051 info->throttle_min_timer_deadline = now;
1052
1053 msecs = info->throttle_io_periods[THROTTLE_LEVEL_THROTTLED];
1054 period.tv_sec = msecs / 1000;
1055 period.tv_usec = (msecs % 1000) * 1000;
1056
1057 timevaladd(&info->throttle_min_timer_deadline, &period);
316670eb
A
1058 }
1059 for (throttle_level = THROTTLE_LEVEL_START; throttle_level < THROTTLE_LEVEL_END; throttle_level++) {
39236c6e
A
1060 elapsed = now;
1061 timevalsub(&elapsed, &info->throttle_window_start_timestamp[throttle_level]);
db609669 1062 elapsed_msecs = (uint64_t)elapsed.tv_sec * (uint64_t)1000 + (elapsed.tv_usec / 1000);
316670eb 1063
39236c6e 1064 for (level = throttle_level + 1; level <= THROTTLE_LEVEL_END; level++) {
39236c6e 1065 if (!TAILQ_EMPTY(&info->throttle_uthlist[level])) {
d190cdc3 1066 if (elapsed_msecs < (uint64_t)throttle_windows_msecs[level] || info->throttle_inflight_count[throttle_level]) {
39236c6e
A
1067 /*
1068 * we had an I/O occur at a higher priority tier within
1069 * this tier's throttle window
1070 */
1071 throttled = TRUE;
1072 }
1073 /*
1074 * we assume that the windows are the same or longer
1075 * as we drop through the throttling tiers... thus
1076 * we can stop looking once we run into a tier with
1077 * threads to schedule regardless of whether it's
1078 * still in its throttling window or not
1079 */
1080 break;
1081 }
316670eb 1082 }
0a7de745 1083 if (throttled == TRUE) {
39236c6e 1084 break;
0a7de745 1085 }
316670eb 1086 }
39236c6e 1087 if (throttled == TRUE) {
0a7de745 1088 uint64_t deadline = 0;
39236c6e
A
1089 struct timeval target;
1090 struct timeval min_target;
316670eb 1091
0a7de745 1092 /*
39236c6e
A
1093 * we've got at least one tier still in a throttled window
1094 * so we need a timer running... compute the next deadline
1095 * and schedule it
316670eb 1096 */
0a7de745
A
1097 for (level = throttle_level + 1; level <= THROTTLE_LEVEL_END; level++) {
1098 if (TAILQ_EMPTY(&info->throttle_uthlist[level])) {
39236c6e 1099 continue;
0a7de745 1100 }
39236c6e
A
1101
1102 target = info->throttle_start_IO_period_timestamp[level];
1103
1104 msecs = info->throttle_io_periods[level];
1105 period.tv_sec = msecs / 1000;
1106 period.tv_usec = (msecs % 1000) * 1000;
1107
1108 timevaladd(&target, &period);
0a7de745 1109
39236c6e
A
1110 if (need_timer == FALSE || timevalcmp(&target, &min_target, <)) {
1111 min_target = target;
1112 need_timer = TRUE;
1113 }
1114 }
1115 if (timevalcmp(&info->throttle_min_timer_deadline, &now, >)) {
0a7de745
A
1116 if (timevalcmp(&info->throttle_min_timer_deadline, &min_target, >)) {
1117 min_target = info->throttle_min_timer_deadline;
1118 }
39236c6e
A
1119 }
1120
1121 if (info->throttle_timer_active) {
1122 if (thread_call_cancel(info->throttle_timer_call) == FALSE) {
1123 /*
1124 * couldn't kill the timer because it's already
1125 * been dispatched, so don't try to start a new
1126 * one... once we drop the lock, the timer will
1127 * proceed and eventually re-run this function
1128 */
1129 need_timer = FALSE;
0a7de745 1130 } else {
39236c6e 1131 info->throttle_timer_active = 0;
0a7de745 1132 }
39236c6e
A
1133 }
1134 if (need_timer == TRUE) {
1135 /*
1136 * This is defined as an int (32-bit) rather than a 64-bit
1137 * value because it would need a really big period in the
1138 * order of ~500 days to overflow this. So, we let this be
1139 * 32-bit which allows us to use the clock_interval_to_deadline()
1140 * routine.
1141 */
0a7de745 1142 int target_msecs;
316670eb 1143
39236c6e
A
1144 if (info->throttle_timer_ref == 0) {
1145 /*
1146 * take a reference for the timer
1147 */
1148 throttle_info_ref(info);
316670eb 1149
39236c6e
A
1150 info->throttle_timer_ref = 1;
1151 }
1152 elapsed = min_target;
1153 timevalsub(&elapsed, &now);
1154 target_msecs = elapsed.tv_sec * 1000 + elapsed.tv_usec / 1000;
1155
1156 if (target_msecs <= 0) {
1157 /*
1158 * we may have computed a deadline slightly in the past
1159 * due to various factors... if so, just set the timer
1160 * to go off in the near future (we don't need to be precise)
1161 */
1162 target_msecs = 1;
1163 }
1164 clock_interval_to_deadline(target_msecs, 1000000, &deadline);
1165
1166 thread_call_enter_delayed(info->throttle_timer_call, deadline);
1167 info->throttle_timer_active = 1;
1168 }
1169 }
0a7de745 1170 return throttle_level;
316670eb
A
1171}
1172
1173
1174static void
1175throttle_timer(struct _throttle_io_info_t *info)
1176{
1177 uthread_t ut, utlist;
0a7de745
A
1178 struct timeval elapsed;
1179 struct timeval now;
1180 uint64_t elapsed_msecs;
1181 int throttle_level;
1182 int level;
1183 int wake_level;
1184 caddr_t wake_address = NULL;
1185 boolean_t update_io_count = FALSE;
1186 boolean_t need_wakeup = FALSE;
1187 boolean_t need_release = FALSE;
316670eb 1188
39236c6e 1189 ut = NULL;
0a7de745 1190 lck_mtx_lock(&info->throttle_lock);
39236c6e
A
1191
1192 info->throttle_timer_active = 0;
1193 microuptime(&now);
1194
1195 elapsed = now;
1196 timevalsub(&elapsed, &info->throttle_start_IO_period_timestamp[THROTTLE_LEVEL_THROTTLED]);
db609669 1197 elapsed_msecs = (uint64_t)elapsed.tv_sec * (uint64_t)1000 + (elapsed.tv_usec / 1000);
316670eb 1198
39236c6e 1199 if (elapsed_msecs >= (uint64_t)info->throttle_io_periods[THROTTLE_LEVEL_THROTTLED]) {
39236c6e
A
1200 wake_level = info->throttle_next_wake_level;
1201
1202 for (level = THROTTLE_LEVEL_START; level < THROTTLE_LEVEL_END; level++) {
39236c6e
A
1203 elapsed = now;
1204 timevalsub(&elapsed, &info->throttle_start_IO_period_timestamp[wake_level]);
1205 elapsed_msecs = (uint64_t)elapsed.tv_sec * (uint64_t)1000 + (elapsed.tv_usec / 1000);
1206
1207 if (elapsed_msecs >= (uint64_t)info->throttle_io_periods[wake_level] && !TAILQ_EMPTY(&info->throttle_uthlist[wake_level])) {
1208 /*
1209 * we're closing out the current IO period...
1210 * if we have a waiting thread, wake it up
1211 * after we have reset the I/O window info
1212 */
1213 need_wakeup = TRUE;
1214 update_io_count = TRUE;
1215
1216 info->throttle_next_wake_level = wake_level - 1;
1217
0a7de745 1218 if (info->throttle_next_wake_level == THROTTLE_LEVEL_START) {
39236c6e 1219 info->throttle_next_wake_level = THROTTLE_LEVEL_END;
0a7de745 1220 }
39236c6e
A
1221
1222 break;
1223 }
1224 wake_level--;
1225
0a7de745 1226 if (wake_level == THROTTLE_LEVEL_START) {
39236c6e 1227 wake_level = THROTTLE_LEVEL_END;
0a7de745 1228 }
39236c6e 1229 }
316670eb 1230 }
39236c6e
A
1231 if (need_wakeup == TRUE) {
1232 if (!TAILQ_EMPTY(&info->throttle_uthlist[wake_level])) {
39236c6e
A
1233 ut = (uthread_t)TAILQ_FIRST(&info->throttle_uthlist[wake_level]);
1234 TAILQ_REMOVE(&info->throttle_uthlist[wake_level], ut, uu_throttlelist);
1235 ut->uu_on_throttlelist = THROTTLE_LEVEL_NONE;
d9a64523 1236 ut->uu_is_throttled = false;
39236c6e
A
1237
1238 wake_address = (caddr_t)&ut->uu_on_throttlelist;
1239 }
0a7de745 1240 } else {
39236c6e 1241 wake_level = THROTTLE_LEVEL_START;
0a7de745 1242 }
39236c6e 1243
0a7de745 1244 throttle_level = throttle_timer_start(info, update_io_count, wake_level);
39236c6e 1245
0a7de745 1246 if (wake_address != NULL) {
39236c6e 1247 wakeup(wake_address);
0a7de745 1248 }
39236c6e
A
1249
1250 for (level = THROTTLE_LEVEL_THROTTLED; level <= throttle_level; level++) {
39236c6e 1251 TAILQ_FOREACH_SAFE(ut, &info->throttle_uthlist[level], uu_throttlelist, utlist) {
39236c6e
A
1252 TAILQ_REMOVE(&info->throttle_uthlist[level], ut, uu_throttlelist);
1253 ut->uu_on_throttlelist = THROTTLE_LEVEL_NONE;
d9a64523 1254 ut->uu_is_throttled = false;
316670eb
A
1255
1256 wakeup(&ut->uu_on_throttlelist);
1257 }
1258 }
39236c6e
A
1259 if (info->throttle_timer_active == 0 && info->throttle_timer_ref) {
1260 info->throttle_timer_ref = 0;
1261 need_release = TRUE;
316670eb 1262 }
0a7de745 1263 lck_mtx_unlock(&info->throttle_lock);
316670eb 1264
0a7de745 1265 if (need_release == TRUE) {
316670eb 1266 throttle_info_rel(info);
0a7de745 1267 }
316670eb
A
1268}
1269
1270
39236c6e
A
1271static int
1272throttle_add_to_list(struct _throttle_io_info_t *info, uthread_t ut, int mylevel, boolean_t insert_tail)
1273{
1274 boolean_t start_timer = FALSE;
1275 int level = THROTTLE_LEVEL_START;
1276
1277 if (TAILQ_EMPTY(&info->throttle_uthlist[mylevel])) {
1278 info->throttle_start_IO_period_timestamp[mylevel] = info->throttle_last_IO_timestamp[mylevel];
1279 start_timer = TRUE;
1280 }
1281
0a7de745 1282 if (insert_tail == TRUE) {
39236c6e 1283 TAILQ_INSERT_TAIL(&info->throttle_uthlist[mylevel], ut, uu_throttlelist);
0a7de745 1284 } else {
39236c6e 1285 TAILQ_INSERT_HEAD(&info->throttle_uthlist[mylevel], ut, uu_throttlelist);
0a7de745 1286 }
39236c6e
A
1287
1288 ut->uu_on_throttlelist = mylevel;
1289
1290 if (start_timer == TRUE) {
1291 /* we may need to start or rearm the timer */
1292 level = throttle_timer_start(info, FALSE, THROTTLE_LEVEL_START);
1293
1294 if (level == THROTTLE_LEVEL_END) {
1295 if (ut->uu_on_throttlelist >= THROTTLE_LEVEL_THROTTLED) {
1296 TAILQ_REMOVE(&info->throttle_uthlist[ut->uu_on_throttlelist], ut, uu_throttlelist);
1297
1298 ut->uu_on_throttlelist = THROTTLE_LEVEL_NONE;
1299 }
1300 }
1301 }
0a7de745 1302 return level;
39236c6e
A
1303}
1304
1305static void
1306throttle_init_throttle_window(void)
1307{
1308 int throttle_window_size;
1309
1310 /*
1311 * The hierarchy of throttle window values is as follows:
1312 * - Global defaults
1313 * - Device tree properties
1314 * - Boot-args
1315 * All values are specified in msecs.
1316 */
1317
1318 /* Override global values with device-tree properties */
0a7de745 1319 if (PE_get_default("kern.io_throttle_window_tier1", &throttle_window_size, sizeof(throttle_window_size))) {
39236c6e 1320 throttle_windows_msecs[THROTTLE_LEVEL_TIER1] = throttle_window_size;
0a7de745 1321 }
39236c6e 1322
0a7de745 1323 if (PE_get_default("kern.io_throttle_window_tier2", &throttle_window_size, sizeof(throttle_window_size))) {
39236c6e 1324 throttle_windows_msecs[THROTTLE_LEVEL_TIER2] = throttle_window_size;
0a7de745 1325 }
39236c6e 1326
0a7de745 1327 if (PE_get_default("kern.io_throttle_window_tier3", &throttle_window_size, sizeof(throttle_window_size))) {
39236c6e 1328 throttle_windows_msecs[THROTTLE_LEVEL_TIER3] = throttle_window_size;
0a7de745
A
1329 }
1330
39236c6e 1331 /* Override with boot-args */
0a7de745 1332 if (PE_parse_boot_argn("io_throttle_window_tier1", &throttle_window_size, sizeof(throttle_window_size))) {
39236c6e 1333 throttle_windows_msecs[THROTTLE_LEVEL_TIER1] = throttle_window_size;
0a7de745 1334 }
39236c6e 1335
0a7de745 1336 if (PE_parse_boot_argn("io_throttle_window_tier2", &throttle_window_size, sizeof(throttle_window_size))) {
39236c6e 1337 throttle_windows_msecs[THROTTLE_LEVEL_TIER2] = throttle_window_size;
0a7de745
A
1338 }
1339
1340 if (PE_parse_boot_argn("io_throttle_window_tier3", &throttle_window_size, sizeof(throttle_window_size))) {
39236c6e 1341 throttle_windows_msecs[THROTTLE_LEVEL_TIER3] = throttle_window_size;
0a7de745 1342 }
39236c6e
A
1343}
1344
1345static void
1346throttle_init_throttle_period(struct _throttle_io_info_t *info, boolean_t isssd)
1347{
1348 int throttle_period_size;
1349
1350 /*
1351 * The hierarchy of throttle period values is as follows:
1352 * - Global defaults
1353 * - Device tree properties
1354 * - Boot-args
1355 * All values are specified in msecs.
1356 */
1357
1358 /* Assign global defaults */
0a7de745 1359 if ((isssd == TRUE) && (info->throttle_is_fusion_with_priority == 0)) {
39236c6e 1360 info->throttle_io_periods = &throttle_io_period_ssd_msecs[0];
0a7de745 1361 } else {
39236c6e 1362 info->throttle_io_periods = &throttle_io_period_msecs[0];
0a7de745 1363 }
39236c6e
A
1364
1365 /* Override global values with device-tree properties */
0a7de745 1366 if (PE_get_default("kern.io_throttle_period_tier1", &throttle_period_size, sizeof(throttle_period_size))) {
39236c6e 1367 info->throttle_io_periods[THROTTLE_LEVEL_TIER1] = throttle_period_size;
0a7de745
A
1368 }
1369
1370 if (PE_get_default("kern.io_throttle_period_tier2", &throttle_period_size, sizeof(throttle_period_size))) {
39236c6e 1371 info->throttle_io_periods[THROTTLE_LEVEL_TIER2] = throttle_period_size;
0a7de745 1372 }
39236c6e 1373
0a7de745 1374 if (PE_get_default("kern.io_throttle_period_tier3", &throttle_period_size, sizeof(throttle_period_size))) {
39236c6e 1375 info->throttle_io_periods[THROTTLE_LEVEL_TIER3] = throttle_period_size;
0a7de745
A
1376 }
1377
39236c6e 1378 /* Override with boot-args */
0a7de745 1379 if (PE_parse_boot_argn("io_throttle_period_tier1", &throttle_period_size, sizeof(throttle_period_size))) {
39236c6e 1380 info->throttle_io_periods[THROTTLE_LEVEL_TIER1] = throttle_period_size;
0a7de745
A
1381 }
1382
1383 if (PE_parse_boot_argn("io_throttle_period_tier2", &throttle_period_size, sizeof(throttle_period_size))) {
39236c6e 1384 info->throttle_io_periods[THROTTLE_LEVEL_TIER2] = throttle_period_size;
0a7de745 1385 }
39236c6e 1386
0a7de745 1387 if (PE_parse_boot_argn("io_throttle_period_tier3", &throttle_period_size, sizeof(throttle_period_size))) {
39236c6e 1388 info->throttle_io_periods[THROTTLE_LEVEL_TIER3] = throttle_period_size;
0a7de745 1389 }
39236c6e
A
1390}
1391
fe8ab488 1392#if CONFIG_IOSCHED
0a7de745
A
1393extern void vm_io_reprioritize_init(void);
1394int iosched_enabled = 1;
fe8ab488
A
1395#endif
1396
316670eb
A
1397void
1398throttle_init(void)
1399{
0a7de745
A
1400 struct _throttle_io_info_t *info;
1401 int i;
1402 int level;
fe8ab488 1403#if CONFIG_IOSCHED
0a7de745 1404 int iosched;
fe8ab488 1405#endif
0a7de745
A
1406 /*
1407 * allocate lock group attribute and group
1408 */
1409 throttle_lock_grp_attr = lck_grp_attr_alloc_init();
1410 throttle_lock_grp = lck_grp_alloc_init("throttle I/O", throttle_lock_grp_attr);
316670eb 1411
39236c6e
A
1412 /* Update throttle parameters based on device tree configuration */
1413 throttle_init_throttle_window();
1414
0a7de745
A
1415 /*
1416 * allocate the lock attribute
1417 */
1418 throttle_lock_attr = lck_attr_alloc_init();
316670eb
A
1419
1420 for (i = 0; i < LOWPRI_MAX_NUM_DEV; i++) {
0a7de745
A
1421 info = &_throttle_io_info[i];
1422
1423 lck_mtx_init(&info->throttle_lock, throttle_lock_grp, throttle_lock_attr);
316670eb
A
1424 info->throttle_timer_call = thread_call_allocate((thread_call_func_t)throttle_timer, (thread_call_param_t)info);
1425
39236c6e
A
1426 for (level = 0; level <= THROTTLE_LEVEL_END; level++) {
1427 TAILQ_INIT(&info->throttle_uthlist[level]);
1428 info->throttle_last_IO_pid[level] = 0;
39037602 1429 info->throttle_inflight_count[level] = 0;
39236c6e
A
1430 }
1431 info->throttle_next_wake_level = THROTTLE_LEVEL_END;
fe8ab488 1432 info->throttle_disabled = 0;
3e170ce0 1433 info->throttle_is_fusion_with_priority = 0;
fe8ab488
A
1434 }
1435#if CONFIG_IOSCHED
1436 if (PE_parse_boot_argn("iosched", &iosched, sizeof(iosched))) {
1437 iosched_enabled = iosched;
1438 }
1439 if (iosched_enabled) {
1440 /* Initialize I/O Reprioritization mechanism */
1441 vm_io_reprioritize_init();
316670eb 1442 }
fe8ab488 1443#endif
316670eb
A
1444}
1445
39236c6e 1446void
d9a64523 1447sys_override_io_throttle(boolean_t enable_override)
39236c6e 1448{
0a7de745 1449 if (enable_override) {
39236c6e 1450 lowpri_throttle_enabled = 0;
0a7de745 1451 } else {
d9a64523 1452 lowpri_throttle_enabled = 1;
0a7de745 1453 }
39236c6e
A
1454}
1455
39037602 1456int rethrottle_wakeups = 0;
316670eb
A
1457
1458/*
39037602
A
1459 * the uu_rethrottle_lock is used to synchronize this function
1460 * with "throttle_lowpri_io" which is where a throttled thread
1461 * will block... that function will grab this lock before beginning
1462 * it's decision making process concerning the need to block, and
1463 * hold it through the assert_wait. When that thread is awakened
1464 * for any reason (timer or rethrottle), it will reacquire the
1465 * uu_rethrottle_lock before determining if it really is ok for
1466 * it to now run. This is the point at which the thread could
1467 * enter a different throttling queue and reblock or return from
1468 * the throttle w/o having waited out it's entire throttle if
1469 * the rethrottle has now moved it out of any currently
1470 * active throttle window.
39236c6e 1471 *
39037602
A
1472 *
1473 * NOTES:
1474 * 1 - This may be called with the task lock held.
1475 * 2 - This may be called with preemption and interrupts disabled
1476 * in the kqueue wakeup path so we can't take the throttle_lock which is a mutex
1477 * 3 - This cannot safely dereference uu_throttle_info, as it may
1478 * get deallocated out from under us
316670eb 1479 */
39236c6e 1480
316670eb 1481void
39236c6e 1482rethrottle_thread(uthread_t ut)
316670eb 1483{
39037602
A
1484 /*
1485 * If uthread doesn't have throttle state, then there's no chance
1486 * of it needing a rethrottle.
1487 */
0a7de745 1488 if (ut->uu_throttle_info == NULL) {
39236c6e 1489 return;
0a7de745 1490 }
316670eb 1491
39037602
A
1492 boolean_t s = ml_set_interrupts_enabled(FALSE);
1493 lck_spin_lock(&ut->uu_rethrottle_lock);
316670eb 1494
0a7de745 1495 if (!ut->uu_is_throttled) {
d9a64523 1496 ut->uu_was_rethrottled = true;
0a7de745 1497 } else {
39037602 1498 int my_new_level = throttle_get_thread_throttle_level(ut);
316670eb 1499
39236c6e 1500 if (my_new_level != ut->uu_on_throttlelist) {
39037602
A
1501 /*
1502 * ut is currently blocked (as indicated by
d9a64523 1503 * ut->uu_is_throttled == true)
39037602
A
1504 * and we're changing it's throttle level, so
1505 * we need to wake it up.
1506 */
d9a64523 1507 ut->uu_is_throttled = false;
39037602 1508 wakeup(&ut->uu_on_throttlelist);
39236c6e 1509
39037602
A
1510 rethrottle_wakeups++;
1511 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_FSRW, 102)), thread_tid(ut->uu_thread), ut->uu_on_throttlelist, my_new_level, 0, 0);
39236c6e
A
1512 }
1513 }
39037602
A
1514 lck_spin_unlock(&ut->uu_rethrottle_lock);
1515 ml_set_interrupts_enabled(s);
316670eb
A
1516}
1517
1518
b0d623f7
A
1519/*
1520 * KPI routine
1521 *
1522 * Create and take a reference on a throttle info structure and return a
1523 * pointer for the file system to use when calling throttle_info_update.
1524 * Calling file system must have a matching release for every create.
1525 */
1526void *
1527throttle_info_create(void)
1528{
0a7de745
A
1529 struct _throttle_io_info_t *info;
1530 int level;
b0d623f7
A
1531
1532 MALLOC(info, struct _throttle_io_info_t *, sizeof(*info), M_TEMP, M_ZERO | M_WAITOK);
1533 /* Should never happen but just in case */
0a7de745 1534 if (info == NULL) {
b0d623f7 1535 return NULL;
0a7de745 1536 }
b0d623f7
A
1537 /* Mark that this one was allocated and needs to be freed */
1538 DEBUG_ALLOC_THROTTLE_INFO("Creating info = %p\n", info, info );
316670eb
A
1539 info->throttle_alloc = TRUE;
1540
39037602 1541 lck_mtx_init(&info->throttle_lock, throttle_lock_grp, throttle_lock_attr);
316670eb
A
1542 info->throttle_timer_call = thread_call_allocate((thread_call_func_t)throttle_timer, (thread_call_param_t)info);
1543
39236c6e
A
1544 for (level = 0; level <= THROTTLE_LEVEL_END; level++) {
1545 TAILQ_INIT(&info->throttle_uthlist[level]);
1546 }
1547 info->throttle_next_wake_level = THROTTLE_LEVEL_END;
316670eb 1548
b0d623f7 1549 /* Take a reference */
316670eb 1550 OSIncrementAtomic(&info->throttle_refcnt);
b0d623f7
A
1551 return info;
1552}
1553
1554/*
1555 * KPI routine
1556 *
0a7de745
A
1557 * Release the throttle info pointer if all the reference are gone. Should be
1558 * called to release reference taken by throttle_info_create
1559 */
b0d623f7
A
1560void
1561throttle_info_release(void *throttle_info)
1562{
1563 DEBUG_ALLOC_THROTTLE_INFO("Releaseing info = %p\n",
0a7de745
A
1564 (struct _throttle_io_info_t *)throttle_info,
1565 (struct _throttle_io_info_t *)throttle_info);
1566 if (throttle_info) { /* Just to be careful */
b0d623f7 1567 throttle_info_rel(throttle_info);
0a7de745 1568 }
b0d623f7
A
1569}
1570
1571/*
1572 * KPI routine
1573 *
1574 * File Systems that create an info structure, need to call this routine in
1575 * their mount routine (used by cluster code). File Systems that call this in
1576 * their mount routines must call throttle_info_mount_rel in their unmount
0a7de745 1577 * routines.
b0d623f7 1578 */
0a7de745 1579void
b0d623f7
A
1580throttle_info_mount_ref(mount_t mp, void *throttle_info)
1581{
0a7de745 1582 if ((throttle_info == NULL) || (mp == NULL)) {
b0d623f7 1583 return;
0a7de745 1584 }
b0d623f7 1585 throttle_info_ref(throttle_info);
316670eb
A
1586
1587 /*
1588 * We already have a reference release it before adding the new one
1589 */
0a7de745 1590 if (mp->mnt_throttle_info) {
b0d623f7 1591 throttle_info_rel(mp->mnt_throttle_info);
0a7de745 1592 }
b0d623f7
A
1593 mp->mnt_throttle_info = throttle_info;
1594}
1595
6d2010ae
A
1596/*
1597 * Private KPI routine
1598 *
1599 * return a handle for accessing throttle_info given a throttle_mask. The
1600 * handle must be released by throttle_info_rel_by_mask
1601 */
1602int
316670eb 1603throttle_info_ref_by_mask(uint64_t throttle_mask, throttle_info_handle_t *throttle_info_handle)
6d2010ae 1604{
0a7de745 1605 int dev_index;
6d2010ae
A
1606 struct _throttle_io_info_t *info;
1607
0a7de745 1608 if (throttle_info_handle == NULL) {
6d2010ae 1609 return EINVAL;
0a7de745
A
1610 }
1611
6d2010ae
A
1612 dev_index = num_trailing_0(throttle_mask);
1613 info = &_throttle_io_info[dev_index];
1614 throttle_info_ref(info);
1615 *(struct _throttle_io_info_t**)throttle_info_handle = info;
316670eb 1616
6d2010ae
A
1617 return 0;
1618}
1619
1620/*
1621 * Private KPI routine
1622 *
1623 * release the handle obtained by throttle_info_ref_by_mask
1624 */
1625void
1626throttle_info_rel_by_mask(throttle_info_handle_t throttle_info_handle)
1627{
316670eb
A
1628 /*
1629 * for now the handle is just a pointer to _throttle_io_info_t
1630 */
6d2010ae
A
1631 throttle_info_rel((struct _throttle_io_info_t*)throttle_info_handle);
1632}
1633
b0d623f7
A
1634/*
1635 * KPI routine
1636 *
1637 * File Systems that throttle_info_mount_ref, must call this routine in their
1638 * umount routine.
0a7de745 1639 */
b0d623f7
A
1640void
1641throttle_info_mount_rel(mount_t mp)
1642{
0a7de745 1643 if (mp->mnt_throttle_info) {
b0d623f7 1644 throttle_info_rel(mp->mnt_throttle_info);
0a7de745 1645 }
b0d623f7
A
1646 mp->mnt_throttle_info = NULL;
1647}
1648
5ba3f43e
A
1649/*
1650 * Reset throttling periods for the given mount point
1651 *
1652 * private interface used by disk conditioner to reset
1653 * throttling periods when 'is_ssd' status changes
1654 */
1655void
1656throttle_info_mount_reset_period(mount_t mp, int isssd)
1657{
1658 struct _throttle_io_info_t *info;
1659
0a7de745 1660 if (mp == NULL) {
5ba3f43e 1661 info = &_throttle_io_info[LOWPRI_MAX_NUM_DEV - 1];
0a7de745 1662 } else if (mp->mnt_throttle_info == NULL) {
5ba3f43e 1663 info = &_throttle_io_info[mp->mnt_devbsdunit];
0a7de745 1664 } else {
5ba3f43e 1665 info = mp->mnt_throttle_info;
0a7de745 1666 }
5ba3f43e
A
1667
1668 throttle_init_throttle_period(info, isssd);
1669}
1670
e2fac8b1
A
1671void
1672throttle_info_get_last_io_time(mount_t mp, struct timeval *tv)
1673{
0a7de745 1674 struct _throttle_io_info_t *info;
e2fac8b1 1675
0a7de745 1676 if (mp == NULL) {
316670eb 1677 info = &_throttle_io_info[LOWPRI_MAX_NUM_DEV - 1];
0a7de745 1678 } else if (mp->mnt_throttle_info == NULL) {
316670eb 1679 info = &_throttle_io_info[mp->mnt_devbsdunit];
0a7de745 1680 } else {
316670eb 1681 info = mp->mnt_throttle_info;
0a7de745 1682 }
b0d623f7 1683
316670eb 1684 *tv = info->throttle_last_write_timestamp;
e2fac8b1
A
1685}
1686
1687void
1688update_last_io_time(mount_t mp)
1689{
0a7de745
A
1690 struct _throttle_io_info_t *info;
1691
1692 if (mp == NULL) {
316670eb 1693 info = &_throttle_io_info[LOWPRI_MAX_NUM_DEV - 1];
0a7de745 1694 } else if (mp->mnt_throttle_info == NULL) {
316670eb 1695 info = &_throttle_io_info[mp->mnt_devbsdunit];
0a7de745 1696 } else {
316670eb 1697 info = mp->mnt_throttle_info;
0a7de745 1698 }
e2fac8b1 1699
316670eb 1700 microuptime(&info->throttle_last_write_timestamp);
0a7de745 1701 if (mp != NULL) {
39236c6e 1702 mp->mnt_last_write_completed_timestamp = info->throttle_last_write_timestamp;
0a7de745 1703 }
e2fac8b1
A
1704}
1705
316670eb
A
1706int
1707throttle_get_io_policy(uthread_t *ut)
6d2010ae 1708{
0a7de745 1709 if (ut != NULL) {
39236c6e 1710 *ut = get_bsdthread_info(current_thread());
0a7de745 1711 }
316670eb 1712
0a7de745 1713 return proc_get_effective_thread_policy(current_thread(), TASK_POLICY_IO);
6d2010ae 1714}
6d2010ae 1715
39236c6e
A
1716int
1717throttle_get_passive_io_policy(uthread_t *ut)
1718{
0a7de745 1719 if (ut != NULL) {
39236c6e 1720 *ut = get_bsdthread_info(current_thread());
0a7de745 1721 }
39236c6e 1722
0a7de745 1723 return proc_get_effective_thread_policy(current_thread(), TASK_POLICY_PASSIVE_IO);
39236c6e 1724}
6d2010ae 1725
316670eb
A
1726
1727static int
39236c6e
A
1728throttle_get_thread_throttle_level(uthread_t ut)
1729{
d190cdc3
A
1730 uthread_t *ut_p = (ut == NULL) ? &ut : NULL;
1731 int io_tier = throttle_get_io_policy(ut_p);
316670eb 1732
d190cdc3
A
1733 return throttle_get_thread_throttle_level_internal(ut, io_tier);
1734}
1735
1736/*
1737 * Return a throttle level given an existing I/O tier (such as returned by throttle_get_io_policy)
1738 */
1739static int
0a7de745
A
1740throttle_get_thread_throttle_level_internal(uthread_t ut, int io_tier)
1741{
d190cdc3
A
1742 int thread_throttle_level = io_tier;
1743 int user_idle_level;
316670eb 1744
d190cdc3 1745 assert(ut != NULL);
316670eb 1746
39236c6e 1747 /* Bootcache misses should always be throttled */
0a7de745 1748 if (ut->uu_throttle_bc) {
39236c6e 1749 thread_throttle_level = THROTTLE_LEVEL_TIER3;
0a7de745 1750 }
316670eb 1751
d190cdc3
A
1752 /*
1753 * Issue tier3 I/O as tier2 when the user is idle
1754 * to allow maintenance tasks to make more progress.
1755 *
1756 * Assume any positive idle level is enough... for now it's
1757 * only ever 0 or 128 but this is not defined anywhere.
1758 */
1759 if (thread_throttle_level >= THROTTLE_LEVEL_TIER3) {
1760 user_idle_level = timer_get_user_idle_level();
1761 if (user_idle_level > 0) {
1762 thread_throttle_level--;
1763 }
1764 }
1765
0a7de745 1766 return thread_throttle_level;
6d2010ae 1767}
6d2010ae 1768
39037602
A
1769/*
1770 * I/O will be throttled if either of the following are true:
1771 * - Higher tiers have in-flight I/O
1772 * - The time delta since the last start/completion of a higher tier is within the throttle window interval
1773 *
1774 * In-flight I/O is bookended by throttle_info_update_internal/throttle_info_end_io_internal
1775 */
b0d623f7 1776static int
39236c6e 1777throttle_io_will_be_throttled_internal(void * throttle_info, int * mylevel, int * throttling_level)
2d21ac55 1778{
0a7de745 1779 struct _throttle_io_info_t *info = throttle_info;
2d21ac55 1780 struct timeval elapsed;
39037602 1781 struct timeval now;
db609669 1782 uint64_t elapsed_msecs;
0a7de745
A
1783 int thread_throttle_level;
1784 int throttle_level;
6d2010ae 1785
0a7de745
A
1786 if ((thread_throttle_level = throttle_get_thread_throttle_level(NULL)) < THROTTLE_LEVEL_THROTTLED) {
1787 return THROTTLE_DISENGAGED;
1788 }
2d21ac55 1789
39037602 1790 microuptime(&now);
2d21ac55 1791
39037602
A
1792 for (throttle_level = THROTTLE_LEVEL_START; throttle_level < thread_throttle_level; throttle_level++) {
1793 if (info->throttle_inflight_count[throttle_level]) {
1794 break;
1795 }
1796 elapsed = now;
39236c6e 1797 timevalsub(&elapsed, &info->throttle_window_start_timestamp[throttle_level]);
db609669 1798 elapsed_msecs = (uint64_t)elapsed.tv_sec * (uint64_t)1000 + (elapsed.tv_usec / 1000);
2d21ac55 1799
0a7de745 1800 if (elapsed_msecs < (uint64_t)throttle_windows_msecs[thread_throttle_level]) {
316670eb 1801 break;
0a7de745 1802 }
316670eb
A
1803 }
1804 if (throttle_level >= thread_throttle_level) {
1805 /*
1806 * we're beyond all of the throttle windows
1807 * that affect the throttle level of this thread,
1808 * so go ahead and treat as normal I/O
1809 */
0a7de745 1810 return THROTTLE_DISENGAGED;
316670eb 1811 }
0a7de745 1812 if (mylevel) {
39236c6e 1813 *mylevel = thread_throttle_level;
0a7de745
A
1814 }
1815 if (throttling_level) {
39236c6e 1816 *throttling_level = throttle_level;
0a7de745 1817 }
39236c6e 1818
316670eb
A
1819 if (info->throttle_io_count != info->throttle_io_count_begin) {
1820 /*
1821 * we've already issued at least one throttleable I/O
1822 * in the current I/O window, so avoid issuing another one
1823 */
0a7de745 1824 return THROTTLE_NOW;
316670eb
A
1825 }
1826 /*
1827 * we're in the throttle window, so
1828 * cut the I/O size back
1829 */
0a7de745 1830 return THROTTLE_ENGAGED;
593a1d5f 1831}
2d21ac55 1832
0a7de745 1833/*
b0d623f7
A
1834 * If we have a mount point and it has a throttle info pointer then
1835 * use it to do the check, otherwise use the device unit number to find
1836 * the correct throttle info array element.
1837 */
1838int
316670eb 1839throttle_io_will_be_throttled(__unused int lowpri_window_msecs, mount_t mp)
b0d623f7 1840{
0a7de745 1841 struct _throttle_io_info_t *info;
b0d623f7 1842
316670eb
A
1843 /*
1844 * Should we just return zero if no mount point
1845 */
0a7de745
A
1846 if (mp == NULL) {
1847 info = &_throttle_io_info[LOWPRI_MAX_NUM_DEV - 1];
1848 } else if (mp->mnt_throttle_info == NULL) {
1849 info = &_throttle_io_info[mp->mnt_devbsdunit];
1850 } else {
1851 info = mp->mnt_throttle_info;
1852 }
316670eb 1853
3e170ce0
A
1854 if (info->throttle_is_fusion_with_priority) {
1855 uthread_t ut = get_bsdthread_info(current_thread());
0a7de745
A
1856 if (ut->uu_lowpri_window == 0) {
1857 return THROTTLE_DISENGAGED;
1858 }
3e170ce0
A
1859 }
1860
0a7de745
A
1861 if (info->throttle_disabled) {
1862 return THROTTLE_DISENGAGED;
1863 } else {
fe8ab488 1864 return throttle_io_will_be_throttled_internal(info, NULL, NULL);
0a7de745 1865 }
b0d623f7
A
1866}
1867
0a7de745 1868/*
39236c6e
A
1869 * Routine to increment I/O throttling counters maintained in the proc
1870 */
1871
0a7de745 1872static void
fe8ab488 1873throttle_update_proc_stats(pid_t throttling_pid, int count)
39236c6e
A
1874{
1875 proc_t throttling_proc;
1876 proc_t throttled_proc = current_proc();
1877
1878 /* The throttled_proc is always the current proc; so we are not concerned with refs */
fe8ab488 1879 OSAddAtomic64(count, &(throttled_proc->was_throttled));
0a7de745 1880
39236c6e
A
1881 /* The throttling pid might have exited by now */
1882 throttling_proc = proc_find(throttling_pid);
1883 if (throttling_proc != PROC_NULL) {
fe8ab488 1884 OSAddAtomic64(count, &(throttling_proc->did_throttle));
39236c6e
A
1885 proc_rele(throttling_proc);
1886 }
1887}
316670eb 1888
39236c6e
A
1889/*
1890 * Block until woken up by the throttle timer or by a rethrottle call.
1891 * As long as we hold the throttle_lock while querying the throttle tier, we're
1892 * safe against seeing an old throttle tier after a rethrottle.
1893 */
6d2010ae
A
1894uint32_t
1895throttle_lowpri_io(int sleep_amount)
593a1d5f 1896{
316670eb 1897 uthread_t ut;
b0d623f7 1898 struct _throttle_io_info_t *info;
0a7de745
A
1899 int throttle_type = 0;
1900 int mylevel = 0;
1901 int throttling_level = THROTTLE_LEVEL_NONE;
1902 int sleep_cnt = 0;
316670eb
A
1903 uint32_t throttle_io_period_num = 0;
1904 boolean_t insert_tail = TRUE;
39037602 1905 boolean_t s;
2d21ac55 1906
593a1d5f
A
1907 ut = get_bsdthread_info(current_thread());
1908
0a7de745
A
1909 if (ut->uu_lowpri_window == 0) {
1910 return 0;
1911 }
593a1d5f 1912
b0d623f7 1913 info = ut->uu_throttle_info;
593a1d5f 1914
39236c6e 1915 if (info == NULL) {
d9a64523 1916 ut->uu_throttle_bc = false;
39236c6e 1917 ut->uu_lowpri_window = 0;
0a7de745 1918 return 0;
39236c6e 1919 }
39236c6e 1920 lck_mtx_lock(&info->throttle_lock);
39037602 1921 assert(ut->uu_on_throttlelist < THROTTLE_LEVEL_THROTTLED);
39236c6e 1922
0a7de745 1923 if (sleep_amount == 0) {
316670eb 1924 goto done;
0a7de745 1925 }
b0d623f7 1926
0a7de745 1927 if (sleep_amount == 1 && !ut->uu_throttle_bc) {
316670eb 1928 sleep_amount = 0;
0a7de745 1929 }
6d2010ae 1930
316670eb
A
1931 throttle_io_period_num = info->throttle_io_period_num;
1932
d9a64523 1933 ut->uu_was_rethrottled = false;
39037602 1934
0a7de745 1935 while ((throttle_type = throttle_io_will_be_throttled_internal(info, &mylevel, &throttling_level))) {
39236c6e 1936 if (throttle_type == THROTTLE_ENGAGED) {
0a7de745 1937 if (sleep_amount == 0) {
316670eb 1938 break;
0a7de745
A
1939 }
1940 if (info->throttle_io_period_num < throttle_io_period_num) {
593a1d5f 1941 break;
0a7de745
A
1942 }
1943 if ((info->throttle_io_period_num - throttle_io_period_num) >= (uint32_t)sleep_amount) {
1944 break;
1945 }
2d21ac55 1946 }
39037602
A
1947 /*
1948 * keep the same position in the list if "rethrottle_thread" changes our throttle level and
1949 * then puts us back to the original level before we get a chance to run
1950 */
1951 if (ut->uu_on_throttlelist >= THROTTLE_LEVEL_THROTTLED && ut->uu_on_throttlelist != mylevel) {
1952 /*
1953 * must have been awakened via "rethrottle_thread" (the timer pulls us off the list)
1954 * and we've changed our throttling level, so pull ourselves off of the appropriate list
1955 * and make sure we get put on the tail of the new list since we're starting anew w/r to
1956 * the throttling engine
1957 */
1958 TAILQ_REMOVE(&info->throttle_uthlist[ut->uu_on_throttlelist], ut, uu_throttlelist);
1959 ut->uu_on_throttlelist = THROTTLE_LEVEL_NONE;
1960 insert_tail = TRUE;
1961 }
39236c6e 1962 if (ut->uu_on_throttlelist < THROTTLE_LEVEL_THROTTLED) {
0a7de745 1963 if (throttle_add_to_list(info, ut, mylevel, insert_tail) == THROTTLE_LEVEL_END) {
316670eb 1964 goto done;
0a7de745 1965 }
316670eb 1966 }
39236c6e 1967 assert(throttling_level >= THROTTLE_LEVEL_START && throttling_level <= THROTTLE_LEVEL_END);
39037602
A
1968
1969 s = ml_set_interrupts_enabled(FALSE);
1970 lck_spin_lock(&ut->uu_rethrottle_lock);
1971
1972 /*
1973 * this is the critical section w/r to our interaction
1974 * with "rethrottle_thread"
1975 */
d9a64523 1976 if (ut->uu_was_rethrottled) {
39037602
A
1977 lck_spin_unlock(&ut->uu_rethrottle_lock);
1978 ml_set_interrupts_enabled(s);
1979 lck_mtx_yield(&info->throttle_lock);
1980
1981 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_FSRW, 103)), thread_tid(ut->uu_thread), ut->uu_on_throttlelist, 0, 0, 0);
1982
d9a64523 1983 ut->uu_was_rethrottled = false;
39037602
A
1984 continue;
1985 }
39236c6e 1986 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_THROTTLE, PROCESS_THROTTLED)) | DBG_FUNC_NONE,
0a7de745
A
1987 info->throttle_last_IO_pid[throttling_level], throttling_level, proc_selfpid(), mylevel, 0);
1988
316670eb
A
1989 if (sleep_cnt == 0) {
1990 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_FSRW, 97)) | DBG_FUNC_START,
0a7de745 1991 throttle_windows_msecs[mylevel], info->throttle_io_periods[mylevel], info->throttle_io_count, 0, 0);
39236c6e 1992 throttled_count[mylevel]++;
316670eb 1993 }
39037602
A
1994 ut->uu_wmesg = "throttle_lowpri_io";
1995
1996 assert_wait((caddr_t)&ut->uu_on_throttlelist, THREAD_UNINT);
1997
d9a64523 1998 ut->uu_is_throttled = true;
39037602
A
1999 lck_spin_unlock(&ut->uu_rethrottle_lock);
2000 ml_set_interrupts_enabled(s);
2001
2002 lck_mtx_unlock(&info->throttle_lock);
2003
2004 thread_block(THREAD_CONTINUE_NULL);
2005
2006 ut->uu_wmesg = NULL;
2007
d9a64523
A
2008 ut->uu_is_throttled = false;
2009 ut->uu_was_rethrottled = false;
39037602
A
2010
2011 lck_mtx_lock(&info->throttle_lock);
593a1d5f 2012
316670eb 2013 sleep_cnt++;
0a7de745
A
2014
2015 if (sleep_amount == 0) {
316670eb 2016 insert_tail = FALSE;
0a7de745
A
2017 } else if (info->throttle_io_period_num < throttle_io_period_num ||
2018 (info->throttle_io_period_num - throttle_io_period_num) >= (uint32_t)sleep_amount) {
316670eb
A
2019 insert_tail = FALSE;
2020 sleep_amount = 0;
2021 }
593a1d5f 2022 }
b0d623f7 2023done:
39236c6e
A
2024 if (ut->uu_on_throttlelist >= THROTTLE_LEVEL_THROTTLED) {
2025 TAILQ_REMOVE(&info->throttle_uthlist[ut->uu_on_throttlelist], ut, uu_throttlelist);
2026 ut->uu_on_throttlelist = THROTTLE_LEVEL_NONE;
316670eb 2027 }
39236c6e
A
2028 lck_mtx_unlock(&info->throttle_lock);
2029
2030 if (sleep_cnt) {
316670eb 2031 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_FSRW, 97)) | DBG_FUNC_END,
0a7de745 2032 throttle_windows_msecs[mylevel], info->throttle_io_periods[mylevel], info->throttle_io_count, 0, 0);
fe8ab488
A
2033 /*
2034 * We update the stats for the last pid which opened a throttle window for the throttled thread.
2035 * This might not be completely accurate since the multiple throttles seen by the lower tier pid
0a7de745 2036 * might have been caused by various higher prio pids. However, updating these stats accurately
fe8ab488
A
2037 * means doing a proc_find while holding the throttle lock which leads to deadlock.
2038 */
2039 throttle_update_proc_stats(info->throttle_last_IO_pid[throttling_level], sleep_cnt);
39236c6e
A
2040 }
2041
b0d623f7 2042 ut->uu_throttle_info = NULL;
d9a64523 2043 ut->uu_throttle_bc = false;
316670eb 2044 ut->uu_lowpri_window = 0;
6d2010ae 2045
39037602
A
2046 throttle_info_rel(info);
2047
0a7de745 2048 return sleep_cnt;
593a1d5f
A
2049}
2050
cb323159
A
2051/*
2052 * returns TRUE if the throttle_lowpri_io called with the same sleep_amount would've slept
2053 * This function mimics the most of the throttle_lowpri_io checks but without actual sleeping
2054 */
2055int
2056throttle_lowpri_io_will_be_throttled(int sleep_amount)
2057{
2058 if (sleep_amount == 0) {
2059 return FALSE;
2060 }
2061
2062 uthread_t ut = get_bsdthread_info(current_thread());
2063 if (ut->uu_lowpri_window == 0) {
2064 return FALSE;
2065 }
2066
2067 struct _throttle_io_info_t *info = ut->uu_throttle_info;
2068 if (info == NULL) {
2069 return FALSE;
2070 }
2071
2072 lck_mtx_lock(&info->throttle_lock);
2073 assert(ut->uu_on_throttlelist < THROTTLE_LEVEL_THROTTLED);
2074
2075 if (sleep_amount == 1 && !ut->uu_throttle_bc) {
2076 sleep_amount = 0;
2077 }
2078
2079 int result = FALSE;
2080
2081 int throttle_type = throttle_io_will_be_throttled_internal(info, NULL, NULL);
2082 if (throttle_type > THROTTLE_DISENGAGED) {
2083 result = TRUE;
2084 if ((throttle_type == THROTTLE_ENGAGED) && (sleep_amount == 0)) {
2085 result = FALSE;
2086 }
2087 }
2088
2089 lck_mtx_unlock(&info->throttle_lock);
2090
2091 return result;
2092}
2093
2094
6d2010ae
A
2095/*
2096 * KPI routine
2097 *
2098 * set a kernel thread's IO policy. policy can be:
39236c6e 2099 * IOPOL_NORMAL, IOPOL_THROTTLE, IOPOL_PASSIVE, IOPOL_UTILITY, IOPOL_STANDARD
6d2010ae
A
2100 *
2101 * explanations about these policies are in the man page of setiopolicy_np
2102 */
0a7de745
A
2103void
2104throttle_set_thread_io_policy(int policy)
593a1d5f 2105{
39037602 2106 proc_set_thread_policy(current_thread(), TASK_POLICY_INTERNAL, TASK_POLICY_IOPOL, policy);
6d2010ae 2107}
593a1d5f 2108
0a7de745
A
2109int
2110throttle_get_thread_effective_io_policy()
a39ff7e2
A
2111{
2112 return proc_get_effective_thread_policy(current_thread(), TASK_POLICY_IO);
2113}
2114
0a7de745
A
2115void
2116throttle_info_reset_window(uthread_t ut)
6d2010ae
A
2117{
2118 struct _throttle_io_info_t *info;
2119
0a7de745 2120 if (ut == NULL) {
3e170ce0 2121 ut = get_bsdthread_info(current_thread());
0a7de745 2122 }
3e170ce0 2123
0a7de745 2124 if ((info = ut->uu_throttle_info)) {
316670eb 2125 throttle_info_rel(info);
6d2010ae 2126
316670eb
A
2127 ut->uu_throttle_info = NULL;
2128 ut->uu_lowpri_window = 0;
d9a64523 2129 ut->uu_throttle_bc = false;
316670eb 2130 }
6d2010ae
A
2131}
2132
2133static
0a7de745
A
2134void
2135throttle_info_set_initial_window(uthread_t ut, struct _throttle_io_info_t *info, boolean_t BC_throttle, boolean_t isssd)
6d2010ae 2136{
0a7de745 2137 if (lowpri_throttle_enabled == 0 || info->throttle_disabled) {
39236c6e 2138 return;
0a7de745 2139 }
39236c6e
A
2140
2141 if (info->throttle_io_periods == 0) {
2142 throttle_init_throttle_period(info, isssd);
2143 }
316670eb 2144 if (ut->uu_throttle_info == NULL) {
316670eb
A
2145 ut->uu_throttle_info = info;
2146 throttle_info_ref(info);
2147 DEBUG_ALLOC_THROTTLE_INFO("updating info = %p\n", info, info );
6d2010ae 2148
39236c6e 2149 ut->uu_lowpri_window = 1;
316670eb 2150 ut->uu_throttle_bc = BC_throttle;
593a1d5f 2151 }
2d21ac55 2152}
91447636 2153
39037602
A
2154/*
2155 * Update inflight IO count and throttling window
2156 * Should be called when an IO is done
2157 *
2158 * Only affects IO that was sent through spec_strategy
2159 */
0a7de745
A
2160void
2161throttle_info_end_io(buf_t bp)
2162{
39037602
A
2163 mount_t mp;
2164 struct bufattr *bap;
2165 struct _throttle_io_info_t *info;
d190cdc3 2166 int io_tier;
39037602
A
2167
2168 bap = &bp->b_attr;
2169 if (!ISSET(bap->ba_flags, BA_STRATEGY_TRACKED_IO)) {
2170 return;
2171 }
2172 CLR(bap->ba_flags, BA_STRATEGY_TRACKED_IO);
2173
2174 mp = buf_vnode(bp)->v_mount;
2175 if (mp != NULL) {
2176 info = &_throttle_io_info[mp->mnt_devbsdunit];
2177 } else {
2178 info = &_throttle_io_info[LOWPRI_MAX_NUM_DEV - 1];
2179 }
2180
d190cdc3
A
2181 io_tier = GET_BUFATTR_IO_TIER(bap);
2182 if (ISSET(bap->ba_flags, BA_IO_TIER_UPGRADE)) {
2183 io_tier--;
2184 }
2185
2186 throttle_info_end_io_internal(info, io_tier);
39037602
A
2187}
2188
2189/*
2190 * Decrement inflight count initially incremented by throttle_info_update_internal
2191 */
2192static
0a7de745
A
2193void
2194throttle_info_end_io_internal(struct _throttle_io_info_t *info, int throttle_level)
2195{
39037602
A
2196 if (throttle_level == THROTTLE_LEVEL_NONE) {
2197 return;
2198 }
6d2010ae 2199
39037602
A
2200 microuptime(&info->throttle_window_start_timestamp[throttle_level]);
2201 OSDecrementAtomic(&info->throttle_inflight_count[throttle_level]);
2202 assert(info->throttle_inflight_count[throttle_level] >= 0);
2203}
2204
2205/*
2206 * If inflight is TRUE and bap is NULL then the caller is responsible for calling
2207 * throttle_info_end_io_internal to avoid leaking in-flight I/O.
2208 */
6d2010ae 2209static
0a7de745
A
2210int
2211throttle_info_update_internal(struct _throttle_io_info_t *info, uthread_t ut, int flags, boolean_t isssd, boolean_t inflight, struct bufattr *bap)
b0d623f7 2212{
0a7de745 2213 int thread_throttle_level;
b0d623f7 2214
0a7de745 2215 if (lowpri_throttle_enabled == 0 || info->throttle_disabled) {
39037602 2216 return THROTTLE_LEVEL_NONE;
0a7de745 2217 }
b0d623f7 2218
0a7de745 2219 if (ut == NULL) {
316670eb 2220 ut = get_bsdthread_info(current_thread());
0a7de745 2221 }
b0d623f7 2222
39037602
A
2223 if (bap && inflight && !ut->uu_throttle_bc) {
2224 thread_throttle_level = GET_BUFATTR_IO_TIER(bap);
d190cdc3
A
2225 if (ISSET(bap->ba_flags, BA_IO_TIER_UPGRADE)) {
2226 thread_throttle_level--;
2227 }
39037602
A
2228 } else {
2229 thread_throttle_level = throttle_get_thread_throttle_level(ut);
2230 }
316670eb 2231
39236c6e 2232 if (thread_throttle_level != THROTTLE_LEVEL_NONE) {
0a7de745 2233 if (!ISSET(flags, B_PASSIVE)) {
39236c6e 2234 info->throttle_last_IO_pid[thread_throttle_level] = proc_selfpid();
39037602
A
2235 if (inflight && !ut->uu_throttle_bc) {
2236 if (NULL != bap) {
2237 SET(bap->ba_flags, BA_STRATEGY_TRACKED_IO);
2238 }
2239 OSIncrementAtomic(&info->throttle_inflight_count[thread_throttle_level]);
2240 } else {
2241 microuptime(&info->throttle_window_start_timestamp[thread_throttle_level]);
2242 }
39236c6e 2243 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_THROTTLE, OPEN_THROTTLE_WINDOW)) | DBG_FUNC_NONE,
0a7de745 2244 current_proc()->p_pid, thread_throttle_level, 0, 0, 0);
39236c6e 2245 }
316670eb 2246 microuptime(&info->throttle_last_IO_timestamp[thread_throttle_level]);
39236c6e
A
2247 }
2248
316670eb
A
2249
2250 if (thread_throttle_level >= THROTTLE_LEVEL_THROTTLED) {
b0d623f7
A
2251 /*
2252 * I'd really like to do the IOSleep here, but
2253 * we may be holding all kinds of filesystem related locks
2254 * and the pages for this I/O marked 'busy'...
2255 * we don't want to cause a normal task to block on
2256 * one of these locks while we're throttling a task marked
2257 * for low priority I/O... we'll mark the uthread and
2258 * do the delay just before we return from the system
2259 * call that triggered this I/O or from vnode_pagein
2260 */
0a7de745 2261 OSAddAtomic(1, &info->throttle_io_count);
316670eb 2262
39236c6e 2263 throttle_info_set_initial_window(ut, info, FALSE, isssd);
316670eb 2264 }
39037602
A
2265
2266 return thread_throttle_level;
316670eb
A
2267}
2268
0a7de745
A
2269void *
2270throttle_info_update_by_mount(mount_t mp)
316670eb
A
2271{
2272 struct _throttle_io_info_t *info;
2273 uthread_t ut;
2274 boolean_t isssd = FALSE;
2275
2276 ut = get_bsdthread_info(current_thread());
2277
316670eb 2278 if (mp != NULL) {
0a7de745 2279 if (disk_conditioner_mount_is_ssd(mp)) {
316670eb 2280 isssd = TRUE;
0a7de745 2281 }
316670eb 2282 info = &_throttle_io_info[mp->mnt_devbsdunit];
0a7de745 2283 } else {
316670eb 2284 info = &_throttle_io_info[LOWPRI_MAX_NUM_DEV - 1];
0a7de745 2285 }
316670eb 2286
0a7de745 2287 if (!ut->uu_lowpri_window) {
39236c6e 2288 throttle_info_set_initial_window(ut, info, FALSE, isssd);
0a7de745 2289 }
316670eb 2290
39236c6e 2291 return info;
b0d623f7
A
2292}
2293
316670eb 2294
6d2010ae
A
2295/*
2296 * KPI routine
2297 *
2298 * this is usually called before every I/O, used for throttled I/O
2299 * book keeping. This routine has low overhead and does not sleep
2300 */
0a7de745
A
2301void
2302throttle_info_update(void *throttle_info, int flags)
6d2010ae 2303{
0a7de745 2304 if (throttle_info) {
39037602 2305 throttle_info_update_internal(throttle_info, NULL, flags, FALSE, FALSE, NULL);
0a7de745 2306 }
6d2010ae
A
2307}
2308
2309/*
2310 * KPI routine
2311 *
2312 * this is usually called before every I/O, used for throttled I/O
2313 * book keeping. This routine has low overhead and does not sleep
2314 */
0a7de745
A
2315void
2316throttle_info_update_by_mask(void *throttle_info_handle, int flags)
6d2010ae
A
2317{
2318 void *throttle_info = throttle_info_handle;
316670eb
A
2319
2320 /*
2321 * for now we only use the lowest bit of the throttle mask, so the
6d2010ae
A
2322 * handle is the same as the throttle_info. Later if we store a
2323 * set of throttle infos in the handle, we will want to loop through
2324 * them and call throttle_info_update in a loop
2325 */
2326 throttle_info_update(throttle_info, flags);
2327}
fe8ab488
A
2328/*
2329 * KPI routine
0a7de745
A
2330 *
2331 * This routine marks the throttle info as disabled. Used for mount points which
fe8ab488
A
2332 * support I/O scheduling.
2333 */
2334
0a7de745
A
2335void
2336throttle_info_disable_throttle(int devno, boolean_t isfusion)
fe8ab488
A
2337{
2338 struct _throttle_io_info_t *info;
2339
0a7de745 2340 if (devno < 0 || devno >= LOWPRI_MAX_NUM_DEV) {
fe8ab488 2341 panic("Illegal devno (%d) passed into throttle_info_disable_throttle()", devno);
0a7de745 2342 }
fe8ab488
A
2343
2344 info = &_throttle_io_info[devno];
3e170ce0
A
2345 // don't disable software throttling on devices that are part of a fusion device
2346 // and override the software throttle periods to use HDD periods
2347 if (isfusion) {
2348 info->throttle_is_fusion_with_priority = isfusion;
2349 throttle_init_throttle_period(info, FALSE);
2350 }
2351 info->throttle_disabled = !info->throttle_is_fusion_with_priority;
fe8ab488 2352 return;
0a7de745 2353}
fe8ab488 2354
6d2010ae 2355
39236c6e
A
2356/*
2357 * KPI routine (private)
2358 * Called to determine if this IO is being throttled to this level so that it can be treated specially
2359 */
0a7de745
A
2360int
2361throttle_info_io_will_be_throttled(void * throttle_info, int policy)
316670eb 2362{
0a7de745 2363 struct _throttle_io_info_t *info = throttle_info;
316670eb 2364 struct timeval elapsed;
db609669 2365 uint64_t elapsed_msecs;
0a7de745
A
2366 int throttle_level;
2367 int thread_throttle_level;
2368
2369 switch (policy) {
2370 case IOPOL_THROTTLE:
2371 thread_throttle_level = THROTTLE_LEVEL_TIER3;
2372 break;
2373 case IOPOL_UTILITY:
2374 thread_throttle_level = THROTTLE_LEVEL_TIER2;
2375 break;
2376 case IOPOL_STANDARD:
2377 thread_throttle_level = THROTTLE_LEVEL_TIER1;
2378 break;
2379 default:
2380 thread_throttle_level = THROTTLE_LEVEL_TIER0;
316670eb
A
2381 break;
2382 }
2383 for (throttle_level = THROTTLE_LEVEL_START; throttle_level < thread_throttle_level; throttle_level++) {
39037602
A
2384 if (info->throttle_inflight_count[throttle_level]) {
2385 break;
2386 }
316670eb
A
2387
2388 microuptime(&elapsed);
39236c6e 2389 timevalsub(&elapsed, &info->throttle_window_start_timestamp[throttle_level]);
db609669 2390 elapsed_msecs = (uint64_t)elapsed.tv_sec * (uint64_t)1000 + (elapsed.tv_usec / 1000);
316670eb 2391
0a7de745 2392 if (elapsed_msecs < (uint64_t)throttle_windows_msecs[thread_throttle_level]) {
316670eb 2393 break;
0a7de745 2394 }
316670eb
A
2395 }
2396 if (throttle_level >= thread_throttle_level) {
2397 /*
2398 * we're beyond all of the throttle windows
2399 * so go ahead and treat as normal I/O
2400 */
0a7de745 2401 return THROTTLE_DISENGAGED;
316670eb
A
2402 }
2403 /*
2404 * we're in the throttle window
2405 */
0a7de745 2406 return THROTTLE_ENGAGED;
316670eb
A
2407}
2408
0a7de745
A
2409int
2410throttle_lowpri_window(void)
39037602
A
2411{
2412 struct uthread *ut = get_bsdthread_info(current_thread());
2413 return ut->uu_lowpri_window;
2414}
2415
5ba3f43e
A
2416
2417#if CONFIG_IOSCHED
2418int upl_get_cached_tier(void *);
2419#endif
2420
91447636 2421int
2d21ac55 2422spec_strategy(struct vnop_strategy_args *ap)
1c79356b 2423{
0a7de745
A
2424 buf_t bp;
2425 int bflags;
2426 int io_tier;
2427 int passive;
2428 dev_t bdev;
b0d623f7 2429 uthread_t ut;
b0d623f7 2430 mount_t mp;
0a7de745
A
2431 struct bufattr *bap;
2432 int strategy_ret;
6d2010ae
A
2433 struct _throttle_io_info_t *throttle_info;
2434 boolean_t isssd = FALSE;
39037602 2435 boolean_t inflight = FALSE;
d190cdc3 2436 boolean_t upgrade = FALSE;
fe8ab488
A
2437 int code = 0;
2438
5ba3f43e 2439#if !CONFIG_EMBEDDED
316670eb 2440 proc_t curproc = current_proc();
5ba3f43e 2441#endif /* !CONFIG_EMBEDDED */
9bccf70c 2442
0a7de745 2443 bp = ap->a_bp;
91447636 2444 bdev = buf_device(bp);
b0d623f7 2445 mp = buf_vnode(bp)->v_mount;
39236c6e 2446 bap = &bp->b_attr;
9bccf70c 2447
5ba3f43e 2448#if CONFIG_IOSCHED
0a7de745
A
2449 if (bp->b_flags & B_CLUSTER) {
2450 io_tier = upl_get_cached_tier(bp->b_upl);
5ba3f43e 2451
0a7de745
A
2452 if (io_tier == -1) {
2453 io_tier = throttle_get_io_policy(&ut);
2454 }
5ba3f43e 2455#if DEVELOPMENT || DEBUG
0a7de745
A
2456 else {
2457 int my_io_tier = throttle_get_io_policy(&ut);
5ba3f43e 2458
0a7de745
A
2459 if (io_tier != my_io_tier) {
2460 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);
2461 }
2462 }
5ba3f43e 2463#endif
0a7de745
A
2464 } else {
2465 io_tier = throttle_get_io_policy(&ut);
2466 }
5ba3f43e 2467#else
39236c6e 2468 io_tier = throttle_get_io_policy(&ut);
5ba3f43e 2469#endif
39236c6e 2470 passive = throttle_get_passive_io_policy(&ut);
6d2010ae 2471
d190cdc3
A
2472 /*
2473 * Mark if the I/O was upgraded by throttle_get_thread_throttle_level
2474 * while preserving the original issued tier (throttle_get_io_policy
2475 * does not return upgraded tiers)
2476 */
2477 if (mp && io_tier > throttle_get_thread_throttle_level_internal(ut, io_tier)) {
2478#if CONFIG_IOSCHED
2479 if (!(mp->mnt_ioflags & MNT_IOFLAGS_IOSCHED_SUPPORTED)) {
2480 upgrade = TRUE;
2481 }
2482#else /* CONFIG_IOSCHED */
2483 upgrade = TRUE;
2484#endif /* CONFIG_IOSCHED */
2485 }
2486
0a7de745 2487 if (bp->b_flags & B_META) {
39236c6e 2488 bap->ba_flags |= BA_META;
0a7de745 2489 }
316670eb 2490
fe8ab488 2491#if CONFIG_IOSCHED
0a7de745 2492 /*
fe8ab488
A
2493 * For I/O Scheduling, we currently do not have a way to track and expedite metadata I/Os.
2494 * To ensure we dont get into priority inversions due to metadata I/Os, we use the following rules:
2495 * For metadata reads, ceil all I/Os to IOSCHED_METADATA_TIER & mark them passive if the I/O tier was upgraded
2496 * For metadata writes, unconditionally mark them as IOSCHED_METADATA_TIER and passive
2497 */
2498 if (bap->ba_flags & BA_META) {
cb323159 2499 if ((mp && (mp->mnt_ioflags & MNT_IOFLAGS_IOSCHED_SUPPORTED)) || (bap->ba_flags & BA_IO_SCHEDULED)) {
fe8ab488
A
2500 if (bp->b_flags & B_READ) {
2501 if (io_tier > IOSCHED_METADATA_TIER) {
2502 io_tier = IOSCHED_METADATA_TIER;
2503 passive = 1;
2504 }
2505 } else {
2506 io_tier = IOSCHED_METADATA_TIER;
2507 passive = 1;
2508 }
2509 }
2510 }
2511#endif /* CONFIG_IOSCHED */
0a7de745 2512
39236c6e
A
2513 SET_BUFATTR_IO_TIER(bap, io_tier);
2514
fe8ab488 2515 if (passive) {
6d2010ae 2516 bp->b_flags |= B_PASSIVE;
fe8ab488
A
2517 bap->ba_flags |= BA_PASSIVE;
2518 }
6d2010ae 2519
5ba3f43e 2520#if !CONFIG_EMBEDDED
0a7de745 2521 if ((curproc != NULL) && ((curproc->p_flag & P_DELAYIDLESLEEP) == P_DELAYIDLESLEEP)) {
39236c6e 2522 bap->ba_flags |= BA_DELAYIDLESLEEP;
0a7de745 2523 }
5ba3f43e 2524#endif /* !CONFIG_EMBEDDED */
0a7de745 2525
6d2010ae
A
2526 bflags = bp->b_flags;
2527
0a7de745 2528 if (((bflags & B_READ) == 0) && ((bflags & B_ASYNC) == 0)) {
39236c6e 2529 bufattr_markquickcomplete(bap);
0a7de745 2530 }
39236c6e 2531
0a7de745
A
2532 if (bflags & B_READ) {
2533 code |= DKIO_READ;
2534 }
2535 if (bflags & B_ASYNC) {
2536 code |= DKIO_ASYNC;
2537 }
39037602 2538
0a7de745
A
2539 if (bap->ba_flags & BA_META) {
2540 code |= DKIO_META;
2541 } else if (bflags & B_PAGEIO) {
2542 code |= DKIO_PAGING;
2543 }
9bccf70c 2544
0a7de745 2545 if (io_tier != 0) {
fe8ab488 2546 code |= DKIO_THROTTLE;
0a7de745 2547 }
9bccf70c 2548
fe8ab488 2549 code |= ((io_tier << DKIO_TIER_SHIFT) & DKIO_TIER_MASK);
39236c6e 2550
0a7de745 2551 if (bflags & B_PASSIVE) {
fe8ab488 2552 code |= DKIO_PASSIVE;
0a7de745 2553 }
39236c6e 2554
0a7de745 2555 if (bap->ba_flags & BA_NOCACHE) {
fe8ab488 2556 code |= DKIO_NOCACHE;
0a7de745 2557 }
316670eb 2558
d190cdc3
A
2559 if (upgrade) {
2560 code |= DKIO_TIER_UPGRADE;
2561 SET(bap->ba_flags, BA_IO_TIER_UPGRADE);
2562 }
2563
fe8ab488 2564 if (kdebug_enable) {
316670eb 2565 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON, FSDBG_CODE(DBG_DKRW, code) | DBG_FUNC_NONE,
0a7de745
A
2566 buf_kernel_addrperm_addr(bp), bdev, buf_blkno(bp), buf_count(bp), 0);
2567 }
fe8ab488
A
2568
2569 thread_update_io_stats(current_thread(), buf_count(bp), code);
2570
6d2010ae 2571 if (mp != NULL) {
0a7de745 2572 if (disk_conditioner_mount_is_ssd(mp)) {
6d2010ae 2573 isssd = TRUE;
0a7de745 2574 }
39037602
A
2575 /*
2576 * Partially initialized mounts don't have a final devbsdunit and should not be tracked.
2577 * Verify that devbsdunit is initialized (non-zero) or that 0 is the correct initialized value
2578 * (mnt_throttle_mask is initialized and num_trailing_0 would be 0)
2579 */
2580 if (mp->mnt_devbsdunit || (mp->mnt_throttle_mask != LOWPRI_MAX_NUM_DEV - 1 && mp->mnt_throttle_mask & 0x1)) {
2581 inflight = TRUE;
2582 }
6d2010ae 2583 throttle_info = &_throttle_io_info[mp->mnt_devbsdunit];
0a7de745 2584 } else {
6d2010ae 2585 throttle_info = &_throttle_io_info[LOWPRI_MAX_NUM_DEV - 1];
0a7de745 2586 }
2d21ac55 2587
39037602 2588 throttle_info_update_internal(throttle_info, ut, bflags, isssd, inflight, bap);
e2fac8b1 2589
b0d623f7 2590 if ((bflags & B_READ) == 0) {
316670eb
A
2591 microuptime(&throttle_info->throttle_last_write_timestamp);
2592
b0d623f7 2593 if (mp) {
39236c6e 2594 mp->mnt_last_write_issued_timestamp = throttle_info->throttle_last_write_timestamp;
b0d623f7
A
2595 INCR_PENDING_IO(buf_count(bp), mp->mnt_pending_write_size);
2596 }
2597 } else if (mp) {
2598 INCR_PENDING_IO(buf_count(bp), mp->mnt_pending_read_size);
e2fac8b1 2599 }
6d2010ae
A
2600 /*
2601 * The BootCache may give us special information about
2602 * the IO, so it returns special values that we check
2603 * for here.
2604 *
2605 * IO_SATISFIED_BY_CACHE
2606 * The read has been satisfied by the boot cache. Don't
2607 * throttle the thread unnecessarily.
2608 *
2609 * IO_SHOULD_BE_THROTTLED
2610 * The boot cache is playing back a playlist and this IO
2611 * cut through. Throttle it so we're not cutting through
2612 * the boot cache too often.
2613 *
2614 * Note that typical strategy routines are defined with
0a7de745 2615 * a void return so we'll get garbage here. In the
6d2010ae
A
2616 * unlikely case the garbage matches our special return
2617 * value, it's not a big deal since we're only adjusting
2618 * the throttling delay.
0a7de745 2619 */
6d2010ae
A
2620#define IO_SATISFIED_BY_CACHE ((int)0xcafefeed)
2621#define IO_SHOULD_BE_THROTTLED ((int)0xcafebeef)
0a7de745
A
2622 typedef int strategy_fcn_ret_t(struct buf *bp);
2623
6d2010ae 2624 strategy_ret = (*(strategy_fcn_ret_t*)bdevsw[major(bdev)].d_strategy)(bp);
5ba3f43e
A
2625
2626 // disk conditioner needs to track when this I/O actually starts
2627 // which means track it after `strategy` which may include delays
2628 // from inflight I/Os
2629 microuptime(&bp->b_timestamp_tv);
0a7de745 2630
316670eb 2631 if (IO_SATISFIED_BY_CACHE == strategy_ret) {
6d2010ae
A
2632 /*
2633 * If this was a throttled IO satisfied by the boot cache,
2634 * don't delay the thread.
2635 */
2636 throttle_info_reset_window(ut);
316670eb 2637 } else if (IO_SHOULD_BE_THROTTLED == strategy_ret) {
6d2010ae
A
2638 /*
2639 * If the boot cache indicates this IO should be throttled,
2640 * delay the thread.
2641 */
39236c6e 2642 throttle_info_set_initial_window(ut, throttle_info, TRUE, isssd);
6d2010ae 2643 }
0a7de745 2644 return 0;
ccc36f2f
A
2645}
2646
1c79356b
A
2647
2648/*
2649 * This is a noop, simply returning what one has been given.
2650 */
91447636
A
2651int
2652spec_blockmap(__unused struct vnop_blockmap_args *ap)
1c79356b 2653{
0a7de745 2654 return ENOTSUP;
1c79356b
A
2655}
2656
2657
2658/*
2659 * Device close routine
2660 */
91447636 2661int
2d21ac55 2662spec_close(struct vnop_close_args *ap)
1c79356b 2663{
2d21ac55 2664 struct vnode *vp = ap->a_vp;
1c79356b 2665 dev_t dev = vp->v_rdev;
6d2010ae 2666 int error = 0;
2d21ac55 2667 int flags = ap->a_fflag;
91447636 2668 struct proc *p = vfs_context_proc(ap->a_context);
2d21ac55 2669 struct session *sessp;
1c79356b
A
2670
2671 switch (vp->v_type) {
1c79356b
A
2672 case VCHR:
2673 /*
2674 * Hack: a tty device that is a controlling terminal
2675 * has a reference from the session structure.
2676 * We cannot easily tell that a character device is
2677 * a controlling terminal, unless it is the closing
2678 * process' controlling terminal. In that case,
b0d623f7 2679 * if the reference count is 1 (this is the very
316670eb 2680 * last close)
1c79356b 2681 */
2d21ac55 2682 sessp = proc_session(p);
39236c6e 2683 devsw_lock(dev, S_IFCHR);
2d21ac55 2684 if (sessp != SESSION_NULL) {
316670eb 2685 if (vp == sessp->s_ttyvp && vcount(vp) == 1) {
fe8ab488 2686 struct tty *tp = TTY_NULL;
6d2010ae 2687
39236c6e 2688 devsw_unlock(dev, S_IFCHR);
2d21ac55 2689 session_lock(sessp);
6d2010ae 2690 if (vp == sessp->s_ttyvp) {
316670eb 2691 tp = SESSION_TP(sessp);
6d2010ae
A
2692 sessp->s_ttyvp = NULL;
2693 sessp->s_ttyvid = 0;
2694 sessp->s_ttyp = TTY_NULL;
2695 sessp->s_ttypgrpid = NO_PID;
0a7de745 2696 }
2d21ac55 2697 session_unlock(sessp);
6d2010ae 2698
fe8ab488
A
2699 if (tp != TTY_NULL) {
2700 /*
2701 * We may have won a race with a proc_exit
2702 * of the session leader, the winner
2703 * clears the flag (even if not set)
2704 */
2705 tty_lock(tp);
2706 ttyclrpgrphup(tp);
2707 tty_unlock(tp);
2708
2709 ttyfree(tp);
6d2010ae 2710 }
39236c6e 2711 devsw_lock(dev, S_IFCHR);
2d21ac55
A
2712 }
2713 session_rele(sessp);
1c79356b 2714 }
2d21ac55 2715
0a7de745 2716 if (--vp->v_specinfo->si_opencount < 0) {
316670eb 2717 panic("negative open count (c, %u, %u)", major(dev), minor(dev));
0a7de745 2718 }
6d2010ae 2719
1c79356b 2720 /*
39236c6e 2721 * close on last reference or on vnode revoke call
1c79356b 2722 */
0a7de745 2723 if (vcount(vp) == 0 || (flags & IO_REVOKE) != 0) {
316670eb 2724 error = cdevsw[major(dev)].d_close(dev, flags, S_IFCHR, p);
0a7de745 2725 }
6d2010ae
A
2726
2727 devsw_unlock(dev, S_IFCHR);
1c79356b
A
2728 break;
2729
2730 case VBLK:
1c79356b 2731 /*
6d2010ae
A
2732 * If there is more than one outstanding open, don't
2733 * send the close to the device.
0b4e3aa0 2734 */
6d2010ae
A
2735 devsw_lock(dev, S_IFBLK);
2736 if (vcount(vp) > 1) {
2737 vp->v_specinfo->si_opencount--;
2738 devsw_unlock(dev, S_IFBLK);
0a7de745 2739 return 0;
6d2010ae
A
2740 }
2741 devsw_unlock(dev, S_IFBLK);
0b4e3aa0
A
2742
2743 /*
2744 * On last close of a block device (that isn't mounted)
2745 * we must invalidate any in core blocks, so that
2746 * we can, for instance, change floppy disks.
2747 */
0a7de745
A
2748 if ((error = spec_fsync_internal(vp, MNT_WAIT, ap->a_context))) {
2749 return error;
2750 }
91447636
A
2751
2752 error = buf_invalidateblks(vp, BUF_WRITE_DATA, 0, 0);
0a7de745
A
2753 if (error) {
2754 return error;
2755 }
b0d623f7 2756
6d2010ae
A
2757 devsw_lock(dev, S_IFBLK);
2758
0a7de745 2759 if (--vp->v_specinfo->si_opencount < 0) {
316670eb 2760 panic("negative open count (b, %u, %u)", major(dev), minor(dev));
0a7de745 2761 }
6d2010ae 2762
0a7de745 2763 if (vcount(vp) == 0) {
316670eb 2764 error = bdevsw[major(dev)].d_close(dev, flags, S_IFBLK, p);
0a7de745 2765 }
6d2010ae
A
2766
2767 devsw_unlock(dev, S_IFBLK);
1c79356b
A
2768 break;
2769
2770 default:
2771 panic("spec_close: not special");
0a7de745 2772 return EBADF;
1c79356b
A
2773 }
2774
6d2010ae 2775 return error;
1c79356b
A
2776}
2777
2778/*
2779 * Return POSIX pathconf information applicable to special devices.
2780 */
91447636 2781int
2d21ac55 2782spec_pathconf(struct vnop_pathconf_args *ap)
1c79356b 2783{
1c79356b
A
2784 switch (ap->a_name) {
2785 case _PC_LINK_MAX:
2786 *ap->a_retval = LINK_MAX;
0a7de745 2787 return 0;
1c79356b
A
2788 case _PC_MAX_CANON:
2789 *ap->a_retval = MAX_CANON;
0a7de745 2790 return 0;
1c79356b
A
2791 case _PC_MAX_INPUT:
2792 *ap->a_retval = MAX_INPUT;
0a7de745 2793 return 0;
1c79356b
A
2794 case _PC_PIPE_BUF:
2795 *ap->a_retval = PIPE_BUF;
0a7de745 2796 return 0;
1c79356b 2797 case _PC_CHOWN_RESTRICTED:
0a7de745
A
2798 *ap->a_retval = 200112; /* _POSIX_CHOWN_RESTRICTED */
2799 return 0;
1c79356b
A
2800 case _PC_VDISABLE:
2801 *ap->a_retval = _POSIX_VDISABLE;
0a7de745 2802 return 0;
1c79356b 2803 default:
0a7de745 2804 return EINVAL;
1c79356b
A
2805 }
2806 /* NOTREACHED */
2807}
2808
1c79356b
A
2809/*
2810 * Special device failed operation
2811 */
91447636
A
2812int
2813spec_ebadf(__unused void *dummy)
1c79356b 2814{
0a7de745 2815 return EBADF;
1c79356b
A
2816}
2817
1c79356b
A
2818/* Blktooff derives file offset from logical block number */
2819int
2d21ac55 2820spec_blktooff(struct vnop_blktooff_args *ap)
1c79356b 2821{
2d21ac55 2822 struct vnode *vp = ap->a_vp;
1c79356b
A
2823
2824 switch (vp->v_type) {
2825 case VCHR:
2826 *ap->a_offset = (off_t)-1; /* failure */
0a7de745 2827 return ENOTSUP;
1c79356b
A
2828
2829 case VBLK:
2830 printf("spec_blktooff: not implemented for VBLK\n");
2831 *ap->a_offset = (off_t)-1; /* failure */
0a7de745 2832 return ENOTSUP;
1c79356b
A
2833
2834 default:
2835 panic("spec_blktooff type");
2836 }
2837 /* NOTREACHED */
91447636 2838
0a7de745 2839 return 0;
1c79356b
A
2840}
2841
2842/* Offtoblk derives logical block number from file offset */
2843int
2d21ac55 2844spec_offtoblk(struct vnop_offtoblk_args *ap)
1c79356b 2845{
2d21ac55 2846 struct vnode *vp = ap->a_vp;
1c79356b
A
2847
2848 switch (vp->v_type) {
2849 case VCHR:
91447636 2850 *ap->a_lblkno = (daddr64_t)-1; /* failure */
0a7de745 2851 return ENOTSUP;
1c79356b
A
2852
2853 case VBLK:
2854 printf("spec_offtoblk: not implemented for VBLK\n");
91447636 2855 *ap->a_lblkno = (daddr64_t)-1; /* failure */
0a7de745 2856 return ENOTSUP;
1c79356b
A
2857
2858 default:
2859 panic("spec_offtoblk type");
2860 }
2861 /* NOTREACHED */
91447636 2862
0a7de745 2863 return 0;
1c79356b 2864}
6d2010ae
A
2865
2866static void filt_specdetach(struct knote *kn);
5ba3f43e 2867static int filt_specevent(struct knote *kn, long hint);
cb323159
A
2868static int filt_spectouch(struct knote *kn, struct kevent_qos_s *kev);
2869static int filt_specprocess(struct knote *kn, struct kevent_qos_s *kev);
d9a64523 2870static int filt_specpeek(struct knote *kn);
6d2010ae 2871
5ba3f43e
A
2872SECURITY_READ_ONLY_EARLY(struct filterops) spec_filtops = {
2873 .f_isfd = 1,
2874 .f_attach = filt_specattach,
2875 .f_detach = filt_specdetach,
2876 .f_event = filt_specevent,
2877 .f_touch = filt_spectouch,
2878 .f_process = filt_specprocess,
2879 .f_peek = filt_specpeek
6d2010ae
A
2880};
2881
5ba3f43e
A
2882
2883/*
2884 * Given a waitq that is assumed to be embedded within a selinfo structure,
2885 * return the containing selinfo structure. While 'wq' is not really a queue
2886 * element, this macro simply does the offset_of calculation to get back to a
2887 * containing struct given the struct type and member name.
2888 */
2889#define selinfo_from_waitq(wq) \
2890 qe_element((wq), struct selinfo, si_waitq)
2891
6d2010ae 2892static int
5ba3f43e 2893spec_knote_select_and_link(struct knote *kn)
6d2010ae 2894{
5ba3f43e
A
2895 uthread_t uth;
2896 vfs_context_t ctx;
2897 vnode_t vp;
2898 struct waitq_set *old_wqs;
2899 uint64_t rsvd, rsvd_arg;
2900 uint64_t *rlptr = NULL;
2901 struct selinfo *si = NULL;
2902 int selres = 0;
2903
2904 uth = get_bsdthread_info(current_thread());
2905
2906 ctx = vfs_context_current();
2907 vp = (vnode_t)kn->kn_fp->f_fglob->fg_data;
2908
cb323159 2909 int error = vnode_getwithvid(vp, vnode_vid(vp));
5ba3f43e
A
2910 if (error != 0) {
2911 knote_set_error(kn, ENOENT);
6d2010ae
A
2912 return 0;
2913 }
5ba3f43e
A
2914
2915 /*
2916 * This function may be called many times to link or re-link the
2917 * underlying vnode to the kqueue. If we've already linked the two,
94ff46dc 2918 * we will have a valid kn_hook_waitqid which ties us to the underlying
5ba3f43e
A
2919 * device's waitq via a the waitq's prepost table object. However,
2920 * devices can abort any select action by calling selthreadclear().
2921 * This is OK because the table object will be invalidated by the
2922 * driver (through a call to selthreadclear), so any attempt to access
2923 * the associated waitq will fail because the table object is invalid.
2924 *
2925 * Even if we've already registered, we need to pass a pointer
2926 * to a reserved link structure. Otherwise, selrecord() will
2927 * infer that we're in the second pass of select() and won't
2928 * actually do anything!
2929 */
2930 rsvd = rsvd_arg = waitq_link_reserve(NULL);
2931 rlptr = (void *)&rsvd_arg;
2932
2933 /*
a39ff7e2
A
2934 * Trick selrecord() into hooking kqueue's wait queue set into the device's
2935 * selinfo wait queue.
5ba3f43e
A
2936 */
2937 old_wqs = uth->uu_wqset;
2938 uth->uu_wqset = &(knote_get_kq(kn)->kq_wqs);
d9a64523
A
2939
2940 /*
2941 * Be sure that the waitq set is linked
2942 * before calling select to avoid possible
2943 * allocation under spinlocks.
2944 */
2945 waitq_set_lazy_init_link(uth->uu_wqset);
2946
a39ff7e2
A
2947 /*
2948 * Now these are the laws of VNOP_SELECT, as old and as true as the sky,
2949 * And the device that shall keep it may prosper, but the device that shall
2950 * break it must receive ENODEV:
2951 *
2952 * 1. Take a lock to protect against other selects on the same vnode.
2953 * 2. Return 1 if data is ready to be read.
2954 * 3. Return 0 and call `selrecord` on a handy `selinfo` structure if there
2955 * is no data.
2956 * 4. Call `selwakeup` when the vnode has an active `selrecord` and data
2957 * can be read or written (depending on the seltype).
2958 * 5. If there's a `selrecord` and no corresponding `selwakeup`, but the
2959 * vnode is going away, call `selthreadclear`.
2960 */
5ba3f43e
A
2961 selres = VNOP_SELECT(vp, knote_get_seltype(kn), 0, rlptr, ctx);
2962 uth->uu_wqset = old_wqs;
2963
2964 /*
a39ff7e2 2965 * Make sure to cleanup the reserved link - this guards against
5ba3f43e
A
2966 * drivers that may not actually call selrecord().
2967 */
2968 waitq_link_release(rsvd);
2969 if (rsvd != rsvd_arg) {
a39ff7e2 2970 /* The driver / handler called selrecord() */
5ba3f43e
A
2971 struct waitq *wq;
2972 memcpy(&wq, rlptr, sizeof(void *));
2973
2974 /*
2975 * The waitq is part of the selinfo structure managed by the
2976 * driver. For certain drivers, we want to hook the knote into
2977 * the selinfo structure's si_note field so selwakeup can call
2978 * KNOTE.
2979 */
2980 si = selinfo_from_waitq(wq);
2981
2982 /*
2983 * The waitq_get_prepost_id() function will (potentially)
2984 * allocate a prepost table object for the waitq and return
2985 * the table object's ID to us. It will also set the
2986 * waitq_prepost_id field within the waitq structure.
2987 *
94ff46dc 2988 * We can just overwrite kn_hook_waitqid because it's simply a
5ba3f43e
A
2989 * table ID used to grab a reference when needed.
2990 *
2991 * We have a reference on the vnode, so we know that the
2992 * device won't go away while we get this ID.
94ff46dc
A
2993 *
2994 * Note: on 32bit this field is 32bit only.
5ba3f43e 2995 */
94ff46dc 2996 kn->kn_hook_waitqid = (typeof(kn->kn_hook_waitqid))waitq_get_prepost_id(wq);
a39ff7e2
A
2997 } else if (selres == 0) {
2998 /*
2999 * The device indicated that there's no data to read, but didn't call
3000 * `selrecord`. Nothing will be notified of changes to this vnode, so
3001 * return an error back to user space, to make it clear that the knote
3002 * is not attached.
3003 */
3004 knote_set_error(kn, ENODEV);
5ba3f43e
A
3005 }
3006
3007 vnode_put(vp);
3008
3009 return selres;
6d2010ae
A
3010}
3011
cb323159
A
3012static int
3013filt_spec_common(struct knote *kn, struct kevent_qos_s *kev, int selres)
5ba3f43e 3014{
cb323159
A
3015 int64_t data;
3016 int ret;
3017
5ba3f43e
A
3018 if (kn->kn_vnode_use_ofst) {
3019 if (kn->kn_fp->f_fglob->fg_offset >= (uint32_t)selres) {
cb323159 3020 data = 0;
5ba3f43e 3021 } else {
cb323159 3022 data = ((uint32_t)selres) - kn->kn_fp->f_fglob->fg_offset;
5ba3f43e
A
3023 }
3024 } else {
cb323159
A
3025 data = selres;
3026 }
3027
3028 ret = data >= knote_low_watermark(kn);
3029
3030 if (ret && kev) {
3031 knote_fill_kevent(kn, kev, data);
5ba3f43e 3032 }
cb323159
A
3033
3034 return ret;
5ba3f43e
A
3035}
3036
3037static int
cb323159 3038filt_specattach(struct knote *kn, __unused struct kevent_qos_s *kev)
6d2010ae
A
3039{
3040 vnode_t vp;
3041 dev_t dev;
3042
3043 vp = (vnode_t)kn->kn_fp->f_fglob->fg_data; /* Already have iocount, and vnode is alive */
3044
3045 assert(vnode_ischr(vp));
3046
3047 dev = vnode_specrdev(vp);
3048
3e170ce0
A
3049 /*
3050 * For a few special kinds of devices, we can attach knotes with
3051 * no restrictions because their "select" vectors return the amount
3052 * of data available. Others require an explicit NOTE_LOWAT with
3053 * data of 1, indicating that the caller doesn't care about actual
3054 * data counts, just an indication that the device has data.
3055 */
5ba3f43e 3056 if (!kn->kn_vnode_kqok &&
3e170ce0 3057 ((kn->kn_sfflags & NOTE_LOWAT) == 0 || kn->kn_sdata != 1)) {
5ba3f43e 3058 knote_set_error(kn, EINVAL);
39037602 3059 return 0;
6d2010ae
A
3060 }
3061
5ba3f43e
A
3062 /*
3063 * This forces the select fallback to call through VNOP_SELECT and hook
3064 * up selinfo on every filter routine.
3065 *
3066 * Pseudo-terminal controllers are opted out of native kevent support --
3067 * remove this when they get their own EVFILTID.
3068 */
3069 if (cdevsw_flags[major(dev)] & CDEVSW_IS_PTC) {
3070 kn->kn_vnode_kqok = 0;
3071 }
6d2010ae 3072
39037602 3073 kn->kn_filtid = EVFILTID_SPEC;
94ff46dc 3074 kn->kn_hook_waitqid = 0;
6d2010ae 3075
39037602 3076 knote_markstayactive(kn);
5ba3f43e 3077 return spec_knote_select_and_link(kn);
6d2010ae
A
3078}
3079
5ba3f43e 3080static void
6d2010ae
A
3081filt_specdetach(struct knote *kn)
3082{
39037602 3083 knote_clearstayactive(kn);
6d2010ae 3084
3e170ce0
A
3085 /*
3086 * This is potentially tricky: the device's selinfo waitq that was
3087 * tricked into being part of this knote's waitq set may not be a part
3088 * of any other set, and the device itself may have revoked the memory
94ff46dc 3089 * in which the waitq was held. We use the knote's kn_hook_waitqid field
3e170ce0
A
3090 * to keep the ID of the waitq's prepost table object. This
3091 * object keeps a pointer back to the waitq, and gives us a safe way
3092 * to decouple the dereferencing of driver allocated memory: if the
3093 * driver goes away (taking the waitq with it) then the prepost table
3094 * object will be invalidated. The waitq details are handled in the
3095 * waitq API invoked here.
6d2010ae 3096 */
94ff46dc
A
3097 if (kn->kn_hook_waitqid) {
3098 waitq_unlink_by_prepost_id(kn->kn_hook_waitqid, &(knote_get_kq(kn)->kq_wqs));
3099 kn->kn_hook_waitqid = 0;
6d2010ae 3100 }
6d2010ae
A
3101}
3102
5ba3f43e
A
3103static int
3104filt_specevent(struct knote *kn, __unused long hint)
6d2010ae 3105{
5ba3f43e
A
3106 /*
3107 * Nothing should call knote or knote_vanish on this knote.
3108 */
3109 panic("filt_specevent(%p)", kn);
39037602
A
3110 return 0;
3111}
3112
39037602 3113static int
cb323159 3114filt_spectouch(struct knote *kn, struct kevent_qos_s *kev)
39037602
A
3115{
3116 kn->kn_sdata = kev->data;
3117 kn->kn_sfflags = kev->fflags;
39037602 3118
5ba3f43e
A
3119 if (kev->flags & EV_ENABLE) {
3120 return spec_knote_select_and_link(kn);
3121 }
3122
39037602
A
3123 return 0;
3124}
3125
3126static int
cb323159 3127filt_specprocess(struct knote *kn, struct kevent_qos_s *kev)
39037602 3128{
6d2010ae
A
3129 vnode_t vp;
3130 uthread_t uth;
6d2010ae 3131 vfs_context_t ctx;
39037602 3132 int res;
6d2010ae
A
3133 int selres;
3134 int error;
6d2010ae 3135
6d2010ae
A
3136 uth = get_bsdthread_info(current_thread());
3137 ctx = vfs_context_current();
3138 vp = (vnode_t)kn->kn_fp->f_fglob->fg_data;
3139
cb323159 3140 error = vnode_getwithvid(vp, vnode_vid(vp));
6d2010ae
A
3141 if (error != 0) {
3142 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
cb323159 3143 knote_fill_kevent(kn, kev, 0);
6d2010ae
A
3144 return 1;
3145 }
6d2010ae 3146
5ba3f43e 3147 selres = spec_knote_select_and_link(kn);
cb323159 3148 res = filt_spec_common(kn, kev, selres);
6d2010ae
A
3149
3150 vnode_put(vp);
3151
39037602 3152 return res;
6d2010ae
A
3153}
3154
d9a64523 3155static int
6d2010ae
A
3156filt_specpeek(struct knote *kn)
3157{
5ba3f43e 3158 int selres = 0;
3e170ce0 3159
5ba3f43e 3160 selres = spec_knote_select_and_link(kn);
cb323159 3161 return filt_spec_common(kn, NULL, selres);
6d2010ae 3162}