News for hackers. Stuff that matters.
Re:Writing a Simple Linux Kernel Module by dottrap on Thursday November 30, 2017 @01:10PM (#15817981)
"And finally, you’ll need to know at least some C. The C++ runtime is far too large for the kernel, so writing bare metal C is essential."

That line reminded me that NetBSD added Lua for writing kernel modules. (https://news.ycombinator.com/item?id=6562611)

Anybody have any experiences to share from this?

Re:Writing a Simple Linux Kernel Module by alxlaz on Thursday November 30, 2017 @01:32PM (#15818204)
There are plenty of operating systems that use C++ on the kernel side of things. Some things, like exceptions, are frowned upon and rarely used -- if at all -- but there is no shortage of C++ kernel code. Just not in Linux.

When it comes to Linux, one could say that most reasons to avoid it are historical, but this does not quite paint the awkward truth -- namely that, for most of the kernel's lifetime (since back in 1991), C++ compilers simply did not have the level of maturity and stability across the breadth of platforms that Linux required. Linus Torvalds' stance on this matter is pretty well-known: http://harmful.cat-v.org/software/c++/linus .

Today, when x86-64 and ARM are the only two families that you need to care about in the following ten years or so (maybe RISC-V but I rather doubt it), it probably makes sense to look at C++ for operating systems work, but the runtime is certainly heavier than back when Linus was writing about it, too. A modern C++ compiler has a lot of baggage; C++ was huge back in 1998, now it's bloody massive. IMHO, all the reasons why you would want to use C++ (templating support without resorting to strange hacks, useful pointer semantics and so on) are reasonably well-served by cleaner languages with less hefty runtimes, like Rust. What these alternatives do lack is the amazing level of commercial support that C++ has.

Re:Writing a Simple Linux Kernel Module by jcelerier on Thursday November 30, 2017 @01:37PM (#15818252)
> C++ was huge back in 1998, now it's bloody massive.

I don't think any "run-time" feature was added since, though. It's all either OS support (<thread>, etc, that you wouldn't use in-kernel anyways) or template stuff that has 0 impact on runtime (and actually sometimes helps decreasing code size).

https://istarc.wordpress.com/2014/07/18/stm32f4-oop-with-emb...

https://www.embedded.com/design/programming-languages-and-to...

https://hackaday.com/2015/12/18/code-craft-embedding-c-templ...

If some guys are able to run c++ on 8kb microcontrollers, there's hardly a non-political reason it couldn't be used in-kernel.

See also IncludeOS: http://www.includeos.org/

Re:Writing a Simple Linux Kernel Module by alxlaz on Thursday November 30, 2017 @02:06PM (#15818527)
Additions have certainly been made since back in 1998 (things like smart pointers are relatively new on this scale, as far as I know). Many runtimes for resource-constrained embedded systems do not support all of C++'s features. Exceptions are the most usual omission.

You can certainly strip things down to a subset that can fit 128K of flash and need only 1 or 2K of RAM at runtime, but the question is not only one of computational resources used for the library itself. Additional code always means additional bugs, the semantics sometimes "hide" memory copying or dynamic allocation in ways that many C++ programmers do not understand (and the ones who do are more expensive to hire than the ones who do not), and so on. You can certainly avoid these things and use C++, but you can also avoid them by using C.

I agree that mistrust and politics definitely play the dominating role in this affair though. I have seen good, solid, well-performing C++ code. I prefer C, but largely due to a vicious circle effect -- C is the more common choice, so I wrote more C code, so I know C better, so unless I have a good reason to recommend or write C++ instead of C, I will recommend or write C instead. I do think (possibly for the same reason) that it is harder to write correct C++ code than it is to write correct C code, but people have sent things to the Moon and back using assembly language for very weird machines, so clearly there are valid trade-offs that can be made and which include using language far quirkier than C++.

Re:Writing a Simple Linux Kernel Module by cmrdporcupine on Thursday November 30, 2017 @02:27PM (#15818716)
I absolutely do not understand your point. Anybody doing OS development in C++ is doing so with absolutely no C++ standard library support, same as if you were using C++ to develop for your microcontroller. If C++ binaries are compact enough for Arduino or Parallax Propeller development (<32KB RAM), they are absolutely fine for kernel development.

The real answer is historical, and cultural. On the latter, Unix is a product of C (well, and BCPL) and C is a product of Unix. They two are intertwined heavily. The former is as was mentioned a product of the relative crappiness of early C++ compilers (and the overzealous OO gung-ho nature of its early adopters perhaps as well...)

C++ without exceptions, RTTI, etc. has a lot to offer for OS development. Working within the right constraints it can definitely make a lot of tasks easier and cleaner.

It won't happen in Linux, tho.

Re:Writing a Simple Linux Kernel Module by pjmlp on Friday December 01, 2017 @11:55AM (#15825127)
And lets not forget C++ got created, because Bjarne didn't want to touch C after his experience going from Simula to BCPL, but it had to be compatible with AT&T official tooling.
Re:Writing a Simple Linux Kernel Module by throwaway613834 on Friday December 01, 2017 @01:10AM (#15822269)
> I do think (possibly for the same reason) that it is harder to write correct C++ code than it is to write correct C code

I call BS on this.

So many mistakes in C simply cannot be made in C++ if you follow the well-established coding patterns and don't try to switch to C-style code. e.g.: You just cannot simply forget to free any resource, because you never have to do it with RAII. You cannot forget to check a status code and return early because you don't have to; the exception will propagate until someone catches it. You cannot forget to initialize a vector because it initializes itself. I could go on and on.

That said, there is a huge caveat to what I am saying above: I am comparing experienced programmers in each language with each other -- those who are basically experts and know what they're doing. I'm not debating whether it's easier to shoot yourself in the foot with C++ if you use it with insufficient experience (and part, though not all, of the reason is that you will probably write C-style code most of the time, and write neither proper C nor proper C++). I'm saying that an experienced programmer is much more likely to write correct code in C++ than C.

Re:Writing a Simple Linux Kernel Module by CyberDildonics on Thursday November 30, 2017 @02:39PM (#15818817)
> Additional code always means additional bugs

What additional code?

> You can certainly avoid these things and use C++, but you can also avoid them by using C.

Right, but what you can't get with C is destructors and move/ownership semantics.

> I do think (possibly for the same reason) that it is harder to write correct C++ code than it is to write correct C code

The ability to write typesafe data structures with move/ownership semantics and specified interfaces while being a superset of C would lead some to say that this is not true.

Re:Writing a Simple Linux Kernel Module by alxlaz on Thursday November 30, 2017 @03:51PM (#15819386)
> What additional code?

All the code that you need in order to support smart pointers, templates, move/copy semantics, exceptions and so on. To paraphrase someone whose opinions you should take far more seriously than mine, you can't just throw Stroustrup's "The C++ Programming Language" on top of an x86 chip and hope that the hardware learns about unique_ptr by osmosis :-). There used to be such a thing as the sad story about get_temporary_buffer ( https://plus.google.com/+KristianK%C3%B6hntopp/posts/bTQByU1... -- not affiliated in any way, just one of the first Google results).

The same goes for all the code that is needed to take C++ code and output machine language. In my whole career, I have run into bugs in a C compiler only three or four times, and one of them was in an early version of a GCC port. The last C++ codebase I was working on had at least a dozen workarounds, for at least half a dozen bugs in the compiler.

Re:Writing a Simple Linux Kernel Module by johncolanduoni on Thursday November 30, 2017 @04:25PM (#15819677)
If you’re writing a kernel, you’ll almost certainly be using “-nostdlib” (or your compiler’s equivalent) so unique_ptr, etc. won’t be there. You could however write your own unique_ptr that allocates via whatever allocator you write for your kernel. See [1] for a decent overview of what using C++ in a kernel entails.

[1]: http://wiki.osdev.org/C%2B%2B

Re:Writing a Simple Linux Kernel Module by throwaway613834 on Friday December 01, 2017 @01:16AM (#15822284)
> If you’re writing a kernel, you’ll almost certainly be using “-nostdlib” (or your compiler’s equivalent) so unique_ptr, etc. won’t be there.

Huh? unique_ptr has a customizable Deleter though; you should be able to provide a custom one so it doesn't call delete.

And doesn't a kernel implement a kmalloc() or something anyway? You would just write your own operator new and have it do what your kernel needs, and the rest of the standard library would just work with it.

Re:Writing a Simple Linux Kernel Module by johncolanduoni on Friday December 01, 2017 @12:18PM (#15825334)
True, but the "rest of the standard library" is somewhat harder to port and last time I played with it (admittedly some years ago) no compilers were good at letting you pick and choose.
Re:Writing a Simple Linux Kernel Module by jcelerier on Thursday November 30, 2017 @04:30PM (#15819720)
> All the code that you need in order to support smart pointers, templates, move/copy semantics, exceptions and so on.

smart pointers are their own classes, and exceptions would certainly be disabled in kernel-mode, sure, but for the rest ? which additional code ? there's no magic behind templates and move semantics, and no run-time impact. It's purely a compile-time feature.

Re:Writing a Simple Linux Kernel Module by CyberDildonics on Friday December 01, 2017 @06:46AM (#15823279)
> and hope that the hardware learns about unique_ptr by osmosis

Unique_ptr is just a template and does not need the standard library. Also move/ownership semantics don't need unique_ptr.

> The last C++ codebase I was working on had at least a dozen workarounds, for at least half a dozen bugs in the compiler.

There are at least four compilers that been extensively production tested - ICC, GCC, MSVC and Clang. Which one had these bugs and was it kept up to date?

Re:Writing a Simple Linux Kernel Module by nickpsecurity on Friday December 01, 2017 @11:13AM (#15824728)
I will say in this that C++ can certainly be used for small, fast kernels given L4/Fiasco was written in C++:

https://os.inf.tu-dresden.de/fiasco/prev/faq.html#0050

That was targeted at x86 with 2MB of RAM usage, though. So, I quickly looked for a microcontroller RTOS thinking that would be a good test. I found ScmRTOS that claims to be written in C++ with size being "from 1KB of code and 512 bytes of RAM" on up. So, it seems feasible to use C++ for small stuff. I'll also note that it has some adoption in high-assurance industry with standards such as MISRA C++ (2008) already available. They might be using powerful CPU's, though, so I looked up the MCU stuff.

http://scmrtos.sourceforge.net/ScmRTOS

The throwaways are talking safety features. I used to think that was a benefit of C++ over C. Following Worse is Better, that's no longer true: the ecosystem effects of C produced so many verification and validation tools that it's C language that's safer than C++ if one uses those tools. There's piles of them for C with hardly any in FOSS for C++ if we're talking about static/dynamic analysis, certified compilation, etc. I put $100 down that a CompCert-like compiler for C++ won't happen in 10 years. At best, you'll get something like KCC in K Framework.

The reason this happened is C++'s unnecessary complexity. The language design is garbage from the perspective of being easy to analyze or transform by machines. That's why the compilers took so long to get ready. LISP, Modula-3, and D showed it could've been much better in terms of ease-of-machine-analysis vs features it has with some careful thought. Right now, though, the tooling advantage of C means most risky constructs can be knocked out automatically, the code can be rigorously analyzed from about every angle one could think of (or not think of), it has best optimizing compilers for if one cares little about their issues, and otherwise supports several methods of producing verified object/machine code from source. There's also CompSci variants with built-in safety (eg SAFEcode, Softbound+CETS, Cyclone) and security (esp Cambridge CHERI). duneroadrunner's SaferCPlusPlus is about only thing like that I know of that's actively maintained and pushed for C++. The result of pro's applying tools on a budget to solve low-level problems in C or C++ will always give a win on defect rate to former just because they had more verification methods to use.

And don't forget that, like with Ivory language, we can always develop in a high-level, safer language such as Haskell with even more tooling benefits to extract to safety-critical subset of C. Extracted code that's then hit with its tooling if we want. We can do that in a language with REPL to get productivity benefits. So, we can have productivity, C as target language, and tons of automated verification. We can't have that with C++ or not as much if we had money for commercial tools.

So, these days, that's my argument against C++ for kernels, browsers, and so on. Just setting oneself up to have more bugs that are harder to find since one looses the verification ecosystem benefits of C++ alternatives. This will just continue since most research in verification tools is done for managed languages such as Java or C# with what's left mostly going to C language.

Re:Writing a Simple Linux Kernel Module by alxlaz on Friday December 01, 2017 @12:39PM (#15825580)
I'm really sorry you're getting downvoted, because there is a lot of useful data in your comment. And I think we definitely see eye to eye on this:

> The throwaways are talking safety features. I used to think that was a benefit of C++ over C. Following Worse is Better, that's no longer true: the ecosystem effects of C produced so many verification and validation tools that it's C language that's safer than C++ if one uses those tools. There's piles of them for C with hardly any in FOSS for C++ if we're talking about static/dynamic analysis, certified compilation, etc. I put $100 down that a CompCert-like compiler for C++ won't happen in 10 years. At best, you'll get something like KCC in K Framework.

Lots of people think additional safety features result in safer code. They likely do most of the time, but when you need to swear to investors, to the public and to the FDA that your machine will not kill anyone, what you want to have is results from 5 verification tools with excellent track records, not "my language does not allow for the kind of programming errors that C allows". Neither does Ada, and yet they crashed a rocket with it, with a bug related to type conversion in a language renowned for its typing system (not Ada's fault, of course, and especially not its typing system's fault -- just making a point about safety features vs. safety guarantees).

A more complex language, with more features, is inherently harder to verify. The tools lag behind more and are more expensive. And, upfront, it seems to me that it is much harder to reason about the code. C++ does not have just safety features, it has a lot of features, of all kind.

Re:Writing a Simple Linux Kernel Module by irhd on Thursday November 30, 2017 @07:41PM (#15821034)
> I do think (possibly for the same reason) that it is harder to write correct C++ code than it is to write correct C code

I'd disagree. Modern C++ has much better safety features than C ever has.

Re:Writing a Simple Linux Kernel Module by userbinator on Thursday November 30, 2017 @11:09PM (#15821827)
The "safety" is not the issue, it's the compulsion of using many layers of (somewhat leaky) abstractions that make debugging and otherwise reasoning about behaviour difficult.
Re:Writing a Simple Linux Kernel Module by pknopf on Friday December 01, 2017 @12:44AM (#15822181)
I could not agree with this more.

gstreamer, gtk, etc, are really easy to work with and browse the source.

This is why I love golang as well.

Side note, it is funny how much gstreamer and glib try to add c++ "ish" features to c.

Re:Writing a Simple Linux Kernel Module by baby on Friday December 01, 2017 @10:11AM (#15824201)
I was going to say +1 with golang, and you said it. So just emphasizing that many people prefer code clarity over cool things that make code unreadable (templates...)

Interestingly, I just found Linus' stance on Golang :) https://www.realworldtech.com/forum/?curpostid=104302&thread...

Re:Writing a Simple Linux Kernel Module by smhenderson on Friday December 01, 2017 @12:58PM (#15825727)
From that post... But introducing a new language? It's hard. Give it a couple of decades, and see where it is then.
Re:Writing a Simple Linux Kernel Module by baby on Friday December 01, 2017 @10:10AM (#15824193)
> 0 impact on runtime (and actually sometimes helps decreasing code size)

beware, performance and code size does not always go hand in hand!

https://channel9.msdn.com/Events/Build/2013/4-329

Re:Writing a Simple Linux Kernel Module by ahoka on Thursday November 30, 2017 @02:41PM (#15818842)
> There are plenty of operating systems that use C++ on the kernel side of things. Some things, like exceptions, are frowned upon and rarely used -- if at all -- but there is no shortage of C++ kernel code. Just not in Linux.

I used to work on one. It was developed in the 90s, around the same time C++ became an ISO standard. I think most of you have used it without knowing. :)

I think OOP is natural for writing kernels and most mainstream kernels implement some kind of object system because of this. I also wrote my toy kernel in modern C++, despite that I'm "fluent" in both C and C++. So yeah, C++ kernels are here, just not in Linux land.

Re:Writing a Simple Linux Kernel Module by saagarjha on Thursday November 30, 2017 @02:47PM (#15818893)
Are you referring to macOS?
Re:Writing a Simple Linux Kernel Module by Cyph0n on Thursday November 30, 2017 @02:50PM (#15818912)
I think the kernel itself is called Mach. Not sure if it was written in C++ though.
Re:Writing a Simple Linux Kernel Module by sigjuice on Thursday November 30, 2017 @03:10PM (#15819066)
The kernel is called XNU. https://opensource.apple.com/source/xnu/ It is mainly written in C.
Re:Writing a Simple Linux Kernel Module by self_awareness on Thursday November 30, 2017 @03:34PM (#15819264)
The BSD part of the kernel is written in C, but IOKit, the kext framework, is written in (a subset of) C++:

http://xr.anadoxin.org/source/xref/macos-10.13-highsierra/xn...

Re:Writing a Simple Linux Kernel Module by ahoka on Sunday December 03, 2017 @06:29AM (#15837131)
Nah. Something from telco land.
Re:Writing a Simple Linux Kernel Module by nabla9 on Thursday November 30, 2017 @02:58PM (#15818958)
>Today, when x86-64 and ARM are the only two families that you need to care about

Wait what? What happened to MIPS, z/Architecture,Power Architecture, QDSP6, TMS320?

Re:Writing a Simple Linux Kernel Module by alxlaz on Thursday November 30, 2017 @03:39PM (#15819307)
I meant that strictly in the realm of general-purpose operating systems (the article is, after all, about writing a Linux kernel). Two of those are DSPs. The Linux kernel and busybox account for 99,99% of the general-purpose code running on them, and bare-metal code for both is still very common. In fact, Linux runs on QDSP6 only under a hypervisor, and gained C6x support only somewhat recently (~5 years, I think?). Last time I saw a MIPS workstation was a very long time ago :-) and, barring a few niche efforts, the same goes for PowerPC as well.

Back when Linus was ranting, one could make a convincing case for supporting all of these architectures in an OS meant for general-purpose workloads, from server to desktop and from thin client (eh? feel old yet :-)?) to VCR -- and . Now you can sell an OS that supports nothing but x86-64 and a few ARMs, and you get most of the server market, and virtually all of the desktop and mobile market.

Obviously the architectures that you need for a particular project are the ones that you want to "care for" -- but in broad terms, it is perfectly possible today for someone to have worked as a programmers for ten years without encountering a single machine that is not either x86 or ARM-based. Someone who had 10 years of programming experience in 2003 probably ran into a SPARC system whether they wanted or not.

Re:Writing a Simple Linux Kernel Module by nabla9 on Thursday November 30, 2017 @05:09PM (#15820043)
We are talking about Linux kernel here. There are billions of embedded processors running Linux on other than ARM and x86.

S390X (IBM mainframes) and OpenPOWER variants are fully supported by Ubuntu and those are not insignificant market either in banking, finance, or numeric computing.

One reason why Linux runs all Top 500 supercomputers is because it they have number of relatively recent systems close to the top running Sunway (Alpha derivative) , Power and SPARC64. Fastest supercomputer today runs on Sunway.

Re:Writing a Simple Linux Kernel Module by johncolanduoni on Thursday November 30, 2017 @04:28PM (#15819700)
I think you’ve missed large segments of Linux’s user base; you’ve definitely missed Android devices using MIPS processors.
Re:Writing a Simple Linux Kernel Module by BlackLotus89 on Friday December 01, 2017 @05:51AM (#15823122)
Name one recent android device using mips. There are, to my knowledge, only a handful of mips android devices (literally).

There was an announcement for a 64bit mips android device for 2016 (Warrior i6400) but I didn't hear anything about it. Did you?

Also there was an ainol tablet with the mips architecture (nov07) which flopped (those are the only instances I can recall where mips on android really came into play).

Re:Writing a Simple Linux Kernel Module by comex on Thursday November 30, 2017 @06:50PM (#15820749)
I’m not sure the number of hardware architectures matters that much, as opposed to the number of OSes or compilers. If you have to target N different C++ compilers written independently by different vendors, then sure, each compiler has a heck of a lot of room for bugs in the frontend or standard library. But with a given compiler (say, GCC or Clang) that has N architecture backends, there shouldn’t be that much added risk of architecture-dependent bugs with C++, compared to pure C.

After all, most of the “extra stuff” that C++ adds on to C has no inherent hardware dependence and exists only on the frontend. By the time the code makes its way to the backend, templates have been monomorphized (i.e. copied and pasted for each set of parameters); methods have been lowered to functions with added ‘this’ parameters; hidden calls to destructors and conversions have been made explicit; and so on. For any given C++ program, you could write a C program that compiles to near-identical IR, so if the backend can reliably compile C code, it should be able to handle C++.

True, that hypothetical C program might not look much like a typical human-written program. In order to achieve zero-cost abstractions, C++ code tends to rely a lot more on inlining and other backend optimizations than pure C, even though those optimizations apply to both languages. So if the backend isn’t reliable, C++ may generate more ‘weird’ IR that could end up getting miscompiled, and the level of indirection may make it harder to figure out what’s going on. But compiler backends these days are a lot more reliable than they used to be. And buggy backends can certainly cause problems for C code as well.

Re:Writing a Simple Linux Kernel Module by jlg23 on Thursday November 30, 2017 @03:07PM (#15819031)
I don't get how you make the connection from Torvald's arguments to what you say. His complaints are on a fundamentally different level: ~"Programmers over-abstracting are a timeless problem, the more complex the language/standard library, the easier it is for programmers to get lost in abstraction. Bad programmers just get lost, mediocre programmers even defend what they do."

I despise his choice of words and general attitude towards programmers "less capable than him", but I do think there is some truth in what he says there: Imagine we'd all be writing asm only - we'd spend much more time on the drawing board and find much more elegant solutions to problems that we today solve with mindlessly writing hundreds lines of code within a "framework".

Side note: I wrote C++ in 1998; it was more "bloody massive" back then than it is today, in my humble experience.

Re:Writing a Simple Linux Kernel Module by alxlaz on Thursday November 30, 2017 @03:30PM (#15819228)
I was referring only to the parts of that rant that are/were of some objective value, e.g.:

> anybody who tells me that STL and especially Boost are stable and portable is just so full of BS that it's not even funny

> the only way to do good, efficient, and system-level and portable C++ ends up to limit yourself to all the things that are basically available in C.

You can certainly write C++ code without overabstracting it. Maybe Torvalds only ran into C++ programmers who liked overabstracting their code -- as it is usually the case with opinions of some people about other people, that part of the rant is best discarded :-).

But as my memory serves me, back when "portability" meant x86, SPARC, Alpha, ARM, MIPS, PPC, PA-RISC and m68k, the only way to do "good, efficient, and system-level and portable C++" was to limit yourself to the (relatively small) subset of C++ that was well-implemented by widely-available compilers (particularly GCC) across all these architectures -- not as a matter of discipline and code elegance, but because writing non-trivial C++ code back then usually resulted in very unpleasant trips down the standard library's source code and poring over objdump outputs trying to figure out what the hell broke this time.

Re:Writing a Simple Linux Kernel Module by pjmlp on Friday December 01, 2017 @11:58AM (#15825160)
Yeah, which is why Java felt like fresh air compared with those portability hurdles.
Re:Writing a Simple Linux Kernel Module by lloeki on Friday December 01, 2017 @09:56AM (#15824129)
> There are plenty of operating systems that use C++ on the kernel side of things

Language-wise, Darwin/XNU/IOkit uses a subset of C++, notably one that excludes exceptions and templates IIRC. stdlib-wise I suspect it's even more restricted/different.

Re:Writing a Simple Linux Kernel Module by vkjv on Thursday November 30, 2017 @03:38PM (#15819295)
Has Linux stated any opinions on rust? Some of his issues with C++ also apply to rust.

> any compiler or language that likes to hide things like memory

Re:Writing a Simple Linux Kernel Module by steveklabnik on Thursday November 30, 2017 @03:49PM (#15819366)
“That's not a new phenomenon at all. We've had the system people who used Modula-2 or Ada, and I have to say Rust looks a lot better than either of those two disasters.”

https://www.infoworld.com/article/3109150/linux/linux-at-25-...

Re:Writing a Simple Linux Kernel Module by madez on Friday December 01, 2017 @01:09AM (#15822267)
Is Rust available on the platforms Linux targets? The kernel community won't react fondly to tries to restrict the target support. I'd expect a justified rant with lots of insults at intellect and sanity, if need be.

Is Rust well-understood, both by the users and the toolchain developers? How many independent compilers are there for Rust? What architectures do they support?

I'm not implying that Rust isn't suitable for Linux, but it has open issues, that prevent usage right now. Rust and it's core ecosystem have a lot of churn, and still feel like a rather early work-in-progress. One example not mentioned is the Rust language server and its integration with consumers.

I cheer for Rust, and hope for solid progress.

Re:Writing a Simple Linux Kernel Module by flukus on Friday December 01, 2017 @01:36AM (#15822358)
Another issue I've seen with rust (benchmarks) that's rarely talked about is memory usage, which is pretty important in a kernel. Sometime it only uses a little more memory, but quite often it will use twice as much and occasionally an order of magnitude more.

To be fair these benchmarks are aiming for speed, but the c baseline typically produces faster code with less memory, so it may very well be a case of TANSTAAFL.

Re:Writing a Simple Linux Kernel Module by madez on Friday December 01, 2017 @01:41AM (#15822379)
I don't see how it necessarily must be TANSTAAFL. Maybe Rust is indeed suited for being zero-cost in memory and speed performance compared to C, but the toolchain isn't there yet.
Re:Writing a Simple Linux Kernel Module by steveklabnik on Friday December 01, 2017 @08:19AM (#15823596)
We use jemalloc by default, which does its own caching. A closer comparison would be to use the system allocator, or make the C code use jemalloc as well.
Re:Writing a Simple Linux Kernel Module by steveklabnik on Friday December 01, 2017 @08:20AM (#15823603)
Oh, I'm not saying Linus would accept Rust code in the kernel at all; I don't think he should for a variety of reasons, including the ones you're talking about. That wasn't what I was responding to, though.
Re:Writing a Simple Linux Kernel Module by pjmlp on Friday December 01, 2017 @11:50AM (#15825075)
BeOS, Symbian, macOS, Windows, AS/400, GenodeOS, mbedOS

Just a couple of examples.

Re:Writing a Simple Linux Kernel Module by waddlesplash on Friday December 01, 2017 @01:30PM (#15826004)
BeOS' kernel itself didn't use C++ if I recall correctly, though some drivers did. Haiku, BeOS' open-source successor, has a kernel written almost entirely in C++.
Re:Writing a Simple Linux Kernel Module by sigjuice on Thursday November 30, 2017 @01:29PM (#15818176)
If you look at the NetBSD kernel source https://github.com/NetBSD/src.git, it doesn't look like there are any real and substantial Lua kernel modules.

  $ find sys -name '*.lua'
  sys/modules/examples/luahello/luahello.lua
  sys/modules/examples/luareadhappy/happy.lua
  sys/modules/lua/infinite.lua
  sys/modules/lua/test.lua
  sys/modules/luasystm/test.lua
Re:Writing a Simple Linux Kernel Module by chowyuncat on Thursday November 30, 2017 @04:03PM (#15819521)
At work we write plenty of C++ for kernel modules, but with the caveat that our C++ can rarely include a Linux kernel header directly. We have to write a small shim for every kernel function we consume.

No library runtime features, so no RTTI or exceptions, but we use templates and dynamic dispatch.

Re:Writing a Simple Linux Kernel Module by pag on Thursday November 30, 2017 @09:52PM (#15821549)
I created a linux kernel dynamic binary translator [1] (think of it as being like an in-situ vmware esxi) using C++. The key was to not use anything that touches floating point. Mostly what I wanted was templates, atomics, and a few other nicities. Recently I got granary working on the 4.4.0 kernel (after a few manual tweaks here and there to the kernel source code and to some of granary's auto-generated files).

[1] https://github.com/Granary/granary

Re:Writing a Simple Linux Kernel Module by revelation on Thursday November 30, 2017 @02:29PM (#15818736)
There is plenty of C++ in the Linux kernel. It just masquerades as function pointers and initial fields in structures called "base".
Re:Writing a Simple Linux Kernel Module by TFortunato on Thursday November 30, 2017 @03:22PM (#15819159)
Your getting downvoted, but you are kind-of correct. Even if it isn't actually written in C++, there is a lot of "OO-style" code in the kernel that does things like dynamic disoatch using VTables...basically making explicit what C++ is going to do behind the scenes for you anyways.

I can see the argument from Linus' POV about avoiding over abstraction, and knowing exactly what your code is doing on a low-level, but at the same time it leads to a lot of reinventing the wheel and makes me think there is a better balance to be found.

https://lwn.net/Articles/444910/

https://lwn.net/Articles/446317/

Re:Writing a Simple Linux Kernel Module by madez on Friday December 01, 2017 @01:12AM (#15822273)
It doesn't only make you think there is a better balance, it also makes you think harder whether the task at hand needs this kind of complexity. If this is all hidden under abstraction, developers might not care enough.
Re:Writing a Simple Linux Kernel Module by TFortunato on Friday December 01, 2017 @12:56PM (#15825723)
I absolutely agree with you on the dangers of overabstracting / hiding too much detail. That said, I do wonder if things have changed, since Linus's c++ "rant" re: the type of developers who would choose to work on kernel code in c++. I do remember a time when learning c++ was very much in popular, before the "in" language moved to java, to python, to JS, etc., so there may have been more of a problem with inexperienced coders back then than there is now. (I have no idea if this is true at all, just curious!)

I'm addressing this as someone who has done C++ work on very small embedded systems (e.g. microcontrollers with no external memory) including RTOS code and low-latency control loops, and there were definitely some nice features in C++ that can save you some time and lines of code without adding a bunch of needless overhead. Of course, there was a learning curve when bringing new people on board the project about what was/wasn't allowed for performance reasons. In general though, I see no reason why there couldn't be some subset of C++ used for kernel development other than Linus' / the kernel dev community's general distaste for it.

Re:Writing a Simple Linux Kernel Module by lmilcin on Friday December 01, 2017 @04:45AM (#15822915)
It is my understanding that there is no technical reason but rather a personal preference of Linus to continue working with plain C.

I did some kernel development (mainly some drivers) and I am also doing a lot of embedded development (credit card terminals, personal projects for ARM Cortex-M) I must say that I am happy with this choice.

The kernel is complex enough and you want to focus on understanding what really matters without being distracted by arcane constructs that would inevitably come with C++.

My experience with C++ guys is that there will inevitably be a population of smart developers which will try to be "smart" with the language which typically ends up with everybody else spending more time on understanding what is happening than the time that was actually saved by the construct.

C does not pose that problem. C is simple and there is relatively little occasion to be very smart with it, which is a good thing, IMHO.

Re:Writing a Simple Linux Kernel Module by albinofrenchy on Thursday November 30, 2017 @03:15PM (#15819110)
The C++ runtime is too big for the kernel. It is also largely unnecessary. You can easily compile C++ code without STL or larger library support.

This still leaves some holes that the runtime provides, but all of which are easy to provide -- namely things like 'new' and 'delete'.

Re:Writing a Simple Linux Kernel Module by josteink on Friday December 01, 2017 @01:43AM (#15822383)
> "And finally, you’ll need to know at least some C. The C++ runtime is far too large for the kernel, so writing bare metal C is essential."

And here I was about to ask if anyone has a similar article based on Rust :)

Re:Writing a Simple Linux Kernel Module by feelin_googley on Thursday November 30, 2017 @04:09PM (#15819550)
"Anybody have any experiences to share from this?"

http://lua-users.org/wiki/MarcBalmer