Skip to contentSkip to conversation

__vmalloc() and no-block support

PATCH Superseded Uladzislau Rezki (Sony) 5 participants 35 messages Aug 7, 2025 – Aug 23, 2025

Subsystems (7)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Uladzislau Rezki (Sony)

author maintainer
#0 lore raw

cover __vmalloc() and no-block support

Hello.

This is a second series of making __vmalloc() to support GFP_ATOMIC and GFP_NOWAIT flags. It tends to improve the non-blocking behaviour.

The first one can be found here:

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

that was an RFC. Using this series for testing i have not found more places which can trigger: scheduling during atomic. Though there is one which requires attention. I will explain in [1].

Please note, non-blocking gets improved in the __vmalloc() call only, i.e. vmalloc_huge() still contains in its paths many cond_resched() points and can not be used as non-blocking as of now.

[1] The vmap_pages_range_noflush() contains the kmsan_vmap_pages_range_noflush() external implementation for KCSAN specifically which is hard coded to GFP_KERNEL. The kernel should be built with CONFIG_KCSAN option. To me it looks like not straight forward to run such kernel on my box, therefore i need more time to investigate what is wrong with CONFIG_KCSAN and my env.

Uladzislau Rezki (Sony) (8):
lib/test_vmalloc: add no_block_alloc_test case lib/test_vmalloc: Remove xfail condition check mm/vmalloc: Support non-blocking GFP flags in alloc_vmap_area() mm/vmalloc: Remove cond_resched() in vm_area_alloc_pages() mm/kasan, mm/vmalloc: Respect GFP flags in kasan_populate_vmalloc() mm/vmalloc: Defer freeing partly initialized vm_struct mm/vmalloc: Support non-blocking GFP flags in __vmalloc_area_node()
mm: Drop __GFP_DIRECT_RECLAIM flag if PF_MEMALLOC is set

 include/linux/kasan.h    |  6 ++--
 include/linux/sched/mm.h |  7 +++-
 include/linux/vmalloc.h  |  6 +++-
 lib/test_vmalloc.c       | 28 ++++++++++++++-
 mm/kasan/shadow.c        | 22 ++++++++----
 mm/vmalloc.c             | 77 ++++++++++++++++++++++++++++++++--------
 6 files changed, 119 insertions(+), 27 deletions(-)
Signature · 1 line
2.39.5
author maintainer
#1 lore raw

PATCH 1/8 lib/test_vmalloc: add no_block_alloc_test case

Introduce a new test case "no_block_alloc_test" that verifies non-blocking allocations using __vmalloc() with GFP_ATOMIC and GFP_NOWAIT flags.

It is recommended to build kernel with CONFIG_DEBUG_ATOMIC_SLEEP enabled to help catch "sleeping while atomic" issues. This test ensures that memory allocation logic under atomic constraints does not inadvertently sleep.

---
 lib/test_vmalloc.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
lib/test_vmalloc.c +26 -0
index 2815658ccc37..aae5f4910aff 100644
@@ -54,6 +54,7 @@ __param(int, run_test_mask, 7,
 		"\t\tid: 256,  name: kvfree_rcu_1_arg_vmalloc_test\n"
 		"\t\tid: 512,  name: kvfree_rcu_2_arg_vmalloc_test\n"
 		"\t\tid: 1024, name: vm_map_ram_test\n"
+		"\t\tid: 2048, name: no_block_alloc_test\n"
 		/* Add a new test case description here. */
 );
 
@@ -283,6 +284,30 @@ static int fix_size_alloc_test(void)
 	return 0;
 }
 
+static int no_block_alloc_test(void)
+{
+	void *ptr;
+	int i;
+
+	for (i = 0; i < test_loop_count; i++) {
+		bool use_atomic = !!(get_random_u8() % 2);
+		gfp_t gfp = use_atomic ? GFP_ATOMIC : GFP_NOWAIT;
+		unsigned long size = (nr_pages > 0 ? nr_pages : 1) * PAGE_SIZE;
+
+		preempt_disable();
+		ptr = __vmalloc(size, gfp);
+		preempt_enable();
+
+		if (!ptr)
+			return -1;
+
+		*((__u8 *)ptr) = 0;
+		vfree(ptr);
+	}
+
+	return 0;
+}
+
 static int
 pcpu_alloc_test(void)
 {
@@ -411,6 +436,7 @@ static struct test_case_desc test_case_array[] = {
 	{ "kvfree_rcu_1_arg_vmalloc_test", kvfree_rcu_1_arg_vmalloc_test, },
 	{ "kvfree_rcu_2_arg_vmalloc_test", kvfree_rcu_2_arg_vmalloc_test, },
 	{ "vm_map_ram_test", vm_map_ram_test, },
+	{ "no_block_alloc_test", no_block_alloc_test, true },
 	/* Add a new test case here. */
 };
 
Signature · 1 line
2.39.5
author maintainer
#2 lore raw

PATCH 2/8 lib/test_vmalloc: Remove xfail condition check

In reply to Uladzislau Rezki (Sony)

A test marked with "xfail = true" is expected to fail but that does not mean it is predetermined to fail. Remove "xfail" condition check for tests which pass successfully.

---
 lib/test_vmalloc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/test_vmalloc.c +1 -1
index aae5f4910aff..6521c05c7816 100644
@@ -500,7 +500,7 @@ static int test_func(void *private)
 		for (j = 0; j < test_repeat_count; j++) {
 			ret = test_case_array[index].test_func();
 
-			if (!ret && !test_case_array[index].xfail)
+			if (!ret)
 				t->data[index].test_passed++;
 			else if (ret && test_case_array[index].xfail)
 				t->data[index].test_xfailed++;
Signature · 1 line
2.39.5
author maintainer
#3 lore raw

PATCH 3/8 mm/vmalloc: Support non-blocking GFP flags in alloc_vmap_area()

In reply to Uladzislau Rezki (Sony)

alloc_vmap_area() currently assumes that sleeping is allowed during allocation. This is not true for callers which pass non-blocking GFP flags, such as GFP_ATOMIC or GFP_NOWAIT.

This patch adds logic to detect whether the given gfp_mask permits blocking. It avoids invoking might_sleep() or falling back to reclaim path if blocking is not allowed.

This makes alloc_vmap_area() safer for use in non-sleeping contexts, where previously it could hit unexpected sleeps, trigger warnings.

---
 mm/vmalloc.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
mm/vmalloc.c +14 -3
index 6dbcdceecae1..81b6d3bde719 100644
@@ -2017,6 +2017,7 @@ static struct vmap_area *alloc_vmap_area(unsigned long size,
 	unsigned long freed;
 	unsigned long addr;
 	unsigned int vn_id;
+	bool allow_block;
 	int purged = 0;
 	int ret;
 
@@ -2026,7 +2027,8 @@ static struct vmap_area *alloc_vmap_area(unsigned long size,
 	if (unlikely(!vmap_initialized))
 		return ERR_PTR(-EBUSY);
 
-	might_sleep();
+	allow_block = gfpflags_allow_blocking(gfp_mask);
+	might_sleep_if(allow_block);
 
 	/*
 	 * If a VA is obtained from a global heap(if it fails here)
@@ -2065,8 +2067,16 @@ static struct vmap_area *alloc_vmap_area(unsigned long size,
 	 * If an allocation fails, the error value is
 	 * returned. Therefore trigger the overflow path.
 	 */
-	if (IS_ERR_VALUE(addr))
-		goto overflow;
+	if (IS_ERR_VALUE(addr)) {
+		if (allow_block)
+			goto overflow;
+
+		/*
+		 * We can not trigger any reclaim logic because
+		 * sleeping is not allowed, thus fail an allocation.
+		 */
+		goto error;
+	}
 
 	va->va_start = addr;
 	va->va_end = addr + size;
@@ -2116,6 +2126,7 @@ static struct vmap_area *alloc_vmap_area(unsigned long size,
 		pr_warn("vmalloc_node_range for size %lu failed: Address range restricted to %#lx - %#lx\n",
 				size, vstart, vend);
 
+error:
 	kmem_cache_free(vmap_area_cachep, va);
 	return ERR_PTR(-EBUSY);
 }
Signature · 1 line
2.39.5
author maintainer
#7 lore raw

PATCH 4/8 mm/vmalloc: Remove cond_resched() in vm_area_alloc_pages()

In reply to Uladzislau Rezki (Sony)

The vm_area_alloc_pages() function uses cond_resched() to yield the CPU during potentially long-running loops. However, these loops are not considered long-running under normal conditions. In non-blocking contexts, calling cond_resched() is inappropriate also.

Remove these calls to ensure correctness for blocking/non-blocking contexts. This also simplifies the code path. In fact, a slow path of page allocator already includes reschedule points to mitigate latency.

This patch was tested for !CONFIG_PREEMPT kernel and with large allocation chunks(~1GB), without triggering any "BUG: soft lockup" warnings.

---
 mm/vmalloc.c | 2 --
 1 file changed, 2 deletions(-)
mm/vmalloc.c +0 -2
index 81b6d3bde719..b0255e0c74b3 100644
@@ -3633,7 +3633,6 @@ vm_area_alloc_pages(gfp_t gfp, int nid,
 							pages + nr_allocated);
 
 			nr_allocated += nr;
-			cond_resched();
 
 			/*
 			 * If zero or pages were obtained partly,
@@ -3675,7 +3674,6 @@ vm_area_alloc_pages(gfp_t gfp, int nid,
 		for (i = 0; i < (1U << order); i++)
 			pages[nr_allocated + i] = page + i;
 
-		cond_resched();
 		nr_allocated += 1U << order;
 	}
 
Signature · 1 line
2.39.5
author maintainer
#11 lore raw

PATCH 5/8 mm/kasan, mm/vmalloc: Respect GFP flags in kasan_populate_vmalloc()

In reply to Uladzislau Rezki (Sony)

The function kasan_populate_vmalloc() internally allocates a page using a hardcoded GFP_KERNEL flag. This is not safe in contexts where non-blocking allocation flags are required, such as GFP_ATOMIC or GFP_NOWAIT, for example during atomic vmalloc paths.

This patch modifies kasan_populate_vmalloc() and its helpers to accept a gfp_mask argument to use it for a page allocation. It allows the caller to specify the correct allocation context.

Also, when non-blocking flags are used, memalloc_noreclaim_save/restore() is used around apply_to_page_range() to suppress potential reclaim behavior that may otherwise violate atomic constraints.

Cc Andrey Ryabinin <[email protected]>
Cc Alexander Potapenko <[email protected]>
---
 include/linux/kasan.h |  6 +++---
 mm/kasan/shadow.c     | 22 +++++++++++++++-------
 mm/vmalloc.c          |  4 ++--
 3 files changed, 20 insertions(+), 12 deletions(-)
include/linux/kasan.h +3 -3
index 890011071f2b..fe5ce9215821 100644
@@ -562,7 +562,7 @@ static inline void kasan_init_hw_tags(void) { }
 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
 
 void kasan_populate_early_vm_area_shadow(void *start, unsigned long size);
-int kasan_populate_vmalloc(unsigned long addr, unsigned long size);
+int kasan_populate_vmalloc(unsigned long addr, unsigned long size, gfp_t gfp_mask);
 void kasan_release_vmalloc(unsigned long start, unsigned long end,
 			   unsigned long free_region_start,
 			   unsigned long free_region_end,
@@ -574,7 +574,7 @@ static inline void kasan_populate_early_vm_area_shadow(void *start,
 						       unsigned long size)
 { }
 static inline int kasan_populate_vmalloc(unsigned long start,
-					unsigned long size)
+					unsigned long size, gfp_t gfp_mask)
 {
 	return 0;
 }
@@ -610,7 +610,7 @@ static __always_inline void kasan_poison_vmalloc(const void *start,
 static inline void kasan_populate_early_vm_area_shadow(void *start,
 						       unsigned long size) { }
 static inline int kasan_populate_vmalloc(unsigned long start,
-					unsigned long size)
+					unsigned long size, gfp_t gfp_mask)
 {
 	return 0;
 }
mm/kasan/shadow.c +15 -7
index d2c70cd2afb1..5edfc1f6b53e 100644
@@ -335,13 +335,13 @@ static void ___free_pages_bulk(struct page **pages, int nr_pages)
 	}
 }
 
-static int ___alloc_pages_bulk(struct page **pages, int nr_pages)
+static int ___alloc_pages_bulk(struct page **pages, int nr_pages, gfp_t gfp_mask)
 {
 	unsigned long nr_populated, nr_total = nr_pages;
 	struct page **page_array = pages;
 
 	while (nr_pages) {
-		nr_populated = alloc_pages_bulk(GFP_KERNEL, nr_pages, pages);
+		nr_populated = alloc_pages_bulk(gfp_mask, nr_pages, pages);
 		if (!nr_populated) {
 			___free_pages_bulk(page_array, nr_total - nr_pages);
 			return -ENOMEM;
@@ -353,25 +353,33 @@ static int ___alloc_pages_bulk(struct page **pages, int nr_pages)
 	return 0;
 }
 
-static int __kasan_populate_vmalloc(unsigned long start, unsigned long end)
+static int __kasan_populate_vmalloc(unsigned long start, unsigned long end, gfp_t gfp_mask)
 {
 	unsigned long nr_pages, nr_total = PFN_UP(end - start);
+	bool noblock = !gfpflags_allow_blocking(gfp_mask);
 	struct vmalloc_populate_data data;
+	unsigned int flags;
 	int ret = 0;
 
-	data.pages = (struct page **)__get_free_page(GFP_KERNEL | __GFP_ZERO);
+	data.pages = (struct page **)__get_free_page(gfp_mask | __GFP_ZERO);
 	if (!data.pages)
 		return -ENOMEM;
 
 	while (nr_total) {
 		nr_pages = min(nr_total, PAGE_SIZE / sizeof(data.pages[0]));
-		ret = ___alloc_pages_bulk(data.pages, nr_pages);
+		ret = ___alloc_pages_bulk(data.pages, nr_pages, gfp_mask);
 		if (ret)
 			break;
 
 		data.start = start;
+		if (noblock)
+			flags = memalloc_noreclaim_save();
+
 		ret = apply_to_page_range(&init_mm, start, nr_pages * PAGE_SIZE,
 					  kasan_populate_vmalloc_pte, &data);
+		if (noblock)
+			memalloc_noreclaim_restore(flags);
+
 		___free_pages_bulk(data.pages, nr_pages);
 		if (ret)
 			break;
@@ -385,7 +393,7 @@ static int __kasan_populate_vmalloc(unsigned long start, unsigned long end)
 	return ret;
 }
 
-int kasan_populate_vmalloc(unsigned long addr, unsigned long size)
+int kasan_populate_vmalloc(unsigned long addr, unsigned long size, gfp_t gfp_mask)
 {
 	unsigned long shadow_start, shadow_end;
 	int ret;
@@ -414,7 +422,7 @@ int kasan_populate_vmalloc(unsigned long addr, unsigned long size)
 	shadow_start = PAGE_ALIGN_DOWN(shadow_start);
 	shadow_end = PAGE_ALIGN(shadow_end);
 
-	ret = __kasan_populate_vmalloc(shadow_start, shadow_end);
+	ret = __kasan_populate_vmalloc(shadow_start, shadow_end, gfp_mask);
 	if (ret)
 		return ret;
 
mm/vmalloc.c +2 -2
index b0255e0c74b3..7f48a54ec108 100644
@@ -2099,7 +2099,7 @@ static struct vmap_area *alloc_vmap_area(unsigned long size,
 	BUG_ON(va->va_start < vstart);
 	BUG_ON(va->va_end > vend);
 
-	ret = kasan_populate_vmalloc(addr, size);
+	ret = kasan_populate_vmalloc(addr, size, gfp_mask);
 	if (ret) {
 		free_vmap_area(va);
 		return ERR_PTR(ret);
@@ -4835,7 +4835,7 @@ struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
 
 	/* populate the kasan shadow space */
 	for (area = 0; area < nr_vms; area++) {
-		if (kasan_populate_vmalloc(vas[area]->va_start, sizes[area]))
+		if (kasan_populate_vmalloc(vas[area]->va_start, sizes[area], GFP_KERNEL))
 			goto err_free_shadow;
 	}
 
Signature · 1 line
2.39.5
author maintainer
#14 lore raw

PATCH 6/8 mm/vmalloc: Defer freeing partly initialized vm_struct

In reply to Uladzislau Rezki (Sony)

__vmalloc_area_node() may call free_vmap_area() or vfree() on error paths, both of which can sleep. This becomes problematic if the function is invoked from an atomic context, such as when GFP_ATOMIC or GFP_NOWAIT is passed via gfp_mask.

To fix this, unify error paths and defer the cleanup of partly initialized vm_struct objects to a workqueue. This ensures that freeing happens in a process context and avoids invalid sleeps in atomic regions.

---
 include/linux/vmalloc.h |  6 +++++-
 mm/vmalloc.c            | 34 +++++++++++++++++++++++++++++++---
 2 files changed, 36 insertions(+), 4 deletions(-)
include/linux/vmalloc.h +5 -1
index fdc9aeb74a44..b1425fae8cbf 100644
@@ -50,7 +50,11 @@ struct iov_iter;		/* in uio.h */
 #endif
 
 struct vm_struct {
-	struct vm_struct	*next;
+	union {
+		struct vm_struct *next;	  /* Early registration of vm_areas. */
+		struct llist_node llnode; /* Asynchronous freeing on error paths. */
+	};
+
 	void			*addr;
 	unsigned long		size;
 	unsigned long		flags;
mm/vmalloc.c +31 -3
index 7f48a54ec108..2424f80d524a 100644
@@ -3680,6 +3680,35 @@ vm_area_alloc_pages(gfp_t gfp, int nid,
 	return nr_allocated;
 }
 
+static LLIST_HEAD(pending_vm_area_cleanup);
+static void cleanup_vm_area_work(struct work_struct *work)
+{
+	struct vm_struct *area, *tmp;
+	struct llist_node *head;
+
+	head = llist_del_all(&pending_vm_area_cleanup);
+	if (!head)
+		return;
+
+	llist_for_each_entry_safe(area, tmp, head, llnode) {
+		if (!area->pages)
+			free_vm_area(area);
+		else
+			vfree(area->addr);
+	}
+}
+
+/*
+ * Helper for __vmalloc_area_node() to defer cleanup
+ * of partially initialized vm_struct in error paths.
+ */
+static DECLARE_WORK(cleanup_vm_area, cleanup_vm_area_work);
+static void defer_vm_area_cleanup(struct vm_struct *area)
+{
+	if (llist_add(&area->llnode, &pending_vm_area_cleanup))
+		schedule_work(&cleanup_vm_area);
+}
+
 static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 				 pgprot_t prot, unsigned int page_shift,
 				 int node)
@@ -3711,8 +3740,7 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 		warn_alloc(gfp_mask, NULL,
 			"vmalloc error: size %lu, failed to allocated page array size %lu",
 			nr_small_pages * PAGE_SIZE, array_size);
-		free_vm_area(area);
-		return NULL;
+		goto fail;
 	}
 
 	set_vm_area_page_order(area, page_shift - PAGE_SHIFT);
@@ -3789,7 +3817,7 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 	return area->addr;
 
 fail:
-	vfree(area->addr);
+	defer_vm_area_cleanup(area);
 	return NULL;
 }
 
Signature · 1 line
2.39.5
author maintainer
#21 lore raw

PATCH 7/8 mm/vmalloc: Support non-blocking GFP flags in __vmalloc_area_node()

In reply to Uladzislau Rezki (Sony)

This patch makes __vmalloc_area_node() to correctly handle non-blocking allocation requests, such as GFP_ATOMIC and GFP_NOWAIT. Main changes:

- Add a __GFP_HIGHMEM to gfp_mask only for blocking requests if there are no DMA constraints.

- vmap_page_range() is wrapped by memalloc_noreclaim_save/restore() to avoid memory reclaim related operations that could sleep during page table setup or mapping pages.

This is particularly important for page table allocations that internally use GFP_PGTABLE_KERNEL, which may sleep unless such scope restrictions are applied. For example:

<snip>
__pte_alloc_kernel()

    pte_alloc_one_kernel(&init_mm);
        pagetable_alloc_noprof(GFP_PGTABLE_KERNEL & ~__GFP_HIGHMEM, 0);

<snip>

Note: in most cases, PTE entries are established only up to the level required by current vmap space usage, meaning the page tables are typically fully populated during the mapping process.

---
 mm/vmalloc.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
mm/vmalloc.c +16 -4
index 2424f80d524a..8a7eab810561 100644
@@ -3721,12 +3721,20 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 	unsigned int nr_small_pages = size >> PAGE_SHIFT;
 	unsigned int page_order;
 	unsigned int flags;
+	bool noblock;
 	int ret;
 
 	array_size = (unsigned long)nr_small_pages * sizeof(struct page *);
+	noblock = !gfpflags_allow_blocking(gfp_mask);
 
-	if (!(gfp_mask & (GFP_DMA | GFP_DMA32)))
-		gfp_mask |= __GFP_HIGHMEM;
+	if (noblock) {
+		/* __GFP_NOFAIL and "noblock" flags are mutually exclusive. */
+		nofail = false;
+	} else {
+		/* Allow highmem allocations if there are no DMA constraints. */
+		if (!(gfp_mask & (GFP_DMA | GFP_DMA32)))
+			gfp_mask |= __GFP_HIGHMEM;
+	}
 
 	/* Please note that the recursion is strictly bounded. */
 	if (array_size > PAGE_SIZE) {
@@ -3790,7 +3798,9 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 	 * page tables allocations ignore external gfp mask, enforce it
 	 * by the scope API
 	 */
-	if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)
+	if (noblock)
+		flags = memalloc_noreclaim_save();
+	else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)
 		flags = memalloc_nofs_save();
 	else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0)
 		flags = memalloc_noio_save();
@@ -3802,7 +3812,9 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 			schedule_timeout_uninterruptible(1);
 	} while (nofail && (ret < 0));
 
-	if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)
+	if (noblock)
+		memalloc_noreclaim_restore(flags);
+	else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)
 		memalloc_nofs_restore(flags);
 	else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0)
 		memalloc_noio_restore(flags);
Signature · 1 line
2.39.5
author maintainer
#27 lore raw

PATCH 8/8 mm: Drop __GFP_DIRECT_RECLAIM flag if PF_MEMALLOC is set

In reply to Uladzislau Rezki (Sony)

The memory allocator already avoids reclaim when PF_MEMALLOC is set. Clear __GFP_DIRECT_RECLAIM explicitly to suppress might_alloc() warnings to make more correct behavior.

---
 include/linux/sched/mm.h | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
include/linux/sched/mm.h +6 -1
index 2201da0afecc..8332fc09f8ac 100644
@@ -246,12 +246,14 @@ static inline bool in_vfork(struct task_struct *tsk)
  * PF_MEMALLOC_NOIO implies GFP_NOIO
  * PF_MEMALLOC_NOFS implies GFP_NOFS
  * PF_MEMALLOC_PIN  implies !GFP_MOVABLE
+ * PF_MEMALLOC      implies !__GFP_DIRECT_RECLAIM
  */
 static inline gfp_t current_gfp_context(gfp_t flags)
 {
 	unsigned int pflags = READ_ONCE(current->flags);
 
-	if (unlikely(pflags & (PF_MEMALLOC_NOIO | PF_MEMALLOC_NOFS | PF_MEMALLOC_PIN))) {
+	if (unlikely(pflags & (PF_MEMALLOC_NOIO | PF_MEMALLOC_NOFS |
+			PF_MEMALLOC_PIN | PF_MEMALLOC))) {
 		/*
 		 * NOIO implies both NOIO and NOFS and it is a weaker context
 		 * so always make sure it makes precedence
@@ -263,6 +265,9 @@ static inline gfp_t current_gfp_context(gfp_t flags)
 
 		if (pflags & PF_MEMALLOC_PIN)
 			flags &= ~__GFP_MOVABLE;
+
+		if (pflags & PF_MEMALLOC)
+			flags &= ~__GFP_DIRECT_RECLAIM;
 	}
 	return flags;
 }
Signature · 1 line
2.39.5
reviewer
#32 lore raw
In reply to Uladzislau Rezki (Sony)
Quoted 17 lines · Uladzislau Rezki (Sony)

Hello.

This is a second series of making __vmalloc() to support GFP_ATOMIC and GFP_NOWAIT flags. It tends to improve the non-blocking behaviour.

The first one can be found here:

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

that was an RFC. Using this series for testing i have not found more places which can trigger: scheduling during atomic. Though there is one which requires attention. I will explain in [1].

Please note, non-blocking gets improved in the __vmalloc() call only, i.e. vmalloc_huge() still contains in its paths many cond_resched() points and can not be used as non-blocking as of now.

Quoted 5 lines

[1] The vmap_pages_range_noflush() contains the kmsan_vmap_pages_range_noflush() external implementation for KCSAN specifically which is hard coded to GFP_KERNEL. The kernel should be built with CONFIG_KCSAN option. To me it looks like not straight forward to run such kernel on my box, therefore i need more time to investigate what is wrong with CONFIG_KCSAN and my env.

KMSAN or KCSAN?

[+Cc KMSAN maintainers]

reviewer Acked-by on v1 3/8
#4 lore raw

Re: [PATCH 3/8] mm/vmalloc: Support non-blocking GFP flags in alloc_vmap_area()

In reply to Uladzislau Rezki (Sony)
Quoted 3 lines · Uladzislau Rezki

alloc_vmap_area() currently assumes that sleeping is allowed during allocation. This is not true for callers which pass non-blocking GFP flags, such as GFP_ATOMIC or GFP_NOWAIT.

Those are currently not allowed so it would be better to mention this is a preparation for those to _be_ supported later in the series.

Quoted 8 lines

This patch adds logic to detect whether the given gfp_mask permits blocking. It avoids invoking might_sleep() or falling back to reclaim path if blocking is not allowed.

This makes alloc_vmap_area() safer for use in non-sleeping contexts, where previously it could hit unexpected sleeps, trigger warnings.

With the changelog clarified

Thanks!

Quoted 55 lines
---
 mm/vmalloc.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
mm/vmalloc.c +14 -3
index 6dbcdceecae1..81b6d3bde719 100644
@@ -2017,6 +2017,7 @@ static struct vmap_area *alloc_vmap_area(unsigned long size,
 	unsigned long freed;
 	unsigned long addr;
 	unsigned int vn_id;
+	bool allow_block;
 	int purged = 0;
 	int ret;
 
@@ -2026,7 +2027,8 @@ static struct vmap_area *alloc_vmap_area(unsigned long size,
 	if (unlikely(!vmap_initialized))
 		return ERR_PTR(-EBUSY);
 
-	might_sleep();
+	allow_block = gfpflags_allow_blocking(gfp_mask);
+	might_sleep_if(allow_block);
 
 	/*
 	 * If a VA is obtained from a global heap(if it fails here)
@@ -2065,8 +2067,16 @@ static struct vmap_area *alloc_vmap_area(unsigned long size,
 	 * If an allocation fails, the error value is
 	 * returned. Therefore trigger the overflow path.
 	 */
-	if (IS_ERR_VALUE(addr))
-		goto overflow;
+	if (IS_ERR_VALUE(addr)) {
+		if (allow_block)
+			goto overflow;
+
+		/*
+		 * We can not trigger any reclaim logic because
+		 * sleeping is not allowed, thus fail an allocation.
+		 */
+		goto error;
+	}
 
 	va->va_start = addr;
 	va->va_end = addr + size;
@@ -2116,6 +2126,7 @@ static struct vmap_area *alloc_vmap_area(unsigned long size,
 		pr_warn("vmalloc_node_range for size %lu failed: Address range restricted to %#lx - %#lx\n",
 				size, vstart, vend);
 
+error:
 	kmem_cache_free(vmap_area_cachep, va);
 	return ERR_PTR(-EBUSY);
 }
Signature · 1 line
2.39.5
Signature · 2 lines
Michal Hocko
SUSE Labs
reviewer Acked-by on v1 4/8
#8 lore raw

Re: [PATCH 4/8] mm/vmalloc: Remove cond_resched() in vm_area_alloc_pages()

In reply to Uladzislau Rezki (Sony)
Quoted 3 lines · Uladzislau Rezki

The vm_area_alloc_pages() function uses cond_resched() to yield the CPU during potentially long-running loops. However, these loops are not considered long-running under normal conditions.

To be more precise they can take long if they dive into the page allocator but that already involves cond_rescheds where appropriate so these are not needed in fact.

Quoted 8 lines

In non-blocking
contexts, calling cond_resched() is inappropriate also.

Remove these calls to ensure correctness for blocking/non-blocking contexts. This also simplifies the code path. In fact, a slow path of page allocator already includes reschedule points to mitigate latency.

Quoted 5 lines

This patch was tested for !CONFIG_PREEMPT kernel and with large allocation chunks(~1GB), without triggering any "BUG: soft lockup" warnings.

Thanks!

Quoted 27 lines
---
 mm/vmalloc.c | 2 --
 1 file changed, 2 deletions(-)
mm/vmalloc.c +0 -2
index 81b6d3bde719..b0255e0c74b3 100644
@@ -3633,7 +3633,6 @@ vm_area_alloc_pages(gfp_t gfp, int nid,
 							pages + nr_allocated);
 
 			nr_allocated += nr;
-			cond_resched();
 
 			/*
 			 * If zero or pages were obtained partly,
@@ -3675,7 +3674,6 @@ vm_area_alloc_pages(gfp_t gfp, int nid,
 		for (i = 0; i < (1U << order); i++)
 			pages[nr_allocated + i] = page + i;
 
-		cond_resched();
 		nr_allocated += 1U << order;
 	}
 
Signature · 1 line
2.39.5
Signature · 2 lines
Michal Hocko
SUSE Labs
reviewer Acked-by on v1 6/8
#15 lore raw

Re: [PATCH 6/8] mm/vmalloc: Defer freeing partly initialized vm_struct

In reply to Uladzislau Rezki (Sony)
Quoted 11 lines · Uladzislau Rezki

__vmalloc_area_node() may call free_vmap_area() or vfree() on error paths, both of which can sleep. This becomes problematic if the function is invoked from an atomic context, such as when GFP_ATOMIC or GFP_NOWAIT is passed via gfp_mask.

To fix this, unify error paths and defer the cleanup of partly initialized vm_struct objects to a workqueue. This ensures that freeing happens in a process context and avoids invalid sleeps in atomic regions.

LGTM

Thanks!

Quoted 83 lines
---
 include/linux/vmalloc.h |  6 +++++-
 mm/vmalloc.c            | 34 +++++++++++++++++++++++++++++++---
 2 files changed, 36 insertions(+), 4 deletions(-)
include/linux/vmalloc.h +5 -1
index fdc9aeb74a44..b1425fae8cbf 100644
@@ -50,7 +50,11 @@ struct iov_iter;		/* in uio.h */
 #endif
 
 struct vm_struct {
-	struct vm_struct	*next;
+	union {
+		struct vm_struct *next;	  /* Early registration of vm_areas. */
+		struct llist_node llnode; /* Asynchronous freeing on error paths. */
+	};
+
 	void			*addr;
 	unsigned long		size;
 	unsigned long		flags;
mm/vmalloc.c +31 -3
index 7f48a54ec108..2424f80d524a 100644
@@ -3680,6 +3680,35 @@ vm_area_alloc_pages(gfp_t gfp, int nid,
 	return nr_allocated;
 }
 
+static LLIST_HEAD(pending_vm_area_cleanup);
+static void cleanup_vm_area_work(struct work_struct *work)
+{
+	struct vm_struct *area, *tmp;
+	struct llist_node *head;
+
+	head = llist_del_all(&pending_vm_area_cleanup);
+	if (!head)
+		return;
+
+	llist_for_each_entry_safe(area, tmp, head, llnode) {
+		if (!area->pages)
+			free_vm_area(area);
+		else
+			vfree(area->addr);
+	}
+}
+
+/*
+ * Helper for __vmalloc_area_node() to defer cleanup
+ * of partially initialized vm_struct in error paths.
+ */
+static DECLARE_WORK(cleanup_vm_area, cleanup_vm_area_work);
+static void defer_vm_area_cleanup(struct vm_struct *area)
+{
+	if (llist_add(&area->llnode, &pending_vm_area_cleanup))
+		schedule_work(&cleanup_vm_area);
+}
+
 static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 				 pgprot_t prot, unsigned int page_shift,
 				 int node)
@@ -3711,8 +3740,7 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 		warn_alloc(gfp_mask, NULL,
 			"vmalloc error: size %lu, failed to allocated page array size %lu",
 			nr_small_pages * PAGE_SIZE, array_size);
-		free_vm_area(area);
-		return NULL;
+		goto fail;
 	}
 
 	set_vm_area_page_order(area, page_shift - PAGE_SHIFT);
@@ -3789,7 +3817,7 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 	return area->addr;
 
 fail:
-	vfree(area->addr);
+	defer_vm_area_cleanup(area);
 	return NULL;
 }
 
Signature · 1 line
2.39.5
Signature · 2 lines
Michal Hocko
SUSE Labs
reviewer Acked-by on v1 7/8
#22 lore raw

Re: [PATCH 7/8] mm/vmalloc: Support non-blocking GFP flags in __vmalloc_area_node()

In reply to Uladzislau Rezki (Sony)
Quoted 5 lines · Uladzislau Rezki

This patch makes __vmalloc_area_node() to correctly handle non-blocking allocation requests, such as GFP_ATOMIC and GFP_NOWAIT. Main changes:

- Add a __GFP_HIGHMEM to gfp_mask only for blocking requests if there are no DMA constraints.

This begs for a more explanation. Why does __GFP_HIGHMEM matters? I suspect this is due to kmapping of those pages but that could be done in an atomic way. But in practice I do not think we really care about highmem all that much for vmalloc. The vmalloc space is really tiny for 32b systems where highmem matters and failing vmalloc allocations due to lack is of __GFP_HIGHMEM is hard to consider important if relevant at all.

Quoted 8 lines

- vmap_page_range() is wrapped by memalloc_noreclaim_save/restore() to avoid memory reclaim related operations that could sleep during page table setup or mapping pages.

This is particularly important for page table allocations that internally use GFP_PGTABLE_KERNEL, which may sleep unless such scope restrictions are applied. For example:

Quoted 5 lines

<snip>
__pte_alloc_kernel()

    pte_alloc_one_kernel(&init_mm);
        pagetable_alloc_noprof(GFP_PGTABLE_KERNEL & ~__GFP_HIGHMEM, 0);

<snip>

As I've said in several occations, I am not entirely happy about this approach because it doesn't really guarantee atomicty. If any architecture decides to use some sleeping locking down that path then the whole thing just blows up. On the other hand this is mostly a theoretical concern at this stage and this is a feature people have been asking for a long time (especially from kvmalloc side) so better good than perfect that his.

That being said, you are missing __kvmalloc_node_noprof, __vmalloc_node_range_noprof (and maybe some more places) documentation update.

Quoted 5 lines

Note: in most cases, PTE entries are established only up to the level required by current vmap space usage, meaning the page tables are typically fully populated during the mapping process.

With the doc part fixed

Thanks!

Quoted 56 lines
---
 mm/vmalloc.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
mm/vmalloc.c +16 -4
index 2424f80d524a..8a7eab810561 100644
@@ -3721,12 +3721,20 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 	unsigned int nr_small_pages = size >> PAGE_SHIFT;
 	unsigned int page_order;
 	unsigned int flags;
+	bool noblock;
 	int ret;
 
 	array_size = (unsigned long)nr_small_pages * sizeof(struct page *);
+	noblock = !gfpflags_allow_blocking(gfp_mask);
 
-	if (!(gfp_mask & (GFP_DMA | GFP_DMA32)))
-		gfp_mask |= __GFP_HIGHMEM;
+	if (noblock) {
+		/* __GFP_NOFAIL and "noblock" flags are mutually exclusive. */
+		nofail = false;
+	} else {
+		/* Allow highmem allocations if there are no DMA constraints. */
+		if (!(gfp_mask & (GFP_DMA | GFP_DMA32)))
+			gfp_mask |= __GFP_HIGHMEM;
+	}
 
 	/* Please note that the recursion is strictly bounded. */
 	if (array_size > PAGE_SIZE) {
@@ -3790,7 +3798,9 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 	 * page tables allocations ignore external gfp mask, enforce it
 	 * by the scope API
 	 */
-	if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)
+	if (noblock)
+		flags = memalloc_noreclaim_save();
+	else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)
 		flags = memalloc_nofs_save();
 	else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0)
 		flags = memalloc_noio_save();
@@ -3802,7 +3812,9 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 			schedule_timeout_uninterruptible(1);
 	} while (nofail && (ret < 0));
 
-	if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)
+	if (noblock)
+		memalloc_noreclaim_restore(flags);
+	else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)
 		memalloc_nofs_restore(flags);
 	else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0)
 		memalloc_noio_restore(flags);
Signature · 1 line
2.39.5
Signature · 2 lines
Michal Hocko
SUSE Labs
reviewer
#28 lore raw

Re: [PATCH 8/8] mm: Drop __GFP_DIRECT_RECLAIM flag if PF_MEMALLOC is set

In reply to Uladzislau Rezki (Sony)
Quoted 3 lines · Uladzislau Rezki

The memory allocator already avoids reclaim when PF_MEMALLOC is set. Clear __GFP_DIRECT_RECLAIM explicitly to suppress might_alloc() warnings to make more correct behavior.

Rather than chaning the gfp mask would it make more sense to update might_alloc instead?

Signature · 2 lines
Michal Hocko
SUSE Labs
maintainer
#12 lore raw

Re: [PATCH 5/8] mm/kasan, mm/vmalloc: Respect GFP flags in kasan_populate_vmalloc()

In reply to Uladzislau Rezki (Sony)
Quoted 22 lines · Uladzislau Rezki (Sony)
mm/kasan/shadow.c +2 -2
index d2c70cd2afb1..5edfc1f6b53e 100644
@@ -335,13 +335,13 @@ static void ___free_pages_bulk(struct page **pages, int nr_pages)
 	}
 }
 
-static int ___alloc_pages_bulk(struct page **pages, int nr_pages)
+static int ___alloc_pages_bulk(struct page **pages, int nr_pages, gfp_t gfp_mask)
 {
 	unsigned long nr_populated, nr_total = nr_pages;
 	struct page **page_array = pages;
 
 	while (nr_pages) {
-		nr_populated = alloc_pages_bulk(GFP_KERNEL, nr_pages, pages);
+		nr_populated = alloc_pages_bulk(gfp_mask, nr_pages, pages);
 		if (!nr_populated) {
 			___free_pages_bulk(page_array, nr_total - nr_pages);
 			return -ENOMEM;
@@ -353,25 +353,33 @@ static int ___alloc_pages_bulk(struct page **pages, int nr_pages)
 	return 0;

mm/kasan/shadow.c static int ___alloc_pages_bulk(struct page **pages, int nr_pages) · line 362

Quoted 10 lines
mm/kasan/shadow.c +3 -1
 }
 
-static int __kasan_populate_vmalloc(unsigned long start, unsigned long end)
+static int __kasan_populate_vmalloc(unsigned long start, unsigned long end, gfp_t gfp_mask)
 {
 	unsigned long nr_pages, nr_total = PFN_UP(end - start);
+	bool noblock = !gfpflags_allow_blocking(gfp_mask);
 	struct vmalloc_populate_data data;
+	unsigned int flags;
 	int ret = 0;

gfp_mask = (gfp_mask & GFP_RECLAIM_MASK);

But it might be better to do this in alloc_vmap_area(). In alloc_vmap_area() we have this:

retry:

	if (IS_ERR_VALUE(addr)) {
		preload_this_cpu_lock(&free_vmap_area_lock, gfp_mask, node);

which probably needs GFP_RECLAIM_MASK too.

Quoted 7 lines
mm/kasan/shadow.c +1 -1
-	data.pages = (struct page **)__get_free_page(GFP_KERNEL | __GFP_ZERO);
+	data.pages = (struct page **)__get_free_page(gfp_mask | __GFP_ZERO);
 	if (!data.pages)
 		return -ENOMEM;
 
 	while (nr_total) {

mm/kasan/shadow.c static int ___alloc_pages_bulk(struct page **pages, int nr_pages) · line 377

Quoted 10 lines
mm/kasan/shadow.c +4 -1
 		nr_pages = min(nr_total, PAGE_SIZE / sizeof(data.pages[0]));
-		ret = ___alloc_pages_bulk(data.pages, nr_pages);
+		ret = ___alloc_pages_bulk(data.pages, nr_pages, gfp_mask);
 		if (ret)
 			break;
 
 		data.start = start;
+		if (noblock)
+			flags = memalloc_noreclaim_save();
+

This should be the same as in __vmalloc_area_node():

	if (noblock)
		flags = memalloc_noreclaim_save();
	else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)

flags = memalloc_nofs_save();
else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0)
flags = memalloc_noio_save();

It would be better to fix noio/nofs stuff first with separate patch, as it's bug and needs cc stable. And add support for noblock in follow up.

It might be a good idea to consolidate such logic in separate function, memalloc_save(gfp_mask)/memalloc_restore(gfp_mask, flags) ?

Quoted 7 lines
mm/kasan/shadow.c +3 -0
 		ret = apply_to_page_range(&init_mm, start, nr_pages * PAGE_SIZE,
 					  kasan_populate_vmalloc_pte, &data);
+		if (noblock)
+			memalloc_noreclaim_restore(flags);
+
 		___free_pages_bulk(data.pages, nr_pages);
 		if (ret)
author maintainer
#33 lore raw
In reply to Marco Elver
Quoted 24 lines · Marco Elver
Quoted 17 lines · Uladzislau Rezki (Sony)

Hello.

This is a second series of making __vmalloc() to support GFP_ATOMIC and GFP_NOWAIT flags. It tends to improve the non-blocking behaviour.

The first one can be found here:

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

that was an RFC. Using this series for testing i have not found more places which can trigger: scheduling during atomic. Though there is one which requires attention. I will explain in [1].

Please note, non-blocking gets improved in the __vmalloc() call only, i.e. vmalloc_huge() still contains in its paths many cond_resched() points and can not be used as non-blocking as of now.

Quoted 5 lines

[1] The vmap_pages_range_noflush() contains the kmsan_vmap_pages_range_noflush() external implementation for KCSAN specifically which is hard coded to GFP_KERNEL. The kernel should be built with CONFIG_KCSAN option. To me it looks like not straight forward to run such kernel on my box, therefore i need more time to investigate what is wrong with CONFIG_KCSAN and my env.

Quoted 4 lines

KMSAN or KCSAN?

[+Cc KMSAN maintainers]

Sorry for type, yes, that was about CONFIG_KMSAN.

--
Uladzislau Rezki

author maintainer
#5 lore raw

Re: [PATCH 3/8] mm/vmalloc: Support non-blocking GFP flags in alloc_vmap_area()

In reply to Michal Hocko
Quoted 12 lines · Michal Hocko
Quoted 3 lines · Uladzislau Rezki

alloc_vmap_area() currently assumes that sleeping is allowed during allocation. This is not true for callers which pass non-blocking GFP flags, such as GFP_ATOMIC or GFP_NOWAIT.

Those are currently not allowed so it would be better to mention this is a preparation for those to _be_ supported later in the series.

Quoted 4 lines

This patch adds logic to detect whether the given gfp_mask permits blocking. It avoids invoking might_sleep() or falling back to reclaim path if blocking is not allowed.

Quoted 9 lines
Quoted 4 lines

This makes alloc_vmap_area() safer for use in non-sleeping contexts, where previously it could hit unexpected sleeps, trigger warnings.

With the changelog clarified

Thanks!

Thank you! Added in the end:

It is a preparation and adjustment step to later allow both GFP_ATOMIC and GFP_NOWAIT allocations in this series.

--
Uladzislau Rezki

author maintainer
#9 lore raw

Re: [PATCH 4/8] mm/vmalloc: Remove cond_resched() in vm_area_alloc_pages()

In reply to Michal Hocko
Quoted 21 lines · Michal Hocko
Quoted 3 lines · Uladzislau Rezki

The vm_area_alloc_pages() function uses cond_resched() to yield the CPU during potentially long-running loops. However, these loops are not considered long-running under normal conditions.

To be more precise they can take long if they dive into the page allocator but that already involves cond_rescheds where appropriate so these are not needed in fact.

Quoted 12 lines

In non-blocking
contexts, calling cond_resched() is inappropriate also.

Remove these calls to ensure correctness for blocking/non-blocking contexts. This also simplifies the code path. In fact, a slow path of page allocator already includes reschedule points to mitigate latency.

This patch was tested for !CONFIG_PREEMPT kernel and with large allocation chunks(~1GB), without triggering any "BUG: soft lockup" warnings.

Quoted 6 lines
Quoted 1 line

Thanks!

Updated the commit message. Right, it can take long time.

Thank you!

--
Uladzislau Rezki

author maintainer
#13 lore raw

Re: [PATCH 5/8] mm/kasan, mm/vmalloc: Respect GFP flags in kasan_populate_vmalloc()

In reply to Andrey Ryabinin
Quoted 39 lines · Andrey Ryabinin
Quoted 22 lines · Uladzislau Rezki (Sony)
mm/kasan/shadow.c +2 -2
index d2c70cd2afb1..5edfc1f6b53e 100644
@@ -335,13 +335,13 @@ static void ___free_pages_bulk(struct page **pages, int nr_pages)
 	}
 }
 
-static int ___alloc_pages_bulk(struct page **pages, int nr_pages)
+static int ___alloc_pages_bulk(struct page **pages, int nr_pages, gfp_t gfp_mask)
 {
 	unsigned long nr_populated, nr_total = nr_pages;
 	struct page **page_array = pages;
 
 	while (nr_pages) {
-		nr_populated = alloc_pages_bulk(GFP_KERNEL, nr_pages, pages);
+		nr_populated = alloc_pages_bulk(gfp_mask, nr_pages, pages);
 		if (!nr_populated) {
 			___free_pages_bulk(page_array, nr_total - nr_pages);
 			return -ENOMEM;
@@ -353,25 +353,33 @@ static int ___alloc_pages_bulk(struct page **pages, int nr_pages)
 	return 0;

mm/kasan/shadow.c static int ___alloc_pages_bulk(struct page **pages, int nr_pages) · line 362

Quoted 10 lines
mm/kasan/shadow.c +3 -1
 }
 
-static int __kasan_populate_vmalloc(unsigned long start, unsigned long end)
+static int __kasan_populate_vmalloc(unsigned long start, unsigned long end, gfp_t gfp_mask)
 {
 	unsigned long nr_pages, nr_total = PFN_UP(end - start);
+	bool noblock = !gfpflags_allow_blocking(gfp_mask);
 	struct vmalloc_populate_data data;
+	unsigned int flags;
 	int ret = 0;

gfp_mask = (gfp_mask & GFP_RECLAIM_MASK);

Quoted 9 lines

But it might be better to do this in alloc_vmap_area(). In alloc_vmap_area() we have this:

retry:

	if (IS_ERR_VALUE(addr)) {
		preload_this_cpu_lock(&free_vmap_area_lock, gfp_mask, node);

which probably needs GFP_RECLAIM_MASK too.

Thank you for pointing to this. I will check it!

Quoted 29 lines
Quoted 7 lines
mm/kasan/shadow.c +1 -1
-	data.pages = (struct page **)__get_free_page(GFP_KERNEL | __GFP_ZERO);
+	data.pages = (struct page **)__get_free_page(gfp_mask | __GFP_ZERO);
 	if (!data.pages)
 		return -ENOMEM;
 
 	while (nr_total) {

mm/kasan/shadow.c static int ___alloc_pages_bulk(struct page **pages, int nr_pages) · line 377

Quoted 10 lines
mm/kasan/shadow.c +4 -1
 		nr_pages = min(nr_total, PAGE_SIZE / sizeof(data.pages[0]));
-		ret = ___alloc_pages_bulk(data.pages, nr_pages);
+		ret = ___alloc_pages_bulk(data.pages, nr_pages, gfp_mask);
 		if (ret)
 			break;
 
 		data.start = start;
+		if (noblock)
+			flags = memalloc_noreclaim_save();
+

This should be the same as in __vmalloc_area_node():

	if (noblock)
		flags = memalloc_noreclaim_save();
	else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)
(quoted diff) +0 -0
 		flags = memalloc_nofs_save();
 	else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0)
 		flags = memalloc_noio_save();
Quoted 3 lines

It would be better to fix noio/nofs stuff first with separate patch, as it's bug and needs cc stable. And add support for noblock in follow up.

Right. KASAN was not fixed together with vmalloc. I will look into it.

Quoted 11 lines

It might be a good idea to consolidate such logic in separate function, memalloc_save(gfp_mask)/memalloc_restore(gfp_mask, flags) ?

mm/kasan/shadow.c static int ___alloc_pages_bulk(struct page **pages, int nr_pages) · line 384

Quoted 7 lines
mm/kasan/shadow.c +3 -0
 		ret = apply_to_page_range(&init_mm, start, nr_pages * PAGE_SIZE,
 					  kasan_populate_vmalloc_pte, &data);
+		if (noblock)
+			memalloc_noreclaim_restore(flags);
+
 		___free_pages_bulk(data.pages, nr_pages);
 		if (ret)

Sounds good.

Thank you.

--
Uladzislau Rezki

author maintainer Applied
#16 lore raw

Re: [PATCH 6/8] mm/vmalloc: Defer freeing partly initialized vm_struct

In reply to Michal Hocko
Quoted 11 lines · Michal Hocko
Quoted 10 lines · Uladzislau Rezki

__vmalloc_area_node() may call free_vmap_area() or vfree() on error paths, both of which can sleep. This becomes problematic if the function is invoked from an atomic context, such as when GFP_ATOMIC or GFP_NOWAIT is passed via gfp_mask.

To fix this, unify error paths and defer the cleanup of partly initialized vm_struct objects to a workqueue. This ensures that freeing happens in a process context and avoids invalid sleeps in atomic regions.

Quoted 6 lines
Quoted 1 line

LGTM

Thanks!

Thanks, applied Acked-by.

--
Uladzislau Rezki

author maintainer Applied
#23 lore raw

Re: [PATCH 7/8] mm/vmalloc: Support non-blocking GFP flags in __vmalloc_area_node()

In reply to Michal Hocko
Quoted 7 lines · Michal Hocko
Quoted 5 lines · Uladzislau Rezki

This patch makes __vmalloc_area_node() to correctly handle non-blocking allocation requests, such as GFP_ATOMIC and GFP_NOWAIT. Main changes:

- Add a __GFP_HIGHMEM to gfp_mask only for blocking requests if there are no DMA constraints.

Quoted 8 lines

This begs for a more explanation. Why does __GFP_HIGHMEM matters? I suspect this is due to kmapping of those pages but that could be done in an atomic way. But in practice I do not think we really care about highmem all that much for vmalloc. The vmalloc space is really tiny for 32b systems where highmem matters and failing vmalloc allocations due to lack is of __GFP_HIGHMEM is hard to consider important if relevant at all.

Thank you for this note. Yes, __GFP_HIGHMEM is about 32 bit systems. Initially, in the RFC series, during testing i saw some incompatibility kernel splats when use together with non-blocking flags.

Whereas i do not see it anymore and now. It looks like i messed up something when testing pre-RFC series.

I will not touch it. I mean i will keep it as it used to be, i.e. apply it if no (GFP_DMA | GFP_DMA32).

Quoted 14 lines
Quoted 8 lines

- vmap_page_range() is wrapped by memalloc_noreclaim_save/restore() to avoid memory reclaim related operations that could sleep during page table setup or mapping pages.

This is particularly important for page table allocations that internally use GFP_PGTABLE_KERNEL, which may sleep unless such scope restrictions are applied. For example:

Quoted 5 lines

<snip>
__pte_alloc_kernel()

    pte_alloc_one_kernel(&init_mm);
        pagetable_alloc_noprof(GFP_PGTABLE_KERNEL & ~__GFP_HIGHMEM, 0);

<snip>

Quoted 8 lines

As I've said in several occations, I am not entirely happy about this approach because it doesn't really guarantee atomicty. If any architecture decides to use some sleeping locking down that path then the whole thing just blows up. On the other hand this is mostly a theoretical concern at this stage and this is a feature people have been asking for a long time (especially from kvmalloc side) so better good than perfect that his.

I agree with it. Unfortunately i can not control the PTE kernel allocation layer.

Quoted 5 lines

That being said, you are missing __kvmalloc_node_noprof, __vmalloc_node_range_noprof (and maybe some more places) documentation update.

I would like to fix documentation in separate patch. That was deliberately.

Quoted 9 lines
Quoted 5 lines

Note: in most cases, PTE entries are established only up to the level required by current vmap space usage, meaning the page tables are typically fully populated during the mapping process.

With the doc part fixed

Thanks!

Thanks! Applied.

--
Uladzislau Rezki

author maintainer
#29 lore raw

Re: [PATCH 8/8] mm: Drop __GFP_DIRECT_RECLAIM flag if PF_MEMALLOC is set

In reply to Michal Hocko
Quoted 8 lines · Michal Hocko
Quoted 3 lines · Uladzislau Rezki

The memory allocator already avoids reclaim when PF_MEMALLOC is set. Clear __GFP_DIRECT_RECLAIM explicitly to suppress might_alloc() warnings to make more correct behavior.

Rather than chaning the gfp mask would it make more sense to update might_alloc instead?

Hm.. I was thinking about it but decided to drop the __GFP_DIRECT_RECLAIM instead just to guarantee a no-reclaim behaviour, as it is written now to the flag.

From the other hand after this patch we would have some unneeded/dead checks(if i do not missing anything). For example:

[1]

    WARN_ON_ONCE(!can_direct_reclaim);
    /*
     * PF_MEMALLOC request from this context is rather bizarre
     * because we cannot reclaim anything and only can loop waiting
     * for somebody to do a work for us.
     */
    WARN_ON_ONCE(current->flags & PF_MEMALLOC);
[2]
    /* no reclaim without waiting on it */
    if (!(gfp_mask & __GFP_DIRECT_RECLAIM))
        return false;

    /* this guy won't enter reclaim */
    if (current->flags & PF_MEMALLOC)
        return false;

[3]

    /* Caller is not willing to reclaim, we can't balance anything */
    if (!can_direct_reclaim)
        goto nopage;

    /* Avoid recursion of direct reclaim */
    if (current->flags & PF_MEMALLOC)
        goto nopage;

etc.

But, yes, might_alloc() can be modified also.

--
Uladzislau Rezki

reviewer
#30 lore raw

Re: [PATCH 8/8] mm: Drop __GFP_DIRECT_RECLAIM flag if PF_MEMALLOC is set

Quoted 41 lines · Uladzislau Rezki
Quoted 8 lines · Michal Hocko
Quoted 3 lines · Uladzislau Rezki

The memory allocator already avoids reclaim when PF_MEMALLOC is set. Clear __GFP_DIRECT_RECLAIM explicitly to suppress might_alloc() warnings to make more correct behavior.

Rather than chaning the gfp mask would it make more sense to update might_alloc instead?

Hm.. I was thinking about it but decided to drop the __GFP_DIRECT_RECLAIM instead just to guarantee a no-reclaim behaviour, as it is written now to the flag.

Quoted 1 line

From the other hand after this patch we would have some unneeded/dead

checks(if i do not missing anything). For example:

[1]

    WARN_ON_ONCE(!can_direct_reclaim);
    /*
     * PF_MEMALLOC request from this context is rather bizarre
     * because we cannot reclaim anything and only can loop waiting
     * for somebody to do a work for us.
     */
    WARN_ON_ONCE(current->flags & PF_MEMALLOC);
[2]
    /* no reclaim without waiting on it */
    if (!(gfp_mask & __GFP_DIRECT_RECLAIM))
        return false;

    /* this guy won't enter reclaim */
    if (current->flags & PF_MEMALLOC)
        return false;

[3]

    /* Caller is not willing to reclaim, we can't balance anything */
    if (!can_direct_reclaim)
        goto nopage;

    /* Avoid recursion of direct reclaim */
    if (current->flags & PF_MEMALLOC)
        goto nopage;
Quoted 3 lines

etc.

But, yes, might_alloc() can be modified also.

I do not have a _strong_ preference but my slight preference would be to deal with this in might_alloc. Not sure what other think.

Signature · 2 lines
Michal Hocko
SUSE Labs
author maintainer
#31 lore raw

Re: [PATCH 8/8] mm: Drop __GFP_DIRECT_RECLAIM flag if PF_MEMALLOC is set

Quoted 44 lines · Michal Hocko
Quoted 43 lines · Uladzislau Rezki
Quoted 8 lines · Michal Hocko
Quoted 3 lines · Uladzislau Rezki

The memory allocator already avoids reclaim when PF_MEMALLOC is set. Clear __GFP_DIRECT_RECLAIM explicitly to suppress might_alloc() warnings to make more correct behavior.

Rather than chaning the gfp mask would it make more sense to update might_alloc instead?

Hm.. I was thinking about it but decided to drop the __GFP_DIRECT_RECLAIM instead just to guarantee a no-reclaim behaviour, as it is written now to the flag.

Quoted 1 line

From the other hand after this patch we would have some unneeded/dead

checks(if i do not missing anything). For example:

[1]

    WARN_ON_ONCE(!can_direct_reclaim);
    /*
     * PF_MEMALLOC request from this context is rather bizarre
     * because we cannot reclaim anything and only can loop waiting
     * for somebody to do a work for us.
     */
    WARN_ON_ONCE(current->flags & PF_MEMALLOC);
[2]
    /* no reclaim without waiting on it */
    if (!(gfp_mask & __GFP_DIRECT_RECLAIM))
        return false;

    /* this guy won't enter reclaim */
    if (current->flags & PF_MEMALLOC)
        return false;

[3]

    /* Caller is not willing to reclaim, we can't balance anything */
    if (!can_direct_reclaim)
        goto nopage;

    /* Avoid recursion of direct reclaim */
    if (current->flags & PF_MEMALLOC)
        goto nopage;

etc.

Quoted 5 lines
Quoted 1 line

But, yes, might_alloc() can be modified also.

I do not have a _strong_ preference but my slight preference would be to deal with this in might_alloc. Not sure what other think.

No problem, that i can easily switch to.

--
Uladzisau Rezki

Reviewed-by on v1 3/8
#6 lore raw

Re: [PATCH 3/8] mm/vmalloc: Support non-blocking GFP flags in alloc_vmap_area()

In reply to Uladzislau Rezki (Sony)
Quoted 8 lines · Uladzislau Rezki (Sony)

alloc_vmap_area() currently assumes that sleeping is allowed during allocation. This is not true for callers which pass non-blocking GFP flags, such as GFP_ATOMIC or GFP_NOWAIT.

This patch adds logic to detect whether the given gfp_mask permits blocking. It avoids invoking might_sleep() or falling back to reclaim path if blocking is not allowed.

Quoted 7 lines

This makes alloc_vmap_area() safer for use in non-sleeping contexts, where previously it could hit unexpected sleeps, trigger warnings.

---
 mm/vmalloc.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

LGTM,

Quoted 53 lines
mm/vmalloc.c +14 -3
index 6dbcdceecae1..81b6d3bde719 100644
@@ -2017,6 +2017,7 @@ static struct vmap_area *alloc_vmap_area(unsigned long size,
 	unsigned long freed;
 	unsigned long addr;
 	unsigned int vn_id;
+	bool allow_block;
 	int purged = 0;
 	int ret;
 
@@ -2026,7 +2027,8 @@ static struct vmap_area *alloc_vmap_area(unsigned long size,
 	if (unlikely(!vmap_initialized))
 		return ERR_PTR(-EBUSY);
 
-	might_sleep();
+	allow_block = gfpflags_allow_blocking(gfp_mask);
+	might_sleep_if(allow_block);
 
 	/*
 	 * If a VA is obtained from a global heap(if it fails here)
@@ -2065,8 +2067,16 @@ static struct vmap_area *alloc_vmap_area(unsigned long size,
 	 * If an allocation fails, the error value is
 	 * returned. Therefore trigger the overflow path.
 	 */
-	if (IS_ERR_VALUE(addr))
-		goto overflow;
+	if (IS_ERR_VALUE(addr)) {
+		if (allow_block)
+			goto overflow;
+
+		/*
+		 * We can not trigger any reclaim logic because
+		 * sleeping is not allowed, thus fail an allocation.
+		 */
+		goto error;
+	}
 
 	va->va_start = addr;
 	va->va_end = addr + size;
@@ -2116,6 +2126,7 @@ static struct vmap_area *alloc_vmap_area(unsigned long size,
 		pr_warn("vmalloc_node_range for size %lu failed: Address range restricted to %#lx - %#lx\n",
 				size, vstart, vend);
 
+error:
 	kmem_cache_free(vmap_area_cachep, va);
 	return ERR_PTR(-EBUSY);
 }
Signature · 1 line
2.39.5
Reviewed-by on v1 4/8
#10 lore raw

Re: [PATCH 4/8] mm/vmalloc: Remove cond_resched() in vm_area_alloc_pages()

In reply to Uladzislau Rezki (Sony)
Quoted 10 lines · Uladzislau Rezki (Sony)

The vm_area_alloc_pages() function uses cond_resched() to yield the CPU during potentially long-running loops. However, these loops are not considered long-running under normal conditions. In non-blocking contexts, calling cond_resched() is inappropriate also.

Remove these calls to ensure correctness for blocking/non-blocking contexts. This also simplifies the code path. In fact, a slow path of page allocator already includes reschedule points to mitigate latency.

Quoted 8 lines

This patch was tested for !CONFIG_PREEMPT kernel and with large allocation chunks(~1GB), without triggering any "BUG: soft lockup" warnings.

---
 mm/vmalloc.c | 2 --
 1 file changed, 2 deletions(-)
Quoted 24 lines
mm/vmalloc.c +0 -2
index 81b6d3bde719..b0255e0c74b3 100644
@@ -3633,7 +3633,6 @@ vm_area_alloc_pages(gfp_t gfp, int nid,
 							pages + nr_allocated);
 
 			nr_allocated += nr;
-			cond_resched();
 
 			/*
 			 * If zero or pages were obtained partly,
@@ -3675,7 +3674,6 @@ vm_area_alloc_pages(gfp_t gfp, int nid,
 		for (i = 0; i < (1U << order); i++)
 			pages[nr_allocated + i] = page + i;
 
-		cond_resched();
 		nr_allocated += 1U << order;
 	}
 
Signature · 1 line
2.39.5
#17 lore raw

Re: [PATCH 6/8] mm/vmalloc: Defer freeing partly initialized vm_struct

In reply to Uladzislau Rezki (Sony)
Quoted 59 lines · Uladzislau Rezki (Sony)

__vmalloc_area_node() may call free_vmap_area() or vfree() on error paths, both of which can sleep. This becomes problematic if the function is invoked from an atomic context, such as when GFP_ATOMIC or GFP_NOWAIT is passed via gfp_mask.

To fix this, unify error paths and defer the cleanup of partly initialized vm_struct objects to a workqueue. This ensures that freeing happens in a process context and avoids invalid sleeps in atomic regions.

---
 include/linux/vmalloc.h |  6 +++++-
 mm/vmalloc.c            | 34 +++++++++++++++++++++++++++++++---
 2 files changed, 36 insertions(+), 4 deletions(-)
include/linux/vmalloc.h +5 -1
index fdc9aeb74a44..b1425fae8cbf 100644
@@ -50,7 +50,11 @@ struct iov_iter;		/* in uio.h */
 #endif
 
 struct vm_struct {
-	struct vm_struct	*next;
+	union {
+		struct vm_struct *next;	  /* Early registration of vm_areas. */
+		struct llist_node llnode; /* Asynchronous freeing on error paths. */
+	};
+
 	void			*addr;
 	unsigned long		size;
 	unsigned long		flags;
mm/vmalloc.c +18 -0
index 7f48a54ec108..2424f80d524a 100644
@@ -3680,6 +3680,35 @@ vm_area_alloc_pages(gfp_t gfp, int nid,
 	return nr_allocated;
 }
 
+static LLIST_HEAD(pending_vm_area_cleanup);
+static void cleanup_vm_area_work(struct work_struct *work)
+{
+	struct vm_struct *area, *tmp;
+	struct llist_node *head;
+
+	head = llist_del_all(&pending_vm_area_cleanup);
+	if (!head)
+		return;
+
+	llist_for_each_entry_safe(area, tmp, head, llnode) {
+		if (!area->pages)
+			free_vm_area(area);
+		else
+			vfree(area->addr);
+	}
+}
+

mm/vmalloc.c vm_area_alloc_pages(gfp_t gfp, int nid, · line 3710

Quoted 10 lines
mm/vmalloc.c +10 -0
+/*
+ * Helper for __vmalloc_area_node() to defer cleanup
+ * of partially initialized vm_struct in error paths.
+ */
+static DECLARE_WORK(cleanup_vm_area, cleanup_vm_area_work);
+static void defer_vm_area_cleanup(struct vm_struct *area)
+{
+	if (llist_add(&area->llnode, &pending_vm_area_cleanup))
+		schedule_work(&cleanup_vm_area);
+}

Wondering why here we need call schudule_work() when pending_vm_area_cleanup was empty before adding new entry. Shouldn't it be as below to schedule the job? Not sure if I miss anything.

	if (!llist_add(&area->llnode, &pending_vm_area_cleanup))
		schedule_work(&cleanup_vm_area);

=====
/**
* llist_add - add a new entry
* @new: new entry to be added
* @head: the head for your lock-less list
*
* Returns true if the list was empty prior to adding this entry. */
static inline bool llist_add(struct llist_node *new, struct llist_head *head)

{
        return llist_add_batch(new, new, head);
}

=====

Quoted 26 lines
mm/vmalloc.c +3 -3
+
 static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 				 pgprot_t prot, unsigned int page_shift,
 				 int node)
@@ -3711,8 +3740,7 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 		warn_alloc(gfp_mask, NULL,
 			"vmalloc error: size %lu, failed to allocated page array size %lu",
 			nr_small_pages * PAGE_SIZE, array_size);
-		free_vm_area(area);
-		return NULL;
+		goto fail;
 	}
 
 	set_vm_area_page_order(area, page_shift - PAGE_SHIFT);
@@ -3789,7 +3817,7 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 	return area->addr;
 
 fail:
-	vfree(area->addr);
+	defer_vm_area_cleanup(area);
 	return NULL;
 }
 
Signature · 1 line
2.39.5
#24 lore raw

Re: [PATCH 7/8] mm/vmalloc: Support non-blocking GFP flags in __vmalloc_area_node()

In reply to Uladzislau Rezki (Sony)
Quoted 68 lines · Uladzislau Rezki (Sony)

This patch makes __vmalloc_area_node() to correctly handle non-blocking allocation requests, such as GFP_ATOMIC and GFP_NOWAIT. Main changes:

- Add a __GFP_HIGHMEM to gfp_mask only for blocking requests if there are no DMA constraints.

- vmap_page_range() is wrapped by memalloc_noreclaim_save/restore() to avoid memory reclaim related operations that could sleep during page table setup or mapping pages.

This is particularly important for page table allocations that internally use GFP_PGTABLE_KERNEL, which may sleep unless such scope restrictions are applied. For example:

<snip>
__pte_alloc_kernel()

    pte_alloc_one_kernel(&init_mm);
        pagetable_alloc_noprof(GFP_PGTABLE_KERNEL & ~__GFP_HIGHMEM, 0);

<snip>

Note: in most cases, PTE entries are established only up to the level required by current vmap space usage, meaning the page tables are typically fully populated during the mapping process.

---
 mm/vmalloc.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
mm/vmalloc.c +13 -3
index 2424f80d524a..8a7eab810561 100644
@@ -3721,12 +3721,20 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 	unsigned int nr_small_pages = size >> PAGE_SHIFT;
 	unsigned int page_order;
 	unsigned int flags;
+	bool noblock;
 	int ret;
 
 	array_size = (unsigned long)nr_small_pages * sizeof(struct page *);
+	noblock = !gfpflags_allow_blocking(gfp_mask);
 
-	if (!(gfp_mask & (GFP_DMA | GFP_DMA32)))
-		gfp_mask |= __GFP_HIGHMEM;
+	if (noblock) {
+		/* __GFP_NOFAIL and "noblock" flags are mutually exclusive. */
+		nofail = false;
+	} else {
+		/* Allow highmem allocations if there are no DMA constraints. */
+		if (!(gfp_mask & (GFP_DMA | GFP_DMA32)))
+			gfp_mask |= __GFP_HIGHMEM;
+	}
 
 	/* Please note that the recursion is strictly bounded. */
 	if (array_size > PAGE_SIZE) {
@@ -3790,7 +3798,9 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 	 * page tables allocations ignore external gfp mask, enforce it
 	 * by the scope API
 	 */
-	if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)
+	if (noblock)
+		flags = memalloc_noreclaim_save();
+	else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)
 		flags = memalloc_nofs_save();
 	else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0)
 		flags = memalloc_noio_save();

mm/vmalloc.c static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, · line 3820

Quoted 10 lines
mm/vmalloc.c +3 -1
@@ -3802,7 +3812,9 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 			schedule_timeout_uninterruptible(1);
 	} while (nofail && (ret < 0));
 
-	if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)
+	if (noblock)
+		memalloc_noreclaim_restore(flags);
+	else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)
 		memalloc_nofs_restore(flags);
 	else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0)
 		memalloc_noio_restore(flags);

Can we use memalloc_flags_restore(flags) directly to replace above if else checking? It can reduce LOC, might be not as readable as the change in patch surely. Not strong opinion.

	memalloc_flags_restore(flags);
author maintainer
#18 lore raw

Re: [PATCH 6/8] mm/vmalloc: Defer freeing partly initialized vm_struct

In reply to Baoquan He
Quoted 87 lines · Baoquan He
Quoted 59 lines · Uladzislau Rezki (Sony)

__vmalloc_area_node() may call free_vmap_area() or vfree() on error paths, both of which can sleep. This becomes problematic if the function is invoked from an atomic context, such as when GFP_ATOMIC or GFP_NOWAIT is passed via gfp_mask.

To fix this, unify error paths and defer the cleanup of partly initialized vm_struct objects to a workqueue. This ensures that freeing happens in a process context and avoids invalid sleeps in atomic regions.

---
 include/linux/vmalloc.h |  6 +++++-
 mm/vmalloc.c            | 34 +++++++++++++++++++++++++++++++---
 2 files changed, 36 insertions(+), 4 deletions(-)
include/linux/vmalloc.h +5 -1
index fdc9aeb74a44..b1425fae8cbf 100644
@@ -50,7 +50,11 @@ struct iov_iter;		/* in uio.h */
 #endif
 
 struct vm_struct {
-	struct vm_struct	*next;
+	union {
+		struct vm_struct *next;	  /* Early registration of vm_areas. */
+		struct llist_node llnode; /* Asynchronous freeing on error paths. */
+	};
+
 	void			*addr;
 	unsigned long		size;
 	unsigned long		flags;
mm/vmalloc.c +18 -0
index 7f48a54ec108..2424f80d524a 100644
@@ -3680,6 +3680,35 @@ vm_area_alloc_pages(gfp_t gfp, int nid,
 	return nr_allocated;
 }
 
+static LLIST_HEAD(pending_vm_area_cleanup);
+static void cleanup_vm_area_work(struct work_struct *work)
+{
+	struct vm_struct *area, *tmp;
+	struct llist_node *head;
+
+	head = llist_del_all(&pending_vm_area_cleanup);
+	if (!head)
+		return;
+
+	llist_for_each_entry_safe(area, tmp, head, llnode) {
+		if (!area->pages)
+			free_vm_area(area);
+		else
+			vfree(area->addr);
+	}
+}
+

mm/vmalloc.c vm_area_alloc_pages(gfp_t gfp, int nid, · line 3710

Quoted 10 lines
mm/vmalloc.c +10 -0
+/*
+ * Helper for __vmalloc_area_node() to defer cleanup
+ * of partially initialized vm_struct in error paths.
+ */
+static DECLARE_WORK(cleanup_vm_area, cleanup_vm_area_work);
+static void defer_vm_area_cleanup(struct vm_struct *area)
+{
+	if (llist_add(&area->llnode, &pending_vm_area_cleanup))
+		schedule_work(&cleanup_vm_area);
+}

Wondering why here we need call schudule_work() when pending_vm_area_cleanup was empty before adding new entry. Shouldn't it be as below to schedule the job? Not sure if I miss anything.

	if (!llist_add(&area->llnode, &pending_vm_area_cleanup))
		schedule_work(&cleanup_vm_area);

=====
/**
* llist_add - add a new entry
* @new: new entry to be added
* @head: the head for your lock-less list
*
* Returns true if the list was empty prior to adding this entry. */
static inline bool llist_add(struct llist_node *new, struct llist_head *head)

Quoted 5 lines
{
        return llist_add_batch(new, new, head);
}

=====

But then you will not schedule. If the list is empty, we add one element llist_add() returns 1, but your condition expects 0.

How it works:

If someone keeps adding to the llist and it is not empty we should not trigger a new work, because a current work is in flight(it will cover new comers), i.e. it has been scheduled but it has not yet completed llist_del_all() on the head.

Once it is done, a new comer will trigger a work again only if it sees NULL, i.e. when the list is empty.

--
Uladzislau Rezki

author maintainer
#25 lore raw

Re: [PATCH 7/8] mm/vmalloc: Support non-blocking GFP flags in __vmalloc_area_node()

In reply to Baoquan He
Quoted 84 lines · Baoquan He
Quoted 68 lines · Uladzislau Rezki (Sony)

This patch makes __vmalloc_area_node() to correctly handle non-blocking allocation requests, such as GFP_ATOMIC and GFP_NOWAIT. Main changes:

- Add a __GFP_HIGHMEM to gfp_mask only for blocking requests if there are no DMA constraints.

- vmap_page_range() is wrapped by memalloc_noreclaim_save/restore() to avoid memory reclaim related operations that could sleep during page table setup or mapping pages.

This is particularly important for page table allocations that internally use GFP_PGTABLE_KERNEL, which may sleep unless such scope restrictions are applied. For example:

<snip>
__pte_alloc_kernel()

    pte_alloc_one_kernel(&init_mm);
        pagetable_alloc_noprof(GFP_PGTABLE_KERNEL & ~__GFP_HIGHMEM, 0);

<snip>

Note: in most cases, PTE entries are established only up to the level required by current vmap space usage, meaning the page tables are typically fully populated during the mapping process.

---
 mm/vmalloc.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
mm/vmalloc.c +13 -3
index 2424f80d524a..8a7eab810561 100644
@@ -3721,12 +3721,20 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 	unsigned int nr_small_pages = size >> PAGE_SHIFT;
 	unsigned int page_order;
 	unsigned int flags;
+	bool noblock;
 	int ret;
 
 	array_size = (unsigned long)nr_small_pages * sizeof(struct page *);
+	noblock = !gfpflags_allow_blocking(gfp_mask);
 
-	if (!(gfp_mask & (GFP_DMA | GFP_DMA32)))
-		gfp_mask |= __GFP_HIGHMEM;
+	if (noblock) {
+		/* __GFP_NOFAIL and "noblock" flags are mutually exclusive. */
+		nofail = false;
+	} else {
+		/* Allow highmem allocations if there are no DMA constraints. */
+		if (!(gfp_mask & (GFP_DMA | GFP_DMA32)))
+			gfp_mask |= __GFP_HIGHMEM;
+	}
 
 	/* Please note that the recursion is strictly bounded. */
 	if (array_size > PAGE_SIZE) {
@@ -3790,7 +3798,9 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 	 * page tables allocations ignore external gfp mask, enforce it
 	 * by the scope API
 	 */
-	if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)
+	if (noblock)
+		flags = memalloc_noreclaim_save();
+	else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)
 		flags = memalloc_nofs_save();
 	else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0)
 		flags = memalloc_noio_save();

