Commit 0a0800ce authored by Mark Zhang's avatar Mark Zhang Committed by Jason Gunthorpe

RDMA/core: Add a helper API rdma_free_hw_stats_struct

Add a new API rdma_free_hw_stats_struct to pair with
rdma_alloc_hw_stats_struct (which is also de-inlined).

This will be useful when there are more alloc/free works in following
patches.

Link: https://lore.kernel.org/r/20211008122439.166063-5-markzhang@nvidia.comSigned-off-by: default avatarLeon Romanovsky <leonro@nvidia.com>
Signed-off-by: default avatarMark Zhang <markzhang@nvidia.com>
Signed-off-by: default avatarJason Gunthorpe <jgg@nvidia.com>
parent 13f30b0f
...@@ -165,7 +165,7 @@ static struct rdma_counter *alloc_and_bind(struct ib_device *dev, u32 port, ...@@ -165,7 +165,7 @@ static struct rdma_counter *alloc_and_bind(struct ib_device *dev, u32 port,
return counter; return counter;
err_mode: err_mode:
kfree(counter->stats); rdma_free_hw_stats_struct(counter->stats);
err_stats: err_stats:
rdma_restrack_put(&counter->res); rdma_restrack_put(&counter->res);
kfree(counter); kfree(counter);
...@@ -186,7 +186,7 @@ static void rdma_counter_free(struct rdma_counter *counter) ...@@ -186,7 +186,7 @@ static void rdma_counter_free(struct rdma_counter *counter)
mutex_unlock(&port_counter->lock); mutex_unlock(&port_counter->lock);
rdma_restrack_del(&counter->res); rdma_restrack_del(&counter->res);
kfree(counter->stats); rdma_free_hw_stats_struct(counter->stats);
kfree(counter); kfree(counter);
} }
...@@ -618,7 +618,7 @@ void rdma_counter_init(struct ib_device *dev) ...@@ -618,7 +618,7 @@ void rdma_counter_init(struct ib_device *dev)
fail: fail:
for (i = port; i >= rdma_start_port(dev); i--) { for (i = port; i >= rdma_start_port(dev); i--) {
port_counter = &dev->port_data[port].port_counter; port_counter = &dev->port_data[port].port_counter;
kfree(port_counter->hstats); rdma_free_hw_stats_struct(port_counter->hstats);
port_counter->hstats = NULL; port_counter->hstats = NULL;
mutex_destroy(&port_counter->lock); mutex_destroy(&port_counter->lock);
} }
...@@ -631,7 +631,7 @@ void rdma_counter_release(struct ib_device *dev) ...@@ -631,7 +631,7 @@ void rdma_counter_release(struct ib_device *dev)
rdma_for_each_port(dev, port) { rdma_for_each_port(dev, port) {
port_counter = &dev->port_data[port].port_counter; port_counter = &dev->port_data[port].port_counter;
kfree(port_counter->hstats); rdma_free_hw_stats_struct(port_counter->hstats);
mutex_destroy(&port_counter->lock); mutex_destroy(&port_counter->lock);
} }
} }
...@@ -755,7 +755,7 @@ static void ib_port_release(struct kobject *kobj) ...@@ -755,7 +755,7 @@ static void ib_port_release(struct kobject *kobj)
for (i = 0; i != ARRAY_SIZE(port->groups); i++) for (i = 0; i != ARRAY_SIZE(port->groups); i++)
kfree(port->groups[i].attrs); kfree(port->groups[i].attrs);
if (port->hw_stats_data) if (port->hw_stats_data)
kfree(port->hw_stats_data->stats); rdma_free_hw_stats_struct(port->hw_stats_data->stats);
kfree(port->hw_stats_data); kfree(port->hw_stats_data);
kfree(port); kfree(port);
} }
...@@ -919,14 +919,14 @@ alloc_hw_stats_device(struct ib_device *ibdev) ...@@ -919,14 +919,14 @@ alloc_hw_stats_device(struct ib_device *ibdev)
err_free_data: err_free_data:
kfree(data); kfree(data);
err_free_stats: err_free_stats:
kfree(stats); rdma_free_hw_stats_struct(stats);
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
} }
void ib_device_release_hw_stats(struct hw_stats_device_data *data) void ib_device_release_hw_stats(struct hw_stats_device_data *data)
{ {
kfree(data->group.attrs); kfree(data->group.attrs);
kfree(data->stats); rdma_free_hw_stats_struct(data->stats);
kfree(data); kfree(data);
} }
...@@ -1018,7 +1018,7 @@ alloc_hw_stats_port(struct ib_port *port, struct attribute_group *group) ...@@ -1018,7 +1018,7 @@ alloc_hw_stats_port(struct ib_port *port, struct attribute_group *group)
err_free_data: err_free_data:
kfree(data); kfree(data);
err_free_stats: err_free_stats:
kfree(stats); rdma_free_hw_stats_struct(stats);
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
} }
......
...@@ -2976,3 +2976,38 @@ bool __rdma_block_iter_next(struct ib_block_iter *biter) ...@@ -2976,3 +2976,38 @@ bool __rdma_block_iter_next(struct ib_block_iter *biter)
return true; return true;
} }
EXPORT_SYMBOL(__rdma_block_iter_next); EXPORT_SYMBOL(__rdma_block_iter_next);
/**
* rdma_alloc_hw_stats_struct - Helper function to allocate dynamic struct
* for the drivers.
* @descs: array of static descriptors
* @num_counters: number of elements in array
* @lifespan: milliseconds between updates
*/
struct rdma_hw_stats *rdma_alloc_hw_stats_struct(
const struct rdma_stat_desc *descs, int num_counters,
unsigned long lifespan)
{
struct rdma_hw_stats *stats;
stats = kzalloc(struct_size(stats, value, num_counters), GFP_KERNEL);
if (!stats)
return NULL;
stats->descs = descs;
stats->num_counters = num_counters;
stats->lifespan = msecs_to_jiffies(lifespan);
return stats;
}
EXPORT_SYMBOL(rdma_alloc_hw_stats_struct);
/**
* rdma_free_hw_stats_struct - Helper function to release rdma_hw_stats
* @stats: statistics to release
*/
void rdma_free_hw_stats_struct(struct rdma_hw_stats *stats)
{
kfree(stats);
}
EXPORT_SYMBOL(rdma_free_hw_stats_struct);
...@@ -582,31 +582,12 @@ struct rdma_hw_stats { ...@@ -582,31 +582,12 @@ struct rdma_hw_stats {
}; };
#define RDMA_HW_STATS_DEFAULT_LIFESPAN 10 #define RDMA_HW_STATS_DEFAULT_LIFESPAN 10
/**
* rdma_alloc_hw_stats_struct - Helper function to allocate dynamic struct
* for drivers.
* @descs - Array of static descriptors
* @num_counters - How many elements in array
* @lifespan - How many milliseconds between updates
*/
static inline struct rdma_hw_stats *rdma_alloc_hw_stats_struct(
const struct rdma_stat_desc *descs, int num_counters,
unsigned long lifespan)
{
struct rdma_hw_stats *stats;
stats = kzalloc(sizeof(*stats) + num_counters * sizeof(u64), struct rdma_hw_stats *rdma_alloc_hw_stats_struct(
GFP_KERNEL); const struct rdma_stat_desc *descs, int num_counters,
if (!stats) unsigned long lifespan);
return NULL;
stats->descs = descs;
stats->num_counters = num_counters;
stats->lifespan = msecs_to_jiffies(lifespan);
return stats;
}
void rdma_free_hw_stats_struct(struct rdma_hw_stats *stats);
/* Define bits for the various functionality this port needs to be supported by /* Define bits for the various functionality this port needs to be supported by
* the core. * the core.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment