mirror of
				git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
				synced 2025-10-31 08:44:41 +00:00 
			
		
		
		
	kcsan: selftest: Cleanup and add missing __init
Make test_encode_decode() more readable and add missing __init. Signed-off-by: Marco Elver <elver@google.com> Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
This commit is contained in:
		
							parent
							
								
									78c3d954e2
								
							
						
					
					
						commit
						ac20e39e8d
					
				
					 1 changed files with 29 additions and 41 deletions
				
			
		|  | @ -18,7 +18,7 @@ | ||||||
| #define ITERS_PER_TEST 2000 | #define ITERS_PER_TEST 2000 | ||||||
| 
 | 
 | ||||||
| /* Test requirements. */ | /* Test requirements. */ | ||||||
| static bool test_requires(void) | static bool __init test_requires(void) | ||||||
| { | { | ||||||
| 	/* random should be initialized for the below tests */ | 	/* random should be initialized for the below tests */ | ||||||
| 	return prandom_u32() + prandom_u32() != 0; | 	return prandom_u32() + prandom_u32() != 0; | ||||||
|  | @ -28,14 +28,18 @@ static bool test_requires(void) | ||||||
|  * Test watchpoint encode and decode: check that encoding some access's info, |  * Test watchpoint encode and decode: check that encoding some access's info, | ||||||
|  * and then subsequent decode preserves the access's info. |  * and then subsequent decode preserves the access's info. | ||||||
|  */ |  */ | ||||||
| static bool test_encode_decode(void) | static bool __init test_encode_decode(void) | ||||||
| { | { | ||||||
| 	int i; | 	int i; | ||||||
| 
 | 
 | ||||||
| 	for (i = 0; i < ITERS_PER_TEST; ++i) { | 	for (i = 0; i < ITERS_PER_TEST; ++i) { | ||||||
| 		size_t size = prandom_u32_max(MAX_ENCODABLE_SIZE) + 1; | 		size_t size = prandom_u32_max(MAX_ENCODABLE_SIZE) + 1; | ||||||
| 		bool is_write = !!prandom_u32_max(2); | 		bool is_write = !!prandom_u32_max(2); | ||||||
|  | 		unsigned long verif_masked_addr; | ||||||
|  | 		long encoded_watchpoint; | ||||||
|  | 		bool verif_is_write; | ||||||
| 		unsigned long addr; | 		unsigned long addr; | ||||||
|  | 		size_t verif_size; | ||||||
| 
 | 
 | ||||||
| 		prandom_bytes(&addr, sizeof(addr)); | 		prandom_bytes(&addr, sizeof(addr)); | ||||||
| 		if (addr < PAGE_SIZE) | 		if (addr < PAGE_SIZE) | ||||||
|  | @ -44,31 +48,18 @@ static bool test_encode_decode(void) | ||||||
| 		if (WARN_ON(!check_encodable(addr, size))) | 		if (WARN_ON(!check_encodable(addr, size))) | ||||||
| 			return false; | 			return false; | ||||||
| 
 | 
 | ||||||
| 		/* Encode and decode */ | 		encoded_watchpoint = encode_watchpoint(addr, size, is_write); | ||||||
| 		{ |  | ||||||
| 			const long encoded_watchpoint = |  | ||||||
| 				encode_watchpoint(addr, size, is_write); |  | ||||||
| 			unsigned long verif_masked_addr; |  | ||||||
| 			size_t verif_size; |  | ||||||
| 			bool verif_is_write; |  | ||||||
| 
 | 
 | ||||||
| 		/* Check special watchpoints */ | 		/* Check special watchpoints */ | ||||||
| 			if (WARN_ON(decode_watchpoint( | 		if (WARN_ON(decode_watchpoint(INVALID_WATCHPOINT, &verif_masked_addr, &verif_size, &verif_is_write))) | ||||||
| 				    INVALID_WATCHPOINT, &verif_masked_addr, |  | ||||||
| 				    &verif_size, &verif_is_write))) |  | ||||||
| 			return false; | 			return false; | ||||||
| 			if (WARN_ON(decode_watchpoint( | 		if (WARN_ON(decode_watchpoint(CONSUMED_WATCHPOINT, &verif_masked_addr, &verif_size, &verif_is_write))) | ||||||
| 				    CONSUMED_WATCHPOINT, &verif_masked_addr, |  | ||||||
| 				    &verif_size, &verif_is_write))) |  | ||||||
| 			return false; | 			return false; | ||||||
| 
 | 
 | ||||||
| 		/* Check decoding watchpoint returns same data */ | 		/* Check decoding watchpoint returns same data */ | ||||||
| 			if (WARN_ON(!decode_watchpoint( | 		if (WARN_ON(!decode_watchpoint(encoded_watchpoint, &verif_masked_addr, &verif_size, &verif_is_write))) | ||||||
| 				    encoded_watchpoint, &verif_masked_addr, |  | ||||||
| 				    &verif_size, &verif_is_write))) |  | ||||||
| 			return false; | 			return false; | ||||||
| 			if (WARN_ON(verif_masked_addr != | 		if (WARN_ON(verif_masked_addr != (addr & WATCHPOINT_ADDR_MASK))) | ||||||
| 				    (addr & WATCHPOINT_ADDR_MASK))) |  | ||||||
| 			goto fail; | 			goto fail; | ||||||
| 		if (WARN_ON(verif_size != size)) | 		if (WARN_ON(verif_size != size)) | ||||||
| 			goto fail; | 			goto fail; | ||||||
|  | @ -78,19 +69,16 @@ static bool test_encode_decode(void) | ||||||
| 		continue; | 		continue; | ||||||
| fail: | fail: | ||||||
| 		pr_err("%s fail: %s %zu bytes @ %lx -> encoded: %lx -> %s %zu bytes @ %lx\n", | 		pr_err("%s fail: %s %zu bytes @ %lx -> encoded: %lx -> %s %zu bytes @ %lx\n", | ||||||
| 			       __func__, is_write ? "write" : "read", size, | 		       __func__, is_write ? "write" : "read", size, addr, encoded_watchpoint, | ||||||
| 			       addr, encoded_watchpoint, | 		       verif_is_write ? "write" : "read", verif_size, verif_masked_addr); | ||||||
| 			       verif_is_write ? "write" : "read", verif_size, |  | ||||||
| 			       verif_masked_addr); |  | ||||||
| 		return false; | 		return false; | ||||||
| 	} | 	} | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	return true; | 	return true; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| /* Test access matching function. */ | /* Test access matching function. */ | ||||||
| static bool test_matching_access(void) | static bool __init test_matching_access(void) | ||||||
| { | { | ||||||
| 	if (WARN_ON(!matching_access(10, 1, 10, 1))) | 	if (WARN_ON(!matching_access(10, 1, 10, 1))) | ||||||
| 		return false; | 		return false; | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue
	
	 Marco Elver
						Marco Elver