Re: [PATCH v2 6/7] platform/x86/amd/hsmp: Make metric table read locking use guard(mutex)
From: M K, Muralidhara
Date: Tue May 12 2026 - 02:27:31 EST
On 5/11/2026 10:57 PM, Ilpo Järvinen wrote:
Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.Thank you for the suggestion. I will update the code and test the changes.
On Mon, 27 Apr 2026, Muralidhara M K wrote:
Add a per-socket mutex (metric_tbl_lock) to serialize concurrent
reads on the metric table sysfs file. Without serialization, two
simultaneous readers could interleave the SMU refresh command and
the memcpy_fromio(), producing a torn (mixed old/new) snapshot.
Use the scoped guard(mutex) API from <linux/cleanup.h> so the lock
is automatically released when hsmp_metric_tbl_read() returns,
including on error paths.
Co-developed-by: Muthusamy Ramalingam <muthusamy.ramalingam@xxxxxxx>
Signed-off-by: Muthusamy Ramalingam <muthusamy.ramalingam@xxxxxxx>
Signed-off-by: Muralidhara M K <muralidhara.mk@xxxxxxx>
---
Changes v1->v2: New patch
drivers/platform/x86/amd/hsmp/hsmp.c | 5 +++++
drivers/platform/x86/amd/hsmp/hsmp.h | 3 +++
2 files changed, 8 insertions(+)
diff --git a/drivers/platform/x86/amd/hsmp/hsmp.c b/drivers/platform/x86/amd/hsmp/hsmp.c
index d6e377078182..49e7ed9981e9 100644
--- a/drivers/platform/x86/amd/hsmp/hsmp.c
+++ b/drivers/platform/x86/amd/hsmp/hsmp.c
@@ -10,7 +10,9 @@
#include <asm/amd/hsmp.h>
#include <linux/acpi.h>
+#include <linux/cleanup.h>
#include <linux/delay.h>
+#include <linux/mutex.h>
#include <linux/device.h>
#include <linux/semaphore.h>
#include <linux/sysfs.h>
@@ -364,6 +366,7 @@ ssize_t hsmp_metric_tbl_read(struct hsmp_socket *sock, char *buf, size_t size)
msg.msg_id = HSMP_GET_METRIC_TABLE;
msg.sock_ind = sock->sock_ind;
+ guard(mutex)(&sock->metric_tbl_lock);
ret = hsmp_send_message(&msg);
if (ret)
return ret;
@@ -408,6 +411,8 @@ int hsmp_get_tbl_dram_base(u16 sock_ind)
dev_err(sock->dev, "Failed to ioremap metric table addr\n");
return -ENOMEM;
}
+
+ mutex_init(&sock->metric_tbl_lock);
devm_mutex_init() + don't forget to handle errors. (without devm_ you'd
have to handle mutex_destroy() calls too)
I'll send the next version once I've verified it works properly.
return 0;
}
EXPORT_SYMBOL_NS_GPL(hsmp_get_tbl_dram_base, "AMD_HSMP");
diff --git a/drivers/platform/x86/amd/hsmp/hsmp.h b/drivers/platform/x86/amd/hsmp/hsmp.h
index e7f051475728..038678b8ab0e 100644
--- a/drivers/platform/x86/amd/hsmp/hsmp.h
+++ b/drivers/platform/x86/amd/hsmp/hsmp.h
@@ -16,6 +16,7 @@
#include <linux/kconfig.h>
#include <linux/miscdevice.h>
#include <linux/pci.h>
+#include <linux/mutex.h>
#include <linux/semaphore.h>
#include <linux/sysfs.h>
@@ -41,6 +42,8 @@ struct hsmp_socket {
struct bin_attribute hsmp_attr;
struct hsmp_mbaddr_info mbinfo;
void __iomem *metric_tbl_addr;
+ /* Serializes concurrent metric table reads */
+ struct mutex metric_tbl_lock;
void __iomem *virt_base_addr;
struct semaphore hsmp_sem;
char name[HSMP_ATTR_GRP_NAME_SIZE];
--
i.