Skip to content

Commit 7620163

Browse files
author
gaohaoyuan
committed
fix dma count bug
1 parent 9bc8963 commit 7620163

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

backtrace/include/malloc_debug.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ int debug_posix_memalign(void** memptr, size_t alignment, size_t size);
1515
void* debug_mmap(void* addr, size_t size, int prot, int flags, int fd, off_t offset);
1616
int debug_munmap(void* addr, size_t size);
1717
int debug_ioctl(int fd, unsigned int request, void* arg);
18+
int debug_close(int fd);
1819
void* debug_mmap64(void* addr, size_t size, int prot, int flags, int fd, off_t offset);

backtrace/src/malloc_debug.cpp

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <sys/param.h> // powerof2 ---> ((((x) - 1) & (x)) == 0)
44
#include <unistd.h>
55
#include <sys/stat.h>
6+
#include <linux/dma-heap.h>
67

78
#include <cstring>
89
#include <string>
@@ -334,7 +335,32 @@ int debug_ioctl(int fd, unsigned int request, void* arg) {
334335
gpu_ioctl_alloc = true; // Only the calling thread will set this to true
335336
}
336337

337-
return (int)syscall(SYS_ioctl, fd, request, arg);
338+
int ret = (int)syscall(SYS_ioctl, fd, request, arg);
339+
if (g_debug->TrackPointers() && request == DMA_HEAP_IOCTL_ALLOC) {
340+
struct dma_heap_allocation_data* heap = (struct dma_heap_allocation_data*)arg;
341+
if(DMA_BUF::is_dma_buf(heap->fd)) {
342+
void* ptr = reinterpret_cast<void*>(heap->fd);
343+
g_debug->pointer->Add(ptr, heap->len, DMA);
344+
}
345+
}
346+
347+
return ret;
348+
}
349+
350+
int debug_close(int fd) {
351+
if (DebugCallsDisabled()) {
352+
return (int)syscall(SYS_close, fd);
353+
}
354+
355+
ScopedConcurrentLock lock;
356+
ScopedDisableDebugCalls disable;
357+
358+
if (g_debug->TrackPointers()) {
359+
void* ptr = reinterpret_cast<void*>(fd);
360+
g_debug->pointer->Remove(ptr);
361+
}
362+
363+
return (int)syscall(SYS_close, fd);
338364
}
339365

340366
void* debug_mmap64(void* addr, size_t size, int prot, int flags, int fd, off_t offset) {
@@ -361,7 +387,7 @@ void* debug_mmap64(void* addr, size_t size, int prot, int flags, int fd, off_t o
361387
}
362388

363389
void* debug_mmap(void* addr, size_t size, int prot, int flags, int fd, off_t offset) {
364-
if (DebugCallsDisabled() || addr != nullptr) {
390+
if (DebugCallsDisabled()) {
365391
return (void*)syscall(SYS_mmap, addr, size, prot, flags, fd, offset);
366392
}
367393

@@ -377,8 +403,10 @@ void* debug_mmap(void* addr, size_t size, int prot, int flags, int fd, off_t off
377403
if (g_debug->TrackPointers()) {
378404
if (fd < 0)
379405
g_debug->pointer->Add(result, size, MMAP);
380-
else if (DMA_BUF::is_dma_buf(fd))
381-
g_debug->pointer->Add(result, size, DMA);
406+
else if (DMA_BUF::is_dma_buf(fd)) {
407+
void* ptr = reinterpret_cast<void*>(fd);
408+
g_debug->pointer->Add(ptr, size, DMA);
409+
}
382410
}
383411

384412
return result;

src/alloc_hook.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class AllocHook {
5454
}
5555
int munmap(void* addr, size_t size) { return debug_munmap(addr, size); }
5656
int ioctl(int fd, int request, void* arg) { return debug_ioctl(fd, request, arg); }
57+
int close(int fd) { return debug_close(fd); }
5758
void* mmap64(void* addr, size_t size, int prot, int flags, int fd, off_t offset) {
5859
return debug_mmap64(addr, size, prot, flags, fd, offset);
5960
}
@@ -152,6 +153,10 @@ int ioctl(int fd, int request, ...) {
152153
return AllocHook::inst().ioctl(fd, request, arg);
153154
}
154155

156+
int close(int fd) {
157+
return AllocHook::inst().close(fd);
158+
}
159+
155160
void* mmap64(void* addr, size_t size, int prot, int flags, int fd, off_t offset) {
156161
if (in_preinit_phase || InitState::allocHook_setup) {
157162
return (void*)syscall(SYS_mmap, addr, size, prot, flags, fd, offset);

version_script.ld

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ global:
99
mmap;
1010
munmap;
1111
ioctl;
12+
close;
1213
mmap64;
1314
checkpoint;
1415

0 commit comments

Comments
 (0)