From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C5ED4C63697 for ; Mon, 23 Nov 2020 17:19:45 +0000 (UTC) Received: from whitealder.osuosl.org (smtp1.osuosl.org [140.211.166.138]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 5882F2083E for ; Mon, 23 Nov 2020 17:19:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5882F2083E Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=suse.de Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=driverdev-devel-bounces@linuxdriverproject.org Received: from localhost (localhost [127.0.0.1]) by whitealder.osuosl.org (Postfix) with ESMTP id D24F186566; Mon, 23 Nov 2020 17:19:44 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from whitealder.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id GhpeK5FNt-QK; Mon, 23 Nov 2020 17:19:42 +0000 (UTC) Received: from ash.osuosl.org (ash.osuosl.org [140.211.166.34]) by whitealder.osuosl.org (Postfix) with ESMTP id D79DC8651E; Mon, 23 Nov 2020 17:19:42 +0000 (UTC) Received: from hemlock.osuosl.org (smtp2.osuosl.org [140.211.166.133]) by ash.osuosl.org (Postfix) with ESMTP id 38A2A1BF395 for ; Mon, 23 Nov 2020 17:19:41 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by hemlock.osuosl.org (Postfix) with ESMTP id 2EB54870EE for ; Mon, 23 Nov 2020 17:19:41 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from hemlock.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ZUoHU1CJOsMY for ; Mon, 23 Nov 2020 17:19:39 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) by hemlock.osuosl.org (Postfix) with ESMTPS id 57BC5870EA for ; Mon, 23 Nov 2020 17:19:39 +0000 (UTC) X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 52398AC82; Mon, 23 Nov 2020 17:19:37 +0000 (UTC) Message-ID: Subject: Re: [PATCH v4 01/11] firmware: raspberrypi: Keep count of all consumers From: Nicolas Saenz Julienne To: Dmitry Torokhov , Andy Shevchenko Date: Mon, 23 Nov 2020 18:19:35 +0100 In-Reply-To: <20201113072615.GE356503@dtor-ws> References: <20201112163630.17177-1-nsaenzjulienne@suse.de> <20201112163630.17177-2-nsaenzjulienne@suse.de> <20201113072615.GE356503@dtor-ws> User-Agent: Evolution 3.38.1 MIME-Version: 1.0 X-BeenThere: driverdev-devel@linuxdriverproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Driver Project Developer List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "open list:STAGING SUBSYSTEM" , linux-pwm@vger.kernel.org, Florian Fainelli , Scott Branden , devicetree , Stephen Boyd , Ray Jui , Linus Walleij , linux-input , Linux Kernel Mailing List , "open list:GPIO SUBSYSTEM" , Greg Kroah-Hartman , bcm-kernel-feedback-list , Stefan Wahren , Philipp Zabel , Uwe =?ISO-8859-1?Q?Kleine-K=F6nig?= , Bartosz Golaszewski , linux-clk , linux-arm Mailing List , linux-rpi-kernel Content-Type: multipart/mixed; boundary="===============1976774087815427540==" Errors-To: driverdev-devel-bounces@linuxdriverproject.org Sender: "devel" --===============1976774087815427540== Content-Type: multipart/signed; micalg="pgp-sha256"; protocol="application/pgp-signature"; boundary="=-4mT4pfaztxo3yJL0guCg" --=-4mT4pfaztxo3yJL0guCg Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, 2020-11-12 at 23:26 -0800, Dmitry Torokhov wrote: > On Thu, Nov 12, 2020 at 07:52:14PM +0200, Andy Shevchenko wrote: > > On Thu, Nov 12, 2020 at 6:40 PM Nicolas Saenz Julienne > > wrote: > > >=20 > > > When unbinding the firmware device we need to make sure it has no > > > consumers left. Otherwise we'd leave them with a firmware handle > > > pointing at freed memory. > > >=20 > > > Keep a reference count of all consumers and introduce rpi_firmware_pu= t() > > > which will permit automatically decrease the reference count upon > > > unbinding consumer drivers. > >=20 > > ... > >=20 > > > =C2=A0/** > > > - * rpi_firmware_get - Get pointer to rpi_firmware structure. > > > =C2=A0=C2=A0* @firmware_node: Pointer to the firmware Device Tree = node. > > > =C2=A0=C2=A0* > > > + * The reference to rpi_firmware has to be released with rpi_firmwar= e_put(). > > > + * > > > =C2=A0=C2=A0* Returns NULL is the firmware device is not ready. > > > =C2=A0=C2=A0*/ > > > =C2=A0struct rpi_firmware *rpi_firmware_get(struct device_node *firmw= are_node) > > > =C2=A0{ > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0struct platform_devic= e *pdev =3D of_find_device_by_node(firmware_node); > > > + struct rpi_firmware *fw; > > >=20 > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0if (!pdev) > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0return NULL; > > >=20 > > > - return platform_get_drvdata(pdev); > > > + fw =3D platform_get_drvdata(pdev); > > > + if (!fw) > > > + return NULL; > > > + > > > + if (!kref_get_unless_zero(&fw->consumers)) > > > + return NULL; > >=20 Hi Andy, Dimitry, > > Don't we have a more traditional way of doing this, i.e. > > try_module_get() coupled with get_device() ? >=20 > get_device() will make sure that device is there, but gives no > assurances that device is bound to a driver, so it will not help with > the racy access to firmware via platform_get_drvdata() call. I also looked at using get/put_device() just as a means for refcounting (i.= e. replacing fw->consumers), but I can't make it work either. I'd need a way t= o hook up into one of the struct device_ktype release() functions. AFAIK it's= not possible for private uses like this. IIUC the way to do this would be to bypass platform device and create a spe= cial device class/bus for RPi's firmware dependent devices (I could pretty much = copy SCMI's implementation), but I fear that's overkill. So, for now I'll stick with the kref based implementation, I'll be happy to change it if you find a better solution. :) Regards, Nicolas --=-4mT4pfaztxo3yJL0guCg Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part Content-Transfer-Encoding: 7bit -----BEGIN PGP SIGNATURE----- iQEzBAABCAAdFiEErOkkGDHCg2EbPcGjlfZmHno8x/4FAl+77ycACgkQlfZmHno8 x/4SuAf/fbVt5dbVlASpaXs9h1cMXb/e8xl+GmDU2l1pP/uQHmyY+sGKGqNo7+G1 gtKuEhPEavnasiHhJaBTWCCpwytJF9/iToX0i75cDZIObrF1xbO1A3L7hvlRiO6x C+oECKGo3/Awayb7MRHqEiRrLqtuu0odnT3Usn26Rbo7J2o5Lc4KF8WwYblFkmV9 KCW77SlB/6W865vD6KK1KaN6nPqOD3XmKC7doM/MWHIvYd8siFy8qlT5m06s/vhC OHHbX2/7bhgcB+3/9LA9TF5J/JU9KIDHuUVbPYC2hcAVbkELXL93OwJ3DnN7csjp 6obres4oiWUNt0B5Zi7lJDT4Y1KnUw== =C3d8 -----END PGP SIGNATURE----- --=-4mT4pfaztxo3yJL0guCg-- --===============1976774087815427540== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ devel mailing list devel@linuxdriverproject.org http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel --===============1976774087815427540==--