uvm_page_status.c revision 1.2 1 /* $NetBSD: uvm_page_status.c,v 1.2 2020/01/15 17:55:45 ad 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.2 2020/01/15 17:55:45 ad 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 const uint64_t idx __diagused = pg->offset >> PAGE_SHIFT;
64
65 KASSERT((~pg->flags & (PG_CLEAN|PG_DIRTY)) != 0);
66 KASSERT(uvm_page_owner_locked_p(pg));
67 KASSERT(uobj == NULL || ((pg->flags & PG_CLEAN) == 0) ==
68 !!radix_tree_get_tag(&uobj->uo_pages, idx, UVM_PAGE_DIRTY_TAG));
69 return pg->flags & (PG_CLEAN|PG_DIRTY);
70 }
71
72 /*
73 * uvm_pagemarkdirty: set the dirtiness status (one of UVM_PAGE_STATUS_ values)
74 * of the page.
75 *
76 * called with the owner locked.
77 *
78 * update the radix tree tag for object-owned page.
79 *
80 * if new status is UVM_PAGE_STATUS_UNKNOWN, clear pmap-level dirty bit
81 * so that later uvm_pagecheckdirty() can notice modifications on the page.
82 */
83
84 void
85 uvm_pagemarkdirty(struct vm_page *pg, unsigned int newstatus)
86 {
87 struct uvm_object * const uobj = pg->uobject;
88 const uint64_t idx = pg->offset >> PAGE_SHIFT;
89 const unsigned int oldstatus = uvm_pagegetdirty(pg);
90 enum cpu_count base;
91
92 KASSERT((~newstatus & (PG_CLEAN|PG_DIRTY)) != 0);
93 KASSERT((newstatus & ~(PG_CLEAN|PG_DIRTY)) == 0);
94 KASSERT(uvm_page_owner_locked_p(pg));
95 KASSERT(uobj == NULL || ((pg->flags & PG_CLEAN) == 0) ==
96 !!radix_tree_get_tag(&uobj->uo_pages, idx, UVM_PAGE_DIRTY_TAG));
97
98 if (oldstatus == newstatus) {
99 return;
100 }
101
102 /*
103 * set UVM_PAGE_DIRTY_TAG tag unless known CLEAN so that putpages can
104 * find possibly-dirty pages quickly.
105 */
106
107 if (uobj != NULL) {
108 if (newstatus == UVM_PAGE_STATUS_CLEAN) {
109 radix_tree_clear_tag(&uobj->uo_pages, idx,
110 UVM_PAGE_DIRTY_TAG);
111 } else {
112 radix_tree_set_tag(&uobj->uo_pages, idx,
113 UVM_PAGE_DIRTY_TAG);
114 }
115 }
116 if (newstatus == UVM_PAGE_STATUS_UNKNOWN) {
117 /*
118 * start relying on pmap-level dirtiness tracking.
119 */
120 pmap_clear_modify(pg);
121 }
122 pg->flags &= ~(PG_CLEAN|PG_DIRTY);
123 pg->flags |= newstatus;
124 KASSERT(uobj == NULL || ((pg->flags & PG_CLEAN) == 0) ==
125 !!radix_tree_get_tag(&uobj->uo_pages, idx, UVM_PAGE_DIRTY_TAG));
126 if ((pg->flags & PG_STAT) != 0) {
127 if ((pg->flags & PG_SWAPBACKED) != 0) {
128 base = CPU_COUNT_ANONUNKNOWN;
129 } else {
130 base = CPU_COUNT_FILEUNKNOWN;
131 }
132 kpreempt_disable();
133 CPU_COUNT(base + oldstatus, -1);
134 CPU_COUNT(base + newstatus, +1);
135 kpreempt_enable();
136 }
137 }
138
139 /*
140 * uvm_pagecheckdirty: check if page is dirty, and remove its dirty bit.
141 *
142 * called with the owner locked.
143 *
144 * returns if the page was dirty.
145 *
146 * if protected is true, mark the page CLEAN. otherwise, mark the page UNKNOWN.
147 * ("mark" in the sense of uvm_pagemarkdirty().)
148 */
149
150 bool
151 uvm_pagecheckdirty(struct vm_page *pg, bool pgprotected)
152 {
153 const unsigned int oldstatus = uvm_pagegetdirty(pg);
154 bool modified;
155
156 KASSERT(uvm_page_owner_locked_p(pg));
157
158 /*
159 * if pgprotected is true, mark the page CLEAN.
160 * otherwise mark the page UNKNOWN unless it's CLEAN.
161 *
162 * possible transitions:
163 *
164 * CLEAN -> CLEAN , modified = false
165 * UNKNOWN -> UNKNOWN, modified = true
166 * UNKNOWN -> UNKNOWN, modified = false
167 * UNKNOWN -> CLEAN , modified = true
168 * UNKNOWN -> CLEAN , modified = false
169 * DIRTY -> UNKNOWN, modified = true
170 * DIRTY -> CLEAN , modified = true
171 *
172 * pmap_clear_modify is necessary if either of
173 * oldstatus or newstatus is UVM_PAGE_STATUS_UNKNOWN.
174 */
175
176 if (oldstatus == UVM_PAGE_STATUS_CLEAN) {
177 modified = false;
178 } else {
179 const unsigned int newstatus = pgprotected ?
180 UVM_PAGE_STATUS_CLEAN : UVM_PAGE_STATUS_UNKNOWN;
181
182 if (oldstatus == UVM_PAGE_STATUS_DIRTY) {
183 modified = true;
184 if (newstatus == UVM_PAGE_STATUS_UNKNOWN) {
185 pmap_clear_modify(pg);
186 }
187 } else {
188 KASSERT(oldstatus == UVM_PAGE_STATUS_UNKNOWN);
189 modified = pmap_clear_modify(pg);
190 }
191 uvm_pagemarkdirty(pg, newstatus);
192 }
193 return modified;
194 }
195