From: Barry Song <21cnbao@gmail.com>
To: hailong.liu@oppo.com
Cc: Andrew Morton <akpm@linux-foundation.org>,
Uladzislau Rezki <urezki@gmail.com>,
Christoph Hellwig <hch@infradead.org>,
Lorenzo Stoakes <lstoakes@gmail.com>,
Vlastimil Babka <vbabka@suse.cz>, Michal Hocko <mhocko@suse.com>,
"Tangquan . Zheng" <zhengtangquan@oppo.com>,
linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: Re: [RFC PATCH v1] mm/vmalloc: fix incorrect __vmap_pages_range_noflush() if vm_area_alloc_pages() from high order fallback to order0
Date: Thu, 25 Jul 2024 10:23:44 +1200 [thread overview]
Message-ID: <CAGsJ_4xNuDroYRydFj6MFey2q6WG=bCp-d8fBOXb47srYZJfRg@mail.gmail.com> (raw)
In-Reply-To: <20240724181916.31776-1-hailong.liu@oppo.com>
On Thu, Jul 25, 2024 at 6:19 AM <hailong.liu@oppo.com> wrote:
>
> From: "Hailong.Liu" <hailong.liu@oppo.com>
>
> The scenario where the issue occurs is as follows:
> CONFIG: vmap_allow_huge = true && 2M is for PMD_SIZE
> kvmalloc(2M)
> __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
>
> Fix it by introducing VM_AREA_ALLOC_PAGES_FALLBACK in page->private if fallback to 0.
> Fixes: e9c3cda4d86e ("mm, vmalloc: fix high order __GFP_NOFAIL allocations")
>
> CC: Barry Song <21cnbao@gmail.com>
> Reported-by: Tangquan.Zheng <zhengtangquan@oppo.com>
> Signed-off-by: Hailong.Liu <hailong.liu@oppo.com>
> ---
> mm/vmalloc.c | 14 ++++++++++++--
> 1 file changed, 12 insertions(+), 2 deletions(-)
>
> diff --git a/mm/vmalloc.c b/mm/vmalloc.c
> index 03c78fae06f3..b35dfd3eeee3 100644
> --- a/mm/vmalloc.c
> +++ b/mm/vmalloc.c
> @@ -75,6 +75,8 @@ early_param("nohugevmalloc", set_nohugevmalloc);
> static const bool vmap_allow_huge = false;
> #endif /* CONFIG_HAVE_ARCH_HUGE_VMALLOC */
>
> +#define VM_AREA_ALLOC_PAGES_FALLBACK 0x1
> +
> bool is_vmalloc_addr(const void *x)
> {
> unsigned long addr = (unsigned long)kasan_reset_tag(x);
> @@ -604,8 +606,13 @@ int __vmap_pages_range_noflush(unsigned long addr, unsigned long end,
> WARN_ON(page_shift < PAGE_SHIFT);
>
> if (!IS_ENABLED(CONFIG_HAVE_ARCH_HUGE_VMALLOC) ||
> - page_shift == PAGE_SHIFT)
> - return vmap_small_pages_range_noflush(addr, end, prot, pages);
> + page_shift == PAGE_SHIFT ||
> + page_private(pages[0]) == VM_AREA_ALLOC_PAGES_FALLBACK) {
> + int ret = vmap_small_pages_range_noflush(addr, end, prot, pages);
> +
> + set_page_private(pages[0], 0);
> + return ret;
> + }
we could have more than one *serious* bug here? do we also need the below
if ((end - start) % PMD_SIZE) != 0) ? no ?
int __vmap_pages_range_noflush(unsigned long addr, unsigned long end,
pgprot_t prot, struct page **pages, unsigned int page_shift)
{
unsigned int i, nr = (end - addr) >> PAGE_SHIFT;
WARN_ON(page_shift < PAGE_SHIFT);
if (!IS_ENABLED(CONFIG_HAVE_ARCH_HUGE_VMALLOC) ||
page_shift == PAGE_SHIFT)
return vmap_small_pages_range_noflush(addr, end, prot, pages);
for (i = 0; i < nr; i += 1U << (page_shift - PAGE_SHIFT)) {
int err;
err = vmap_range_noflush(addr, addr + (1UL << page_shift),
page_to_phys(pages[i]), prot,
page_shift);
if (err)
return err;
addr += 1UL << page_shift;
}
+ if (addr < end)
+ return vmap_small_pages_range_noflush(addr, end,
prot, pages + i);
return 0;
}
>
> for (i = 0; i < nr; i += 1U << (page_shift - PAGE_SHIFT)) {
> int err;
> @@ -3583,6 +3590,7 @@ vm_area_alloc_pages(gfp_t gfp, int nid,
>
> /* fall back to the zero order allocations */
> alloc_gfp |= __GFP_NOFAIL;
> + fallback = true;
> order = 0;
> continue;
> }
> @@ -3608,6 +3616,8 @@ vm_area_alloc_pages(gfp_t gfp, int nid,
> cond_resched();
> nr_allocated += 1U << order;
> }
> + if (nr_allocated && fallback)
> + set_page_private(pages[0], VM_AREA_ALLOC_PAGES_FALLBACK);
>
> return nr_allocated;
> }
> --
> 2.34.1
>
prev parent reply other threads:[~2024-07-24 22:23 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-24 18:19 hailong.liu
2024-07-24 18:28 ` Hailong.Liu
2024-07-24 20:02 ` Matthew Wilcox
2024-07-24 22:11 ` Barry Song
2024-07-25 6:15 ` Hailong.Liu
2024-07-24 22:23 ` Barry Song [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAGsJ_4xNuDroYRydFj6MFey2q6WG=bCp-d8fBOXb47srYZJfRg@mail.gmail.com' \
--to=21cnbao@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=hailong.liu@oppo.com \
--cc=hch@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lstoakes@gmail.com \
--cc=mhocko@suse.com \
--cc=urezki@gmail.com \
--cc=vbabka@suse.cz \
--cc=zhengtangquan@oppo.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox