net/mlx5e: Prevent encap flow counter update async to user query [Linux 4.19.66]

This Linux kernel change "net/mlx5e: Prevent encap flow counter update async to user query" is included in the Linux 4.19.66 release. This change is authored by Ariel Levkovich <lariel [at]> on Sat Jul 6 18:06:15 2019 +0300. The commit for this change in Linux stable tree is 0ccf472 (patch) which is from upstream commit 90bb769. The same Linux upstream change may have been applied to various maintained Linux releases and you can find all Linux releases containing changes from upstream 90bb769.

net/mlx5e: Prevent encap flow counter update async to user query

[ Upstream commit 90bb769291161cf25a818d69cf608c181654473e ]

This patch prevents a race between user invoked cached counters
query and a neighbor last usage updater.

The cached flow counter stats can be queried by calling
"mlx5_fc_query_cached" which provides the number of bytes and
packets that passed via this flow since the last time this counter
was queried.
It does so by reducting the last saved stats from the current, cached
stats and then updating the last saved stats with the cached stats.
It also provide the lastuse value for that flow.

Since "mlx5e_tc_update_neigh_used_value" needs to retrieve the
last usage time of encapsulation flows, it calls the flow counter
query method periodically and async to user queries of the flow counter
using cls_flower.
This call is causing the driver to update the last reported bytes and
packets from the cache and therefore, future user queries of the flow
stats will return lower than expected number for bytes and packets
since the last saved stats in the driver was updated async to the last
saved stats in cls_flower.

This causes wrong stats presentation of encapsulation flows to user.

Since the neighbor usage updater only needs the lastuse stats from the
cached counter, the fix is to use a dedicated lastuse query call that
returns the lastuse value without synching between the cached stats and
the last saved stats.

Fixes: f6dfb4c3f216 ("net/mlx5e: Update neighbour 'used' state using HW flow rules counters")
Signed-off-by: Ariel Levkovich <>
Reviewed-by: Roi Dayan <>
Signed-off-by: Saeed Mahameed <>
Signed-off-by: Greg Kroah-Hartman <>

There are 10 lines of Linux source code added/deleted in this change. Code changes to Linux kernel are as follows.

 drivers/net/ethernet/mellanox/mlx5/core/en_tc.c       | 4 ++--
 drivers/net/ethernet/mellanox/mlx5/core/fs_counters.c | 5 +++++
 include/linux/mlx5/fs.h                               | 1 +
 3 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_tc.c b/drivers/net/ethernet/mellanox/mlx5/core/en_tc.c
index 9f7f842..c8928ce 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en_tc.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en_tc.c
@@ -992,13 +992,13 @@ void mlx5e_tc_encap_flows_del(struct mlx5e_priv *priv,
 void mlx5e_tc_update_neigh_used_value(struct mlx5e_neigh_hash_entry *nhe)
    struct mlx5e_neigh *m_neigh = &nhe->m_neigh;
-   u64 bytes, packets, lastuse = 0;
    struct mlx5e_tc_flow *flow;
    struct mlx5e_encap_entry *e;
    struct mlx5_fc *counter;
    struct neigh_table *tbl;
    bool neigh_used = false;
    struct neighbour *n;
+   u64 lastuse;

    if (m_neigh->family == AF_INET)
        tbl = &arp_tbl;
@@ -1015,7 +1015,7 @@ void mlx5e_tc_update_neigh_used_value(struct mlx5e_neigh_hash_entry *nhe)
        list_for_each_entry(flow, &e->flows, encap) {
            if (flow->flags & MLX5E_TC_FLOW_OFFLOADED) {
                counter = mlx5_flow_rule_counter(flow->rule[0]);
-               mlx5_fc_query_cached(counter, &bytes, &packets, &lastuse);
+               lastuse = mlx5_fc_query_lastuse(counter);
                if (time_after((unsigned long)lastuse, nhe->reported_lastuse)) {
                    neigh_used = true;
diff --git a/drivers/net/ethernet/mellanox/mlx5/core/fs_counters.c b/drivers/net/ethernet/mellanox/mlx5/core/fs_counters.c
index 58af6be..808ddd7 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/fs_counters.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/fs_counters.c
@@ -321,6 +321,11 @@ int mlx5_fc_query(struct mlx5_core_dev *dev, struct mlx5_fc *counter,

+u64 mlx5_fc_query_lastuse(struct mlx5_fc *counter)
+   return counter->cache.lastuse;
 void mlx5_fc_query_cached(struct mlx5_fc *counter,
              u64 *bytes, u64 *packets, u64 *lastuse)
diff --git a/include/linux/mlx5/fs.h b/include/linux/mlx5/fs.h
index 804516e..3386399 100644
--- a/include/linux/mlx5/fs.h
+++ b/include/linux/mlx5/fs.h
@@ -188,6 +188,7 @@ int mlx5_modify_rule_destination(struct mlx5_flow_handle *handler,
 struct mlx5_fc *mlx5_flow_rule_counter(struct mlx5_flow_handle *handler);
 struct mlx5_fc *mlx5_fc_create(struct mlx5_core_dev *dev, bool aging);
 void mlx5_fc_destroy(struct mlx5_core_dev *dev, struct mlx5_fc *counter);
+u64 mlx5_fc_query_lastuse(struct mlx5_fc *counter);
 void mlx5_fc_query_cached(struct mlx5_fc *counter,
              u64 *bytes, u64 *packets, u64 *lastuse);
 int mlx5_fc_query(struct mlx5_core_dev *dev, struct mlx5_fc *counter,

Leave a Reply

Your email address will not be published. Required fields are marked *