Skip to content

Commit aee06da

Browse files
Julian Anastasovdavem330
authored andcommitted
ipv4: use seqlock for nh_exceptions
Use global seqlock for the nh_exceptions. Call fnhe_oldest with the right hash chain. Correct the diff value for dst_set_expires. v2: after suggestions from Eric Dumazet: * get rid of spin lock fnhe_lock, rearrange update_or_create_fnhe * continue daddr search in rt_bind_exception v3: * remove the daddr check before seqlock in rt_bind_exception * restart lookup in rt_bind_exception on detected seqlock change, as suggested by David Miller Signed-off-by: Julian Anastasov <ja@ssi.bg> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 7fed84f commit aee06da

2 files changed

Lines changed: 69 additions & 51 deletions

File tree

‎include/net/ip_fib.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ struct fib_nh_exception {
5151
struct fib_nh_exception __rcu *fnhe_next;
5252
__be32 fnhe_daddr;
5353
u32 fnhe_pmtu;
54-
u32 fnhe_gw;
54+
__be32 fnhe_gw;
5555
unsigned long fnhe_expires;
5656
unsigned long fnhe_stamp;
5757
};

‎net/ipv4/route.c‎

Lines changed: 68 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,9 +1333,9 @@ static void ip_rt_build_flow_key(struct flowi4 *fl4, const struct sock *sk,
13331333
build_sk_flow_key(fl4, sk);
13341334
}
13351335

1336-
static DEFINE_SPINLOCK(fnhe_lock);
1336+
static DEFINE_SEQLOCK(fnhe_seqlock);
13371337

1338-
static struct fib_nh_exception *fnhe_oldest(struct fnhe_hash_bucket *hash, __be32 daddr)
1338+
static struct fib_nh_exception *fnhe_oldest(struct fnhe_hash_bucket *hash)
13391339
{
13401340
struct fib_nh_exception *fnhe, *oldest;
13411341

@@ -1358,47 +1358,63 @@ static inline u32 fnhe_hashfun(__be32 daddr)
13581358
return hval & (FNHE_HASH_SIZE - 1);
13591359
}
13601360

