Received: by 10.223.176.46 with SMTP id f43csp309092wra; Thu, 18 Jan 2018 17:57:21 -0800 (PST) X-Google-Smtp-Source: ACJfBosuIUujc7pyIYosix9GQAmc/2HQiJnx33HQALQ0kOEzjwe7JlCB9QfC+d6QW0hkhWVo4rRh X-Received: by 10.98.70.194 with SMTP id o63mr31190478pfi.58.1516327041317; Thu, 18 Jan 2018 17:57:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516327041; cv=none; d=google.com; s=arc-20160816; b=TQyeGzqa4MZnufGDbjXVCFbVZ7HPjZWBArmK0Sx2ukeWSFnCsEomc+KXMUkLiBaU7G LIxclY7Ny8BUMLo6SEqQHD/ZlBJ/JMa/1U+njdWdkMb/CBa/2cPj/VUp6pUVVIETNm/r W4Qwu41ksSYzC0bmrueor5+1uiY2SPLAOF2CrlBp98965jGAypDpIvoOLcqUoXPGW3vx 4iJZLeKvuR2GLXVNzYHeLaT1XzBP1vg7vUaHY6gZ7T7OzzKgIpu5So0aaJBbv80pW/Y5 AxCjX/AOHWA03YXRN6yK3F17uQDHFmCTZneT2FFFHQy72SQnBtlG9tKNdVWuK161Gg2G dXuw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=kd1QjyNzVstvf/hKaQSav5u6CIH1waK1YXXOi0/Q7Vg=; b=DiuF70fdGxN0CAuiPaLADZrdjAtBxlmuGG7y66qE+qDjXrgVlvnylz9Y2A81LLZPaM cFdCDraxJC/2UD/N/OrnzP0naj6deRdq769dvvtvzoaukTYbI48fgex0z3NudyxocQdq b2Ez5AX+KtsoeGh9XaDYcn8lPjJCMxwPjXMIIJeBGJ0UTYdgiUeZgvzXyogQCacJte58 FxG//VD96W935Qgnx6uqvqLmTqiV/KdFFAwSc18UVy7htMORRBvuIm5ePUGkv90rUsiB Vvy3sVSpG7wLYYi+A7cEqjWedpcHSIoRwD+f+6cgCeqfUu++2LDK8X6Xf4IE99ucBPJj OtEg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=JdOoDp4y; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q2si7028388pgp.400.2018.01.18.17.57.07; Thu, 18 Jan 2018 17:57:21 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=JdOoDp4y; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755829AbeASBwv (ORCPT + 99 others); Thu, 18 Jan 2018 20:52:51 -0500 Received: from mail-qt0-f196.google.com ([209.85.216.196]:39269 "EHLO mail-qt0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755294AbeASBwD (ORCPT ); Thu, 18 Jan 2018 20:52:03 -0500 Received: by mail-qt0-f196.google.com with SMTP id f4so439040qtj.6; Thu, 18 Jan 2018 17:52:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=kd1QjyNzVstvf/hKaQSav5u6CIH1waK1YXXOi0/Q7Vg=; b=JdOoDp4y6r6o0MQYwPD81P/t8GFjFs4FpIksfOCpZ/+UVHMxhTttsPjub1Li+B2eOS /vTVyuTCyEfIC2Y1qrT5cVS07bJ/kR/xeA39OsvIPZeZRgJuWqZcHVw4CqaEPK/zyyRR VBE+xmg32KuNGUIQP7Jx1iqIQOQzfKvCVaPv+mwLvKfuaOmO0JgdQb7gdtphKKqtI5yv HuOuDDIN7f+BBhLav6IFMuz8zp9dp+d2r50JaaBbQ0LJ2AO+GULWSIvHLmsL0perb9cU fyGStvJfpzbe6qFlIbKy9q3RlYnctHjl2Zd7mOlHgqHCPWUUWw6WdgoMkrP66n8PlRkl L8vA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=kd1QjyNzVstvf/hKaQSav5u6CIH1waK1YXXOi0/Q7Vg=; b=o+1Cu3nYorgI1jRoKyy2ITLXLrtCvfv1p7pgqEGzxbWCZNnJBGrcZEnXM6uV1CbX8o rwreOMHHmrsA7q7F4MQQqzUOPCSMY+FmLyMOSQ7ksg8ykBuRV3il/VBz+z/9boCJz+9W FQFOum+JBg1QQcwPGWG9yH8OE/XaXbjnn8FLU3RuEfOcnprOfNromP0SzO5Tnmo6E1lo 2zPBby2EWFIQ1Y4Z5lPRnUmqXKogdNMAdgUYtjyvYLf7CdoDKwpqtncWGVar1Sl1ILxP j3eBIIAfcUesx7wUD1C5izmSOXnQsJXtyIozctGRr9AVK2vYOMq0kLSVrKwzCYbIpenl VXQA== X-Gm-Message-State: AKwxyteNMIQ/hvFY3lgp9vW+L6DkjKBZfpGcirTLKyhxprM3JeAjWmh+ 5OvNF5aDeR6I+5pBv6JEwr8= X-Received: by 10.55.198.14 with SMTP id b14mr66250571qkj.144.1516326722473; Thu, 18 Jan 2018 17:52:02 -0800 (PST) Received: from localhost.localdomain (50-39-100-161.bvtn.or.frontiernet.net. [50.39.100.161]) by smtp.gmail.com with ESMTPSA id c127sm5483161qke.78.2018.01.18.17.51.59 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 18 Jan 2018 17:52:01 -0800 (PST) From: Ram Pai To: mpe@ellerman.id.au, mingo@redhat.com, akpm@linux-foundation.org, corbet@lwn.net, arnd@arndb.de Cc: linuxppc-dev@lists.ozlabs.org, linux-mm@kvack.org, x86@kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, dave.hansen@intel.com, benh@kernel.crashing.org, paulus@samba.org, khandual@linux.vnet.ibm.com, aneesh.kumar@linux.vnet.ibm.com, bsingharora@gmail.com, hbabu@us.ibm.com, mhocko@kernel.org, bauerman@linux.vnet.ibm.com, ebiederm@xmission.com, linuxram@us.ibm.com Subject: [PATCH v10 08/27] powerpc: implementation for arch_set_user_pkey_access() Date: Thu, 18 Jan 2018 17:50:29 -0800 Message-Id: <1516326648-22775-9-git-send-email-linuxram@us.ibm.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1516326648-22775-1-git-send-email-linuxram@us.ibm.com> References: <1516326648-22775-1-git-send-email-linuxram@us.ibm.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch provides the detailed implementation for a user to allocate a key and enable it in the hardware. It provides the plumbing, but it cannot be used till the system call is implemented. The next patch will do so. Reviewed-by: Thiago Jung Bauermann Signed-off-by: Ram Pai --- arch/powerpc/include/asm/pkeys.h | 6 ++++- arch/powerpc/mm/pkeys.c | 40 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletions(-) diff --git a/arch/powerpc/include/asm/pkeys.h b/arch/powerpc/include/asm/pkeys.h index 9964b46..2500a90 100644 --- a/arch/powerpc/include/asm/pkeys.h +++ b/arch/powerpc/include/asm/pkeys.h @@ -139,10 +139,14 @@ static inline int arch_override_mprotect_pkey(struct vm_area_struct *vma, return 0; } +extern int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey, + unsigned long init_val); static inline int arch_set_user_pkey_access(struct task_struct *tsk, int pkey, unsigned long init_val) { - return 0; + if (static_branch_likely(&pkey_disabled)) + return -EINVAL; + return __arch_set_user_pkey_access(tsk, pkey, init_val); } extern void pkey_mm_init(struct mm_struct *mm); diff --git a/arch/powerpc/mm/pkeys.c b/arch/powerpc/mm/pkeys.c index e1dc45b..eca04cd 100644 --- a/arch/powerpc/mm/pkeys.c +++ b/arch/powerpc/mm/pkeys.c @@ -9,6 +9,7 @@ * (at your option) any later version. */ +#include #include DEFINE_STATIC_KEY_TRUE(pkey_disabled); @@ -17,6 +18,9 @@ u32 initial_allocation_mask; /* Bits set for reserved keys */ #define AMR_BITS_PER_PKEY 2 +#define AMR_RD_BIT 0x1UL +#define AMR_WR_BIT 0x2UL +#define IAMR_EX_BIT 0x1UL #define PKEY_REG_BITS (sizeof(u64)*8) #define pkeyshift(pkey) (PKEY_REG_BITS - ((pkey+1) * AMR_BITS_PER_PKEY)) @@ -112,6 +116,20 @@ static inline void write_uamor(u64 value) mtspr(SPRN_UAMOR, value); } +static bool is_pkey_enabled(int pkey) +{ + u64 uamor = read_uamor(); + u64 pkey_bits = 0x3ul << pkeyshift(pkey); + u64 uamor_pkey_bits = (uamor & pkey_bits); + + /* + * Both the bits in UAMOR corresponding to the key should be set or + * reset. + */ + WARN_ON(uamor_pkey_bits && (uamor_pkey_bits != pkey_bits)); + return !!(uamor_pkey_bits); +} + static inline void init_amr(int pkey, u8 init_bits) { u64 new_amr_bits = (((u64)init_bits & 0x3UL) << pkeyshift(pkey)); @@ -154,3 +172,25 @@ void __arch_deactivate_pkey(int pkey) { pkey_status_change(pkey, false); } + +/* + * Set the access rights in AMR IAMR and UAMOR registers for @pkey to that + * specified in @init_val. + */ +int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey, + unsigned long init_val) +{ + u64 new_amr_bits = 0x0ul; + + if (!is_pkey_enabled(pkey)) + return -EINVAL; + + /* Set the bits we need in AMR: */ + if (init_val & PKEY_DISABLE_ACCESS) + new_amr_bits |= AMR_RD_BIT | AMR_WR_BIT; + else if (init_val & PKEY_DISABLE_WRITE) + new_amr_bits |= AMR_WR_BIT; + + init_amr(pkey, new_amr_bits); + return 0; +} -- 1.7.1