cederom commented on PR #22142: URL: https://github.com/apache/nuttx/pull/22142#issuecomment-7136417333
Reviewed at b04f77af (10 commits on 7876a93a). nxstyle is clean. The cleanups in c6fab979, 7f3a80a1, 5e0675d2 and bd64587c look good to me. Most of my comments are on the new blocking-read path and the bound-count handling. ### Blocking reads (7c24ea89, 8a2aecd7) **1. Race between `memset(pend)` and the next READ (rpmsgdev_server.c:310)** `rpmsgdev_read_work()` sends the reply first and clears `dev->readpend` afterwards, on the `rpmsgdev_server` work queue. Once the reply is out, the client can send the next `RPMSGDEV_READ` straight away. `rpmsgdev_read_handler()` then runs on the rpmsg thread, fills in `pend`, and calls `file_poll(..., true)`. If the work thread's `memset()` runs after that (on SMP, or whenever the rpmsg thread preempts the work thread), it wipes a live pollfd while the driver still holds it: - `fd.cb` becomes NULL, so `poll_notify()` never calls back. The client's blocking read never returns, and with `rpmsg_wait()` it can't be interrupted (see 3). - `fd.priv` becomes NULL, so `rpmsgdev_read_cancel()` skips the teardown. The driver keeps a pointer into `dev` after `kmm_free(dev)`. That is the same use-after-free 8a2aecd7 fixes. Please finish the teardown and clear `pend` *before* the reply is sent, or serialize `readpend` with a lock (for example `server->lock`). The early `memset()` on the error path at line 354 has the same ordering question, though it runs on the rpmsg thread. **2. A blocking `read()` can now return `-EAGAIN`** After POLLIN, `rpmsgdev_read_work()` does a single non-blocking `file_read()` and forwards whatever it gets. The client used to retry on `-EAGAIN`, but now just returns it. So a blocking read can fail with `EAGAIN` whenever the data is gone by the time the work runs. That happens with spurious wakeups, and with exported devices that also have a local reader on the server. The `/dev/console` export in the sim config is exactly that case. Suggest re-arming the poll in the work when `file_read()` returns `-EAGAIN`, rather than replying. **3. A blocking read can't be interrupted** `rpmsgdev_send_recv()` waits with `rpmsg_wait()`. That ends up in `nxsem_wait_uninterruptible()` (or the wqueue loop), so a read blocked on the server now ignores signals: Ctrl-C, `alarm()` and `pthread_cancel()` do nothing. The removed path went through `rpmsgdev_wait()`/`nxsem_wait()` and could return `-EINTR`. Supporting interruption needs a cancel message, or a way to drop the cookie so a late reply is ignored, because the server still holds the cookie address from the client's stack. Even if that isn't done in this PR, the limitation should be documented. Minor: `read(fd, buf, 0)` on a blocking fd now waits for POLLIN instead of returning 0. **4. The read commands didn't need renumbering** The old `RPMSGDEV_READ` (3) already behaved like a non-blocking read, because the server opens the file with `O_NONBLOCK`. If command 3 keeps that meaning and the blocking read gets a new number (10), old and new images still interoperate, and the second `BREAKING CHANGE` goes away. ### Bound count / teardown (1d152c9d, 8a2aecd7) **5. `rpmsgdev_device_destroy()` can hang (rpmsgdev.c:1122)** `rpmsg_wait(&priv->ept, &priv->wait)` blocks without a timeout. If the endpoint was never bound, the count is 0 and the rpmsg device teardown hangs. That happens when the remote doesn't run the server or never exports that path, as with the third sim registration (`/dev/ttyUSB0`). Taking the count back with `nxsem_trywait()` avoids the hang. **6. `rpmsgdev_ns_unbind()` leaves the count at 1** `ns_unbind` fails the pending cookies, but `priv->wait` stays posted. So requests issued after an unbind go straight to the unbound endpoint instead of waiting for a rebind. This is the same problem 1d152c9d fixes for destroy. If the server binds again, `ns_bound` posts once more and the count reaches 2, so destroy then takes back only one. Suggest `nxsem_trywait(&priv->wait)` in `ns_unbind` as well. ### Write fragmentation (b9ea5d67 vs b04f77af) **7. The two commits contradict each other, and the opt-out is gone** b9ea5d67 removes write fragmentation because "most character drivers do not handle a read or write split into several pieces". b04f77af then splits every oversized write again, unconditionally. In the final state, any device that was registered with `RPMSGDEV_NOFRAG_WRITE` to keep writes atomic now gets its writes split silently, with no way to opt out. Please pick one: keep `-EMSGSIZE` (drop b04f77af), or keep splitting with a per-device opt-out. Either way, please don't land an intermediate commit that breaks large writes (console output included), since that hurts bisecting. Also, b9ea5d67 says "Large reads are split again later in this series". The later commit splits writes; nothing in the series splits reads again. ### Description The description mentions pool requests and a `RPMSG_POOL_COUNT` Kconfig default, but neither is in these 10 commits. Is the description stale, or is a commit missing? Please also link what nuttx-apps#4102 changes, since it has to be merged together with this PR. ### Nits - `FAR struct rpmsgdev_read_pend_s * pend` in `rpmsgdev_read_handler()`: drop the space after `*`. - `rpmsgdev_init_wqueue()` is not thread-safe. That's fine for bringup-time callers, but a comment would help now that `rpmsgdev_export_with_prefix()` calls it too. - `rpmsgdev_fail_cookies()` calls `rpmsg_post()`, which goes on to `ops->post()`, with a spinlock held and IRQs off. Please confirm every rpmsg transport's `post()` is safe to call in that state. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
