[BACK]Return to uvm_aobj.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / uvm

Annotation of src/sys/uvm/uvm_aobj.c, Revision 1.45.2.1

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

CVSweb <webmaster@jp.NetBSD.org>