public inbox for linux-media@vger.kernel.org
 help / color / mirror / Atom feed
From: Huan Yang <link@vivo.com>
To: bingbu.cao@linux.intel.com,
	"Matthew Wilcox" <willy@infradead.org>,
	"Christoph Hellwig" <hch@lst.de>,
	"Gerd Hoffmann" <kraxel@redhat.com>,
	"Vivek Kasireddy" <vivek.kasireddy@intel.com>,
	"Sumit Semwal" <sumit.semwal@linaro.org>,
	"Christian König" <christian.koenig@amd.com>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Uladzislau Rezki" <urezki@gmail.com>,
	"Shuah Khan" <shuah@kernel.org>, "Huan Yang" <link@vivo.com>,
	linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org,
	linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org,
	linux-mm@kvack.org, linux-kselftest@vger.kernel.org
Cc: opensource.kernel@vivo.com
Subject: [RFC PATCH 3/6] mm/vmalloc: try add vmap folios range
Date: Thu, 27 Mar 2025 17:28:30 +0800	[thread overview]
Message-ID: <20250327092922.536-4-link@vivo.com> (raw)
In-Reply-To: <20250327092922.536-1-link@vivo.com>

This patch add a new api vmap_range_folios.

It support both entire folio array and range based
folio array.

By this, some situation can support, like HVO folio range vmap.
Also can reduce array size, compare to 4k based page array, only need to
record each folio and it's range(offset, nr_pages).

Signed-off-by: Huan Yang <link@vivo.com>
---
 include/linux/vmalloc.h | 57 +++++++++++++++++++++++++++++++++++++++++
 mm/vmalloc.c            | 47 +++++++++++++++++++++++++++++++++
 2 files changed, 104 insertions(+)

diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h
index 31e9ffd936e3..007a398152af 100644
--- a/include/linux/vmalloc.h
+++ b/include/linux/vmalloc.h
@@ -11,6 +11,7 @@
 #include <asm/page.h>		/* pgprot_t */
 #include <linux/rbtree.h>
 #include <linux/overflow.h>
+#include <linux/mm.h>
 
 #include <asm/vmalloc.h>
 
@@ -83,6 +84,24 @@ struct vmap_area {
 	unsigned long flags; /* mark type of vm_map_ram area */
 };
 
+/**
+ * folio range [pgoff, pgoff + nr_pages) in [0, folio_nr_pages)
+ */
+struct vmap_folios_range {
+	struct folio *folio;
+
+	pgoff_t pgoff;
+	unsigned int nr_pages;
+};
+
+struct vmap_folios_struct {
+	union {
+		struct folio **_folios;
+		struct vmap_folios_range *_range_folios;
+	};
+	bool range;
+};
+
 /* archs that select HAVE_ARCH_HUGE_VMAP should override one or more of these */
 #ifndef arch_vmap_p4d_supported
 static inline bool arch_vmap_p4d_supported(pgprot_t prot)
