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 <linux/spinlock.h>
32 #include <linux/sched.h>
33 #include <linux/wait.h>
35 #include <linux/module.h>
36 #include <linux/slab.h>
37 #include <linux/swap.h>
39 #include <drm/drm_device.h>
40 #include <drm/drm_file.h>
41 #include <drm/ttm/ttm_device.h>
43 #include "ttm_memory.h"
45 #define TTM_MEMORY_ALLOC_RETRIES 4
47 struct ttm_mem_global ttm_mem_glob;
48 EXPORT_SYMBOL(ttm_mem_glob);
52 struct ttm_mem_global *glob;
61 static struct attribute ttm_mem_sys = {
62 .name = "zone_memory",
65 static struct attribute ttm_mem_emer = {
66 .name = "emergency_memory",
67 .mode = S_IRUGO | S_IWUSR
69 static struct attribute ttm_mem_max = {
70 .name = "available_memory",
71 .mode = S_IRUGO | S_IWUSR
73 static struct attribute ttm_mem_swap = {
75 .mode = S_IRUGO | S_IWUSR
77 static struct attribute ttm_mem_used = {
78 .name = "used_memory",
82 static void ttm_mem_zone_kobj_release(struct kobject *kobj)
84 struct ttm_mem_zone *zone =
85 container_of(kobj, struct ttm_mem_zone, kobj);
87 pr_info("Zone %7s: Used memory at exit: %llu KiB\n",
88 zone->name, (unsigned long long)zone->used_mem >> 10);
92 static ssize_t ttm_mem_zone_show(struct kobject *kobj,
93 struct attribute *attr,
96 struct ttm_mem_zone *zone =
97 container_of(kobj, struct ttm_mem_zone, kobj);
100 spin_lock(&zone->glob->lock);
101 if (attr == &ttm_mem_sys)
102 val = zone->zone_mem;
103 else if (attr == &ttm_mem_emer)
104 val = zone->emer_mem;
105 else if (attr == &ttm_mem_max)
107 else if (attr == &ttm_mem_swap)
108 val = zone->swap_limit;
109 else if (attr == &ttm_mem_used)
110 val = zone->used_mem;
111 spin_unlock(&zone->glob->lock);
113 return snprintf(buffer, PAGE_SIZE, "%llu\n",
114 (unsigned long long) val >> 10);
117 static void ttm_check_swapping(struct ttm_mem_global *glob);
119 static ssize_t ttm_mem_zone_store(struct kobject *kobj,
120 struct attribute *attr,
124 struct ttm_mem_zone *zone =
125 container_of(kobj, struct ttm_mem_zone, kobj);
130 chars = sscanf(buffer, "%lu", &val);
137 spin_lock(&zone->glob->lock);
138 if (val64 > zone->zone_mem)
139 val64 = zone->zone_mem;
140 if (attr == &ttm_mem_emer) {
141 zone->emer_mem = val64;
142 if (zone->max_mem > val64)
143 zone->max_mem = val64;
144 } else if (attr == &ttm_mem_max) {
145 zone->max_mem = val64;
146 if (zone->emer_mem < val64)
147 zone->emer_mem = val64;
148 } else if (attr == &ttm_mem_swap)
149 zone->swap_limit = val64;
150 spin_unlock(&zone->glob->lock);
152 ttm_check_swapping(zone->glob);
157 static struct attribute *ttm_mem_zone_attrs[] = {
166 static const struct sysfs_ops ttm_mem_zone_ops = {
167 .show = &ttm_mem_zone_show,
168 .store = &ttm_mem_zone_store
171 static struct kobj_type ttm_mem_zone_kobj_type = {
172 .release = &ttm_mem_zone_kobj_release,
173 .sysfs_ops = &ttm_mem_zone_ops,
174 .default_attrs = ttm_mem_zone_attrs,
177 static struct attribute ttm_mem_global_lower_mem_limit = {
178 .name = "lower_mem_limit",
179 .mode = S_IRUGO | S_IWUSR
182 static ssize_t ttm_mem_global_show(struct kobject *kobj,
183 struct attribute *attr,
186 struct ttm_mem_global *glob =
187 container_of(kobj, struct ttm_mem_global, kobj);
190 spin_lock(&glob->lock);
191 val = glob->lower_mem_limit;
192 spin_unlock(&glob->lock);
193 /* convert from number of pages to KB */
194 val <<= (PAGE_SHIFT - 10);
195 return snprintf(buffer, PAGE_SIZE, "%llu\n",
196 (unsigned long long) val);
199 static ssize_t ttm_mem_global_store(struct kobject *kobj,
200 struct attribute *attr,
207 struct ttm_mem_global *glob =
208 container_of(kobj, struct ttm_mem_global, kobj);
210 chars = sscanf(buffer, "%lu", &val);
215 /* convert from KB to number of pages */
216 val64 >>= (PAGE_SHIFT - 10);
218 spin_lock(&glob->lock);
219 glob->lower_mem_limit = val64;
220 spin_unlock(&glob->lock);
225 static struct attribute *ttm_mem_global_attrs[] = {
226 &ttm_mem_global_lower_mem_limit,
230 static const struct sysfs_ops ttm_mem_global_ops = {
231 .show = &ttm_mem_global_show,
232 .store = &ttm_mem_global_store,
235 static struct kobj_type ttm_mem_glob_kobj_type = {
236 .sysfs_ops = &ttm_mem_global_ops,
237 .default_attrs = ttm_mem_global_attrs,
240 static bool ttm_zones_above_swap_target(struct ttm_mem_global *glob,
241 bool from_wq, uint64_t extra)
244 struct ttm_mem_zone *zone;
247 for (i = 0; i < glob->num_zones; ++i) {
248 zone = glob->zones[i];
251 target = zone->swap_limit;
252 else if (capable(CAP_SYS_ADMIN))
253 target = zone->emer_mem;
255 target = zone->max_mem;
257 target = (extra > target) ? 0ULL : target;
259 if (zone->used_mem > target)
266 * At this point we only support a single shrink callback.
267 * Extend this if needed, perhaps using a linked list of callbacks.
268 * Note that this function is reentrant:
269 * many threads may try to swap out at any given time.
272 static void ttm_shrink(struct ttm_mem_global *glob, bool from_wq,
273 uint64_t extra, struct ttm_operation_ctx *ctx)
277 spin_lock(&glob->lock);
279 while (ttm_zones_above_swap_target(glob, from_wq, extra)) {
280 spin_unlock(&glob->lock);
281 ret = ttm_global_swapout(ctx, GFP_KERNEL);
282 spin_lock(&glob->lock);
283 if (unlikely(ret < 0))
287 spin_unlock(&glob->lock);
290 static void ttm_shrink_work(struct work_struct *work)
292 struct ttm_operation_ctx ctx = {
293 .interruptible = false,
296 struct ttm_mem_global *glob =
297 container_of(work, struct ttm_mem_global, work);
299 ttm_shrink(glob, true, 0ULL, &ctx);
302 static int ttm_mem_init_kernel_zone(struct ttm_mem_global *glob,
303 const struct sysinfo *si)
305 struct ttm_mem_zone *zone = kzalloc(sizeof(*zone), GFP_KERNEL);
312 mem = si->totalram - si->totalhigh;
315 zone->name = "kernel";
316 zone->zone_mem = mem;
317 zone->max_mem = mem >> 1;
318 zone->emer_mem = (mem >> 1) + (mem >> 2);
319 zone->swap_limit = zone->max_mem - (mem >> 3);
322 glob->zone_kernel = zone;
323 ret = kobject_init_and_add(
324 &zone->kobj, &ttm_mem_zone_kobj_type, &glob->kobj, zone->name);
325 if (unlikely(ret != 0)) {
326 kobject_put(&zone->kobj);
329 glob->zones[glob->num_zones++] = zone;
333 #ifdef CONFIG_HIGHMEM
334 static int ttm_mem_init_highmem_zone(struct ttm_mem_global *glob,
335 const struct sysinfo *si)
337 struct ttm_mem_zone *zone;
341 if (si->totalhigh == 0)
344 zone = kzalloc(sizeof(*zone), GFP_KERNEL);
351 zone->name = "highmem";
352 zone->zone_mem = mem;
353 zone->max_mem = mem >> 1;
354 zone->emer_mem = (mem >> 1) + (mem >> 2);
355 zone->swap_limit = zone->max_mem - (mem >> 3);
358 glob->zone_highmem = zone;
359 ret = kobject_init_and_add(
360 &zone->kobj, &ttm_mem_zone_kobj_type, &glob->kobj, "%s",
362 if (unlikely(ret != 0)) {
363 kobject_put(&zone->kobj);
366 glob->zones[glob->num_zones++] = zone;
370 static int ttm_mem_init_dma32_zone(struct ttm_mem_global *glob,
371 const struct sysinfo *si)
373 struct ttm_mem_zone *zone = kzalloc(sizeof(*zone), GFP_KERNEL);
384 * No special dma32 zone needed.
387 if (mem <= ((uint64_t) 1ULL << 32)) {
393 * Limit max dma32 memory to 4GB for now
394 * until we can figure out how big this
398 mem = ((uint64_t) 1ULL << 32);
399 zone->name = "dma32";
400 zone->zone_mem = mem;
401 zone->max_mem = mem >> 1;
402 zone->emer_mem = (mem >> 1) + (mem >> 2);
403 zone->swap_limit = zone->max_mem - (mem >> 3);
406 glob->zone_dma32 = zone;
407 ret = kobject_init_and_add(
408 &zone->kobj, &ttm_mem_zone_kobj_type, &glob->kobj, zone->name);
409 if (unlikely(ret != 0)) {
410 kobject_put(&zone->kobj);
413 glob->zones[glob->num_zones++] = zone;
418 int ttm_mem_global_init(struct ttm_mem_global *glob, struct device *dev)
423 struct ttm_mem_zone *zone;
425 spin_lock_init(&glob->lock);
426 glob->swap_queue = create_singlethread_workqueue("ttm_swap");
427 INIT_WORK(&glob->work, ttm_shrink_work);
429 ret = kobject_init_and_add(&glob->kobj, &ttm_mem_glob_kobj_type,
430 &dev->kobj, "memory_accounting");
431 if (unlikely(ret != 0)) {
432 kobject_put(&glob->kobj);
438 /* set it as 0 by default to keep original behavior of OOM */
439 glob->lower_mem_limit = 0;
441 ret = ttm_mem_init_kernel_zone(glob, &si);
442 if (unlikely(ret != 0))
444 #ifdef CONFIG_HIGHMEM
445 ret = ttm_mem_init_highmem_zone(glob, &si);
446 if (unlikely(ret != 0))
449 ret = ttm_mem_init_dma32_zone(glob, &si);
450 if (unlikely(ret != 0))
453 for (i = 0; i < glob->num_zones; ++i) {
454 zone = glob->zones[i];
455 pr_info("Zone %7s: Available graphics memory: %llu KiB\n",
456 zone->name, (unsigned long long)zone->max_mem >> 10);
460 ttm_mem_global_release(glob);
464 void ttm_mem_global_release(struct ttm_mem_global *glob)
466 struct ttm_mem_zone *zone;
469 flush_workqueue(glob->swap_queue);
470 destroy_workqueue(glob->swap_queue);
471 glob->swap_queue = NULL;
472 for (i = 0; i < glob->num_zones; ++i) {
473 zone = glob->zones[i];
474 kobject_del(&zone->kobj);
475 kobject_put(&zone->kobj);
477 kobject_del(&glob->kobj);
478 kobject_put(&glob->kobj);
479 memset(glob, 0, sizeof(*glob));
482 static void ttm_check_swapping(struct ttm_mem_global *glob)
484 bool needs_swapping = false;
486 struct ttm_mem_zone *zone;
488 spin_lock(&glob->lock);
489 for (i = 0; i < glob->num_zones; ++i) {
490 zone = glob->zones[i];
491 if (zone->used_mem > zone->swap_limit) {
492 needs_swapping = true;
497 spin_unlock(&glob->lock);
499 if (unlikely(needs_swapping))
500 (void)queue_work(glob->swap_queue, &glob->work);
504 static void ttm_mem_global_free_zone(struct ttm_mem_global *glob,
505 struct ttm_mem_zone *single_zone,
509 struct ttm_mem_zone *zone;
511 spin_lock(&glob->lock);
512 for (i = 0; i < glob->num_zones; ++i) {
513 zone = glob->zones[i];
514 if (single_zone && zone != single_zone)
516 zone->used_mem -= amount;
518 spin_unlock(&glob->lock);
521 void ttm_mem_global_free(struct ttm_mem_global *glob,
524 return ttm_mem_global_free_zone(glob, glob->zone_kernel, amount);
526 EXPORT_SYMBOL(ttm_mem_global_free);
529 * check if the available mem is under lower memory limit
531 * a. if no swap disk at all or free swap space is under swap_mem_limit
532 * but available system mem is bigger than sys_mem_limit, allow TTM
535 * b. if the available system mem is less than sys_mem_limit but free
536 * swap disk is bigger than swap_mem_limit, allow TTM allocation.
539 ttm_check_under_lowerlimit(struct ttm_mem_global *glob,
541 struct ttm_operation_ctx *ctx)
545 /* We allow over commit during suspend */
546 if (ctx->force_alloc)
549 available = get_nr_swap_pages() + si_mem_available();
550 available -= num_pages;
551 if (available < glob->lower_mem_limit)
557 static int ttm_mem_global_reserve(struct ttm_mem_global *glob,
558 struct ttm_mem_zone *single_zone,
559 uint64_t amount, bool reserve)
564 struct ttm_mem_zone *zone;
566 spin_lock(&glob->lock);
567 for (i = 0; i < glob->num_zones; ++i) {
568 zone = glob->zones[i];
569 if (single_zone && zone != single_zone)
572 limit = (capable(CAP_SYS_ADMIN)) ?
573 zone->emer_mem : zone->max_mem;
575 if (zone->used_mem > limit)
580 for (i = 0; i < glob->num_zones; ++i) {
581 zone = glob->zones[i];
582 if (single_zone && zone != single_zone)
584 zone->used_mem += amount;
590 spin_unlock(&glob->lock);
591 ttm_check_swapping(glob);
597 static int ttm_mem_global_alloc_zone(struct ttm_mem_global *glob,
598 struct ttm_mem_zone *single_zone,
600 struct ttm_operation_ctx *ctx)
602 int count = TTM_MEMORY_ALLOC_RETRIES;
604 while (unlikely(ttm_mem_global_reserve(glob,
608 if (ctx->no_wait_gpu)
610 if (unlikely(count-- == 0))
612 ttm_shrink(glob, false, memory + (memory >> 2) + 16, ctx);
618 int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
619 struct ttm_operation_ctx *ctx)
622 * Normal allocations of kernel memory are registered in
626 return ttm_mem_global_alloc_zone(glob, glob->zone_kernel, memory, ctx);
628 EXPORT_SYMBOL(ttm_mem_global_alloc);
630 int ttm_mem_global_alloc_page(struct ttm_mem_global *glob,
631 struct page *page, uint64_t size,
632 struct ttm_operation_ctx *ctx)
634 struct ttm_mem_zone *zone = NULL;
637 * Page allocations may be registed in a single zone
638 * only if highmem or !dma32.
641 #ifdef CONFIG_HIGHMEM
642 if (PageHighMem(page) && glob->zone_highmem != NULL)
643 zone = glob->zone_highmem;
645 if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL)
646 zone = glob->zone_kernel;
648 return ttm_mem_global_alloc_zone(glob, zone, size, ctx);
651 void ttm_mem_global_free_page(struct ttm_mem_global *glob, struct page *page,
654 struct ttm_mem_zone *zone = NULL;
656 #ifdef CONFIG_HIGHMEM
657 if (PageHighMem(page) && glob->zone_highmem != NULL)
658 zone = glob->zone_highmem;
660 if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL)
661 zone = glob->zone_kernel;
663 ttm_mem_global_free_zone(glob, zone, size);
666 size_t ttm_round_pot(size_t size)
668 if ((size & (size - 1)) == 0)
670 else if (size > PAGE_SIZE)
671 return PAGE_ALIGN(size);
675 while (tmp_size < size)
682 EXPORT_SYMBOL(ttm_round_pot);