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 DCD2EE77188 for ; Tue, 14 Jan 2025 06:30:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 41DA06B0085; Tue, 14 Jan 2025 01:30:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3CB4D6B0088; Tue, 14 Jan 2025 01:30:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 292A56B0089; Tue, 14 Jan 2025 01:30:42 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 0CD316B0085 for ; Tue, 14 Jan 2025 01:30:42 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id AFD2A1C730A for ; Tue, 14 Jan 2025 06:30:41 +0000 (UTC) X-FDA: 83005083882.06.5DBAF53 Received: from mail-ed1-f47.google.com (mail-ed1-f47.google.com [209.85.208.47]) by imf27.hostedemail.com (Postfix) with ESMTP id A69C840014 for ; Tue, 14 Jan 2025 06:30:39 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=kVb0s0wx; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf27.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.208.47 as permitted sender) smtp.mailfrom=ioworker0@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736836239; 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=zFvK5oiBH8+JxtcYHZ8dNqgQlFpHniYFsNHsV6mOU4Y=; b=BzEJFkAHCmpEADRtocdNOnxF+PPU5ZYI5K7DypTWrR2xHncm0Z+iQQwDwrx4dT1frk5ciq yya9G+MC+Dpissq4bXUuxf1+IViFtYc6q0b1UaKEZZCvFpaRa8vH3nYNFXtlJhBq9HdiDl k2nsOx/bx+kt5vJGCN0G/i1mAtkQVR4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736836239; a=rsa-sha256; cv=none; b=5Ra/b5Cmd0D7zy0EZi9KUtz9P+IyjL21Og+98+9lbG832fYgOhencOumlqm45bVsoMfh60 9dgrNvgnNBiiRZphlalvYPtDkLP15WQBBgzVhEWUUR9K3VnIDJKVcEAoOmnoVshHFSZbqd L6j1aHaX+D/lxdUPk6cRG05cmh6kp8o= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=kVb0s0wx; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf27.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.208.47 as permitted sender) smtp.mailfrom=ioworker0@gmail.com Received: by mail-ed1-f47.google.com with SMTP id 4fb4d7f45d1cf-5d3e9a88793so8561989a12.1 for ; Mon, 13 Jan 2025 22:30:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736836238; x=1737441038; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=zFvK5oiBH8+JxtcYHZ8dNqgQlFpHniYFsNHsV6mOU4Y=; b=kVb0s0wxdzdVE+Rb1rPcYNHRsf53dXaRyb3KxYI6ViiRcOg3tn1CQXyDY+fsSgZBH8 SE+WYAQLPThQnjkSR/Bs6vCRKwUMuOCGGdmyNBDz7PlLXb6qG2CyICKqS/EJKVbjQgES KRJFUGqbhmRYDVdlVni42p2aPFfbO2amVYf3xWj/np2FpGSod3VnI5VLUg44h+Qt/DWE m6lpB/Yg2U3VAxt2hNEGKM2cFWwhrbrChnSvxVg8dilDSvfTuHo3vRl+Gk6qYm5OAwTb +Hb8py8LrtP1pSfrdwFbNHm72+42B8PBfefQRifKeHtqpKwlfX0J3Vlf/t10ycxC9+vJ z04w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736836238; x=1737441038; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zFvK5oiBH8+JxtcYHZ8dNqgQlFpHniYFsNHsV6mOU4Y=; b=Ks9wwIsMEdUXSCc04bge3AjUZGaTgHUvW9yzSUdNK3mbTTLZiRIA/JIIQXPdkOT+0h ihM0IRrdbAdobluG+qDj7UCD8FVygfUFMDPO5c9d1Q9tKnPsInhErhIGnW64/k0riUIv BZ9sjH7mnl3gIO+6Y+2X+/61slz+jLvnJqv4qDZjn1gpnZp7O4jGeRTPKXE/6Y5jRxdg PDdlfLHIFHCxn3qlAyfvf482lC3v74Ik0lyFvB+nvxT5IHBYI/BJLSyoFGUkf9iEYe8Y XWNsyoZTiujLkPYL542NDVnGPrW1lA7FVBh6+T/PBoFo3GG9cd+PWWHF12yUKFJ9RpUB B8iw== X-Forwarded-Encrypted: i=1; AJvYcCU3gDMqcxHCladbLRhnrsdKPxQURV0W0SktxxX1T2YiQi/rUIif+T6pVxSQy4gDl/nldfugigdb+w==@kvack.org X-Gm-Message-State: AOJu0Yyc5EO6ri1xdyY+syWMEaY1jHbWOl8Gp4WHifpzOFBW8yFRbAZC imJy142nKFODpdD0zIOu8JzBaY1qCdkuVrHqF7/hKgDEcC8id7/2iIHZQSpX6nTX758Jxw8eKl0 qYGYPcBY9EIV8JesTL0xIbSNNdVg= X-Gm-Gg: ASbGncsFIz8j00GMFtnQBx8oBARk9gAmTn8BqulmyRmCAbvBM7/QTtmg849FHct/IwU Cpk6BQttpZVgW2Gl0jMaoy/jxS2sTocyC39FCxDHS X-Google-Smtp-Source: AGHT+IGF2PGcv/ddJcvq18Tqw5yvzNhCGSdnO2VVwGsY9CNG0MdQiK0klb2/fYcTkEUzs3VV5uaT0CaCnBfqbFxV2uc= X-Received: by 2002:a05:6402:51d0:b0:5d2:723c:a559 with SMTP id 4fb4d7f45d1cf-5d972e06b2bmr23233356a12.10.1736836237804; Mon, 13 Jan 2025 22:30:37 -0800 (PST) MIME-Version: 1.0 References: <20250114040914.9986-1-21cnbao@gmail.com> In-Reply-To: <20250114040914.9986-1-21cnbao@gmail.com> From: Lance Yang Date: Tue, 14 Jan 2025 14:30:01 +0800 X-Gm-Features: AbW1kvaplad4TfLLQqpTXzlvv8FYaLHUCpIqhqpG7RAFpIiKP56uUpv9oj3H-zI Message-ID: Subject: Re: [PATCH v2 4/4] mm: Avoid splitting pmd for lazyfree pmd-mapped THP in try_to_unmap To: Barry Song <21cnbao@gmail.com> Cc: baolin.wang@linux.alibaba.com, akpm@linux-foundation.org, chrisl@kernel.org, david@redhat.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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Stat-Signature: etb33yrw9gxpfoyigeude3j4fpn8f739 X-Rspamd-Queue-Id: A69C840014 X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1736836239-643075 X-HE-Meta: U2FsdGVkX1+qoeTKw2BqoZ+83eyuACuU7oaYQYvPe58dDzTDhCqkn//dlwpDZKirKSY2WrokWTM7CVr4fBV2jED93hZD9v1t4U96yMuM6guC3ucxy9UtFRWeDJ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sCJZlMnKqb12w2LAL3o9DU8wi7tHU8fAFXu0IRouIKWRM5Gcf5ilwGHdOY7aTFQN78O/XndN7Uzx/0flfdDTWFQZtEo+DXLrie8eqKyn8aAAh2e0+xX+1IW6+s9E/rbHtFyZAFWXb6pGFIcjRkLgCsL0CEmaYTt4Kx1OD5cN1XpJXg3neCU4Wh3qfnr3JjKqUP16BULefz/eqM+0uuRvOxcWkrmq2MSe/LE5ik9Q== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000012, 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 Tue, Jan 14, 2025 at 12:09=E2=80=AFPM Barry Song <21cnbao@gmail.com> wro= te: > > On Tue, Jan 14, 2025 at 4:40=E2=80=AFPM 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_FRE= E, > > > 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 =3D mmap(0, SIZE, PROT_READ | PROT_WRIT= E, > > > 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 =3D clock(); > > > madvise((void *)p, SIZE, MADV_PAGEOUT); > > > clock_t end_time =3D clock(); > > > > > > double elapsed_time =3D (double)(end_time - start_time)= / CLOCKS_PER_SEC; > > > printf("Time taken by reclamation: %f seconds\n", elaps= ed_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 > > > =E2=80=A6 > > > > > > 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 > > > =E2=80=A6 > > > > > > 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(st= ruct vm_area_struct *vma, > > > int ref_count, map_count; > > > pmd_t orig_pmd =3D *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 =3D pmdp_huge_clear_flush(vma, addr, pmdp); > > > > > > @@ -3098,8 +3102,15 @@ static bool __discard_anon_folio_pmd_locked(st= ruct 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 !=3D 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 !=3D 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 *foli= o, struct vm_area_struct *vma, > > > DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, address, 0); > > > pte_t pteval; > > > struct page *subpage; > > > - bool anon_exclusive, ret =3D true; > > > + bool anon_exclusive, lazyfree, ret =3D true; > > > struct mmu_notifier_range range; > > > enum ttu_flags flags =3D (enum ttu_flags)(long)arg; > > > int nr_pages =3D 1; > > > @@ -1724,9 +1724,18 @@ static bool try_to_unmap_one(struct folio *fol= io, struct vm_area_struct *vma, > > > } > > > > > > if (!pvmw.pte) { > > > + lazyfree =3D folio_test_anon(folio) && !folio_t= est_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: Yep, it would be better to remove the duplicate check in unmap_huge_pmd_locked(), and we can extend unmap_huge_pmd_locked() in the future if needed. Thanks, Lance > > 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, f= olio); > - > - 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, s= truct vm_area_struct *vma, > DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, address, 0); > pte_t pteval; > struct page *subpage; > - bool anon_exclusive, lazyfree, ret =3D true; > + bool anon_exclusive, ret =3D true; > struct mmu_notifier_range range; > enum ttu_flags flags =3D (enum ttu_flags)(long)arg; > int nr_pages =3D 1; > @@ -1724,18 +1724,16 @@ static bool try_to_unmap_one(struct folio *folio,= struct vm_area_struct *vma, > } > > if (!pvmw.pte) { > - lazyfree =3D folio_test_anon(folio) && !folio_tes= t_swapbacked(folio); > - > - if (unmap_huge_pmd_locked(vma, pvmw.address, pvmw= .pmd, > - folio)) > - goto walk_done; > - /* > - * unmap_huge_pmd_locked has either already marke= d > - * 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_swapbac= ked(folio)) { > + if (unmap_huge_pmd_locked(vma, pvmw.addre= ss, pvmw.pmd, folio)) > + goto walk_done; > + /* > + * unmap_huge_pmd_locked has either alrea= dy 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, pv= mw.pmd, > > > folio)) > > > goto walk_done; > > > + /* > > > + * unmap_huge_pmd_locked has either already mar= ked > > > + * the folio as swap-backed or decided to retai= n it > > > + * due to GUP or speculative references. > > > + */ > > > + if (lazyfree) > > > + goto walk_abort; > > > > > > if (flags & TTU_SPLIT_HUGE_PMD) { > > > /*