Skip to content

Commit ebc0ffa

Browse files
Eric Dumazetdavem330
authored andcommitted
fib: RCU conversion of fib_lookup()
fib_lookup() converted to be called in RCU protected context, no reference taken and released on a contended cache line (fib_clntref) fib_table_lookup() and fib_semantic_match() get an additional parameter. struct fib_info gets an rcu_head field, and is freed after an rcu grace period. Stress test : (Sending 160.000.000 UDP frames on same neighbour, IP route cache disabled, dual E5540 @2.53GHz, 32bit kernel, FIB_HASH) (about same results for FIB_TRIE) Before patch : real 1m31.199s user 0m13.761s sys 23m24.780s After patch: real 1m5.375s user 0m14.997s sys 15m50.115s Before patch Profile : 13044.00 15.4% __ip_route_output_key vmlinux 8438.00 10.0% dst_destroy vmlinux 5983.00 7.1% fib_semantic_match vmlinux 5410.00 6.4% fib_rules_lookup vmlinux 4803.00 5.7% neigh_lookup vmlinux 4420.00 5.2% _raw_spin_lock vmlinux 3883.00 4.6% rt_set_nexthop vmlinux 3261.00 3.9% _raw_read_lock vmlinux 2794.00 3.3% fib_table_lookup vmlinux 2374.00 2.8% neigh_resolve_output vmlinux 2153.00 2.5% dst_alloc vmlinux 1502.00 1.8% _raw_read_lock_bh vmlinux 1484.00 1.8% kmem_cache_alloc vmlinux 1407.00 1.7% eth_header vmlinux 1406.00 1.7% ipv4_dst_destroy vmlinux 1298.00 1.5% __copy_from_user_ll vmlinux 1174.00 1.4% dev_queue_xmit vmlinux 1000.00 1.2% ip_output vmlinux After patch Profile : 13712.00 15.8% dst_destroy vmlinux 8548.00 9.9% __ip_route_output_key vmlinux 7017.00 8.1% neigh_lookup vmlinux 4554.00 5.3% fib_semantic_match vmlinux 4067.00 4.7% _raw_read_lock vmlinux 3491.00 4.0% dst_alloc vmlinux 3186.00 3.7% neigh_resolve_output vmlinux 3103.00 3.6% fib_table_lookup vmlinux 2098.00 2.4% _raw_read_lock_bh vmlinux 2081.00 2.4% kmem_cache_alloc vmlinux 2013.00 2.3% _raw_spin_lock vmlinux 1763.00 2.0% __copy_from_user_ll vmlinux 1763.00 2.0% ip_output vmlinux 1761.00 2.0% ipv4_dst_destroy vmlinux 1631.00 1.9% eth_header vmlinux 1440.00 1.7% _raw_read_unlock_bh vmlinux Reference results, if IP route cache is enabled : real 0m29.718s user 0m10.845s sys 7m37.341s 25213.00 29.5% __ip_route_output_key vmlinux 9011.00 10.5% dst_release vmlinux 4817.00 5.6% ip_push_pending_frames vmlinux 4232.00 5.0% ip_finish_output vmlinux 3940.00 4.6% udp_sendmsg vmlinux 3730.00 4.4% __copy_from_user_ll vmlinux 3716.00 4.4% ip_route_output_flow vmlinux 2451.00 2.9% __xfrm_lookup vmlinux 2221.00 2.6% ip_append_data vmlinux 1718.00 2.0% _raw_spin_lock_bh vmlinux 1655.00 1.9% __alloc_skb vmlinux 1572.00 1.8% sock_wfree vmlinux 1345.00 1.6% kfree vmlinux Signed-off-by: Eric Dumazet <eric.dumazet@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent c2952c3 commit ebc0ffa

10 files changed

Lines changed: 72 additions & 77 deletions

File tree

‎include/net/fib_rules.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ struct fib_lookup_arg {
3131
void *lookup_ptr;
3232
void *result;
3333
struct fib_rule *rule;
34+
int flags;
35+
#define FIB_LOOKUP_NOREF 1
3436
};
3537

3638
struct fib_rules_ops {

‎include/net/ip_fib.h‎

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ struct fib_info {
8686
#ifdef CONFIG_IP_ROUTE_MULTIPATH
8787
int fib_power;
8888
#endif
89+
struct rcu_head rcu;
8990
struct fib_nh fib_nh[0];
9091
#define fib_dev fib_nh[0].nh_dev
9192
};
@@ -148,7 +149,7 @@ struct fib_table {
148149
};
149150

150151
extern int fib_table_lookup(struct fib_table *tb, const struct flowi *flp,
151-
struct fib_result *res);
152+
struct fib_result *res, int fib_flags);
152153
extern int fib_table_insert(struct fib_table *, struct fib_config *);
153154
extern int fib_table_delete(struct fib_table *, struct fib_config *);
154155
extern int fib_table_dump(struct fib_table *table, struct sk_buff *skb,
@@ -185,11 +186,11 @@ static inline int fib_lookup(struct net *net, const struct flowi *flp,
185186
struct fib_table *table;
186187

187188
table = fib_get_table(net, RT_TABLE_LOCAL);
188-
if (!fib_table_lookup(table, flp, res))
189+
if (!fib_table_lookup(table, flp, res, FIB_LOOKUP_NOREF))
189190
return 0;
190191

191192
table = fib_get_table(net, RT_TABLE_MAIN);
192-
if (!fib_table_lookup(table, flp, res))
193+
if (!fib_table_lookup(table, flp, res, FIB_LOOKUP_NOREF))
193194
return 0;
194195
return -ENETUNREACH;
195196
}
@@ -254,16 +255,6 @@ static inline void fib_info_put(struct fib_info *fi)
254255
free_fib_info(fi);
255256
}
256257

257-
static inline void fib_res_put(struct fib_result *res)
258-
{
259-
if (res->fi)
260-
fib_info_put(res->fi);
261-
#ifdef CONFIG_IP_MULTIPLE_TABLES
262-
if (res->r)
263-
fib_rule_put(res->r);
264-
#endif
265-
}
266-
267258
#ifdef CONFIG_PROC_FS
268259
extern int __net_init fib_proc_init(struct net *net);
269260
extern void __net_exit fib_proc_exit(struct net *net);

‎net/core/fib_rules.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
225225
err = ops->action(rule, fl, flags, arg);
226226

