DragonFlyBSD Kernel Audit
sys/vm/vm_pager.c
← back
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
/*
 * (MPSAFE)
 *
 * Copyright (c) 1991, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * The Mach Operating System project at Carnegie-Mellon University.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	from: @(#)vm_pager.c	8.6 (Berkeley) 1/12/94
 *
 *
 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
 * All rights reserved.
 *
 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
 *
 * Permission to use, copy, modify and distribute this software and
 * its documentation is hereby granted, provided that both the copyright
 * notice and this permission notice appear in all copies of the
 * software, derivative works or modified versions, and any portions
 * thereof, and that both notices appear in supporting documentation.
 *
 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 *
 * Carnegie Mellon requests users of this software to return to
 *
 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
 *  School of Computer Science
 *  Carnegie Mellon University
 *  Pittsburgh PA 15213-3890
 *
 * any improvements or extensions that they make and grant Carnegie the
 * rights to redistribute these changes.
 *
 * $FreeBSD: src/sys/vm/vm_pager.c,v 1.54.2.2 2001/11/18 07:11:00 dillon Exp $
 */

/*
 *	Paging space routine stubs.  Emulates a matchmaker-like interface
 *	for builtin pagers.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/vnode.h>
#include <sys/buf.h>
#include <sys/ucred.h>
#include <sys/dsched.h>
#include <sys/proc.h>
#include <sys/sysctl.h>

#include <vm/vm.h>
#include <vm/vm_param.h>
#include <vm/vm_kern.h>
#include <vm/vm_object.h>
#include <vm/vm_page.h>
#include <vm/vm_pager.h>
#include <vm/vm_extern.h>

#include <sys/buf2.h>
#include <vm/vm_page2.h>

static	pgo_dealloc_t		dead_pager_dealloc;
static	pgo_getpage_t		dead_pager_getpage;
static	pgo_putpages_t		dead_pager_putpages;
static	pgo_haspage_t		dead_pager_haspage;

static struct pagerops deadpagerops = {
	.pgo_dealloc =		dead_pager_dealloc,
	.pgo_getpage =		dead_pager_getpage,
	.pgo_putpages =		dead_pager_putpages,
	.pgo_haspage =		dead_pager_haspage
};

extern struct pagerops defaultpagerops;
extern struct pagerops swappagerops;
extern struct pagerops vnodepagerops;
extern struct pagerops devicepagerops;
extern struct pagerops physpagerops;

/*
 * No requirements.
 */
static int
dead_pager_getpage(vm_object_t obj, vm_pindex_t pindex,
		   vm_page_t *mpp, int seqaccess)
{
	return VM_PAGER_FAIL;
}

/*
 * No requirements.
 */
static void
dead_pager_putpages(vm_object_t object, vm_page_t *m, int count, int flags,
		    int *rtvals)
{
	int i;

	for (i = 0; i < count; i++) {
		rtvals[i] = VM_PAGER_AGAIN;
	}
}

/*
 * No requirements.
 */
static boolean_t
dead_pager_haspage(vm_object_t object, vm_pindex_t pindex)
{
	return FALSE;
}

/*
 * No requirements.
 */
static void
dead_pager_dealloc(vm_object_t object)
{
	KKASSERT(object->swblock_count == 0);
	return;
}

struct pagerops *pagertab[] = {
	&defaultpagerops,	/* OBJT_DEFAULT */
	&swappagerops,		/* OBJT_SWAP */
	&vnodepagerops,		/* OBJT_VNODE */
	&devicepagerops,	/* OBJT_DEVICE */
	&devicepagerops,	/* OBJT_MGTDEVICE */
	&physpagerops,		/* OBJT_PHYS */
	&deadpagerops		/* OBJT_DEAD */
};

/*
 * Kernel address space for mapping pages.
 * Used by pagers where KVAs are needed for IO.
 *
 * XXX needs to be large enough to support the number of pending async
 * cleaning requests (NPENDINGIO == 64) * the maximum swap cluster size
 * (MAXPHYS == 64k) if you want to get the most efficiency.
 */
