162306a36Sopenharmony_ciTainted kernels
262306a36Sopenharmony_ci---------------
362306a36Sopenharmony_ci
462306a36Sopenharmony_ciThe kernel will mark itself as 'tainted' when something occurs that might be
562306a36Sopenharmony_cirelevant later when investigating problems. Don't worry too much about this,
662306a36Sopenharmony_cimost of the time it's not a problem to run a tainted kernel; the information is
762306a36Sopenharmony_cimainly of interest once someone wants to investigate some problem, as its real
862306a36Sopenharmony_cicause might be the event that got the kernel tainted. That's why bug reports
962306a36Sopenharmony_cifrom tainted kernels will often be ignored by developers, hence try to reproduce
1062306a36Sopenharmony_ciproblems with an untainted kernel.
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ciNote the kernel will remain tainted even after you undo what caused the taint
1362306a36Sopenharmony_ci(i.e. unload a proprietary kernel module), to indicate the kernel remains not
1462306a36Sopenharmony_citrustworthy. That's also why the kernel will print the tainted state when it
1562306a36Sopenharmony_cinotices an internal problem (a 'kernel bug'), a recoverable error
1662306a36Sopenharmony_ci('kernel oops') or a non-recoverable error ('kernel panic') and writes debug
1762306a36Sopenharmony_ciinformation about this to the logs ``dmesg`` outputs. It's also possible to
1862306a36Sopenharmony_cicheck the tainted state at runtime through a file in ``/proc/``.
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ciTainted flag in bugs, oops or panics messages
2262306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ciYou find the tainted state near the top in a line starting with 'CPU:'; if or
2562306a36Sopenharmony_ciwhy the kernel was tainted is shown after the Process ID ('PID:') and a shortened
2662306a36Sopenharmony_ciname of the command ('Comm:') that triggered the event::
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci	BUG: unable to handle kernel NULL pointer dereference at 0000000000000000
2962306a36Sopenharmony_ci	Oops: 0002 [#1] SMP PTI
3062306a36Sopenharmony_ci	CPU: 0 PID: 4424 Comm: insmod Tainted: P        W  O      4.20.0-0.rc6.fc30 #1
3162306a36Sopenharmony_ci	Hardware name: Red Hat KVM, BIOS 0.5.1 01/01/2011
3262306a36Sopenharmony_ci	RIP: 0010:my_oops_init+0x13/0x1000 [kpanic]
3362306a36Sopenharmony_ci	[...]
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ciYou'll find a 'Not tainted: ' there if the kernel was not tainted at the
3662306a36Sopenharmony_citime of the event; if it was, then it will print 'Tainted: ' and characters
3762306a36Sopenharmony_cieither letters or blanks. In above example it looks like this::
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci	Tainted: P        W  O
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ciThe meaning of those characters is explained in the table below. In this case
4262306a36Sopenharmony_cithe kernel got tainted earlier because a proprietary Module (``P``) was loaded,
4362306a36Sopenharmony_cia warning occurred (``W``), and an externally-built module was loaded (``O``).
4462306a36Sopenharmony_ciTo decode other letters use the table below.
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ciDecoding tainted state at runtime
4862306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ciAt runtime, you can query the tainted state by reading
5162306a36Sopenharmony_ci``cat /proc/sys/kernel/tainted``. If that returns ``0``, the kernel is not
5262306a36Sopenharmony_citainted; any other number indicates the reasons why it is. The easiest way to
5362306a36Sopenharmony_cidecode that number is the script ``tools/debugging/kernel-chktaint``, which your
5462306a36Sopenharmony_cidistribution might ship as part of a package called ``linux-tools`` or
5562306a36Sopenharmony_ci``kernel-tools``; if it doesn't you can download the script from
5662306a36Sopenharmony_ci`git.kernel.org <https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/plain/tools/debugging/kernel-chktaint>`_
5762306a36Sopenharmony_ciand execute it with ``sh kernel-chktaint``, which would print something like
5862306a36Sopenharmony_cithis on the machine that had the statements in the logs that were quoted earlier::
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci	Kernel is Tainted for following reasons:
6162306a36Sopenharmony_ci	 * Proprietary module was loaded (#0)
6262306a36Sopenharmony_ci	 * Kernel issued warning (#9)
6362306a36Sopenharmony_ci	 * Externally-built ('out-of-tree') module was loaded  (#12)
6462306a36Sopenharmony_ci	See Documentation/admin-guide/tainted-kernels.rst in the Linux kernel or
6562306a36Sopenharmony_ci	 https://www.kernel.org/doc/html/latest/admin-guide/tainted-kernels.html for
6662306a36Sopenharmony_ci	 a more details explanation of the various taint flags.
6762306a36Sopenharmony_ci	Raw taint value as int/string: 4609/'P        W  O     '
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ciYou can try to decode the number yourself. That's easy if there was only one
7062306a36Sopenharmony_cireason that got your kernel tainted, as in this case you can find the number
7162306a36Sopenharmony_ciwith the table below. If there were multiple reasons you need to decode the
7262306a36Sopenharmony_cinumber, as it is a bitfield, where each bit indicates the absence or presence of
7362306a36Sopenharmony_cia particular type of taint. It's best to leave that to the aforementioned
7462306a36Sopenharmony_ciscript, but if you need something quick you can use this shell command to check
7562306a36Sopenharmony_ciwhich bits are set::
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci	$ for i in $(seq 18); do echo $(($i-1)) $(($(cat /proc/sys/kernel/tainted)>>($i-1)&1));done
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ciTable for decoding tainted state
8062306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci===  ===  ======  ========================================================
8362306a36Sopenharmony_ciBit  Log  Number  Reason that got the kernel tainted
8462306a36Sopenharmony_ci===  ===  ======  ========================================================
8562306a36Sopenharmony_ci  0  G/P       1  proprietary module was loaded
8662306a36Sopenharmony_ci  1  _/F       2  module was force loaded
8762306a36Sopenharmony_ci  2  _/S       4  kernel running on an out of specification system
8862306a36Sopenharmony_ci  3  _/R       8  module was force unloaded
8962306a36Sopenharmony_ci  4  _/M      16  processor reported a Machine Check Exception (MCE)
9062306a36Sopenharmony_ci  5  _/B      32  bad page referenced or some unexpected page flags
9162306a36Sopenharmony_ci  6  _/U      64  taint requested by userspace application
9262306a36Sopenharmony_ci  7  _/D     128  kernel died recently, i.e. there was an OOPS or BUG
9362306a36Sopenharmony_ci  8  _/A     256  ACPI table overridden by user
9462306a36Sopenharmony_ci  9  _/W     512  kernel issued warning
9562306a36Sopenharmony_ci 10  _/C    1024  staging driver was loaded
9662306a36Sopenharmony_ci 11  _/I    2048  workaround for bug in platform firmware applied
9762306a36Sopenharmony_ci 12  _/O    4096  externally-built ("out-of-tree") module was loaded
9862306a36Sopenharmony_ci 13  _/E    8192  unsigned module was loaded
9962306a36Sopenharmony_ci 14  _/L   16384  soft lockup occurred
10062306a36Sopenharmony_ci 15  _/K   32768  kernel has been live patched
10162306a36Sopenharmony_ci 16  _/X   65536  auxiliary taint, defined for and used by distros
10262306a36Sopenharmony_ci 17  _/T  131072  kernel was built with the struct randomization plugin
10362306a36Sopenharmony_ci 18  _/N  262144  an in-kernel test has been run
10462306a36Sopenharmony_ci===  ===  ======  ========================================================
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ciNote: The character ``_`` is representing a blank in this table to make reading
10762306a36Sopenharmony_cieasier.
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ciMore detailed explanation for tainting
11062306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci 0)  ``G`` if all modules loaded have a GPL or compatible license, ``P`` if
11362306a36Sopenharmony_ci     any proprietary module has been loaded.  Modules without a
11462306a36Sopenharmony_ci     MODULE_LICENSE or with a MODULE_LICENSE that is not recognised by
11562306a36Sopenharmony_ci     insmod as GPL compatible are assumed to be proprietary.
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci 1)  ``F`` if any module was force loaded by ``insmod -f``, ``' '`` if all
11862306a36Sopenharmony_ci     modules were loaded normally.
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci 2)  ``S`` if the kernel is running on a processor or system that is out of
12162306a36Sopenharmony_ci     specification: hardware has been put into an unsupported configuration,
12262306a36Sopenharmony_ci     therefore proper execution cannot be guaranteed.
12362306a36Sopenharmony_ci     Kernel will be tainted if, for example:
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci     - on x86: PAE is forced through forcepae on intel CPUs (such as Pentium M)
12662306a36Sopenharmony_ci       which do not report PAE but may have a functional implementation, an SMP
12762306a36Sopenharmony_ci       kernel is running on non officially capable SMP Athlon CPUs, MSRs are
12862306a36Sopenharmony_ci       being poked at from userspace.
12962306a36Sopenharmony_ci     - on arm: kernel running on certain CPUs (such as Keystone 2) without
13062306a36Sopenharmony_ci       having certain kernel features enabled.
13162306a36Sopenharmony_ci     - on arm64: there are mismatched hardware features between CPUs, the
13262306a36Sopenharmony_ci       bootloader has booted CPUs in different modes.
13362306a36Sopenharmony_ci     - certain drivers are being used on non supported architectures (such as
13462306a36Sopenharmony_ci       scsi/snic on something else than x86_64, scsi/ips on non
13562306a36Sopenharmony_ci       x86/x86_64/itanium, have broken firmware settings for the
13662306a36Sopenharmony_ci       irqchip/irq-gic on arm64 ...).
13762306a36Sopenharmony_ci     - x86/x86_64: Microcode late loading is dangerous and will result in
13862306a36Sopenharmony_ci       tainting the kernel. It requires that all CPUs rendezvous to make sure
13962306a36Sopenharmony_ci       the update happens when the system is as quiescent as possible. However,
14062306a36Sopenharmony_ci       a higher priority MCE/SMI/NMI can move control flow away from that
14162306a36Sopenharmony_ci       rendezvous and interrupt the update, which can be detrimental to the
14262306a36Sopenharmony_ci       machine.
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci 3)  ``R`` if a module was force unloaded by ``rmmod -f``, ``' '`` if all
14562306a36Sopenharmony_ci     modules were unloaded normally.
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci 4)  ``M`` if any processor has reported a Machine Check Exception,
14862306a36Sopenharmony_ci     ``' '`` if no Machine Check Exceptions have occurred.
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci 5)  ``B`` If a page-release function has found a bad page reference or some
15162306a36Sopenharmony_ci     unexpected page flags. This indicates a hardware problem or a kernel bug;
15262306a36Sopenharmony_ci     there should be other information in the log indicating why this tainting
15362306a36Sopenharmony_ci     occurred.
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci 6)  ``U`` if a user or user application specifically requested that the
15662306a36Sopenharmony_ci     Tainted flag be set, ``' '`` otherwise.
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci 7)  ``D`` if the kernel has died recently, i.e. there was an OOPS or BUG.
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci 8)  ``A`` if an ACPI table has been overridden.
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci 9)  ``W`` if a warning has previously been issued by the kernel.
16362306a36Sopenharmony_ci     (Though some warnings may set more specific taint flags.)
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci 10) ``C`` if a staging driver has been loaded.
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci 11) ``I`` if the kernel is working around a severe bug in the platform
16862306a36Sopenharmony_ci     firmware (BIOS or similar).
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci 12) ``O`` if an externally-built ("out-of-tree") module has been loaded.
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci 13) ``E`` if an unsigned module has been loaded in a kernel supporting
17362306a36Sopenharmony_ci     module signature.
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ci 14) ``L`` if a soft lockup has previously occurred on the system.
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci 15) ``K`` if the kernel has been live patched.
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci 16) ``X`` Auxiliary taint, defined for and used by Linux distributors.
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ci 17) ``T`` Kernel was build with the randstruct plugin, which can intentionally
18262306a36Sopenharmony_ci     produce extremely unusual kernel structure layouts (even performance
18362306a36Sopenharmony_ci     pathological ones), which is important to know when debugging. Set at
18462306a36Sopenharmony_ci     build time.
185