peci: core: prevent sysfs new_device race during peci_del_adapter

When peci_del_adapter() tears down an adapter while a userspace daemon
such as intel-cpusensor is actively polling, the daemon sees the client
devices (e.g. 0-30, 0-31) disappear during device_for_each_child() and
immediately writes to /sys/bus/peci/devices/peci-0/new_device to
re-instantiate them.

Because peci_sysfs_new_device() called peci_new_device() outside
adapter->userspace_clients_lock, a racing new_device write could
register a new child onto adapter->dev after both userspace_clients and
device_for_each_child() had already completed, right before adapter->dev
was unregistered and freed.

Fix this by:
1. Adding an is_registered flag to struct peci_adapter, set on successful
   adapter registration and checked/cleared under
   adapter->userspace_clients_lock at the start of peci_del_adapter().
   Checking is_registered before teardown also prevents concurrent or
   re-entrant double device_unregister().
2. Holding adapter->userspace_clients_lock across the registration check,
   peci_new_device(), and list_add_tail() in peci_sysfs_new_device(), so
   racing new_device writes are immediately rejected once teardown begins.
3. Deferring idr_remove() until after device_unregister() completes,
   preventing premature bus number reuse and sysfs name collisions.
4. Adding dev_info() traces in peci_del_adapter() and
   peci_sysfs_new_device() to confirm clean teardown and rejection of
   late instantiations.

Google-Bug-Id: 540863271
Change-Id: I8873cd4bb6c068c63ff00f1c6f0411af16915463
Signed-off-by: William A. Kennington III <wak@google.com>
1 file changed