From patchwork Mon Jan 6 03:17:11 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: 13926917 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 2537BE77198 for ; Mon, 6 Jan 2025 03:17:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A0BBD6B0098; Sun, 5 Jan 2025 22:17:51 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9BB226B0099; Sun, 5 Jan 2025 22:17:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 835596B009A; Sun, 5 Jan 2025 22:17:51 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 626C06B0098 for ; Sun, 5 Jan 2025 22:17:51 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id DCF3AB0313 for ; Mon, 6 Jan 2025 03:17:50 +0000 (UTC) X-FDA: 82975567500.04.36DEBEF Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) by imf24.hostedemail.com (Postfix) with ESMTP id E4CDF180003 for ; Mon, 6 Jan 2025 03:17:48 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Pbsbzhjv; spf=pass (imf24.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.216.52 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=1736133469; 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=n0uoTlJkT3xJASppF2RyQQeqHMmR9kwA4pIBCxskX/8=; b=PdB85/SkogMHpGcepsz/ZBqI31DpGE43TJ1c2JO1yABhYEbirX4NLtYLE4RI6uYZ22X3lG 933kXrju7o5lbMqTi1OUPYjpRmBqmBBm24KFilFFZBNw+TelOs8vwT/SefrkRPs7YjR+ny JagaQF+GgJlQCAmLG5XycDYZDxvoU5I= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736133469; a=rsa-sha256; cv=none; b=4lUH18NZO74N9dgbjBsuWErnes1UvV/bUW0nyvpEXH/B8reH8OKfKCxx2WBlxszHaWbUdy GwHfsyN6Fnn/enKTIIeJ/36vxGAg3G6TN8ulsCYORogiY4lxEEJMHkwEdfF2+ZJ5lYES16 lZHmzzbLMDK60nPV0YhIXgks9BBj9CQ= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Pbsbzhjv; spf=pass (imf24.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.216.52 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-2efb17478adso19841248a91.1 for ; Sun, 05 Jan 2025 19:17:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736133467; x=1736738267; 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=n0uoTlJkT3xJASppF2RyQQeqHMmR9kwA4pIBCxskX/8=; b=PbsbzhjvO0FE5+cSEEjCp6MVYukTQd00x5q1bVycwG/LdLgZRzMpgFKF8RjNF7JH/i EzKrEyoRjaOqPoiB88G/plhJ7v8zfGrvMlINpO8jLALDN7syl9xpQ5hrrmQgrVpnyfV8 k2VkjHx/2brTrp2mihJy1Ddkg6vFYI7ZmmsDsiu9l72u2R/bjs+8UtIlrp+decCiOOFe nL27FDyyJ5jlhVtWAPKV7dKO+ssw8yxGuMffgiOZuEr6SJQSe1uyeoS1yJPmU5Q8A22S aI3RfjaM+tZG0m4ui9+HsxPptaPcbN4fPUjhlzmPiTGDefP/oV3YOt6q72kaSC9Km3y3 jSsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736133467; x=1736738267; 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=n0uoTlJkT3xJASppF2RyQQeqHMmR9kwA4pIBCxskX/8=; b=VoWvGH4bTJTBH+2p9sc6yTHwl2mahEGUk+yjz5pr8G5XXdwMRF+ypICcCDLc6q14ly +BqB7dtGKqz5Vg6Fm8WqZv8D8gjF/wdb6Nu0T1vrKbcGPyf1rMdY6ytydoXXlJS2v94O DCJONCtzoNSeBFdp4dcqS/+XFp8I9Bu6N42ToIiBdD3zCi9gVZepVVewFxQQFRE1i8iC iftAbTq80GzbKV0PpzAkUKPfvg3wcMwcYm3AGeGhLt5B32JFATtSL05DX1xXeAK7pY49 7rcO32R6GkUugV6OcszVKy+B0sHnJG/RN6E2zzZyKYObWiDa055YyRB+6Noih4Mu4tVh w9Ww== X-Forwarded-Encrypted: i=1; AJvYcCX87dZsEJgZ75XqINXjTy2rGhn+PPmnkbFrKBdhN6XJA1+aLK9DzYfHwwePd+4k6UQcVkQGVDqxUw==@kvack.org X-Gm-Message-State: AOJu0YwWfusFJiC81a9tPpTCw/y7aAnlzbVR1g0QBDrq/UCGuswA41E3 9n4pcn0JsV3mY9/9Bjqhiy6I9VagCUbP+aORk9NEVg2SwUrBL2Kd X-Gm-Gg: ASbGncsg7c9EMQPiGoKZi31XbRdLuT6ooe2oFn/vngXaXp1z/NbeODUMhBhxDXw+0Xd 85coWocttpIdNbK3enh2Dr71H8jQDtSEwGS+LQrSTEsl4W8Iv9JMpFpmH0Tk8fgmX3J305kMtTq K/IWDfdPZ9b82SReCltJEDyzAaZXQ6vmMYYqD0IhjqysPkx5li07hHHXCjHihpUvaVoCjV2E0Fe TNMAprrtM9NXgNEdfmPUghYlljQluqaZI82TzRG2HbMGlw41gsp+f7jCj4fEJEi3+u+TR6vFoZ4 8SkibML7 X-Google-Smtp-Source: AGHT+IHWP7zdolqMJNXBm1PXeI0GcejV+nCAy9WuJHL0SUoSVOj1myQWg2/Wp3YfIF+X1TNQvseTgA== X-Received: by 2002:a17:90b:540f:b0:2ee:6db1:21d3 with SMTP id 98e67ed59e1d1-2f452ec922bmr79065291a91.25.1736133467432; Sun, 05 Jan 2025 19:17:47 -0800 (PST) Received: from Barrys-MBP.hub ([2407:7000:af65:8200:a54c:5ad3:ad27:edb7]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2f2ed62cde6sm38471399a91.13.2025.01.05.19.17.42 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 05 Jan 2025 19:17:47 -0800 (PST) From: Barry Song <21cnbao@gmail.com> To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: linux-arm-kernel@lists.infradead.org, x86@kernel.org, linux-kernel@vger.kernel.org, ioworker0@gmail.com, david@redhat.com, ryan.roberts@arm.com, zhengtangquan@oppo.com, ying.huang@intel.com, kasong@tencent.com, chrisl@kernel.org, baolin.wang@linux.alibaba.com, Barry Song Subject: [PATCH 3/3] mm: Support batched unmap for lazyfree large folios during reclamation Date: Mon, 6 Jan 2025 16:17:11 +1300 Message-Id: <20250106031711.82855-4-21cnbao@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20250106031711.82855-1-21cnbao@gmail.com> References: <20250106031711.82855-1-21cnbao@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: E4CDF180003 X-Rspam-User: X-Rspamd-Server: rspam07 X-Stat-Signature: he81y9nregr78xfwrcajxsgds7qb3jym X-HE-Tag: 1736133468-972778 X-HE-Meta: U2FsdGVkX19ywWE3Q63B4LAUa+G+X8UhF8E6EU0rT/qxcDq+bhgdU5V2uz7iBJDVNgx5xxtXZkNrOhTRGU0CXHhSJyf8KoKOIzng38KNzR3hbnrpprM9XpVlkKNDvPAXwFqpIOI6kowSetsrpKP/kCT48euhScjAqqk60YE8fZblpeQIrMbZTkwAOEtV48VHRQskK2WTWeg37F8Ggg71rHNoZnVaof8ZYNJKzv64QMEWeMpp60bs6DIh47UKbvwqGwoKT7ImPsyBY9NfmbxGmhBSKqDtDeWbn0e0ucLqj6sN7F2TzYwFzDjFBJ9GUOK8SYHxefJjh2WMLj9jLZfTlQwFDRovmZnWU4QoT/Ma1eto1Vris8FfNGp0tXY5Xsh9pHVOopgTCbSTVwFMEHYZJFXVns8M8IX56rorBSpc2uoW9+BkVMHm0kXNvn4bwO066O2IQmyMpXW4mmGWVPyCkkeS8dLhbF+7thEfakt+33qqXb2mnkU7l7INuoG9Kksas8SuMLfp8PgsCJamtiyWZZHxkK9wJ1KblOy5Z7ST01Y6bqzzHiVj/40VqE2dMa9jl9lSyVpBECqtJpBXc4FxY55dXTVgj9CGKaDGjh5XIvlvtYctf+f2IpDRTvSpybbdpsVL1UD+9hrJs7fuyJxYzEvbk1HtAWGXNXSRJ+RKKAY8nqoFBKNdPG2xvAlkg/Mz8tltmKQ/jhulqUXNZA9w7CAznnSy6eLZkOiAccr+aLIGZan4qkFz3qjBWhC/+Op5d5aa17KYXhgvLehGsq4me6zEzu/sY/KcRhGXI6c8lr03Hmaf5WeKc2C03ocfa8HXCiXu4YJFDr1jDf9ZWLrBkddd7C1/NThNTl7BbWoJhYKlGe9L/bE3FnJpjkmMekeOjO9zQYjr7pBdKodh/x3kKB61CCGfwWAnIlFvnkVX14gbXQzzK5w2OUD8JSRjKtIDLYgnDQbc3pa9CqPY4GU ns6zAmTr Q4gabKFvj7y06LYEIdsodtx+Y2LVhGxo4sxTEshyJm1z3EPfaap6t+dJGtpI5s9ZEutJcHjB5cCC+BL2DDjD7SpbQVznX1u0Rw7Ui5RWQ6OfqjgYh9BClDEVhvAQjoPoKXyWVPIR8+VNm11+BuZOcA1rHSBeysNO5Ed7NKP37rS5M3oydHmoy7liTfKhUNXysHRkqeME8sjvW58ksh8Igtt1/Pq7Xr/M8UL7f528VCGQWIGO2hX6ZhhIXi6PHKXmfIX5BCAaSRRLhErqO1z4qD5Y72KdB2hzJ30c0CHzVZEKMVYEr7ePwGw4ycsf92FJswMyEFSmiT+kkCSorDqAK7ZpADigMu1iMkM/pY66xf6mlWrgDt50mStXSpZ33dCHlwXvkwmOh8Ec4RtDe3ImaZY8MZ1SlC70b9FE/CGP/z3TvHUI= 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 | 48 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/mm/rmap.c b/mm/rmap.c index 365112af5291..9424b96f8482 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1642,6 +1642,27 @@ 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 (pte_none(pte)) + return false; + if (!pte_present(pte)) + return false; + if (!folio_test_anon(folio)) + return false; + if (folio_test_swapbacked(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 +1676,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 +1802,15 @@ 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) && + 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, 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 +1906,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 +1919,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 +1974,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;