From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4F35BC27C53 for ; Wed, 5 Jun 2024 13:51:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:CC:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=KzY3WH9bs/2KNgzyM2XUPhNZy5URtrQ9lzBK4Xqh0Is=; b=ShFw0ZsfM9wBF+ 69TZmXWOE9T53oLRHqt7NRlh/xx3aK0CGa1nPe/Hjw5evxHvN9nNKWskEr2Jql7miJ/01CqeWctk0 b2+JVuJn+6ggMgk0nVQxBS7ZERhgoTOCNpDTgD954oB1AyyDP31hCENP18BEbFjrH8DnadM1KlZl0 4Ac/VPPCTAd/3j3V9vgPmNymukFs4nd4odLuGzVHDQA9z5jNpI95zoUrHiL3oW3iaodIIVtBxFxNn iEYleBGxhAm4q1eQH3jKVZffmW1eieTsxzIb+NBjIY5hji17JxLwnGtg+U2V3ap8YUmUi10MOokiP Pq3B6oUnhgIRrD2eRvKQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sEr2u-00000006Lum-1KnW; Wed, 05 Jun 2024 13:51:20 +0000 Received: from mailgw01.mediatek.com ([216.200.240.184]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sEr2r-00000006LsY-3mw5 for linux-arm-kernel@lists.infradead.org; Wed, 05 Jun 2024 13:51:19 +0000 X-UUID: ab3e97aa234211ef82c84f6416ccf2f3-20240605 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:CC:To:From; bh=JYpZKB8QAW8ggxby7kMOqF+pDIxp0qgYSQ23xHr7lHM=; b=Sp2NGaebFK7Pp1EGzN+dXB2s1ZQomfwFOGLYr/qVZA/IvBwfK9O4xxDFVMe2A2tSZbYQElLdXNL/9hdM4qideBKJi9il2ZGTM843LNrfAjwEQcIH24Kj1eBG7YDqSOlfMaUrerAXKK+SlmCVl1zdHeX9nw+jxvgi13eIeTZaNMM=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.39,REQID:b2aeea8f-a75b-45a0-85b8-31b19bf7bdb8,IP:0,U RL:0,TC:0,Content:0,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTION: release,TS:0 X-CID-META: VersionHash:393d96e,CLOUDID:c1eda693-e2c0-40b0-a8fe-7c7e47299109,B ulkID:nil,BulkQuantity:0,Recheck:0,SF:102,TC:nil,Content:0,EDM:-3,IP:nil,U RL:0,File:nil,RT:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA:0,AV:0,LES:1, SPR:NO,DKR:0,DKP:0,BRR:0,BRE:0,ARC:0 X-CID-BVR: 0 X-CID-BAS: 0,_,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR X-UUID: ab3e97aa234211ef82c84f6416ccf2f3-20240605 Received: from mtkmbs09n1.mediatek.inc [(172.21.101.35)] by mailgw01.mediatek.com (envelope-from ) (musrelay.mediatek.com ESMTP with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 1067908588; Wed, 05 Jun 2024 06:51:11 -0700 Received: from mtkmbs13n2.mediatek.inc (172.21.101.108) by MTKMBS14N1.mediatek.inc (172.21.101.75) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.26; Wed, 5 Jun 2024 21:51:08 +0800 Received: from mtksdccf07.mediatek.inc (172.21.84.99) by mtkmbs13n2.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1118.26 via Frontend Transport; Wed, 5 Jun 2024 21:51:08 +0800 From: Phil Chang To: CC: , , , , , , , , Subject: Re: [PATCH] hrtimer: check hrtimer with a NULL function Date: Wed, 5 Jun 2024 21:51:06 +0800 Message-ID: <20240605135106.16065-1-phil.chang@mediatek.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <87plsvvgpn.fsf@somnus> References: <87plsvvgpn.fsf@somnus> MIME-Version: 1.0 X-TM-AS-Product-Ver: SMEX-14.0.0.3152-9.1.1006-23728.005 X-TM-AS-Result: No-10--9.629500-8.000000 X-TMASE-MatchedRID: vbSD0OnL8/I4HKI/yaqRm0hwlOfYeSqxgRykyfrH1xlNShtWKzkFv14r Z+EaD3uZ2xDStkOROV+KjSD+BEuGCyjrjvzj49diiRSHMuHguxBAq6/y5AEOOhae5J4l8aLzpwO Vv9hS1homWOdWIcQ1bRmrh2/8FzyKQReF5Rcm8scXrP0cYcrA2zhaxI2If9ReDZjzHLL3eRY3Bo tHDlVWrPA0WF4WJM3ZQhm5m+SJzSb1FVVkgOCqk+0yyL51qL/RyeUl7aCTy8gzs7oxv6pPASrgy brSX+LZkxZqoEsW6HDZxSvqBpsQLDbcsMCH+ZLFq1ZJZ2z+SbYhauGyjTkf9avM+zzl/BST3FfO Xa5ZTXj8aLTyW2a3YsTEEesDrPniorSG4OrF3ojmAId+2bAQwsDyPNek9e/amyiLZetSf8lBgw5 G4NYCZDDmMp5UGBhvC24oEZ6SpSk6XEE7Yhw4FiYaQXAhWj/ixEQ4ODpWkCiZKj/pr9IiWM0bxa MOP4nwn1FdFl1ht2M= X-TM-AS-User-Approved-Sender: No X-TM-AS-User-Blocked-Sender: No X-TMASE-Result: 10--9.629500-8.000000 X-TMASE-Version: SMEX-14.0.0.3152-9.1.1006-23728.005 X-TM-SNTS-SMTP: 79D5D9248368AD314CA5C9B7DBFD18BD47287B77DC2FE410921A2872ADDF6D0D2000:8 X-MTK: N X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240605_065117_977228_2789DDB8 X-CRM114-Status: GOOD ( 23.92 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org >> simillar with timers, check for timer->function == NULL. >> If the pointer is NULL, discard the request silently. > Can you please explain, why this change is required? > The statement "similar to timers" is not a valid explaination as timer > list timers and hrtimers are two different things. The function pointer > for timer list timers is explicitly set to NULL in shutdown path to > prevent unwanted rearming of the timer. For hrtimers there is no > shutdown function implemented and function is never set to NULL by > hrtimer code. > The timer->function is provided by caller, which is invaild if fuction is NULL, and currently, the hrtime code does not perform any checks to validate this. Passing a NULL function can lead to a system panic, with a backtrace likes: ``` __hrtimer_run_queues+0x1d8/0x3b8 hrtimer_interrupt+0xdc/0x3a0 arch_timer_handler_phys+0x54/0x94 handle_percpu_devid_irq+0xb8/0x308 handle_domain_irq+0x78/0xec gic_handle_irq+0x50/0x10c call_on_irq_stack+0x38/0x54 do_interrupt_handler+0x40/0x98 ``` This backtrace does not clearly indicate the source of the invalid usage of hrtimer. >> Signed-off-by: Phil Chang >> --- >> kernel/time/hrtimer.c | 10 ++++++++++ >> 1 file changed, 10 insertions(+) >> >> diff --git a/kernel/time/hrtimer.c b/kernel/time/hrtimer.c >> index 492c14aac642..72d6e7bc9cd9 100644 >> --- a/kernel/time/hrtimer.c >> +++ b/kernel/time/hrtimer.c >> @@ -1297,9 +1297,13 @@ void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim, >> >> base = lock_hrtimer_base(timer, &flags); >> >> + if (!timer->function) >> + goto out; > > When this happens, user of hrtimers do not follow the semantics of > hrtimers which means this is a bug. > Agree, how about BUG_ON here ? >> + >> if (__hrtimer_start_range_ns(timer, tim, delta_ns, mode, base)) >> hrtimer_reprogram(timer, true); >> >> +out: >> unlock_hrtimer_base(timer, &flags); >> } >> EXPORT_SYMBOL_GPL(hrtimer_start_range_ns); >> @@ -1667,6 +1671,11 @@ static void __run_hrtimer(struct hrtimer_cpu_base *cpu_base, >> __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE, 0); >> fn = timer->function; >> >> + if (WARN_ON_ONCE(!fn)) { >> + /* Should never happen. */ > > ...same as above... > >> + goto out; >> + } >> + >> /* >> * Clear the 'is relative' flag for the TIME_LOW_RES case. If the >> * timer is restarted with a period then it becomes an absolute >> @@ -1710,6 +1719,7 @@ static void __run_hrtimer(struct hrtimer_cpu_base *cpu_base, >> * hrtimer_active() cannot observe base->running.timer == NULL && >> * timer->state == INACTIVE. >> */ >> +out: >> raw_write_seqcount_barrier(&base->seq); >> >> WARN_ON_ONCE(base->running != timer); > > > Thanks, > > Anna-Maria Thank You _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel