Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1740975pxb; Sat, 23 Jan 2021 04:17:16 -0800 (PST) X-Google-Smtp-Source: ABdhPJyTzQRYLBG3AVg8Nl5TjbJ3UiYpAxGAR0i0r/eUMzVN2+FD2RKu2uM1gTsFP2apTyyd1OBY X-Received: by 2002:a50:84e7:: with SMTP id 94mr1951521edq.87.1611404236314; Sat, 23 Jan 2021 04:17:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611404236; cv=none; d=google.com; s=arc-20160816; b=JdEHqnDO6Iflc2O2/HRU39cGONyuEhpvXt9FXZPkZUdyT2Id7zqureoeCIR+TiDrWB +Cp90SXNTQ9SNg6ajacfsMHpxUS/RyLzfJX8lyRIpWDV27z1qaddXHH++rkVHTWEOiOJ LDQVmrqyQ3DXpJf9ltTb3j4YbDDQQXvLGI1ymVSdKNDXkmVxuopu6za+4trhxVUYGRJ2 O2ZOBnEowSGJv+LzN1zvMwmnVm9fB1ueG9ChyV6K0p7aQUxbwgB8lBzkjNd5IrLu5j3+ HgItDa8cCtAb4PJjfefUumQnvpZSv2yAxu7ld3qgljWujvuH03JZ9/sCKX0zL9Oke7V2 /67g== 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=www6MexSVUEbc0LeWPO4UbvkjxCqTT9CBJi8I/PSmvc=; b=vZ1V9laVJM9p6yScjVfGDfN3hqlMRC93Ja9NIxAf8Fv6a+ow82+HpLke/GsO7QD9IU mqC/BrugYweyNWDhqIXC/9/zvP8UXCF+K/WQf20I7m/zPAeIBTUG6tojuvBH4f/yQEQ4 abz3UD5zqwSvqAevnnucGcFJxk/p6T8V67zZsvRKIZSCUB92uMfLWhNofer8SqQ4IFxB FQ5IO9djtJGrKnaj6Bf5Dzso6k2XfbG5joiGhqrOHP5s89Bvd4Qt0LknyGsjMkoBiL1p ORJtT93uVX6Ob9A+M+OAOCgT4bQ/eqfqxSdg6u7X9hBAkd04A+Wl6wFpbmvyyqt0Bvmh tNwA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=IDNpAiEa; 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 e22si5080279edu.45.2021.01.23.04.16.52; Sat, 23 Jan 2021 04:17:16 -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=IDNpAiEa; 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 S1726929AbhAWMPe (ORCPT + 99 others); Sat, 23 Jan 2021 07:15:34 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:55590 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726700AbhAWMO5 (ORCPT ); Sat, 23 Jan 2021 07:14:57 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1611404009; 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=www6MexSVUEbc0LeWPO4UbvkjxCqTT9CBJi8I/PSmvc=; b=IDNpAiEar1iO1yy6K5rtYylxeKogTN7weMXGbgYyRQ8g69P1pZ0k7DQfzmC2+p2rbY2JVy 7Ykt9A4wPB/u82vt3G2dkbtPvQ5DU/HlsMPC+G3Q+H1bbPMt1JUriwiotHr+XvN6yrbVdB Uune6XOOGpSQtBM17V8fkMlXIQoA+9Q= 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-18-eMptoEOfMYS4yX3BpSnHfw-1; Sat, 23 Jan 2021 07:13:26 -0500 X-MC-Unique: eMptoEOfMYS4yX3BpSnHfw-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 83266E74F; Sat, 23 Jan 2021 12:13:24 +0000 (UTC) Received: from x1.localdomain (ovpn-112-82.ams2.redhat.com [10.36.112.82]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4D206189B8; Sat, 23 Jan 2021 12:13:22 +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 v4 03/13] extcon: arizona: Fix various races on driver unbind Date: Sat, 23 Jan 2021 13:13:03 +0100 Message-Id: <20210123121313.79530-4-hdegoede@redhat.com> In-Reply-To: <20210123121313.79530-1-hdegoede@redhat.com> References: <20210123121313.79530-1-hdegoede@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 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 --- drivers/extcon/extcon-arizona.c | 40 +++++++++++++++++---------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/drivers/extcon/extcon-arizona.c b/drivers/extcon/extcon-arizona.c index f7ef247de46a..76aacbac5869 100644 --- a/drivers/extcon/extcon-arizona.c +++ b/drivers/extcon/extcon-arizona.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.29.2