Received: by 2002:a89:2c3:0:b0:1ed:23cc:44d1 with SMTP id d3csp236600lqs; Mon, 4 Mar 2024 23:42:11 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCX/VyQBEXL1hGv4F4J5GTwsl/bekVxh46s8IxwZ/SaoDIOf08ns2qT9T2+QcrQdNkdTpQ7hby0jAGwC4nyPUocqlSTmLNNvOwtbQtrekw== X-Google-Smtp-Source: AGHT+IEl165LsHzSTm8zQh0w0RBSF4XNReh+jYAq6kBWLk2YrraU1ngbWAcdAQ9T4kodmCnoKaJK X-Received: by 2002:a05:6214:8ec:b0:690:7c34:b887 with SMTP id dr12-20020a05621408ec00b006907c34b887mr956216qvb.64.1709624531476; Mon, 04 Mar 2024 23:42:11 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709624531; cv=pass; d=google.com; s=arc-20160816; b=ZcDhclsN4DoMO2T/DzvY0+DoI5B8SLVLue1rM5LCS57J/2FvQskRivNrXNuJ+aOFOP xkpeiQ/3nTXZ8BVNEH6ZT8NCVVEsGR4DeuWwfREEkEvGleaMA1P76bJC6eyHwoG2XvP7 ROPcHGlUP0YQKrRk9PKtFaKdWi0p6hlRw4wwDwTm3MZ+Y2mA1xv9gLMIsdbVBROkzDck p2CuYG233DlOplJxrSWTyVog33tOECJSt4BAUq74yd7ZrYAXiXB7OiPdteiK1uGq+wSv ej20j08SendyZY3J4BFUeBl0zyDgZf/s4ekIHLyLFjWFUzivc5qqhCL9E79MfRaKI5h7 sFvg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=daHd4/rk4YR8JgTHCAN7Y317bGNMDE6rXZNNjeGvtCI=; fh=YMTUmdikQLjOmm5bR025m4/FYj0ZLn+Kg4zPIekTkLE=; b=eyp0Kg063YOguKeCaeuLmHM/q5Wup8H6RG8KHjcUYRW7+wosXFhrcqelrpj1pU+o9/ 3vTYMvMfsS6t1gZ9KjKRt6660u144YpSGCEMkhlQvdDo6z2x8E6AQTXliGz0J5rhE5me tfgByCVdTm9r3Agz5UcSGr505cB5mDoQO4YkBX5vRSerUELLhRkITAOyyCV93iV9QjDZ TNyJ4rRLZikF5tVPbxE25JgsofaNjuNp71syYtr1t8r2sTzD+gwqUxFCtGS7Qkb0Q26m uRCVLqUKja3+pd+lwj2pAkyATyUxXIYHvv3ObwCqEvUPVJPXpTiS2/ebJ7L2TEKF39HC IvOA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-91824-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-91824-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id kl25-20020a056214519900b0068fa09a8976si11164640qvb.589.2024.03.04.23.42.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Mar 2024 23:42:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-91824-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-91824-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-91824-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 212241C213C4 for ; Tue, 5 Mar 2024 07:42:11 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C38317E775; Tue, 5 Mar 2024 07:42:04 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 99DD97E59F for ; Tue, 5 Mar 2024 07:42:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709624524; cv=none; b=eugArjAavxAjJ4MjVcslzyWtPu1JuuL/cjGP+ErOxmZIqIqtl2ZcTBwM+YG282ym157iq1PfdtffuByDInbYCgVO2B1en+xIJIsNuIQsidg1ArbVXPiTofM+Jx8DhObA7Bj1WKPOYhG1RgqHmsIV0P1iVsEilaXehSpvaPP9oqg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709624524; c=relaxed/simple; bh=z+UnsHAlBpxE1YVss+8GZf8dsMPc5lVQnqhC/0PvQdQ=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=FFTrGmt03jLnqiFF+2LPjS02yFGGGScgkGCzUpm9G5CXohnPqSWB6CXcoHKoSW3uk/idtTQcnuZm+SDEYiz9FyCEBFqJ7uDyTXnIc7JKcwtshqk0KExePPQ5nmR/mzW+QIuJGWuwRO9CpwXzgIcbcWNEvuqo3N0ri0n0ZN00HrA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 413682F4; Mon, 4 Mar 2024 23:42:37 -0800 (PST) Received: from [10.57.68.162] (unknown [10.57.68.162]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id DB8703F73F; Mon, 4 Mar 2024 23:41:58 -0800 (PST) Message-ID: <62d740f2-f8df-40df-b624-36e099ec1671@arm.com> Date: Tue, 5 Mar 2024 07:41:57 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v3 1/4] mm: swap: Remove CLUSTER_FLAG_HUGE from swap_cluster_info:flags Content-Language: en-GB To: Barry Song <21cnbao@gmail.com> Cc: Matthew Wilcox , David Hildenbrand , Andrew Morton , Huang Ying , Gao Xiang , Yu Zhao , Yang Shi , Michal Hocko , Kefeng Wang , linux-kernel@vger.kernel.org, linux-mm@kvack.org References: <6541e29b-f25a-48b8-a553-fd8febe85e5a@redhat.com> <2934125a-f2e2-417c-a9f9-3cb1e074a44f@redhat.com> <049818ca-e656-44e4-b336-934992c16028@arm.com> <4a73b16e-9317-477a-ac23-8033004b0637@arm.com> <1195531c-d985-47e2-b7a2-8895fbb49129@redhat.com> <5ebac77a-5c61-481f-8ac1-03bc4f4e2b1d@arm.com> From: Ryan Roberts In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On 04/03/2024 05:42, Barry Song wrote: > On Mon, Mar 4, 2024 at 5:52 PM Barry Song <21cnbao@gmail.com> wrote: >> >> On Sat, Mar 2, 2024 at 6:08 AM Ryan Roberts wrote: >>> >>> On 01/03/2024 16:44, Ryan Roberts wrote: >>>> On 01/03/2024 16:31, Matthew Wilcox wrote: >>>>> On Fri, Mar 01, 2024 at 04:27:32PM +0000, Ryan Roberts wrote: >>>>>> I've implemented the batching as David suggested, and I'm pretty confident it's >>>>>> correct. The only problem is that during testing I can't provoke the code to >>>>>> take the path. I've been pouring through the code but struggling to figure out >>>>>> under what situation you would expect the swap entry passed to >>>>>> free_swap_and_cache() to still have a cached folio? Does anyone have any idea? >>>>>> >>>>>> This is the original (unbatched) function, after my change, which caused David's >>>>>> concern that we would end up calling __try_to_reclaim_swap() far too much: >>>>>> >>>>>> int free_swap_and_cache(swp_entry_t entry) >>>>>> { >>>>>> struct swap_info_struct *p; >>>>>> unsigned char count; >>>>>> >>>>>> if (non_swap_entry(entry)) >>>>>> return 1; >>>>>> >>>>>> p = _swap_info_get(entry); >>>>>> if (p) { >>>>>> count = __swap_entry_free(p, entry); >>>>>> if (count == SWAP_HAS_CACHE) >>>>>> __try_to_reclaim_swap(p, swp_offset(entry), >>>>>> TTRS_UNMAPPED | TTRS_FULL); >>>>>> } >>>>>> return p != NULL; >>>>>> } >>>>>> >>>>>> The trouble is, whenever its called, count is always 0, so >>>>>> __try_to_reclaim_swap() never gets called. >>>>>> >>>>>> My test case is allocating 1G anon memory, then doing madvise(MADV_PAGEOUT) over >>>>>> it. Then doing either a munmap() or madvise(MADV_FREE), both of which cause this >>>>>> function to be called for every PTE, but count is always 0 after >>>>>> __swap_entry_free() so __try_to_reclaim_swap() is never called. I've tried for >>>>>> order-0 as well as PTE- and PMD-mapped 2M THP. >>>>> >>>>> I think you have to page it back in again, then it will have an entry in >>>>> the swap cache. Maybe. I know little about anon memory ;-) >>>> >>>> Ahh, I was under the impression that the original folio is put into the swap >>>> cache at swap out, then (I guess) its removed once the IO is complete? I'm sure >>>> I'm miles out... what exactly is the lifecycle of a folio going through swap out? >>>> >>>> I guess I can try forking after swap out, then fault it back in in the child and >>>> exit. Then do the munmap in the parent. I guess that could force it? Thanks for >>>> the tip - I'll have a play. >>> >>> That has sort of solved it, the only problem now is that all the folios in the >>> swap cache are small (because I don't have Barry's large swap-in series). So >>> really I need to figure out how to avoid removing the folio from the cache in >>> the first place... >> >> I am quite sure we have a chance to hit a large swapcache even using zRAM - >> a sync swapfile and even during swap-out. >> >> I have a test case as below, >> 1. two threads to run MADV_PAGEOUT >> 2. two threads to read data being swapped-out >> >> in do_swap_page, from time to time, I can get a large swapcache. >> >> We have a short time window after add_to_swap() and before >> __removing_mapping() of >> vmscan, a large folio is still in swapcache. >> >> So Ryan, I guess you can trigger this by adding one more thread of >> MADV_DONTNEED to do zap_pte_range? > > Ryan, I have modified my test case to have 4 threads: > 1. MADV_PAGEOUT > 2. MADV_DONTNEED > 3. write data > 4. read data > > and git push the code here so that you can get it, > https://github.com/BarrySong666/swaptest/blob/main/swptest.c Thanks for this, Barry! > > I can reproduce the issue in zap_pte_range() in just a couple of minutes. > >> >> >>> >>>> >>>>> >>>>> If that doesn't work, perhaps use tmpfs, and use some memory pressure to >>>>> force that to swap? >>>>> >>>>>> I'm guessing the swapcache was already reclaimed as part of MADV_PAGEOUT? I'm >>>>>> using a block ram device as my backing store - I think this does synchronous IO >>>>>> so perhaps if I have a real block device with async IO I might have more luck? >>>>>> Just a guess... >>>>>> >>>>>> Or perhaps this code path is a corner case? In which case, perhaps its not worth >>>>>> adding the batching optimization after all? >>>>>> >>>>>> Thanks, >>>>>> Ryan >>>>>> >>>> > > Thanks > Barry