From 345084a2ede24f7249f639ccf04bf24e6acf1538 Mon Sep 17 00:00:00 2001 From: Liam Mark Date: Thu, 21 Feb 2019 11:56:18 -0800 Subject: [PATCH] BACKPORT: ANDROID: GKI: dma-buf: Add support to set a destructor on a dma-buf dma-buf destructor support is useful as it allows clients an opportunity to undo any attributes, such as security attributes, they have applied to the dma-buf's memory. The destructor is called when the dma-buf is freed, if the destructor returns an error the dma-buf's exporter release function is not called in order to ensure that memory which has not been properly cleaned up isn't returned to the system. Signed-off-by: Liam Mark Signed-off-by: Swathi Sridhar [surenb: cherry-picked from: 3af4db1543c9 "dma-buf: Add support to set a destructor on a dma-buf"] Bug: 150611569 Test: build Signed-off-by: Suren Baghdasaryan Change-Id: I2d435b99fb9b1747bc1b32a4e0d484957614a5a3 Signed-off-by: Jianqun Xu --- drivers/dma-buf/dma-buf.c | 11 ++++++++++- include/linux/dma-buf.h | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c index 78f37f7c3462..83bfedcebc90 100644 --- a/drivers/dma-buf/dma-buf.c +++ b/drivers/dma-buf/dma-buf.c @@ -81,6 +81,9 @@ static char *dmabuffs_dname(struct dentry *dentry, char *buffer, int buflen) static void dma_buf_release(struct dentry *dentry) { struct dma_buf *dmabuf; +#ifdef CONFIG_NO_GKI + int dtor_ret = 0; +#endif dmabuf = dentry->d_fsdata; if (unlikely(!dmabuf)) @@ -99,7 +102,13 @@ static void dma_buf_release(struct dentry *dentry) BUG_ON(dmabuf->cb_shared.active || dmabuf->cb_excl.active); dma_buf_stats_teardown(dmabuf); - dmabuf->ops->release(dmabuf); +#ifdef CONFIG_NO_GKI + if (dmabuf->dtor) + dtor_ret = dmabuf->dtor(dmabuf, dmabuf->dtor_data); + + if (!dtor_ret) +#endif + dmabuf->ops->release(dmabuf); if (dmabuf->resv == (struct dma_resv *)&dmabuf[1]) dma_resv_fini(dmabuf->resv); diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h index 63ffe57a61d5..d3ad7aeee438 100644 --- a/include/linux/dma-buf.h +++ b/include/linux/dma-buf.h @@ -359,6 +359,20 @@ struct dma_buf_ops { ANDROID_KABI_RESERVE(2); }; +#ifdef CONFIG_NO_GKI +/** + * dma_buf_destructor - dma-buf destructor function + * @dmabuf: [in] pointer to dma-buf + * @dtor_data: [in] destructor data associated with this buffer + * + * The dma-buf destructor which is called when the dma-buf is freed. + * + * If the destructor returns an error the dma-buf's exporter release function + * won't be called. + */ +typedef int (*dma_buf_destructor)(struct dma_buf *dmabuf, void *dtor_data); +#endif + /** * struct dma_buf - shared buffer object * @size: size of the buffer @@ -425,6 +439,10 @@ struct dma_buf { struct dma_buf *dmabuf; } *sysfs_entry; #endif +#ifdef CONFIG_NO_GKI + dma_buf_destructor dtor; + void *dtor_data; +#endif ANDROID_KABI_RESERVE(1); ANDROID_KABI_RESERVE(2); @@ -626,4 +644,19 @@ void dma_buf_vunmap(struct dma_buf *, void *vaddr); int dma_buf_get_flags(struct dma_buf *dmabuf, unsigned long *flags); int dma_buf_get_uuid(struct dma_buf *dmabuf, uuid_t *uuid); +#ifdef CONFIG_NO_GKI +/** + * dma_buf_set_destructor - set the dma-buf's destructor + * @dmabuf: [in] pointer to dma-buf + * @dma_buf_destructor [in] the destructor function + * @dtor_data: [in] destructor data associated with this buffer + */ +static inline void dma_buf_set_destructor(struct dma_buf *dmabuf, + dma_buf_destructor dtor, + void *dtor_data) +{ + dmabuf->dtor = dtor; + dmabuf->dtor_data = dtor_data; +} +#endif #endif /* __DMA_BUF_H__ */