From 9c6866c99b8a6e6c04753d928233a927bac0c632 Mon Sep 17 00:00:00 2001 From: Guangming Cao Date: Wed, 1 Sep 2021 20:41:54 +0800 Subject: [PATCH] ANDROID: dma-buf: support users to change dma_buf.name User space user can call DMA_BUF_SET_NAME to set dma_buf.name, but until now we can't set it at kernel side, it's difficult to debug kernel dma_buf users. There are some kernel users of dma_heap also need it at MTK, such as camera, it's also have a allocator for other camera part, unlike most case in userspace, it's in kernel. For debug buffer owner, we need add it to let it can set debug name for each dmabuf, so that we can know dmabuf owner by dma_buf.name. Leaf changes summary: 1 artifact changed Changed leaf types summary: 0 leaf type changed Removed/Changed/Added functions summary: 0 Removed, 0 Changed, 1 Added function Removed/Changed/Added variables summary: 0 Removed, 0 Changed, 0 Added variable 1 Added function: [A] 'function long int dma_buf_set_name(dma_buf*, const char*)' Bug: 223353875 Link: https://lore.kernel.org/patchwork/patch/1459719/ Change-Id: Iac5c6b8838b9b4d976f4525d000e17a3abab94f6 Signed-off-by: Guangming Cao Signed-off-by: Georgi Djakov Signed-off-by: Chris Goldsworthy (cherry picked from commit af2ae8657c9a8123c333a5301278b68168a18a15) Jacky Liu : resolve 6.1 conflicts Signed-off-by: Andrew Chant --- drivers/dma-buf/dma-buf.c | 40 ++++++++++++++++++++++++++++++++------- include/linux/dma-buf.h | 1 + 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c index 861ba2289484..842f0fe5d9e6 100644 --- a/drivers/dma-buf/dma-buf.c +++ b/drivers/dma-buf/dma-buf.c @@ -339,6 +339,16 @@ static __poll_t dma_buf_poll(struct file *file, poll_table *poll) return events; } +static long _dma_buf_set_name(struct dma_buf *dmabuf, const char *name) +{ + spin_lock(&dmabuf->name_lock); + kfree(dmabuf->name); + dmabuf->name = name; + spin_unlock(&dmabuf->name_lock); + + return 0; +} + /** * dma_buf_set_name - Set a name to a specific dma_buf to track the usage. * It could support changing the name of the dma-buf if the same @@ -352,19 +362,35 @@ static __poll_t dma_buf_poll(struct file *file, poll_table *poll) * devices, return -EBUSY. * */ -static long dma_buf_set_name(struct dma_buf *dmabuf, const char __user *buf) +long dma_buf_set_name(struct dma_buf *dmabuf, const char *name) +{ + long ret = 0; + char *buf = kstrndup(name, DMA_BUF_NAME_LEN, GFP_KERNEL); + + if (!buf) + return -ENOMEM; + + ret = _dma_buf_set_name(dmabuf, buf); + if (ret) + kfree(buf); + + return ret; +} +EXPORT_SYMBOL_GPL(dma_buf_set_name); + +static long dma_buf_set_name_user(struct dma_buf *dmabuf, const char __user *buf) { char *name = strndup_user(buf, DMA_BUF_NAME_LEN); + long ret = 0; if (IS_ERR(name)) return PTR_ERR(name); - spin_lock(&dmabuf->name_lock); - kfree(dmabuf->name); - dmabuf->name = name; - spin_unlock(&dmabuf->name_lock); + ret = _dma_buf_set_name(dmabuf, name); + if (ret) + kfree(name); - return 0; + return ret; } #if IS_ENABLED(CONFIG_SYNC_FILE) @@ -513,7 +539,7 @@ static long dma_buf_ioctl(struct file *file, case DMA_BUF_SET_NAME_A: case DMA_BUF_SET_NAME_B: - return dma_buf_set_name(dmabuf, (const char __user *)arg); + return dma_buf_set_name_user(dmabuf, (const char __user *)arg); #if IS_ENABLED(CONFIG_SYNC_FILE) case DMA_BUF_IOCTL_EXPORT_SYNC_FILE: diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h index f88238e08279..5efb9b98847b 100644 --- a/include/linux/dma-buf.h +++ b/include/linux/dma-buf.h @@ -729,5 +729,6 @@ int dma_buf_mmap(struct dma_buf *, struct vm_area_struct *, unsigned long); int dma_buf_vmap(struct dma_buf *dmabuf, struct iosys_map *map); void dma_buf_vunmap(struct dma_buf *dmabuf, struct iosys_map *map); +long dma_buf_set_name(struct dma_buf *dmabuf, const char *name); int dma_buf_get_flags(struct dma_buf *dmabuf, unsigned long *flags); #endif /* __DMA_BUF_H__ */