linux-arch.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm/pkey: Add PKEY_UNRESTRICTED macro
@ 2024-10-22 12:01 Yury Khrustalev
  2024-10-22 12:56 ` Kevin Brodsky
  2024-10-22 14:04 ` Dave Hansen
  0 siblings, 2 replies; 8+ messages in thread
From: Yury Khrustalev @ 2024-10-22 12:01 UTC (permalink / raw)
  To: linux-arch
  Cc: Arnd Bergmann, Kevin Brodsky, Joey Gouly, Dave Hansen, nd,
	Yury Khrustalev

Memory protection keys (pkeys) uapi previously used two macros:

 - PKEY_DISABLE_ACCESS 0x1
 - PKEY_DISABLE_WRITE  0x2

with implicit literal value of 0x0 that means "unrestricted". Code that
works with pkeys has to use this literal value when implying that a pkey
imposes no restrictions. This may reduce readability because 0 can be
written in various ways (e.g. 0x0 or 0) and also because 0 in the context
of pkeys can be mistaken for "no permissions" (akin PROT_NONE) while it
actually means "no restrictions". This is important because pkeys are
oftentimes used near mprotect() that uses PROT_ macros.

This patch adds PKEY_UNRESTRICTED macro defined as 0x0.

---
Applies to 42f7652d3eb5 (tag: v6.12-rc4).

For context, this change will also allow for more consistent update of the
Glibc manual [1] which in turn will help with introducing memory protection
keys on AArch64 targets [2].

[1] https://inbox.sourceware.org/libc-alpha/20241022073837.151355-1-yury.khrustalev@arm.com/
[2] https://inbox.sourceware.org/libc-alpha/20241011153614.3189334-1-yury.khrustalev@arm.com/

Is this patch OK?

Kind regards,
Yury

---
 include/uapi/asm-generic/mman-common.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/include/uapi/asm-generic/mman-common.h b/include/uapi/asm-generic/mman-common.h
index 6ce1f1ceb432..ea40e27e6dea 100644
--- a/include/uapi/asm-generic/mman-common.h
+++ b/include/uapi/asm-generic/mman-common.h
@@ -82,6 +82,7 @@
 /* compatibility flags */
 #define MAP_FILE	0
 
+#define PKEY_UNRESTRICTED	0x0
 #define PKEY_DISABLE_ACCESS	0x1
 #define PKEY_DISABLE_WRITE	0x2
 #define PKEY_ACCESS_MASK	(PKEY_DISABLE_ACCESS |\
-- 
2.39.5


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

* Re: [PATCH] mm/pkey: Add PKEY_UNRESTRICTED macro
  2024-10-22 12:01 [PATCH] mm/pkey: Add PKEY_UNRESTRICTED macro Yury Khrustalev
@ 2024-10-22 12:56 ` Kevin Brodsky
  2024-10-22 14:04 ` Dave Hansen
  1 sibling, 0 replies; 8+ messages in thread
From: Kevin Brodsky @ 2024-10-22 12:56 UTC (permalink / raw)
  To: Yury Khrustalev, linux-arch; +Cc: Arnd Bergmann, Joey Gouly, Dave Hansen, nd

On 22/10/2024 14:01, Yury Khrustalev wrote:
> Memory protection keys (pkeys) uapi previously used two macros:

Nit: I would remove "previously" as it's not clear what it means here.

>  - PKEY_DISABLE_ACCESS 0x1
>  - PKEY_DISABLE_WRITE  0x2
>
> with implicit literal value of 0x0 that means "unrestricted". Code that
> works with pkeys has to use this literal value when implying that a pkey
> imposes no restrictions. This may reduce readability because 0 can be
> written in various ways (e.g. 0x0 or 0) and also because 0 in the context
> of pkeys can be mistaken for "no permissions" (akin PROT_NONE) while it
> actually means "no restrictions". This is important because pkeys are
> oftentimes used near mprotect() that uses PROT_ macros.
>
> This patch adds PKEY_UNRESTRICTED macro defined as 0x0.

Your Signed-off-by is missing.

With that fixed:

Reviewed-by: Kevin Brodsky <kevin.brodsky@arm.com>

- Kevin

> ---
> Applies to 42f7652d3eb5 (tag: v6.12-rc4).
>
> For context, this change will also allow for more consistent update of the
> Glibc manual [1] which in turn will help with introducing memory protection
> keys on AArch64 targets [2].
>
> [1] https://inbox.sourceware.org/libc-alpha/20241022073837.151355-1-yury.khrustalev@arm.com/
> [2] https://inbox.sourceware.org/libc-alpha/20241011153614.3189334-1-yury.khrustalev@arm.com/
>
> Is this patch OK?
>
> Kind regards,
> Yury
>
> ---
>  include/uapi/asm-generic/mman-common.h | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/include/uapi/asm-generic/mman-common.h b/include/uapi/asm-generic/mman-common.h
> index 6ce1f1ceb432..ea40e27e6dea 100644
> --- a/include/uapi/asm-generic/mman-common.h
> +++ b/include/uapi/asm-generic/mman-common.h
> @@ -82,6 +82,7 @@
>  /* compatibility flags */
>  #define MAP_FILE	0
>  
> +#define PKEY_UNRESTRICTED	0x0
>  #define PKEY_DISABLE_ACCESS	0x1
>  #define PKEY_DISABLE_WRITE	0x2
>  #define PKEY_ACCESS_MASK	(PKEY_DISABLE_ACCESS |\


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

* Re: [PATCH] mm/pkey: Add PKEY_UNRESTRICTED macro
  2024-10-22 12:01 [PATCH] mm/pkey: Add PKEY_UNRESTRICTED macro Yury Khrustalev
  2024-10-22 12:56 ` Kevin Brodsky
@ 2024-10-22 14:04 ` Dave Hansen
  2024-10-22 15:11   ` Kevin Brodsky
  1 sibling, 1 reply; 8+ messages in thread
From: Dave Hansen @ 2024-10-22 14:04 UTC (permalink / raw)
  To: Yury Khrustalev, linux-arch
  Cc: Arnd Bergmann, Kevin Brodsky, Joey Gouly, Dave Hansen, nd

On 10/22/24 05:01, Yury Khrustalev wrote:
> diff --git a/include/uapi/asm-generic/mman-common.h b/include/uapi/asm-generic/mman-common.h
> index 6ce1f1ceb432..ea40e27e6dea 100644
> --- a/include/uapi/asm-generic/mman-common.h
> +++ b/include/uapi/asm-generic/mman-common.h
> @@ -82,6 +82,7 @@
>  /* compatibility flags */
>  #define MAP_FILE	0
>  
> +#define PKEY_UNRESTRICTED	0x0
>  #define PKEY_DISABLE_ACCESS	0x1
>  #define PKEY_DISABLE_WRITE	0x2
>  #define PKEY_ACCESS_MASK	(PKEY_DISABLE_ACCESS |\

It seems sane, but it would be nice to have at least site or two use it
in the kernel so show that it's useful in practice.  Is there any kernel
code or anything in selftests/ that would be improved with this?

That said, it's pretty harmless:

Acked-by: Dave Hansen <dave.hansen@linux.intel.com>

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

* Re: [PATCH] mm/pkey: Add PKEY_UNRESTRICTED macro
  2024-10-22 14:04 ` Dave Hansen
@ 2024-10-22 15:11   ` Kevin Brodsky
  2024-10-22 17:15     ` Dave Hansen
  0 siblings, 1 reply; 8+ messages in thread
From: Kevin Brodsky @ 2024-10-22 15:11 UTC (permalink / raw)
  To: Dave Hansen, Yury Khrustalev, linux-arch
  Cc: Arnd Bergmann, Joey Gouly, Dave Hansen, nd

