[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20220706082016.2603916-3-chao.p.peng@linux.intel.com>
Date: Wed, 6 Jul 2022 16:20:04 +0800
From: Chao Peng <chao.p.peng@...ux.intel.com>
To: kvm@...r.kernel.org, linux-kernel@...r.kernel.org,
linux-mm@...ck.org, linux-fsdevel@...r.kernel.org,
linux-api@...r.kernel.org, linux-doc@...r.kernel.org,
qemu-devel@...gnu.org, linux-kselftest@...r.kernel.org
Cc: Paolo Bonzini <pbonzini@...hat.com>,
Jonathan Corbet <corbet@....net>,
Sean Christopherson <seanjc@...gle.com>,
Vitaly Kuznetsov <vkuznets@...hat.com>,
Wanpeng Li <wanpengli@...cent.com>,
Jim Mattson <jmattson@...gle.com>,
Joerg Roedel <joro@...tes.org>,
Thomas Gleixner <tglx@...utronix.de>,
Ingo Molnar <mingo@...hat.com>, Borislav Petkov <bp@...en8.de>,
x86@...nel.org, "H . Peter Anvin" <hpa@...or.com>,
Hugh Dickins <hughd@...gle.com>,
Jeff Layton <jlayton@...nel.org>,
"J . Bruce Fields" <bfields@...ldses.org>,
Andrew Morton <akpm@...ux-foundation.org>,
Shuah Khan <shuah@...nel.org>, Mike Rapoport <rppt@...nel.org>,
Steven Price <steven.price@....com>,
"Maciej S . Szmigiero" <mail@...iej.szmigiero.name>,
Vlastimil Babka <vbabka@...e.cz>,
Vishal Annapurve <vannapurve@...gle.com>,
Yu Zhang <yu.c.zhang@...ux.intel.com>,
Chao Peng <chao.p.peng@...ux.intel.com>,
"Kirill A . Shutemov" <kirill.shutemov@...ux.intel.com>,
luto@...nel.org, jun.nakajima@...el.com, dave.hansen@...el.com,
ak@...ux.intel.com, david@...hat.com, aarcange@...hat.com,
ddutile@...hat.com, dhildenb@...hat.com,
Quentin Perret <qperret@...gle.com>,
Michael Roth <michael.roth@....com>, mhocko@...e.com,
Muchun Song <songmuchun@...edance.com>
Subject: [PATCH v7 02/14] selftests/memfd: Add tests for F_SEAL_AUTO_ALLOCATE
Add tests to verify sealing memfds with the F_SEAL_AUTO_ALLOCATE works
as expected.
Signed-off-by: Chao Peng <chao.p.peng@...ux.intel.com>
---
tools/testing/selftests/memfd/memfd_test.c | 166 +++++++++++++++++++++
1 file changed, 166 insertions(+)
diff --git a/tools/testing/selftests/memfd/memfd_test.c b/tools/testing/selftests/memfd/memfd_test.c
index 94df2692e6e4..b849ece295fd 100644
--- a/tools/testing/selftests/memfd/memfd_test.c
+++ b/tools/testing/selftests/memfd/memfd_test.c
@@ -9,6 +9,7 @@
#include <fcntl.h>
#include <linux/memfd.h>
#include <sched.h>
+#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
@@ -232,6 +233,31 @@ static void mfd_fail_open(int fd, int flags, mode_t mode)
}
}
+static void mfd_assert_fallocate(int fd)
+{
+ int r;
+
+ r = fallocate(fd, 0, 0, mfd_def_size);
+ if (r < 0) {
+ printf("fallocate(ALLOC) failed: %m\n");
+ abort();
+ }
+}
+
+static void mfd_assert_punch_hole(int fd)
+{
+ int r;
+
+ r = fallocate(fd,
+ FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
+ 0,
+ mfd_def_size);
+ if (r < 0) {
+ printf("fallocate(PUNCH_HOLE) failed: %m\n");
+ abort();
+ }
+}
+
static void mfd_assert_read(int fd)
{
char buf[16];
@@ -594,6 +620,94 @@ static void mfd_fail_grow_write(int fd)
}
}
+static void mfd_assert_hole_write(int fd)
+{
+ ssize_t l;
+ void *p;
+ char *p1;
+
+ /*
+ * huegtlbfs does not support write, but we want to
+ * verify everything else here.
+ */
+ if (!hugetlbfs_test) {
+ /* verify direct write() succeeds */
+ l = write(fd, "\0\0\0\0", 4);
+ if (l != 4) {
+ printf("write() failed: %m\n");
+ abort();
+ }
+ }
+
+ /* verify mmaped write succeeds */
+ p = mmap(NULL,
+ mfd_def_size,
+ PROT_READ | PROT_WRITE,
+ MAP_SHARED,
+ fd,
+ 0);
+ if (p == MAP_FAILED) {
+ printf("mmap() failed: %m\n");
+ abort();
+ }
+ p1 = (char *)p + mfd_def_size - 1;
+ *p1 = 'H';
+ if (*p1 != 'H') {
+ printf("mmaped write failed: %m\n");
+ abort();
+
+ }
+ munmap(p, mfd_def_size);
+}
+
+sigjmp_buf jbuf, *sigbuf;
+static void sig_handler(int sig, siginfo_t *siginfo, void *ptr)
+{
+ if (sig == SIGBUS) {
+ if (sigbuf)
+ siglongjmp(*sigbuf, 1);
+ abort();
+ }
+}
+
+static void mfd_fail_hole_write(int fd)
+{
+ ssize_t l;
+ void *p;
+ char *p1;
+
+ /* verify direct write() fails */
+ l = write(fd, "data", 4);
+ if (l > 0) {
+ printf("expected failure on write(), but got %d: %m\n", (int)l);
+ abort();
+ }
+
+ /* verify mmaped write fails */
+ p = mmap(NULL,
+ mfd_def_size,
+ PROT_READ | PROT_WRITE,
+ MAP_SHARED,
+ fd,
+ 0);
+ if (p == MAP_FAILED) {
+ printf("mmap() failed: %m\n");
+ abort();
+ }
+
+ sigbuf = &jbuf;
+ if (sigsetjmp(*sigbuf, 1))
+ goto out;
+
+ /* Below write should trigger SIGBUS signal */
+ p1 = (char *)p + mfd_def_size - 1;
+ *p1 = 'H';
+ printf("failed to receive SIGBUS for mmaped write: %m\n");
+ abort();
+out:
+ munmap(p, mfd_def_size);
+}
+
static int idle_thread_fn(void *arg)
{
sigset_t set;
@@ -880,6 +994,57 @@ static void test_seal_resize(void)
close(fd);
}
+/*
+ * Test F_SEAL_AUTO_ALLOCATE
+ * Test whether F_SEAL_AUTO_ALLOCATE actually prevents allocation.
+ */
+static void test_seal_auto_allocate(void)
+{
+ struct sigaction act;
+ int fd;
+
+ printf("%s SEAL-AUTO-ALLOCATE\n", memfd_str);
+
+ memset(&act, 0, sizeof(act));
+ act.sa_sigaction = sig_handler;
+ act.sa_flags = SA_SIGINFO;
+ if (sigaction(SIGBUS, &act, 0)) {
+ printf("sigaction() failed: %m\n");
+ abort();
+ }
+
+ fd = mfd_assert_new("kern_memfd_seal_auto_allocate",
+ mfd_def_size,
+ MFD_CLOEXEC | MFD_ALLOW_SEALING);
+
+ /* read/write should pass if F_SEAL_AUTO_ALLOCATE not set */
+ mfd_assert_read(fd);
+ mfd_assert_hole_write(fd);
+
+ mfd_assert_has_seals(fd, 0);
+ mfd_assert_add_seals(fd, F_SEAL_AUTO_ALLOCATE);
+ mfd_assert_has_seals(fd, F_SEAL_AUTO_ALLOCATE);
+
+ /* read/write should pass for pre-allocated area */
+ mfd_assert_read(fd);
+ mfd_assert_hole_write(fd);
+
+ mfd_assert_punch_hole(fd);
+
+ /* read should pass, write should fail in hole */
+ mfd_assert_read(fd);
+ mfd_fail_hole_write(fd);
+
+ mfd_assert_fallocate(fd);
+
+ /* read/write should pass after fallocate */
+ mfd_assert_read(fd);
+ mfd_assert_hole_write(fd);
+
+ close(fd);
+}
+
+
/*
* Test sharing via dup()
* Test that seals are shared between dupped FDs and they're all equal.
@@ -1059,6 +1224,7 @@ int main(int argc, char **argv)
test_seal_shrink();
test_seal_grow();
test_seal_resize();
+ test_seal_auto_allocate();
test_share_dup("SHARE-DUP", "");
test_share_mmap("SHARE-MMAP", "");
--
2.25.1
Powered by blists - more mailing lists