From patchwork Mon Jan 13 03:39:00 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: 13936680 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 8A5E7E7719E for ; Mon, 13 Jan 2025 03:39:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1D6516B008C; Sun, 12 Jan 2025 22:39:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 160576B0092; Sun, 12 Jan 2025 22:39:49 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F1B646B0093; Sun, 12 Jan 2025 22:39:48 -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 CA3266B008C for ; Sun, 12 Jan 2025 22:39:48 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 7F8161A1A83 for ; Mon, 13 Jan 2025 03:39:48 +0000 (UTC) X-FDA: 83001024456.13.E46F148 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) by imf23.hostedemail.com (Postfix) with ESMTP id 95192140009 for ; Mon, 13 Jan 2025 03:39:46 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Gymt825r; spf=pass (imf23.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.214.173 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=1736739586; 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=7ePF7LRaXT6WJxMxcE/kQzKdknlZvi8u0Wr7nYwGrGY=; b=D0H0IU/YpmYdJfKHPN76foqJFHLmN+YBl0J0hCrjILc5aOjekQn6V6kIBv5Hd56dA0wOxY 0RCAM8bxFqwKCa7EqNeyvDVRAarfjXuQllubQOnvLLglSkEfHGiPV0ddvoYppbrQ7Ae3sY Dl0PcC/3iW9mY+sliAb3Y+L9VJZqSQo= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Gymt825r; spf=pass (imf23.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.214.173 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=1736739586; a=rsa-sha256; cv=none; b=Vvy66ftWp/Xd3+uNaIrrmXGN0x0SmdhxonNwBOX2XN93omOLQZ9Z0gPQ7QXEQz6Xk9H3hT SHlCt3SQg1xmayprtpM9h5BSlCKB5qcCxOfYGeQA8G6F28DyXatJWghmdUDnUd5ufNFZZ3 1EdOL8g8i4CWF0dPlPTJAvrIQA6ydps= Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-2166022c5caso57669175ad.2 for ; Sun, 12 Jan 2025 19:39:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736739585; x=1737344385; 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=7ePF7LRaXT6WJxMxcE/kQzKdknlZvi8u0Wr7nYwGrGY=; b=Gymt825r7bA1WX6Qb48nToTN4El/vwq/2WwkGxSj/T1W7a1yDhyO4dg9g2mA3F+Q6U tbsjKMkhEvZEtXlzV+h39/hshbBlSjm3VSlq+nMS+SsoPx8xL5gQ5OkjM91gOH6m/s2p VIdrGdGHQXJckgHZVHDLjY1dX8pQa6WXSvNta1/r9tBclS7zyQd9OdmH+QJkMI8NR2gz do36YnMXbmKs6M1/w53mRFaDtTJWcMrnWsFa2hc325sysy8g90swmpFJybyzRnHYaKe+ trTbe9AnaYeTWFk7RCPH1bsMoskhxEKqwZQSxj2dsBqiyKkvk8nwJnD3JLMknLyjDpzU rj8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736739585; x=1737344385; 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=7ePF7LRaXT6WJxMxcE/kQzKdknlZvi8u0Wr7nYwGrGY=; b=p12U9u8vVt3M015pu56Z9uVJJknWxASQpUx1NLrKWgdqU2YuQG0tBrAwoBumF0ulp5 23/r/YPVqguxj1M+kCM/8sIdZkG3endOzWwOSXBiGr9zF/qLJTl0cpGO7Hhbjtq+DVLb G8RfL1s7SU5mhDUUnFVMLZHfwrLMyp2u6VO6Dxm7TSIrofBkmSVz7MBsJdHfKuDILH7h /1b5LlhVRJDO+n3IIj8zZHZyZnmG4Bse5WAuriQ586ifF+QxDU7+YT6RHG7A2NaQYmju 6/Y24p7Sw7Rimf2Qg5PO781H/OxEp11MQo2fqqZ7ZDPhm9QX3ivUMDx7DXjmODTSzc8R C60Q== X-Forwarded-Encrypted: i=1; AJvYcCUlm/4QM4HiDEkFYPWQydspDnsGeAHWScQjKo3sv4tklmrJ0z0sZRF1l5SVeH76ivki7iCfEBSWqg==@kvack.org X-Gm-Message-State: AOJu0Yzv7UgI84r1jezSGSWcrzCw/YzHHb7FjU7zmNCzSngIBgb9c4Vr 3r542teemBGnJ67ejOujnN1QXdcXRMz1jM70v9O9np1tEPcFgGXi X-Gm-Gg: ASbGncsPZx4VjiUvZo0FNzlfBYTbMnyxA1Wh0Y2jFToEx7AQXui6ttl5/7FSDCGWcIs NMY/h24BbY0p+pt4/3+LuypD7hn4ldvg/TxMUwW0+yKuH4zD9ZD0B6zQdDv2Y5H2SiEtWV443h2 4MTfaxb4LIF2lSZfMx5k8BiPoxXU2Ssy/VHl2HDswJv1PeJLzeX3BMhoOtYzX0Ag/Pg040tpUuW dqq/hLUAR99CnpIOtLszJAto/dUq6Ohu6HS55pqGXB6MJy+VwUpNgqxmV63DPzpNcAlgH76vyOw Htdwl6jU X-Google-Smtp-Source: AGHT+IEJyXTMuDf5NuY3j1SIPBbXyZqFASFj5tk1fMcghhN0EpO5xryOPxQNqGVnzZ7Hib6ZrPMFDA== X-Received: by 2002:a17:903:3385:b0:216:1cf8:8b8 with SMTP id d9443c01a7336-21a83f69643mr199408035ad.27.1736739585271; Sun, 12 Jan 2025 19:39:45 -0800 (PST) Received: from Barrys-MBP.hub ([2407:7000:af65:8200:39b5:3f0b:acf3:9158]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f25aabfsm44368405ad.246.2025.01.12.19.39.39 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 12 Jan 2025 19:39:44 -0800 (PST) From: Barry Song <21cnbao@gmail.com> To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: 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, 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 Subject: [PATCH v2 3/4] mm: Support batched unmap for lazyfree large folios during reclamation Date: Mon, 13 Jan 2025 16:39:00 +1300 Message-Id: <20250113033901.68951-4-21cnbao@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20250113033901.68951-1-21cnbao@gmail.com> References: <20250113033901.68951-1-21cnbao@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 95192140009 X-Rspam-User: X-Stat-Signature: egbrkpg1e7315zmnxmhsodrd9t5id8qg X-HE-Tag: 1736739586-985713 X-HE-Meta: U2FsdGVkX1+xhREVtnn63bAgwN9ujMFu4mK2Q1JVwiSXzbNzeCHtOKVdVxC7pNXF+Lhe5ilEERhW/X24sC0Z2KkifS4JIL3c2DWoVzQIX/pYUU8ZYNZqhKrtmsepnA1nvBlj4VmEr/u0bSPPl16LW+dStD5eznH33kxaaFg6XIgxaEjlmjEoBXFybc/TdLTFYpbg5ut4/N6wEsrjrxJvKAHvPu15hD5axXwjwvn1VPdoninmjldMkYvB+6YV229K1nXi9YKXBBSRtkRBdat2VxKcNEKdn4XhA3muOGPpaSookAOfFyhdS56K91JzB957v5qt9PY1mzmJb45o1fiIwU/q6g9jXb0/AFQ+qscacUfqwwj2FzI3kDGcCD9F78f0ZEdoPXfIH/dNJ1imYI+iYxAeFz/HHRU+5mVz0CHyemrtTpLI1/jo7+Q2Jrv8Yha8Wa5prJX3HF4RsGO9P2k3D2xOnnb4dz8sMH7xeH1lL5+16sMTYEEsrOIwS+uFhJ9KvC4gvGnPkYLc1YpG+WxeY46QBJ7C05m8QJtFLmtngJrTKYf2bfb5GSokGoAeqb0XdekXy9qRzJujcK2e5yO/HtDpKxmqvfpH07fZtEPfGvU7q5zK0+ZX396tl+cE3lo7KCCtNNTmtTH9ShH/m4nqoT/0dZDw9Up7l4uKodpowEn9qb+0YsqS8RKCZ3OdSyuEOnN+0oRTaJfBUI4Ai+Zr48CVzge0AJ6RNG3SPwjPuSpOu31ynmUFBJ4YdeGmRwdhchjVgLorPLQl+EBEKlJ2hNjEzbUbgd7D1e2tO7kFQQwejXRJm7r7PGIlcaN7BPQSKL5qothPfL1II316KmBHeKGmtczO51kJfiqGFxxwioPTfFGFoobS+e0o/Mu0oI6iPxKaoA0JK9TxamIQDqy7coDYLmlF6ou+0dmpLro7PNor7xgERj8Ln3LPShMlUMn77X8aAHDiFYqxMJT96YP TraVOsKk aXMnwLrHNpNjh1fRQgh/QONKFhL1/zo0MEln2I2rHaLmoVQcWIXVZSdF8Gcu1rjHCl8WAdG6JODFCOFWIFDFQ4OR3ahpiOYhhosvMORomJsv/N2euZRt6t4btx9FgBGhD8WvaINQ5N/kf4SUjmAudkMSfmVv/N1Rw1F+NiadPLpFuiXicddgJCFbbwZ+0ipnWTIqqBNll0rpZLwfI36vbqFwPRPzwPNl9rEMZhhBXAnSxOIyci1hVoX7bV+jBxmJ3Bt0r+kCi2ccYbVgsv91iRhL5VOyljeFSHVnTFSj5/+j1q6tulRrXHq8NZpvo18eTx5MAECu80F36me+QeD6W/+Hxp3cm2H8vGdCp49kaw/hnHRrPya6d+9oik/4WY0KWCfGYAvseA0hSLVOlw6SR9Aa8AKDbUJCu2PKmXv36DH6kmJ4= 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 | 46 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/mm/rmap.c b/mm/rmap.c index 365112af5291..3ef659310797 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,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) && !(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, 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 +1904,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 +1917,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 +1972,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;