Skip to content

RP1: fix remove and re-probe in clk-rp1, pwm-rp1, rp1-adc and raspberrypi-gpiomem - #7785

Open
joshuacov1 wants to merge 5 commits into
raspberrypi:rpi-6.18.yfrom
joshuacov1:rp1-fixes
Open

joshuacov1 wants to merge 5 commits into
raspberrypi:rpi-6.18.yfrom
joshuacov1:rp1-fixes

Conversation

@joshuacov1

Copy link
Copy Markdown

Unbinding and binding the RP1 PCI device again (rp1_pci, drivers/misc/rp1) removes and re-probes all RP1 peripheral drivers. Several of them do not survive this:

  • raspberrypi-gpiomem: use-after-free in rpi_gpiomem_remove(), which logs priv->name after kfree(priv).
  • pwm-rp1: NULL pointer dereference in rp1_pwm_remove(), as the driver data is never set. The clock is devm-managed, so the remove callback and the manual disable are dropped.
  • clk-rp1: oops in __clk_register() on the second probe, because the clock core clears hw.init of the static clock descriptors after the first registration.
  • rp1-adc: the hwmon device is devm-registered and also unregistered in remove(), and module_platform_driver_probe() prevents the driver from binding again after the device has been removed.

To reproduce (run detached, or over SSH, as a ttyAMA0 console is an RP1 UART and goes away with the unbind):

  setsid sh -c '
    echo 0002:01:00.0 > /sys/bus/pci/drivers/rp1_pci/unbind
    sleep 3
    echo 0002:01:00.0 > /sys/bus/pci/drivers/rp1_pci/bind
  ' </dev/null >/dev/null 2>&1 &

Without these patches, the unbind oopses in rpi_gpiomem_remove() and then in rp1_pwm_remove(), and the bind oopses in __clk_register(). Each oops stops the test, so each one shows up only once the previous one is fixed:

  pc : string+0x50/0x140
  lr : vsnprintf+0x1bc/0x64c
   _dev_info+0x54/0x60
   rpi_gpiomem_remove+0x5c/0x6c

  pc : rp1_pwm_remove+0x14/0x38
  lr : platform_remove+0x20/0x30

  pc : __clk_register+0x5c/0x9dc
   devm_clk_hw_register+0x5c/0xe0
   rp1_register_pll+0x24/0x40
   rp1_clk_probe+0xc0/0x168

Tested on a CM5 with 6.18.64: unbind and rebind of 0002:01:00.0 complete without oopses, and all RP1 platform devices enabled in the DT (13 on this board), Ethernet, USB and the ttyAMA0 console come back. The rp1-adc patches are build-tested only, as the ADC driver was not enabled on the test image.

The same code is still present in rpi-7.4.y.

@joshuacov1

joshuacov1 commented Nov 21, 2026 •

Copy link
Copy Markdown
Author

The two upstream fixes are posted:

Both carry Fixes: tags, so they should reach 6.18.y through stable.

rpi_gpiomem_remove() frees its private data and then reads priv->name
for its final log message. The freed memory may already have been
reused, and unbinding the driver oopses:

  pc : string+0x50/0x140
  lr : vsnprintf+0x1bc/0x64c
  Call trace:
   string+0x50/0x140 (P)
   vsnprintf+0x1bc/0x64c
   ...
   _dev_info+0x54/0x60
   rpi_gpiomem_remove+0x5c/0x6c
   platform_remove+0x20/0x30

Log the message before freeing the data, and terminate it with a
newline.

Signed-off-by: Joshua Covington <joshuacov@gmail.com>
rp1_pwm_probe() never sets the platform driver data, so
rp1_pwm_remove() reads the clock through a NULL pointer, and removing
the device oopses with a NULL pointer dereference at address 0x8:

  pc : rp1_pwm_remove+0x14/0x38
  lr : platform_remove+0x20/0x30

The clock comes from devm_clk_get_enabled(), which already disables it
on probe failure and on remove, so both the remove callback and the
error path in probe would disable it a second time.

Drop the remove callback and the manual clock disable.

Signed-off-by: Joshua Covington <joshuacov@gmail.com>
The clock descriptors are static. The clock core clears hw.init when a
clock is registered. When RP1 is unbound and bound again, the second
probe registers the clocks without init data and oopses:

  pc : __clk_register+0x5c/0x9dc
  Call trace:
   __clk_register+0x5c/0x9dc (P)
   devm_clk_hw_register+0x5c/0xe0
   rp1_register_pll+0x24/0x40
   rp1_clk_probe+0xc0/0x168

The clock core also leaves hw.core pointing to the freed clk_core on
unregister. Some clocks refer to a parent that is registered after
them, e.g. clk_mipi0_dpi to clksrc_mipi0_dsi_byteclk. On the second
probe, their parent lookup can use the stale clk_core.

Restore hw.init and clear the stale state of all descriptors before
registering them.

Fixes: 6486341 ("clk: rp1: Add support for clocks provided by RP1")
Signed-off-by: Joshua Covington <joshuacov@gmail.com>
The hwmon device is registered with
devm_hwmon_device_register_with_groups(), which unregisters it when
the driver is unbound. rp1_adc_remove() unregisters it as well, so the
devres action later operates on a device that is already gone.

Drop the remove callback.

Signed-off-by: Joshua Covington <joshuacov@gmail.com>
The driver is registered with module_platform_driver_probe(), so its
probe function is discarded after boot and later devices fail to
probe. When the RP1 device is unbound and bound again, the ADC device
is recreated but never gets a driver, and the RP1 sensors stay missing
until the next reboot.

Register the driver with module_platform_driver() and drop __init from
the probe function.

Signed-off-by: Joshua Covington <joshuacov@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant