Linux kernel interfaces: Difference between revisions

Content deleted Content added
No edit summary
m Bot: http → https
 
(238 intermediate revisions by 75 users not shown)
Line 1:
{{Short description|Linux kernel APIs and ABIs}}
[[File:SUS History.svg|thumb|250px|right|Chronological hierarchy of relationships of various UNIX API standards that ultimately gave rise to [[POSIX|POSIX.1-2008]]/[[Single UNIX Specification|SUSv4]]. Solid lines indicate direct descent, whereas dashed lines indicate a case where either 1) one standard influenced another 2) one standard was incorporated into another, or 3) one standard deferred to another.]]
{{essay-like|date=July 2014}}
{{Portal|Linux}}
The Linux kernel provides several interfaces to user-space applications that are used for different purposes and that have different properties by design.
 
[[File:Linux kernel interfaces.svg|thumb|upright=1.5|Linux API, Linux ABI, and in-kernel APIs and ABIs]]
There are two types of [[application programming interface]] (API) in the [[Linux kernel]] that are not to be confused: the "kernel user-space" API and the "kernel-internal" API.
 
The [[Linux kernel]] provides multiple interfaces to [[User space and kernel space|user-space and kernel-mode]] code. The interfaces can be classified as either [[application programming interface]] (API) or [[application binary interface]] (ABI), and they can be classified as either kernel–user space or kernel-internal.
== Kernel user-space API ==
[[File:Linux kernel API.svg|thumb|300px|The '''Linux kernel API''' versus the [[POSIX]] API.]]
The kernel user-space API is the API that allows programs in user space to access system resources and services of the Linux kernel. This API, by choice, never breaks. The [[POSIX]] API is only a small part of the Linux kernel API.
 
