[PATCH V8 31/44] mm/pkeys: Add pks_available()

ira.weiny
2022-01-27T17:57:32Z

From: Ira Weiny <ira.weiny@intel.com>

The PKS code calls will not fail if they are called and a CPU does not
support the PKS feature. There will be no protection but the API is
safe to call. However, adding the overhead of these calls on CPUs which
don't support PKS is inefficient

Define pks_available() to allow users to check if PKS is enabled on the
current system. If not they can chose to optimize around the PKS calls.

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

---
Changes for V8
s/pks_enabled/pks_available
---
Documentation/core-api/protection-keys.rst | 3 +++
arch/x86/mm/pkeys.c | 10 ++++++++++
include/linux/pkeys.h | 6 ++++++
3 files changed, 19 insertions(+)

diff --git a/Documentation/core-api/protection-keys.rst b/Documentation/core-api/protection-keys.rst
index 267efa2112e7..27c9701d4aeb 100644
--- a/Documentation/core-api/protection-keys.rst
+++ b/Documentation/core-api/protection-keys.rst
@@ -151,6 +151,9 @@ Changing permissions of individual keys
.. kernel-doc:: arch/x86/mm/pkeys.c
:identifiers: pks_update_exception

+.. kernel-doc:: arch/x86/mm/pkeys.c
+ :identifiers: pks_available
+
Overriding Default Fault Behavior
---------------------------------

diff --git a/arch/x86/mm/pkeys.c b/arch/x86/mm/pkeys.c
index f30ac8215785..fa71037c1dd0 100644
--- a/arch/x86/mm/pkeys.c
+++ b/arch/x86/mm/pkeys.c
@@ -418,6 +418,16 @@ static void __pks_update_protection(int pkey, u32 protection)
pks_write_pkrs(current->thread.pks_saved_pkrs);
}

+/**
+ * pks_available() - Is PKS available on this system
+ *
+ * Return if PKS is currently supported and enabled on this system.
+ */
+bool pks_available(void)
+{
+ return cpu_feature_enabled(X86_FEATURE_PKS);
+}
+
/*
* Do not call this directly, see pks_mk*().
*
diff --git a/include/linux/pkeys.h b/include/linux/pkeys.h
index a53e4f2c41af..ec5463c373a1 100644
--- a/include/linux/pkeys.h
+++ b/include/linux/pkeys.h
@@ -55,6 +55,7 @@ static inline bool arch_pkeys_enabled(void)

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

+bool pks_available(void);
void pks_update_protection(int pkey, u32 protection);
void pks_update_exception(struct pt_regs *regs, int pkey, u32 protection);

@@ -87,6 +88,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_mk_noaccess(int pkey) {}
static inline void pks_mk_readwrite(int pkey) {}
static inline void pks_update_exception(struct pt_regs *regs,
--
2.31.1