[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250530103941.11092-5-tao.wangtao@honor.com>
Date: Fri, 30 May 2025 18:39:41 +0800
From: wangtao <tao.wangtao@...or.com>
To: <sumit.semwal@...aro.org>, <christian.koenig@....com>,
<kraxel@...hat.com>, <vivek.kasireddy@...el.com>, <viro@...iv.linux.org.uk>,
<brauner@...nel.org>, <hughd@...gle.com>, <akpm@...ux-foundation.org>,
<amir73il@...il.com>
CC: <benjamin.gaignard@...labora.com>, <Brian.Starkey@....com>,
<jstultz@...gle.com>, <tjmercier@...gle.com>, <jack@...e.cz>,
<baolin.wang@...ux.alibaba.com>, <linux-media@...r.kernel.org>,
<dri-devel@...ts.freedesktop.org>, <linaro-mm-sig@...ts.linaro.org>,
<linux-kernel@...r.kernel.org>, <linux-fsdevel@...r.kernel.org>,
<linux-mm@...ck.org>, <bintian.wang@...or.com>, <yipengxiang@...or.com>,
<liulu.liu@...or.com>, <feng.han@...or.com>, wangtao <tao.wangtao@...or.com>
Subject: [PATCH v3 4/4] dmabuf:system_heap Implement system_heap exporter's rw_file callback.
First verify system_heap exporter has exclusive dmabuf access.
Build bio_vec from sgtable, then invoke target file's r/w callbacks for IO.
Outperforms buffer IO mmap/read by 250%, beats direct I/O udmabuf
copy_file_range by over 30% with initialization time significantly lower
than udmabuf.
Test data:
| 32x32MB Read 1024MB |Creat-ms|Close-ms| I/O-ms|I/O-MB/s| I/O%
|-------------------------|--------|--------|--------|--------|-----
| 1)Beg dmabuf buffer R/W| 47 | 5 | 1125 | 954 | 100%
| 2) udmabuf buffer R/W| 576 | 323 | 1228 | 874 | 91%
| 3) udma+memfd buffer R/W| 596 | 340 | 2166 | 495 | 51%
| 4) udma+memfd direct R/W| 570 | 338 | 711 | 1510 | 158%
| 5) udmabuf buffer c_f_r| 578 | 329 | 1128 | 952 | 99%
| 6) udmabuf direct c_f_r| 570 | 324 | 405 | 2651 | 277%
| 7) dmabuf buffer c_f_r| 47 | 5 | 1035 | 1037 | 108%
| 8) dmabuf direct c_f_r| 51 | 5 | 309 | 3480 | 364%
| 9)End dmabuf buffer R/W| 48 | 5 | 1153 | 931 | 97%
| 32x32MB Write 1024MB |Creat-ms|Close-ms| I/O-ms|I/O-MB/s| I/O%
|-------------------------|--------|--------|--------|--------|-----
| 1)Beg dmabuf buffer R/W| 50 | 5 | 1405 | 764 | 100%
| 2) udmabuf buffer R/W| 580 | 341 | 1337 | 803 | 105%
| 3) udma+memfd buffer R/W| 588 | 331 | 1820 | 590 | 77%
| 4) udma+memfd direct R/W| 585 | 333 | 662 | 1622 | 212%
| 5) udmabuf buffer c_f_r| 577 | 329 | 1326 | 810 | 106%
| 6) udmabuf direct c_f_r| 580 | 330 | 602 | 1784 | 233%
| 7) dmabuf buffer c_f_r| 49 | 5 | 1330 | 807 | 105%
| 8) dmabuf direct c_f_r| 49 | 5 | 344 | 3127 | 409%
| 9)End dmabuf buffer R/W| 50 | 5 | 1442 | 745 | 97%
Signed-off-by: wangtao <tao.wangtao@...or.com>
---
drivers/dma-buf/heaps/system_heap.c | 79 +++++++++++++++++++++++++++++
1 file changed, 79 insertions(+)
diff --git a/drivers/dma-buf/heaps/system_heap.c b/drivers/dma-buf/heaps/system_heap.c
index 26d5dc89ea16..d3a1956ebad8 100644
--- a/drivers/dma-buf/heaps/system_heap.c
+++ b/drivers/dma-buf/heaps/system_heap.c
@@ -20,6 +20,9 @@
#include <linux/scatterlist.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
+#include <linux/bvec.h>
+#include <linux/bio.h>
+#include <linux/uio.h>
static struct dma_heap *sys_heap;
@@ -281,6 +284,81 @@ static void system_heap_vunmap(struct dma_buf *dmabuf, struct iosys_map *map)
iosys_map_clear(map);
}
+static ssize_t system_heap_buffer_rw_other(struct system_heap_buffer *buffer,
+ loff_t my_pos, struct file *other, loff_t pos,
+ size_t count, bool is_write)
+{
+ struct sg_table *sgt = &buffer->sg_table;
+ struct scatterlist *sg;
+ loff_t my_end = my_pos + count, bv_beg, bv_end = 0;
+ pgoff_t pg_idx = my_pos / PAGE_SIZE;
+ pgoff_t pg_end = DIV_ROUND_UP(my_end, PAGE_SIZE);
+ size_t i, bv_off, bv_len, bv_num, bv_idx = 0, bv_total = 0;
+ struct bio_vec *bvec;
+ struct kiocb kiocb;
+ struct iov_iter iter;
+ unsigned int direction = is_write ? ITER_SOURCE : ITER_DEST;
+ ssize_t ret = 0, rw_total = 0;
+
+ bv_num = min_t(size_t, pg_end - pg_idx + 1, 1024);
+ bvec = kvcalloc(bv_num, sizeof(*bvec), GFP_KERNEL);
+ if (!bvec)
+ return -ENOMEM;
+
+ init_sync_kiocb(&kiocb, other);
+ kiocb.ki_pos = pos;
+
+ for_each_sg(sgt->sgl, sg, sgt->nents, i) {
+ if (my_pos >= my_end)
+ break;
+ bv_beg = bv_end;
+ bv_end += sg->length;
+ if (bv_end <= my_pos)
+ continue;
+
+ bv_len = min(bv_end, my_end) - my_pos;
+ bv_off = sg->offset + my_pos - bv_beg;
+ my_pos += bv_len;
+ bv_total += bv_len;
+ bvec_set_page(&bvec[bv_idx], sg_page(sg), bv_len, bv_off);
+ if (++bv_idx < bv_num && my_pos < my_end)
+ continue;
+
+ /* start R/W if bvec is full or count reaches zero. */
+ iov_iter_bvec(&iter, direction, bvec, bv_idx, bv_total);
+ if (is_write)
+ ret = other->f_op->write_iter(&kiocb, &iter);
+ else
+ ret = other->f_op->read_iter(&kiocb, &iter);
+ if (ret <= 0)
+ break;
+ rw_total += ret;
+ if (ret < bv_total || fatal_signal_pending(current))
+ break;
+
+ bv_idx = bv_total = 0;
+ }
+ kvfree(bvec);
+
+ return rw_total > 0 ? rw_total : ret;
+}
+
+static ssize_t system_heap_dma_buf_rw_file(struct dma_buf *dmabuf,
+ loff_t my_pos, struct file *file, loff_t pos,
+ size_t count, bool is_write)
+{
+ struct system_heap_buffer *buffer = dmabuf->priv;
+ ssize_t ret = -EBUSY;
+
+ mutex_lock(&buffer->lock);
+ if (list_empty(&buffer->attachments) && !buffer->vmap_cnt)
+ ret = system_heap_buffer_rw_other(buffer, my_pos,
+ file, pos, count, is_write);
+ mutex_unlock(&buffer->lock);
+
+ return ret;
+}
+
static void system_heap_dma_buf_release(struct dma_buf *dmabuf)
{
struct system_heap_buffer *buffer = dmabuf->priv;
@@ -308,6 +386,7 @@ static const struct dma_buf_ops system_heap_buf_ops = {
.mmap = system_heap_mmap,
.vmap = system_heap_vmap,
.vunmap = system_heap_vunmap,
+ .rw_file = system_heap_dma_buf_rw_file,
.release = system_heap_dma_buf_release,
};
--
2.17.1
Powered by blists - more mailing lists