From: Jiri Olsa <jolsa@kernel.org>
To: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>
Cc: bpf@vger.kernel.org, linux-mm@kvack.org,
Andrii Nakryiko <andrii@kernel.org>,
Eduard Zingerman <eddyz87@gmail.com>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>,
Lorenzo Stoakes <ljs@kernel.org>,
John Fastabend <john.fastabend@gmail.com>,
Federico Di Pierro <nierro92@gmail.com>
Subject: [PATCH bpf-next 7/8] selftests/bpf: Test bpf_copy_to_user kfunc
Date: Thu, 29 Oct 2026 22:25:50 +0100 [thread overview]
Message-ID: <20261029212551.56964-8-jolsa@kernel.org> (raw)
In-Reply-To: <20261029212551.56964-1-jolsa@kernel.org>
Test that bpf_copy_to_user can fault in a paged-out page and write to a
read-only mapping. Also verify that a partial copy crossing into an unmapped
page returns -EFAULT while leaving the copied byte in place.
Assisted-by: Codex:GPT-6
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
.../selftests/bpf/prog_tests/user_mem.c | 106 ++++++++++++++++++
tools/testing/selftests/bpf/progs/user_mem.c | 17 +++
2 files changed, 123 insertions(+)
diff --git a/tools/testing/selftests/bpf/prog_tests/user_mem.c b/tools/testing/selftests/bpf/prog_tests/user_mem.c
index 7fa827298d24..c87620e39abb 100644
--- a/tools/testing/selftests/bpf/prog_tests/user_mem.c
+++ b/tools/testing/selftests/bpf/prog_tests/user_mem.c
@@ -12,8 +12,11 @@ enum {
USER_MEM_MMAP = 1,
USER_MEM_UNMAP,
USER_MEM_COPY_INSNS,
+ USER_MEM_COPY,
};
+static const char data[] = "fault-enabled BPF write";
+
static noinline void user_mem_trigger(void)
{
asm volatile ("" ::: "memory");
@@ -102,6 +105,18 @@ static bool user_mem_copy_insns(struct user_mem *skel, unsigned long addr)
return ASSERT_OK(skel->bss->ret, "user_write");
}
+static int user_mem_copy(struct user_mem *skel, unsigned long addr,
+ u32 size, u64 flags)
+{
+ skel->bss->user_addr = addr;
+ skel->bss->copy_size = size;
+ skel->bss->copy_flags = flags;
+ skel->bss->action = USER_MEM_COPY;
+ user_mem_trigger();
+
+ return skel->bss->ret;
+}
+
static void test_execute_user_page(struct user_mem *skel)
{
long page_size = sysconf(_SC_PAGESIZE);
@@ -163,6 +178,91 @@ static void test_partial_exec(struct user_mem *skel)
munmap((void *)addr, page_size * 3);
}
+static void test_faultable_write(struct user_mem *skel)
+{
+ long page_size = sysconf(_SC_PAGESIZE);
+ unsigned long addr = ~0UL;
+ unsigned char vec;
+ int i;
+
+ if (!user_mem_mmap(skel, page_size, PROT_READ | PROT_WRITE, &addr))
+ return;
+
+ /* Make sure there is a private resident page to page out. */
+ memset((void *)addr, 0, page_size);
+
+ /* Retry pageout until mincore confirms that the page is no longer resident. */
+ for (i = 0; i < 500; i++) {
+ if (madvise((void *)addr, page_size, MADV_PAGEOUT)) {
+ test__skip();
+ goto out;
+ }
+ if (!ASSERT_OK(mincore((void *)addr, page_size, &vec), "mincore"))
+ goto out;
+ if (!(vec & 1))
+ break;
+ usleep(100);
+ }
+ if (i == 500) {
+ test__skip();
+ goto out;
+ }
+
+ /* ..and do the copy to it. */
+ if (!ASSERT_OK(user_mem_copy(skel, addr, sizeof(data), 0), "user_write_data"))
+ goto out;
+
+ ASSERT_EQ(memcmp((void *)addr, data, sizeof(data)), 0, "written_data");
+
+out:
+ if (addr != ~0UL)
+ munmap((void *)addr, page_size);
+}
+
+static void test_copy_to_readonly_mapping(struct user_mem *skel)
+{
+ long page_size = sysconf(_SC_PAGESIZE);
+ unsigned long addr = ~0UL;
+
+ if (!user_mem_mmap(skel, page_size, PROT_READ, &addr))
+ return;
+
+ /* Assert we can copy to read-only mapping. */
+ if (!ASSERT_OK(user_mem_copy(skel, addr, sizeof(data), 0), "readonly_copy"))
+ goto out;
+
+ ASSERT_EQ(memcmp((void *)addr, data, sizeof(data)), 0, "readonly_copy_data");
+
+out:
+ if (addr != ~0UL)
+ munmap((void *)addr, page_size);
+}
+
+static void test_partial_copy_fault(struct user_mem *skel)
+{
+ long page_size = sysconf(_SC_PAGESIZE);
+ bool second_page_unmapped = false;
+ unsigned long addr = ~0UL;
+
+ if (!user_mem_mmap(skel, page_size * 2, PROT_READ | PROT_WRITE, &addr))
+ return;
+
+ if (!user_mem_munmap(skel, addr + page_size, page_size))
+ goto out;
+
+ second_page_unmapped = true;
+
+ ASSERT_EQ(user_mem_copy(skel, addr + page_size - 1, sizeof(data), 0),
+ -EFAULT, "partial_copy");
+
+ /* The byte before the fault stays written. */
+ ASSERT_EQ(((char *)addr)[page_size - 1], 'f', "partial_copy_data");
+
+out:
+ if (addr != ~0UL)
+ munmap((void *)addr, second_page_unmapped ? page_size : page_size * 2);
+}
+
void test_user_mem(void)
{
struct user_mem *skel;
@@ -181,6 +281,12 @@ void test_user_mem(void)
test_execute_user_page(skel);
if (test__start_subtest("partial_exec"))
test_partial_exec(skel);
+ if (test__start_subtest("faultable_write"))
+ test_faultable_write(skel);
+ if (test__start_subtest("copy_to_readonly_mapping"))
+ test_copy_to_readonly_mapping(skel);
+ if (test__start_subtest("partial_copy_fault"))
+ test_partial_copy_fault(skel);
out:
user_mem__destroy(skel);
diff --git a/tools/testing/selftests/bpf/progs/user_mem.c b/tools/testing/selftests/bpf/progs/user_mem.c
index b477132e3883..6ebfdc54edad 100644
--- a/tools/testing/selftests/bpf/progs/user_mem.c
+++ b/tools/testing/selftests/bpf/progs/user_mem.c
@@ -17,6 +17,7 @@ enum {
USER_MEM_MMAP = 1,
USER_MEM_UNMAP,
USER_MEM_COPY_INSNS,
+ USER_MEM_COPY,
};
int monitored_pid;
@@ -27,6 +28,9 @@ unsigned long user_addr;
unsigned long map_len;
unsigned long map_prot;
+u64 copy_flags;
+u32 copy_size;
+
#if defined(__TARGET_ARCH_x86)
static const unsigned char insns[] = {
0xf3, 0x0f, 0x1e, 0xfa, /* endbr64 */
@@ -43,9 +47,13 @@ static const unsigned char insns[] = {
static const unsigned char insns[] = { 0 };
#endif
+static const char data[] = "fault-enabled BPF write";
+
SEC("uprobe.s//proc/self/exe:user_mem_trigger")
int user_mem_test(struct pt_regs *ctx)
{
+ u32 size;
+
if ((bpf_get_current_pid_tgid() >> 32) != monitored_pid)
return 0;
@@ -59,6 +67,15 @@ int user_mem_test(struct pt_regs *ctx)
case USER_MEM_COPY_INSNS:
ret = bpf_copy_to_user((void *)user_addr, insns, sizeof(insns), 0);
break;
+ case USER_MEM_COPY:
+ /* Bound the kfunc length using a local copy of copy_size. */
+ size = copy_size;
+ if (size > sizeof(data)) {
+ ret = -22; /* -EINVAL */
+ break;
+ }
+ ret = bpf_copy_to_user((void *)user_addr, data, size, copy_flags);
+ break;
}
return 0;
--
2.54.0
next prev parent reply other threads:[~2026-10-29 21:27 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-29 21:25 [PATCH bpf-next 0/8] bpf: Add user memory kfuncs Jiri Olsa
2026-10-29 21:25 ` [PATCH bpf-next 1/8] bpf: Add __srcptr kfunc argument suffix Jiri Olsa
2026-10-29 21:49 ` Andrii Nakryiko
2026-10-29 21:25 ` [PATCH bpf-next 2/8] bpf: Add user memory kfuncs Jiri Olsa
2026-10-29 22:00 ` Andrii Nakryiko
2026-10-29 22:12 ` bot+bpf-ci
2026-10-29 21:25 ` [PATCH bpf-next 3/8] selftests/bpf: Add get_procmap_info helper Jiri Olsa
2026-10-29 21:25 ` [PATCH bpf-next 4/8] selftests/bpf: Test __srcptr kfunc arguments Jiri Olsa
2026-10-29 21:25 ` [PATCH bpf-next 5/8] selftests/bpf: Test user memory mmap kfuncs Jiri Olsa
2026-10-29 21:59 ` bot+bpf-ci
2026-10-29 21:25 ` [PATCH bpf-next 6/8] selftests/bpf: Test user memory page exec Jiri Olsa
2026-10-29 21:25 ` Jiri Olsa [this message]
2026-10-29 21:25 ` [PATCH bpf-next 8/8] selftests/bpf: Test user memory kfunc errors Jiri Olsa
2026-10-29 21:59 ` bot+bpf-ci
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261029212551.56964-8-jolsa@kernel.org \
--to=jolsa@kernel.org \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=john.fastabend@gmail.com \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=memxor@gmail.com \
--cc=nierro92@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox