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 BA9D0CD1288 for ; Wed, 3 Apr 2024 07:21:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2607A6B0083; Wed, 3 Apr 2024 03:21:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 210016B0085; Wed, 3 Apr 2024 03:21:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0D7EB6B0087; Wed, 3 Apr 2024 03:21:21 -0400 (EDT) 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 E67D66B0083 for ; Wed, 3 Apr 2024 03:21:20 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 795361A0D80 for ; Wed, 3 Apr 2024 07:21:20 +0000 (UTC) X-FDA: 81967374720.18.B988925 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf05.hostedemail.com (Postfix) with ESMTP id B3B7B100008 for ; Wed, 3 Apr 2024 07:21:18 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf05.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712128878; 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; bh=RH7hiU2+lqB45HtkbzCsBLctEA0lOItltldrW3en6cc=; b=eUeTqdFopgquWWIniAm5tm+iRMWMdKjGKKDq9/xthZvg8JuLDGHtOqwCpdfgoHsI+v92LQ hW6Vt1+ecV8r9fS0GpW/smjrAzy9PIfNJPLrhFvmJ3drK+2tMDFr30PxVX3xoe6hKZCfcI rGEvvjaUtId/G3HuTIZxCHUrqd6fNAE= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf05.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712128878; a=rsa-sha256; cv=none; b=wFHlpE6yGBKppFgSzMC8dvPE7dMe1Eg4VJd300/5IIv5u6eeFHPnFj+lVPkX0V/xpD7WTl qQX4GKvkqUoXxcYOBwLdsnxW/Owjn+c7UDWvdz6pRSe5nj4xQtR6JtXBVlQsPy+/uSw7T7 QspXQTYxRXedZuzX26vn5FTn9qbJoiw= 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 2B4D91007; Wed, 3 Apr 2024 00:21:49 -0700 (PDT) Received: from [10.57.72.245] (unknown [10.57.72.245]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id F2B3B3F7B4; Wed, 3 Apr 2024 00:21:15 -0700 (PDT) Message-ID: <4020c532-d20d-4624-8ea6-607de423396c@arm.com> Date: Wed, 3 Apr 2024 08:21:14 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v5 2/6] mm: swap: free_swap_and_cache_nr() as batched free_swap_and_cache() Content-Language: en-GB To: Zi Yan Cc: Andrew Morton , David Hildenbrand , Matthew Wilcox , Huang Ying , Gao Xiang , Yu Zhao , Yang Shi , Michal Hocko , Kefeng Wang , Barry Song <21cnbao@gmail.com>, Chris Li , Lance Yang , linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20240327144537.4165578-1-ryan.roberts@arm.com> <20240327144537.4165578-3-ryan.roberts@arm.com> From: Ryan Roberts In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: B3B7B100008 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: 5wputmjbxg8jaw631zcyn8ocxh5kezxu X-HE-Tag: 1712128878-198095 X-HE-Meta: U2FsdGVkX19fm6OY25kyWyGU/D0tCNZiDbj2UKdAoxuNZwjv6sGESYdn4gufoQT6n1boES+p3FzAR+KUeX/f+lbeTiHV2j9zFFaHFeTvgRS6/ywITAd+92ARQovO2FBSTcl8AC+axguyKiJVp8gD7HM3v3z0LfOZO4U9ALsoEzP5bJ55J47Pby3iINOCSfopYZHO1qftNejJESFe2KFGL2gk5bvNNn/ja980zN0EsSzH1V++eFNYhKHbnvrj3Rg0LUQbOTu7mt1SItERMtbHLFTdp/2zWPyxSihpt3qla5OJZQIb9Y3fYEJXRtcsbApMFOcfRwpG44cXsFE/3WnZEXP39otw+x/4Zz3aSvj09y7iY2kHQsx0i/aClkgIaBC3FB+QXVcxHvu/KR4+JKjLmCRk4KivKrGGrst7vpg1CA1af5LEpiSqzAxBMqPfckNXayBebMKH7wGYk5mVK98CggiApgBk8hd473xIifTclIa0KxfmyIAQIjZhDMpScNJDht6TKJaF+s/p9GVRPN9iQCFNvjpVA7JpwFxBynxhqJ7Pix+AO7VAZL1QwKClEu0NV5yC0zQPHDNeYNCvJYVtQ3CDuU1+Tf3FgxNYJvM1TQ0N+eV7Qvm5Wc9yP/el9/Xgs3QRt7MmBCf2g/btGVRelEMX4s8VM1HeQVAaok3SMrh1Vk3yhn9cNb5cwY9PWbTYrEy8jeYqu8fuAPl8tHBZpkT+lqVKmqC9y/WnNZc6bgPY+BUEIvhAwuf8MXQ5JDUJP9GwiAE0iG7JEwGKMg9Zu+NQubs1LZtYZotm43en81sQYwC3yhEj5pd0+DKCLPjR9VlRZBuoaxl5xKOP/88xFguFUOblP1HFBUpE9cFX+sS3+bhQOB0wwRZZ3+XymTaLgmL4mO1sp1KlTDEdZjgncbqvtuM/AskBfbAu7hXtlURBCVs+B+lHuPc6OXq6bNFCNt09IbDSwwspbFbeMeS cBPyw3nE HvnqYpuz7FbFtDUiyP0H/2TfylfneZyjlhOiiYXxa/ZZaxKfXQPgZ8wgrn5fAIrY1XntB0+Ydu6vMQ07DZ+EedDeFPDz7w+B3DEpHMq+0/H9tJ49WnkkLAYpsr6sqG1GTsPX9gt9QxUK8hzx4+SX2xxFOyL1TMF5jp246cJDdtsmdVYNeCncwL9+50rJ9xuFy9Lw9pvWH5wxKyfPvLAHGQaAc6ESlQjjYtOQOyR4c4U+kaHTzxOigtZmfD05bpfRFXrWX 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 03/04/2024 01:30, Zi Yan wrote: > On 27 Mar 2024, at 10:45, Ryan Roberts wrote: > >> Now that we no longer have a convenient flag in the cluster to determine >> if a folio is large, free_swap_and_cache() will take a reference and >> lock a large folio much more often, which could lead to contention and >> (e.g.) failure to split large folios, etc. >> >> Let's solve that problem by batch freeing swap and cache with a new >> function, free_swap_and_cache_nr(), to free a contiguous range of swap >> entries together. This allows us to first drop a reference to each swap >> slot before we try to release the cache folio. This means we only try to >> release the folio once, only taking the reference and lock once - much >> better than the previous 512 times for the 2M THP case. >> >> Contiguous swap entries are gathered in zap_pte_range() and >> madvise_free_pte_range() in a similar way to how present ptes are >> already gathered in zap_pte_range(). >> >> While we are at it, let's simplify by converting the return type of both >> functions to void. The return value was used only by zap_pte_range() to >> print a bad pte, and was ignored by everyone else, so the extra >> reporting wasn't exactly guaranteed. We will still get the warning with >> most of the information from get_swap_device(). With the batch version, >> we wouldn't know which pte was bad anyway so could print the wrong one. >> >> Signed-off-by: Ryan Roberts >> --- >> include/linux/pgtable.h | 28 +++++++++++++++ >> include/linux/swap.h | 12 +++++-- >> mm/internal.h | 48 +++++++++++++++++++++++++ >> mm/madvise.c | 12 ++++--- >> mm/memory.c | 13 +++---- >> mm/swapfile.c | 78 ++++++++++++++++++++++++++++++----------- >> 6 files changed, 157 insertions(+), 34 deletions(-) >> >> diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h >> index 09c85c7bf9c2..8185939df1e8 100644 >> --- a/include/linux/pgtable.h >> +++ b/include/linux/pgtable.h >> @@ -708,6 +708,34 @@ static inline void pte_clear_not_present_full(struct mm_struct *mm, >> } >> #endif >> >> +#ifndef clear_not_present_full_ptes >> +/** >> + * clear_not_present_full_ptes - Clear consecutive not present PTEs. >> + * @mm: Address space the ptes represent. >> + * @addr: Address of the first pte. >> + * @ptep: Page table pointer for the first entry. >> + * @nr: Number of entries to clear. >> + * @full: Whether we are clearing a full mm. >> + * >> + * May be overridden by the architecture; otherwise, implemented as a simple >> + * loop over pte_clear_not_present_full(). >> + * >> + * Context: The caller holds the page table lock. The PTEs are all not present. >> + * The PTEs are all in the same PMD. >> + */ >> +static inline void clear_not_present_full_ptes(struct mm_struct *mm, >> + unsigned long addr, pte_t *ptep, unsigned int nr, int full) >> +{ >> + for (;;) { >> + pte_clear_not_present_full(mm, addr, ptep, full); >> + if (--nr == 0) >> + break; >> + ptep++; >> + addr += PAGE_SIZE; >> + } >> +} >> +#endif >> + > > Would the code below be better? > > for (i = 0; i < nr; i++, ptep++, addr += PAGE_SIZE) > pte_clear_not_present_full(mm, addr, ptep, full); I certainly agree that this is cleaner and more standard. But I'm copying the pattern used by the other batch helpers. I believe this pattern was first done by Willy for set_ptes(), then continued by DavidH for wrprotect_ptes() and clear_full_ptes(). I guess the benefit is that ptep and addr are only incremented if we are going around the loop again. I'd rather continue to be consistent with those other helpers. > > -- > Best Regards, > Yan, Zi