diff --git a/dsp/adsprpc.c b/dsp/adsprpc.c index 4d0500942c..af940a59cd 100644 --- a/dsp/adsprpc.c +++ b/dsp/adsprpc.c @@ -1089,6 +1089,14 @@ static void fastrpc_mmap_free(struct fastrpc_mmap *map, uint32_t flags) if (!IS_ERR_OR_NULL(map->buf)) dma_buf_put(map->buf); } + if (fl) { + spin_lock(&fl->hlock); + if ((map->flags == ADSP_MMAP_ADD_PAGES) || (map->flags == ADSP_MMAP_ADD_PAGES_LLC)) + fl->mem_snap.heap_bufs_size -= map->size; + else + fl->mem_snap.nonheap_bufs_size -= map->size; + spin_unlock(&fl->hlock); + } bail: if (!map->is_persistent) kfree(map); @@ -1163,6 +1171,8 @@ static int fastrpc_mmap_create(struct fastrpc_file *fl, int fd, struct dma_buf * struct fastrpc_mmap *map = NULL; int err = 0, vmid, sgl_index = 0; struct scatterlist *sgl = NULL; + bool dma_attach_fail = false; + size_t tot_bufs_size = 0; if (!fl) { err = -EBADF; @@ -1227,6 +1237,7 @@ static int fastrpc_mmap_create(struct fastrpc_file *fl, int fd, struct dma_buf * ADSPRPC_ERR( "dma_buf_attach for fd %d for len 0x%zx failed to map buffer on SMMU device %s ret %ld\n", fd, len, dev_name(me->dev), PTR_ERR(map->attach)); + dma_attach_fail = true; err = -EFAULT; goto bail; } @@ -1319,6 +1330,7 @@ static int fastrpc_mmap_create(struct fastrpc_file *fl, int fd, struct dma_buf * "dma_buf_attach for fd %d failed for len 0x%zx to map buffer on SMMU device %s ret %ld\n", fd, len, dev_name(sess->smmu.dev), PTR_ERR(map->attach)); + dma_attach_fail = true; err = -EFAULT; goto bail; } @@ -1398,10 +1410,24 @@ static int fastrpc_mmap_create(struct fastrpc_file *fl, int fd, struct dma_buf * } map->len = len; + spin_lock(&fl->hlock); + if ((mflags == ADSP_MMAP_ADD_PAGES) || (mflags == ADSP_MMAP_ADD_PAGES_LLC)) + fl->mem_snap.heap_bufs_size += map->size; + else + fl->mem_snap.nonheap_bufs_size += map->size; + spin_unlock(&fl->hlock); + fastrpc_mmap_add(map); *ppmap = map; bail: + if (dma_attach_fail && fl) { + tot_bufs_size = fl->mem_snap.heap_bufs_size + + fl->mem_snap.nonheap_bufs_size; + ADSPRPC_INFO("Heapbufs size: %zu, non-heapbufs size: %zu, total size: %zu\n", + fl->mem_snap.heap_bufs_size, fl->mem_snap.nonheap_bufs_size, + tot_bufs_size); + } if (map) ktime_get_real_ts64(&map->map_end_time); if (err && map) diff --git a/dsp/adsprpc_shared.h b/dsp/adsprpc_shared.h index b682318966..e343680990 100644 --- a/dsp/adsprpc_shared.h +++ b/dsp/adsprpc_shared.h @@ -827,6 +827,13 @@ struct fastrpc_dspsignal { int state; }; +struct memory_snapshot { + /* Total size of heap buffers allocated in userspace */ + size_t heap_bufs_size; + /* Total size of non-heap buffers allocated in userspace */ + size_t nonheap_bufs_size; +}; + struct fastrpc_file { struct hlist_node hn; spinlock_t hlock; @@ -844,6 +851,8 @@ struct fastrpc_file { struct fastrpc_buf *pers_hdr_buf; /* Pre-allocated buffer divided into N chunks */ struct fastrpc_buf *hdr_bufs; + /* Store snapshot of memory occupied by different buffers */ + struct memory_snapshot mem_snap; struct fastrpc_session_ctx *secsctx; uint32_t mode;