From 3966efc2e58f1802411f44fd00967dde448f278d Mon Sep 17 00:00:00 2001 From: Alex Waterman Date: Wed, 11 Jan 2017 16:58:14 -0800 Subject: gpu: nvgpu: Give nvgpu_kalloc a less generic name Change nvgpu_kalloc() to nvgpu_big_[mz]alloc(). This is necessary since the natural free function name for this is nvgpu_kfree() but that conflicts with nvgpu_k[mz]alloc() (implemented in a subsequent patch). This API exists becasue not all allocation sizes can be determined at compile time and in some cases sizes may vary across the system page size. Thus always using kmalloc() could lead to OOM errors due to fragmentation. But always using vmalloc() is wastful of memory for small allocations. This API tries to alleviate those problems. Bug 1799159 Bug 1823380 Change-Id: I49ec5292ce13bcdecf112afbb4a0cfffeeb5ecfc Signed-off-by: Alex Waterman Reviewed-on: http://git-master/r/1283827 Reviewed-by: mobile promotions Tested-by: mobile promotions --- drivers/gpu/nvgpu/include/nvgpu/kmem.h | 82 ++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) (limited to 'drivers/gpu/nvgpu/include') diff --git a/drivers/gpu/nvgpu/include/nvgpu/kmem.h b/drivers/gpu/nvgpu/include/nvgpu/kmem.h index 3d983e77..c08e40a6 100644 --- a/drivers/gpu/nvgpu/include/nvgpu/kmem.h +++ b/drivers/gpu/nvgpu/include/nvgpu/kmem.h @@ -17,6 +17,12 @@ #ifndef NVGPU_KMEM_H #define NVGPU_KMEM_H +#include +#include +#include + +#include + struct gk20a; /* @@ -37,4 +43,80 @@ void nvgpu_kmem_cache_destroy(struct nvgpu_kmem_cache *cache); void *nvgpu_kmem_cache_alloc(struct nvgpu_kmem_cache *cache); void nvgpu_kmem_cache_free(struct nvgpu_kmem_cache *cache, void *ptr); +static inline void *__nvgpu_big_alloc(size_t size, bool clear) +{ + void *p; + + if (size > PAGE_SIZE) { + if (clear) + p = vzalloc(size); + else + p = vmalloc(size); + } else { + if (clear) + p = kzalloc(size, GFP_KERNEL); + else + p = kmalloc(size, GFP_KERNEL); + } + + return p; +} + +/** + * nvgpu_big_malloc - Pick virtual or physical alloc based on @size + * + * @size - Size of the allocation. + * + * On some platforms (i.e Linux) it is possible to allocate memory directly + * mapped into the kernel's address space (kmalloc) or allocate discontiguous + * pages which are then mapped into a special kernel address range. Each type + * of allocation has pros and cons. kmalloc() for instance lets you allocate + * small buffers more space efficiently but vmalloc() allows you to successfully + * allocate much larger buffers without worrying about fragmentation as much + * (but will allocate in multiples of page size). + * + * This function aims to provide the right allocation for when buffers are of + * variable size. In some cases the code doesn't know ahead of time if the + * buffer is going to be big or small so this does the check for you and + * provides the right type of memory allocation. + * + * Returns a pointer to a virtual address range that the kernel can access or + * %NULL on failure. + */ +static inline void *nvgpu_big_malloc(size_t size) +{ + return __nvgpu_big_alloc(size, false); +} + +/** + * nvgpu_big_malloc - Pick virtual or physical alloc based on @size + * + * @size - Size of the allocation. + * + * Zeroed memory version of nvgpu_big_malloc(). + */ +static inline void *nvgpu_big_zalloc(size_t size) +{ + return __nvgpu_big_alloc(size, true); +} + +/** + * nvgpu_big_free - Free and alloc from nvgpu_big_zalloc() or + * nvgpu_big_malloc(). + * + * @p - A pointer allocated by nvgpu_big_zalloc() or nvgpu_big_malloc(). + */ +static inline void nvgpu_big_free(void *p) +{ + /* + * This will have to be fixed eventually. Allocs that use + * nvgpu_big_[mz]alloc() will need to remember the size of the alloc + * when freeing. + */ + if (virt_addr_valid(p)) + kfree(p); + else + vfree(p); +} + #endif -- cgit v1.2.2