linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] x86, powerpc : pkey-mprotect must allow pkey-0
@ 2018-03-09  8:12 Ram Pai
  2018-03-09  8:37 ` Balbir Singh
                   ` (4 more replies)
  0 siblings, 5 replies; 16+ messages in thread
From: Ram Pai @ 2018-03-09  8:12 UTC (permalink / raw)
  To: mpe, mingo, akpm
  Cc: linuxppc-dev, linux-mm, x86, linux-arch, linux-kernel,
	dave.hansen, benh, paulus, khandual, aneesh.kumar, bsingharora,
	hbabu, mhocko, bauerman, ebiederm, linuxram, corbet, arnd,
	fweimer, msuchanek, Ulrich.Weigand

Once an address range is associated with an allocated pkey, it cannot be
reverted back to key-0. There is no valid reason for the above behavior.  On
the contrary applications need the ability to do so.

The patch relaxes the restriction.

Tested on powerpc and x86_64.

cc: Dave Hansen <dave.hansen@intel.com>
cc: Michael Ellermen <mpe@ellerman.id.au>
cc: Ingo Molnar <mingo@kernel.org>
Signed-off-by: Ram Pai <linuxram@us.ibm.com>
---
 arch/powerpc/include/asm/pkeys.h | 19 ++++++++++++++-----
 arch/x86/include/asm/pkeys.h     |  5 +++--
 2 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/arch/powerpc/include/asm/pkeys.h b/arch/powerpc/include/asm/pkeys.h
index 0409c80..3e8abe4 100644
--- a/arch/powerpc/include/asm/pkeys.h
+++ b/arch/powerpc/include/asm/pkeys.h
@@ -101,10 +101,18 @@ static inline u16 pte_to_pkey_bits(u64 pteflags)
 
 static inline bool mm_pkey_is_allocated(struct mm_struct *mm, int pkey)
 {
-	/* A reserved key is never considered as 'explicitly allocated' */
-	return ((pkey < arch_max_pkey()) &&
-		!__mm_pkey_is_reserved(pkey) &&
-		__mm_pkey_is_allocated(mm, pkey));
+	/* pkey 0 is allocated by default. */
+	if (!pkey)
+	       return true;
+
+	if (pkey < 0 || pkey >= arch_max_pkey())
+	       return false;
+
+	/* reserved keys are never allocated. */
+	if (__mm_pkey_is_reserved(pkey))
+	       return false;
+
+	return(__mm_pkey_is_allocated(mm, pkey));
 }
 
 extern void __arch_activate_pkey(int pkey);
@@ -150,7 +158,8 @@ static inline int mm_pkey_free(struct mm_struct *mm, int pkey)
 	if (static_branch_likely(&pkey_disabled))
 		return -1;
 
-	if (!mm_pkey_is_allocated(mm, pkey))
+	/* pkey 0 cannot be freed */
+	if (!pkey || !mm_pkey_is_allocated(mm, pkey))
 		return -EINVAL;
 
 	/*
diff --git a/arch/x86/include/asm/pkeys.h b/arch/x86/include/asm/pkeys.h
index a0ba1ff..6ea7486 100644
--- a/arch/x86/include/asm/pkeys.h
+++ b/arch/x86/include/asm/pkeys.h
@@ -52,7 +52,7 @@ bool mm_pkey_is_allocated(struct mm_struct *mm, int pkey)
 	 * from pkey_alloc().  pkey 0 is special, and never
 	 * returned from pkey_alloc().
 	 */
-	if (pkey <= 0)
+	if (pkey < 0)
 		return false;
 	if (pkey >= arch_max_pkey())
 		return false;
