Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp3488720iob; Tue, 17 May 2022 00:42:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwbisvOIC9Gu0sHGM9igZm2TxZ2SatVF9KCp0mxdftL7f1fVkhGUPLARaeZ1CY53jjaFs1h X-Received: by 2002:a17:907:94cf:b0:6f5:942:5db7 with SMTP id dn15-20020a17090794cf00b006f509425db7mr18858751ejc.625.1652773373099; Tue, 17 May 2022 00:42:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652773373; cv=none; d=google.com; s=arc-20160816; b=qXa0MD6RK7UTD4xaGad52XIXdf/dK1CEyqQooAVISIyXs6ug0PdiGT+v4/Et7swSl1 pJyxKTl9Z1Iyw/U4mrdhAugdXsIhPTQiTK4pHHGuhf7r0lCko3znkD/9qEdolpthJvuk 5y+XwilnUWMWDDnOWC/ifFPiZ7lhYWy0afqnlizBygvJdDRyZaSgIo7HPVH6cVFu0vdr PfedCziBEzAA6zjnZu/ZeQ0+OlXFkgzYCqHIhqjGZ6Bca+hzK44KuYe1CQPS0nannn20 SXcyftZdI0hh7BD+VkU4B5sSawv471nT5mFnHSzfeQvl+oICot5434EXVdI8jT9Zh22f n/dQ== 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=m/nFmzTOH6c+X1vJp/Olw69QuJSFQiGDd075+7hLnMg=; b=xUkZAXY+7PYDpRRfFOfuN6seNtheFOu5H8yaCAdqq2pW5Y8iqY9ZouYNYVhebJij2L EbZ7qWOrDDkbUYM6SxkNfzcfCUa2XDWwPUd7P01RxUMuQE07S3RGXgN4zulJHWBgg6QS N8S/gosUB7eyQe7Sj7x3YLHDEZqvxSs6VA4elHXnno++Fg1W6lvKXGWwNQaX0DTdxhvS Wz1hu4zBNu1yc/Lat44p36w47UlTGAYqVW7fU10lFOnMPpvsKEXP9BjmU1zptRHRLAOE SMNKnzwNPwjaxpvj/pWORUTfKhun/2doWf4Ip5Q1oRQ91PwmBL2z95WhujSEzpvFg6Ps Jmjg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=SG1FGgVA; 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 oz44-20020a1709077dac00b006f3ed35a97dsi2095882ejc.63.2022.05.17.00.42.27; Tue, 17 May 2022 00:42:53 -0700 (PDT) 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=SG1FGgVA; 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 S1346987AbiEPUT2 (ORCPT + 99 others); Mon, 16 May 2022 16:19:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46776 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348882AbiEPT7C (ORCPT ); Mon, 16 May 2022 15:59:02 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E6DFCB3D; Mon, 16 May 2022 12:52:05 -0700 (PDT) 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 843ED60ABE; Mon, 16 May 2022 19:52:05 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 93105C385AA; Mon, 16 May 2022 19:52:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1652730725; bh=6rNJoLS/Ojh91LhmB/MjVAf98QJkadN8ugt+pWo3vKo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SG1FGgVAS1meurC8IpbR4Rvyk3qn4XZIKLMiBNWvVQLc0jh7GtnYkF/M9Ohjg3DMS DLP6zlgxiK3EHyUZZITYQYgX+vnp5aKUffu2wnm3F6EX5QhDnAsB6cBoKBjnQxbkaQ fXwvGLrquh5etkm5QrRwmvW8cLLqW72bAGWfLqEs= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Francesco Dolcini , Andrew Lunn , Jakub Kicinski Subject: [PATCH 5.15 088/102] net: phy: Fix race condition on link status change Date: Mon, 16 May 2022 21:37:02 +0200 Message-Id: <20220516193626.522008606@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220516193623.989270214@linuxfoundation.org> References: <20220516193623.989270214@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.4 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=unavailable 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: Francesco Dolcini commit 91a7cda1f4b8bdf770000a3b60640576dafe0cec upstream. This fixes the following error caused by a race condition between phydev->adjust_link() and a MDIO transaction in the phy interrupt handler. The issue was reproduced with the ethernet FEC driver and a micrel KSZ9031 phy. [ 146.195696] fec 2188000.ethernet eth0: MDIO read timeout [ 146.201779] ------------[ cut here ]------------ [ 146.206671] WARNING: CPU: 0 PID: 571 at drivers/net/phy/phy.c:942 phy_error+0x24/0x6c [ 146.214744] Modules linked in: bnep imx_vdoa imx_sdma evbug [ 146.220640] CPU: 0 PID: 571 Comm: irq/128-2188000 Not tainted 5.18.0-rc3-00080-gd569e86915b7 #9 [ 146.229563] Hardware name: Freescale i.MX6 Quad/DualLite (Device Tree) [ 146.236257] unwind_backtrace from show_stack+0x10/0x14 [ 146.241640] show_stack from dump_stack_lvl+0x58/0x70 [ 146.246841] dump_stack_lvl from __warn+0xb4/0x24c [ 146.251772] __warn from warn_slowpath_fmt+0x5c/0xd4 [ 146.256873] warn_slowpath_fmt from phy_error+0x24/0x6c [ 146.262249] phy_error from kszphy_handle_interrupt+0x40/0x48 [ 146.268159] kszphy_handle_interrupt from irq_thread_fn+0x1c/0x78 [ 146.274417] irq_thread_fn from irq_thread+0xf0/0x1dc [ 146.279605] irq_thread from kthread+0xe4/0x104 [ 146.284267] kthread from ret_from_fork+0x14/0x28 [ 146.289164] Exception stack(0xe6fa1fb0 to 0xe6fa1ff8) [ 146.294448] 1fa0: 00000000 00000000 00000000 00000000 [ 146.302842] 1fc0: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 [ 146.311281] 1fe0: 00000000 00000000 00000000 00000000 00000013 00000000 [ 146.318262] irq event stamp: 12325 [ 146.321780] hardirqs last enabled at (12333): [] __up_console_sem+0x50/0x60 [ 146.330013] hardirqs last disabled at (12342): [] __up_console_sem+0x3c/0x60 [ 146.338259] softirqs last enabled at (12324): [] __do_softirq+0x2c0/0x624 [ 146.346311] softirqs last disabled at (12319): [] __irq_exit_rcu+0x138/0x178 [ 146.354447] ---[ end trace 0000000000000000 ]--- With the FEC driver phydev->adjust_link() calls fec_enet_adjust_link() calls fec_stop()/fec_restart() and both these function reset and temporary disable the FEC disrupting any MII transaction that could be happening at the same time. fec_enet_adjust_link() and phy_read() can be running at the same time when we have one additional interrupt before the phy_state_machine() is able to terminate. Thread 1 (phylib WQ) | Thread 2 (phy interrupt) | | phy_interrupt() <-- PHY IRQ | handle_interrupt() | phy_read() | phy_trigger_machine() | --> schedule phylib WQ | | phy_state_machine() | phy_check_link_status() | phy_link_change() | phydev->adjust_link() | fec_enet_adjust_link() | --> FEC reset | phy_interrupt() <-- PHY IRQ | phy_read() | Fix this by acquiring the phydev lock in phy_interrupt(). Link: https://lore.kernel.org/all/20220422152612.GA510015@francesco-nb.int.toradex.com/ Fixes: c974bdbc3e77 ("net: phy: Use threaded IRQ, to allow IRQ from sleeping devices") cc: Signed-off-by: Francesco Dolcini Reviewed-by: Andrew Lunn Link: https://lore.kernel.org/r/20220506060815.327382-1-francesco.dolcini@toradex.com Signed-off-by: Jakub Kicinski Signed-off-by: Greg Kroah-Hartman --- drivers/net/phy/phy.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) --- a/drivers/net/phy/phy.c +++ b/drivers/net/phy/phy.c @@ -970,8 +970,13 @@ static irqreturn_t phy_interrupt(int irq { struct phy_device *phydev = phy_dat; struct phy_driver *drv = phydev->drv; + irqreturn_t ret; - return drv->handle_interrupt(phydev); + mutex_lock(&phydev->lock); + ret = drv->handle_interrupt(phydev); + mutex_unlock(&phydev->lock); + + return ret; } /**