RDMA/core: Create and destroy rdma_counter using rdma_zalloc_drv_obj()
authorPatrisious Haddad <phaddad@nvidia.com>
Thu, 13 Mar 2025 14:18:42 +0000 (16:18 +0200)
committerLeon Romanovsky <leon@kernel.org>
Tue, 18 Mar 2025 10:18:37 +0000 (06:18 -0400)
Change rdma_counter allocation to use rdma_zalloc_drv_obj() instead of,
explicitly allocating at core, in order to be contained inside driver
specific structures.

Adjust all drivers that use it to have their containing structure, and
add driver specific initialization operation.

This change is needed to allow upcoming patches to implement
optional-counters binding whereas inside each driver specific counter
struct his bound optional-counters will be maintained.

Signed-off-by: Patrisious Haddad <phaddad@nvidia.com>
Reviewed-by: Mark Bloch <mbloch@nvidia.com>
Link: https://patch.msgid.link/a5a484f421fc2e5595158e61a354fba43272b02d.1741875070.git.leon@kernel.org
Signed-off-by: Leon Romanovsky <leon@kernel.org>
drivers/infiniband/core/counters.c
drivers/infiniband/core/device.c
drivers/infiniband/hw/mlx5/counters.c
drivers/infiniband/hw/mlx5/counters.h
include/rdma/ib_verbs.h

index af59486fe41852533fe2463fb161884ef303d2d1..981d5a28614a21a1694dd07bf2c06c51c40a4476 100644 (file)
@@ -149,13 +149,15 @@ static struct rdma_counter *alloc_and_bind(struct ib_device *dev, u32 port,
        if (!dev->ops.counter_dealloc || !dev->ops.counter_alloc_stats)
                return NULL;
 
-       counter = kzalloc(sizeof(*counter), GFP_KERNEL);
+       counter = rdma_zalloc_drv_obj(dev, rdma_counter);
        if (!counter)
                return NULL;
 
        counter->device    = dev;
        counter->port      = port;
 
+       dev->ops.counter_init(counter);
+
        rdma_restrack_new(&counter->res, RDMA_RESTRACK_COUNTER);
        counter->stats = dev->ops.counter_alloc_stats(counter);
        if (!counter->stats)
index ee75b99f84bcc233710a4c806146e4f5c18e65ff..b4e3e4beb7f455683e62a57b9bf10adce23415e9 100644 (file)
@@ -2683,6 +2683,7 @@ void ib_set_device_ops(struct ib_device *dev, const struct ib_device_ops *ops)
        SET_DEVICE_OP(dev_ops, counter_alloc_stats);
        SET_DEVICE_OP(dev_ops, counter_bind_qp);
        SET_DEVICE_OP(dev_ops, counter_dealloc);
+       SET_DEVICE_OP(dev_ops, counter_init);
        SET_DEVICE_OP(dev_ops, counter_unbind_qp);
        SET_DEVICE_OP(dev_ops, counter_update_stats);
        SET_DEVICE_OP(dev_ops, create_ah);
@@ -2797,6 +2798,7 @@ void ib_set_device_ops(struct ib_device *dev, const struct ib_device_ops *ops)
        SET_OBJ_SIZE(dev_ops, ib_srq);
        SET_OBJ_SIZE(dev_ops, ib_ucontext);
        SET_OBJ_SIZE(dev_ops, ib_xrcd);
+       SET_OBJ_SIZE(dev_ops, rdma_counter);
 }
 EXPORT_SYMBOL(ib_set_device_ops);
 
index e2a54c1dbc7a47aa9470d4645918dde8175f9ee5..018bb96bdbf480e4b6bf37480959b095c06493e9 100644 (file)
@@ -1105,6 +1105,8 @@ out:
        return 0;
 }
 
+static void mlx5_ib_counter_init(struct rdma_counter *counter) {}
+
 static const struct ib_device_ops hw_stats_ops = {
        .alloc_hw_port_stats = mlx5_ib_alloc_hw_port_stats,
        .get_hw_stats = mlx5_ib_get_hw_stats,
@@ -1115,6 +1117,9 @@ static const struct ib_device_ops hw_stats_ops = {
        .counter_update_stats = mlx5_ib_counter_update_stats,
        .modify_hw_stat = IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS) ?
                          mlx5_ib_modify_stat : NULL,
+       .counter_init = mlx5_ib_counter_init,
+
+       INIT_RDMA_OBJ_SIZE(rdma_counter, mlx5_rdma_counter, rdma_counter),
 };
 
 static const struct ib_device_ops hw_switchdev_vport_op = {
@@ -1129,6 +1134,9 @@ static const struct ib_device_ops hw_switchdev_stats_ops = {
        .counter_dealloc = mlx5_ib_counter_dealloc,
        .counter_alloc_stats = mlx5_ib_counter_alloc_stats,
        .counter_update_stats = mlx5_ib_counter_update_stats,
+       .counter_init = mlx5_ib_counter_init,
+
+       INIT_RDMA_OBJ_SIZE(rdma_counter, mlx5_rdma_counter, rdma_counter),
 };
 
 static const struct ib_device_ops counters_ops = {
index 6bcaaa52e2b2429e1144525814336751a9d7eeff..f153901a43bedbaf6a24963565d73eadf0feef7d 100644 (file)
@@ -8,6 +8,17 @@
 
 #include "mlx5_ib.h"
 
+
+struct mlx5_rdma_counter {
+       struct rdma_counter rdma_counter;
+};
+
+static inline struct mlx5_rdma_counter *
+to_mcounter(struct rdma_counter *counter)
+{
+       return container_of(counter, struct mlx5_rdma_counter, rdma_counter);
+}
+
 int mlx5_ib_counters_init(struct mlx5_ib_dev *dev);
 void mlx5_ib_counters_cleanup(struct mlx5_ib_dev *dev);
 void mlx5_ib_counters_clear_description(struct ib_counters *counters);
index 9941f4185c795287003241c2ad0bf9ed3aae5027..90e93297d59eba7b236aa1de67099cd3d3e8e04a 100644 (file)
@@ -2665,6 +2665,11 @@ struct ib_device_ops {
         */
        int (*counter_update_stats)(struct rdma_counter *counter);
 
+       /**
+        * counter_init - Initialize the driver specific rdma counter struct.
+        */
+       void (*counter_init)(struct rdma_counter *counter);
+
        /**
         * Allows rdma drivers to add their own restrack attributes
         * dumped via 'rdma stat' iproute2 command.
@@ -2716,6 +2721,7 @@ struct ib_device_ops {
        DECLARE_RDMA_OBJ_SIZE(ib_srq);
        DECLARE_RDMA_OBJ_SIZE(ib_ucontext);
        DECLARE_RDMA_OBJ_SIZE(ib_xrcd);
+       DECLARE_RDMA_OBJ_SIZE(rdma_counter);
 };
 
 struct ib_core_device {