linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
From: Balbir Singh <bsingharora@gmail.com>
To: Ram Pai <linuxram@us.ibm.com>
Cc: mpe@ellerman.id.au, linuxppc-dev@lists.ozlabs.org,
	benh@kernel.crashing.org, paulus@samba.org,
	khandual@linux.vnet.ibm.com, aneesh.kumar@linux.vnet.ibm.com,
	hbabu@us.ibm.com, mhocko@kernel.org, bauerman@linux.vnet.ibm.com,
	ebiederm@xmission.com
Subject: Re: [PATCH 10/25] powerpc: store and restore the pkey state across context switches
Date: Thu, 19 Oct 2017 10:00:38 +1100	[thread overview]
Message-ID: <20171019100038.57ecebc2@MiWiFi-R3-srv> (raw)
In-Reply-To: <20171018204705.GF5617@ram.oc3035372033.ibm.com>

On Wed, 18 Oct 2017 13:47:05 -0700
Ram Pai <linuxram@us.ibm.com> wrote:

> On Wed, Oct 18, 2017 at 02:49:14PM +1100, Balbir Singh wrote:
> > On Fri,  8 Sep 2017 15:44:58 -0700
> > Ram Pai <linuxram@us.ibm.com> wrote:
> >   
> > > Store and restore the AMR, IAMR and UAMOR register state of the task
> > > before scheduling out and after scheduling in, respectively.
> > > 
> > > Signed-off-by: Ram Pai <linuxram@us.ibm.com>
> > > ---
> > >  arch/powerpc/include/asm/pkeys.h     |    4 +++
> > >  arch/powerpc/include/asm/processor.h |    5 ++++
> > >  arch/powerpc/kernel/process.c        |   10 ++++++++
> > >  arch/powerpc/mm/pkeys.c              |   39 ++++++++++++++++++++++++++++++++++
> > >  4 files changed, 58 insertions(+), 0 deletions(-)
> > > 
> > > diff --git a/arch/powerpc/include/asm/pkeys.h b/arch/powerpc/include/asm/pkeys.h
> > > index 7fd48a4..78c5362 100644
> > > --- a/arch/powerpc/include/asm/pkeys.h
> > > +++ b/arch/powerpc/include/asm/pkeys.h
> > > @@ -143,5 +143,9 @@ static inline void pkey_mm_init(struct mm_struct *mm)
> > >  	mm_pkey_allocation_map(mm) = initial_allocation_mask;
> > >  }
> > >  
> > > +extern void thread_pkey_regs_save(struct thread_struct *thread);
> > > +extern void thread_pkey_regs_restore(struct thread_struct *new_thread,
> > > +			struct thread_struct *old_thread);
> > > +extern void thread_pkey_regs_init(struct thread_struct *thread);
> > >  extern void pkey_initialize(void);
> > >  #endif /*_ASM_PPC64_PKEYS_H */
> > > diff --git a/arch/powerpc/include/asm/processor.h b/arch/powerpc/include/asm/processor.h
> > > index fab7ff8..de9d9ba 100644
> > > --- a/arch/powerpc/include/asm/processor.h
> > > +++ b/arch/powerpc/include/asm/processor.h
> > > @@ -309,6 +309,11 @@ struct thread_struct {
> > >  	struct thread_vr_state ckvr_state; /* Checkpointed VR state */
> > >  	unsigned long	ckvrsave; /* Checkpointed VRSAVE */
> > >  #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
> > > +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS
> > > +	unsigned long	amr;
> > > +	unsigned long	iamr;
> > > +	unsigned long	uamor;
> > > +#endif
> > >  #ifdef CONFIG_KVM_BOOK3S_32_HANDLER
> > >  	void*		kvm_shadow_vcpu; /* KVM internal data */
> > >  #endif /* CONFIG_KVM_BOOK3S_32_HANDLER */
> > > diff --git a/arch/powerpc/kernel/process.c b/arch/powerpc/kernel/process.c
> > > index a0c74bb..ba80002 100644
> > > --- a/arch/powerpc/kernel/process.c
> > > +++ b/arch/powerpc/kernel/process.c
> > > @@ -42,6 +42,7 @@
> > >  #include <linux/hw_breakpoint.h>
> > >  #include <linux/uaccess.h>
> > >  #include <linux/elf-randomize.h>
> > > +#include <linux/pkeys.h>
> > >  
> > >  #include <asm/pgtable.h>
> > >  #include <asm/io.h>
> > > @@ -1085,6 +1086,9 @@ static inline void save_sprs(struct thread_struct *t)
> > >  		t->tar = mfspr(SPRN_TAR);
> > >  	}
> > >  #endif
> > > +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS
> > > +	thread_pkey_regs_save(t);
> > > +#endif  
> > 
> > Just define two variants of thread_pkey_regs_save() based on
> > CONFIG_PPC64_MEMORY_PROTECTION_KEYS and remove the #ifdefs from process.c
> > Ditto for the lines below  
> 
> ok.
> 
> >   
> > >  }
> > >  
> > >  static inline void restore_sprs(struct thread_struct *old_thread,
> > > @@ -1120,6 +1124,9 @@ static inline void restore_sprs(struct thread_struct *old_thread,
> > >  			mtspr(SPRN_TAR, new_thread->tar);
> > >  	}
> > >  #endif
> > > +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS
> > > +	thread_pkey_regs_restore(new_thread, old_thread);
> > > +#endif  
> 
> ok.
> 
> > >  }
> > >  
> > >  #ifdef CONFIG_PPC_BOOK3S_64
> > > @@ -1705,6 +1712,9 @@ void start_thread(struct pt_regs *regs, unsigned long start, unsigned long sp)
> > >  	current->thread.tm_tfiar = 0;
> > >  	current->thread.load_tm = 0;
> > >  #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
> > > +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS
> > > +	thread_pkey_regs_init(&current->thread);
> > > +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */
> > >  }
> > >  EXPORT_SYMBOL(start_thread);
> > >  
> > > diff --git a/arch/powerpc/mm/pkeys.c b/arch/powerpc/mm/pkeys.c
> > > index 2282864..7cd1be4 100644
> > > --- a/arch/powerpc/mm/pkeys.c
> > > +++ b/arch/powerpc/mm/pkeys.c
> > > @@ -149,3 +149,42 @@ int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
> > >  	init_amr(pkey, new_amr_bits);
> > >  	return 0;
> > >  }
> > > +
> > > +void thread_pkey_regs_save(struct thread_struct *thread)
> > > +{
> > > +	if (!pkey_inited)
> > > +		return;
> > > +
> > > +	/* @TODO skip saving any registers if the thread
> > > +	 * has not used any keys yet.
> > > +	 */  
> > 
> > Comment style is broken  
> 
> ok. this time i will fix them. It misses by radar screen because
> checkpatch.pl does not complain. 
>

Yep, there is an lkml thread on this style of coding. It's
best avoided.

> >   
> > > +
> > > +	thread->amr = read_amr();
> > > +	thread->iamr = read_iamr();
> > > +	thread->uamor = read_uamor();
> > > +}
> > > +
> > > +void thread_pkey_regs_restore(struct thread_struct *new_thread,
> > > +			struct thread_struct *old_thread)
> > > +{
> > > +	if (!pkey_inited)
> > > +		return;
> > > +
> > > +	/* @TODO just reset uamor to zero if the new_thread
> > > +	 * has not used any keys yet.
> > > +	 */  
> > 
> > Comment style is broken.
> >   
> > > +
> > > +	if (old_thread->amr != new_thread->amr)
> > > +		write_amr(new_thread->amr);
> > > +	if (old_thread->iamr != new_thread->iamr)
> > > +		write_iamr(new_thread->iamr);
> > > +	if (old_thread->uamor != new_thread->uamor)
> > > +		write_uamor(new_thread->uamor);  
> > 
> > Is this order correct? Ideally, You want to write the uamor first
> > but since we are in supervisor state, I think we can get away
> > with this order.   
> 
> we could be in hypervisor state too, as is the case when we run
> a powernv kernel.
> 
> But..does it matter in which order they are written? if
> the thread is in the kernel, it cannot execute any instructions
> in userspace. So it wont see a intermediate state. right?
> or am i getting this wrong?

You are right, since uamor + amor control what can and
cannot be set, there is a subtle dependency, but it does
not apply to the kernel doing the context switch. AMR has
two SPR values, 13 and 29. I presume we are using SPR #29
here?

> 
> > Do we want to expose the uamor to user space
> > for it to modify the AMR directly?  
> 
> sorry I did not understand the comment. UAMOR cannot
> be accessed from usespace. and there are no system calls
> currently to help userspace to program the UAMOR on its
> behalf.
> 

I was just wondering how two threads can share a key if
they decide to. They would need uamor to give them permissions
to the same set of keys and then reuse the key via
pkey_mprotect(.., pkey). I am missing the bit about how
uamor's across these threads would be synchronized.


> >   
> > > +}
> > > +
> > > +void thread_pkey_regs_init(struct thread_struct *thread)
> > > +{
> > > +	write_amr(0x0ul);
> > > +	write_iamr(0x0ul);
> > > +	write_uamor(0x0ul);  
> > 
> > This is not correct, reserved keys should not be set to 0's  
> 
> ok. makes sense. best to not touch reserved key bits here.

Also this implies that at init time, the thread has access to all
keys, but it can't modify any of the keys in the AMR/IAMR.

> 
> > 
> > Balbir Singh.  
> 

  reply	other threads:[~2017-10-18 23:00 UTC|newest]

Thread overview: 134+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-08 22:44 [PATCH 0/7] powerpc: Free up RPAGE_RSV bits Ram Pai
2017-09-08 22:44 ` [PATCH 1/7] powerpc: introduce pte_set_hash_slot() helper Ram Pai
2017-09-13  7:55   ` Balbir Singh
2017-10-19  4:52   ` Michael Ellerman
2017-09-08 22:44 ` [PATCH 2/7] powerpc: introduce pte_get_hash_gslot() helper Ram Pai
2017-09-13  9:32   ` Balbir Singh
2017-09-13 20:10     ` Ram Pai
2017-09-08 22:44 ` [PATCH 3/7] powerpc: Free up four 64K PTE bits in 4K backed HPTE pages Ram Pai
2017-09-14  1:18   ` Balbir Singh
2017-10-19  3:25   ` Michael Ellerman
2017-10-19 17:02     ` Ram Pai
2017-10-23  8:47     ` Aneesh Kumar K.V
2017-10-23 16:29       ` Ram Pai
2017-10-25  9:18         ` Michael Ellerman
2017-10-26  6:08           ` Ram Pai
2017-09-08 22:44 ` [PATCH 4/7] powerpc: Free up four 64K PTE bits in 64K " Ram Pai
2017-09-14  1:44   ` Balbir Singh
2017-09-14 17:54     ` Ram Pai
2017-09-14 18:25       ` Ram Pai
2017-09-14  8:13   ` Benjamin Herrenschmidt
2017-10-23  8:52     ` Aneesh Kumar K.V
2017-10-23 23:42       ` Ram Pai
2017-10-23 19:22     ` Ram Pai
2017-10-24  3:37       ` Aneesh Kumar K.V
2017-09-08 22:44 ` [PATCH 5/7] powerpc: Swizzle around 4K PTE bits to free up bit 5 and bit 6 Ram Pai
2017-09-14  1:48   ` Balbir Singh
2017-09-14 17:23     ` Ram Pai
2017-09-08 22:44 ` [PATCH 6/7] powerpc: use helper functions to get and set hash slots Ram Pai
2017-09-08 22:44 ` [PATCH 7/7] powerpc: capture the PTE format changes in the dump pte report Ram Pai
2017-09-14  3:22   ` Balbir Singh
2017-09-14 17:19     ` Ram Pai
2017-09-08 22:44 ` [PATCH 00/25] powerpc: Memory Protection Keys Ram Pai
2017-09-08 22:44 ` [PATCH 01/25] powerpc: initial pkey plumbing Ram Pai
2017-09-14  3:32   ` Balbir Singh
2017-09-14 16:17     ` Ram Pai
2017-10-19  4:20   ` Michael Ellerman
2017-10-19 17:11     ` Ram Pai
2017-10-24  8:17       ` Michael Ellerman
2017-09-08 22:44 ` [PATCH 02/25] powerpc: define an additional vma bit for protection keys Ram Pai
2017-09-14  4:38   ` Balbir Singh
2017-09-14  8:11     ` Benjamin Herrenschmidt
2017-10-23 21:06       ` Ram Pai
2017-09-14 16:15     ` Ram Pai
2017-10-23  9:25   ` Aneesh Kumar K.V
2017-10-23  9:28     ` Aneesh Kumar K.V
2017-10-23 17:57       ` Ram Pai
2017-10-23 17:43     ` Ram Pai
2017-09-08 22:44 ` [PATCH 03/25] powerpc: track allocation status of all pkeys Ram Pai
2017-10-07 10:02   ` Michael Ellerman
2017-10-08 23:02     ` Ram Pai
2017-10-18  2:47   ` Balbir Singh
2017-10-23  9:41   ` Aneesh Kumar K.V
2017-10-23 18:14     ` Ram Pai
2017-10-24  6:28   ` Aneesh Kumar K.V
2017-10-24  7:23     ` Ram Pai
2017-09-08 22:44 ` [PATCH 04/25] powerpc: helper function to read, write AMR, IAMR, UAMOR registers Ram Pai
2017-10-18  3:17   ` [PATCH 04/25] powerpc: helper function to read,write AMR,IAMR,UAMOR registers Balbir Singh
2017-10-18  3:42     ` Ram Pai
2017-09-08 22:44 ` [PATCH 05/25] powerpc: helper functions to initialize AMR, IAMR and UAMOR registers Ram Pai
2017-10-18  3:24   ` Balbir Singh
2017-10-18 20:38     ` Ram Pai
2017-10-24  6:25   ` Aneesh Kumar K.V
2017-10-24  7:04     ` Ram Pai
2017-10-24  8:29       ` Michael Ellerman
2017-09-08 22:44 ` [PATCH 06/25] powerpc: cleaup AMR, iAMR when a key is allocated or freed Ram Pai
2017-10-18  3:34   ` [PATCH 06/25] powerpc: cleaup AMR,iAMR " Balbir Singh
2017-10-23  9:43     ` [PATCH 06/25] powerpc: cleaup AMR, iAMR " Aneesh Kumar K.V
2017-10-23 18:36       ` [PATCH 06/25] powerpc: cleaup AMR,iAMR " Ram Pai
2017-10-23  9:43   ` [PATCH 06/25] powerpc: cleaup AMR, iAMR " Aneesh Kumar K.V
2017-10-23 18:29     ` [PATCH 06/25] powerpc: cleaup AMR,iAMR " Ram Pai
2017-09-08 22:44 ` [PATCH 07/25] powerpc: implementation for arch_set_user_pkey_access() Ram Pai
2017-09-08 22:44 ` [PATCH 08/25] powerpc: sys_pkey_alloc() and sys_pkey_free() system calls Ram Pai
2017-10-24 15:48   ` Michael Ellerman
2017-10-24 18:34     ` Ram Pai
2017-10-25  9:26       ` Michael Ellerman
2017-09-08 22:44 ` [PATCH 09/25] powerpc: ability to create execute-disabled pkeys Ram Pai
2017-10-18  3:42   ` Balbir Singh
2017-10-18  5:15     ` Ram Pai
2017-10-24  6:58       ` Aneesh Kumar K.V
2017-10-24  7:20         ` Ram Pai
2017-10-24  4:36   ` Aneesh Kumar K.V
2017-10-28 23:18     ` Ram Pai
2017-09-08 22:44 ` [PATCH 10/25] powerpc: store and restore the pkey state across context switches Ram Pai
2017-10-18  3:49   ` Balbir Singh
2017-10-18 20:47     ` Ram Pai
2017-10-18 23:00       ` Balbir Singh [this message]
2017-10-19  0:52         ` Ram Pai
2017-09-08 22:44 ` [PATCH 11/25] powerpc: introduce execute-only pkey Ram Pai
2017-10-18  4:15   ` Balbir Singh
2017-10-18 20:57     ` Ram Pai
2017-10-18 23:02       ` Balbir Singh
2017-10-19 15:52         ` Ram Pai
2017-09-08 22:45 ` [PATCH 12/25] powerpc: ability to associate pkey to a vma Ram Pai
2017-10-18  4:27   ` Balbir Singh
2017-10-18 21:01     ` Ram Pai
2017-09-08 22:45 ` [PATCH 13/25] powerpc: implementation for arch_override_mprotect_pkey() Ram Pai
2017-10-18  4:36   ` Balbir Singh
2017-10-18 21:10     ` Ram Pai
2017-10-18 23:04       ` Balbir Singh
2017-10-19 16:39         ` Ram Pai
2017-09-08 22:45 ` [PATCH 14/25] powerpc: map vma key-protection bits to pte key bits Ram Pai
2017-10-18  4:39   ` Balbir Singh
2017-10-18 21:14     ` Ram Pai
2017-09-08 22:45 ` [PATCH 15/25] powerpc: sys_pkey_mprotect() system call Ram Pai
2017-09-08 22:45 ` [PATCH 16/25] powerpc: Program HPTE key protection bits Ram Pai
2017-10-18  4:43   ` Balbir Singh
2017-09-08 22:45 ` [PATCH 17/25] powerpc: helper to validate key-access permissions of a pte Ram Pai
2017-10-18  4:48   ` Balbir Singh
2017-10-18 21:19     ` Ram Pai
2017-09-08 22:45 ` [PATCH 18/25] powerpc: check key protection for user page access Ram Pai
2017-10-18 19:57   ` Balbir Singh
2017-10-18 21:29     ` Ram Pai
2017-10-18 23:08       ` Balbir Singh
2017-10-19 16:46         ` Ram Pai
2017-09-08 22:45 ` [PATCH 19/25] powerpc: implementation for arch_vma_access_permitted() Ram Pai
2017-10-18 23:20   ` Balbir Singh
2017-10-24 15:48   ` Michael Ellerman
2017-09-08 22:45 ` [PATCH 20/25] powerpc: Handle exceptions caused by pkey violation Ram Pai
2017-10-18 23:27   ` Balbir Singh
2017-10-19 16:53     ` Ram Pai
2017-10-24 15:47   ` Michael Ellerman
2017-10-24 18:26     ` Ram Pai
2017-10-29 14:03     ` Aneesh Kumar K.V
2017-10-30  0:37       ` Ram Pai
2017-09-08 22:45 ` [PATCH 21/25] powerpc: introduce get_pte_pkey() helper Ram Pai
2017-10-18 23:29   ` Balbir Singh
2017-10-19 16:55     ` Ram Pai
2017-09-08 22:45 ` [PATCH 22/25] powerpc: capture the violated protection key on fault Ram Pai
2017-10-24 15:46   ` Michael Ellerman
2017-09-08 22:45 ` [PATCH 23/25] powerpc: Deliver SEGV signal on pkey violation Ram Pai
2017-10-24 15:46   ` Michael Ellerman
2017-10-24 17:19     ` Ram Pai
2017-09-08 22:45 ` [PATCH 24/25] powerpc/ptrace: Add memory protection key regset Ram Pai
2017-09-08 22:45 ` [PATCH 25/25] powerpc: Enable pkey subsystem Ram Pai

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20171019100038.57ecebc2@MiWiFi-R3-srv \
    --to=bsingharora@gmail.com \
    --cc=aneesh.kumar@linux.vnet.ibm.com \
    --cc=bauerman@linux.vnet.ibm.com \
    --cc=benh@kernel.crashing.org \
    --cc=ebiederm@xmission.com \
    --cc=hbabu@us.ibm.com \
    --cc=khandual@linux.vnet.ibm.com \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=linuxram@us.ibm.com \
    --cc=mhocko@kernel.org \
    --cc=mpe@ellerman.id.au \
    --cc=paulus@samba.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).