From patchwork Wed Jan 15 03:38:07 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Barry Song <21cnbao@gmail.com> X-Patchwork-Id: 13939798 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 E6D79C02183 for ; Wed, 15 Jan 2025 03:38:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 730DB280005; Tue, 14 Jan 2025 22:38:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6E14C280001; Tue, 14 Jan 2025 22:38:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 581E0280005; Tue, 14 Jan 2025 22:38:55 -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 37048280001 for ; Tue, 14 Jan 2025 22:38:55 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id D9860C0FD3 for ; Wed, 15 Jan 2025 03:38:54 +0000 (UTC) X-FDA: 83008279788.25.2295434 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) by imf18.hostedemail.com (Postfix) with ESMTP id D2D4A1C0005 for ; Wed, 15 Jan 2025 03:38:52 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=bUd1L4jw; spf=pass (imf18.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.214.180 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=1736912332; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=bGaU/+TE2oGF3eimQ2KnLuagLzu4AwU2TBvptcVR7ws=; b=H9TSazV6C/98B3tPowzcbBTLo+dYimz1emARRFJ9XNskmK6B8GEen7XQEQ8RLt6lzVZswy f4lwxIAoyXvWs2ETKmHaa4g1qQPEoRlCBuO5AFIwDdywOFZth9E1FoT9zQbll15Awefo90 OtgfSqKm7iwjV+TKOg44Uoj0EugtNCY= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=bUd1L4jw; spf=pass (imf18.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.214.180 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736912332; a=rsa-sha256; cv=none; b=2Bqfs2t/U7sDpmG4NsjUi0lFaVvOvSPr6+Pi6hLU8YSgDR3DxmByQk2BkV5dfIXeZBiXZF RGGtbagn9gCAbdqDjNSh7bgQSEOR5fBiVRSTK5L06oZFq1jZKpIBfc5YdocE7YTTgz24ih C18jDbj5SJpLstXQbMZYzO6gqwRZBsY= Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-21654fdd5daso106217435ad.1 for ; Tue, 14 Jan 2025 19:38:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736912331; x=1737517131; darn=kvack.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=bGaU/+TE2oGF3eimQ2KnLuagLzu4AwU2TBvptcVR7ws=; b=bUd1L4jwoWvE8B7fdAZPU6jbUWPc4qovHiy5eQp5sGgiVw1r9GwPmohmsWKUrgoyuG q0GY+CvS1xhC78BgRybs6P84X8KqxrB4iWiw6QeMLd45bIUlM9J8g8BCo1CLySRU3vlv hcnML6jZz0juoN0ZT5EuQ0gtowsOA66hxDIGgS4/fCAh2IIVjG1k92MvbSJ86CPTs2Se T8d6CPzV4n3oQh9O2CgeT7mUySTOAxOHEXcbC+eu+G2CNzaS8RgPJjkRzYByE1vX+qMq WnxfkcXHFQHVNFXdvR1e5LPHsgkxmLK9o9u0tdqVuhh+FHKpf0OO4Ln2rgvllwEA1b6F l03g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736912331; x=1737517131; 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=bGaU/+TE2oGF3eimQ2KnLuagLzu4AwU2TBvptcVR7ws=; b=CoGBqj+1xBgDO7GHhEskWY1RLPYAFDuOoqvJxol4IcTvVRKAINdCyUJ82JkN3mS0+y zJwO3JpgJE9Ka9WL2mxeWxoy2CXQZHsKocvjqXVSywqNikvkREqAjpgB6VH9LgfoT9wt sxU0+gv2FXHr2TeYQlOq67/40Kcne3noLdO3RwkCSyiF/JEjr9vPbWn4QUXcQSkSHAq+ Bbqd3WZUrhSgNZZ/98AvnM/x8Ah8/pSNrybOyCuP02UaszQKJ1s9A1AFK5Q31G+LycHG t1hVlDzcIP/WUd6ZdIrTNshClngNZD/y+3qeU8nI+svSuiXMbCcMx0yPceCe66Auzhl2 D4VA== X-Forwarded-Encrypted: i=1; AJvYcCVhbuhTmxBo8J9GhjxnblCoGTbBd/wBbTSSINZNgKqBmcRXlw04nMFhw3ICei/nbjbI6bEzzrHbWw==@kvack.org X-Gm-Message-State: AOJu0YxXRDSkRFkf9RazkxDRGZdLcq6cIDT5NpUohPwOfXYeex/WedWR WLPaxvoPp3d0cPo35by+hNfyPcoiSRSxREmY2MxARrpbeJnrbv/P X-Gm-Gg: ASbGncs1sK8cE/ApKqSpd3PwIwDJuwTpPGAQQipvf+P48A0rqpdXZtCejsxFeVKLoHo odJWXlKlnsToS6bIjONNO50uh+eynNgfga3DdSvEvhEFykwCw80oqSfKpUiBUTt3UPUotDFDUqP SN3PIgxiyr5VcCrScmiVH4Fq6XJzF4bubj1eJpHzxS8cTxleIM7D90MJP3zKLz4msLf5L4FjNIW +AnyYxaUkhoNZuKonUm/jEJe8LD1R1OuSalnKcdMt4j19/hi39IMoSdwWOuh9yVX5t/LCTB1H/k 6NiS/bJc X-Google-Smtp-Source: AGHT+IEqw72Wem1/NpwpTk58spR/v0PCzoGmmngV+d3HWDRVCCL9vc8Xbq25OJt+gLxiUfAdXr2j8w== X-Received: by 2002:a17:902:f644:b0:212:40e0:9562 with SMTP id d9443c01a7336-21a83f69651mr417875835ad.25.1736912331517; Tue, 14 Jan 2025 19:38:51 -0800 (PST) Received: from Barrys-MBP.hub ([2407:7000:af65:8200:e5d5:b870:ca9b:78f8]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f10dffbsm73368195ad.49.2025.01.14.19.38.45 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 14 Jan 2025 19:38:51 -0800 (PST) From: Barry Song <21cnbao@gmail.com> To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: 21cnbao@gmail.com, baolin.wang@linux.alibaba.com, chrisl@kernel.org, david@redhat.com, ioworker0@gmail.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 Subject: [PATCH v3 3/4] mm: Support batched unmap for lazyfree large folios during reclamation Date: Wed, 15 Jan 2025 16:38:07 +1300 Message-Id: <20250115033808.40641-4-21cnbao@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20250115033808.40641-1-21cnbao@gmail.com> References: <20250115033808.40641-1-21cnbao@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: D2D4A1C0005 X-Stat-Signature: yo5hibc7ds8burnara3mgu88o4c17o1i X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1736912332-477064 X-HE-Meta: U2FsdGVkX1+2DNxGKy4tj+gYzNvIBhKOUp8j3mDovUtsTwAKBwzJddcCgDWmF+1A/S+ELEzvjU8iZRuUpPiGwPjWmPshX9394bBsLCOYrYhmjf3Ra2c5U+xKVO+S2V6A5crCtAM3bN/NJes04GMaLPWX2SWSKjcDuDbjTh5fiXgWwbHM4qArJXbpxMVQZKziRvQYrk+8BktkmXsTX4XGsyfsnJkJZW4o/h3pwCJnV1MSwf1qLVVcBqf2IQISVyS/yuNJSWT/9bE/ZviRjKZdh8arCHsgQrb25KU974OBFebsaZ3l3oBLXGiyK+HllhsmEU25IGmCJoo2cK7B5y8StGfbvKAog3ddx2J1/A7/P1jxV9nBEZtiHfdB0jJr2wxFEujWdoDGh0b98uDJvW5SMIns/GnLwLLNcBhNJxh1M/wF5ceAsP8aBZJtc7mQouTRmNGXlrAF2iDhNhctdegrxTZON4woqhrIvFt9yNrlM1ltXTciNRhCnvj1y/5BVwxa7OZr94qJh5Y21Cf9zDzHa0VyEUCwwOLqeg2nzKWkbomL739ni0CPbt86UjB/9qcB1aqUhK56IdtVljsQIUAzohramS5TXl2Sntx7yLXSn0mla2Q1l4FB68ufc9rwNSlDi51+ZrdeirYmVhxq22QsD2QIQlqzPvGtfsYVI3FFyxOhV1WvaV/mqltazGUBVf1pTuWy2LsPOsIujLY8RKT19iqaXTqoLF18b+3Yz0kzzZ6vgRpLZFXvbBhdasvA7acl73IHb3HP75vJSvIJadSgEyd9ghMIPbQoil8klbZCHrFfqJcn6eQ79DMzvn7w1KJSPUVt07gCPCPHpbllkNL6laXX3kRoLX+AbvkgB+BAQVJL2p6rqbw0Lr4MEU/UMK8skwj0b+pxhfxb841Z72FHGUb4gDo9FYM5RBRS513g/BvogAR36Swy763aIcEeh8Sw+ytbXpOe7ReBH3/Dinl Z335zG4+ df7txNshGejkSWs30b5HfTiKCAdXhmk7q6H+6JjqQmL73vRGgm12UawC//8u4NkfcZs0HyRlvNTeZFOelXiMpXhftNWxoKJze6HULSv13rBRGrwWosf9K31GC8R70YJ/nBZkBibOBmqjXWOSMXoNn4MqMr+5kNxhBOPgGCyGsS/FK2Jyj48Gk8hWOowP4B18F7tirrOmci8WXUlXSMDRgRlO4zq96vcYsWHaEMfUoeb8AXrPnHCXVEN95SAvI1o0jM7N4/1ZOc4MEGpaOKMRmJPf+DHMzJTCxnN5c5pxOLgHFTRj9zFF4LC2QkV1IPOK4pOnFTPg6Ten9lzS4CNEox4QBryVm/B/BMX0dZLCiYEORbqp9DnXSaMZ7CosW0Cbu7DtRGu0ad7dssUoPFd1qWSyR6e7cteSFh6nXljf93CUcV2Q= 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: From: Barry Song Currently, the PTEs and rmap of a large folio are removed one at a time. This is not only slow but also causes the large folio to be unnecessarily added to deferred_split, which can lead to races between the deferred_split shrinker callback and memory reclamation. This patch releases all PTEs and rmap entries in a batch. Currently, it only handles lazyfree large folios. The below microbench tries to reclaim 128MB lazyfree large folios whose sizes are 64KiB: #include #include #include #include #define SIZE 128*1024*1024 // 128 MB unsigned long read_split_deferred() { FILE *file = fopen("/sys/kernel/mm/transparent_hugepage" "/hugepages-64kB/stats/split_deferred", "r"); if (!file) { perror("Error opening file"); return 0; } unsigned long value; if (fscanf(file, "%lu", &value) != 1) { perror("Error reading value"); fclose(file); return 0; } fclose(file); return value; } 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); clock_t start_time = clock(); unsigned long start_split = read_split_deferred(); madvise((void *)p, SIZE, MADV_PAGEOUT); clock_t end_time = clock(); unsigned long end_split = read_split_deferred(); double elapsed_time = (double)(end_time - start_time) / CLOCKS_PER_SEC; printf("Time taken by reclamation: %f seconds, split_deferred: %ld\n", elapsed_time, end_split - start_split); munmap((void *)p, SIZE); } return 0; } w/o patch: ~ # ./a.out Time taken by reclamation: 0.177418 seconds, split_deferred: 2048 Time taken by reclamation: 0.178348 seconds, split_deferred: 2048 Time taken by reclamation: 0.174525 seconds, split_deferred: 2048 Time taken by reclamation: 0.171620 seconds, split_deferred: 2048 Time taken by reclamation: 0.172241 seconds, split_deferred: 2048 Time taken by reclamation: 0.174003 seconds, split_deferred: 2048 Time taken by reclamation: 0.171058 seconds, split_deferred: 2048 Time taken by reclamation: 0.171993 seconds, split_deferred: 2048 Time taken by reclamation: 0.169829 seconds, split_deferred: 2048 Time taken by reclamation: 0.172895 seconds, split_deferred: 2048 Time taken by reclamation: 0.176063 seconds, split_deferred: 2048 Time taken by reclamation: 0.172568 seconds, split_deferred: 2048 Time taken by reclamation: 0.171185 seconds, split_deferred: 2048 Time taken by reclamation: 0.170632 seconds, split_deferred: 2048 Time taken by reclamation: 0.170208 seconds, split_deferred: 2048 Time taken by reclamation: 0.174192 seconds, split_deferred: 2048 ... w/ patch: ~ # ./a.out Time taken by reclamation: 0.074231 seconds, split_deferred: 0 Time taken by reclamation: 0.071026 seconds, split_deferred: 0 Time taken by reclamation: 0.072029 seconds, split_deferred: 0 Time taken by reclamation: 0.071873 seconds, split_deferred: 0 Time taken by reclamation: 0.073573 seconds, split_deferred: 0 Time taken by reclamation: 0.071906 seconds, split_deferred: 0 Time taken by reclamation: 0.073604 seconds, split_deferred: 0 Time taken by reclamation: 0.075903 seconds, split_deferred: 0 Time taken by reclamation: 0.073191 seconds, split_deferred: 0 Time taken by reclamation: 0.071228 seconds, split_deferred: 0 Time taken by reclamation: 0.071391 seconds, split_deferred: 0 Time taken by reclamation: 0.071468 seconds, split_deferred: 0 Time taken by reclamation: 0.071896 seconds, split_deferred: 0 Time taken by reclamation: 0.072508 seconds, split_deferred: 0 Time taken by reclamation: 0.071884 seconds, split_deferred: 0 Time taken by reclamation: 0.072433 seconds, split_deferred: 0 Time taken by reclamation: 0.071939 seconds, split_deferred: 0 ... Signed-off-by: Barry Song --- mm/rmap.c | 47 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/mm/rmap.c b/mm/rmap.c index abeb9fcec384..be1978d2712d 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1642,6 +1642,25 @@ void folio_remove_rmap_pmd(struct folio *folio, struct page *page, #endif } +/* We support batch unmapping of PTEs for lazyfree large folios */ +static inline bool can_batch_unmap_folio_ptes(unsigned long addr, + struct folio *folio, pte_t *ptep) +{ + const fpb_t fpb_flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY; + int max_nr = folio_nr_pages(folio); + pte_t pte = ptep_get(ptep); + + if (!folio_test_anon(folio) || folio_test_swapbacked(folio)) + return false; + if (pte_none(pte) || pte_unused(pte) || !pte_present(pte)) + return false; + if (pte_pfn(pte) != folio_pfn(folio)) + return false; + + return folio_pte_batch(folio, addr, ptep, pte, max_nr, fpb_flags, NULL, + NULL, NULL) == max_nr; +} + /* * @arg: enum ttu_flags will be passed to this argument */ @@ -1655,6 +1674,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, bool anon_exclusive, ret = true; struct mmu_notifier_range range; enum ttu_flags flags = (enum ttu_flags)(long)arg; + int nr_pages = 1; unsigned long pfn; unsigned long hsz = 0; @@ -1780,6 +1800,16 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, hugetlb_vma_unlock_write(vma); } pteval = huge_ptep_clear_flush(vma, address, pvmw.pte); + } else if (folio_test_large(folio) && !(flags & TTU_HWPOISON) && + can_batch_unmap_folio_ptes(address, folio, pvmw.pte)) { + nr_pages = folio_nr_pages(folio); + flush_cache_range(vma, range.start, range.end); + pteval = get_and_clear_full_ptes(mm, address, pvmw.pte, nr_pages, 0); + if (should_defer_flush(mm, flags)) + set_tlb_ubc_flush_pending(mm, pteval, address, + address + folio_size(folio)); + else + flush_tlb_range(vma, range.start, range.end); } else { flush_cache_page(vma, address, pfn); /* Nuke the page table entry. */ @@ -1875,7 +1905,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, * redirtied either using the page table or a previously * obtained GUP reference. */ - set_pte_at(mm, address, pvmw.pte, pteval); + set_ptes(mm, address, pvmw.pte, pteval, nr_pages); folio_set_swapbacked(folio); goto walk_abort; } else if (ref_count != 1 + map_count) { @@ -1888,10 +1918,10 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, * We'll come back here later and detect if the folio was * dirtied when the additional reference is gone. */ - set_pte_at(mm, address, pvmw.pte, pteval); + set_ptes(mm, address, pvmw.pte, pteval, nr_pages); goto walk_abort; } - dec_mm_counter(mm, MM_ANONPAGES); + add_mm_counter(mm, MM_ANONPAGES, -nr_pages); goto discard; } @@ -1943,13 +1973,18 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, dec_mm_counter(mm, mm_counter_file(folio)); } discard: - if (unlikely(folio_test_hugetlb(folio))) + if (unlikely(folio_test_hugetlb(folio))) { hugetlb_remove_rmap(folio); - else - folio_remove_rmap_pte(folio, subpage, vma); + } else { + folio_remove_rmap_ptes(folio, subpage, nr_pages, vma); + folio_ref_sub(folio, nr_pages - 1); + } if (vma->vm_flags & VM_LOCKED) mlock_drain_local(); folio_put(folio); + /* We have already batched the entire folio */ + if (nr_pages > 1) + goto walk_done; continue; walk_abort: ret = false;