Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp8619800pxu; Sun, 27 Dec 2020 13:16:15 -0800 (PST) X-Google-Smtp-Source: ABdhPJwet4vXLSxiO8XD86/4nRValz+/V/pDyUWjpVeOAihgD6yZw5pckdIE/vc2TyngcfIZ4Zhg X-Received: by 2002:a17:906:f949:: with SMTP id ld9mr33631272ejb.401.1609103775155; Sun, 27 Dec 2020 13:16:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609103775; cv=none; d=google.com; s=arc-20160816; b=O+3ol3oV/Ivv4d8Vw4FBkdc6Yde3aO02w5u7bwWNwSbXvCNbaNTLvTPQBn+1WSG5PS zY4ZS0oDU+9S/wpH2kUyajgypHGfP5zDK+0e4dji1GagdjBHJsQ/o14bwE+35Hhrv5g6 Z42e7fHuf0OF2z7GBcdiorQUfKRp/C0uSr2kBtaE33Ropidj+XXw6eHhpcbXOcchZ4zR 8YVG3QhYSlCZjv5W4roYsAeVCb13dZ4btS01fMMX0yHuvRSs3GsepGW844JdKy1AtIJM OIoE1cRIJbygZvO6Vx5ntK+ne0Btve9gFhcbGmmO2cxV8vzrP+aoOaTFNgidCk36WoRz WVRQ== 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=5Qte4LXTNVsCbRV8fe0PWojTp633/OkxgsioYNfV7Wc=; b=mYhSzrgjE1cAz49o88Mc4p9C3n8/zQLNb/0m9cURad+61os7qf5PRltcr2erhQIzLd zZHQwxSiaTLyP+0U72FYUsoLKJxnKk/e5fOVisSaDvyOWAMYot7crrYA4golS6vj+TgW 0t1xae3RF/k4bwhirciBAGGW53pAPmXwfX+wZ1EZq7CGXySyLef7KF7XBc1nIrh9ZG5R kg/+bUVhZfIz7u5Dpxmofxi0U5K2ctq3admdQTUialI8eXlCzMw/B4nQHGz1IuS0hbu3 UwiKBWrlAgIjcWaHpH09VFhomJE1Q7iaxib0qvrrGsP4gDcmmlgwj6xu1VJZjiCXTJZa WeaA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=YNwqHECl; 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 h14si17492422ejt.198.2020.12.27.13.15.53; Sun, 27 Dec 2020 13:16:15 -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=YNwqHECl; 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 S1726529AbgL0VOW (ORCPT + 99 others); Sun, 27 Dec 2020 16:14:22 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:28251 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726508AbgL0VOW (ORCPT ); Sun, 27 Dec 2020 16:14:22 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1609103575; 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=5Qte4LXTNVsCbRV8fe0PWojTp633/OkxgsioYNfV7Wc=; b=YNwqHEClNoJOMtxyYexoPzwNOmkZYuXAFs8QaAEphyuTHg0eNF2u/jsKmeflnuZ+VnC1gb OrpDnAlFFN/+J7upWGgqkVgkgh7RW69iGW+CJakzXFFJdq7zE6ZjX0jT7xYA2Hqd/Beuig /JSeSjwid3ToJMWFuJOvbRRUVdIlE4s= 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-498-yqQfchauPbSla8VsEfOAMw-1; Sun, 27 Dec 2020 16:12:53 -0500 X-MC-Unique: yqQfchauPbSla8VsEfOAMw-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9F243800D55; Sun, 27 Dec 2020 21:12:51 +0000 (UTC) Received: from x1.localdomain.com (ovpn-112-25.ams2.redhat.com [10.36.112.25]) by smtp.corp.redhat.com (Postfix) with ESMTP id 71C291ACB9; Sun, 27 Dec 2020 21:12:49 +0000 (UTC) From: Hans de Goede To: Lee Jones , MyungJoo Ham , Chanwoo Choi , Cezary Rojewski , Pierre-Louis Bossart , Liam Girdwood , Jie Yang , Mark Brown Cc: Hans de Goede , patches@opensource.cirrus.com, linux-kernel@vger.kernel.org, alsa-devel@alsa-project.org Subject: [PATCH 06/14] extcon: arizona: Fix various races on driver unbind Date: Sun, 27 Dec 2020 22:12:24 +0100 Message-Id: <20201227211232.117801-7-hdegoede@redhat.com> In-Reply-To: <20201227211232.117801-1-hdegoede@redhat.com> References: <20201227211232.117801-1-hdegoede@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 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. 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.28.0