@@ -92,7 +92,8 @@ int mm_pkey_alloc(struct mm_struct *mm)
 static inline
 int mm_pkey_free(struct mm_struct *mm, int pkey)
 {
-	if (!mm_pkey_is_allocated(mm, pkey))
+	/* pkey 0 is special and can never be freed */
+	if (!pkey || !mm_pkey_is_allocated(mm, pkey))
 		return -EINVAL;
 
 	mm_set_pkey_free(mm, pkey);
-- 
1.8.3.1

^ permalink raw reply related	[flat|nested] 16+ messages in thread

* Re: [PATCH] x86, powerpc : pkey-mprotect must allow pkey-0
  2018-03-09  8:12 [PATCH] x86, powerpc : pkey-mprotect must allow pkey-0 Ram Pai
@ 2018-03-09  8:37 ` Balbir Singh
  2018-03-09 19:54   ` Ram Pai
  2018-03-09  8:43 ` Ingo Molnar
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 16+ messages in thread
From: Balbir Singh @ 2018-03-09  8:37 UTC (permalink / raw)
  To: Ram Pai
  Cc: Michael Ellerman, Ingo Molnar, akpm,
	open list:LINUX FOR POWERPC (32-BIT AND 64-BIT),
	linux-mm, maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT),
	linux-arch, linux-kernel, Dave Hansen, Benjamin Herrenschmidt,
	Paul Mackerras, Anshuman Khandual, Aneesh Kumar KV,
	Haren Myneni/Beaverton/IBM, Michal Hocko, Thiago Jung Bauermann,
	Eric W. Biederman, Jonathan Corbet, Arnd Bergmann, fweimer,
	msuchanek, Ulrich.Weigand

On Fri, Mar 9, 2018 at 7:12 PM, Ram Pai <linuxram@us.ibm.com> wrote:
> Once an address range is associated with an allocated pkey, it cannot be
> reverted back to key-0. There is no valid reason for the above behavior.  On
> the contrary applications need the ability to do so.
>
> The patch relaxes the restriction.

I looked at the code and my observation was going to be that we need
to change mm_pkey_is_allocated. I still fail to understand what
happens if pkey 0 is reserved? What is the default key is it the first
available key? Assuming 0 is the default key may work and seems to
work, but I am sure its mostly by accident. It would be nice, if we
could have  a notion of the default key. I don't like the special
meaning given to key 0 here. Remember on powerpc if 0 is reserved and
UAMOR/AMOR does not allow modification because it's reserved, setting
0 will still fail

Balbir

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH] x86, powerpc : pkey-mprotect must allow pkey-0
  2018-03-09  8:12 [PATCH] x86, powerpc : pkey-mprotect must allow pkey-0 Ram Pai
  2018-03-09  8:37 ` Balbir Singh
@ 2018-03-09  8:43 ` Ingo Molnar
  2018-03-09 20:09   ` Ram Pai
  2018-03-09 10:19 ` Michael Ellerman
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 16+ messages in thread
From: Ingo Molnar @ 2018-03-09  8:43 UTC (permalink / raw)
  To: Ram Pai
  Cc: mpe, mingo, akpm, linuxppc-dev, linux-mm, x86, linux-arch,
	linux-kernel, dave.hansen, benh, paulus, khandual, aneesh.kumar,
	bsingharora, hbabu, mhocko, bauerman, ebiederm, corbet, arnd,
	fweimer, msuchanek, Ulrich.Weigand


* Ram Pai <linuxram@us.ibm.com> wrote:

> Once an address range is associated with an allocated pkey, it cannot be
> reverted back to key-0. There is no valid reason for the above behavior.  On
> the contrary applications need the ability to do so.
> 
> The patch relaxes the restriction.
> 
> Tested on powerpc and x86_64.
> 
> cc: Dave Hansen <dave.hansen@intel.com>
> cc: Michael Ellermen <mpe@ellerman.id.au>
> cc: Ingo Molnar <mingo@kernel.org>
> Signed-off-by: Ram Pai <linuxram@us.ibm.com>
> ---
>  arch/powerpc/include/asm/pkeys.h | 19 ++++++++++++++-----
>  arch/x86/include/asm/pkeys.h     |  5 +++--
>  2 files changed, 17 insertions(+), 7 deletions(-)
> 
> diff --git a/arch/powerpc/include/asm/pkeys.h b/arch/powerpc/include/asm/pkeys.h
> index 0409c80..3e8abe4 100644
> --- a/arch/powerpc/include/asm/pkeys.h
> +++ b/arch/powerpc/include/asm/pkeys.h
> @@ -101,10 +101,18 @@ static inline u16 pte_to_pkey_bits(u64 pteflags)
>  
>  static inline bool mm_pkey_is_allocated(struct mm_struct *mm, int pkey)
>  {
> -	/* A reserved key is never considered as 'explicitly allocated' */
> -	return ((pkey < arch_max_pkey()) &&
> -		!__mm_pkey_is_reserved(pkey) &&
> -		__mm_pkey_is_allocated(mm, pkey));
> +	/* pkey 0 is allocated by default. */
> +	if (!pkey)
> +	       return true;
> +
> +	if (pkey < 0 || pkey >= arch_max_pkey())
> +	       return false;
> +
> +	/* reserved keys are never allocated. */
> +	if (__mm_pkey_is_reserved(pkey))
> +	       return false;

Please capitalize in comments consistently, i.e.:

	/* Reserved keys are never allocated: */

> +
> +	return(__mm_pkey_is_allocated(mm, pkey));

'return' is not a function.

Thanks,

	Ingo

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH] x86, powerpc : pkey-mprotect must allow pkey-0
  2018-03-09  8:12 [PATCH] x86, powerpc : pkey-mprotect must allow pkey-0 Ram Pai
  2018-03-09  8:37 ` Balbir Singh
  2018-03-09  8:43 ` Ingo Molnar
@ 2018-03-09 10:19 ` Michael Ellerman
  2018-03-09 20:06   ` Ram Pai
  2018-03-09 11:04 ` Florian Weimer
  2018-03-09 22:40 ` Dave Hansen
  4 siblings, 1 reply; 16+ messages in thread
From: Michael Ellerman @ 2018-03-09 10:19 UTC (permalink / raw)
  To: Ram Pai, mingo, akpm
  Cc: linuxppc-dev, linux-mm, x86, linux-arch, linux-kernel,
	dave.hansen, benh, paulus, khandual, aneesh.kumar, bsingharora,
	hbabu, mhocko, bauerman, ebiederm, linuxram, corbet, arnd,
	fweimer, msuchanek, Ulrich.Weigand

Ram Pai <linuxram@us.ibm.com> writes:

> Once an address range is associated with an allocated pkey, it cannot be
> reverted back to key-0. There is no valid reason for the above behavior.  On
> the contrary applications need the ability to do so.

Please explain this in much more detail. Is it an ABI change?

And why did we just notice this?

> The patch relaxes the restriction.
>
> Tested on powerpc and x86_64.

Thanks, but please split the patch, one for each arch.

cheers

> diff --git a/arch/powerpc/include/asm/pkeys.h b/arch/powerpc/include/asm/pkeys.h
> index 0409c80..3e8abe4 100644
> --- a/arch/powerpc/include/asm/pkeys.h
> +++ b/arch/powerpc/include/asm/pkeys.h
> @@ -101,10 +101,18 @@ static inline u16 pte_to_pkey_bits(u64 pteflags)
>  
>  static inline bool mm_pkey_is_allocated(struct mm_struct *mm, int pkey)
>  {
> -	/* A reserved key is never considered as 'explicitly allocated' */
> -	return ((pkey < arch_max_pkey()) &&
> -		!__mm_pkey_is_reserved(pkey) &&
> -		__mm_pkey_is_allocated(mm, pkey));
> +	/* pkey 0 is allocated by default. */
> +	if (!pkey)
> +	       return true;
> +
> +	if (pkey < 0 || pkey >= arch_max_pkey())
> +	       return false;
> +
> +	/* reserved keys are never allocated. */
> +	if (__mm_pkey_is_reserved(pkey))
> +	       return false;
> +
> +	return(__mm_pkey_is_allocated(mm, pkey));
>  }
>  
>  extern void __arch_activate_pkey(int pkey);
> @@ -150,7 +158,8 @@ static inline int mm_pkey_free(struct mm_struct *mm, int pkey)
>  	if (static_branch_likely(&pkey_disabled))
>  		return -1;
>  
> -	if (!mm_pkey_is_allocated(mm, pkey))
> +	/* pkey 0 cannot be freed */
> +	if (!pkey || !mm_pkey_is_allocated(mm, pkey))
>  		return -EINVAL;
>  
>  	/*
> diff --git a/arch/x86/include/asm/pkeys.h b/arch/x86/include/asm/pkeys.h
> index a0ba1ff..6ea7486 100644
> --- a/arch/x86/include/asm/pkeys.h
> +++ b/arch/x86/include/asm/pkeys.h
> @@ -52,7 +52,7 @@ bool mm_pkey_is_allocated(struct mm_struct *mm, int pkey)
>  	 * from pkey_alloc().  pkey 0 is special, and never
>  	 * returned from pkey_alloc().
>  	 */
> -	if (pkey <= 0)
> +	if (pkey < 0)
>  		return false;
>  	if (pkey >= arch_max_pkey())
>  		return false;
> @@ -92,7 +92,8 @@ int mm_pkey_alloc(struct mm_struct *mm)
>  static inline
>  int mm_pkey_free(struct mm_struct *mm, int pkey)
>  {
> -	if (!mm_pkey_is_allocated(mm, pkey))
> +	/* pkey 0 is special and can never be freed */
> +	if (!pkey || !mm_pkey_is_allocated(mm, pkey))
>  		return -EINVAL;
>  
>  	mm_set_pkey_free(mm, pkey);
> -- 
> 1.8.3.1

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH] x86, powerpc : pkey-mprotect must allow pkey-0
  2018-03-09  8:12 [PATCH] x86, powerpc : pkey-mprotect must allow pkey-0 Ram Pai
                   ` (2 preceding siblings ...)
  2018-03-09 10:19 ` Michael Ellerman
@ 2018-03-09 11:04 ` Florian Weimer
  2018-03-09 20:00   ` Ram Pai
  2018-03-09 22:40 ` Dave Hansen
  4 siblings, 1 reply; 16+ messages in thread
From: Florian Weimer @ 2018-03-09 11:04 UTC (permalink / raw)
  To: Ram Pai, mpe, mingo, akpm
  Cc: linuxppc-dev, linux-mm, x86, linux-arch, linux-kernel,
	dave.hansen, benh, paulus, khandual, aneesh.kumar, bsingharora,
	hbabu, mhocko, bauerman, ebiederm, corbet, arnd, msuchanek,
	Ulrich.Weigand

On 03/09/2018 09:12 AM, Ram Pai wrote:
> Once an address range is associated with an allocated pkey, it cannot be
> reverted back to key-0. There is no valid reason for the above behavior.

mprotect without a key does not necessarily use key 0, e.g. if 
protection keys are used to emulate page protection flag combination 
which is not directly supported by the hardware.

Therefore, it seems to me that filtering out non-allocated keys is the 
right thing to do.

Thanks,
Florian

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH] x86, powerpc : pkey-mprotect must allow pkey-0
  2018-03-09  8:37 ` Balbir Singh
@ 2018-03-09 19:54   ` Ram Pai
  0 siblings, 0 replies; 16+ messages in thread
