From patchwork Fri Sep 25 14:07:52 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atsushi Nemoto X-Patchwork-Id: 34275 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from mail-px0-f156.google.com (mail-px0-f156.google.com [209.85.216.156]) by ozlabs.org (Postfix) with ESMTP id 2C9D6B7B76 for ; Sat, 26 Sep 2009 00:07:56 +1000 (EST) Received: by pxi28 with SMTP id 28so1171285pxi.2 for ; Fri, 25 Sep 2009 07:07:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=beta; h=domainkey-signature:received:received:x-sender:x-apparently-to :received:received:received:received-spf:received:from:to:cc:subject :date:message-id:x-mailer:reply-to:sender:precedence:x-google-loop :mailing-list:list-id:list-post:list-help:list-unsubscribe :x-beenthere-env:x-beenthere; bh=sMN04MIsek54wnvi3nE6pm8JqBiLbXQwVCHE4TXflq0=; b=2LXpFjID2b7J07cSR/SSX6NwQmtnGBqmJnmVjvJCcpOTzmf1ZdBJ3ZChgmx/REzwbF 6zNqlBGFBZxHDN1RnLjbERxaWPkoWxXA+xbtl9z/7/U3SuXh+YC0p3lJbCEjPSSJvYkL LzpElFVRxgiqfZ9Z8val4/L6VCpXcanWyXjxE= DomainKey-Signature: a=rsa-sha1; c=nofws; d=googlegroups.com; s=beta; h=x-sender:x-apparently-to:received-spf:authentication-results:from :to:cc:subject:date:message-id:x-mailer:reply-to:sender:precedence :x-google-loop:mailing-list:list-id:list-post:list-help :list-unsubscribe:x-beenthere-env:x-beenthere; b=eYTh12W7q6B4f0yLaKUd7CJZiUfHfe2sBKje0j9WSv0LtpvXmUEA2fDgLwffWbqQqc fz7zbjldvOqZPsG4hrsanMH8xve3ZJlknzL2k1ziBZLYs9r1oyWqbve7Ov40HId0+XVp Rc/u2SPI4i/P7h+WJIYQPWn9iRGsihEV3WE4Y= Received: by 10.142.7.2 with SMTP id 2mr4718wfg.34.1253887674263; Fri, 25 Sep 2009 07:07:54 -0700 (PDT) Received: by 10.107.167.28 with SMTP id u28gr2901pro.0; Fri, 25 Sep 2009 07:07:54 -0700 (PDT) X-Sender: anemo@mba.ocn.ne.jp X-Apparently-To: rtc-linux@googlegroups.com Received: by 10.143.21.21 with SMTP id y21mr11051wfi.28.1253887673203; Fri, 25 Sep 2009 07:07:53 -0700 (PDT) Received: by 10.143.21.21 with SMTP id y21mr11050wfi.28.1253887673183; Fri, 25 Sep 2009 07:07:53 -0700 (PDT) Received: from smtp.mba.ocn.ne.jp (mba.ocn.ne.jp [122.28.14.163]) by gmr-mx.google.com with ESMTP id 4si1060655pxi.6.2009.09.25.07.07.52; Fri, 25 Sep 2009 07:07:53 -0700 (PDT) Received-SPF: pass (google.com: domain of anemo@mba.ocn.ne.jp designates 122.28.14.163 as permitted sender) client-ip=122.28.14.163; Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of anemo@mba.ocn.ne.jp designates 122.28.14.163 as permitted sender) smtp.mail=anemo@mba.ocn.ne.jp Received: from localhost.localdomain (p8215-ipad310funabasi.chiba.ocn.ne.jp [123.217.210.215]) by smtp.mba.ocn.ne.jp (Postfix) with ESMTP id 74CF26F2E; Fri, 25 Sep 2009 23:07:51 +0900 (JST) From: Atsushi Nemoto To: Alessandro Zummo Cc: rtc-linux@googlegroups.com, linux-kernel@vger.kernel.org, akpm@linux-foundation.org Subject: [rtc-linux] [PATCH 3/3] rtc-tx4939: Fix races around device registration Date: Fri, 25 Sep 2009 23:07:52 +0900 Message-Id: <1253887672-16250-3-git-send-email-anemo@mba.ocn.ne.jp> X-Mailer: git-send-email 1.5.6.5 Reply-To: rtc-linux@googlegroups.com Sender: rtc-linux@googlegroups.com Precedence: bulk X-Google-Loop: groups Mailing-List: list rtc-linux@googlegroups.com; contact rtc-linux+owner@googlegroups.com List-Id: List-Post: List-Help: List-Unsubscribe: , X-BeenThere-Env: rtc-linux@googlegroups.com X-BeenThere: rtc-linux@googlegroups.com * Use its own spinlock instead of rtc->irq_lock * Check pdata->rtc before calling rtc_update_irq * Disable interrupt after rtc_device_unregister Signed-off-by: Atsushi Nemoto --- drivers/rtc/rtc-tx4939.c | 51 +++++++++++++++++++++++---------------------- 1 files changed, 26 insertions(+), 25 deletions(-) diff --git a/drivers/rtc/rtc-tx4939.c b/drivers/rtc/rtc-tx4939.c index 4a6ed11..9ee81d8 100644 --- a/drivers/rtc/rtc-tx4939.c +++ b/drivers/rtc/rtc-tx4939.c @@ -17,6 +17,7 @@ struct tx4939rtc_plat_data { struct rtc_device *rtc; struct tx4939_rtc_reg __iomem *rtcreg; + spinlock_t lock; }; static struct tx4939rtc_plat_data *get_tx4939rtc_plat_data(struct device *dev) @@ -52,14 +53,14 @@ static int tx4939_rtc_set_mmss(struct device *dev, unsigned long secs) buf[3] = secs >> 8; buf[4] = secs >> 16; buf[5] = secs >> 24; - spin_lock_irq(&pdata->rtc->irq_lock); + spin_lock_irq(&pdata->lock); __raw_writel(0, &rtcreg->adr); for (i = 0; i < 6; i++) __raw_writel(buf[i], &rtcreg->dat); ret = tx4939_rtc_cmd(rtcreg, TX4939_RTCCTL_COMMAND_SETTIME | (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME)); - spin_unlock_irq(&pdata->rtc->irq_lock); + spin_unlock_irq(&pdata->lock); return ret; } @@ -71,18 +72,18 @@ static int tx4939_rtc_read_time(struct device *dev, struct rtc_time *tm) unsigned long sec; unsigned char buf[6]; - spin_lock_irq(&pdata->rtc->irq_lock); + spin_lock_irq(&pdata->lock); ret = tx4939_rtc_cmd(rtcreg, TX4939_RTCCTL_COMMAND_GETTIME | (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME)); if (ret) { - spin_unlock_irq(&pdata->rtc->irq_lock); + spin_unlock_irq(&pdata->lock); return ret; } __raw_writel(2, &rtcreg->adr); for (i = 2; i < 6; i++) buf[i] = __raw_readl(&rtcreg->dat); - spin_unlock_irq(&pdata->rtc->irq_lock); + spin_unlock_irq(&pdata->lock); sec = (buf[5] << 24) | (buf[4] << 16) | (buf[3] << 8) | buf[2]; rtc_time_to_tm(sec, tm); return rtc_valid_tm(tm); @@ -110,13 +111,13 @@ static int tx4939_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) buf[3] = sec >> 8; buf[4] = sec >> 16; buf[5] = sec >> 24; - spin_lock_irq(&pdata->rtc->irq_lock); + spin_lock_irq(&pdata->lock); __raw_writel(0, &rtcreg->adr); for (i = 0; i < 6; i++) __raw_writel(buf[i], &rtcreg->dat); ret = tx4939_rtc_cmd(rtcreg, TX4939_RTCCTL_COMMAND_SETALARM | (alrm->enabled ? TX4939_RTCCTL_ALME : 0)); - spin_unlock_irq(&pdata->rtc->irq_lock); + spin_unlock_irq(&pdata->lock); return ret; } @@ -129,12 +130,12 @@ static int tx4939_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) unsigned char buf[6]; u32 ctl; - spin_lock_irq(&pdata->rtc->irq_lock); + spin_lock_irq(&pdata->lock); ret = tx4939_rtc_cmd(rtcreg, TX4939_RTCCTL_COMMAND_GETALARM | (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME)); if (ret) { - spin_unlock_irq(&pdata->rtc->irq_lock); + spin_unlock_irq(&pdata->lock); return ret; } __raw_writel(2, &rtcreg->adr); @@ -143,7 +144,7 @@ static int tx4939_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) ctl = __raw_readl(&rtcreg->ctl); alrm->enabled = (ctl & TX4939_RTCCTL_ALME) ? 1 : 0; alrm->pending = (ctl & TX4939_RTCCTL_ALMD) ? 1 : 0; - spin_unlock_irq(&pdata->rtc->irq_lock); + spin_unlock_irq(&pdata->lock); sec = (buf[5] << 24) | (buf[4] << 16) | (buf[3] << 8) | buf[2]; rtc_time_to_tm(sec, &alrm->time); return rtc_valid_tm(&alrm->time); @@ -153,11 +154,11 @@ static int tx4939_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled) { struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev); - spin_lock_irq(&pdata->rtc->irq_lock); + spin_lock_irq(&pdata->lock); tx4939_rtc_cmd(pdata->rtcreg, TX4939_RTCCTL_COMMAND_NOP | (enabled ? TX4939_RTCCTL_ALME : 0)); - spin_unlock_irq(&pdata->rtc->irq_lock); + spin_unlock_irq(&pdata->lock); return 0; } @@ -167,13 +168,14 @@ static irqreturn_t tx4939_rtc_interrupt(int irq, void *dev_id) struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg; unsigned long events = RTC_IRQF; - spin_lock(&pdata->rtc->irq_lock); + spin_lock(&pdata->lock); if (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALMD) { events |= RTC_AF; tx4939_rtc_cmd(rtcreg, TX4939_RTCCTL_COMMAND_NOP); } - spin_unlock(&pdata->rtc->irq_lock); - rtc_update_irq(pdata->rtc, 1, events); + spin_unlock(&pdata->lock); + if (likely(pdata->rtc)) + rtc_update_irq(pdata->rtc, 1, events); return IRQ_HANDLED; } @@ -194,13 +196,13 @@ static ssize_t tx4939_rtc_nvram_read(struct kobject *kobj, struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg; ssize_t count; - spin_lock_irq(&pdata->rtc->irq_lock); + spin_lock_irq(&pdata->lock); for (count = 0; size > 0 && pos < TX4939_RTC_REG_RAMSIZE; count++, size--) { __raw_writel(pos++, &rtcreg->adr); *buf++ = __raw_readl(&rtcreg->dat); } - spin_unlock_irq(&pdata->rtc->irq_lock); + spin_unlock_irq(&pdata->lock); return count; } @@ -213,13 +215,13 @@ static ssize_t tx4939_rtc_nvram_write(struct kobject *kobj, struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg; ssize_t count; - spin_lock_irq(&pdata->rtc->irq_lock); + spin_lock_irq(&pdata->lock); for (count = 0; size > 0 && pos < TX4939_RTC_REG_RAMSIZE; count++, size--) { __raw_writel(pos++, &rtcreg->adr); __raw_writel(*buf++, &rtcreg->dat); } - spin_unlock_irq(&pdata->rtc->irq_lock); + spin_unlock_irq(&pdata->lock); return count; } @@ -259,6 +261,7 @@ static int __init tx4939_rtc_probe(struct platform_device *pdev) if (!pdata->rtcreg) return -EBUSY; + spin_lock_init(&pdata->lock); tx4939_rtc_cmd(pdata->rtcreg, TX4939_RTCCTL_COMMAND_NOP); if (devm_request_irq(&pdev->dev, irq, tx4939_rtc_interrupt, IRQF_DISABLED, pdev->name, &pdev->dev) < 0) @@ -277,14 +280,12 @@ static int __init tx4939_rtc_probe(struct platform_device *pdev) static int __exit tx4939_rtc_remove(struct platform_device *pdev) { struct tx4939rtc_plat_data *pdata = platform_get_drvdata(pdev); - struct rtc_device *rtc = pdata->rtc; - spin_lock_irq(&rtc->irq_lock); - tx4939_rtc_cmd(pdata->rtcreg, TX4939_RTCCTL_COMMAND_NOP); - spin_unlock_irq(&rtc->irq_lock); sysfs_remove_bin_file(&pdev->dev.kobj, &tx4939_rtc_nvram_attr); - rtc_device_unregister(rtc); - platform_set_drvdata(pdev, NULL); + rtc_device_unregister(pdata->rtc); + spin_lock_irq(&pdata->lock); + tx4939_rtc_cmd(pdata->rtcreg, TX4939_RTCCTL_COMMAND_NOP); + spin_unlock_irq(&pdata->lock); return 0; }