User Space vs Kernel Space Explained for Systems Programmers
User space and kernel space are two privilege levels and two address ranges; code in one reaches the other only through system calls, faults and interrupts.

Two worlds with one door between them
A Linux process lives in two worlds at once. Most of the time it runs user code in user space: its own functions, its libraries, its data. Occasionally it asks the kernel to do something, and for the duration of that request the same process runs kernel code in kernel space. The process ID does not change, but the rules do: different privilege level, different stack, access to different memory.
This post explains what separates the two, how a request crosses over, and what can and cannot be shared. The details use x86-64 and Linux 6.12 LTS; the concept holds on arm64 and RISC-V with different names.
What separates them
There are three separations, and they reinforce each other.
Privilege level. The processor has modes. On x86-64 they are called rings, and Linux uses ring 3 for user space and ring 0 for the kernel. In ring 3 some instructions are forbidden (loading page tables, disabling interrupts, accessing I/O ports) and executing one raises a fault. Ring 0 may do anything.
Address ranges. Each process has a virtual address space. On x86-64 with 4-level paging, user space gets the lower half, 0 to 0x00007fffffffffff, and the kernel gets the upper half starting at 0xffff800000000000. Page table entries carry a user/supervisor bit; a user-mode access to a supervisor page faults.
Stacks. A process has a user stack that it controls. When it enters the kernel, the processor switches to a separate kernel stack for that task. A user program cannot corrupt kernel control flow by arranging a bad stack pointer, because the kernel never trusts the one it was handed.
See the address split yourself
You can print where your own data lives, which shows the user half in practice.
#include <stdio.h>
#include <stdlib.h>
int global_counter = 7;
int main(void)
{
int local = 1;
int *heap = malloc(sizeof *heap);
printf("code : %p\n", (void *)main);
printf("global : %p\n", (void *)&global_counter);
printf("heap : %p\n", (void *)heap);
printf("stack : %p\n", (void *)&local);
free(heap);
return 0;
}gcc -Wall -o where where.c
./wherecode : 0x5581c3a4a149
global : 0x5581c3a4d010
heap : 0x5581c4c7e2a0
stack : 0x7ffd9e1b3a2cThe numbers will differ on every run because of address space layout randomization, which is the kernel randomizing where it places each region. What stays the same is that every address is below 0x0000800000000000. A kernel address such as 0xffffffff81000000 would never appear here; if you tried to read it, the processor would fault and the kernel would send the process SIGSEGV.
Crossing the boundary
There are exactly three ways execution moves from user space into the kernel.
A system call. The program asks. It executes
syscall, the processor jumps to the address stored in theIA32_LSTARmodel-specific register, which the kernel set at boot to point at its system call entry code.An exception. The program does something the processor cannot complete, such as touching an unmapped page or dividing by zero. The processor jumps to a handler in the interrupt descriptor table.
An interrupt. A device or timer needs attention. The processor stops the user code between two instructions and jumps to the handler for that vector.
The first is synchronous and requested. The other two happen without the program's consent, which is the point: a timer interrupt is how the kernel takes the CPU back from a program that never yields.
Leaving the kernel is the reverse: the kernel restores the user registers and executes sysret or iret, which drops the processor back to ring 3.
Moving data across
The two address ranges are not interchangeable, so the kernel cannot dereference a user pointer as if it were its own. If it did, a malicious program could pass a kernel address as a buffer and trick the kernel into reading or overwriting its own memory. Instead, the kernel uses dedicated helpers that check the range and handle faults gracefully.
#include <linux/uaccess.h>
#include <linux/fs.h>
static ssize_t demo_write(struct file *f, const char __user *ubuf,
size_t len, loff_t *off)
{
char kbuf[64];
if (len >= sizeof(kbuf))
return -EINVAL;
if (copy_from_user(kbuf, ubuf, len))
return -EFAULT;
kbuf[len] = '\0';
pr_info("demo: got %zu bytes\n", len);
return len;
}copy_from_user() returns the number of bytes it could not copy, so a nonzero result means the user pointer was bad. The __user annotation is a marker that the static analysis tool Sparse uses to flag code that dereferences a user pointer directly. The kernel's memory access documentation lists the full set of helpers.
If the module above loaded and a program wrote ten bytes to its device, the log would show:
demo: got 10 bytesThis cost is why high-throughput interfaces avoid copying. mmap, splice and io_uring exist partly to let user space and the kernel share pages instead of copying them in both directions.
Mitigations that tighten the split
Hardware features have been added to harden the line between the two worlds.
SMEP (Supervisor Mode Execution Prevention) stops ring 0 from executing user pages, defeating the classic attack of pointing the kernel at user-supplied code.
SMAP (Supervisor Mode Access Prevention) stops ring 0 from reading or writing user pages unless the kernel explicitly allows it with the
stacandclacinstructions around helpers likecopy_from_user.KPTI (Kernel Page Table Isolation) keeps the kernel's mappings mostly out of the page tables used while user code runs, a response to the Meltdown speculative execution attack. It makes each crossing more expensive, which is one reason system call cost changed across kernel versions.
You can check which are active on your machine:
grep -o -w -E 'smep|smap' /proc/cpuinfo | sort -u
grep . /sys/devices/system/cpu/vulnerabilities/meltdownsmap
smep
Mitigation: PTIThe second line depends on the CPU. A processor not vulnerable to Meltdown prints Not affected and the kernel does not enable page-table isolation.
What is not true
A frequent misconception is that user space and kernel space are different processes or different machines. They are not. A system call runs on the caller's own kernel stack, in the context of the caller's task, charged to its CPU time. When top shows "sy" time, that is the portion of the CPU spent executing kernel code on behalf of processes.
Another is that kernel space is only for the operating system. Loadable kernel modules and drivers run there too, with full privilege. Code in a module can read any memory and crash the machine, which is why modules are signed on systems with Secure Boot.
Reading the boundary in a trace
A system call crossing is visible in tools you already have. strace shows each call a program makes, and perf can count mode switches. The useful habit is to ask, for any slow program, how many times it crosses into the kernel and how much data it moves each time. A program that reads a file one byte per read() call pays the crossing cost once per byte, while one that reads 64 KiB at a time pays it once per 64 KiB. The kernel does the same work either way; the difference is how many times the processor changes privilege level and how many times the kernel validates arguments.
This is also the reason buffered I/O exists in the C library. fread and fwrite collect small operations into one larger system call, which keeps most of a program's time on its own side of the boundary. When you see a profile dominated by system call entry and exit rather than by work inside the calls, batching is usually the fix.


