Skip to content

Commit b581f42

Browse files
mosheshemesh2kuba-moo
authored andcommitted
net/mlx5: fs, manage flow counters HWS action sharing by refcount
Multiple flow counters can utilize a single Hardware Steering (HWS) action for Hardware Steering rules. Given that these counter bulks are not exclusively created for Hardware Steering, but also serve purposes such as statistics gathering and other steering modes, it's more efficient to create the HWS action only when it's first needed by a Hardware Steering rule. This approach allows for better resource management through the use of a reference count, rather than automatically creating an HWS action for every bulk of flow counters. Signed-off-by: Moshe Shemesh <[email protected]> Reviewed-by: Yevgeny Kliteynik <[email protected]> Reviewed-by: Mark Bloch <[email protected]> Signed-off-by: Tariq Toukan <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent b36315c commit b581f42

File tree

4 files changed

+94
-29
lines changed

4 files changed

+94
-29
lines changed

drivers/net/ethernet/mellanox/mlx5/core/fs_core.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,42 @@ struct mlx5_flow_root_namespace {
316316
const struct mlx5_flow_cmds *cmds;
317317
};
318318

319+
enum mlx5_fc_type {
320+
MLX5_FC_TYPE_ACQUIRED = 0,
321+
MLX5_FC_TYPE_LOCAL,
322+
};
323+
324+
struct mlx5_fc_cache {
325+
u64 packets;
326+
u64 bytes;
327+
u64 lastuse;
328+
};
329+
330+
struct mlx5_fc {
331+
u32 id;
332+
bool aging;
333+
enum mlx5_fc_type type;
334+
struct mlx5_fc_bulk *bulk;
335+
struct mlx5_fc_cache cache;
336+
/* last{packets,bytes} are used for calculating deltas since last reading. */
337+
u64 lastpackets;
338+
u64 lastbytes;
339+
};
340+
341+
struct mlx5_fc_bulk_hws_data {
342+
struct mlx5hws_action *hws_action;
343+
struct mutex lock; /* protects hws_action */
344+
refcount_t hws_action_refcount;
345+
};
346+
347+
struct mlx5_fc_bulk {
348+
struct mlx5_fs_bulk fs_bulk;
349+
u32 base_id;
350+
struct mlx5_fc_bulk_hws_data hws_data;
351+
struct mlx5_fc fcs[];
352+
};
353+
354+
u32 mlx5_fc_get_base_id(struct mlx5_fc *counter);
319355
int mlx5_init_fc_stats(struct mlx5_core_dev *dev);
320356
void mlx5_cleanup_fc_stats(struct mlx5_core_dev *dev);
321357
void mlx5_fc_queue_stats_work(struct mlx5_core_dev *dev,

drivers/net/ethernet/mellanox/mlx5/core/fs_counters.c

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,6 @@
4444
#define MLX5_FC_POOL_MAX_THRESHOLD BIT(18)
4545
#define MLX5_FC_POOL_USED_BUFF_RATIO 10
4646

47-
enum mlx5_fc_type {
48-
MLX5_FC_TYPE_ACQUIRED = 0,
49-
MLX5_FC_TYPE_LOCAL,
50-
};
51-
52-
struct mlx5_fc_cache {
53-
u64 packets;
54-
u64 bytes;
55-
u64 lastuse;
56-
};
57-
58-
struct mlx5_fc {
59-
u32 id;
60-
bool aging;
61-
enum mlx5_fc_type type;
62-
struct mlx5_fc_bulk *bulk;
63-
struct mlx5_fc_cache cache;
64-
/* last{packets,bytes} are used for calculating deltas since last reading. */
65-
u64 lastpackets;
66-
u64 lastbytes;
67-
};
68-
6947
struct mlx5_fc_stats {
7048
struct xarray counters;
7149

@@ -434,13 +412,7 @@ void mlx5_fc_update_sampling_interval(struct mlx5_core_dev *dev,
434412
fc_stats->sampling_interval);
435413
}
436414

437-
/* Flow counter bluks */
438-
439-
struct mlx5_fc_bulk {
440-
struct mlx5_fs_bulk fs_bulk;
441-
u32 base_id;
442-
struct mlx5_fc fcs[];
443-
};
415+
/* Flow counter bulks */
444416

445417
static void mlx5_fc_init(struct mlx5_fc *counter, struct mlx5_fc_bulk *bulk,
446418
u32 id)
@@ -449,6 +421,11 @@ static void mlx5_fc_init(struct mlx5_fc *counter, struct mlx5_fc_bulk *bulk,
449421
counter->id = id;
450422
}
451423

424+
u32 mlx5_fc_get_base_id(struct mlx5_fc *counter)
425+
{
426+
return counter->bulk->base_id;
427+
}
428+
452429
static struct mlx5_fs_bulk *mlx5_fc_bulk_create(struct mlx5_core_dev *dev,
453430
void *pool_ctx)
454431
{
@@ -474,6 +451,8 @@ static struct mlx5_fs_bulk *mlx5_fc_bulk_create(struct mlx5_core_dev *dev,
474451
for (i = 0; i < bulk_len; i++)
475452
mlx5_fc_init(&fc_bulk->fcs[i], fc_bulk, base_id + i);
476453

454+
refcount_set(&fc_bulk->hws_data.hws_action_refcount, 0);
455+
mutex_init(&fc_bulk->hws_data.lock);
477456
return &fc_bulk->fs_bulk;
478457

479458
fs_bulk_cleanup:

drivers/net/ethernet/mellanox/mlx5/core/steering/hws/fs_hws_pools.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,3 +401,50 @@ bool mlx5_fs_hws_mh_pool_match(struct mlx5_fs_pool *mh_pool,
401401
}
402402
return true;
403403
}
404+
405+
struct mlx5hws_action *mlx5_fc_get_hws_action(struct mlx5hws_context *ctx,
406+
struct mlx5_fc *counter)
407+
{
408+
u32 flags = MLX5HWS_ACTION_FLAG_HWS_FDB | MLX5HWS_ACTION_FLAG_SHARED;
409+
struct mlx5_fc_bulk *fc_bulk = counter->bulk;
410+
struct mlx5_fc_bulk_hws_data *fc_bulk_hws;
411+
412+
fc_bulk_hws = &fc_bulk->hws_data;
413+
/* try avoid locking if not necessary */
414+
if (refcount_inc_not_zero(&fc_bulk_hws->hws_action_refcount))
415+
return fc_bulk_hws->hws_action;
416+
417+
mutex_lock(&fc_bulk_hws->lock);
418+
if (refcount_inc_not_zero(&fc_bulk_hws->hws_action_refcount)) {
419+
mutex_unlock(&fc_bulk_hws->lock);
420+
return fc_bulk_hws->hws_action;
421+
}
422+
fc_bulk_hws->hws_action =
423+
mlx5hws_action_create_counter(ctx, fc_bulk->base_id, flags);
424+
if (!fc_bulk_hws->hws_action) {
425+
mutex_unlock(&fc_bulk_hws->lock);
426+
return NULL;
427+
}
428+
refcount_set(&fc_bulk_hws->hws_action_refcount, 1);
429+
mutex_unlock(&fc_bulk_hws->lock);
430+
431+
return fc_bulk_hws->hws_action;
432+
}
433+
434+
void mlx5_fc_put_hws_action(struct mlx5_fc *counter)
435+
{
436+
struct mlx5_fc_bulk_hws_data *fc_bulk_hws = &counter->bulk->hws_data;
437+
438+
/* try avoid locking if not necessary */
439+
if (refcount_dec_not_one(&fc_bulk_hws->hws_action_refcount))
440+
return;
441+
442+
mutex_lock(&fc_bulk_hws->lock);
443+
if (!refcount_dec_and_test(&fc_bulk_hws->hws_action_refcount)) {
444+
mutex_unlock(&fc_bulk_hws->lock);
445+
return;
446+
}
447+
mlx5hws_action_destroy(fc_bulk_hws->hws_action);
448+
fc_bulk_hws->hws_action = NULL;
449+
mutex_unlock(&fc_bulk_hws->lock);
450+
}

drivers/net/ethernet/mellanox/mlx5/core/steering/hws/fs_hws_pools.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,7 @@ void mlx5_fs_hws_mh_pool_release_mh(struct mlx5_fs_pool *mh_pool,
6767
struct mlx5_fs_hws_mh *mh_data);
6868
bool mlx5_fs_hws_mh_pool_match(struct mlx5_fs_pool *mh_pool,
6969
struct mlx5hws_action_mh_pattern *pattern);
70+
struct mlx5hws_action *mlx5_fc_get_hws_action(struct mlx5hws_context *ctx,
71+
struct mlx5_fc *counter);
72+
void mlx5_fc_put_hws_action(struct mlx5_fc *counter);
7073
#endif /* __MLX5_FS_HWS_POOLS_H__ */

0 commit comments

Comments
 (0)