#define PAGER_MAP_SIZE	(8 * 1024 * 1024)

#define BSWHSIZE	16
#define BSWHMASK	(BSWHSIZE - 1)

TAILQ_HEAD(swqueue, buf);

int pager_map_size = PAGER_MAP_SIZE;

static struct vm_map pager_map_store;
struct vm_map *pager_map = &pager_map_store;

static vm_offset_t swapbkva_mem;	/* swap buffers kva */
static vm_offset_t swapbkva_kva;	/* swap buffers kva */
static struct swqueue bswlist_mem[BSWHSIZE];	/* with preallocated memory */
static struct swqueue bswlist_kva[BSWHSIZE];	/* with kva */
static struct swqueue bswlist_raw[BSWHSIZE];	/* without kva */
static struct spinlock bswspin_mem[BSWHSIZE];
static struct spinlock bswspin_kva[BSWHSIZE];
static struct spinlock bswspin_raw[BSWHSIZE];
static int pbuf_raw_count;
static int pbuf_kva_count;
static int pbuf_mem_count;

SYSCTL_INT(_vm, OID_AUTO, pbuf_raw_count, CTLFLAG_RD, &pbuf_raw_count, 0,
    "Kernel pbuf raw reservations");
SYSCTL_INT(_vm, OID_AUTO, pbuf_kva_count, CTLFLAG_RD, &pbuf_kva_count, 0,
    "Kernel pbuf kva reservations");
SYSCTL_INT(_vm, OID_AUTO, pbuf_mem_count, CTLFLAG_RD, &pbuf_mem_count, 0,
    "Kernel pbuf mem reservations");

/*
 * Initialize the swap buffer list.
 *
 * Called from the low level boot code only.
 */
static void
vm_pager_init(void *arg __unused)
{
	int i;

	for (i = 0; i < BSWHSIZE; ++i) {
		TAILQ_INIT(&bswlist_mem[i]);
		TAILQ_INIT(&bswlist_kva[i]);
		TAILQ_INIT(&bswlist_raw[i]);
		spin_init(&bswspin_mem[i], "bswmem");
		spin_init(&bswspin_kva[i], "bswkva");
		spin_init(&bswspin_raw[i], "bswraw");
	}
}
SYSINIT(vm_mem, SI_BOOT1_VM, SI_ORDER_SECOND, vm_pager_init, NULL);

/*
 * Called from the low level boot code only.
 */
static
void
vm_pager_bufferinit(void *dummy __unused)
{
	struct buf *bp;
	long i;

	/*
	 * Reserve KVM space for pbuf data.
	 */
	swapbkva_mem = kmem_alloc_pageable(pager_map, nswbuf_mem * MAXPHYS,
					   VM_SUBSYS_BUFDATA);
	if (!swapbkva_mem)
		panic("Not enough pager_map VM space for physical buffers");
	swapbkva_kva = kmem_alloc_pageable(pager_map, nswbuf_kva * MAXPHYS,
					   VM_SUBSYS_BUFDATA);
	if (!swapbkva_kva)
		panic("Not enough pager_map VM space for physical buffers");

	/*
	 * Initial pbuf setup.
	 *
	 * mem - These pbufs have permanently allocated memory
	 * kva - These pbufs have unallocated kva reservations
	 * raw - These pbufs have no kva reservations
	 */

	/*
	 * Buffers with pre-allocated kernel memory can be convenient for
	 * copyin/copyout because no SMP page invalidation or other pmap
	 * operations are needed.
	 */
	bp = swbuf_mem;
	for (i = 0; i < nswbuf_mem; ++i, ++bp) {
		vm_page_t m;
		vm_pindex_t pg;
		int j;

		bp->b_kvabase = (caddr_t)((intptr_t)i * MAXPHYS) + swapbkva_mem;
		bp->b_kvasize = MAXPHYS;
		bp->b_swindex = i & BSWHMASK;
		bp->b_cpumask = smp_active_mask;
		BUF_LOCKINIT(bp);
		buf_dep_init(bp);
		TAILQ_INSERT_HEAD(&bswlist_mem[i & BSWHMASK], bp, b_freelist);
		atomic_add_int(&pbuf_mem_count, 1);
		bp->b_data = bp->b_kvabase;
		bp->b_bcount = MAXPHYS;
		bp->b_xio.xio_pages = bp->b_xio.xio_internal_pages;

		pg = (vm_offset_t)bp->b_kvabase >> PAGE_SHIFT;
		vm_object_hold(kernel_object);
		for (j = 0; j < MAXPHYS / PAGE_SIZE; ++j) {
			m = vm_page_alloc(kernel_object, pg, VM_ALLOC_NORMAL |
							      VM_ALLOC_SYSTEM);
			KKASSERT(m != NULL);
			bp->b_xio.xio_internal_pages[j] = m;
			vm_page_wire(m);
			/* early boot, no other cpus running yet */
			pmap_kenter_noinval(pg * PAGE_SIZE, VM_PAGE_TO_PHYS(m));
			cpu_invlpg((void *)(pg * PAGE_SIZE));
			vm_page_wakeup(m);
			++pg;
		}
		vm_object_drop(kernel_object);
		bp->b_xio.xio_npages = j;
	}

	/*
	 * Buffers with pre-assigned KVA bases.  The KVA has no memory pages
	 * assigned to it.  Saves the caller from having to reserve KVA for
	 * the page map.
	 */
	bp = swbuf_kva;
	for (i = 0; i < nswbuf_kva; ++i, ++bp) {
		bp->b_kvabase = (caddr_t)((intptr_t)i * MAXPHYS) + swapbkva_kva;
		bp->b_kvasize = MAXPHYS;
		bp->b_swindex = i & BSWHMASK;
		BUF_LOCKINIT(bp);
		buf_dep_init(bp);
		TAILQ_INSERT_HEAD(&bswlist_kva[i & BSWHMASK], bp, b_freelist);
		atomic_add_int(&pbuf_kva_count, 1);
	}

	/*
	 * RAW buffers with no KVA mappings.
	 *
	 * NOTE: We use KM_NOTLBSYNC here to reduce unnecessary IPIs
	 *	 during startup, which can really slow down emulated
	 *	 systems.
	 */
	nswbuf_raw = nbuf * 2;
	swbuf_raw = (void *)kmem_alloc3(kernel_map,
				round_page(nswbuf_raw * sizeof(struct buf)),
				VM_SUBSYS_BUFDATA,
				KM_NOTLBSYNC);
	smp_invltlb();
	bp = swbuf_raw;
	for (i = 0; i < nswbuf_raw; ++i, ++bp) {
		bp->b_swindex = i & BSWHMASK;
		BUF_LOCKINIT(bp);
		buf_dep_init(bp);
		TAILQ_INSERT_HEAD(&bswlist_raw[i & BSWHMASK], bp, b_freelist);
		atomic_add_int(&pbuf_raw_count, 1);
	}
}

SYSINIT(do_vmpg, SI_BOOT2_MACHDEP, SI_ORDER_FIRST, vm_pager_bufferinit, NULL);

/*
 * No requirements.
 */
void
vm_pager_deallocate(vm_object_t object)
{
	(*pagertab[object->type]->pgo_dealloc) (object);
}

/*
 * vm_pager_get_pages() - inline, see vm/vm_pager.h
 * vm_pager_put_pages() - inline, see vm/vm_pager.h
 * vm_pager_has_page() - inline, see vm/vm_pager.h
 * vm_pager_page_inserted() - inline, see vm/vm_pager.h
 * vm_pager_page_removed() - inline, see vm/vm_pager.h
 */

/*
 * Search the specified pager object list for an object with the
 * specified handle.  If an object with the specified handle is found,
 * increase its reference count and return it.  Otherwise, return NULL.
 *
 * The pager object list must be locked.
 */
