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 接口可靠。

相关推荐
Coder_Boy_1 分钟前
基于SpringAI的在线考试系统-企业级软件研发工程应用规范案例
java·运维·spring boot·软件工程·devops
生产队队长4 分钟前
Linux:awk进行行列转换操作
android·linux·运维
捷米研发三部4 分钟前
EtherNet/IP转CAN协议转换网关实现罗克韦尔 PLC与压力传感器通讯在轮胎压力监测系统的应用案例
服务器·网络
白玉瑕14 分钟前
服务器的构成
运维·服务器
linweidong15 分钟前
在Ubuntu新版本安装gcc4.8等老版本环境
linux·运维·ubuntu
寄思~22 分钟前
Excel 数据匹配工具 -笔记
笔记·python·学习·excel
jarreyer28 分钟前
【docker的gpu加速相关问题解决记录】
运维·docker·容器
石像鬼₧魂石30 分钟前
80 端口(Web 服务)渗透测试完整总结(含踩坑 + 绕过 + 实战流程)
linux·运维·服务器·前端·网络·阿里云
韭菜钟32 分钟前
制作自定义Docker镜像并部署使用
运维·docker·容器
米高梅狮子37 分钟前
11. Linux 防火墙管理
linux·运维·服务器