mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2026-08-09 06:14:34 +02:00
sdca_dev_unregister_functions() iterates over all SDCA function
descriptors and calls sdca_dev_unregister() on each func_dev without
checking for NULL. When a function registration has failed partway
through, or the device cleanup races with probe deferral, func_dev
entries may be NULL, leading to a kernel oops:
BUG: kernel NULL pointer dereference, address: 0000000000000040
RIP: 0010:device_del+0x1e/0x3e0
Call Trace:
sdca_dev_unregister_functions+0x37/0x60 [snd_soc_sdca]
release_nodes+0x35/0xb0
devres_release_all+0x90/0x100
device_unbind_cleanup+0xe/0x80
device_release_driver_internal+0x1c1/0x200
bus_remove_device+0xc6/0x130
device_del+0x161/0x3e0
device_unregister+0x17/0x60
sdw_delete_slave+0xb6/0xd0 [soundwire_bus]
sdw_bus_master_delete+0x1e/0x50 [soundwire_bus]
...
sof_probe_work+0x19/0x30 [snd_sof]
This was observed on a Lenovo ThinkPad X1 Carbon G14 (Panther Lake)
with the SOF audio driver probe failing due to missing Panther Lake
firmware, causing the subsequent cleanup of SoundWire devices to
trigger the crash.
Fix this with three changes:
1) Add a NULL guard in sdca_dev_unregister() so that callers do not
need to pre-validate the pointer (defense in depth).
2) In sdca_dev_unregister_functions(), skip NULL func_dev entries
and clear func_dev to NULL after unregistration, making the
function idempotent and safe against double-invocation.
3) In sdca_dev_register_functions(), roll back all previously
registered functions when a later one fails, so the function
array is never left in a partially-populated state.
Fixes: 4496d1c65b ("ASoC: SDCA: add function devices")
Signed-off-by: Kean Ren <rh_king@163.com>
Reviewed-by: Charles Keepax <ckeepax@opensource.cirrus.com>
Link: https://patch.msgid.link/20260611023757.1553960-1-rh_king@163.com
Signed-off-by: Mark Brown <broonie@kernel.org>
136 lines
3.3 KiB
C
136 lines
3.3 KiB
C
// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
|
|
// Copyright(c) 2024 Intel Corporation.
|
|
|
|
/*
|
|
* SDCA Function Device management
|
|
*/
|
|
|
|
#include <linux/acpi.h>
|
|
#include <linux/module.h>
|
|
#include <linux/auxiliary_bus.h>
|
|
#include <linux/soundwire/sdw.h>
|
|
#include <sound/sdca.h>
|
|
#include <sound/sdca_function.h>
|
|
#include "sdca_function_device.h"
|
|
|
|
/*
|
|
* A SoundWire device can have multiple SDCA functions identified by
|
|
* their type and ADR. there can be multiple SoundWire devices per
|
|
* link, or multiple devices spread across multiple links. An IDA is
|
|
* required to identify each instance.
|
|
*/
|
|
static DEFINE_IDA(sdca_function_ida);
|
|
|
|
static void sdca_dev_release(struct device *dev)
|
|
{
|
|
struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
|
|
struct sdca_dev *sdev = auxiliary_dev_to_sdca_dev(auxdev);
|
|
|
|
ida_free(&sdca_function_ida, auxdev->id);
|
|
kfree(sdev);
|
|
}
|
|
|
|
/* alloc, init and add link devices */
|
|
static struct sdca_dev *sdca_dev_register(struct device *parent,
|
|
struct sdca_function_desc *function_desc)
|
|
{
|
|
struct sdca_dev *sdev;
|
|
struct auxiliary_device *auxdev;
|
|
int ret;
|
|
int rc;
|
|
|
|
sdev = kzalloc_obj(*sdev);
|
|
if (!sdev)
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
auxdev = &sdev->auxdev;
|
|
auxdev->name = function_desc->name;
|
|
auxdev->dev.parent = parent;
|
|
auxdev->dev.fwnode = function_desc->node;
|
|
auxdev->dev.release = sdca_dev_release;
|
|
|
|
sdev->function.desc = function_desc;
|
|
|
|
rc = ida_alloc(&sdca_function_ida, GFP_KERNEL);
|
|
if (rc < 0) {
|
|
kfree(sdev);
|
|
return ERR_PTR(rc);
|
|
}
|
|
auxdev->id = rc;
|
|
|
|
/* now follow the two-step init/add sequence */
|
|
ret = auxiliary_device_init(auxdev);
|
|
if (ret < 0) {
|
|
dev_err(parent, "failed to initialize SDCA function dev %s\n",
|
|
function_desc->name);
|
|
ida_free(&sdca_function_ida, auxdev->id);
|
|
kfree(sdev);
|
|
return ERR_PTR(ret);
|
|
}
|
|
|
|
ret = auxiliary_device_add(auxdev);
|
|
if (ret < 0) {
|
|
dev_err(parent, "failed to add SDCA function dev %s\n",
|
|
sdev->auxdev.name);
|
|
/* sdev will be freed with the put_device() and .release sequence */
|
|
auxiliary_device_uninit(&sdev->auxdev);
|
|
return ERR_PTR(ret);
|
|
}
|
|
|
|
return sdev;
|
|
}
|
|
|
|
static void sdca_dev_unregister(struct sdca_dev *sdev)
|
|
{
|
|
if (!sdev)
|
|
return;
|
|
|
|
auxiliary_device_delete(&sdev->auxdev);
|
|
auxiliary_device_uninit(&sdev->auxdev);
|
|
}
|
|
|
|
int sdca_dev_register_functions(struct sdw_slave *slave)
|
|
{
|
|
struct sdca_device_data *sdca_data = &slave->sdca_data;
|
|
int i;
|
|
int ret;
|
|
|
|
for (i = 0; i < sdca_data->num_functions; i++) {
|
|
struct sdca_dev *func_dev;
|
|
|
|
func_dev = sdca_dev_register(&slave->dev,
|
|
&sdca_data->function[i]);
|
|
if (IS_ERR(func_dev)) {
|
|
ret = PTR_ERR(func_dev);
|
|
/*
|
|
* Unregister functions that were successfully
|
|
* registered before this failure. This also
|
|
* sets func_dev to NULL so the caller will not
|
|
* try to unregister them again.
|
|
*/
|
|
sdca_dev_unregister_functions(slave);
|
|
return ret;
|
|
}
|
|
|
|
sdca_data->function[i].func_dev = func_dev;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL_NS(sdca_dev_register_functions, "SND_SOC_SDCA");
|
|
|
|
void sdca_dev_unregister_functions(struct sdw_slave *slave)
|
|
{
|
|
struct sdca_device_data *sdca_data = &slave->sdca_data;
|
|
int i;
|
|
|
|
for (i = 0; i < sdca_data->num_functions; i++) {
|
|
if (!sdca_data->function[i].func_dev)
|
|
continue;
|
|
|
|
sdca_dev_unregister(sdca_data->function[i].func_dev);
|
|
sdca_data->function[i].func_dev = NULL;
|
|
}
|
|
}
|
|
EXPORT_SYMBOL_NS(sdca_dev_unregister_functions, "SND_SOC_SDCA");
|