vm_object_t
vm_pager_object_lookup(struct pagerlst *pg_list, void *handle)
{
	vm_object_t object;

	TAILQ_FOREACH(object, pg_list, pager_object_entry) {
		if (object->handle == handle) {
			VM_OBJECT_LOCK(object);
			if ((object->flags & OBJ_DEAD) == 0) {
				vm_object_reference_locked(object);
				VM_OBJECT_UNLOCK(object);
				break;
			}
			VM_OBJECT_UNLOCK(object);
		}
	}
	return (object);
}

/*
 * Initialize a physical buffer.
 *
 * No requirements.
 */
static void
initpbuf(struct buf *bp)
{
	bp->b_qindex = 0;		/* BQUEUE_NONE */
	bp->b_data = bp->b_kvabase;	/* NULL if pbuf sans kva */
	bp->b_flags = B_PAGING;
	bp->b_cmd = BUF_CMD_DONE;
	bp->b_error = 0;
	bp->b_bcount = 0;
	bp->b_bufsize = MAXPHYS;
	initbufbio(bp);
	xio_init(&bp->b_xio);
	BUF_LOCK(bp, LK_EXCLUSIVE);
}

/*
 * Allocate a physical buffer
 *
 * If (pfreecnt != NULL) then *pfreecnt will be decremented on return and
 * the function will block while it is <= 0.
 *
 * Physical buffers can be with or without KVA space reserved.  There
 * are severe limitations on the ones with KVA reserved, and fewer
 * limitations on the ones without.  getpbuf() gets one without,
 * getpbuf_kva() gets one with.
 *
 * No requirements.
 */
struct buf *
getpbuf(int *pfreecnt)
{
	struct buf *bp;
	int iter;
	int loops;

	for (;;) {
		while (pfreecnt && *pfreecnt <= 0) {
			tsleep_interlock(pfreecnt, 0);
			if ((int)atomic_fetchadd_int(pfreecnt, 0) <= 0)
				tsleep(pfreecnt, PINTERLOCKED, "wswbuf0", 0);
		}
		if (pbuf_raw_count <= 0) {
			tsleep_interlock(&pbuf_raw_count, 0);
			if ((int)atomic_fetchadd_int(&pbuf_raw_count, 0) <= 0)
				tsleep(&pbuf_raw_count, PINTERLOCKED,
				       "wswbuf1", 0);
			continue;
		}
		iter = mycpuid & BSWHMASK;
		for (loops = BSWHSIZE; loops; --loops) {
			if (TAILQ_FIRST(&bswlist_raw[iter]) == NULL) {
				iter = (iter + 1) & BSWHMASK;
				continue;
			}
			spin_lock(&bswspin_raw[iter]);
			if ((bp = TAILQ_FIRST(&bswlist_raw[iter])) == NULL) {
				spin_unlock(&bswspin_raw[iter]);
				iter = (iter + 1) & BSWHMASK;
				continue;
			}
			TAILQ_REMOVE(&bswlist_raw[iter], bp, b_freelist);
			atomic_add_int(&pbuf_raw_count, -1);
			if (pfreecnt)
				atomic_add_int(pfreecnt, -1);
			spin_unlock(&bswspin_raw[iter]);
			initpbuf(bp);

			return bp;
		}
	}
	/* not reached */
}

