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 37C50C6FA8E for ; Fri, 3 Mar 2023 01:25:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B9BB76B0071; Thu, 2 Mar 2023 20:25:35 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B4AD16B0072; Thu, 2 Mar 2023 20:25:35 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9EB9B6B0073; Thu, 2 Mar 2023 20:25:35 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 8FE286B0071 for ; Thu, 2 Mar 2023 20:25:35 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 54DD91C6A45 for ; Fri, 3 Mar 2023 01:25:35 +0000 (UTC) X-FDA: 80525844630.22.08271E0 Received: from mail-pg1-f173.google.com (mail-pg1-f173.google.com [209.85.215.173]) by imf06.hostedemail.com (Postfix) with ESMTP id 668EE180011 for ; Fri, 3 Mar 2023 01:25:32 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=pZD0jF0W; spf=pass (imf06.hostedemail.com: domain of minchan.kim@gmail.com designates 209.85.215.173 as permitted sender) smtp.mailfrom=minchan.kim@gmail.com; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=kernel.org (policy=none) ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1677806732; h=from:from:sender: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=dYeC3FwTfGhfkjB/riHuzE3tvyC/Yh2+BGto8oPTis4=; b=SMubH0YgyUs6xLxXv2uFlpJtedr6JulzxN+VN4aBcWvuUuqVaw6saNxHOz9Q9rJ87QB59X zl4ESYTEyGZ4CEXUAfdHbdNAmGk+rJNQ4eI06p2kl22+2k5T9enLVCppq4ShStz0HChI/0 Ccpwilu5hf0X1JLRdXslkBVrDIguzX0= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=pZD0jF0W; spf=pass (imf06.hostedemail.com: domain of minchan.kim@gmail.com designates 209.85.215.173 as permitted sender) smtp.mailfrom=minchan.kim@gmail.com; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=kernel.org (policy=none) ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1677806732; a=rsa-sha256; cv=none; b=IHeXyrkuPcGZlav0a1Wx1vEWi29Q7cW4kK0sROPnbWH6nPVSibUrt5NALJWzcOBt3CjcI1 /zxVMKUjg87u0p1GxHk6s7j05RUFBA6iXuFaW6AZtWTyHBay7cCIyVdmsYKyhmXDS+2nYt 3oYohkSlRxOPo+kdfk6jBUyuOPfJiOY= Received: by mail-pg1-f173.google.com with SMTP id z10so586284pgr.8 for ; Thu, 02 Mar 2023 17:25:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1677806731; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:sender:from:to:cc:subject:date:message-id :reply-to; bh=dYeC3FwTfGhfkjB/riHuzE3tvyC/Yh2+BGto8oPTis4=; b=pZD0jF0WlwO9bDFc6t99ZP/9WGwTZw1WzP33ahfzdDfB0oA71km+e+PmzMlKoNYBbI V/PFGNt4OjZuKBmE+s/RSs/eFSki/v/fwOJ+9Ew5FYwDkQdtQeJpnb8fUbLixcmhz0Af tZimXblFihyHeWE+GaMLGmASsB4YOZUDylzsLl7i3qedlHeBrEv+ZrDg1tDshkN3DlaD Anik9USjOBRK64htw3Z02fW9SV2IBBte+xp5+YtTR2SUA6XVxsWk4wOZMA1eJNKRNzNv 3+gyu0hzUQRxz1l8CJdD+aEPBWzDzWAlcusCH4tR++aiMYeMIvT0yK5XsB6XOtPKhx8e maaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677806731; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:sender:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=dYeC3FwTfGhfkjB/riHuzE3tvyC/Yh2+BGto8oPTis4=; b=pFnT4mIjkh+LuGtrcJSqigHt9NszCN/0cUTHCm6JwObK/ZU2uU7B5JIGQNo19yojFD B9fBa41WStfIpiCoP2pR3Dwrpm+BaYLbyDnlQJqBwtbLJabrxF7RLY+R1xcOYiawYcq/ mE367q/lMBsG9Xjr6J3u9j06Ay+cpWXM/68HAfcm9PgJAAzaHSY5jTLqafwnfF/CGTSB wGKBWApcUYcrmb94O+ulhVSviysg+7WOsWbhjGHCx8Z+MiufNrTred6uCHMvDBGzPcbS Ooy7XheDbmBle8hRSIHdolfVeXxIAOqrcsCJD5DmEB5PdEIBdF1dYQtqfknGRpc0s4e4 XYpA== X-Gm-Message-State: AO0yUKVbPq4yAWDZdDyyc3s75rCgLteyzdtTXHBqLc7nopii9JVK+e+v nIvTped1fG31wv2/n+B1Okg= X-Google-Smtp-Source: AK7set82hE4raxWNYdZJOX9t8+0hAkF7D/HeMN+ZrACasfiynj1duOkHYq37hcmNb2s5CRbRiDvKrQ== X-Received: by 2002:aa7:9504:0:b0:5db:ba06:1825 with SMTP id b4-20020aa79504000000b005dbba061825mr475512pfp.3.1677806731168; Thu, 02 Mar 2023 17:25:31 -0800 (PST) Received: from google.com ([2620:15c:211:201:4036:e990:6bc4:4206]) by smtp.gmail.com with ESMTPSA id 5-20020aa79205000000b00593ce7ebbaasm293124pfo.184.2023.03.02.17.25.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Mar 2023 17:25:30 -0800 (PST) Date: Thu, 2 Mar 2023 17:25:28 -0800 From: Minchan Kim To: Yosry Ahmed Cc: Chris Li , lsf-pc@lists.linux-foundation.org, Johannes Weiner , Linux-MM , Michal Hocko , Shakeel Butt , David Rientjes , Hugh Dickins , Seth Jennings , Dan Streetman , Vitaly Wool , Yang Shi , Peter Xu , Andrew Morton Subject: Re: [LSF/MM/BPF TOPIC] Swap Abstraction / Native Zswap Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 668EE180011 X-Rspam-User: X-Stat-Signature: 3masfdr8kz8uc1yxze5gdehi6wuu7aqt X-HE-Tag: 1677806732-69672 X-HE-Meta: U2FsdGVkX19KVkpozlSINv5rL1upZ7TN+KMsCWJgPN+N649z1k9/Ep7B54xLSp3PlmCgXYDkUYJYyK2y+uHYHDwEvEkvi43RvlDlcaWgYCYCcrB2h8Llg2yiuawUxpn9oiDRNhSfiiAeYoKebac2OevIgFn+E8aM7FFcnXX/Nh/lNid8CVhD29UAnSYm0npfMiVjGu12VHMn43UKhiw/3Y/GwdfNJ2cBNrcWTE7lvsCs2P6GVCAYd6PYIS4t8V56Vx2BSnBbszh2l0XyMQcUD3IBYIwA0vr+umigIoy+2BS4qQn82QccgE3+qNfic4A1tVK0cblybb7QLxm0aqFJraA1yKZ4orq3KpSN/ZXgrIneh6hXvdEUBELzOPxeAJIEduzrebZCEQDRHwR1ae3x/RTIM/s81VIzhE7wU07s6Ki2YIKtNtaqkVztsIbtNcZmcbRPq8/0K6UburcNM+GEZzvA63SqzU+KxKAI+8yYwPSE6YSq8kkOws5Op63Cz5XVrXUompYOozuGD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B7JC8L7ymMxSMbP1j4rbAhvyhzdpJgRt7BP5TGq8WecP5CWk5QS+Mu0tLuAw== 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 Thu, Mar 02, 2023 at 04:49:01PM -0800, Yosry Ahmed wrote: > On Thu, Mar 2, 2023 at 4:33 PM Minchan Kim 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 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. Thanks.