Re: [RFC PATCH v2] mm/vmalloc: fix incorrect __vmap_pages_range_noflush() if vm_area_alloc_pages() from high order fallback to order0

From: Baoquan He
Date: Thu Jul 25 2024 - 22:31:28 EST


On 07/26/24 at 12:40am, Hailong Liu wrote:
> On Thu, 25. Jul 19:39, Baoquan He wrote:
> > On 07/25/24 at 11:53am, hailong.liu@xxxxxxxx wrote:
> > > From: "Hailong.Liu" <hailong.liu@xxxxxxxx>
> > >
> > > The scenario where the issue occurs is as follows:
> > > CONFIG: vmap_allow_huge = true && 2M is for PMD_SIZE
> > > kvmalloc(2M, __GFP_NOFAIL|GFP_XXX)
> > > __vmalloc_node_range(vm_flags=VM_ALLOW_HUGE_VMAP)
> > > vm_area_alloc_pages(order=9) --->allocs order9 failed and fallback to order0
> > > and phys_addr is aligned with PMD_SIZE
> > > vmap_pages_range
> > > vmap_pages_range_noflush
> > > __vmap_pages_range_noflush(page_shift = 21) ----> incorrect vmap *huge* here
> > >
> > > In fact, as long as page_shift is not equal to PAGE_SHIFT, there
> > > might be issues with the __vmap_pages_range_noflush().
> > >
> > > The patch also remove VM_ALLOW_HUGE_VMAP in kvmalloc_node(), There
> > > are several reasons for this:
> > > - This increases memory footprint because ALIGNMENT.
> > > - This increases the likelihood of kvmalloc allocation failures.
> > > - Without this it fixes the origin issue of kvmalloc with __GFP_NOFAIL may return NULL.
> > > Besides if drivers want to vmap huge, user vmalloc_huge instead.
> >
> > Seem there are two issues you are folding into one patch:
> Got it. I will separate in the next version.
>
> >
> > one is the wrong informatin passed into __vmap_pages_range_noflush();
> > the other is you want to take off VM_ALLOW_HUGE_VMAP on kvmalloc().
> >
> > About the 1st one, do you think below draft is OK to you?
> >
> > Pass out the fall back order and adjust the order and shift for later
> > usage, mainly for vmap_pages_range().
> >
> > diff --git a/mm/vmalloc.c b/mm/vmalloc.c
> > index 260897b21b11..5ee9ae518f3d 100644
> > --- a/mm/vmalloc.c
> > +++ b/mm/vmalloc.c
> > @@ -3508,9 +3508,9 @@ EXPORT_SYMBOL_GPL(vmap_pfn);
> >
> > static inline unsigned int
> > vm_area_alloc_pages(gfp_t gfp, int nid,
> > - unsigned int order, unsigned int nr_pages, struct page **pages)
> > + unsigned int *page_order, unsigned int nr_pages, struct page **pages)
> > {
> > - unsigned int nr_allocated = 0;
> > + unsigned int nr_allocated = 0, order = *page_order;
> > gfp_t alloc_gfp = gfp;
> > bool nofail = gfp & __GFP_NOFAIL;
> > struct page *page;
> > @@ -3611,6 +3611,7 @@ vm_area_alloc_pages(gfp_t gfp, int nid,
> > cond_resched();
> > nr_allocated += 1U << order;
> > }
> > + *page_order = order;
> >
> > return nr_allocated;
> > }
> > @@ -3654,7 +3655,7 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
> > page_order = vm_area_page_order(area);
> >
> > area->nr_pages = vm_area_alloc_pages(gfp_mask | __GFP_NOWARN,
> > - node, page_order, nr_small_pages, area->pages);
> > + node, &page_order, nr_small_pages, area->pages);
> >
> > atomic_long_add(area->nr_pages, &nr_vmalloc_pages);
> > if (gfp_mask & __GFP_ACCOUNT) {
> > @@ -3686,6 +3687,10 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
> > goto fail;
> > }
> >
> > +
> > + set_vm_area_page_order(area, page_order);
> > + page_shift = page_order + PAGE_SHIFT;
> > +
> > /*
> > * page tables allocations ignore external gfp mask, enforce it
> > * by the scope API
> >
> The logic of this patch is somewhat similar to my first one. If high order
> allocation fails, it will go normal mapping.
>
> However I also save the fallback position. The ones before this position are
> used for huge mapping, the ones >= position for normal mapping as Barry said.
> "support the combination of PMD and PTE mapping". this will take some
> times as it needs to address the corner cases and do some tests.

Hmm, we may not need to worry about the imperfect mapping. Currently
there are two places setting VM_ALLOW_HUGE_VMAP: __kvmalloc_node_noprof()
and vmalloc_huge().

For vmalloc_huge(), it's called in below three interfaces which are all
invoked during boot. Basically they can succeed to get required contiguous
physical memory. I guess that's why Tangquan only spot this issue on kvmalloc
invocation when the required size exceeds e.g 2M. For kvmalloc_node(),
we have told that in the code comment above __kvmalloc_node_noprof(),
it's a best effort behaviour.

mm/mm_init.c <<alloc_large_system_hash>>
table = vmalloc_huge(size, gfp_flags);
net/ipv4/inet_hashtables.c <<inet_pernet_hashinfo_alloc>>
new_hashinfo->ehash = vmalloc_huge(ehash_entries * sizeof(struct inet_ehash_bucket),
net/ipv4/udp.c <<udp_pernet_table_alloc>>
udptable->hash = vmalloc_huge(hash_entries * 2 * sizeof(struct udp_hslot)

Maybe we should add code comment or document to notice people that the
contiguous physical pages are not guaranteed for vmalloc_huge() if you
use it after boot.

>
> IMO, the draft can fix the current issue, it also does not have significant side
> effects. Barry, what do you think about this patch? If you think it's okay,
> I will split this patch into two: one to remove the VM_ALLOW_HUGE_VMAP and the
> other to address the current mapping issue.
>
> --
> help you, help me,
> Hailong.
>