remove dst garbage collector logic
Subsystems (6)
v2 cover remove dst garbage collector logic
From: Wei Wang <[email protected]>
The current mechanism of dst release is a bit complicated. It is because the users of dst get divided into 2 situations:
1. Most users take the reference count when using a dst and release the reference count when done.
2. Exceptional users like IPv4/IPv6/decnet/xfrm routing code do not take reference count when referencing to a dst due to some histotic reasons.
Due to those exceptional use cases in 2, reference count being 0 is not an adequate evidence to indicate that no user is using this dst. So users in 1 can't free the dst simply based on reference count being 0 because users in 2 might still hold reference to it.
Instead, a dst garbage list is needed to hold the dst entries that already get removed by the users in 2 but are still held by users in 1. And a periodic garbage collector task is run to check all the dst entries in the list to see if the users in 1 have released the reference to those dst entries. If so, the dst is now ready to be freed.
This logic introduces unnecessary complications in the dst code which makes it hard to understand and to debug.
In order to get rid of the whole dst garbage collector (gc) and make the dst code more unified and simplified, we can make the users in 2 also take reference count on the dst and release it properly when done.
This way, dst can be safely freed once the refcount drops to 0 and no gc thread is needed anymore.
This patch series' target is to completely get rid of dst gc logic and free dst based on reference count only.
Patch 1-3 are preparation patches to do some cleanup/improvement on the existing code to make later work easier.
Patch 4-21 are real implementations.
In these patches, a temporary flag DST_NOGC is used to help transition those exceptional users one by one. Once every component is transitioned, this temporary flag is removed.
By the end of this patch series, all dst are refcounted when being used and released when done. And dst will be freed when its refcount drops to 0. No dst gc task is running anymore.
Note: This patch series depends on the decnet fix that was sent right before:
"decnet: always not take dst->__refcnt when inserting dst into hash table"
v2:
add curly braces in udp_v4/6_early_demux() in patch 02 add EXPORT_SYMBOL() for dst_dev_put() in patch 05
Wei Wang (21):
ipv6: remove unnecessary dst_hold() in ip6_fragment()
udp: call dst_hold_safe() in udp_sk_rx_set_dst()
net: use loopback dev when generating blackhole route
net: introduce DST_NOGC in dst_release() to destroy dst based on
refcnt
net: introduce a new function dst_dev_put()
ipv4: take dst->__refcnt when caching dst in fib
ipv4: call dst_dev_put() properly
ipv4: call dst_hold_safe() properly
ipv4: mark DST_NOGC and remove the operation of dst_free()
ipv6: take dst->__refcnt for insertion into fib6 tree
ipv6: call dst_dev_put() properly
ipv6: call dst_hold_safe() properly
ipv6: mark DST_NOGC and remove the operation of dst_free()
ipv6: get rid of icmp6 dst garbage collector
xfrm: take refcnt of dst when creating struct xfrm_dst bundle
decnet: take dst->__refcnt when struct dn_route is created
net: remove dst gc related code
net: remove DST_NOGC flag
net: remove DST_NOCACHE flag
net: reorder all the dst flags
net: add debug atomic_inc_not_zero() in dst_hold()
drivers/net/vrf.c | 6 +- include/net/dst.h | 43 ++------ include/net/ip6_fib.h | 2 +- include/net/ip6_route.h | 1 - include/net/route.h | 4 +- net/core/dev.c | 1 - net/core/dst.c | 276 ++++++++--------------------------------------- net/decnet/dn_route.c | 34 +++--- net/ipv4/fib_semantics.c | 9 +- net/ipv4/route.c | 62 ++++++----- net/ipv4/udp.c | 19 ++-- net/ipv6/addrconf.c | 4 +- net/ipv6/ip6_fib.c | 32 +++--- net/ipv6/ip6_output.c | 4 - net/ipv6/route.c | 127 ++++++++-------------- net/ipv6/udp.c | 11 +- net/xfrm/xfrm_policy.c | 49 +++++---- 17 files changed, 218 insertions(+), 466 deletions(-)
Signature · 1 line
2.13.1.518.g3df882009-goog
PATCH v2 1/21 ipv6: remove unnecessary dst_hold() in ip6_fragment()
From: Wei Wang <[email protected]>
In ipv6 tx path, rcu_read_lock() is taken so that dst won't get freed during the execution of ip6_fragment(). Hence, no need to hold dst in it.
--- net/ipv6/ip6_output.c | 4 ---- 1 file changed, 4 deletions(-)
net/ipv6/ip6_output.c +0 -4
@@ -698,8 +698,6 @@ int ip6_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,ipv6_hdr (skb)->payload_len =htons (first_len -sizeof (struct ipv6hdr )); -dst_hold (&rt->dst); -for (;;) {/* Prepare header of the next frame, * before previous one went down. */
@@ -742,7 +740,6 @@ int ip6_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,if (err ==0 ) {IP6_INC_STATS (net,ip6_dst_idev (&rt->dst),IPSTATS_MIB_FRAGOKS ); -ip6_rt_put (rt);return 0 ; }
@@ -750,7 +747,6 @@ int ip6_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,IP6_INC_STATS (net,ip6_dst_idev (&rt->dst),IPSTATS_MIB_FRAGFAILS ); -ip6_rt_put (rt);return err; slow_path_clean:
Signature · 1 line
2.13.1.518.g3df882009-goog
PATCH v2 2/21 udp: call dst_hold_safe() in udp_sk_rx_set_dst()
In reply to Wei WangFrom: Wei Wang <[email protected]>
In udp_v4/6_early_demux() code, we try to hold dst->__refcnt for dst with DST_NOCACHE flag. This is because later in udp_sk_rx_dst_set() function, we will try to cache this dst in sk for connected case. However, a better way to achieve this is to not try to hold dst in early_demux(), but in udp_sk_rx_dst_set(), call dst_hold_safe(). This approach is also more consistant with how tcp is handling it. And it will make later changes simpler.
--- net/ipv4/udp.c | 19 +++++++++---------- net/ipv6/udp.c | 11 +++++------ 2 files changed, 14 insertions(+), 16 deletions(-)
net/ipv4/udp.c +9 -10
@@ -1977,9 +1977,10 @@ static void udp_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst) {struct dst_entry *old; -dst_hold (dst); - old =xchg (&sk->sk_rx_dst, dst); -dst_release (old); +if (dst_hold_safe (dst)) { + old =xchg (&sk->sk_rx_dst, dst); +dst_release (old); + } }/*
@@ -2303,13 +2304,11 @@ void udp_v4_early_demux(struct sk_buff *skb)if (dst) dst =dst_check (dst,0 );if (dst) { -/* DST_NOCACHE can not be used without taking a reference */ -if (dst->flags &DST_NOCACHE ) { -if (likely (atomic_inc_not_zero (&dst->__refcnt))) -skb_dst_set (skb, dst); - }else { -skb_dst_set_noref (skb, dst); - } +/* set noref for now. +* any place which wants to hold dst has to call +* dst_hold_safe() +*/ +skb_dst_set_noref (skb, dst); } }
net/ipv6/udp.c +5 -6
@@ -920,12 +920,11 @@ static void udp_v6_early_demux(struct sk_buff *skb)if (dst) dst =dst_check (dst,inet6_sk (sk)->rx_dst_cookie);if (dst) { -if (dst->flags &DST_NOCACHE ) { -if (likely (atomic_inc_not_zero (&dst->__refcnt))) -skb_dst_set (skb, dst); - }else { -skb_dst_set_noref (skb, dst); - } +/* set noref for now. +* any place which wants to hold dst has to call +* dst_hold_safe() +*/ +skb_dst_set_noref (skb, dst); } }
Signature · 1 line
2.13.1.518.g3df882009-goog
PATCH v2 5/21 net: introduce a new function dst_dev_put()
In reply to Wei WangFrom: Wei Wang <[email protected]>
This function should be called when removing routes from fib tree after the dst gc is no longer in use.
We first mark DST_OBSOLETE_DEAD on this dst to make sure next dst_ops->check() fails and returns NULL.
Secondly, as we no longer keep the gc_list, we need to properly release dst->dev right at the moment when the dst is removed from the fib/fib6 tree.
It does the following:
1. change dst->input and output pointers to dst_discard/dst_dscard_out to discard all packets
2. replace dst->dev with loopback interface
--- include/net/dst.h | 1 + net/core/dst.c | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+)
include/net/dst.h +1 -0
@@ -428,6 +428,7 @@ void dst_init(struct dst_entry *dst, struct dst_ops *ops, unsigned short flags);void __dst_free (struct dst_entry *dst);struct dst_entry *dst_destroy (struct dst_entry *dst); +void dst_dev_put (struct dst_entry *dst);static inline void dst_free (struct dst_entry *dst) {
net/core/dst.c +24 -0
@@ -296,6 +296,30 @@ static void dst_destroy_rcu(struct rcu_head *head)__dst_free (dst); } +/* Operations to mark dst as DEAD and clean up the net device referenced +* by dst: +* 1. put the dst under loopback interface and discard all tx/rx packets +* on this route. +* 2. release the net_device +* This function should be called when removing routes from the fib tree +* in preparation for a NETDEV_DOWN/NETDEV_UNREGISTER event and also to +* make the next dst_ops->check() fail. +*/ +void dst_dev_put (struct dst_entry *dst) +{ +struct net_device *dev = dst->dev; + + dst->obsolete =DST_OBSOLETE_DEAD ; +if (dst->ops->ifdown) + dst->ops->ifdown (dst, dev, true); + dst->input = dst_discard; + dst->output = dst_discard_out; + dst->dev =dev_net (dst->dev)->loopback_dev; +dev_hold (dst->dev); +dev_put (dev); +} +EXPORT_SYMBOL (dst_dev_put); +void dst_release (struct dst_entry *dst) {if (dst) {
Signature · 1 line
2.13.1.518.g3df882009-goog
PATCH v2 6/21 ipv4: take dst->__refcnt when caching dst in fib
In reply to Wei WangFrom: Wei Wang <[email protected]>
In IPv4 routing code, fib_nh and fib_nh_exception can hold pointers to struct rtable but they never increment dst->__refcnt. This leads to the need of the dst garbage collector because when user is done with this dst and calls dst_release(), it can only decrement dst->__refcnt and can not free the dst even it sees dst->__refcnt drops from 1 to 0 (unless DST_NOCACHE flag is set) because the routing code might still hold reference to it.
And when the routing code tries to delete a route, it has to put the dst to the gc_list if dst->__refcnt is not yet 0 and have a gc thread running periodically to check on dst->__refcnt and finally to free dst when refcnt becomes 0.
This patch increments dst->__refcnt when fib_nh/fib_nh_exception holds reference to this dst and properly release the dst when fib_nh/fib_nh_exception has been updated with a new dst.
This patch is a preparation in order to fully get rid of dst gc later.
--- net/ipv4/fib_semantics.c | 5 ++++- net/ipv4/route.c | 19 ++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-)
net/ipv4/fib_semantics.c +4 -1
@@ -152,6 +152,7 @@ static void rt_fibinfo_free(struct rtable __rcu **rtp)* free_fib_info_rcu() */ +dst_release (&rt->dst);dst_free (&rt->dst); }
@@ -194,8 +195,10 @@ static void rt_fibinfo_free_cpus(struct rtable __rcu * __percpu *rtp)struct rtable *rt; rt =rcu_dereference_protected (*per_cpu_ptr (rtp, cpu),1 ); -if (rt) +if (rt) { +dst_release (&rt->dst);dst_free (&rt->dst); + } }free_percpu (rtp); }
net/ipv4/route.c +16 -3
@@ -603,11 +603,13 @@ static void fnhe_flush_routes(struct fib_nh_exception *fnhe) rt =rcu_dereference (fnhe->fnhe_rth_input);if (rt) {RCU_INIT_POINTER (fnhe->fnhe_rth_input,NULL ); +dst_release (&rt->dst);rt_free (rt); } rt =rcu_dereference (fnhe->fnhe_rth_output);if (rt) {RCU_INIT_POINTER (fnhe->fnhe_rth_output,NULL ); +dst_release (&rt->dst);rt_free (rt); } }
@@ -1332,9 +1334,12 @@ static bool rt_bind_exception(struct rtable *rt, struct fib_nh_exception *fnhe, rt->rt_gateway = daddr;if (!(rt->dst.flags &DST_NOCACHE )) { +dst_hold (&rt->dst);rcu_assign_pointer (*porig, rt); -if (orig) +if (orig) { +dst_release (&orig->dst);rt_free (orig); + } ret = true; }
@@ -1357,12 +1362,20 @@ static bool rt_cache_route(struct fib_nh *nh, struct rtable *rt) } orig = *p; +/* hold dst before doing cmpxchg() to avoid race condition +* on this dst +*/ +dst_hold (&rt->dst); prev =cmpxchg (p, orig, rt);if (prev == orig) { -if (orig) +if (orig) { +dst_release (&orig->dst);rt_free (orig); - }else + } + }else { +dst_release (&rt->dst); ret = false; + }return ret; }
Signature · 1 line
2.13.1.518.g3df882009-goog
PATCH v2 7/21 ipv4: call dst_dev_put() properly
In reply to Wei WangFrom: Wei Wang <[email protected]>
As the intend of this patch series is to completely remove dst gc, we need to call dst_dev_put() to release the reference to dst->dev when removing routes from fib because we won't keep the gc list anymore and will lose the dst pointer right after removing the routes. Without the gc list, there is no way to find all the dst's that have dst->dev pointing to the going-down dev.
Hence, we are doing dst_dev_put() immediately before we lose the last reference of the dst from the routing code. The next dst_check() will trigger a route re-lookup to find another route (if there is any).
--- net/ipv4/fib_semantics.c | 2 ++ net/ipv4/route.c | 4 ++++ 2 files changed, 6 insertions(+)
net/ipv4/fib_semantics.c +2 -0
@@ -152,6 +152,7 @@ static void rt_fibinfo_free(struct rtable __rcu **rtp)* free_fib_info_rcu() */ +dst_dev_put (&rt->dst);dst_release (&rt->dst);dst_free (&rt->dst); }
@@ -196,6 +197,7 @@ static void rt_fibinfo_free_cpus(struct rtable __rcu * __percpu *rtp) rt =rcu_dereference_protected (*per_cpu_ptr (rtp, cpu),1 );if (rt) { +dst_dev_put (&rt->dst);dst_release (&rt->dst);dst_free (&rt->dst); }
net/ipv4/route.c +4 -0
@@ -603,12 +603,14 @@ static void fnhe_flush_routes(struct fib_nh_exception *fnhe) rt =rcu_dereference (fnhe->fnhe_rth_input);if (rt) {RCU_INIT_POINTER (fnhe->fnhe_rth_input,NULL ); +dst_dev_put (&rt->dst);dst_release (&rt->dst);rt_free (rt); } rt =rcu_dereference (fnhe->fnhe_rth_output);if (rt) {RCU_INIT_POINTER (fnhe->fnhe_rth_output,NULL ); +dst_dev_put (&rt->dst);dst_release (&rt->dst);rt_free (rt); }
@@ -1337,6 +1339,7 @@ static bool rt_bind_exception(struct rtable *rt, struct fib_nh_exception *fnhe,dst_hold (&rt->dst);rcu_assign_pointer (*porig, rt);if (orig) { +dst_dev_put (&orig->dst);dst_release (&orig->dst);rt_free (orig); }
@@ -1369,6 +1372,7 @@ static bool rt_cache_route(struct fib_nh *nh, struct rtable *rt) prev =cmpxchg (p, orig, rt);if (prev == orig) {if (orig) { +dst_dev_put (&orig->dst);dst_release (&orig->dst);rt_free (orig); }
Signature · 1 line
2.13.1.518.g3df882009-goog
PATCH v2 8/21 ipv4: call dst_hold_safe() properly
In reply to Wei WangFrom: Wei Wang <[email protected]>
This patch checks all the calls to dst_hold()/skb_dst_force()/dst_clone()/dst_use() to see if dst_hold_safe() is needed to avoid double free issue if dst gc is removed and dst_release() directly destroys dst when dst->__refcnt drops to 0.
In tx path, TCP hold sk->sk_rx_dst ref count and also hold sock_lock(). UDP and other similar protocols always hold refcount for skb->_skb_refdst. So both paths seem to be safe.
In rx path, as it is lockless and skb_dst_set_noref() is likely to be used, dst_hold_safe() should always be used when trying to hold dst.
In the routing code, if dst is held during an rcu protected session, it is necessary to call dst_hold_safe() as the current dst might be in its rcu grace period.
--- include/net/route.h | 4 +++- net/ipv4/route.c | 4 +--- 2 files changed, 4 insertions(+), 4 deletions(-)
include/net/route.h +3 -1
@@ -190,7 +190,9 @@ static inline int ip_route_input(struct sk_buff *skb, __be32 dst, __be32 src,rcu_read_lock (); err =ip_route_input_noref (skb, dst, src, tos, devin);if (!err) -skb_dst_force (skb); +skb_dst_force_safe (skb); +if (!skb_dst (skb)) + err = -EINVAL ;rcu_read_unlock ();return err;
net/ipv4/route.c +1 -3
@@ -2234,10 +2234,8 @@ static struct rtable *__mkroute_output(const struct fib_result *res, rth =rcu_dereference (*prth); rt_cache: -if (rt_cache_valid (rth)) { -dst_hold (&rth->dst); +if (rt_cache_valid (rth) &&dst_hold_safe (&rth->dst))return rth; - } } add:
Signature · 1 line
2.13.1.518.g3df882009-goog
PATCH v2 3/21 net: use loopback dev when generating blackhole route
In reply to Wei WangFrom: Wei Wang <[email protected]>
Existing ipv4/6_blackhole_route() code generates a blackhole route with dst->dev pointing to the passed in dst->dev.
It is not necessary to hold reference to the passed in dst->dev because the packets going through this route are dropped anyway. A loopback interface is good enough so that we don't need to worry about releasing this dst->dev when this dev is going down.
--- net/ipv4/route.c | 2 +- net/ipv6/route.c | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-)
net/ipv4/route.c +1 -1
@@ -2504,7 +2504,7 @@ struct dst_entry *ipv4_blackhole_route(struct net *net, struct dst_entry *dst_or new->input = dst_discard; new->output = dst_discard_out; - new->dev = ort->dst.dev; + new->dev = net->loopback_dev;if (new->dev)dev_hold (new->dev);
net/ipv6/route.c +5 -4
@@ -1245,9 +1245,12 @@ EXPORT_SYMBOL_GPL(ip6_route_output_flags);struct dst_entry *ip6_blackhole_route (struct net *net,struct dst_entry *dst_orig) {struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; +struct net_device *loopback_dev = net->loopback_dev;struct dst_entry *new =NULL ; - rt =dst_alloc (&ip6_dst_blackhole_ops, ort->dst.dev,1 ,DST_OBSOLETE_NONE ,0 ); + + rt =dst_alloc (&ip6_dst_blackhole_ops, loopback_dev,1 , +DST_OBSOLETE_NONE ,0 );if (rt) {rt6_info_init (rt);
@@ -1257,10 +1260,8 @@ struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_ori new->output = dst_discard_out;dst_copy_metrics (new, &ort->dst); - rt->rt6i_idev = ort->rt6i_idev; -if (rt->rt6i_idev) -in6_dev_hold (rt->rt6i_idev); + rt->rt6i_idev =in6_dev_get (loopback_dev); rt->rt6i_gateway = ort->rt6i_gateway; rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU ; rt->rt6i_metric =0 ;
Signature · 1 line
2.13.1.518.g3df882009-goog
PATCH v2 12/21 ipv6: call dst_hold_safe() properly
In reply to Wei WangFrom: Wei Wang <[email protected]>
Similar as ipv4, ipv6 path also needs to call dst_hold_safe() when necessary to avoid double free issue on the dst.
--- net/ipv6/addrconf.c | 4 ++-- net/ipv6/route.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)
net/ipv6/addrconf.c +2 -2
@@ -5576,8 +5576,8 @@ static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)ip6_del_rt (rt); }if (ifp->rt) { -dst_hold (&ifp->rt->dst); -ip6_del_rt (ifp->rt); +if (dst_hold_safe (&ifp->rt->dst)) +ip6_del_rt (ifp->rt); }rt_genid_bump_ipv6 (net);break ;
net/ipv6/route.c +2 -2
@@ -1366,8 +1366,8 @@ static void ip6_link_failure(struct sk_buff *skb) rt = (struct rt6_info *)skb_dst (skb);if (rt) {if (rt->rt6i_flags &RTF_CACHE ) { -dst_hold (&rt->dst); -ip6_del_rt (rt); +if (dst_hold_safe (&rt->dst)) +ip6_del_rt (rt); }else if (rt->rt6i_node && (rt->rt6i_flags &RTF_DEFAULT )) { rt->rt6i_node->fn_sernum =-1 ; }
Signature · 1 line
2.13.1.518.g3df882009-goog
PATCH v2 13/21 ipv6: mark DST_NOGC and remove the operation of dst_free()
In reply to Wei WangFrom: Wei Wang <[email protected]>
With the previous preparation patches, we are ready to get rid of the dst gc operation in ipv6 code and release dst based on refcnt only. So this patch adds DST_NOGC flag for all IPv6 dst and remove the calls to dst_free() and its related functions.
At this point, all dst created in ipv6 code do not use the dst gc anymore and will be destroyed at the point when refcnt drops to 0.
Also, as icmp6 dst route is refcounted during creation and will be freed by user during its call of dst_release(), there is no need to add this dst to the icmp6 gc list as well.
Instead, we need to add it into uncached list so that when a NETDEV_DOWN/NETDEV_UNREGISRER event comes, we can properly go through these icmp6 dst as well and release the net device properly.
--- net/ipv6/ip6_fib.c | 15 ++------------- net/ipv6/route.c | 49 +++++++++++++++++-------------------------------- 2 files changed, 19 insertions(+), 45 deletions(-)
net/ipv6/ip6_fib.c +2 -13
@@ -153,11 +153,6 @@ static void node_free(struct fib6_node *fn)kmem_cache_free (fib6_node_kmem, fn); } -static void rt6_rcu_free (struct rt6_info *rt) -{ -call_rcu (&rt->dst.rcu_head, dst_rcu_free); -} -static void rt6_free_pcpu (struct rt6_info *non_pcpu_rt) {int cpu;
@@ -174,7 +169,6 @@ static void rt6_free_pcpu(struct rt6_info *non_pcpu_rt)if (pcpu_rt) {dst_dev_put (&pcpu_rt->dst);dst_release (&pcpu_rt->dst); -rt6_rcu_free (pcpu_rt); *ppcpu_rt =NULL ; } }
@@ -189,7 +183,6 @@ static void rt6_release(struct rt6_info *rt)rt6_free_pcpu (rt);dst_dev_put (&rt->dst);dst_release (&rt->dst); -rt6_rcu_free (rt); } }
@@ -1108,9 +1101,7 @@ int fib6_add(struct fib6_node *root, struct rt6_info *rt,/* Always release dst as dst->__refcnt is guaranteed * to be taken before entering this function */ -dst_release (&rt->dst); -if (!(rt->dst.flags &DST_NOCACHE )) -dst_free (&rt->dst); +dst_release_immediate (&rt->dst); }return err;
@@ -1124,9 +1115,7 @@ int fib6_add(struct fib6_node *root, struct rt6_info *rt,/* Always release dst as dst->__refcnt is guaranteed * to be taken before entering this function */ -dst_release (&rt->dst); -if (!(rt->dst.flags &DST_NOCACHE )) -dst_free (&rt->dst); +dst_release_immediate (&rt->dst);return err;#endif }
net/ipv6/route.c +17 -32
116 lines not on this page · raw message
Signature · 1 line
2.13.1.518.g3df882009-goog
PATCH v2 4/21 net: introduce DST_NOGC in dst_release() to destroy dst based on refcnt
In reply to Wei WangFrom: Wei Wang <[email protected]>
The current mechanism of freeing dst is a bit complicated. dst has its ref count and when user grabs the reference to the dst, the ref count is properly taken in most cases except in IPv4/IPv6/decnet/xfrm routing code due to some historic reasons.
If the reference to dst is always taken properly, we should be able to simplify the logic in dst_release() to destroy dst when dst->__refcnt drops from 1 to 0. And this should be the only condition to determine if we can call dst_destroy().
And as dst is always ref counted, there is no need for a dst garbage list to hold the dst entries that already get removed by the routing code but are still held by other users. And the task to periodically check the list to free dst if ref count become 0 is also not needed anymore.
This patch introduces a temporary flag DST_NOGC(no garbage collector). If it is set in the dst, dst_release() will call dst_destroy() when dst->__refcnt drops to 0. dst_hold_safe() will also check for this flag and do atomic_inc_not_zero() similar as DST_NOCACHE to avoid double free issue.
This temporary flag is mainly used so that we can make the transition component by component without breaking other parts.
This flag will be removed after all components are properly transitioned.
This patch also introduces a new function dst_release_immediate() which destroys dst without waiting on the rcu when refcnt drops to 0. It will be used in later patches.
Follow-up patches will correct all the places to properly take ref count on dst and mark DST_NOGC. dst_release() or dst_release_immediate() will be used to release the dst instead of dst_free() and its related functions.
And final clean-up patch will remove the DST_NOGC flag.
--- include/net/dst.h | 5 ++++- net/core/dst.c | 20 ++++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-)
include/net/dst.h +4 -1
@@ -58,6 +58,7 @@ struct dst_entry {#define DST_XFRM_TUNNEL 0x0080#define DST_XFRM_QUEUE 0x0100#define DST_METADATA 0x0200 +#define DST_NOGC 0x0400short error;
@@ -278,6 +279,8 @@ static inline struct dst_entry *dst_clone(struct dst_entry *dst)void dst_release (struct dst_entry *dst); +void dst_release_immediate (struct dst_entry *dst); +static inline void refdst_drop (unsigned long refdst) {if (!(refdst &SKB_DST_NOREF ))
@@ -334,7 +337,7 @@ static inline void skb_dst_force(struct sk_buff *skb)*/ static inline bool dst_hold_safe (struct dst_entry *dst) { -if (dst->flags &DST_NOCACHE ) +if (dst->flags & (DST_NOCACHE |DST_NOGC ))return atomic_inc_not_zero (&dst->__refcnt);dst_hold (dst);return true;
net/core/dst.c +18 -2
@@ -300,18 +300,34 @@ void dst_release(struct dst_entry *dst) {if (dst) {int newrefcnt; -unsigned short nocache = dst->flags &DST_NOCACHE ; +unsigned short destroy_after_rcu = dst->flags & + (DST_NOCACHE |DST_NOGC ); newrefcnt =atomic_dec_return (&dst->__refcnt);if (unlikely (newrefcnt <0 ))net_warn_ratelimited ("%s: dst:%p refcnt:%d\n" , __func__, dst, newrefcnt); -if (!newrefcnt &&unlikely (nocache)) +if (!newrefcnt &&unlikely (destroy_after_rcu))call_rcu (&dst->rcu_head, dst_destroy_rcu); } }EXPORT_SYMBOL (dst_release); +void dst_release_immediate (struct dst_entry *dst) +{ +if (dst) { +int newrefcnt; + + newrefcnt =atomic_dec_return (&dst->__refcnt); +if (unlikely (newrefcnt <0 )) +net_warn_ratelimited ("%s: dst:%p refcnt:%d\n" , + __func__, dst, newrefcnt); +if (!newrefcnt) +dst_destroy (dst); + } +} +EXPORT_SYMBOL (dst_release_immediate); +u32 *dst_cow_metrics_generic (struct dst_entry *dst,unsigned long old) {struct dst_metrics *p =kmalloc (sizeof (*p),GFP_ATOMIC );
Signature · 1 line
2.13.1.518.g3df882009-goog
PATCH v2 15/21 xfrm: take refcnt of dst when creating struct xfrm_dst bundle
In reply to Wei WangFrom: Wei Wang <[email protected]>
During the creation of xfrm_dst bundle, always take ref count when allocating the dst. This way, xfrm_bundle_create() will form a linked list of dst with dst->child pointing to a ref counted dst child. And the returned dst pointer is also ref counted. This makes the link from the flow cache to this dst now ref counted properly.
As the dst is always ref counted properly, we can safely mark DST_NOGC flag so dst_release() will release dst based on refcnt only. And dst gc is no longer needed and all dst_free() and its related function calls should be replaced with dst_release() or dst_release_immediate().
The special handling logic for dst->child in dst_destroy() can be replaced with a simple dst_release_immediate() call on the child to release the whole list linked by dst->child pointer. Previously used DST_NOHASH flag is not needed anymore as well. The reason that DST_NOHASH is used in the existing code is mainly to prevent the dst inserted in the fib tree to be wrongly destroyed during the deletion of the xfrm_dst bundle. So in the existing code, DST_NOHASH flag is marked in all the dst children except the one which is in the fib tree.
However, with this patch series to remove dst gc logic and release dst only based on ref count, it is safe to release all the children from a xfrm_dst bundle as long as the dst children are all ref counted properly which is already the case in the existing code.
So, this patch removes the use of DST_NOHASH flag.
--- include/net/dst.h | 1 - net/core/dst.c | 19 ++----------------- net/xfrm/xfrm_policy.c | 48 ++++++++++++++++++++++++++++++------------------ 3 files changed, 32 insertions(+), 36 deletions(-)
include/net/dst.h +0 -1
@@ -51,7 +51,6 @@ struct dst_entry {#define DST_HOST 0x0001#define DST_NOXFRM 0x0002#define DST_NOPOLICY 0x0004 -#define DST_NOHASH 0x0008#define DST_NOCACHE 0x0010#define DST_NOCOUNT 0x0020#define DST_FAKE_RTABLE 0x0040
net/core/dst.c +2 -17
@@ -250,7 +250,6 @@ struct dst_entry *dst_destroy(struct dst_entry * dst)smp_rmb (); -again: child = dst->child;if (!(dst->flags &DST_NOCOUNT ))
@@ -269,20 +268,8 @@ struct dst_entry *dst_destroy(struct dst_entry * dst)kmem_cache_free (dst->ops->kmem_cachep, dst); dst = child; -if (dst) { -int nohash = dst->flags &DST_NOHASH ; - -if (atomic_dec_and_test (&dst->__refcnt)) { -/* We were real parent of this dst, so kill child. */ -if (nohash) - goto again; - }else { -/* Child is still referenced, return it for freeing. */ -if (nohash) -return dst; -/* Child is still in his hash table */ - } - } +if (dst) +dst_release_immediate (dst);return NULL ; }EXPORT_SYMBOL (dst_destroy);
@@ -292,8 +279,6 @@ static void dst_destroy_rcu(struct rcu_head *head) struct dst_entry *dst = container_of(head, struct dst_entry, rcu_head); dst =dst_destroy (dst); -if (dst) -__dst_free (dst); }/* Operations to mark dst as DEAD and clean up the net device referenced
net/xfrm/xfrm_policy.c +30 -18
98 lines not on this page · raw message
Signature · 1 line
2.13.1.518.g3df882009-goog
PATCH v2 18/21 net: remove DST_NOGC flag
In reply to Wei WangFrom: Wei Wang <[email protected]>
Now that all the components have been changed to release dst based on refcnt only and not depend on dst gc anymore, we can remove the temporary flag DST_NOGC.
Note that we also need to remove the DST_NOCACHE check in dst_release() and dst_hold_safe() because now all the dst are released based on refcnt and behaves as DST_NOCACHE.
--- include/net/dst.h | 6 +----- net/core/dst.c | 4 +--- net/decnet/dn_route.c | 6 ++---- net/ipv4/route.c | 5 ++--- net/ipv6/route.c | 5 ++--- net/xfrm/xfrm_policy.c | 2 +- 6 files changed, 9 insertions(+), 19 deletions(-)
include/net/dst.h +1 -5
@@ -57,7 +57,6 @@ struct dst_entry {#define DST_XFRM_TUNNEL 0x0080#define DST_XFRM_QUEUE 0x0100#define DST_METADATA 0x0200 -#define DST_NOGC 0x0400short error;
@@ -336,10 +335,7 @@ static inline void skb_dst_force(struct sk_buff *skb)*/ static inline bool dst_hold_safe (struct dst_entry *dst) { -if (dst->flags & (DST_NOCACHE |DST_NOGC )) -return atomic_inc_not_zero (&dst->__refcnt); -dst_hold (dst); -return true; +return atomic_inc_not_zero (&dst->__refcnt); }/**
net/core/dst.c +1 -3
@@ -179,14 +179,12 @@ void dst_release(struct dst_entry *dst) {if (dst) {int newrefcnt; -unsigned short destroy_after_rcu = dst->flags & - (DST_NOCACHE |DST_NOGC ); newrefcnt =atomic_dec_return (&dst->__refcnt);if (unlikely (newrefcnt <0 ))net_warn_ratelimited ("%s: dst:%p refcnt:%d\n" , __func__, dst, newrefcnt); -if (!newrefcnt &&unlikely (destroy_after_rcu)) +if (!newrefcnt)call_rcu (&dst->rcu_head, dst_destroy_rcu); } }
net/decnet/dn_route.c +2 -4
@@ -1179,8 +1179,7 @@ static int dn_route_output_slow(struct dst_entry **pprt, const struct flowidn *oif (dev_out->flags &IFF_LOOPBACK ) flags |=RTCF_LOCAL ; - rt =dst_alloc (&dn_dst_ops, dev_out,1 ,DST_OBSOLETE_NONE , -DST_HOST |DST_NOGC ); + rt =dst_alloc (&dn_dst_ops, dev_out,1 ,DST_OBSOLETE_NONE ,DST_HOST );if (rt ==NULL ) goto e_nobufs;
@@ -1445,8 +1444,7 @@ static int dn_route_input_slow(struct sk_buff *skb) } make_route: - rt =dst_alloc (&dn_dst_ops, out_dev,1 ,DST_OBSOLETE_NONE , -DST_HOST |DST_NOGC ); + rt =dst_alloc (&dn_dst_ops, out_dev,1 ,DST_OBSOLETE_NONE ,DST_HOST );if (rt ==NULL ) goto e_nobufs;
net/ipv4/route.c +2 -3
@@ -1496,8 +1496,7 @@ struct rtable *rt_dst_alloc(struct net_device *dev, rt =dst_alloc (&ipv4_dst_ops, dev,1 ,DST_OBSOLETE_FORCE_CHK , (will_cache ?0 : (DST_HOST |DST_NOCACHE )) | (nopolicy ?DST_NOPOLICY :0 ) | - (noxfrm ?DST_NOXFRM :0 ) | -DST_NOGC ); + (noxfrm ?DST_NOXFRM :0 ));if (rt) { rt->rt_genid =rt_genid_ipv4 (dev_net (dev));
@@ -2503,7 +2502,7 @@ struct dst_entry *ipv4_blackhole_route(struct net *net, struct dst_entry *dst_orstruct rtable *ort = (struct rtable *) dst_orig;struct rtable *rt; - rt =dst_alloc (&ipv4_dst_blackhole_ops,NULL ,1 ,DST_OBSOLETE_NONE ,DST_NOGC ); + rt =dst_alloc (&ipv4_dst_blackhole_ops,NULL ,1 ,DST_OBSOLETE_NONE ,0 );if (rt) {struct dst_entry *new = &rt->dst;
net/ipv6/route.c +2 -3
@@ -354,8 +354,7 @@ static struct rt6_info *__ip6_dst_alloc(struct net *net, int flags) {struct rt6_info *rt =dst_alloc (&net->ipv6.ip6_dst_ops, dev, -1 ,DST_OBSOLETE_FORCE_CHK , - flags |DST_NOGC ); +1 ,DST_OBSOLETE_FORCE_CHK , flags);if (rt)rt6_info_init (rt);
@@ -1255,7 +1254,7 @@ struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_oristruct dst_entry *new =NULL ; rt =dst_alloc (&ip6_dst_blackhole_ops, loopback_dev,1 , -DST_OBSOLETE_NONE ,DST_NOGC ); +DST_OBSOLETE_NONE ,0 );if (rt) {rt6_info_init (rt);
net/xfrm/xfrm_policy.c +1 -1
@@ -1622,7 +1622,7 @@ static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)default :BUG (); } - xdst =dst_alloc (dst_ops,NULL ,1 ,DST_OBSOLETE_NONE ,DST_NOGC ); + xdst =dst_alloc (dst_ops,NULL ,1 ,DST_OBSOLETE_NONE ,0 );if (likely (xdst)) {struct dst_entry *dst = &xdst->u.dst;
Signature · 1 line
2.13.1.518.g3df882009-goog
PATCH v2 9/21 ipv4: mark DST_NOGC and remove the operation of dst_free()
In reply to Wei WangFrom: Wei Wang <[email protected]>
With the previous preparation patches, we are ready to get rid of the dst gc operation in ipv4 code and release dst based on refcnt only. So this patch adds DST_NOGC flag for all IPv4 dst and remove the calls to dst_free().
At this point, all dst created in ipv4 code do not use the dst gc anymore and will be destroyed at the point when refcnt drops to 0.
--- net/ipv4/fib_semantics.c | 6 ++---- net/ipv4/route.c | 15 +++------------ 2 files changed, 5 insertions(+), 16 deletions(-)
net/ipv4/fib_semantics.c +2 -4
@@ -153,8 +153,7 @@ static void rt_fibinfo_free(struct rtable __rcu **rtp)*/ dst_dev_put (&rt->dst); -dst_release (&rt->dst); -dst_free (&rt->dst); +dst_release_immediate (&rt->dst); }static void free_nh_exceptions (struct fib_nh *nh)
@@ -198,8 +197,7 @@ static void rt_fibinfo_free_cpus(struct rtable __rcu * __percpu *rtp) rt =rcu_dereference_protected (*per_cpu_ptr (rtp, cpu),1 );if (rt) {dst_dev_put (&rt->dst); -dst_release (&rt->dst); -dst_free (&rt->dst); +dst_release_immediate (&rt->dst); } }free_percpu (rtp);
net/ipv4/route.c +3 -12
@@ -589,11 +589,6 @@ static void ip_rt_build_flow_key(struct flowi4 *fl4, const struct sock *sk,build_sk_flow_key (fl4, sk); } -static inline void rt_free (struct rtable *rt) -{ -call_rcu (&rt->dst.rcu_head, dst_rcu_free); -} -static DEFINE_SPINLOCK (fnhe_lock);static void fnhe_flush_routes (struct fib_nh_exception *fnhe)
@@ -605,14 +600,12 @@ static void fnhe_flush_routes(struct fib_nh_exception *fnhe)RCU_INIT_POINTER (fnhe->fnhe_rth_input,NULL );dst_dev_put (&rt->dst);dst_release (&rt->dst); -rt_free (rt); } rt =rcu_dereference (fnhe->fnhe_rth_output);if (rt) {RCU_INIT_POINTER (fnhe->fnhe_rth_output,NULL );dst_dev_put (&rt->dst);dst_release (&rt->dst); -rt_free (rt); } }
@@ -1341,7 +1334,6 @@ static bool rt_bind_exception(struct rtable *rt, struct fib_nh_exception *fnhe,if (orig) {dst_dev_put (&orig->dst);dst_release (&orig->dst); -rt_free (orig); } ret = true; }
@@ -1374,7 +1366,6 @@ static bool rt_cache_route(struct fib_nh *nh, struct rtable *rt)if (orig) {dst_dev_put (&orig->dst);dst_release (&orig->dst); -rt_free (orig); } }else {dst_release (&rt->dst);
@@ -1505,7 +1496,8 @@ struct rtable *rt_dst_alloc(struct net_device *dev, rt =dst_alloc (&ipv4_dst_ops, dev,1 ,DST_OBSOLETE_FORCE_CHK , (will_cache ?0 : (DST_HOST |DST_NOCACHE )) | (nopolicy ?DST_NOPOLICY :0 ) | - (noxfrm ?DST_NOXFRM :0 )); + (noxfrm ?DST_NOXFRM :0 ) | +DST_NOGC );if (rt) { rt->rt_genid =rt_genid_ipv4 (dev_net (dev));
@@ -2511,7 +2503,7 @@ struct dst_entry *ipv4_blackhole_route(struct net *net, struct dst_entry *dst_orstruct rtable *ort = (struct rtable *) dst_orig;struct rtable *rt; - rt =dst_alloc (&ipv4_dst_blackhole_ops,NULL ,1 ,DST_OBSOLETE_NONE ,0 ); + rt =dst_alloc (&ipv4_dst_blackhole_ops,NULL ,1 ,DST_OBSOLETE_NONE ,DST_NOGC );if (rt) {struct dst_entry *new = &rt->dst;
@@ -2534,7 +2526,6 @@ struct dst_entry *ipv4_blackhole_route(struct net *net, struct dst_entry *dst_or rt->rt_uses_gateway = ort->rt_uses_gateway;INIT_LIST_HEAD (&rt->rt_uncached); -dst_free (new); }dst_release (dst_orig);
Signature · 1 line
2.13.1.518.g3df882009-goog
PATCH v2 14/21 ipv6: get rid of icmp6 dst garbage collector
In reply to Wei WangFrom: Wei Wang <[email protected]>
icmp6 dst route is currently ref counted during creation and will be freed by user during its call of dst_release(). So no need of a garbage collector for it.
Remove all icmp6 dst garbage collector related code.
--- include/net/ip6_route.h | 1 - net/ipv6/ip6_fib.c | 3 +-- net/ipv6/route.c | 46 ---------------------------------------------- 3 files changed, 1 insertion(+), 49 deletions(-)
include/net/ip6_route.h +0 -1
@@ -116,7 +116,6 @@ struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr, const struct in6_addr *saddr, int oif, int flags);struct dst_entry *icmp6_dst_alloc (struct net_device *dev,struct flowi6 *fl6); -int icmp6_dst_gc (void );void fib6_force_start_gc (struct net *net);
net/ipv6/ip6_fib.c +1 -2
@@ -1822,8 +1822,7 @@ void fib6_run_gc(unsigned long expires, struct net *net, bool force) } gc_args.timeout = expires ? (int )expires : net->ipv6.sysctl.ip6_rt_gc_interval; - - gc_args.more =icmp6_dst_gc (); + gc_args.more =0 ;fib6_clean_all (net, fib6_age, &gc_args); now = jiffies;
net/ipv6/route.c +0 -46
64 lines not on this page · raw message
Signature · 1 line
2.13.1.518.g3df882009-goog
PATCH v2 20/21 net: reorder all the dst flags
In reply to Wei WangFrom: Wei Wang <[email protected]>
As some dst flags are removed, reorder the dst flags to fill in the blanks.
Note: these flags are not exposed into user space. So it is safe to reorder.
--- include/net/dst.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
include/net/dst.h +5 -5
@@ -51,11 +51,11 @@ struct dst_entry {#define DST_HOST 0x0001#define DST_NOXFRM 0x0002#define DST_NOPOLICY 0x0004 -#define DST_NOCOUNT 0x0020 -#define DST_FAKE_RTABLE 0x0040 -#define DST_XFRM_TUNNEL 0x0080 -#define DST_XFRM_QUEUE 0x0100 -#define DST_METADATA 0x0200 +#define DST_NOCOUNT 0x0008 +#define DST_FAKE_RTABLE 0x0010 +#define DST_XFRM_TUNNEL 0x0020 +#define DST_XFRM_QUEUE 0x0040 +#define DST_METADATA 0x0080short error;
Signature · 1 line
2.13.1.518.g3df882009-goog
PATCH v2 10/21 ipv6: take dst->__refcnt for insertion into fib6 tree
In reply to Wei WangFrom: Wei Wang <[email protected]>
In IPv6 routing code, struct rt6_info is created for each static route and RTF_CACHE route and inserted into fib6 tree. In both cases, dst ref count is not taken.
As explained in the previous patch, this leads to the need of the dst garbage collector.
This patch holds ref count of dst before inserting the route into fib6 tree and properly releases the dst when deleting it from the fib6 tree as a preparation in order to fully get rid of dst gc later.
Also, correct fib6_age() logic to check dst->__refcnt to be 1 to indicate no user is referencing the dst.
And remove dst_hold() in vrf_rt6_create() as ip6_dst_alloc() already puts dst->__refcnt to 1.
--- drivers/net/vrf.c | 4 ---- net/ipv6/ip6_fib.c | 12 +++++++++++- net/ipv6/route.c | 55 ++++++++++++++++++++++++++++++++++++++---------------- 3 files changed, 50 insertions(+), 21 deletions(-)
drivers/net/vrf.c +0 -4
@@ -583,8 +583,6 @@ static int vrf_rt6_create(struct net_device *dev)if (!rt6) goto out; -dst_hold (&rt6->dst); - rt6->rt6i_table = rt6i_table; rt6->dst.output = vrf_output6;
@@ -597,8 +595,6 @@ static int vrf_rt6_create(struct net_device *dev) goto out; } -dst_hold (&rt6_local->dst); - rt6_local->rt6i_idev =in6_dev_get (dev); rt6_local->rt6i_flags =RTF_UP |RTF_NONEXTHOP |RTF_LOCAL ; rt6_local->rt6i_table = rt6i_table;
net/ipv6/ip6_fib.c +11 -1
@@ -172,6 +172,7 @@ static void rt6_free_pcpu(struct rt6_info *non_pcpu_rt) ppcpu_rt =per_cpu_ptr (non_pcpu_rt->rt6i_pcpu, cpu); pcpu_rt = *ppcpu_rt;if (pcpu_rt) { +dst_release (&pcpu_rt->dst);rt6_rcu_free (pcpu_rt); *ppcpu_rt =NULL ; }
@@ -185,6 +186,7 @@ static void rt6_release(struct rt6_info *rt) {if (atomic_dec_and_test (&rt->rt6i_ref)) {rt6_free_pcpu (rt); +dst_release (&rt->dst);rt6_rcu_free (rt); } }
@@ -1101,6 +1103,10 @@ int fib6_add(struct fib6_node *root, struct rt6_info *rt, atomic_inc(&pn->leaf->rt6i_ref); }#endif +/* Always release dst as dst->__refcnt is guaranteed +* to be taken before entering this function +*/ +dst_release (&rt->dst);if (!(rt->dst.flags &DST_NOCACHE ))dst_free (&rt->dst); }
@@ -1113,6 +1119,10 @@ int fib6_add(struct fib6_node *root, struct rt6_info *rt, st_failure:if (fn && !(fn->fn_flags & (RTN_RTINFO |RTN_ROOT )))fib6_repair_tree (info->nl_net, fn); +/* Always release dst as dst->__refcnt is guaranteed +* to be taken before entering this function +*/ +dst_release (&rt->dst);if (!(rt->dst.flags &DST_NOCACHE ))dst_free (&rt->dst);return err;
@@ -1783,7 +1793,7 @@ static int fib6_age(struct rt6_info *rt, void *arg) } gc_args->more++; }else if (rt->rt6i_flags &RTF_CACHE ) { -if (atomic_read (&rt->dst.__refcnt) ==0 && +if (atomic_read (&rt->dst.__refcnt) ==1 &&time_after_eq (now, rt->dst.lastuse + gc_args->timeout)) {RT6_TRACE ("aging clone %p\n" , rt);return -1 ;
net/ipv6/route.c +39 -16
156 lines not on this page · raw message
Signature · 1 line
2.13.1.518.g3df882009-goog
PATCH v2 21/21 net: add debug atomic_inc_not_zero() in dst_hold()
In reply to Wei WangFrom: Wei Wang <[email protected]>
This patch is meant to add a debug warning on the situation where dst is being held during its destroy phase. This could potentially cause double free issue on the dst.
--- include/net/dst.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/net/dst.h +1 -1
@@ -251,7 +251,7 @@ static inline void dst_hold(struct dst_entry *dst)* __pad_to_align_refcnt declaration in struct dst_entry */ BUILD_BUG_ON (offsetof(struct dst_entry , __refcnt) &63 ); -atomic_inc (&dst->__refcnt); +WARN_ON (atomic_inc_not_zero (&dst->__refcnt) ==0 ); }static inline void dst_use (struct dst_entry *dst,unsigned long time)
Signature · 1 line
2.13.1.518.g3df882009-goog
PATCH v2 11/21 ipv6: call dst_dev_put() properly
In reply to Wei WangFrom: Wei Wang <[email protected]>
As the intend of this patch series is to completely remove dst gc, we need to call dst_dev_put() to release the reference to dst->dev when removing routes from fib because we won't keep the gc list anymore and will lose the dst pointer right after removing the routes. Without the gc list, there is no way to find all the dst's that have dst->dev pointing to the going-down dev.
Hence, we are doing dst_dev_put() immediately before we lose the last reference of the dst from the routing code. The next dst_check() will trigger a route re-lookup to find another route (if there is any).
--- net/ipv6/ip6_fib.c | 2 ++ 1 file changed, 2 insertions(+)
net/ipv6/ip6_fib.c +2 -0
@@ -172,6 +172,7 @@ static void rt6_free_pcpu(struct rt6_info *non_pcpu_rt) ppcpu_rt =per_cpu_ptr (non_pcpu_rt->rt6i_pcpu, cpu); pcpu_rt = *ppcpu_rt;if (pcpu_rt) { +dst_dev_put (&pcpu_rt->dst);dst_release (&pcpu_rt->dst);rt6_rcu_free (pcpu_rt); *ppcpu_rt =NULL ;
@@ -186,6 +187,7 @@ static void rt6_release(struct rt6_info *rt) {if (atomic_dec_and_test (&rt->rt6i_ref)) {rt6_free_pcpu (rt); +dst_dev_put (&rt->dst);dst_release (&rt->dst);rt6_rcu_free (rt); }
Signature · 1 line
2.13.1.518.g3df882009-goog
PATCH v2 17/21 net: remove dst gc related code
In reply to Wei WangFrom: Wei Wang <[email protected]>
This patch removes all dst gc related code and all the dst free functions
--- include/net/dst.h | 21 ------ net/core/dev.c | 1 - net/core/dst.c | 213 ------------------------------------------------------ 3 files changed, 235 deletions(-)
include/net/dst.h +0 -21
@@ -425,28 +425,9 @@ void *dst_alloc(struct dst_ops *ops, struct net_device *dev, int initial_ref,void dst_init (struct dst_entry *dst,struct dst_ops *ops,struct net_device *dev,int initial_ref,int initial_obsolete,unsigned short flags); -void __dst_free (struct dst_entry *dst);struct dst_entry *dst_destroy (struct dst_entry *dst);void dst_dev_put (struct dst_entry *dst); -static inline void dst_free (struct dst_entry *dst) -{ -if (dst->obsolete >0 ) -return ; -if (!atomic_read (&dst->__refcnt)) { - dst =dst_destroy (dst); -if (!dst) -return ; - } -__dst_free (dst); -} - -static inline void dst_rcu_free (struct rcu_head *head) -{ - struct dst_entry *dst = container_of(head, struct dst_entry, rcu_head); -dst_free (dst); -} -static inline void dst_confirm (struct dst_entry *dst) { }
@@ -508,8 +489,6 @@ static inline struct dst_entry *dst_check(struct dst_entry *dst, u32 cookie)return dst; } -void dst_subsys_init (void ); -/* Flags for xfrm_lookup flags argument. */ enum {XFRM_LOOKUP_ICMP =1 <<0 ,
net/core/dev.c +0 -1
@@ -8681,7 +8681,6 @@ static int __init net_dev_init(void) rc =cpuhp_setup_state_nocalls (CPUHP_NET_DEV_DEAD ,"net/dev:dead" ,NULL , dev_cpu_dead);WARN_ON (rc <0 ); -dst_subsys_init (); rc =0 ; out:return rc;
net/core/dst.c +0 -213
228 lines not on this page · raw message
Signature · 1 line
2.13.1.518.g3df882009-goog
PATCH v2 19/21 net: remove DST_NOCACHE flag
In reply to Wei WangFrom: Wei Wang <[email protected]>
DST_NOCACHE flag check has been removed from dst_release() and dst_hold_safe() in a previous patch because all the dst are now ref counted properly and can be released based on refcnt only. Looking at the rest of the DST_NOCACHE use, all of them can now be removed or replaced with other checks.
So this patch gets rid of all the DST_NOCACHE usage and remove this flag completely.
--- drivers/net/vrf.c | 2 +- include/net/dst.h | 1 - include/net/ip6_fib.h | 2 +- net/core/dst.c | 2 +- net/ipv4/route.c | 23 +++++++++++------------ net/ipv6/ip6_fib.c | 4 +--- net/ipv6/route.c | 7 ++----- net/xfrm/xfrm_policy.c | 1 - 8 files changed, 17 insertions(+), 25 deletions(-)
drivers/net/vrf.c +1 -1
@@ -563,7 +563,7 @@ static void vrf_rt6_release(struct net_device *dev, struct net_vrf *vrf)static int vrf_rt6_create (struct net_device *dev) { -int flags =DST_HOST |DST_NOPOLICY |DST_NOXFRM |DST_NOCACHE ; +int flags =DST_HOST |DST_NOPOLICY |DST_NOXFRM ;struct net_vrf *vrf =netdev_priv (dev);struct net *net =dev_net (dev);struct fib6_table *rt6i_table;
include/net/dst.h +0 -1
@@ -51,7 +51,6 @@ struct dst_entry {#define DST_HOST 0x0001#define DST_NOXFRM 0x0002#define DST_NOPOLICY 0x0004 -#define DST_NOCACHE 0x0010#define DST_NOCOUNT 0x0020#define DST_FAKE_RTABLE 0x0040#define DST_XFRM_TUNNEL 0x0080
include/net/ip6_fib.h +1 -1
@@ -170,7 +170,7 @@ static inline void rt6_update_expires(struct rt6_info *rt0, int timeout)static inline u32 rt6_get_cookie (const struct rt6_info *rt) {if (rt->rt6i_flags &RTF_PCPU || - (unlikely (rt->dst.flags &DST_NOCACHE ) && rt->dst.from)) + (unlikely (!list_empty (&rt->rt6i_uncached)) && rt->dst.from)) rt = (struct rt6_info *)(rt->dst.from);return rt->rt6i_node ? rt->rt6i_node->fn_sernum :0 ;
net/core/dst.c +1 -1
@@ -270,7 +270,7 @@ static void __metadata_dst_init(struct metadata_dst *md_dst, u8 optslen) dst = &md_dst->dst;dst_init (dst, &md_dst_ops,NULL ,1 ,DST_OBSOLETE_NONE , -DST_METADATA |DST_NOCACHE |DST_NOCOUNT ); +DST_METADATA |DST_NOCOUNT ); dst->input = dst_md_discard; dst->output = dst_md_discard_out;
net/ipv4/route.c +11 -12
@@ -1299,7 +1299,7 @@ static struct fib_nh_exception *find_exception(struct fib_nh *nh, __be32 daddr) }static bool rt_bind_exception (struct rtable *rt,struct fib_nh_exception *fnhe, -__be32 daddr) +__be32 daddr,const bool do_cache) {bool ret = false;
@@ -1328,7 +1328,7 @@ static bool rt_bind_exception(struct rtable *rt, struct fib_nh_exception *fnhe,if (!rt->rt_gateway) rt->rt_gateway = daddr; -if (!(rt->dst.flags &DST_NOCACHE )) { +if (do_cache) {dst_hold (&rt->dst);rcu_assign_pointer (*porig, rt);if (orig) {
@@ -1441,7 +1441,8 @@ static bool rt_cache_valid(const struct rtable *rt)static void rt_set_nexthop (struct rtable *rt,__be32 daddr,const struct fib_result *res,struct fib_nh_exception *fnhe, -struct fib_info *fi,u16 type,u32 itag) +struct fib_info *fi,u16 type,u32 itag, +const bool do_cache) {bool cached = false;
@@ -1462,8 +1463,8 @@ static void rt_set_nexthop(struct rtable *rt, __be32 daddr,#endif rt->dst.lwtstate =lwtstate_get (nh->nh_lwtstate);if (unlikely (fnhe)) - cached =rt_bind_exception (rt, fnhe, daddr); -else if (!(rt->dst.flags &DST_NOCACHE )) + cached =rt_bind_exception (rt, fnhe, daddr, do_cache); +else if (do_cache) cached =rt_cache_route (nh, rt);if (unlikely (!cached)) {/* Routes we intend to cache in nexthop exception or
@@ -1471,7 +1472,6 @@ static void rt_set_nexthop(struct rtable *rt, __be32 daddr,* However, if we are unsuccessful at storing this * route into the cache we really need to set it. */ - rt->dst.flags |=DST_NOCACHE ;if (!rt->rt_gateway) rt->rt_gateway = daddr;rt_add_uncached_list (rt);
@@ -1494,7 +1494,7 @@ struct rtable *rt_dst_alloc(struct net_device *dev,struct rtable *rt; rt =dst_alloc (&ipv4_dst_ops, dev,1 ,DST_OBSOLETE_FORCE_CHK , - (will_cache ?0 : (DST_HOST |DST_NOCACHE )) | + (will_cache ?0 :DST_HOST ) | (nopolicy ?DST_NOPOLICY :0 ) | (noxfrm ?DST_NOXFRM :0 ));
@@ -1738,7 +1738,8 @@ static int __mkroute_input(struct sk_buff *skb, rth->dst.input = ip_forward; -rt_set_nexthop (rth, daddr, res, fnhe, res->fi, res->type, itag); +rt_set_nexthop (rth, daddr, res, fnhe, res->fi, res->type, itag, + do_cache);set_lwt_redirect (rth);skb_dst_set (skb, &rth->dst); out:
@@ -2026,10 +2027,8 @@ out: return err; rth->dst.input = lwtunnel_input; } -if (unlikely (!rt_cache_route (nh, rth))) { - rth->dst.flags |=DST_NOCACHE ; +if (unlikely (!rt_cache_route (nh, rth)))rt_add_uncached_list (rth); - } }skb_dst_set (skb, &rth->dst); err =0 ;
@@ -2260,7 +2259,7 @@ static struct rtable *__mkroute_output(const struct fib_result *res,#endif } -rt_set_nexthop (rth, fl4->daddr, res, fnhe, fi, type,0 ); +rt_set_nexthop (rth, fl4->daddr, res, fnhe, fi, type,0 , do_cache);set_lwt_redirect (rth);return rth;
net/ipv6/ip6_fib.c +1 -3
@@ -975,8 +975,7 @@ int fib6_add(struct fib6_node *root, struct rt6_info *rt,int replace_required =0 ;int sernum =fib6_new_sernum (info->nl_net); -if (WARN_ON_ONCE ((rt->dst.flags &DST_NOCACHE ) && - !atomic_read (&rt->dst.__refcnt))) +if (WARN_ON_ONCE (!atomic_read (&rt->dst.__refcnt)))return -EINVAL ;if (info->nlh) {
@@ -1073,7 +1072,6 @@ int fib6_add(struct fib6_node *root, struct rt6_info *rt,fib6_start_gc (info->nl_net, rt);if (!(rt->rt6i_flags &RTF_CACHE ))fib6_prune_clones (info->nl_net, pn); - rt->dst.flags &= ~DST_NOCACHE ; } out:
net/ipv6/route.c +2 -5
@@ -128,7 +128,6 @@ static void rt6_uncached_list_add(struct rt6_info *rt) {struct uncached_list *ul =raw_cpu_ptr (&rt6_uncached_list); - rt->dst.flags |=DST_NOCACHE ; rt->rt6i_uncached_list = ul;spin_lock_bh (&ul->lock);
@@ -1326,7 +1325,7 @@ static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)rt6_dst_from_metrics_check (rt);if (rt->rt6i_flags &RTF_PCPU || - (unlikely (dst->flags &DST_NOCACHE ) && rt->dst.from)) + (unlikely (!list_empty (&rt->rt6i_uncached)) && rt->dst.from))return rt6_dst_from_check (rt, cookie);else return rt6_check (rt, cookie);
@@ -2130,8 +2129,7 @@ static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)struct fib6_table *table;struct net *net =dev_net (rt->dst.dev); -if (rt == net->ipv6.ip6_null_entry || - rt->dst.flags &DST_NOCACHE ) { +if (rt == net->ipv6.ip6_null_entry) { err = -ENOENT ; goto out; }
@@ -2722,7 +2720,6 @@ struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev, rt->rt6i_dst.plen =128 ; tb_id =l3mdev_fib_table (idev->dev) ? :RT6_TABLE_LOCAL ; rt->rt6i_table =fib6_get_table (net, tb_id); - rt->dst.flags |=DST_NOCACHE ;return rt; }
net/xfrm/xfrm_policy.c +0 -1
@@ -2231,7 +2231,6 @@ struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig, }dst_hold (&xdst->u.dst); - xdst->u.dst.flags |=DST_NOCACHE ; route = xdst->route; } }
Signature · 1 line
2.13.1.518.g3df882009-goog
PATCH v2 16/21 decnet: take dst->__refcnt when struct dn_route is created
In reply to Wei WangFrom: Wei Wang <[email protected]>
struct dn_route is inserted into dn_rt_hash_table but no dst->__refcnt is taken.
This patch makes sure the dn_rt_hash_table's reference to the dst is ref counted.
As the dst is always ref counted properly, we can safely mark DST_NOGC flag so dst_release() will release dst based on refcnt only. And dst gc is no longer needed and all dst_free() or its related function calls should be replaced with dst_release() or dst_release_immediate(). And dst_dev_put() is called when removing dst from the hash table to release the reference on dst->dev before we lose pointer to it.
Also, correct the logic in dn_dst_check_expire() and dn_dst_gc() to check dst->__refcnt to be > 1 to indicate it is referenced by other users.
--- net/decnet/dn_route.c | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-)
net/decnet/dn_route.c +19 -17
@@ -183,11 +183,6 @@ static __inline__ unsigned int dn_hash(__le16 src, __le16 dst)return dn_rt_hash_mask & (unsigned int )tmp; } -static inline void dnrt_free (struct dn_route *rt) -{ -call_rcu_bh (&rt->dst.rcu_head, dst_rcu_free); -} -static void dn_dst_check_expire (unsigned long dummy) {int i;
@@ -202,14 +197,15 @@ static void dn_dst_check_expire(unsigned long dummy)spin_lock (&dn_rt_hash_table[i].lock);while ((rt =rcu_dereference_protected (*rtp,lockdep_is_held (&dn_rt_hash_table[i].lock))) !=NULL ) { -if (atomic_read (&rt->dst.__refcnt) || - (now - rt->dst.lastuse) < expire) { +if (atomic_read (&rt->dst.__refcnt) >1 || + (now - rt->dst.lastuse) < expire) { rtp = &rt->dst.dn_next;continue ; } *rtp = rt->dst.dn_next; rt->dst.dn_next =NULL ; -dnrt_free (rt); +dst_dev_put (&rt->dst); +dst_release (&rt->dst); }spin_unlock (&dn_rt_hash_table[i].lock);
@@ -235,14 +231,15 @@ static int dn_dst_gc(struct dst_ops *ops)while ((rt =rcu_dereference_protected (*rtp,lockdep_is_held (&dn_rt_hash_table[i].lock))) !=NULL ) { -if (atomic_read (&rt->dst.__refcnt) || - (now - rt->dst.lastuse) < expire) { +if (atomic_read (&rt->dst.__refcnt) >1 || + (now - rt->dst.lastuse) < expire) { rtp = &rt->dst.dn_next;continue ; } *rtp = rt->dst.dn_next; rt->dst.dn_next =NULL ; -dnrt_free (rt); +dst_dev_put (&rt->dst); +dst_release (&rt->dst);break ; }spin_unlock_bh (&dn_rt_hash_table[i].lock);
@@ -344,7 +341,7 @@ static int dn_insert_route(struct dn_route *rt, unsigned int hash, struct dn_roudst_use (&rth->dst, now);spin_unlock_bh (&dn_rt_hash_table[hash].lock); -dst_free (&rt->dst); +dst_release_immediate (&rt->dst); *rp = rth;return 0 ; }
@@ -374,7 +371,8 @@ static void dn_run_flush(unsigned long dummy)for (; rt; rt = next) { next =rcu_dereference_raw (rt->dst.dn_next);RCU_INIT_POINTER (rt->dst.dn_next,NULL ); -dnrt_free (rt); +dst_dev_put (&rt->dst); +dst_release (&rt->dst); } nothing_to_declare:
@@ -1181,7 +1179,8 @@ static int dn_route_output_slow(struct dst_entry **pprt, const struct flowidn *oif (dev_out->flags &IFF_LOOPBACK ) flags |=RTCF_LOCAL ; - rt =dst_alloc (&dn_dst_ops, dev_out,0 ,DST_OBSOLETE_NONE ,DST_HOST ); + rt =dst_alloc (&dn_dst_ops, dev_out,1 ,DST_OBSOLETE_NONE , +DST_HOST |DST_NOGC );if (rt ==NULL ) goto e_nobufs;
@@ -1215,6 +1214,7 @@ static int dn_route_output_slow(struct dst_entry **pprt, const struct flowidn *o goto e_neighbour; hash =dn_hash (rt->fld.saddr, rt->fld.daddr); +/* dn_insert_route() increments dst->__refcnt */ dn_insert_route (rt, hash, (struct dn_route **)pprt); done:
@@ -1237,7 +1237,7 @@ static int dn_route_output_slow(struct dst_entry **pprt, const struct flowidn *o err = -ENOBUFS ; goto done; e_neighbour: -dst_free (&rt->dst); +dst_release_immediate (&rt->dst); goto e_nobufs; }
@@ -1445,7 +1445,8 @@ static int dn_route_input_slow(struct sk_buff *skb) } make_route: - rt =dst_alloc (&dn_dst_ops, out_dev,0 ,DST_OBSOLETE_NONE ,DST_HOST ); + rt =dst_alloc (&dn_dst_ops, out_dev,1 ,DST_OBSOLETE_NONE , +DST_HOST |DST_NOGC );if (rt ==NULL ) goto e_nobufs;
@@ -1491,6 +1492,7 @@ static int dn_route_input_slow(struct sk_buff *skb) goto e_neighbour; hash =dn_hash (rt->fld.saddr, rt->fld.daddr); +/* dn_insert_route() increments dst->__refcnt */ dn_insert_route (rt, hash, &rt);skb_dst_set (skb, &rt->dst);
@@ -1514,7 +1516,7 @@ static int dn_route_input_slow(struct sk_buff *skb) goto done; e_neighbour: -dst_free (&rt->dst); +dst_release_immediate (&rt->dst); goto done; }
Signature · 1 line
2.13.1.518.g3df882009-goog
David Miller
maintainerFrom: Wei Wang <[email protected]>
Date: Sat, 17 Jun 2017 10:42:23 -0700
Quoted 38 lines
The current mechanism of dst release is a bit complicated. It is because the users of dst get divided into 2 situations:
1. Most users take the reference count when using a dst and release the reference count when done.
2. Exceptional users like IPv4/IPv6/decnet/xfrm routing code do not take reference count when referencing to a dst due to some histotic reasons.Due to those exceptional use cases in 2, reference count being 0 is not an adequate evidence to indicate that no user is using this dst. So users in 1 can't free the dst simply based on reference count being 0 because users in 2 might still hold reference to it.
Instead, a dst garbage list is needed to hold the dst entries that already get removed by the users in 2 but are still held by users in 1. And a periodic garbage collector task is run to check all the dst entries in the list to see if the users in 1 have released the reference to those dst entries. If so, the dst is now ready to be freed.This logic introduces unnecessary complications in the dst code which makes it hard to understand and to debug.
In order to get rid of the whole dst garbage collector (gc) and make the dst code more unified and simplified, we can make the users in 2 also take reference count on the dst and release it properly when done.
This way, dst can be safely freed once the refcount drops to 0 and no gc thread is needed anymore.This patch series' target is to completely get rid of dst gc logic and free dst based on reference count only.
Patch 1-3 are preparation patches to do some cleanup/improvement on the existing code to make later work easier.
Patch 4-21 are real implementations.
In these patches, a temporary flagDST_NOGCis used to help transition those exceptional users one by one. Once every component is transitioned, this temporary flag is removed.
By the end of this patch series, all dst are refcounted when being used and released when done. And dst will be freed when its refcount drops to 0. No dst gc task is running anymore.
Quoted 7 lines
Note: This patch series depends on the decnet fix that was sent right before:
"decnet: always not take dst->__refcnt when inserting dst into hash table"v2:
add curly braces in udp_v4/6_early_demux() in patch 02 addEXPORT_SYMBOL()fordst_dev_put()in patch 05
Series applied, thanks!