xfrm: policy: make policy backend const
Subsystems (3)
cover xfrm: policy: make policy backend const
Hi Steffen,
This series removes all places that need to write to the afinfo policy backends. This then allows us to make the structures const.
PATCH 1/6 xfrm: policy: xfrm_get_tos cannot fail
The comment makes it look like get_tos() is used to validate something, but it turns out the comment was about xfrm_find_bundle() which got removed years ago.
xfrm_get_tos will return either the tos (ipv4) or 0 (ipv6).
--- net/xfrm/xfrm_policy.c | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-)
net/xfrm/xfrm_policy.c +5 -14
@@ -1538,19 +1538,13 @@ xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl, } -/* Check that the bundle accepts the flow and its components are -* still valid. -*/ - -static inline int xfrm_get_tos (const struct flowi *fl,int family) +static int xfrm_get_tos (const struct flowi *fl,int family) { -struct xfrm_policy_afinfo *afinfo =xfrm_policy_get_afinfo (family); -int tos; - -if (!afinfo) -return -EINVAL ; +struct xfrm_policy_afinfo *afinfo; +int tos =0 ; - tos = afinfo->get_tos (fl); + afinfo =xfrm_policy_get_afinfo (family); + tos = afinfo ? afinfo->get_tos (fl) :0 ;xfrm_policy_put_afinfo (afinfo);
@@ -1705,9 +1699,6 @@ static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,xfrm_flowi_addr_get (fl, &saddr, &daddr, family); tos =xfrm_get_tos (fl, family); - err = tos; -if (tos <0 ) - goto put_states;dst_hold (dst);
Signature · 1 line
2.10.2
PATCH 2/6 xfrm: policy: xfrm_policy_unregister_afinfo can return void
In reply to Florian WestphalNothing checks the return value. Also, the errors returned on unregister are impossible (we only support INET and INET6, so no way xfrm_policy_afinfo[afinfo->family] can be anything other than 'afinfo' itself).
--- include/net/xfrm.h | 2 +- net/xfrm/xfrm_policy.c | 35 +++++++++++++---------------------- 2 files changed, 14 insertions(+), 23 deletions(-)
include/net/xfrm.h +1 -1
@@ -304,7 +304,7 @@ struct xfrm_policy_afinfo { };int xfrm_policy_register_afinfo (struct xfrm_policy_afinfo *afinfo); -int xfrm_policy_unregister_afinfo (struct xfrm_policy_afinfo *afinfo); +void xfrm_policy_unregister_afinfo (struct xfrm_policy_afinfo *afinfo);void km_policy_notify (struct xfrm_policy *xp,int dir,const struct km_event *c);void km_state_notify (struct xfrm_state *x,const struct km_event *c);
net/xfrm/xfrm_policy.c +13 -22
@@ -2883,34 +2883,25 @@ int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo) }EXPORT_SYMBOL (xfrm_policy_register_afinfo); -int xfrm_policy_unregister_afinfo (struct xfrm_policy_afinfo *afinfo) +void xfrm_policy_unregister_afinfo (struct xfrm_policy_afinfo *afinfo) { -int err =0 ; -if (unlikely (afinfo ==NULL )) -return -EINVAL ; +struct dst_ops *dst_ops = afinfo->dst_ops; +if (unlikely (afinfo->family >=NPROTO )) -return -EAFNOSUPPORT ; -spin_lock (&xfrm_policy_afinfo_lock); -if (likely (xfrm_policy_afinfo[afinfo->family] !=NULL )) { -if (unlikely (xfrm_policy_afinfo[afinfo->family] != afinfo)) - err = -EINVAL ; -else -RCU_INIT_POINTER (xfrm_policy_afinfo[afinfo->family], +return ; + +if (likely (xfrm_policy_afinfo[afinfo->family] != afinfo)) { +RCU_INIT_POINTER (xfrm_policy_afinfo[afinfo->family],NULL ); } -spin_unlock (&xfrm_policy_afinfo_lock); -if (!err) { -struct dst_ops *dst_ops = afinfo->dst_ops; -synchronize_rcu (); +synchronize_rcu (); - dst_ops->kmem_cachep =NULL ; - dst_ops->check =NULL ; - dst_ops->negative_advice =NULL ; - dst_ops->link_failure =NULL ; - afinfo->garbage_collect =NULL ; - } -return err; + dst_ops->kmem_cachep =NULL ; + dst_ops->check =NULL ; + dst_ops->negative_advice =NULL ; + dst_ops->link_failure =NULL ; + afinfo->garbage_collect =NULL ; }EXPORT_SYMBOL (xfrm_policy_unregister_afinfo);
Signature · 1 line
2.10.2
PATCH 3/6 xfrm: policy: remove garbage_collect callback
In reply to Florian WestphalJust call xfrm_garbage_collect_deferred() directly.
This gets rid of a write to afinfo in register/unregister and allows to constify afinfo later on.
--- include/net/xfrm.h | 2 +- net/ipv4/xfrm4_policy.c | 2 +- net/ipv6/xfrm6_policy.c | 2 +- net/xfrm/xfrm_policy.c | 6 ++---- 4 files changed, 5 insertions(+), 7 deletions(-)
include/net/xfrm.h +1 -1
@@ -282,7 +282,6 @@ struct xfrm_dst;struct xfrm_policy_afinfo {unsigned short family;struct dst_ops *dst_ops; -void (*garbage_collect)(struct net *net); struct dst_entry *(*dst_lookup)(struct net *net, int tos, int oif, const xfrm_address_t *saddr,
@@ -1169,6 +1168,7 @@ static inline void xfrm_sk_free_policy(struct sock *sk) }void xfrm_garbage_collect (struct net *net); +void xfrm_garbage_collect_deferred (struct net *net);#else
net/ipv4/xfrm4_policy.c +1 -1
@@ -219,7 +219,7 @@ static inline int xfrm4_garbage_collect(struct dst_ops *ops) { struct net *net = container_of(ops, struct net, xfrm.xfrm4_dst_ops); - xfrm4_policy_afinfo.garbage_collect (net); +xfrm_garbage_collect_deferred (net);return (dst_entries_get_slow (ops) > ops->gc_thresh *2 ); }
net/ipv6/xfrm6_policy.c +1 -1
@@ -220,7 +220,7 @@ static inline int xfrm6_garbage_collect(struct dst_ops *ops) { struct net *net = container_of(ops, struct net, xfrm.xfrm6_dst_ops); - xfrm6_policy_afinfo.garbage_collect (net); +xfrm_garbage_collect_deferred (net);return dst_entries_get_fast (ops) > ops->gc_thresh *2 ; }
net/xfrm/xfrm_policy.c +2 -4
@@ -2733,10 +2733,11 @@ void xfrm_garbage_collect(struct net *net) }EXPORT_SYMBOL (xfrm_garbage_collect); -static void xfrm_garbage_collect_deferred (struct net *net) +void xfrm_garbage_collect_deferred (struct net *net) {flow_cache_flush_deferred (net); } +EXPORT_SYMBOL (xfrm_garbage_collect_deferred);static void xfrm_init_pmtu (struct dst_entry *dst) {
@@ -2873,8 +2874,6 @@ int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo) dst_ops->link_failure = xfrm_link_failure;if (likely (dst_ops->neigh_lookup ==NULL )) dst_ops->neigh_lookup = xfrm_neigh_lookup; -if (likely (afinfo->garbage_collect ==NULL )) - afinfo->garbage_collect = xfrm_garbage_collect_deferred;rcu_assign_pointer (xfrm_policy_afinfo[afinfo->family], afinfo); }spin_unlock (&xfrm_policy_afinfo_lock);
@@ -2901,7 +2900,6 @@ void xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo) dst_ops->check =NULL ; dst_ops->negative_advice =NULL ; dst_ops->link_failure =NULL ; - afinfo->garbage_collect =NULL ; }EXPORT_SYMBOL (xfrm_policy_unregister_afinfo);
Signature · 1 line
2.10.2
PATCH 4/6 xfrm: policy: remove family field
In reply to Florian WestphalOnly needed it to register the policy backend at init time.
--- include/net/xfrm.h | 5 ++--- net/ipv4/xfrm4_policy.c | 5 +---- net/ipv6/xfrm6_policy.c | 5 +---- net/xfrm/xfrm_policy.c | 34 +++++++++++++++++----------------- 4 files changed, 21 insertions(+), 28 deletions(-)
include/net/xfrm.h +2 -3
@@ -280,7 +280,6 @@ struct net_device;struct xfrm_type ;struct xfrm_dst ;struct xfrm_policy_afinfo { -unsigned short family;struct dst_ops *dst_ops; struct dst_entry *(*dst_lookup)(struct net *net, int tos, int oif,
@@ -302,8 +301,8 @@ struct xfrm_policy_afinfo {struct dst_entry *(*blackhole_route)(struct net *net,struct dst_entry *orig); }; -int xfrm_policy_register_afinfo (struct xfrm_policy_afinfo *afinfo); -void xfrm_policy_unregister_afinfo (struct xfrm_policy_afinfo *afinfo); +int xfrm_policy_register_afinfo (const struct xfrm_policy_afinfo *afinfo,int family); +void xfrm_policy_unregister_afinfo (const struct xfrm_policy_afinfo *afinfo);void km_policy_notify (struct xfrm_policy *xp,int dir,const struct km_event *c);void km_state_notify (struct xfrm_state *x,const struct km_event *c);
net/ipv4/xfrm4_policy.c +1 -4
@@ -17,8 +17,6 @@#include <net/ip.h> #include <net/l3mdev.h> -static struct xfrm_policy_afinfo xfrm4_policy_afinfo; - static struct dst_entry *__xfrm4_dst_lookup(struct net *net, struct flowi4 *fl4, int tos, int oif, const xfrm_address_t *saddr,
@@ -272,7 +270,6 @@ static struct dst_ops xfrm4_dst_ops_template = { };static struct xfrm_policy_afinfo xfrm4_policy_afinfo = { - .family =AF_INET , .dst_ops = &xfrm4_dst_ops_template, .dst_lookup = xfrm4_dst_lookup, .get_saddr = xfrm4_get_saddr,
@@ -376,7 +373,7 @@ static struct pernet_operations __net_initdata xfrm4_net_ops = {static void __init xfrm4_policy_init (void ) { -xfrm_policy_register_afinfo (&xfrm4_policy_afinfo); +xfrm_policy_register_afinfo (&xfrm4_policy_afinfo,AF_INET ); }void __init xfrm4_init (void )
net/ipv6/xfrm6_policy.c +1 -4
@@ -25,8 +25,6 @@#include <net/mip6.h> #endif -static struct xfrm_policy_afinfo xfrm6_policy_afinfo; -static struct dst_entry *xfrm6_dst_lookup (struct net *net,int tos,int oif,const xfrm_address_t *saddr,const xfrm_address_t *daddr)
@@ -292,7 +290,6 @@ static struct dst_ops xfrm6_dst_ops_template = { };static struct xfrm_policy_afinfo xfrm6_policy_afinfo = { - .family =AF_INET6 , .dst_ops = &xfrm6_dst_ops_template, .dst_lookup = xfrm6_dst_lookup, .get_saddr = xfrm6_get_saddr,
@@ -305,7 +302,7 @@ static struct xfrm_policy_afinfo xfrm6_policy_afinfo = {static int __init xfrm6_policy_init (void ) { -return xfrm_policy_register_afinfo (&xfrm6_policy_afinfo); +return xfrm_policy_register_afinfo (&xfrm6_policy_afinfo,AF_INET6 ); }static void xfrm6_policy_fini (void )
net/xfrm/xfrm_policy.c +17 -17
@@ -45,7 +45,7 @@ struct xfrm_flo { };static DEFINE_SPINLOCK (xfrm_policy_afinfo_lock); -static struct xfrm_policy_afinfo __rcu *xfrm_policy_afinfo[NPROTO ] +static struct xfrm_policy_afinfo __rcu *xfrm_policy_afinfo[AF_INET6 +1 ]__read_mostly ;static struct kmem_cache *xfrm_dst_cache__read_mostly ;
@@ -103,11 +103,11 @@ bool xfrm_selector_match(const struct xfrm_selector *sel, const struct flowi *flreturn false; } -static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo (unsigned short family) +static const struct xfrm_policy_afinfo *xfrm_policy_get_afinfo (unsigned short family) { -struct xfrm_policy_afinfo *afinfo; +const struct xfrm_policy_afinfo *afinfo; -if (unlikely (family >=NPROTO )) +if (unlikely (family >=ARRAY_SIZE (xfrm_policy_afinfo)))return NULL ;rcu_read_lock (); afinfo =rcu_dereference (xfrm_policy_afinfo[family]);
@@ -2848,15 +2848,15 @@ static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,return dst->path->ops->neigh_lookup (dst, skb, daddr); } -int xfrm_policy_register_afinfo (struct xfrm_policy_afinfo *afinfo) +int xfrm_policy_register_afinfo (const struct xfrm_policy_afinfo *afinfo,int family) {int err =0 ; -if (unlikely (afinfo ==NULL )) -return -EINVAL ; -if (unlikely (afinfo->family >=NPROTO )) + +if (WARN_ON (family >=ARRAY_SIZE (xfrm_policy_afinfo)))return -EAFNOSUPPORT ; +spin_lock (&xfrm_policy_afinfo_lock); -if (unlikely (xfrm_policy_afinfo[afinfo->family] !=NULL )) +if (unlikely (xfrm_policy_afinfo[family] !=NULL )) err = -EEXIST ;else {struct dst_ops *dst_ops = afinfo->dst_ops;
@@ -2874,7 +2874,7 @@ int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo) dst_ops->link_failure = xfrm_link_failure;if (likely (dst_ops->neigh_lookup ==NULL )) dst_ops->neigh_lookup = xfrm_neigh_lookup; -rcu_assign_pointer (xfrm_policy_afinfo[afinfo->family], afinfo); +rcu_assign_pointer (xfrm_policy_afinfo[family], afinfo); }spin_unlock (&xfrm_policy_afinfo_lock);
@@ -2882,16 +2882,16 @@ int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo) }EXPORT_SYMBOL (xfrm_policy_register_afinfo); -void xfrm_policy_unregister_afinfo (struct xfrm_policy_afinfo *afinfo) +void xfrm_policy_unregister_afinfo (const struct xfrm_policy_afinfo *afinfo) {struct dst_ops *dst_ops = afinfo->dst_ops; +int i; -if (unlikely (afinfo->family >=NPROTO )) -return ; - -if (likely (xfrm_policy_afinfo[afinfo->family] != afinfo)) { -RCU_INIT_POINTER (xfrm_policy_afinfo[afinfo->family], -NULL ); +for (i =0 ; i <ARRAY_SIZE (xfrm_policy_afinfo); i++) { +if (xfrm_policy_afinfo[i] != afinfo) +continue ; +RCU_INIT_POINTER (xfrm_policy_afinfo[i],NULL ); +break ; }synchronize_rcu ();
Signature · 1 line
2.10.2
PATCH 5/6 xfrm: policy: remove xfrm_policy_put_afinfo
In reply to Florian WestphalAlternative is to keep it an make the (unused) afinfo arg const to avoid the compiler warnings once the afinfo structs get constified.
--- net/xfrm/xfrm_policy.c | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-)
net/xfrm/xfrm_policy.c +8 -13
@@ -116,11 +116,6 @@ static const struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short fareturn afinfo; } -static void xfrm_policy_put_afinfo (struct xfrm_policy_afinfo *afinfo) -{ -rcu_read_unlock (); -} - static inline struct dst_entry *__xfrm_dst_lookup(struct net *net, int tos, int oif, const xfrm_address_t *saddr,
@@ -136,7 +131,7 @@ static inline struct dst_entry *__xfrm_dst_lookup(struct net *net, dst = afinfo->dst_lookup (net, tos, oif, saddr, daddr); -xfrm_policy_put_afinfo (afinfo); +rcu_read_unlock ();return dst; }
@@ -1436,7 +1431,7 @@ xfrm_get_saddr(struct net *net, int oif, xfrm_address_t *local,if (unlikely (afinfo ==NULL ))return -EINVAL ; err = afinfo->get_saddr (net, oif, local, remote); -xfrm_policy_put_afinfo (afinfo); +rcu_read_unlock ();return err; }
@@ -1546,7 +1541,7 @@ static int xfrm_get_tos(const struct flowi *fl, int family) afinfo =xfrm_policy_get_afinfo (family); tos = afinfo ? afinfo->get_tos (fl) :0 ; -xfrm_policy_put_afinfo (afinfo); +rcu_read_unlock ();return tos; }
@@ -1632,7 +1627,7 @@ static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family) }else xdst =ERR_PTR (-ENOBUFS ); -xfrm_policy_put_afinfo (afinfo); +rcu_read_unlock ();return xdst; }
@@ -1649,7 +1644,7 @@ static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst, err = afinfo->init_path (path, dst, nfheader_len); -xfrm_policy_put_afinfo (afinfo); +rcu_read_unlock ();return err; }
@@ -1666,7 +1661,7 @@ static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev, err = afinfo->fill_dst (xdst, dev, fl); -xfrm_policy_put_afinfo (afinfo); +rcu_read_unlock ();return err; }
@@ -2215,7 +2210,7 @@ static struct dst_entry *make_blackhole(struct net *net, u16 family, }else { ret = afinfo->blackhole_route (net, dst_orig); } -xfrm_policy_put_afinfo (afinfo); +rcu_read_unlock ();return ret; }
@@ -2465,7 +2460,7 @@ int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, afinfo->decode_session (skb, fl, reverse); err =security_xfrm_decode_session (skb, &fl->flowi_secid); -xfrm_policy_put_afinfo (afinfo); +rcu_read_unlock ();return err; }EXPORT_SYMBOL (__xfrm_decode_session);
Signature · 1 line
2.10.2
PATCH 6/6 xfrm: policy: make policy backend const
In reply to Florian Westphal--- net/ipv4/xfrm4_policy.c | 2 +- net/ipv6/xfrm6_policy.c | 2 +- net/xfrm/xfrm_policy.c | 18 +++++++++--------- 3 files changed, 11 insertions(+), 11 deletions(-)
net/ipv4/xfrm4_policy.c +1 -1
@@ -269,7 +269,7 @@ static struct dst_ops xfrm4_dst_ops_template = { .gc_thresh = INT_MAX, }; -static struct xfrm_policy_afinfo xfrm4_policy_afinfo = { +static const struct xfrm_policy_afinfo xfrm4_policy_afinfo = { .dst_ops = &xfrm4_dst_ops_template, .dst_lookup = xfrm4_dst_lookup, .get_saddr = xfrm4_get_saddr,
net/ipv6/xfrm6_policy.c +1 -1
@@ -289,7 +289,7 @@ static struct dst_ops xfrm6_dst_ops_template = { .gc_thresh = INT_MAX, }; -static struct xfrm_policy_afinfo xfrm6_policy_afinfo = { +static const struct xfrm_policy_afinfo xfrm6_policy_afinfo = { .dst_ops = &xfrm6_dst_ops_template, .dst_lookup = xfrm6_dst_lookup, .get_saddr = xfrm6_get_saddr,
net/xfrm/xfrm_policy.c +9 -9
@@ -45,7 +45,7 @@ struct xfrm_flo { };static DEFINE_SPINLOCK (xfrm_policy_afinfo_lock); -static struct xfrm_policy_afinfo __rcu *xfrm_policy_afinfo[AF_INET6 +1 ] +static struct xfrm_policy_afinfo const __rcu *xfrm_policy_afinfo[AF_INET6 +1 ]__read_mostly ;static struct kmem_cache *xfrm_dst_cache__read_mostly ;
@@ -122,7 +122,7 @@ static inline struct dst_entry *__xfrm_dst_lookup(struct net *net, const xfrm_address_t *daddr, int family) { -struct xfrm_policy_afinfo *afinfo; +const struct xfrm_policy_afinfo *afinfo;struct dst_entry *dst; afinfo =xfrm_policy_get_afinfo (family);
@@ -1426,7 +1426,7 @@ xfrm_get_saddr(struct net *net, int oif, xfrm_address_t *local, xfrm_address_t *remote, unsigned short family) { int err; -struct xfrm_policy_afinfo *afinfo =xfrm_policy_get_afinfo (family); +const struct xfrm_policy_afinfo *afinfo =xfrm_policy_get_afinfo (family);if (unlikely (afinfo ==NULL ))return -EINVAL ;
@@ -1535,7 +1535,7 @@ xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,static int xfrm_get_tos (const struct flowi *fl,int family) { -struct xfrm_policy_afinfo *afinfo; +const struct xfrm_policy_afinfo *afinfo;int tos =0 ; afinfo =xfrm_policy_get_afinfo (family);
@@ -1598,7 +1598,7 @@ static const struct flow_cache_ops xfrm_bundle_fc_ops = {static inline struct xfrm_dst *xfrm_alloc_dst (struct net *net,int family) { -struct xfrm_policy_afinfo *afinfo =xfrm_policy_get_afinfo (family); +const struct xfrm_policy_afinfo *afinfo =xfrm_policy_get_afinfo (family);struct dst_ops *dst_ops;struct xfrm_dst *xdst;
@@ -1635,7 +1635,7 @@ static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)static inline int xfrm_init_path (struct xfrm_dst *path,struct dst_entry *dst,int nfheader_len) { -struct xfrm_policy_afinfo *afinfo = +const struct xfrm_policy_afinfo *afinfo =xfrm_policy_get_afinfo (dst->ops->family);int err;
@@ -1652,7 +1652,7 @@ static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,static inline int xfrm_fill_dst (struct xfrm_dst *xdst,struct net_device *dev,const struct flowi *fl) { -struct xfrm_policy_afinfo *afinfo = +const struct xfrm_policy_afinfo *afinfo =xfrm_policy_get_afinfo (xdst->u.dst.ops->family);int err;
@@ -2201,7 +2201,7 @@ xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir,static struct dst_entry *make_blackhole (struct net *net,u16 family,struct dst_entry *dst_orig) { -struct xfrm_policy_afinfo *afinfo =xfrm_policy_get_afinfo (family); +const struct xfrm_policy_afinfo *afinfo =xfrm_policy_get_afinfo (family);struct dst_entry *ret;if (!afinfo) {
@@ -2452,7 +2452,7 @@ xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int starint __xfrm_decode_session (struct sk_buff *skb,struct flowi *fl,unsigned int family,int reverse) { -struct xfrm_policy_afinfo *afinfo =xfrm_policy_get_afinfo (family); +const struct xfrm_policy_afinfo *afinfo =xfrm_policy_get_afinfo (family);int err;if (unlikely (afinfo ==NULL ))
Signature · 1 line
2.10.2
Steffen Klassert
maintainerQuoted 4 lines · Florian Westphal
Hi Steffen,
This series removes all places that need to write to the afinfo policy backends. This then allows us to make the structures const.
All applied to ipsec-next, thanks a lot Florian!