Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp4933228yba; Mon, 13 May 2019 02:17:30 -0700 (PDT) X-Google-Smtp-Source: APXvYqwaAqbH6tjCYPUZiIc6knF+j9cnYe80xUd9qev7+eXuMzrvh8HOJU/wlIWeC8KeiXhlULwd X-Received: by 2002:a62:6a81:: with SMTP id f123mr32353959pfc.40.1557739050293; Mon, 13 May 2019 02:17:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1557739050; cv=none; d=google.com; s=arc-20160816; b=Wi6GDQgrLUOVw6/T7phqZsYGuIAe+zPXT8Hf9B1yeag4Vkjzqz5RgWo01zyDZLvWsV Un2WgW6J+jggUQvu6mDRoOglwrgztxx85/fnXIW3n9GHT7UrB0Rc/wSjjVp1+NpOVUtj fTJpjAKgNWmZDg43Rd9uTpZcNXQx7q037TFqA9V1kipBKB0RTlXROshXLdUyNCdRlMKd Fn+KntMWmH3LG19Hg1FSflLfvyG6ivYT3tMM6UNdHu38oLD9DGPdD2ZKD2gUeUR6nEm1 a1RYUj6nD1QqAMFLzPQv1P5bRlXTZNgn4OYWybdLVc72rniNABqA/5xcc2ucjdGoJcW3 fAfw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=gi9XzIA5Sp7V3yKx13dFH0FyWhMX+H1EjxWP0WJH9RM=; b=bkp2gPOsCamzOZc1GB+Ay6P+5YNeloKPXPv990szBwW3uI4tvEWlHb72yi9bqOop3P /W7ucmGuQeLpCMgIaoEq+Bg/wO2fTgwJVRzDqXGPY5QlcY0YSSEsy09yRy8VuPsyG8Fg KyPXg27+2lNBJZzt+li7EGAdnGLPPhoHFHnFBpvfom8Fl8gpMcb6qPJSFaFOjNL86p+u mKKt8k0BxmMW7M56EbjghK96hYRNRS/cMlmmgiQixt12SYiC4C+tAlcAILOZbtYsIoMh 7xX5rbqX98hkLuSYFR2LrA3sHm5KAWaZWH6lb4yekyNq5p7bkBzKveVt+iQVvDjRjJ3N 6h1A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id s14si5069775pfa.91.2019.05.13.02.17.13; Mon, 13 May 2019 02:17:30 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728820AbfEMJOf (ORCPT + 99 others); Mon, 13 May 2019 05:14:35 -0400 Received: from mga06.intel.com ([134.134.136.31]:48372 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728718AbfEMJOd (ORCPT ); Mon, 13 May 2019 05:14:33 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga104.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 13 May 2019 02:14:32 -0700 X-ExtLoop1: 1 Received: from xxx.igk.intel.com ([10.237.93.170]) by fmsmga001.fm.intel.com with ESMTP; 13 May 2019 02:14:31 -0700 From: =?UTF-8?q?Amadeusz=20S=C5=82awi=C5=84ski?= To: alsa-devel@alsa-project.org Cc: Jaroslav Kysela , Takashi Iwai , linux-kernel@vger.kernel.org, =?UTF-8?q?Amadeusz=20S=C5=82awi=C5=84ski?= Subject: [PATCH v2] ALSA: hda: Fix race between creating and refreshing sysfs entries Date: Mon, 13 May 2019 11:18:01 +0200 Message-Id: <20190513091801.985-1-amadeuszx.slawinski@linux.intel.com> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- Changes since v1: - Moved mutexes around the callers - Added comments noting that functions should be called with mutex held --- include/sound/hdaudio.h | 1 + sound/hda/hdac_device.c | 7 +++++++ sound/hda/hdac_sysfs.c | 3 +++ 3 files changed, 11 insertions(+) diff --git a/include/sound/hdaudio.h b/include/sound/hdaudio.h index 45f944d57982..85835d0c33cc 100644 --- a/include/sound/hdaudio.h +++ b/include/sound/hdaudio.h @@ -81,6 +81,7 @@ struct hdac_device { atomic_t in_pm; /* suspend/resume being performed */ /* sysfs */ + struct mutex widget_lock; struct hdac_widget_tree *widgets; /* regmap */ diff --git a/sound/hda/hdac_device.c b/sound/hda/hdac_device.c index 95b073ee4b32..4769f4c03e14 100644 --- a/sound/hda/hdac_device.c +++ b/sound/hda/hdac_device.c @@ -55,6 +55,7 @@ int snd_hdac_device_init(struct hdac_device *codec, struct hdac_bus *bus, codec->bus = bus; codec->addr = addr; codec->type = HDA_DEV_CORE; + mutex_init(&codec->widget_lock); pm_runtime_set_active(&codec->dev); pm_runtime_get_noresume(&codec->dev); atomic_set(&codec->in_pm, 0); @@ -141,7 +142,9 @@ int snd_hdac_device_register(struct hdac_device *codec) err = device_add(&codec->dev); if (err < 0) return err; + mutex_lock(&codec->widget_lock); err = hda_widget_sysfs_init(codec); + mutex_unlock(&codec->widget_lock); if (err < 0) { device_del(&codec->dev); return err; @@ -158,7 +161,9 @@ EXPORT_SYMBOL_GPL(snd_hdac_device_register); void snd_hdac_device_unregister(struct hdac_device *codec) { if (device_is_registered(&codec->dev)) { + mutex_lock(&codec->widget_lock); hda_widget_sysfs_exit(codec); + mutex_unlock(&codec->widget_lock); device_del(&codec->dev); 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) { + mutex_lock(&codec->widget_lock); err = hda_widget_sysfs_reinit(codec, start_nid, nums); + mutex_unlock(&codec->widget_lock); if (err < 0) return err; } diff --git a/sound/hda/hdac_sysfs.c b/sound/hda/hdac_sysfs.c index fb2aa344981e..909d5ef1179c 100644 --- a/sound/hda/hdac_sysfs.c +++ b/sound/hda/hdac_sysfs.c @@ -395,6 +395,7 @@ static int widget_tree_create(struct hdac_device *codec) return 0; } +/* call with codec->widget_lock held */ int hda_widget_sysfs_init(struct hdac_device *codec) { int err; @@ -411,11 +412,13 @@ int hda_widget_sysfs_init(struct hdac_device *codec) return 0; } +/* call with codec->widget_lock held */ void hda_widget_sysfs_exit(struct hdac_device *codec) { widget_tree_free(codec); } +/* call with codec->widget_lock held */ int hda_widget_sysfs_reinit(struct hdac_device *codec, hda_nid_t start_nid, int num_nodes) { -- 2.17.1