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 B8EF7C67861 for ; Tue, 9 Apr 2024 09:55:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 387D86B0092; Tue, 9 Apr 2024 05:55:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3380A6B0095; Tue, 9 Apr 2024 05:55:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2009F6B0098; Tue, 9 Apr 2024 05:55:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 032EE6B0092 for ; Tue, 9 Apr 2024 05:55:27 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 89A05A0864 for ; Tue, 9 Apr 2024 09:55:27 +0000 (UTC) X-FDA: 81989535894.16.DFA7EC7 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf24.hostedemail.com (Postfix) with ESMTP id 77673180003 for ; Tue, 9 Apr 2024 09:55:25 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=none; spf=pass (imf24.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com; dmarc=pass (policy=none) header.from=arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712656525; 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=hKxGWiWsG/nLh84oVzoI4Yi/3IN/Xk9dwD7Wsxig4Mg=; b=JNoeonYKfeD8NOwHwThM4iagkNe2nlKIjNTYuuUUAHpy1E+Ts+eVH/+hXi4mm3DCnqeIqC YI4OEQc918rLlLZbpp/9sB96FIHRcphma7kW3+T+Xeq7xZ8Vv/lITI7X0TE1r2vAai1JcR iBTpQ1k698oa7YXCLmWDheAnkI5zYEk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712656525; a=rsa-sha256; cv=none; b=Vip64Qmau9R1W4PPiahtBFvYqiw1n9mWTHvmlA4h70Tn2nUb2+08OHu0+AzQ9yijc6mc8e O3oUjeua9BaWiMgIx/zt9uMyTIjwOtOZEzc88xX33T1RavtocMdiYTkhlFfs0zaXHjWTWg am9XWlODDCBYmfGliCfz7dG07Vt/la8= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=none; spf=pass (imf24.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com; dmarc=pass (policy=none) header.from=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 E6965DA7; Tue, 9 Apr 2024 02:55:54 -0700 (PDT) Received: from [10.1.33.185] (unknown [10.1.33.185]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id C044B3F6C4; Tue, 9 Apr 2024 02:55:22 -0700 (PDT) Message-ID: Date: Tue, 9 Apr 2024 10:55:21 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v7 2/7] mm: swap: free_swap_and_cache_nr() as batched free_swap_and_cache() Content-Language: en-GB To: Barry Song <21cnbao@gmail.com>, David Hildenbrand Cc: Andrew Morton , Matthew Wilcox , Huang Ying , Gao Xiang , Yu Zhao , Yang Shi , Michal Hocko , Kefeng Wang , Chris Li , Lance Yang , linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20240408183946.2991168-1-ryan.roberts@arm.com> <20240408183946.2991168-3-ryan.roberts@arm.com> <44e23a4d-97e7-4d84-848a-f9325b7b76c0@redhat.com> From: Ryan Roberts In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Stat-Signature: gkc98wtrm5njqtpfaj3frghb6g8p488c X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 77673180003 X-Rspam-User: X-HE-Tag: 1712656525-848651 X-HE-Meta: U2FsdGVkX1/+8gwYYljjTfq3Fn/0ZQRq5cq+VTTpi6yKnWCcXz7zhvEYYHHNR+VOxmGjzOi+y6Ej1LlS3Y/H1tR9RJa6G5bjicFhpaZMqr8dEQZkIuf78UyiN89erBUjQHZipS76VTsIoj4nuYA+sURdLsDHzpfpg9NCIoXRuPFSv/CvmWfIgkQ9krV+gd/vxIlNF2AQVNoVZwtqL8ccN5g7UTPuv7weRnnPh1Q4unmTHRB+Z6QGlp2OE+9DOzmYPJUa9uGjI0j6camJap9/u4CvTkiQpqQHXXXzfvlLpS8u+NE6UkQ84HVghcpA0vCembVVm3vWH8BcVPgdaRz+HGuDnPe9JvTPYPtjEGMd7iZtkOsSgdZPjrNnbYwFOUJXIEA7WVEU62JPVGZHWDv+4uiEFg/+5L6Ku3IskDAlkXh/H541yxWAUhM/q7jIewcNZuL9ftHJWs9u/uQx0Tg2YBSGSr6N1e6qsu9ahqSi3/ks5FdSg8HkKuSB2mYiPw8hEWlqHG08FfdPTij28hXiI4i+y9497kwVCLWPJdYHIRG13JiiFBJm4zJryM0m99FcRvrmUEvSpNWKo3Q13Fzz7G7nAYOfEMmJuil34ZXUyrR348B06Tw07FE0tyK6A2BxQDq59OH4ufMiCc7qQTN4NBcBl6p04yMasGetXwopC8hW19xCzvTHJqjEAM12zj/u2PxsFQYXd/C5qmBed6yttc1+PYthsZePmxAxev1yYNGNBO+3K2OIHaULK84LWaD4awCsljkusyZ3zuwUh8oRD0xbvJEX5lFQZyJKqmMSkNojdgu+bUfOL/MWEUIb+2i9f1p6O+NmDKVtf8CpzlQrlMt8hxYGvc7uUlQ7833Gxr1jr38bCcnSEVCt5QW6NOyYLkpfKriDoy/uZg5YDOB51SzBDL/M6PvPi8dZv8w3cd+olkAv32Mq+qg9Ka13b3NpC/X7xSUkAnze83vu07t apdPh7SN qRIveC9vbQ238imwOeKl+0kj3eG46Pv8oH3Sw1/cSS+P7KKh1Y2oEmzNdbxw+tO2tJPhDCjq3IqJmzOo2oOUlsPjdUmy/FbMRupYDwddZzpB7TUBKwnUlfNosi3aI76/xoP3JZt9qmjI8lvu3Phaoc8UU2oafR45mLTlhwUq3tAuHGuQl2BVY7GRCOWLuzjhv0XOGSV+nEZ5ef+RUyAHHFHf/UKaX4tBoNCNBUflDffSsa5prPsm3IW88mdNSnH58TcU051diP+U2Dt9glyIyPj2syEbxZMGQ1ISnhcD2CbGn9mHluEkGm4tqF7/uqWAPqJ3PjXC0QcZqmP5WGEhmEN4tnmwrq02mH0rGQFvz2GCPKzLP5DfpFcB5n4W0GaUFUQI/ 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 09/04/2024 10:41, Barry Song wrote: > On Tue, Apr 9, 2024 at 9:24 PM David Hildenbrand wrote: >> >> On 09.04.24 11:22, Barry Song wrote: >>> On Tue, Apr 9, 2024 at 8:51 PM Barry Song <21cnbao@gmail.com> wrote: >>>> >>>> On Tue, Apr 9, 2024 at 6:40 AM 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 | 29 ++++++++++++ >>>>> include/linux/swap.h | 12 +++-- >>>>> mm/internal.h | 63 ++++++++++++++++++++++++++ >>>>> mm/madvise.c | 12 +++-- >>>>> mm/memory.c | 13 +++--- >>>>> mm/swapfile.c | 97 +++++++++++++++++++++++++++++++++-------- >>>>> 6 files changed, 195 insertions(+), 31 deletions(-) >>>>> >>>>> diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h >>>>> index a3fc8150b047..75096025fe52 100644 >>>>> --- a/include/linux/pgtable.h >>>>> +++ b/include/linux/pgtable.h >>>>> @@ -708,6 +708,35 @@ 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 multiple not present PTEs which are >>>>> + * consecutive in the pgtable. >>>>> + * @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 >>>>> + >>>>> #ifndef __HAVE_ARCH_PTEP_CLEAR_FLUSH >>>>> extern pte_t ptep_clear_flush(struct vm_area_struct *vma, >>>>> unsigned long address, >>>>> diff --git a/include/linux/swap.h b/include/linux/swap.h >>>>> index f6f78198f000..5737236dc3ce 100644 >>>>> --- a/include/linux/swap.h >>>>> +++ b/include/linux/swap.h >>>>> @@ -471,7 +471,7 @@ extern int swap_duplicate(swp_entry_t); >>>>> extern int swapcache_prepare(swp_entry_t); >>>>> extern void swap_free(swp_entry_t); >>>>> extern void swapcache_free_entries(swp_entry_t *entries, int n); >>>>> -extern int free_swap_and_cache(swp_entry_t); >>>>> +extern void free_swap_and_cache_nr(swp_entry_t entry, int nr); >>>>> int swap_type_of(dev_t device, sector_t offset); >>>>> int find_first_swap(dev_t *device); >>>>> extern unsigned int count_swap_pages(int, int); >>>>> @@ -520,8 +520,9 @@ static inline void put_swap_device(struct swap_info_struct *si) >>>>> #define free_pages_and_swap_cache(pages, nr) \ >>>>> release_pages((pages), (nr)); >>>>> >>>>> -/* used to sanity check ptes in zap_pte_range when CONFIG_SWAP=0 */ >>>>> -#define free_swap_and_cache(e) is_pfn_swap_entry(e) >>>>> +static inline void free_swap_and_cache_nr(swp_entry_t entry, int nr) >>>>> +{ >>>>> +} >>>>> >>>>> static inline void free_swap_cache(struct folio *folio) >>>>> { >>>>> @@ -589,6 +590,11 @@ static inline int add_swap_extent(struct swap_info_struct *sis, >>>>> } >>>>> #endif /* CONFIG_SWAP */ >>>>> >>>>> +static inline void free_swap_and_cache(swp_entry_t entry) >>>>> +{ >>>>> + free_swap_and_cache_nr(entry, 1); >>>>> +} >>>>> + >>>>> #ifdef CONFIG_MEMCG >>>>> static inline int mem_cgroup_swappiness(struct mem_cgroup *memcg) >>>>> { >>>>> diff --git a/mm/internal.h b/mm/internal.h >>>>> index 3bdc8693b54f..de68705624b0 100644 >>>>> --- a/mm/internal.h >>>>> +++ b/mm/internal.h >>>>> @@ -11,6 +11,8 @@ >>>>> #include >>>>> #include >>>>> #include >>>>> +#include >>>>> +#include >>>>> #include >>>>> >>>>> struct folio_batch; >>>>> @@ -189,6 +191,67 @@ static inline int folio_pte_batch(struct folio *folio, unsigned long addr, >>>>> >>>>> return min(ptep - start_ptep, max_nr); >>>>> } >>>>> + >>>>> +/** >>>>> + * pte_next_swp_offset - Increment the swap entry offset field of a swap pte. >>>>> + * @pte: The initial pte state; is_swap_pte(pte) must be true. >>>>> + * >>>>> + * Increments the swap offset, while maintaining all other fields, including >>>>> + * swap type, and any swp pte bits. The resulting pte is returned. >>>>> + */ >>>>> +static inline pte_t pte_next_swp_offset(pte_t pte) >>>>> +{ >>>>> + swp_entry_t entry = pte_to_swp_entry(pte); >>>>> + pte_t new = __swp_entry_to_pte(__swp_entry(swp_type(entry), >>>>> + swp_offset(entry) + 1)); >>>>> + >>>>> + if (pte_swp_soft_dirty(pte)) >>>>> + new = pte_swp_mksoft_dirty(new); >>>>> + if (pte_swp_exclusive(pte)) >>>>> + new = pte_swp_mkexclusive(new); >>>>> + if (pte_swp_uffd_wp(pte)) >>>>> + new = pte_swp_mkuffd_wp(new); >>>> >>>> I don't quite understand this. If this page table entry is exclusive, >>>> will its subsequent page table entry also be exclusive without >>>> question? >>>> in try_to_unmap_one, exclusive is per-subpage but not per-folio: >>>> >>>> anon_exclusive = folio_test_anon(folio) && >>>> PageAnonExclusive(subpage); >>>> >>>> same questions also for diry, wp etc. >>> >>> Sorry for the noise. you are right. based on your new version, I think I should >>> entirely drop: >>> >>> [PATCH v2 3/5] mm: swap_pte_batch: add an output argument to reture if >>> all swap entries are exclusive >> >> Yes. If we ever want to ignore some bits, we should likely add flags to >> change the behavior, like for folio_pte_batch(). >> >> For swapin, you really want the exclusive bits to match, though. > > I am not quite sure I definitely need exclusive bits to match. i can either > drop my 3/5 or ignore the exclusive bit as below (if anyone is not shared, > swpin won't reuse the large folio, but it can still entirely map it read-only): > > diff --git a/mm/internal.h b/mm/internal.h > index cae39c372bfc..5726e729c9ee 100644 > --- a/mm/internal.h > +++ b/mm/internal.h > @@ -253,10 +253,22 @@ static inline int swap_pte_batch(pte_t > *start_ptep, int max_nr, pte_t pte, > *any_shared |= !pte_swp_exclusive(pte); > > while (ptep < end_ptep) { > + pte_t ignore_exclusive_pte; > + pte_t ignore_exclusive_expected_pte; > pte = ptep_get(ptep); > > - if (!pte_same(pte, expected_pte)) > - break; > + if (any_shared) { > + ignore_exclusive_pte = pte; > + ignore_exclusive_expected_pte = expected_pte; > + ignore_exclusive_pte = > pte_swp_clear_exclusive(ignore_exclusive_pte); > + ignore_exclusive_expected_pte = > pte_swp_clear_exclusive(expected_pte); > + > + if (!pte_same(ignore_exclusive_pte, > ignore_exclusive_expected_pte)) > + break; > + } else { > + if (!pte_same(pte, expected_pte)) > + break; > + } > > if (any_shared) > *any_shared |= !pte_swp_exclusive(pte); I'll leave David to comment on this proposal; I'm not sure I understand all the details. The code change does look a bit "busy" though - sometimes that can be an indicator :) > >> softdirty and uffd-wp as well at least initially for simplicity. > > yes for this. > > By the way, I wonder if you and Ryan have a moment to review swpin > refault patchset > v2 :-) It's on my todo list! I'm very keen to get as much large swap-out and swap-in support into v6.10 as we can. Hoping to get to it inthe next couple of days. > > [PATCH v2 0/5] large folios swap-in: handle refault cases first > https://lore.kernel.org/linux-mm/20240409082631.187483-1-21cnbao@gmail.com/ > > >> >> -- >> Cheers, >> >> David / dhildenb >> > > Thanks > Barry