Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
Signed-off-by: Geliang Tang <[email protected]>
  • Loading branch information
Geliang Tang committed Nov 20, 2024
1 parent b8af698 commit 7d29165
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 32 deletions.
43 changes: 23 additions & 20 deletions net/mptcp/bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -491,18 +491,27 @@ __bpf_kfunc_start_defs();

__bpf_kfunc static struct mptcp_sock *bpf_mptcp_sk(struct sock *sk)
{
if (!sk || sk->sk_protocol != IPPROTO_MPTCP)
return NULL;

return mptcp_sk(sk);
}

__bpf_kfunc static struct mptcp_subflow_context *
bpf_mptcp_subflow_ctx(const struct sock *sk)
{
if (!sk)
return NULL;

return mptcp_subflow_ctx(sk);
}

__bpf_kfunc static struct sock *
bpf_mptcp_subflow_tcp_sock(const struct mptcp_subflow_context *subflow)
{
if (!subflow)
return NULL;

return mptcp_subflow_tcp_sock(subflow);
}

Expand All @@ -516,7 +525,8 @@ bpf_iter_mptcp_subflow_new(struct bpf_iter_mptcp_subflow *it,
if (!msk)
return -EINVAL;

msk_owned_by_me(msk);
if (!lockdep_sock_is_held((const struct sock *)msk))
return -EINVAL;

kit->pos = &msk->conn_list;
return 0;
Expand Down Expand Up @@ -549,7 +559,8 @@ bpf_iter_mptcp_userspace_pm_addr_new(struct bpf_iter_mptcp_userspace_pm_addr *it
if (!msk)
return -EINVAL;

lockdep_assert_held(&msk->pm.lock);
if (!lockdep_is_held(&msk->pm.lock))
return -EINVAL;

kit->pos = &msk->pm.userspace_pm_local_addr_list;
return 0;
Expand Down Expand Up @@ -697,10 +708,10 @@ __bpf_kfunc static bool bpf_mptcp_subflow_queues_empty(struct sock *sk)

__bpf_kfunc_end_defs();

BTF_KFUNCS_START(bpf_mptcp_common_kfunc_ids)
BTF_ID_FLAGS(func, bpf_mptcp_sk)
BTF_ID_FLAGS(func, bpf_mptcp_subflow_ctx)
BTF_ID_FLAGS(func, bpf_mptcp_subflow_tcp_sock)
BTF_KFUNCS_START(bpf_mptcp_kfunc_ids)
BTF_ID_FLAGS(func, bpf_mptcp_sk, KF_TRUSTED_ARGS | KF_RET_NULL)
BTF_ID_FLAGS(func, bpf_mptcp_subflow_ctx, KF_RET_NULL)
BTF_ID_FLAGS(func, bpf_mptcp_subflow_tcp_sock, KF_RET_NULL)
BTF_ID_FLAGS(func, bpf_iter_mptcp_subflow_new, KF_ITER_NEW | KF_TRUSTED_ARGS)
BTF_ID_FLAGS(func, bpf_iter_mptcp_subflow_next, KF_ITER_NEXT | KF_RET_NULL)
BTF_ID_FLAGS(func, bpf_iter_mptcp_subflow_destroy, KF_ITER_DESTROY)
Expand All @@ -712,14 +723,6 @@ BTF_ID_FLAGS(func, bpf_mptcp_sock_release, KF_RELEASE)
BTF_ID_FLAGS(func, bpf_spin_lock_bh)
BTF_ID_FLAGS(func, bpf_spin_unlock_bh)
BTF_ID_FLAGS(func, bpf_ipv6_addr_v4mapped)
BTF_KFUNCS_END(bpf_mptcp_common_kfunc_ids)

static const struct btf_kfunc_id_set bpf_mptcp_common_kfunc_set = {
.owner = THIS_MODULE,
.set = &bpf_mptcp_common_kfunc_ids,
};

BTF_KFUNCS_START(bpf_mptcp_struct_ops_kfunc_ids)
BTF_ID_FLAGS(func, bpf_ipv6_addr_set_v4mapped)
BTF_ID_FLAGS(func, bpf_list_add_tail_rcu)
BTF_ID_FLAGS(func, bpf_list_del_rcu)
Expand Down Expand Up @@ -749,22 +752,22 @@ BTF_ID_FLAGS(func, mptcp_wnd_end)
BTF_ID_FLAGS(func, tcp_stream_memory_free)
BTF_ID_FLAGS(func, bpf_mptcp_subflow_queues_empty)
BTF_ID_FLAGS(func, mptcp_pm_subflow_chk_stale, KF_SLEEPABLE)
BTF_KFUNCS_END(bpf_mptcp_struct_ops_kfunc_ids)
BTF_KFUNCS_END(bpf_mptcp_kfunc_ids)

static const struct btf_kfunc_id_set bpf_mptcp_struct_ops_kfunc_set = {
static const struct btf_kfunc_id_set bpf_mptcp_kfunc_set = {
.owner = THIS_MODULE,
.set = &bpf_mptcp_struct_ops_kfunc_ids,
.set = &bpf_mptcp_kfunc_ids,
};

static int __init bpf_mptcp_kfunc_init(void)
{
int ret;

ret = register_btf_fmodret_id_set(&bpf_mptcp_fmodret_set);
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC,
&bpf_mptcp_common_kfunc_set);
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SOCKOPT,
&bpf_mptcp_kfunc_set);
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS,
&bpf_mptcp_struct_ops_kfunc_set);
&bpf_mptcp_kfunc_set);
#ifdef CONFIG_BPF_JIT
ret = ret ?: register_bpf_struct_ops(&bpf_mptcp_pm_ops, mptcp_pm_ops);
ret = ret ?: register_bpf_struct_ops(&bpf_mptcp_sched_ops, mptcp_sched_ops);
Expand Down
8 changes: 7 additions & 1 deletion tools/testing/selftests/bpf/progs/mptcp_bpf_first.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ void BPF_PROG(mptcp_sched_first_release, struct mptcp_sock *msk)

