On Thu, Mar 2, 2023 at 5:25 PM Minchan Kim <minchan@xxxxxxxxxx> wrote: > > On Thu, Mar 02, 2023 at 04:49:01PM -0800, Yosry Ahmed wrote: > > On Thu, Mar 2, 2023 at 4:33 PM Minchan Kim <minchan@xxxxxxxxxx> wrote: > > > > > > On Wed, Mar 01, 2023 at 04:30:22PM -0800, Yosry Ahmed wrote: > > > > On Tue, Feb 28, 2023 at 3:11 PM Chris Li <chrisl@xxxxxxxxxx> wrote: > > > > > > > > > > Hi Yosry, > > > > > > > > > > On Sat, Feb 18, 2023 at 02:38:40PM -0800, Yosry Ahmed wrote: > > > > > > Hello everyone, > > > > > > > > > > > > I would like to propose a topic for the upcoming LSF/MM/BPF in May > > > > > > 2023 about swap & zswap (hope I am not too late). > > > > > > > > > > I am very interested in participating in this discussion as well. > > > > > > > > That's great to hear! > > > > > > > > > > > > > > > ==================== Objective ==================== > > > > > > Enabling the use of zswap without a backing swapfile, which makes > > > > > > zswap useful for a wider variety of use cases. Also, when zswap is > > > > > > used with a swapfile, the pages in zswap do not use up space in the > > > > > > swapfile, so the overall swapping capacity increases. > > > > > > > > > > Agree. > > > > > > > > > > > > > > > > > ==================== Idea ==================== > > > > > > Introduce a data structure, which I currently call a swap_desc, as an > > > > > > abstraction layer between swapping implementation and the rest of MM > > > > > > code. Page tables & page caches would store a swap id (encoded as a > > > > > > swp_entry_t) instead of directly storing the swap entry associated > > > > > > with the swapfile. This swap id maps to a struct swap_desc, which acts > > > > > > > > > > Can you provide a bit more detail? I am curious how this swap id > > > > > maps into the swap_desc? Is the swp_entry_t cast into "struct > > > > > swap_desc*" or going through some lookup table/tree? > > > > > > > > swap id would be an index in a radix tree (aka xarray), which contains > > > > a pointer to the swap_desc struct. This lookup should be free with > > > > this design as we also use swap_desc to directly store the swap cache > > > > pointer, so this lookup essentially replaces the swap cache lookup. > > > > > > > > > > > > > > > as our abstraction layer. All MM code not concerned with swapping > > > > > > details would operate in terms of swap descs. The swap_desc can point > > > > > > to either a normal swap entry (associated with a swapfile) or a zswap > > > > > > entry. It can also include all non-backend specific operations, such > > > > > > as the swapcache (which would be a simple pointer in swap_desc), swap > > > > > > > > > > Does the zswap entry still use the swap slot cache and swap_info_struct? > > > > > > > > In this design no, it shouldn't. > > > > > > > > > > > > > > > This work enables using zswap without a backing swapfile and increases > > > > > > the swap capacity when zswap is used with a swapfile. It also creates > > > > > > a separation that allows us to skip code paths that don't make sense > > > > > > in the zswap path (e.g. readahead). We get to drop zswap's rbtree > > > > > > which might result in better performance (less lookups, less lock > > > > > > contention). > > > > > > > > > > > > The abstraction layer also opens the door for multiple cleanups (e.g. > > > > > > removing swapper address spaces, removing swap count continuation > > > > > > code, etc). Another nice cleanup that this work enables would be > > > > > > separating the overloaded swp_entry_t into two distinct types: one for > > > > > > things that are stored in page tables / caches, and for actual swap > > > > > > entries. In the future, we can potentially further optimize how we use > > > > > > the bits in the page tables instead of sticking everything into the > > > > > > current type/offset format. > > > > > > > > > > Looking forward to seeing more details in the upcoming discussion. > > > > > > > > > > > > ==================== Cost ==================== > > > > > > The obvious downside of this is added memory overhead, specifically > > > > > > for users that use swapfiles without zswap. Instead of paying one byte > > > > > > (swap_map) for every potential page in the swapfile (+ swap count > > > > > > continuation), we pay the size of the swap_desc for every page that is > > > > > > actually in the swapfile, which I am estimating can be roughly around > > > > > > 24 bytes or so, so maybe 0.6% of swapped out memory. The overhead only > > > > > > scales with pages actually swapped out. For zswap users, it should be > > > > > > > > > > Is there a way to avoid turning 1 byte into 24 byte per swapped > > > > > pages? For the users that use swap but no zswap, this is pure overhead. > > > > > > > > That's what I could think of at this point. My idea was something like this: > > > > > > > > struct swap_desc { > > > > union { /* Use one bit to distinguish them */ > > > > swp_entry_t swap_entry; > > > > struct zswap_entry *zswap_entry; > > > > }; > > > > struct folio *swapcache; > > > > atomic_t swap_count; > > > > u32 id; > > > > } > > > > > > > > Having the id in the swap_desc is convenient as we can directly map > > > > the swap_desc to a swp_entry_t to place in the page tables, but I > > > > don't think it's necessary. Without it, the struct size is 20 bytes, > > > > so I think the extra 4 bytes are okay to use anyway if the slab > > > > allocator only allocates multiples of 8 bytes. > > > > > > > > The idea here is to unify the swapcache and swap_count implementation > > > > between different swap backends (swapfiles, zswap, etc), which would > > > > create a better abstraction and reduce reinventing the wheel. > > > > > > > > We can reduce to only 8 bytes and only store the swap/zswap entry, but > > > > we still need the swap cache anyway so might as well just store the > > > > pointer in the struct and have a unified lookup-free swapcache, so > > > > really 16 bytes is the minimum. > > > > > > > > If we stop at 16 bytes, then we need to handle swap count separately > > > > in swapfiles and zswap. This is not the end of the world, but are the > > > > 8 bytes worth this? > > > > > > > > Keep in mind that the current overhead is 1 byte O(max swap pages) not > > > > O(swapped). Also, 1 byte is assuming we do not use the swap > > > > > > Just to share info: > > > > > > Android usually used swap space fully most of times via Compacting > > > background Apps so O(swapped) ~= O(max swap pages). > > > > Thanks for sharing this, that's definitely interesting. > > > > What percentage of memory is usually provisioned as swap in such > > cases? Would you consider an extra overhead of ~8M per 1G of swapped > > memory particularly unacceptable? > > Vendors have different sizes and usually decide by how many apps > they want to cache at the cost of foreground app's trouble. > I couldn't speak for all vendors but half of DRAM may be small > size on the market. > > I recall the ~80M less free memory made huge differene of jank > ratio for memory hungry app launchhing on the memory pressure > state so we needed to cut down the additional memory in the end. > > I cannot say the ~8M per 1G is acceptible or not since it's > depends on workload with the device's RAM size(worried more on > entry level devices) so I am not sure what's the code complexity > we will bring in the end but considering swap metadata is one of > the largest area from memory consumption view(struct page is working > toward to shrink - folio, Yay!), IMHO, it's worthwhile to see > whether we could take the complexity. We can do something similar to what Chris suggested, and have the swap entries act the same way as today if frontswap/zswap is not configured. If frontswap/zswap is configured, all swap entries go through frontswap, we can have an xarray there (instead of today's rbtree) that either points to a zswap entry or a swap entry. So the indirection lives in frontswap essentially. A few problems with this alternative approach vs. the initial proposal: a) The abstraction layer is within frontswap/zswap, so we cannot move the writeback LRU logic outside zswap like you mentioned before to support different combinations of swapping backends. b) Today we do one lookup in the fault path (in the swapcache) for swapfiles, and 2 lookups for zswap (an extra lookup in the zswap rbtree). The initial proposal allows us to remove the rbtree and have a single lookup in both cases to get the swap_desc, in which the swapcache and zswap_entry are just pointers we can access directly. Furthermore, adding the swapped in page to the swapcache is an O(1) operation now instead of storing into an xarray (IIUC we did an optimization to skip the swapcache for zram single-mapping fault to avoid this). With the alternative approach, we have to do 2 lookups in the fault path if zswap is enabled (one in the swapcache, and one in the zswap tree to get the underlying zswap_entry / swap entry). c) The initial proposal allows us to simplify the swap counting and swapcache logic, and perhaps reduce the locking we have to do (one lock to update the swapcache, one lock to update swap count). I even think we might be able to do such updates locklessly with atomic operations on the swap_desc. With the alternative approach, we need to have swap counting logic in swapfiles (the one we have today), and another swap counting logic in zswap. When a page is moved from zswap to swapfile, we may need to hand over the swap count. Instead of simplifying the complicated swap counting logic we double down by having two implementations. I am not saying one or the alternative approach is worse, I am just saying there is more to it than complexity vs. memory savings. > > Thanks. >