Skip to content

Commit 0a0800c

Browse files
MarkZhang81jgunthorpe
authored andcommitted
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/[email protected] Signed-off-by: Leon Romanovsky <[email protected]> Signed-off-by: Mark Zhang <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]>
1 parent 13f30b0 commit 0a0800c

File tree

4 files changed

+47
-31
lines changed

4 files changed

+47
-31
lines changed

drivers/infiniband/core/counters.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ static struct rdma_counter *alloc_and_bind(struct ib_device *dev, u32 port,
165165
return counter;
166166

167167
err_mode:
168-
kfree(counter->stats);
168+
rdma_free_hw_stats_struct(counter->stats);
169169
err_stats:
170170
rdma_restrack_put(&counter->res);
171171
kfree(counter);
@@ -186,7 +186,7 @@ static void rdma_counter_free(struct rdma_counter *counter)
186186
mutex_unlock(&port_counter->lock);
187187

188188
rdma_restrack_del(&counter->res);
189-
kfree(counter->stats);
189+
rdma_free_hw_stats_struct(counter->stats);
190190
kfree(counter);
191191
}
192192

@@ -618,7 +618,7 @@ void rdma_counter_init(struct ib_device *dev)
618618
fail:
619619
for (i = port; i >= rdma_start_port(dev); i--) {
620620
port_counter = &dev->port_data[port].port_counter;
621-
kfree(port_counter->hstats);
621+
rdma_free_hw_stats_struct(port_counter->hstats);
622622
port_counter->hstats = NULL;
623623
mutex_destroy(&port_counter->lock);
624624
}
@@ -631,7 +631,7 @@ void rdma_counter_release(struct ib_device *dev)
631631

632632
rdma_for_each_port(dev, port) {
633633
port_counter = &dev->port_data[port].port_counter;
634-
kfree(port_counter->hstats);
634+
rdma_free_hw_stats_struct(port_counter->hstats);
635635
mutex_destroy(&port_counter->lock);
636636
}
637637
}

drivers/infiniband/core/sysfs.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ static void ib_port_release(struct kobject *kobj)
755755
for (i = 0; i != ARRAY_SIZE(port->groups); i++)
756756
kfree(port->groups[i].attrs);
757757
if (port->hw_stats_data)
758-
kfree(port->hw_stats_data->stats);
758+
rdma_free_hw_stats_struct(port->hw_stats_data->stats);
759759
kfree(port->hw_stats_data);
760760
kfree(port);
761761
}
@@ -919,14 +919,14 @@ alloc_hw_stats_device(struct ib_device *ibdev)
919919
err_free_data:
920920
kfree(data);
921921
err_free_stats:
922-
kfree(stats);
922+
rdma_free_hw_stats_struct(stats);
923923
return ERR_PTR(-ENOMEM);
924924
}
925925

926926
void ib_device_release_hw_stats(struct hw_stats_device_data *data)
927927
{
928928
kfree(data->group.attrs);
929-
kfree(data->stats);
929+
rdma_free_hw_stats_struct(data->stats);
930930
kfree(data);
931931
}
932932

@@ -1018,7 +1018,7 @@ alloc_hw_stats_port(struct ib_port *port, struct attribute_group *group)
10181018
err_free_data:
10191019
kfree(data);
10201020
err_free_stats:
1021-
kfree(stats);
1021+
rdma_free_hw_stats_struct(stats);
10221022
return ERR_PTR(-ENOMEM);
10231023
}
10241024

drivers/infiniband/core/verbs.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2976,3 +2976,38 @@ bool __rdma_block_iter_next(struct ib_block_iter *biter)
29762976
return true;
29772977
}
29782978
EXPORT_SYMBOL(__rdma_block_iter_next);
2979+
2980+
/**
2981+
* rdma_alloc_hw_stats_struct - Helper function to allocate dynamic struct
2982+
* for the drivers.
2983+
* @descs: array of static descriptors
2984+
* @num_counters: number of elements in array
2985+
* @lifespan: milliseconds between updates
2986+
*/
2987+
struct rdma_hw_stats *rdma_alloc_hw_stats_struct(
2988+
const struct rdma_stat_desc *descs, int num_counters,
2989+
unsigned long lifespan)
2990+
{
2991+
struct rdma_hw_stats *stats;
2992+
2993+
stats = kzalloc(struct_size(stats, value, num_counters), GFP_KERNEL);
2994+
if (!stats)
2995+
return NULL;
2996+
2997+
stats->descs = descs;
2998+
stats->num_counters = num_counters;
2999+
stats->lifespan = msecs_to_jiffies(lifespan);
3000+
3001+
return stats;
3002+
}
3003+
EXPORT_SYMBOL(rdma_alloc_hw_stats_struct);
3004+
3005+
/**
3006+
* rdma_free_hw_stats_struct - Helper function to release rdma_hw_stats
3007+
* @stats: statistics to release
3008+
*/
3009+
void rdma_free_hw_stats_struct(struct rdma_hw_stats *stats)
3010+
{
3011+
kfree(stats);
3012+
}
3013+
EXPORT_SYMBOL(rdma_free_hw_stats_struct);

include/rdma/ib_verbs.h

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -582,31 +582,12 @@ struct rdma_hw_stats {
582582
};
583583

584584
#define RDMA_HW_STATS_DEFAULT_LIFESPAN 10
585-
/**
586-
* rdma_alloc_hw_stats_struct - Helper function to allocate dynamic struct
587-
* for drivers.
588-
* @descs - Array of static descriptors
589-
* @num_counters - How many elements in array
590-
* @lifespan - How many milliseconds between updates
591-
*/
592-
static inline struct rdma_hw_stats *rdma_alloc_hw_stats_struct(
593-
const struct rdma_stat_desc *descs, int num_counters,
594-
unsigned long lifespan)
595-
{
596-
struct rdma_hw_stats *stats;
597585

598-
stats = kzalloc(sizeof(*stats) + num_counters * sizeof(u64),
599-
GFP_KERNEL);
600-
if (!stats)
601-
return NULL;
602-
603-
stats->descs = descs;
604-
stats->num_counters = num_counters;
605-
stats->lifespan = msecs_to_jiffies(lifespan);
606-
607-
return stats;
608-
}
586+
struct rdma_hw_stats *rdma_alloc_hw_stats_struct(
587+
const struct rdma_stat_desc *descs, int num_counters,
588+
unsigned long lifespan);
609589

590+
void rdma_free_hw_stats_struct(struct rdma_hw_stats *stats);
610591

611592
/* Define bits for the various functionality this port needs to be supported by
612593
* the core.

0 commit comments

Comments
 (0)