lib/alloc_page: complete rewrite of the page allocator

This is a complete rewrite of the page allocator.

This will bring a few improvements:
* no need to specify the size when freeing
* allocate small areas with a large alignment without wasting memory
* ability to initialize and use multiple memory areas (e.g. DMA)
* more sanity checks

A few things have changed:
* initialization cannot be done with free_pages like before,
  page_alloc_init_area has to be used instead

Arch-specific changes:
* s390x now uses the area below 2GiB for SMP lowcore initialization.

Each memory area has metadata at the very beginning. The metadata is a
byte array with one entry per usable page (so, excluding the metadata
itself). Each entry indicates if the page is special (unused for now),
if it is allocated, and the order of the block. Both free and allocated
pages are part of larger blocks.

Some more fixed size metadata is present in a fixed-size static array.
This metadata contains start and end page frame numbers, the pointer to
the metadata array, and the array of freelists. The array of freelists
has an entry for each possible order (indicated by the macro NLISTS,

On allocation, if the free list for the needed size is empty, larger
blocks are split. When a small allocation with a large alignment is
requested, an appropriately large block is split, to guarantee the

When a block is freed, an attempt will be made to merge it into the
neighbour, iterating the process as long as possible.

Signed-off-by: Claudio Imbrenda <>
Message-Id: <>
Signed-off-by: Paolo Bonzini <>
6 files changed
tree: ad4e0c6261e57aa18508e531e460dc47a60a85cb
  1. .editorconfig
  2. .gitignore
  3. .gitlab-ci.yml
  4. .travis.yml
  7. Makefile
  10. arm/
  11. configure
  12. errata.txt
  13. lib/
  14. powerpc/
  16. s390x/
  17. scripts/
  18. x86/

Welcome to kvm-unit-tests

See for a high-level description of this project, as well as running tests and adding tests HOWTOs.

Building the tests

This directory contains sources for a KVM test suite.

To create the test images do:


in this directory. Test images are created in ./ARCH/*.flat

NOTE: GCC cross-compiler is required for build on macOS.

Standalone tests

The tests can be built as standalone. To create and use standalone tests do:

make standalone
(send tests/some-test somewhere)
(go to somewhere)

make install will install all tests in PREFIX/share/kvm-unit-tests/tests, each as a standalone test.

Running the tests

Then use the runner script to detect the correct invocation and invoke the test:

./x86-run ./x86/msr.flat



to run them all.

By default the runner script searches for a suitable QEMU binary in the system. To select a specific QEMU binary though, specify the QEMU=path/to/binary environment variable:

QEMU=/tmp/qemu/x86_64-softmmu/qemu-system-x86_64 ./x86-run ./x86/msr.flat

To select an accelerator, for example “kvm”, “hvf” or “tcg”, specify the ACCEL=name environment variable:

ACCEL=kvm ./x86-run ./x86/msr.flat

Tests configuration file

The test case may need specific runtime configurations, for example, extra QEMU parameters and time to execute limited, the runner script reads those information from a configuration file found at ./ARCH/unittests.cfg.

The configuration file also contain the groups (if any) each test belong to. So that a given group can be executed by specifying its name in the runner's -g option.

Unit test inputs

Unit tests use QEMU's ‘-append args...’ parameter for command line inputs, i.e. all args will be available as argv strings in main(). Additionally a file of the form


may be passed with ‘-initrd file’ to become the unit test's environ, which can then be accessed in the usual ways, e.g. VAL = getenv(“KEY”). Any key=val strings can be passed, but some have reserved meanings in the framework. The list of reserved environment variables is below

QEMU_ACCEL                   either kvm, hvf or tcg
QEMU_VERSION_STRING          string of the form `qemu -h | head -1`
KERNEL_VERSION_STRING        string of the form `uname -r`

Additionally these self-explanatory variables are reserved


Guarding unsafe tests

Some tests are not safe to run by default, as they may crash the host. kvm-unit-tests provides two ways to handle tests like those.

  1. Adding ‘nodefault’ to the groups field for the unit test in the unittests.cfg file. When a unit test is in the nodefault group it is only run when invoked

    a) independently, ARCH-run ARCH/test

    b) by specifying any other non-nodefault group it is in, groups = nodefault,mygroup : ./ -g mygroup

    c) by specifying all tests should be run, ./ -a

  2. Making the test conditional on errata in the code,

    if (ERRATA(abcdef012345)) {

    With the errata condition the unsafe unit test is only run when

    a) the ERRATA_abcdef012345 environment variable is provided and ‘y’

    b) the ERRATA_FORCE environment variable is provided and ‘y’

    c) by specifying all tests should be run, ./ -a (The -a switch ensures the ERRATA_FORCE is provided and set to ‘y’.)

The ./errata.txt file provides a mapping of the commits needed by errata conditionals to their respective minimum kernel versions. By default, when the user does not provide an environ, then an environ generated from the ./errata.txt file and the host's kernel version is provided to all unit tests.


Directory structure

.:                  configure script, top-level Makefile, and
./scripts:          general architecture neutral helper scripts for building and running tests
./scripts/<ARCH>:   architecture dependent helper scripts for building and running tests
./lib:              general architecture neutral services for the tests
./lib/<ARCH>:       architecture dependent services for the tests
./<ARCH>:           the sources of the tests and the created objects/images

See ./ARCH/README for architecture specific documentation.


Currently there is a mix of indentation styles so any changes to existing files should be consistent with the existing style. For new files:

  • C: please use standard linux-with-tabs, see Linux kernel doc Documentation/process/coding-style.rst
  • Shell: use TABs for indentation


  • While the kernel standard requires 80 columns, we allow up to 120.


Patches are welcome at the KVM mailing list

Please prefix messages with: [kvm-unit-tests PATCH]

You can add the following to .git/config to do this automatically for you:

    subjectprefix = kvm-unit-tests PATCH

Additionally it‘s helpful to have a common order of file types in patches. Our chosen order attempts to place the more declarative files before the code files. We also start with common code and finish with unit test code. git-diff’s orderFile feature allows us to specify the order in a file. The orderFile we use is scripts/git.difforder; adding the config with git config diff.orderFile scripts/git.difforder enables it.