Merge branch 'vendor/WPA_SUPPLICANT'
[dragonfly.git] / sys / vm / vm_pager.c
1 /*
2  * (MPSAFE)
3  *
4  * Copyright (c) 1991, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * The Mach Operating System project at Carnegie-Mellon University.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *      from: @(#)vm_pager.c    8.6 (Berkeley) 1/12/94
35  *
36  *
37  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
38  * All rights reserved.
39  *
40  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
41  *
42  * Permission to use, copy, modify and distribute this software and
43  * its documentation is hereby granted, provided that both the copyright
44  * notice and this permission notice appear in all copies of the
45  * software, derivative works or modified versions, and any portions
46  * thereof, and that both notices appear in supporting documentation.
47  *
48  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
50  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
51  *
52  * Carnegie Mellon requests users of this software to return to
53  *
54  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
55  *  School of Computer Science
56  *  Carnegie Mellon University
57  *  Pittsburgh PA 15213-3890
58  *
59  * any improvements or extensions that they make and grant Carnegie the
60  * rights to redistribute these changes.
61  *
62  * $FreeBSD: src/sys/vm/vm_pager.c,v 1.54.2.2 2001/11/18 07:11:00 dillon Exp $
63  */
64
65 /*
66  *      Paging space routine stubs.  Emulates a matchmaker-like interface
67  *      for builtin pagers.
68  */
69
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/kernel.h>
73 #include <sys/vnode.h>
74 #include <sys/buf.h>
75 #include <sys/ucred.h>
76 #include <sys/dsched.h>
77 #include <sys/proc.h>
78 #include <sys/sysctl.h>
79
80 #include <vm/vm.h>
81 #include <vm/vm_param.h>
82 #include <vm/vm_kern.h>
83 #include <vm/vm_object.h>
84 #include <vm/vm_page.h>
85 #include <vm/vm_pager.h>
86 #include <vm/vm_extern.h>
87
88 #include <sys/buf2.h>
89 #include <vm/vm_page2.h>
90
91 static  pgo_dealloc_t           dead_pager_dealloc;
92 static  pgo_getpage_t           dead_pager_getpage;
93 static  pgo_putpages_t          dead_pager_putpages;
94 static  pgo_haspage_t           dead_pager_haspage;
95
96 static struct pagerops deadpagerops = {
97         .pgo_dealloc =          dead_pager_dealloc,
98         .pgo_getpage =          dead_pager_getpage,
99         .pgo_putpages =         dead_pager_putpages,
100         .pgo_haspage =          dead_pager_haspage
101 };
102
103 extern struct pagerops defaultpagerops;
104 extern struct pagerops swappagerops;
105 extern struct pagerops vnodepagerops;
106 extern struct pagerops devicepagerops;
107 extern struct pagerops physpagerops;
108
109 /*
110  * No requirements.
111  */
112 static int
113 dead_pager_getpage(vm_object_t obj, vm_pindex_t pindex,
114                    vm_page_t *mpp, int seqaccess)
115 {
116         return VM_PAGER_FAIL;
117 }
118
119 /*
120  * No requirements.
121  */
122 static void
123 dead_pager_putpages(vm_object_t object, vm_page_t *m, int count, int flags,
124                     int *rtvals)
125 {
126         int i;
127
128         for (i = 0; i < count; i++) {
129                 rtvals[i] = VM_PAGER_AGAIN;
130         }
131 }
132
133 /*
134  * No requirements.
135  */
136 static boolean_t
137 dead_pager_haspage(vm_object_t object, vm_pindex_t pindex)
138 {
139         return FALSE;
140 }
141
142 /*
143  * No requirements.
144  */
145 static void
146 dead_pager_dealloc(vm_object_t object)
147 {
148         KKASSERT(object->swblock_count == 0);
149         return;
150 }
151
152 struct pagerops *pagertab[] = {
153         &defaultpagerops,       /* OBJT_DEFAULT */
154         &swappagerops,          /* OBJT_SWAP */
155         &vnodepagerops,         /* OBJT_VNODE */
156         &devicepagerops,        /* OBJT_DEVICE */
157         &devicepagerops,        /* OBJT_MGTDEVICE */
158         &physpagerops,          /* OBJT_PHYS */
159         &deadpagerops           /* OBJT_DEAD */
160 };
161
162 int npagers = NELEM(pagertab);
163
164 /*
165  * Kernel address space for mapping pages.
166  * Used by pagers where KVAs are needed for IO.
167  *
168  * XXX needs to be large enough to support the number of pending async
169  * cleaning requests (NPENDINGIO == 64) * the maximum swap cluster size
170  * (MAXPHYS == 64k) if you want to get the most efficiency.
171  */
172 #define PAGER_MAP_SIZE  (8 * 1024 * 1024)
173
174 #define BSWHSIZE        16
175 #define BSWHMASK        (BSWHSIZE - 1)
176
177 TAILQ_HEAD(swqueue, buf);
178
179 int pager_map_size = PAGER_MAP_SIZE;
180
181 static struct vm_map pager_map_store;
182 struct vm_map *pager_map = &pager_map_store;
183
184 static vm_offset_t swapbkva_mem;        /* swap buffers kva */
185 static vm_offset_t swapbkva_kva;        /* swap buffers kva */
186 static struct swqueue bswlist_mem[BSWHSIZE];    /* with preallocated memory */
187 static struct swqueue bswlist_kva[BSWHSIZE];    /* with kva */
188 static struct swqueue bswlist_raw[BSWHSIZE];    /* without kva */
189 static struct spinlock bswspin_mem[BSWHSIZE];
190 static struct spinlock bswspin_kva[BSWHSIZE];
191 static struct spinlock bswspin_raw[BSWHSIZE];
192 static int pbuf_raw_count;
193 static int pbuf_kva_count;
194 static int pbuf_mem_count;
195
196 SYSCTL_INT(_vm, OID_AUTO, pbuf_raw_count, CTLFLAG_RD, &pbuf_raw_count, 0,
197     "Kernel pbuf raw reservations");
198 SYSCTL_INT(_vm, OID_AUTO, pbuf_kva_count, CTLFLAG_RD, &pbuf_kva_count, 0,
199     "Kernel pbuf kva reservations");
200 SYSCTL_INT(_vm, OID_AUTO, pbuf_mem_count, CTLFLAG_RD, &pbuf_mem_count, 0,
201     "Kernel pbuf mem reservations");
202
203 /*
204  * Initialize the swap buffer list.
205  *
206  * Called from the low level boot code only.
207  */
208 static void
209 vm_pager_init(void *arg __unused)
210 {
211         int i;
212
213         for (i = 0; i < BSWHSIZE; ++i) {
214                 TAILQ_INIT(&bswlist_mem[i]);
215                 TAILQ_INIT(&bswlist_kva[i]);
216                 TAILQ_INIT(&bswlist_raw[i]);
217                 spin_init(&bswspin_mem[i], "bswmem");
218                 spin_init(&bswspin_kva[i], "bswkva");
219                 spin_init(&bswspin_raw[i], "bswraw");
220         }
221 }
222 SYSINIT(vm_mem, SI_BOOT1_VM, SI_ORDER_SECOND, vm_pager_init, NULL);
223
224 /*
225  * Called from the low level boot code only.
226  */
227 static
228 void
229 vm_pager_bufferinit(void *dummy __unused)
230 {
231         struct buf *bp;
232         long i;
233
234         /*
235          * Reserve KVM space for pbuf data.
236          */
237         swapbkva_mem = kmem_alloc_pageable(pager_map, nswbuf_mem * MAXPHYS,
238                                            VM_SUBSYS_BUFDATA);
239         if (!swapbkva_mem)
240                 panic("Not enough pager_map VM space for physical buffers");
241         swapbkva_kva = kmem_alloc_pageable(pager_map, nswbuf_kva * MAXPHYS,
242                                            VM_SUBSYS_BUFDATA);
243         if (!swapbkva_kva)
244                 panic("Not enough pager_map VM space for physical buffers");
245
246         /*
247          * Initial pbuf setup.
248          *
249          * mem - These pbufs have permanently allocated memory
250          * kva - These pbufs have unallocated kva reservations
251          * raw - These pbufs have no kva reservations
252          */
253
254         /*
255          * Buffers with pre-allocated kernel memory can be convenient for
256          * copyin/copyout because no SMP page invalidation or other pmap
257          * operations are needed.
258          */
259         bp = swbuf_mem;
260         for (i = 0; i < nswbuf_mem; ++i, ++bp) {
261                 vm_page_t m;
262                 vm_pindex_t pg;
263                 int j;
264
265                 bp->b_kvabase = (caddr_t)((intptr_t)i * MAXPHYS) + swapbkva_mem;
266                 bp->b_kvasize = MAXPHYS;
267                 bp->b_swindex = i & BSWHMASK;
268                 bp->b_cpumask = smp_active_mask;
269                 BUF_LOCKINIT(bp);
270                 buf_dep_init(bp);
271                 TAILQ_INSERT_HEAD(&bswlist_mem[i & BSWHMASK], bp, b_freelist);
272                 atomic_add_int(&pbuf_mem_count, 1);
273                 bp->b_data = bp->b_kvabase;
274                 bp->b_bcount = MAXPHYS;
275                 bp->b_xio.xio_pages = bp->b_xio.xio_internal_pages;
276
277                 pg = (vm_offset_t)bp->b_kvabase >> PAGE_SHIFT;
278                 vm_object_hold(kernel_object);
279                 for (j = 0; j < MAXPHYS / PAGE_SIZE; ++j) {
280                         m = vm_page_alloc(kernel_object, pg, VM_ALLOC_NORMAL |
281                                                               VM_ALLOC_SYSTEM);
282                         KKASSERT(m != NULL);
283                         bp->b_xio.xio_internal_pages[j] = m;
284                         vm_page_wire(m);
285                         /* early boot, no other cpus running yet */
286                         pmap_kenter_noinval(pg * PAGE_SIZE, VM_PAGE_TO_PHYS(m));
287                         cpu_invlpg((void *)(pg * PAGE_SIZE));
288                         vm_page_wakeup(m);
289                         ++pg;
290                 }
291                 vm_object_drop(kernel_object);
292                 bp->b_xio.xio_npages = j;
293         }
294
295         /*
296          * Buffers with pre-assigned KVA bases.  The KVA has no memory pages
297          * assigned to it.  Saves the caller from having to reserve KVA for
298          * the page map.
299          */
300         bp = swbuf_kva;
301         for (i = 0; i < nswbuf_kva; ++i, ++bp) {
302                 bp->b_kvabase = (caddr_t)((intptr_t)i * MAXPHYS) + swapbkva_kva;
303                 bp->b_kvasize = MAXPHYS;
304                 bp->b_swindex = i & BSWHMASK;
305                 BUF_LOCKINIT(bp);
306                 buf_dep_init(bp);
307                 TAILQ_INSERT_HEAD(&bswlist_kva[i & BSWHMASK], bp, b_freelist);
308                 atomic_add_int(&pbuf_kva_count, 1);
309         }
310
311         /*
312          * RAW buffers with no KVA mappings.
313          *
314          * NOTE: We use KM_NOTLBSYNC here to reduce unnecessary IPIs
315          *       during startup, which can really slow down emulated
316          *       systems.
317          */
318         nswbuf_raw = nbuf * 2;
319         swbuf_raw = (void *)kmem_alloc3(kernel_map,
320                                 round_page(nswbuf_raw * sizeof(struct buf)),
321                                 VM_SUBSYS_BUFDATA,
322                                 KM_NOTLBSYNC);
323         smp_invltlb();
324         bp = swbuf_raw;
325         for (i = 0; i < nswbuf_raw; ++i, ++bp) {
326                 bp->b_swindex = i & BSWHMASK;
327                 BUF_LOCKINIT(bp);
328                 buf_dep_init(bp);
329                 TAILQ_INSERT_HEAD(&bswlist_raw[i & BSWHMASK], bp, b_freelist);
330                 atomic_add_int(&pbuf_raw_count, 1);
331         }
332 }
333
334 SYSINIT(do_vmpg, SI_BOOT2_MACHDEP, SI_ORDER_FIRST, vm_pager_bufferinit, NULL);
335
336 /*
337  * No requirements.
338  */
339 void
340 vm_pager_deallocate(vm_object_t object)
341 {
342         (*pagertab[object->type]->pgo_dealloc) (object);
343 }
344
345 /*
346  * vm_pager_get_pages() - inline, see vm/vm_pager.h
347  * vm_pager_put_pages() - inline, see vm/vm_pager.h
348  * vm_pager_has_page() - inline, see vm/vm_pager.h
349  * vm_pager_page_inserted() - inline, see vm/vm_pager.h
350  * vm_pager_page_removed() - inline, see vm/vm_pager.h
351  */
352
353 /*
354  * Search the specified pager object list for an object with the
355  * specified handle.  If an object with the specified handle is found,
356  * increase its reference count and return it.  Otherwise, return NULL.
357  *
358  * The pager object list must be locked.
359  */
360 vm_object_t
361 vm_pager_object_lookup(struct pagerlst *pg_list, void *handle)
362 {
363         vm_object_t object;
364
365         TAILQ_FOREACH(object, pg_list, pager_object_entry) {
366                 if (object->handle == handle) {
367                         VM_OBJECT_LOCK(object);
368                         if ((object->flags & OBJ_DEAD) == 0) {
369                                 vm_object_reference_locked(object);
370                                 VM_OBJECT_UNLOCK(object);
371                                 break;
372                         }
373                         VM_OBJECT_UNLOCK(object);
374                 }
375         }
376         return (object);
377 }
378
379 /*
380  * Initialize a physical buffer.
381  *
382  * No requirements.
383  */
384 static void
385 initpbuf(struct buf *bp)
386 {
387         bp->b_qindex = 0;               /* BQUEUE_NONE */
388         bp->b_data = bp->b_kvabase;     /* NULL if pbuf sans kva */
389         bp->b_flags = B_PAGING;
390         bp->b_cmd = BUF_CMD_DONE;
391         bp->b_error = 0;
392         bp->b_bcount = 0;
393         bp->b_bufsize = MAXPHYS;
394         initbufbio(bp);
395         xio_init(&bp->b_xio);
396         BUF_LOCK(bp, LK_EXCLUSIVE);
397 }
398
399 /*
400  * Allocate a physical buffer
401  *
402  * If (pfreecnt != NULL) then *pfreecnt will be decremented on return and
403  * the function will block while it is <= 0.
404  *
405  * Physical buffers can be with or without KVA space reserved.  There
406  * are severe limitations on the ones with KVA reserved, and fewer
407  * limitations on the ones without.  getpbuf() gets one without,
408  * getpbuf_kva() gets one with.
409  *
410  * No requirements.
411  */
412 struct buf *
413 getpbuf(int *pfreecnt)
414 {
415         struct buf *bp;
416         int iter;
417         int loops;
418
419         for (;;) {
420                 while (pfreecnt && *pfreecnt <= 0) {
421                         tsleep_interlock(pfreecnt, 0);
422                         if ((int)atomic_fetchadd_int(pfreecnt, 0) <= 0)
423                                 tsleep(pfreecnt, PINTERLOCKED, "wswbuf0", 0);
424                 }
425                 if (pbuf_raw_count <= 0) {
426                         tsleep_interlock(&pbuf_raw_count, 0);
427                         if ((int)atomic_fetchadd_int(&pbuf_raw_count, 0) <= 0)
428                                 tsleep(&pbuf_raw_count, PINTERLOCKED,
429                                        "wswbuf1", 0);
430                         continue;
431                 }
432                 iter = mycpuid & BSWHMASK;
433                 for (loops = BSWHSIZE; loops; --loops) {
434                         if (TAILQ_FIRST(&bswlist_raw[iter]) == NULL) {
435                                 iter = (iter + 1) & BSWHMASK;
436                                 continue;
437                         }
438                         spin_lock(&bswspin_raw[iter]);
439                         if ((bp = TAILQ_FIRST(&bswlist_raw[iter])) == NULL) {
440                                 spin_unlock(&bswspin_raw[iter]);
441                                 iter = (iter + 1) & BSWHMASK;
442                                 continue;
443                         }
444                         TAILQ_REMOVE(&bswlist_raw[iter], bp, b_freelist);
445                         atomic_add_int(&pbuf_raw_count, -1);
446                         if (pfreecnt)
447                                 atomic_add_int(pfreecnt, -1);
448                         spin_unlock(&bswspin_raw[iter]);
449                         initpbuf(bp);
450
451                         return bp;
452                 }
453         }
454         /* not reached */
455 }
456
457 struct buf *
458 getpbuf_kva(int *pfreecnt)
459 {
460         struct buf *bp;
461         int iter;
462         int loops;
463
464         for (;;) {
465                 while (pfreecnt && *pfreecnt <= 0) {
466                         tsleep_interlock(pfreecnt, 0);
467                         if ((int)atomic_fetchadd_int(pfreecnt, 0) <= 0)
468                                 tsleep(pfreecnt, PINTERLOCKED, "wswbuf2", 0);
469                 }
470                 if (pbuf_kva_count <= 0) {
471                         tsleep_interlock(&pbuf_kva_count, 0);
472                         if ((int)atomic_fetchadd_int(&pbuf_kva_count, 0) <= 0)
473                                 tsleep(&pbuf_kva_count, PINTERLOCKED,
474                                        "wswbuf3", 0);
475                         continue;
476                 }
477                 iter = mycpuid & BSWHMASK;
478                 for (loops = BSWHSIZE; loops; --loops) {
479                         if (TAILQ_FIRST(&bswlist_kva[iter]) == NULL) {
480                                 iter = (iter + 1) & BSWHMASK;
481                                 continue;
482                         }
483                         spin_lock(&bswspin_kva[iter]);
484                         if ((bp = TAILQ_FIRST(&bswlist_kva[iter])) == NULL) {
485                                 spin_unlock(&bswspin_kva[iter]);
486                                 iter = (iter + 1) & BSWHMASK;
487                                 continue;
488                         }
489                         TAILQ_REMOVE(&bswlist_kva[iter], bp, b_freelist);
490                         atomic_add_int(&pbuf_kva_count, -1);
491                         if (pfreecnt)
492                                 atomic_add_int(pfreecnt, -1);
493                         spin_unlock(&bswspin_kva[iter]);
494                         initpbuf(bp);
495
496                         return bp;
497                 }
498         }
499         /* not reached */
500 }
501
502 /*
503  * Allocate a pbuf with kernel memory already preallocated.  Caller must
504  * not change the mapping.
505  */
506 struct buf *
507 getpbuf_mem(int *pfreecnt)
508 {
509         struct buf *bp;
510         int iter;
511         int loops;
512
513         for (;;) {
514                 while (pfreecnt && *pfreecnt <= 0) {
515                         tsleep_interlock(pfreecnt, 0);
516                         if ((int)atomic_fetchadd_int(pfreecnt, 0) <= 0)
517                                 tsleep(pfreecnt, PINTERLOCKED, "wswbuf4", 0);
518                 }
519                 if (pbuf_mem_count <= 0) {
520                         tsleep_interlock(&pbuf_mem_count, 0);
521                         if ((int)atomic_fetchadd_int(&pbuf_mem_count, 0) <= 0)
522                                 tsleep(&pbuf_mem_count, PINTERLOCKED,
523                                        "wswbuf5", 0);
524                         continue;
525                 }
526                 iter = mycpuid & BSWHMASK;
527                 for (loops = BSWHSIZE; loops; --loops) {
528                         if (TAILQ_FIRST(&bswlist_mem[iter]) == NULL) {
529                                 iter = (iter + 1) & BSWHMASK;
530                                 continue;
531                         }
532                         spin_lock(&bswspin_mem[iter]);
533                         if ((bp = TAILQ_FIRST(&bswlist_mem[iter])) == NULL) {
534                                 spin_unlock(&bswspin_mem[iter]);
535                                 iter = (iter + 1) & BSWHMASK;
536                                 continue;
537                         }
538                         TAILQ_REMOVE(&bswlist_mem[iter], bp, b_freelist);
539                         atomic_add_int(&pbuf_mem_count, -1);
540                         if (pfreecnt)
541                                 atomic_add_int(pfreecnt, -1);
542                         spin_unlock(&bswspin_mem[iter]);
543                         initpbuf(bp);
544
545                         return bp;
546                 }
547         }
548         /* not reached */
549 }
550
551 /*
552  * Allocate a physical buffer, if one is available.
553  *
554  * Note that there is no NULL hack here - all subsystems using this
555  * call are required to use a non-NULL pfreecnt.
556  *
557  * No requirements.
558  */
559 struct buf *
560 trypbuf(int *pfreecnt)
561 {
562         struct buf *bp;
563         int iter = mycpuid & BSWHMASK;
564         int loops;
565
566         for (loops = BSWHSIZE; loops; --loops) {
567                 if (*pfreecnt <= 0 || TAILQ_FIRST(&bswlist_raw[iter]) == NULL) {
568                         iter = (iter + 1) & BSWHMASK;
569                         continue;
570                 }
571                 spin_lock(&bswspin_raw[iter]);
572                 if (*pfreecnt <= 0 ||
573                     (bp = TAILQ_FIRST(&bswlist_raw[iter])) == NULL) {
574                         spin_unlock(&bswspin_raw[iter]);
575                         iter = (iter + 1) & BSWHMASK;
576                         continue;
577                 }
578                 TAILQ_REMOVE(&bswlist_raw[iter], bp, b_freelist);
579                 atomic_add_int(&pbuf_raw_count, -1);
580                 atomic_add_int(pfreecnt, -1);
581
582                 spin_unlock(&bswspin_raw[iter]);
583
584                 initpbuf(bp);
585
586                 return bp;
587         }
588         return NULL;
589 }
590
591 struct buf *
592 trypbuf_kva(int *pfreecnt)
593 {
594         struct buf *bp;
595         int iter = mycpuid & BSWHMASK;
596         int loops;
597
598         for (loops = BSWHSIZE; loops; --loops) {
599                 if (*pfreecnt <= 0 || TAILQ_FIRST(&bswlist_kva[iter]) == NULL) {
600                         iter = (iter + 1) & BSWHMASK;
601                         continue;
602                 }
603                 spin_lock(&bswspin_kva[iter]);
604                 if (*pfreecnt <= 0 ||
605                     (bp = TAILQ_FIRST(&bswlist_kva[iter])) == NULL) {
606                         spin_unlock(&bswspin_kva[iter]);
607                         iter = (iter + 1) & BSWHMASK;
608                         continue;
609                 }
610                 TAILQ_REMOVE(&bswlist_kva[iter], bp, b_freelist);
611                 atomic_add_int(&pbuf_kva_count, -1);
612                 atomic_add_int(pfreecnt, -1);
613
614                 spin_unlock(&bswspin_kva[iter]);
615
616                 initpbuf(bp);
617
618                 return bp;
619         }
620         return NULL;
621 }
622
623 /*
624  * Release a physical buffer
625  *
626  *      NOTE: pfreecnt can be NULL, but this 'feature' will be removed
627  *      relatively soon when the rest of the subsystems get smart about it. XXX
628  *
629  * No requirements.
630  */
631 void
632 relpbuf(struct buf *bp, int *pfreecnt)
633 {
634         int wake = 0;
635         int wake_free = 0;
636         int iter = bp->b_swindex;
637
638         KKASSERT(bp->b_flags & B_PAGING);
639         dsched_buf_exit(bp);
640
641         BUF_UNLOCK(bp);
642
643         if (bp >= swbuf_mem && bp < &swbuf_mem[nswbuf_mem]) {
644                 KKASSERT(bp->b_kvabase);
645                 spin_lock(&bswspin_mem[iter]);
646                 TAILQ_INSERT_HEAD(&bswlist_mem[iter], bp, b_freelist);
647                 if (atomic_fetchadd_int(&pbuf_mem_count, 1) == nswbuf_mem / 4)
648                         wake = 1;
649                 if (pfreecnt) {
650                         if (atomic_fetchadd_int(pfreecnt, 1) == 1)
651                                 wake_free = 1;
652                 }
653                 spin_unlock(&bswspin_mem[iter]);
654                 if (wake)
655                         wakeup(&pbuf_mem_count);
656         } else if (bp >= swbuf_kva && bp < &swbuf_kva[nswbuf_kva]) {
657                 KKASSERT(bp->b_kvabase);
658                 CPUMASK_ASSZERO(bp->b_cpumask);
659                 spin_lock(&bswspin_kva[iter]);
660                 TAILQ_INSERT_HEAD(&bswlist_kva[iter], bp, b_freelist);
661                 if (atomic_fetchadd_int(&pbuf_kva_count, 1) == nswbuf_kva / 4)
662                         wake = 1;
663                 if (pfreecnt) {
664                         if (atomic_fetchadd_int(pfreecnt, 1) == 1)
665                                 wake_free = 1;
666                 }
667                 spin_unlock(&bswspin_kva[iter]);
668                 if (wake)
669                         wakeup(&pbuf_kva_count);
670         } else {
671                 KKASSERT(bp->b_kvabase == NULL);
672                 KKASSERT(bp >= swbuf_raw && bp < &swbuf_raw[nswbuf_raw]);
673                 CPUMASK_ASSZERO(bp->b_cpumask);
674                 spin_lock(&bswspin_raw[iter]);
675                 TAILQ_INSERT_HEAD(&bswlist_raw[iter], bp, b_freelist);
676                 if (atomic_fetchadd_int(&pbuf_raw_count, 1) == nswbuf_raw / 4)
677                         wake = 1;
678                 if (pfreecnt) {
679                         if (atomic_fetchadd_int(pfreecnt, 1) == 1)
680                                 wake_free = 1;
681                 }
682                 spin_unlock(&bswspin_raw[iter]);
683                 if (wake)
684                         wakeup(&pbuf_raw_count);
685         }
686         if (wake_free)
687                 wakeup(pfreecnt);
688 }
689
690 void
691 pbuf_adjcount(int *pfreecnt, int n)
692 {
693         if (n) {
694                 atomic_add_int(pfreecnt, n);
695                 wakeup(pfreecnt);
696         }
697 }