Skip to content

Commit fb50d3a

Browse files
paravmellanoxSomasundaram Krishnasamy
authored andcommitted
IB/core: Implement clear counters
This patch adds sysfs clear counter interface to clear the counters. Reviewers-notes: Please note that there is a possible race between clearing and updating the counter values on architectures that do not write 64-bit atomically. Orabug: 26761264 MlnxSF: 00384047 Upstream_status: ignore Signed-off-by: Parav Pandit <[email protected]> Signed-off-by: Aron Silverton <[email protected]> Reviewed-by: Håkon Bugge <[email protected]> Signed-off-by: Somasundaram Krishnasamy <[email protected]>
1 parent 6196551 commit fb50d3a

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

drivers/infiniband/core/sysfs.c

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,70 @@ static struct attribute *alloc_hsa_lifespan(char *name, u8 port_num)
909909
return &hsa->attr;
910910
}
911911

912+
static void clear_cached_hw_stats(struct rdma_hw_stats *stats)
913+
{
914+
int i;
915+
916+
for (i = 0; i < stats->num_counters; i++)
917+
stats->value[i] = 0;
918+
}
919+
920+
static ssize_t set_clear_hw_stats(struct kobject *kobj,
921+
struct attribute *attr,
922+
const char *buf, size_t count)
923+
{
924+
struct hw_stats_attribute *hsa;
925+
struct rdma_hw_stats *stats;
926+
struct ib_device *dev;
927+
struct ib_port *port;
928+
int ret;
929+
930+
hsa = container_of(attr, struct hw_stats_attribute, attr);
931+
if (!hsa->port_num) {
932+
dev = container_of((struct device *)kobj,
933+
struct ib_device, dev);
934+
stats = dev->hw_stats;
935+
} else {
936+
port = container_of(kobj, struct ib_port, kobj);
937+
dev = port->ibdev;
938+
stats = port->hw_stats;
939+
}
940+
if (dev->clear_hw_stats) {
941+
ret = dev->clear_hw_stats(dev, hsa->port_num);
942+
clear_cached_hw_stats(stats);
943+
} else {
944+
return -EINVAL;
945+
}
946+
947+
if (ret)
948+
return ret;
949+
return count;
950+
}
951+
952+
static ssize_t show_clear_hw_stats(struct kobject *kobj, struct attribute *attr,
953+
char *buf)
954+
{
955+
return sprintf(buf, "%d\n", 0);
956+
}
957+
958+
static struct attribute *alloc_hsa_clear_stats(char *name, u8 port_num)
959+
{
960+
struct hw_stats_attribute *hsa;
961+
962+
hsa = kmalloc(sizeof(*hsa), GFP_KERNEL);
963+
if (!hsa)
964+
return NULL;
965+
966+
hsa->attr.name = name;
967+
hsa->attr.mode = S_IWUSR | S_IRUSR;
968+
hsa->store = set_clear_hw_stats;
969+
hsa->show = show_clear_hw_stats;
970+
hsa->index = 0;
971+
hsa->port_num = port_num;
972+
973+
return &hsa->attr;
974+
}
975+
912976
static void setup_hw_stats(struct ib_device *device, struct ib_port *port,
913977
u8 port_num)
914978
{
@@ -929,7 +993,7 @@ static void setup_hw_stats(struct ib_device *device, struct ib_port *port,
929993
* one to NULL terminate the list for the sysfs core code
930994
*/
931995
hsag = kzalloc(sizeof(*hsag) +
932-
sizeof(void *) * (stats->num_counters + 2),
996+
sizeof(void *) * (stats->num_counters + 3),
933997
GFP_KERNEL);
934998
if (!hsag)
935999
goto err_free_stats;
@@ -956,6 +1020,11 @@ static void setup_hw_stats(struct ib_device *device, struct ib_port *port,
9561020
if (hsag->attrs[i])
9571021
sysfs_attr_init(hsag->attrs[i]);
9581022

1023+
i++;
1024+
hsag->attrs[i] = alloc_hsa_clear_stats("clear_counters", port_num);
1025+
if (hsag->attrs[i])
1026+
sysfs_attr_init(hsag->attrs[i]);
1027+
9591028
if (port) {
9601029
struct kobject *kobj = &port->kobj;
9611030
ret = sysfs_create_group(kobj, hsag);

include/rdma/ib_verbs.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2097,6 +2097,13 @@ struct ib_device {
20972097
int (*get_hw_stats)(struct ib_device *device,
20982098
struct rdma_hw_stats *stats,
20992099
u8 port, int index);
2100+
/**
2101+
* clear_hw_stats - Clear the hw stats
2102+
* @ib_device - device whose counters to clear.
2103+
* @port - port whose counters to clear.
2104+
*/
2105+
int (*clear_hw_stats)(struct ib_device *device,
2106+
u8 port);
21002107
int (*query_device)(struct ib_device *device,
21012108
struct ib_device_attr *device_attr,
21022109
struct ib_udata *udata);

0 commit comments

Comments
 (0)