From patchwork Fri Jan 31 10:23:10 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxime Ripard X-Patchwork-Id: 3561021 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 85ED3C02DD for ; Fri, 31 Jan 2014 10:26:25 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 9BE7C201FE for ; Fri, 31 Jan 2014 10:26:24 +0000 (UTC) Received: from casper.infradead.org (casper.infradead.org [85.118.1.10]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 834DB201D5 for ; Fri, 31 Jan 2014 10:26:23 +0000 (UTC) Received: from merlin.infradead.org ([2001:4978:20e::2]) by casper.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1W9BIM-0007NW-Gg; Fri, 31 Jan 2014 10:25:58 +0000 Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1W9BIF-0003XJ-Lp; Fri, 31 Jan 2014 10:25:51 +0000 Received: from top.free-electrons.com ([176.31.233.9] helo=mail.free-electrons.com) by merlin.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1W9BHz-0003U6-6B for linux-arm-kernel@lists.infradead.org; Fri, 31 Jan 2014 10:25:36 +0000 Received: by mail.free-electrons.com (Postfix, from userid 106) id B54DFA38; Fri, 31 Jan 2014 11:25:12 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Spam-Level: X-Spam-Status: No, score=-4.8 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 Received: from localhost (col31-4-88-188-83-94.fbx.proxad.net [88.188.83.94]) by mail.free-electrons.com (Postfix) with ESMTPSA id 1EEF57BF; Fri, 31 Jan 2014 11:25:02 +0100 (CET) From: Maxime Ripard To: Mark Brown Subject: [PATCH 1/3] spi: core: Add devm_spi_alloc_master Date: Fri, 31 Jan 2014 11:23:10 +0100 Message-Id: <1391163792-21819-2-git-send-email-maxime.ripard@free-electrons.com> X-Mailer: git-send-email 1.8.4.2 In-Reply-To: <1391163792-21819-1-git-send-email-maxime.ripard@free-electrons.com> References: <1391163792-21819-1-git-send-email-maxime.ripard@free-electrons.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20140131_052535_348319_F563A7D0 X-CRM114-Status: GOOD ( 13.32 ) X-Spam-Score: -1.7 (-) Cc: Maxime Ripard , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-spi@vger.kernel.org X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Using devm_spi_register_master leads to a memory leak on the spi_master structure. spi_alloc_master uses kzalloc to allocate the spi_master but the introduction of devm_spi_register_master removed all the matching calls to spi_master_put, leaking the spi_master structure. Add a devm_spi_alloc_master to provide the intended behaviour. Signed-off-by: Maxime Ripard --- drivers/spi/spi.c | 36 ++++++++++++++++++++++++++++++++++++ include/linux/spi/spi.h | 2 ++ 2 files changed, 38 insertions(+) diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index 63613a9..eb728ec 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -1266,6 +1266,42 @@ struct spi_master *spi_alloc_master(struct device *dev, unsigned size) } EXPORT_SYMBOL_GPL(spi_alloc_master); +static void devm_spi_put(struct device *dev, void *res) +{ + spi_master_put(*(struct spi_master **)res); +} + +/** + * devm_spi_alloc_master - allocate SPI master controller + * @dev: the controller, possibly using the platform_bus + * @size: how much zeroed driver-private data to allocate; the pointer to this + * memory is in the driver_data field of the returned device, + * accessible with spi_master_get_devdata(). + * Context: can sleep + * + * Allocates a master controller as with spi_alloc_master() which will + * automatically be freed + */ +struct spi_master *devm_spi_alloc_master(struct device *dev, unsigned size) +{ + struct spi_master **ptr, *master; + + ptr = devres_alloc(devm_spi_put, sizeof(*ptr), GFP_KERNEL); + if (!ptr) + return NULL; + + master = spi_alloc_master(dev, size); + if (master) { + *ptr = master; + devres_add(dev, ptr); + } else { + devres_free(ptr); + } + + return master; +} +EXPORT_SYMBOL_GPL(devm_spi_alloc_master); + #ifdef CONFIG_OF static int of_spi_register_master(struct spi_master *master) { diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h index a1d4ca2..6fbdb2b 100644 --- a/include/linux/spi/spi.h +++ b/include/linux/spi/spi.h @@ -462,6 +462,8 @@ extern void spi_finalize_current_transfer(struct spi_master *master); /* the spi driver core manages memory for the spi_master classdev */ extern struct spi_master * spi_alloc_master(struct device *host, unsigned size); +extern struct spi_master * +devm_spi_alloc_master(struct device *host, unsigned size); extern int spi_register_master(struct spi_master *master); extern int devm_spi_register_master(struct device *dev,