Files
nuttx/net/can/Kconfig
T
dongjiuzhu1 b8585b9b0c net/can: fix poll setup to properly find and use free pollfd slots
Bug Description:
The original code always used conn->pollinfo[0] (the first element) to store
new poll setup context, regardless of whether that slot was already in use.
This caused multiple poll operations on the same CAN socket to overwrite each
other's context, leading to:
- Lost poll waiters when multiple threads poll the same socket
- Memory corruption in pollfd structures
- Undefined behavior when poll_teardown tries to clean up

Root Cause:
The code directly assigned `info = conn->pollinfo` without checking if the
slot was available, effectively always using pollinfo[0]. When a second
thread called poll() on the same socket, it would overwrite the first
thread's poll context.

Solution:
1. Initialize info to NULL instead of conn->pollinfo
2. Before setting up poll, iterate through all CONFIG_NET_CAN_NPOLLWAITERS
   slots to find the first free slot (where fds == NULL)
3. Return -EBUSY if no free slots are available
4. During teardown, properly mark the slot as free by setting fds = NULL

Additional Changes:
- Added CONFIG_NET_CAN_NPOLLWAITERS Kconfig option (default 4) to make the
  maximum number of concurrent poll waiters configurable
- Changed hardcoded array size from 4 to CONFIG_NET_CAN_NPOLLWAITERS
- Fixed lock ordering in teardown to ensure fds is cleared before unlock

Impact:
- Enables multiple threads to safely poll the same CAN socket concurrently
- Prevents poll context corruption in multi-threaded applications
- Provides proper resource management with -EBUSY when all slots are full
- Makes the number of supported concurrent pollers configurable per use case

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-12-31 02:35:42 +08:00

177 lines
5.0 KiB
Plaintext

#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#
menu "SocketCAN Support"
config NET_CAN
bool "SocketCAN support"
default n
select NET_READAHEAD
depends on NET
---help---
Enable support for SocketCAN sockets.
This requires a lower-half driver compatible with the SocketCAN
API.
if NET_CAN
config NET_CAN_HAVE_TX_DEADLINE
bool
default n
config NET_CAN_HAVE_CANFD
bool
default n
config NET_CAN_HAVE_ERRORS
bool
default n
config NET_CAN_WRITE_BUFFERS
bool "Enable CAN write buffering"
default n
select NET_WRITE_BUFFERS
---help---
Write buffers allows buffering of ongoing CAN packets, providing
for higher performance, nonblocking output.
You might want to disable CAN write buffering on a highly memory
memory constrained system where there are no performance issues.
config CAN_PREALLOC_CONNS
int "Preallocated CAN socket connections"
default 4
---help---
Number of CAN connections (all tasks).
This number of connections will be pre-allocated during system boot.
If dynamic connections allocation is enabled, more connections may
be allocated at a later time, as the system needs them. Else this
will be the maximum number of connections available to the system
at all times.
Set to 0 to disable (and rely only on dynamic allocations).
config CAN_ALLOC_CONNS
int "Dynamic CAN connections allocation"
default 0
---help---
Dynamic memory allocations for CAN.
When set to 0 all dynamic allocations are disabled.
When set to 1 a new connection will be allocated every time,
and it will be free'd when no longer needed.
Setting this to 2 or more will allocate the connections in
batches (with batch size equal to this config). When a
connection is no longer needed, it will be returned to the
free connections pool, and it will never be deallocated!
config CAN_MAX_CONNS
int "Maximum number of CAN connections"
default 0
depends on CAN_ALLOC_CONNS > 0
---help---
If dynamic connections allocation is selected (CAN_ALLOC_CONNS > 0)
this will limit the number of connections that can be allocated.
This is useful in case the system is under very heavy load (or
under attack), ensuring that the heap will not be exhausted.
config NET_CAN_EXTID
bool "Enable CAN extended IDs"
default n
---help---
Enables support for the 29-bit extended ID in SocketCAN stack.
Default Standard 11-bit IDs.
config NET_CAN_CANFD
bool "Enable CAN FD support"
default y
depends on NET_CAN_HAVE_CANFD
---help---
Enable CAN FD support in SocketCAN stack
config NET_CAN_ERRORS
bool "Enable CAN errors support"
default n
depends on NET_CAN_HAVE_ERRORS
---help---
Enable CAN errors support in SocketCAN stack
config NET_CAN_SOCK_OPTS
bool "sockopt support"
default n
select NET_SOCKOPTS
select NET_CANPROTO_OPTIONS
---help---
Enable support for the CAN socket options
config NET_CAN_RAW_TX_DEADLINE
bool "TX deadline sockopt"
default n
depends on NET_CAN_SOCK_OPTS && NET_CAN_HAVE_TX_DEADLINE
---help---
Note: Non-standard SocketCAN sockopt, but this options helps us in
real-time use cases.
When the CAN_RAW_TX_DEADLINE sockopt is enabled. The user can send
CAN frames using sendmsg() function and add a deadline timespec
value in the CMSG data. When the deadline has been passed and the
CAN frame is still in the HW TX mailbox then the CAN driver will
discard the CAN frame automatically.
config NET_CAN_RAW_DEFAULT_TX_DEADLINE
int "Default TX deadline when no deadline is given (us)"
default 0
depends on NET_CAN_RAW_TX_DEADLINE
---help---
Some applications may not use the NET_CAN_RAW_TX_DEADLINE flag.
By default their deadline becomes 0 which means it becomes infinite.
This would mean that packets from applications without the
NET_CAN_RAW_TX_DEADLINE flag, can block the TX mailboxes forever.
This config can set the default deadline when no deadline has been
given.
config NET_CAN_RAW_FILTER_MAX
int "CAN_RAW_FILTER max filter count"
default 32
depends on NET_CANPROTO_OPTIONS
---help---
Maximum number of CAN_RAW filters that can be set per CAN connection.
config NET_CAN_NOTIFIER
bool "Support CAN notifications"
default n
depends on SCHED_WORKQUEUE
select WQUEUE_NOTIFIER
---help---
Enable building of CAN notifier logic that will execute a worker
function on the low priority work queue when read-ahead data
is available or when a CAN connection is lost. This is is a general
purpose notifier, but was developed specifically to support poll()
logic where the poll must wait for these events.
config NET_CAN_NBUFFERS
int "Number of CAN buffers"
depends on IOB_ALLOC
default 0
---help---
Each CAN packet is represented by a CAN buffers.
This setting determines the number of preallocated CAN buffers
available for packet data.
config NET_CAN_NPOLLWAITERS
int "Number of poll threads"
default 4
---help---
Maximum number of threads than can be waiting for POLL events.
Default: 4
endif # NET_CAN
endmenu # CAN Socket Support