Skip to contentSkip to conversation

mfd: ls2kbmc: multiple fixes for this driver

RFC PATCH Superseded Miao Wang 2 participants 11 messages Jul 7

Subsystems (3)

v1 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Miao Wang

author
#0 lore raw

cover mfd: ls2kbmc: multiple fixes for this driver

Previously, the driver has been introduced to support the Loongson 2K BMC running on the Loongson Servers, which is essential to prevent the system from hanging when the BMC is being reset and the default efi-framebuffer is being used. However, there are some drawbacks in the driver.

Firstly, the driver tries to read and write to the connected PCI-E host controller registers, assuming that the BMC is connected to LS7A PCI-E host controller. This assumption should be true for real products, but to prevent from accidentally reading and writing to the wrong PCI-E host controller, this driver should be modified to check this before accessing the registers.

Secondly, the driver uses non-exported functions to tell the vt subsystem to redraw the screen, preventing the driver from being compiling as a module. This can be fixed by using the exported functions instead.

Thirdly, the driver directly accesses the GPIO controller registers using hard-coded addresses, which might conflict with the loaded GPIO controller driver for the same GPIO controller. This is fixed in this series by using the GPIO subsystem APIs instead. However, legacy GPIO APIs have to be used in this fixed to correctly request a GPIO descriptor from the GPIO subsystem, which might be further discussed to find a better solution.

Finally, there is a minor issue in the driver where it changes the mode string describing the screen resolution during probing, which prevents the device from being probed again if -EPROBE_DEFER is returned by the probe function.

I have tested the changes in this series on a single-socket Loongson 3C6000 server with a Loongson 2K BMC, and the driver works as expected when the corresponding GPIO driver is additionally loaded.

---
Miao Wang (5):
mfd: ls2kbmc: make a copy when parsing mode string
mfd: ls2kbmc: sanity check for the connected pci port
mfd: ls2kbmc: redraw using exported functions
mfd: ls2kbmc: able to compile as a module
mfd: ls2kbmc: capture the reset event of BMC through GPIO

 drivers/mfd/Kconfig         |   2 +-
 drivers/mfd/ls2k-bmc-core.c | 224 ++++++++++++++++++++++++++++++++++----------
 2 files changed, 174 insertions(+), 52 deletions(-)

---
base-commit: 0e35b9b6ec0ffcc5e23cbdec09f5c622ad532b53
change-id: 20260626-ls2kbmc-mod-5209193009b2

Best regards,

Signature · 1 line
Miao Wang <[email protected]>
author
#1 lore raw

RFC PATCH 1/5 mfd: ls2kbmc: make a copy when parsing mode string

When parsing the mode string from BMC, the string is manipulated in-place with strsep(), preventing from parsing it again. Make a copy of the original string and manipulate the copy instead to fix this.

Fixes 0d64f6d1ffe9 ("mfd: ls2kbmc: Introduce Loongson-2K BMC core driver")
---
 drivers/mfd/ls2k-bmc-core.c | 25 ++++++++++++++++++++++---
 1 file changed, 22 insertions(+), 3 deletions(-)
drivers/mfd/ls2k-bmc-core.c +22 -3
index 408056bfb2fe757a5bde43775a483a48352e706d..30b275bc48794a22249aeb8ba805ef78772d3466 100644
@@ -427,34 +427,53 @@ static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata)
  */
 static int ls2k_bmc_parse_mode(struct pci_dev *pdev, struct simplefb_platform_data *pd)
 {
-	char *mode;
+	/* Assume 1024 bytes is enough for the resolution string */
+	char mode_buf[1024];
+	char *mode = mode_buf;
+	const void __iomem *mode_base;
 	int depth, ret;
 
 	/* The last 16M of PCI BAR0 is used to store the resolution string. */
-	mode = devm_ioremap(&pdev->dev, pci_resource_start(pdev, 0) + SZ_16M, SZ_16M);
-	if (!mode)
+	mode_base = devm_ioremap(&pdev->dev, pci_resource_start(pdev, 0) + SZ_16M,
+				 sizeof(mode_buf));
+	if (!mode_base)
 		return -ENOMEM;
+	memcpy(mode_buf, mode_base, sizeof(mode_buf) - 1);
+	mode_buf[sizeof(mode_buf) - 1] = '\0';
+	devm_iounmap(&pdev->dev, (void __iomem *)mode_base);
 
 	/* The resolution field starts with the flag "video=". */
 	if (!strncmp(mode, "video=", 6))
 		mode = mode + 6;
+	else
+		goto invalid_mode;
 
 	ret = kstrtoint(strsep(&mode, "x"), 10, &pd->width);
 	if (ret)
 		return ret;
+	if (mode == NULL)
+		goto invalid_mode;
 
 	ret = kstrtoint(strsep(&mode, "-"), 10, &pd->height);
 	if (ret)
 		return ret;
+	if (mode == NULL)
+		goto invalid_mode;
 
 	ret = kstrtoint(strsep(&mode, "@"), 10, &depth);
 	if (ret)
 		return ret;
+	if (mode == NULL)
+		goto invalid_mode;
 
 	pd->stride = pd->width * depth / 8;
 	pd->format = depth == 32 ? "a8r8g8b8" : "r5g6b5";
 
 	return 0;
+
+invalid_mode:
+	dev_err(&pdev->dev, "Invalid resolution string: %s\n", mode_buf);
+	return -EINVAL;
 }
 
 static int ls2k_bmc_probe(struct pci_dev *dev, const struct pci_device_id *id)
Signature · 1 line
2.49.0
#2 lore raw

Re: [PATCH RFC 1/5] mfd: ls2kbmc: make a copy when parsing mode string

Thank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider:

New issues:
- [High] The new mode parsing logic incorrectly rejects valid resolution strings that lack a trailing refresh rate (e.g. strings without an '@').
- [High] Standard memcpy is used to read from I/O memory instead of the required memcpy_fromio.
- [Low] Allocating a 1024-byte array on the kernel stack is wasteful and increases the risk of stack overflow.
- [Low] The patch abuses managed devm_ioremap and devm_iounmap for a temporary, short-lived memory mapping.
- [Low] The newly introduced error log prints a truncated and misleading string due to in-place modifications by strsep().
- [Low] Printing raw, untrusted memory directly to the kernel log allows for terminal escape sequence injection.

Pre-existing issues:
- [High] The driver dynamically modifies a shared static mfd_cell array and stores a local stack pointer into it, violating MFD core guidelines.
- [High] The bmc_reset_work workqueue is not explicitly cancelled during probe failure or device unbind, leading to a potential Use-After-Free.
- [High] Invalid use of kstrtoint on u32 variables combined with untrusted input can lead to an integer overflow when calculating the framebuffer stride. --

