[PATCH net-next 09/15] net/smc: introduce loopback-ism statistics attributes

From: Wen Gu
Date: Thu Jan 11 2024 - 07:04:10 EST


This introduces some statistics attributes of loopback-ism. They can be
read from /sys/devices/virtual/smc/loopback-ism/{xfer_tytes|dmbs_cnt}.

Signed-off-by: Wen Gu <guwen@xxxxxxxxxxxxxxxxx>
---
net/smc/smc_loopback.c | 74 ++++++++++++++++++++++++++++++++++++++++++
net/smc/smc_loopback.h | 22 +++++++++++++
2 files changed, 96 insertions(+)

diff --git a/net/smc/smc_loopback.c b/net/smc/smc_loopback.c
index 3bf7bf5e8c96..a89dbf84aea5 100644
--- a/net/smc/smc_loopback.c
+++ b/net/smc/smc_loopback.c
@@ -30,6 +30,65 @@ static struct class *smc_class;
static int smcd_lo_register_dev(struct smc_lo_dev *ldev);
static void smcd_lo_unregister_dev(struct smc_lo_dev *ldev);

+static void smc_lo_clear_stats(struct smc_lo_dev *ldev)
+{
+ struct smc_lo_dev_stats64 *tmp;
+ int cpu;
+
+ for_each_possible_cpu(cpu) {
+ tmp = per_cpu_ptr(ldev->stats, cpu);
+ tmp->xfer_bytes = 0;
+ }
+}
+
+static void smc_lo_get_stats(struct smc_lo_dev *ldev,
+ struct smc_lo_dev_stats64 *stats)
+{
+ int size, cpu, i;
+ u64 *src, *sum;
+
+ memset(stats, 0, sizeof(*stats));
+ size = sizeof(*stats) / sizeof(u64);
+ for_each_possible_cpu(cpu) {
+ src = (u64 *)per_cpu_ptr(ldev->stats, cpu);
+ sum = (u64 *)stats;
+ for (i = 0; i < size; i++)
+ *(sum++) += *(src++);
+ }
+}
+
+static ssize_t smc_lo_show_stats(struct device *dev,
+ struct device_attribute *attr,
+ char *buf, unsigned long offset)
+{
+ struct smc_lo_dev *ldev =
+ container_of(dev, struct smc_lo_dev, dev);
+ struct smc_lo_dev_stats64 stats;
+ ssize_t ret = -EINVAL;
+
+ if (WARN_ON(offset > sizeof(struct smc_lo_dev_stats64) ||
+ offset % sizeof(u64) != 0))
+ goto out;
+
+ smc_lo_get_stats(ldev, &stats);
+ ret = sysfs_emit(buf, "%llu\n", *(u64 *)(((u8 *)&stats) + offset));
+out:
+ return ret;
+}
+
+/* generate a read-only statistics attribute */
+#define SMC_LO_DEVICE_ATTR_RO(name) \
+static ssize_t name##_show(struct device *dev, \
+ struct device_attribute *attr, char *buf) \
+{ \
+ return smc_lo_show_stats(dev, attr, buf, \
+ offsetof(struct smc_lo_dev_stats64, name)); \
+} \
+static DEVICE_ATTR_RO(name)
+
+SMC_LO_DEVICE_ATTR_RO(xfer_bytes);
+SMC_LO_DEVICE_ATTR_RO(dmbs_cnt);
+
static ssize_t active_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
@@ -67,6 +126,8 @@ static ssize_t active_store(struct device *dev,
static DEVICE_ATTR_RW(active);
static struct attribute *smc_lo_attrs[] = {
&dev_attr_active.attr,
+ &dev_attr_xfer_bytes.attr,
+ &dev_attr_dmbs_cnt.attr,
NULL,
};

@@ -152,6 +213,7 @@ static int smc_lo_register_dmb(struct smcd_dev *smcd, struct smcd_dmb *dmb,
}
hash_add(ldev->dmb_ht, &dmb_node->list, dmb_node->token);
write_unlock(&ldev->dmb_ht_lock);
+ SMC_LO_STAT_DMBS_INC(ldev);

dmb->sba_idx = dmb_node->sba_idx;
dmb->dmb_tok = dmb_node->token;
@@ -191,6 +253,7 @@ static int smc_lo_unregister_dmb(struct smcd_dev *smcd, struct smcd_dmb *dmb)
clear_bit(dmb_node->sba_idx, ldev->sba_idx_mask);
kfree(dmb_node->cpu_addr);
kfree(dmb_node);
+ SMC_LO_STAT_DMBS_DEC(ldev);

return 0;
}
@@ -249,6 +312,8 @@ static int smc_lo_move_data(struct smcd_dev *smcd, u64 dmb_tok,

if (conn && !conn->killed)
smcd_cdc_rx_handler(conn);
+ } else {
+ SMC_LO_STAT_XFER_BYTES(ldev, size);
}
return 0;
}
@@ -354,6 +419,7 @@ static void smcd_lo_unregister_dev(struct smc_lo_dev *ldev)
mutex_unlock(&smcd_dev_list.mutex);
kfree(smcd->conn);
kfree(smcd);
+ smc_lo_clear_stats(ldev);
}

static int smc_lo_dev_init(struct smc_lo_dev *ldev)
@@ -374,6 +440,7 @@ static void smc_lo_dev_release(struct device *dev)
struct smc_lo_dev *ldev =
container_of(dev, struct smc_lo_dev, dev);

+ free_percpu(ldev->stats);
kfree(ldev);
}

@@ -392,6 +459,13 @@ static int smc_lo_dev_probe(void)
goto destroy_class;
}

+ ldev->stats = alloc_percpu(struct smc_lo_dev_stats64);
+ if (!ldev->stats) {
+ ret = -ENOMEM;
+ kfree(ldev);
+ goto destroy_class;
+ }
+
ldev->dev.parent = NULL;
ldev->dev.class = smc_class;
ldev->dev.groups = smc_lo_attr_groups;
diff --git a/net/smc/smc_loopback.h b/net/smc/smc_loopback.h
index 02a522e322b4..d4572ca42f08 100644
--- a/net/smc/smc_loopback.h
+++ b/net/smc/smc_loopback.h
@@ -32,16 +32,38 @@ struct smc_lo_dmb_node {
dma_addr_t dma_addr;
};

+struct smc_lo_dev_stats64 {
+ __u64 xfer_bytes;
+ __u64 dmbs_cnt;
+};
+
struct smc_lo_dev {
struct smcd_dev *smcd;
struct device dev;
u8 active;
u16 chid;
struct smcd_gid local_gid;
+ struct smc_lo_dev_stats64 __percpu *stats;
rwlock_t dmb_ht_lock;
DECLARE_BITMAP(sba_idx_mask, SMC_LO_MAX_DMBS);
DECLARE_HASHTABLE(dmb_ht, SMC_LO_DMBS_HASH_BITS);
};
+
+#define SMC_LO_STAT_SUB(ldev, key, val) \
+do { \
+ struct smc_lo_dev_stats64 *_stats = (ldev)->stats; \
+ this_cpu_add((*(_stats)).key, val); \
+} \
+while (0)
+
+#define SMC_LO_STAT_XFER_BYTES(ldev, val) \
+ SMC_LO_STAT_SUB(ldev, xfer_bytes, val)
+
+#define SMC_LO_STAT_DMBS_INC(ldev) \
+ SMC_LO_STAT_SUB(ldev, dmbs_cnt, 1)
+
+#define SMC_LO_STAT_DMBS_DEC(ldev) \
+ SMC_LO_STAT_SUB(ldev, dmbs_cnt, -1)
#endif

int smc_loopback_init(void);
--
2.32.0.3.g01195cf9f