1
0
Fork 0

ALSA: hda: Fix race between creating and refreshing sysfs entries

hda_widget_sysfs_reinit() can free underlying codec->widgets structure
on which widget_tree_create() operates. Add locking to prevent such
issues from happening.

Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=110382
Signed-off-by: Amadeusz Sławiński <amadeuszx.slawinski@linux.intel.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
hifive-unleashed-5.2
Amadeusz Sławiński 2019-05-13 11:18:01 +02:00 committed by Takashi Iwai
parent 891afcf246
commit ed180abba7
3 changed files with 11 additions and 0 deletions

View File

@ -81,6 +81,7 @@ struct hdac_device {
atomic_t in_pm; /* suspend/resume being performed */ atomic_t in_pm; /* suspend/resume being performed */
/* sysfs */ /* sysfs */
struct mutex widget_lock;
struct hdac_widget_tree *widgets; struct hdac_widget_tree *widgets;
/* regmap */ /* regmap */

View File

@ -55,6 +55,7 @@ int snd_hdac_device_init(struct hdac_device *codec, struct hdac_bus *bus,
codec->bus = bus; codec->bus = bus;
codec->addr = addr; codec->addr = addr;
codec->type = HDA_DEV_CORE; codec->type = HDA_DEV_CORE;
mutex_init(&codec->widget_lock);
pm_runtime_set_active(&codec->dev); pm_runtime_set_active(&codec->dev);
pm_runtime_get_noresume(&codec->dev); pm_runtime_get_noresume(&codec->dev);
atomic_set(&codec->in_pm, 0); atomic_set(&codec->in_pm, 0);
@ -141,7 +142,9 @@ int snd_hdac_device_register(struct hdac_device *codec)
err = device_add(&codec->dev); err = device_add(&codec->dev);
if (err < 0) if (err < 0)
return err; return err;
mutex_lock(&codec->widget_lock);
err = hda_widget_sysfs_init(codec); err = hda_widget_sysfs_init(codec);
mutex_unlock(&codec->widget_lock);
if (err < 0) { if (err < 0) {
device_del(&codec->dev); device_del(&codec->dev);
return err; return err;
@ -158,7 +161,9 @@ EXPORT_SYMBOL_GPL(snd_hdac_device_register);
void snd_hdac_device_unregister(struct hdac_device *codec) void snd_hdac_device_unregister(struct hdac_device *codec)
{ {
if (device_is_registered(&codec->dev)) { if (device_is_registered(&codec->dev)) {
mutex_lock(&codec->widget_lock);
hda_widget_sysfs_exit(codec); hda_widget_sysfs_exit(codec);
mutex_unlock(&codec->widget_lock);
device_del(&codec->dev); device_del(&codec->dev);
snd_hdac_bus_remove_device(codec->bus, codec); snd_hdac_bus_remove_device(codec->bus, codec);
} }
@ -404,7 +409,9 @@ int snd_hdac_refresh_widgets(struct hdac_device *codec, bool sysfs)
} }
if (sysfs) { if (sysfs) {
mutex_lock(&codec->widget_lock);
err = hda_widget_sysfs_reinit(codec, start_nid, nums); err = hda_widget_sysfs_reinit(codec, start_nid, nums);
mutex_unlock(&codec->widget_lock);
if (err < 0) if (err < 0)
return err; return err;
} }

View File

@ -395,6 +395,7 @@ static int widget_tree_create(struct hdac_device *codec)
return 0; return 0;
} }
/* call with codec->widget_lock held */
int hda_widget_sysfs_init(struct hdac_device *codec) int hda_widget_sysfs_init(struct hdac_device *codec)
{ {
int err; int err;
@ -411,11 +412,13 @@ int hda_widget_sysfs_init(struct hdac_device *codec)
return 0; return 0;
} }
/* call with codec->widget_lock held */
void hda_widget_sysfs_exit(struct hdac_device *codec) void hda_widget_sysfs_exit(struct hdac_device *codec)
{ {
widget_tree_free(codec); widget_tree_free(codec);
} }
/* call with codec->widget_lock held */
int hda_widget_sysfs_reinit(struct hdac_device *codec, int hda_widget_sysfs_reinit(struct hdac_device *codec,
hda_nid_t start_nid, int num_nodes) hda_nid_t start_nid, int num_nodes)
{ {