From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CC80D4400 for ; Thu, 11 Dec 2025 04:21:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765426902; cv=none; b=IWeGcnIEQ8iahWSlGsWFHu0wCAfXFbN+Y438vCHuxMQakJLMIFf27891SN8GufFVIffDgNQvBHqTYAhLj2JTBLPXIKQtr/ItqwZnS0jVCTcMbP5wsAnOuTb1JIlBGRms27h13zhFNpUIz2lD945o7Nwm+yUN/Mvl2+v7aOjVyEM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765426902; c=relaxed/simple; bh=+TbSWk+GUJBaMy98wlb0g5FkLcriQKN7GQexhAcvBBc=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=mwFRnam488VOgD0BoRd8EiR+v9kTqS+FmQ4BOiPaOIYLi4KY1VycFO9Y7trBiWIVonm5WkKVxve2Gj7NR3hN/d4otd0gHZv4QbA2izf66EHWfXe40MX/eFCWbPBZ/mQNpHqD3B59NYHDUIJSo8fORcz0A+BshkGvPYBJ13ZQ3Ts= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=V8MYIOfD; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="V8MYIOfD" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1765426898; 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: in-reply-to:in-reply-to:references:references; bh=6yd3nUaH+JVSycTF7AKt0rbB2xusebsv+tukG2IUDQs=; b=V8MYIOfDfWTD0A/G9oiNqaAUPsPKjqxnS5g8eVg3HXVqJXfowbHQkTQtm47fkIuNEbpoOc IfShFovn4p1sycQCqh5jqrZAU+vZcLmjbr0Il33yMPOpM7fP0KLL7yCz9MGMknJDaXzcrX PveDf8qE4Hsv1DJnZlFXhb3BLLPOXfw= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-330-iGScH-pBN-K2E6ksIt0ztQ-1; Wed, 10 Dec 2025 23:21:34 -0500 X-MC-Unique: iGScH-pBN-K2E6ksIt0ztQ-1 X-Mimecast-MFC-AGG-ID: iGScH-pBN-K2E6ksIt0ztQ_1765426892 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 401681956060; Thu, 11 Dec 2025 04:21:30 +0000 (UTC) Received: from localhost (unknown [10.72.112.161]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 5F43C19560AD; Thu, 11 Dec 2025 04:21:27 +0000 (UTC) Date: Thu, 11 Dec 2025 12:21:22 +0800 From: Baoquan He To: Kairui Song Cc: linux-mm@kvack.org, Andrew Morton , Barry Song , Chris Li , Nhat Pham , Yosry Ahmed , David Hildenbrand , Johannes Weiner , Youngjun Park , Hugh Dickins , Baolin Wang , Ying Huang , Kemeng Shi , Lorenzo Stoakes , "Matthew Wilcox (Oracle)" , linux-kernel@vger.kernel.org, Kairui Song Subject: Re: [PATCH v4 06/19] mm, swap: free the swap cache after folio is mapped Message-ID: References: <20251205-swap-table-p2-v4-0-cb7e28a26a40@tencent.com> <20251205-swap-table-p2-v4-6-cb7e28a26a40@tencent.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20251205-swap-table-p2-v4-6-cb7e28a26a40@tencent.com> X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 On 12/05/25 at 03:29am, Kairui Song wrote: > From: Kairui Song > > To reduce repeated faults due to parallel swapins of the same PTE, > remove the folio from the swap cache after it is mapped. So new faults > from the swap PTE will be much more likely to see the folio in the swap > cache and wait on it. This does not eliminate all swapin races: an > ongoing swapin fault may still see an empty swap cache. That's harmless, > as the PTE is changed before the swap cache is cleared, so it will just > return and not trigger any repeated faults. It may be clearer to mention that this patch makes change to defer the swap cache freeing from before pte mapping to after pte mapping. Other than this nit pick, it looks good to me. Reviewed-by: Baoquan He > > Signed-off-by: Kairui Song > --- > mm/memory.c | 21 +++++++++++---------- > 1 file changed, 11 insertions(+), 10 deletions(-) > > diff --git a/mm/memory.c b/mm/memory.c > index 3f707275d540..ce9f56f77ae5 100644 > --- a/mm/memory.c > +++ b/mm/memory.c > @@ -4362,6 +4362,7 @@ static vm_fault_t remove_device_exclusive_entry(struct vm_fault *vmf) > static inline bool should_try_to_free_swap(struct swap_info_struct *si, > struct folio *folio, > struct vm_area_struct *vma, > + unsigned int extra_refs, > unsigned int fault_flags) > { > if (!folio_test_swapcache(folio)) > @@ -4384,7 +4385,7 @@ static inline bool should_try_to_free_swap(struct swap_info_struct *si, > * reference only in case it's likely that we'll be the exclusive user. > */ > return (fault_flags & FAULT_FLAG_WRITE) && !folio_test_ksm(folio) && > - folio_ref_count(folio) == (1 + folio_nr_pages(folio)); > + folio_ref_count(folio) == (extra_refs + folio_nr_pages(folio)); > } > > static vm_fault_t pte_marker_clear(struct vm_fault *vmf) > @@ -4936,15 +4937,6 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) > */ > arch_swap_restore(folio_swap(entry, folio), folio); > > - /* > - * Remove the swap entry and conditionally try to free up the swapcache. > - * We're already holding a reference on the page but haven't mapped it > - * yet. > - */ > - swap_free_nr(entry, nr_pages); > - if (should_try_to_free_swap(si, folio, vma, vmf->flags)) > - folio_free_swap(folio); > - > 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); > @@ -4998,6 +4990,15 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) > arch_do_swap_page_nr(vma->vm_mm, vma, address, > pte, pte, nr_pages); > > + /* > + * Remove the swap entry and conditionally try to free up the swapcache. > + * Do it after mapping, so raced page faults will likely see the folio > + * in swap cache and wait on the folio lock. > + */ > + swap_free_nr(entry, nr_pages); > + if (should_try_to_free_swap(si, folio, vma, nr_pages, vmf->flags)) > + folio_free_swap(folio); > + > folio_unlock(folio); > if (unlikely(folio != swapcache)) { > /* > > -- > 2.52.0 >