Skip to contentSkip to conversation

vmalloc: bpf: introduce VM_ALLOW_HUGE_VMAP

PATCH Superseded Song Liu 4 participants 17 messages Apr 11, 2022 – Apr 21, 2022

Subsystems (10)
Memory management x86 PowerPC BPF +6 more subsystems

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Song Liu

author reviewer
#0 lore raw

v2 cover vmalloc: bpf: introduce VM_ALLOW_HUGE_VMAP

Changes v1 => v2:
1. Add vmalloc_huge(). (Christoph Hellwig)
2. Add module_alloc_huge(). (Christoph Hellwig)
3. Add Fixes tag and Link tag. (Thorsten Leemhuis)

Enabling HAVE_ARCH_HUGE_VMALLOC on x86_64 and use it for bpf_prog_pack has caused some issues [1], as many users of vmalloc are not yet ready to handle huge pages. To enable a more smooth transition to use huge page backed vmalloc memory, this set replaces VM_NO_HUGE_VMAP flag with an new opt-in flag, VM_ALLOW_HUGE_VMAP. More discussions about this topic can be found at [2].

Patch 1 removes VM_NO_HUGE_VMAP and adds VM_ALLOW_HUGE_VMAP. Patch 2 uses VM_ALLOW_HUGE_VMAP in bpf_prog_pack.