On 22/10/2024 16:04, Dave Hansen wrote:
> On 10/22/24 05:01, Yury Khrustalev wrote:
>> diff --git a/include/uapi/asm-generic/mman-common.h b/include/uapi/asm-generic/mman-common.h
>> index 6ce1f1ceb432..ea40e27e6dea 100644
>> --- a/include/uapi/asm-generic/mman-common.h
>> +++ b/include/uapi/asm-generic/mman-common.h
>> @@ -82,6 +82,7 @@
>>  /* compatibility flags */
>>  #define MAP_FILE	0
>>  
>> +#define PKEY_UNRESTRICTED	0x0
>>  #define PKEY_DISABLE_ACCESS	0x1
>>  #define PKEY_DISABLE_WRITE	0x2
>>  #define PKEY_ACCESS_MASK	(PKEY_DISABLE_ACCESS |\
> It seems sane, but it would be nice to have at least site or two use it
> in the kernel so show that it's useful in practice.  Is there any kernel
> code or anything in selftests/ that would be improved with this?

As a matter of fact this would improve the readability of [1] quite a
bit: all those set_pkey_bits(..., 0) calls would become
set_pkey_bits(..., PKEY_UNRESTRICTED). I'm sure other pkey-related
kselftests could benefit too.

Kevin

[1]
https://lore.kernel.org/linux-arm-kernel/20241017133909.3837547-5-kevin.brodsky@arm.com/

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

* Re: [PATCH] mm/pkey: Add PKEY_UNRESTRICTED macro
  2024-10-22 15:11   ` Kevin Brodsky
@ 2024-10-22 17:15     ` Dave Hansen
  2024-10-25  8:26       ` Kevin Brodsky
  0 siblings, 1 reply; 8+ messages in thread
From: Dave Hansen @ 2024-10-22 17:15 UTC (permalink / raw)
  To: Kevin Brodsky, Yury Khrustalev, linux-arch
  Cc: Arnd Bergmann, Joey Gouly, Dave Hansen, nd

On 10/22/24 08:11, Kevin Brodsky wrote:
>>> +#define PKEY_UNRESTRICTED	0x0
>>>  #define PKEY_DISABLE_ACCESS	0x1
>>>  #define PKEY_DISABLE_WRITE	0x2
>>>  #define PKEY_ACCESS_MASK	(PKEY_DISABLE_ACCESS |\
>> It seems sane, but it would be nice to have at least site or two use it
>> in the kernel so show that it's useful in practice.  Is there any kernel
>> code or anything in selftests/ that would be improved with this?
> As a matter of fact this would improve the readability of [1] quite a
> bit: all those set_pkey_bits(..., 0) calls would become
> set_pkey_bits(..., PKEY_UNRESTRICTED). I'm sure other pkey-related
> kselftests could benefit too.

It would be much appreciated if someone could make a pass over kernel
code and fix up the places where PKEY_UNRESTRICTED makes things more clear.


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

* Re: [PATCH] mm/pkey: Add PKEY_UNRESTRICTED macro
  2024-10-22 17:15     ` Dave Hansen
@ 2024-10-25  8:26       ` Kevin Brodsky
  2024-10-25 14:48         ` Dave Hansen
  0 siblings, 1 reply; 8+ messages in thread
From: Kevin Brodsky @ 2024-10-25  8:26 UTC (permalink / raw)
  To: Dave Hansen, Yury Khrustalev, linux-arch
  Cc: Arnd Bergmann, Joey Gouly, Dave Hansen, nd

