| 1 |
.. SPDX-License-Identifier: GPL-2.0 |
| 2 |
|
| 3 |
:Author: Chris Li <[email protected]>, Kairui Song <[email protected]> |
| 4 |
|
| 5 |
========== |
| 6 |
Swap Table |
| 7 |
========== |
| 8 |
|
| 9 |
Swap table implements swap cache as a per-cluster swap cache value array. |
| 10 |
|
| 11 |
Swap Entry |
| 12 |
---------- |
| 13 |
|
| 14 |
A swap entry contains the information required to serve the anonymous page |
| 15 |
fault. |
| 16 |
|
| 17 |
Swap entry is encoded as two parts: swap type and swap offset. |
| 18 |
|
| 19 |
The swap type indicates which swap device to use. |
| 20 |
The swap offset is the offset of the swap file to read the page data from. |
| 21 |
|
| 22 |
Swap Cache |
| 23 |
---------- |
| 24 |
|
| 25 |
Swap cache is a map to look up folios using swap entry as the key. The result |
| 26 |
value can have three possible types depending on which stage of this swap entry |
| 27 |
was in. |
| 28 |
|
| 29 |
1. NULL: This swap entry is not used. |
| 30 |
|
| 31 |
2. folio: A folio has been allocated and bound to this swap entry. This is |
| 32 |
the transient state of swap out or swap in. The folio data can be in |
| 33 |
the folio or swap file, or both. |
| 34 |
|
| 35 |
3. shadow: The shadow contains the working set information of the swapped |
| 36 |
out folio. This is the normal state for a swapped out page. |
| 37 |
|
| 38 |
Swap Table Internals |
| 39 |
-------------------- |
| 40 |
|
| 41 |
The previous swap cache is implemented by XArray. The XArray is a tree |
| 42 |
structure. Each lookup will go through multiple nodes. Can we do better? |
| 43 |
|
| 44 |
Notice that most of the time when we look up the swap cache, we are either |
| 45 |
in a swap in or swap out path. We should already have the swap cluster, |
| 46 |
which contains the swap entry. |
| 47 |
|
| 48 |
If we have a per-cluster array to store swap cache value in the cluster. |
| 49 |
Swap cache lookup within the cluster can be a very simple array lookup. |
| 50 |
|
| 51 |
We give such a per-cluster swap cache value array a name: the swap table. |
| 52 |
|
| 53 |
A swap table is an array of pointers. Each pointer is the same size as a |
| 54 |
PTE. The size of a swap table for one swap cluster typically matches a PTE |
| 55 |
page table, which is one page on modern 64-bit systems. |
| 56 |
|
| 57 |
With swap table, swap cache lookup can achieve great locality, simpler, |
| 58 |
and faster. |
| 59 |
|
| 60 |
Locking |
| 61 |
------- |
| 62 |
|
| 63 |
Swap table modification requires taking the cluster lock. If a folio |
| 64 |
is being added to or removed from the swap table, the folio must be |
| 65 |
locked prior to the cluster lock. After adding or removing is done, the |
| 66 |
folio shall be unlocked. |
| 67 |
|
| 68 |
Swap table lookup is protected by RCU and atomic read. If the lookup |
| 69 |
returns a folio, the user must lock the folio before use. |