Skip to contentSkip to conversation

x86: Fix ARCH_REQ_XCOMP_PERM and update the test

PATCH Superseded Chang S. Bae 1 participant 3 messages Nov 10, 2021

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover x86: Fix ARCH_REQ_XCOMP_PERM and update the test

Fixed the authorship from V2:

    https://lore.kernel.org/lkml/[email protected]/

---

The recent x86 dynamic state support incorporates the arch_prctl option to request permission before using a dynamic state.

It was designed to add the requested feature in the group leader's permission bitmask so that every thread can reference this master bitmask. The group leader is assumed to be unchanged here. The mainline is the case as a group leader is identified at fork() or exec() time only.

This master bitmask should include non-dynamic features always, as they are permitted by default. Users may check them via ARCH_GET_XCOMP_PERM.

But, in hindsight, the implementation does overwrite the bitmask with the requested bit only, instead of adding the bit to the existing one. This overwrite effectively revokes the permission that is granted already.

Fix the code and also update the selftest to disclose the issue if there is.

Cc Yang Zhong <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Dave Hansen <[email protected]>

Chang S. Bae (1):
selftests/x86/amx: Update the ARCH_REQ_XCOMP_PERM test

Yang Zhong (1):
x86/arch_prctl: Fix the ARCH_REQ_XCOMP_PERM implementation

 arch/x86/kernel/fpu/xstate.c      |  2 +-
 tools/testing/selftests/x86/amx.c | 16 ++++++++++++++--
 2 files changed, 15 insertions(+), 3 deletions(-)

base-commit: 9a6cf455a952725422f4fb10848839989f833579

Signature · 1 line
2.17.1
#1 lore raw

PATCH v3 1/2 x86/arch_prctl: Fix the ARCH_REQ_XCOMP_PERM implementation

From: Yang Zhong <[email protected]>

ARCH_REQ_XCOMP_PERM is supposed to add the requested feature to the permission bitmap of thread_group_leader()->fpu. But the code overwrites the bitmap with the requested feature bit only rather than adding it.

Fix the code to add the request feature bit to the master bitmask.

Fixes db8268df0983 ("x86/arch_prctl: Add controls for dynamic XSTATE components")
Cc Thomas Gleixner <[email protected]>

---
Changes from v2:
* Fix the authorship.

Changes from v1:
* Change the mask value only and trim the changelog.

---
 arch/x86/kernel/fpu/xstate.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/fpu/xstate.c +1 -1
index d28829403ed0..fc1ab0116f4e 100644
@@ -1626,7 +1626,7 @@ static int __xstate_request_perm(u64 permitted, u64 requested)
 		return ret;
 
 	/* Pairs with the READ_ONCE() in xstate_get_group_perm() */
-	WRITE_ONCE(fpu->perm.__state_perm, requested);
+	WRITE_ONCE(fpu->perm.__state_perm, mask);
 	/* Protected by sighand lock */
 	fpu->perm.__state_size = ksize;
 	fpu->perm.__user_state_size = usize;
Signature · 1 line
2.17.1
#2 lore raw

PATCH v3 2/2 selftests/x86/amx: Update the ARCH_REQ_XCOMP_PERM test

In reply to Chang S. Bae

Update the arch_prctl() test to check the permission bitmap whether the requested feature is added as expected or not.

Every non-dynamic feature that is enabled is permitted already for use. TILECFG is not dynamic feature. Ensure the bit is always on from ARCH_GET_XCOMP_PERM.

---
 tools/testing/selftests/x86/amx.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
tools/testing/selftests/x86/amx.c +14 -2
index 3615ef4a48bb..e1e2c8f3356f 100644
@@ -368,9 +368,16 @@ static void req_xtiledata_perm(void)
 
 static void validate_req_xcomp_perm(enum expected_result exp)
 {
-	unsigned long bitmask;
+	unsigned long bitmask, expected_bitmask;
 	long rc;
 
+	rc = syscall(SYS_arch_prctl, ARCH_GET_XCOMP_PERM, &bitmask);
+	if (rc) {
+		fatal_error("prctl(ARCH_GET_XCOMP_PERM) error: %ld", rc);
+	} else if (!(bitmask & XFEATURE_MASK_XTILECFG)) {
+		fatal_error("ARCH_GET_XCOMP_PERM returns XFEATURE_XTILECFG off.");
+	}
+
 	rc = syscall(SYS_arch_prctl, ARCH_REQ_XCOMP_PERM, XFEATURE_XTILEDATA);
 	if (exp == FAIL_EXPECTED) {
 		if (rc) {
@@ -383,10 +390,15 @@ static void validate_req_xcomp_perm(enum expected_result exp)
 		fatal_error("ARCH_REQ_XCOMP_PERM saw unexpected failure.\n");
 	}
 
+	expected_bitmask = bitmask | XFEATURE_MASK_XTILEDATA;
+
 	rc = syscall(SYS_arch_prctl, ARCH_GET_XCOMP_PERM, &bitmask);
 	if (rc) {
 		fatal_error("prctl(ARCH_GET_XCOMP_PERM) error: %ld", rc);
-	} else if (bitmask & XFEATURE_MASK_XTILE) {
+	} else if (bitmask != expected_bitmask) {
+		fatal_error("ARCH_REQ_XCOMP_PERM saw a wrong bitmask: %lx, expected: %lx.\n",
+			    bitmask, expected_bitmask);
+	} else {
 		printf("\tARCH_REQ_XCOMP_PERM is successful.\n");
 	}
 }
Signature · 1 line
2.17.1