net: rfs: hash function change
Subsystems (2)
PATCH 1/1 net: rfs: hash function change
RFS is using two kinds of hash tables.
First one is controled by /proc/sys/net/core/rps_sock_flow_entries = 2^N and using the N low order bits of the l4 hash is good enough.
Then each RX queue has its own hash table, controled by /sys/class/net/eth1/queues/rx-$q/rps_flow_cnt = 2^X
Current hash function, using the X low order bits is suboptimal, because RSS is usually using Func(hash) = (hash % power_of_two);
For example, with 32 RX queues, 6 low order bits have no entropy for a given queue.
Switch this hash function to hash_32(hash, log) to increase chances to use all possible slots and reduce collisions.
--- include/net/rps.h | 2 +- net/core/dev.c | 13 +++++++++---- net/core/net-sysfs.c | 4 ++-- 3 files changed, 12 insertions(+), 7 deletions(-)
include/net/rps.h +1 -1
@@ -39,7 +39,7 @@ struct rps_dev_flow {* The rps_dev_flow_table structure contains a table of flow mappings. */ struct rps_dev_flow_table { -unsigned int mask; +u8 log;struct rcu_head rcu;struct rps_dev_flow flows[]; };
net/core/dev.c +9 -4
@@ -4731,6 +4731,11 @@ EXPORT_SYMBOL(rps_needed);struct static_key_false rfs_needed__read_mostly ;EXPORT_SYMBOL (rfs_needed); +static u32 rfs_slot (u32 hash,const struct rps_dev_flow_table *flow_table) +{ +return hash_32 (hash, flow_table->log); +} +static struct rps_dev_flow *set_rps_cpu (struct net_device *dev,struct sk_buff *skb,struct rps_dev_flow *rflow,u16 next_cpu)
@@ -4757,7 +4762,7 @@ set_rps_cpu(struct net_device *dev, struct sk_buff *skb, flow_table =rcu_dereference (rxqueue->rps_flow_table);if (!flow_table) goto out; - flow_id =skb_get_hash (skb) & flow_table->mask; + flow_id =rfs_slot (skb_get_hash (skb), flow_table); rc = dev->netdev_ops->ndo_rx_flow_steer (dev, skb, rxq_index, flow_id);if (rc <0 )
@@ -4836,7 +4841,7 @@ static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,/* OK, now we know there is a match, * we can look at the local (per receive queue) flow table */ - rflow = &flow_table->flows[hash & flow_table->mask]; + rflow = &flow_table->flows[rfs_slot (hash, flow_table)]; tcpu = rflow->cpu;/*
@@ -4903,13 +4908,13 @@ bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index,rcu_read_lock (); flow_table =rcu_dereference (rxqueue->rps_flow_table); -if (flow_table && flow_id <= flow_table->mask) { +if (flow_table && flow_id < (1UL << flow_table->log)) { rflow = &flow_table->flows[flow_id]; cpu =READ_ONCE (rflow->cpu);if (READ_ONCE (rflow->filter) == filter_id && cpu < nr_cpu_ids && ((int )(READ_ONCE (per_cpu (softnet_data, cpu).input_queue_head) -READ_ONCE (rflow->last_qtail)) < - (int )(10 * flow_table->mask))) + (int )(10 << flow_table->log))) expire = false; }rcu_read_unlock ();
net/core/net-sysfs.c +2 -2
@@ -1056,7 +1056,7 @@ static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,rcu_read_lock (); flow_table =rcu_dereference (queue->rps_flow_table);if (flow_table) - val = (unsigned long )flow_table->mask +1 ; + val =1UL << flow_table->log;rcu_read_unlock ();return sysfs_emit (buf,"%lu\n" , val);
@@ -1109,7 +1109,7 @@ static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,if (!table)return -ENOMEM ; - table->mask = mask; + table->log =ilog2 (mask) +1 ;for (count =0 ; count <= mask; count++) table->flows[count].cpu =RPS_NO_CPU ; }else {
Signature · 1 line
2.49.0.395.g12beb8f557-goog
Quoted 11 lines · Eric Dumazet
RFS is using two kinds of hash tables.
First one is controled by
/proc/sys/net/core/rps_sock_flow_entries= 2^N and using the N low order bits of the l4 hash is good enough.Then each RX queue has its own hash table, controled by
/sys/class/net/eth1/queues/rx-$q/rps_flow_cnt= 2^XCurrent hash function, using the X low order bits is suboptimal, because RSS is usually using Func(hash) = (hash %
power_of_two);
Quoted 8 lines
For example, with 32 RX queues, 6 low order bits have no entropy for a given queue.
Switch this hash function to
hash_32(hash, log)to increase chances to use all possible slots and reduce collisions.Signed-off-by Eric Dumazet <[email protected]>Cc Tom Herbert <[email protected]>
...
net/core/dev.c bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index, · line 4917
Quoted 13 lines
net/core/dev.c +2
-2@@ -4903,13 +4908,13 @@ bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index,rcu_read_lock (); flow_table =rcu_dereference (rxqueue->rps_flow_table); -if (flow_table && flow_id <= flow_table->mask) { +if (flow_table && flow_id < (1UL << flow_table->log)) { rflow = &flow_table->flows[flow_id]; cpu =READ_ONCE (rflow->cpu);if (READ_ONCE (rflow->filter) == filter_id && cpu < nr_cpu_ids && ((int )(READ_ONCE (per_cpu (softnet_data, cpu).input_queue_head) -READ_ONCE (rflow->last_qtail)) < - (int )(10 * flow_table->mask))) + (int )(10 << flow_table->log)))
I am assuming that we don't care that (10 * flow_table->mask) and (10 << flow_table->log) are close but not exactly the same.
e.g. mask = 0x3f => log = 6
10 * 0x3f = 630
10 << 6 = 640Quoted 3 lines
net/core/dev.c +0
-0expire = false; }rcu_read_unlock ();
...
Quoted 43 lines · Simon Horman
Quoted 11 lines · Eric Dumazet
RFS is using two kinds of hash tables.
First one is controled by
/proc/sys/net/core/rps_sock_flow_entries= 2^N and using the N low order bits of the l4 hash is good enough.Then each RX queue has its own hash table, controled by
/sys/class/net/eth1/queues/rx-$q/rps_flow_cnt= 2^XCurrent hash function, using the X low order bits is suboptimal, because RSS is usually using Func(hash) = (hash %
power_of_two);Quoted 8 lines
For example, with 32 RX queues, 6 low order bits have no entropy for a given queue.
Switch this hash function to
hash_32(hash, log)to increase chances to use all possible slots and reduce collisions.Signed-off-by Eric Dumazet <[email protected]>Cc Tom Herbert <[email protected]>Reviewed-by Simon Horman <[email protected]>...
net/core/dev.c bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index, · line 4917
Quoted 13 lines
net/core/dev.c +2
-2@@ -4903,13 +4908,13 @@ bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index,rcu_read_lock (); flow_table =rcu_dereference (rxqueue->rps_flow_table); -if (flow_table && flow_id <= flow_table->mask) { +if (flow_table && flow_id < (1UL << flow_table->log)) { rflow = &flow_table->flows[flow_id]; cpu =READ_ONCE (rflow->cpu);if (READ_ONCE (rflow->filter) == filter_id && cpu < nr_cpu_ids && ((int )(READ_ONCE (per_cpu (softnet_data, cpu).input_queue_head) -READ_ONCE (rflow->last_qtail)) < - (int )(10 * flow_table->mask))) + (int )(10 << flow_table->log)))I am assuming that we don't care that (10 *
flow_table->mask) and (10 <<flow_table->log) are close but not exactly the same.
Quoted 1 line
e.g. mask =
0x3f=> log = 6
Yes, I doubt we care.
The 10 constant seems quite arbitrary anyway.
We also could keep both fields in flow_table : ->log and ->mask
I chose to remove ->mask mostly to detect all places needing scrutiny for the hash function change.
Simon Horman
reviewerQuoted 46 lines · Eric Dumazet
Quoted 43 lines · Simon Horman
Quoted 11 lines · Eric Dumazet
RFS is using two kinds of hash tables.
First one is controled by
/proc/sys/net/core/rps_sock_flow_entries= 2^N and using the N low order bits of the l4 hash is good enough.Then each RX queue has its own hash table, controled by
/sys/class/net/eth1/queues/rx-$q/rps_flow_cnt= 2^XCurrent hash function, using the X low order bits is suboptimal, because RSS is usually using Func(hash) = (hash %
power_of_two);Quoted 8 lines
For example, with 32 RX queues, 6 low order bits have no entropy for a given queue.
Switch this hash function to
hash_32(hash, log)to increase chances to use all possible slots and reduce collisions.Signed-off-by Eric Dumazet <[email protected]>Cc Tom Herbert <[email protected]>Reviewed-by Simon Horman <[email protected]>...
net/core/dev.c bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index, · line 4917
Quoted 13 lines
net/core/dev.c +2
-2@@ -4903,13 +4908,13 @@ bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index,rcu_read_lock (); flow_table =rcu_dereference (rxqueue->rps_flow_table); -if (flow_table && flow_id <= flow_table->mask) { +if (flow_table && flow_id < (1UL << flow_table->log)) { rflow = &flow_table->flows[flow_id]; cpu =READ_ONCE (rflow->cpu);if (READ_ONCE (rflow->filter) == filter_id && cpu < nr_cpu_ids && ((int )(READ_ONCE (per_cpu (softnet_data, cpu).input_queue_head) -READ_ONCE (rflow->last_qtail)) < - (int )(10 * flow_table->mask))) + (int )(10 << flow_table->log)))I am assuming that we don't care that (10 *
flow_table->mask) and (10 <<flow_table->log) are close but not exactly the same.Quoted 1 line
e.g. mask =
0x3f=> log = 6
Quoted 7 lines
Yes, I doubt we care.
The 10 constant seems quite arbitrary anyway.We also could keep both fields in
flow_table: ->log and ->maskI chose to remove ->mask mostly to detect all places needing scrutiny for the hash function change.
Thanks for the clarification. I agree that 10 seems arbitrary and that it is nice to remove ->mask entirely.
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 9 lines · On Fri, 21 Mar 2025 17:13:09 +0000 you wrote:
RFS is using two kinds of hash tables.
First one is controled by
/proc/sys/net/core/rps_sock_flow_entries= 2^N and using the N low order bits of the l4 hash is good enough.Then each RX queue has its own hash table, controled by
/sys/class/net/eth1/queues/rx-$q/rps_flow_cnt= 2^X[...]
Here is the summary with links:
- [net-next] net: rfs: hash function change
https://git.kernel.org/netdev/net-next/c/f3483c8e1da6
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html