Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp645254pxf; Wed, 31 Mar 2021 12:16:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz14RUkweg7rWM1qLTe11+Lw7UQud/N2E7odqBITM3rlLcDL/83HL33XnWU/K8tXlys0uN0 X-Received: by 2002:a17:907:3e92:: with SMTP id hs18mr5174126ejc.396.1617218198219; Wed, 31 Mar 2021 12:16:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617218198; cv=none; d=google.com; s=arc-20160816; b=T5iNM8R+Ch6vpvbePm7T/972reqLYI5OE0Jii4//GnU6Canjen4mOhrL/K7StDjOtN jvVdFSXI/OuAT8AulxxH/B+LPhYhNxokw4Pe8Ix79bp8QCjaVt9PjxSrHexxoRlOX+lo cHl2RViGRo/p+DmQ3KN/oAoV4foXpdyCJYu6pqoqDcWahg2x4SCvzSUhQsiHxFXxDPoz lfO8er/bkNC5cXdQJannxSKNveowZc2koUOvFK6Kc/wZSXkCMYRY1+ZtvVZAZkjdBGei 8efu4ThTJSFRo1/hRjVOhIqrsY1ZelzS4yccW+ICAddzTvnXqERKtzGvpj4m4G7cFEA1 RS9g== 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 :ironport-sdr:ironport-sdr; bh=mvoOE5r1C7vAD8Z9OJZBNiZgc+LSH/RY54xRSgsQ0Rk=; b=v5t4uWAxzgKi7JsroPzjyFSrMqtDqLZX7EwJGP2hzKyXcMHDNYdQ6Xxk2z1/3/2fnF WoIEXEa8KMO31WfXxgdEZpAYDPDKZknNQgwgU5PuxTdK3Arsy93A4Yfv8+CNHcGIY45A DVV/Y1xk/gFIFvk2dDLQmPKQKTzUtvyA3ZcCrQwTbdUg7yOJ33FWCLZ9AJLVx6JUklZP BPSwkoU30+BK9B9FMxwA9+1mtsoxGsytIAhT+kOs6+H7naAQXRXTxSpbCYnwA69Nx5qE IZOGFwyg368d5LHGxwsWMDXfxi4Ssp5EeMkDRyZJy9aA/W3QGAJORQpE94/UyzssZeqb oQuA== 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 r21si2464932edy.321.2021.03.31.12.16.15; Wed, 31 Mar 2021 12:16:38 -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 S235991AbhCaTOq (ORCPT + 99 others); Wed, 31 Mar 2021 15:14:46 -0400 Received: from mga12.intel.com ([192.55.52.136]:51675 "EHLO mga12.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234922AbhCaTOP (ORCPT ); Wed, 31 Mar 2021 15:14:15 -0400 IronPort-SDR: 4a+VVHoUFDGwOL79kYpTdzhhvAulnlx4UEVmZVkj+n3uvZ4ls1YhZ3nBi3Mmcst2CcLvT6mSYk 6DFV7Ep+zYEw== X-IronPort-AV: E=McAfee;i="6000,8403,9940"; a="171490593" X-IronPort-AV: E=Sophos;i="5.81,293,1610438400"; d="scan'208";a="171490593" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Mar 2021 12:14:15 -0700 IronPort-SDR: IjFkLYsM8F1gNcD5ahlKsXkbhNxLpN98CrB7OFbcQ/xrtnUpvT6Hreol7Bk1K44pJKKSb8JsAt 9A/GZonk2yuQ== X-IronPort-AV: E=Sophos;i="5.81,293,1610438400"; d="scan'208";a="610625378" Received: from iweiny-desk2.sc.intel.com (HELO localhost) ([10.3.52.147]) by fmsmga005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Mar 2021 12:14:15 -0700 From: ira.weiny@intel.com To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Andy Lutomirski , Peter Zijlstra Cc: Ira Weiny , Dan Williams , Dave Hansen , x86@kernel.org, linux-kernel@vger.kernel.org, Fenghua Yu , linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH V5 02/10] x86/fpu: Refactor arch_set_user_pkey_access() for PKS support Date: Wed, 31 Mar 2021 12:13:57 -0700 Message-Id: <20210331191405.341999-3-ira.weiny@intel.com> X-Mailer: git-send-email 2.28.0.rc0.12.gb6a658bd00c9 In-Reply-To: <20210331191405.341999-1-ira.weiny@intel.com> References: <20210331191405.341999-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 Define a helper, update_pkey_val(), which will be used to support both Protection Key User (PKU) and the new Protection Key for Supervisor (PKS) in subsequent patches. Reviewed-by: Dan Williams Co-developed-by: Peter Zijlstra Signed-off-by: Peter Zijlstra Signed-off-by: Ira Weiny --- Changes from RFC V3: Per Dave Hansen Update and add comments per Dave's review Per Peter Correct attribution --- arch/x86/include/asm/pkeys.h | 2 ++ arch/x86/kernel/fpu/xstate.c | 22 ++++------------------ arch/x86/mm/pkeys.c | 23 +++++++++++++++++++++++ 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/arch/x86/include/asm/pkeys.h b/arch/x86/include/asm/pkeys.h index f9feba80894b..4526245b03e5 100644 --- a/arch/x86/include/asm/pkeys.h +++ b/arch/x86/include/asm/pkeys.h @@ -136,4 +136,6 @@ static inline int vma_pkey(struct vm_area_struct *vma) return (vma->vm_flags & vma_pkey_mask) >> VM_PKEY_SHIFT; } +u32 update_pkey_val(u32 pk_reg, int pkey, unsigned int flags); + #endif /*_ASM_X86_PKEYS_H */ diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c index face29dab0e3..00251bdf759b 100644 --- a/arch/x86/kernel/fpu/xstate.c +++ b/arch/x86/kernel/fpu/xstate.c @@ -994,9 +994,7 @@ const void *get_xsave_field_ptr(int xfeature_nr) int arch_set_user_pkey_access(struct task_struct *tsk, int pkey, unsigned long init_val) { - u32 old_pkru; - int pkey_shift = (pkey * PKR_BITS_PER_PKEY); - u32 new_pkru_bits = 0; + u32 pkru; /* * This check implies XSAVE support. OSPKE only gets @@ -1012,21 +1010,9 @@ int arch_set_user_pkey_access(struct task_struct *tsk, int pkey, */ WARN_ON_ONCE(pkey >= arch_max_pkey()); - /* Set the bits we need in PKRU: */ - if (init_val & PKEY_DISABLE_ACCESS) - new_pkru_bits |= PKR_AD_BIT; - if (init_val & PKEY_DISABLE_WRITE) - new_pkru_bits |= PKR_WD_BIT; - - /* Shift the bits in to the correct place in PKRU for pkey: */ - new_pkru_bits <<= pkey_shift; - - /* Get old PKRU and mask off any old bits in place: */ - old_pkru = read_pkru(); - old_pkru &= ~((PKR_AD_BIT|PKR_WD_BIT) << pkey_shift); - - /* Write old part along with new part: */ - write_pkru(old_pkru | new_pkru_bits); + pkru = read_pkru(); + pkru = update_pkey_val(pkru, pkey, init_val); + write_pkru(pkru); return 0; } diff --git a/arch/x86/mm/pkeys.c b/arch/x86/mm/pkeys.c index f5efb4007e74..d1dfe743e79f 100644 --- a/arch/x86/mm/pkeys.c +++ b/arch/x86/mm/pkeys.c @@ -208,3 +208,26 @@ static __init int setup_init_pkru(char *opt) return 1; } __setup("init_pkru=", setup_init_pkru); + +/* + * Replace disable bits for @pkey with values from @flags + * + * Kernel users use the same flags as user space: + * PKEY_DISABLE_ACCESS + * PKEY_DISABLE_WRITE + */ +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); + + /* Or in new values */ + if (flags & PKEY_DISABLE_ACCESS) + pk_reg |= PKR_AD_BIT << pkey_shift; + if (flags & PKEY_DISABLE_WRITE) + pk_reg |= PKR_WD_BIT << pkey_shift; + + return pk_reg; +} -- 2.28.0.rc0.12.gb6a658bd00c9