summaryrefslogtreecommitdiffstats
path: root/drivers/gpu/nvgpu/include
diff options
context:
space:
mode:
authorAlex Waterman <alexw@nvidia.com>2017-01-11 19:58:14 -0500
committermobile promotions <svcmobile_promotions@nvidia.com>2017-03-03 13:34:43 -0500
commit3966efc2e58f1802411f44fd00967dde448f278d (patch)
treeb6cf822abc638b79acbd12b749a97ab5507a6fe9 /drivers/gpu/nvgpu/include
parent76b78b6fdcb0bbed72645aaa85de6013e2b135c3 (diff)
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 <alexw@nvidia.com> Reviewed-on: http://git-master/r/1283827 Reviewed-by: mobile promotions <svcmobile_promotions@nvidia.com> Tested-by: mobile promotions <svcmobile_promotions@nvidia.com>
Diffstat (limited to 'drivers/gpu/nvgpu/include')
-rw-r--r--drivers/gpu/nvgpu/include/nvgpu/kmem.h82
1 files changed, 82 insertions, 0 deletions
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 @@
17#ifndef NVGPU_KMEM_H 17#ifndef NVGPU_KMEM_H
18#define NVGPU_KMEM_H 18#define NVGPU_KMEM_H
19 19
20#include <linux/mm.h>
21#include <linux/slab.h>
22#include <linux/vmalloc.h>
23
24#include <asm/page.h>
25
20struct gk20a; 26struct gk20a;
21 27
22/* 28/*
@@ -37,4 +43,80 @@ void nvgpu_kmem_cache_destroy(struct nvgpu_kmem_cache *cache);
37void *nvgpu_kmem_cache_alloc(struct nvgpu_kmem_cache *cache); 43void *nvgpu_kmem_cache_alloc(struct nvgpu_kmem_cache *cache);
38void nvgpu_kmem_cache_free(struct nvgpu_kmem_cache *cache, void *ptr); 44void nvgpu_kmem_cache_free(struct nvgpu_kmem_cache *cache, void *ptr);
39 45
46static inline void *__nvgpu_big_alloc(size_t size, bool clear)
47{
48 void *p;
49
50 if (size > PAGE_SIZE) {
51 if (clear)
52 p = vzalloc(size);
53 else
54 p = vmalloc(size);
55 } else {
56 if (clear)
57 p = kzalloc(size, GFP_KERNEL);
58 else
59 p = kmalloc(size, GFP_KERNEL);
60 }
61
62 return p;
63}
64
65/**
66 * nvgpu_big_malloc - Pick virtual or physical alloc based on @size
67 *
68 * @size - Size of the allocation.
69 *
70 * On some platforms (i.e Linux) it is possible to allocate memory directly
71 * mapped into the kernel's address space (kmalloc) or allocate discontiguous
72 * pages which are then mapped into a special kernel address range. Each type
73 * of allocation has pros and cons. kmalloc() for instance lets you allocate
74 * small buffers more space efficiently but vmalloc() allows you to successfully
75 * allocate much larger buffers without worrying about fragmentation as much
76 * (but will allocate in multiples of page size).
77 *
78 * This function aims to provide the right allocation for when buffers are of
79 * variable size. In some cases the code doesn't know ahead of time if the
80 * buffer is going to be big or small so this does the check for you and
81 * provides the right type of memory allocation.
82 *
83 * Returns a pointer to a virtual address range that the kernel can access or
84 * %NULL on failure.
85 */
86static inline void *nvgpu_big_malloc(size_t size)
87{
88 return __nvgpu_big_alloc(size, false);
89}
90
91/**
92 * nvgpu_big_malloc - Pick virtual or physical alloc based on @size
93 *
94 * @size - Size of the allocation.
95 *
96 * Zeroed memory version of nvgpu_big_malloc().
97 */
98static inline void *nvgpu_big_zalloc(size_t size)
99{
100 return __nvgpu_big_alloc(size, true);
101}
102
103/**
104 * nvgpu_big_free - Free and alloc from nvgpu_big_zalloc() or
105 * nvgpu_big_malloc().
106 *
107 * @p - A pointer allocated by nvgpu_big_zalloc() or nvgpu_big_malloc().
108 */
109static inline void nvgpu_big_free(void *p)
110{
111 /*
112 * This will have to be fixed eventually. Allocs that use
113 * nvgpu_big_[mz]alloc() will need to remember the size of the alloc
114 * when freeing.
115 */
116 if (virt_addr_valid(p))
117 kfree(p);
118 else
119 vfree(p);
120}
121
40#endif 122#endif