Skip to content

Commit 8f51dfc

Browse files
fomichevborkmann
authored andcommitted
bpf: support cloning sk storage on accept()
Add new helper bpf_sk_storage_clone which optionally clones sk storage and call it from sk_clone_lock. Cc: Martin KaFai Lau <kafai@fb.com> Cc: Yonghong Song <yhs@fb.com> Acked-by: Martin KaFai Lau <kafai@fb.com> Acked-by: Yonghong Song <yhs@fb.com> Signed-off-by: Stanislav Fomichev <sdf@google.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent b0e4701 commit 8f51dfc

4 files changed

Lines changed: 120 additions & 6 deletions

File tree

‎include/net/bpf_sk_storage.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,14 @@ void bpf_sk_storage_free(struct sock *sk);
1010
extern const struct bpf_func_proto bpf_sk_storage_get_proto;
1111
extern const struct bpf_func_proto bpf_sk_storage_delete_proto;
1212

13+
#ifdef CONFIG_BPF_SYSCALL
14+
int bpf_sk_storage_clone(const struct sock *sk, struct sock *newsk);
15+
#else
16+
static inline int bpf_sk_storage_clone(const struct sock *sk,
17+
struct sock *newsk)
18+
{
19+
return 0;
20+
}
21+
#endif
22+
1323
#endif /* _BPF_SK_STORAGE_H */

‎include/uapi/linux/bpf.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,9 @@ enum bpf_attach_type {
337337
#define BPF_F_RDONLY_PROG (1U << 7)
338338
#define BPF_F_WRONLY_PROG (1U << 8)
339339

340+
/* Clone map from listener for newly accepted socket */
341+
#define BPF_F_CLONE (1U << 9)
342+
340343
/* flags for BPF_PROG_QUERY */
341344
#define BPF_F_QUERY_EFFECTIVE (1U << 0)
342345

‎net/core/bpf_sk_storage.c‎

Lines changed: 101 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
static atomic_t cache_idx;
1414

15+
#define SK_STORAGE_CREATE_FLAG_MASK \
16+
(BPF_F_NO_PREALLOC | BPF_F_CLONE)
17+
1518
struct bucket {
1619
struct hlist_head list;
1720
raw_spinlock_t lock;
@@ -209,7 +212,6 @@ static void selem_unlink_sk(struct bpf_sk_storage_elem *selem)
209212
kfree_rcu(sk_storage, rcu);
210213
}
211214

212-
/* sk_storage->lock must be held and sk_storage->list cannot be empty */
213215
static void __selem_link_sk(struct bpf_sk_storage *sk_storage,
214216
struct bpf_sk_storage_elem *selem)
215217
{
@@ -509,7 +511,7 @@ static int sk_storage_delete(struct sock *sk, struct bpf_map *map)
509511
return 0;
510512
}
511513

512-
/* Called by __sk_destruct() */
514+
/* Called by __sk_destruct() & bpf_sk_storage_clone() */
513515
void bpf_sk_storage_free(struct sock *sk)
514516
{
515517
struct bpf_sk_storage_elem *selem;
@@ -557,6 +559,11 @@ static void bpf_sk_storage_map_free(struct bpf_map *map)
557559

558560
smap = (struct bpf_sk_storage_map *)map;
559561

562+
/* Note that this map might be concurrently cloned from
563+
* bpf_sk_storage_clone. Wait for any existing bpf_sk_storage_clone
564+
* RCU read section to finish before proceeding. New RCU
565+
* read sections should be prevented via bpf_map_inc_not_zero.
566+
*/
560567
synchronize_rcu();
561568

562569
/* bpf prog and the userspace can no longer access this map
@@ -601,7 +608,9 @@ static void bpf_sk_storage_map_free(struct bpf_map *map)
601608

602609
static int bpf_sk_storage_map_alloc_check(union bpf_attr *attr)
603610
{
604-
if (attr->map_flags != BPF_F_NO_PREALLOC || attr->max_entries ||
611+
if (attr->map_flags & ~SK_STORAGE_CREATE_FLAG_MASK ||
612+
!(attr->map_flags & BPF_F_NO_PREALLOC) ||
613+
attr->max_entries ||
605614
attr->key_size != sizeof(int) || !attr->value_size ||
606615
/* Enforce BTF for userspace sk dumping */
607616
!attr->btf_key_type_id || !attr->btf_value_type_id)
@@ -739,6 +748,95 @@ static int bpf_fd_sk_storage_delete_elem(struct bpf_map *map, void *key)
739748
return err;
740749
}
741750

