Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp1377144iog; Tue, 14 Jun 2022 05:04:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzX4b2jGi8RmVhMwS8GcVC4zOik74q9hwTxB5Amy8ZXjhYvbnWU+QCULE2lh6SsFfSR5ZRI X-Received: by 2002:a63:5f0d:0:b0:3fd:7b18:bad8 with SMTP id t13-20020a635f0d000000b003fd7b18bad8mr4321297pgb.213.1655208290560; Tue, 14 Jun 2022 05:04:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655208290; cv=none; d=google.com; s=arc-20160816; b=HxONDT4h9IPpY3kNejD2y6lhZ69wx2pMeq3JZM3uBPrEeLj/q0hMib9/uNuKedMCUt Ku62O8c29acY/Y7vQqDIeHUl4FAPpFeJipUyuoeAd5axO+0QJYbZu3DRLmod6Y8HjDM3 XeDPSJe4tuyY9L4FLfKCx3WaS1Ne3Me6R/jqMD2G5afgSxnm9Mi+pUTWDpxGSLKLBpFE Z0W57WXNe1N007xhm3JPYu9GtXuUhklNEVFCnUn9vOMion4X/UGQwbOmnfaBUwZ7fjjG rTiqt78VcqBLIvtzGt5pjgWFQaVVa2QAPnVuHobBF7zJOmXkmpUq9LB7mrkOY71XWfvo aQww== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=moklifrXT687O3bE/HV0TN8CPE9FCj861qdwV/m46QM=; b=yCOzY/gNuuyhvJsXzFx2owiy4ehJNMqhZUR6QBvkbdwI4btJoynvdmFnX+pbT8Zpp8 1O7wfEXA0OgwnqxmBcksdWSAwb3uqIdHTvEgbHakfg1FjyarPTVBkfKK29DLTwCyQysw 5K5N3qT3rZo7f7AdKdRVzzfp1/YB0cJ4yEqC3WV178JLZiPXlnQdprDiY3q2sLIDoEXf 0FuwBvu/VgHBAI5zvU6nZtTYDtpnrgHHsisDwPeTp4NsqtUZkNjxVh9wFnty5TEK7wS5 mKd45OvPOPYV44mLt9+62P78XXdFXHWSCrP7M5wQF9T2fqkub8FmlLGVjwuqkrOqOO11 H1vg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Ay1kn7rO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q18-20020a637512000000b003facc0b7a8dsi14017466pgc.710.2022.06.14.05.04.32; Tue, 14 Jun 2022 05:04:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Ay1kn7rO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356619AbiFNMD2 (ORCPT + 99 others); Tue, 14 Jun 2022 08:03:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54402 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356278AbiFNMCq (ORCPT ); Tue, 14 Jun 2022 08:02:46 -0400 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CC0E219F92; Tue, 14 Jun 2022 05:02:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1655208163; x=1686744163; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=4+cZiXkfCn9pd/1sYbCEjbMclwb37pFvdZ6baZwVkzM=; b=Ay1kn7rOWzhe/eDdxI8T6YLzpQkCPCU8VIA6kUwWfQpQXlsplen/FDOa zOaZfls7LTP+zQ8RMF5WjP6Ze/AWDjj628LiillmHWBQyfr5MTOM1Nju6 oMRvK02UQtcEFxuH6DfgHOy3sjqR6NfvxmTQ6KXazTk6U7F7XMN8UUBuI EQHHk0IVucy65OUK77eDoYSLgpPhRJ+PrdQeitGXuILVzaOG7MHkDkUie khDvwLJ2u1hlNg0mTPQP2RGq7V4Lkht35QycRfXljovTfZhFUf3sAhqj4 j9FOkj8VJlYz8jNKPGmGF9rBT5Wb/pnRzFixvLlx8GvPYigmKXq+ytZVH Q==; X-IronPort-AV: E=McAfee;i="6400,9594,10377"; a="258432261" X-IronPort-AV: E=Sophos;i="5.91,300,1647327600"; d="scan'208";a="258432261" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Jun 2022 05:02:42 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.91,300,1647327600"; d="scan'208";a="686633480" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga002.fm.intel.com with ESMTP; 14 Jun 2022 05:02:36 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id C571B4EB; Tue, 14 Jun 2022 15:02:32 +0300 (EEST) From: "Kirill A. Shutemov" To: Borislav Petkov , Andy Lutomirski , Sean Christopherson , Andrew Morton , Joerg Roedel , Ard Biesheuvel Cc: Andi Kleen , Kuppuswamy Sathyanarayanan , David Rientjes , Vlastimil Babka , Tom Lendacky , Thomas Gleixner , Peter Zijlstra , Paolo Bonzini , Ingo Molnar , Varad Gautam , Dario Faggioli , Dave Hansen , Mike Rapoport , David Hildenbrand , marcelo.cerri@canonical.com, tim.gardner@canonical.com, khalid.elmously@canonical.com, philip.cox@canonical.com, x86@kernel.org, linux-mm@kvack.org, linux-coco@lists.linux.dev, linux-efi@vger.kernel.org, linux-kernel@vger.kernel.org, "Kirill A. Shutemov" Subject: [PATCHv7 09/14] x86/mm: Provide helpers for unaccepted memory Date: Tue, 14 Jun 2022 15:02:26 +0300 Message-Id: <20220614120231.48165-10-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220614120231.48165-1-kirill.shutemov@linux.intel.com> References: <20220614120231.48165-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-5.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_PASS, SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Core-mm requires few helpers to support unaccepted memory: - accept_memory() checks the range of addresses against the bitmap and accept memory if needed. - range_contains_unaccepted_memory() checks if anything within the range requires acceptance. Signed-off-by: Kirill A. Shutemov --- arch/x86/include/asm/page.h | 3 ++ arch/x86/include/asm/unaccepted_memory.h | 4 ++ arch/x86/mm/Makefile | 2 + arch/x86/mm/unaccepted_memory.c | 61 ++++++++++++++++++++++++ 4 files changed, 70 insertions(+) create mode 100644 arch/x86/mm/unaccepted_memory.c diff --git a/arch/x86/include/asm/page.h b/arch/x86/include/asm/page.h index 9cc82f305f4b..df4ec3a988dc 100644 --- a/arch/x86/include/asm/page.h +++ b/arch/x86/include/asm/page.h @@ -19,6 +19,9 @@ struct page; #include + +#include + extern struct range pfn_mapped[]; extern int nr_pfn_mapped; diff --git a/arch/x86/include/asm/unaccepted_memory.h b/arch/x86/include/asm/unaccepted_memory.h index 41fbfc798100..89fc91c61560 100644 --- a/arch/x86/include/asm/unaccepted_memory.h +++ b/arch/x86/include/asm/unaccepted_memory.h @@ -7,6 +7,10 @@ struct boot_params; void process_unaccepted_memory(struct boot_params *params, u64 start, u64 num); +#ifdef CONFIG_UNACCEPTED_MEMORY + void accept_memory(phys_addr_t start, phys_addr_t end); +bool range_contains_unaccepted_memory(phys_addr_t start, phys_addr_t end); #endif +#endif diff --git a/arch/x86/mm/Makefile b/arch/x86/mm/Makefile index f8220fd2c169..96fc2766b6d7 100644 --- a/arch/x86/mm/Makefile +++ b/arch/x86/mm/Makefile @@ -59,3 +59,5 @@ obj-$(CONFIG_AMD_MEM_ENCRYPT) += mem_encrypt_amd.o obj-$(CONFIG_AMD_MEM_ENCRYPT) += mem_encrypt_identity.o obj-$(CONFIG_AMD_MEM_ENCRYPT) += mem_encrypt_boot.o + +obj-$(CONFIG_UNACCEPTED_MEMORY) += unaccepted_memory.o diff --git a/arch/x86/mm/unaccepted_memory.c b/arch/x86/mm/unaccepted_memory.c new file mode 100644 index 000000000000..1df918b21469 --- /dev/null +++ b/arch/x86/mm/unaccepted_memory.c @@ -0,0 +1,61 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include +#include + +#include +#include +#include + +/* Protects unaccepted memory bitmap */ +static DEFINE_SPINLOCK(unaccepted_memory_lock); + +void accept_memory(phys_addr_t start, phys_addr_t end) +{ + unsigned long range_start, range_end; + unsigned long *bitmap; + unsigned long flags; + + if (!boot_params.unaccepted_memory) + return; + + bitmap = __va(boot_params.unaccepted_memory); + range_start = start / PMD_SIZE; + + spin_lock_irqsave(&unaccepted_memory_lock, flags); + for_each_set_bitrange_from(range_start, range_end, bitmap, + DIV_ROUND_UP(end, PMD_SIZE)) { + unsigned long len = range_end - range_start; + + /* Platform-specific memory-acceptance call goes here */ + panic("Cannot accept memory: unknown platform\n"); + bitmap_clear(bitmap, range_start, len); + } + spin_unlock_irqrestore(&unaccepted_memory_lock, flags); +} + +bool range_contains_unaccepted_memory(phys_addr_t start, phys_addr_t end) +{ + unsigned long *bitmap; + unsigned long flags; + bool ret = false; + + if (!boot_params.unaccepted_memory) + return 0; + + bitmap = __va(boot_params.unaccepted_memory); + + spin_lock_irqsave(&unaccepted_memory_lock, flags); + while (start < end) { + if (test_bit(start / PMD_SIZE, bitmap)) { + ret = true; + break; + } + + start += PMD_SIZE; + } + spin_unlock_irqrestore(&unaccepted_memory_lock, flags); + + return ret; +} -- 2.35.1