From nobody Sun Dec 14 21:54:00 2025 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8B7CA242249 for ; Wed, 15 Jan 2025 13:41:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.165.32 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736948505; cv=none; b=nJ7fjPtBovBb6U3OG00V8XkWpi41eUm+BkCDeWgabZri4etQU+huJp0+2kUdbcoLQCYr3nZwHtXqJCKsR2dcyWQGudGPld56OP+AJLYLiBnh7FmKRx5x+pSopQmW+CI0YUZUBbZaLJRSi4Yrn0jOtuAqRmlY0lyKEx8V/6nGfII= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736948505; c=relaxed/simple; bh=2b8kaFsmptBxNllM469JMWLKSEf7V8y18Scf6sl0VSQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Jost38nj1tn6tB1IK6pFM4EAZ5sWClIydsR7sEwm0ljwJWsW518ojt26qcMYOQ8u4xG4bDG1UsZTvsQe0h2JmeqUsV5g8j7/BhI2/4wamXsKpwDUXQf9TojrjQWk/fagGq44iYMUcPSkO+pPcXpxKHwcXieG23P0yWGEc7ISaNM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com; spf=pass smtp.mailfrom=oracle.com; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b=Eb7k6Jsm; arc=none smtp.client-ip=205.220.165.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="Eb7k6Jsm" Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 50FCiiwW017442; Wed, 15 Jan 2025 13:41:30 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=corp-2023-11-20; bh=XMmi6 J+UHg0qAn1NtUl5Qd7nrl0fgJTdq9iw4mc3n/g=; b=Eb7k6Jsmc9tw5aqMBtOPa yHr6633JjP2a+nAJPqB3Baqpg2ItDDlZcAeOy7PYorOLnXhV9n2nxScePgcTmUD8 jCM2NRBzxAdDs+Z27gOosxc0TCh7zPf3Xg+tuashXS0DHi0HXy8mobqzeet7QQ2d R1ju/amU6ka1fQ6nflyCuOdhj/rnQHqGs76yn0e2SjKrde/pcTR0Vd9k5bzqpjxu 2WRxZI+Mv6/+WPw6iIXSxSloviKI9UOCpb6F9AA4zuKdqxsGMrSjRaGl0w8yGgvS Z1gIH2o8UcC9eYLf5D+LeBDSIXhMJDkkCrEWsbF45ivKgcRlKfbHahni3OiUkkOc Q== Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 443h6syyee-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 15 Jan 2025 13:41:30 +0000 (GMT) Received: from pps.filterd (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 50FCpdBn020444; Wed, 15 Jan 2025 13:41:22 GMT Received: from localhost.localdomain (dhcp-10-191-135-68.vpn.oracle.com [10.191.135.68]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTP id 443f3fsvea-2; Wed, 15 Jan 2025 13:41:21 +0000 From: Imran Khan To: anna-maria@linutronix.de, frederic@kernel.org, tglx@linutronix.de Cc: linux-kernel@vger.kernel.org Subject: [PATCH 1/2] timers: WARN if add_timer_on is used with offlined cpu. Date: Thu, 16 Jan 2025 00:41:10 +1100 Message-Id: <20250115134111.2703089-2-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250115134111.2703089-1-imran.f.khan@oracle.com> References: <20250115134111.2703089-1-imran.f.khan@oracle.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.68.34 definitions=2025-01-15_05,2025-01-15_02,2024-11-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 adultscore=0 spamscore=0 suspectscore=0 phishscore=0 malwarescore=0 mlxscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2411120000 definitions=main-2501150103 X-Proofpoint-GUID: CTq6MbkUVMFR24HBwmeIGWDwQ8ObVJtG X-Proofpoint-ORIG-GUID: CTq6MbkUVMFR24HBwmeIGWDwQ8ObVJtG Content-Type: text/plain; charset="utf-8" timer started on an offlined cpu will not fire after its expiry time and may never fire if that cpu remains offline. So add a WARN_ON_ONCE in add_timer_on, to indicate if any of its users are (wrongly) starting a timer on an offlined cpu. Suggested-by: Thomas Gleixner Signed-off-by: Imran Khan --- kernel/time/timer.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/kernel/time/timer.c b/kernel/time/timer.c index a5860bf6d16f9..ec9eb58e45241 100644 --- a/kernel/time/timer.c +++ b/kernel/time/timer.c @@ -1370,6 +1370,12 @@ void add_timer_on(struct timer_list *timer, int cpu) if (!timer->function) goto out_unlock; =20 + /* + * WARN if specified cpu is offline, because on offlined cpu + * timer will not fire even after its expiry. + */ + WARN_ON_ONCE(!cpu_online(cpu)); + if (base !=3D new_base) { timer->flags |=3D TIMER_MIGRATING; =20 --=20 2.34.1 From nobody Sun Dec 14 21:54:00 2025 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2F53324A7EA for ; Wed, 15 Jan 2025 13:41:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.177.32 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736948499; cv=none; b=ll/ZRqAVqKf/PyF6rig7Gib8rfuuEXjWWufSz6CZN/o1FIN0r02LxZnjtyV+0vsRePuJao2Js+l/3cZtejaOTKc8yxNTPzK5NYD4ygLCzr1VGT5PfUOslWII1GO6DjaX/H/hDQloov528ycnZaUGIKfgvdzaPUNyJg2JUvXRZ3k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736948499; c=relaxed/simple; bh=nFYR7pXQiFMPAZr8J1wxNBJqy0frX7GvXQZLcOr7s/c=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=pJ/DJ2H8zI9oPMt+P+2tSHvp74xE8wKdR/WkVbNNLhIIl/GfDtkVabS3BDLlgX6ZVj8ZEH8gu6hUSmBvntAt/StUCffRWKf1YFjAyL9AfsyhJTk6+2LiJIcijPZIRXZNA1kwMv1IW/FXYOJuN5GfeD6q59lr+2P9VTFk8j1enh0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com; spf=pass smtp.mailfrom=oracle.com; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b=Qsl95/VZ; arc=none smtp.client-ip=205.220.177.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="Qsl95/VZ" Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 50FCj4aE018458; Wed, 15 Jan 2025 13:41:26 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=corp-2023-11-20; bh=NzJ2C woXqA6+1Wsay2KiumrMU9JGYeaQIrJD2Z3vJWg=; b=Qsl95/VZubF8Znc7yYJIf qiukJK/yk8hq1gGvTbB3Z7oyniGPRtYZy3ZOynySPDgV4JWZLUkwnRMPDMe/BLQU X9pCtEs9WRa0yj7MlW4m9QlYkf/+jDJdqje3MpJcJVDmXkXzeRlE4hnTi7WFTtlh kcIHI/ihafM0h3uFGeQ7MIinmCiwTKpVMUYNCWqdVflLO+njbDkEQ0cA05yENhOi t9cAHcSe7pOHY1lHVnJB2VkjolAsWNhwp3TXmE9Yc/tsj2ySg4mLDq8/qwGHUbdu IvjARXGPaftcvoFGFx8R7K2r4KpFqLtKRmtb1jfYYSfdlB5n7vRIZPqK29aSnpHm Q== Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 443g8sg2jf-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 15 Jan 2025 13:41:25 +0000 (GMT) Received: from pps.filterd (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 50FCpdBp020444; Wed, 15 Jan 2025 13:41:24 GMT Received: from localhost.localdomain (dhcp-10-191-135-68.vpn.oracle.com [10.191.135.68]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTP id 443f3fsvea-3; Wed, 15 Jan 2025 13:41:24 +0000 From: Imran Khan To: anna-maria@linutronix.de, frederic@kernel.org, tglx@linutronix.de Cc: linux-kernel@vger.kernel.org Subject: [PATCH 2/2] timers: introduce timer_try_add_on_cpu. Date: Thu, 16 Jan 2025 00:41:11 +1100 Message-Id: <20250115134111.2703089-3-imran.f.khan@oracle.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250115134111.2703089-1-imran.f.khan@oracle.com> References: <20250115134111.2703089-1-imran.f.khan@oracle.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.68.34 definitions=2025-01-15_05,2025-01-15_02,2024-11-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 adultscore=0 spamscore=0 suspectscore=0 phishscore=0 malwarescore=0 mlxscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2411120000 definitions=main-2501150103 X-Proofpoint-GUID: eOulz92W57q7vUzpQUYCgtsVt97qKMpl X-Proofpoint-ORIG-GUID: eOulz92W57q7vUzpQUYCgtsVt97qKMpl Content-Type: text/plain; charset="utf-8" Timers started using add_timer_on, can get stuck if the specified cpu is offline. If the user of add_timer_on can't guarantee that the specified cpu is online and ends up starting timer on an offline cpu, then that timer may not give expected results. Such users can use new interface timer_try_add_on_cpu, which starts timer on a given cpu, only after ensuring that it remains online. If it sees that the specified cpu is offline or if it can't ensure that the cpu is online, it does not start timer on any cpu and leaves the decision of selecting other cpu to the caller. Thus it ensures that started timer does not get lost, because it was started on an offlined cpu. Suggested-by: Thomas Gleixner Signed-off-by: Imran Khan --- include/linux/timer.h | 1 + kernel/time/timer.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/include/linux/timer.h b/include/linux/timer.h index e67ecd1cbc97d..210c15527b325 100644 --- a/include/linux/timer.h +++ b/include/linux/timer.h @@ -148,6 +148,7 @@ static inline int timer_pending(const struct timer_list= * timer) } =20 extern void add_timer_on(struct timer_list *timer, int cpu); +extern bool timer_try_add_on_cpu(struct timer_list *timer, int cpu); extern int mod_timer(struct timer_list *timer, unsigned long expires); extern int mod_timer_pending(struct timer_list *timer, unsigned long expir= es); extern int timer_reduce(struct timer_list *timer, unsigned long expires); diff --git a/kernel/time/timer.c b/kernel/time/timer.c index ec9eb58e45241..800ed9b4dea7a 100644 --- a/kernel/time/timer.c +++ b/kernel/time/timer.c @@ -1394,6 +1394,39 @@ void add_timer_on(struct timer_list *timer, int cpu) } EXPORT_SYMBOL_GPL(add_timer_on); =20 +/** + * timer_try_add_on_cpu - Try to start a timer on a particular CPU, + * after ensuring that it is and remains online. + * @timer: The timer to be started + * @cpu: The CPU to start it on + * + * Check and ensure that specified cpu is around, before starting a timer + * on it. + * + * Return: + * * %true - If timer was started on an online cpu + * * %false - If the specified cpu was offline or if its online status + * could not be ensured due to unavailability of hotplug lock. + */ +bool timer_try_add_on_cpu(struct timer_list *timer, int cpu) +{ + bool ret =3D true; + + if (unlikely(!cpu_online(cpu))) + ret =3D false; + else if (cpus_read_trylock()) { + if (likely(cpu_online(cpu))) + add_timer_on(timer, cpu); + else + ret =3D false; + cpus_read_unlock(); + } else + ret =3D false; + + return ret; +} +EXPORT_SYMBOL_GPL(timer_try_add_on_cpu); + /** * __timer_delete - Internal function: Deactivate a timer * @timer: The timer to be deactivated --=20 2.34.1