pull request (net-next): ipsec-next 2016-09-08
Subsystems (3)
Steffen Klassert
maintainer1) Constify the xfrm_replay structures. From Julia Lawall
2) Protect xfrm state hash tables with rcu, lookups can be done now without acquiring xfrm_state_lock. From Florian Westphal.
3) Protect xfrm policy hash tables with rcu, lookups can be done now without acquiring xfrm_policy_lock. From Florian Westphal.
4) We don't need to have a garbage collector list per namespace anymore, so use a global one instead. From Florian Westphal.
Please pull or let me know if there are problems.
Thanks!
The following changes since commit 0184165b2f42c4b032da9dd11546bfbaeb5afd4e:
ravb: add sleep PM suspend/resume support (2016-08-09 16:16:08 -0700)
are available in the git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/klassert/ipsec-next.git master
for you to fetch changes up to 35db57bbc4b7ab810bba6e6d6954a0faf5a842cf:
xfrm: state: remove per-netns gc task (2016-08-24 13:16:06 +0200)
----------------------------------------------------------------
Florian Westphal (15):
xfrm: state: use hlist_for_each_entry_rcu helper
xfrm: state: use atomic_inc_not_zero to increment refcount
xfrm: state: delay freeing until rcu grace period has elapsed
xfrm: state: add sequence count to detect hash resizes
xfrm: state: use rcu_deref and assign_pointer helpers
xfrm: state: don't use lock anymore unless acquire operation is needed
xfrm: policy: use rcu versions for iteration and list add/del
xfrm: policy: prepare policy_bydst hash for rcu lookups
xfrm: policy: add sequence count to sync with hash resize
xfrm: policy: use atomic_inc_not_zero in rcu section
xfrm: policy: make xfrm_policy_lookup_bytype lockless
xfrm: policy: only use rcu in xfrm_sk_policy_lookup
xfrm: policy: don't acquire policy lock in xfrm_spd_getinfo
xfrm: policy: convert policy_lock to spinlock
xfrm: state: remove per-netns gc task
Julia Lawall (1):
xfrm: constify xfrm_replay structures
Steffen Klassert (1):
xfrm: Fix xfrm_policy_lock imbalance
include/net/netns/xfrm.h | 12 ++-- include/net/xfrm.h | 2 +- net/xfrm/xfrm_policy.c | 145 ++++++++++++++++++++++++++++------------------- net/xfrm/xfrm_replay.c | 6 +- net/xfrm/xfrm_state.c | 121 +++++++++++++++++++++++++-------------- 5 files changed, 173 insertions(+), 113 deletions(-)
Steffen Klassert
maintainerPATCH 3/17 xfrm: state: use atomic_inc_not_zero to increment refcount
From: Florian Westphal <[email protected]>
Once xfrm_state_lookup_byaddr no longer acquires the state lock another cpu might be freeing the state entry at the same time.
To detect this we use atomic_inc_not_zero, we then signal -EAGAIN to caller in case our result was stale.
--- net/xfrm/xfrm_state.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-)
net/xfrm/xfrm_state.c +16 -5
@@ -37,6 +37,11 @@static unsigned int xfrm_state_hashmax__read_mostly =1 *1024 *1024 ; +static inline bool xfrm_state_hold_rcu (struct xfrm_state __rcu *x) +{ +return atomic_inc_not_zero (&x->refcnt); +} + static inline unsigned int xfrm_dst_hash(struct net *net, const xfrm_address_t *daddr, const xfrm_address_t *saddr,
@@ -668,7 +673,8 @@ static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,if ((mark & x->mark.m) != x->mark.v)continue ; -xfrm_state_hold (x); +if (!xfrm_state_hold_rcu (x)) +continue ;return x; }
@@ -692,7 +698,8 @@ static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,if ((mark & x->mark.m) != x->mark.v)continue ; -xfrm_state_hold (x); +if (!xfrm_state_hold_rcu (x)) +continue ;return x; }
@@ -871,10 +878,14 @@ found: } } out: -if (x) -xfrm_state_hold (x); -else +if (x) { +if (!xfrm_state_hold_rcu (x)) { + *err = -EAGAIN ; + x =NULL ; + } + }else { *err = acquire_in_progress ? -EAGAIN : error; + }spin_unlock_bh (&net->xfrm.xfrm_state_lock);if (to_put)xfrm_state_put (to_put);
Signature · 1 line
1.9.1
Steffen Klassert
maintainerPATCH 6/17 xfrm: state: use rcu_deref and assign_pointer helpers
In reply to Steffen KlassertFrom: Florian Westphal <[email protected]>
Before xfrm_state_find() can use rcu_read_lock instead of xfrm_state_lock we need to switch users of the hash table to assign/obtain the pointers with the appropriate rcu helpers.
--- net/xfrm/xfrm_state.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-)
net/xfrm/xfrm_state.c +10 -8
@@ -28,6 +28,9 @@#include "xfrm_hash.h" +#define xfrm_state_deref_prot (table, net) \ + rcu_dereference_protected((table), lockdep_is_held(&(net)->xfrm.xfrm_state_lock)) +/* Each xfrm_state may be linked to two tables: 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
@@ -131,18 +134,17 @@ static void xfrm_hash_resize(struct work_struct *work)write_seqcount_begin (&xfrm_state_hash_generation); nhashmask = (nsize /sizeof (struct hlist_head )) -1U ; + odst =xfrm_state_deref_prot (net->xfrm.state_bydst, net);for (i = net->xfrm.state_hmask; i >=0 ; i--) -xfrm_hash_transfer (net->xfrm.state_bydst+i, ndst, nsrc, nspi, - nhashmask); +xfrm_hash_transfer (odst + i, ndst, nsrc, nspi, nhashmask); - odst = net->xfrm.state_bydst; - osrc = net->xfrm.state_bysrc; - ospi = net->xfrm.state_byspi; + osrc =xfrm_state_deref_prot (net->xfrm.state_bysrc, net); + ospi =xfrm_state_deref_prot (net->xfrm.state_byspi, net); ohashmask = net->xfrm.state_hmask; - net->xfrm.state_bydst = ndst; - net->xfrm.state_bysrc = nsrc; - net->xfrm.state_byspi = nspi; +rcu_assign_pointer (net->xfrm.state_bydst, ndst); +rcu_assign_pointer (net->xfrm.state_bysrc, nsrc); +rcu_assign_pointer (net->xfrm.state_byspi, nspi); net->xfrm.state_hmask = nhashmask;write_seqcount_end (&xfrm_state_hash_generation);
Signature · 1 line
1.9.1
Steffen Klassert
maintainerPATCH 7/17 xfrm: state: don't use lock anymore unless acquire operation is needed
In reply to Steffen KlassertFrom: Florian Westphal <[email protected]>
push the lock down, after earlier patches we can rely on rcu to make sure state struct won't go away.
--- include/net/netns/xfrm.h | 6 +++--- net/xfrm/xfrm_state.c | 6 ++++-- 2 files changed, 7 insertions(+), 5 deletions(-)
include/net/netns/xfrm.h +3 -3
@@ -38,9 +38,9 @@ struct netns_xfrm {* mode. Also, it can be used by ah/esp icmp error handler to find * offending SA. */ -struct hlist_head *state_bydst; -struct hlist_head *state_bysrc; -struct hlist_head *state_byspi; +struct hlist_head __rcu *state_bydst; +struct hlist_head __rcu *state_bysrc; +struct hlist_head __rcu *state_byspi;unsigned int state_hmask;unsigned int state_num;struct work_struct state_hash_work;
net/xfrm/xfrm_state.c +4 -2
@@ -799,7 +799,7 @@ xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr, sequence =read_seqcount_begin (&xfrm_state_hash_generation); -spin_lock_bh (&net->xfrm.xfrm_state_lock); +rcu_read_lock (); h =xfrm_dst_hash (net, daddr, saddr, tmpl->reqid, encap_family);hlist_for_each_entry_rcu (x, net->xfrm.state_bydst + h, bydst) { if (x->props.family == encap_family &&
@@ -870,6 +870,7 @@ found: }if (km_query (x, tmpl, pol) ==0 ) { +spin_lock_bh (&net->xfrm.xfrm_state_lock); x->km.state =XFRM_STATE_ACQ ;list_add (&x->km.all, &net->xfrm.state_all);hlist_add_head_rcu (&x->bydst, net->xfrm.state_bydst + h);
@@ -883,6 +884,7 @@ found:tasklet_hrtimer_start (&x->mtimer,ktime_set (net->xfrm.sysctl_acq_expires,0 ),HRTIMER_MODE_REL ); net->xfrm.state_num++;xfrm_hash_grow_check (net, x->bydst.next !=NULL ); +spin_unlock_bh (&net->xfrm.xfrm_state_lock); }else { x->km.state =XFRM_STATE_DEAD ; to_put = x;
@@ -899,7 +901,7 @@ out: }else { *err = acquire_in_progress ? -EAGAIN : error; } -spin_unlock_bh (&net->xfrm.xfrm_state_lock); +rcu_read_unlock ();if (to_put)xfrm_state_put (to_put);
Signature · 1 line
1.9.1
Steffen Klassert
maintainerPATCH 17/17 xfrm: state: remove per-netns gc task
In reply to Steffen KlassertFrom: Florian Westphal <[email protected]>
After commit 5b8ef3415a21f173
("xfrm: Remove ancient sleeping when the SA is in acquire state") gc does not need any per-netns data anymore.
As far as gc is concerned all state structs are the same, so we can use a global work struct for it.
--- include/net/netns/xfrm.h | 2 -- net/xfrm/xfrm_state.c | 18 +++++++++--------- 2 files changed, 9 insertions(+), 11 deletions(-)
include/net/netns/xfrm.h +0 -2
@@ -44,8 +44,6 @@ struct netns_xfrm {unsigned int state_hmask;unsigned int state_num;struct work_struct state_hash_work; -struct hlist_head state_gc_list; -struct work_struct state_gc_work;struct list_head policy_all;struct hlist_head *policy_byidx;
net/xfrm/xfrm_state.c +9 -9
@@ -31,6 +31,8 @@#define xfrm_state_deref_prot (table, net) \ rcu_dereference_protected((table), lockdep_is_held(&(net)->xfrm.xfrm_state_lock)) +static void xfrm_state_gc_task (struct work_struct *work); +/* Each xfrm_state may be linked to two tables: 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
@@ -41,6 +43,9 @@static unsigned int xfrm_state_hashmax__read_mostly =1 *1024 *1024 ;static __read_mostly seqcount_t xfrm_state_hash_generation =SEQCNT_ZERO (xfrm_state_hash_generation); +static DECLARE_WORK(xfrm_state_gc_work, xfrm_state_gc_task); +static HLIST_HEAD (xfrm_state_gc_list); +static inline bool xfrm_state_hold_rcu (struct xfrm_state __rcu *x) {return atomic_inc_not_zero (&x->refcnt);
@@ -368,13 +373,12 @@ static void xfrm_state_gc_destroy(struct xfrm_state *x)static void xfrm_state_gc_task (struct work_struct *work) { - struct net *net = container_of(work, struct net, xfrm.state_gc_work);struct xfrm_state *x;struct hlist_node *tmp;struct hlist_head gc_list;spin_lock_bh (&xfrm_state_gc_lock); -hlist_move_list (&net->xfrm.state_gc_list, &gc_list); +hlist_move_list (&xfrm_state_gc_list, &gc_list);spin_unlock_bh (&xfrm_state_gc_lock);synchronize_rcu ();
@@ -515,14 +519,12 @@ EXPORT_SYMBOL(xfrm_state_alloc);void __xfrm_state_destroy (struct xfrm_state *x) { -struct net *net =xs_net (x); -WARN_ON (x->km.state !=XFRM_STATE_DEAD );spin_lock_bh (&xfrm_state_gc_lock); -hlist_add_head (&x->gclist, &net->xfrm.state_gc_list); +hlist_add_head (&x->gclist, &xfrm_state_gc_list);spin_unlock_bh (&xfrm_state_gc_lock); -schedule_work (&net->xfrm.state_gc_work); +schedule_work (&xfrm_state_gc_work); }EXPORT_SYMBOL (__xfrm_state_destroy);
@@ -2134,8 +2136,6 @@ int __net_init xfrm_state_init(struct net *net) net->xfrm.state_num =0 ;INIT_WORK (&net->xfrm.state_hash_work, xfrm_hash_resize); -INIT_HLIST_HEAD (&net->xfrm.state_gc_list); -INIT_WORK (&net->xfrm.state_gc_work, xfrm_state_gc_task);spin_lock_init (&net->xfrm.xfrm_state_lock);return 0 ;
@@ -2153,7 +2153,7 @@ void xfrm_state_fini(struct net *net)flush_work (&net->xfrm.state_hash_work);xfrm_state_flush (net,IPSEC_PROTO_ANY , false); -flush_work (&net->xfrm.state_gc_work); +flush_work (&xfrm_state_gc_work);WARN_ON (!list_empty (&net->xfrm.state_all));
Signature · 1 line
1.9.1
Steffen Klassert
maintainerPATCH 4/17 xfrm: state: delay freeing until rcu grace period has elapsed
In reply to Steffen KlassertFrom: Florian Westphal <[email protected]>
The hash table backend memory and the state structs are free'd via kfree/vfree.
Once we only rely on rcu during lookups we have to make sure no other cpu is currently accessing this before doing the free.
Free operations already happen from worker so we can use synchronize_rcu to wait until concurrent readers are done.
--- net/xfrm/xfrm_state.c | 5 +++++ 1 file changed, 5 insertions(+)
net/xfrm/xfrm_state.c +5 -0
@@ -146,6 +146,9 @@ static void xfrm_hash_resize(struct work_struct *work)spin_unlock_bh (&net->xfrm.xfrm_state_lock); osize = (ohashmask +1 ) *sizeof (struct hlist_head ); + +synchronize_rcu (); +xfrm_hash_free (odst, osize);xfrm_hash_free (osrc, osize);xfrm_hash_free (ospi, osize);
@@ -369,6 +372,8 @@ static void xfrm_state_gc_task(struct work_struct *work)hlist_move_list (&net->xfrm.state_gc_list, &gc_list);spin_unlock_bh (&xfrm_state_gc_lock); +synchronize_rcu (); +hlist_for_each_entry_safe (x, tmp, &gc_list, gclist)xfrm_state_gc_destroy (x); }
Signature · 1 line
1.9.1
Steffen Klassert
maintainerPATCH 11/17 xfrm: policy: use atomic_inc_not_zero in rcu section
In reply to Steffen KlassertFrom: Florian Westphal <[email protected]>
If we don't hold the policy lock anymore the refcnt might already be 0, i.e. policy struct is about to be free'd.
Switch to atomic_inc_not_zero to avoid this.
On removal policies are already unlinked from the tables (lists) before the last _put occurs so we are not supposed to find the same 'dead' entry on the next loop, so its safe to just repeat the lookup.
--- net/xfrm/xfrm_policy.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
net/xfrm/xfrm_policy.c +7 -1
@@ -60,6 +60,11 @@ static void __xfrm_policy_link(struct xfrm_policy *pol, int dir);static struct xfrm_policy *__xfrm_policy_unlink (struct xfrm_policy *pol,int dir); +static inline bool xfrm_pol_hold_rcu (struct xfrm_policy *policy) +{ +return atomic_inc_not_zero (&policy->refcnt); +} +static inline bool __xfrm4_selector_match (const struct xfrm_selector *sel,const struct flowi *fl) {
@@ -1164,7 +1169,8 @@ static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,if (read_seqcount_retry (&xfrm_policy_hash_generation, sequence)) goto retry; -xfrm_pol_hold (ret); +if (ret && !xfrm_pol_hold_rcu (ret)) + goto retry; fail:read_unlock_bh (&net->xfrm.xfrm_policy_lock);
Signature · 1 line
1.9.1
Steffen Klassert
maintainerPATCH 13/17 xfrm: policy: only use rcu in xfrm_sk_policy_lookup
In reply to Steffen KlassertFrom: Florian Westphal <[email protected]>
Don't acquire the readlock anymore and rely on rcu alone.
In case writer on other CPU changed policy at the wrong moment (after we obtained sk policy pointer but before we could obtain the reference) just repeat the lookup.
--- net/xfrm/xfrm_policy.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
net/xfrm/xfrm_policy.c +3 -5
@@ -1249,10 +1249,9 @@ static struct xfrm_policy *xfrm_sk_policy_lookup(const struct sock *sk, int dir, const struct flowi *fl) {struct xfrm_policy *pol; -struct net *net =sock_net (sk);rcu_read_lock (); -read_lock_bh (&net->xfrm.xfrm_policy_lock); + again: pol =rcu_dereference (sk->sk_policy[dir]);if (pol !=NULL ) { bool match = xfrm_selector_match(&pol->selector, fl,
@@ -1267,8 +1266,8 @@ static struct xfrm_policy *xfrm_sk_policy_lookup(const struct sock *sk, int dir, err =security_xfrm_policy_lookup (pol->security, fl->flowi_secid,policy_to_flow_dir (dir)); -if (!err) -xfrm_pol_hold (pol); +if (!err && !xfrm_pol_hold_rcu (pol)) + goto again;else if (err == -ESRCH ) pol =NULL ;else
@@ -1277,7 +1276,6 @@ static struct xfrm_policy *xfrm_sk_policy_lookup(const struct sock *sk, int dir, pol =NULL ; } out: -read_unlock_bh (&net->xfrm.xfrm_policy_lock);rcu_read_unlock ();return pol; }
Signature · 1 line
1.9.1
Steffen Klassert
maintainerPATCH 2/17 xfrm: state: use hlist_for_each_entry_rcu helper
In reply to Steffen KlassertFrom: Florian Westphal <[email protected]>
This is required once we allow lockless access of bydst/bysrc hash tables.
--- net/xfrm/xfrm_state.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-)
net/xfrm/xfrm_state.c +19 -19
@@ -76,18 +76,18 @@ static void xfrm_hash_transfer(struct hlist_head *list, h =__xfrm_dst_hash (&x->id.daddr, &x->props.saddr, x->props.reqid, x->props.family, nhashmask); -hlist_add_head (&x->bydst, ndsttable+h); +hlist_add_head_rcu (&x->bydst, ndsttable + h); h =__xfrm_src_hash (&x->id.daddr, &x->props.saddr, x->props.family, nhashmask); -hlist_add_head (&x->bysrc, nsrctable+h); +hlist_add_head_rcu (&x->bysrc, nsrctable + h);if (x->id.spi) { h =__xfrm_spi_hash (&x->id.daddr, x->id.spi, x->id.proto, x->props.family, nhashmask); -hlist_add_head (&x->byspi, nspitable+h); +hlist_add_head_rcu (&x->byspi, nspitable + h); } } }
@@ -520,10 +520,10 @@ int __xfrm_state_delete(struct xfrm_state *x) x->km.state =XFRM_STATE_DEAD ;spin_lock (&net->xfrm.xfrm_state_lock);list_del (&x->km.all); -hlist_del (&x->bydst); -hlist_del (&x->bysrc); +hlist_del_rcu (&x->bydst); +hlist_del_rcu (&x->bysrc);if (x->id.spi) -hlist_del (&x->byspi); +hlist_del_rcu (&x->byspi); net->xfrm.state_num--;spin_unlock (&net->xfrm.xfrm_state_lock);
@@ -659,7 +659,7 @@ static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,unsigned int h =xfrm_spi_hash (net, daddr, spi, proto, family);struct xfrm_state *x; -hlist_for_each_entry (x, net->xfrm.state_byspi+h, byspi) { +hlist_for_each_entry_rcu (x, net->xfrm.state_byspi + h, byspi) { if (x->props.family != family || x->id.spi != spi || x->id.proto != proto ||
@@ -683,7 +683,7 @@ static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,unsigned int h =xfrm_src_hash (net, daddr, saddr, family);struct xfrm_state *x; -hlist_for_each_entry (x, net->xfrm.state_bysrc+h, bysrc) { +hlist_for_each_entry_rcu (x, net->xfrm.state_bysrc + h, bysrc) { if (x->props.family != family || x->id.proto != proto || !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
@@ -781,7 +781,7 @@ xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,spin_lock_bh (&net->xfrm.xfrm_state_lock); h =xfrm_dst_hash (net, daddr, saddr, tmpl->reqid, encap_family); -hlist_for_each_entry (x, net->xfrm.state_bydst+h, bydst) { +hlist_for_each_entry_rcu (x, net->xfrm.state_bydst + h, bydst) { if (x->props.family == encap_family && x->props.reqid == tmpl->reqid && (mark & x->mark.m) == x->mark.v &&
@@ -797,7 +797,7 @@ xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr, goto found; h_wildcard =xfrm_dst_hash (net, daddr, &saddr_wildcard, tmpl->reqid, encap_family); -hlist_for_each_entry (x, net->xfrm.state_bydst+h_wildcard, bydst) { +hlist_for_each_entry_rcu (x, net->xfrm.state_bydst + h_wildcard, bydst) { if (x->props.family == encap_family && x->props.reqid == tmpl->reqid && (mark & x->mark.m) == x->mark.v &&
@@ -852,12 +852,12 @@ found:if (km_query (x, tmpl, pol) ==0 ) { x->km.state =XFRM_STATE_ACQ ;list_add (&x->km.all, &net->xfrm.state_all); -hlist_add_head (&x->bydst, net->xfrm.state_bydst+h); +hlist_add_head_rcu (&x->bydst, net->xfrm.state_bydst + h); h =xfrm_src_hash (net, daddr, saddr, encap_family); -hlist_add_head (&x->bysrc, net->xfrm.state_bysrc+h); +hlist_add_head_rcu (&x->bysrc, net->xfrm.state_bysrc + h);if (x->id.spi) { h =xfrm_spi_hash (net, &x->id.daddr, x->id.spi, x->id.proto, encap_family); -hlist_add_head (&x->byspi, net->xfrm.state_byspi+h); +hlist_add_head_rcu (&x->byspi, net->xfrm.state_byspi + h); } x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;tasklet_hrtimer_start (&x->mtimer,ktime_set (net->xfrm.sysctl_acq_expires,0 ),HRTIMER_MODE_REL );
@@ -945,16 +945,16 @@ static void __xfrm_state_insert(struct xfrm_state *x) h =xfrm_dst_hash (net, &x->id.daddr, &x->props.saddr, x->props.reqid, x->props.family); -hlist_add_head (&x->bydst, net->xfrm.state_bydst+h); +hlist_add_head_rcu (&x->bydst, net->xfrm.state_bydst + h); h =xfrm_src_hash (net, &x->id.daddr, &x->props.saddr, x->props.family); -hlist_add_head (&x->bysrc, net->xfrm.state_bysrc+h); +hlist_add_head_rcu (&x->bysrc, net->xfrm.state_bysrc + h);if (x->id.spi) { h =xfrm_spi_hash (net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family); -hlist_add_head (&x->byspi, net->xfrm.state_byspi+h); +hlist_add_head_rcu (&x->byspi, net->xfrm.state_byspi + h); }tasklet_hrtimer_start (&x->mtimer,ktime_set (1 ,0 ),HRTIMER_MODE_REL );
@@ -1063,9 +1063,9 @@ static struct xfrm_state *__find_acq_core(struct net *net,xfrm_state_hold (x);tasklet_hrtimer_start (&x->mtimer,ktime_set (net->xfrm.sysctl_acq_expires,0 ),HRTIMER_MODE_REL );list_add (&x->km.all, &net->xfrm.state_all); -hlist_add_head (&x->bydst, net->xfrm.state_bydst+h); +hlist_add_head_rcu (&x->bydst, net->xfrm.state_bydst + h); h =xfrm_src_hash (net, daddr, saddr, family); -hlist_add_head (&x->bysrc, net->xfrm.state_bysrc+h); +hlist_add_head_rcu (&x->bysrc, net->xfrm.state_bysrc + h); net->xfrm.state_num++;
@@ -1581,7 +1581,7 @@ int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)if (x->id.spi) {spin_lock_bh (&net->xfrm.xfrm_state_lock); h =xfrm_spi_hash (net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family); -hlist_add_head (&x->byspi, net->xfrm.state_byspi+h); +hlist_add_head_rcu (&x->byspi, net->xfrm.state_byspi + h);spin_unlock_bh (&net->xfrm.xfrm_state_lock); err =0 ;
Signature · 1 line
1.9.1
Steffen Klassert
maintainerPATCH 8/17 xfrm: policy: use rcu versions for iteration and list add/del
In reply to Steffen KlassertFrom: Florian Westphal <[email protected]>
This is required once we allow lockless readers.
--- net/xfrm/xfrm_policy.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
net/xfrm/xfrm_policy.c +7 -7
@@ -426,14 +426,14 @@ redo: h =__addr_hash (&pol->selector.daddr, &pol->selector.saddr, pol->family, nhashmask, dbits, sbits);if (!entry0) { -hlist_del (&pol->bydst); -hlist_add_head (&pol->bydst, ndsttable+h); +hlist_del_rcu (&pol->bydst); +hlist_add_head_rcu (&pol->bydst, ndsttable + h); h0 = h; }else {if (h != h0)continue ; -hlist_del (&pol->bydst); -hlist_add_behind (&pol->bydst, entry0); +hlist_del_rcu (&pol->bydst); +hlist_add_behind_rcu (&pol->bydst, entry0); } entry0 = &pol->bydst; }
@@ -1106,7 +1106,7 @@ static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,read_lock_bh (&net->xfrm.xfrm_policy_lock); chain =policy_hash_direct (net, daddr, saddr, family, dir); ret =NULL ; -hlist_for_each_entry (pol, chain, bydst) { +hlist_for_each_entry_rcu (pol, chain, bydst) { err =xfrm_policy_match (pol, fl, type, family, dir);if (err) {if (err == -ESRCH )
@@ -1122,7 +1122,7 @@ static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type, } } chain = &net->xfrm.policy_inexact[dir]; -hlist_for_each_entry (pol, chain, bydst) { +hlist_for_each_entry_rcu (pol, chain, bydst) {if ((pol->priority >= priority) && ret)break ;
@@ -1271,7 +1271,7 @@ static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,/* Socket policies are not hashed. */ if (!hlist_unhashed (&pol->bydst)) { -hlist_del (&pol->bydst); +hlist_del_rcu (&pol->bydst);hlist_del (&pol->byidx); }
Signature · 1 line
1.9.1
Steffen Klassert
maintainerPATCH 9/17 xfrm: policy: prepare policy_bydst hash for rcu lookups
In reply to Steffen KlassertFrom: Florian Westphal <[email protected]>
Since commit 56f047305dd4b6b617
("xfrm: add rcu grace period in xfrm_policy_destroy()") xfrm policy objects are already free'd via rcu.
In order to make more places lockless (i.e. use rcu_read_lock instead of grabbing read-side of policy rwlock) we only need to:
- use rcu_assign_pointer to store address of new hash table backend memory
- add rcu barrier so that freeing of old memory is delayed (expansion and free happens from system workqueue, so synchronize_rcu is fine)
- use rcu_dereference to fetch current address of the hash table.
--- net/xfrm/xfrm_policy.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)
net/xfrm/xfrm_policy.c +14 -6
@@ -385,9 +385,11 @@ static struct hlist_head *policy_hash_bysel(struct net *net,__get_hash_thresh (net, family, dir, &dbits, &sbits); hash =__sel_hash (sel, family, hmask, dbits, sbits); -return (hash == hmask +1 ? - &net->xfrm.policy_inexact[dir] : - net->xfrm.policy_bydst[dir].table + hash); +if (hash == hmask +1 ) +return &net->xfrm.policy_inexact[dir]; + +return rcu_dereference_check (net->xfrm.policy_bydst[dir].table, +lockdep_is_held (&net->xfrm.xfrm_policy_lock)) + hash; } static struct hlist_head *policy_hash_direct(struct net *net,
@@ -403,7 +405,8 @@ static struct hlist_head *policy_hash_direct(struct net *net,__get_hash_thresh (net, family, dir, &dbits, &sbits); hash =__addr_hash (daddr, saddr, family, hmask, dbits, sbits); -return net->xfrm.policy_bydst[dir].table + hash; +return rcu_dereference_check (net->xfrm.policy_bydst[dir].table, +lockdep_is_held (&net->xfrm.xfrm_policy_lock)) + hash; } static void xfrm_dst_hash_transfer(struct net *net,
@@ -468,8 +471,8 @@ static void xfrm_bydst_resize(struct net *net, int dir)unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;unsigned int nhashmask =xfrm_new_hash_mask (hmask);unsigned int nsize = (nhashmask +1 ) *sizeof (struct hlist_head ); -struct hlist_head *odst = net->xfrm.policy_bydst[dir].table;struct hlist_head *ndst =xfrm_hash_alloc (nsize); +struct hlist_head *odst;int i;if (!ndst)
@@ -477,14 +480,19 @@ static void xfrm_bydst_resize(struct net *net, int dir)write_lock_bh (&net->xfrm.xfrm_policy_lock); + odst =rcu_dereference_protected (net->xfrm.policy_bydst[dir].table, +lockdep_is_held (&net->xfrm.xfrm_policy_lock)); +for (i = hmask; i >=0 ; i--)xfrm_dst_hash_transfer (net, odst + i, ndst, nhashmask, dir); - net->xfrm.policy_bydst[dir].table = ndst; +rcu_assign_pointer (net->xfrm.policy_bydst[dir].table, ndst); net->xfrm.policy_bydst[dir].hmask = nhashmask;write_unlock_bh (&net->xfrm.xfrm_policy_lock); +synchronize_rcu (); +xfrm_hash_free (odst, (hmask +1 ) *sizeof (struct hlist_head )); }
Signature · 1 line
1.9.1
Steffen Klassert
maintainerPATCH 10/17 xfrm: policy: add sequence count to sync with hash resize
In reply to Steffen KlassertFrom: Florian Westphal <[email protected]>
Once xfrm_policy_lookup_bytype doesn't grab xfrm_policy_lock anymore its possible for a hash resize to occur in parallel.
Use sequence counter to block lookup in case a resize is in progress and to also re-lookup in case hash table was altered in the mean time (might cause use to not find the best-match).
--- net/xfrm/xfrm_policy.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-)
net/xfrm/xfrm_policy.c +19 -2
@@ -49,6 +49,7 @@ static struct xfrm_policy_afinfo __rcu *xfrm_policy_afinfo[NPROTO]__read_mostly ;static struct kmem_cache *xfrm_dst_cache__read_mostly ; +static __read_mostly seqcount_t xfrm_policy_hash_generation;static void xfrm_init_pmtu (struct dst_entry *dst);static int stale_bundle (struct dst_entry *dst);
@@ -479,6 +480,10 @@ static void xfrm_bydst_resize(struct net *net, int dir)return ;write_lock_bh (&net->xfrm.xfrm_policy_lock); +write_seqcount_begin (&xfrm_policy_hash_generation); + + odst =rcu_dereference_protected (net->xfrm.policy_bydst[dir].table, +lockdep_is_held (&net->xfrm.xfrm_policy_lock)); odst =rcu_dereference_protected (net->xfrm.policy_bydst[dir].table,lockdep_is_held (&net->xfrm.xfrm_policy_lock));
@@ -489,6 +494,7 @@ static void xfrm_bydst_resize(struct net *net, int dir)rcu_assign_pointer (net->xfrm.policy_bydst[dir].table, ndst); net->xfrm.policy_bydst[dir].hmask = nhashmask; +write_seqcount_end (&xfrm_policy_hash_generation);write_unlock_bh (&net->xfrm.xfrm_policy_lock);synchronize_rcu ();
@@ -1104,7 +1110,8 @@ static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,struct xfrm_policy *pol, *ret;const xfrm_address_t *daddr, *saddr;struct hlist_head *chain; -u32 priority = ~0U ; +unsigned int sequence; +u32 priority; daddr =xfrm_flowi_daddr (fl, family); saddr =xfrm_flowi_saddr (fl, family);
@@ -1112,7 +1119,13 @@ static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,return NULL ;read_lock_bh (&net->xfrm.xfrm_policy_lock); - chain =policy_hash_direct (net, daddr, saddr, family, dir); + retry: +do { + sequence =read_seqcount_begin (&xfrm_policy_hash_generation); + chain =policy_hash_direct (net, daddr, saddr, family, dir); + }while (read_seqcount_retry (&xfrm_policy_hash_generation, sequence)); + + priority = ~0U ; ret =NULL ;hlist_for_each_entry_rcu (pol, chain, bydst) { err =xfrm_policy_match (pol, fl, type, family, dir);
@@ -1148,6 +1161,9 @@ static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type, } } +if (read_seqcount_retry (&xfrm_policy_hash_generation, sequence)) + goto retry; +xfrm_pol_hold (ret); fail:read_unlock_bh (&net->xfrm.xfrm_policy_lock);
@@ -3090,6 +3106,7 @@ static struct pernet_operations __net_initdata xfrm_net_ops = {void __init xfrm_init (void ) {register_pernet_subsys (&xfrm_net_ops); +seqcount_init (&xfrm_policy_hash_generation);xfrm_input_init (); }
Signature · 1 line
1.9.1
Steffen Klassert
maintainerPATCH 12/17 xfrm: policy: make xfrm_policy_lookup_bytype lockless
In reply to Steffen KlassertFrom: Florian Westphal <[email protected]>
side effect: no longer disables BH (should be fine).
--- include/net/netns/xfrm.h | 2 +- net/xfrm/xfrm_policy.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-)
include/net/netns/xfrm.h +1 -1
@@ -11,7 +11,7 @@struct ctl_table_header ;struct xfrm_policy_hash { -struct hlist_head *table; +struct hlist_head __rcu *table;unsigned int hmask;u8 dbits4;u8 sbits4;
net/xfrm/xfrm_policy.c +2 -2
@@ -1123,7 +1123,7 @@ static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,if (unlikely (!daddr || !saddr))return NULL ; -read_lock_bh (&net->xfrm.xfrm_policy_lock); +rcu_read_lock (); retry: do { sequence = read_seqcount_begin(&xfrm_policy_hash_generation);
@@ -1172,7 +1172,7 @@ static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,if (ret && !xfrm_pol_hold_rcu (ret)) goto retry; fail: -read_unlock_bh (&net->xfrm.xfrm_policy_lock); +rcu_read_unlock ();return ret; }
Signature · 1 line
1.9.1
Steffen Klassert
maintainerPATCH 14/17 xfrm: policy: don't acquire policy lock in xfrm_spd_getinfo
In reply to Steffen KlassertFrom: Florian Westphal <[email protected]>
It doesn't seem that important.
We now get inconsistent view of the counters, but those are stale anyway right after we drop the lock.
--- net/xfrm/xfrm_policy.c | 2 -- 1 file changed, 2 deletions(-)
net/xfrm/xfrm_policy.c +0 -2
@@ -560,7 +560,6 @@ static inline int xfrm_byidx_should_resize(struct net *net, int total)void xfrm_spd_getinfo (struct net *net,struct xfrmk_spdinfo *si) { -read_lock_bh (&net->xfrm.xfrm_policy_lock); si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN ]; si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT ]; si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD ];
@@ -569,7 +568,6 @@ void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si) si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD +XFRM_POLICY_MAX ]; si->spdhcnt = net->xfrm.policy_idx_hmask; si->spdhmcnt = xfrm_policy_hashmax; -read_unlock_bh (&net->xfrm.xfrm_policy_lock); }EXPORT_SYMBOL (xfrm_spd_getinfo);
Signature · 1 line
1.9.1
Steffen Klassert
maintainerPATCH 1/17 xfrm: constify xfrm_replay structures
In reply to Steffen KlassertFrom: Julia Lawall <[email protected]>
The xfrm_replay structures are never modified, so declare them as const.
Done with the help of Coccinelle.
--- include/net/xfrm.h | 2 +- net/xfrm/xfrm_replay.c | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-)
include/net/xfrm.h +1 -1
@@ -187,7 +187,7 @@ struct xfrm_state {struct xfrm_replay_state_esn *preplay_esn;/* The functions for replay detection. */ -struct xfrm_replay *repl; +const struct xfrm_replay *repl;/* internal flag that only holds state for delayed aevent at the * moment
net/xfrm/xfrm_replay.c +3 -3
@@ -558,7 +558,7 @@ static void xfrm_replay_advance_esn(struct xfrm_state *x, __be32 net_seq) x->repl->notify (x,XFRM_REPLAY_UPDATE ); } -static struct xfrm_replay xfrm_replay_legacy = { +static const struct xfrm_replay xfrm_replay_legacy = { .advance = xfrm_replay_advance, .check = xfrm_replay_check, .recheck = xfrm_replay_check,
@@ -566,7 +566,7 @@ static struct xfrm_replay xfrm_replay_legacy = { .overflow = xfrm_replay_overflow, }; -static struct xfrm_replay xfrm_replay_bmp = { +static const struct xfrm_replay xfrm_replay_bmp = { .advance = xfrm_replay_advance_bmp, .check = xfrm_replay_check_bmp, .recheck = xfrm_replay_check_bmp,
@@ -574,7 +574,7 @@ static struct xfrm_replay xfrm_replay_bmp = { .overflow = xfrm_replay_overflow_bmp, }; -static struct xfrm_replay xfrm_replay_esn = { +static const struct xfrm_replay xfrm_replay_esn = { .advance = xfrm_replay_advance_esn, .check = xfrm_replay_check_esn, .recheck = xfrm_replay_recheck_esn,
Signature · 1 line
1.9.1
Steffen Klassert
maintainerPATCH 5/17 xfrm: state: add sequence count to detect hash resizes
In reply to Steffen KlassertFrom: Florian Westphal <[email protected]>
Once xfrm_state_find is lockless we have to cope with a concurrent resize opertion.
We use a sequence counter to block in case a resize is in progress and to detect if we might have missed a state that got moved to a new hash table.
--- net/xfrm/xfrm_state.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
net/xfrm/xfrm_state.c +15 -0
@@ -36,6 +36,7 @@*/ static unsigned int xfrm_state_hashmax__read_mostly =1 *1024 *1024 ; +static __read_mostly seqcount_t xfrm_state_hash_generation =SEQCNT_ZERO (xfrm_state_hash_generation);static inline bool xfrm_state_hold_rcu (struct xfrm_state __rcu *x) {
@@ -127,6 +128,7 @@ static void xfrm_hash_resize(struct work_struct *work) }spin_lock_bh (&net->xfrm.xfrm_state_lock); +write_seqcount_begin (&xfrm_state_hash_generation); nhashmask = (nsize /sizeof (struct hlist_head )) -1U ;for (i = net->xfrm.state_hmask; i >=0 ; i--)
@@ -143,6 +145,7 @@ static void xfrm_hash_resize(struct work_struct *work) net->xfrm.state_byspi = nspi; net->xfrm.state_hmask = nhashmask; +write_seqcount_end (&xfrm_state_hash_generation);spin_unlock_bh (&net->xfrm.xfrm_state_lock); osize = (ohashmask +1 ) *sizeof (struct hlist_head );
@@ -787,10 +790,13 @@ xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,struct xfrm_state *best =NULL ;u32 mark = pol->mark.v & pol->mark.m;unsigned short encap_family = tmpl->encap_family; +unsigned int sequence;struct km_event c; to_put =NULL ; + sequence =read_seqcount_begin (&xfrm_state_hash_generation); +spin_lock_bh (&net->xfrm.xfrm_state_lock); h =xfrm_dst_hash (net, daddr, saddr, tmpl->reqid, encap_family);hlist_for_each_entry_rcu (x, net->xfrm.state_bydst + h, bydst) {
@@ -894,6 +900,15 @@ out:spin_unlock_bh (&net->xfrm.xfrm_state_lock);if (to_put)xfrm_state_put (to_put); + +if (read_seqcount_retry (&xfrm_state_hash_generation, sequence)) { + *err = -EAGAIN ; +if (x) { +xfrm_state_put (x); + x =NULL ; + } + } +return x; }
Signature · 1 line
1.9.1
Steffen Klassert
maintainerPATCH 15/17 xfrm: policy: convert policy_lock to spinlock
In reply to Steffen KlassertFrom: Florian Westphal <[email protected]>
After earlier patches conversions all spots acquire the writer lock and we can now convert this to a normal spinlock.
--- include/net/netns/xfrm.h | 2 +- net/xfrm/xfrm_policy.c | 68 ++++++++++++++++++++++++------------------------ 2 files changed, 35 insertions(+), 35 deletions(-)
include/net/netns/xfrm.h +1 -1
@@ -73,7 +73,7 @@ struct netns_xfrm {struct dst_ops xfrm6_dst_ops;#endif spinlock_t xfrm_state_lock; -rwlock_t xfrm_policy_lock; +spinlock_t xfrm_policy_lock;struct mutex xfrm_cfg_mutex;/* flow cache part */
net/xfrm/xfrm_policy.c +34 -34
256 lines not on this page · raw message
Signature · 1 line
1.9.1
Steffen Klassert
maintainerPATCH 16/17 xfrm: Fix xfrm_policy_lock imbalance
In reply to Steffen KlassertAn earlier patch accidentally replaced a write_lock_bh with a spin_unlock_bh. Fix this by using spin_lock_bh instead.
--- net/xfrm/xfrm_policy.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/xfrm/xfrm_policy.c +1 -1
@@ -979,7 +979,7 @@ int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)xfrm_policy_kill (pol); -spin_unlock_bh (&net->xfrm.xfrm_policy_lock); +spin_lock_bh (&net->xfrm.xfrm_policy_lock); goto again1; }
Signature · 1 line
1.9.1
David Miller
maintainerFrom: Steffen Klassert <[email protected]>
Date: Thu, 8 Sep 2016 07:54:35 +0200
Quoted 6 lines
1) Constify the
xfrm_replaystructures. From Julia Lawall2) Protect xfrm state hash tables with rcu, lookups can be done now without acquiring
xfrm_state_lock. From Florian Westphal.
Quoted 7 lines
3) Protect xfrm policy hash tables with rcu, lookups can be done now without acquiring
xfrm_policy_lock. From Florian Westphal.4) We don't need to have a garbage collector list per namespace anymore, so use a global one instead. From Florian Westphal.
Pulled, thanks Steffen.