751+
static struct bpf_sk_storage_elem *
752+
bpf_sk_storage_clone_elem(struct sock *newsk,
753+
struct bpf_sk_storage_map *smap,
754+
struct bpf_sk_storage_elem *selem)
755+
{
756+
struct bpf_sk_storage_elem *copy_selem;
757+
758+
copy_selem = selem_alloc(smap, newsk, NULL, true);
759+
if (!copy_selem)
760+
return NULL;
761+
762+
if (map_value_has_spin_lock(&smap->map))
763+
copy_map_value_locked(&smap->map, SDATA(copy_selem)->data,
764+
SDATA(selem)->data, true);
765+
else
766+
copy_map_value(&smap->map, SDATA(copy_selem)->data,
767+
SDATA(selem)->data);
768+
769+
return copy_selem;
770+
}
771+
772+
int bpf_sk_storage_clone(const struct sock *sk, struct sock *newsk)
773+
{
774+
struct bpf_sk_storage *new_sk_storage = NULL;
775+
struct bpf_sk_storage *sk_storage;
776+
struct bpf_sk_storage_elem *selem;
777+
int ret = 0;
778+
779+
RCU_INIT_POINTER(newsk->sk_bpf_storage, NULL);
780+
781+
rcu_read_lock();
782+
sk_storage = rcu_dereference(sk->sk_bpf_storage);
783+
784+
if (!sk_storage || hlist_empty(&sk_storage->list))
785+
goto out;
786+
787+
hlist_for_each_entry_rcu(selem, &sk_storage->list, snode) {
788+
struct bpf_sk_storage_elem *copy_selem;
789+
struct bpf_sk_storage_map *smap;
790+
struct bpf_map *map;
791+
792+
smap = rcu_dereference(SDATA(selem)->smap);
793+
if (!(smap->map.map_flags & BPF_F_CLONE))
794+
continue;
795+
796+
/* Note that for lockless listeners adding new element
797+
* here can race with cleanup in bpf_sk_storage_map_free.
798+
* Try to grab map refcnt to make sure that it's still
799+
* alive and prevent concurrent removal.
800+
*/
801+
map = bpf_map_inc_not_zero(&smap->map, false);
802+
if (IS_ERR(map))
803+
continue;
804+
805+
copy_selem = bpf_sk_storage_clone_elem(newsk, smap, selem);
806+
if (!copy_selem) {
807+
ret = -ENOMEM;
808+
bpf_map_put(map);
809+
goto out;
810+
}
811+
812+
if (new_sk_storage) {
813+
selem_link_map(smap, copy_selem);
814+
__selem_link_sk(new_sk_storage, copy_selem);
815+
} else {
816+
ret = sk_storage_alloc(newsk, smap, copy_selem);
817+
if (ret) {
818+
kfree(copy_selem);
819+
atomic_sub(smap->elem_size,
820+
&newsk->sk_omem_alloc);
821+
bpf_map_put(map);
822+
goto out;
823+
}
824+
825+
new_sk_storage = rcu_dereference(copy_selem->sk_storage);
826+
}
827+
bpf_map_put(map);
828+
}
829+
830+
out:
831+
rcu_read_unlock();
832+
833+
/* In case of an error, don't free anything explicitly here, the
834+
* caller is responsible to call bpf_sk_storage_free.
835+
*/
836+
837+
return ret;
838+
}
839+
742840
BPF_CALL_4(bpf_sk_storage_get, struct bpf_map *, map, struct sock *, sk,
743841
void *, value, u64, flags)
744842
{

‎net/core/sock.c‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,9 +1851,12 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)
18511851
goto out;
18521852
}
18531853
RCU_INIT_POINTER(newsk->sk_reuseport_cb, NULL);
1854-
#ifdef CONFIG_BPF_SYSCALL
1855-
RCU_INIT_POINTER(newsk->sk_bpf_storage, NULL);
1856-
#endif
1854+
1855+
if (bpf_sk_storage_clone(sk, newsk)) {
1856+
sk_free_unlock_clone(newsk);
1857+
newsk = NULL;
1858+
goto out;
1859+
}
18571860

18581861
newsk->sk_err = 0;
18591862
newsk->sk_err_soft = 0;

0 commit comments

Comments
 (0)