net/netfilter: minor refactoring of conntrack ecache
Subsystems (2)
cover net/netfilter: minor refactoring of conntrack ecache
Just saw some issues that better to refactor and save a bit of CPU cycles while looking around conntracks. So, here they are.
Patches based on net-next tree.
Tony Zelenoff (3):
net/netfilter: whitespace removed net/netfilter: refactor notifier registration net/netfilter: refactor nf_ct_deliver_cached_events
net/netfilter/nf_conntrack_ecache.c | 81 +++++++++++++++++------------------ 1 files changed, 39 insertions(+), 42 deletions(-)
PATCH 1/3 net/netfilter: whitespace removed
--- net/netfilter/nf_conntrack_ecache.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
net/netfilter/nf_conntrack_ecache.c +1 -1
@@ -70,7 +70,7 @@ void nf_ct_deliver_cached_events(struct nf_conn *ct)else e->missed &= ~missed;spin_unlock_bh (&ct->lock); - } + } } out_unlock:
Signature · 1 line
1.7.1
Re: [PATCH 1/3] net/netfilter: whitespace removed
Quoted 1 line · Tony Zelenoff
Signed-off-by Tony Zelenoff <[email protected]>
Applied, thanks.
PATCH 2/3 net/netfilter: refactor notifier registration
In reply to Tony Zelenoff* ret variable initialization removed as useless
* Similar code strings concatenated and functions code flow became more plain
--- net/netfilter/nf_conntrack_ecache.c | 26 ++++++++++---------------- 1 files changed, 10 insertions(+), 16 deletions(-)
net/netfilter/nf_conntrack_ecache.c +10 -16
@@ -81,21 +81,18 @@ EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events);int nf_conntrack_register_notifier (struct net *net,struct nf_ct_event_notifier *new) { -int ret =0 ; +int ret;struct nf_ct_event_notifier *notify;mutex_lock (&nf_ct_ecache_mutex); notify =rcu_dereference_protected (net->ct.nf_conntrack_event_cb,lockdep_is_held (&nf_ct_ecache_mutex)); -if (notify !=NULL ) { +if (likely (!notify)) { +rcu_assign_pointer (net->ct.nf_conntrack_event_cb, new); + ret =0 ; + }else ret = -EBUSY ; - goto out_unlock; - } -rcu_assign_pointer (net->ct.nf_conntrack_event_cb, new); -mutex_unlock (&nf_ct_ecache_mutex); -return ret; -out_unlock:mutex_unlock (&nf_ct_ecache_mutex);return ret; }
@@ -118,21 +115,18 @@ EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier);int nf_ct_expect_register_notifier (struct net *net,struct nf_exp_event_notifier *new) { -int ret =0 ; +int ret;struct nf_exp_event_notifier *notify;mutex_lock (&nf_ct_ecache_mutex); notify =rcu_dereference_protected (net->ct.nf_expect_event_cb,lockdep_is_held (&nf_ct_ecache_mutex)); -if (notify !=NULL ) { +if (likely (!notify)) { +rcu_assign_pointer (net->ct.nf_expect_event_cb, new); + ret =0 ; + }else ret = -EBUSY ; - goto out_unlock; - } -rcu_assign_pointer (net->ct.nf_expect_event_cb, new); -mutex_unlock (&nf_ct_ecache_mutex); -return ret; -out_unlock:mutex_unlock (&nf_ct_ecache_mutex);return ret; }
Signature · 1 line
1.7.1
Re: [PATCH 2/3] net/netfilter: refactor notifier registration
Quoted 57 lines · Tony Zelenoff
* ret variable initialization removed as useless
* Similar code strings concatenated and functions code flow became more plainSigned-off-by Tony Zelenoff <[email protected]>--- net/netfilter/nf_conntrack_ecache.c | 26 ++++++++++---------------- 1 files changed, 10 insertions(+), 16 deletions(-)net/netfilter/nf_conntrack_ecache.c +10
-10@@ -81,21 +81,18 @@ EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events);int nf_conntrack_register_notifier (struct net *net,struct nf_ct_event_notifier *new) { -int ret =0 ; +int ret;struct nf_ct_event_notifier *notify;mutex_lock (&nf_ct_ecache_mutex); notify =rcu_dereference_protected (net->ct.nf_conntrack_event_cb,lockdep_is_held (&nf_ct_ecache_mutex)); -if (notify !=NULL ) { +if (likely (!notify)) { +rcu_assign_pointer (net->ct.nf_conntrack_event_cb, new); + ret =0 ; + }else ret = -EBUSY ; - goto out_unlock; - } -rcu_assign_pointer (net->ct.nf_conntrack_event_cb, new); -mutex_unlock (&nf_ct_ecache_mutex); -return ret; -out_unlock:mutex_unlock (&nf_ct_ecache_mutex);return ret; }@@ -118,21 +115,18 @@ EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier);int nf_ct_expect_register_notifier (struct net *net,struct nf_exp_event_notifier *new) { -int ret =0 ; +int ret;struct nf_exp_event_notifier *notify;mutex_lock (&nf_ct_ecache_mutex); notify =rcu_dereference_protected (net->ct.nf_expect_event_cb,lockdep_is_held (&nf_ct_ecache_mutex)); -if (notify !=NULL ) { +if (likely (!notify)) { +rcu_assign_pointer (net->ct.nf_expect_event_cb, new); + ret =0 ; + }else ret = -EBUSY ;
net/netfilter/nf_conntrack_ecache.c EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier); · line 132
Quoted 10 lines
net/netfilter/nf_conntrack_ecache.c +0
-6- goto out_unlock; - } -rcu_assign_pointer (net->ct.nf_expect_event_cb, new); -mutex_unlock (&nf_ct_ecache_mutex); -return ret; -out_unlock:mutex_unlock (&nf_ct_ecache_mutex);return ret; }
Please leave the code as is, I find it more readable.
It is standard coding practice, and permits stacking of new init code, with proper error path.
Dont add likely()/unlikely() clauses in slow path, this obfuscate code for litle gain.
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 2/3] net/netfilter: refactor notifier registration
Quoted 71 lines · Eric Dumazet
Quoted 57 lines · Tony Zelenoff
* ret variable initialization removed as useless
* Similar code strings concatenated and functions code flow became more plainSigned-off-by Tony Zelenoff<[email protected]>--- net/netfilter/nf_conntrack_ecache.c | 26 ++++++++++---------------- 1 files changed, 10 insertions(+), 16 deletions(-)net/netfilter/nf_conntrack_ecache.c +10
-10@@ -81,21 +81,18 @@ EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events);int nf_conntrack_register_notifier (struct net *net,struct nf_ct_event_notifier *new) { -int ret =0 ; +int ret;struct nf_ct_event_notifier *notify;mutex_lock (&nf_ct_ecache_mutex); notify =rcu_dereference_protected (net->ct.nf_conntrack_event_cb,lockdep_is_held (&nf_ct_ecache_mutex)); -if (notify !=NULL ) { +if (likely (!notify)) { +rcu_assign_pointer (net->ct.nf_conntrack_event_cb, new); + ret =0 ; + }else ret = -EBUSY ; - goto out_unlock; - } -rcu_assign_pointer (net->ct.nf_conntrack_event_cb, new); -mutex_unlock (&nf_ct_ecache_mutex); -return ret; -out_unlock:mutex_unlock (&nf_ct_ecache_mutex);return ret; }@@ -118,21 +115,18 @@ EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier);int nf_ct_expect_register_notifier (struct net *net,struct nf_exp_event_notifier *new) { -int ret =0 ; +int ret;struct nf_exp_event_notifier *notify;mutex_lock (&nf_ct_ecache_mutex); notify =rcu_dereference_protected (net->ct.nf_expect_event_cb,lockdep_is_held (&nf_ct_ecache_mutex)); -if (notify !=NULL ) { +if (likely (!notify)) { +rcu_assign_pointer (net->ct.nf_expect_event_cb, new); + ret =0 ; + }else ret = -EBUSY ;net/netfilter/nf_conntrack_ecache.c EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier); · line 132
Quoted 10 lines
net/netfilter/nf_conntrack_ecache.c +0
-6- goto out_unlock; - } -rcu_assign_pointer (net->ct.nf_expect_event_cb, new); -mutex_unlock (&nf_ct_ecache_mutex); -return ret; -out_unlock:mutex_unlock (&nf_ct_ecache_mutex);return ret; }Please leave the code as is, I find it more readable.
Quoted 2 lines
It is standard coding practice, and permits stacking of new init code, with proper error path.
Do not agree a bit. Of course, the code stacking and so on is good, but there is no reason to write:
rcu_assign_pointer(net->ct.nf_expect_event_cb, new); mutex_unlock(&nf_ct_ecache_mutex); return ret;
out_unlock:
mutex_unlock(&nf_ct_ecache_mutex);
return ret;
if you can do it (without breaking logic and stacking ability and as it done everywhere) this way:
rcu_assign_pointer(net->ct.nf_expect_event_cb, new);
out_unlock:
mutex_unlock(&nf_ct_ecache_mutex);
return ret;
with only one exit with proper locks freeing or deinitialization.
Ok, after that i've remove ret initialization at start and without goto this label became unused and compiler warn about it. Thus it was removed.
Quoted 3 lines
Dont add likely()/unlikely() clauses in slow path, this obfuscate code for litle gain.
ok
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Pablo Neira Ayuso
maintainerRe: [PATCH 2/3] net/netfilter: refactor notifier registration
In reply to Tony ZelenoffQuoted 18 lines · Tony Zelenoff
* ret variable initialization removed as useless
* Similar code strings concatenated and functions code flow became more plainSigned-off-by Tony Zelenoff <[email protected]>--- net/netfilter/nf_conntrack_ecache.c | 26 ++++++++++---------------- 1 files changed, 10 insertions(+), 16 deletions(-)net/netfilter/nf_conntrack_ecache.c +0
-1@@ -81,21 +81,18 @@ EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events);int nf_conntrack_register_notifier (struct net *net,struct nf_ct_event_notifier *new) { -int ret =0 ;
net/netfilter/nf_conntrack_ecache.c EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events); · line 92
Quoted 10 lines
net/netfilter/nf_conntrack_ecache.c +4
-1+int ret;struct nf_ct_event_notifier *notify;mutex_lock (&nf_ct_ecache_mutex); notify =rcu_dereference_protected (net->ct.nf_conntrack_event_cb,lockdep_is_held (&nf_ct_ecache_mutex)); -if (notify !=NULL ) { +if (likely (!notify)) { +rcu_assign_pointer (net->ct.nf_conntrack_event_cb, new); + ret =0 ;
I agree with Eric here.
1) Code readability is worst after this change.
2) More important, event notifier registration is not in the hot path, so this likely is not worth to have.
Sorry, I won't take this patch.
PATCH 3/3 net/netfilter: refactor nf_ct_deliver_cached_events
In reply to Tony Zelenoff* identation lowered
* some CPU cycles saved at delayed item variable initialization
--- net/netfilter/nf_conntrack_ecache.c | 55 ++++++++++++++++++---------------- 1 files changed, 29 insertions(+), 26 deletions(-)
net/netfilter/nf_conntrack_ecache.c +29 -26
@@ -32,9 +32,11 @@ static DEFINE_MUTEX(nf_ct_ecache_mutex);void nf_ct_deliver_cached_events (struct nf_conn *ct) {struct net *net =nf_ct_net (ct); -unsigned long events; +unsigned long events, missed;struct nf_ct_event_notifier *notify;struct nf_conntrack_ecache *e; +struct nf_ct_event item; +int ret;rcu_read_lock (); notify =rcu_dereference (net->ct.nf_conntrack_event_cb);
@@ -47,31 +49,32 @@ void nf_ct_deliver_cached_events(struct nf_conn *ct) events =xchg (&e->cache,0 ); -if (nf_ct_is_confirmed (ct) && !nf_ct_is_dying (ct) && events) { -struct nf_ct_event item = { - .ct = ct, - .pid =0 , - .report =0 - }; -int ret; -/* We make a copy of the missed event cache without taking -* the lock, thus we may send missed events twice. However, -* this does not harm and it happens very rarely. */ -unsigned long missed = e->missed; - -if (!((events | missed) & e->ctmask)) - goto out_unlock; - - ret = notify->fcn (events | missed, &item); -if (unlikely (ret <0 || missed)) { -spin_lock_bh (&ct->lock); -if (ret <0 ) - e->missed |= events; -else - e->missed &= ~missed; -spin_unlock_bh (&ct->lock); - } - } +if (!nf_ct_is_confirmed (ct) ||nf_ct_is_dying (ct) || !events) + goto out_unlock; + +/* We make a copy of the missed event cache without taking +* the lock, thus we may send missed events twice. However, +* this does not harm and it happens very rarely. */ + missed = e->missed; + +if (!((events | missed) & e->ctmask)) + goto out_unlock; + + item.ct = ct; + item.pid =0 ; + item.report =0 ; + + ret = notify->fcn (events | missed, &item); + +if (likely (ret >=0 && !missed)) + goto out_unlock; + +spin_lock_bh (&ct->lock); +if (ret <0 ) + e->missed |= events; +else + e->missed &= ~missed; +spin_unlock_bh (&ct->lock); out_unlock:rcu_read_unlock ();
Signature · 1 line
1.7.1
Re: [PATCH 3/3] net/netfilter: refactor nf_ct_deliver_cached_events
Quoted 4 lines · Tony Zelenoff
* identation lowered
* some CPU cycles saved at delayed item variable initializationSigned-off-by Tony Zelenoff <[email protected]>
Applied, thanks Tony.