1361-
static struct fib_nh_exception *find_or_create_fnhe(struct fib_nh *nh, __be32 daddr)
1361+
static void update_or_create_fnhe(struct fib_nh *nh, __be32 daddr, __be32 gw,
1362+
u32 pmtu, unsigned long expires)
13621363
{
1363-
struct fnhe_hash_bucket *hash = nh->nh_exceptions;
1364+
struct fnhe_hash_bucket *hash;
13641365
struct fib_nh_exception *fnhe;
13651366
int depth;
1366-
u32 hval;
1367+
u32 hval = fnhe_hashfun(daddr);
1368+
1369+
write_seqlock_bh(&fnhe_seqlock);
13671370

1371+
hash = nh->nh_exceptions;
13681372
if (!hash) {
1369-
hash = nh->nh_exceptions = kzalloc(FNHE_HASH_SIZE * sizeof(*hash),
1370-
GFP_ATOMIC);
1373+
hash = kzalloc(FNHE_HASH_SIZE * sizeof(*hash), GFP_ATOMIC);
13711374
if (!hash)
1372-
return NULL;
1375+
goto out_unlock;
1376+
nh->nh_exceptions = hash;
13731377
}
13741378

1375-
hval = fnhe_hashfun(daddr);
13761379
hash += hval;
13771380

13781381
depth = 0;
13791382
for (fnhe = rcu_dereference(hash->chain); fnhe;
13801383
fnhe = rcu_dereference(fnhe->fnhe_next)) {
13811384
if (fnhe->fnhe_daddr == daddr)
1382-
goto out;
1385+
break;
13831386
depth++;
13841387
}
13851388

1386-
if (depth > FNHE_RECLAIM_DEPTH) {
1387-
fnhe = fnhe_oldest(hash + hval, daddr);
1388-
goto out_daddr;
1389+
if (fnhe) {
1390+
if (gw)
1391+
fnhe->fnhe_gw = gw;
1392+
if (pmtu) {
1393+
fnhe->fnhe_pmtu = pmtu;
1394+
fnhe->fnhe_expires = expires;
1395+
}
1396+
} else {
1397+
if (depth > FNHE_RECLAIM_DEPTH)
1398+
fnhe = fnhe_oldest(hash);
1399+
else {
1400+
fnhe = kzalloc(sizeof(*fnhe), GFP_ATOMIC);
1401+
if (!fnhe)
1402+
goto out_unlock;
1403+
1404+
fnhe->fnhe_next = hash->chain;
1405+
rcu_assign_pointer(hash->chain, fnhe);
1406+
}
1407+
fnhe->fnhe_daddr = daddr;
1408+
fnhe->fnhe_gw = gw;
1409+
fnhe->fnhe_pmtu = pmtu;
1410+
fnhe->fnhe_expires = expires;
13891411
}
1390-
fnhe = kzalloc(sizeof(*fnhe), GFP_ATOMIC);
1391-
if (!fnhe)
1392-
return NULL;
1393-
1394-
fnhe->fnhe_next = hash->chain;
1395-
rcu_assign_pointer(hash->chain, fnhe);
13961412

1397-
out_daddr:
1398-
fnhe->fnhe_daddr = daddr;
1399-
out:
14001413
fnhe->fnhe_stamp = jiffies;
1401-
return fnhe;
1414+
1415+
out_unlock:
1416+
write_sequnlock_bh(&fnhe_seqlock);
1417+
return;
14021418
}
14031419

14041420
static void __ip_do_redirect(struct rtable *rt, struct sk_buff *skb, struct flowi4 *fl4)
@@ -1452,13 +1468,9 @@ static void __ip_do_redirect(struct rtable *rt, struct sk_buff *skb, struct flow
14521468
} else {
14531469
if (fib_lookup(net, fl4, &res) == 0) {
14541470
struct fib_nh *nh = &FIB_RES_NH(res);
1455-
struct fib_nh_exception *fnhe;
14561471

1457-
spin_lock_bh(&fnhe_lock);
1458-
fnhe = find_or_create_fnhe(nh, fl4->daddr);
1459-
if (fnhe)
1460-
fnhe->fnhe_gw = new_gw;
1461-
spin_unlock_bh(&fnhe_lock);
1472+
update_or_create_fnhe(nh, fl4->daddr, new_gw,
1473+
0, 0);
14621474
}
14631475
rt->rt_gateway = new_gw;
14641476
rt->rt_flags |= RTCF_REDIRECTED;
@@ -1663,15 +1675,9 @@ static void __ip_rt_update_pmtu(struct rtable *rt, struct flowi4 *fl4, u32 mtu)
16631675

16641676
if (fib_lookup(dev_net(rt->dst.dev), fl4, &res) == 0) {
16651677
struct fib_nh *nh = &FIB_RES_NH(res);
1666-
struct fib_nh_exception *fnhe;
16671678

1668-
spin_lock_bh(&fnhe_lock);
1669-
fnhe = find_or_create_fnhe(nh, fl4->daddr);
1670-
if (fnhe) {
1671-
fnhe->fnhe_pmtu = mtu;
1672-
fnhe->fnhe_expires = jiffies + ip_rt_mtu_expires;
1673-
}
1674-
spin_unlock_bh(&fnhe_lock);
1679+
update_or_create_fnhe(nh, fl4->daddr, 0, mtu,
1680+
jiffies + ip_rt_mtu_expires);
16751681
}
16761682
rt->rt_pmtu = mtu;
16771683
dst_set_expires(&rt->dst, ip_rt_mtu_expires);
@@ -1902,23 +1908,35 @@ static void rt_bind_exception(struct rtable *rt, struct fib_nh *nh, __be32 daddr
19021908

19031909
hval = fnhe_hashfun(daddr);
19041910

1911+
restart:
19051912
for (fnhe = rcu_dereference(hash[hval].chain); fnhe;
19061913
fnhe = rcu_dereference(fnhe->fnhe_next)) {
1907-
if (fnhe->fnhe_daddr == daddr) {
1908-
if (fnhe->fnhe_pmtu) {
1909-
unsigned long expires = fnhe->fnhe_expires;
1910-
unsigned long diff = expires - jiffies;
1911-
1912-
if (time_before(jiffies, expires)) {
1913-
rt->rt_pmtu = fnhe->fnhe_pmtu;
1914-
dst_set_expires(&rt->dst, diff);
1915-
}
1914+
__be32 fnhe_daddr, gw;
1915+
unsigned long expires;
1916+
unsigned int seq;
1917+
u32 pmtu;
1918+
1919+
seq = read_seqbegin(&fnhe_seqlock);
1920+
fnhe_daddr = fnhe->fnhe_daddr;
1921+
gw = fnhe->fnhe_gw;
1922+
pmtu = fnhe->fnhe_pmtu;
1923+
expires = fnhe->fnhe_expires;
1924+
if (read_seqretry(&fnhe_seqlock, seq))
1925+
goto restart;
1926+
if (daddr != fnhe_daddr)
1927+
continue;
1928+
if (pmtu) {
1929+
unsigned long diff = jiffies - expires;
1930+
1931+
if (time_before(jiffies, expires)) {
1932+
rt->rt_pmtu = pmtu;
1933+
dst_set_expires(&rt->dst, diff);
19161934
}
1917-
if (fnhe->fnhe_gw)
1918-
rt->rt_gateway = fnhe->fnhe_gw;
1919-
fnhe->fnhe_stamp = jiffies;
1920-
break;
19211935
}
1936+
if (gw)
1937+
rt->rt_gateway = gw;
1938+
fnhe->fnhe_stamp = jiffies;
1939+
break;
19221940
}
19231941
}
19241942

0 commit comments

Comments
 (0)