[PATCH 5/8] x86/clear_page: add clear_page_uncached()

From: Ankur Arora
Date: Wed Oct 14 2020 - 04:33:52 EST


Define clear_page_uncached() as an alternative_call() to clear_page_nt()
if the CPU sets X86_FEATURE_NT_GOOD and fallback to clear_page() if it
doesn't.

Similarly define clear_page_uncached_flush() which provides an SFENCE
if the CPU sets X86_FEATURE_NT_GOOD.

Also, add the glue interface clear_user_highpage_uncached().

Signed-off-by: Ankur Arora <ankur.a.arora@xxxxxxxxxx>
---
arch/x86/include/asm/page.h | 6 ++++++
arch/x86/include/asm/page_32.h | 9 +++++++++
arch/x86/include/asm/page_64.h | 14 ++++++++++++++
include/asm-generic/page.h | 3 +++
include/linux/highmem.h | 10 ++++++++++
5 files changed, 42 insertions(+)

diff --git a/arch/x86/include/asm/page.h b/arch/x86/include/asm/page.h
index 7555b48803a8..ca0aa379ac7f 100644
--- a/arch/x86/include/asm/page.h
+++ b/arch/x86/include/asm/page.h
@@ -28,6 +28,12 @@ static inline void clear_user_page(void *page, unsigned long vaddr,
clear_page(page);
}

+static inline void clear_user_page_uncached(void *page, unsigned long vaddr,
+ struct page *pg)
+{
+ clear_page_uncached(page);
+}
+
static inline void copy_user_page(void *to, void *from, unsigned long vaddr,
struct page *topage)
{
diff --git a/arch/x86/include/asm/page_32.h b/arch/x86/include/asm/page_32.h
index 94dbd51df58f..7a03a274a9a4 100644
--- a/arch/x86/include/asm/page_32.h
+++ b/arch/x86/include/asm/page_32.h
@@ -39,6 +39,15 @@ static inline void clear_page(void *page)
memset(page, 0, PAGE_SIZE);
}

+static inline void clear_page_uncached(void *page)
+{
+ clear_page(page);
+}
+
+static inline void clear_page_uncached_flush(void)
+{
+}
+
static inline void copy_page(void *to, void *from)
{
memcpy(to, from, PAGE_SIZE);
diff --git a/arch/x86/include/asm/page_64.h b/arch/x86/include/asm/page_64.h
index bde3c2785ec4..5897075e77dd 100644
--- a/arch/x86/include/asm/page_64.h
+++ b/arch/x86/include/asm/page_64.h
@@ -55,6 +55,20 @@ static inline void clear_page(void *page)
: "cc", "memory", "rax", "rcx");
}

+static inline void clear_page_uncached(void *page)
+{
+ alternative_call(clear_page,
+ clear_page_nt, X86_FEATURE_NT_GOOD,
+ "=D" (page),
+ "0" (page)
+ : "cc", "memory", "rax", "rcx");
+}
+
+static inline void clear_page_uncached_flush(void)
+{
+ alternative("", "sfence", X86_FEATURE_NT_GOOD);
+}
+
void copy_page(void *to, void *from);

#endif /* !__ASSEMBLY__ */
diff --git a/include/asm-generic/page.h b/include/asm-generic/page.h
index fe801f01625e..60235a0cf24a 100644
--- a/include/asm-generic/page.h
+++ b/include/asm-generic/page.h
@@ -26,6 +26,9 @@
#ifndef __ASSEMBLY__

#define clear_page(page) memset((page), 0, PAGE_SIZE)
+#define clear_page_uncached(page) clear_page(page)
+#define clear_page_uncached_flush() do { } while (0)
+
#define copy_page(to,from) memcpy((to), (from), PAGE_SIZE)

#define clear_user_page(page, vaddr, pg) clear_page(page)
diff --git a/include/linux/highmem.h b/include/linux/highmem.h
index 14e6202ce47f..f842593e2474 100644
--- a/include/linux/highmem.h
+++ b/include/linux/highmem.h
@@ -232,6 +232,16 @@ static inline void clear_user_highpage(struct page *page, unsigned long vaddr)
}
#endif

+#ifndef clear_user_highpage_uncached
+static inline void clear_user_highpage_uncached(struct page *page, unsigned long vaddr)
+{
+ void *addr = kmap_atomic(page);
+
+ clear_user_page_uncached(addr, vaddr, page);
+ kunmap_atomic(addr);
+}
+#endif
+
#ifndef __HAVE_ARCH_ALLOC_ZEROED_USER_HIGHPAGE
/**
* __alloc_zeroed_user_highpage - Allocate a zeroed HIGHMEM page for a VMA with caller-specified movable GFP flags
--
2.9.3