All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
To: Ingo Molnar <mingo@redhat.com>,
	x86@kernel.org, Thomas Gleixner <tglx@linutronix.de>,
	"H. Peter Anvin" <hpa@zytor.com>,
	Tom Lendacky <thomas.lendacky@amd.com>
Cc: Dave Hansen <dave.hansen@intel.com>,
	Kai Huang <kai.huang@linux.intel.com>,
	Jacob Pan <jacob.jun.pan@linux.intel.com>,
	linux-kernel@vger.kernel.org, linux-mm@kvack.org,
	"Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
Subject: [PATCHv4 09/18] x86/mm: Implement page_keyid() using page_ext
Date: Tue, 26 Jun 2018 17:22:36 +0300	[thread overview]
Message-ID: <20180626142245.82850-10-kirill.shutemov@linux.intel.com> (raw)
In-Reply-To: <20180626142245.82850-1-kirill.shutemov@linux.intel.com>

Store KeyID in bits 31:16 of extended page flags. These bits are unused.

page_keyid() returns zero until page_ext is ready. page_ext initializer
enables static branch to indicate that page_keyid() can use page_ext.
The same static branch will gate MKTME readiness in general.

We don't yet set KeyID for the page. It will come in the following
patch that implements prep_encrypted_page(). All pages have KeyID-0 for
now.

Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
---
 arch/x86/include/asm/mktme.h |  7 +++++++
 arch/x86/include/asm/page.h  |  1 +
 arch/x86/mm/mktme.c          | 34 ++++++++++++++++++++++++++++++++++
 include/linux/page_ext.h     | 11 ++++++++++-
 mm/page_ext.c                |  3 +++
 5 files changed, 55 insertions(+), 1 deletion(-)

diff --git a/arch/x86/include/asm/mktme.h b/arch/x86/include/asm/mktme.h
index df31876ec48c..7266494b4f0a 100644
--- a/arch/x86/include/asm/mktme.h
+++ b/arch/x86/include/asm/mktme.h
@@ -2,11 +2,18 @@
 #define	_ASM_X86_MKTME_H
 
 #include <linux/types.h>
+#include <linux/page_ext.h>
 
 #ifdef CONFIG_X86_INTEL_MKTME
 extern phys_addr_t mktme_keyid_mask;
 extern int mktme_nr_keyids;
 extern int mktme_keyid_shift;
+
+extern struct page_ext_operations page_mktme_ops;
+
+#define page_keyid page_keyid
+int page_keyid(const struct page *page);
+
 #else
 #define mktme_keyid_mask	((phys_addr_t)0)
 #define mktme_nr_keyids		0
diff --git a/arch/x86/include/asm/page.h b/arch/x86/include/asm/page.h
index 7555b48803a8..39af59487d5f 100644
--- a/arch/x86/include/asm/page.h
+++ b/arch/x86/include/asm/page.h
@@ -19,6 +19,7 @@
 struct page;
 
 #include <linux/range.h>
+#include <asm/mktme.h>
 extern struct range pfn_mapped[];
 extern int nr_pfn_mapped;
 
diff --git a/arch/x86/mm/mktme.c b/arch/x86/mm/mktme.c
index 467f1b26c737..09cbff678b9f 100644
--- a/arch/x86/mm/mktme.c
+++ b/arch/x86/mm/mktme.c
@@ -3,3 +3,37 @@
 phys_addr_t mktme_keyid_mask;
 int mktme_nr_keyids;
 int mktme_keyid_shift;
+
+static DEFINE_STATIC_KEY_FALSE(mktme_enabled_key);
+
+static inline bool mktme_enabled(void)
+{
+	return static_branch_unlikely(&mktme_enabled_key);
+}
+
+int page_keyid(const struct page *page)
+{
+	if (!mktme_enabled())
+		return 0;
+
+	return lookup_page_ext(page)->keyid;
+}
+EXPORT_SYMBOL(page_keyid);
+
+static bool need_page_mktme(void)
+{
+	/* Make sure keyid doesn't collide with extended page flags */
+	BUILD_BUG_ON(__NR_PAGE_EXT_FLAGS > 16);
+
+	return !!mktme_nr_keyids;
+}
+
+static void init_page_mktme(void)
+{
+	static_branch_enable(&mktme_enabled_key);
+}
+
+struct page_ext_operations page_mktme_ops = {
+	.need = need_page_mktme,
+	.init = init_page_mktme,
+};
diff --git a/include/linux/page_ext.h b/include/linux/page_ext.h
index f84f167ec04c..d9c5aae9523f 100644
--- a/include/linux/page_ext.h
+++ b/include/linux/page_ext.h
@@ -23,6 +23,7 @@ enum page_ext_flags {
 	PAGE_EXT_YOUNG,
 	PAGE_EXT_IDLE,
 #endif
+	__NR_PAGE_EXT_FLAGS
 };
 
 /*
@@ -33,7 +34,15 @@ enum page_ext_flags {
  * then the page_ext for pfn always exists.
  */
 struct page_ext {
-	unsigned long flags;
+	union {
+		unsigned long flags;
+#ifdef CONFIG_X86_INTEL_MKTME
+		struct {
+			unsigned short __pad;
+			unsigned short keyid;
+		};
+#endif
+	};
 };
 
 extern void pgdat_page_ext_init(struct pglist_data *pgdat);
