Skip to content

Commit

Permalink
selftests: mm: protection_keys: save/restore nr_hugepages value from …
Browse files Browse the repository at this point in the history
…launch script

The save/restore of nr_hugepages was added to the test itself by using the
atexit() functionality.  But it is broken as parent exits after creating
child.  Hence calling the atexit() function early.  That's not it.  The
child exits after creating its child and so on.

The parent cannot wait to get the termination status for its children as
it'll keep on holding the resources until the new pkey allocation fails. 
It is impossible to wait for exits of all the grand and great grand
children.  Hence the restoring of nr_hugepages value from parent is wrong.

Let's save/restore the nr_hugepages settings in the launch script
instead of doing it in the test.

Link: https://lkml.kernel.org/r/20240419115027.3848958-1-usama.anjum@collabora.com
Fixes: c52eb6d ("selftests: mm: restore settings from only parent process")
Signed-off-by: Muhammad Usama Anjum <usama.anjum@collabora.com>
Reported-by: Joey Gouly <joey.gouly@arm.com>
Closes: https://lore.kernel.org/all/20240418125250.GA2941398@e124191.cambridge.arm.com
Cc: Joey Gouly <joey.gouly@arm.com>
Cc: Shuah Khan <shuah@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
  • Loading branch information
musamaanjum authored and akpm00 committed Apr 25, 2024
1 parent 6fe6046 commit ed74abc
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 38 deletions.
38 changes: 0 additions & 38 deletions tools/testing/selftests/mm/protection_keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ int test_nr;
u64 shadow_pkey_reg;
int dprint_in_signal;
char dprint_in_signal_buffer[DPRINT_IN_SIGNAL_BUF_SIZE];
char buf[256];

void cat_into_file(char *str, char *file)
{
Expand Down Expand Up @@ -1745,50 +1744,13 @@ void pkey_setup_shadow(void)
shadow_pkey_reg = __read_pkey_reg();
}

pid_t parent_pid;

void restore_settings_atexit(void)
{
if (parent_pid == getpid())
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__);
}

parent_pid = getpid();
atexit(restore_settings_atexit);
close(fd);
}

int main(void)
{
int nr_iterations = 22;
int pkeys_supported = is_pkeys_supported();

srand((unsigned int)time(NULL));

save_settings();
setup_handlers();

printf("has pkeys: %d\n", pkeys_supported);
Expand Down
2 changes: 2 additions & 0 deletions tools/testing/selftests/mm/run_vmtests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ CATEGORY="ksm_numa" run_test ./ksm_tests -N -m 0
CATEGORY="ksm" run_test ./ksm_functional_tests

# protection_keys tests
nr_hugepgs=$(cat /proc/sys/vm/nr_hugepages)
if [ -x ./protection_keys_32 ]
then
CATEGORY="pkey" run_test ./protection_keys_32
Expand All @@ -394,6 +395,7 @@ if [ -x ./protection_keys_64 ]
then
CATEGORY="pkey" run_test ./protection_keys_64
fi
echo "$nr_hugepgs" > /proc/sys/vm/nr_hugepages

if [ -x ./soft-dirty ]
then
Expand Down

0 comments on commit ed74abc

Please sign in to comment.