1 /* 2 * Copyright (c) 2003, 2004 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Hiten Pandya <hmp@backplane.com>. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 3. Neither the name of The DragonFly Project nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific, prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 */ 35 /* 36 * Copyright (c) 1991 Regents of the University of California. 37 * All rights reserved. 38 * 39 * This code is derived from software contributed to Berkeley by 40 * The Mach Operating System project at Carnegie-Mellon University. 41 * 42 * Redistribution and use in source and binary forms, with or without 43 * modification, are permitted provided that the following conditions 44 * are met: 45 * 1. Redistributions of source code must retain the above copyright 46 * notice, this list of conditions and the following disclaimer. 47 * 2. Redistributions in binary form must reproduce the above copyright 48 * notice, this list of conditions and the following disclaimer in the 49 * documentation and/or other materials provided with the distribution. 50 * 3. Neither the name of the University nor the names of its contributors 51 * may be used to endorse or promote products derived from this software 52 * without specific prior written permission. 53 * 54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 64 * SUCH DAMAGE. 65 * 66 * from: @(#)vm_page.c 7.4 (Berkeley) 5/7/91 67 * $DragonFly: src/sys/vm/vm_contig.c,v 1.21 2006/12/28 21:24:02 dillon Exp $ 68 */ 69 70 /* 71 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 72 * All rights reserved. 73 * 74 * Authors: Avadis Tevanian, Jr., Michael Wayne Young 75 * 76 * Permission to use, copy, modify and distribute this software and 77 * its documentation is hereby granted, provided that both the copyright 78 * notice and this permission notice appear in all copies of the 79 * software, derivative works or modified versions, and any portions 80 * thereof, and that both notices appear in supporting documentation. 81 * 82 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 83 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 84 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 85 * 86 * Carnegie Mellon requests users of this software to return to 87 * 88 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 89 * School of Computer Science 90 * Carnegie Mellon University 91 * Pittsburgh PA 15213-3890 92 * 93 * any improvements or extensions that they make and grant Carnegie the 94 * rights to redistribute these changes. 95 */ 96 97 /* 98 * Contiguous memory allocation API. 99 */ 100 101 #include <sys/param.h> 102 #include <sys/systm.h> 103 #include <sys/malloc.h> 104 #include <sys/proc.h> 105 #include <sys/lock.h> 106 #include <sys/vmmeter.h> 107 #include <sys/vnode.h> 108 109 #include <vm/vm.h> 110 #include <vm/vm_param.h> 111 #include <vm/vm_kern.h> 112 #include <vm/pmap.h> 113 #include <vm/vm_map.h> 114 #include <vm/vm_object.h> 115 #include <vm/vm_page.h> 116 #include <vm/vm_pageout.h> 117 #include <vm/vm_pager.h> 118 #include <vm/vm_extern.h> 119 120 #include <sys/thread2.h> 121 #include <sys/spinlock2.h> 122 #include <vm/vm_page2.h> 123 124 static void vm_contig_pg_free(int start, u_long size); 125 126 /* 127 * vm_contig_pg_clean: 128 * 129 * Do a thorough cleanup of the specified 'queue', which can be either 130 * PQ_ACTIVE or PQ_INACTIVE by doing a walkthrough. If the page is not 131 * marked dirty, it is shoved into the page cache, provided no one has 132 * currently aqcuired it, otherwise localized action per object type 133 * is taken for cleanup: 134 * 135 * In the OBJT_VNODE case, the whole page range is cleaned up 136 * using the vm_object_page_clean() routine, by specyfing a 137 * start and end of '0'. 138 * 139 * Otherwise if the object is of any other type, the generic 140 * pageout (daemon) flush routine is invoked. 141 */ 142 static void 143 vm_contig_pg_clean(int queue, int count) 144 { 145 vm_object_t object; 146 vm_page_t m, m_tmp; 147 struct vm_page marker; 148 struct vpgqueues *pq = &vm_page_queues[queue]; 149 150 /* 151 * Setup a local marker 152 */ 153 bzero(&marker, sizeof(marker)); 154 marker.flags = PG_FICTITIOUS | PG_MARKER; 155 marker.busy_count = PBUSY_LOCKED; 156 marker.queue = queue; 157 marker.wire_count = 1; 158 159 vm_page_queues_spin_lock(queue); 160 TAILQ_INSERT_HEAD(&pq->pl, &marker, pageq); 161 vm_page_queues_spin_unlock(queue); 162 163 /* 164 * Iterate the queue. Note that the vm_page spinlock must be 165 * acquired before the pageq spinlock so it's easiest to simply 166 * not hold it in the loop iteration. 167 */ 168 while (count-- > 0 && (m = TAILQ_NEXT(&marker, pageq)) != NULL) { 169 vm_page_and_queue_spin_lock(m); 170 if (m != TAILQ_NEXT(&marker, pageq)) { 171 vm_page_and_queue_spin_unlock(m); 172 ++count; 173 continue; 174 } 175 KKASSERT(m->queue == queue); 176 177 TAILQ_REMOVE(&pq->pl, &marker, pageq); 178 TAILQ_INSERT_AFTER(&pq->pl, m, &marker, pageq); 179 180 if (m->flags & PG_MARKER) { 181 vm_page_and_queue_spin_unlock(m); 182 continue; 183 } 184 if (vm_page_busy_try(m, TRUE)) { 185 vm_page_and_queue_spin_unlock(m); 186 continue; 187 } 188 vm_page_and_queue_spin_unlock(m); 189 190 /* 191 * We've successfully busied the page 192 */ 193 if (m->queue - m->pc != queue) { 194 vm_page_wakeup(m); 195 continue; 196 } 197 if (m->wire_count || m->hold_count) { 198 vm_page_wakeup(m); 199 continue; 200 } 201 if ((object = m->object) == NULL) { 202 vm_page_wakeup(m); 203 continue; 204 } 205 vm_page_test_dirty(m); 206 if (m->dirty || (m->flags & PG_NEED_COMMIT)) { 207 vm_object_hold(object); 208 KKASSERT(m->object == object); 209 210 if (object->type == OBJT_VNODE) { 211 vm_page_wakeup(m); 212 vn_lock(object->handle, LK_EXCLUSIVE|LK_RETRY); 213 vm_object_page_clean(object, 0, 0, OBJPC_SYNC); 214 vn_unlock(((struct vnode *)object->handle)); 215 } else if (object->type == OBJT_SWAP || 216 object->type == OBJT_DEFAULT) { 217 m_tmp = m; 218 vm_pageout_flush(&m_tmp, 1, 0); 219 } else { 220 vm_page_wakeup(m); 221 } 222 vm_object_drop(object); 223 } else if (m->hold_count == 0) { 224 vm_page_cache(m); 225 } else { 226 vm_page_wakeup(m); 227 } 228 } 229 230 /* 231 * Scrap our local marker 232 */ 233 vm_page_queues_spin_lock(queue); 234 TAILQ_REMOVE(&pq->pl, &marker, pageq); 235 vm_page_queues_spin_unlock(queue); 236 } 237 238 /* 239 * vm_contig_pg_alloc: 240 * 241 * Allocate contiguous pages from the VM. This function does not 242 * map the allocated pages into the kernel map, otherwise it is 243 * impossible to make large allocations (i.e. >2G). 244 * 245 * Malloc()'s data structures have been used for collection of 246 * statistics and for allocations of less than a page. 247 */ 248 static int 249 vm_contig_pg_alloc(unsigned long size, vm_paddr_t low, vm_paddr_t high, 250 unsigned long alignment, unsigned long boundary, int mflags) 251 { 252 int i, q, start, pass; 253 vm_offset_t phys; 254 vm_page_t pga = vm_page_array; 255 vm_page_t m; 256 int pqtype; 257 258 size = round_page(size); 259 if (size == 0) 260 panic("vm_contig_pg_alloc: size must not be 0"); 261 if ((alignment & (alignment - 1)) != 0) 262 panic("vm_contig_pg_alloc: alignment must be a power of 2"); 263 if ((boundary & (boundary - 1)) != 0) 264 panic("vm_contig_pg_alloc: boundary must be a power of 2"); 265 266 /* 267 * See if we can get the pages from the contiguous page reserve 268 * alist. The returned pages will be allocated and wired but not 269 * busied. 270 */ 271 m = vm_page_alloc_contig( 272 low, high, alignment, boundary, size, VM_MEMATTR_DEFAULT); 273 if (m) 274 return (m - &pga[0]); 275 276 /* 277 * Three passes (0, 1, 2). Each pass scans the VM page list for 278 * free or cached pages. After each pass if the entire scan failed 279 * we attempt to flush inactive pages and reset the start index back 280 * to 0. For passes 1 and 2 we also attempt to flush active pages. 281 */ 282 start = 0; 283 for (pass = 0; pass < 3; pass++) { 284 /* 285 * Find first page in array that is free, within range, 286 * aligned, and such that the boundary won't be crossed. 287 */ 288 again: 289 for (i = start; i < vmstats.v_page_count; i++) { 290 m = &pga[i]; 291 phys = VM_PAGE_TO_PHYS(m); 292 pqtype = m->queue - m->pc; 293 if (((pqtype == PQ_FREE) || (pqtype == PQ_CACHE)) && 294 (phys >= low) && (phys < high) && 295 ((phys & (alignment - 1)) == 0) && 296 (((phys ^ (phys + size - 1)) & ~(boundary - 1)) == 0) && 297 m->wire_count == 0 && m->hold_count == 0 && 298 (m->busy_count & 299 (PBUSY_LOCKED | PBUSY_MASK)) == 0 && 300 (m->flags & PG_NEED_COMMIT) == 0) 301 { 302 break; 303 } 304 } 305 306 /* 307 * If we cannot find the page in the given range, or we have 308 * crossed the boundary, call the vm_contig_pg_clean() function 309 * for flushing out the queues, and returning it back to 310 * normal state. 311 */ 312 if ((i == vmstats.v_page_count) || 313 ((VM_PAGE_TO_PHYS(&pga[i]) + size) > high)) { 314 315 /* 316 * Best effort flush of all inactive pages. 317 * This is quite quick, for now stall all 318 * callers, even if they've specified M_NOWAIT. 319 */ 320 for (q = 0; q < PQ_L2_SIZE; ++q) { 321 vm_contig_pg_clean(PQ_INACTIVE + q, 322 vmstats.v_inactive_count); 323 lwkt_yield(); 324 } 325 326 /* 327 * Best effort flush of active pages. 328 * 329 * This is very, very slow. 330 * Only do this if the caller has agreed to M_WAITOK. 331 * 332 * If enough pages are flushed, we may succeed on 333 * next (final) pass, if not the caller, contigmalloc(), 334 * will fail in the index < 0 case. 335 */ 336 if (pass > 0 && (mflags & M_WAITOK)) { 337 for (q = 0; q < PQ_L2_SIZE; ++q) { 338 vm_contig_pg_clean(PQ_ACTIVE + q, 339 vmstats.v_active_count); 340 } 341 lwkt_yield(); 342 } 343 344 /* 345 * We're already too high in the address space 346 * to succeed, reset to 0 for the next iteration. 347 */ 348 start = 0; 349 continue; /* next pass */ 350 } 351 start = i; 352 353 /* 354 * Check successive pages for contiguous and free. 355 * 356 * (still in critical section) 357 */ 358 for (i = start + 1; i < (start + size / PAGE_SIZE); i++) { 359 m = &pga[i]; 360 pqtype = m->queue - m->pc; 361 if ((VM_PAGE_TO_PHYS(&m[0]) != 362 (VM_PAGE_TO_PHYS(&m[-1]) + PAGE_SIZE)) || 363 ((pqtype != PQ_FREE) && (pqtype != PQ_CACHE)) || 364 m->wire_count || 365 m->hold_count || 366 (m->busy_count & (PBUSY_LOCKED | PBUSY_MASK)) || 367 (m->flags & PG_NEED_COMMIT)) 368 { 369 start++; 370 goto again; 371 } 372 } 373 374 /* 375 * Try to allocate the pages, wiring them as we go. 376 * 377 * (still in critical section) 378 */ 379 for (i = start; i < (start + size / PAGE_SIZE); i++) { 380 m = &pga[i]; 381 382 if (vm_page_busy_try(m, TRUE)) { 383 vm_contig_pg_free(start, 384 (i - start) * PAGE_SIZE); 385 start++; 386 goto again; 387 } 388 pqtype = m->queue - m->pc; 389 if (pqtype == PQ_CACHE && 390 m->hold_count == 0 && 391 m->wire_count == 0 && 392 (m->flags & (PG_UNMANAGED | PG_NEED_COMMIT)) == 0) { 393 vm_page_protect(m, VM_PROT_NONE); 394 KKASSERT((m->flags & PG_MAPPED) == 0); 395 KKASSERT(m->dirty == 0); 396 vm_page_free(m); 397 --i; 398 continue; /* retry the page */ 399 } 400 if (pqtype != PQ_FREE || m->hold_count) { 401 vm_page_wakeup(m); 402 vm_contig_pg_free(start, 403 (i - start) * PAGE_SIZE); 404 start++; 405 goto again; 406 } 407 KKASSERT((m->valid & m->dirty) == 0); 408 KKASSERT(m->wire_count == 0); 409 KKASSERT(m->object == NULL); 410 vm_page_unqueue_nowakeup(m); 411 m->valid = VM_PAGE_BITS_ALL; 412 KASSERT(m->dirty == 0, 413 ("vm_contig_pg_alloc: page %p was dirty", m)); 414 KKASSERT(m->wire_count == 0); 415 KKASSERT((m->busy_count & PBUSY_MASK) == 0); 416 417 /* 418 * Clear all flags. Then unbusy the now allocated 419 * page. 420 */ 421 vm_page_flag_clear(m, ~PG_KEEP_NEWPAGE_MASK); 422 vm_page_wire(m); 423 vm_page_wakeup(m); 424 } 425 426 /* 427 * Our job is done, return the index page of vm_page_array. 428 */ 429 return (start); /* aka &pga[start] */ 430 } 431 432 /* 433 * Failed. 434 */ 435 return (-1); 436 } 437 438 /* 439 * vm_contig_pg_free: 440 * 441 * Remove pages previously allocated by vm_contig_pg_alloc, and 442 * assume all references to the pages have been removed, and that 443 * it is OK to add them back to the free list. 444 * 445 * Caller must ensure no races on the page range in question. 446 * No other requirements. 447 */ 448 static void 449 vm_contig_pg_free(int start, u_long size) 450 { 451 vm_page_t pga = vm_page_array; 452 453 size = round_page(size); 454 if (size == 0) 455 panic("vm_contig_pg_free: size must not be 0"); 456 457 /* 458 * The pages are wired, vm_page_free_contig() determines whether they 459 * belong to the contig space or not and either frees them to that 460 * space (leaving them wired), or unwires the page and frees it to the 461 * normal PQ_FREE queue. 462 */ 463 vm_page_free_contig(&pga[start], size); 464 } 465 466 /* 467 * vm_contig_pg_kmap: 468 * 469 * Map previously allocated (vm_contig_pg_alloc) range of pages from 470 * vm_page_array[] into the KVA. Once mapped, the pages are part of 471 * the Kernel, and are to free'ed with kmem_free(&kernel_map, addr, size). 472 * 473 * No requirements. 474 */ 475 static vm_offset_t 476 vm_contig_pg_kmap(int start, u_long size, vm_map_t map, int flags) 477 { 478 vm_offset_t addr; 479 vm_paddr_t pa; 480 vm_page_t pga = vm_page_array; 481 u_long offset; 482 483 if (size == 0) 484 panic("vm_contig_pg_kmap: size must not be 0"); 485 size = round_page(size); 486 addr = kmem_alloc_pageable(&kernel_map, size, VM_SUBSYS_CONTIG); 487 if (addr) { 488 pa = VM_PAGE_TO_PHYS(&pga[start]); 489 for (offset = 0; offset < size; offset += PAGE_SIZE) 490 pmap_kenter_noinval(addr + offset, pa + offset); 491 pmap_invalidate_range(&kernel_pmap, addr, addr + size); 492 if (flags & M_ZERO) 493 bzero((void *)addr, size); 494 } 495 return(addr); 496 } 497 498 /* 499 * No requirements. 500 */ 501 void * 502 contigmalloc( 503 unsigned long size, /* should be size_t here and for malloc() */ 504 struct malloc_type *type, 505 int flags, 506 vm_paddr_t low, 507 vm_paddr_t high, 508 unsigned long alignment, 509 unsigned long boundary) 510 { 511 return contigmalloc_map(size, type, flags, low, high, alignment, 512 boundary, &kernel_map); 513 } 514 515 /* 516 * No requirements. 517 */ 518 void * 519 contigmalloc_map(unsigned long size, struct malloc_type *type, 520 int flags, vm_paddr_t low, vm_paddr_t high, 521 unsigned long alignment, unsigned long boundary, 522 vm_map_t map) 523 { 524 int index; 525 void *rv; 526 527 index = vm_contig_pg_alloc(size, low, high, alignment, boundary, flags); 528 if (index < 0) { 529 kprintf("contigmalloc_map: failed size %lu low=%llx " 530 "high=%llx align=%lu boundary=%lu flags=%08x\n", 531 size, (long long)low, (long long)high, 532 alignment, boundary, flags); 533 return NULL; 534 } 535 536 rv = (void *)vm_contig_pg_kmap(index, size, map, flags); 537 if (rv == NULL) 538 vm_contig_pg_free(index, size); 539 540 return rv; 541 } 542 543 /* 544 * No requirements. 545 */ 546 void 547 contigfree(void *addr, unsigned long size, struct malloc_type *type) 548 { 549 vm_paddr_t pa; 550 vm_page_t m; 551 552 if (size == 0) 553 panic("vm_contig_pg_kmap: size must not be 0"); 554 size = round_page(size); 555 556 pa = pmap_kextract((vm_offset_t)addr); 557 pmap_qremove((vm_offset_t)addr, size / PAGE_SIZE); 558 kmem_free(&kernel_map, (vm_offset_t)addr, size); 559 560 m = PHYS_TO_VM_PAGE(pa); 561 vm_page_free_contig(m, size); 562 } 563 564 /* 565 * No requirements. 566 */ 567 vm_offset_t 568 kmem_alloc_contig(vm_offset_t size, vm_paddr_t low, vm_paddr_t high, 569 vm_offset_t alignment) 570 { 571 return ((vm_offset_t)contigmalloc_map(size, M_DEVBUF, M_NOWAIT, low, 572 high, alignment, 0ul, &kernel_map)); 573 } 574