41

Linux uses a virtual memory system where all of the addresses are virtual addresses and not physical addresses. These virtual addresses are converted into physical addresses by the processor.

To make this translation easier, virtual and physical memory are divided into pages. Each of these pages is given a unique number; the page frame number.

Some page sizes can be 2 KB, 4 KB, etc. But how is this page size number determined? Is it influenced by the size of the architecture? For example, a 32-bit bus will have 4 GB address space.

53

You can find out a system's default page size by querying its configuration via the getconf command:

$ getconf PAGE_SIZE
4096

or

$ getconf PAGESIZE
4096

NOTE: The above units are typically in bytes, so the 4096 equates to 4096 bytes or 4kB.

This is hardwired in the Linux kernel's source here:

Example

$ more /usr/src/kernels/3.13.9-100.fc19.x86_64/include/asm-generic/page.h
...
...
/* PAGE_SHIFT determines the page size */

#define PAGE_SHIFT  12
#ifdef __ASSEMBLY__
#define PAGE_SIZE   (1 << PAGE_SHIFT)
#else
#define PAGE_SIZE   (1UL << PAGE_SHIFT)
#endif
#define PAGE_MASK   (~(PAGE_SIZE-1))

How does shifting give you 4096?

When you shift bits, you're performing a binary multiplication by 2. So in effect a shifting of bits to the left (1 << PAGE_SHIFT) is doing the multiplication of 2^12 = 4096.

$ echo "2^12" | bc
4096
  • 2
    Modern hardware supports 2MB, and some 1GB, pagesizes. Can "PAGE_SHIFT" be set to 21 for 2MB pages as the default for the kernel build? – ReverseFlow Apr 11 '18 at 2:41
  • 2
    @ReverseFlow To get such a thing answered, I would ask a separate question. – Kirill Bulygin Jul 15 '18 at 11:27
17

The hardware (specifically, the MMU, which is part of the CPU) determines what page sizes are possible. There is no relation to the processor register size and only an indirect relation to the address space size (in that the MMU determines both).

Almost all architectures support a 4kB page size. Some architectures support larger pages (and a few also support smaller pages), but 4kB is a very widespread default.

Linux supports two page sizes:

  • Normal-sized pages, which I believe are 4kB by default on all architectures, though some architectures allow other values, e.g. 16kB on ARM64 or 8kB, 16kB or 64kB on IA64. These correspond to the deepest level of descriptors on the MMU (what Linux calls PTE).
  • Huge pages, if compiled in (CONFIG_HUGETLB_PAGE is necessary, and CONFIG_HUGETLBFS as well for most uses). This corresponds to the second-deepest level of MMU descriptors (what Linux calls PMD) (or at least it usually does, I don't know if this holds on all architectures).

The page size is a compromise between memory usage, memory usage and speed.

  • A larger page size means more waste when a page is partially used, so the system runs out of memory sooner.
  • A deeper MMU descriptor level means more kernel memory for page tables.
  • A deeper MMU descriptor level means more time spent in page table traversal.

The gains of larger page sizes are tiny for most applications, whereas the cost is substantial. This is why most systems use only normal-sized pages.

You can query the (normal) page size on your system with the getconf utility or the C function sysconf.

$ getconf PAGE_SIZE
4096

Using huge pages requires mounting the hugetlbfs filesystem and mmapping files there.

  • FYI: Using huge pages doesn't require mounting hugetlbfs, as that kernel doc notes at the bottom you can also use shared memory calls or a mmap flag. Also, more interestingly, you can enable transparent huge pages, then the kernel will automatically convert normal pages to huge pages when possible across all apps. – derobert May 8 '14 at 13:40
  • This is the truly fundamental answer. Linux is just designed to use the paging hardware well. More details about paging: stackoverflow.com/questions/18431261/how-does-x86-paging-work – Ciro Santilli 新疆改造中心996ICU六四事件 Oct 27 '15 at 6:31
0

The page size depends mostly on the processor architecture. On x86, from the times of the 386 processor which introduced protected mode, the page size has been 4 kB.

In x64 mode, there can be also huge pages, which are 2 MB in size. However, using those is a bit complicated.

You can find more information on page size in Wikipedia article

0

The processor determines the page sizes available. For most purposes, the hardware implemented page size on x86 and x86_64 processors is 4kb. However, the operating system can allocate more than one page at a time if it wants to, and thereby effectively implement 8kb, 16kb or 32kb pages in software.

The x86 and x86_64 processors can respectively also intermix 4mb and 2mb pages along with the standard 4kb pages. If that capability is used at all, it would mostly be used to allocate kernel space.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.