netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net-next] net-procfs: use xarray iterator to implement /proc/net/dev
@ 2024-02-07 16:53 Eric Dumazet
  2024-02-07 17:55 ` Jakub Kicinski
  2024-02-09  3:11 ` patchwork-bot+netdevbpf
  0 siblings, 2 replies; 3+ messages in thread
From: Eric Dumazet @ 2024-02-07 16:53 UTC (permalink / raw)
  To: David S . Miller, Jakub Kicinski, Paolo Abeni
  Cc: netdev, eric.dumazet, Eric Dumazet

In commit 759ab1edb56c ("net: store netdevs in an xarray")
Jakub added net->dev_by_index to map ifindex to netdevices.

We can get rid of the old hash table (net->dev_index_head),
one patch at a time, if performance is acceptable.

This patch removes unpleasant code to something more readable.

As a bonus, /proc/net/dev gets netdevices sorted by their ifindex.

Signed-off-by: Eric Dumazet <edumazet@google.com>
---
 net/core/net-procfs.c | 48 +++++++------------------------------------
 1 file changed, 7 insertions(+), 41 deletions(-)

diff --git a/net/core/net-procfs.c b/net/core/net-procfs.c
index 09f7ed1a04e8ab881e461971e919728641927252..2e4e96d30ee1a7a51e49587378aab47aed1290da 100644
--- a/net/core/net-procfs.c
+++ b/net/core/net-procfs.c
@@ -6,49 +6,18 @@
 
 #include "dev.h"
 
-#define BUCKET_SPACE (32 - NETDEV_HASHBITS - 1)
-
-#define get_bucket(x) ((x) >> BUCKET_SPACE)
-#define get_offset(x) ((x) & ((1 << BUCKET_SPACE) - 1))
-#define set_bucket_offset(b, o) ((b) << BUCKET_SPACE | (o))
-
-static inline struct net_device *dev_from_same_bucket(struct seq_file *seq, loff_t *pos)
+static void *dev_seq_from_index(struct seq_file *seq, loff_t *pos)
 {
-	struct net *net = seq_file_net(seq);
+	unsigned long ifindex = *pos;
 	struct net_device *dev;
-	struct hlist_head *h;
-	unsigned int count = 0, offset = get_offset(*pos);
 
-	h = &net->dev_index_head[get_bucket(*pos)];
-	hlist_for_each_entry_rcu(dev, h, index_hlist) {
-		if (++count == offset)
-			return dev;
+	for_each_netdev_dump(seq_file_net(seq), dev, ifindex) {
+		*pos = dev->ifindex;
+		return dev;
 	}
-
-	return NULL;
-}
-
-static inline struct net_device *dev_from_bucket(struct seq_file *seq, loff_t *pos)
-{
-	struct net_device *dev;
-	unsigned int bucket;
-
-	do {
-		dev = dev_from_same_bucket(seq, pos);
-		if (dev)
-			return dev;
-
-		bucket = get_bucket(*pos) + 1;
-		*pos = set_bucket_offset(bucket, 1);
-	} while (bucket < NETDEV_HASHENTRIES);
-
 	return NULL;
 }
 
-/*
- *	This is invoked by the /proc filesystem handler to display a device
- *	in detail.
- */
 static void *dev_seq_start(struct seq_file *seq, loff_t *pos)
 	__acquires(RCU)
 {
@@ -56,16 +25,13 @@ static void *dev_seq_start(struct seq_file *seq, loff_t *pos)
 	if (!*pos)
 		return SEQ_START_TOKEN;
 
-	if (get_bucket(*pos) >= NETDEV_HASHENTRIES)
-		return NULL;
-
-	return dev_from_bucket(seq, pos);
+	return dev_seq_from_index(seq, pos);
 }
 
 static void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
 {
 	++*pos;
-	return dev_from_bucket(seq, pos);
+	return dev_seq_from_index(seq, pos);
 }
 
 static void dev_seq_stop(struct seq_file *seq, void *v)
-- 
2.43.0.594.gd9cf4e227d-goog


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH net-next] net-procfs: use xarray iterator to implement /proc/net/dev
  2024-02-07 16:53 [PATCH net-next] net-procfs: use xarray iterator to implement /proc/net/dev Eric Dumazet
@ 2024-02-07 17:55 ` Jakub Kicinski
  2024-02-09  3:11 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 3+ messages in thread
From: Jakub Kicinski @ 2024-02-07 17:55 UTC (permalink / raw)
  To: Eric Dumazet; +Cc: David S . Miller, Paolo Abeni, netdev, eric.dumazet

On Wed,  7 Feb 2024 16:53:18 +0000 Eric Dumazet wrote:
> In commit 759ab1edb56c ("net: store netdevs in an xarray")
> Jakub added net->dev_by_index to map ifindex to netdevices.
> 
> We can get rid of the old hash table (net->dev_index_head),
> one patch at a time, if performance is acceptable.

FWIW there was a basic benchmark result in that commit:

     #devs | hash |  xa  | delta
        2  | 18.3 | 20.1 | + 9.8%
       16  | 18.3 | 20.1 | + 9.5%
       64  | 18.3 | 26.3 | +43.8%
      128  | 20.4 | 26.3 | +28.6%
      256  | 20.0 | 26.4 | +32.1%
     1024  | 26.6 | 26.7 | + 0.2%
     8192  |541.3 | 33.5 | -93.8%

    The microbenchmark scans indexes in order, if the pattern is more
    random xa starts to win at 512 devices already. But that's a lot
    of devices, in practice.

obviously not a very realistic load, but I wanted to mention that,
in case you were planning to do similar testing. Having
wasted few hours yesterday reimplementing tls fixes Sabrina
already posted makes me hyper-vigilant now about people 
repeating work ;)

> This patch removes unpleasant code to something more readable.
> 
> As a bonus, /proc/net/dev gets netdevices sorted by their ifindex.
> 
> Signed-off-by: Eric Dumazet <edumazet@google.com>

Reviewed-by: Jakub Kicinski <kuba@kernel.org>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH net-next] net-procfs: use xarray iterator to implement /proc/net/dev
  2024-02-07 16:53 [PATCH net-next] net-procfs: use xarray iterator to implement /proc/net/dev Eric Dumazet
  2024-02-07 17:55 ` Jakub Kicinski
@ 2024-02-09  3:11 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 3+ messages in thread
From: patchwork-bot+netdevbpf @ 2024-02-09  3:11 UTC (permalink / raw)
  To: Eric Dumazet; +Cc: davem, kuba, pabeni, netdev, eric.dumazet

Hello:

This patch was applied to netdev/net-next.git (main)
by Jakub Kicinski <kuba@kernel.org>:

On Wed,  7 Feb 2024 16:53:18 +0000 you wrote:
> In commit 759ab1edb56c ("net: store netdevs in an xarray")
> Jakub added net->dev_by_index to map ifindex to netdevices.
> 
> We can get rid of the old hash table (net->dev_index_head),
> one patch at a time, if performance is acceptable.
> 
> This patch removes unpleasant code to something more readable.
> 
> [...]

Here is the summary with links:
  - [net-next] net-procfs: use xarray iterator to implement /proc/net/dev
    https://git.kernel.org/netdev/net-next/c/0e0939c0adf9

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-02-09  3:11 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-02-07 16:53 [PATCH net-next] net-procfs: use xarray iterator to implement /proc/net/dev Eric Dumazet
2024-02-07 17:55 ` Jakub Kicinski
2024-02-09  3:11 ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).