The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/vm/vm_fault.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*
    2  * Copyright (c) 1991, 1993
    3  *      The Regents of the University of California.  All rights reserved.
    4  * Copyright (c) 1994 John S. Dyson
    5  * All rights reserved.
    6  * Copyright (c) 1994 David Greenman
    7  * All rights reserved.
    8  *
    9  *
   10  * This code is derived from software contributed to Berkeley by
   11  * The Mach Operating System project at Carnegie-Mellon University.
   12  *
   13  * Redistribution and use in source and binary forms, with or without
   14  * modification, are permitted provided that the following conditions
   15  * are met:
   16  * 1. Redistributions of source code must retain the above copyright
   17  *    notice, this list of conditions and the following disclaimer.
   18  * 2. Redistributions in binary form must reproduce the above copyright
   19  *    notice, this list of conditions and the following disclaimer in the
   20  *    documentation and/or other materials provided with the distribution.
   21  * 3. All advertising materials mentioning features or use of this software
   22  *    must display the following acknowledgement:
   23  *      This product includes software developed by the University of
   24  *      California, Berkeley and its contributors.
   25  * 4. Neither the name of the University nor the names of its contributors
   26  *    may be used to endorse or promote products derived from this software
   27  *    without specific prior written permission.
   28  *
   29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   39  * SUCH DAMAGE.
   40  *
   41  *      from: @(#)vm_fault.c    8.4 (Berkeley) 1/12/94
   42  *
   43  *
   44  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
   45  * All rights reserved.
   46  *
   47  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
   48  *
   49  * Permission to use, copy, modify and distribute this software and
   50  * its documentation is hereby granted, provided that both the copyright
   51  * notice and this permission notice appear in all copies of the
   52  * software, derivative works or modified versions, and any portions
   53  * thereof, and that both notices appear in supporting documentation.
   54  *
   55  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
   56  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
   57  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
   58  *
   59  * Carnegie Mellon requests users of this software to return to
   60  *
   61  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
   62  *  School of Computer Science
   63  *  Carnegie Mellon University
   64  *  Pittsburgh PA 15213-3890
   65  *
   66  * any improvements or extensions that they make and grant Carnegie the
   67  * rights to redistribute these changes.
   68  *
   69  * $FreeBSD$
   70  */
   71 
   72 /*
   73  *      Page fault handling module.
   74  */
   75 
   76 #include <sys/param.h>
   77 #include <sys/systm.h>
   78 #include <sys/proc.h>
   79 #include <sys/vnode.h>
   80 #include <sys/resourcevar.h>
   81 #include <sys/vmmeter.h>
   82 
   83 #include <vm/vm.h>
   84 #include <vm/vm_param.h>
   85 #include <vm/vm_prot.h>
   86 #include <sys/lock.h>
   87 #include <vm/pmap.h>
   88 #include <vm/vm_map.h>
   89 #include <vm/vm_object.h>
   90 #include <vm/vm_page.h>
   91 #include <vm/vm_pageout.h>
   92 #include <vm/vm_kern.h>
   93 #include <vm/vm_pager.h>
   94 #include <vm/vnode_pager.h>
   95 #include <vm/vm_extern.h>
   96 
   97 static int vm_fault_additional_pages __P((vm_page_t, int,
   98                                           int, vm_page_t *, int *));
   99 
  100 #define VM_FAULT_READ_AHEAD 8
  101 #define VM_FAULT_READ_BEHIND 7
  102 #define VM_FAULT_READ (VM_FAULT_READ_AHEAD+VM_FAULT_READ_BEHIND+1)
  103 
  104 struct faultstate {
  105         vm_page_t m;
  106         vm_object_t object;
  107         vm_pindex_t pindex;
  108         vm_page_t first_m;
  109         vm_object_t     first_object;
  110         vm_pindex_t first_pindex;
  111         vm_map_t map;
  112         vm_map_entry_t entry;
  113         int lookup_still_valid;
  114         struct vnode *vp;
  115 };
  116 
  117 static void
  118 release_page(struct faultstate *fs)
  119 {
  120         vm_page_wakeup(fs->m);
  121         vm_page_deactivate(fs->m);
  122         fs->m = NULL;
  123 }
  124 
  125 static void
  126 unlock_map(struct faultstate *fs)
  127 {
  128         if (fs->lookup_still_valid) {
  129                 vm_map_lookup_done(fs->map, fs->entry);
  130                 fs->lookup_still_valid = FALSE;
  131         }
  132 }
  133 
  134 static void
  135 _unlock_things(struct faultstate *fs, int dealloc)
  136 {
  137         vm_object_pip_wakeup(fs->object);
  138         if (fs->object != fs->first_object) {
  139                 vm_page_free(fs->first_m);
  140                 vm_object_pip_wakeup(fs->first_object);
  141                 fs->first_m = NULL;
  142         }
  143         if (dealloc) {
  144                 vm_object_deallocate(fs->first_object);
  145         }
  146         unlock_map(fs); 
  147         if (fs->vp != NULL) { 
  148                 vput(fs->vp);
  149                 fs->vp = NULL;
  150         }
  151 }
  152 
  153 #define unlock_things(fs) _unlock_things(fs, 0)
  154 #define unlock_and_deallocate(fs) _unlock_things(fs, 1)
  155 
  156 /*
  157  *      vm_fault:
  158  *
  159  *      Handle a page fault occuring at the given address,
  160  *      requiring the given permissions, in the map specified.
  161  *      If successful, the page is inserted into the
  162  *      associated physical map.
  163  *
  164  *      NOTE: the given address should be truncated to the
  165  *      proper page address.
  166  *
  167  *      KERN_SUCCESS is returned if the page fault is handled; otherwise,
  168  *      a standard error specifying why the fault is fatal is returned.
  169  *
  170  *
  171  *      The map in question must be referenced, and remains so.
  172  *      Caller may hold no locks.
  173  */
  174 int
  175 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, int fault_flags)
  176 {
  177         vm_prot_t prot;
  178         int result;
  179         boolean_t wired;
  180         int map_generation;
  181         vm_page_t old_m;
  182         vm_object_t next_object;
  183         vm_page_t marray[VM_FAULT_READ];
  184         int hardfault;
  185         int faultcount;
  186         struct faultstate fs;
  187 
  188         cnt.v_vm_faults++;      /* needs lock XXX */
  189         hardfault = 0;
  190 
  191 RetryFault:;
  192         fs.map = map;
  193 
  194         /*
  195          * Find the backing store object and offset into it to begin the
  196          * search.
  197          */
  198         if ((result = vm_map_lookup(&fs.map, vaddr,
  199                 fault_type, &fs.entry, &fs.first_object,
  200                 &fs.first_pindex, &prot, &wired)) != KERN_SUCCESS) {
  201                 if ((result != KERN_PROTECTION_FAILURE) ||
  202                         ((fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE)) {
  203                         return result;
  204                 }
  205 
  206                 /*
  207                  * If we are user-wiring a r/w segment, and it is COW, then
  208                  * we need to do the COW operation.  Note that we don't COW
  209                  * currently RO sections now, because it is NOT desirable
  210                  * to COW .text.  We simply keep .text from ever being COW'ed
  211                  * and take the heat that one cannot debug wired .text sections.
  212                  */
  213                 result = vm_map_lookup(&fs.map, vaddr,
  214                         VM_PROT_READ|VM_PROT_WRITE|VM_PROT_OVERRIDE_WRITE,
  215                         &fs.entry, &fs.first_object, &fs.first_pindex, &prot, &wired);
  216                 if (result != KERN_SUCCESS) {
  217                         return result;
  218                 }
  219 
  220                 /*
  221                  * If we don't COW now, on a user wire, the user will never
  222                  * be able to write to the mapping.  If we don't make this
  223                  * restriction, the bookkeeping would be nearly impossible.
  224                  */
  225                 if ((fs.entry->protection & VM_PROT_WRITE) == 0)
  226                         fs.entry->max_protection &= ~VM_PROT_WRITE;
  227         }
  228 
  229         map_generation = fs.map->timestamp;
  230 
  231         if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
  232                 panic("vm_fault: fault on nofault entry, addr: %lx",
  233                     (u_long)vaddr);
  234         }
  235 
  236         /*
  237          * Make a reference to this object to prevent its disposal while we
  238          * are messing with it.  Once we have the reference, the map is free
  239          * to be diddled.  Since objects reference their shadows (and copies),
  240          * they will stay around as well.
  241          */
  242         vm_object_reference(fs.first_object);
  243         vm_object_pip_add(fs.first_object, 1);
  244 
  245         fs.vp = vnode_pager_lock(fs.first_object);
  246         if ((fault_type & VM_PROT_WRITE) &&
  247                 (fs.first_object->type == OBJT_VNODE)) {
  248                 vm_freeze_copyopts(fs.first_object,
  249                         fs.first_pindex, fs.first_pindex + 1);
  250         }
  251 
  252         fs.lookup_still_valid = TRUE;
  253 
  254         if (wired)
  255                 fault_type = prot;
  256 
  257         fs.first_m = NULL;
  258 
  259         /*
  260          * Search for the page at object/offset.
  261          */
  262 
  263         fs.object = fs.first_object;
  264         fs.pindex = fs.first_pindex;
  265 
  266         /*
  267          * See whether this page is resident
  268          */
  269         while (TRUE) {
  270                 /*
  271                  * If the object is dead, we stop here
  272                  */
  273 
  274                 if (fs.object->flags & OBJ_DEAD) {
  275                         unlock_and_deallocate(&fs);
  276                         return (KERN_PROTECTION_FAILURE);
  277                 }
  278 
  279                 /*
  280                  * See if page is resident
  281                  */
  282                         
  283                 fs.m = vm_page_lookup(fs.object, fs.pindex);
  284                 if (fs.m != NULL) {
  285                         int queue, s;
  286                         /*
  287                          * Wait/Retry if the page is busy.  We have to do this
  288                          * if the page is busy via either PG_BUSY or
  289                          * vm_page_t->busy because the vm_pager may be using
  290                          * vm_page_t->busy for pageouts ( and even pageins if
  291                          * it is the vnode pager ), and we could end up trying
  292                          * to pagein and pageout the same page simultaniously.
  293                          *
  294                          * We can theoretically allow the busy case on a read
  295                          * fault if the page is marked valid, but since such
  296                          * pages are typically already pmap'd, putting that
  297                          * special case in might be more effort then it is
  298                          * worth.  We cannot under any circumstances mess
  299                          * around with a vm_page_t->busy page except, perhaps,
  300                          * to pmap it.
  301                          */
  302                         if ((fs.m->flags & PG_BUSY) || fs.m->busy) {
  303                                 unlock_things(&fs);
  304                                 s = splvm();
  305                                 if ((fs.m->flags & PG_BUSY) || fs.m->busy) {
  306                                         vm_page_flag_set(fs.m, PG_WANTED | PG_REFERENCED);
  307                                         cnt.v_intrans++;
  308                                         tsleep(fs.m, PSWP, "vmpfw", 0);
  309                                 }
  310                                 splx(s);
  311                                 vm_object_deallocate(fs.first_object);
  312                                 goto RetryFault;
  313                         }
  314 
  315                         queue = fs.m->queue;
  316                         s = splvm();
  317                         vm_page_unqueue_nowakeup(fs.m);
  318                         splx(s);
  319 
  320                         /*
  321                          * Mark page busy for other processes, and the pagedaemon.
  322                          */
  323                         if (((queue - fs.m->pc) == PQ_CACHE) &&
  324                             (cnt.v_free_count + cnt.v_cache_count) < cnt.v_free_min) {
  325                                 vm_page_activate(fs.m);
  326                                 unlock_and_deallocate(&fs);
  327                                 VM_WAIT;
  328                                 goto RetryFault;
  329                         }
  330 
  331                         vm_page_busy(fs.m);
  332                         if (((fs.m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) &&
  333                                 fs.m->object != kernel_object && fs.m->object != kmem_object) {
  334                                 goto readrest;
  335                         }
  336 
  337                         break;
  338                 }
  339                 if (((fs.object->type != OBJT_DEFAULT) &&
  340                                 (((fault_flags & VM_FAULT_WIRE_MASK) == 0) || wired))
  341                     || (fs.object == fs.first_object)) {
  342 
  343                         if (fs.pindex >= fs.object->size) {
  344                                 unlock_and_deallocate(&fs);
  345                                 return (KERN_PROTECTION_FAILURE);
  346                         }
  347 
  348                         /*
  349                          * Allocate a new page for this object/offset pair.
  350                          */
  351                         fs.m = vm_page_alloc(fs.object, fs.pindex,
  352                                 (fs.vp || fs.object->backing_object)? VM_ALLOC_NORMAL: VM_ALLOC_ZERO);
  353 
  354                         if (fs.m == NULL) {
  355                                 unlock_and_deallocate(&fs);
  356                                 VM_WAIT;
  357                                 goto RetryFault;
  358                         }
  359                 }
  360 
  361 readrest:
  362                 if (fs.object->type != OBJT_DEFAULT &&
  363                         (((fault_flags & VM_FAULT_WIRE_MASK) == 0) || wired)) {
  364                         int rv;
  365                         int reqpage;
  366                         int ahead, behind;
  367 
  368                         if (fs.first_object->behavior == OBJ_RANDOM) {
  369                                 ahead = 0;
  370                                 behind = 0;
  371                         } else {
  372                                 behind = (vaddr - fs.entry->start) >> PAGE_SHIFT;
  373                                 if (behind > VM_FAULT_READ_BEHIND)
  374                                         behind = VM_FAULT_READ_BEHIND;
  375 
  376                                 ahead = ((fs.entry->end - vaddr) >> PAGE_SHIFT) - 1;
  377                                 if (ahead > VM_FAULT_READ_AHEAD)
  378                                         ahead = VM_FAULT_READ_AHEAD;
  379                         }
  380 
  381                         if ((fs.first_object->type != OBJT_DEVICE) &&
  382                                 (fs.first_object->behavior == OBJ_SEQUENTIAL)) {
  383                                 vm_pindex_t firstpindex, tmppindex;
  384                                 if (fs.first_pindex <
  385                                         2*(VM_FAULT_READ_BEHIND + VM_FAULT_READ_AHEAD + 1))
  386                                         firstpindex = 0;
  387                                 else
  388                                         firstpindex = fs.first_pindex -
  389                                                 2*(VM_FAULT_READ_BEHIND + VM_FAULT_READ_AHEAD + 1);
  390 
  391                                 for(tmppindex = fs.first_pindex - 1;
  392                                         tmppindex >= firstpindex;
  393                                         --tmppindex) {
  394                                         vm_page_t mt;
  395                                         mt = vm_page_lookup( fs.first_object, tmppindex);
  396                                         if (mt == NULL || (mt->valid != VM_PAGE_BITS_ALL))
  397                                                 break;
  398                                         if (mt->busy ||
  399                                                 (mt->flags & (PG_BUSY | PG_FICTITIOUS)) ||
  400                                                 mt->hold_count ||
  401                                                 mt->wire_count) 
  402                                                 continue;
  403                                         if (mt->dirty == 0)
  404                                                 vm_page_test_dirty(mt);
  405                                         if (mt->dirty) {
  406                                                 vm_page_protect(mt, VM_PROT_NONE);
  407                                                 vm_page_deactivate(mt);
  408                                         } else {
  409                                                 vm_page_cache(mt);
  410                                         }
  411                                 }
  412 
  413                                 ahead += behind;
  414                                 behind = 0;
  415                         }
  416 
  417                         /*
  418                          * now we find out if any other pages should be paged
  419                          * in at this time this routine checks to see if the
  420                          * pages surrounding this fault reside in the same
  421                          * object as the page for this fault.  If they do,
  422                          * then they are faulted in also into the object.  The
  423                          * array "marray" returned contains an array of
  424                          * vm_page_t structs where one of them is the
  425                          * vm_page_t passed to the routine.  The reqpage
  426                          * return value is the index into the marray for the
  427                          * vm_page_t passed to the routine.
  428                          */
  429                         faultcount = vm_fault_additional_pages(
  430                             fs.m, behind, ahead, marray, &reqpage);
  431 
  432                         /*
  433                          * Call the pager to retrieve the data, if any, after
  434                          * releasing the lock on the map.
  435                          */
  436                         unlock_map(&fs);
  437 
  438                         rv = faultcount ?
  439                             vm_pager_get_pages(fs.object, marray, faultcount,
  440                                 reqpage) : VM_PAGER_FAIL;
  441 
  442                         if (rv == VM_PAGER_OK) {
  443                                 /*
  444                                  * Found the page. Leave it busy while we play
  445                                  * with it.
  446                                  */
  447 
  448                                 /*
  449                                  * Relookup in case pager changed page. Pager
  450                                  * is responsible for disposition of old page
  451                                  * if moved.
  452                                  */
  453                                 fs.m = vm_page_lookup(fs.object, fs.pindex);
  454                                 if(!fs.m) {
  455                                         unlock_and_deallocate(&fs);
  456                                         goto RetryFault;
  457                                 }
  458 
  459                                 hardfault++;
  460                                 break;
  461                         }
  462                         /*
  463                          * Remove the bogus page (which does not exist at this
  464                          * object/offset); before doing so, we must get back
  465                          * our object lock to preserve our invariant.
  466                          *
  467                          * Also wake up any other process that may want to bring
  468                          * in this page.
  469                          *
  470                          * If this is the top-level object, we must leave the
  471                          * busy page to prevent another process from rushing
  472                          * past us, and inserting the page in that object at
  473                          * the same time that we are.
  474                          */
  475 
  476                         if (rv == VM_PAGER_ERROR)
  477                                 printf("vm_fault: pager read error, pid %d (%s)\n",
  478                                     curproc->p_pid, curproc->p_comm);
  479                         /*
  480                          * Data outside the range of the pager or an I/O error
  481                          */
  482                         /*
  483                          * XXX - the check for kernel_map is a kludge to work
  484                          * around having the machine panic on a kernel space
  485                          * fault w/ I/O error.
  486                          */
  487                         if (((fs.map != kernel_map) && (rv == VM_PAGER_ERROR)) ||
  488                                 (rv == VM_PAGER_BAD)) {
  489                                 vm_page_free(fs.m);
  490                                 fs.m = NULL;
  491                                 unlock_and_deallocate(&fs);
  492                                 return ((rv == VM_PAGER_ERROR) ? KERN_FAILURE : KERN_PROTECTION_FAILURE);
  493                         }
  494                         if (fs.object != fs.first_object) {
  495                                 vm_page_free(fs.m);
  496                                 fs.m = NULL;
  497                                 /*
  498                                  * XXX - we cannot just fall out at this
  499                                  * point, m has been freed and is invalid!
  500                                  */
  501                         }
  502                 }
  503                 /*
  504                  * We get here if the object has default pager (or unwiring) or the
  505                  * pager doesn't have the page.
  506                  */
  507                 if (fs.object == fs.first_object)
  508                         fs.first_m = fs.m;
  509 
  510                 /*
  511                  * Move on to the next object.  Lock the next object before
  512                  * unlocking the current one.
  513                  */
  514 
  515                 fs.pindex += OFF_TO_IDX(fs.object->backing_object_offset);
  516                 next_object = fs.object->backing_object;
  517                 if (next_object == NULL) {
  518                         /*
  519                          * If there's no object left, fill the page in the top
  520                          * object with zeros.
  521                          */
  522                         if (fs.object != fs.first_object) {
  523                                 vm_object_pip_wakeup(fs.object);
  524 
  525                                 fs.object = fs.first_object;
  526                                 fs.pindex = fs.first_pindex;
  527                                 fs.m = fs.first_m;
  528                         }
  529                         fs.first_m = NULL;
  530 
  531                         if ((fs.m->flags & PG_ZERO) == 0) {
  532                                 vm_page_zero_fill(fs.m);
  533                         }
  534                         else
  535                                 cnt.v_ozfod++;
  536                         cnt.v_zfod++;
  537                         break;
  538                 } else {
  539                         if (fs.object != fs.first_object) {
  540                                 vm_object_pip_wakeup(fs.object);
  541                         }
  542                         fs.object = next_object;
  543                         vm_object_pip_add(fs.object, 1);
  544                 }
  545         }
  546         KASSERT((fs.m->flags & PG_BUSY) != 0,
  547             ("vm_fault: not busy after main loop"));
  548 
  549         /*
  550          * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
  551          * is held.]
  552          */
  553 
  554         old_m = fs.m;   /* save page that would be copied */
  555 
  556         /*
  557          * If the page is being written, but isn't already owned by the
  558          * top-level object, we have to copy it into a new page owned by the
  559          * top-level object.
  560          */
  561 
  562         if (fs.object != fs.first_object) {
  563                 /*
  564                  * We only really need to copy if we want to write it.
  565                  */
  566 
  567                 if (fault_type & VM_PROT_WRITE) {
  568 
  569                         /*
  570                          * This allows pages to be virtually copied from a backing_object
  571                          * into the first_object, where the backing object has no other
  572                          * refs to it, and cannot gain any more refs.  Instead of a
  573                          * bcopy, we just move the page from the backing object to the
  574                          * first object.  Note that we must mark the page dirty in the
  575                          * first object so that it will go out to swap when needed.
  576                          */
  577                         if (map_generation == fs.map->timestamp &&
  578                                 /*
  579                                  * Only one shadow object
  580                                  */
  581                                 (fs.object->shadow_count == 1) &&
  582                                 /*
  583                                  * No COW refs, except us
  584                                  */
  585                                 (fs.object->ref_count == 1) &&
  586                                 /*
  587                                  * Noone else can look this object up
  588                                  */
  589                                 (fs.object->handle == NULL) &&
  590                                 /*
  591                                  * No other ways to look the object up
  592                                  */
  593                                 ((fs.object->type == OBJT_DEFAULT) ||
  594                                  (fs.object->type == OBJT_SWAP)) &&
  595                                 /*
  596                                  * We don't chase down the shadow chain
  597                                  */
  598                                 (fs.object == fs.first_object->backing_object) &&
  599 
  600                                 /*
  601                                  * grab the lock if we need to
  602                                  */
  603                                 (fs.lookup_still_valid ||
  604                                                 (((fs.entry->eflags & MAP_ENTRY_IS_A_MAP) == 0) &&
  605                                                  lockmgr(&fs.map->lock,
  606                                                         LK_EXCLUSIVE|LK_NOWAIT, (void *)0, curproc) == 0))) {
  607                                 
  608                                 fs.lookup_still_valid = 1;
  609                                 /*
  610                                  * get rid of the unnecessary page
  611                                  */
  612                                 vm_page_protect(fs.first_m, VM_PROT_NONE);
  613                                 vm_page_free(fs.first_m);
  614                                 fs.first_m = NULL;
  615 
  616                                 /*
  617                                  * grab the page and put it into the process'es object
  618                                  */
  619                                 vm_page_rename(fs.m, fs.first_object, fs.first_pindex);
  620                                 fs.first_m = fs.m;
  621                                 fs.first_m->dirty = VM_PAGE_BITS_ALL;
  622                                 vm_page_busy(fs.first_m);
  623                                 fs.m = NULL;
  624                                 cnt.v_cow_optim++;
  625                         } else {
  626                                 /*
  627                                  * Oh, well, lets copy it.
  628                                  */
  629                                 vm_page_copy(fs.m, fs.first_m);
  630                         }
  631 
  632                         if (fs.m) {
  633                                 /*
  634                                  * We no longer need the old page or object.
  635                                  */
  636                                 release_page(&fs);
  637                         }
  638 
  639                         vm_object_pip_wakeup(fs.object);
  640                         /*
  641                          * Only use the new page below...
  642                          */
  643 
  644                         cnt.v_cow_faults++;
  645                         fs.m = fs.first_m;
  646                         fs.object = fs.first_object;
  647                         fs.pindex = fs.first_pindex;
  648 
  649                 } else {
  650                         prot &= ~VM_PROT_WRITE;
  651                 }
  652         }
  653 
  654         /*
  655          * We must verify that the maps have not changed since our last
  656          * lookup.
  657          */
  658 
  659         if (!fs.lookup_still_valid &&
  660                 (fs.map->timestamp != map_generation)) {
  661                 vm_object_t retry_object;
  662                 vm_pindex_t retry_pindex;
  663                 vm_prot_t retry_prot;
  664 
  665                 /*
  666                  * Since map entries may be pageable, make sure we can take a
  667                  * page fault on them.
  668                  */
  669 
  670  
  671                 /*
  672                  * Unlock vnode before the lookup to avoid deadlock.   E.G.
  673                  * avoid a deadlock between the inode and exec_map that can
  674                  * occur due to locks being obtained in different orders.
  675                  */
  676 
  677                 if (fs.vp != NULL) {
  678                         vput(fs.vp);
  679                         fs.vp = NULL;
  680                 }
  681 
  682                 /*
  683                  * To avoid trying to write_lock the map while another process
  684                  * has it read_locked (in vm_map_pageable), we do not try for
  685                  * write permission.  If the page is still writable, we will
  686                  * get write permission.  If it is not, or has been marked
  687                  * needs_copy, we enter the mapping without write permission,
  688                  * and will merely take another fault.
  689                  */
  690                 result = vm_map_lookup(&fs.map, vaddr, fault_type & ~VM_PROT_WRITE,
  691                     &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired);
  692                 map_generation = fs.map->timestamp;
  693 
  694                 /*
  695                  * If we don't need the page any longer, put it on the active
  696                  * list (the easiest thing to do here).  If no one needs it,
  697                  * pageout will grab it eventually.
  698                  */
  699 
  700                 if (result != KERN_SUCCESS) {
  701                         release_page(&fs);
  702                         unlock_and_deallocate(&fs);
  703                         return (result);
  704                 }
  705                 fs.lookup_still_valid = TRUE;
  706 
  707                 if ((retry_object != fs.first_object) ||
  708                     (retry_pindex != fs.first_pindex)) {
  709                         release_page(&fs);
  710                         unlock_and_deallocate(&fs);
  711                         goto RetryFault;
  712                 }
  713                 /*
  714                  * Check whether the protection has changed or the object has
  715                  * been copied while we left the map unlocked. Changing from
  716                  * read to write permission is OK - we leave the page
  717                  * write-protected, and catch the write fault. Changing from
  718                  * write to read permission means that we can't mark the page
  719                  * write-enabled after all.
  720                  */
  721                 prot &= retry_prot;
  722         }
  723 
  724         /*
  725          * Put this page into the physical map. We had to do the unlock above
  726          * because pmap_enter may cause other faults.   We don't put the page
  727          * back on the active queue until later so that the page-out daemon
  728          * won't find us (yet).
  729          */
  730 
  731         if (prot & VM_PROT_WRITE) {
  732                 vm_page_flag_set(fs.m, PG_WRITEABLE);
  733                 vm_object_set_flag(fs.m->object,
  734                                    OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
  735                 /*
  736                  * If the fault is a write, we know that this page is being
  737                  * written NOW. This will save on the pmap_is_modified() calls
  738                  * later.
  739                  */
  740                 if (fault_flags & VM_FAULT_DIRTY) {
  741                         fs.m->dirty = VM_PAGE_BITS_ALL;
  742                 }
  743         }
  744 
  745         unlock_things(&fs);
  746         fs.m->valid = VM_PAGE_BITS_ALL;
  747         vm_page_flag_clear(fs.m, PG_ZERO);
  748 
  749         pmap_enter(fs.map->pmap, vaddr, VM_PAGE_TO_PHYS(fs.m), prot, wired);
  750         if (((fault_flags & VM_FAULT_WIRE_MASK) == 0) && (wired == 0)) {
  751                 pmap_prefault(fs.map->pmap, vaddr, fs.entry);
  752         }
  753 
  754         vm_page_flag_set(fs.m, PG_MAPPED|PG_REFERENCED);
  755         if (fault_flags & VM_FAULT_HOLD)
  756                 vm_page_hold(fs.m);
  757 
  758         /*
  759          * If the page is not wired down, then put it where the pageout daemon
  760          * can find it.
  761          */
  762         if (fault_flags & VM_FAULT_WIRE_MASK) {
  763                 if (wired)
  764                         vm_page_wire(fs.m);
  765                 else
  766                         vm_page_unwire(fs.m, 1);
  767         } else {
  768                 vm_page_activate(fs.m);
  769         }
  770 
  771         if (curproc && (curproc->p_flag & P_INMEM) && curproc->p_stats) {
  772                 if (hardfault) {
  773                         curproc->p_stats->p_ru.ru_majflt++;
  774                 } else {
  775                         curproc->p_stats->p_ru.ru_minflt++;
  776                 }
  777         }
  778 
  779         /*
  780          * Unlock everything, and return
  781          */
  782 
  783         vm_page_wakeup(fs.m);
  784         vm_object_deallocate(fs.first_object);
  785 
  786         return (KERN_SUCCESS);
  787 
  788 }
  789 
  790 /*
  791  *      vm_fault_wire:
  792  *
  793  *      Wire down a range of virtual addresses in a map.
  794  */
  795 int
  796 vm_fault_wire(map, start, end)
  797         vm_map_t map;
  798         vm_offset_t start, end;
  799 {
  800 
  801         register vm_offset_t va;
  802         register pmap_t pmap;
  803         int rv;
  804 
  805         pmap = vm_map_pmap(map);
  806 
  807         /*
  808          * Inform the physical mapping system that the range of addresses may
  809          * not fault, so that page tables and such can be locked down as well.
  810          */
  811 
  812         pmap_pageable(pmap, start, end, FALSE);
  813 
  814         /*
  815          * We simulate a fault to get the page and enter it in the physical
  816          * map.
  817          */
  818 
  819         for (va = start; va < end; va += PAGE_SIZE) {
  820                 rv = vm_fault(map, va, VM_PROT_READ|VM_PROT_WRITE,
  821                         VM_FAULT_CHANGE_WIRING);
  822                 if (rv) {
  823                         if (va != start)
  824                                 vm_fault_unwire(map, start, va);
  825                         return (rv);
  826                 }
  827         }
  828         return (KERN_SUCCESS);
  829 }
  830 
  831 /*
  832  *      vm_fault_user_wire:
  833  *
  834  *      Wire down a range of virtual addresses in a map.  This
  835  *      is for user mode though, so we only ask for read access
  836  *      on currently read only sections.
  837  */
  838 int
  839 vm_fault_user_wire(map, start, end)
  840         vm_map_t map;
  841         vm_offset_t start, end;
  842 {
  843 
  844         register vm_offset_t va;
  845         register pmap_t pmap;
  846         int rv;
  847 
  848         pmap = vm_map_pmap(map);
  849 
  850         /*
  851          * Inform the physical mapping system that the range of addresses may
  852          * not fault, so that page tables and such can be locked down as well.
  853          */
  854 
  855         pmap_pageable(pmap, start, end, FALSE);
  856 
  857         /*
  858          * We simulate a fault to get the page and enter it in the physical
  859          * map.
  860          */
  861         for (va = start; va < end; va += PAGE_SIZE) {
  862                 rv = vm_fault(map, va, VM_PROT_READ, VM_FAULT_USER_WIRE);
  863                 if (rv) {
  864                         if (va != start)
  865                                 vm_fault_unwire(map, start, va);
  866                         return (rv);
  867                 }
  868         }
  869         return (KERN_SUCCESS);
  870 }
  871 
  872 
  873 /*
  874  *      vm_fault_unwire:
  875  *
  876  *      Unwire a range of virtual addresses in a map.
  877  */
  878 void
  879 vm_fault_unwire(map, start, end)
  880         vm_map_t map;
  881         vm_offset_t start, end;
  882 {
  883 
  884         register vm_offset_t va, pa;
  885         register pmap_t pmap;
  886 
  887         pmap = vm_map_pmap(map);
  888 
  889         /*
  890          * Since the pages are wired down, we must be able to get their
  891          * mappings from the physical map system.
  892          */
  893 
  894         for (va = start; va < end; va += PAGE_SIZE) {
  895                 pa = pmap_extract(pmap, va);
  896                 if (pa != (vm_offset_t) 0) {
  897                         pmap_change_wiring(pmap, va, FALSE);
  898                         vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1);
  899                 }
  900         }
  901 
  902         /*
  903          * Inform the physical mapping system that the range of addresses may
  904          * fault, so that page tables and such may be unwired themselves.
  905          */
  906 
  907         pmap_pageable(pmap, start, end, TRUE);
  908 
  909 }
  910 
  911 /*
  912  *      Routine:
  913  *              vm_fault_copy_entry
  914  *      Function:
  915  *              Copy all of the pages from a wired-down map entry to another.
  916  *
  917  *      In/out conditions:
  918  *              The source and destination maps must be locked for write.
  919  *              The source map entry must be wired down (or be a sharing map
  920  *              entry corresponding to a main map entry that is wired down).
  921  */
  922 
  923 void
  924 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry)
  925         vm_map_t dst_map;
  926         vm_map_t src_map;
  927         vm_map_entry_t dst_entry;
  928         vm_map_entry_t src_entry;
  929 {
  930         vm_object_t dst_object;
  931         vm_object_t src_object;
  932         vm_ooffset_t dst_offset;
  933         vm_ooffset_t src_offset;
  934         vm_prot_t prot;
  935         vm_offset_t vaddr;
  936         vm_page_t dst_m;
  937         vm_page_t src_m;
  938 
  939 #ifdef  lint
  940         src_map++;
  941 #endif  /* lint */
  942 
  943         src_object = src_entry->object.vm_object;
  944         src_offset = src_entry->offset;
  945 
  946         /*
  947          * Create the top-level object for the destination entry. (Doesn't
  948          * actually shadow anything - we copy the pages directly.)
  949          */
  950         dst_object = vm_object_allocate(OBJT_DEFAULT,
  951             (vm_size_t) OFF_TO_IDX(dst_entry->end - dst_entry->start));
  952 
  953         dst_entry->object.vm_object = dst_object;
  954         dst_entry->offset = 0;
  955 
  956         prot = dst_entry->max_protection;
  957 
  958         /*
  959          * Loop through all of the pages in the entry's range, copying each
  960          * one from the source object (it should be there) to the destination
  961          * object.
  962          */
  963         for (vaddr = dst_entry->start, dst_offset = 0;
  964             vaddr < dst_entry->end;
  965             vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) {
  966 
  967                 /*
  968                  * Allocate a page in the destination object
  969                  */
  970                 do {
  971                         dst_m = vm_page_alloc(dst_object,
  972                                 OFF_TO_IDX(dst_offset), VM_ALLOC_NORMAL);
  973                         if (dst_m == NULL) {
  974                                 VM_WAIT;
  975                         }
  976                 } while (dst_m == NULL);
  977 
  978                 /*
  979                  * Find the page in the source object, and copy it in.
  980                  * (Because the source is wired down, the page will be in
  981                  * memory.)
  982                  */
  983                 src_m = vm_page_lookup(src_object,
  984                         OFF_TO_IDX(dst_offset + src_offset));
  985                 if (src_m == NULL)
  986                         panic("vm_fault_copy_wired: page missing");
  987 
  988                 vm_page_copy(src_m, dst_m);
  989 
  990                 /*
  991                  * Enter it in the pmap...
  992                  */
  993 
  994                 vm_page_flag_clear(dst_m, PG_ZERO);
  995                 pmap_enter(dst_map->pmap, vaddr, VM_PAGE_TO_PHYS(dst_m),
  996                     prot, FALSE);
  997                 vm_page_flag_set(dst_m, PG_WRITEABLE|PG_MAPPED);
  998 
  999                 /*
 1000                  * Mark it no longer busy, and put it on the active list.
 1001                  */
 1002                 vm_page_activate(dst_m);
 1003                 vm_page_wakeup(dst_m);
 1004         }
 1005 }
 1006 
 1007 
 1008 /*
 1009  * This routine checks around the requested page for other pages that
 1010  * might be able to be faulted in.  This routine brackets the viable
 1011  * pages for the pages to be paged in.
 1012  *
 1013  * Inputs:
 1014  *      m, rbehind, rahead
 1015  *
 1016  * Outputs:
 1017  *  marray (array of vm_page_t), reqpage (index of requested page)
 1018  *
 1019  * Return value:
 1020  *  number of pages in marray
 1021  */
 1022 static int
 1023 vm_fault_additional_pages(m, rbehind, rahead, marray, reqpage)
 1024         vm_page_t m;
 1025         int rbehind;
 1026         int rahead;
 1027         vm_page_t *marray;
 1028         int *reqpage;
 1029 {
 1030         int i,j;
 1031         vm_object_t object;
 1032         vm_pindex_t pindex, startpindex, endpindex, tpindex;
 1033         vm_page_t rtm;
 1034         int cbehind, cahead;
 1035 
 1036         object = m->object;
 1037         pindex = m->pindex;
 1038 
 1039         /*
 1040          * we don't fault-ahead for device pager
 1041          */
 1042         if (object->type == OBJT_DEVICE) {
 1043                 *reqpage = 0;
 1044                 marray[0] = m;
 1045                 return 1;
 1046         }
 1047 
 1048         /*
 1049          * if the requested page is not available, then give up now
 1050          */
 1051 
 1052         if (!vm_pager_has_page(object,
 1053                 OFF_TO_IDX(object->paging_offset) + pindex, &cbehind, &cahead)) {
 1054                 return 0;
 1055         }
 1056 
 1057         if ((cbehind == 0) && (cahead == 0)) {
 1058                 *reqpage = 0;
 1059                 marray[0] = m;
 1060                 return 1;
 1061         }
 1062 
 1063         if (rahead > cahead) {
 1064                 rahead = cahead;
 1065         }
 1066 
 1067         if (rbehind > cbehind) {
 1068                 rbehind = cbehind;
 1069         }
 1070 
 1071         /*
 1072          * try to do any readahead that we might have free pages for.
 1073          */
 1074         if ((rahead + rbehind) >
 1075                 ((cnt.v_free_count + cnt.v_cache_count) - cnt.v_free_reserved)) {
 1076                 pagedaemon_wakeup();
 1077                 marray[0] = m;
 1078                 *reqpage = 0;
 1079                 return 1;
 1080         }
 1081 
 1082         /*
 1083          * scan backward for the read behind pages -- in memory 
 1084          */
 1085         if (pindex > 0) {
 1086                 if (rbehind > pindex) {
 1087                         rbehind = pindex;
 1088                         startpindex = 0;
 1089                 } else {
 1090                         startpindex = pindex - rbehind;
 1091                 }
 1092 
 1093                 for ( tpindex = pindex - 1; tpindex >= startpindex; tpindex -= 1) {
 1094                         if (vm_page_lookup( object, tpindex)) {
 1095                                 startpindex = tpindex + 1;
 1096                                 break;
 1097                         }
 1098                         if (tpindex == 0)
 1099                                 break;
 1100                 }
 1101 
 1102                 for(i = 0, tpindex = startpindex; tpindex < pindex; i++, tpindex++) {
 1103 
 1104                         rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
 1105                         if (rtm == NULL) {
 1106                                 for (j = 0; j < i; j++) {
 1107                                         vm_page_free(marray[j]);
 1108                                 }
 1109                                 marray[0] = m;
 1110                                 *reqpage = 0;
 1111                                 return 1;
 1112                         }
 1113 
 1114                         marray[i] = rtm;
 1115                 }
 1116         } else {
 1117                 startpindex = 0;
 1118                 i = 0;
 1119         }
 1120 
 1121         marray[i] = m;
 1122         /* page offset of the required page */
 1123         *reqpage = i;
 1124 
 1125         tpindex = pindex + 1;
 1126         i++;
 1127 
 1128         /*
 1129          * scan forward for the read ahead pages
 1130          */
 1131         endpindex = tpindex + rahead;
 1132         if (endpindex > object->size)
 1133                 endpindex = object->size;
 1134 
 1135         for( ; tpindex < endpindex; i++, tpindex++) {
 1136 
 1137                 if (vm_page_lookup(object, tpindex)) {
 1138                         break;
 1139                 }
 1140 
 1141                 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
 1142                 if (rtm == NULL) {
 1143                         break;
 1144                 }
 1145 
 1146                 marray[i] = rtm;
 1147         }
 1148 
 1149         /* return number of bytes of pages */
 1150         return i;
 1151 }

Cache object: 1018199b2b892c946cc988a68c3726d6


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.