Skip to content

Commit 4bdb132

Browse files
committed
Export the uvm_vmalloc_hdr_t structure to nv-nanos.h. And modify vfree, get alloc_size from ptr
1 parent d340014 commit 4bdb132

File tree

6 files changed

+27
-26
lines changed

6 files changed

+27
-26
lines changed

kernel-open/common/inc/nv-linux.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ static inline void *nv_vmalloc(unsigned long size)
506506
static inline void nv_vfree(void *ptr, NvU64 size)
507507
{
508508
NV_MEMDBG_REMOVE(ptr, size);
509-
vfree(ptr, size);
509+
vfree(ptr);
510510
}
511511

512512
static inline void *nv_ioremap(NvU64 phys, NvU64 size)

kernel-open/common/inc/nv-nanos.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,18 @@ typedef struct nvidia_event
481481
#define BUILD_BUG_ON(expr) build_assert(!(expr))
482482
#define BUILD_BUG_ON_NOT_POWER_OF_2(expr) build_assert(((expr) & ((expr) - 1)) == 0)
483483

484+
// To implement realloc for vmalloc-based allocations we need to track the size
485+
// of the original allocation. We can do that by allocating a header along with
486+
// the allocation itself. Since vmalloc is only used for relatively large
487+
// allocations, this overhead is very small.
488+
//
489+
// We don't need this for kmalloc since we can use ksize().
490+
typedef struct
491+
{
492+
size_t alloc_size;
493+
uint8_t ptr[0];
494+
} uvm_vmalloc_hdr_t;
495+
484496
#define ZERO_SIZE_PTR pointer_from_u64(16)
485497
#define ZERO_OR_NULL_PTR(p) (u64_from_pointer(p) <= u64_from_pointer(ZERO_SIZE_PTR))
486498

@@ -508,7 +520,12 @@ typedef struct nvidia_event
508520
#define vzalloc(size) kzalloc(size, 0)
509521
#define ksize(p) objcache_from_object(u64_from_pointer(p), PAGESIZE_2M)->pagesize
510522
#define is_vmalloc_addr(p) (objcache_from_object(u64_from_pointer(p), PAGESIZE_2M) == INVALID_ADDRESS)
511-
#define vfree NV_KFREE
523+
#define vfree(p) do { \
524+
uvm_vmalloc_hdr_t *hdr; \
525+
hdr = container_of(p, uvm_vmalloc_hdr_t, ptr); \
526+
NV_KFREE(p, hdr->alloc_size); \
527+
} while (0)
528+
512529

513530
static inline void *kmalloc(unsigned long size, int flags)
514531
{

kernel-open/nvidia-modeset/nvidia-modeset-linux.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ void nvkms_free(void *ptr, size_t size)
260260
if (size <= KMALLOC_LIMIT) {
261261
kfree(ptr);
262262
} else {
263-
vfree(ptr, size);
263+
vfree(ptr);
264264
}
265265
}
266266

kernel-open/nvidia-uvm/nv-kthread-q-selftest.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ static int _multithreaded_q_kthread_function(void *args)
241241

242242
done:
243243
if (q_items)
244-
vfree(q_items, alloc_size);
244+
vfree(q_items);
245245

246246
while (!kthread_should_stop())
247247
schedule();

kernel-open/nvidia-uvm/uvm_kvmalloc.c

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,6 @@
2727
#include "uvm_kvmalloc.h"
2828
#include "uvm_rb_tree.h"
2929

30-
// To implement realloc for vmalloc-based allocations we need to track the size
31-
// of the original allocation. We can do that by allocating a header along with
32-
// the allocation itself. Since vmalloc is only used for relatively large
33-
// allocations, this overhead is very small.
34-
//
35-
// We don't need this for kmalloc since we can use ksize().
36-
typedef struct
37-
{
38-
size_t alloc_size;
39-
uint8_t ptr[0];
40-
} uvm_vmalloc_hdr_t;
41-
4230
typedef struct
4331
{
4432
const char *file;
@@ -297,20 +285,16 @@ void *__uvm_kvmalloc_zero(size_t size, const char *file, int line, const char *f
297285

298286
void uvm_kvfree(void *p)
299287
{
300-
uvm_vmalloc_hdr_t *hdr = NULL;
301-
302288
if (!p)
303289
return;
304290

305291
if (uvm_leak_checker)
306292
alloc_tracking_remove(p);
307293

308-
if (is_vmalloc_addr(p)){
309-
hdr = get_hdr(p);
310-
vfree(hdr, hdr->alloc_size);
311-
} else {
294+
if (is_vmalloc_addr(p))
295+
vfree(get_hdr(p));
296+
else
312297
kfree(p);
313-
}
314298
}
315299

316300
// Handle reallocs of kmalloc-based allocations
@@ -338,7 +322,7 @@ static void *realloc_from_vmalloc(void *p, size_t new_size)
338322
void *new_p;
339323

340324
if (new_size == 0) {
341-
vfree(old_hdr, old_hdr->alloc_size);
325+
vfree(old_hdr);
342326
return ZERO_SIZE_PTR; // What krealloc returns for this case
343327
}
344328

@@ -352,7 +336,7 @@ static void *realloc_from_vmalloc(void *p, size_t new_size)
352336
return NULL;
353337

354338
memcpy(new_p, p, min(new_size, old_hdr->alloc_size));
355-
vfree(old_hdr, old_hdr->alloc_size);
339+
vfree(old_hdr);
356340
return new_p;
357341
}
358342

kernel-open/nvidia/linux_nvswitch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ _nvswitch_os_free
711711

712712
if (is_vmalloc_addr(ptr))
713713
{
714-
vfree(ptr, -1ull);
714+
vfree(ptr);
715715
}
716716
else
717717
{

0 commit comments

Comments
 (0)