== {{Anchor|Linux API}}Linux API ==
Much available [[free and open-source software]] is written for the POSIX API. But since the last POSIX version is from 2008, and since so much more development is going on with the Linux kernel as compared to the other POSIX-compatible kernels, various software has been developed that target solely the Linux kernel user-space API. Well-known examples are [[systemd]] and [[Weston (software)|Weston]].
[[File:Linux API.svg|thumb|upright=1.5|The '''Linux API''' is composed out of the system call interface of the Linux kernel, the [[GNU C Library]] (by [[GNU]]), [[cgroups|libcgroup]],<ref name="libcgroup">{{cite web|url=https://www.freedesktop.org/wiki/Software/systemd/ControlGroupInterface/|title=ControlGroupInterface|publisher=[[freedesktop.org]]}}</ref> [[Direct Rendering Manager|libdrm]], [[Advanced Linux Sound Architecture|libalsa]] and [[evdev|libevdev]]<ref name="libevdev">{{cite web |url=https://www.freedesktop.org/wiki/Software/libevdev/ |title=libevdev |publisher=[[freedesktop.org]]}}</ref> (by [[freedesktop.org]]).]]
[[File:Linux kernel API.svg|thumb|upright=1.5|Linux API vs. [[POSIX]] API]]
 
The Linux API includes the kernel&ndash;user space API, which allows code in user space to access system resources and services of the Linux kernel.<ref>{{cite web
People such as [[Lennart Poettering]] openly advocate writing software solely for the Linux kernel user-space API instead of POSIX, where this offers advantages.
| url = https://www.linux.it/~rubini/docs/ksys/ksys.html
| title = Kernel System Calls
| date = 2006-11-02 | access-date = 2014-11-11
| author = Alessandro Rubini | website = linux.it
}}</ref> It is composed of the system call interface of the Linux kernel and the subroutines in the [[C standard library]]. The focus of the development of the Linux API has been to provide the ''usable features'' of the specifications defined in [[POSIX]] in a way which is reasonably compatible, robust and performant, and to provide additional useful features not defined in POSIX, just as the kernel&ndash;user space APIs of other systems implementing the POSIX API also provide additional features not defined in POSIX.
 
The Linux API, by choice, has been kept stable over the decades through a policy of not introducing breaking changes; this stability guarantees the portability of [[source code]].<ref>{{cite web
== In-kernel API ==
| url = https://lkml.org/lkml/2012/12/23/75
By choice, the Linux kernel has no ''stable'' in-kernel API (a.k.a. [[Application binary interface|Binary Kernel Interface]]).<ref>{{cite web |url=https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/stable_api_nonsense.txt |title=The Linux Kernel Driver Interface}}</ref>
| title = Re: [Regression w/ patch] Media commit causes user space to misbahave (was: Re: Linux 3.8-rc1)
| date = 2012-12-23 | access-date = 2014-08-26
| author = Linus Torvalds | publisher = [[Linux kernel mailing list]]
| quote = If a change results in user programs breaking, it's a bug in the kernel. We never EVER blame the user programs.
}}</ref> At the same time, Linux kernel developers have historically been conservative and meticulous about introducing new system calls.{{citation needed|date=November 2014}}
 
Much available [[free and open-source software]] is written for the POSIX API. Since so much more development flows into the Linux kernel as compared to the other POSIX-compliant combinations of kernel and C standard library,{{citation needed|date=July 2014}} the Linux kernel and its API have been augmented with additional features. Programming for the full Linux API, rather than just the POSIX API, may provide advantages in cases where those additional features are useful. Well-known current examples are [[udev]], [[systemd]] and [[Weston (software)|Weston]].<ref>{{cite web |url=https://lwn.net/Articles/430598/ |title=Choosing between portability and innovation |publisher=[[LWN.net]] |date=2011-03-02}}</ref> People such as [[Lennart Poettering]] openly advocate to prefer the Linux API over the POSIX API, where this offers advantages.<ref name="fosdem1">{{cite web|url=https://archive.fosdem.org/2011/interview/lennart-poettering.html |publisher=fosdem.org |title=Interview: Lennart Poettering - Lennart Poettering will give a talk about "Systemd: beyond init" at FOSDEM 2011.|year=2011 |access-date=2014-06-16 |quote=''In fact, the way I see things the '''[[Linux kernel API|Linux API]]''' has been taking the role of the '''POSIX API''' and Linux is the focal point of all Free Software development. Due to that I can only recommend developers to try to hack with only Linux in mind and experience the freedom and the opportunities this offers you. So, get yourself a copy of [[The Linux Programming Interface]], ignore everything it says about [[POSIX]] compatibility and hack away your amazing Linux software. It's quite relieving!''}}</ref>
 
At [[FOSDEM]] 2016, [[Michael Kerrisk]] explained some of the perceived issues with the Linux kernel's user-space API, describing that it contains multiple design errors by being non-extensible, unmaintainable, overly complex, of limited purpose, in violation of standards, and inconsistent. Most of those mistakes cannot be fixed because doing so would break the ABI that the kernel presents to the user space.<ref>{{cite web|url=https://archive.fosdem.org/2016/schedule/event/design_linux_kernel_api/|title=How to design a Linux kernel API|author=Michael Kerrisk|author-link=Michael Kerrisk|date=2016-01-31|access-date=2016-02-04}}</ref>
 
=== {{Anchor|System Call Interface|SCI}} System call interface of the Linux kernel===
The ''system call interface'' of a kernel is the set of all implemented and available [[system calls]] in a kernel. In the Linux kernel, various subsystems, such as the [[Direct Rendering Manager]] (DRM), define their own system calls, all of which are part of the system call interface.
 
Various issues with the organization of the Linux kernel system calls are being publicly discussed. Issues have been pointed out by Andy Lutomirski, [[Michael Kerrisk]] and others.<ref>{{cite web |url=https://www.linux-magazine.com/Issues/2014/165/Kernel-News |title=System Call Organization}}</ref><ref>{{cite web |url=https://lkml.org/lkml/2014/2/27/571 |title= Making a universal list of syscalls? |publisher=[[LKML]] |date=2014-02-27}}</ref><ref>{{cite web |url=https://lwn.net/Articles/585415/ |title=Flags as a system call API design pattern |publisher=[[LWN.net]] |date=2014-02-12}}</ref><ref>{{cite web |url=https://lwn.net/Articles/446528/ |title=On vsyscalls and the vDSO |publisher=[[LWN.net]] |date=2011-06-08}}</ref>
 
=== The C standard library ===
[[File:Linux kernel System Call Interface and glibc.svg|thumb|upright=1.5|The [[GNU C Library]] is a wrapper around the Linux kernel system call interface.]]
 
A [[C standard library]] for Linux includes wrappers around the system calls of the Linux kernel; the combination of the Linux kernel system call interface and a C standard library is what builds the Linux API. Some popular implementations of the C standard library are
 
* [[glibc]]
* [[uClibc]]
* [[klibc]]
* [[Newlib]]
* [[musl]]
* [[dietlibc]]
* [[Bionic (software)|libbionic]] and [[Hybris (software)|libhybris]]
 
Although the landscape is shifting, amongst these options, glibc remains the most popular implementation, to the point of many treating it as the default and the term equivalent to libc.
 
==== Additions to POSIX ====
As in other [[Unix-like]] systems, additional capabilities of the Linux kernel exist that are not part of POSIX:
 
* [[cgroups]] subsystem, the system calls it introduces and libcgroup<ref name="libcgroup" />
* The system calls of the [[Direct Rendering Manager]], especially the driver-private ioctls for the command submission, are ''not'' part of the POSIX specifications.
* [[Advanced Linux Sound Architecture]] ''could'' set system calls, which are not part of the POSIX specifications
* The system calls <code>[[futex]]</code> (fast userspace mutex), <code>[[epoll]]</code>, <code>[[splice (system call)|splice]]</code>, <code>[[dnotify]]</code>, <code>[[fanotify]]</code>, and <code>[[inotify]]</code> have been exclusive to the Linux kernel so far.
* The system call <code>[[getrandom]]</code> was introduced in version 3.17 of the [[Linux kernel mainline]]<ref>{{cite web |url=https://lkml.org/lkml/2014/7/17/145 |title=[PATCH, RFC] random: introduce getrandom(2) system call |publisher=[[Linux kernel mailing list|LKML]] |date=2014-07-17}}</ref>
* <code>[[memfd]]</code> was proposed by the [[kdbus]] developers<ref>{{cite web |url=https://github.com/gregkh/kdbus/blob/master/memfd.c |archive-url=https://archive.today/20140422172420/https://github.com/gregkh/kdbus/blob/master/memfd.c |url-status=dead |archive-date=2014-04-22 |title=memfd.c |website=[[GitHub]] }}</ref>
** <code>[[memfd_create]]</code> was merged into the Linux kernel mainline in kernel version 3.17
* <code>[[readahead]]</code> initiates a file "read-ahead" into page cache
 
[[Direct Rendering Manager|DRM]] has been paramount for the development and implementations of well-defined and performant [[Free and open-source graphics device driver#Software architecture|free and open-source graphics device drivers]] without which no rendering acceleration would be available at all, only the 2D drivers would be available in the [[X.Org Server]]. DRM was developed for Linux, and since has been ported to other operating systems as well.<ref>{{cite web |url=https://www.phoronix.com/scan.php?page=news_item&px=MTYzNTQ |title=NetBSD 7.0 Will Finally Have DRM/KMS Drivers |publisher=[[Phoronix]] |date=2014-03-19}}</ref>
 
=== Further libraries ===
* libdrm (for [[Direct Rendering Manager]])
* libnl (The libnl suite is a collection of libraries providing APIs to netlink protocol based Linux kernel interfaces.)
* libevdev (for [[evdev]])
* libasound ([[Advanced Linux Sound Architecture]])
 
== Linux ABI ==
{{Expert needed|free and open-source software|ex2=software|ex3=computing|reason=This section mostly ignores the kernel-userspace ABI (that is very real and important thing) and jumps into userspace-to-userspace APIs|date=February 2018}}
[[File:Linux API and Linux ABI.svg|thumb|upright=1.5|The Linux API and ABI ]]
{{Main article|x32 ABI|Linux Standard Base}}
 
The Linux ABI is a kernel–user space ABI. As ABI is a [[machine code]] interface, the Linux ABI is bound to the [[instruction set]]. Defining a useful ABI and keeping it stable is less the responsibility of the Linux kernel developers or of the developers of the GNU C Library, and more the task for [[Linux distribution]]s and [[independent software vendor]]s (ISVs) who wish to sell and provide support for their proprietary software as binaries only for such a single Linux ABI, as opposed to supporting multiple Linux ABIs.
 
An ABI has to be defined for every instruction set, such as [[x86]], [[x86-64]], [[MIPS architecture|MIPS]], [[ARMv7-A]] (32-Bit), [[ARMv8-A]] (64-Bit), etc. with the [[endianness]], if both are supported.
 
It should be able to compile the software with different compilers against the definitions specified in the ABI and achieve full binary compatibility. Compilers that are [[free and open-source software]] are e.g. [[GNU Compiler Collection]], [[LLVM]]/[[Clang]].
 
== In-kernel APIs ==
Many kernel-internal APIs exist, allowing kernel subsystems to interface with one another. These are being kept fairly stable, but there is no guarantee for stability. A kernel-internal API can be changed when such a need is indicated by new research or insights; all necessary modifications and testing have to be done by the author.
 
The Linux kernel is a monolithic kernel, hence device drivers are kernel components. To ease the burden of companies maintaining their (proprietary) device drivers outside of the main kernel tree, stable APIs for the device drivers have been repeatedly requested. The Linux kernel developers have repeatedly denied guaranteeing stable in-kernel APIs for device drivers. Guaranteeing such would have faltered the development of the Linux kernel in the past and would still in the future and, due to the nature of free and open-source software, are not necessary. Ergo, by choice, the Linux kernel has no ''stable'' in-kernel API.<ref>{{cite web|url=https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/stable-api-nonsense.rst |title=The Linux Kernel Driver Interface}}</ref>
 
== In-kernel ABIs ==
Since there are no stable in-kernel APIs, there cannot be stable in-kernel ABIs.<ref>{{cite web|url=https://abi-laboratory.pro/tracker/timeline/linux/ | title=Analysis of ABI changes in the Linux kernel |publisher=Andrey Ponomarenko's ABI laboratory|date=2016-03-15}}</ref>
 
== Abstraction APIs ==
[[File:Linux kernel and OpenGL video games.svg|thumb|OpenGL is indeed an abstraction API to make use of diverse GPUs of multiple vendors without the need to program for each one specifically.]]
[[File:Division of labor cpu and gpu.svg|thumb|But the implementation of the OpenGL-specification is executed on the CPU in the context of the running operating system. One design goal of [[Vulkan (API)|Vulkan]] was to make the "graphics driver", i.e. the implementation of the graphics API, do less.]]
For many use cases, the Linux API is considered too low-level, so APIs of higher abstraction must be used. Higher-level APIs must be implemeted on top of lower-level APIs. Examples:
 
* Implementation of the [[OpenGL]] and [[Vulkan (API)|Vulkan]] specifications in proprietary Linux graphics drivers and the free and open-source implementation in [[Mesa (computer graphics)|Mesa]].
* Implementation of the [[OpenAL]] specification.
* [[Simple DirectMedia Layer]]: abstraction API for input/sound/etc. available for many operating systems.
* [[Simple and Fast Multimedia Library]]: like above.
 
== See also ==
{{Portal|Linux}}
* [[C standard library]] – the library for the C programming language made available across implementations
* {{Annotated link|File descriptor}}
** [[GNU C Library]] – the GNU Project's implementation of the C standard library
* {{Annotated link|Hybris (software)}}
* [[system call]] – is a function to facilitate programs to requests services from the kernel
* ''{{Annotated link|The Linux Programming Interface}}''
** [[netlink]] – Netlink was added by Alan Cox during Linux kernel 1.3 development as a character driver interface to provide multiple kernel and user-space bidirectional communications links. Then, Alexey Kuznetsov extended it during Linux kernel 2.1 development to provide a flexible and extensible messaging interface to the new advanced routing infrastructure. Since then, Netlink sockets have become one of the main interfaces that kernel subsystems provide to user-space applications in Linux.
* {{Annotated link|netlink}}
** [[mmap]] – a system call
* {{Annotated link|Semaphore (programming)}}
** fanotify – a system call
** [[dnotify]] - a{{Annotated link|system call}}
* {{Annotated link|Windows API}}
** [[inotify]] – a system call
* {{Annotated link|windows.h}}
** [[netlink]] – socket family used for IPC between kernel and user space processes, designed as the successor of [[ioctl]]
* {{Annotated link|Wine (software)}}
* [[Windows API]] – article on various API available on Microsoft Windows operating systems
** [[windows.h]] – header file for the [[C (programming language)|C programming language]] which contains declarations for ''all'' of the functions in the Windows API
* [[Wine (software)|Wine]] – a compatibility layer between Linux and programs written for Microsoft Windows
 
== References ==
{{reflistReflist|30em}}
 
== External links ==
*[https://www.kernel.org/doc/html/v5.0/core-api/kernel-api.html The Linux Kernel API 5.0], [https://www.kernel.org/doc/html/v5.0/core-api/mm-api.html Memory Management APIs 5.0] (new [[Sphinx (documentation generator)|sphinx]] format)
* [http://www.gnugeneration.com/books/linux/2.6.20/kernel-api/ The API of Linux kernel 2.6.20] – sadly no current version available
*[https://web.archive.org/web/20070227215533/http://www.gnugeneration.com/books/linux/2.6.20/kernel-api/ The API of Linux kernel 2.6.20] and [https://www.kernel.org/doc/htmldocs/kernel-api/ 4.12] (in deprecated htmldocs format)
* [http://man7.org/tlpi/ The Linux Programming Interface]
*[https://abi-laboratory.pro/?view=timeline&l=linux API/ABI changes review for Linux]
* [http://www.makelinux.net/kernel_map Interactive Linux kernel map] with main API functions and structures
* [https://man7.org/tlpi/ The Linux Programming Interface] book, [https://man7.org/tlpi/api_changes/ Linux and ''glibc'' API changes] since [[The Linux Programming Interface]] was released in 2010
* [http://www.makelinux.net/ldd3 Linux Device Drivers] by Jonathan Corbet, Greg Kroah-Hartman and Alessandro Rubini, 3rd edition
*{{usurped|1=[https://archive.today/20121220032539/http://www.makelinux.net/kernel_map/ Interactive Linux kernel map]}} with main API functions and structures, {{usurped|1=[https://web.archive.org/web/20180827040646/http://www.makelinux.net/kernel_map/LKM.pdf PDF]}} version
* [http://isis.poly.edu/kulesh/stuff/src/klist/ Linux Kernel Linked List Explained]
* {{usurped|1=[https://archive.today/20130221193824/http://www.makelinux.net/ldd3 Linux Device Drivers]}} by Jonathan Corbet, Greg Kroah-Hartman and Alessandro Rubini, 3rd edition
* [http://isis.poly.edu/kulesh/stuff/src/klist/ Linux Kernel Linked List Explained] {{Webarchive|url=https://web.archive.org/web/20090925130610/http://isis.poly.edu/kulesh/stuff/src/klist/ |date=2009-09-25 }}
 
{{Linux kernel}}
{{Operating system}}
 
{{DEFAULTSORT:Linux Kernel Api}}
[[Category:Interfaces of the Linux kernel| ]]
 
{{Linux-stub}}