227227
if (err != -EAGAIN) {
228-
if (likely(atomic_inc_not_zero(&rule->refcnt))) {
228+
if ((arg->flags & FIB_LOOKUP_NOREF) ||
229+
likely(atomic_inc_not_zero(&rule->refcnt))) {
229230
arg->rule = rule;
230231
goto out;
231232
}

‎net/ipv4/fib_frontend.c‎

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,19 @@ struct net_device *__ip_dev_find(struct net *net, __be32 addr, bool devref)
168168
struct fib_result res = { 0 };
169169
struct net_device *dev = NULL;
170170

171-
if (fib_lookup(net, &fl, &res))
171+
rcu_read_lock();
172+
if (fib_lookup(net, &fl, &res)) {
173+
rcu_read_unlock();
172174
return NULL;
175+
}
173176
if (res.type != RTN_LOCAL)
174177
goto out;
175178
dev = FIB_RES_DEV(res);
176179

177180
if (dev && devref)
178181
dev_hold(dev);
179182
out:
180-
fib_res_put(&res);
183+
rcu_read_unlock();
181184
return dev;
182185
}
183186
EXPORT_SYMBOL(__ip_dev_find);
@@ -207,11 +210,12 @@ static inline unsigned __inet_dev_addr_type(struct net *net,
207210
local_table = fib_get_table(net, RT_TABLE_LOCAL);
208211
if (local_table) {
209212
ret = RTN_UNICAST;
210-
if (!fib_table_lookup(local_table, &fl, &res)) {
213+
rcu_read_lock();
214+
if (!fib_table_lookup(local_table, &fl, &res, FIB_LOOKUP_NOREF)) {
211215
if (!dev || dev == res.fi->fib_dev)
212216
ret = res.type;
213-
fib_res_put(&res);
214217
}
218+
rcu_read_unlock();
215219
}
216220
return ret;
217221
}
@@ -235,6 +239,7 @@ EXPORT_SYMBOL(inet_dev_addr_type);
235239
* - figure out what "logical" interface this packet arrived
236240
* and calculate "specific destination" address.
237241
* - check, that packet arrived from expected physical interface.
242+
* called with rcu_read_lock()
238243
*/
239244
int fib_validate_source(__be32 src, __be32 dst, u8 tos, int oif,
240245
struct net_device *dev, __be32 *spec_dst,
@@ -259,7 +264,6 @@ int fib_validate_source(__be32 src, __be32 dst, u8 tos, int oif,
259264
struct net *net;
260265

261266
no_addr = rpf = accept_local = 0;
262-
rcu_read_lock();
263267
in_dev = __in_dev_get_rcu(dev);
264268
if (in_dev) {
265269
no_addr = in_dev->ifa_list == NULL;
@@ -268,7 +272,6 @@ int fib_validate_source(__be32 src, __be32 dst, u8 tos, int oif,
268272
if (mark && !IN_DEV_SRC_VMARK(in_dev))
269273
fl.mark = 0;
270274
}
271-
rcu_read_unlock();
272275

273276
if (in_dev == NULL)
274277
goto e_inval;
@@ -278,7 +281,7 @@ int fib_validate_source(__be32 src, __be32 dst, u8 tos, int oif,
278281
goto last_resort;
279282
if (res.type != RTN_UNICAST) {
280283
if (res.type != RTN_LOCAL || !accept_local)
281-
goto e_inval_res;
284+
goto e_inval;
282285
}
283286
*spec_dst = FIB_RES_PREFSRC(res);
284287
fib_combine_itag(itag, &res);
@@ -299,10 +302,8 @@ int fib_validate_source(__be32 src, __be32 dst, u8 tos, int oif,
299302
#endif
300303
if (dev_match) {
301304
ret = FIB_RES_NH(res).nh_scope >= RT_SCOPE_HOST;
302-
fib_res_put(&res);
303305
return ret;
304306
}
305-
fib_res_put(&res);
306307
if (no_addr)
307308
goto last_resort;
308309
if (rpf == 1)
@@ -315,7 +316,6 @@ int fib_validate_source(__be32 src, __be32 dst, u8 tos, int oif,
315316
*spec_dst = FIB_RES_PREFSRC(res);
316317
ret = FIB_RES_NH(res).nh_scope >= RT_SCOPE_HOST;
317318
}
318-
fib_res_put(&res);
319319
}
320320
return ret;
321321

@@ -326,8 +326,6 @@ int fib_validate_source(__be32 src, __be32 dst, u8 tos, int oif,
326326
*itag = 0;
327327
return 0;
328328

329-
e_inval_res:
330-
fib_res_put(&res);
331329
e_inval:
332330
return -EINVAL;
333331
e_rpf:
@@ -873,15 +871,16 @@ static void nl_fib_lookup(struct fib_result_nl *frn, struct fib_table *tb)
873871
local_bh_disable();
874872

875873
frn->tb_id = tb->tb_id;
876-
frn->err = fib_table_lookup(tb, &fl, &res);
874+
rcu_read_lock();
875+
frn->err = fib_table_lookup(tb, &fl, &res, FIB_LOOKUP_NOREF);
877876

878877
if (!frn->err) {
879878
frn->prefixlen = res.prefixlen;
880879
frn->nh_sel = res.nh_sel;
881880
frn->type = res.type;
882881
frn->scope = res.scope;
883-
fib_res_put(&res);
884882
}
883+
rcu_read_unlock();
885884
local_bh_enable();
886885
}
887886
}

‎net/ipv4/fib_hash.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,8 @@ fn_new_zone(struct fn_hash *table, int z)
244244
}
245245

246246
int fib_table_lookup(struct fib_table *tb,
247-
const struct flowi *flp, struct fib_result *res)
247+
const struct flowi *flp, struct fib_result *res,
248+
int fib_flags)
248249
{
249250
int err;
250251
struct fn_zone *fz;
@@ -264,7 +265,7 @@ int fib_table_lookup(struct fib_table *tb,
264265

265266
err = fib_semantic_match(&f->fn_alias,
266267
flp, res,
267-
fz->fz_order);
268+
fz->fz_order, fib_flags);
268269
if (err <= 0)
269270
goto out;
270271
}

‎net/ipv4/fib_lookup.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ struct fib_alias {
2222
/* Exported by fib_semantics.c */
2323
extern int fib_semantic_match(struct list_head *head,
2424
const struct flowi *flp,
25-
struct fib_result *res, int prefixlen);
25+
struct fib_result *res, int prefixlen, int fib_flags);
2626
extern void fib_release_info(struct fib_info *);
2727
extern struct fib_info *fib_create_info(struct fib_config *cfg);
2828
extern int fib_nh_match(struct fib_config *cfg, struct fib_info *fi);

‎net/ipv4/fib_rules.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ int fib_lookup(struct net *net, struct flowi *flp, struct fib_result *res)
5757
{
5858
struct fib_lookup_arg arg = {
5959
.result = res,
60+
.flags = FIB_LOOKUP_NOREF,
6061
};
6162
int err;
6263

@@ -94,7 +95,7 @@ static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp,
9495
if (!tbl)
9596
goto errout;
9697

97-
err = fib_table_lookup(tbl, flp, (struct fib_result *) arg->result);
98+
err = fib_table_lookup(tbl, flp, (struct fib_result *) arg->result, arg->flags);
9899
if (err > 0)
99100
err = -EAGAIN;
100101
errout:

‎net/ipv4/fib_semantics.c‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ static const struct
148148

149149
/* Release a nexthop info record */
150150

151+
static void free_fib_info_rcu(struct rcu_head *head)
152+
{
153+
struct fib_info *fi = container_of(head, struct fib_info, rcu);
154+
155+
kfree(fi);
156+
}
157+
151158
void free_fib_info(struct fib_info *fi)
152159
{
153160
if (fi->fib_dead == 0) {
@@ -161,7 +168,7 @@ void free_fib_info(struct fib_info *fi)
161168
} endfor_nexthops(fi);
162169
fib_info_cnt--;
163170
release_net(fi->fib_net);
164-
kfree(fi);
171+
call_rcu(&fi->rcu, free_fib_info_rcu);
165172
}
166173

167174
void fib_release_info(struct fib_info *fi)
@@ -553,6 +560,7 @@ static int fib_check_nh(struct fib_config *cfg, struct fib_info *fi,
553560
nh->nh_scope = RT_SCOPE_LINK;
554561
return 0;
555562
}
563+
rcu_read_lock();
556564
{
557565
struct flowi fl = {
558566
.nl_u = {
@@ -568,8 +576,10 @@ static int fib_check_nh(struct fib_config *cfg, struct fib_info *fi,
568576
if (fl.fl4_scope < RT_SCOPE_LINK)
569577
fl.fl4_scope = RT_SCOPE_LINK;
570578
err = fib_lookup(net, &fl, &res);
571-
if (err)
579+
if (err) {
580+
rcu_read_unlock();
572581
return err;
582+
}
573583
}
574584
err = -EINVAL;
575585
if (res.type != RTN_UNICAST && res.type != RTN_LOCAL)
@@ -585,7 +595,7 @@ static int fib_check_nh(struct fib_config *cfg, struct fib_info *fi,
585595
goto out;
586596
err = 0;
587597
out:
588-
fib_res_put(&res);
598+
rcu_read_unlock();
589599
return err;
590600
} else {
591601
struct in_device *in_dev;
@@ -879,7 +889,7 @@ struct fib_info *fib_create_info(struct fib_config *cfg)
879889

880890
/* Note! fib_semantic_match intentionally uses RCU list functions. */
881891
int fib_semantic_match(struct list_head *head, const struct flowi *flp,
882-
struct fib_result *res, int prefixlen)
892+
struct fib_result *res, int prefixlen, int fib_flags)
883893
{
884894
struct fib_alias *fa;
885895
int nh_sel = 0;
@@ -943,7 +953,8 @@ int fib_semantic_match(struct list_head *head, const struct flowi *flp,
943953
res->type = fa->fa_type;
944954
res->scope = fa->fa_scope;
945955
res->fi = fa->fa_info;
946-
atomic_inc(&res->fi->fib_clntref);
956+
if (!(fib_flags & FIB_LOOKUP_NOREF))
957+
atomic_inc(&res->fi->fib_clntref);
947958
return 0;
948959
}
949960

‎net/ipv4/fib_trie.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,7 +1342,7 @@ int fib_table_insert(struct fib_table *tb, struct fib_config *cfg)
13421342
/* should be called with rcu_read_lock */
13431343
static int check_leaf(struct trie *t, struct leaf *l,
13441344
t_key key, const struct flowi *flp,
1345-
struct fib_result *res)
1345+
struct fib_result *res, int fib_flags)
13461346
{
13471347
struct leaf_info *li;
13481348
struct hlist_head *hhead = &l->list;
@@ -1356,7 +1356,7 @@ static int check_leaf(struct trie *t, struct leaf *l,
13561356
if (l->key != (key & ntohl(mask)))
13571357
continue;
13581358

1359-
err = fib_semantic_match(&li->falh, flp, res, plen);
1359+
err = fib_semantic_match(&li->falh, flp, res, plen, fib_flags);
13601360

13611361
#ifdef CONFIG_IP_FIB_TRIE_STATS
13621362
if (err <= 0)
@@ -1372,7 +1372,7 @@ static int check_leaf(struct trie *t, struct leaf *l,
13721372
}
13731373

13741374
int fib_table_lookup(struct fib_table *tb, const struct flowi *flp,
1375-
struct fib_result *res)
1375+
struct fib_result *res, int fib_flags)
13761376
{
13771377
struct trie *t = (struct trie *) tb->tb_data;
13781378
int ret;
@@ -1399,7 +1399,7 @@ int fib_table_lookup(struct fib_table *tb, const struct flowi *flp,
13991399

14001400
/* Just a leaf? */
14011401
if (IS_LEAF(n)) {
1402-
ret = check_leaf(t, (struct leaf *)n, key, flp, res);
1402+
ret = check_leaf(t, (struct leaf *)n, key, flp, res, fib_flags);
14031403
goto found;
14041404
}
14051405

@@ -1424,7 +1424,7 @@ int fib_table_lookup(struct fib_table *tb, const struct flowi *flp,
14241424
}
14251425

14261426
if (IS_LEAF(n)) {
1427-
ret = check_leaf(t, (struct leaf *)n, key, flp, res);
1427+
ret = check_leaf(t, (struct leaf *)n, key, flp, res, fib_flags);
14281428
if (ret > 0)
14291429
goto backtrace;
14301430
goto found;

0 commit comments

Comments
 (0)