Skip to content

Commit d9f37d0

Browse files
lrq-maxdavem330
authored andcommitted
net: convert gro_count to bitmask
gro_hash size is 192 bytes, and uses 3 cache lines, if there is few flows, gro_hash may be not fully used, so it is unnecessary to iterate all gro_hash in napi_gro_flush(), to occupy unnecessary cacheline. convert gro_count to a bitmask, and rename it as gro_bitmask, each bit represents a element of gro_hash, only flush a gro_hash element if the related bit is set, to speed up napi_gro_flush(). and update gro_bitmask only if it will be changed, to reduce cache update Suggested-by: Eric Dumazet <[email protected]> Signed-off-by: Li RongQing <[email protected]> Cc: Stefano Brivio <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 48559af commit d9f37d0

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

include/linux/netdevice.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,14 @@ struct gro_list {
308308
};
309309

310310
/*
311-
* Structure for NAPI scheduling similar to tasklet but with weighting
311+
* size of gro hash buckets, must less than bit number of
312+
* napi_struct::gro_bitmask
312313
*/
313314
#define GRO_HASH_BUCKETS 8
315+
316+
/*
317+
* Structure for NAPI scheduling similar to tasklet but with weighting
318+
*/
314319
struct napi_struct {
315320
/* The poll_list must only be managed by the entity which
316321
* changes the state of the NAPI_STATE_SCHED bit. This means
@@ -322,7 +327,7 @@ struct napi_struct {
322327

323328
unsigned long state;
324329
int weight;
325-
unsigned int gro_count;
330+
unsigned long gro_bitmask;
326331
int (*poll)(struct napi_struct *, int);
327332
#ifdef CONFIG_NETPOLL
328333
int poll_owner;

net/core/dev.c

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5282,9 +5282,11 @@ static void __napi_gro_flush_chain(struct napi_struct *napi, u32 index,
52825282
list_del(&skb->list);
52835283
skb->next = NULL;
52845284
napi_gro_complete(skb);
5285-
napi->gro_count--;
52865285
napi->gro_hash[index].count--;
52875286
}
5287+
5288+
if (!napi->gro_hash[index].count)
5289+
__clear_bit(index, &napi->gro_bitmask);
52885290
}
52895291

52905292
/* napi->gro_hash[].list contains packets ordered by age.
@@ -5295,8 +5297,10 @@ void napi_gro_flush(struct napi_struct *napi, bool flush_old)
52955297
{
52965298
u32 i;
52975299

5298-
for (i = 0; i < GRO_HASH_BUCKETS; i++)
5299-
__napi_gro_flush_chain(napi, i, flush_old);
5300+
for (i = 0; i < GRO_HASH_BUCKETS; i++) {
5301+
if (test_bit(i, &napi->gro_bitmask))
5302+
__napi_gro_flush_chain(napi, i, flush_old);
5303+
}
53005304
}
53015305
EXPORT_SYMBOL(napi_gro_flush);
53025306

@@ -5388,8 +5392,8 @@ static void gro_flush_oldest(struct list_head *head)
53885392
if (WARN_ON_ONCE(!oldest))
53895393
return;
53905394

5391-
/* Do not adjust napi->gro_count, caller is adding a new SKB to
5392-
* the chain.
5395+
/* Do not adjust napi->gro_hash[].count, caller is adding a new
5396+
* SKB to the chain.
53935397
*/
53945398
list_del(&oldest->list);
53955399
napi_gro_complete(oldest);
@@ -5464,7 +5468,6 @@ static enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff
54645468
list_del(&pp->list);
54655469
pp->next = NULL;
54665470
napi_gro_complete(pp);
5467-
napi->gro_count--;
54685471
napi->gro_hash[hash].count--;
54695472
}
54705473

@@ -5477,7 +5480,6 @@ static enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff
54775480
if (unlikely(napi->gro_hash[hash].count >= MAX_GRO_SKBS)) {
54785481
gro_flush_oldest(gro_head);
54795482
} else {
5480-
napi->gro_count++;
54815483
napi->gro_hash[hash].count++;
54825484
}
54835485
NAPI_GRO_CB(skb)->count = 1;
@@ -5492,6 +5494,13 @@ static enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff
54925494
if (grow > 0)
54935495
gro_pull_from_frag0(skb, grow);
54945496
ok:
5497+
if (napi->gro_hash[hash].count) {
5498+
if (!test_bit(hash, &napi->gro_bitmask))
5499+
__set_bit(hash, &napi->gro_bitmask);
5500+
} else if (test_bit(hash, &napi->gro_bitmask)) {
5501+
__clear_bit(hash, &napi->gro_bitmask);
5502+
}
5503+
54955504
return ret;
54965505

54975506
normal:
@@ -5890,7 +5899,7 @@ bool napi_complete_done(struct napi_struct *n, int work_done)
58905899
NAPIF_STATE_IN_BUSY_POLL)))
58915900
return false;
58925901

5893-
if (n->gro_count) {
5902+
if (n->gro_bitmask) {
58945903
unsigned long timeout = 0;
58955904

58965905
if (work_done)
@@ -6099,7 +6108,7 @@ static enum hrtimer_restart napi_watchdog(struct hrtimer *timer)
60996108
/* Note : we use a relaxed variant of napi_schedule_prep() not setting
61006109
* NAPI_STATE_MISSED, since we do not react to a device IRQ.
61016110
*/
6102-
if (napi->gro_count && !napi_disable_pending(napi) &&
6111+
if (napi->gro_bitmask && !napi_disable_pending(napi) &&
61036112
!test_and_set_bit(NAPI_STATE_SCHED, &napi->state))
61046113
__napi_schedule_irqoff(napi);
61056114

@@ -6114,7 +6123,7 @@ void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
61146123
INIT_LIST_HEAD(&napi->poll_list);
61156124
hrtimer_init(&napi->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
61166125
napi->timer.function = napi_watchdog;
6117-
napi->gro_count = 0;
6126+
napi->gro_bitmask = 0;
61186127
for (i = 0; i < GRO_HASH_BUCKETS; i++) {
61196128
INIT_LIST_HEAD(&napi->gro_hash[i].list);
61206129
napi->gro_hash[i].count = 0;
@@ -6174,7 +6183,7 @@ void netif_napi_del(struct napi_struct *napi)
61746183
napi_free_frags(napi);
61756184

61766185
flush_gro_hash(napi);
6177-
napi->gro_count = 0;
6186+
napi->gro_bitmask = 0;
61786187
}
61796188
EXPORT_SYMBOL(netif_napi_del);
61806189

@@ -6216,7 +6225,7 @@ static int napi_poll(struct napi_struct *n, struct list_head *repoll)
62166225
goto out_unlock;
62176226
}
62186227

6219-
if (n->gro_count) {
6228+
if (n->gro_bitmask) {
62206229
/* flush too old packets
62216230
* If HZ < 1000, flush all packets.
62226231
*/
@@ -9272,6 +9281,9 @@ static struct hlist_head * __net_init netdev_create_hash(void)
92729281
/* Initialize per network namespace state */
92739282
static int __net_init netdev_init(struct net *net)
92749283
{
9284+
BUILD_BUG_ON(GRO_HASH_BUCKETS >
9285+
FIELD_SIZEOF(struct napi_struct, gro_bitmask));
9286+
92759287
if (net != &init_net)
92769288
INIT_LIST_HEAD(&net->dev_base_head);
92779289

0 commit comments

Comments
 (0)