static int bpf_first_get_subflow(struct mptcp_sock *msk)
{
mptcp_subflow_set_scheduled(bpf_mptcp_subflow_ctx(msk->first), true);
struct mptcp_subflow_context *subflow;

subflow = bpf_mptcp_subflow_ctx(msk->first);
if (!subflow)
return -1;

mptcp_subflow_set_scheduled(subflow, true);
return 0;
}

Expand Down
9 changes: 4 additions & 5 deletions tools/testing/selftests/bpf/progs/mptcp_bpf_iters.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ int iters_subflow(struct bpf_sockopt *ctx)
return 1;

msk = bpf_mptcp_sk((struct sock *)sk);
if (msk->pm.server_side || !msk->pm.subflows)
if (!msk || msk->pm.server_side || !msk->pm.subflows)
return 1;

msk = bpf_mptcp_sock_acquire(msk);
Expand All @@ -53,7 +53,7 @@ int iters_subflow(struct bpf_sockopt *ctx)

/* only to check the following kfunc works */
subflow = bpf_mptcp_subflow_ctx(ssk);
if (subflow->token != msk->token)
if (!subflow || subflow->token != msk->token)
goto out;

ids = local_ids;
Expand All @@ -71,12 +71,11 @@ int iters_address(struct bpf_sockopt *ctx)
struct mptcp_sock *msk;
int local_ids = 0;

if (!sk || sk->protocol != IPPROTO_MPTCP ||
ctx->level != SOL_TCP || ctx->optname != TCP_IS_MPTCP)
if (ctx->level != SOL_TCP || ctx->optname != TCP_IS_MPTCP)
return 1;

msk = bpf_mptcp_sk((struct sock *)sk);
if (msk->pm.server_side)
if (!msk || msk->pm.server_side)
return 1;

msk = bpf_mptcp_sock_acquire(msk);
Expand Down
2 changes: 2 additions & 0 deletions tools/testing/selftests/bpf/progs/mptcp_bpf_rr.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ static int bpf_rr_get_subflow(struct mptcp_sock *msk)
return -1;

next = bpf_mptcp_subflow_ctx(msk->first);
if (!next)
return -1;
if (!ptr->last_snd)
goto out;

Expand Down
20 changes: 16 additions & 4 deletions tools/testing/selftests/bpf/progs/mptcp_bpf_sockopt.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ static int mptcp_setsockopt_mark(struct bpf_sock *sk, struct bpf_sockopt *ctx)

