selftests/arm64: MTE check_tags_inclusion cleanups
cover selftests/arm64: MTE check_tags_inclusion cleanups
This series contains some random cleanups and improvements that I came up with while looking at the check_tags_inclusion test. There's nothing too exciting in here but the changes did seem like they might help the next person to look at this.
Mark Brown (5):
selftests/arm64: Log errors in verify_mte_pointer_validity() selftests/arm64: Allow zero tags in mte_switch_mode() selftests/arm64: Check failures to set tags in check_tags_inclusion selftests/arm64: Remove casts to/from void in check_tags_inclusion selftests/arm64: Use switch statements in mte_common_util.c
.../arm64/mte/check_tags_inclusion.c | 54 +++++++++++-------- .../selftests/arm64/mte/mte_common_util.c | 25 ++++++--- 2 files changed, 50 insertions(+), 29 deletions(-)
base-commit: ae60e0763e97e977b03af1ac6ba782a4a86c3a5a
Signature · 1 line
2.30.2
PATCH 1/5 selftests/arm64: Log errors in verify_mte_pointer_validity()
When we detect a problem in verify_mte_pointer_validity() while checking tags we don't log what the problem was which makes debugging harder. Add some diagnostics.
--- .../selftests/arm64/mte/check_tags_inclusion.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
tools/testing/selftests/arm64/mte/check_tags_inclusion.c +9 -3
@@ -25,8 +25,11 @@ static int verify_mte_pointer_validity(char *ptr, int mode)/* Check the validity of the tagged pointer */ memset ((void *)ptr,'1' ,BUFFER_SIZE );mte_wait_after_trig (); -if (cur_mte_cxt.fault_valid) +if (cur_mte_cxt.fault_valid) { +ksft_print_msg ("Unexpected fault recorded for %p-%p in mode %x\n" , + ptr, ptr +BUFFER_SIZE , mode);return KSFT_FAIL ; + }/* Proceed further for nonzero tags */ if (!MT_FETCH_TAG ((uintptr_t )ptr))return KSFT_PASS ;
@@ -34,10 +37,13 @@ static int verify_mte_pointer_validity(char *ptr, int mode)/* Check the validity outside the range */ ptr[BUFFER_SIZE ] ='2' ;mte_wait_after_trig (); -if (!cur_mte_cxt.fault_valid) +if (!cur_mte_cxt.fault_valid) { +ksft_print_msg ("No valid fault recorded for %p in mode %x\n" , + ptr, mode);return KSFT_FAIL ; -else + }else {return KSFT_PASS ; + } }static int check_single_included_tags (int mem_type,int mode)
Signature · 1 line
2.30.2
Re: [PATCH v1 1/5] selftests/arm64: Log errors in verify_mte_pointer_validity()
Quoted 33 lines · Mark Brown
When we detect a problem in
verify_mte_pointer_validity()while checking tags we don't log what the problem was which makes debugging harder. Add some diagnostics.Signed-off-by Mark Brown <[email protected]>--- .../selftests/arm64/mte/check_tags_inclusion.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)tools/testing/selftests/arm64/mte/check_tags_inclusion.c +5
-2@@ -25,8 +25,11 @@ static int verify_mte_pointer_validity(char *ptr, int mode)/* Check the validity of the tagged pointer */ memset ((void *)ptr,'1' ,BUFFER_SIZE );mte_wait_after_trig (); -if (cur_mte_cxt.fault_valid) +if (cur_mte_cxt.fault_valid) { +ksft_print_msg ("Unexpected fault recorded for %p-%p in mode %x\n" , + ptr, ptr +BUFFER_SIZE , mode);return KSFT_FAIL ; + }/* Proceed further for nonzero tags */ if (!MT_FETCH_TAG ((uintptr_t )ptr))return KSFT_PASS ;@@ -34,10 +37,13 @@ static int verify_mte_pointer_validity(char *ptr, int mode)/* Check the validity outside the range */ ptr[BUFFER_SIZE ] ='2' ;mte_wait_after_trig (); -if (!cur_mte_cxt.fault_valid) +if (!cur_mte_cxt.fault_valid) {
tools/testing/selftests/arm64/mte/check_tags_inclusion.c static int verify_mte_pointer_validity(char *ptr, int mode) · line 49
Quoted 10 lines
tools/testing/selftests/arm64/mte/check_tags_inclusion.c +4
-1+ksft_print_msg ("No valid fault recorded for %p in mode %x\n" , + ptr, mode);return KSFT_FAIL ; - else + }else {return KSFT_PASS ; + } }static int check_single_included_tags (int mem_type,int mode)
Nice. Thanks for the patch.
It would be a nice addition to print mode names as strings to make it easy to understand. Could done in a future patch. e,g: MTE_NONE_ERR etc.
thanks,
-- Shuah
PATCH 2/5 selftests/arm64: Allow zero tags in mte_switch_mode()
In reply to Mark Brownmte_switch_mode() currently rejects attempts to set a zero tag however there are tests such as check_tags_inclusion which attempt to cover cases with zero tags using mte_switch_mode(). Since it is not clear why we are rejecting zero tags change the test to accept them.
The issue has not previously been as apparent as it should be since the return value of mte_switch_mode() was not always checked in the callers and the tests weren't otherwise failing.
--- tools/testing/selftests/arm64/mte/mte_common_util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/arm64/mte/mte_common_util.c +1 -1
@@ -283,7 +283,7 @@ int mte_switch_mode(int mte_option, unsigned long incl_mask)return -EINVAL ; } -if (!(incl_mask <=MTE_ALLOW_NON_ZERO_TAG )) { +if (incl_mask & ~MT_INCLUDE_TAG_MASK ) {ksft_print_msg ("FAIL: Invalid incl_mask %lx\n" , incl_mask);return -EINVAL ; }
Signature · 1 line
2.30.2
Re: [PATCH v1 2/5] selftests/arm64: Allow zero tags in mte_switch_mode()
Quoted 20 lines · Mark Brown
mte_switch_mode()currently rejects attempts to set a zero tag however there are tests such ascheck_tags_inclusionwhich attempt to cover cases with zero tags usingmte_switch_mode(). Since it is not clear why we are rejecting zero tags change the test to accept them.The issue has not previously been as apparent as it should be since the return value of
mte_switch_mode()was not always checked in the callers and the tests weren't otherwise failing.Signed-off-by Mark Brown <[email protected]>--- tools/testing/selftests/arm64/mte/mte_common_util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/testing/selftests/arm64/mte/mte_common_util.c +0
-0
tools/testing/selftests/arm64/mte/mte_common_util.c int mte_switch_mode(int mte_option, unsigned long incl_mask) · line 289
Quoted 8 lines
tools/testing/selftests/arm64/mte/mte_common_util.c +1
-1@@ -283,7 +283,7 @@ int mte_switch_mode(int mte_option, unsigned long incl_mask)return -EINVAL ; } -if (!(incl_mask <=MTE_ALLOW_NON_ZERO_TAG )) { +if (incl_mask & ~MT_INCLUDE_TAG_MASK ) {ksft_print_msg ("FAIL: Invalid incl_mask %lx\n" , incl_mask);return -EINVAL ; }
Looks good to me.
thanks,
-- Shuah
PATCH 3/5 selftests/arm64: Check failures to set tags in check_tags_inclusion
In reply to Mark BrownThe MTE check_tags_inclusion test uses the mte_switch_mode() helper but ignores the return values it generates meaning we might not be testing the things we're trying to test, fail the test if it reports an error. The helper will log any errors it returns.
--- .../selftests/arm64/mte/check_tags_inclusion.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-)
tools/testing/selftests/arm64/mte/check_tags_inclusion.c +12 -6
@@ -49,7 +49,7 @@ static int verify_mte_pointer_validity(char *ptr, int mode)static int check_single_included_tags (int mem_type,int mode) {char *ptr; -int tag, run, result =KSFT_PASS ; +int tag, run, ret, result =KSFT_PASS ; ptr = (char *)mte_allocate_memory (BUFFER_SIZE +MT_GRANULE_SIZE , mem_type,0 , false); if (check_allocated_memory(ptr, BUFFER_SIZE + MT_GRANULE_SIZE,
@@ -57,7 +57,9 @@ static int check_single_included_tags(int mem_type, int mode)return KSFT_FAIL ;for (tag =0 ; (tag <MT_TAG_COUNT ) && (result ==KSFT_PASS ); tag++) { -mte_switch_mode (mode,MT_INCLUDE_VALID_TAG (tag)); + ret =mte_switch_mode (mode,MT_INCLUDE_VALID_TAG (tag)); +if (ret !=0 ) + result =KSFT_FAIL ;/* Try to catch a excluded tag by a number of tries. */ for (run =0 ; (run <RUNS ) && (result ==KSFT_PASS ); run++) { ptr = (char *)mte_insert_tags (ptr,BUFFER_SIZE );
@@ -111,14 +113,16 @@ static int check_multiple_included_tags(int mem_type, int mode)static int check_all_included_tags (int mem_type,int mode) {char *ptr; -int run, result =KSFT_PASS ; +int run, ret, result =KSFT_PASS ; ptr = (char *)mte_allocate_memory (BUFFER_SIZE +MT_GRANULE_SIZE , mem_type,0 , false);if (check_allocated_memory (ptr,BUFFER_SIZE +MT_GRANULE_SIZE , mem_type, false) !=KSFT_PASS )return KSFT_FAIL ; -mte_switch_mode (mode,MT_INCLUDE_TAG_MASK ); + ret =mte_switch_mode (mode,MT_INCLUDE_TAG_MASK ); +if (ret !=0 ) +return KSFT_FAIL ;/* Try to catch a excluded tag by a number of tries. */ for (run =0 ; (run <RUNS ) && (result ==KSFT_PASS ); run++) { ptr = (char *)mte_insert_tags (ptr,BUFFER_SIZE );
@@ -135,13 +139,15 @@ static int check_all_included_tags(int mem_type, int mode)static int check_none_included_tags (int mem_type,int mode) {char *ptr; -int run; +int run, ret; ptr = (char *)mte_allocate_memory (BUFFER_SIZE , mem_type,0 , false);if (check_allocated_memory (ptr,BUFFER_SIZE , mem_type, false) !=KSFT_PASS )return KSFT_FAIL ; -mte_switch_mode (mode,MT_EXCLUDE_TAG_MASK ); + ret =mte_switch_mode (mode,MT_EXCLUDE_TAG_MASK ); +if (ret !=0 ) +return KSFT_FAIL ;/* Try to catch a excluded tag by a number of tries. */ for (run =0 ; run <RUNS ; run++) { ptr = (char *)mte_insert_tags (ptr,BUFFER_SIZE );
Signature · 1 line
2.30.2
Re: [PATCH v1 3/5] selftests/arm64: Check failures to set tags in check_tags_inclusion
Quoted 10 lines · Mark Brown
The MTE
check_tags_inclusiontest uses themte_switch_mode()helper but ignores the return values it generates meaning we might not be testing the things we're trying to test, fail the test if it reports an error. The helper will log any errors it returns.Signed-off-by Mark Brown <[email protected]>--- .../selftests/arm64/mte/check_tags_inclusion.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-)
Thank you. Looks good to me.
thanks,
-- Shuah
PATCH 4/5 selftests/arm64: Remove casts to/from void in check_tags_inclusion
In reply to Mark BrownVoid pointers may be freely used with other pointer types in C, any casts between void * and other pointer types serve no purpose other than to mask potential warnings. Drop such casts from check_tags_inclusion to help with future review of the code.
--- .../arm64/mte/check_tags_inclusion.c | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-)
tools/testing/selftests/arm64/mte/check_tags_inclusion.c +12 -12
@@ -23,7 +23,7 @@ static int verify_mte_pointer_validity(char *ptr, int mode) {mte_initialize_current_context (mode, (uintptr_t )ptr,BUFFER_SIZE );/* Check the validity of the tagged pointer */ -memset ((void *)ptr,'1' ,BUFFER_SIZE ); +memset (ptr,'1' ,BUFFER_SIZE );mte_wait_after_trig ();if (cur_mte_cxt.fault_valid) { ksft_print_msg("Unexpected fault recorded for %p-%p in mode %x\n",
@@ -51,7 +51,7 @@ static int check_single_included_tags(int mem_type, int mode)char *ptr;int tag, run, ret, result =KSFT_PASS ; - ptr = (char *)mte_allocate_memory (BUFFER_SIZE +MT_GRANULE_SIZE , mem_type,0 , false); + ptr =mte_allocate_memory (BUFFER_SIZE +MT_GRANULE_SIZE , mem_type,0 , false);if (check_allocated_memory (ptr,BUFFER_SIZE +MT_GRANULE_SIZE , mem_type, false) !=KSFT_PASS )return KSFT_FAIL ;
@@ -62,7 +62,7 @@ static int check_single_included_tags(int mem_type, int mode) result =KSFT_FAIL ;/* Try to catch a excluded tag by a number of tries. */ for (run =0 ; (run <RUNS ) && (result ==KSFT_PASS ); run++) { - ptr = (char *)mte_insert_tags (ptr,BUFFER_SIZE ); + ptr =mte_insert_tags (ptr,BUFFER_SIZE );/* Check tag value */ if (MT_FETCH_TAG ((uintptr_t )ptr) == tag) { ksft_print_msg("FAIL: wrong tag = 0x%x with include mask=0x%x\n",
@@ -74,7 +74,7 @@ static int check_single_included_tags(int mem_type, int mode) result =verify_mte_pointer_validity (ptr, mode); } } -mte_free_memory_tag_range ((void *)ptr,BUFFER_SIZE , mem_type,0 ,MT_GRANULE_SIZE ); +mte_free_memory_tag_range (ptr,BUFFER_SIZE , mem_type,0 ,MT_GRANULE_SIZE );return result; }
@@ -84,7 +84,7 @@ static int check_multiple_included_tags(int mem_type, int mode)int tag, run, result =KSFT_PASS ;unsigned long excl_mask =0 ; - ptr = (char *)mte_allocate_memory (BUFFER_SIZE +MT_GRANULE_SIZE , mem_type,0 , false); + ptr =mte_allocate_memory (BUFFER_SIZE +MT_GRANULE_SIZE , mem_type,0 , false);if (check_allocated_memory (ptr,BUFFER_SIZE +MT_GRANULE_SIZE , mem_type, false) !=KSFT_PASS )return KSFT_FAIL ;
@@ -94,7 +94,7 @@ static int check_multiple_included_tags(int mem_type, int mode)mte_switch_mode (mode,MT_INCLUDE_VALID_TAGS (excl_mask));/* Try to catch a excluded tag by a number of tries. */ for (run =0 ; (run <RUNS ) && (result ==KSFT_PASS ); run++) { - ptr = (char *)mte_insert_tags (ptr,BUFFER_SIZE ); + ptr =mte_insert_tags (ptr,BUFFER_SIZE );/* Check tag value */ if (MT_FETCH_TAG ((uintptr_t )ptr) < tag) { ksft_print_msg("FAIL: wrong tag = 0x%x with include mask=0x%x\n",
@@ -106,7 +106,7 @@ static int check_multiple_included_tags(int mem_type, int mode) result =verify_mte_pointer_validity (ptr, mode); } } -mte_free_memory_tag_range ((void *)ptr,BUFFER_SIZE , mem_type,0 ,MT_GRANULE_SIZE ); +mte_free_memory_tag_range (ptr,BUFFER_SIZE , mem_type,0 ,MT_GRANULE_SIZE );return result; }
@@ -115,7 +115,7 @@ static int check_all_included_tags(int mem_type, int mode)char *ptr;int run, ret, result =KSFT_PASS ; - ptr = (char *)mte_allocate_memory (BUFFER_SIZE +MT_GRANULE_SIZE , mem_type,0 , false); + ptr =mte_allocate_memory (BUFFER_SIZE +MT_GRANULE_SIZE , mem_type,0 , false);if (check_allocated_memory (ptr,BUFFER_SIZE +MT_GRANULE_SIZE , mem_type, false) !=KSFT_PASS )return KSFT_FAIL ;
@@ -132,7 +132,7 @@ static int check_all_included_tags(int mem_type, int mode)*/ result =verify_mte_pointer_validity (ptr, mode); } -mte_free_memory_tag_range ((void *)ptr,BUFFER_SIZE , mem_type,0 ,MT_GRANULE_SIZE ); +mte_free_memory_tag_range (ptr,BUFFER_SIZE , mem_type,0 ,MT_GRANULE_SIZE );return result; }
@@ -141,7 +141,7 @@ static int check_none_included_tags(int mem_type, int mode)char *ptr;int run, ret; - ptr = (char *)mte_allocate_memory (BUFFER_SIZE , mem_type,0 , false); + ptr =mte_allocate_memory (BUFFER_SIZE , mem_type,0 , false);if (check_allocated_memory (ptr,BUFFER_SIZE , mem_type, false) !=KSFT_PASS )return KSFT_FAIL ;
@@ -159,12 +159,12 @@ static int check_none_included_tags(int mem_type, int mode) }mte_initialize_current_context (mode, (uintptr_t )ptr,BUFFER_SIZE );/* Check the write validity of the untagged pointer */ -memset ((void *)ptr,'1' ,BUFFER_SIZE ); +memset (ptr,'1' ,BUFFER_SIZE );mte_wait_after_trig ();if (cur_mte_cxt.fault_valid)break ; } -mte_free_memory ((void *)ptr,BUFFER_SIZE , mem_type, false); +mte_free_memory (ptr,BUFFER_SIZE , mem_type, false);if (cur_mte_cxt.fault_valid)return KSFT_FAIL ;else
Signature · 1 line
2.30.2
Re: [PATCH v1 4/5] selftests/arm64: Remove casts to/from void in check_tags_inclusion
Quoted 10 lines · Mark Brown
Void pointers may be freely used with other pointer types in C, any casts between void * and other pointer types serve no purpose other than to mask potential warnings. Drop such casts from
check_tags_inclusionto help with future review of the code.Signed-off-by Mark Brown <[email protected]>--- .../arm64/mte/check_tags_inclusion.c | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-)
Looks good to me.
thanks,
-- Shuah
PATCH 5/5 selftests/arm64: Use switch statements in mte_common_util.c
In reply to Mark BrownIn the MTE tests there are several places where we use chains of if statements to open code what could be written as switch statements, move over to switch statements to make the idiom clearer.
--- .../selftests/arm64/mte/mte_common_util.c | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-)
tools/testing/selftests/arm64/mte/mte_common_util.c +16 -7
@@ -128,13 +128,16 @@ static void *__mte_allocate_memory_range(size_t size, int mem_type, int mapping,int prot_flag, map_flag;size_t entire_size = size + range_before + range_after; -if (mem_type !=USE_MALLOC && mem_type !=USE_MMAP && - mem_type !=USE_MPROTECT ) { +switch (mem_type) { +case USE_MALLOC : +return malloc (entire_size) + range_before; +case USE_MMAP : +case USE_MPROTECT : +break ; +default :ksft_print_msg ("FAIL: Invalid allocate request\n" );return NULL ; } -if (mem_type ==USE_MALLOC ) -return malloc (entire_size) + range_before; prot_flag =PROT_READ |PROT_WRITE ;if (mem_type ==USE_MMAP )
@@ -287,13 +290,19 @@ int mte_switch_mode(int mte_option, unsigned long incl_mask)ksft_print_msg ("FAIL: Invalid incl_mask %lx\n" , incl_mask);return -EINVAL ; } + en =PR_TAGGED_ADDR_ENABLE ; -if (mte_option ==MTE_SYNC_ERR ) +switch (mte_option) { +case MTE_SYNC_ERR : en |=PR_MTE_TCF_SYNC ; -else if (mte_option ==MTE_ASYNC_ERR ) +break ; +case MTE_ASYNC_ERR : en |=PR_MTE_TCF_ASYNC ; -else if (mte_option ==MTE_NONE_ERR ) +break ; +case MTE_NONE_ERR : en |=PR_MTE_TCF_NONE ; +break ; + } en |= (incl_mask <<PR_MTE_TAG_SHIFT );/* Enable address tagging ABI, mte error reporting mode and tag inclusion mask. */
Signature · 1 line
2.30.2
Catalin Marinas
maintainerQuoted 5 lines · Mark Brown
This series contains some random cleanups and improvements that I came up with while looking at the
check_tags_inclusiontest. There's nothing too exciting in here but the changes did seem like they might help the next person to look at this.
Quoted 8 lines
Mark Brown (5):
selftests/arm64: Log errors inverify_mte_pointer_validity()selftests/arm64: Allow zero tags inmte_switch_mode()selftests/arm64: Check failures to set tags incheck_tags_inclusionselftests/arm64: Remove casts to/from void incheck_tags_inclusionselftests/arm64: Use switch statements inmte_common_util.c[...]
Applied to arm64 (for-next/kselftest), thanks!
[1/5] selftests/arm64: Log errors in verify_mte_pointer_validity()
https://git.kernel.org/arm64/c/9a5681710740 [2/5] selftests/arm64: Allow zero tags in mte_switch_mode() https://git.kernel.org/arm64/c/ffc8274c2193 [3/5] selftests/arm64: Check failures to set tags in check_tags_inclusion https://git.kernel.org/arm64/c/72d6771cb173 [4/5] selftests/arm64: Remove casts to/from void in check_tags_inclusion https://git.kernel.org/arm64/c/541235dee011 [5/5] selftests/arm64: Use switch statements in mte_common_util.c https://git.kernel.org/arm64/c/0639e02254e6
Signature · 1 line
Catalin