[PATCH V8 04/44] x86/pkeys: Add additional PKEY helper macros

From: ira . weiny
Date: Thu Jan 27 2022 - 12:55:43 EST


From: Ira Weiny <ira.weiny@xxxxxxxxx>

Avoid open coding shift and mask operations by defining and using helper
macros for PKey operations.

Suggested-by: Dan Williams <dan.j.williams@xxxxxxxxx>
Signed-off-by: Ira Weiny <ira.weiny@xxxxxxxxx>

---
Changes for V8
Move ahead of other patches.
Simplify to only the macros used in the series
---
arch/x86/include/asm/pkeys_common.h | 5 ++++-
arch/x86/include/asm/pkru.h | 8 ++------
2 files changed, 6 insertions(+), 7 deletions(-)

diff --git a/arch/x86/include/asm/pkeys_common.h b/arch/x86/include/asm/pkeys_common.h
index 08c736669244..d02ab5bc3fff 100644
--- a/arch/x86/include/asm/pkeys_common.h
+++ b/arch/x86/include/asm/pkeys_common.h
@@ -6,6 +6,9 @@
#define PKR_WD_BIT 0x2u
#define PKR_BITS_PER_PKEY 2

-#define PKR_AD_KEY(pkey) (PKR_AD_BIT << ((pkey) * PKR_BITS_PER_PKEY))
+#define PKR_PKEY_SHIFT(pkey) (pkey * PKR_BITS_PER_PKEY)
+
+#define PKR_AD_KEY(pkey) (PKR_AD_BIT << PKR_PKEY_SHIFT(pkey))
+#define PKR_WD_KEY(pkey) (PKR_WD_BIT << PKR_PKEY_SHIFT(pkey))

#endif /*_ASM_X86_PKEYS_COMMON_H */
diff --git a/arch/x86/include/asm/pkru.h b/arch/x86/include/asm/pkru.h
index 06980dd42946..81ddf88ac3c9 100644
--- a/arch/x86/include/asm/pkru.h
+++ b/arch/x86/include/asm/pkru.h
@@ -15,17 +15,13 @@ extern u32 init_pkru_value;

static inline bool __pkru_allows_read(u32 pkru, u16 pkey)
{
- int pkru_pkey_bits = pkey * PKR_BITS_PER_PKEY;
-
- return !(pkru & (PKR_AD_BIT << pkru_pkey_bits));
+ return !(pkru & PKR_AD_KEY(pkey));
}

static inline bool __pkru_allows_write(u32 pkru, u16 pkey)
{
- int pkru_pkey_bits = pkey * PKR_BITS_PER_PKEY;
-
/* Access-disable disables writes too so check both bits here. */
- return !(pkru & ((PKR_AD_BIT|PKR_WD_BIT) << pkru_pkey_bits));
+ return !(pkru & (PKR_AD_KEY(pkey) | PKR_WD_KEY(pkey)));
}

static inline u32 read_pkru(void)
--
2.31.1