Linux Internals
CPU Write-Combining Explained: WB, WC and UC Memory Types and PAT
By NileVPS Team · Published · 5 min read
اقرأ المقال بالعربيMost discussions of CPU performance focus on caches: how data is kept close to the core so repeated accesses are fast. But not all memory should be cached. Device registers, framebuffers, and some I/O regions behave very differently from ordinary RAM, and treating them like normal memory would be either incorrect or slow. x86 processors handle this with memory types, and one of them, write-combining (WC), is a quiet but important optimization for graphics, networking, and storage. This article explains the memory types, how write-combining buffers work, how Linux assigns types with PAT, and where it matters in practice.
Memory types on x86
Every physical address an x86 CPU accesses has an effective memory type that controls caching and ordering. The most important are:
- WB (Write-Back): the normal type for RAM. Reads and writes go through the cache hierarchy; modified lines are written to memory later. Fast and coherent between cores.
- UC (Uncacheable): every access goes directly to the bus, in program order, with no speculation. Required for device control registers where a read can have side effects, such as clearing a status bit.
- UC- (UC minus): like UC, but can be overridden to WC by MTRR settings. Linux uses it as the default for many I/O mappings.
- WC (Write-Combining): reads are uncached, and writes are collected in special buffers and sent out in larger bursts, with weak ordering.
- WT (Write-Through) and WP (Write-Protect): less common types where writes go to memory immediately or are not cached.
MTRRs and PAT
Two mechanisms decide which type applies:
- MTRRs (Memory Type Range Registers) assign types to physical address ranges. Firmware programs them at boot, for example marking RAM as WB and the PCI hole as UC. There are only a few variable ranges, so they are coarse.
- PAT (Page Attribute Table) assigns types per page, through bits in the page table entry. This is far more flexible, and modern Linux relies on PAT to map specific device regions as WC while leaving neighboring registers UC.
When MTRR and PAT disagree, the CPU combines them according to defined rules; for instance, a WC page attribute over a UC MTRR range results in WC.
How write-combining buffers work
A CPU core has a small number of write-combining buffers, on recent Intel designs shared with the line fill buffers, each the size of a cache line, 64 bytes. When software writes to a WC address, the data goes into one of these buffers instead of the cache. Further writes to the same 64-byte line merge into that buffer. When the buffer fills, or when it is evicted or explicitly flushed, its contents are sent to the destination as a single burst transaction.
The benefit is clear when you think about a framebuffer or a device queue across PCI Express. Sixteen separate 4-byte uncached writes would generate sixteen small bus transactions, each with its own header overhead. With WC, they become one 64-byte transaction.
The ordering trade-off
WC writes are weakly ordered. They can reach the device in a different order from the program's order, and the CPU does not guarantee when a partially filled buffer is flushed. Code that needs a specific order, for example "write the descriptor, then write the doorbell", must use a fence such as sfence (on Linux, wmb() in drivers) to make sure earlier WC writes are visible before later ones.
Reads from WC memory are uncached and slow. WC is intended for regions that software mostly writes and rarely reads.
Non-temporal stores
Write-combining also appears with ordinary RAM. Non-temporal stores (movnti, movntdq, and their AVX variants) bypass the cache and use the WC buffers even on WB memory. Optimized memcpy and memset implementations use them for very large copies to avoid evicting useful data from the cache. Like WC writes, they require sfence when ordering matters.
Where write-combining matters
GPU framebuffers and BARs
Graphics drivers map the framebuffer and other large GPU memory apertures as WC. Uploading textures or writing pixels is a stream of writes that benefits from burst transfers, and these regions are rarely read back by the CPU. Mapping them UC would be dramatically slower; mapping them WB would be incorrect because the CPU cache is not coherent with device memory across the bus.
NIC doorbells and inline descriptors
Network drivers notify the NIC of new work by writing to a doorbell register in device memory. A doorbell is often a single write and mapped UC. Some high-performance NICs, however, let the driver write a whole descriptor, or even a small packet, directly into a WC-mapped region of the device. Mellanox/NVIDIA ConnectX "BlueFlame" is a well-known example. This saves the NIC a DMA round trip to fetch the descriptor from host memory and lowers latency for small messages.
NVMe and other devices
NVMe controllers can expose a Controller Memory Buffer (CMB) that hosts may map as WC to place submission queues in device memory.
Practical: checking memory types on Linux
These commands are safe, read-only checks on a VPS or a dedicated server:
# Does the CPU expose PAT?
grep -o -w pat /proc/cpuinfo | head -1
# View MTRR ranges (may be empty or unavailable inside a VM)
cat /proc/mtrr
# PAT memtype list (needs debugfs and root)
sudo mount -t debugfs none /sys/kernel/debug 2>/dev/null
sudo cat /sys/kernel/debug/x86/pat_memtype_list | head
# PCI BARs that support a WC mapping appear as resourceN_wc
ls /sys/bus/pci/devices/*/resource*_wc 2>/dev/nullIn kernel code, drivers request types through APIs such as ioremap() (UC-), ioremap_wc() (WC), and pgprot_writecombine(). Application developers rarely touch these directly.
What changes inside a VPS
In a virtual machine the hypervisor controls the final memory types for guest RAM, and emulated or paravirtual devices such as virtio do not expose real device memory to map as WC. Write-combining becomes directly relevant mainly with device passthrough, on dedicated hardware, or when profiling large memory copies that use non-temporal stores. For typical web, database, and application workloads on a VPS, the benefit is already built into the kernel and libraries you use. You can review CPU and memory options on our pricing page, and see how devices move data in our article on DMA.
FAQ
Is write-combining the same as the CPU cache?
No. Caches keep copies of data for fast reads and writes and stay coherent between cores. Write-combining buffers only collect outgoing writes for a short time and flush them in bursts; WC memory is not cached for reads.
Why not map device memory as normal write-back memory?
Device memory across PCI Express is not coherent with CPU caches, and register reads can have side effects. Caching it would let the CPU serve stale data or delay writes indefinitely, which breaks drivers. UC or WC mappings keep behavior correct.
Do I need to tune write-combining on my VPS?
Almost never. Memory types are set by firmware, the kernel, and drivers. Tuning becomes relevant only for specialized work such as writing device drivers, using passthrough GPUs or NICs, or optimizing very large memory copies.