Linux 下的 time_before/time_after 接口

1、引言

最近在开发过程中遇到一个因 jiffies 使用不当导致系统卡死的问题。问题的根源在于:在延时或超时判断中直接比较 jiffies 的原始值,而未使用内核提供的安全宏(如 time_before)。由于 jiffies 是一个 unsigned long 类型的全局计数器,在 HZ=1000(即每毫秒递增一次)的系统上,大约每 49.7 天就会发生一次回绕(wrap-around)。若在回绕窗口附近进行裸数值比较,很容易因整数溢出导致条件判断失效,进而引发死循环或超时逻辑异常。

2、代码讲解

include\linux\jiffies.h

c 复制代码
/*
 *	These inlines deal with timer wrapping correctly. You are 
 *	strongly encouraged to use them
 *	1. Because people otherwise forget
 *	2. Because if the timer wrap changes in future you won't have to
 *	   alter your driver code.
 *
 * time_after(a,b) returns true if the time a is after time b.
 *
 * Do this with "<0" and ">=0" to only test the sign of the result. A
 * good compiler would generate better code (and a really good compiler
 * wouldn't care). Gcc is currently neither.
 */
#define time_after(a,b)		\
	(typecheck(unsigned long, a) && \
	 typecheck(unsigned long, b) && \
	 ((long)((b) - (a)) < 0))
#define time_before(a,b)	time_after(b,a)

代码片段:

c 复制代码
static int sunxi_mmc_reset_host(struct sunxi_mmc_host *host)
{
	unsigned long expire = jiffies + msecs_to_jiffies(250);
	u32 rval;

	mmc_writel(host, REG_GCTRL, SDXC_HARDWARE_RESET);
	do {
		rval = mmc_readl(host, REG_GCTRL);
	} while (time_before(jiffies, expire) && (rval & SDXC_HARDWARE_RESET));

	if (rval & SDXC_HARDWARE_RESET) {
		dev_err(mmc_dev(host->mmc), "fatal err reset timeout\n");
		return -EIO;
	}

	return 0;
}

为什么 time_before 是安全的呢?

2.1 无符号运算

C 标准中关于无符号数的运算有这样一个定义

A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type.

------------------------------------------《ISO/IEC 9899:2011, Programming languages --- C, Section 6.2.5, paragraph 9.》

翻译过来,其实就是:

  • 在 C 语言中,unsigned 整数的运算不会产生 UB(未定义行为)
  • 当结果超出它能表示的范围时,它会自动对模进行取余(wrap around)

举个例子,对于 unsigned int 类型,如果结果超出其表示范围(如 UINT_MAX + 1),结果会自动对 UINT_MAX + 1 取模。不会发生溢出错误,行为是明确定义的(well-defined)。

类型 最大值
unsigned char 256 254
unsigned int 2³² 2³² - 1

取模运算公式:
x mod y=x−y×⌊x/y⌋x\bmod y = x - y \times \lfloor x / y \rfloorxmody=x−y×⌊x/y⌋

无符号运算的本质是 模 2ⁿ 的环(wrap around)。

32 位系统上:

cpp 复制代码
unsigned long 范围:0 ~ 2^32 - 1
模数:2^32

代入上面的取模公式:

0U−254U=−254 mod 232=−254−232×⌊−254/232⌋0U - 254U = -254\bmod 2^{32} = -254 - 2^{32} \times \lfloor -254 / 2^{32} \rfloor0U−254U=−254mod232=−254−232×⌊−254/232⌋

因此:

cpp 复制代码
0U - 254U = (2^32 - 254)

2.2 小结

time_before/time_after 的数学原理

宏内部做了:

  • (b - a) 使用 unsigned long 算术
    → 自动 wrap-around
  • 结果再强制转换为 long(有符号长整型)

如果 a 和 b 的时间差不超过 2³¹−1(有符号 long 的最大值),那么 (long)(b - a) 的符号足以判断先后顺序:

  • 如果 a 在 b 之后:数学差值 a−b 是正数
    • 如果 a - b < 231 - 1,(a - b) 的 unsigned 值落在 0 ~ 2^31-1,转成 long 是正数
  • 如果 a 在 b 之前:数学差值 a−b 是负数(可能 wrap 了 ):
    • 如果 |a-b| <= 232 - 231 + 1, (a - b) 的 unsigned 值落在 2^31 ~ 2^32-1,转成 long 是负数
    • 如果 232 - 231 + 1 < |a-b| , (a - b) 的 unsigned 值落在 0 ~ 2^31-1,转成 long 是正数

上面的 ∣a−b∣ 指的是数学意义上的时间差。以 sunxi_mmc_reset_host 为例,expire 的生成时刻与之后在 time_before() 中读取到的 jiffies 之间的真实时间差只要小于 231 - 1,就能保证 (long)(a-b) 的结果符号是正确的,从而保证 time_before() 返回的先后判断是可靠的。

两个时间点之间的间隔:

bash 复制代码
2^31 - 1 ticks

也就是约 24.8 天(100Hz 时)/ 49.7 天(HZ=100)/ 497 天(HZ=1000)这个范围远大于内核中绝大多数超时判断。所以可认为 time_before 接口可靠。

相关推荐
BestOrNothing_201538 分钟前
(2)联想拯救者安装 Ubuntu 双系统前的 BIOS 设置全过程
linux·bios·拯救者·ubuntu22.04·联想lenovo
以太浮标39 分钟前
华为eNSP模拟器综合实验之- ACL控制列表核心命令全解析及场景应用
运维·网络·网络协议·华为·信息与通信
23.1 小时前
【Linux】grep命令终极指南
linux
巨斧空间掌门1 小时前
JDK17 下载 windows Linux
linux·运维·服务器
小挪号底迪滴1 小时前
Docker容器化实战:从“在我机器上能跑“到环境一致性
运维·docker·容器
AI+程序员在路上1 小时前
CANopen 协议:介绍、调试命令与应用
linux·c语言·开发语言·网络
江畔何人初1 小时前
kube-apiserver、kube-proxy、Calico 关系
运维·服务器·网络·云原生·kubernetes
learndiary2 小时前
2026.03.12~2026.03.19制作的共7个视频及简介
linux·视频·学习日记小店
JiMoKuangXiangQu2 小时前
Linux:ARM64 中断处理简析
linux·arm64 中断
_下雨天.2 小时前
Nginx性能调优与深度监控
运维·nginx