From patchwork Wed May 1 04:26:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lance Yang X-Patchwork-Id: 13650206 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 79DBEC4345F for ; Wed, 1 May 2024 04:27:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 05EF36B0085; Wed, 1 May 2024 00:27:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F287A6B0087; Wed, 1 May 2024 00:27:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D79D86B0088; Wed, 1 May 2024 00:27:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id B814F6B0085 for ; Wed, 1 May 2024 00:27:18 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 25D3B1406B3 for ; Wed, 1 May 2024 04:27:18 +0000 (UTC) X-FDA: 82068542556.06.03A4048 Received: from mail-oi1-f180.google.com (mail-oi1-f180.google.com [209.85.167.180]) by imf11.hostedemail.com (Postfix) with ESMTP id 7D2F840006 for ; Wed, 1 May 2024 04:27:16 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=UuDOiPQW; spf=pass (imf11.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.167.180 as permitted sender) smtp.mailfrom=ioworker0@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=1714537636; 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=qxwHmPfQRT1njG/0ZnhsAAHKQR4nWB4DSExpUtfTagU=; b=3vGyKgVS5V5AnNw/gaL38fqcvHBDGvvESVvEAk43/Dc5O1a4/Z6VkT88GLdO66Nk2vYbsC JG4BOHjzsq6XhqKp3yQCesIMKE1LYqe6reMRJ//2m44J+iqz5Z8hIZEDxd824joY/uG2ZV IV2BDeC97YSnzftgOKq5YWRqyk+BQs0= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=UuDOiPQW; spf=pass (imf11.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.167.180 as permitted sender) smtp.mailfrom=ioworker0@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1714537636; a=rsa-sha256; cv=none; b=IuPDBGvnD8p+1/k5W5yhfU+sea/ZMXsSc7kdWdQrmM0LiYSDWzUK3fyuddTHCeuDFC4q/q PFtBVVG1U/fYdG1X73DYu8miDxe4bRWOuB6mtaY3UGikwd3esqM3iomCT4ytP2PTWmKIWR vBRSh7Yx/F8M5h0J+1dECoKdW+2oi+4= Received: by mail-oi1-f180.google.com with SMTP id 5614622812f47-3c70999ff96so3357603b6e.2 for ; Tue, 30 Apr 2024 21:27:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714537635; x=1715142435; 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=qxwHmPfQRT1njG/0ZnhsAAHKQR4nWB4DSExpUtfTagU=; b=UuDOiPQWgwNl57LWtzzM5uqVWhySwbevLElw66l0ROfvvVdxwyNgGjooVJJf54Pznr tEiPqcxGkj1VI1y4odah9B7LvI0k0Y4wDBqLoyO8DLDu6hBjrrWIMS/TpYwoRBx//zsS 2qNjWWWruE6POs04fxQtgiAdjranOgZOAxuBLCZlz+4FOi6E6LiUXwED9TS7wGdmq32H CMoRyqqQC5GRzcD9vtCF9f3DnoFPAExT+nBWURwdBKajW+vs/kfVnTKuNF1hDdLR/RFL UnenfEzuRqyQaJ/+uK5P2LvdzbeimPTH/vh126Q0TAUv0rCp4nd7x10mn8QN3T8URb6W rLtg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714537635; x=1715142435; 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=qxwHmPfQRT1njG/0ZnhsAAHKQR4nWB4DSExpUtfTagU=; b=vLRLKWYOeif3SeAnuaZ2sgSWetQVTVhyHw2qyUV1swO+CM+JiZVcH3jAsIUf7DSv// Ko21yGJYCryY43TX84km1B0BkSSob3GHKzkwMhbe4T+wIJt5i7NUdmcB2uEpDweJXMfL 5lKUC7pFGxppLzhG0OqxxR/R8vOCRafTO/rxB/+08W2B1DAqJKoMfGx5VgSjWRx+qZJZ BIYbFlSlVy21s2OUNKy9lZ/AiONQuUEozqd7GQBxa2ufrg5q96qM3wP1dJCRA77kMu21 NLSTNUiV2Qnr9PR0tgmnXtrYs1hlJDG2liKx+qQLa0q5PMbQa7kjKkOpdoMOlTGDKSql 50Tw== X-Forwarded-Encrypted: i=1; AJvYcCXjRYlR9eLqz8VsLv5Bd4pfqtu00NlwWOoRqIx9/s7fj8gU5JrBkM8ZeWUprxRVUWgrNu8HRFT736cTbT4lvWohaEw= X-Gm-Message-State: AOJu0Yy1aSFE6B3jq+kLEdv4jVcVH2hoRkuSlwxzXCEQJLcag+hTo5Bu 6Lfsnjhqgx9qswhnul/EHK0Fp8nC5DDoMfmM6SsYv5dsfiYDbLCA X-Google-Smtp-Source: AGHT+IEl+/L1ituyaJTnk0BjJi3vRmGfqMR+zcexC0PXTPj0xeHACJfp1bfVqYeei+6Mig3AUi1xRg== X-Received: by 2002:a05:6808:1813:b0:3c9:147c:bd22 with SMTP id bh19-20020a056808181300b003c9147cbd22mr1266333oib.29.1714537635509; Tue, 30 Apr 2024 21:27:15 -0700 (PDT) Received: from LancedeMBP.lan ([112.10.225.242]) by smtp.gmail.com with ESMTPSA id m15-20020a656a0f000000b005dc4806ad7dsm19165970pgu.40.2024.04.30.21.27.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Apr 2024 21:27:15 -0700 (PDT) From: Lance Yang To: akpm@linux-foundation.org Cc: willy@infradead.org, sj@kernel.org, maskray@google.com, ziy@nvidia.com, ryan.roberts@arm.com, david@redhat.com, 21cnbao@gmail.com, mhocko@suse.com, fengwei.yin@intel.com, zokeefe@google.com, shy828301@gmail.com, xiehuan09@gmail.com, libang.li@antgroup.com, wangkefeng.wang@huawei.com, songmuchun@bytedance.com, peterx@redhat.com, minchan@kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Lance Yang Subject: [PATCH v4 1/3] mm/rmap: remove duplicated exit code in pagewalk loop Date: Wed, 1 May 2024 12:26:58 +0800 Message-Id: <20240501042700.83974-2-ioworker0@gmail.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20240501042700.83974-1-ioworker0@gmail.com> References: <20240501042700.83974-1-ioworker0@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 7D2F840006 X-Rspamd-Server: rspam06 X-Stat-Signature: tohoyzfi8yqwp6amarr4r3etaqzoycex X-HE-Tag: 1714537636-28642 X-HE-Meta: U2FsdGVkX19UPTdnlhAWxSzMwzGTdmWxBiEmmicz/SaejSw2qybUF2HBYbd1QNnItkTS3nwxL9VvVN1bDAtIkjANygF0ew3WQIFIk8n8/H1nkdSGi5gfDQzutIXDkgcHF0GR2cSWNLFP5sJjSGfQ1kpS2ZwmNcB7Wy1bpRAW/ESiiWCCagYCIaRSUKM3bB00TEls4GHZvc5K/34/bnPkHHQ88N1uiwXQb1/wxbvk1YR3Dhsn2mUNyLFx1F10iOb5wJpl3x1g8nSrIYr0oGjAkuwHkejT577kldOrx/Ec8WOXyMe05v/MkFeC99kS9uhbTA/OimE3QLde2xdU70VXcF3SmZ+vZRfUsFcgriQ/EFjyAbBRpWqZAieXZ6uKN9AOdrlLg9LULQI2vWGndgFLpOD13OybdFe8PyRZTTtaJ+Ci9Zgh43dKRHNJBNqNioqzdLms0o4ITWjQxJbaPhcrd/FzsqLqzG+5J1IIgjhiFro8/PmHZO8OqxpRy1boksH079ngpisODu9DMmuTYtLKvu1xRK2iucE1gOvfhmdmQbvXVlw7/bZdVRZapJPijccXS9tWqj/LYAmLLkTFE8IZL3daxa4qKG2+4Wh5gHpxq5CQ8SVVyJSh3KCJ7/xXi9wa0Ds0bJavB1bpdEp+4ZbsVYSF58oyPy//GtxTdlzMoiPJgMyr2mWxZbU4DLfFKzVKezLGJN3qwZevbFxLVnybf40Dzyh1waC09eDv0n3zXh7kyP8F6+EWTUiWei7pYlWiOStqChPC8hi+Q/lgNuihsFaxniQzJ/QKWJ+XSJsaPxhKO/RsgicWnjsuntMNOJ3rCfhFFJjYKkrOXXKqG+QjBlRiH0EiHIbkDPpiNyt0RtzDbVzkqlNjBefH8WBDDllfFl6JDoeWDEqHnTp6ucsOuhzGSZvsOp2fVlWGC4bJcbeJgcaVuLFd9hp5SrUr1AjVi9ktM1t6C8a1JzC3gqn 6JENLkzJ 07QryO0QtiNp0EDM8kMZM9VcRzPMZ36RAtZik5PgwMb+aNc6cNYqi8pkMJ1/skZLC6+Go0SxSkcDceWcuYqr/zhWxsW9/9ONyNmDo+wN0xJHhTqXm0AYUrbXj/A== 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: Introduce the labels walk_done and walk_done_err as exit points to eliminate duplicated exit code in the pagewalk loop. Signed-off-by: Lance Yang --- mm/rmap.c | 40 +++++++++++++++------------------------- 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/mm/rmap.c b/mm/rmap.c index 7faa60bc3e4d..7e2575d669a9 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1675,9 +1675,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, /* Restore the mlock which got missed */ if (!folio_test_large(folio)) mlock_vma_folio(folio, vma); - page_vma_mapped_walk_done(&pvmw); - ret = false; - break; + goto walk_done_err; } pfn = pte_pfn(ptep_get(pvmw.pte)); @@ -1715,11 +1713,8 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, */ if (!anon) { VM_BUG_ON(!(flags & TTU_RMAP_LOCKED)); - if (!hugetlb_vma_trylock_write(vma)) { - page_vma_mapped_walk_done(&pvmw); - ret = false; - break; - } + if (!hugetlb_vma_trylock_write(vma)) + goto walk_done_err; if (huge_pmd_unshare(mm, vma, address, pvmw.pte)) { hugetlb_vma_unlock_write(vma); flush_tlb_range(vma, @@ -1734,8 +1729,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, * actual page and drop map count * to zero. */ - page_vma_mapped_walk_done(&pvmw); - break; + goto walk_done; } hugetlb_vma_unlock_write(vma); } @@ -1807,9 +1801,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, if (unlikely(folio_test_swapbacked(folio) != folio_test_swapcache(folio))) { WARN_ON_ONCE(1); - ret = false; - page_vma_mapped_walk_done(&pvmw); - break; + goto walk_done_err; } /* MADV_FREE page check */ @@ -1848,23 +1840,17 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, */ set_pte_at(mm, address, pvmw.pte, pteval); folio_set_swapbacked(folio); - ret = false; - page_vma_mapped_walk_done(&pvmw); - break; + goto walk_done_err; } if (swap_duplicate(entry) < 0) { set_pte_at(mm, address, pvmw.pte, pteval); - ret = false; - page_vma_mapped_walk_done(&pvmw); - break; + goto walk_done_err; } if (arch_unmap_one(mm, vma, address, pteval) < 0) { swap_free(entry); set_pte_at(mm, address, pvmw.pte, pteval); - ret = false; - page_vma_mapped_walk_done(&pvmw); - break; + goto walk_done_err; } /* See folio_try_share_anon_rmap(): clear PTE first. */ @@ -1872,9 +1858,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, folio_try_share_anon_rmap_pte(folio, subpage)) { swap_free(entry); set_pte_at(mm, address, pvmw.pte, pteval); - ret = false; - page_vma_mapped_walk_done(&pvmw); - break; + goto walk_done_err; } if (list_empty(&mm->mmlist)) { spin_lock(&mmlist_lock); @@ -1914,6 +1898,12 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, if (vma->vm_flags & VM_LOCKED) mlock_drain_local(); folio_put(folio); + continue; +walk_done_err: + ret = false; +walk_done: + page_vma_mapped_walk_done(&pvmw); + break; } mmu_notifier_invalidate_range_end(&range); From patchwork Wed May 1 04:26:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lance Yang X-Patchwork-Id: 13650207 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 4E096C4345F for ; Wed, 1 May 2024 04:27:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D2D4A6B0088; Wed, 1 May 2024 00:27:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CDBF16B0089; Wed, 1 May 2024 00:27:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B7C1E6B008A; Wed, 1 May 2024 00:27:23 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 998116B0088 for ; Wed, 1 May 2024 00:27:23 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 62455120553 for ; Wed, 1 May 2024 04:27:23 +0000 (UTC) X-FDA: 82068542766.28.34C45A7 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) by imf11.hostedemail.com (Postfix) with ESMTP id 8BFC64000D for ; Wed, 1 May 2024 04:27:21 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=CRAT+njx; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf11.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.210.178 as permitted sender) smtp.mailfrom=ioworker0@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1714537641; 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=ROFqZ6lfkIoUlwY4du6p7l8Lb0MkeIYOr3uqszlhzvk=; b=fNbgrOlX/sWP/YX8IA7DATu0iVT2jrMqCMsp4C5UcRsVos1HTf/uvGYBNe7jRL+XURhD6c hD9O0I8rvAlA1MlFJDRvpNSJs+fV+rskUSbbv6EK90dm2qFk7our7IccE8Mihmi4cwhNxc pZg35OMG5cEaSbWc4ydkcLbFJ81/0J4= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=CRAT+njx; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf11.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.210.178 as permitted sender) smtp.mailfrom=ioworker0@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1714537641; a=rsa-sha256; cv=none; b=7nDfTqjGsMtozd0j5/rKrfilDECinrTsl12BlDs4fEEdB/o/vT2ndP7DgCqagK0dex7n3Y QFLvxIJ5M8hFBL6Pp9F3TmpGQQ56L2IYtGYhqvCOjufdeuM/qftaELBMYpM9MIG3HR0cj5 GpbnWiFWUtkwekeivgbon6Ovdty0yQM= Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-6ee12766586so383181b3a.0 for ; Tue, 30 Apr 2024 21:27:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714537640; x=1715142440; 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=ROFqZ6lfkIoUlwY4du6p7l8Lb0MkeIYOr3uqszlhzvk=; b=CRAT+njxFTNUvETa1ISPxjBMcF3wefJm+y+Zk62ffjCLgHE5DD5K2qRfTRhRI4PQxk cYqb9wX71DhznmYq4St3BQJyiXyOGYMctn9Vqv08mTgHBrp+zlTmDRMOI87lx939vUWc u3m0TO2EpBEjHIwP+sU5+gzwn6RL4iIxtDBxoyh0TNJGQT2wIdEeORkZc+qGepV5I9uY 7GoAJ8heFy+4n7kXngRvbV4YsjMldd8Az4MwF4L6znv1OwdQVy3Y4wSPygrcTJo5nvDa 5K/ldc4n4J9XY990e4cRvKyX6Ea/Bl7Cgr8Pk61PS8r52mpbqL6XFwk5U7LCDOf5nr9J I4iw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714537640; x=1715142440; 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=ROFqZ6lfkIoUlwY4du6p7l8Lb0MkeIYOr3uqszlhzvk=; b=pAq7KYPMB+u008pWNbQfNsKXAoIvgGay9hhjKy95Y2hS4zeDZ0Zl8eAu4yg9GnHBVP Hw4NMHWJKcLXOHS8PdkrJt8noO1wMqPRWMBWoWWkC73XaF6hzEriuL3FXt//CQANNGGA YJQEopmCcgMLdVtE3REoQvITGddh0UNVfOUZlbAK5kRNOdPvEMJILRTP1GuC7iWSMXFC hmd2tgwtLgKcI/5nanOAZJz4fP9nJ6QG/vNRqZNuMlg/h81EU2tOBdirtyYfsh2ELugG am5k0/Q4PqAyDphMysX10/oOGAlN0opqi7pc0qvIQMnysv2Ju4S0SWjeKLB7vAoRDR0h gigA== X-Forwarded-Encrypted: i=1; AJvYcCX4OGuUgFLBaRWGr0du14atz6qk/jOOTJspnRxTtuBFbBa/hxYaM3Htzh8Gsq7nX0s6df2pT8V/pJE/XSFLLKICra0= X-Gm-Message-State: AOJu0YwTUyAugenBbKFhJFnwbUSmOxc7ZkCKXJQ6sexSBFZan23+/aiA 1G9HOqdL9j5vEGlBIdxkafcw+vG6XR6QbbKh3wlQaQ9WmUvgrAdX X-Google-Smtp-Source: AGHT+IF2W0eVQxBQaMDY8AWoFZ/AvARxWOBrEttWhO+PAqpgYqb/8VwaVvuvijythAV5TvB4SnJUzQ== X-Received: by 2002:a05:6a20:431a:b0:1ad:4978:adf4 with SMTP id h26-20020a056a20431a00b001ad4978adf4mr7731975pzk.1.1714537640184; Tue, 30 Apr 2024 21:27:20 -0700 (PDT) Received: from LancedeMBP.lan ([112.10.225.242]) by smtp.gmail.com with ESMTPSA id m15-20020a656a0f000000b005dc4806ad7dsm19165970pgu.40.2024.04.30.21.27.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Apr 2024 21:27:19 -0700 (PDT) From: Lance Yang To: akpm@linux-foundation.org Cc: willy@infradead.org, sj@kernel.org, maskray@google.com, ziy@nvidia.com, ryan.roberts@arm.com, david@redhat.com, 21cnbao@gmail.com, mhocko@suse.com, fengwei.yin@intel.com, zokeefe@google.com, shy828301@gmail.com, xiehuan09@gmail.com, libang.li@antgroup.com, wangkefeng.wang@huawei.com, songmuchun@bytedance.com, peterx@redhat.com, minchan@kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Lance Yang Subject: [PATCH v4 2/3] mm/rmap: integrate PMD-mapped folio splitting into pagewalk loop Date: Wed, 1 May 2024 12:26:59 +0800 Message-Id: <20240501042700.83974-3-ioworker0@gmail.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20240501042700.83974-1-ioworker0@gmail.com> References: <20240501042700.83974-1-ioworker0@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 8BFC64000D X-Stat-Signature: hczabxnobhksm3ju3o691newmqprpzyo X-Rspam-User: X-HE-Tag: 1714537641-317989 X-HE-Meta: U2FsdGVkX18z83/Tpa/bzIfE5IQMeMsjhg156rpojpL2+Pgcw9OY1iCDqMSUIN1ivwcagoSKJBnq1cm5TLCOddTMHtnzDbYuIbUIOzx5Mwrt5T1wwhvXsYc4U78l9w60NBlzu9xY8bavVH3OfA4XTp44Su2ApgIf6g1Bg62yAAOSGLnBPuSavhTB+h/R5IQxMdjf/IvRnC31K4sJP/fA9L7N8K8bQilkjcdK5PXV8WY2+GpcJ/cgnhUpR4Wr9SkYfvxN5bkKb3sh38W5eDlZCxN1ISJHv++NMCLHfzBxmdQl7ry3+GiOx4DGuK8OC1tALAeO4U1Ud+LzHb8EIwHeRacUWBNy6vPTD2KjLqY2MJo5s2t2iOWpmjP117Wr4X5OCXeSgqr6JGkcMH9W3TxxUihWbD4RqX5UpFRZjo+04q1jLPRzratQi8yJ8u8x/8zbxPYflsycas471AVf+monQfn47dZlRrQFW0NCFoEl4dflMvW51LulARR3gLQSdn7NPx3CaFMYXpp59E9cA6geE3rzV/f2bJC3sq1xRAu4/7EvQz1MELGKbTr6o933FbDjzYU4bScFCCObFIhxu/aLaWtKv8sZ+k5WHQHXuVoOSHPo2vIthY24VFaa2EdrwuOP9WCxFaSsExVZIOfI6NZFUDGieT6vZ1yrbO6tsGDLZ5sb7KUdyTgbVExyjwZJulTGwPuyV/CjBUEKpG0qn8E+H0kAS4S6SSYkzTIrPVjpAZlvV+dtXda3kNt3LzEMzbTllP6VdwRQZiM0An2GxVzCKrRF1IoKw3tcBIekp31/3PQh525EcXwUgaO+YT4zalXHdiermLDgf0RmT+wGa4tHxWu7VMZuTWYrRy/pS/fJn1y1wHp0Dvz36kF3qIwdoUf/47e0imj+oEaPfPYeTAbPqz6RfisfKlCcRlI4HnTznKQ6aS7VFiUx9Sb3ISdvgO2j6PUaSJY1PijrfNWnotj +T/x4tU/ xSobKo8cSKhFLXXzchrh9chSYYInh36EkttwT5i6hcVeb6OZbASx3P4x2ZCEN4CZ0IqwWZudUFgIMfjI4ScpIg7tBHx7kB1P2/yR5WLzquZqtQT+Cn4yuFcHurkPNOiM4uIWbTkADNXoRx6f9XvQvSDhKiG0g/HwaeoWzvKny6c6mWwY3znEJtCuO+CyOos4dn7MaLzkYCF9DwFGpLUc9EZRG17OKYRXynh/Ch5tWWuMa4TOlj/JC4MeAQ5SKLC9bTRIec+unYpeSTV6Mf6KBkM1K7wUO4p/vZkcFskFllzalUevqExUtlZiaiioS2yd79HdiE18UXE44g6R1Na7GUjgnSCkXHjlUXFuPrEHE+e1fMKtiwxVt3gnanOID05Ufy31XAVamBZIQjpN+a2oJgiU5Fi2OdIaEsB+EX2Ecaon3hSlKSitXsGLhRfFb50gA7ZxyGNjvpY+GGvXlGldnSxX/CQ== 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: In preparation for supporting try_to_unmap_one() to unmap PMD-mapped folios, start the pagewalk first, then call split_huge_pmd_address() to split the folio. Suggested-by: David Hildenbrand Signed-off-by: Lance Yang --- include/linux/huge_mm.h | 20 ++++++++++++++++++++ mm/huge_memory.c | 42 +++++++++++++++++++++-------------------- mm/rmap.c | 24 +++++++++++++++++------ 3 files changed, 60 insertions(+), 26 deletions(-) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index c8d3ec116e29..38c4b5537715 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -409,6 +409,20 @@ static inline bool thp_migration_supported(void) return IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION); } +void split_huge_pmd_locked(struct vm_area_struct *vma, unsigned long address, + pmd_t *pmd, bool freeze, struct folio *folio); + +static inline void align_huge_pmd_range(struct vm_area_struct *vma, + unsigned long *start, + unsigned long *end) +{ + *start = ALIGN(*start, HPAGE_PMD_SIZE); + *end = ALIGN_DOWN(*end, HPAGE_PMD_SIZE); + + VM_WARN_ON_ONCE(vma->vm_start > *start); + VM_WARN_ON_ONCE(vma->vm_end < *end); +} + #else /* CONFIG_TRANSPARENT_HUGEPAGE */ static inline bool folio_test_pmd_mappable(struct folio *folio) @@ -471,6 +485,12 @@ static inline void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd, unsigned long address, bool freeze, struct folio *folio) {} static inline void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address, bool freeze, struct folio *folio) {} +static inline void split_huge_pmd_locked(struct vm_area_struct *vma, + unsigned long address, pmd_t *pmd, + bool freeze, struct folio *folio) {} +static inline void align_huge_pmd_range(struct vm_area_struct *vma, + unsigned long *start, + unsigned long *end) {} #define split_huge_pud(__vma, __pmd, __address) \ do { } while (0) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 8261b5669397..145505a1dd05 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2584,6 +2584,27 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, pmd_populate(mm, pmd, pgtable); } +void split_huge_pmd_locked(struct vm_area_struct *vma, unsigned long address, + pmd_t *pmd, bool freeze, struct folio *folio) +{ + VM_WARN_ON_ONCE(folio && !folio_test_pmd_mappable(folio)); + VM_WARN_ON_ONCE(!IS_ALIGNED(address, HPAGE_PMD_SIZE)); + VM_WARN_ON_ONCE(folio && !folio_test_locked(folio)); + VM_BUG_ON(freeze && !folio); + + /* + * When the caller requests to set up a migration entry, we + * require a folio to check the PMD against. Otherwise, there + * is a risk of replacing the wrong folio. + */ + if (pmd_trans_huge(*pmd) || pmd_devmap(*pmd) || + is_pmd_migration_entry(*pmd)) { + if (folio && folio != pmd_folio(*pmd)) + return; + __split_huge_pmd_locked(vma, pmd, address, freeze); + } +} + void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd, unsigned long address, bool freeze, struct folio *folio) { @@ -2595,26 +2616,7 @@ void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd, (address & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE); mmu_notifier_invalidate_range_start(&range); ptl = pmd_lock(vma->vm_mm, pmd); - - /* - * If caller asks to setup a migration entry, we need a folio to check - * pmd against. Otherwise we can end up replacing wrong folio. - */ - VM_BUG_ON(freeze && !folio); - VM_WARN_ON_ONCE(folio && !folio_test_locked(folio)); - - if (pmd_trans_huge(*pmd) || pmd_devmap(*pmd) || - is_pmd_migration_entry(*pmd)) { - /* - * It's safe to call pmd_page when folio is set because it's - * guaranteed that pmd is present. - */ - if (folio && folio != pmd_folio(*pmd)) - goto out; - __split_huge_pmd_locked(vma, pmd, range.start, freeze); - } - -out: + split_huge_pmd_locked(vma, range.start, pmd, freeze, folio); spin_unlock(ptl); mmu_notifier_invalidate_range_end(&range); } diff --git a/mm/rmap.c b/mm/rmap.c index 7e2575d669a9..432601154583 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1636,9 +1636,6 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, if (flags & TTU_SYNC) pvmw.flags = PVMW_SYNC; - if (flags & TTU_SPLIT_HUGE_PMD) - split_huge_pmd_address(vma, address, false, folio); - /* * For THP, we have to assume the worse case ie pmd for invalidation. * For hugetlb, it could be much worse if we need to do pud @@ -1650,6 +1647,8 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, range.end = vma_address_end(&pvmw); mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma->vm_mm, address, range.end); + if (flags & TTU_SPLIT_HUGE_PMD) + align_huge_pmd_range(vma, &range.start, &range.end); if (folio_test_hugetlb(folio)) { /* * If sharing is possible, start and end will be adjusted @@ -1664,9 +1663,6 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, mmu_notifier_invalidate_range_start(&range); while (page_vma_mapped_walk(&pvmw)) { - /* Unexpected PMD-mapped THP? */ - VM_BUG_ON_FOLIO(!pvmw.pte, folio); - /* * If the folio is in an mlock()d vma, we must not swap it out. */ @@ -1678,6 +1674,22 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, goto walk_done_err; } + if (!pvmw.pte && (flags & TTU_SPLIT_HUGE_PMD)) { + /* + * We temporarily have to drop the PTL and start once + * again from that now-PTE-mapped page table. + */ + split_huge_pmd_locked(vma, range.start, pvmw.pmd, false, + folio); + pvmw.pmd = NULL; + spin_unlock(pvmw.ptl); + flags &= ~TTU_SPLIT_HUGE_PMD; + continue; + } + + /* Unexpected PMD-mapped THP? */ + VM_BUG_ON_FOLIO(!pvmw.pte, folio); + pfn = pte_pfn(ptep_get(pvmw.pte)); subpage = folio_page(folio, pfn - folio_pfn(folio)); address = pvmw.address; From patchwork Wed May 1 04:27:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lance Yang X-Patchwork-Id: 13650208 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 067CDC4345F for ; Wed, 1 May 2024 04:27:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 90CB06B008A; Wed, 1 May 2024 00:27:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8BD096B008C; Wed, 1 May 2024 00:27:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 737AE6B0092; Wed, 1 May 2024 00:27:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 540B56B008A for ; Wed, 1 May 2024 00:27:28 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id C89DFA16A5 for ; Wed, 1 May 2024 04:27:27 +0000 (UTC) X-FDA: 82068542934.22.130B7C3 Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.173]) by imf19.hostedemail.com (Postfix) with ESMTP id 05CC81A000B for ; Wed, 1 May 2024 04:27:25 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="eC/5nZlL"; spf=pass (imf19.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.210.173 as permitted sender) smtp.mailfrom=ioworker0@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=1714537646; 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=hOzQapCM9/nhQFCiXcuQFHHgx8BSo7e2vP87qJAgbT0=; b=ZYT9dQsHrAeHhN39/AUAnBHyJ+R9fSrGlgxQ8btfahVfOOSxrLHZzE3kgwyLGeTuGrRSvv cdwx2EW3D3f+W/M4VNr87HNsUOyUtciZ6Gnuw7EnLFaIoeovvSTD1ySx1r/A8XVqybhqLB 00oxgs3jTKVUXAiYVKc3tw4drV765fI= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="eC/5nZlL"; spf=pass (imf19.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.210.173 as permitted sender) smtp.mailfrom=ioworker0@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1714537646; a=rsa-sha256; cv=none; b=DNMHwqDDOlY/Eyd2hJlScxL9OtxUx86eQ3GfCnuGlqfGDmv5U8NuE9L/Xy+uxqoJETVKh7 eWVfVxo+iKFpeihFEiTrb6VcQ+M3/M8e/DogXV1++8xC9w7zdiHiBz/W+gyC/VnQDonbT+ gjwVrE4chjGn/45/GQi5hNCMLjSdfsE= Received: by mail-pf1-f173.google.com with SMTP id d2e1a72fcca58-6effe9c852eso5767910b3a.3 for ; Tue, 30 Apr 2024 21:27:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714537645; x=1715142445; 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=hOzQapCM9/nhQFCiXcuQFHHgx8BSo7e2vP87qJAgbT0=; b=eC/5nZlLP5acP1YfnY0MUc1mgioLhu+OLi5KMLolAr/eKTOJPkapcIO/1azaFIiNrC J3PaL46p09BxBIjjFjaw/taYu1FEadWrMWBYN8OIdQktWQ3eYj0vQomoq3RKI4JEUmgG WPwJGDzMSLLdca/NVamT41u3a5Grfr4WftqZjCEIrF8nCXKFsjO0hmRztx3fsaPDZPzf i/BHvfZIacxZ/LfPclv1pTLGLSL0EHXCDujxTW3cdIKnWaK90dz17kyGBIsxHXnWzBhv mPHY/bSxKHzeLJKMtm0jS8gqZOtrAT6aHc0fZf6jveHMx5JSdgnZcJ7oZkbh1N8nSZ7X Iqyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714537645; x=1715142445; 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=hOzQapCM9/nhQFCiXcuQFHHgx8BSo7e2vP87qJAgbT0=; b=QNdtrgt6bSoTpmZT7u1vrqbg2OxnkY3nJcWVjOqvTpel0fEFQIMGyDenltx3mW+Zea byaFKoK72iv7+1D7S39LX0S1w7McLFOCE1CCTa5JGZPe9HKzK3/wFey/u+ATOM1Uaqee dSDfju9pE635BgRPD9AzfcNwpXHE4Uthw/GYjcaOCnciKiMNMMqtaykWwaavW5uCOqbA Q9bxqeBWYzFteXsaIoYAjZXujLywO2mgnpvV/cBy4uCuVMvenSuZli5eIzUo2PpaKbY/ D/D+kVzIP9p5dR2tn+he2aWuUqbeoK+C6v+5nSniF9E/+4gX97vfB6MNJvW7lmQ4e42P TT9w== X-Forwarded-Encrypted: i=1; AJvYcCV0Hj8eTx6sRFmgSzxU1il4Us8gTwKeAzKR0Uen/Kboz88CQpHIzFUG5+kzTpqkM60vLH81KuHg8OT1eCHVzffmksI= X-Gm-Message-State: AOJu0YyJ85DSZHvhdnxVEQ+psKszzLm0U6jmTAkQ9FP1igUokcMmTlV4 oRbggDmZ7lzKjvPq4G327hctXtSMMIy7E1IMPO8KKlUj6+MFOpbP X-Google-Smtp-Source: AGHT+IExH8/mQKcC+a0yuiE7nJbe/y3Zn478CUKsgvKf8sffmUuOcVVFi/q1aIYi5BB/0pju90QCCg== X-Received: by 2002:a05:6a00:2345:b0:6ed:d8d2:503d with SMTP id j5-20020a056a00234500b006edd8d2503dmr2150218pfj.20.1714537644912; Tue, 30 Apr 2024 21:27:24 -0700 (PDT) Received: from LancedeMBP.lan ([112.10.225.242]) by smtp.gmail.com with ESMTPSA id m15-20020a656a0f000000b005dc4806ad7dsm19165970pgu.40.2024.04.30.21.27.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Apr 2024 21:27:24 -0700 (PDT) From: Lance Yang To: akpm@linux-foundation.org Cc: willy@infradead.org, sj@kernel.org, maskray@google.com, ziy@nvidia.com, ryan.roberts@arm.com, david@redhat.com, 21cnbao@gmail.com, mhocko@suse.com, fengwei.yin@intel.com, zokeefe@google.com, shy828301@gmail.com, xiehuan09@gmail.com, libang.li@antgroup.com, wangkefeng.wang@huawei.com, songmuchun@bytedance.com, peterx@redhat.com, minchan@kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Lance Yang Subject: [PATCH v4 3/3] mm/vmscan: avoid split lazyfree THP during shrink_folio_list() Date: Wed, 1 May 2024 12:27:00 +0800 Message-Id: <20240501042700.83974-4-ioworker0@gmail.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20240501042700.83974-1-ioworker0@gmail.com> References: <20240501042700.83974-1-ioworker0@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 05CC81A000B X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: tqtue8wdumt8xg86horfi6i35ercahdz X-HE-Tag: 1714537645-90837 X-HE-Meta: U2FsdGVkX18Gm7OOo1tHbcucuDhauj6qo2FQyh/0Daw1nFv/mQVFYf3+qv2Czct1lp4C3QBL4Sg1JR2n4VsBrTJ/pi5AZbNRgZt8o7RIEbzR2qGNfF4bNrgNVfs/a8+bdUYVJe6kMboR0AKYdVdiT7plj3upH/V5M82VBQ1ScrS0DDiBJyilzza6C0y0MqP+CIgoh+IZgERJYVHQ1CfIVS46R+3eYP8pyYXtf6/WBHuSip52XWrev6E+GbmWuMWCImPpBvoL1zL7unLl27ivHd7FNR5ZolMdtR1G8XK9cVJ1zzfgC8UwI/5UpHZSqUdZg2KKrGl6P9Dm064Rg7TcNDaDRxTBLRHoDRKn/nYaIcAq8qlxDw+vQ37OC+4f9jllqSi20AFT3z9TZwdzAft6ci43DtTfaoBM+ehk0HFVqOEUyNHshCQ9ZYk8LKc4p3HWrMMzNw/uXrlEyAoCGcvzQY0iIRpztlCRjp9oFZHntQYuEgn57ouZr0DAkaQqpQ5uodp/QuFMzxaQTJgZPpoxTwauzI8v6gNgDtozMKNRfiU+VaDd+h88ulCVEnZspupe6xQLrytClN4C95VXHUyKQIOjcWizy30KThqLZPHgELYxjoeStI4JQsZOGocZu7kZ731bYygN9P7TI6Zp10YP9OF9+25SfLr+gvbS+z3e2KdrIPV580bgGUnMObTFqiijHJyHZdmKx5rtGyO+jtgajrYff3P+ZcRaSepAy1zeJX8hqCxT69qObIyie6PbmtQoR4k/spJMK4nWIrJvXSzhy/zbtPneuNZQoKO7vbleUbFt+Jvggkzxmnt8lC9PmyIfW07o9THP+Dn+GAXGET5HG/bJN7rzswl6TMvYNRqrhW5JjxMT3JXP1yq5zHLmEv9Qa1n97T5W0C+nPvKnqyp9iPJg7s9o9njtJw33ZmGoZwTOsmObXmKaahZt43VTBkJTF9tr2/cMPa2tku44xBh zrR9BFt+ /yRW71JEis8hZxESzaJe6Ozlcfh1Kq00rh4/oSoCbkEdwgdkZsblLO0heahCfMtZMd7CpsL+95PHg81Nv7WeZjPiEQ+/NtEu3JZt41AZzy6mKWGCrmcawXWcRtCViDr/zHLr47b7EbQSi/u9yNONtXEVtWv1caLO2nM/VEXpGkcNmK5H2QkHApdu/uQ1QOQta0VhnnUHpYF44d/V3rAwtl+MnYi4jasbHWeJDUb/T918Et5cdxoedIMmue/zJGIHfmMQ7z4AZ4h+QYY/6Xr0FHGUn812WzEiylwamOuXkpU48HTkFL7FdxNaUvu5Ihr2DQgmUR2J+WEu6/TRoBHHgpxPRhE+L/Bk802qA3aE3ozKiB8+7SlIEqbiH2M12NRL7EmVTfVCnIdkibCufWBJeRsIH3zWOiZAAtEKgSZUVHnUqdQiTV1SHA8aQjnpyMcDGyplNakYo72Ej7AQ4It3ktKvVn7alrv3MqzVv+dv3Wu9P45Ftty5Fw169Lg== 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: When the user no longer requires the pages, they would use madvise(MADV_FREE) to mark the pages as lazy free. Subsequently, they typically would not re-write to that memory again. During memory reclaim, if we detect that the large folio and its PMD are both still marked as clean and there are no unexpected references (such as GUP), so we can just discard the memory lazily, improving the efficiency of memory reclamation in this case. On an Intel i5 CPU, reclaiming 1GiB of lazyfree THPs using mem_cgroup_force_empty() results in the following runtimes in seconds (shorter is better): -------------------------------------------- | Old | New | Change | -------------------------------------------- | 0.683426 | 0.049197 | -92.80% | -------------------------------------------- Suggested-by: Zi Yan Suggested-by: David Hildenbrand Signed-off-by: Lance Yang --- include/linux/huge_mm.h | 9 +++++ mm/huge_memory.c | 73 +++++++++++++++++++++++++++++++++++++++++ mm/rmap.c | 3 ++ 3 files changed, 85 insertions(+) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index 38c4b5537715..017cee864080 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -411,6 +411,8 @@ static inline bool thp_migration_supported(void) void split_huge_pmd_locked(struct vm_area_struct *vma, unsigned long address, pmd_t *pmd, bool freeze, struct folio *folio); +bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr, + pmd_t *pmdp, struct folio *folio); static inline void align_huge_pmd_range(struct vm_area_struct *vma, unsigned long *start, @@ -492,6 +494,13 @@ static inline void align_huge_pmd_range(struct vm_area_struct *vma, unsigned long *start, unsigned long *end) {} +static inline bool unmap_huge_pmd_locked(struct vm_area_struct *vma, + unsigned long addr, pmd_t *pmdp, + struct folio *folio) +{ + return false; +} + #define split_huge_pud(__vma, __pmd, __address) \ do { } while (0) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 145505a1dd05..90fdef847a88 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2690,6 +2690,79 @@ static void unmap_folio(struct folio *folio) try_to_unmap_flush(); } +static bool __discard_trans_pmd_locked(struct vm_area_struct *vma, + unsigned long addr, pmd_t *pmdp, + struct folio *folio) +{ + struct mm_struct *mm = vma->vm_mm; + int ref_count, map_count; + pmd_t orig_pmd = *pmdp; + struct mmu_gather tlb; + struct page *page; + + if (pmd_dirty(orig_pmd) || folio_test_dirty(folio)) + return false; + if (unlikely(!pmd_present(orig_pmd) || !pmd_trans_huge(orig_pmd))) + return false; + + page = pmd_page(orig_pmd); + if (unlikely(page_folio(page) != folio)) + return false; + + tlb_gather_mmu(&tlb, mm); + orig_pmd = pmdp_huge_get_and_clear(mm, addr, pmdp); + tlb_remove_pmd_tlb_entry(&tlb, pmdp, addr); + + /* + * Syncing against concurrent GUP-fast: + * - clear PMD; barrier; read refcount + * - inc refcount; barrier; read PMD + */ + smp_mb(); + + ref_count = folio_ref_count(folio); + map_count = folio_mapcount(folio); + + /* + * Order reads for folio refcount and dirty flag + * (see comments in __remove_mapping()). + */ + smp_rmb(); + + /* + * If the PMD or folio is redirtied at this point, or if there are + * unexpected references, we will give up to discard this folio + * and remap it. + * + * The only folio refs must be one from isolation plus the rmap(s). + */ + if (ref_count != map_count + 1 || folio_test_dirty(folio) || + pmd_dirty(orig_pmd)) { + set_pmd_at(mm, addr, pmdp, orig_pmd); + return false; + } + + folio_remove_rmap_pmd(folio, page, vma); + zap_deposited_table(mm, pmdp); + add_mm_counter(mm, MM_ANONPAGES, -HPAGE_PMD_NR); + folio_put(folio); + + return true; +} + +bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr, + pmd_t *pmdp, struct folio *folio) +{ + VM_WARN_ON_FOLIO(!folio_test_pmd_mappable(folio), folio); + VM_WARN_ON_FOLIO(!folio_test_locked(folio), folio); + VM_WARN_ON_ONCE(!IS_ALIGNED(addr, HPAGE_PMD_SIZE)); + + if (folio_test_anon(folio) && !folio_test_swapbacked(folio)) + return __discard_trans_pmd_locked(vma, addr, pmdp, folio); + + return false; +} + static void remap_page(struct folio *folio, unsigned long nr) { int i = 0; diff --git a/mm/rmap.c b/mm/rmap.c index 432601154583..1d3d30cb752c 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1675,6 +1675,9 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, } if (!pvmw.pte && (flags & TTU_SPLIT_HUGE_PMD)) { + if (unmap_huge_pmd_locked(vma, range.start, pvmw.pmd, + folio)) + goto walk_done; /* * We temporarily have to drop the PTL and start once * again from that now-PTE-mapped page table.