mfd: ls2kbmc: multiple fixes for this driver
Subsystems (4)
v4 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.
---
Changes in v4:
- Use a better way to get the GPIO device fwnode.
- Add a comment to describe a problem found by AI bot which is actually intended.
- Link to v3: https://lore.kernel.org/r/[email protected]
Changes in v3:
- Check the return value of devm_add_action_or_reset when registering
the cleanup hook of the work queue
- Use swnode to create the link between the device to the GPIO chip,
and prevent borrowing the legacy GPIO APIs
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Several fixes suggested by the Sashiko AI review bot
- Add a cleanup function for the wq on removal of the device
- Relax the reverse dependency from CONFIG_IPMI_LS2K to CONFIG_MFD_LS2K_BMC_CORE to allow the driver to be built as a module
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Miao Wang (7):
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: Cancel the work queue on removal
ipmi: ls2k: Relax the dependency to its mfd driver
mfd: ls2kbmc: Able to be compiled as a module
mfd: ls2kbmc: Capture the reset event of BMC through GPIO
drivers/char/ipmi/Kconfig | 2 +- drivers/mfd/Kconfig | 2 +- drivers/mfd/ls2k-bmc-core.c | 240 ++++++++++++++++++++++++++++++++++---------- 3 files changed, 189 insertions(+), 55 deletions(-)
---
base-commit: 11028ab62899e4191e074ee364c712b77823a9c4
change-id: 20260626-ls2kbmc-mod-5209193009b2
Best regards,
Signature · 1 line
Miao Wang <[email protected]>
RFC PATCH v4 1/7 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.
--- drivers/mfd/ls2k-bmc-core.c | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-)
drivers/mfd/ls2k-bmc-core.c +25 -5
@@ -427,34 +427,54 @@ 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 64 bytes is enough for the resolution string */ +char mode_buf[64 ], mode_buf_orig[64 ]; +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 =ioremap (pci_resource_start (pdev,0 ) +SZ_16M , +sizeof (mode_buf)); +if (!mode_base)return -ENOMEM ; +memcpy_fromio (mode_buf, mode_base,sizeof (mode_buf) -1 ); + mode_buf[sizeof (mode_buf) -1 ] ='\0' ; +iounmap ((void __iomem *)mode_base); +memcpy (mode_buf_orig, mode_buf,sizeof (mode_buf_orig));/* 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); + ret =kstrtouint (strsep (&mode,"x" ),10 , &pd->width);if (ret)return ret; +if (mode ==NULL ) + goto invalid_mode; - ret =kstrtoint (strsep (&mode,"-" ),10 , &pd->height); + ret =kstrtouint (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_orig); +return -EINVAL ; }static int ls2k_bmc_probe (struct pci_dev *dev,const struct pci_device_id *id)
Signature · 1 line
2.49.0
[email protected]
AI reviewRe: [PATCH RFC v4 1/7] mfd: ls2kbmc: Make a copy when parsing mode string
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:
New issues:
- [High] The patch introduces unintentional strict validation on the BMC mode string format, breaking backward compatibility for previously valid strings.
Pre-existing issues:
- [Critical] Massive busy-wait delays inside atomic stop_machine context.
- [High] The ACPI GSI mapping is destroyed immediately after being requested, breaking the interrupt and creating an unbalanced lifecycle.
- [High] The driver modifies a global static struct mfd_cell array during probe, violating MFD subsystem guidelines and creating a concurrency race condition.
- [High] Unconditional IRQ_HANDLED in shared interrupt handler breaks interrupt sharing.
--
--- Patch [1]: [PATCH RFC v4 1/7] mfd: ls2kbmc: Make a copy when parsing mode string --- commit 08b5951096b7b6051b970b868f431e24b8983d1a
Author: Miao Wang <[email protected]>
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.
Quoted 4 lines
drivers/mfd/ls2k-bmc-core.c +0
-0
[ ... ]
Quoted 4 lines
drivers/mfd/ls2k-bmc-core.c +0
-0@@ -427,34 +427,54 @@ 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) {
[ ... ]
drivers/mfd/ls2k-bmc-core.c static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata) · line 450
Quoted 5 lines
drivers/mfd/ls2k-bmc-core.c +2
-0/* The resolution field starts with the flag "video=". */ if (!strncmp (mode,"video=" ,6 )) mode = mode +6 ; +else + goto invalid_mode;
[Severity: High]
Will this make the "video=" prefix mandatory, causing valid strings that previously worked without this prefix to be rejected?
Quoted 10 lines
drivers/mfd/ls2k-bmc-core.c +4
-2- ret =kstrtoint (strsep (&mode,"x" ),10 , &pd->width); + ret =kstrtouint (strsep (&mode,"x" ),10 , &pd->width);if (ret)return ret; +if (mode ==NULL ) + goto invalid_mode; - ret =kstrtoint (strsep (&mode,"-" ),10 , &pd->height); + ret =kstrtouint (strsep (&mode,"-" ),10 , &pd->height);
drivers/mfd/ls2k-bmc-core.c static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata) · line 468
Quoted 10 lines
drivers/mfd/ls2k-bmc-core.c +4
-0if (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]
Is it possible that this rejects strings missing an explicit refresh rate (e.g., "@60")?
Since strsep() sets mode to NULL when the delimiter is absent, these new checks could force the string to have an "@", rejecting valid fbdev configurations that worked previously.
[ ... ]
[Severity: High]
This is a pre-existing issue, but the ACPI GSI mapping appears to be destroyed immediately after being requested in ls2k_bmc_init():
drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_init() {
...
acpi_unregister_gsi(LS2K_BMC_RESET_GPIO_GSI);
return ret;
}Can this unconditional unregistration break the hardware interrupt mapping just acquired, preventing the interrupt from ever reaching the driver handler?
[Severity: High]
This isn't a bug introduced by this patch, but the driver modifies a shared static array during probe:
drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_probe() {
...
ls2k_bmc_cells[LS2K_BMC_DISPLAY].platform_data = &pd;
ls2k_bmc_cells[LS2K_BMC_DISPLAY].pdata_size = sizeof(pd);
...
}Could writing a stack pointer (&pd) to a global variable violate MFD subsystem guidelines?
If multiple instances of this driver were probed concurrently, they might race to overwrite the shared .platform_data pointer, causing a use-after-free during platform device registration.
[Severity: High]
This is a pre-existing issue, but the shared interrupt handler seems to unconditionally return IRQ_HANDLED:
drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_interrupt() {
...
return IRQ_HANDLED;
}Since the driver requests its interrupts with the IRQF_SHARED flag, does returning IRQ_HANDLED without verifying interrupt ownership break interrupt sharing and unconditionally claim all interrupts on the shared line?
[Severity: Critical]
This is a pre-existing issue, but there appear to be massive busy-wait delays inside an atomic stop_machine context:
drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_events_fn() {
...
stop_machine(ls2k_bmc_recover_pci_data, ddata, NULL);
...
}drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_recover_pci_data() {
...
/* Waiting for U-Boot and DDR ready */
mdelay(LS2K_BMC_RESET_WAIT_TIME);
...
}Stop_machine() freezes all CPUs and disables interrupts globally.
Could spinning for 10 seconds inside ls2k_bmc_recover_pci_data() cause system lockups and trigger NMI watchdogs?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
RFC PATCH v4 2/7 mfd: ls2kbmc: Sanity check for the connected pci port
In reply to Miao WangWhen 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.
--- drivers/mfd/ls2k-bmc-core.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+)
drivers/mfd/ls2k-bmc-core.c +32 -0
@@ -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 */
@@ -477,6 +486,24 @@ static int ls2k_bmc_parse_mode(struct pci_dev *pdev, struct simplefb_platform_dareturn -EINVAL ; } +static const 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 parent &&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;
@@ -488,6 +515,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
[email protected]
AI reviewRe: [PATCH RFC v4 2/7] mfd: ls2kbmc: Sanity check for the connected pci port
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [Critical] The driver executes a 10-second busy wait (mdelay) inside a stop_machine context, stalling all CPUs and likely triggering the NMI watchdog.
- [High] The driver dynamically modifies a global non-const mfd_cell array and assigns a pointer to a local stack variable to its platform_data field. --
--- Patch [2]: [PATCH RFC v4 2/7] mfd: ls2kbmc: Sanity check for the connected pci port --- commit ec9f316b181d1a4fc9b0daee3548a4f8a40ef766
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. Add a sanity check on initialization to ensure this and prevent from accidentally operating on non-LS7A ports.
Quoted 4 lines
drivers/mfd/ls2k-bmc-core.c +0
-0
[ ... ]
drivers/mfd/ls2k-bmc-core.c static int ls2k_bmc_probe(struct pci_dev *dev, const struct pci_device_id *id) · line 523
Quoted 10 lines
drivers/mfd/ls2k-bmc-core.c +5
-0@@ -488,6 +515,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 );
[Severity: High]
This isn't a bug introduced by this patch, but looking further down in ls2k_bmc_probe(), does assigning a pointer to a local stack variable to the platform_data field of a global mfd_cell array create a dangling pointer once the probe function returns?
drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_probe() {
...
ls2k_bmc_cells[LS2K_BMC_DISPLAY].platform_data = &pd;
ls2k_bmc_cells[LS2K_BMC_DISPLAY].pdata_size = sizeof(pd);
...
}This modifies a global non-const mfd_cell array, which violates MFD subsystem guidelines requiring mfd_cell arrays to be static const. If multiple devices bind to this driver, could concurrent probes race to modify this global array and cause the wrong platform data to be copied?
[Severity: Critical]
This is a pre-existing issue, but since the commit message mentions the recovery procedure, does the 10-second mdelay() inside the recovery procedure's stop_machine callback stall all CPUs and trigger the NMI hardlockup watchdog?
The hardware triggers a BMC reset, which queues the work that calls stop_machine():
drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_events_fn() {
...
stop_machine(ls2k_bmc_recover_pci_data, ddata, NULL);
...
}Inside the callback, the code executes a 10-second busy wait:
drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_recover_pci_data() {
...
mdelay(LS2K_BMC_RESET_WAIT_TIME);
...
}Because stop_machine() halts all other CPUs with interrupts disabled, and the executing CPU also runs with local interrupts disabled, will this 10-second delay cause a kernel panic?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
RFC PATCH v4 3/7 mfd: ls2kbmc: Redraw using exported functions
In reply to Miao WangUse update_screen, i.e. redraw_screen() to trigger the redraw of the current vt.
--- drivers/mfd/ls2k-bmc-core.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/mfd/ls2k-bmc-core.c +4 -1
@@ -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
[email protected]
AI reviewRe: [PATCH RFC v4 3/7] 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] The global static mfd_cell array is dynamically mutated with a stack pointer, violating MFD rules and introducing a data race.
- [High] The bmc_reset_work workqueue item is never canceled on driver unbind, leading to a Use-After-Free.
- [High] acpi_unregister_gsi() is unconditionally called immediately after successfully requesting the GPIO IRQ, destroying the mapping.
- [High] The workqueue ignores the failure of PCI-E recovery and proceeds to update the screen unconditionally, which can cause a kernel crash.
- [Medium] Untrusted resolution parameters read from the BMC PCI memory can cause an integer overflow when calculating the framebuffer stride. --
--- Patch [3]: [PATCH RFC v4 3/7] mfd: ls2kbmc: Redraw using exported functions --- commit 8920bccc1429d53315723d84c6b17a6efd535a5b
Author: Miao Wang <[email protected]>
mfd: ls2kbmc: Redraw using exported functions
Use update_screen, i.e. redraw_screen() to trigger the redraw of the current vt.
Quoted 4 lines
drivers/mfd/ls2k-bmc-core.c +0
-0
[ ... ]
drivers/mfd/ls2k-bmc-core.c static void ls2k_bmc_events_fn(struct work_struct *work) · line 315
Quoted 7 lines
drivers/mfd/ls2k-bmc-core.c +2
-1@@ -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);
[Severity: High]
This isn't a bug introduced by this patch, but does this blindly access the framebuffer without ensuring the pcie link is actually connected?
Looking at ls2k_bmc_recover_pci_data() in drivers/mfd/ls2k-bmc-core.c, the failure paths return false (which evaluates to 0, or success). This masks the failure from stop_machine(). Then in ls2k_bmc_events_fn(), the return value of stop_machine() is entirely ignored.
If the pcie link recovery fails, won't calling update_screen() here access device memory over a disconnected pcie link, triggering a master abort and leading to a fatal kernel panic?
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 does this driver mutate a global mfd_cell array with a stack pointer?
In drivers/mfd/ls2k-bmc-core.c, ls2k_bmc_probe() modifies the shared global array:
ls2k_bmc_probe() {
...
ls2k_bmc_cells[LS2K_BMC_DISPLAY].platform_data = &pd;
...
}If multiple device instances are probed concurrently, won't they corrupt each other's pointers in the global array ls2k_bmc_cells? Furthermore, after the probe function returns, doesn't the global array retain a dangling pointer to the destroyed stack frame? The mfd subsystem also requires that mfd_cell arrays must be declared as static const.
[Severity: High]
This isn't a bug introduced by this patch, but is the bmc_reset_work workqueue item ever canceled on driver unbind?
The work struct is initialized in ls2k_bmc_init() in drivers/mfd/ls2k-bmc-core.c:
INIT_WORK(&ddata->bmc_reset_work, ls2k_bmc_events_fn);
However, the pci_driver struct ls2k_bmc_driver lacks a .remove callback, and there is no devm_add_action() hook to call cancel_work_sync().
If the work is scheduled just before the interrupt is freed during device unbind, wouldn't the workqueue execute ls2k_bmc_events_fn() and dereference the already freed ddata, causing a use-after-free?
[Severity: High]
This is a pre-existing issue, but is the active interrupt mapping unconditionally destroyed during initialization?
In ls2k_bmc_init() in drivers/mfd/ls2k-bmc-core.c, acpi_unregister_gsi() is called unconditionally after requesting the interrupt:
acpi_unregister_gsi(LS2K_BMC_RESET_GPIO_GSI);
return ret;Does this execute irq_dispose_mapping() and tear down the interrupt mapping while the interrupt is still actively requested, rendering it completely non-functional?
[Severity: Medium]
This isn't a bug introduced by this patch, but could untrusted resolution parameters read from the bmc pci memory cause an integer overflow?
In ls2k_bmc_parse_mode() in drivers/mfd/ls2k-bmc-core.c, the parameters are read and used in 32-bit arithmetic:
pd->stride = pd->width * depth / 8;
Since the resolution string is parsed using kstrtouint() without bounds checking, couldn't large input values (such as width = 0x40000000) overflow the multiplication? This would result in a tiny or zero stride being passed to the simplefb driver.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
RFC PATCH v4 4/7 mfd: ls2kbmc: Cancel the work queue on removal
In reply to Miao WangWhen the device is being removeed, the work queue should be canceled to avoid any pending work to be executed after the device is removed.
--- drivers/mfd/ls2k-bmc-core.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
drivers/mfd/ls2k-bmc-core.c +10 -0
@@ -375,6 +375,12 @@ 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 void ls2k_bmc_cancel_wq (void *data) +{ +struct ls2k_bmc_ddata *ddata = data; + (void )cancel_work_sync (&ddata->bmc_reset_work); +} +static int ls2k_bmc_init (struct ls2k_bmc_ddata *ddata) {struct pci_dev *pdev =to_pci_dev (ddata->dev);
@@ -385,6 +391,10 @@ static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata)INIT_WORK (&ddata->bmc_reset_work, ls2k_bmc_events_fn); + ret =devm_add_action_or_reset (ddata->dev, ls2k_bmc_cancel_wq, ddata); +if (ret) +return ret; + ret =devm_request_irq (&pdev->dev, pdev->irq, ls2k_bmc_interrupt,IRQF_SHARED |IRQF_TRIGGER_FALLING ,"ls2kbmc pcie" , ddata);if (ret) {
Signature · 1 line
2.49.0
[email protected]
AI reviewRe: [PATCH RFC v4 4/7] mfd: ls2kbmc: Cancel the work queue on removal
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Pre-existing issues:
- [High] The driver mutates a global static mfd_cell array with a stack-allocated platform data pointer, violating MFD rules and causing a race condition and potential use-after-free.
- [High] acpi_unregister_gsi() is unconditionally called immediately after successfully requesting the GPIO interrupt, destroying the active IRQ mapping.
- [High] A 10-second busy-wait (mdelay) is executed inside stop_machine(), which will reliably trigger the NMI watchdog and panic or freeze the system. --
--- Patch [4]: [PATCH RFC v4 4/7] mfd: ls2kbmc: Cancel the work queue on removal --- commit c84fa18464ab08c1c1ddbc9fda0a717e97dc86d4
Author: Miao Wang <[email protected]>
Subject: mfd: ls2kbmc: Cancel the work queue on removal
When the device is being removed, the work queue should be canceled to avoid any pending work to be executed after the device is removed.
Quoted 18 lines
drivers/mfd/ls2k-bmc-core.c +6
-0@@ -375,6 +375,12 @@ 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 void ls2k_bmc_cancel_wq (void *data) +{ +struct ls2k_bmc_ddata *ddata = data; + (void )cancel_work_sync (&ddata->bmc_reset_work); +} +static int ls2k_bmc_init (struct ls2k_bmc_ddata *ddata) {struct pci_dev *pdev =to_pci_dev (ddata->dev);
drivers/mfd/ls2k-bmc-core.c static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata) · line 392
Quoted 2 lines
drivers/mfd/ls2k-bmc-core.c +0
-0@@ -385,6 +391,10 @@ static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata)INIT_WORK (&ddata->bmc_reset_work, ls2k_bmc_events_fn);
[Severity: High]
This isn't a bug introduced by this patch, but looking at ls2k_bmc_events_fn():
stop_machine(ls2k_bmc_recover_pci_data, ddata, NULL);
Which then initiates a highly atomic context where all CPUs are halted, and ls2k_bmc_recover_pci_data() executes:
mdelay(LS2K_BMC_RESET_WAIT_TIME);
Does a 10-second busy-wait inside stop_machine() trigger the NMI watchdog and freeze the system, since it spins with local interrupts disabled on all CPUs?
drivers/mfd/ls2k-bmc-core.c static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata) · line 400
Quoted 8 lines
drivers/mfd/ls2k-bmc-core.c +4
-0+ ret =devm_add_action_or_reset (ddata->dev, ls2k_bmc_cancel_wq, ddata); +if (ret) +return ret; + ret =devm_request_irq (&pdev->dev, pdev->irq, ls2k_bmc_interrupt,IRQF_SHARED |IRQF_TRIGGER_FALLING ,"ls2kbmc pcie" , ddata);if (ret) {
[Severity: High]
This isn't a bug introduced by this patch, but later in ls2k_bmc_init(), acpi_unregister_gsi() is unconditionally called:
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); acpi_unregister_gsi(LS2K_BMC_RESET_GPIO_GSI); return ret; }
Since this executes unconditionally on the success path after requesting the IRQ, does it destroy the active IRQ mapping? When the driver is unbound, will devm's automated free_irq() then operate on a disposed IRQ descriptor?
[Severity: High]
This isn't a bug introduced by this patch, but looking at ls2k_bmc_probe():
ls2k_bmc_cells[LS2K_BMC_DISPLAY].platform_data = &pd; ls2k_bmc_cells[LS2K_BMC_DISPLAY].pdata_size = sizeof(pd);
Does mutating the global static ls2k_bmc_cells array with a stack-allocated platform data pointer (&pd) leave a dangling pointer in global state after the probe returns?
Could this also cause a data race if multiple device instances probe concurrently? MFD subsystem rules require mfd_cell arrays to be static const, and dynamic platform data should not be passed this way.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
RFC PATCH v4 5/7 ipmi: ls2k: Relax the dependency to its mfd driver
In reply to Miao WangThere is no strong dependency between the IPMI driver and its mfd driver. Although the IPMI driver will not work without the mfd driver, it is not a hard dependency. The IPMI driver can actually be compiled without the mfd driver, and it will just fail to probe. When the mfd driver is loaded, the IPMI driver will probe successfully. Therefore, the dependency of the IPMI driver on its mfd driver should be relaxed to "imply" from "select". This will allow the mfd driver to be compiled as a module and the IPMI driver to be compiled as a part of the ipmi_si module.
--- drivers/char/ipmi/Kconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/char/ipmi/Kconfig +1 -1
@@ -87,7 +87,7 @@ config IPMI_IPMB config IPMI_LS2K bool 'Loongson-2K IPMI interface' depends on LOONGARCH - select MFD_LS2K_BMC_CORE + imply MFD_LS2K_BMC_CORE help Provides a driver for Loongson-2K IPMI interfaces.
Signature · 1 line
2.49.0
[email protected]
AI reviewRe: [PATCH RFC v4 5/7] ipmi: ls2k: Relax the dependency to its mfd driver
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [Medium] The commit message incorrectly claims the Kconfig change allows the MFD driver to be compiled as a module, but the target config is a boolean.
Pre-existing issues:
- [High] The MFD core driver dynamically modifies a non-const global mfd_cell array with a stack-allocated pointer during probe. --
--- Patch [5]: [PATCH RFC v4 5/7] ipmi: ls2k: Relax the dependency to its mfd driver --- commit df8dbca48cc9588013370bfdb3946a4662d09444
Author: Miao Wang <[email protected]>
ipmi: ls2k: Relax the dependency to its mfd driver
There is no strong dependency between the IPMI driver and its mfd driver. Although the IPMI driver will not work without the mfd driver, it is not a hard dependency. The IPMI driver can actually be compiled without the mfd driver, and it will just fail to probe. When the mfd driver is loaded, the IPMI driver will probe successfully.
Quoted 5 lines
Therefore,
the dependency of the IPMI driver on its mfd driver should be relaxed to "imply" from "select". This will allow the mfd driver to be compiled as a module and the IPMI driver to be compiled as a part of theipmi_simodule.
[Severity: Medium]
Is this description accurate regarding the mfd driver being compiled as a module?
Looking at drivers/mfd/Kconfig, MFD_LS2K_BMC_CORE is defined as a bool rather than a tristate:
config MFD_LS2K_BMC_CORE
bool "Loongson-2K Board Management Controller Support"
Since it is a bool, it can only evaluate to built-in or disabled, so it cannot be compiled as a module.
drivers/char/ipmi/Kconfig +1 -1
@@ -87,7 +87,7 @@ config IPMI_IPMB config IPMI_LS2K bool 'Loongson-2K IPMI interface' depends on LOONGARCH - select MFD_LS2K_BMC_CORE + imply MFD_LS2K_BMC_CORE
[Severity: High]
This is a pre-existing issue, but does mutating the global mfd_cell array during probe create a dangling pointer and potential data race?
Looking at ls2k_bmc_probe() in drivers/mfd/ls2k-bmc-core.c, a local stack variable is assigned to the platform_data pointer of the global array:
drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_probe() {
...
ls2k_bmc_cells[LS2K_BMC_DISPLAY].platform_data = &pd;
...
}The array itself is declared as static instead of static const:
static struct mfd_cell ls2k_bmc_cells[] = {
...
};While devm_mfd_add_devices() deep-copies the platform data, this leaves a dangling pointer to a destroyed stack frame in the global array after the probe function returns.
If multiple such devices are probed concurrently, could this also result in a data race on the shared global array?
help
Provides a driver for Loongson-2K IPMI interfaces.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
RFC PATCH v4 6/7 mfd: ls2kbmc: Able to be compiled as a module
In reply to Miao WangMake ls2kmc able to be compiled as a module
--- drivers/mfd/Kconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mfd/Kconfig +1 -1
@@ -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
[email protected]
AI reviewRe: [PATCH RFC v4 6/7] mfd: ls2kbmc: Able to be compiled as a module
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
Pre-existing issues:
- [High] System Hang / Watchdog Panic due to 13+ second mdelay() in stop_machine()
- [High] Broken hardware interrupt due to immediate GSI unregistration
- [Medium] MFD Subsystem Violation: Dynamic modification of a global static MFD cell array
- [Medium] Concurrency Bug: Interrupt handler uses a static local variable for rate-limiting --
--- Patch [6]: [PATCH RFC v4 6/7] mfd: ls2kbmc: Able to be compiled as a module --- commit 25dc6cbaebb11df8a5a5578847da95bd91aa28f3
Author: Miao Wang <[email protected]>
mfd: ls2kbmc: Able to be compiled as a module
Make ls2kmc able to be compiled as a module
drivers/mfd/Kconfig config MFD_LOONGSON_SE · line 2500
Quoted 13 lines
drivers/mfd/Kconfig +1
-1@@ -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
Since this patch allows the driver to be compiled as a module, it exposes several pre-existing issues in drivers/mfd/ls2k-bmc-core.c.
drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_events_fn() {
...
stop_machine(ls2k_bmc_recover_pci_data, ddata, NULL);
...
}drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_recover_pci_data() {
...
mdelay(LS2K_BMC_RESET_WAIT_TIME);
...
}[Severity: High]
This is a pre-existing issue, but does this trigger a watchdog panic? Calling mdelay() for a long time inside a stop_machine() callback halts all CPUs with local interrupts disabled. Can this destroy system latency, RCU state, and inevitably trigger a kernel panic due to the Hard Lockup Detector?
drivers/mfd/ls2k-bmc-core.c:
...
static struct mfd_cell ls2k_bmc_cells[] = {
[LS2K_BMC_DISPLAY] = {
...drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_probe() {
...
ls2k_bmc_cells[LS2K_BMC_DISPLAY].platform_data = &pd;
...
}[Severity: Medium]
This isn't a bug introduced by this patch, but does this dynamically modify a global static array using a local stack variable? The MFD subsystem mandates that mfd_cell arrays must be static const.
Modifying the global array with a stack pointer leaves a dangling pointer after the probe returns, and introduces a race condition if multiple devices are probed concurrently.
drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_init() {
...
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);
acpi_unregister_gsi(LS2K_BMC_RESET_GPIO_GSI);
return ret;
}[Severity: High]
This is a pre-existing issue, but does this immediately tear down the hardware interrupt mapping on the success path? Unregistering the GSI calls irq_dispose_mapping(), which destroys the mapping between the hardware interrupt and the Linux IRQ number.
Will this prevent the interrupt from ever firing?
drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_interrupt() {
...
struct ls2k_bmc_ddata *ddata = arg;
static unsigned long last_jiffies;
if (system_state != SYSTEM_RUNNING)
return IRQ_HANDLED;
/* Skip interrupt in LS2K_BMC_INT_INTERVAL */
if (time_after(jiffies, last_jiffies + LS2K_BMC_INT_INTERVAL)) {
schedule_work(&ddata->bmc_reset_work);
last_jiffies = jiffies;
}
...
}[Severity: Medium]
This is a pre-existing issue, but does this use shared state across all device instances to rate-limit interrupts globally instead of per-device?
If multiple BMC devices exist in a system, an interrupt on one device will erroneously lock out interrupt handling on all other BMC devices for the interval. Can this state be tracked within the device-specific private structure instead?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
RFC PATCH v4 7/7 mfd: ls2kbmc: Capture the reset event of BMC through GPIO
In reply to Miao WangThe 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 | 163 +++++++++++++++++++++++++++++++------------- 1 file changed, 116 insertions(+), 47 deletions(-)
drivers/mfd/ls2k-bmc-core.c +116 -47
@@ -26,6 +26,10 @@#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/property.h> +#include <linux/gpio/machine.h> /* LS2K BMC resources */ #define LS2K_DISPLAY_RES_START (SZ_16M + SZ_2M)
@@ -81,18 +85,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 +178,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)
@@ -375,6 +368,91 @@ 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 struct fwnode_handle *gpio_dev_get_fwnode (struct gpio_device *gdev) +{ +struct device *gdev_dev; + + gdev_dev =gpio_device_to_device (gdev); +return dev_fwnode (gdev_dev); +} + +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 =gpio_dev_get_fwnode (gc->gpiodev); +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; +/* +* ACPI memory resources are ordered and only the first one is +* considered by the driver of the expected GPIO controller. So +* here we also only check the first one to see if it matches the +* expected address. +*/ + 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 reflecting 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 ; +int ret; + struct gpio_device *gdev __free(gpio_device_put) = NULL; +struct property_entry ls2k_bmc_swnode_properties[2 ] = {0 }; + +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 ); + } + + ls2k_bmc_swnode_properties[0 ] =PROPERTY_ENTRY_GPIO ("gpio" , +gpio_dev_get_fwnode (gdev),LS2K_BMC_RESET_GPIO ,GPIO_ACTIVE_HIGH ); + + ret =device_create_managed_software_node (ddata->dev, ls2k_bmc_swnode_properties,NULL ); +if (ret) { +dev_err (ddata->dev,"Failed to create software node for GPIO reset: %d\n" , ret); +return ERR_PTR (ret); + } + +return devm_gpiod_get_index (ddata->dev,NULL ,0 ,GPIOD_IN ); +} +static void ls2k_bmc_cancel_wq (void *data) {struct ls2k_bmc_ddata *ddata = data;
@@ -384,8 +462,7 @@ static void ls2k_bmc_cancel_wq(void *data)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);
@@ -402,44 +479,36 @@ 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 GPIO pin for reset signal\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 GPIO pin for reset signal\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 IRQ for GPIO reset signal input\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_interrupt, +IRQF_SHARED |IRQF_TRIGGER_FALLING ,"ls2kbmc reset" , ddata); + +if (ret !=0 ) +return dev_err_probe (ddata->dev, ret, +"Failed to request IRQ %d for GPIO reset signal input.\n" , + gpio_irq); -acpi_unregister_gsi (LS2K_BMC_RESET_GPIO_GSI );return ret; }
Signature · 1 line
2.49.0
[email protected]
AI reviewRe: [PATCH RFC v4 7/7] mfd: ls2kbmc: Capture the reset event of BMC through GPIO
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [High] Dynamic platform data pointer assigned to a shared static MFD cell array.
- [High] Data race and cross-device interference on static last_jiffies variable in the interrupt handler. --
--- Patch [7]: [PATCH RFC v4 7/7] mfd: ls2kbmc: Capture the reset event of BMC through GPIO --- commit a8353dcca0807b3f06c4230ec30411f5eac5f0e8
Author: Miao Wang <[email protected]>
mfd: ls2kbmc: Capture the reset event of BMC through GPIO
This commit updates the driver to capture the BMC reset event through the GPIO framework instead of bypassing it. It attempts to find the GPIO pin via ACPI resources, and falls back to a hardcoded controller and pin if an ACPI declaration is not found.
drivers/mfd/ls2k-bmc-core.c
Quoted 4 lines
drivers/mfd/ls2k-bmc-core.c +0
-0
[ ... ]
[Severity: High]
This isn't a bug introduced by this patch, but assigning dynamic platform data to a statically allocated global MFD cell array in ls2k_bmc_probe() seems unsafe:
drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_probe() {
...
struct simplefb_platform_data pd;
...
ret = ls2k_bmc_parse_mode(dev, &pd);
if (ret)
return ret;
ls2k_bmc_cells[LS2K_BMC_DISPLAY].platform_data = &pd;
ls2k_bmc_cells[LS2K_BMC_DISPLAY].pdata_size = sizeof(pd);
...
}This violates the MFD subsystem API contract which requires mfd_cell arrays to be static const. Since ls2k_bmc_cells is shared across all instances of the driver, concurrent probing of multiple PCI devices will cause a data race where one thread overwrites the platform_data pointer of the other.
Furthermore, pd is a local stack variable, meaning the global array retains a dangling pointer after ls2k_bmc_probe() returns.
Should the cells array be defined as static const and the platform data be passed using a dynamically allocated copy or another mechanism?
[Severity: High]
This is a pre-existing issue, but I noticed a data race in how last_jiffies is defined in ls2k_bmc_interrupt():
drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_interrupt() {
...
static unsigned long last_jiffies;
if (system_state != SYSTEM_RUNNING)
return IRQ_HANDLED;
/* Skip interrupt in LS2K_BMC_INT_INTERVAL */
if (time_after(jiffies, last_jiffies + LS2K_BMC_INT_INTERVAL)) {
schedule_work(&ddata->bmc_reset_work);
last_jiffies = jiffies;
}
...
}Because this interrupt handler is registered with IRQF_SHARED and last_jiffies is a shared static variable, concurrent interrupts on different CPUs or multiple BMC devices will cause a data race.
Could this static state be moved to the per-device struct ls2k_bmc_ddata structure to prevent cross-device interference and ensure proper isolation?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7