|
|
Subscribe / Log in / New account

Scope-based resource management for the kernel

Scope-based resource management for the kernel

Posted Jun 16, 2023 14:51 UTC (Fri) by make (subscriber, #62794)
In reply to: Scope-based resource management for the kernel by tialaramex
Parent article: Scope-based resource management for the kernel

> If your proposal is to instead use a different programming language, Linux already has Rust for Linux.

You're missing the point. Rust's scope within the Linux kernel is extremely limited (no Rust core code, only a narrow scope of modules/drivers). Rust will not help with the problem that those macros are attempting to solve, because that would require converting all caller locations of those macros to Rust before doing anything else, and that would require converting all their dependencies to Rust, and so on... won't happen.

On the other hand, allowing a C++ (subset) would make all desirable C++ features available everywhere, immediately.


to post comments

Scope-based resource management for the kernel

Posted Jun 16, 2023 15:05 UTC (Fri) by mbunkus (subscriber, #87248) [Link] (10 responses)

> On the other hand, allowing a C++ (subset) would make all desirable C++ features available everywhere, immediately.

Aren't several of you arguing in this very thread against one of the most iconic & useful C++ feature, RAII? Mostly because it imposes a performance impact (which it does, I don't dispute that; see ibukanov's comment) & isn't as efficient if it requires storing a second copy of a pointer that's already available elsewhere (see vegard's comment)?

Be prepared for resistance, not just from the folks who don't want C++ or don't want language no. 3 in the kernel, but also from those preferring raw speed & space efficiency.

Scope-based resource management for the kernel

Posted Jun 16, 2023 15:28 UTC (Fri) by make (subscriber, #62794) [Link] (2 responses)

> Aren't several of you arguing in this very thread against one of the most iconic & useful C++ feature, RAII?

Several of "us"? I see nobody arguing against RAII. This whole thread is about adding RAII to the C part of the kernel (implemented with macros and non-standard extensions). RAII already exists in the Rust part of the kernel, it's a standard Rust feature.

> Mostly because it imposes a performance impact

That is not a natural property of RAII. It is possible to use C++ in a way that is less efficient than C, and it is just as possible to use C++ in a way that is more efficient than C, but such comparisons are difficult. In any case, my point is: RAII has no performance impact per se.

(But the more important point is: it is easier to write correct C++ code than to write correct C code.)

> if it requires storing a second copy of a pointer that's already available elsewhere

Sometimes, extra context needs to be stored in the RAII class, that is true. But often, that overhead will be optimized away by the compiler.

I just wrote an example on Godbolt: https://godbolt.org/z/GjsWx1vda - as you see, the machine code is identical, and the whole RAII class gets optimized away; the allocated pointer lives in the same register in both versions, and the size isn't stored anywhere, it remains in the register argument (in both versions).

Writing code where the C++ compiler can optimize away all the RAII "overhead" can be tricky, but even if your compiler is too dumb or your code isn't optimized well enough - optimize later, but have good robust leak-free code from the beginning. Those who really care will get identical machine code, with less effort than writing plain C.

> but also from those preferring raw speed & space efficiency.

That's a myth.

Scope-based resource management for the kernel

Posted Jun 16, 2023 16:45 UTC (Fri) by mbunkus (subscriber, #87248) [Link] (1 responses)

> Several of "us"? I see nobody arguing against RAII.

Sorry about the "us"; I was just conflating the different people I was replying to.

OK, maybe "against RAII as commonly used in C++" would be more precise? I was only confused by ibukanov's initial statement that C++'s destructors not allowing arguments. The examples that were given sounded very un-C++-like to me as both cases could easily be solved in the usual C++-RAII style by storing copies of the relevant pointers inside the class. But the arguments against that was "no, bad for performance" (paraphrasing).

I'm really, really not arguing against RAII or against C++ in general, nor against RAII in C++ in particular; quite the opposite. RAII in C++ is one feature I really like about C++. I'm just… confused by what different people wrote.

And about performance: I was referring to std::unique_ptr notbeing a zero-cost abstraction; see Chandler Carruth's presentation about it: https://www.youtube.com/watch?v=rHIkrotSwcc (the most interesting part is probably 17:30–24:00) That falls into the category you've mentioned yourself:

> Writing code where the C++ compiler can optimize away all the RAII "overhead" can be tricky

Scope-based resource management for the kernel

Posted Jun 16, 2023 17:25 UTC (Fri) by Wol (subscriber, #4433) [Link]

> The examples that were given sounded very un-C++-like to me as both cases could easily be solved in the usual C++-RAII style by storing copies of the relevant pointers inside the class. But the arguments against that was "no, bad for performance" (paraphrasing).

For the kernel, yes. Don't forget, if your structures grow in size, you're talking cache misses, cache eviction, cache line bounce, etc. All of which can be VERY expensive.

Cheers,
Wol

Scope-based resource management for the kernel

Posted Jun 17, 2023 7:00 UTC (Sat) by ibukanov (subscriber, #3942) [Link] (6 responses)

There are two separated questions. RAII as a paradigm and RAII as it is implemented in C++.

From a system/emebeded programming point of view I would rather prefer if C++ would require explicit destructor calls for RAII rather than calling destructors automatically. Yes, it will make code more verbose, but at least it will make very obvious what the code is doing. And it will prevent a few bugs due to unexpected destruction of temporaries. For C such enforcement of destructor-type calls will be even more natural that the current attribute hack.

Scope-based resource management for the kernel

Posted Jun 17, 2023 7:31 UTC (Sat) by mbunkus (subscriber, #87248) [Link] (2 responses)

The whole idea of RAII is automatic freeing of the acquired resources in order to prevent memory leaks. Additionally in C++ you get the nice property that you have deterministic behavior when the resource is freed: when the life of the object holding the
resource ends.

Manual calls to destructors are not RAII. You don't gain any of RAII's advantages by requiring manual calls to free the resource.

Scope-based resource management for the kernel

Posted Jun 18, 2023 14:46 UTC (Sun) by ibukanov (subscriber, #3942) [Link] (1 responses)

The idea is that the compiler requires explicit destructor calls exactly at the places where presently it inserts it automatically.

Scope-based resource management for the kernel

Posted Jun 19, 2023 0:32 UTC (Mon) by tialaramex (subscriber, #21167) [Link]

Linear aka Use-exactly-once types. C++ isn't very well suited for this kind of types. Neither is Rust. There are languages which thrive on linear types and a few newer ones are purpose built for these types.

Scope-based resource management for the kernel

Posted Jun 18, 2023 21:13 UTC (Sun) by nksingh (subscriber, #94354) [Link] (1 responses)

I would instead prefer a paradigm like WordPerfect's 'reveal codes', where the compiler and editor can display the code that the compiler is injecting as an option rather than needing to have the code visible at all times.

The advantage of producing the code as needed for review/debugging rather than requiring it to be explicit: when editing, your code remains flexible and modifiable with the compiler doing the bookkeeping at the end, rather than you having to compensate for changes manually before you can even test your code out. I think for domains like embedded, high performance, and kernel code, this transform between 'debuggable' and 'editable' source code would help satisfy both people who want productivity and those who want to know exactly what will happen in the CPU.

Scope-based resource management for the kernel

Posted Jun 26, 2023 6:13 UTC (Mon) by ssmith32 (subscriber, #72404) [Link]

Then it should all be implemented in the editor.

Scope-based resource management for the kernel

Posted Jul 2, 2023 17:05 UTC (Sun) by bluss (guest, #47454) [Link]

Non-trival C++ functions have two exit paths: the return statement (if a single one) and when unwinding an exception. To be fully explicit, one needs to write out destructor calls that happen in both those cases(!).

Scope-based resource management for the kernel

Posted Jun 17, 2023 0:09 UTC (Sat) by tialaramex (subscriber, #21167) [Link] (13 responses)

> On the other hand, allowing a C++ (subset) would make all desirable C++ features available everywhere, immediately.

In the C++ fantasy (which Bjarne has been selling for decades) you take any C software, you just rename a few files and bingo, a more capable, more expressive much better language. What are people waiting for?

It's a fantasy, which Linus won't be indulging in his tree. The Linux source code is readily available though, so you can try this out for yourself and make sure not to go talking about this fantasy again until you've actually found out what the reality is like and have something to show for your effort.

Scope-based resource management for the kernel

Posted Jun 17, 2023 8:15 UTC (Sat) by make (subscriber, #62794) [Link] (6 responses)

> The Linux source code is readily available though, so you can try this out for yourself and make sure not to go talking about this fantasy again until you've actually found out what the reality is like and have something to show for your effort.

Sure there's a lot of hate for C++, the language has its many warts and gives you lots of reasons to hate it, and of course people can have different opinions, but your comment bashing it as "fantasy" and asking me to "have something to show" doesn't quite shine a good light on your competence in programming languages.

Long ago, I was a C fanboy and disliked C++, but then came C++11 which solved many of the things I disliked. In the following years, I converted all of my dayjob C projects to C++ and did the same to several open source projects I manage. (Maybe even one you're using.)

My point is: your rather immature request "not to go talking about this fantasy again" is aimed at the wrong guy. I went through this "fantasy", and I've been living in this C++ utopia for many years. It's not fantasy, it works. Of course, it's not all unicorns and rainbows, but strictly better than C in every aspect.

Scope-based resource management for the kernel

Posted Jun 17, 2023 19:03 UTC (Sat) by Wol (subscriber, #4433) [Link] (2 responses)

> My point is: your rather immature request "not to go talking about this fantasy again" is aimed at the wrong guy. I went through this "fantasy", and I've been living in this C++ utopia for many years. It's not fantasy, it works. Of course, it's not all unicorns and rainbows, but strictly better than C in every aspect.

There speaks the user-space programmer who thinks the solution for a slow program is to throw hardware at it.

Many moons ago, my company was given a six week deadline, and I accepted the challenge to try and meet it. I had five weeks to automate a very manual setup. Four weeks into the project, I estimated my program would take three or four weeks to just run... Oh - and I met the deadline.

That was back in the day when the most powerful weather-forcasting computers had difficulty forcasting the weather before it happened.

Less so now, but as a database programmer I've heard plenty of stories of changes to the Oracle optimiser screwing over carefully crafted and optimised queries.

The kernel programmers have enough trouble fighting C optimisations in gcc - they don't want to have to fight far more obscure optimisations in C++.

How much effort / often times have you had to optimise a program for speed? It's an art that most young people just don't understand.

Cheers,
Wol

Scope-based resource management for the kernel

Posted Jun 17, 2023 19:36 UTC (Sat) by make (subscriber, #62794) [Link] (1 responses)

> There speaks the user-space programmer who thinks the solution for a slow program is to throw hardware at it.

Your whole post sounds awfully arrogant (and wrong). It doesn't add any content to the discussion, you only bash somebody you don't know with bogus and strawman arguments.

Compiler optimizations in C and C++ aren't fundamentally different, and aren't fundamentally different between userspace and kernel space. Yes, there are differences in both aspects, but not in a way that adds to the difficulty like you pretend it does.

> How much effort / often times have you had to optimise a program for speed? It's an art that most young people just don't understand.

Every day for several decades, and I wouldn't call it "art", because it's deterministic and mechanical, the simplest part of my job. Yes, many people don't value fast/lean code, that's a big problem that annoys me a lot, but your way of ranting at me says more about yourself. You sound like "confused old man yells at cloud" rather than "wise old man".

Scope-based resource management for the kernel

Posted Jun 26, 2023 14:36 UTC (Mon) by anton (subscriber, #25547) [Link]

Compiler optimizations in C and C++ aren't fundamentally different
My impression is that they are, and that the pretence that programs do not exercise standard-undefined behaviour comes, for a large part, from C++ programmers, while those who use options like -fno-strict-aliasing to get rid of "optimizations" that are based on this pretence are, for a large part, C programmers.

In earlier discussions on the topic, several advocates of these "optimizations" argued that they are very useful for not-quite-source code coming from expanding the original source code (typically when I pointed out that nobody writes programs in a way that the advocate just showed as an example of the usefulness of the "optimization"), and for the same reason that it's not a good idea to warn when the "optimization" strikes (supposedly there is so much not-quite-source code where the "optimization" strikes that one supposedly would be flooded with warnings). Both claims are very doubtful for even the most macro-laden C code, so I assumed the advocates had C++ code in mind; or maybe the advocates were just bullshitting me.

Anyway, I wonder how much the fact that GCC and Clang are written in C++ contributes to adding these kinds of "optimizations" (especially adding them (at first) without a kill switch).

Scope-based resource management for the kernel

Posted Jun 18, 2023 2:24 UTC (Sun) by tialaramex (subscriber, #21167) [Link] (2 responses)

So where is it then? Where is your C++ Linux kernel?

Scope-based resource management for the kernel

Posted Jun 19, 2023 11:38 UTC (Mon) by zorro (subscriber, #45643) [Link] (1 responses)

You already know the answer to that question, so why ask it? Your trolling doesn't add anything useful to the discussion.

Scope-based resource management for the kernel

Posted Jun 19, 2023 12:32 UTC (Mon) by rahulsundaram (subscriber, #21946) [Link]

> You already know the answer to that question, so why ask it? Your trolling doesn't add anything useful to the discussion.

Bringing up C++ when it is never going to be used in the Linux kernel adds nothing to the discussion in the first place.

Scope-based resource management for the kernel

Posted Jun 17, 2023 20:57 UTC (Sat) by mss (subscriber, #138799) [Link] (5 responses)

The Linux source code is readily available though, so you can try this out for yourself and make sure not to go talking about this fantasy again until you've actually found out what the reality is like and have something to show for your effort.

There has been a few patch sets posted to allow C++ usage in the Linux kernel - the most recent one AFAIK was posted as an April Fools' joke 5 years ago.

Although, given the well-known upstream hostility to C++, people find it hard to justify the amount of work required to prepare a serious RFC.

Scope-based resource management for the kernel

Posted Jun 17, 2023 21:22 UTC (Sat) by mb (subscriber, #50428) [Link] (3 responses)

There is no hostility.
It's just that the advantages of using C++ are not seen by kernel developers as being significant enough to justify the downsides of having C++. (e.g. much longer build time).

Scope-based resource management for the kernel

Posted Jun 17, 2023 22:29 UTC (Sat) by mss (subscriber, #138799) [Link] (1 responses)

There is no hostility.

Expressions like C++ is a horrible language or the crap that is C++ or (more recent) It really is a crap language certainly sound like hostility.

Scope-based resource management for the kernel

Posted Jun 18, 2023 12:38 UTC (Sun) by tialaramex (subscriber, #21167) [Link]

Sentiments from WG21 (the C++ committee) members don't seem far removed from what you're describing here as "hostility". Their conclusions are different but the reported facts seem similar.

The remaining C++ proponents continue to believe in a sort of Phlogiston Theory for programming languages. If they can just add the right things to C++ then it will not be an over-complicated language any more. It's true that in C++ 11, C++ 14, C++ 17, C++ 20 and C++ 23 the additions did make the language even more complicated each time, but surely this time it will work.

Scope-based resource management for the kernel

Posted Jun 18, 2023 6:01 UTC (Sun) by make (subscriber, #62794) [Link]

> much longer build time

Switching GCC from C to C++ mode alone has no effect on the build time. There are C++ features that do add to the build time, like excessive use of templates, but so do C features like excessive use of macros. It depends on which C++ subset you allow.

I bet there's no build-time difference between Peter's pseudo-RAII using macros and the GNU extensions and "true" C++ RAII (using no other C++ feature).

Scope-based resource management for the kernel

Posted Jun 17, 2023 23:11 UTC (Sat) by Cyberax (✭ supporter ✭, #52523) [Link]

> There has been a few patch sets posted to allow C++ usage in the Linux kernel

It used to be possible to compile Linux with C++ enabled, back in the early 2.6 days. I remember because I worked with a hardware device that had a C++ kernel module.

Then the kernel started using "class" and "operator" as regular variables.

Scope-based resource management for the kernel

Posted Jun 22, 2023 8:28 UTC (Thu) by marcH (subscriber, #57642) [Link] (11 responses)

> On the other hand, allowing a C++ (subset) would make all desirable C++ features...

This is the main issue: define "subset" and "desirable".
Everyone will have different answers (and C++ has everyone's favorite features).

Another top problem is: "C++ is not hard, your developers are just holding it wrong". I've called this the "mythical workplace" argument.

Scope-based resource management for the kernel

Posted Jun 22, 2023 9:24 UTC (Thu) by make (subscriber, #62794) [Link] (10 responses)

> This is the main issue: define "subset" and "desirable". Everyone will have different answers (and C++ has everyone's favorite features).

If that's your main issue, then luckily it's a non-issue.

The premise of the article is that the kernel developers have already decided they want (something like) RAII, and my point is that for this feature, C++ is a better tool than C with non-standard extensions.

Discussions which C++ features are allowed in the kernel is no different than any other coding style discussion. There are already discussions on which C standard shall be used and which new C standard features are acceptable in the kernel (like atomics). Nothing new here, just more of the usual.

> Another top problem is: "C++ is not hard, your developers are just holding it wrong".

That's a straw man. Let's discuss the language rather than discussing theoretical arguments from hypothetical C++ fanboys, because that's not relevant for the language choice.

About C++: I know very well that C++ is hard, and I admit it's a complex mess of a language. Many reasonable things can be said about the ugliness of C++.

If you're starting with C, and you want to use certain advanced features like RAII, C++ is a good choice, because the migration is so simple. It is certainly reasonable to say Rust is better than C++, and I don't argue C++ should be used *instead* of Rust - I only say C++ could easily be used *now* to improve code quality where a Rust migration is not possible currently. Additionally, a future rewrite to Rust is easier from C++ with RAII than from plain C, because the source code flow in C++ and Rust is similar. C++ is actually a good intermediate language for a smoother migration to Rust.

Scope-based resource management for the kernel

Posted Jun 22, 2023 10:32 UTC (Thu) by mb (subscriber, #50428) [Link] (8 responses)

>because the source code flow in C++ and Rust is similar.

No, not at all.
Rust has no inheritance. Code flows between ideomatic C++ and ideomatic Rust are quite different.

It does not work to say that we're going to switch to C++ but only use RAII from it.
People *will* start using more and more of C++'s features.

It's better to keep that huge amount of stuff out of the kernel to begin with.
The C implementation might not be as pretty as C++, but pulling in C++ comes with a tremendous cost of arguing over and over again why this and that C++ feature shall not be used in the kernel.

Scope-based resource management for the kernel

Posted Jun 22, 2023 18:38 UTC (Thu) by make (subscriber, #62794) [Link] (7 responses)

> No, not at all. Rust has no inheritance. Code flows between ideomatic C++ and ideomatic Rust are quite different.

You're missing the point. The article and this thread is about using RAII in the Linux kernel, not about inheritance. RAII works the same in C++ and Rust (and in C with GNU extensions, as it's going to be used soon).

The fact that C++ has features that Rust has not (which I didn't suggest to use and which do not affect the code flow) does not falsify my argument - it doesn't even have anything to do with this thread.

> It does not work to say that we're going to switch to C++ but only use RAII from it.

It works with other C language features or Rust language features or coding style policies or anything else. What makes you think policies about acceptable C++ features are different? Just because of "arguing over and over again"? Why is arguing over C++ features different? Bikeshedding discussions have existed as long as Linux exists, and will always exist. This argument seems rather arbitrary, because you can use the same argument to reject C11, C17, Rust (or anything else).

> The C implementation might not be as pretty as C++

Agree. We don't agree on the rest, but that's fine.

Scope-based resource management for the kernel

Posted Jun 22, 2023 18:55 UTC (Thu) by mb (subscriber, #50428) [Link] (6 responses)

>You're missing the point.

No. But you didn't read my full text, before answering on the first paragraph.

>What makes you think policies about acceptable C++ features are different?

So you really think that inheritance and virtual functions would not be one of the first next C++ features that people would demand, if we would compile the kernel with a C++ compiler?

Converting all manual implementations of virtual function calls is the next obvious transistion we would have.
And there goes your same code flow.

Scope-based resource management for the kernel

Posted Jun 22, 2023 19:07 UTC (Thu) by make (subscriber, #62794) [Link] (3 responses)

> No. But you didn't read my full text, before answering on the first paragraph.

Oh yes, you were missing the point, because instead of replying to the point I made (about RAII), you made up a new story and replied to this story of yours.

Now you came up with a new story, this time about virtual functions, only to prove that code flow in C++ and Rust is different after all, but that again misses my point. Don't you get it? This article and this thread is about RAII and nothing else.

Scope-based resource management for the kernel

Posted Jun 22, 2023 19:19 UTC (Thu) by mb (subscriber, #50428) [Link] (2 responses)

>only to prove that code flow in C++ and Rust is different after all

Nice that you agree.

Scope-based resource management for the kernel

Posted Jun 22, 2023 19:35 UTC (Thu) by make (subscriber, #62794) [Link] (1 responses)

> Nice that you agree.

No, I don't agree. Virtual functions don't affect the code flow; they are just syntactic sugar for dynamic dispatch, but the code flow is the same as with other ways of doing dynamic dispatch (no matter if C++, C or Rust). But, uh, we're now even more off-topic, you're dragging me away from the topic (=RAII) so much, that I'm confident you're just a troll, and I've already been feeding you too much, shame on me. That was unexpected on LWN.

Scope-based resource management for the kernel

Posted Jun 22, 2023 19:41 UTC (Thu) by mb (subscriber, #50428) [Link]

>but the code flow is the same as with other ways of doing dynamic dispatch (no matter if C++, C or Rust).

No it isn't.

Scope-based resource management for the kernel

Posted Jun 22, 2023 20:51 UTC (Thu) by marcH (subscriber, #57642) [Link] (1 responses)

> So you really think that inheritance and virtual functions would not be one of the first next C++ features that people would demand, if we would compile the kernel with a C++ compiler?

Of course it would; implementing classes in C is not fun at all and it's done all over the kernel already: https://lwn.net/Articles/444910/

BTW I suspect this is another reason people want to keep C++ out: Object-Oriented Obfuscation. There are some problems to which object-orientation maps well but C++ went really "all-in" and kept functional programming techniques out of developers' mindsets for much too long of a time, holding back the entire industry for decades.

Funny how Javascript out of all languages helped functional programming finally break out.

Rust is of course more functional than OO.

Scope-based resource management for the kernel

Posted Jun 22, 2023 21:02 UTC (Thu) by marcH (subscriber, #57642) [Link]

> There are some problems to which object-orientation maps well but C++ went really "all-in" and kept functional programming techniques out of developers' mindsets for much too long of a time, holding back the entire industry for decades.

Forgot the mandatory reference to the masterpiece, sorry for the extra email:

http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html

Yes this one is not about C++ but Java which is even worse. But: close enough.

Scope-based resource management for the kernel

Posted Jun 22, 2023 20:42 UTC (Thu) by marcH (subscriber, #57642) [Link]

> If that's your main issue,

Not "mine"; the issue of everyone who does not want C++

> then luckily it's a non-issue.

Must feel good to be right when so many people are wrong :-)

> my point is that for this feature, C++ is a better tool than C with non-standard extensions.

Yes! But: see above.

> Discussions which C++ features are allowed in the kernel is no different than any other coding style discussion.

Qualitatively yes. Quantitatively no. I know: I don't have any metrics to show you; these things are very hard to measure. But you don't have any either and the consensus / educated guess in Linux (and some other projects) is that the trade-off is not worth it. That is of course based on observations in other, actual C++ projects, I mean the lack of metrics does not mean it's based on thin air either.

BTW: which features are allowed is IMHO a small part of code style discussions. A very important part of course, maybe the most important one but small in "email volume". I'm just saying "code style" is not a great choice of words here, never mind.

> That's a straw man. Let's discuss the language rather than discussing theoretical arguments from hypothetical C++ fanboys

Oh come on: "you're holding it wrong" is the typical answer of _every_, _real_ C++ fanboy! Very vocal and hopefully not representative as usual on the Internet but certainly not "hypothetical".

> I know very well that C++ is hard, and I admit it's a complex mess of a language.

Thank you!


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds