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 65B2DCFA45F for ; Wed, 23 Oct 2024 19:47:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F28EE6B0082; Wed, 23 Oct 2024 15:47:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ED7EA6B009B; Wed, 23 Oct 2024 15:47:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D77AA6B009E; Wed, 23 Oct 2024 15:47:05 -0400 (EDT) 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 B35486B0082 for ; Wed, 23 Oct 2024 15:47:05 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 7D90C1A0E72 for ; Wed, 23 Oct 2024 19:46:33 +0000 (UTC) X-FDA: 82705899906.24.32DAC75 Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) by imf14.hostedemail.com (Postfix) with ESMTP id 5D284100020 for ; Wed, 23 Oct 2024 19:46:43 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=hF7tXd+2; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf14.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.128.53 as permitted sender) smtp.mailfrom=usamaarif642@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729712784; a=rsa-sha256; cv=none; b=nHlKqdqaaR0YjJfowikhCgZD+s2GBSouzTTqlX+DWfR2AAMwZx1sLQdtWqQ6EgINtkVkhv yGleuDcFL7jk/cDNqSmXi9A7c/HkNLCAFeLnZ3pcIsNcwCOU/0AC/9Fcxs8QAvPIHt6do+ DsUuWHdxqHfPLuwp1/osKA/FG5sxl00= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=hF7tXd+2; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf14.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.128.53 as permitted sender) smtp.mailfrom=usamaarif642@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1729712784; 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=D5iSjnuhuDC0DOpI59kZ9NDtU1vZVCYiMLuN86UCJqw=; b=dFgeVRKJoY1zSbQreQGfCv2Wg1PteerSAWpikq6PexCp9UxsxK8S5XbEcaXy3LMu/p21Pe mol6TVTBx9LQQCw781wL9UkqUrRHdWIOj5YnlRu08LemzJvSJHDP7ONZIZd4UlZv7KE/Iw acnLrJqngvNr7pjyAMC4EAklmYkt06Y= Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-431616c23b5so1540505e9.0 for ; Wed, 23 Oct 2024 12:47:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729712822; x=1730317622; darn=kvack.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=D5iSjnuhuDC0DOpI59kZ9NDtU1vZVCYiMLuN86UCJqw=; b=hF7tXd+2QdIbL+iBF75IAJCVW8l9w8t1jszXLaxJsSiCaqeS9B2Lm8jWcyEPAkb+Bf D3FFrpPOrT/9x6ynN98giHouT+2pszPIbNcGpMbhX9BnSBZZ0jf2mtRWQzXCAK1w5/z1 Zxdlqo+cXRRIjQTpYKQMxpiv6FgmeQvPcciqWaT/skBd8aR5AWbNGUIspQoRoolRTifT 2VpCVIRatc6Ct8meaxlzMqLDhkVrxiGlLoo6eAq3fuG0lgh1/x5EupWMSeFkct+qKRSm LnBB+iBNPe6WUo1RUPbMyQsXVZgdmZp1O0u2bUts6LskGZgx5Z+vu0yRLbaGylwOTnIB 1sKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729712822; x=1730317622; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=D5iSjnuhuDC0DOpI59kZ9NDtU1vZVCYiMLuN86UCJqw=; b=AqWvC52b5NA+jkBxjIEgd+6V6P20xbbk9qZ/+Xw6T2A/Fo6NeqXBuR2DavtFixuoIl lNymMVidyZhFrRG3/tTTRU1ENFD45eZTVAwiNKo+6MWPXuKjEtNlgktvP99mhkLkOjcZ O7Dz4VB3CsSI1SB7aBgOYZH9m1CTw5vCJ8hlWMpXFWBloYWjtMwt/KVjK+R0XI0/fM1f hX3TbckNuD84QIkDWpHevWR2HSQ3YR0nbh3V0NgGkRdz87AeqiPdiadk1m2ElNYdH4DZ qgwqX1ozb56Dlg2+YxQDGBtOLst25RS5o1mnWMfTbrM1X/YY/n5NOY6VSjNl3hwrUk0F nFTA== X-Forwarded-Encrypted: i=1; AJvYcCU4Xa5C0RFDUhhD3xtxjhM6KZQ/nfinvIsXiPhQoEtwy3t/PGLTdnbBZJLj0+s8Qx+SEX9qen+4dw==@kvack.org X-Gm-Message-State: AOJu0YyV0dLQx6Lgn+D0lDcc7OAI5Mw2Q5oKsgHbgK3J+/7rNpvisyWK DPXGqcCWdvakO4Xyq0j9JAbYXK0b/PQuKNrjaGybkr05CYiMeA+x X-Google-Smtp-Source: AGHT+IGSgbb9Dtn2zixDYzF1lkFR+CixRzTX88TfIjLLmQudW/61Mt6ogVCex3tHv4UxYkKRS3dA0g== X-Received: by 2002:a05:600c:45c6:b0:431:4fbd:f571 with SMTP id 5b1f17b1804b1-4318445a11amr23806835e9.13.1729712821463; Wed, 23 Oct 2024 12:47:01 -0700 (PDT) Received: from ?IPV6:2a02:6b67:d751:7400:c2b:f323:d172:e42a? ([2a02:6b67:d751:7400:c2b:f323:d172:e42a]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43186c1eccesm24571635e9.46.2024.10.23.12.47.00 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 23 Oct 2024 12:47:01 -0700 (PDT) Message-ID: Date: Wed, 23 Oct 2024 20:47:00 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [RFC 0/4] mm: zswap: add support for zswapin of large folios To: Barry Song <21cnbao@gmail.com> Cc: Yosry Ahmed , senozhatsky@chromium.org, minchan@kernel.org, hanchuanhua@oppo.com, v-songbaohua@oppo.com, akpm@linux-foundation.org, linux-mm@kvack.org, hannes@cmpxchg.org, david@redhat.com, willy@infradead.org, kanchana.p.sridhar@intel.com, nphamcs@gmail.com, chengming.zhou@linux.dev, ryan.roberts@arm.com, ying.huang@intel.com, riel@surriel.com, shakeel.butt@linux.dev, kernel-team@meta.com, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org References: <20241018105026.2521366-1-usamaarif642@gmail.com> <5313c721-9cf1-4ecd-ac23-1eeddabd691f@gmail.com> <4c30cc30-0f7c-4ca7-a933-c8edfadaee5c@gmail.com> <7a14c332-3001-4b9a-ada3-f4d6799be555@gmail.com> <3dca2498-363c-4ba5-a7e6-80c5e5532db5@gmail.com> Content-Language: en-US From: Usama Arif In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspam-User: X-Stat-Signature: auwpg4gtox8g76yzr8h5gth3cnj6b41u X-Rspamd-Queue-Id: 5D284100020 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zirIOojSsTZIdjuFsjQR6VWCKMiG+7dR14bSE3OwhjQPL4avk86an0fF1PF6Mmy83McZ8mhelcuBIiTV6IEu62dfmMZnsD3BlSElzh8o0kPKRK/ZkIbIyrqsM/qo6lKpYz2jy6Y+1K/lsZ0CeM6qurl8MR3Ics+wELaaqq56B24Nj9vU7CPSs3q/yhIf2dk+BvCwsu+ri/j2dgoJW7MV/ScSALAYaWf0ePdkKKSD+O11iqpcOK12sOaNxvAfmGo7Efvoo2TPWWm+L/r3ZxGkAvDb5jy6VWu2u5ZMHF71Nd5N+xDI4S5ETYpvqDenLLN31vhDZ1P9I2Fhk/KSfhzoBpQUL114nrHjo92e8sed+JoT3olF8qdcIhgqpm8CK/FCWSt38CU2Lx+akj5eiwc1oc9TOlyQ3iU+c+qxC1tdJmU0+18PNcsozvVGB1BodLrS4Q3AKLN5WPTKsv4l5biPY0= 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: List-Subscribe: List-Unsubscribe: On 23/10/2024 19:52, Barry Song wrote: > On Thu, Oct 24, 2024 at 7:31 AM Usama Arif wrote: >> >> >> >> On 23/10/2024 19:02, Yosry Ahmed wrote: >>> [..] >>>>>> I suspect the regression occurs because you're running an edge case >>>>>> where the memory cgroup stays nearly full most of the time (this isn't >>>>>> an inherent issue with large folio swap-in). As a result, swapping in >>>>>> mTHP quickly triggers a memcg overflow, causing a swap-out. The >>>>>> next swap-in then recreates the overflow, leading to a repeating >>>>>> cycle. >>>>>> >>>>> >>>>> Yes, agreed! Looking at the swap counters, I think this is what is going >>>>> on as well. >>>>> >>>>>> We need a way to stop the cup from repeatedly filling to the brim and >>>>>> overflowing. While not a definitive fix, the following change might help >>>>>> improve the situation: >>>>>> >>>>>> diff --git a/mm/memcontrol.c b/mm/memcontrol.c >>>>>> >>>>>> index 17af08367c68..f2fa0eeb2d9a 100644 >>>>>> --- a/mm/memcontrol.c >>>>>> +++ b/mm/memcontrol.c >>>>>> >>>>>> @@ -4559,7 +4559,10 @@ int mem_cgroup_swapin_charge_folio(struct folio >>>>>> *folio, struct mm_struct *mm, >>>>>> memcg = get_mem_cgroup_from_mm(mm); >>>>>> rcu_read_unlock(); >>>>>> >>>>>> - ret = charge_memcg(folio, memcg, gfp); >>>>>> + if (folio_test_large(folio) && mem_cgroup_margin(memcg) < >>>>>> MEMCG_CHARGE_BATCH) >>>>>> + ret = -ENOMEM; >>>>>> + else >>>>>> + ret = charge_memcg(folio, memcg, gfp); >>>>>> >>>>>> css_put(&memcg->css); >>>>>> return ret; >>>>>> } >>>>>> >>>>> >>>>> The diff makes sense to me. Let me test later today and get back to you. >>>>> >>>>> Thanks! >>>>> >>>>>> Please confirm if it makes the kernel build with memcg limitation >>>>>> faster. If so, let's >>>>>> work together to figure out an official patch :-) The above code hasn't consider >>>>>> the parent memcg's overflow, so not an ideal fix. >>>>>> >>>> >>>> Thanks Barry, I think this fixes the regression, and even gives an improvement! >>>> I think the below might be better to do: >>>> >>>> diff --git a/mm/memcontrol.c b/mm/memcontrol.c >>>> index c098fd7f5c5e..0a1ec55cc079 100644 >>>> --- a/mm/memcontrol.c >>>> +++ b/mm/memcontrol.c >>>> @@ -4550,7 +4550,11 @@ int mem_cgroup_swapin_charge_folio(struct folio *folio, struct mm_struct *mm, >>>> memcg = get_mem_cgroup_from_mm(mm); >>>> rcu_read_unlock(); >>>> >>>> - ret = charge_memcg(folio, memcg, gfp); >>>> + if (folio_test_large(folio) && >>>> + mem_cgroup_margin(memcg) < max(MEMCG_CHARGE_BATCH, folio_nr_pages(folio))) >>>> + ret = -ENOMEM; >>>> + else >>>> + ret = charge_memcg(folio, memcg, gfp); >>>> >>>> css_put(&memcg->css); >>>> return ret; >>>> >>>> >>>> AMD 16K+32K THP=always >>>> metric mm-unstable mm-unstable + large folio zswapin series mm-unstable + large folio zswapin + no swap thrashing fix >>>> real 1m23.038s 1m23.050s 1m22.704s >>>> user 53m57.210s 53m53.437s 53m52.577s >>>> sys 7m24.592s 7m48.843s 7m22.519s >>>> zswpin 612070 999244 815934 >>>> zswpout 2226403 2347979 2054980 >>>> pgfault 20667366 20481728 20478690 >>>> pgmajfault 385887 269117 309702 >>>> >>>> AMD 16K+32K+64K THP=always >>>> metric mm-unstable mm-unstable + large folio zswapin series mm-unstable + large folio zswapin + no swap thrashing fix >>>> real 1m22.975s 1m23.266s 1m22.549s >>>> user 53m51.302s 53m51.069s 53m46.471s >>>> sys 7m40.168s 7m57.104s 7m25.012s >>>> zswpin 676492 1258573 1225703 >>>> zswpout 2449839 2714767 2899178 >>>> pgfault 17540746 17296555 17234663 >>>> pgmajfault 429629 307495 287859 >>>> >>> >>> Thanks Usama and Barry for looking into this. It seems like this would >>> fix a regression with large folio swapin regardless of zswap. Can the >>> same result be reproduced on zram without this series? >> >> >> Yes, its a regression in large folio swapin support regardless of zswap/zram. >> >> Need to do 3 tests, one with probably the below diff to remove large folio support, >> one with current upstream and one with upstream + swap thrashing fix. >> >> We only use zswap and dont have a zram setup (and I am a bit lazy to create one :)). >> Any zram volunteers to try this? > > Hi Usama, > > I tried a quick experiment: > > echo 1 > /sys/module/zswap/parameters/enabled > echo 0 > /sys/module/zswap/parameters/enabled > > This was to test the zRAM scenario. Enabling zswap even > once disables mTHP swap-in. :) > > I noticed a similar regression with zRAM alone, but the change resolved > the issue and even sped up the kernel build compared to the setup without > mTHP swap-in. Thanks for trying, this is amazing! > > However, I’m still working on a proper patch to address this. The current > approach: > > mem_cgroup_margin(memcg) < max(MEMCG_CHARGE_BATCH, folio_nr_pages(folio)) > > isn’t sufficient, as it doesn’t cover cases where group A contains group B, and > we’re operating within group B. The problem occurs not at the boundary of > group B but at the boundary of group A. I am not sure I completely followed this. As MEMCG_CHARGE_BATCH=64, if we are trying to swapin a 16kB page, we basically check if atleast 64/4 = 16 folios can be charged to cgroup, which is reasonable. If we try to swapin a 1M folio, we just check if we can charge atleast 1 folio. Are you saying that checking just 1 folio is not enough in this case and can still cause thrashing, i.e we should check more? If we want to maintain consitency for all folios another option is mem_cgroup_margin(memcg) < MEMCG_CHARGE_BATCH * folio_nr_pages(folio) but I think this is too extreme, we would be checking if 64M can be charged to cgroup just to swapin 1M. > > I believe there’s still room for improvement. For example, if a 64KB charge > attempt fails, there’s no need to waste time trying 32KB or 16KB. We can > directly fall back to 4KB, as 32KB and 16KB will also fail based on our > margin detection logic. > Yes that makes sense. Would something like below work to fix that: diff --git a/mm/memcontrol.c b/mm/memcontrol.c index c098fd7f5c5e..0a1ec55cc079 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -4550,7 +4550,11 @@ int mem_cgroup_swapin_charge_folio(struct folio *folio, struct mm_struct *mm, memcg = get_mem_cgroup_from_mm(mm); rcu_read_unlock(); - ret = charge_memcg(folio, memcg, gfp); + if (folio_test_large(folio) && + mem_cgroup_margin(memcg) < max(MEMCG_CHARGE_BATCH, folio_nr_pages(folio))) + ret = -ENOMEM; + else + ret = charge_memcg(folio, memcg, gfp); css_put(&memcg->css); return ret; diff --git a/mm/memory.c b/mm/memory.c index fecdd044bc0b..b6ce6605dc63 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4123,6 +4123,7 @@ static struct folio *alloc_swap_folio(struct vm_fault *vmf) pte_t *pte; gfp_t gfp; int order; + int ret; /* * If uffd is active for the vma we need per-page fault fidelity to @@ -4170,9 +4171,13 @@ static struct folio *alloc_swap_folio(struct vm_fault *vmf) addr = ALIGN_DOWN(vmf->address, PAGE_SIZE << order); folio = vma_alloc_folio(gfp, order, vma, addr, true); if (folio) { - if (!mem_cgroup_swapin_charge_folio(folio, vma->vm_mm, - gfp, entry)) + ret = mem_cgroup_swapin_charge_folio(folio, vma->vm_mm, gfp, entry); + if (!ret) { return folio; + } else if (ret == -ENOMEM) { + folio_put(folio); + goto fallback; + } folio_put(folio); } order = next_order(&orders, order);