struct buf *
getpbuf_kva(int *pfreecnt)
{
	struct buf *bp;
	int iter;
	int loops;

	for (;;) {
		while (pfreecnt && *pfreecnt <= 0) {
			tsleep_interlock(pfreecnt, 0);
			if ((int)atomic_fetchadd_int(pfreecnt, 0) <= 0)
				tsleep(pfreecnt, PINTERLOCKED, "wswbuf2", 0);
		}
		if (pbuf_kva_count <= 0) {
			tsleep_interlock(&pbuf_kva_count, 0);
			if ((int)atomic_fetchadd_int(&pbuf_kva_count, 0) <= 0)
				tsleep(&pbuf_kva_count, PINTERLOCKED,
				       "wswbuf3", 0);
			continue;
		}
		iter = mycpuid & BSWHMASK;
		for (loops = BSWHSIZE; loops; --loops) {
			if (TAILQ_FIRST(&bswlist_kva[iter]) == NULL) {
				iter = (iter + 1) & BSWHMASK;
				continue;
			}
			spin_lock(&bswspin_kva[iter]);
			if ((bp = TAILQ_FIRST(&bswlist_kva[iter])) == NULL) {
				spin_unlock(&bswspin_kva[iter]);
				iter = (iter + 1) & BSWHMASK;
				continue;
			}
			TAILQ_REMOVE(&bswlist_kva[iter], bp, b_freelist);
			atomic_add_int(&pbuf_kva_count, -1);
			if (pfreecnt)
				atomic_add_int(pfreecnt, -1);
			spin_unlock(&bswspin_kva[iter]);
			initpbuf(bp);

			return bp;
		}
	}
	/* not reached */
}

/*
 * Allocate a pbuf with kernel memory already preallocated.  Caller must
 * not change the mapping.
 */
struct buf *
getpbuf_mem(int *pfreecnt)
{
	struct buf *bp;
	int iter;
	int loops;

	for (;;) {
		while (pfreecnt && *pfreecnt <= 0) {
			tsleep_interlock(pfreecnt, 0);
			if ((int)atomic_fetchadd_int(pfreecnt, 0) <= 0)
				tsleep(pfreecnt, PINTERLOCKED, "wswbuf4", 0);
		}
		if (pbuf_mem_count <= 0) {
			tsleep_interlock(&pbuf_mem_count, 0);
			if ((int)atomic_fetchadd_int(&pbuf_mem_count, 0) <= 0)
				tsleep(&pbuf_mem_count, PINTERLOCKED,
				       "wswbuf5", 0);
			continue;
		}
		iter = mycpuid & BSWHMASK;
		for (loops = BSWHSIZE; loops; --loops) {
			if (TAILQ_FIRST(&bswlist_mem[iter]) == NULL) {
				iter = (iter + 1) & BSWHMASK;
				continue;
			}
			spin_lock(&bswspin_mem[iter]);
			if ((bp = TAILQ_FIRST(&bswlist_mem[iter])) == NULL) {
				spin_unlock(&bswspin_mem[iter]);
				iter = (iter + 1) & BSWHMASK;
				continue;
			}
			TAILQ_REMOVE(&bswlist_mem[iter], bp, b_freelist);
			atomic_add_int(&pbuf_mem_count, -1);
			if (pfreecnt)
				atomic_add_int(pfreecnt, -1);
			spin_unlock(&bswspin_mem[iter]);
			initpbuf(bp);

			return bp;
		}
	}
	/* not reached */
}

/*
 * Allocate a physical buffer, if one is available.
 *
 * Note that there is no NULL hack here - all subsystems using this
 * call are required to use a non-NULL pfreecnt.
 *
 * No requirements.
 */
struct buf *
trypbuf(int *pfreecnt)
{
	struct buf *bp;
	int iter = mycpuid & BSWHMASK;
	int loops;

	for (loops = BSWHSIZE; loops; --loops) {
		if (*pfreecnt <= 0 || TAILQ_FIRST(&bswlist_raw[iter]) == NULL) {
			iter = (iter + 1) & BSWHMASK;
			continue;
		}
		spin_lock(&bswspin_raw[iter]);
		if (*pfreecnt <= 0 ||
		    (bp = TAILQ_FIRST(&bswlist_raw[iter])) == NULL) {
			spin_unlock(&bswspin_raw[iter]);
			iter = (iter + 1) & BSWHMASK;
			continue;
		}
		TAILQ_REMOVE(&bswlist_raw[iter], bp, b_freelist);
		atomic_add_int(&pbuf_raw_count, -1);
		atomic_add_int(pfreecnt, -1);

		spin_unlock(&bswspin_raw[iter]);

		initpbuf(bp);

		return bp;
	}
	return NULL;
}

struct buf *
trypbuf_kva(int *pfreecnt)
{
	struct buf *bp;
	int iter = mycpuid & BSWHMASK;
	int loops;

	for (loops = BSWHSIZE; loops; --loops) {
		if (*pfreecnt <= 0 || TAILQ_FIRST(&bswlist_kva[iter]) == NULL) {
			iter = (iter + 1) & BSWHMASK;
			continue;
		}
		spin_lock(&bswspin_kva[iter]);
		if (*pfreecnt <= 0 ||
		    (bp = TAILQ_FIRST(&bswlist_kva[iter])) == NULL) {
			spin_unlock(&bswspin_kva[iter]);
			iter = (iter + 1) & BSWHMASK;
			continue;
		}
		TAILQ_REMOVE(&bswlist_kva[iter], bp, b_freelist);
		atomic_add_int(&pbuf_kva_count, -1);
		atomic_add_int(pfreecnt, -1);

		spin_unlock(&bswspin_kva[iter]);

		initpbuf(bp);

		return bp;
	}
	return NULL;
}

/*
 * Release a physical buffer
 *
 *	NOTE: pfreecnt can be NULL, but this 'feature' will be removed
 *	relatively soon when the rest of the subsystems get smart about it. XXX
 *
 * No requirements.
 */
void
relpbuf(struct buf *bp, int *pfreecnt)
{
	int wake = 0;
	int wake_free = 0;
	int iter = bp->b_swindex;

	KKASSERT(bp->b_flags & B_PAGING);
	dsched_buf_exit(bp);

	BUF_UNLOCK(bp);

	if (bp >= swbuf_mem && bp < &swbuf_mem[nswbuf_mem]) {
		KKASSERT(bp->b_kvabase);
		spin_lock(&bswspin_mem[iter]);
		TAILQ_INSERT_HEAD(&bswlist_mem[iter], bp, b_freelist);
		if (atomic_fetchadd_int(&pbuf_mem_count, 1) == nswbuf_mem / 4)
			wake = 1;
		if (pfreecnt) {
			if (atomic_fetchadd_int(pfreecnt, 1) == 1)
				wake_free = 1;
		}
		spin_unlock(&bswspin_mem[iter]);
		if (wake)
			wakeup(&pbuf_mem_count);
	} else if (bp >= swbuf_kva && bp < &swbuf_kva[nswbuf_kva]) {
		KKASSERT(bp->b_kvabase);
		CPUMASK_ASSZERO(bp->b_cpumask);
		spin_lock(&bswspin_kva[iter]);
		TAILQ_INSERT_HEAD(&bswlist_kva[iter], bp, b_freelist);
		if (atomic_fetchadd_int(&pbuf_kva_count, 1) == nswbuf_kva / 4)
			wake = 1;
		if (pfreecnt) {
			if (atomic_fetchadd_int(pfreecnt, 1) == 1)
				wake_free = 1;
		}
		spin_unlock(&bswspin_kva[iter]);
		if (wake)
			wakeup(&pbuf_kva_count);
	} else {
		KKASSERT(bp->b_kvabase == NULL);
		KKASSERT(bp >= swbuf_raw && bp < &swbuf_raw[nswbuf_raw]);
		CPUMASK_ASSZERO(bp->b_cpumask);
		spin_lock(&bswspin_raw[iter]);
		TAILQ_INSERT_HEAD(&bswlist_raw[iter], bp, b_freelist);
		if (atomic_fetchadd_int(&pbuf_raw_count, 1) == nswbuf_raw / 4)
			wake = 1;
		if (pfreecnt) {
			if (atomic_fetchadd_int(pfreecnt, 1) == 1)
				wake_free = 1;
		}
		spin_unlock(&bswspin_raw[iter]);
		if (wake)
			wakeup(&pbuf_raw_count);
	}
	if (wake_free)
		wakeup(pfreecnt);
}

void
pbuf_adjcount(int *pfreecnt, int n)
{
	if (n) {
		atomic_add_int(pfreecnt, n);
		wakeup(pfreecnt);
	}
}