On 22/10/2024 19:15, Dave Hansen wrote:
> On 10/22/24 08:11, Kevin Brodsky wrote:
>>>> +#define PKEY_UNRESTRICTED	0x0
>>>>  #define PKEY_DISABLE_ACCESS	0x1
>>>>  #define PKEY_DISABLE_WRITE	0x2
>>>>  #define PKEY_ACCESS_MASK	(PKEY_DISABLE_ACCESS |\
>>> It seems sane, but it would be nice to have at least site or two use it
>>> in the kernel so show that it's useful in practice.  Is there any kernel
>>> code or anything in selftests/ that would be improved with this?
>> As a matter of fact this would improve the readability of [1] quite a
>> bit: all those set_pkey_bits(..., 0) calls would become
>> set_pkey_bits(..., PKEY_UNRESTRICTED). I'm sure other pkey-related
>> kselftests could benefit too.
> It would be much appreciated if someone could make a pass over kernel
> code and fix up the places where PKEY_UNRESTRICTED makes things more clear.

It doesn't look like kernel code would have a use for it at the moment,
but I have found a few places in kselftests (mm, ppc) where 0 could be
replaced with PKEY_UNRESTRICTED. I can send a follow-up series.

Kevin

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

* Re: [PATCH] mm/pkey: Add PKEY_UNRESTRICTED macro
  2024-10-25  8:26       ` Kevin Brodsky
@ 2024-10-25 14:48         ` Dave Hansen
  2024-10-28  8:33           ` Yury Khrustalev
  0 siblings, 1 reply; 8+ messages in thread
From: Dave Hansen @ 2024-10-25 14:48 UTC (permalink / raw)
  To: Kevin Brodsky, Yury Khrustalev, linux-arch
  Cc: Arnd Bergmann, Joey Gouly, Dave Hansen, nd

On 10/25/24 01:26, Kevin Brodsky wrote:
>> It would be much appreciated if someone could make a pass over kernel
>> code and fix up the places where PKEY_UNRESTRICTED makes things more clear.
> It doesn't look like kernel code would have a use for it at the moment,
> but I have found a few places in kselftests (mm, ppc) where 0 could be
> replaced with PKEY_UNRESTRICTED. I can send a follow-up series.

Just doing it in the selftests would be great.

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

* Re: [PATCH] mm/pkey: Add PKEY_UNRESTRICTED macro
  2024-10-25 14:48         ` Dave Hansen
@ 2024-10-28  8:33           ` Yury Khrustalev
  0 siblings, 0 replies; 8+ messages in thread
From: Yury Khrustalev @ 2024-10-28  8:33 UTC (permalink / raw)
  To: Dave Hansen
  Cc: Kevin Brodsky, linux-arch, Arnd Bergmann, Joey Gouly, Dave Hansen,
	nd

Hi,

On Fri, Oct 25, 2024 at 07:48:01AM -0700, Dave Hansen wrote:
> On 10/25/24 01:26, Kevin Brodsky wrote:
> >> It would be much appreciated if someone could make a pass over kernel
> >> code and fix up the places where PKEY_UNRESTRICTED makes things more clear.
> > It doesn't look like kernel code would have a use for it at the moment,
> > but I have found a few places in kselftests (mm, ppc) where 0 could be
> > replaced with PKEY_UNRESTRICTED. I can send a follow-up series.
> 
> Just doing it in the selftests would be great.


Thank you for the feedback, I've sent v2 with updated tests [1].

[1] https://lore.kernel.org/linux-arch/20241027170006.464252-1-yury.khrustalev@arm.com/

Kind regards,
Yury


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

end of thread, other threads:[~2024-10-28  8:33 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-22 12:01 [PATCH] mm/pkey: Add PKEY_UNRESTRICTED macro Yury Khrustalev
2024-10-22 12:56 ` Kevin Brodsky
2024-10-22 14:04 ` Dave Hansen
2024-10-22 15:11   ` Kevin Brodsky
2024-10-22 17:15     ` Dave Hansen
2024-10-25  8:26       ` Kevin Brodsky
2024-10-25 14:48         ` Dave Hansen
2024-10-28  8:33           ` Yury Khrustalev

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