Received: by 10.223.176.46 with SMTP id f43csp310244wra; Thu, 18 Jan 2018 17:58:46 -0800 (PST) X-Google-Smtp-Source: ACJfBosi7TI6senp35GMRQwRI9bwzb4Qfj+zMVEwLAPfv9aAYa7sw0f5WAr3S3B8SZYy1PiuuF67 X-Received: by 10.98.218.24 with SMTP id c24mr19391159pfh.145.1516327126437; Thu, 18 Jan 2018 17:58:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516327126; cv=none; d=google.com; s=arc-20160816; b=h3bfp58gnbuzVhkCXXcDbRyc1rMK+Z1P03Juvtea4QP8ZekmrVscPAbC61Jha3Cp3n RfJBPi9zvEvcOeans15nmHzeLNzV+NsebXQZAa3CenmbZfRXk1QCZP5XN9JN7PmVIsA8 si1k45GKalZoQsTJyN+O+HN9E9aJzE9rPcHsyd0IOzyE/bq6G0ZJuz20Iur21pxMMhQg Nt6MfEk4ALC3WK/JL9lvjqYxpuJLGDTXsKbYgxQezJauf54HRZt9rGoXldbQwjQmWnom eneQxOILobESMOGqZf3Z5OkliwkzasKdj0kEwn7v8ee3pkGmxAWnBZVjhuB2IHK2bTdg 6ACA== 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=f5ZHkLqviQYhSh+k7/+1/yjJdNiFnq41CmKcHEyZxoo=; b=B98IQGbwyaOQFnhMpuJFmV1+P7sslpFsWoBME8NhB6Fx7E8P9zcBtiKwb9SxjZ1h1y YrfWRDF41w1pq8VpltMCuLYRBh9FTnI94a8jM9YQ62OPRpYEriYjRXaGkYPV7z4w9/Bw FskWuOOpPq01hQWwChN3rmvlQA37ZiXwbFSe45hlr8AAUGjGyLLY609D5uqZO7+rVHLH +O3xUWNTkmmKU3nSjuZfLFaodK8kAFJq0CBwL7bvPEgYJpDPcKdlbUdF+cTrRv7sOnTf 6ezzSGdbnZaoUab+kJWLqQurJ882beWG/LusuYlbFiOqrKnov0aXPVG+aGTQOUCVEvZE MZ5A== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=K1VCRGVs; 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 j26si7429161pga.407.2018.01.18.17.58.32; Thu, 18 Jan 2018 17:58:46 -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=K1VCRGVs; 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 S1755587AbeASB4v (ORCPT + 99 others); Thu, 18 Jan 2018 20:56:51 -0500 Received: from mail-qt0-f196.google.com ([209.85.216.196]:34852 "EHLO mail-qt0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755676AbeASBwb (ORCPT ); Thu, 18 Jan 2018 20:52:31 -0500 Received: by mail-qt0-f196.google.com with SMTP id u10so457714qtg.2; Thu, 18 Jan 2018 17:52:30 -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=f5ZHkLqviQYhSh+k7/+1/yjJdNiFnq41CmKcHEyZxoo=; b=K1VCRGVsslsjAYqV9eGNh+/zEBfJsdd1eV6V8vt+qSnvVOutUdBhcbSBpb4+XvsqSu cc4Ias9ewLnzIWQ5u2fLL5hOZqbi03ln+ExactkwOyj3fdjYDI0iX/gefesESVnjotiI 3++Lkm9DVPQ9meEbRUedvMYM4OLx3PyfDLJmxvcLLK2p+9Fq/WvOx/cuI3vso5nkcKdp pYItqh5GxKK+96HYggUXp9xumZ8rML434KZzCekNnG/G8R2UurGpm7ovbdWHAwKHxqs4 4+/WDnqWwo5Htz1yAogUul+mD8wskI+hZoYepbkFdS0pXGRfzNQHMToRRd9wiFHUNRDX 0INA== 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=f5ZHkLqviQYhSh+k7/+1/yjJdNiFnq41CmKcHEyZxoo=; b=H2LVLdfiq2dCXz7soV9Y3u0FZnaNJFTenfdxlT7Rn85nLXSUMPSy0YN0LwpdEj1ARM jIggtXY0zKfkSX6PTCmNNBXBBXdGrCfU/7DHdGLSJvzg+vXxmb0Er2BaDCbIuwjem1oT Pgs3az1t0PTEljEXp4394X4tpLwbsNB6GvvQ5DNFLdpS2rq+sRIfhIkXjPfzDwlxCWUv qDeqLfDZBhADlGCNKhI/eTyBHbe43n8iILxRbRrv9rgA6hlC/3n/WvBLNMILzevrH3Ia CoQ/mIJlQ2EKgP92YVxW7ePbIM3Z4WP/P18NGpxeqU20V6YgXRVUdT49LQj8irhjEaYw bLYA== X-Gm-Message-State: AKwxytd28NeiTpdVgZObmJvnSiw/aCuIYIEiVpEKsFbrTH6EM4rqyoLi UhKQH0Ie7b7LkoosXucSBfU= X-Received: by 10.237.41.69 with SMTP id s63mr23235782qtd.218.1516326749476; Thu, 18 Jan 2018 17:52:29 -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.52.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 18 Jan 2018 17:52:29 -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 16/27] powerpc: helper to validate key-access permissions of a pte Date: Thu, 18 Jan 2018 17:50:37 -0800 Message-Id: <1516326648-22775-17-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 helper function that checks if the read/write/execute is allowed on the pte. Signed-off-by: Ram Pai --- arch/powerpc/include/asm/book3s/64/pgtable.h | 4 +++ arch/powerpc/include/asm/pkeys.h | 9 ++++++++ arch/powerpc/mm/pkeys.c | 28 ++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 0 deletions(-) diff --git a/arch/powerpc/include/asm/book3s/64/pgtable.h b/arch/powerpc/include/asm/book3s/64/pgtable.h index e1a8bb6..e785c68 100644 --- a/arch/powerpc/include/asm/book3s/64/pgtable.h +++ b/arch/powerpc/include/asm/book3s/64/pgtable.h @@ -462,6 +462,10 @@ static inline void huge_ptep_set_wrprotect(struct mm_struct *mm, pte_update(mm, addr, ptep, 0, _PAGE_PRIVILEGED, 1); } +#ifdef CONFIG_PPC_MEM_KEYS +extern bool arch_pte_access_permitted(u64 pte, bool write, bool execute); +#endif /* CONFIG_PPC_MEM_KEYS */ + #define __HAVE_ARCH_PTEP_GET_AND_CLEAR static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep) diff --git a/arch/powerpc/include/asm/pkeys.h b/arch/powerpc/include/asm/pkeys.h index 523d66c..7c45a40 100644 --- a/arch/powerpc/include/asm/pkeys.h +++ b/arch/powerpc/include/asm/pkeys.h @@ -82,6 +82,15 @@ static inline u64 pte_to_hpte_pkey_bits(u64 pteflags) ((pteflags & H_PTE_PKEY_BIT4) ? HPTE_R_KEY_BIT4 : 0x0UL)); } +static inline u16 pte_to_pkey_bits(u64 pteflags) +{ + return (((pteflags & H_PTE_PKEY_BIT0) ? 0x10 : 0x0UL) | + ((pteflags & H_PTE_PKEY_BIT1) ? 0x8 : 0x0UL) | + ((pteflags & H_PTE_PKEY_BIT2) ? 0x4 : 0x0UL) | + ((pteflags & H_PTE_PKEY_BIT3) ? 0x2 : 0x0UL) | + ((pteflags & H_PTE_PKEY_BIT4) ? 0x1 : 0x0UL)); +} + #define pkey_alloc_mask(pkey) (0x1 << pkey) #define mm_pkey_allocation_map(mm) (mm->context.pkey_allocation_map) diff --git a/arch/powerpc/mm/pkeys.c b/arch/powerpc/mm/pkeys.c index 7630c2f..0e044ea 100644 --- a/arch/powerpc/mm/pkeys.c +++ b/arch/powerpc/mm/pkeys.c @@ -362,3 +362,31 @@ int __arch_override_mprotect_pkey(struct vm_area_struct *vma, int prot, /* Nothing to override. */ return vma_pkey(vma); } + +static bool pkey_access_permitted(int pkey, bool write, bool execute) +{ + int pkey_shift; + u64 amr; + + if (!pkey) + return true; + + if (!is_pkey_enabled(pkey)) + return true; + + pkey_shift = pkeyshift(pkey); + if (execute && !(read_iamr() & (IAMR_EX_BIT << pkey_shift))) + return true; + + amr = read_amr(); /* Delay reading amr until absolutely needed */ + return ((!write && !(amr & (AMR_RD_BIT << pkey_shift))) || + (write && !(amr & (AMR_WR_BIT << pkey_shift)))); +} + +bool arch_pte_access_permitted(u64 pte, bool write, bool execute) +{ + if (static_branch_likely(&pkey_disabled)) + return true; + + return pkey_access_permitted(pte_to_pkey_bits(pte), write, execute); +} -- 1.7.1