From: Ram Pai @ 2018-03-09 19:54 UTC (permalink / raw)
  To: Balbir Singh
  Cc: Michael Ellerman, Ingo Molnar, akpm,
	open list:LINUX FOR POWERPC (32-BIT AND 64-BIT),
	linux-mm, maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT),
	linux-arch, linux-kernel, Dave Hansen, Benjamin Herrenschmidt,
	Paul Mackerras, Anshuman Khandual, Aneesh Kumar KV,
	Haren Myneni/Beaverton/IBM, Michal Hocko, Thiago Jung Bauermann,
	Eric W. Biederman, Jonathan Corbet, Arnd Bergmann, fweimer,
	msuchanek, Ulrich.Weigand

On Fri, Mar 09, 2018 at 07:37:04PM +1100, Balbir Singh wrote:
> On Fri, Mar 9, 2018 at 7:12 PM, Ram Pai <linuxram@us.ibm.com> wrote:
> > Once an address range is associated with an allocated pkey, it cannot be
> > reverted back to key-0. There is no valid reason for the above behavior.  On
> > the contrary applications need the ability to do so.
> >
> > The patch relaxes the restriction.
> 
> I looked at the code and my observation was going to be that we need
> to change mm_pkey_is_allocated. I still fail to understand what
> happens if pkey 0 is reserved? What is the default key is it the first
> available key? Assuming 0 is the default key may work and seems to
> work, but I am sure its mostly by accident. It would be nice, if we
> could have  a notion of the default key. I don't like the special
> meaning given to key 0 here. Remember on powerpc if 0 is reserved and
> UAMOR/AMOR does not allow modification because it's reserved, setting
> 0 will still fail