diff --git a/mm/page_ext.c b/mm/page_ext.c
index a9826da84ccb..036658229842 100644
--- a/mm/page_ext.c
+++ b/mm/page_ext.c
@@ -68,6 +68,9 @@ static struct page_ext_operations *page_ext_ops[] = {
 #if defined(CONFIG_IDLE_PAGE_TRACKING) && !defined(CONFIG_64BIT)
 	&page_idle_ops,
 #endif
+#ifdef CONFIG_X86_INTEL_MKTME
+	&page_mktme_ops,
+#endif
 };
 
 static unsigned long total_usage;
-- 
2.18.0


  parent reply	other threads:[~2018-06-26 14:25 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-26 14:22 [PATCHv4 00/18] MKTME enabling Kirill A. Shutemov
2018-06-26 14:22 ` [PATCHv4 01/18] mm: Do no merge VMAs with different encryption KeyIDs Kirill A. Shutemov
2018-06-26 14:22 ` [PATCHv4 02/18] mm/ksm: Do not merge pages with different KeyIDs Kirill A. Shutemov
2018-07-09 18:03   ` Konrad Rzeszutek Wilk
2018-06-26 14:22 ` [PATCHv4 03/18] mm/page_alloc: Unify alloc_hugepage_vma() Kirill A. Shutemov
2018-06-26 14:22 ` [PATCHv4 04/18] mm/page_alloc: Handle allocation for encrypted memory Kirill A. Shutemov
2018-06-26 14:22 ` [PATCHv4 05/18] mm/khugepaged: Handle encrypted pages Kirill A. Shutemov
2018-06-26 14:22 ` [PATCHv4 06/18] x86/mm: Mask out KeyID bits from page table entry pfn Kirill A. Shutemov
2018-06-26 14:22 ` [PATCHv4 07/18] x86/mm: Introduce variables to store number, shift and mask of KeyIDs Kirill A. Shutemov
2018-07-09 18:09   ` Konrad Rzeszutek Wilk
2018-07-10 10:48     ` Kirill A. Shutemov
2018-06-26 14:22 ` [PATCHv4 08/18] x86/mm: Preserve KeyID on pte_modify() and pgprot_modify() Kirill A. Shutemov
2018-06-26 14:22 ` Kirill A. Shutemov [this message]
2018-06-26 14:22 ` [PATCHv4 10/18] x86/mm: Implement vma_keyid() Kirill A. Shutemov
2018-06-26 14:22 ` [PATCHv4 11/18] x86/mm: Implement prep_encrypted_page() and arch_free_page() Kirill A. Shutemov
2018-06-26 14:22 ` [PATCHv4 12/18] x86/mm: Rename CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING Kirill A. Shutemov
2018-06-26 14:22 ` [PATCHv4 13/18] x86/mm: Allow to disable MKTME after enumeration Kirill A. Shutemov
2018-07-09 18:20   ` Konrad Rzeszutek Wilk
2018-07-10 10:49     ` Kirill A. Shutemov
2018-07-10 11:21       ` Konrad Rzeszutek Wilk
2018-06-26 14:22 ` [PATCHv4 14/18] x86/mm: Detect MKTME early Kirill A. Shutemov
2018-06-26 14:22 ` [PATCHv4 15/18] x86/mm: Calculate direct mapping size Kirill A. Shutemov
2018-07-09 18:32   ` Konrad Rzeszutek Wilk
2018-06-26 14:22 ` [PATCHv4 16/18] x86/mm: Implement sync_direct_mapping() Kirill A. Shutemov
2018-06-26 14:22 ` [PATCHv4 17/18] x86/mm: Handle encrypted memory in page_to_virt() and __pa() Kirill A. Shutemov
2018-06-26 16:38   ` Dave Hansen
2018-06-27 21:56     ` Kirill A. Shutemov
2018-06-26 14:22 ` [PATCHv4 18/18] x86: Introduce CONFIG_X86_INTEL_MKTME Kirill A. Shutemov
2018-06-26 17:30   ` Randy Dunlap
2018-06-27 21:57     ` Kirill A. Shutemov
2018-06-27 23:48       ` Randy Dunlap
2018-07-09 18:36   ` Konrad Rzeszutek Wilk
2018-07-09 18:44     ` Dave Hansen
2018-07-09 18:52       ` Konrad Rzeszutek Wilk
2018-07-09 18:59         ` Dave Hansen
2018-07-09 20:29           ` Konrad Rzeszutek Wilk

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180626142245.82850-10-kirill.shutemov@linux.intel.com \
    --to=kirill.shutemov@linux.intel.com \
    --cc=dave.hansen@intel.com \
    --cc=hpa@zytor.com \
    --cc=jacob.jun.pan@linux.intel.com \
    --cc=kai.huang@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mingo@redhat.com \
    --cc=tglx@linutronix.de \
    --cc=thomas.lendacky@amd.com \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.