mark = *optval;

msk = bpf_mptcp_sock_acquire(bpf_mptcp_sk((struct sock *)sk));
msk = bpf_mptcp_sk((struct sock *)sk);
if (!msk)
return 1;
msk = bpf_mptcp_sock_acquire(msk);
if (!msk)
return 1;

Expand Down Expand Up @@ -54,7 +57,10 @@ static int mptcp_setsockopt_cc(struct bpf_sock *sk, struct bpf_sockopt *ctx)

__builtin_memcpy(cc, optval, TCP_CA_NAME_MAX);

msk = bpf_mptcp_sock_acquire(bpf_mptcp_sk((struct sock *)sk));
msk = bpf_mptcp_sk((struct sock *)sk);
if (!msk)
return 1;
msk = bpf_mptcp_sock_acquire(msk);
if (!msk)
return 1;

Expand Down Expand Up @@ -96,7 +102,10 @@ static int mptcp_getsockopt_mark(struct bpf_sock *sk, struct bpf_sockopt *ctx)
struct mptcp_sock *msk;
int i = 0;

msk = bpf_mptcp_sock_acquire(bpf_mptcp_sk((struct sock *)sk));
msk = bpf_mptcp_sk((struct sock *)sk);
if (!msk)
return 1;
msk = bpf_mptcp_sock_acquire(msk);
if (!msk)
return 1;

Expand All @@ -121,7 +130,10 @@ static int mptcp_getsockopt_cc(struct bpf_sock *sk, struct bpf_sockopt *ctx)
struct mptcp_sock *msk;
int i = 0;

msk = bpf_mptcp_sock_acquire(bpf_mptcp_sk((struct sock *)sk));
msk = bpf_mptcp_sk((struct sock *)sk);
if (!msk)
return 1;
msk = bpf_mptcp_sock_acquire(msk);
if (!msk)
return 1;

Expand Down
5 changes: 4 additions & 1 deletion tools/testing/selftests/bpf/progs/mptcp_bpf_stale.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ static int bpf_stale_get_subflow(struct mptcp_sock *msk)
int i;

if (!msk->pm.subflows) {
mptcp_subflow_set_scheduled(bpf_mptcp_subflow_ctx(msk->first), true);
subflow = bpf_mptcp_subflow_ctx(msk->first);
if (!subflow)
return -1;
mptcp_subflow_set_scheduled(subflow, true);
return 0;
}

Expand Down
7 changes: 6 additions & 1 deletion tools/testing/selftests/bpf/progs/mptcp_bpf_userspace_pm.c
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ static struct sock *mptcp_pm_find_ssk(struct mptcp_sock *msk,
struct sock *ssk;

ssk = bpf_mptcp_subflow_tcp_sock(subflow);
if (!ssk)
continue;

if (local->family != ssk->sk_family)
continue;
Expand Down Expand Up @@ -295,6 +297,7 @@ SEC("struct_ops")
int BPF_PROG(mptcp_pm_subflow_destroy, struct mptcp_sock *msk,
struct mptcp_pm_addr_entry *local, struct mptcp_addr_info *remote)
{
struct mptcp_subflow_context *subflow;
struct sock *sk = (struct sock *)msk;
struct sock *ssk;
int err = 0;
Expand Down Expand Up @@ -330,7 +333,9 @@ int BPF_PROG(mptcp_pm_subflow_destroy, struct mptcp_sock *msk,
err = mptcp_userspace_pm_delete_local_addr(msk, local);
bpf_spin_unlock_bh(&msk->pm.lock);
mptcp_subflow_shutdown(sk, ssk, RCV_SHUTDOWN | SEND_SHUTDOWN);
mptcp_close_ssk(sk, ssk, bpf_mptcp_subflow_ctx(ssk));
subflow = bpf_mptcp_subflow_ctx(ssk);
if (subflow)
mptcp_close_ssk(sk, ssk, subflow);
BPF_MPTCP_INC_STATS(sk, MPTCP_MIB_RMSUBFLOW);

bpf_printk("mptcp_pm_subflow_destroy done");
Expand Down

0 comments on commit 7d29165

Please sign in to comment.