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 624B6C636D7 for ; Tue, 21 Feb 2023 19:26:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A77C46B0071; Tue, 21 Feb 2023 14:26:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A27CA6B0072; Tue, 21 Feb 2023 14:26:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8EFCC6B0073; Tue, 21 Feb 2023 14:26:39 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 7C9586B0071 for ; Tue, 21 Feb 2023 14:26:39 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 4033E1201A8 for ; Tue, 21 Feb 2023 19:26:39 +0000 (UTC) X-FDA: 80492280918.15.96ACF70 Received: from mail-pj1-f41.google.com (mail-pj1-f41.google.com [209.85.216.41]) by imf28.hostedemail.com (Postfix) with ESMTP id 5543CC000E for ; Tue, 21 Feb 2023 19:26:37 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=ohF3T5S0; spf=pass (imf28.hostedemail.com: domain of shy828301@gmail.com designates 209.85.216.41 as permitted sender) smtp.mailfrom=shy828301@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1677007597; 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=QkjmN3aXJEFk272e1HDNdSDs3L0hrkA2oPUmkyNMybk=; b=uP32xc1bEwPr+j+JTNQ4WgR+TMYQmRxzDJOpByLBMB8C6mh+zlAKEatlQCPeCui1O6i77A ORcuCi4I9f3O0yZQfm0DL1adSN6hk1I/JtFFtv6SWtnHgZm11Fg09KRSD9bT3FZbMwmIo9 PJBTNgRj+DUdmJ7neCONZIjefarAI9E= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=ohF3T5S0; spf=pass (imf28.hostedemail.com: domain of shy828301@gmail.com designates 209.85.216.41 as permitted sender) smtp.mailfrom=shy828301@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1677007597; a=rsa-sha256; cv=none; b=sreEcud/dQodd8EjcZFG8p7USv+a9foVsVQUxCWLXc2kp0vfHeoErm5Ck7LJz+E2hlkFWi Y+VMf/MG2Ooz8vOvWkOIPNka8R9S+whVrIh8m/B+asEWMNQV67HbZ/sjyD6ZTXT5Ll0Gwl yrKye8fF5lGN84tHVLdclpIkLv6o8fc= Received: by mail-pj1-f41.google.com with SMTP id m3-20020a17090ade0300b00229eec90a7fso60278pjv.0 for ; Tue, 21 Feb 2023 11:26:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=QkjmN3aXJEFk272e1HDNdSDs3L0hrkA2oPUmkyNMybk=; b=ohF3T5S077FB6e1Sl4MSuv9QBBXEG6TjNBG28P/8iCi5jQLGZMt6/Ny3BD7KDk5d58 JSbS8Y8tdhkV1c1bsqAzFazHb0Cv3Wu5TR+56wO5df2p7DWAMNsec+ThiM3QIHyCz6dH fCCFbHZ8arpWgxhHxayF2k1eyP8IWsYvd1WY8gRDfzUmSgh/4E5NPyEvn0F17Y4H4jYU sbctLZaJPiNks23Z9E9wFZMi/YGiZLbxKFhiQM3arNJlDHhWfiiCt0zQT2BMg1MPpozZ rBrWy6BUyhL8FNRtmFcJkROM9LAxLvYmuMf+3RuVnbKabw0GVZJDzX1gU9R8FOmHDBIZ /FHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=QkjmN3aXJEFk272e1HDNdSDs3L0hrkA2oPUmkyNMybk=; b=ZHSFeaoSp3jTHj+IWWN2DmGYUuJnCFNdABMrqbY4hzdHwXPiOt/wnPo3zr/MMs9aGa XcyqAsfgWLB03tj/YcKNhu17S219S1sCN0+5EwhdUPl6O9qbbBk8xMy1QxOn+AT9Spfv hW4NtBWABh2+7DozFgpAUBXli8GRopDs7lHjthkdHdnPPPCXPakfMxvxIG6RgctECGay F3yabrA2bQidhRztk2k1XZGxm4JOBQwcUrftqSfG9/8H5BOtnPT/Sd5ImaHOk9Pq32lk legC8yoT8egyQGN0woYo5wfIqzkmYC982TKD/LCwBGylik7vfR/Rhp679IgLQXUPO3J7 ZneQ== X-Gm-Message-State: AO0yUKUTOKyZALvCazWv0dabVrZgjrcX87bYL5Dzdzd8aGqBAIAdHimq ptWyG+zA8kLmjgFucOxnwEOVFquwB1FZQ4exrWc= X-Google-Smtp-Source: AK7set/hr9Lbw0ObwC1MN2xx6WUMiA8UYpyJU22Bq//KKj+eE52eFx3bHWiaYnaPAgQg/yt98z/1+8jCObQKqijhJVE= X-Received: by 2002:a17:90b:3b92:b0:233:e796:7583 with SMTP id pc18-20020a17090b3b9200b00233e7967583mr1608459pjb.1.1677007596035; Tue, 21 Feb 2023 11:26:36 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Yang Shi Date: Tue, 21 Feb 2023 11:26:24 -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-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 5543CC000E X-Stat-Signature: 1ha8inruram3oztgmib3x5aktwqces4h X-HE-Tag: 1677007597-81729 X-HE-Meta: U2FsdGVkX19f9Bj8IR1woPULAbIltLiOTQ4Pt3kc66EvOyycZxGuwKxA+R5S8vk8bg3UBM3AsZ96JbhUp7UofKg+nR91Gexce3pBG2xOuHUMwvS9kSz8ltMvqfAIs64QUAnDFy7jls3f3TpB6E6d4hNwPoHuUQgXVaLoaKQTy6j/XZYWhgwM3xZ1cHgPP1bYGBl+8ySaXbCAqrOHz/QK+Xa5GRPblOPr04KumX6tBlmDbp8nB2C76ZBLAT41EasaJjW5aF+xVDZKiHT6yzfyvGoXSHt40w3+z6W+HrOGq6AJUmcnf02n76HvWrYCFDnE5tj6OO5mFG0hIgrJbxTp4EbGt6+sHaX4BdRbop1MY1zVQ6+uX+xgM8JXq6N1CQTCGJnmk1yAtcYcQ+2+djw+9WT1DhHXHvmH2BlTdV+rJZM2e0zaZqMEOl1b66VI7qnJ7g5nqS412Z7/bDyaEhpuBipBsxZLwp8EQS/tTV/DKei/b9cDnQvDgxvjR29nu8pbUZXF7FxwmpL9siqvdiXOpG8nRQrgIPsQxn5ZXejaJV+mZ/XEXJe+Fz9Y66bbcsWLflFP8wrT55ejhFKLpc9Lxajb2QH5rW1R62dbVdinqCnpKEUYdlgy8uc1Z/QiM1G8JSVa3iE95pgtGsW4c3f76CiAGSR4eUu0WPJwLKO0xiYk6CNHpUjzxKSbh589HwvIJbadJ5YEX9OV2D1qQ0QXiWs6Via7NfqDWrZodwOocVPTLdwf8jxDXp07HwY90E/0jvj8KTk0S6G4fFmC5J8Emxrun0V3j8rX8wGaC9OKo9m8C1Qnzt2XeJT8OBuMU/8SKsvEhXj6pL+E8+MPe31lzFl84qFroAtFMI/dzbAH717zzIpcfQSbF4bTqV8JSGaJ0fJo/Tr7xLRcHC3mraNtzJR9a1+c6mGbO8f79ixuhsKJt5q18kd642GhKvAw9oGGCSLNI4RVgG8IZRUbkRq tWJlfBrt ZKWj5dJLtllTzSQ9iB5aH+D4bJOeN2UdyU8PrKp5XBBgz0F9AcBxhNPr1YjR3UkMdlW2BXTiSLIC4DOZ+GuFZ3lS+USaUIgreJcHg6Dog4xbwJHH1B38RkKTsfyIEZE1NNNokqhaB9I4p8tqf/funcugv56Iz9++S85c37HyXIR5TeFsfdkZ+CH/7GH6o9pytc3BbkZL+DVHZ1QAg0sG3YmJw7KGKtNniqfg0hgWHs+SvsgbtNTGRFJbIcDPlswA+VNEUzsoVFgxtfyZnS0q6T4105hYz365zmAM9TiTenS+pvX3T+i/ZglrQ2X1biG/g4toicC9kwzcmk4g9Lqq01EdKzuxnl7OPKt0mgAbq4FQZtZe9UZeVPizvnpi6s1G8byhIHnGM/pjmklU2UInVvJz4V8/Ox4WoD3hE 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: On Tue, Feb 21, 2023 at 10:56 AM Yosry Ahmed wrote: > > On Tue, Feb 21, 2023 at 10:40 AM Yang Shi wrote: > > > > 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. > > Great to see interested folks, thanks! > > > > > > > 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)? > > I imagine swap_desc's would be dynamically allocated when we need to > swap something out. When allocated, a swap_desc would either point to > a zswap_entry (if available), or a swap slot otherwise. In this case, > it would be 1:1 mapped to swapped out pages, not the swap slots on > devices. It makes sense to be 1:1 mapped to swapped out pages if the swapfile is used as the back of zswap. > > I know that it might not be ideal to make allocations on the reclaim > path (although it would be a small-ish slab allocation so we might be > able to get away with it), but otherwise we would have statically > allocated swap_desc's for all swap slots on a swap device, even unused > ones, which I imagine is too expensive. Also for things like zswap, it > doesn't really make sense to preallocate at all. Yeah, it is not perfect to allocate memory in the reclamation path. We do have such cases, but the fewer the better IMHO. > > WDYT? > > > > > > > > > ==================== 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? > > When we try to writeback from zswap, we try to allocate a swap slot in > the swapfile, and switch the swap_desc to point to that instead. The > process would be transparent to the rest of MM (page tables, page > cache, etc). If the swapfile is full, then there's really nothing we > can do, reclaim fails and we start OOMing. I imagine this is the same > behavior as today when swap is full, the difference would be that we > have to fill both zswap AND the swapfile to get to the OOMing point, > so an overall increased swapping capacity. When zswap is full, but swapfile is not yet, will the swap try to writeback zswap to swapfile to make more room for zswap or just swap out to swapfile directly? > > > > > Anyway I'm interested in attending the discussion for this topic. > > Great! Looking forward to discuss this more! > > > > > > > > > ==================== 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 :)