Home | History | Annotate | Line # | Download | only in uvm
      1 /*	$NetBSD: uvm_page_status.c,v 1.6 2020/08/14 09:06:15 chs Exp $	*/
      2 
      3 /*-
      4  * Copyright (c)2011 YAMAMOTO Takashi,
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: uvm_page_status.c,v 1.6 2020/08/14 09:06:15 chs Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 
     35 #include <uvm/uvm.h>
     36 
     37 /*
     38  * page dirtiness status tracking
     39  *
     40  * separated from uvm_page.c mainly for rump
     41  */
     42 
     43 /*
     44  * these constants are chosen to match so that we can convert between
     45  * them quickly.
     46  */
     47 
     48 __CTASSERT(UVM_PAGE_STATUS_UNKNOWN == 0);
     49 __CTASSERT(UVM_PAGE_STATUS_DIRTY == PG_DIRTY);
     50 __CTASSERT(UVM_PAGE_STATUS_CLEAN == PG_CLEAN);
     51 
     52 /*
     53  * uvm_pagegetdirty: return the dirtiness status (one of UVM_PAGE_STATUS_
     54  * values) of the page.
     55  *
     56  * called with the owner locked.
     57  */
     58 
     59 unsigned int
     60 uvm_pagegetdirty(struct vm_page *pg)
     61 {
     62 	struct uvm_object * const uobj __diagused = pg->uobject;
     63 
     64 	KASSERT((~pg->flags & (PG_CLEAN|PG_DIRTY)) != 0);
     65 	KASSERT(uvm_page_owner_locked_p(pg, false));
     66 	KASSERT(uobj == NULL || ((pg->flags & PG_CLEAN) == 0) ==
     67 		uvm_obj_page_dirty_p(pg));
     68 	return pg->flags & (PG_CLEAN|PG_DIRTY);
     69 }
     70 
     71 /*
     72  * uvm_pagemarkdirty: set the dirtiness status (one of UVM_PAGE_STATUS_ values)
     73  * of the page.
     74  *
     75  * called with the owner locked.
     76  *
     77  * update the radix tree tag for object-owned page.
     78  *
     79  * if new status is UVM_PAGE_STATUS_UNKNOWN, clear pmap-level dirty bit
     80  * so that later uvm_pagecheckdirty() can notice modifications on the page.
     81  */
     82 
     83 void
     84 uvm_pagemarkdirty(struct vm_page *pg, unsigned int newstatus)
     85 {
     86 	struct uvm_object * const uobj = pg->uobject;
     87 	const unsigned int oldstatus = uvm_pagegetdirty(pg);
     88 	enum cpu_count base;
     89 
     90 	KASSERT((~newstatus & (PG_CLEAN|PG_DIRTY)) != 0);
     91 	KASSERT((newstatus & ~(PG_CLEAN|PG_DIRTY)) == 0);
     92 	KASSERT(uvm_page_owner_locked_p(pg, true));
     93 	KASSERT(uobj == NULL || ((pg->flags & PG_CLEAN) == 0) ==
     94 		uvm_obj_page_dirty_p(pg));
     95 
     96 	if (oldstatus == newstatus) {
     97 		return;
     98 	}
     99 
    100 	/*
    101 	 * set UVM_PAGE_DIRTY_TAG tag unless known CLEAN so that putpages can
    102 	 * find possibly-dirty pages quickly.
    103 	 */
    104 
    105 	if (uobj != NULL) {
    106 		if (newstatus == UVM_PAGE_STATUS_CLEAN) {
    107 			uvm_obj_page_clear_dirty(pg);
    108 		} else if (oldstatus == UVM_PAGE_STATUS_CLEAN) {
    109 			/*
    110 			 * on first dirty page, mark the object dirty.
    111 			 * for vnodes this inserts to the syncer worklist.
    112 			 */
    113 			if (uvm_obj_clean_p(uobj) &&
    114 		            uobj->pgops->pgo_markdirty != NULL) {
    115 				(*uobj->pgops->pgo_markdirty)(uobj);
    116 			}
    117 			uvm_obj_page_set_dirty(pg);
    118 		}
    119 	}
    120 	if (newstatus == UVM_PAGE_STATUS_UNKNOWN) {
    121 		/*
    122 		 * start relying on pmap-level dirtiness tracking.
    123 		 */
    124 		pmap_clear_modify(pg);
    125 	}
    126 	pg->flags &= ~(PG_CLEAN|PG_DIRTY);
    127 	pg->flags |= newstatus;
    128 	KASSERT(uobj == NULL || ((pg->flags & PG_CLEAN) == 0) ==
    129 		uvm_obj_page_dirty_p(pg));
    130 	if ((pg->flags & PG_STAT) != 0) {
    131 		if ((pg->flags & PG_SWAPBACKED) != 0) {
    132 			base = CPU_COUNT_ANONUNKNOWN;
    133 		} else {
    134 			base = CPU_COUNT_FILEUNKNOWN;
    135 		}
    136 		kpreempt_disable();
    137 		CPU_COUNT(base + oldstatus, -1);
    138 		CPU_COUNT(base + newstatus, +1);
    139 		kpreempt_enable();
    140 	}
    141 }
    142 
    143 /*
    144  * uvm_pagecheckdirty: check if page is dirty, and remove its dirty bit.
    145  *
    146  * called with the owner locked.
    147  *
    148  * returns if the page was dirty.
    149  *
    150  * if protected is true, mark the page CLEAN.  otherwise, mark the page UNKNOWN.
    151  * ("mark" in the sense of uvm_pagemarkdirty().)
    152  */
    153 
    154 bool
    155 uvm_pagecheckdirty(struct vm_page *pg, bool pgprotected)
    156 {
    157 	const unsigned int oldstatus = uvm_pagegetdirty(pg);
    158 	bool modified;
    159 
    160 	KASSERT(uvm_page_owner_locked_p(pg, true));
    161 
    162 	/*
    163 	 * if pgprotected is true, mark the page CLEAN.
    164 	 * otherwise mark the page UNKNOWN unless it's CLEAN.
    165 	 *
    166 	 * possible transitions:
    167 	 *
    168 	 *	CLEAN   -> CLEAN  , modified = false
    169 	 *	UNKNOWN -> UNKNOWN, modified = true
    170 	 *	UNKNOWN -> UNKNOWN, modified = false
    171 	 *	UNKNOWN -> CLEAN  , modified = true
    172 	 *	UNKNOWN -> CLEAN  , modified = false
    173 	 *	DIRTY   -> UNKNOWN, modified = true
    174 	 *	DIRTY   -> CLEAN  , modified = true
    175 	 *
    176 	 * pmap_clear_modify is necessary if either of
    177 	 * oldstatus or newstatus is UVM_PAGE_STATUS_UNKNOWN.
    178 	 */
    179 
    180 	if (oldstatus == UVM_PAGE_STATUS_CLEAN) {
    181 		modified = false;
    182 	} else {
    183 		const unsigned int newstatus = pgprotected ?
    184 		    UVM_PAGE_STATUS_CLEAN : UVM_PAGE_STATUS_UNKNOWN;
    185 
    186 		if (oldstatus == UVM_PAGE_STATUS_DIRTY) {
    187 			modified = true;
    188 			if (newstatus == UVM_PAGE_STATUS_UNKNOWN) {
    189 				pmap_clear_modify(pg);
    190 			}
    191 		} else {
    192 			KASSERT(oldstatus == UVM_PAGE_STATUS_UNKNOWN);
    193 			modified = pmap_clear_modify(pg);
    194 		}
    195 		uvm_pagemarkdirty(pg, newstatus);
    196 	}
    197 	return modified;
    198 }
    199