From f4a6415f8d5fa447868d1fdc7119e0a328966379 Mon Sep 17 00:00:00 2001
From: Kienan Stewart <kstewart@efficios.com>
Date: Mon, 25 Mar 2024 10:30:32 -0400
Subject: [PATCH 3/4] Fix: mm_compaction_migratepages changed in linux 6.9-rc1

See upstream commit:

    commit ab755bf4249b992fc2140d615ab0a686d50765b4
    Author: Baolin Wang <baolin.wang@linux.alibaba.com>
    Date:   Tue Feb 20 14:16:31 2024 +0800

        mm: compaction: update the cc->nr_migratepages when allocating or freeing the freepages

        Currently we will use 'cc->nr_freepages >= cc->nr_migratepages' comparison
        to ensure that enough freepages are isolated in isolate_freepages(),
        however it just decreases the cc->nr_freepages without updating
        cc->nr_migratepages in compaction_alloc(), which will waste more CPU
        cycles and cause too many freepages to be isolated.

        So we should also update the cc->nr_migratepages when allocating or
        freeing the freepages to avoid isolating excess freepages.  And I can see
        fewer free pages are scanned and isolated when running thpcompact on my
        Arm64 server:

                                               k6.7         k6.7_patched
        Ops Compaction pages isolated      120692036.00   118160797.00
        Ops Compaction migrate scanned     131210329.00   154093268.00
        Ops Compaction free scanned       1090587971.00  1080632536.00
        Ops Compact scan efficiency               12.03          14.26

        Moreover, I did not see an obvious latency improvements, this is likely
        because isolating freepages is not the bottleneck in the thpcompact test
        case.

                                      k6.7                  k6.7_patched
        Amean     fault-both-1      1089.76 (   0.00%)     1080.16 *   0.88%*
        Amean     fault-both-3      1616.48 (   0.00%)     1636.65 *  -1.25%*
        Amean     fault-both-5      2266.66 (   0.00%)     2219.20 *   2.09%*
        Amean     fault-both-7      2909.84 (   0.00%)     2801.90 *   3.71%*
        Amean     fault-both-12     4861.26 (   0.00%)     4733.25 *   2.63%*
        Amean     fault-both-18     7351.11 (   0.00%)     6950.51 *   5.45%*
        Amean     fault-both-24     9059.30 (   0.00%)     9159.99 *  -1.11%*
        Amean     fault-both-30    10685.68 (   0.00%)    11399.02 *  -6.68%*

Upstream-Status: Backport [175fe77c Fix: mm_compaction_migratepages changed in linux 6.9-rc1]

Change-Id: I103a43fd1b549360b3fc978fd409b7c17ef3e192
Signed-off-by: Kienan Stewart <kstewart@efficios.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
---
 include/instrumentation/events/compaction.h | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

Index: lttng-modules-2.13.12/include/instrumentation/events/compaction.h
===================================================================
--- lttng-modules-2.13.12.orig/include/instrumentation/events/compaction.h
+++ lttng-modules-2.13.12/include/instrumentation/events/compaction.h
@@ -97,7 +97,22 @@ LTTNG_TRACEPOINT_EVENT_INSTANCE_MAP(comp
 
 #endif /* #else #if LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,0,0) */
 
-#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,18,0) || \
+#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(6,9,0))
+LTTNG_TRACEPOINT_EVENT_MAP(mm_compaction_migratepages,
+
+	compaction_migratepages,
+
+	TP_PROTO(unsigned int nr_migratepages,
+		unsigned int nr_succeeded),
+
+	TP_ARGS(nr_migratepages, nr_succeeded),
+
+	TP_FIELDS(
+		ctf_integer(unsigned long, nr_migrated, nr_succeeded)
+		ctf_integer(unsigned long, nr_failed, nr_migratepages - nr_succeeded)
+	)
+)
+#elif (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,18,0) || \
     LTTNG_RHEL_KERNEL_RANGE(5,14,0,163,0,0, 5,15,0,0,0,0))
 LTTNG_TRACEPOINT_EVENT_MAP(mm_compaction_migratepages,
 
