Skip to content

Commit ae8a76f

Browse files
committed
Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next
Alexei Starovoitov says: ==================== pull-request: bpf-next 2019-11-02 The following pull-request contains BPF updates for your *net-next* tree. We've added 30 non-merge commits during the last 7 day(s) which contain a total of 41 files changed, 1864 insertions(+), 474 deletions(-). The main changes are: 1) Fix long standing user vs kernel access issue by introducing bpf_probe_read_user() and bpf_probe_read_kernel() helpers, from Daniel. 2) Accelerated xskmap lookup, from Björn and Maciej. 3) Support for automatic map pinning in libbpf, from Toke. 4) Cleanup of BTF-enabled raw tracepoints, from Alexei. 5) Various fixes to libbpf and selftests. ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
2 parents d31e955 + 358fdb4 commit ae8a76f

41 files changed

Lines changed: 1863 additions & 476 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Documentation/bpf/index.rst‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ Program types
4747
prog_flow_dissector
4848

4949

50+
Testing BPF
51+
===========
52+
53+
.. toctree::
54+
:maxdepth: 1
55+
56+
s390
57+
58+
5059
.. Links:
5160
.. _Documentation/networking/filter.txt: ../networking/filter.txt
5261
.. _man-pages: https://www.kernel.org/doc/man-pages/

‎Documentation/bpf/s390.rst‎

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
===================
2+
Testing BPF on s390
3+
===================
4+
5+
1. Introduction
6+
***************
7+
8+
IBM Z are mainframe computers, which are descendants of IBM System/360 from
9+
year 1964. They are supported by the Linux kernel under the name "s390". This
10+
document describes how to test BPF in an s390 QEMU guest.
11+
12+
2. One-time setup
13+
*****************
14+
15+
The following is required to build and run the test suite:
16+
17+
* s390 GCC
18+
* s390 development headers and libraries
19+
* Clang with BPF support
20+
* QEMU with s390 support
21+
* Disk image with s390 rootfs
22+
23+
Debian supports installing compiler and libraries for s390 out of the box.
24+
Users of other distros may use debootstrap in order to set up a Debian chroot::
25+
26+
sudo debootstrap \
27+
--variant=minbase \
28+
--include=sudo \
29+
testing \
30+
./s390-toolchain
31+
sudo mount --rbind /dev ./s390-toolchain/dev
32+
sudo mount --rbind /proc ./s390-toolchain/proc
33+
sudo mount --rbind /sys ./s390-toolchain/sys
34+
sudo chroot ./s390-toolchain
35+
36+
Once on Debian, the build prerequisites can be installed as follows::
37+
38+
sudo dpkg --add-architecture s390x
39+
sudo apt-get update
40+
sudo apt-get install \
41+
bc \
42+
bison \
43+
cmake \
44+
debootstrap \
45+
dwarves \
46+
flex \
47+
g++ \
48+
gcc \
49+
g++-s390x-linux-gnu \
50+
gcc-s390x-linux-gnu \
51+
gdb-multiarch \
52+
git \
53+
make \
54+
python3 \
55+
qemu-system-misc \
56+
qemu-utils \
57+
rsync \
58+
libcap-dev:s390x \
59+
libelf-dev:s390x \
60+
libncurses-dev
61+
62+
Latest Clang targeting BPF can be installed as follows::
63+
64+
git clone https://github.com/llvm/llvm-project.git
65+
ln -s ../../clang llvm-project/llvm/tools/
66+
mkdir llvm-project-build
67+
cd llvm-project-build
68+
cmake \
69+
-DLLVM_TARGETS_TO_BUILD=BPF \
70+
-DCMAKE_BUILD_TYPE=Release \
71+
-DCMAKE_INSTALL_PREFIX=/opt/clang-bpf \
72+
../llvm-project/llvm
73+
make
74+
sudo make install
75+
export PATH=/opt/clang-bpf/bin:$PATH
76+
77+
The disk image can be prepared using a loopback mount and debootstrap::
78+
79+
qemu-img create -f raw ./s390.img 1G
80+
sudo losetup -f ./s390.img
81+
sudo mkfs.ext4 /dev/loopX
82+
mkdir ./s390.rootfs
83+
sudo mount /dev/loopX ./s390.rootfs
84+
sudo debootstrap \
85+
--foreign \
86+
--arch=s390x \
87+
--variant=minbase \
88+
--include=" \
89+
iproute2, \
90+
iputils-ping, \
91+
isc-dhcp-client, \
92+
kmod, \
93+
libcap2, \
94+
libelf1, \
95+
netcat, \
96+
procps" \
97+
testing \
98+
./s390.rootfs
99+
sudo umount ./s390.rootfs
100+
sudo losetup -d /dev/loopX
101+
102+
3. Compilation
103+
**************
104+
105+
In addition to the usual Kconfig options required to run the BPF test suite, it
106+
is also helpful to select::
107+
108+
CONFIG_NET_9P=y
109+
CONFIG_9P_FS=y
110+
CONFIG_NET_9P_VIRTIO=y
111+
CONFIG_VIRTIO_PCI=y
112+
113+
as that would enable a very easy way to share files with the s390 virtual
114+
machine.
115+
116+
Compiling kernel, modules and testsuite, as well as preparing gdb scripts to
117+
simplify debugging, can be done using the following commands::
118+
119+
make ARCH=s390 CROSS_COMPILE=s390x-linux-gnu- menuconfig
120+
make ARCH=s390 CROSS_COMPILE=s390x-linux-gnu- bzImage modules scripts_gdb
121+
make ARCH=s390 CROSS_COMPILE=s390x-linux-gnu- \
122+
-C tools/testing/selftests \
123+
TARGETS=bpf \
124+
INSTALL_PATH=$PWD/tools/testing/selftests/kselftest_install \
125+
install
126+
127+
4. Running the test suite
128+
*************************
129+
130+
The virtual machine can be started as follows::
131+
132+
qemu-system-s390x \
133+
-cpu max,zpci=on \
134+
-smp 2 \
135+
-m 4G \
136+
-kernel linux/arch/s390/boot/compressed/vmlinux \
137+
-drive file=./s390.img,if=virtio,format=raw \
138+
-nographic \
139+
-append 'root=/dev/vda rw console=ttyS1' \
140+
-virtfs local,path=./linux,security_model=none,mount_tag=linux \
141+
-object rng-random,filename=/dev/urandom,id=rng0 \
142+
-device virtio-rng-ccw,rng=rng0 \
143+
-netdev user,id=net0 \
144+
-device virtio-net-ccw,netdev=net0
145+
146+
When using this on a real IBM Z, ``-enable-kvm`` may be added for better
147+
performance. When starting the virtual machine for the first time, disk image
148+
setup must be finalized using the following command::
149+
150+
/debootstrap/debootstrap --second-stage
151+
152+
Directory with the code built on the host as well as ``/proc`` and ``/sys``
153+
need to be mounted as follows::
154+
155+
mkdir -p /linux
156+
mount -t 9p linux /linux
157+
mount -t proc proc /proc
158+
mount -t sysfs sys /sys
159+
160+
After that, the test suite can be run using the following commands::
161+
162+
cd /linux/tools/testing/selftests/kselftest_install
163+
./run_kselftest.sh
164+
165+
As usual, tests can be also run individually::
166+
167+
cd /linux/tools/testing/selftests/bpf
168+
./test_verifier
169+
170+
5. Debugging
171+
************
172+
173+
It is possible to debug the s390 kernel using QEMU GDB stub, which is activated
174+
by passing ``-s`` to QEMU.
175+
176+
It is preferable to turn KASLR off, so that gdb would know where to find the
177+
kernel image in memory, by building the kernel with::
178+
179+
RANDOMIZE_BASE=n
180+
181+
GDB can then be attached using the following command::
182+
183+
gdb-multiarch -ex 'target remote localhost:1234' ./vmlinux
184+
185+
6. Network
186+
**********
187+
188+
In case one needs to use the network in the virtual machine in order to e.g.
189+
install additional packages, it can be configured using::
190+
191+
dhclient eth0
192+
193+
7. Links
194+
********
195+
196+
This document is a compilation of techniques, whose more comprehensive
197+
descriptions can be found by following these links:
198+
199+
- `Debootstrap <https://wiki.debian.org/EmDebian/CrossDebootstrap>`_
200+
- `Multiarch <https://wiki.debian.org/Multiarch/HOWTO>`_
201+
- `Building LLVM <https://llvm.org/docs/CMake.html>`_
202+
- `Cross-compiling the kernel <https://wiki.gentoo.org/wiki/Embedded_Handbook/General/Cross-compiling_the_kernel>`_
203+
- `QEMU s390x Guest Support <https://wiki.qemu.org/Documentation/Platforms/S390X>`_
204+
- `Plan 9 folder sharing over Virtio <https://wiki.qemu.org/Documentation/9psetup>`_
205+
- `Using GDB with QEMU <https://wiki.osdev.org/Kernel_Debugging#Use_GDB_with_QEMU>`_

