From: Huan Yang <link@vivo.com>
To: "Gerd Hoffmann" <kraxel@redhat.com>,
"Sumit Semwal" <sumit.semwal@linaro.org>,
"Christian König" <christian.koenig@amd.com>,
dri-devel@lists.freedesktop.org, linux-media@vger.kernel.org,
linaro-mm-sig@lists.linaro.org, linux-kernel@vger.kernel.org
Cc: opensource.kernel@vivo.com, Huan Yang <link@vivo.com>
Subject: [PATCH 4/5] udmabuf: add get_sg_table helper function
Date: Thu, 1 Aug 2024 18:45:08 +0800 [thread overview]
Message-ID: <20240801104512.4056860-5-link@vivo.com> (raw)
In-Reply-To: <20240801104512.4056860-1-link@vivo.com>
Currently, there are three duplicate pieces of code that retrieve
sg_table and update uduf->sg.
Since the sgt is used to populate the page in both mmap and vmap.It is
necessary to ensure that ubuf->sg is set correctly.
This patch add a helper function, if ubuf->sg exist, just return it.
Or else, try alloc a new sgt, and cmpxchg to set it.
When the swap fails, it means that another process has set sg correctly.
Therefore, we reuse the new sg. If trigger by device, need invoke map to
sync it.
Signed-off-by: Huan Yang <link@vivo.com>
---
drivers/dma-buf/udmabuf.c | 60 ++++++++++++++++++++++++++++-----------
1 file changed, 43 insertions(+), 17 deletions(-)
diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c
index 7ed532342d7f..677ebb2d462f 100644
--- a/drivers/dma-buf/udmabuf.c
+++ b/drivers/dma-buf/udmabuf.c
@@ -38,8 +38,9 @@ struct udmabuf_folio {
struct list_head list;
};
-static struct sg_table *get_sg_table(struct device *dev, struct dma_buf *buf,
- enum dma_data_direction direction);
+static struct sg_table *udmabuf_get_sg_table(struct device *dev,
+ struct dma_buf *buf,
+ enum dma_data_direction direction);
static int mmap_udmabuf(struct dma_buf *buf, struct vm_area_struct *vma)
{
@@ -52,12 +53,9 @@ static int mmap_udmabuf(struct dma_buf *buf, struct vm_area_struct *vma)
if ((vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) == 0)
return -EINVAL;
- if (!table) {
- table = get_sg_table(NULL, buf, 0);
- if (IS_ERR(table))
- return PTR_ERR(table);
- ubuf->sg = table;
- }
+ table = udmabuf_get_sg_table(NULL, buf, 0);
+ if (IS_ERR(table))
+ return PTR_ERR(table);
for_each_sgtable_page(table, &piter, vma->vm_pgoff) {
struct page *page = sg_page_iter_page(&piter);
@@ -84,12 +82,9 @@ static int vmap_udmabuf(struct dma_buf *buf, struct iosys_map *map)
dma_resv_assert_held(buf->resv);
- if (!sg) {
- sg = get_sg_table(NULL, buf, 0);
- if (IS_ERR(sg))
- return PTR_ERR(sg);
- ubuf->sg = sg;
- }
+ sg = udmabuf_get_sg_table(NULL, buf, 0);
+ if (IS_ERR(sg))
+ return PTR_ERR(sg);
pages = kvmalloc_array(ubuf->pagecount, sizeof(*pages), GFP_KERNEL);
if (!pages)
@@ -154,6 +149,39 @@ static struct sg_table *get_sg_table(struct device *dev, struct dma_buf *buf,
return ERR_PTR(ret);
}
+static struct sg_table *udmabuf_get_sg_table(struct device *dev,
+ struct dma_buf *buf,
+ enum dma_data_direction direction)
+{
+ struct udmabuf *ubuf = buf->priv;
+ struct sg_table *sg = READ_ONCE(ubuf->sg);
+ int ret = 0;
+
+ if (sg)
+ return sg;
+
+ sg = get_sg_table(dev, buf, direction);
+ if (IS_ERR(sg))
+ return sg;
+
+ // Success update ubuf's sg, just return.
+ if (!cmpxchg(&ubuf->sg, NULL, sg))
+ return sg;
+
+ // use the new sg table.
+ sg_free_table(sg);
+ kfree(sg);
+ sg = READ_ONCE(ubuf->sg);
+
+ if (dev)
+ ret = dma_map_sgtable(dev, sg, direction, 0);
+
+ if (ret < 0)
+ return ERR_PTR(ret);
+
+ return sg;
+}
+
static void put_sg_table(struct device *dev, struct sg_table *sg,
enum dma_data_direction direction)
{
@@ -230,12 +258,10 @@ static int begin_cpu_udmabuf(struct dma_buf *buf,
return 0;
}
- sg = get_sg_table(dev, buf, direction);
+ sg = udmabuf_get_sg_table(dev, buf, direction);
if (IS_ERR(sg))
return PTR_ERR(sg);
- ubuf->sg = sg;
-
return 0;
}
--
2.45.2
next prev parent reply other threads:[~2024-08-01 10:45 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-01 10:45 [PATCH 0/5] udmbuf bug fix and some improvements Huan Yang
2024-08-01 10:45 ` [PATCH 1/5] udmabuf: cancel mmap page fault, direct map it Huan Yang
2024-08-01 10:50 ` Christian König
2024-08-01 11:08 ` Huan Yang
2024-08-01 10:45 ` [PATCH 2/5] udmabuf: change folios array from kmalloc to kvmalloc Huan Yang
2024-08-01 10:54 ` Christian König
2024-08-01 10:45 ` [PATCH 3/5] udmabuf: fix vmap_udmabuf error page set Huan Yang
2024-08-01 10:55 ` Christian König
2024-08-01 10:45 ` Huan Yang [this message]
2024-08-01 10:56 ` [PATCH 4/5] udmabuf: add get_sg_table helper function Christian König
2024-08-01 10:45 ` [PATCH 5/5] udmabuf: remove folio pin list Huan Yang
2024-08-01 18:32 ` [PATCH 0/5] udmbuf bug fix and some improvements Kasireddy, Vivek
2024-08-02 1:16 ` Huan Yang
2024-08-02 15:26 ` Michel Dänzer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240801104512.4056860-5-link@vivo.com \
--to=link@vivo.com \
--cc=christian.koenig@amd.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=kraxel@redhat.com \
--cc=linaro-mm-sig@lists.linaro.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=opensource.kernel@vivo.com \
--cc=sumit.semwal@linaro.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox