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 D24A2C67861 for ; Tue, 9 Apr 2024 10:42:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 33AE56B0087; Tue, 9 Apr 2024 06:42:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2E8E76B0088; Tue, 9 Apr 2024 06:42:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 18C2A6B0089; Tue, 9 Apr 2024 06:42:49 -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 EDD3E6B0087 for ; Tue, 9 Apr 2024 06:42:48 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id B113CA0370 for ; Tue, 9 Apr 2024 10:42:48 +0000 (UTC) X-FDA: 81989655216.17.902802D Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf23.hostedemail.com (Postfix) with ESMTP id 9316114000C for ; Tue, 9 Apr 2024 10:42:46 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Hj2bfB5d; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf23.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712659366; 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=VXOzD6MLdAZeG2UL81ssnUgyrwyD9xxc8QB1oCbjdu4=; b=6bPI6M4x0KjR9xP5XpP/g1tf2CJ2By0Skq3QChZkjW2PFX3+MAgNqJvjE5EvIZrEXb1uEv unB+2/VKrPGVVIwTW2Fs0b2dYzvJe4WOKuLp3ho2Ax53RwHSTj3QbOwulMgIGIw1HwXzrI VM10y11v+FPLZxZb33VGGx4GgL0YHGg= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Hj2bfB5d; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf23.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712659366; a=rsa-sha256; cv=none; b=qTaLBgdJ5P/VGWXkSEvPxQ062aLYfx0ujgiNM1Zi1FVjkx2OdPJ+hGmMcdNTZqlYAHawcA HEqCPPndOrkLPPf4LZzrmDlLA0fi3ctuTrHZh6YGyeQOl0qGjV9EQT0zOEMLr/URc4BHHz sO93lm5gtye+dJ+2wgpWad+U2Qgy5c4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712659365; h=from:from: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:autocrypt:autocrypt; bh=VXOzD6MLdAZeG2UL81ssnUgyrwyD9xxc8QB1oCbjdu4=; b=Hj2bfB5dAiifNuMocTetlWkA/V+edMKhJthlhQBjANa2NBee1U/UG3B39qV2WuEDAfFL+d si4SbyIKBt1mLWIMZCtqec4Fkh6ZY5Cxa1iB+U1eCDa1LAIuVN9KGzpY5+X2xU6gTLp60k RDr56g6MV4mQ0T+AEBA9nfKFDw6ctx0= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-614-rNzMwllYN3CQ9tf90-4i9w-1; Tue, 09 Apr 2024 06:42:42 -0400 X-MC-Unique: rNzMwllYN3CQ9tf90-4i9w-1 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-33ed489edcaso3209388f8f.3 for ; Tue, 09 Apr 2024 03:42:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712659361; x=1713264161; h=content-transfer-encoding:in-reply-to:organization:autocrypt :content-language:from: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=VXOzD6MLdAZeG2UL81ssnUgyrwyD9xxc8QB1oCbjdu4=; b=iZYOyNLoxyai6e3gL5AzpVeab6KekQnyUNZ6KbatC/EFzAnzMA27oJ/TYTH/NQKBL/ LQHdmBYGllg3l9hYhuDWvLO7nfEM7septddREy8gBdHiwKZbnGswX7/NmEVbId5UcGA/ D4wqWi26nqDAvGd1fE68rvRhNutcetvfPbn+mYyqTsDgq/8GgP2uiCoq1SyfwbDOMWTq PG0MGlREoBw52c63RX7EGM9iKbxO3gPQJq4sr0exQHqo9DGdcwwTx0gXGqqahQLgyrQZ RZBTYF2TkvLrIm03Z7FSxlTIB82Ha90fHGmhHu6zykMdql7BNm79KlAJgI/jKgTX6Sl+ MdHw== X-Forwarded-Encrypted: i=1; AJvYcCWsccYqOy4qz+7fjNlU/kBgVa9ODTWwNCSt/U4KYnXQeuwJ/WVLBJO6TBWBoDfZDRL97Koirgh06BTHgLXSvymwhBg= X-Gm-Message-State: AOJu0YymX5M/QPbUoIp8w8NL1yzJFScRq77jVb14/DgdbH/hcx9+vbpa ckgfEJVm9FES/9iG9GH9i+XPt5mGokjF9epgCShollkgnhnc4k17sPw7K891w5RfwrY/gTkJ+Up za8FoMDP+V6lo0QEsWnIUkWkZbQDRC9pAmqvuQHGVS9Z9nnZw X-Received: by 2002:adf:ea48:0:b0:33e:1e2b:3f76 with SMTP id j8-20020adfea48000000b0033e1e2b3f76mr8091059wrn.61.1712659361205; Tue, 09 Apr 2024 03:42:41 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFSTaNSNoukylIDXb3TZzNp/UhY36WxWaitKpBqKVWjmdMvW4eRRoaTUFLZnmrOsUdaDPQ4fA== X-Received: by 2002:adf:ea48:0:b0:33e:1e2b:3f76 with SMTP id j8-20020adfea48000000b0033e1e2b3f76mr8091029wrn.61.1712659360719; Tue, 09 Apr 2024 03:42:40 -0700 (PDT) Received: from ?IPV6:2003:cb:c70a:be00:a285:bc76:307d:4eaa? (p200300cbc70abe00a285bc76307d4eaa.dip0.t-ipconnect.de. [2003:cb:c70a:be00:a285:bc76:307d:4eaa]) by smtp.gmail.com with ESMTPSA id p2-20020a056000018200b0034625392416sm1178261wrx.104.2024.04.09.03.42.39 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 09 Apr 2024 03:42:40 -0700 (PDT) Message-ID: <25d2a8ad-aad4-4bd5-bd3e-190b76175f61@redhat.com> Date: Tue, 9 Apr 2024 12:42:39 +0200 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() To: Ryan Roberts , Barry Song <21cnbao@gmail.com> 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: David Hildenbrand Autocrypt: addr=david@redhat.com; keydata= xsF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NH/UCzcsstw2TAkFnMEbo9Zu9w7Kv AxBQXWeXhJI9XQssfrf4Gusdqx8nPEpfOqCtbbwJMATbHyqLt7/oz/5deGuwxgb65pWIzufa N7eop7uh+6bezi+rugUI+w6DABEBAAHCwXwEGAEIACYCGwwWIQQb2cqtc1xMOkYN/MpN3hD3 AP+DWgUCXw7HsgUJEqkpoQAKCRBN3hD3AP+DWrrpD/4qS3dyVRxDcDHIlmguXjC1Q5tZTwNB boaBTPHSy/Nksu0eY7x6HfQJ3xajVH32Ms6t1trDQmPx2iP5+7iDsb7OKAb5eOS8h+BEBDeq 3ecsQDv0fFJOA9ag5O3LLNk+3x3q7e0uo06XMaY7UHS341ozXUUI7wC7iKfoUTv03iO9El5f XpNMx/YrIMduZ2+nd9Di7o5+KIwlb2mAB9sTNHdMrXesX8eBL6T9b+MZJk+mZuPxKNVfEQMQ a5SxUEADIPQTPNvBewdeI80yeOCrN+Zzwy/Mrx9EPeu59Y5vSJOx/z6OUImD/GhX7Xvkt3kq Er5KTrJz3++B6SH9pum9PuoE/k+nntJkNMmQpR4MCBaV/J9gIOPGodDKnjdng+mXliF3Ptu6 3oxc2RCyGzTlxyMwuc2U5Q7KtUNTdDe8T0uE+9b8BLMVQDDfJjqY0VVqSUwImzTDLX9S4g/8 kC4HRcclk8hpyhY2jKGluZO0awwTIMgVEzmTyBphDg/Gx7dZU1Xf8HFuE+UZ5UDHDTnwgv7E th6RC9+WrhDNspZ9fJjKWRbveQgUFCpe1sa77LAw+XFrKmBHXp9ZVIe90RMe2tRL06BGiRZr jPrnvUsUUsjRoRNJjKKA/REq+sAnhkNPPZ/NNMjaZ5b8Tovi8C0tmxiCHaQYqj7G2rgnT0kt WNyWQQ== Organization: Red Hat In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Rspam-User: X-Stat-Signature: 5zckrcsb3kpnamuduhp6fmut8eu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gVhm4AttKjqtqwiucv+8oTISfPzuqmeOVFw3C9apxaYPloGThV51NhE+DlVpYBx59PoufAud5Zeox2hnrHKMAAKN/4u3ezVVCb6TCxY4Mly5pJzqnMtGIm/fWwdbgcVW7Cm29/67rmXolBSke2gnP8hlSAscOvqOSJ3myR9EkXnSIxuugxcVGH5eJxf4TPpiwv1IZNgGGfUOAPB4TuOxTBT5X+3n+0Ai9MO3BrVX4jg== 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.24 11:55, Ryan Roberts wrote: > 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 :) I'd prefer to keep it simple initially. Devil is in the detail for the refault case: in the past, dropping an exclusive flag could have been problematic with some O_DIRECT workloads that were not using FOLL_PIN. IIUC, some still remain. More details can be had from https://lore.kernel.org/all/20230113171026.582290-1-david@redhat.com/ It might all change a bit if I manage to get folio_test_anon_exclusive() flying. The current plan is that all individual swp PTEs would still have pte_swp_exclusive() set, and we would *not* clear the folio_test_anon_exclusive() flag while the folio is still in the swapcache [which we do today to make fork() handling easier]. That will make refault significantly easier to handle regarding exclusivity handling with large folios. -- Cheers, David / dhildenb