Commit e945130b authored by Mark Bloch's avatar Mark Bloch Committed by Jason Gunthorpe

IB/core: Protect against concurrent access to hardware stats

Currently access to hardware stats buffer isn't protected, this can
result in multiple writes and reads at the same time to the same
memory location. This can lead to providing an incorrect value to
the user. Add a mutex to protect against it.

Fixes: b40f4757 ("IB/core: Make device counter infrastructure dynamic")
Signed-off-by: default avatarMark Bloch <markb@mellanox.com>
Signed-off-by: default avatarLeon Romanovsky <leonro@mellanox.com>
Signed-off-by: default avatarJason Gunthorpe <jgg@mellanox.com>
parent c8d75a98
...@@ -811,10 +811,15 @@ static ssize_t show_hw_stats(struct kobject *kobj, struct attribute *attr, ...@@ -811,10 +811,15 @@ static ssize_t show_hw_stats(struct kobject *kobj, struct attribute *attr,
dev = port->ibdev; dev = port->ibdev;
stats = port->hw_stats; stats = port->hw_stats;
} }
mutex_lock(&stats->lock);
ret = update_hw_stats(dev, stats, hsa->port_num, hsa->index); ret = update_hw_stats(dev, stats, hsa->port_num, hsa->index);
if (ret) if (ret)
return ret; goto unlock;
return print_hw_stat(stats, hsa->index, buf); ret = print_hw_stat(stats, hsa->index, buf);
unlock:
mutex_unlock(&stats->lock);
return ret;
} }
static ssize_t show_stats_lifespan(struct kobject *kobj, static ssize_t show_stats_lifespan(struct kobject *kobj,
...@@ -822,17 +827,25 @@ static ssize_t show_stats_lifespan(struct kobject *kobj, ...@@ -822,17 +827,25 @@ static ssize_t show_stats_lifespan(struct kobject *kobj,
char *buf) char *buf)
{ {
struct hw_stats_attribute *hsa; struct hw_stats_attribute *hsa;
struct rdma_hw_stats *stats;
int msecs; int msecs;
hsa = container_of(attr, struct hw_stats_attribute, attr); hsa = container_of(attr, struct hw_stats_attribute, attr);
if (!hsa->port_num) { if (!hsa->port_num) {
struct ib_device *dev = container_of((struct device *)kobj, struct ib_device *dev = container_of((struct device *)kobj,
struct ib_device, dev); struct ib_device, dev);
msecs = jiffies_to_msecs(dev->hw_stats->lifespan);
stats = dev->hw_stats;
} else { } else {
struct ib_port *p = container_of(kobj, struct ib_port, kobj); struct ib_port *p = container_of(kobj, struct ib_port, kobj);
msecs = jiffies_to_msecs(p->hw_stats->lifespan);
stats = p->hw_stats;
} }
mutex_lock(&stats->lock);
msecs = jiffies_to_msecs(stats->lifespan);
mutex_unlock(&stats->lock);
return sprintf(buf, "%d\n", msecs); return sprintf(buf, "%d\n", msecs);
} }
...@@ -841,6 +854,7 @@ static ssize_t set_stats_lifespan(struct kobject *kobj, ...@@ -841,6 +854,7 @@ static ssize_t set_stats_lifespan(struct kobject *kobj,
const char *buf, size_t count) const char *buf, size_t count)
{ {
struct hw_stats_attribute *hsa; struct hw_stats_attribute *hsa;
struct rdma_hw_stats *stats;
int msecs; int msecs;
int jiffies; int jiffies;
int ret; int ret;
...@@ -855,11 +869,18 @@ static ssize_t set_stats_lifespan(struct kobject *kobj, ...@@ -855,11 +869,18 @@ static ssize_t set_stats_lifespan(struct kobject *kobj,
if (!hsa->port_num) { if (!hsa->port_num) {
struct ib_device *dev = container_of((struct device *)kobj, struct ib_device *dev = container_of((struct device *)kobj,
struct ib_device, dev); struct ib_device, dev);
dev->hw_stats->lifespan = jiffies;
stats = dev->hw_stats;
} else { } else {
struct ib_port *p = container_of(kobj, struct ib_port, kobj); struct ib_port *p = container_of(kobj, struct ib_port, kobj);
p->hw_stats->lifespan = jiffies;
stats = p->hw_stats;
} }
mutex_lock(&stats->lock);
stats->lifespan = jiffies;
mutex_unlock(&stats->lock);
return count; return count;
} }
...@@ -952,6 +973,7 @@ static void setup_hw_stats(struct ib_device *device, struct ib_port *port, ...@@ -952,6 +973,7 @@ static void setup_hw_stats(struct ib_device *device, struct ib_port *port,
sysfs_attr_init(hsag->attrs[i]); sysfs_attr_init(hsag->attrs[i]);
} }
mutex_init(&stats->lock);
/* treat an error here as non-fatal */ /* treat an error here as non-fatal */
hsag->attrs[i] = alloc_hsa_lifespan("lifespan", port_num); hsag->attrs[i] = alloc_hsa_lifespan("lifespan", port_num);
if (hsag->attrs[i]) if (hsag->attrs[i])
......
...@@ -470,6 +470,9 @@ enum ib_port_speed { ...@@ -470,6 +470,9 @@ enum ib_port_speed {
/** /**
* struct rdma_hw_stats * struct rdma_hw_stats
* @lock - Mutex to protect parallel write access to lifespan and values
* of counters, which are 64bits and not guaranteeed to be written
* atomicaly on 32bits systems.
* @timestamp - Used by the core code to track when the last update was * @timestamp - Used by the core code to track when the last update was
* @lifespan - Used by the core code to determine how old the counters * @lifespan - Used by the core code to determine how old the counters
* should be before being updated again. Stored in jiffies, defaults * should be before being updated again. Stored in jiffies, defaults
...@@ -485,6 +488,7 @@ enum ib_port_speed { ...@@ -485,6 +488,7 @@ enum ib_port_speed {
* filled in by the drivers get_stats routine * filled in by the drivers get_stats routine
*/ */
struct rdma_hw_stats { struct rdma_hw_stats {
struct mutex lock; /* Protect lifespan and values[] */
unsigned long timestamp; unsigned long timestamp;
unsigned long lifespan; unsigned long lifespan;
const char * const *names; const char * const *names;
......
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