Skip to contentSkip to conversation

vmalloc: Introduce vmap_file()

PATCH Superseded Vishal Moola (Oracle) 4 participants 9 messages Jan 31, 2025 – Apr 8, 2025

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover vmalloc: Introduce vmap_file()

Currently, users have to call vmap() or vmap_pfn() to map pages to kernel virtual space. vmap() requires the page references, and vmap_pfn() requires page pfns. If we have a file but no page references, we have to do extra work to map them.

Create a function, vmap_file(), to map a specified range of a given file to kernel virtual space. Also convert a user that benefits from vmap_file().

Vishal Moola (Oracle) (2):
mm/vmalloc: Introduce vmap_file()
drm: Use vmap_file() in shmem_pin_map()

 drivers/gpu/drm/i915/gt/shmem_utils.c | 23 +------
 include/linux/vmalloc.h               |  2 +
 mm/vmalloc.c                          | 97 +++++++++++++++++++++++++++
 3 files changed, 102 insertions(+), 20 deletions(-)
Signature · 1 line
2.47.1
#1 lore raw

PATCH 1/2 mm/vmalloc: Introduce vmap_file()

vmap_file() is effectively an in-kernel equivalent to calling mmap() on a file. A user can pass in a file mapping, and vmap_file() will map the specified portion of that file directly to kernel virtual space.

---
 include/linux/vmalloc.h |  2 +
 mm/vmalloc.c            | 97 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 99 insertions(+)
include/linux/vmalloc.h +2 -0
index 31e9ffd936e3..d5420985865f 100644
@@ -192,6 +192,8 @@ extern void vfree_atomic(const void *addr);
 
 extern void *vmap(struct page **pages, unsigned int count,
 			unsigned long flags, pgprot_t prot);
+void *vmap_file(struct address_space *mapping, loff_t start, loff_t end,
+			unsigned long flags, pgprot_t prot);
 void *vmap_pfn(unsigned long *pfns, unsigned int count, pgprot_t prot);
 extern void vunmap(const void *addr);
 
mm/vmalloc.c +97 -0

103 lines not on this page · raw message

Signature · 1 line
2.47.1
#2 lore raw

Re: [PATCH 1/2] mm/vmalloc: Introduce vmap_file()

