Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp1684821pxp; Thu, 10 Mar 2022 10:02:43 -0800 (PST) X-Google-Smtp-Source: ABdhPJwq+KTK6b0jjQiLuGXI0BT5ARmbtFq2HnovGVVXYaQypsguEKAWcSzlGPQ/bPt+X7R72xF3 X-Received: by 2002:a17:907:6e8c:b0:6da:7ac4:5346 with SMTP id sh12-20020a1709076e8c00b006da7ac45346mr5252173ejc.300.1646935363275; Thu, 10 Mar 2022 10:02:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646935363; cv=none; d=google.com; s=arc-20160816; b=sqrtl5yeGFBlL+gZ7jALC99/giCTW/EqcJwZH4PreC9P9biSAyJgiChqtYpllH8CsB LMfqJ7MCnU27ewKKlCYRLEHQATWFqGZH9uktKOHC5CIQoqb/AkJ8PTLDHUBiVe5qyad+ 05sTwEUULBa/IjJG1SfGsEzKoPQufg97+7Cbsl+8sG68cgtfZOs6moAcm9a4BbHS7nIB 6EUN00mRdWZHcYbF5NyilTlDwFkAzMQm9M0TbU6XuK4Z9St7elVXxSmHvt7xkawe+ZQd salx9L0fZiaDf2ZW2ouXykDx0nExzpSL9yvLTJWPZL2Igfcx/RRcaDt6P/CSA3+tPmVF dtXA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=4t/VydR3HoSNNvIZg0UWVAFn+VWqmtcsjJp3t+W7Oq8=; b=esQNEZ+DiYLjU05vr4O4Qi7BQgJs9a0V0ikq3DvA19/0bikkwSgwbv8YtGoNfftH0w r1UFZsCePFBYY2HZ6pWi08b6SQbEG1CfNBQIVnT35uVkHMXKQR+Ww6FEWoDF5UGXvcJQ 4ZxEg0RRxtfav3yVGY4Pdqdocap3mCVIeQwT5Yu9oXJSPXwQY1kT5M+JKUf3yzXb3/rJ GYoHL8td4uzdtZSbV0fNfLI2llBlNUFDnLj9YS0o+E8EPz3Lw+TYyd4lQssVWEYom0tC xLYEfk8Y7W6aMvQFS3XCDslW4a82bSpOz5HxNSTF0E2pEuCUfFHROk0IuOXsdb9gj8JU XDzQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=YhgKXsoX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g1-20020a1709065d0100b006cf37fdb0cfsi5625743ejt.936.2022.03.10.10.02.18; Thu, 10 Mar 2022 10:02:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=YhgKXsoX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244562AbiCJRWE (ORCPT + 99 others); Thu, 10 Mar 2022 12:22:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52304 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244518AbiCJRV4 (ORCPT ); Thu, 10 Mar 2022 12:21:56 -0500 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A4202199D5E for ; Thu, 10 Mar 2022 09:20:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1646932852; x=1678468852; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=3NA1AOfkWvyInGYP3ZXXPe9wyH6gcZKqLJYd1GBzKOE=; b=YhgKXsoXU30wU+Imqa+J1zcg7w+Ef+0rv1HoYnA5cxiCKRHFn9b3FVOP t9bZQyz5I55HVu8Rmm0hX2KBs4dwmGnLVw84XSuNUziwG/vrQlPcWTNd+ nHGkgTLEKmZxlyD2f3NaFk0igl6xSRlxM2tWqe0nt1v90G6qWuEJd7IvM RkBzcYwWvXsbWAYnTKK//J0lUN3HO26xuQo5yhqu7nrF8V/LUrKhXL0CU TlWtWFbuJATkRXXo/sbvyz+71jd4LSCrvcLHiDjVrcZf1S7iEHlzuJHhp xF3A068M4Yb8irAFqWVoE3XO82F/smXTLkxt7/EC5k9o4f6dMaHhFN5WN Q==; X-IronPort-AV: E=McAfee;i="6200,9189,10282"; a="242758931" X-IronPort-AV: E=Sophos;i="5.90,171,1643702400"; d="scan'208";a="242758931" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Mar 2022 09:20:48 -0800 X-IronPort-AV: E=Sophos;i="5.90,171,1643702400"; d="scan'208";a="510983950" Received: from gdavids1-mobl.amr.corp.intel.com (HELO localhost) ([10.212.65.108]) by orsmga002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Mar 2022 09:20:47 -0800 From: ira.weiny@intel.com To: Dave Hansen , "H. Peter Anvin" , Dan Williams Cc: Ira Weiny , Fenghua Yu , Rick Edgecombe , "Shankar, Ravi V" , linux-kernel@vger.kernel.org Subject: [PATCH V9 10/45] mm/pkeys: Provide for PKS key allocation Date: Thu, 10 Mar 2022 09:19:44 -0800 Message-Id: <20220310172019.850939-11-ira.weiny@intel.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220310172019.850939-1-ira.weiny@intel.com> References: <20220310172019.850939-1-ira.weiny@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Ira Weiny Kernel consumers of PKS need a way to allocate a PKS pkey and assign the initial permissions for that key. It is desirable to not allocate keys for consumers which are not configured. Introduce a macro to allocate keys sequentially based on which consumers are configured. In addition define a macro to set the proper permission bits based on the actual pkey value allocated. pks-keys.h is added as a new header with minimal header dependencies. This allows the use of PKS_INIT_VALUE within other headers where the additional includes from other pkey headers caused major conflicts. The main conflict was using PKS_INIT_VALUE for INIT_TRHEAD in asm/processor.h Add documentation. Suggested-by: Dan Williams Signed-off-by: Ira Weiny --- Changes for V9 Reword the commit message Move this patch ahead of the enable patch so that the enable patch can use PKS_INIT_VALUE From Dan Williams Use Dan's macro magic enhanced it to account for the max number of keys Update documentation for the change From Dave Hansen use pkey s/PKR_RW_KEY/PKR_RW_MASK Changes for V8 Create pks-keys.h to solve header conflicts in subsequent patches. Remove create_initial_pkrs_value() which did not work Replace it with PKS_INIT_VALUE Fix up documentation to match s/PKR_RW_BIT/PKR_RW_KEY()/ s/PKRS_INIT_VALUE/PKS_INIT_VALUE Split this off of the previous patch Update documentation and embed it in the code to help ensure it is kept up to date. Changes for V7 Create a dynamic pkrs_initial_value in early init code. Clean up comments Add comment to macro guard --- Documentation/core-api/protection-keys.rst | 5 ++ arch/x86/include/asm/pkeys_common.h | 9 ++- include/linux/pks-keys.h | 78 ++++++++++++++++++++++ 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 include/linux/pks-keys.h diff --git a/Documentation/core-api/protection-keys.rst b/Documentation/core-api/protection-keys.rst index 13eedb0119e1..d501bd27ee29 100644 --- a/Documentation/core-api/protection-keys.rst +++ b/Documentation/core-api/protection-keys.rst @@ -131,3 +131,8 @@ ARCH_HAS_SUPERVISOR_PKEYS. It also makes it possible for multiple independent features to "select ARCH_ENABLE_SUPERVISOR_PKEYS". If no features enable PKS by selecting ARCH_ENABLE_SUPERVISOR_PKEYS, PKS support will not be compiled into the kernel. + +PKS Key Allocation +------------------ +.. kernel-doc:: include/linux/pks-keys.h + :doc: PKS_KEY_ALLOCATION diff --git a/arch/x86/include/asm/pkeys_common.h b/arch/x86/include/asm/pkeys_common.h index 359b94cdcc0c..b28a72dea22b 100644 --- a/arch/x86/include/asm/pkeys_common.h +++ b/arch/x86/include/asm/pkeys_common.h @@ -2,10 +2,17 @@ #ifndef _ASM_X86_PKEYS_COMMON_H #define _ASM_X86_PKEYS_COMMON_H +#define PKS_NUM_PKEYS 16 +#define PKS_ALL_AD (0x55555555UL) + #define PKR_AD_BIT 0x1u #define PKR_WD_BIT 0x2u #define PKR_BITS_PER_PKEY 2 -#define PKR_AD_MASK(pkey) (PKR_AD_BIT << ((pkey) * PKR_BITS_PER_PKEY)) +#define PKR_PKEY_SHIFT(pkey) (pkey * PKR_BITS_PER_PKEY) + +#define PKR_RW_MASK(pkey) (0 << PKR_PKEY_SHIFT(pkey)) +#define PKR_AD_MASK(pkey) (PKR_AD_BIT << PKR_PKEY_SHIFT(pkey)) +#define PKR_WD_MASK(pkey) (PKR_WD_BIT << PKR_PKEY_SHIFT(pkey)) #endif /*_ASM_X86_PKEYS_COMMON_H */ diff --git a/include/linux/pks-keys.h b/include/linux/pks-keys.h new file mode 100644 index 000000000000..c914afecb2d3 --- /dev/null +++ b/include/linux/pks-keys.h @@ -0,0 +1,78 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_PKS_KEYS_H +#define _LINUX_PKS_KEYS_H + +/* + * The contents of this header should be limited to assigning PKS keys and + * default values to avoid intricate header dependencies. + */ + +#ifdef CONFIG_ARCH_ENABLE_SUPERVISOR_PKEYS + +#include + +#define PKS_NEW_KEY(prev, config) \ + (prev + __is_defined(config)) +#define PKS_DECLARE_INIT_VALUE(pkey, value, config) \ + (PKR_##value##_MASK(pkey) * __is_defined(config)) + +/** + * DOC: PKS_KEY_ALLOCATION + * + * Users reserve a key value in 5 steps. + * 1) Use PKS_NEW_KEY to create a new key + * 2) Ensure that the last key value is specified in the PKS_NEW_KEY macro + * 3) Adjust PKS_KEY_MAX to use the newly defined key value + * 4) Use PKS_DECLARE_INIT_VALUE to define an initial value + * 5) Add the new PKS default value to PKS_INIT_VALUE + * + * The PKS_NEW_KEY and PKS_DECLARE_INIT_VALUE macros require the Kconfig + * option to be specified to automatically adjust the number of keys used. + * + * PKS_KEY_DEFAULT must remain 0 with a default of PKS_DECLARE_INIT_VALUE(..., + * RW, ...) to support non-pks protected pages. + * + * Example: to configure a key for 'MY_FEATURE' with a default of Write + * Disabled. + * + * .. code-block:: c + * + * #define PKS_KEY_DEFAULT 0 + * + * // 1) Use PKS_NEW_KEY to create a new key + * // 2) Ensure that the last key value is specified (eg PKS_KEY_DEFAULT) + * #define PKS_KEY_MY_FEATURE PKS_NEW_KEY(PKS_KEY_DEFAULT, CONFIG_MY_FEATURE) + * + * // 3) Adjust PKS_KEY_MAX + * #define PKS_KEY_MAX PKS_NEW_KEY(PKS_KEY_MY_FEATURE, 1) + * + * // 4) Define initial value + * #define PKS_KEY_MY_FEATURE_INIT PKS_DECLARE_INIT_VALUE(PKS_KEY_MY_FEATURE, \ + * WD, CONFIG_MY_FEATURE) + * + * + * // 5) Add initial value to PKS_INIT_VALUE + * #define PKS_INIT_VALUE ((PKS_ALL_AD & PKS_ALL_AD_MASK) | \ + * PKS_KEY_DEFAULT_INIT | \ + * PKS_KEY_MY_FEATURE_INIT \ + * ) + */ + +/* PKS_KEY_DEFAULT must be 0 */ +#define PKS_KEY_DEFAULT 0 +#define PKS_KEY_MAX PKS_NEW_KEY(PKS_KEY_DEFAULT, 1) + +/* PKS_KEY_DEFAULT_INIT must be RW */ +#define PKS_KEY_DEFAULT_INIT PKS_DECLARE_INIT_VALUE(PKS_KEY_DEFAULT, RW, 1) + +#define PKS_ALL_AD_MASK \ + GENMASK(PKS_NUM_PKEYS * PKR_BITS_PER_PKEY, \ + PKS_KEY_MAX * PKR_BITS_PER_PKEY) + +#define PKS_INIT_VALUE ((PKS_ALL_AD & PKS_ALL_AD_MASK) | \ + PKS_KEY_DEFAULT_INIT \ + ) + +#endif /* CONFIG_ARCH_ENABLE_SUPERVISOR_PKEYS */ + +#endif /* _LINUX_PKS_KEYS_H */ -- 2.35.1