Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp6061067rwi; Tue, 18 Oct 2022 07:40:13 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4nDKEepMI+nqLxGiFCyB77Ew6ar+zec6nuy1LbXER/g6FYr3aULYP2OMyR24YDAZNrQJ9Q X-Received: by 2002:aa7:c698:0:b0:458:8274:12ac with SMTP id n24-20020aa7c698000000b00458827412acmr2961362edq.351.1666104013235; Tue, 18 Oct 2022 07:40:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666104013; cv=none; d=google.com; s=arc-20160816; b=euJu4z+dGaa/16b973UM1C/fTF6DVDVBwhZNQm8BE4sm3U57Kr2t0FpLRV63awTQtT fQNYskCpgyMGQouxAjCFBeJlPJuVTXRWhe7p0xQyZ06HNrsBlX1GOhOn7tYRCh8vEupw 7YGBmA3t8hIoH1T/2V3BC8UbCTcRC0HJLq8y4n4nLcT1XAfZ9KUvvNw6EruwrZl8hgqA wFQXOfWWTTKrs4PdpKAsEglbFrbpJZ4eshlpYclBrkUHAqXH8ukVgahR+mkCHy08U1rl ya+rlRCvFDfuyQxW3KSvBuZrPlf5IytFIfcKeHBJYghqMF029XqK+x0avar7YqtODg2/ 1+PA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=8lpCQJBoYLjbpScXrhwNSInIBTo3txxmB+srKoQZRtM=; b=0MPSN2z84gB3HKuQoxc243HWgpY4ps7ffn7Sq86cyVs/XDM/1jKPBXLpnqP4fbAet/ Ae6xSoK20gPSYk7v99B4zIHeQTE2aDxWmGSYA87a7XNCyOqvy7b2Ke7ZYz4rBunPyFDJ NUxJxqclVbK1F2aUKxCOKlAFKq48Ggotv2Q3P7vjZdQpjoGuoHXQAhkVqDSyc+anpzQB jqqUdQgJ0w8K2s7u6uImsrgG3gXoXPGEIvdUiPcODByFM9bglh7kujn8ZXnxs2lbo/vC JcirxQkP3NlsUqck4yLqP9vo+AY8NuPODI5WAOiCDHqHnmjmbi6s0tPWXnSK5HM0AStm 8zqA== 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 qk16-20020a1709077f9000b0078e19037659si6053918ejc.792.2022.10.18.07.39.47; Tue, 18 Oct 2022 07:40:13 -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 S229610AbiJRN3j (ORCPT + 99 others); Tue, 18 Oct 2022 09:29:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57250 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229509AbiJRN3e (ORCPT ); Tue, 18 Oct 2022 09:29:34 -0400 Received: from metis.ext.pengutronix.de (metis.ext.pengutronix.de [IPv6:2001:67c:670:201:290:27ff:fe1d:cc33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5BFE21005C for ; Tue, 18 Oct 2022 06:29:32 -0700 (PDT) Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.ext.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1okmeq-0000U6-8n; Tue, 18 Oct 2022 15:29:24 +0200 Received: from [2a0a:edc0:0:900:1d::77] (helo=ptz.office.stw.pengutronix.de) by drehscheibe.grey.stw.pengutronix.de with esmtp (Exim 4.94.2) (envelope-from ) id 1okmeo-002Hwp-Pu; Tue, 18 Oct 2022 15:29:22 +0200 Received: from ukl by ptz.office.stw.pengutronix.de with local (Exim 4.94.2) (envelope-from ) id 1okmeo-008os2-1H; Tue, 18 Oct 2022 15:29:22 +0200 Date: Tue, 18 Oct 2022 15:29:21 +0200 From: Uwe =?utf-8?Q?Kleine-K=C3=B6nig?= To: Emil Renner Berthing Cc: Thierry Reding , Palmer Dabbelt , Paul Walmsley , Atish Patra , "Wesley W. Terpstra" , linux-pwm@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v1] pwm: sifive: Always let the first pwm_apply_state succeed Message-ID: <20221018132921.5fsbiz254npk2fci@pengutronix.de> References: <20221018091316.415685-1-emil.renner.berthing@canonical.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="4mm6vvxtocb2k52r" Content-Disposition: inline In-Reply-To: <20221018091316.415685-1-emil.renner.berthing@canonical.com> X-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: ukl@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, 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 --4mm6vvxtocb2k52r Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Hello, On Tue, Oct 18, 2022 at 11:13:16AM +0200, Emil Renner Berthing wrote: > Commit 2cfe9bbec56ea579135cdd92409fff371841904f added support for the > RGB and green PWM controlled LEDs on the HiFive Unmatched board > managed by the leds-pwm-multicolor and leds-pwm drivers respectively. > All three colours of the RGB LED and the green LED run from different > lines of the same PWM, but with the same period so this works fine when > the LED drivers are loaded one after the other. >=20 > Unfortunately it does expose a race in the PWM driver when both LED > drivers are loaded at roughly the same time. Here is an example: >=20 > | Thread A | Thread B | > | led_pwm_mc_probe | led_pwm_probe | > | devm_fwnode_pwm_get | | > | pwm_sifive_request | | > | ddata->user_count++ | | > | | devm_fwnode_pwm_get | > | | pwm_sifive_request | > | | ddata->user_count++ | > | ... | ... | > | pwm_state_apply | pwm_state_apply | > | pwm_sifive_apply | pwm_sifive_apply | >=20 > Now both calls to pwm_sifive_apply will see that ddata->approx_period, > initially 0, is different from the requested period and the clock needs > to be updated. But since ddata->user_count >=3D 2 both calls will fail > with -EBUSY, which will then cause both LED drivers to fail to probe. >=20 > Fix it by letting the first call to pwm_sifive_apply update the clock > even when ddata->user_count !=3D 1. >=20 > Fixes: 9e37a53eb051 ("pwm: sifive: Add a driver for SiFive SoC PWM") > Signed-off-by: Emil Renner Berthing > --- > drivers/pwm/pwm-sifive.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) >=20 > diff --git a/drivers/pwm/pwm-sifive.c b/drivers/pwm/pwm-sifive.c > index 2d4fa5e5fdd4..ccdf92045f34 100644 > --- a/drivers/pwm/pwm-sifive.c > +++ b/drivers/pwm/pwm-sifive.c > @@ -159,7 +159,7 @@ static int pwm_sifive_apply(struct pwm_chip *chip, st= ruct pwm_device *pwm, > =20 > mutex_lock(&ddata->lock); > if (state->period !=3D ddata->approx_period) { > - if (ddata->user_count !=3D 1) { > + if (ddata->user_count !=3D 1 && ddata->approx_period) { IMHO this needs a code comment. It should among others mention that approx_period is only zero if .apply() wasn't called before. Let me note this is inconsistent. I didn't check the details, but let's assume the PWM can implement .period =3D 500 and .period =3D 514 and nothing in between. So if the the first PWM requests 512 ns it gets (I hope) 500 ns. Then when the second requests comes in requesting 511 it fails and if it requests 512 is succeeds also getting 500 ns. Hmm. Best regards Uwe --=20 Pengutronix e.K. | Uwe Kleine-K=F6nig | Industrial Linux Solutions | https://www.pengutronix.de/ | --4mm6vvxtocb2k52r Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQEzBAABCgAdFiEEfnIqFpAYrP8+dKQLwfwUeK3K7AkFAmNOqi8ACgkQwfwUeK3K 7AkPuwf/aMdvPVFLhS+ygY/c1Guwk89BPz1Yy9kO0c0YNna1B6Vp8f02XEeGMeK7 hirxK+eqKE+2mhj/4Ma1kqkcDCA11curbutAxiKlBo13FGJsSYGfI6fPQIb4QFc2 B2dbxcF7MkJaDJ9+1RB5FhaK84cfJ0YSXvt0zC2MXzUQUaKQYXsO8KRIcExrmsSY 3qni4f8ZGIojR/5BSW4wxZvdok3rt4LqAYKavkVwXsPM1gAC7QzW8bM5ian2Qz5u TGE+O9jX2frictFolA3vL8dW/Ums8MziSnncS1VhCKjW0Z0Hwu4JePf8Jc8Fofbq a9UJSsy7sT/d+Zj+1Nu7DOgPmoT8LQ== =ysZi -----END PGP SIGNATURE----- --4mm6vvxtocb2k52r--