skip to content
Wonbin Jin

The Problem With Page Tables

/ 6 min read

GitHub

In my undergrad operating systems class, I was taught the historical progression of virtual to physical address translation techniques. Starting from physical addressing, we moved on to base and bounds where the virtual to physical address mapping was linear with one big continuous chunk of physical memory backing the virtual address. Then we had segmentation where we split up one large contiguous chunks to several variable sized smaller chunks for different purposes like instructions, data, heap, stack, and so on. Then we moved on to paging where physical memory was split into same sized pages and virtual pages were mapped to physical pages. Finally, we had the multilevel page table which extended paging to larger address spaces and better support for sparse address spaces. Each new technique was motivated by a shortcoming of its ancestors. However, ever since the introduction of multilevel paging in around the 1970s (the earliest system I know that used multilevel paging is Multics), there has not been a drastically new translation technique. Sure there have been advancements like huge pages or nested page tables with hypervisors, but to me these are simply feature extensions of multilevel paging.

Over the summer, I worked on io-pgtable code fairly intimately. One of the problems I was trying to solve was an excessive number of empty page tables. An “empty” page table is a page table that is allocated but doesn’t contain any mappings. Lets walk through how this can happen.

Consider a 3 level page table. Typically, the first level (L0) page table is already allocated when a translation domain is created. However, as you subsequently traverse the tree, new page tables may need to be allocated. In the case of a 4 KiB mapping in a new domain, the second level (L1) page table and the third and terminal level (L2) page tables need to be allocated. Then, the actually mapping from virtual page to physical page can be created in a leaf mapping in the L2 table.

Now, when this leaf mapping is unmapped, these page tables remain allocated. The L2 table no longer has any mappings in it and is thus considered “empty”. The code that implements this mapping and unmapping is arm_lpae_map_pages and arm_lpae_unmap_pages (LPAE stands for Large Physical Addressing Extension) and these interfaces are specifically for the ARM VMSA PTE formats. You may be wondering why these page tables remain alloacted. There are several valid reasons for this.

  1. Hard synchronization problem. When page tables need to be allocated, it can be done safely by atomically switching out the parent’s PTE with a cmpxchg. Thus, even if concurrent threads decide to allocate a next level page table, only one “wins” and the other simply frees its page table and uses the page table that the “winner” allocated. This is not as simple when freeing empty page tables. Consider the scenario where thread A wants to free a L2 page table. It is very hard to know with certainty that noone else will access the table when you free it. A first removes the pointer from the parent L1 page tables entry. However, thread B has already followed that pointer and is about to access the page table. I do not know of a way to fix this problem without an elaborate locking scheme.
  2. Invalidation of walk cache. IOMMUs typically have a “walk cache” that contain references to intermediate page tables. This is to speed up page table walks in case of TLB misses. Thus, before freeing a page table, you must also invalidate this walk cache entry so that the hardware PTW engine does not access freed memory. This adds latency to the unmap API which leads into my next point
  3. Hyrum’s Law. The logical place to insert the logic to free allocated page tables is in the unmap path. However, we can observe Hyrum’s law at work here as indeed, every observable behavior of an abstraction is depended upon by somone. It turns out that some of the users of the IOMMU APIs depende on fast unmaps. For example, many network drivers map a page for every packet and subsequently unmap the page, thus depending on the latency of unmap. Even though there is no guarantee on unmap latency in the API contract, the dependency on this behavior makes it hard to add additional overhead like freeing a page table and flushing the relevant walk cache entries.

There are some very narrow cases where empty page tables are freed. The first is when a translation domain is freed. All the tables in that domain will be freed with the __arm_lpae_free_pgtable function. In my experience, this rarely happens as a domain is typically tied with a peripheral device and remains allocated throughout the lifetime of the kernel. The second is when an unmap spans an entire child page table. All this is to say that the conditions of page tables being freed, rarely ever happens. The callers of the API need to be very deliberate about the addresses that they are mapping.

Android robot

The IOMMU API contract is that there cannot be any concurrent map or unmaps to the same IOVA range. This means that if there is an unmap that spans an entire page table, for example unmapping 2MiB, it is not our responsiblity, to care about whether or not there are any concurrent accesses to the L2 table that is being freed. This is explicitly different from challenge 1 that I mentioned above. It is allowed for concurrent map or unmaps to happen on different IOVA ranges, for example an unmapping to the first and second leaf entry in the L2 table.

That being said, are these empty page tables really a problem? So what if a couple pages are sitting around in memory not being used? To answer these questions, I went down the rabbit hole of address translation. Enough introduction, here is where the actual content begins.

So how bad can it get?

Pretend you are a malicious actor. If all you had was access to the IOMMU APIs iommu_map and iommu_unmap, how would you do the most damage to a system. It turns out, it’s not too hard to do some serious damage.