aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid S. Miller <davem@davemloft.net>2019-07-18 17:04:45 -0400
committerDavid S. Miller <davem@davemloft.net>2019-07-18 17:04:45 -0400
commitbb745231678cd92ab0c4d37343b06e4879072368 (patch)
tree34b96b1bc5310b40779b8848389868e58ce37b5d
parent7369c10f81172b55b284944caa2f51f595bbdb84 (diff)
parent59fd3486c3dd5678bc2fcac75e14466775465c3e (diff)
Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf
Alexei Starovoitov says: ==================== pull-request: bpf 2019-07-18 The following pull-request contains BPF updates for your *net* tree. The main changes are: 1) verifier precision propagation fix, from Andrii. 2) BTF size fix for typedefs, from Andrii. 3) a bunch of big endian fixes, from Ilya. 4) wide load from bpf_sock_addr fixes, from Stanislav. 5) a bunch of misc fixes from a number of developers. ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
-rw-r--r--MAINTAINERS2
-rw-r--r--include/linux/filter.h2
-rw-r--r--include/uapi/linux/bpf.h4
-rw-r--r--kernel/bpf/btf.c19
-rw-r--r--kernel/bpf/verifier.c13
-rw-r--r--net/core/filter.c24
-rw-r--r--net/xdp/xdp_umem.c16
-rw-r--r--net/xdp/xsk.c13
-rw-r--r--samples/bpf/Makefile2
-rw-r--r--tools/bpf/bpftool/main.h1
-rw-r--r--tools/include/uapi/linux/bpf.h4
-rw-r--r--tools/lib/bpf/libbpf.c4
-rw-r--r--tools/lib/bpf/xsk.c3
-rw-r--r--tools/testing/selftests/bpf/Makefile64
-rw-r--r--tools/testing/selftests/bpf/bpf_helpers.h89
-rw-r--r--tools/testing/selftests/bpf/prog_tests/attach_probe.c10
-rw-r--r--tools/testing/selftests/bpf/prog_tests/perf_buffer.c8
-rw-r--r--tools/testing/selftests/bpf/prog_tests/send_signal.c33
-rw-r--r--tools/testing/selftests/bpf/progs/loop1.c2
-rw-r--r--tools/testing/selftests/bpf/progs/loop2.c2
-rw-r--r--tools/testing/selftests/bpf/progs/loop3.c2
-rw-r--r--tools/testing/selftests/bpf/progs/test_get_stack_rawtp.c3
-rw-r--r--tools/testing/selftests/bpf/progs/test_stacktrace_build_id.c3
-rw-r--r--tools/testing/selftests/bpf/progs/test_stacktrace_map.c2
-rw-r--r--tools/testing/selftests/bpf/progs/test_xdp_noinline.c17
-rw-r--r--tools/testing/selftests/bpf/test_btf.c88
-rw-r--r--tools/testing/selftests/bpf/test_progs.h8
-rw-r--r--tools/testing/selftests/bpf/test_verifier.c35
-rw-r--r--tools/testing/selftests/bpf/verifier/array_access.c2
-rw-r--r--tools/testing/selftests/bpf/verifier/value_ptr_arith.c2
-rw-r--r--tools/testing/selftests/bpf/verifier/wide_access.c73
-rw-r--r--tools/testing/selftests/bpf/verifier/wide_store.c36
32 files changed, 391 insertions, 195 deletions
diff --git a/MAINTAINERS b/MAINTAINERS
index 64f659d8346c..2854d42288e9 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -3108,9 +3108,9 @@ S: Maintained
3108F: arch/riscv/net/ 3108F: arch/riscv/net/
3109 3109
3110BPF JIT for S390 3110BPF JIT for S390
3111M: Ilya Leoshkevich <iii@linux.ibm.com>
3111M: Heiko Carstens <heiko.carstens@de.ibm.com> 3112M: Heiko Carstens <heiko.carstens@de.ibm.com>
3112M: Vasily Gorbik <gor@linux.ibm.com> 3113M: Vasily Gorbik <gor@linux.ibm.com>
3113M: Christian Borntraeger <borntraeger@de.ibm.com>
3114L: netdev@vger.kernel.org 3114L: netdev@vger.kernel.org
3115L: bpf@vger.kernel.org 3115L: bpf@vger.kernel.org
3116S: Maintained 3116S: Maintained
diff --git a/include/linux/filter.h b/include/linux/filter.h
index 6d944369ca87..ff65d22cf336 100644
--- a/include/linux/filter.h
+++ b/include/linux/filter.h
@@ -747,7 +747,7 @@ bpf_ctx_narrow_access_ok(u32 off, u32 size, u32 size_default)
747 return size <= size_default && (size & (size - 1)) == 0; 747 return size <= size_default && (size & (size - 1)) == 0;
748} 748}
749 749
750#define bpf_ctx_wide_store_ok(off, size, type, field) \ 750#define bpf_ctx_wide_access_ok(off, size, type, field) \
751 (size == sizeof(__u64) && \ 751 (size == sizeof(__u64) && \
752 off >= offsetof(type, field) && \ 752 off >= offsetof(type, field) && \
753 off + sizeof(__u64) <= offsetofend(type, field) && \ 753 off + sizeof(__u64) <= offsetofend(type, field) && \
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 6f68438aa4ed..81be929b89fc 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -3248,7 +3248,7 @@ struct bpf_sock_addr {
3248 __u32 user_ip4; /* Allows 1,2,4-byte read and 4-byte write. 3248 __u32 user_ip4; /* Allows 1,2,4-byte read and 4-byte write.
3249 * Stored in network byte order. 3249 * Stored in network byte order.
3250 */ 3250 */
3251 __u32 user_ip6[4]; /* Allows 1,2,4-byte read and 4,8-byte write. 3251 __u32 user_ip6[4]; /* Allows 1,2,4,8-byte read and 4,8-byte write.
3252 * Stored in network byte order. 3252 * Stored in network byte order.
3253 */ 3253 */
3254 __u32 user_port; /* Allows 4-byte read and write. 3254 __u32 user_port; /* Allows 4-byte read and write.
@@ -3260,7 +3260,7 @@ struct bpf_sock_addr {
3260 __u32 msg_src_ip4; /* Allows 1,2,4-byte read and 4-byte write. 3260 __u32 msg_src_ip4; /* Allows 1,2,4-byte read and 4-byte write.
3261 * Stored in network byte order. 3261 * Stored in network byte order.
3262 */ 3262 */
3263 __u32 msg_src_ip6[4]; /* Allows 1,2,4-byte read and 4,8-byte write. 3263 __u32 msg_src_ip6[4]; /* Allows 1,2,4,8-byte read and 4,8-byte write.
3264 * Stored in network byte order. 3264 * Stored in network byte order.
3265 */ 3265 */
3266 __bpf_md_ptr(struct bpf_sock *, sk); 3266 __bpf_md_ptr(struct bpf_sock *, sk);
diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 546ebee39e2a..5fcc7a17eb5a 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -1073,11 +1073,18 @@ const struct btf_type *btf_type_id_size(const struct btf *btf,
1073 !btf_type_is_var(size_type))) 1073 !btf_type_is_var(size_type)))
1074 return NULL; 1074 return NULL;
1075 1075
1076 size = btf->resolved_sizes[size_type_id];
1077 size_type_id = btf->resolved_ids[size_type_id]; 1076 size_type_id = btf->resolved_ids[size_type_id];
1078 size_type = btf_type_by_id(btf, size_type_id); 1077 size_type = btf_type_by_id(btf, size_type_id);
1079 if (btf_type_nosize_or_null(size_type)) 1078 if (btf_type_nosize_or_null(size_type))
1080 return NULL; 1079 return NULL;
1080 else if (btf_type_has_size(size_type))
1081 size = size_type->size;
1082 else if (btf_type_is_array(size_type))
1083 size = btf->resolved_sizes[size_type_id];
1084 else if (btf_type_is_ptr(size_type))
1085 size = sizeof(void *);
1086 else
1087 return NULL;
1081 } 1088 }
1082 1089
1083 *type_id = size_type_id; 1090 *type_id = size_type_id;
@@ -1602,7 +1609,6 @@ static int btf_modifier_resolve(struct btf_verifier_env *env,
1602 const struct btf_type *next_type; 1609 const struct btf_type *next_type;
1603 u32 next_type_id = t->type; 1610 u32 next_type_id = t->type;
1604 struct btf *btf = env->btf; 1611 struct btf *btf = env->btf;
1605 u32 next_type_size = 0;
1606 1612
1607 next_type = btf_type_by_id(btf, next_type_id); 1613 next_type = btf_type_by_id(btf, next_type_id);
1608 if (!next_type || btf_type_is_resolve_source_only(next_type)) { 1614 if (!next_type || btf_type_is_resolve_source_only(next_type)) {
@@ -1620,7 +1626,7 @@ static int btf_modifier_resolve(struct btf_verifier_env *env,
1620 * save us a few type-following when we use it later (e.g. in 1626 * save us a few type-following when we use it later (e.g. in
1621 * pretty print). 1627 * pretty print).
1622 */ 1628 */
1623 if (!btf_type_id_size(btf, &next_type_id, &next_type_size)) { 1629 if (!btf_type_id_size(btf, &next_type_id, NULL)) {
1624 if (env_type_is_resolved(env, next_type_id)) 1630 if (env_type_is_resolved(env, next_type_id))
1625 next_type = btf_type_id_resolve(btf, &next_type_id); 1631 next_type = btf_type_id_resolve(btf, &next_type_id);
1626 1632
@@ -1633,7 +1639,7 @@ static int btf_modifier_resolve(struct btf_verifier_env *env,
1633 } 1639 }
1634 } 1640 }
1635 1641
1636 env_stack_pop_resolved(env, next_type_id, next_type_size); 1642 env_stack_pop_resolved(env, next_type_id, 0);
1637 1643
1638 return 0; 1644 return 0;
1639} 1645}
@@ -1645,7 +1651,6 @@ static int btf_var_resolve(struct btf_verifier_env *env,
1645 const struct btf_type *t = v->t; 1651 const struct btf_type *t = v->t;
1646 u32 next_type_id = t->type; 1652 u32 next_type_id = t->type;
1647 struct btf *btf = env->btf; 1653 struct btf *btf = env->btf;
1648 u32 next_type_size;
1649 1654
1650 next_type = btf_type_by_id(btf, next_type_id); 1655 next_type = btf_type_by_id(btf, next_type_id);
1651 if (!next_type || btf_type_is_resolve_source_only(next_type)) { 1656 if (!next_type || btf_type_is_resolve_source_only(next_type)) {
@@ -1675,12 +1680,12 @@ static int btf_var_resolve(struct btf_verifier_env *env,
1675 * forward types or similar that would resolve to size of 1680 * forward types or similar that would resolve to size of
1676 * zero is allowed. 1681 * zero is allowed.
1677 */ 1682 */
1678 if (!btf_type_id_size(btf, &next_type_id, &next_type_size)) { 1683 if (!btf_type_id_size(btf, &next_type_id, NULL)) {
1679 btf_verifier_log_type(env, v->t, "Invalid type_id"); 1684 btf_verifier_log_type(env, v->t, "Invalid type_id");
1680 return -EINVAL; 1685 return -EINVAL;
1681 } 1686 }
1682 1687
1683 env_stack_pop_resolved(env, next_type_id, next_type_size); 1688 env_stack_pop_resolved(env, next_type_id, 0);
1684 1689
1685 return 0; 1690 return 0;
1686} 1691}
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index a2e763703c30..5900cbb966b1 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -1519,9 +1519,9 @@ static int backtrack_insn(struct bpf_verifier_env *env, int idx,
1519 return -EFAULT; 1519 return -EFAULT;
1520 } 1520 }
1521 *stack_mask |= 1ull << spi; 1521 *stack_mask |= 1ull << spi;
1522 } else if (class == BPF_STX) { 1522 } else if (class == BPF_STX || class == BPF_ST) {
1523 if (*reg_mask & dreg) 1523 if (*reg_mask & dreg)
1524 /* stx shouldn't be using _scalar_ dst_reg 1524 /* stx & st shouldn't be using _scalar_ dst_reg
1525 * to access memory. It means backtracking 1525 * to access memory. It means backtracking
1526 * encountered a case of pointer subtraction. 1526 * encountered a case of pointer subtraction.
1527 */ 1527 */
@@ -1540,7 +1540,8 @@ static int backtrack_insn(struct bpf_verifier_env *env, int idx,
1540 if (!(*stack_mask & (1ull << spi))) 1540 if (!(*stack_mask & (1ull << spi)))
1541 return 0; 1541 return 0;
1542 *stack_mask &= ~(1ull << spi); 1542 *stack_mask &= ~(1ull << spi);
1543 *reg_mask |= sreg; 1543 if (class == BPF_STX)
1544 *reg_mask |= sreg;
1544 } else if (class == BPF_JMP || class == BPF_JMP32) { 1545 } else if (class == BPF_JMP || class == BPF_JMP32) {
1545 if (opcode == BPF_CALL) { 1546 if (opcode == BPF_CALL) {
1546 if (insn->src_reg == BPF_PSEUDO_CALL) 1547 if (insn->src_reg == BPF_PSEUDO_CALL)
@@ -1569,10 +1570,6 @@ static int backtrack_insn(struct bpf_verifier_env *env, int idx,
1569 if (mode == BPF_IND || mode == BPF_ABS) 1570 if (mode == BPF_IND || mode == BPF_ABS)
1570 /* to be analyzed */ 1571 /* to be analyzed */
1571 return -ENOTSUPP; 1572 return -ENOTSUPP;
1572 } else if (class == BPF_ST) {
1573 if (*reg_mask & dreg)
1574 /* likely pointer subtraction */
1575 return -ENOTSUPP;
1576 } 1573 }
1577 return 0; 1574 return 0;
1578} 1575}
@@ -6106,11 +6103,13 @@ static int check_return_code(struct bpf_verifier_env *env)
6106 if (env->prog->expected_attach_type == BPF_CGROUP_UDP4_RECVMSG || 6103 if (env->prog->expected_attach_type == BPF_CGROUP_UDP4_RECVMSG ||
6107 env->prog->expected_attach_type == BPF_CGROUP_UDP6_RECVMSG) 6104 env->prog->expected_attach_type == BPF_CGROUP_UDP6_RECVMSG)
6108 range = tnum_range(1, 1); 6105 range = tnum_range(1, 1);
6106 break;
6109 case BPF_PROG_TYPE_CGROUP_SKB: 6107 case BPF_PROG_TYPE_CGROUP_SKB:
6110 if (env->prog->expected_attach_type == BPF_CGROUP_INET_EGRESS) { 6108 if (env->prog->expected_attach_type == BPF_CGROUP_INET_EGRESS) {
6111 range = tnum_range(0, 3); 6109 range = tnum_range(0, 3);
6112 enforce_attach_type_range = tnum_range(2, 3); 6110 enforce_attach_type_range = tnum_range(2, 3);
6113 } 6111 }
6112 break;
6114 case BPF_PROG_TYPE_CGROUP_SOCK: 6113 case BPF_PROG_TYPE_CGROUP_SOCK:
6115 case BPF_PROG_TYPE_SOCK_OPS: 6114 case BPF_PROG_TYPE_SOCK_OPS:
6116 case BPF_PROG_TYPE_CGROUP_DEVICE: 6115 case BPF_PROG_TYPE_CGROUP_DEVICE:
diff --git a/net/core/filter.c b/net/core/filter.c
index 47f6386fb17a..0f6854ccf894 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -6884,20 +6884,30 @@ static bool sock_addr_is_valid_access(int off, int size,
6884 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4): 6884 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
6885 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0], 6885 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
6886 msg_src_ip6[3]): 6886 msg_src_ip6[3]):
6887 /* Only narrow read access allowed for now. */
6888 if (type == BPF_READ) { 6887 if (type == BPF_READ) {
6889 bpf_ctx_record_field_size(info, size_default); 6888 bpf_ctx_record_field_size(info, size_default);
6889
6890 if (bpf_ctx_wide_access_ok(off, size,
6891 struct bpf_sock_addr,
6892 user_ip6))
6893 return true;
6894
6895 if (bpf_ctx_wide_access_ok(off, size,
6896 struct bpf_sock_addr,
6897 msg_src_ip6))
6898 return true;
6899
6890 if (!bpf_ctx_narrow_access_ok(off, size, size_default)) 6900 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
6891 return false; 6901 return false;
6892 } else { 6902 } else {
6893 if (bpf_ctx_wide_store_ok(off, size, 6903 if (bpf_ctx_wide_access_ok(off, size,
6894 struct bpf_sock_addr, 6904 struct bpf_sock_addr,
6895 user_ip6)) 6905 user_ip6))
6896 return true; 6906 return true;
6897 6907
6898 if (bpf_ctx_wide_store_ok(off, size, 6908 if (bpf_ctx_wide_access_ok(off, size,
6899 struct bpf_sock_addr, 6909 struct bpf_sock_addr,
6900 msg_src_ip6)) 6910 msg_src_ip6))
6901 return true; 6911 return true;
6902 6912
6903 if (size != size_default) 6913 if (size != size_default)
diff --git a/net/xdp/xdp_umem.c b/net/xdp/xdp_umem.c
index 20c91f02d3d8..83de74ca729a 100644
--- a/net/xdp/xdp_umem.c
+++ b/net/xdp/xdp_umem.c
@@ -87,21 +87,20 @@ int xdp_umem_assign_dev(struct xdp_umem *umem, struct net_device *dev,
87 struct netdev_bpf bpf; 87 struct netdev_bpf bpf;
88 int err = 0; 88 int err = 0;
89 89
90 ASSERT_RTNL();
91
90 force_zc = flags & XDP_ZEROCOPY; 92 force_zc = flags & XDP_ZEROCOPY;
91 force_copy = flags & XDP_COPY; 93 force_copy = flags & XDP_COPY;
92 94
93 if (force_zc && force_copy) 95 if (force_zc && force_copy)
94 return -EINVAL; 96 return -EINVAL;
95 97
96 rtnl_lock(); 98 if (xdp_get_umem_from_qid(dev, queue_id))
97 if (xdp_get_umem_from_qid(dev, queue_id)) { 99 return -EBUSY;
98 err = -EBUSY;
99 goto out_rtnl_unlock;
100 }
101 100
102 err = xdp_reg_umem_at_qid(dev, umem, queue_id); 101 err = xdp_reg_umem_at_qid(dev, umem, queue_id);
103 if (err) 102 if (err)
104 goto out_rtnl_unlock; 103 return err;
105 104
106 umem->dev = dev; 105 umem->dev = dev;
107 umem->queue_id = queue_id; 106 umem->queue_id = queue_id;
@@ -110,7 +109,7 @@ int xdp_umem_assign_dev(struct xdp_umem *umem, struct net_device *dev,
110 109
111 if (force_copy) 110 if (force_copy)
112 /* For copy-mode, we are done. */ 111 /* For copy-mode, we are done. */
113 goto out_rtnl_unlock; 112 return 0;
114 113
115 if (!dev->netdev_ops->ndo_bpf || 114 if (!dev->netdev_ops->ndo_bpf ||
116 !dev->netdev_ops->ndo_xsk_async_xmit) { 115 !dev->netdev_ops->ndo_xsk_async_xmit) {
@@ -125,7 +124,6 @@ int xdp_umem_assign_dev(struct xdp_umem *umem, struct net_device *dev,
125 err = dev->netdev_ops->ndo_bpf(dev, &bpf); 124 err = dev->netdev_ops->ndo_bpf(dev, &bpf);
126 if (err) 125 if (err)
127 goto err_unreg_umem; 126 goto err_unreg_umem;
128 rtnl_unlock();
129 127
130 umem->zc = true; 128 umem->zc = true;
131 return 0; 129 return 0;
@@ -135,8 +133,6 @@ err_unreg_umem:
135 err = 0; /* fallback to copy mode */ 133 err = 0; /* fallback to copy mode */
136 if (err) 134 if (err)
137 xdp_clear_umem_at_qid(dev, queue_id); 135 xdp_clear_umem_at_qid(dev, queue_id);
138out_rtnl_unlock:
139 rtnl_unlock();
140 return err; 136 return err;
141} 137}
142 138
diff --git a/net/xdp/xsk.c b/net/xdp/xsk.c
index d4d6f10aa936..59b57d708697 100644
--- a/net/xdp/xsk.c
+++ b/net/xdp/xsk.c
@@ -240,6 +240,9 @@ static int xsk_generic_xmit(struct sock *sk, struct msghdr *m,
240 240
241 mutex_lock(&xs->mutex); 241 mutex_lock(&xs->mutex);
242 242
243 if (xs->queue_id >= xs->dev->real_num_tx_queues)
244 goto out;
245
243 while (xskq_peek_desc(xs->tx, &desc)) { 246 while (xskq_peek_desc(xs->tx, &desc)) {
244 char *buffer; 247 char *buffer;
245 u64 addr; 248 u64 addr;
@@ -250,12 +253,6 @@ static int xsk_generic_xmit(struct sock *sk, struct msghdr *m,
250 goto out; 253 goto out;
251 } 254 }
252 255
253 if (xskq_reserve_addr(xs->umem->cq))
254 goto out;
255
256 if (xs->queue_id >= xs->dev->real_num_tx_queues)
257 goto out;
258
259 len = desc.len; 256 len = desc.len;
260 skb = sock_alloc_send_skb(sk, len, 1, &err); 257 skb = sock_alloc_send_skb(sk, len, 1, &err);
261 if (unlikely(!skb)) { 258 if (unlikely(!skb)) {
@@ -267,7 +264,7 @@ static int xsk_generic_xmit(struct sock *sk, struct msghdr *m,
267 addr = desc.addr; 264 addr = desc.addr;
268 buffer = xdp_umem_get_data(xs->umem, addr); 265 buffer = xdp_umem_get_data(xs->umem, addr);
269 err = skb_store_bits(skb, 0, buffer, len); 266 err = skb_store_bits(skb, 0, buffer, len);
270 if (unlikely(err)) { 267 if (unlikely(err) || xskq_reserve_addr(xs->umem->cq)) {
271 kfree_skb(skb); 268 kfree_skb(skb);
272 goto out; 269 goto out;
273 } 270 }
@@ -433,6 +430,7 @@ static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
433 if (flags & ~(XDP_SHARED_UMEM | XDP_COPY | XDP_ZEROCOPY)) 430 if (flags & ~(XDP_SHARED_UMEM | XDP_COPY | XDP_ZEROCOPY))
434 return -EINVAL; 431 return -EINVAL;
435 432
433 rtnl_lock();
436 mutex_lock(&xs->mutex); 434 mutex_lock(&xs->mutex);
437 if (xs->state != XSK_READY) { 435 if (xs->state != XSK_READY) {
438 err = -EBUSY; 436 err = -EBUSY;
@@ -518,6 +516,7 @@ out_unlock:
518 xs->state = XSK_BOUND; 516 xs->state = XSK_BOUND;
519out_release: 517out_release:
520 mutex_unlock(&xs->mutex); 518 mutex_unlock(&xs->mutex);
519 rtnl_unlock();
521 return err; 520 return err;
522} 521}
523 522
diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile
index f90daadfbc89..1d9be26b4edd 100644
--- a/samples/bpf/Makefile
+++ b/samples/bpf/Makefile
@@ -284,7 +284,7 @@ $(obj)/%.o: $(src)/%.c
284 $(Q)$(CLANG) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) $(EXTRA_CFLAGS) -I$(obj) \ 284 $(Q)$(CLANG) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) $(EXTRA_CFLAGS) -I$(obj) \
285 -I$(srctree)/tools/testing/selftests/bpf/ \ 285 -I$(srctree)/tools/testing/selftests/bpf/ \
286 -D__KERNEL__ -D__BPF_TRACING__ -Wno-unused-value -Wno-pointer-sign \ 286 -D__KERNEL__ -D__BPF_TRACING__ -Wno-unused-value -Wno-pointer-sign \
287 -D__TARGET_ARCH_$(ARCH) -Wno-compare-distinct-pointer-types \ 287 -D__TARGET_ARCH_$(SRCARCH) -Wno-compare-distinct-pointer-types \
288 -Wno-gnu-variable-sized-type-not-at-end \ 288 -Wno-gnu-variable-sized-type-not-at-end \
289 -Wno-address-of-packed-member -Wno-tautological-compare \ 289 -Wno-address-of-packed-member -Wno-tautological-compare \
290 -Wno-unknown-warning-option $(CLANG_ARCH_ARGS) \ 290 -Wno-unknown-warning-option $(CLANG_ARCH_ARGS) \
diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h
index 3ef0d9051e10..7031a4bf87a0 100644
--- a/tools/bpf/bpftool/main.h
+++ b/tools/bpf/bpftool/main.h
@@ -74,6 +74,7 @@ static const char * const prog_type_name[] = {
74 [BPF_PROG_TYPE_SK_REUSEPORT] = "sk_reuseport", 74 [BPF_PROG_TYPE_SK_REUSEPORT] = "sk_reuseport",
75 [BPF_PROG_TYPE_FLOW_DISSECTOR] = "flow_dissector", 75 [BPF_PROG_TYPE_FLOW_DISSECTOR] = "flow_dissector",
76 [BPF_PROG_TYPE_CGROUP_SYSCTL] = "cgroup_sysctl", 76 [BPF_PROG_TYPE_CGROUP_SYSCTL] = "cgroup_sysctl",
77 [BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE] = "raw_tracepoint_writable",
77 [BPF_PROG_TYPE_CGROUP_SOCKOPT] = "cgroup_sockopt", 78 [BPF_PROG_TYPE_CGROUP_SOCKOPT] = "cgroup_sockopt",
78}; 79};
79 80
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index f506c68b2612..1f61374fcf81 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -3245,7 +3245,7 @@ struct bpf_sock_addr {
3245 __u32 user_ip4; /* Allows 1,2,4-byte read and 4-byte write. 3245 __u32 user_ip4; /* Allows 1,2,4-byte read and 4-byte write.
3246 * Stored in network byte order. 3246 * Stored in network byte order.
3247 */ 3247 */
3248 __u32 user_ip6[4]; /* Allows 1,2,4-byte read and 4,8-byte write. 3248 __u32 user_ip6[4]; /* Allows 1,2,4,8-byte read and 4,8-byte write.
3249 * Stored in network byte order. 3249 * Stored in network byte order.
3250 */ 3250 */
3251 __u32 user_port; /* Allows 4-byte read and write. 3251 __u32 user_port; /* Allows 4-byte read and write.
@@ -3257,7 +3257,7 @@ struct bpf_sock_addr {
3257 __u32 msg_src_ip4; /* Allows 1,2,4-byte read and 4-byte write. 3257 __u32 msg_src_ip4; /* Allows 1,2,4-byte read and 4-byte write.
3258 * Stored in network byte order. 3258 * Stored in network byte order.
3259 */ 3259 */
3260 __u32 msg_src_ip6[4]; /* Allows 1,2,4-byte read and 4,8-byte write. 3260 __u32 msg_src_ip6[4]; /* Allows 1,2,4,8-byte read and 4,8-byte write.
3261 * Stored in network byte order. 3261 * Stored in network byte order.
3262 */ 3262 */
3263 __bpf_md_ptr(struct bpf_sock *, sk); 3263 __bpf_md_ptr(struct bpf_sock *, sk);
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index ed07789b3e62..794dd5064ae8 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -4126,8 +4126,8 @@ static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,
4126 } 4126 }
4127 attr.size = sizeof(attr); 4127 attr.size = sizeof(attr);
4128 attr.type = type; 4128 attr.type = type;
4129 attr.config1 = (uint64_t)(void *)name; /* kprobe_func or uprobe_path */ 4129 attr.config1 = ptr_to_u64(name); /* kprobe_func or uprobe_path */
4130 attr.config2 = offset; /* kprobe_addr or probe_offset */ 4130 attr.config2 = offset; /* kprobe_addr or probe_offset */
4131 4131
4132 /* pid filter is meaningful only for uprobes */ 4132 /* pid filter is meaningful only for uprobes */
4133 pfd = syscall(__NR_perf_event_open, &attr, 4133 pfd = syscall(__NR_perf_event_open, &attr,
diff --git a/tools/lib/bpf/xsk.c b/tools/lib/bpf/xsk.c
index b33740221b7e..5007b5d4fd2c 100644
--- a/tools/lib/bpf/xsk.c
+++ b/tools/lib/bpf/xsk.c
@@ -517,7 +517,8 @@ int xsk_socket__create(struct xsk_socket **xsk_ptr, const char *ifname,
517 err = -errno; 517 err = -errno;
518 goto out_socket; 518 goto out_socket;
519 } 519 }
520 strncpy(xsk->ifname, ifname, IFNAMSIZ); 520 strncpy(xsk->ifname, ifname, IFNAMSIZ - 1);
521 xsk->ifname[IFNAMSIZ - 1] = '\0';
521 522
522 err = xsk_set_xdp_socket_config(&xsk->config, usr_config); 523 err = xsk_set_xdp_socket_config(&xsk->config, usr_config);
523 if (err) 524 if (err)
diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile
index 2620406a53ec..11c9c62c3362 100644
--- a/tools/testing/selftests/bpf/Makefile
+++ b/tools/testing/selftests/bpf/Makefile
@@ -1,4 +1,6 @@
1# SPDX-License-Identifier: GPL-2.0 1# SPDX-License-Identifier: GPL-2.0
2include ../../../../scripts/Kbuild.include
3include ../../../scripts/Makefile.arch
2 4
3LIBDIR := ../../../lib 5LIBDIR := ../../../lib
4BPFDIR := $(LIBDIR)/bpf 6BPFDIR := $(LIBDIR)/bpf
@@ -81,13 +83,14 @@ all: $(TEST_CUSTOM_PROGS)
81$(OUTPUT)/urandom_read: $(OUTPUT)/%: %.c 83$(OUTPUT)/urandom_read: $(OUTPUT)/%: %.c
82 $(CC) -o $@ $< -Wl,--build-id 84 $(CC) -o $@ $< -Wl,--build-id
83 85
84$(OUTPUT)/test_maps: map_tests/*.c 86$(OUTPUT)/test_stub.o: test_stub.c
87 $(CC) $(TEST_PROGS_CFLAGS) $(CFLAGS) -c -o $@ $<
85 88
86BPFOBJ := $(OUTPUT)/libbpf.a 89BPFOBJ := $(OUTPUT)/libbpf.a
87 90
88$(TEST_GEN_PROGS): test_stub.o $(BPFOBJ) 91$(TEST_GEN_PROGS): $(OUTPUT)/test_stub.o $(BPFOBJ)
89 92
90$(TEST_GEN_PROGS_EXTENDED): test_stub.o $(OUTPUT)/libbpf.a 93$(TEST_GEN_PROGS_EXTENDED): $(OUTPUT)/test_stub.o $(OUTPUT)/libbpf.a
91 94
92$(OUTPUT)/test_dev_cgroup: cgroup_helpers.c 95$(OUTPUT)/test_dev_cgroup: cgroup_helpers.c
93$(OUTPUT)/test_skb_cgroup_id_user: cgroup_helpers.c 96$(OUTPUT)/test_skb_cgroup_id_user: cgroup_helpers.c
@@ -138,7 +141,8 @@ CLANG_SYS_INCLUDES := $(shell $(CLANG) -v -E - </dev/null 2>&1 \
138 141
139CLANG_FLAGS = -I. -I./include/uapi -I../../../include/uapi \ 142CLANG_FLAGS = -I. -I./include/uapi -I../../../include/uapi \
140 $(CLANG_SYS_INCLUDES) \ 143 $(CLANG_SYS_INCLUDES) \
141 -Wno-compare-distinct-pointer-types 144 -Wno-compare-distinct-pointer-types \
145 -D__TARGET_ARCH_$(SRCARCH)
142 146
143$(OUTPUT)/test_l4lb_noinline.o: CLANG_FLAGS += -fno-inline 147$(OUTPUT)/test_l4lb_noinline.o: CLANG_FLAGS += -fno-inline
144$(OUTPUT)/test_xdp_noinline.o: CLANG_FLAGS += -fno-inline 148$(OUTPUT)/test_xdp_noinline.o: CLANG_FLAGS += -fno-inline
@@ -172,6 +176,7 @@ endif
172endif 176endif
173 177
174TEST_PROGS_CFLAGS := -I. -I$(OUTPUT) 178TEST_PROGS_CFLAGS := -I. -I$(OUTPUT)
179TEST_MAPS_CFLAGS := -I. -I$(OUTPUT)
175TEST_VERIFIER_CFLAGS := -I. -I$(OUTPUT) -Iverifier 180TEST_VERIFIER_CFLAGS := -I. -I$(OUTPUT) -Iverifier
176 181
177ifneq ($(SUBREG_CODEGEN),) 182ifneq ($(SUBREG_CODEGEN),)
@@ -180,12 +185,12 @@ TEST_CUSTOM_PROGS += $(ALU32_BUILD_DIR)/test_progs_32
180$(ALU32_BUILD_DIR): 185$(ALU32_BUILD_DIR):
181 mkdir -p $@ 186 mkdir -p $@
182 187
183$(ALU32_BUILD_DIR)/urandom_read: $(OUTPUT)/urandom_read 188$(ALU32_BUILD_DIR)/urandom_read: $(OUTPUT)/urandom_read | $(ALU32_BUILD_DIR)
184 cp $< $@ 189 cp $< $@
185 190
186$(ALU32_BUILD_DIR)/test_progs_32: test_progs.c $(OUTPUT)/libbpf.a\ 191$(ALU32_BUILD_DIR)/test_progs_32: test_progs.c $(OUTPUT)/libbpf.a\
187 $(ALU32_BUILD_DIR) \ 192 $(ALU32_BUILD_DIR)/urandom_read \
188 $(ALU32_BUILD_DIR)/urandom_read 193 | $(ALU32_BUILD_DIR)
189 $(CC) $(TEST_PROGS_CFLAGS) $(CFLAGS) \ 194 $(CC) $(TEST_PROGS_CFLAGS) $(CFLAGS) \
190 -o $(ALU32_BUILD_DIR)/test_progs_32 \ 195 -o $(ALU32_BUILD_DIR)/test_progs_32 \
191 test_progs.c test_stub.c trace_helpers.c prog_tests/*.c \ 196 test_progs.c test_stub.c trace_helpers.c prog_tests/*.c \
@@ -194,10 +199,10 @@ $(ALU32_BUILD_DIR)/test_progs_32: test_progs.c $(OUTPUT)/libbpf.a\
194$(ALU32_BUILD_DIR)/test_progs_32: $(PROG_TESTS_H) 199$(ALU32_BUILD_DIR)/test_progs_32: $(PROG_TESTS_H)
195$(ALU32_BUILD_DIR)/test_progs_32: prog_tests/*.c 200$(ALU32_BUILD_DIR)/test_progs_32: prog_tests/*.c
196 201
197$(ALU32_BUILD_DIR)/%.o: progs/%.c $(ALU32_BUILD_DIR) \ 202$(ALU32_BUILD_DIR)/%.o: progs/%.c $(ALU32_BUILD_DIR)/test_progs_32 \
198 $(ALU32_BUILD_DIR)/test_progs_32 203 | $(ALU32_BUILD_DIR)
199 $(CLANG) $(CLANG_FLAGS) \ 204 ($(CLANG) $(CLANG_FLAGS) -O2 -target bpf -emit-llvm -c $< -o - || \
200 -O2 -target bpf -emit-llvm -c $< -o - | \ 205 echo "clang failed") | \
201 $(LLC) -march=bpf -mattr=+alu32 -mcpu=$(CPU) $(LLC_FLAGS) \ 206 $(LLC) -march=bpf -mattr=+alu32 -mcpu=$(CPU) $(LLC_FLAGS) \
202 -filetype=obj -o $@ 207 -filetype=obj -o $@
203ifeq ($(DWARF2BTF),y) 208ifeq ($(DWARF2BTF),y)
@@ -208,32 +213,30 @@ endif
208# Have one program compiled without "-target bpf" to test whether libbpf loads 213# Have one program compiled without "-target bpf" to test whether libbpf loads
209# it successfully 214# it successfully
210$(OUTPUT)/test_xdp.o: progs/test_xdp.c 215$(OUTPUT)/test_xdp.o: progs/test_xdp.c
211 $(CLANG) $(CLANG_FLAGS) \ 216 ($(CLANG) $(CLANG_FLAGS) -O2 -emit-llvm -c $< -o - || \
212 -O2 -emit-llvm -c $< -o - | \ 217 echo "clang failed") | \
213 $(LLC) -march=bpf -mcpu=$(CPU) $(LLC_FLAGS) -filetype=obj -o $@ 218 $(LLC) -march=bpf -mcpu=$(CPU) $(LLC_FLAGS) -filetype=obj -o $@
214ifeq ($(DWARF2BTF),y) 219ifeq ($(DWARF2BTF),y)
215 $(BTF_PAHOLE) -J $@ 220 $(BTF_PAHOLE) -J $@
216endif 221endif
217 222
218$(OUTPUT)/%.o: progs/%.c 223$(OUTPUT)/%.o: progs/%.c
219 $(CLANG) $(CLANG_FLAGS) \ 224 ($(CLANG) $(CLANG_FLAGS) -O2 -target bpf -emit-llvm -c $< -o - || \
220 -O2 -target bpf -emit-llvm -c $< -o - | \ 225 echo "clang failed") | \
221 $(LLC) -march=bpf -mcpu=$(CPU) $(LLC_FLAGS) -filetype=obj -o $@ 226 $(LLC) -march=bpf -mcpu=$(CPU) $(LLC_FLAGS) -filetype=obj -o $@
222ifeq ($(DWARF2BTF),y) 227ifeq ($(DWARF2BTF),y)
223 $(BTF_PAHOLE) -J $@ 228 $(BTF_PAHOLE) -J $@
224endif 229endif
225 230
226PROG_TESTS_H := $(OUTPUT)/prog_tests/tests.h
227test_progs.c: $(PROG_TESTS_H)
228$(OUTPUT)/test_progs: CFLAGS += $(TEST_PROGS_CFLAGS)
229$(OUTPUT)/test_progs: prog_tests/*.c
230
231PROG_TESTS_DIR = $(OUTPUT)/prog_tests 231PROG_TESTS_DIR = $(OUTPUT)/prog_tests
232$(PROG_TESTS_DIR): 232$(PROG_TESTS_DIR):
233 mkdir -p $@ 233 mkdir -p $@
234 234PROG_TESTS_H := $(PROG_TESTS_DIR)/tests.h
235PROG_TESTS_FILES := $(wildcard prog_tests/*.c) 235PROG_TESTS_FILES := $(wildcard prog_tests/*.c)
236$(PROG_TESTS_H): $(PROG_TESTS_DIR) $(PROG_TESTS_FILES) 236test_progs.c: $(PROG_TESTS_H)
237$(OUTPUT)/test_progs: CFLAGS += $(TEST_PROGS_CFLAGS)
238$(OUTPUT)/test_progs: test_progs.c $(PROG_TESTS_H) $(PROG_TESTS_FILES)
239$(PROG_TESTS_H): $(PROG_TESTS_FILES) | $(PROG_TESTS_DIR)
237 $(shell ( cd prog_tests/; \ 240 $(shell ( cd prog_tests/; \
238 echo '/* Generated header, do not edit */'; \ 241 echo '/* Generated header, do not edit */'; \
239 echo '#ifdef DECLARE'; \ 242 echo '#ifdef DECLARE'; \
@@ -246,15 +249,15 @@ $(PROG_TESTS_H): $(PROG_TESTS_DIR) $(PROG_TESTS_FILES)
246 echo '#endif' \ 249 echo '#endif' \
247 ) > $(PROG_TESTS_H)) 250 ) > $(PROG_TESTS_H))
248 251
249TEST_MAPS_CFLAGS := -I. -I$(OUTPUT)
250MAP_TESTS_DIR = $(OUTPUT)/map_tests 252MAP_TESTS_DIR = $(OUTPUT)/map_tests
251$(MAP_TESTS_DIR): 253$(MAP_TESTS_DIR):
252 mkdir -p $@ 254 mkdir -p $@
253MAP_TESTS_H := $(MAP_TESTS_DIR)/tests.h 255MAP_TESTS_H := $(MAP_TESTS_DIR)/tests.h
256MAP_TESTS_FILES := $(wildcard map_tests/*.c)
254test_maps.c: $(MAP_TESTS_H) 257test_maps.c: $(MAP_TESTS_H)
255$(OUTPUT)/test_maps: CFLAGS += $(TEST_MAPS_CFLAGS) 258$(OUTPUT)/test_maps: CFLAGS += $(TEST_MAPS_CFLAGS)
256MAP_TESTS_FILES := $(wildcard map_tests/*.c) 259$(OUTPUT)/test_maps: test_maps.c $(MAP_TESTS_H) $(MAP_TESTS_FILES)
257$(MAP_TESTS_H): $(MAP_TESTS_DIR) $(MAP_TESTS_FILES) 260$(MAP_TESTS_H): $(MAP_TESTS_FILES) | $(MAP_TESTS_DIR)
258 $(shell ( cd map_tests/; \ 261 $(shell ( cd map_tests/; \
259 echo '/* Generated header, do not edit */'; \ 262 echo '/* Generated header, do not edit */'; \
260 echo '#ifdef DECLARE'; \ 263 echo '#ifdef DECLARE'; \
@@ -267,16 +270,15 @@ $(MAP_TESTS_H): $(MAP_TESTS_DIR) $(MAP_TESTS_FILES)
267 echo '#endif' \ 270 echo '#endif' \
268 ) > $(MAP_TESTS_H)) 271 ) > $(MAP_TESTS_H))
269 272
270VERIFIER_TESTS_H := $(OUTPUT)/verifier/tests.h
271test_verifier.c: $(VERIFIER_TESTS_H)
272$(OUTPUT)/test_verifier: CFLAGS += $(TEST_VERIFIER_CFLAGS)
273
274VERIFIER_TESTS_DIR = $(OUTPUT)/verifier 273VERIFIER_TESTS_DIR = $(OUTPUT)/verifier
275$(VERIFIER_TESTS_DIR): 274$(VERIFIER_TESTS_DIR):
276 mkdir -p $@ 275 mkdir -p $@
277 276VERIFIER_TESTS_H := $(VERIFIER_TESTS_DIR)/tests.h
278VERIFIER_TEST_FILES := $(wildcard verifier/*.c) 277VERIFIER_TEST_FILES := $(wildcard verifier/*.c)
279$(OUTPUT)/verifier/tests.h: $(VERIFIER_TESTS_DIR) $(VERIFIER_TEST_FILES) 278test_verifier.c: $(VERIFIER_TESTS_H)
279$(OUTPUT)/test_verifier: CFLAGS += $(TEST_VERIFIER_CFLAGS)
280$(OUTPUT)/test_verifier: test_verifier.c $(VERIFIER_TESTS_H)
281$(VERIFIER_TESTS_H): $(VERIFIER_TEST_FILES) | $(VERIFIER_TESTS_DIR)
280 $(shell ( cd verifier/; \ 282 $(shell ( cd verifier/; \
281 echo '/* Generated header, do not edit */'; \ 283 echo '/* Generated header, do not edit */'; \
282 echo '#ifdef FILL_ARRAY'; \ 284 echo '#ifdef FILL_ARRAY'; \
diff --git a/tools/testing/selftests/bpf/bpf_helpers.h b/tools/testing/selftests/bpf/bpf_helpers.h
index 5a3d92c8bec8..f804f210244e 100644
--- a/tools/testing/selftests/bpf/bpf_helpers.h
+++ b/tools/testing/selftests/bpf/bpf_helpers.h
@@ -315,8 +315,8 @@ static int (*bpf_skb_adjust_room)(void *ctx, __s32 len_diff, __u32 mode,
315#if defined(__TARGET_ARCH_x86) 315#if defined(__TARGET_ARCH_x86)
316 #define bpf_target_x86 316 #define bpf_target_x86
317 #define bpf_target_defined 317 #define bpf_target_defined
318#elif defined(__TARGET_ARCH_s930x) 318#elif defined(__TARGET_ARCH_s390)
319 #define bpf_target_s930x 319 #define bpf_target_s390
320 #define bpf_target_defined 320 #define bpf_target_defined
321#elif defined(__TARGET_ARCH_arm) 321#elif defined(__TARGET_ARCH_arm)
322 #define bpf_target_arm 322 #define bpf_target_arm
@@ -341,8 +341,8 @@ static int (*bpf_skb_adjust_room)(void *ctx, __s32 len_diff, __u32 mode,
341#ifndef bpf_target_defined 341#ifndef bpf_target_defined
342#if defined(__x86_64__) 342#if defined(__x86_64__)
343 #define bpf_target_x86 343 #define bpf_target_x86
344#elif defined(__s390x__) 344#elif defined(__s390__)
345 #define bpf_target_s930x 345 #define bpf_target_s390
346#elif defined(__arm__) 346#elif defined(__arm__)
347 #define bpf_target_arm 347 #define bpf_target_arm
348#elif defined(__aarch64__) 348#elif defined(__aarch64__)
@@ -358,6 +358,7 @@ static int (*bpf_skb_adjust_room)(void *ctx, __s32 len_diff, __u32 mode,
358 358
359#if defined(bpf_target_x86) 359#if defined(bpf_target_x86)
360 360
361#ifdef __KERNEL__
361#define PT_REGS_PARM1(x) ((x)->di) 362#define PT_REGS_PARM1(x) ((x)->di)
362#define PT_REGS_PARM2(x) ((x)->si) 363#define PT_REGS_PARM2(x) ((x)->si)
363#define PT_REGS_PARM3(x) ((x)->dx) 364#define PT_REGS_PARM3(x) ((x)->dx)
@@ -368,19 +369,49 @@ static int (*bpf_skb_adjust_room)(void *ctx, __s32 len_diff, __u32 mode,
368#define PT_REGS_RC(x) ((x)->ax) 369#define PT_REGS_RC(x) ((x)->ax)
369#define PT_REGS_SP(x) ((x)->sp) 370#define PT_REGS_SP(x) ((x)->sp)
370#define PT_REGS_IP(x) ((x)->ip) 371#define PT_REGS_IP(x) ((x)->ip)
372#else
373#ifdef __i386__
374/* i386 kernel is built with -mregparm=3 */
375#define PT_REGS_PARM1(x) ((x)->eax)
376#define PT_REGS_PARM2(x) ((x)->edx)
377#define PT_REGS_PARM3(x) ((x)->ecx)
378#define PT_REGS_PARM4(x) 0
379#define PT_REGS_PARM5(x) 0
380#define PT_REGS_RET(x) ((x)->esp)
381#define PT_REGS_FP(x) ((x)->ebp)
382#define PT_REGS_RC(x) ((x)->eax)
383#define PT_REGS_SP(x) ((x)->esp)
384#define PT_REGS_IP(x) ((x)->eip)
385#else
386#define PT_REGS_PARM1(x) ((x)->rdi)
387#define PT_REGS_PARM2(x) ((x)->rsi)
388#define PT_REGS_PARM3(x) ((x)->rdx)
389#define PT_REGS_PARM4(x) ((x)->rcx)
390#define PT_REGS_PARM5(x) ((x)->r8)
391#define PT_REGS_RET(x) ((x)->rsp)
392#define PT_REGS_FP(x) ((x)->rbp)
393#define PT_REGS_RC(x) ((x)->rax)
394#define PT_REGS_SP(x) ((x)->rsp)
395#define PT_REGS_IP(x) ((x)->rip)
396#endif
397#endif
371 398
372#elif defined(bpf_target_s390x) 399#elif defined(bpf_target_s390)
373 400
374#define PT_REGS_PARM1(x) ((x)->gprs[2]) 401/* s390 provides user_pt_regs instead of struct pt_regs to userspace */
375#define PT_REGS_PARM2(x) ((x)->gprs[3]) 402struct pt_regs;
376#define PT_REGS_PARM3(x) ((x)->gprs[4]) 403#define PT_REGS_S390 const volatile user_pt_regs
377#define PT_REGS_PARM4(x) ((x)->gprs[5]) 404#define PT_REGS_PARM1(x) (((PT_REGS_S390 *)(x))->gprs[2])
378#define PT_REGS_PARM5(x) ((x)->gprs[6]) 405#define PT_REGS_PARM2(x) (((PT_REGS_S390 *)(x))->gprs[3])
379#define PT_REGS_RET(x) ((x)->gprs[14]) 406#define PT_REGS_PARM3(x) (((PT_REGS_S390 *)(x))->gprs[4])
380#define PT_REGS_FP(x) ((x)->gprs[11]) /* Works only with CONFIG_FRAME_POINTER */ 407#define PT_REGS_PARM4(x) (((PT_REGS_S390 *)(x))->gprs[5])
381#define PT_REGS_RC(x) ((x)->gprs[2]) 408#define PT_REGS_PARM5(x) (((PT_REGS_S390 *)(x))->gprs[6])
382#define PT_REGS_SP(x) ((x)->gprs[15]) 409#define PT_REGS_RET(x) (((PT_REGS_S390 *)(x))->gprs[14])
383#define PT_REGS_IP(x) ((x)->psw.addr) 410/* Works only with CONFIG_FRAME_POINTER */
411#define PT_REGS_FP(x) (((PT_REGS_S390 *)(x))->gprs[11])
412#define PT_REGS_RC(x) (((PT_REGS_S390 *)(x))->gprs[2])
413#define PT_REGS_SP(x) (((PT_REGS_S390 *)(x))->gprs[15])
414#define PT_REGS_IP(x) (((PT_REGS_S390 *)(x))->psw.addr)
384 415
385#elif defined(bpf_target_arm) 416#elif defined(bpf_target_arm)
386 417
@@ -397,16 +428,20 @@ static int (*bpf_skb_adjust_room)(void *ctx, __s32 len_diff, __u32 mode,
397 428
398#elif defined(bpf_target_arm64) 429#elif defined(bpf_target_arm64)
399 430
400#define PT_REGS_PARM1(x) ((x)->regs[0]) 431/* arm64 provides struct user_pt_regs instead of struct pt_regs to userspace */
401#define PT_REGS_PARM2(x) ((x)->regs[1]) 432struct pt_regs;
402#define PT_REGS_PARM3(x) ((x)->regs[2]) 433#define PT_REGS_ARM64 const volatile struct user_pt_regs
403#define PT_REGS_PARM4(x) ((x)->regs[3]) 434#define PT_REGS_PARM1(x) (((PT_REGS_ARM64 *)(x))->regs[0])
404#define PT_REGS_PARM5(x) ((x)->regs[4]) 435#define PT_REGS_PARM2(x) (((PT_REGS_ARM64 *)(x))->regs[1])
405#define PT_REGS_RET(x) ((x)->regs[30]) 436#define PT_REGS_PARM3(x) (((PT_REGS_ARM64 *)(x))->regs[2])
406#define PT_REGS_FP(x) ((x)->regs[29]) /* Works only with CONFIG_FRAME_POINTER */ 437#define PT_REGS_PARM4(x) (((PT_REGS_ARM64 *)(x))->regs[3])
407#define PT_REGS_RC(x) ((x)->regs[0]) 438#define PT_REGS_PARM5(x) (((PT_REGS_ARM64 *)(x))->regs[4])
408#define PT_REGS_SP(x) ((x)->sp) 439#define PT_REGS_RET(x) (((PT_REGS_ARM64 *)(x))->regs[30])
409#define PT_REGS_IP(x) ((x)->pc) 440/* Works only with CONFIG_FRAME_POINTER */
441#define PT_REGS_FP(x) (((PT_REGS_ARM64 *)(x))->regs[29])
442#define PT_REGS_RC(x) (((PT_REGS_ARM64 *)(x))->regs[0])
443#define PT_REGS_SP(x) (((PT_REGS_ARM64 *)(x))->sp)
444#define PT_REGS_IP(x) (((PT_REGS_ARM64 *)(x))->pc)
410 445
411#elif defined(bpf_target_mips) 446#elif defined(bpf_target_mips)
412 447
@@ -452,10 +487,10 @@ static int (*bpf_skb_adjust_room)(void *ctx, __s32 len_diff, __u32 mode,
452 487
453#endif 488#endif
454 489
455#ifdef bpf_target_powerpc 490#if defined(bpf_target_powerpc)
456#define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = (ctx)->link; }) 491#define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = (ctx)->link; })
457#define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP 492#define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP
458#elif bpf_target_sparc 493#elif defined(bpf_target_sparc)
459#define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = PT_REGS_RET(ctx); }) 494#define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = PT_REGS_RET(ctx); })
460#define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP 495#define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP
461#else 496#else
diff --git a/tools/testing/selftests/bpf/prog_tests/attach_probe.c b/tools/testing/selftests/bpf/prog_tests/attach_probe.c
index a4686395522c..5ecc267d98b0 100644
--- a/tools/testing/selftests/bpf/prog_tests/attach_probe.c
+++ b/tools/testing/selftests/bpf/prog_tests/attach_probe.c
@@ -21,12 +21,6 @@ ssize_t get_base_addr() {
21 return -EINVAL; 21 return -EINVAL;
22} 22}
23 23
24#ifdef __x86_64__
25#define SYS_KPROBE_NAME "__x64_sys_nanosleep"
26#else
27#define SYS_KPROBE_NAME "sys_nanosleep"
28#endif
29
30void test_attach_probe(void) 24void test_attach_probe(void)
31{ 25{
32 const char *kprobe_name = "kprobe/sys_nanosleep"; 26 const char *kprobe_name = "kprobe/sys_nanosleep";
@@ -84,7 +78,7 @@ void test_attach_probe(void)
84 78
85 kprobe_link = bpf_program__attach_kprobe(kprobe_prog, 79 kprobe_link = bpf_program__attach_kprobe(kprobe_prog,
86 false /* retprobe */, 80 false /* retprobe */,
87 SYS_KPROBE_NAME); 81 SYS_NANOSLEEP_KPROBE_NAME);
88 if (CHECK(IS_ERR(kprobe_link), "attach_kprobe", 82 if (CHECK(IS_ERR(kprobe_link), "attach_kprobe",
89 "err %ld\n", PTR_ERR(kprobe_link))) { 83 "err %ld\n", PTR_ERR(kprobe_link))) {
90 kprobe_link = NULL; 84 kprobe_link = NULL;
@@ -92,7 +86,7 @@ void test_attach_probe(void)
92 } 86 }
93 kretprobe_link = bpf_program__attach_kprobe(kretprobe_prog, 87 kretprobe_link = bpf_program__attach_kprobe(kretprobe_prog,
94 true /* retprobe */, 88 true /* retprobe */,
95 SYS_KPROBE_NAME); 89 SYS_NANOSLEEP_KPROBE_NAME);
96 if (CHECK(IS_ERR(kretprobe_link), "attach_kretprobe", 90 if (CHECK(IS_ERR(kretprobe_link), "attach_kretprobe",
97 "err %ld\n", PTR_ERR(kretprobe_link))) { 91 "err %ld\n", PTR_ERR(kretprobe_link))) {
98 kretprobe_link = NULL; 92 kretprobe_link = NULL;
diff --git a/tools/testing/selftests/bpf/prog_tests/perf_buffer.c b/tools/testing/selftests/bpf/prog_tests/perf_buffer.c
index 3f1ef95865ff..3003fddc0613 100644
--- a/tools/testing/selftests/bpf/prog_tests/perf_buffer.c
+++ b/tools/testing/selftests/bpf/prog_tests/perf_buffer.c
@@ -5,12 +5,6 @@
5#include <sys/socket.h> 5#include <sys/socket.h>
6#include <test_progs.h> 6#include <test_progs.h>
7 7
8#ifdef __x86_64__
9#define SYS_KPROBE_NAME "__x64_sys_nanosleep"
10#else
11#define SYS_KPROBE_NAME "sys_nanosleep"
12#endif
13
14static void on_sample(void *ctx, int cpu, void *data, __u32 size) 8static void on_sample(void *ctx, int cpu, void *data, __u32 size)
15{ 9{
16 int cpu_data = *(int *)data, duration = 0; 10 int cpu_data = *(int *)data, duration = 0;
@@ -56,7 +50,7 @@ void test_perf_buffer(void)
56 50
57 /* attach kprobe */ 51 /* attach kprobe */
58 link = bpf_program__attach_kprobe(prog, false /* retprobe */, 52 link = bpf_program__attach_kprobe(prog, false /* retprobe */,
59 SYS_KPROBE_NAME); 53 SYS_NANOSLEEP_KPROBE_NAME);
60 if (CHECK(IS_ERR(link), "attach_kprobe", "err %ld\n", PTR_ERR(link))) 54 if (CHECK(IS_ERR(link), "attach_kprobe", "err %ld\n", PTR_ERR(link)))
61 goto out_close; 55 goto out_close;
62 56
diff --git a/tools/testing/selftests/bpf/prog_tests/send_signal.c b/tools/testing/selftests/bpf/prog_tests/send_signal.c
index 67cea1686305..54218ee3c004 100644
--- a/tools/testing/selftests/bpf/prog_tests/send_signal.c
+++ b/tools/testing/selftests/bpf/prog_tests/send_signal.c
@@ -173,6 +173,18 @@ static int test_send_signal_tracepoint(void)
173 return test_send_signal_common(&attr, BPF_PROG_TYPE_TRACEPOINT, "tracepoint"); 173 return test_send_signal_common(&attr, BPF_PROG_TYPE_TRACEPOINT, "tracepoint");
174} 174}
175 175
176static int test_send_signal_perf(void)
177{
178 struct perf_event_attr attr = {
179 .sample_period = 1,
180 .type = PERF_TYPE_SOFTWARE,
181 .config = PERF_COUNT_SW_CPU_CLOCK,
182 };
183
184 return test_send_signal_common(&attr, BPF_PROG_TYPE_PERF_EVENT,
185 "perf_sw_event");
186}
187
176static int test_send_signal_nmi(void) 188static int test_send_signal_nmi(void)
177{ 189{
178 struct perf_event_attr attr = { 190 struct perf_event_attr attr = {
@@ -181,8 +193,26 @@ static int test_send_signal_nmi(void)
181 .type = PERF_TYPE_HARDWARE, 193 .type = PERF_TYPE_HARDWARE,
182 .config = PERF_COUNT_HW_CPU_CYCLES, 194 .config = PERF_COUNT_HW_CPU_CYCLES,
183 }; 195 };
196 int pmu_fd;
197
198 /* Some setups (e.g. virtual machines) might run with hardware
199 * perf events disabled. If this is the case, skip this test.
200 */
201 pmu_fd = syscall(__NR_perf_event_open, &attr, 0 /* pid */,
202 -1 /* cpu */, -1 /* group_fd */, 0 /* flags */);
203 if (pmu_fd == -1) {
204 if (errno == ENOENT) {
205 printf("%s:SKIP:no PERF_COUNT_HW_CPU_CYCLES\n",
206 __func__);
207 return 0;
208 }
209 /* Let the test fail with a more informative message */
210 } else {
211 close(pmu_fd);
212 }
184 213
185 return test_send_signal_common(&attr, BPF_PROG_TYPE_PERF_EVENT, "perf_event"); 214 return test_send_signal_common(&attr, BPF_PROG_TYPE_PERF_EVENT,
215 "perf_hw_event");
186} 216}
187 217
188void test_send_signal(void) 218void test_send_signal(void)
@@ -190,6 +220,7 @@ void test_send_signal(void)
190 int ret = 0; 220 int ret = 0;
191 221
192 ret |= test_send_signal_tracepoint(); 222 ret |= test_send_signal_tracepoint();
223 ret |= test_send_signal_perf();
193 ret |= test_send_signal_nmi(); 224 ret |= test_send_signal_nmi();
194 if (!ret) 225 if (!ret)
195 printf("test_send_signal:OK\n"); 226 printf("test_send_signal:OK\n");
diff --git a/tools/testing/selftests/bpf/progs/loop1.c b/tools/testing/selftests/bpf/progs/loop1.c
index dea395af9ea9..7cdb7f878310 100644
--- a/tools/testing/selftests/bpf/progs/loop1.c
+++ b/tools/testing/selftests/bpf/progs/loop1.c
@@ -18,7 +18,7 @@ int nested_loops(volatile struct pt_regs* ctx)
18 for (j = 0; j < 300; j++) 18 for (j = 0; j < 300; j++)
19 for (i = 0; i < j; i++) { 19 for (i = 0; i < j; i++) {
20 if (j & 1) 20 if (j & 1)
21 m = ctx->rax; 21 m = PT_REGS_RC(ctx);
22 else 22 else
23 m = j; 23 m = j;
24 sum += i * m; 24 sum += i * m;
diff --git a/tools/testing/selftests/bpf/progs/loop2.c b/tools/testing/selftests/bpf/progs/loop2.c
index 0637bd8e8bcf..9b2f808a2863 100644
--- a/tools/testing/selftests/bpf/progs/loop2.c
+++ b/tools/testing/selftests/bpf/progs/loop2.c
@@ -16,7 +16,7 @@ int while_true(volatile struct pt_regs* ctx)
16 int i = 0; 16 int i = 0;
17 17
18 while (true) { 18 while (true) {
19 if (ctx->rax & 1) 19 if (PT_REGS_RC(ctx) & 1)
20 i += 3; 20 i += 3;
21 else 21 else
22 i += 7; 22 i += 7;
diff --git a/tools/testing/selftests/bpf/progs/loop3.c b/tools/testing/selftests/bpf/progs/loop3.c
index 30a0f6cba080..d727657d51e2 100644
--- a/tools/testing/selftests/bpf/progs/loop3.c
+++ b/tools/testing/selftests/bpf/progs/loop3.c
@@ -16,7 +16,7 @@ int while_true(volatile struct pt_regs* ctx)
16 __u64 i = 0, sum = 0; 16 __u64 i = 0, sum = 0;
17 do { 17 do {
18 i++; 18 i++;
19 sum += ctx->rax; 19 sum += PT_REGS_RC(ctx);
20 } while (i < 0x100000000ULL); 20 } while (i < 0x100000000ULL);
21 return sum; 21 return sum;
22} 22}
diff --git a/tools/testing/selftests/bpf/progs/test_get_stack_rawtp.c b/tools/testing/selftests/bpf/progs/test_get_stack_rawtp.c
index d06b47a09097..33254b771384 100644
--- a/tools/testing/selftests/bpf/progs/test_get_stack_rawtp.c
+++ b/tools/testing/selftests/bpf/progs/test_get_stack_rawtp.c
@@ -47,11 +47,12 @@ struct {
47 * issue and avoid complicated C programming massaging. 47 * issue and avoid complicated C programming massaging.
48 * This is an acceptable workaround since there is one entry here. 48 * This is an acceptable workaround since there is one entry here.
49 */ 49 */
50typedef __u64 raw_stack_trace_t[2 * MAX_STACK_RAWTP];
50struct { 51struct {
51 __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); 52 __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
52 __uint(max_entries, 1); 53 __uint(max_entries, 1);
53 __type(key, __u32); 54 __type(key, __u32);
54 __u64 (*value)[2 * MAX_STACK_RAWTP]; 55 __type(value, raw_stack_trace_t);
55} rawdata_map SEC(".maps"); 56} rawdata_map SEC(".maps");
56 57
57SEC("tracepoint/raw_syscalls/sys_enter") 58SEC("tracepoint/raw_syscalls/sys_enter")
diff --git a/tools/testing/selftests/bpf/progs/test_stacktrace_build_id.c b/tools/testing/selftests/bpf/progs/test_stacktrace_build_id.c
index bbfc8337b6f0..f5638e26865d 100644
--- a/tools/testing/selftests/bpf/progs/test_stacktrace_build_id.c
+++ b/tools/testing/selftests/bpf/progs/test_stacktrace_build_id.c
@@ -36,8 +36,7 @@ struct {
36 __uint(type, BPF_MAP_TYPE_ARRAY); 36 __uint(type, BPF_MAP_TYPE_ARRAY);
37 __uint(max_entries, 128); 37 __uint(max_entries, 128);
38 __type(key, __u32); 38 __type(key, __u32);
39 /* there seems to be a bug in kernel not handling typedef properly */ 39 __type(value, stack_trace_t);
40 struct bpf_stack_build_id (*value)[PERF_MAX_STACK_DEPTH];
41} stack_amap SEC(".maps"); 40} stack_amap SEC(".maps");
42 41
43/* taken from /sys/kernel/debug/tracing/events/random/urandom_read/format */ 42/* taken from /sys/kernel/debug/tracing/events/random/urandom_read/format */
diff --git a/tools/testing/selftests/bpf/progs/test_stacktrace_map.c b/tools/testing/selftests/bpf/progs/test_stacktrace_map.c
index 803c15dc109d..fa0be3e10a10 100644
--- a/tools/testing/selftests/bpf/progs/test_stacktrace_map.c
+++ b/tools/testing/selftests/bpf/progs/test_stacktrace_map.c
@@ -35,7 +35,7 @@ struct {
35 __uint(type, BPF_MAP_TYPE_ARRAY); 35 __uint(type, BPF_MAP_TYPE_ARRAY);
36 __uint(max_entries, 16384); 36 __uint(max_entries, 16384);
37 __type(key, __u32); 37 __type(key, __u32);
38 __u64 (*value)[PERF_MAX_STACK_DEPTH]; 38 __type(value, stack_trace_t);
39} stack_amap SEC(".maps"); 39} stack_amap SEC(".maps");
40 40
41/* taken from /sys/kernel/debug/tracing/events/sched/sched_switch/format */ 41/* taken from /sys/kernel/debug/tracing/events/sched/sched_switch/format */
diff --git a/tools/testing/selftests/bpf/progs/test_xdp_noinline.c b/tools/testing/selftests/bpf/progs/test_xdp_noinline.c
index dad8a7e33eaa..e88d7b9d65ab 100644
--- a/tools/testing/selftests/bpf/progs/test_xdp_noinline.c
+++ b/tools/testing/selftests/bpf/progs/test_xdp_noinline.c
@@ -14,6 +14,7 @@
14#include <linux/tcp.h> 14#include <linux/tcp.h>
15#include <linux/udp.h> 15#include <linux/udp.h>
16#include "bpf_helpers.h" 16#include "bpf_helpers.h"
17#include "bpf_endian.h"
17 18
18static __u32 rol32(__u32 word, unsigned int shift) 19static __u32 rol32(__u32 word, unsigned int shift)
19{ 20{
@@ -305,7 +306,7 @@ bool encap_v6(struct xdp_md *xdp, struct ctl_value *cval,
305 ip6h->nexthdr = IPPROTO_IPV6; 306 ip6h->nexthdr = IPPROTO_IPV6;
306 ip_suffix = pckt->flow.srcv6[3] ^ pckt->flow.port16[0]; 307 ip_suffix = pckt->flow.srcv6[3] ^ pckt->flow.port16[0];
307 ip6h->payload_len = 308 ip6h->payload_len =
308 __builtin_bswap16(pkt_bytes + sizeof(struct ipv6hdr)); 309 bpf_htons(pkt_bytes + sizeof(struct ipv6hdr));
309 ip6h->hop_limit = 4; 310 ip6h->hop_limit = 4;
310 311
311 ip6h->saddr.in6_u.u6_addr32[0] = 1; 312 ip6h->saddr.in6_u.u6_addr32[0] = 1;
@@ -322,7 +323,7 @@ bool encap_v4(struct xdp_md *xdp, struct ctl_value *cval,
322 struct real_definition *dst, __u32 pkt_bytes) 323 struct real_definition *dst, __u32 pkt_bytes)
323{ 324{
324 325
325 __u32 ip_suffix = __builtin_bswap16(pckt->flow.port16[0]); 326 __u32 ip_suffix = bpf_ntohs(pckt->flow.port16[0]);
326 struct eth_hdr *new_eth; 327 struct eth_hdr *new_eth;
327 struct eth_hdr *old_eth; 328 struct eth_hdr *old_eth;
328 __u16 *next_iph_u16; 329 __u16 *next_iph_u16;
@@ -352,7 +353,7 @@ bool encap_v4(struct xdp_md *xdp, struct ctl_value *cval,
352 iph->protocol = IPPROTO_IPIP; 353 iph->protocol = IPPROTO_IPIP;
353 iph->check = 0; 354 iph->check = 0;
354 iph->tos = 1; 355 iph->tos = 1;
355 iph->tot_len = __builtin_bswap16(pkt_bytes + sizeof(struct iphdr)); 356 iph->tot_len = bpf_htons(pkt_bytes + sizeof(struct iphdr));
356 /* don't update iph->daddr, since it will overwrite old eth_proto 357 /* don't update iph->daddr, since it will overwrite old eth_proto
357 * and multiple iterations of bpf_prog_run() will fail 358 * and multiple iterations of bpf_prog_run() will fail
358 */ 359 */
@@ -639,7 +640,7 @@ static int process_l3_headers_v6(struct packet_description *pckt,
639 iph_len = sizeof(struct ipv6hdr); 640 iph_len = sizeof(struct ipv6hdr);
640 *protocol = ip6h->nexthdr; 641 *protocol = ip6h->nexthdr;
641 pckt->flow.proto = *protocol; 642 pckt->flow.proto = *protocol;
642 *pkt_bytes = __builtin_bswap16(ip6h->payload_len); 643 *pkt_bytes = bpf_ntohs(ip6h->payload_len);
643 off += iph_len; 644 off += iph_len;
644 if (*protocol == 45) { 645 if (*protocol == 45) {
645 return XDP_DROP; 646 return XDP_DROP;
@@ -671,7 +672,7 @@ static int process_l3_headers_v4(struct packet_description *pckt,
671 return XDP_DROP; 672 return XDP_DROP;
672 *protocol = iph->protocol; 673 *protocol = iph->protocol;
673 pckt->flow.proto = *protocol; 674 pckt->flow.proto = *protocol;
674 *pkt_bytes = __builtin_bswap16(iph->tot_len); 675 *pkt_bytes = bpf_ntohs(iph->tot_len);
675 off += 20; 676 off += 20;
676 if (iph->frag_off & 65343) 677 if (iph->frag_off & 65343)
677 return XDP_DROP; 678 return XDP_DROP;
@@ -808,10 +809,10 @@ int balancer_ingress(struct xdp_md *ctx)
808 nh_off = sizeof(struct eth_hdr); 809 nh_off = sizeof(struct eth_hdr);
809 if (data + nh_off > data_end) 810 if (data + nh_off > data_end)
810 return XDP_DROP; 811 return XDP_DROP;
811 eth_proto = eth->eth_proto; 812 eth_proto = bpf_ntohs(eth->eth_proto);
812 if (eth_proto == 8) 813 if (eth_proto == ETH_P_IP)
813 return process_packet(data, nh_off, data_end, 0, ctx); 814 return process_packet(data, nh_off, data_end, 0, ctx);
814 else if (eth_proto == 56710) 815 else if (eth_proto == ETH_P_IPV6)
815 return process_packet(data, nh_off, data_end, 1, ctx); 816 return process_packet(data, nh_off, data_end, 1, ctx);
816 else 817 else
817 return XDP_DROP; 818 return XDP_DROP;
diff --git a/tools/testing/selftests/bpf/test_btf.c b/tools/testing/selftests/bpf/test_btf.c
index 8351cb5f4a20..3d617e806054 100644
--- a/tools/testing/selftests/bpf/test_btf.c
+++ b/tools/testing/selftests/bpf/test_btf.c
@@ -3417,6 +3417,94 @@ static struct btf_raw_test raw_tests[] = {
3417 .value_type_id = 1, 3417 .value_type_id = 1,
3418 .max_entries = 4, 3418 .max_entries = 4,
3419}, 3419},
3420/*
3421 * typedef int arr_t[16];
3422 * struct s {
3423 * arr_t *a;
3424 * };
3425 */
3426{
3427 .descr = "struct->ptr->typedef->array->int size resolution",
3428 .raw_types = {
3429 BTF_STRUCT_ENC(NAME_TBD, 1, 8), /* [1] */
3430 BTF_MEMBER_ENC(NAME_TBD, 2, 0),
3431 BTF_PTR_ENC(3), /* [2] */
3432 BTF_TYPEDEF_ENC(NAME_TBD, 4), /* [3] */
3433 BTF_TYPE_ARRAY_ENC(5, 5, 16), /* [4] */
3434 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [5] */
3435 BTF_END_RAW,
3436 },
3437 BTF_STR_SEC("\0s\0a\0arr_t"),
3438 .map_type = BPF_MAP_TYPE_ARRAY,
3439 .map_name = "ptr_mod_chain_size_resolve_map",
3440 .key_size = sizeof(int),
3441 .value_size = sizeof(int) * 16,
3442 .key_type_id = 5 /* int */,
3443 .value_type_id = 3 /* arr_t */,
3444 .max_entries = 4,
3445},
3446/*
3447 * typedef int arr_t[16][8][4];
3448 * struct s {
3449 * arr_t *a;
3450 * };
3451 */
3452{
3453 .descr = "struct->ptr->typedef->multi-array->int size resolution",
3454 .raw_types = {
3455 BTF_STRUCT_ENC(NAME_TBD, 1, 8), /* [1] */
3456 BTF_MEMBER_ENC(NAME_TBD, 2, 0),
3457 BTF_PTR_ENC(3), /* [2] */
3458 BTF_TYPEDEF_ENC(NAME_TBD, 4), /* [3] */
3459 BTF_TYPE_ARRAY_ENC(5, 7, 16), /* [4] */
3460 BTF_TYPE_ARRAY_ENC(6, 7, 8), /* [5] */
3461 BTF_TYPE_ARRAY_ENC(7, 7, 4), /* [6] */
3462 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [7] */
3463 BTF_END_RAW,
3464 },
3465 BTF_STR_SEC("\0s\0a\0arr_t"),
3466 .map_type = BPF_MAP_TYPE_ARRAY,
3467 .map_name = "multi_arr_size_resolve_map",
3468 .key_size = sizeof(int),
3469 .value_size = sizeof(int) * 16 * 8 * 4,
3470 .key_type_id = 7 /* int */,
3471 .value_type_id = 3 /* arr_t */,
3472 .max_entries = 4,
3473},
3474/*
3475 * typedef int int_t;
3476 * typedef int_t arr3_t[4];
3477 * typedef arr3_t arr2_t[8];
3478 * typedef arr2_t arr1_t[16];
3479 * struct s {
3480 * arr1_t *a;
3481 * };
3482 */
3483{
3484 .descr = "typedef/multi-arr mix size resolution",
3485 .raw_types = {
3486 BTF_STRUCT_ENC(NAME_TBD, 1, 8), /* [1] */
3487 BTF_MEMBER_ENC(NAME_TBD, 2, 0),
3488 BTF_PTR_ENC(3), /* [2] */
3489 BTF_TYPEDEF_ENC(NAME_TBD, 4), /* [3] */
3490 BTF_TYPE_ARRAY_ENC(5, 10, 16), /* [4] */
3491 BTF_TYPEDEF_ENC(NAME_TBD, 6), /* [5] */
3492 BTF_TYPE_ARRAY_ENC(7, 10, 8), /* [6] */
3493 BTF_TYPEDEF_ENC(NAME_TBD, 8), /* [7] */
3494 BTF_TYPE_ARRAY_ENC(9, 10, 4), /* [8] */
3495 BTF_TYPEDEF_ENC(NAME_TBD, 10), /* [9] */
3496 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [10] */
3497 BTF_END_RAW,
3498 },
3499 BTF_STR_SEC("\0s\0a\0arr1_t\0arr2_t\0arr3_t\0int_t"),
3500 .map_type = BPF_MAP_TYPE_ARRAY,
3501 .map_name = "typedef_arra_mix_size_resolve_map",
3502 .key_size = sizeof(int),
3503 .value_size = sizeof(int) * 16 * 8 * 4,
3504 .key_type_id = 10 /* int */,
3505 .value_type_id = 3 /* arr_t */,
3506 .max_entries = 4,
3507},
3420 3508
3421}; /* struct btf_raw_test raw_tests[] */ 3509}; /* struct btf_raw_test raw_tests[] */
3422 3510
diff --git a/tools/testing/selftests/bpf/test_progs.h b/tools/testing/selftests/bpf/test_progs.h
index f095e1d4c657..49e0f7d85643 100644
--- a/tools/testing/selftests/bpf/test_progs.h
+++ b/tools/testing/selftests/bpf/test_progs.h
@@ -92,3 +92,11 @@ int compare_map_keys(int map1_fd, int map2_fd);
92int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len); 92int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len);
93int extract_build_id(char *build_id, size_t size); 93int extract_build_id(char *build_id, size_t size);
94void *spin_lock_thread(void *arg); 94void *spin_lock_thread(void *arg);
95
96#ifdef __x86_64__
97#define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep"
98#elif defined(__s390x__)
99#define SYS_NANOSLEEP_KPROBE_NAME "__s390x_sys_nanosleep"
100#else
101#define SYS_NANOSLEEP_KPROBE_NAME "sys_nanosleep"
102#endif
diff --git a/tools/testing/selftests/bpf/test_verifier.c b/tools/testing/selftests/bpf/test_verifier.c
index b0773291012a..84135d5f4b35 100644
--- a/tools/testing/selftests/bpf/test_verifier.c
+++ b/tools/testing/selftests/bpf/test_verifier.c
@@ -86,7 +86,7 @@ struct bpf_test {
86 int fixup_sk_storage_map[MAX_FIXUPS]; 86 int fixup_sk_storage_map[MAX_FIXUPS];
87 const char *errstr; 87 const char *errstr;
88 const char *errstr_unpriv; 88 const char *errstr_unpriv;
89 uint32_t retval, retval_unpriv, insn_processed; 89 uint32_t insn_processed;
90 int prog_len; 90 int prog_len;
91 enum { 91 enum {
92 UNDEF, 92 UNDEF,
@@ -95,16 +95,20 @@ struct bpf_test {
95 } result, result_unpriv; 95 } result, result_unpriv;
96 enum bpf_prog_type prog_type; 96 enum bpf_prog_type prog_type;
97 uint8_t flags; 97 uint8_t flags;
98 __u8 data[TEST_DATA_LEN];
99 void (*fill_helper)(struct bpf_test *self); 98 void (*fill_helper)(struct bpf_test *self);
100 uint8_t runs; 99 uint8_t runs;
101 struct { 100#define bpf_testdata_struct_t \
102 uint32_t retval, retval_unpriv; 101 struct { \
103 union { 102 uint32_t retval, retval_unpriv; \
104 __u8 data[TEST_DATA_LEN]; 103 union { \
105 __u64 data64[TEST_DATA_LEN / 8]; 104 __u8 data[TEST_DATA_LEN]; \
106 }; 105 __u64 data64[TEST_DATA_LEN / 8]; \
107 } retvals[MAX_TEST_RUNS]; 106 }; \
107 }
108 union {
109 bpf_testdata_struct_t;
110 bpf_testdata_struct_t retvals[MAX_TEST_RUNS];
111 };
108 enum bpf_attach_type expected_attach_type; 112 enum bpf_attach_type expected_attach_type;
109}; 113};
110 114
@@ -949,17 +953,8 @@ static void do_test_single(struct bpf_test *test, bool unpriv,
949 uint32_t expected_val; 953 uint32_t expected_val;
950 int i; 954 int i;
951 955
952 if (!test->runs) { 956 if (!test->runs)
953 expected_val = unpriv && test->retval_unpriv ? 957 test->runs = 1;
954 test->retval_unpriv : test->retval;
955
956 err = do_prog_test_run(fd_prog, unpriv, expected_val,
957 test->data, sizeof(test->data));
958 if (err)
959 run_errs++;
960 else
961 run_successes++;
962 }
963 958
964 for (i = 0; i < test->runs; i++) { 959 for (i = 0; i < test->runs; i++) {
965 if (unpriv && test->retvals[i].retval_unpriv) 960 if (unpriv && test->retvals[i].retval_unpriv)
diff --git a/tools/testing/selftests/bpf/verifier/array_access.c b/tools/testing/selftests/bpf/verifier/array_access.c
index bcb83196e459..f3c33e128709 100644
--- a/tools/testing/selftests/bpf/verifier/array_access.c
+++ b/tools/testing/selftests/bpf/verifier/array_access.c
@@ -226,7 +226,7 @@
226 BPF_LD_MAP_FD(BPF_REG_1, 0), 226 BPF_LD_MAP_FD(BPF_REG_1, 0),
227 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), 227 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
228 BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 1), 228 BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 1),
229 BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 0), 229 BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_0, 0),
230 BPF_EXIT_INSN(), 230 BPF_EXIT_INSN(),
231 }, 231 },
232 .fixup_map_array_ro = { 3 }, 232 .fixup_map_array_ro = { 3 },
diff --git a/tools/testing/selftests/bpf/verifier/value_ptr_arith.c b/tools/testing/selftests/bpf/verifier/value_ptr_arith.c
index c3de1a2c9dc5..a53d99cebd9f 100644
--- a/tools/testing/selftests/bpf/verifier/value_ptr_arith.c
+++ b/tools/testing/selftests/bpf/verifier/value_ptr_arith.c
@@ -183,7 +183,7 @@
183 BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem), 183 BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
184 BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1), 184 BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
185 BPF_EXIT_INSN(), 185 BPF_EXIT_INSN(),
186 BPF_LDX_MEM(BPF_B, BPF_REG_1, BPF_REG_0, 0), 186 BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_0, 0),
187 BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 3), 187 BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 3),
188 BPF_MOV64_IMM(BPF_REG_2, 0), 188 BPF_MOV64_IMM(BPF_REG_2, 0),
189 BPF_MOV64_IMM(BPF_REG_3, 0x100000), 189 BPF_MOV64_IMM(BPF_REG_3, 0x100000),
diff --git a/tools/testing/selftests/bpf/verifier/wide_access.c b/tools/testing/selftests/bpf/verifier/wide_access.c
new file mode 100644
index 000000000000..ccade9312d21
--- /dev/null
+++ b/tools/testing/selftests/bpf/verifier/wide_access.c
@@ -0,0 +1,73 @@
1#define BPF_SOCK_ADDR_STORE(field, off, res, err) \
2{ \
3 "wide store to bpf_sock_addr." #field "[" #off "]", \
4 .insns = { \
5 BPF_MOV64_IMM(BPF_REG_0, 1), \
6 BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0, \
7 offsetof(struct bpf_sock_addr, field[off])), \
8 BPF_EXIT_INSN(), \
9 }, \
10 .result = res, \
11 .prog_type = BPF_PROG_TYPE_CGROUP_SOCK_ADDR, \
12 .expected_attach_type = BPF_CGROUP_UDP6_SENDMSG, \
13 .errstr = err, \
14}
15
16/* user_ip6[0] is u64 aligned */
17BPF_SOCK_ADDR_STORE(user_ip6, 0, ACCEPT,
18 NULL),
19BPF_SOCK_ADDR_STORE(user_ip6, 1, REJECT,
20 "invalid bpf_context access off=12 size=8"),
21BPF_SOCK_ADDR_STORE(user_ip6, 2, ACCEPT,
22 NULL),
23BPF_SOCK_ADDR_STORE(user_ip6, 3, REJECT,
24 "invalid bpf_context access off=20 size=8"),
25
26/* msg_src_ip6[0] is _not_ u64 aligned */
27BPF_SOCK_ADDR_STORE(msg_src_ip6, 0, REJECT,
28 "invalid bpf_context access off=44 size=8"),
29BPF_SOCK_ADDR_STORE(msg_src_ip6, 1, ACCEPT,
30 NULL),
31BPF_SOCK_ADDR_STORE(msg_src_ip6, 2, REJECT,
32 "invalid bpf_context access off=52 size=8"),
33BPF_SOCK_ADDR_STORE(msg_src_ip6, 3, REJECT,
34 "invalid bpf_context access off=56 size=8"),
35
36#undef BPF_SOCK_ADDR_STORE
37
38#define BPF_SOCK_ADDR_LOAD(field, off, res, err) \
39{ \
40 "wide load from bpf_sock_addr." #field "[" #off "]", \
41 .insns = { \
42 BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, \
43 offsetof(struct bpf_sock_addr, field[off])), \
44 BPF_MOV64_IMM(BPF_REG_0, 1), \
45 BPF_EXIT_INSN(), \
46 }, \
47 .result = res, \
48 .prog_type = BPF_PROG_TYPE_CGROUP_SOCK_ADDR, \
49 .expected_attach_type = BPF_CGROUP_UDP6_SENDMSG, \
50 .errstr = err, \
51}
52
53/* user_ip6[0] is u64 aligned */
54BPF_SOCK_ADDR_LOAD(user_ip6, 0, ACCEPT,
55 NULL),
56BPF_SOCK_ADDR_LOAD(user_ip6, 1, REJECT,
57 "invalid bpf_context access off=12 size=8"),
58BPF_SOCK_ADDR_LOAD(user_ip6, 2, ACCEPT,
59 NULL),
60BPF_SOCK_ADDR_LOAD(user_ip6, 3, REJECT,
61 "invalid bpf_context access off=20 size=8"),
62
63/* msg_src_ip6[0] is _not_ u64 aligned */
64BPF_SOCK_ADDR_LOAD(msg_src_ip6, 0, REJECT,
65 "invalid bpf_context access off=44 size=8"),
66BPF_SOCK_ADDR_LOAD(msg_src_ip6, 1, ACCEPT,
67 NULL),
68BPF_SOCK_ADDR_LOAD(msg_src_ip6, 2, REJECT,
69 "invalid bpf_context access off=52 size=8"),
70BPF_SOCK_ADDR_LOAD(msg_src_ip6, 3, REJECT,
71 "invalid bpf_context access off=56 size=8"),
72
73#undef BPF_SOCK_ADDR_LOAD
diff --git a/tools/testing/selftests/bpf/verifier/wide_store.c b/tools/testing/selftests/bpf/verifier/wide_store.c
deleted file mode 100644
index 8fe99602ded4..000000000000
--- a/tools/testing/selftests/bpf/verifier/wide_store.c
+++ /dev/null
@@ -1,36 +0,0 @@
1#define BPF_SOCK_ADDR(field, off, res, err) \
2{ \
3 "wide store to bpf_sock_addr." #field "[" #off "]", \
4 .insns = { \
5 BPF_MOV64_IMM(BPF_REG_0, 1), \
6 BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0, \
7 offsetof(struct bpf_sock_addr, field[off])), \
8 BPF_EXIT_INSN(), \
9 }, \
10 .result = res, \
11 .prog_type = BPF_PROG_TYPE_CGROUP_SOCK_ADDR, \
12 .expected_attach_type = BPF_CGROUP_UDP6_SENDMSG, \
13 .errstr = err, \
14}
15
16/* user_ip6[0] is u64 aligned */
17BPF_SOCK_ADDR(user_ip6, 0, ACCEPT,
18 NULL),
19BPF_SOCK_ADDR(user_ip6, 1, REJECT,
20 "invalid bpf_context access off=12 size=8"),
21BPF_SOCK_ADDR(user_ip6, 2, ACCEPT,
22 NULL),
23BPF_SOCK_ADDR(user_ip6, 3, REJECT,
24 "invalid bpf_context access off=20 size=8"),
25
26/* msg_src_ip6[0] is _not_ u64 aligned */
27BPF_SOCK_ADDR(msg_src_ip6, 0, REJECT,
28 "invalid bpf_context access off=44 size=8"),
29BPF_SOCK_ADDR(msg_src_ip6, 1, ACCEPT,
30 NULL),
31BPF_SOCK_ADDR(msg_src_ip6, 2, REJECT,
32 "invalid bpf_context access off=52 size=8"),
33BPF_SOCK_ADDR(msg_src_ip6, 3, REJECT,
34 "invalid bpf_context access off=56 size=8"),
35
36#undef BPF_SOCK_ADDR