From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 13C7535953 for ; Tue, 14 Jan 2025 04:09:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736827766; cv=none; b=DFgdN1JJz5X/+NlzycNvlELCLvUeXCwUPpT0n3RqTjlPQPI/ooofNXiuDL+xYtNE0xqqRI6w1BX2bzdbJqOW4CoVC59qJ/ojh633zC4dqcGxWvBarquaAfTispXJFJTZL5Jk1sitWVE3xADnMrq48CBS0w8XCW98/12oQFyOsr8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736827766; c=relaxed/simple; bh=ORsRUZGpTvqWxWjxSYoEuDcIh44ePDu+7emh6cxczpk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=WmWDn6OMbnKHWzOWrOLuqZkdo1KdYqI0hkU0R0HL6fkvvpP0soo1d9O4FNBU6VOiPCGA7Ef9Prjt8au4Mynq3p0xldxxmLCXwV8t7eLXmmzmjbg8JZLJflELW3hzKAnbKzeTcWFmgFzF9zcN5Be2UOU1DBrO9pI2hm5AO2yQkCA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=N5jp39as; arc=none smtp.client-ip=209.85.214.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="N5jp39as" Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-2161eb94cceso60985495ad.2 for ; Mon, 13 Jan 2025 20:09:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736827764; x=1737432564; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=n9OhR8H2gjpx6QaBe0RO8fceTVFYek4y7WXPW0XqYgE=; b=N5jp39aselzvDBuFkX1BfudLl0FWUw1HTR79fctnfpcEnB1nRDSPG+/+19+2w979br GFjzyCmasydsr99vZ+8sXenIrY6eIPaGQdlQioCsaYjVxMu0b6LUvA9tT/WMNHs2ITT0 nD2DyDLBpenjPTEETle4h3Qv6h4GA0Db5IAvLIXQQJ6I5UCKe/Vsd7FK+3SWqPdP/9aw WGUVJ0+d5wzLT4rrJ2jO45X/CQ30+pHgqCCqY9a0C/p4aB2wVTduN7RC6hsnUK6L1abV 53Eorzqr651G9vtLsPZrcVhKBwYC+OtGb80zW2LBHxRzC4ap8Dz35FKSBFh5T6P9NsbR /KZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736827764; x=1737432564; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=n9OhR8H2gjpx6QaBe0RO8fceTVFYek4y7WXPW0XqYgE=; b=Av+Ih4jFTN+yv2hnYaWQqzgfvGOT2JpH3AnUyHu3yWKXSSgrSqV+Gk2QyGUTT3hIMn t1EfonMKHY2x+kkCIG3avSWEKxdOKQnby4O8Afg+g5TeVwj/cPCNMuoY0uokShl10KVZ lO7AIA12ja2LZDIVVFFQpPfUwawgXUNKK8Ss1eeJ+ycbOcQQ8meyusomePXUXqt1Z/cg W5sts6FNUkgzXJaH9//ptw5q4v3oIXnPxCNT50LZrCQETNWQpX9G9Aq5QsetzXxcx8tV FdvmkbGyAStSL0dUyNs7iKkLw8SjBVz1VvKMEM/Ro171Zhjb9IZ7DZcJ1xzFRATY4DZ3 8kfw== X-Forwarded-Encrypted: i=1; AJvYcCWo70ZLCh/AcWnX8sxwGFDA9czd/x62c+ZJSqAaWH6oYvDKB4/aZgMXL41ubq1EP9ScnDG8varXP4tA9vY=@vger.kernel.org X-Gm-Message-State: AOJu0YzS7OWK3D5CEOgdNNYdIaLGe/36Xe3dzyXLQSL4NXCF+1a1uPLs CK9NIebNnfNcm4IPRjmsofAU2IwSwF4JrsOJzVP+Gn6aR2bZwmUu X-Gm-Gg: ASbGncuhphYgRqa8vmXrtvX6+s/ycZDZ4rl6NXX13Nf+hxulOzbob2a6QOJ0eIGc4si gNVLyE9K5hDovDdcQxmw2H+TTpDVrhs9ExLkk3Im80RDgEJhD1xnn4PvgoTtaAbntqtIznEO0KU BxmYAbP7ePm9MyckmAgf+9b8jNHvcuKIDm9HqsBHYCHu6t5g9Jibm03Ii2TrAwq7Qxs7Ksnca0e ZiFp0rx0g9CF0HCcLfKcbAETizD47q3meKGC08w/yqtj6W2B2TUEy6q1wApAPtQ6mx+XzEjNNxZ W/vT5VMK X-Google-Smtp-Source: AGHT+IHT2li0BzNMESB2ucdlAXFPz0+v3u/41Z6CwFvGFPW3mNOsWkDv1pbcf1Z6H258l6sGfOmnJg== X-Received: by 2002:a17:902:ecc5:b0:216:57a6:2887 with SMTP id d9443c01a7336-21a83f5db98mr392066215ad.21.1736827764068; Mon, 13 Jan 2025 20:09:24 -0800 (PST) Received: from Barrys-MBP.hub ([2407:7000:af65:8200:39b5:3f0b:acf3:9158]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2f54a2872a3sm10803658a91.16.2025.01.13.20.09.17 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 13 Jan 2025 20:09:23 -0800 (PST) From: Barry Song <21cnbao@gmail.com> To: baolin.wang@linux.alibaba.com Cc: 21cnbao@gmail.com, akpm@linux-foundation.org, chrisl@kernel.org, david@redhat.com, ioworker0@gmail.com, kasong@tencent.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, lorenzo.stoakes@oracle.com, ryan.roberts@arm.com, v-songbaohua@oppo.com, x86@kernel.org, ying.huang@intel.com, zhengtangquan@oppo.com Subject: Re: [PATCH v2 4/4] mm: Avoid splitting pmd for lazyfree pmd-mapped THP in try_to_unmap Date: Tue, 14 Jan 2025 17:09:14 +1300 Message-Id: <20250114040914.9986-1-21cnbao@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: References: 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=UTF-8 Content-Transfer-Encoding: 8bit On Tue, Jan 14, 2025 at 4:40 PM Baolin Wang wrote: > > > > On 2025/1/13 11:39, Barry Song wrote: > > From: Barry Song > > > > The try_to_unmap_one() function currently handles PMD-mapped THPs > > inefficiently. It first splits the PMD into PTEs, copies the dirty > > state from the PMD to the PTEs, iterates over the PTEs to locate > > the dirty state, and then marks the THP as swap-backed. This process > > involves unnecessary PMD splitting and redundant iteration. Instead, > > this functionality can be efficiently managed in > > __discard_anon_folio_pmd_locked(), avoiding the extra steps and > > improving performance. > > > > The following microbenchmark redirties folios after invoking MADV_FREE, > > then measures the time taken to perform memory reclamation (actually > > set those folios swapbacked again) on the redirtied folios. > > > > #include > > #include > > #include > > #include > > > > #define SIZE 128*1024*1024 // 128 MB > > > > int main(int argc, char *argv[]) > > { > > while(1) { > > volatile int *p = mmap(0, SIZE, PROT_READ | PROT_WRITE, > > MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); > > > > memset((void *)p, 1, SIZE); > > madvise((void *)p, SIZE, MADV_FREE); > > /* redirty after MADV_FREE */ > > memset((void *)p, 1, SIZE); > > > > clock_t start_time = clock(); > > madvise((void *)p, SIZE, MADV_PAGEOUT); > > clock_t end_time = clock(); > > > > double elapsed_time = (double)(end_time - start_time) / CLOCKS_PER_SEC; > > printf("Time taken by reclamation: %f seconds\n", elapsed_time); > > > > munmap((void *)p, SIZE); > > } > > return 0; > > } > > > > Testing results are as below, > > w/o patch: > > ~ # ./a.out > > Time taken by reclamation: 0.007300 seconds > > Time taken by reclamation: 0.007226 seconds > > Time taken by reclamation: 0.007295 seconds > > Time taken by reclamation: 0.007731 seconds > > Time taken by reclamation: 0.007134 seconds > > Time taken by reclamation: 0.007285 seconds > > Time taken by reclamation: 0.007720 seconds > > Time taken by reclamation: 0.007128 seconds > > Time taken by reclamation: 0.007710 seconds > > Time taken by reclamation: 0.007712 seconds > > Time taken by reclamation: 0.007236 seconds > > Time taken by reclamation: 0.007690 seconds > > Time taken by reclamation: 0.007174 seconds > > Time taken by reclamation: 0.007670 seconds > > Time taken by reclamation: 0.007169 seconds > > Time taken by reclamation: 0.007305 seconds > > Time taken by reclamation: 0.007432 seconds > > Time taken by reclamation: 0.007158 seconds > > Time taken by reclamation: 0.007133 seconds > > … > > > > w/ patch > > > > ~ # ./a.out > > Time taken by reclamation: 0.002124 seconds > > Time taken by reclamation: 0.002116 seconds > > Time taken by reclamation: 0.002150 seconds > > Time taken by reclamation: 0.002261 seconds > > Time taken by reclamation: 0.002137 seconds > > Time taken by reclamation: 0.002173 seconds > > Time taken by reclamation: 0.002063 seconds > > Time taken by reclamation: 0.002088 seconds > > Time taken by reclamation: 0.002169 seconds > > Time taken by reclamation: 0.002124 seconds > > Time taken by reclamation: 0.002111 seconds > > Time taken by reclamation: 0.002224 seconds > > Time taken by reclamation: 0.002297 seconds > > Time taken by reclamation: 0.002260 seconds > > Time taken by reclamation: 0.002246 seconds > > Time taken by reclamation: 0.002272 seconds > > Time taken by reclamation: 0.002277 seconds > > Time taken by reclamation: 0.002462 seconds > > … > > > > This patch significantly speeds up try_to_unmap_one() by allowing it > > to skip redirtied THPs without splitting the PMD. > > > > Suggested-by: Baolin Wang > > Suggested-by: Lance Yang > > Signed-off-by: Barry Song > > --- > > mm/huge_memory.c | 17 ++++++++++++++--- > > mm/rmap.c | 11 ++++++++++- > > 2 files changed, 24 insertions(+), 4 deletions(-) > > > > diff --git a/mm/huge_memory.c b/mm/huge_memory.c > > index 3d3ebdc002d5..aea49f7125f1 100644 > > --- a/mm/huge_memory.c > > +++ b/mm/huge_memory.c > > @@ -3070,8 +3070,12 @@ static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma, > > int ref_count, map_count; > > pmd_t orig_pmd = *pmdp; > > > > - if (folio_test_dirty(folio) || pmd_dirty(orig_pmd)) > > + if (pmd_dirty(orig_pmd)) > > + folio_set_dirty(folio); > > + if (folio_test_dirty(folio) && !(vma->vm_flags & VM_DROPPABLE)) { > > + folio_set_swapbacked(folio); > > return false; > > + } > > > > orig_pmd = pmdp_huge_clear_flush(vma, addr, pmdp); > > > > @@ -3098,8 +3102,15 @@ static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma, > > * > > * The only folio refs must be one from isolation plus the rmap(s). > > */ > > - if (folio_test_dirty(folio) || pmd_dirty(orig_pmd) || > > - ref_count != map_count + 1) { > > + if (pmd_dirty(orig_pmd)) > > + folio_set_dirty(folio); > > + if (folio_test_dirty(folio) && !(vma->vm_flags & VM_DROPPABLE)) { > > + folio_set_swapbacked(folio); > > + set_pmd_at(mm, addr, pmdp, orig_pmd); > > + return false; > > + } > > + > > + if (ref_count != map_count + 1) { > > set_pmd_at(mm, addr, pmdp, orig_pmd); > > return false; > > } > > diff --git a/mm/rmap.c b/mm/rmap.c > > index 3ef659310797..02c4e4b2cd7b 100644 > > --- a/mm/rmap.c > > +++ b/mm/rmap.c > > @@ -1671,7 +1671,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, > > DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, address, 0); > > pte_t pteval; > > struct page *subpage; > > - bool anon_exclusive, ret = true; > > + bool anon_exclusive, lazyfree, ret = true; > > struct mmu_notifier_range range; > > enum ttu_flags flags = (enum ttu_flags)(long)arg; > > int nr_pages = 1; > > @@ -1724,9 +1724,18 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, > > } > > > > if (!pvmw.pte) { > > + lazyfree = folio_test_anon(folio) && !folio_test_swapbacked(folio); > > You've checked lazyfree here, so can we remove the duplicate check in > unmap_huge_pmd_locked()? Then the code should be: > > if (lazyfree && unmap_huge_pmd_locked(...)) > goto walk_done; right. it seems unmap_huge_pmd_locked() only handles lazyfree pmd-mapped thp. so i guess the code could be: diff --git a/mm/huge_memory.c b/mm/huge_memory.c index aea49f7125f1..c4c3a7896de4 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -3131,11 +3131,10 @@ bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr, VM_WARN_ON_FOLIO(!folio_test_pmd_mappable(folio), folio); VM_WARN_ON_FOLIO(!folio_test_locked(folio), folio); VM_WARN_ON_ONCE(!IS_ALIGNED(addr, HPAGE_PMD_SIZE)); + VM_WARN_ON_FOLIO(!folio_test_anon(folio), folio); + VM_WARN_ON_FOLIO(folio_test_swapbacked(folio), folio); - if (folio_test_anon(folio) && !folio_test_swapbacked(folio)) - return __discard_anon_folio_pmd_locked(vma, addr, pmdp, folio); - - return false; + return __discard_anon_folio_pmd_locked(vma, addr, pmdp, folio); } static void remap_page(struct folio *folio, unsigned long nr, int flags) diff --git a/mm/rmap.c b/mm/rmap.c index 02c4e4b2cd7b..72907eb1b8fe 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1671,7 +1671,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, address, 0); pte_t pteval; struct page *subpage; - bool anon_exclusive, lazyfree, ret = true; + bool anon_exclusive, ret = true; struct mmu_notifier_range range; enum ttu_flags flags = (enum ttu_flags)(long)arg; int nr_pages = 1; @@ -1724,18 +1724,16 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, } if (!pvmw.pte) { - lazyfree = folio_test_anon(folio) && !folio_test_swapbacked(folio); - - if (unmap_huge_pmd_locked(vma, pvmw.address, pvmw.pmd, - folio)) - goto walk_done; - /* - * unmap_huge_pmd_locked has either already marked - * the folio as swap-backed or decided to retain it - * due to GUP or speculative references. - */ - if (lazyfree) + if (folio_test_anon(folio) && !folio_test_swapbacked(folio)) { + if (unmap_huge_pmd_locked(vma, pvmw.address, pvmw.pmd, folio)) + goto walk_done; + /* + * unmap_huge_pmd_locked has either already marked + * the folio as swap-backed or decided to retain it + * due to GUP or speculative references. + */ goto walk_abort; + } if (flags & TTU_SPLIT_HUGE_PMD) { /* > > > if (unmap_huge_pmd_locked(vma, pvmw.address, pvmw.pmd, > > folio)) > > goto walk_done; > > + /* > > + * unmap_huge_pmd_locked has either already marked > > + * the folio as swap-backed or decided to retain it > > + * due to GUP or speculative references. > > + */ > > + if (lazyfree) > > + goto walk_abort; > > > > if (flags & TTU_SPLIT_HUGE_PMD) { > > /*