Skip to contentSkip to conversation

x86/MTRR: avoid several indirect calls

PATCH In review Jan Beulich 3 participants 4 messages Jan 17, 2024 – Apr 23, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/MTRR: avoid several indirect calls

The use of (supposedly) vendor-specific hooks is a relic from the days when Xen was still possible to build as 32-bit binary. There's no expectation that a new need for such an abstraction would arise. Convert mttr_if to a mere boolean and all prior calls through it to direct ones, thus allowing to eliminate 6 ENDBR from .text.

---
Strictly speaking mtrr_if could go apparently away as well, for effectively mirroring cpu_has_mtrr now (once mtrr_bp_init() has run). Replacing the variable uses would be further code churn, though.

xen/arch/x86/cpu/mtrr/generic.c +7 -19
@@ -287,7 +287,7 @@ static void set_fixed_range(int msr, boo
 	}
 }
 
-int cf_check generic_get_free_region(
+int mtrr_get_free_region(
     unsigned long base, unsigned long size, int replace_reg)
 /*  [SUMMARY] Get a free MTRR.
     <base> The starting (base) address of the region.
@@ -303,14 +303,14 @@ int cf_check generic_get_free_region(
 	if (replace_reg >= 0 && replace_reg < max)
 		return replace_reg;
 	for (i = 0; i < max; ++i) {
-		mtrr_if->get(i, &lbase, &lsize, &ltype);
+		mtrr_get(i, &lbase, &lsize, &ltype);
 		if (lsize == 0)
 			return i;
 	}
 	return -ENOSPC;
 }
 
-static void cf_check generic_get_mtrr(
+void mtrr_get(
     unsigned int reg, unsigned long *base, unsigned long *size, mtrr_type *type)
 {
 	uint64_t _mask, _base;
@@ -500,7 +500,7 @@ static void post_set(bool pge)
 	spin_unlock(&set_atomicity_lock);
 }
 
-static void cf_check generic_set_all(void)
+void mtrr_set_all(void)
 {
 	unsigned long mask, count;
 	unsigned long flags;
@@ -523,7 +523,7 @@ static void cf_check generic_set_all(voi
 	}
 }
 
-static void cf_check generic_set_mtrr(
+void mtrr_set(
     unsigned int reg, unsigned long base, unsigned long size, mtrr_type type)
 /*  [SUMMARY] Set variable MTRR register on the local CPU.
     <reg> The register to set.
@@ -567,7 +567,7 @@ static void cf_check generic_set_mtrr(
 	local_irq_restore(flags);
 }
 
-int cf_check generic_validate_add_page(
+int mtrr_validate_add_page(
     unsigned long base, unsigned long size, unsigned int type)
 {
 	unsigned long lbase, last;
@@ -586,21 +586,9 @@ int cf_check generic_validate_add_page(
 }
 
 
-static int cf_check generic_have_wrcomb(void)
+bool mtrr_have_wrcomb(void)
 {
 	unsigned long config;
 	rdmsrl(MSR_MTRRcap, config);
 	return (config & (1ULL << 10));
 }
-
-/* generic structure...
- */
-const struct mtrr_ops generic_mtrr_ops = {
-	.use_intel_if      = true,
-	.set_all	   = generic_set_all,
-	.get               = generic_get_mtrr,
-	.get_free_region   = generic_get_free_region,
-	.set               = generic_set_mtrr,
-	.validate_add_page = generic_validate_add_page,
-	.have_wrcomb       = generic_have_wrcomb,
-};
xen/arch/x86/cpu/mtrr/main.c +22 -42

176 lines not on this page · raw message

xen/arch/x86/cpu/mtrr/mtrr.h +9 -28
@@ -6,40 +6,21 @@
 #define MTRR_CHANGE_MASK_VARIABLE  0x02
 #define MTRR_CHANGE_MASK_DEFTYPE   0x04
 
-
-struct mtrr_ops {
-	u32	vendor;
-	bool	use_intel_if;
-//	void	(*init)(void);
-	void	(*set)(unsigned int reg, unsigned long base,
-		       unsigned long size, mtrr_type type);
-	void	(*set_all)(void);
-
-	void	(*get)(unsigned int reg, unsigned long *base,
-		       unsigned long *size, mtrr_type * type);
-	int	(*get_free_region)(unsigned long base, unsigned long size,
-				   int replace_reg);
-	int	(*validate_add_page)(unsigned long base, unsigned long size,
-				     unsigned int type);
-	int	(*have_wrcomb)(void);
-};
-
-int cf_check generic_get_free_region(
+void mtrr_get(
+    unsigned int reg, unsigned long *base, unsigned long *size,
+    mtrr_type *type);
+void mtrr_set(
+    unsigned int reg, unsigned long base, unsigned long size, mtrr_type type);
+void mtrr_set_all(void);
+int mtrr_get_free_region(
     unsigned long base, unsigned long size, int replace_reg);
-int cf_check generic_validate_add_page(
+int mtrr_validate_add_page(
     unsigned long base, unsigned long size, unsigned int type);
-
-extern const struct mtrr_ops generic_mtrr_ops;
+bool mtrr_have_wrcomb(void);
 
 void get_mtrr_state(void);
 
-extern void set_mtrr_ops(const struct mtrr_ops *);
-
 extern u64 size_or_mask, size_and_mask;
-extern const struct mtrr_ops *mtrr_if;
-
-#define is_cpu(vnd)	(mtrr_if && mtrr_if->vendor == X86_VENDOR_##vnd)
-#define use_intel()	(mtrr_if && mtrr_if->use_intel_if)
 
 extern unsigned int num_var_ranges;
 
xen/arch/x86/platform_hypercall.c +1 -1
@@ -295,7 +295,7 @@ ret_t do_platform_op(
         ret = -EINVAL;
         if ( op->u.read_memtype.reg < num_var_ranges )
         {
-            mtrr_if->get(op->u.read_memtype.reg, &mfn, &nr_mfns, &type);
+            mtrr_get(op->u.read_memtype.reg, &mfn, &nr_mfns, &type);
             op->u.read_memtype.mfn     = mfn;
             op->u.read_memtype.nr_mfns = nr_mfns;
             op->u.read_memtype.type    = type;
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Jan Beulich

The use of (supposedly) vendor-specific hooks is a relic from the days when Xen was still possible to build as 32-bit binary. There's no expectation that a new need for such an abstraction would arise. Convert mttr_if to a mere boolean and all prior calls through it to direct ones, thus allowing to eliminate 6 ENDBR from .text.

Quoted 4 lines

---
Strictly speaking mtrr_if could go apparently away as well, for effectively mirroring cpu_has_mtrr now (once mtrr_bp_init() has run). Replacing the variable uses would be further code churn, though.

There's loads more cleanup I'd like to do, but that doesn't invalidate this as an improvement.

Bloat-o-meter reports:

    add/remove: 7/8 grow/shrink: 1/14 up/down: 1354/-1803 (-449)

for this change, which definitely welcome.  That said ...

xen/arch/x86/cpu/mtrr/generic.c int cf_check generic_validate_add_page( · line 594

Quoted 13 lines
xen/arch/x86/cpu/mtrr/generic.c +1 -1
@@ -586,21 +586,9 @@ int cf_check generic_validate_add_page(
 }
 
 
-static int cf_check generic_have_wrcomb(void)
+bool mtrr_have_wrcomb(void)
 {
 	unsigned long config;
 	rdmsrl(MSR_MTRRcap, config);
 	return (config & (1ULL << 10));
 }

... I do have half a mind to fix this as a followon.  We really don't want to be re-reading the caps MSR for every call to mtrr_add_page().

~Andrew

#2 lore raw
In reply to Jan Beulich

xen/arch/x86/cpu/mtrr/main.c int mtrr_add_page(unsigned long base, un · line 319

Quoted 8 lines · Jan Beulich
xen/arch/x86/cpu/mtrr/main.c +1 -1
@@ -328,7 +316,7 @@ int mtrr_add_page(unsigned long base, un
 	}
 
 	/*  If the type is WC, check that this processor supports it  */
-	if ((type == X86_MT_WC) && !have_wrcomb()) {
+	if ((type == X86_MT_WC) && mtrr_have_wrcomb()) {

Is reversing the condition intentional here?

Quoted 3 lines
xen/arch/x86/cpu/mtrr/main.c +0 -0
 		printk(KERN_WARNING
 		       "mtrr: your processor doesn't support write-combining\n");
 		return -EOPNOTSUPP;
Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
#3 lore raw
Quoted 11 lines · Marek Marczykowski-Górecki

xen/arch/x86/cpu/mtrr/main.c int mtrr_add_page(unsigned long base, un · line 319

Quoted 8 lines · Jan Beulich
xen/arch/x86/cpu/mtrr/main.c +1 -1
@@ -328,7 +316,7 @@ int mtrr_add_page(unsigned long base, un
 	}
 
 	/*  If the type is WC, check that this processor supports it  */
-	if ((type == X86_MT_WC) && !have_wrcomb()) {
+	if ((type == X86_MT_WC) && mtrr_have_wrcomb()) {

Is reversing the condition intentional here?

Clearly not. Patch sent.

Jan