1 /************************************************************************** 2 * 3 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sub license, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 24 * USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 * $FreeBSD: head/sys/dev/drm2/ttm/ttm_memory.c 248663 2013-03-23 20:46:47Z dumbbell $ 27 **************************************************************************/ 28 29 #define pr_fmt(fmt) "[TTM] " fmt 30 31 #include <drm/drmP.h> 32 #include <drm/ttm/ttm_memory.h> 33 #include <drm/ttm/ttm_module.h> 34 #include <drm/ttm/ttm_page_alloc.h> 35 #include <linux/export.h> 36 37 #define TTM_MEMORY_ALLOC_RETRIES 4 38 39 struct ttm_mem_zone { 40 struct kobject kobj; 41 struct ttm_mem_global *glob; 42 const char *name; 43 uint64_t zone_mem; 44 uint64_t emer_mem; 45 uint64_t max_mem; 46 uint64_t swap_limit; 47 uint64_t used_mem; 48 }; 49 50 static struct attribute ttm_mem_sys = { 51 .name = "zone_memory", 52 .mode = S_IRUGO 53 }; 54 static struct attribute ttm_mem_emer = { 55 .name = "emergency_memory", 56 .mode = S_IRUGO | S_IWUSR 57 }; 58 static struct attribute ttm_mem_max = { 59 .name = "available_memory", 60 .mode = S_IRUGO | S_IWUSR 61 }; 62 static struct attribute ttm_mem_swap = { 63 .name = "swap_limit", 64 .mode = S_IRUGO | S_IWUSR 65 }; 66 static struct attribute ttm_mem_used = { 67 .name = "used_memory", 68 .mode = S_IRUGO 69 }; 70 71 static void ttm_mem_zone_kobj_release(struct kobject *kobj) 72 { 73 struct ttm_mem_zone *zone = 74 container_of(kobj, struct ttm_mem_zone, kobj); 75 76 pr_info("Zone %7s: Used memory at exit: %llu kiB\n", 77 zone->name, (unsigned long long)zone->used_mem >> 10); 78 kfree(zone); 79 } 80 81 static ssize_t ttm_mem_zone_show(struct kobject *kobj, 82 struct attribute *attr, 83 char *buffer) 84 { 85 struct ttm_mem_zone *zone = 86 container_of(kobj, struct ttm_mem_zone, kobj); 87 uint64_t val = 0; 88 89 lockmgr(&zone->glob->lock, LK_EXCLUSIVE); 90 if (attr == &ttm_mem_sys) 91 val = zone->zone_mem; 92 else if (attr == &ttm_mem_emer) 93 val = zone->emer_mem; 94 else if (attr == &ttm_mem_max) 95 val = zone->max_mem; 96 else if (attr == &ttm_mem_swap) 97 val = zone->swap_limit; 98 else if (attr == &ttm_mem_used) 99 val = zone->used_mem; 100 lockmgr(&zone->glob->lock, LK_RELEASE); 101 102 return ksnprintf(buffer, PAGE_SIZE, "%llu\n", 103 (unsigned long long) val >> 10); 104 } 105 106 static void ttm_check_swapping(struct ttm_mem_global *glob); 107 108 static ssize_t ttm_mem_zone_store(struct kobject *kobj, 109 struct attribute *attr, 110 const char *buffer, 111 size_t size) 112 { 113 struct ttm_mem_zone *zone = 114 container_of(kobj, struct ttm_mem_zone, kobj); 115 int chars; 116 unsigned long val; 117 uint64_t val64; 118 119 chars = ksscanf(buffer, "%lu", &val); 120 if (chars == 0) 121 return size; 122 123 val64 = val; 124 val64 <<= 10; 125 126 lockmgr(&zone->glob->lock, LK_EXCLUSIVE); 127 if (val64 > zone->zone_mem) 128 val64 = zone->zone_mem; 129 if (attr == &ttm_mem_emer) { 130 zone->emer_mem = val64; 131 if (zone->max_mem > val64) 132 zone->max_mem = val64; 133 } else if (attr == &ttm_mem_max) { 134 zone->max_mem = val64; 135 if (zone->emer_mem < val64) 136 zone->emer_mem = val64; 137 } else if (attr == &ttm_mem_swap) 138 zone->swap_limit = val64; 139 lockmgr(&zone->glob->lock, LK_RELEASE); 140 141 ttm_check_swapping(zone->glob); 142 143 return size; 144 } 145 146 static struct attribute *ttm_mem_zone_attrs[] = { 147 &ttm_mem_sys, 148 &ttm_mem_emer, 149 &ttm_mem_max, 150 &ttm_mem_swap, 151 &ttm_mem_used, 152 NULL 153 }; 154 155 static const struct sysfs_ops ttm_mem_zone_ops = { 156 .show = &ttm_mem_zone_show, 157 .store = &ttm_mem_zone_store 158 }; 159 160 static struct kobj_type ttm_mem_zone_kobj_type = { 161 .release = &ttm_mem_zone_kobj_release, 162 .sysfs_ops = &ttm_mem_zone_ops, 163 .default_attrs = ttm_mem_zone_attrs, 164 }; 165 166 static void ttm_mem_global_kobj_release(struct kobject *kobj) 167 { 168 struct ttm_mem_global *glob = 169 container_of(kobj, struct ttm_mem_global, kobj); 170 171 kfree(glob); 172 } 173 174 static struct kobj_type ttm_mem_glob_kobj_type = { 175 .release = &ttm_mem_global_kobj_release, 176 }; 177 178 static bool ttm_zones_above_swap_target(struct ttm_mem_global *glob, 179 bool from_wq, uint64_t extra) 180 { 181 unsigned int i; 182 struct ttm_mem_zone *zone; 183 uint64_t target; 184 185 for (i = 0; i < glob->num_zones; ++i) { 186 zone = glob->zones[i]; 187 188 if (from_wq) 189 target = zone->swap_limit; 190 else if (priv_check(curthread, PRIV_VM_MLOCK) == 0) 191 target = zone->emer_mem; 192 else 193 target = zone->max_mem; 194 195 target = (extra > target) ? 0ULL : target; 196 197 if (zone->used_mem > target) 198 return true; 199 } 200 return false; 201 } 202 203 /** 204 * At this point we only support a single shrink callback. 205 * Extend this if needed, perhaps using a linked list of callbacks. 206 * Note that this function is reentrant: 207 * many threads may try to swap out at any given time. 208 */ 209 210 static void ttm_shrink(struct ttm_mem_global *glob, bool from_wq, 211 uint64_t extra) 212 { 213 int ret; 214 struct ttm_mem_shrink *shrink; 215 216 lockmgr(&glob->lock, LK_EXCLUSIVE); 217 if (glob->shrink == NULL) 218 goto out; 219 220 while (ttm_zones_above_swap_target(glob, from_wq, extra)) { 221 shrink = glob->shrink; 222 lockmgr(&glob->lock, LK_RELEASE); 223 ret = shrink->do_shrink(shrink); 224 lockmgr(&glob->lock, LK_EXCLUSIVE); 225 if (unlikely(ret != 0)) 226 goto out; 227 } 228 out: 229 lockmgr(&glob->lock, LK_RELEASE); 230 } 231 232 233 234 static void ttm_shrink_work(void *arg, int pending __unused) 235 { 236 struct ttm_mem_global *glob = arg; 237 238 ttm_shrink(glob, true, 0ULL); 239 } 240 241 static int ttm_mem_init_kernel_zone(struct ttm_mem_global *glob, 242 uint64_t mem) 243 { 244 struct ttm_mem_zone *zone = kzalloc(sizeof(*zone), GFP_KERNEL); 245 int ret; 246 247 zone->name = "kernel"; 248 zone->zone_mem = mem; 249 zone->max_mem = mem >> 1; 250 zone->emer_mem = (mem >> 1) + (mem >> 2); 251 zone->swap_limit = zone->max_mem - (mem >> 3); 252 zone->used_mem = 0; 253 zone->glob = glob; 254 glob->zone_kernel = zone; 255 ret = kobject_init_and_add( 256 &zone->kobj, &ttm_mem_zone_kobj_type, &glob->kobj, zone->name); 257 if (unlikely(ret != 0)) { 258 kobject_put(&zone->kobj); 259 return ret; 260 } 261 glob->zones[glob->num_zones++] = zone; 262 return 0; 263 } 264 265 #ifdef CONFIG_HIGHMEM 266 #else 267 static int ttm_mem_init_dma32_zone(struct ttm_mem_global *glob, 268 uint64_t mem) 269 { 270 struct ttm_mem_zone *zone = kzalloc(sizeof(*zone), GFP_KERNEL); 271 int ret; 272 273 /** 274 * No special dma32 zone needed. 275 */ 276 277 if ((physmem * PAGE_SIZE) <= ((uint64_t) 1ULL << 32)) { 278 kfree(zone); 279 return 0; 280 } 281 282 /* 283 * Limit max dma32 memory to 4GB for now 284 * until we can figure out how big this 285 * zone really is. 286 */ 287 if (mem > ((uint64_t) 1ULL << 32)) 288 mem = ((uint64_t) 1ULL << 32); 289 290 zone->name = "dma32"; 291 zone->zone_mem = mem; 292 zone->max_mem = mem >> 1; 293 zone->emer_mem = (mem >> 1) + (mem >> 2); 294 zone->swap_limit = zone->max_mem - (mem >> 3); 295 zone->used_mem = 0; 296 zone->glob = glob; 297 glob->zone_dma32 = zone; 298 ret = kobject_init_and_add( 299 &zone->kobj, &ttm_mem_zone_kobj_type, &glob->kobj, "%s", 300 zone->name); 301 if (unlikely(ret != 0)) { 302 kobject_put(&zone->kobj); 303 return ret; 304 } 305 glob->zones[glob->num_zones++] = zone; 306 return 0; 307 } 308 #endif 309 310 int ttm_mem_global_init(struct ttm_mem_global *glob) 311 { 312 u_int64_t mem; 313 int ret; 314 int i; 315 struct ttm_mem_zone *zone; 316 317 lockinit(&glob->lock, "ttmemglob", 0, 0); 318 glob->swap_queue = taskqueue_create("ttm_swap", M_WAITOK, 319 taskqueue_thread_enqueue, &glob->swap_queue); 320 taskqueue_start_threads(&glob->swap_queue, 1, TDPRI_KERN_DAEMON, 321 -1, "ttm swap"); 322 TASK_INIT(&glob->work, 0, ttm_shrink_work, glob); 323 ret = kobject_init_and_add( 324 &glob->kobj, &ttm_mem_glob_kobj_type, ttm_get_kobj(), "memory_accounting"); 325 if (unlikely(ret != 0)) { 326 kobject_put(&glob->kobj); 327 return ret; 328 } 329 330 /* 331 * Managed contiguous memory for TTM. Only use kernel-reserved 332 * dma memory for TTM, which can be controlled via /boot/loader.conf 333 * (e.g. vm.dma_reserved=256m). This is the only truly dependable 334 * DMA memory. 335 */ 336 mem = (uint64_t)vm_contig_avail_pages() * PAGE_SIZE; 337 338 ret = ttm_mem_init_kernel_zone(glob, mem); 339 if (unlikely(ret != 0)) 340 goto out_no_zone; 341 ret = ttm_mem_init_dma32_zone(glob, mem); 342 if (unlikely(ret != 0)) 343 goto out_no_zone; 344 pr_info("(struct ttm_mem_global *)%p\n", glob); 345 for (i = 0; i < glob->num_zones; ++i) { 346 zone = glob->zones[i]; 347 pr_info("Zone %7s: Available graphics memory: %llu kiB\n", 348 zone->name, (unsigned long long)zone->max_mem >> 10); 349 } 350 ttm_page_alloc_init(glob, glob->zone_kernel->max_mem/(2*PAGE_SIZE)); 351 ttm_dma_page_alloc_init(glob, glob->zone_kernel->max_mem/(2*PAGE_SIZE)); 352 return 0; 353 out_no_zone: 354 ttm_mem_global_release(glob); 355 return ret; 356 } 357 EXPORT_SYMBOL(ttm_mem_global_init); 358 359 void ttm_mem_global_release(struct ttm_mem_global *glob) 360 { 361 unsigned int i; 362 struct ttm_mem_zone *zone; 363 364 /* let the page allocator first stop the shrink work. */ 365 ttm_page_alloc_fini(); 366 ttm_dma_page_alloc_fini(); 367 368 taskqueue_drain(glob->swap_queue, &glob->work); 369 taskqueue_free(glob->swap_queue); 370 glob->swap_queue = NULL; 371 for (i = 0; i < glob->num_zones; ++i) { 372 zone = glob->zones[i]; 373 kobject_del(&zone->kobj); 374 kobject_put(&zone->kobj); 375 } 376 kobject_del(&glob->kobj); 377 kobject_put(&glob->kobj); 378 379 } 380 EXPORT_SYMBOL(ttm_mem_global_release); 381 382 static void ttm_check_swapping(struct ttm_mem_global *glob) 383 { 384 bool needs_swapping = false; 385 unsigned int i; 386 struct ttm_mem_zone *zone; 387 388 lockmgr(&glob->lock, LK_EXCLUSIVE); 389 for (i = 0; i < glob->num_zones; ++i) { 390 zone = glob->zones[i]; 391 if (zone->used_mem > zone->swap_limit) { 392 needs_swapping = true; 393 break; 394 } 395 } 396 lockmgr(&glob->lock, LK_RELEASE); 397 398 if (unlikely(needs_swapping)) 399 taskqueue_enqueue(glob->swap_queue, &glob->work); 400 401 } 402 403 static void ttm_mem_global_free_zone(struct ttm_mem_global *glob, 404 struct ttm_mem_zone *single_zone, 405 uint64_t amount) 406 { 407 unsigned int i; 408 struct ttm_mem_zone *zone; 409 410 lockmgr(&glob->lock, LK_EXCLUSIVE); 411 for (i = 0; i < glob->num_zones; ++i) { 412 zone = glob->zones[i]; 413 if (single_zone && zone != single_zone) 414 continue; 415 zone->used_mem -= amount; 416 } 417 lockmgr(&glob->lock, LK_RELEASE); 418 } 419 420 void ttm_mem_global_free(struct ttm_mem_global *glob, 421 uint64_t amount) 422 { 423 ttm_mem_global_free_zone(glob, NULL, amount); 424 } 425 EXPORT_SYMBOL(ttm_mem_global_free); 426 427 static int ttm_mem_global_reserve(struct ttm_mem_global *glob, 428 struct ttm_mem_zone *single_zone, 429 uint64_t amount, bool reserve) 430 { 431 uint64_t limit; 432 int ret = -ENOMEM; 433 unsigned int i; 434 struct ttm_mem_zone *zone; 435 436 lockmgr(&glob->lock, LK_EXCLUSIVE); 437 for (i = 0; i < glob->num_zones; ++i) { 438 zone = glob->zones[i]; 439 if (single_zone && zone != single_zone) 440 continue; 441 442 limit = (priv_check(curthread, PRIV_VM_MLOCK) == 0) ? 443 zone->emer_mem : zone->max_mem; 444 445 if (zone->used_mem > limit) 446 goto out_unlock; 447 } 448 449 if (reserve) { 450 for (i = 0; i < glob->num_zones; ++i) { 451 zone = glob->zones[i]; 452 if (single_zone && zone != single_zone) 453 continue; 454 zone->used_mem += amount; 455 } 456 } 457 458 ret = 0; 459 out_unlock: 460 lockmgr(&glob->lock, LK_RELEASE); 461 ttm_check_swapping(glob); 462 463 return ret; 464 } 465 466 467 static int ttm_mem_global_alloc_zone(struct ttm_mem_global *glob, 468 struct ttm_mem_zone *single_zone, 469 uint64_t memory, 470 bool no_wait, bool interruptible) 471 { 472 int count = TTM_MEMORY_ALLOC_RETRIES; 473 474 while (unlikely(ttm_mem_global_reserve(glob, 475 single_zone, 476 memory, true) 477 != 0)) { 478 if (no_wait) 479 return -ENOMEM; 480 if (unlikely(count-- == 0)) 481 return -ENOMEM; 482 ttm_shrink(glob, false, memory + (memory >> 2) + 16); 483 } 484 485 return 0; 486 } 487 488 int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory, 489 bool no_wait, bool interruptible) 490 { 491 /** 492 * Normal allocations of kernel memory are registered in 493 * all zones. 494 */ 495 496 return ttm_mem_global_alloc_zone(glob, NULL, memory, no_wait, 497 interruptible); 498 } 499 EXPORT_SYMBOL(ttm_mem_global_alloc); 500 501 int ttm_mem_global_alloc_page(struct ttm_mem_global *glob, 502 struct page *page, 503 bool no_wait, bool interruptible) 504 { 505 506 struct ttm_mem_zone *zone = NULL; 507 508 /** 509 * Page allocations may be registed in a single zone 510 * only if highmem or !dma32. 511 */ 512 513 if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL) 514 zone = glob->zone_kernel; 515 return ttm_mem_global_alloc_zone(glob, zone, PAGE_SIZE, no_wait, 516 interruptible); 517 } 518 519 void ttm_mem_global_free_page(struct ttm_mem_global *glob, struct page *page) 520 { 521 struct ttm_mem_zone *zone = NULL; 522 523 if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL) 524 zone = glob->zone_kernel; 525 ttm_mem_global_free_zone(glob, zone, PAGE_SIZE); 526 } 527 528 529 size_t ttm_round_pot(size_t size) 530 { 531 if ((size & (size - 1)) == 0) 532 return size; 533 else if (size > PAGE_SIZE) 534 return PAGE_ALIGN(size); 535 else { 536 size_t tmp_size = 4; 537 538 while (tmp_size < size) 539 tmp_size <<= 1; 540 541 return tmp_size; 542 } 543 return 0; 544 } 545 EXPORT_SYMBOL(ttm_round_pot); 546 547 uint64_t ttm_get_kernel_zone_memory_size(struct ttm_mem_global *glob) 548 { 549 return glob->zone_kernel->max_mem; 550 } 551 EXPORT_SYMBOL(ttm_get_kernel_zone_memory_size); 552