@@ -195,6 +214,44 @@ extern void *vmap(struct page **pages, unsigned int count,
 void *vmap_pfn(unsigned long *pfns, unsigned int count, pgprot_t prot);
 extern void vunmap(const void *addr);
 
+extern void *__vmap_range_folios(struct vmap_folios_struct *folios,
+				 unsigned int count, unsigned int size,
+				 unsigned long flags, pgprot_t prot);
+
+static inline void *vmap_range_folios(struct vmap_folios_struct *folios,
+				      unsigned int count, unsigned long flags,
+				      pgprot_t prot)
+{
+	unsigned int size, i;
+
+	for (i = 0, size = 0; i < count; ++i) {
+		struct vmap_folios_range *range = &folios->_range_folios[i];
+
+		if (WARN_ON(!range || !range->folio))
+			return NULL;
+
+		if (WARN_ON(range->pgoff + range->nr_pages >
+			    folio_nr_pages(range->folio)))
+			return NULL;
+
+		size += PAGE_SIZE * range->nr_pages;
+	}
+
+	return __vmap_range_folios(folios, count, size, flags, prot);
+}
+
+static inline void *vmap_folios(struct vmap_folios_struct *folios,
+				unsigned int count, unsigned long flags,
+				pgprot_t prot)
+{
+	unsigned int size, i;
+
+	for (i = 0, size = 0; i < count; ++i)
+		size += folio_size(folios->_folios[i]);
+
+	return __vmap_range_folios(folios, count, size, flags, prot);
+}
+
 extern int remap_vmalloc_range_partial(struct vm_area_struct *vma,
 				       unsigned long uaddr, void *kaddr,
 				       unsigned long pgoff, unsigned long size);
diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index 044af7088359..247a4b940be1 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -3530,6 +3530,53 @@ void *vmap_pfn(unsigned long *pfns, unsigned int count, pgprot_t prot)
 EXPORT_SYMBOL_GPL(vmap_pfn);
 #endif /* CONFIG_VMAP_PFN */
 
+void *__vmap_range_folios(struct vmap_folios_struct *folios, unsigned int count,
+			  unsigned int size, unsigned long flags, pgprot_t prot)
+{
+	struct vm_struct *area;
+	unsigned long addr;
+	unsigned int i;
+
+	area = get_vm_area_caller(size, flags, __builtin_return_address(0));
+	if (!area)
+		return NULL;
+
+	addr = (unsigned long)area->addr;
+	for (i = 0; i < count; ++i) {
+		unsigned int nr_pages;
+		unsigned long pfn;
+		int err;
+
+		if (folios->range) {
+			struct vmap_folios_range *folio_range =
+				&folios->_range_folios[i];
+
+			pfn = folio_pfn(folio_range->folio) +
+			      folio_range->pgoff;
+			nr_pages = folio_range->nr_pages;
+		} else {
+			struct folio *folio = folios->_folios[i];
+
+			pfn = folio_pfn(folio);
+			nr_pages = folio_nr_pages(folio);
+		}
+
+		err = vmap_range_noflush(addr, addr + (nr_pages << PAGE_SHIFT),
+					 PFN_PHYS(pfn), prot, PAGE_SHIFT);
+		if (err) {
+			free_vm_area(area);
+			return NULL;
+		}
+
+		addr += (nr_pages << PAGE_SHIFT);
+	}
+
+	flush_cache_vmap((unsigned long)area->addr,
+			 (unsigned long)area->addr + size);
+	return area->addr;
+}
+EXPORT_SYMBOL_GPL(__vmap_range_folios);
+
 static inline unsigned int
 vm_area_alloc_pages(gfp_t gfp, int nid,
 		unsigned int order, unsigned int nr_pages, struct page **pages)
-- 
2.48.1


  parent reply	other threads:[~2025-03-27  9:30 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-27  9:28 [RFC PATCH 0/6] Deep talk about folio vmap Huan Yang
2025-03-27  9:28 ` [RFC PATCH 1/6] udmabuf: try fix udmabuf vmap Huan Yang
2025-03-27  9:28 ` [RFC PATCH 2/6] udmabuf: try udmabuf vmap test Huan Yang
2025-03-27  9:28 ` Huan Yang [this message]
2025-03-27  9:28 ` [RFC PATCH 4/6] udmabuf: use vmap_range_folios Huan Yang
2025-03-27  9:28 ` [RFC PATCH 5/6] udmabuf: vmap test suit for pages and pfns compare Huan Yang
2025-03-27  9:28 ` [RFC PATCH 6/6] udmabuf: remove no need code Huan Yang
2025-03-28 21:09 ` [RFC PATCH 0/6] Deep talk about folio vmap Vishal Moola (Oracle)
2025-04-04  9:01 ` CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP is broken, was " Christoph Hellwig
2025-04-04  9:38   ` Muchun Song
2025-04-04 10:07     ` Muchun Song
2025-04-07  1:59       ` Huan Yang
2025-04-07  2:57         ` Muchun Song
2025-04-07  3:21           ` Huan Yang
2025-04-07  3:37             ` Muchun Song
2025-04-07  6:43               ` Muchun Song
2025-04-07  7:09                 ` Huan Yang
2025-04-07  7:22                   ` Muchun Song
2025-04-07  8:55                     ` Huan Yang
2025-04-07  8:59                 ` Christoph Hellwig
2025-04-07  9:48                   ` Muchun Song

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=20250327092922.536-4-link@vivo.com \
    --to=link@vivo.com \
    --cc=akpm@linux-foundation.org \
    --cc=bingbu.cao@linux.intel.com \
    --cc=christian.koenig@amd.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=hch@lst.de \
    --cc=kraxel@redhat.com \
    --cc=linaro-mm-sig@lists.linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=opensource.kernel@vivo.com \
    --cc=shuah@kernel.org \
    --cc=sumit.semwal@linaro.org \
    --cc=urezki@gmail.com \
    --cc=vivek.kasireddy@intel.com \
    --cc=willy@infradead.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