[PATCH v2 09/10] mm: Skip might_alloc() warnings when PF_MEMALLOC is set

Uladzislau Rezki (Sony) posted 10 patches 2 weeks, 3 days ago
[PATCH v2 09/10] mm: Skip might_alloc() warnings when PF_MEMALLOC is set
Posted by Uladzislau Rezki (Sony) 2 weeks, 3 days ago
might_alloc() catches invalid blocking allocations in contexts
where sleeping is not allowed.

However when PF_MEMALLOC is set, the page allocator already skips
reclaim and other blocking paths. In such cases, a blocking gfp_mask
does not actually lead to blocking, so triggering might_alloc() splats
is misleading.

Adjust might_alloc() to skip warnings when the current task has
PF_MEMALLOC set, matching the allocator's actual blocking behaviour.

Signed-off-by: Uladzislau Rezki (Sony) <urezki@gmail.com>
---
 include/linux/sched/mm.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/include/linux/sched/mm.h b/include/linux/sched/mm.h
index 2201da0afecc..dc2d3cab32ef 100644
--- a/include/linux/sched/mm.h
+++ b/include/linux/sched/mm.h
@@ -318,7 +318,8 @@ static inline void might_alloc(gfp_t gfp_mask)
 	fs_reclaim_acquire(gfp_mask);
 	fs_reclaim_release(gfp_mask);
 
-	might_sleep_if(gfpflags_allow_blocking(gfp_mask));
+	might_sleep_if(gfpflags_allow_blocking(gfp_mask) &&
+		!(current->flags & PF_MEMALLOC));
 }
 
 /**
-- 
2.47.3
Re: [PATCH v2 09/10] mm: Skip might_alloc() warnings when PF_MEMALLOC is set
Posted by Michal Hocko 2 weeks, 2 days ago
On Mon 15-09-25 15:40:39, Uladzislau Rezki wrote:
> might_alloc() catches invalid blocking allocations in contexts
> where sleeping is not allowed.
> 
> However when PF_MEMALLOC is set, the page allocator already skips
> reclaim and other blocking paths. In such cases, a blocking gfp_mask
> does not actually lead to blocking, so triggering might_alloc() splats
> is misleading.
> 
> Adjust might_alloc() to skip warnings when the current task has
> PF_MEMALLOC set, matching the allocator's actual blocking behaviour.
> 
> Signed-off-by: Uladzislau Rezki (Sony) <urezki@gmail.com>

I would probably just bail out early for PF_MEMALLOC to not meddle with
might_sleep_if condition as it seems to read better but I do not insist.
Acked-by: Michal Hocko <mhocko@suse.com>
Thanks

> ---
>  include/linux/sched/mm.h | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/include/linux/sched/mm.h b/include/linux/sched/mm.h
> index 2201da0afecc..dc2d3cab32ef 100644
> --- a/include/linux/sched/mm.h
> +++ b/include/linux/sched/mm.h
> @@ -318,7 +318,8 @@ static inline void might_alloc(gfp_t gfp_mask)
>  	fs_reclaim_acquire(gfp_mask);
>  	fs_reclaim_release(gfp_mask);
>  
> -	might_sleep_if(gfpflags_allow_blocking(gfp_mask));
> +	might_sleep_if(gfpflags_allow_blocking(gfp_mask) &&
> +		!(current->flags & PF_MEMALLOC));
>  }
>  
>  /**
> -- 
> 2.47.3

-- 
Michal Hocko
SUSE Labs
Re: [PATCH v2 09/10] mm: Skip might_alloc() warnings when PF_MEMALLOC is set
Posted by Uladzislau Rezki 2 weeks, 1 day ago
On Mon, Sep 15, 2025 at 07:16:50PM +0200, Michal Hocko wrote:
> On Mon 15-09-25 15:40:39, Uladzislau Rezki wrote:
> > might_alloc() catches invalid blocking allocations in contexts
> > where sleeping is not allowed.
> > 
> > However when PF_MEMALLOC is set, the page allocator already skips
> > reclaim and other blocking paths. In such cases, a blocking gfp_mask
> > does not actually lead to blocking, so triggering might_alloc() splats
> > is misleading.
> > 
> > Adjust might_alloc() to skip warnings when the current task has
> > PF_MEMALLOC set, matching the allocator's actual blocking behaviour.
> > 
> > Signed-off-by: Uladzislau Rezki (Sony) <urezki@gmail.com>
> 
> I would probably just bail out early for PF_MEMALLOC to not meddle with
> might_sleep_if condition as it seems to read better but I do not insist.
> Acked-by: Michal Hocko <mhocko@suse.com>
>
Thank you, i will apply it and place the check into separate "if"
condition.

--
Uladzislau Rezki