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 v3 5/5] udmabuf: remove udmabuf_folio
Date: Tue, 13 Aug 2024 17:05:11 +0800 [thread overview]
Message-ID: <20240813090518.3252469-6-link@vivo.com> (raw)
In-Reply-To: <20240813090518.3252469-1-link@vivo.com>
Currently, udmabuf handles folio by creating an unpin list to record
each folio obtained from the list and unpinning them when released. To
maintain this approach, many data structures have been established.
However, maintaining this type of data structure requires a significant
amount of memory and traversing the list is a substantial overhead,
which is not friendly to the CPU cache.
Considering that during creation, we arranged the folio array in the
order of pin and set the offset according to pgcnt.
We actually don't need to use unpin_list to unpin during release.
Instead, we can iterate through the folios array during release and
unpin any folio that is different from the ones previously accessed.
By this, not only saves the overhead of the udmabuf_folio data structure
but also makes array access more cache-friendly.
Signed-off-by: Huan Yang <link@vivo.com>
---
drivers/dma-buf/udmabuf.c | 68 +++++++++++++++++----------------------
1 file changed, 30 insertions(+), 38 deletions(-)
diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c
index 8f9cb0e2e71a..1e7f46c33d1a 100644
--- a/drivers/dma-buf/udmabuf.c
+++ b/drivers/dma-buf/udmabuf.c
@@ -26,16 +26,19 @@ MODULE_PARM_DESC(size_limit_mb, "Max size of a dmabuf, in megabytes. Default is
struct udmabuf {
pgoff_t pagecount;
- struct folio **folios;
struct sg_table *sg;
struct miscdevice *device;
+ struct folio **folios;
+ /**
+ * offset in folios array's folio, byte unit.
+ * udmabuf can use either shmem or hugetlb pages, an array based on
+ * pages may not be suitable.
+ * Especially when HVO is enabled, the tail page will be released,
+ * so our reference to the page will no longer be correct.
+ * Hence, it's necessary to record the offset in order to reference
+ * the correct PFN within the folio.
+ */
pgoff_t *offsets;
- struct list_head unpin_list;
-};
-
-struct udmabuf_folio {
- struct folio *folio;
- struct list_head list;
};
static int mmap_udmabuf(struct dma_buf *buf, struct vm_area_struct *vma)
@@ -160,32 +163,28 @@ static void unmap_udmabuf(struct dma_buf_attachment *at,
return put_sg_table(at->dev, sg, direction);
}
-static void unpin_all_folios(struct list_head *unpin_list)
+/**
+ * unpin_all_folios: unpin each folio we pinned in create
+ * The udmabuf set all folio in folios and pinned it, but for large folio,
+ * We may have only used a small portion of the physical in the folio.
+ * we will repeatedly, sequentially set the folio into the array to ensure
+ * that the offset can index the correct folio at the corresponding index.
+ * Hence, we only need to unpin the first iterred folio.
+ */
+static void unpin_all_folios(struct udmabuf *ubuf)
{
- struct udmabuf_folio *ubuf_folio;
-
- while (!list_empty(unpin_list)) {
- ubuf_folio = list_first_entry(unpin_list,
- struct udmabuf_folio, list);
- unpin_folio(ubuf_folio->folio);
-
- list_del(&ubuf_folio->list);
- kfree(ubuf_folio);
- }
-}
+ pgoff_t pg;
+ struct folio *last = NULL;
-static int add_to_unpin_list(struct list_head *unpin_list,
- struct folio *folio)
-{
- struct udmabuf_folio *ubuf_folio;
+ for (pg = 0; pg < ubuf->pagecount; pg++) {
+ struct folio *tmp = ubuf->folios[pg];
- ubuf_folio = kzalloc(sizeof(*ubuf_folio), GFP_KERNEL);
- if (!ubuf_folio)
- return -ENOMEM;
+ if (tmp == last)
+ continue;
- ubuf_folio->folio = folio;
- list_add_tail(&ubuf_folio->list, unpin_list);
- return 0;
+ last = tmp;
+ unpin_folio(tmp);
+ }
}
static void release_udmabuf(struct dma_buf *buf)
@@ -196,7 +195,7 @@ static void release_udmabuf(struct dma_buf *buf)
if (ubuf->sg)
put_sg_table(dev, ubuf->sg, DMA_BIDIRECTIONAL);
- unpin_all_folios(&ubuf->unpin_list);
+ unpin_all_folios(ubuf);
kvfree(ubuf->offsets);
kvfree(ubuf->folios);
kfree(ubuf);
@@ -308,7 +307,6 @@ static long udmabuf_create(struct miscdevice *device,
if (!ubuf)
return -ENOMEM;
- INIT_LIST_HEAD(&ubuf->unpin_list);
pglimit = (size_limit_mb * 1024 * 1024) >> PAGE_SHIFT;
for (i = 0; i < head->count; i++) {
if (!IS_ALIGNED(list[i].offset, PAGE_SIZE))
@@ -366,12 +364,6 @@ static long udmabuf_create(struct miscdevice *device,
u32 k;
long fsize = folio_size(folios[j]);
- ret = add_to_unpin_list(&ubuf->unpin_list, folios[j]);
- if (ret < 0) {
- kfree(folios);
- goto err;
- }
-
for (k = pgoff; k < fsize; k += PAGE_SIZE) {
ubuf->folios[pgbuf] = folios[j];
ubuf->offsets[pgbuf] = k;
@@ -399,7 +391,7 @@ static long udmabuf_create(struct miscdevice *device,
err:
if (memfd)
fput(memfd);
- unpin_all_folios(&ubuf->unpin_list);
+ unpin_all_folios(ubuf);
kvfree(ubuf->offsets);
kvfree(ubuf->folios);
kfree(ubuf);
--
2.45.2
next prev parent reply other threads:[~2024-08-13 9:05 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-13 9:05 [PATCH v3 0/5] udmbuf bug fix and some improvements Huan Yang
2024-08-13 9:05 ` [PATCH v3 1/5] udmabuf: cancel mmap page fault, direct map it Huan Yang
2024-08-17 0:53 ` Kasireddy, Vivek
2024-08-20 1:30 ` Huan Yang
2024-08-13 9:05 ` [PATCH v3 2/5] udmabuf: change folios array from kmalloc to kvmalloc Huan Yang
2024-08-13 9:05 ` [PATCH v3 3/5] fix vmap_udmabuf error page set Huan Yang
2024-08-17 0:54 ` Kasireddy, Vivek
2024-08-20 1:33 ` Huan Yang
2024-08-13 9:05 ` [PATCH v3 4/5] udmabuf: codestyle cleanup Huan Yang
2024-08-17 0:58 ` Kasireddy, Vivek
2024-08-20 1:37 ` Huan Yang
2024-08-13 9:05 ` Huan Yang [this message]
2024-08-16 12:54 ` [PATCH v3 5/5] udmabuf: remove udmabuf_folio kernel test robot
2024-08-17 1:05 ` Kasireddy, Vivek
2024-08-20 1:41 ` Huan Yang
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=20240813090518.3252469-6-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
all inboxes | Powered by JetHome®