[RFC 2/7] mm/vmalloc: Support non-blocking GFP flags in alloc_vmap_area()

Uladzislau Rezki (Sony) posted 7 patches 3 months ago
[RFC 2/7] mm/vmalloc: Support non-blocking GFP flags in alloc_vmap_area()
Posted by Uladzislau Rezki (Sony) 3 months ago
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.

Signed-off-by: Uladzislau Rezki (Sony) <urezki@gmail.com>
---
 mm/vmalloc.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index ab986dd09b6a..8c375b8e269d 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -2009,6 +2009,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;
 
@@ -2018,7 +2019,9 @@ 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);
+	if (allow_block)
+		might_sleep();
 
 	/*
 	 * If a VA is obtained from a global heap(if it fails here)
@@ -2030,7 +2033,8 @@ static struct vmap_area *alloc_vmap_area(unsigned long size,
 	 */
 	va = node_alloc(size, align, vstart, vend, &addr, &vn_id);
 	if (!va) {
-		gfp_mask = gfp_mask & GFP_RECLAIM_MASK;
+		if (allow_block)
+			gfp_mask = gfp_mask & GFP_RECLAIM_MASK;
 
 		va = kmem_cache_alloc_node(vmap_area_cachep, gfp_mask, node);
 		if (unlikely(!va))
@@ -2057,8 +2061,14 @@ 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))
+	if (IS_ERR_VALUE(addr)) {
+		if (!allow_block) {
+			kmem_cache_free(vmap_area_cachep, va);
+			return ERR_PTR(-ENOMEM);
+		}
+
 		goto overflow;
+	}
 
 	va->va_start = addr;
 	va->va_end = addr + size;
-- 
2.39.5
Re: [RFC 2/7] mm/vmalloc: Support non-blocking GFP flags in alloc_vmap_area()
Posted by Michal Hocko 3 months ago
On Fri 04-07-25 17:25:32, Uladzislau Rezki wrote:
[...]
> @@ -2030,7 +2033,8 @@ static struct vmap_area *alloc_vmap_area(unsigned long size,
>  	 */
>  	va = node_alloc(size, align, vstart, vend, &addr, &vn_id);
>  	if (!va) {
> -		gfp_mask = gfp_mask & GFP_RECLAIM_MASK;
> +		if (allow_block)
> +			gfp_mask = gfp_mask & GFP_RECLAIM_MASK;

I don't follow here and is this even correct?

>  
>  		va = kmem_cache_alloc_node(vmap_area_cachep, gfp_mask, node);
>  		if (unlikely(!va))
> @@ -2057,8 +2061,14 @@ 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))
> +	if (IS_ERR_VALUE(addr)) {
> +		if (!allow_block) {
> +			kmem_cache_free(vmap_area_cachep, va);
> +			return ERR_PTR(-ENOMEM);

I would suggest to add a comment for this. Something like

for blockable requests trigger the overflow paths because that
relies on vmap_purge_lock mutex and blocking notifiers.

> +		}
> +
>  		goto overflow;
> +	}
>  
>  	va->va_start = addr;
>  	va->va_end = addr + size;
> -- 
> 2.39.5

