Skip to content

Commit 440ffcd

Browse files
committed
Daniel Borkmann says: ==================== pull-request: bpf 2021-10-26 We've added 12 non-merge commits during the last 7 day(s) which contain a total of 23 files changed, 118 insertions(+), 98 deletions(-). The main changes are: 1) Fix potential race window in BPF tail call compatibility check, from Toke Høiland-Jørgensen. 2) Fix memory leak in cgroup fs due to missing cgroup_bpf_offline(), from Quanyang Wang. 3) Fix file descriptor reference counting in generic_map_update_batch(), from Xu Kuohai. 4) Fix bpf_jit_limit knob to the max supported limit by the arch's JIT, from Lorenz Bauer. 5) Fix BPF sockmap ->poll callbacks for UDP and AF_UNIX sockets, from Cong Wang and Yucong Sun. 6) Fix BPF sockmap concurrency issue in TCP on non-blocking sendmsg calls, from Liu Jian. 7) Fix build failure of INODE_STORAGE and TASK_STORAGE maps on !CONFIG_NET, from Tejun Heo. * https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf: bpf: Fix potential race in tail call compatibility check bpf: Move BPF_MAP_TYPE for INODE_STORAGE and TASK_STORAGE outside of CONFIG_NET selftests/bpf: Use recv_timeout() instead of retries net: Implement ->sock_is_readable() for UDP and AF_UNIX skmsg: Extract and reuse sk_msg_is_readable() net: Rename ->stream_memory_read to ->sock_is_readable tcp_bpf: Fix one concurrency problem in the tcp_bpf_send_verdict function cgroup: Fix memory leak caused by missing cgroup_bpf_offline bpf: Fix error usage of map_fd and fdget() in generic_map_update_batch() bpf: Prevent increasing bpf_jit_limit above max bpf: Define bpf_jit_alloc_exec_limit for arm64 JIT bpf: Define bpf_jit_alloc_exec_limit for riscv JIT ==================== Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
2 parents 19fa088 + 54713c8 commit 440ffcd

File tree

23 files changed

+118
-98
lines changed

23 files changed

+118
-98
lines changed

arch/arm64/net/bpf_jit_comp.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,11 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
11361136
return prog;
11371137
}
11381138

1139+
u64 bpf_jit_alloc_exec_limit(void)
1140+
{
1141+
return BPF_JIT_REGION_SIZE;
1142+
}
1143+
11391144
void *bpf_jit_alloc_exec(unsigned long size)
11401145
{
11411146
return __vmalloc_node_range(size, PAGE_SIZE, BPF_JIT_REGION_START,

arch/riscv/net/bpf_jit_core.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
166166
return prog;
167167
}
168168

169+
u64 bpf_jit_alloc_exec_limit(void)
170+
{
171+
return BPF_JIT_REGION_SIZE;
172+
}
173+
169174
void *bpf_jit_alloc_exec(unsigned long size)
170175
{
171176
return __vmalloc_node_range(size, PAGE_SIZE, BPF_JIT_REGION_START,

include/linux/bpf.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -929,8 +929,11 @@ struct bpf_array_aux {
929929
* stored in the map to make sure that all callers and callees have
930930
* the same prog type and JITed flag.
931931
*/
932-
enum bpf_prog_type type;
933-
bool jited;
932+
struct {
933+
spinlock_t lock;
934+
enum bpf_prog_type type;
935+
bool jited;
936+
} owner;
934937
/* Programs with direct jumps into programs part of this array. */
935938
struct list_head poke_progs;
936939
struct bpf_map *map;

include/linux/bpf_types.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,14 @@ BPF_MAP_TYPE(BPF_MAP_TYPE_STACK_TRACE, stack_trace_map_ops)
101101
#endif
102102
BPF_MAP_TYPE(BPF_MAP_TYPE_ARRAY_OF_MAPS, array_of_maps_map_ops)
103103
BPF_MAP_TYPE(BPF_MAP_TYPE_HASH_OF_MAPS, htab_of_maps_map_ops)
104-
#ifdef CONFIG_NET
105-
BPF_MAP_TYPE(BPF_MAP_TYPE_DEVMAP, dev_map_ops)
106-
BPF_MAP_TYPE(BPF_MAP_TYPE_DEVMAP_HASH, dev_map_hash_ops)
107-
BPF_MAP_TYPE(BPF_MAP_TYPE_SK_STORAGE, sk_storage_map_ops)
108104
#ifdef CONFIG_BPF_LSM
109105
BPF_MAP_TYPE(BPF_MAP_TYPE_INODE_STORAGE, inode_storage_map_ops)
110106
#endif
111107
BPF_MAP_TYPE(BPF_MAP_TYPE_TASK_STORAGE, task_storage_map_ops)
108+
#ifdef CONFIG_NET
109+
BPF_MAP_TYPE(BPF_MAP_TYPE_DEVMAP, dev_map_ops)
110+
BPF_MAP_TYPE(BPF_MAP_TYPE_DEVMAP_HASH, dev_map_hash_ops)
111+
BPF_MAP_TYPE(BPF_MAP_TYPE_SK_STORAGE, sk_storage_map_ops)
112112
BPF_MAP_TYPE(BPF_MAP_TYPE_CPUMAP, cpu_map_ops)
113113
#if defined(CONFIG_XDP_SOCKETS)
114114
BPF_MAP_TYPE(BPF_MAP_TYPE_XSKMAP, xsk_map_ops)

include/linux/filter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,7 @@ extern int bpf_jit_enable;
10511051
extern int bpf_jit_harden;
10521052
extern int bpf_jit_kallsyms;
10531053
extern long bpf_jit_limit;
1054+
extern long bpf_jit_limit_max;
10541055

10551056
typedef void (*bpf_jit_fill_hole_t)(void *area, unsigned int size);
10561057

include/linux/skmsg.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ int sk_msg_memcopy_from_iter(struct sock *sk, struct iov_iter *from,
128128
struct sk_msg *msg, u32 bytes);
129129
int sk_msg_recvmsg(struct sock *sk, struct sk_psock *psock, struct msghdr *msg,
130130
int len, int flags);
131+
bool sk_msg_is_readable(struct sock *sk);
131132

132133
static inline void sk_msg_check_to_free(struct sk_msg *msg, u32 i, u32 bytes)
133134
{

include/net/sock.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1208,7 +1208,7 @@ struct proto {
12081208
#endif
12091209

12101210
bool (*stream_memory_free)(const struct sock *sk, int wake);
1211-
bool (*stream_memory_read)(const struct sock *sk);
1211+
bool (*sock_is_readable)(struct sock *sk);
12121212
/* Memory pressure */
12131213
void (*enter_memory_pressure)(struct sock *sk);
12141214
void (*leave_memory_pressure)(struct sock *sk);
@@ -2820,4 +2820,10 @@ void sock_set_sndtimeo(struct sock *sk, s64 secs);
28202820

28212821
int sock_bind_add(struct sock *sk, struct sockaddr *addr, int addr_len);
28222822

2823+
static inline bool sk_is_readable(struct sock *sk)
2824+
{
2825+
if (sk->sk_prot->sock_is_readable)
2826+
return sk->sk_prot->sock_is_readable(sk);
2827+
return false;
2828+
}
28232829
#endif /* _SOCK_H */

include/net/tls.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ void tls_sw_release_resources_rx(struct sock *sk);
375375
void tls_sw_free_ctx_rx(struct tls_context *tls_ctx);
376376
int tls_sw_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
377377
int nonblock, int flags, int *addr_len);
378-
bool tls_sw_stream_read(const struct sock *sk);
378+
bool tls_sw_sock_is_readable(struct sock *sk);
379379
ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,
380380
struct pipe_inode_info *pipe,
381381
size_t len, unsigned int flags);

kernel/bpf/arraymap.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,7 @@ static struct bpf_map *prog_array_map_alloc(union bpf_attr *attr)
10721072
INIT_WORK(&aux->work, prog_array_map_clear_deferred);
10731073
INIT_LIST_HEAD(&aux->poke_progs);
10741074
mutex_init(&aux->poke_mutex);
1075+
spin_lock_init(&aux->owner.lock);
10751076

10761077
map = array_map_alloc(attr);
10771078
if (IS_ERR(map)) {

kernel/bpf/core.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ int bpf_jit_enable __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_DEFAULT_ON);
524524
int bpf_jit_kallsyms __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_DEFAULT_ON);
525525
int bpf_jit_harden __read_mostly;
526526
long bpf_jit_limit __read_mostly;
527+
long bpf_jit_limit_max __read_mostly;
527528

528529
static void
529530
bpf_prog_ksym_set_addr(struct bpf_prog *prog)
@@ -817,7 +818,8 @@ u64 __weak bpf_jit_alloc_exec_limit(void)
817818
static int __init bpf_jit_charge_init(void)
818819
{
819820
/* Only used as heuristic here to derive limit. */
820-
bpf_jit_limit = min_t(u64, round_up(bpf_jit_alloc_exec_limit() >> 2,
821+
bpf_jit_limit_max = bpf_jit_alloc_exec_limit();
822+
bpf_jit_limit = min_t(u64, round_up(bpf_jit_limit_max >> 2,
821823
PAGE_SIZE), LONG_MAX);
822824
return 0;
823825
}
@@ -1821,20 +1823,26 @@ static unsigned int __bpf_prog_ret0_warn(const void *ctx,
18211823
bool bpf_prog_array_compatible(struct bpf_array *array,
18221824
const struct bpf_prog *fp)
18231825
{
1826+
bool ret;
1827+
18241828
if (fp->kprobe_override)
18251829
return false;
18261830

1827-
if (!array->aux->type) {
1831+
spin_lock(&array->aux->owner.lock);
1832+
1833+
if (!array->aux->owner.type) {
18281834
/* There's no owner yet where we could check for
18291835
* compatibility.
18301836
*/
1831-
array->aux->type = fp->type;
1832-
array->aux->jited = fp->jited;
1833-
return true;
1837+
array->aux->owner.type = fp->type;
1838+
array->aux->owner.jited = fp->jited;
1839+
ret = true;
1840+
} else {
1841+
ret = array->aux->owner.type == fp->type &&
1842+
array->aux->owner.jited == fp->jited;
18341843
}
1835-
1836-
return array->aux->type == fp->type &&
1837-
array->aux->jited == fp->jited;
1844+
spin_unlock(&array->aux->owner.lock);
1845+
return ret;
18381846
}
18391847

18401848
static int bpf_check_tail_call(const struct bpf_prog *fp)

0 commit comments

Comments
 (0)