Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp1010642pxu; Mon, 23 Nov 2020 09:23:35 -0800 (PST) X-Google-Smtp-Source: ABdhPJzOawoY+SmyIJDo7tKMWqxL+btWqv6sTEmtfwq6krJGVeRvOhWUFvo+CIopT22ay0zGwiQ5 X-Received: by 2002:a50:a40c:: with SMTP id u12mr189832edb.337.1606152215335; Mon, 23 Nov 2020 09:23:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606152215; cv=none; d=google.com; s=arc-20160816; b=NICDiE8dWKvXUGuwv+b+2qj3MbyzaESCoe9c8BBdL/OzVyEXLhDp4suxUi4nrJHRB7 66+ewkK6ncdKD3kmxxPkTnxABj+V1EEbTJIKmMvc1hHkYhA9gkG3xBl7FY/pB7GVBH9W LjVVBq8Ppanm9/fMSQ/6I9Not/HVf4heW1emIFJ5KLalaq0Pwb1+sQRp5UYPjGXD0KeT +SIafvHI5zw+O123LGMeirxgfXHA8jzatkf1MEbjClxj9WVLvn6bpRy0Fdf5HIn1By9u RDReIPdR73dMYAbryBri1/1sAQ+8cbv2vfz3RzpBddQseB/SC5/d8mnNOXpeitEkIe2V HntA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:in-reply-to :date:cc:to:from:subject:message-id; bh=SoSXpdi2uvkBokIBvc/Ew9YrMH3hGfOCHoM+D71LbFg=; b=X0/VRwKMAbUBzWaozvcWsOaHI75Zul0q5qxTiDr046TsnvpPMiAo6y1HRF847bRs6b pkM6v9MCY07Z8Z7FlnhLz3676SFR867qJwgnovbhfgEYBHdPZUUcj5DEWH44S7RrnHF1 +umdFzGR10ThwW7duM2wYvVTH+7RTGGkZqGTFTR6eeh1hkWVQQuw3B0Rwdfarsx38C31 s37R0OQ+l0nrsxk3lKqk0g0FBNkc86YW7SRzXA+yWUi2hYN63LW8G/vsN7NBUFvaOwKV 0xsP+warZsZRqyVOhA13UySUznxfSEYgZcZoby3o+9Lm6L6+XiaRgvyIqsXGYfs2Wpmo GPXw== 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 v12si6602511eje.754.2020.11.23.09.23.12; Mon, 23 Nov 2020 09:23:35 -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 S2387403AbgKWRTk (ORCPT + 99 others); Mon, 23 Nov 2020 12:19:40 -0500 Received: from mx2.suse.de ([195.135.220.15]:58310 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726417AbgKWRTj (ORCPT ); Mon, 23 Nov 2020 12:19:39 -0500 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 Cc: Uwe =?ISO-8859-1?Q?Kleine-K=F6nig?= , Linux Kernel Mailing List , Florian Fainelli , Ray Jui , Scott Branden , bcm-kernel-feedback-list , linux-pwm@vger.kernel.org, linux-arm Mailing List , devicetree , Stefan Wahren , linux-input , Greg Kroah-Hartman , "open list:STAGING SUBSYSTEM" , Philipp Zabel , "open list:GPIO SUBSYSTEM" , Linus Walleij , linux-clk , Stephen Boyd , linux-rpi-kernel , Bartosz Golaszewski 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> Content-Type: multipart/signed; micalg="pgp-sha256"; protocol="application/pgp-signature"; boundary="=-4mT4pfaztxo3yJL0guCg" User-Agent: Evolution 3.38.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org --=-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--