Skip to contentSkip to conversation

x86/asm: inline constant inputs in rdpkru(), wrpkru()

PATCH In review Alexey Dobriyan 2 participants 3 messages Mar 6, 2025 – Mar 7, 2025 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 x86/asm: inline constant inputs in rdpkru(), wrpkru()

Put immediate values directly into registers deleting dummy variables.

---
 arch/x86/include/asm/special_insns.h | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
arch/x86/include/asm/special_insns.h +2 -5
index 21ce480658b1..494a1aa19f05 100644
@@ -79,7 +79,6 @@ void native_write_cr4(unsigned long val);
 #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
 static inline u32 rdpkru(void)
 {
-	u32 ecx = 0;
 	u32 edx, pkru;
 
 	/*
@@ -88,20 +87,18 @@ static inline u32 rdpkru(void)
 	 */
 	asm volatile(".byte 0x0f,0x01,0xee\n\t"
 		     : "=a" (pkru), "=d" (edx)
-		     : "c" (ecx));
+		     : "c" (0));
 	return pkru;
 }
 
 static inline void wrpkru(u32 pkru)
 {
-	u32 ecx = 0, edx = 0;
-
 	/*
 	 * "wrpkru" instruction.  Loads contents in EAX to PKRU,
 	 * requires that ecx = edx = 0.
 	 */
 	asm volatile(".byte 0x0f,0x01,0xef\n\t"
-		     : : "a" (pkru), "c"(ecx), "d"(edx));
+		     : : "a" (pkru), "c" (0), "d" (0));
 }
 
 #else
Signature · 1 line
2.45.3
#1 lore raw

arch/x86/include/asm/special_insns.h static inline u32 rdpkru(void) · line 102

Quoted 12 lines · Alexey Dobriyan

static inline void wrpkru(u32 pkru)
{

arch/x86/include/asm/special_insns.h +1 -3
-	u32 ecx = 0, edx = 0;
-
 	/*
 	 * "wrpkru" instruction.  Loads contents in EAX to PKRU,
 	 * requires that ecx = edx = 0.
 	 */
 	asm volatile(".byte 0x0f,0x01,0xef\n\t"
-		     : : "a" (pkru), "c"(ecx), "d"(edx));
+		     : : "a" (pkru), "c" (0), "d" (0));
 }

Hey Alexey,

I appreciate the patch. But I do like how it's written currently. I honestly kinda wish it went even further and did:

	u32 eax = pkru;
#2 lore raw
Quoted 16 lines · Dave Hansen

arch/x86/include/asm/special_insns.h static inline u32 rdpkru(void) · line 102

Quoted 12 lines · Alexey Dobriyan

static inline void wrpkru(u32 pkru)
{

arch/x86/include/asm/special_insns.h +1 -3
-	u32 ecx = 0, edx = 0;
-
 	/*
 	 * "wrpkru" instruction.  Loads contents in EAX to PKRU,
 	 * requires that ecx = edx = 0.
 	 */
 	asm volatile(".byte 0x0f,0x01,0xef\n\t"
-		     : : "a" (pkru), "c"(ecx), "d"(edx));
+		     : : "a" (pkru), "c" (0), "d" (0));
 }

Hey Alexey,

Quoted 4 lines

I appreciate the patch. But I do like how it's written currently. I honestly kinda wish it went even further and did:

	u32 eax = pkru;

I _think_ you can write "eax" (pkru) .

I don't like the comment either. It just reiterates the SDM. Trailing \n\t too -- they don't do anything in one-liners.

The function is basically:

	// wrpkru
	asm volatile (
		".byte 0x0f,0x01,0xef"
		:
		: "a" (pkru), "d" (0) , "c" (0)
	);