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=-11.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,NICE_REPLY_A,SIGNED_OFF_BY,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 D41E1C56201 for ; Wed, 11 Nov 2020 10:07:00 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 594B520674 for ; Wed, 11 Nov 2020 10:07:00 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="V8T3XZ0z" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727415AbgKKKG6 (ORCPT ); Wed, 11 Nov 2020 05:06:58 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:53442 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727017AbgKKKG4 (ORCPT ); Wed, 11 Nov 2020 05:06:56 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1605089214; h=from:from: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; bh=RaLHPjmJ00M1E8sahIOjQ0lphEJM3N6HGgzEWqFx0X4=; b=V8T3XZ0zcC3acOTcKZJN+YKFzpCy3AZn78CstApDD/q3dphB93hO10zClLHwvEUPNHRKrh 8+r4eh4ul48VIxH6uT6+GN9ySF/KDWfeeuBTprOmbBSQJ10gsNBCCIrIhhHxCxvqHbsC2i b8RoyV5hPS1/zDo5do4SKX7HC/XSiw8= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-188-D_xATvuaP964THNols8Ong-1; Wed, 11 Nov 2020 05:06:50 -0500 X-MC-Unique: D_xATvuaP964THNols8Ong-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 376E711CC7E1; Wed, 11 Nov 2020 10:06:48 +0000 (UTC) Received: from [10.36.114.151] (ovpn-114-151.ams2.redhat.com [10.36.114.151]) by smtp.corp.redhat.com (Postfix) with ESMTP id CB56E5B4D5; Wed, 11 Nov 2020 10:06:45 +0000 (UTC) Subject: Re: [PATCH v1] mm/page_alloc: clear pages in alloc_contig_pages() with init_on_alloc=1 or __GFP_ZERO To: Mike Rapoport Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andrew Morton , Alexander Potapenko , Michal Hocko , Mike Kravetz , Vlastimil Babka , Oscar Salvador , Kees Cook , Michael Ellerman References: <20201110193240.25401-1-david@redhat.com> <20201111095909.GC4755@linux.ibm.com> From: David Hildenbrand Organization: Red Hat GmbH Message-ID: <3e98187a-2a2f-e5fb-c364-d230d3ba132f@redhat.com> Date: Wed, 11 Nov 2020 11:06:45 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.6.0 MIME-Version: 1.0 In-Reply-To: <20201111095909.GC4755@linux.ibm.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 11.11.20 10:59, Mike Rapoport wrote: > On Tue, Nov 10, 2020 at 08:32:40PM +0100, 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 and add support for __GFP_ZERO. >> >> 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. >> >> 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 >> --- >> mm/page_alloc.c | 24 +++++++++++++++++++++--- >> 1 file changed, 21 insertions(+), 3 deletions(-) >> >> diff --git a/mm/page_alloc.c b/mm/page_alloc.c >> index eed4f4075b3c..0361b119b74e 100644 >> --- a/mm/page_alloc.c >> +++ b/mm/page_alloc.c >> @@ -8453,6 +8453,19 @@ static int __alloc_contig_migrate_range(struct compact_control *cc, >> return 0; >> } >> >> +static void __alloc_contig_clear_range(unsigned long start_pfn, >> + unsigned long end_pfn) > > Maybe clear_contig_range() ? I chose the naming to match "__alloc_contig_migrate_range", but I agree that your version sounds better. > >> +{ >> + unsigned long pfn; >> + >> + for (pfn = start_pfn; pfn < end_pfn; pfn += MAX_ORDER_NR_PAGES) { >> + cond_resched(); >> + kernel_init_free_pages(pfn_to_page(pfn), >> + min_t(unsigned long, end_pfn - pfn, >> + MAX_ORDER_NR_PAGES)); >> + } >> +} >> + >> /** >> * alloc_contig_range() -- tries to allocate given range of pages >> * @start: start PFN to allocate >> @@ -8461,7 +8474,8 @@ static int __alloc_contig_migrate_range(struct compact_control *cc, >> * #MIGRATE_MOVABLE or #MIGRATE_CMA). All pageblocks >> * in range must have the same migratetype and it must >> * be either of the two. >> - * @gfp_mask: GFP mask to use during compaction >> + * @gfp_mask: GFP mask to use during compaction. __GFP_ZERO clears allocated >> + * pages. > > "__GFP_ZERO is not passed to compaction but rather clears allocated pages" Bought! Thanks :) -- Thanks, David / dhildenb