[Solved] How to include a user level C program in Linux source to be compiled with the Linux kernel?


Userspace programs do exist in the Linux kernel source tree in the tools/ subdirectory.
There does not seem to be a clear-cut (or any) definition of what kind of program constitutes a “tool” that requires/deserves inclusion/distribution with the kernel source.

The types of utilities that do (currently) exist in the kernel source tree include an admin program for examining status bits of memory pages (tools/vm/page-types.c) to three simple programs that utilize/demonstrate the (“new”) chardev GPIO interface (tools/gpio/gpio-event-mon.c and others).

The largest category of userspace programs or tools in the kernel source is in the tools/testing/selftests/ kernel subdirectory.
The documentation is in Documentation/dev-tools/kselftest.rst

======================
Linux Kernel Selftests
======================

The kernel contains a set of "self tests" under the tools/testing/selftests/
directory. These are intended to be small tests to exercise individual code
paths in the kernel. Tests are intended to be run after building, installing
and booting a kernel.  
...  
kselftest runs as a userspace process.  Tests that can be written/run in
userspace may wish to use the `Test Harness`_.  Tests that need to be
run in kernel space may wish to use a `Test Module`_.

Alternatively there are many kernel subsystems and hardware components that do not have their tools in the kernel source, but rather have that code available as separate source/project packages.

Given the stability of the binary API that the Linux kernel provides to userspace, a program is rarely tied to a specific kernel version.
A regression (i.e. a change which causes something to break for existing users) is avoided whenever possible by the kernel maintainers.

One reason for inclusion of programs with the kernel source seems to be convenience for the kernel maintainers.
Kernel builders are encouraged to also build and run the selftest programs.

solved How to include a user level C program in Linux source to be compiled with the Linux kernel?