Skip to contentSkip to conversation

platform/x86: dell: dcdbas: Fix SMI completion wait and buffer handling

PATCH In review Rosen Penev 3 participants 9 messages Nov 18 – Nov 20

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v3 cover platform/x86: dell: dcdbas: Fix SMI completion wait and buffer handling

Fixes for dcdbas.

Patch 1 fixes the host control SMI wait for types 2 and 3, which the compiler removed entirely because the status was a plain memory read, and converts the type 1 wait along with it. Patches 2 and 3 fix the SMI buffer lifetime on unbind and module removal. Patch 4 hardens the validation of the firmware-provided WSMT buffer description.

These patches were developed with the help of an AI coding assistant (Claude Opus 5.5): it reviewed the drivers, found the issues and drafted the fixes and changelogs. I reviewed every change and I am responsible for them.

All patches build cleanly with W=1.

Tested on a Dell Precision M4800 running v7.2.15, using the equivalent backports built as out-of-tree modules:
the driver probes, and growing smi_data_buf_size and then unloading and reloading dcdbas with dell-smbios on top is clean, with the buffer freed on remove. The host control SMI (only used at power-off) and the WSMT paths were not exercised.

Changes in v3:
- Patch 1: use readx_poll_timeout_atomic() instead of read_poll_timeout_atomic().
- v2: https://lore.kernel.org/platform-driver-x86/[email protected]/

Changes in v2:
- Squash the type 1 read_poll_timeout_atomic() conversion (v1 patch 5) into patch 1, so both waits in host_control_smi() change at once.
- Patch 1: include iopoll.h instead of io.h, and return the error from read_poll_timeout_atomic() (-ETIMEDOUT) instead of -ETIME.
- Patch 2: add a Fixes tag.
- Patch 3: make the dcdbas_release_buffer() comment a plain comment instead of malformed kernel-doc.
- Patch 4: the problems were found by code reading, so drop the Fixes
tags and describe the patch as hardening; reject remap_size < 8
instead of <= 8, as an 8 byte mapping just gives an empty buffer.
- v1: https://lore.kernel.org/platform-driver-x86/[email protected]/

Rosen Penev (4):
platform/x86: dell: dcdbas: Actually wait for the host control SMI platform/x86: dell: dcdbas: Suppress bind attributes of the platform
driver
platform/x86: dell: dcdbas: Release the SMI buffer in remove platform/x86: dell: dcdbas: Validate the WSMT buffer description

 drivers/platform/x86/dell/dcdbas.c | 80 ++++++++++++++++++------------
 drivers/platform/x86/dell/dcdbas.h |  1 -
 2 files changed, 48 insertions(+), 33 deletions(-)

base-commit: 2800868f63227c8237d70c41e70d441c919620d6

Signature · 1 line
2.56.1
#1 lore raw

PATCH v3 1/4 platform/x86: dell: dcdbas: Actually wait for the host control SMI

