32 bit system timeout integer overflow9/11/2023 ![]() Unsigned long timeout = jiffies + HZ/2 /* timeout in 0.5s */ When the tick count is equal to this maximum and it is incremented, it wraps around to zero. Thus, a possible 4294967295 timer ticks can occur before the tick count overflows. For a 32-bit unsigned integer, the maximum value is 2 32 - 1. The jiffies variable, like any C integer, experiences overflow when its value is increased beyond its maximum storage limit. ![]() Code can either read jiffies or call get_jiffies_64() because both actions have the same effect. On 64-bit architectures, jiffies_64 and jiffies refer to the same thing. The special function locks the jiffies count via the xtime_lock lock before reading.įigure 10.1. A special function is needed because 32-bit architectures cannot atomically access both 32-bit words in a 64-bit value and because jiffies_64 is not marked volatile. Such a need is rare consequently, most code simply continues to read the lower 32 bits directly via the jiffies variable. The function get_jiffies_64() can be used to read the full 64-bit value. Code that accesses jiffies simply reads the lower 32 bits of jiffies_64. Figure 10.1 shows the layout of jiffies and jiffies_64. The time management code uses the entire 64 bits, however, and thus prevents overflow of the full 64-bit value. Because most code uses jiffies simply to measure elapses in time, most code cares about only the lower 32 bits. Code can continue to access the jiffies variable exactly as before. Thus, jiffies is the lower 32 bits of the full 64-bit jiffies_64 variable. The ld(1) script used to link the main kernel image ( arch/i386/kernel/ on x86) then overlays the jiffies variable over the start of the jiffies_64 variable: Some smart thinking and a little linker magic saved that day.Īs you previously saw, jiffies is defined as an unsigned long: With HZ increased to 1000, however, that overflow now occurs in just 49.7 days! If jiffies were stored in a 64-bit variable on all architectures, then for any reasonable HZ value the jiffies variable would never overflow in anyone's lifetime.įor performance and historical reasonsmainly compatibility with existing kernel codethe kernel developers wanted to keep jiffies an unsigned long. With a tick rate of 100, a 32-bit jiffies variable would overflow in about 497 days. ![]() The jiffies variable has always been an unsigned long, and therefore 32 bits in size on 32-bit architectures and 64-bits on 64-bit architectures. Note that the jiffies variable is prototyped as unsigned long and that storing it in anything else is incorrect. The latter is typically reserved for communicating with user-space, as the kernel itself rarely cares about any sort of absolute time. Unsigned long later = jiffies + 5*HZ /* five seconds from now */ Unsigned long next_tick = jiffies +_1 /* one tick from now */ Unsigned long time_stamp = jiffies /* now */ For example, code often needs to set a value for some time in the future, for example: Likewise, this code converts from jiffies to seconds: The following code converts from seconds to a unit of jiffies: For now, let's look at some sample kernel code. In the next section, we will look at its actual definition, which is a bit peculiar. As we have seen in this chapter, however, a jiffy in Linux can have various values. In operating systems, especially Unix, a jiffy is the time between two successive clock ticks. In the United States, this is 1/60 of a second. In electrical engineering, a jiffy is the time to complete one AC (alternating current) cycle. In computer engineering, a jiffy is often the time between two successive clock cycles. In physics, a jiffy is sometimes used to refer to the time it takes for light to travel some specific distance (usually a foot or a centimeter or across a nucleon). In scientific applications, jiffy represents various intervals of time, most commonly 10ms. In lay terms, jiffy refers to an indeterminate but very brief period of time. Phrases such as in a jiffy are thought to originate from eighteenth-century England. The system uptime is therefore jiffies/HZ seconds. Thus, because there are HZ timer interrupts in a second, there are HZ jiffies in a second. On boot, the kernel initializes the variable to zero, and it is incremented by one during each timer interrupt. The global variable jiffies holds the number of ticks that have occurred since the system booted.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |