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 1948AE77188 for ; Tue, 14 Jan 2025 03:40:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 658266B008C; Mon, 13 Jan 2025 22:40:14 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 608426B0092; Mon, 13 Jan 2025 22:40:14 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4F7376B0093; Mon, 13 Jan 2025 22:40:14 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 32B1E6B008C for ; Mon, 13 Jan 2025 22:40:14 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id DF0BB1205DC for ; Tue, 14 Jan 2025 03:40:13 +0000 (UTC) X-FDA: 83004654306.22.5B817FA Received: from out30-97.freemail.mail.aliyun.com (out30-97.freemail.mail.aliyun.com [115.124.30.97]) by imf09.hostedemail.com (Postfix) with ESMTP id 27CA8140005 for ; Tue, 14 Jan 2025 03:40:09 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=PgjjNH7N; spf=pass (imf09.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.97 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com; dmarc=pass (policy=none) header.from=linux.alibaba.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736826012; 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=ED3v0KZ62Otclzj/DtPlHw7G1s3gURYmmZedtowKKdY=; b=8nLfre/uF7pmYlaAZwQG1LbZhY2zd5c0GJQkmsZ7xDuLjQFXHUeA72cuGY5FgDPqLLvhou 6+zJ3d+xgJAg19LD0m8doSk6p+TKxadaZYAJDskdnkFlDEvMhcoeU5reQxArNttqxT/AMq CdbfihshK5z2Op4FyDNjBr+pFSO0YSM= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=PgjjNH7N; spf=pass (imf09.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.97 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com; dmarc=pass (policy=none) header.from=linux.alibaba.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736826012; a=rsa-sha256; cv=none; b=SUAg7hwlhLTDOMYdtHJsHaARSULpXYlVmm4WL8DvBI1dzFGBjP8C956dPvd3G+d/OTGviM v45nXHKHKzRFmJo868sCjkxmlEpeWAAAv8SsRgb88lruaeeujPhMWzckTT3wcryrbZPrSS s0ixDcFFsH8w8j9VQAXyQChhrILoKGc= DKIM-Signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1736826007; h=Message-ID:Date:MIME-Version:Subject:To:From:Content-Type; bh=ED3v0KZ62Otclzj/DtPlHw7G1s3gURYmmZedtowKKdY=; b=PgjjNH7N4PYC1crtK1RlhVquOiTALTD1eq0v0QqLL+XyNH+6y04YBdXCziYWi5XsWwNN7kSDJGYy7WRngsm9miPR94QlUApChV+RanBUPn2e8BHAK2uKmBT8a/mqGuSd1ch0uOeR6aYKHiEGO6/fQbgywQe/GrKJMqkZHxbJd9Q= Received: from 30.74.144.113(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0WNdJbji_1736826004 cluster:ay36) by smtp.aliyun-inc.com; Tue, 14 Jan 2025 11:40:05 +0800 Message-ID: Date: Tue, 14 Jan 2025 11:40:04 +0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird 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>, akpm@linux-foundation.org, linux-mm@kvack.org Cc: chrisl@kernel.org, david@redhat.com, ioworker0@gmail.com, kasong@tencent.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, ryan.roberts@arm.com, v-songbaohua@oppo.com, x86@kernel.org, linux-riscv@lists.infradead.org, ying.huang@intel.com, zhengtangquan@oppo.com, lorenzo.stoakes@oracle.com References: <20250113033901.68951-1-21cnbao@gmail.com> <20250113033901.68951-5-21cnbao@gmail.com> From: Baolin Wang In-Reply-To: <20250113033901.68951-5-21cnbao@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam05 X-Stat-Signature: aosw8stdgqeiwuybbb14zmbbrrxptrbg X-Rspamd-Queue-Id: 27CA8140005 X-Rspam-User: X-HE-Tag: 1736826009-475329 X-HE-Meta: U2FsdGVkX18t4HCXZSqspCeRvKHL+1jCHn0QsClmQ+tctZgQzq7tnywcoxjFCOzczhDVVCeRMg6JY+tfRkHpTt0LqL1gfoTTj0+s/T++itCjkOsPcWvmpSp2GPxQL1ZYaiWTcNEEYJOLEgv+02z7jzODKPP1hM6bw07SK6lIk83IeCxXiCbeZLui5LpCfGSx/0pjIoK+dOdi2EYY8jEskibSt6VL9QTB+SrqWxeo9jzcQeKhQYkVu0HZ9c3oSAMLp0uM8YVLh71st7SQXmPmGEcAwZO8yNtK2NBUHX1KFQ/8w5yQDldDGaUbpCVEctgR6bVYLcK0zooytyFcsgycucIOoOsq9fn37f298058T8ae1+T6NndmWxE5C8y0SIaSp6F3ImUqlRbb/BqjoztSj5paGl+tCLjYHPNMq+L+ecW1sk5W0YqXvqGwlpy3qkEZF6t/PuHGTo/gBbCA5dcG9E6vR63/97mjF+gatCRGWvFdDz4pXFzjJ556D5VSXur7C7bANOX/W0SzjoiwvxrREssoP32QmdbVleACtToY/HJushW5FPewM5gMB3+UJ1Q+IISNsLJw9YsbNBkEreG9pa1obTGeIsmbFE4L3Vs0rAmoST3GagYfcbFA5yKjcCbpH+vr3uozwKdT2ororlGF+wIDM3Wupz9LfPRVeUzJytt6f3oK6YbqPKKZihtOpmTlaOAkyYowR84OYo9wK+0Ig2RabK+gv/5Xuh3pEZ5v5kUhrn2WkMx0yzKq73YWgIhXLm/trwbkS3eIUkv+HHGzEHCF+PIX9AoWfnauIkhwyL/JqkQTENBE1IMzWchxN1CnuNsPlg5j1wCNjTZVV7NGr7Z141R5Nf0U4Rn9k/8UrBpibHu3wN/y1+ZXstfB5YMDo+xK/TP+xPLHcMVarRQQ49s4n9oirdjemB2JxViBD3RTbmqQiSVoEVeHN/H3udJvbzW7bA4fChLpJGNpr+4 ii0jwZf8 gfOmAeRiCJ2WVFqKGRJx9dgVgdf1PzXYDkkAMBOHUF6AUWf0dD+1nTeHY7wgOmuU/fd0w7kravdjDGknAqBra1JirIyyyHIr8+IAM/POb45RBQtSDbywULyqYhbq40GL5OnpbGPBjXTR5wLhwzdbjUFKJTMFDGzvDuXEHTSXU0R7yzHjtRx6Xtby8smJC+ytc/kjSCz7oT4dqjUgMBISpONhKdFj95EpG60ZjFilQoroTt/AUCVjFHLVMxG1XkDcnwxJ+M3Nnuxpje8gBR9RlB4lpo8l3tTY+HPT/H8MsFElCY1Du4lJB2J4KQRRTNhwXJN2bNlUaQnQVkRugEHKWaw24Rb2a4b9Lj9VWqDsL9+PxVI1sUlC1lsoEhlU9Dvkk3gVTuRzPoYdmPWQ= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, 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 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; > 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) { > /*