Re: [RFC PATCH] mm/vmalloc: fix vmalloc which may return null if called with __GFP_NOFAIL

Barry Song
2024-05-09T08:57:17Z

On Thu, May 9, 2024 at 8:32 PM Barry Song <21cnbao@gmail.com> wrote:
>
> On Thu, May 9, 2024 at 8:21 PM Hailong Liu <hailong.liu@oppo.com> wrote:
> >
> > On Thu, 09. May 09:48, Michal Hocko wrote:
> > > On Wed 08-05-24 20:58:08, hailong.liu@oppo.com wrote:
> > > > From: "Hailong.Liu" <hailong.liu@oppo.com>
> > > >
> > > > Commit a421ef303008 ("mm: allow !GFP_KERNEL allocations for kvmalloc")
> > > > includes support for __GFP_NOFAIL, but it presents a conflict with
> > > > commit dd544141b9eb ("vmalloc: back off when the current task is
> > > > OOM-killed"). A possible scenario is as belows:
> > > >
> > > > process-a
> > > > kvcalloc(n, m, GFP_KERNEL | __GFP_NOFAIL)
> > > > __vmalloc_node_range()
> > > > __vmalloc_area_node()
> > > > vm_area_alloc_pages()
> > > > --> oom-killer send SIGKILL to process-a
> > > > if (fatal_signal_pending(current)) break;
> > > > --> return NULL;
> > > >
> > > > to fix this, do not check fatal_signal_pending() in vm_area_alloc_pages()
> > > > if __GFP_NOFAIL set.
> > > >
> > > > Reported-by: Oven <liyangouwen1@oppo.com>
> > > > Signed-off-by: Hailong.Liu <hailong.liu@oppo.com>
> > > > ---
> > > > mm/vmalloc.c | 2 +-
> > > > 1 file changed, 1 insertion(+), 1 deletion(-)
> > > >
> > > > diff --git a/mm/vmalloc.c b/mm/vmalloc.c
> > > > index 6641be0ca80b..2f359d08bf8d 100644
> > > > --- a/mm/vmalloc.c
> > > > +++ b/mm/vmalloc.c
> > > > @@ -3560,7 +3560,7 @@ vm_area_alloc_pages(gfp_t gfp, int nid,
> > > >
> > > > /* High-order pages or fallback path if "bulk" fails. */
> > > > while (nr_allocated < nr_pages) {
> > > > - if (fatal_signal_pending(current))
> > > > + if (!(gfp & __GFP_NOFAIL) && fatal_signal_pending(current))
> > >
> > > Use nofail instead of gfp & __GFP_NOFAIL.
> > >
> > > Other than that looks good to me. After that is fixed, please feel free
> > > to add Acked-by: Michal Hocko <mhocko@suse.com>
> > >
> > > I believe this should also have Fixes: 9376130c390a ("mm/vmalloc: add support for __GFP_NOFAIL")
> > > --
> > > Michal Hocko
> > > SUSE Labs
> >
> > Thanks for the review and the Ack!
> >
> > Add Fixes in V2 patch.
> >
> > IIUC, nofail could not used for this case.
> >
> > /*
> > * For order-0 pages we make use of bulk allocator, if
> > * the page array is partly or not at all populated due
> > * to fails, fallback to a single page allocator that is
> > * more permissive.
> > */
> > if (!order) {
> > /* bulk allocator doesn't support nofail req. officially */
> > xxx
> > -> nofail = false;
>
> isn't it another bug that needs a fix?

Upon further examination, it's not a bug, but we can still utilize 'nofail'.
The current code is very hard to read about gfp and "nofail" :-)

maybe:

diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index 6641be0ca80b..7c66fe16c2ad 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -3498,7 +3498,7 @@ vm_area_alloc_pages(gfp_t gfp, int nid,
{
unsigned int nr_allocated = 0;
gfp_t alloc_gfp = gfp;
- bool nofail = false;
+ bool nofail = !!(gfp & __GFP_NOFAIL);
struct page *page;
int i;

@@ -3555,7 +3555,6 @@ vm_area_alloc_pages(gfp_t gfp, int nid,
* and compaction etc.
*/
alloc_gfp &= ~__GFP_NOFAIL;
- nofail = true;
}

/* High-order pages or fallback path if "bulk" fails. */

>
> > } else if (gfp & __GFP_NOFAIL) {
> > /*
> > * Higher order nofail allocations are really expensive and
> > * potentially dangerous (pre-mature OOM, disruptive reclaim
> > * and compaction etc.
> > */
> > alloc_gfp &= ~__GFP_NOFAIL;
> > nofail = true;
> > }
> >
> > /* High-order pages or fallback path if "bulk" fails. */
> > while (nr_allocated < nr_pages) {
> >
> > -> nofail is false here if bulk allocator fails.
> > if (fatal_signal_pending(current))
> > break;
> >
> > --
> >
> > Best Regards,
> > Hailong.