From d870c5bea7856469130d8b2b15ea0540932c1e70 Mon Sep 17 00:00:00 2001 From: Jianqun Xu Date: Thu, 5 May 2022 09:57:40 +0800 Subject: [PATCH] dma-buf: support to stat peak size for all dmabuf The system memory presure always take care of the peak memory size, for dmabuf, the peak size is useful when media module to design drivers. Get peak can show the peak size currently, and reset peak can clear it. Signed-off-by: Jianqun Xu Change-Id: I56b0323167361e11dd657a22449aad65751fc81a --- drivers/dma-buf/dma-buf.c | 44 +++++++++++++++++++++++++++++++++++++++ include/linux/dma-buf.h | 11 ++++++++++ 2 files changed, 55 insertions(+) diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c index d0c06ceb204e..023f34ed98af 100644 --- a/drivers/dma-buf/dma-buf.c +++ b/drivers/dma-buf/dma-buf.c @@ -63,6 +63,43 @@ int get_each_dmabuf(int (*callback)(const struct dma_buf *dmabuf, } EXPORT_SYMBOL_GPL(get_each_dmabuf); +#if IS_ENABLED(CONFIG_DMABUF_DEBUG) +static size_t db_total_size; +static size_t db_peak_size; + +void dma_buf_reset_peak_size(void) +{ + mutex_lock(&db_list.lock); + db_peak_size = 0; + mutex_unlock(&db_list.lock); +} +EXPORT_SYMBOL_GPL(dma_buf_reset_peak_size); + +size_t dma_buf_get_peak_size(void) +{ + size_t sz; + + mutex_lock(&db_list.lock); + sz = db_peak_size; + mutex_unlock(&db_list.lock); + + return sz; +} +EXPORT_SYMBOL_GPL(dma_buf_get_peak_size); + +size_t dma_buf_get_total_size(void) +{ + size_t sz; + + mutex_lock(&db_list.lock); + sz = db_total_size; + mutex_unlock(&db_list.lock); + + return sz; +} +EXPORT_SYMBOL_GPL(dma_buf_get_total_size); +#endif + static char *dmabuffs_dname(struct dentry *dentry, char *buffer, int buflen) { struct dma_buf *dmabuf; @@ -129,6 +166,9 @@ static int dma_buf_file_release(struct inode *inode, struct file *file) dmabuf = file->private_data; mutex_lock(&db_list.lock); +#if IS_ENABLED(CONFIG_DMABUF_DEBUG) + db_total_size -= dmabuf->size; +#endif list_del(&dmabuf->list_node); mutex_unlock(&db_list.lock); @@ -700,6 +740,10 @@ struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info) mutex_lock(&db_list.lock); list_add(&dmabuf->list_node, &db_list.head); +#if IS_ENABLED(CONFIG_DMABUF_DEBUG) + db_total_size += dmabuf->size; + db_peak_size = max(db_total_size, db_peak_size); +#endif mutex_unlock(&db_list.lock); if (IS_ENABLED(CONFIG_DMABUF_DEBUG)) diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h index 4177f6512072..f87ebbdf945b 100644 --- a/include/linux/dma-buf.h +++ b/include/linux/dma-buf.h @@ -660,4 +660,15 @@ static inline void dma_buf_set_destructor(struct dma_buf *dmabuf, dmabuf->dtor_data = dtor_data; } #endif + +#if IS_ENABLED(CONFIG_DMABUF_DEBUG) +void dma_buf_reset_peak_size(void); +size_t dma_buf_get_peak_size(void); +size_t dma_buf_get_total_size(void); +#else +static inline void dma_buf_reset_peak_size(void) {} +static inline size_t dma_buf_get_peak_size(void) { return 0; } +static inline size_t dma_buf_get_total_size(void) { return 0; } +#endif + #endif /* __DMA_BUF_H__ */