Linus Torvalds prepares to move the Linux kernel to modern C

TheProf

Well-Known Member
Joined
Jun 15, 2021
Messages
318
Reaction score
328
Credits
2,941
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."

 


To be clear I'm not a programmer and I don't know much about programming but just more of a question. Why doesn't Linus Torvalds move the Linux kernel from C to C++ or would that be the next logical step after moving the kernel to a modern version of C?
 
To be clear I'm not a programmer and I don't know much about programming but just more of a question. Why doesn't Linus Torvalds move the Linux kernel from C to C++ or would that be the next logical step after moving the kernel to a modern version of C?

Updating to the C11 standard is definitely a good move. That particular version of the standard makes several improvements over the ancient C89 standard.

I don’t think Linus will ever allow C++ code in the kernel. He’s been very vocal in his opposition to the C++ programming language in the past.

But standard C++ has come a long way, it’s much safer to program in than it used to be. But it’s not perfect. Nothing really is!

Rust is very C like and offers more safety than C. So rust might also be a possible candidate. But there’s no concept of pointers in rust.

And then there is Go.. Another possible language that could supersede C/C++ for systems programming.

Between C, C++, rust and go - there are a number of advantages and disadvantages.

But of those four languages - C is still the lowest level language and will probably offer the best performance.
That said, when compiled - the performance of C++, rust and go isn’t much slower than C. Compilers nowadays can produce highly optimised binaries. There’s almost no need for programmers to mess around writing in-line assembly any more.
 

f33dm3bit wrote:​

To be clear I'm not a programmer and I don't know much about programming but just more of a question. Why doesn't Linus Torvalds move the Linux kernel from C to C++ or would that be the next logical step after moving the kernel to a modern version of C?
You needn't be a programmer to get an idea how dependent the kernel is on C. If you have a look at a recent vanilla kernel's source code and run a find command to discover how many files there are in C, that is, files ending in .c, you would be up over 64,000. That's in a single kernel. And recent kernels have over 25 million lines of code. There would be a lot to alter to put it into any other programming language. I guess that's just tangential to the point though because C just covers most bases for the kernel, together with assembler. C++, though it's built upon C itself, provides object oriented programming extras which aren't necessary. The C language also has a purity about it, which is probably an aesthetic statement, but it is attractive because it's economical in the number and usability of the elements it uses, particularly pointers. Today's C code has a direct relationship with the code of Linus's original kernel release which is often apparent in the source files where one often finds code dated 1991. Some things haven't changed much.
 
@JasKinasis @NorthWest I knew how to find all the c files in the Linux kernel source code, but I don't know enough about programming languages(in this case C and C++) to know why it wouldn't be possible or smart to convert the kernel form C to C++. Thanks for explaining!!
 
It not a simple task to get all those developers on the same page. The move to C11 will be a good one though c89 is pretty long in the tooth. I'm not a programmer but understand how difficult it can be to get everyone working together.

 
Why not pick a more recent standard like C17?
 
Why not pick a more recent standard like C17?
That is a good question. I guess only Linus can answer the reasons why. I did read that c17 was almost identical to C11 and was primarily a bug fix release. But I don't know enough to be able to tell the difference. I may have to do some learning here.
 
Why not pick a more recent standard like C17?

This was asked in a couple of other forums about a year ago.
Someone posted a link from Linus himself saying he would be OK with a newer standard.
(it seems not necessarily C17, but C11 if I remember)
and the push back wasn't so much from the "core" kernel developers, so much as the
external module and drivers developers. I'll see if I can find that link.
 
That makes me think...

It'd be nice if we had a kernel team rep here on Linux.org.

As it stands, you only get a couple of devs of smaller distros and me (the Ubuntu/Lubuntu team member - but I mostly only concentrate on a tiny area of Lubuntu).

Hmm... I wonder if I can find a volunteer liaison?
 

Members online


Top