Skip to content

wireless/bluetooth/zblue: Run the work queues as pthreads - #4129

Open
chengkai15 wants to merge 1 commit into
apache:masterfrom
chengkai15:zblue-pthread-wq
Open

chengkai15 wants to merge 1 commit into
apache:masterfrom
chengkai15:zblue-pthread-wq

Conversation

@chengkai15

Copy link
Copy Markdown
Contributor

Note: Please adhere to Contributing Guidelines.

depends-on: apache/nuttx#22268

Summary

On current master, zblue_peripheral on sim:zblue hangs in bt_enable(): HCI_Reset is never sent.

In the flat build the Zephyr work queues are kernel threads (work_queue_create()), while the HCI receive thread and the application threads are pthreads of the application. They are in different task groups:

  • The thread-specific key of the porting layer is created by the first thread that uses it, a work queue (kernel thread). Since apache/nuttx 0bc013bb2b, pthread_setspecific() rejects a key that was not created in the group of the caller, so it returns EINVAL in the receive thread. That thread then does not release the cooperative lock while it blocks in read(), and the host TX processor waits on that lock forever.
  • File descriptors are not shared either, which is why the HCI driver had its own TX work queue that opens the device a second time.

This PR creates the work queues with work_queue_create_pthread() (apache/nuttx#22268), so all host threads are pthreads of the application. The HCI driver writes in send() again, and the TX work queue and its thread are removed. uart_bth4 writes each packet whole under its send lock, so writes from different threads do not interleave.

Impact

Testing

sim:zblue on Linux with a CSR USB dongle (CONFIG_SIM_HCISOCKET) as controller. BlueZ gatttool on a second CSR dongle is the central.

master (nuttx c68c0bfbc1, nuttx-apps fa64592): bt_enable() hangs, and ps shows the TX processor work queue waiting on a mutex held by the receive thread:

nsh> zblue_peripheral &
zblue_peripheral [8:100]
  TID   PID  PPID PRI POLICY   TYPE    NPX STATE    EVENT     SIGMASK            STACK COMMAND
    8     8     7 100 FIFO     Task      - Waiting  Semaphore 0000000000000000 0067456 zblue_peripheral
    9     0     8 121 FIFO     Kthread   - Waiting  Semaphore 0000000000000000 0071520 sysworkq 0x7caf25724010 0x7caf25724098
   10     0     8 110 FIFO     Kthread   - Waiting  Semaphore 0000000000000000 0070768 BT LW WQ 0x7caf25724280 0x7caf25724308
   11     0     8 121 FIFO     Kthread   - Waiting  Mutex:14  0000000000000000 0070496 zblue_wq 0x7caf257244f0 0x7caf25724578
   12     0     8 128 FIFO     Kthread   - Waiting  Semaphore 0000000000000000 0070672 BT RX WQ 0x7caf25724760 0x7caf257247e8
   13     0     8 129 FIFO     Kthread   - Waiting  Semaphore 0000000000000000 0070496 zblue_hci_tx 0x7caf25724a00 0x7caf25724a88
   14     8     7 130 FIFO     pthread   - Waiting  Semaphore 0000000000000000 0071600 zblue_hci_rx 0x400cc998 0x4023c440
nsh> W: bt_hci_core: Dropping queued command 0x0c03: HCI transport closed

With a debug print in z_zblue_set_self(), the work queues (pid 0) set the key, and the receive thread of the application (pid 8) gets EINVAL:

DBG set_self sysworkq pid=0 ret=0
DBG set_self BT LW WQ pid=0 ret=0
DBG set_self zblue_wq pid=0 ret=0
DBG set_self zblue_wq pid=0 ret=0
DBG set_self zblue_hci_tx pid=0 ret=0
DBG set_self (null) pid=8 ret=22

This PR + apache/nuttx#22268 on the same master:

I: bt_hci_core: HCI transport: HCI
I: bt_hci_core: Identity: P:00:1A:7D:DA:71:11
I: bt_hci_core: HCI: version 4.0 (0x06) revision 0x22bb, manufacturer 0x000a
I: bt_hci_core: LMP: version 4.0 (0x06) subver 0x22bb
zblue: Bluetooth ready, identity P:00:1A:7D:DA:71:11
zblue: advertising as "NuttX zblue"

All host threads now belong to the application (PID 8):

    8     8     7 100 FIFO     Task      - Waiting  Semaphore 0000000000000000 0067456 zblue_peripheral
    9     8     7 121 FIFO     pthread   - Waiting  Semaphore 0000000000000000 0071600 sysworkq 0x40130dac 0x7dfaa49e7f60
   10     8     7 110 FIFO     pthread   - Waiting  Semaphore 0000000000000000 0070848 BT LW WQ 0x40130dac 0x7dfaa49e7f30
   11     8     7 121 FIFO     pthread   - Waiting  Semaphore 0000000000000000 0070576 zblue_wq 0x40130dac 0x7dfaa49e7f30
   12     8     7 128 FIFO     pthread   - Waiting  Semaphore 0000000000000000 0070752 BT RX WQ 0x40130dac 0x7dfaa49e7ff0
   13     8     7 130 FIFO     pthread   - Waiting  Semaphore 0000000000000000 0071600 zblue_hci_rx 0x400cc8c8 0x4023d5c0
  • 10 GATT reads of the Device Name (gatttool --char-read -a 0x000b), each on a new connection: 10/10 read 4e 75 74 74 58 20 7a 62 6c 75 65 ("NuttX zblue").
  • 200 reads on one connection: 200/200. With the host ACL flow control (on by default), btmon on the controller shows 201 ACL packets received, 201 sent and 201 HCI_Host_Number_Of_Completed_Packets, and no write failed.

Not tested: protected and kernel builds (zblue supports only the simulator), real boards.

In the flat build the Zephyr work queues are kernel threads, created
with work_queue_create(), while the HCI receive thread and the threads
of the application are pthreads of the application.  The two kinds of
thread are in different task groups and do not share per-process
resources:

- the thread-specific key of the porting layer is created in the group
  of the first thread that uses it.  Since pthread_setspecific() rejects
  keys that were not created in the group of the caller, the other
  threads cannot record their k_thread, the cooperative lock is not
  released while the receive thread blocks in read(), and bt_enable()
  hangs: HCI_Reset is never sent.
- the file descriptors: the HCI driver needed its own TX work queue,
  which opens the device again, to write from the host work queues.

Create the work queues with work_queue_create_pthread(), so that all
the threads of the host are pthreads of the application, and write the
packets directly in send() again.  The TX work queue and its thread are
removed.

Signed-off-by: Kai Cheng <chengkai@xiaomi.com>
@chengkai15

Copy link
Copy Markdown
Contributor Author

This PR depends on apache/nuttx#22268 (sched/wqueue: Add work_queue_create_pthread()), which is not merged yet.

work.c calls work_queue_create_pthread(), which that PR adds. Until it is merged:

  • this PR does not build without it (work_queue_create_pthread() is not declared or defined), also in CI;
  • current master still hangs in bt_enable() on sim:zblue (see the description).

Please merge apache/nuttx#22268 first. The test results in the description are from master with both PRs applied.

@github-actions

Copy link
Copy Markdown

⚠️ depends-on could not be parsed

A depends-on: line was found in the PR description, but no valid dependency was parsed. Supported repositories: apache/nuttx, apache/nuttx-apps; the PR id must be numeric.

Expected format:

depends-on: [apache/nuttx/pull/<N> apache/nuttx-apps/pull/<M>]

CI run: https://github.com/apache/nuttx-apps/actions/runs/45418995878

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant