Skip to content

Commit f04565d

Browse files
mihaimaruseacdavem330
authored andcommitted
dev: use name hash for dev_seq_ops
Instead of using the dev->next chain and trying to resync at each call to dev_seq_start, use the name hash, keeping the bucket and the offset in seq->private field. Tests revealed the following results for ifconfig > /dev/null * 1000 interfaces: * 0.114s without patch * 0.089s with patch * 3000 interfaces: * 0.489s without patch * 0.110s with patch * 5000 interfaces: * 1.363s without patch * 0.250s with patch * 128000 interfaces (other setup): * ~100s without patch * ~30s with patch Signed-off-by: Mihai Maruseac <mmaruseac@ixiacom.com> Signed-off-by: Eric Dumazet <eric.dumazet@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent e09eff7 commit f04565d

1 file changed

Lines changed: 69 additions & 15 deletions

File tree

‎net/core/dev.c‎

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4093,40 +4093,94 @@ static int dev_ifconf(struct net *net, char __user *arg)
40934093
}
40944094

40954095
#ifdef CONFIG_PROC_FS
4096+
4097+
#define BUCKET_SPACE (32 - NETDEV_HASHBITS)
4098+
4099+
struct dev_iter_state {
4100+
struct seq_net_private p;
4101+
unsigned int pos; /* bucket << BUCKET_SPACE + offset */
4102+
};
4103+
4104+
#define get_bucket(x) ((x) >> BUCKET_SPACE)
4105+
#define get_offset(x) ((x) & ((1 << BUCKET_SPACE) - 1))
4106+
#define set_bucket_offset(b, o) ((b) << BUCKET_SPACE | (o))
4107+
4108+
static inline struct net_device *dev_from_same_bucket(struct seq_file *seq)
4109+
{
4110+
struct dev_iter_state *state = seq->private;
4111+
struct net *net = seq_file_net(seq);
4112+
struct net_device *dev;
4113+
struct hlist_node *p;
4114+
struct hlist_head *h;
4115+
unsigned int count, bucket, offset;
4116+
4117+
bucket = get_bucket(state->pos);
4118+
offset = get_offset(state->pos);
4119+
h = &net->dev_name_head[bucket];
4120+
count = 0;
4121+
hlist_for_each_entry_rcu(dev, p, h, name_hlist) {
4122+
if (count++ == offset) {
4123+
state->pos = set_bucket_offset(bucket, count);
4124+
return dev;
4125+
}
4126+
}
4127+
4128+
return NULL;
4129+
}
4130+
4131+
static inline struct net_device *dev_from_new_bucket(struct seq_file *seq)
4132+
{
4133+
struct dev_iter_state *state = seq->private;
4134+
struct net_device *dev;
4135+
unsigned int bucket;
4136+
4137+
bucket = get_bucket(state->pos);
4138+
do {
4139+
dev = dev_from_same_bucket(seq);
4140+
if (dev)
4141+
return dev;
4142+
4143+
bucket++;
4144+
state->pos = set_bucket_offset(bucket, 0);
4145+
} while (bucket < NETDEV_HASHENTRIES);
4146+
4147+
return NULL;
4148+
}
4149+
40964150
/*
40974151
* This is invoked by the /proc filesystem handler to display a device
40984152
* in detail.
40994153
*/
41004154
void *dev_seq_start(struct seq_file *seq, loff_t *pos)
41014155
__acquires(RCU)
41024156
{
4103-
struct net *net = seq_file_net(seq);
4104-
loff_t off;
4105-
struct net_device *dev;
4157+
struct dev_iter_state *state = seq->private;
41064158

41074159
rcu_read_lock();
41084160
if (!*pos)
41094161
return SEQ_START_TOKEN;
41104162

4111-
off = 1;
4112-
for_each_netdev_rcu(net, dev)
4113-
if (off++ == *pos)
4114-
return dev;
4163+
/* check for end of the hash */
4164+
if (state->pos == 0 && *pos > 1)
4165+
return NULL;
41154166

4116-
return NULL;
4167+
return dev_from_new_bucket(seq);
41174168
}
41184169

41194170
void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
41204171
{
4121-
struct net_device *dev = v;
4172+
struct net_device *dev;
4173+
4174+
++*pos;
41224175

41234176
if (v == SEQ_START_TOKEN)
4124-
dev = first_net_device_rcu(seq_file_net(seq));
4125-
else
4126-
dev = next_net_device_rcu(dev);
4177+
return dev_from_new_bucket(seq);
41274178

4128-
++*pos;
4129-
return dev;
4179+
dev = dev_from_same_bucket(seq);
4180+
if (dev)
4181+
return dev;
4182+
4183+
return dev_from_new_bucket(seq);
41304184
}
41314185

41324186
void dev_seq_stop(struct seq_file *seq, void *v)
@@ -4225,7 +4279,7 @@ static const struct seq_operations dev_seq_ops = {
42254279
static int dev_seq_open(struct inode *inode, struct file *file)
42264280
{
42274281
return seq_open_net(inode, file, &dev_seq_ops,
4228-
sizeof(struct seq_net_private));
4282+
sizeof(struct dev_iter_state));
42294283
}
42304284

42314285
static const struct file_operations dev_seq_fops = {

0 commit comments

Comments
 (0)