xhh233 opened a new issue, #22171:
URL: https://github.com/apache/nuttx/issues/22171

   # Case: pm_domain_unregister() clears the target domain registry on a double 
unregister
   
   Status: prepared, not submitted to upstream.
   
   Scope: public NuttX sources only; no vendor identifiers, no private 
addresses.
   
   Evidence level: E1_src for this case (hosted; the unmodified upstream `.c` is
   compiled before the patch, and the patched build applies `fix.patch` to a
   throw-away copy of the same source); not a real board / `sim:nsh` run.
   
   ## Summary
   
   Within a single PM domain, calling `pm_domain_unregister(domain, cb)` twice 
for
   the same callback makes the second call operate on a node whose `flink` and
   `blink` are already NULL. `dq_rem()` then assigns `q->head = NULL` and
   `q->tail = NULL`, which clears the registry of **that domain** and silently
   drops every other callback registered in the same domain. The second call 
still
   returns `OK`.
   
   This report is scoped to: **same domain, same callback, repeated 
unregister**.
   It is not a claim about arbitrary repeated unregisters across domains or 
about
   unrelated objects.
   
   ## Caller misuse vs confirmed driver defect
   
   This is classified as **caller misuse / missing API guard**, not as a 
confirmed
   driver defect. In the pinned NuttX 13.1.0 tree (commit
   `2b5c0de5e933281c209f954a63b99cf10c132346`), the drivers that call
   `pm_unregister()` each show at most one unregister for a given callback on
   their normal error/teardown paths:
   
   - `drivers/timers/watchdog.c:435`: guarded by `upper->monitor`; repeated stop
     does not unregister again.
   - `drivers/power/supply/regulator.c:1443`: `regulator_unregister()` frees the
     device afterwards; there is no normal second teardown of the same object.
   - `drivers/coresight/coresight_core.c:161`: `coresight_disable_clk()` is 
called
     on the `refcnt` 0-transition or enable failure; `coresight_unregister()` 
calls
     it once while `refcnt > 0`, then deletes the node.
   - `drivers/1wire/1wire.c:738`: `onewire_uninitialize()` frees `master`;
     a second call would be a use-after-free, not a normal second unregister.
   - STM32 I2C / 1-wire drivers: unregister only when the reference count 
reaches
     zero, then free the instance.
   
   No driver in the pinned tree was confirmed to unregister the same callback
   twice along an error/unload path. Reachability therefore depends on a caller
   that violates the one-unregister-per-callback contract.
   
   ## Affected code
   
   - `drivers/power/pm/pm_unregister.c:59-74`: unconditional `dq_rem()` and
     unconditional `return OK`.
   - `include/nuttx/queue.h:211-238`: `dq_rem()` re-links neighbors from
     `blink/flink`; with both NULL it clears the queue head and tail.
   - `include/nuttx/queue.h:307-308`: `dq_inqueue(p, q)` is available, but it 
is a
     **per-queue link test** (`p->flink || q->tail == p`), not a global or
     cross-domain membership check.
   
   ## Impact
   
   Only the registry of the domain passed to `pm_domain_unregister()` is 
affected.
   In the hosted scenario, domain 1 remains intact while domain 0 is cleared.
   `dq_inqueue()` does not search other domains; the candidate fix uses it only 
to
   ask whether the callback is still linked in that one domain's registry. The
   case makes no claim of cross-domain safety for uninitialized callbacks or 
for a
   callback that belongs to a different domain.
   
   ## Reproduction (host)
   
   The reproducer compiles the unmodified `pm_unregister.c` and registers 
`devA`,
   `devB` in domain 0 and `devC` in domain 1, then unregisters `devA` twice in
   domain 0:
   
   ```
   registry_before=2 first_unregister_ret=0 second_unregister_ret=0
   registry_count=0 registry_head_null=1 registry_tail_null=1
   devB_lost=1 other_domain_intact=1
   ```
   
   With a per-domain membership check the second call returns `-ENOENT` and the
   domain 0 registry keeps `devB`:
   
   ```
   registry_before=2 first_unregister_ret=0 second_unregister_ret=-2
   registry_count=1 registry_head_is_devB=1 registry_tail_is_devB=1
   devB_lost=0 other_domain_intact=1
   ```
   
   ## Suggested fix
   
   Only call `dq_rem()` when `dq_inqueue(&cb->entry, &pdom->registry)` is true;
   otherwise return `-ENOENT` without touching that domain's registry. This is a
   guard for repeated unregister of the same callback in the same domain; it is
   not a cross-domain lookup.
   
   ## Novelty
   
   As of 2026-11-23, local searches found no corresponding public report (search
   expressions and request counts are recorded in `../NOVELTY.md`). This has not
   been confirmed by upstream.
   
   ## Evidence files
   
   `README.md`, `ISSUE-DRAFT-EN.md`, `before.log`, `after.log`, `expected.txt`,
   `fix.patch`, `driver.c`, `build.sh`, `run.sh`, `validate.sh`, `src/`, 
