Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/mm: ensure non-global pkey symbols are marked static

The pkey tests define a whole lot of functions and some global variables.
A few are truly global (declared in pkey-helpers.h), but the majority are
file-scoped. Make sure those are labelled static.

Some of the pkey_{access,write}_{allow,deny} helpers are not called, or
only called when building for some architectures. Mark them
__maybe_unused to suppress compiler warnings.

Link: https://lkml.kernel.org/r/20241209095019.1732120-11-kevin.brodsky@arm.com
Signed-off-by: Kevin Brodsky <kevin.brodsky@arm.com>
Cc: Aruna Ramakrishna <aruna.ramakrishna@oracle.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Joey Gouly <joey.gouly@arm.com>
Cc: Keith Lucas <keith.lucas@oracle.com>
Cc: Ryan Roberts <ryan.roberts@arm.com>
Cc: Shuah Khan <shuah@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Kevin Brodsky and committed by
Andrew Morton
b0cc2984 f3f55597

+72 -69
+3
tools/testing/selftests/mm/pkey-helpers.h
··· 83 83 #ifndef noinline 84 84 # define noinline __attribute__((noinline)) 85 85 #endif 86 + #ifndef __maybe_unused 87 + # define __maybe_unused __attribute__((__unused__)) 88 + #endif 86 89 87 90 int sys_pkey_alloc(unsigned long flags, unsigned long init_val); 88 91 int sys_pkey_free(unsigned long pkey);
+3 -3
tools/testing/selftests/mm/pkey_sighandler_tests.c
··· 32 32 33 33 #define STACK_SIZE PTHREAD_STACK_MIN 34 34 35 - pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 36 - pthread_cond_t cond = PTHREAD_COND_INITIALIZER; 37 - siginfo_t siginfo = {0}; 35 + static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 36 + static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; 37 + static siginfo_t siginfo = {0}; 38 38 39 39 /* 40 40 * We need to use inline assembly instead of glibc's syscall because glibc's
+66 -66
tools/testing/selftests/mm/protection_keys.c
··· 61 61 return *ptr; 62 62 } 63 63 64 - void cat_into_file(char *str, char *file) 64 + static void cat_into_file(char *str, char *file) 65 65 { 66 66 int fd = open(file, O_RDWR); 67 67 int ret; ··· 88 88 89 89 #if CONTROL_TRACING > 0 90 90 static int warned_tracing; 91 - int tracing_root_ok(void) 91 + static int tracing_root_ok(void) 92 92 { 93 93 if (geteuid() != 0) { 94 94 if (!warned_tracing) ··· 101 101 } 102 102 #endif 103 103 104 - void tracing_on(void) 104 + static void tracing_on(void) 105 105 { 106 106 #if CONTROL_TRACING > 0 107 107 #define TRACEDIR "/sys/kernel/tracing" ··· 125 125 #endif 126 126 } 127 127 128 - void tracing_off(void) 128 + static void tracing_off(void) 129 129 { 130 130 #if CONTROL_TRACING > 0 131 131 if (!tracing_root_ok()) ··· 159 159 #else 160 160 __attribute__((__aligned__(PAGE_SIZE))) 161 161 #endif 162 - void lots_o_noops_around_write(int *write_to_me) 162 + static void lots_o_noops_around_write(int *write_to_me) 163 163 { 164 164 dprintf3("running %s()\n", __func__); 165 165 __page_o_noops(); ··· 170 170 dprintf3("%s() done\n", __func__); 171 171 } 172 172 173 - void dump_mem(void *dumpme, int len_bytes) 173 + static void dump_mem(void *dumpme, int len_bytes) 174 174 { 175 175 char *c = (void *)dumpme; 176 176 int i; ··· 213 213 return 0; 214 214 } 215 215 216 - void pkey_disable_set(int pkey, int flags) 216 + static void pkey_disable_set(int pkey, int flags) 217 217 { 218 218 unsigned long syscall_flags = 0; 219 219 int ret; ··· 251 251 pkey, flags); 252 252 } 253 253 254 - void pkey_disable_clear(int pkey, int flags) 254 + static void pkey_disable_clear(int pkey, int flags) 255 255 { 256 256 unsigned long syscall_flags = 0; 257 257 int ret; ··· 277 277 pkey, read_pkey_reg()); 278 278 } 279 279 280 - void pkey_write_allow(int pkey) 280 + __maybe_unused static void pkey_write_allow(int pkey) 281 281 { 282 282 pkey_disable_clear(pkey, PKEY_DISABLE_WRITE); 283 283 } 284 - void pkey_write_deny(int pkey) 284 + __maybe_unused static void pkey_write_deny(int pkey) 285 285 { 286 286 pkey_disable_set(pkey, PKEY_DISABLE_WRITE); 287 287 } 288 - void pkey_access_allow(int pkey) 288 + __maybe_unused static void pkey_access_allow(int pkey) 289 289 { 290 290 pkey_disable_clear(pkey, PKEY_DISABLE_ACCESS); 291 291 } 292 - void pkey_access_deny(int pkey) 292 + __maybe_unused static void pkey_access_deny(int pkey) 293 293 { 294 294 pkey_disable_set(pkey, PKEY_DISABLE_ACCESS); 295 295 } ··· 307 307 return "UNKNOWN"; 308 308 } 309 309 310 - int pkey_faults; 311 - int last_si_pkey = -1; 312 - void signal_handler(int signum, siginfo_t *si, void *vucontext) 310 + static int pkey_faults; 311 + static int last_si_pkey = -1; 312 + static void signal_handler(int signum, siginfo_t *si, void *vucontext) 313 313 { 314 314 ucontext_t *uctxt = vucontext; 315 315 int trapno; ··· 403 403 dprint_in_signal = 0; 404 404 } 405 405 406 - void sig_chld(int x) 406 + static void sig_chld(int x) 407 407 { 408 408 dprint_in_signal = 1; 409 409 dprintf2("[%d] SIGCHLD: %d\n", getpid(), x); 410 410 dprint_in_signal = 0; 411 411 } 412 412 413 - void setup_sigsegv_handler(void) 413 + static void setup_sigsegv_handler(void) 414 414 { 415 415 int r, rs; 416 416 struct sigaction newact; ··· 436 436 pkey_assert(r == 0); 437 437 } 438 438 439 - void setup_handlers(void) 439 + static void setup_handlers(void) 440 440 { 441 441 signal(SIGCHLD, &sig_chld); 442 442 setup_sigsegv_handler(); 443 443 } 444 444 445 - pid_t fork_lazy_child(void) 445 + static pid_t fork_lazy_child(void) 446 446 { 447 447 pid_t forkret; 448 448 ··· 488 488 return ret; 489 489 } 490 490 491 - int alloc_pkey(void) 491 + static int alloc_pkey(void) 492 492 { 493 493 int ret; 494 494 unsigned long init_val = 0x0; ··· 546 546 * not cleared. This ensures we get lots of random bit sets 547 547 * and clears on the vma and pte pkey bits. 548 548 */ 549 - int alloc_random_pkey(void) 549 + static int alloc_random_pkey(void) 550 550 { 551 551 int max_nr_pkey_allocs; 552 552 int ret; ··· 629 629 }; 630 630 struct pkey_malloc_record *pkey_malloc_records; 631 631 struct pkey_malloc_record *pkey_last_malloc_record; 632 - long nr_pkey_malloc_records; 632 + static long nr_pkey_malloc_records; 633 633 void record_pkey_malloc(void *ptr, long size, int prot) 634 634 { 635 635 long i; ··· 667 667 nr_pkey_malloc_records++; 668 668 } 669 669 670 - void free_pkey_malloc(void *ptr) 670 + static void free_pkey_malloc(void *ptr) 671 671 { 672 672 long i; 673 673 int ret; ··· 694 694 pkey_assert(false); 695 695 } 696 696 697 - 698 - void *malloc_pkey_with_mprotect(long size, int prot, u16 pkey) 697 + static void *malloc_pkey_with_mprotect(long size, int prot, u16 pkey) 699 698 { 700 699 void *ptr; 701 700 int ret; ··· 714 715 return ptr; 715 716 } 716 717 717 - void *malloc_pkey_anon_huge(long size, int prot, u16 pkey) 718 + static void *malloc_pkey_anon_huge(long size, int prot, u16 pkey) 718 719 { 719 720 int ret; 720 721 void *ptr; ··· 744 745 return ptr; 745 746 } 746 747 747 - int hugetlb_setup_ok; 748 + static int hugetlb_setup_ok; 748 749 #define SYSFS_FMT_NR_HUGE_PAGES "/sys/kernel/mm/hugepages/hugepages-%ldkB/nr_hugepages" 749 750 #define GET_NR_HUGE_PAGES 10 750 - void setup_hugetlbfs(void) 751 + static void setup_hugetlbfs(void) 751 752 { 752 753 int err; 753 754 int fd; ··· 795 796 hugetlb_setup_ok = 1; 796 797 } 797 798 798 - void *malloc_pkey_hugetlb(long size, int prot, u16 pkey) 799 + static void *malloc_pkey_hugetlb(long size, int prot, u16 pkey) 799 800 { 800 801 void *ptr; 801 802 int flags = MAP_ANONYMOUS|MAP_PRIVATE|MAP_HUGETLB; ··· 816 817 return ptr; 817 818 } 818 819 819 - void *(*pkey_malloc[])(long size, int prot, u16 pkey) = { 820 + static void *(*pkey_malloc[])(long size, int prot, u16 pkey) = { 820 821 821 822 malloc_pkey_with_mprotect, 822 823 malloc_pkey_with_mprotect_subpage, ··· 824 825 malloc_pkey_hugetlb 825 826 }; 826 827 827 - void *malloc_pkey(long size, int prot, u16 pkey) 828 + static void *malloc_pkey(long size, int prot, u16 pkey) 828 829 { 829 830 void *ret; 830 831 static int malloc_type; ··· 854 855 return ret; 855 856 } 856 857 857 - int last_pkey_faults; 858 + static int last_pkey_faults; 858 859 #define UNKNOWN_PKEY -2 859 860 void expected_pkey_fault(int pkey) 860 861 { ··· 896 897 pkey_assert(last_pkey_faults == pkey_faults); \ 897 898 } while (0) 898 899 899 - int test_fds[10] = { -1 }; 900 - int nr_test_fds; 901 - void __save_test_fd(int fd) 900 + static int test_fds[10] = { -1 }; 901 + static int nr_test_fds; 902 + static void __save_test_fd(int fd) 902 903 { 903 904 pkey_assert(fd >= 0); 904 905 pkey_assert(nr_test_fds < ARRAY_SIZE(test_fds)); ··· 906 907 nr_test_fds++; 907 908 } 908 909 909 - int get_test_read_fd(void) 910 + static int get_test_read_fd(void) 910 911 { 911 912 int test_fd = open("/etc/passwd", O_RDONLY); 912 913 __save_test_fd(test_fd); 913 914 return test_fd; 914 915 } 915 916 916 - void close_test_fds(void) 917 + static void close_test_fds(void) 917 918 { 918 919 int i; 919 920 ··· 926 927 nr_test_fds = 0; 927 928 } 928 929 929 - void test_pkey_alloc_free_attach_pkey0(int *ptr, u16 pkey) 930 + static void test_pkey_alloc_free_attach_pkey0(int *ptr, u16 pkey) 930 931 { 931 932 int i, err; 932 933 int max_nr_pkey_allocs; ··· 978 979 pkey_assert(!err); 979 980 } 980 981 981 - void test_read_of_write_disabled_region(int *ptr, u16 pkey) 982 + static void test_read_of_write_disabled_region(int *ptr, u16 pkey) 982 983 { 983 984 int ptr_contents; 984 985 ··· 988 989 dprintf1("*ptr: %d\n", ptr_contents); 989 990 dprintf1("\n"); 990 991 } 991 - void test_read_of_access_disabled_region(int *ptr, u16 pkey) 992 + static void test_read_of_access_disabled_region(int *ptr, u16 pkey) 992 993 { 993 994 int ptr_contents; 994 995 ··· 1000 1001 expected_pkey_fault(pkey); 1001 1002 } 1002 1003 1003 - void test_read_of_access_disabled_region_with_page_already_mapped(int *ptr, 1004 + static void test_read_of_access_disabled_region_with_page_already_mapped(int *ptr, 1004 1005 u16 pkey) 1005 1006 { 1006 1007 int ptr_contents; ··· 1017 1018 expected_pkey_fault(pkey); 1018 1019 } 1019 1020 1020 - void test_write_of_write_disabled_region_with_page_already_mapped(int *ptr, 1021 + static void test_write_of_write_disabled_region_with_page_already_mapped(int *ptr, 1021 1022 u16 pkey) 1022 1023 { 1023 1024 *ptr = __LINE__; ··· 1028 1029 expected_pkey_fault(pkey); 1029 1030 } 1030 1031 1031 - void test_write_of_write_disabled_region(int *ptr, u16 pkey) 1032 + static void test_write_of_write_disabled_region(int *ptr, u16 pkey) 1032 1033 { 1033 1034 dprintf1("disabling write access to PKEY[%02d], doing write\n", pkey); 1034 1035 pkey_write_deny(pkey); 1035 1036 *ptr = __LINE__; 1036 1037 expected_pkey_fault(pkey); 1037 1038 } 1038 - void test_write_of_access_disabled_region(int *ptr, u16 pkey) 1039 + static void test_write_of_access_disabled_region(int *ptr, u16 pkey) 1039 1040 { 1040 1041 dprintf1("disabling access to PKEY[%02d], doing write\n", pkey); 1041 1042 pkey_access_deny(pkey); ··· 1043 1044 expected_pkey_fault(pkey); 1044 1045 } 1045 1046 1046 - void test_write_of_access_disabled_region_with_page_already_mapped(int *ptr, 1047 + static void test_write_of_access_disabled_region_with_page_already_mapped(int *ptr, 1047 1048 u16 pkey) 1048 1049 { 1049 1050 *ptr = __LINE__; ··· 1054 1055 expected_pkey_fault(pkey); 1055 1056 } 1056 1057 1057 - void test_kernel_write_of_access_disabled_region(int *ptr, u16 pkey) 1058 + static void test_kernel_write_of_access_disabled_region(int *ptr, u16 pkey) 1058 1059 { 1059 1060 int ret; 1060 1061 int test_fd = get_test_read_fd(); ··· 1066 1067 dprintf1("read ret: %d\n", ret); 1067 1068 pkey_assert(ret); 1068 1069 } 1069 - void test_kernel_write_of_write_disabled_region(int *ptr, u16 pkey) 1070 + 1071 + static void test_kernel_write_of_write_disabled_region(int *ptr, u16 pkey) 1070 1072 { 1071 1073 int ret; 1072 1074 int test_fd = get_test_read_fd(); ··· 1080 1080 pkey_assert(ret); 1081 1081 } 1082 1082 1083 - void test_kernel_gup_of_access_disabled_region(int *ptr, u16 pkey) 1083 + static void test_kernel_gup_of_access_disabled_region(int *ptr, u16 pkey) 1084 1084 { 1085 1085 int pipe_ret, vmsplice_ret; 1086 1086 struct iovec iov; ··· 1102 1102 close(pipe_fds[1]); 1103 1103 } 1104 1104 1105 - void test_kernel_gup_write_to_write_disabled_region(int *ptr, u16 pkey) 1105 + static void test_kernel_gup_write_to_write_disabled_region(int *ptr, u16 pkey) 1106 1106 { 1107 1107 int ignored = 0xdada; 1108 1108 int futex_ret; ··· 1120 1120 } 1121 1121 1122 1122 /* Assumes that all pkeys other than 'pkey' are unallocated */ 1123 - void test_pkey_syscalls_on_non_allocated_pkey(int *ptr, u16 pkey) 1123 + static void test_pkey_syscalls_on_non_allocated_pkey(int *ptr, u16 pkey) 1124 1124 { 1125 1125 int err; 1126 1126 int i; ··· 1143 1143 } 1144 1144 1145 1145 /* Assumes that all pkeys other than 'pkey' are unallocated */ 1146 - void test_pkey_syscalls_bad_args(int *ptr, u16 pkey) 1146 + static void test_pkey_syscalls_bad_args(int *ptr, u16 pkey) 1147 1147 { 1148 1148 int err; 1149 1149 int bad_pkey = NR_PKEYS+99; ··· 1153 1153 pkey_assert(err); 1154 1154 } 1155 1155 1156 - void become_child(void) 1156 + static void become_child(void) 1157 1157 { 1158 1158 pid_t forkret; 1159 1159 ··· 1169 1169 } 1170 1170 1171 1171 /* Assumes that all pkeys other than 'pkey' are unallocated */ 1172 - void test_pkey_alloc_exhaust(int *ptr, u16 pkey) 1172 + static void test_pkey_alloc_exhaust(int *ptr, u16 pkey) 1173 1173 { 1174 1174 int err; 1175 1175 int allocated_pkeys[NR_PKEYS] = {0}; ··· 1236 1236 } 1237 1237 } 1238 1238 1239 - void arch_force_pkey_reg_init(void) 1239 + static void arch_force_pkey_reg_init(void) 1240 1240 { 1241 1241 #if defined(__i386__) || defined(__x86_64__) /* arch */ 1242 1242 u64 *buf; ··· 1275 1275 * a long-running test that continually checks the pkey 1276 1276 * register. 1277 1277 */ 1278 - void test_pkey_init_state(int *ptr, u16 pkey) 1278 + static void test_pkey_init_state(int *ptr, u16 pkey) 1279 1279 { 1280 1280 int err; 1281 1281 int allocated_pkeys[NR_PKEYS] = {0}; ··· 1313 1313 * have to call pkey_alloc() to use it first. Make sure that it 1314 1314 * is usable. 1315 1315 */ 1316 - void test_mprotect_with_pkey_0(int *ptr, u16 pkey) 1316 + static void test_mprotect_with_pkey_0(int *ptr, u16 pkey) 1317 1317 { 1318 1318 long size; 1319 1319 int prot; ··· 1337 1337 mprotect_pkey(ptr, size, prot, pkey); 1338 1338 } 1339 1339 1340 - void test_ptrace_of_child(int *ptr, u16 pkey) 1340 + static void test_ptrace_of_child(int *ptr, u16 pkey) 1341 1341 { 1342 1342 __attribute__((__unused__)) int peek_result; 1343 1343 pid_t child_pid; ··· 1413 1413 free(plain_ptr_unaligned); 1414 1414 } 1415 1415 1416 - void *get_pointer_to_instructions(void) 1416 + static void *get_pointer_to_instructions(void) 1417 1417 { 1418 1418 void *p1; 1419 1419 ··· 1434 1434 return p1; 1435 1435 } 1436 1436 1437 - void test_executing_on_unreadable_memory(int *ptr, u16 pkey) 1437 + static void test_executing_on_unreadable_memory(int *ptr, u16 pkey) 1438 1438 { 1439 1439 void *p1; 1440 1440 int scratch; ··· 1466 1466 pkey_assert(!ret); 1467 1467 } 1468 1468 1469 - void test_implicit_mprotect_exec_only_memory(int *ptr, u16 pkey) 1469 + static void test_implicit_mprotect_exec_only_memory(int *ptr, u16 pkey) 1470 1470 { 1471 1471 void *p1; 1472 1472 int scratch; ··· 1515 1515 } 1516 1516 1517 1517 #if defined(__i386__) || defined(__x86_64__) 1518 - void test_ptrace_modifies_pkru(int *ptr, u16 pkey) 1518 + static void test_ptrace_modifies_pkru(int *ptr, u16 pkey) 1519 1519 { 1520 1520 u32 new_pkru; 1521 1521 pid_t child; ··· 1638 1638 #endif 1639 1639 1640 1640 #if defined(__aarch64__) 1641 - void test_ptrace_modifies_pkru(int *ptr, u16 pkey) 1641 + static void test_ptrace_modifies_pkru(int *ptr, u16 pkey) 1642 1642 { 1643 1643 pid_t child; 1644 1644 int status, ret; ··· 1715 1715 } 1716 1716 #endif 1717 1717 1718 - void test_mprotect_pkey_on_unsupported_cpu(int *ptr, u16 pkey) 1718 + static void test_mprotect_pkey_on_unsupported_cpu(int *ptr, u16 pkey) 1719 1719 { 1720 1720 int size = PAGE_SIZE; 1721 1721 int sret; ··· 1729 1729 pkey_assert(sret < 0); 1730 1730 } 1731 1731 1732 - void (*pkey_tests[])(int *ptr, u16 pkey) = { 1732 + static void (*pkey_tests[])(int *ptr, u16 pkey) = { 1733 1733 test_read_of_write_disabled_region, 1734 1734 test_read_of_access_disabled_region, 1735 1735 test_read_of_access_disabled_region_with_page_already_mapped, ··· 1755 1755 #endif 1756 1756 }; 1757 1757 1758 - void run_tests_once(void) 1758 + static void run_tests_once(void) 1759 1759 { 1760 1760 int *ptr; 1761 1761 int prot = PROT_READ|PROT_WRITE; ··· 1789 1789 iteration_nr++; 1790 1790 } 1791 1791 1792 - void pkey_setup_shadow(void) 1792 + static void pkey_setup_shadow(void) 1793 1793 { 1794 1794 shadow_pkey_reg = __read_pkey_reg(); 1795 1795 }