Received: by 2002:ab2:7041:0:b0:1f4:bcc8:f211 with SMTP id x1csp79973lql; Fri, 12 Apr 2024 04:33:51 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWlssSgNIWniMq5dIlp1y+RQMiN2wUjVXAKZQaLIUB2aBuFf2npaneAKTVVhDfG9pH0MOVQ/q8UIY6cu+nGsU5+mHKUp8Nvgw3y8FaPUw== X-Google-Smtp-Source: AGHT+IFQRUQnKoLxDweQ4YWzkb0p+PJvBvttxHIjo4qo28eTu5mYmnF6NmoG7zpdJ+FeGQr+cNCD X-Received: by 2002:a05:6a00:3904:b0:6e7:48e3:7895 with SMTP id fh4-20020a056a00390400b006e748e37895mr6960283pfb.2.1712921631030; Fri, 12 Apr 2024 04:33:51 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712921631; cv=pass; d=google.com; s=arc-20160816; b=c7hmBgdN1L2M1QOe+HubGTKtG5eX+w/LPmvJ2sAhCbO5bQw5aqkimPLOJDoKwa4Qi3 pBkDAeB+36F4UlE1itjm7HYQTNgPqnQi81LNUPOmIJaje84eKPjSrEA4DuqQk+yiJDTm 2iqJJNq1FPnhgVbcBr7x9Q+ORIrOgTMr7qOCJjmIg8fJpq6Eoqx/xjWU8rmYbY6SZSIK h4q+V6NHA3CP0fce1uFxjmTC0YeMhMoiXJ2502RUUbC6xuV3GiY9aIuexhLCDD8AvQqW VLLY4rYs8iVjahyhk7cDVn34WyNFyhLsQlaeZCmsLgvfxS6AbggiEoHbr1I2jyAk2TYs s/ew== 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=Hv2McFYS0XyWpxcuisvPLWKItRrqxwKBtet26L5nYgc=; fh=//JgYQ8vkYB9VADRdvnybuUMiCWTdoJSW/5hl7ntpkE=; b=H8XX5EZCjN6UmqNJNJAmHeblslunRfd9i1Zb3JkvuUcbQL4/f8XKymWV70J0Mh4MBS 89JAGMvEKMPzKvRO9ez1LS9SmB9DyGq/Eso1OiBTkwMtA0UC4qjj7M7nLFV2A3VXLW/D qqlQCLQxbHVXR0Cpb108Xt5/4Br+hCCLxD29RNX5R5ApzswAGNcJH3aJrn5vnjj+zg73 ymiL/ncXdaFvHVoCbOqCb+GBu+i+Gxnp0fjRdFkuPPEbsBks1gK7dAgip9bwUDjYgC77 TkM2qqYv5V745DoDQzn4brMp7PyQPEUwktx5RiDTQVA1Z9X9iEU4gd4JwzYSD6cBnP5l mT2A==; 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-142584-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-142584-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id 28-20020a630e5c000000b005dc816e3080si3142494pgo.12.2024.04.12.04.33.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 04:33:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-142584-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::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-142584-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-142584-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id DECDAB22557 for ; Fri, 12 Apr 2024 11:31:32 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 090D655C29; Fri, 12 Apr 2024 11:31:16 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D1EB05579F for ; Fri, 12 Apr 2024 11:31:12 +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=1712921475; cv=none; b=hIbfcjbMGnfSsaViwwqN+FQINMSX66szIZ5kl0kzWH88hLEJ+Y3HfyaF1Qyppo/dAHr9e7qAX4JKKJ1+nreCu8nIT/7yJ5J2fc/FSO3sReIi6M/BVOLyvrTouY55SOF1VGe0QvgOlQuWt6Mikq6E/G9Ug1pFZebH897FDGKIZP0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712921475; c=relaxed/simple; bh=2Tj7lNIpUpCYC+JAYGGDQXfoqtROazbatZ7tNnKHFQg=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=Am6kj0RcPwCft4Qa1TYeQac0GHpr14U0/Jy4q0uH+xVWxGOyLf21i2+e2Wvo6x3617v5TSlBFJattTQ/2TrXn4WijjNu/es/riESS90SXaRatt6vTM2VfxOLG7XzFcqDxdTegcJsr9hautArQjFPMMm0yT6n8hTRuD49mENEWYc= 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 149B7339; Fri, 12 Apr 2024 04:31:41 -0700 (PDT) Received: from [10.57.73.208] (unknown [10.57.73.208]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 2B57F3F766; Fri, 12 Apr 2024 04:31:09 -0700 (PDT) Message-ID: <2f6cc7a4-ea64-40aa-842a-8d85309a5cbd@arm.com> Date: Fri, 12 Apr 2024 12:31:07 +0100 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 v2 4/5] mm: swap: entirely map large folios found in swapcache Content-Language: en-GB To: Barry Song <21cnbao@gmail.com> Cc: akpm@linux-foundation.org, linux-mm@kvack.org, baolin.wang@linux.alibaba.com, chrisl@kernel.org, david@redhat.com, hanchuanhua@oppo.com, hannes@cmpxchg.org, hughd@google.com, kasong@tencent.com, surenb@google.com, v-songbaohua@oppo.com, willy@infradead.org, xiang@kernel.org, ying.huang@intel.com, yosryahmed@google.com, yuzhao@google.com, ziy@nvidia.com, linux-kernel@vger.kernel.org References: <20240409082631.187483-1-21cnbao@gmail.com> <20240409082631.187483-5-21cnbao@gmail.com> <1008d688-757a-4c2d-86bd-793f5e787d30@arm.com> From: Ryan Roberts In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On 12/04/2024 00:30, Barry Song wrote: > On Fri, Apr 12, 2024 at 3:33 AM Ryan Roberts wrote: >> >> On 09/04/2024 09:26, Barry Song wrote: >>> From: Chuanhua Han >>> >>> When a large folio is found in the swapcache, the current implementation >>> requires calling do_swap_page() nr_pages times, resulting in nr_pages >>> page faults. This patch opts to map the entire large folio at once to >>> minimize page faults. Additionally, redundant checks and early exits >>> for ARM64 MTE restoring are removed. >>> >>> Signed-off-by: Chuanhua Han >>> Co-developed-by: Barry Song >>> Signed-off-by: Barry Song >>> --- >>> mm/memory.c | 64 +++++++++++++++++++++++++++++++++++++++++++---------- >>> 1 file changed, 52 insertions(+), 12 deletions(-) >>> >>> diff --git a/mm/memory.c b/mm/memory.c >>> index c4a52e8d740a..9818dc1893c8 100644 >>> --- a/mm/memory.c >>> +++ b/mm/memory.c >>> @@ -3947,6 +3947,10 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) >>> pte_t pte; >>> vm_fault_t ret = 0; >>> void *shadow = NULL; >>> + int nr_pages = 1; >>> + unsigned long start_address = vmf->address; >>> + pte_t *start_pte = vmf->pte; >> >> possible bug?: there are code paths that assign to vmf-pte below in this >> function, so couldn't start_pte be stale in some cases? I'd just do the >> assignment (all 4 of these variables in fact) in an else clause below, after any >> messing about with them is complete. >> >> nit: rename start_pte -> start_ptep ? > > Agreed. > >> >>> + bool any_swap_shared = false; >> >> Suggest you defer initialization of this to your "We hit large folios in >> swapcache" block below, and init it to: >> >> any_swap_shared = !pte_swp_exclusive(vmf->pte); >> >> Then the any_shared semantic in swap_pte_batch() can be the same as for >> folio_pte_batch(). >> >>> >>> if (!pte_unmap_same(vmf)) >>> goto out; >>> @@ -4137,6 +4141,35 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) >>> */ >>> vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, >>> &vmf->ptl); >> >> bug: vmf->pte may be NULL and you are not checking it until check_pte:. Byt you >> are using it in this block. It also seems odd to do all the work in the below >> block under the PTL but before checking if the pte has changed. Suggest moving >> both checks here. > > agreed. > >> >>> + >>> + /* We hit large folios in swapcache */ >>> + if (start_pte && folio_test_large(folio) && folio_test_swapcache(folio)) { >> >> What's the start_pte check protecting? > > This is exactly protecting the case vmf->pte==NULL but for some reason it was > assigned in the beginning of the function incorrectly. The intention of the code > was actually doing start_pte = vmf->pte after "vmf->pte = pte_offset_map_lock". > >> >>> + int nr = folio_nr_pages(folio); >>> + int idx = folio_page_idx(folio, page); >>> + unsigned long folio_start = vmf->address - idx * PAGE_SIZE; >>> + unsigned long folio_end = folio_start + nr * PAGE_SIZE; >>> + pte_t *folio_ptep; >>> + pte_t folio_pte; >>> + >>> + if (unlikely(folio_start < max(vmf->address & PMD_MASK, vma->vm_start))) >>> + goto check_pte; >>> + if (unlikely(folio_end > pmd_addr_end(vmf->address, vma->vm_end))) >>> + goto check_pte; >>> + >>> + folio_ptep = vmf->pte - idx; >>> + folio_pte = ptep_get(folio_ptep); >>> + if (!is_swap_pte(folio_pte) || non_swap_entry(pte_to_swp_entry(folio_pte)) || >>> + swap_pte_batch(folio_ptep, nr, folio_pte, &any_swap_shared) != nr) >>> + goto check_pte; >>> + >>> + start_address = folio_start; >>> + start_pte = folio_ptep; >>> + nr_pages = nr; >>> + entry = folio->swap; >>> + page = &folio->page; >>> + } >>> + >>> +check_pte: >>> if (unlikely(!vmf->pte || !pte_same(ptep_get(vmf->pte), vmf->orig_pte))) >>> goto out_nomap; >>> >>> @@ -4190,6 +4223,10 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) >>> */ >>> exclusive = false; >>> } >>> + >>> + /* Reuse the whole large folio iff all entries are exclusive */ >>> + if (nr_pages > 1 && any_swap_shared) >>> + exclusive = false; >> >> If you init any_shared with the firt pte as I suggested then you could just set >> exclusive = !any_shared at the top of this if block without needing this >> separate fixup. > > Since your swap_pte_batch() function checks that all PTEs have the same > exclusive bits, I'll be removing any_shared first in version 3 per David's > suggestions. We could potentially develop "any_shared" as an incremental > patchset later on . Ahh yes, good point. I'll admit that your conversation about this went over my head at the time since I hadn't yet looked at this. > >>> } >>> >>> /* >>> @@ -4204,12 +4241,14 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) >>> * We're already holding a reference on the page but haven't mapped it >>> * yet. >>> */ >>> - swap_free(entry); >>> + swap_free_nr(entry, nr_pages); >>> if (should_try_to_free_swap(folio, vma, vmf->flags)) >>> folio_free_swap(folio); >>> >>> - inc_mm_counter(vma->vm_mm, MM_ANONPAGES); >>> - dec_mm_counter(vma->vm_mm, MM_SWAPENTS); >>> + folio_ref_add(folio, nr_pages - 1); >>> + add_mm_counter(vma->vm_mm, MM_ANONPAGES, nr_pages); >>> + add_mm_counter(vma->vm_mm, MM_SWAPENTS, -nr_pages); >>> + >>> pte = mk_pte(page, vma->vm_page_prot); >>> >>> /* >>> @@ -4219,33 +4258,34 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) >>> * exclusivity. >>> */ >>> if (!folio_test_ksm(folio) && >>> - (exclusive || folio_ref_count(folio) == 1)) { >>> + (exclusive || (folio_ref_count(folio) == nr_pages && >>> + folio_nr_pages(folio) == nr_pages))) { >>> if (vmf->flags & FAULT_FLAG_WRITE) { >>> pte = maybe_mkwrite(pte_mkdirty(pte), vma); >>> vmf->flags &= ~FAULT_FLAG_WRITE; >>> } >>> rmap_flags |= RMAP_EXCLUSIVE; >>> } >>> - flush_icache_page(vma, page); >>> + flush_icache_pages(vma, page, nr_pages); >>> if (pte_swp_soft_dirty(vmf->orig_pte)) >>> pte = pte_mksoft_dirty(pte); >>> if (pte_swp_uffd_wp(vmf->orig_pte)) >>> pte = pte_mkuffd_wp(pte); >> >> I'm not sure about all this... you are smearing these SW bits from the faulting >> PTE across all the ptes you are mapping. Although I guess actually that's ok >> because swap_pte_batch() only returns a batch with all these bits the same? > > Initially, I didn't recognize the issue at all because the tested > architecture arm64 > didn't include these bits. However, after reviewing your latest swpout series, > which verifies the consistent bits for soft_dirty and uffd_wp, I now > feel its safety > even for platforms with these bits. Yep, agreed. > >> >>> - vmf->orig_pte = pte; >> >> Instead of doing a readback below, perhaps: >> >> vmf->orig_pte = pte_advance_pfn(pte, nr_pages); > > Nice ! > >> >>> >>> /* ksm created a completely new copy */ >>> if (unlikely(folio != swapcache && swapcache)) { >>> - folio_add_new_anon_rmap(folio, vma, vmf->address); >>> + folio_add_new_anon_rmap(folio, vma, start_address); >>> folio_add_lru_vma(folio, vma); >>> } else { >>> - folio_add_anon_rmap_pte(folio, page, vma, vmf->address, >>> - rmap_flags); >>> + folio_add_anon_rmap_ptes(folio, page, nr_pages, vma, start_address, >>> + rmap_flags); >>> } >>> >>> VM_BUG_ON(!folio_test_anon(folio) || >>> (pte_write(pte) && !PageAnonExclusive(page))); >>> - set_pte_at(vma->vm_mm, vmf->address, vmf->pte, pte); >>> - arch_do_swap_page(vma->vm_mm, vma, vmf->address, pte, vmf->orig_pte); >>> + set_ptes(vma->vm_mm, start_address, start_pte, pte, nr_pages); >>> + vmf->orig_pte = ptep_get(vmf->pte); >>> + arch_do_swap_page(vma->vm_mm, vma, start_address, pte, pte); >>> >>> folio_unlock(folio); >>> if (folio != swapcache && swapcache) { >>> @@ -4269,7 +4309,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) >>> } >>> >>> /* No need to invalidate - it was non-present before */ >>> - update_mmu_cache_range(vmf, vma, vmf->address, vmf->pte, 1); >>> + update_mmu_cache_range(vmf, vma, start_address, start_pte, nr_pages); >>> unlock: >>> if (vmf->pte) >>> pte_unmap_unlock(vmf->pte, vmf->ptl); >> > > Thanks > Barry