[{"id":3677725,"web_url":"http://patchwork.ozlabs.org/comment/3677725/","msgid":"<ad-nOb2Tj9PombuN@monoceros>","list_archive_url":null,"date":"2026-04-15T15:12:37","subject":"Re: [PATCH] pwm: atmel-tcb: Fix sleeping function called from\n invalid context","submitter":{"id":88416,"url":"http://patchwork.ozlabs.org/api/people/88416/","name":"Uwe Kleine-König","email":"ukleinek@kernel.org"},"content":"Hello Sangyun (I hope this is the right part of your name to address\nyou, feel free to tell me, when I'm wrong),\n\nOn Wed, Apr 15, 2026 at 06:34:33PM +0900, Sangyun Kim wrote:\n> atmel_tcb_pwm_apply() holds tcbpwmc->lock as a spinlock via\n> guard(spinlock)() and then calls atmel_tcb_pwm_config(), which calls\n> clk_get_rate() twice. clk_get_rate() acquires clk_prepare_lock (a\n> mutex), so this is a sleep-in-atomic-context violation.\n> \n> On CONFIG_DEBUG_ATOMIC_SLEEP kernels every pwm_apply_state() that\n> enables or reconfigures the PWM triggers a \"BUG: sleeping function\n> called from invalid context\" warning.\n> \n> All callers of tcbpwmc->lock (the .request and .apply callbacks) run in\n> process context and only need mutual exclusion against each other, so\n> use a mutex instead of a spinlock and allow the sleeping calls inside\n> atmel_tcb_pwm_config().\n> \n> Fixes: 37f7707077f5 (\"pwm: atmel-tcb: Fix race condition and convert to guards\")\n> Signed-off-by: Sangyun Kim <sangyun.kim@snu.ac.kr>\n\nThe issue is real. I first thought the lock isn't needed at all and can\nbetter be dropped, but the chip lock doesn't cover .request().\n\nIt would be great if you could rework the patch to keep the spinlock and\ninstead make use of clk_rate_exclusive_get() at probe time and then\nstore the rate in struct atmel_tcb_pwm_chip.\n\nOr alternatively drop the lock and call guard(pwmchip)(chip) in\n.request(). (This however would require to move the GUARD definition to\na header.)\n\nWithout the mutex we could then do:\n\ndiff --git a/drivers/pwm/pwm-atmel-tcb.c b/drivers/pwm/pwm-atmel-tcb.c\nindex f9ff78ba122d..70856be12517 100644\n--- a/drivers/pwm/pwm-atmel-tcb.c\n+++ b/drivers/pwm/pwm-atmel-tcb.c\n@@ -431,6 +431,7 @@ static int atmel_tcb_pwm_probe(struct platform_device *pdev)\n \t}\n \n \tchip->ops = &atmel_tcb_pwm_ops;\n+\tchip->atomic = true;\n \ttcbpwmc->channel = channel;\n \ttcbpwmc->width = config->counter_width;\n \nwhich is nice for some usages.\n\nBest regards\nUwe","headers":{"Return-Path":"\n <linux-pwm+bounces-8593-incoming=patchwork.ozlabs.org@vger.kernel.org>","X-Original-To":["incoming@patchwork.ozlabs.org","linux-pwm@vger.kernel.org"],"Delivered-To":"patchwork-incoming@legolas.ozlabs.org","Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (2048-bit key;\n unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256\n header.s=k20201202 header.b=We3/qUEx;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org\n (client-ip=172.234.253.10; helo=sea.lore.kernel.org;\n envelope-from=linux-pwm+bounces-8593-incoming=patchwork.ozlabs.org@vger.kernel.org;\n receiver=patchwork.ozlabs.org)","smtp.subspace.kernel.org;\n\tdkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org\n header.b=\"We3/qUEx\"","smtp.subspace.kernel.org;\n arc=none smtp.client-ip=10.30.226.201"],"Received":["from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4fwlBM4XSBz1yHM\n\tfor <incoming@patchwork.ozlabs.org>; Thu, 16 Apr 2026 01:17:31 +1000 (AEST)","from smtp.subspace.kernel.org (conduit.subspace.kernel.org\n [100.90.174.1])\n\tby sea.lore.kernel.org (Postfix) with ESMTP id BA8FC30DEE33\n\tfor <incoming@patchwork.ozlabs.org>; Wed, 15 Apr 2026 15:12:41 +0000 (UTC)","from localhost.localdomain (localhost.localdomain [127.0.0.1])\n\tby smtp.subspace.kernel.org (Postfix) with ESMTP id 4EE653D890F;\n\tWed, 15 Apr 2026 15:12:40 +0000 (UTC)","from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org\n [10.30.226.201])\n\t(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))\n\t(No client certificate requested)\n\tby smtp.subspace.kernel.org (Postfix) with ESMTPS id 2B04A3D88E9;\n\tWed, 15 Apr 2026 15:12:39 +0000 (UTC)","by smtp.kernel.org (Postfix) with ESMTPSA id 71C8BC19424;\n\tWed, 15 Apr 2026 15:12:39 +0000 (UTC)"],"ARC-Seal":"i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;\n\tt=1776265960; cv=none;\n b=AGqPRwgYTnyQpk7Kz4nCgp/s8ICKtT8Wy+Ac+p4tZGRkk5U8h3ZcesMhSSC0ndFLb/l55MNCwgZFzJiUz14aIRMj1uEas2+DnUWrtHEDzemNVHH4mrcIDSmsdZ9QKe6C2OBzxcArS/PnP71475RxnPB7G9Q/wsREkvBFazLHvmA=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=subspace.kernel.org;\n\ts=arc-20240116; t=1776265960; c=relaxed/simple;\n\tbh=YUJjqqhI9gnFtU556Ey/dqmGHfXQ9/59t0+1jgwdbIc=;\n\th=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version:\n\t Content-Type:Content-Disposition:In-Reply-To;\n b=YoWiEdkkNTESo+kVRKEFlBTai7lWqsR6m8J1uhbaFwmFc6Df4G6ZNODlXIUWCOWZOGRKVgthnF75HFC8fdgn66rHb5MMjW1UhuX5RqjCmB8uYnRnIH48ljGkjlL6SGI4eaCBGKSrCCcYyn/0R40gnvOq+5L910N1k4lsrE0GSD8=","ARC-Authentication-Results":"i=1; smtp.subspace.kernel.org;\n dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org\n header.b=We3/qUEx; arc=none smtp.client-ip=10.30.226.201","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org;\n\ts=k20201202; t=1776265959;\n\tbh=YUJjqqhI9gnFtU556Ey/dqmGHfXQ9/59t0+1jgwdbIc=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=We3/qUExm3NpUuEfh+Nhj5vfjEkZVXtEmFHLQMxarih9Vs8InW4IRliQcLEOcOFyA\n\t q9ee479sSUDglZ0g/gHnfTVfMyf1d7XaRingMYQrql8Ib3cg1ogN4bmH1cssTbStuq\n\t 6sjwatgxMEmdyEfkT1FedUiekoNhAOGd2FOMosvmw30nkw2DBoIdjDzMqkZCStPbkY\n\t v3wYDC4KL+DJjwsByqKnjVBWFMfaAMqE8NT/PUjsbmwM+mkcaxrqLCRJJTwX9y6xYb\n\t UP6H1XrO4Gao346aaHQeNVXboGUjer7IXZSGE7guY2SLXGeQFepnX/ub6+NeAPmH6+\n\t ZZLPF2E+p2gfA==","Date":"Wed, 15 Apr 2026 17:12:37 +0200","From":"Uwe =?utf-8?q?Kleine-K=C3=B6nig?= <ukleinek@kernel.org>","To":"Sangyun Kim <sangyun.kim@snu.ac.kr>","Cc":"nicolas.ferre@microchip.com, alexandre.belloni@bootlin.com,\n\tclaudiu.beznea@tuxon.dev, linux-pwm@vger.kernel.org,\n linux-arm-kernel@lists.infradead.org,\n\tlinux-kernel@vger.kernel.org","Subject":"Re: [PATCH] pwm: atmel-tcb: Fix sleeping function called from\n invalid context","Message-ID":"<ad-nOb2Tj9PombuN@monoceros>","References":"<20260415093433.2359955-1-sangyun.kim@snu.ac.kr>","Precedence":"bulk","X-Mailing-List":"linux-pwm@vger.kernel.org","List-Id":"<linux-pwm.vger.kernel.org>","List-Subscribe":"<mailto:linux-pwm+subscribe@vger.kernel.org>","List-Unsubscribe":"<mailto:linux-pwm+unsubscribe@vger.kernel.org>","MIME-Version":"1.0","Content-Type":"multipart/signed; micalg=pgp-sha512;\n\tprotocol=\"application/pgp-signature\"; boundary=\"hke3yhwg6rcplifn\"","Content-Disposition":"inline","In-Reply-To":"<20260415093433.2359955-1-sangyun.kim@snu.ac.kr>"}}]