[PATCH V9 33/45] mm/pkeys: Add pks_available()

From: ira . weiny
Date: Thu Mar 10 2022 - 12:24:15 EST


From: Ira Weiny <ira.weiny@xxxxxxxxx>

If PKS is configured within the kernel but the CPU does not support PKS,
the PKS calls remain safe to execute even without protection. However,
adding the overhead of these calls on CPUs which don't support PKS is
inefficient and best avoided.

Define pks_available() to allow users to check if PKS is enabled on the
current system.

The implementation of pks_available() is placed in the asm headers while
being directly exported via linux/pks.h to allow for the inline calling
of cpu_feature_enabled() by consumers outside of the architecture.

Signed-off-by: Ira Weiny <ira.weiny@xxxxxxxxx>

---
Changes for V9
Driven by a request by Dan Williams to make this static inline
Place this in pks.h to avoid header conflicts while
allowing for an optimized call to cpu_feature_enabled()

Changes for V8
s/pks_enabled/pks_available
---
Documentation/core-api/protection-keys.rst | 3 +++
arch/x86/include/asm/pks.h | 12 ++++++++++++
include/linux/pks.h | 8 ++++++++
3 files changed, 23 insertions(+)

diff --git a/Documentation/core-api/protection-keys.rst b/Documentation/core-api/protection-keys.rst
index 68fe7a92cc98..36621cbc2cc6 100644
--- a/Documentation/core-api/protection-keys.rst
+++ b/Documentation/core-api/protection-keys.rst
@@ -152,6 +152,9 @@ Changing permissions of individual keys
.. kernel-doc:: arch/x86/mm/pkeys.c
:identifiers: pks_update_exception

+.. kernel-doc:: arch/x86/include/asm/pks.h
+ :identifiers: pks_available
+
Overriding Default Fault Behavior
---------------------------------

diff --git a/arch/x86/include/asm/pks.h b/arch/x86/include/asm/pks.h
index de67d5b5a2af..cab42aadea07 100644
--- a/arch/x86/include/asm/pks.h
+++ b/arch/x86/include/asm/pks.h
@@ -2,8 +2,20 @@
#ifndef _ASM_X86_PKS_H
#define _ASM_X86_PKS_H

+#include <asm/cpufeature.h>
+
#ifdef CONFIG_ARCH_ENABLE_SUPERVISOR_PKEYS

+/**
+ * pks_available() - Is PKS available on this system
+ *
+ * Return if PKS is currently supported and enabled on this system.
+ */
+static inline bool pks_available(void)
+{
+ return cpu_feature_enabled(X86_FEATURE_PKS);
+}
+
void pks_setup(void);
void x86_pkrs_load(struct thread_struct *thread);
void pks_save_pt_regs(struct pt_regs *regs);
diff --git a/include/linux/pks.h b/include/linux/pks.h
index 45156f358776..163c75992a8a 100644
--- a/include/linux/pks.h
+++ b/include/linux/pks.h
@@ -8,6 +8,9 @@

#include <uapi/asm-generic/mman-common.h>

+#include <asm/pks.h>
+
+bool pks_available(void);
void pks_update_protection(u8 pkey, u8 protection);
void pks_update_exception(struct pt_regs *regs, u8 pkey, u8 protection);

@@ -40,6 +43,11 @@ typedef bool (*pks_key_callback)(struct pt_regs *regs, unsigned long address,

#else /* !CONFIG_ARCH_ENABLE_SUPERVISOR_PKEYS */

+static inline bool pks_available(void)
+{
+ return false;
+}
+
static inline void pks_set_noaccess(u8 pkey) {}
static inline void pks_set_readwrite(u8 pkey) {}
static inline void pks_update_exception(struct pt_regs *regs,
--
2.35.1