Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp306703pxu; Thu, 3 Dec 2020 00:08:12 -0800 (PST) X-Google-Smtp-Source: ABdhPJzqIsIlWnF7UC7aSXQd9dFXpAHcWaPlIyCkiU+PoweMWAAtscgoqQZys4vlP39Cl4u/l4k9 X-Received: by 2002:a17:906:c193:: with SMTP id g19mr1391392ejz.393.1606982892505; Thu, 03 Dec 2020 00:08:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606982892; cv=none; d=google.com; s=arc-20160816; b=Ygnt/pAWHztCF4o1CsGOdUH036Tw2CSrg4DEMKiCAx2zYQjHf545SdSOyGTZ7DNFpt VQwpbTcZ0VZdttxmAwtmL9b6RIsIwTkXPoBDSKD3JHQi01Ds9uoLyDnNgoj0Njj096Z7 W3iN5irAeg19BTtDI3z3FNyk0VQ69ZqoG9IrfOTBYaU1lyI3f1DixdrYTAqPibIC6Oyn 8/vM/2C9aZ6xfG3HsPjTq/XJXhw4H9sBjDtOeT0u0hpbbw8/topZx6giBlbymkgTsjJJ OrJ26F1dURBfeyKLvBNZZh5flKB8hYZkUXzmFitv0zsqKS/+0tspCMjwAN/E7iHaebZN +Xzg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=FZ8VxZZvqmh6ehr3KHE/PiRk3db+weOkpDGOlsh3YO8=; b=AgTWJdmUq45milQPzwlflTi/4ip/JNZUij5tKo6GCbAssS1Oili87/bvyzGOAsABYX IYO922/UHLI0w59WYBOyU3LaLeEtgJ2RaLc9CYa4+KxPpafLUyPsfOQS+57Y/k6Z2C73 ThqFA89ihJ24MTzRynEKZwETAlyAzl5VINgCoGMwGtUxTBcoSfGignIA3Uhf6Cmpg0f1 OCMBGWEe+acF/QB/s9U/FZvfEG4derfFHvVmx5Fk57OkNzQmqJ3aq9T8vKRXllG7wP8z 57yP9gxlW7UBBFuUDM97qAFJBUGKyyA/zixg3X6eOkaU1AMrC+RtaUvFGgfPnwnPGr70 kaCQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@baylibre-com.20150623.gappssmtp.com header.s=20150623 header.b=haDseJv+; 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 f12si548008edx.51.2020.12.03.00.07.49; Thu, 03 Dec 2020 00:08:12 -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; dkim=pass header.i=@baylibre-com.20150623.gappssmtp.com header.s=20150623 header.b=haDseJv+; 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 S2388170AbgLCIGI (ORCPT + 99 others); Thu, 3 Dec 2020 03:06:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36872 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729897AbgLCIGI (ORCPT ); Thu, 3 Dec 2020 03:06:08 -0500 Received: from mail-ej1-x643.google.com (mail-ej1-x643.google.com [IPv6:2a00:1450:4864:20::643]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 97EDCC061A52 for ; Thu, 3 Dec 2020 00:05:27 -0800 (PST) Received: by mail-ej1-x643.google.com with SMTP id n26so2087200eju.6 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=QYoJasX0kPdUjQoF5A/HrZdWxrglIKeWK0h9bGVhfni8atnAu1KbmagACa4t1Ert+G 3SC2PkKt99bItUlc5RRxwZYCa8kr9OmW+o6+NNQ/8Uzo0yeWwYsaYJHuptsXb9vG7K+i QXJvQ1a7DSSuGbhhjmtw16MLuwWaZ2KC0k0oddlZj5mk9J0Cup0vf22DdVD10YGRd1Hk hZhL5YaiW78RlAHlMEwc15a624BjI7Z/HWxRh2WpMmqCtk2gAKz766V7UE7ipHRo5J49 QdlbV1nf2NvkBNB9UvhpZAFz8ST/wNbWmXdj74oqmdUDvA8jEw3+9MTAGPdSWPdxq45q Bl5A== X-Gm-Message-State: AOAM530sjxkFkWcVc4q1peNS3+mWGbHd6pUW8uSRIy9bJwzvLBi7veFh Ob3DlYylEn569GORiOUMgnaaXyvlxa5PaYjrETzJJg== 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-kernel@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 >