‎arch/x86/mm/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ CFLAGS_REMOVE_mem_encrypt_identity.o = -pg
1313
endif
1414

1515
obj-y := init.o init_$(BITS).o fault.o ioremap.o extable.o pageattr.o mmap.o \
16-
pat.o pgtable.o physaddr.o setup_nx.o tlb.o cpu_entry_area.o
16+
pat.o pgtable.o physaddr.o setup_nx.o tlb.o cpu_entry_area.o maccess.o
1717

1818
# Make sure __phys_addr has no stackprotector
1919
nostackp := $(call cc-option, -fno-stack-protector)

‎arch/x86/mm/maccess.c‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
3+
#include <linux/uaccess.h>
4+
#include <linux/kernel.h>
5+
6+
#ifdef CONFIG_X86_64
7+
static __always_inline u64 canonical_address(u64 vaddr, u8 vaddr_bits)
8+
{
9+
return ((s64)vaddr << (64 - vaddr_bits)) >> (64 - vaddr_bits);
10+
}
11+
12+
static __always_inline bool invalid_probe_range(u64 vaddr)
13+
{
14+
/*
15+
* Range covering the highest possible canonical userspace address
16+
* as well as non-canonical address range. For the canonical range
17+
* we also need to include the userspace guard page.
18+
*/
19+
return vaddr < TASK_SIZE_MAX + PAGE_SIZE ||
20+
canonical_address(vaddr, boot_cpu_data.x86_virt_bits) != vaddr;
21+
}
22+
#else
23+
static __always_inline bool invalid_probe_range(u64 vaddr)
24+
{
25+
return vaddr < TASK_SIZE_MAX;
26+
}
27+
#endif
28+
29+
long probe_kernel_read_strict(void *dst, const void *src, size_t size)
30+
{
31+
if (unlikely(invalid_probe_range((unsigned long)src)))
32+
return -EFAULT;
33+
34+
return __probe_kernel_read(dst, src, size);
35+
}
36+
37+
long strncpy_from_unsafe_strict(char *dst, const void *unsafe_addr, long count)
38+
{
39+
if (unlikely(invalid_probe_range((unsigned long)unsafe_addr)))
40+
return -EFAULT;
41+
42+
return __strncpy_from_unsafe(dst, unsafe_addr, count);
43+
}