-- 
Michal Hocko
SUSE Labs
Re: [RFC 2/7] mm/vmalloc: Support non-blocking GFP flags in alloc_vmap_area()
Posted by Uladzislau Rezki 3 months ago
On Mon, Jul 07, 2025 at 09:11:35AM +0200, Michal Hocko wrote:
> On Fri 04-07-25 17:25:32, Uladzislau Rezki wrote:
> [...]
> > @@ -2030,7 +2033,8 @@ static struct vmap_area *alloc_vmap_area(unsigned long size,
> >  	 */
> >  	va = node_alloc(size, align, vstart, vend, &addr, &vn_id);
> >  	if (!va) {
> > -		gfp_mask = gfp_mask & GFP_RECLAIM_MASK;
> > +		if (allow_block)
> > +			gfp_mask = gfp_mask & GFP_RECLAIM_MASK;
> 
> I don't follow here and is this even correct?
> 
Allow nested flags to follow a user request if there is a request
to not block. For example if we apply GFP_RECLAIM_MASK to GFP_ATOMIC
GFP_ATOMIC is converted to zero, thus to GFP_NOWAIT.

> >  
> >  		va = kmem_cache_alloc_node(vmap_area_cachep, gfp_mask, node);
> >  		if (unlikely(!va))
> > @@ -2057,8 +2061,14 @@ 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))
> > +	if (IS_ERR_VALUE(addr)) {
> > +		if (!allow_block) {
> > +			kmem_cache_free(vmap_area_cachep, va);
> > +			return ERR_PTR(-ENOMEM);
> 
> I would suggest to add a comment for this. Something like
> 
> for blockable requests trigger the overflow paths because that
> relies on vmap_purge_lock mutex and blocking notifiers.
> 
Thanks, i can do it easily. Also, this is an RFC i think it should
be split and improved. Maybe to move out some functionality into a
separate function.

--
Uladzislau Rezki
Re: [RFC 2/7] mm/vmalloc: Support non-blocking GFP flags in alloc_vmap_area()
Posted by Michal Hocko 3 months ago
On Tue 08-07-25 14:34:28, Uladzislau Rezki wrote:
> On Mon, Jul 07, 2025 at 09:11:35AM +0200, Michal Hocko wrote:
> > On Fri 04-07-25 17:25:32, Uladzislau Rezki wrote:
> > [...]
> > > @@ -2030,7 +2033,8 @@ static struct vmap_area *alloc_vmap_area(unsigned long size,
> > >  	 */
> > >  	va = node_alloc(size, align, vstart, vend, &addr, &vn_id);
> > >  	if (!va) {
> > > -		gfp_mask = gfp_mask & GFP_RECLAIM_MASK;
> > > +		if (allow_block)
> > > +			gfp_mask = gfp_mask & GFP_RECLAIM_MASK;
> > 
> > I don't follow here and is this even correct?
> > 
> Allow nested flags to follow a user request if there is a request
> to not block. For example if we apply GFP_RECLAIM_MASK to GFP_ATOMIC
> GFP_ATOMIC is converted to zero, thus to GFP_NOWAIT.

I still do not follow. The aim of this code is to filter out all
non-reclaim related flags. Why that should work differently for
non-waiting allocations?
Btw. if you had GPP_ATOMIC the resulting mask will be still GFP_ATOMIC
as both __GFP_HIGH|__GFP_KSWAPD_RECLAIM are part of GFP_RECLAIM_MASK.

-- 
Michal Hocko
SUSE Labs
Re: [RFC 2/7] mm/vmalloc: Support non-blocking GFP flags in alloc_vmap_area()
Posted by Uladzislau Rezki 3 months ago
On Tue, Jul 08, 2025 at 05:17:33PM +0200, Michal Hocko wrote:
> On Tue 08-07-25 14:34:28, Uladzislau Rezki wrote:
> > On Mon, Jul 07, 2025 at 09:11:35AM +0200, Michal Hocko wrote:
> > > On Fri 04-07-25 17:25:32, Uladzislau Rezki wrote:
> > > [...]
> > > > @@ -2030,7 +2033,8 @@ static struct vmap_area *alloc_vmap_area(unsigned long size,
> > > >  	 */
> > > >  	va = node_alloc(size, align, vstart, vend, &addr, &vn_id);
> > > >  	if (!va) {
> > > > -		gfp_mask = gfp_mask & GFP_RECLAIM_MASK;
> > > > +		if (allow_block)
> > > > +			gfp_mask = gfp_mask & GFP_RECLAIM_MASK;
> > > 
> > > I don't follow here and is this even correct?
> > > 
> > Allow nested flags to follow a user request if there is a request
> > to not block. For example if we apply GFP_RECLAIM_MASK to GFP_ATOMIC
> > GFP_ATOMIC is converted to zero, thus to GFP_NOWAIT.
> 
> I still do not follow. The aim of this code is to filter out all
> non-reclaim related flags. Why that should work differently for
> non-waiting allocations?
> Btw. if you had GPP_ATOMIC the resulting mask will be still GFP_ATOMIC
> as both __GFP_HIGH|__GFP_KSWAPD_RECLAIM are part of GFP_RECLAIM_MASK.
> 
Right. I misread the GFP_RECLAIM_MASK, i thought that GFP_ATOMIC and
GFP_NOWAIT are not part of it. They allow reclaim, but not direct,
i.e. it is OK to wake-up a kswapd.

So, they should not work differently. Thank you for the comment! 

--
Uladzislau Rezki