mm/vmalloc.c void *vmap(struct page **pages, unsigned int count, · line 3525

Quoted 2 lines · Vishal Moola (Oracle)
mm/vmalloc.c +2 -0
+	rcu_read_lock();
+	xas_for_each(&xas, folio, last) {

This only maps folios currently in the page cache, which makes it usefull for everything except ramfs-style purely in-memory file systems. I.e. for the shmem use case in the second patch it fails to swap in swapped out tmpfs folios.

Quoted 1 line

+EXPORT_SYMBOL(vmap_file);

EXPORT_SYMBOL_GPL for any advances vmalloc-layer functionality, please.

#3 lore raw

Re: [PATCH 1/2] mm/vmalloc: Introduce vmap_file()

Quoted 9 lines · Christoph Hellwig

mm/vmalloc.c void *vmap(struct page **pages, unsigned int count, · line 3525

Quoted 2 lines · Vishal Moola (Oracle)
mm/vmalloc.c +2 -0
+     rcu_read_lock();
+     xas_for_each(&xas, folio, last) {

This only maps folios currently in the page cache, which makes it usefull for everything except ramfs-style purely in-memory file systems. I.e. for the shmem use case in the second patch it fails to swap in swapped out tmpfs folios.

Ah, I see. I can drop that patch then. Its primary purpose was to provide a user for vmap_file(). As you've pointed out, that won't work with tmpfs or anon pages. I'll hold off on a v2 until there are better usecases for vmap_file().

Quoted 3 lines
Quoted 1 line

+EXPORT_SYMBOL(vmap_file);

EXPORT_SYMBOL_GPL for any advances vmalloc-layer functionality, please.

Ok, I'll keep that in mind in the future.

#4 lore raw

PATCH 2/2 drm: Use vmap_file() in shmem_pin_map()

In reply to Vishal Moola (Oracle)

We no longer need to allocate a new array of pages to map this file to kernel virtual space. This simplifies shmem_pin_map(), and gets rid of a user of VM_MAP_PUT_PAGES.

---
 drivers/gpu/drm/i915/gt/shmem_utils.c | 23 +++--------------------
 1 file changed, 3 insertions(+), 20 deletions(-)
drivers/gpu/drm/i915/gt/shmem_utils.c +3 -20
index bb696b29ee2c..79d930ed5229 100644
@@ -57,32 +57,15 @@ struct file *shmem_create_from_object(struct drm_i915_gem_object *obj)
 
 void *shmem_pin_map(struct file *file)
 {
-	struct page **pages;
-	size_t n_pages, i;
 	void *vaddr;
 
-	n_pages = file->f_mapping->host->i_size >> PAGE_SHIFT;
-	pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL);
-	if (!pages)
-		return NULL;
-
-	for (i = 0; i < n_pages; i++) {
-		pages[i] = shmem_read_mapping_page_gfp(file->f_mapping, i,
-						       GFP_KERNEL);
-		if (IS_ERR(pages[i]))
-			goto err_page;
-	}
+	vaddr = vmap_file(file->f_mapping, 0, file->f_mapping->host->i_size,
+			VM_MAP, PAGE_KERNEL);
 
-	vaddr = vmap(pages, n_pages, VM_MAP_PUT_PAGES, PAGE_KERNEL);
 	if (!vaddr)
-		goto err_page;
+		return NULL;
 	mapping_set_unevictable(file->f_mapping);
 	return vaddr;
-err_page:
-	while (i--)
-		put_page(pages[i]);
-	kvfree(pages);
-	return NULL;
 }
 
 void shmem_unpin_map(struct file *file, void *ptr)
Signature · 1 line
2.47.1
maintainer
#5 lore raw
In reply to Vishal Moola (Oracle)
Quoted 9 lines · On Thu, 30 Jan 2025 16:18:04 -0800 "Vishal Moola (Oracle)"…

Currently, users have to call vmap() or vmap_pfn() to map pages to kernel virtual space. vmap() requires the page references, and vmap_pfn() requires page pfns. If we have a file but no page references, we have to do extra work to map them.

Create a function, vmap_file(), to map a specified range of a given file to kernel virtual space. Also convert a user that benefits from vmap_file().

Seems like a pretty specialized thing. Have you identified any other potential users of vmap_file()? I couldn't see any.

If drm is likely to remain the only user of this, perhaps we should leave the code down in drivers/gpu/drm for now?

Also, the amount of copy-n-pasting from vmap() into vmap_file() is undesirable - code size, maintenance overhead, etc.

#6 lore raw
Quoted 13 lines · Andrew Morton
Quoted 9 lines · On Thu, 30 Jan 2025 16:18:04 -0800 "Vishal Moola (Oracle)"…

Currently, users have to call vmap() or vmap_pfn() to map pages to kernel virtual space. vmap() requires the page references, and vmap_pfn() requires page pfns. If we have a file but no page references, we have to do extra work to map them.

Create a function, vmap_file(), to map a specified range of a given file to kernel virtual space. Also convert a user that benefits from vmap_file().

Quoted 5 lines

Seems like a pretty specialized thing. Have you identified any other potential users of vmap_file()? I couldn't see any.

If drm is likely to remain the only user of this, perhaps we should leave the code down in drivers/gpu/drm for now?

This function is generally useful for file-systems that use the pagecache. I simply chose to highlight the most obvious user that benefits from it (and so that the function is introduced with a user).

I haven't identified any other specific users of vmap_file() myself. I know Matthew has some other ideas for it; I've cc-ed him so he can chime in.

Quoted 3 lines

Also, the amount of copy-n-pasting from vmap() into vmap_file() is undesirable - code size, maintenance overhead, etc.

I wasn't particularly a fan of it either, but I couldn't find a more readable way to do this (without reorganizing multiple other functions). Aside from the initial flags checks, the rest of the function is slightly different from vmap(), so calling existing functions won't suffice.

I considered passing more arguments through to vmap(), but I think that would make the code more confusing, especially because the 2 functions have some different usage prerequisites.

#7 lore raw
Quoted 20 lines · Vishal Moola
Quoted 13 lines · Andrew Morton
Quoted 9 lines · On Thu, 30 Jan 2025 16:18:04 -0800 "Vishal Moola (Oracle)"…

Currently, users have to call vmap() or vmap_pfn() to map pages to kernel virtual space. vmap() requires the page references, and vmap_pfn() requires page pfns. If we have a file but no page references, we have to do extra work to map them.

Create a function, vmap_file(), to map a specified range of a given file to kernel virtual space. Also convert a user that benefits from vmap_file().

Quoted 5 lines

Seems like a pretty specialized thing. Have you identified any other potential users of vmap_file()? I couldn't see any.

If drm is likely to remain the only user of this, perhaps we should leave the code down in drivers/gpu/drm for now?

Quoted 6 lines

This function is generally useful for file-systems that use the pagecache. I simply chose to highlight the most obvious user that benefits from it (and so that the function is introduced with a user).

I haven't identified any other specific users of vmap_file() myself. I know Matthew has some other ideas for it; I've cc-ed him so he can chime in.

Not much to add but just to confirm - yep, this seems like it might be useful as a part of the solution to the page cache perf issue[1] with ASI that I spoke about (briefly and chaotically) at the end of the LSF/MM/BPF session[0] on ASI this year.

[0] https://lwn.net/Articles/1016013/
[1] https://lore.kernel.org/linux-mm/[email protected]/

But, for the moment this is all still pretty vague stuff, not at all clear yet that this idea makes total sense. Hopefully I'll be able to follow up in a few weeks after I've made some time to stare at/prototype things.

#8 lore raw
In reply to Vishal Moola (Oracle)
Quoted 8 lines · Vishal Moola (Oracle)

Currently, users have to call vmap() or vmap_pfn() to map pages to kernel virtual space. vmap() requires the page references, and vmap_pfn() requires page pfns. If we have a file but no page references, we have to do extra work to map them.

Create a function, vmap_file(), to map a specified range of a given file to kernel virtual space. Also convert a user that benefits from vmap_file().

As far as I can tell there is exatly one user that maps file pages into vmalloc space. It's a pretty odd thing to do, so figuring out a way to get rid of that might be a better use of time.