Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp4256469pxb; Tue, 10 Nov 2020 11:38:22 -0800 (PST) X-Google-Smtp-Source: ABdhPJxmpgK8yMwDCgQfUXsKXboyeeyD7TZbTYDW7OsRKhPWvGRd2s/HQophee+vu2kU3IBQa6pD X-Received: by 2002:a17:906:f18f:: with SMTP id gs15mr22322796ejb.474.1605037102506; Tue, 10 Nov 2020 11:38:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605037102; cv=none; d=google.com; s=arc-20160816; b=R9MsbD9Gq5CovNcLLG0zYDhYekaBtzMDkCnE8YFQ0Ks+MwTwDTq8TV6SYxZMAONRwQ PrA8sUE1BfXRAvtGc3SdBMGo+mJi7AVpUhwjALhGmK2gBLN6K6HC3NMs8mpNGe6+wJ7c RL3QPQugezhyekWf7jjH8YAr8iNEvRYnD3j3ndCKGjpPpRAZgWP9dslVM18J9VF1NlP7 b+oPDHyNAjHAuzJ41w/RcK8Z6by+6JbxUpv/92sxhxNbR9b16B+tOoDR9aclxmFdnoIP Do4bTdyFMhD6+stoMDW97yxvq6vOLaDoQ9Z3++a0w6Rowp+RJqVTnFxAa9zBrIiZt2fl SiqQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=m4Ph1suYX4ben8sinoTI3mwXxIcZAzftReDT/Kw+gqs=; b=AD2INT3UVU4S0BMEeNDk+dh3QfU897OuQiS0YENMqEW0ZrrZ6u1A7xA6A174KLB8qd 4r9C9OFiLEzVIxB5mrpY1apecpUji/EnBEaWYW4vq7ER70/rm1F/xhFSaWlbDjtA4zjr KOtP4pTEVJjma4bcD8L5V4CeCrAEJVhVgwPWZkspY/d+ILHM545uZOSlMJX+kDTtasKs 6fg5Uy4SJHxAwGtRuPzq0nuZ7AgOoW5w2r5QLl+6gLukmPCEn1QblPnI3TUrU77dCR4q V8cQg9a/GBB3+ZqY2l59PUcmz4NNCWYjmvn3id4FY4ZSZMtLp2wRreQLWAaJEb2YqTXL ohUQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=dusbLTy0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h8si4437343ejj.161.2020.11.10.11.37.57; Tue, 10 Nov 2020 11:38:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=dusbLTy0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732593AbgKJTc7 (ORCPT + 99 others); Tue, 10 Nov 2020 14:32:59 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:31275 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732722AbgKJTc4 (ORCPT ); Tue, 10 Nov 2020 14:32:56 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1605036774; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=m4Ph1suYX4ben8sinoTI3mwXxIcZAzftReDT/Kw+gqs=; b=dusbLTy0RMeVriT7gRaDs4QgWXsFDDwj1Bp3CaTYhLR5neHZWRmSb65PC8+BYy9YoTRBJ7 tNsgRhyQg4J1SKY9P5rCUtpxqvG3Tuoxh3PLeakouZpBnYSd1qMpMVKYaCNf6UYZKGaB3D wjJCl8XCgp7kc1qW/ZfkriVrdKXGNWY= 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-200-SDgoVi_HO3GfYuNGMHgeGg-1; Tue, 10 Nov 2020 14:32:50 -0500 X-MC-Unique: SDgoVi_HO3GfYuNGMHgeGg-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 A9DA310082E0; Tue, 10 Nov 2020 19:32:48 +0000 (UTC) Received: from t480s.redhat.com (ovpn-114-232.ams2.redhat.com [10.36.114.232]) by smtp.corp.redhat.com (Postfix) with ESMTP id A86255B4A0; Tue, 10 Nov 2020 19:32:41 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Alexander Potapenko , Michal Hocko , Mike Kravetz , Vlastimil Babka , Mike Rapoport , Oscar Salvador , Kees Cook , Michael Ellerman Subject: [PATCH v1] mm/page_alloc: clear pages in alloc_contig_pages() with init_on_alloc=1 or __GFP_ZERO Date: Tue, 10 Nov 2020 20:32:40 +0100 Message-Id: <20201110193240.25401-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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) +{ + 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. * * The PFN range does not have to be pageblock or MAX_ORDER_NR_PAGES * aligned. The PFN range must belong to a single zone. @@ -8488,7 +8502,7 @@ int alloc_contig_range(unsigned long start, unsigned long end, .mode = MIGRATE_SYNC, .ignore_skip_hint = true, .no_set_skip_hint = true, - .gfp_mask = current_gfp_context(gfp_mask), + .gfp_mask = current_gfp_context(gfp_mask & ~__GFP_ZERO), .alloc_contig = true, }; INIT_LIST_HEAD(&cc.migratepages); @@ -8600,6 +8614,9 @@ int alloc_contig_range(unsigned long start, unsigned long end, if (end != outer_end) free_contig_range(end, outer_end - end); + if (!want_init_on_free() && want_init_on_alloc(gfp_mask)) + __alloc_contig_clear_range(start, end); + done: undo_isolate_page_range(pfn_max_align_down(start), pfn_max_align_up(end), migratetype); @@ -8653,7 +8670,8 @@ static bool zone_spans_last_pfn(const struct zone *zone, /** * alloc_contig_pages() -- tries to find and allocate contiguous range of pages * @nr_pages: Number of contiguous pages to allocate - * @gfp_mask: GFP mask to limit search and used during compaction + * @gfp_mask: GFP mask to limit search and used during compaction. __GFP_ZERO + * clears allocated pages. * @nid: Target node * @nodemask: Mask for other possible nodes * -- 2.26.2