[RFC 3/7] mm/vmalloc: Avoid cond_resched() when blocking is not permitted

Uladzislau Rezki (Sony) posted 7 patches 5 months, 2 weeks ago
[RFC 3/7] mm/vmalloc: Avoid cond_resched() when blocking is not permitted
Posted by Uladzislau Rezki (Sony) 5 months, 2 weeks ago
The vm_area_alloc_pages() function uses cond_resched() to yield the
CPU during potentially long-running loops. However, yielding should
only be done if the given GFP flags allow blocking.

This patch avoids calling cond_resched() when the allocation context
is non-blocking(GFP_ATOMIC, GFP_NOWAIT).

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

diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index 8c375b8e269d..25d09f753239 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -3624,7 +3624,9 @@ vm_area_alloc_pages(gfp_t gfp, int nid,
 							pages + nr_allocated);
 
 			nr_allocated += nr;
-			cond_resched();
+
+			if (gfpflags_allow_blocking(gfp))
+				cond_resched();
 
 			/*
 			 * If zero or pages were obtained partly,
@@ -3666,7 +3668,9 @@ vm_area_alloc_pages(gfp_t gfp, int nid,
 		for (i = 0; i < (1U << order); i++)
 			pages[nr_allocated + i] = page + i;
 
-		cond_resched();
+		if (gfpflags_allow_blocking(gfp))
+			cond_resched();
+
 		nr_allocated += 1U << order;
 	}
 
-- 
2.39.5
Re: [RFC 3/7] mm/vmalloc: Avoid cond_resched() when blocking is not permitted
Posted by Michal Hocko 5 months, 2 weeks ago
On Fri 04-07-25 17:25:33, Uladzislau Rezki wrote:
> The vm_area_alloc_pages() function uses cond_resched() to yield the
> CPU during potentially long-running loops. However, yielding should
> only be done if the given GFP flags allow blocking.
> 
> This patch avoids calling cond_resched() when the allocation context
> is non-blocking(GFP_ATOMIC, GFP_NOWAIT).

Do we even need those cond_resched calls? Both of them are called
shortly after memory allocator which already yields CPU when allowed.
> 
> Signed-off-by: Uladzislau Rezki (Sony) <urezki@gmail.com>
> ---
>  mm/vmalloc.c | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
> 
> diff --git a/mm/vmalloc.c b/mm/vmalloc.c
> index 8c375b8e269d..25d09f753239 100644
> --- a/mm/vmalloc.c
> +++ b/mm/vmalloc.c
> @@ -3624,7 +3624,9 @@ vm_area_alloc_pages(gfp_t gfp, int nid,
>  							pages + nr_allocated);
>  
>  			nr_allocated += nr;
> -			cond_resched();
> +
> +			if (gfpflags_allow_blocking(gfp))
> +				cond_resched();
>  
>  			/*
>  			 * If zero or pages were obtained partly,
> @@ -3666,7 +3668,9 @@ vm_area_alloc_pages(gfp_t gfp, int nid,
>  		for (i = 0; i < (1U << order); i++)
>  			pages[nr_allocated + i] = page + i;
>  
> -		cond_resched();
> +		if (gfpflags_allow_blocking(gfp))
> +			cond_resched();
> +
>  		nr_allocated += 1U << order;
>  	}
>  
> -- 
> 2.39.5

-- 
Michal Hocko
SUSE Labs
Re: [RFC 3/7] mm/vmalloc: Avoid cond_resched() when blocking is not permitted
Posted by Uladzislau Rezki 5 months, 2 weeks ago
On Mon, Jul 07, 2025 at 09:11:43AM +0200, Michal Hocko wrote:
> On Fri 04-07-25 17:25:33, Uladzislau Rezki wrote:
> > The vm_area_alloc_pages() function uses cond_resched() to yield the
> > CPU during potentially long-running loops. However, yielding should
> > only be done if the given GFP flags allow blocking.
> > 
> > This patch avoids calling cond_resched() when the allocation context
> > is non-blocking(GFP_ATOMIC, GFP_NOWAIT).
> 
> Do we even need those cond_resched calls? Both of them are called
> shortly after memory allocator which already yields CPU when allowed.
>
I think it can be just dropped.

--
Uladzislau Rezki