cma_heap_attach() merges the physically contiguous buffer into one
scatterlist entry without considering the importing device's maximum
segment size. With DMA_API_DEBUG, importing a buffer larger than that
limit reports, here with atmel-hlcdc:

  DMA-API: atmel-hlcdc-display-controller atmel-hlcdc-dc: mapping sg
  segment longer than device claims to support [len=307200] [max=65536]

Build each attachment table using the importing device's segment limit.
Return -EINVAL if the limit is smaller than PAGE_SIZE because the
page-based allocator cannot honor it.

An importer that needs the whole buffer as one entry has to declare a
segment size that covers it. tegra-vde without an IOMMU domain and
armada did not. Their limits must be raised before this change is
applied.

Reproduced with a custom QEMU model of the SAM9X75 Curiosity. Not
tested on real hardware.

Fixes: b61614ec318a ("dma-buf: heaps: Add CMA heap to dmabuf heaps")
Cc: [email protected] # needs the tegra-vde and armada segment size 
changes first
Assisted-by: LLM
Signed-off-by: Karl Mehltretter <[email protected]>
---

Notes:
    Tested on v7.3-rc4-70-gfe2ec83746e5 with DMA_API_DEBUG and
    DMABUF_DEBUG, on a custom QEMU model of the SAM9X75 Curiosity
    (ARM926EJ-S, TCG) with an XLCDC model.
    
    vivid captures into three 307200-byte CMA heap buffers, which
    atmel-hlcdc imports and scans out. All 8 frames are shown before and
    after the patch, with 3 DMA-API reports before and none after.
    
    A test importer maps a 256 KiB CMA heap buffer with different
    maximum segment sizes (4 KiB pages):
    
      max segment (bytes)   before         after
      2048                  1 x 256 KiB    attach returns -EINVAL
      4096                  1 x 256 KiB    64 x 4 KiB
      65536                 1 x 256 KiB    4 x 64 KiB
      UINT_MAX              1 x 256 KiB    1 x 256 KiB
    
    The first three cases gave a DMA-API report before the patch and
    none after it.

 drivers/dma-buf/heaps/cma_heap.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/drivers/dma-buf/heaps/cma_heap.c b/drivers/dma-buf/heaps/cma_heap.c
index 3fb4b946c91a..9d61d14688b8 100644
--- a/drivers/dma-buf/heaps/cma_heap.c
+++ b/drivers/dma-buf/heaps/cma_heap.c
@@ -58,16 +58,22 @@ static int cma_heap_attach(struct dma_buf *dmabuf,
 {
        struct cma_heap_buffer *buffer = dmabuf->priv;
        struct dma_heap_attachment *a;
+       unsigned int max_segment;
        int ret;
 
+       max_segment = dma_get_max_seg_size(attachment->dev);
+       /* The SG allocator requires a segment limit of at least PAGE_SIZE. */
+       if (max_segment < PAGE_SIZE)
+               return -EINVAL;
+
        a = kzalloc_obj(*a);
        if (!a)
                return -ENOMEM;
 
-       ret = sg_alloc_table_from_pages(&a->table, buffer->pages,
-                                       buffer->pagecount, 0,
-                                       buffer->pagecount << PAGE_SHIFT,
-                                       GFP_KERNEL);
+       ret = sg_alloc_table_from_pages_segment(&a->table, buffer->pages,
+                                               buffer->pagecount, 0,
+                                               buffer->pagecount << PAGE_SHIFT,
+                                               max_segment, GFP_KERNEL);
        if (ret) {
                kfree(a);
                return ret;
-- 
2.53.0

Reply via email to