Sync ACPICA with Intel's version 20161222.
[dragonfly.git] / sys / vm / vm_swap.c
blobc4c8fe0db1c1cf9ce4f619bb77220c9ab4d27a87
1 /*
2 * (MPSAFE)
4 * Copyright (c) 1982, 1986, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
31 * @(#)vm_swap.c 8.5 (Berkeley) 2/17/94
32 * $FreeBSD: src/sys/vm/vm_swap.c,v 1.96.2.2 2001/10/14 18:46:47 iedowse Exp $
35 #include "opt_swap.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/sysproto.h>
40 #include <sys/buf.h>
41 #include <sys/proc.h>
42 #include <sys/priv.h>
43 #include <sys/nlookup.h>
44 #include <sys/sysctl.h>
45 #include <sys/dmap.h> /* XXX */
46 #include <sys/vnode.h>
47 #include <sys/fcntl.h>
48 #include <sys/blist.h>
49 #include <sys/kernel.h>
50 #include <sys/lock.h>
51 #include <sys/conf.h>
52 #include <sys/stat.h>
54 #include <vm/vm.h>
55 #include <vm/vm_extern.h>
56 #include <vm/swap_pager.h>
57 #include <vm/vm_zone.h>
58 #include <vm/vm_param.h>
60 #include <sys/thread2.h>
61 #include <sys/mplock2.h>
62 #include <sys/mutex2.h>
63 #include <sys/spinlock2.h>
66 * Indirect driver for multi-controller paging.
69 #ifndef NSWAPDEV
70 #define NSWAPDEV 4
71 #endif
72 static struct swdevt should_be_malloced[NSWAPDEV];
73 struct swdevt *swdevt = should_be_malloced; /* exported to pstat/systat */
74 static swblk_t nswap; /* first block after the interleaved devs */
75 static struct mtx swap_mtx = MTX_INITIALIZER("swpmtx");
76 int nswdev = NSWAPDEV; /* exported to pstat/systat */
77 swblk_t vm_swap_size;
78 swblk_t vm_swap_max;
80 static int swapoff_one(int index);
81 struct vnode *swapdev_vp;
84 * (struct vnode *a_vp, struct bio *b_bio)
86 * vn_strategy() for swapdev_vp. Perform swap strategy interleave device
87 * selection.
89 * No requirements.
91 static int
92 swapdev_strategy(struct vop_strategy_args *ap)
94 struct bio *bio = ap->a_bio;
95 struct bio *nbio;
96 struct buf *bp = bio->bio_buf;
97 swblk_t sz, off, seg, blkno, nblkno;
98 int index;
99 struct swdevt *sp;
100 sz = howmany(bp->b_bcount, PAGE_SIZE);
101 blkno = (swblk_t)(bio->bio_offset >> PAGE_SHIFT);
104 * Convert interleaved swap into per-device swap. Note that
105 * the block size is left in PAGE_SIZE'd chunks (for the newswap)
106 * here.
108 nbio = push_bio(bio);
109 if (nswdev > 1) {
110 off = blkno % SWB_DMMAX;
111 if (off + sz > SWB_DMMAX) {
112 bp->b_error = EINVAL;
113 bp->b_flags |= B_ERROR;
114 biodone(bio);
115 return 0;
117 seg = blkno / SWB_DMMAX;
118 index = seg % nswdev;
119 seg /= nswdev;
120 nbio->bio_offset = (off_t)(seg * SWB_DMMAX + off) << PAGE_SHIFT;
121 } else {
122 index = 0;
123 nbio->bio_offset = bio->bio_offset;
125 nblkno = (swblk_t)(nbio->bio_offset >> PAGE_SHIFT);
126 sp = &swdevt[index];
127 if (nblkno + sz > sp->sw_nblks) {
128 bp->b_error = EINVAL;
129 bp->b_flags |= B_ERROR;
130 /* I/O was never started on nbio, must biodone(bio) */
131 biodone(bio);
132 return 0;
134 if (sp->sw_vp == NULL) {
135 bp->b_error = ENODEV;
136 bp->b_flags |= B_ERROR;
137 /* I/O was never started on nbio, must biodone(bio) */
138 biodone(bio);
139 return 0;
143 * Issue a strategy call on the appropriate swap vnode. Note that
144 * bp->b_vp is not modified. Strategy code is always supposed to
145 * use the passed vp.
147 * We have to use vn_strategy() here even if we know we have a
148 * device in order to properly break up requests which exceed the
149 * device's DMA limits.
151 vn_strategy(sp->sw_vp, nbio);
152 return 0;
155 static int
156 swapdev_inactive(struct vop_inactive_args *ap)
158 vrecycle(ap->a_vp);
159 return(0);
162 static int
163 swapdev_reclaim(struct vop_reclaim_args *ap)
165 return(0);
169 * Create a special vnode op vector for swapdev_vp - we only use
170 * vn_strategy(), everything else returns an error.
172 static struct vop_ops swapdev_vnode_vops = {
173 .vop_default = vop_defaultop,
174 .vop_strategy = swapdev_strategy,
175 .vop_inactive = swapdev_inactive,
176 .vop_reclaim = swapdev_reclaim
178 static struct vop_ops *swapdev_vnode_vops_p = &swapdev_vnode_vops;
180 VNODEOP_SET(swapdev_vnode_vops);
183 * swapon_args(char *name)
185 * System call swapon(name) enables swapping on device name,
186 * which must be in the swdevsw. Return EBUSY
187 * if already swapping on this device.
189 * No requirements.
192 sys_swapon(struct swapon_args *uap)
194 struct thread *td = curthread;
195 struct vattr attr;
196 struct vnode *vp;
197 struct nlookupdata nd;
198 int error;
200 error = priv_check(td, PRIV_ROOT);
201 if (error)
202 return (error);
204 mtx_lock(&swap_mtx);
205 get_mplock();
206 vp = NULL;
207 error = nlookup_init(&nd, uap->name, UIO_USERSPACE, NLC_FOLLOW);
208 if (error == 0)
209 error = nlookup(&nd);
210 if (error == 0)
211 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
212 nlookup_done(&nd);
213 if (error) {
214 rel_mplock();
215 mtx_unlock(&swap_mtx);
216 return (error);
219 if (vn_isdisk(vp, &error)) {
220 error = swaponvp(td, vp, 0);
221 } else if (vp->v_type == VREG && vp->v_tag == VT_NFS &&
222 (error = VOP_GETATTR(vp, &attr)) == 0) {
224 * Allow direct swapping to NFS regular files in the same
225 * way that nfs_mountroot() sets up diskless swapping.
227 error = swaponvp(td, vp, attr.va_size / DEV_BSIZE);
229 if (error)
230 vrele(vp);
231 rel_mplock();
232 mtx_unlock(&swap_mtx);
234 return (error);
238 * Swfree(index) frees the index'th portion of the swap map.
239 * Each of the nswdev devices provides 1/nswdev'th of the swap
240 * space, which is laid out with blocks of SWB_DMMAX pages circularly
241 * among the devices.
243 * The new swap code uses page-sized blocks. The old swap code used
244 * DEV_BSIZE'd chunks.
246 * XXX locking when multiple swapon's run in parallel
249 swaponvp(struct thread *td, struct vnode *vp, u_quad_t nblks)
251 swblk_t aligned_nblks;
252 int64_t dpsize;
253 struct ucred *cred;
254 struct swdevt *sp;
255 swblk_t vsbase;
256 swblk_t dvbase;
257 cdev_t dev;
258 int index;
259 int error;
260 swblk_t blk;
262 cred = td->td_ucred;
264 lwkt_gettoken(&vm_token); /* needed for vm_swap_size and blist */
265 mtx_lock(&swap_mtx);
267 if (!swapdev_vp) {
268 error = getspecialvnode(VT_NON, NULL, &swapdev_vnode_vops_p,
269 &swapdev_vp, 0, 0);
270 if (error)
271 panic("Cannot get vnode for swapdev");
272 swapdev_vp->v_type = VNON; /* Untyped */
273 vx_unlock(swapdev_vp);
276 for (sp = swdevt, index = 0 ; index < nswdev; index++, sp++) {
277 if (sp->sw_vp == vp) {
278 error = EBUSY;
279 goto done;
281 if (!sp->sw_vp)
282 goto found;
285 error = EINVAL;
286 goto done;
287 found:
288 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
289 error = VOP_OPEN(vp, FREAD | FWRITE, cred, NULL);
290 vn_unlock(vp);
291 if (error)
292 goto done;
295 * v_rdev is not valid until after the VOP_OPEN() call. dev_psize()
296 * must be supported if a character device has been specified.
298 if (vp->v_type == VCHR)
299 dev = vp->v_rdev;
300 else
301 dev = NULL;
303 if (nblks == 0 && dev != NULL) {
304 dpsize = dev_dpsize(dev);
305 if (dpsize == -1) {
306 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
307 VOP_CLOSE(vp, FREAD | FWRITE, NULL);
308 vn_unlock(vp);
309 error = ENXIO;
310 goto done;
312 nblks = (u_quad_t)dpsize;
314 if (nblks == 0) {
315 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
316 VOP_CLOSE(vp, FREAD | FWRITE, NULL);
317 vn_unlock(vp);
318 error = ENXIO;
319 goto done;
323 * nblks is in DEV_BSIZE'd chunks, convert to PAGE_SIZE'd chunks.
324 * First chop nblks off to page-align it, then convert.
326 * sw->sw_nblks is in page-sized chunks now too.
328 nblks &= ~(u_quad_t)(ctodb(1) - 1);
329 nblks = dbtoc(nblks);
332 * Post-conversion nblks must not be >= BLIST_MAXBLKS, and
333 * we impose a 4-swap-device limit so we have to divide it out
334 * further. Going beyond this will result in overflows in the
335 * blist code.
337 * Post-conversion nblks must fit within a (swblk_t), which
338 * this test also ensures.
340 if (nblks > BLIST_MAXBLKS / nswdev) {
341 kprintf("exceeded maximum of %ld blocks per swap unit\n",
342 (long)BLIST_MAXBLKS / nswdev);
343 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
344 VOP_CLOSE(vp, FREAD | FWRITE, NULL);
345 vn_unlock(vp);
346 error = ENXIO;
347 goto done;
350 sp->sw_vp = vp;
351 sp->sw_dev = dev2udev(dev);
352 sp->sw_device = dev;
353 sp->sw_flags = SW_FREED;
354 sp->sw_nused = 0;
357 * nblks, nswap, and SWB_DMMAX are PAGE_SIZE'd parameters now, not
358 * DEV_BSIZE'd. aligned_nblks is used to calculate the
359 * size of the swap bitmap, taking into account the stripe size.
361 aligned_nblks = (swblk_t)((nblks + SWB_DMMASK) &
362 ~(u_swblk_t)SWB_DMMASK);
363 sp->sw_nblks = aligned_nblks;
365 if (aligned_nblks * nswdev > nswap)
366 nswap = aligned_nblks * nswdev;
368 if (swapblist == NULL)
369 swapblist = blist_create(nswap);
370 else
371 blist_resize(&swapblist, nswap, 0);
373 for (dvbase = SWB_DMMAX; dvbase < aligned_nblks; dvbase += SWB_DMMAX) {
374 blk = min(aligned_nblks - dvbase, SWB_DMMAX);
375 vsbase = index * SWB_DMMAX + dvbase * nswdev;
376 blist_free(swapblist, vsbase, blk);
377 vm_swap_size += blk;
378 vm_swap_max += blk;
380 swap_pager_newswap();
381 error = 0;
382 done:
383 mtx_unlock(&swap_mtx);
384 lwkt_reltoken(&vm_token);
385 return (error);
389 * swapoff_args(char *name)
391 * System call swapoff(name) disables swapping on device name,
392 * which must be an active swap device. Return ENOMEM
393 * if there is not enough memory to page in the contents of
394 * the given device.
396 * No requirements.
399 sys_swapoff(struct swapoff_args *uap)
401 struct vnode *vp;
402 struct nlookupdata nd;
403 struct swdevt *sp;
404 int error, index;
406 error = priv_check(curthread, PRIV_ROOT);
407 if (error)
408 return (error);
410 mtx_lock(&swap_mtx);
411 get_mplock();
412 vp = NULL;
413 error = nlookup_init(&nd, uap->name, UIO_USERSPACE, NLC_FOLLOW);
414 if (error == 0)
415 error = nlookup(&nd);
416 if (error == 0)
417 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
418 nlookup_done(&nd);
419 if (error)
420 goto done;
422 for (sp = swdevt, index = 0; index < nswdev; index++, sp++) {
423 if (sp->sw_vp == vp)
424 goto found;
426 error = EINVAL;
427 goto done;
428 found:
429 error = swapoff_one(index);
430 swap_pager_newswap();
432 done:
433 rel_mplock();
434 mtx_unlock(&swap_mtx);
435 return (error);
438 static int
439 swapoff_one(int index)
441 swblk_t blk, aligned_nblks;
442 swblk_t dvbase, vsbase;
443 u_int pq_active_clean, pq_inactive_clean;
444 struct swdevt *sp;
445 struct vm_page marker;
446 vm_page_t m;
447 int q;
449 mtx_lock(&swap_mtx);
451 sp = &swdevt[index];
452 aligned_nblks = sp->sw_nblks;
453 pq_active_clean = pq_inactive_clean = 0;
456 * We can turn off this swap device safely only if the
457 * available virtual memory in the system will fit the amount
458 * of data we will have to page back in, plus an epsilon so
459 * the system doesn't become critically low on swap space.
461 for (q = 0; q < PQ_L2_SIZE; ++q) {
462 bzero(&marker, sizeof(marker));
463 marker.flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER;
464 marker.queue = PQ_ACTIVE + q;
465 marker.pc = q;
466 marker.wire_count = 1;
468 vm_page_queues_spin_lock(marker.queue);
469 TAILQ_INSERT_HEAD(&vm_page_queues[marker.queue].pl,
470 &marker, pageq);
472 while ((m = TAILQ_NEXT(&marker, pageq)) != NULL) {
473 TAILQ_REMOVE(&vm_page_queues[marker.queue].pl,
474 &marker, pageq);
475 TAILQ_INSERT_AFTER(&vm_page_queues[marker.queue].pl, m,
476 &marker, pageq);
477 if (m->flags & (PG_MARKER | PG_FICTITIOUS))
478 continue;
480 if (vm_page_busy_try(m, FALSE) == 0) {
481 vm_page_queues_spin_unlock(marker.queue);
482 if (m->dirty == 0) {
483 vm_page_test_dirty(m);
484 if (m->dirty == 0)
485 ++pq_active_clean;
487 vm_page_wakeup(m);
488 vm_page_queues_spin_lock(marker.queue);
491 TAILQ_REMOVE(&vm_page_queues[marker.queue].pl, &marker, pageq);
492 vm_page_queues_spin_unlock(marker.queue);
494 marker.queue = PQ_INACTIVE + q;
495 marker.pc = q;
496 vm_page_queues_spin_lock(marker.queue);
497 TAILQ_INSERT_HEAD(&vm_page_queues[marker.queue].pl,
498 &marker, pageq);
500 while ((m = TAILQ_NEXT(&marker, pageq)) != NULL) {
501 TAILQ_REMOVE(
502 &vm_page_queues[marker.queue].pl,
503 &marker, pageq);
504 TAILQ_INSERT_AFTER(
505 &vm_page_queues[marker.queue].pl,
506 m, &marker, pageq);
507 if (m->flags & (PG_MARKER | PG_FICTITIOUS))
508 continue;
510 if (vm_page_busy_try(m, FALSE) == 0) {
511 vm_page_queues_spin_unlock(marker.queue);
512 if (m->dirty == 0) {
513 vm_page_test_dirty(m);
514 if (m->dirty == 0)
515 ++pq_inactive_clean;
517 vm_page_wakeup(m);
518 vm_page_queues_spin_lock(marker.queue);
521 TAILQ_REMOVE(&vm_page_queues[marker.queue].pl,
522 &marker, pageq);
523 vm_page_queues_spin_unlock(marker.queue);
526 if (vmstats.v_free_count + vmstats.v_cache_count + pq_active_clean +
527 pq_inactive_clean + vm_swap_size < aligned_nblks + nswap_lowat) {
528 mtx_unlock(&swap_mtx);
529 return (ENOMEM);
533 * Prevent further allocations on this device
535 sp->sw_flags |= SW_CLOSING;
536 for (dvbase = SWB_DMMAX; dvbase < aligned_nblks; dvbase += SWB_DMMAX) {
537 blk = min(aligned_nblks - dvbase, SWB_DMMAX);
538 vsbase = index * SWB_DMMAX + dvbase * nswdev;
539 vm_swap_size -= blist_fill(swapblist, vsbase, blk);
540 vm_swap_max -= blk;
544 * Page in the contents of the device and close it.
546 if (swap_pager_swapoff(index) && swap_pager_swapoff(index)) {
547 mtx_unlock(&swap_mtx);
548 return (EINTR);
551 vn_lock(sp->sw_vp, LK_EXCLUSIVE | LK_RETRY);
552 VOP_CLOSE(sp->sw_vp, FREAD | FWRITE, NULL);
553 vn_unlock(sp->sw_vp);
554 vrele(sp->sw_vp);
555 bzero(swdevt + index, sizeof(struct swdevt));
558 * Resize the bitmap based on the nem largest swap device,
559 * or free the bitmap if there are no more devices.
561 for (sp = swdevt, aligned_nblks = 0; sp < swdevt + nswdev; sp++) {
562 if (sp->sw_vp)
563 aligned_nblks = max(aligned_nblks, sp->sw_nblks);
566 nswap = aligned_nblks * nswdev;
568 if (nswap == 0) {
569 blist_destroy(swapblist);
570 swapblist = NULL;
571 vrele(swapdev_vp);
572 swapdev_vp = NULL;
573 } else {
574 blist_resize(&swapblist, nswap, 0);
577 mtx_unlock(&swap_mtx);
578 return (0);
582 * Account for swap space in individual swdevt's. The caller ensures
583 * that the provided range falls into a single swdevt.
585 * +count space freed
586 * -count space allocated
588 void
589 swapacctspace(swblk_t base, swblk_t count)
591 int index;
592 swblk_t seg;
594 vm_swap_size += count;
595 seg = base / SWB_DMMAX;
596 index = seg % nswdev;
597 swdevt[index].sw_nused -= count;
601 * Retrieve swap info
603 static int
604 sysctl_vm_swap_info(SYSCTL_HANDLER_ARGS)
606 struct xswdev xs;
607 struct swdevt *sp;
608 int error;
609 int n;
611 error = 0;
612 for (n = 0; n < nswdev; ++n) {
613 sp = &swdevt[n];
615 xs.xsw_size = sizeof(xs);
616 xs.xsw_version = XSWDEV_VERSION;
617 xs.xsw_blksize = PAGE_SIZE;
618 xs.xsw_dev = sp->sw_dev;
619 xs.xsw_flags = sp->sw_flags;
620 xs.xsw_nblks = sp->sw_nblks;
621 xs.xsw_used = sp->sw_nused;
623 error = SYSCTL_OUT(req, &xs, sizeof(xs));
624 if (error)
625 break;
627 return (error);
630 SYSCTL_INT(_vm, OID_AUTO, nswapdev, CTLFLAG_RD, &nswdev, 0,
631 "Number of swap devices");
632 SYSCTL_NODE(_vm, OID_AUTO, swap_info_array, CTLFLAG_RD, sysctl_vm_swap_info,
633 "Swap statistics by device");