From patchwork Mon Oct 15 10:44:56 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Russell King - ARM Linux X-Patchwork-Id: 1593341 Return-Path: X-Original-To: patchwork-linux-mmc@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork2.kernel.org (Postfix) with ESMTP id B003ADFB34 for ; Mon, 15 Oct 2012 10:45:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751809Ab2JOKpD (ORCPT ); Mon, 15 Oct 2012 06:45:03 -0400 Received: from caramon.arm.linux.org.uk ([78.32.30.218]:52470 "EHLO caramon.arm.linux.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751643Ab2JOKpC (ORCPT ); Mon, 15 Oct 2012 06:45:02 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=arm.linux.org.uk; s=caramon; h=Sender:In-Reply-To:Content-Type:MIME-Version:References:Message-ID:Subject:Cc:To:From:Date; bh=9QVt4h9vgRqpbm0mEQ8UIOyn1wdzByJAWOw0ytK6/m0=; b=FMLFzo6LcJWHnitYiHLHGVIJc+oDhvpwF/rYrAGbq3CGub4HgbN3VH7kQCe261xc+TeMLKRzuuxpo/W3oSOTuswO3/DdLhD4pS034kWgwtt6VF7S3ZGsLHY4rrWrOA8DCnNC/x/BFT+EM+HGWl+MpQu2behO/CNvsuyfLgS+3tM=; Received: from n2100.arm.linux.org.uk ([2002:4e20:1eda:1:214:fdff:fe10:4f86]:49743) by caramon.arm.linux.org.uk with esmtpsa (TLSv1:AES256-SHA:256) (Exim 4.76) (envelope-from ) id 1TNiAM-0007aU-5Z; Mon, 15 Oct 2012 11:44:58 +0100 Received: from linux by n2100.arm.linux.org.uk with local (Exim 4.76) (envelope-from ) id 1TNiAL-00053T-8E; Mon, 15 Oct 2012 11:44:57 +0100 Date: Mon, 15 Oct 2012 11:44:56 +0100 From: Russell King - ARM Linux To: Chris Ball Cc: linux-mmc@vger.kernel.org, linux-arm-kernel@lists.infradead.org Subject: Re: [PATCH] MMC: fix sdhci-dove removal Message-ID: <20121015104456.GR21164@n2100.arm.linux.org.uk> References: <20121015094348.GP21164@n2100.arm.linux.org.uk> <20121015103725.GQ21164@n2100.arm.linux.org.uk> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20121015103725.GQ21164@n2100.arm.linux.org.uk> User-Agent: Mutt/1.5.19 (2009-01-05) Sender: linux-mmc-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-mmc@vger.kernel.org On Mon, Oct 15, 2012 at 11:37:25AM +0100, Russell King - ARM Linux wrote: > On Mon, Oct 15, 2012 at 10:43:48AM +0100, Russell King - ARM Linux wrote: > > 1. Unregister the device _BEFORE_ taking away any resources it may > > be using. > > 2. Don't check clks against NULL. > > > > Signed-off-by: Russell King > > Looking at this driver some more, who the hell came up with the sdhci > registration interface? It violates one of the most fundamental > principles of kernel driver programming. You do _NOT_ publish your > driver interfaces _UNTIL_ you have finished setting your device up. > Otherwise, in a preemptible or SMP kernel, your driver can be used > before the initialization has completed. > > As this driver calls sdhci_pltfm_register() before it has obtained the > clock for the interface, and this function does: > sdhci_pltfm_init > sdhci_add_host > mmc_add_host > mmc_start_host > mmc_power_up > mmc_set_ios > sdhci_set_ios > > See, we're trying to power up and clock the card _before_ the dove > sdhci driver has even claimed the clock let alone enabled it. This > is total bollocks. The sdhci platform interface is total crap for > creating this broken design in the first place. This is why MMC has > the init + add interfaces, they're there to allow drivers to do stuff > the Right(tm) way and avoid shit like the above. > > This should have been picked up at review time before the driver went > into mainline. In any case, it needs to be fixed. Here's an updated patch which just about fixes the sdhci-dove driver. I would not be surprised given the idiotic sdhci-pltfm API if many other drivers suffered the same bug. 8<==== From: Russell King Subject: [PATCH] MMC: fix sdhci-dove probe/removal 1. Never ever publish a device in the system before it has been setup to a usable state. 2. Unregister the device _BEFORE_ taking away any resources it may be using. 3. Don't check clks against NULL. Signed-off-by: Russell King --- drivers/mmc/host/sdhci-dove.c | 36 ++++++++++++++++++------------------ 1 files changed, 18 insertions(+), 18 deletions(-) diff --git a/drivers/mmc/host/sdhci-dove.c b/drivers/mmc/host/sdhci-dove.c index a6e53a1..7d3a4e4 100644 --- a/drivers/mmc/host/sdhci-dove.c +++ b/drivers/mmc/host/sdhci-dove.c @@ -83,30 +83,31 @@ static int __devinit sdhci_dove_probe(struct platform_device *pdev) struct sdhci_dove_priv *priv; int ret; - ret = sdhci_pltfm_register(pdev, &sdhci_dove_pdata); - if (ret) - goto sdhci_dove_register_fail; - priv = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_dove_priv), GFP_KERNEL); if (!priv) { dev_err(&pdev->dev, "unable to allocate private data"); - ret = -ENOMEM; - goto sdhci_dove_allocate_fail; + return -ENOMEM; } + priv->clk = clk_get(&pdev->dev, NULL); + if (!IS_ERR(priv->clk)) + clk_prepare_enable(priv->clk); + + ret = sdhci_pltfm_register(pdev, &sdhci_dove_pdata); + if (ret) + goto sdhci_dove_register_fail; + host = platform_get_drvdata(pdev); pltfm_host = sdhci_priv(host); pltfm_host->priv = priv; - priv->clk = clk_get(&pdev->dev, NULL); - if (!IS_ERR(priv->clk)) - clk_prepare_enable(priv->clk); return 0; -sdhci_dove_allocate_fail: - sdhci_pltfm_unregister(pdev); sdhci_dove_register_fail: + clk_unprepare_disable(priv->clk); + clk_put(priv->clk); +sdhci_dove_allocate_fail: return ret; } @@ -116,14 +117,13 @@ static int __devexit sdhci_dove_remove(struct platform_device *pdev) struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); struct sdhci_dove_priv *priv = pltfm_host->priv; - if (priv->clk) { - if (!IS_ERR(priv->clk)) { - clk_disable_unprepare(priv->clk); - clk_put(priv->clk); - } - devm_kfree(&pdev->dev, priv->clk); + sdhci_pltfm_unregister(pdev); + + if (!IS_ERR(priv->clk)) { + clk_disable_unprepare(priv->clk); + clk_put(priv->clk); } - return sdhci_pltfm_unregister(pdev); + return 0; } static struct platform_driver sdhci_dove_driver = {