From patchwork Mon Jan 13 03:39:01 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Barry Song <21cnbao@gmail.com> X-Patchwork-Id: 13936689 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id CD633E7719A for ; Mon, 13 Jan 2025 03:46:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: Content-Type:MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc: To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=t1XMRg54nQSp8/EH5otPlOs5Y//8NM069hYIc60Rxo8=; b=XJE7tk6h1fMUTXd1zJSjxG17EF I+wKsfVTzlYxtDJ1rFGnt3+wyTmBhjEsI80FUyDhNHpfD4Wz+DaOgO4HTOcpd3W2LfU4JLUEF0m0G PpcPmUsLBiGZorVXiqa0uLj90cXSwDX5tQrzddFA6V/Mjn5jQDc627yBWQnpTYRo2CHxn2RhiPcyb jEnWV7797jeeOiDKcFmvRGmOSmOWGVKS+L0Ty543NJWbahpfxEaoFY/cRmyeGQzesGWkoKYkGWfNt 2awr1TI8wSxdKVLokSX4LblCbK7O3BVEQASthqoAmBVzCbdOzirm8P10raGwgcxo8WtoYhda2N20h p97qIFxg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tXBOp-00000003wBX-30h7; Mon, 13 Jan 2025 03:45:59 +0000 Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tXBIv-00000003vFv-0ipe; Mon, 13 Jan 2025 03:39:54 +0000 Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-21669fd5c7cso66199665ad.3; Sun, 12 Jan 2025 19:39:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736739592; x=1737344392; darn=lists.infradead.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=t1XMRg54nQSp8/EH5otPlOs5Y//8NM069hYIc60Rxo8=; b=DPK5OnIvLXkdS5o3EP2JJk8J3jr5cgfl6b6bP5pL0jHmHK4fgcbfbKVyqWRFuVicgv fS71by+jEn73bAsIJPsPPeDyEwCKHBywsUcHDjhvkkGBv7Cyumeu30QbqDA+K3BE9ofM CiTmQRZe1wdkILrqxijFDJvCl0ljIhAk4dQKUhxwA/2Lwc4DWwZDkAeICKLE2mSDrLot hps9gN4aZZlTF4KwSV5pYJ4yaBgyIjxtdIici+VFby/Wlny+tTnGXokaAZPm7Q5QSZw9 hTfEzjpyGNbdvkCgm5BMF/fHQCfvQ4JQcVSmv/wDjT8APYAfO+esnflwJU6pf19b+Flc 7YVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736739592; x=1737344392; 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=t1XMRg54nQSp8/EH5otPlOs5Y//8NM069hYIc60Rxo8=; b=mSvYdtZOjv6i30aVpqMGh/h45R4xnYTieN2taMOhlOl4rWXAxl9ul70jPWryi07xZW /qQip0qpYPNYP58t4Bkc/L4TNv5l+KYmbTLYNIZQRpTqKvpYbnl0WhPCjjpssACSn2LT 73NzdFqGHkjnHSB5tW8BYx6KzalW7Oz1SZ//gNDl3ehJRzzhl1ZThyyrVQ38oNy80Srs QOQdh7wh03jPncojHrnZb4pWJoG40k5kMGGfz+AMC73//IJ9P4SYYGtEUzfCOq/WiQGr CziryRYChYi0r0ZBSU8uqkFMnwFCSTA/3WnxGv4oRMXRbBbtXhpvlR4kL7kHVkGIXeJg cd2g== X-Forwarded-Encrypted: i=1; AJvYcCVjphbZuLEsP5Sz/WMujIhpseahVj4FwtPlPhizWOZGnaeIULZz/zWViSlRXRzwGic4mDy/DBEM2WwbpNTboNiF@lists.infradead.org, AJvYcCWq4N+cFnPOeOiH4VCyzU0V9iQCPbz6VNWjP0ljC8YwHfHO+r48w2uX4OSOQ4ZlBkmRS86AVrTpjS8ZD3o=@lists.infradead.org X-Gm-Message-State: AOJu0Yx/vxI/TAXrwilnsmBb6EjRQznFidHpvGZAaZBw0uDCB8+XDgZ5 QBVO1gZ781QQdt49QEigERwZ+DvPLg/g+K1K28JeJCVHHll+sjCD X-Gm-Gg: ASbGncs6vkdk0M2LNYQWglTvS4YmRmeHgPgoq2qqqZhW4TXUBt5V0MUYuDnkC55MYxJ YiYUwrMY1I73+rYls3ho5IpMvE/3fhbmdl+IfUHmKUvIB0ZNuotNIbuHRsx5M+yxm9isS077lWN 4/hXwlQ9KXM0hK5O5HrqaWT3u4pIXpR0l9vOm5jJ6kpix6LzWf+OMFbhq0X/ojb7Rr3jyj8kJoG LiFlzFKWYbWVMBSB2a1dOlMT0r3aefrcEEX90OQmtn7/CC/QdbCvKpCr5zAtSmh2X943a5VMNLf 7TVMfZEl X-Google-Smtp-Source: AGHT+IFvTUatRCa49/9Qoa3okmsFGYsqNW658p8oE5OURLMqB1h00MlvT5OAmq5hWQRPMmGvVdLulw== X-Received: by 2002:a17:902:cec3:b0:216:6fb5:fd83 with SMTP id d9443c01a7336-21a83f69d84mr299035325ad.29.1736739592302; Sun, 12 Jan 2025 19:39:52 -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.46 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 12 Jan 2025 19:39:51 -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 4/4] mm: Avoid splitting pmd for lazyfree pmd-mapped THP in try_to_unmap Date: Mon, 13 Jan 2025 16:39:01 +1300 Message-Id: <20250113033901.68951-5-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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250112_193953_214905_A6D159BF X-CRM114-Status: GOOD ( 15.01 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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); + 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) { /*