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 611DEC6FD19 for ; Thu, 16 Mar 2023 07:51:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D9D756B007B; Thu, 16 Mar 2023 03:51:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D273C6B007D; Thu, 16 Mar 2023 03:51:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B79106B007E; Thu, 16 Mar 2023 03:51:41 -0400 (EDT) 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 A00F96B007B for ; Thu, 16 Mar 2023 03:51:41 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 689FD407C2 for ; Thu, 16 Mar 2023 07:51:41 +0000 (UTC) X-FDA: 80573992002.09.4E41574 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by imf19.hostedemail.com (Postfix) with ESMTP id 5B70D1A0003 for ; Thu, 16 Mar 2023 07:51:38 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=CC10tHnL; spf=pass (imf19.hostedemail.com: domain of ying.huang@intel.com designates 192.55.52.115 as permitted sender) smtp.mailfrom=ying.huang@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1678953099; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ELUXUCzUfUHiyTmdLHrNofc0TlMqTjm73U6OVTDKwbA=; b=FjBn8ObSuDwNwGLbA+DFMe9B0qneYAEzYYCdAs5szVmdZEcd0bD++MGmRJb0T7FIz6jtL+ cNjDlxdoIwbhl0HkPbQI4gGVVsi/bHgbPCV6ilWK51CL2u0hUwmpjKpl0I92khid35fkeR 6Ywl009rvYuHDLXvatCdy+gyiav3xXE= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=CC10tHnL; spf=pass (imf19.hostedemail.com: domain of ying.huang@intel.com designates 192.55.52.115 as permitted sender) smtp.mailfrom=ying.huang@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1678953099; a=rsa-sha256; cv=none; b=rljxqHDeGy8Aey0MTagKlm13GPEOQQAk2LfTPteop/SGYJah7x9VsmEFRw/q25kz1N5Yjl ncHLtowmGxgWVLcFouJX2j3NNwUYEe6pzfM07lgpsQCJe8l0QgaXvWjBQjgoQ7YbGX+RGx E5IjT3gr6wHF77EDLs7tTG7nmjbrHno= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1678953098; x=1710489098; h=from:to:cc:subject:references:date:in-reply-to: message-id:mime-version:content-transfer-encoding; bh=VxHeUplewIjPZC9dvGzk+Z4S1Pe4HYqxSKVnfOo+Fwg=; b=CC10tHnLK8TH2TywjePJmGlotPiBJU4TyeJf7rdlzQhmNVBQ6C9be+/z zZ3giEvROg0hFhbgKU32VKtIK8lewVURX2WWA9RFQnq9YSfXQRcefP/09 LLvnih7473aGWQ7dVsmo9uQlicecuI5hSOT6ByZcaqDkJ34EF6jirGq1x Rm+arXSsxhi7sxKwYSOQt++FTr1NafIYblhUP49l++0XOBKEdy7w1ORjr obH6su/r9VQqvXkFfoDSd7HTZ0Hb0eruaHz1HavY7WMU2uZeURC1ccEE5 yhhHGCowCCWjqlWWe0MEZNZ3o0cKF+NGKkbyogVLzQeAPU5/IXVgCQlD/ Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10650"; a="337934870" X-IronPort-AV: E=Sophos;i="5.98,265,1673942400"; d="scan'208";a="337934870" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Mar 2023 00:51:36 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10650"; a="673045721" X-IronPort-AV: E=Sophos;i="5.98,265,1673942400"; d="scan'208";a="673045721" Received: from yhuang6-desk2.sh.intel.com (HELO yhuang6-desk2.ccr.corp.intel.com) ([10.238.208.55]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Mar 2023 00:51:21 -0700 From: "Huang, Ying" 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 , Minchan Kim , Andrew Morton , Aneesh Kumar K V , Michal Hocko , Wei Xu Subject: Re: [LSF/MM/BPF TOPIC] Swap Abstraction / Native Zswap References: <87356e850j.fsf@yhuang6-desk2.ccr.corp.intel.com> <87y1o571aa.fsf@yhuang6-desk2.ccr.corp.intel.com> <87o7ox762m.fsf@yhuang6-desk2.ccr.corp.intel.com> Date: Thu, 16 Mar 2023 15:50:15 +0800 In-Reply-To: (Yosry Ahmed's message of "Wed, 15 Mar 2023 01:01:39 -0700") Message-ID: <87bkkt5e4o.fsf@yhuang6-desk2.ccr.corp.intel.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Stat-Signature: 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x1CL/ROM/7 WruxNfUT zDFSG/eUgTGuXJRHKnNrt/uHGRd+SSg23RV24q8FMAXbbrVCRyZfjNel48lPG2BZWmThkjSrmUp6pAIHAkpzNAK6C9Yfe08GaVu++oWQoZSPx87GhMbIRVqQP1zeAjOY4qb6WLO9PWl2kgFCxaeGCwwkAlC4157kgj7g2DXkbzCqe6twqQPXV9vkaU1i3OpJT2MiEtVvrXwX56gooVeIttILczA== 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: Yosry Ahmed writes: > On Sun, Mar 12, 2023 at 7:13=E2=80=AFPM Huang, Ying wrote: >> >> Yosry Ahmed writes: >> >> >> > >> > My current idea is to have one xarray that stores the swap_descs >> > (which include swap_entry, swapcache, swap_count, etc), and only for >> > rotating disks have an additional xarray that maps swap_entry -> >> > swap_desc for cluster readahead, assuming we can eliminate all other >> > situations requiring a reverse mapping. >> > >> > I am not sure how having separate xarrays help? If we have one xarray, >> > might as well save the other lookups on put everything in swap_desc. >> > In fact, this should improve the locking today as swapcache / >> > swap_count operations can be lockless or very lightly contended. >> >> The condition of the proposal is "reverse mapping cannot be avoided for >> enough situation". So, if reverse mapping (or cluster readahead) can be >> avoided for enough situations, I think your proposal is good. Otherwise, >> I propose to use 2 xarrays. You don't need another reverse mapping >> xarray, because you just need to read the next several swap_entry into >> the swap cache for cluster readahead. swap_desc isn't needed for >> cluster readahead. > > swap_desc would be needed for cluster readahead in my original > proposal as the swap cache lives in swap_descs. Based on the current > implementation, we would need a reverse mapping (swap entry -> > swap_desc) in 3 situations: > > 1) __try_to_reclaim_swap(): when trying to find an empty swap slot and > failing, we fallback to trying to find swap entries that only have a > page in the swap cache (no references in page tables or page cache) > and free them. This would require a reverse mapping. > > 2) swapoff: we need to swap in all entries in a swapfile, so we need > to get all swap_descs associated with that swapfile. > > 3) swap cluster readahead. > > For (1), I think we can drop the dependency of a reverse mapping if we > free swap entries once we swap a page in and add it to the swap cache, > even if the swap count does not drop to 0. Now, we will not drop the swap cache even if the swap count becomes 0 if swap space utility < 50%. Per my understanding, this avoid swap page writing for read accesses. So I don't think we can change this directly without necessary discussion firstly. > For (2), instead of scanning page tables and shmem page cache to find > swapped out pages for the swapfile, we can scan all swap_descs > instead, we should be more efficient. This is one of the proposal's > potential advantages. Good. > (3) is the one that would still need a reverse mapping with the > current proposal. Today we use swap cluster readahead for anon pages > if we have a spinning disk or vma readahead is disabled. For shmem, we > always use cluster readahead. If we can limit cluster readahead to > only rotating disks, then the reverse mapping can only be maintained > for swapfiles on rotating disks. Otherwise, we will need to maintain a > reverse mapping for all swapfiles. For shmem, I think that it should be good to readahead based on shmem file offset instead of swap device offset. It's possible that some pages in the readahead window are from HDD while some other pages aren't. So it's a little hard to enable cluster read for HDD only. Anyway, it's not common to use HDD for swap now. >> >> > If the point is to store the swap_desc directly inside the xarray to >> > save 8 bytes, I am concerned that having multiple xarrays for >> > swapcache, swap_count, etc will use more than that. >> >> The idea is to save the memory used by reverse mapping xarray. > > I see. > >> >> >> >> >> >> >> > Keep in mind that the current overhead is 1 byte O(max swap page= s) not >> >> >> > O(swapped). Also, 1 byte is assuming we do not use the swap >> >> >> > continuation pages. If we do, it may end up being more. We also >> >> >> > allocate continuation in full 4k pages, so even if one swap_map >> >> >> > element in a page requires continuation, we will allocate an ent= ire >> >> >> > page. What I am trying to say is that to get an actual compariso= n you >> >> >> > need to also factor in the swap utilization and the rate of usag= e of >> >> >> > swap continuation. I don't know how to come up with a formula fo= r this >> >> >> > tbh. >> >> >> > >> >> >> > Also, like Johannes said, the worst case overhead (32 bytes if y= ou >> >> >> > count the reverse mapping) is 0.8% of swapped memory, aka 8M for= every >> >> >> > 1G swapped. It doesn't sound *very* bad. I understand that it is= pure >> >> >> > overhead for people not using zswap, but it is not very awful. >> >> >> > >> >> >> >> >> >> >> >> It seems what you really need is one bit of information to indi= cate >> >> >> >> this page is backed by zswap. Then you can have a seperate poin= ter >> >> >> >> for the zswap entry. >> >> >> > >> >> >> > If you use one bit in swp_entry_t (or one of the available swap = types) >> >> >> > to indicate whether the page is backed with a swapfile or zswap = it >> >> >> > doesn't really work. We lose the indirection layer. How do we mo= ve the >> >> >> > page from zswap to swapfile? We need to go update the page table= s and >> >> >> > the shmem page cache, similar to swapoff. >> >> >> > >> >> >> > Instead, if we store a key else in swp_entry_t and use this to l= ookup >> >> >> > the swp_entry_t or zswap_entry pointer then that's essentially w= hat >> >> >> > the swap_desc does. It just goes the extra mile of unifying the >> >> >> > swapcache as well and storing it directly in the swap_desc inste= ad of >> >> >> > storing it in another lookup structure. >> >> >> >> >> >> If we choose to make sizeof(struct swap_desc) =3D=3D 8, that is, s= tore only >> >> >> swap_entry in swap_desc. The added indirection appears to be anot= her >> >> >> level of page table with 1 entry. Then, we may use the similar me= thod >> >> >> as supporting system with 2 level and 3 level page tables, like th= e code >> >> >> in include/asm-generic/pgtable-nopmd.h. But I haven't thought abo= ut >> >> >> this deeply. >> >> > >> >> > Can you expand further on this idea? I am not sure I fully understa= nd. >> >> >> >> OK. The goal is to avoid the overhead if indirection isn't enabled v= ia >> >> kconfig. >> >> >> >> If indirection isn't enabled, store swap_entry in PTE directly. >> >> Otherwise, store index of swap_desc in PTE. Different functions (e.g= ., >> >> to get/set swap_entry in PTE) are implemented based on kconfig. >> > >> > >> > I thought about this, the problem is that we will have multiple >> > implementations of multiple things. For example, swap_count without >> > the indirection layer lives in the swap_map (with continuation logic). >> > With the indirection layer, it lives in the swap_desc (or somewhere >> > else). Same for the swapcache. Even if we keep the swapcache in an >> > xarray and not inside swap_desc, it would be indexed by swap_entry if >> > the indirection is disabled, and by swap_desc (or similar) if the >> > indirection is enabled. I think maintaining separate implementations >> > for when the indirection is enabled/disabled would be adding too much >> > complexity. >> > >> > WDYT? >> >> If we go this way, swap cache and swap_count will always be indexed by >> swap_entry. swap_desc just provides a indirection to make it possible >> to move between swap devices. >> >> Why must we index swap cache and swap_count by swap_desc if indirection >> is enabled? Yes, we can save one xarray indexing if we do so, but I >> don't think the overhead of one xarray indexing is a showstopper. >> >> I think this can be one intermediate step towards your final target. >> The changes to current implementation can be smaller. > > IIUC, the idea is to have two xarrays: > (a) xarray that stores a pointer to a struct containing swap_count and > swap cache. > (b) xarray that stores the underlying swap entry or zswap entry. > > When indirection is disabled: > page tables & page cache have swap entry directly like today, xarray > (a) is indexed by swap entry, xarray (b) does not exist. No reverse > mapping needed. > > In this case we have an extra overhead of 12-16 bytes (the struct > containing swap_count and swap cache) vs. 24 bytes of the swap_desc. > > When indirection is enabled: > page tables & page cache have a swap id (or swap_desc index), xarray > (a) is indexed by swap id, xarray (a) is indexed by swap entry. > xarray (b) is indexed by swap id as well > and contain swap entry or zswap entry. Reverse mapping might be > needed. Reverse mapping isn't needed. > In this case we have an extra overhead of 12-16 bytes + 8 bytes for > xarray (b) entry + memory overhead from 2nd xarray + reverse mapping > where needed. > > There is also the extra cpu overhead for an extra lookup in certain paths. > > Is my analysis correct? If yes, I agree that the original proposal is > good if the reverse mapping can be avoided in enough situations, and > that we should consider such alternatives otherwise. As I mentioned > above, I think it comes down to whether we can completely restrict > cluster readahead to rotating disks or not -- in which case we need to > decide what to do for shmem and for anon when vma readahead is > disabled. We can even have a minimal indirection implementation. Where, swap cache and swap_map[] are kept as they ware before, just one xarray is added. The xarray is indexed by swap id (or swap_desc index) to store the corresponding swap entry. When indirection is disabled, no extra overhead. When indirection is enabled, the extra overhead is just 8 bytes per swapped page. The basic migration support can be build on top of this. I think that this could be a baseline for indirection support. Then further optimization can be built on top of it step by step with supporting data. >> >> >> >> >> >> >> >> >> Depending on how much you are going to reuse the swap cache, yo= u might >> >> >> >> need to have something like a swap_info_struct to keep the lock= s happy. >> >> >> > >> >> >> > My current intention is to reimplement the swapcache completely = as a >> >> >> > pointer in struct swap_desc. This would eliminate this need and = a lot >> >> >> > of the locking we do today if I get things right. >> >> >> > >> >> >> >> >> >> >> >> > Another potential concern is readahead. With this design, we = have no >> >> >> >> >> >> >> >> Readahead is for spinning disk :-) Even a normal swap file with= an SSD can >> >> >> >> use some modernization. >> >> >> > >> >> >> > Yeah, I initially thought we would only need the swp_entry_t -> >> >> >> > swap_desc reverse mapping for readahead, and that we can only st= ore >> >> >> > that for spinning disks, but I was wrong. We need for other thin= gs as >> >> >> > well today: swapoff, when trying to find an empty swap slot and = we >> >> >> > start trying to free swap slots used only by the swapcache. Howe= ver, I >> >> >> > think both of these cases can be fixed (I can share more details= if >> >> >> > you want). If everything goes well we should only need to mainta= in the >> >> >> > reverse mapping (extra overhead above 24 bytes) for swap files on >> >> >> > spinning disks for readahead. >> >> >> > >> >> >> >> >> >> >> >> Looking forward to your discussion. >> >> >> >> Per my understanding, the indirection is to make it easy to move >> >> (swapped) pages among swap devices based on hot/cold. This is similar >> >> as the target of memory tiering. It appears that we can extend the >> >> memory tiering (mm/memory-tiers.c) framework to cover swap devices to= o? >> >> Is it possible for zswap to be faster than some slow memory media? >> > >> > >> > Agree with Chris that this may require a much larger overhaul. A slow >> > memory tier is still addressable memory, swap/zswap requires a page >> > fault to read the pages. I think (at least for now) there is a >> > fundamental difference. We want reclaim to eventually treat slow >> > memory & swap as just different tiers to place cold memory in with >> > different characteristics, but otherwise I think the swapping >> > implementation itself is very different. Am I missing something? >> >> Is it possible that zswap is faster than a really slow memory >> addressable device backed by NAND? TBH, I don't have the answer. > > I am not sure either. > >> >> Anyway, do you need a way to describe the tiers of the swap devices? >> So, you can move the cold pages among the swap devices based on that? > > For now I think the "tiers" in this proposal are just zswap and normal > swapfiles. We can later extend it to support more explicit tiering. IIUC, in original zswap implementation, there's 1:1 relationship between zswap and normal swapfile. But now, you make demoting among swap devices more general. Then we need some general way to specify which swap devices are fast and which are slow, and the demoting relationship among them. It can be memory tiers or something else, but we need one. Best Regards, Huang, Ying