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_contig.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 Regents of the University of California.
    3  * All rights reserved.
    4  *
    5  * This code is derived from software contributed to Berkeley by
    6  * The Mach Operating System project at Carnegie-Mellon University.
    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  * 4. Neither the name of the University nor the names of its contributors
   17  *    may be used to endorse or promote products derived from this software
   18  *    without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   30  * SUCH DAMAGE.
   31  *
   32  *      from: @(#)vm_page.c     7.4 (Berkeley) 5/7/91
   33  */
   34 
   35 /*-
   36  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
   37  * All rights reserved.
   38  *
   39  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
   40  *
   41  * Permission to use, copy, modify and distribute this software and
   42  * its documentation is hereby granted, provided that both the copyright
   43  * notice and this permission notice appear in all copies of the
   44  * software, derivative works or modified versions, and any portions
   45  * thereof, and that both notices appear in supporting documentation.
   46  *
   47  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
   48  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
   49  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
   50  *
   51  * Carnegie Mellon requests users of this software to return to
   52  *
   53  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
   54  *  School of Computer Science
   55  *  Carnegie Mellon University
   56  *  Pittsburgh PA 15213-3890
   57  *
   58  * any improvements or extensions that they make and grant Carnegie the
   59  * rights to redistribute these changes.
   60  */
   61 
   62 #include <sys/cdefs.h>
   63 __FBSDID("$FreeBSD: releng/8.0/sys/vm/vm_contig.c 195649 2009-07-12 23:31:20Z alc $");
   64 
   65 #include <sys/param.h>
   66 #include <sys/systm.h>
   67 #include <sys/lock.h>
   68 #include <sys/malloc.h>
   69 #include <sys/mount.h>
   70 #include <sys/mutex.h>
   71 #include <sys/proc.h>
   72 #include <sys/kernel.h>
   73 #include <sys/linker_set.h>
   74 #include <sys/sysctl.h>
   75 #include <sys/vmmeter.h>
   76 #include <sys/vnode.h>
   77 
   78 #include <vm/vm.h>
   79 #include <vm/vm_param.h>
   80 #include <vm/vm_kern.h>
   81 #include <vm/pmap.h>
   82 #include <vm/vm_map.h>
   83 #include <vm/vm_object.h>
   84 #include <vm/vm_page.h>
   85 #include <vm/vm_pageout.h>
   86 #include <vm/vm_pager.h>
   87 #include <vm/vm_phys.h>
   88 #include <vm/vm_extern.h>
   89 
   90 static int
   91 vm_contig_launder_page(vm_page_t m, vm_page_t *next)
   92 {
   93         vm_object_t object;
   94         vm_page_t m_tmp;
   95         struct vnode *vp;
   96         struct mount *mp;
   97         int vfslocked;
   98 
   99         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
  100         object = m->object;
  101         if (!VM_OBJECT_TRYLOCK(object) &&
  102             !vm_pageout_fallback_object_lock(m, next)) {
  103                 VM_OBJECT_UNLOCK(object);
  104                 return (EAGAIN);
  105         }
  106         if (vm_page_sleep_if_busy(m, TRUE, "vpctw0")) {
  107                 VM_OBJECT_UNLOCK(object);
  108                 vm_page_lock_queues();
  109                 return (EBUSY);
  110         }
  111         vm_page_test_dirty(m);
  112         if (m->dirty == 0 && m->hold_count == 0)
  113                 pmap_remove_all(m);
  114         if (m->dirty) {
  115                 if ((object->flags & OBJ_DEAD) != 0) {
  116                         VM_OBJECT_UNLOCK(object);
  117                         return (EAGAIN);
  118                 }
  119                 if (object->type == OBJT_VNODE) {
  120                         vm_page_unlock_queues();
  121                         vp = object->handle;
  122                         vm_object_reference_locked(object);
  123                         VM_OBJECT_UNLOCK(object);
  124                         (void) vn_start_write(vp, &mp, V_WAIT);
  125                         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
  126                         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
  127                         VM_OBJECT_LOCK(object);
  128                         vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
  129                         VM_OBJECT_UNLOCK(object);
  130                         VOP_UNLOCK(vp, 0);
  131                         VFS_UNLOCK_GIANT(vfslocked);
  132                         vm_object_deallocate(object);
  133                         vn_finished_write(mp);
  134                         vm_page_lock_queues();
  135                         return (0);
  136                 } else if (object->type == OBJT_SWAP ||
  137                            object->type == OBJT_DEFAULT) {
  138                         m_tmp = m;
  139                         vm_pageout_flush(&m_tmp, 1, VM_PAGER_PUT_SYNC);
  140                         VM_OBJECT_UNLOCK(object);
  141                         return (0);
  142                 }
  143         } else if (m->hold_count == 0)
  144                 vm_page_cache(m);
  145         VM_OBJECT_UNLOCK(object);
  146         return (0);
  147 }
  148 
  149 static int
  150 vm_contig_launder(int queue)
  151 {
  152         vm_page_t m, next;
  153         int error;
  154 
  155         TAILQ_FOREACH_SAFE(m, &vm_page_queues[queue].pl, pageq, next) {
  156 
  157                 /* Skip marker pages */
  158                 if ((m->flags & PG_MARKER) != 0)
  159                         continue;
  160 
  161                 KASSERT(VM_PAGE_INQUEUE2(m, queue),
  162                     ("vm_contig_launder: page %p's queue is not %d", m, queue));
  163                 error = vm_contig_launder_page(m, &next);
  164                 if (error == 0)
  165                         return (TRUE);
  166                 if (error == EBUSY)
  167                         return (FALSE);
  168         }
  169         return (FALSE);
  170 }
  171 
  172 /*
  173  *      Frees the given physically contiguous pages.
  174  *
  175  *      N.B.: Any pages with PG_ZERO set must, in fact, be zero filled.
  176  */
  177 static void
  178 vm_page_release_contig(vm_page_t m, vm_pindex_t count)
  179 {
  180 
  181         while (count--) {
  182                 /* Leave PG_ZERO unchanged. */
  183                 vm_page_free_toq(m);
  184                 m++;
  185         }
  186 }
  187 
  188 /*
  189  *      Allocates a region from the kernel address map, inserts the
  190  *      given physically contiguous pages into the kernel object,
  191  *      creates a wired mapping from the region to the pages, and
  192  *      returns the region's starting virtual address.  If M_ZERO is
  193  *      specified through the given flags, then the pages are zeroed
  194  *      before they are mapped.
  195  */
  196 static vm_offset_t
  197 contigmapping(vm_map_t map, vm_size_t size, vm_page_t m, vm_memattr_t memattr,
  198     int flags)
  199 {
  200         vm_object_t object = kernel_object;
  201         vm_offset_t addr, tmp_addr;
  202  
  203         vm_map_lock(map);
  204         if (vm_map_findspace(map, vm_map_min(map), size, &addr)) {
  205                 vm_map_unlock(map);
  206                 return (0);
  207         }
  208         vm_object_reference(object);
  209         vm_map_insert(map, object, addr - VM_MIN_KERNEL_ADDRESS,
  210             addr, addr + size, VM_PROT_ALL, VM_PROT_ALL, 0);
  211         vm_map_unlock(map);
  212         VM_OBJECT_LOCK(object);
  213         for (tmp_addr = addr; tmp_addr < addr + size; tmp_addr += PAGE_SIZE) {
  214                 if (memattr != VM_MEMATTR_DEFAULT)
  215                         pmap_page_set_memattr(m, memattr);
  216                 vm_page_insert(m, object,
  217                     OFF_TO_IDX(tmp_addr - VM_MIN_KERNEL_ADDRESS));
  218                 if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
  219                         pmap_zero_page(m);
  220                 m->valid = VM_PAGE_BITS_ALL;
  221                 m++;
  222         }
  223         VM_OBJECT_UNLOCK(object);
  224         vm_map_wire(map, addr, addr + size,
  225             VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
  226         return (addr);
  227 }
  228 
  229 void *
  230 contigmalloc(
  231         unsigned long size,     /* should be size_t here and for malloc() */
  232         struct malloc_type *type,
  233         int flags,
  234         vm_paddr_t low,
  235         vm_paddr_t high,
  236         unsigned long alignment,
  237         unsigned long boundary)
  238 {
  239         void *ret;
  240 
  241         ret = (void *)kmem_alloc_contig(kernel_map, size, flags, low, high,
  242             alignment, boundary, VM_MEMATTR_DEFAULT);
  243         if (ret != NULL)
  244                 malloc_type_allocated(type, round_page(size));
  245         return (ret);
  246 }
  247 
  248 vm_offset_t
  249 kmem_alloc_contig(vm_map_t map, vm_size_t size, int flags, vm_paddr_t low,
  250     vm_paddr_t high, unsigned long alignment, unsigned long boundary,
  251     vm_memattr_t memattr)
  252 {
  253         vm_offset_t ret;
  254         vm_page_t pages;
  255         unsigned long npgs;
  256         int actl, actmax, inactl, inactmax, tries;
  257 
  258         size = round_page(size);
  259         npgs = size >> PAGE_SHIFT;
  260         tries = 0;
  261 retry:
  262         pages = vm_phys_alloc_contig(npgs, low, high, alignment, boundary);
  263         if (pages == NULL) {
  264                 if (tries < ((flags & M_NOWAIT) != 0 ? 1 : 3)) {
  265                         vm_page_lock_queues();
  266                         inactl = 0;
  267                         inactmax = tries < 1 ? 0 : cnt.v_inactive_count;
  268                         actl = 0;
  269                         actmax = tries < 2 ? 0 : cnt.v_active_count;
  270 again:
  271                         if (inactl < inactmax &&
  272                             vm_contig_launder(PQ_INACTIVE)) {
  273                                 inactl++;
  274                                 goto again;
  275                         }
  276                         if (actl < actmax &&
  277                             vm_contig_launder(PQ_ACTIVE)) {
  278                                 actl++;
  279                                 goto again;
  280                         }
  281                         vm_page_unlock_queues();
  282                         tries++;
  283                         goto retry;
  284                 }
  285                 ret = 0;
  286         } else {
  287                 ret = contigmapping(map, size, pages, memattr, flags);
  288                 if (ret == 0)
  289                         vm_page_release_contig(pages, npgs);
  290         }
  291         return (ret);
  292 }
  293 
  294 void
  295 contigfree(void *addr, unsigned long size, struct malloc_type *type)
  296 {
  297 
  298         kmem_free(kernel_map, (vm_offset_t)addr, size);
  299         malloc_type_freed(type, round_page(size));
  300 }

Cache object: 92ed24793ac5eb251973c663b02f0d1d


[ 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.