Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp121957rwb; Tue, 8 Nov 2022 23:27:15 -0800 (PST) X-Google-Smtp-Source: AMsMyM6Uds1Zy6oShOvbLYvinomeQenTo2X3WyT4lnRxzohrS2kjd5Aqv/11cB6I2XKrkJ2SGmKY X-Received: by 2002:a17:907:3e92:b0:7ad:d4f1:11d8 with SMTP id hs18-20020a1709073e9200b007add4f111d8mr46901904ejc.291.1667978834873; Tue, 08 Nov 2022 23:27:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667978834; cv=none; d=google.com; s=arc-20160816; b=vJo9NOUyz+hfIANHyMy+VUXnCiNkK1nKP80iZQsU1d1zy6W2vnZhdqhVXiPxsa6U9I 1nE0YKbaeNmkxcYepXg9I3IAJIclQtzeiJTidxP8NLmAKTnOUKxcnwXu6DiThKYyHnAI 6YIywKC6KYXkjQ7bObvP5vHPp++GwQJUeedpmAUouUZqh3hB47CPPMueWfSPO+YotL/+ GV+qxP33UbzvEmUvRhh+/UNg1u8L5VAHIj7o/JV42jvUWy67JD8lsSYXUn5Ql6Uk9TNz XPiqdIgQ6pwqewQAq7FzlQlttBpqsxX33ZXOdI3jTE39NwntTlpIUyiGME2l1Me74z1A Ykeg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=gK3FfLZ7jALtYz8IRz5iTZefBK65iZa94pkBVKl5Zxo=; b=lCuSyvmF5CoQYVZbuxqf9KNRMo3iu4reWG1mP4j4jKGOn0fVdJjc68NJ7w4Q/FQeFy CFDw4Wh62nkWsSrKGJqTzXGnfR9pLLGJ+p+/grETPYLzjTlqQIeDtSbZ6rkUSO0cSk28 iun+JfdI4Fv6aCNV1xgrv238yna2cft7+XyYR7MqRIKj5Se7Vjn++/rk0h+XGGFIl0IE NdtzD3vezFLue3yy1dzFK4beh2Iu5bHuc4qJAQlrVrL4RrQfiDu20/L8lT3kqa+HJvKz z/aHMX+ZmVkeIGJAqYRYD4p8+o9iRQZQjyiaqrZBoruoJdFEZiHHGk0r9UX1H9eLHg0K IeCQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p11-20020a056402500b00b004614e263f51si14744164eda.118.2022.11.08.23.26.40; Tue, 08 Nov 2022 23:27:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229801AbiKIGvr (ORCPT + 93 others); Wed, 9 Nov 2022 01:51:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50630 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229787AbiKIGvn (ORCPT ); Wed, 9 Nov 2022 01:51:43 -0500 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8BCC818B03; Tue, 8 Nov 2022 22:51:42 -0800 (PST) Received: from dggpeml500023.china.huawei.com (unknown [172.30.72.55]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4N6bCW4ljXzDsJM; Wed, 9 Nov 2022 14:48:39 +0800 (CST) Received: from dggpeml500003.china.huawei.com (7.185.36.200) by dggpeml500023.china.huawei.com (7.185.36.114) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Wed, 9 Nov 2022 14:51:40 +0800 Received: from huawei.com (10.175.103.91) by dggpeml500003.china.huawei.com (7.185.36.200) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Wed, 9 Nov 2022 14:51:40 +0800 From: Yu Liao To: , CC: , , , Subject: [PATCH v1 2/2] rtc: fix race condition in rtc_set_time() Date: Wed, 9 Nov 2022 14:41:47 +0800 Message-ID: <20221109064147.1836133-3-liaoyu15@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221109064147.1836133-1-liaoyu15@huawei.com> References: <20221109064147.1836133-1-liaoyu15@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.103.91] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpeml500003.china.huawei.com (7.185.36.200) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-2.2 required=5.0 tests=BAYES_00,HK_RANDOM_ENVFROM, HK_RANDOM_FROM,RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit 7e7c005b4b1f ("rtc: disable uie before setting time and enable after") was introduced to solve problem that rtc_timer_do_work will loop for a while, when setting the time in the future with the uie timer enabled. But reading uie timer state and setting rtc time are not in a critical section, a race condition may occur in rtc_set_time which has two issues: 1) In the above scenario, rtc_timer_do_work may still loop for a while. For example consider the following sequence: CPU0 CPU1 ---- ---- ioctl(RTC_SET_TIME) ioctl(RTC_UIE_ON) uie = rtc->uie_rtctimer.enabled; [ assume uie is 0 ] if (uie) rtc_update_irq_enable(rtc, 0); rtc_update_irq_enable(rtc, 1); [ uie is enabled ] rtc->ops->set_time(); [ set rtc time in the future ] 2) A thread try to turn off uie, however rtc_settime called by another thread turns on uie when they run in parallel. Consider the following sequence: CPU0 CPU1 ---- ---- ioctl(RTC_SET_TIME) ioctl(RTC_UIE_OFF) rtc->ops->set_time(); rtc_update_irq_enable(rtc, 0); rtc_update_irq_enable(rtc, 1); Fix it by guaranteeing that reading uie timer state, setting rtc time and enabling uie timer within a critical section. Fixes: 7e7c005b4b1f ("rtc: disable uie before setting time and enable after") Signed-off-by: Yu Liao --- drivers/rtc/interface.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c index bc55dd31bece..ef78e2977330 100644 --- a/drivers/rtc/interface.c +++ b/drivers/rtc/interface.c @@ -139,21 +139,23 @@ int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm) rtc_subtract_offset(rtc, tm); + err = mutex_lock_interruptible(&rtc->ops_lock); + if (err) + return err; + #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL uie = rtc->uie_rtctimer.enabled || rtc->uie_irq_active; #else uie = rtc->uie_rtctimer.enabled; #endif if (uie) { - err = rtc_update_irq_enable(rtc, 0); - if (err) + err = __rtc_update_irq_enable(rtc, 0); + if (err) { + mutex_unlock(&rtc->ops_lock); return err; + } } - err = mutex_lock_interruptible(&rtc->ops_lock); - if (err) - return err; - if (!rtc->ops) err = -ENODEV; else if (rtc->ops->set_time) @@ -162,15 +164,17 @@ int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm) err = -EINVAL; pm_stay_awake(rtc->dev.parent); - mutex_unlock(&rtc->ops_lock); /* A timer might have just expired */ schedule_work(&rtc->irqwork); if (uie) { - err = rtc_update_irq_enable(rtc, 1); - if (err) + err = __rtc_update_irq_enable(rtc, 1); + if (err) { + mutex_unlock(&rtc->ops_lock); return err; + } } + mutex_unlock(&rtc->ops_lock); trace_rtc_set_time(rtc_tm_to_time64(tm), err); return err; -- 2.25.1