Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp2495113pxu; Mon, 7 Dec 2020 08:03:12 -0800 (PST) X-Google-Smtp-Source: ABdhPJx7P2niJoXiE59Q5Ksu+T43Inix5zCM2KYmE9Jijfc9tp1kU3StApeXGjyjqAu7O44/XKEg X-Received: by 2002:a17:907:9691:: with SMTP id hd17mr3958828ejc.306.1607356992095; Mon, 07 Dec 2020 08:03:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607356992; cv=none; d=google.com; s=arc-20160816; b=BbevDAF1TrU9fInT8ooGv9M2FCAV+/7O73KipLm2J/TM/7JGHjOjUIvE1PzX32h9xM rwsypon9pG+dyoQ6OfsCbKABqOTe2v3SI53yMuSPwGjN0pFlaCKslXTYdN4FEqD9Cltj pwJ16YSZcC7rVVJZ66dK8FjlMX0gxhqg4XB3yp+Cjp+2JlCpPraCuCoc0grUbAlvj17u UNLru7iyBLQkB0/kkwGA65lrpIzgt0eK8MpFxM9YWzrDIUorPyLTrugwlp1Mx0Rw3tgD NoktFyR6j9mM3dwtqLOQTSBjHlL20SDYMIveaYxwiOWozS5cz3NvNuQ5CzO0TPhjp5W9 Rtew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version; bh=b9ZSSIcD36/XpL2N+IfRn8af/auYdeq7gF3iKNSLdNM=; b=fxYwBEzaK1VopI6v88U6CBvYHjyWh1N2WfRX4P2QuZvpH4v4zt6mhUwi9jpzQBBQak 0uTok5W+sqMC5PrsVqxkHkAfzQAONe5gBcNSTE/5moMe9eBLl6Y5axAp8qzJ06fznRGO DM/PZf0dpuZgIXd0zJthmX9kqTlf6yxlVP2aah6bEliXl9QHQaN+2VSEn4mFktX3dH+Y IXwc6RLMMFgs/CWhMGM+d4TI6rWh0+N+AvzDqzXHmdSyj2yLGkYo4gJd0QbSHEwl7T0A A4nDRBZxww2V2sECSwIW6VM/GLrE4lMEi/W/3BrIsKgajfIfzpZ6sgxQ9hbRndmjs4yV shtw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ch18si6762202ejb.736.2020.12.07.08.02.38; Mon, 07 Dec 2020 08:03:12 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727377AbgLGP6c convert rfc822-to-8bit (ORCPT + 99 others); Mon, 7 Dec 2020 10:58:32 -0500 Received: from mail-oo1-f65.google.com ([209.85.161.65]:34302 "EHLO mail-oo1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726167AbgLGP6b (ORCPT ); Mon, 7 Dec 2020 10:58:31 -0500 Received: by mail-oo1-f65.google.com with SMTP id t63so734019ooa.1; Mon, 07 Dec 2020 07:58:09 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=oIC9pRuaPIJn4lXLFyyVqpM55/v1ynsM88+vIja88PQ=; b=sc/KMzxKjc4CwyTdRzmKgnsrlRQAoMZT/eaXXEO4h3kX3rIyN7s666RXoKp5xeIfbl uUF3KpUeW7m3pd5pmGA4FJ9oULTLaVeK7oWU0bUrvjsC56ViF9nOu3cWssldeHE5ZBYI KK5Mk5FbGVteyTdYulDTgnh6Nfy48mQ5fVjSB1C1avUsQMvNRZomCXW8TDMs9vop9Vil 0C5LvrEUsfgl43E6VS3KiZ2sWamfL6bFCTwTVpq01+ibDVe9A65YK+vbbrQGUm4aMQ3f ebd8zVoyNQLN4R2aKG/mQfe5DdH7FW4LANXEkm78U+bfW3UDFpYL07OfF5phXVWHZBsG 6PaQ== X-Gm-Message-State: AOAM53296z9t71AyWVKO0to0UVFxLw0iIZps37pmEdp0Q+Wr4zERsuSC tHXBcc7lkCX/SXaJHOYdt97c+QhlyDF+gml3BlA= X-Received: by 2002:a4a:abc9:: with SMTP id o9mr13306587oon.1.1607356664371; Mon, 07 Dec 2020 07:57:44 -0800 (PST) MIME-Version: 1.0 References: <20201205021921.1456190-1-niklas.soderlund+renesas@ragnatech.se> <20201205021921.1456190-2-niklas.soderlund+renesas@ragnatech.se> In-Reply-To: <20201205021921.1456190-2-niklas.soderlund+renesas@ragnatech.se> From: Geert Uytterhoeven Date: Mon, 7 Dec 2020 16:57:33 +0100 Message-ID: Subject: Re: [PATCH 1/2] clocksource/drivers/sh_cmt: Fix potential deadlock when calling runtime PM To: =?UTF-8?Q?Niklas_S=C3=B6derlund?= Cc: Daniel Lezcano , Thomas Gleixner , John Stultz , Linux Kernel Mailing List , Linux-Renesas Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8BIT Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Niklas, On Sat, Dec 5, 2020 at 3:20 AM Niklas Söderlund wrote: > The ch->lock is used to protect the whole enable() and read() of > sh_cmt's implementation of struct clocksource. The enable() > implementation calls pm_runtime_get_sync() which may result in the clock > source to be read() triggering a cyclic lockdep warning for the > ch->lock. > > The sh_cmt driver implement its own balancing of calls to > sh_cmt_{enable,disable}() with flags in sh_cmt_{start,stop}(). It does > this to deal with that start and stop are shared between the clock > source and clock event providers. While this could be improved on > verifying corner cases based on any substantial rework on all devices > this driver supports might prove hard. > > As a first step separate the PM handling for clock event and clock > source. Always put/get the device when enabling/disabling the clock > source but keep the clock event logic unchanged. This allows the sh_cmt > implementation of struct clocksource to call PM without holding the > ch->lock and avoiding the deadlock. > > Triggering and log of the deadlock warning, > > # echo e60f0000.timer > /sys/devices/system/clocksource/clocksource0/current_clocksource > [ 46.948370] ====================================================== > [ 46.954730] WARNING: possible circular locking dependency detected [...] > Signed-off-by: Niklas Söderlund Thanks for looking into this! > --- a/drivers/clocksource/sh_cmt.c > +++ b/drivers/clocksource/sh_cmt.c > @@ -319,7 +319,6 @@ static int sh_cmt_enable(struct sh_cmt_channel *ch) > { > int k, ret; > > - pm_runtime_get_sync(&ch->cmt->pdev->dev); > dev_pm_syscore_device(&ch->cmt->pdev->dev, true); > > /* enable clock */ > @@ -394,7 +393,6 @@ static void sh_cmt_disable(struct sh_cmt_channel *ch) > clk_disable(ch->cmt->clk); > > dev_pm_syscore_device(&ch->cmt->pdev->dev, false); > - pm_runtime_put(&ch->cmt->pdev->dev); > } > > /* private flags */ > @@ -562,10 +560,16 @@ static int sh_cmt_start(struct sh_cmt_channel *ch, unsigned long flag) > int ret = 0; > unsigned long flags; > > + if (flag & FLAG_CLOCKSOURCE) > + pm_runtime_get_sync(&ch->cmt->pdev->dev); > + > raw_spin_lock_irqsave(&ch->lock, flags); > > - if (!(ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE))) > + if (!(ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE))) { > + if (flag & FLAG_CLOCKEVENT) > + pm_runtime_get_sync(&ch->cmt->pdev->dev); This change emphasizes the (pre-existing) issue with clock events: pm_runtime_get_sync() is called while holding a spinlock, leading to the following splat on r8a7740/armadillo: sh_cmt e6138000.timer: ch0: used for periodic clock events ============================= [ BUG: Invalid wait context ] 5.10.0-rc5-armadillo-00566-g8eaa6103691d-dirty #225 Not tainted ----------------------------- swapper/1 is trying to lock: c254cd2c (&dev->power.lock){....}-{3:3}, at: __pm_runtime_resume+0x54/0x80 other info that might help us debug this: context-{5:5} 3 locks held by swapper/1: #0: c254ccd0 (&dev->mutex){....}-{4:4}, at: device_driver_attach+0x18/0x5c #1: c0bed230 (clockevents_lock){....}-{2:2}, at: clockevents_register_device+0x5c/0x10c #2: c26a5038 (&ch->lock){....}-{2:2}, at: sh_cmt_start+0x18/0x1b0 As this is a pre-existing issue: Reviewed-by: Geert Uytterhoeven > ret = sh_cmt_enable(ch); > + } > > if (ret) > goto out; Gr{oetje,eeting}s, Geert -- Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds