Skip to content

Commit 0ed8ddf

Browse files
Eric Dumazetdavem330
authored andcommitted
neigh: Protect neigh->ha[] with a seqlock
Add a seqlock in struct neighbour to protect neigh->ha[], and avoid dirtying neighbour in stress situation (many different flows / dsts) Dirtying takes place because of read_lock(&n->lock) and n->used writes. Switching to a seqlock, and writing n->used only on jiffies changes permits less dirtying. Signed-off-by: Eric Dumazet <eric.dumazet@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent d122179 commit 0ed8ddf

4 files changed

Lines changed: 51 additions & 26 deletions

File tree

‎include/net/neighbour.h‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ struct neighbour {
105105
atomic_t refcnt;
106106
atomic_t probes;
107107
rwlock_t lock;
108+
seqlock_t ha_lock;
108109
unsigned char ha[ALIGN(MAX_ADDR_LEN, sizeof(unsigned long))];
109110
struct hh_cache *hh;
110111
int (*output)(struct sk_buff *skb);
@@ -302,7 +303,10 @@ static inline void neigh_confirm(struct neighbour *neigh)
302303

303304
static inline int neigh_event_send(struct neighbour *neigh, struct sk_buff *skb)
304305
{
305-
neigh->used = jiffies;
306+
unsigned long now = ACCESS_ONCE(jiffies);
307+
308+
if (neigh->used != now)
309+
neigh->used = now;
306310
if (!(neigh->nud_state&(NUD_CONNECTED|NUD_DELAY|NUD_PROBE)))
307311
return __neigh_event_send(neigh, skb);
308312
return 0;
@@ -373,4 +377,14 @@ struct neighbour_cb {
373377

374378
#define NEIGH_CB(skb) ((struct neighbour_cb *)(skb)->cb)
375379

380+
static inline void neigh_ha_snapshot(char *dst, const struct neighbour *n,
381+
const struct net_device *dev)
382+
{
383+
unsigned int seq;
384+
385+
do {
386+
seq = read_seqbegin(&n->ha_lock);
387+
memcpy(dst, n->ha, dev->addr_len);
388+
} while (read_seqretry(&n->ha_lock, seq));
389+
}
376390
#endif

‎net/core/neighbour.c‎

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ static struct neighbour *neigh_alloc(struct neigh_table *tbl)
294294

295295
skb_queue_head_init(&n->arp_queue);
296296
rwlock_init(&n->lock);
297+
seqlock_init(&n->ha_lock);
297298
n->updated = n->used = now;
298299
n->nud_state = NUD_NONE;
299300
n->output = neigh_blackhole;
@@ -1015,7 +1016,7 @@ int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb)
10151016
}
10161017
EXPORT_SYMBOL(__neigh_event_send);
10171018

1018-
static void neigh_update_hhs(struct neighbour *neigh)
1019+
static void neigh_update_hhs(const struct neighbour *neigh)
10191020
{
10201021
struct hh_cache *hh;
10211022
void (*update)(struct hh_cache*, const struct net_device*, const unsigned char *)
@@ -1151,7 +1152,9 @@ int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new,
11511152
}
11521153

11531154
if (lladdr != neigh->ha) {
1155+
write_seqlock(&neigh->ha_lock);
11541156
memcpy(&neigh->ha, lladdr, dev->addr_len);
1157+
write_sequnlock(&neigh->ha_lock);
11551158
neigh_update_hhs(neigh);
11561159
if (!(new & NUD_CONNECTED))
11571160
neigh->confirmed = jiffies -
@@ -1214,6 +1217,7 @@ static inline bool neigh_hh_lookup(struct neighbour *n, struct dst_entry *dst,
12141217
{
12151218
struct hh_cache *hh;
12161219

1220+
smp_rmb(); /* paired with smp_wmb() in neigh_hh_init() */
12171221
for (hh = n->hh; hh; hh = hh->hh_next) {
12181222
if (hh->hh_type == protocol) {
12191223
atomic_inc(&hh->hh_refcnt);
@@ -1248,8 +1252,8 @@ static void neigh_hh_init(struct neighbour *n, struct dst_entry *dst,
12481252
kfree(hh);
12491253
return;
12501254
}
1251-
read_unlock(&n->lock);
1252-
write_lock(&n->lock);
1255+
1256+
write_lock_bh(&n->lock);
12531257

12541258
/* must check if another thread already did the insert */
12551259
if (neigh_hh_lookup(n, dst, protocol)) {
@@ -1263,13 +1267,13 @@ static void neigh_hh_init(struct neighbour *n, struct dst_entry *dst,
12631267
hh->hh_output = n->ops->output;
12641268

12651269
hh->hh_next = n->hh;
1270+
smp_wmb(); /* paired with smp_rmb() in neigh_hh_lookup() */
12661271
n->hh = hh;
12671272

12681273
if (unlikely(cmpxchg(&dst->hh, NULL, hh) != NULL))
12691274
hh_cache_put(hh);
12701275
end:
1271-
write_unlock(&n->lock);
1272-
read_lock(&n->lock);
1276+
write_unlock_bh(&n->lock);
12731277
}
12741278

12751279
/* This function can be used in contexts, where only old dev_queue_xmit
@@ -1308,16 +1312,18 @@ int neigh_resolve_output(struct sk_buff *skb)
13081312
if (!neigh_event_send(neigh, skb)) {
13091313
int err;
13101314
struct net_device *dev = neigh->dev;
1315+
unsigned int seq;
13111316

1312-
read_lock_bh(&neigh->lock);
13131317
if (dev->header_ops->cache &&
13141318
!dst->hh &&
13151319
!(dst->flags & DST_NOCACHE))
13161320
neigh_hh_init(neigh, dst, dst->ops->protocol);
13171321

1318-
err = dev_hard_header(skb, dev, ntohs(skb->protocol),
1319-
neigh->ha, NULL, skb->len);
1320-
read_unlock_bh(&neigh->lock);
1322+
do {
1323+
seq = read_seqbegin(&neigh->ha_lock);
1324+
err = dev_hard_header(skb, dev, ntohs(skb->protocol),
1325+
neigh->ha, NULL, skb->len);
1326+
} while (read_seqretry(&neigh->ha_lock, seq));
13211327

13221328
if (err >= 0)
13231329
rc = neigh->ops->queue_xmit(skb);
@@ -1344,13 +1350,16 @@ int neigh_connected_output(struct sk_buff *skb)
13441350
struct dst_entry *dst = skb_dst(skb);
13451351
struct neighbour *neigh = dst->neighbour;
13461352
struct net_device *dev = neigh->dev;
1353+
unsigned int seq;
13471354

13481355
__skb_pull(skb, skb_network_offset(skb));
13491356

1350-
read_lock_bh(&neigh->lock);
1351-
err = dev_hard_header(skb, dev, ntohs(skb->protocol),
1352-
neigh->ha, NULL, skb->len);
1353-
read_unlock_bh(&neigh->lock);
1357+
do {
1358+
seq = read_seqbegin(&neigh->ha_lock);
1359+
err = dev_hard_header(skb, dev, ntohs(skb->protocol),
1360+
neigh->ha, NULL, skb->len);
1361+
} while (read_seqretry(&neigh->ha_lock, seq));
1362+
13541363
if (err >= 0)
13551364
err = neigh->ops->queue_xmit(skb);
13561365
else {
@@ -2148,10 +2157,14 @@ static int neigh_fill_info(struct sk_buff *skb, struct neighbour *neigh,
21482157

21492158
read_lock_bh(&neigh->lock);
21502159
ndm->ndm_state = neigh->nud_state;
2151-
if ((neigh->nud_state & NUD_VALID) &&
2152-
nla_put(skb, NDA_LLADDR, neigh->dev->addr_len, neigh->ha) < 0) {
2153-
read_unlock_bh(&neigh->lock);
2154-
goto nla_put_failure;
2160+
if (neigh->nud_state & NUD_VALID) {
2161+
char haddr[MAX_ADDR_LEN];
2162+
2163+
neigh_ha_snapshot(haddr, neigh, neigh->dev);
2164+
if (nla_put(skb, NDA_LLADDR, neigh->dev->addr_len, haddr) < 0) {
2165+
read_unlock_bh(&neigh->lock);
2166+
goto nla_put_failure;
2167+
}
21552168
}
21562169

21572170
ci.ndm_used = jiffies_to_clock_t(now - neigh->used);

‎net/ipv4/arp.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -502,10 +502,8 @@ int arp_find(unsigned char *haddr, struct sk_buff *skb)
502502

503503
if (n) {
504504
n->used = jiffies;
505-
if (n->nud_state&NUD_VALID || neigh_event_send(n, skb) == 0) {
506-
read_lock_bh(&n->lock);
507-
memcpy(haddr, n->ha, dev->addr_len);
508-
read_unlock_bh(&n->lock);
505+
if (n->nud_state & NUD_VALID || neigh_event_send(n, skb) == 0) {
506+
neigh_ha_snapshot(haddr, n, dev);
509507
neigh_release(n);
510508
return 0;
511509
}

‎net/sched/sch_teql.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,11 @@ __teql_resolve(struct sk_buff *skb, struct sk_buff *skb_res, struct net_device *
241241
}
242242
if (neigh_event_send(n, skb_res) == 0) {
243243
int err;
244+
char haddr[MAX_ADDR_LEN];
244245

245-
read_lock(&n->lock);
246-
err = dev_hard_header(skb, dev, ntohs(skb->protocol),
247-
n->ha, NULL, skb->len);
248-
read_unlock(&n->lock);
246+
neigh_ha_snapshot(haddr, n, dev);
247+
err = dev_hard_header(skb, dev, ntohs(skb->protocol), haddr,
248+
NULL, skb->len);
249249

250250
if (err < 0) {
251251
neigh_release(n);

0 commit comments

Comments
 (0)