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 70E03C433EF for ; Wed, 13 Jul 2022 15:45:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0285B940149; Wed, 13 Jul 2022 11:45:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EF43C940134; Wed, 13 Jul 2022 11:45:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D9424940149; Wed, 13 Jul 2022 11:45:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id C5AE5940134 for ; Wed, 13 Jul 2022 11:45:41 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 8EC573444B for ; Wed, 13 Jul 2022 15:45:41 +0000 (UTC) X-FDA: 79682501682.14.EA1EF54 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf08.hostedemail.com (Postfix) with ESMTP id 25CDA160062 for ; Wed, 13 Jul 2022 15:45:40 +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 2545BB81D9B; Wed, 13 Jul 2022 15:45:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 847BCC3411E; Wed, 13 Jul 2022 15:45:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1657727137; bh=GZo3SLj/oGl49Xss42bjHxtoHp0PqzsJ7wTiSiRsKu8=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=Qf191j2Qu9cUwxfj8D/zsyS2B4cUskeOsFriYkXt/W+kcMl4o9Mk5WdXS/GcPWXOq 5Q4lPI07mFjOLy7o9uJApchupsOxpFIxEisxbyRPjmmWX3odJE+mPQO599lI3f1Jl2 Rm/ZJrHp7XHl3HEpQSVc30Mxt8bictGaERSkKA64= Date: Wed, 13 Jul 2022 08:45:36 -0700 From: Andrew Morton To: Maurizio Lombardi Cc: alexander.duyck@gmail.com, kuba@kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, chen45464546@163.com Subject: Re: [PATCH V2] mm: prevent page_frag_alloc() from corrupting the memory Message-Id: <20220713084536.1af461b016a16c58baad7db2@linux-foundation.org> In-Reply-To: <20220713150143.147537-1-mlombard@redhat.com> References: <20220713150143.147537-1-mlombard@redhat.com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1657727141; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=tXf1/GwN5VRQHjpjwxB/v4i3CCyRId2B1xpC6Jr7gXM=; b=2yRBMOJ33v3HWXxwGFjF/v7fkEPUCPRfVl27H7ii3WNJrbbYFKC7AdEM4O7L/OVrN0TikM CvryInPjpH9Y1E8dOUOZ+ygORPM/G7UXjZsjDjO7owj7Pv6q9SrkQw4IqPMNT8/lmdI1Ps EA9uiXQB3UpR1goOhcmyYfDu0ocIKcE= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1657727141; a=rsa-sha256; cv=none; b=MEgKz7KFMlM97M1+rKxFYw0VJYWToJsGzkbXSLRaasRzhn+q90xjNhAENO8C7vtoEbCNcA mFjqU5WTaNzSqS+DBJxOzoRHErnHb1UvNimMs8+VeNjrUxi2jHVd9OKlz9aJOLEJbt8L0k iWeO6mHs4aGNMCyW9mF33jBHGmbwIcM= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b=Qf191j2Q; dmarc=none; spf=pass (imf08.hostedemail.com: domain of akpm@linux-foundation.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=akpm@linux-foundation.org X-Stat-Signature: 5i5n1eerm56q8stxwfs4fe78n9cgbpxd X-Rspamd-Queue-Id: 25CDA160062 X-Rspam-User: Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b=Qf191j2Q; dmarc=none; spf=pass (imf08.hostedemail.com: domain of akpm@linux-foundation.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=akpm@linux-foundation.org X-Rspamd-Server: rspam05 X-HE-Tag: 1657727140-759364 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 Wed, 13 Jul 2022 17:01:43 +0200 Maurizio Lombardi wrote: > A number of drivers call page_frag_alloc() with a > fragment's size > PAGE_SIZE. > In low memory conditions, __page_frag_cache_refill() may fail the order 3 > cache allocation and fall back to order 0; > In this case, the cache will be smaller than the fragment, causing > memory corruptions. > > Prevent this from happening by checking if the newly allocated cache > is large enough for the fragment; if not, the allocation will fail > and page_frag_alloc() will return NULL. > > ... > > --- a/mm/page_alloc.c > +++ b/mm/page_alloc.c > @@ -5617,6 +5617,8 @@ void *page_frag_alloc_align(struct page_frag_cache *nc, > /* reset page count bias and offset to start of new frag */ > nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; > offset = size - fragsz; > + if (unlikely(offset < 0)) > + return NULL; > } > > nc->pagecnt_bias--; I think we should have a comment here explaining (at least) why we'd bale after a successful allocation and explaining why we don't call free_the_page().