summaryrefslogtreecommitdiff
path: root/sys/uvm/uvm_page_status.c
blob: 19b48b53a1d75149265b1fe69535d63b76ce8b5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*	$NetBSD: uvm_page_status.c,v 1.6 2020/08/14 09:06:15 chs Exp $	*/

/*-
 * Copyright (c)2011 YAMAMOTO Takashi,
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: uvm_page_status.c,v 1.6 2020/08/14 09:06:15 chs Exp $");

#include <sys/param.h>
#include <sys/systm.h>

#include <uvm/uvm.h>

/*
 * page dirtiness status tracking
 *
 * separated from uvm_page.c mainly for rump
 */

/*
 * these constants are chosen to match so that we can convert between
 * them quickly.
 */

__CTASSERT(UVM_PAGE_STATUS_UNKNOWN == 0);
__CTASSERT(UVM_PAGE_STATUS_DIRTY == PG_DIRTY);
__CTASSERT(UVM_PAGE_STATUS_CLEAN == PG_CLEAN);

/*
 * uvm_pagegetdirty: return the dirtiness status (one of UVM_PAGE_STATUS_
 * values) of the page.
 *
 * called with the owner locked.
 */

unsigned int
uvm_pagegetdirty(struct vm_page *pg)
{
	struct uvm_object * const uobj __diagused = pg->uobject;

	KASSERT((~pg->flags & (PG_CLEAN|PG_DIRTY)) != 0);
	KASSERT(uvm_page_owner_locked_p(pg, false));
	KASSERT(uobj == NULL || ((pg->flags & PG_CLEAN) == 0) ==
		uvm_obj_page_dirty_p(pg));
	return pg->flags & (PG_CLEAN|PG_DIRTY);
}

/*
 * uvm_pagemarkdirty: set the dirtiness status (one of UVM_PAGE_STATUS_ values)
 * of the page.
 *
 * called with the owner locked.
 *
 * update the radix tree tag for object-owned page.
 *
 * if new status is UVM_PAGE_STATUS_UNKNOWN, clear pmap-level dirty bit
 * so that later uvm_pagecheckdirty() can notice modifications on the page.
 */

void
uvm_pagemarkdirty(struct vm_page *pg, unsigned int newstatus)
{
	struct uvm_object * const uobj = pg->uobject;
	const unsigned int oldstatus = uvm_pagegetdirty(pg);
	enum cpu_count base;

	KASSERT((~newstatus & (PG_CLEAN|PG_DIRTY)) != 0);
	KASSERT((newstatus & ~(PG_CLEAN|PG_DIRTY)) == 0);
	KASSERT(uvm_page_owner_locked_p(pg, true));
	KASSERT(uobj == NULL || ((pg->flags & PG_CLEAN) == 0) ==
		uvm_obj_page_dirty_p(pg));

	if (oldstatus == newstatus) {
		return;
	}

	/*
	 * set UVM_PAGE_DIRTY_TAG tag unless known CLEAN so that putpages can
	 * find possibly-dirty pages quickly.
	 */

	if (uobj != NULL) {
		if (newstatus == UVM_PAGE_STATUS_CLEAN) {
			uvm_obj_page_clear_dirty(pg);
		} else if (oldstatus == UVM_PAGE_STATUS_CLEAN) {
			/*
			 * on first dirty page, mark the object dirty.
			 * for vnodes this inserts to the syncer worklist.
			 */
			if (uvm_obj_clean_p(uobj) &&
		            uobj->pgops->pgo_markdirty != NULL) {
				(*uobj->pgops->pgo_markdirty)(uobj);
			}
			uvm_obj_page_set_dirty(pg);
		}
	}
	if (newstatus == UVM_PAGE_STATUS_UNKNOWN) {
		/*
		 * start relying on pmap-level dirtiness tracking.
		 */
		pmap_clear_modify(pg);
	}
	pg->flags &= ~(PG_CLEAN|PG_DIRTY);
	pg->flags |= newstatus;
	KASSERT(uobj == NULL || ((pg->flags & PG_CLEAN) == 0) ==
		uvm_obj_page_dirty_p(pg));
	if ((pg->flags & PG_STAT) != 0) {
		if ((pg->flags & PG_SWAPBACKED) != 0) {
			base = CPU_COUNT_ANONUNKNOWN;
		} else {
			base = CPU_COUNT_FILEUNKNOWN;
		}
		kpreempt_disable();
		CPU_COUNT(base + oldstatus, -1);
		CPU_COUNT(base + newstatus, +1);
		kpreempt_enable();
	}
}

/*
 * uvm_pagecheckdirty: check if page is dirty, and remove its dirty bit.
 *
 * called with the owner locked.
 *
 * returns if the page was dirty.
 *
 * if protected is true, mark the page CLEAN.  otherwise, mark the page UNKNOWN.
 * ("mark" in the sense of uvm_pagemarkdirty().)
 */

bool
uvm_pagecheckdirty(struct vm_page *pg, bool pgprotected)
{
	const unsigned int oldstatus = uvm_pagegetdirty(pg);
	bool modified;

	KASSERT(uvm_page_owner_locked_p(pg, true));

	/*
	 * if pgprotected is true, mark the page CLEAN.
	 * otherwise mark the page UNKNOWN unless it's CLEAN.
	 *
	 * possible transitions:
	 *
	 *	CLEAN   -> CLEAN  , modified = false
	 *	UNKNOWN -> UNKNOWN, modified = true
	 *	UNKNOWN -> UNKNOWN, modified = false
	 *	UNKNOWN -> CLEAN  , modified = true
	 *	UNKNOWN -> CLEAN  , modified = false
	 *	DIRTY   -> UNKNOWN, modified = true
	 *	DIRTY   -> CLEAN  , modified = true
	 *
	 * pmap_clear_modify is necessary if either of
	 * oldstatus or newstatus is UVM_PAGE_STATUS_UNKNOWN.
	 */

	if (oldstatus == UVM_PAGE_STATUS_CLEAN) {
		modified = false;
	} else {
		const unsigned int newstatus = pgprotected ?
		    UVM_PAGE_STATUS_CLEAN : UVM_PAGE_STATUS_UNKNOWN;

		if (oldstatus == UVM_PAGE_STATUS_DIRTY) {
			modified = true;
			if (newstatus == UVM_PAGE_STATUS_UNKNOWN) {
				pmap_clear_modify(pg);
			}
		} else {
			KASSERT(oldstatus == UVM_PAGE_STATUS_UNKNOWN);
			modified = pmap_clear_modify(pg);
		}
		uvm_pagemarkdirty(pg, newstatus);
	}
	return modified;
}