mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Awal Awal via B4 Relay <devnull+awal.nova.gmail.com@kernel.org>
To: "Shyam Sundar S K" <Shyam-sundar.S-k@amd.com>,
	"Hans de Goede" <hansg@kernel.org>,
	"Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
Cc: Lizhi Hou <lizhi.hou@amd.com>,
	Mario Limonciello <superm1@kernel.org>,
	 platform-driver-x86@vger.kernel.org,
	linux-kernel@vger.kernel.org,  stable@vger.kernel.org,
	Awal Awal <awal.nova@gmail.com>
Subject: [PATCH] platform/x86/amd/pmf: Fix memory leak in NPU metrics query
Date: Fri, 23 Oct 2026 10:08:45 +0200	[thread overview]
Message-ID: <20261023-amd-pmf-npu-leak-v1-1-798e7161bfba@gmail.com> (raw)

From: Awal Awal <awal.nova@gmail.com>

amd_pmf_get_smu_metrics() calls amd_pmf_set_dram_addr() with
alloc_buffer set to true on every NPU metrics request. Each call
devm_kzalloc()s a new metrics table buffer and overwrites dev->buf with
it. The previous buffer stays on the devres list and is only released
when the device is unbound, so every request leaks one buffer.

amdxdna calls amd_pmf_get_npu_data() for its sensor query, so any
userspace monitor that polls NPU utilisation through
DRM_IOCTL_AMDXDNA_GET_INFO leaks memory for as long as it runs. On a
Ryzen AI 9 365 (family 1Ah model 20h) laptop with a system monitor
polling about 1.4 times per second, the kmalloc-512 cache grew to 5.8
million objects (2.8 GiB) over 38 days of uptime.

The buffer size depends only on the CPU ID, and the buffer is usually
already allocated by amd_pmf_init_metrics_table() or
amd_pmf_init_smart_pc(). Allocate it only if it does not exist yet and
keep reprogramming its address on every request as before.

Fixes: 118222e20d16 ("platform/x86/amd/pmf: Introduce new interface to export NPU metrics")
Cc: stable@vger.kernel.org
Signed-off-by: Awal Awal <awal.nova@gmail.com>
---
Tested on a Lenovo Yoga Pro 7 14ASP10 (Ryzen AI 9 365) with the
equivalent change applied to core.c of a 7.0.14 based kernel. With the
Resources app polling, the unpatched driver made 84 devm_kmalloc() calls
for 84 NPU metrics queries in 60 s. The patched driver served 106
queries in 60 s, all returning 0, with no devm_kmalloc() calls and a
flat kmalloc-512 cache.

Up to v7.2 this code lives in drivers/platform/x86/amd/pmf/core.c
(it moved to metrics.c in v7.3); the same one-line change applies to
amd_pmf_get_smu_metrics() there.
---
 drivers/platform/x86/amd/pmf/metrics.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/platform/x86/amd/pmf/metrics.c b/drivers/platform/x86/amd/pmf/metrics.c
index 5c0e368ec..6bad7a409 100644
--- a/drivers/platform/x86/amd/pmf/metrics.c
+++ b/drivers/platform/x86/amd/pmf/metrics.c
@@ -261,7 +261,7 @@ static int amd_pmf_get_smu_metrics(struct amd_pmf_dev *dev, struct amd_pmf_npu_m
 	switch (dev->cpu_id) {
 	case PCI_DEVICE_ID_AMD_1AH_M20H_ROOT:
 	case PCI_DEVICE_ID_AMD_1AH_M60H_ROOT:
-		ret = amd_pmf_set_dram_addr(dev, true);
+		ret = amd_pmf_set_dram_addr(dev, !dev->buf);
 		if (ret)
 			return ret;
 

---
base-commit: 893018d51f27ec0449155b0afe371aaf65380630
change-id: 20261023-amd-pmf-npu-leak-f449a38c0300

Best regards,
--  
Awal Awal <awal.nova@gmail.com>



                 reply	other threads:[~2026-10-23  8:08 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261023-amd-pmf-npu-leak-v1-1-798e7161bfba@gmail.com \
    --to=devnull+awal.nova.gmail.com@kernel.org \
    --cc=Shyam-sundar.S-k@amd.com \
    --cc=awal.nova@gmail.com \
    --cc=hansg@kernel.org \
    --cc=ilpo.jarvinen@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lizhi.hou@amd.com \
    --cc=platform-driver-x86@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=superm1@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®