The [vx]swap showdown
The kernel's zswap layer highlights the difficulties with the current design of the swap subsystem. Zswap intercepts folios that are to be pushed out to a swap file and, instead of writing them to that file, compresses their contents and stores the result in RAM. It is, essentially, a way of trading some CPU time for more free memory while keeping data close to hand and minimizing disk I/O. Use of zswap has become increasingly popular with current workloads.
The problem is that every folio stored in zswap must still have a slot assigned in a persistent swap device. If the zswap area overflows, folios may be flushed out into their assigned (persistent) slots, but normally the intent is for that to not happen. So, most of the time, that storage space is entirely wasted. It must, however, be present for zswap to be used. This problem is, it seems, especially vexing in large data centers, where massive amounts of flash storage must be dedicated to storing nothing.
One proposed solution to this problem, vswap by Nhat Pham, was described in this article. Vswap adds a new virtualization layer on top of the existing swap devices; when a folio is added to the swap cache, it is assigned a slot in this virtual layer. That slot can then be mapped to a number of different locations, including a slot in any physical swap file, a slot in zswap, or, for pages containing only zeroes, no slot at all. Folios can move between backing devices while retaining their virtual slot, making it easy to flush folios out of zswap or to evacuate a swap file prior to its removal. An xarray data structure is used to track the slots. This work has been through numerous revisions as the swap layer has changed underneath it; vswap was last posted in mid-September.
The alternative is xswap, by Baoquan He and Chris Li. In its mid-September form (it has since been updated), xswap can be thought of as a virtual swap device that does not actually have any backing store. Attempts to write folios to an xswap device do not succeed, so the device cannot actually store any data. What it can do is to provide a "physical" swap slot for folios stored in zswap. On a system where xswap is the only configured swap device, folios can be stored in zswap with no physical storage needed. Xswap uses a sparsely mapped address range for its housekeeping data, minimizing its memory use when swap usage is light.
Toward the beginning of September, Pham, who has been working on vswap for over a year, posted a lengthy query, asking how work on a virtualized swap layer could move forward. He summarized both the vswap and xswap work, noting that both projects had their advantages. For example, he said, the sparsely mapped housekeeping used by xswap was perhaps more attractive than the xarray he used for vswap.
Pham suggested that parts of both projects could eventually be merged, leading to the question of which should go first. Perhaps unsurprisingly, he suggested that vswap should be applied ahead of xswap; the latter, he suggested, can be seen mostly as an optimization that could be adopted once the major design elements of vswap were in place.
Equally unsurprisingly, the xswap developers argued that it would make more
sense to merge xswap first. "The VM_SPARSE array is not an optimization
to bolt on later; it is a structural choice, and the code reflects it
".
If vswap were to be applied first, He said, its xarray-based data
structures would eventually have to be replaced with the xswap
implementation. Better to add the relatively simple "foundation
"
that is xswap than to add the extra complexity of vswap only to remove it
shortly thereafter. Kairui Song, who has done much of the
swap-modernization work to date, echoed
some of these arguments.
While no decisions were made in the extensive ensuing discussion, it
quickly became clear that there was not a groundswell of support for xswap.
The fact that xswap lacked a way to write back folios from zswap to a
physical swap device was seen as a severe shortcoming that did not exist in
vswap. The most contentious issue, though, stemmed from the sparsely
mapped housekeeping area that is the core "structural choice
"
underlying xswap.
Specifically, that area requires that its full address space be reserved at the outset; that, in turn, sets a maximum size for the xswap device and, consequently, the maximum number of folios that can be stored in zswap. The size of the swap area presented by vswap, instead, can grow or shrink dynamically as needed by the workload. Johannes Weiner argued that requiring administrators to set the size of the swap area is inherently mistaken; how much memory can be stored in zswap depends heavily on how effective its compression is, which will be dependent on the workload. There is no way for an administrator to know what the proper size is, and no reason for the kernel to apply limits beyond those that already control memory use, he said.
This part of the discussion went on for a while, and it became clear that the limits applied to swap space (both in device sizing and with control groups) were being used in different and somewhat contradictory ways. Some sites use the limits to regulate the maximum virtual size of processes, while others are interested specifically in controlling the use of physical swap space. In the former case, charging zswap pages against the limits makes sense; in the latter case, it does not. Once zswap is separated from physical swap slots, developers will have to decide which use case to support, and somebody seems sure to go away unhappy. This message from Gregory Price describes the disconnect (and a possible resolution) in some detail.
The conversation eventually slowed down without any clear conclusions. An ongoing effort to set up a conference call to discuss the issue has not yet found success. In the meantime, He has posted a new xswap implementation in three parts. Part 1 is the core swap-device implementation as described above, part 2 implements the physical writeback that was missing from previous revisions, and part 3 implements the accounting for use of physical swap space. So far, the only commentary is this message from Pham, who was unhappy about the perceived use of his ideas without attribution.
The evolution of the kernel's swap subsystem has been rapid over the last
year, after many years of relative stasis. The virtualization of the swap
space is a logical next step that will resolve a number of longstanding
problems. But the memory-management developers will want to be sure that
they are getting it right; they will have to live with the consequences of
their decisions for years to come. Changes in this part of the kernel
often take a long time to land even in the absence of disagreement over
fundamental details; this one may take longer than usual.
| Index entries for this article | |
|---|---|
| Kernel | Memory management/Swapping |
