summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/fchmodat2
diff options
context:
space:
mode:
authorSean Christopherson <seanjc@google.com>2026-09-23 09:37:21 -0700
committerPaolo Bonzini <pbonzini@redhat.com>2026-09-26 00:39:55 -0400
commit93de2a6a4b91b72607136dd656edf03fb399d27f (patch)
tree9220c76eb23cb39a146fac9692ee69afa4aa3135 /tools/testing/selftests/fchmodat2
downloadlinux-stable-93de2a6a4b91b72607136dd656edf03fb399d27f.tar.gz
linux-stable-93de2a6a4b91b72607136dd656edf03fb399d27f.zip
KVM: SEV: Do cache maintenance on the source VM during intra-host migrationgrafted
Manually perform cache maintenance on the source VM during intra-host migration to ensure no stale data is left in CPU caches after the VM is destroyed. Because the source VM is "converted" to a non-SEV VM, KVM's memory reclaim flows won't trigger cache maintenance, e.g. when all guest memory is reclaimed in response to detaching from the mmu_notifier. Note, relying on the destination VM to do cache maintenance isn't an option as KVM doesn't require identical guest memory configurations, i.e. the source VM may have access to memory that the destination VM does not. Enforcing equivalent memory configurations is infeasible, as it would require a *deep* comparison of memslots, e.g. to verify that not only are the memslot identical, but what the memslots point at is also identical. Fixes: b56639318bb2 ("KVM: SEV: Add support for SEV intra host migration") Cc: stable@vger.kernel.org Reported-by: Stefan Teodorescu <fane@google.com> Signed-off-by: Sean Christopherson <seanjc@google.com> Message-ID: <20260923163721.1584779-3-seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Diffstat (limited to 'tools/testing/selftests/fchmodat2')
-rw-r--r--tools/testing/selftests/fchmodat2/.gitignore2
-rw-r--r--tools/testing/selftests/fchmodat2/Makefile15
-rw-r--r--tools/testing/selftests/fchmodat2/fchmodat2_test.c200
3 files changed, 217 insertions, 0 deletions
diff --git a/tools/testing/selftests/fchmodat2/.gitignore b/tools/testing/selftests/fchmodat2/.gitignore
new file mode 100644
index 000000000..82a4846cb
--- /dev/null
+++ b/tools/testing/selftests/fchmodat2/.gitignore
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0-only
+/*_test
diff --git a/tools/testing/selftests/fchmodat2/Makefile b/tools/testing/selftests/fchmodat2/Makefile
new file mode 100644
index 000000000..4373cea79
--- /dev/null
+++ b/tools/testing/selftests/fchmodat2/Makefile
@@ -0,0 +1,15 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+CFLAGS += -Wall -O2 -g -fsanitize=address -fsanitize=undefined $(KHDR_INCLUDES)
+
+# gcc requires -static-libasan in order to ensure that Address Sanitizer's
+# library is the first one loaded. However, clang already statically links the
+# Address Sanitizer if -fsanitize is specified. Therefore, simply omit
+# -static-libasan for clang builds.
+ifeq ($(LLVM),)
+ CFLAGS += -static-libasan
+endif
+
+TEST_GEN_PROGS := fchmodat2_test
+
+include ../lib.mk
diff --git a/tools/testing/selftests/fchmodat2/fchmodat2_test.c b/tools/testing/selftests/fchmodat2/fchmodat2_test.c
new file mode 100644
index 000000000..8e1b6384b
--- /dev/null
+++ b/tools/testing/selftests/fchmodat2/fchmodat2_test.c
@@ -0,0 +1,200 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#define _GNU_SOURCE
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <syscall.h>
+#include <unistd.h>
+
+#include "kselftest.h"
+
+struct testdir {
+ char *dirname;
+ int dfd;
+};
+
+int sys_fchmodat2(int dfd, const char *filename, mode_t mode, int flags)
+{
+ int ret = syscall(__NR_fchmodat2, dfd, filename, mode, flags);
+
+ return ret >= 0 ? ret : -errno;
+}
+
+static void setup_testdir(struct testdir *testdir)
+{
+ int ret, dfd;
+ char dirname[] = "/tmp/ksft-fchmodat2.XXXXXX";
+
+ /* Make the top-level directory. */
+ if (!mkdtemp(dirname))
+ ksft_exit_fail_msg("%s: failed to create tmpdir\n", __func__);
+
+ dfd = open(dirname, O_PATH | O_DIRECTORY);
+ if (dfd < 0) {
+ ksft_perror("failed to open tmpdir");
+ goto err;
+ }
+
+ ret = openat(dfd, "regfile", O_CREAT | O_WRONLY | O_TRUNC, 0644);
+ if (ret < 0) {
+ ksft_perror("failed to create file in tmpdir");
+ goto err;
+ }
+ close(ret);
+
+ ret = symlinkat("regfile", dfd, "symlink");
+ if (ret < 0) {
+ ksft_perror("symlinkat() failed");
+ goto err_regfile;
+ }
+
+ testdir->dirname = strdup(dirname);
+ if (!testdir->dirname) {
+ ksft_perror("Out of memory");
+ goto err_symlink;
+ }
+ testdir->dfd = dfd;
+
+ return;
+
+err_symlink:
+ unlinkat(testdir->dfd, "symlink", 0);
+err_regfile:
+ unlinkat(testdir->dfd, "regfile", 0);
+err:
+ unlink(dirname);
+ ksft_exit_fail();
+}
+
+static void cleanup_testdir(struct testdir *testdir)
+{
+ unlinkat(testdir->dfd, "regfile", 0);
+ unlinkat(testdir->dfd, "symlink", 0);
+ rmdir(testdir->dirname);
+ free(testdir->dirname);
+}
+
+int expect_mode(int dfd, const char *filename, mode_t expect_mode)
+{
+ struct stat st;
+ int ret = fstatat(dfd, filename, &st, AT_SYMLINK_NOFOLLOW);
+
+ if (ret) {
+ ksft_perror("fstatat() failed\n");
+ return 0;
+ }
+
+ return (st.st_mode == expect_mode);
+}
+
+void test_regfile(void)
+{
+ struct testdir testdir;
+ int ret;
+
+ setup_testdir(&testdir);
+
+ ret = sys_fchmodat2(testdir.dfd, "regfile", 0640, 0);
+
+ if (ret < 0) {
+ ksft_perror("fchmodat2(noflag) failed");
+ goto out;
+ }
+
+ if (!expect_mode(testdir.dfd, "regfile", 0100640)) {
+ ksft_print_msg("%s: wrong file mode bits after fchmodat2\n",
+ __func__);
+ ret = 1;
+ goto out;
+ }
+
+ ret = sys_fchmodat2(testdir.dfd, "regfile", 0600, AT_SYMLINK_NOFOLLOW);
+
+ if (ret < 0) {
+ ksft_perror("fchmodat2(AT_SYMLINK_NOFOLLOW) failed");
+ goto out;
+ }
+
+ if (!expect_mode(testdir.dfd, "regfile", 0100600)) {
+ ksft_print_msg("%s: wrong file mode bits after fchmodat2 with nofollow\n",
+ __func__);
+ ret = 1;
+ }
+
+out:
+ ksft_test_result(ret == 0, "fchmodat2(regfile)\n");
+ cleanup_testdir(&testdir);
+}
+
+void test_symlink(void)
+{
+ struct testdir testdir;
+ int ret;
+
+ setup_testdir(&testdir);
+
+ ret = sys_fchmodat2(testdir.dfd, "symlink", 0640, 0);
+
+ if (ret < 0) {
+ ksft_perror("fchmodat2(noflag) failed");
+ goto err;
+ }
+
+ if (!expect_mode(testdir.dfd, "regfile", 0100640)) {
+ ksft_print_msg("%s: wrong file mode bits after fchmodat2\n",
+ __func__);
+ goto err;
+ }
+
+ if (!expect_mode(testdir.dfd, "symlink", 0120777)) {
+ ksft_print_msg("%s: wrong symlink mode bits after fchmodat2\n",
+ __func__);
+ goto err;
+ }
+
+ ret = sys_fchmodat2(testdir.dfd, "symlink", 0600, AT_SYMLINK_NOFOLLOW);
+
+ /*
+ * On certain filesystems (xfs or btrfs), chmod operation fails. So we
+ * first check the symlink target but if the operation fails we mark the
+ * test as skipped.
+ *
+ * https://sourceware.org/legacy-ml/libc-alpha/2020-02/msg00467.html
+ */
+ if (ret == 0 && !expect_mode(testdir.dfd, "symlink", 0120600)) {
+ ksft_print_msg("%s: wrong symlink mode bits after fchmodat2 with nofollow\n",
+ __func__);
+ ret = 1;
+ goto err;
+ }
+
+ if (!expect_mode(testdir.dfd, "regfile", 0100640)) {
+ ksft_print_msg("%s: wrong file mode bits after fchmodat2 with nofollow\n",
+ __func__);
+ }
+
+ if (ret != 0)
+ ksft_test_result_skip("fchmodat2(symlink)\n");
+ else
+ ksft_test_result_pass("fchmodat2(symlink)\n");
+ cleanup_testdir(&testdir);
+ return;
+
+err:
+ ksft_test_result_fail("fchmodat2(symlink)\n");
+ cleanup_testdir(&testdir);
+}
+
+#define NUM_TESTS 2
+
+int main(int argc, char **argv)
+{
+ ksft_print_header();
+ ksft_set_plan(NUM_TESTS);
+
+ test_regfile();
+ test_symlink();
+
+ ksft_finished();
+}