mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-29 21:41:00 +08:00
152 lines
16 KiB
Markdown
152 lines
16 KiB
Markdown
|
[#]: subject: "A 10-minute guide to the Linux ABI"
|
||
|
[#]: via: "https://opensource.com/article/22/12/linux-abi"
|
||
|
[#]: author: "Alison Chaiken https://opensource.com/users/chaiken"
|
||
|
[#]: collector: "lkxed"
|
||
|
[#]: translator: " "
|
||
|
[#]: reviewer: " "
|
||
|
[#]: publisher: " "
|
||
|
[#]: url: " "
|
||
|
|
||
|
A 10-minute guide to the Linux ABI
|
||
|
======
|
||
|
|
||
|
Many Linux enthusiasts are familiar with Linus Torvalds' [famous admonition][1], "we don't break user space," but perhaps not everyone who recognizes the phrase is certain about what it means.
|
||
|
|
||
|
The "#1 rule" reminds developers about the stability of the applications' binary interface via which applications communicate with and configure the kernel. What follows is intended to familiarize readers with the concept of an ABI, describe why ABI stability matters, and discuss precisely what is included in Linux's stable ABI. The ongoing growth and evolution of Linux necessitate changes to the ABI, some of which have been controversial.
|
||
|
|
||
|
### What is an ABI?
|
||
|
|
||
|
ABI stands for Applications Binary Interface. One way to understand the concept of an ABI is to consider what it is not. Applications Programming Interfaces (APIs) are more familiar to many developers. Generally, the headers and documentation of libraries are considered to be their API, as are standards documents like those for [HTML5][2], for example. Programs that call into libraries or exchange string-formatted data must comply with the conventions described in the API or expect unwanted results.
|
||
|
|
||
|
ABIs are similar to APIs in that they govern the interpretation of commands and exchange of binary data. For C programs, the ABI generally comprises the return types and parameter lists of functions, the layout of structs, and the meaning, ordering, and range of enumerated types. The Linux kernel remains, as of 2022, almost entirely a C program, so it must adhere to these specifications.
|
||
|
|
||
|
"[The kernel syscall interface][3]" is described by [Section 2 of the Linux man pages][4] and includes the C versions of familiar functions like "mount" and "sync" that are callable from middleware applications. The binary layout of these functions is the first major part of Linux's ABI. In answer to the question, "What is in Linux's stable ABI?" many users and developers will respond with "the contents of sysfs (/sys) and procfs (/proc)." In fact, the [official Linux ABI documentation][5] concentrates mostly on these [virtual filesystems][6].
|
||
|
|
||
|
The preceding text focuses on how the Linux ABI is exercised by programs but fails to capture the equally important human aspect. As the figure below illustrates, the functionality of the ABI requires a joint, ongoing effort by the kernel community, C compilers (such as [GCC][7] or [clang][8]), the developers who create the userspace C library (most commonly [glibc][9]) that implements system calls, and binary applications, which much be laid out in accordance with the Executable and Linking Format ([ELF][10]).
|
||
|
|
||
|
![Cooperation within the development community][11]
|
||
|
|
||
|
### Why do we care about the ABI?
|
||
|
|
||
|
The Linux ABI stability guarantee that comes from Torvalds himself enables Linux distros and individual users to update the kernel independently of the operating system.
|
||
|
|
||
|
If Linux did not have a stable ABI, then every time the kernel needed patching to address a security problem, a large part of the operating system, if not the entirety, would need to be reinstalled. Obviously, the stability of the binary interface is a major contributing factor to Linux's usability and wide adoption.
|
||
|
|
||
|
![Terminal output][12]
|
||
|
|
||
|
As the second figure illustrates, both the kernel (in linux-libc-dev) and Glibc (in `libc6-dev`) provide bitmasks that define file permissions. Obviously the two sets of definitions must agree! The `apt` package manager identifies which software project provided each file. The potentially unstable part of Glibc's ABI is found in the `bits/` directory.
|
||
|
|
||
|
For the most part, the Linux ABI stability guarantee works just fine. In keeping with [Conway's Law][13], vexing technical issues that arise in the course of development most frequently occur due to misunderstandings or disagreements between different software development communities that contribute to Linux. The interface between communities is easy to envision via Linux package-manager metadata, as shown in the image above.
|
||
|
|
||
|
### Y2038: An example of an ABI break
|
||
|
|
||
|
The Linux ABI is best understood by considering the example of the ongoing, [slow-motion][14] "Y2038" ABI break. In January 2038, 32-bit time counters will roll over to all zeroes, just like the odometer of an older vehicle. January 2038 sounds far away, but assuredly many IoT devices sold in 2022 will still be operational. Mundane products like [smart electrical meters][15] and [smart parking systems][16] installed this year may or may not have 32-bit processor architectures and may or may not support software updates.
|
||
|
|
||
|
The Linux kernel has already moved to a 64-bit `time_t` opaque data type internally to represent later timepoints. The implication is that system calls like `time()` have already changed their function signature on 64-bit systems. The arduousness of these efforts is on ready display in kernel headers like [time_types.h][17], which includes new and "_old" versions of data structures.
|
||
|
|
||
|
![Odometer rolling over][18]
|
||
|
|
||
|
The Glibc project also [supports 64-bit time][19], so yay, we're done, right? Unfortunately, no, as a [discussion on the Debian mailing list][20] makes clear. Distros are faced with the unenviable choice of either providing two versions of all binary packages for 32-bit systems or two versions of installation media. In the latter case, users of 32-bit time will have to recompile their applications and reinstall. As always, proprietary applications will be a real headache.
|
||
|
|
||
|
### What precisely is in the Linux stable ABI anyway?
|
||
|
|
||
|
Understanding the stable ABI is a bit subtle. Consider that, while most of sysfs is stable ABI, the debug interfaces are guaranteed to be _un_stable since they expose kernel internals to userspace. In general, Linus Torvalds has pronounced that by "don't break userspace," he means to protect ordinary users who "just want it to work" rather than system programmers and kernel engineers, who should be able to read the kernel documentation and source code to figure out what has changed between releases. The distinction is illustrated in the figure below.
|
||
|
|
||
|
![Stability guarantee][21]
|
||
|
|
||
|
Ordinary users are unlikely to interact with unstable parts of the Linux ABI, but system programmers may do so inadvertently. All of sysfs (`/sys`) and procfs (`/proc`) are guaranteed stable except for `/sys/kernel/debug`.
|
||
|
|
||
|
But what about other binary interfaces that are userspace-visible, including miscellaneous ABI bits like device files in `/dev`, the kernel log file (readable with the `dmesg` command), filesystem metadata, or "bootargs" provided on the kernel "command line" that are visible in a bootloader like GRUB or u-boot? Naturally, "it depends."
|
||
|
|
||
|
### Mounting old filesystems
|
||
|
|
||
|
Next to observing a Linux system hang during the boot sequence, having a filesystem fail to mount is the greatest disappointment. If the filesystem resides on an SSD belonging to a paying customer, the matter is grave indeed. Surely a Linux filesystem that mounts with an old kernel version will still mount when the kernel is upgraded, right? Actually, "[it depends][22]."
|
||
|
|
||
|
In 2020 an aggrieved Linux developer [complained on the kernel's mailing list][23]:
|
||
|
|
||
|
> The kernel already accepted this as a valid mountable filesystem format, without a single error or warning of any kind, and has done so stably for years. . . . I was generally under the impression that mounting existing root filesystems fell under the scope of the kernel<->userspace or kernel<->existing-system boundary, as defined by what the kernel accepts and existing userspace has used successfully, and that upgrading the kernel should work with existing userspace and systems.
|
||
|
|
||
|
But there was a catch: The filesystems that failed to mount were created with a proprietary tool that relied on a flag that was defined but not used by the kernel. The flag did not appear in Linux's API header files or procfs/sysfs but was instead an [implementation detail][24]. Therefore, interpreting the flag in userspace code meant relying on "[undefined behavior][25]," a phrase that will make software developers almost universally shudder. When the kernel community improved its internal testing and started making new consistency checks, the "[man 2 mount][26]" system call suddenly began rejecting filesystems with the proprietary format. Since the format creator was decidedly a software developer, he got little sympathy from kernel filesystem maintainers.
|
||
|
|
||
|
![Construction sign reading crews working in trees][27]
|
||
|
|
||
|
### Threading the kernel dmesg log
|
||
|
|
||
|
Is the format of files in `/dev` guaranteed stable or not? The [command dmesg][28] reads from the file `/dev/kmsg`. In 2018, a developer [made output to dmesg threaded][29], enabling the kernel "to print a series of printk() messages to consoles without being disturbed by concurrent printk() from interrupts and/or other threads." Sounds excellent! Threading was made possible by adding a thread ID to each line of the `/dev/kmsg` output. Readers following closely will realize that the addition changed the ABI of `/dev/kmsg`, meaning that applications that parse that file needed to change too. Since many distros didn't compile their kernels with the new feature enabled, most users of `/bin/dmesg` won't have noticed, but the change broke the [GDB debugger][30]'s ability to read the kernel log.
|
||
|
|
||
|
Assuredly, astute readers will think users of GDB are out of luck because debuggers are developer tools. Actually, no, since the code that needed to be updated to support the new `/dev/kmsg` format was "[in-tree][31]," meaning part of the kernel's own Git source repository. The failure of programs within a single repo to work together is just an out-and-out bug for any sane project, and a [patch that made GDB work with threaded /dev/kmsg][32] was merged.
|
||
|
|
||
|
### What about BPF programs?
|
||
|
|
||
|
[BPF][33] is a powerful tool to monitor and even configure the running kernel dynamically. BPF's original purpose was to support on-the-fly network configuration by allowing sysadmins to modify packet filters from the command line instantly. [Alexei Starovoitov and others greatly extended BPF][34], giving it the power to trace arbitrary kernel functions. Tracing is clearly the domain of developers rather than ordinary users, so it is certainly not subject to any ABI guarantee (although the [bpf() system call][35] has the same stability promise as any other). On the other hand, BPF programs that create new functionality present the possibility of "[replacing kernel modules as the de-facto means of extending the kernel][36]." Kernel modules make devices, filesystems, crypto, networks, and the like work, and therefore clearly are a facility on which the "just want it to work" user relies. The problem arises that BFP programs have not traditionally been "in-tree" as most open-source kernel modules are. A proposal in spring 2022 to [provide support to the vast array of human interface devices (HIDs) like mice and keyboards via tiny BPF programs][37] rather than patches to device drivers brought the issue into sharp focus.
|
||
|
|
||
|
A rather heated discussion followed, but the issue was apparently settled by [Torvalds' comments at Open Source Summit][38]:
|
||
|
|
||
|
> He specified if you break 'real user space tools, that normal (non-kernel developers) users use,' then you need to fix it, regardless of whether it is using eBPF or not.
|
||
|
|
||
|
A consensus appears to be forming that developers who expect their BPF programs to withstand kernel updates [will need to submit them to an as-yet unspecified place in the kernel source repository][39]. Stay tuned to find out what policy the kernel community adopts regarding BPF and ABI stability.
|
||
|
|
||
|
### Conclusion
|
||
|
|
||
|
The kernel ABI stability guarantee applies to procfs, sysfs, and the system call interface, with important exceptions. When "in-tree" code or userspace applications are "broken" by kernel changes, the offending patches are typically quickly reverted. When proprietary code relies on kernel implementation details that are incidentally accessible from userspace, it is not protected and garners little sympathy when it breaks. When, as with Y2038, there is no way to avoid an ABI break, the transition is made as thoughtfully and methodically as possible. Newer features like BPF programs present as-yet-unanswered questions about where exactly the ABI-stability border lies.
|
||
|
|
||
|
##### Acknowledgments
|
||
|
|
||
|
Thanks to [Akkana Peck][40], [Sarah R. Newman][41], and [Luke S. Crawford][42] for their helpful comments on early versions of this material.
|
||
|
|
||
|
--------------------------------------------------------------------------------
|
||
|
|
||
|
via: https://opensource.com/article/22/12/linux-abi
|
||
|
|
||
|
作者:[Alison Chaiken][a]
|
||
|
选题:[lkxed][b]
|
||
|
译者:[译者ID](https://github.com/译者ID)
|
||
|
校对:[校对者ID](https://github.com/校对者ID)
|
||
|
|
||
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||
|
|
||
|
[a]: https://opensource.com/users/chaiken
|
||
|
[b]: https://github.com/lkxed
|
||
|
[1]: https://lkml.org/lkml/2018/12/22/232
|
||
|
[2]: https://www.w3.org/TR/2014/REC-html5-20141028/
|
||
|
[3]: https://www.kernel.org/doc/html/v6.0/admin-guide/abi-stable.html#the-kernel-syscall-interface
|
||
|
[4]: https://www.man7.org/linux/man-pages/dir_section_2.html
|
||
|
[5]: https://www.kernel.org/doc/html/v6.0/admin-guide/abi.html
|
||
|
[6]: https://opensource.com/article/19/3/virtual-filesystems-linux
|
||
|
[7]: https://gcc.gnu.org/
|
||
|
[8]: https://clang.llvm.org/get_started.html
|
||
|
[9]: https://www.gnu.org/software/libc/
|
||
|
[10]: https://www.man7.org/linux/man-pages/man5/elf.5.html
|
||
|
[11]: https://opensource.com/sites/default/files/2022-11/1cooperation.png
|
||
|
[12]: https://opensource.com/sites/default/files/2022-12/better_apt-file-find_ABI-boundary.png
|
||
|
[13]: https://en.wikipedia.org/wiki/Conway's_law
|
||
|
[14]: https://www.phoronix.com/news/MTc2Mjg
|
||
|
[15]: https://www.lfenergy.org/projects/super-advanced-meter-sam/
|
||
|
[16]: https://www.ncbi.nlm.nih.gov/pmc/articles/PMC7506899/
|
||
|
[17]: https://github.com/torvalds/linux/blob/master/include/uapi/linux/time_types.h
|
||
|
[18]: https://opensource.com/sites/default/files/2022-11/3speedometerrollingover_0.jpg
|
||
|
[19]: https://www.phoronix.com/scan.php?page=news_item&px=Glibc-More-Y2038-Work
|
||
|
[20]: https://groups.google.com/g/linux.debian.ports.arm/c/_KBFSz4YRZs
|
||
|
[21]: https://opensource.com/sites/default/files/2022-11/4stability.png
|
||
|
[22]: https://lwn.net/Articles/833696/
|
||
|
[23]: https://lwn.net/ml/linux-kernel/20201006050306.GA8098@localhost/
|
||
|
[24]: https://en.wikipedia.org/wiki/Encapsulation_(computer_programming)
|
||
|
[25]: https://en.wikipedia.org/wiki/Undefined_behavior
|
||
|
[26]: https://www.man7.org/linux/man-pages/man2/mount.2.html
|
||
|
[27]: https://opensource.com/sites/default/files/2022-11/5crewworkingintrees.jpg
|
||
|
[28]: https://www.man7.org/linux/man-pages/man1/dmesg.1.html
|
||
|
[29]: https://lkml.org/lkml/2018/11/24/180
|
||
|
[30]: https://sourceware.org/gdb/current/onlinedocs/gdb/
|
||
|
[31]: https://unix.stackexchange.com/questions/208638/linux-kernel-meaning-of-source-tree-in-tree-and-out-of-tree
|
||
|
[32]: https://lore.kernel.org/all/20191011142500.2339-1-joel.colledge@linbit.com/
|
||
|
[33]: https://opensource.com/article/19/8/introduction-bpftrace
|
||
|
[34]: https://lwn.net/Articles/740157/
|
||
|
[35]: https://www.man7.org/linux/man-pages/man2/bpf.2.html
|
||
|
[36]: https://lwn.net/Articles/909095/
|
||
|
[37]: https://lwn.net/ml/ksummit-discuss/CAO-hwJJxCteD_BHZTeqQ1f7gWOHoj+05qP8bmFsRYVfMc_3FxQ@mail.gmail.com/
|
||
|
[38]: https://lwn.net/ml/ksummit-discuss/20220621110514.6ef174d0@rorschach.local.home/
|
||
|
[39]: https://lwn.net/ml/ksummit-discuss/20220616125128.68151432@gandalf.local.home/
|
||
|
[40]: https://shallowsky.com/blog/
|
||
|
[41]: https://www.socallinuxexpo.org/scale/19x/presentations/live-patching-down-trenches-view
|
||
|
[42]: https://www.amazon.com/Book-Xen-Practical-System-Administrator/dp/1593271867
|