[RFC PATCH 14/47] mm: asi: Disable ASI API when ASI is not enabled for a process

Junaid Shahid
2022-02-23T05:25:43Z

If ASI is not enabled for a process, then asi_init() will return a NULL
ASI pointer as output, though it will return a 0 error code. All other
ASI API functions will return without an error when they get a NULL ASI
pointer.

Signed-off-by: Junaid Shahid <junaids@google.com>

---
arch/x86/include/asm/asi.h | 2 +-
arch/x86/mm/asi.c | 18 ++++++++++--------
include/asm-generic/asi.h | 7 ++++++-
3 files changed, 17 insertions(+), 10 deletions(-)

diff --git a/arch/x86/include/asm/asi.h b/arch/x86/include/asm/asi.h
index 64c2b4d1dba2..f69e1f2f09a4 100644
--- a/arch/x86/include/asm/asi.h
+++ b/arch/x86/include/asm/asi.h
@@ -51,7 +51,7 @@ int asi_register_class(const char *name, uint flags,
const struct asi_hooks *ops);
void asi_unregister_class(int index);

-int asi_init(struct mm_struct *mm, int asi_index);
+int asi_init(struct mm_struct *mm, int asi_index, struct asi **out_asi);
void asi_destroy(struct asi *asi);

void asi_enter(struct asi *asi);
diff --git a/arch/x86/mm/asi.c b/arch/x86/mm/asi.c
index ca50a32ecd7e..58d1c532274a 100644
--- a/arch/x86/mm/asi.c
+++ b/arch/x86/mm/asi.c
@@ -207,11 +207,13 @@ static int __init asi_global_init(void)
}
subsys_initcall(asi_global_init)

-int asi_init(struct mm_struct *mm, int asi_index)
+int asi_init(struct mm_struct *mm, int asi_index, struct asi **out_asi)
{
struct asi *asi = &mm->asi[asi_index];

- if (!boot_cpu_has(X86_FEATURE_ASI))
+ *out_asi = NULL;
+
+ if (!boot_cpu_has(X86_FEATURE_ASI) || !mm->asi_enabled)
return 0;

/* Index 0 is reserved for special purposes. */
@@ -238,13 +240,15 @@ int asi_init(struct mm_struct *mm, int asi_index)
set_pgd(asi->pgd + i, asi_global_nonsensitive_pgd[i]);
}

+ *out_asi = asi;
+
return 0;
}
EXPORT_SYMBOL_GPL(asi_init);

void asi_destroy(struct asi *asi)
{
- if (!boot_cpu_has(X86_FEATURE_ASI))
+ if (!boot_cpu_has(X86_FEATURE_ASI) || !asi)
return;

asi_free_pgd(asi);
@@ -278,11 +282,9 @@ void __asi_enter(void)

void asi_enter(struct asi *asi)
{
- if (!static_cpu_has(X86_FEATURE_ASI))
+ if (!static_cpu_has(X86_FEATURE_ASI) || !asi)
return;

- VM_WARN_ON_ONCE(!asi);
-
this_cpu_write(asi_cpu_state.target_asi, asi);
barrier();

@@ -423,7 +425,7 @@ int asi_map_gfp(struct asi *asi, void *addr, size_t len, gfp_t gfp_flags)
size_t end = start + len;
size_t page_size;

- if (!static_cpu_has(X86_FEATURE_ASI))
+ if (!static_cpu_has(X86_FEATURE_ASI) || !asi)
return 0;

VM_BUG_ON(start & ~PAGE_MASK);
@@ -514,7 +516,7 @@ void asi_unmap(struct asi *asi, void *addr, size_t len, bool flush_tlb)
size_t end = start + len;
pgtbl_mod_mask mask = 0;

- if (!static_cpu_has(X86_FEATURE_ASI) || !len)
+ if (!static_cpu_has(X86_FEATURE_ASI) || !asi || !len)
return;

VM_BUG_ON(start & ~PAGE_MASK);
diff --git a/include/asm-generic/asi.h b/include/asm-generic/asi.h
index f918cd052722..51c9c4a488e8 100644
--- a/include/asm-generic/asi.h
+++ b/include/asm-generic/asi.h
@@ -33,7 +33,12 @@ static inline void asi_unregister_class(int asi_index) { }

static inline void asi_init_mm_state(struct mm_struct *mm) { }

-static inline int asi_init(struct mm_struct *mm, int asi_index) { return 0; }
+static inline
+int asi_init(struct mm_struct *mm, int asi_index, struct asi **out_asi)
+{
+ *out_asi = NULL;
+ return 0;
+}

static inline void asi_destroy(struct asi *asi) { }

--
2.35.1.473.g83b2b277ed-goog