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