Return-Path: Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 3yHGtr2yLxzDrG0 for ; Thu, 19 Oct 2017 02:58:28 +1100 (AEDT) Received: from pps.filterd (m0098393.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.21/8.16.0.21) with SMTP id v9IFulB5096750 for ; Wed, 18 Oct 2017 11:58:26 -0400 Received: from e06smtp14.uk.ibm.com (e06smtp14.uk.ibm.com [195.75.94.110]) by mx0a-001b2d01.pphosted.com with ESMTP id 2dp6x8uxfw-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Wed, 18 Oct 2017 11:58:25 -0400 Received: from localhost by e06smtp14.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 18 Oct 2017 16:58:22 +0100 Subject: Re: [RFC v7 12/25] powerpc: implementation for arch_override_mprotect_pkey() To: Ram Pai , linuxppc-dev@lists.ozlabs.org Cc: mhocko@kernel.org, paulus@samba.org, aneesh.kumar@linux.vnet.ibm.com, bauerman@linux.vnet.ibm.com, khandual@linux.vnet.ibm.com References: <1501459946-11619-1-git-send-email-linuxram@us.ibm.com> <1501459946-11619-13-git-send-email-linuxram@us.ibm.com> From: Laurent Dufour Date: Wed, 18 Oct 2017 17:58:18 +0200 MIME-Version: 1.0 In-Reply-To: <1501459946-11619-13-git-send-email-linuxram@us.ibm.com> Content-Type: text/plain; charset=utf-8 Message-Id: <94ca6bc3-14da-e171-1ab6-5bea5fb71650@linux.vnet.ibm.com> List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Hi Ram, On 31/07/2017 02:12, Ram Pai wrote: > arch independent code calls arch_override_mprotect_pkey() > to return a pkey that best matches the requested protection. > > This patch provides the implementation. > > Signed-off-by: Ram Pai > --- > arch/powerpc/include/asm/mmu_context.h | 5 +++ > arch/powerpc/include/asm/pkeys.h | 17 ++++++++++- > arch/powerpc/mm/pkeys.c | 47 ++++++++++++++++++++++++++++++++ > 3 files changed, 67 insertions(+), 2 deletions(-) > > diff --git a/arch/powerpc/include/asm/mmu_context.h b/arch/powerpc/include/asm/mmu_context.h > index 4705dab..7232484 100644 > --- a/arch/powerpc/include/asm/mmu_context.h > +++ b/arch/powerpc/include/asm/mmu_context.h > @@ -185,6 +185,11 @@ static inline bool arch_vma_access_permitted(struct vm_area_struct *vma, > #ifndef CONFIG_PPC64_MEMORY_PROTECTION_KEYS > #define pkey_initialize() > #define pkey_mm_init(mm) > + > +static inline int vma_pkey(struct vm_area_struct *vma) > +{ > + return 0; > +} > #endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ > > #endif /* __KERNEL__ */ > diff --git a/arch/powerpc/include/asm/pkeys.h b/arch/powerpc/include/asm/pkeys.h > index a715a08..03f7ea2 100644 > --- a/arch/powerpc/include/asm/pkeys.h > +++ b/arch/powerpc/include/asm/pkeys.h > @@ -46,6 +46,16 @@ static inline u64 pkey_to_vmflag_bits(u16 pkey) > ((pkey & 0x10UL) ? VM_PKEY_BIT4 : 0x0UL)); > } > > +#define ARCH_VM_PKEY_FLAGS (VM_PKEY_BIT0 | VM_PKEY_BIT1 | VM_PKEY_BIT2 | \ > + VM_PKEY_BIT3 | VM_PKEY_BIT4) > + > +static inline int vma_pkey(struct vm_area_struct *vma) > +{ > + if (!pkey_inited) > + return 0; > + return (vma->vm_flags & ARCH_VM_PKEY_FLAGS) >> VM_PKEY_SHIFT; > +} > + > #define arch_max_pkey() pkeys_total > #define AMR_RD_BIT 0x1UL > #define AMR_WR_BIT 0x2UL > @@ -146,11 +156,14 @@ static inline int execute_only_pkey(struct mm_struct *mm) > return __execute_only_pkey(mm); > } > > - > +extern int __arch_override_mprotect_pkey(struct vm_area_struct *vma, > + int prot, int pkey); > static inline int arch_override_mprotect_pkey(struct vm_area_struct *vma, > int prot, int pkey) > { > - return 0; > + if (!pkey_inited) > + return 0; > + return __arch_override_mprotect_pkey(vma, prot, pkey); > } > > extern int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey, > diff --git a/arch/powerpc/mm/pkeys.c b/arch/powerpc/mm/pkeys.c > index 25749bf..edcbf48 100644 > --- a/arch/powerpc/mm/pkeys.c > +++ b/arch/powerpc/mm/pkeys.c > @@ -154,3 +154,50 @@ int __execute_only_pkey(struct mm_struct *mm) > mm->context.execute_only_pkey = execute_only_pkey; > return execute_only_pkey; > } > + > +static inline bool vma_is_pkey_exec_only(struct vm_area_struct *vma) > +{ > + /* Do this check first since the vm_flags should be hot */ > + if ((vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)) != VM_EXEC) > + return false; > + > + return (vma_pkey(vma) == vma->vm_mm->context.execute_only_pkey); > +} > + > +/* > + * This should only be called for *plain* mprotect calls. > + */ > +int __arch_override_mprotect_pkey(struct vm_area_struct *vma, int prot, > + int pkey) > +{ > + /* > + * Is this an mprotect_pkey() call? If so, never > + * override the value that came from the user. > + */ > + if (pkey != -1) > + return pkey; This check should be moved in arch_override_mprotect_pkey() in arch/powerpc/include/asm/pkeys.h > + > + /* > + * If the currently associated pkey is execute-only, > + * but the requested protection requires read or write, > + * move it back to the default pkey. > + */ > + if (vma_is_pkey_exec_only(vma) && > + (prot & (PROT_READ|PROT_WRITE))) > + return 0; > + > + /* > + * the requested protection is execute-only. Hence > + * lets use a execute-only pkey. > + */ > + if (prot == PROT_EXEC) { > + pkey = execute_only_pkey(vma->vm_mm); > + if (pkey > 0) > + return pkey; > + } > + > + /* > + * nothing to override. > + */ > + return vma_pkey(vma); > +} >