Merge tag 'linux_kselftest-next-6.12-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest

Pull kselftest update from Shuah Khan:

 - test coverage for dup_fd() failure handling in unshare_fd()

 - new selftest for the acct() syscall

 - basic uprobe testcase

 - several small fixes and cleanups to existing tests

 - user and strscpy removal as they became kunit tests

 - fixes to build failures and warnings

* tag 'linux_kselftest-next-6.12-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest: (21 commits)
  selftests: kselftest: Use strerror() on nolibc
  selftests/timers: Remove unused NSEC_PER_SEC macro
  selftests:resctrl: Fix build failure on archs without __cpuid_count()
  selftests/ftrace: Fix eventfs ownership testcase to find mount point
  selftests: filesystems: fix warn_unused_result build warnings
  selftests:core: test coverage for dup_fd() failure handling in unshare_fd()
  selftests/ftrace: Fix test to handle both old and new kernels
  kselftest: timers: Fix const correctness
  selftests/ftrace: Add required dependency for kprobe tests
  selftests: rust: config: disable GCC_PLUGINS
  selftests: rust: config: add trailing newline
  tracing/selftests: Run the ownership test twice
  selftests/uprobes: Add a basic uprobe testcase
  selftests: harness: rename __constructor_order for clarification
  selftests: harness: remove unneeded __constructor_order_last()
  selftest: acct: Add selftest for the acct() syscall
  selftests: lib: remove strscpy test
  selftests: user: remove user suite
  kselftest: cpufreq: Add RTC wakeup alarm
  selftests/exec: Fix grammar in an error message.
  ...
This commit is contained in:
Linus Torvalds
2024-09-17 16:49:56 +02:00
34 changed files with 302 additions and 111 deletions

View File

@@ -1,4 +1,5 @@
# SPDX-License-Identifier: GPL-2.0
TARGETS += acct
TARGETS += alsa
TARGETS += amd-pstate
TARGETS += arm64
@@ -109,7 +110,6 @@ TARGETS += tmpfs
TARGETS += tpm2
TARGETS += tty
TARGETS += uevent
TARGETS += user
TARGETS += user_events
TARGETS += vDSO
TARGETS += mm

View File

@@ -0,0 +1,3 @@
acct_syscall
config
process_log

View File

@@ -0,0 +1,5 @@
# SPDX-License-Identifier: GPL-2.0
TEST_GEN_PROGS := acct_syscall
CFLAGS += -Wall
include ../lib.mk

View File

@@ -0,0 +1,78 @@
// SPDX-License-Identifier: GPL-2.0
/* kselftest for acct() system call
* The acct() system call enables or disables process accounting.
*/
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/wait.h>
#include "../kselftest.h"
int main(void)
{
char filename[] = "process_log";
FILE *fp;
pid_t child_pid;
int sz;
// Setting up kselftest framework
ksft_print_header();
ksft_set_plan(1);
// Check if test is run a root
if (geteuid()) {
ksft_test_result_skip("This test needs root to run!\n");
return 1;
}
// Create file to log closed processes
fp = fopen(filename, "w");
if (!fp) {
ksft_test_result_error("%s.\n", strerror(errno));
ksft_finished();
return 1;
}
acct(filename);
// Handle error conditions
if (errno) {
ksft_test_result_error("%s.\n", strerror(errno));
fclose(fp);
ksft_finished();
return 1;
}
// Create child process and wait for it to terminate.
child_pid = fork();
if (child_pid < 0) {
ksft_test_result_error("Creating a child process to log failed\n");
acct(NULL);
return 1;
} else if (child_pid > 0) {
wait(NULL);
fseek(fp, 0L, SEEK_END);
sz = ftell(fp);
acct(NULL);
if (sz <= 0) {
ksft_test_result_fail("Terminated child process not logged\n");
ksft_exit_fail();
return 1;
}
ksft_test_result_pass("Successfully logged terminated process.\n");
fclose(fp);
ksft_exit_pass();
return 0;
}
return 1;
}

View File

@@ -1,7 +1,7 @@
# SPDX-License-Identifier: GPL-2.0-only
CFLAGS += -g $(KHDR_INCLUDES)
TEST_GEN_PROGS := close_range_test
TEST_GEN_PROGS := close_range_test unshare_test
include ../lib.mk

View File