The linux pkey API, assumes pkey-0 is the default key. If no key is
explicitly associated with a page, the default key gets associated.
When a default key gets associated with a page, the permissions on the
page are not dictated by the permissions of the default key, but by the
permission of other bits in the pte; i.e _PAGE_RWX.

On powerpc, and AFAICT on x86, neither the hardware nor the hypervisor
reserves key-0. Hence the OS is free to use the key value, the
way it chooses. On Linux we choose to associate key-0 the special status
called default-key.

However I see your point. If some cpu architecture takes away key-0 from
Linux, than implementing the special status for key-0 on that
architecture can become challenging, though not impossible. That
architecture implementation can internally map key-0 value to some other
available key, and associate that key to the page. And offcourse make
sure that the hardware/MMU uses the pte's RWX bits to enforce
permissions, for that key.


-- 
Ram Pai

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH] x86, powerpc : pkey-mprotect must allow pkey-0
  2018-03-09 11:04 ` Florian Weimer
@ 2018-03-09 20:00   ` Ram Pai
  2018-03-14  8:00     ` Florian Weimer
  0 siblings, 1 reply; 16+ messages in thread
From: Ram Pai @ 2018-03-09 20:00 UTC (permalink / raw)
  To: Florian Weimer
  Cc: mpe, mingo, akpm, linuxppc-dev, linux-mm, x86, linux-arch,
	linux-kernel, dave.hansen, benh, paulus, khandual, aneesh.kumar,
	bsingharora, hbabu, mhocko, bauerman, ebiederm, corbet, arnd,
	msuchanek, Ulrich.Weigand

On Fri, Mar 09, 2018 at 12:04:49PM +0100, Florian Weimer wrote:
> On 03/09/2018 09:12 AM, Ram Pai wrote:
> >Once an address range is associated with an allocated pkey, it cannot be
> >reverted back to key-0. There is no valid reason for the above behavior.
> 
> mprotect without a key does not necessarily use key 0, e.g. if
> protection keys are used to emulate page protection flag combination
> which is not directly supported by the hardware.
> 
> Therefore, it seems to me that filtering out non-allocated keys is
> the right thing to do.

I am not sure, what you mean. Do you agree with the patch or otherwise?
RP

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH] x86, powerpc : pkey-mprotect must allow pkey-0
  2018-03-09 10:19 ` Michael Ellerman
@ 2018-03-09 20:06   ` Ram Pai
  2018-03-12 15:46     ` Dave Hansen
  0 siblings, 1 reply; 16+ messages in thread
From: Ram Pai @ 2018-03-09 20:06 UTC (permalink / raw)
  To: Michael Ellerman
  Cc: mingo, akpm, linuxppc-dev, linux-mm, x86, linux-arch,
	linux-kernel, dave.hansen, benh, paulus, khandual, aneesh.kumar,
	bsingharora, hbabu, mhocko, bauerman, ebiederm, corbet, arnd,
	fweimer, msuchanek, Ulrich.Weigand

On Fri, Mar 09, 2018 at 09:19:53PM +1100, Michael Ellerman wrote:
> Ram Pai <linuxram@us.ibm.com> writes:
> 
> > Once an address range is associated with an allocated pkey, it cannot be
> > reverted back to key-0. There is no valid reason for the above behavior.  On
> > the contrary applications need the ability to do so.
> 
> Please explain this in much more detail. Is it an ABI change?

Not necessarily an ABI change. older binary applications  will continue
to work. It can be considered as a bug-fix.

> 
> And why did we just notice this?

Yes. this was noticed by an application vendor.

> 
> > The patch relaxes the restriction.
> >
> > Tested on powerpc and x86_64.
> 
> Thanks, but please split the patch, one for each arch.

Will do.
RP

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH] x86, powerpc : pkey-mprotect must allow pkey-0
  2018-03-09  8:43 ` Ingo Molnar
@ 2018-03-09 20:09   ` Ram Pai
  0 siblings, 0 replies; 16+ messages in thread
From: Ram Pai @ 2018-03-09 20:09 UTC (permalink / raw)
  To: Ingo Molnar
  Cc: mpe, mingo, akpm, linuxppc-dev, linux-mm, x86, linux-arch,
	linux-kernel, dave.hansen, benh, paulus, khandual, aneesh.kumar,
	bsingharora, hbabu, mhocko, bauerman, ebiederm, corbet, arnd,
	fweimer, msuchanek, Ulrich.Weigand

