Alexei Starovoitov says: ==================== pull-request: bpf 2020-07-09 The following pull-request contains BPF updates for your *net* tree. We've added 4 non-merge commits during the last 1 day(s) which contain a total of 4 files changed, 26 insertions(+), 15 deletions(-). The main changes are: 1) fix crash in libbpf on 32-bit archs, from Jakub and Andrii. 2) fix crash when l2tp and bpf_sk_reuseport conflict, from Martin. ====================
Unified · Split
| @@ -533,7 +533,8 @@ enum sk_pacing { · 532 unmodified lines | ||
| 533 | 533 | * be copied. |
| 534 | 534 | */ |
| 535 | 535 | #define SK_USER_DATA_NOCOPY 1UL |
| 536 | −#define SK_USER_DATA_PTRMASK ~(SK_USER_DATA_NOCOPY) | |
| 536 | +#define SK_USER_DATA_BPF 2UL /* Managed by BPF */ | |
| 537 | +#define SK_USER_DATA_PTRMASK ~(SK_USER_DATA_NOCOPY | SK_USER_DATA_BPF) | |
| 537 | 538 | |
| 538 | 539 | /** |
| 539 | 540 | * sk_user_data_is_nocopy - Test if sk_user_data pointer must not be copied |
| @@ -20,11 +20,14 @@ static struct reuseport_array *reuseport_array(struct bpf_map *map) · 19 unmodified lines | ||
| 20 | 20 | /* The caller must hold the reuseport_lock */ |
| 21 | 21 | void bpf_sk_reuseport_detach(struct sock *sk) |
| 22 | 22 | { |
| 23 | − struct sock __rcu **socks; | |
| 23 | + uintptr_t sk_user_data; | |
| 24 | 24 | |
| 25 | 25 | write_lock_bh(&sk->sk_callback_lock); |
| 26 | − socks = sk->sk_user_data; | |
| 27 | − if (socks) { | |
| 26 | + sk_user_data = (uintptr_t)sk->sk_user_data; | |
| 27 | + if (sk_user_data & SK_USER_DATA_BPF) { | |
| 28 | + struct sock __rcu **socks; | |
| 29 | + | |
| 30 | + socks = (void *)(sk_user_data & SK_USER_DATA_PTRMASK); | |
| 28 | 31 | WRITE_ONCE(sk->sk_user_data, NULL); |
| 29 | 32 | /* |
| 30 | 33 | * Do not move this NULL assignment outside of |
| @@ -252,6 +255,7 @@ int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key, · 221 unmodified lines | ||
| 252 | 255 | struct sock *free_osk = NULL, *osk, *nsk; |
| 253 | 256 | struct sock_reuseport *reuse; |
| 254 | 257 | u32 index = *(u32 *)key; |
| 258 | + uintptr_t sk_user_data; | |
| 255 | 259 | struct socket *socket; |
| 256 | 260 | int err, fd; |
| 257 | 261 | |
| @@ -305,7 +309,9 @@ int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key, · 47 unmodified lines | ||
| 305 | 309 | if (err) |
| 306 | 310 | goto put_file_unlock; |
| 307 | 311 | |
| 308 | − WRITE_ONCE(nsk->sk_user_data, &array->ptrs[index]); | |
| 312 | + sk_user_data = (uintptr_t)&array->ptrs[index] | SK_USER_DATA_NOCOPY | | |
| 313 | + SK_USER_DATA_BPF; | |
| 314 | + WRITE_ONCE(nsk->sk_user_data, (void *)sk_user_data); | |
| 309 | 315 | rcu_assign_pointer(array->ptrs[index], nsk); |
| 310 | 316 | free_osk = osk; |
| 311 | 317 | err = 0; |
| @@ -11,14 +11,18 @@ · 10 unmodified lines | ||
| 11 | 11 | #include <stdbool.h> |
| 12 | 12 | #include <stddef.h> |
| 13 | 13 | #include <limits.h> |
| 14 | −#ifndef __WORDSIZE | |
| 15 | −#define __WORDSIZE (__SIZEOF_LONG__ * 8) | |
| 16 | −#endif | |
| 17 | 14 | |
| 18 | 15 | static inline size_t hash_bits(size_t h, int bits) |
| 19 | 16 | { |
| 20 | 17 | /* shuffle bits and return requested number of upper bits */ |
| 21 | − return (h * 11400714819323198485llu) >> (__WORDSIZE - bits); | |
| 18 | +#if (__SIZEOF_SIZE_T__ == __SIZEOF_LONG_LONG__) | |
| 19 | + /* LP64 case */ | |
| 20 | + return (h * 11400714819323198485llu) >> (__SIZEOF_LONG_LONG__ * 8 - bits); | |
| 21 | +#elif (__SIZEOF_SIZE_T__ <= __SIZEOF_LONG__) | |
| 22 | + return (h * 2654435769lu) >> (__SIZEOF_LONG__ * 8 - bits); | |
| 23 | +#else | |
| 24 | +# error "Unsupported size_t size" | |
| 25 | +#endif | |
| 22 | 26 | } |
| 23 | 27 | |
| 24 | 28 | typedef size_t (*hashmap_hash_fn)(const void *key, void *ctx); |
| @@ -789,19 +789,19 @@ static void test_sockmap(unsigned int tasks, void *data) · 788 unmodified lines | ||
| 789 | 789 | } |
| 790 | 790 | |
| 791 | 791 | err = bpf_prog_detach(fd, BPF_SK_SKB_STREAM_PARSER); |
| 792 | − if (err) { | |
| 792 | + if (!err) { | |
| 793 | 793 | printf("Failed empty parser prog detach\n"); |
| 794 | 794 | goto out_sockmap; |
| 795 | 795 | } |
| 796 | 796 | |
| 797 | 797 | err = bpf_prog_detach(fd, BPF_SK_SKB_STREAM_VERDICT); |
| 798 | − if (err) { | |
| 798 | + if (!err) { | |
| 799 | 799 | printf("Failed empty verdict prog detach\n"); |
| 800 | 800 | goto out_sockmap; |
| 801 | 801 | } |
| 802 | 802 | |
| 803 | 803 | err = bpf_prog_detach(fd, BPF_SK_MSG_VERDICT); |
| 804 | − if (err) { | |
| 804 | + if (!err) { | |
| 805 | 805 | printf("Failed empty msg verdict prog detach\n"); |
| 806 | 806 | goto out_sockmap; |
| 807 | 807 | } |
| @@ -1090,19 +1090,19 @@ static void test_sockmap(unsigned int tasks, void *data) · 282 unmodified lines | ||
| 1090 | 1090 | assert(status == 0); |
| 1091 | 1091 | } |
| 1092 | 1092 | |
| 1093 | − err = bpf_prog_detach(map_fd_rx, __MAX_BPF_ATTACH_TYPE); | |
| 1093 | + err = bpf_prog_detach2(parse_prog, map_fd_rx, __MAX_BPF_ATTACH_TYPE); | |
| 1094 | 1094 | if (!err) { |
| 1095 | 1095 | printf("Detached an invalid prog type.\n"); |
| 1096 | 1096 | goto out_sockmap; |
| 1097 | 1097 | } |
| 1098 | 1098 | |
| 1099 | − err = bpf_prog_detach(map_fd_rx, BPF_SK_SKB_STREAM_PARSER); | |
| 1099 | + err = bpf_prog_detach2(parse_prog, map_fd_rx, BPF_SK_SKB_STREAM_PARSER); | |
| 1100 | 1100 | if (err) { |
| 1101 | 1101 | printf("Failed parser prog detach\n"); |
| 1102 | 1102 | goto out_sockmap; |
| 1103 | 1103 | } |
| 1104 | 1104 | |
| 1105 | − err = bpf_prog_detach(map_fd_rx, BPF_SK_SKB_STREAM_VERDICT); | |
| 1105 | + err = bpf_prog_detach2(verdict_prog, map_fd_rx, BPF_SK_SKB_STREAM_VERDICT); | |
| 1106 | 1106 | if (err) { |
| 1107 | 1107 | printf("Failed parser prog detach\n"); |
| 1108 | 1108 | goto out_sockmap; |