uvm_aobj.c revision 1.116.12.3       1  1.116.12.2       tls /*	$NetBSD: uvm_aobj.c,v 1.116.12.3 2017/12/03 11:39:22 jdolecek Exp $	*/
      2         1.6       mrg 
      3         1.7       chs /*
      4         1.7       chs  * Copyright (c) 1998 Chuck Silvers, Charles D. Cranor and
      5         1.7       chs  *                    Washington University.
      6         1.7       chs  * All rights reserved.
      7         1.7       chs  *
      8         1.7       chs  * Redistribution and use in source and binary forms, with or without
      9         1.7       chs  * modification, are permitted provided that the following conditions
     10         1.7       chs  * are met:
     11         1.7       chs  * 1. Redistributions of source code must retain the above copyright
     12         1.7       chs  *    notice, this list of conditions and the following disclaimer.
     13         1.7       chs  * 2. Redistributions in binary form must reproduce the above copyright
     14         1.7       chs  *    notice, this list of conditions and the following disclaimer in the
     15         1.7       chs  *    documentation and/or other materials provided with the distribution.
     16         1.7       chs  *
     17         1.7       chs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18         1.7       chs  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19         1.7       chs  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20         1.7       chs  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21         1.7       chs  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22         1.7       chs  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23         1.7       chs  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24         1.7       chs  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25         1.7       chs  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26         1.7       chs  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27         1.7       chs  *
     28         1.4       mrg  * from: Id: uvm_aobj.c,v 1.1.2.5 1998/02/06 05:14:38 chs Exp
     29         1.4       mrg  */
     30       1.113     rmind 
     31         1.7       chs /*
     32         1.7       chs  * uvm_aobj.c: anonymous memory uvm_object pager
     33         1.7       chs  *
     34         1.7       chs  * author: Chuck Silvers <chuq (at) chuq.com>
     35         1.7       chs  * started: Jan-1998
     36         1.7       chs  *
     37         1.7       chs  * - design mostly from Chuck Cranor
     38         1.7       chs  */
     39        1.49     lukem 
     40        1.49     lukem #include <sys/cdefs.h>
     41  1.116.12.2       tls __KERNEL_RCSID(0, "$NetBSD: uvm_aobj.c,v 1.116.12.3 2017/12/03 11:39:22 jdolecek Exp $");
     42         1.7       chs 
     43  1.116.12.3  jdolecek #ifdef _KERNEL_OPT
     44         1.7       chs #include "opt_uvmhist.h"
     45  1.116.12.3  jdolecek #endif
     46         1.1       mrg 
     47         1.1       mrg #include <sys/param.h>
     48         1.1       mrg #include <sys/systm.h>
     49        1.37       chs #include <sys/kernel.h>
     50       1.104     rmind #include <sys/kmem.h>
     51        1.12   thorpej #include <sys/pool.h>
     52  1.116.12.1       tls #include <sys/atomic.h>
     53         1.1       mrg 
     54         1.1       mrg #include <uvm/uvm.h>
     55         1.1       mrg 
     56         1.1       mrg /*
     57  1.116.12.1       tls  * An anonymous UVM object (aobj) manages anonymous-memory.  In addition to
     58  1.116.12.1       tls  * keeping the list of resident pages, it may also keep a list of allocated
     59  1.116.12.1       tls  * swap blocks.  Depending on the size of the object, this list is either
     60  1.116.12.1       tls  * stored in an array (small objects) or in a hash table (large objects).
     61  1.116.12.1       tls  *
     62  1.116.12.1       tls  * Lock order
     63  1.116.12.1       tls  *
     64  1.116.12.1       tls  *	uao_list_lock ->
     65  1.116.12.1       tls  *		uvm_object::vmobjlock
     66         1.1       mrg  */
     67         1.1       mrg 
     68         1.1       mrg /*
     69  1.116.12.1       tls  * Note: for hash tables, we break the address space of the aobj into blocks
     70  1.116.12.1       tls  * of UAO_SWHASH_CLUSTER_SIZE pages, which shall be a power of two.
     71         1.1       mrg  */
     72         1.1       mrg 
     73  1.116.12.1       tls #define	UAO_SWHASH_CLUSTER_SHIFT	4
     74  1.116.12.1       tls #define	UAO_SWHASH_CLUSTER_SIZE		(1 << UAO_SWHASH_CLUSTER_SHIFT)
     75         1.1       mrg 
     76  1.116.12.1       tls /* Get the "tag" for this page index. */
     77  1.116.12.1       tls #define	UAO_SWHASH_ELT_TAG(idx)		((idx) >> UAO_SWHASH_CLUSTER_SHIFT)
     78  1.116.12.1       tls #define UAO_SWHASH_ELT_PAGESLOT_IDX(idx) \
     79  1.116.12.1       tls     ((idx) & (UAO_SWHASH_CLUSTER_SIZE - 1))
     80         1.1       mrg 
     81  1.116.12.1       tls /* Given an ELT and a page index, find the swap slot. */
     82  1.116.12.1       tls #define	UAO_SWHASH_ELT_PAGESLOT(elt, idx) \
     83  1.116.12.1       tls     ((elt)->slots[UAO_SWHASH_ELT_PAGESLOT_IDX(idx)])
     84        1.75      yamt 
     85  1.116.12.1       tls /* Given an ELT, return its pageidx base. */
     86  1.116.12.1       tls #define	UAO_SWHASH_ELT_PAGEIDX_BASE(ELT) \
     87  1.116.12.1       tls     ((elt)->tag << UAO_SWHASH_CLUSTER_SHIFT)
     88        1.46       chs 
     89  1.116.12.1       tls /* The hash function. */
     90  1.116.12.1       tls #define	UAO_SWHASH_HASH(aobj, idx) \
     91  1.116.12.1       tls     (&(aobj)->u_swhash[(((idx) >> UAO_SWHASH_CLUSTER_SHIFT) \
     92  1.116.12.1       tls     & (aobj)->u_swhashmask)])
     93         1.1       mrg 
     94         1.1       mrg /*
     95  1.116.12.1       tls  * The threshold which determines whether we will use an array or a
     96         1.1       mrg  * hash table to store the list of allocated swap blocks.
     97         1.1       mrg  */
     98  1.116.12.1       tls #define	UAO_SWHASH_THRESHOLD		(UAO_SWHASH_CLUSTER_SIZE * 4)
     99  1.116.12.1       tls #define	UAO_USES_SWHASH(aobj) \
    100  1.116.12.1       tls     ((aobj)->u_pages > UAO_SWHASH_THRESHOLD)
    101  1.116.12.1       tls 
    102  1.116.12.1       tls /* The number of buckets in a hash, with an upper bound. */
    103  1.116.12.1       tls #define	UAO_SWHASH_MAXBUCKETS		256
    104  1.116.12.1       tls #define	UAO_SWHASH_BUCKETS(aobj) \
    105  1.116.12.1       tls     (MIN((aobj)->u_pages >> UAO_SWHASH_CLUSTER_SHIFT, UAO_SWHASH_MAXBUCKETS))
    106         1.1       mrg 
    107         1.1       mrg /*
    108         1.1       mrg  * uao_swhash_elt: when a hash table is being used, this structure defines
    109         1.1       mrg  * the format of an entry in the bucket list.
    110         1.1       mrg  */
    111         1.1       mrg 
    112         1.1       mrg struct uao_swhash_elt {
    113         1.5       mrg 	LIST_ENTRY(uao_swhash_elt) list;	/* the hash list */
    114        1.28    kleink 	voff_t tag;				/* our 'tag' */
    115         1.5       mrg 	int count;				/* our number of active slots */
    116         1.5       mrg 	int slots[UAO_SWHASH_CLUSTER_SIZE];	/* the slots */
    117         1.1       mrg };
    118         1.1       mrg 
    119         1.1       mrg /*
    120         1.1       mrg  * uao_swhash: the swap hash table structure
    121         1.1       mrg  */
    122         1.1       mrg 
    123         1.1       mrg LIST_HEAD(uao_swhash, uao_swhash_elt);
    124         1.1       mrg 
    125        1.12   thorpej /*
    126       1.113     rmind  * uao_swhash_elt_pool: pool of uao_swhash_elt structures.
    127       1.113     rmind  * Note: pages for this pool must not come from a pageable kernel map.
    128        1.12   thorpej  */
    129  1.116.12.1       tls static struct pool	uao_swhash_elt_pool	__cacheline_aligned;
    130         1.1       mrg 
    131         1.1       mrg /*
    132         1.1       mrg  * uvm_aobj: the actual anon-backed uvm_object
    133         1.1       mrg  *
    134         1.1       mrg  * => the uvm_object is at the top of the structure, this allows
    135        1.46       chs  *   (struct uvm_aobj *) == (struct uvm_object *)
    136         1.1       mrg  * => only one of u_swslots and u_swhash is used in any given aobj
    137         1.1       mrg  */
    138         1.1       mrg 
    139         1.1       mrg struct uvm_aobj {
    140         1.5       mrg 	struct uvm_object u_obj; /* has: lock, pgops, memq, #pages, #refs */
    141        1.79    cherry 	pgoff_t u_pages;	 /* number of pages in entire object */
    142         1.5       mrg 	int u_flags;		 /* the flags (see uvm_aobj.h) */
    143         1.5       mrg 	int *u_swslots;		 /* array of offset->swapslot mappings */
    144         1.5       mrg 				 /*
    145         1.5       mrg 				  * hashtable of offset->swapslot mappings
    146         1.5       mrg 				  * (u_swhash is an array of bucket heads)
    147         1.5       mrg 				  */
    148         1.5       mrg 	struct uao_swhash *u_swhash;
    149         1.5       mrg 	u_long u_swhashmask;		/* mask for hashtable */
    150         1.5       mrg 	LIST_ENTRY(uvm_aobj) u_list;	/* global list of aobjs */
    151  1.116.12.2       tls 	int u_freelist;		  /* freelist to allocate pages from */
    152         1.1       mrg };
    153         1.1       mrg 
    154        1.62  junyoung static void	uao_free(struct uvm_aobj *);
    155        1.62  junyoung static int	uao_get(struct uvm_object *, voff_t, struct vm_page **,
    156        1.62  junyoung 		    int *, int, vm_prot_t, int, int);
    157        1.86      matt static int	uao_put(struct uvm_object *, voff_t, voff_t, int);
    158        1.72      yamt 
    159        1.72      yamt #if defined(VMSWAP)
    160        1.72      yamt static struct uao_swhash_elt *uao_find_swhash_elt
    161        1.85   thorpej     (struct uvm_aobj *, int, bool);
    162        1.72      yamt 
    163        1.85   thorpej static bool uao_pagein(struct uvm_aobj *, int, int);
    164        1.85   thorpej static bool uao_pagein_page(struct uvm_aobj *, int);
    165        1.72      yamt #endif /* defined(VMSWAP) */
    166         1.1       mrg 
    167  1.116.12.2       tls static struct vm_page	*uao_pagealloc(struct uvm_object *, voff_t, int);
    168  1.116.12.2       tls 
    169         1.1       mrg /*
    170         1.1       mrg  * aobj_pager
    171        1.41       chs  *
    172         1.1       mrg  * note that some functions (e.g. put) are handled elsewhere
    173         1.1       mrg  */
    174         1.1       mrg 
    175        1.95      yamt const struct uvm_pagerops aobj_pager = {
    176        1.94      yamt 	.pgo_reference = uao_reference,
    177        1.94      yamt 	.pgo_detach = uao_detach,
    178        1.94      yamt 	.pgo_get = uao_get,
    179        1.94      yamt 	.pgo_put = uao_put,
    180         1.1       mrg };
    181         1.1       mrg 
    182         1.1       mrg /*
    183         1.1       mrg  * uao_list: global list of active aobjs, locked by uao_list_lock
    184         1.1       mrg  */
    185         1.1       mrg 
    186  1.116.12.1       tls static LIST_HEAD(aobjlist, uvm_aobj) uao_list	__cacheline_aligned;
    187  1.116.12.1       tls static kmutex_t		uao_list_lock		__cacheline_aligned;
    188         1.1       mrg 
    189         1.1       mrg /*
    190         1.1       mrg  * hash table/array related functions
    191         1.1       mrg  */
    192         1.1       mrg 
    193        1.72      yamt #if defined(VMSWAP)
    194        1.72      yamt 
    195         1.1       mrg /*
    196         1.1       mrg  * uao_find_swhash_elt: find (or create) a hash table entry for a page
    197         1.1       mrg  * offset.
    198         1.1       mrg  *
    199         1.1       mrg  * => the object should be locked by the caller
    200         1.1       mrg  */
    201         1.1       mrg 
    202         1.5       mrg static struct uao_swhash_elt *
    203        1.85   thorpej uao_find_swhash_elt(struct uvm_aobj *aobj, int pageidx, bool create)
    204         1.5       mrg {
    205         1.5       mrg 	struct uao_swhash *swhash;
    206         1.5       mrg 	struct uao_swhash_elt *elt;
    207        1.28    kleink 	voff_t page_tag;
    208         1.1       mrg 
    209        1.45       chs 	swhash = UAO_SWHASH_HASH(aobj, pageidx);
    210        1.45       chs 	page_tag = UAO_SWHASH_ELT_TAG(pageidx);
    211         1.1       mrg 
    212         1.5       mrg 	/*
    213         1.5       mrg 	 * now search the bucket for the requested tag
    214         1.5       mrg 	 */
    215        1.45       chs 
    216        1.37       chs 	LIST_FOREACH(elt, swhash, list) {
    217        1.45       chs 		if (elt->tag == page_tag) {
    218        1.45       chs 			return elt;
    219        1.45       chs 		}
    220         1.5       mrg 	}
    221        1.45       chs 	if (!create) {
    222         1.5       mrg 		return NULL;
    223        1.45       chs 	}
    224         1.5       mrg 
    225         1.5       mrg 	/*
    226        1.12   thorpej 	 * allocate a new entry for the bucket and init/insert it in
    227         1.5       mrg 	 */
    228        1.45       chs 
    229        1.45       chs 	elt = pool_get(&uao_swhash_elt_pool, PR_NOWAIT);
    230        1.45       chs 	if (elt == NULL) {
    231        1.45       chs 		return NULL;
    232        1.45       chs 	}
    233         1.5       mrg 	LIST_INSERT_HEAD(swhash, elt, list);
    234         1.5       mrg 	elt->tag = page_tag;
    235         1.5       mrg 	elt->count = 0;
    236         1.9     perry 	memset(elt->slots, 0, sizeof(elt->slots));
    237        1.45       chs 	return elt;
    238         1.1       mrg }
    239         1.1       mrg 
    240         1.1       mrg /*
    241         1.1       mrg  * uao_find_swslot: find the swap slot number for an aobj/pageidx
    242         1.1       mrg  *
    243        1.41       chs  * => object must be locked by caller
    244         1.1       mrg  */
    245        1.46       chs 
    246        1.46       chs int
    247        1.67   thorpej uao_find_swslot(struct uvm_object *uobj, int pageidx)
    248         1.1       mrg {
    249        1.46       chs 	struct uvm_aobj *aobj = (struct uvm_aobj *)uobj;
    250        1.46       chs 	struct uao_swhash_elt *elt;
    251         1.1       mrg 
    252         1.5       mrg 	/*
    253         1.5       mrg 	 * if noswap flag is set, then we never return a slot
    254         1.5       mrg 	 */
    255         1.1       mrg 
    256         1.5       mrg 	if (aobj->u_flags & UAO_FLAG_NOSWAP)
    257  1.116.12.1       tls 		return 0;
    258         1.1       mrg 
    259         1.5       mrg 	/*
    260         1.5       mrg 	 * if hashing, look in hash table.
    261         1.5       mrg 	 */
    262         1.1       mrg 
    263         1.5       mrg 	if (UAO_USES_SWHASH(aobj)) {
    264        1.87   thorpej 		elt = uao_find_swhash_elt(aobj, pageidx, false);
    265  1.116.12.1       tls 		return elt ? UAO_SWHASH_ELT_PAGESLOT(elt, pageidx) : 0;
    266         1.5       mrg 	}
    267         1.1       mrg 
    268        1.41       chs 	/*
    269         1.5       mrg 	 * otherwise, look in the array
    270         1.5       mrg 	 */
    271        1.46       chs 
    272  1.116.12.1       tls 	return aobj->u_swslots[pageidx];
    273         1.1       mrg }
    274         1.1       mrg 
    275         1.1       mrg /*
    276         1.1       mrg  * uao_set_swslot: set the swap slot for a page in an aobj.
    277         1.1       mrg  *
    278         1.1       mrg  * => setting a slot to zero frees the slot
    279         1.1       mrg  * => object must be locked by caller
    280        1.45       chs  * => we return the old slot number, or -1 if we failed to allocate
    281        1.45       chs  *    memory to record the new slot number
    282         1.1       mrg  */
    283        1.46       chs 
    284         1.5       mrg int
    285        1.67   thorpej uao_set_swslot(struct uvm_object *uobj, int pageidx, int slot)
    286         1.5       mrg {
    287         1.5       mrg 	struct uvm_aobj *aobj = (struct uvm_aobj *)uobj;
    288        1.45       chs 	struct uao_swhash_elt *elt;
    289         1.5       mrg 	int oldslot;
    290         1.5       mrg 	UVMHIST_FUNC("uao_set_swslot"); UVMHIST_CALLED(pdhist);
    291  1.116.12.3  jdolecek 	UVMHIST_LOG(pdhist, "aobj %#jx pageidx %jd slot %jd",
    292  1.116.12.3  jdolecek 	    (uintptr_t)aobj, pageidx, slot, 0);
    293         1.1       mrg 
    294       1.115     rmind 	KASSERT(mutex_owned(uobj->vmobjlock) || uobj->uo_refs == 0);
    295       1.109     rmind 
    296         1.5       mrg 	/*
    297        1.46       chs 	 * if noswap flag is set, then we can't set a non-zero slot.
    298         1.5       mrg 	 */
    299         1.1       mrg 
    300         1.5       mrg 	if (aobj->u_flags & UAO_FLAG_NOSWAP) {
    301  1.116.12.1       tls 		KASSERTMSG(slot == 0, "uao_set_swslot: no swap object");
    302  1.116.12.1       tls 		return 0;
    303         1.5       mrg 	}
    304         1.1       mrg 
    305         1.5       mrg 	/*
    306         1.5       mrg 	 * are we using a hash table?  if so, add it in the hash.
    307         1.5       mrg 	 */
    308         1.1       mrg 
    309         1.5       mrg 	if (UAO_USES_SWHASH(aobj)) {
    310        1.39       chs 
    311        1.12   thorpej 		/*
    312        1.12   thorpej 		 * Avoid allocating an entry just to free it again if
    313        1.12   thorpej 		 * the page had not swap slot in the first place, and
    314        1.12   thorpej 		 * we are freeing.
    315        1.12   thorpej 		 */
    316        1.39       chs 
    317        1.46       chs 		elt = uao_find_swhash_elt(aobj, pageidx, slot != 0);
    318        1.12   thorpej 		if (elt == NULL) {
    319        1.45       chs 			return slot ? -1 : 0;
    320        1.12   thorpej 		}
    321         1.5       mrg 
    322         1.5       mrg 		oldslot = UAO_SWHASH_ELT_PAGESLOT(elt, pageidx);
    323         1.5       mrg 		UAO_SWHASH_ELT_PAGESLOT(elt, pageidx) = slot;
    324         1.5       mrg 
    325         1.5       mrg 		/*
    326         1.5       mrg 		 * now adjust the elt's reference counter and free it if we've
    327         1.5       mrg 		 * dropped it to zero.
    328         1.5       mrg 		 */
    329         1.5       mrg 
    330         1.5       mrg 		if (slot) {
    331         1.5       mrg 			if (oldslot == 0)
    332         1.5       mrg 				elt->count++;
    333        1.45       chs 		} else {
    334        1.45       chs 			if (oldslot)
    335         1.5       mrg 				elt->count--;
    336         1.5       mrg 
    337         1.5       mrg 			if (elt->count == 0) {
    338         1.5       mrg 				LIST_REMOVE(elt, list);
    339        1.12   thorpej 				pool_put(&uao_swhash_elt_pool, elt);
    340         1.5       mrg 			}
    341         1.5       mrg 		}
    342        1.41       chs 	} else {
    343         1.5       mrg 		/* we are using an array */
    344         1.5       mrg 		oldslot = aobj->u_swslots[pageidx];
    345         1.5       mrg 		aobj->u_swslots[pageidx] = slot;
    346         1.5       mrg 	}
    347  1.116.12.1       tls 	return oldslot;
    348         1.1       mrg }
    349         1.1       mrg 
    350        1.72      yamt #endif /* defined(VMSWAP) */
    351        1.72      yamt 
    352         1.1       mrg /*
    353         1.1       mrg  * end of hash/array functions
    354         1.1       mrg  */
    355         1.1       mrg 
    356         1.1       mrg /*
    357         1.1       mrg  * uao_free: free all resources held by an aobj, and then free the aobj
    358         1.1       mrg  *
    359         1.1       mrg  * => the aobj should be dead
    360         1.1       mrg  */
    361        1.46       chs 
    362         1.1       mrg static void
    363        1.67   thorpej uao_free(struct uvm_aobj *aobj)
    364         1.1       mrg {
    365  1.116.12.1       tls 	struct uvm_object *uobj = &aobj->u_obj;
    366        1.96        ad 
    367  1.116.12.1       tls 	KASSERT(mutex_owned(uobj->vmobjlock));
    368  1.116.12.1       tls 	uao_dropswap_range(uobj, 0, 0);
    369  1.116.12.1       tls 	mutex_exit(uobj->vmobjlock);
    370        1.72      yamt 
    371        1.72      yamt #if defined(VMSWAP)
    372         1.5       mrg 	if (UAO_USES_SWHASH(aobj)) {
    373         1.1       mrg 
    374         1.5       mrg 		/*
    375        1.75      yamt 		 * free the hash table itself.
    376         1.5       mrg 		 */
    377        1.46       chs 
    378       1.104     rmind 		hashdone(aobj->u_swhash, HASH_LIST, aobj->u_swhashmask);
    379         1.5       mrg 	} else {
    380         1.5       mrg 
    381         1.5       mrg 		/*
    382        1.75      yamt 		 * free the array itsself.
    383         1.5       mrg 		 */
    384         1.5       mrg 
    385       1.104     rmind 		kmem_free(aobj->u_swslots, aobj->u_pages * sizeof(int));
    386         1.1       mrg 	}
    387        1.72      yamt #endif /* defined(VMSWAP) */
    388        1.72      yamt 
    389         1.5       mrg 	/*
    390         1.5       mrg 	 * finally free the aobj itself
    391         1.5       mrg 	 */
    392        1.46       chs 
    393  1.116.12.1       tls 	uvm_obj_destroy(uobj, true);
    394       1.113     rmind 	kmem_free(aobj, sizeof(struct uvm_aobj));
    395         1.1       mrg }
    396         1.1       mrg 
    397         1.1       mrg /*
    398         1.1       mrg  * pager functions
    399         1.1       mrg  */
    400         1.1       mrg 
    401         1.1       mrg /*
    402         1.1       mrg  * uao_create: create an aobj of the given size and return its uvm_object.
    403         1.1       mrg  *
    404         1.1       mrg  * => for normal use, flags are always zero
    405         1.1       mrg  * => for the kernel object, the flags are:
    406         1.1       mrg  *	UAO_FLAG_KERNOBJ - allocate the kernel object (can only happen once)
    407         1.1       mrg  *	UAO_FLAG_KERNSWAP - enable swapping of kernel object ("           ")
    408         1.1       mrg  */
    409        1.46       chs 
    410         1.5       mrg struct uvm_object *
    411        1.67   thorpej uao_create(vsize_t size, int flags)
    412         1.5       mrg {
    413        1.46       chs 	static struct uvm_aobj kernel_object_store;
    414       1.115     rmind 	static kmutex_t kernel_object_lock;
    415  1.116.12.2       tls 	static int kobj_alloced __diagused = 0;
    416        1.79    cherry 	pgoff_t pages = round_page(size) >> PAGE_SHIFT;
    417         1.5       mrg 	struct uvm_aobj *aobj;
    418        1.66      yamt 	int refs;
    419         1.1       mrg 
    420         1.5       mrg 	/*
    421       1.114     rmind 	 * Allocate a new aobj, unless kernel object is requested.
    422        1.27       chs 	 */
    423         1.5       mrg 
    424        1.46       chs 	if (flags & UAO_FLAG_KERNOBJ) {
    425        1.46       chs 		KASSERT(!kobj_alloced);
    426         1.5       mrg 		aobj = &kernel_object_store;
    427         1.5       mrg 		aobj->u_pages = pages;
    428        1.46       chs 		aobj->u_flags = UAO_FLAG_NOSWAP;
    429        1.66      yamt 		refs = UVM_OBJ_KERN;
    430         1.5       mrg 		kobj_alloced = UAO_FLAG_KERNOBJ;
    431         1.5       mrg 	} else if (flags & UAO_FLAG_KERNSWAP) {
    432        1.46       chs 		KASSERT(kobj_alloced == UAO_FLAG_KERNOBJ);
    433         1.5       mrg 		aobj = &kernel_object_store;
    434         1.5       mrg 		kobj_alloced = UAO_FLAG_KERNSWAP;
    435        1.66      yamt 		refs = 0xdeadbeaf; /* XXX: gcc */
    436        1.46       chs 	} else {
    437       1.113     rmind 		aobj = kmem_alloc(sizeof(struct uvm_aobj), KM_SLEEP);
    438         1.5       mrg 		aobj->u_pages = pages;
    439        1.46       chs 		aobj->u_flags = 0;
    440        1.66      yamt 		refs = 1;
    441         1.5       mrg 	}
    442         1.1       mrg 
    443         1.5       mrg 	/*
    444  1.116.12.2       tls 	 * no freelist by default
    445  1.116.12.2       tls 	 */
    446  1.116.12.2       tls 
    447  1.116.12.2       tls 	aobj->u_freelist = VM_NFREELIST;
    448  1.116.12.2       tls 
    449  1.116.12.2       tls 	/*
    450         1.5       mrg  	 * allocate hash/array if necessary
    451         1.5       mrg  	 *
    452         1.5       mrg  	 * note: in the KERNSWAP case no need to worry about locking since
    453         1.5       mrg  	 * we are still booting we should be the only thread around.
    454         1.5       mrg  	 */
    455        1.46       chs 
    456         1.5       mrg 	if (flags == 0 || (flags & UAO_FLAG_KERNSWAP) != 0) {
    457        1.72      yamt #if defined(VMSWAP)
    458       1.104     rmind 		const int kernswap = (flags & UAO_FLAG_KERNSWAP) != 0;
    459         1.5       mrg 
    460         1.5       mrg 		/* allocate hash table or array depending on object size */
    461        1.27       chs 		if (UAO_USES_SWHASH(aobj)) {
    462       1.104     rmind 			aobj->u_swhash = hashinit(UAO_SWHASH_BUCKETS(aobj),
    463       1.104     rmind 			    HASH_LIST, kernswap ? false : true,
    464       1.104     rmind 			    &aobj->u_swhashmask);
    465         1.5       mrg 			if (aobj->u_swhash == NULL)
    466         1.5       mrg 				panic("uao_create: hashinit swhash failed");
    467         1.5       mrg 		} else {
    468       1.104     rmind 			aobj->u_swslots = kmem_zalloc(pages * sizeof(int),
    469       1.104     rmind 			    kernswap ? KM_NOSLEEP : KM_SLEEP);
    470         1.5       mrg 			if (aobj->u_swslots == NULL)
    471       1.114     rmind 				panic("uao_create: swslots allocation failed");
    472         1.5       mrg 		}
    473        1.72      yamt #endif /* defined(VMSWAP) */
    474         1.5       mrg 
    475         1.5       mrg 		if (flags) {
    476         1.5       mrg 			aobj->u_flags &= ~UAO_FLAG_NOSWAP; /* clear noswap */
    477  1.116.12.1       tls 			return &aobj->u_obj;
    478         1.5       mrg 		}
    479         1.5       mrg 	}
    480         1.5       mrg 
    481         1.5       mrg 	/*
    482       1.115     rmind 	 * Initialise UVM object.
    483       1.115     rmind 	 */
    484        1.46       chs 
    485       1.115     rmind 	const bool kernobj = (flags & UAO_FLAG_KERNOBJ) != 0;
    486       1.115     rmind 	uvm_obj_init(&aobj->u_obj, &aobj_pager, !kernobj, refs);
    487       1.115     rmind 	if (__predict_false(kernobj)) {
    488       1.115     rmind 		/* Initialisation only once, for UAO_FLAG_KERNOBJ. */
    489       1.115     rmind 		mutex_init(&kernel_object_lock, MUTEX_DEFAULT, IPL_NONE);
    490       1.115     rmind 		uvm_obj_setlock(&aobj->u_obj, &kernel_object_lock);
    491       1.115     rmind 	}
    492         1.1       mrg 
    493         1.5       mrg 	/*
    494         1.5       mrg  	 * now that aobj is ready, add it to the global list
    495         1.5       mrg  	 */
    496        1.46       chs 
    497        1.90        ad 	mutex_enter(&uao_list_lock);
    498         1.5       mrg 	LIST_INSERT_HEAD(&uao_list, aobj, u_list);
    499        1.90        ad 	mutex_exit(&uao_list_lock);
    500         1.5       mrg 	return(&aobj->u_obj);
    501         1.1       mrg }
    502         1.1       mrg 
    503         1.1       mrg /*
    504  1.116.12.2       tls  * uao_set_pgfl: allocate pages only from the specified freelist.
    505  1.116.12.2       tls  *
    506  1.116.12.2       tls  * => must be called before any pages are allocated for the object.
    507  1.116.12.2       tls  * => reset by setting it to VM_NFREELIST, meaning any freelist.
    508  1.116.12.2       tls  */
    509  1.116.12.2       tls 
    510  1.116.12.2       tls void
    511  1.116.12.2       tls uao_set_pgfl(struct uvm_object *uobj, int freelist)
    512  1.116.12.2       tls {
    513  1.116.12.2       tls 	struct uvm_aobj *aobj = (struct uvm_aobj *)uobj;
    514  1.116.12.2       tls 
    515  1.116.12.2       tls 	KASSERTMSG((0 <= freelist), "invalid freelist %d", freelist);
    516  1.116.12.2       tls 	KASSERTMSG((freelist <= VM_NFREELIST), "invalid freelist %d",
    517  1.116.12.2       tls 	    freelist);
    518  1.116.12.2       tls 
    519  1.116.12.2       tls 	aobj->u_freelist = freelist;
    520  1.116.12.2       tls }
    521  1.116.12.2       tls 
    522  1.116.12.2       tls /*
    523  1.116.12.2       tls  * uao_pagealloc: allocate a page for aobj.
    524  1.116.12.2       tls  */
    525  1.116.12.2       tls 
    526  1.116.12.2       tls static inline struct vm_page *
    527  1.116.12.2       tls uao_pagealloc(struct uvm_object *uobj, voff_t offset, int flags)
    528  1.116.12.2       tls {
    529  1.116.12.2       tls 	struct uvm_aobj *aobj = (struct uvm_aobj *)uobj;
    530  1.116.12.2       tls 
    531  1.116.12.2       tls 	if (__predict_true(aobj->u_freelist == VM_NFREELIST))
    532  1.116.12.2       tls 		return uvm_pagealloc(uobj, offset, NULL, flags);
    533  1.116.12.2       tls 	else
    534  1.116.12.2       tls 		return uvm_pagealloc_strat(uobj, offset, NULL, flags,
    535  1.116.12.2       tls 		    UVM_PGA_STRAT_ONLY, aobj->u_freelist);
    536  1.116.12.2       tls }
    537  1.116.12.2       tls 
    538  1.116.12.2       tls /*
    539         1.1       mrg  * uao_init: set up aobj pager subsystem
    540         1.1       mrg  *
    541         1.1       mrg  * => called at boot time from uvm_pager_init()
    542         1.1       mrg  */
    543        1.46       chs 
    544        1.27       chs void
    545        1.46       chs uao_init(void)
    546         1.5       mrg {
    547        1.12   thorpej 	static int uao_initialized;
    548        1.12   thorpej 
    549        1.12   thorpej 	if (uao_initialized)
    550        1.12   thorpej 		return;
    551        1.87   thorpej 	uao_initialized = true;
    552         1.5       mrg 	LIST_INIT(&uao_list);
    553        1.96        ad 	mutex_init(&uao_list_lock, MUTEX_DEFAULT, IPL_NONE);
    554       1.107     pooka 	pool_init(&uao_swhash_elt_pool, sizeof(struct uao_swhash_elt),
    555       1.107     pooka 	    0, 0, 0, "uaoeltpl", NULL, IPL_VM);
    556         1.1       mrg }
    557         1.1       mrg 
    558         1.1       mrg /*
    559  1.116.12.1       tls  * uao_reference: hold a reference to an anonymous UVM object.
    560         1.1       mrg  */
    561         1.5       mrg void
    562        1.67   thorpej uao_reference(struct uvm_object *uobj)
    563         1.1       mrg {
    564  1.116.12.1       tls 	/* Kernel object is persistent. */
    565  1.116.12.1       tls 	if (UVM_OBJ_IS_KERN_OBJECT(uobj)) {
    566         1.5       mrg 		return;
    567  1.116.12.1       tls 	}
    568  1.116.12.1       tls 	atomic_inc_uint(&uobj->uo_refs);
    569         1.1       mrg }
    570         1.1       mrg 
    571         1.1       mrg /*
    572  1.116.12.1       tls  * uao_detach: drop a reference to an anonymous UVM object.
    573         1.1       mrg  */
    574         1.5       mrg void
    575        1.67   thorpej uao_detach(struct uvm_object *uobj)
    576         1.5       mrg {
    577         1.5       mrg 	struct uvm_aobj *aobj = (struct uvm_aobj *)uobj;
    578        1.46       chs 	struct vm_page *pg;
    579  1.116.12.1       tls 
    580         1.5       mrg 	UVMHIST_FUNC("uao_detach"); UVMHIST_CALLED(maphist);
    581         1.1       mrg 
    582         1.5       mrg 	/*
    583  1.116.12.1       tls 	 * Detaching from kernel object is a NOP.
    584  1.116.12.1       tls 	 */
    585        1.46       chs 
    586  1.116.12.1       tls 	if (UVM_OBJ_IS_KERN_OBJECT(uobj))
    587         1.5       mrg 		return;
    588  1.116.12.1       tls 
    589  1.116.12.1       tls 	/*
    590  1.116.12.1       tls 	 * Drop the reference.  If it was the last one, destroy the object.
    591  1.116.12.1       tls 	 */
    592         1.5       mrg 
    593  1.116.12.3  jdolecek 	KASSERT(uobj->uo_refs > 0);
    594  1.116.12.3  jdolecek 	UVMHIST_LOG(maphist,"  (uobj=0x%#jx)  ref=%jd",
    595  1.116.12.3  jdolecek 	    (uintptr_t)uobj, uobj->uo_refs, 0, 0);
    596  1.116.12.1       tls 	if (atomic_dec_uint_nv(&uobj->uo_refs) > 0) {
    597         1.5       mrg 		UVMHIST_LOG(maphist, "<- done (rc>0)", 0,0,0,0);
    598         1.5       mrg 		return;
    599         1.5       mrg 	}
    600         1.5       mrg 
    601         1.5       mrg 	/*
    602  1.116.12.1       tls 	 * Remove the aobj from the global list.
    603  1.116.12.1       tls 	 */
    604        1.46       chs 
    605        1.92        ad 	mutex_enter(&uao_list_lock);
    606         1.5       mrg 	LIST_REMOVE(aobj, u_list);
    607        1.92        ad 	mutex_exit(&uao_list_lock);
    608         1.5       mrg 
    609         1.5       mrg 	/*
    610  1.116.12.1       tls 	 * Free all the pages left in the aobj.  For each page, when the
    611  1.116.12.1       tls 	 * page is no longer busy (and thus after any disk I/O that it is
    612  1.116.12.1       tls 	 * involved in is complete), release any swap resources and free
    613  1.116.12.1       tls 	 * the page itself.
    614  1.116.12.1       tls 	 */
    615        1.46       chs 
    616  1.116.12.1       tls 	mutex_enter(uobj->vmobjlock);
    617        1.96        ad 	mutex_enter(&uvm_pageqlock);
    618        1.46       chs 	while ((pg = TAILQ_FIRST(&uobj->memq)) != NULL) {
    619        1.46       chs 		pmap_page_protect(pg, VM_PROT_NONE);
    620         1.5       mrg 		if (pg->flags & PG_BUSY) {
    621        1.46       chs 			pg->flags |= PG_WANTED;
    622        1.96        ad 			mutex_exit(&uvm_pageqlock);
    623       1.115     rmind 			UVM_UNLOCK_AND_WAIT(pg, uobj->vmobjlock, false,
    624        1.46       chs 			    "uao_det", 0);
    625       1.115     rmind 			mutex_enter(uobj->vmobjlock);
    626        1.96        ad 			mutex_enter(&uvm_pageqlock);
    627         1.5       mrg 			continue;
    628         1.5       mrg 		}
    629        1.18       chs 		uao_dropswap(&aobj->u_obj, pg->offset >> PAGE_SHIFT);
    630         1.5       mrg 		uvm_pagefree(pg);
    631         1.5       mrg 	}
    632        1.96        ad 	mutex_exit(&uvm_pageqlock);
    633         1.1       mrg 
    634         1.5       mrg 	/*
    635  1.116.12.1       tls 	 * Finally, free the anonymous UVM object itself.
    636  1.116.12.1       tls 	 */
    637         1.1       mrg 
    638         1.5       mrg 	uao_free(aobj);
    639         1.5       mrg }
    640         1.1       mrg 
    641         1.1       mrg /*
    642        1.46       chs  * uao_put: flush pages out of a uvm object
    643        1.22   thorpej  *
    644        1.22   thorpej  * => object should be locked by caller.  we may _unlock_ the object
    645        1.22   thorpej  *	if (and only if) we need to clean a page (PGO_CLEANIT).
    646        1.22   thorpej  *	XXXJRT Currently, however, we don't.  In the case of cleaning
    647        1.22   thorpej  *	XXXJRT a page, we simply just deactivate it.  Should probably
    648        1.22   thorpej  *	XXXJRT handle this better, in the future (although "flushing"
    649        1.22   thorpej  *	XXXJRT anonymous memory isn't terribly important).
    650        1.22   thorpej  * => if PGO_CLEANIT is not set, then we will neither unlock the object
    651        1.22   thorpej  *	or block.
    652        1.22   thorpej  * => if PGO_ALLPAGE is set, then all pages in the object are valid targets
    653        1.22   thorpej  *	for flushing.
    654        1.22   thorpej  * => NOTE: we rely on the fact that the object's memq is a TAILQ and
    655        1.22   thorpej  *	that new pages are inserted on the tail end of the list.  thus,
    656        1.22   thorpej  *	we can make a complete pass through the object in one go by starting
    657        1.22   thorpej  *	at the head and working towards the tail (new pages are put in
    658        1.22   thorpej  *	front of us).
    659        1.22   thorpej  * => NOTE: we are allowed to lock the page queues, so the caller
    660        1.22   thorpej  *	must not be holding the lock on them [e.g. pagedaemon had
    661        1.22   thorpej  *	better not call us with the queues locked]
    662        1.86      matt  * => we return 0 unless we encountered some sort of I/O error
    663        1.22   thorpej  *	XXXJRT currently never happens, as we never directly initiate
    664        1.22   thorpej  *	XXXJRT I/O
    665        1.22   thorpej  *
    666        1.22   thorpej  * note on page traversal:
    667        1.22   thorpej  *	we can traverse the pages in an object either by going down the
    668        1.22   thorpej  *	linked list in "uobj->memq", or we can go over the address range
    669        1.22   thorpej  *	by page doing hash table lookups for each address.  depending
    670        1.22   thorpej  *	on how many pages are in the object it may be cheaper to do one
    671        1.22   thorpej  *	or the other.  we set "by_list" to true if we are using memq.
    672        1.22   thorpej  *	if the cost of a hash lookup was equal to the cost of the list
    673        1.22   thorpej  *	traversal we could compare the number of pages in the start->stop
    674        1.22   thorpej  *	range to the total number of pages in the object.  however, it
    675        1.22   thorpej  *	seems that a hash table lookup is more expensive than the linked
    676        1.22   thorpej  *	list traversal, so we multiply the number of pages in the
    677        1.22   thorpej  *	start->stop range by a penalty which we define below.
    678         1.1       mrg  */
    679        1.22   thorpej 
    680        1.68   thorpej static int
    681        1.67   thorpej uao_put(struct uvm_object *uobj, voff_t start, voff_t stop, int flags)
    682         1.5       mrg {
    683        1.46       chs 	struct uvm_aobj *aobj = (struct uvm_aobj *)uobj;
    684        1.51     enami 	struct vm_page *pg, *nextpg, curmp, endmp;
    685        1.85   thorpej 	bool by_list;
    686        1.28    kleink 	voff_t curoff;
    687        1.46       chs 	UVMHIST_FUNC("uao_put"); UVMHIST_CALLED(maphist);
    688        1.22   thorpej 
    689       1.115     rmind 	KASSERT(mutex_owned(uobj->vmobjlock));
    690        1.96        ad 
    691        1.46       chs 	curoff = 0;
    692        1.22   thorpej 	if (flags & PGO_ALLPAGES) {
    693        1.22   thorpej 		start = 0;
    694        1.22   thorpej 		stop = aobj->u_pages << PAGE_SHIFT;
    695        1.86      matt 		by_list = true;		/* always go by the list */
    696        1.22   thorpej 	} else {
    697        1.22   thorpej 		start = trunc_page(start);
    698        1.71      yamt 		if (stop == 0) {
    699        1.71      yamt 			stop = aobj->u_pages << PAGE_SHIFT;
    700        1.71      yamt 		} else {
    701        1.71      yamt 			stop = round_page(stop);
    702        1.71      yamt 		}
    703        1.22   thorpej 		if (stop > (aobj->u_pages << PAGE_SHIFT)) {
    704        1.22   thorpej 			printf("uao_flush: strange, got an out of range "
    705        1.22   thorpej 			    "flush (fixed)\n");
    706        1.22   thorpej 			stop = aobj->u_pages << PAGE_SHIFT;
    707        1.22   thorpej 		}
    708        1.22   thorpej 		by_list = (uobj->uo_npages <=
    709       1.105      yamt 		    ((stop - start) >> PAGE_SHIFT) * UVM_PAGE_TREE_PENALTY);
    710        1.22   thorpej 	}
    711        1.22   thorpej 	UVMHIST_LOG(maphist,
    712  1.116.12.3  jdolecek 	    " flush start=0x%jx, stop=0x%jx, by_list=%jd, flags=0x%jx",
    713        1.22   thorpej 	    start, stop, by_list, flags);
    714         1.1       mrg 
    715         1.5       mrg 	/*
    716        1.22   thorpej 	 * Don't need to do any work here if we're not freeing
    717        1.22   thorpej 	 * or deactivating pages.
    718        1.22   thorpej 	 */
    719        1.46       chs 
    720        1.22   thorpej 	if ((flags & (PGO_DEACTIVATE|PGO_FREE)) == 0) {
    721       1.115     rmind 		mutex_exit(uobj->vmobjlock);
    722        1.46       chs 		return 0;
    723        1.22   thorpej 	}
    724        1.22   thorpej 
    725         1.5       mrg 	/*
    726        1.51     enami 	 * Initialize the marker pages.  See the comment in
    727        1.51     enami 	 * genfs_putpages() also.
    728        1.51     enami 	 */
    729        1.51     enami 
    730       1.110   hannken 	curmp.flags = PG_MARKER;
    731       1.110   hannken 	endmp.flags = PG_MARKER;
    732        1.51     enami 
    733        1.51     enami 	/*
    734        1.46       chs 	 * now do it.  note: we must update nextpg in the body of loop or we
    735        1.51     enami 	 * will get stuck.  we need to use nextpg if we'll traverse the list
    736        1.51     enami 	 * because we may free "pg" before doing the next loop.
    737        1.21   thorpej 	 */
    738        1.22   thorpej 
    739        1.22   thorpej 	if (by_list) {
    740       1.102        ad 		TAILQ_INSERT_TAIL(&uobj->memq, &endmp, listq.queue);
    741        1.51     enami 		nextpg = TAILQ_FIRST(&uobj->memq);
    742        1.22   thorpej 	} else {
    743        1.22   thorpej 		curoff = start;
    744        1.52       scw 		nextpg = NULL;	/* Quell compiler warning */
    745        1.22   thorpej 	}
    746        1.22   thorpej 
    747        1.99        ad 	/* locked: uobj */
    748        1.51     enami 	for (;;) {
    749        1.22   thorpej 		if (by_list) {
    750        1.51     enami 			pg = nextpg;
    751        1.51     enami 			if (pg == &endmp)
    752        1.51     enami 				break;
    753       1.102        ad 			nextpg = TAILQ_NEXT(pg, listq.queue);
    754       1.110   hannken 			if (pg->flags & PG_MARKER)
    755       1.110   hannken 				continue;
    756        1.46       chs 			if (pg->offset < start || pg->offset >= stop)
    757        1.22   thorpej 				continue;
    758        1.22   thorpej 		} else {
    759        1.51     enami 			if (curoff < stop) {
    760        1.51     enami 				pg = uvm_pagelookup(uobj, curoff);
    761        1.51     enami 				curoff += PAGE_SIZE;
    762        1.51     enami 			} else
    763        1.51     enami 				break;
    764        1.46       chs 			if (pg == NULL)
    765        1.22   thorpej 				continue;
    766        1.22   thorpej 		}
    767        1.98      yamt 
    768        1.98      yamt 		/*
    769        1.98      yamt 		 * wait and try again if the page is busy.
    770        1.98      yamt 		 */
    771        1.98      yamt 
    772        1.98      yamt 		if (pg->flags & PG_BUSY) {
    773        1.98      yamt 			if (by_list) {
    774       1.102        ad 				TAILQ_INSERT_BEFORE(pg, &curmp, listq.queue);
    775        1.98      yamt 			}
    776        1.98      yamt 			pg->flags |= PG_WANTED;
    777       1.115     rmind 			UVM_UNLOCK_AND_WAIT(pg, uobj->vmobjlock, 0,
    778        1.98      yamt 			    "uao_put", 0);
    779       1.115     rmind 			mutex_enter(uobj->vmobjlock);
    780        1.98      yamt 			if (by_list) {
    781       1.102        ad 				nextpg = TAILQ_NEXT(&curmp, listq.queue);
    782        1.98      yamt 				TAILQ_REMOVE(&uobj->memq, &curmp,
    783       1.102        ad 				    listq.queue);
    784        1.98      yamt 			} else
    785        1.98      yamt 				curoff -= PAGE_SIZE;
    786        1.98      yamt 			continue;
    787        1.98      yamt 		}
    788        1.98      yamt 
    789        1.46       chs 		switch (flags & (PGO_CLEANIT|PGO_FREE|PGO_DEACTIVATE)) {
    790        1.41       chs 
    791        1.22   thorpej 		/*
    792        1.22   thorpej 		 * XXX In these first 3 cases, we always just
    793        1.22   thorpej 		 * XXX deactivate the page.  We may want to
    794        1.22   thorpej 		 * XXX handle the different cases more specifically
    795        1.22   thorpej 		 * XXX in the future.
    796        1.22   thorpej 		 */
    797        1.46       chs 
    798        1.22   thorpej 		case PGO_CLEANIT|PGO_FREE:
    799        1.22   thorpej 		case PGO_CLEANIT|PGO_DEACTIVATE:
    800        1.22   thorpej 		case PGO_DEACTIVATE:
    801        1.25   thorpej  deactivate_it:
    802        1.98      yamt 			mutex_enter(&uvm_pageqlock);
    803        1.83      yamt 			/* skip the page if it's wired */
    804        1.98      yamt 			if (pg->wire_count == 0) {
    805        1.98      yamt 				uvm_pagedeactivate(pg);
    806        1.98      yamt 			}
    807        1.98      yamt 			mutex_exit(&uvm_pageqlock);
    808        1.98      yamt 			break;
    809        1.22   thorpej 
    810        1.22   thorpej 		case PGO_FREE:
    811        1.25   thorpej 			/*
    812        1.25   thorpej 			 * If there are multiple references to
    813        1.25   thorpej 			 * the object, just deactivate the page.
    814        1.25   thorpej 			 */
    815        1.46       chs 
    816        1.25   thorpej 			if (uobj->uo_refs > 1)
    817        1.25   thorpej 				goto deactivate_it;
    818        1.25   thorpej 
    819        1.22   thorpej 			/*
    820        1.98      yamt 			 * free the swap slot and the page.
    821        1.22   thorpej 			 */
    822        1.46       chs 
    823        1.46       chs 			pmap_page_protect(pg, VM_PROT_NONE);
    824        1.75      yamt 
    825        1.75      yamt 			/*
    826        1.75      yamt 			 * freeing swapslot here is not strictly necessary.
    827        1.75      yamt 			 * however, leaving it here doesn't save much
    828        1.75      yamt 			 * because we need to update swap accounting anyway.
    829        1.75      yamt 			 */
    830        1.75      yamt 
    831        1.46       chs 			uao_dropswap(uobj, pg->offset >> PAGE_SHIFT);
    832        1.98      yamt 			mutex_enter(&uvm_pageqlock);
    833        1.46       chs 			uvm_pagefree(pg);
    834        1.98      yamt 			mutex_exit(&uvm_pageqlock);
    835        1.98      yamt 			break;
    836        1.98      yamt 
    837        1.98      yamt 		default:
    838        1.98      yamt 			panic("%s: impossible", __func__);
    839        1.22   thorpej 		}
    840        1.22   thorpej 	}
    841        1.51     enami 	if (by_list) {
    842       1.102        ad 		TAILQ_REMOVE(&uobj->memq, &endmp, listq.queue);
    843        1.89        ad 	}
    844       1.115     rmind 	mutex_exit(uobj->vmobjlock);
    845        1.46       chs 	return 0;
    846         1.1       mrg }
    847         1.1       mrg 
    848         1.1       mrg /*
    849         1.1       mrg  * uao_get: fetch me a page
    850         1.1       mrg  *
    851         1.1       mrg  * we have three cases:
    852         1.1       mrg  * 1: page is resident     -> just return the page.
    853         1.1       mrg  * 2: page is zero-fill    -> allocate a new page and zero it.
    854         1.1       mrg  * 3: page is swapped out  -> fetch the page from swap.
    855         1.1       mrg  *
    856         1.1       mrg  * cases 1 and 2 can be handled with PGO_LOCKED, case 3 cannot.
    857         1.1       mrg  * so, if the "center" page hits case 3 (or any page, with PGO_ALLPAGES),
    858        1.40       chs  * then we will need to return EBUSY.
    859         1.1       mrg  *
    860         1.1       mrg  * => prefer map unlocked (not required)
    861         1.1       mrg  * => object must be locked!  we will _unlock_ it before starting any I/O.
    862         1.1       mrg  * => flags: PGO_ALLPAGES: get all of the pages
    863         1.1       mrg  *           PGO_LOCKED: fault data structures are locked
    864         1.1       mrg  * => NOTE: offset is the offset of pps[0], _NOT_ pps[centeridx]
    865         1.1       mrg  * => NOTE: caller must check for released pages!!
    866         1.1       mrg  */
    867        1.46       chs 
    868         1.5       mrg static int
    869        1.67   thorpej uao_get(struct uvm_object *uobj, voff_t offset, struct vm_page **pps,
    870        1.82      yamt     int *npagesp, int centeridx, vm_prot_t access_type, int advice, int flags)
    871         1.5       mrg {
    872        1.28    kleink 	voff_t current_offset;
    873        1.52       scw 	struct vm_page *ptmp = NULL;	/* Quell compiler warning */
    874        1.72      yamt 	int lcv, gotpages, maxpages, swslot, pageidx;
    875        1.85   thorpej 	bool done;
    876         1.5       mrg 	UVMHIST_FUNC("uao_get"); UVMHIST_CALLED(pdhist);
    877         1.5       mrg 
    878  1.116.12.3  jdolecek 	UVMHIST_LOG(pdhist, "aobj=%#jx offset=%jd, flags=%jd",
    879  1.116.12.3  jdolecek 		    (uintptr_t)uobj, offset, flags,0);
    880        1.37       chs 
    881         1.5       mrg 	/*
    882         1.5       mrg  	 * get number of pages
    883         1.5       mrg  	 */
    884        1.46       chs 
    885         1.5       mrg 	maxpages = *npagesp;
    886         1.5       mrg 
    887         1.5       mrg 	/*
    888         1.5       mrg  	 * step 1: handled the case where fault data structures are locked.
    889         1.5       mrg  	 */
    890         1.1       mrg 
    891         1.5       mrg 	if (flags & PGO_LOCKED) {
    892        1.46       chs 
    893         1.5       mrg 		/*
    894         1.5       mrg  		 * step 1a: get pages that are already resident.   only do
    895         1.5       mrg 		 * this if the data structures are locked (i.e. the first
    896         1.5       mrg 		 * time through).
    897         1.5       mrg  		 */
    898         1.5       mrg 
    899        1.87   thorpej 		done = true;	/* be optimistic */
    900         1.5       mrg 		gotpages = 0;	/* # of pages we got so far */
    901         1.5       mrg 		for (lcv = 0, current_offset = offset ; lcv < maxpages ;
    902         1.5       mrg 		    lcv++, current_offset += PAGE_SIZE) {
    903         1.5       mrg 			/* do we care about this page?  if not, skip it */
    904         1.5       mrg 			if (pps[lcv] == PGO_DONTCARE)
    905         1.5       mrg 				continue;
    906         1.5       mrg 			ptmp = uvm_pagelookup(uobj, current_offset);
    907         1.5       mrg 
    908         1.5       mrg 			/*
    909        1.30   thorpej  			 * if page is new, attempt to allocate the page,
    910        1.30   thorpej 			 * zero-fill'd.
    911         1.5       mrg  			 */
    912        1.46       chs 
    913  1.116.12.1       tls 			if (ptmp == NULL && uao_find_swslot(uobj,
    914        1.15       chs 			    current_offset >> PAGE_SHIFT) == 0) {
    915  1.116.12.2       tls 				ptmp = uao_pagealloc(uobj, current_offset,
    916  1.116.12.2       tls 				    UVM_FLAG_COLORMATCH|UVM_PGA_ZERO);
    917         1.5       mrg 				if (ptmp) {
    918         1.5       mrg 					/* new page */
    919        1.47       chs 					ptmp->flags &= ~(PG_FAKE);
    920         1.5       mrg 					ptmp->pqflags |= PQ_AOBJ;
    921        1.47       chs 					goto gotpage;
    922         1.5       mrg 				}
    923         1.5       mrg 			}
    924         1.5       mrg 
    925         1.5       mrg 			/*
    926        1.46       chs 			 * to be useful must get a non-busy page
    927         1.5       mrg 			 */
    928        1.46       chs 
    929        1.46       chs 			if (ptmp == NULL || (ptmp->flags & PG_BUSY) != 0) {
    930         1.5       mrg 				if (lcv == centeridx ||
    931         1.5       mrg 				    (flags & PGO_ALLPAGES) != 0)
    932         1.5       mrg 					/* need to do a wait or I/O! */
    933        1.87   thorpej 					done = false;
    934  1.116.12.3  jdolecek 				continue;
    935         1.5       mrg 			}
    936         1.5       mrg 
    937         1.5       mrg 			/*
    938         1.5       mrg 			 * useful page: busy/lock it and plug it in our
    939         1.5       mrg 			 * result array
    940         1.5       mrg 			 */
    941        1.46       chs 
    942         1.5       mrg 			/* caller must un-busy this page */
    943        1.41       chs 			ptmp->flags |= PG_BUSY;
    944         1.5       mrg 			UVM_PAGE_OWN(ptmp, "uao_get1");
    945        1.47       chs gotpage:
    946         1.5       mrg 			pps[lcv] = ptmp;
    947         1.5       mrg 			gotpages++;
    948        1.46       chs 		}
    949         1.5       mrg 
    950         1.5       mrg 		/*
    951         1.5       mrg  		 * step 1b: now we've either done everything needed or we
    952         1.5       mrg 		 * to unlock and do some waiting or I/O.
    953         1.5       mrg  		 */
    954         1.5       mrg 
    955  1.116.12.3  jdolecek 		UVMHIST_LOG(pdhist, "<- done (done=%jd)", done, 0,0,0);
    956         1.5       mrg 		*npagesp = gotpages;
    957         1.5       mrg 		if (done)
    958        1.46       chs 			return 0;
    959         1.5       mrg 		else
    960        1.46       chs 			return EBUSY;
    961         1.1       mrg 	}
    962         1.1       mrg 
    963         1.5       mrg 	/*
    964         1.5       mrg  	 * step 2: get non-resident or busy pages.
    965         1.5       mrg  	 * object is locked.   data structures are unlocked.
    966         1.5       mrg  	 */
    967         1.5       mrg 
    968        1.76      yamt 	if ((flags & PGO_SYNCIO) == 0) {
    969        1.76      yamt 		goto done;
    970        1.76      yamt 	}
    971        1.76      yamt 
    972         1.5       mrg 	for (lcv = 0, current_offset = offset ; lcv < maxpages ;
    973         1.5       mrg 	    lcv++, current_offset += PAGE_SIZE) {
    974        1.27       chs 
    975         1.5       mrg 		/*
    976         1.5       mrg 		 * - skip over pages we've already gotten or don't want
    977         1.5       mrg 		 * - skip over pages we don't _have_ to get
    978         1.5       mrg 		 */
    979        1.27       chs 
    980         1.5       mrg 		if (pps[lcv] != NULL ||
    981         1.5       mrg 		    (lcv != centeridx && (flags & PGO_ALLPAGES) == 0))
    982         1.5       mrg 			continue;
    983         1.5       mrg 
    984        1.27       chs 		pageidx = current_offset >> PAGE_SHIFT;
    985        1.27       chs 
    986         1.5       mrg 		/*
    987         1.5       mrg  		 * we have yet to locate the current page (pps[lcv]).   we
    988         1.5       mrg 		 * first look for a page that is already at the current offset.
    989         1.5       mrg 		 * if we find a page, we check to see if it is busy or
    990         1.5       mrg 		 * released.  if that is the case, then we sleep on the page
    991         1.5       mrg 		 * until it is no longer busy or released and repeat the lookup.
    992         1.5       mrg 		 * if the page we found is neither busy nor released, then we
    993         1.5       mrg 		 * busy it (so we own it) and plug it into pps[lcv].   this
    994         1.5       mrg 		 * 'break's the following while loop and indicates we are
    995         1.5       mrg 		 * ready to move on to the next page in the "lcv" loop above.
    996         1.5       mrg  		 *
    997         1.5       mrg  		 * if we exit the while loop with pps[lcv] still set to NULL,
    998         1.5       mrg 		 * then it means that we allocated a new busy/fake/clean page
    999         1.5       mrg 		 * ptmp in the object and we need to do I/O to fill in the data.
   1000         1.5       mrg  		 */
   1001         1.5       mrg 
   1002         1.5       mrg 		/* top of "pps" while loop */
   1003         1.5       mrg 		while (pps[lcv] == NULL) {
   1004         1.5       mrg 			/* look for a resident page */
   1005         1.5       mrg 			ptmp = uvm_pagelookup(uobj, current_offset);
   1006         1.5       mrg 
   1007         1.5       mrg 			/* not resident?   allocate one now (if we can) */
   1008         1.5       mrg 			if (ptmp == NULL) {
   1009         1.5       mrg 
   1010  1.116.12.2       tls 				ptmp = uao_pagealloc(uobj, current_offset, 0);
   1011         1.5       mrg 
   1012         1.5       mrg 				/* out of RAM? */
   1013         1.5       mrg 				if (ptmp == NULL) {
   1014       1.115     rmind 					mutex_exit(uobj->vmobjlock);
   1015         1.5       mrg 					UVMHIST_LOG(pdhist,
   1016         1.5       mrg 					    "sleeping, ptmp == NULL\n",0,0,0,0);
   1017         1.5       mrg 					uvm_wait("uao_getpage");
   1018       1.115     rmind 					mutex_enter(uobj->vmobjlock);
   1019        1.41       chs 					continue;
   1020         1.5       mrg 				}
   1021         1.5       mrg 
   1022         1.5       mrg 				/*
   1023         1.5       mrg 				 * safe with PQ's unlocked: because we just
   1024         1.5       mrg 				 * alloc'd the page
   1025         1.5       mrg 				 */
   1026        1.46       chs 
   1027         1.5       mrg 				ptmp->pqflags |= PQ_AOBJ;
   1028         1.5       mrg 
   1029        1.41       chs 				/*
   1030         1.5       mrg 				 * got new page ready for I/O.  break pps while
   1031         1.5       mrg 				 * loop.  pps[lcv] is still NULL.
   1032         1.5       mrg 				 */
   1033        1.46       chs 
   1034         1.5       mrg 				break;
   1035         1.5       mrg 			}
   1036         1.5       mrg 
   1037         1.5       mrg 			/* page is there, see if we need to wait on it */
   1038        1.46       chs 			if ((ptmp->flags & PG_BUSY) != 0) {
   1039         1.5       mrg 				ptmp->flags |= PG_WANTED;
   1040         1.5       mrg 				UVMHIST_LOG(pdhist,
   1041  1.116.12.3  jdolecek 				    "sleeping, ptmp->flags 0x%jx\n",
   1042         1.5       mrg 				    ptmp->flags,0,0,0);
   1043       1.115     rmind 				UVM_UNLOCK_AND_WAIT(ptmp, uobj->vmobjlock,
   1044        1.87   thorpej 				    false, "uao_get", 0);
   1045       1.115     rmind 				mutex_enter(uobj->vmobjlock);
   1046        1.46       chs 				continue;
   1047         1.5       mrg 			}
   1048        1.41       chs 
   1049        1.41       chs 			/*
   1050         1.5       mrg  			 * if we get here then the page has become resident and
   1051         1.5       mrg 			 * unbusy between steps 1 and 2.  we busy it now (so we
   1052         1.5       mrg 			 * own it) and set pps[lcv] (so that we exit the while
   1053         1.5       mrg 			 * loop).
   1054         1.5       mrg  			 */
   1055        1.46       chs 
   1056         1.5       mrg 			/* we own it, caller must un-busy */
   1057         1.5       mrg 			ptmp->flags |= PG_BUSY;
   1058         1.5       mrg 			UVM_PAGE_OWN(ptmp, "uao_get2");
   1059         1.5       mrg 			pps[lcv] = ptmp;
   1060         1.5       mrg 		}
   1061         1.5       mrg 
   1062         1.5       mrg 		/*
   1063         1.5       mrg  		 * if we own the valid page at the correct offset, pps[lcv] will
   1064         1.5       mrg  		 * point to it.   nothing more to do except go to the next page.
   1065         1.5       mrg  		 */
   1066        1.46       chs 
   1067         1.5       mrg 		if (pps[lcv])
   1068         1.5       mrg 			continue;			/* next lcv */
   1069         1.5       mrg 
   1070         1.5       mrg 		/*
   1071        1.41       chs  		 * we have a "fake/busy/clean" page that we just allocated.
   1072         1.5       mrg  		 * do the needed "i/o", either reading from swap or zeroing.
   1073         1.5       mrg  		 */
   1074        1.46       chs 
   1075  1.116.12.1       tls 		swslot = uao_find_swslot(uobj, pageidx);
   1076         1.5       mrg 
   1077         1.5       mrg 		/*
   1078         1.5       mrg  		 * just zero the page if there's nothing in swap.
   1079         1.5       mrg  		 */
   1080        1.46       chs 
   1081        1.46       chs 		if (swslot == 0) {
   1082        1.46       chs 
   1083         1.5       mrg 			/*
   1084         1.5       mrg 			 * page hasn't existed before, just zero it.
   1085         1.5       mrg 			 */
   1086        1.46       chs 
   1087         1.5       mrg 			uvm_pagezero(ptmp);
   1088        1.27       chs 		} else {
   1089        1.72      yamt #if defined(VMSWAP)
   1090        1.72      yamt 			int error;
   1091        1.72      yamt 
   1092  1.116.12.3  jdolecek 			UVMHIST_LOG(pdhist, "pagein from swslot %jd",
   1093         1.5       mrg 			     swslot, 0,0,0);
   1094         1.5       mrg 
   1095         1.5       mrg 			/*
   1096         1.5       mrg 			 * page in the swapped-out page.
   1097         1.5       mrg 			 * unlock object for i/o, relock when done.
   1098         1.5       mrg 			 */
   1099        1.46       chs 
   1100       1.115     rmind 			mutex_exit(uobj->vmobjlock);
   1101        1.46       chs 			error = uvm_swap_get(ptmp, swslot, PGO_SYNCIO);
   1102       1.115     rmind 			mutex_enter(uobj->vmobjlock);
   1103         1.5       mrg 
   1104         1.5       mrg 			/*
   1105         1.5       mrg 			 * I/O done.  check for errors.
   1106         1.5       mrg 			 */
   1107        1.46       chs 
   1108        1.46       chs 			if (error != 0) {
   1109  1.116.12.3  jdolecek 				UVMHIST_LOG(pdhist, "<- done (error=%jd)",
   1110        1.46       chs 				    error,0,0,0);
   1111         1.5       mrg 				if (ptmp->flags & PG_WANTED)
   1112        1.24   thorpej 					wakeup(ptmp);
   1113        1.27       chs 
   1114        1.27       chs 				/*
   1115        1.27       chs 				 * remove the swap slot from the aobj
   1116        1.27       chs 				 * and mark the aobj as having no real slot.
   1117        1.27       chs 				 * don't free the swap slot, thus preventing
   1118        1.27       chs 				 * it from being used again.
   1119        1.27       chs 				 */
   1120        1.46       chs 
   1121  1.116.12.1       tls 				swslot = uao_set_swslot(uobj, pageidx,
   1122  1.116.12.1       tls 				    SWSLOT_BAD);
   1123        1.57        pk 				if (swslot > 0) {
   1124        1.45       chs 					uvm_swap_markbad(swslot, 1);
   1125        1.45       chs 				}
   1126        1.27       chs 
   1127        1.96        ad 				mutex_enter(&uvm_pageqlock);
   1128         1.5       mrg 				uvm_pagefree(ptmp);
   1129        1.96        ad 				mutex_exit(&uvm_pageqlock);
   1130       1.115     rmind 				mutex_exit(uobj->vmobjlock);
   1131        1.46       chs 				return error;
   1132         1.5       mrg 			}
   1133        1.72      yamt #else /* defined(VMSWAP) */
   1134        1.72      yamt 			panic("%s: pagein", __func__);
   1135        1.72      yamt #endif /* defined(VMSWAP) */
   1136         1.5       mrg 		}
   1137         1.5       mrg 
   1138        1.78      yamt 		if ((access_type & VM_PROT_WRITE) == 0) {
   1139        1.78      yamt 			ptmp->flags |= PG_CLEAN;
   1140        1.78      yamt 			pmap_clear_modify(ptmp);
   1141        1.78      yamt 		}
   1142        1.78      yamt 
   1143        1.41       chs 		/*
   1144         1.5       mrg  		 * we got the page!   clear the fake flag (indicates valid
   1145         1.5       mrg 		 * data now in page) and plug into our result array.   note
   1146        1.41       chs 		 * that page is still busy.
   1147         1.5       mrg  		 *
   1148         1.5       mrg  		 * it is the callers job to:
   1149         1.5       mrg  		 * => check if the page is released
   1150         1.5       mrg  		 * => unbusy the page
   1151         1.5       mrg  		 * => activate the page
   1152         1.5       mrg  		 */
   1153         1.5       mrg 
   1154        1.46       chs 		ptmp->flags &= ~PG_FAKE;
   1155         1.5       mrg 		pps[lcv] = ptmp;
   1156        1.46       chs 	}
   1157         1.1       mrg 
   1158         1.1       mrg 	/*
   1159         1.5       mrg  	 * finally, unlock object and return.
   1160         1.5       mrg  	 */
   1161         1.1       mrg 
   1162        1.76      yamt done:
   1163       1.115     rmind 	mutex_exit(uobj->vmobjlock);
   1164         1.5       mrg 	UVMHIST_LOG(pdhist, "<- done (OK)",0,0,0,0);
   1165        1.46       chs 	return 0;
   1166         1.1       mrg }
   1167         1.1       mrg 
   1168        1.72      yamt #if defined(VMSWAP)
   1169        1.72      yamt 
   1170         1.1       mrg /*
   1171        1.18       chs  * uao_dropswap:  release any swap resources from this aobj page.
   1172        1.41       chs  *
   1173        1.18       chs  * => aobj must be locked or have a reference count of 0.
   1174        1.18       chs  */
   1175        1.18       chs 
   1176        1.18       chs void
   1177        1.67   thorpej uao_dropswap(struct uvm_object *uobj, int pageidx)
   1178        1.18       chs {
   1179        1.18       chs 	int slot;
   1180        1.18       chs 
   1181        1.18       chs 	slot = uao_set_swslot(uobj, pageidx, 0);
   1182        1.18       chs 	if (slot) {
   1183        1.18       chs 		uvm_swap_free(slot, 1);
   1184        1.18       chs 	}
   1185        1.27       chs }
   1186        1.27       chs 
   1187        1.27       chs /*
   1188        1.27       chs  * page in every page in every aobj that is paged-out to a range of swslots.
   1189        1.41       chs  *
   1190        1.27       chs  * => nothing should be locked.
   1191        1.87   thorpej  * => returns true if pagein was aborted due to lack of memory.
   1192        1.27       chs  */
   1193        1.46       chs 
   1194        1.85   thorpej bool
   1195        1.67   thorpej uao_swap_off(int startslot, int endslot)
   1196        1.27       chs {
   1197  1.116.12.1       tls 	struct uvm_aobj *aobj;
   1198        1.27       chs 
   1199        1.27       chs 	/*
   1200  1.116.12.1       tls 	 * Walk the list of all anonymous UVM objects.  Grab the first.
   1201        1.27       chs 	 */
   1202        1.90        ad 	mutex_enter(&uao_list_lock);
   1203  1.116.12.1       tls 	if ((aobj = LIST_FIRST(&uao_list)) == NULL) {
   1204  1.116.12.1       tls 		mutex_exit(&uao_list_lock);
   1205  1.116.12.1       tls 		return false;
   1206  1.116.12.1       tls 	}
   1207  1.116.12.1       tls 	uao_reference(&aobj->u_obj);
   1208        1.46       chs 
   1209  1.116.12.1       tls 	do {
   1210  1.116.12.1       tls 		struct uvm_aobj *nextaobj;
   1211  1.116.12.1       tls 		bool rv;
   1212        1.27       chs 
   1213        1.27       chs 		/*
   1214  1.116.12.1       tls 		 * Prefetch the next object and immediately hold a reference
   1215  1.116.12.1       tls 		 * on it, so neither the current nor the next entry could
   1216  1.116.12.1       tls 		 * disappear while we are iterating.
   1217        1.27       chs 		 */
   1218  1.116.12.1       tls 		if ((nextaobj = LIST_NEXT(aobj, u_list)) != NULL) {
   1219  1.116.12.1       tls 			uao_reference(&nextaobj->u_obj);
   1220  1.116.12.1       tls 		}
   1221        1.90        ad 		mutex_exit(&uao_list_lock);
   1222        1.27       chs 
   1223        1.27       chs 		/*
   1224  1.116.12.1       tls 		 * Page in all pages in the swap slot range.
   1225        1.27       chs 		 */
   1226  1.116.12.1       tls 		mutex_enter(aobj->u_obj.vmobjlock);
   1227        1.27       chs 		rv = uao_pagein(aobj, startslot, endslot);
   1228  1.116.12.1       tls 		mutex_exit(aobj->u_obj.vmobjlock);
   1229  1.116.12.1       tls 
   1230  1.116.12.1       tls 		/* Drop the reference of the current object. */
   1231  1.116.12.1       tls 		uao_detach(&aobj->u_obj);
   1232        1.27       chs 		if (rv) {
   1233  1.116.12.1       tls 			if (nextaobj) {
   1234  1.116.12.1       tls 				uao_detach(&nextaobj->u_obj);
   1235  1.116.12.1       tls 			}
   1236        1.27       chs 			return rv;
   1237        1.27       chs 		}
   1238        1.27       chs 
   1239  1.116.12.1       tls 		aobj = nextaobj;
   1240        1.90        ad 		mutex_enter(&uao_list_lock);
   1241  1.116.12.1       tls 	} while (aobj);
   1242        1.27       chs 
   1243        1.90        ad 	mutex_exit(&uao_list_lock);
   1244        1.87   thorpej 	return false;
   1245        1.27       chs }
   1246        1.27       chs 
   1247        1.27       chs /*
   1248        1.27       chs  * page in any pages from aobj in the given range.
   1249        1.27       chs  *
   1250        1.27       chs  * => aobj must be locked and is returned locked.
   1251        1.87   thorpej  * => returns true if pagein was aborted due to lack of memory.
   1252        1.27       chs  */
   1253        1.85   thorpej static bool
   1254        1.67   thorpej uao_pagein(struct uvm_aobj *aobj, int startslot, int endslot)
   1255        1.27       chs {
   1256        1.85   thorpej 	bool rv;
   1257        1.27       chs 
   1258        1.27       chs 	if (UAO_USES_SWHASH(aobj)) {
   1259        1.27       chs 		struct uao_swhash_elt *elt;
   1260        1.65  christos 		int buck;
   1261        1.27       chs 
   1262        1.27       chs restart:
   1263        1.65  christos 		for (buck = aobj->u_swhashmask; buck >= 0; buck--) {
   1264        1.65  christos 			for (elt = LIST_FIRST(&aobj->u_swhash[buck]);
   1265        1.27       chs 			     elt != NULL;
   1266        1.27       chs 			     elt = LIST_NEXT(elt, list)) {
   1267        1.27       chs 				int i;
   1268        1.27       chs 
   1269        1.27       chs 				for (i = 0; i < UAO_SWHASH_CLUSTER_SIZE; i++) {
   1270        1.27       chs 					int slot = elt->slots[i];
   1271        1.27       chs 
   1272        1.27       chs 					/*
   1273        1.27       chs 					 * if the slot isn't in range, skip it.
   1274        1.27       chs 					 */
   1275        1.46       chs 
   1276        1.41       chs 					if (slot < startslot ||
   1277        1.27       chs 					    slot >= endslot) {
   1278        1.27       chs 						continue;
   1279        1.27       chs 					}
   1280        1.27       chs 
   1281        1.27       chs 					/*
   1282        1.27       chs 					 * process the page,
   1283        1.27       chs 					 * the start over on this object
   1284        1.27       chs 					 * since the swhash elt
   1285        1.27       chs 					 * may have been freed.
   1286        1.27       chs 					 */
   1287        1.46       chs 
   1288        1.27       chs 					rv = uao_pagein_page(aobj,
   1289        1.27       chs 					  UAO_SWHASH_ELT_PAGEIDX_BASE(elt) + i);
   1290        1.27       chs 					if (rv) {
   1291        1.27       chs 						return rv;
   1292        1.27       chs 					}
   1293        1.27       chs 					goto restart;
   1294        1.27       chs 				}
   1295        1.27       chs 			}
   1296        1.27       chs 		}
   1297        1.27       chs 	} else {
   1298        1.27       chs 		int i;
   1299        1.27       chs 
   1300        1.27       chs 		for (i = 0; i < aobj->u_pages; i++) {
   1301        1.27       chs 			int slot = aobj->u_swslots[i];
   1302        1.27       chs 
   1303        1.27       chs 			/*
   1304        1.27       chs 			 * if the slot isn't in range, skip it
   1305        1.27       chs 			 */
   1306        1.46       chs 
   1307        1.27       chs 			if (slot < startslot || slot >= endslot) {
   1308        1.27       chs 				continue;
   1309        1.27       chs 			}
   1310        1.27       chs 
   1311        1.27       chs 			/*
   1312        1.27       chs 			 * process the page.
   1313        1.27       chs 			 */
   1314        1.46       chs 
   1315        1.27       chs 			rv = uao_pagein_page(aobj, i);
   1316        1.27       chs 			if (rv) {
   1317        1.27       chs 				return rv;
   1318        1.27       chs 			}
   1319        1.27       chs 		}
   1320        1.27       chs 	}
   1321        1.27       chs 
   1322        1.87   thorpej 	return false;
   1323        1.27       chs }
   1324        1.27       chs 
   1325        1.27       chs /*
   1326  1.116.12.1       tls  * uao_pagein_page: page in a single page from an anonymous UVM object.
   1327        1.27       chs  *
   1328  1.116.12.1       tls  * => Returns true if pagein was aborted due to lack of memory.
   1329  1.116.12.1       tls  * => Object must be locked and is returned locked.
   1330        1.27       chs  */
   1331        1.46       chs 
   1332        1.85   thorpej static bool
   1333        1.67   thorpej uao_pagein_page(struct uvm_aobj *aobj, int pageidx)
   1334        1.27       chs {
   1335  1.116.12.1       tls 	struct uvm_object *uobj = &aobj->u_obj;
   1336        1.27       chs 	struct vm_page *pg;
   1337        1.57        pk 	int rv, npages;
   1338        1.27       chs 
   1339        1.27       chs 	pg = NULL;
   1340        1.27       chs 	npages = 1;
   1341  1.116.12.1       tls 
   1342  1.116.12.1       tls 	KASSERT(mutex_owned(uobj->vmobjlock));
   1343  1.116.12.1       tls 	rv = uao_get(uobj, pageidx << PAGE_SHIFT, &pg, &npages,
   1344  1.116.12.1       tls 	    0, VM_PROT_READ | VM_PROT_WRITE, 0, PGO_SYNCIO);
   1345        1.27       chs 
   1346        1.27       chs 	/*
   1347        1.27       chs 	 * relock and finish up.
   1348        1.27       chs 	 */
   1349        1.46       chs 
   1350  1.116.12.1       tls 	mutex_enter(uobj->vmobjlock);
   1351        1.27       chs 	switch (rv) {
   1352        1.40       chs 	case 0:
   1353        1.27       chs 		break;
   1354        1.27       chs 
   1355        1.40       chs 	case EIO:
   1356        1.40       chs 	case ERESTART:
   1357        1.46       chs 
   1358        1.27       chs 		/*
   1359        1.27       chs 		 * nothing more to do on errors.
   1360        1.40       chs 		 * ERESTART can only mean that the anon was freed,
   1361        1.27       chs 		 * so again there's nothing to do.
   1362        1.27       chs 		 */
   1363        1.46       chs 
   1364        1.87   thorpej 		return false;
   1365        1.59        pk 
   1366        1.59        pk 	default:
   1367        1.87   thorpej 		return true;
   1368        1.27       chs 	}
   1369        1.27       chs 
   1370        1.27       chs 	/*
   1371        1.27       chs 	 * ok, we've got the page now.
   1372        1.27       chs 	 * mark it as dirty, clear its swslot and un-busy it.
   1373        1.27       chs 	 */
   1374        1.57        pk 	uao_dropswap(&aobj->u_obj, pageidx);
   1375        1.27       chs 
   1376        1.27       chs 	/*
   1377        1.80      yamt 	 * make sure it's on a page queue.
   1378        1.27       chs 	 */
   1379        1.96        ad 	mutex_enter(&uvm_pageqlock);
   1380        1.58        pk 	if (pg->wire_count == 0)
   1381        1.80      yamt 		uvm_pageenqueue(pg);
   1382        1.96        ad 	mutex_exit(&uvm_pageqlock);
   1383        1.56      yamt 
   1384        1.59        pk 	if (pg->flags & PG_WANTED) {
   1385        1.59        pk 		wakeup(pg);
   1386        1.59        pk 	}
   1387        1.59        pk 	pg->flags &= ~(PG_WANTED|PG_BUSY|PG_CLEAN|PG_FAKE);
   1388        1.56      yamt 	UVM_PAGE_OWN(pg, NULL);
   1389        1.56      yamt 
   1390        1.87   thorpej 	return false;
   1391         1.1       mrg }
   1392        1.72      yamt 
   1393        1.75      yamt /*
   1394        1.75      yamt  * uao_dropswap_range: drop swapslots in the range.
   1395        1.75      yamt  *
   1396        1.75      yamt  * => aobj must be locked and is returned locked.
   1397        1.75      yamt  * => start is inclusive.  end is exclusive.
   1398        1.75      yamt  */
   1399        1.75      yamt 
   1400        1.75      yamt void
   1401        1.75      yamt uao_dropswap_range(struct uvm_object *uobj, voff_t start, voff_t end)
   1402        1.75      yamt {
   1403        1.75      yamt 	struct uvm_aobj *aobj = (struct uvm_aobj *)uobj;
   1404  1.116.12.1       tls 	int swpgonlydelta = 0;
   1405        1.75      yamt 
   1406       1.115     rmind 	KASSERT(mutex_owned(uobj->vmobjlock));
   1407        1.75      yamt 
   1408        1.75      yamt 	if (end == 0) {
   1409        1.75      yamt 		end = INT64_MAX;
   1410        1.75      yamt 	}
   1411        1.75      yamt 
   1412        1.75      yamt 	if (UAO_USES_SWHASH(aobj)) {
   1413        1.75      yamt 		int i, hashbuckets = aobj->u_swhashmask + 1;
   1414        1.75      yamt 		voff_t taghi;
   1415        1.75      yamt 		voff_t taglo;
   1416        1.75      yamt 
   1417        1.75      yamt 		taglo = UAO_SWHASH_ELT_TAG(start);
   1418        1.75      yamt 		taghi = UAO_SWHASH_ELT_TAG(end);
   1419        1.75      yamt 
   1420        1.75      yamt 		for (i = 0; i < hashbuckets; i++) {
   1421        1.75      yamt 			struct uao_swhash_elt *elt, *next;
   1422        1.75      yamt 
   1423        1.75      yamt 			for (elt = LIST_FIRST(&aobj->u_swhash[i]);
   1424        1.75      yamt 			     elt != NULL;
   1425        1.75      yamt 			     elt = next) {
   1426        1.75      yamt 				int startidx, endidx;
   1427        1.75      yamt 				int j;
   1428        1.75      yamt 
   1429        1.75      yamt 				next = LIST_NEXT(elt, list);
   1430        1.75      yamt 
   1431        1.75      yamt 				if (elt->tag < taglo || taghi < elt->tag) {
   1432        1.75      yamt 					continue;
   1433        1.75      yamt 				}
   1434        1.75      yamt 
   1435        1.75      yamt 				if (elt->tag == taglo) {
   1436        1.75      yamt 					startidx =
   1437        1.75      yamt 					    UAO_SWHASH_ELT_PAGESLOT_IDX(start);
   1438        1.75      yamt 				} else {
   1439        1.75      yamt 					startidx = 0;
   1440        1.75      yamt 				}
   1441        1.75      yamt 
   1442        1.75      yamt 				if (elt->tag == taghi) {
   1443        1.75      yamt 					endidx =
   1444        1.75      yamt 					    UAO_SWHASH_ELT_PAGESLOT_IDX(end);
   1445        1.75      yamt 				} else {
   1446        1.75      yamt 					endidx = UAO_SWHASH_CLUSTER_SIZE;
   1447        1.75      yamt 				}
   1448        1.75      yamt 
   1449        1.75      yamt 				for (j = startidx; j < endidx; j++) {
   1450        1.75      yamt 					int slot = elt->slots[j];
   1451        1.75      yamt 
   1452        1.75      yamt 					KASSERT(uvm_pagelookup(&aobj->u_obj,
   1453        1.75      yamt 					    (UAO_SWHASH_ELT_PAGEIDX_BASE(elt)
   1454        1.75      yamt 					    + j) << PAGE_SHIFT) == NULL);
   1455        1.75      yamt 					if (slot > 0) {
   1456        1.75      yamt 						uvm_swap_free(slot, 1);
   1457        1.75      yamt 						swpgonlydelta++;
   1458        1.75      yamt 						KASSERT(elt->count > 0);
   1459        1.75      yamt 						elt->slots[j] = 0;
   1460        1.75      yamt 						elt->count--;
   1461        1.75      yamt 					}
   1462        1.75      yamt 				}
   1463        1.75      yamt 
   1464        1.75      yamt 				if (elt->count == 0) {
   1465        1.75      yamt 					LIST_REMOVE(elt, list);
   1466        1.75      yamt 					pool_put(&uao_swhash_elt_pool, elt);
   1467        1.75      yamt 				}
   1468        1.75      yamt 			}
   1469        1.75      yamt 		}
   1470        1.75      yamt 	} else {
   1471        1.75      yamt 		int i;
   1472        1.75      yamt 
   1473        1.75      yamt 		if (aobj->u_pages < end) {
   1474        1.75      yamt 			end = aobj->u_pages;
   1475        1.75      yamt 		}
   1476        1.75      yamt 		for (i = start; i < end; i++) {
   1477        1.75      yamt 			int slot = aobj->u_swslots[i];
   1478        1.75      yamt 
   1479        1.75      yamt 			if (slot > 0) {
   1480        1.75      yamt 				uvm_swap_free(slot, 1);
   1481        1.75      yamt 				swpgonlydelta++;
   1482        1.75      yamt 			}
   1483        1.75      yamt 		}
   1484        1.75      yamt 	}
   1485        1.75      yamt 
   1486        1.75      yamt 	/*
   1487        1.75      yamt 	 * adjust the counter of pages only in swap for all
   1488        1.75      yamt 	 * the swap slots we've freed.
   1489        1.75      yamt 	 */
   1490        1.75      yamt 
   1491        1.75      yamt 	if (swpgonlydelta > 0) {
   1492        1.92        ad 		mutex_enter(&uvm_swap_data_lock);
   1493        1.75      yamt 		KASSERT(uvmexp.swpgonly >= swpgonlydelta);
   1494        1.75      yamt 		uvmexp.swpgonly -= swpgonlydelta;
   1495        1.92        ad 		mutex_exit(&uvm_swap_data_lock);
   1496        1.75      yamt 	}
   1497        1.75      yamt }
   1498        1.75      yamt 
   1499        1.72      yamt #endif /* defined(VMSWAP) */
   1500