--- Patch [1]: [PATCH RFC 1/5] mfd: ls2kbmc: make a copy when parsing mode string --- commit 50370b4ef23f8df41c25e8399d557676642d9cdc
Author: Miao Wang <[email protected]>

mfd: ls2kbmc: make a copy when parsing mode string

This patch modifies the mode string parsing logic to copy the BMC memory into a local buffer instead of parsing it in-place. This prevents the original memory from being modified by strsep().

drivers/mfd/ls2k-bmc-core.c static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata) · line 431

Quoted 11 lines
drivers/mfd/ls2k-bmc-core.c +2 -1
index 408056bfb2fe7..30b275bc48794 100644
@@ -427,34 +427,53 @@ static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata)
  */
 static int ls2k_bmc_parse_mode(struct pci_dev *pdev, struct simplefb_platform_data *pd)
 {
-	char *mode;
+	/* Assume 1024 bytes is enough for the resolution string */
+	char mode_buf[1024];

[Severity: Low]
Is allocating a 1024-byte array on the kernel stack necessary here? Standard video mode strings are usually well under 64 bytes. Large stack allocations consume limited stack space and can increase the risk of stack overflows.

drivers/mfd/ls2k-bmc-core.c static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata) · line 438

Quoted 9 lines
drivers/mfd/ls2k-bmc-core.c +4 -2
+	char *mode = mode_buf;
+	const void __iomem *mode_base;
 	int depth, ret;
 
 	/* The last 16M of PCI BAR0 is used to store the resolution string. */
-	mode = devm_ioremap(&pdev->dev, pci_resource_start(pdev, 0) + SZ_16M, SZ_16M);
-	if (!mode)
+	mode_base = devm_ioremap(&pdev->dev, pci_resource_start(pdev, 0) + SZ_16M,
+				 sizeof(mode_buf));

[Severity: Low]
Since this mapping is temporary and is unmapped immediately after copying, does it make sense to use the managed devres APIs (devm_ioremap)? Using standard ioremap() and iounmap() avoids allocating unnecessary tracking nodes for a short-lived mapping.

drivers/mfd/ls2k-bmc-core.c static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata) · line 441

Quoted 3 lines
drivers/mfd/ls2k-bmc-core.c +2 -0
+	if (!mode_base)
 		return -ENOMEM;
+	memcpy(mode_buf, mode_base, sizeof(mode_buf) - 1);

[Severity: High]
Can we safely use standard memcpy() on an I/O mapped pointer (__iomem)? On architectures like LoongArch and MIPS, accessing memory-mapped I/O with standard string functions can lead to unaligned access exceptions or kernel panics. Should this be using memcpy_fromio() instead?

drivers/mfd/ls2k-bmc-core.c static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata) · line 451

Quoted 10 lines
drivers/mfd/ls2k-bmc-core.c +4 -0
+	mode_buf[sizeof(mode_buf) - 1] = '\0';
+	devm_iounmap(&pdev->dev, (void __iomem *)mode_base);
 
 	/* The resolution field starts with the flag "video=". */
 	if (!strncmp(mode, "video=", 6))
 		mode = mode + 6;
+	else
+		goto invalid_mode;
 
 	ret = kstrtoint(strsep(&mode, "x"), 10, &pd->width);

[Severity: High]
This is a pre-existing issue, but could passing a negative value in the mode string bypass the kstrtoint() check?

Since pd->width and pd->height are u32, a maliciously constructed string like "video=-1x1080-32" would be parsed successfully by kstrtoint(), cast to a huge u32 value, and cause a silent integer overflow later when calculating the framebuffer stride.

Quoted 6 lines
drivers/mfd/ls2k-bmc-core.c +2 -0
 	if (ret)
 		return ret;
+	if (mode == NULL)
+		goto invalid_mode;
 
 	ret = kstrtoint(strsep(&mode, "-"), 10, &pd->height);

drivers/mfd/ls2k-bmc-core.c static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata) · line 467

Quoted 10 lines
drivers/mfd/ls2k-bmc-core.c +4 -0
 	if (ret)
 		return ret;
+	if (mode == NULL)
+		goto invalid_mode;
 
 	ret = kstrtoint(strsep(&mode, "@"), 10, &depth);
 	if (ret)
 		return ret;
+	if (mode == NULL)
+		goto invalid_mode;

[Severity: High]
Does this new check erroneously reject valid resolution strings?

If the mode string does not contain a refresh rate (for example, "video=1920x1080-32" with no '@' character), strsep(&mode, "@") will set mode to NULL. This check will then abort the probe, even though the width, height, and depth were successfully parsed.

drivers/mfd/ls2k-bmc-core.c static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata) · line 475

Quoted 8 lines
drivers/mfd/ls2k-bmc-core.c +3 -0
 	pd->stride = pd->width * depth / 8;
 	pd->format = depth == 32 ? "a8r8g8b8" : "r5g6b5";
 
 	return 0;
+
+invalid_mode:
+	dev_err(&pdev->dev, "Invalid resolution string: %s\n", mode_buf);

[Severity: Low]
Does printing the modified mode_buf obscure the actual error? Because the preceding strsep() calls modify the string in-place by replacing delimiters with NUL bytes, this log message will terminate early and only print a truncated portion of the string.

[Severity: Low]
Additionally, is it safe to print raw, untrusted hardware memory directly to the kernel log? Embedding non-printable characters or ANSI terminal escape sequences could spoof log entries or exploit terminal emulator vulnerabilities.

drivers/mfd/ls2k-bmc-core.c static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata) · line 479

Quoted 4 lines
drivers/mfd/ls2k-bmc-core.c +1 -0
+	return -EINVAL;
 }
 
 static int ls2k_bmc_probe(struct pci_dev *dev, const struct pci_device_id *id)

[Severity: High]
This is a pre-existing issue, but I noticed that during probe, the ls2k_bmc_cells array is dynamically modified:

drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_probe() {
    struct simplefb_platform_data pd;
    ...
    ls2k_bmc_cells[LS2K_BMC_DISPLAY].platform_data = &pd;
    ...
}

Is it safe to store a local stack pointer (&pd) into a global non-const mfd_cell array? The MFD guidelines explicitly forbid this pattern as it leaves a dangling stack pointer after the probe function returns and causes a data race if multiple devices probe concurrently.

[Severity: High]
This is another pre-existing issue, but if ls2k_bmc_parse_mode() fails and causes the probe to abort, does the driver leave the bmc_reset_work active?

drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_init() {
    ...
    INIT_WORK(&ddata->bmc_reset_work, ls2k_bmc_events_fn);
    ret = devm_request_irq(&pdev->dev, pdev->irq, ls2k_bmc_interrupt, ...);
    ...
}

If the IRQ is requested and schedules the work, but a later step in probe fails, the devres cleanup will free the data structure without synchronously cancelling the workqueue via cancel_work_sync(). Could this lead to a use-after-free if the worker thread runs after the data is freed?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author
#3 lore raw

