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: 13194078 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 12A3AC761A6 for ; Thu, 30 Mar 2023 11:50:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9B7BD6B0087; Thu, 30 Mar 2023 07:50:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9671D6B0088; Thu, 30 Mar 2023 07:50:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 795496B0089; Thu, 30 Mar 2023 07:50:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 65AD76B0087 for ; Thu, 30 Mar 2023 07:50:41 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 3F4431C64B8 for ; Thu, 30 Mar 2023 11:50:41 +0000 (UTC) X-FDA: 80625397482.28.AB44BA6 Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by imf09.hostedemail.com (Postfix) with ESMTP id 3AF15140010 for ; Thu, 30 Mar 2023 11:50:38 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=HXjJYQmy; spf=none (imf09.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 192.55.52.120) smtp.mailfrom=kirill.shutemov@linux.intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1680177039; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=4c+6RTABm8IpgO4JlTWZJ1FT6J6ytqIY0EMrGiPzEdU=; b=P+TVTkehlamcqRjCn82fxV041ILIyI2h7O7TwNfsGvTaStAh6TJN3c4GoqlBph+VIvxl0X l3gSMvT9eMljkJBUZ8bWY1yJCSEPrnQSZZmJeHJwfD5kymUMuelMCx6BhEFUYNj9Qpo+D7 EkrBijUYE7f5n2wqcEO3a4iI6f2ZBiw= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=HXjJYQmy; spf=none (imf09.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 192.55.52.120) smtp.mailfrom=kirill.shutemov@linux.intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1680177039; a=rsa-sha256; cv=none; b=ZjVqLf81Ay4yqm6AqE649JTnNdYeOj8pMlKcxSfpmAwup0uOuhCBAVIel4hhChCn/z1LFw mn2jJU1h2pAruD1Nn4QYfQ3aYdCDNcv5C8mnO/EgbPGBZw2tjbZ7VprMscfTGdVPxGCNbp Z/zJGDt4i82dS/+g3DC+qaJfDZ7BN84= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1680177039; x=1711713039; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=EdpB5LGN65LHmZVLPoPRNthJZeU3YLHWwC+hwkbMqQk=; b=HXjJYQmysKy8ldWzwbx7nHjQRt/xem4aw1nxqcD65yKkdTVIZav1joLj Bg3QOxS7qCy6Kcy+Cw/WNyGn5E8QDk4gbL2LzdRyd4h5K7S5qNUQ4DMAC RnzId96ew/NZt0hM6Z4DOn5WKQ2yMQbHmlOMzwf2PdBd7Ei3qCkeSmfly J45RzYE358qHt4iekrh9fVa7SrxbwoacGj+4gfQiQ/28VhajwtSDI25mU wFeZldh+/6y40rZP00QGdb1Bq2PvNeevym7W0QMuoPha/sIKOLMYId2Fr cpfeqpYLyna5lkvINVWpt329usNYRu4igEk1QovIdzFzWbCoXugh5klIx A==; X-IronPort-AV: E=McAfee;i="6600,9927,10664"; a="339868502" X-IronPort-AV: E=Sophos;i="5.98,303,1673942400"; d="scan'208";a="339868502" 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-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 3AF15140010 X-Rspam-User: X-Stat-Signature: 7u48n4wy3r3rzthoh4gsadomd6rcaadr X-HE-Tag: 1680177038-755977 X-HE-Meta: U2FsdGVkX18RtobPXdSlym0O4ZXjLS42aQBfcvgXVYobGlfdbFSV3tzj1hD1O/n70frBoIVifPBG/y+juZ6M74k3Dq6UkeDON8OM9pGRuEVV9l4UHODH0CKUa5OPM6ZAaXoXM276yTb4Sdy6cn4OK1MGO6SrOqC6kO1A7skLV5Lr6ovkU2CE1E/xJ4Y0hYD5CEPmsU+NE0J7WiOL3lSgxVn7JO0ckoKS2lGNAVIucEoaZwBLH2dHkpqPg1U2tkU9EzMdXABMQW/fIcclUJgG4sasS3Lg+ER21hkbjqUj7dpnC+TLXJ2ZRf89IGTW2HRo/T+PzJbJlP3cM5oG26kBUbEudeI+zcKEOTvGBrv0eFYBS7sfzT2CRjC3cgvoya4pgoGj42kSgP5QyA2pvG+DqODS7jXRDtkyOjBt71j6CeSlMZ/DDycgAHKZxuvDQZnS5zoh9bfFpewjyomuMcVES/u1iXbzIZBfe1A1oSBYX4OWuXJDgj2zBVxqKEST53pn+RDjjgKWNc+z2aAOhXS6fC23wnwlpBDJTeByte1dGNQNw5NgY89Kul1j1oJ9OV1DQdhj4LK6MDBG4smXLg2m5gn6phXqTc0bFApg3p0QrRObJ93G2GTlRoLEheujo7R1gdq+vNtGiMOvwpTCU/MgF7Se1qB4RuwwPyxwDPiIiAfCuyHqTie/kqvXZHEvWGyrsR3Y1CRposbm/x8LbWjKtZrY0pc4tsUi6VzOwLdtGBphT6hHkB0y77aVe7oagmumbsX1fKMSsHrn8KlAJtF9OM/vt7SPeOtg80Fumd+VGolE02JgoVh5/IJzeY8mymPT0J81w4x3jh+uIcgP652rb4fYY6Y9AiPycohxJY9vFRqDZ4f2uQ/yFPXWWSmqSt/yC3fs0WNCr8ls1+xlxvTQ6/yapnGS6deh4M+AELUv9jjUMNLcqf3xf0rjeCw2BB9EotyVyEIykC/o+OgmwYb B8PW8skc UPUeh60msLfjQZXtZwbSL8f8juLXP29JhIxk4AvthjgZ3apy5cuD6Fosht4CbjWpK+3NKSrsTdZGp0O9B+UDI5zmqEYL0Cjm3BjNMvlzTEWPbH4At6TenibryvfHZgHDnRZc0GM7UvqHUiNV79vf00eNeYYMF6MhKlj7V 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: 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; +}