On Fri, Mar 09, 2018 at 09:43:32AM +0100, Ingo Molnar wrote:
> 
> * Ram Pai <linuxram@us.ibm.com> wrote:
> 
> > Once an address range is associated with an allocated pkey, it cannot be
> > reverted back to key-0. There is no valid reason for the above behavior.  On
> > the contrary applications need the ability to do so.
> > 
> > The patch relaxes the restriction.
> > 
> > Tested on powerpc and x86_64.
> > 
> > cc: Dave Hansen <dave.hansen@intel.com>
> > cc: Michael Ellermen <mpe@ellerman.id.au>
> > cc: Ingo Molnar <mingo@kernel.org>
> > Signed-off-by: Ram Pai <linuxram@us.ibm.com>
> > ---
> >  arch/powerpc/include/asm/pkeys.h | 19 ++++++++++++++-----
> >  arch/x86/include/asm/pkeys.h     |  5 +++--
> >  2 files changed, 17 insertions(+), 7 deletions(-)
> > 
> > diff --git a/arch/powerpc/include/asm/pkeys.h b/arch/powerpc/include/asm/pkeys.h
> > index 0409c80..3e8abe4 100644
> > --- a/arch/powerpc/include/asm/pkeys.h
> > +++ b/arch/powerpc/include/asm/pkeys.h
> > @@ -101,10 +101,18 @@ static inline u16 pte_to_pkey_bits(u64 pteflags)
> >  
> >  static inline bool mm_pkey_is_allocated(struct mm_struct *mm, int pkey)
> >  {
> > -	/* A reserved key is never considered as 'explicitly allocated' */
> > -	return ((pkey < arch_max_pkey()) &&
> > -		!__mm_pkey_is_reserved(pkey) &&
> > -		__mm_pkey_is_allocated(mm, pkey));
> > +	/* pkey 0 is allocated by default. */
> > +	if (!pkey)
> > +	       return true;
> > +
> > +	if (pkey < 0 || pkey >= arch_max_pkey())
> > +	       return false;
> > +
> > +	/* reserved keys are never allocated. */
> > +	if (__mm_pkey_is_reserved(pkey))
> > +	       return false;
> 
> Please capitalize in comments consistently, i.e.:

ok.

> 
> 	/* Reserved keys are never allocated: */
> 
> > +
> > +	return(__mm_pkey_is_allocated(mm, pkey));
> 
> 'return' is not a function.

right. will fix.

Thanks,
RP

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH] x86, powerpc : pkey-mprotect must allow pkey-0
  2018-03-09  8:12 [PATCH] x86, powerpc : pkey-mprotect must allow pkey-0 Ram Pai
                   ` (3 preceding siblings ...)
  2018-03-09 11:04 ` Florian Weimer
@ 2018-03-09 22:40 ` Dave Hansen
  2018-03-10  5:55   ` Ram Pai
  4 siblings, 1 reply; 16+ messages in thread
From: Dave Hansen @ 2018-03-09 22:40 UTC (permalink / raw)
  To: Ram Pai, mpe, mingo, akpm
  Cc: linuxppc-dev, linux-mm, x86, linux-arch, linux-kernel, benh,
	paulus, khandual, aneesh.kumar, bsingharora, hbabu, mhocko,
	bauerman, ebiederm, corbet, arnd, fweimer, msuchanek,
	Ulrich.Weigand

On 03/09/2018 12:12 AM, Ram Pai wrote:
> Once an address range is associated with an allocated pkey, it cannot be
> reverted back to key-0. There is no valid reason for the above behavior.  On
> the contrary applications need the ability to do so.

Why don't we just set pkey 0 to be allocated in the allocation bitmap by
default?

We *could* also just not let it be special and let it be freed.  An app
could theoretically be careful and make sure nothing is using it.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH] x86, powerpc : pkey-mprotect must allow pkey-0
  2018-03-09 22:40 ` Dave Hansen
@ 2018-03-10  5:55   ` Ram Pai
  2018-03-10  6:50     ` Dave Hansen
  0 siblings, 1 reply; 16+ messages in thread
From: Ram Pai @ 2018-03-10  5:55 UTC (permalink / raw)
  To: Dave Hansen
  Cc: mpe, mingo, akpm, linuxppc-dev, linux-mm, x86, linux-arch,
	linux-kernel, benh, paulus, khandual, aneesh.kumar, bsingharora,
	hbabu, mhocko, bauerman, ebiederm, corbet, arnd, fweimer,
	msuchanek, Ulrich.Weigand

On Fri, Mar 09, 2018 at 02:40:32PM -0800, Dave Hansen wrote:
> On 03/09/2018 12:12 AM, Ram Pai wrote:
> > Once an address range is associated with an allocated pkey, it cannot be
> > reverted back to key-0. There is no valid reason for the above behavior.  On
> > the contrary applications need the ability to do so.
> 
> Why don't we just set pkey 0 to be allocated in the allocation bitmap by
> default?

ok. that will make it allocatable. But it will not be associatable,
given the bug in the current code. And what will be the
default key associated with a pte? zero? or something else?

> 
> We *could* also just not let it be special and let it be freed.  An app
> could theoretically be careful and make sure nothing is using it.

unable to see how this solves the problem. Need some more explaination.


RP

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH] x86, powerpc : pkey-mprotect must allow pkey-0
  2018-03-10  5:55   ` Ram Pai