‎include/linux/bpf.h‎

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,11 @@ enum bpf_cgroup_storage_type {
373373

374374
#define MAX_BPF_CGROUP_STORAGE_TYPE __BPF_CGROUP_STORAGE_MAX
375375

376+
/* The longest tracepoint has 12 args.
377+
* See include/trace/bpf_probe.h
378+
*/
379+
#define MAX_BPF_FUNC_ARGS 12
380+
376381
struct bpf_prog_stats {
377382
u64 cnt;
378383
u64 nsecs;
@@ -1004,31 +1009,6 @@ static inline int sock_map_get_from_fd(const union bpf_attr *attr,
10041009
}
10051010
#endif
10061011

1007-
#if defined(CONFIG_XDP_SOCKETS)
1008-
struct xdp_sock;
1009-
struct xdp_sock *__xsk_map_lookup_elem(struct bpf_map *map, u32 key);
1010-
int __xsk_map_redirect(struct bpf_map *map, struct xdp_buff *xdp,
1011-
struct xdp_sock *xs);
1012-
void __xsk_map_flush(struct bpf_map *map);
1013-
#else
1014-
struct xdp_sock;
1015-
static inline struct xdp_sock *__xsk_map_lookup_elem(struct bpf_map *map,
1016-
u32 key)
1017-
{
1018-
return NULL;
1019-
}
1020-
1021-
static inline int __xsk_map_redirect(struct bpf_map *map, struct xdp_buff *xdp,
1022-
struct xdp_sock *xs)
1023-
{
1024-
return -EOPNOTSUPP;
1025-
}
1026-
1027-
static inline void __xsk_map_flush(struct bpf_map *map)
1028-
{
1029-
}
1030-
#endif
1031-
10321012
#if defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL)
10331013
void bpf_sk_reuseport_detach(struct sock *sk);
10341014
int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map, void *key,

‎include/linux/bpf_types.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ BPF_PROG_TYPE(BPF_PROG_TYPE_TRACEPOINT, tracepoint)
2626
BPF_PROG_TYPE(BPF_PROG_TYPE_PERF_EVENT, perf_event)
2727
BPF_PROG_TYPE(BPF_PROG_TYPE_RAW_TRACEPOINT, raw_tracepoint)
2828
BPF_PROG_TYPE(BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE, raw_tracepoint_writable)
29+
BPF_PROG_TYPE(BPF_PROG_TYPE_TRACING, tracing)
2930
#endif
3031
#ifdef CONFIG_CGROUP_BPF
3132
BPF_PROG_TYPE(BPF_PROG_TYPE_CGROUP_DEVICE, cg_dev)

‎include/linux/uaccess.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ copy_struct_from_user(void *dst, size_t ksize, const void __user *src,
311311
* happens, handle that and return -EFAULT.
312312
*/
313313
extern long probe_kernel_read(void *dst, const void *src, size_t size);
314+
extern long probe_kernel_read_strict(void *dst, const void *src, size_t size);
314315
extern long __probe_kernel_read(void *dst, const void *src, size_t size);
315316

316317
/*
@@ -337,7 +338,22 @@ extern long __probe_user_read(void *dst, const void __user *src, size_t size);
337338
extern long notrace probe_kernel_write(void *dst, const void *src, size_t size);
338339
extern long notrace __probe_kernel_write(void *dst, const void *src, size_t size);
339340

341+
/*
342+
* probe_user_write(): safely attempt to write to a location in user space
343+
* @dst: address to write to
344+
* @src: pointer to the data that shall be written
345+
* @size: size of the data chunk
346+
*
347+
* Safely write to address @dst from the buffer at @src. If a kernel fault
348+
* happens, handle that and return -EFAULT.
349+
*/
350+
extern long notrace probe_user_write(void __user *dst, const void *src, size_t size);
351+
extern long notrace __probe_user_write(void __user *dst, const void *src, size_t size);
352+
340353
extern long strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count);
354+
extern long strncpy_from_unsafe_strict(char *dst, const void *unsafe_addr,
355+
long count);
356+
extern long __strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count);
341357
extern long strncpy_from_unsafe_user(char *dst, const void __user *unsafe_addr,
342358
long count);
343359
extern long strnlen_unsafe_user(const void __user *unsafe_addr, long count);

0 commit comments

Comments
 (0)