xref: /netbsd-src/sys/uvm/uvm_object.c (revision a6f3f22f245acb8ee3bbf6871d7dce989204fa97)
1 /*	$NetBSD: uvm_object.c,v 1.13 2015/08/24 22:50:32 pooka Exp $	*/
2 
3 /*
4  * Copyright (c) 2006, 2010 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Mindaugas Rasiukevicius.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * uvm_object.c: operate with memory objects
34  *
35  * TODO:
36  *  1. Support PG_RELEASED-using objects
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: uvm_object.c,v 1.13 2015/08/24 22:50:32 pooka Exp $");
41 
42 #ifdef _KERNEL_OPT
43 #include "opt_ddb.h"
44 #endif
45 
46 #include <sys/param.h>
47 #include <sys/mutex.h>
48 #include <sys/queue.h>
49 #include <sys/rbtree.h>
50 
51 #include <uvm/uvm.h>
52 #include <uvm/uvm_ddb.h>
53 
54 /* Page count to fetch per single step. */
55 #define	FETCH_PAGECOUNT			16
56 
57 /*
58  * uvm_obj_init: initialize UVM memory object.
59  */
60 void
61 uvm_obj_init(struct uvm_object *uo, const struct uvm_pagerops *ops,
62     bool alock, u_int refs)
63 {
64 
65 	if (alock) {
66 		/* Allocate and assign a lock. */
67 		uo->vmobjlock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
68 	} else {
69 		/* The lock will need to be set via uvm_obj_setlock(). */
70 		uo->vmobjlock = NULL;
71 	}
72 	uo->pgops = ops;
73 	TAILQ_INIT(&uo->memq);
74 	LIST_INIT(&uo->uo_ubc);
75 	uo->uo_npages = 0;
76 	uo->uo_refs = refs;
77 	rb_tree_init(&uo->rb_tree, &uvm_page_tree_ops);
78 }
79 
80 /*
81  * uvm_obj_destroy: destroy UVM memory object.
82  */
83 void
84 uvm_obj_destroy(struct uvm_object *uo, bool dlock)
85 {
86 
87 	KASSERT(rb_tree_iterate(&uo->rb_tree, NULL, RB_DIR_LEFT) == NULL);
88 
89 	/* Purge any UBC entries associated with this object. */
90 	ubc_purge(uo);
91 
92 	/* Destroy the lock, if requested. */
93 	if (dlock) {
94 		mutex_obj_free(uo->vmobjlock);
95 	}
96 }
97 
98 /*
99  * uvm_obj_setlock: assign a vmobjlock to the UVM object.
100  *
101  * => Caller is responsible to ensure that UVM objects is not use.
102  * => Only dynamic lock may be previously set.  We drop the reference then.
103  */
104 void
105 uvm_obj_setlock(struct uvm_object *uo, kmutex_t *lockptr)
106 {
107 	kmutex_t *olockptr = uo->vmobjlock;
108 
109 	if (olockptr) {
110 		/* Drop the reference on the old lock. */
111 		mutex_obj_free(olockptr);
112 	}
113 	if (lockptr == NULL) {
114 		/* If new lock is not passed - allocate default one. */
115 		lockptr = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
116 	}
117 	uo->vmobjlock = lockptr;
118 }
119 
120 /*
121  * uvm_obj_wirepages: wire the pages of entire UVM object.
122  *
123  * => NOTE: this function should only be used for types of objects
124  *  where PG_RELEASED flag is never set (aobj objects)
125  * => caller must pass page-aligned start and end values
126  */
127 int
128 uvm_obj_wirepages(struct uvm_object *uobj, off_t start, off_t end,
129     struct pglist *list)
130 {
131 	int i, npages, error;
132 	struct vm_page *pgs[FETCH_PAGECOUNT], *pg = NULL;
133 	off_t offset = start, left;
134 
135 	left = (end - start) >> PAGE_SHIFT;
136 
137 	mutex_enter(uobj->vmobjlock);
138 	while (left) {
139 
140 		npages = MIN(FETCH_PAGECOUNT, left);
141 
142 		/* Get the pages */
143 		memset(pgs, 0, sizeof(pgs));
144 		error = (*uobj->pgops->pgo_get)(uobj, offset, pgs, &npages, 0,
145 			VM_PROT_READ | VM_PROT_WRITE, UVM_ADV_SEQUENTIAL,
146 			PGO_ALLPAGES | PGO_SYNCIO);
147 
148 		if (error)
149 			goto error;
150 
151 		mutex_enter(uobj->vmobjlock);
152 		for (i = 0; i < npages; i++) {
153 
154 			KASSERT(pgs[i] != NULL);
155 			KASSERT(!(pgs[i]->flags & PG_RELEASED));
156 
157 			/*
158 			 * Loan break
159 			 */
160 			if (pgs[i]->loan_count) {
161 				while (pgs[i]->loan_count) {
162 					pg = uvm_loanbreak(pgs[i]);
163 					if (!pg) {
164 						mutex_exit(uobj->vmobjlock);
165 						uvm_wait("uobjwirepg");
166 						mutex_enter(uobj->vmobjlock);
167 						continue;
168 					}
169 				}
170 				pgs[i] = pg;
171 			}
172 
173 			if (pgs[i]->pqflags & PQ_AOBJ) {
174 				pgs[i]->flags &= ~(PG_CLEAN);
175 				uao_dropswap(uobj, i);
176 			}
177 		}
178 
179 		/* Wire the pages */
180 		mutex_enter(&uvm_pageqlock);
181 		for (i = 0; i < npages; i++) {
182 			uvm_pagewire(pgs[i]);
183 			if (list != NULL)
184 				TAILQ_INSERT_TAIL(list, pgs[i], pageq.queue);
185 		}
186 		mutex_exit(&uvm_pageqlock);
187 
188 		/* Unbusy the pages */
189 		uvm_page_unbusy(pgs, npages);
190 
191 		left -= npages;
192 		offset += npages << PAGE_SHIFT;
193 	}
194 	mutex_exit(uobj->vmobjlock);
195 
196 	return 0;
197 
198 error:
199 	/* Unwire the pages which has been wired */
200 	uvm_obj_unwirepages(uobj, start, offset);
201 
202 	return error;
203 }
204 
205 /*
206  * uvm_obj_unwirepages: unwire the pages of entire UVM object.
207  *
208  * => NOTE: this function should only be used for types of objects
209  *  where PG_RELEASED flag is never set
210  * => caller must pass page-aligned start and end values
211  */
212 void
213 uvm_obj_unwirepages(struct uvm_object *uobj, off_t start, off_t end)
214 {
215 	struct vm_page *pg;
216 	off_t offset;
217 
218 	mutex_enter(uobj->vmobjlock);
219 	mutex_enter(&uvm_pageqlock);
220 	for (offset = start; offset < end; offset += PAGE_SIZE) {
221 		pg = uvm_pagelookup(uobj, offset);
222 
223 		KASSERT(pg != NULL);
224 		KASSERT(!(pg->flags & PG_RELEASED));
225 
226 		uvm_pageunwire(pg);
227 	}
228 	mutex_exit(&uvm_pageqlock);
229 	mutex_exit(uobj->vmobjlock);
230 }
231 
232 #if defined(DDB) || defined(DEBUGPRINT)
233 
234 /*
235  * uvm_object_printit: actually prints the object
236  */
237 void
238 uvm_object_printit(struct uvm_object *uobj, bool full,
239     void (*pr)(const char *, ...))
240 {
241 	struct vm_page *pg;
242 	int cnt = 0;
243 
244 	(*pr)("OBJECT %p: locked=%d, pgops=%p, npages=%d, ",
245 	    uobj, mutex_owned(uobj->vmobjlock), uobj->pgops, uobj->uo_npages);
246 	if (UVM_OBJ_IS_KERN_OBJECT(uobj))
247 		(*pr)("refs=<SYSTEM>\n");
248 	else
249 		(*pr)("refs=%d\n", uobj->uo_refs);
250 
251 	if (!full) {
252 		return;
253 	}
254 	(*pr)("  PAGES <pg,offset>:\n  ");
255 	TAILQ_FOREACH(pg, &uobj->memq, listq.queue) {
256 		cnt++;
257 		(*pr)("<%p,0x%llx> ", pg, (long long)pg->offset);
258 		if ((cnt % 3) == 0) {
259 			(*pr)("\n  ");
260 		}
261 	}
262 	if ((cnt % 3) != 0) {
263 		(*pr)("\n");
264 	}
265 }
266 
267 #endif /* DDB || DEBUGPRINT */
268