mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] x86/pkeys: Fix pkey_alloc return value when pkeys are not supported
@ 2026-07-16 22:06 Bijan Tabatabai
  2026-07-16 22:37 ` Dave Hansen
  2026-08-13 17:24 ` [tip: x86/mm] x86/pkeys: Fix pkey_alloc() " tip-bot2 for Bijan Tabatabai
  0 siblings, 2 replies; 3+ messages in thread
From: Bijan Tabatabai @ 2026-07-16 22:06 UTC (permalink / raw)
  To: tglx, dave.hansen, mgorman, linux-kernel
  Cc: mingo, bp, x86, hpa, sgsu.park, akpm, Bijan Tabatabai

From: Bijan Tabatabai <btabatabai@wisc.edu>

The man page for pkey_alloc(2) specifies that it should return -1 with
the errno set to ENOSPC when pkeys are not supported [1]. However, on
x86 pkey_alloc() sets errno to EINVAL when called for the first time
on a CPU that does not support pkeys.

The root cause of this is the x86 implementation of mm_pkey_alloc() not
directly checking if pkeys are supported. It only checks if all the
pkeys have been allocated by comparing the allocation map against
all_pkeys_mask. When OSPKE is not enabled, init_new_context() skips the
initialization of the allocation map, leaving it as 0, while
all_pkeys_mask is 1. mm_pkey_alloc() interprets this as there being a
pkey available and it returns pkey 0. Then, pkey_alloc() fails with
-EINVAL from arch_set_user_pkey_access() instead of returning -ENOSPC.
Subsequent calls to pkey_alloc() do return -ENOSPC because pkey 0 is
left marked as allocated.

Change mm_pkey_alloc() to directly check if OSPKE is enabled, and
return -1 if it is not, which causes pkey_alloc() to return -ENOSPC. The
arm64 and powerpc implementations of mm_pkey_alloc() already do this
check.

[1] https://man7.org/linux/man-pages/man2/pkey_alloc.2.html

Fixes: e8c24d3a23a4 ("x86/pkeys: Allocation/free syscalls")
Signed-off-by: Bijan Tabatabai <btabatabai@wisc.edu>
---
 arch/x86/include/asm/pkeys.h | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/arch/x86/include/asm/pkeys.h b/arch/x86/include/asm/pkeys.h
index 06ed2cd2592e..fbb15f6666f1 100644
--- a/arch/x86/include/asm/pkeys.h
+++ b/arch/x86/include/asm/pkeys.h
@@ -88,6 +88,9 @@ int mm_pkey_alloc(struct mm_struct *mm)
 	u16 all_pkeys_mask = ((1U << arch_max_pkey()) - 1);
 	int ret;
 
