From: Andrew Morton <akpm@linux-foundation.org>
To: Chen Lin <chen45464546@163.com>
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org,
alexander.h.duyck@linux.intel.com, netdev@vger.kernel.org
Subject: Re: [PATCH v2] mm: page_frag: Warn_on when frag_alloc size is bigger than PAGE_SIZE
Date: Mon, 30 May 2022 13:07:05 -0700 [thread overview]
Message-ID: <20220530130705.29c5fa4a5225265d3736bfa4@linux-foundation.org> (raw)
In-Reply-To: <1653917942-5982-1-git-send-email-chen45464546@163.com>
On Mon, 30 May 2022 21:39:02 +0800 Chen Lin <chen45464546@163.com> wrote:
> netdev_alloc_frag->page_frag_alloc may cause memory corruption in
> the following process:
>
> 1. A netdev_alloc_frag function call need alloc 200 Bytes to build a skb.
>
> 2. Insufficient memory to alloc PAGE_FRAG_CACHE_MAX_ORDER(32K) in
> __page_frag_cache_refill to fill frag cache, then one page(eg:4K)
> is allocated, now current frag cache is 4K, alloc is success,
> nc->pagecnt_bias--.
>
> 3. Then this 200 bytes skb in step 1 is freed, page->_refcount--.
>
> 4. Another netdev_alloc_frag function call need alloc 5k, page->_refcount
> is equal to nc->pagecnt_bias, reset page count bias and offset to
> start of new frag. page_frag_alloc will return the 4K memory for a
> 5K memory request.
>
> 5. The caller write on the extra 1k memory which is not actual allocated
> will cause memory corruption.
>
> page_frag_alloc is for fragmented allocation. We should warn the caller
> to avoid memory corruption.
>
> When fragsz is larger than one page, we report the failure and return.
> I don't think it is a good idea to make efforts to support the
> allocation of more than one page in this function because the total
> frag cache size(PAGE_FRAG_CACHE_MAX_SIZE 32768) is relatively small.
> When the request is larger than one page, the caller should switch to
> use other kernel interfaces, such as kmalloc and alloc_Pages.
>
> This bug is mainly caused by the reuse of the previously allocated
> frag cache memory by the following LARGER allocations. This bug existed
> before page_frag_alloc was ported from __netdev_alloc_frag in
> net/core/skbuff.c, so most Linux versions have this problem.
>
I won't attempt to address the large issues here (like, should
networking be changed to support this). But I can nitpick :)
> --- a/mm/page_alloc.c
> +++ b/mm/page_alloc.c
> @@ -5574,6 +5574,16 @@ void *page_frag_alloc_align(struct page_frag_cache *nc,
> struct page *page;
> int offset;
>
> + /* frag_alloc is not suitable for memory alloc which fragsz
Like this please:
/*
* frag_alloc...
> + * is bigger than PAGE_SIZE, use kmalloc or alloc_pages instead.
> + */
> + if (unlikely(fragsz > PAGE_SIZE)) {
> + WARN(1, "alloc fragsz(%d) > PAGE_SIZE(%ld) not supported,
> + alloc fail\n", fragsz, PAGE_SIZE);
It's neater to do
if (WARN(fragsz > PAGE_SIZE, "alloc fragsz(%d...", ...))
return NULL;
Also, you have a newline and a bunch of tabs in that string.
Also, please consider WARN_ONCE. We don't want to provide misbehaved
or malicious userspace with the ability to flood the logs with
warnings.
prev parent reply other threads:[~2022-05-30 20:07 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-05-28 15:39 [PATCH] " Chen Lin
2022-05-29 23:30 ` Andrew Morton
[not found] ` <1653917942-5982-1-git-send-email-chen45464546@163.com>
2022-05-30 19:27 ` [PATCH v2] " Jakub Kicinski
2022-05-30 19:29 ` Jakub Kicinski
2022-05-31 14:41 ` Chen Lin
2022-05-31 15:14 ` Jakub Kicinski
2022-05-31 15:36 ` Chen Lin
2022-05-31 15:47 ` Jakub Kicinski
2022-05-31 18:28 ` Alexander Duyck
2022-06-01 12:32 ` 愚树
2022-06-01 15:04 ` Alexander Duyck
2022-07-06 15:21 ` Maurizio Lombardi
2022-05-30 20:07 ` Andrew Morton [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=20220530130705.29c5fa4a5225265d3736bfa4@linux-foundation.org \
--to=akpm@linux-foundation.org \
--cc=alexander.h.duyck@linux.intel.com \
--cc=chen45464546@163.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=netdev@vger.kernel.org \
/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