From cf0ef133e6e3075c808bb3f553b35e94fc000afd Mon Sep 17 00:00:00 2001 From: Alex Waterman Date: Tue, 4 Oct 2016 14:04:01 -0700 Subject: gpu: nvgpu: Move kmem_caches to allocator Instead of using a single static kmem_cache for each type of data structure the allocators may want to allocate each allocator now has its own instance of the kmem_cache. This is done so that each GPU driver instance can accurately track how much memory it is using. In order to support this on older kernels a new NVGPU API has been made, nvgpu_kmem_cache_create(struct gk20a *g, size_t size) To handle the possibility that caches cannot be created with the same name. This patch also fixes numerous places where kfree() was wrongly used to free kmem_cache allocs. Bug 1799159 Bug 1823380 Change-Id: Id674f9a5445fde3f95db65ad6bf3ea990444603d Signed-off-by: Alex Waterman Reviewed-on: http://git-master/r/1283826 Reviewed-by: svccoveritychecker Reviewed-by: Terje Bergstrom GVS: Gerrit_Virtual_Submit --- drivers/gpu/nvgpu/common/linux/kmem.c | 83 +++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 drivers/gpu/nvgpu/common/linux/kmem.c (limited to 'drivers/gpu/nvgpu/common/linux') diff --git a/drivers/gpu/nvgpu/common/linux/kmem.c b/drivers/gpu/nvgpu/common/linux/kmem.c new file mode 100644 index 00000000..24e0ca5d --- /dev/null +++ b/drivers/gpu/nvgpu/common/linux/kmem.c @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include + +#include + +/* + * Statically declared because this needs to be shared across all nvgpu driver + * instances. This makes sure that all kmem caches are _definitely_ uniquely + * named. + */ +static atomic_t kmem_cache_id; + +/* + * Linux specific version of the nvgpu_kmem_cache struct. This type is + * completely opaque to the rest of the driver. + */ +struct nvgpu_kmem_cache { + struct gk20a *g; + struct kmem_cache *cache; + + /* + * Memory to hold the kmem_cache unique name. Only necessary on our + * k3.10 kernel when not using the SLUB allocator but it's easier to + * just carry this on to newer kernels. + */ + char name[128]; +}; + +struct nvgpu_kmem_cache *nvgpu_kmem_cache_create(struct gk20a *g, size_t size) +{ + struct nvgpu_kmem_cache *cache = + kzalloc(sizeof(struct nvgpu_kmem_cache), GFP_KERNEL); + + if (!cache) + return NULL; + + cache->g = g; + + snprintf(cache->name, sizeof(cache->name), + "nvgpu-cache-0x%p-%d-%d", g, (int)size, + atomic_inc_return(&kmem_cache_id)); + cache->cache = kmem_cache_create(cache->name, + size, size, 0, NULL); + if (!cache->cache) { + kfree(cache); + return NULL; + } + + return cache; +} + +void nvgpu_kmem_cache_destroy(struct nvgpu_kmem_cache *cache) +{ + kmem_cache_destroy(cache->cache); + kfree(cache); +} + +void *nvgpu_kmem_cache_alloc(struct nvgpu_kmem_cache *cache) +{ + return kmem_cache_alloc(cache->cache, GFP_KERNEL); +} + +void nvgpu_kmem_cache_free(struct nvgpu_kmem_cache *cache, void *ptr) +{ + kmem_cache_free(cache->cache, ptr); +} -- cgit v1.2.2