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 94103C02180 for ; Wed, 15 Jan 2025 07:01:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D48CE6B0085; Wed, 15 Jan 2025 02:01:52 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CD0AE6B0088; Wed, 15 Jan 2025 02:01:52 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B49AC6B0089; Wed, 15 Jan 2025 02:01:52 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 91FDF6B0085 for ; Wed, 15 Jan 2025 02:01:52 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 4C2051C7094 for ; Wed, 15 Jan 2025 07:01:52 +0000 (UTC) X-FDA: 83008791264.11.AA38024 Received: from mail-ed1-f54.google.com (mail-ed1-f54.google.com [209.85.208.54]) by imf02.hostedemail.com (Postfix) with ESMTP id 4D92080009 for ; Wed, 15 Jan 2025 07:01:50 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=WVWWkOpR; spf=pass (imf02.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.208.54 as permitted sender) smtp.mailfrom=ioworker0@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736924510; a=rsa-sha256; cv=none; b=PpGFpsAUnq11imh688i5flsidG4qg87QdOLZFDlGCfP1IJ7hb7Lbvg08GjHciKSVHteuV/ q8r6Znq51yEctRh8aavXRVx8WgUWUVlb1dSeX9Mp/KwmJwTeULKy+rroAMlxoLe1peUyfw oxarv7F6S0LSXnr87rrYwqSbAIJdZmA= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=WVWWkOpR; spf=pass (imf02.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.208.54 as permitted sender) smtp.mailfrom=ioworker0@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=1736924510; 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=6O3QZsGSuRSWHlL1j6E1TG5WwZq7nPfjNi9Rr7JBI/E=; b=cTVP31b6QnsYaUoefHDGu8H+RGC58++y5u0TdBbEN1uQpzSqd6IzhLsdhADUMgwxwiYtLc IPjght7Dx8BFRleSrq3dLix2aDXow6C/YBaYpYBXKZWEo/lFknWxdmMLoWgYs9vZ4/FN77 5CXoNlclkMiAczwHD0OdrY/N5xJGyCU= Received: by mail-ed1-f54.google.com with SMTP id 4fb4d7f45d1cf-5d3d14336f0so10857536a12.3 for ; Tue, 14 Jan 2025 23:01:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736924509; x=1737529309; 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=6O3QZsGSuRSWHlL1j6E1TG5WwZq7nPfjNi9Rr7JBI/E=; b=WVWWkOpRO5zAe2RzqmSst0asoRjoMucefDvzNSBJudaRKxhA8l3jZHxTkz6Up4mjnr JEKVQI72IbsUesUZ0ncPx2uqbtGf2WwKeClOCOtYrOiRtbr80Ox75qm/YP0NAHRHd1sa J63pqmcotuyOYIExv4YqtZ2j22jh1cw51puKwNefn1BZVfQIrd+XV/76ink2C5Ze5/0o AABTNQbIizcTMBvNoHSCn0NW23Vj2kRE/FV+8RRU6XrwjO6uBk0JbQYCx9/Em2MwTu9v zzttZvmZF4sfXw1bNMpUJgxKhrEqfjRH+7ghAeoeIkPMMl8JL7WGH04LjBC07JLDR1LS LhVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736924509; x=1737529309; 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=6O3QZsGSuRSWHlL1j6E1TG5WwZq7nPfjNi9Rr7JBI/E=; b=jyoOFMB4w95f2cJqsst6aJ9M2i4dd9P05n28ZwuZX3VV2vkl9Lk+LEq9cO6SeazqXr C7V7uD+f7lEoedz36oPV5Fuuq4sN5S90rB9/fKlFzpiv9X7nNxWvLl00pworkhpwoR5T IKSMDQl0uJWWrN4TM5TvteL2OE356WHrkbk9Xs3hRuBFnK5Cp4KtUz3zKc4MvL+s2eM5 1NKZBx70oP8coanAFQSLzdVry00AhB11y8lApoUp11pM0cCeL2t7vyxim/5PFJC5lXFX JsNHFJ67v3QqcwMFtMhC5HCXmHC7lf6X6fp3aU3Hx+5vZpwtMUqkS7hBOcDykUjC6x2U F/iA== X-Forwarded-Encrypted: i=1; AJvYcCW3hNrUlgqvIXAw7hoc2drxGzmeBI50OWPmEavgzwKQwCiDw+xd9afxYn0Er7OmOMWv3cIKxKP12A==@kvack.org X-Gm-Message-State: AOJu0YykRHpnG9HGe7Ua6oGdnMRhkE6pDHRgnO4wjEpOdzSQ69uU6Vcl er9vYlhbbRloINna9UhDHytIT+BRkAe1BEEK8rMMPo8M3wXcqpPTBHFWQ6iZcmhzu/QNyIVMfWW cm2PoyzXFrZ7Rmln49X6sIzp4bck= X-Gm-Gg: ASbGncsDpGNc6CBDLrMKRUaddJxsn1iHxkx1Gij1qiOT4DMwLHEQrzoX34glcLeIS5G 6nh5zQF55pJ6Bl+Bg1uXPwBOb0fF9584402Y0hQ== X-Google-Smtp-Source: AGHT+IHTr/h5l3etOcELLw+AEUaUlNzZAGjDNs/fSDwtY8uTelZHVRZmbRM6DxRV5P4bU3LQyIMJ1k/3bslZiWZXe6A= X-Received: by 2002:a05:6402:2689:b0:5d2:7383:a3b7 with SMTP id 4fb4d7f45d1cf-5d972e6482cmr24311616a12.23.1736924508474; Tue, 14 Jan 2025 23:01:48 -0800 (PST) MIME-Version: 1.0 References: <20250115033808.40641-1-21cnbao@gmail.com> <20250115033808.40641-5-21cnbao@gmail.com> In-Reply-To: From: Lance Yang Date: Wed, 15 Jan 2025 15:01:11 +0800 X-Gm-Features: AbW1kvYtB-OTeTnzBW80sbgsK3g7LTcWu2tQUEvRIy_gNS43i-szryvnWHRhFtY Message-ID: Subject: Re: [PATCH v3 4/4] mm: Avoid splitting pmd for lazyfree pmd-mapped THP in try_to_unmap 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, 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: 4D92080009 X-Stat-Signature: i9exbdgwhtbocjg8e41y4g9becesfhuj X-Rspam-User: X-Rspamd-Server: rspam09 X-HE-Tag: 1736924510-79481 X-HE-Meta: U2FsdGVkX1/qki2X80nOgPVJRCwrWg04Pk3nxqIcMdMibpG+7lvpnr6lePyKTe/iJxJTJv9hnolBkuIYcI770djZyNKqoq+o3PyDl1vULKFaZ9nsFxZpLAu/naqm3luqVTxHMktrS7HGIz/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Su9qFJEWgV3a/hHdtztQFJMEx1c5lk+TkFj4mvPA2hANUjnx4VsDcZeZhxfsAQFKMfKOny6cNecJ+8smH5YjCaXIUPTVFDCkg0rxUgLZLdIx0z7UqIoTQvA6SlpKR3Z35FBwmUqnxoXAuflb59h7OEHniYGgpehW8pigZBqoFOlPAn5yOnrSkKZQSWckQaa8dTCS5jRGPgYy+LFHm8e4+l4A== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, 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 2:42=E2=80=AFPM Barry Song <21cnbao@gmail.com> wrot= e: > > On Wed, Jan 15, 2025 at 7:26=E2=80=AFPM Lance Yang = wrote: > > > > On Wed, Jan 15, 2025 at 1:09=E2=80=AFPM Barry Song <21cnbao@gmail.com> = wrote: > > > > > > 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.= com> 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 dirt= y > > > > > 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 proc= ess > > > > > involves unnecessary PMD splitting and redundant iteration. Inste= ad, > > > > > 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 (actua= lly > > > > > 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 | PRO= T_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= _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 > > > > > =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 | 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_locke= d(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_DROPP= ABLE)) { > > > > > + folio_set_swapbacked(folio); > > > > > return false; > > > > > + } > > > > > > > > If either the PMD or the folio is dirty, should we just return fals= e right away, > > > > regardless of VM_DROPPABLE? There=E2=80=99s no need to proceed furt= her in that > > > > case, IMHO ;) > > > > > > I don't quite understand you, but we need to proceed to clear pmd ent= ry. > > > if vm_droppable is true, even if the folio is dirty, we still drop th= e 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 clea= n). > > > > 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" Ah, I see ~ As the patch says: "Unlike MADV_FREE mappings, VM_DROPPABLE ones can be dropped even if they've been dirtied". Thanks a lot for the lesson! Lance > > > > > Thanks, > > Lance > > > > > > > > > > > > > > > > > > > Thanks, > > > > Lance > > > > > > > > > > > > > > orig_pmd =3D pmdp_huge_clear_flush(vma, addr, pmdp); > > > > > > > > > > @@ -3098,8 +3102,15 @@ static bool __discard_anon_folio_pmd_locke= d(struct vm_area_struct *vma, > > > > > * > > > > > * The only folio refs must be one from isolation plus th= e 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_DROPP= ABLE)) { > > > > > + 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= _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_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(foli= o)) > > > > > - return __discard_anon_folio_pmd_locked(vma, addr,= pmdp, folio); > > > > > - > > > > > - return false; > > > > > + return __discard_anon_folio_pmd_locked(vma, addr, pmdp, f= olio); > > > > > } > > > > > > > > > > static void remap_page(struct folio *folio, unsigned long nr, in= t 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 = *folio, struct vm_area_struct *vma, > > > > > } > > > > > > > > > > if (!pvmw.pte) { > > > > > - if (unmap_huge_pmd_locked(vma, pvmw.addre= ss, pvmw.pmd, > > > > > - folio)) > > > > > - goto walk_done; > > > > > + if (folio_test_anon(folio) && !folio_test= _swapbacked(folio)) { > > > > > + if (unmap_huge_pmd_locked(vma, pv= mw.address, pvmw.pmd, folio)) > > > > > + goto walk_done; > > > > > + /* > > > > > + * unmap_huge_pmd_locked has eith= er already marked > > > > > + * the folio as swap-backed or de= cided to retain it > > > > > + * due to GUP or speculative refe= rences. > > > > > + */ > > > > > + goto walk_abort; > > > > > + } > > > > > > > > > > if (flags & TTU_SPLIT_HUGE_PMD) { > > > > > /* > > > > > -- > > > > > 2.39.3 (Apple Git-146) > > > > > > > Thanks > Barry