2022-10-04 08:32:17

by Jürgen Groß

[permalink] [raw]
Subject: [PATCH v4 03/16] x86/mtrr: replace use_intel() with a local flag

In MTRR code use_intel() is only used in one source file, and the
relevant use_intel_if member of struct mtrr_ops is set only in
generic_mtrr_ops.

Replace use_intel() with a single flag in cacheinfo.c, which can be set
when assigning generic_mtrr_ops to mtrr_if. This allows to drop
use_intel_if from mtrr_ops, while preparing to support PAT without
MTRR. As another preparation for the PAT/MTRR decoupling use a bit for
MTRR control and one for PAT control. For now set both bits together,
this can be changed later.

As the new flag will be set only if mtrr_enabled is set, the test for
mtrr_enabled can be dropped at some places.

At the same time drop the local mtrr_enabled() function and rename
the __mtrr_enabled flag to mtrr_enabled.

Signed-off-by: Juergen Gross <[email protected]>
---
V2:
- new patch
V4:
- rename cache_generic to memory_caching_control (Borislav Petkov)
- rename CACHE_GENERIC_* to CACHE_* (Borislav Petkov)
- get rid of use_generic in mtrr_bp_init() (Borislav Petkov)
---
arch/x86/include/asm/cacheinfo.h | 5 +++
arch/x86/kernel/cpu/cacheinfo.c | 3 ++
arch/x86/kernel/cpu/mtrr/generic.c | 1 -
arch/x86/kernel/cpu/mtrr/mtrr.c | 55 +++++++++++++-----------------
arch/x86/kernel/cpu/mtrr/mtrr.h | 2 --
5 files changed, 32 insertions(+), 34 deletions(-)

diff --git a/arch/x86/include/asm/cacheinfo.h b/arch/x86/include/asm/cacheinfo.h
index 86b2e0dcc4bf..c3873962a7cd 100644
--- a/arch/x86/include/asm/cacheinfo.h
+++ b/arch/x86/include/asm/cacheinfo.h
@@ -2,6 +2,11 @@
#ifndef _ASM_X86_CACHEINFO_H
#define _ASM_X86_CACHEINFO_H

+/* Kernel controls MTRR and/or PAT MSRs. */
+extern unsigned int memory_caching_control;
+#define CACHE_MTRR 0x01
+#define CACHE_PAT 0x02
+
void cacheinfo_amd_init_llc_id(struct cpuinfo_x86 *c, int cpu);
void cacheinfo_hygon_init_llc_id(struct cpuinfo_x86 *c, int cpu);

diff --git a/arch/x86/kernel/cpu/cacheinfo.c b/arch/x86/kernel/cpu/cacheinfo.c
index 66556833d7af..5228fb9a3798 100644
--- a/arch/x86/kernel/cpu/cacheinfo.c
+++ b/arch/x86/kernel/cpu/cacheinfo.c
@@ -35,6 +35,9 @@ DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_llc_shared_map);
/* Shared L2 cache maps */
DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_l2c_shared_map);

