selftests/bpf: Pass page size from userspace in sockopt_sk
authorYauheni Kaliuta <yauheni.kaliuta@redhat.com>
Thu, 8 Apr 2021 06:13:04 +0000 (09:13 +0300)
committerAndrii Nakryiko <andrii@kernel.org>
Fri, 9 Apr 2021 06:54:47 +0000 (23:54 -0700)
Since there is no convenient way for bpf program to get PAGE_SIZE
from inside of the kernel, pass the value from userspace.

Zero-initialize the variable in bpf prog, otherwise it will cause
problems on some versions of Clang.

Signed-off-by: Yauheni Kaliuta <yauheni.kaliuta@redhat.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20210408061310.95877-3-yauheni.kaliuta@redhat.com
tools/testing/selftests/bpf/prog_tests/sockopt_sk.c
tools/testing/selftests/bpf/progs/sockopt_sk.c

index 7274b12..4b937e5 100644 (file)
@@ -200,6 +200,8 @@ static void run_test(int cgroup_fd)
        if (!ASSERT_OK_PTR(skel, "skel_load"))
                goto cleanup;
 
+       skel->bss->page_size = getpagesize();
+
        skel->links._setsockopt =
                bpf_program__attach_cgroup(skel->progs._setsockopt, cgroup_fd);
        if (!ASSERT_OK_PTR(skel->links._setsockopt, "setsockopt_link"))
index 978a680..8acdb99 100644 (file)
@@ -7,9 +7,7 @@
 
 char _license[] SEC("license") = "GPL";
 
-#ifndef PAGE_SIZE
-#define PAGE_SIZE 4096
-#endif
+int page_size = 0; /* userspace should set it */
 
 #ifndef SOL_TCP
 #define SOL_TCP IPPROTO_TCP
@@ -89,7 +87,7 @@ int _getsockopt(struct bpf_sockopt *ctx)
                 * program can only see the first PAGE_SIZE
                 * bytes of data.
                 */
-               if (optval_end - optval != PAGE_SIZE)
+               if (optval_end - optval != page_size)
                        return 0; /* EPERM, unexpected data size */
 
                return 1;
@@ -160,7 +158,7 @@ int _setsockopt(struct bpf_sockopt *ctx)
 
        if (ctx->level == SOL_IP && ctx->optname == IP_FREEBIND) {
                /* Original optlen is larger than PAGE_SIZE. */
-               if (ctx->optlen != PAGE_SIZE * 2)
+               if (ctx->optlen != page_size * 2)
                        return 0; /* EPERM, unexpected data size */
 
                if (optval + 1 > optval_end)
@@ -174,7 +172,7 @@ int _setsockopt(struct bpf_sockopt *ctx)
                 * program can only see the first PAGE_SIZE
                 * bytes of data.
                 */
-               if (optval_end - optval != PAGE_SIZE)
+               if (optval_end - optval != page_size)
                        return 0; /* EPERM, unexpected data size */
 
                return 1;