Skip to content

Commit 1d8ce41

Browse files
krikkkuNipaLocal
authored andcommitted
net: Prevent RPS table overwrite of active flows
This patch fixes an issue where two different flows on the same RXq produce the same hash resulting in continuous flow overwrites. Flow kernel-patches#1: A packet for Flow kernel-patches#1 comes in, kernel calls the steering function. The driver gives back a filter id. The kernel saves this filter id in the selected slot. Later, the driver's service task checks if any filters have expired and then installs the rule for Flow kernel-patches#1. Flow kernel-patches#2: A packet for Flow kernel-patches#2 comes in. It goes through the same steps. But this time, the chosen slot is being used by Flow kernel-patches#1. The driver gives a new filter id and the kernel saves it in the same slot. When the driver's service task runs, it runs through all the flows, checks if Flow kernel-patches#1 should be expired, the kernel returns True as the slot has a different filter id, and then the driver installs the rule for Flow kernel-patches#2. Flow kernel-patches#1: Another packet for Flow kernel-patches#1 comes in. The same thing repeats. The slot is overwritten with a new filter id for Flow kernel-patches#1. This causes a repeated cycle of flow programming for missed packets, wasting CPU cycles while not improving performance. This problem happens at higher rates when the RPS table is small, but tests show it still happens even with 12,000 connections and an RPS size of 16K per queue (global table size = 144x16K = 64K). This patch prevents overwriting an rps_dev_flow entry if it is active. The intention is that it is better to do aRFS for the first flow instead of hurting all flows on the same hash. Without this, two (or more) flows on one RX queue with the same hash can keep overwriting each other. This causes the driver to reprogram the flow repeatedly. Changes: 1. Add a new 'hash' field to struct rps_dev_flow. 2. Add rps_flow_is_active(): a helper function to check if a flow is active or not, extracted from rps_may_expire_flow(). It is further simplified as per reviewer feedback. 3. In set_rps_cpu(): - Avoid overwriting by programming a new filter if: - The slot is not in use, or - The slot is in use but the flow is not active, or - The slot has an active flow with the same hash, but target CPU differs. - Save the hash in the rps_dev_flow entry. 4. rps_may_expire_flow(): Use earlier extracted rps_flow_is_active(). Testing & results: - Driver: ice (E810 NIC), Kernel: net-next - #CPUs = #RXq = 144 (1:1) - Number of flows: 12K - Eight RPS settings from 256 to 32768. Though RPS=256 is not ideal, it is still sufficient to cover 12K flows (256*144 rx-queues = 64K global table slots) - Global Table Size = 144 * RPS (effectively equal to 256 * RPS) - Each RPS test duration = 8 mins (org code) + 8 mins (new code). - Metrics captured on client Legend for following tables: Steer-C: #times ndo_rx_flow_steer() was Called by set_rps_cpu() Steer-L: #times ice_arfs_flow_steer() Looped over aRFS entries Add: #times driver actually programmed aRFS (ice_arfs_build_entry()) Del: #times driver deleted the flow (ice_arfs_del_flow_rules()) Units: K = 1,000 times, M = 1 million times |-------|---------|------| Org Code |---------|---------| | RPS | Latency | CPU | Add | Del | Steer-C | Steer-L | |-------|---------|------|--------|--------|---------|---------| | 256 | 227.0 | 93.2 | 1.6M | 1.6M | 121.7M | 267.6M | | 512 | 225.9 | 94.1 | 11.5M | 11.2M | 65.7M | 199.6M | | 1024 | 223.5 | 95.6 | 16.5M | 16.5M | 27.1M | 187.3M | | 2048 | 222.2 | 96.3 | 10.5M | 10.5M | 12.5M | 115.2M | | 4096 | 223.9 | 94.1 | 5.5M | 5.5M | 7.2M | 65.9M | | 8192 | 224.7 | 92.5 | 2.7M | 2.7M | 3.0M | 29.9M | | 16384 | 223.5 | 92.5 | 1.3M | 1.3M | 1.4M | 13.9M | | 32768 | 219.6 | 93.2 | 838.1K | 838.1K | 965.1K | 8.9M | |-------|---------|------| New Code |---------|---------| | 256 | 201.5 | 99.1 | 13.4K | 5.0K | 13.7K | 75.2K | | 512 | 202.5 | 98.2 | 11.2K | 5.9K | 11.2K | 55.5K | | 1024 | 207.3 | 93.9 | 11.5K | 9.7K | 11.5K | 59.6K | | 2048 | 207.5 | 96.7 | 11.8K | 11.1K | 15.5K | 79.3K | | 4096 | 206.9 | 96.6 | 11.8K | 11.7K | 11.8K | 63.2K | | 8192 | 205.8 | 96.7 | 11.9K | 11.8K | 11.9K | 63.9K | | 16384 | 200.9 | 98.2 | 11.9K | 11.9K | 11.9K | 64.2K | | 32768 | 202.5 | 98.0 | 11.9K | 11.9K | 11.9K | 64.2K | |-------|---------|------|--------|--------|---------|---------| Some observations: 1. Overall Latency improved: (1790.19-1634.94)/1790.19*100 = 8.67% 2. Overall CPU increased: (777.32-751.49)/751.45*100 = 3.44% 3. Flow Management (add/delete) remained almost constant at ~11K compared to values in millions. Reported-by: kernel test robot <lkp@intel.com> Closes: https://lore.kernel.org/oe-kbuild-all/202507161125.rUCoz9ov-lkp@intel.com/ Signed-off-by: Krishna Kumar <krikku@gmail.com> Signed-off-by: NipaLocal <nipa@local>
1 parent 87923d5 commit 1d8ce41

File tree

3 files changed

+65
-10
lines changed

3 files changed

+65
-10
lines changed

include/net/rps.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@ struct rps_map {
2525

2626
/*
2727
* The rps_dev_flow structure contains the mapping of a flow to a CPU, the
28-
* tail pointer for that CPU's input queue at the time of last enqueue, and
29-
* a hardware filter index.
28+
* tail pointer for that CPU's input queue at the time of last enqueue, a
29+
* hardware filter index, and the hash of the flow if aRFS is enabled.
3030
*/
3131
struct rps_dev_flow {
3232
u16 cpu;
3333
u16 filter;
3434
unsigned int last_qtail;
35+
#ifdef CONFIG_RFS_ACCEL
36+
u32 hash;
37+
#endif
3538
};
3639
#define RPS_NO_FILTER 0xffff
3740

net/core/dev.c

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4837,6 +4837,36 @@ static u32 rfs_slot(u32 hash, const struct rps_dev_flow_table *flow_table)
48374837
return hash_32(hash, flow_table->log);
48384838
}
48394839

4840+
#ifdef CONFIG_RFS_ACCEL
4841+
/**
4842+
* rps_flow_is_active - check whether the flow is recently active.
4843+
* @rflow: Specific flow to check activity.
4844+
* @flow_table: per-queue flowtable that @rflow belongs to.
4845+
* @cpu: CPU saved in @rflow.
4846+
*
4847+
* If the CPU has processed many packets since the flow's last activity
4848+
* (beyond 10 times the table size), the flow is considered stale.
4849+
*
4850+
* Return: true if flow was recently active.
4851+
*/
4852+
static bool rps_flow_is_active(struct rps_dev_flow *rflow,
4853+
struct rps_dev_flow_table *flow_table,
4854+
unsigned int cpu)
4855+
{
4856+
unsigned int flow_last_active;
4857+
unsigned int sd_input_head;
4858+
4859+
if (cpu >= nr_cpu_ids)
4860+
return false;
4861+
4862+
sd_input_head = READ_ONCE(per_cpu(softnet_data, cpu).input_queue_head);
4863+
flow_last_active = READ_ONCE(rflow->last_qtail);
4864+
4865+
return (int)(sd_input_head - flow_last_active) <
4866+
(int)(10 << flow_table->log);
4867+
}
4868+
#endif
4869+
48404870
static struct rps_dev_flow *
48414871
set_rps_cpu(struct net_device *dev, struct sk_buff *skb,
48424872
struct rps_dev_flow *rflow, u16 next_cpu)
@@ -4847,8 +4877,11 @@ set_rps_cpu(struct net_device *dev, struct sk_buff *skb,
48474877
struct netdev_rx_queue *rxqueue;
48484878
struct rps_dev_flow_table *flow_table;
48494879
struct rps_dev_flow *old_rflow;
4880+
struct rps_dev_flow *tmp_rflow;
4881+
unsigned int tmp_cpu;
48504882
u16 rxq_index;
48514883
u32 flow_id;
4884+
u32 hash;
48524885
int rc;
48534886

48544887
/* Should we steer this flow to a different hardware queue? */
@@ -4863,14 +4896,32 @@ set_rps_cpu(struct net_device *dev, struct sk_buff *skb,
48634896
flow_table = rcu_dereference(rxqueue->rps_flow_table);
48644897
if (!flow_table)
48654898
goto out;
4866-
flow_id = rfs_slot(skb_get_hash(skb), flow_table);
4899+
4900+
hash = skb_get_hash(skb);
4901+
flow_id = rfs_slot(hash, flow_table);
4902+
4903+
tmp_rflow = &flow_table->flows[flow_id];
4904+
tmp_cpu = READ_ONCE(tmp_rflow->cpu);
4905+
4906+
if (READ_ONCE(tmp_rflow->filter) != RPS_NO_FILTER) {
4907+
if (rps_flow_is_active(tmp_rflow, flow_table,
4908+
tmp_cpu)) {
4909+
if (hash != READ_ONCE(tmp_rflow->hash) ||
4910+
next_cpu == tmp_cpu)
4911+
goto out;
4912+
}
4913+
}
4914+
48674915
rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb,
48684916
rxq_index, flow_id);
48694917
if (rc < 0)
48704918
goto out;
4919+
48714920
old_rflow = rflow;
4872-
rflow = &flow_table->flows[flow_id];
4921+
rflow = tmp_rflow;
48734922
WRITE_ONCE(rflow->filter, rc);
4923+
WRITE_ONCE(rflow->hash, hash);
4924+
48744925
if (old_rflow->filter == rc)
48754926
WRITE_ONCE(old_rflow->filter, RPS_NO_FILTER);
48764927
out:
@@ -5005,17 +5056,16 @@ bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index,
50055056
struct rps_dev_flow_table *flow_table;
50065057
struct rps_dev_flow *rflow;
50075058
bool expire = true;
5008-
unsigned int cpu;
50095059

50105060
rcu_read_lock();
50115061
flow_table = rcu_dereference(rxqueue->rps_flow_table);
50125062
if (flow_table && flow_id < (1UL << flow_table->log)) {
5063+
unsigned int cpu;
5064+
50135065
rflow = &flow_table->flows[flow_id];
50145066
cpu = READ_ONCE(rflow->cpu);
5015-
if (READ_ONCE(rflow->filter) == filter_id && cpu < nr_cpu_ids &&
5016-
((int)(READ_ONCE(per_cpu(softnet_data, cpu).input_queue_head) -
5017-
READ_ONCE(rflow->last_qtail)) <
5018-
(int)(10 << flow_table->log)))
5067+
if (READ_ONCE(rflow->filter) == filter_id &&
5068+
rps_flow_is_active(rflow, flow_table, cpu))
50195069
expire = false;
50205070
}
50215071
rcu_read_unlock();

net/core/net-sysfs.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1120,8 +1120,10 @@ static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
11201120
return -ENOMEM;
11211121

11221122
table->log = ilog2(mask) + 1;
1123-
for (count = 0; count <= mask; count++)
1123+
for (count = 0; count <= mask; count++) {
11241124
table->flows[count].cpu = RPS_NO_CPU;
1125+
table->flows[count].filter = RPS_NO_FILTER;
1126+
}
11251127
} else {
11261128
table = NULL;
11271129
}

0 commit comments

Comments
 (0)