`shim/`,
   `PROVENANCE.md`, `SRC-SHA256SUMS`, `SHA256SUMS`.
   
   <details><summary>Host reproducer: driver.c + build.sh + logs</summary>
   
   ```c
   /* SPDX-License-Identifier: Apache-2.0
    *
    * PM-06 hosted driver: exercises the UNMODIFIED upstream
    * drivers/power/pm/pm_unregister.c against a minimal kernel shim.
    *
    * Scenario:
    *   1. domain 0 registry holds two callbacks, devA and devB (added last);
    *      domain 1 registry holds devC.
    *   2. pm_domain_unregister(0, devA)  -> devA legitimately removed.
    *   3. pm_domain_unregister(0, devA)  -> same node unregistered again.
    *   4. Inspect the domain-0 registry head/tail/count and domain 1.
    *
    * Upstream (unpatched) behaviour: step 3 dq_rem()s a node whose links were
    * cleared by step 2, so dq_rem() writes q->head = q->tail = NULL and every
    * remaining callback (devB) is silently lost, while the call returns OK.
    */
   
   #include <nuttx/config.h>
   
   #include <stdio.h>
   #include <string.h>
   
   #include <nuttx/queue.h>
   #include <nuttx/power/pm.h>
   
   #include "pm.h"
   
   /* pm_initialize.c normally defines this; the shim harness supplies it. */
   
   struct pm_domain_s g_pmdomains[CONFIG_PM_NDOMAINS];
   
   static struct pm_callback_s g_dev_a;
   static struct pm_callback_s g_dev_b;
   static struct pm_callback_s g_dev_c;
   
   static int registry_count(int domain)
   {
     FAR dq_entry_t *node;
     int count = 0;
   
     for (node = dq_peek(&g_pmdomains[domain].registry);
          node != NULL && count < 64;
          node = dq_next(node))
       {
         count++;
       }
   
     return count;
   }
   
   int main(void)
   {
     FAR dq_queue_t *reg0 = &g_pmdomains[0].registry;
     FAR dq_queue_t *reg1 = &g_pmdomains[1].registry;
     int first_ret;
     int second_ret;
     int count0;
     int other_intact;
   
     memset(g_pmdomains, 0, sizeof(g_pmdomains));
     dq_init(reg0);
     dq_init(reg1);
     memset(&g_dev_a, 0, sizeof(g_dev_a));
     memset(&g_dev_b, 0, sizeof(g_dev_b));
     memset(&g_dev_c, 0, sizeof(g_dev_c));
   
     /* Register devA then devB into domain 0; devC into domain 1. */
   
     dq_addlast(&g_dev_a.entry, reg0);
     dq_addlast(&g_dev_b.entry, reg0);
     dq_addlast(&g_dev_c.entry, reg1);
   
     printf("case=PM-06\n");
     printf("upstream_object=src/drivers/power/pm/pm_unregister.c\n");
     printf("registry_before=%d\n", registry_count(0));
   
     /* First unregister of devA: a normal, in-queue removal. */
   
     first_ret = pm_domain_unregister(0, &g_dev_a);
   
     /* Second unregister of the same, already-removed devA. */
   
     second_ret = pm_domain_unregister(0, &g_dev_a);
   
     count0       = registry_count(0);
     other_intact = (dq_peek(reg1) == &g_dev_c.entry &&
                     dq_tail(reg1) == &g_dev_c.entry);
   
     printf("first_unregister_ret=%d\n", first_ret);
     printf("second_unregister_ret=%d\n", second_ret);
     printf("registry_count=%d\n", count0);
     printf("registry_head_null=%d\n", reg0->head == NULL);
     printf("registry_tail_null=%d\n", reg0->tail == NULL);
     printf("registry_head_is_devB=%d\n", reg0->head == &g_dev_b.entry);
     printf("registry_tail_is_devB=%d\n", reg0->tail == &g_dev_b.entry);
     printf("devB_lost=%d\n", reg0->head != &g_dev_b.entry);
     printf("other_domain_intact=%d\n", other_intact);
     printf("verdict=%s\n",
            (count0 == 1 && reg0->head == &g_dev_b.entry &&
             reg0->tail == &g_dev_b.entry && other_intact)
              ? "REGISTRY_INTACT"
              : "REGISTRY_CLEARED");
   
     return 0;
   }
   ```
   ```sh
   #!/bin/sh
   # SPDX-License-Identifier: Apache-2.0
   #
   # Build the PM-06 reproducer.  The single compile command below takes the
   # UNMODIFIED upstream file src/drivers/power/pm/pm_unregister.c straight from
   # this case's byte-identical source copy; shim/ only satisfies the kernel API
   # symbols it references.  Nothing in src/ is edited.
   set -eu
   
   cd "$(CDPATH= cd -- "$(dirname "$0")" && pwd)"
   
   CC=${CC:-cc}
   
   $CC -O0 -g -Wall -Wextra -Werror -Wno-unused-parameter \
       -Ishim/include -Isrc/include -Isrc/drivers/power/pm \
       src/drivers/power/pm/pm_unregister.c shim/kernel_shim.c driver.c \
       -o repro
   
   echo "built: repro (upstream src/drivers/power/pm/pm_unregister.c)"
   ```
   ```text
   # before.log
   case=PM-06
   upstream_object=src/drivers/power/pm/pm_unregister.c
   registry_before=2
   first_unregister_ret=0
   second_unregister_ret=0
   registry_count=0
   registry_head_null=1
   registry_tail_null=1
   registry_head_is_devB=0
   registry_tail_is_devB=0
   devB_lost=1
   other_domain_intact=1
   verdict=REGISTRY_CLEARED
   
   # after.log
   case=PM-06
   upstream_object=src/drivers/power/pm/pm_unregister.c
   registry_before=2
   first_unregister_ret=0
   second_unregister_ret=-2
   registry_count=1
   registry_head_null=0
   registry_tail_null=0
   registry_head_is_devB=1
   registry_tail_is_devB=1
   devB_lost=0
   other_domain_intact=1
   verdict=REGISTRY_INTACT
   ```
   </details>
   
   The reproducer compiles the unmodified upstream C file from a pinned 
snapshot (see PROVENANCE.md in the attached case); the shim only provides 
kernel API stubs. Full bundle (source, shim, logs, patch, sha256) available on 
request.
   


-- 
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]

Reply via email to