Skip to content

drivers/can: Fix close drain, write-only reader lifecycle, and STM32 RX header#18759

Merged
acassis merged 1 commit intoapache:masterfrom
shizacat:update-arch-can-driver-stm32
Apr 28, 2026
Merged

drivers/can: Fix close drain, write-only reader lifecycle, and STM32 RX header#18759
acassis merged 1 commit intoapache:masterfrom
shizacat:update-arch-can-driver-stm32

Conversation

@shizacat
Copy link
Copy Markdown
Contributor

Summary

This PR hardens the generic CAN character driver (drivers/can/can.c) and the STM32 bxCAN backend (arch/arm/src/stm32/stm32_can.c) for write-only use, safe shutdown, and consistent RX headers.

  • Per-file context on every open: init_can_reader() runs for all successful opens so write-only fds get msgalign and ioctl-related state. Readers are still only queued on cd_readers when O_RDOK is set.
  • can_close: frees that per-file context when the fd was write-only (never on cd_readers), fixing a leak.
  • Last close / TX drain: leaves the critical section before waiting so TX-complete ISRs can run; bounds SW-queue and H/W drain waits (20 × 500 ms per stage) so close() cannot hang forever when the controller retries without ACK or dev_txempty() never clears.
  • STM32 RX path: zeroes the full can_hdr before filling it so padding/reserved fields are not stack garbage—avoids false memcmp mismatches vs TX (e.g. examples/can with CONFIG_CAN_LOOPBACK).
  • Debug: stm32can_vputreg() log uses value, not an undefined val.

Impact

  • Loopback / full-header compare: Without clearing the RX header, apps that compare the whole header (loopback tests) could see spurious failures; this change makes RX headers deterministic.

  • close(): Unbounded waits on TX drain could freeze the app on a silent bus or endless retries; timeouts avoid infinite blocking while still calling dev_shutdown afterward.

  • Write-only open: Previously f_priv could stay NULL, breaking can_write (msgalign) and some ioctls—risk of fault/panic; allocating can_reader_s for every open fixes that.

Testing

Manual testing on stm32f103-minimum with a custom config (CAN loopback / Example CAN / app scenario as applicable).

@github-actions github-actions Bot added Arch: arm Issues related to ARM (32-bit) architecture Area: Drivers Drivers issues Size: M The size of the change in this PR is medium labels Apr 18, 2026
@shizacat shizacat force-pushed the update-arch-can-driver-stm32 branch from c9537e6 to e1149b7 Compare April 18, 2026 18:44
Comment thread drivers/can/can.c Outdated
Comment thread arch/arm/src/stm32/stm32_can.c Outdated
@shizacat shizacat force-pushed the update-arch-can-driver-stm32 branch from e1149b7 to 267649d Compare April 19, 2026 14:48
Comment thread drivers/can/can.c
Comment thread drivers/can/can.c Outdated
Comment thread drivers/can/can.c Outdated
Comment thread drivers/can/can.c Outdated
@shizacat shizacat force-pushed the update-arch-can-driver-stm32 branch 4 times, most recently from 198c069 to c57c35d Compare April 20, 2026 20:34
@github-actions github-actions Bot added Size: S The size of the change in this PR is small and removed Size: M The size of the change in this PR is medium labels Apr 20, 2026
@shizacat shizacat force-pushed the update-arch-can-driver-stm32 branch 3 times, most recently from 34b3dde to 10a16f8 Compare April 23, 2026 05:30
Comment thread drivers/can/can.c
Comment thread drivers/can/can.c
Comment thread drivers/can/can.c Outdated
acassis
acassis previously approved these changes Apr 23, 2026
…RX header

Always allocate per-file can_reader on open so write-only descriptors get msgalign / ioctl state; free that context on close when it was never linked into cd_readers (fixes leak).

Cap each drain loop (20×500 ms) so close() cannot block forever when bxCAN retries without ACK or dev_txempty never clears.

Fix stm32can_vputreg debug log to print the written value (correct parameter name).

Signed-off-by: Alexey Matveev <tippet@yandex.ru>
@shizacat
Copy link
Copy Markdown
Contributor Author

@acassis What to do also need for MR?

@acassis
Copy link
Copy Markdown
Contributor

acassis commented Apr 28, 2026

@acassis What to do also need for MR?

Everything is fine from my side, I was waiting for other to review too, but since nobody show up, I will merge now.

@acassis acassis merged commit 369755d into apache:master Apr 28, 2026
41 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: arm Issues related to ARM (32-bit) architecture Area: Drivers Drivers issues Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants