[PATCH v2 14/19] platform/x86/intel/tpmi: Use scope based cleanup in the memory dump
From: Kuppuswamy Sathyanarayanan
Date: Thu Sep 24 2026 - 14:36:02 EST
tpmi_mem_dump_show() frees its dump buffer and drops tpmi_dev_lock by
hand at the end of the function, so the ioremap() failure in the middle
of the loop cannot just return. It breaks out with an error stashed in
ret, which is the only reason ret exists.
Attach the buffer to __free(kfree) at its allocation and take the mutex
with guard(), then return the error directly from the loop. mem_write()
in this file already does both, so this makes the two debugfs paths
consistent.
No functional change intended.
Suggested-by: Ilpo Järvinen <ilpo.jarvinen@xxxxxxxxxxxxxxx>
Signed-off-by: Kuppuswamy Sathyanarayanan <sathyanarayanan.kuppuswamy@xxxxxxxxxxxxxxx>
---
drivers/platform/x86/intel/tpmi_common.c | 19 ++++++-------------
1 file changed, 6 insertions(+), 13 deletions(-)
diff --git a/drivers/platform/x86/intel/tpmi_common.c b/drivers/platform/x86/intel/tpmi_common.c
index 00f27ac0a753..5332d30aac6c 100644
--- a/drivers/platform/x86/intel/tpmi_common.c
+++ b/drivers/platform/x86/intel/tpmi_common.c
@@ -393,32 +393,29 @@ static int tpmi_mem_dump_show(struct seq_file *s, void *unused)
{
size_t row_size = MEM_DUMP_COLUMN_COUNT * sizeof(u32);
struct intel_tpmi_pm_feature *pfs = s->private;
- int count, ret = 0;
void __iomem *mem;
+ int count;
u32 size;
u64 off;
- u8 *buffer;
size = tpmi_get_single_entry_size(pfs);
if (!size)
return -EIO;
- buffer = kmalloc(size, GFP_KERNEL);
+ u8 *buffer __free(kfree) = kmalloc(size, GFP_KERNEL);
if (!buffer)
return -ENOMEM;
off = pfs->vsec_offset;
- mutex_lock(&tpmi_dev_lock);
+ guard(mutex)(&tpmi_dev_lock);
for (count = 0; count < pfs->pfs_header.num_entries; ++count) {
seq_printf(s, "TPMI Instance:%d offset:0x%llx\n", count, off);
mem = ioremap(off, size);
- if (!mem) {
- ret = -ENOMEM;
- break;
- }
+ if (!mem)
+ return -ENOMEM;
memcpy_fromio(buffer, mem, size);
@@ -430,11 +427,7 @@ static int tpmi_mem_dump_show(struct seq_file *s, void *unused)
off += size;
}
- mutex_unlock(&tpmi_dev_lock);
-
- kfree(buffer);
-
- return ret;
+ return 0;
}
DEFINE_SHOW_ATTRIBUTE(tpmi_mem_dump);
--
2.43.0