471 points · by Klasiaster · 5y ago · 194 comments
I know this kind of stuff spark the ``it's meaningless to rewrite everything (especially Linux) in Rust'' debate. I agree 100% that rewriting Linux in Rust (or your favorite language) is just a waste of time and such a project won't live long.
That said, what I'd say here is that it's fun. Really fun. Implementing ABI compatibility requires you to understand under the hood of the kernel. You can learn how printf(3) works. You can learn what happens before main().
Especially, in Rust, it's inspiring to explore ways to write a kernel safely and expressively. One of my favorite part is the implementation of read system call [1].
Lastly, You can try this OS on SSH. Hope you enjoy :)
ssh root@kerla-demo.seiya.me
[1]: https://github.com/nuta/kerla/blob/main/kernel/syscalls/read...Considering that this is exactly how Linux was born (just a hobby project for fun), I wouldn't assume so fast that it's useless. Moreover, you don't need to justify yourself if you just want to have fun !
I agree they shouldn't have to justify themselves, but it is handy to preempt that.
In fact in this instance I think it's a little disingenuous to quote Linux and say it could happen again. The industry is totally different now. There's much more competition than there was when Linux was released and that competition is much more mature too. Plus the bar for a production-quality kernel is a lot higher than it was when Linux was released.
Seems apropos to me, given the fact that a Linux ABI compatible hobby project is under discussion - and that everyone here is familiar with the famous Usenet announcement.
> The industry is totally different now. There's much more competition than there was when...
I wonder how you define "competition"? Because there were way more operating systems in use then, and the industry was far more fractured. Fractured in a way that was meaningful - not like today where you can spin up a VM and be productive in short order, thanks to the significant lack of distinguishing difference. That is the really interesting thing about these hobby projects - they introduce possibilities that are either completely ignored by organizations suffering from inertia constraints, or can't be mimicked because they're diametrically opposed to present designs.
Indeed, there's a word for it which I find particularly lovely, autotelic.
I think it's the opposite. When Linux was released, basically every major tech company had their own variant of Unix. Now almost everyone has migrated to Linux or Windows.
Competing with modern Linux by creating a drop-in replacement for Linux would be a pretty difficult task given that people have been optimizing Linux for decades now, but on the other hand I think there are ways to improve dramatically on the traditional POSIX-style API that Linux mostly adheres to. For a random example, why can't a given process have more than one "current working directory" at a time? That seems like an arbitrary limitation imposed by an implementation detail in early Unix system, and it causes problems for modularity. There are many other little details like that. I think if Linux is replaced by something eventually, it'll most likely be because the new thing has a cleaner, more powerful, or more generally usable API.
(Kerla is apparently not trying to invent a new API; I'm just saying that's the direction I would recommend to anyone project with a goal of seriously competing with Linux.)
For example, I've written a Mandelbrot visualizer so many times I've lost count. Not because the world needs another poorly written or optimized rainbow-ladybug-simulator, but because it serves as a slightly-non-trivial hello-world. For example, it's the first end-to-end thing I made in Common Lisp. https://git.sr.ht/~amtunlimited/mandelbrot-plot
I just rewrite them all the time as means to get a feeling about programming languages.
I dumped a couple of other stuff on GitHub so that HR people are happy to get a link that they never read anyway.
Then I get back to Java and .NET at the office. :)
* Some people bake bread even though there's a good bakery nearby.
* Some people grow gardens even though there's a farmers' market down the street.
* Some people restore old cars even though there's a good restoration professional in town.
* Some people make k8s clusters on rpi even though they could rent that for cheap.
You're making an OS that's linux compatible even though there's already linux. And that is awesome!
[0]: https://gitlab.com/0xTJ/mosys/-/blob/master/src/vfs.c#L469
[1]: https://gitlab.com/0xTJ/mosys/-/blob/master/src/vfs.c#L907
If only you could write an entirely new (meant to be better, for some cases at least) kernel compatible with Linux hardware drivers - this would be not waste at all but in fact fantastic.
An idea in my mind is to use Kerla for virtualized environments like KVM where only limited device drivers are needed (virtio for example).
IMHO getting a minimal set that works is good enough to get people on board. This is still nontrivial. But for example, getting FUSE working would be useful. Even if the driver itself was not ABI compatible, it would bring functionality and someone might then aim for ABI compatibility afterward which would open even more doors.
Especially since, as I understand it, even Linux isn't compatible with the Linux driver API across versions; they can and will change internals at will and just update in-tree drivers to match. Hence some of the difficulty doing things like getting a newer kernel on assorted embedded devices (ex. 99% of phones) because you have to port the vendor's drivers to the new version and both sides changed stuff.
What do you think about other Rust OS projects like Redox?
Aside from Redox, I'd mention Tock [2], an embedded operating system. It introduces a novel components isolation using Rust's power. I believe this field is where Rust shines and am looking forward to seeing it in production.
[1]: https://www.redox-os.org/screens/ [2]: https://www.tockos.org
https://github.com/betrusted-io/xous-core
As a embedded service processor OS for a big server rack, Oxide Computer is working on 'HubrisOS'. They seem to have not released it yet, but that will be open sourced.
https://github.com/oxidecomputer
Those are two efforts where I know real resources are going into.
I have to say, I feel a bit “dirty” carrying so much unused and legacy code around with Linux, so I like people trying to reinvent the wheel just for the pleasure of a fresh start. For the aesthetics. Even if it’s merely a fantasy and not replacing anything soon, realistically. They are also keeping OS development accessible to new generations of geeks. The unfriendliness of C, the gigantic codebase and seemingly distinct culture make the Linux kernel quite off putting, filtering possible engagement by unfortunate parameters IMO. Novel OS development in Rust takes away at least some of those barriers and some of the gained knowledge may be applicable with the Linux kernel later.
> You can learn how printf(3) works. You can learn what happens before main().
Yes! It's such a wonderful experience. This is exactly what I most enjoy doing, just seeing how things work, maybe making my own version. I hope you have lots of fun.
You're reimplementing Linux's kernel-userspace binary interface, right? The system call interface is stable and language agnostic, it's really nice. Some pointers for anyone who'd like to know more:
https://man7.org/linux/man-pages/man2/syscalls.2.html
https://man7.org/linux/man-pages/man2/syscall.2.html
https://github.com/torvalds/linux/blob/master/Documentation/...
The permissive license would also be interesting in some applications.
IMO, writing and running tests in the kernel space is pretty easy thanks to Rust’s flexible testing feature [1].
"I completely agree that rewriting existing large, feature-rich, and robust operating system kernels is not a good idea. However, a question came to my mind: what are the pros and cons of writing a practical UNIX-like OS kernel in Rust from scratch? How does it look like? This is why I started this project."[1]
I would love to see a solid implementation of a real micro kernel based OS based on a message passing core in Rust to become a viable alternative to the now 50 year old Unix architecture. This would give us a possibility of some real progress on the security and process scheduling front, both of which are severely lacking in the Unix model.
In fact, micro kernels have a lot of issues and limit the possibilities of what an OS can do! One of the most important issues is they prevents resource sharing in a rich and efficient way between the components of the kernel. They make building rich relationships between kernel data structures and kernel subsystems very messy, complicated, impracticable and very inefficient.
Micro kernels are delusional and people should stop implicitly assuming their superiority.
Rust's memory model largely makes those isolated address spaces unnecessary, and sending a message at runtime basically amounts to copying a pointer. So, with these performance issues resolved, I think it makes sense to look at microkernels again. The reasons why Linus was opposed to writing Linux as a microkernel in the 90's don't necessarily still apply now in 2021.
There may some things the Linux kernel does that can't be efficiently expressed using a message-passing style, but I don't know if that's actually true or not.
I think you're confusing several concepts. Rust's "memory model" is the C++11 memory model. Maybe you meant the borrow checker and the distinction between shared and exclusive references? Yes, those make it much harder to accidentally screw up other parts of your address space, but it's downright trivial to do it on purpose – you just need an unsafe block and std::ptr::write.
There's nothing about Rust that could replace isolated address spaces. Rust is typically compiled to native machine instructions, there's no sandbox or virtual machine that will protect different modules from each other.
But this does argue against message passing, since such systems preferentially try to use messages over sharing whenever it's possible.
There have been several other "real micro kernel based OS based on a message passing core", and they too, by almost any metric, have been a failure.
Process scheduling in Linux in 2021 is far, far ahead of anything in any other OS, ukernel or MP-based. The idea that there hasn't been progress in this area is really completely absurd. In fact, the general idea that Linux follows "the now 50 year old Unix architecture" is also pushing the boundaries quite a lot.
But you're right, QnX is the one (probably the only?) micro-kernel that has seen widespread adoption. Almost all of it has been in the context of embedded systems, which doesn't invalidate the substantial success, but it does leave QnX as the exception that proves the rule. It also leaves QnX as yet another example of the general failure of microkernels as general purpose computing platforms. QnX design is excellent for the contexts where it is deployed, but there's a reason you don't run it on PCs, tablets, data crunchers, smartphones and many other sorts of computing contexts.
BeOS wasn't half bad. The failure was probably mostly commercial. It is true that they moved their networking stack into the kernel, but it's not entirely clear to me that they had to do that, it was just the most expedient way to get acceptable performance and stability given the (programmer-time) resourcing constraints of the project.
Fair point. You could probably say this of other attempted microkernels too, to be even fairer. But that doesn't really change the fundamental point that just cooking up "a better OS design" doesn't lead to it being successful. There's a lot more in play that "mere quality".
https://en.wikipedia.org/wiki/QNX
https://en.wikipedia.org/wiki/Fuchsia_(operating_system)
https://lobste.rs/s/p8bizb/getting_know_fuchsia_google_s_ope...
I've taken an intro to OS class and am currently going through Linux From Scratch [1], which is interesting and is teaching me a lot, but it's more about how to setup a Linux distro using existing packages and not really about reading/writing the code involved.
Any recommendations?
For more general information, there are also a handful of articles and/or blog posts that I've come across when I was getting started that gave a very good introduction to many of the low-level components of how computers work and what the OS needs to do to run them. One of my favorites was Many But Finite [1]. Check out a bunch of his posts about the memory map, the computer boot process [2], cpu rings, etc... Not exhaustive by any means, but good, well-presented info. Many other such sources exist; maybe others will share their favorites.
And if you'd just like to get a more gentle introduction into actual OS code than the monster that is Linux, maybe try looking at some of the BSD's. NetBSD [3] and OpenBSD [4] are remarkably easy to navigate and follow from a code flow perspective, and if you just want to see how things work in a real OS, it's both a good reference and a good place to start.
[0] https://wiki.osdev.org/Main_Page
[1] https://manybutfinite.com/archives
Also, you could try reading the Plan 9 source code.
(I would say the code is what you're after now, but in case you are interested in more of the theory of why it's designed that way, you can check out the research paper here: https://dedis.cs.yale.edu/2010/det/)
ssh root@kerla-demo.seiya.meAlso, as owner of the instances are you able to see what commands people are running? First thing I did was to type hello, and it said hello world to me :)
The mechanism is pretty simple: a Node.js server (running on genuine Linux) listens on tcp:22. Once you connect, it boots a dedicated Firecracker microVM instance and forwards network packets between your SSH client and VM.
Regarding the command history, others (including I) can't see what you type. If you could, it must be a vulnerability.
I think a killer feature would be to emulate the Linux kernel device driver model such that existing C driver modules could be used with Kerla. Further, borrow (no pun intended!) the Linux device driver Rust wrappers from the Rust for Linux kernel project to then enable writing Linux device drivers in Rust.
The question is whether emulating the Linux device driver model (by which I kind of mean mimicking the set of core kernel services needed to support drivers - kernel threads, sync primitives, allocators etc) ends up being a massive task akin to writing the core kernel itself.
I think there might be reasonable trade offs that make the problem more tractable.
Having the ability to have a significantly simpler kernel with Rust’s memory and thread safety guarantees plus the ability to use even a small set of Linux’s massive set of drivers would be very compelling.
Heck, it'd probably be easier to emulate the Windows driver architecture - at least those have proper ABI compat.
My take away was that while 'new' driver classes/subsystems do involve more churn in the core kernel support subsystems, there is a clear trend towards convergence in terms of significant change.
If the change truly was significant and continuous then that would impede productisation with the Rust kernel. I'm not saying that the A{B,P}Is are in any way stable but I personally don't think that keeping up will be an intractable problem.
In the spirit of Kerla's authors prime motive - which appears to be hacking for knowledge - this might be something to try, perhaps with a driver class that is known to be more stable.
And better drivers.
The kernel has (deliberately) never had a simple or stable driver ABI, so I think this is potentially a huge task. And then you've also left a C-based attack surface in place.
I'm not sure what the minimal set is if you decide to only support 2021-era hardware. PCIe+SATA+framebuffer+USBhost+USBHID?
I think using the Rust-for-linux wrappers to implement the drivers in Rust would eventually help.
Also, even if initially Linux C drivers were transplanted into Kerla that could be a net benefit if the tradeoff of wide device driver availability vs increase in the trusted compute base was acceptable.
I personally think that tradeoff would be acceptable - if the transplanting ability was tractable. It's a hard one to absolutely agree on, admittedly.
Towards the minimal set of hardware, fair point! I think that one should be able to get quite far with judicious selection of device driver classes and leave the truly in-flux classes alone ?
While I completely agree that NetBSD's Rump kernel would be a great way to go (I've played with it in the past and quite like it!) I do wish there was a similar initiative bound to the Linux kernel.
The closest I've seen is Octavian Purdila's Linux-Kernel-Library project: https://lkml.org/lkml/2015/11/3/706
Not sure how far they've come though. Perhaps Kerla could be a prime mover for them ?
:)
The only technical problem I can think of is that Rust may not be available for all CPU architectures Linux supports, but this is just speculation on my part having done no research on the matter.
See: https://lwn.net/Articles/862018/
The issue of Rust's LLVM based compiler toolchain not targeting all CPU archs is intended to be solved by the gcc-rs project.
See: https://lwn.net/Articles/871283/
I think the approach the Rust for Linux project is taking is wise: Not about outright re-writes but more about focusing on those subsystems where Rust's intrinsic safety and security properties helps the most.
Your ordinary userspace apps use std (the standard library) which relies on the alloc crate, and that provides heap allocation which indeed panics if the allocation fails. Because it correctly guesses that your "clever" strategy to handle allocation failure actually isn't and will just triple fault anyway so it should cut to the chase.
The kernel obviously doesn't have std, and Rust for Linux implements its own alloc crate.
Linus' requirement that you can fail memory allocation just means all the calls in alloc that can actually allocate memory now return Result to indicate whether the allocation was successful. This isn't how you'd do things in userspace, but, this isn't userspace so fine.
We have a light / temporary fork of the Rust stdlib allocator with fallible allocation support: https://github.com/Rust-for-Linux/linux/tree/rust/rust/alloc
See e.g. https://github.com/Rust-for-Linux/linux/commit/487d7578bd036...
If the kernel is running out of memory, IMHO that's a bug. The kernel is ultimately responsible for memory management right? It needs to prioritize itself over everything else or the system is in trouble.
The limitation of that approach is that all internal interfaces (which includes most data structures) have to be C-compatible. You can get much of the safety benefits of Rust, but lose a lot of Rust's ergonomics.
Just like Android Linux compiles just fine with clang for the last five years or so, it now makes use of Rust.
https://source.android.com/setup/build/rust/building-rust-mo...
If upstream ever cares to support clang or Rust, that is another matter.
C rewritten in Rust is still very C-like, and requires refactorings that you can't do until it's all Rust.
BTW: There are two GCC-based Rust implementations in the works, so compatibility with exotic platforms is going to be solved.
I still think the project is really cool and worthwhile.
And then, in the longer term, someone ought to write a book about operating system implementation in Rust that ignores Linux interoperability and focuses on readability, maintainability by showing use of Rust's idiomatic style.
It’s just not an interesting or unique perspective, and pretty silly to suggest seriously.
Using an incremental approach at least gives us some benefits in the near future.
I'm not even saying a fresh start would be a bad idea. But incrementally replacing parts of Linux seems like a more promising approach, IMHO.
Thanks so much for sharing!
However, we have seen *many* memory-safety crashes in the drivers to talk to network interfaces. Those really are full of security vulnerabilities. The most prominent recent one was broadcom's wifi driver having heap buffer overflows allowing remote code execution by anyone within wifi range in 2019[0].
[0]: https://www.bleepingcomputer.com/news/security/broadcom-wifi...
Is it possible to write C code that has the safety properties of Rust code which provides the same functionality? Absolutely, in theory. But will you be able to accomplish this and then guarantee this safety and have the confidence that future code changes won't break those guarantees? That becomes exceedingly more difficult to do, nevermind the extra time and effort it would take. In most cases this would be prohibitively expensive, and you'd just get on with life hoping for the best and mitigating what you can.
In my view, one of the killer features of Rust is its ability to be easily ABI-compatible with C as needed, which I see as a pragmatic feature of Rust that acknowledges the messy software reality of our world and the fact that C (or any other language) isn't just going to be "replaced" by rewriting stuff everywhere, neither anytime soon nor probably ever in totality. This lets us more readily combine the great capabilities of Rust with the enormous volumes of useful code that already exist today without resorting to dogmatic approaches of always thinking we need to rewrite the latter. The "rewrite everything in Rust" idea is fun in an academic kind of way, but in the real world economics will prevent that from happening everywhere, in a similar way that economics usually prevents you from validating and providing certain safety guarantees about your C code.
Must be an awesome language to work on.
1. the toolchain is better. (cargo ala)
2. maintenance of the software is better. (static linked binaries, easier deploys)
3. memory safety.
4. resource consumption (cpu/ram)
when I don't need 4 I use golang (most cases) because of 1, 2, and 3.
and I choose tools (all other things being equal) written in rust for the same reasons.
Mozilla decided to create Rust after years of using C/C++. Why?
Because they spent a fortune fixing the same problems over and over again, and they realized that no matter how clever their developers were, those problems were always going to arise as long as they kept using C/C++.
Not because C/C++ is inherently bad, but because is more flexible than strictly needed in some ways.
Rust was designed to prevent many classes of problems.
If you want Linux to survive, it needs to be re-written from scratch, optimised in Rust and compatible with the existing Linux binaries or drivers.
This project might have a chance.
From: [0]
> TL;DR: I'm writing a Linux clone in Rust just for fun. It does NOT aim to replace the Linux kernel.
A certain someone thought that their hobby OS Minix clone won't be 'big or professional like GNU'.
It's time for 'change'.
How on earth have you come to that one?
Linux is going to survive. And no it does not need to be re-written from scratch in Rust. Someone lied to you at the church of rust.
My assumption is that all titles with Rust get blind upvotes, sure a new kernel is cool but would be more cool if is more then someone toy project, this should probably be posted and upvoted in Rust forums so it gets support from the fans and hopefully the Rust guys will decide what kernel should they focus on and stop promoting every student weekend project here.
Why do you think the Linux developers are bolting on Rust in the Linux kernel themselves? [0]
It has already been admitted.
[0] https://lore.kernel.org/lkml/20210704202756.29107-1-ojeda@ke...
A big problem in the Rust ecosystem is the lack of continued work and support on libraries (understandably, since many are new and aren't commercially supported).
For example, pyroute2 is still better for Linux admin that the equivalent Rust crates. Whilst the latter often fully support async, none of them cover all of the features of pyroute2 nor the easy-to-use API.
Hopefully things will improve over time, it's always a shame to see great projects be abandoned.
This is Hacker News, not Enterprise IT News.
Even Rocket https://github.com/SergioBenitez/Rocket seems to have greatly slowed development unfortunately.
https://github.com/seanmonstar/warp
https://github.com/tokio-rs/axum
re headcrab, https://github.com/headcrab-rs/headcrab/issues/132