From nobody Sun Nov 24 01:04:04 2024 Received: from mail-io1-f51.google.com (mail-io1-f51.google.com [209.85.166.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9D5591A9B37 for ; Fri, 8 Nov 2024 17:48:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731088132; cv=none; b=XAfn+w5nofT2pTa6MVt6tWtjhnzC24XdEmueKNY8SBiDyotIFBzWcno9wP76eCAg3ivOrz7X587lJ91zw+6qnpSZ/5kBPg1BsnMhV6tQmh2O9XqhU4npvo+eDODUH4mGtV0W/yBSssQMRXQTALdah1MyE81MxYggbOQ9wnnezqo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731088132; c=relaxed/simple; bh=tmFq8a+ZAxdOgfeOQxZHaNcqzEsFbHUZQrCFrrD8SYA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QscXSFVDN2iadKkSDKHrcXcA01jYwQqeRIbz1CYPxKPlMZgxaK4LJVdyn6pKa3zy/2JFfKQj9X44DTJo0PMmKqAMIhYhbarpJT/zpJlY1Nvsep7fun1gvodg9kr5HwXdkhrbtiVzBLIevSVLKynDIL48tTLGjBk0HiTVpnfqj7Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=joelfernandes.org; spf=pass smtp.mailfrom=joelfernandes.org; dkim=pass (1024-bit key) header.d=joelfernandes.org header.i=@joelfernandes.org header.b=j6f6gzk0; arc=none smtp.client-ip=209.85.166.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=joelfernandes.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=joelfernandes.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=joelfernandes.org header.i=@joelfernandes.org header.b="j6f6gzk0" Received: by mail-io1-f51.google.com with SMTP id ca18e2360f4ac-83aad4a05eeso91431639f.1 for ; Fri, 08 Nov 2024 09:48:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; t=1731088129; x=1731692929; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=51zFNAixGSsd9ALX5NS0WV/Z/XAO7RyEwGQJD2JSoVM=; b=j6f6gzk0VrAxWFGLJ3bz9Vxcsgo/6jOTG0laY4WprNXOBkmbtOEZ7NGJP1z1vT3MRf 66Mhw2zaHsnjRtygtvyEZ4nAdf86nYBxlOj1euWhSaTORwMXcg6a9oGvSFKBbevTSuMg 6qsSeIa73OAB3wwO3qqjq9PBACJSC8U8suA/8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731088129; x=1731692929; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=51zFNAixGSsd9ALX5NS0WV/Z/XAO7RyEwGQJD2JSoVM=; b=KHjVKAXgD7F5sXJ35WQnqKrhAZ+elxcpQNQ2W26SA8c1zCL1WFsXQurpgOospy+YHc 4Y6THK2p33TkxzFUY2dssq2QjvzdBVEMuDzz951yzLuH0glwyQC5J/NlqdU6EcpVEs7m XIubIXZiSfIxg5ajbUua9nvpnh5QgwZSzdilZgFWnJ/3rvo7LXNHpo32aLejBt4wCtqc z2aXhr1vplSn0NO46OKvkWqm4N/k1C94Symi27yvLUfmpwgbEyEpzDseelZ8oXehdDtH PRUxbRqxVvQQyKMBVS9dhb79eQtp+Nf0qrfjbUzmGB5Rp/g/zfQRUdChgaoOxQRk2F82 WR/g== X-Gm-Message-State: AOJu0Yz+kizYt0qLJZLUsNfCy600JcyDDIjlbdsmtLZ9xqtJ2n0Okw+G 9rnRJmvMMfwsqhWahtZpBlzHEAt15no23byKsy4BoVmuZU+H9qBdJdud3r/iXo8zO0c5T5Bo0ou h X-Google-Smtp-Source: AGHT+IH4kmeAX/ogEJUE0aSZ5tgef2/OtRkzMBTW1yI8mtzM0hYiXrymeKzfi/TGoKvFM+2wiYuTKQ== X-Received: by 2002:a05:6e02:1a65:b0:3a6:c1ad:3a02 with SMTP id e9e14a558f8ab-3a6f1a556ddmr42001355ab.24.1731088129371; Fri, 08 Nov 2024 09:48:49 -0800 (PST) Received: from joelboxx5.c.googlers.com.com (222.121.121.34.bc.googleusercontent.com. [34.121.121.222]) by smtp.gmail.com with ESMTPSA id 8926c6da1cb9f-4de5f8e41easm918119173.136.2024.11.08.09.48.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Nov 2024 09:48:48 -0800 (PST) From: "Joel Fernandes (Google)" To: linux-kernel@vger.kernel.org, Anna-Maria Behnsen , Frederic Weisbecker , Ingo Molnar , Thomas Gleixner Cc: "Joel Fernandes (Google)" Subject: [RFC 1/3] tick-sched: Remove last_tick and calculate next tick from now Date: Fri, 8 Nov 2024 17:48:34 +0000 Message-ID: <20241108174839.1016424-2-joel@joelfernandes.org> X-Mailer: git-send-email 2.47.0.277.g8800431eea-goog In-Reply-To: <20241108174839.1016424-1-joel@joelfernandes.org> References: <20241108174839.1016424-1-joel@joelfernandes.org> 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 Content-Type: text/plain; charset="utf-8" During tick restart, we use last_tick and forward it past now. Since we are forwarding past now, we can simply use now as a reference instead of last_tick. This patch removes last_tick and does so. This patch potentially does more mul/imul than the existing code, as sometimes forwarding past now need not be done if last_tick > now. However, the patch is a cleanup which reduces LOC and reduces the size of struct tick_sched. Signed-off-by: Joel Fernandes (Google) --- kernel/time/tick-sched.c | 7 ++----- kernel/time/tick-sched.h | 1 - kernel/time/timer_list.c | 1 - 3 files changed, 2 insertions(+), 7 deletions(-) diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c index 71a792cd8936..52a4eda664cf 100644 --- a/kernel/time/tick-sched.c +++ b/kernel/time/tick-sched.c @@ -837,11 +837,9 @@ EXPORT_SYMBOL_GPL(get_cpu_iowait_time_us); =20 static void tick_nohz_restart(struct tick_sched *ts, ktime_t now) { + /* Set the time to expire on the next tick and not some far away future. = */ hrtimer_cancel(&ts->sched_timer); - hrtimer_set_expires(&ts->sched_timer, ts->last_tick); - - /* Forward the time to expire in the future */ - hrtimer_forward(&ts->sched_timer, now, TICK_NSEC); + hrtimer_set_expires(&ts->sched_timer, DIV_ROUND_UP_ULL(now, TICK_NSEC) * = TICK_NSEC); =20 if (tick_sched_flag_test(ts, TS_FLAG_HIGHRES)) { hrtimer_start_expires(&ts->sched_timer, @@ -1043,7 +1041,6 @@ static void tick_nohz_stop_tick(struct tick_sched *ts= , int cpu) calc_load_nohz_start(); quiet_vmstat(); =20 - ts->last_tick =3D hrtimer_get_expires(&ts->sched_timer); tick_sched_flag_set(ts, TS_FLAG_STOPPED); trace_tick_stop(1, TICK_DEP_MASK_NONE); } diff --git a/kernel/time/tick-sched.h b/kernel/time/tick-sched.h index b4a7822f495d..7210cc473855 100644 --- a/kernel/time/tick-sched.h +++ b/kernel/time/tick-sched.h @@ -71,7 +71,6 @@ struct tick_sched { =20 /* Tick handling */ struct hrtimer sched_timer; - ktime_t last_tick; ktime_t next_tick; unsigned long idle_jiffies; ktime_t idle_waketime; diff --git a/kernel/time/timer_list.c b/kernel/time/timer_list.c index 1c311c46da50..26688a3b8ea8 100644 --- a/kernel/time/timer_list.c +++ b/kernel/time/timer_list.c @@ -154,7 +154,6 @@ static void print_cpu(struct seq_file *m, int cpu, u64 = now) struct tick_sched *ts =3D tick_get_tick_sched(cpu); P_flag(nohz, TS_FLAG_NOHZ); P_flag(highres, TS_FLAG_HIGHRES); - P_ns(last_tick); P_flag(tick_stopped, TS_FLAG_STOPPED); P(idle_jiffies); P(idle_calls); --=20 2.47.0.277.g8800431eea-goog From nobody Sun Nov 24 01:04:04 2024 Received: from mail-io1-f53.google.com (mail-io1-f53.google.com [209.85.166.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 832A71A9B3F for ; Fri, 8 Nov 2024 17:48:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731088133; cv=none; b=skAQVnNnMBd2WjlczEho2d+2MwQRS8DIUHrCuRJnknu8tmF1iQX4g8uUIsSvHxSEnIGT9wpVgvggJipfUnuqQ9I2eIOJcF96TK2xKEXvHHRlJpZ6hQHkmWwOt0ga2Z/J6Cvnk2lcfGz3s3STEvPxABaEZjxIwaT6BtbwxXahJWA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731088133; c=relaxed/simple; bh=mXiHMP//La9C+ginQ4TENC1F1LlUlL0LuRlSUjujvGs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hyFwWc3wrMgMCWcc8HBNJeEXKCGrDq0lmYgzdMbx6M7tVmeQ3QW5MelzGljcixezT5E0K5d5eQY0FPqLZkNDa6xqo8Cg/riUiy06HqDPvjR79IXXIPT173mEhw1BjsaSl3Y+mD2Hm8rMBCvLnWxZsg8qUStDqr9B7El12VVm0L4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=joelfernandes.org; spf=pass smtp.mailfrom=joelfernandes.org; dkim=pass (1024-bit key) header.d=joelfernandes.org header.i=@joelfernandes.org header.b=k+HlvGCX; arc=none smtp.client-ip=209.85.166.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=joelfernandes.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=joelfernandes.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=joelfernandes.org header.i=@joelfernandes.org header.b="k+HlvGCX" Received: by mail-io1-f53.google.com with SMTP id ca18e2360f4ac-83abcfb9f37so99135639f.1 for ; Fri, 08 Nov 2024 09:48:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; t=1731088130; x=1731692930; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=mkhFTRyVD6Ai5buzZVqIBspp22Sj1YPI/QUIN0IDlwE=; b=k+HlvGCXXskSa2NLCu01/af+0iaLwfZbWg0QZ9vNah6FPwTDrf8xpWwz6uSBZc0yQI /Ee3nF63qQOfdWfzWJ02r+snXnigpvDCwrFXhJHAZ2JZkAav3rgY75AP0lcCeNIdfuGf g+TOImhr1PN4SmFug+KvaO3ZhyIqf+UcRaVnQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731088130; x=1731692930; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mkhFTRyVD6Ai5buzZVqIBspp22Sj1YPI/QUIN0IDlwE=; b=LQlJwk3cp5pbHGKUjvibxqrZPJTkinpHzwp6W8uLbJf/JHaGCUMhD0/2tm27Ooxtu7 wVWjwiZTlKEz/j//2or0OLb2L5CY95T3Litn3fI+RTp4ACZdTQARCzPUEE9hRcg4S20n rHBsEuCN+09o1mlTY8M1fuPRdS4epmja5/GdnpWi2UKoKdSMpszy460SlF/3MXUkT8Yl Sjdm0bp0ZNkZML76Pjz9w4VZep6lld+wrPCYHqm83k3eEVYk6tJ/Pu4eM/bdttktGQjB xJhbTshOedBGBR0YS4Xn24ONF8IGRieGtpuJUUpsjFdAe/fVVqRJg8+fxh6TOEBKuwdZ CiHA== X-Gm-Message-State: AOJu0YwhHEMc2q+zFk2xO8UIXiEvrqYZnitw7xl43zfWUCjrocsesjf6 mdt/OiEzz5F4wE9ugGGTqnK2vfcj8Tv8wpToOfDyH1sXh7zsUsVQ1ND8z32LUqCCEl+9uFJqcta 3 X-Google-Smtp-Source: AGHT+IH7qp3KyZU76qy5nhetPGEkO2NbGz06FSbdDuJtL6kVE65IlB0C4+J5Zpeicu7VIBHMW2BwYw== X-Received: by 2002:a05:6e02:1706:b0:3a6:acdf:1a19 with SMTP id e9e14a558f8ab-3a6f1a22195mr46094365ab.18.1731088130471; Fri, 08 Nov 2024 09:48:50 -0800 (PST) Received: from joelboxx5.c.googlers.com.com (222.121.121.34.bc.googleusercontent.com. [34.121.121.222]) by smtp.gmail.com with ESMTPSA id 8926c6da1cb9f-4de5f8e41easm918119173.136.2024.11.08.09.48.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Nov 2024 09:48:50 -0800 (PST) From: "Joel Fernandes (Google)" To: linux-kernel@vger.kernel.org, Anna-Maria Behnsen , Frederic Weisbecker , Ingo Molnar , Thomas Gleixner Cc: "Joel Fernandes (Google)" Subject: [RFC 2/3] tick-sched: Keep tick on if hrtimer is due imminently Date: Fri, 8 Nov 2024 17:48:35 +0000 Message-ID: <20241108174839.1016424-3-joel@joelfernandes.org> X-Mailer: git-send-email 2.47.0.277.g8800431eea-goog In-Reply-To: <20241108174839.1016424-1-joel@joelfernandes.org> References: <20241108174839.1016424-1-joel@joelfernandes.org> 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 Content-Type: text/plain; charset="utf-8" In highres mode, the kernel only considers timer wheel events when considering whether to keep the tick on (via get_next_interrupt()). This seems odd because it consider several other reasons to keep the tick on. Further, turning off the tick does not help because once idle exit happens due to that imminent hrtimer interrupt, the tick hrtimer interrupt is requeued. That means more hrtimer rbtree operations for not much benefit. Ideally we should not have to do anything because the cpuidle governor should not try to the stop the tick because it knows about this situation, but apparently it still does try to stop the tick. To be more efficient, check for any immminent non-sched hrtimer event and keep the tick on if we know such events exist. Signed-off-by: Joel Fernandes (Google) --- kernel/time/tick-sched.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c index 52a4eda664cf..4aa64266f2b0 100644 --- a/kernel/time/tick-sched.c +++ b/kernel/time/tick-sched.c @@ -890,7 +890,7 @@ u64 get_jiffies_update(unsigned long *basej) */ static ktime_t tick_nohz_next_event(struct tick_sched *ts, int cpu) { - u64 basemono, next_tick, delta, expires; + u64 basemono, next_tick, delta, expires, delta_hr, next_hr_wo; unsigned long basejiff; int tick_cpu; =20 @@ -932,7 +932,10 @@ static ktime_t tick_nohz_next_event(struct tick_sched = *ts, int cpu) * force prod the timer. */ delta =3D next_tick - basemono; - if (delta <=3D (u64)TICK_NSEC) { + next_hr_wo =3D hrtimer_next_event_without(&ts->sched_timer); + delta_hr =3D next_hr_wo - basemono; + + if (delta <=3D (u64)TICK_NSEC || delta_hr <=3D (u64)TICK_NSEC) { /* * We've not stopped the tick yet, and there's a timer in the * next period, so no point in stopping it either, bail. --=20 2.47.0.277.g8800431eea-goog From nobody Sun Nov 24 01:04:04 2024 Received: from mail-io1-f46.google.com (mail-io1-f46.google.com [209.85.166.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E38292245F5 for ; Fri, 8 Nov 2024 17:48:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731088134; cv=none; b=BKMjbzXSoPYjCQQ80g1281xq8eL9sJD7DHiSAQXjeaNwk8+Boyxbjyaihj2vY2GAjmV5OoXxmKM+xhFILQintKruqU61zr9M/0w5Vwl4vsPYjaYLmVGaoNLeGlZCdgHuxrVXlR0rZH7FeWTu6oeQXfI2DoQpL44jGDsxfulj4C0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731088134; c=relaxed/simple; bh=nKHMcWayOScOxZEyAqzNq46KOGIDWVjkP+eAfDEo2ns=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=chGnjVX5n0K+MUXPvFAiyA8jAiZeYaypBaTzSjOF+iOIhHtDcvd7zfn5mozOk6JTGfF53E50VX9+3iTvnHQTSHbpABNqAfoxVXJRzdemBAGXhlGQoaZV9wZIulwKLz1VUFPU1ooSGsNW7VUB8C+K1pmLE88uNQAFrGbbkdA+FtM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=joelfernandes.org; spf=pass smtp.mailfrom=joelfernandes.org; dkim=pass (1024-bit key) header.d=joelfernandes.org header.i=@joelfernandes.org header.b=Z6IydHdt; arc=none smtp.client-ip=209.85.166.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=joelfernandes.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=joelfernandes.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=joelfernandes.org header.i=@joelfernandes.org header.b="Z6IydHdt" Received: by mail-io1-f46.google.com with SMTP id ca18e2360f4ac-83aad4a05eeso91433139f.1 for ; Fri, 08 Nov 2024 09:48:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; t=1731088132; x=1731692932; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=zogpuYrLrV6p+Ie1T3jZzQiuF3EC5ETYsImm5LZPhdg=; b=Z6IydHdt82ocqpwpO0GiATMhnyaV0878s0xam7C62DGkau1pOMF4/AjBe0VAc2QtPm Wb5x5+L0GyFSzLAtlyhzg1bfglMn/ZbVLZd4vv3ivuhF0pw8nBOYXg3mUQ0axTWE4akd 8KDm2W6SMihxuTfzRUZ2OXjVrIxxq9EeNShYw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731088132; x=1731692932; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zogpuYrLrV6p+Ie1T3jZzQiuF3EC5ETYsImm5LZPhdg=; b=Wd2s6WE3PD81Ob4fudOUST85ge72FYgLfHgmdPekeeBLxm8Jh7dM3Jg7AYyz7xjKSZ mXYs03kIfID5JA7n3IeGjqzqBl+7XNs1w2EsKuoqM+o6x+utPRQ/HVVv2kSaxBCApxfS GMNtmr69hf0SoQBxtWl1GBux4hY6Tsg6hQaTAWNiL/YAzTlApTwrC4CKEmqqEMwsW2UR euMfZRVdA5a5M6GzhwOPce4j4GCpA6QmgKDz1VYCto88ObaQMsIbDOPgQ+XdwVkhWJcX PdicJqamutjdXMy+7rL+l6TWEhnedr4LvrM3qwRR1s6AI7cRJjMN0kO08enjHqWMraCN S7mg== X-Gm-Message-State: AOJu0YyDDvGBo+oc/XcNOeEFsHr7RhjDmTtOAsn9jz+NuUTuBaPWchFd 1Ifd5xwFAVRCLkvsNkwzc7NsRjvaIfqKi32CzvV3YVOne6XKdjzbt8MirGPAsdWGsIkq30NAqH4 l X-Google-Smtp-Source: AGHT+IEEkwswzUiadUN1E1foVhKVlckkwZrKWDJEpyl0L9fL9YRNg7SHx4yXMCTUbC0/MZzaITx3Pw== X-Received: by 2002:a05:6602:3cb:b0:83a:b2c1:8b40 with SMTP id ca18e2360f4ac-83e033b15ddmr434444139f.13.1731088131772; Fri, 08 Nov 2024 09:48:51 -0800 (PST) Received: from joelboxx5.c.googlers.com.com (222.121.121.34.bc.googleusercontent.com. [34.121.121.222]) by smtp.gmail.com with ESMTPSA id 8926c6da1cb9f-4de5f8e41easm918119173.136.2024.11.08.09.48.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Nov 2024 09:48:51 -0800 (PST) From: "Joel Fernandes (Google)" To: linux-kernel@vger.kernel.org, Anna-Maria Behnsen , Frederic Weisbecker , Ingo Molnar , Thomas Gleixner Cc: "Joel Fernandes (Google)" Subject: [RFC 3/3] tick-sched: Replace jiffie readout with idle_entrytime Date: Fri, 8 Nov 2024 17:48:36 +0000 Message-ID: <20241108174839.1016424-4-joel@joelfernandes.org> X-Mailer: git-send-email 2.47.0.277.g8800431eea-goog In-Reply-To: <20241108174839.1016424-1-joel@joelfernandes.org> References: <20241108174839.1016424-1-joel@joelfernandes.org> 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 Content-Type: text/plain; charset="utf-8" This solves the issue where jiffies can be stale and inaccurate. Putting some prints, I see that basemono can be quite stale: tick_nohz_next_event: basemono=3D18692000000 basemono_from_idle_entrytime= =3D18695000000 Since we have 'now' in ts->idle_entrytime, we can just use that. It is more accurate, cleaner, reduces lines of code and reduces any lock contention with the seq locks. I was also concerned about issue where jiffies is not updated for a long time, and then we receive a non-tick interrupt in the future. Relying on stale jiffies value and using that as base can be inaccurate to determine whether next event occurs within next tick. Fix that. XXX: Need to fix issue in idle accounting which does 'jiffies - idle_entrytime'. If idle_entrytime is more current than jiffies, it could cause negative values. I could replace jiffies with idle_exittime in this computation potentially to fix that. Signed-off-by: Joel Fernandes (Google) --- kernel/time/tick-sched.c | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c index 4aa64266f2b0..22a4f96d9585 100644 --- a/kernel/time/tick-sched.c +++ b/kernel/time/tick-sched.c @@ -860,24 +860,6 @@ static inline bool local_timer_softirq_pending(void) return local_softirq_pending() & BIT(TIMER_SOFTIRQ); } =20 -/* - * Read jiffies and the time when jiffies were updated last - */ -u64 get_jiffies_update(unsigned long *basej) -{ - unsigned long basejiff; - unsigned int seq; - u64 basemono; - - do { - seq =3D read_seqcount_begin(&jiffies_seq); - basemono =3D last_jiffies_update; - basejiff =3D jiffies; - } while (read_seqcount_retry(&jiffies_seq, seq)); - *basej =3D basejiff; - return basemono; -} - /** * tick_nohz_next_event() - return the clock monotonic based next event * @ts: pointer to tick_sched struct @@ -887,14 +869,19 @@ u64 get_jiffies_update(unsigned long *basej) * *%0 - When the next event is a maximum of TICK_NSEC in the future * and the tick is not stopped yet * *%next_event - Next event based on clock monotonic + * + * Note: ts->idle_entrytime is updated with 'now' via tick_nohz_idle_enter= (). */ static ktime_t tick_nohz_next_event(struct tick_sched *ts, int cpu) { - u64 basemono, next_tick, delta, expires, delta_hr, next_hr_wo; + u64 basemono, next_tick, delta, expires, delta_hr, next_hr_wo, boot_ticks; unsigned long basejiff; int tick_cpu; =20 - basemono =3D get_jiffies_update(&basejiff); + boot_ticks =3D DIV_ROUND_DOWN_ULL(ts->idle_entrytime, TICK_NSEC); + basejiff =3D boot_ticks + INITIAL_JIFFIES; + basemono =3D boot_ticks * TICK_NSEC; + ts->last_jiffies =3D basejiff; ts->timer_expires_base =3D basemono; =20 --=20 2.47.0.277.g8800431eea-goog