mm/vmalloc.c static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, · line 3820

Quoted 10 lines
mm/vmalloc.c +3 -1
@@ -3802,7 +3812,9 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 			schedule_timeout_uninterruptible(1);
 	} while (nofail && (ret < 0));
 
-	if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)
+	if (noblock)
+		memalloc_noreclaim_restore(flags);
+	else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)
 		memalloc_nofs_restore(flags);
 	else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0)
 		memalloc_noio_restore(flags);

Can we use memalloc_flags_restore(flags) directly to replace above if else checking? It can reduce LOC, might be not as readable as the change in patch surely. Not strong opinion.

Quoted 2 lines
	memalloc_flags_restore(flags);

I agree, those if/else cases looks ugly. Maybe adding two save/restore functions are worth doing specifically for vmalloc part.

--
Uladzislau Rezki

#26 lore raw

Re: [PATCH 7/8] mm/vmalloc: Support non-blocking GFP flags in __vmalloc_area_node()

Quoted 85 lines · Uladzislau Rezki
Quoted 84 lines · Baoquan He
Quoted 68 lines · Uladzislau Rezki (Sony)

This patch makes __vmalloc_area_node() to correctly handle non-blocking allocation requests, such as GFP_ATOMIC and GFP_NOWAIT. Main changes:

- Add a __GFP_HIGHMEM to gfp_mask only for blocking requests if there are no DMA constraints.

- vmap_page_range() is wrapped by memalloc_noreclaim_save/restore() to avoid memory reclaim related operations that could sleep during page table setup or mapping pages.

This is particularly important for page table allocations that internally use GFP_PGTABLE_KERNEL, which may sleep unless such scope restrictions are applied. For example:

<snip>
__pte_alloc_kernel()

    pte_alloc_one_kernel(&init_mm);
        pagetable_alloc_noprof(GFP_PGTABLE_KERNEL & ~__GFP_HIGHMEM, 0);

<snip>

Note: in most cases, PTE entries are established only up to the level required by current vmap space usage, meaning the page tables are typically fully populated during the mapping process.

---
 mm/vmalloc.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
mm/vmalloc.c +13 -3
index 2424f80d524a..8a7eab810561 100644
@@ -3721,12 +3721,20 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 	unsigned int nr_small_pages = size >> PAGE_SHIFT;
 	unsigned int page_order;
 	unsigned int flags;
+	bool noblock;
 	int ret;
 
 	array_size = (unsigned long)nr_small_pages * sizeof(struct page *);
+	noblock = !gfpflags_allow_blocking(gfp_mask);
 
-	if (!(gfp_mask & (GFP_DMA | GFP_DMA32)))
-		gfp_mask |= __GFP_HIGHMEM;
+	if (noblock) {
+		/* __GFP_NOFAIL and "noblock" flags are mutually exclusive. */
+		nofail = false;
+	} else {
+		/* Allow highmem allocations if there are no DMA constraints. */
+		if (!(gfp_mask & (GFP_DMA | GFP_DMA32)))
+			gfp_mask |= __GFP_HIGHMEM;
+	}
 
 	/* Please note that the recursion is strictly bounded. */
 	if (array_size > PAGE_SIZE) {
@@ -3790,7 +3798,9 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 	 * page tables allocations ignore external gfp mask, enforce it
 	 * by the scope API
 	 */
-	if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)
+	if (noblock)
+		flags = memalloc_noreclaim_save();
+	else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)
 		flags = memalloc_nofs_save();
 	else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0)
 		flags = memalloc_noio_save();

mm/vmalloc.c static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, · line 3820

Quoted 10 lines
mm/vmalloc.c +3 -1
@@ -3802,7 +3812,9 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 			schedule_timeout_uninterruptible(1);
 	} while (nofail && (ret < 0));
 
-	if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)
+	if (noblock)
+		memalloc_noreclaim_restore(flags);
+	else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)
 		memalloc_nofs_restore(flags);
 	else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0)
 		memalloc_noio_restore(flags);

Can we use memalloc_flags_restore(flags) directly to replace above if else checking? It can reduce LOC, might be not as readable as the change in patch surely. Not strong opinion.

Quoted 4 lines
Quoted 2 lines
	memalloc_flags_restore(flags);

I agree, those if/else cases looks ugly. Maybe adding two save/restore functions are worth doing specifically for vmalloc part.

Yeah, that is also great idea.

#19 lore raw

Re: [PATCH 6/8] mm/vmalloc: Defer freeing partly initialized vm_struct

In reply to Uladzislau Rezki
Quoted 98 lines · Uladzislau Rezki
Quoted 87 lines · Baoquan He
Quoted 59 lines · Uladzislau Rezki (Sony)

__vmalloc_area_node() may call free_vmap_area() or vfree() on error paths, both of which can sleep. This becomes problematic if the function is invoked from an atomic context, such as when GFP_ATOMIC or GFP_NOWAIT is passed via gfp_mask.

To fix this, unify error paths and defer the cleanup of partly initialized vm_struct objects to a workqueue. This ensures that freeing happens in a process context and avoids invalid sleeps in atomic regions.

---
 include/linux/vmalloc.h |  6 +++++-
 mm/vmalloc.c            | 34 +++++++++++++++++++++++++++++++---
 2 files changed, 36 insertions(+), 4 deletions(-)
