|
|
Subscribe / Log in / New account

An alternative TTY layer

By Jonathan Corbet
April 27, 2017
The Linux kernel is highly scalable but, while it runs nicely on the world's largest computers, it is not an entirely comfortable fit on the smallest. The difficulties involved in running Linux on machines with 1MB or less of memory have left an opening for other operating systems, such as Zephyr, with lower memory needs. Some developers have not given up on scaling Linux to the smallest computers, but the approaches they have to take have always been a bit of a hard sell with the rest of the development community. Nicolas Pitre's minitty patch set is a case in point.

The kernel's terminal (TTY) layer supports a long list of hardware and no end of features. It is a complex subsystem that must support decades of Unix and Linux history; core-kernel developer Ingo Molnar once confessed that the TTY code is "one of the very few pieces of kernel code that scares the hell out of me". It is also quite large, weighing in at about 46KB of compiled code without the low-level hardware drivers. 46KB is not a lot of memory on a typical desktop system, or even on a mobile handset but, in situations where every byte counts, it hurts.

When Pitre set out to find a way to slim down the TTY subsystem for small systems, he ran into some problems. The TTY code has a multi-layer design to give it the maximum amount of flexibility; each layer adds overhead. Line disciplines — modules that manage the low-level protocols used on serial lines — can be plugged in and out, and the kernel supports about two dozen of them. The line disciplines themselves are easy to leave out, but the infrastructure that allows them to be modular is not optional. The TTY layer is designed to support pseudoterminals, and it is able to support dozens of ports running at full speed — an important feature in the early days of Linux. Pitre concluded that there is no way to make this infrastructure optional without worsening performance or functionality for those who need it.

So, instead, he went and wrote an entirely new TTY subsystem called "minitty". About the only thing it can do is move data over a hardware serial port. There are no pseudoterminals, no choice of line disciplines, no job control, and not much of anything else. What it does provide is the ability to get debugging information out of the system via a serial port or to run simple login sessions. Minitty shares the TTY layer's low-level hardware drivers, though, and it provides a standard (if limited) interface to user space. And, importantly, it does that in 8KB of memory — about 1/6 of what is required by the standard TTY layer.

Minitty seems like a clear win for extremely small Linux deployments, but it quickly ran into trouble on the mailing list. Kernel developers have a strong aversion to duplicated subsystems, and for a number of good reasons. This kind of duplication forces choices onto distributors and users that they may not be in a position to make. It can split development effort between multiple competing implementations, bugs present in both subsystems are often only fixed in one, and maintenance can be complicated if changes to one subsystem can break the other. So it is not entirely surprising that minitty has drawn some unfavorable responses.

Greg Kroah-Hartman, the current maintainer of the TTY layer, described it as "a horrid thing to do" and advised Pitre to work with the existing code instead:

So please, take what we have, refactor, and carve things up so that the _same_ code paths are being used for both "big and little" tty layers. That way _everyone_ benefits, no need to have totally separate code paths, and totally different files that different people maintain.

Other developers also made their opposition to a separate TTY layer known. Pitre is not so easily discouraged, though. He reiterated that the TTY layer simply cannot be shrunk to the same degree:

Let's be honest here: the existing code _could_ possibly be reduced of course. That would require a lot of efforts to gain 50% reduction maybe? What I'm looking at with my proposal here is a 6x reduction factor and I'm still not done with it. There is no way I could do that with the existing code.

Most of his message (which is worth reading in its entirety) was devoted to explaining why he thinks a tiny TTY layer is so important. Linux is simply not a viable kernel for the smallest systems without the ability to make it as small as possible. But there is a great advantage to using Linux in such settings. Linux is well maintained and well debugged; security issues are fixed (relatively) quickly there. If a target system is running Linux, developers can write and debug their code on an ordinary desktop Linux system and, with a bit of care, expect it to work unmodified on the target system. All of the existing Linux-based development tools will be available, and existing Linux developers already know how to develop for such targets.

The ability to shrink the kernel in this way is, according to Pitre, a crucial part of making Linux successful in the smallest Internet-of-things settings. A smaller TTY layer is only the beginning; he would eventually like to come up with miniaturized versions of many other kernel subsystems as well. Accommodating these mini-subsystems will require a bit of a change to the normal kernel development model, he said, but the rewards will be more than worth the cost.

At least part of this message seems to have gotten through; Kroah-Hartman responded to the most recent posting by saying: "I agree with your goal of getting Linux running on these very tiny chips, I want that to happen too". If minitty can find its way to acceptance, that may eventually open the door to other tinification efforts; one should remember that this is the kernel community we're talking about, though, so expect a lengthy discussion for each proposal. Getting this kind of work merged will never be easy but, just maybe, the community will be able to accept enough of it to help ensure that the kernel is competitive in the smallest of deployments.

Index entries for this article
KernelEmbedded systems
KernelTTY layer


to post comments

An alternative TTY layer

Posted Apr 27, 2017 20:06 UTC (Thu) by post-factum (subscriber, #53836) [Link] (6 responses)

Indeed, duplicating the code must be a no-no.

But since current TTY subsystem is a huge mess, it worth throwing it out, replacing with something equivalent but properly architected.

If such an effort is started already, why stopping with minimal implementation? If designed properly, optional layers might be disabled if necessary.

An alternative TTY layer

Posted Apr 27, 2017 22:52 UTC (Thu) by Cyberax (✭ supporter ✭, #52523) [Link] (4 responses)

TTY subsystem is hairy but not excessively badly designed. It just has the usual abstraction overhead penalty.

An alternative TTY layer

Posted Apr 28, 2017 6:49 UTC (Fri) by josh (subscriber, #17465) [Link] (3 responses)

It does, however, have the problem of an extensive and under-specified userspace semantics, with no comprehensive test suite.

The last time someone tried to significantly overhaul the TTY layer, they ran into obscure issues with bits of the interface that only emacs used, and almost nothing else.

An alternative TTY layer

Posted Apr 28, 2017 6:51 UTC (Fri) by Cyberax (✭ supporter ✭, #52523) [Link] (1 responses)

But you don't need all the legacy crap for mini-interfaces. Small devices are not likely to run emacs - you're essentially are creating a NEW userspace ABI.

An alternative TTY layer

Posted Apr 28, 2017 22:06 UTC (Fri) by nix (subscriber, #2304) [Link]

Well, indeed. That's why a tiny reimplementation for those small systems only is preferable to uglifying the existing one in an attempt to make all the abstraction layers optional. Throwing out the TTY layer wholesale and replacing it with something "properly architected" (without any evidence that what we have now is poorly architected for its intended use case, which does not include 1MiB RAM IoT boxes), as the original commenter suggested, is quite different.

(A better API without all the horror shows of the existing one around job control, signal handling, ioctls etc would be nice too, but it sounds like minitty just throws most of that out since none of it is needed for its use case -- and for any other use case, the better API would have to coexist with the horrible existing one that everything uses in any case, and because that API is so ancient and widely used there really could never be a sunset period for it -- like it or not, we're stuck with SIOC* forever.)

An alternative TTY layer

Posted May 11, 2017 1:45 UTC (Thu) by geek (guest, #45074) [Link]

OK, who's the emacs maintainer then?

An alternative TTY layer

Posted Apr 29, 2017 1:58 UTC (Sat) by flussence (guest, #85566) [Link]

I wonder if cleaning up the TTY layer would open a path to make the framebuffer console layer less awful. Its emulation of a 1980s dumb terminal—including the redraw speed of one—is a little too accurate for my tastes. This is only getting worse as screen resolutions increase.

An alternative TTY layer

Posted Apr 27, 2017 21:13 UTC (Thu) by mm7323 (subscriber, #87386) [Link] (5 responses)

So Pitre honourably strives to make new mini implementations of various Linux subsystems with the goal of shrinking the kernel to work on tiny IoT devices.

Isn't the logical conclusion of such work essentially a _new_ kernel with some interfaces compatible with the Linux kernel (syscall, drivers, netlink etc...).

Wouldn't it be better to take an existing hardened RTOS or small kernel and add Linux interface compatibility? Doesn't this just point to the unsuitability of the Linux kernel to tiny IoT devices?

An alternative TTY layer

Posted Apr 27, 2017 22:19 UTC (Thu) by jkingweb (subscriber, #113039) [Link] (1 responses)

> Isn't the logical conclusion of such work essentially a _new_ kernel with some interfaces compatible with the Linux kernel (syscall, drivers, netlink etc...).

That's certainly what it sounds like to me. I don't see how you you benefit from active maintenance of the kernel if you throw huge chunks of it away.

An alternative TTY layer

Posted Apr 27, 2017 22:54 UTC (Thu) by Cyberax (✭ supporter ✭, #52523) [Link]

It's not all-or-nothing. For example, you might want a couple of full-scale feature-rich subsystems and to get them you just need to throw a switch in the config file. With a separate OS you'll have to actually reimplement the full-scale systems (with all the resulting bugs and issues).

An alternative TTY layer

Posted Apr 28, 2017 11:41 UTC (Fri) by roblucid (guest, #48964) [Link] (1 responses)

That is IMHO sophistry. The same argument for an emulation layer, could have been used in the 90's for multi-cpu large memory systems. Who runs Minix now? Linux would have been a dead end, without scaling efforts, even in mobile phones.

In the long run, who knows what parts of the kernel will become irrelevant in some deployments?

Many people may not see tinification effort benefits, but making software more adaptable opens up new possibilities and can drive growth as it's deployed more widely, as well as enhancing survival characteristics.

An alternative TTY layer

Posted Apr 29, 2017 17:26 UTC (Sat) by flussence (guest, #85566) [Link]

>Who runs Minix now?
Intel's hardware spyware runs on it, supposedly: https://mail.coreboot.org/pipermail/coreboot/2017-April/0...

An alternative TTY layer

Posted Apr 30, 2017 9:04 UTC (Sun) by linusw (subscriber, #40300) [Link]

I think the ambition is to reuse the major parts, especially all device drivers, MMU and other arch code and the system call interface and just shrink down the subsystems that actually are just too bloated.

Of course it is possible to fork, or maintain a separate kernel, but history tell us it is not a good idea.

An alternative TTY layer

Posted Jun 27, 2017 22:49 UTC (Tue) by gavinsmith (guest, #117044) [Link] (1 responses)

The impression I get from this article is that the author of the new subsystem does not want to put the effort into understanding and changing the existing TTY code, because it is large and complicated. That is understandable. It's also understandable that those responsible for long-term maintenance of the Linux kernel do not want to maintain the new code in addition to the existing TTY code. The two have different priorities: one the narrow task of providing a simplified TTY interface for low-memory computers, the other the wider task of long-term stability and maintainability of all the TTY interface code.

If it is really so vital that these low-memory systems are supported, then I expect that someone will do the work to implement the new functionality in a way that works well with the existing code, not increasing the total complexity as much as a completely new and separate subsystem would.

An alternative TTY layer

Posted Jun 28, 2017 17:30 UTC (Wed) by corbet (editor, #1) [Link]

That is not a correct impression and I apologize if the article suggested that. Nico understands the tty subsystem very well, but does not believe it can be made small without impacting other use cases.


Copyright © 2017, Eklektix, Inc.
This article may be redistributed under the terms of the Creative Commons CC BY-SA 4.0 license
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds