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 X-Spam-Level: X-Spam-Status: No, score=-15.3 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 46CD2C63798 for ; Wed, 25 Nov 2020 16:16:10 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 7D3FE2087C for ; Wed, 25 Nov 2020 16:16:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7D3FE2087C Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id AFE8E6B0071; Wed, 25 Nov 2020 11:16:08 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AB0CF6B0075; Wed, 25 Nov 2020 11:16:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9C6E66B0078; Wed, 25 Nov 2020 11:16:08 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0072.hostedemail.com [216.40.44.72]) by kanga.kvack.org (Postfix) with ESMTP id 83B936B0071 for ; Wed, 25 Nov 2020 11:16:08 -0500 (EST) Received: from smtpin23.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 415D31EE6 for ; Wed, 25 Nov 2020 16:16:08 +0000 (UTC) X-FDA: 77523442416.23.waves97_430ec3a27378 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin23.hostedemail.com (Postfix) with ESMTP id 0974137609 for ; Wed, 25 Nov 2020 16:16:08 +0000 (UTC) X-HE-Tag: waves97_430ec3a27378 X-Filterd-Recvd-Size: 5118 Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) by imf20.hostedemail.com (Postfix) with ESMTP for ; Wed, 25 Nov 2020 16:16:07 +0000 (UTC) X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id E57A6ABCE; Wed, 25 Nov 2020 16:16:05 +0000 (UTC) Subject: Re: [PATCH v2] mm/page_alloc: clear all pages in post_alloc_hook() with init_on_alloc=1 To: David Hildenbrand , linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, Andrew Morton , Alexander Potapenko , Michal Hocko , Mike Kravetz , Mike Rapoport , Oscar Salvador , Kees Cook , Michael Ellerman References: <20201120180452.19071-1-david@redhat.com> From: Vlastimil Babka Message-ID: <645f62ce-18fc-1586-7cd8-737924d919ba@suse.cz> Date: Wed, 25 Nov 2020 17:16:03 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.5.0 MIME-Version: 1.0 In-Reply-To: <20201120180452.19071-1-david@redhat.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit 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 11/20/20 7:04 PM, David Hildenbrand wrote: > commit 6471384af2a6 ("mm: security: introduce init_on_alloc=1 and > init_on_free=1 boot options") resulted with init_on_alloc=1 in all pages > leaving the buddy via alloc_pages() and friends to be > initialized/cleared/zeroed on allocation. > > However, the same logic is currently not applied to > alloc_contig_pages(): allocated pages leaving the buddy aren't cleared > with init_on_alloc=1 and init_on_free=0. Let's also properly clear > pages on that allocation path. > > To achieve that, let's move clearing into post_alloc_hook(). This will not > only affect alloc_contig_pages() allocations but also any pages used as > migration target in compaction code via compaction_alloc(). > > While this sounds sub-optimal, it's the very same handling as when > allocating migration targets via alloc_migration_target() - pages will > get properly cleared with init_on_free=1. In case we ever want to optimize > migration in that regard, we should tackle all such migration users - if we > believe migration code can be fully trusted. > > With this change, we will see double clearing of pages in some > cases. One example are gigantic pages (either allocated via CMA, or > allocated dynamically via alloc_contig_pages()) - which is the right > thing to do (and to be optimized outside of the buddy in the callers) as > discussed in: > https://lkml.kernel.org/r/20201019182853.7467-1-gpiccoli@canonical.com > > This change implies that with init_on_alloc=1 > - All CMA allocations will be cleared > - Gigantic pages allocated via alloc_contig_pages() will be cleared > - virtio-mem memory to be unplugged will be cleared. While this is > suboptimal, it's similar to memory balloon drivers handling, where > all pages to be inflated will get cleared as well. > - Pages isolated for compaction will be cleared > > Cc: Andrew Morton > Cc: Alexander Potapenko > Cc: Michal Hocko > Cc: Mike Kravetz > Cc: Vlastimil Babka > Cc: Mike Rapoport > Cc: Oscar Salvador > Cc: Kees Cook > Cc: Michael Ellerman > Signed-off-by: David Hildenbrand Acked-by: Vlastimil Babka > --- > > This is the follow-up of: > "[PATCH v1] mm/page_alloc: clear pages in alloc_contig_pages() with > init_on_alloc=1 or __GFP_ZERO" > > v1 -> v2: > - Let's clear anything that leaves the buddy, also affecting compaction. > - Don't implement __GFP_ZERO support for now > > --- > mm/page_alloc.c | 6 +++--- > 1 file changed, 3 insertions(+), 3 deletions(-) > > diff --git a/mm/page_alloc.c b/mm/page_alloc.c > index eaa227a479e4..108b81c0dfa8 100644 > --- a/mm/page_alloc.c > +++ b/mm/page_alloc.c > @@ -2275,6 +2275,9 @@ inline void post_alloc_hook(struct page *page, unsigned int order, > kasan_alloc_pages(page, order); > kernel_poison_pages(page, 1 << order, 1); > set_page_owner(page, order, gfp_flags); > + > + if (!free_pages_prezeroed() && want_init_on_alloc(gfp_flags)) > + kernel_init_free_pages(page, 1 << order); > } > > static void prep_new_page(struct page *page, unsigned int order, gfp_t gfp_flags, > @@ -2282,9 +2285,6 @@ static void prep_new_page(struct page *page, unsigned int order, gfp_t gfp_flags > { > post_alloc_hook(page, order, gfp_flags); > > - if (!free_pages_prezeroed() && want_init_on_alloc(gfp_flags)) > - kernel_init_free_pages(page, 1 << order); > - > if (order && (gfp_flags & __GFP_COMP)) > prep_compound_page(page, order); > >