Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp1189132pxb; Tue, 8 Feb 2022 11:11:51 -0800 (PST) X-Google-Smtp-Source: ABdhPJytWSSk2poj+HZCsBEE4/OTppV59JdA1Nzb1AX6mfxSgiEjC3lKg69BM7+dBBKMhDYbLo52 X-Received: by 2002:a17:902:e74a:: with SMTP id p10mr5950263plf.16.1644347510858; Tue, 08 Feb 2022 11:11:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644347510; cv=none; d=google.com; s=arc-20160816; b=r3AYNll2+HW+teRzmgQM1V+r4K5N/rQgKTBg8OUaSlqleVwglq16//rvRSPM289bRu hMEiW3AxpKkbA49HD8upqQqeP8DmfHoan0FizXwdo+HW6+I6TdSoI2vdJZUntQEChFec ZcVZSmmjdIhqK/Z/yx2xzgU77T4EqVJn+oOrLdjTfjnOPCwq4yfmlHkKBtKnfF/I2VF6 8tXFXJ6JxZExLpbaIydrSb4+YDn1wwtWMrezfSdGY5O2KWR7xKWH0rNn5Fpauw7/tpTH foz4oQGbpxynzJfP5Bo3vl+abOTWt5i4vFtFoRqLbWJwTFVSe1O1Ht0jAxlKBYzbCtuo cbJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=FzRjZUtdabv+BFUSt/Ryns+LFxW8oIflG6yEHqZh22E=; b=fntdp6jFrv8EgFy+NjrCqxNxkBF2me1nM3sIXJd7k6kwjcC9L+MHsEzarBcPu+IGM+ o9wVqsSvP2WLj/oLjFPTQiSRZKVg/nppzHEH+uDuRmSlrdFYroeVONXDh08s7dtv5y95 UIaE2D7B479ZD/0F7buLLlJy83tsJHYECi68caY0Vslt49mlJrqKeb4HB+pevGt4uv1T vYwo7Wkb3LowtnVuPvOKRCAOEICts4bO/LKZu/ej+xQHpv95hUVH4QXgoqPS7qwK4Wei TSg1mMRB5b/wSr5qegUFSXn/rpp8wZpFCPBMu7VQLh4pWgw6oyPf2B92R0nNEWt2L1ck khtA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=IMxqTPub; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r72si13540393pgr.218.2022.02.08.11.11.38; Tue, 08 Feb 2022 11:11:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=IMxqTPub; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1385812AbiBGLcm (ORCPT + 99 others); Mon, 7 Feb 2022 06:32:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33768 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1383108AbiBGLVh (ORCPT ); Mon, 7 Feb 2022 06:21:37 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 75C4AC0401D9; Mon, 7 Feb 2022 03:21:21 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 06BE26126D; Mon, 7 Feb 2022 11:21:21 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D6906C004E1; Mon, 7 Feb 2022 11:21:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1644232880; bh=DNdWn2flkb8w9nBNpBs9iVhmk+7EkEXEnkSrwUo7IMM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IMxqTPub1JI6bAmCGCGuwd7CCq4neR5sfYnJRLiDKatTy7+Mj3bNLizLa+mnwnk4Z VCvFeKDZfXAfk27NZdrFpHHDOU9AepHgaC77hxHp8llS0CvXu+k8WFuxoRDs5FEQCO ig5k7f4K8cGV0GyPqGOcDVxb/gxL6DgAYkLtbfK4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Alexander Sergeyev , Takashi Iwai Subject: [PATCH 5.10 08/74] ALSA: hda: realtek: Fix race at concurrent COEF updates Date: Mon, 7 Feb 2022 12:06:06 +0100 Message-Id: <20220207103757.509087979@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220207103757.232676988@linuxfoundation.org> References: <20220207103757.232676988@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Takashi Iwai commit b837a9f5ab3bdfab9233c9f98a6bef717673a3e5 upstream. The COEF access is done with two steps: setting the index then read or write the data. When multiple COEF accesses are performed concurrently, the index and data might be paired unexpectedly. In most cases, this isn't a big problem as the COEF setup is done at the initialization, but some dynamic changes like the mute LED may hit such a race. For avoiding the racy COEF accesses, this patch introduces a new mutex coef_mutex to alc_spec, and wrap the COEF accessing functions with it. Reported-by: Alexander Sergeyev Cc: Link: https://lore.kernel.org/r/20220111195229.a77wrpjclqwrx4bx@localhost.localdomain Link: https://lore.kernel.org/r/20220131075738.24323-1-tiwai@suse.de Signed-off-by: Takashi Iwai Signed-off-by: Greg Kroah-Hartman --- sound/pci/hda/patch_realtek.c | 61 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 11 deletions(-) --- a/sound/pci/hda/patch_realtek.c +++ b/sound/pci/hda/patch_realtek.c @@ -97,6 +97,7 @@ struct alc_spec { unsigned int gpio_mic_led_mask; struct alc_coef_led mute_led_coef; struct alc_coef_led mic_led_coef; + struct mutex coef_mutex; hda_nid_t headset_mic_pin; hda_nid_t headphone_mic_pin; @@ -133,8 +134,8 @@ struct alc_spec { * COEF access helper functions */ -static int alc_read_coefex_idx(struct hda_codec *codec, hda_nid_t nid, - unsigned int coef_idx) +static int __alc_read_coefex_idx(struct hda_codec *codec, hda_nid_t nid, + unsigned int coef_idx) { unsigned int val; @@ -143,28 +144,61 @@ static int alc_read_coefex_idx(struct hd return val; } +static int alc_read_coefex_idx(struct hda_codec *codec, hda_nid_t nid, + unsigned int coef_idx) +{ + struct alc_spec *spec = codec->spec; + unsigned int val; + + mutex_lock(&spec->coef_mutex); + val = __alc_read_coefex_idx(codec, nid, coef_idx); + mutex_unlock(&spec->coef_mutex); + return val; +} + #define alc_read_coef_idx(codec, coef_idx) \ alc_read_coefex_idx(codec, 0x20, coef_idx) -static void alc_write_coefex_idx(struct hda_codec *codec, hda_nid_t nid, - unsigned int coef_idx, unsigned int coef_val) +static void __alc_write_coefex_idx(struct hda_codec *codec, hda_nid_t nid, + unsigned int coef_idx, unsigned int coef_val) { snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_COEF_INDEX, coef_idx); snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PROC_COEF, coef_val); } +static void alc_write_coefex_idx(struct hda_codec *codec, hda_nid_t nid, + unsigned int coef_idx, unsigned int coef_val) +{ + struct alc_spec *spec = codec->spec; + + mutex_lock(&spec->coef_mutex); + __alc_write_coefex_idx(codec, nid, coef_idx, coef_val); + mutex_unlock(&spec->coef_mutex); +} + #define alc_write_coef_idx(codec, coef_idx, coef_val) \ alc_write_coefex_idx(codec, 0x20, coef_idx, coef_val) +static void __alc_update_coefex_idx(struct hda_codec *codec, hda_nid_t nid, + unsigned int coef_idx, unsigned int mask, + unsigned int bits_set) +{ + unsigned int val = __alc_read_coefex_idx(codec, nid, coef_idx); + + if (val != -1) + __alc_write_coefex_idx(codec, nid, coef_idx, + (val & ~mask) | bits_set); +} + static void alc_update_coefex_idx(struct hda_codec *codec, hda_nid_t nid, unsigned int coef_idx, unsigned int mask, unsigned int bits_set) { - unsigned int val = alc_read_coefex_idx(codec, nid, coef_idx); + struct alc_spec *spec = codec->spec; - if (val != -1) - alc_write_coefex_idx(codec, nid, coef_idx, - (val & ~mask) | bits_set); + mutex_lock(&spec->coef_mutex); + __alc_update_coefex_idx(codec, nid, coef_idx, mask, bits_set); + mutex_unlock(&spec->coef_mutex); } #define alc_update_coef_idx(codec, coef_idx, mask, bits_set) \ @@ -197,13 +231,17 @@ struct coef_fw { static void alc_process_coef_fw(struct hda_codec *codec, const struct coef_fw *fw) { + struct alc_spec *spec = codec->spec; + + mutex_lock(&spec->coef_mutex); for (; fw->nid; fw++) { if (fw->mask == (unsigned short)-1) - alc_write_coefex_idx(codec, fw->nid, fw->idx, fw->val); + __alc_write_coefex_idx(codec, fw->nid, fw->idx, fw->val); else - alc_update_coefex_idx(codec, fw->nid, fw->idx, - fw->mask, fw->val); + __alc_update_coefex_idx(codec, fw->nid, fw->idx, + fw->mask, fw->val); } + mutex_unlock(&spec->coef_mutex); } /* @@ -1160,6 +1198,7 @@ static int alc_alloc_spec(struct hda_cod codec->spdif_status_reset = 1; codec->forced_resume = 1; codec->patch_ops = alc_patch_ops; + mutex_init(&spec->coef_mutex); err = alc_codec_rename_from_preset(codec); if (err < 0) {