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+Pgcw9OY1iCDqMSUIN1ivwcagoSKJBnq1cm5TLCOddTMHtnzDbYuIbUIOzx5Mwrt5T1wwhvXsYc4U78l9w60NBlzu9xY8bavVH3O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4TOlj/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;