162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 262306a36Sopenharmony_ci#define _GNU_SOURCE 362306a36Sopenharmony_ci#include <linux/membarrier.h> 462306a36Sopenharmony_ci#include <syscall.h> 562306a36Sopenharmony_ci#include <stdio.h> 662306a36Sopenharmony_ci#include <errno.h> 762306a36Sopenharmony_ci#include <string.h> 862306a36Sopenharmony_ci#include <pthread.h> 962306a36Sopenharmony_ci 1062306a36Sopenharmony_ci#include "../kselftest.h" 1162306a36Sopenharmony_ci 1262306a36Sopenharmony_cistatic int registrations; 1362306a36Sopenharmony_ci 1462306a36Sopenharmony_cistatic int sys_membarrier(int cmd, int flags) 1562306a36Sopenharmony_ci{ 1662306a36Sopenharmony_ci return syscall(__NR_membarrier, cmd, flags); 1762306a36Sopenharmony_ci} 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_cistatic int test_membarrier_get_registrations(int cmd) 2062306a36Sopenharmony_ci{ 2162306a36Sopenharmony_ci int ret, flags = 0; 2262306a36Sopenharmony_ci const char *test_name = 2362306a36Sopenharmony_ci "sys membarrier MEMBARRIER_CMD_GET_REGISTRATIONS"; 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_ci registrations |= cmd; 2662306a36Sopenharmony_ci 2762306a36Sopenharmony_ci ret = sys_membarrier(MEMBARRIER_CMD_GET_REGISTRATIONS, 0); 2862306a36Sopenharmony_ci if (ret < 0) { 2962306a36Sopenharmony_ci ksft_exit_fail_msg( 3062306a36Sopenharmony_ci "%s test: flags = %d, errno = %d\n", 3162306a36Sopenharmony_ci test_name, flags, errno); 3262306a36Sopenharmony_ci } else if (ret != registrations) { 3362306a36Sopenharmony_ci ksft_exit_fail_msg( 3462306a36Sopenharmony_ci "%s test: flags = %d, ret = %d, registrations = %d\n", 3562306a36Sopenharmony_ci test_name, flags, ret, registrations); 3662306a36Sopenharmony_ci } 3762306a36Sopenharmony_ci ksft_test_result_pass( 3862306a36Sopenharmony_ci "%s test: flags = %d, ret = %d, registrations = %d\n", 3962306a36Sopenharmony_ci test_name, flags, ret, registrations); 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_ci return 0; 4262306a36Sopenharmony_ci} 4362306a36Sopenharmony_ci 4462306a36Sopenharmony_cistatic int test_membarrier_cmd_fail(void) 4562306a36Sopenharmony_ci{ 4662306a36Sopenharmony_ci int cmd = -1, flags = 0; 4762306a36Sopenharmony_ci const char *test_name = "sys membarrier invalid command"; 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_ci if (sys_membarrier(cmd, flags) != -1) { 5062306a36Sopenharmony_ci ksft_exit_fail_msg( 5162306a36Sopenharmony_ci "%s test: command = %d, flags = %d. Should fail, but passed\n", 5262306a36Sopenharmony_ci test_name, cmd, flags); 5362306a36Sopenharmony_ci } 5462306a36Sopenharmony_ci if (errno != EINVAL) { 5562306a36Sopenharmony_ci ksft_exit_fail_msg( 5662306a36Sopenharmony_ci "%s test: flags = %d. Should return (%d: \"%s\"), but returned (%d: \"%s\").\n", 5762306a36Sopenharmony_ci test_name, flags, EINVAL, strerror(EINVAL), 5862306a36Sopenharmony_ci errno, strerror(errno)); 5962306a36Sopenharmony_ci } 6062306a36Sopenharmony_ci 6162306a36Sopenharmony_ci ksft_test_result_pass( 6262306a36Sopenharmony_ci "%s test: command = %d, flags = %d, errno = %d. Failed as expected\n", 6362306a36Sopenharmony_ci test_name, cmd, flags, errno); 6462306a36Sopenharmony_ci return 0; 6562306a36Sopenharmony_ci} 6662306a36Sopenharmony_ci 6762306a36Sopenharmony_cistatic int test_membarrier_flags_fail(void) 6862306a36Sopenharmony_ci{ 6962306a36Sopenharmony_ci int cmd = MEMBARRIER_CMD_QUERY, flags = 1; 7062306a36Sopenharmony_ci const char *test_name = "sys membarrier MEMBARRIER_CMD_QUERY invalid flags"; 7162306a36Sopenharmony_ci 7262306a36Sopenharmony_ci if (sys_membarrier(cmd, flags) != -1) { 7362306a36Sopenharmony_ci ksft_exit_fail_msg( 7462306a36Sopenharmony_ci "%s test: flags = %d. Should fail, but passed\n", 7562306a36Sopenharmony_ci test_name, flags); 7662306a36Sopenharmony_ci } 7762306a36Sopenharmony_ci if (errno != EINVAL) { 7862306a36Sopenharmony_ci ksft_exit_fail_msg( 7962306a36Sopenharmony_ci "%s test: flags = %d. Should return (%d: \"%s\"), but returned (%d: \"%s\").\n", 8062306a36Sopenharmony_ci test_name, flags, EINVAL, strerror(EINVAL), 8162306a36Sopenharmony_ci errno, strerror(errno)); 8262306a36Sopenharmony_ci } 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ci ksft_test_result_pass( 8562306a36Sopenharmony_ci "%s test: flags = %d, errno = %d. Failed as expected\n", 8662306a36Sopenharmony_ci test_name, flags, errno); 8762306a36Sopenharmony_ci return 0; 8862306a36Sopenharmony_ci} 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_cistatic int test_membarrier_global_success(void) 9162306a36Sopenharmony_ci{ 9262306a36Sopenharmony_ci int cmd = MEMBARRIER_CMD_GLOBAL, flags = 0; 9362306a36Sopenharmony_ci const char *test_name = "sys membarrier MEMBARRIER_CMD_GLOBAL"; 9462306a36Sopenharmony_ci 9562306a36Sopenharmony_ci if (sys_membarrier(cmd, flags) != 0) { 9662306a36Sopenharmony_ci ksft_exit_fail_msg( 9762306a36Sopenharmony_ci "%s test: flags = %d, errno = %d\n", 9862306a36Sopenharmony_ci test_name, flags, errno); 9962306a36Sopenharmony_ci } 10062306a36Sopenharmony_ci 10162306a36Sopenharmony_ci ksft_test_result_pass( 10262306a36Sopenharmony_ci "%s test: flags = %d\n", test_name, flags); 10362306a36Sopenharmony_ci return 0; 10462306a36Sopenharmony_ci} 10562306a36Sopenharmony_ci 10662306a36Sopenharmony_cistatic int test_membarrier_private_expedited_fail(void) 10762306a36Sopenharmony_ci{ 10862306a36Sopenharmony_ci int cmd = MEMBARRIER_CMD_PRIVATE_EXPEDITED, flags = 0; 10962306a36Sopenharmony_ci const char *test_name = "sys membarrier MEMBARRIER_CMD_PRIVATE_EXPEDITED not registered failure"; 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_ci if (sys_membarrier(cmd, flags) != -1) { 11262306a36Sopenharmony_ci ksft_exit_fail_msg( 11362306a36Sopenharmony_ci "%s test: flags = %d. Should fail, but passed\n", 11462306a36Sopenharmony_ci test_name, flags); 11562306a36Sopenharmony_ci } 11662306a36Sopenharmony_ci if (errno != EPERM) { 11762306a36Sopenharmony_ci ksft_exit_fail_msg( 11862306a36Sopenharmony_ci "%s test: flags = %d. Should return (%d: \"%s\"), but returned (%d: \"%s\").\n", 11962306a36Sopenharmony_ci test_name, flags, EPERM, strerror(EPERM), 12062306a36Sopenharmony_ci errno, strerror(errno)); 12162306a36Sopenharmony_ci } 12262306a36Sopenharmony_ci 12362306a36Sopenharmony_ci ksft_test_result_pass( 12462306a36Sopenharmony_ci "%s test: flags = %d, errno = %d\n", 12562306a36Sopenharmony_ci test_name, flags, errno); 12662306a36Sopenharmony_ci return 0; 12762306a36Sopenharmony_ci} 12862306a36Sopenharmony_ci 12962306a36Sopenharmony_cistatic int test_membarrier_register_private_expedited_success(void) 13062306a36Sopenharmony_ci{ 13162306a36Sopenharmony_ci int cmd = MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED, flags = 0; 13262306a36Sopenharmony_ci const char *test_name = "sys membarrier MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED"; 13362306a36Sopenharmony_ci 13462306a36Sopenharmony_ci if (sys_membarrier(cmd, flags) != 0) { 13562306a36Sopenharmony_ci ksft_exit_fail_msg( 13662306a36Sopenharmony_ci "%s test: flags = %d, errno = %d\n", 13762306a36Sopenharmony_ci test_name, flags, errno); 13862306a36Sopenharmony_ci } 13962306a36Sopenharmony_ci 14062306a36Sopenharmony_ci ksft_test_result_pass( 14162306a36Sopenharmony_ci "%s test: flags = %d\n", 14262306a36Sopenharmony_ci test_name, flags); 14362306a36Sopenharmony_ci 14462306a36Sopenharmony_ci test_membarrier_get_registrations(cmd); 14562306a36Sopenharmony_ci return 0; 14662306a36Sopenharmony_ci} 14762306a36Sopenharmony_ci 14862306a36Sopenharmony_cistatic int test_membarrier_private_expedited_success(void) 14962306a36Sopenharmony_ci{ 15062306a36Sopenharmony_ci int cmd = MEMBARRIER_CMD_PRIVATE_EXPEDITED, flags = 0; 15162306a36Sopenharmony_ci const char *test_name = "sys membarrier MEMBARRIER_CMD_PRIVATE_EXPEDITED"; 15262306a36Sopenharmony_ci 15362306a36Sopenharmony_ci if (sys_membarrier(cmd, flags) != 0) { 15462306a36Sopenharmony_ci ksft_exit_fail_msg( 15562306a36Sopenharmony_ci "%s test: flags = %d, errno = %d\n", 15662306a36Sopenharmony_ci test_name, flags, errno); 15762306a36Sopenharmony_ci } 15862306a36Sopenharmony_ci 15962306a36Sopenharmony_ci ksft_test_result_pass( 16062306a36Sopenharmony_ci "%s test: flags = %d\n", 16162306a36Sopenharmony_ci test_name, flags); 16262306a36Sopenharmony_ci return 0; 16362306a36Sopenharmony_ci} 16462306a36Sopenharmony_ci 16562306a36Sopenharmony_cistatic int test_membarrier_private_expedited_sync_core_fail(void) 16662306a36Sopenharmony_ci{ 16762306a36Sopenharmony_ci int cmd = MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE, flags = 0; 16862306a36Sopenharmony_ci const char *test_name = "sys membarrier MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE not registered failure"; 16962306a36Sopenharmony_ci 17062306a36Sopenharmony_ci if (sys_membarrier(cmd, flags) != -1) { 17162306a36Sopenharmony_ci ksft_exit_fail_msg( 17262306a36Sopenharmony_ci "%s test: flags = %d. Should fail, but passed\n", 17362306a36Sopenharmony_ci test_name, flags); 17462306a36Sopenharmony_ci } 17562306a36Sopenharmony_ci if (errno != EPERM) { 17662306a36Sopenharmony_ci ksft_exit_fail_msg( 17762306a36Sopenharmony_ci "%s test: flags = %d. Should return (%d: \"%s\"), but returned (%d: \"%s\").\n", 17862306a36Sopenharmony_ci test_name, flags, EPERM, strerror(EPERM), 17962306a36Sopenharmony_ci errno, strerror(errno)); 18062306a36Sopenharmony_ci } 18162306a36Sopenharmony_ci 18262306a36Sopenharmony_ci ksft_test_result_pass( 18362306a36Sopenharmony_ci "%s test: flags = %d, errno = %d\n", 18462306a36Sopenharmony_ci test_name, flags, errno); 18562306a36Sopenharmony_ci return 0; 18662306a36Sopenharmony_ci} 18762306a36Sopenharmony_ci 18862306a36Sopenharmony_cistatic int test_membarrier_register_private_expedited_sync_core_success(void) 18962306a36Sopenharmony_ci{ 19062306a36Sopenharmony_ci int cmd = MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE, flags = 0; 19162306a36Sopenharmony_ci const char *test_name = "sys membarrier MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE"; 19262306a36Sopenharmony_ci 19362306a36Sopenharmony_ci if (sys_membarrier(cmd, flags) != 0) { 19462306a36Sopenharmony_ci ksft_exit_fail_msg( 19562306a36Sopenharmony_ci "%s test: flags = %d, errno = %d\n", 19662306a36Sopenharmony_ci test_name, flags, errno); 19762306a36Sopenharmony_ci } 19862306a36Sopenharmony_ci 19962306a36Sopenharmony_ci ksft_test_result_pass( 20062306a36Sopenharmony_ci "%s test: flags = %d\n", 20162306a36Sopenharmony_ci test_name, flags); 20262306a36Sopenharmony_ci 20362306a36Sopenharmony_ci test_membarrier_get_registrations(cmd); 20462306a36Sopenharmony_ci return 0; 20562306a36Sopenharmony_ci} 20662306a36Sopenharmony_ci 20762306a36Sopenharmony_cistatic int test_membarrier_private_expedited_sync_core_success(void) 20862306a36Sopenharmony_ci{ 20962306a36Sopenharmony_ci int cmd = MEMBARRIER_CMD_PRIVATE_EXPEDITED, flags = 0; 21062306a36Sopenharmony_ci const char *test_name = "sys membarrier MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE"; 21162306a36Sopenharmony_ci 21262306a36Sopenharmony_ci if (sys_membarrier(cmd, flags) != 0) { 21362306a36Sopenharmony_ci ksft_exit_fail_msg( 21462306a36Sopenharmony_ci "%s test: flags = %d, errno = %d\n", 21562306a36Sopenharmony_ci test_name, flags, errno); 21662306a36Sopenharmony_ci } 21762306a36Sopenharmony_ci 21862306a36Sopenharmony_ci ksft_test_result_pass( 21962306a36Sopenharmony_ci "%s test: flags = %d\n", 22062306a36Sopenharmony_ci test_name, flags); 22162306a36Sopenharmony_ci return 0; 22262306a36Sopenharmony_ci} 22362306a36Sopenharmony_ci 22462306a36Sopenharmony_cistatic int test_membarrier_register_global_expedited_success(void) 22562306a36Sopenharmony_ci{ 22662306a36Sopenharmony_ci int cmd = MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED, flags = 0; 22762306a36Sopenharmony_ci const char *test_name = "sys membarrier MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED"; 22862306a36Sopenharmony_ci 22962306a36Sopenharmony_ci if (sys_membarrier(cmd, flags) != 0) { 23062306a36Sopenharmony_ci ksft_exit_fail_msg( 23162306a36Sopenharmony_ci "%s test: flags = %d, errno = %d\n", 23262306a36Sopenharmony_ci test_name, flags, errno); 23362306a36Sopenharmony_ci } 23462306a36Sopenharmony_ci 23562306a36Sopenharmony_ci ksft_test_result_pass( 23662306a36Sopenharmony_ci "%s test: flags = %d\n", 23762306a36Sopenharmony_ci test_name, flags); 23862306a36Sopenharmony_ci 23962306a36Sopenharmony_ci test_membarrier_get_registrations(cmd); 24062306a36Sopenharmony_ci return 0; 24162306a36Sopenharmony_ci} 24262306a36Sopenharmony_ci 24362306a36Sopenharmony_cistatic int test_membarrier_global_expedited_success(void) 24462306a36Sopenharmony_ci{ 24562306a36Sopenharmony_ci int cmd = MEMBARRIER_CMD_GLOBAL_EXPEDITED, flags = 0; 24662306a36Sopenharmony_ci const char *test_name = "sys membarrier MEMBARRIER_CMD_GLOBAL_EXPEDITED"; 24762306a36Sopenharmony_ci 24862306a36Sopenharmony_ci if (sys_membarrier(cmd, flags) != 0) { 24962306a36Sopenharmony_ci ksft_exit_fail_msg( 25062306a36Sopenharmony_ci "%s test: flags = %d, errno = %d\n", 25162306a36Sopenharmony_ci test_name, flags, errno); 25262306a36Sopenharmony_ci } 25362306a36Sopenharmony_ci 25462306a36Sopenharmony_ci ksft_test_result_pass( 25562306a36Sopenharmony_ci "%s test: flags = %d\n", 25662306a36Sopenharmony_ci test_name, flags); 25762306a36Sopenharmony_ci return 0; 25862306a36Sopenharmony_ci} 25962306a36Sopenharmony_ci 26062306a36Sopenharmony_cistatic int test_membarrier_fail(void) 26162306a36Sopenharmony_ci{ 26262306a36Sopenharmony_ci int status; 26362306a36Sopenharmony_ci 26462306a36Sopenharmony_ci status = test_membarrier_cmd_fail(); 26562306a36Sopenharmony_ci if (status) 26662306a36Sopenharmony_ci return status; 26762306a36Sopenharmony_ci status = test_membarrier_flags_fail(); 26862306a36Sopenharmony_ci if (status) 26962306a36Sopenharmony_ci return status; 27062306a36Sopenharmony_ci status = test_membarrier_private_expedited_fail(); 27162306a36Sopenharmony_ci if (status) 27262306a36Sopenharmony_ci return status; 27362306a36Sopenharmony_ci status = sys_membarrier(MEMBARRIER_CMD_QUERY, 0); 27462306a36Sopenharmony_ci if (status < 0) { 27562306a36Sopenharmony_ci ksft_test_result_fail("sys_membarrier() failed\n"); 27662306a36Sopenharmony_ci return status; 27762306a36Sopenharmony_ci } 27862306a36Sopenharmony_ci if (status & MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE) { 27962306a36Sopenharmony_ci status = test_membarrier_private_expedited_sync_core_fail(); 28062306a36Sopenharmony_ci if (status) 28162306a36Sopenharmony_ci return status; 28262306a36Sopenharmony_ci } 28362306a36Sopenharmony_ci return 0; 28462306a36Sopenharmony_ci} 28562306a36Sopenharmony_ci 28662306a36Sopenharmony_cistatic int test_membarrier_success(void) 28762306a36Sopenharmony_ci{ 28862306a36Sopenharmony_ci int status; 28962306a36Sopenharmony_ci 29062306a36Sopenharmony_ci status = test_membarrier_global_success(); 29162306a36Sopenharmony_ci if (status) 29262306a36Sopenharmony_ci return status; 29362306a36Sopenharmony_ci status = test_membarrier_register_private_expedited_success(); 29462306a36Sopenharmony_ci if (status) 29562306a36Sopenharmony_ci return status; 29662306a36Sopenharmony_ci status = test_membarrier_private_expedited_success(); 29762306a36Sopenharmony_ci if (status) 29862306a36Sopenharmony_ci return status; 29962306a36Sopenharmony_ci status = sys_membarrier(MEMBARRIER_CMD_QUERY, 0); 30062306a36Sopenharmony_ci if (status < 0) { 30162306a36Sopenharmony_ci ksft_test_result_fail("sys_membarrier() failed\n"); 30262306a36Sopenharmony_ci return status; 30362306a36Sopenharmony_ci } 30462306a36Sopenharmony_ci if (status & MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE) { 30562306a36Sopenharmony_ci status = test_membarrier_register_private_expedited_sync_core_success(); 30662306a36Sopenharmony_ci if (status) 30762306a36Sopenharmony_ci return status; 30862306a36Sopenharmony_ci status = test_membarrier_private_expedited_sync_core_success(); 30962306a36Sopenharmony_ci if (status) 31062306a36Sopenharmony_ci return status; 31162306a36Sopenharmony_ci } 31262306a36Sopenharmony_ci /* 31362306a36Sopenharmony_ci * It is valid to send a global membarrier from a non-registered 31462306a36Sopenharmony_ci * process. 31562306a36Sopenharmony_ci */ 31662306a36Sopenharmony_ci status = test_membarrier_global_expedited_success(); 31762306a36Sopenharmony_ci if (status) 31862306a36Sopenharmony_ci return status; 31962306a36Sopenharmony_ci status = test_membarrier_register_global_expedited_success(); 32062306a36Sopenharmony_ci if (status) 32162306a36Sopenharmony_ci return status; 32262306a36Sopenharmony_ci status = test_membarrier_global_expedited_success(); 32362306a36Sopenharmony_ci if (status) 32462306a36Sopenharmony_ci return status; 32562306a36Sopenharmony_ci return 0; 32662306a36Sopenharmony_ci} 32762306a36Sopenharmony_ci 32862306a36Sopenharmony_cistatic int test_membarrier_query(void) 32962306a36Sopenharmony_ci{ 33062306a36Sopenharmony_ci int flags = 0, ret; 33162306a36Sopenharmony_ci 33262306a36Sopenharmony_ci ret = sys_membarrier(MEMBARRIER_CMD_QUERY, flags); 33362306a36Sopenharmony_ci if (ret < 0) { 33462306a36Sopenharmony_ci if (errno == ENOSYS) { 33562306a36Sopenharmony_ci /* 33662306a36Sopenharmony_ci * It is valid to build a kernel with 33762306a36Sopenharmony_ci * CONFIG_MEMBARRIER=n. However, this skips the tests. 33862306a36Sopenharmony_ci */ 33962306a36Sopenharmony_ci ksft_exit_skip( 34062306a36Sopenharmony_ci "sys membarrier (CONFIG_MEMBARRIER) is disabled.\n"); 34162306a36Sopenharmony_ci } 34262306a36Sopenharmony_ci ksft_exit_fail_msg("sys_membarrier() failed\n"); 34362306a36Sopenharmony_ci } 34462306a36Sopenharmony_ci if (!(ret & MEMBARRIER_CMD_GLOBAL)) 34562306a36Sopenharmony_ci ksft_exit_skip( 34662306a36Sopenharmony_ci "sys_membarrier unsupported: CMD_GLOBAL not found.\n"); 34762306a36Sopenharmony_ci 34862306a36Sopenharmony_ci ksft_test_result_pass("sys_membarrier available\n"); 34962306a36Sopenharmony_ci return 0; 35062306a36Sopenharmony_ci} 351