mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
To: Muralidhara M K <muralidhara.mk@amd.com>
Cc: platform-driver-x86@vger.kernel.org,
	LKML <linux-kernel@vger.kernel.org>,
	 Muthusamy Ramalingam <muthusamy.ramalingam@amd.com>
Subject: Re: [PATCH v3 7/7] platform/x86/amd/hsmp: Make metric table read locking use guard(mutex)
Date: Fri, 22 May 2026 13:55:56 +0300 (EEST)	[thread overview]
Message-ID: <1af1f3b6-7554-4e15-8ca1-665e821bced9@linux.intel.com> (raw)
In-Reply-To: <20260517151211.415627-8-muralidhara.mk@amd.com>

On Sun, 17 May 2026, Muralidhara M K wrote:

> Add a per-socket mutex (metric_tbl_lock) to serialize concurrent reads
> on the metric table. Without serialization, two simultaneous
> readers could interleave the SMU refresh command and the
> memcpy_fromio(), producing a torn (mixed old/new) snapshot.
> 
> Use scoped guard(mutex) in hsmp_metric_tbl_read() so the lock is
> automatically released on all return paths. Initialize the mutex with
> devm_mutex_init() in hsmp_get_tbl_dram_base() and return an error if
> initialization fails, avoiding manual mutex_destroy() cleanup paths.
> 
> Co-developed-by: Muthusamy Ramalingam <muthusamy.ramalingam@amd.com>
> Signed-off-by: Muthusamy Ramalingam <muthusamy.ramalingam@amd.com>
> Signed-off-by: Muralidhara M K <muralidhara.mk@amd.com>
> ---
> Changes:
> v1->v2: Add lock
> v2->v3: Replace mutex_init to devm_mutex_init call
> 
>  drivers/platform/x86/amd/hsmp/hsmp.c | 8 ++++++++
>  drivers/platform/x86/amd/hsmp/hsmp.h | 3 +++
>  2 files changed, 11 insertions(+)
> 
> diff --git a/drivers/platform/x86/amd/hsmp/hsmp.c b/drivers/platform/x86/amd/hsmp/hsmp.c
> index 3a02d683dea0..2fec897a95be 100644
> --- a/drivers/platform/x86/amd/hsmp/hsmp.c
> +++ b/drivers/platform/x86/amd/hsmp/hsmp.c
> @@ -447,6 +447,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;
> @@ -492,6 +493,13 @@ int hsmp_get_tbl_dram_base(u16 sock_ind)
>  		dev_err(sock->dev, "Failed to ioremap metric table addr\n");
>  		return -ENOMEM;
>  	}
> +
> +	ret = devm_mutex_init(sock->dev, &sock->metric_tbl_lock);
> +	if (ret) {
> +		dev_err(sock->dev, "Failed to initialize metric table lock\n");
> +		return ret;
> +	}

Sashiko notes a potential problem with this failing and that not resulting 
in a probe fail, which leaves the mutex uninitialized.

But the mutex could be initialized earlier to avoid the problem I think.

> +
>  	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..f7b1cbf19932 100644
> --- a/drivers/platform/x86/amd/hsmp/hsmp.h
> +++ b/drivers/platform/x86/amd/hsmp/hsmp.h
> @@ -15,6 +15,7 @@
>  #include <linux/hwmon.h>
>  #include <linux/kconfig.h>
>  #include <linux/miscdevice.h>
> +#include <linux/mutex.h>
>  #include <linux/pci.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 refreshes from the sysfs path */
> +	struct mutex metric_tbl_lock;
>  	void __iomem *virt_base_addr;
>  	struct semaphore hsmp_sem;
>  	char name[HSMP_ATTR_GRP_NAME_SIZE];
> 

-- 
 i.


  reply	other threads:[~2026-05-22 10:56 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-17 15:12 [PATCH v3 0/7] platform/x86/amd/hsmp: Add support for Family 1Ah, Model 50h-5Fh Muralidhara M K
2026-05-17 15:12 ` [PATCH v3 1/7] platform/x86/amd/hsmp: Add new HSMP messages " Muralidhara M K
2026-05-22 11:49   ` Ilpo Järvinen
2026-05-26  9:21     ` M K, Muralidhara
2026-05-17 15:12 ` [PATCH v3 2/7] platform/x86/amd/hsmp: Add UAPI structures for Family 1Ah Model 50h-5Fh metrics table Muralidhara M K
2026-05-22 11:09   ` Ilpo Järvinen
2026-05-26  9:36     ` M K, Muralidhara
2026-05-17 15:12 ` [PATCH v3 3/7] platform/x86/amd/hsmp: Unify response_sz validation to an upper-bound check Muralidhara M K
2026-05-17 15:12 ` [PATCH v3 4/7] platform/x86/amd/hsmp: Source metric-table size from firmware Muralidhara M K
2026-05-17 15:12 ` [PATCH v3 5/7] platform/x86/amd/hsmp: Add IOCTL_GET_TELEMETRY_DATA for metric table reads Muralidhara M K
2026-05-22 11:04   ` Ilpo Järvinen
2026-05-22 11:27     ` Ilpo Järvinen
2026-05-26  9:43     ` M K, Muralidhara
2026-05-17 15:12 ` [PATCH v3 6/7] platform/x86/amd/hsmp: Drop ACPI sysfs metrics_bin in favour of the IOCTL Muralidhara M K
2026-05-22 11:44   ` Ilpo Järvinen
2026-05-26  9:59     ` M K, Muralidhara
2026-05-17 15:12 ` [PATCH v3 7/7] platform/x86/amd/hsmp: Make metric table read locking use guard(mutex) Muralidhara M K
2026-05-22 10:55   ` Ilpo Järvinen [this message]
2026-05-26 10:02     ` M K, Muralidhara

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1af1f3b6-7554-4e15-8ca1-665e821bced9@linux.intel.com \
    --to=ilpo.jarvinen@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=muralidhara.mk@amd.com \
    --cc=muthusamy.ramalingam@amd.com \
    --cc=platform-driver-x86@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®