From patchwork Thu Oct 22 15:58:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 11851563 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=-12.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT 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 D79ACC388F7 for ; Thu, 22 Oct 2020 16:02:03 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (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 5A4EA21707 for ; Thu, 22 Oct 2020 16:02:02 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="HSz/WEma" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5A4EA21707 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=suse.de Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To:Message-Id:Date: Subject:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=/qs1Nzx12+C4zCdPQtHTt75mCdZqXkbdQ7Ho9LJ+MYw=; b=HSz/WEmauTqP4gs5WJrg0mphj +f9PnjWrlkIM22ure5XI+zIo+kZnFgrF/56s1n/N/kytdC/JurbJVr03NaOsgM6BstuJ5IbSeLWdw EpzSL6xm9Ub+sKJoQS5bCScNb2SkfUgaS/VHwLOmveBWEWaZy6sIFqN1MvBkp3sFuKyL2pZPfMe9A zwoBrE9qeZ3pkcRSKtWBheGg75xd8wufE2HT3Y8K3Ubll1DnAXmP5hzfzq+Ov3DmAPmgqtwZnIZ03 vFdCtY8bcGNP+4NxqyVyBZ8jujsyqkJyLNoP6JIIoAv96h5kKw2gb1zx4Gg2ChI5qSK8TLY8/MMuy 18hEdaojA==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kVczr-0001uJ-D3; Thu, 22 Oct 2020 15:59:23 +0000 Received: from mx2.suse.de ([195.135.220.15]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kVczb-0001o6-CR; Thu, 22 Oct 2020 15:59:08 +0000 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 4A8ADAC23; Thu, 22 Oct 2020 15:59:06 +0000 (UTC) From: Nicolas Saenz Julienne To: u.kleine-koenig@pengutronix.de, linux-kernel@vger.kernel.org, Nicolas Saenz Julienne , Florian Fainelli , Ray Jui , Scott Branden , bcm-kernel-feedback-list@broadcom.com Subject: [PATCH v2 01/10] firmware: raspberrypi: Introduce rpi_firmware_put() Date: Thu, 22 Oct 2020 17:58:48 +0200 Message-Id: <20201022155858.20867-2-nsaenzjulienne@suse.de> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20201022155858.20867-1-nsaenzjulienne@suse.de> References: <20201022155858.20867-1-nsaenzjulienne@suse.de> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201022_115907_673788_20092B55 X-CRM114-Status: GOOD ( 20.66 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: devel@driverdev.osuosl.org, linux-pwm@vger.kernel.org, devicetree@vger.kernel.org, sboyd@kernel.org, gregkh@linuxfoundation.org, linus.walleij@linaro.org, dmitry.torokhov@gmail.com, linux-gpio@vger.kernel.org, wahrenst@gmx.net, p.zabel@pengutronix.de, linux-input@vger.kernel.org, linux-clk@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-rpi-kernel@lists.infradead.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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 make sure they all finished unbinding before we do. Suggested-by: Uwe Kleine-König Signed-off-by: Nicolas Saenz Julienne --- @Uwe: I didn't found it necessary to call 'try_module_get()' as the rest of modules depend on the 'rpi_firmware_get/put()' symbols which already block users from unloading the module prematurely. drivers/firmware/raspberrypi.c | 30 +++++++++++++++++++++- include/soc/bcm2835/raspberrypi-firmware.h | 3 +++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/drivers/firmware/raspberrypi.c b/drivers/firmware/raspberrypi.c index 2371d08bdd17..e5ba609e3985 100644 --- a/drivers/firmware/raspberrypi.c +++ b/drivers/firmware/raspberrypi.c @@ -11,7 +11,9 @@ #include #include #include +#include #include +#include #include #define MBOX_MSG(chan, data28) (((data28) & ~0xf) | ((chan) & 0xf)) @@ -27,6 +29,9 @@ struct rpi_firmware { struct mbox_chan *chan; /* The property channel. */ struct completion c; u32 enabled; + + refcount_t consumers; + wait_queue_head_t wait; }; static DEFINE_MUTEX(transaction_lock); @@ -247,6 +252,8 @@ static int rpi_firmware_probe(struct platform_device *pdev) } init_completion(&fw->c); + refcount_set(&fw->consumers, 1); + init_waitqueue_head(&fw->wait); platform_set_drvdata(pdev, fw); @@ -275,6 +282,8 @@ static int rpi_firmware_remove(struct platform_device *pdev) rpi_hwmon = NULL; platform_device_unregister(rpi_clk); rpi_clk = NULL; + + wait_event(fw->wait, refcount_dec_if_one(&fw->consumers)); mbox_free_channel(fw->chan); return 0; @@ -289,14 +298,33 @@ static int rpi_firmware_remove(struct platform_device *pdev) struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node) { struct platform_device *pdev = of_find_device_by_node(firmware_node); + struct rpi_firmware *fw; if (!pdev) return NULL; - return platform_get_drvdata(pdev); + fw = platform_get_drvdata(pdev); + if (!fw) + return NULL; + + if (!refcount_inc_not_zero(&fw->consumers)) + return NULL; + + return fw; } EXPORT_SYMBOL_GPL(rpi_firmware_get); +/** + * rpi_firmware_put - Put pointer to rpi_firmware structure. + * @rpi_firmware: Pointer to struct rpi_firmware + */ +void rpi_firmware_put(struct rpi_firmware *fw) +{ + refcount_dec(&fw->consumers); + wake_up(&fw->wait); +} +EXPORT_SYMBOL_GPL(rpi_firmware_put); + static const struct of_device_id rpi_firmware_of_match[] = { { .compatible = "raspberrypi,bcm2835-firmware", }, {}, diff --git a/include/soc/bcm2835/raspberrypi-firmware.h b/include/soc/bcm2835/raspberrypi-firmware.h index cc9cdbc66403..7836ea51fbdf 100644 --- a/include/soc/bcm2835/raspberrypi-firmware.h +++ b/include/soc/bcm2835/raspberrypi-firmware.h @@ -141,6 +141,7 @@ int rpi_firmware_property(struct rpi_firmware *fw, int rpi_firmware_property_list(struct rpi_firmware *fw, void *data, size_t tag_size); struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node); +void rpi_firmware_put(struct rpi_firmware *fw); #else static inline int rpi_firmware_property(struct rpi_firmware *fw, u32 tag, void *data, size_t len) @@ -158,6 +159,8 @@ static inline struct rpi_firmware *rpi_firmware_get(struct device_node *firmware { return NULL; } + +void rpi_firmware_put(struct rpi_firmware *fw) { } #endif #endif /* __SOC_RASPBERRY_FIRMWARE_H__ */