RFC PATCH 2/5 mfd: ls2kbmc: sanity check for the connected pci port

In reply to Miao Wang

When the bmc resets, the recovery procedure require to reconfigure the parent device. The driver assumes that the parent device should be LS7A. Add a sanity check on initialization to ensure this and prevent from accidentally operating on non-LS7A ports.

Fixes d952bba3fbb5 ("mfd: ls2kbmc: Add Loongson-2K BMC reset function support")
---
 drivers/mfd/ls2k-bmc-core.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
drivers/mfd/ls2k-bmc-core.c +32 -0
index 30b275bc48794a22249aeb8ba805ef78772d3466..39ce5e55ffd6d3a578008dd752b3e8dceec9bd81 100644
@@ -35,6 +35,15 @@
 #define LS2K_IPMI3_RES_START		(LS2K_IPMI2_RES_START + LS2K_IPMI_RES_SIZE)
 #define LS2K_IPMI4_RES_START		(LS2K_IPMI3_RES_START + LS2K_IPMI_RES_SIZE)
 
+/* LS7A port Device IDs */
+#define DEV_LS7A1K_PCIE_PORT0	0x7a09
+#define DEV_LS7A1K_PCIE_PORT1	0x7a19
+#define DEV_LS7A1K_PCIE_PORT2	0x7a29
+#define DEV_LS7A2K_PCIE_PORT0	0x7a39
+#define DEV_LS7A2K_PCIE_PORT1	0x7a49
+#define DEV_LS7A2K_PCIE_PORT2	0x7a59
+#define DEV_LS7A2K_PCIE_PORT3	0x7a69
+
 #define LS7A_PCI_CFG_SIZE		0x100
 
 /* LS7A bridge registers */
@@ -476,6 +485,24 @@ static int ls2k_bmc_parse_mode(struct pci_dev *pdev, struct simplefb_platform_da
 	return -EINVAL;
 }
 
+static struct pci_device_id ls7a_ports[] = {
+	{ PCI_DEVICE(PCI_VENDOR_ID_LOONGSON, DEV_LS7A1K_PCIE_PORT0) },
+	{ PCI_DEVICE(PCI_VENDOR_ID_LOONGSON, DEV_LS7A1K_PCIE_PORT1) },
+	{ PCI_DEVICE(PCI_VENDOR_ID_LOONGSON, DEV_LS7A1K_PCIE_PORT2) },
+	{ PCI_DEVICE(PCI_VENDOR_ID_LOONGSON, DEV_LS7A2K_PCIE_PORT0) },
+	{ PCI_DEVICE(PCI_VENDOR_ID_LOONGSON, DEV_LS7A2K_PCIE_PORT1) },
+	{ PCI_DEVICE(PCI_VENDOR_ID_LOONGSON, DEV_LS7A2K_PCIE_PORT2) },
+	{ PCI_DEVICE(PCI_VENDOR_ID_LOONGSON, DEV_LS7A2K_PCIE_PORT3) },
+	{ }
+};
+
+static bool ls2k_check_parent(struct pci_dev *dev)
+{
+	struct pci_dev *parent = dev->bus->self;
+
+	return pci_match_id(ls7a_ports, parent) != NULL;
+}
+
 static int ls2k_bmc_probe(struct pci_dev *dev, const struct pci_device_id *id)
 {
 	struct simplefb_platform_data pd;
@@ -487,6 +514,11 @@ static int ls2k_bmc_probe(struct pci_dev *dev, const struct pci_device_id *id)
 	if (ret)
 		return ret;
 
+	if (!ls2k_check_parent(dev)) {
+		dev_err(&dev->dev, "Expected to be connected to LS7A PCI-E port\n");
+		return -ENODEV;
+	}
+
 	ddata = devm_kzalloc(&dev->dev, sizeof(*ddata), GFP_KERNEL);
 	if (!ddata)
 		return -ENOMEM;
Signature · 1 line
2.49.0
#4 lore raw

Re: [PATCH RFC 2/5] mfd: ls2kbmc: sanity check for the connected pci port

Thank you for your contribution! Sashiko AI review found 8 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 8 potential issue(s) to consider:

New issues:
- [High] The newly added ls2k_check_parent function dereferences a potentially NULL parent pointer, leading to a kernel panic during device probe.
- [Low] The ls7a_ports device ID array is not marked const.
- [Low] The commit message description is not capitalized, violating MFD subsystem guidelines.

Pre-existing issues:
- [High] Pending bmc_reset_work is not canceled on probe failure or device unbind, leading to a Use-After-Free.
- [High] Dynamically modifying the global ls2k_bmc_cells array with a stack pointer violates MFD standards and creates a data race with a dangling pointer.
- [High] Standard memcpy is used to read from I/O memory instead of memcpy_fromio.
- [High] The interrupt handler unconditionally claims the interrupt without checking hardware status, breaking shared IRQ lines.
- [High] The driver maps and reads memory from PCI BAR0 without validating the size of the BAR, potentially leaking arbitrary physical memory to the kernel log. --

--- Patch [2]: [PATCH RFC 2/5] mfd: ls2kbmc: sanity check for the connected pci port --- commit cc352456f3958fb7d41378df0f417c2526354011
Author: Miao Wang <[email protected]>

mfd: ls2kbmc: sanity check for the connected pci port

When the bmc resets, the recovery procedure require to reconfigure the parent device. The driver assumes that the parent device should be LS7A. Add a sanity check on initialization to ensure this and prevent from accidentally operating on non-LS7A ports.

Quoted 1 line

mfd: ls2kbmc: sanity check for the connected pci port

[Severity: Low]
Does the commit message description need to be capitalized to match MFD subsystem guidelines?

Quoted 4 lines
drivers/mfd/ls2k-bmc-core.c +0 -0
index 30b275bc48794..39ce5e55ffd6d 100644

[ ... ]

drivers/mfd/ls2k-bmc-core.c static int ls2k_bmc_parse_mode(struct pci_dev *pdev, struct simplefb_platform_da · line 486

Quoted 3 lines
drivers/mfd/ls2k-bmc-core.c +0 -0
@@ -476,6 +485,24 @@ static int ls2k_bmc_parse_mode(struct pci_dev *pdev, struct simplefb_platform_da
 	return -EINVAL;
 }

[Severity: High]
This isn't a bug introduced by this patch, but looking at ls2k_bmc_parse_mode() just above this diff:

drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_parse_mode() {
    ...
    mode_base = devm_ioremap(&pdev->dev, pci_resource_start(pdev, 0) + SZ_16M,
                             sizeof(mode_buf));
    ...
    memcpy(mode_buf, mode_base, sizeof(mode_buf) - 1);
    ...
}

Does this code map and read memory from PCI BAR0 without validating the size of the BAR via pci_resource_len(), potentially leaking arbitrary physical memory to the kernel log if a malformed PCIe device provides a small BAR0?

Also, is it safe to use the standard memcpy() to read from an I/O memory region instead of using memcpy_fromio()?

[Severity: High]
This is a pre-existing issue, but does the interrupt handler unconditionally claim a shared interrupt without checking the hardware status?

drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_interrupt() {
    ...
    if (time_after(jiffies, last_jiffies + LS2K_BMC_INT_INTERVAL)) {
        schedule_work(&ddata->bmc_reset_work);
        last_jiffies = jiffies;
    }
    return IRQ_HANDLED;
}

If the interrupt is requested with IRQF_SHARED, could this break interrupt sharing and cause spurious device resets when another device on the same line triggers an interrupt?

[Severity: High]
This is another pre-existing issue, but it looks like the driver initializes bmc_reset_work and requests an interrupt in ls2k_bmc_init(), but doesn't cancel the work on probe failure or device unbind:

drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_init() {
    ...
    INIT_WORK(&ddata->bmc_reset_work, ls2k_bmc_events_fn);
    ret = devm_request_irq(...);
    ...
}

Since there is no .remove callback in the ls2k_bmc_driver pci_driver struct to cancel the work, could a late interrupt scheduled by this work execute after ddata is freed by devres, leading to a use-after-free?

drivers/mfd/ls2k-bmc-core.c static int ls2k_bmc_parse_mode(struct pci_dev *pdev, struct simplefb_platform_da · line 497

Quoted 10 lines
drivers/mfd/ls2k-bmc-core.c +10 -0
+static struct pci_device_id ls7a_ports[] = {
+	{ PCI_DEVICE(PCI_VENDOR_ID_LOONGSON, DEV_LS7A1K_PCIE_PORT0) },
+	{ PCI_DEVICE(PCI_VENDOR_ID_LOONGSON, DEV_LS7A1K_PCIE_PORT1) },
+	{ PCI_DEVICE(PCI_VENDOR_ID_LOONGSON, DEV_LS7A1K_PCIE_PORT2) },
+	{ PCI_DEVICE(PCI_VENDOR_ID_LOONGSON, DEV_LS7A2K_PCIE_PORT0) },
+	{ PCI_DEVICE(PCI_VENDOR_ID_LOONGSON, DEV_LS7A2K_PCIE_PORT1) },
+	{ PCI_DEVICE(PCI_VENDOR_ID_LOONGSON, DEV_LS7A2K_PCIE_PORT2) },
+	{ PCI_DEVICE(PCI_VENDOR_ID_LOONGSON, DEV_LS7A2K_PCIE_PORT3) },
+	{ }
+};

[Severity: Low]
Can this array be marked const to avoid it being placed in the read-write data section?

drivers/mfd/ls2k-bmc-core.c static int ls2k_bmc_parse_mode(struct pci_dev *pdev, struct simplefb_platform_da · line 503

Quoted 6 lines
drivers/mfd/ls2k-bmc-core.c +6 -0
+
+static bool ls2k_check_parent(struct pci_dev *dev)
+{
+	struct pci_dev *parent = dev->bus->self;
+
+	return pci_match_id(ls7a_ports, parent) != NULL;

[Severity: High]
If the device is attached to a topology without a parent bridge (such as directly to a PCIe root complex in a virtualized environment), could dev->bus->self be NULL? Passing a NULL parent to pci_match_id() would lead to a kernel panic.

drivers/mfd/ls2k-bmc-core.c static int ls2k_bmc_parse_mode(struct pci_dev *pdev, struct simplefb_platform_da · line 508

Quoted 5 lines
drivers/mfd/ls2k-bmc-core.c +2 -0
+}
+
 static int ls2k_bmc_probe(struct pci_dev *dev, const struct pci_device_id *id)
 {
 	struct simplefb_platform_data pd;

[Severity: High]
This isn't a bug introduced by this patch, but does ls2k_bmc_probe() dynamically modify the global ls2k_bmc_cells array with a stack pointer?

drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_probe() {
    struct simplefb_platform_data pd;
    ...
    ls2k_bmc_cells[LS2K_BMC_DISPLAY].platform_data = &pd;
    ls2k_bmc_cells[LS2K_BMC_DISPLAY].pdata_size = sizeof(pd);
    ...
}

Could this cause stack memory corruption and data races if two devices are probed concurrently, leaving a dangling pointer to a destroyed stack frame?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author
#5 lore raw

RFC PATCH 3/5 mfd: ls2kbmc: redraw using exported functions

In reply to Miao Wang

Use update_screen, i.e. redraw_screen() to trigger the redraw of the current vt.

Fixes d952bba3fbb5 ("mfd: ls2kbmc: Add Loongson-2K BMC reset function support")
---
 drivers/mfd/ls2k-bmc-core.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/mfd/ls2k-bmc-core.c +4 -1
index 39ce5e55ffd6d3a578008dd752b3e8dceec9bd81..4ceae6c70fb27ae2d6a2a92f4f45a376f1cdb699 100644
@@ -25,6 +25,7 @@
 #include <linux/platform_device.h>
 #include <linux/stop_machine.h>
 #include <linux/vt_kern.h>
+#include <linux/console.h>
 
 /* LS2K BMC resources */
 #define LS2K_DISPLAY_RES_START		(SZ_16M + SZ_2M)
@@ -310,7 +311,9 @@ static void ls2k_bmc_events_fn(struct work_struct *work)
 
 	if (IS_ENABLED(CONFIG_VT)) {
 		/* Re-push the display due to previous PCI-E loss. */
-		set_console(vt_move_to_console(MAX_NR_CONSOLES - 1, 1));
+		console_lock();
+		update_screen(vc_cons[fg_console].d);
+		console_unlock();
 	}
 }
 
Signature · 1 line
2.49.0
#6 lore raw

Re: [PATCH RFC 3/5] mfd: ls2kbmc: redraw using exported functions

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:

Pre-existing issues:
- [High] A 10-second busy-wait is executed inside a stop_machine callback, causing an uninterruptible system freeze that will trigger hardware lockup watchdogs.
- [High] The scheduled work item is never canceled upon device unbind, leading to a use-after-free vulnerability.
- [High] The probe function assigns a stack variable pointer to a globally shared, dynamically mutated mfd_cell array.
- [High] The recovery callback returns 0 (false) on failure, and the caller ignores the return value, leading to unchecked PCIe errors and system panics.
- [High] Unconditional IRQ_HANDLED return in a shared interrupt handler without source validation.
--

--- Patch [3]: [PATCH RFC 3/5] mfd: ls2kbmc: redraw using exported functions --- commit 2d719e1f4433582637922b9095dc176ee03b5768
Author: Miao Wang <[email protected]>

mfd: ls2kbmc: redraw using exported functions

This commit modifies ls2k_bmc_events_fn to use update_screen to trigger a redraw of the current vt, replacing set_console. This aims to safely restore the display state after a PCIe loss.

drivers/mfd/ls2k-bmc-core.c static void ls2k_bmc_events_fn(struct work_struct *work) · line 315

Quoted 9 lines
drivers/mfd/ls2k-bmc-core.c +2 -0
@@ -310,7 +311,9 @@ static void ls2k_bmc_events_fn(struct work_struct *work)
 
 	if (IS_ENABLED(CONFIG_VT)) {
 		/* Re-push the display due to previous PCI-E loss. */
+		console_lock();
+		update_screen(vc_cons[fg_console].d);

[Severity: High]
This isn't a bug introduced by this patch, but earlier in this function stop_machine is called to execute ls2k_bmc_recover_pci_data:

stop_machine(ls2k_bmc_recover_pci_data, ddata, NULL);

Inside ls2k_bmc_recover_pci_data, the code calls mdelay(LS2K_BMC_RESET_WAIT_TIME) which delays for 10 seconds.

Because stop_machine executes its callback with local interrupts disabled on the executing CPU while all other CPUs spin with interrupts disabled, a 10-second delay is executed in this state.

Could this uninterruptible freeze trigger the kernel's NMI hard lockup watchdog or RCU stall detectors, leading to a panic?

[Severity: High]
This isn't a bug introduced by this patch, but the return value of stop_machine is ignored here before unconditionally calling update_screen.

In ls2k_bmc_recover_pci_data, failure paths return false, which evaluates to 0 (success). If the PCIe link fails to retrain and the failure is ignored, update_screen will still attempt to perform PCIe MMIO reads/writes.

Can accessing MMIO on a down PCIe link cause severe hardware exceptions like an asynchronous external abort or AER panic?

Should we verify the PCIe link is actually recovered before updating the screen?

drivers/mfd/ls2k-bmc-core.c static void ls2k_bmc_events_fn(struct work_struct *work) · line 318

Quoted 3 lines
drivers/mfd/ls2k-bmc-core.c +1 -0
+		console_unlock();
 	}
 }

[Severity: High]
This is a pre-existing issue, but I noticed the bmc_reset_work work item (which executes ls2k_bmc_events_fn) is never canceled on driver unbind.

In ls2k_bmc_probe, the driver dynamically allocates ddata via devm_kzalloc and initializes the work:

drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_init() {
    ...
    INIT_WORK(&ddata->bmc_reset_work, ls2k_bmc_events_fn);
    ...
}

The driver lacks a .remove callback or a devm action to call cancel_work_sync.

When the device is unbound via sysfs, devres automatically frees the ddata structure.

If the work was scheduled right before unbind, will the workqueue dereference the freed ddata pointer when it executes ls2k_bmc_events_fn, causing a use-after-free?

[Severity: High]
This is a pre-existing issue, but looking at how the device is initialized in ls2k_bmc_probe, a stack variable pointer is assigned to the globally shared ls2k_bmc_cells array:

drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_probe() {
    struct simplefb_platform_data pd;
    ...
    ls2k_bmc_cells[LS2K_BMC_DISPLAY].platform_data = &pd;
    ...
}

Because ls2k_bmc_cells is not declared as static const (which violates MFD subsystem rules), the global array permanently retains a dangling pointer to the pd stack frame after probe returns.

If another instance of the device probes, or if the driver unbinds and rebounds, won't this read unmapped or corrupted stack memory?

[Severity: High]
This is a pre-existing issue, but the shared interrupt handler for the BMC unconditionally returns IRQ_HANDLED:

drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_interrupt() {
    ...
    if (time_after(jiffies, last_jiffies + LS2K_BMC_INT_INTERVAL)) {
        schedule_work(&ddata->bmc_reset_work);
        last_jiffies = jiffies;
    }

    return IRQ_HANDLED;
}

Without checking hardware status registers to verify if this specific device generated the interrupt, any other device sharing the IRQ line will cause this handler to falsely claim the interrupt.

Does this behavior starve unrelated devices on the same line and incorrectly schedule the 10-second BMC reset sequence?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author
#7 lore raw

RFC PATCH 4/5 mfd: ls2kbmc: able to compile as a module

In reply to Miao Wang

Make ls2kmc able to be compiled as a module

---
 drivers/mfd/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mfd/Kconfig +1 -1
index 763ce6a34782bdd5d2b1a4d840c75b040092d83e..a7a9f97af248c88489dc1203a1ba05f2ce4827df 100644
@@ -2494,7 +2494,7 @@ config MFD_LOONGSON_SE
 	  which will forward them to the corresponding engine.
 
 config MFD_LS2K_BMC_CORE
-	bool "Loongson-2K Board Management Controller Support"
+	tristate "Loongson-2K Board Management Controller Support"
 	depends on PCI && ACPI_GENERIC_GSI
 	select MFD_CORE
 	help
Signature · 1 line
2.49.0
#8 lore raw

Re: [PATCH RFC 4/5] mfd: ls2kbmc: able to compile as a module

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:

New issues:
- [Medium] A dependent boolean Kconfig symbol silently forces this driver back to built-in, defeating the patch's purpose.
- [Low] The commit message fails to follow the mandatory MFD subsystem capitalization rules.

Pre-existing issues:
- [High] Missing Module Cleanup / Use-After-Free
- [High] Global mfd_cells array is mutated dynamically with stack pointers
- [High] Immediate unregistering of GSI mapping breaks the GPIO interrupt.
- [High] Direct use of memcpy() on __iomem pointers instead of memcpy_fromio(). --

--- Patch [4]: [PATCH RFC 4/5] mfd: ls2kbmc: able to compile as a module --- commit 06b671aceea4e266ddc107c8d7f73d19e48b4e8a
Author: Miao Wang <[email protected]>

mfd: ls2kbmc: able to compile as a module

Make ls2kmc able to be compiled as a module

Quoted 1 line

mfd: ls2kbmc: able to compile as a module

[Severity: Low]
The MFD subsystem rules require the description after the prefix to be capitalized. Could the subject be updated to capitalize "able" (e.g., "mfd: ls2kbmc: Able to compile as a module")?

drivers/mfd/Kconfig config MFD_LOONGSON_SE · line 2497

Quoted 10 lines
drivers/mfd/Kconfig +1 -1
index 763ce6a34782b..a7a9f97af248c 100644
@@ -2494,7 +2494,7 @@ config MFD_LOONGSON_SE
 	  which will forward them to the corresponding engine.
 
 config MFD_LS2K_BMC_CORE
-	bool "Loongson-2K Board Management Controller Support"
+	tristate "Loongson-2K Board Management Controller Support"

[Severity: Medium]
Does this achieve the intended goal if IPMI_LS2K is enabled?

IPMI_LS2K in drivers/char/ipmi/Kconfig is a boolean config that selects MFD_LS2K_BMC_CORE. If a user enables IPMI_LS2K=y, Kconfig will silently force MFD_LS2K_BMC_CORE to be built-in, ignoring the choice to compile as a module.

[Severity: High]
This is a pre-existing issue, but enabling module compilation exposes a missing cleanup path. Looking at drivers/mfd/ls2k-bmc-core.c, ls2k_bmc_init() initializes a work item:

INIT_WORK(&ddata->bmc_reset_work, ls2k_bmc_events_fn);

However, the driver struct ls2k_bmc_driver lacks a .remove callback, and there is no devm action added to cancel this work.

If the module is unloaded while the work is scheduled, will this result in a use-after-free when the work executes and attempts to jump to unmapped module memory?

[Severity: High]
This is a pre-existing issue, but the MFD subsystem requires cell arrays to be static const. In drivers/mfd/ls2k-bmc-core.c, the global array ls2k_bmc_cells is mutated dynamically with stack pointers during ls2k_bmc_probe():

static struct mfd_cell ls2k_bmc_cells[] = {
...
ls2k_bmc_cells[LS2K_BMC_DISPLAY].platform_data = &pd; ls2k_bmc_cells[LS2K_BMC_DISPLAY].pdata_size = sizeof(pd);

Since pd is allocated on the stack, doesn't this leave a dangling pointer in the global array once the probe function returns, potentially leading to memory corruption or data races if multiple devices are probed?

[Severity: High]
This is a pre-existing issue, but does immediate unregistering of the GSI mapping break the GPIO interrupt? In drivers/mfd/ls2k-bmc-core.c, ls2k_bmc_init() calls:

acpi_unregister_gsi(LS2K_BMC_RESET_GPIO_GSI);
return ret;

Since this is called immediately after requesting the IRQ, wouldn't it dispose of the IRQ mapping and cause hardware interrupts to be ignored?

[Severity: High]
This is a pre-existing issue, but is it safe to use standard memcpy() directly on an __iomem pointer? In drivers/mfd/ls2k-bmc-core.c, ls2k_bmc_parse_mode() does:

const void __iomem *mode_base;
...
memcpy(mode_buf, mode_base, sizeof(mode_buf) - 1);

Standard C library routines do not perform hardware-specific I/O barriers.

Should this use memcpy_fromio() instead to prevent I/O access violations?

Quoted 3 lines
drivers/mfd/Kconfig +0 -0
 	depends on PCI && ACPI_GENERIC_GSI
 	select MFD_CORE
 	help
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
author
#9 lore raw

RFC PATCH 5/5 mfd: ls2kbmc: capture the reset event of BMC through GPIO

In reply to Miao Wang

The reset event of BMC is captured through GPIO. However, this driver bypasses the GPIO framework and directly accesses the GPIO controller through the fixed address. When the same GPIO controller is also exposed through ACPI and probed by the corresponding GPIO driver, there would be a conflict between the two drivers.

This patch will try to find the GPIO through declared GPIO pin in the _CRS resources of the ACPI node. If no such delaration is found, the driver will fall back to search for the correct GPIO controller and pin according to the fixed address and pin number. A possible DSDT declaration for the GPIO pin might be as follows:

    Device (BMC0) {
        Name (_ADR, ...) // Match the PCI address of the BMC device
        // \_SB.GPO1 is the ACPI path of the GPIO controller
        Name (_CRS, ResourceTemplate () {
            GpioInt (Edge, ActiveLow, Exclusive, PullNone, 0,
                     "\\_SB.GPO1", 0) {
                14 // 14 is the GPIO pin number
            }
    }
---
 drivers/mfd/ls2k-bmc-core.c | 162 +++++++++++++++++++++++++++++++-------------
 1 file changed, 115 insertions(+), 47 deletions(-)
drivers/mfd/ls2k-bmc-core.c +115 -47
index 4ceae6c70fb27ae2d6a2a92f4f45a376f1cdb699..5005908802766cf855bc48a4d7d73495dea16e9c 100644
@@ -26,6 +26,9 @@
 #include <linux/stop_machine.h>
 #include <linux/vt_kern.h>
 #include <linux/console.h>
+#include <linux/gpio/consumer.h>
+#include <linux/gpio/driver.h>
+#include <linux/gpio.h>
 
 /* LS2K BMC resources */
 #define LS2K_DISPLAY_RES_START		(SZ_16M + SZ_2M)
@@ -81,18 +84,6 @@
 
 #define PCI_REG_STRIDE			0x4
 
-#define LS2K_BMC_RESET_GPIO		14
-#define LOONGSON_GPIO_REG_BASE		0x1FE00500
-#define LOONGSON_GPIO_REG_SIZE		0x18
-#define LOONGSON_GPIO_OEN		0x0
-#define LOONGSON_GPIO_FUNC		0x4
-#define LOONGSON_GPIO_INTPOL		0x10
-#define LOONGSON_GPIO_INTEN		0x14
-
-#define LOONGSON_IO_INT_BASE		16
-#define LS2K_BMC_RESET_GPIO_INT_VEC	(LS2K_BMC_RESET_GPIO % 8)
-#define LS2K_BMC_RESET_GPIO_GSI		(LOONGSON_IO_INT_BASE + LS2K_BMC_RESET_GPIO_INT_VEC)
-
 enum {
 	LS2K_BMC_DISPLAY,
 	LS2K_BMC_IPMI0,
@@ -186,6 +177,7 @@ struct ls2k_bmc_ddata {
 	struct work_struct bmc_reset_work;
 	struct ls2k_bmc_pci_data bmc_pci_data;
 	struct ls2k_bmc_bridge_pci_data bridge_pci_data;
+	struct gpio_desc *reset_gpio;
 };
 
 static bool ls2k_bmc_bar0_addr_is_set(struct pci_dev *pdev)
@@ -334,6 +326,22 @@ static irqreturn_t ls2k_bmc_interrupt(int irq, void *arg)
 	return IRQ_HANDLED;
 }
 
+static irqreturn_t ls2k_bmc_gpio_interrupt(int irq, void *arg)
+{
+	struct ls2k_bmc_ddata *ddata = arg;
+
+	int val = gpiod_get_raw_value(ddata->reset_gpio);
+
+	/*
+	 * The GPIO is active low, so when the value is 0,
+	 * it indicates a reset event.
+	 */
+	if (val == 0)
+		return ls2k_bmc_interrupt(irq, arg);
+
+	return IRQ_NONE;
+}
+
 /*
  * Saves the BMC parent device (LS7A) and its own PCI configuration space registers
  * that need to be restored after BMC reset.
@@ -375,11 +383,83 @@ static void ls2k_bmc_save_pci_data(struct pci_dev *pdev, struct ls2k_bmc_ddata *
 	pci_read_config_dword(pdev, PCI_INTERRUPT_LINE, &ddata->bmc_pci_data.interrupt_line);
 }
 
+static int ls2k_bmc_gpiochip_find(struct gpio_chip *gc, const void *data)
+{
+	struct acpi_device *adev;
+	struct list_head resource_list;
+	struct resource_entry *rentry;
+	struct fwnode_handle *fwnode = gc->fwnode ?: gc->parent->fwnode;
+	phys_addr_t start_addr = (phys_addr_t) data;
+	int ret, found = 0;
+
+	if (!is_acpi_node(fwnode))
+		goto out;
+
+	adev = to_acpi_device_node(fwnode);
+	if (!adev)
+		goto out;
+
+	INIT_LIST_HEAD(&resource_list);
+
+	ret = acpi_dev_get_memory_resources(adev, &resource_list);
+	if (ret < 0)
+		goto out;
+	rentry = list_first_entry_or_null(&resource_list, struct resource_entry, node);
+	if (!rentry)
+		goto free_resource_list;
+	if (rentry->res->start == start_addr)
+		found = 1;
+
+free_resource_list:
+	acpi_dev_free_resource_list(&resource_list);
+out:
+	return found;
+}
+
+static struct gpio_desc *ls2k_bmc_find_gpio(struct ls2k_bmc_ddata *ddata)
+{
+	/*
+	 * In conventional way, the GPIO should be obtained through ACPI or
+	 * device tree. However, when the information is not available,
+	 * we should find the GPIO according to the convention of the server
+	 * boards with LS2K BMC, the gpio signal relelecting the reset event
+	 * of the BMC should be connected to pin 14 of the GPIO input of
+	 * the first CPU node. The address of that GPIO controller is fixed.
+	 */
+	static const phys_addr_t LOONGSON_GPIO_REG_BASE = 0x1FE00500;
+	static const unsigned int LS2K_BMC_RESET_GPIO = 14;
+	struct gpio_desc *desc;
+	unsigned int legacy_gpio;
+	int ret;
+	struct gpio_device *gdev __free(gpio_device_put) = NULL;
+
+	dev_dbg(ddata->dev, "Searching for GPIO chip at address %pa\n", &LOONGSON_GPIO_REG_BASE);
+
+	gdev = gpio_device_find((void *)LOONGSON_GPIO_REG_BASE, ls2k_bmc_gpiochip_find);
+
+	if (!gdev) {
+		dev_dbg(ddata->dev, "cannot find GPIO chip at address %pa, deferring\n",
+			&LOONGSON_GPIO_REG_BASE);
+		return ERR_PTR(-EPROBE_DEFER);
+	}
+
+	desc = gpio_device_get_desc(gdev, LS2K_BMC_RESET_GPIO);
+	if (IS_ERR(desc))
+		return desc;
+
+	// XXX: might be better to use gpiod_request()
+	legacy_gpio = desc_to_gpio(desc);
+	ret = devm_gpio_request_one(ddata->dev, legacy_gpio, GPIOF_IN, dev_name(ddata->dev));
+	if (ret < 0)
+		return ERR_PTR(ret);
+
+	return gpio_to_desc(legacy_gpio);
+}
+
 static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata)
 {
 	struct pci_dev *pdev = to_pci_dev(ddata->dev);
-	void __iomem *gpio_base;
-	int gpio_irq, ret, val;
+	int gpio_irq, ret;
 
 	ls2k_bmc_save_pci_data(pdev, ddata);
 
@@ -392,44 +472,32 @@ static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata)
 		return ret;
 	}
 
-	gpio_base = ioremap(LOONGSON_GPIO_REG_BASE, LOONGSON_GPIO_REG_SIZE);
-	if (!gpio_base)
-		return -ENOMEM;
-
-	/* Disable GPIO output */
-	val = readl(gpio_base + LOONGSON_GPIO_OEN);
-	writel(val | BIT(LS2K_BMC_RESET_GPIO), gpio_base + LOONGSON_GPIO_OEN);
-
-	/* Enable GPIO functionality */
-	val = readl(gpio_base + LOONGSON_GPIO_FUNC);
-	writel(val & ~BIT(LS2K_BMC_RESET_GPIO), gpio_base + LOONGSON_GPIO_FUNC);
-
-	/* Set GPIO interrupts to low-level active */
-	val = readl(gpio_base + LOONGSON_GPIO_INTPOL);
-	writel(val & ~BIT(LS2K_BMC_RESET_GPIO), gpio_base + LOONGSON_GPIO_INTPOL);
-
-	/* Enable GPIO interrupts */
-	val = readl(gpio_base + LOONGSON_GPIO_INTEN);
-	writel(val | BIT(LS2K_BMC_RESET_GPIO), gpio_base + LOONGSON_GPIO_INTEN);
+	ddata->reset_gpio = devm_gpiod_get_index_optional(&pdev->dev, NULL, 0, GPIOD_IN);
+	if (IS_ERR(ddata->reset_gpio)) {
+		ret = PTR_ERR(ddata->reset_gpio);
+		ddata->reset_gpio = NULL;
+		return dev_err_probe(ddata->dev, ret, "Failed to get reset GPIO\n");
+	}
+	if (ddata->reset_gpio == NULL) {
+		ddata->reset_gpio = ls2k_bmc_find_gpio(ddata);
+		if (IS_ERR(ddata->reset_gpio)) {
+			ret = PTR_ERR(ddata->reset_gpio);
+			ddata->reset_gpio = NULL;
+			return dev_err_probe(ddata->dev, ret, "Failed to find reset GPIO\n");
+		}
+	}
 
-	iounmap(gpio_base);
+	gpio_irq = gpiod_to_irq(ddata->reset_gpio);
 
-	/*
-	 * Since gpio_chip->to_irq is not implemented in the Loongson-3 GPIO driver,
-	 * acpi_register_gsi() is used to obtain the GPIO IRQ. The GPIO interrupt is a
-	 * watchdog interrupt that is triggered when the BMC resets.
-	 */
-	gpio_irq = acpi_register_gsi(NULL, LS2K_BMC_RESET_GPIO_GSI, ACPI_EDGE_SENSITIVE,
-				     ACPI_ACTIVE_LOW);
 	if (gpio_irq < 0)
-		return gpio_irq;
+		return dev_err_probe(ddata->dev, gpio_irq, "Failed to get GPIO IRQ\n");
 
-	ret = devm_request_irq(ddata->dev, gpio_irq, ls2k_bmc_interrupt,
-			       IRQF_SHARED | IRQF_TRIGGER_FALLING, "ls2kbmc gpio", ddata);
-	if (ret)
-		dev_err(ddata->dev, "Failed to request LS2KBMC GPIO IRQ %d.\n", gpio_irq);
+	ret = devm_request_irq(&pdev->dev, gpio_irq, ls2k_bmc_gpio_interrupt,
+			       IRQF_SHARED | IRQF_TRIGGER_FALLING, "ls2kbmc reset", ddata);
+
+	if (ret != 0)
+		return dev_err_probe(ddata->dev, ret, "Failed to request GPIO IRQ %d.\n", gpio_irq);
 
-	acpi_unregister_gsi(LS2K_BMC_RESET_GPIO_GSI);
 	return ret;
 }
 
Signature · 1 line
2.49.0
#10 lore raw

Re: [PATCH RFC 5/5] mfd: ls2kbmc: capture the reset event of BMC through GPIO

Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:

New issues:
- [High] Unchecked dereference of gc->parent in ls2k_bmc_gpiochip_find causes a kernel crash if any GPIO chip lacks a parent.
- [High] Reading the instantaneous GPIO line state in an edge-triggered shared interrupt handler causes valid pulse interrupts to be missed and the IRQ to be disabled.
- [Medium] Bypassing logical polarity abstraction by using gpiod_get_raw_value hardcodes physical logic levels.
- [Medium] Calling gpiod_get_raw_value() from a hardirq context may sleep if the GPIO controller is sleepable.
- [Low] The commit message prefix violates MFD subsystem capitalization guidelines.

Pre-existing issues:
- [High] Missing cancel_work_sync() for bmc_reset_work leads to a Use-After-Free during driver unbind or probe failure.
- [High] MFD cell array is dynamically modified and not declared as static const.
--

--- Patch [5]: [PATCH RFC 5/5] mfd: ls2kbmc: capture the reset event of BMC through GPIO --- commit f9d0fc327645db53505282b9d08103e16c85472d
Author: Miao Wang <[email protected]>

mfd: ls2kbmc: capture the reset event of BMC through GPIO

This commit updates the ls2kbmc driver to capture the BMC reset event using the GPIO framework rather than bypassing it via fixed memory addresses. It attempts to find the GPIO pin via ACPI _CRS resources, falling back to a hardcoded GPIO controller address if not found.

Quoted 1 line

mfd: ls2kbmc: capture the reset event of BMC through GPIO

[Severity: Low]
Is the subsystem prefix capitalization correct here? The MFD subsystem guidelines require capitalizing the description after the subsystem prefix.

Quoted 4 lines
drivers/mfd/ls2k-bmc-core.c +0 -0
index 4ceae6c70fb27..5005908802766 100644

[ ... ]

drivers/mfd/ls2k-bmc-core.c static irqreturn_t ls2k_bmc_interrupt(int irq, void *arg) · line 333

Quoted 9 lines
drivers/mfd/ls2k-bmc-core.c +5 -0
@@ -334,6 +326,22 @@ static irqreturn_t ls2k_bmc_interrupt(int irq, void *arg)
 	return IRQ_HANDLED;
 }
 
+static irqreturn_t ls2k_bmc_gpio_interrupt(int irq, void *arg)
+{
+	struct ls2k_bmc_ddata *ddata = arg;
+
+	int val = gpiod_get_raw_value(ddata->reset_gpio);

[Severity: Medium]
Could this sleep in atomic context? Since this interrupt is requested via devm_request_irq() without a thread function, it runs in hardirq context. If the ACPI-provided GPIO is backed by a sleepable controller like an I2C expander, calling gpiod_get_raw_value() here might trigger a gpiolib warning and sleep.

drivers/mfd/ls2k-bmc-core.c static irqreturn_t ls2k_bmc_interrupt(int irq, void *arg) · line 339

Quoted 6 lines
drivers/mfd/ls2k-bmc-core.c +6 -0
+
+	/*
+	 * The GPIO is active low, so when the value is 0,
+	 * it indicates a reset event.
+	 */
+	if (val == 0)

[Severity: Medium]
Does using gpiod_get_raw_value() bypass the logical polarity abstraction? If the hardware revision has different active polarities or the firmware declares it as ActiveLow, hardcoding a check for physical 0 ignores the gpiolib abstraction.

[Severity: High]
Could this miss valid interrupts and cause the IRQ line to be disabled? The interrupt is configured as edge-triggered (IRQF_TRIGGER_FALLING). If the reset is a short pulse, the line might return high by the time this hardirq handler reads the instantaneous value. Returning IRQ_NONE for valid latched events could trigger the kernel's spurious interrupt detection and disable the IRQ.

Quoted 4 lines
drivers/mfd/ls2k-bmc-core.c +4 -0
+		return ls2k_bmc_interrupt(irq, arg);
+
+	return IRQ_NONE;
+}

[ ... ]

drivers/mfd/ls2k-bmc-core.c static void ls2k_bmc_save_pci_data(struct pci_dev *pdev, struct ls2k_bmc_ddata * · line 391

Quoted 10 lines
drivers/mfd/ls2k-bmc-core.c +6 -0
@@ -375,11 +383,83 @@ static void ls2k_bmc_save_pci_data(struct pci_dev *pdev, struct ls2k_bmc_ddata *
 	pci_read_config_dword(pdev, PCI_INTERRUPT_LINE, &ddata->bmc_pci_data.interrupt_line);
 }
 
+static int ls2k_bmc_gpiochip_find(struct gpio_chip *gc, const void *data)
+{
+	struct acpi_device *adev;
+	struct list_head resource_list;
+	struct resource_entry *rentry;
+	struct fwnode_handle *fwnode = gc->fwnode ?: gc->parent->fwnode;

[Severity: High]
Could this cause a NULL pointer dereference? This callback is executed globally over all registered GPIO chips by gpio_device_find(). If any GPIO chip in the system lacks both a fwnode and a parent, evaluating gc->parent->fwnode will crash the kernel.

drivers/mfd/ls2k-bmc-core.c static void ls2k_bmc_save_pci_data(struct pci_dev *pdev, struct ls2k_bmc_ddata * · line 393

Quoted 2 lines
drivers/mfd/ls2k-bmc-core.c +2 -0
+	phys_addr_t start_addr = (phys_addr_t) data;
+	int ret, found = 0;

[ ... ]

[Severity: High]
This isn't a bug introduced by this patch, but is it possible for bmc_reset_work to cause a use-after-free?

The work is initialized in ls2k_bmc_init() and queued in the interrupt handlers, but it doesn't appear to be canceled with cancel_work_sync() during driver unbind or probe failure. If an interrupt fires right before unbind, devres will free ddata, leaving the pending work to access freed memory when it executes.

[Severity: High]
This is a pre-existing issue, but does ls2k_bmc_probe() modify the global ls2k_bmc_cells array using a stack variable?

ls2k_bmc_cells[LS2K_BMC_DISPLAY].platform_data = &pd;

If multiple devices probe concurrently, they could overwrite each other's pointers. This also violates the MFD subsystem rule that cell arrays must be static const to prevent race conditions and dangling pointers.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5