aboutsummaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/memfd/memfd_test.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/testing/selftests/memfd/memfd_test.c')
-rw-r--r--tools/testing/selftests/memfd/memfd_test.c212
1 files changed, 33 insertions, 179 deletions
diff --git a/tools/testing/selftests/memfd/memfd_test.c b/tools/testing/selftests/memfd/memfd_test.c
index 132a54f74e88..10baa1652fc2 100644
--- a/tools/testing/selftests/memfd/memfd_test.c
+++ b/tools/testing/selftests/memfd/memfd_test.c
@@ -19,7 +19,10 @@
19#include <sys/wait.h> 19#include <sys/wait.h>
20#include <unistd.h> 20#include <unistd.h>
21 21
22#include "common.h"
23
22#define MEMFD_STR "memfd:" 24#define MEMFD_STR "memfd:"
25#define MEMFD_HUGE_STR "memfd-hugetlb:"
23#define SHARED_FT_STR "(shared file-table)" 26#define SHARED_FT_STR "(shared file-table)"
24 27
25#define MFD_DEF_SIZE 8192 28#define MFD_DEF_SIZE 8192
@@ -28,41 +31,8 @@
28/* 31/*
29 * Default is not to test hugetlbfs 32 * Default is not to test hugetlbfs
30 */ 33 */
31static int hugetlbfs_test;
32static size_t mfd_def_size = MFD_DEF_SIZE; 34static size_t mfd_def_size = MFD_DEF_SIZE;
33 35static const char *memfd_str = MEMFD_STR;
34/*
35 * Copied from mlock2-tests.c
36 */
37static unsigned long default_huge_page_size(void)
38{
39 unsigned long hps = 0;
40 char *line = NULL;
41 size_t linelen = 0;
42 FILE *f = fopen("/proc/meminfo", "r");
43
44 if (!f)
45 return 0;
46 while (getline(&line, &linelen, f) > 0) {
47 if (sscanf(line, "Hugepagesize: %lu kB", &hps) == 1) {
48 hps <<= 10;
49 break;
50 }
51 }
52
53 free(line);
54 fclose(f);
55 return hps;
56}
57
58static int sys_memfd_create(const char *name,
59 unsigned int flags)
60{
61 if (hugetlbfs_test)
62 flags |= MFD_HUGETLB;
63
64 return syscall(__NR_memfd_create, name, flags);
65}
66 36
67static int mfd_assert_new(const char *name, loff_t sz, unsigned int flags) 37static int mfd_assert_new(const char *name, loff_t sz, unsigned int flags)
68{ 38{
@@ -513,6 +483,10 @@ static void mfd_assert_grow_write(int fd)
513 static char *buf; 483 static char *buf;
514 ssize_t l; 484 ssize_t l;
515 485
486 /* hugetlbfs does not support write */
487 if (hugetlbfs_test)
488 return;
489
516 buf = malloc(mfd_def_size * 8); 490 buf = malloc(mfd_def_size * 8);
517 if (!buf) { 491 if (!buf) {
518 printf("malloc(%zu) failed: %m\n", mfd_def_size * 8); 492 printf("malloc(%zu) failed: %m\n", mfd_def_size * 8);
@@ -533,6 +507,10 @@ static void mfd_fail_grow_write(int fd)
533 static char *buf; 507 static char *buf;
534 ssize_t l; 508 ssize_t l;
535 509
510 /* hugetlbfs does not support write */
511 if (hugetlbfs_test)
512 return;
513
536 buf = malloc(mfd_def_size * 8); 514 buf = malloc(mfd_def_size * 8);
537 if (!buf) { 515 if (!buf) {
538 printf("malloc(%zu) failed: %m\n", mfd_def_size * 8); 516 printf("malloc(%zu) failed: %m\n", mfd_def_size * 8);
@@ -598,7 +576,7 @@ static void test_create(void)
598 char buf[2048]; 576 char buf[2048];
599 int fd; 577 int fd;
600 578
601 printf("%s CREATE\n", MEMFD_STR); 579 printf("%s CREATE\n", memfd_str);
602 580
603 /* test NULL name */ 581 /* test NULL name */
604 mfd_fail_new(NULL, 0); 582 mfd_fail_new(NULL, 0);
@@ -627,18 +605,13 @@ static void test_create(void)
627 fd = mfd_assert_new("", 0, MFD_CLOEXEC); 605 fd = mfd_assert_new("", 0, MFD_CLOEXEC);
628 close(fd); 606 close(fd);
629 607
630 if (!hugetlbfs_test) { 608 /* verify MFD_ALLOW_SEALING is allowed */
631 /* verify MFD_ALLOW_SEALING is allowed */ 609 fd = mfd_assert_new("", 0, MFD_ALLOW_SEALING);
632 fd = mfd_assert_new("", 0, MFD_ALLOW_SEALING); 610 close(fd);
633 close(fd); 611
634 612 /* verify MFD_ALLOW_SEALING | MFD_CLOEXEC is allowed */
635 /* verify MFD_ALLOW_SEALING | MFD_CLOEXEC is allowed */ 613 fd = mfd_assert_new("", 0, MFD_ALLOW_SEALING | MFD_CLOEXEC);
636 fd = mfd_assert_new("", 0, MFD_ALLOW_SEALING | MFD_CLOEXEC); 614 close(fd);
637 close(fd);
638 } else {
639 /* sealing is not supported on hugetlbfs */
640 mfd_fail_new("", MFD_ALLOW_SEALING);
641 }
642} 615}
643 616
644/* 617/*
@@ -649,11 +622,7 @@ static void test_basic(void)
649{ 622{
650 int fd; 623 int fd;
651 624
652 /* hugetlbfs does not contain sealing support */ 625 printf("%s BASIC\n", memfd_str);
653 if (hugetlbfs_test)
654 return;
655
656 printf("%s BASIC\n", MEMFD_STR);
657 626
658 fd = mfd_assert_new("kern_memfd_basic", 627 fd = mfd_assert_new("kern_memfd_basic",
659 mfd_def_size, 628 mfd_def_size,
@@ -698,28 +667,6 @@ static void test_basic(void)
698} 667}
699 668
700/* 669/*
701 * hugetlbfs doesn't support seals or write, so just verify grow and shrink
702 * on a hugetlbfs file created via memfd_create.
703 */
704static void test_hugetlbfs_grow_shrink(void)
705{
706 int fd;
707
708 printf("%s HUGETLBFS-GROW-SHRINK\n", MEMFD_STR);
709
710 fd = mfd_assert_new("kern_memfd_seal_write",
711 mfd_def_size,
712 MFD_CLOEXEC);
713
714 mfd_assert_read(fd);
715 mfd_assert_write(fd);
716 mfd_assert_shrink(fd);
717 mfd_assert_grow(fd);
718
719 close(fd);
720}
721
722/*
723 * Test SEAL_WRITE 670 * Test SEAL_WRITE
724 * Test whether SEAL_WRITE actually prevents modifications. 671 * Test whether SEAL_WRITE actually prevents modifications.
725 */ 672 */
@@ -727,14 +674,7 @@ static void test_seal_write(void)
727{ 674{
728 int fd; 675 int fd;
729 676
730 /* 677 printf("%s SEAL-WRITE\n", memfd_str);
731 * hugetlbfs does not contain sealing or write support. Just test
732 * basic grow and shrink via test_hugetlbfs_grow_shrink.
733 */
734 if (hugetlbfs_test)
735 return test_hugetlbfs_grow_shrink();
736
737 printf("%s SEAL-WRITE\n", MEMFD_STR);
738 678
739 fd = mfd_assert_new("kern_memfd_seal_write", 679 fd = mfd_assert_new("kern_memfd_seal_write",
740 mfd_def_size, 680 mfd_def_size,
@@ -760,11 +700,7 @@ static void test_seal_shrink(void)
760{ 700{
761 int fd; 701 int fd;
762 702
763 /* hugetlbfs does not contain sealing support */ 703 printf("%s SEAL-SHRINK\n", memfd_str);
764 if (hugetlbfs_test)
765 return;
766
767 printf("%s SEAL-SHRINK\n", MEMFD_STR);
768 704
769 fd = mfd_assert_new("kern_memfd_seal_shrink", 705 fd = mfd_assert_new("kern_memfd_seal_shrink",
770 mfd_def_size, 706 mfd_def_size,
@@ -790,11 +726,7 @@ static void test_seal_grow(void)
790{ 726{
791 int fd; 727 int fd;
792 728
793 /* hugetlbfs does not contain sealing support */ 729 printf("%s SEAL-GROW\n", memfd_str);
794 if (hugetlbfs_test)
795 return;
796
797 printf("%s SEAL-GROW\n", MEMFD_STR);
798 730
799 fd = mfd_assert_new("kern_memfd_seal_grow", 731 fd = mfd_assert_new("kern_memfd_seal_grow",
800 mfd_def_size, 732 mfd_def_size,
@@ -820,11 +752,7 @@ static void test_seal_resize(void)
820{ 752{
821 int fd; 753 int fd;
822 754
823 /* hugetlbfs does not contain sealing support */ 755 printf("%s SEAL-RESIZE\n", memfd_str);
824 if (hugetlbfs_test)
825 return;
826
827 printf("%s SEAL-RESIZE\n", MEMFD_STR);
828 756
829 fd = mfd_assert_new("kern_memfd_seal_resize", 757 fd = mfd_assert_new("kern_memfd_seal_resize",
830 mfd_def_size, 758 mfd_def_size,
@@ -843,32 +771,6 @@ static void test_seal_resize(void)
843} 771}
844 772
845/* 773/*
846 * hugetlbfs does not support seals. Basic test to dup the memfd created
847 * fd and perform some basic operations on it.
848 */
849static void hugetlbfs_dup(char *b_suffix)
850{
851 int fd, fd2;
852
853 printf("%s HUGETLBFS-DUP %s\n", MEMFD_STR, b_suffix);
854
855 fd = mfd_assert_new("kern_memfd_share_dup",
856 mfd_def_size,
857 MFD_CLOEXEC);
858
859 fd2 = mfd_assert_dup(fd);
860
861 mfd_assert_read(fd);
862 mfd_assert_write(fd);
863
864 mfd_assert_shrink(fd2);
865 mfd_assert_grow(fd2);
866
867 close(fd2);
868 close(fd);
869}
870
871/*
872 * Test sharing via dup() 774 * Test sharing via dup()
873 * Test that seals are shared between dupped FDs and they're all equal. 775 * Test that seals are shared between dupped FDs and they're all equal.
874 */ 776 */
@@ -876,16 +778,7 @@ static void test_share_dup(char *banner, char *b_suffix)
876{ 778{
877 int fd, fd2; 779 int fd, fd2;
878 780
879 /* 781 printf("%s %s %s\n", memfd_str, banner, b_suffix);
880 * hugetlbfs does not contain sealing support. Perform some
881 * basic testing on dup'ed fd instead via hugetlbfs_dup.
882 */
883 if (hugetlbfs_test) {
884 hugetlbfs_dup(b_suffix);
885 return;
886 }
887
888 printf("%s %s %s\n", MEMFD_STR, banner, b_suffix);
889 782
890 fd = mfd_assert_new("kern_memfd_share_dup", 783 fd = mfd_assert_new("kern_memfd_share_dup",
891 mfd_def_size, 784 mfd_def_size,
@@ -927,11 +820,7 @@ static void test_share_mmap(char *banner, char *b_suffix)
927 int fd; 820 int fd;
928 void *p; 821 void *p;
929 822
930 /* hugetlbfs does not contain sealing support */ 823 printf("%s %s %s\n", memfd_str, banner, b_suffix);
931 if (hugetlbfs_test)
932 return;
933
934 printf("%s %s %s\n", MEMFD_STR, banner, b_suffix);
935 824
936 fd = mfd_assert_new("kern_memfd_share_mmap", 825 fd = mfd_assert_new("kern_memfd_share_mmap",
937 mfd_def_size, 826 mfd_def_size,
@@ -956,32 +845,6 @@ static void test_share_mmap(char *banner, char *b_suffix)
956} 845}
957 846
958/* 847/*
959 * Basic test to make sure we can open the hugetlbfs fd via /proc and
960 * perform some simple operations on it.
961 */
962static void hugetlbfs_proc_open(char *b_suffix)
963{
964 int fd, fd2;
965
966 printf("%s HUGETLBFS-PROC-OPEN %s\n", MEMFD_STR, b_suffix);
967
968 fd = mfd_assert_new("kern_memfd_share_open",
969 mfd_def_size,
970 MFD_CLOEXEC);
971
972 fd2 = mfd_assert_open(fd, O_RDWR, 0);
973
974 mfd_assert_read(fd);
975 mfd_assert_write(fd);
976
977 mfd_assert_shrink(fd2);
978 mfd_assert_grow(fd2);
979
980 close(fd2);
981 close(fd);
982}
983
984/*
985 * Test sealing with open(/proc/self/fd/%d) 848 * Test sealing with open(/proc/self/fd/%d)
986 * Via /proc we can get access to a separate file-context for the same memfd. 849 * Via /proc we can get access to a separate file-context for the same memfd.
987 * This is *not* like dup(), but like a real separate open(). Make sure the 850 * This is *not* like dup(), but like a real separate open(). Make sure the
@@ -991,16 +854,7 @@ static void test_share_open(char *banner, char *b_suffix)
991{ 854{
992 int fd, fd2; 855 int fd, fd2;
993 856
994 /* 857 printf("%s %s %s\n", memfd_str, banner, b_suffix);
995 * hugetlbfs does not contain sealing support. So test basic
996 * functionality of using /proc fd via hugetlbfs_proc_open
997 */
998 if (hugetlbfs_test) {
999 hugetlbfs_proc_open(b_suffix);
1000 return;
1001 }
1002
1003 printf("%s %s %s\n", MEMFD_STR, banner, b_suffix);
1004 858
1005 fd = mfd_assert_new("kern_memfd_share_open", 859 fd = mfd_assert_new("kern_memfd_share_open",
1006 mfd_def_size, 860 mfd_def_size,
@@ -1043,11 +897,7 @@ static void test_share_fork(char *banner, char *b_suffix)
1043 int fd; 897 int fd;
1044 pid_t pid; 898 pid_t pid;
1045 899
1046 /* hugetlbfs does not contain sealing support */ 900 printf("%s %s %s\n", memfd_str, banner, b_suffix);
1047 if (hugetlbfs_test)
1048 return;
1049
1050 printf("%s %s %s\n", MEMFD_STR, banner, b_suffix);
1051 901
1052 fd = mfd_assert_new("kern_memfd_share_fork", 902 fd = mfd_assert_new("kern_memfd_share_fork",
1053 mfd_def_size, 903 mfd_def_size,
@@ -1083,7 +933,11 @@ int main(int argc, char **argv)
1083 } 933 }
1084 934
1085 hugetlbfs_test = 1; 935 hugetlbfs_test = 1;
936 memfd_str = MEMFD_HUGE_STR;
1086 mfd_def_size = hpage_size * 2; 937 mfd_def_size = hpage_size * 2;
938 } else {
939 printf("Unknown option: %s\n", argv[1]);
940 abort();
1087 } 941 }
1088 } 942 }
1089 943