1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 /**************************************************************************
4 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25 * USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
29 #define pr_fmt(fmt) "[TTM] " fmt
31 #include <drm/ttm/ttm_memory.h>
32 #include <linux/spinlock.h>
33 #include <linux/sched.h>
34 #include <linux/wait.h>
36 #include <linux/module.h>
37 #include <linux/slab.h>
38 #include <linux/swap.h>
39 #include <drm/ttm/ttm_pool.h>
41 #include "ttm_module.h"
43 #define TTM_MEMORY_ALLOC_RETRIES 4
45 struct ttm_mem_global ttm_mem_glob;
46 EXPORT_SYMBOL(ttm_mem_glob);
50 struct ttm_mem_global *glob;
59 static struct attribute ttm_mem_sys = {
60 .name = "zone_memory",
63 static struct attribute ttm_mem_emer = {
64 .name = "emergency_memory",
65 .mode = S_IRUGO | S_IWUSR
67 static struct attribute ttm_mem_max = {
68 .name = "available_memory",
69 .mode = S_IRUGO | S_IWUSR
71 static struct attribute ttm_mem_swap = {
73 .mode = S_IRUGO | S_IWUSR
75 static struct attribute ttm_mem_used = {
76 .name = "used_memory",
80 static void ttm_mem_zone_kobj_release(struct kobject *kobj)
82 struct ttm_mem_zone *zone =
83 container_of(kobj, struct ttm_mem_zone, kobj);
85 pr_info("Zone %7s: Used memory at exit: %llu KiB\n",
86 zone->name, (unsigned long long)zone->used_mem >> 10);
90 static ssize_t ttm_mem_zone_show(struct kobject *kobj,
91 struct attribute *attr,
94 struct ttm_mem_zone *zone =
95 container_of(kobj, struct ttm_mem_zone, kobj);
98 spin_lock(&zone->glob->lock);
99 if (attr == &ttm_mem_sys)
100 val = zone->zone_mem;
101 else if (attr == &ttm_mem_emer)
102 val = zone->emer_mem;
103 else if (attr == &ttm_mem_max)
105 else if (attr == &ttm_mem_swap)
106 val = zone->swap_limit;
107 else if (attr == &ttm_mem_used)
108 val = zone->used_mem;
109 spin_unlock(&zone->glob->lock);
111 return snprintf(buffer, PAGE_SIZE, "%llu\n",
112 (unsigned long long) val >> 10);
115 static void ttm_check_swapping(struct ttm_mem_global *glob);
117 static ssize_t ttm_mem_zone_store(struct kobject *kobj,
118 struct attribute *attr,
122 struct ttm_mem_zone *zone =
123 container_of(kobj, struct ttm_mem_zone, kobj);
128 chars = sscanf(buffer, "%lu", &val);
135 spin_lock(&zone->glob->lock);
136 if (val64 > zone->zone_mem)
137 val64 = zone->zone_mem;
138 if (attr == &ttm_mem_emer) {
139 zone->emer_mem = val64;
140 if (zone->max_mem > val64)
141 zone->max_mem = val64;
142 } else if (attr == &ttm_mem_max) {
143 zone->max_mem = val64;
144 if (zone->emer_mem < val64)
145 zone->emer_mem = val64;
146 } else if (attr == &ttm_mem_swap)
147 zone->swap_limit = val64;
148 spin_unlock(&zone->glob->lock);
150 ttm_check_swapping(zone->glob);
155 static struct attribute *ttm_mem_zone_attrs[] = {
164 static const struct sysfs_ops ttm_mem_zone_ops = {
165 .show = &ttm_mem_zone_show,
166 .store = &ttm_mem_zone_store
169 static struct kobj_type ttm_mem_zone_kobj_type = {
170 .release = &ttm_mem_zone_kobj_release,
171 .sysfs_ops = &ttm_mem_zone_ops,
172 .default_attrs = ttm_mem_zone_attrs,
175 static struct attribute ttm_mem_global_lower_mem_limit = {
176 .name = "lower_mem_limit",
177 .mode = S_IRUGO | S_IWUSR
180 static ssize_t ttm_mem_global_show(struct kobject *kobj,
181 struct attribute *attr,
184 struct ttm_mem_global *glob =
185 container_of(kobj, struct ttm_mem_global, kobj);
188 spin_lock(&glob->lock);
189 val = glob->lower_mem_limit;
190 spin_unlock(&glob->lock);
191 /* convert from number of pages to KB */
192 val <<= (PAGE_SHIFT - 10);
193 return snprintf(buffer, PAGE_SIZE, "%llu\n",
194 (unsigned long long) val);
197 static ssize_t ttm_mem_global_store(struct kobject *kobj,
198 struct attribute *attr,
205 struct ttm_mem_global *glob =
206 container_of(kobj, struct ttm_mem_global, kobj);
208 chars = sscanf(buffer, "%lu", &val);
213 /* convert from KB to number of pages */
214 val64 >>= (PAGE_SHIFT - 10);
216 spin_lock(&glob->lock);
217 glob->lower_mem_limit = val64;
218 spin_unlock(&glob->lock);
223 static struct attribute *ttm_mem_global_attrs[] = {
224 &ttm_mem_global_lower_mem_limit,
228 static const struct sysfs_ops ttm_mem_global_ops = {
229 .show = &ttm_mem_global_show,
230 .store = &ttm_mem_global_store,
233 static struct kobj_type ttm_mem_glob_kobj_type = {
234 .sysfs_ops = &ttm_mem_global_ops,
235 .default_attrs = ttm_mem_global_attrs,
238 static bool ttm_zones_above_swap_target(struct ttm_mem_global *glob,
239 bool from_wq, uint64_t extra)
242 struct ttm_mem_zone *zone;
245 for (i = 0; i < glob->num_zones; ++i) {
246 zone = glob->zones[i];
249 target = zone->swap_limit;
250 else if (capable(CAP_SYS_ADMIN))
251 target = zone->emer_mem;
253 target = zone->max_mem;
255 target = (extra > target) ? 0ULL : target;
257 if (zone->used_mem > target)
264 * At this point we only support a single shrink callback.
265 * Extend this if needed, perhaps using a linked list of callbacks.
266 * Note that this function is reentrant:
267 * many threads may try to swap out at any given time.
270 static void ttm_shrink(struct ttm_mem_global *glob, bool from_wq,
271 uint64_t extra, struct ttm_operation_ctx *ctx)
275 spin_lock(&glob->lock);
277 while (ttm_zones_above_swap_target(glob, from_wq, extra)) {
278 spin_unlock(&glob->lock);
279 ret = ttm_bo_swapout(ctx);
280 spin_lock(&glob->lock);
281 if (unlikely(ret != 0))
285 spin_unlock(&glob->lock);
288 static void ttm_shrink_work(struct work_struct *work)
290 struct ttm_operation_ctx ctx = {
291 .interruptible = false,
294 struct ttm_mem_global *glob =
295 container_of(work, struct ttm_mem_global, work);
297 ttm_shrink(glob, true, 0ULL, &ctx);
300 static int ttm_mem_init_kernel_zone(struct ttm_mem_global *glob,
301 const struct sysinfo *si)
303 struct ttm_mem_zone *zone = kzalloc(sizeof(*zone), GFP_KERNEL);
310 mem = si->totalram - si->totalhigh;
313 zone->name = "kernel";
314 zone->zone_mem = mem;
315 zone->max_mem = mem >> 1;
316 zone->emer_mem = (mem >> 1) + (mem >> 2);
317 zone->swap_limit = zone->max_mem - (mem >> 3);
320 glob->zone_kernel = zone;
321 ret = kobject_init_and_add(
322 &zone->kobj, &ttm_mem_zone_kobj_type, &glob->kobj, zone->name);
323 if (unlikely(ret != 0)) {
324 kobject_put(&zone->kobj);
327 glob->zones[glob->num_zones++] = zone;
331 #ifdef CONFIG_HIGHMEM
332 static int ttm_mem_init_highmem_zone(struct ttm_mem_global *glob,
333 const struct sysinfo *si)
335 struct ttm_mem_zone *zone;
339 if (si->totalhigh == 0)
342 zone = kzalloc(sizeof(*zone), GFP_KERNEL);
349 zone->name = "highmem";
350 zone->zone_mem = mem;
351 zone->max_mem = mem >> 1;
352 zone->emer_mem = (mem >> 1) + (mem >> 2);
353 zone->swap_limit = zone->max_mem - (mem >> 3);
356 glob->zone_highmem = zone;
357 ret = kobject_init_and_add(
358 &zone->kobj, &ttm_mem_zone_kobj_type, &glob->kobj, "%s",
360 if (unlikely(ret != 0)) {
361 kobject_put(&zone->kobj);
364 glob->zones[glob->num_zones++] = zone;
368 static int ttm_mem_init_dma32_zone(struct ttm_mem_global *glob,
369 const struct sysinfo *si)
371 struct ttm_mem_zone *zone = kzalloc(sizeof(*zone), GFP_KERNEL);
382 * No special dma32 zone needed.
385 if (mem <= ((uint64_t) 1ULL << 32)) {
391 * Limit max dma32 memory to 4GB for now
392 * until we can figure out how big this
396 mem = ((uint64_t) 1ULL << 32);
397 zone->name = "dma32";
398 zone->zone_mem = mem;
399 zone->max_mem = mem >> 1;
400 zone->emer_mem = (mem >> 1) + (mem >> 2);
401 zone->swap_limit = zone->max_mem - (mem >> 3);
404 glob->zone_dma32 = zone;
405 ret = kobject_init_and_add(
406 &zone->kobj, &ttm_mem_zone_kobj_type, &glob->kobj, zone->name);
407 if (unlikely(ret != 0)) {
408 kobject_put(&zone->kobj);
411 glob->zones[glob->num_zones++] = zone;
416 int ttm_mem_global_init(struct ttm_mem_global *glob)
421 struct ttm_mem_zone *zone;
423 spin_lock_init(&glob->lock);
424 glob->swap_queue = create_singlethread_workqueue("ttm_swap");
425 INIT_WORK(&glob->work, ttm_shrink_work);
426 ret = kobject_init_and_add(
427 &glob->kobj, &ttm_mem_glob_kobj_type, ttm_get_kobj(), "memory_accounting");
428 if (unlikely(ret != 0)) {
429 kobject_put(&glob->kobj);
435 /* set it as 0 by default to keep original behavior of OOM */
436 glob->lower_mem_limit = 0;
438 ret = ttm_mem_init_kernel_zone(glob, &si);
439 if (unlikely(ret != 0))
441 #ifdef CONFIG_HIGHMEM
442 ret = ttm_mem_init_highmem_zone(glob, &si);
443 if (unlikely(ret != 0))
446 ret = ttm_mem_init_dma32_zone(glob, &si);
447 if (unlikely(ret != 0))
450 for (i = 0; i < glob->num_zones; ++i) {
451 zone = glob->zones[i];
452 pr_info("Zone %7s: Available graphics memory: %llu KiB\n",
453 zone->name, (unsigned long long)zone->max_mem >> 10);
455 ttm_pool_mgr_init(glob->zone_kernel->max_mem/(2*PAGE_SIZE));
458 ttm_mem_global_release(glob);
462 void ttm_mem_global_release(struct ttm_mem_global *glob)
464 struct ttm_mem_zone *zone;
467 /* let the page allocator first stop the shrink work. */
470 flush_workqueue(glob->swap_queue);
471 destroy_workqueue(glob->swap_queue);
472 glob->swap_queue = NULL;
473 for (i = 0; i < glob->num_zones; ++i) {
474 zone = glob->zones[i];
475 kobject_del(&zone->kobj);
476 kobject_put(&zone->kobj);
478 kobject_del(&glob->kobj);
479 kobject_put(&glob->kobj);
480 memset(glob, 0, sizeof(*glob));
483 static void ttm_check_swapping(struct ttm_mem_global *glob)
485 bool needs_swapping = false;
487 struct ttm_mem_zone *zone;
489 spin_lock(&glob->lock);
490 for (i = 0; i < glob->num_zones; ++i) {
491 zone = glob->zones[i];
492 if (zone->used_mem > zone->swap_limit) {
493 needs_swapping = true;
498 spin_unlock(&glob->lock);
500 if (unlikely(needs_swapping))
501 (void)queue_work(glob->swap_queue, &glob->work);
505 static void ttm_mem_global_free_zone(struct ttm_mem_global *glob,
506 struct ttm_mem_zone *single_zone,
510 struct ttm_mem_zone *zone;
512 spin_lock(&glob->lock);
513 for (i = 0; i < glob->num_zones; ++i) {
514 zone = glob->zones[i];
515 if (single_zone && zone != single_zone)
517 zone->used_mem -= amount;
519 spin_unlock(&glob->lock);
522 void ttm_mem_global_free(struct ttm_mem_global *glob,
525 return ttm_mem_global_free_zone(glob, glob->zone_kernel, amount);
527 EXPORT_SYMBOL(ttm_mem_global_free);
530 * check if the available mem is under lower memory limit
532 * a. if no swap disk at all or free swap space is under swap_mem_limit
533 * but available system mem is bigger than sys_mem_limit, allow TTM
536 * b. if the available system mem is less than sys_mem_limit but free
537 * swap disk is bigger than swap_mem_limit, allow TTM allocation.
540 ttm_check_under_lowerlimit(struct ttm_mem_global *glob,
542 struct ttm_operation_ctx *ctx)
546 /* We allow over commit during suspend */
547 if (ctx->force_alloc)
550 available = get_nr_swap_pages() + si_mem_available();
551 available -= num_pages;
552 if (available < glob->lower_mem_limit)
558 static int ttm_mem_global_reserve(struct ttm_mem_global *glob,
559 struct ttm_mem_zone *single_zone,
560 uint64_t amount, bool reserve)
565 struct ttm_mem_zone *zone;
567 spin_lock(&glob->lock);
568 for (i = 0; i < glob->num_zones; ++i) {
569 zone = glob->zones[i];
570 if (single_zone && zone != single_zone)
573 limit = (capable(CAP_SYS_ADMIN)) ?
574 zone->emer_mem : zone->max_mem;
576 if (zone->used_mem > limit)
581 for (i = 0; i < glob->num_zones; ++i) {
582 zone = glob->zones[i];
583 if (single_zone && zone != single_zone)
585 zone->used_mem += amount;
591 spin_unlock(&glob->lock);
592 ttm_check_swapping(glob);
598 static int ttm_mem_global_alloc_zone(struct ttm_mem_global *glob,
599 struct ttm_mem_zone *single_zone,
601 struct ttm_operation_ctx *ctx)
603 int count = TTM_MEMORY_ALLOC_RETRIES;
605 while (unlikely(ttm_mem_global_reserve(glob,
609 if (ctx->no_wait_gpu)
611 if (unlikely(count-- == 0))
613 ttm_shrink(glob, false, memory + (memory >> 2) + 16, ctx);
619 int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
620 struct ttm_operation_ctx *ctx)
623 * Normal allocations of kernel memory are registered in
627 return ttm_mem_global_alloc_zone(glob, glob->zone_kernel, memory, ctx);
629 EXPORT_SYMBOL(ttm_mem_global_alloc);
631 int ttm_mem_global_alloc_page(struct ttm_mem_global *glob,
632 struct page *page, uint64_t size,
633 struct ttm_operation_ctx *ctx)
635 struct ttm_mem_zone *zone = NULL;
638 * Page allocations may be registed in a single zone
639 * only if highmem or !dma32.
642 #ifdef CONFIG_HIGHMEM
643 if (PageHighMem(page) && glob->zone_highmem != NULL)
644 zone = glob->zone_highmem;
646 if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL)
647 zone = glob->zone_kernel;
649 return ttm_mem_global_alloc_zone(glob, zone, size, ctx);
652 void ttm_mem_global_free_page(struct ttm_mem_global *glob, struct page *page,
655 struct ttm_mem_zone *zone = NULL;
657 #ifdef CONFIG_HIGHMEM
658 if (PageHighMem(page) && glob->zone_highmem != NULL)
659 zone = glob->zone_highmem;
661 if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL)
662 zone = glob->zone_kernel;
664 ttm_mem_global_free_zone(glob, zone, size);
667 size_t ttm_round_pot(size_t size)
669 if ((size & (size - 1)) == 0)
671 else if (size > PAGE_SIZE)
672 return PAGE_ALIGN(size);
676 while (tmp_size < size)
683 EXPORT_SYMBOL(ttm_round_pot);