[RFC PATCH, x86]: Disable CPA cache flush for selfsnoop targets

From: Uros Bizjak
Date: Thu Jul 11 2019 - 04:13:09 EST


Recent patch [1] disabled a self-snoop feature on a list of processor
models with a known errata, so we are confident that the feature
should work on remaining models also for other purposes than to speed
up MTRR programming.

I would like to resurrect an old patch [2] that avoids calling clflush
and wbinvd
to invalidate caches when CPU supports selfsnoop.

The patch was ported to latest Fedora kernel (5.1.16) and tested with
CONFIG_CPA_DEBUG on INTEL_FAM6_IVYBRIDGE_X. The relevant ports of
dmesg show:

...
< hundreds of CPA protect messages, resulting from set_memory_rw CPA
undo test in mm/init_64.c >
CPA protect Rodata RO: 0xffffffffbd1fe000 - 0xffffffffbd1fefff PFN
1461fe req 8000000000000063 prevent 0000000000000002
CPA protect Rodata RO: 0xffff889c461fe000 - 0xffff889c461fefff PFN
1461fe req 8000000000000063 prevent 0000000000000002
Testing CPA: again
Freeing unused kernel image memory: 2016K
Freeing unused kernel image memory: 4K
x86/mm: Checked W+X mappings: passed, no W+X pages found.
rodata_test: all tests were successful
x86/mm: Checking user space page tables
x86/mm: Checked W+X mappings: passed, no W+X pages found.

and from CPA selftest:

CPA self-test:
4k 36352 large 4021 gb 0 x 81[ffff889b00098000-ffff889bdf7ff000] miss 133120
4k 180224 large 3740 gb 0 x 81[ffff889b00098000-ffff889bdf7ff000] miss 133120
4k 180224 large 3740 gb 0 x 81[ffff889b00098000-ffff889bdf7ff000] miss 133120
ok.

[1] https://lkml.org/lkml/2019/6/27/828
[2] https://lkml.org/lkml/2009/4/8/508

Uros.
From 17cbad5d45b6ae26dbcfe02deba597c83cd63a0d Mon Sep 17 00:00:00 2001
From: Uros Bizjak <ubizjak@xxxxxxxxx>
Date: Wed, 10 Jul 2019 15:01:44 +0200
Subject: [PATCH] Disable CPA cache flush for selfsnoop targets

Signed-off-by: Uros Bizjak <ubizjak@xxxxxxxxx>
---
arch/x86/mm/pageattr.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/arch/x86/mm/pageattr.c b/arch/x86/mm/pageattr.c
index 6a9a77a403c9..8893ef5f70cd 100644
--- a/arch/x86/mm/pageattr.c
+++ b/arch/x86/mm/pageattr.c
@@ -1725,10 +1725,11 @@ static int change_page_attr_set_clr(unsigned long *addr, int numpages,
goto out;

/*
- * No need to flush, when we did not set any of the caching
- * attributes:
+ * No need to flush when CPU supports self snoop or
+ * when we did not set any of the caching attributes:
*/
- cache = !!pgprot2cachemode(mask_set);
+ cache = !static_cpu_has(X86_FEATURE_SELFSNOOP) &&
+ pgprot2cachemode(mask_set);

/*
* On error; flush everything to be sure.
--
2.21.0