Skip to content

Commit

Permalink
map 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 Feb 15, 2025
1 parent 8a027ee commit 375d2c2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
19 changes: 13 additions & 6 deletions net/mptcp/bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -649,21 +649,28 @@ __bpf_kfunc static bool bpf_ipv4_is_private_10(__be32 addr)
return ipv4_is_private_10(addr);
}

__bpf_kfunc static void bpf_list_add_tail_rcu(struct list_head *new__ign,
__bpf_kfunc static void bpf_list_add_tail_rcu(struct list_head *new,
struct list_head *head)
{
list_add_tail_rcu(new__ign, head);
list_add_tail_rcu(new, head);
}

__bpf_kfunc static void bpf_list_del_rcu(struct list_head *entry)
{
list_del_rcu(entry);
}

__bpf_kfunc static struct mptcp_pm_addr_entry *
bpf_sock_kmalloc_entry(struct sock *sk, int size, gfp_t priority)
{
return sock_kmalloc(sk, size, priority);
}

__bpf_kfunc static void
bpf_sock_kfree_s(struct sock *sk, void *mem__ign, int size)
bpf_sock_kfree_entry(struct sock *sk, struct mptcp_pm_addr_entry *entry__ign,
int size__sz)
{
sock_kfree_s(sk, mem__ign, size);
sock_kfree_s(sk, entry__ign, size__sz);
}

__bpf_kfunc static void bpf_set_bit(unsigned long nr, unsigned long *addr__ign)
Expand Down Expand Up @@ -747,8 +754,8 @@ BTF_ID_FLAGS(func, bpf_spin_unlock_bh)
BTF_ID_FLAGS(func, bpf_ipv4_is_private_10)
BTF_ID_FLAGS(func, bpf_list_add_tail_rcu)
BTF_ID_FLAGS(func, bpf_list_del_rcu)
BTF_ID_FLAGS(func, sock_kmalloc)
BTF_ID_FLAGS(func, bpf_sock_kfree_s)
BTF_ID_FLAGS(func, bpf_sock_kmalloc_entry)
BTF_ID_FLAGS(func, bpf_sock_kfree_entry)
BTF_ID_FLAGS(func, mptcp_pm_alloc_anno_list)
BTF_ID_FLAGS(func, mptcp_pm_announce_addr)
BTF_ID_FLAGS(func, mptcp_pm_nl_addr_send_ack, KF_SLEEPABLE)
Expand Down
14 changes: 9 additions & 5 deletions tools/testing/selftests/bpf/progs/mptcp_bpf_hashmap_pm.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ char _license[] SEC("license") = "GPL";

extern bool CONFIG_MPTCP_IPV6 __kconfig __weak;

struct map_value {
struct mptcp_pm_addr_entry *entry;
};

struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, MPTCP_PM_MAX_ADDR_ID);
__type(key, __u32);
__type(value, struct mptcp_pm_addr_entry *);
__type(value, struct map_value);
} mptcp_pm_addr_map SEC(".maps");

struct callback_ctx {
Expand All @@ -24,11 +28,11 @@ struct callback_ctx {
static struct mptcp_pm_addr_entry *
mptcp_pm_lookup_addr_by_id(struct mptcp_sock *msk, unsigned int id)
{
struct mptcp_pm_addr_entry **p;
struct map_value *p;

p = bpf_map_lookup_elem(&mptcp_pm_addr_map, &id);
if (p && *p)
return bpf_core_cast(*p, struct mptcp_pm_addr_entry);
if (p && p->entry)
return bpf_core_cast(p->entry, struct mptcp_pm_addr_entry);
return NULL;
}

Expand Down Expand Up @@ -119,7 +123,7 @@ static int release_callback(struct bpf_map *map, __u32 *key, void *val,
entry = mptcp_pm_lookup_addr_by_id(data->msk, *key);
if (entry) {
mptcp_pm_delete_entry(entry);
bpf_sock_kfree_s(sk, entry, sizeof(*entry));
bpf_sock_kfree_entry(sk, entry, sizeof(*entry));
}

return 0;
Expand Down
14 changes: 7 additions & 7 deletions tools/testing/selftests/bpf/progs/mptcp_bpf_pm.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@

#define inet_sk(ptr) container_of(ptr, struct inet_sock, sk)

extern void *
sock_kmalloc(struct sock *sk, int size, gfp_t priority) __ksym;
extern struct mptcp_pm_addr_entry *
bpf_sock_kmalloc_entry(struct sock *sk, int size, gfp_t priority) __ksym;
extern void
bpf_sock_kfree_s(struct sock *sk, void *mem, int size) __ksym;
bpf_sock_kfree_entry(struct sock *sk, struct mptcp_pm_addr_entry *entry,
int size) __ksym;

extern bool mptcp_pm_alloc_anno_list(struct mptcp_sock *msk,
const struct mptcp_addr_info *addr) __ksym;
Expand Down Expand Up @@ -108,13 +109,12 @@ static int mptcp_pm_append_new_local_addr(struct mptcp_sock *msk,
/* Memory for the entry is allocated from the
* sock option buffer.
*/
e = sock_kmalloc(sk, sizeof(*e), GFP_ATOMIC);
e = bpf_sock_kmalloc_entry(sk, sizeof(*e), GFP_ATOMIC);
if (!e) {
ret = -ENOMEM;
goto append_err;
}

e = bpf_core_cast(e, struct mptcp_pm_addr_entry);
mptcp_pm_copy_entry(e, entry);
mptcp_pm_add_entry(msk, e);
msk->pm.local_addr_used++;
Expand All @@ -137,7 +137,7 @@ static int mptcp_pm_delete_local_addr(struct mptcp_sock *msk,
return -EINVAL;

mptcp_pm_delete_entry(entry);
bpf_sock_kfree_s(sk, entry, sizeof(*entry));
bpf_sock_kfree_entry(sk, entry, sizeof(*entry));
msk->pm.local_addr_used--;
return 0;
}
Expand Down Expand Up @@ -210,7 +210,7 @@ static int mptcp_pm_address_removed(struct mptcp_sock *msk, u8 id)

mptcp_pm_remove_addr_entry(msk, entry);

bpf_sock_kfree_s((struct sock *)msk, entry, sizeof(*entry));
bpf_sock_kfree_entry((struct sock *)msk, entry, sizeof(*entry));

bpf_printk("2 mptcp_pm_address_removed done");

Expand Down

0 comments on commit 375d2c2

Please sign in to comment.