@ 2018-03-10  6:50     ` Dave Hansen
  0 siblings, 0 replies; 16+ messages in thread
From: Dave Hansen @ 2018-03-10  6:50 UTC (permalink / raw)
  To: Ram Pai
  Cc: mpe, mingo, akpm, linuxppc-dev, linux-mm, x86, linux-arch,
	linux-kernel, benh, paulus, khandual, aneesh.kumar, bsingharora,
	hbabu, mhocko, bauerman, ebiederm, corbet, arnd, fweimer,
	msuchanek, Ulrich.Weigand

On 03/09/2018 09:55 PM, Ram Pai wrote:
> On Fri, Mar 09, 2018 at 02:40:32PM -0800, Dave Hansen wrote:
>> On 03/09/2018 12:12 AM, Ram Pai wrote:
>>> Once an address range is associated with an allocated pkey, it cannot be
>>> reverted back to key-0. There is no valid reason for the above behavior.  On
>>> the contrary applications need the ability to do so.
>> Why don't we just set pkey 0 to be allocated in the allocation bitmap by
>> default?
> ok. that will make it allocatable. But it will not be associatable,
> given the bug in the current code. And what will be the
> default key associated with a pte? zero? or something else?

I'm just saying that I think we should try to keep from making it
special as much as possible.

Let's fix the bug that keeps it from being associatable.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH] x86, powerpc : pkey-mprotect must allow pkey-0
  2018-03-09 20:06   ` Ram Pai
@ 2018-03-12 15:46     ` Dave Hansen
  0 siblings, 0 replies; 16+ messages in thread
From: Dave Hansen @ 2018-03-12 15:46 UTC (permalink / raw)
  To: Ram Pai, Michael Ellerman
  Cc: mingo, akpm, linuxppc-dev, linux-mm, x86, linux-arch,
	linux-kernel, benh, paulus, khandual, aneesh.kumar, bsingharora,
	hbabu, mhocko, bauerman, ebiederm, corbet, arnd, fweimer,
	msuchanek, Ulrich.Weigand

On 03/09/2018 12:06 PM, Ram Pai wrote:
> On Fri, Mar 09, 2018 at 09:19:53PM +1100, Michael Ellerman wrote:
>> Ram Pai <linuxram@us.ibm.com> writes:
>>
>>> Once an address range is associated with an allocated pkey, it cannot be
>>> reverted back to key-0. There is no valid reason for the above behavior.  On
>>> the contrary applications need the ability to do so.
>> Please explain this in much more detail. Is it an ABI change?
> Not necessarily an ABI change. older binary applications  will continue
> to work. It can be considered as a bug-fix.

Yeah, agreed.  I do not think this is an ABI change.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH] x86, powerpc : pkey-mprotect must allow pkey-0
  2018-03-09 20:00   ` Ram Pai
@ 2018-03-14  8:00     ` Florian Weimer
  2018-03-14  8:05       ` Florian Weimer
  2018-03-14 14:08       ` Dave Hansen
  0 siblings, 2 replies; 16+ messages in thread
From: Florian Weimer @ 2018-03-14  8:00 UTC (permalink / raw)
  To: Ram Pai
  Cc: mpe, mingo, akpm, linuxppc-dev, linux-mm, x86, linux-arch,
	linux-kernel, dave.hansen, benh, paulus, khandual, aneesh.kumar,
	bsingharora, hbabu, mhocko, bauerman, ebiederm, corbet, arnd,
	msuchanek, Ulrich.Weigand

On 03/09/2018 09:00 PM, Ram Pai wrote:
> On Fri, Mar 09, 2018 at 12:04:49PM +0100, Florian Weimer wrote:
>> On 03/09/2018 09:12 AM, Ram Pai wrote:
>>> Once an address range is associated with an allocated pkey, it cannot be
>>> reverted back to key-0. There is no valid reason for the above behavior.
>>
>> mprotect without a key does not necessarily use key 0, e.g. if
>> protection keys are used to emulate page protection flag combination
>> which is not directly supported by the hardware.
>>
>> Therefore, it seems to me that filtering out non-allocated keys is
>> the right thing to do.
> 
> I am not sure, what you mean. Do you agree with the patch or otherwise?

I think it's inconsistent to make key 0 allocated, but not the key which 
is used for PROT_EXEC emulation, which is still reserved.  Even if you 
change the key 0 behavior, it is still not possible to emulate mprotect 
behavior faithfully with an allocated key.

Thanks,
Florian

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH] x86, powerpc : pkey-mprotect must allow pkey-0
  2018-03-14  8:00     ` Florian Weimer
