Home | History | Annotate | Line # | Download | only in uvm
uvm_page_status.c revision 1.1.2.1
      1  1.1.2.1  yamt /*	$NetBSD: uvm_page_status.c,v 1.1.2.1 2011/11/02 21:55:39 yamt Exp $	*/
      2  1.1.2.1  yamt 
      3  1.1.2.1  yamt /*-
      4  1.1.2.1  yamt  * Copyright (c)2011 YAMAMOTO Takashi,
      5  1.1.2.1  yamt  * All rights reserved.
      6  1.1.2.1  yamt  *
      7  1.1.2.1  yamt  * Redistribution and use in source and binary forms, with or without
      8  1.1.2.1  yamt  * modification, are permitted provided that the following conditions
      9  1.1.2.1  yamt  * are met:
     10  1.1.2.1  yamt  * 1. Redistributions of source code must retain the above copyright
     11  1.1.2.1  yamt  *    notice, this list of conditions and the following disclaimer.
     12  1.1.2.1  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1.2.1  yamt  *    notice, this list of conditions and the following disclaimer in the
     14  1.1.2.1  yamt  *    documentation and/or other materials provided with the distribution.
     15  1.1.2.1  yamt  *
     16  1.1.2.1  yamt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  1.1.2.1  yamt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.1.2.1  yamt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.1.2.1  yamt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  1.1.2.1  yamt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.1.2.1  yamt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.1.2.1  yamt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1.2.1  yamt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.1.2.1  yamt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1.2.1  yamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1.2.1  yamt  * SUCH DAMAGE.
     27  1.1.2.1  yamt  */
     28  1.1.2.1  yamt 
     29  1.1.2.1  yamt #include <sys/cdefs.h>
     30  1.1.2.1  yamt __KERNEL_RCSID(0, "$NetBSD: uvm_page_status.c,v 1.1.2.1 2011/11/02 21:55:39 yamt Exp $");
     31  1.1.2.1  yamt 
     32  1.1.2.1  yamt #include <sys/param.h>
     33  1.1.2.1  yamt #include <sys/systm.h>
     34  1.1.2.1  yamt 
     35  1.1.2.1  yamt #include <uvm/uvm.h>
     36  1.1.2.1  yamt 
     37  1.1.2.1  yamt /*
     38  1.1.2.1  yamt  * page dirtiness status tracking
     39  1.1.2.1  yamt  *
     40  1.1.2.1  yamt  * separated from uvm_page.c mainly for rump
     41  1.1.2.1  yamt  */
     42  1.1.2.1  yamt 
     43  1.1.2.1  yamt /*
     44  1.1.2.1  yamt  * uvm_pagegetdirty: return the dirtiness status (one of UVM_PAGE_STATUS_
     45  1.1.2.1  yamt  * values) of the page.
     46  1.1.2.1  yamt  */
     47  1.1.2.1  yamt 
     48  1.1.2.1  yamt unsigned int
     49  1.1.2.1  yamt uvm_pagegetdirty(struct vm_page *pg)
     50  1.1.2.1  yamt {
     51  1.1.2.1  yamt 	struct uvm_object * const uobj __unused = pg->uobject;
     52  1.1.2.1  yamt 	const uint64_t idx __unused = pg->offset >> PAGE_SHIFT;
     53  1.1.2.1  yamt 
     54  1.1.2.1  yamt 	KASSERT((~pg->flags & (PG_CLEAN|PG_DIRTY)) != 0);
     55  1.1.2.1  yamt 	KASSERT(uvm_page_locked_p(pg));
     56  1.1.2.1  yamt 	KASSERT(uobj == NULL || ((pg->flags & PG_CLEAN) == 0) ==
     57  1.1.2.1  yamt 	    radix_tree_get_tag(&uobj->uo_pages, idx, UVM_PAGE_DIRTY_TAG));
     58  1.1.2.1  yamt 	return pg->flags & (PG_CLEAN|PG_DIRTY);
     59  1.1.2.1  yamt }
     60  1.1.2.1  yamt 
     61  1.1.2.1  yamt /*
     62  1.1.2.1  yamt  * uvm_pagemarkdirty: set the dirtiness status (one of UVM_PAGE_STATUS_ values)
     63  1.1.2.1  yamt  * of the page.
     64  1.1.2.1  yamt  */
     65  1.1.2.1  yamt 
     66  1.1.2.1  yamt void
     67  1.1.2.1  yamt uvm_pagemarkdirty(struct vm_page *pg, unsigned int status)
     68  1.1.2.1  yamt {
     69  1.1.2.1  yamt 	struct uvm_object * const uobj = pg->uobject;
     70  1.1.2.1  yamt 	const uint64_t idx = pg->offset >> PAGE_SHIFT;
     71  1.1.2.1  yamt 
     72  1.1.2.1  yamt 	KASSERT((~status & (PG_CLEAN|PG_DIRTY)) != 0);
     73  1.1.2.1  yamt 	KASSERT((status & ~(PG_CLEAN|PG_DIRTY)) == 0);
     74  1.1.2.1  yamt 	KASSERT(uvm_page_locked_p(pg));
     75  1.1.2.1  yamt 	KASSERT(uobj == NULL || ((pg->flags & PG_CLEAN) == 0) ==
     76  1.1.2.1  yamt 	    radix_tree_get_tag(&uobj->uo_pages, idx, UVM_PAGE_DIRTY_TAG));
     77  1.1.2.1  yamt 
     78  1.1.2.1  yamt 	if (uvm_pagegetdirty(pg) == status) {
     79  1.1.2.1  yamt 		return;
     80  1.1.2.1  yamt 	}
     81  1.1.2.1  yamt 	/*
     82  1.1.2.1  yamt 	 * set UVM_PAGE_DIRTY_TAG tag unless known CLEAN so that putpages can
     83  1.1.2.1  yamt 	 * find possibly-dirty pages quickly.
     84  1.1.2.1  yamt 	 */
     85  1.1.2.1  yamt 	if (uobj != NULL) {
     86  1.1.2.1  yamt 		if (status == UVM_PAGE_STATUS_CLEAN) {
     87  1.1.2.1  yamt 			radix_tree_clear_tag(&uobj->uo_pages, idx,
     88  1.1.2.1  yamt 			    UVM_PAGE_DIRTY_TAG);
     89  1.1.2.1  yamt 		} else {
     90  1.1.2.1  yamt 			radix_tree_set_tag(&uobj->uo_pages, idx,
     91  1.1.2.1  yamt 			    UVM_PAGE_DIRTY_TAG);
     92  1.1.2.1  yamt 		}
     93  1.1.2.1  yamt 	}
     94  1.1.2.1  yamt 	if (status == UVM_PAGE_STATUS_UNKNOWN) {
     95  1.1.2.1  yamt 		/*
     96  1.1.2.1  yamt 		 * start relying on pmap-level dirtiness tracking.
     97  1.1.2.1  yamt 		 */
     98  1.1.2.1  yamt 		pmap_clear_modify(pg);
     99  1.1.2.1  yamt 	}
    100  1.1.2.1  yamt 	pg->flags &= ~(PG_CLEAN|PG_DIRTY);
    101  1.1.2.1  yamt 	pg->flags |= status;
    102  1.1.2.1  yamt 	KASSERT(uobj == NULL || ((pg->flags & PG_CLEAN) == 0) ==
    103  1.1.2.1  yamt 	    radix_tree_get_tag(&uobj->uo_pages, idx, UVM_PAGE_DIRTY_TAG));
    104  1.1.2.1  yamt }
    105  1.1.2.1  yamt 
    106  1.1.2.1  yamt /*
    107  1.1.2.1  yamt  * uvm_pagecheckdirty: check if page is dirty, and remove its dirty-marks.
    108  1.1.2.1  yamt  *
    109  1.1.2.1  yamt  * called with the owner locked.
    110  1.1.2.1  yamt  */
    111  1.1.2.1  yamt 
    112  1.1.2.1  yamt bool
    113  1.1.2.1  yamt uvm_pagecheckdirty(struct vm_page *pg, bool protected)
    114  1.1.2.1  yamt {
    115  1.1.2.1  yamt 	const unsigned int oldstatus = uvm_pagegetdirty(pg);
    116  1.1.2.1  yamt 	bool modified;
    117  1.1.2.1  yamt 
    118  1.1.2.1  yamt 	KASSERT(uvm_page_locked_p(pg));
    119  1.1.2.1  yamt 
    120  1.1.2.1  yamt 	/*
    121  1.1.2.1  yamt 	 * if protected is true, mark the page CLEAN.
    122  1.1.2.1  yamt 	 * otherwise mark the page UNKNOWN unless it's CLEAN.
    123  1.1.2.1  yamt 	 *
    124  1.1.2.1  yamt 	 * possible transitions:
    125  1.1.2.1  yamt 	 *
    126  1.1.2.1  yamt 	 *	CLEAN   -> CLEAN  , modified = false
    127  1.1.2.1  yamt 	 *	UNKNOWN -> UNKNOWN, modified = true
    128  1.1.2.1  yamt 	 *	UNKNOWN -> UNKNOWN, modified = false
    129  1.1.2.1  yamt 	 *	UNKNOWN -> CLEAN  , modified = true
    130  1.1.2.1  yamt 	 *	UNKNOWN -> CLEAN  , modified = false
    131  1.1.2.1  yamt 	 *	DIRTY   -> UNKNOWN, modified = true
    132  1.1.2.1  yamt 	 *	DIRTY   -> CLEAN  , modified = true
    133  1.1.2.1  yamt 	 *
    134  1.1.2.1  yamt 	 * pmap_clear_modify is necessary if either of
    135  1.1.2.1  yamt 	 * oldstatus or newstatus is UVM_PAGE_STATUS_UNKNOWN.
    136  1.1.2.1  yamt 	 */
    137  1.1.2.1  yamt 
    138  1.1.2.1  yamt 	if (oldstatus == UVM_PAGE_STATUS_CLEAN) {
    139  1.1.2.1  yamt 		modified = false;
    140  1.1.2.1  yamt 	} else {
    141  1.1.2.1  yamt 		const unsigned int newstatus =
    142  1.1.2.1  yamt 		    protected ? UVM_PAGE_STATUS_CLEAN : UVM_PAGE_STATUS_UNKNOWN;
    143  1.1.2.1  yamt 
    144  1.1.2.1  yamt 		if (oldstatus == UVM_PAGE_STATUS_DIRTY) {
    145  1.1.2.1  yamt 			modified = true;
    146  1.1.2.1  yamt 			if (newstatus == UVM_PAGE_STATUS_UNKNOWN) {
    147  1.1.2.1  yamt 				pmap_clear_modify(pg);
    148  1.1.2.1  yamt 			}
    149  1.1.2.1  yamt 		} else {
    150  1.1.2.1  yamt 			modified = pmap_clear_modify(pg);
    151  1.1.2.1  yamt 		}
    152  1.1.2.1  yamt 		uvm_pagemarkdirty(pg, newstatus);
    153  1.1.2.1  yamt 	}
    154  1.1.2.1  yamt 	return modified;
    155  1.1.2.1  yamt }
    156