From nobody Sat Jul 25 06:09:12 2026 Received: from mail-yx1-f52.google.com (mail-yx1-f52.google.com [74.125.224.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E87053CB57C for ; Thu, 16 Jul 2026 22:06:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.224.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784239572; cv=none; b=ETGpfUJIzKQOGYdVTAjLVc4TfuPab0oDRLl23r7mCdMKDMo2iA5Jgeq+nnMwGIbZZLtfnVBh5RP4Fe3is7ZKcRMzeDaLdHIYFC+BQIWInRzt5Frvoqa5tHFE94DUBSRQT6jeFmLTVdQ12KwR2AVLVAh9k8ntk6UIfPtVp9qwfEI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784239572; c=relaxed/simple; bh=cb0IbFOuu+L6b5HIwcIo5N5So6CHAN1pxDD/iLttFSs=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=W1AzkyHwvgBrCNnARo241V+VCfqB8yycAcMDF+3miv0QrgnMowVpHkRTICx8vYRQIkiKN3cXS2BKGdUzKB/Xov6wBpUgnp8VQtBn8XAxQ03gUEHY/o47FM1AQ+UAq6heuPAhlQTMTrdo1O1ofmMPQg3k/Oo2UqXpVF5MZCs7dHQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=MPi+TjrK; arc=none smtp.client-ip=74.125.224.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="MPi+TjrK" Received: by mail-yx1-f52.google.com with SMTP id 956f58d0204a3-668296d0ff3so1541732d50.0 for ; Thu, 16 Jul 2026 15:06:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1784239570; x=1784844370; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=if4SZoTx/+oBrTXZglLStib3EMqB+QcnZMMTT8J/kxY=; b=MPi+TjrKIj0prlHZtpQUT/nQwq3z0yqXJgy/JG+FPs+ev38giFS/ZW5ByF9LYYH8VL 053Lxy6Kj58k8aq3A/yvody6DwGDrxQ9nQ0ofaFBEOoiRXGhXYrwcHokf+24jBktHYhG 5PVdCz4KyIY/w7p+QtcgWc0t8S9MA2ViPcxTIliKsBvkKf+PFi8UehcCJfuoARh6dpNQ phd0mogL5qpVXDhz/zU0Ef+3XrnpE5PXUWdvx5C7FdTv9/pmugpxdNEHEwUc9TMomTMy t6iitdqC5F/oNElk0SINeMF6OACKcUPZHWzhyoIzh8LXGUPZ3Kdkx+PA3rDXjROh02MT HXjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784239570; x=1784844370; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=if4SZoTx/+oBrTXZglLStib3EMqB+QcnZMMTT8J/kxY=; b=NJZLhTsaXjRbY8S/kehLwx8nUofRAbFIseSKrc3lmgl1OanS0nXAZVN5+WC5ncWC5C hEEsPY7Jp/JF87gd71HOOAkzr7vyYHDh1h+DbvrC6eus5p+2IUCj4TBGwGTg4coh+Ojn MQ3VF2OUM7p0Ddz5qkDKlWoEODj4ISmHT9o3DF9drE0uigGLO87Pj9OBOQs3Y4/AjWK4 oXYi7C4aG49vQ2AmCN/MGccZDeffsYJZrgDDxq2CQdEkOFoUsfWSrMifr+BV5DqWaSgA SwGn5SvMBWTE1ojBguIQE9T46FZA6NvqFk1VI6h6GrLN75iaW8jiFBmav4gdVNLZJplO Ugng== X-Forwarded-Encrypted: i=1; AHgh+RpEKmBSZU9+lo54DqwLrZ82msqnpOlHRbgyUkC5xDaNsAZBnzzFLe1rD1L673BmK+w4lDZBMdDo7tIgqpc=@vger.kernel.org X-Gm-Message-State: AOJu0YxesgpeqVbysQB7262N/CciHX/hOC0dw6JkwWSb13jUQMhjgoqK jpCSHw6lQ4QX1diK1NR1BcWYIxKeGvblYJXO7bmXrzu3R4LSSDGjIAow X-Gm-Gg: AfdE7clwFLeK6W2hdqJWuajReH5Xv3VcMPW0Zrfdn8G8oqiod0CS24Kt1s2rWEbzYVi qQAVVAXlsMN31spknvKNOJPVUXXUjk3UIVzyVGbXRgFOXU+dJLoYjvBXR+bxb/G5geH85e1Cq8r dkc3lcF41ywbQBSq+ZGvcz8n1SddrMspu+WmXuy9VvwYuFRn0yiHaeN1+AHYcgjkLKebMUk74it e1V980ZMSbIJJZgYnV6L/6COsaSZxfbRqZdK56jIzWGNNih9uxdfs9X7NerqdF3UzWjSPy6F9Z5 2mnEOel+R/XVXbrdxch2yWvAvAvGUuQIJv2Giod4wFrvY13mQpSOJY4mFCxJMHjOzXTTSgCakd3 l1Ati1ma2jiWoi+SaD/30NEvPZPmwdM/+i7n1MAux2HrSGj6bV0tj+W3VTQVnl2wpwefkvgssm9 RApKdOqRtLH6395/LYWz3vKouIiI+Pl8D9/Z/HodqBtx85qBbMDZVFXS0= X-Received: by 2002:a05:690e:4853:b0:664:a5d0:5a48 with SMTP id 956f58d0204a3-66813433afamr4596935d50.63.1784239569768; Thu, 16 Jul 2026 15:06:09 -0700 (PDT) Received: from bijan-laptop.attlocal.net ([2600:1700:680e:c000:ca23:a143:d088:cb76]) by smtp.gmail.com with ESMTPSA id 956f58d0204a3-668200aab9asm4560564d50.19.2026.07.16.15.06.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Jul 2026 15:06:09 -0700 (PDT) From: Bijan Tabatabai To: tglx@kernel.org, dave.hansen@linux.intel.com, mgorman@techsingularity.net, linux-kernel@vger.kernel.org Cc: mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, sgsu.park@samsung.com, akpm@linux-foundation.org, Bijan Tabatabai Subject: [PATCH] x86/pkeys: Fix pkey_alloc return value when pkeys are not supported Date: Thu, 16 Jul 2026 17:06:04 -0500 Message-ID: <20260716220604.26452-1-bijan311@gmail.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Bijan Tabatabai 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 --- 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 =3D ((1U << arch_max_pkey()) - 1); int ret; =20 + 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 --=20 2.34.1