netfilter: conntrack: removal prealloc support
Subsystems (2)
Florian Westphal
maintainerv2 cover netfilter: conntrack: removal prealloc support
The prealloc flag is only used for the nat extension (and that extension is not even needed anymore in some cases).
This removes prealloc, marks all extension structs as const and then also simplifies the initial extension allocation.
Currently (default settings), each conntrack will use 64 bytes of the initial extension allocation.
include/net/netfilter/nf_conntrack_extend.h | 10 -- net/netfilter/nf_conntrack_acct.c | 2 net/netfilter/nf_conntrack_ecache.c | 2 net/netfilter/nf_conntrack_extend.c | 98 +++++----------------------- net/netfilter/nf_conntrack_helper.c | 2 net/netfilter/nf_conntrack_labels.c | 2 net/netfilter/nf_conntrack_seqadj.c | 2 net/netfilter/nf_conntrack_timeout.c | 2 net/netfilter/nf_conntrack_timestamp.c | 2 net/netfilter/nf_nat_core.c | 1 10 files changed, 27 insertions(+), 96 deletions(-)
Florian Westphal
maintainerPATCH v2 1/3 netfilter: conntrack: remove prealloc support
It was used by the nat extension, but since commit 7c9664351980 ("netfilter: move nat hlist_head to nf_conn") its only needed for connections that use MASQUERADE target or a nat helper.
Also it seems a lot easier to preallocate a fixed size instead.
With default settings, conntrack first adds ecache extension (sysctl defaults to 1), so we get 40(ct extension header) + 24 (ecache) == 64 byte on x86_64 for initial allocation.
Followup patches can constify the extension structs and avoid the initial zeroing of the entire extension area.
---
No changes since v1.
include/net/netfilter/nf_conntrack_extend.h | 6 ---- net/netfilter/nf_conntrack_extend.c | 49 +++-------------------------- net/netfilter/nf_nat_core.c | 1 - 3 files changed, 4 insertions(+), 52 deletions(-)
include/net/netfilter/nf_conntrack_extend.h +0 -6
@@ -88,21 +88,15 @@ static inline void nf_ct_ext_free(struct nf_conn *ct)/* Add this type, returns pointer to data or NULL. */ void *nf_ct_ext_add (struct nf_conn *ct,enum nf_ct_ext_id id,gfp_t gfp); -#define NF_CT_EXT_F_PREALLOC 0x0001 -struct nf_ct_ext_type {/* Destroys relationships (can be NULL). */ void (*destroy)(struct nf_conn *ct);enum nf_ct_ext_id id; -unsigned int flags; -/* Length and min alignment. */ u8 len;u8 align; -/* initial size of nf_ct_ext. */ -u8 alloc_size; };int nf_ct_extend_register (struct nf_ct_ext_type *type);
net/netfilter/nf_conntrack_extend.c +4 -45
89 lines not on this page · raw message
net/netfilter/nf_nat_core.c +0 -1
@@ -717,7 +717,6 @@ static struct nf_ct_ext_type nat_extend __read_mostly = { .align = __alignof__(struct nf_conn_nat), .destroy = nf_nat_cleanup_conntrack, .id = NF_CT_EXT_NAT, - .flags = NF_CT_EXT_F_PREALLOC, };#if IS_ENABLED (CONFIG_NF_CT_NETLINK )
Signature · 1 line
2.10.2
Florian Westphal
maintainerPATCH v2 2/3 netfilter: conntrack: mark extension structs as const
In reply to Florian Westphal---
No changes since v1.
include/net/netfilter/nf_conntrack_extend.h | 4 ++-- net/netfilter/nf_conntrack_acct.c | 2 +- net/netfilter/nf_conntrack_ecache.c | 2 +- net/netfilter/nf_conntrack_extend.c | 4 ++-- net/netfilter/nf_conntrack_helper.c | 2 +- net/netfilter/nf_conntrack_labels.c | 2 +- net/netfilter/nf_conntrack_seqadj.c | 2 +- net/netfilter/nf_conntrack_timeout.c | 2 +- net/netfilter/nf_conntrack_timestamp.c | 2 +- 9 files changed, 11 insertions(+), 11 deletions(-)
include/net/netfilter/nf_conntrack_extend.h +2 -2
@@ -99,6 +99,6 @@ struct nf_ct_ext_type { u8 align; }; -int nf_ct_extend_register (struct nf_ct_ext_type *type); -void nf_ct_extend_unregister (struct nf_ct_ext_type *type); +int nf_ct_extend_register (const struct nf_ct_ext_type *type); +void nf_ct_extend_unregister (const struct nf_ct_ext_type *type);#endif /* _NF_CONNTRACK_EXTEND_H */
net/netfilter/nf_conntrack_acct.c +1 -1
@@ -55,7 +55,7 @@ seq_print_acct(struct seq_file *s, const struct nf_conn *ct, int dir) };EXPORT_SYMBOL_GPL (seq_print_acct); -static struct nf_ct_ext_type acct_extend__read_mostly = { +static const struct nf_ct_ext_type acct_extend = { .len =sizeof (struct nf_conn_acct ), .align = __alignof__(struct nf_conn_acct ), .id =NF_CT_EXT_ACCT ,
net/netfilter/nf_conntrack_ecache.c +1 -1
@@ -347,7 +347,7 @@ static struct ctl_table event_sysctl_table[] = { };#endif /* CONFIG_SYSCTL */ -static struct nf_ct_ext_type event_extend__read_mostly = { +static const struct nf_ct_ext_type event_extend = { .len =sizeof (struct nf_conntrack_ecache ), .align = __alignof__(struct nf_conntrack_ecache ), .id =NF_CT_EXT_ECACHE ,
net/netfilter/nf_conntrack_extend.c +2 -2
@@ -116,7 +116,7 @@ void *nf_ct_ext_add(struct nf_conn *ct, enum nf_ct_ext_id id, gfp_t gfp)EXPORT_SYMBOL (nf_ct_ext_add);/* This MUST be called in process context. */ -int nf_ct_extend_register (struct nf_ct_ext_type *type) +int nf_ct_extend_register (const struct nf_ct_ext_type *type) {int ret =0 ;
@@ -134,7 +134,7 @@ int nf_ct_extend_register(struct nf_ct_ext_type *type)EXPORT_SYMBOL_GPL (nf_ct_extend_register);/* This MUST be called in process context. */ -void nf_ct_extend_unregister (struct nf_ct_ext_type *type) +void nf_ct_extend_unregister (const struct nf_ct_ext_type *type) {mutex_lock (&nf_ct_ext_type_mutex);RCU_INIT_POINTER (nf_ct_ext_types[type->id],NULL );
net/netfilter/nf_conntrack_helper.c +1 -1
@@ -535,7 +535,7 @@ void nf_conntrack_helpers_unregister(struct nf_conntrack_helper *helper, }EXPORT_SYMBOL_GPL (nf_conntrack_helpers_unregister); -static struct nf_ct_ext_type helper_extend__read_mostly = { +static const struct nf_ct_ext_type helper_extend = { .len =sizeof (struct nf_conn_help ), .align = __alignof__(struct nf_conn_help ), .id =NF_CT_EXT_HELPER ,
net/netfilter/nf_conntrack_labels.c +1 -1
@@ -82,7 +82,7 @@ void nf_connlabels_put(struct net *net) }EXPORT_SYMBOL_GPL (nf_connlabels_put); -static struct nf_ct_ext_type labels_extend__read_mostly = { +static const struct nf_ct_ext_type labels_extend = { .len =sizeof (struct nf_conn_labels ), .align = __alignof__(struct nf_conn_labels ), .id =NF_CT_EXT_LABELS ,
net/netfilter/nf_conntrack_seqadj.c +1 -1
@@ -231,7 +231,7 @@ s32 nf_ct_seq_offset(const struct nf_conn *ct, }EXPORT_SYMBOL_GPL (nf_ct_seq_offset); -static struct nf_ct_ext_type nf_ct_seqadj_extend__read_mostly = { +static const struct nf_ct_ext_type nf_ct_seqadj_extend = { .len =sizeof (struct nf_conn_seqadj ), .align = __alignof__(struct nf_conn_seqadj ), .id =NF_CT_EXT_SEQADJ ,
net/netfilter/nf_conntrack_timeout.c +1 -1
@@ -31,7 +31,7 @@ EXPORT_SYMBOL_GPL(nf_ct_timeout_find_get_hook);void (*nf_ct_timeout_put_hook)(struct ctnl_timeout *timeout)__read_mostly ;EXPORT_SYMBOL_GPL (nf_ct_timeout_put_hook); -static struct nf_ct_ext_type timeout_extend__read_mostly = { +static const struct nf_ct_ext_type timeout_extend = { .len =sizeof (struct nf_conn_timeout ), .align = __alignof__(struct nf_conn_timeout ), .id =NF_CT_EXT_TIMEOUT ,
net/netfilter/nf_conntrack_timestamp.c +1 -1
@@ -33,7 +33,7 @@ static struct ctl_table tstamp_sysctl_table[] = { };#endif /* CONFIG_SYSCTL */ -static struct nf_ct_ext_type tstamp_extend__read_mostly = { +static const struct nf_ct_ext_type tstamp_extend = { .len =sizeof (struct nf_conn_tstamp ), .align = __alignof__(struct nf_conn_tstamp ), .id =NF_CT_EXT_TSTAMP ,
Signature · 1 line
2.10.2
Florian Westphal
maintainerPATCH v2 3/3 netfilter: conntrack: handle initial extension alloc via krealloc
In reply to Florian Westphalkrealloc(NULL, ..) is same as kmalloc(), so we can avoid special-casing the initial allocation after the prealloc removal (we had to use ->alloc_len as the initial allocation size).
This also means we do not zero the preallocated memory anymore; only offsets[]. Existing code makes sure the new (used) extension space gets zeroed out.
---
changes since v1:
- don't use RCU_INIT_POINTER().
real fix would be to add '__rcu' annotation to nf_conn->ext, but that seems excessive given we'd like to get rid of rcu for nf_ct_ext...
net/netfilter/nf_conntrack_extend.c | 51 +++++++++++-------------------------- 1 file changed, 15 insertions(+), 36 deletions(-)
net/netfilter/nf_conntrack_extend.c +15 -36
@@ -44,49 +44,24 @@ void __nf_ct_ext_destroy(struct nf_conn *ct) }EXPORT_SYMBOL (__nf_ct_ext_destroy); -static void * -nf_ct_ext_create (struct nf_ct_ext **ext,enum nf_ct_ext_id id,gfp_t gfp) -{ -unsigned int off, len, alloc; -struct nf_ct_ext_type *t; - -rcu_read_lock (); - t =rcu_dereference (nf_ct_ext_types[id]); -if (!t) { -rcu_read_unlock (); -return NULL ; - } - - off =ALIGN (sizeof (struct nf_ct_ext ), t->align); - len = off + t->len; -rcu_read_unlock (); - - alloc =max (len,NF_CT_EXT_PREALLOC ); - *ext =kzalloc (alloc, gfp); -if (!*ext) -return NULL ; - - (*ext)->offset[id] = off; - (*ext)->len = len; - -return (void *)(*ext) + off; -} -void *nf_ct_ext_add (struct nf_conn *ct,enum nf_ct_ext_id id,gfp_t gfp) { +unsigned int newlen, newoff, oldlen, alloc;struct nf_ct_ext *old, *new; -int newlen, newoff;struct nf_ct_ext_type *t;/* Conntrack must not be confirmed to avoid races on reallocation. */ NF_CT_ASSERT (!nf_ct_is_confirmed (ct)); old = ct->ext; -if (!old) -return nf_ct_ext_create (&ct->ext, id, gfp); -if (__nf_ct_ext_exist (old, id)) -return NULL ; +if (old) { +if (__nf_ct_ext_exist (old, id)) +return NULL ; + oldlen = old->len; + }else { + oldlen =sizeof (*new); + }rcu_read_lock (); t =rcu_dereference (nf_ct_ext_types[id]);
@@ -95,15 +70,19 @@ void *nf_ct_ext_add(struct nf_conn *ct, enum nf_ct_ext_id id, gfp_t gfp)return NULL ; } - newoff =ALIGN (old->len, t->align); + newoff =ALIGN (oldlen, t->align); newlen = newoff + t->len;rcu_read_unlock (); - new =__krealloc (old, newlen, gfp); + alloc =max (newlen,NF_CT_EXT_PREALLOC ); + new =__krealloc (old, alloc, gfp);if (!new)return NULL ; -if (new != old) { +if (!old) { +memset (new->offset,0 ,sizeof (new->offset)); + ct->ext = new; + }else if (new != old) {kfree_rcu (old, rcu);rcu_assign_pointer (ct->ext, new); }
Signature · 1 line
2.10.2
Pablo Neira Ayuso
maintainerQuoted 8 lines · Florian Westphal
The prealloc flag is only used for the nat extension (and that extension is not even needed anymore in some cases).
This removes prealloc, marks all extension structs as const and then also simplifies the initial extension allocation.
Currently (default settings), each conntrack will use 64 bytes of the initial extension allocation.
Series applied, thanks.