From patchwork Thu Mar 30 11:49:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kirill A. Shutemov" X-Patchwork-Id: 77150 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1066913vqo; Thu, 30 Mar 2023 04:56:47 -0700 (PDT) X-Google-Smtp-Source: AKy350ZLVPbDSGnji/0EkqbFmVuCQ+4yv9DXgO6l1uOho0wHV66t5cPluSugf3BmRLyExMqzU6KA X-Received: by 2002:a17:903:2343:b0:1a1:ee8c:eef8 with SMTP id c3-20020a170903234300b001a1ee8ceef8mr28262158plh.2.1680177406806; Thu, 30 Mar 2023 04:56:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680177406; cv=none; d=google.com; s=arc-20160816; b=afoi0DXS2uxc7OkEVwqbXLJ0Oo/aQ/Z3FX6/sX8mUegcAT6g4lYfBG1mZBiPi1DPE1 MXz//XeJ74NpWADQEY7TC8B0FY8rw2VwSJMHtRerMDa0s/f7cwoAUxFmQEre8zcmTASJ l9WQO3I8YCJLE9nVW6OaTNFcUUK8z6QXUxUBTWDk1sbNEro4sFva05TW/M7PPNqqiW2r 713M4LAqtPaZ3IsxHEnPzg4nbQcP04WWgr4FKKAlPS+UkDrE55kU1WflHFxEMYNqxQbO HeYP9fFcn+24kVksPFAcGCp4XSs90AsnbAsiyIhJnF3xCsnLZaaL7ZDvmSdxxydroHfT cNRQ== 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=4c+6RTABm8IpgO4JlTWZJ1FT6J6ytqIY0EMrGiPzEdU=; b=MzfXTlzCMtSf4w03OoY+fT4euXDf/HT3f40nTiuURkfTopYrO76cPPBSA1U3OFq4S4 mSSczssw1/YkGWhbTpKNLSPJtaR7a5YASx/FxC35rU1ZmBYwUc2OxLtGVnLxA83VRodI TfMPsqYUFK/RsgMQGuUx6UAJwNBeT8GPrk+drdNydyhLPSq4LNtbannT0JUcI3rx16wI nipMuUQDewLKaH3QF6K2cgefJ14e+DVqnjtjJAHRjPaLrqFTtJWPn/zQx/QG4Z30Mqnj WLgqT+RDjie6KksgoDTVvKekRaF0BCfCsDQlYgscW3z/L4bSUyHgIdHvCpNL49lgDFxR RPTA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=MVWl1k1W; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id jw12-20020a170903278c00b0019f359c651esi33076994plb.556.2023.03.30.04.56.34; Thu, 30 Mar 2023 04:56:46 -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=MVWl1k1W; 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 S231839AbjC3Lus (ORCPT + 99 others); Thu, 30 Mar 2023 07:50:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43778 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231499AbjC3Lu2 (ORCPT ); Thu, 30 Mar 2023 07:50:28 -0400 Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 21FD6900D; Thu, 30 Mar 2023 04:50:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1680177027; x=1711713027; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=EdpB5LGN65LHmZVLPoPRNthJZeU3YLHWwC+hwkbMqQk=; b=MVWl1k1WUDMU4a/R81WPZZDk5VJufX9wbZwKQRQascuQDZKVcQFW+46p IZ9nExMHfiWZqedPnMHGELbAUK5iGJzuUCe6ryrDElWkmcD5mGkbcr2T2 UdEmnP9pEKNPkDkWKgTtntOLDfP5mX2iSy/R+UFqX3Faqz9boUNaCMRcM PoHyP2EVIrBIrhuKXSjFqPZVXYSf7FMByZdSiQOSSEb/3OyB2lTn4nzJt un3Ccq/7dD6P7HceVbnsE2l5GJyqZomX58YO8spQfyNAW8dzzxqXy8dbJ E6eibpj4tSxSHaPpbPJnBseG/TvSn+8HThXUDBjOPpHiVm4ydR23Stb4r Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10664"; a="339868511" X-IronPort-AV: E=Sophos;i="5.98,303,1673942400"; d="scan'208";a="339868511" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Mar 2023 04:50:25 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10664"; a="1014401451" X-IronPort-AV: E=Sophos;i="5.98,303,1673942400"; d="scan'208";a="1014401451" Received: from ngreburx-mobl.ger.corp.intel.com (HELO box.shutemov.name) ([10.251.209.91]) by fmsmga005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Mar 2023 04:50:17 -0700 Received: by box.shutemov.name (Postfix, from userid 1000) id 54CD11046EE; Thu, 30 Mar 2023 14:50:00 +0300 (+03) 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 , Dario Faggioli , Dave Hansen , Mike Rapoport , David Hildenbrand , Mel Gorman , marcelo.cerri@canonical.com, tim.gardner@canonical.com, khalid.elmously@canonical.com, philip.cox@canonical.com, aarcange@redhat.com, peterx@redhat.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: [PATCHv9 10/14] x86/mm: Provide helpers for unaccepted memory Date: Thu, 30 Mar 2023 14:49:52 +0300 Message-Id: <20230330114956.20342-11-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230330114956.20342-1-kirill.shutemov@linux.intel.com> References: <20230330114956.20342-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.4 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_NONE autolearn=unavailable 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761793704165299018?= X-GMAIL-MSGID: =?utf-8?q?1761793704165299018?= 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 d18e5c332cb9..4bab2bb2c9c0 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 c80febc44cd2..b0ef1755e5c8 100644 --- a/arch/x86/mm/Makefile +++ b/arch/x86/mm/Makefile @@ -67,3 +67,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; +}