xfrm: remove xfrm_state_get_afinfo conditional rcu locking
Subsystems (3)
cover xfrm: remove xfrm_state_get_afinfo conditional rcu locking
xfrm_state_get_afinfo still uses a conditional locking scheme dating back to when this still used an rwlock:
If return value is NULL, no lock (rcu readlock) was taken, otherwise, rcu_read_unlock has to be called.
This series moves rcu read lock/unlock responsibility to the callers.
xfrm_state_put_afinfo is removed (it is equivalent to rcu_read_unlock so thats what will be used instead).
xfrm_state_get_afinfo is renamed to xfrm_state_afinfo_get_rcu() and is only a rcu dereference wrapper.
In one case, rcu read/unlock can be avoided as we're always called with rcu read lock held.
PATCH 1/6 xfrm: remove unused function
Has been ifdef'd out for more than 10 years, remove it.
--- net/ipv4/xfrm4_state.c | 8 -------- 1 file changed, 8 deletions(-)
net/ipv4/xfrm4_state.c +0 -8
@@ -90,11 +90,3 @@ void __init xfrm4_state_init(void) {xfrm_state_register_afinfo (&xfrm4_state_afinfo); } - -#if 0 -void __exit xfrm4_state_fini(void) -{ -xfrm_state_unregister_afinfo (&xfrm4_state_afinfo); -} -#endif /* 0 */ -
Signature · 1 line
2.7.3
PATCH 2/6 xfrm: avoid rcu sparse warning
In reply to Florian Westphalxfrm/xfrm_state.c:1973:21: error: incompatible types in comparison expression (different address spaces)
Harmless, but lets fix it to reduce the noise.
While at it, get rid of unneeded NULL check, its never hit:
net/ipv4/xfrm4_state.c: xfrm_state_register_afinfo(&xfrm4_state_afinfo); net/ipv6/xfrm6_state.c: return xfrm_state_register_afinfo(&xfrm6_state_afinfo); net/ipv6/xfrm6_state.c: xfrm_state_unregister_afinfo(&xfrm6_state_afinfo);
... are the only callsites.
--- net/xfrm/xfrm_state.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
net/xfrm/xfrm_state.c +8 -8
@@ -1932,10 +1932,10 @@ EXPORT_SYMBOL(xfrm_unregister_km);int xfrm_state_register_afinfo (struct xfrm_state_afinfo *afinfo) {int err =0 ; -if (unlikely (afinfo ==NULL )) -return -EINVAL ; -if (unlikely (afinfo->family >=NPROTO )) + +if (WARN_ON (afinfo->family >=NPROTO ))return -EAFNOSUPPORT ; +spin_lock_bh (&xfrm_state_afinfo_lock);if (unlikely (xfrm_state_afinfo[afinfo->family] !=NULL )) err = -EEXIST ;
@@ -1948,14 +1948,14 @@ EXPORT_SYMBOL(xfrm_state_register_afinfo);int xfrm_state_unregister_afinfo (struct xfrm_state_afinfo *afinfo) { -int err =0 ; -if (unlikely (afinfo ==NULL )) -return -EINVAL ; -if (unlikely (afinfo->family >=NPROTO )) +int err =0 , family = afinfo->family; + +if (WARN_ON (family >=NPROTO ))return -EAFNOSUPPORT ; +spin_lock_bh (&xfrm_state_afinfo_lock);if (likely (xfrm_state_afinfo[afinfo->family] !=NULL )) { -if (unlikely (xfrm_state_afinfo[afinfo->family] != afinfo)) +if (rcu_access_pointer (xfrm_state_afinfo[family]) != afinfo) err = -EINVAL ;else RCU_INIT_POINTER (xfrm_state_afinfo[afinfo->family],NULL );
Signature · 1 line
2.7.3
PATCH 3/6 xfrm: remove xfrm_state_put_afinfo
In reply to Florian Westphalcommit 44abdc3047aecafc141dfbaf1ed
("xfrm: replace rwlock on xfrm_state_afinfo with rcu") made xfrm_state_put_afinfo equivalent to rcu_read_unlock.
Use spatch to replace it with direct calls to rcu_read_unlock:
@@
struct xfrm_state_afinfo *a;
@@
- xfrm_state_put_afinfo(a);
+ rcu_read_unlock();
old:
text data bss dec hex filename
22570 72 424 23066 5a1a xfrm_state.o
1612 0 0 1612 64c xfrm_output.o
new:
22554 72 424 23050 5a0a xfrm_state.o
1596 0 0 1596 63c xfrm_output.o
--- include/net/xfrm.h | 1 - net/xfrm/xfrm_output.c | 8 +++----- net/xfrm/xfrm_state.c | 31 +++++++++++++------------------ 3 files changed, 16 insertions(+), 24 deletions(-)
include/net/xfrm.h +0 -1
@@ -343,7 +343,6 @@ struct xfrm_state_afinfo {int xfrm_state_register_afinfo (struct xfrm_state_afinfo *afinfo);int xfrm_state_unregister_afinfo (struct xfrm_state_afinfo *afinfo);struct xfrm_state_afinfo *xfrm_state_get_afinfo (unsigned int family); -void xfrm_state_put_afinfo (struct xfrm_state_afinfo *afinfo);struct xfrm_input_afinfo {unsigned int family;
net/xfrm/xfrm_output.c +3 -5
@@ -246,10 +246,8 @@ void xfrm_local_error(struct sk_buff *skb, int mtu)return ; afinfo =xfrm_state_get_afinfo (proto); -if (!afinfo) -return ; - - afinfo->local_error (skb, mtu); -xfrm_state_put_afinfo (afinfo); +if (afinfo) + afinfo->local_error (skb, mtu); +rcu_read_unlock (); }EXPORT_SYMBOL_GPL (xfrm_local_error);
net/xfrm/xfrm_state.c +13 -18
@@ -192,7 +192,7 @@ int xfrm_register_type(const struct xfrm_type *type, unsigned short family)else err = -EEXIST ;spin_unlock_bh (&xfrm_type_lock); -xfrm_state_put_afinfo (afinfo); +rcu_read_unlock ();return err; }EXPORT_SYMBOL (xfrm_register_type);
@@ -213,7 +213,7 @@ int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)else typemap[type->proto] =NULL ;spin_unlock_bh (&xfrm_type_lock); -xfrm_state_put_afinfo (afinfo); +rcu_read_unlock ();return err; }EXPORT_SYMBOL (xfrm_unregister_type);
@@ -235,13 +235,13 @@ static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)if (unlikely (type && !try_module_get (type->owner))) type =NULL ;if (!type && !modload_attempted) { -xfrm_state_put_afinfo (afinfo); +rcu_read_unlock ();request_module ("xfrm-type-%d-%d" , family, proto); modload_attempted =1 ; goto retry; } -xfrm_state_put_afinfo (afinfo); +rcu_read_unlock ();return type; }
@@ -280,7 +280,7 @@ int xfrm_register_mode(struct xfrm_mode *mode, int family) out:spin_unlock_bh (&xfrm_mode_lock); -xfrm_state_put_afinfo (afinfo); +rcu_read_unlock ();return err; }EXPORT_SYMBOL (xfrm_register_mode);
@@ -308,7 +308,7 @@ int xfrm_unregister_mode(struct xfrm_mode *mode, int family) }spin_unlock_bh (&xfrm_mode_lock); -xfrm_state_put_afinfo (afinfo); +rcu_read_unlock ();return err; }EXPORT_SYMBOL (xfrm_unregister_mode);
@@ -331,13 +331,13 @@ static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)if (unlikely (mode && !try_module_get (mode->owner))) mode =NULL ;if (!mode && !modload_attempted) { -xfrm_state_put_afinfo (afinfo); +rcu_read_unlock ();request_module ("xfrm-mode-%d-%d" , family, encap); modload_attempted =1 ; goto retry; } -xfrm_state_put_afinfo (afinfo); +rcu_read_unlock ();return mode; }
@@ -651,13 +651,13 @@ xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl, afinfo->init_tempsel (&x->sel, fl);if (family != tmpl->encap_family) { -xfrm_state_put_afinfo (afinfo); +rcu_read_unlock (); afinfo =xfrm_state_get_afinfo (tmpl->encap_family);if (!afinfo)return -1 ; } afinfo->init_temprop (x, tmpl, daddr, saddr); -xfrm_state_put_afinfo (afinfo); +rcu_read_unlock ();return 0 ; }
@@ -1474,7 +1474,7 @@ xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,if (afinfo->tmpl_sort) err = afinfo->tmpl_sort (dst, src, n);spin_unlock_bh (&net->xfrm.xfrm_state_lock); -xfrm_state_put_afinfo (afinfo); +rcu_read_unlock ();return err; }EXPORT_SYMBOL (xfrm_tmpl_sort);
@@ -1494,7 +1494,7 @@ xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,if (afinfo->state_sort) err = afinfo->state_sort (dst, src, n);spin_unlock_bh (&net->xfrm.xfrm_state_lock); -xfrm_state_put_afinfo (afinfo); +rcu_read_unlock ();return err; }EXPORT_SYMBOL (xfrm_state_sort);
@@ -1978,11 +1978,6 @@ struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)return afinfo; } -void xfrm_state_put_afinfo (struct xfrm_state_afinfo *afinfo) -{ -rcu_read_unlock (); -} -/* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */ void xfrm_state_delete_tunnel (struct xfrm_state *x) {
@@ -2028,7 +2023,7 @@ int __xfrm_init_state(struct xfrm_state *x, bool init_replay)if (afinfo->init_flags) err = afinfo->init_flags (x); -xfrm_state_put_afinfo (afinfo); +rcu_read_unlock ();if (err) goto error;
Signature · 1 line
2.7.3
PATCH 4/6 xfrm: add and use xfrm_state_afinfo_get_rcu
In reply to Florian Westphalxfrm_init_tempstate is always called from within rcu read side section. We can thus use a simpler function that doesn't call rcu_read_lock again.
While at it, also make xfrm_init_tempstate return value void, the return value was never tested.
A followup patch will replace remaining callers of xfrm_state_get_afinfo with xfrm_state_afinfo_get_rcu variant and then remove the 'old' get_afinfo interface.
--- include/net/xfrm.h | 1 + net/xfrm/xfrm_state.c | 25 +++++++++++++++---------- 2 files changed, 16 insertions(+), 10 deletions(-)
include/net/xfrm.h +1 -0
@@ -343,6 +343,7 @@ struct xfrm_state_afinfo {int xfrm_state_register_afinfo (struct xfrm_state_afinfo *afinfo);int xfrm_state_unregister_afinfo (struct xfrm_state_afinfo *afinfo);struct xfrm_state_afinfo *xfrm_state_get_afinfo (unsigned int family); +struct xfrm_state_afinfo *xfrm_state_afinfo_get_rcu (unsigned int family);struct xfrm_input_afinfo {unsigned int family;
net/xfrm/xfrm_state.c +15 -10
@@ -639,26 +639,23 @@ void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si) }EXPORT_SYMBOL (xfrm_sad_getinfo); -static int +static void xfrm_init_tempstate (struct xfrm_state *x,const struct flowi *fl,const struct xfrm_tmpl *tmpl,const xfrm_address_t *daddr,const xfrm_address_t *saddr,unsigned short family) { -struct xfrm_state_afinfo *afinfo =xfrm_state_get_afinfo (family); -if (!afinfo) -return -1 ; - afinfo->init_tempsel (&x->sel, fl); +struct xfrm_state_afinfo *afinfo =xfrm_state_afinfo_get_rcu (family); + +if (afinfo) + afinfo->init_tempsel (&x->sel, fl);if (family != tmpl->encap_family) { -rcu_read_unlock (); - afinfo =xfrm_state_get_afinfo (tmpl->encap_family); + afinfo =xfrm_state_afinfo_get_rcu (tmpl->encap_family);if (!afinfo) -return -1 ; +return ; } afinfo->init_temprop (x, tmpl, daddr, saddr); -rcu_read_unlock (); -return 0 ; } static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
@@ -1966,6 +1963,14 @@ int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo) }EXPORT_SYMBOL (xfrm_state_unregister_afinfo); +struct xfrm_state_afinfo *xfrm_state_afinfo_get_rcu (unsigned int family) +{ +if (unlikely (family >=NPROTO )) +return NULL ; + +return rcu_dereference (xfrm_state_afinfo[family]); +} +struct xfrm_state_afinfo *xfrm_state_get_afinfo (unsigned int family) {struct xfrm_state_afinfo *afinfo;
Signature · 1 line
2.7.3
PATCH 5/6 xfrm: state: simplify rcu_read_unlock handling in two spots
In reply to Florian WestphalInstead of:
if (foo) {
unlock();
return bar(); }
unlock();
do:
unlock();
if (foo)
return bar();
This is ok because rcu protected structure is only dereferenced before the conditional.
--- net/xfrm/xfrm_state.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)
net/xfrm/xfrm_state.c +7 -6
@@ -231,17 +231,18 @@ static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)return NULL ; typemap = afinfo->type_map; - type = typemap[proto]; + type =READ_ONCE (typemap[proto]);if (unlikely (type && !try_module_get (type->owner))) type =NULL ; + +rcu_read_unlock (); +if (!type && !modload_attempted) { -rcu_read_unlock ();request_module ("xfrm-type-%d-%d" , family, proto); modload_attempted =1 ; goto retry; } -rcu_read_unlock ();return type; }
@@ -327,17 +328,17 @@ static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)if (unlikely (afinfo ==NULL ))return NULL ; - mode = afinfo->mode_map[encap]; + mode =READ_ONCE (afinfo->mode_map[encap]);if (unlikely (mode && !try_module_get (mode->owner))) mode =NULL ; + +rcu_read_unlock ();if (!mode && !modload_attempted) { -rcu_read_unlock ();request_module ("xfrm-mode-%d-%d" , family, encap); modload_attempted =1 ; goto retry; } -rcu_read_unlock ();return mode; }
Signature · 1 line
2.7.3
PATCH 6/6 xfrm: remove xfrm_state_get_afinfo
In reply to Florian WestphalUse xfrm_state_afinfo_get_rcu() everywhere, this also moves rcu read (un)lock responsibility to the callers.
This avoids the conditional locking scheme we currently have.
--- include/net/xfrm.h | 1 - net/xfrm/xfrm_output.c | 3 +- net/xfrm/xfrm_state.c | 97 +++++++++++++++++++++++++++++++------------------- 3 files changed, 62 insertions(+), 39 deletions(-)
include/net/xfrm.h +0 -1
@@ -342,7 +342,6 @@ struct xfrm_state_afinfo {int xfrm_state_register_afinfo (struct xfrm_state_afinfo *afinfo);int xfrm_state_unregister_afinfo (struct xfrm_state_afinfo *afinfo); -struct xfrm_state_afinfo *xfrm_state_get_afinfo (unsigned int family);struct xfrm_state_afinfo *xfrm_state_afinfo_get_rcu (unsigned int family);struct xfrm_input_afinfo {
net/xfrm/xfrm_output.c +2 -1
@@ -245,7 +245,8 @@ void xfrm_local_error(struct sk_buff *skb, int mtu)else return ; - afinfo =xfrm_state_get_afinfo (proto); +rcu_read_lock (); + afinfo =xfrm_state_afinfo_get_rcu (proto);if (afinfo) afinfo->local_error (skb, mtu);rcu_read_unlock ();
net/xfrm/xfrm_state.c +60 -37
192 lines not on this page · raw message
Signature · 1 line
2.7.3
Steffen Klassert
maintainerQuoted 8 lines · Florian Westphal
xfrm_state_get_afinfostill uses a conditional locking scheme dating back to when this still used an rwlock:If return value is NULL, no lock (rcu readlock) was taken, otherwise,
rcu_read_unlockhas to be called.This series moves rcu read lock/unlock responsibility to the callers.
Quoted 8 lines
xfrm_state_put_afinfois removed (it is equivalent torcu_read_unlockso thats what will be used instead).
xfrm_state_get_afinfois renamed toxfrm_state_afinfo_get_rcu()and is only a rcu dereference wrapper.In one case, rcu read/unlock can be avoided as we're always called with rcu read lock held.
All applied to ipsec-next, thanks a lot!