xref: /netbsd-src/sys/uvm/uvm_pglist.c (revision 8a8f936f250a330d54f8a24ed0e92aadf9743a7b)
1 /*	$NetBSD: uvm_pglist.c,v 1.18 2001/09/15 20:36:47 chs Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *      This product includes software developed by the NetBSD
22  *      Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * uvm_pglist.c: pglist functions
42  */
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/malloc.h>
47 #include <sys/proc.h>
48 
49 #include <uvm/uvm.h>
50 
51 #ifdef VM_PAGE_ALLOC_MEMORY_STATS
52 #define	STAT_INCR(v)	(v)++
53 #define	STAT_DECR(v)	do { \
54 		if ((v) == 0) \
55 			printf("%s:%d -- Already 0!\n", __FILE__, __LINE__); \
56 		else \
57 			(v)--; \
58 	} while (0)
59 u_long	uvm_pglistalloc_npages;
60 #else
61 #define	STAT_INCR(v)
62 #define	STAT_DECR(v)
63 #endif
64 
65 /*
66  * uvm_pglistalloc: allocate a list of pages
67  *
68  * => allocated pages are placed at the tail of rlist.  rlist is
69  *    assumed to be properly initialized by caller.
70  * => returns 0 on success or errno on failure
71  * => XXX: implementation allocates only a single segment, also
72  *	might be able to better advantage of vm_physeg[].
73  * => doesn't take into account clean non-busy pages on inactive list
74  *	that could be used(?)
75  * => params:
76  *	size		the size of the allocation, rounded to page size.
77  *	low		the low address of the allowed allocation range.
78  *	high		the high address of the allowed allocation range.
79  *	alignment	memory must be aligned to this power-of-two boundary.
80  *	boundary	no segment in the allocation may cross this
81  *			power-of-two boundary (relative to zero).
82  */
83 
84 int
85 uvm_pglistalloc(size, low, high, alignment, boundary, rlist, nsegs, waitok)
86 	psize_t size;
87 	paddr_t low, high, alignment, boundary;
88 	struct pglist *rlist;
89 	int nsegs, waitok;
90 {
91 	paddr_t try, idxpa, lastidxpa;
92 	int psi;
93 	struct vm_page *pgs, *pg;
94 	int s, tryidx, idx, pgflidx, end, error, free_list, color;
95 	u_long pagemask;
96 #ifdef DEBUG
97 	struct vm_page *tp;
98 #endif
99 
100 	KASSERT((alignment & (alignment - 1)) == 0);
101 	KASSERT((boundary & (boundary - 1)) == 0);
102 
103 	/*
104 	 * Our allocations are always page granularity, so our alignment
105 	 * must be, too.
106 	 */
107 
108 	if (alignment < PAGE_SIZE)
109 		alignment = PAGE_SIZE;
110 	size = round_page(size);
111 	try = roundup(low, alignment);
112 	if (boundary != 0 && boundary < size)
113 		return (EINVAL);
114 	pagemask = ~(boundary - 1);
115 
116 	/* Default to "lose". */
117 	error = ENOMEM;
118 
119 	/*
120 	 * Block all memory allocation and lock the free list.
121 	 */
122 
123 	s = uvm_lock_fpageq();
124 
125 	/* Are there even any free pages? */
126 	if (uvmexp.free <= (uvmexp.reserve_pagedaemon + uvmexp.reserve_kernel))
127 		goto out;
128 
129 	for (;; try += alignment) {
130 		if (try + size > high) {
131 
132 			/*
133 			 * We've run past the allowable range.
134 			 */
135 
136 			goto out;
137 		}
138 
139 		/*
140 		 * Make sure this is a managed physical page.
141 		 */
142 
143 		if ((psi = vm_physseg_find(atop(try), &idx)) == -1)
144 			continue; /* managed? */
145 		if (vm_physseg_find(atop(try + size), NULL) != psi)
146 			continue; /* end must be in this segment */
147 		tryidx = idx;
148 		end = idx + (size / PAGE_SIZE);
149 		pgs = vm_physmem[psi].pgs;
150 
151 		/*
152 		 * Found a suitable starting page.  See of the range is free.
153 		 */
154 
155 		for (; idx < end; idx++) {
156 			if (VM_PAGE_IS_FREE(&pgs[idx]) == 0) {
157 				break;
158 			}
159 			idxpa = VM_PAGE_TO_PHYS(&pgs[idx]);
160 			if (idx > tryidx) {
161 				lastidxpa = VM_PAGE_TO_PHYS(&pgs[idx - 1]);
162 				if ((lastidxpa + PAGE_SIZE) != idxpa) {
163 
164 					/*
165 					 * Region not contiguous.
166 					 */
167 
168 					break;
169 				}
170 				if (boundary != 0 &&
171 				    ((lastidxpa ^ idxpa) & pagemask) != 0) {
172 
173 					/*
174 					 * Region crosses boundary.
175 					 */
176 
177 					break;
178 				}
179 			}
180 		}
181 		if (idx == end) {
182 			break;
183 		}
184 	}
185 
186 #if PGFL_NQUEUES != 2
187 #error uvm_pglistalloc needs to be updated
188 #endif
189 
190 	/*
191 	 * we have a chunk of memory that conforms to the requested constraints.
192 	 */
193 	idx = tryidx;
194 	while (idx < end) {
195 		pg = &pgs[idx];
196 		free_list = uvm_page_lookup_freelist(pg);
197 		color = VM_PGCOLOR_BUCKET(pg);
198 		pgflidx = (pg->flags & PG_ZERO) ? PGFL_ZEROS : PGFL_UNKNOWN;
199 #ifdef DEBUG
200 		for (tp = TAILQ_FIRST(&uvm.page_free[
201 			free_list].pgfl_buckets[color].pgfl_queues[pgflidx]);
202 		     tp != NULL;
203 		     tp = TAILQ_NEXT(tp, pageq)) {
204 			if (tp == pg)
205 				break;
206 		}
207 		if (tp == NULL)
208 			panic("uvm_pglistalloc: page not on freelist");
209 #endif
210 		TAILQ_REMOVE(&uvm.page_free[free_list].pgfl_buckets[
211 		    color].pgfl_queues[pgflidx], pg, pageq);
212 		uvmexp.free--;
213 		if (pg->flags & PG_ZERO)
214 			uvmexp.zeropages--;
215 		pg->flags = PG_CLEAN;
216 		pg->pqflags = 0;
217 		pg->uobject = NULL;
218 		pg->uanon = NULL;
219 		TAILQ_INSERT_TAIL(rlist, pg, pageq);
220 		idx++;
221 		STAT_INCR(uvm_pglistalloc_npages);
222 	}
223 	error = 0;
224 
225 out:
226 	/*
227 	 * check to see if we need to generate some free pages waking
228 	 * the pagedaemon.
229 	 */
230 
231 	UVM_KICK_PDAEMON();
232 	uvm_unlock_fpageq(s);
233 	return (error);
234 }
235 
236 /*
237  * uvm_pglistfree: free a list of pages
238  *
239  * => pages should already be unmapped
240  */
241 
242 void
243 uvm_pglistfree(list)
244 	struct pglist *list;
245 {
246 	struct vm_page *pg;
247 	int s;
248 
249 	/*
250 	 * Lock the free list and free each page.
251 	 */
252 
253 	s = uvm_lock_fpageq();
254 	while ((pg = TAILQ_FIRST(list)) != NULL) {
255 		KASSERT((pg->pqflags & (PQ_ACTIVE|PQ_INACTIVE)) == 0);
256 		TAILQ_REMOVE(list, pg, pageq);
257 		pg->pqflags = PQ_FREE;
258 		TAILQ_INSERT_TAIL(&uvm.page_free[uvm_page_lookup_freelist(pg)].
259 		    pgfl_buckets[VM_PGCOLOR_BUCKET(pg)].
260 		    pgfl_queues[PGFL_UNKNOWN], pg, pageq);
261 		uvmexp.free++;
262 		if (uvmexp.zeropages < UVM_PAGEZERO_TARGET)
263 			uvm.page_idle_zero = vm_page_zero_enable;
264 		STAT_DECR(uvm_pglistalloc_npages);
265 	}
266 	uvm_unlock_fpageq(s);
267 }
268