* [PATCH v3 0/4] make vmalloc gfp flags usage more apparent
@ 2025-11-17 17:35 Vishal Moola (Oracle)
2025-11-17 17:35 ` [PATCH v3 1/4] mm/vmalloc: warn on invalid vmalloc gfp flags Vishal Moola (Oracle)
` (4 more replies)
0 siblings, 5 replies; 14+ messages in thread
From: Vishal Moola (Oracle) @ 2025-11-17 17:35 UTC (permalink / raw)
To: linux-kernel, linux-mm, bpf
Cc: Uladzislau Rezki, Christoph Hellwig, Andrew Morton,
Vishal Moola (Oracle)
We should do a better job at enforcing gfp flags for vmalloc. Right now, we
have a kernel-doc for __vmalloc_node_range(), and hope callers pass in
supported flags. If a caller were to pass in an unsupported flag, we may
BUG, silently clear it, or completely ignore it.
If we are more proactive about enforcing gfp flags, we can making sure
callers know when they may be asking for unsupported behavior.
This patchset lets vmalloc control the incoming gfp flags, and cleans up
some hard to read gfp code.
---
Linked rfc [1] and rfc v2[2] for convenience.
Patch v2 -> v3:
Only changes the whitelist mask and comment in patch 1:
- Replace __GFP_HARDWALL with GFP_USER
- Add GFP_KERNEL_ACCOUNT[4]
- Add GFP_NOFS and GFP_NOIO just so all supported flags are explicitly
listed in the mask.
v2:
- Add __GFP_HARDWALL[3] for bpf and drm users.
- cc BPF mailing list
RFC -> PATCH:
- Collected review tags (Patches 1 & 4)
- Add unlikely keyword to help the compiler
- Replace pr_warn() with WARN(1)
RFC v2:
- Whitelist supported gfp flags instead of blacklisting the unsupported
- Move the flags check up to the only exported functions that accept
flags:
__vmalloc_noprof() and vmalloc_huge_node_prof()
[1] https://lore.kernel.org/linux-mm/20251030164330.44995-1-vishal.moola@gmail.com/
[2] https://lore.kernel.org/linux-mm/20251103190429.104747-1-vishal.moola@gmail.com/
[3] https://lore.kernel.org/linux-mm/20251110160457.61791-1-vishal.moola@gmail.com/T/#me8b548520ce9c81a5099c00abe53dd248c16eae7
[4] https://lore.kernel.org/linux-mm/69158bb1.a70a0220.3124cb.001e.GAE@google.com/
Vishal Moola (Oracle) (4):
mm/vmalloc: warn on invalid vmalloc gfp flags
mm/vmalloc: Add a helper to optimize vmalloc allocation gfps
mm/vmalloc: cleanup large_gfp in vm_area_alloc_pages()
mm/vmalloc: cleanup gfp flag use in new_vmap_block()
mm/vmalloc.c | 50 ++++++++++++++++++++++++++++++++++++++++++--------
1 file changed, 42 insertions(+), 8 deletions(-)
--
2.51.1
^ permalink raw reply [flat|nested] 14+ messages in thread
* [PATCH v3 1/4] mm/vmalloc: warn on invalid vmalloc gfp flags
2025-11-17 17:35 [PATCH v3 0/4] make vmalloc gfp flags usage more apparent Vishal Moola (Oracle)
@ 2025-11-17 17:35 ` Vishal Moola (Oracle)
2025-11-18 9:34 ` Uladzislau Rezki
2025-11-18 22:44 ` Nathan Chancellor
2025-11-17 17:35 ` [PATCH v3 2/4] mm/vmalloc: Add a helper to optimize vmalloc allocation gfps Vishal Moola (Oracle)
` (3 subsequent siblings)
4 siblings, 2 replies; 14+ messages in thread
From: Vishal Moola (Oracle) @ 2025-11-17 17:35 UTC (permalink / raw)
To: linux-kernel, linux-mm, bpf
Cc: Uladzislau Rezki, Christoph Hellwig, Andrew Morton,
Vishal Moola (Oracle),
Christoph Hellwig
Vmalloc explicitly supports a list of flags, but we never enforce them.
vmalloc has been trying to handle unsupported flags by clearing and
setting flags wherever necessary. This is messy and makes the code
harder to understand, when we could simply check for a supported input
immediately instead.
Define a helper mask and function telling callers they have passed in
invalid flags, and clear those unsupported vmalloc flags.
Suggested-by: Christoph Hellwig <hch@infradead.org>
Signed-off-by: Vishal Moola (Oracle) <vishal.moola@gmail.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
---
mm/vmalloc.c | 26 ++++++++++++++++++++++++++
1 file changed, 26 insertions(+)
diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index 0832f944544c..5dc467c6cab4 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -3911,6 +3911,28 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
return NULL;
}
+/*
+ * See __vmalloc_node_range() for a clear list of supported vmalloc flags.
+ * This gfp lists all flags currently passed through vmalloc. Currently,
+ * __GFP_ZERO is used by BPF and __GFP_NORETRY is used by percpu. Both drm
+ * and BPF also use GFP_USER. Additionally, various users pass
+ * GFP_KERNEL_ACCOUNT.
+ */
+#define GFP_VMALLOC_SUPPORTED (GFP_KERNEL | GFP_ATOMIC | GFP_NOWAIT |\
+ __GFP_NOFAIL | __GFP_ZERO | __GFP_NORETRY |\
+ GFP_NOFS | GFP_NOIO | GFP_KERNEL_ACCOUNT |\
+ GFP_USER)
+
+static gfp_t vmalloc_fix_flags(gfp_t flags)
+{
+ gfp_t invalid_mask = flags & ~GFP_VMALLOC_SUPPORTED;
+
+ flags &= GFP_VMALLOC_SUPPORTED;
+ WARN(1, "Unexpected gfp: %#x (%pGg). Fixing up to gfp: %#x (%pGg). Fix your code!\n",
+ invalid_mask, &invalid_mask, flags, &flags);
+ return flags;
+}
+
/**
* __vmalloc_node_range - allocate virtually contiguous memory
* @size: allocation size
@@ -4092,6 +4114,8 @@ EXPORT_SYMBOL_GPL(__vmalloc_node_noprof);
void *__vmalloc_noprof(unsigned long size, gfp_t gfp_mask)
{
+ if (unlikely(gfp_mask & ~GFP_VMALLOC_SUPPORTED))
+ gfp_mask = vmalloc_fix_flags(gfp_mask);
return __vmalloc_node_noprof(size, 1, gfp_mask, NUMA_NO_NODE,
__builtin_return_address(0));
}
@@ -4131,6 +4155,8 @@ EXPORT_SYMBOL(vmalloc_noprof);
*/
void *vmalloc_huge_node_noprof(unsigned long size, gfp_t gfp_mask, int node)
{
+ if (unlikely(gfp_mask & ~GFP_VMALLOC_SUPPORTED))
+ gfp_mask = vmalloc_fix_flags(gfp_mask);
return __vmalloc_node_range_noprof(size, 1, VMALLOC_START, VMALLOC_END,
gfp_mask, PAGE_KERNEL, VM_ALLOW_HUGE_VMAP,
node, __builtin_return_address(0));
--
2.51.1
^ permalink raw reply [flat|nested] 14+ messages in thread
* [PATCH v3 2/4] mm/vmalloc: Add a helper to optimize vmalloc allocation gfps
2025-11-17 17:35 [PATCH v3 0/4] make vmalloc gfp flags usage more apparent Vishal Moola (Oracle)
2025-11-17 17:35 ` [PATCH v3 1/4] mm/vmalloc: warn on invalid vmalloc gfp flags Vishal Moola (Oracle)
@ 2025-11-17 17:35 ` Vishal Moola (Oracle)
2025-11-18 9:35 ` Uladzislau Rezki
2025-11-17 17:35 ` [PATCH v3 3/4] mm/vmalloc: cleanup large_gfp in vm_area_alloc_pages() Vishal Moola (Oracle)
` (2 subsequent siblings)
4 siblings, 1 reply; 14+ messages in thread
From: Vishal Moola (Oracle) @ 2025-11-17 17:35 UTC (permalink / raw)
To: linux-kernel, linux-mm, bpf
Cc: Uladzislau Rezki, Christoph Hellwig, Andrew Morton,
Vishal Moola (Oracle)
vm_area_alloc_pages() attempts to use different gfp flags as a way
to optimize allocations. This has been done inline which makes things
harder to read.
Add a helper function to make the code more readable.
Signed-off-by: Vishal Moola (Oracle) <vishal.moola@gmail.com>
---
mm/vmalloc.c | 17 ++++++++++++++---
1 file changed, 14 insertions(+), 3 deletions(-)
diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index 5dc467c6cab4..0929f4f53ffe 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -3614,6 +3614,17 @@ void *vmap_pfn(unsigned long *pfns, unsigned int count, pgprot_t prot)
EXPORT_SYMBOL_GPL(vmap_pfn);
#endif /* CONFIG_VMAP_PFN */
+/*
+ * Helper for vmalloc to adjust the gfp flags for certain allocations.
+ */
+static inline gfp_t vmalloc_gfp_adjust(gfp_t flags, const bool large)
+{
+ flags |= __GFP_NOWARN;
+ if (large)
+ flags &= ~__GFP_NOFAIL;
+ return flags;
+}
+
static inline unsigned int
vm_area_alloc_pages(gfp_t gfp, int nid,
unsigned int order, unsigned int nr_pages, struct page **pages)
@@ -3852,9 +3863,9 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
* Please note, the __vmalloc_node_range_noprof() falls-back
* to order-0 pages if high-order attempt is unsuccessful.
*/
- area->nr_pages = vm_area_alloc_pages((page_order ?
- gfp_mask & ~__GFP_NOFAIL : gfp_mask) | __GFP_NOWARN,
- node, page_order, nr_small_pages, area->pages);
+ area->nr_pages = vm_area_alloc_pages(
+ vmalloc_gfp_adjust(gfp_mask, page_order), node,
+ page_order, nr_small_pages, area->pages);
atomic_long_add(area->nr_pages, &nr_vmalloc_pages);
/* All pages of vm should be charged to same memcg, so use first one. */
--
2.51.1
^ permalink raw reply [flat|nested] 14+ messages in thread
* [PATCH v3 3/4] mm/vmalloc: cleanup large_gfp in vm_area_alloc_pages()
2025-11-17 17:35 [PATCH v3 0/4] make vmalloc gfp flags usage more apparent Vishal Moola (Oracle)
2025-11-17 17:35 ` [PATCH v3 1/4] mm/vmalloc: warn on invalid vmalloc gfp flags Vishal Moola (Oracle)
2025-11-17 17:35 ` [PATCH v3 2/4] mm/vmalloc: Add a helper to optimize vmalloc allocation gfps Vishal Moola (Oracle)
@ 2025-11-17 17:35 ` Vishal Moola (Oracle)
2025-11-18 9:35 ` Uladzislau Rezki
2025-11-17 17:35 ` [PATCH v3 4/4] mm/vmalloc: cleanup gfp flag use in new_vmap_block() Vishal Moola (Oracle)
2025-11-20 1:03 ` [PATCH v3 0/4] make vmalloc gfp flags usage more apparent SeongJae Park
4 siblings, 1 reply; 14+ messages in thread
From: Vishal Moola (Oracle) @ 2025-11-17 17:35 UTC (permalink / raw)
To: linux-kernel, linux-mm, bpf
Cc: Uladzislau Rezki, Christoph Hellwig, Andrew Morton,
Vishal Moola (Oracle)
Now that we have already checked for unsupported flags, we can use the
helper function to set the necessary gfp flags for the large order
allocation optimization.
Signed-off-by: Vishal Moola (Oracle) <vishal.moola@gmail.com>
---
mm/vmalloc.c | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index 0929f4f53ffe..d343db806170 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -3634,10 +3634,8 @@ vm_area_alloc_pages(gfp_t gfp, int nid,
unsigned int max_attempt_order = MAX_PAGE_ORDER;
struct page *page;
int i;
- gfp_t large_gfp = (gfp &
- ~(__GFP_DIRECT_RECLAIM | __GFP_NOFAIL | __GFP_COMP))
- | __GFP_NOWARN;
unsigned int large_order = ilog2(nr_remaining);
+ gfp_t large_gfp = vmalloc_gfp_adjust(gfp, large_order) & ~__GFP_DIRECT_RECLAIM;
large_order = min(max_attempt_order, large_order);
--
2.51.1
^ permalink raw reply [flat|nested] 14+ messages in thread
* [PATCH v3 4/4] mm/vmalloc: cleanup gfp flag use in new_vmap_block()
2025-11-17 17:35 [PATCH v3 0/4] make vmalloc gfp flags usage more apparent Vishal Moola (Oracle)
` (2 preceding siblings ...)
2025-11-17 17:35 ` [PATCH v3 3/4] mm/vmalloc: cleanup large_gfp in vm_area_alloc_pages() Vishal Moola (Oracle)
@ 2025-11-17 17:35 ` Vishal Moola (Oracle)
2025-11-18 9:36 ` Uladzislau Rezki
2025-11-20 1:03 ` [PATCH v3 0/4] make vmalloc gfp flags usage more apparent SeongJae Park
4 siblings, 1 reply; 14+ messages in thread
From: Vishal Moola (Oracle) @ 2025-11-17 17:35 UTC (permalink / raw)
To: linux-kernel, linux-mm, bpf
Cc: Uladzislau Rezki, Christoph Hellwig, Andrew Morton,
Vishal Moola (Oracle)
The only caller, vb_alloc(), passes GFP_KERNEL into new_vmap_block()
which is a subset of GFP_RECLAIM_MASK. Since there's no reason to use
this mask here, remove it.
Signed-off-by: Vishal Moola (Oracle) <vishal.moola@gmail.com>
Reviewed-by: Uladzislau Rezki (Sony) <urezki@gmail.com>
---
mm/vmalloc.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index d343db806170..d55a77977762 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -2699,8 +2699,7 @@ static void *new_vmap_block(unsigned int order, gfp_t gfp_mask)
node = numa_node_id();
- vb = kmalloc_node(sizeof(struct vmap_block),
- gfp_mask & GFP_RECLAIM_MASK, node);
+ vb = kmalloc_node(sizeof(struct vmap_block), gfp_mask, node);
if (unlikely(!vb))
return ERR_PTR(-ENOMEM);
--
2.51.1
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH v3 1/4] mm/vmalloc: warn on invalid vmalloc gfp flags
2025-11-17 17:35 ` [PATCH v3 1/4] mm/vmalloc: warn on invalid vmalloc gfp flags Vishal Moola (Oracle)
@ 2025-11-18 9:34 ` Uladzislau Rezki
2025-11-18 22:44 ` Nathan Chancellor
1 sibling, 0 replies; 14+ messages in thread
From: Uladzislau Rezki @ 2025-11-18 9:34 UTC (permalink / raw)
To: Vishal Moola (Oracle)
Cc: linux-kernel, linux-mm, bpf, Uladzislau Rezki, Christoph Hellwig,
Andrew Morton, Christoph Hellwig
On Mon, Nov 17, 2025 at 09:35:27AM -0800, Vishal Moola (Oracle) wrote:
> Vmalloc explicitly supports a list of flags, but we never enforce them.
> vmalloc has been trying to handle unsupported flags by clearing and
> setting flags wherever necessary. This is messy and makes the code
> harder to understand, when we could simply check for a supported input
> immediately instead.
>
> Define a helper mask and function telling callers they have passed in
> invalid flags, and clear those unsupported vmalloc flags.
>
> Suggested-by: Christoph Hellwig <hch@infradead.org>
> Signed-off-by: Vishal Moola (Oracle) <vishal.moola@gmail.com>
> Reviewed-by: Christoph Hellwig <hch@lst.de>
> ---
> mm/vmalloc.c | 26 ++++++++++++++++++++++++++
> 1 file changed, 26 insertions(+)
>
> diff --git a/mm/vmalloc.c b/mm/vmalloc.c
> index 0832f944544c..5dc467c6cab4 100644
> --- a/mm/vmalloc.c
> +++ b/mm/vmalloc.c
> @@ -3911,6 +3911,28 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
> return NULL;
> }
>
> +/*
> + * See __vmalloc_node_range() for a clear list of supported vmalloc flags.
> + * This gfp lists all flags currently passed through vmalloc. Currently,
> + * __GFP_ZERO is used by BPF and __GFP_NORETRY is used by percpu. Both drm
> + * and BPF also use GFP_USER. Additionally, various users pass
> + * GFP_KERNEL_ACCOUNT.
> + */
> +#define GFP_VMALLOC_SUPPORTED (GFP_KERNEL | GFP_ATOMIC | GFP_NOWAIT |\
> + __GFP_NOFAIL | __GFP_ZERO | __GFP_NORETRY |\
> + GFP_NOFS | GFP_NOIO | GFP_KERNEL_ACCOUNT |\
> + GFP_USER)
> +
> +static gfp_t vmalloc_fix_flags(gfp_t flags)
> +{
> + gfp_t invalid_mask = flags & ~GFP_VMALLOC_SUPPORTED;
> +
> + flags &= GFP_VMALLOC_SUPPORTED;
> + WARN(1, "Unexpected gfp: %#x (%pGg). Fixing up to gfp: %#x (%pGg). Fix your code!\n",
> + invalid_mask, &invalid_mask, flags, &flags);
> + return flags;
> +}
> +
> /**
> * __vmalloc_node_range - allocate virtually contiguous memory
> * @size: allocation size
> @@ -4092,6 +4114,8 @@ EXPORT_SYMBOL_GPL(__vmalloc_node_noprof);
>
> void *__vmalloc_noprof(unsigned long size, gfp_t gfp_mask)
> {
> + if (unlikely(gfp_mask & ~GFP_VMALLOC_SUPPORTED))
> + gfp_mask = vmalloc_fix_flags(gfp_mask);
> return __vmalloc_node_noprof(size, 1, gfp_mask, NUMA_NO_NODE,
> __builtin_return_address(0));
> }
> @@ -4131,6 +4155,8 @@ EXPORT_SYMBOL(vmalloc_noprof);
> */
> void *vmalloc_huge_node_noprof(unsigned long size, gfp_t gfp_mask, int node)
> {
> + if (unlikely(gfp_mask & ~GFP_VMALLOC_SUPPORTED))
> + gfp_mask = vmalloc_fix_flags(gfp_mask);
> return __vmalloc_node_range_noprof(size, 1, VMALLOC_START, VMALLOC_END,
> gfp_mask, PAGE_KERNEL, VM_ALLOW_HUGE_VMAP,
> node, __builtin_return_address(0));
> --
> 2.51.1
>
Reviewed-by: "Uladzislau Rezki (Sony)" <urezki@gmail.com>
--
Uladzislau Rezki
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH v3 2/4] mm/vmalloc: Add a helper to optimize vmalloc allocation gfps
2025-11-17 17:35 ` [PATCH v3 2/4] mm/vmalloc: Add a helper to optimize vmalloc allocation gfps Vishal Moola (Oracle)
@ 2025-11-18 9:35 ` Uladzislau Rezki
0 siblings, 0 replies; 14+ messages in thread
From: Uladzislau Rezki @ 2025-11-18 9:35 UTC (permalink / raw)
To: Vishal Moola (Oracle)
Cc: linux-kernel, linux-mm, bpf, Uladzislau Rezki, Christoph Hellwig,
Andrew Morton
On Mon, Nov 17, 2025 at 09:35:28AM -0800, Vishal Moola (Oracle) wrote:
> vm_area_alloc_pages() attempts to use different gfp flags as a way
> to optimize allocations. This has been done inline which makes things
> harder to read.
>
> Add a helper function to make the code more readable.
>
> Signed-off-by: Vishal Moola (Oracle) <vishal.moola@gmail.com>
> ---
> mm/vmalloc.c | 17 ++++++++++++++---
> 1 file changed, 14 insertions(+), 3 deletions(-)
>
> diff --git a/mm/vmalloc.c b/mm/vmalloc.c
> index 5dc467c6cab4..0929f4f53ffe 100644
> --- a/mm/vmalloc.c
> +++ b/mm/vmalloc.c
> @@ -3614,6 +3614,17 @@ void *vmap_pfn(unsigned long *pfns, unsigned int count, pgprot_t prot)
> EXPORT_SYMBOL_GPL(vmap_pfn);
> #endif /* CONFIG_VMAP_PFN */
>
> +/*
> + * Helper for vmalloc to adjust the gfp flags for certain allocations.
> + */
> +static inline gfp_t vmalloc_gfp_adjust(gfp_t flags, const bool large)
> +{
> + flags |= __GFP_NOWARN;
> + if (large)
> + flags &= ~__GFP_NOFAIL;
> + return flags;
> +}
> +
> static inline unsigned int
> vm_area_alloc_pages(gfp_t gfp, int nid,
> unsigned int order, unsigned int nr_pages, struct page **pages)
> @@ -3852,9 +3863,9 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
> * Please note, the __vmalloc_node_range_noprof() falls-back
> * to order-0 pages if high-order attempt is unsuccessful.
> */
> - area->nr_pages = vm_area_alloc_pages((page_order ?
> - gfp_mask & ~__GFP_NOFAIL : gfp_mask) | __GFP_NOWARN,
> - node, page_order, nr_small_pages, area->pages);
> + area->nr_pages = vm_area_alloc_pages(
> + vmalloc_gfp_adjust(gfp_mask, page_order), node,
> + page_order, nr_small_pages, area->pages);
>
> atomic_long_add(area->nr_pages, &nr_vmalloc_pages);
> /* All pages of vm should be charged to same memcg, so use first one. */
> --
> 2.51.1
>
Reviewed-by: "Uladzislau Rezki (Sony)" <urezki@gmail.com>
--
Uladzislau Rezki
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH v3 3/4] mm/vmalloc: cleanup large_gfp in vm_area_alloc_pages()
2025-11-17 17:35 ` [PATCH v3 3/4] mm/vmalloc: cleanup large_gfp in vm_area_alloc_pages() Vishal Moola (Oracle)
@ 2025-11-18 9:35 ` Uladzislau Rezki
0 siblings, 0 replies; 14+ messages in thread
From: Uladzislau Rezki @ 2025-11-18 9:35 UTC (permalink / raw)
To: Vishal Moola (Oracle)
Cc: linux-kernel, linux-mm, bpf, Uladzislau Rezki, Christoph Hellwig,
Andrew Morton
On Mon, Nov 17, 2025 at 09:35:29AM -0800, Vishal Moola (Oracle) wrote:
> Now that we have already checked for unsupported flags, we can use the
> helper function to set the necessary gfp flags for the large order
> allocation optimization.
>
> Signed-off-by: Vishal Moola (Oracle) <vishal.moola@gmail.com>
> ---
> mm/vmalloc.c | 4 +---
> 1 file changed, 1 insertion(+), 3 deletions(-)
>
> diff --git a/mm/vmalloc.c b/mm/vmalloc.c
> index 0929f4f53ffe..d343db806170 100644
> --- a/mm/vmalloc.c
> +++ b/mm/vmalloc.c
> @@ -3634,10 +3634,8 @@ vm_area_alloc_pages(gfp_t gfp, int nid,
> unsigned int max_attempt_order = MAX_PAGE_ORDER;
> struct page *page;
> int i;
> - gfp_t large_gfp = (gfp &
> - ~(__GFP_DIRECT_RECLAIM | __GFP_NOFAIL | __GFP_COMP))
> - | __GFP_NOWARN;
> unsigned int large_order = ilog2(nr_remaining);
> + gfp_t large_gfp = vmalloc_gfp_adjust(gfp, large_order) & ~__GFP_DIRECT_RECLAIM;
>
> large_order = min(max_attempt_order, large_order);
>
> --
> 2.51.1
>
Reviewed-by: "Uladzislau Rezki (Sony)" <urezki@gmail.com>
--
Uladzislau Rezki
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH v3 4/4] mm/vmalloc: cleanup gfp flag use in new_vmap_block()
2025-11-17 17:35 ` [PATCH v3 4/4] mm/vmalloc: cleanup gfp flag use in new_vmap_block() Vishal Moola (Oracle)
@ 2025-11-18 9:36 ` Uladzislau Rezki
0 siblings, 0 replies; 14+ messages in thread
From: Uladzislau Rezki @ 2025-11-18 9:36 UTC (permalink / raw)
To: Vishal Moola (Oracle)
Cc: linux-kernel, linux-mm, bpf, Uladzislau Rezki, Christoph Hellwig,
Andrew Morton
On Mon, Nov 17, 2025 at 09:35:30AM -0800, Vishal Moola (Oracle) wrote:
> The only caller, vb_alloc(), passes GFP_KERNEL into new_vmap_block()
> which is a subset of GFP_RECLAIM_MASK. Since there's no reason to use
> this mask here, remove it.
>
> Signed-off-by: Vishal Moola (Oracle) <vishal.moola@gmail.com>
> Reviewed-by: Uladzislau Rezki (Sony) <urezki@gmail.com>
> ---
> mm/vmalloc.c | 3 +--
> 1 file changed, 1 insertion(+), 2 deletions(-)
>
> diff --git a/mm/vmalloc.c b/mm/vmalloc.c
> index d343db806170..d55a77977762 100644
> --- a/mm/vmalloc.c
> +++ b/mm/vmalloc.c
> @@ -2699,8 +2699,7 @@ static void *new_vmap_block(unsigned int order, gfp_t gfp_mask)
>
> node = numa_node_id();
>
> - vb = kmalloc_node(sizeof(struct vmap_block),
> - gfp_mask & GFP_RECLAIM_MASK, node);
> + vb = kmalloc_node(sizeof(struct vmap_block), gfp_mask, node);
> if (unlikely(!vb))
> return ERR_PTR(-ENOMEM);
>
> --
> 2.51.1
>
Reviewed-by: "Uladzislau Rezki (Sony)" <urezki@gmail.com>
--
Uladzislau Rezki
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH v3 1/4] mm/vmalloc: warn on invalid vmalloc gfp flags
2025-11-17 17:35 ` [PATCH v3 1/4] mm/vmalloc: warn on invalid vmalloc gfp flags Vishal Moola (Oracle)
2025-11-18 9:34 ` Uladzislau Rezki
@ 2025-11-18 22:44 ` Nathan Chancellor
2025-11-19 0:54 ` Oliver Upton
2025-11-19 5:44 ` Christoph Hellwig
1 sibling, 2 replies; 14+ messages in thread
From: Nathan Chancellor @ 2025-11-18 22:44 UTC (permalink / raw)
To: Vishal Moola (Oracle)
Cc: linux-kernel, linux-mm, bpf, Uladzislau Rezki, Christoph Hellwig,
Andrew Morton, Christoph Hellwig, kvmarm
Hi Vishal,
On Mon, Nov 17, 2025 at 09:35:27AM -0800, Vishal Moola (Oracle) wrote:
> Vmalloc explicitly supports a list of flags, but we never enforce them.
> vmalloc has been trying to handle unsupported flags by clearing and
> setting flags wherever necessary. This is messy and makes the code
> harder to understand, when we could simply check for a supported input
> immediately instead.
>
> Define a helper mask and function telling callers they have passed in
> invalid flags, and clear those unsupported vmalloc flags.
>
> Suggested-by: Christoph Hellwig <hch@infradead.org>
> Signed-off-by: Vishal Moola (Oracle) <vishal.moola@gmail.com>
> Reviewed-by: Christoph Hellwig <hch@lst.de>
> ---
> mm/vmalloc.c | 26 ++++++++++++++++++++++++++
> 1 file changed, 26 insertions(+)
>
> diff --git a/mm/vmalloc.c b/mm/vmalloc.c
> index 0832f944544c..5dc467c6cab4 100644
> --- a/mm/vmalloc.c
> +++ b/mm/vmalloc.c
> @@ -3911,6 +3911,28 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
> return NULL;
> }
>
> +/*
> + * See __vmalloc_node_range() for a clear list of supported vmalloc flags.
> + * This gfp lists all flags currently passed through vmalloc. Currently,
> + * __GFP_ZERO is used by BPF and __GFP_NORETRY is used by percpu. Both drm
> + * and BPF also use GFP_USER. Additionally, various users pass
> + * GFP_KERNEL_ACCOUNT.
> + */
> +#define GFP_VMALLOC_SUPPORTED (GFP_KERNEL | GFP_ATOMIC | GFP_NOWAIT |\
> + __GFP_NOFAIL | __GFP_ZERO | __GFP_NORETRY |\
> + GFP_NOFS | GFP_NOIO | GFP_KERNEL_ACCOUNT |\
> + GFP_USER)
> +
> +static gfp_t vmalloc_fix_flags(gfp_t flags)
> +{
> + gfp_t invalid_mask = flags & ~GFP_VMALLOC_SUPPORTED;
> +
> + flags &= GFP_VMALLOC_SUPPORTED;
> + WARN(1, "Unexpected gfp: %#x (%pGg). Fixing up to gfp: %#x (%pGg). Fix your code!\n",
> + invalid_mask, &invalid_mask, flags, &flags);
> + return flags;
> +}
I am seeing this warning trigger when starting a VM on one of my arm64
boxes.
[ 6345.145795] ------------[ cut here ]------------
[ 6345.145803] Unexpected gfp: 0x2 (__GFP_HIGHMEM). Fixing up to gfp: 0x400dc0 (GFP_KERNEL_ACCOUNT|__GFP_ZERO). Fix your code!
[ 6345.145819] WARNING: mm/vmalloc.c:3940 at vmalloc_fix_flags+0x60/0x90, CPU#32: qemu-system-aar/4325
[ 6345.176990] Modules linked in: ...
[ 6345.254421] CPU: 32 UID: 1000 PID: 4325 Comm: qemu-system-aar Not tainted 6.18.0-rc6-next-20251118-00002-g2331e73a4769 #1 PREEMPT(voluntary)
[ 6345.267101] Hardware name: To be filled by O.E.M Ampere Altra Developer Platform/Ampere Altra Developer Platform, BIOS TianoCore 2.10.100.02 (SYS: 2.10.20
[ 6345.280907] pstate: 60400009 (nZCv daif +PAN -UAO -TCO -DIT -SSBS BTYPE=--)
[ 6345.287856] pc : vmalloc_fix_flags+0x60/0x90
[ 6345.292115] lr : vmalloc_fix_flags+0x58/0x90
[ 6345.296374] sp : ffff80008d5b3a10
[ 6345.299676] x29: ffff80008d5b3a20 x28: ffff07ff8a148000 x27: 0000000000000000
[ 6345.306800] x26: 0000000000000000 x25: 0000000000000000 x24: 0000000000000000
[ 6345.313923] x23: 000000000000000b x22: 000000000000ae01 x21: 0000000000000028
[ 6345.321047] x20: ffffc682ea643fbc x19: 0000000000001040 x18: ffff800083465050
[ 6345.328170] x17: 00000000d949c370 x16: 00000000d949c370 x15: 0000000000000004
[ 6345.335294] x14: 0000000000000002 x13: 0000000000000002 x12: 0000000000000000
[ 6345.342417] x11: 0000000000000001 x10: ffffc682ec577744 x9 : bf6a46a6bf3b7900
[ 6345.349541] x8 : bf6a46a6bf3b7900 x7 : 65646f632072756f x6 : 7920786946202e29
[ 6345.356664] x5 : ffff081f6fcdc678 x4 : 0000000000000000 x3 : ffff80008d5b3688
[ 6345.363788] x2 : 0000000000000021 x1 : 0000000000000000 x0 : 000000000000006f
[ 6345.370911] Call trace:
[ 6345.373346] vmalloc_fix_flags+0x60/0x90 (P)
[ 6345.377606] __vmalloc_noprof+0xa0/0xb0
[ 6345.381431] kvm_arch_alloc_vm+0x64/0x70
[ 6345.385344] kvm_dev_ioctl+0x9c/0x58c
[ 6345.388997] __arm64_sys_ioctl+0xb0/0x100
[ 6345.392995] invoke_syscall+0x84/0xf4
[ 6345.396648] el0_svc_common.llvm.4390888008543260363+0x90/0xf4
[ 6345.402469] do_el0_svc+0x2c/0x3c
[ 6345.405773] el0_svc+0x54/0x2a8
[ 6345.408905] el0t_64_sync_handler+0x88/0x134
[ 6345.413164] el0t_64_sync+0x1b8/0x1bc
[ 6345.416815] ---[ end trace 0000000000000000 ]---
where kvm_arch_alloc_vm() from arch/arm64/kvm/arm.c is
struct kvm *kvm_arch_alloc_vm(void)
{
size_t sz = sizeof(struct kvm);
if (!has_vhe())
return kzalloc(sz, GFP_KERNEL_ACCOUNT);
return __vmalloc(sz, GFP_KERNEL_ACCOUNT | __GFP_HIGHMEM | __GFP_ZERO);
}
Should __GFP_HIGHMEM be dropped from the call to __vmalloc? It looks
like it was added by commit 115bae923ac8 ("KVM: arm64: Add memcg
accounting to KVM allocations") back in 5.16.
Cheers,
Nathan
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH v3 1/4] mm/vmalloc: warn on invalid vmalloc gfp flags
2025-11-18 22:44 ` Nathan Chancellor
@ 2025-11-19 0:54 ` Oliver Upton
2025-11-19 5:44 ` Christoph Hellwig
1 sibling, 0 replies; 14+ messages in thread
From: Oliver Upton @ 2025-11-19 0:54 UTC (permalink / raw)
To: Nathan Chancellor
Cc: Vishal Moola (Oracle),
linux-kernel, linux-mm, bpf, Uladzislau Rezki, Christoph Hellwig,
Andrew Morton, Christoph Hellwig, kvmarm
Hi Nathan,
Thanks for reporting this.
On Tue, Nov 18, 2025 at 03:44:48PM -0700, Nathan Chancellor wrote:
> where kvm_arch_alloc_vm() from arch/arm64/kvm/arm.c is
>
> struct kvm *kvm_arch_alloc_vm(void)
> {
> size_t sz = sizeof(struct kvm);
>
> if (!has_vhe())
> return kzalloc(sz, GFP_KERNEL_ACCOUNT);
>
> return __vmalloc(sz, GFP_KERNEL_ACCOUNT | __GFP_HIGHMEM | __GFP_ZERO);
> }
>
> Should __GFP_HIGHMEM be dropped from the call to __vmalloc? It looks
> like it was added by commit 115bae923ac8 ("KVM: arm64: Add memcg
> accounting to KVM allocations") back in 5.16.
Yep. May as well switch to kvzalloc() while we're at it.
Thanks,
Oliver
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH v3 1/4] mm/vmalloc: warn on invalid vmalloc gfp flags
2025-11-18 22:44 ` Nathan Chancellor
2025-11-19 0:54 ` Oliver Upton
@ 2025-11-19 5:44 ` Christoph Hellwig
1 sibling, 0 replies; 14+ messages in thread
From: Christoph Hellwig @ 2025-11-19 5:44 UTC (permalink / raw)
To: Nathan Chancellor
Cc: Vishal Moola (Oracle),
linux-kernel, linux-mm, bpf, Uladzislau Rezki, Christoph Hellwig,
Andrew Morton, Christoph Hellwig, kvmarm
On Tue, Nov 18, 2025 at 03:44:48PM -0700, Nathan Chancellor wrote:
> where kvm_arch_alloc_vm() from arch/arm64/kvm/arm.c is
>
> struct kvm *kvm_arch_alloc_vm(void)
> {
> size_t sz = sizeof(struct kvm);
>
> if (!has_vhe())
> return kzalloc(sz, GFP_KERNEL_ACCOUNT);
>
> return __vmalloc(sz, GFP_KERNEL_ACCOUNT | __GFP_HIGHMEM | __GFP_ZERO);
> }
>
> Should __GFP_HIGHMEM be dropped from the call to __vmalloc?
Yes. vmalloc uses highmem internally where useful (on arm64 it won't be
useful of course).
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH v3 0/4] make vmalloc gfp flags usage more apparent
2025-11-17 17:35 [PATCH v3 0/4] make vmalloc gfp flags usage more apparent Vishal Moola (Oracle)
` (3 preceding siblings ...)
2025-11-17 17:35 ` [PATCH v3 4/4] mm/vmalloc: cleanup gfp flag use in new_vmap_block() Vishal Moola (Oracle)
@ 2025-11-20 1:03 ` SeongJae Park
2025-11-20 18:04 ` Andrew Morton
4 siblings, 1 reply; 14+ messages in thread
From: SeongJae Park @ 2025-11-20 1:03 UTC (permalink / raw)
To: Vishal Moola (Oracle)
Cc: SeongJae Park, linux-kernel, linux-mm, bpf, Uladzislau Rezki,
Christoph Hellwig, Andrew Morton
On Mon, 17 Nov 2025 09:35:26 -0800 "Vishal Moola (Oracle)" <vishal.moola@gmail.com> wrote:
> We should do a better job at enforcing gfp flags for vmalloc. Right now, we
> have a kernel-doc for __vmalloc_node_range(), and hope callers pass in
> supported flags. If a caller were to pass in an unsupported flag, we may
> BUG, silently clear it, or completely ignore it.
>
> If we are more proactive about enforcing gfp flags, we can making sure
> callers know when they may be asking for unsupported behavior.
>
> This patchset lets vmalloc control the incoming gfp flags, and cleans up
> some hard to read gfp code.
For the series,
Acked-by: SeongJae Park <sj@kernel.org>
>
> ---
> Linked rfc [1] and rfc v2[2] for convenience.
>
> Patch v2 -> v3:
> Only changes the whitelist mask and comment in patch 1:
I'd suggest s/whitelist/allow-list/.
Thanks,
SJ
[...]
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH v3 0/4] make vmalloc gfp flags usage more apparent
2025-11-20 1:03 ` [PATCH v3 0/4] make vmalloc gfp flags usage more apparent SeongJae Park
@ 2025-11-20 18:04 ` Andrew Morton
0 siblings, 0 replies; 14+ messages in thread
From: Andrew Morton @ 2025-11-20 18:04 UTC (permalink / raw)
To: SeongJae Park
Cc: Vishal Moola (Oracle),
linux-kernel, linux-mm, bpf, Uladzislau Rezki, Christoph Hellwig
On Wed, 19 Nov 2025 17:03:02 -0800 SeongJae Park <sj@kernel.org> wrote:
> On Mon, 17 Nov 2025 09:35:26 -0800 "Vishal Moola (Oracle)" <vishal.moola@gmail.com> wrote:
>
> > We should do a better job at enforcing gfp flags for vmalloc. Right now, we
> > have a kernel-doc for __vmalloc_node_range(), and hope callers pass in
> > supported flags. If a caller were to pass in an unsupported flag, we may
> > BUG, silently clear it, or completely ignore it.
> >
> > If we are more proactive about enforcing gfp flags, we can making sure
> > callers know when they may be asking for unsupported behavior.
> >
> > This patchset lets vmalloc control the incoming gfp flags, and cleans up
> > some hard to read gfp code.
>
> For the series,
>
> Acked-by: SeongJae Park <sj@kernel.org>
Thanks.
> >
> > ---
> > Linked rfc [1] and rfc v2[2] for convenience.
> >
> > Patch v2 -> v3:
> > Only changes the whitelist mask and comment in patch 1:
>
> I'd suggest s/whitelist/allow-list/.
Yeah, it's the modern way. But this was below the ^---$ separator so
it went away anyway.
^ permalink raw reply [flat|nested] 14+ messages in thread
end of thread, other threads:[~2025-11-20 18:04 UTC | newest]
Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-11-17 17:35 [PATCH v3 0/4] make vmalloc gfp flags usage more apparent Vishal Moola (Oracle)
2025-11-17 17:35 ` [PATCH v3 1/4] mm/vmalloc: warn on invalid vmalloc gfp flags Vishal Moola (Oracle)
2025-11-18 9:34 ` Uladzislau Rezki
2025-11-18 22:44 ` Nathan Chancellor
2025-11-19 0:54 ` Oliver Upton
2025-11-19 5:44 ` Christoph Hellwig
2025-11-17 17:35 ` [PATCH v3 2/4] mm/vmalloc: Add a helper to optimize vmalloc allocation gfps Vishal Moola (Oracle)
2025-11-18 9:35 ` Uladzislau Rezki
2025-11-17 17:35 ` [PATCH v3 3/4] mm/vmalloc: cleanup large_gfp in vm_area_alloc_pages() Vishal Moola (Oracle)
2025-11-18 9:35 ` Uladzislau Rezki
2025-11-17 17:35 ` [PATCH v3 4/4] mm/vmalloc: cleanup gfp flag use in new_vmap_block() Vishal Moola (Oracle)
2025-11-18 9:36 ` Uladzislau Rezki
2025-11-20 1:03 ` [PATCH v3 0/4] make vmalloc gfp flags usage more apparent SeongJae Park
2025-11-20 18:04 ` Andrew Morton
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox