Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2537713pxj; Mon, 10 May 2021 05:27:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwNRlQeM7vo7i/JDVEWKLjT4yQdq7x3WggrflNY8h3jlFUceoeXXjy/AyR6Njrl7vywmW/T X-Received: by 2002:a05:6638:221:: with SMTP id f1mr21479390jaq.16.1620649633343; Mon, 10 May 2021 05:27:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620649633; cv=none; d=google.com; s=arc-20160816; b=0XvjoeB8f4yf0pRYOOOF5r4CZkZzeHiq0MGsrD9S8Fcp88ephh/+E1cmylqsk/WBUg bKthHCN8dAD7gqS+/iWlz4DvJDqCEjNMQ9ewIl3mmDoEd38xp+XBrIuGxsUi1Id30P9R 9CrFwT4vlGwz2V01oiHY5qRA828NSpPuAe7sadTLVslTgJkUkFWKCM7GPAfQ0a8kJRdT 0GcLQepnWZseAl8aRk/UVdZtptvtVUJVYEtGKaWz5rM6o28xg6+730dI2vPweIwkz55c 7E4Kp1q6CA7hQiN0vkC0YYQyz6pdjlqvomQfJc+4y7Gu+l4gfO0hPv7NvgmskqaJEUlh M3qg== 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=vZta4hNNJvDhqlBV/nlGct1RYz1h15iiMoEcKIyq4A4=; b=Nq+BFXkmE+4HUNfA8nYtDkb/1T2dg2kTJJXCNZ5m8qwOpAbQovw+EyRcZlus3MdZ0z gf/t4kfnuHB76MKsetQonCTRhc+csLAQZVLa67zeN35Iz970pi48FgB3hIgCcCE6c7Cz A/H347FzVGWJcKMfcT7W1/kTO3Obm9HRKPu3JkM3noZsxuITnWl3tceG6Wz6rFzdDbI6 6nXVgXrCyxtHlc2YCC2hx8CjP4Y/6udviAcL/Obuy0Gva8505I3dzG7RYFtrpMUE1Cac 5UnW3foICAcyD/zKoco4iSQtNR4aAaLr1WuVPNchEeXqzDIA+ZQZbUuZDOY1HzxLi5Cg Rk8A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=dDGZhB3z; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k13si17917255jav.44.2021.05.10.05.27.01; Mon, 10 May 2021 05:27:13 -0700 (PDT) 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=@linuxfoundation.org header.s=korg header.b=dDGZhB3z; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344799AbhEJMUW (ORCPT + 99 others); Mon, 10 May 2021 08:20:22 -0400 Received: from mail.kernel.org ([198.145.29.99]:45768 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234770AbhEJLJI (ORCPT ); Mon, 10 May 2021 07:09:08 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id A54E361924; Mon, 10 May 2021 11:04:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1620644668; bh=68IAiojGTqdlEEmbV+6OLViD4ckdZJ2dRbHFp8ldvfY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dDGZhB3zNqSDaccne02ZegyLxFdsBXCKSQWw6WwAKYT7lund//plv6lBjeNAUDgjx 48HDyS4vDBIHkXI0X2WCW9K/KacIht6EA3BsagGD177g5knoxt7t/v3a1EMN2UniLw TfzwFgsIq6c9ZUXCarCer+AOng8BSWB9PR6pAgP0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Hans de Goede , Andy Shevchenko , Charles Keepax , Chanwoo Choi , Lee Jones , Sasha Levin Subject: [PATCH 5.12 175/384] extcon: arizona: Fix various races on driver unbind Date: Mon, 10 May 2021 12:19:24 +0200 Message-Id: <20210510102020.658355522@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210510102014.849075526@linuxfoundation.org> References: <20210510102014.849075526@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Hans de Goede [ Upstream commit e5b499f6fb17bc95a813e85d0796522280203806 ] 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. Signed-off-by: Hans de Goede Reviewed-by: Andy Shevchenko Acked-by: Charles Keepax Tested-by: Charles Keepax Acked-by: Chanwoo Choi Signed-off-by: Lee Jones Signed-off-by: Sasha Levin --- 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.30.2