include/linux/vmalloc.h +5 -1
index fdc9aeb74a44..b1425fae8cbf 100644
@@ -50,7 +50,11 @@ struct iov_iter;		/* in uio.h */
 #endif
 
 struct vm_struct {
-	struct vm_struct	*next;
+	union {
+		struct vm_struct *next;	  /* Early registration of vm_areas. */
+		struct llist_node llnode; /* Asynchronous freeing on error paths. */
+	};
+
 	void			*addr;
 	unsigned long		size;
 	unsigned long		flags;
mm/vmalloc.c +18 -0
index 7f48a54ec108..2424f80d524a 100644
@@ -3680,6 +3680,35 @@ vm_area_alloc_pages(gfp_t gfp, int nid,
 	return nr_allocated;
 }
 
+static LLIST_HEAD(pending_vm_area_cleanup);
+static void cleanup_vm_area_work(struct work_struct *work)
+{
+	struct vm_struct *area, *tmp;
+	struct llist_node *head;
+
+	head = llist_del_all(&pending_vm_area_cleanup);
+	if (!head)
+		return;
+
+	llist_for_each_entry_safe(area, tmp, head, llnode) {
+		if (!area->pages)
+			free_vm_area(area);
+		else
+			vfree(area->addr);
+	}
+}
+

mm/vmalloc.c vm_area_alloc_pages(gfp_t gfp, int nid, · line 3710

Quoted 10 lines
mm/vmalloc.c +10 -0
+/*
+ * Helper for __vmalloc_area_node() to defer cleanup
+ * of partially initialized vm_struct in error paths.
+ */
+static DECLARE_WORK(cleanup_vm_area, cleanup_vm_area_work);
+static void defer_vm_area_cleanup(struct vm_struct *area)
+{
+	if (llist_add(&area->llnode, &pending_vm_area_cleanup))
+		schedule_work(&cleanup_vm_area);
+}

Wondering why here we need call schudule_work() when pending_vm_area_cleanup was empty before adding new entry. Shouldn't it be as below to schedule the job? Not sure if I miss anything.

	if (!llist_add(&area->llnode, &pending_vm_area_cleanup))
		schedule_work(&cleanup_vm_area);

=====
/**
* llist_add - add a new entry
* @new: new entry to be added
* @head: the head for your lock-less list
*
* Returns true if the list was empty prior to adding this entry. */
static inline bool llist_add(struct llist_node *new, struct llist_head *head)

Quoted 5 lines
{
        return llist_add_batch(new, new, head);
}

=====

But then you will not schedule. If the list is empty, we add one element llist_add() returns 1, but your condition expects 0.

How it works:

Quoted 7 lines

If someone keeps adding to the llist and it is not empty we should not trigger a new work, because a current work is in flight(it will cover new comers), i.e. it has been scheduled but it has not yet completed llist_del_all() on the head.

Once it is done, a new comer will trigger a work again only if it sees NULL, i.e. when the list is empty.

Fair enough. I thought it's a deferring work, in fact it's aiming to put the error handling in a workqueue, but not the current atomic context. Thanks for the explanation.

author maintainer
#20 lore raw

Re: [PATCH 6/8] mm/vmalloc: Defer freeing partly initialized vm_struct

Quoted 104 lines · Baoquan He
Quoted 103 lines · Uladzislau Rezki
Quoted 87 lines · Baoquan He
Quoted 59 lines · Uladzislau Rezki (Sony)

__vmalloc_area_node() may call free_vmap_area() or vfree() on error paths, both of which can sleep. This becomes problematic if the function is invoked from an atomic context, such as when GFP_ATOMIC or GFP_NOWAIT is passed via gfp_mask.

To fix this, unify error paths and defer the cleanup of partly initialized vm_struct objects to a workqueue. This ensures that freeing happens in a process context and avoids invalid sleeps in atomic regions.

---
 include/linux/vmalloc.h |  6 +++++-
 mm/vmalloc.c            | 34 +++++++++++++++++++++++++++++++---
 2 files changed, 36 insertions(+), 4 deletions(-)
include/linux/vmalloc.h +5 -1
index fdc9aeb74a44..b1425fae8cbf 100644
@@ -50,7 +50,11 @@ struct iov_iter;		/* in uio.h */
 #endif
 
 struct vm_struct {
-	struct vm_struct	*next;
+	union {
+		struct vm_struct *next;	  /* Early registration of vm_areas. */
+		struct llist_node llnode; /* Asynchronous freeing on error paths. */
+	};
+
 	void			*addr;
 	unsigned long		size;
 	unsigned long		flags;
mm/vmalloc.c +18 -0
index 7f48a54ec108..2424f80d524a 100644
@@ -3680,6 +3680,35 @@ vm_area_alloc_pages(gfp_t gfp, int nid,
 	return nr_allocated;
 }
 
+static LLIST_HEAD(pending_vm_area_cleanup);
+static void cleanup_vm_area_work(struct work_struct *work)
+{
+	struct vm_struct *area, *tmp;
+	struct llist_node *head;
+
+	head = llist_del_all(&pending_vm_area_cleanup);
+	if (!head)
+		return;
+
+	llist_for_each_entry_safe(area, tmp, head, llnode) {
+		if (!area->pages)
+			free_vm_area(area);
+		else
+			vfree(area->addr);
+	}
+}
+

mm/vmalloc.c vm_area_alloc_pages(gfp_t gfp, int nid, · line 3710

Quoted 10 lines
mm/vmalloc.c +10 -0
+/*
+ * Helper for __vmalloc_area_node() to defer cleanup
+ * of partially initialized vm_struct in error paths.
+ */
+static DECLARE_WORK(cleanup_vm_area, cleanup_vm_area_work);
+static void defer_vm_area_cleanup(struct vm_struct *area)
+{
+	if (llist_add(&area->llnode, &pending_vm_area_cleanup))
+		schedule_work(&cleanup_vm_area);
+}

Wondering why here we need call schudule_work() when pending_vm_area_cleanup was empty before adding new entry. Shouldn't it be as below to schedule the job? Not sure if I miss anything.

	if (!llist_add(&area->llnode, &pending_vm_area_cleanup))
		schedule_work(&cleanup_vm_area);

=====
/**
* llist_add - add a new entry
* @new: new entry to be added
* @head: the head for your lock-less list
*
* Returns true if the list was empty prior to adding this entry. */
static inline bool llist_add(struct llist_node *new, struct llist_head *head)

Quoted 5 lines
{
        return llist_add_batch(new, new, head);
}

=====

But then you will not schedule. If the list is empty, we add one element llist_add() returns 1, but your condition expects 0.

How it works:

If someone keeps adding to the llist and it is not empty we should not trigger a new work, because a current work is in flight(it will cover new comers), i.e. it has been scheduled but it has not yet completed llist_del_all() on the head.

Quoted 7 lines
Quoted 2 lines

Once it is done, a new comer will trigger a work again only if it sees NULL, i.e. when the list is empty.

Fair enough. I thought it's a deferring work, in fact it's aiming to put the error handling in a workqueue, but not the current atomic context. Thanks for the explanation.

You are welcome!

--
Uladzislau Rezki

author maintainer
#34 lore raw
In reply to Uladzislau Rezki

Hello, Alexander!

I am working on making vmalloc to support extra non-blocking flags. Currently i see one more place that i need to address:

kmsan_vmap_pages_range_noflush() function which uses hard-coded GFP_KERNEL flags for allocation of two arrays for its internal use only.

I have a question to you, can we just get rid of those two allocations? It is the easiest way, if possible. Otherwise i can add "gfp_t gfp_mask" extra parameter and pass there a corresponding gfp_mask flag. See below:

<snip>

include/linux/kmsan.h +4 -2
index 2b1432cc16d5..e4b34e7a3b11 100644
@@ -133,6 +133,7 @@ void kmsan_kfree_large(const void *ptr);
  * @prot:      page protection flags used for vmap.
  * @pages:     array of pages.
  * @page_shift:        page_shift passed to vmap_range_noflush().
+ * @gfp_mask:  gfp_mask to use internally.
  *
  * KMSAN maps shadow and origin pages of @pages into contiguous ranges in
  * vmalloc metadata address range. Returns 0 on success, callers must check
@@ -142,7 +143,8 @@ int __must_check kmsan_vmap_pages_range_noflush(unsigned long start,
                                                unsigned long end,
                                                pgprot_t prot,
                                                struct page **pages,
-                                               unsigned int page_shift);
+                                               unsigned int page_shift,
+                                               gfp_t gfp_mask);
 
 /**
  * kmsan_vunmap_kernel_range_noflush() - Notify KMSAN about a vunmap.
@@ -348,7 +350,7 @@ static inline void kmsan_kfree_large(const void *ptr)
 
 static inline int __must_check kmsan_vmap_pages_range_noflush(
        unsigned long start, unsigned long end, pgprot_t prot,
-       struct page **pages, unsigned int page_shift)
+       struct page **pages, unsigned int page_shift, gfp_t gfp_mask)
 {
        return 0;
 }
mm/internal.h +2 -2
index 45b725c3dc03..6a13b8ee1e6c 100644
@@ -1359,7 +1359,7 @@ size_t splice_folio_into_pipe(struct pipe_inode_info *pipe,
 #ifdef CONFIG_MMU
 void __init vmalloc_init(void);
 int __must_check vmap_pages_range_noflush(unsigned long addr, unsigned long end,
-                pgprot_t prot, struct page **pages, unsigned int page_shift);
+               pgprot_t prot, struct page **pages, unsigned int page_shift, gfp_t gfp_mask);
 unsigned int get_vm_area_page_order(struct vm_struct *vm);
 #else
 static inline void vmalloc_init(void)
@@ -1368,7 +1368,7 @@ static inline void vmalloc_init(void)
 
 static inline
 int __must_check vmap_pages_range_noflush(unsigned long addr, unsigned long end,
-                pgprot_t prot, struct page **pages, unsigned int page_shift)
+               pgprot_t prot, struct page **pages, unsigned int page_shift, gfp_t gfp_mask)
 {
        return -EINVAL;
 }
mm/kmsan/init.c +2 -1
index b14ce3417e65..5b74d6dbf0b8 100644
@@ -233,5 +233,6 @@ void __init kmsan_init_runtime(void)
        kmsan_memblock_discard();
        pr_info("Starting KernelMemorySanitizer\n");
        pr_info("ATTENTION: KMSAN is a debugging tool! Do not use it on production machines!\n");
-       kmsan_enabled = true;
+       /* kmsan_enabled = true; */
+       kmsan_enabled = false;
 }
mm/kmsan/shadow.c +3 -3
index 54f3c3c962f0..3cd733663100 100644
@@ -215,7 +215,7 @@ void kmsan_free_page(struct page *page, unsigned int order)
 
 int kmsan_vmap_pages_range_noflush(unsigned long start, unsigned long end,
                                   pgprot_t prot, struct page **pages,
-                                  unsigned int page_shift)
+                                  unsigned int page_shift, gfp_t gfp_mask)
 {
        unsigned long shadow_start, origin_start, shadow_end, origin_end;
        struct page **s_pages, **o_pages;
@@ -230,8 +230,8 @@ int kmsan_vmap_pages_range_noflush(unsigned long start, unsigned long end,
                return 0;
 
        nr = (end - start) / PAGE_SIZE;
-       s_pages = kcalloc(nr, sizeof(*s_pages), GFP_KERNEL);
-       o_pages = kcalloc(nr, sizeof(*o_pages), GFP_KERNEL);
+       s_pages = kcalloc(nr, sizeof(*s_pages), gfp_mask);
+       o_pages = kcalloc(nr, sizeof(*o_pages), gfp_mask);
        if (!s_pages || !o_pages) {
                err = -ENOMEM;
                goto ret;
mm/percpu-vm.c +1 -1
index cd69caf6aa8d..4f5937090590 100644
@@ -194,7 +194,7 @@ static int __pcpu_map_pages(unsigned long addr, struct page **pages,
                            int nr_pages)
 {
        return vmap_pages_range_noflush(addr, addr + (nr_pages << PAGE_SHIFT),
-                                       PAGE_KERNEL, pages, PAGE_SHIFT);
+                       PAGE_KERNEL, pages, PAGE_SHIFT, GFP_KERNEL);
 }
 
 /**
mm/vmalloc.c +17 -9
index ee197f5b8cf0..9be01dcca690 100644
@@ -671,16 +671,28 @@ int __vmap_pages_range_noflush(unsigned long addr, unsigned long end,
 }
 
 int vmap_pages_range_noflush(unsigned long addr, unsigned long end,
-               pgprot_t prot, struct page **pages, unsigned int page_shift)
+               pgprot_t prot, struct page **pages, unsigned int page_shift,
+               gfp_t gfp_mask)
 {
        int ret = kmsan_vmap_pages_range_noflush(addr, end, prot, pages,
-                                                page_shift);
+                                               page_shift, gfp_mask);
 
        if (ret)
                return ret;
        return __vmap_pages_range_noflush(addr, end, prot, pages, page_shift);
 }
 
+static int __vmap_pages_range(unsigned long addr, unsigned long end,
+               pgprot_t prot, struct page **pages, unsigned int page_shift,
+               gfp_t gfp_mask)
+{
+       int err;
+
+       err = vmap_pages_range_noflush(addr, end, prot, pages, page_shift, gfp_mask);
+       flush_cache_vmap(addr, end);
+       return err;
+}
+
 /**
  * vmap_pages_range - map pages to a kernel virtual address
  * @addr: start of the VM area to map
@@ -696,11 +708,7 @@ int vmap_pages_range_noflush(unsigned long addr, unsigned long end,
 int vmap_pages_range(unsigned long addr, unsigned long end,
                pgprot_t prot, struct page **pages, unsigned int page_shift)
 {
-       int err;
-
-       err = vmap_pages_range_noflush(addr, end, prot, pages, page_shift);
-       flush_cache_vmap(addr, end);
-       return err;
+       return __vmap_pages_range(addr, end, prot, pages, page_shift, GFP_KERNEL);
 }
 
 static int check_sparse_vm_area(struct vm_struct *area, unsigned long start,
@@ -3804,8 +3812,8 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
                flags = memalloc_noio_save();
 
        do {
-               ret = vmap_pages_range(addr, addr + size, prot, area->pages,
-                       page_shift);
+               ret = __vmap_pages_range(addr, addr + size, prot, area->pages,
+                               page_shift, gfp_mask);
                if (nofail && (ret < 0))
                        schedule_timeout_uninterruptible(1);
        } while (nofail && (ret < 0));

<snip>

Thanks!

--
Uladzislau Rezki