From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7EB97C636D7 for ; Tue, 21 Feb 2023 18:40:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0D2726B0075; Tue, 21 Feb 2023 13:40:12 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 033C56B007B; Tue, 21 Feb 2023 13:40:11 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E183F6B007D; Tue, 21 Feb 2023 13:40:11 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id CBEB96B0075 for ; Tue, 21 Feb 2023 13:40:11 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 886651C6371 for ; Tue, 21 Feb 2023 18:40:11 +0000 (UTC) X-FDA: 80492163822.29.643EB1B Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by imf11.hostedemail.com (Postfix) with ESMTP id 7DAF540004 for ; Tue, 21 Feb 2023 18:40:09 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=TU9RoLSX; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf11.hostedemail.com: domain of shy828301@gmail.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=shy828301@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1677004809; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=JO3vhEzu5jjWFDer962uc+qstblkzvaa0PtOkrsieuY=; b=g7M0FnqaUz6pB4lSSZ75TsxawyF+rBXwyluCIajmh9qVGg7ACkN8swoEz7eefSmGWOO7FR zVsw/tdcjZcv+3zdKmAqX4cR3FfGvMwO0YgUulFO9/ezP+Azz5KnAh+A8mC1j+tD7SiUlk LGNJTAVHgTYWrxUKHo4zdEksn3+LNb4= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=TU9RoLSX; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf11.hostedemail.com: domain of shy828301@gmail.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=shy828301@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1677004809; a=rsa-sha256; cv=none; b=kd3OKlwDtdBtJ31hEUTKnhCiHYef6k9IXt8MHsXCYy2qixl47tXbV2aPGyg4mshtxMaQ8V Ehz7sXnhhynIKtMsr4YwK+FzXdVqxZe1/mkEUf4QlHwRHYnGcxh52fr8nEithQF+c62UwQ IUqUN/StUft/L964nlbEh+Pc9Bfru5c= Received: by mail-pl1-f182.google.com with SMTP id c1so6317707plg.4 for ; Tue, 21 Feb 2023 10:40:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1677004808; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=JO3vhEzu5jjWFDer962uc+qstblkzvaa0PtOkrsieuY=; b=TU9RoLSXm891Vw9zGOInnl1IMvshP7CNdxZuoaCVW0xGdn98geOumWh5hkJSfcF7rB 0YVIHUjV3XhJ2QYoA6USvZG3LPVFoEOOVpJOhrt30l+mwzjt2f6J5ZLAJyHGvSLdWqPE O7tn+oPZSK8TJa5dbsmool+PZRvFFcOhzpSkdBibWBU7F9r19lY/T/pLIthFRxkCuUU+ Qo/+jNkXQzNrRquZcIDgIR+BsITRCFYmjna6MgkFWpgEW4Wj4CRLdVDQftNqHeXjloPW CChHX/w7e7hPPqD90cV3zMDapGE9RJGvvznfxgzGkyD0clVEIxS4OWQhpVhCRxuVDHQl YTjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677004808; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=JO3vhEzu5jjWFDer962uc+qstblkzvaa0PtOkrsieuY=; b=XUXslZfZDhUucrgQfduDygVr6qollGYMxtoTV0Nmct+DSUurFUjOCC2m+g1zC96ZjK v92tUZf98Hw8JaIYvO4Rz++2SyG/gycnytiEhUGUyJAedVo5vJj2SOGTElsJ7LRCYZSv V0ZnlzZP49Gz7r5m2k8YJmItDeZBERkWhITeJ1HQxJs6kKWwG47l9+8GkTvveCBZCDql p/gkfGCSSXX1swgP7t6F42N+eQYDb4fA+gQ8hO5kbMAunEwVG/6s3SB7l0TLLJf9dLw6 UX7l43S7QAA4DgmVbAXqz81BLSNxRTZmlR+8dDSUl0HZJk1dtE9yXEMLdE1OPoT22nix 2uqQ== X-Gm-Message-State: AO0yUKUGs/uXCTFgqPjalTdr8lyBDyqy/PN29ZlSR1OApdpSuOiCsNGU Xjfi+11geQiAGt7UsoKa0YE+Pn3QRv4hcFLTSUU= X-Google-Smtp-Source: AK7set+rqWLJ7GhHJ5REcvpm2bTex3bELTWwj1+znYCVUPydb2zUwk+pFdS8c9tYkQINLYC2PdpU9ZX3vFZDoepUXm4= X-Received: by 2002:a17:902:d48e:b0:19c:2b1c:4d41 with SMTP id c14-20020a170902d48e00b0019c2b1c4d41mr763574plg.28.1677004808022; Tue, 21 Feb 2023 10:40:08 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Yang Shi Date: Tue, 21 Feb 2023 10:39:56 -0800 Message-ID: Subject: Re: [LSF/MM/BPF TOPIC] Swap Abstraction / Native Zswap To: Yosry Ahmed Cc: lsf-pc@lists.linux-foundation.org, Johannes Weiner , Linux-MM , Michal Hocko , Shakeel Butt , David Rientjes , Hugh Dickins , Seth Jennings , Dan Streetman , Vitaly Wool , Peter Xu , Minchan Kim , Andrew Morton Content-Type: text/plain; charset="UTF-8" X-Rspamd-Queue-Id: 7DAF540004 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: 9hjwwf4wyx8qousiiu6wxdckbidf9u58 X-HE-Tag: 1677004809-792214 X-HE-Meta: U2FsdGVkX1/OmJajSYpJRnta/EZ0jSCTupsuOFqUJxIl4RuGfFa5T1gIgrjC9OecOW5VyPuzLAB3NFAkg1ja2V3B3uVxbraMG4LRiSNj0cB/WH5DGpOjNWbtk6TO8W5/PQVmHu/ulOhAXQza6P8Atu6996Osx+/GjYIWHQ3dGLj6Ca//jWtZo9kWeJG8FeaFJjDC+9z4U4pcvEYfsgSLpOg3oEChEsExpVEHX5pUJbsdtGMX1d9ehMzIayGLpW3pC4ixNU2sQW4vZOWtqAgdtknq37PeAnLyrPaKYHuij6VSZZqZkyNrpxMTemmr/Ix8UMsURd8+3dcXuobTC4+dIenpR74E/3nhnfuCZKEE2/pl8Zolma19+ZKZFi7uGIM5V7f5HRKmgKkTI2IaoK/GRVlk6Y5ePm90aW0U3+g9amd4KLUJjX8OeMaV/Ds8iikmtSi7aS0lG41kLtp2g6yZR4bJsNFFj2nHitqIObViRcvCwDOtuQTxydJuo94MvGEr+RMzjc4wNmYtjLb3kw+ZIEeTMFQ+1xInQan2BEeSjPXbk/IBOHoMypS9qTW05Cv0WRy3qRcPRNuG+kLAgly3cwNPD9bJcJR1UpfzKxIFSa9B99+9QiFUyqb2sx3nU9aZI5PHJOwcOMtXhEAhrTsoJ9CqVnGXb/SfOlU/9yVYLZH5LVNt7xd/mVUCnmvFMXDYwMMcN+BIv6hcz+cyVmLrIMV9hpTN11LYgMMCWhaKOYZzwBxcRQuGXHo6YGduqsPnmN62e2cufTVjBhwBjeczO/7QMv4QOQZcq4oX7Mxtf7nQuqjhqOcgMsH5MV7F7zovCp5QXn0P/AW2hPlCa3g/eVSr5+uuwWMiUgzy5qjYqF6oCTXSn/UAnRxIzrOrDdiUW16osUUCbzPH0SuzINTXEBXOTyQNmP07QIJJ7Iz/rbYg5ymAKweT/WByazKR/FRzQz3JeemSPOvV5Ug+2CI Dc/R1CCq +44KVX49ncXd+IkKaow2cMRy0IQgw6oovL7G+xApgtHPMvQCKkaxzliIf9IUqfoBHxnRfzbBHnqGKe4QhenjjxmTFKjGUYnbmZ4mcUZmkB0XQH2wQ0lxvkGP2lGmb3TRtDca8zWWDant/G597Efws+Me0x/wRROfsNwPS87IVQAXTP7iVpEQVoBbsYvricnulYnjfLUxMsy9bO9fXUViSkzmiGo+Ig0Nq/BgCougWGjNjwQKR24ve/wkAikdjLRpmkaUAMw9Y5RBlsDLVFBu0xZGWfYPX7Kmf5DIwITKHJsq4XkHV7KvhnSjuL49YsWuYWCtBB2eBtRHNc0TJAcLhI6Qdh71LweaP2fINXKc6rLPCWl8lKcglXnt0t680xA0kNj2ZWXz9UCjxhCY= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Hi Yosry, Thanks for proposing this topic. I was thinking about this before but I didn't make too much progress due to some other distractions, and I got a couple of follow up questions about your design. Please see the inline comments below. On Sat, Feb 18, 2023 at 2:39 PM 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). > > ==================== Intro ==================== > Currently, using zswap is dependent on swapfiles in an unnecessary > way. To use zswap, you need a swapfile configured (even if the space > will not be used) and zswap is restricted by its size. When pages > reside in zswap, the corresponding swap entry in the swapfile cannot > be used, and is essentially wasted. We also go through unnecessary > code paths when using zswap, such as finding and allocating a swap > entry on the swapout path, or readahead in the swapin path. I am > proposing a swapping abstraction layer that would allow us to remove > zswap's dependency on swapfiles. This can be done by introducing a > data structure between the actual swapping implementation (swapfiles, > zswap) and the rest of the MM code. > > ==================== 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. > > ==================== 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 > 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 > counting, etc. It creates a clear, nice abstraction layer between MM > code and the actual swapping implementation. How will the swap_desc be allocated? Dynamically or preallocated? Is it 1:1 mapped to the swap slots on swap devices (whatever it is backed, for example, zswap, swap partition, swapfile, etc)? > > ==================== Benefits ==================== > 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. > > Another potential win here can be swapoff, which can be more practical > by directly scanning all swap_desc's instead of going through page > tables and shmem page caches. > > Overall zswap becomes more accessible and available to a wider range > of use cases. How will you handle zswap writeback? Zswap may writeback to the backed swap device IIUC. Assuming you have both zswap and swapfile, they are separate devices with this design, right? If so, is the swapfile still the writeback target of zswap? And if it is the writeback target, what if swapfile is full? Anyway I'm interested in attending the discussion for this topic. > > ==================== 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 > a win (or at least even) because we get to drop a lot of fields from > struct zswap_entry (e.g. rbtree, index, etc). > > Another potential concern is readahead. With this design, we have no > way to get a swap_desc given a swap entry (type & offset). We would > need to maintain a reverse mapping, adding a little bit more overhead, > or search all swapped out pages instead :). A reverse mapping might > pump the per-swapped page overhead to ~32 bytes (~0.8% of swapped out > memory). > > ==================== Bottom Line ==================== > It would be nice to discuss the potential here and the tradeoffs. I > know that other folks using zswap (or interested in using it) may find > this very useful. I am sure I am missing some context on why things > are the way they are, and perhaps some obvious holes in my story. > Looking forward to discussing this with anyone interested :) > > I think Johannes may be interested in attending this discussion, since > a lot of ideas here are inspired by discussions I had with him :)