User pkey minor bug fixes
Subsystems (6)
cover User pkey minor bug fixes
From: Ira Weiny <[email protected]>
While evaluating the possibility of defining a new type for pkeys within the kernel I found a couple of minor bugs.
Because these patches clean up the return codes from system calls I'm sending this out RFC hoping that users will speak up if anything breaks.
I'm not too concerned about pkey_free() because it is unlikely that anyone is checking the return code. Interestingly enough, glibc recommends not calling pkey_free() because it does not change the access rights to the key and may be subsequently allocated again.[1][2]
The pkey_alloc() is more concerning. However, I checked the Chrome source and it does not differentiate among the return codes and maps all errors into kNoMemoryProtectionKey.
glibc says it returns ENOSYS if the system does not support pkeys but I don't see where ENOSYS is returned? AFAICS it just returns what the kernel returns. So it is probably up to user of glibc.
In addition I've enhanced the pkey tests to verify and test the changes.
Thanks to Rick Edgecombe and Sohil Mehta for internal review.
[1] Quote from manual/memory.texi:
Calling this function does not change the access rights of the freed protection key. The calling thread and other threads may retain access to it, even if it is subsequently allocated again. For this reason, it is not recommended to call the @code{pkey_free} function.
[2] PKS had a similar issue and went to statically allocated keys instead.
Ira Weiny (6):
testing/pkeys: Add command line options testing/pkeys: Don't use uninitialized variable testing/pkeys: Add additional test for pkey_alloc()
pkeys: Lift pkey hardware check for pkey_alloc()
pkeys: Up level pkey_free() checks
pkeys: Change mm_pkey_free() to void
arch/powerpc/include/asm/pkeys.h | 18 ++--- arch/x86/include/asm/pkeys.h | 7 +- include/linux/pkeys.h | 5 +- mm/mprotect.c | 13 +++- tools/testing/selftests/vm/pkey-helpers.h | 7 +- tools/testing/selftests/vm/protection_keys.c | 75 +++++++++++++++++--- 6 files changed, 86 insertions(+), 39 deletions(-)
base-commit: 874c8ca1e60b2c564a48f7e7acc40d328d5c8733
Signature · 1 line
2.35.1
RFC PATCH 1/6 testing/pkeys: Add command line options
From: Ira Weiny <[email protected]>
It is more convenient to use command line options for debug and iterations vs changing the code and recompiling.
Add command line options for debug level and number of iterations.
$ ./protection_keys_64 -h
Usage: ./protection_keys_64 [-h,-d,-i <iter>]
--help,-h This help
--debug,-d Increase debug level for each -d
--iterations,-i <iter> repeate test <iter> times
default: 22--- tools/testing/selftests/vm/pkey-helpers.h | 7 +-- tools/testing/selftests/vm/protection_keys.c | 59 +++++++++++++++++--- 2 files changed, 55 insertions(+), 11 deletions(-)
tools/testing/selftests/vm/pkey-helpers.h +3 -4
@@ -23,9 +23,8 @@#define PTR_ERR_ENOTSUP ((void *)-ENOTSUP) -#ifndef DEBUG_LEVEL -#define DEBUG_LEVEL 0 -#endif +extern int debug_level; +#define DPRINT_IN_SIGNAL_BUF_SIZE 4096extern int dprint_in_signal;extern char dprint_in_signal_buffer[DPRINT_IN_SIGNAL_BUF_SIZE ];
@@ -58,7 +57,7 @@ static inline void sigsafe_printf(const char *format, ...) } } #define dprintf_level(level, args...) do { \ - if (level <= DEBUG_LEVEL) \ + if (level <= debug_level) \ sigsafe_printf(args); \ } while (0) #define dprintf0(args...) dprintf_level(0, args)
tools/testing/selftests/vm/protection_keys.c +52 -7
100 lines not on this page · raw message
Signature · 1 line
2.35.1
Re: [RFC PATCH 1/6] testing/pkeys: Add command line options
Quoted 6 lines · On 6/10/2022 4:35 PM, [email protected] wrote:
Add command line options for debug level and number of iterations.
$ ./protection_keys_64 -h Usage: ./protection_keys_64 [-h,-d,-i <iter>] --help,-h This help --debug,-d Increase debug level for each -d
Is this mechanism (of counting d's) commonplace in other selftests as well? Looking at the test code for pkeys the debug levels run from 1-5. That feels like quite a few d's to input :)
Would it be easier to input the number in the command line directly?
Either way it would be useful to know the debug range in the help. Maybe something like:
--debug,-d Increase debug level for each -d (1-5)
The patch seems fine to me otherwise.
Quoted 3 lines
--iterations,-i <iter> repeate test <iter> times default: 22
Thanks,
Sohil
Re: [RFC PATCH 1/6] testing/pkeys: Add command line options
Quoted 4 lines · Sohil
Quoted 2 lines · On 6/10/2022 4:35 PM, [email protected] wrote:
Add command line options for debug level and number of iterations.
Quoted 8 lines
Quoted 4 lines
$ ./protection_keys_64 -h Usage: ./protection_keys_64 [-h,-d,-i <iter>] --help,-h This help --debug,-d Increase debug level for each -dIs this mechanism (of counting d's) commonplace in other selftests as well? Looking at the test code for pkeys the debug levels run from 1-5. That feels like quite a few d's to input :)
I've seen (and used) it before yes. See ibnetdiscover.
...
# Debugging flags
-d raise the IB debugging level. May be used several times (-ddd or -d -d -d).
...
-v increase the application verbosity level. May be used several times (-vv or -v -v -v)
...
- https://linux.die.net/man/8/ibnetdiscover
But a much more mainstream example I can think of is verbosity level with lspci.
16:29:12 > lspci -h
...
Display options:
-v Be verbose (-vv or -vvv for higher verbosity)
...
Quoted 6 lines
Would it be easier to input the number in the command line directly?
Either way it would be useful to know the debug range in the help. Maybe something like:
--debug,-d Increase debug level for each -d (1-5)
I'm inclined not to do this because it would encode the max debug level. On the other hand I'm not sure why there are 5 levels now. ;-)
Having the multiple options specified was an easy way to maintain the large number of levels.
Ira
Quoted 9 lines
The patch seems fine to me otherwise.
Quoted 3 lines
--iterations,-i <iter> repeate test <iter> times default: 22Thanks,
Sohil
RFC PATCH 2/6 testing/pkeys: Don't use uninitialized variable
In reply to [email protected]From: Ira Weiny <[email protected]>
err was being used in test_pkey_alloc_exhaust() prior to being assigned. errno is useful to know after a failed alloc_pkey() call.
Change err to errno in the debug print.
--- tools/testing/selftests/vm/protection_keys.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/vm/protection_keys.c +2 -2
@@ -1225,9 +1225,9 @@ void test_pkey_alloc_exhaust(int *ptr, u16 pkey)int new_pkey;dprintf1 ("%s() alloc loop: %d\n" , __func__, i); new_pkey =alloc_pkey (); -dprintf4 ("%s()::%d, err: %d pkey_reg: 0x%016llx" +dprintf4 ("%s()::%d, errno: %d pkey_reg: 0x%016llx" " shadow: 0x%016llx\n" , - __func__, __LINE__, err,__read_pkey_reg (), + __func__, __LINE__, errno,__read_pkey_reg (), shadow_pkey_reg);read_pkey_reg ();/* for shadow checking */ dprintf2 ("%s() errno: %d ENOSPC: %d\n" , __func__, errno,ENOSPC );
Signature · 1 line
2.35.1
Re: [RFC PATCH 2/6] testing/pkeys: Don't use uninitialized variable
tools/testing/selftests/vm/protection_keys.c void test_pkey_alloc_exhaust(int *ptr, u16 pkey) · line 1228
Quoted 10 lines · On 6/10/2022 4:35 PM, [email protected] wrote:
tools/testing/selftests/vm/protection_keys.c +1
-1@@ -1225,9 +1225,9 @@ void test_pkey_alloc_exhaust(int *ptr, u16 pkey)int new_pkey;dprintf1 ("%s() alloc loop: %d\n" , __func__, i); new_pkey =alloc_pkey (); - dprintf4("%s()::%d, err: %d pkey_reg: 0x%016llx" + dprintf4("%s()::%d, errno: %d pkey_reg: 0x%016llx"
What is errno referring to over here? There are a few things happening in alloc_pkey(). I guess it would show the latest error that happened. Does errno need to be set to 0 before the call?
Also, would it be useful to print the return value (new_pkey) from alloc_pkey() here?
tools/testing/selftests/vm/protection_keys.c void test_pkey_alloc_exhaust(int *ptr, u16 pkey) · line 1233
Quoted 6 lines
tools/testing/selftests/vm/protection_keys.c +1
-1" shadow: 0x%016llx\n" , - __func__, __LINE__, err,__read_pkey_reg (), + __func__, __LINE__, errno,__read_pkey_reg (), shadow_pkey_reg);read_pkey_reg ();/* for shadow checking */ dprintf2 ("%s() errno: %d ENOSPC: %d\n" , __func__, errno,ENOSPC );
Sohil
Re: [RFC PATCH 2/6] testing/pkeys: Don't use uninitialized variable
Quoted 14 lines · Sohil
tools/testing/selftests/vm/protection_keys.c void test_pkey_alloc_exhaust(int *ptr, u16 pkey) · line 1228
Quoted 10 lines · On 6/10/2022 4:35 PM, [email protected] wrote:
tools/testing/selftests/vm/protection_keys.c +1
-1@@ -1225,9 +1225,9 @@ void test_pkey_alloc_exhaust(int *ptr, u16 pkey)int new_pkey;dprintf1 ("%s() alloc loop: %d\n" , __func__, i); new_pkey =alloc_pkey (); - dprintf4("%s()::%d, err: %d pkey_reg: 0x%016llx" + dprintf4("%s()::%d, errno: %d pkey_reg: 0x%016llx"What is errno referring to over here? There are a few things happening in
alloc_pkey().
Good point, but the only system call in alloc_pkey() is pkey_alloc() so it will be the errno from there.
In test_pkey_alloc_exhaust() we are expecting the errno to be from pkey_alloc()
...
if ((new_pkey == -1) && (errno == ENOSPC)) {...
Quoted 2 lines
I guess it would show the latest error that happened. Does errno need to be set to 0 before the call?
Maybe. Now that I look again errno is printed just below at level 2.
dprintf2("%s() errno: %d ENOSPC: %d\n", __func__, errno, ENOSPC);I missed that.
Quoted 3 lines
Also, would it be useful to print the return value (
new_pkey) fromalloc_pkey()here?
Yea that might be useful. Perhaps change err to new_pkey instead since errno is already printed.
Ira
Quoted 9 lines
tools/testing/selftests/vm/protection_keys.c void test_pkey_alloc_exhaust(int *ptr, u16 pkey) · line 1233
Quoted 7 lines
" shadow: 0x%016llx\n",
tools/testing/selftests/vm/protection_keys.c +1
-1- __func__, __LINE__, err,__read_pkey_reg (), + __func__, __LINE__, errno,__read_pkey_reg (), shadow_pkey_reg);read_pkey_reg ();/* for shadow checking */ dprintf2 ("%s() errno: %d ENOSPC: %d\n" , __func__, errno,ENOSPC );Sohil
RFC PATCH 3/6 testing/pkeys: Add additional test for pkey_alloc()
In reply to [email protected]From: Ira Weiny <[email protected]>
When pkeys are not available on the hardware pkey_alloc() has specific behavior which was previously untested.
Add test for this.
--- tools/testing/selftests/vm/protection_keys.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
tools/testing/selftests/vm/protection_keys.c +12 -0
@@ -1554,6 +1554,16 @@ void test_implicit_mprotect_exec_only_memory(int *ptr, u16 pkey)do_not_expect_pkey_fault ("plain read on recently PROT_EXEC area" ); } +void test_pkey_alloc_on_unsupported_cpu (void ) +{ +int test_pkey =sys_pkey_alloc (0 ,0 ); + +dprintf1 ("pkey_alloc: %d (%d %s)\n" , test_pkey, errno, +strerror (errno)); +pkey_assert (test_pkey <0 ); +pkey_assert (errno ==ENOSPC ); +} +void test_mprotect_pkey_on_unsupported_cpu (int *ptr,u16 pkey) {int size =PAGE_SIZE ;
@@ -1688,6 +1698,8 @@ int main(int argc, char *argv[])printf ("running PKEY tests for unsupported CPU/OS\n" ); +test_pkey_alloc_on_unsupported_cpu (); + ptr =mmap (NULL , size,PROT_NONE ,MAP_ANONYMOUS |MAP_PRIVATE ,-1 ,0 );assert (ptr != (void *)-1 );test_mprotect_pkey_on_unsupported_cpu (ptr,1 );
Signature · 1 line
2.35.1
Re: [RFC PATCH 3/6] testing/pkeys: Add additional test for pkey_alloc()
tools/testing/selftests/vm/protection_keys.c void test_implicit_mprotect_exec_only_memory(int *ptr, u16 pkey) · line 1564
Quoted 9 lines · On 6/10/2022 4:35 PM, [email protected] wrote:
tools/testing/selftests/vm/protection_keys.c +8
-0+void test_pkey_alloc_on_unsupported_cpu (void ) +{ +int test_pkey =sys_pkey_alloc (0 ,0 ); + +dprintf1 ("pkey_alloc: %d (%d %s)\n" , test_pkey, errno, +strerror (errno)); +pkey_assert (test_pkey <0 ); +pkey_assert (errno ==ENOSPC );
This assert fails on a kernel with CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS disabled.
Since pkey_alloc() is an architecture dependent syscall, ENOSYS is returned instead of ENOSPC when support is disabled at compile time. See kernel/sys_ni.c
This brings us to an interesting question.
Should we have different return error codes when compile support is disabled vs when runtime support is missing?
Here is the current behavior for pkey_alloc():
No compile time support -> return ENOSYS
No runtime support (but compile time support present) -> return ENOSPC
I would think applications would prefer the same error code. But, I am not sure if we can achieve this now due to ABI reasons.
Quoted 2 lines
tools/testing/selftests/vm/protection_keys.c +2
-0+} +
Re: [RFC PATCH 3/6] testing/pkeys: Add additional test for pkey_alloc()
Quoted 2 lines · Sohil Mehta
Should we have different return error codes when compile support is disabled vs when runtime support is missing?
It doesn't *really* matter. Programs have to be able to run on old kernels which will return ENOSYS. So, _when_ new kernels return ENOSYS or ENOSPC is pretty immaterial.
RFC PATCH 4/6 pkeys: Lift pkey hardware check for pkey_alloc()
In reply to [email protected]From: Ira Weiny <[email protected]>
pkey_alloc() is documented to return ENOSPC when the hardware does not support pkeys. On x86, pkey_alloc() incorrectly returns EINVAL.
This is because mm_pkey_alloc() does not check for pkey support before returning a key. Therefore, if the keys are not exhausted pkey_alloc() continues on to call arch_set_user_pkey_access(). Unfortunately, when arch_set_user_pkey_access() detects the failed support it overwrites the ENOSPC return value with EINVAL.
Ensure consistent behavior across architectures by lifting this check to the core mm code.
Remove a couple of 'we' references in code comments as well.
---
Thanks to Sohil for pointing out that the commit message could be more clear WRT how EINVAL is returned incorrectly.
--- arch/powerpc/include/asm/pkeys.h | 8 +++----- mm/mprotect.c | 3 +++ 2 files changed, 6 insertions(+), 5 deletions(-)
arch/powerpc/include/asm/pkeys.h +3 -5
@@ -85,18 +85,16 @@ static inline bool mm_pkey_is_allocated(struct mm_struct *mm, int pkey)static inline int mm_pkey_alloc (struct mm_struct *mm) {/* -* Note: this is the one and only place we make sure that the pkey is +* Note: this is the one and only place to make sure that the pkey is * valid as far as the hardware is concerned. The rest of the kernel * trusts that only good, valid pkeys come out of here. */ u32 all_pkeys_mask = (u32)(~(0x0 ));int ret; -if (!mmu_has_feature (MMU_FTR_PKEY )) -return -1 ;/* -* Are we out of pkeys? We must handle this specially because ffz() -* behavior is undefined if there are no zeros. +* Out of pkeys? Handle this specially because ffz() behavior is +* undefined if there are no zeros. */ if (mm_pkey_allocation_map (mm) == all_pkeys_mask)return -1 ;
mm/mprotect.c +3 -0
@@ -773,6 +773,9 @@ SYSCALL_DEFINE2(pkey_alloc, unsigned long, flags, unsigned long, init_val)int pkey;int ret; +if (!arch_pkeys_enabled ()) +return -ENOSPC ; +/* No flags supported yet. */ if (flags)return -EINVAL ;
Signature · 1 line
2.35.1
Re: [RFC PATCH 4/6] pkeys: Lift pkey hardware check for pkey_alloc()
mm/mprotect.c SYSCALL_DEFINE2(pkey_alloc, unsigned long, flags, unsigned long, init_val) · line 778
Quoted 12 lines
mm/mprotect.c +3
-0@@ -773,6 +773,9 @@ SYSCALL_DEFINE2(pkey_alloc, unsigned long, flags, unsigned long, init_val)int pkey;int ret; +if (!arch_pkeys_enabled ()) +return -ENOSPC ; +
See comments in patch 3/6. Since we are modifying (fixing) old behavior, should we just return ENOSYS to make this consistent?
Sohil
Quoted 3 lines
mm/mprotect.c +0
-0/* No flags supported yet. */ if (flags)return -EINVAL ;
RFC PATCH 5/6 pkeys: Up level pkey_free() checks
In reply to [email protected]From: Ira Weiny <[email protected]>
x86 is missing a hardware check for pkey support in pkey_free(). While the net result is the same (-EINVAL returned), pkey_free() has well defined behavior which will be easier to maintain in one place.
For powerpc the return code is -1 rather than -EINVAL. This changes that behavior slightly but this is very unlikely to break any user space.
Lift the checks for pkey_free() to the core mm code and ensure consistency with returning -EINVAL.
---
Thanks to Sohil for suggesting I mention the powerpc return value in the commit message.
Also Sohil suggested changing mm_pkey_free() from int to void. This is added as a separate patch with his suggested by.
--- arch/powerpc/include/asm/pkeys.h | 6 ------ arch/x86/include/asm/pkeys.h | 3 --- mm/mprotect.c | 8 ++++++-- 3 files changed, 6 insertions(+), 11 deletions(-)
arch/powerpc/include/asm/pkeys.h +0 -6
@@ -107,12 +107,6 @@ static inline int mm_pkey_alloc(struct mm_struct *mm)static inline int mm_pkey_free (struct mm_struct *mm,int pkey) { -if (!mmu_has_feature (MMU_FTR_PKEY )) -return -1 ; - -if (!mm_pkey_is_allocated (mm, pkey)) -return -EINVAL ; -__mm_pkey_free (mm, pkey);return 0 ;
arch/x86/include/asm/pkeys.h +0 -3
@@ -107,9 +107,6 @@ 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)) -return -EINVAL ; -mm_set_pkey_free (mm, pkey);return 0 ;
mm/mprotect.c +6 -2
@@ -803,10 +803,14 @@ SYSCALL_DEFINE2(pkey_alloc, unsigned long, flags, unsigned long, init_val) SYSCALL_DEFINE1(pkey_free, int, pkey) { -int ret; +int ret = -EINVAL ; + +if (!arch_pkeys_enabled ()) +return ret;mmap_write_lock (current->mm); - ret =mm_pkey_free (current->mm, pkey); +if (mm_pkey_is_allocated (current->mm, pkey)) + ret =mm_pkey_free (current->mm, pkey);mmap_write_unlock (current->mm);/*
Signature · 1 line
2.35.1
Re: [RFC PATCH 5/6] pkeys: Up level pkey_free() checks
Le 11/06/2022 à 01:35, [email protected] a écrit :
Quoted 46 lines
From: Ira Weiny <[email protected]>
x86 is missing a hardware check for pkey support in
pkey_free(). While the net result is the same (-EINVALreturned),pkey_free()has well defined behavior which will be easier to maintain in one place.For powerpc the return code is -1 rather than
-EINVAL. This changes that behavior slightly but this is very unlikely to break any user space.Lift the checks for
pkey_free()to the core mm code and ensure consistency with returning-EINVAL.Cc Florian Weimer <[email protected]>Cc Andrew Morton <[email protected]>Cc Sohil Mehta <[email protected]>Cc Dave Hansen <[email protected]>Cc Aneesh Kumar K.V <[email protected]>Signed-off-by Ira Weiny <[email protected]>---
Thanks to Sohil for suggesting I mention the powerpc return value in the commit message.Also Sohil suggested changing
mm_pkey_free()from int to void. This is added as a separate patch with his suggested by.--- arch/powerpc/include/asm/pkeys.h | 6 ------ arch/x86/include/asm/pkeys.h | 3 --- mm/mprotect.c | 8 ++++++-- 3 files changed, 6 insertions(+), 11 deletions(-)arch/powerpc/include/asm/pkeys.h +0
-0@@ -107,12 +107,6 @@ static inline int mm_pkey_alloc(struct mm_struct *mm)static inline int mm_pkey_free (struct mm_struct *mm,int pkey)
arch/powerpc/include/asm/pkeys.h static inline int mm_pkey_alloc(struct mm_struct *mm) · line 112
Quoted 10 lines
arch/powerpc/include/asm/pkeys.h +0
-6{ -if (!mmu_has_feature (MMU_FTR_PKEY )) -return -1 ; - -if (!mm_pkey_is_allocated (mm, pkey)) -return -EINVAL ; -__mm_pkey_free (mm, pkey);return 0 ;
If it returns always 0, the return value is pointless and the function mm_pkey_free() should be changed to return void.
Quoted 5 lines
arch/x86/include/asm/pkeys.h +0
-0
arch/x86/include/asm/pkeys.h int mm_pkey_alloc(struct mm_struct *mm) · line 112
Quoted 9 lines
arch/x86/include/asm/pkeys.h +0
-3@@ -107,9 +107,6 @@ 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)) -return -EINVAL ; -mm_set_pkey_free (mm, pkey);return 0 ;
Same.
mm/mprotect.c SYSCALL_DEFINE2(pkey_alloc, unsigned long, flags, unsigned long, init_val) · line 806
Quoted 10 lines
mm/mprotect.c +1
-1@@ -803,10 +803,14 @@ SYSCALL_DEFINE2(pkey_alloc, unsigned long, flags, unsigned long, init_val) SYSCALL_DEFINE1(pkey_free, int, pkey) { -int ret; +int ret = -EINVAL ;
Don't initialise 'ret'
mm/mprotect.c SYSCALL_DEFINE2(pkey_alloc, unsigned long, flags, unsigned long, init_val) · line 809
Quoted 3 lines
mm/mprotect.c +3
-0+ +if (!arch_pkeys_enabled ()) +return ret;
Make it explicit, do 'return -EINVAL'
Once that is done, is there any point in having a fallback version of mm_pkey_free() which returns -EINVAL ?
mm/mprotect.c SYSCALL_DEFINE2(pkey_alloc, unsigned long, flags, unsigned long, init_val) · line 813
Quoted 5 lines
mm/mprotect.c +2
-1mmap_write_lock (current->mm); - ret =mm_pkey_free (current->mm, pkey); +if (mm_pkey_is_allocated (current->mm, pkey)) + ret =mm_pkey_free (current->mm, pkey);
Add:
else ret = -EINVAL;
Quoted 3 lines
mm/mprotect.c +0
-0mmap_write_unlock (current->mm);/*
RFC PATCH 6/6 pkeys: Change mm_pkey_free() to void
In reply to [email protected]From: Ira Weiny <[email protected]>
Now that the pkey arch support is no longer checked in mm_pkey_free() there is no reason to have it return int.
Change the return value to void.
--- arch/powerpc/include/asm/pkeys.h | 4 +--- arch/x86/include/asm/pkeys.h | 4 +--- include/linux/pkeys.h | 5 +---- mm/mprotect.c | 6 ++++-- 4 files changed, 7 insertions(+), 12 deletions(-)
arch/powerpc/include/asm/pkeys.h +1 -3
@@ -105,11 +105,9 @@ static inline int mm_pkey_alloc(struct mm_struct *mm)return ret; } -static inline int mm_pkey_free (struct mm_struct *mm,int pkey) +static inline void mm_pkey_free (struct mm_struct *mm,int pkey) {__mm_pkey_free (mm, pkey); - -return 0 ; }/*
arch/x86/include/asm/pkeys.h +1 -3
@@ -105,11 +105,9 @@ int mm_pkey_alloc(struct mm_struct *mm) }static inline -int mm_pkey_free (struct mm_struct *mm,int pkey) +void mm_pkey_free (struct mm_struct *mm,int pkey) {mm_set_pkey_free (mm, pkey); - -return 0 ; }static inline int vma_pkey (struct vm_area_struct *vma)
include/linux/pkeys.h +1 -4
@@ -30,10 +30,7 @@ static inline int mm_pkey_alloc(struct mm_struct *mm)return -1 ; } -static inline int mm_pkey_free (struct mm_struct *mm,int pkey) -{ -return -EINVAL ; -} +static inline void mm_pkey_free (struct mm_struct *mm,int pkey) { }static inline int arch_set_user_pkey_access (struct task_struct *tsk,int pkey,unsigned long init_val)
mm/mprotect.c +4 -2
@@ -809,8 +809,10 @@ SYSCALL_DEFINE1(pkey_free, int, pkey)return ret;mmap_write_lock (current->mm); -if (mm_pkey_is_allocated (current->mm, pkey)) - ret =mm_pkey_free (current->mm, pkey); +if (mm_pkey_is_allocated (current->mm, pkey)) { +mm_pkey_free (current->mm, pkey); + ret =0 ; + }mmap_write_unlock (current->mm);/*
Signature · 1 line
2.35.1
Re: [RFC PATCH 6/6] pkeys: Change mm_pkey_free() to void
Le 11/06/2022 à 01:35, [email protected] a écrit :
Quoted 4 lines
From: Ira Weiny <[email protected]>
Now that the pkey arch support is no longer checked in
mm_pkey_free()there is no reason to have it return int.
Right, I see this is doing what I commented in previous patch.
Quoted 69 lines
Change the return value to void.
Cc Dave Hansen <[email protected]>Cc Aneesh Kumar K.V <[email protected]>Suggested-by Sohil Mehta <[email protected]>Signed-off-by Ira Weiny <[email protected]>--- arch/powerpc/include/asm/pkeys.h | 4 +--- arch/x86/include/asm/pkeys.h | 4 +--- include/linux/pkeys.h | 5 +---- mm/mprotect.c | 6 ++++-- 4 files changed, 7 insertions(+), 12 deletions(-)arch/powerpc/include/asm/pkeys.h +1
-3@@ -105,11 +105,9 @@ static inline int mm_pkey_alloc(struct mm_struct *mm)return ret; } -static inline int mm_pkey_free (struct mm_struct *mm,int pkey) +static inline void mm_pkey_free (struct mm_struct *mm,int pkey) {__mm_pkey_free (mm, pkey); - -return 0 ; }/* arch/x86/include/asm/pkeys.h +1
-3@@ -105,11 +105,9 @@ int mm_pkey_alloc(struct mm_struct *mm) }static inline -int mm_pkey_free (struct mm_struct *mm,int pkey) +void mm_pkey_free (struct mm_struct *mm,int pkey) {mm_set_pkey_free (mm, pkey); - -return 0 ; }static inline int vma_pkey (struct vm_area_struct *vma)include/linux/pkeys.h +1
-4@@ -30,10 +30,7 @@ static inline int mm_pkey_alloc(struct mm_struct *mm)return -1 ; } -static inline int mm_pkey_free (struct mm_struct *mm,int pkey) -{ -return -EINVAL ; -} +static inline void mm_pkey_free (struct mm_struct *mm,int pkey) { }static inline int arch_set_user_pkey_access (struct task_struct *tsk,int pkey,unsigned long init_val)mm/mprotect.c +0
-0
mm/mprotect.c SYSCALL_DEFINE1(pkey_free, int, pkey) · line 815
Quoted 9 lines
mm/mprotect.c +4
-2@@ -809,8 +809,10 @@ SYSCALL_DEFINE1(pkey_free, int, pkey)return ret;mmap_write_lock (current->mm); -if (mm_pkey_is_allocated (current->mm, pkey)) - ret =mm_pkey_free (current->mm, pkey); +if (mm_pkey_is_allocated (current->mm, pkey)) { +mm_pkey_free (current->mm, pkey); + ret =0 ; + }
Or you could have ret = 0 by default and do
if (mm_pkey_is_allocated(current->mm, pkey)) mm_pkey_free(current->mm, pkey); else ret = -EINVAL;
Quoted 3 lines
mm/mprotect.c +0
-0mmap_write_unlock (current->mm);/*
Re: [RFC PATCH 6/6] pkeys: Change mm_pkey_free() to void
Quoted 9 lines · Christophe Leroy
Le 11/06/2022 à 01:35, [email protected] a écrit :
Quoted 4 lines
From: Ira Weiny <[email protected]>
Now that the pkey arch support is no longer checked in
mm_pkey_free()there is no reason to have it return int.Right, I see this is doing what I commented in previous patch.
Yes because it was suggested by Sohil I decided to make it a separate patch to make the credit easier.
Quoted 17 lines
Quoted 5 lines
mm/mprotect.c +0
-0mm/mprotect.c SYSCALL_DEFINE1(pkey_free, int, pkey) · line 815
Quoted 9 lines
mm/mprotect.c +4
-2@@ -809,8 +809,10 @@ SYSCALL_DEFINE1(pkey_free, int, pkey)return ret;mmap_write_lock (current->mm); -if (mm_pkey_is_allocated (current->mm, pkey)) - ret =mm_pkey_free (current->mm, pkey); +if (mm_pkey_is_allocated (current->mm, pkey)) { +mm_pkey_free (current->mm, pkey); + ret =0 ; + }Or you could have ret = 0 by default and do
Quoted 4 lines
if (mm_pkey_is_allocated(current->mm, pkey)) mm_pkey_free(current->mm, pkey); else ret = -EINVAL;
Yes that fits the kernel style better.
Thanks for the review!
Ira
Quoted 4 lines
Quoted 4 lines
mm/mprotect.c +0
-0mmap_write_unlock (current->mm);/*
Quoted 5 lines · On 6/10/2022 4:35 PM, [email protected] wrote:
glibc says it returns ENOSYS if the system does not support pkeys but I don't see where ENOSYS is returned? AFAICS it just returns what the kernel returns. So it is probably up to user of glibc.
Implementation of the pkeys system calls is arch specific and conditional. See kernel/sys_ni.c
glibc is probably talking about ENOSYS being returned when the architecture doesn't have support or the CONFIG option is disabled on supported architectures.
Thanks,
Sohil