drivers/net/ethernet/mellanox/mlx5/core/fs_counters.c
Source file repositories/reference/linux-study-clean/drivers/net/ethernet/mellanox/mlx5/core/fs_counters.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/net/ethernet/mellanox/mlx5/core/fs_counters.c- Extension
.c- Size
- 16961 bytes
- Lines
- 619
- Domain
- Driver Families
- Bucket
- drivers/net
- Inferred role
- Driver Families: exported/initcall integration point
- Status
- integration implementation candidate
Why This File Exists
Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/mlx5/driver.hlinux/mlx5/fs.hmlx5_core.hfs_core.hfs_pool.hfs_cmd.h
Detected Declarations
struct mlx5_fc_statsfunction get_init_bulk_query_lenfunction get_max_bulk_query_lenfunction update_counter_cachefunction mlx5_fc_stats_query_all_countersfunction mlx5_fc_freefunction mlx5_fc_releasefunction mlx5_fc_stats_bulk_query_buf_reallocfunction mlx5_fc_num_countersfunction mlx5_fc_stats_workfunction mlx5_fc_bulk_initfunction mlx5_fc_idfunction mlx5_fc_destroyfunction mlx5_init_fc_statsfunction mlx5_cleanup_fc_statsfunction xa_for_eachfunction mlx5_fc_queryfunction mlx5_fc_query_lastusefunction mlx5_fc_query_cachedfunction mlx5_fc_query_cached_rawfunction mlx5_fc_queue_stats_workfunction mlx5_fc_update_sampling_intervalfunction mlx5_fc_initfunction mlx5_fc_get_base_idfunction mlx5_fc_bulk_destroyfunction mlx5_fc_pool_update_thresholdfunction mlx5_fc_pool_initfunction mlx5_fc_pool_cleanupfunction mlx5_fc_pool_acquire_counterfunction mlx5_fc_pool_release_counterfunction mlx5_fc_local_createfunction mlx5_fc_local_destroyfunction mlx5_fc_local_getfunction mlx5_fc_local_putexport mlx5_fc_createexport mlx5_fc_idexport mlx5_fc_destroyexport mlx5_fc_queryexport mlx5_fc_local_createexport mlx5_fc_local_destroy
Annotated Snippet
struct mlx5_fc_stats {
struct xarray counters;
struct workqueue_struct *wq;
struct delayed_work work;
unsigned long sampling_interval; /* jiffies */
u32 *bulk_query_out;
int bulk_query_len;
bool bulk_query_alloc_failed;
unsigned long next_bulk_query_alloc;
struct mlx5_fs_pool fc_pool;
};
static void mlx5_fc_pool_init(struct mlx5_fs_pool *fc_pool, struct mlx5_core_dev *dev);
static void mlx5_fc_pool_cleanup(struct mlx5_fs_pool *fc_pool);
static struct mlx5_fc *mlx5_fc_pool_acquire_counter(struct mlx5_fs_pool *fc_pool);
static void mlx5_fc_pool_release_counter(struct mlx5_fs_pool *fc_pool, struct mlx5_fc *fc);
static int get_init_bulk_query_len(struct mlx5_core_dev *dev)
{
return min_t(int, MLX5_INIT_COUNTERS_BULK,
(1 << MLX5_CAP_GEN(dev, log_max_flow_counter_bulk)));
}
static int get_max_bulk_query_len(struct mlx5_core_dev *dev)
{
return min_t(int, MLX5_SW_MAX_COUNTERS_BULK,
(1 << MLX5_CAP_GEN(dev, log_max_flow_counter_bulk)));
}
static void update_counter_cache(int index, u32 *bulk_raw_data,
struct mlx5_fc_cache *cache)
{
void *stats = MLX5_ADDR_OF(query_flow_counter_out, bulk_raw_data,
flow_statistics[index]);
u64 packets = MLX5_GET64(traffic_counter, stats, packets);
u64 bytes = MLX5_GET64(traffic_counter, stats, octets);
if (cache->packets == packets)
return;
cache->packets = packets;
cache->bytes = bytes;
cache->lastuse = jiffies;
}
/* Synchronization notes
*
* Access to counter array:
* - create - mlx5_fc_create() (user context)
* - inserts the counter into the xarray.
*
* - destroy - mlx5_fc_destroy() (user context)
* - erases the counter from the xarray and releases it.
*
* - query mlx5_fc_query(), mlx5_fc_query_cached{,_raw}() (user context)
* - user should not access a counter after destroy.
*
* - bulk query (single thread workqueue context)
* - create: query relies on 'lastuse' to avoid updating counters added
* around the same time as the current bulk cmd.
* - destroy: destroyed counters will not be accessed, even if they are
* destroyed during a bulk query command.
*/
static void mlx5_fc_stats_query_all_counters(struct mlx5_core_dev *dev)
{
struct mlx5_fc_stats *fc_stats = dev->priv.fc_stats;
u32 bulk_len = fc_stats->bulk_query_len;
XA_STATE(xas, &fc_stats->counters, 0);
u32 *data = fc_stats->bulk_query_out;
struct mlx5_fc *counter;
u32 last_bulk_id = 0;
u64 bulk_query_time;
u32 bulk_base_id;
int err;
xas_lock(&xas);
xas_for_each(&xas, counter, U32_MAX) {
if (xas_retry(&xas, counter))
continue;
if (unlikely(counter->id >= last_bulk_id)) {
/* Start new bulk query. */
/* First id must be aligned to 4 when using bulk query. */
bulk_base_id = counter->id & ~0x3;
last_bulk_id = bulk_base_id + bulk_len;
/* The lock is released while querying the hw and reacquired after. */
xas_unlock(&xas);
/* The same id needs to be processed again in the next loop iteration. */
xas_reset(&xas);
bulk_query_time = jiffies;
Annotation
- Immediate include surface: `linux/mlx5/driver.h`, `linux/mlx5/fs.h`, `mlx5_core.h`, `fs_core.h`, `fs_pool.h`, `fs_cmd.h`.
- Detected declarations: `struct mlx5_fc_stats`, `function get_init_bulk_query_len`, `function get_max_bulk_query_len`, `function update_counter_cache`, `function mlx5_fc_stats_query_all_counters`, `function mlx5_fc_free`, `function mlx5_fc_release`, `function mlx5_fc_stats_bulk_query_buf_realloc`, `function mlx5_fc_num_counters`, `function mlx5_fc_stats_work`.
- Atlas domain: Driver Families / drivers/net.
- Implementation status: integration implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.