1 /*- 2 * Copyright (c) 1982, 1986, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)vmmeter.h 8.2 (Berkeley) 7/10/94 34 * $FreeBSD: src/sys/sys/vmmeter.h,v 1.21.2.2 2002/10/10 19:28:21 dillon Exp $ 35 * $DragonFly: src/sys/vm/vm_page2.h,v 1.3 2008/04/14 20:00:29 dillon Exp $ 36 */ 37 38 #ifndef _VM_VM_PAGE2_H_ 39 #define _VM_VM_PAGE2_H_ 40 41 #ifndef _SYS_VMMETER_H_ 42 #include <sys/vmmeter.h> 43 #endif 44 #ifndef _SYS_QUEUE_H_ 45 #include <sys/queue.h> 46 #endif 47 #ifndef _VM_PAGE_H_ 48 #include <vm/vm_page.h> 49 #endif 50 #ifndef _SYS_SPINLOCK_H_ 51 #include <sys/spinlock.h> 52 #endif 53 #ifndef _SYS_SPINLOCK2_H_ 54 #include <sys/spinlock2.h> 55 #endif 56 57 #ifdef _KERNEL 58 59 /* 60 * Return TRUE if we are under our severe low-free-pages threshold 61 * 62 * This causes user processes to stall to avoid exhausting memory that 63 * the kernel might need. 64 * 65 * reserved < severe < minimum < target < paging_target 66 */ 67 static __inline 68 int 69 vm_page_count_severe(void) 70 { 71 return (vmstats.v_free_severe > 72 vmstats.v_free_count + vmstats.v_cache_count || 73 vmstats.v_free_reserved > vmstats.v_free_count); 74 } 75 76 /* 77 * Return TRUE if we are under our minimum low-free-pages threshold. 78 * This activates the pageout demon. The pageout demon tries to 79 * reach the target but may stop once it satisfies the minimum. 80 * 81 * reserved < severe < minimum < target < paging_target 82 */ 83 static __inline 84 int 85 vm_page_count_min(int donotcount) 86 { 87 return (vmstats.v_free_min + donotcount > 88 (vmstats.v_free_count + vmstats.v_cache_count) || 89 vmstats.v_free_reserved > vmstats.v_free_count); 90 } 91 92 /* 93 * Return TRUE if we are under our free page target. The pageout demon 94 * tries to reach the target but may stop once it gets past the min. 95 * 96 * User threads doing normal allocations might wait based on this 97 * function but MUST NOT wait in a loop based on this function as the 98 * VM load may prevent the target from being reached. 99 */ 100 static __inline 101 int 102 vm_page_count_target(void) 103 { 104 return (vmstats.v_free_target > 105 (vmstats.v_free_count + vmstats.v_cache_count) || 106 vmstats.v_free_reserved > vmstats.v_free_count); 107 } 108 109 /* 110 * Return the number of pages the pageout daemon needs to move into the 111 * cache or free lists. A negative number means we have sufficient free 112 * pages. 113 * 114 * The target free+cache is greater than vm_page_count_target(). The 115 * frontend uses vm_page_count_target() while the backend continue freeing 116 * based on vm_paging_target(). 117 * 118 * This function DOES NOT return TRUE or FALSE. 119 */ 120 static __inline 121 int 122 vm_paging_target(void) 123 { 124 return ( 125 (vmstats.v_free_target + vmstats.v_cache_min) - 126 (vmstats.v_free_count + vmstats.v_cache_count) 127 ); 128 } 129 130 /* 131 * Return TRUE if hysteresis dictates we should nominally wakeup the 132 * pageout daemon to start working on freeing up some memory. This 133 * routine should NOT be used to determine when to block on the VM system. 134 * We want to wakeup the pageout daemon before we might otherwise block. 135 * 136 * Paging begins when cache+free drops below cache_min + free_min. 137 */ 138 static __inline 139 int 140 vm_paging_needed(void) 141 { 142 if (vmstats.v_free_min + vmstats.v_cache_min > 143 vmstats.v_free_count + vmstats.v_cache_count) { 144 return 1; 145 } 146 if (vmstats.v_free_min > vmstats.v_free_count) 147 return 1; 148 return 0; 149 } 150 151 static __inline 152 void 153 vm_page_event(vm_page_t m, vm_page_event_t event) 154 { 155 if (m->flags & PG_ACTIONLIST) 156 vm_page_event_internal(m, event); 157 } 158 159 static __inline 160 void 161 vm_page_init_action(vm_page_t m, vm_page_action_t action, 162 void (*func)(vm_page_t, vm_page_action_t), void *data) 163 { 164 action->m = m; 165 action->func = func; 166 action->data = data; 167 } 168 169 /* 170 * Clear dirty bits in the VM page but truncate the 171 * end to a DEV_BSIZE'd boundary. 172 * 173 * Used when reading data in, typically via getpages. 174 * The partial device block at the end of the truncation 175 * range should not lose its dirty bit. 176 * 177 * NOTE: This function does not clear the pmap modified bit. 178 */ 179 static __inline 180 void 181 vm_page_clear_dirty_end_nonincl(vm_page_t m, int base, int size) 182 { 183 size = (base + size) & ~DEV_BMASK; 184 if (base < size) 185 vm_page_clear_dirty(m, base, size - base); 186 } 187 188 /* 189 * Clear dirty bits in the VM page but truncate the 190 * beginning to a DEV_BSIZE'd boundary. 191 * 192 * Used when truncating a buffer. The partial device 193 * block at the beginning of the truncation range 194 * should not lose its dirty bit. 195 * 196 * NOTE: This function does not clear the pmap modified bit. 197 */ 198 static __inline 199 void 200 vm_page_clear_dirty_beg_nonincl(vm_page_t m, int base, int size) 201 { 202 size += base; 203 base = (base + DEV_BMASK) & ~DEV_BMASK; 204 if (base < size) 205 vm_page_clear_dirty(m, base, size - base); 206 } 207 208 static __inline 209 void 210 vm_page_spin_lock(vm_page_t m) 211 { 212 spin_pool_lock(m); 213 } 214 215 static __inline 216 void 217 vm_page_spin_unlock(vm_page_t m) 218 { 219 spin_pool_unlock(m); 220 } 221 222 /* 223 * Wire a vm_page that is already wired. Does not require a busied 224 * page. 225 */ 226 static __inline 227 void 228 vm_page_wire_quick(vm_page_t m) 229 { 230 if (atomic_fetchadd_int(&m->wire_count, 1) == 0) 231 panic("vm_page_wire_quick: wire_count was 0"); 232 } 233 234 /* 235 * Unwire a vm_page quickly, does not require a busied page. 236 * 237 * This routine refuses to drop the wire_count to 0 and will return 238 * TRUE if it would have had to (instead of decrementing it to 0). 239 * The caller can then busy the page and deal with it. 240 */ 241 static __inline 242 int 243 vm_page_unwire_quick(vm_page_t m) 244 { 245 KKASSERT(m->wire_count > 0); 246 for (;;) { 247 u_int wire_count = m->wire_count; 248 249 cpu_ccfence(); 250 if (wire_count == 1) 251 return TRUE; 252 if (atomic_cmpset_int(&m->wire_count, wire_count, wire_count - 1)) 253 return FALSE; 254 } 255 } 256 257 /* 258 * Functions implemented as macros 259 */ 260 261 static __inline void 262 vm_page_flag_set(vm_page_t m, unsigned int bits) 263 { 264 atomic_set_int(&(m)->flags, bits); 265 } 266 267 static __inline void 268 vm_page_flag_clear(vm_page_t m, unsigned int bits) 269 { 270 atomic_clear_int(&(m)->flags, bits); 271 } 272 273 /* 274 * Wakeup anyone waiting for the page after potentially unbusying 275 * (hard or soft) or doing other work on a page that might make a 276 * waiter ready. The setting of PG_WANTED is integrated into the 277 * related flags and it can't be set once the flags are already 278 * clear, so there should be no races here. 279 */ 280 281 static __inline void 282 vm_page_flash(vm_page_t m) 283 { 284 if (m->flags & PG_WANTED) { 285 vm_page_flag_clear(m, PG_WANTED); 286 wakeup(m); 287 } 288 } 289 290 /* 291 * Reduce the protection of a page. This routine never raises the 292 * protection and therefore can be safely called if the page is already 293 * at VM_PROT_NONE (it will be a NOP effectively ). 294 * 295 * VM_PROT_NONE will remove all user mappings of a page. This is often 296 * necessary when a page changes state (for example, turns into a copy-on-write 297 * page or needs to be frozen for write I/O) in order to force a fault, or 298 * to force a page's dirty bits to be synchronized and avoid hardware 299 * (modified/accessed) bit update races with pmap changes. 300 * 301 * Since 'prot' is usually a constant, this inline usually winds up optimizing 302 * out the primary conditional. 303 * 304 * WARNING: VM_PROT_NONE can block, but will loop until all mappings have 305 * been cleared. Callers should be aware that other page related elements 306 * might have changed, however. 307 */ 308 static __inline void 309 vm_page_protect(vm_page_t m, int prot) 310 { 311 KKASSERT(m->flags & PG_BUSY); 312 if (prot == VM_PROT_NONE) { 313 if (m->flags & (PG_WRITEABLE|PG_MAPPED)) { 314 pmap_page_protect(m, VM_PROT_NONE); 315 /* PG_WRITEABLE & PG_MAPPED cleared by call */ 316 } 317 } else if ((prot == VM_PROT_READ) && (m->flags & PG_WRITEABLE)) { 318 pmap_page_protect(m, VM_PROT_READ); 319 /* PG_WRITEABLE cleared by call */ 320 } 321 } 322 323 /* 324 * Zero-fill the specified page. The entire contents of the page will be 325 * zero'd out. 326 */ 327 static __inline boolean_t 328 vm_page_zero_fill(vm_page_t m) 329 { 330 pmap_zero_page(VM_PAGE_TO_PHYS(m)); 331 return (TRUE); 332 } 333 334 /* 335 * Copy the contents of src_m to dest_m. The pages must be stable but spl 336 * and other protections depend on context. 337 */ 338 static __inline void 339 vm_page_copy(vm_page_t src_m, vm_page_t dest_m) 340 { 341 pmap_copy_page(VM_PAGE_TO_PHYS(src_m), VM_PAGE_TO_PHYS(dest_m)); 342 dest_m->valid = VM_PAGE_BITS_ALL; 343 dest_m->dirty = VM_PAGE_BITS_ALL; 344 } 345 346 /* 347 * Free a page. The page must be marked BUSY. 348 * 349 * Always clear PG_ZERO when freeing a page, which ensures the flag is not 350 * set unless we are absolutely certain the page is zerod. This is 351 * particularly important when the vm_page_alloc*() code moves pages from 352 * PQ_CACHE to PQ_FREE. 353 */ 354 static __inline void 355 vm_page_free(vm_page_t m) 356 { 357 vm_page_flag_clear(m, PG_ZERO); 358 vm_page_free_toq(m); 359 } 360 361 /* 362 * Free a page to the zerod-pages queue. The caller must ensure that the 363 * page has been zerod. 364 */ 365 static __inline void 366 vm_page_free_zero(vm_page_t m) 367 { 368 #ifdef PMAP_DEBUG 369 #ifdef PHYS_TO_DMAP 370 char *p = (char *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m)); 371 int i; 372 373 for (i = 0; i < PAGE_SIZE; i++) { 374 if (p[i] != 0) { 375 panic("non-zero page in vm_page_free_zero()"); 376 } 377 } 378 #endif 379 #endif 380 vm_page_flag_set(m, PG_ZERO); 381 vm_page_free_toq(m); 382 } 383 384 /* 385 * Set page to not be dirty. Note: does not clear pmap modify bits . 386 */ 387 static __inline void 388 vm_page_undirty(vm_page_t m) 389 { 390 m->dirty = 0; 391 } 392 393 #endif /* _KERNEL */ 394 #endif /* _VM_VM_PAGE2_H_ */ 395 396