@@ -0,0 +1,94 @@
// SPDX-License-Identifier: GPL-2.0
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <linux/kernel.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syscall.h>
#include <unistd.h>
#include <sys/resource.h>
#include <linux/close_range.h>
#include "../kselftest_harness.h"
#include "../clone3/clone3_selftests.h"
TEST(unshare_EMFILE)
{
pid_t pid;
int status;
struct __clone_args args = {
.flags = CLONE_FILES,
.exit_signal = SIGCHLD,
};
int fd;
ssize_t n, n2;
static char buf[512], buf2[512];
struct rlimit rlimit;
int nr_open;
fd = open("/proc/sys/fs/nr_open", O_RDWR);
ASSERT_GE(fd, 0);
n = read(fd, buf, sizeof(buf));
ASSERT_GT(n, 0);
ASSERT_EQ(buf[n - 1], '\n');
ASSERT_EQ(sscanf(buf, "%d", &nr_open), 1);
ASSERT_EQ(0, getrlimit(RLIMIT_NOFILE, &rlimit));
/* bump fs.nr_open */
n2 = sprintf(buf2, "%d\n", nr_open + 1024);
lseek(fd, 0, SEEK_SET);
write(fd, buf2, n2);
/* bump ulimit -n */
rlimit.rlim_cur = nr_open + 1024;
rlimit.rlim_max = nr_open + 1024;
EXPECT_EQ(0, setrlimit(RLIMIT_NOFILE, &rlimit)) {
lseek(fd, 0, SEEK_SET);
write(fd, buf, n);
exit(EXIT_FAILURE);
}
/* get a descriptor past the old fs.nr_open */
EXPECT_GE(dup2(2, nr_open + 64), 0) {
lseek(fd, 0, SEEK_SET);
write(fd, buf, n);
exit(EXIT_FAILURE);
}
/* get descriptor table shared */
pid = sys_clone3(&args, sizeof(args));
EXPECT_GE(pid, 0) {
lseek(fd, 0, SEEK_SET);
write(fd, buf, n);
exit(EXIT_FAILURE);
}
if (pid == 0) {
int err;
/* restore fs.nr_open */
lseek(fd, 0, SEEK_SET);
write(fd, buf, n);
/* ... and now unshare(CLONE_FILES) must fail with EMFILE */
err = unshare(CLONE_FILES);
EXPECT_EQ(err, -1)
exit(EXIT_FAILURE);
EXPECT_EQ(errno, EMFILE)
exit(EXIT_FAILURE);
exit(EXIT_SUCCESS);
}
EXPECT_EQ(waitpid(pid, &status, 0), pid);
EXPECT_EQ(true, WIFEXITED(status));
EXPECT_EQ(0, WEXITSTATUS(status));
}
TEST_HARNESS_MAIN

View File

@@ -231,6 +231,21 @@ do_suspend()
for i in `seq 1 $2`; do
printf "Starting $1\n"
if [ "$3" = "rtc" ]; then
if ! command -v rtcwake &> /dev/null; then
printf "rtcwake could not be found, please install it.\n"
return 1
fi
rtcwake -m $filename -s 15
if [ $? -ne 0 ]; then
printf "Failed to suspend using RTC wake alarm\n"
return 1
fi
fi
echo $filename > $SYSFS/power/state
printf "Came out of $1\n"

View File

@@ -24,6 +24,8 @@ helpme()
[-t <basic: Basic cpufreq testing
suspend: suspend/resume,
hibernate: hibernate/resume,
suspend_rtc: suspend/resume back using the RTC wakeup alarm,
hibernate_rtc: hibernate/resume back using the RTC wakeup alarm,
modtest: test driver or governor modules. Only to be used with -d or -g options,
sptest1: Simple governor switch to produce lockdep.
sptest2: Concurrent governor switch to produce lockdep.
@@ -76,7 +78,8 @@ parse_arguments()
helpme
;;
t) # --func_type (Function to perform: basic, suspend, hibernate, modtest, sptest1/2/3/4 (default: basic))
t) # --func_type (Function to perform: basic, suspend, hibernate,
# suspend_rtc, hibernate_rtc, modtest, sptest1/2/3/4 (default: basic))
FUNC=$OPTARG
;;
@@ -121,6 +124,14 @@ do_test()
do_suspend "hibernate" 1
;;
"suspend_rtc")
do_suspend "suspend" 1 rtc
;;
"hibernate_rtc")
do_suspend "hibernate" 1 rtc
;;
"modtest")
# Do we have modules in place?
if [ -z $DRIVER_MOD ] && [ -z $GOVERNOR_MOD ]; then

View File