[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/linux-mm/[email protected]/

Song Liu (3):
vmalloc: replace VM_NO_HUGE_VMAP with VM_ALLOW_HUGE_VMAP
module: introduce module_alloc_huge
bpf: use vmalloc with VM_ALLOW_HUGE_VMAP for bpf_prog_pack

 arch/Kconfig                 |  6 ++----
 arch/powerpc/kernel/module.c |  2 +-
 arch/s390/kvm/pv.c           |  2 +-
 arch/x86/kernel/module.c     | 21 +++++++++++++++++++
 include/linux/moduleloader.h |  5 +++++
 include/linux/vmalloc.h      |  4 ++--
 kernel/bpf/core.c            |  9 +++++----
 kernel/module.c              |  8 ++++++++
 mm/vmalloc.c                 | 39 +++++++++++++++++++-----------------
 9 files changed, 66 insertions(+), 30 deletions(-)

--
2.30.2

author reviewer
#1 lore raw

PATCH v2 1/3 vmalloc: replace VM_NO_HUGE_VMAP with VM_ALLOW_HUGE_VMAP

Huge page backed vmalloc memory could benefit performance in many cases. Since some users of vmalloc may not be ready to handle huge pages, VM_NO_HUGE_VMAP was introduced to allow vmalloc users to opt-out huge pages. However, it is not easy to add VM_NO_HUGE_VMAP to all the users that may try to allocate >= PMD_SIZE pages, but are not ready to handle huge pages properly.

Replace VM_NO_HUGE_VMAP with an opt-in flag, VM_ALLOW_HUGE_VMAP, so that users that benefit from huge pages could ask specificially.

Also, replace vmalloc_no_huge() with opt-in helper vmalloc_huge().

Fixes fac54e2bfb5b ("x86/Kconfig: Select HAVE_ARCH_HUGE_VMALLOC with

HAVE_ARCH_HUGE_VMAP")

---
 arch/Kconfig                 |  6 ++----
 arch/powerpc/kernel/module.c |  2 +-
 arch/s390/kvm/pv.c           |  2 +-
 include/linux/vmalloc.h      |  4 ++--
 mm/vmalloc.c                 | 39 +++++++++++++++++++-----------------
 5 files changed, 27 insertions(+), 26 deletions(-)
arch/Kconfig +2 -4
index 29b0167c088b..31c4fdc4a4ba 100644
@@ -854,10 +854,8 @@ config HAVE_ARCH_HUGE_VMAP
 
 #
 #  Archs that select this would be capable of PMD-sized vmaps (i.e.,
-#  arch_vmap_pmd_supported() returns true), and they must make no assumptions
-#  that vmalloc memory is mapped with PAGE_SIZE ptes. The VM_NO_HUGE_VMAP flag
-#  can be used to prohibit arch-specific allocations from using hugepages to
-#  help with this (e.g., modules may require it).
+#  arch_vmap_pmd_supported() returns true). The VM_ALLOW_HUGE_VMAP flag
+#  must be used to enable allocations to use hugepages.
 #
 config HAVE_ARCH_HUGE_VMALLOC
 	depends on HAVE_ARCH_HUGE_VMAP
arch/powerpc/kernel/module.c +1 -1
index 40a583e9d3c7..97a76a8619fb 100644
@@ -101,7 +101,7 @@ __module_alloc(unsigned long size, unsigned long start, unsigned long end, bool
 	 * too.
 	 */
 	return __vmalloc_node_range(size, 1, start, end, gfp, prot,
-				    VM_FLUSH_RESET_PERMS | VM_NO_HUGE_VMAP,
+				    VM_FLUSH_RESET_PERMS,
 				    NUMA_NO_NODE, __builtin_return_address(0));
 }
 
arch/s390/kvm/pv.c +1 -1
index 7f7c0d6af2ce..8afede243903 100644
@@ -142,7 +142,7 @@ static int kvm_s390_pv_alloc_vm(struct kvm *kvm)
 	 * using large pages for the virtual memory area.
 	 * This is a hardware limitation.
 	 */
-	kvm->arch.pv.stor_var = vmalloc_no_huge(vlen);
+	kvm->arch.pv.stor_var = vmalloc(vlen);
 	if (!kvm->arch.pv.stor_var)
 		goto out_err;
 	return 0;
include/linux/vmalloc.h +2 -2
index 3b1df7da402d..1024517d7937 100644
@@ -26,7 +26,7 @@ struct notifier_block;		/* in notifier.h */
 #define VM_KASAN		0x00000080      /* has allocated kasan shadow memory */
 #define VM_FLUSH_RESET_PERMS	0x00000100	/* reset direct map and flush TLB on unmap, can't be freed in atomic context */
 #define VM_MAP_PUT_PAGES	0x00000200	/* put pages and free array in vfree */
-#define VM_NO_HUGE_VMAP		0x00000400	/* force PAGE_SIZE pte mapping */
+#define VM_ALLOW_HUGE_VMAP	0x00000400      /* Allow for huge pages on archs with HAVE_ARCH_HUGE_VMALLOC */
 
 #if (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)) && \
 	!defined(CONFIG_KASAN_VMALLOC)
@@ -153,7 +153,7 @@ extern void *__vmalloc_node_range(unsigned long size, unsigned long align,
 			const void *caller) __alloc_size(1);
 void *__vmalloc_node(unsigned long size, unsigned long align, gfp_t gfp_mask,
 		int node, const void *caller) __alloc_size(1);
-void *vmalloc_no_huge(unsigned long size) __alloc_size(1);
+extern void *vmalloc_huge(unsigned long size) __alloc_size(1);
 
 extern void *__vmalloc_array(size_t n, size_t size, gfp_t flags) __alloc_size(1, 2);
 extern void *vmalloc_array(size_t n, size_t size) __alloc_size(1, 2);
mm/vmalloc.c +21 -18
index e163372d3967..7cc2be6a7554 100644
@@ -3106,7 +3106,7 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,
 		return NULL;
 	}
 
-	if (vmap_allow_huge && !(vm_flags & VM_NO_HUGE_VMAP)) {
+	if (vmap_allow_huge && (vm_flags & VM_ALLOW_HUGE_VMAP)) {
 		unsigned long size_per_node;
 
 		/*
@@ -3272,23 +3272,6 @@ void *vmalloc(unsigned long size)
 }
 EXPORT_SYMBOL(vmalloc);
 
-/**
- * vmalloc_no_huge - allocate virtually contiguous memory using small pages
- * @size:    allocation size
- *
- * Allocate enough non-huge pages to cover @size from the page level
- * allocator and map them into contiguous kernel virtual space.
- *
- * Return: pointer to the allocated memory or %NULL on error
- */
-void *vmalloc_no_huge(unsigned long size)
-{
-	return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END,
-				    GFP_KERNEL, PAGE_KERNEL, VM_NO_HUGE_VMAP,
-				    NUMA_NO_NODE, __builtin_return_address(0));
-}
-EXPORT_SYMBOL(vmalloc_no_huge);
-
 /**
  * vzalloc - allocate virtually contiguous memory with zero fill
  * @size:    allocation size
@@ -3347,6 +3330,26 @@ void *vmalloc_node(unsigned long size, int node)
 }
 EXPORT_SYMBOL(vmalloc_node);
 
+/**
+ * vmalloc_huge - allocate virtually contiguous memory, allow huge pages
+ * @size:    allocation size
+ *
+ * Allocate enough pages to cover @size from the page level
+ * allocator and map them into contiguous kernel virtual space.
+ * If @size is greater than or equal to PMD_SIZE, allow using
+ * huge pages for the memory
+ *
+ * Return: pointer to the allocated memory or %NULL on error
+ */
+void *vmalloc_huge(unsigned long size)
+{
+	return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END,
+				    GFP_KERNEL, PAGE_KERNEL, VM_ALLOW_HUGE_VMAP,
+				    NUMA_NO_NODE, __builtin_return_address(0));
+
+}
+EXPORT_SYMBOL(vmalloc_huge);
+
 /**
  * vzalloc_node - allocate memory on a specific node with zero fill
  * @size:	allocation size
Signature · 1 line
2.30.2
#2 lore raw

Re: [PATCH v2 bpf 1/3] vmalloc: replace VM_NO_HUGE_VMAP with VM_ALLOW_HUGE_VMAP

Quoted 6 lines · Song Liu

Huge page backed vmalloc memory could benefit performance in many cases. Since some users of vmalloc may not be ready to handle huge pages, VM_NO_HUGE_VMAP was introduced to allow vmalloc users to opt-out huge pages. However, it is not easy to add VM_NO_HUGE_VMAP to all the users that may try to allocate >= PMD_SIZE pages, but are not ready to handle huge pages properly.

This is a good place to document what the problems are, and how they are hard to track down (e.g. because the allocations are passed down I/O stacks)

Quoted 5 lines

Replace VM_NO_HUGE_VMAP with an opt-in flag, VM_ALLOW_HUGE_VMAP, so that users that benefit from huge pages could ask specificially.

Also, replace vmalloc_no_huge() with opt-in helper vmalloc_huge().

We still need to find out what the primary users of the large vmalloc hashes was and convert them.

Quoted 1 line

+extern void *vmalloc_huge(unsigned long size) __alloc_size(1);

No need for the extern.

Quoted 1 line

+EXPORT_SYMBOL(vmalloc_huge);

EXPORT_SYMBOL_GPL for all advanced vmalloc functionality, please.

author reviewer
#3 lore raw

Re: [PATCH v2 bpf 1/3] vmalloc: replace VM_NO_HUGE_VMAP with VM_ALLOW_HUGE_VMAP

Quoted 9 lines · Christoph Hellwig
Quoted 6 lines · Song Liu

Huge page backed vmalloc memory could benefit performance in many cases. Since some users of vmalloc may not be ready to handle huge pages, VM_NO_HUGE_VMAP was introduced to allow vmalloc users to opt-out huge pages. However, it is not easy to add VM_NO_HUGE_VMAP to all the users that may try to allocate >= PMD_SIZE pages, but are not ready to handle huge pages properly.

Quoted 3 lines

This is a good place to document what the problems are, and how they are hard to track down (e.g. because the allocations are passed down I/O stacks)

Will add it in v3.

Quoted 9 lines
Quoted 6 lines

Replace VM_NO_HUGE_VMAP with an opt-in flag, VM_ALLOW_HUGE_VMAP, so that users that benefit from huge pages could ask specificially.

Also, replace vmalloc_no_huge() with opt-in helper vmalloc_huge().

We still need to find out what the primary users of the large vmalloc hashes was and convert them.

@ Claudio and Nicholas,

Could you please help identify users of large vmalloc? So far, I found alloc_large_system_hash(), and something like the following seems to work:

mm/page_alloc.c +0 -0
index 6e5b4488a0c5..20d38b8482c4 100644
@@ -8919,7 +8919,7 @@ void *__init alloc_large_system_hash(const char

*tablename,

                                table = memblock_alloc_raw(size,
                                                           SMP_CACHE_BYTES);
                } else if (get_order(size) >= MAX_ORDER || hashdist) {
-                       table = __vmalloc(size, gfp_flags);
+                       table = __vmalloc_huge(size, gfp_flags);
                        virt = true;
                        if (table)
                                huge = is_vm_area_hugepages(table);
mm/vmalloc.c +8 -0
index 7cc2be6a7554..cbadbe83e6a6 100644
@@ -3253,6 +3253,14 @@ void *__vmalloc(unsigned long size, gfp_t gfp_mask)
 }
 EXPORT_SYMBOL(__vmalloc);
 
+void *__vmalloc_huge(unsigned long size, gfp_t gfp_mask)
+{
+       return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END,
+                                   gfp_mask, PAGE_KERNEL, VM_ALLOW_HUGE_VMAP,
+                                   NUMA_NO_NODE, __builtin_return_address(0));
+}
+EXPORT_SYMBOL_GPL(__vmalloc_huge);
+
 /**
  * vmalloc - allocate virtually contiguous memory
  * @size:    allocation size
Quoted 8 lines
Quoted 2 lines

+extern void *vmalloc_huge(unsigned long size) __alloc_size(1);

No need for the extern.

Quoted 1 line

+EXPORT_SYMBOL(vmalloc_huge);

EXPORT_SYMBOL_GPL for all advanced vmalloc functionality, please.

Will fix these in v3.

Thanks,
Song

#4 lore raw

Re: [PATCH v2 bpf 1/3] vmalloc: replace VM_NO_HUGE_VMAP with VM_ALLOW_HUGE_VMAP

Excerpts from Song Liu's message of April 12, 2022 4:00 pm:

Quoted 23 lines · Christoph Hellwig
Quoted 9 lines · Song Liu
Quoted 6 lines

Huge page backed vmalloc memory could benefit performance in many cases. Since some users of vmalloc may not be ready to handle huge pages, VM_NO_HUGE_VMAP was introduced to allow vmalloc users to opt-out huge pages. However, it is not easy to add VM_NO_HUGE_VMAP to all the users that may try to allocate >= PMD_SIZE pages, but are not ready to handle huge pages properly.

Quoted 3 lines

This is a good place to document what the problems are, and how they are hard to track down (e.g. because the allocations are passed down I/O stacks)

Will add it in v3.

Quoted 7 lines
Quoted 6 lines

Replace VM_NO_HUGE_VMAP with an opt-in flag, VM_ALLOW_HUGE_VMAP, so that users that benefit from huge pages could ask specificially.

Also, replace vmalloc_no_huge() with opt-in helper vmalloc_huge().

Quoted 8 lines
Quoted 2 lines

We still need to find out what the primary users of the large vmalloc hashes was and convert them.

@ Claudio and Nicholas,

Could you please help identify users of large vmalloc? So far, I found alloc_large_system_hash(), and something like the following seems to work:

The large system hashes were the main ones I was interested in. IIRC there was a few more in some drivers or tracing things depending on config but those are less important (to me at least).

Curious what the problem is though. powerpc so far has not required any special case outside arch/powerpc/ for this so I would much prefer x86 to fix itself rather than add APIs which non-arch code really shouldn't need to know about.

Thanks,
Nick

#5 lore raw

Re: [PATCH v2 bpf 1/3] vmalloc: replace VM_NO_HUGE_VMAP with VM_ALLOW_HUGE_VMAP

Excerpts from Nicholas Piggin's message of April 21, 2022 12:24 pm:

Quoted 29 lines

Excerpts from Song Liu's message of April 12, 2022 4:00 pm:

Quoted 28 lines · Christoph Hellwig
Quoted 9 lines · Song Liu
Quoted 6 lines

Huge page backed vmalloc memory could benefit performance in many cases. Since some users of vmalloc may not be ready to handle huge pages, VM_NO_HUGE_VMAP was introduced to allow vmalloc users to opt-out huge pages. However, it is not easy to add VM_NO_HUGE_VMAP to all the users that may try to allocate >= PMD_SIZE pages, but are not ready to handle huge pages properly.

Quoted 3 lines

This is a good place to document what the problems are, and how they are hard to track down (e.g. because the allocations are passed down I/O stacks)

Will add it in v3.

Quoted 9 lines
Quoted 6 lines

Replace VM_NO_HUGE_VMAP with an opt-in flag, VM_ALLOW_HUGE_VMAP, so that users that benefit from huge pages could ask specificially.

Also, replace vmalloc_no_huge() with opt-in helper vmalloc_huge().

We still need to find out what the primary users of the large vmalloc hashes was and convert them.

@ Claudio and Nicholas,

Quoted 7 lines
Quoted 3 lines

Could you please help identify users of large vmalloc? So far, I found alloc_large_system_hash(), and something like the following seems to work:

The large system hashes were the main ones I was interested in. IIRC there was a few more in some drivers or tracing things depending on config but those are less important (to me at least).

Oh there is also a reverse map array in KVM now I think of it.

Thanks,
Nick

author reviewer
#6 lore raw

PATCH v2 2/3 module: introduce module_alloc_huge

In reply to Song Liu

Introduce module_alloc_huge, which allocates huge page backed memory in module memory space. The primary user of this memory is bpf_prog_pack (multiple BPF programs sharing a huge page).

---
 arch/x86/kernel/module.c     | 21 +++++++++++++++++++++
 include/linux/moduleloader.h |  5 +++++
 kernel/module.c              |  8 ++++++++
 3 files changed, 34 insertions(+)
arch/x86/kernel/module.c +21 -0
index b98ffcf4d250..63f6a16c70dc 100644
@@ -86,6 +86,27 @@ void *module_alloc(unsigned long size)
 	return p;
 }
 
+void *module_alloc_huge(unsigned long size)
+{
+	gfp_t gfp_mask = GFP_KERNEL;
+	void *p;
+
+	if (PAGE_ALIGN(size) > MODULES_LEN)
+		return NULL;
+
+	p = __vmalloc_node_range(size, MODULE_ALIGN,
+				 MODULES_VADDR + get_module_load_offset(),
+				 MODULES_END, gfp_mask, PAGE_KERNEL,
+				 VM_DEFER_KMEMLEAK | VM_ALLOW_HUGE_VMAP,
+				 NUMA_NO_NODE, __builtin_return_address(0));
+	if (p && (kasan_alloc_module_shadow(p, size, gfp_mask) < 0)) {
+		vfree(p);
+		return NULL;
+	}
+
+	return p;
+}
+
 #ifdef CONFIG_X86_32
 int apply_relocate(Elf32_Shdr *sechdrs,
 		   const char *strtab,
include/linux/moduleloader.h +5 -0
index 9e09d11ffe5b..d34743a88938 100644
@@ -26,6 +26,11 @@ unsigned int arch_mod_section_prepend(struct module *mod, unsigned int section);
    sections.  Returns NULL on failure. */
 void *module_alloc(unsigned long size);
 
+/* Allocator used for allocating memory in module memory space. If size is
+ * greater than PMD_SIZE, allow using huge pages. Returns NULL on failure.
+ */
+void *module_alloc_huge(unsigned long size);
+
 /* Free memory returned from module_alloc. */
 void module_memfree(void *module_region);
 
kernel/module.c +8 -0
index 6cea788fd965..2af20ac3209c 100644
@@ -2839,6 +2839,14 @@ void * __weak module_alloc(unsigned long size)
 			NUMA_NO_NODE, __builtin_return_address(0));
 }
 
+void * __weak module_alloc_huge(unsigned long size)
+{
+	return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END,
+				    GFP_KERNEL, PAGE_KERNEL_EXEC,
+				    VM_FLUSH_RESET_PERMS | VM_ALLOW_HUGE_VMAP,
+				    NUMA_NO_NODE, __builtin_return_address(0));
+}
+
 bool __weak module_init_section(const char *name)
 {
 	return strstarts(name, ".init");
Signature · 1 line
2.30.2
#7 lore raw

Re: [PATCH v2 bpf 2/3] module: introduce module_alloc_huge

Quoted 3 lines · Song Liu

Introduce module_alloc_huge, which allocates huge page backed memory in module memory space. The primary user of this memory is bpf_prog_pack (multiple BPF programs sharing a huge page).

I kow I lead you downthis road first, but I wonder if we just want to pass a flag to module_alloc instead. This avoids duplicating all the arch overrides.

author reviewer
#8 lore raw

Re: [PATCH v2 bpf 2/3] module: introduce module_alloc_huge

Quoted 9 lines · Christoph Hellwig
Quoted 3 lines · Song Liu

Introduce module_alloc_huge, which allocates huge page backed memory in module memory space. The primary user of this memory is bpf_prog_pack (multiple BPF programs sharing a huge page).

I kow I lead you downthis road first, but I wonder if we just want to pass a flag to module_alloc instead. This avoids duplicating all the arch overrides.

I don't think we will see many archs support bpf_prog_pack, so the __weak version might be good enough for a long time. Adding an argument to module_alloc seems like more trouble to me.

Thanks,
Song

author reviewer
#9 lore raw

PATCH v2 3/3 bpf: use vmalloc with VM_ALLOW_HUGE_VMAP for bpf_prog_pack

In reply to Song Liu

Use __vmalloc_node_range with VM_ALLOW_HUGE_VMAP for bpf_prog_pack so that BPF programs sit on PMD_SIZE pages. This benefits system performance by reducing iTLB miss rate.

---
 kernel/bpf/core.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
kernel/bpf/core.c +5 -4
index 13e9dbeeedf3..fd45bdd80a75 100644
@@ -857,7 +857,7 @@ static size_t select_bpf_prog_pack_size(void)
 	void *ptr;
 
 	size = BPF_HPAGE_SIZE * num_online_nodes();
-	ptr = module_alloc(size);
+	ptr = module_alloc_huge(size);
 
 	/* Test whether we can get huge pages. If not just use PAGE_SIZE
 	 * packs.
@@ -881,7 +881,7 @@ static struct bpf_prog_pack *alloc_new_pack(void)
 		       GFP_KERNEL);
 	if (!pack)
 		return NULL;
-	pack->ptr = module_alloc(bpf_prog_pack_size);
+	pack->ptr = module_alloc_huge(bpf_prog_pack_size);
 	if (!pack->ptr) {
 		kfree(pack);
 		return NULL;
@@ -889,7 +889,6 @@ static struct bpf_prog_pack *alloc_new_pack(void)
 	bitmap_zero(pack->bitmap, bpf_prog_pack_size / BPF_PROG_CHUNK_SIZE);
 	list_add_tail(&pack->list, &pack_list);
 
-	set_vm_flush_reset_perms(pack->ptr);
 	set_memory_ro((unsigned long)pack->ptr, bpf_prog_pack_size / PAGE_SIZE);
 	set_memory_x((unsigned long)pack->ptr, bpf_prog_pack_size / PAGE_SIZE);
 	return pack;
@@ -970,7 +969,9 @@ static void bpf_prog_pack_free(struct bpf_binary_header *hdr)
 	if (bitmap_find_next_zero_area(pack->bitmap, bpf_prog_chunk_count(), 0,
 				       bpf_prog_chunk_count(), 0) == 0) {
 		list_del(&pack->list);
-		module_memfree(pack->ptr);
+		set_memory_nx((unsigned long)pack->ptr, bpf_prog_pack_size / PAGE_SIZE);
+		set_memory_rw((unsigned long)pack->ptr, bpf_prog_pack_size / PAGE_SIZE);
+		vfree(pack->ptr);
 		kfree(pack);
 	}
 out:
Signature · 1 line
2.30.2
#10 lore raw

Re: [PATCH v2 bpf 3/3] bpf: use vmalloc with VM_ALLOW_HUGE_VMAP for bpf_prog_pack

Quoted 1 line · Song Liu

Use __vmalloc_node_range with VM_ALLOW_HUGE_VMAP for bpf_prog_pack so that

That is only very indirectly true now.

author reviewer
#11 lore raw

Re: [PATCH v2 bpf 3/3] bpf: use vmalloc with VM_ALLOW_HUGE_VMAP for bpf_prog_pack

Quoted 5 lines · Christoph Hellwig
Quoted 1 line · Song Liu

Use __vmalloc_node_range with VM_ALLOW_HUGE_VMAP for bpf_prog_pack so that

That is only very indirectly true now.

Yeah, I realized I missed this part after sending it. Will fix.

Thanks,
Song

#12 lore raw

Re: [PATCH v2 bpf 3/3] bpf: use vmalloc with VM_ALLOW_HUGE_VMAP for bpf_prog_pack

In reply to Song Liu
Quoted 11 lines · Song Liu
(quoted diff) +0 -1
@@ -889,7 +889,6 @@ static struct bpf_prog_pack *alloc_new_pack(void)
        bitmap_zero(pack->bitmap, bpf_prog_pack_size /
 BPF_PROG_CHUNK_SIZE);
        list_add_tail(&pack->list, &pack_list);
 
-       set_vm_flush_reset_perms(pack->ptr);
        set_memory_ro((unsigned long)pack->ptr, bpf_prog_pack_size /
 PAGE_SIZE);
        set_memory_x((unsigned long)pack->ptr, bpf_prog_pack_size /
PAGE_SIZE);
        return pack;

Dropping set_vm_flush_reset_perms() is not mentioned in the commit log. It is kind of a fix for a different issue.

Now that x86 supports vmalloc huge pages, but VM_FLUSH_RESET_PERMS does not work with them, we should have some comments or warnings to that effect somewhere. Someone may try to pass the flags in together.

Quoted 14 lines
(quoted diff) +1 -1
@@ -970,7 +969,9 @@ static void bpf_prog_pack_free(struct
 bpf_binary_header *hdr)
        if (bitmap_find_next_zero_area(pack->bitmap,
 bpf_prog_chunk_count(), 0,
                                       bpf_prog_chunk_count(), 0) ==
 0) {
                list_del(&pack->list);
-               module_memfree(pack->ptr);
 
 
+               set_memory_nx((unsigned long)pack->ptr,

bpf_prog_pack_size / PAGE_SIZE);
+ set_memory_rw((unsigned long)pack->ptr,
bpf_prog_pack_size / PAGE_SIZE);

Quoted 2 lines
+               vfree(pack->ptr);
                kfree(pack);

Now that it calls module_alloc_huge() instead of vmalloc_node_range(), should it call module_memfree() instead of vfree()?

Since there are bugs, simple, immediate fixes seem like the right thing to do, but I had a couple long term focused comments on this new feature:

It would be nice if bpf and the other module_alloc() callers could share the same large pages. Meaning, ultimately that this whole thing should probably live outside of bpf. BPF tracing usages might benefit for example, and kprobes and ftrace are not too different than bpf progs from a text allocation perspective.

I agree that the module's part is non-trivial. A while back I had tried to do something like bpf_prog_pack() that worked for all the module_alloc() callers. It had some modules changes to allow different permissions to go to different allocations so they could be made to share large pages:

https://lore.kernel.org/lkml/[email protected]/

I thought the existing kernel special permission allocation methods were just too brittle and intertwined to improve without a new interface. The hope was the new interface could wrap all the arch intricacies instead of leaving them exposed in the cross-arch callers.

I wonder what you think of that general direction or if you have any follow up plans for this?

author reviewer
#13 lore raw

Re: [PATCH v2 bpf 3/3] bpf: use vmalloc with VM_ALLOW_HUGE_VMAP for bpf_prog_pack

Quoted 17 lines · Rick P
Quoted 11 lines · Song Liu
(quoted diff) +0 -1
@@ -889,7 +889,6 @@ static struct bpf_prog_pack *alloc_new_pack(void)
        bitmap_zero(pack->bitmap, bpf_prog_pack_size /
 BPF_PROG_CHUNK_SIZE);
        list_add_tail(&pack->list, &pack_list);
 
-       set_vm_flush_reset_perms(pack->ptr);
        set_memory_ro((unsigned long)pack->ptr, bpf_prog_pack_size /
 PAGE_SIZE);
        set_memory_x((unsigned long)pack->ptr, bpf_prog_pack_size /
PAGE_SIZE);
        return pack;

Dropping set_vm_flush_reset_perms() is not mentioned in the commit log. It is kind of a fix for a different issue.

Quoted 3 lines

Now that x86 supports vmalloc huge pages, but VM_FLUSH_RESET_PERMS does not work with them, we should have some comments or warnings to that effect somewhere. Someone may try to pass the flags in together.

Good catch! I will add it in the next version.

Quoted 18 lines
Quoted 15 lines
(quoted diff) +1 -1
@@ -970,7 +969,9 @@ static void bpf_prog_pack_free(struct
 bpf_binary_header *hdr)
        if (bitmap_find_next_zero_area(pack->bitmap,
 bpf_prog_chunk_count(), 0,
                                       bpf_prog_chunk_count(), 0) ==
 0) {
                list_del(&pack->list);
-               module_memfree(pack->ptr);
 
 
+               set_memory_nx((unsigned long)pack->ptr,

bpf_prog_pack_size / PAGE_SIZE);
+ set_memory_rw((unsigned long)pack->ptr,
bpf_prog_pack_size / PAGE_SIZE);

Quoted 2 lines
+               vfree(pack->ptr);
                kfree(pack);
Quoted 2 lines

Now that it calls module_alloc_huge() instead of vmalloc_node_range(), should it call module_memfree() instead of vfree()?

Right. Let me sort that out. (Also, whether we introduce module_alloc_huge() or not).

Quoted 7 lines

Since there are bugs, simple, immediate fixes seem like the right thing to do, but I had a couple long term focused comments on this new feature:

Quoted 5 lines

It would be nice if bpf and the other module_alloc() callers could share the same large pages. Meaning, ultimately that this whole thing should probably live outside of bpf. BPF tracing usages might benefit for example, and kprobes and ftrace are not too different than bpf progs from a text allocation perspective.

Agreed.

Quoted 9 lines

I agree that the module's part is non-trivial. A while back I had tried to do something like bpf_prog_pack() that worked for all the module_alloc() callers. It had some modules changes to allow different permissions to go to different allocations so they could be made to share large pages:

https://lore.kernel.org/lkml/[email protected]/

Quoted 7 lines

I thought the existing kernel special permission allocation methods were just too brittle and intertwined to improve without a new interface. The hope was the new interface could wrap all the arch intricacies instead of leaving them exposed in the cross-arch callers.

I wonder what you think of that general direction or if you have any follow up plans for this?

Since I am still learning the vmalloc/module_alloc code, I think I am not really capable of commenting on the direction. From our use cases, we do see performance hit due to large number of BPF program fragmenting the page table. Kernel module, OTOH, is not too big an issue for us, as we usually build hot modules into the kernel. That being said, we are interested in making the huge page interface general for BPF program and kernel module. We can commit resources to this effort.

Thanks,
Song

#14 lore raw

Re: [PATCH v2 bpf 3/3] bpf: use vmalloc with VM_ALLOW_HUGE_VMAP for bpf_prog_pack

CC Mike, who has been working on a direct map fragmentation solution.
[0]

Quoted 8 lines · Song Liu

Since I am still learning the vmalloc/module_alloc code, I think I am not really capable of commenting on the direction. From our use cases, we do see performance hit due to large number of BPF program fragmenting the page table. Kernel module, OTOH, is not too big an issue for us, as we usually build hot modules into the kernel. That being said, we are interested in making the huge page interface general for BPF program and kernel module. We can commit resources to this effort.

That sounds great. Please feel free to loop me in if you do.

[0]
https://lore.kernel.org/lkml/[email protected]/

author reviewer
#15 lore raw

PATCH v2 3/3 bpf: use module_alloc_huge for bpf_prog_pack

In reply to Song Liu

module_alloc_huge for bpf_prog_pack so that BPF programs sit on PMD_SIZE pages. This benefits system performance by reducing iTLB miss rate.

---
 kernel/bpf/core.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
kernel/bpf/core.c +5 -4
index 13e9dbeeedf3..fd45bdd80a75 100644
@@ -857,7 +857,7 @@ static size_t select_bpf_prog_pack_size(void)
 	void *ptr;
 
 	size = BPF_HPAGE_SIZE * num_online_nodes();
-	ptr = module_alloc(size);
+	ptr = module_alloc_huge(size);
 
 	/* Test whether we can get huge pages. If not just use PAGE_SIZE
 	 * packs.
@@ -881,7 +881,7 @@ static struct bpf_prog_pack *alloc_new_pack(void)
 		       GFP_KERNEL);
 	if (!pack)
 		return NULL;
-	pack->ptr = module_alloc(bpf_prog_pack_size);
+	pack->ptr = module_alloc_huge(bpf_prog_pack_size);
 	if (!pack->ptr) {
 		kfree(pack);
 		return NULL;
@@ -889,7 +889,6 @@ static struct bpf_prog_pack *alloc_new_pack(void)
 	bitmap_zero(pack->bitmap, bpf_prog_pack_size / BPF_PROG_CHUNK_SIZE);
 	list_add_tail(&pack->list, &pack_list);
 
-	set_vm_flush_reset_perms(pack->ptr);
 	set_memory_ro((unsigned long)pack->ptr, bpf_prog_pack_size / PAGE_SIZE);
 	set_memory_x((unsigned long)pack->ptr, bpf_prog_pack_size / PAGE_SIZE);
 	return pack;
@@ -970,7 +969,9 @@ static void bpf_prog_pack_free(struct bpf_binary_header *hdr)
 	if (bitmap_find_next_zero_area(pack->bitmap, bpf_prog_chunk_count(), 0,
 				       bpf_prog_chunk_count(), 0) == 0) {
 		list_del(&pack->list);
-		module_memfree(pack->ptr);
+		set_memory_nx((unsigned long)pack->ptr, bpf_prog_pack_size / PAGE_SIZE);
+		set_memory_rw((unsigned long)pack->ptr, bpf_prog_pack_size / PAGE_SIZE);
+		vfree(pack->ptr);
 		kfree(pack);
 	}
 out:
Signature · 1 line
2.30.2
author reviewer
#16 lore raw

Re: [PATCH v2 bpf 3/3] bpf: use module_alloc_huge for bpf_prog_pack

Quoted 5 lines · Song Liu

module_alloc_huge for bpf_prog_pack so that BPF programs sit on PMD_SIZE pages. This benefits system performance by reducing iTLB miss rate.

I am really sorry for spamming the list twice. The first set experienced long lag,
so I thought it didn't get through.

I also updated the commit log of 3/3, and this one is the latest version.

Thanks,
Song

Quoted 49 lines
---
 kernel/bpf/core.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
kernel/bpf/core.c +5 -5
index 13e9dbeeedf3..fd45bdd80a75 100644
@@ -857,7 +857,7 @@ static size_t select_bpf_prog_pack_size(void)
        void *ptr;
 
        size = BPF_HPAGE_SIZE * num_online_nodes();
-       ptr = module_alloc(size);
+       ptr = module_alloc_huge(size);
 
        /* Test whether we can get huge pages. If not just use PAGE_SIZE
         * packs.
@@ -881,7 +881,7 @@ static struct bpf_prog_pack *alloc_new_pack(void)
                       GFP_KERNEL);
        if (!pack)
                return NULL;
-       pack->ptr = module_alloc(bpf_prog_pack_size);
+       pack->ptr = module_alloc_huge(bpf_prog_pack_size);
        if (!pack->ptr) {
                kfree(pack);
                return NULL;
@@ -889,7 +889,6 @@ static struct bpf_prog_pack *alloc_new_pack(void)
        bitmap_zero(pack->bitmap, bpf_prog_pack_size / BPF_PROG_CHUNK_SIZE);
        list_add_tail(&pack->list, &pack_list);
 
-       set_vm_flush_reset_perms(pack->ptr);
        set_memory_ro((unsigned long)pack->ptr, bpf_prog_pack_size / PAGE_SIZE);
        set_memory_x((unsigned long)pack->ptr, bpf_prog_pack_size / PAGE_SIZE);
        return pack;
@@ -970,7 +969,9 @@ static void bpf_prog_pack_free(struct bpf_binary_header *hdr)
        if (bitmap_find_next_zero_area(pack->bitmap, bpf_prog_chunk_count(), 0,
                                       bpf_prog_chunk_count(), 0) == 0) {
                list_del(&pack->list);
-               module_memfree(pack->ptr);
+               set_memory_nx((unsigned long)pack->ptr, bpf_prog_pack_size / PAGE_SIZE);
+               set_memory_rw((unsigned long)pack->ptr, bpf_prog_pack_size / PAGE_SIZE);
+               vfree(pack->ptr);
                kfree(pack);
        }
 out:
--

2.30.2