Linus Torvalds prepares to move the Linux kernel to modern C

We all know Linux is written in C. What you may not know is that it’s written in a long-outdated C dialect: The 1989 version of the C language standard, C89. This is also known as ANSI X3.159-1989, or ANSI C. Linus Torvalds has decided that enough is enough and will move Linux’s official C to 2011’s C11 standard.

Open Source

This isn’t as big a transition as it may seem. C89 still has almost universal support. Because any C compiler is backward compatible with earlier versions, you won’t have any trouble compiling or running a C89 program. So, a C11 compliant compiler won’t have any trouble with any C89 legacy code.

So why bother? The change being made doesn’t include useful features that appear in newer versions.

The situation came to Torvald’s attention when, in order to patch a potential security problem with the kernel’s linked-list primitive speculative-execution functions, another problem was revealed in the patch. While fixing this, Torvalds realized that in C99 the iterator passed to the list-traversal macros must be declared in a scope outside of the loop itself.

Torvalds wrote to the Linux Kernel Mailing List (LKML) that “the whole reason this kind of non-speculative bug can happen is that we historically didn’t have C99-style ‘declare variables in loops.” So list_for_each_entry() – and all the other ones – fundamentally always leaks the last HEAD entry out of the loop, simply because we couldn’t declare the iterator variable in the loop itself.”

The answer? Finally, move from C89 to a newer standard C that makes this kind of problem can’t occur.  So, “the time had come to look at moving to the C99 standard — it is still over 20 years old, but is at least recent enough to allow block-level variable declarations.”

Linux kernel developer, Arnd Bergmann, agreed that this was doable. He added that it should be possible to move up to 2011’s C11 standard. Since C99 was never that popular and C11 introduced standardized multithreading support and made the language a trifle safer this sounds like a good move.

This would also be an easy move to make. The Linux kernel’s minimum C compiler, GCC version 5.1 already supports C11. This works for Torvalds. “I really would love to finally move forward on this, considering that it’s been brewing for many many years.”

Then, after making sure the new standard C should work without problems in the kernel, Torvalds decided to pull the trigger. “Let’s just try this early in the 5.18 merge window.” Since the 5.18 merge window is almost here, we may have C11 code in the kernel as early as March.

Or not. Linux kernel developer and journalist Jonathan Corbet warns, “It is worth keeping in mind, though, that a lot of things can happen between the merge window and the 5.18 release. Moving to a new version of the language standard could reveal any number of surprises in obscure places in the kernel; it would not take many of those to cause the change to be reverted for now. But, if all goes well, the shift to C11 will happen in the next kernel release.”

Source Link

LEAVE A REPLY

Please enter your comment!
Please enter your name here