Received: by 2002:a05:6a10:c7c6:0:0:0:0 with SMTP id h6csp2965645pxy; Tue, 3 Aug 2021 21:42:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyZarftQjwOOVsnf9OJjVllclSMGsNFnke0aw+pQiHyhayW9U6nBcrQARbr86B100+vJr3C X-Received: by 2002:a05:6e02:12e3:: with SMTP id l3mr227142iln.160.1628052178572; Tue, 03 Aug 2021 21:42:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628052178; cv=none; d=google.com; s=arc-20160816; b=oalMCx/GMdwpSU77HjOZD6kBJn2PdSbuEnjBdnMDFw7IJWpOgHxlggWPcDFu+rJhTs 5w90RIv3KjoxFufe0ZwVku7pAF3E33SSOrN33fLnKcHnhaTbeKm429xLZx0iq8dilTKo 3+xOaZsEq7dddXGlO53W0QwngkMNc0cd2k3FfqporZ4owi9Jx6BFghu42ynIEWbhDxkp F9zAufIIZKCTC4c1UvwqOesxL1Ra3PGs10rrZp/PosgWXVqHxW7Pdxxdd6+4gzFgW0pR qoa4pT8qI56Brqe6SJNU6BG61iA71sUTe0XQ+pz3l/UQEXUk3K2ud8inwUniHJB3oreS MqvA== 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; bh=cFvq2UjRVWD2IRGX7amPqbFQgGSBKoCFfHRRcnx0s98=; b=SjrgoY11KchCqJ2FiunirXKlKlLG1MyPgZ2PEgeoAqAVKEYBAIP+4SkK4nyhyAlv+p RrEF7ZMhQWLMTURYlHgITRtw1Kl0YVOJxpbITPgH1uBZXRzSO2MRAH++mhdcWA0rFTsJ pHgzSn87HoY1HIQfvMhgxoOWCVvxpqwiJU1rq7cdGUfo+9BdwktWxF+etY41QVCPpqcC Vdmxs9h8251ywZAlBbJ6e6kCvqt/Rzqs6nsYreVEFAqNv3xJGaZWCrgii4fFL4uYTAf6 9V68payLn4TVDNuj0p9oyqhOkitQZyBfAts6o9EZaBhlX7Amul57GnhTwG0qq8itnMwB 7LFg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k2si857860iol.39.2021.08.03.21.42.47; Tue, 03 Aug 2021 21:42:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235031AbhHDEc7 (ORCPT + 99 others); Wed, 4 Aug 2021 00:32:59 -0400 Received: from mga14.intel.com ([192.55.52.115]:41723 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234313AbhHDEcs (ORCPT ); Wed, 4 Aug 2021 00:32:48 -0400 X-IronPort-AV: E=McAfee;i="6200,9189,10065"; a="213574609" X-IronPort-AV: E=Sophos;i="5.84,293,1620716400"; d="scan'208";a="213574609" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Aug 2021 21:32:35 -0700 X-IronPort-AV: E=Sophos;i="5.84,293,1620716400"; d="scan'208";a="511702663" Received: from iweiny-desk2.sc.intel.com (HELO localhost) ([10.3.52.147]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Aug 2021 21:32:35 -0700 From: ira.weiny@intel.com To: Dave Hansen , Dan Williams Cc: Ira Weiny , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Peter Zijlstra , Andy Lutomirski , "H. Peter Anvin" , Fenghua Yu , Rick Edgecombe , x86@kernel.org, linux-kernel@vger.kernel.org, nvdimm@lists.linux.dev, linux-mm@kvack.org Subject: [PATCH V7 03/18] x86/pks: Add additional PKEY helper macros Date: Tue, 3 Aug 2021 21:32:16 -0700 Message-Id: <20210804043231.2655537-4-ira.weiny@intel.com> X-Mailer: git-send-email 2.28.0.rc0.12.gb6a658bd00c9 In-Reply-To: <20210804043231.2655537-1-ira.weiny@intel.com> References: <20210804043231.2655537-1-ira.weiny@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Ira Weiny Avoid open coding shift and mask operations by defining and using helper macros for PKey operations. Signed-off-by: Ira Weiny --- arch/x86/include/asm/pkeys_common.h | 6 +++++- arch/x86/include/asm/pkru.h | 6 ++---- arch/x86/mm/pkeys.c | 8 +++----- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/arch/x86/include/asm/pkeys_common.h b/arch/x86/include/asm/pkeys_common.h index f3277717faeb..8a3c6d2e6a8a 100644 --- a/arch/x86/include/asm/pkeys_common.h +++ b/arch/x86/include/asm/pkeys_common.h @@ -6,6 +6,10 @@ #define PKR_WD_BIT 0x2 #define PKR_BITS_PER_PKEY 2 -#define PKR_AD_KEY(pkey) (PKR_AD_BIT << ((pkey) * PKR_BITS_PER_PKEY)) +#define PKR_PKEY_SHIFT(pkey) (pkey * PKR_BITS_PER_PKEY) +#define PKR_PKEY_MASK(pkey) (((1 << PKR_BITS_PER_PKEY) - 1) << PKR_PKEY_SHIFT(pkey)) + +#define PKR_AD_KEY(pkey) (PKR_AD_BIT << PKR_PKEY_SHIFT(pkey)) +#define PKR_WD_KEY(pkey) (PKR_WD_BIT << PKR_PKEY_SHIFT(pkey)) #endif /*_ASM_X86_PKEYS_COMMON_H */ diff --git a/arch/x86/include/asm/pkru.h b/arch/x86/include/asm/pkru.h index a74325b0d1df..fb44ff542028 100644 --- a/arch/x86/include/asm/pkru.h +++ b/arch/x86/include/asm/pkru.h @@ -15,15 +15,13 @@ extern u32 init_pkru_value; static inline bool __pkru_allows_read(u32 pkru, u16 pkey) { - int pkru_pkey_bits = pkey * PKR_BITS_PER_PKEY; - return !(pkru & (PKR_AD_BIT << pkru_pkey_bits)); + return !(pkru & PKR_AD_KEY(pkey)); } static inline bool __pkru_allows_write(u32 pkru, u16 pkey) { - int pkru_pkey_bits = pkey * PKR_BITS_PER_PKEY; /* Access-disable disables writes too so check both bits here. */ - return !(pkru & ((PKR_AD_BIT|PKR_WD_BIT) << pkru_pkey_bits)); + return !(pkru & (PKR_AD_KEY(pkey) | PKR_WD_KEY(pkey))); } static inline u32 read_pkru(void) diff --git a/arch/x86/mm/pkeys.c b/arch/x86/mm/pkeys.c index ca2e20b18645..75437aa8fc56 100644 --- a/arch/x86/mm/pkeys.c +++ b/arch/x86/mm/pkeys.c @@ -200,16 +200,14 @@ __setup("init_pkru=", setup_init_pkru); */ u32 update_pkey_val(u32 pk_reg, int pkey, unsigned int flags) { - int pkey_shift = pkey * PKR_BITS_PER_PKEY; - /* Mask out old bit values */ - pk_reg &= ~(((1 << PKR_BITS_PER_PKEY) - 1) << pkey_shift); + pk_reg &= ~PKR_PKEY_MASK(pkey); /* Or in new values */ if (flags & PKEY_DISABLE_ACCESS) - pk_reg |= PKR_AD_BIT << pkey_shift; + pk_reg |= PKR_AD_KEY(pkey); if (flags & PKEY_DISABLE_WRITE) - pk_reg |= PKR_WD_BIT << pkey_shift; + pk_reg |= PKR_WD_KEY(pkey); return pk_reg; } -- 2.28.0.rc0.12.gb6a658bd00c9