netfilter: conntrack: removal prealloc support
Subsystems (2)
Florian Westphal
maintainercover 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 | 104 +++++----------------------- 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, 30 insertions(+), 99 deletions(-)
Florian Westphal
maintainerPATCH 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 32(ct extension header) + 24 (ecache) == 56 byte on x86_64 for initial allocation.
Followup patches can constify the extension structs and avoid the initial zeroing of the entire extension area.
--- 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 2/3 netfilter: conntrack: mark extension structs as const
In reply to Florian Westphal--- 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 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.
--- 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) { +RCU_INIT_POINTER (ct->ext, new); +memset (new->offset,0 ,sizeof (new->offset)); + }else if (new != old) {kfree_rcu (old, rcu);rcu_assign_pointer (ct->ext, new); }
Signature · 1 line
2.10.2
kbuild test robot
build botRe: [PATCH nf-next 3/3] netfilter: conntrack: handle initial extension alloc via krealloc
reproduce:
Hi Florian,
[auto build test WARNING on nf-next/master]
url: https://github.com/0day-ci/linux/commits/Florian-Westphal/netfilter-conntrack-removal-prealloc-support/20170420-100813 base: https://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next.git master reproduce: # apt-get install sparse make ARCH=x86_64 allmodconfig make C=1 CF=-D__CHECK_ENDIAN__
sparse warnings: (new ones prefixed by >>)
include/linux/compiler.h:264:8: sparse: attribute 'no_sanitize_address': unknown attribute
>> net/netfilter/nf_conntrack_extend.c:83:17: sparse: incompatible types in comparison expression (different address spaces)
vim +83 net/netfilter/nf_conntrack_extend.c
67 t = rcu_dereference(nf_ct_ext_types[id]);
68 if (!t) {
69 rcu_read_unlock();
70 return NULL;
71 }
72
73 newoff = ALIGN(oldlen, t->align);
74 newlen = newoff + t->len;
75 rcu_read_unlock();
76
77 alloc = max(newlen, NF_CT_EXT_PREALLOC);
78 new = __krealloc(old, alloc, gfp);
79 if (!new)
80 return NULL;
81
82 if (!old) {
> 83 RCU_INIT_POINTER(ct->ext, new);
84 memset(new->offset, 0, sizeof(new->offset));
85 } else if (new != old) {
86 kfree_rcu(old, rcu);
87 rcu_assign_pointer(ct->ext, new);
88 }
89
90 new->offset[id] = newoff;
91 new->len = newlen;---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation