Kernel Size Tuning Guide
This document describes how to configure the Linux kernel to use a small amount of memory and flash.
Note: This document is a work in progress. Please feel free to add material anywhere you have additional information or data. Sections of this document which need additional work are denoted with [FIXTHIS] markers.
- 1 Introduction
- 2 Measuring the kernel
- 3 Kernel Size Tuning features
- 3.1 Linux-tiny patches
- 3.2 How to configure the kernel
- 3.3 Kernel Configuration Options
- 3.4 Special Instructions for some kernel options
- 3.5 Booting without SysFS
- 3.6 Booting without /proc fs
- 3.7 Using kernel memory measurement features
- 4 Outline
- 5 References
- 6 Appendices
One big problem area when using Linux in an embedded project is the size of the Linux kernel.
Measuring the kernel
There are 3 aspects of kernel size which are important:
- the size of the kernel image stored in flash (or other persistent storage)
- the static size of kernel image in RAM (usually, this will be the size of the uncompressed image)
- This includes the text, data, and BSS segments of the kernel at the time it is loaded. The text and BSS segments will stay the same size for the kernel throughout it execution. However, the data and stack segments may grow according to the needs of the system.
- the amount of dynamic RAM used by the kernel.
- This will fluctuate during system execution. However, there is a baseline amount of memory which is allocated at system startup. Application-specific RAM can be calculated to be above this minimal amount of required RAM.
For now, this document ignores Execute-In-Place (XIP) and Data-Read-In-Place (DRIP) techniques, the use of which have an impact on the amount of flash and RAM used by the kernel. See the following online resources for more information about these techniques: Kernel XIP and Data Read In Place
Measuring the kernel image size
The compressed kernel image is what is stored in the flash or ROM of the target device. The size of this image can be obtained by examining the size of the image file in the host filesystem with the 'ls -l' command:
- for example: 'ls -l vmlinuz' or 'ls -l bzImage' (or whatever the compressed image name is for your platform.)
Measuring the kernel text, data and bss segments
Use the size command to determine the size of the text, data, and BSS segments of a kernel image.
Note that the BSS segment is not stored in the kernel image because it can be synthesized at boot time by filling a block of memory with zeros. Note also that portions of the kernel text and data are set aside in special initialization segments, which are discarded when the kernel finishes booting. Because of these factors, the size command does not give you an exactly correct value for the static kernel RAM size. However, it can be used as a reasonable estimate.
To use the size command, run it with the filename of the uncompressed kernel image (which is usually vmlinux).
- for example: 'size vmlinux'
text data bss dec hex filename 2921377 369712 132996 3424085 343f55 vmlinux
Measuring and comparing sub-parts of the kernel
In order to find areas where the kernel size can be reduced, it is often useful to break down the static size of the kernel by sub-system or by kernel symbol. The following sections describe how to see the size of each kernel sub-system, how to see the size of individual kernel symbols, and how to compare the size of symbols between two kernel versions. This is useful because as you make changes to the kernel configuration you can determine what part of the kernel is affected by the change. From this information you may be able to predict what the affect of the change will be, and decide whether the change is acceptable.
Measuring major kernel subsystems
The major sub-systems of the kernel are put into library object files named built-in.o in the corresponding sub-directory for that sub-system within the kernel build directory. The major sub-directories, at the time of this writing (for kernel 2.6.17) are: init, user, kernel, mm, fs, ipc, security, crypto, block, ltt, drivers, sound, net, lib
To see the size of the major kernel sections (code, data, and BSS), use the size command, with a wildcard for the first level of sub-directory:
- size */built-in.o
You can pipe this output through sort to sort by the largest libraries:
- size */built-in.o | sort -n -r -k 4
731596 53144 33588 818328 c7c98 drivers/built-in.o 687960 24972 2648 715580 aeb3c fs/built-in.o 547844 19508 28052 595404 915cc net/built-in.o 184072 6256 32440 222768 36630 kernel/built-in.o 141956 3300 2852 148108 2428c mm/built-in.o 68048 1804 1096 70948 11524 block/built-in.o 26216 768 0 26984 6968 crypto/built-in.o 17744 2412 2124 22280 5708 init/built-in.o 20780 292 124 21196 52cc ipc/built-in.o 18768 68 0 18836 4994 lib/built-in.o 2116 0 0 2116 844 security/built-in.o 134 0 0 134 86 usr/built-in.o text data bss dec hex filename
To see even greater detail, you can examine the size of built-in.o files even deeper in the kernel build hierarchy, using the find command:
- find . -name "built-in.o" | xargs size | sort -n -r -k 4
731596 53144 33588 818328 c7c98 ./drivers/built-in.o 687960 24972 2648 715580 aeb3c ./fs/built-in.o 547844 19508 28052 595404 915cc ./net/built-in.o 260019 9824 4944 274787 43163 ./net/ipv4/built-in.o 184072 6256 32440 222768 36630 ./kernel/built-in.o ...
Measuring individual kernel symbols
You can measure the size of individual kernel symbols using the 'nm' command. Using the nm --size -r vmlinux
[tbird@crest ebony]$ nm --size -r vmlinux | head -10 00008000 b read_buffers 00004000 b __log_buf 00003100 B ide_hwifs 000024f8 T jffs2_garbage_collect_pass 00002418 T journal_commit_transaction 00002400 b futex_queues 000021a8 t jedec_probe_chip 00002000 b write_buf 00002000 D init_thread_union 00001e6c t tcp_ack
Legend: The columns of this output are:
- size in bytes (in hexadecimal)
- symbol type
- symbol name.
The symbol type is usually one of:
- 'b' or 'B' for a symbol in the BSS segment (uninitialized data),
- 't' or 'T' for a symbol in the text segment (code), or
- 'd' or 'D' for a symbol in the data segment.
Use 'man nm' for additional information on the 'nm' command.
Comparing kernel symbols between two kernel images
Use the bloat-o-meter command, found in the kernel source scripts directory, to compare the symbol sizes between two kernel images.
- <kernel-src>/scripts/bloat-o-meter vmlinux.default vmlinux.altconfig
If you get an error: 'chmod a+x <kernel-src>/scripts/bloat-o-meter'
Example output, comparing a baseline kernel to one configured with CONFIG_PRINTK=n:
 $ ../../linux/scripts/bloat-o-meter vmlinux.baseline vmlinux.no-printk add/remove: 5/23 grow/shrink: 8/1541 up/down: 1141/-199824 (-198683) function old new delta proc_ioctl_default - 610 +610 proc_reapurb - 296 +296 proc_disconnectsignal - 88 +88 proc_releaseinterface - 72 +72 proc_claiminterface - 36 +36 xprt_adjust_cwnd 169 182 +13 do_timer 1052 1063 +11 i8042_controller_reset 78 84 +6 serio_init 167 172 +5 usb_exit 80 81 +1 early_uart_console_init 45 46 +1 console_unblank 103 104 +1 console_conditional_schedule 21 22 +1 parse_early_param 102 101 -1 machine_emergency_restart 249 248 -1 console_callback 239 238 -1 arch_align_stack 45 44 -1 quirk_p64h2_1k_io 183 181 -2 printk_time 4 - -4 printk_cpu 4 - -4 oops_timestamp.7 4 - -4 neigh_resolve_output 733 729 -4 msg_level.4 4 - -4 ... de_dump_status 1586 313 -1273 decode_getfattr 3156 1748 -1408 ext3_fill_super 5980 4545 -1435 usbdev_ioctl 6476 4846 -1630 usb_get_configuration 4001 1878 -2123 proc_submiturb 2294 - -2294 __log_buf 131072 - -131072
Kernel Size Tuning features
The Linux kernel includes a number of options for to control the features and options it supports. The kernel, over time, has accumulated a large set of features and capabilities. But many features are not needed in Consumer Electronics products. By carefully tuning the kernel options, you can omit many parts of the kernel and save memory in your product.
The Linux-tiny patch set is a set of patches maintained by Matt Mackall developed with the intent to help a developer reduce the size of the Linux kernel.
These patches are described at: Linux Tiny
The Linux-tiny patch set includes a number of different patches to allow the kernel to be reduced in size. Sometimes, the size reductions are accomplished by reducing the number of objects for a particular features (like the number of possible swap areas, or the number of tty discipline structures). Sometimes, the size reductions are achieved by removing features or functions from the kernel.
Here is a list of the individual Linux-tiny patches that are available for the 2.6.22 kernel at Linux Tiny Patch Details
Please note that the last patch in this list ("do-printk") is available separately from the main Linux-tiny patch set. Please find this patch at: Do Printk
The patches listed in this table represent patches that can be applied to a 2.6.16 Linux kernel. However, as of version 2.6.16, many options for reducing the kernel were already available in Linux. A list of options, both from these patches and from existing code, which are interesting for tuning the kernel size is provided in the section: "Kernel configuration Options"
How to configure the kernel
[FIXTHIS - need detailed kernel configuration instructions]
- use 'make menuconfig'
- perform thorough testing of your library and applications with the smaller config
- development vs. deployment configurations
- describe all_no config - most times it won't boot.
Kernel Configuration Options
Here is a table of kernel configuration options, including a description, the default value for a kernel, and the recommended value for a smaller configuration of the kernel:
|CONFIG_CORE_SMALL||tune some kernel data sizes||N||Y|
|CONFIG_NET_SMALL||tune some net-related data sizes||N||Y|
|CONFIG_KMALLOC_ACCOUNTING||turn on kmalloc accounting||N||Y *|
|CONFIG_AUDIT_BOOTMEM||print out all bootmem allocations||N||Y *|
|CONFIG_DEPRECATE_INLINES||cause compiler to emit info about inlines||N||Y *|
|CONFIG_PRINTK||allow disable of printk code and message data||Y||N|
|CONFIG_BUG||allow elimination of BUG (and BUG_ON??) code||Y||N|
|CONFIG_ELF_CORE||allow disabling of ELF core dumps||Y||N|
|CONFIG_PROC_KCORE||allow disabling of /proc/kcore||Y||N|
|CONFIG_AIO||allow disabling of async IO syscalls||Y||N|
|CONFIG_XATTR||allow disabling of xattr syscalls||Y||N|
|CONFIG_FILE_LOCKING||allow disabling of file locking syscalls||Y||N|
|CONFIG_DIRECTIO||allow disabling of direct IO support||Y||N|
|CONFIG_MAX_SWAPFILES_SHIFT||number of swapfiles||5||0|
|CONFIG_NR_LDISCS||number of tty line disciplines||16||2|
|CONFIG_MAX_USER_RT_PRIO||number of RT priority levels (schedule slots)||100||5|
|Other config options||These are not in Linux-tiny, but help with size||default||small|
|CONFIG_KALLSYMS||load all symbols for debugging/kksymoops||Y||N|
|CONFIG_SHMEM||allow disabling of shmem filesystem||Y||N +|
|CONFIG_SWAP||allow disabling of support for a swap segment (virtual memory)||Y||N|
|CONFIG_SYSV_IPC||allow disabling of support for System V IPC||Y||N +|
|CONFIG_POSIX_MQUEUE||allow disabling of POSIX message queue support||Y||N +|
|CONFIG_SYSCTL||allow disabling of sysctl support||Y||N +|
|CONFIG_LOG_BUF_SHIFT||control size of kernel printk buffer||14||11|
|CONFIG_UID16||allow support for 16-bit uids||Y||??|
|CONFIG_CC_OPTIMIZE_FOR_SIZE||Use gcc -os to optimize for size||Y||Y|
|CONFIG_MODULES||allow support for kernel loadable modules||Y||N +|
|CONFIG_KMOD||allow support for automatic kernel module loading||Y||N|
|CONFIG_PCI||allow support for PCI bus and devices||Y||Y -|
|CONFIG_XIP_KERNEL||allow support for kernel Execute-in-Place||N||N|
|CONFIG_BLK_DEV_LOOP||support for loopback block device||Y||Y -|
|CONFIG_BLK_DEV_RAM||support for block devices for RAM filesystems||Y||Y -|
|CONFIG_BLK_DEV_RAM_COUNT||Number of block devices for RAM filesystems||16||2?|
|CONFIG_BLK_DEV_RAM_SIZE||Size of block device struct for RAM filesystems||4096||??|
|CONFIG_IOSCHED_AS||Include Anticipatory IO scheduler||Y||Y|
|CONFIG_IOSCHED_DEADLINE||Include Deadline IO scheduler||Y||N +|
|CONFIG_IOSCHED_CFQ||Include CFQ IO scheduler||Y||N +|
|CONFIG_IP_PNP||support for IP autoconfiguration||Y||N +|
|CONFIG_IP_PNP_DHCP||support for IP autoconfiguration via DHCP||Y||N +|
|CONFIG_IDE||support for IDE devices||Y||N +|
|CONFIG_SCSI||support for SCSI devices||Y||N +|
- "Y *" - Set to 'Y' for measurement during development, and set to 'N' for deployment.
- "N +" - Whether you can set this to 'N' depends on whether this feaure is needed by your applications.
- "Y -" - You probably need this, but it might we worth checking to see if you don't.
Special Instructions for some kernel options
How to use CONFIG_PRINTK
If the "do-printk" patch is applied, there are two options which control the compilation of printk elements in the kernel: CONFIG_PRINTK_FUNC and CONFIG_PRINTK. You can use these options to control how much printk support the kernel provides, and to control on a global basis whether any printk messages at all are compiled into the kernel. Another special preprocessor variable is also available, called DO_PRINTK, which provides the ability to enable printk messages inside a single C compilation unit, even if printk messages are disabled globally.
This section explains how to use these features to reduce the kernel size, while still enabling sufficient printk messages to be useful during development and deployment.
The CONFIG_PRINTK option disables all of the kernel printk calls. By setting this option to 'N' in your kernel configuration, all uses of "printk" throughout the kernel source are turned into empty statements, and omitted when the program is compiled. This provides a substantial size savings, since the kernel messages often account for more than 100 kilobytes of space in the kernel image. Setting this option to 'N' will not, however, remove the actual
code itself (just the calls to
). The CONFIG_PRINTK_FUNC option controls whether the
function and various helper functions are compiled into the Linux kernel. When this is set to 'N', CONFIG_PRINTK is automatically set to 'N', and no printk messages are compiled into the kernel. This usually saves about another 4K of size in the kernel image.
By using both CONFIG_PRINTK and CONFIG_PRINTK_FUNC, you can reduce the size of the kernel image (and that flash and RAM it requires). However, there is a drawback to eliminating all the messages. Obviously, it is then not possible to get any status, diagnostic or debug messages from the kernel! Another mechanism is available, which allows you to control on a per-file basis which printk calls are compiled into the kernel. This is the pre-processor variable DO_PRINTK.
To use DO_PRINTK, set CONFIG_PRINTK to 'N' and CONFIG_PRINTK_FUNC to 'Y' in your kernel configuration. This will globally disable all printk calls in the kernel. Now, determine the C files where you wish to enable printk messages, and add the line:
#define DO_PRINTK 1
at the top of each file. Now, the printk calls in those files will be compiled normally. Printk calls in other modules will be omitted.
lines. In order to change the set of printk messages preserved in the code, you will need to modify the
lines, and recompile the kernel. (There is no runtime control of the printk calls.) This is a simple mechanism, but it does provide a way to omit most of the printk messages from the kernel while still preserving some messages that may be useful during
development or on a deployed product.
In review, there are basically 3 different settings combinations for CONFIG_PRINTK_FUNC and CONFIG_PRINTK that make sense:
|Y||Y||This is the default setting for the kernel configuration. In this setting the |
|Y||N||This leaves the actual printk() routine in the kernel, but disables all calls to printk throughout the entire source code. However, you can use DO_PRINTK in individual modules to enable the printk calls from those modules.|
|N||N||This removes the printk() routine from the kernel, and disables all kernel printk messages, and gives the smallest kernel code and data size. DO_PRINTK will NOT enable any module-specific printk calls.|
Booting without SysFS
(copied from linux-tiny wiki)
Turning off sysfs support can save a substantial amount of memory in some setups. One big downside is that it breaks the normal boot process because the kernel can no longer mapa symbolic device name to the internal device numbers.
Thus, you will need to pass a numeric device number in hex. For example, to boot off /dev/hda1, which has major number 3 and minor 1, you'll need to append a root== option like this:
/boot/vmlinuz root==0x0301 ro
Booting without /proc fs
It is also possible to boot with
fs, but many programs expect this psuedo-filesystem to be present and mounted. For example,
are two commands which retrieve information from
in order to run.
list some workarounds here
Using kernel memory measurement features
This is a features of Linux-tiny, which tracks callers of kmalloc and kfree, and produces summary statistics for kernel memory allocations, as well as detailed information about specific kmalloc callers.
This was first published by Matt Mackall in February of 2005, but was not mainlined at that time.
To see results for kernel allocations, follow these steps:
- turn on the CONFIG_KMALLOC option. This will show up on the kernel configuration menus as "Enabled accounting of kmalloc/kfree allocations?"
- recompile your kernel
- boot the kernel
- periodically, examine the accounting stats
- cat /proc/kmalloc
- Tuning the kernel
- how to measure kernel size
- in-kernel size reporting - kmalloc accounting
- kernel configuration options
- mainline options
- optional features
- minimal config
- sufficient API?
- POSIX compliance
- LSB compliance
- LTP compliance
- file systems
- comparison of file system sizes
- compiler options for reducing size
- gcc -os
- gcc -whole-program
- online resources:
- Linux-tiny project web site: [linux-tiny]
- eLinux wiki Linux-tiny page: Linux Tiny
- Matt Mackall's https://elinux.org/images//8/83/Pdf.gif Linux-tiny presentation
- CE Linux Forum resources for reducing system size: System Size
Appendix A - Sample minimum configuration for ARM
[FIXTHIS - need ARM minimum config.]
Appendix B - Configuration Option Details
For each option, would like to document:
- what is size affect for different option values
- This page & Kernel Size Tuning Guide Config Option Impact describe kernel size and RAM usage impact affected by each configuration option listed in "Kernel Configuration Options" above, on i386.
- what is affect of performance, functionality, etc.
- what programs (if any) will stop working if option is turned off (or reduced)
Appendix C - Things to research
- how to use an initramfs (to avoid using NFS-mounted rootfs)
- how to use a local fs (to avoid using NFS-mounted rootfs)
- Eric Biederman's turning off CONFIG_BLOCK - will any FS work after this??
- he got a 2.6.1 kernel (presumably all_no) to: "191K bzImage and a 323K text segment". See here.
- why is networking so big??
- why are file systems so big??
- capture serial output from kernel for size measurement (see grabserial program)