]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
net/mlx5: HWS, num_of_rules counter on matcher should be atomic
authorYevgeny Kliteynik <kliteyn@nvidia.com>
Thu, 2 Jan 2025 18:14:08 +0000 (20:14 +0200)
committerJakub Kicinski <kuba@kernel.org>
Tue, 7 Jan 2025 00:33:40 +0000 (16:33 -0800)
Rule counter in matcher's struct is used in two places:

1. As heuristics to decide when the number of rules have crossed a
certain percentage threshold and the matcher should be resized.
We don't mind here if the number will be off by 1-2 due to concurrency.

2. When destroying matcher, the counter value is checked and the
user is warned if it is not 0. Here we lock all the queues, so the
counter will be correct.

We don't need to always have *exact* number, but we do need this
number to not be corrupted, which is what is happening when the
counter isn't atomic, due to update by different threads.

Signed-off-by: Yevgeny Kliteynik <kliteyn@nvidia.com>
Reviewed-by: Erez Shitrit <erezsh@nvidia.com>
Reviewed-by: Mark Bloch <mbloch@nvidia.com>
Signed-off-by: Tariq Toukan <tariqt@nvidia.com>
Link: https://patch.msgid.link/20250102181415.1477316-10-tariqt@nvidia.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
drivers/net/ethernet/mellanox/mlx5/core/steering/hws/bwc.c
drivers/net/ethernet/mellanox/mlx5/core/steering/hws/bwc.h

index af8ab8750c703f436a210db87a63d3a864c388f4..40d688ed6153a376ba0b2b4307608a018fcce734 100644 (file)
@@ -152,6 +152,8 @@ mlx5hws_bwc_matcher_create(struct mlx5hws_table *table,
        if (!bwc_matcher)
                return NULL;
 
+       atomic_set(&bwc_matcher->num_of_rules, 0);
+
        /* Check if the required match params can be all matched
         * in single STE, otherwise complex matcher is needed.
         */
@@ -199,10 +201,12 @@ int mlx5hws_bwc_matcher_destroy_simple(struct mlx5hws_bwc_matcher *bwc_matcher)
 
 int mlx5hws_bwc_matcher_destroy(struct mlx5hws_bwc_matcher *bwc_matcher)
 {
-       if (bwc_matcher->num_of_rules)
+       u32 num_of_rules = atomic_read(&bwc_matcher->num_of_rules);
+
+       if (num_of_rules)
                mlx5hws_err(bwc_matcher->matcher->tbl->ctx,
                            "BWC matcher destroy: matcher still has %d rules\n",
-                           bwc_matcher->num_of_rules);
+                           num_of_rules);
 
        mlx5hws_bwc_matcher_destroy_simple(bwc_matcher);
 
@@ -309,7 +313,7 @@ static void hws_bwc_rule_list_add(struct mlx5hws_bwc_rule *bwc_rule, u16 idx)
 {
        struct mlx5hws_bwc_matcher *bwc_matcher = bwc_rule->bwc_matcher;
 
-       bwc_matcher->num_of_rules++;
+       atomic_inc(&bwc_matcher->num_of_rules);
        bwc_rule->bwc_queue_idx = idx;
        list_add(&bwc_rule->list_node, &bwc_matcher->rules[idx]);
 }
@@ -318,7 +322,7 @@ static void hws_bwc_rule_list_remove(struct mlx5hws_bwc_rule *bwc_rule)
 {
        struct mlx5hws_bwc_matcher *bwc_matcher = bwc_rule->bwc_matcher;
 
-       bwc_matcher->num_of_rules--;
+       atomic_dec(&bwc_matcher->num_of_rules);
        list_del_init(&bwc_rule->list_node);
 }
 
@@ -711,7 +715,8 @@ hws_bwc_matcher_rehash_size(struct mlx5hws_bwc_matcher *bwc_matcher)
         * Need to check again if we really need rehash.
         * If the reason for rehash was size, but not any more - skip rehash.
         */
-       if (!hws_bwc_matcher_rehash_size_needed(bwc_matcher, bwc_matcher->num_of_rules))
+       if (!hws_bwc_matcher_rehash_size_needed(bwc_matcher,
+                                               atomic_read(&bwc_matcher->num_of_rules)))
                return 0;
 
        /* Now we're done all the checking - do the rehash:
@@ -804,7 +809,7 @@ int mlx5hws_bwc_rule_create_simple(struct mlx5hws_bwc_rule *bwc_rule,
        }
 
        /* check if number of rules require rehash */
-       num_of_rules = bwc_matcher->num_of_rules;
+       num_of_rules = atomic_read(&bwc_matcher->num_of_rules);
 
        if (unlikely(hws_bwc_matcher_rehash_size_needed(bwc_matcher, num_of_rules))) {
                mutex_unlock(queue_lock);
index 1d27638fa171caf116bf46e44f01a0f9f20f81da..06c2a30c0d4e7804adf599da21a2dde2285252d8 100644 (file)
@@ -25,7 +25,7 @@ struct mlx5hws_bwc_matcher {
        u8 num_of_at;
        u16 priority;
        u8 size_log;
-       u32 num_of_rules; /* atomically accessed */
+       atomic_t num_of_rules;
        struct list_head *rules;
 };