From nobody Tue Sep 29 02:01:32 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8EC5E4A2E17; Thu, 13 Aug 2026 17:24:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786641857; cv=none; b=AnWRBJoe09z2xGE2igznvx8lcFpWpM8RgCuUBfzsffILV8lApDuDvToyIp6WIFKiB1izXr+K+yMVq+506LDgHSROEfBCkHhHoxmaZQ3FByo/NbRReHCLN+mbJe3hDXt5q+2ButVMjry+Iyd6P/TQZEhiVYboPfsfN42D5epHWH0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786641857; c=relaxed/simple; bh=HscFoo3+GH3MNO6EgtEDtyveC78zE2PI3N7+0zxEjNQ=; h=Date:From:To:Subject:Cc:In-Reply-To:References:MIME-Version: Message-ID:Content-Type; b=r/IE6S5pFFLpQGLR7/OTPyuTRgq7SMaI3+WqFZhoOPl2GMEctRG2ULbd22WRbTen4u21LGqZgifeTtKkiaG0oIyJJEko8v8V/tRmf+vzd0ZkXXDt7vNooKzoB8asOdb2TRQXd869gpa2GouAmbNposCHDDcmLqlnfGliQ/hRQrs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=TP8kMv/L; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=I1byF8pG; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="TP8kMv/L"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="I1byF8pG" Date: Thu, 13 Aug 2026 17:24:11 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1786641853; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=FLNHn3yfjx3jTkqvErT8Tf8SOwtVZG0HeLOiq2otzZ0=; b=TP8kMv/LpzHM97LSFy9yA+aHOQZhmPQaOU2zRMtGqQNkU0+ZhEbVIwvBvaJhgUQ+mEx2Q2 xNui7jjJI8lHB4XG0lENTmjkFsVu1hfX0wCF5l62Sez2tqgVEWjnHdkB2/wG/hVhO5CXJ1 7jvLx+k925J7KxFEADxaGkeZACkJI1FygzhRfVMLOj3lOI/aePE54EP7n63N3NpMCQCwRU ktDNEASM8jrTggmBH4o4hD/L6gHDSe5h0DcY9BwzC0udbJMoe5dUU6cUQjbpjkd4mFbBM0 9UV/oDCI5zJ0uyovuPnoWiB03crtcy5hBfVTHQIlQdS9OwoXLw0kPtoALQrqsw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1786641853; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=FLNHn3yfjx3jTkqvErT8Tf8SOwtVZG0HeLOiq2otzZ0=; b=I1byF8pG399RWG5zZyPp5fLln7MVHg7oxzpqwpCBi1nJj23K8bsN96ETZcYvnSjh3rXZls k3hNhrTh6YgfpyCg== From: "tip-bot2 for Bijan Tabatabai" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/mm] x86/pkeys: Fix pkey_alloc() return value when pkeys are not supported Cc: Bijan Tabatabai , Dave Hansen , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20260716220604.26452-1-bijan311@gmail.com> References: <20260716220604.26452-1-bijan311@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-ID: <178664185150.442315.2534098997977860605.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Precedence: bulk Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable The following commit has been merged into the x86/mm branch of tip: Commit-ID: dee87e09b0dd63da9b1e1876167ccae37842dfd0 Gitweb: https://git.kernel.org/tip/dee87e09b0dd63da9b1e1876167ccae37= 842dfd0 Author: Bijan Tabatabai AuthorDate: Thu, 16 Jul 2026 17:06:04 -05:00 Committer: Dave Hansen 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 Signed-off-by: Dave Hansen 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 =3D ((1U << arch_max_pkey()) - 1); int ret; =20 + 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