2024-01-29 18:09:18

by Ard Biesheuvel

[permalink] [raw]
Subject: [PATCH v3 14/19] x86/coco: Make cc_set_mask() static inline

From: Ard Biesheuvel <[email protected]>

Setting the cc_mask global variable may be done early in the boot while
running fromm a 1:1 translation. This code is built with -fPIC in order
to support this.

Make cc_set_mask() static inline so it can execute safely in this
context as well.

Signed-off-by: Ard Biesheuvel <[email protected]>
---
arch/x86/coco/core.c | 7 +------
arch/x86/include/asm/coco.h | 8 +++++++-
2 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/arch/x86/coco/core.c b/arch/x86/coco/core.c
index eeec9986570e..d07be9d05cd0 100644
--- a/arch/x86/coco/core.c
+++ b/arch/x86/coco/core.c
@@ -14,7 +14,7 @@
#include <asm/processor.h>

enum cc_vendor cc_vendor __ro_after_init = CC_VENDOR_NONE;
-static u64 cc_mask __ro_after_init;
+u64 cc_mask __ro_after_init;

static bool noinstr intel_cc_platform_has(enum cc_attr attr)
{
@@ -148,8 +148,3 @@ u64 cc_mkdec(u64 val)
}
}
EXPORT_SYMBOL_GPL(cc_mkdec);
-
-__init void cc_set_mask(u64 mask)
-{
- cc_mask = mask;
-}
diff --git a/arch/x86/include/asm/coco.h b/arch/x86/include/asm/coco.h
index 6ae2d16a7613..ecc29d6136ad 100644
--- a/arch/x86/include/asm/coco.h
+++ b/arch/x86/include/asm/coco.h
@@ -13,7 +13,13 @@ enum cc_vendor {
extern enum cc_vendor cc_vendor;

#ifdef CONFIG_ARCH_HAS_CC_PLATFORM
-void cc_set_mask(u64 mask);
+static inline void cc_set_mask(u64 mask)
+{
+ extern u64 cc_mask;
+
+ cc_mask = mask;
+}
+
u64 cc_mkenc(u64 val);
u64 cc_mkdec(u64 val);
#else
--
2.43.0.429.g432eaa2c6b-goog



2024-01-30 23:20:16

by Kevin Loughlin

[permalink] [raw]
Subject: Re: [PATCH v3 14/19] x86/coco: Make cc_set_mask() static inline

On Mon, Jan 29, 2024 at 10:06 AM Ard Biesheuvel <[email protected]> wrote:
>
> From: Ard Biesheuvel <[email protected]>
>
> Setting the cc_mask global variable may be done early in the boot while
> running fromm a 1:1 translation. This code is built with -fPIC in order
> to support this.
>
> Make cc_set_mask() static inline so it can execute safely in this
> context as well.
>
> Signed-off-by: Ard Biesheuvel <[email protected]>
> ---
> arch/x86/coco/core.c | 7 +------
> arch/x86/include/asm/coco.h | 8 +++++++-
> 2 files changed, 8 insertions(+), 7 deletions(-)
>
> diff --git a/arch/x86/coco/core.c b/arch/x86/coco/core.c
> index eeec9986570e..d07be9d05cd0 100644
> --- a/arch/x86/coco/core.c
> +++ b/arch/x86/coco/core.c
> @@ -14,7 +14,7 @@
> #include <asm/processor.h>
>
> enum cc_vendor cc_vendor __ro_after_init = CC_VENDOR_NONE;
> -static u64 cc_mask __ro_after_init;
> +u64 cc_mask __ro_after_init;
>
> static bool noinstr intel_cc_platform_has(enum cc_attr attr)
> {
> @@ -148,8 +148,3 @@ u64 cc_mkdec(u64 val)
> }
> }
> EXPORT_SYMBOL_GPL(cc_mkdec);
> -
> -__init void cc_set_mask(u64 mask)
> -{
> - cc_mask = mask;
> -}
> diff --git a/arch/x86/include/asm/coco.h b/arch/x86/include/asm/coco.h
> index 6ae2d16a7613..ecc29d6136ad 100644
> --- a/arch/x86/include/asm/coco.h
> +++ b/arch/x86/include/asm/coco.h
> @@ -13,7 +13,13 @@ enum cc_vendor {
> extern enum cc_vendor cc_vendor;
>
> #ifdef CONFIG_ARCH_HAS_CC_PLATFORM
> -void cc_set_mask(u64 mask);
> +static inline void cc_set_mask(u64 mask)

In the inline functions I changed/added to core.c in [0], I saw an
objtool warning on clang builds when using inline instead of
__always_inline; I did not see the same warning for gcc . Should we
similarly use __always_inline to strictly-enforce here?

[0] https://lore.kernel.org/lkml/[email protected]/#Z31arch:x86:coco:core.c

2024-01-30 23:36:38

by Ard Biesheuvel

[permalink] [raw]
Subject: Re: [PATCH v3 14/19] x86/coco: Make cc_set_mask() static inline

On Wed, 31 Jan 2024 at 00:16, Kevin Loughlin <[email protected]> wrote:
>
> On Mon, Jan 29, 2024 at 10:06 AM Ard Biesheuvel <[email protected]> wrote:
> >
> > From: Ard Biesheuvel <[email protected]>
> >
> > Setting the cc_mask global variable may be done early in the boot while
> > running fromm a 1:1 translation. This code is built with -fPIC in order
> > to support this.
> >
> > Make cc_set_mask() static inline so it can execute safely in this
> > context as well.
> >
> > Signed-off-by: Ard Biesheuvel <[email protected]>
> > ---
> > arch/x86/coco/core.c | 7 +------
> > arch/x86/include/asm/coco.h | 8 +++++++-
> > 2 files changed, 8 insertions(+), 7 deletions(-)
> >
> > diff --git a/arch/x86/coco/core.c b/arch/x86/coco/core.c
> > index eeec9986570e..d07be9d05cd0 100644
> > --- a/arch/x86/coco/core.c
> > +++ b/arch/x86/coco/core.c
> > @@ -14,7 +14,7 @@
> > #include <asm/processor.h>
> >
> > enum cc_vendor cc_vendor __ro_after_init = CC_VENDOR_NONE;
> > -static u64 cc_mask __ro_after_init;
> > +u64 cc_mask __ro_after_init;
> >
> > static bool noinstr intel_cc_platform_has(enum cc_attr attr)
> > {
> > @@ -148,8 +148,3 @@ u64 cc_mkdec(u64 val)
> > }
> > }
> > EXPORT_SYMBOL_GPL(cc_mkdec);
> > -
> > -__init void cc_set_mask(u64 mask)
> > -{
> > - cc_mask = mask;
> > -}
> > diff --git a/arch/x86/include/asm/coco.h b/arch/x86/include/asm/coco.h
> > index 6ae2d16a7613..ecc29d6136ad 100644
> > --- a/arch/x86/include/asm/coco.h
> > +++ b/arch/x86/include/asm/coco.h
> > @@ -13,7 +13,13 @@ enum cc_vendor {
> > extern enum cc_vendor cc_vendor;
> >
> > #ifdef CONFIG_ARCH_HAS_CC_PLATFORM
> > -void cc_set_mask(u64 mask);
> > +static inline void cc_set_mask(u64 mask)
>
> In the inline functions I changed/added to core.c in [0], I saw an
> objtool warning on clang builds when using inline instead of
> __always_inline; I did not see the same warning for gcc . Should we
> similarly use __always_inline to strictly-enforce here?
>
> [0] https://lore.kernel.org/lkml/[email protected]/#Z31arch:x86:coco:core.c

This assembles to a single instruction

movq %rsi, cc_mask(%rip)

and the definition is in a header file, so I'm not convinced it makes
a different.

And looking at your series, I think there is no need to modify coco.c
at all if you just take this patch instead: the other code in that
file should not be called early at all (unless our downstream has
substantial changes there)