Skip to content

soundwire: Fix race between handle_nested_irq() and irq_dispose_mappi… - #6077

Open
vijendarmukunda wants to merge 1 commit into
thesofproject:topic/sof-devfrom
vijendarmukunda:topic/sof-dev
Open

vijendarmukunda wants to merge 1 commit into
thesofproject:topic/sof-devfrom
vijendarmukunda:topic/sof-dev

Conversation

@vijendarmukunda

Copy link
Copy Markdown
  • Fixes a NULL pointer dereference race between sdw_irq_dispose_mapping() and sdw_handle_slave_alerts() calling handle_nested_irq(slave->irq) concurrently
  • After commit ccb7bb1, handle_nested_irq() is called before sdw_dev_lock / slave->probed check with no serialization against irq_dispose_mapping()
  • Adds a dedicated per-slave irq_lock mutex to serialize the two critical sections
  • Clears slave->irq to 0 under irq_lock before irq_dispose_mapping() runs, preventing any post-dispose use of a freed IRQ descriptor

This race was identified during the AMD SoundWire driver series upstream review as a pre-existing issue: https://sashiko.dev/#/message/9fa8cd26-f97b-4027-97bc-ad60fb0b6e5a%40amd.com

…ng()

sdw_irq_dispose_mapping() and sdw_handle_slave_alerts() can run
concurrently with no serialization, leading to a NULL pointer
dereference:

  CPU0                                CPU1
  device_release_driver_internal()
    sdw_bus_remove()
      slave->probed = false
    device_unbind_cleanup()
      sdw_irq_dispose_mapping()
        irq_dispose_mapping(slave->irq)
          delete_irq_desc()
                                      sdw_handle_slave_alerts()
                                        handle_nested_irq(slave->irq)
                                          desc = irq_to_desc(irq)
                                          raw_spin_lock_irq(&desc->lock)
                                            <- NULL pointer dereference

After commit ccb7bb1 ("soundwire: Move handle_nested_irq outside
of sdw_dev_lock"), handle_nested_irq(slave->irq) is called before the
sdw_dev_lock / slave->probed check and therefore has no protection
against concurrent irq_dispose_mapping(). sdw_dev_lock cannot be held
for the full duration of handle_nested_irq() as that would reintroduce
the deadlock that commit ccb7bb1 ("soundwire: Move handle_nested_irq
outside of sdw_dev_lock") was fixing.

Add a dedicated per-slave irq_lock mutex to serialize the two critical
sections. handle_nested_irq() holds irq_lock for its entire duration,
preventing irq_dispose_mapping() from proceeding until the handler
completes. sdw_irq_dispose_mapping() acquires irq_lock to clear
slave->irq atomically, then calls irq_dispose_mapping() after releasing
the lock. Once slave->irq is zeroed under irq_lock, no new
handle_nested_irq() calls can start, and any call already in progress
will complete before irq_dispose_mapping() is reached.

slave->irq is explicitly cleared to 0 before irq_dispose_mapping() is
called, so any alert arriving after dispose safely skips
handle_nested_irq() without reaching a freed descriptor.

irq_lock is separate from sdw_dev_lock and is not visible to peripheral
drivers, so there is no deadlock risk from the IRQ handler calling back
into SoundWire.

Fixes: ccb7bb1 ("soundwire: Move handle_nested_irq outside of sdw_dev_lock")
Signed-off-by: Vijendar Mukunda <Vijendar.Mukunda@amd.com>
@vijendarmukunda

Copy link
Copy Markdown
Author

Shuming and the other CODEOWNERS are already included. Could you please also
add @plbossart @charleskeepax @bardliao as additional
reviewers?

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the per-slave IRQ locking change.

No open issues 🎉

What changed in this PR

Addresses a SoundWire NULL-pointer race between nested interrupt handling and IRQ mapping teardown using a per-slave mutex.

Changes:

  • Adds and manages the per-slave IRQ lock.
  • Protects nested interrupt handling and clears the IRQ number under the lock before disposing of its mapping.
File Description
include/​linux/​soundwire/​sdw.h Adds the per-slave irq_lock mutex.
drivers/​soundwire/​slave.c Initializes and destroys the mutex.
drivers/​soundwire/​irq.c Clears the IRQ under the lock before disposal.
drivers/​soundwire/​bus.c Locks around nested IRQ validation and handling.

Review effort: Balanced

Comment thread drivers/soundwire/irq.c
mutex_unlock(&slave->irq_lock);

if (irq)
irq_dispose_mapping(irq);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe a dumb question but shouldn't the lock be used in sdw_irq_create mapping, line 58 just below?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, not a dumb question. As per our understanding, the lock is not needed in sdw_irq_create_mapping() because slave->prop.use_domain_irq is always false at that point.
All drivers that use the domain IRQ set this flag in their read_prop callback, which is invoked after drv->probe() completes . The alert handler in sdw_handle_slave_alerts() guards the entire slave->irq access with if (slave->prop.use_domain_irq),
so it cannot reach the irq_lock block or read slave->irq during the creation window.
No concurrent access to slave->irq is possible at that stage.

The lock in sdw_irq_dispose_mapping() serializes against the alert handler during teardown — a later phase where use_domain_irq is already true and the race is real.

Please correct us if we have missed something.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants