From: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
To: Suma Hegde <suma.hegde@amd.com>
Cc: platform-driver-x86@vger.kernel.org,
Hans de Goede <hdegoede@redhat.com>,
Naveen Krishna Chatradhi <naveenkrishna.chatradhi@amd.com>
Subject: Re: [PATCH v2 2/4] platform/x86/amd/hsmp: Add reference counter to track ACPI probe and removal
Date: Wed, 23 Jul 2025 10:17:19 +0300 (EEST) [thread overview]
Message-ID: <117d9290-7e02-e7ce-fe19-eb91628e3f90@linux.intel.com> (raw)
In-Reply-To: <20250723051251.3009625-3-suma.hegde@amd.com>
On Wed, 23 Jul 2025, Suma Hegde wrote:
> Implement reference counting to ensure that the memory allocated for
> the hsmp_pdev->sock structure is released, and the miscellaneous device
> is deregistered only after all socket's removal operations have been
> completed.
>
> Also replace hsmp_pdev->is_probed with hsmp_pdev->ref_cnt.
>
> Signed-off-by: Suma Hegde <suma.hegde@amd.com>
> Reviewed-by: Naveen Krishna Chatradhi <naveenkrishna.chatradhi@amd.com>
> ---
> Changes since v1:
> New patch
>
> drivers/platform/x86/amd/hsmp/acpi.c | 16 ++++++++--------
> drivers/platform/x86/amd/hsmp/hsmp.c | 17 +++++++++++++++++
> drivers/platform/x86/amd/hsmp/hsmp.h | 2 +-
> 3 files changed, 26 insertions(+), 9 deletions(-)
>
> diff --git a/drivers/platform/x86/amd/hsmp/acpi.c b/drivers/platform/x86/amd/hsmp/acpi.c
> index b981ae3157ea..232105226407 100644
> --- a/drivers/platform/x86/amd/hsmp/acpi.c
> +++ b/drivers/platform/x86/amd/hsmp/acpi.c
> @@ -23,6 +23,7 @@
> #include <linux/module.h>
> #include <linux/mutex.h>
> #include <linux/platform_device.h>
> +#include <linux/slab.h>
> #include <linux/sysfs.h>
> #include <linux/uuid.h>
>
> @@ -590,16 +591,15 @@ static int hsmp_acpi_probe(struct platform_device *pdev)
> return -ENOMEM;
> guard(mutex)(&hsmp_lock);
>
> - if (!hsmp_pdev->is_probed) {
> + if (!hsmp_pdev->ref_cnt) {
> hsmp_pdev->num_sockets = amd_num_nodes();
> if (hsmp_pdev->num_sockets == 0 || hsmp_pdev->num_sockets > MAX_AMD_NUM_NODES) {
> dev_err(&pdev->dev, "Wrong number of sockets\n");
> return -ENODEV;
> }
>
> - hsmp_pdev->sock = devm_kcalloc(&pdev->dev, hsmp_pdev->num_sockets,
> - sizeof(*hsmp_pdev->sock),
> - GFP_KERNEL);
> + hsmp_pdev->sock = kcalloc(hsmp_pdev->num_sockets, sizeof(*hsmp_pdev->sock),
> + GFP_KERNEL);
> if (!hsmp_pdev->sock)
> return -ENOMEM;
> }
> @@ -610,15 +610,15 @@ static int hsmp_acpi_probe(struct platform_device *pdev)
> return ret;
> }
>
> - if (!hsmp_pdev->is_probed) {
> + if (!hsmp_pdev->ref_cnt) {
> ret = hsmp_misc_register(&pdev->dev);
> if (ret) {
> dev_err(&pdev->dev, "Failed to register misc device\n");
> return ret;
> }
> - hsmp_pdev->is_probed = true;
> dev_dbg(&pdev->dev, "AMD HSMP ACPI is probed successfully\n");
> }
> + hsmp_pdev->ref_cnt++;
Unfortunately, also this is buggy.
hsmp_pdev->sock is assigned before we're committed to increase ref_cnt
which causes a memleak as the next one probing after the mutex is unlocked
on return will overwrite hsmp_pdev->sock.
There are also linux/kref.h linux/refcount.h which should be considered
instead of creating own ref count code.
> return 0;
> }
> @@ -630,9 +630,9 @@ static void hsmp_acpi_remove(struct platform_device *pdev)
> * We register only one misc_device even on multi-socket system.
> * So, deregister should happen only once.
> */
> - if (hsmp_pdev->is_probed) {
> + if (!(--hsmp_pdev->ref_cnt)) {
> hsmp_misc_deregister();
> - hsmp_pdev->is_probed = false;
> + kfree(hsmp_pdev->sock);
> }
> }
>
> diff --git a/drivers/platform/x86/amd/hsmp/hsmp.c b/drivers/platform/x86/amd/hsmp/hsmp.c
> index 885e2f8136fd..39804ee848ba 100644
> --- a/drivers/platform/x86/amd/hsmp/hsmp.c
> +++ b/drivers/platform/x86/amd/hsmp/hsmp.c
> @@ -12,6 +12,8 @@
> #include <linux/acpi.h>
> #include <linux/delay.h>
> #include <linux/device.h>
> +#include <linux/init.h>
> +#include <linux/module.h>
> #include <linux/semaphore.h>
> #include <linux/sysfs.h>
>
> @@ -454,6 +456,21 @@ struct hsmp_plat_device *get_hsmp_pdev(void)
> {
> return &hsmp_pdev;
> }
> +
> +static int __init hsmp_common_init(void)
> +{
> + hsmp_pdev.ref_cnt = 0;
> +
> + return 0;
> +}
> +
> +static void __exit hsmp_common_exit(void)
> +{
> +}
> +
> +device_initcall(hsmp_common_init);
> +module_exit(hsmp_common_exit);
> +
> EXPORT_SYMBOL_NS_GPL(get_hsmp_pdev, "AMD_HSMP");
>
> MODULE_DESCRIPTION("AMD HSMP Common driver");
> diff --git a/drivers/platform/x86/amd/hsmp/hsmp.h b/drivers/platform/x86/amd/hsmp/hsmp.h
> index 0509a442eaae..1b16fd6a38e1 100644
> --- a/drivers/platform/x86/amd/hsmp/hsmp.h
> +++ b/drivers/platform/x86/amd/hsmp/hsmp.h
> @@ -54,7 +54,7 @@ struct hsmp_plat_device {
> struct hsmp_socket *sock;
> u32 proto_ver;
> u16 num_sockets;
> - bool is_probed;
> + int ref_cnt;
> };
>
> int hsmp_cache_proto_ver(u16 sock_ind);
>
--
i.
next prev parent reply other threads:[~2025-07-23 7:17 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-23 5:12 [PATCH v2 0/4] platform/x86/amd/hsmp: Fix potential issues during async probing of ACPI driver Suma Hegde
2025-07-23 5:12 ` [PATCH v2 1/4] platform/x86/amd/hsmp: Use mutex to synchronize ACPI based probe Suma Hegde
2025-08-19 8:23 ` Ilpo Järvinen
2025-07-23 5:12 ` [PATCH v2 2/4] platform/x86/amd/hsmp: Add reference counter to track ACPI probe and removal Suma Hegde
2025-07-23 7:17 ` Ilpo Järvinen [this message]
2025-07-23 5:12 ` [PATCH v2 3/4] platform/x86/amd/hsmp: Move initialization of num_sockets to hsmp_common_init() Suma Hegde
2025-07-23 5:12 ` [PATCH v2 4/4] platform/x86/amd/hsmp: Ensure initialization of the sock struct in hsmp_send_message() Suma Hegde
2025-07-23 7:10 ` Ilpo Järvinen
2025-07-24 9:20 ` Suma Hegde
2025-08-02 12:31 ` Suma Hegde
2025-08-19 8:21 ` Ilpo Järvinen
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=117d9290-7e02-e7ce-fe19-eb91628e3f90@linux.intel.com \
--to=ilpo.jarvinen@linux.intel.com \
--cc=hdegoede@redhat.com \
--cc=naveenkrishna.chatradhi@amd.com \
--cc=platform-driver-x86@vger.kernel.org \
--cc=suma.hegde@amd.com \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.