uvm_page.h revision 1.100 1 /* $NetBSD: uvm_page.h,v 1.100 2020/03/14 20:23:51 ad Exp $ */
2
3 /*
4 * Copyright (c) 1997 Charles D. Cranor and Washington University.
5 * Copyright (c) 1991, 1993, The Regents of the University of California.
6 *
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to Berkeley by
10 * The Mach Operating System project at Carnegie-Mellon University.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)vm_page.h 7.3 (Berkeley) 4/21/91
37 * from: Id: uvm_page.h,v 1.1.2.6 1998/02/04 02:31:42 chuck Exp
38 *
39 *
40 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
41 * All rights reserved.
42 *
43 * Permission to use, copy, modify and distribute this software and
44 * its documentation is hereby granted, provided that both the copyright
45 * notice and this permission notice appear in all copies of the
46 * software, derivative works or modified versions, and any portions
47 * thereof, and that both notices appear in supporting documentation.
48 *
49 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
50 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
51 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
52 *
53 * Carnegie Mellon requests users of this software to return to
54 *
55 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
56 * School of Computer Science
57 * Carnegie Mellon University
58 * Pittsburgh PA 15213-3890
59 *
60 * any improvements or extensions that they make and grant Carnegie the
61 * rights to redistribute these changes.
62 */
63
64 #ifndef _UVM_UVM_PAGE_H_
65 #define _UVM_UVM_PAGE_H_
66
67 #ifdef _KERNEL_OPT
68 #include "opt_uvm_page_trkown.h"
69 #endif
70
71 #include <uvm/uvm_extern.h>
72 #include <uvm/uvm_pglist.h>
73
74 /*
75 * Management of resident (logical) pages.
76 *
77 * Each resident page has a vm_page structure, indexed by page number.
78 * There are several lists in the structure:
79 *
80 * - A red-black tree rooted with the containing object is used to
81 * quickly perform object+offset lookups.
82 * - A list of all pages for a given object, for a quick deactivation
83 * at a time of deallocation.
84 * - An ordered list of pages due for pageout.
85 *
86 * In addition, the structure contains the object and offset to which
87 * this page belongs (for pageout) and sundry status bits.
88 *
89 * Note that the page structure has no lock of its own. The page is
90 * generally protected by its owner's lock (UVM object or amap/anon).
91 * It should be noted that UVM has to serialize pmap(9) operations on
92 * the managed pages, e.g. for pmap_enter() calls. Hence, the lock
93 * order is as follows:
94 *
95 * [vmpage-owner-lock] ->
96 * any pmap locks (e.g. PV hash lock)
97 *
98 * Since the kernel is always self-consistent, no serialization is
99 * required for unmanaged mappings, e.g. for pmap_kenter_pa() calls.
100 *
101 * Field markings and the corresponding locks:
102 *
103 * f: free page queue lock, uvm_fpageqlock
104 * o: page owner (uvm_object::vmobjlock, vm_amap::am_lock, vm_anon::an_lock)
105 * i: vm_page::interlock
106 * => flags set and cleared only with o&i held can
107 * safely be tested for with only o held.
108 * o,i: o|i for read, o&i for write (depends on context - if could be loaned)
109 * => see uvm_loan.c
110 * w: wired page queue or uvm_pglistalloc:
111 * => wired page queue: o&i to change, stable from wire to unwire
112 * XXX What about concurrent or nested wire?
113 * => uvm_pglistalloc: owned by caller
114 * ?: locked by pmap or assumed page owner's lock
115 * p: locked by pagedaemon policy module (pdpolicy)
116 * c: cpu private
117 * s: stable, does not change
118 *
119 * UVM and pmap(9) may use uvm_page_owner_locked_p() to assert whether the
120 * page owner's lock is acquired.
121 *
122 * A page can have one of four identities:
123 *
124 * o free
125 * => pageq.list is entry on global free page queue
126 * => uanon is unused (or (void *)0xdeadbeef for DEBUG)
127 * => uobject is unused (or (void *)0xdeadbeef for DEBUG)
128 * => PG_FREE is set in flags
129 * o owned by a uvm_object
130 * => pageq.queue is entry on wired page queue, if any
131 * => uanon is NULL or the vm_anon to which it has been O->A loaned
132 * => uobject is owner
133 * o owned by a vm_anon
134 * => pageq is unused (XXX correct?)
135 * => uanon is owner
136 * => uobject is NULL
137 * => PG_ANON is set in flags
138 * o allocated by uvm_pglistalloc
139 * => pageq.queue is entry on resulting pglist, owned by caller
140 * => uanon is unused
141 * => uobject is unused
142 *
143 * The following transitions are allowed:
144 *
145 * - uvm_pagealloc: free -> owned by a uvm_object/vm_anon
146 * - uvm_pagefree: owned by a uvm_object/vm_anon -> free
147 * - uvm_pglistalloc: free -> allocated by uvm_pglistalloc
148 * - uvm_pglistfree: allocated by uvm_pglistalloc -> free
149 *
150 * On the ordering of fields:
151 *
152 * The fields most heavily used by the page allocator and uvmpdpol are
153 * clustered together at the start of the structure, so that while under
154 * global lock it's more likely that only one cache line for each page need
155 * be touched.
156 */
157
158 struct vm_page {
159 union {
160 TAILQ_ENTRY(vm_page) queue; /* w: wired page queue
161 * or uvm_pglistalloc output */
162 LIST_ENTRY(vm_page) list; /* f: global free page queue */
163 } pageq;
164 TAILQ_ENTRY(vm_page) pdqueue; /* p: pagedaemon queue */
165 kmutex_t interlock; /* s: lock on identity */
166 uint32_t pqflags; /* i: pagedaemon flags */
167 uint32_t flags; /* o: object flags */
168 paddr_t phys_addr; /* o: physical address of pg */
169 uint32_t loan_count; /* o,i: num. active loans */
170 uint32_t wire_count; /* o,i: wired down map refs */
171 struct vm_anon *uanon; /* o,i: anon */
172 struct uvm_object *uobject; /* o,i: object */
173 voff_t offset; /* o: offset into object */
174
175 #ifdef __HAVE_VM_PAGE_MD
176 struct vm_page_md mdpage; /* ?: pmap-specific data */
177 #endif
178
179 #if defined(UVM_PAGE_TRKOWN)
180 /* debugging fields to track page ownership */
181 pid_t owner; /* proc that set PG_BUSY */
182 lwpid_t lowner; /* lwp that set PG_BUSY */
183 const char *owner_tag; /* why it was set busy */
184 #endif
185 };
186
187 /*
188 * Overview of UVM page flags, stored in pg->flags.
189 *
190 * Locking notes:
191 *
192 * PG_, struct vm_page::flags => locked by owner
193 * PG_AOBJ => additionally locked by vm_page::interlock
194 * PG_ANON => additionally locked by vm_page::interlock
195 * PG_FREE => additionally locked by uvm_fpageqlock
196 * for uvm_pglistalloc()
197 *
198 * Flag descriptions:
199 *
200 * PG_CLEAN:
201 * Page is known clean.
202 * The contents of the page is consistent with its backing store.
203 *
204 * PG_DIRTY:
205 * Page is known dirty.
206 * To avoid losing data, the contents of the page should be written
207 * back to the backing store before freeing the page.
208 *
209 * PG_BUSY:
210 * Page is long-term locked, usually because of I/O (transfer from the
211 * page memory to the backing store) is in progress. LWP attempting
212 * to access the page shall set PQ_WANTED and wait. PG_BUSY may only
213 * be set with a write lock held on the object.
214 *
215 * PG_PAGEOUT:
216 * Indicates that the page is being paged-out in preparation for
217 * being freed.
218 *
219 * PG_RELEASED:
220 * Indicates that the page, which is currently PG_BUSY, should be freed
221 * after the release of long-term lock. It is responsibility of the
222 * owning LWP (i.e. which set PG_BUSY) to do it.
223 *
224 * PG_FAKE:
225 * Page has been allocated, but not yet initialised. The flag is used
226 * to avoid overwriting of valid data, e.g. to prevent read from the
227 * backing store when in-core data is newer.
228 *
229 * PG_RDONLY:
230 * Indicates that the page must be mapped read-only.
231 *
232 * PG_ZERO:
233 * Indicates that the page has been pre-zeroed. This flag is only
234 * set when the page is not in the queues and is cleared when the
235 * page is placed on the free list.
236 *
237 * PG_MARKER:
238 * Dummy marker page, generally used for list traversal.
239 */
240
241 /*
242 * if you want to renumber PG_CLEAN and PG_DIRTY, check __CTASSERTs in
243 * uvm_page_status.c first.
244 */
245
246 #define PG_CLEAN 0x00000001 /* page is known clean */
247 #define PG_DIRTY 0x00000002 /* page is known dirty */
248 #define PG_BUSY 0x00000004 /* page is locked */
249 #define PG_PAGEOUT 0x00000010 /* page to be freed for pagedaemon */
250 #define PG_RELEASED 0x00000020 /* page to be freed when unbusied */
251 #define PG_FAKE 0x00000040 /* page is not yet initialized */
252 #define PG_RDONLY 0x00000080 /* page must be mapped read-only */
253 #define PG_ZERO 0x00000100 /* page is pre-zero'd */
254 #define PG_TABLED 0x00000200 /* page is tabled in object */
255 #define PG_AOBJ 0x00000400 /* page is part of an anonymous
256 uvm_object */
257 #define PG_ANON 0x00000800 /* page is part of an anon, rather
258 than an uvm_object */
259 #define PG_FILE 0x00001000 /* file backed (non-anonymous) */
260 #define PG_READAHEAD 0x00002000 /* read-ahead but not "hit" yet */
261 #define PG_FREE 0x00004000 /* page is on free list */
262 #define PG_MARKER 0x00008000 /* dummy marker page */
263 #define PG_PAGER1 0x00010000 /* pager-specific flag */
264
265 #define PG_STAT (PG_ANON|PG_AOBJ|PG_FILE)
266 #define PG_SWAPBACKED (PG_ANON|PG_AOBJ)
267
268 #define UVM_PGFLAGBITS \
269 "\20\1CLEAN\2DIRTY\3BUSY" \
270 "\5PAGEOUT\6RELEASED\7FAKE\10RDONLY" \
271 "\11ZERO\12TABLED\13AOBJ\14ANON" \
272 "\15FILE\16READAHEAD\17FREE\20MARKER" \
273 "\21PAGER1"
274
275 /*
276 * Flags stored in pg->pqflags, which is protected by pg->interlock.
277 *
278 * PQ_PRIVATE:
279 * ... is for uvmpdpol to do whatever it wants with.
280 *
281 * PQ_INTENT_SET:
282 * Indicates that the intent set on the page has not yet been realized.
283 *
284 * PQ_INTENT_QUEUED:
285 * Indicates that the page is, or will soon be, on a per-CPU queue for
286 * the intent to be realized.
287 *
288 * PQ_WANTED:
289 * Indicates that the page, which is currently PG_BUSY, is wanted by
290 * some other LWP. The page owner (i.e. LWP which set PG_BUSY) is
291 * responsible to clear both flags and wake up any waiters once it has
292 * released the long-term lock (PG_BUSY).
293 */
294
295 #define PQ_INTENT_A 0x00000000 /* intend activation */
296 #define PQ_INTENT_I 0x00000001 /* intend deactivation */
297 #define PQ_INTENT_E 0x00000002 /* intend enqueue */
298 #define PQ_INTENT_D 0x00000003 /* intend dequeue */
299 #define PQ_INTENT_MASK 0x00000003 /* mask of intended state */
300 #define PQ_INTENT_SET 0x00000004 /* not realized yet */
301 #define PQ_INTENT_QUEUED 0x00000008 /* queued for processing */
302 #define PQ_PRIVATE 0x00000ff0 /* private for pdpolicy */
303 #define PQ_WANTED 0x00001000 /* someone is waiting for page */
304
305 #define UVM_PQFLAGBITS \
306 "\20\1INTENT_0\2INTENT_1\3INTENT_SET\4INTENT_QUEUED" \
307 "\5PRIVATE1\6PRIVATE2\7PRIVATE3\10PRIVATE4" \
308 "\11PRIVATE5\12PRIVATE6\13PRIVATE7\14PRIVATE8" \
309 "\15WANTED"
310
311 /*
312 * physical memory layout structure
313 *
314 * MD vmparam.h must #define:
315 * VM_PHYSEG_MAX = max number of physical memory segments we support
316 * (if this is "1" then we revert to a "contig" case)
317 * VM_PHYSSEG_STRAT: memory sort/search options (for VM_PHYSEG_MAX > 1)
318 * - VM_PSTRAT_RANDOM: linear search (random order)
319 * - VM_PSTRAT_BSEARCH: binary search (sorted by address)
320 * - VM_PSTRAT_BIGFIRST: linear search (sorted by largest segment first)
321 * - others?
322 * XXXCDC: eventually we should purge all left-over global variables...
323 */
324 #define VM_PSTRAT_RANDOM 1
325 #define VM_PSTRAT_BSEARCH 2
326 #define VM_PSTRAT_BIGFIRST 3
327
328 #ifdef _KERNEL
329
330 /*
331 * globals
332 */
333
334 extern bool vm_page_zero_enable;
335
336 /*
337 * prototypes: the following prototypes define the interface to pages
338 */
339
340 void uvm_page_init(vaddr_t *, vaddr_t *);
341 #if defined(UVM_PAGE_TRKOWN)
342 void uvm_page_own(struct vm_page *, const char *);
343 #endif
344 #if !defined(PMAP_STEAL_MEMORY)
345 bool uvm_page_physget(paddr_t *);
346 #endif
347 void uvm_page_recolor(int);
348 void uvm_page_rebucket(void);
349 void uvm_pageidlezero(void);
350
351 void uvm_pageactivate(struct vm_page *);
352 vaddr_t uvm_pageboot_alloc(vsize_t);
353 void uvm_pagecopy(struct vm_page *, struct vm_page *);
354 void uvm_pagedeactivate(struct vm_page *);
355 void uvm_pagedequeue(struct vm_page *);
356 void uvm_pageenqueue(struct vm_page *);
357 void uvm_pagefree(struct vm_page *);
358 void uvm_pagelock(struct vm_page *);
359 void uvm_pagelock2(struct vm_page *, struct vm_page *);
360 void uvm_pageunlock(struct vm_page *);
361 void uvm_pageunlock2(struct vm_page *, struct vm_page *);
362 void uvm_page_unbusy(struct vm_page **, int);
363 struct vm_page *uvm_pagelookup(struct uvm_object *, voff_t);
364 void uvm_pageunwire(struct vm_page *);
365 void uvm_pagewire(struct vm_page *);
366 void uvm_pagezero(struct vm_page *);
367 bool uvm_pageismanaged(paddr_t);
368 bool uvm_page_owner_locked_p(struct vm_page *, bool);
369 void uvm_pgfl_lock(void);
370 void uvm_pgfl_unlock(void);
371 unsigned int uvm_pagegetdirty(struct vm_page *);
372 void uvm_pagemarkdirty(struct vm_page *, unsigned int);
373 bool uvm_pagecheckdirty(struct vm_page *, bool);
374 bool uvm_pagereadonly_p(struct vm_page *);
375 bool uvm_page_locked_p(struct vm_page *);
376 void uvm_pageunbusy(struct vm_page *);
377 void uvm_pagewait(struct vm_page *, krwlock_t *, const char *);
378
379 int uvm_page_lookup_freelist(struct vm_page *);
380
381 struct vm_page *uvm_phys_to_vm_page(paddr_t);
382 paddr_t uvm_vm_page_to_phys(const struct vm_page *);
383
384 #if defined(PMAP_DIRECT)
385 extern bool ubc_direct;
386 int uvm_direct_process(struct vm_page **, u_int, voff_t, vsize_t,
387 int (*)(void *, size_t, void *), void *);
388 #endif
389
390 /*
391 * page dirtiness status for uvm_pagegetdirty and uvm_pagemarkdirty
392 *
393 * UNKNOWN means that we need to consult pmap to know if the page is
394 * dirty or not.
395 * basically, UVM_PAGE_STATUS_CLEAN implies that the page has no writable
396 * mapping.
397 *
398 * if you want to renumber these, check __CTASSERTs in
399 * uvm_page_status.c first.
400 */
401
402 #define UVM_PAGE_STATUS_UNKNOWN 0
403 #define UVM_PAGE_STATUS_CLEAN 1
404 #define UVM_PAGE_STATUS_DIRTY 2
405 #define UVM_PAGE_NUM_STATUS 3
406
407 /*
408 * macros
409 */
410
411 #define VM_PAGE_TO_PHYS(entry) uvm_vm_page_to_phys(entry)
412
413 #ifdef __HAVE_VM_PAGE_MD
414 #define VM_PAGE_TO_MD(pg) (&(pg)->mdpage)
415 #endif
416
417 /*
418 * Compute the page color for a given page.
419 */
420 #define VM_PGCOLOR(pg) \
421 (atop(VM_PAGE_TO_PHYS((pg))) & uvmexp.colormask)
422 #define PHYS_TO_VM_PAGE(pa) uvm_phys_to_vm_page(pa)
423
424 /*
425 * VM_PAGE_IS_FREE() can't tell if the page is on global free list, or a
426 * per-CPU cache. If you need to be certain, pause caching.
427 */
428 #define VM_PAGE_IS_FREE(entry) ((entry)->flags & PG_FREE)
429
430 /*
431 * Use the lower 10 bits of pg->phys_addr to cache some some locators for
432 * the page. This implies that the smallest possible page size is 1kB, and
433 * that nobody should use pg->phys_addr directly (use VM_PAGE_TO_PHYS()).
434 *
435 * - 5 bits for the freelist index, because uvm_page_lookup_freelist()
436 * traverses an rbtree and therefore features prominently in traces
437 * captured during performance test. It would probably be more useful to
438 * cache physseg index here because freelist can be inferred from physseg,
439 * but it requires changes to allocation for UVM_HOTPLUG, so for now we'll
440 * go with freelist.
441 *
442 * - 5 bits for "bucket", a way for us to categorise pages further as
443 * needed (e.g. NUMA node).
444 *
445 * None of this is set in stone; it can be adjusted as needed.
446 */
447
448 #define UVM_PHYSADDR_FREELIST __BITS(0,4)
449 #define UVM_PHYSADDR_BUCKET __BITS(5,9)
450
451 static inline unsigned
452 uvm_page_get_freelist(struct vm_page *pg)
453 {
454 unsigned fl = __SHIFTOUT(pg->phys_addr, UVM_PHYSADDR_FREELIST);
455 KASSERT(fl == (unsigned)uvm_page_lookup_freelist(pg));
456 return fl;
457 }
458
459 static inline unsigned
460 uvm_page_get_bucket(struct vm_page *pg)
461 {
462 return __SHIFTOUT(pg->phys_addr, UVM_PHYSADDR_BUCKET);
463 }
464
465 static inline void
466 uvm_page_set_freelist(struct vm_page *pg, unsigned fl)
467 {
468 KASSERT(fl < 32);
469 pg->phys_addr &= ~UVM_PHYSADDR_FREELIST;
470 pg->phys_addr |= __SHIFTIN(fl, UVM_PHYSADDR_FREELIST);
471 }
472
473 static inline void
474 uvm_page_set_bucket(struct vm_page *pg, unsigned b)
475 {
476 KASSERT(b < 32);
477 pg->phys_addr &= ~UVM_PHYSADDR_BUCKET;
478 pg->phys_addr |= __SHIFTIN(b, UVM_PHYSADDR_BUCKET);
479 }
480
481 #ifdef DEBUG
482 void uvm_pagezerocheck(struct vm_page *);
483 #endif /* DEBUG */
484
485 #endif /* _KERNEL */
486
487 #endif /* _UVM_UVM_PAGE_H_ */
488