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 87397C02180 for ; Wed, 15 Jan 2025 06:43:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1CAF76B008A; Wed, 15 Jan 2025 01:43:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 17CDB6B008C; Wed, 15 Jan 2025 01:43:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 043236B0092; Wed, 15 Jan 2025 01:42:59 -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 D9C6E6B008A for ; Wed, 15 Jan 2025 01:42:59 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 598BDB05B6 for ; Wed, 15 Jan 2025 06:42:59 +0000 (UTC) X-FDA: 83008743678.21.33EA881 Received: from mail-ua1-f48.google.com (mail-ua1-f48.google.com [209.85.222.48]) by imf27.hostedemail.com (Postfix) with ESMTP id 81D9340003 for ; Wed, 15 Jan 2025 06:42:57 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=cVmWUHH6; spf=pass (imf27.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.222.48 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736923377; 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=jfT5j66TIy5bOj0HZ0JrS0SuQCmOp5snddIcXxEymy8=; b=WfKQjh2gSdLax9e5ap8+k1OwgsV8hs+Sn1/Ry32tVqL+0xl7lQcVXrlbXbmoMQyBx0u037 /8KQMiLvrRPPjLZbCi/i53+66nJRgibNZ1HmoLx31qd+Yh+0X7IULoBPhNlvEvMioI+7Ab 07DidDSycoVUKvra2T1E1/BTBt9LEcE= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736923377; a=rsa-sha256; cv=none; b=ZoHw6En8LGBe2M5kRg+brSrHkVXifHl03hNUfDDWsdlpx8Yy1DygTCWlsRUBILo1D5rGnD 2suhrDqadun7nIjIe1OmYfeJs3N/jWwL0ZTijea39fZ1i2PEIkdkDEc93ng0ldOdetCAaX 0cDXgUATtijfWjEwq88iZU/8ucUKWlw= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=cVmWUHH6; spf=pass (imf27.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.222.48 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-ua1-f48.google.com with SMTP id a1e0cc1a2514c-85b92397fe2so1473719241.3 for ; Tue, 14 Jan 2025 22:42:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736923376; x=1737528176; 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=jfT5j66TIy5bOj0HZ0JrS0SuQCmOp5snddIcXxEymy8=; b=cVmWUHH6CmRIbc+ezuRd8rXJZzSB1f+LghG3J/T44DK7GViimzxFy7PK/xPXqLbjIK c7QGNklpG8Ueal9o58IJBFMAr5Q2FWYXZ+et2ncp6g5lQo3f14rqynu7Yd3hu/zUlQu9 xI9okz5mZhUG0tbIuaZINmBiQ7l700r2b8k92ileJ4k+cs1GAEkMnWfd9wh+XP3PN7Cd m7q0ArXpAvWTm1rjvw5jcHRKiKYR3CSqjMFNRQzbvNj0nN2FyeHVh/l7She/LLwD3p1X irzTgW7fUvgfqhZbMUk2WZR3JI6ahnA8SCaB175eHjDkZgTaJeQoeBVQ5r9lPA3xf3EU x0qQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736923376; x=1737528176; 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=jfT5j66TIy5bOj0HZ0JrS0SuQCmOp5snddIcXxEymy8=; b=OVtHgzYdBjKxDVF0COZk/pMT5G8LW0JNoTt6aDIZoATv9anI06sIx5POxqGU29D12O qQGy61Ezh33hBCx+x+sqz4BEp+ihx8fz9btuo6r5sZE0T9PRo3ai6bSLnG8ubTbrjNRW J06uN+vFX+vuuFE85z+/a7z4Bc532x7QIUJge9BNbBRFPEEhRlkAEIcgWr1IvYrM11Fw vQFc3k/tsTC8ngiRvFPATAXXQvsaxL2BGKSDF3TTN+Lk9R66EJ+larNaVZBnccRm+gdX 6sG2Y6UWIOIm/XBMisvjYT2OYouD8jKi1wRkCapj1g8URenUKj7OLuyCQVleOx5Uw72K bo/A== X-Forwarded-Encrypted: i=1; AJvYcCUksBZbNFhogFZHcun61pWgAKQy5WXta4h3Q7k9hDW//Z+z+14dlKxb0bEAZ1WFc9JDHqpIbuu0aQ==@kvack.org X-Gm-Message-State: AOJu0Yy4twRS2KWGxTtUYQnDDz+42TYhFZXENVXQs3OMDIHrgkF1OBXg yrFuFz/B7VFWluwYakWJ855zg+VKmrwf2p6AzVLCSE95pDbz637AHUfE3/pBXn+ladJgYTtZWC+ dBgwCgYvYYjC1zvt6VHb2+aEr8Ck= X-Gm-Gg: ASbGnctciYMw6B1ZWspqKTkyYQ6COeWs1r+1WSSphGyXIypFxd4I4guWt8iA/Fz37w6 P7rL/oH3wKVrksbxf6Oa0oYrau2ZBs+X9QtFIkulLHw1qsaX/0LsljqA9MaE8OOW99dblbZ61 X-Google-Smtp-Source: AGHT+IFeA886AAdmTgRpzsWoaJeHbplRnLYE/tk44/GUv9PSa7EdHW1js/dQwdEYkSzZc/Q6+8MgT3aaSUhpMnhPpms= X-Received: by 2002:a05:6102:3582:b0:4af:fa7d:71bd with SMTP id ada2fe7eead31-4b3d0f66d4amr26682282137.3.1736923376403; Tue, 14 Jan 2025 22:42:56 -0800 (PST) MIME-Version: 1.0 References: <20250115033808.40641-1-21cnbao@gmail.com> <20250115033808.40641-5-21cnbao@gmail.com> In-Reply-To: From: Barry Song <21cnbao@gmail.com> Date: Wed, 15 Jan 2025 19:42:43 +1300 X-Gm-Features: AbW1kvY3joK1Uy5NZW3NFfAWiiTRxsilAJP4JjE8-wj2Jry8A1MN1eOJwrgLAZc Message-ID: Subject: Re: [PATCH v3 4/4] mm: Avoid splitting pmd for lazyfree pmd-mapped THP in try_to_unmap To: Lance Yang Cc: akpm@linux-foundation.org, linux-mm@kvack.org, baolin.wang@linux.alibaba.com, chrisl@kernel.org, david@redhat.com, kasong@tencent.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.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-Rspamd-Queue-Id: 81D9340003 X-Rspam-User: X-Rspamd-Server: rspam07 X-Stat-Signature: zjd97x8wo4c8wqme4dfrafjefrsgjbqs X-HE-Tag: 1736923377-920168 X-HE-Meta: U2FsdGVkX194b+X+lwN9ni3e3vjbwftGn1ZIc5ehcZKWZLaCwTUn3FdCebt11fDkHSNThcq3VipPwuZyrwiklITTxK9Bl1Dos0FOmiLaDolkswoRKJMZXH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TI9aRCfkCl1iCXqD5Yst+e6BfNT2uoHWSLP6Kow8dgPeI2U0pokAr6s7MjE29OmPbezslH/rOzSBk8tyimpTFG4F6B3YT1aIFrqu0a/z5llyy31RSsH0HlBbGDuUpp4W8igPh+lvrgC5Z40XqKCbbFd2yQWoLkqM8NqdT3T+/NPycB2ygiVNsOEFjtL+ggu3gL/yJRSsVHVNPJ/BVsDpb62x2NyNF7HD0u2A== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000010, 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 Wed, Jan 15, 2025 at 7:26=E2=80=AFPM Lance Yang wr= ote: > > On Wed, Jan 15, 2025 at 1:09=E2=80=AFPM Barry Song <21cnbao@gmail.com> wr= ote: > > > > On Wed, Jan 15, 2025 at 6:01=E2=80=AFPM Lance Yang wrote: > > > > > > On Wed, Jan 15, 2025 at 11:38=E2=80=AFAM Barry Song <21cnbao@gmail.co= m> 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 proces= s > > > > 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_F= REE, > > > > then measures the time taken to perform memory reclamation (actuall= y > > > > 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_= 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 =3D clock(); > > > > madvise((void *)p, SIZE, MADV_PAGEOUT); > > > > clock_t end_time =3D clock(); > > > > > > > > double elapsed_time =3D (double)(end_time - start_t= ime) / CLOCKS_PER_SEC; > > > > printf("Time taken by reclamation: %f seconds\n", e= lapsed_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 i= t > > > > 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 | 24 +++++++++++++++++------- > > > > mm/rmap.c | 13 ++++++++++--- > > > > 2 files changed, 27 insertions(+), 10 deletions(-) > > > > > > > > diff --git a/mm/huge_memory.c b/mm/huge_memory.c > > > > index 3d3ebdc002d5..47cc8c3f8f80 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 =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_DROPPAB= LE)) { > > > > + folio_set_swapbacked(folio); > > > > return false; > > > > + } > > > > > > If either the PMD or the folio is dirty, should we just return false = right away, > > > regardless of VM_DROPPABLE? There=E2=80=99s no need to proceed furthe= r in that > > > case, IMHO ;) > > > > I don't quite understand you, but we need to proceed to clear pmd entry= . > > if vm_droppable is true, even if the folio is dirty, we still drop the = folio. > > Hey barry, > > One thing I still don=E2=80=99t quite understand is as follows: > > One of the semantics of VM_DROPPABLE is that, under memory pressure, > the kernel can drop the pages. Similarly, for MADV_FREE, one of its > semantics is that the kernel can free the pages when memory pressure > occurs, but only if there is no subsequent write (i.e., the PMD is clean)= . > > So, if VM_DROPPABLE is true, we still drop the folio even if it's dirty. = This > seems to conflict with the semantics of MADV_FREE, which requires the > folio or PMD to be clean before being dropped. wdyt? I think we can simply revisit the patch where VM_DROPPABLE was introduced: commit 9651fcedf7b92d3f7 "mm: add MAP_DROPPABLE for designating always lazily freeable mappings" > > Thanks, > Lance > > > > > > > > > > > > Thanks, > > > Lance > > > > > > > > > > > orig_pmd =3D 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 !=3D map_count + 1) { > > > > + if (pmd_dirty(orig_pmd)) > > > > + folio_set_dirty(folio); > > > > + if (folio_test_dirty(folio) && !(vma->vm_flags & VM_DROPPAB= LE)) { > > > > + 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; > > > > } > > > > @@ -3119,12 +3130,11 @@ bool unmap_huge_pmd_locked(struct vm_area_s= truct *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_FOLIO(!folio_test_anon(folio), folio); > > > > + VM_WARN_ON_FOLIO(folio_test_swapbacked(folio), folio); > > > > VM_WARN_ON_ONCE(!IS_ALIGNED(addr, HPAGE_PMD_SIZE)); > > > > > > > > - if (folio_test_anon(folio) && !folio_test_swapbacked(folio)= ) > > > > - return __discard_anon_folio_pmd_locked(vma, addr, p= mdp, folio); > > > > - > > > > - return false; > > > > + return __discard_anon_folio_pmd_locked(vma, addr, pmdp, fol= io); > > > > } > > > > > > > > static void remap_page(struct folio *folio, unsigned long nr, int = flags) > > > > diff --git a/mm/rmap.c b/mm/rmap.c > > > > index be1978d2712d..a859c399ec7c 100644 > > > > --- a/mm/rmap.c > > > > +++ b/mm/rmap.c > > > > @@ -1724,9 +1724,16 @@ static bool try_to_unmap_one(struct folio *f= olio, struct vm_area_struct *vma, > > > > } > > > > > > > > if (!pvmw.pte) { > > > > - if (unmap_huge_pmd_locked(vma, pvmw.address= , pvmw.pmd, > > > > - folio)) > > > > - goto walk_done; > > > > + if (folio_test_anon(folio) && !folio_test_s= wapbacked(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 deci= ded to retain it > > > > + * due to GUP or speculative refere= nces. > > > > + */ > > > > + goto walk_abort; > > > > + } > > > > > > > > if (flags & TTU_SPLIT_HUGE_PMD) { > > > > /* > > > > -- > > > > 2.39.3 (Apple Git-146) > > > > Thanks Barry