xref: /openbsd-src/sys/uvm/uvm_object.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: uvm_object.c,v 1.8 2014/05/08 20:08:50 kettenis Exp $	*/
2 
3 /*
4  * Copyright (c) 2006 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  */
36 
37 #include <sys/param.h>
38 #include <sys/proc.h>		/* XXX for atomic */
39 
40 #include <uvm/uvm.h>
41 
42 /* We will fetch this page count per step */
43 #define	FETCH_PAGECOUNT	16
44 
45 /*
46  * uvm_objinit: initialise a uvm object.
47  */
48 void
49 uvm_objinit(struct uvm_object *uobj, struct uvm_pagerops *pgops, int refs)
50 {
51 	uobj->pgops = pgops;
52 	RB_INIT(&uobj->memt);
53 	uobj->uo_npages = 0;
54 	uobj->uo_refs = refs;
55 }
56 
57 #ifndef SMALL_KERNEL
58 /*
59  * uvm_objwire: wire the pages of entire uobj
60  *
61  * => caller must pass page-aligned start and end values
62  * => if the caller passes in a pageq pointer, we'll return a list of
63  *  wired pages.
64  */
65 
66 int
67 uvm_objwire(struct uvm_object *uobj, voff_t start, voff_t end,
68     struct pglist *pageq)
69 {
70 	int i, npages, left, error;
71 	struct vm_page *pgs[FETCH_PAGECOUNT];
72 	voff_t offset = start;
73 
74 	left = (end - start) >> PAGE_SHIFT;
75 
76 	while (left) {
77 
78 		npages = MIN(FETCH_PAGECOUNT, left);
79 
80 		/* Get the pages */
81 		memset(pgs, 0, sizeof(pgs));
82 		error = (*uobj->pgops->pgo_get)(uobj, offset, pgs, &npages, 0,
83 			VM_PROT_READ | VM_PROT_WRITE, UVM_ADV_SEQUENTIAL,
84 			PGO_ALLPAGES | PGO_SYNCIO);
85 
86 		if (error)
87 			goto error;
88 
89 		for (i = 0; i < npages; i++) {
90 
91 			KASSERT(pgs[i] != NULL);
92 			KASSERT(!(pgs[i]->pg_flags & PG_RELEASED));
93 
94 #if 0
95 			/*
96 			 * Loan break
97 			 */
98 			if (pgs[i]->loan_count) {
99 				while (pgs[i]->loan_count) {
100 					pg = uvm_loanbreak(pgs[i]);
101 					if (!pg) {
102 						uvm_wait("uobjwirepg");
103 						continue;
104 					}
105 				}
106 				pgs[i] = pg;
107 			}
108 #endif
109 
110 			if (pgs[i]->pg_flags & PQ_AOBJ) {
111 				atomic_clearbits_int(&pgs[i]->pg_flags,
112 				    PG_CLEAN);
113 				uao_dropswap(uobj, i);
114 			}
115 		}
116 
117 		/* Wire the pages */
118 		uvm_lock_pageq();
119 		for (i = 0; i < npages; i++) {
120 			uvm_pagewire(pgs[i]);
121 			if (pageq != NULL)
122 				TAILQ_INSERT_TAIL(pageq, pgs[i], pageq);
123 		}
124 		uvm_unlock_pageq();
125 
126 		/* Unbusy the pages */
127 		uvm_page_unbusy(pgs, npages);
128 
129 		left -= npages;
130 		offset += (voff_t)npages << PAGE_SHIFT;
131 	}
132 
133 	return 0;
134 
135 error:
136 	/* Unwire the pages which have been wired */
137 	uvm_objunwire(uobj, start, offset);
138 
139 	return error;
140 }
141 
142 /*
143  * uobj_unwirepages: unwire the pages of entire uobj
144  *
145  * => caller must pass page-aligned start and end values
146  */
147 
148 void
149 uvm_objunwire(struct uvm_object *uobj, voff_t start, voff_t end)
150 {
151 	struct vm_page *pg;
152 	off_t offset;
153 
154 	uvm_lock_pageq();
155 	for (offset = start; offset < end; offset += PAGE_SIZE) {
156 		pg = uvm_pagelookup(uobj, offset);
157 
158 		KASSERT(pg != NULL);
159 		KASSERT(!(pg->pg_flags & PG_RELEASED));
160 
161 		uvm_pageunwire(pg);
162 	}
163 	uvm_unlock_pageq();
164 }
165 #endif /* !SMALL_KERNEL */
166