For SMI types 2 and 3, host_control_smi() writes the command status as -1, raises the SMI and then waits for the BIOS to change the status:

	while (apm_cmd->status == ESM_STATUS_CMD_UNSUCCESSFUL) {

apm_cmd->status is a plain memory read, and neither outb() nor inb() tells the compiler that memory may have changed, so the compiler assumes status is still -1. The loop then has no side effects and its return value is not used, so it is removed entirely: in the generated code, the read of the control port is followed directly by the return. The kernel goes on to power off right after raising the host control SMI, without waiting for the BIOS to accept it.

The type 1 wait polls the APM status port instead, in a loop that counts down TIMEOUT_USEC_SHORT_SEMA_BLOCKING iterations and relies on each inb() taking about a microsecond.

Convert both waits to readx_poll_timeout_atomic(), using READ_ONCE() for the status in memory and inb() for the status port, 1us apart for up to TIMEOUT_USEC_SHORT_SEMA_BLOCKING microseconds as the name suggests, and return its error. That is -ETIMEDOUT rather than -ETIME, but both callers ignore the return value. With the delay plus the port read, the type 1 worst-case wait grows from about 10ms to about 20ms, which only matters when the BIOS never answers.

This removes the last users of num_ticks and EXPIRED_TIMER.

Fixes 90563ec4129f ("[PATCH] dcdbas: add Dell Systems Management Base Driver with sysfs support")
---
 drivers/platform/x86/dell/dcdbas.c | 26 ++++++++++++--------------
 drivers/platform/x86/dell/dcdbas.h |  1 -
 2 files changed, 12 insertions(+), 15 deletions(-)
drivers/platform/x86/dell/dcdbas.c +12 -14
index 678f44252a45..f5d214b7bb6c 100644
@@ -20,7 +20,7 @@
 #include <linux/cpu.h>
 #include <linux/gfp.h>
 #include <linux/init.h>
-#include <linux/io.h>
+#include <linux/iopoll.h>
 #include <linux/kernel.h>
 #include <linux/mc146818rtc.h>
 #include <linux/module.h>
@@ -377,9 +377,9 @@ static int host_control_smi(void)
 	struct apm_cmd *apm_cmd;
 	u8 *data;
 	unsigned long flags;
-	u32 num_ticks;
 	s8 cmd_status;
 	u8 index;
+	int ret;
 
 	apm_cmd = (struct apm_cmd *)smi_buf.virt;
 	apm_cmd->status = ESM_STATUS_CMD_UNSUCCESSFUL;
@@ -407,12 +407,11 @@ static int host_control_smi(void)
 		spin_unlock_irqrestore(&rtc_lock, flags);
 
 		/* wait a few to see if it executed */
-		num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING;
-		while ((s8)inb(PCAT_APM_STATUS_PORT) == ESM_STATUS_CMD_UNSUCCESSFUL) {
-			num_ticks--;
-			if (num_ticks == EXPIRED_TIMER)
-				return -ETIME;
-		}
+		ret = readx_poll_timeout_atomic(inb, PCAT_APM_STATUS_PORT, cmd_status,
+						cmd_status != ESM_STATUS_CMD_UNSUCCESSFUL,
+						1, TIMEOUT_USEC_SHORT_SEMA_BLOCKING);
+		if (ret)
+			return ret;
 		break;
 
 	case HC_SMITYPE_TYPE2:
@@ -441,12 +440,11 @@ static int host_control_smi(void)
 		cmd_status = inb(PE1400_APM_CONTROL_PORT);
 
 		/* wait a few to see if it executed */
-		num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING;
-		while (apm_cmd->status == ESM_STATUS_CMD_UNSUCCESSFUL) {
-			num_ticks--;
-			if (num_ticks == EXPIRED_TIMER)
-				return -ETIME;
-		}
+		ret = readx_poll_timeout_atomic(READ_ONCE, apm_cmd->status, cmd_status,
+						cmd_status != ESM_STATUS_CMD_UNSUCCESSFUL,
+						1, TIMEOUT_USEC_SHORT_SEMA_BLOCKING);
+		if (ret)
+			return ret;
 		break;
 
 	default:
drivers/platform/x86/dell/dcdbas.h +0 -1
index a05d7f667586..d5ad2363dd9e 100644
@@ -42,7 +42,6 @@
 #define MAX_SYSMGMT_LONGCMD_SGENTRY_NUM		(16)
 
 #define TIMEOUT_USEC_SHORT_SEMA_BLOCKING	(10000)
-#define EXPIRED_TIMER				(0)
 
 #define SMI_CMD_MAGIC				(0x534D4931)
 #define SMM_EPS_SIG				"$SCB"
Signature · 1 line
2.56.1
Reviewed-by on v3 1/4
#2 lore raw

Re: [PATCH v3 1/4] platform/x86: dell: dcdbas: Actually wait for the host control SMI

Quoted 25 lines · Rosen Penev

For SMI types 2 and 3, host_control_smi() writes the command status as -1, raises the SMI and then waits for the BIOS to change the status:

	while (apm_cmd->status == ESM_STATUS_CMD_UNSUCCESSFUL) {

apm_cmd->status is a plain memory read, and neither outb() nor inb() tells the compiler that memory may have changed, so the compiler assumes status is still -1. The loop then has no side effects and its return value is not used, so it is removed entirely: in the generated code, the read of the control port is followed directly by the return. The kernel goes on to power off right after raising the host control SMI, without waiting for the BIOS to accept it.

The type 1 wait polls the APM status port instead, in a loop that counts down TIMEOUT_USEC_SHORT_SEMA_BLOCKING iterations and relies on each inb() taking about a microsecond.

Convert both waits to readx_poll_timeout_atomic(), using READ_ONCE() for the status in memory and inb() for the status port, 1us apart for up to TIMEOUT_USEC_SHORT_SEMA_BLOCKING microseconds as the name suggests, and return its error. That is -ETIMEDOUT rather than -ETIME, but both callers ignore the return value. With the delay plus the port read, the type 1 worst-case wait grows from about 10ms to about 20ms, which only matters when the BIOS never answers.

Quoted 1 line

This removes the last users of num_ticks and EXPIRED_TIMER.

Looks good now.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

PATCH v3 2/4 platform/x86: dell: dcdbas: Suppress bind attributes of the platform driver

In reply to Rosen Penev

The dcdbas platform device is created by the module itself and stays in use for as long as the module is loaded: dell-smbios-smm allocates its SMI buffers against it through dcdbas_smi_alloc() whether or not the driver is bound.

Binding the driver again after an unbind also leaks state:
dcdbas_check_wsmt() runs a second time and memremap()s the firmware SMI buffer again, overwriting bios_buffer and leaking the previous mapping.

Unbinding or rebinding the device by hand is not useful, so hide the bind and unbind files.

Fixes 12c956c4f32e ("firmware: dcdbas: Add support for WSMT ACPI table")
---
 drivers/platform/x86/dell/dcdbas.c | 1 +
 1 file changed, 1 insertion(+)
drivers/platform/x86/dell/dcdbas.c +1 -0
index f5d214b7bb6c..6bab3e0619bc 100644
@@ -706,6 +706,7 @@ static void dcdbas_remove(struct platform_device *dev)
 static struct platform_driver dcdbas_driver = {
 	.driver		= {
 		.name	= DRIVER_NAME,
+		.suppress_bind_attrs = true,
 	},
 	.probe		= dcdbas_probe,
 	.remove		= dcdbas_remove,
Signature · 1 line
2.56.1
Reviewed-by on v3 2/4
#4 lore raw

Re: [PATCH v3 2/4] platform/x86: dell: dcdbas: Suppress bind attributes of the platform driver

Quoted 5 lines · Rosen Penev

The dcdbas platform device is created by the module itself and stays in use for as long as the module is loaded: dell-smbios-smm allocates its SMI buffers against it through dcdbas_smi_alloc() whether or not the driver is bound.

Quoted 7 lines

Binding the driver again after an unbind also leaks state:
dcdbas_check_wsmt() runs a second time and memremap()s the firmware SMI buffer again, overwriting bios_buffer and leaking the previous mapping.

Unbinding or rebinding the device by hand is not useful, so hide the bind and unbind files.

Sounds legit and a proposed workaround is often the way to fix it.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw

PATCH v3 3/4 platform/x86: dell: dcdbas: Release the SMI buffer in remove

In reply to Rosen Penev

dcdbas_exit() frees the SMI data buffer before platform_device_unregister() removes the sysfs attributes, and without holding smi_data_lock. The comment claims that only at module exit can all sysfs attributes be released, but device attributes do not pin the
module: while rmmod runs, smi_data_write() or smi_data_buf_size_store() can still be executing. They can then memcpy() into the buffer as it is freed, or allocate a new buffer that is never freed.

sysfs_remove_group() in dcdbas_remove() waits for running attribute handlers, and dcdbas_pdev is still valid at that point. Free the buffer there, or unmap the firmware-provided buffer when WSMT is in use, and also do it when probe fails after dcdbas_check_wsmt() mapped the firmware buffer. Reset the WSMT state with it so a later probe starts clean.

---
 drivers/platform/x86/dell/dcdbas.c | 44 ++++++++++++++++++------------
 1 file changed, 26 insertions(+), 18 deletions(-)
drivers/platform/x86/dell/dcdbas.c +26 -18
index 6bab3e0619bc..1f64f707744c 100644
@@ -97,6 +97,20 @@ static void smi_data_buf_free(void)
 	dcdbas_smi_free(&smi_buf);
 }
 
+/* Free the SMI data buffer, or unmap the firmware one when WSMT is in use */
+static void dcdbas_release_buffer(void)
+{
+	if (wsmt_enabled) {
+		memunmap(bios_buffer);
+		bios_buffer = NULL;
+		smi_buf = (struct smi_buffer){};
+		max_smi_data_buf_size = MAX_SMI_DATA_BUF_SIZE;
+		wsmt_enabled = false;
+	} else {
+		smi_data_buf_free();
+	}
+}
+
 /**
  * smi_data_buf_realloc: grow SMI data buffer if needed
  */
@@ -683,11 +697,11 @@ static int dcdbas_probe(struct platform_device *dev)
 	 */
 	error = dma_set_coherent_mask(&dcdbas_pdev->dev, DMA_BIT_MASK(32));
 	if (error)
-		return error;
+		goto err_release_buffer;
 
 	error = sysfs_create_group(&dev->dev.kobj, &dcdbas_attr_group);
 	if (error)
-		return error;
+		goto err_release_buffer;
 
 	register_reboot_notifier(&dcdbas_reboot_nb);
 
@@ -695,12 +709,22 @@ static int dcdbas_probe(struct platform_device *dev)
 		 DRIVER_DESCRIPTION, DRIVER_VERSION);
 
 	return 0;
+
+err_release_buffer:
+	dcdbas_release_buffer();
+	return error;
 }
 
 static void dcdbas_remove(struct platform_device *dev)
 {
 	unregister_reboot_notifier(&dcdbas_reboot_nb);
 	sysfs_remove_group(&dev->dev.kobj, &dcdbas_attr_group);
+
+	/*
+	 * sysfs_remove_group() waited for any attribute handler still
+	 * running, so nothing else uses the buffer now.
+	 */
+	dcdbas_release_buffer();
 }
 
 static struct platform_driver dcdbas_driver = {
@@ -749,22 +773,6 @@ static int __init dcdbas_init(void)
  */
 static void __exit dcdbas_exit(void)
 {
-	/*
-	 * make sure functions that use dcdbas_pdev are called
-	 * before platform_device_unregister
-	 */
-	unregister_reboot_notifier(&dcdbas_reboot_nb);
-
-	/*
-	 * We have to free the buffer here instead of dcdbas_remove
-	 * because only in module exit function we can be sure that
-	 * all sysfs attributes belonging to this module have been
-	 * released.
-	 */
-	if (dcdbas_pdev)
-		smi_data_buf_free();
-	if (bios_buffer)
-		memunmap(bios_buffer);
 	platform_device_unregister(dcdbas_pdev_reg);
 	platform_driver_unregister(&dcdbas_driver);
 }
Signature · 1 line
2.56.1
maintainer
#6 lore raw

Re: [PATCH v3 3/4] platform/x86: dell: dcdbas: Release the SMI buffer in remove

Quoted 2 lines · Rosen Penev

dcdbas_exit() frees the SMI data buffer before platform_device_unregister() removes the sysfs attributes, and without

This is very confusingly written, platform_device_unregister() does not remove sysfs attributes. dcdbas_remove() does as is written below.

I think the entire sysfs attribute discussion (which is also stating contradicting things) is sidetracking what looks to be the main problem, that something is done in dcdbas_exit() that should be done in dcdbas_remove(), I needed to read very much between the lines and had already misunderstood what the all-over-the-place changelog had spewn on my face.

Also, why isn't this driver using .dev_groups??

Quoted 1 line

holding smi_data_lock.

Not sure why this is relevant, this looks an ordering problem, not a locking problem??

It's further reinforced by the fact that you did not make smi_data_lock related changes in the patch, AFAICT.

Quoted 104 lines

The comment claims that only at module exit can
all sysfs attributes be released, but device attributes do not pin the
module: while rmmod runs, smi_data_write() or smi_data_buf_size_store() can still be executing. They can then memcpy() into the buffer as it is freed, or allocate a new buffer that is never freed.

sysfs_remove_group() in dcdbas_remove() waits for running attribute handlers, and dcdbas_pdev is still valid at that point. Free the buffer there, or unmap the firmware-provided buffer when WSMT is in use, and also do it when probe fails after dcdbas_check_wsmt() mapped the firmware buffer. Reset the WSMT state with it so a later probe starts clean.

---
 drivers/platform/x86/dell/dcdbas.c | 44 ++++++++++++++++++------------
 1 file changed, 26 insertions(+), 18 deletions(-)
drivers/platform/x86/dell/dcdbas.c +26 -18
index 6bab3e0619bc..1f64f707744c 100644
@@ -97,6 +97,20 @@ static void smi_data_buf_free(void)
 	dcdbas_smi_free(&smi_buf);
 }
 