+	if (!cpu_feature_enabled(X86_FEATURE_OSPKE))
+		return -1;
+
 	/*
 	 * Are we out of pkeys?  We must handle this specially
 	 * because ffz() behavior is undefined if there are no
-- 
2.34.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] x86/pkeys: Fix pkey_alloc return value when pkeys are not supported
  2026-07-16 22:06 [PATCH] x86/pkeys: Fix pkey_alloc return value when pkeys are not supported Bijan Tabatabai
@ 2026-07-16 22:37 ` Dave Hansen
  2026-08-13 17:24 ` [tip: x86/mm] x86/pkeys: Fix pkey_alloc() " tip-bot2 for Bijan Tabatabai
  1 sibling, 0 replies; 3+ messages in thread
From: Dave Hansen @ 2026-07-16 22:37 UTC (permalink / raw)
  To: Bijan Tabatabai, tglx, dave.hansen, mgorman, linux-kernel
  Cc: mingo, bp, x86, hpa, sgsu.park, akpm, Bijan Tabatabai

On 7/16/26 15:06, Bijan Tabatabai wrote:
> Change mm_pkey_alloc() to directly check if OSPKE is enabled, and
> return -1 if it is not, which causes pkey_alloc() to return -ENOSPC. The
> arm64 and powerpc implementations of mm_pkey_alloc() already do this
> check.

Thanks for the patch! I made one tweak which was to use
arch_pkeys_enabled() instead of the cpu_feature_enabled() check. That
makes the code darn close to a perfect copy of the ARM code at least.

It would be really nice if someone could go consolidate all the copying
and pasting between the architectures there some day.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [tip: x86/mm] x86/pkeys: Fix pkey_alloc() return value when pkeys are not supported
  2026-07-16 22:06 [PATCH] x86/pkeys: Fix pkey_alloc return value when pkeys are not supported Bijan Tabatabai
  2026-07-16 22:37 ` Dave Hansen
@ 2026-08-13 17:24 ` tip-bot2 for Bijan Tabatabai
  1 sibling, 0 replies; 3+ messages in thread
From: tip-bot2 for Bijan Tabatabai @ 2026-08-13 17:24 UTC (permalink / raw)
  To: linux-tip-commits; +Cc: Bijan Tabatabai, Dave Hansen, x86, linux-kernel

The following commit has been merged into the x86/mm branch of tip:

Commit-ID:     dee87e09b0dd63da9b1e1876167ccae37842dfd0
Gitweb:        https://git.kernel.org/tip/dee87e09b0dd63da9b1e1876167ccae37842dfd0
Author:        Bijan Tabatabai <btabatabai@wisc.edu>
AuthorDate:    Thu, 16 Jul 2026 17:06:04 -05:00
Committer:     Dave Hansen <dave.hansen@linux.intel.com>
CommitterDate: Thu, 13 Aug 2026 10:18:58 -07:00

x86/pkeys: Fix pkey_alloc() return value when pkeys are not supported

The man page for pkey_alloc(2) specifies that it should return -1 with
the errno set to ENOSPC when pkeys are not supported [1]. However, on
x86 pkey_alloc() sets errno to EINVAL when called for the first time
on a CPU that does not support pkeys.

The root cause of this is the x86 implementation of mm_pkey_alloc() not
directly checking if pkeys are supported. It only checks if all the
pkeys have been allocated by comparing the allocation map against
all_pkeys_mask. When OSPKE is not enabled, init_new_context() skips the
initialization of the allocation map, leaving it as 0, while
all_pkeys_mask is 1. mm_pkey_alloc() interprets this as there being a
pkey available and it returns pkey 0. Then, pkey_alloc() fails with
-EINVAL from arch_set_user_pkey_access() instead of returning -ENOSPC.
Subsequent calls to pkey_alloc() do return -ENOSPC because pkey 0 is
left marked as allocated.

Change mm_pkey_alloc() to directly check if OSPKE is enabled, and
return -1 if it is not, which causes pkey_alloc() to return -ENOSPC. The
arm64 and powerpc implementations of mm_pkey_alloc() already do this
check.

[1] https://man7.org/linux/man-pages/man2/pkey_alloc.2.html

[ dhansen: use arch_pkeys_enabled() to follow arm ]

Fixes: e8c24d3a23a4 ("x86/pkeys: Allocation/free syscalls")
Signed-off-by: Bijan Tabatabai <btabatabai@wisc.edu>
Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
Link: https://patch.msgid.link/20260716220604.26452-1-bijan311@gmail.com
---
 arch/x86/include/asm/pkeys.h | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/arch/x86/include/asm/pkeys.h b/arch/x86/include/asm/pkeys.h
index 06ed2cd..fcec521 100644
--- a/arch/x86/include/asm/pkeys.h
+++ b/arch/x86/include/asm/pkeys.h
@@ -88,6 +88,9 @@ int mm_pkey_alloc(struct mm_struct *mm)
 	u16 all_pkeys_mask = ((1U << arch_max_pkey()) - 1);
 	int ret;
 
+	if (!arch_pkeys_enabled())
+		return -1;
+
 	/*
 	 * Are we out of pkeys?  We must handle this specially
 	 * because ffz() behavior is undefined if there are no

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-08-13 17:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-07-16 22:06 [PATCH] x86/pkeys: Fix pkey_alloc return value when pkeys are not supported Bijan Tabatabai
2026-07-16 22:37 ` Dave Hansen
2026-08-13 17:24 ` [tip: x86/mm] x86/pkeys: Fix pkey_alloc() " tip-bot2 for Bijan Tabatabai

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®