News for hackers. Stuff that matters.
Re:Moving the Linux Kernel to Modern C by kllrnohj on Thursday February 24, 2022 @04:04PM (#30460249)
GCC already has that functionality, it's called C++. Like that macro is just a crappy version of std::for_each.

It doesn't seem useful to make a "C+" instead of switching to a language that just has the feature set the kernel needs. Like Rust, which is gaining some support within the Linux kernel already.

Re:Moving the Linux Kernel to Modern C by mhh__ on Thursday February 24, 2022 @10:11PM (#30463327)
They've dug themselves into a hole when it comes to C++.

If you started writing a major project in C now you'd rightfully get sacked, but open source projects like this are often dominated by the opinions of those who only work on that project. That doesn't mean they're automatically wrong but just that they're often massively detached from any feedback apart from disaster.

Re:Moving the Linux Kernel to Modern C by dale_glass on Thursday February 24, 2022 @04:15PM (#30460391)
C++ was discussed as having too many undesirable characteristics.

But that's exactly why I think a customized language for the kernel is an interesting idea. You could get pretty much exactly what you want for the kernel. Add features you need, remove any undesirable behavior or features.

For most programs that would be too much complexity, but the kernel has very particular needs. And I think a similar in spirit approach has worked very well with Qt.

Re:Moving the Linux Kernel to Modern C by jcelerier on Thursday February 24, 2022 @05:24PM (#30461153)
> C++ was discussed as having too many undesirable characteristics.

C++ allowed the Serenity OS people to produce an entire OS with a GUI stack able to play Diablo and their own web browser in something like two years. It's depressing to think where we could be today in terms of OS if the Linux and GNU people weren't as insistent on their hate of C++

Re:Moving the Linux Kernel to Modern C by von_lohengramm on Thursday February 24, 2022 @07:45PM (#30462393)
Note that Serenity OS also has its own stdlib "AK" that excludes a lot of criticized C++ "features". If the Linux devs were to carefully splice out the Good from the Bad and Ugly of C++, then maybe it'd suit their tastes just fine. I think the bigger factor here is that Linux became decidedly anti-C++ back when C++ was actually pretty crumby.
Re:Moving the Linux Kernel to Modern C by jcelerier on Friday February 25, 2022 @02:42AM (#30464624)
The Linux kernel has its own libc-ish too.
Re:Moving the Linux Kernel to Modern C by tayo42 on Thursday February 24, 2022 @05:48PM (#30461431)
Why is c++ necessary? Shouldn't any programing language be able to do the same things. Maybe with different amounts of code
Re:Moving the Linux Kernel to Modern C by Koshkin on Thursday February 24, 2022 @06:19PM (#30461702)
Sure, as my prof once noted, "Why, with the assembler you just need to type more!"
Re:Moving the Linux Kernel to Modern C by microtherion on Thursday February 24, 2022 @08:42PM (#30462761)
You must have had one of those progressive professors who did not consider assemblers "a waste of a valuable scientific computing instrument [...] to do clerical work": http://worrydream.com/dbx/
Re:Moving the Linux Kernel to Modern C by reasonabl_human on Friday February 25, 2022 @10:24AM (#30468211)
I love historical anecdotes like this. Goes to show that we should always be questioning our status quo and thinking bigger.
Re:Moving the Linux Kernel to Modern C by josefx on Thursday February 24, 2022 @06:30PM (#30461794)
Please write a JavaScript program that will not deploy to your production system if you accidentally pass a string to any function that expects an integer.
Re:Moving the Linux Kernel to Modern C by cprecioso on Thursday February 24, 2022 @07:23PM (#30462240)
So… typescript?
Re:Moving the Linux Kernel to Modern C by mhh__ on Friday February 25, 2022 @02:34AM (#30464578)
Because it's better.

Simple question, simple answer. Using C is purely an act of risk-aversion, contrarianism, and fashion.

Re:Moving the Linux Kernel to Modern C by edgyquant on Friday February 25, 2022 @12:44AM (#30464057)
You could build Serenity in C in a similar about of time
Re:Moving the Linux Kernel to Modern C by akling on Friday February 25, 2022 @06:21AM (#30466080)
Perhaps someone could, but I certainly couldn't. :)
Re:Moving the Linux Kernel to Modern C by jcelerier on Friday February 25, 2022 @02:40AM (#30464618)
What are the arguments towards this ?
Re:Moving the Linux Kernel to Modern C by Koshkin on Thursday February 24, 2022 @04:33PM (#30460604)
The desirable characteristics of C++ overweigh, by far, its "undesirable" ones (whatever those may be, and which by the way you can for the most part safely ignore, if you wish).
Re:Moving the Linux Kernel to Modern C by chaxor on Thursday February 24, 2022 @05:27PM (#30461205)
I don't know anything about C++ really, but I have been interested in learning it several times. The biggest deterrent for me has been the huge number of languages that you have to know to learn c++ it seems. Every new version comes with so many different features and seemingly (by looking for example repos on git) different paradigms that it essentially feels like many different languages.

This is probably one of the biggest reasons I may turn to rust over C++ - simply less features creep due to less time being around.

Can you comment on how wrong I am about my feelings this way?

Re:Moving the Linux Kernel to Modern C by Koshkin on Thursday February 24, 2022 @05:59PM (#30461519)
Yes, I understand, I would probably feel the same if I wanted to learn C++ all at once and for its own sake. Luckily, I have never had to do such a thing. Indeed, I may never learn C++ in its entirety, and I may even miss some "important" pieces, but I am OK with that. I am very comfortable using C++, and I still continue picking up pieces of wisdom here and there - as I go. My advice, learn by example, start with a small project and go from there.
Re:Moving the Linux Kernel to Modern C by kllrnohj on Thursday February 24, 2022 @07:27PM (#30462269)
Learning/teaching C++ is definitely a sore spot about it. Many still teach C++98 which is... Bad. It'd be like still teaching Java 1.3. yeah the syntax is the same basic shape, but pretty much everything else is different.

C++11 and newer are all largely one "category" in terms of recommendations. The CppCoreGuidelines is a good place to cover all that, but it's not a from-scratch introduction by any means

Re:Moving the Linux Kernel to Modern C by midjji on Friday February 25, 2022 @03:27PM (#30471964)
Not wrong, but its a feature, rather than a problem.

C++(17+) template based metaprogramming far more powerful and generic than what you can do in eg rust. Converting eg a rust or go or python, or julia, etc library into c++ is pretty straightforward, just use appropriate overloads, and a few template tricks, and you can mostly copy the code directly. But copying between these, or from c++ is much harder.

The solution is to learn one approach suitable for the problem you have right now, then widen over time. The many languages of c++ as you describe is less of a problem, the problem is that some approaches are deeply and fundamentally flawed, but still in common use.

Re:Moving the Linux Kernel to Modern C by kllrnohj on Thursday February 24, 2022 @07:30PM (#30462296)
Let's say you wanted to do that but also stay in the C family. It's going to be a lot easier to take C++ and just ban all the stuff you don't like (such as via lint rules) than to make a new language. In the former case you continue to benefit from all the ecosystem compiler & tooling improvements (ide support, static analysis, etc...). In the latter case you're on your own. Which is no small burden here.

I'm not necessarily advocating that Linux should switch to C++, just that there's probably not a good reason to invest in a new C/C++ hybrid language at this point in time. Not when C++11 is honestly pretty good, but also Rust, Zig, or even D's betterC all already exist.

Re:Moving the Linux Kernel to Modern C by tialaramex on Thursday February 24, 2022 @09:22PM (#30463023)
C++ has implicit allocation and Linus doesn't like that, for good reason. While it's amusing to imagine trying to "ban all the stuff you don't like" when that means core language features, it is not a realistic plan.

In Rust, allocation lives in a library, alloc, and so the Rust for Linux project did all the work to offer alloc (it's full of useful stuff and it isn't like the Linux kernel can't allocate memory) but without implicit allocation.

If you use Rust to write say a Linux command line program, you can write

  greeting += " and welcome traveller";
... and of course implicitly this is an allocation, 'cos it's not like this greeting variable just magically already has enough space to append a string. But in Rust for Linux, you can't do that, the implicitly allocating += operator is not provided on this type in their alloc library. If you want to say "Allocate more space for the greeting" you can do that of course, just as you can today in C but you must do so explicitly and so when you try to add 16GB of extra string space because you're an idiot, the API you had to explicitly call gives you an error and that's your problem.

However the most critical reason Linux doesn't have C++ is that C++ proponents didn't do the work. Now, that will probably be because "reform the entire language to suit Linus" wasn't a viable plan, but the fact is that Linus can't accept patches that nobody writes, so even if you're sure C++ would be viable without drastic changes, you didn't write the patchset that does it. Likewise if people don't send Linus patches to do C11 it probably won't happen.

Re:Moving the Linux Kernel to Modern C by kllrnohj on Thursday February 24, 2022 @09:51PM (#30463215)
> C++ has implicit allocation

Ah, but see, it doesn't. Linus was wrong about many things in his rant. This being one of them.

Now the standard library does indeed have things that do implicit allocations, such as std::string. But these, like with Rust, are distinct from the language & replaceable. Would it be effort to make a kernel-safe std:: replacement? Yes. Would it be a lot of work? Not really. And it's the kind of thing Linux has been doing for decades with C anyway. It's not like they use a standard libc implementation (much less a rich libc implementation like glibc), for example.

And it's something game devs have been doing with C++ for decades without any issues, too.

So for this one you don't even have to ban anything. Just don't pass a standard library implementation to the compiler. It doesn't come with one, after all. You have to add it. So you could just... Not do that.

As for nobody did the work... That's true. But Linus also pretty much nuked the entire concept of using C++, regardless of the what or how. Time seems to have changed his mindset on some of those things, hence his reaction to Rust, which largely makes all this moot. But we shouldn't confuse short term politics with technical issues, either.

Re:Moving the Linux Kernel to Modern C by Galanwe on Friday February 25, 2022 @02:01AM (#30464417)
> Linus was wrong about many things in his rant. This being one of them.

Its not what is being discussed here. C++ has an implementation defined memory model. That just cannot work with Linux out of the box. You need an OS and compiler designed from the ground up to handle this.

Re:Moving the Linux Kernel to Modern C by kccqzy on Friday February 25, 2022 @03:43AM (#30465019)
C++ has a well defined memory model: https://en.cppreference.com/w/cpp/language/memory_model And it's not that different from the C memory model. How did Linux work with C?
Re:Moving the Linux Kernel to Modern C by Galanwe on Friday February 25, 2022 @03:57AM (#30465114)
> C++ has a well defined memory model

Only since C++11, and it's not a full blown memory model.

Re:Moving the Linux Kernel to Modern C by gpderetta on Friday February 25, 2022 @06:00AM (#30465921)
C++ has with a close, approximation, the same memory model as C, both concurrent and otherwise. In fact C11 just adopted the C++11 concurrent memory model wholesale.
Re:Moving the Linux Kernel to Modern C by jcelerier on Friday February 25, 2022 @05:39AM (#30465784)
it's defined enough for C to have adopted it since C11 (https://people.mpi-sws.org/~viktor/papers/cpp2015-invited.pd...)
Re:Moving the Linux Kernel to Modern C by pjmlp on Friday February 25, 2022 @02:03AM (#30464427)
Arduino, ARM mbed, Symbian, macOS, Windows, BeOS, IBM i and z/OS accepted the work of C++ proponents.

There is nothing when can do to change political views.

And lets not pretend that Rust in the kernel won't suffer from creative uses of macros, and there is still a big laundry list of issue to fix before it actually makes it.

Or that for the time being, Rust compilers need to link to C++ code to actually work, at least until Cranelift is a match in code quality against LLVM or GCC.

So in the end Linus gets C++ into the kernel, even if indirectly.

Re:Moving the Linux Kernel to Modern C by oconnor663 on Friday February 25, 2022 @02:59AM (#30464732)
> Rust compilers need to link to C++ code to actually work

GCC itself has been written in C++ since 2010.

Re:Moving the Linux Kernel to Modern C by pjmlp on Friday February 25, 2022 @05:05AM (#30465572)
Indeed, which kind of diminishes Linus's assertions against C++ even more.
Re:Moving the Linux Kernel to Modern C by midjji on Friday February 25, 2022 @03:32PM (#30472007)
Its notable that the main improvement over gnu98 C to C11 essentially templates, if a worse version of it with better compile time performance.

If they switched to c++17, and banned everything from throw and RTTI to the standard library, they would still get for each, auto, and templates and inheritance which does not have to be emulated in C.

Re:Moving the Linux Kernel to Modern C by xroche on Friday February 25, 2022 @06:53AM (#30466299)
> C++ was discussed as having too many undesirable characteristics.

I tend to agree, as a C++ developer. There are many core issues in the language that haven't been resolved and that are unacceptable for kernel code.

My personal pet peeve: C++ is unable to reallocate a new[] region. This makes basically all structures (vector, hashmap, trees...) unusable for large data handling.

Re:Moving the Linux Kernel to Modern C by kllrnohj on Friday February 25, 2022 @07:07AM (#30466393)
Of those structures only vector benefits from realloc anyway. And if you really want it, it's not terribly hard to write. You can static assert that the template type is trivially movable which would make it safe to realloc.

But this is assuming you have a malloc implementation that does something other than implement realloc as just malloc+memcpy+free. Which not many do, not unless the allocation is so large as to be in its own dedicated mmap or similar.

That aside, sure would be great if you elaborated on these unsuitable, unresolved for kernel language issues? Exceptions and rtti are the only two I'm aware of and both have had off switches for decades.

Re:Moving the Linux Kernel to Modern C by xroche on Friday February 25, 2022 @08:04AM (#30466806)
> And if you really want it, it's not terribly hard to write

Sure, but basically it means rewriting all structures that rely on a bucket of stuff.

By the way maps often use a large bucket, and rehash in-place can be preferable.

> Which not many do, not unless the allocation is so large as to be in its own dedicated mmap

Do you know a modern operating system that does not have a mremap equivalent ?

On Linux you pretty much use it as soon as you reach large blocks.

Re:Moving the Linux Kernel to Modern C by kllrnohj on Friday February 25, 2022 @11:15AM (#30468835)
> By the way maps often use a large bucket, and rehash in-place can be preferable.

std::unordered_map (what I'm guessing you meant by a hashmap) uses a linked list for the nodes. There's no movement in the first place to worry about being realloc'd.

> Do you know a modern operating system that does not have a mremap equivalent ?

You have to be very large before most mallocs will put you on a dedicated mmap that can even be mremap'd at all.

If you're working with stonking huge data inline in a std:: vector... Yeah just make a container for that usage, not really an issue. There's tons of examples out there, typically to add SSO but doing realloc would be the same basic thing.

Re:Moving the Linux Kernel to Modern C by pjmlp on Friday February 25, 2022 @07:39AM (#30466642)
Google (C++17 subset in Zircon), Apple (IO Kit drivers with Embedded C++) and Microsoft (since Vista), ARM (mbed) IBM (C++ as PL/S replacement) think otherwise.
Re:Moving the Linux Kernel to Modern C by stjohnswarts on Thursday February 24, 2022 @05:19PM (#30461080)
*for drivers primarily
Re:Moving the Linux Kernel to Modern C by Gigachad on Thursday February 24, 2022 @05:55PM (#30461484)
That's basically the trial period. Nothing depends on drivers so they are pretty safe to try new things with and the hardware they are used for is limited so you can know you the hardware the driver is used for is supported by the Rust compiler. If we go a few years and its all good and the rust compiler supports everything linux targets, I can imagine more core parts becoming rust.
Re:Moving the Linux Kernel to Modern C by brenns10 on Thursday February 24, 2022 @06:55PM (#30462031)
> Like that macro is just a crappy version of std::for_each.

It really, truly is not comparable. list_for_each_entry does a well known operation that's just moving some pointers around. Safe to do while holding a spinlock, or in interrupt context, etc.

std::for_each is a generic iterator implementation. What code runs in order to iterate? Will it transparently take locks, risking a deadlock? Will it need to schedule()? You don't need to ask those questions about the standard linked list macro, and that has a lot of value when every instruction the compiler generates might matter.

Re:Moving the Linux Kernel to Modern C by kllrnohj on Thursday February 24, 2022 @07:22PM (#30462234)
list_for_each_entry just does blind string substitution. std::for_each is the basic same thing except with type safety & variable scope safety instead. It's still just substitutions.

All your other stuff about for_each is just... Wrong? It's all well defined what it does. The substitutions that for_each makes and exactly what it calls are all defined.

You could give it a container that does something stupid, but you could also give list_for_each_entry the wrong field or accidentally use it when it's no longer valid (literally the bug being fixed)

Re:Moving the Linux Kernel to Modern C by brenns10 on Thursday February 24, 2022 @07:53PM (#30462461)
Point taken on how for_each works for linked lists. I'm (obviously) not a C++ expert and it shows in that context.

I suppose the thrust of my opinion is that a lot of code depends on the fact that it iterates over a linked list, which is safe in many contexts. The idea that callers could substitute container types which break its assumptions seems unwelcome to me. Nobody provides the wrong fields to list_for_each_entry because they get very nasty compiler errors if they try. But anybody can plug a new container type into a for each statement and get new behavior that silently breaks the assumptions of the original code.

I suppose that could be protected by a better type system though. There's a lot to improve on C even for kernel dev, and "C is greatest" isn't a hill I'm willing to die on :)

Re:Moving the Linux Kernel to Modern C by kllrnohj on Thursday February 24, 2022 @09:44PM (#30463169)
> But anybody can plug a new container type into a for each statement and get new behavior that silently breaks the assumptions of the original code.

How would it be "silent"? You're changing the container at the point where you're making any assumptions about it. This would just fall under normal code review purview, just like code reviews are how Linux enforces that anyone even used this linked list macro in the first place. C certainly doesn't care if you use list_for_each_entry for your container iteration, after all.

But it'd be a lot easier to replace these linked lists with a vector that has the same observable contract if C++ was used, just would be way faster to iterate on.

Re:Moving the Linux Kernel to Modern C by midjji on Friday February 25, 2022 @03:51PM (#30472211)
All for(auto x:xs){L} does is:

{ iter= xs.begin(); while(iter++ != xs.end()){L} } Not exactly hard to see what happens. In particular as iter is typically just a raw pointer.

Regardless of if you use one or the other you must answer the question, Should I take locks, etc.

With the macro you also have the concern that if any of the headers you include happens to change order of inclusions, the macro called may change without being noticeable. That is a harder problem.