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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C2590C7EE22 for ; Sun, 7 May 2023 23:46:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5A6E76B0080; Sun, 7 May 2023 19:46:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 52F646B0081; Sun, 7 May 2023 19:46:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3A9396B0082; Sun, 7 May 2023 19:46:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 244BF6B0080 for ; Sun, 7 May 2023 19:46:44 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id EFD6D1A03F1 for ; Sun, 7 May 2023 23:46:43 +0000 (UTC) X-FDA: 80765096286.27.17107E5 Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by imf17.hostedemail.com (Postfix) with ESMTP id BD37140009 for ; Sun, 7 May 2023 23:46:41 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=bxFX0ajp; spf=none (imf17.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 134.134.136.126) 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=1683503202; 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=jIUiMzRZmjN22DK9+hQvhuVQsg2FufEr/pUGXFgijKU=; b=Wqnz0x4j31Inejjw17syocZ9xI69I/TCBmnn8/+nxjrgrmDavdD/Ny7gsVC2xORJ+LKhAw FbuqELJpA+gYlWrFFy2kvkwsHbaVjOnfjGR5cGQBNlu3yo9Csme3uTzAbj17iZd07auzFJ d4xTEfpBW4BACuaOiWHVOjX2Vq7HSiU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1683503202; a=rsa-sha256; cv=none; b=KWYuL0LBeoQt6XMRgqJJgoe2TweIFmFuNMMoqhJUBzXtEGkjIUuiO+5nDlaSi6tznrTtiB ZKY/449ffiQ2RsMXpPy7UasGTXfzEEZdfLhuFkybp9X7VNwsGgYFek9MJQPaDgZfVGKHdI iWhM1MSBf0A9efsrB8KPRKxAIrbKk6g= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=bxFX0ajp; spf=none (imf17.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 134.134.136.126) smtp.mailfrom=kirill.shutemov@linux.intel.com; dmarc=pass (policy=none) header.from=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1683503202; x=1715039202; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=gf4ahacUkHLsgr0L6SRROIOspGC7mKmz2423NAe8qwk=; b=bxFX0ajptPlksyf0eHzPbkzpB2sOOt7ygtkiWZnL6nNyCl8S1oVipU4+ 6lRydv4IDrJS+s5Bt/+yepZA2YTm+Mm7Cn44z9YhlcS/3vO1GJQZhSMu0 xZ+QNwKV7ip4YMd1oIAyaskGHCiJvSd34KuRGgXD8gGzeXMTN6V1eboFO FOu479TSPGFx27qIZAAiZ2JztRZANHwo1LMPDGit2HIEA+071BZ/gjCSB U4wlmpX0wRPX5SklPWGkvWHd9JeoZFdl/dvVQr90C9HRG3S9zu5dqY5nO rMNj2sF/n78XxsiQ3piMXTB6hfw05Lt2oi4xhSR7QhzVX+CCJE30THrBn Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10703"; a="333953538" X-IronPort-AV: E=Sophos;i="5.99,258,1677571200"; d="scan'208";a="333953538" Received: from orsmga006.jf.intel.com ([10.7.209.51]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 May 2023 16:46:40 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10703"; a="675852968" X-IronPort-AV: E=Sophos;i="5.99,258,1677571200"; d="scan'208";a="675852968" Received: from dancaspi-mobl1.ger.corp.intel.com (HELO box.shutemov.name) ([10.252.61.73]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 May 2023 16:46:32 -0700 Received: by box.shutemov.name (Postfix, from userid 1000) id 53CDB10D33B; Mon, 8 May 2023 02:46:20 +0300 (+03) From: "Kirill A. Shutemov" To: Borislav Petkov , Andy Lutomirski , Dave Hansen , 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 , 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: [PATCHv10 07/11] x86/mm: Provide helpers for unaccepted memory Date: Mon, 8 May 2023 02:46:14 +0300 Message-Id: <20230507234618.18067-8-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230507234618.18067-1-kirill.shutemov@linux.intel.com> References: <20230507234618.18067-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: BD37140009 X-Rspam-User: X-Rspamd-Server: rspam06 X-Stat-Signature: 3fs8t9gwefyphfrbeowxs83b3hibo3w7 X-HE-Tag: 1683503201-95247 X-HE-Meta: U2FsdGVkX19pK9UVOTL0g2fn5P9DkbJdPRoKT0vREL/DoZeFSVXhZ2W2OMG75qd/vIijQnTHx/pmx4AObNb6oi0tvJ0L8EHPrnmk/YGT+Apzj8Mt0jWahptLKW9058HVd9XXmTEMElzOTd5+SwO+hCGm0xuyKikvSX6YXaXU0lEXJrcjGI5V1gBABwnaxpE59BA4ccvZCMLkf0dq0MOk1LNg1oDdY6Lzpw/X4tcGay7VpvdZ8rXDeryLy4fo0xSudQ7KVC+SNhU9Q0foWwkvl7sZH/Bn3/VM0jwzgC3p/EuXEGuKpH0HZSPmuYDt4ZJQ79ePQRHCMJa9Km+gZCtWZR52oispCxt9XSimBiF/Xg2KogYCcNATgupDItbcp+6We1MuftRxBaVaclIVSoGCjp0Sb3Uxv0MGb/UHH1jn8pC2uhS52MWSMUjtp1g5mdcanSrY5Fval9Pb0l1ojPputQWL/R4X/Uqk5QeWUOVYxxAqWL1/SD3OAIgaHia0lb3EnXaTo12O9+Gi867+yDJwPvF/vy8kAqmxOyig+qY6ct/xVblgueGoc24keFxLehWdDDqSHyaMTpXvC7DPh1aneGAJ4SmcOUbo0a5L7rR8EqWIcRn951L3EApEw5eW2B9xfRGx/IxVbpifgmF4KmLF34ntPuU/c/zNyliQslunfHocgMZq2zIo0lVvvy0fa4dhRSEAKma56Mnbn0Bz4k6arI0MWLCSrUzLjW52M3qeM6HFHuZYZ1zNauJ63ejBJIwW+65qa5UwJquQ4rq6x6Xm7L7Jz2nDIEr/vvjp5oIT8xVE9olAF+jLMK/P7BFOyBHktb0xHISUSZrGK4a8fjqd3vR2yu6PdMe2NnD6g0OKzNk6UBVjd58DFANKtVJ6MMC/CbG45nenPG2mQGk7kGCg4cPlG3fZxI/2BUGIRqZcVDQNaXWt9N6lT0DQ+FJNcenyum4EavvatuDylnDU4a3 16WCEpHz w2vkmsBo+uDCHENY9BFsQmcwC3fKkBharpOIov+mZaDnOVYorXEPyipCzuVbHSVpsZ2jsG1MZuDmhDTT9m40ekfK/WiJYf3gMU2ZZyUs6ar6kws/OQgEOASdN88oqs6dFh+KoxSGQKZRHm+M= 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; +} -- 2.39.3