@ 2018-03-14  8:05       ` Florian Weimer
  2018-03-14 14:08       ` Dave Hansen
  1 sibling, 0 replies; 16+ messages in thread
From: Florian Weimer @ 2018-03-14  8:05 UTC (permalink / raw)
  To: Ram Pai
  Cc: mpe, mingo, akpm, linuxppc-dev, linux-mm, x86, linux-arch,
	linux-kernel, dave.hansen, benh, paulus, khandual, aneesh.kumar,
	bsingharora, hbabu, mhocko, bauerman, ebiederm, corbet, arnd,
	msuchanek, Ulrich.Weigand

On 03/14/2018 09:00 AM, Florian Weimer wrote:
> On 03/09/2018 09:00 PM, Ram Pai wrote:
>> On Fri, Mar 09, 2018 at 12:04:49PM +0100, Florian Weimer wrote:
>>> On 03/09/2018 09:12 AM, Ram Pai wrote:
>>>> Once an address range is associated with an allocated pkey, it 
>>>> cannot be
>>>> reverted back to key-0. There is no valid reason for the above 
>>>> behavior.
>>>
>>> mprotect without a key does not necessarily use key 0, e.g. if
>>> protection keys are used to emulate page protection flag combination
>>> which is not directly supported by the hardware.
>>>
>>> Therefore, it seems to me that filtering out non-allocated keys is
>>> the right thing to do.
>>
>> I am not sure, what you mean. Do you agree with the patch or otherwise?
> 
> I think it's inconsistent to make key 0 allocated, but not the key which 
> is used for PROT_EXEC emulation, which is still reserved.  Even if you 
> change the key 0 behavior, it is still not possible to emulate mprotect 
> behavior faithfully with an allocated key.

Ugh.  Should have read the code first before replying:

         /* Do we need to assign a pkey for mm's execute-only maps? */
         if (execute_only_pkey == -1) {
                 /* Go allocate one to use, which might fail */
                 execute_only_pkey = mm_pkey_alloc(mm);
                 if (execute_only_pkey < 0)
                         return -1;
                 need_to_set_mm_pkey = true;
         }

So we do allocate the PROT_EXEC-only key, and I assume it means that the 
key can be restored using pkey_mprotect.  So the key 0 behavior is a 
true exception after all, and it makes sense to realign the behavior 
with the other keys.

Thanks,
Florian

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH] x86, powerpc : pkey-mprotect must allow pkey-0
  2018-03-14  8:00     ` Florian Weimer
  2018-03-14  8:05       ` Florian Weimer
@ 2018-03-14 14:08       ` Dave Hansen
  1 sibling, 0 replies; 16+ messages in thread
From: Dave Hansen @ 2018-03-14 14:08 UTC (permalink / raw)
  To: Florian Weimer, Ram Pai
  Cc: mpe, mingo, akpm, linuxppc-dev, linux-mm, x86, linux-arch,
	linux-kernel, benh, paulus, khandual, aneesh.kumar, bsingharora,
	hbabu, mhocko, bauerman, ebiederm, corbet, arnd, msuchanek,
	Ulrich.Weigand

On 03/14/2018 01:00 AM, Florian Weimer wrote:
> ... but not the key which is used for PROT_EXEC emulation, which is still
> reserved

The PROT_EXEC key is dynamically allocated.  There is no "the key".

^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2018-03-14 14:08 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-03-09  8:12 [PATCH] x86, powerpc : pkey-mprotect must allow pkey-0 Ram Pai
2018-03-09  8:37 ` Balbir Singh
2018-03-09 19:54   ` Ram Pai
2018-03-09  8:43 ` Ingo Molnar
2018-03-09 20:09   ` Ram Pai
2018-03-09 10:19 ` Michael Ellerman
2018-03-09 20:06   ` Ram Pai
2018-03-12 15:46     ` Dave Hansen
2018-03-09 11:04 ` Florian Weimer
2018-03-09 20:00   ` Ram Pai
2018-03-14  8:00     ` Florian Weimer
2018-03-14  8:05       ` Florian Weimer
2018-03-14 14:08       ` Dave Hansen
2018-03-09 22:40 ` Dave Hansen
2018-03-10  5:55   ` Ram Pai
2018-03-10  6:50     ` Dave Hansen

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).