diff options
| author | Joshua Bakita <bakitajoshua@gmail.com> | 2023-06-28 18:24:25 -0400 |
|---|---|---|
| committer | Joshua Bakita <bakitajoshua@gmail.com> | 2023-06-28 18:24:25 -0400 |
| commit | 01e6fac4d61fdd7fff5433942ec93fc2ea1e4df1 (patch) | |
| tree | 4ef34501728a087be24f4ba0af90f91486bf780b /include/os/linux/kmem.c | |
| parent | 306a03d18b305e4e573be3b2931978fa10679eb9 (diff) | |
Include nvgpu headers
These are needed to build on NVIDIA's Jetson boards for the time
being. Only a couple structs are required, so it should be fairly
easy to remove this dependency at some point in the future.
Diffstat (limited to 'include/os/linux/kmem.c')
| -rw-r--r-- | include/os/linux/kmem.c | 653 |
1 files changed, 653 insertions, 0 deletions
diff --git a/include/os/linux/kmem.c b/include/os/linux/kmem.c new file mode 100644 index 0000000..395cc45 --- /dev/null +++ b/include/os/linux/kmem.c | |||
| @@ -0,0 +1,653 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 2017-2018, NVIDIA CORPORATION. All rights reserved. | ||
| 3 | * | ||
| 4 | * This program is free software; you can redistribute it and/or modify it | ||
| 5 | * under the terms and conditions of the GNU General Public License, | ||
| 6 | * version 2, as published by the Free Software Foundation. | ||
| 7 | * | ||
| 8 | * This program is distributed in the hope it will be useful, but WITHOUT | ||
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
| 11 | * more details. | ||
| 12 | * | ||
| 13 | * You should have received a copy of the GNU General Public License | ||
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 15 | */ | ||
| 16 | |||
| 17 | #include <linux/mm.h> | ||
| 18 | #include <linux/slab.h> | ||
| 19 | #include <linux/debugfs.h> | ||
| 20 | #include <linux/seq_file.h> | ||
| 21 | #include <linux/vmalloc.h> | ||
| 22 | #include <linux/stacktrace.h> | ||
| 23 | |||
| 24 | #include <nvgpu/lock.h> | ||
| 25 | #include <nvgpu/kmem.h> | ||
| 26 | #include <nvgpu/atomic.h> | ||
| 27 | #include <nvgpu/bug.h> | ||
| 28 | #include <nvgpu/gk20a.h> | ||
| 29 | |||
| 30 | #include "kmem_priv.h" | ||
| 31 | |||
| 32 | /* | ||
| 33 | * Statically declared because this needs to be shared across all nvgpu driver | ||
| 34 | * instances. This makes sure that all kmem caches are _definitely_ uniquely | ||
| 35 | * named. | ||
| 36 | */ | ||
| 37 | static atomic_t kmem_cache_id; | ||
| 38 | |||
| 39 | void *__nvgpu_big_alloc(struct gk20a *g, size_t size, bool clear) | ||
| 40 | { | ||
| 41 | void *p; | ||
| 42 | |||
| 43 | if (size > PAGE_SIZE) { | ||
| 44 | if (clear) | ||
| 45 | p = nvgpu_vzalloc(g, size); | ||
| 46 | else | ||
| 47 | p = nvgpu_vmalloc(g, size); | ||
| 48 | } else { | ||
| 49 | if (clear) | ||
| 50 | p = nvgpu_kzalloc(g, size); | ||
| 51 | else | ||
| 52 | p = nvgpu_kmalloc(g, size); | ||
| 53 | } | ||
| 54 | |||
| 55 | return p; | ||
| 56 | } | ||
| 57 | |||
| 58 | void nvgpu_big_free(struct gk20a *g, void *p) | ||
| 59 | { | ||
| 60 | /* | ||
| 61 | * This will have to be fixed eventually. Allocs that use | ||
| 62 | * nvgpu_big_[mz]alloc() will need to remember the size of the alloc | ||
| 63 | * when freeing. | ||
| 64 | */ | ||
| 65 | if (is_vmalloc_addr(p)) | ||
| 66 | nvgpu_vfree(g, p); | ||
| 67 | else | ||
| 68 | nvgpu_kfree(g, p); | ||
| 69 | } | ||
| 70 | |||
| 71 | void *__nvgpu_kmalloc(struct gk20a *g, size_t size, void *ip) | ||
| 72 | { | ||
| 73 | void *alloc; | ||
| 74 | |||
| 75 | #ifdef CONFIG_NVGPU_TRACK_MEM_USAGE | ||
| 76 | alloc = __nvgpu_track_kmalloc(g, size, ip); | ||
| 77 | #else | ||
| 78 | alloc = kmalloc(size, GFP_KERNEL); | ||
| 79 | #endif | ||
| 80 | |||
| 81 | kmem_dbg(g, "kmalloc: size=%-6ld addr=0x%p gfp=0x%08x", | ||
| 82 | size, alloc, GFP_KERNEL); | ||
| 83 | |||
| 84 | return alloc; | ||
| 85 | } | ||
| 86 | |||
| 87 | void *__nvgpu_kzalloc(struct gk20a *g, size_t size, void *ip) | ||
| 88 | { | ||
| 89 | void *alloc; | ||
| 90 | |||
| 91 | #ifdef CONFIG_NVGPU_TRACK_MEM_USAGE | ||
| 92 | alloc = __nvgpu_track_kzalloc(g, size, ip); | ||
| 93 | #else | ||
| 94 | alloc = kzalloc(size, GFP_KERNEL); | ||
| 95 | #endif | ||
| 96 | |||
| 97 | kmem_dbg(g, "kzalloc: size=%-6ld addr=0x%p gfp=0x%08x", | ||
| 98 | size, alloc, GFP_KERNEL); | ||
| 99 | |||
| 100 | return alloc; | ||
| 101 | } | ||
| 102 | |||
| 103 | void *__nvgpu_kcalloc(struct gk20a *g, size_t n, size_t size, void *ip) | ||
| 104 | { | ||
| 105 | void *alloc; | ||
| 106 | |||
| 107 | #ifdef CONFIG_NVGPU_TRACK_MEM_USAGE | ||
| 108 | alloc = __nvgpu_track_kcalloc(g, n, size, ip); | ||
| 109 | #else | ||
| 110 | alloc = kcalloc(n, size, GFP_KERNEL); | ||
| 111 | #endif | ||
| 112 | |||
| 113 | kmem_dbg(g, "kcalloc: size=%-6ld addr=0x%p gfp=0x%08x", | ||
| 114 | n * size, alloc, GFP_KERNEL); | ||
| 115 | |||
| 116 | return alloc; | ||
| 117 | } | ||
| 118 | |||
| 119 | void *__nvgpu_vmalloc(struct gk20a *g, unsigned long size, void *ip) | ||
| 120 | { | ||
| 121 | void *alloc; | ||
| 122 | |||
| 123 | #ifdef CONFIG_NVGPU_TRACK_MEM_USAGE | ||
| 124 | alloc = __nvgpu_track_vmalloc(g, size, ip); | ||
| 125 | #else | ||
| 126 | alloc = vmalloc(size); | ||
| 127 | #endif | ||
| 128 | |||
| 129 | kmem_dbg(g, "vmalloc: size=%-6ld addr=0x%p", size, alloc); | ||
| 130 | |||
| 131 | return alloc; | ||
| 132 | } | ||
| 133 | |||
| 134 | void *__nvgpu_vzalloc(struct gk20a *g, unsigned long size, void *ip) | ||
| 135 | { | ||
| 136 | void *alloc; | ||
| 137 | |||
| 138 | #ifdef CONFIG_NVGPU_TRACK_MEM_USAGE | ||
| 139 | alloc = __nvgpu_track_vzalloc(g, size, ip); | ||
| 140 | #else | ||
| 141 | alloc = vzalloc(size); | ||
| 142 | #endif | ||
| 143 | |||
| 144 | kmem_dbg(g, "vzalloc: size=%-6ld addr=0x%p", size, alloc); | ||
| 145 | |||
| 146 | return alloc; | ||
| 147 | } | ||
| 148 | |||
| 149 | void __nvgpu_kfree(struct gk20a *g, void *addr) | ||
| 150 | { | ||
| 151 | kmem_dbg(g, "kfree: addr=0x%p", addr); | ||
| 152 | #ifdef CONFIG_NVGPU_TRACK_MEM_USAGE | ||
| 153 | __nvgpu_track_kfree(g, addr); | ||
| 154 | #else | ||
| 155 | kfree(addr); | ||
| 156 | #endif | ||
| 157 | } | ||
| 158 | |||
| 159 | void __nvgpu_vfree(struct gk20a *g, void *addr) | ||
| 160 | { | ||
| 161 | kmem_dbg(g, "vfree: addr=0x%p", addr); | ||
| 162 | #ifdef CONFIG_NVGPU_TRACK_MEM_USAGE | ||
| 163 | __nvgpu_track_vfree(g, addr); | ||
| 164 | #else | ||
| 165 | vfree(addr); | ||
| 166 | #endif | ||
| 167 | } | ||
| 168 | |||
| 169 | #ifdef CONFIG_NVGPU_TRACK_MEM_USAGE | ||
| 170 | |||
| 171 | void nvgpu_lock_tracker(struct nvgpu_mem_alloc_tracker *tracker) | ||
| 172 | { | ||
| 173 | nvgpu_mutex_acquire(&tracker->lock); | ||
| 174 | } | ||
| 175 | |||
| 176 | void nvgpu_unlock_tracker(struct nvgpu_mem_alloc_tracker *tracker) | ||
| 177 | { | ||
| 178 | nvgpu_mutex_release(&tracker->lock); | ||
| 179 | } | ||
| 180 | |||
| 181 | void kmem_print_mem_alloc(struct gk20a *g, | ||
| 182 | struct nvgpu_mem_alloc *alloc, | ||
| 183 | struct seq_file *s) | ||
| 184 | { | ||
| 185 | #ifdef __NVGPU_SAVE_KALLOC_STACK_TRACES | ||
| 186 | int i; | ||
| 187 | |||
| 188 | __pstat(s, "nvgpu-alloc: addr=0x%llx size=%ld\n", | ||
| 189 | alloc->addr, alloc->size); | ||
| 190 | for (i = 0; i < alloc->stack_length; i++) | ||
| 191 | __pstat(s, " %3d [<%p>] %pS\n", i, | ||
| 192 | (void *)alloc->stack[i], | ||
| 193 | (void *)alloc->stack[i]); | ||
| 194 | __pstat(s, "\n"); | ||
| 195 | #else | ||
| 196 | __pstat(s, "nvgpu-alloc: addr=0x%llx size=%ld src=%pF\n", | ||
| 197 | alloc->addr, alloc->size, alloc->ip); | ||
| 198 | #endif | ||
| 199 | } | ||
| 200 | |||
| 201 | static int nvgpu_add_alloc(struct nvgpu_mem_alloc_tracker *tracker, | ||
| 202 | struct nvgpu_mem_alloc *alloc) | ||
| 203 | { | ||
| 204 | alloc->allocs_entry.key_start = alloc->addr; | ||
| 205 | alloc->allocs_entry.key_end = alloc->addr + alloc->size; | ||
| 206 | |||
| 207 | nvgpu_rbtree_insert(&alloc->allocs_entry, &tracker->allocs); | ||
| 208 | return 0; | ||
| 209 | } | ||
| 210 | |||
| 211 | static struct nvgpu_mem_alloc *nvgpu_rem_alloc( | ||
| 212 | struct nvgpu_mem_alloc_tracker *tracker, u64 alloc_addr) | ||
| 213 | { | ||
| 214 | struct nvgpu_mem_alloc *alloc; | ||
| 215 | struct nvgpu_rbtree_node *node = NULL; | ||
| 216 | |||
| 217 | nvgpu_rbtree_search(alloc_addr, &node, tracker->allocs); | ||
| 218 | if (!node) | ||
| 219 | return NULL; | ||
| 220 | |||
| 221 | alloc = nvgpu_mem_alloc_from_rbtree_node(node); | ||
| 222 | |||
| 223 | nvgpu_rbtree_unlink(node, &tracker->allocs); | ||
| 224 | |||
| 225 | return alloc; | ||
| 226 | } | ||
