Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1463052pxb; Sun, 17 Jan 2021 09:46:19 -0800 (PST) X-Google-Smtp-Source: ABdhPJzqwVqeU6McAMP6qXsIvEDmFQJrWy8Mm8u4oZxb6cAc/XaKXU3SJYj3Te7ynngfVV0qoNTF X-Received: by 2002:a17:906:eb46:: with SMTP id mc6mr11535771ejb.184.1610905579406; Sun, 17 Jan 2021 09:46:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610905579; cv=none; d=google.com; s=arc-20160816; b=fIIdwMMAltzvpPPYuvk+YlcX+uce2iXHN5bg7J0RChXnxbTU72f89HiRVwkr8HpHAR HTFCvKI3lmKWNK9PmVomlSUmb3R450BQCO29KxgKabKj0nQtumLNBxOBUMEuHdmZ9CzU OvNdFIxvHnBpOrxDwNmXZ2PumPT1SfE3BKw5Uy7TH7aBviEyFjeMq2IvIvjJEad58Qao 4iFpKFirAE2A4TtyQEqJ+7PX3sw8/OZxHyzEyQ756sQLW8TjTgJvM52LURTArSmy7bJw aJfDKiNrhfr6EENS/JQNjtAb7kFg0pCBzbROntEyEwCaUbCXNWnjH2QCmpDLHKtYPDWz sbvg== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=3GFr+M+b77IYgatau3bEnca3lkuiTNNdKCzNUpFVVAw=; b=g4a37udwCtMyNi9eGSi3cMCaGdAc1kmkRV+ru4y8UJL/8e6qzHee64FavP1bUG7KOE 80fzK+RhLnkRdHDaY0MR/MY+RzkX4fJQwhzlbc2I/UQXbFMGhfCh6P/0YONzEupUWAUg lsfcX+94weaoOLjQGb/EpcxxJD7j+aozuo2ofcnAWZqWpkSB2bh7UnwlNAK6nyVQa2aZ A7Q5nvwjMr0Aknc2hT1+DeVniV12OVkNrd5RMfGan1bkkOvNbq62krPnXn0FIcRRPGkU cUTl3Rw+0M/bbu1MX2zoLigH0dWAZPYyJBEU1lGGMOv6eoekVveVDKbJsyZdq7ntIcme iP8w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Dp09h7iS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y10si6107091ejb.351.2021.01.17.09.45.56; Sun, 17 Jan 2021 09:46:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Dp09h7iS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729504AbhAQQJ0 (ORCPT + 99 others); Sun, 17 Jan 2021 11:09:26 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:21392 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729123AbhAQQHs (ORCPT ); Sun, 17 Jan 2021 11:07:48 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1610899580; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3GFr+M+b77IYgatau3bEnca3lkuiTNNdKCzNUpFVVAw=; b=Dp09h7iS7hirs6GTJdxYmebM5Wvv4GupMClYCXconBb2evSeBMzGG/0BWWxdik4znqwDK3 xzLurdibDfNXdNsdgLa21912zojYFypZDsyKkzYkp45OMYKKtZ5WApzoBnvD+m3mmeN8TB ty+wFr00ApX+WW/hJhE+mGouF/RHFpE= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-250-ExUQhB0oN6itsCvZwi2XJg-1; Sun, 17 Jan 2021 11:06:17 -0500 X-MC-Unique: ExUQhB0oN6itsCvZwi2XJg-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 56DEA107ACE3; Sun, 17 Jan 2021 16:06:15 +0000 (UTC) Received: from x1.localdomain.com (ovpn-112-7.ams2.redhat.com [10.36.112.7]) by smtp.corp.redhat.com (Postfix) with ESMTP id 28C9E5C233; Sun, 17 Jan 2021 16:06:13 +0000 (UTC) From: Hans de Goede To: Lee Jones , Cezary Rojewski , Pierre-Louis Bossart , Liam Girdwood , Jie Yang , Mark Brown Cc: Hans de Goede , patches@opensource.cirrus.com, linux-kernel@vger.kernel.org, Andy Shevchenko , Charles Keepax , alsa-devel@alsa-project.org Subject: [PATCH v2 04/12] ASoC: arizona-jack: Fix various races on driver unbind Date: Sun, 17 Jan 2021 17:05:47 +0100 Message-Id: <20210117160555.78376-5-hdegoede@redhat.com> In-Reply-To: <20210117160555.78376-1-hdegoede@redhat.com> References: <20210117160555.78376-1-hdegoede@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org We must free/disable all interrupts and cancel all pending works before doing further cleanup. Before this commit arizona_extcon_remove() was doing several register writes to shut things down before disabling the IRQs and it was cancelling only 1 of the 3 different works used. Move all the register-writes shutting things down to after the disabling of the IRQs and add the 2 missing cancel_delayed_work_sync() calls. This fixes various possible races on driver unbind. One of which would always trigger on devices using the mic-clamp feature for jack detection. The ARIZONA_MICD_CLAMP_MODE_MASK update was done before disabling the IRQs, causing: 1. arizona_jackdet() to run 2. detect a jack being inserted (clamp disabled means jack inserted) 3. call arizona_start_mic() which: 3.1 Enables the MICVDD regulator 3.2 takes a pm_runtime_reference And this was all happening after the ARIZONA_MICD_ENA bit clearing, which would undo 3.1 and 3.2 because the ARIZONA_MICD_CLAMP_MODE_MASK update was being done after the ARIZONA_MICD_ENA bit clearing. So this means that arizona_extcon_remove() would exit with 1. MICVDD enabled and 2. The pm_runtime_reference being unbalanced. MICVDD still being enabled caused the following oops when the regulator is released by the devm framework: [ 2850.745757] ------------[ cut here ]------------ [ 2850.745827] WARNING: CPU: 2 PID: 2098 at drivers/regulator/core.c:2123 _regulator_put.part.0+0x19f/0x1b0 [ 2850.745835] Modules linked in: extcon_arizona ... ... [ 2850.746909] Call Trace: [ 2850.746932] regulator_put+0x2d/0x40 [ 2850.746946] release_nodes+0x22a/0x260 [ 2850.746984] __device_release_driver+0x190/0x240 [ 2850.747002] driver_detach+0xd4/0x120 ... [ 2850.747337] ---[ end trace f455dfd7abd9781f ]--- Note this oops is just one of various theoretically possible races caused by the wrong ordering inside arizona_extcon_remove(), this fixes the ordering fixing all possible races, including the reported oops. Reviewed-by: Andy Shevchenko Acked-by: Charles Keepax Signed-off-by: Hans de Goede --- sound/soc/codecs/arizona-jack.c | 40 +++++++++++++++++---------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/sound/soc/codecs/arizona-jack.c b/sound/soc/codecs/arizona-jack.c index f7ef247de46a..76aacbac5869 100644 --- a/sound/soc/codecs/arizona-jack.c +++ b/sound/soc/codecs/arizona-jack.c @@ -1760,25 +1760,6 @@ static int arizona_extcon_remove(struct platform_device *pdev) bool change; int ret; - ret = regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1, - ARIZONA_MICD_ENA, 0, - &change); - if (ret < 0) { - dev_err(&pdev->dev, "Failed to disable micd on remove: %d\n", - ret); - } else if (change) { - regulator_disable(info->micvdd); - pm_runtime_put(info->dev); - } - - gpiod_put(info->micd_pol_gpio); - - pm_runtime_disable(&pdev->dev); - - regmap_update_bits(arizona->regmap, - ARIZONA_MICD_CLAMP_CONTROL, - ARIZONA_MICD_CLAMP_MODE_MASK, 0); - if (info->micd_clamp) { jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE; jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL; @@ -1794,10 +1775,31 @@ static int arizona_extcon_remove(struct platform_device *pdev) arizona_free_irq(arizona, jack_irq_rise, info); arizona_free_irq(arizona, jack_irq_fall, info); cancel_delayed_work_sync(&info->hpdet_work); + cancel_delayed_work_sync(&info->micd_detect_work); + cancel_delayed_work_sync(&info->micd_timeout_work); + + ret = regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1, + ARIZONA_MICD_ENA, 0, + &change); + if (ret < 0) { + dev_err(&pdev->dev, "Failed to disable micd on remove: %d\n", + ret); + } else if (change) { + regulator_disable(info->micvdd); + pm_runtime_put(info->dev); + } + + regmap_update_bits(arizona->regmap, + ARIZONA_MICD_CLAMP_CONTROL, + ARIZONA_MICD_CLAMP_MODE_MASK, 0); regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE, ARIZONA_JD1_ENA, 0); arizona_clk32k_disable(arizona); + gpiod_put(info->micd_pol_gpio); + + pm_runtime_disable(&pdev->dev); + return 0; } -- 2.28.0