Commit c23ea617 authored by Muhammad Usama Anjum's avatar Muhammad Usama Anjum Committed by Andrew Morton

selftests/mm: protection_keys: save/restore nr_hugepages settings

Save and restore nr_hugepages before changing it during the test.  A test
should not change system wide settings.

Link: https://lkml.kernel.org/r/20240125154608.720072-5-usama.anjum@collabora.com
Fixes: 5f23f6d0 ("x86/pkeys: Add self-tests")
Signed-off-by: default avatarMuhammad Usama Anjum <usama.anjum@collabora.com>
Cc: Ryan Roberts <ryan.roberts@arm.com>
Cc: Shuah Khan <shuah@kernel.org>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
parent 7d695b1c
...@@ -54,6 +54,7 @@ int test_nr; ...@@ -54,6 +54,7 @@ int test_nr;
u64 shadow_pkey_reg; u64 shadow_pkey_reg;
int dprint_in_signal; int dprint_in_signal;
char dprint_in_signal_buffer[DPRINT_IN_SIGNAL_BUF_SIZE]; char dprint_in_signal_buffer[DPRINT_IN_SIGNAL_BUF_SIZE];
char buf[256];
void cat_into_file(char *str, char *file) void cat_into_file(char *str, char *file)
{ {
...@@ -1744,6 +1745,38 @@ void pkey_setup_shadow(void) ...@@ -1744,6 +1745,38 @@ void pkey_setup_shadow(void)
shadow_pkey_reg = __read_pkey_reg(); shadow_pkey_reg = __read_pkey_reg();
} }
void restore_settings_atexit(void)
{
cat_into_file(buf, "/proc/sys/vm/nr_hugepages");
}
void save_settings(void)
{
int fd;
int err;
if (geteuid())
return;
fd = open("/proc/sys/vm/nr_hugepages", O_RDONLY);
if (fd < 0) {
fprintf(stderr, "error opening\n");
perror("error: ");
exit(__LINE__);
}
/* -1 to guarantee leaving the trailing \0 */
err = read(fd, buf, sizeof(buf)-1);
if (err < 0) {
fprintf(stderr, "error reading\n");
perror("error: ");
exit(__LINE__);
}
atexit(restore_settings_atexit);
close(fd);
}
int main(void) int main(void)
{ {
int nr_iterations = 22; int nr_iterations = 22;
...@@ -1751,6 +1784,7 @@ int main(void) ...@@ -1751,6 +1784,7 @@ int main(void)
srand((unsigned int)time(NULL)); srand((unsigned int)time(NULL));
save_settings();
setup_handlers(); setup_handlers();
printf("has pkeys: %d\n", pkeys_supported); printf("has pkeys: %d\n", pkeys_supported);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment