Skip to content

Commit 0830106

Browse files
tracywwnjdavem330
authored andcommitted
ipv4: take dst->__refcnt when caching dst in fib
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. Signed-off-by: Wei Wang <weiwan@google.com> Acked-by: Martin KaFai Lau <kafai@fb.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 4a6ce2b commit 0830106

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

net/ipv4/fib_semantics.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ static void rt_fibinfo_free(struct rtable __rcu **rtp)
152152
* free_fib_info_rcu()
153153
*/
154154

155+
dst_release(&rt->dst);
155156
dst_free(&rt->dst);
156157
}
157158

@@ -194,8 +195,10 @@ static void rt_fibinfo_free_cpus(struct rtable __rcu * __percpu *rtp)
194195
struct rtable *rt;
195196

196197
rt = rcu_dereference_protected(*per_cpu_ptr(rtp, cpu), 1);
197-
if (rt)
198+
if (rt) {
199+
dst_release(&rt->dst);
198200
dst_free(&rt->dst);
201+
}
199202
}
200203
free_percpu(rtp);
201204
}

net/ipv4/route.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -603,11 +603,13 @@ static void fnhe_flush_routes(struct fib_nh_exception *fnhe)
603603
rt = rcu_dereference(fnhe->fnhe_rth_input);
604604
if (rt) {
605605
RCU_INIT_POINTER(fnhe->fnhe_rth_input, NULL);
606+
dst_release(&rt->dst);
606607
rt_free(rt);
607608
}
608609
rt = rcu_dereference(fnhe->fnhe_rth_output);
609610
if (rt) {
610611
RCU_INIT_POINTER(fnhe->fnhe_rth_output, NULL);
612+
dst_release(&rt->dst);
611613
rt_free(rt);
612614
}
613615
}
@@ -1332,9 +1334,12 @@ static bool rt_bind_exception(struct rtable *rt, struct fib_nh_exception *fnhe,
13321334
rt->rt_gateway = daddr;
13331335

13341336
if (!(rt->dst.flags & DST_NOCACHE)) {
1337+
dst_hold(&rt->dst);
13351338
rcu_assign_pointer(*porig, rt);
1336-
if (orig)
1339+
if (orig) {
1340+
dst_release(&orig->dst);
13371341
rt_free(orig);
1342+
}
13381343
ret = true;
13391344
}
13401345

@@ -1357,12 +1362,20 @@ static bool rt_cache_route(struct fib_nh *nh, struct rtable *rt)
13571362
}
13581363
orig = *p;
13591364

1365+
/* hold dst before doing cmpxchg() to avoid race condition
1366+
* on this dst
1367+
*/
1368+
dst_hold(&rt->dst);
13601369
prev = cmpxchg(p, orig, rt);
13611370
if (prev == orig) {
1362-
if (orig)
1371+
if (orig) {
1372+
dst_release(&orig->dst);
13631373
rt_free(orig);
1364-
} else
1374+
}
1375+
} else {
1376+
dst_release(&rt->dst);
13651377
ret = false;
1378+
}
13661379

13671380
return ret;
13681381
}

0 commit comments

Comments
 (0)