Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp3061267ybi; Thu, 18 Jul 2019 20:13:34 -0700 (PDT) X-Google-Smtp-Source: APXvYqyTSPTkQS9duTVoOEUpX1634R4/Mp76wV4FHeb9tDDyFNUi8hR5J3WQAt+Zsp53hjWAeFyv X-Received: by 2002:a17:90a:bf0e:: with SMTP id c14mr52507132pjs.55.1563506014692; Thu, 18 Jul 2019 20:13:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563506014; cv=none; d=google.com; s=arc-20160816; b=eLR4nJfeq1Yc9+ANWSQkLFGOiRITXb0d3Ib+r+DL2wqFDmp7WkA7Ce6YRQWlXN1rEC Vr4yW1N1zHiH+FQAWrqNzqXdidjtRXOGVJJSmxnQf4ydWwhes14nh4XRLf70vhobRA/+ f4TyJ5v/56Qd25xydA8HsD0e4pCh7l3Oo4n60VPh6OpN+xzytu8+xQKXozzI/aCuR5hP t6sFdm4BS4i4McaCYZ5U0vA+d24D18QbOsW4nbEUKTQ+wPayb6Ds+QyXEcpoxs/e/FPy QuXUrHnD1N+zlgYVYMLcJY72/xCR/qas0G68ouyZTQIUO8nGk34Ef+iu+MpHlAY05+oJ qoKQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from; bh=qsa6PagcoHt1h/le7wPFfn9Hhujlz40aleHasivnY+Q=; b=TEZ2u2SslPeNNe6zR9J2LEa7tP64LbTo5rogsMZyBx2S3Y71lUAzv4cdi79Zj9wDBg rVnlQCmq7XFouX+yrpjG5ubkUFsGUmeU1brW1ZwGIVAkNACnTtVZFu8oyYSJMEohHtWe cv+H5p9+kYsdSDZuD6LtRNcpH/8W5Jl9Q/2/SyYNJf161H7STCoPDbcqQ3gskFRxNqWV 54XUIZgt4iK1DHkepKXI2P+5nQiBhIsO5T1KphTd3XoYFbbqoyyef9T3GXT47i9i1lb6 IbBP25fIZ0QbeCbpEaW4IyxYQ26VVdHN0P8+nM0hRVkz2q/WL9iWPKcKqEg7Ol5C808L zsjQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v10si125905pgq.17.2019.07.18.20.13.04; Thu, 18 Jul 2019 20:13:34 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726147AbfGSDKW (ORCPT + 99 others); Thu, 18 Jul 2019 23:10:22 -0400 Received: from inva021.nxp.com ([92.121.34.21]:57136 "EHLO inva021.nxp.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726055AbfGSDKV (ORCPT ); Thu, 18 Jul 2019 23:10:21 -0400 Received: from inva021.nxp.com (localhost [127.0.0.1]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id 4C2EA200205; Fri, 19 Jul 2019 05:10:19 +0200 (CEST) Received: from invc005.ap-rdc01.nxp.com (invc005.ap-rdc01.nxp.com [165.114.16.14]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id 4A4AA200072; Fri, 19 Jul 2019 05:10:16 +0200 (CEST) Received: from titan.ap.freescale.net (TITAN.ap.freescale.net [10.192.208.233]) by invc005.ap-rdc01.nxp.com (Postfix) with ESMTP id 3BB04402A9; Fri, 19 Jul 2019 11:10:12 +0800 (SGT) From: Anson.Huang@nxp.com To: a.zummo@towertech.it, alexandre.belloni@bootlin.com, linux-rtc@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Linux-imx@nxp.com Subject: [PATCH V2] rtc: snvs: fix possible race condition Date: Fri, 19 Jul 2019 11:01:02 +0800 Message-Id: <20190719030102.6141-1-Anson.Huang@nxp.com> X-Mailer: git-send-email 2.9.5 X-Virus-Scanned: ClamAV using ClamSMTP Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Anson Huang The RTC IRQ is requested before the struct rtc_device is allocated, this may lead to a NULL pointer dereference in IRQ handler. To fix this issue, allocating the rtc_device struct and register rtc device before requesting the RTC IRQ. Using devm_rtc_allocate_device/rtc_register_device instead of devm_rtc_device_register. Signed-off-by: Anson Huang --- Changes since V1: - move devm_request_irq() to after rtc device register done, make sure everything is ready before enabling IRQ. --- drivers/rtc/rtc-snvs.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/drivers/rtc/rtc-snvs.c b/drivers/rtc/rtc-snvs.c index 7ee673a2..d9650e7 100644 --- a/drivers/rtc/rtc-snvs.c +++ b/drivers/rtc/rtc-snvs.c @@ -279,6 +279,10 @@ static int snvs_rtc_probe(struct platform_device *pdev) if (!data) return -ENOMEM; + data->rtc = devm_rtc_allocate_device(&pdev->dev); + if (IS_ERR(data->rtc)) + return PTR_ERR(data->rtc); + data->regmap = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "regmap"); if (IS_ERR(data->regmap)) { @@ -335,6 +339,13 @@ static int snvs_rtc_probe(struct platform_device *pdev) if (ret) dev_err(&pdev->dev, "failed to enable irq wake\n"); + data->rtc->ops = &snvs_rtc_ops; + ret = rtc_register_device(data->rtc); + if (ret) { + dev_err(&pdev->dev, "failed to register rtc: %d\n", ret); + goto error_rtc_device_register; + } + ret = devm_request_irq(&pdev->dev, data->irq, snvs_rtc_irq_handler, IRQF_SHARED, "rtc alarm", &pdev->dev); if (ret) { @@ -343,14 +354,6 @@ static int snvs_rtc_probe(struct platform_device *pdev) goto error_rtc_device_register; } - data->rtc = devm_rtc_device_register(&pdev->dev, pdev->name, - &snvs_rtc_ops, THIS_MODULE); - if (IS_ERR(data->rtc)) { - ret = PTR_ERR(data->rtc); - dev_err(&pdev->dev, "failed to register rtc: %d\n", ret); - goto error_rtc_device_register; - } - return 0; error_rtc_device_register: -- 2.7.4