Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp479909rdb; Fri, 8 Sep 2023 06:50:53 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFlKGV1ubgb8xplRh8/0iHqABHmIVNJtyenS0JCCDpYefYUzR5zkkpkNlaSy3oNFEO26nr6 X-Received: by 2002:a05:6a21:35c7:b0:132:965d:5323 with SMTP id ba7-20020a056a2135c700b00132965d5323mr2526284pzc.33.1694181052969; Fri, 08 Sep 2023 06:50:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694181052; cv=none; d=google.com; s=arc-20160816; b=rBo53ahxvGlkMmphQlKIop4h9ak225HWRZ/xfmvrzGswh0/frnipJyE/H/5IHmHj1B dGNYWSRayRMolU480sfSDYRJPEXrILpWn6aJR5sravHU6FKe7AGI9PtymW+NW9nr/QM3 vZF3oZ/qv7EW7oIkNombE3nxwQUU5c7bmdl/1SB0En3kPsxFGMWlbt5Bvz1nsb+wlfVy f88Tkw7Qz6Mnb+79ydHuQhCge/s45ItjR27VynjSAm5PruPUKCLUP9I1bc+DKqkhdsnz 86hpzN3MCsZfpKJVmLjICZWs6i9QiRwaPYQ+7bfHFBOb6rHiBMFwxydGikO+RJZX8JBt /Inw== 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 :message-id:date:subject:cc:to:from; bh=sj5vhBj54eUxkBIknEgzvXuDQhWHr29yRi+RrAJnZjU=; fh=Bv+99m6pZ+GwPON2RYJDYeW+uAizqgZxQ3ym/5OCZ/Q=; b=rOv/IoG/Kj38S3nEwsdrw2+farSX/QanU01PfrkNLGhSd7mSZR3E+4psLO6JIKlF1q ZUCZX3XrPwRGT4nsydzE+Mjzl+A7N73Z2Ql9MRvIU3UtfUOPmwdSyygvYEKV6nLbcVmE aMMa9zVxRcXdDlsIy4Qo1N4SZWORmG+djKkplVhR3fM1s1czCk5K11lhHqZ5nH6orVIy 6IdXRRyVxtViwsXNdsGNDvWbQh1CCY3M/uU1P1uZUAC/CURFc8p7qjlje4YIHMo6z59U SWL/+PD5PWQSmYwR5qiABAE/w0X4oOc/sfhFWVzG5w+hjJiAZGTqTL+xaxMOHbs9/AQN +UHA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d11-20020a63ed0b000000b0056a670ebd73si1499712pgi.498.2023.09.08.06.50.37; Fri, 08 Sep 2023 06:50:52 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240909AbjIHL3c (ORCPT + 99 others); Fri, 8 Sep 2023 07:29:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59118 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231164AbjIHL3b (ORCPT ); Fri, 8 Sep 2023 07:29:31 -0400 Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [IPv6:2a0a:edc0:2:b01:1d::104]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 22A221BE7 for ; Fri, 8 Sep 2023 04:29:27 -0700 (PDT) Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qeZfu-0005mN-Ih; Fri, 08 Sep 2023 13:29:22 +0200 Received: from [2a0a:edc0:0:1101:1d::28] (helo=dude02.red.stw.pengutronix.de) by drehscheibe.grey.stw.pengutronix.de with esmtp (Exim 4.94.2) (envelope-from ) id 1qeZft-004roo-SJ; Fri, 08 Sep 2023 13:29:21 +0200 Received: from sha by dude02.red.stw.pengutronix.de with local (Exim 4.94.2) (envelope-from ) id 1qeZfs-0078i4-W9; Fri, 08 Sep 2023 13:29:21 +0200 From: Sascha Hauer To: netdev@vger.kernel.org Cc: Russell King , linux-kernel@vger.kernel.org, Nicolas Ferre , Claudiu Beznea , kernel@pengutronix.de, Sascha Hauer Subject: [PATCH] net: macb: fix sleep inside spinlock Date: Fri, 8 Sep 2023 13:29:13 +0200 Message-Id: <20230908112913.1701766-1-s.hauer@pengutronix.de> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: sha@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS 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 macb_set_tx_clk() is called under a spinlock but itself calls clk_set_rate() which can sleep. This results in: | BUG: sleeping function called from invalid context at kernel/locking/mutex.c:580 | pps pps1: new PPS source ptp1 | in_atomic(): 1, irqs_disabled(): 1, non_block: 0, pid: 40, name: kworker/u4:3 | preempt_count: 1, expected: 0 | RCU nest depth: 0, expected: 0 | 4 locks held by kworker/u4:3/40: | #0: ffff000003409148 | macb ff0c0000.ethernet: gem-ptp-timer ptp clock registered. | ((wq_completion)events_power_efficient){+.+.}-{0:0}, at: process_one_work+0x14c/0x51c | #1: ffff8000833cbdd8 ((work_completion)(&pl->resolve)){+.+.}-{0:0}, at: process_one_work+0x14c/0x51c | #2: ffff000004f01578 (&pl->state_mutex){+.+.}-{4:4}, at: phylink_resolve+0x44/0x4e8 | #3: ffff000004f06f50 (&bp->lock){....}-{3:3}, at: macb_mac_link_up+0x40/0x2ac | irq event stamp: 113998 | hardirqs last enabled at (113997): [] _raw_spin_unlock_irq+0x30/0x64 | hardirqs last disabled at (113998): [] _raw_spin_lock_irqsave+0xac/0xc8 | softirqs last enabled at (113608): [] __do_softirq+0x430/0x4e4 | softirqs last disabled at (113597): [] ____do_softirq+0x10/0x1c | CPU: 0 PID: 40 Comm: kworker/u4:3 Not tainted 6.5.0-11717-g9355ce8b2f50-dirty #368 | Hardware name: ... ZynqMP ... (DT) | Workqueue: events_power_efficient phylink_resolve | Call trace: | dump_backtrace+0x98/0xf0 | show_stack+0x18/0x24 | dump_stack_lvl+0x60/0xac | dump_stack+0x18/0x24 | __might_resched+0x144/0x24c | __might_sleep+0x48/0x98 | __mutex_lock+0x58/0x7b0 | mutex_lock_nested+0x24/0x30 | clk_prepare_lock+0x4c/0xa8 | clk_set_rate+0x24/0x8c | macb_mac_link_up+0x25c/0x2ac | phylink_resolve+0x178/0x4e8 | process_one_work+0x1ec/0x51c | worker_thread+0x1ec/0x3e4 | kthread+0x120/0x124 | ret_from_fork+0x10/0x20 The obvious fix is to move the call to macb_set_tx_clk() out of the protected area. This seems safe as rx and tx are both disabled anyway at this point. It is however not entirely clear what the spinlock shall protect. It could be the read-modify-write access to the NCFGR register, but this is accessed in macb_set_rx_mode() and macb_set_rxcsum_feature() as well without holding the spinlock. It could also be the register accesses done in mog_init_rings() or macb_init_buffers(), but again these functions are called without holding the spinlock in macb_hresp_error_task(). The locking seems fishy in this driver and it might deserve another look before this patch is applied. Fixes: 633e98a711ac0 ("net: macb: use resolved link config in mac_link_up()") Signed-off-by: Sascha Hauer --- drivers/net/ethernet/cadence/macb_main.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/net/ethernet/cadence/macb_main.c b/drivers/net/ethernet/cadence/macb_main.c index 31f664ee4d778..b940dcd3ace68 100644 --- a/drivers/net/ethernet/cadence/macb_main.c +++ b/drivers/net/ethernet/cadence/macb_main.c @@ -756,8 +756,6 @@ static void macb_mac_link_up(struct phylink_config *config, if (rx_pause) ctrl |= MACB_BIT(PAE); - macb_set_tx_clk(bp, speed); - /* Initialize rings & buffers as clearing MACB_BIT(TE) in link down * cleared the pipeline and control registers. */ @@ -777,6 +775,9 @@ static void macb_mac_link_up(struct phylink_config *config, spin_unlock_irqrestore(&bp->lock, flags); + if (!(bp->caps & MACB_CAPS_MACB_IS_EMAC)) + macb_set_tx_clk(bp, speed); + /* Enable Rx and Tx; Enable PTP unicast */ ctrl = macb_readl(bp, NCR); if (gem_has_ptp(bp)) -- 2.39.2