From nobody Tue Feb 10 23:53:35 2026 Received: from mail-ot1-f73.google.com (mail-ot1-f73.google.com [209.85.210.73]) (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 4E7862727EB for ; Tue, 10 Feb 2026 17:02:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770742950; cv=none; b=cAhbeDWKuBSxrE3Zu40NcEDqgWYzwiXCBdZeRnDWuYk5uF9J0toKCcHfvsiY2LuMNMAsxTGOE6k9cysqsSbEmWSkC2JzhcwZ3Y5AAGe4RTWSn8Hf4YYwCOjzO+0xyqvp/t6YKGkS3YFdFF9Js6fzLvEIZbc2jr8dK7xwK9vYneg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770742950; c=relaxed/simple; bh=uroyUVrfjt05bpt+E0qE5D1XXCycsXLDCDUIx/p5tlg=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=FsHP6Cwbjn9w5rGiEvMo6n8poe8jjbVzIoM+VZSeGAFgMgbJFGvWjM0ZOe3nt3dXI+Q7njasBUa4Pog3rH0tsJzQiZtmRd3kBmxkk34estSzga7HCjpR/eWTJkF9xiL862xsqxSj2IB9NNkrK59CjYUtggDJ/vjFUTXEud9BIK4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=MbKiZA8/; arc=none smtp.client-ip=209.85.210.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="MbKiZA8/" Received: by mail-ot1-f73.google.com with SMTP id 46e09a7af769-7d49709f87aso3599626a34.1 for ; Tue, 10 Feb 2026 09:02:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1770742948; x=1771347748; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=/RO+s5OI+Gjj8tx3zTi9MpXje/mDxt85JjmHjc/CX/Y=; b=MbKiZA8/TLYA2JnPPl0nMvv8f8VeSuo7/A8oXAayaF8bcW20sgKCRtFjp+QqhKDvOF efOwydYie2PcXqkC/eMjMm78rtRF4i7otql6T6iBN9FJVUX7zvITbaS4xIE0HY8bExz5 UxQBE02FVtWF2MJp4J1OlInkoo8zAhnGUrFscJV1JddZ6YOhCWPBLRlprhQmfCBdlCa0 IYPFL/hHCDeljqn7ZRHDPIHxS3U5+DJ6SwZTz7GhRukSLA5SuCC5Qp+mHfo7jsFtRRS8 pMnFELdR7QQGaMMjCIEgnwFLBhkcbvVhWfIVheujtIewJ/LKgDhryE/xVgYr/mxKrNAD HU5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770742948; x=1771347748; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=/RO+s5OI+Gjj8tx3zTi9MpXje/mDxt85JjmHjc/CX/Y=; b=r+W4nfsVWoIJoKKMjSSq3+vHIlp28DhE8RJQbu6c8qe/JURDb6w6855E+ExoQmNp56 93zhcMRp0u4h8RpsoDy1/CFRx4JnGASyo5MUURWcgxP4nupzyc/24Bt6eCETM17Fc2ov WyDENr7d/iEhbkZ8w9jWNzLmJZ9CLLyKgyDSaEVuxmmaFJd1aZ36bpo64AUvDhcr4Rox 53cTLqiszGYb+Dxrf1zR8Mo+IYCc2mixD+bmmudejLRDhxSr6m+hSms2gS2Sb/218yoD e/5vx2o3fyunMhNqZGkAl7bcIpxDIbsBiWJthh1wKWQHGh6bF5YXRPkQMWgzrdIDpmZT vFhQ== X-Gm-Message-State: AOJu0YzaInBNiSsFkNYX2BaIXDQoioDmXtz+M0LBlli5Ed5FUwnZ13FV u356OYgK78/yFlWnUTgzf5vxORK/Ct5ZCA0fvUllsYxNxLCDIR9BhsmtZ1Tgg4vOBm7cNTfCThN 65YTzUxqXO6oteg== X-Received: from yxvx3.prod.google.com ([2002:a53:bf03:0:b0:649:db2e:3c13]) (user=edumazet job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6830:4488:b0:7cf:cb0b:dd10 with SMTP id 46e09a7af769-7d46469c969mr9541829a34.33.1770742948035; Tue, 10 Feb 2026 09:02:28 -0800 (PST) Date: Tue, 10 Feb 2026 17:02:25 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.53.0.rc2.204.g2597b5adb4-goog Message-ID: <20260210170226.57209-1-edumazet@google.com> Subject: [PATCH] time/jiffies: inline jiffies_to_msecs() and jiffies_to_usecs() From: Eric Dumazet To: Andrew Morton , John Stultz , Thomas Gleixner , Stephen Boyd Cc: linux-kernel , Eric Dumazet , Eric Dumazet Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" For common cases (HZ=3D100, 250 or 1000), these helpers are at most one multiply, there is no point calling a tiny function. Keep them out of line for HZ=3D300 and others. This saves cycles in TCP fast path, among other things. $ scripts/bloat-o-meter -t vmlinux.old vmlinux.new add/remove: 0/8 grow/shrink: 25/89 up/down: 530/-3474 (-2944) ... nla_put_msecs 193 - -193 message_stats_print 2131 920 -1211 Total: Before=3D25365208, After=3D25362264, chg -0.01% Signed-off-by: Eric Dumazet --- include/linux/jiffies.h | 40 ++++++++++++++++++++++++++++++++++++++-- kernel/time/time.c | 19 +++++++------------ 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/include/linux/jiffies.h b/include/linux/jiffies.h index fdef2c155c27202ae8ba910f170a62521294dc36..d1c3d4941854a82c4571032adb4= 1ca57d5faa8bd 100644 --- a/include/linux/jiffies.h +++ b/include/linux/jiffies.h @@ -434,8 +434,44 @@ extern unsigned long preset_lpj; /* * Convert various time units to each other: */ -extern unsigned int jiffies_to_msecs(const unsigned long j); -extern unsigned int jiffies_to_usecs(const unsigned long j); + +#if HZ <=3D MSEC_PER_SEC && !(MSEC_PER_SEC % HZ) +/** + * jiffies_to_msecs - Convert jiffies to milliseconds + * @j: jiffies value + * + * This inline version takes care of HZ in {100,250,1000}. + * + * Return: milliseconds value + */ +static inline unsigned int jiffies_to_msecs(const unsigned long j) +{ + return (MSEC_PER_SEC / HZ) * j; +} +#else +unsigned int jiffies_to_msecs(const unsigned long j); +#endif + +#if !(USEC_PER_SEC % HZ) +/** + * jiffies_to_usecs - Convert jiffies to microseconds + * @j: jiffies value + * + * Return: microseconds value + */ +static inline unsigned int jiffies_to_usecs(const unsigned long j) +{ + /* + * Hz usually doesn't go much further MSEC_PER_SEC. + * jiffies_to_usecs() and usecs_to_jiffies() depend on that. + */ + BUILD_BUG_ON(HZ > USEC_PER_SEC); + + return (USEC_PER_SEC / HZ) * j; +} +#else +unsigned int jiffies_to_usecs(const unsigned long j); +#endif =20 /** * jiffies_to_nsecs - Convert jiffies to nanoseconds diff --git a/kernel/time/time.c b/kernel/time/time.c index 0ba8e3c50d62570d7248dd02a80395cb0c51c42e..36fd2313ae7eef5686722687008= 750981646a9e0 100644 --- a/kernel/time/time.c +++ b/kernel/time/time.c @@ -365,20 +365,16 @@ SYSCALL_DEFINE1(adjtimex_time32, struct old_timex32 _= _user *, utp) } #endif =20 +#if HZ > MSEC_PER_SEC || (MSEC_PER_SEC % HZ) /** * jiffies_to_msecs - Convert jiffies to milliseconds * @j: jiffies value * - * Avoid unnecessary multiplications/divisions in the - * two most common HZ cases. - * * Return: milliseconds value */ unsigned int jiffies_to_msecs(const unsigned long j) { -#if HZ <=3D MSEC_PER_SEC && !(MSEC_PER_SEC % HZ) - return (MSEC_PER_SEC / HZ) * j; -#elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC) +#if HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC) return (j + (HZ / MSEC_PER_SEC) - 1)/(HZ / MSEC_PER_SEC); #else # if BITS_PER_LONG =3D=3D 32 @@ -390,7 +386,9 @@ unsigned int jiffies_to_msecs(const unsigned long j) #endif } EXPORT_SYMBOL(jiffies_to_msecs); +#endif =20 +#if (USEC_PER_SEC % HZ) /** * jiffies_to_usecs - Convert jiffies to microseconds * @j: jiffies value @@ -405,17 +403,14 @@ unsigned int jiffies_to_usecs(const unsigned long j) */ BUILD_BUG_ON(HZ > USEC_PER_SEC); =20 -#if !(USEC_PER_SEC % HZ) - return (USEC_PER_SEC / HZ) * j; -#else -# if BITS_PER_LONG =3D=3D 32 +#if BITS_PER_LONG =3D=3D 32 return (HZ_TO_USEC_MUL32 * j) >> HZ_TO_USEC_SHR32; -# else +#else return (j * HZ_TO_USEC_NUM) / HZ_TO_USEC_DEN; -# endif #endif } EXPORT_SYMBOL(jiffies_to_usecs); +#endif =20 /** * mktime64 - Converts date to seconds. --=20 2.53.0.rc2.204.g2597b5adb4-goog