Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp1772051pxp; Thu, 10 Mar 2022 11:51:47 -0800 (PST) X-Google-Smtp-Source: ABdhPJzvu39QktAQc1E0fEWQSUgDNgelhALnNou4V24pBY2U+YGBZO9fkJOeDiIHZtCBlyu+3jET X-Received: by 2002:a17:907:8690:b0:6da:8436:2b94 with SMTP id qa16-20020a170907869000b006da84362b94mr5536028ejc.33.1646941906921; Thu, 10 Mar 2022 11:51:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646941906; cv=none; d=google.com; s=arc-20160816; b=MfGtBZG7+HYgsnL5o/MvGdImqZJ3AG/gXQqZl3CoX+dQ1h2ieU82Auy8eyx/oC3Am5 OjjQFShh9HVzaUSe5HP0xhFejGf4Pp7POFc2AhE4bJocesf1chxEHzpnJNXZ2HM8mb7P 9gjVkG8ySJ6hvHt3pJmkOq5xuTlpOw4assWACs4OW3J+tyyxQ8E/94Oo0bQKape3bgET K88CAzYNw3yLj2uXnZKvedpVtBReF5iV1HDW5y+UFmjp7jORS8o0dIpe8ey4iygq8vD6 i/OWsVLNvop4NGjwrosNf+oxO3aXdbLxuDukFgxXPjp2WDuykPo+CKei4tmq8U+T2Qgu GGtA== 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=Ix6zMmnZESJk9ACuHjQ79U/hpySBIW0IEbJZ8OgnHlQ=; b=IRuEvtWSvaAi4L6uk7QZh/bjAVrl7xWlqT8l7XIDZv43nmoDQGzlQZh7jAZgljmZgh 29tWLuR3eJ5pKLn4wjb1Iv1OkSIMVn58kAF0bFZH+mALFHNplvsJRBwEUNJk8mIieTf7 HiMltx59q12ntjRkq6CADZjaqUTFGaBQQwMvTWsl9FYz3jR5zukyAm2zBpaqB2vsk4Qp iBTRZnMGr1oND7o/2mxDkwZbZ32LAwWrp6ao5i9ZZ9UNBUc3BrARC2nTeD+S+AbBIVCm iyJDlstk7OamkqNR4IbXL70Oe8ZPgAdW7BQfKVg9tm70RH2x5+MxqVwvwVVO4ZrFKKNH VyOQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ewRiZuDH; 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 bu26-20020a170906a15a00b006d09dc2b438si3815869ejb.268.2022.03.10.11.51.24; Thu, 10 Mar 2022 11:51:46 -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=ewRiZuDH; 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 S244949AbiCJRZM (ORCPT + 99 others); Thu, 10 Mar 2022 12:25:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244895AbiCJRW5 (ORCPT ); Thu, 10 Mar 2022 12:22:57 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8CB7919BE62 for ; Thu, 10 Mar 2022 09:21:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1646932899; x=1678468899; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Sb7K9DLoM6ZEfjM7I2FIPjnC4RdnN9IAPaKEfWLdtuU=; b=ewRiZuDHKetwsGJ6P8WshwPxxsaJlxjoA9EpWasKAJzfl17JdvShW6hi yuCZnkR8IgVcXVT6zaf0rlQZTQIXclDPXbNmi5TkJZnbUAR+pq4eJO01z kZmC8FFbRML4483eg7umtGil9DGxlxCmIeW+7toLihn9jB8SGz8VoGnUh XAtzw+T0ycnsg9NVvHAQmGQr1jUEh8PuuQdNdZLxqUhooR2BKsxKoCv4/ QGx+mifFhIRl/dKXJtnEqfCpIgJKRJeNuXomAE1wTs91YfesRGY1E39fh AzBmh04eDAkiOxMgvWWUp9y15j+zXNEbG3JfW84EXGLKoFuGHZ47HUiAs A==; X-IronPort-AV: E=McAfee;i="6200,9189,10282"; a="255260149" X-IronPort-AV: E=Sophos;i="5.90,171,1643702400"; d="scan'208";a="255260149" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Mar 2022 09:21:39 -0800 X-IronPort-AV: E=Sophos;i="5.90,171,1643702400"; d="scan'208";a="815940621" Received: from gdavids1-mobl.amr.corp.intel.com (HELO localhost) ([10.212.65.108]) by fmsmga005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Mar 2022 09:21:38 -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 33/45] mm/pkeys: Add pks_available() Date: Thu, 10 Mar 2022 09:20:07 -0800 Message-Id: <20220310172019.850939-34-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, 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 If PKS is configured within the kernel but the CPU does not support PKS, the PKS calls remain safe to execute even without protection. However, adding the overhead of these calls on CPUs which don't support PKS is inefficient and best avoided. Define pks_available() to allow users to check if PKS is enabled on the current system. The implementation of pks_available() is placed in the asm headers while being directly exported via linux/pks.h to allow for the inline calling of cpu_feature_enabled() by consumers outside of the architecture. Signed-off-by: Ira Weiny --- Changes for V9 Driven by a request by Dan Williams to make this static inline Place this in pks.h to avoid header conflicts while allowing for an optimized call to cpu_feature_enabled() Changes for V8 s/pks_enabled/pks_available --- Documentation/core-api/protection-keys.rst | 3 +++ arch/x86/include/asm/pks.h | 12 ++++++++++++ include/linux/pks.h | 8 ++++++++ 3 files changed, 23 insertions(+) diff --git a/Documentation/core-api/protection-keys.rst b/Documentation/core-api/protection-keys.rst index 68fe7a92cc98..36621cbc2cc6 100644 --- a/Documentation/core-api/protection-keys.rst +++ b/Documentation/core-api/protection-keys.rst @@ -152,6 +152,9 @@ Changing permissions of individual keys .. kernel-doc:: arch/x86/mm/pkeys.c :identifiers: pks_update_exception +.. kernel-doc:: arch/x86/include/asm/pks.h + :identifiers: pks_available + Overriding Default Fault Behavior --------------------------------- diff --git a/arch/x86/include/asm/pks.h b/arch/x86/include/asm/pks.h index de67d5b5a2af..cab42aadea07 100644 --- a/arch/x86/include/asm/pks.h +++ b/arch/x86/include/asm/pks.h @@ -2,8 +2,20 @@ #ifndef _ASM_X86_PKS_H #define _ASM_X86_PKS_H +#include + #ifdef CONFIG_ARCH_ENABLE_SUPERVISOR_PKEYS +/** + * pks_available() - Is PKS available on this system + * + * Return if PKS is currently supported and enabled on this system. + */ +static inline bool pks_available(void) +{ + return cpu_feature_enabled(X86_FEATURE_PKS); +} + void pks_setup(void); void x86_pkrs_load(struct thread_struct *thread); void pks_save_pt_regs(struct pt_regs *regs); diff --git a/include/linux/pks.h b/include/linux/pks.h index 45156f358776..163c75992a8a 100644 --- a/include/linux/pks.h +++ b/include/linux/pks.h @@ -8,6 +8,9 @@ #include +#include + +bool pks_available(void); void pks_update_protection(u8 pkey, u8 protection); void pks_update_exception(struct pt_regs *regs, u8 pkey, u8 protection); @@ -40,6 +43,11 @@ typedef bool (*pks_key_callback)(struct pt_regs *regs, unsigned long address, #else /* !CONFIG_ARCH_ENABLE_SUPERVISOR_PKEYS */ +static inline bool pks_available(void) +{ + return false; +} + static inline void pks_set_noaccess(u8 pkey) {} static inline void pks_set_readwrite(u8 pkey) {} static inline void pks_update_exception(struct pt_regs *regs, -- 2.35.1