+/* Free the SMI data buffer, or unmap the firmware one when WSMT is in use */
+static void dcdbas_release_buffer(void)
+{
+	if (wsmt_enabled) {
+		memunmap(bios_buffer);
+		bios_buffer = NULL;
+		smi_buf = (struct smi_buffer){};
+		max_smi_data_buf_size = MAX_SMI_DATA_BUF_SIZE;
+		wsmt_enabled = false;
+	} else {
+		smi_data_buf_free();
+	}
+}
+
 /**
  * smi_data_buf_realloc: grow SMI data buffer if needed
  */
@@ -683,11 +697,11 @@ static int dcdbas_probe(struct platform_device *dev)
 	 */
 	error = dma_set_coherent_mask(&dcdbas_pdev->dev, DMA_BIT_MASK(32));
 	if (error)
-		return error;
+		goto err_release_buffer;
 
 	error = sysfs_create_group(&dev->dev.kobj, &dcdbas_attr_group);
 	if (error)
-		return error;
+		goto err_release_buffer;
 
 	register_reboot_notifier(&dcdbas_reboot_nb);
 
@@ -695,12 +709,22 @@ static int dcdbas_probe(struct platform_device *dev)
 		 DRIVER_DESCRIPTION, DRIVER_VERSION);
 
 	return 0;
+
+err_release_buffer:
+	dcdbas_release_buffer();
+	return error;
 }
 
 static void dcdbas_remove(struct platform_device *dev)
 {
 	unregister_reboot_notifier(&dcdbas_reboot_nb);
 	sysfs_remove_group(&dev->dev.kobj, &dcdbas_attr_group);
+
+	/*
+	 * sysfs_remove_group() waited for any attribute handler still
+	 * running, so nothing else uses the buffer now.
+	 */
+	dcdbas_release_buffer();
 }
 
 static struct platform_driver dcdbas_driver = {
@@ -749,22 +773,6 @@ static int __init dcdbas_init(void)
  */
 static void __exit dcdbas_exit(void)
 {
-	/*
-	 * make sure functions that use dcdbas_pdev are called
-	 * before platform_device_unregister
-	 */
-	unregister_reboot_notifier(&dcdbas_reboot_nb);
-
-	/*
-	 * We have to free the buffer here instead of dcdbas_remove
-	 * because only in module exit function we can be sure that
-	 * all sysfs attributes belonging to this module have been
-	 * released.
-	 */
-	if (dcdbas_pdev)
-		smi_data_buf_free();
-	if (bios_buffer)
-		memunmap(bios_buffer);
 	platform_device_unregister(dcdbas_pdev_reg);
 	platform_driver_unregister(&dcdbas_driver);
 }
Signature · 1 line
 i.
#7 lore raw

PATCH v3 4/4 platform/x86: dell: dcdbas: Validate the WSMT buffer description

In reply to Rosen Penev

dcdbas_check_wsmt() takes the address and size of the firmware SMI buffer from an SMBIOS OEM string or from an EPS table in the BIOS area, and sets

	smi_buf.size = remap_size - 8;
	max_smi_data_buf_size = smi_buf.size;

without checking that remap_size covers the 8 byte semaphore. An OEM string such as "30[addr;4]" would make the size wrap to almost 2^64, and smi_data reads and writes could then access memory well past the mapping.

check_eps_table() also accepts a table with length 0, since the checksum of zero bytes is zero, and then trusts the rest of it.

No firmware is known to describe the buffer like this, so this is hardening against malformed tables rather than a fix for a reported problem. Reject buffers smaller than the semaphore, and EPS tables shorter than struct smm_eps_table.

---
 drivers/platform/x86/dell/dcdbas.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/platform/x86/dell/dcdbas.c +9 -0
index 1f64f707744c..7fa6faead7c3 100644
@@ -536,6 +536,9 @@ static inline struct smm_eps_table *check_eps_table(u8 *addr)
 	if (strncmp(eps->smm_comm_buff_anchor, SMM_EPS_SIG, 4) != 0)
 		return NULL;
 
+	if (eps->length < sizeof(*eps))
+		return NULL;
+
 	if (checksum(addr, eps->length) != 0)
 		return NULL;
 
@@ -596,6 +599,12 @@ static int dcdbas_check_wsmt(void)
 		dev_warn(&dcdbas_pdev->dev, "found WSMT, but buffer address is above 4GB\n");
 		return -EINVAL;
 	}
+	/* The first 8 bytes are a semaphore, not part of the data buffer */
+	if (remap_size < 8) {
+		dev_warn(&dcdbas_pdev->dev, "found WSMT, but buffer size %llu is too small\n",
+			 remap_size);
+		return -EINVAL;
+	}
 	/*
 	 * Limit remap size to MAX_SMI_DATA_BUF_SIZE + 8 (since the first 8
 	 * bytes are used for a semaphore, not the data buffer itself).
Signature · 1 line
2.56.1
Reviewed-by on v3 4/4
#8 lore raw

Re: [PATCH v3 4/4] platform/x86: dell: dcdbas: Validate the WSMT buffer description

Quoted 12 lines · Rosen Penev

dcdbas_check_wsmt() takes the address and size of the firmware SMI buffer from an SMBIOS OEM string or from an EPS table in the BIOS area, and sets

	smi_buf.size = remap_size - 8;
	max_smi_data_buf_size = smi_buf.size;

without checking that remap_size covers the 8 byte semaphore. An OEM string such as "30[addr;4]" would make the size wrap to almost 2^64, and smi_data reads and writes could then access memory well past the mapping.

Quoted 7 lines

check_eps_table() also accepts a table with length 0, since the checksum of zero bytes is zero, and then trusts the rest of it.

No firmware is known to describe the buffer like this, so this is hardening against malformed tables rather than a fix for a reported problem. Reject buffers smaller than the semaphore, and EPS tables shorter than struct smm_eps_table.

Now the commit message reads fine.

Signature · 2 lines
With Best Regards,
Andy Shevchenko