[PATCH 3/5] mm: add pmd level THP COW judgement helpers

Luka Bai
2026-05-01T05:56:17Z

From: Luka Bai <lukabai@tencent.com>

We add hugepage_cow_always and hugepage_cow_madvise as two convenient
helpers to decide whether we want to do THP COW under each specific
circumstance.

Also, we add a helper hugepage_cow_enabled to help us know the setup
more easily. THP COW is only opened when hugepage is globally enabled
or madvise enabled.

Signed-off-by: Luka Bai <lukabai@tencent.com>
---
include/linux/huge_mm.h | 32 ++++++++++++++++++++++++++++++++
1 file changed, 32 insertions(+)

diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h
index 2a62f0f92f68..3e5c6da3905b 100644
--- a/include/linux/huge_mm.h
+++ b/include/linux/huge_mm.h
@@ -203,6 +203,38 @@ static inline bool hugepage_global_always(void)
(1<<TRANSPARENT_HUGEPAGE_FLAG);
}

+static inline bool hugepage_cow_always(void)
+{
+ return transparent_hugepage_flags &
+ (1<<TRANSPARENT_HUGEPAGE_COW_FLAG);
+}
+
+static inline bool hugepage_cow_madvise(void)
+{
+ return transparent_hugepage_flags &
+ (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_COW_FLAG);
+}
+
+static inline bool hugepage_cow_enabled(struct vm_area_struct *vma)
+{
+ vm_flags_t vm_flags = vma->vm_flags;
+
+ /* anonymous THP need to be enabled first */
+ if (!hugepage_global_always() &&
+ (!hugepage_global_enabled() || !(vm_flags & VM_HUGEPAGE)))
+ return false;
+
+ /* always enables all the THP COW */
+ if (hugepage_cow_always())
+ return true;
+
+ /* madvise enables THP cow only when vm_flags says so */
+ if (hugepage_cow_madvise() && (vm_flags & VM_THP_COW))
+ return true;
+
+ return false;
+}
+
static inline int highest_order(unsigned long orders)
{
return fls_long(orders) - 1;

--
2.52.0