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=-13.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable 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 AE7C2C83012 for ; Thu, 3 Dec 2020 08:06:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 63C0420C56 for ; Thu, 3 Dec 2020 08:06:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388205AbgLCIGN (ORCPT ); Thu, 3 Dec 2020 03:06:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36866 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388003AbgLCIGN (ORCPT ); Thu, 3 Dec 2020 03:06:13 -0500 Received: from mail-ej1-x644.google.com (mail-ej1-x644.google.com [IPv6:2a00:1450:4864:20::644]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5C06EC061A4E for ; Thu, 3 Dec 2020 00:05:27 -0800 (PST) Received: by mail-ej1-x644.google.com with SMTP id d17so2067744ejy.9 for ; Thu, 03 Dec 2020 00:05:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=FZ8VxZZvqmh6ehr3KHE/PiRk3db+weOkpDGOlsh3YO8=; b=haDseJv+D02T10aLAxQHXG0BsYACsfkijqaB+ELZmfch2JYU9g6y9ryqDyIqWShXaW yaAZ88xKKVmpIrZKV/qetu3zM2AbVORk0wYWL03XQMh8ss+hr7Dc6CnNOdvjOspJC/vQ R2df0RgSro0FHNGu8o3DpusAvPeMobIgNjOfc7hfwHJ6Nvy0U69h0GmKMctPOdP8JHuK uCRMiqbef850rMYfMmIEUS0TzdqaTlcSg4Jgua9sT1AuBgZSfdy1txMa/Fszl/iCAsRa 6RBF8zj3OIpntYgPprvZoROYfpZUKnhmmTVDrR3O2ZTkmVat83g3y0aDOoH39HHPuS4h RbOA== 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=FZ8VxZZvqmh6ehr3KHE/PiRk3db+weOkpDGOlsh3YO8=; b=r/FphDUh3WKEeik0GgjdUbGC3WlIYp7ds9LozK6QoPxa6uoZ8lp7EDvv3FDK7VdQVj IKMNawDBsALE5yeZErIqFkM+la6rop88OjMpXTI86O9/dL5T7jXfucGs+yYZQjvKA20R 95xW+3nzM1Leo70bygeLUGaxx7ntklm+q3EJoW24TQvfQBUsELWrTLAlENf4kbibxnqm kRso/NtVhMnFCahntM7JD1FB/1iGnqiFrLdjKmeBkz67jsODmOPn7c/qlshvpscVR0+b BrZSI8iicnhmrC2v0WRvdRxtiYgk+l0oXIPxbC6vQDWBpVL6hSP242tg/xu8GRRj0a/j xIeQ== X-Gm-Message-State: AOAM532+AHp7Ste7Bp/aCK0DEGbRUq143xEX7KcnWKmxbUHTcFq1PMOv RT/xz7/cpCAGFluDvibBPG1o7v8kF9QSyS9eYv9waQ== X-Google-Smtp-Source: ABdhPJxP2EJznI3pbg+Rqtm4SZQefIKFXf0kkiRRTqzbUDyEG5oMEZvNtOYbyFNg3cm8vTl+G0cfLIdKavT7T0DOD1k= X-Received: by 2002:a17:906:7f01:: with SMTP id d1mr1414079ejr.429.1606982726027; Thu, 03 Dec 2020 00:05:26 -0800 (PST) MIME-Version: 1.0 References: <20201123183833.18750-1-nsaenzjulienne@suse.de> <20201123183833.18750-2-nsaenzjulienne@suse.de> In-Reply-To: <20201123183833.18750-2-nsaenzjulienne@suse.de> From: Bartosz Golaszewski Date: Thu, 3 Dec 2020 09:05:15 +0100 Message-ID: Subject: Re: [PATCH v5 01/11] firmware: raspberrypi: Keep count of all consumers To: Nicolas Saenz Julienne Cc: =?UTF-8?Q?Uwe_Kleine=2DK=C3=B6nig?= , LKML , Florian Fainelli , Ray Jui , Scott Branden , bcm-kernel-feedback-list@broadcom.com, linux-pwm@vger.kernel.org, arm-soc , linux-devicetree , wahrenst@gmx.net, Linux Input , Dmitry Torokhov , Greg KH , devel@driverdev.osuosl.org, Philipp Zabel , linux-gpio , Linus Walleij , linux-clk , Stephen Boyd , linux-rpi-kernel@lists.infradead.org, Andy Shevchenko Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org On Mon, Nov 23, 2020 at 7:38 PM Nicolas Saenz Julienne wrote: > > 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. > > Keep a reference count of all consumers and introduce rpi_firmware_put() > which will permit automatically decrease the reference count upon > unbinding consumer drivers. > > Suggested-by: Uwe Kleine-K=C3=B6nig > Signed-off-by: Nicolas Saenz Julienne > --- > > Changes since v3: > - Use kref instead of waiting on refcount > > drivers/firmware/raspberrypi.c | 37 +++++++++++++++++++--- > include/soc/bcm2835/raspberrypi-firmware.h | 2 ++ > 2 files changed, 35 insertions(+), 4 deletions(-) > > diff --git a/drivers/firmware/raspberrypi.c b/drivers/firmware/raspberryp= i.c > index 30259dc9b805..ed793aef7851 100644 > --- a/drivers/firmware/raspberrypi.c > +++ b/drivers/firmware/raspberrypi.c > @@ -7,6 +7,7 @@ > */ > > #include > +#include > #include > #include > #include > @@ -27,6 +28,8 @@ struct rpi_firmware { > struct mbox_chan *chan; /* The property channel. */ > struct completion c; > u32 enabled; > + > + struct kref consumers; > }; > > static DEFINE_MUTEX(transaction_lock); > @@ -225,12 +228,27 @@ static void rpi_register_clk_driver(struct device *= dev) > -1, NULL, 0); > } > > +static void rpi_firmware_delete(struct kref *kref) > +{ > + struct rpi_firmware *fw =3D container_of(kref, struct rpi_firmwar= e, > + consumers); > + > + mbox_free_channel(fw->chan); > + kfree(fw); > +} > + > +void rpi_firmware_put(struct rpi_firmware *fw) > +{ > + kref_put(&fw->consumers, rpi_firmware_delete); > +} > +EXPORT_SYMBOL_GPL(rpi_firmware_put); > + > static int rpi_firmware_probe(struct platform_device *pdev) > { > struct device *dev =3D &pdev->dev; > struct rpi_firmware *fw; > > - fw =3D devm_kzalloc(dev, sizeof(*fw), GFP_KERNEL); One nit from my side: maybe add a comment here saying that you really want to use non-managed kzalloc() because you're going to get people blindly converting it to devm_kzalloc() very soon. Bartosz > + fw =3D kzalloc(sizeof(*fw), GFP_KERNEL); > if (!fw) > return -ENOMEM; > > @@ -247,6 +265,7 @@ static int rpi_firmware_probe(struct platform_device = *pdev) > } > > init_completion(&fw->c); > + kref_init(&fw->consumers); > > platform_set_drvdata(pdev, fw); > > @@ -275,25 +294,35 @@ static int rpi_firmware_remove(struct platform_devi= ce *pdev) > rpi_hwmon =3D NULL; > platform_device_unregister(rpi_clk); > rpi_clk =3D NULL; > - mbox_free_channel(fw->chan); > + > + rpi_firmware_put(fw); > > return 0; > } > > /** > - * rpi_firmware_get - Get pointer to rpi_firmware structure. > * @firmware_node: Pointer to the firmware Device Tree node. > * > + * The reference to rpi_firmware has to be released with rpi_firmware_pu= t(). > + * > * Returns NULL is the firmware device is not ready. > */ > struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node) > { > struct platform_device *pdev =3D of_find_device_by_node(firmware_= node); > + struct rpi_firmware *fw; > > if (!pdev) > return NULL; > > - return platform_get_drvdata(pdev); > + fw =3D platform_get_drvdata(pdev); > + if (!fw) > + return NULL; > + > + if (!kref_get_unless_zero(&fw->consumers)) > + return NULL; > + > + return fw; > } > EXPORT_SYMBOL_GPL(rpi_firmware_get); > > diff --git a/include/soc/bcm2835/raspberrypi-firmware.h b/include/soc/bcm= 2835/raspberrypi-firmware.h > index cc9cdbc66403..fdfef7fe40df 100644 > --- a/include/soc/bcm2835/raspberrypi-firmware.h > +++ b/include/soc/bcm2835/raspberrypi-firmware.h > @@ -140,6 +140,7 @@ int rpi_firmware_property(struct rpi_firmware *fw, > u32 tag, void *data, size_t len); > int rpi_firmware_property_list(struct rpi_firmware *fw, > void *data, size_t tag_size); > +void rpi_firmware_put(struct rpi_firmware *fw); > struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node)= ; > #else > static inline int rpi_firmware_property(struct rpi_firmware *fw, u32 tag= , > @@ -154,6 +155,7 @@ static inline int rpi_firmware_property_list(struct r= pi_firmware *fw, > return -ENOSYS; > } > > +static inline void rpi_firmware_put(struct rpi_firmware *fw) { } > static inline struct rpi_firmware *rpi_firmware_get(struct device_node *= firmware_node) > { > return NULL; > -- > 2.29.2 >