]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/vm/vm_pageq.c
This commit was generated by cvs2svn to compensate for changes in r147072,
[FreeBSD/FreeBSD.git] / sys / vm / vm_pageq.c
1 /*-
2  * Copyright (c) 1998 Matthew Dillon.  All Rights Reserved.
3  * Redistribution and use in source and binary forms, with or without
4  * modification, are permitted provided that the following conditions
5  * are met:
6  * 1. Redistributions of source code must retain the above copyright
7  *    notice, this list of conditions and the following disclaimer.
8  * 2. Redistributions in binary form must reproduce the above copyright
9  *    notice, this list of conditions and the following disclaimer in the
10  *    documentation and/or other materials provided with the distribution.
11  * 4. Neither the name of the University nor the names of its contributors
12  *    may be used to endorse or promote products derived from this software
13  *    without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/lock.h>
34 #include <sys/malloc.h>
35 #include <sys/mutex.h>
36 #include <sys/proc.h>
37 #include <sys/vmmeter.h>
38 #include <sys/vnode.h>
39
40 #include <vm/vm.h>
41 #include <vm/vm_param.h>
42 #include <vm/vm_kern.h>
43 #include <vm/vm_object.h>
44 #include <vm/vm_page.h>
45 #include <vm/vm_pageout.h>
46 #include <vm/vm_pager.h>
47 #include <vm/vm_extern.h>
48
49 struct vpgqueues vm_page_queues[PQ_COUNT];
50
51 void
52 vm_pageq_init(void) 
53 {
54         int i;
55
56         for (i = 0; i < PQ_L2_SIZE; i++) {
57                 vm_page_queues[PQ_FREE+i].cnt = &cnt.v_free_count;
58         }
59         for (i = 0; i < PQ_L2_SIZE; i++) {
60                 vm_page_queues[PQ_CACHE+i].cnt = &cnt.v_cache_count;
61         }
62         vm_page_queues[PQ_INACTIVE].cnt = &cnt.v_inactive_count;
63         vm_page_queues[PQ_ACTIVE].cnt = &cnt.v_active_count;
64         vm_page_queues[PQ_HOLD].cnt = &cnt.v_active_count;
65
66         for (i = 0; i < PQ_COUNT; i++) {
67                 TAILQ_INIT(&vm_page_queues[i].pl);
68         }
69 }
70
71 void
72 vm_pageq_requeue(vm_page_t m)
73 {
74         int queue = m->queue;
75         struct vpgqueues *vpq;
76
77         if (queue != PQ_NONE) {
78                 vpq = &vm_page_queues[queue];
79                 TAILQ_REMOVE(&vpq->pl, m, pageq);
80                 TAILQ_INSERT_TAIL(&vpq->pl, m, pageq);
81         }
82 }
83
84 /*
85  *      vm_pageq_enqueue:
86  */
87 void
88 vm_pageq_enqueue(int queue, vm_page_t m)
89 {
90         struct vpgqueues *vpq;
91
92         vpq = &vm_page_queues[queue];
93         m->queue = queue;
94         TAILQ_INSERT_TAIL(&vpq->pl, m, pageq);
95         ++*vpq->cnt;
96         ++vpq->lcnt;
97 }
98
99 /*
100  *      vm_add_new_page:
101  *
102  *      Add a new page to the freelist for use by the system.
103  */
104 vm_page_t
105 vm_pageq_add_new_page(vm_paddr_t pa)
106 {
107         vm_paddr_t bad;
108         vm_page_t m;
109         char *cp, *list, *pos;
110
111         GIANT_REQUIRED;
112
113         /*
114          * See if a physical address in this page has been listed
115          * in the blacklist tunable.  Entries in the tunable are
116          * separated by spaces or commas.  If an invalid integer is
117          * encountered then the rest of the string is skipped.
118          */
119         if (testenv("vm.blacklist")) {
120                 list = getenv("vm.blacklist");
121                 for (pos = list; *pos != '\0'; pos = cp) {
122                         bad = strtoq(pos, &cp, 0);
123                         if (*cp != '\0') {
124                                 if (*cp == ' ' || *cp == ',') {
125                                         cp++;
126                                         if (cp == pos)
127                                                 continue;
128                                 } else
129                                         break;
130                         }
131                         if (pa == trunc_page(bad)) {
132                                 printf("Skipping page with pa 0x%jx\n",
133                                     (uintmax_t)pa);
134                                 freeenv(list);
135                                 return (NULL);
136                         }
137                 }
138                 freeenv(list);
139         }
140
141         ++cnt.v_page_count;
142         m = PHYS_TO_VM_PAGE(pa);
143         m->phys_addr = pa;
144         m->flags = 0;
145         m->pc = (pa >> PAGE_SHIFT) & PQ_L2_MASK;
146         vm_pageq_enqueue(m->pc + PQ_FREE, m);
147         return (m);
148 }
149
150 /*
151  * vm_pageq_remove_nowakeup:
152  *
153  *      vm_page_unqueue() without any wakeup
154  *
155  *      The queue containing the given page must be locked.
156  *      This routine may not block.
157  */
158 void
159 vm_pageq_remove_nowakeup(vm_page_t m)
160 {
161         int queue = m->queue;
162         struct vpgqueues *pq;
163         if (queue != PQ_NONE) {
164                 pq = &vm_page_queues[queue];
165                 m->queue = PQ_NONE;
166                 TAILQ_REMOVE(&pq->pl, m, pageq);
167                 (*pq->cnt)--;
168                 pq->lcnt--;
169         }
170 }
171
172 /*
173  * vm_pageq_remove:
174  *
175  *      Remove a page from its queue.
176  *
177  *      The queue containing the given page must be locked.
178  *      This routine may not block.
179  */
180 void
181 vm_pageq_remove(vm_page_t m)
182 {
183         int queue = m->queue;
184         struct vpgqueues *pq;
185
186         if (queue != PQ_NONE) {
187                 m->queue = PQ_NONE;
188                 pq = &vm_page_queues[queue];
189                 TAILQ_REMOVE(&pq->pl, m, pageq);
190                 (*pq->cnt)--;
191                 pq->lcnt--;
192                 if ((queue - m->pc) == PQ_CACHE) {
193                         if (vm_paging_needed())
194                                 pagedaemon_wakeup();
195                 }
196         }
197 }
198
199 #if PQ_L2_SIZE > 1
200
201 /*
202  *      vm_pageq_find:
203  *
204  *      Find a page on the specified queue with color optimization.
205  *
206  *      The page coloring optimization attempts to locate a page
207  *      that does not overload other nearby pages in the object in
208  *      the cpu's L2 cache.  We need this optimization because cpu
209  *      caches tend to be physical caches, while object spaces tend 
210  *      to be virtual.
211  *
212  *      The specified queue must be locked.
213  *      This routine may not block.
214  *
215  *      This routine may only be called from the vm_pageq_find()
216  *      function in this file.
217  */
218 static __inline vm_page_t
219 _vm_pageq_find(int basequeue, int index)
220 {
221         int i;
222         vm_page_t m = NULL;
223         struct vpgqueues *pq;
224
225         pq = &vm_page_queues[basequeue];
226
227         /*
228          * Note that for the first loop, index+i and index-i wind up at the
229          * same place.  Even though this is not totally optimal, we've already
230          * blown it by missing the cache case so we do not care.
231          */
232         for (i = PQ_L2_SIZE / 2; i > 0; --i) {
233                 if ((m = TAILQ_FIRST(&pq[(index + i) & PQ_L2_MASK].pl)) != NULL)
234                         break;
235
236                 if ((m = TAILQ_FIRST(&pq[(index - i) & PQ_L2_MASK].pl)) != NULL)
237                         break;
238         }
239         return (m);
240 }
241 #endif          /* PQ_L2_SIZE > 1 */
242
243 vm_page_t
244 vm_pageq_find(int basequeue, int index, boolean_t prefer_zero)
245 {
246         vm_page_t m;
247
248 #if PQ_L2_SIZE > 1
249         if (prefer_zero) {
250                 m = TAILQ_LAST(&vm_page_queues[basequeue+index].pl, pglist);
251         } else {
252                 m = TAILQ_FIRST(&vm_page_queues[basequeue+index].pl);
253         }
254         if (m == NULL) {
255                 m = _vm_pageq_find(basequeue, index);
256         }
257 #else
258         if (prefer_zero) {
259                 m = TAILQ_LAST(&vm_page_queues[basequeue].pl, pglist);
260         } else {
261                 m = TAILQ_FIRST(&vm_page_queues[basequeue].pl);
262         }
263 #endif
264         return (m);
265 }
266