+/* Kernel controls MTRR and/or PAT MSRs. */
+unsigned int memory_caching_control __ro_after_init = 0;
+
struct _cache_table {
unsigned char descriptor;
char cache_type;
diff --git a/arch/x86/kernel/cpu/mtrr/generic.c b/arch/x86/kernel/cpu/mtrr/generic.c
index cd64eab02393..81742870ecc5 100644
--- a/arch/x86/kernel/cpu/mtrr/generic.c
+++ b/arch/x86/kernel/cpu/mtrr/generic.c
@@ -917,7 +917,6 @@ int positive_have_wrcomb(void)
* Generic structure...
*/
const struct mtrr_ops generic_mtrr_ops = {
- .use_intel_if = 1,
.set_all = generic_set_all,
.get = generic_get_mtrr,
.get_free_region = generic_get_free_region,
diff --git a/arch/x86/kernel/cpu/mtrr/mtrr.c b/arch/x86/kernel/cpu/mtrr/mtrr.c
index 2746cac9d8a9..dacb537da126 100644
--- a/arch/x86/kernel/cpu/mtrr/mtrr.c
+++ b/arch/x86/kernel/cpu/mtrr/mtrr.c
@@ -46,6 +46,7 @@
#include <linux/syscore_ops.h>
#include <linux/rcupdate.h>

+#include <asm/cacheinfo.h>
#include <asm/cpufeature.h>
#include <asm/e820/api.h>
#include <asm/mtrr.h>
@@ -58,12 +59,7 @@
#define MTRR_TO_PHYS_WC_OFFSET 1000

u32 num_var_ranges;
-static bool __mtrr_enabled;
-
-static bool mtrr_enabled(void)
-{
- return __mtrr_enabled;
-}
+static bool mtrr_enabled;

unsigned int mtrr_usage_table[MTRR_MAX_VAR_RANGES];
static DEFINE_MUTEX(mtrr_mutex);
@@ -119,11 +115,11 @@ static int have_wrcomb(void)
}

/* This function returns the number of variable MTRRs */
-static void __init set_num_var_ranges(void)
+static void __init set_num_var_ranges(bool use_generic)
{
unsigned long config = 0, dummy;

- if (use_intel())
+ if (use_generic)
rdmsr(MSR_MTRRcap, config, dummy);
else if (is_cpu(AMD) || is_cpu(HYGON))
config = 2;
@@ -303,7 +299,7 @@ int mtrr_add_page(unsigned long base, unsigned long size,
int i, replace, error;
mtrr_type ltype;

- if (!mtrr_enabled())
+ if (!mtrr_enabled)
return -ENXIO;

error = mtrr_if->validate_add_page(base, size, type);
@@ -451,7 +447,7 @@ static int mtrr_check(unsigned long base, unsigned long size)
int mtrr_add(unsigned long base, unsigned long size, unsigned int type,
bool increment)
{
- if (!mtrr_enabled())
+ if (!mtrr_enabled)
return -ENODEV;
if (mtrr_check(base, size))
return -EINVAL;
@@ -480,7 +476,7 @@ int mtrr_del_page(int reg, unsigned long base, unsigned long size)
unsigned long lbase, lsize;
int error = -EINVAL;

- if (!mtrr_enabled())
+ if (!mtrr_enabled)
return -ENODEV;

max = num_var_ranges;
@@ -540,7 +536,7 @@ int mtrr_del_page(int reg, unsigned long base, unsigned long size)
*/
int mtrr_del(int reg, unsigned long base, unsigned long size)
{
- if (!mtrr_enabled())
+ if (!mtrr_enabled)
return -ENODEV;
if (mtrr_check(base, size))
return -EINVAL;
@@ -566,7 +562,7 @@ int arch_phys_wc_add(unsigned long base, unsigned long size)
{
int ret;

- if (pat_enabled() || !mtrr_enabled())
+ if (pat_enabled() || !mtrr_enabled)
return 0; /* Success! (We don't need to do anything.) */

ret = mtrr_add(base, size, MTRR_TYPE_WRCOMB, true);
@@ -755,15 +751,17 @@ void __init mtrr_bp_init(void)
}

if (mtrr_if) {
- __mtrr_enabled = true;
- set_num_var_ranges();
+ mtrr_enabled = true;
+ set_num_var_ranges(mtrr_if == &generic_mtrr_ops);
init_table();
- if (use_intel()) {
+ if (mtrr_if == &generic_mtrr_ops) {
/* BIOS may override */
- __mtrr_enabled = get_mtrr_state();
+ mtrr_enabled = get_mtrr_state();

- if (mtrr_enabled())
+ if (mtrr_enabled) {
mtrr_bp_pat_init();
+ memory_caching_control |= CACHE_MTRR | CACHE_PAT;
+ }

if (mtrr_cleanup(phys_addr)) {
changed_by_mtrr_cleanup = 1;
@@ -772,7 +770,7 @@ void __init mtrr_bp_init(void)
}
}

- if (!mtrr_enabled()) {
+ if (!mtrr_enabled) {
pr_info("Disabled\n");

/*
@@ -786,10 +784,7 @@ void __init mtrr_bp_init(void)

void mtrr_ap_init(void)
{
- if (!mtrr_enabled())
- return;
-
- if (!use_intel() || mtrr_aps_delayed_init)
+ if (!memory_caching_control || mtrr_aps_delayed_init)
return;

/*
@@ -816,7 +811,7 @@ void mtrr_save_state(void)
{
int first_cpu;

- if (!mtrr_enabled())
+ if (!mtrr_enabled)
return;

first_cpu = cpumask_first(cpu_online_mask);
@@ -825,9 +820,7 @@ void mtrr_save_state(void)

void set_mtrr_aps_delayed_init(void)
{
- if (!mtrr_enabled())
- return;
- if (!use_intel())
+ if (!memory_caching_control)
return;

mtrr_aps_delayed_init = true;
@@ -838,7 +831,7 @@ void set_mtrr_aps_delayed_init(void)
*/
void mtrr_aps_init(void)
{
- if (!use_intel() || !mtrr_enabled())
+ if (!memory_caching_control)
return;

/*
@@ -855,7 +848,7 @@ void mtrr_aps_init(void)

void mtrr_bp_restore(void)
{
- if (!use_intel() || !mtrr_enabled())
+ if (!memory_caching_control)
return;

mtrr_if->set_all();
@@ -863,10 +856,10 @@ void mtrr_bp_restore(void)

static int __init mtrr_init_finialize(void)
{
- if (!mtrr_enabled())
+ if (!mtrr_enabled)
return 0;

- if (use_intel()) {
+ if (memory_caching_control & CACHE_MTRR) {
if (!changed_by_mtrr_cleanup)
mtrr_state_warn();
return 0;
diff --git a/arch/x86/kernel/cpu/mtrr/mtrr.h b/arch/x86/kernel/cpu/mtrr/mtrr.h
index 2ac99e561181..88b1c4b6174a 100644
--- a/arch/x86/kernel/cpu/mtrr/mtrr.h
+++ b/arch/x86/kernel/cpu/mtrr/mtrr.h
@@ -14,7 +14,6 @@ extern unsigned int mtrr_usage_table[MTRR_MAX_VAR_RANGES];

struct mtrr_ops {
u32 vendor;
- u32 use_intel_if;
void (*set)(unsigned int reg, unsigned long base,
unsigned long size, mtrr_type type);
void (*set_all)(void);
@@ -61,7 +60,6 @@ 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 == 1)

extern unsigned int num_var_ranges;
extern u64 mtrr_tom2;
--
2.35.3


2022-10-21 17:29:29

by Borislav Petkov

[permalink] [raw]
Subject: Re: [PATCH v4 03/16] x86/mtrr: replace use_intel() with a local flag

On Tue, Oct 04, 2022 at 10:10:10AM +0200, Juergen Gross wrote:
> In MTRR code use_intel() is only used in one source file, and the
> relevant use_intel_if member of struct mtrr_ops is set only in
> generic_mtrr_ops.
>
> Replace use_intel() with a single flag in cacheinfo.c, which can be set
> when assigning generic_mtrr_ops to mtrr_if. This allows to drop
> use_intel_if from mtrr_ops, while preparing to support PAT without
> MTRR. As another preparation for the PAT/MTRR decoupling use a bit for
> MTRR control and one for PAT control. For now set both bits together,
> this can be changed later.
>
> As the new flag will be set only if mtrr_enabled is set, the test for
> mtrr_enabled can be dropped at some places.
>
> At the same time drop the local mtrr_enabled() function and rename
> the __mtrr_enabled flag to mtrr_enabled.

So, I kinda like your idea about "replace the bool with mtrr_if != NULL"
to test whether MTRRs are enabled.

IOW, how does this look ontop of yours?

At the end of mtrr_bp_init() I need to do some careful dancing but I
think this way it is even clearer. I'm pretty sure it can be simplified
even more but one thing at a time...

Thx.

---
diff --git a/arch/x86/kernel/cpu/mtrr/mtrr.c b/arch/x86/kernel/cpu/mtrr/mtrr.c
index dacb537da126..927b463a22bd 100644
--- a/arch/x86/kernel/cpu/mtrr/mtrr.c
+++ b/arch/x86/kernel/cpu/mtrr/mtrr.c
@@ -59,7 +59,6 @@
#define MTRR_TO_PHYS_WC_OFFSET 1000

u32 num_var_ranges;
-static bool mtrr_enabled;

unsigned int mtrr_usage_table[MTRR_MAX_VAR_RANGES];
static DEFINE_MUTEX(mtrr_mutex);
@@ -71,15 +70,17 @@ static const struct mtrr_ops *mtrr_ops[X86_VENDOR_NUM] __ro_after_init;

const struct mtrr_ops *mtrr_if;

-static void set_mtrr(unsigned int reg, unsigned long base,
- unsigned long size, mtrr_type type);
-
void __init set_mtrr_ops(const struct mtrr_ops *ops)
{
if (ops->vendor && ops->vendor < X86_VENDOR_NUM)
mtrr_ops[ops->vendor] = ops;
}

+static bool mtrr_enabled(void)
+{
+ return !!mtrr_if;
+}
+
/* Returns non-zero if we have the write-combining memory type */
static int have_wrcomb(void)
{
@@ -299,7 +300,7 @@ int mtrr_add_page(unsigned long base, unsigned long size,
int i, replace, error;
mtrr_type ltype;

- if (!mtrr_enabled)
+ if (!mtrr_enabled())
return -ENXIO;

error = mtrr_if->validate_add_page(base, size, type);
@@ -447,7 +448,7 @@ static int mtrr_check(unsigned long base, unsigned long size)
int mtrr_add(unsigned long base, unsigned long size, unsigned int type,
bool increment)
{
- if (!mtrr_enabled)
+ if (!mtrr_enabled())
return -ENODEV;
if (mtrr_check(base, size))
return -EINVAL;
@@ -476,7 +477,7 @@ int mtrr_del_page(int reg, unsigned long base, unsigned long size)
unsigned long lbase, lsize;
int error = -EINVAL;

- if (!mtrr_enabled)
+ if (!mtrr_enabled())
return -ENODEV;

max = num_var_ranges;
@@ -536,7 +537,7 @@ int mtrr_del_page(int reg, unsigned long base, unsigned long size)
*/
int mtrr_del(int reg, unsigned long base, unsigned long size)
{
- if (!mtrr_enabled)
+ if (!mtrr_enabled())
return -ENODEV;
if (mtrr_check(base, size))
return -EINVAL;
@@ -562,7 +563,7 @@ int arch_phys_wc_add(unsigned long base, unsigned long size)
{
int ret;

- if (pat_enabled() || !mtrr_enabled)
+ if (pat_enabled() || !mtrr_enabled())
return 0; /* Success! (We don't need to do anything.) */

ret = mtrr_add(base, size, MTRR_TYPE_WRCOMB, true);
@@ -750,15 +751,18 @@ void __init mtrr_bp_init(void)
}
}

- if (mtrr_if) {
- mtrr_enabled = true;
- set_num_var_ranges(mtrr_if == &generic_mtrr_ops);
+ if (mtrr_enabled()) {
+ bool use_generic = mtrr_if == &generic_mtrr_ops;
+ bool mtrr_state_enabled = true;
+
+ set_num_var_ranges(use_generic);
init_table();
- if (mtrr_if == &generic_mtrr_ops) {
+
+ if (use_generic) {
/* BIOS may override */
- mtrr_enabled = get_mtrr_state();
+ mtrr_state_enabled = get_mtrr_state();

- if (mtrr_enabled) {
+ if (mtrr_state_enabled) {
mtrr_bp_pat_init();
memory_caching_control |= CACHE_MTRR | CACHE_PAT;
}
@@ -767,10 +771,13 @@ void __init mtrr_bp_init(void)
changed_by_mtrr_cleanup = 1;
mtrr_if->set_all();
}
+
+ if (!mtrr_state_enabled)
+ mtrr_if = NULL;
}
}

- if (!mtrr_enabled) {
+ if (!mtrr_enabled()) {
pr_info("Disabled\n");

/*
@@ -811,7 +818,7 @@ void mtrr_save_state(void)
{
int first_cpu;

- if (!mtrr_enabled)
+ if (!mtrr_enabled())
return;

first_cpu = cpumask_first(cpu_online_mask);
@@ -856,7 +863,7 @@ void mtrr_bp_restore(void)

static int __init mtrr_init_finialize(void)
{
- if (!mtrr_enabled)
+ if (!mtrr_enabled())
return 0;

if (memory_caching_control & CACHE_MTRR) {

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette

2022-10-21 18:53:23

by Borislav Petkov

[permalink] [raw]
Subject: Re: [PATCH v4 03/16] x86/mtrr: replace use_intel() with a local flag

On Fri, Oct 21, 2022 at 08:05:39PM +0200, Juergen Gross wrote:
> Please have a look at patch 10 (I've put it after the rework of
> mtrr_bp_init() exactly in order to avoid the dance you had to do).

/me goes and looks.

Yeah,

if (mtrr_enabled())

seems more readable to me than

if (mtrr_if)

but ok, changing that is easy.

Thx.

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette

2022-10-21 18:57:06

by Jürgen Groß

[permalink] [raw]
Subject: Re: [PATCH v4 03/16] x86/mtrr: replace use_intel() with a local flag

On 21.10.22 19:19, Borislav Petkov wrote:
> On Tue, Oct 04, 2022 at 10:10:10AM +0200, Juergen Gross wrote:
>> In MTRR code use_intel() is only used in one source file, and the
>> relevant use_intel_if member of struct mtrr_ops is set only in
>> generic_mtrr_ops.
>>
>> Replace use_intel() with a single flag in cacheinfo.c, which can be set
>> when assigning generic_mtrr_ops to mtrr_if. This allows to drop
>> use_intel_if from mtrr_ops, while preparing to support PAT without
>> MTRR. As another preparation for the PAT/MTRR decoupling use a bit for
>> MTRR control and one for PAT control. For now set both bits together,
>> this can be changed later.
>>
>> As the new flag will be set only if mtrr_enabled is set, the test for
>> mtrr_enabled can be dropped at some places.
>>
>> At the same time drop the local mtrr_enabled() function and rename
>> the __mtrr_enabled flag to mtrr_enabled.
>
> So, I kinda like your idea about "replace the bool with mtrr_if != NULL"
> to test whether MTRRs are enabled.
>
> IOW, how does this look ontop of yours?
>
> At the end of mtrr_bp_init() I need to do some careful dancing but I
> think this way it is even clearer. I'm pretty sure it can be simplified
> even more but one thing at a time...

Please have a look at patch 10 (I've put it after the rework of mtrr_bp_init()
exactly in order to avoid the dance you had to do).


Juergen

>
> Thx.
>
> ---
> diff --git a/arch/x86/kernel/cpu/mtrr/mtrr.c b/arch/x86/kernel/cpu/mtrr/mtrr.c
> index dacb537da126..927b463a22bd 100644
> --- a/arch/x86/kernel/cpu/mtrr/mtrr.c
> +++ b/arch/x86/kernel/cpu/mtrr/mtrr.c
> @@ -59,7 +59,6 @@
> #define MTRR_TO_PHYS_WC_OFFSET 1000
>
> u32 num_var_ranges;
> -static bool mtrr_enabled;
>
> unsigned int mtrr_usage_table[MTRR_MAX_VAR_RANGES];
> static DEFINE_MUTEX(mtrr_mutex);
> @@ -71,15 +70,17 @@ static const struct mtrr_ops *mtrr_ops[X86_VENDOR_NUM] __ro_after_init;
>
> const struct mtrr_ops *mtrr_if;
>
> -static void set_mtrr(unsigned int reg, unsigned long base,
> - unsigned long size, mtrr_type type);
> -
> void __init set_mtrr_ops(const struct mtrr_ops *ops)
> {
> if (ops->vendor && ops->vendor < X86_VENDOR_NUM)
> mtrr_ops[ops->vendor] = ops;
> }
>
> +static bool mtrr_enabled(void)
> +{
> + return !!mtrr_if;
> +}
> +
> /* Returns non-zero if we have the write-combining memory type */
> static int have_wrcomb(void)
> {
> @@ -299,7 +300,7 @@ int mtrr_add_page(unsigned long base, unsigned long size,
> int i, replace, error;
> mtrr_type ltype;
>
> - if (!mtrr_enabled)
> + if (!mtrr_enabled())
> return -ENXIO;
>
> error = mtrr_if->validate_add_page(base, size, type);
> @@ -447,7 +448,7 @@ static int mtrr_check(unsigned long base, unsigned long size)
> int mtrr_add(unsigned long base, unsigned long size, unsigned int type,
> bool increment)
> {
> - if (!mtrr_enabled)
> + if (!mtrr_enabled())
> return -ENODEV;
> if (mtrr_check(base, size))
> return -EINVAL;
> @@ -476,7 +477,7 @@ int mtrr_del_page(int reg, unsigned long base, unsigned long size)
> unsigned long lbase, lsize;
> int error = -EINVAL;
>
> - if (!mtrr_enabled)
> + if (!mtrr_enabled())
> return -ENODEV;
>
> max = num_var_ranges;
> @@ -536,7 +537,7 @@ int mtrr_del_page(int reg, unsigned long base, unsigned long size)
> */
> int mtrr_del(int reg, unsigned long base, unsigned long size)
> {
> - if (!mtrr_enabled)
> + if (!mtrr_enabled())
> return -ENODEV;
> if (mtrr_check(base, size))
> return -EINVAL;
> @@ -562,7 +563,7 @@ int arch_phys_wc_add(unsigned long base, unsigned long size)
> {
> int ret;
>
> - if (pat_enabled() || !mtrr_enabled)
> + if (pat_enabled() || !mtrr_enabled())
> return 0; /* Success! (We don't need to do anything.) */
>
> ret = mtrr_add(base, size, MTRR_TYPE_WRCOMB, true);
> @@ -750,15 +751,18 @@ void __init mtrr_bp_init(void)
> }
> }
>
> - if (mtrr_if) {
> - mtrr_enabled = true;
> - set_num_var_ranges(mtrr_if == &generic_mtrr_ops);
> + if (mtrr_enabled()) {
> + bool use_generic = mtrr_if == &generic_mtrr_ops;
> + bool mtrr_state_enabled = true;
> +
> + set_num_var_ranges(use_generic);
> init_table();
> - if (mtrr_if == &generic_mtrr_ops) {
> +
> + if (use_generic) {
> /* BIOS may override */
> - mtrr_enabled = get_mtrr_state();
> + mtrr_state_enabled = get_mtrr_state();
>
> - if (mtrr_enabled) {
> + if (mtrr_state_enabled) {
> mtrr_bp_pat_init();
> memory_caching_control |= CACHE_MTRR | CACHE_PAT;
> }
> @@ -767,10 +771,13 @@ void __init mtrr_bp_init(void)
> changed_by_mtrr_cleanup = 1;
> mtrr_if->set_all();
> }
> +
> + if (!mtrr_state_enabled)
> + mtrr_if = NULL;
> }
> }
>
> - if (!mtrr_enabled) {
> + if (!mtrr_enabled()) {
> pr_info("Disabled\n");
>
> /*
> @@ -811,7 +818,7 @@ void mtrr_save_state(void)
> {
> int first_cpu;
>
> - if (!mtrr_enabled)
> + if (!mtrr_enabled())
> return;
>
> first_cpu = cpumask_first(cpu_online_mask);
> @@ -856,7 +863,7 @@ void mtrr_bp_restore(void)
>
> static int __init mtrr_init_finialize(void)
> {
> - if (!mtrr_enabled)
> + if (!mtrr_enabled())
> return 0;
>
> if (memory_caching_control & CACHE_MTRR) {
>


Attachments:
OpenPGP_0xB0DE9DD628BF132F.asc (3.08 kB)
OpenPGP public key
OpenPGP_signature (505.00 B)
OpenPGP digital signature
Download all attachments