@@ -257,12 +257,6 @@ TEST_F(attest_fixture, att_inval_addr)
att_inval_addr_test(&self->uvio_attest.meas_addr, _metadata, self);
}
static void __attribute__((constructor)) __constructor_order_last(void)
{
if (!__constructor_order)
__constructor_order = _CONSTRUCTOR_ORDER_BACKWARD;
}
int main(int argc, char **argv)
{
int fd = open(UV_PATH, O_ACCMODE);

View File

@@ -117,7 +117,7 @@ static int check_execveat_invoked_rc(int fd, const char *path, int flags,
}
if ((WEXITSTATUS(status) != expected_rc) &&
(WEXITSTATUS(status) != expected_rc2)) {
ksft_print_msg("child %d exited with %d not %d nor %d\n",
ksft_print_msg("child %d exited with %d neither %d nor %d\n",
child, WEXITSTATUS(status), expected_rc,
expected_rc2);
ksft_test_result_fail("%s\n", test_name);

View File

@@ -319,8 +319,11 @@ static void test_listmount_ns(void)
* Tell our parent how many mounts we have, and then wait for it
* to tell us we're done.
*/
write(child_ready_pipe[1], &nr_mounts, sizeof(nr_mounts));
read(parent_ready_pipe[0], &cval, sizeof(cval));
if (write(child_ready_pipe[1], &nr_mounts, sizeof(nr_mounts)) !=
sizeof(nr_mounts))
ret = NSID_ERROR;
if (read(parent_ready_pipe[0], &cval, sizeof(cval)) != sizeof(cval))
ret = NSID_ERROR;
exit(NSID_PASS);
}

View File

@@ -6,6 +6,18 @@ original_group=`stat -c "%g" .`
original_owner=`stat -c "%u" .`
mount_point=`stat -c '%m' .`
# If stat -c '%m' does not work (e.g. busybox) or failed, try to use the
# current working directory (which should be a tracefs) as the mount point.
if [ ! -d "$mount_point" ]; then
if mount | grep -qw $PWD ; then
mount_point=$PWD
else
# If PWD doesn't work, that is an environmental problem.
exit_unresolved
fi
fi
mount_options=`mount | grep "$mount_point" | sed -e 's/.*(\(.*\)).*/\1/'`
# find another owner and group that is not the original
@@ -83,32 +95,38 @@ run_tests() {
done
}
mount -o remount,"$new_options" .
# Run the tests twice as leftovers can cause issues
for loop in 1 2 ; do
run_tests
echo "Running iteration $loop"
mount -o remount,"$mount_options" .
mount -o remount,"$new_options" .
for d in "." "events" "events/sched" "events/sched/sched_switch" "events/sched/sched_switch/enable" $canary; do
test "$d" $original_group
done
run_tests
mount -o remount,"$mount_options" .
for d in "." "events" "events/sched" "events/sched/sched_switch" "events/sched/sched_switch/enable" $canary; do
test "$d" $original_group
done
# check instances as well
chgrp $other_group instances
chgrp $other_group instances
instance="$(mktemp -u test-XXXXXX)"
instance="$(mktemp -u test-XXXXXX)"
mkdir instances/$instance
mkdir instances/$instance
cd instances/$instance
cd instances/$instance
run_tests
run_tests
cd ../..
cd ../..
rmdir instances/$instance
rmdir instances/$instance
chgrp $original_group instances
chgrp $original_group instances
done
exit 0

View File

@@ -0,0 +1,26 @@
#!/bin/sh
# SPDX-License-Identifier: GPL-2.0
# description: Generic dynamic event - add/remove/test uprobe events
# requires: uprobe_events
echo 0 > events/enable
echo > dynamic_events
echo 'cat /proc/$$/maps' | /bin/sh | \
grep "r-xp .*/bin/.*sh$" | \
awk '{printf "p:myevent %s:0x%s\n", $6,$3 }' >> uprobe_events
grep -q myevent uprobe_events
test -d events/uprobes/myevent
echo 1 > events/uprobes/myevent/enable
echo 'ls' | /bin/sh > /dev/null
echo 0 > events/uprobes/myevent/enable
grep -q myevent trace
echo "-:myevent" >> uprobe_events
! grep -q myevent uprobe_events
echo > uprobe_events
clear_trace

View File

@@ -19,7 +19,14 @@ fail() { # mesg
FILTER=set_ftrace_filter
FUNC1="schedule"
FUNC2="sched_tick"
if grep '^sched_tick\b' available_filter_functions; then
FUNC2="sched_tick"
elif grep '^scheduler_tick\b' available_filter_functions; then
FUNC2="scheduler_tick"
else
exit_unresolved
fi
ALL_FUNCS="#### all functions enabled ####"

View File

@@ -1,7 +1,7 @@
#!/bin/sh
# SPDX-License-Identifier: GPL-2.0
# description: Kprobe event char type argument
# requires: kprobe_events
# requires: kprobe_events available_filter_functions
case `uname -m` in
x86_64)

View File

@@ -1,7 +1,7 @@
#!/bin/sh
# SPDX-License-Identifier: GPL-2.0
# description: Kprobe event string type argument
# requires: kprobe_events
# requires: kprobe_events available_filter_functions
case `uname -m` in
x86_64)

View File

@@ -1357,12 +1357,6 @@ static int libbpf_print_fn(enum libbpf_print_level level,
return 0;
}
static void __attribute__((constructor)) __constructor_order_last(void)
{
if (!__constructor_order)
__constructor_order = _CONSTRUCTOR_ORDER_BACKWARD;
}
int main(int argc, char **argv)
{
/* Use libbpf 1.0 API mode */

View File

@@ -61,6 +61,7 @@
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
#endif
#if defined(__i386__) || defined(__x86_64__) /* arch */
/*
* gcc cpuid.h provides __cpuid_count() since v4.4.
* Clang/LLVM cpuid.h provides __cpuid_count() since v3.4.0.
@@ -75,6 +76,7 @@
: "=a" (a), "=b" (b), "=c" (c), "=d" (d) \
: "0" (level), "2" (count))
#endif
#endif /* end arch */
/* define kselftest exit codes */
#define KSFT_PASS 0
@@ -371,15 +373,7 @@ static inline __noreturn __printf(1, 2) void ksft_exit_fail_msg(const char *msg,
static inline __noreturn void ksft_exit_fail_perror(const char *msg)
{
#ifndef NOLIBC
ksft_exit_fail_msg("%s: %s (%d)\n", msg, strerror(errno), errno);
#else
/*
* nolibc doesn't provide strerror() and it seems
* inappropriate to add one, just print the errno.
*/
ksft_exit_fail_msg("%s: %d)\n", msg, errno);
#endif
}
static inline __noreturn void ksft_exit_xfail(void)

View File

@@ -488,12 +488,6 @@
* Use once to append a main() to the test file.
*/
#define TEST_HARNESS_MAIN \
static void __attribute__((constructor)) \
__constructor_order_last(void) \
{ \
if (!__constructor_order) \
__constructor_order = _CONSTRUCTOR_ORDER_BACKWARD; \
} \
int main(int argc, char **argv) { \
return test_harness_run(argc, argv); \
}
@@ -824,7 +818,7 @@
item->prev = item; \
return; \
} \
if (__constructor_order == _CONSTRUCTOR_ORDER_FORWARD) { \
if (__constructor_order_forward) { \
item->next = NULL; \
item->prev = head->prev; \
item->prev->next = item; \
@@ -888,10 +882,7 @@ struct __test_xfail {
}
static struct __fixture_metadata *__fixture_list = &_fixture_global;
static int __constructor_order;
#define _CONSTRUCTOR_ORDER_FORWARD 1
#define _CONSTRUCTOR_ORDER_BACKWARD -1
static bool __constructor_order_forward;
static inline void __register_fixture(struct __fixture_metadata *f)
{
@@ -942,7 +933,7 @@ static inline bool __test_passed(struct __test_metadata *metadata)
* list so tests are run in source declaration order.
* https://gcc.gnu.org/onlinedocs/gccint/Initialization.html
* However, it seems not all toolchains do this correctly, so use
* __constructor_order to detect which direction is called first
* __constructor_order_foward to detect which direction is called first
* and adjust list building logic to get things running in the right
* direction.
*/
@@ -1337,8 +1328,7 @@ static int test_harness_run(int argc, char **argv)
static void __attribute__((constructor)) __constructor_order_first(void)
{
if (!__constructor_order)
__constructor_order = _CONSTRUCTOR_ORDER_FORWARD;
__constructor_order_forward = true;
}
#endif /* __KSELFTEST_HARNESS_H */

View File

@@ -4,6 +4,5 @@
# No binaries, but make sure arg-less "make" doesn't trigger "run_tests"
all:
TEST_PROGS := printf.sh bitmap.sh prime_numbers.sh scanf.sh strscpy.sh
TEST_PROGS := printf.sh bitmap.sh prime_numbers.sh scanf.sh
include ../lib.mk

Some files were not shown because too many files have changed in this diff Show More