From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B0967C433F5 for ; Sun, 29 May 2022 23:30:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E19DF8D0002; Sun, 29 May 2022 19:30:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DC6058D0001; Sun, 29 May 2022 19:30:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CB9238D0002; Sun, 29 May 2022 19:30:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id BBDBC8D0001 for ; Sun, 29 May 2022 19:30:34 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay12.hostedemail.com (Postfix) with ESMTP id 7E577120862 for ; Sun, 29 May 2022 23:30:34 +0000 (UTC) X-FDA: 79520377188.18.CB5F5B5 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf19.hostedemail.com (Postfix) with ESMTP id A38971A0047 for ; Sun, 29 May 2022 23:30:19 +0000 (UTC) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id EF104B80ABD; Sun, 29 May 2022 23:30:31 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 844B5C385B8; Sun, 29 May 2022 23:30:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1653867030; bh=GpzTPsNqRojtOnbCgreA4wsBMUClKlvqnaoCqS7Q2yQ=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=KQN+Ec1m1OMjJ+UgZwlfD9T6ILbxe9+b+NogjSKzaZo1W1GjKoD9jeHRuicyTluyc Y46PdY9URSoFNW57j4/XKpmaiBgrj1ciQxaSkul9U9dTYTKiq1R56QatdIs0SAEJHx OOvlfw0dtnht5dc3r6aVTVDcQB2SvqPjf1wmm0BM= Date: Sun, 29 May 2022 16:30:29 -0700 From: Andrew Morton To: Chen Lin Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Alexander Duyck , netdev@vger.kernel.org Subject: Re: [PATCH] mm: page_frag: Warn_on when frag_alloc size is bigger than PAGE_SIZE Message-Id: <20220529163029.12425c1e5286d7c7e3fe3708@linux-foundation.org> In-Reply-To: <1653752373-3172-1-git-send-email-chen45464546@163.com> References: <1653752373-3172-1-git-send-email-chen45464546@163.com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-redhat-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Rspam-User: X-Rspamd-Queue-Id: A38971A0047 X-Stat-Signature: qtddq9r39h6fcif7ax5mayx1bgg9fmqz Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b=KQN+Ec1m; spf=pass (imf19.hostedemail.com: domain of akpm@linux-foundation.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=akpm@linux-foundation.org; dmarc=none X-Rspamd-Server: rspam09 X-HE-Tag: 1653867019-642870 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Sat, 28 May 2022 23:39:33 +0800 Chen Lin 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. > Let's cc Alexander and the networking developers. > --- a/mm/page_alloc.c > +++ b/mm/page_alloc.c > @@ -5574,6 +5574,11 @@ 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 > + * is bigger than PAGE_SIZE, use kmalloc or alloc_pages instead. > + */ > + WARN_ON(fragsz > PAGE_SIZE); > + > if (unlikely(!nc->va)) { > refill: > page = __page_frag_cache_refill(nc, gfp_mask); Odd. All this does is generate a warning. If the kernel is corrupting memory, that's a bug which needs fixing?