#include "dma-buf-sysfs-stats.h"
+/* Wrapper to hide the sg_table page link from the importer */
+struct dma_buf_sg_table_wrapper {
+ struct sg_table *original;
+ struct sg_table wrapper;
+};
+
static inline int is_dma_buf_file(struct file *);
static DEFINE_MUTEX(dmabuf_list_mutex);
}
EXPORT_SYMBOL_NS_GPL(dma_buf_put, "DMA_BUF");
-static void mangle_sg_table(struct sg_table *sg_table)
+static int dma_buf_wrap_sg_table(struct sg_table **sg_table)
{
-#ifdef CONFIG_DMABUF_DEBUG
- int i;
- struct scatterlist *sg;
-
- /* To catch abuse of the underlying struct page by importers mix
- * up the bits, but take care to preserve the low SG_ bits to
- * not corrupt the sgt. The mixing is undone on unmap
- * before passing the sgt back to the exporter.
+ struct scatterlist *to_sg, *from_sg;
+ struct sg_table *from = *sg_table;
+ struct dma_buf_sg_table_wrapper *to;
+ int i, ret;
+
+ if (!IS_ENABLED(CONFIG_DMABUF_DEBUG))
+ return 0;
+
+ /*
+ * To catch abuse of the underlying struct page by importers copy the
+ * sg_table without copying the page_link and give only the copy back to
+ * the importer.
*/
- for_each_sgtable_sg(sg_table, sg, i)
- sg->page_link ^= ~0xffUL;
-#endif
+ to = kzalloc(sizeof(*to), GFP_KERNEL);
+ if (!to)
+ return -ENOMEM;
+
+ ret = sg_alloc_table(&to->wrapper, from->nents, GFP_KERNEL);
+ if (ret)
+ goto free_to;
+
+ to_sg = to->wrapper.sgl;
+ for_each_sgtable_dma_sg(from, from_sg, i) {
+ to_sg->offset = 0;
+ to_sg->length = 0;
+ sg_assign_page(to_sg, NULL);
+ sg_dma_address(to_sg) = sg_dma_address(from_sg);
+ sg_dma_len(to_sg) = sg_dma_len(from_sg);
+ to_sg = sg_next(to_sg);
+ }
+ to->original = from;
+ *sg_table = &to->wrapper;
+ return 0;
+
+free_to:
+ kfree(to);
+ return ret;
+}
+
+static void dma_buf_unwrap_sg_table(struct sg_table **sg_table)
+{
+ struct dma_buf_sg_table_wrapper *copy;
+
+ if (!IS_ENABLED(CONFIG_DMABUF_DEBUG))
+ return;
+
+ copy = container_of(*sg_table, typeof(*copy), wrapper);
+ *sg_table = copy->original;
+ sg_free_table(©->wrapper);
+ kfree(copy);
}
static inline bool
if (ret < 0)
goto error_unmap;
}
- mangle_sg_table(sg_table);
+ ret = dma_buf_wrap_sg_table(&sg_table);
+ if (ret)
+ goto error_unmap;
if (IS_ENABLED(CONFIG_DMA_API_DEBUG)) {
struct scatterlist *sg;
dma_resv_assert_held(attach->dmabuf->resv);
- mangle_sg_table(sg_table);
+ dma_buf_unwrap_sg_table(&sg_table);
attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, direction);
if (dma_buf_pin_on_map(attach))