Skip to content

Commit fbc907f

Browse files
Daniel Borkmanndavem330
authored andcommitted
net: filter: move filter accounting to filter core
This patch basically does two things, i) removes the extern keyword from the include/linux/filter.h file to be more consistent with the rest of Joe's changes, and ii) moves filter accounting into the filter core framework. Filter accounting mainly done through sk_filter_{un,}charge() take care of the case when sockets are being cloned through sk_clone_lock() so that removal of the filter on one socket won't result in eviction as it's still referenced by the other. These functions actually belong to net/core/filter.c and not include/net/sock.h as we want to keep all that in a central place. It's also not in fast-path so uninlining them is fine and even allows us to get rd of sk_filter_release_rcu()'s EXPORT_SYMBOL and a forward declaration. Joint work with Alexei Starovoitov. Signed-off-by: Daniel Borkmann <dborkman@redhat.com> Signed-off-by: Alexei Starovoitov <ast@plumgrid.com> Cc: Pavel Emelyanov <xemul@parallels.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent a3ea269 commit fbc907f

3 files changed

Lines changed: 42 additions & 42 deletions

File tree

‎include/linux/filter.h‎

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,28 +50,32 @@ static inline unsigned int sk_filter_size(unsigned int proglen)
5050
#define sk_filter_proglen(fprog) \
5151
(fprog->len * sizeof(fprog->filter[0]))
5252

53-
extern int sk_filter(struct sock *sk, struct sk_buff *skb);
54-
extern unsigned int sk_run_filter(const struct sk_buff *skb,
55-
const struct sock_filter *filter);
53+
int sk_filter(struct sock *sk, struct sk_buff *skb);
54+
unsigned int sk_run_filter(const struct sk_buff *skb,
55+
const struct sock_filter *filter);
5656

57-
extern int sk_unattached_filter_create(struct sk_filter **pfp,
58-
struct sock_fprog *fprog);
59-
extern void sk_unattached_filter_destroy(struct sk_filter *fp);
57+
int sk_unattached_filter_create(struct sk_filter **pfp,
58+
struct sock_fprog *fprog);
59+
void sk_unattached_filter_destroy(struct sk_filter *fp);
6060

61-
extern int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk);
62-
extern int sk_detach_filter(struct sock *sk);
61+
int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk);
62+
int sk_detach_filter(struct sock *sk);
6363

64-
extern int sk_chk_filter(struct sock_filter *filter, unsigned int flen);
65-
extern int sk_get_filter(struct sock *sk, struct sock_filter __user *filter, unsigned len);
66-
extern void sk_decode_filter(struct sock_filter *filt, struct sock_filter *to);
64+
int sk_chk_filter(struct sock_filter *filter, unsigned int flen);
65+
int sk_get_filter(struct sock *sk, struct sock_filter __user *filter,
66+
unsigned int len);
67+
void sk_decode_filter(struct sock_filter *filt, struct sock_filter *to);
68+
69+
void sk_filter_charge(struct sock *sk, struct sk_filter *fp);
70+
void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp);
6771

6872
#ifdef CONFIG_BPF_JIT
6973
#include <stdarg.h>
7074
#include <linux/linkage.h>
7175
#include <linux/printk.h>
7276

73-
extern void bpf_jit_compile(struct sk_filter *fp);
74-
extern void bpf_jit_free(struct sk_filter *fp);
77+
void bpf_jit_compile(struct sk_filter *fp);
78+
void bpf_jit_free(struct sk_filter *fp);
7579

7680
static inline void bpf_jit_dump(unsigned int flen, unsigned int proglen,
7781
u32 pass, void *image)

‎include/net/sock.h‎

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,33 +1621,6 @@ void sk_common_release(struct sock *sk);
16211621
/* Initialise core socket variables */
16221622
void sock_init_data(struct socket *sock, struct sock *sk);
16231623

1624-
void sk_filter_release_rcu(struct rcu_head *rcu);
1625-
1626-
/**
1627-
* sk_filter_release - release a socket filter
1628-
* @fp: filter to remove
1629-
*
1630-
* Remove a filter from a socket and release its resources.
1631-
*/
1632-
1633-
static inline void sk_filter_release(struct sk_filter *fp)
1634-
{
1635-
if (atomic_dec_and_test(&fp->refcnt))
1636-
call_rcu(&fp->rcu, sk_filter_release_rcu);
1637-
}
1638-
1639-
static inline void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
1640-
{
1641-
atomic_sub(sk_filter_size(fp->len), &sk->sk_omem_alloc);
1642-
sk_filter_release(fp);
1643-
}
1644-
1645-
static inline void sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1646-
{
1647-
atomic_inc(&fp->refcnt);
1648-
atomic_add(sk_filter_size(fp->len), &sk->sk_omem_alloc);
1649-
}
1650-
16511624
/*
16521625
* Socket reference counting postulates.
16531626
*

‎net/core/filter.c‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -664,14 +664,37 @@ static void sk_release_orig_filter(struct sk_filter *fp)
664664
* sk_filter_release_rcu - Release a socket filter by rcu_head
665665
* @rcu: rcu_head that contains the sk_filter to free
666666
*/
667-
void sk_filter_release_rcu(struct rcu_head *rcu)
667+
static void sk_filter_release_rcu(struct rcu_head *rcu)
668668
{
669669
struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
670670

671671
sk_release_orig_filter(fp);
672672
bpf_jit_free(fp);
673673
}
674-
EXPORT_SYMBOL(sk_filter_release_rcu);
674+
675+
/**
676+
* sk_filter_release - release a socket filter
677+
* @fp: filter to remove
678+
*
679+
* Remove a filter from a socket and release its resources.
680+
*/
681+
static void sk_filter_release(struct sk_filter *fp)
682+
{
683+
if (atomic_dec_and_test(&fp->refcnt))
684+
call_rcu(&fp->rcu, sk_filter_release_rcu);
685+
}
686+
687+
void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
688+
{
689+
atomic_sub(sk_filter_size(fp->len), &sk->sk_omem_alloc);
690+
sk_filter_release(fp);
691+
}
692+
693+
void sk_filter_charge(struct sock *sk, struct sk_filter *fp)
694+
{
695+
atomic_inc(&fp->refcnt);
696+
atomic_add(sk_filter_size(fp->len), &sk->sk_omem_alloc);
697+
}
675698

676699
static int __sk_prepare_filter(struct sk_filter *fp)
677700
{

0 commit comments

Comments
 (0)