Skip to content

Archlinux Kernel based on different schedulers and some other performance improvements.

Add cachy-tt patch

Piotr Gorski5c508e5df60c
diff --git a/linux-cachyos-tt/.SRCINFO b/linux-cachyos-tt/.SRCINFO
index ad23e4e7..90e2ff15 100755
--- a/linux-cachyos-tt/.SRCINFO
+++ b/linux-cachyos-tt/.SRCINFO
@@ -1,9 +1,9 @@
# Generated by makepkg 6.0.1
-# Wed Jun 29 16:12:28 UTC 2022
+# Wed Jun 29 16:36:53 UTC 2022
pkgbase = linux-cachyos-tt
pkgdesc = Linux TT scheduler Kernel by CachyOS with other patches and improvements
pkgver = 5.18.8
- pkgrel = 2
+ pkgrel = 3
url = https://github.com/CachyOS/linux-cachyos
arch = x86_64
arch = x86_64_v3
@@ -28,6 +28,7 @@ pkgbase = linux-cachyos-tt
options = !strip
source = https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.18.8.tar.xz
source = config
+ source = 0001-cachy-tt.patch
source = https://raw.githubusercontent.com/ptr1337/kernel-patches/master/5.18/sched/0001-tt-5.18.patch
source = https://raw.githubusercontent.com/ptr1337/kernel-patches/master/5.18/0001-amd-perf.patch
source = https://raw.githubusercontent.com/ptr1337/kernel-patches/master/5.18/0001-bbr2.patch
@@ -44,6 +45,7 @@ pkgbase = linux-cachyos-tt
source = auto-cpu-optimization.sh
sha256sums = 0823eb05dba001cf9e2560a76dfa4d81c854e1dcfbfb25cc73ba64cd7e900a36
sha256sums = 58a21aaa4dda3d7cd3c617fd655782b35b27ef52c9e55157d5b2092dba61c5ab
+ sha256sums = 9b2dd4b454d7d57522ed0227df3b3c230d254ba3c3e59dc8da619f5f3372bf24
sha256sums = dcf5df8bd041a44d8657effc6cd13477957b100110038875ae7872c515e3d680
sha256sums = 326d129f9435145add756dc967accd56ffe1d8ff1b6650f84d2578c41bd6dfd6
sha256sums = dc2898751118804bc3f36b5a6928a2927d04919ce41c0ce013009f5564d6d232
diff --git a/linux-cachyos-tt/0001-cachy-tt.patch b/linux-cachyos-tt/0001-cachy-tt.patch
new file mode 100755
index 00000000..510027f5
--- /dev/null
+++ b/linux-cachyos-tt/0001-cachy-tt.patch
@@ -0,0 +1,4665 @@
+From c8b4c929c29af86837f17981e7ec31e11fd2a90c Mon Sep 17 00:00:00 2001
+From: Peter Jung <[email protected]>
+Date: Mon, 27 Jun 2022 13:53:28 +0200
+Subject: [PATCH 1/5] cachy
+
+Signed-off-by: Peter Jung <[email protected]>
+---
+ .../admin-guide/kernel-parameters.txt | 13 +
+ Documentation/admin-guide/pm/cpuidle.rst | 15 +-
+ Makefile | 2 +
+ arch/alpha/kernel/syscalls/syscall.tbl | 1 +
+ arch/arm/tools/syscall.tbl | 1 +
+ arch/arm64/include/asm/unistd.h | 2 +-
+ arch/arm64/include/asm/unistd32.h | 2 +
+ arch/ia64/kernel/syscalls/syscall.tbl | 1 +
+ arch/m68k/kernel/syscalls/syscall.tbl | 1 +
+ arch/microblaze/kernel/syscalls/syscall.tbl | 1 +
+ arch/mips/kernel/syscalls/syscall_n32.tbl | 1 +
+ arch/mips/kernel/syscalls/syscall_n64.tbl | 1 +
+ arch/mips/kernel/syscalls/syscall_o32.tbl | 1 +
+ arch/parisc/kernel/syscalls/syscall.tbl | 1 +
+ arch/powerpc/kernel/syscalls/syscall.tbl | 1 +
+ arch/s390/kernel/syscalls/syscall.tbl | 1 +
+ arch/sh/kernel/syscalls/syscall.tbl | 1 +
+ arch/sparc/kernel/syscalls/syscall.tbl | 1 +
+ arch/x86/Kconfig.cpu | 332 +++++++++++++++++-
+ arch/x86/Makefile | 40 ++-
+ arch/x86/entry/syscalls/syscall_32.tbl | 1 +
+ arch/x86/entry/syscalls/syscall_64.tbl | 1 +
+ arch/x86/include/asm/cpufeatures.h | 2 +-
+ arch/x86/include/asm/mwait.h | 1 +
+ arch/x86/include/asm/vdso/processor.h | 2 +-
+ arch/x86/include/asm/vermagic.h | 66 ++++
+ arch/x86/kernel/cpu/amd.c | 3 +-
+ arch/x86/kernel/process.c | 39 +-
+ arch/x86/kernel/tsc.c | 3 +
+ arch/x86/mm/fault.c | 4 +-
+ arch/xtensa/kernel/syscalls/syscall.tbl | 1 +
+ block/blk-core.c | 3 +
+ drivers/Makefile | 15 +-
+ drivers/ata/libahci.c | 4 +-
+ drivers/block/zram/Kconfig | 18 +
+ drivers/block/zram/zram_drv.c | 39 ++
+ drivers/cpufreq/Kconfig | 2 +-
+ drivers/cpufreq/cpufreq_ondemand.c | 6 +-
+ drivers/firmware/sysfb.c | 18 +-
+ drivers/hid/usbhid/hid-core.c | 8 +
+ drivers/idle/intel_idle.c | 44 +--
+ drivers/input/evdev.c | 19 +-
+ drivers/input/serio/i8042.c | 10 +-
+ drivers/md/dm-crypt.c | 5 +
+ drivers/net/dummy.c | 2 +-
+ drivers/nvme/host/core.c | 2 +-
+ drivers/pci/pci.c | 2 +-
+ drivers/powercap/intel_rapl_common.c | 2 +-
+ drivers/thermal/intel/intel_powerclamp.c | 10 +
+ fs/xattr.c | 15 +-
+ include/linux/ipc_namespace.h | 5 +-
+ include/linux/jbd2.h | 2 +-
+ include/linux/ksm.h | 4 +
+ include/linux/page_counter.h | 1 +
+ include/linux/pageblock-flags.h | 2 +-
+ include/linux/pagemap.h | 2 +-
+ include/linux/sched.h | 2 +
+ include/linux/syscalls.h | 1 +
+ include/linux/user_namespace.h | 4 +
+ include/linux/wait.h | 2 +
+ include/uapi/asm-generic/unistd.h | 5 +-
+ include/uapi/linux/if_bonding.h | 2 +-
+ init/Kconfig | 26 ++
+ init/do_mounts.c | 16 +-
+ init/version.c | 17 +
+ kernel/Kconfig.hz | 24 ++
+ kernel/fork.c | 14 +
+ kernel/locking/rwsem.c | 4 +-
+ kernel/module.c | 20 ++
+ kernel/sched/core.c | 5 +-
+ kernel/sched/debug.c | 1 +
+ kernel/sched/fair.c | 79 ++++-
+ kernel/sched/features.h | 2 +
+ kernel/sched/wait.c | 24 ++
+ kernel/sys_ni.c | 1 +
+ kernel/sysctl.c | 12 +
+ kernel/user_namespace.c | 7 +
+ kernel/watchdog.c | 2 +-
+ mm/compaction.c | 6 +-
+ mm/huge_memory.c | 4 +
+ mm/internal.h | 1 +
+ mm/ksm.c | 99 ++++--
+ mm/madvise.c | 111 ++++++
+ mm/memcontrol.c | 2 +-
+ mm/page-writeback.c | 8 +
+ mm/page_alloc.c | 42 ++-
+ mm/vmpressure.c | 4 +
+ mm/vmscan.c | 24 +-
+ net/ipv4/inet_connection_sock.c | 2 +-
+ net/ipv4/tcp.c | 4 +-
+ 90 files changed, 1180 insertions(+), 179 deletions(-)
+
+diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
+index c48937820..b23ba248c 100644
+--- a/Documentation/admin-guide/kernel-parameters.txt
++++ b/Documentation/admin-guide/kernel-parameters.txt
+@@ -1615,6 +1615,19 @@
+ Valid parameters: "on", "off"
+ Default: "on"
+
++ hostname= [KNL] Set the hostname (aka UTS nodename).
++ Format: <string>
++ This allows setting the system's hostname during early
++ startup. This sets the name returned by gethostname.
++ Using this parameter to set the hostname makes it
++ possible to ensure the hostname is correctly set before
++ any userspace processes run, avoiding the possibility
++ that a process may call gethostname before the hostname
++ has been explicitly set, resulting in the calling
++ process getting an incorrect result. The string must
++ not exceed the maximum allowed hostname length (usually
++ 64 characters) and will be truncated otherwise.
++
+ hlt [BUGS=ARM,SH]
+
+ hpet= [X86-32,HPET] option to control HPET usage
+diff --git a/Documentation/admin-guide/pm/cpuidle.rst b/Documentation/admin-guide/pm/cpuidle.rst
+index aec2cd2aa..19754beb5 100644
+--- a/Documentation/admin-guide/pm/cpuidle.rst
++++ b/Documentation/admin-guide/pm/cpuidle.rst
+@@ -612,8 +612,8 @@ the ``menu`` governor to be used on the systems that use the ``ladder`` governor
+ by default this way, for example.
+
+ The other kernel command line parameters controlling CPU idle time management
+-described below are only relevant for the *x86* architecture and some of
+-them affect Intel processors only.
++described below are only relevant for the *x86* architecture and references
++to ``intel_idle`` affect Intel processors only.
+
+ The *x86* architecture support code recognizes three kernel command line
+ options related to CPU idle time management: ``idle=poll``, ``idle=halt``,
+@@ -635,10 +635,13 @@ idle, so it very well may hurt single-thread computations performance as well as
+ energy-efficiency. Thus using it for performance reasons may not be a good idea
+ at all.]
+
+-The ``idle=nomwait`` option disables the ``intel_idle`` driver and causes
+-``acpi_idle`` to be used (as long as all of the information needed by it is
+-there in the system's ACPI tables), but it is not allowed to use the
+-``MWAIT`` instruction of the CPUs to ask the hardware to enter idle states.
++The ``idle=nomwait`` option prevents the use of ``MWAIT`` instruction of
++the CPU to enter idle states. When this option is used, the ``acpi_idle``
++driver will use the ``HLT`` instruction instead of ``MWAIT``. On systems
++running Intel processors, this option disables the ``intel_idle`` driver
++and forces the use of the ``acpi_idle`` driver instead. Note that in either
++case, ``acpi_idle`` driver will function only if all the information needed
++by it is in the system's ACPI tables.
+
+ In addition to the architecture-level kernel command line options affecting CPU
+ idle time management, there are parameters affecting individual ``CPUIdle``
+diff --git a/Makefile b/Makefile
+index 6ac3335f6..2dac6d2db 100644
+--- a/Makefile
++++ b/Makefile
+@@ -758,6 +758,8 @@ else ifdef CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE_O3
+ KBUILD_CFLAGS += -O3
+ else ifdef CONFIG_CC_OPTIMIZE_FOR_SIZE
+ KBUILD_CFLAGS += -Os
++else ifdef CONFIG_CC_OPTIMIZE_BASAL
++KBUILD_CFLAGS += -O1
+ endif
+
+ # Tell gcc to never replace conditional load with a non-conditional one
+diff --git a/arch/alpha/kernel/syscalls/syscall.tbl b/arch/alpha/kernel/syscalls/syscall.tbl
+index 3515bc4f1..00ff721da 100644
+--- a/arch/alpha/kernel/syscalls/syscall.tbl
++++ b/arch/alpha/kernel/syscalls/syscall.tbl
+@@ -490,3 +490,4 @@
+ 558 common process_mrelease sys_process_mrelease
+ 559 common futex_waitv sys_futex_waitv
+ 560 common set_mempolicy_home_node sys_ni_syscall
++561 common pmadv_ksm sys_pmadv_ksm
+diff --git a/arch/arm/tools/syscall.tbl b/arch/arm/tools/syscall.tbl
+index ac964612d..90933eabe 100644
+--- a/arch/arm/tools/syscall.tbl
++++ b/arch/arm/tools/syscall.tbl
+@@ -464,3 +464,4 @@
+ 448 common process_mrelease sys_process_mrelease
+ 449 common futex_waitv sys_futex_waitv
+ 450 common set_mempolicy_home_node sys_set_mempolicy_home_node
++451 common pmadv_ksm sys_pmadv_ksm
+diff --git a/arch/arm64/include/asm/unistd.h b/arch/arm64/include/asm/unistd.h
+index 4e65da344..14c95844f 100644
+--- a/arch/arm64/include/asm/unistd.h
++++ b/arch/arm64/include/asm/unistd.h
+@@ -38,7 +38,7 @@
+ #define __ARM_NR_compat_set_tls (__ARM_NR_COMPAT_BASE + 5)
+ #define __ARM_NR_COMPAT_END (__ARM_NR_COMPAT_BASE + 0x800)
+
+-#define __NR_compat_syscalls 451
++#define __NR_compat_syscalls 452
+ #endif
+
+ #define __ARCH_WANT_SYS_CLONE
+diff --git a/arch/arm64/include/asm/unistd32.h b/arch/arm64/include/asm/unistd32.h
+index 604a2053d..91f2bb719 100644
+--- a/arch/arm64/include/asm/unistd32.h
++++ b/arch/arm64/include/asm/unistd32.h
+@@ -907,6 +907,8 @@ __SYSCALL(__NR_process_mrelease, sys_process_mrelease)
+ __SYSCALL(__NR_futex_waitv, sys_futex_waitv)
+ #define __NR_set_mempolicy_home_node 450
+ __SYSCALL(__NR_set_mempolicy_home_node, sys_set_mempolicy_home_node)
++#define __NR_pmadv_ksm 451
++__SYSCALL(__NR_pmadv_ksm, sys_pmadv_ksm)
+
+ /*
+ * Please add new compat syscalls above this comment and update
+diff --git a/arch/ia64/kernel/syscalls/syscall.tbl b/arch/ia64/kernel/syscalls/syscall.tbl
+index 78b1d03e8..79ad5a568 100644
+--- a/arch/ia64/kernel/syscalls/syscall.tbl
++++ b/arch/ia64/kernel/syscalls/syscall.tbl
+@@ -371,3 +371,4 @@
+ 448 common process_mrelease sys_process_mrelease
+ 449 common futex_waitv sys_futex_waitv
+ 450 common set_mempolicy_home_node sys_set_mempolicy_home_node
++451 common pmadv_ksm sys_pmadv_ksm
+diff --git a/arch/m68k/kernel/syscalls/syscall.tbl b/arch/m68k/kernel/syscalls/syscall.tbl
+index b1f3940bc..5ccf92556 100644
+--- a/arch/m68k/kernel/syscalls/syscall.tbl
++++ b/arch/m68k/kernel/syscalls/syscall.tbl
+@@ -450,3 +450,4 @@
+ 448 common process_mrelease sys_process_mrelease
+ 449 common futex_waitv sys_futex_waitv
+ 450 common set_mempolicy_home_node sys_set_mempolicy_home_node
++451 common pmadv_ksm sys_pmadv_ksm
+diff --git a/arch/microblaze/kernel/syscalls/syscall.tbl b/arch/microblaze/kernel/syscalls/syscall.tbl
+index 820145e47..6b7620859 100644
+--- a/arch/microblaze/kernel/syscalls/syscall.tbl
++++ b/arch/microblaze/kernel/syscalls/syscall.tbl
+@@ -456,3 +456,4 @@
+ 448 common process_mrelease sys_process_mrelease
+ 449 common futex_waitv sys_futex_waitv
+ 450 common set_mempolicy_home_node sys_set_mempolicy_home_node
++451 common pmadv_ksm sys_pmadv_ksm
+diff --git a/arch/mips/kernel/syscalls/syscall_n32.tbl b/arch/mips/kernel/syscalls/syscall_n32.tbl
+index 253ff994e..e4aeedb17 100644
+--- a/arch/mips/kernel/syscalls/syscall_n32.tbl
++++ b/arch/mips/kernel/syscalls/syscall_n32.tbl
+@@ -389,3 +389,4 @@
+ 448 n32 process_mrelease sys_process_mrelease
+ 449 n32 futex_waitv sys_futex_waitv
+ 450 n32 set_mempolicy_home_node sys_set_mempolicy_home_node
++451 n32 pmadv_ksm sys_pmadv_ksm
+diff --git a/arch/mips/kernel/syscalls/syscall_n64.tbl b/arch/mips/kernel/syscalls/syscall_n64.tbl
+index 3f1886ad9..fe88db51e 100644
+--- a/arch/mips/kernel/syscalls/syscall_n64.tbl
++++ b/arch/mips/kernel/syscalls/syscall_n64.tbl
+@@ -365,3 +365,4 @@
+ 448 n64 process_mrelease sys_process_mrelease
+ 449 n64 futex_waitv sys_futex_waitv
+ 450 common set_mempolicy_home_node sys_set_mempolicy_home_node
++451 n64 pmadv_ksm sys_pmadv_ksm
+diff --git a/arch/mips/kernel/syscalls/syscall_o32.tbl b/arch/mips/kernel/syscalls/syscall_o32.tbl
+index 8f243e35a..674cb940b 100644
+--- a/arch/mips/kernel/syscalls/syscall_o32.tbl
++++ b/arch/mips/kernel/syscalls/syscall_o32.tbl
+@@ -438,3 +438,4 @@
+ 448 o32 process_mrelease sys_process_mrelease
+ 449 o32 futex_waitv sys_futex_waitv
+ 450 o32 set_mempolicy_home_node sys_set_mempolicy_home_node
++451 o32 pmadv_ksm sys_pmadv_ksm
+diff --git a/arch/parisc/kernel/syscalls/syscall.tbl b/arch/parisc/kernel/syscalls/syscall.tbl
+index 68b46fe2f..e19e8deb4 100644
+--- a/arch/parisc/kernel/syscalls/syscall.tbl
++++ b/arch/parisc/kernel/syscalls/syscall.tbl
+@@ -448,3 +448,4 @@
+ 448 common process_mrelease sys_process_mrelease
+ 449 common futex_waitv sys_futex_waitv
+ 450 common set_mempolicy_home_node sys_set_mempolicy_home_node
++451 common pmadv_ksm sys_pmadv_ksm
+diff --git a/arch/powerpc/kernel/syscalls/syscall.tbl b/arch/powerpc/kernel/syscalls/syscall.tbl
+index 2600b4237..bb2f71a36 100644
+--- a/arch/powerpc/kernel/syscalls/syscall.tbl
++++ b/arch/powerpc/kernel/syscalls/syscall.tbl
+@@ -530,3 +530,4 @@
+ 448 common process_mrelease sys_process_mrelease
+ 449 common futex_waitv sys_futex_waitv
+ 450 nospu set_mempolicy_home_node sys_set_mempolicy_home_node
++451 common pmadv_ksm sys_pmadv_ksm
+diff --git a/arch/s390/kernel/syscalls/syscall.tbl b/arch/s390/kernel/syscalls/syscall.tbl
+index 799147658..1cd523748 100644
+--- a/arch/s390/kernel/syscalls/syscall.tbl
++++ b/arch/s390/kernel/syscalls/syscall.tbl
+@@ -453,3 +453,4 @@
+ 448 common process_mrelease sys_process_mrelease sys_process_mrelease
+ 449 common futex_waitv sys_futex_waitv sys_futex_waitv
+ 450 common set_mempolicy_home_node sys_set_mempolicy_home_node sys_set_mempolicy_home_node
++451 common pmadv_ksm sys_pmadv_ksm sys_pmadv_ksm
+diff --git a/arch/sh/kernel/syscalls/syscall.tbl b/arch/sh/kernel/syscalls/syscall.tbl
+index 2de85c977..cfc75fa43 100644
+--- a/arch/sh/kernel/syscalls/syscall.tbl
++++ b/arch/sh/kernel/syscalls/syscall.tbl
+@@ -453,3 +453,4 @@
+ 448 common process_mrelease sys_process_mrelease
+ 449 common futex_waitv sys_futex_waitv
+ 450 common set_mempolicy_home_node sys_set_mempolicy_home_node
++451 common pmadv_ksm sys_pmadv_ksm
+diff --git a/arch/sparc/kernel/syscalls/syscall.tbl b/arch/sparc/kernel/syscalls/syscall.tbl
+index 4398cc6fb..d2c0a6426 100644
+--- a/arch/sparc/kernel/syscalls/syscall.tbl
++++ b/arch/sparc/kernel/syscalls/syscall.tbl
+@@ -496,3 +496,4 @@
+ 448 common process_mrelease sys_process_mrelease
+ 449 common futex_waitv sys_futex_waitv
+ 450 common set_mempolicy_home_node sys_set_mempolicy_home_node
++451 common pmadv_ksm sys_pmadv_ksm
+diff --git a/arch/x86/Kconfig.cpu b/arch/x86/Kconfig.cpu
+index 542377cd4..22b919cdb 100644
+--- a/arch/x86/Kconfig.cpu
++++ b/arch/x86/Kconfig.cpu
+@@ -157,7 +157,7 @@ config MPENTIUM4
+
+
+ config MK6
+- bool "K6/K6-II/K6-III"
++ bool "AMD K6/K6-II/K6-III"
+ depends on X86_32
+ help
+ Select this for an AMD K6-family processor. Enables use of
+@@ -165,7 +165,7 @@ config MK6
+ flags to GCC.
+
+ config MK7
+- bool "Athlon/Duron/K7"
++ bool "AMD Athlon/Duron/K7"
+ depends on X86_32
+ help
+ Select this for an AMD Athlon K7-family processor. Enables use of
+@@ -173,12 +173,98 @@ config MK7
+ flags to GCC.
+
+ config MK8
+- bool "Opteron/Athlon64/Hammer/K8"
++ bool "AMD Opteron/Athlon64/Hammer/K8"
+ help
+ Select this for an AMD Opteron or Athlon64 Hammer-family processor.
+ Enables use of some extended instructions, and passes appropriate
+ optimization flags to GCC.
+
++config MK8SSE3
++ bool "AMD Opteron/Athlon64/Hammer/K8 with SSE3"
++ help
++ Select this for improved AMD Opteron or Athlon64 Hammer-family processors.
++ Enables use of some extended instructions, and passes appropriate
++ optimization flags to GCC.
++
++config MK10
++ bool "AMD 61xx/7x50/PhenomX3/X4/II/K10"
++ help
++ Select this for an AMD 61xx Eight-Core Magny-Cours, Athlon X2 7x50,
++ Phenom X3/X4/II, Athlon II X2/X3/X4, or Turion II-family processor.
++ Enables use of some extended instructions, and passes appropriate
++ optimization flags to GCC.
++
++config MBARCELONA
++ bool "AMD Barcelona"
++ help
++ Select this for AMD Family 10h Barcelona processors.
++
++ Enables -march=barcelona
++
++config MBOBCAT
++ bool "AMD Bobcat"
++ help
++ Select this for AMD Family 14h Bobcat processors.
++
++ Enables -march=btver1
++
++config MJAGUAR
++ bool "AMD Jaguar"
++ help
++ Select this for AMD Family 16h Jaguar processors.
++
++ Enables -march=btver2
++
++config MBULLDOZER
++ bool "AMD Bulldozer"
++ help
++ Select this for AMD Family 15h Bulldozer processors.
++
++ Enables -march=bdver1
++
++config MPILEDRIVER
++ bool "AMD Piledriver"
++ help
++ Select this for AMD Family 15h Piledriver processors.
++
++ Enables -march=bdver2
++
++config MSTEAMROLLER
++ bool "AMD Steamroller"
++ help
++ Select this for AMD Family 15h Steamroller processors.
++
++ Enables -march=bdver3
++
++config MEXCAVATOR
++ bool "AMD Excavator"
++ help
++ Select this for AMD Family 15h Excavator processors.
++
++ Enables -march=bdver4
++
++config MZEN
++ bool "AMD Zen"
++ help
++ Select this for AMD Family 17h Zen processors.
++
++ Enables -march=znver1
++
++config MZEN2
++ bool "AMD Zen 2"
++ help
++ Select this for AMD Family 17h Zen 2 processors.
++
++ Enables -march=znver2
++
++config MZEN3
++ bool "AMD Zen 3"
++ depends on (CC_IS_GCC && GCC_VERSION >= 100300) || (CC_IS_CLANG && CLANG_VERSION >= 120000)
++ help
++ Select this for AMD Family 19h Zen 3 processors.
++
++ Enables -march=znver3
++
+ config MCRUSOE
+ bool "Crusoe"
+ depends on X86_32
+@@ -270,7 +356,7 @@ config MPSC
+ in /proc/cpuinfo. Family 15 is an older Xeon, Family 6 a newer one.
+
+ config MCORE2
+- bool "Core 2/newer Xeon"
++ bool "Intel Core 2"
+ help
+
+ Select this for Intel Core 2 and newer Core 2 Xeons (Xeon 51xx and
+@@ -278,6 +364,8 @@ config MCORE2
+ family in /proc/cpuinfo. Newer ones have 6 and older ones 15
+ (not a typo)
+
++ Enables -march=core2
++
+ config MATOM
+ bool "Intel Atom"
+ help
+@@ -287,6 +375,182 @@ config MATOM
+ accordingly optimized code. Use a recent GCC with specific Atom
+ support in order to fully benefit from selecting this option.
+
++config MNEHALEM
++ bool "Intel Nehalem"
++ select X86_P6_NOP
++ help
++
++ Select this for 1st Gen Core processors in the Nehalem family.
++
++ Enables -march=nehalem
++
++config MWESTMERE
++ bool "Intel Westmere"
++ select X86_P6_NOP
++ help
++
++ Select this for the Intel Westmere formerly Nehalem-C family.
++
++ Enables -march=westmere
++
++config MSILVERMONT
++ bool "Intel Silvermont"
++ select X86_P6_NOP
++ help
++
++ Select this for the Intel Silvermont platform.
++
++ Enables -march=silvermont
++
++config MGOLDMONT
++ bool "Intel Goldmont"
++ select X86_P6_NOP
++ help
++
++ Select this for the Intel Goldmont platform including Apollo Lake and Denverton.
++
++ Enables -march=goldmont
++
++config MGOLDMONTPLUS
++ bool "Intel Goldmont Plus"
++ select X86_P6_NOP
++ help
++
++ Select this for the Intel Goldmont Plus platform including Gemini Lake.
++
++ Enables -march=goldmont-plus
++
++config MSANDYBRIDGE
++ bool "Intel Sandy Bridge"
++ select X86_P6_NOP
++ help
++
++ Select this for 2nd Gen Core processors in the Sandy Bridge family.
++
++ Enables -march=sandybridge
++
++config MIVYBRIDGE
++ bool "Intel Ivy Bridge"
++ select X86_P6_NOP
++ help
++
++ Select this for 3rd Gen Core processors in the Ivy Bridge family.
++
++ Enables -march=ivybridge
++
++config MHASWELL
++ bool "Intel Haswell"
++ select X86_P6_NOP
++ help
++
++ Select this for 4th Gen Core processors in the Haswell family.
++
++ Enables -march=haswell
++
++config MBROADWELL
++ bool "Intel Broadwell"
++ select X86_P6_NOP
++ help
++
++ Select this for 5th Gen Core processors in the Broadwell family.
++
++ Enables -march=broadwell
++
++config MSKYLAKE
++ bool "Intel Skylake"
++ select X86_P6_NOP
++ help
++
++ Select this for 6th Gen Core processors in the Skylake family.
++
++ Enables -march=skylake
++
++config MSKYLAKEX
++ bool "Intel Skylake X"
++ select X86_P6_NOP
++ help
++
++ Select this for 6th Gen Core processors in the Skylake X family.
++
++ Enables -march=skylake-avx512
++
++config MCANNONLAKE
++ bool "Intel Cannon Lake"
++ select X86_P6_NOP
++ help
++
++ Select this for 8th Gen Core processors
++
++ Enables -march=cannonlake
++
++config MICELAKE
++ bool "Intel Ice Lake"
++ select X86_P6_NOP
++ help
++
++ Select this for 10th Gen Core processors in the Ice Lake family.
++
++ Enables -march=icelake-client
++
++config MCASCADELAKE
++ bool "Intel Cascade Lake"
++ select X86_P6_NOP
++ help
++
++ Select this for Xeon processors in the Cascade Lake family.
++
++ Enables -march=cascadelake
++
++config MCOOPERLAKE
++ bool "Intel Cooper Lake"
++ depends on (CC_IS_GCC && GCC_VERSION > 100100) || (CC_IS_CLANG && CLANG_VERSION >= 100000)
++ select X86_P6_NOP
++ help
++
++ Select this for Xeon processors in the Cooper Lake family.
++
++ Enables -march=cooperlake
++
++config MTIGERLAKE
++ bool "Intel Tiger Lake"
++ depends on (CC_IS_GCC && GCC_VERSION > 100100) || (CC_IS_CLANG && CLANG_VERSION >= 100000)
++ select X86_P6_NOP
++ help
++
++ Select this for third-generation 10 nm process processors in the Tiger Lake family.
++
++ Enables -march=tigerlake
++
++config MSAPPHIRERAPIDS
++ bool "Intel Sapphire Rapids"
++ depends on (CC_IS_GCC && GCC_VERSION > 110000) || (CC_IS_CLANG && CLANG_VERSION >= 120000)
++ select X86_P6_NOP
++ help
++
++ Select this for third-generation 10 nm process processors in the Sapphire Rapids family.
++
++ Enables -march=sapphirerapids
++
++config MROCKETLAKE
++ bool "Intel Rocket Lake"
++ depends on (CC_IS_GCC && GCC_VERSION > 110000) || (CC_IS_CLANG && CLANG_VERSION >= 120000)
++ select X86_P6_NOP
++ help
++
++ Select this for eleventh-generation processors in the Rocket Lake family.
++
++ Enables -march=rocketlake
++
++config MALDERLAKE
++ bool "Intel Alder Lake"
++ depends on (CC_IS_GCC && GCC_VERSION > 110000) || (CC_IS_CLANG && CLANG_VERSION >= 120000)
++ select X86_P6_NOP
++ help
++
++ Select this for twelfth-generation processors in the Alder Lake family.
++
++ Enables -march=alderlake
++
+ config GENERIC_CPU
+ bool "Generic-x86-64"
+ depends on X86_64
+@@ -294,6 +558,50 @@ config GENERIC_CPU
+ Generic x86-64 CPU.
+ Run equally well on all x86-64 CPUs.
+
++config GENERIC_CPU2
++ bool "Generic-x86-64-v2"
++ depends on (CC_IS_GCC && GCC_VERSION > 110000) || (CC_IS_CLANG && CLANG_VERSION >= 120000)
++ depends on X86_64
++ help
++ Generic x86-64 CPU.
++ Run equally well on all x86-64 CPUs with min support of x86-64-v2.
++
++config GENERIC_CPU3
++ bool "Generic-x86-64-v3"
++ depends on (CC_IS_GCC && GCC_VERSION > 110000) || (CC_IS_CLANG && CLANG_VERSION >= 120000)
++ depends on X86_64
++ help
++ Generic x86-64-v3 CPU with v3 instructions.
++ Run equally well on all x86-64 CPUs with min support of x86-64-v3.
++
++config GENERIC_CPU4
++ bool "Generic-x86-64-v4"
++ depends on (CC_IS_GCC && GCC_VERSION > 110000) || (CC_IS_CLANG && CLANG_VERSION >= 120000)
++ depends on X86_64
++ help
++ Generic x86-64 CPU with v4 instructions.
++ Run equally well on all x86-64 CPUs with min support of x86-64-v4.
++
++config MNATIVE_INTEL
++ bool "Intel-Native optimizations autodetected by the compiler"
++ help
++
++ Clang 3.8, GCC 4.2 and above support -march=native, which automatically detects
++ the optimum settings to use based on your processor. Do NOT use this
++ for AMD CPUs. Intel Only!
++
++ Enables -march=native
++
++config MNATIVE_AMD
++ bool "AMD-Native optimizations autodetected by the compiler"
++ help
++
++ Clang 3.8, GCC 4.2 and above support -march=native, which automatically detects
++ the optimum settings to use based on your processor. Do NOT use this
++ for Intel CPUs. AMD Only!
++
++ Enables -march=native
++
+ endchoice
+
+ config X86_GENERIC
+@@ -318,7 +626,7 @@ config X86_INTERNODE_CACHE_SHIFT
+ config X86_L1_CACHE_SHIFT
+ int
+ default "7" if MPENTIUM4 || MPSC
+- default "6" if MK7 || MK8 || MPENTIUMM || MCORE2 || MATOM || MVIAC7 || X86_GENERIC || GENERIC_CPU
++ default "6" if MK7 || MK8 || MPENTIUMM || MCORE2 || MATOM || MVIAC7 || MK8SSE3 || MK10 || MBARCELONA || MBOBCAT || MJAGUAR || MBULLDOZER || MPILEDRIVER || MSTEAMROLLER || MEXCAVATOR || MZEN || MZEN2 || MZEN3 || MNEHALEM || MWESTMERE || MSILVERMONT || MGOLDMONT || MGOLDMONTPLUS || MSANDYBRIDGE || MIVYBRIDGE || MHASWELL || MBROADWELL || MSKYLAKE || MSKYLAKEX || MCANNONLAKE || MICELAKE || MCASCADELAKE || MCOOPERLAKE || MTIGERLAKE || MSAPPHIRERAPIDS || MROCKETLAKE || MALDERLAKE || MNATIVE_INTEL || MNATIVE_AMD || X86_GENERIC || GENERIC_CPU || GENERIC_CPU2 || GENERIC_CPU3 || GENERIC_CPU4
+ default "4" if MELAN || M486SX || M486 || MGEODEGX1
+ default "5" if MWINCHIP3D || MWINCHIPC6 || MCRUSOE || MEFFICEON || MCYRIXIII || MK6 || MPENTIUMIII || MPENTIUMII || M686 || M586MMX || M586TSC || M586 || MVIAC3_2 || MGEODE_LX
+
+@@ -336,11 +644,11 @@ config X86_ALIGNMENT_16
+
+ config X86_INTEL_USERCOPY
+ def_bool y
+- depends on MPENTIUM4 || MPENTIUMM || MPENTIUMIII || MPENTIUMII || M586MMX || X86_GENERIC || MK8 || MK7 || MEFFICEON || MCORE2
++ depends on MPENTIUM4 || MPENTIUMM || MPENTIUMIII || MPENTIUMII || M586MMX || X86_GENERIC || MK8 || MK7 || MEFFICEON || MCORE2 || MNEHALEM || MWESTMERE || MSILVERMONT || MGOLDMONT || MGOLDMONTPLUS || MSANDYBRIDGE || MIVYBRIDGE || MHASWELL || MBROADWELL || MSKYLAKE || MSKYLAKEX || MCANNONLAKE || MICELAKE || MCASCADELAKE || MCOOPERLAKE || MTIGERLAKE || MSAPPHIRERAPIDS || MROCKETLAKE || MALDERLAKE || MNATIVE_INTEL
+
+ config X86_USE_PPRO_CHECKSUM
+ def_bool y
+- depends on MWINCHIP3D || MWINCHIPC6 || MCYRIXIII || MK7 || MK6 || MPENTIUM4 || MPENTIUMM || MPENTIUMIII || MPENTIUMII || M686 || MK8 || MVIAC3_2 || MVIAC7 || MEFFICEON || MGEODE_LX || MCORE2 || MATOM
++ depends on MWINCHIP3D || MWINCHIPC6 || MCYRIXIII || MK7 || MK6 || MPENTIUM4 || MPENTIUMM || MPENTIUMIII || MPENTIUMII || M686 || MK8 || MVIAC3_2 || MVIAC7 || MEFFICEON || MGEODE_LX || MCORE2 || MATOM || MK8SSE3 || MK10 || MBARCELONA || MBOBCAT || MJAGUAR || MBULLDOZER || MPILEDRIVER || MSTEAMROLLER || MEXCAVATOR || MZEN || MZEN2 || MZEN3 || MNEHALEM || MWESTMERE || MSILVERMONT || MGOLDMONT || MGOLDMONTPLUS || MSANDYBRIDGE || MIVYBRIDGE || MHASWELL || MBROADWELL || MSKYLAKE || MSKYLAKEX || MCANNONLAKE || MICELAKE || MCASCADELAKE || MCOOPERLAKE || MTIGERLAKE || MSAPPHIRERAPIDS || MROCKETLAKE || MALDERLAKE || MNATIVE_INTEL || MNATIVE_AMD
+
+ #
+ # P6_NOPs are a relatively minor optimization that require a family >=
+@@ -356,26 +664,26 @@ config X86_USE_PPRO_CHECKSUM
+ config X86_P6_NOP
+ def_bool y
+ depends on X86_64
+- depends on (MCORE2 || MPENTIUM4 || MPSC)
++ depends on (MCORE2 || MPENTIUM4 || MPSC || MNEHALEM || MWESTMERE || MSILVERMONT || MGOLDMONT || MGOLDMONTPLUS || MSANDYBRIDGE || MIVYBRIDGE || MHASWELL || MBROADWELL || MSKYLAKE || MSKYLAKEX || MCANNONLAKE || MICELAKE || MCASCADELAKE || MCOOPERLAKE || MTIGERLAKE || MSAPPHIRERAPIDS || MROCKETLAKE || MALDERLAKE || MNATIVE_INTEL)
+
+ config X86_TSC
+ def_bool y
+- depends on (MWINCHIP3D || MCRUSOE || MEFFICEON || MCYRIXIII || MK7 || MK6 || MPENTIUM4 || MPENTIUMM || MPENTIUMIII || MPENTIUMII || M686 || M586MMX || M586TSC || MK8 || MVIAC3_2 || MVIAC7 || MGEODEGX1 || MGEODE_LX || MCORE2 || MATOM) || X86_64
++ depends on (MWINCHIP3D || MCRUSOE || MEFFICEON || MCYRIXIII || MK7 || MK6 || MPENTIUM4 || MPENTIUMM || MPENTIUMIII || MPENTIUMII || M686 || M586MMX || M586TSC || MK8 || MVIAC3_2 || MVIAC7 || MGEODEGX1 || MGEODE_LX || MCORE2 || MATOM || MK8SSE3 || MK10 || MBARCELONA || MBOBCAT || MJAGUAR || MBULLDOZER || MPILEDRIVER || MSTEAMROLLER || MEXCAVATOR || MZEN || MZEN2 || MZEN3 || MNEHALEM || MWESTMERE || MSILVERMONT || MGOLDMONT || MGOLDMONTPLUS || MSANDYBRIDGE || MIVYBRIDGE || MHASWELL || MBROADWELL || MSKYLAKE || MSKYLAKEX || MCANNONLAKE || MICELAKE || MCASCADELAKE || MCOOPERLAKE || MTIGERLAKE || MSAPPHIRERAPIDS || MROCKETLAKE || MALDERLAKE || MNATIVE_INTEL || MNATIVE_AMD) || X86_64
+
+ config X86_CMPXCHG64
+ def_bool y
+- depends on X86_PAE || X86_64 || MCORE2 || MPENTIUM4 || MPENTIUMM || MPENTIUMIII || MPENTIUMII || M686 || M586TSC || M586MMX || MATOM || MGEODE_LX || MGEODEGX1 || MK6 || MK7 || MK8
++ depends on X86_PAE || X86_64 || MCORE2 || MPENTIUM4 || MPENTIUMM || MPENTIUMIII || MPENTIUMII || M686 || M586TSC || M586MMX || MATOM || MGEODE_LX || MGEODEGX1 || MK6 || MK7 || MK8 || MK8SSE3 || MK10 || MBARCELONA || MBOBCAT || MJAGUAR || MBULLDOZER || MPILEDRIVER || MSTEAMROLLER || MEXCAVATOR || MZEN || MZEN2 || MZEN3 || MNEHALEM || MWESTMERE || MSILVERMONT || MGOLDMONT || MGOLDMONTPLUS || MSANDYBRIDGE || MIVYBRIDGE || MHASWELL || MBROADWELL || MSKYLAKE || MSKYLAKEX || MCANNONLAKE || MICELAKE || MCASCADELAKE || MCOOPERLAKE || MTIGERLAKE || MSAPPHIRERAPIDS || MROCKETLAKE || MALDERLAKE || MNATIVE_INTEL || MNATIVE_AMD
+
+ # this should be set for all -march=.. options where the compiler
+ # generates cmov.
+ config X86_CMOV
+ def_bool y
+- depends on (MK8 || MK7 || MCORE2 || MPENTIUM4 || MPENTIUMM || MPENTIUMIII || MPENTIUMII || M686 || MVIAC3_2 || MVIAC7 || MCRUSOE || MEFFICEON || X86_64 || MATOM || MGEODE_LX)
++ depends on (MK8 || MK7 || MCORE2 || MPENTIUM4 || MPENTIUMM || MPENTIUMIII || MPENTIUMII || M686 || MVIAC3_2 || MVIAC7 || MCRUSOE || MEFFICEON || X86_64 || MATOM || MGEODE_LX || MK8SSE3 || MK10 || MBARCELONA || MBOBCAT || MJAGUAR || MBULLDOZER || MPILEDRIVER || MSTEAMROLLER || MEXCAVATOR || MZEN || MZEN2 || MZEN3 || MNEHALEM || MWESTMERE || MSILVERMONT || MGOLDMONT || MGOLDMONTPLUS || MSANDYBRIDGE || MIVYBRIDGE || MHASWELL || MBROADWELL || MSKYLAKE || MSKYLAKEX || MCANNONLAKE || MICELAKE || MCASCADELAKE || MCOOPERLAKE || MTIGERLAKE || MSAPPHIRERAPIDS || MROCKETLAKE || MALDERLAKE || MNATIVE_INTEL || MNATIVE_AMD)
+
+ config X86_MINIMUM_CPU_FAMILY
+ int
+ default "64" if X86_64
+- default "6" if X86_32 && (MPENTIUM4 || MPENTIUMM || MPENTIUMIII || MPENTIUMII || M686 || MVIAC3_2 || MVIAC7 || MEFFICEON || MATOM || MCRUSOE || MCORE2 || MK7 || MK8)
++ default "6" if X86_32 && (MPENTIUM4 || MPENTIUMM || MPENTIUMIII || MPENTIUMII || M686 || MVIAC3_2 || MVIAC7 || MEFFICEON || MATOM || MCRUSOE || MCORE2 || MK7 || MK8 || MK8SSE3 || MK10 || MBARCELONA || MBOBCAT || MJAGUAR || MBULLDOZER || MPILEDRIVER || MSTEAMROLLER || MEXCAVATOR || MZEN || MZEN2 || MZEN3 || MNEHALEM || MWESTMERE || MSILVERMONT || MGOLDMONT || MGOLDMONTPLUS || MSANDYBRIDGE || MIVYBRIDGE || MHASWELL || MBROADWELL || MSKYLAKE || MSKYLAKEX || MCANNONLAKE || MICELAKE || MCASCADELAKE || MCOOPERLAKE || MTIGERLAKE || MSAPPHIRERAPIDS || MROCKETLAKE || MALDERLAKE || MNATIVE_INTEL || MNATIVE_AMD)
+ default "5" if X86_32 && X86_CMPXCHG64
+ default "4"
+
+diff --git a/arch/x86/Makefile b/arch/x86/Makefile
+index 63d50f65b..8cd235d38 100644
+--- a/arch/x86/Makefile
++++ b/arch/x86/Makefile
+@@ -143,8 +143,44 @@ else
+ # FIXME - should be integrated in Makefile.cpu (Makefile_32.cpu)
+ cflags-$(CONFIG_MK8) += -march=k8
+ cflags-$(CONFIG_MPSC) += -march=nocona
+- cflags-$(CONFIG_MCORE2) += -march=core2
+- cflags-$(CONFIG_MATOM) += -march=atom
++ cflags-$(CONFIG_MK8SSE3) += -march=k8-sse3
++ cflags-$(CONFIG_MK10) += -march=amdfam10
++ cflags-$(CONFIG_MBARCELONA) += -march=barcelona
++ cflags-$(CONFIG_MBOBCAT) += -march=btver1
++ cflags-$(CONFIG_MJAGUAR) += -march=btver2
++ cflags-$(CONFIG_MBULLDOZER) += -march=bdver1
++ cflags-$(CONFIG_MPILEDRIVER) += -march=bdver2 -mno-tbm
++ cflags-$(CONFIG_MSTEAMROLLER) += -march=bdver3 -mno-tbm
++ cflags-$(CONFIG_MEXCAVATOR) += -march=bdver4 -mno-tbm
++ cflags-$(CONFIG_MZEN) += -march=znver1
++ cflags-$(CONFIG_MZEN2) += -march=znver2
++ cflags-$(CONFIG_MZEN3) += -march=znver3
++ cflags-$(CONFIG_MNATIVE_INTEL) += -march=native
++ cflags-$(CONFIG_MNATIVE_AMD) += -march=native
++ cflags-$(CONFIG_MATOM) += -march=bonnell
++ cflags-$(CONFIG_MCORE2) += -march=core2
++ cflags-$(CONFIG_MNEHALEM) += -march=nehalem
++ cflags-$(CONFIG_MWESTMERE) += -march=westmere
++ cflags-$(CONFIG_MSILVERMONT) += -march=silvermont
++ cflags-$(CONFIG_MGOLDMONT) += -march=goldmont
++ cflags-$(CONFIG_MGOLDMONTPLUS) += -march=goldmont-plus
++ cflags-$(CONFIG_MSANDYBRIDGE) += -march=sandybridge
++ cflags-$(CONFIG_MIVYBRIDGE) += -march=ivybridge
++ cflags-$(CONFIG_MHASWELL) += -march=haswell
++ cflags-$(CONFIG_MBROADWELL) += -march=broadwell
++ cflags-$(CONFIG_MSKYLAKE) += -march=skylake
++ cflags-$(CONFIG_MSKYLAKEX) += -march=skylake-avx512
++ cflags-$(CONFIG_MCANNONLAKE) += -march=cannonlake
++ cflags-$(CONFIG_MICELAKE) += -march=icelake-client
++ cflags-$(CONFIG_MCASCADELAKE) += -march=cascadelake
++ cflags-$(CONFIG_MCOOPERLAKE) += -march=cooperlake
++ cflags-$(CONFIG_MTIGERLAKE) += -march=tigerlake
++ cflags-$(CONFIG_MSAPPHIRERAPIDS) += -march=sapphirerapids
++ cflags-$(CONFIG_MROCKETLAKE) += -march=rocketlake
++ cflags-$(CONFIG_MALDERLAKE) += -march=alderlake
++ cflags-$(CONFIG_GENERIC_CPU2) += -march=x86-64-v2
++ cflags-$(CONFIG_GENERIC_CPU3) += -march=x86-64-v3
++ cflags-$(CONFIG_GENERIC_CPU4) += -march=x86-64-v4
+ cflags-$(CONFIG_GENERIC_CPU) += -mtune=generic
+ KBUILD_CFLAGS += $(cflags-y)
+
+diff --git a/arch/x86/entry/syscalls/syscall_32.tbl b/arch/x86/entry/syscalls/syscall_32.tbl
+index 320480a8d..331aaf1a7 100644
+--- a/arch/x86/entry/syscalls/syscall_32.tbl
++++ b/arch/x86/entry/syscalls/syscall_32.tbl
+@@ -455,3 +455,4 @@
+ 448 i386 process_mrelease sys_process_mrelease
+ 449 i386 futex_waitv sys_futex_waitv
+ 450 i386 set_mempolicy_home_node sys_set_mempolicy_home_node
++451 i386 pmadv_ksm sys_pmadv_ksm
+diff --git a/arch/x86/entry/syscalls/syscall_64.tbl b/arch/x86/entry/syscalls/syscall_64.tbl
+index c84d12608..14902db4c 100644
+--- a/arch/x86/entry/syscalls/syscall_64.tbl
++++ b/arch/x86/entry/syscalls/syscall_64.tbl
+@@ -372,6 +372,7 @@
+ 448 common process_mrelease sys_process_mrelease
+ 449 common futex_waitv sys_futex_waitv
+ 450 common set_mempolicy_home_node sys_set_mempolicy_home_node
++451 common pmadv_ksm sys_pmadv_ksm
+
+ #
+ # Due to a historical design error, certain syscalls are numbered differently
+diff --git a/arch/x86/include/asm/cpufeatures.h b/arch/x86/include/asm/cpufeatures.h
+index e17de69fa..dd7a27e09 100644
+--- a/arch/x86/include/asm/cpufeatures.h
++++ b/arch/x86/include/asm/cpufeatures.h
+@@ -219,7 +219,7 @@
+ #define X86_FEATURE_IBRS ( 7*32+25) /* Indirect Branch Restricted Speculation */
+ #define X86_FEATURE_IBPB ( 7*32+26) /* Indirect Branch Prediction Barrier */
+ #define X86_FEATURE_STIBP ( 7*32+27) /* Single Thread Indirect Branch Predictors */
+-#define X86_FEATURE_ZEN ( 7*32+28) /* "" CPU is AMD family 0x17 or above (Zen) */
++#define X86_FEATURE_ZEN (7*32+28) /* "" CPU based on Zen microarchitecture */
+ #define X86_FEATURE_L1TF_PTEINV ( 7*32+29) /* "" L1TF workaround PTE inversion */
+ #define X86_FEATURE_IBRS_ENHANCED ( 7*32+30) /* Enhanced IBRS */
+ #define X86_FEATURE_MSR_IA32_FEAT_CTL ( 7*32+31) /* "" MSR IA32_FEAT_CTL configured */
+diff --git a/arch/x86/include/asm/mwait.h b/arch/x86/include/asm/mwait.h
+index 29dd27b5a..3a8fdf881 100644
+--- a/arch/x86/include/asm/mwait.h
++++ b/arch/x86/include/asm/mwait.h
+@@ -13,6 +13,7 @@
+ #define MWAIT_SUBSTATE_SIZE 4
+ #define MWAIT_HINT2CSTATE(hint) (((hint) >> MWAIT_SUBSTATE_SIZE) & MWAIT_CSTATE_MASK)
+ #define MWAIT_HINT2SUBSTATE(hint) ((hint) & MWAIT_CSTATE_MASK)
++#define MWAIT_C1_SUBSTATE_MASK 0xf0
+
+ #define CPUID_MWAIT_LEAF 5
+ #define CPUID5_ECX_EXTENSIONS_SUPPORTED 0x1
+diff --git a/arch/x86/include/asm/vdso/processor.h b/arch/x86/include/asm/vdso/processor.h
+index 57b1a7034..e2c45674f 100644
+--- a/arch/x86/include/asm/vdso/processor.h
++++ b/arch/x86/include/asm/vdso/processor.h
+@@ -10,7 +10,7 @@
+ /* REP NOP (PAUSE) is a good thing to insert into busy-wait loops. */
+ static __always_inline void rep_nop(void)
+ {
+- asm volatile("rep; nop" ::: "memory");
++ asm volatile("lfence" ::: "memory");
+ }
+
+ static __always_inline void cpu_relax(void)
+diff --git a/arch/x86/include/asm/vermagic.h b/arch/x86/include/asm/vermagic.h
+index 75884d2cd..4e6a08d4c 100644
+--- a/arch/x86/include/asm/vermagic.h
++++ b/arch/x86/include/asm/vermagic.h
+@@ -17,6 +17,48 @@
+ #define MODULE_PROC_FAMILY "586MMX "
+ #elif defined CONFIG_MCORE2
+ #define MODULE_PROC_FAMILY "CORE2 "
++#elif defined CONFIG_MNATIVE_INTEL
++#define MODULE_PROC_FAMILY "NATIVE_INTEL "
++#elif defined CONFIG_MNATIVE_AMD
++#define MODULE_PROC_FAMILY "NATIVE_AMD "
++#elif defined CONFIG_MNEHALEM
++#define MODULE_PROC_FAMILY "NEHALEM "
++#elif defined CONFIG_MWESTMERE
++#define MODULE_PROC_FAMILY "WESTMERE "
++#elif defined CONFIG_MSILVERMONT
++#define MODULE_PROC_FAMILY "SILVERMONT "
++#elif defined CONFIG_MGOLDMONT
++#define MODULE_PROC_FAMILY "GOLDMONT "
++#elif defined CONFIG_MGOLDMONTPLUS
++#define MODULE_PROC_FAMILY "GOLDMONTPLUS "
++#elif defined CONFIG_MSANDYBRIDGE
++#define MODULE_PROC_FAMILY "SANDYBRIDGE "
++#elif defined CONFIG_MIVYBRIDGE
++#define MODULE_PROC_FAMILY "IVYBRIDGE "
++#elif defined CONFIG_MHASWELL
++#define MODULE_PROC_FAMILY "HASWELL "
++#elif defined CONFIG_MBROADWELL
++#define MODULE_PROC_FAMILY "BROADWELL "
++#elif defined CONFIG_MSKYLAKE
++#define MODULE_PROC_FAMILY "SKYLAKE "
++#elif defined CONFIG_MSKYLAKEX
++#define MODULE_PROC_FAMILY "SKYLAKEX "
++#elif defined CONFIG_MCANNONLAKE
++#define MODULE_PROC_FAMILY "CANNONLAKE "
++#elif defined CONFIG_MICELAKE
++#define MODULE_PROC_FAMILY "ICELAKE "
++#elif defined CONFIG_MCASCADELAKE
++#define MODULE_PROC_FAMILY "CASCADELAKE "
++#elif defined CONFIG_MCOOPERLAKE
++#define MODULE_PROC_FAMILY "COOPERLAKE "
++#elif defined CONFIG_MTIGERLAKE
++#define MODULE_PROC_FAMILY "TIGERLAKE "
++#elif defined CONFIG_MSAPPHIRERAPIDS
++#define MODULE_PROC_FAMILY "SAPPHIRERAPIDS "
++#elif defined CONFIG_ROCKETLAKE
++#define MODULE_PROC_FAMILY "ROCKETLAKE "
++#elif defined CONFIG_MALDERLAKE
++#define MODULE_PROC_FAMILY "ALDERLAKE "
+ #elif defined CONFIG_MATOM
+ #define MODULE_PROC_FAMILY "ATOM "
+ #elif defined CONFIG_M686
+@@ -35,6 +77,30 @@
+ #define MODULE_PROC_FAMILY "K7 "
+ #elif defined CONFIG_MK8
+ #define MODULE_PROC_FAMILY "K8 "
++#elif defined CONFIG_MK8SSE3
++#define MODULE_PROC_FAMILY "K8SSE3 "
++#elif defined CONFIG_MK10
++#define MODULE_PROC_FAMILY "K10 "
++#elif defined CONFIG_MBARCELONA
++#define MODULE_PROC_FAMILY "BARCELONA "
++#elif defined CONFIG_MBOBCAT
++#define MODULE_PROC_FAMILY "BOBCAT "
++#elif defined CONFIG_MBULLDOZER
++#define MODULE_PROC_FAMILY "BULLDOZER "
++#elif defined CONFIG_MPILEDRIVER
++#define MODULE_PROC_FAMILY "PILEDRIVER "
++#elif defined CONFIG_MSTEAMROLLER
++#define MODULE_PROC_FAMILY "STEAMROLLER "
++#elif defined CONFIG_MJAGUAR
++#define MODULE_PROC_FAMILY "JAGUAR "
++#elif defined CONFIG_MEXCAVATOR
++#define MODULE_PROC_FAMILY "EXCAVATOR "
++#elif defined CONFIG_MZEN
++#define MODULE_PROC_FAMILY "ZEN "
++#elif defined CONFIG_MZEN2
++#define MODULE_PROC_FAMILY "ZEN2 "
++#elif defined CONFIG_MZEN3
++#define MODULE_PROC_FAMILY "ZEN3 "
+ #elif defined CONFIG_MELAN
+ #define MODULE_PROC_FAMILY "ELAN "
+ #elif defined CONFIG_MCRUSOE
+diff --git a/arch/x86/kernel/cpu/amd.c b/arch/x86/kernel/cpu/amd.c
+index 0c0b09796..ff2075f26 100644
+--- a/arch/x86/kernel/cpu/amd.c
++++ b/arch/x86/kernel/cpu/amd.c
+@@ -1152,7 +1152,8 @@ u32 amd_get_highest_perf(void)
+ struct cpuinfo_x86 *c = &boot_cpu_data;
+
+ if (c->x86 == 0x17 && ((c->x86_model >= 0x30 && c->x86_model < 0x40) ||
+- (c->x86_model >= 0x70 && c->x86_model < 0x80)))
++ (c->x86_model >= 0x70 && c->x86_model < 0x80) ||
++ (c->x86_model >= 0xa0 && c->x86_model < 0xb0)))
+ return 166;
+
+ if (c->x86 == 0x19 && ((c->x86_model >= 0x20 && c->x86_model < 0x30) ||
+diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c
+index b370767f5..e26c63615 100644
+--- a/arch/x86/kernel/process.c
++++ b/arch/x86/kernel/process.c
+@@ -813,24 +813,43 @@ static void amd_e400_idle(void)
+ }
+
+ /*
+- * Intel Core2 and older machines prefer MWAIT over HALT for C1.
+- * We can't rely on cpuidle installing MWAIT, because it will not load
+- * on systems that support only C1 -- so the boot default must be MWAIT.
++ * Prefer MWAIT over HALT if MWAIT is supported, MWAIT_CPUID leaf
++ * exists and whenever MONITOR/MWAIT extensions are present there is at
++ * least one C1 substate.
+ *
+- * Some AMD machines are the opposite, they depend on using HALT.
+- *
+- * So for default C1, which is used during boot until cpuidle loads,
+- * use MWAIT-C1 on Intel HW that has it, else use HALT.
++ * Do not prefer MWAIT if MONITOR instruction has a bug or idle=nomwait
++ * is passed to kernel commandline parameter.
+ */
+ static int prefer_mwait_c1_over_halt(const struct cpuinfo_x86 *c)
+ {
+- if (c->x86_vendor != X86_VENDOR_INTEL)
++ u32 eax, ebx, ecx, edx;
++
++ /* User has disallowed the use of MWAIT. Fallback to HALT */
++ if (boot_option_idle_override == IDLE_NOMWAIT)
+ return 0;
+
+- if (!cpu_has(c, X86_FEATURE_MWAIT) || boot_cpu_has_bug(X86_BUG_MONITOR))
++ /* MWAIT is not supported on this platform. Fallback to HALT */
++ if (!cpu_has(c, X86_FEATURE_MWAIT))
+ return 0;
+
+- return 1;
++ /* Monitor has a bug. Fallback to HALT */
++ if (boot_cpu_has_bug(X86_BUG_MONITOR))
++ return 0;
++
++ cpuid(CPUID_MWAIT_LEAF, &eax, &ebx, &ecx, &edx);
++
++ /*
++ * If MWAIT extensions are not available, it is safe to use MWAIT
++ * with EAX=0, ECX=0.
++ */
++ if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED))
++ return 1;
++
++ /*
++ * If MWAIT extensions are available, there should be least one
++ * MWAIT C1 substate present.
++ */
++ return (edx & MWAIT_C1_SUBSTATE_MASK);
+ }
+
+ /*
+diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c
+index cafacb2e5..c2f80184f 100644
+--- a/arch/x86/kernel/tsc.c
++++ b/arch/x86/kernel/tsc.c
+@@ -1569,6 +1569,9 @@ unsigned long calibrate_delay_is_known(void)
+ if (!constant_tsc || !mask)
+ return 0;
+
++ if (cpu != 0)
++ return cpu_data(0).loops_per_jiffy;
++
+ sibling = cpumask_any_but(mask, cpu);
+ if (sibling < nr_cpu_ids)
+ return cpu_data(sibling).loops_per_jiffy;
+diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c
+index d0074c6ed..aeeae77fe 100644
+--- a/arch/x86/mm/fault.c
++++ b/arch/x86/mm/fault.c
+@@ -776,9 +776,9 @@ show_signal_msg(struct pt_regs *regs, unsigned long error_code,
+ if (!printk_ratelimit())
+ return;
+
+- printk("%s%s[%d]: segfault at %lx ip %px sp %px error %lx",
++ printk("%s%s[%d]: segfault at %lx ip %px sp %px error %lx cpu %i",
+ loglvl, tsk->comm, task_pid_nr(tsk), address,
+- (void *)regs->ip, (void *)regs->sp, error_code);
++ (void *)regs->ip, (void *)regs->sp, error_code, raw_smp_processor_id());
+
+ print_vma_addr(KERN_CONT " in ", regs->ip);
+
+diff --git a/arch/xtensa/kernel/syscalls/syscall.tbl b/arch/xtensa/kernel/syscalls/syscall.tbl
+index 52c94ab5c..1518e261d 100644
+--- a/arch/xtensa/kernel/syscalls/syscall.tbl
++++ b/arch/xtensa/kernel/syscalls/syscall.tbl
+@@ -421,3 +421,4 @@
+ 448 common process_mrelease sys_process_mrelease
+ 449 common futex_waitv sys_futex_waitv
+ 450 common set_mempolicy_home_node sys_set_mempolicy_home_node
++451 common pmadv_ksm sys_pmadv_ksm
+diff --git a/block/blk-core.c b/block/blk-core.c
+index a7329475a..e5f4471b6 100644
+--- a/block/blk-core.c
++++ b/block/blk-core.c
+@@ -889,6 +889,9 @@ void submit_bio(struct bio *bio)
+ task_io_account_read(bio->bi_iter.bi_size);
+ count_vm_events(PGPGIN, count);
+ }
++
++ if (bio->bi_opf & REQ_PREFLUSH)
++ current->fsync_count++;
+ }
+
+ /*
+diff --git a/drivers/Makefile b/drivers/Makefile
+index 020780b6b..9cf43ac81 100644
+--- a/drivers/Makefile
++++ b/drivers/Makefile
+@@ -59,15 +59,8 @@ obj-y += char/
+ # iommu/ comes before gpu as gpu are using iommu controllers
+ obj-y += iommu/
+
+-# gpu/ comes after char for AGP vs DRM startup and after iommu
+-obj-y += gpu/
+-
+ obj-$(CONFIG_CONNECTOR) += connector/
+
+-# i810fb and intelfb depend on char/agp/
+-obj-$(CONFIG_FB_I810) += video/fbdev/i810/
+-obj-$(CONFIG_FB_INTEL) += video/fbdev/intelfb/
+-
+ obj-$(CONFIG_PARPORT) += parport/
+ obj-y += base/ block/ misc/ mfd/ nfc/
+ obj-$(CONFIG_LIBNVDIMM) += nvdimm/
+@@ -79,6 +72,14 @@ obj-y += macintosh/
+ obj-y += scsi/
+ obj-y += nvme/
+ obj-$(CONFIG_ATA) += ata/
++
++# gpu/ comes after char for AGP vs DRM startup and after iommu
++obj-y += gpu/
++
++# i810fb and intelfb depend on char/agp/
++obj-$(CONFIG_FB_I810) += video/fbdev/i810/
++obj-$(CONFIG_FB_INTEL) += video/fbdev/intelfb/
++
+ obj-$(CONFIG_TARGET_CORE) += target/
+ obj-$(CONFIG_MTD) += mtd/
+ obj-$(CONFIG_SPI) += spi/
+diff --git a/drivers/ata/libahci.c b/drivers/ata/libahci.c
+index cf8c7fd59..ad9bb5353 100644
+--- a/drivers/ata/libahci.c
++++ b/drivers/ata/libahci.c
+@@ -33,14 +33,14 @@
+ #include "libata.h"
+
+ static int ahci_skip_host_reset;
+-int ahci_ignore_sss;
++int ahci_ignore_sss=1;
+ EXPORT_SYMBOL_GPL(ahci_ignore_sss);
+
+ module_param_named(skip_host_reset, ahci_skip_host_reset, int, 0444);
+ MODULE_PARM_DESC(skip_host_reset, "skip global host reset (0=don't skip, 1=skip)");
+
+ module_param_named(ignore_sss, ahci_ignore_sss, int, 0444);
+-MODULE_PARM_DESC(ignore_sss, "Ignore staggered spinup flag (0=don't ignore, 1=ignore)");
++MODULE_PARM_DESC(ignore_sss, "Ignore staggered spinup flag (0=don't ignore, 1=ignore [default])");
+
+ static int ahci_set_lpm(struct ata_link *link, enum ata_lpm_policy policy,
+ unsigned hints);
+diff --git a/drivers/block/zram/Kconfig b/drivers/block/zram/Kconfig
+index 668c6bf25..d873d631a 100644
+--- a/drivers/block/zram/Kconfig
++++ b/drivers/block/zram/Kconfig
+@@ -77,3 +77,21 @@ config ZRAM_MEMORY_TRACKING
+ /sys/kernel/debug/zram/zramX/block_state.
+
+ See Documentation/admin-guide/blockdev/zram.rst for more information.
++
++config ZRAM_ENTROPY
++ bool "Use entropy optimization for zram"
++ depends on ZRAM && ZRAM_DEF_COMP_ZSTD
++ help
++ With this feature, entropy will be calculated for each page.
++ Pages above ZRAM_ENTROPY_THRESHOLD entropy will be
++ stored uncompressed. Use this feature if you need a performance
++ boost and a small loss in compression.
++
++config ZRAM_ENTROPY_THRESHOLD
++ int
++ depends on ZRAM && ZRAM_ENTROPY
++ default 100000 if ZRAM_DEF_COMP_ZSTD
++ help
++ Pages with entropy above ZRAM_ENTROPY_THRESHOLD will be stored
++ uncompressed. The default value was chosen as a result a lot of
++ experiments. You can try set your own value.
+diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
+index e9474b020..c1369462f 100644
+--- a/drivers/block/zram/zram_drv.c
++++ b/drivers/block/zram/zram_drv.c
+@@ -1343,6 +1343,35 @@ static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,
+ return ret;
+ }
+
++
++#ifdef CONFIG_ZRAM_ENTROPY
++static inline u32 ilog2_w(u64 n)
++{
++ return ilog2(n * n * n * n);
++}
++
++static inline s32 shannon_entropy(const u8 *src)
++{
++ s32 entropy_sum = 0;
++ u32 sz_base, i;
++ u16 entropy_count[256] = { 0 };
++
++ for (i = 0; i < PAGE_SIZE; ++i)
++ entropy_count[src[i]]++;
++
++ sz_base = ilog2_w(PAGE_SIZE);
++ for (i = 0; i < ARRAY_SIZE(entropy_count); ++i) {
++ if (entropy_count[i] > 0) {
++ s32 p = entropy_count[i];
++
++ entropy_sum += p * (sz_base - ilog2_w((u64)p));
++ }
++ }
++
++ return entropy_sum;
++}
++#endif
++
+ static int __zram_bvec_write(struct zram *zram, struct bio_vec *bvec,
+ u32 index, struct bio *bio)
+ {
+@@ -1369,7 +1398,17 @@ static int __zram_bvec_write(struct zram *zram, struct bio_vec *bvec,
+ compress_again:
+ zstrm = zcomp_stream_get(zram->comp);
+ src = kmap_atomic(page);
++
++#ifdef CONFIG_ZRAM_ENTROPY
++ /* Just save this page uncompressible */
++ if (shannon_entropy((const u8 *)src) > CONFIG_ZRAM_ENTROPY_THRESHOLD)
++ comp_len = PAGE_SIZE;
++ else
++ ret = zcomp_compress(zstrm, src, &comp_len);
++#else
+ ret = zcomp_compress(zstrm, src, &comp_len);
++#endif
++
+ kunmap_atomic(src);
+
+ if (unlikely(ret)) {
+diff --git a/drivers/cpufreq/Kconfig b/drivers/cpufreq/Kconfig
+index c3038cdc6..25bf88b57 100644
+--- a/drivers/cpufreq/Kconfig
++++ b/drivers/cpufreq/Kconfig
+@@ -72,7 +72,7 @@ config CPU_FREQ_DEFAULT_GOV_USERSPACE
+
+ config CPU_FREQ_DEFAULT_GOV_ONDEMAND
+ bool "ondemand"
+- depends on !(X86_INTEL_PSTATE && SMP)
++ depends on SMP
+ select CPU_FREQ_GOV_ONDEMAND
+ select CPU_FREQ_GOV_PERFORMANCE
+ help
+diff --git a/drivers/cpufreq/cpufreq_ondemand.c b/drivers/cpufreq/cpufreq_ondemand.c
+index e8fbf970f..416e20977 100644
+--- a/drivers/cpufreq/cpufreq_ondemand.c
++++ b/drivers/cpufreq/cpufreq_ondemand.c
+@@ -18,10 +18,10 @@
+ #include "cpufreq_ondemand.h"
+
+ /* On-demand governor macros */
+-#define DEF_FREQUENCY_UP_THRESHOLD (80)
+-#define DEF_SAMPLING_DOWN_FACTOR (1)
++#define DEF_FREQUENCY_UP_THRESHOLD (65)
++#define DEF_SAMPLING_DOWN_FACTOR (10)
+ #define MAX_SAMPLING_DOWN_FACTOR (100000)
+-#define MICRO_FREQUENCY_UP_THRESHOLD (95)
++#define MICRO_FREQUENCY_UP_THRESHOLD (85)
+ #define MICRO_FREQUENCY_MIN_SAMPLE_RATE (10000)
+ #define MIN_FREQUENCY_UP_THRESHOLD (1)
+ #define MAX_FREQUENCY_UP_THRESHOLD (100)
+diff --git a/drivers/firmware/sysfb.c b/drivers/firmware/sysfb.c
+index 2bfbb05f7..a504f7234 100644
+--- a/drivers/firmware/sysfb.c
++++ b/drivers/firmware/sysfb.c
+@@ -34,6 +34,22 @@
+ #include <linux/screen_info.h>
+ #include <linux/sysfb.h>
+
++static int skip_simpledrm;
++
++static int __init simpledrm_disable(char *opt)
++{
++ if (!opt)
++ return -EINVAL;
++
++ get_option(&opt, &skip_simpledrm);
++
++ if (skip_simpledrm)
++ pr_info("The simpledrm driver will not be probed\n");
++
++ return 0;
++}
++early_param("nvidia-drm.modeset", simpledrm_disable);
++
+ static __init int sysfb_init(void)
+ {
+ struct screen_info *si = &screen_info;
+@@ -45,7 +61,7 @@ static __init int sysfb_init(void)
+
+ /* try to create a simple-framebuffer device */
+ compatible = sysfb_parse_mode(si, &mode);
+- if (compatible) {
++ if (compatible && !skip_simpledrm) {
+ ret = sysfb_create_simplefb(si, &mode);
+ if (!ret)
+ return 0;
+diff --git a/drivers/hid/usbhid/hid-core.c b/drivers/hid/usbhid/hid-core.c
+index 54752c856..6fa8de3ce 100644
+--- a/drivers/hid/usbhid/hid-core.c
++++ b/drivers/hid/usbhid/hid-core.c
+@@ -45,6 +45,10 @@
+ * Module parameters.
+ */
+
++static unsigned int hid_poll_interval;
++module_param_named(poll, hid_poll_interval, uint, 0644);
++MODULE_PARM_DESC(poll, "Polling interval of any device");
++
+ static unsigned int hid_mousepoll_interval;
+ module_param_named(mousepoll, hid_mousepoll_interval, uint, 0644);
+ MODULE_PARM_DESC(mousepoll, "Polling interval of mice");
+@@ -1107,6 +1111,10 @@ static int usbhid_start(struct hid_device *hid)
+ hid->name, endpoint->bInterval, interval);
+ }
+
++ /* First set polling interval for all devices */
++ if (hid_poll_interval > 0)
++ interval = hid_poll_interval;
++
+ /* Change the polling interval of mice, joysticks
+ * and keyboards.
+ */
+diff --git a/drivers/idle/intel_idle.c b/drivers/idle/intel_idle.c
+index c5a019eab..ed48d6c20 100644
+--- a/drivers/idle/intel_idle.c
++++ b/drivers/idle/intel_idle.c
+@@ -529,7 +529,7 @@ static struct cpuidle_state hsw_cstates[] __initdata = {
+ .desc = "MWAIT 0x01",
+ .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_ALWAYS_ENABLE,
+ .exit_latency = 10,
+- .target_residency = 20,
++ .target_residency = 120,
+ .enter = &intel_idle,
+ .enter_s2idle = intel_idle_s2idle, },
+ {
+@@ -537,7 +537,7 @@ static struct cpuidle_state hsw_cstates[] __initdata = {
+ .desc = "MWAIT 0x10",
+ .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
+ .exit_latency = 33,
+- .target_residency = 100,
++ .target_residency = 900,
+ .enter = &intel_idle,
+ .enter_s2idle = intel_idle_s2idle, },
+ {
+@@ -545,7 +545,7 @@ static struct cpuidle_state hsw_cstates[] __initdata = {
+ .desc = "MWAIT 0x20",
+ .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
+ .exit_latency = 133,
+- .target_residency = 400,
++ .target_residency = 1000,
+ .enter = &intel_idle,
+ .enter_s2idle = intel_idle_s2idle, },
+ {
+@@ -553,7 +553,7 @@ static struct cpuidle_state hsw_cstates[] __initdata = {
+ .desc = "MWAIT 0x32",
+ .flags = MWAIT2flg(0x32) | CPUIDLE_FLAG_TLB_FLUSHED,
+ .exit_latency = 166,
+- .target_residency = 500,
++ .target_residency = 1500,
+ .enter = &intel_idle,
+ .enter_s2idle = intel_idle_s2idle, },
+ {
+@@ -561,7 +561,7 @@ static struct cpuidle_state hsw_cstates[] __initdata = {
+ .desc = "MWAIT 0x40",
+ .flags = MWAIT2flg(0x40) | CPUIDLE_FLAG_TLB_FLUSHED,
+ .exit_latency = 300,
+- .target_residency = 900,
++ .target_residency = 2000,
+ .enter = &intel_idle,
+ .enter_s2idle = intel_idle_s2idle, },
+ {
+@@ -569,7 +569,7 @@ static struct cpuidle_state hsw_cstates[] __initdata = {
+ .desc = "MWAIT 0x50",
+ .flags = MWAIT2flg(0x50) | CPUIDLE_FLAG_TLB_FLUSHED,
+ .exit_latency = 600,
+- .target_residency = 1800,
++ .target_residency = 5000,
+ .enter = &intel_idle,
+ .enter_s2idle = intel_idle_s2idle, },
+ {
+@@ -577,7 +577,7 @@ static struct cpuidle_state hsw_cstates[] __initdata = {
+ .desc = "MWAIT 0x60",
+ .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
+ .exit_latency = 2600,
+- .target_residency = 7700,
++ .target_residency = 9000,
+ .enter = &intel_idle,
+ .enter_s2idle = intel_idle_s2idle, },
+ {
+@@ -597,7 +597,7 @@ static struct cpuidle_state bdw_cstates[] __initdata = {
+ .desc = "MWAIT 0x01",
+ .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_ALWAYS_ENABLE,
+ .exit_latency = 10,
+- .target_residency = 20,
++ .target_residency = 120,
+ .enter = &intel_idle,
+ .enter_s2idle = intel_idle_s2idle, },
+ {
+@@ -605,7 +605,7 @@ static struct cpuidle_state bdw_cstates[] __initdata = {
+ .desc = "MWAIT 0x10",
+ .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
+ .exit_latency = 40,
+- .target_residency = 100,
++ .target_residency = 1000,
+ .enter = &intel_idle,
+ .enter_s2idle = intel_idle_s2idle, },
+ {
+@@ -613,7 +613,7 @@ static struct cpuidle_state bdw_cstates[] __initdata = {
+ .desc = "MWAIT 0x20",
+ .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
+ .exit_latency = 133,
+- .target_residency = 400,
++ .target_residency = 1000,
+ .enter = &intel_idle,
+ .enter_s2idle = intel_idle_s2idle, },
+ {
+@@ -621,7 +621,7 @@ static struct cpuidle_state bdw_cstates[] __initdata = {
+ .desc = "MWAIT 0x32",
+ .flags = MWAIT2flg(0x32) | CPUIDLE_FLAG_TLB_FLUSHED,
+ .exit_latency = 166,
+- .target_residency = 500,
++ .target_residency = 2000,
+ .enter = &intel_idle,
+ .enter_s2idle = intel_idle_s2idle, },
+ {
+@@ -629,7 +629,7 @@ static struct cpuidle_state bdw_cstates[] __initdata = {
+ .desc = "MWAIT 0x40",
+ .flags = MWAIT2flg(0x40) | CPUIDLE_FLAG_TLB_FLUSHED,
+ .exit_latency = 300,
+- .target_residency = 900,
++ .target_residency = 4000,
+ .enter = &intel_idle,
+ .enter_s2idle = intel_idle_s2idle, },
+ {
+@@ -637,7 +637,7 @@ static struct cpuidle_state bdw_cstates[] __initdata = {
+ .desc = "MWAIT 0x50",
+ .flags = MWAIT2flg(0x50) | CPUIDLE_FLAG_TLB_FLUSHED,
+ .exit_latency = 600,
+- .target_residency = 1800,
++ .target_residency = 7000,
+ .enter = &intel_idle,
+ .enter_s2idle = intel_idle_s2idle, },
+ {
+@@ -645,7 +645,7 @@ static struct cpuidle_state bdw_cstates[] __initdata = {
+ .desc = "MWAIT 0x60",
+ .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
+ .exit_latency = 2600,
+- .target_residency = 7700,
++ .target_residency = 9000,
+ .enter = &intel_idle,
+ .enter_s2idle = intel_idle_s2idle, },
+ {
+@@ -666,7 +666,7 @@ static struct cpuidle_state skl_cstates[] __initdata = {
+ .desc = "MWAIT 0x01",
+ .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_ALWAYS_ENABLE,
+ .exit_latency = 10,
+- .target_residency = 20,
++ .target_residency = 120,
+ .enter = &intel_idle,
+ .enter_s2idle = intel_idle_s2idle, },
+ {
+@@ -674,7 +674,7 @@ static struct cpuidle_state skl_cstates[] __initdata = {
+ .desc = "MWAIT 0x10",
+ .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
+ .exit_latency = 70,
+- .target_residency = 100,
++ .target_residency = 1000,
+ .enter = &intel_idle,
+ .enter_s2idle = intel_idle_s2idle, },
+ {
+@@ -682,7 +682,7 @@ static struct cpuidle_state skl_cstates[] __initdata = {
+ .desc = "MWAIT 0x20",
+ .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
+ .exit_latency = 85,
+- .target_residency = 200,
++ .target_residency = 600,
+ .enter = &intel_idle,
+ .enter_s2idle = intel_idle_s2idle, },
+ {
+@@ -690,7 +690,7 @@ static struct cpuidle_state skl_cstates[] __initdata = {
+ .desc = "MWAIT 0x33",
+ .flags = MWAIT2flg(0x33) | CPUIDLE_FLAG_TLB_FLUSHED,
+ .exit_latency = 124,
+- .target_residency = 800,
++ .target_residency = 3000,
+ .enter = &intel_idle,
+ .enter_s2idle = intel_idle_s2idle, },
+ {
+@@ -698,7 +698,7 @@ static struct cpuidle_state skl_cstates[] __initdata = {
+ .desc = "MWAIT 0x40",
+ .flags = MWAIT2flg(0x40) | CPUIDLE_FLAG_TLB_FLUSHED,
+ .exit_latency = 200,
+- .target_residency = 800,
++ .target_residency = 3200,
+ .enter = &intel_idle,
+ .enter_s2idle = intel_idle_s2idle, },
+ {
+@@ -706,7 +706,7 @@ static struct cpuidle_state skl_cstates[] __initdata = {
+ .desc = "MWAIT 0x50",
+ .flags = MWAIT2flg(0x50) | CPUIDLE_FLAG_TLB_FLUSHED,
+ .exit_latency = 480,
+- .target_residency = 5000,
++ .target_residency = 9000,
+ .enter = &intel_idle,
+ .enter_s2idle = intel_idle_s2idle, },
+ {
+@@ -714,7 +714,7 @@ static struct cpuidle_state skl_cstates[] __initdata = {
+ .desc = "MWAIT 0x60",
+ .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
+ .exit_latency = 890,
+- .target_residency = 5000,
++ .target_residency = 9000,
+ .enter = &intel_idle,
+ .enter_s2idle = intel_idle_s2idle, },
+ {
+@@ -735,7 +735,7 @@ static struct cpuidle_state skx_cstates[] __initdata = {
+ .desc = "MWAIT 0x01",
+ .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_ALWAYS_ENABLE,
+ .exit_latency = 10,
+- .target_residency = 20,
++ .target_residency = 300,
+ .enter = &intel_idle,
+ .enter_s2idle = intel_idle_s2idle, },
+ {
+diff --git a/drivers/input/evdev.c b/drivers/input/evdev.c
+index 95f90699d..2b10fe29d 100644
+--- a/drivers/input/evdev.c
++++ b/drivers/input/evdev.c
+@@ -46,6 +46,7 @@ struct evdev_client {
+ struct fasync_struct *fasync;
+ struct evdev *evdev;
+ struct list_head node;
++ struct rcu_head rcu;
+ enum input_clock_type clk_type;
+ bool revoked;
+ unsigned long *evmasks[EV_CNT];
+@@ -377,13 +378,22 @@ static void evdev_attach_client(struct evdev *evdev,
+ spin_unlock(&evdev->client_lock);
+ }
+
++static void evdev_reclaim_client(struct rcu_head *rp)
++{
++ struct evdev_client *client = container_of(rp, struct evdev_client, rcu);
++ unsigned int i;
++ for (i = 0; i < EV_CNT; ++i)
++ bitmap_free(client->evmasks[i]);
++ kvfree(client);
++}
++
+ static void evdev_detach_client(struct evdev *evdev,
+ struct evdev_client *client)
+ {
+ spin_lock(&evdev->client_lock);
+ list_del_rcu(&client->node);
+ spin_unlock(&evdev->client_lock);
+- synchronize_rcu();
++ call_rcu(&client->rcu, evdev_reclaim_client);
+ }
+
+ static int evdev_open_device(struct evdev *evdev)
+@@ -436,7 +446,6 @@ static int evdev_release(struct inode *inode, struct file *file)
+ {
+ struct evdev_client *client = file->private_data;
+ struct evdev *evdev = client->evdev;
+- unsigned int i;
+
+ mutex_lock(&evdev->mutex);
+
+@@ -448,11 +457,6 @@ static int evdev_release(struct inode *inode, struct file *file)
+
+ evdev_detach_client(evdev, client);
+
+- for (i = 0; i < EV_CNT; ++i)
+- bitmap_free(client->evmasks[i]);
+-
+- kvfree(client);
+-
+ evdev_close_device(evdev);
+
+ return 0;
+@@ -495,7 +499,6 @@ static int evdev_open(struct inode *inode, struct file *file)
+
+ err_free_client:
+ evdev_detach_client(evdev, client);
+- kvfree(client);
+ return error;
+ }
+
+diff --git a/drivers/input/serio/i8042.c b/drivers/input/serio/i8042.c
+index 3fc0a89cc..a7c103f9d 100644
+--- a/drivers/input/serio/i8042.c
++++ b/drivers/input/serio/i8042.c
+@@ -621,7 +621,7 @@ static int i8042_enable_kbd_port(void)
+ if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
+ i8042_ctr &= ~I8042_CTR_KBDINT;
+ i8042_ctr |= I8042_CTR_KBDDIS;
+- pr_err("Failed to enable KBD port\n");
++ pr_info("Failed to enable KBD port\n");
+ return -EIO;
+ }
+
+@@ -640,7 +640,7 @@ static int i8042_enable_aux_port(void)
+ if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
+ i8042_ctr &= ~I8042_CTR_AUXINT;
+ i8042_ctr |= I8042_CTR_AUXDIS;
+- pr_err("Failed to enable AUX port\n");
++ pr_info("Failed to enable AUX port\n");
+ return -EIO;
+ }
+
+@@ -732,7 +732,7 @@ static int i8042_check_mux(void)
+ i8042_ctr &= ~I8042_CTR_AUXINT;
+
+ if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
+- pr_err("Failed to disable AUX port, can't use MUX\n");
++ pr_info("Failed to disable AUX port, can't use MUX\n");
+ return -EIO;
+ }
+
+@@ -955,7 +955,7 @@ static int i8042_controller_selftest(void)
+ do {
+
+ if (i8042_command(&param, I8042_CMD_CTL_TEST)) {
+- pr_err("i8042 controller selftest timeout\n");
++ pr_info("i8042 controller selftest timeout\n");
+ return -ENODEV;
+ }
+
+@@ -977,7 +977,7 @@ static int i8042_controller_selftest(void)
+ pr_info("giving up on controller selftest, continuing anyway...\n");
+ return 0;
+ #else
+- pr_err("i8042 controller selftest failed\n");
++ pr_info("i8042 controller selftest failed\n");
+ return -EIO;
+ #endif
+ }
+diff --git a/drivers/md/dm-crypt.c b/drivers/md/dm-crypt.c
+index 159c6806c..a201f1c2f 100644
+--- a/drivers/md/dm-crypt.c
++++ b/drivers/md/dm-crypt.c
+@@ -3137,6 +3137,11 @@ static int crypt_ctr_optional(struct dm_target *ti, unsigned int argc, char **ar
+ }
+ }
+
++#ifdef CONFIG_CACHY
++ set_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags);
++ set_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags);
++#endif
++
+ return 0;
+ }
+
+diff --git a/drivers/net/dummy.c b/drivers/net/dummy.c
+index f82ad7419..5e8faa70a 100644
+--- a/drivers/net/dummy.c
++++ b/drivers/net/dummy.c
+@@ -43,7 +43,7 @@
+
+ #define DRV_NAME "dummy"
+
+-static int numdummies = 1;
++static int numdummies = 0;
+
+ /* fake multicast ability */
+ static void set_multicast_list(struct net_device *dev)
+diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c
+index a2862a56f..2b5c9bcd1 100644
+--- a/drivers/nvme/host/core.c
++++ b/drivers/nvme/host/core.c
+@@ -48,7 +48,7 @@ static u8 nvme_max_retries = 5;
+ module_param_named(max_retries, nvme_max_retries, byte, 0644);
+ MODULE_PARM_DESC(max_retries, "max number of retries a command may have");
+
+-static unsigned long default_ps_max_latency_us = 100000;
++static unsigned long default_ps_max_latency_us = 200;
+ module_param(default_ps_max_latency_us, ulong, 0644);
+ MODULE_PARM_DESC(default_ps_max_latency_us,
+ "max power saving latency for new devices; use PM QOS to change per device");
+diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
+index 8ac110d6c..a5031c976 100644
+--- a/drivers/pci/pci.c
++++ b/drivers/pci/pci.c
+@@ -60,7 +60,7 @@ struct pci_pme_device {
+ struct pci_dev *dev;
+ };
+
+-#define PME_TIMEOUT 1000 /* How long between PME checks */
++#define PME_TIMEOUT 4000 /* How long between PME checks */
+
+ static void pci_dev_d3_sleep(struct pci_dev *dev)
+ {
+diff --git a/drivers/powercap/intel_rapl_common.c b/drivers/powercap/intel_rapl_common.c
+index 07611a00b..4031d810d 100644
+--- a/drivers/powercap/intel_rapl_common.c
++++ b/drivers/powercap/intel_rapl_common.c
+@@ -1512,7 +1512,7 @@ static int __init rapl_init(void)
+
+ id = x86_match_cpu(rapl_ids);
+ if (!id) {
+- pr_err("driver does not support CPU family %d model %d\n",
++ pr_info("driver does not support CPU family %d model %d\n",
+ boot_cpu_data.x86, boot_cpu_data.x86_model);
+
+ return -ENODEV;
+diff --git a/drivers/thermal/intel/intel_powerclamp.c b/drivers/thermal/intel/intel_powerclamp.c
+index c841ab37e..fe3d190ab 100644
+--- a/drivers/thermal/intel/intel_powerclamp.c
++++ b/drivers/thermal/intel/intel_powerclamp.c
+@@ -644,6 +644,11 @@ static const struct thermal_cooling_device_ops powerclamp_cooling_ops = {
+ .set_cur_state = powerclamp_set_cur_state,
+ };
+
++static const struct x86_cpu_id amd_cpu[] = {
++ { X86_VENDOR_AMD },
++ {},
++};
++
+ static const struct x86_cpu_id __initconst intel_powerclamp_ids[] = {
+ X86_MATCH_VENDOR_FEATURE(INTEL, X86_FEATURE_MWAIT, NULL),
+ {}
+@@ -653,6 +658,11 @@ MODULE_DEVICE_TABLE(x86cpu, intel_powerclamp_ids);
+ static int __init powerclamp_probe(void)
+ {
+
++ if (x86_match_cpu(amd_cpu)){
++ pr_info("Intel PowerClamp does not support AMD CPUs\n");
++ return -ENODEV;
++ }
++
+ if (!x86_match_cpu(intel_powerclamp_ids)) {
+ pr_err("CPU does not support MWAIT\n");
+ return -ENODEV;
+diff --git a/fs/xattr.c b/fs/xattr.c
+index 998045165..62b6fb4de 100644
+--- a/fs/xattr.c
++++ b/fs/xattr.c
+@@ -120,16 +120,17 @@ xattr_permission(struct user_namespace *mnt_userns, struct inode *inode,
+ }
+
+ /*
+- * In the user.* namespace, only regular files and directories can have
+- * extended attributes. For sticky directories, only the owner and
+- * privileged users can write attributes.
++ * In the user.* namespace, only regular files, symbolic links, and
++ * directories can have extended attributes. For symbolic links and
++ * sticky directories, only the owner and privileged users can write
++ * attributes.
+ */
+ if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) {
+- if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
++ if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode) && !S_ISLNK(inode->i_mode))
+ return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
+- if (S_ISDIR(inode->i_mode) && (inode->i_mode & S_ISVTX) &&
+- (mask & MAY_WRITE) &&
+- !inode_owner_or_capable(mnt_userns, inode))
++ if (((S_ISDIR(inode->i_mode) && (inode->i_mode & S_ISVTX))
++ || S_ISLNK(inode->i_mode)) && (mask & MAY_WRITE)
++ && !inode_owner_or_capable(mnt_userns, inode))
+ return -EPERM;
+ }
+
+diff --git a/include/linux/ipc_namespace.h b/include/linux/ipc_namespace.h
+index b75395ec8..c6f3336a6 100644
+--- a/include/linux/ipc_namespace.h
++++ b/include/linux/ipc_namespace.h
+@@ -35,8 +35,6 @@ struct ipc_namespace {
+ unsigned int msg_ctlmax;
+ unsigned int msg_ctlmnb;
+ unsigned int msg_ctlmni;
+- atomic_t msg_bytes;
+- atomic_t msg_hdrs;
+
+ size_t shm_ctlmax;
+ size_t shm_ctlall;
+@@ -70,6 +68,9 @@ struct ipc_namespace {
+ struct llist_node mnt_llist;
+
+ struct ns_common ns;
++ int padding[16];
++ atomic_t msg_bytes;
++ atomic_t msg_hdrs;
+ } __randomize_layout;
+
+ extern struct ipc_namespace init_ipc_ns;
+diff --git a/include/linux/jbd2.h b/include/linux/jbd2.h
+index de9536680..01213b822 100644
+--- a/include/linux/jbd2.h
++++ b/include/linux/jbd2.h
+@@ -45,7 +45,7 @@
+ /*
+ * The default maximum commit age, in seconds.
+ */
+-#define JBD2_DEFAULT_MAX_COMMIT_AGE 5
++#define JBD2_DEFAULT_MAX_COMMIT_AGE 30
+
+ #ifdef CONFIG_JBD2_DEBUG
+ /*
+diff --git a/include/linux/ksm.h b/include/linux/ksm.h
+index 0630e545f..a3234efb5 100644
+--- a/include/linux/ksm.h
++++ b/include/linux/ksm.h
+@@ -19,6 +19,10 @@ struct stable_node;
+ struct mem_cgroup;
+
+ #ifdef CONFIG_KSM
++int ksm_madvise_merge(struct mm_struct *mm, struct vm_area_struct *vma,
++ unsigned long *vm_flags);
++int ksm_madvise_unmerge(struct vm_area_struct *vma, unsigned long start,
++ unsigned long end, unsigned long *vm_flags);
+ int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
+ unsigned long end, int advice, unsigned long *vm_flags);
+ int __ksm_enter(struct mm_struct *mm);
+diff --git a/include/linux/page_counter.h b/include/linux/page_counter.h
+index 679591301..685193470 100644
+--- a/include/linux/page_counter.h
++++ b/include/linux/page_counter.h
+@@ -8,6 +8,7 @@
+
+ struct page_counter {
+ atomic_long_t usage;
++ unsigned long padding[7];
+ unsigned long min;
+ unsigned long low;
+ unsigned long high;
+diff --git a/include/linux/pageblock-flags.h b/include/linux/pageblock-flags.h
+index 83c724805..d2b8741ea 100644
+--- a/include/linux/pageblock-flags.h
++++ b/include/linux/pageblock-flags.h
+@@ -48,7 +48,7 @@ extern unsigned int pageblock_order;
+ #else /* CONFIG_HUGETLB_PAGE */
+
+ /* If huge pages are not used, group by MAX_ORDER_NR_PAGES */
+-#define pageblock_order (MAX_ORDER-1)
++#define pageblock_order PAGE_ALLOC_COSTLY_ORDER
+
+ #endif /* CONFIG_HUGETLB_PAGE */
+
+diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h
+index 993994cd9..4bf55082d 100644
+--- a/include/linux/pagemap.h
++++ b/include/linux/pagemap.h
+@@ -1153,7 +1153,7 @@ struct readahead_control {
+ ._index = i, \
+ }
+
+-#define VM_READAHEAD_PAGES (SZ_128K / PAGE_SIZE)
++#define VM_READAHEAD_PAGES (SZ_8M / PAGE_SIZE)
+
+ void page_cache_ra_unbounded(struct readahead_control *,
+ unsigned long nr_to_read, unsigned long lookahead_count);
+diff --git a/include/linux/sched.h b/include/linux/sched.h
+index a8911b1f3..62d9acb62 100644
+--- a/include/linux/sched.h
++++ b/include/linux/sched.h
+@@ -541,6 +541,7 @@ struct sched_entity {
+ struct rb_node run_node;
+ struct list_head group_node;
+ unsigned int on_rq;
++ unsigned int migrated;
+
+ u64 exec_start;
+ u64 sum_exec_runtime;
+@@ -1049,6 +1050,7 @@ struct task_struct {
+ /* Cached requested key. */
+ struct key *cached_requested_key;
+ #endif
++ int fsync_count;
+
+ /*
+ * executable name, excluding path.
+diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h
+index a34b0f9a9..82afad91d 100644
+--- a/include/linux/syscalls.h
++++ b/include/linux/syscalls.h
+@@ -917,6 +917,7 @@ asmlinkage long sys_madvise(unsigned long start, size_t len, int behavior);
+ asmlinkage long sys_process_madvise(int pidfd, const struct iovec __user *vec,
+ size_t vlen, int behavior, unsigned int flags);
+ asmlinkage long sys_process_mrelease(int pidfd, unsigned int flags);
++asmlinkage long sys_pmadv_ksm(int pidfd, int behavior, unsigned int flags);
+ asmlinkage long sys_remap_file_pages(unsigned long start, unsigned long size,
+ unsigned long prot, unsigned long pgoff,
+ unsigned long flags);
+diff --git a/include/linux/user_namespace.h b/include/linux/user_namespace.h
+index 33a4240e6..82213f9c4 100644
+--- a/include/linux/user_namespace.h
++++ b/include/linux/user_namespace.h
+@@ -139,6 +139,8 @@ static inline void set_rlimit_ucount_max(struct user_namespace *ns,
+
+ #ifdef CONFIG_USER_NS
+
++extern int unprivileged_userns_clone;
++
+ static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
+ {
+ if (ns)
+@@ -172,6 +174,8 @@ extern bool current_in_userns(const struct user_namespace *target_ns);
+ struct ns_common *ns_get_owner(struct ns_common *ns);
+ #else
+
++#define unprivileged_userns_clone 0
++
+ static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
+ {
+ return &init_user_ns;
+diff --git a/include/linux/wait.h b/include/linux/wait.h
+index 851e07da2..85653fc33 100644
+--- a/include/linux/wait.h
++++ b/include/linux/wait.h
+@@ -165,6 +165,7 @@ static inline bool wq_has_sleeper(struct wait_queue_head *wq_head)
+
+ extern void add_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
+ extern void add_wait_queue_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
++extern void add_wait_queue_exclusive_lifo(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
+ extern void add_wait_queue_priority(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
+ extern void remove_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
+
+@@ -1163,6 +1164,7 @@ do { \
+ */
+ void prepare_to_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
+ bool prepare_to_wait_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
++void prepare_to_wait_exclusive_lifo(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
+ long prepare_to_wait_event(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
+ void finish_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
+ long wait_woken(struct wait_queue_entry *wq_entry, unsigned mode, long timeout);
+diff --git a/include/uapi/asm-generic/unistd.h b/include/uapi/asm-generic/unistd.h
+index 1c48b0ae3..c780129ab 100644
+--- a/include/uapi/asm-generic/unistd.h
++++ b/include/uapi/asm-generic/unistd.h
+@@ -886,8 +886,11 @@ __SYSCALL(__NR_futex_waitv, sys_futex_waitv)
+ #define __NR_set_mempolicy_home_node 450
+ __SYSCALL(__NR_set_mempolicy_home_node, sys_set_mempolicy_home_node)
+
++#define __NR_pmadv_ksm 451
++__SYSCALL(__NR_pmadv_ksm, sys_pmadv_ksm)
++
+ #undef __NR_syscalls
+-#define __NR_syscalls 451
++#define __NR_syscalls 452
+
+ /*
+ * 32 bit systems traditionally used different
+diff --git a/include/uapi/linux/if_bonding.h b/include/uapi/linux/if_bonding.h
+index d174914a8..bf8e2af10 100644
+--- a/include/uapi/linux/if_bonding.h
++++ b/include/uapi/linux/if_bonding.h
+@@ -82,7 +82,7 @@
+ #define BOND_STATE_ACTIVE 0 /* link is active */
+ #define BOND_STATE_BACKUP 1 /* link is backup */
+
+-#define BOND_DEFAULT_MAX_BONDS 1 /* Default maximum number of devices to support */
++#define BOND_DEFAULT_MAX_BONDS 0 /* Default maximum number of devices to support */
+
+ #define BOND_DEFAULT_TX_QUEUES 16 /* Default number of tx queues per device */
+
+diff --git a/init/Kconfig b/init/Kconfig
+index fa63cc019..6c1fa0780 100644
+--- a/init/Kconfig
++++ b/init/Kconfig
+@@ -116,6 +116,10 @@ config THREAD_INFO_IN_TASK
+
+ menu "General setup"
+
++config CACHY
++ bool "Some kernel tweaks by CachyOS"
++ default y
++
+ config BROKEN
+ bool
+
+@@ -1249,6 +1253,22 @@ config USER_NS
+
+ If unsure, say N.
+
++config USER_NS_UNPRIVILEGED
++ bool "Allow unprivileged users to create namespaces"
++ default y
++ depends on USER_NS
++ help
++ When disabled, unprivileged users will not be able to create
++ new namespaces. Allowing users to create their own namespaces
++ has been part of several recent local privilege escalation
++ exploits, so if you need user namespaces but are
++ paranoid^Wsecurity-conscious you want to disable this.
++
++ This setting can be overridden at runtime via the
++ kernel.unprivileged_userns_clone sysctl.
++
++ If unsure, say Y.
++
+ config PID_NS
+ bool "PID Namespaces"
+ default y
+@@ -1379,6 +1399,12 @@ choice
+ prompt "Compiler optimization level"
+ default CC_OPTIMIZE_FOR_PERFORMANCE
+
++config CC_OPTIMIZE_BASAL
++ bool "Optimize for performance (-O1)"
++ help
++ Choosing this option will pass "-O1" to your compiler resulting
++ in basal optimization, possibly speeding up compilation.
++
+ config CC_OPTIMIZE_FOR_PERFORMANCE
+ bool "Optimize for performance (-O2)"
+ help
+diff --git a/init/do_mounts.c b/init/do_mounts.c
+index 7058e14ad..ce4e841ea 100644
+--- a/init/do_mounts.c
++++ b/init/do_mounts.c
+@@ -283,8 +283,18 @@ dev_t name_to_dev_t(const char *name)
+ if (strcmp(name, "/dev/ram") == 0)
+ return Root_RAM0;
+ #ifdef CONFIG_BLOCK
+- if (strncmp(name, "PARTUUID=", 9) == 0)
+- return devt_from_partuuid(name + 9);
++ if (strncmp(name, "PARTUUID=", 9) == 0) {
++ dev_t res;
++ int needtowait = 40<<1;
++ res = devt_from_partuuid(name + 9);
++ while (!res && needtowait) {
++ /* waiting 0.5 sec */
++ msleep(500);
++ res = devt_from_partuuid(name + 9);
++ needtowait--;
++ }
++ return res;
++ }
+ if (strncmp(name, "PARTLABEL=", 10) == 0)
+ return devt_from_partlabel(name + 10);
+ if (strncmp(name, "/dev/", 5) == 0)
+@@ -612,7 +622,9 @@ void __init prepare_namespace(void)
+ * For example, it is not atypical to wait 5 seconds here
+ * for the touchpad of a laptop to initialize.
+ */
++ async_synchronize_full();
+ wait_for_device_probe();
++ async_synchronize_full();
+
+ md_run_setup();
+
+diff --git a/init/version.c b/init/version.c
+index 1a356f549..66d237d56 100644
+--- a/init/version.c
++++ b/init/version.c
+@@ -11,6 +11,8 @@
+ #include <linux/build-salt.h>
+ #include <linux/elfnote-lto.h>
+ #include <linux/export.h>
++#include <linux/init.h>
++#include <linux/printk.h>
+ #include <linux/uts.h>
+ #include <linux/utsname.h>
+ #include <generated/utsrelease.h>
+@@ -35,6 +37,21 @@ struct uts_namespace init_uts_ns = {
+ };
+ EXPORT_SYMBOL_GPL(init_uts_ns);
+
++static int __init early_hostname(char *arg)
++{
++ size_t bufsize = sizeof(init_uts_ns.name.nodename);
++ size_t maxlen = bufsize - 1;
++
++ strncpy(init_uts_ns.name.nodename, arg, bufsize);
++ if (strlen(arg) > maxlen) {
++ pr_warn("hostname parameter exceeds %zd characters and will be truncated",
++ maxlen);
++ init_uts_ns.name.nodename[maxlen] = '\0';
++ }
++ return 0;
++}
++early_param("hostname", early_hostname);
++
+ /* FIXED STRINGS! Don't touch! */
+ const char linux_banner[] =
+ "Linux version " UTS_RELEASE " (" LINUX_COMPILE_BY "@"
+diff --git a/kernel/Kconfig.hz b/kernel/Kconfig.hz
+index 38ef6d068..0f78364ef 100644
+--- a/kernel/Kconfig.hz
++++ b/kernel/Kconfig.hz
+@@ -40,6 +40,27 @@ choice
+ on SMP and NUMA systems and exactly dividing by both PAL and
+ NTSC frame rates for video and multimedia work.
+
++ config HZ_500
++ bool "500 HZ"
++ help
++ 500 Hz is a balanced timer frequency. Provides fast interactivity
++ on desktops with good smoothness without increasing CPU power
++ consumption and sacrificing the battery life on laptops.
++
++ config HZ_600
++ bool "600 HZ"
++ help
++ 600 Hz is a balanced timer frequency. Provides fast interactivity
++ on desktops with good smoothness without increasing CPU power
++ consumption and sacrificing the battery life on laptops.
++
++ config HZ_750
++ bool "750 HZ"
++ help
++ 750 Hz is a balanced timer frequency. Provides fast interactivity
++ on desktops with good smoothness without increasing CPU power
++ consumption and sacrificing the battery life on laptops.
++
+ config HZ_1000
+ bool "1000 HZ"
+ help
+@@ -53,6 +74,9 @@ config HZ
+ default 100 if HZ_100
+ default 250 if HZ_250
+ default 300 if HZ_300
++ default 500 if HZ_500
++ default 600 if HZ_600
++ default 750 if HZ_750
+ default 1000 if HZ_1000
+
+ config SCHED_HRTICK
+diff --git a/kernel/fork.c b/kernel/fork.c
+index 0d8abfb9e..bd7c215e3 100644
+--- a/kernel/fork.c
++++ b/kernel/fork.c
+@@ -99,6 +99,10 @@
+ #include <linux/bpf.h>
+ #include <linux/sched/mm.h>
+
++#ifdef CONFIG_USER_NS
++#include <linux/user_namespace.h>
++#endif
++
+ #include <asm/pgalloc.h>
+ #include <linux/uaccess.h>
+ #include <asm/mmu_context.h>
+@@ -1992,6 +1996,10 @@ static __latent_entropy struct task_struct *copy_process(
+ if ((clone_flags & (CLONE_NEWUSER|CLONE_FS)) == (CLONE_NEWUSER|CLONE_FS))
+ return ERR_PTR(-EINVAL);
+
++ if ((clone_flags & CLONE_NEWUSER) && !unprivileged_userns_clone)
++ if (!capable(CAP_SYS_ADMIN))
++ return ERR_PTR(-EPERM);
++
+ /*
+ * Thread groups must share signals as well, and detached threads
+ * can only be started up within the thread group.
+@@ -3128,6 +3136,12 @@ int ksys_unshare(unsigned long unshare_flags)
+ if (unshare_flags & CLONE_NEWNS)
+ unshare_flags |= CLONE_FS;
+
++ if ((unshare_flags & CLONE_NEWUSER) && !unprivileged_userns_clone) {
++ err = -EPERM;
++ if (!capable(CAP_SYS_ADMIN))
++ goto bad_unshare_out;
++ }
++
+ err = check_unshare_flags(unshare_flags);
+ if (err)
+ goto bad_unshare_out;
+diff --git a/kernel/locking/rwsem.c b/kernel/locking/rwsem.c
+index acde5d6f1..557f683fd 100644
+--- a/kernel/locking/rwsem.c
++++ b/kernel/locking/rwsem.c
+@@ -707,6 +707,7 @@ rwsem_spin_on_owner(struct rw_semaphore *sem)
+ struct task_struct *new, *owner;
+ unsigned long flags, new_flags;
+ enum owner_state state;
++ int i = 0;
+
+ lockdep_assert_preemption_disabled();
+
+@@ -743,7 +744,8 @@ rwsem_spin_on_owner(struct rw_semaphore *sem)
+ break;
+ }
+
+- cpu_relax();
++ if (i++ > 1000)
++ cpu_relax();
+ }
+
+ return state;
+diff --git a/kernel/module.c b/kernel/module.c
+index 6529c84c5..7201b19b7 100644
+--- a/kernel/module.c
++++ b/kernel/module.c
+@@ -57,6 +57,7 @@
+ #include <linux/bsearch.h>
+ #include <linux/dynamic_debug.h>
+ #include <linux/audit.h>
++#include <linux/efi.h>
+ #include <uapi/linux/module.h>
+ #include "module-internal.h"
+
+@@ -270,6 +271,10 @@ static void module_assert_mutex_or_preempt(void)
+ #ifdef CONFIG_MODULE_SIG
+ static bool sig_enforce = IS_ENABLED(CONFIG_MODULE_SIG_FORCE);
+ module_param(sig_enforce, bool_enable_only, 0644);
++/* Allow disabling module signature requirement by adding boot param */
++static bool sig_unenforce = false;
++module_param(sig_unenforce, bool_enable_only, 0644);
++
+
+ void set_module_sig_enforced(void)
+ {
+@@ -416,6 +421,8 @@ extern const struct kernel_symbol __stop___ksymtab_gpl[];
+ extern const s32 __start___kcrctab[];
+ extern const s32 __start___kcrctab_gpl[];
+
++extern struct boot_params boot_params;
++
+ #ifndef CONFIG_MODVERSIONS
+ #define symversion(base, idx) NULL
+ #else
+@@ -4665,6 +4672,19 @@ static const struct proc_ops modules_proc_ops = {
+ static int __init proc_modules_init(void)
+ {
+ proc_create("modules", 0, NULL, &modules_proc_ops);
++
++#ifdef CONFIG_MODULE_SIG_FORCE
++ switch (boot_params.secure_boot) {
++ case efi_secureboot_mode_unset:
++ case efi_secureboot_mode_unknown:
++ case efi_secureboot_mode_disabled:
++ /*
++ * sig_unenforce is only applied if SecureBoot is not
++ * enabled.
++ */
++ sig_enforce = !sig_unenforce;
++ }
++#endif
+ return 0;
+ }
+ module_init(proc_modules_init);
+diff --git a/kernel/sched/core.c b/kernel/sched/core.c
+index dd11daa7a..78ecb72df 100644
+--- a/kernel/sched/core.c
++++ b/kernel/sched/core.c
+@@ -448,8 +448,11 @@ sched_core_dequeue(struct rq *rq, struct task_struct *p, int flags) { }
+ * part of the period that we allow rt tasks to run in us.
+ * default: 0.95s
+ */
++#ifdef CONFIG_CACHY
++int sysctl_sched_rt_runtime = 980000;
++#else
+ int sysctl_sched_rt_runtime = 950000;
+-
++#endif
+
+ /*
+ * Serialization rules:
+diff --git a/kernel/sched/debug.c b/kernel/sched/debug.c
+index bb3d63bdf..e0e356eb0 100644
+--- a/kernel/sched/debug.c
++++ b/kernel/sched/debug.c
+@@ -959,6 +959,7 @@ void proc_sched_show_task(struct task_struct *p, struct pid_namespace *ns,
+ PN(se.exec_start);
+ PN(se.vruntime);
+ PN(se.sum_exec_runtime);
++ P(fsync_count);
+
+ nr_switches = p->nvcsw + p->nivcsw;
+
+diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
+index cc8daa3dc..5be882a6b 100644
+--- a/kernel/sched/fair.c
++++ b/kernel/sched/fair.c
+@@ -67,9 +67,13 @@
+ *
+ * (default: 6ms * (1 + ilog(ncpus)), units: nanoseconds)
+ */
++#ifdef CONFIG_CACHY
++unsigned int sysctl_sched_latency = 3000000ULL;
++static unsigned int normalized_sysctl_sched_latency = 4000000ULL;
++#else
+ unsigned int sysctl_sched_latency = 6000000ULL;
+ static unsigned int normalized_sysctl_sched_latency = 6000000ULL;
+-
++#endif
+ /*
+ * The initial- and re-scaling of tunables is configurable
+ *
+@@ -88,8 +92,13 @@ unsigned int sysctl_sched_tunable_scaling = SCHED_TUNABLESCALING_LOG;
+ *
+ * (default: 0.75 msec * (1 + ilog(ncpus)), units: nanoseconds)
+ */
++#ifdef CONFIG_CACHY
++unsigned int sysctl_sched_min_granularity = 400000ULL;
++static unsigned int normalized_sysctl_sched_min_granularity = 500000ULL;
++#else
+ unsigned int sysctl_sched_min_granularity = 750000ULL;
+ static unsigned int normalized_sysctl_sched_min_granularity = 750000ULL;
++#endif
+
+ /*
+ * Minimal preemption granularity for CPU-bound SCHED_IDLE tasks.
+@@ -119,8 +128,13 @@ unsigned int sysctl_sched_child_runs_first __read_mostly;
+ *
+ * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds)
+ */
++#ifdef CONFIG_CACHY
++unsigned int sysctl_sched_wakeup_granularity = 500000UL;
++static unsigned int normalized_sysctl_sched_wakeup_granularity = 800000UL;
++#else
+ unsigned int sysctl_sched_wakeup_granularity = 1000000UL;
+ static unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL;
++#endif
+
+ const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
+
+@@ -173,8 +187,12 @@ int __weak arch_asym_cpu_priority(int cpu)
+ *
+ * (default: 5 msec, units: microseconds)
+ */
++#ifdef CONFIG_CACHY
++unsigned int sysctl_sched_cfs_bandwidth_slice = 3000UL;
++#else
+ unsigned int sysctl_sched_cfs_bandwidth_slice = 5000UL;
+ #endif
++#endif
+
+ static inline void update_load_add(struct load_weight *lw, unsigned long inc)
+ {
+@@ -1036,6 +1054,7 @@ update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
+ /*
+ * We are starting a new run period:
+ */
++ se->migrated = 0;
+ se->exec_start = rq_clock_task(rq_of(cfs_rq));
+ }
+
+@@ -4243,17 +4262,18 @@ place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
+ {
+ u64 vruntime = cfs_rq->min_vruntime;
+
+- /*
+- * The 'current' period is already promised to the current tasks,
+- * however the extra weight of the new task will slow them down a
+- * little, place the new task so that it fits in the slot that
+- * stays open at the end.
+- */
+- if (initial && sched_feat(START_DEBIT))
+- vruntime += sched_vslice(cfs_rq, se);
++ if (unlikely(initial)) { /* task creation is once, wakeup is often */
++ /*
++ * The 'current' period is already promised to the current
++ * tasks, however the extra weight of the new task will slow
++ * them down a little, place the new task so that it fits in
++ * the slot that stays open at the end.
++ */
++ if (sched_feat(START_DEBIT))
++ vruntime += sched_vslice(cfs_rq, se);
+
+- /* sleeps up to a single latency don't count. */
+- if (!initial) {
++ } else {
++ /* sleeps up to a single latency don't count. */
+ unsigned long thresh;
+
+ if (se_is_idle(se))
+@@ -4275,6 +4295,27 @@ place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
+ se->vruntime = max_vruntime(se->vruntime, vruntime);
+ }
+
++static void place_entity_migrate(struct cfs_rq *cfs_rq, struct sched_entity *se)
++{
++ if (!sched_feat(PLACE_MIGRATE))
++ return;
++
++ if (cfs_rq->nr_running < se->migrated) {
++ /*
++ * Migrated to a shorter runqueue, go first because
++ * we were under-served on the old runqueue.
++ */
++ se->vruntime = cfs_rq->min_vruntime;
++ return;
++ }
++
++ /*
++ * Migrated to a longer runqueue, go last because
++ * we got over-served on the old runqueue.
++ */
++ se->vruntime = cfs_rq->min_vruntime + sched_vslice(cfs_rq, se);
++}
++
+ static void check_enqueue_throttle(struct cfs_rq *cfs_rq);
+
+ static inline bool cfs_bandwidth_used(void);
+@@ -4348,6 +4389,8 @@ enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
+
+ if (flags & ENQUEUE_WAKEUP)
+ place_entity(cfs_rq, se, 0);
++ else if (se->migrated)
++ place_entity_migrate(cfs_rq, se);
+
+ check_schedstat_required();
+ update_stats_enqueue_fair(cfs_rq, se, flags);
+@@ -6982,6 +7025,7 @@ static void detach_entity_cfs_rq(struct sched_entity *se);
+ */
+ static void migrate_task_rq_fair(struct task_struct *p, int new_cpu)
+ {
++ struct sched_entity *se = &p->se;
+ /*
+ * As blocked tasks retain absolute vruntime the migration needs to
+ * deal with this by subtracting the old and adding the new
+@@ -6989,7 +7033,6 @@ static void migrate_task_rq_fair(struct task_struct *p, int new_cpu)
+ * the task on the new runqueue.
+ */
+ if (READ_ONCE(p->__state) == TASK_WAKING) {
+- struct sched_entity *se = &p->se;
+ struct cfs_rq *cfs_rq = cfs_rq_of(se);
+ u64 min_vruntime;
+
+@@ -7014,7 +7057,7 @@ static void migrate_task_rq_fair(struct task_struct *p, int new_cpu)
+ * rq->lock and can modify state directly.
+ */
+ lockdep_assert_rq_held(task_rq(p));
+- detach_entity_cfs_rq(&p->se);
++ detach_entity_cfs_rq(se);
+
+ } else {
+ /*
+@@ -7025,14 +7068,15 @@ static void migrate_task_rq_fair(struct task_struct *p, int new_cpu)
+ * wakee task is less decayed, but giving the wakee more load
+ * sounds not bad.
+ */
+- remove_entity_load_avg(&p->se);
++ remove_entity_load_avg(se);
+ }
+
+ /* Tell new CPU we are migrated */
+- p->se.avg.last_update_time = 0;
++ se->avg.last_update_time = 0;
+
+ /* We have migrated, no longer consider this task hot */
+- p->se.exec_start = 0;
++ for_each_sched_entity(se)
++ se->migrated = READ_ONCE(cfs_rq_of(se)->nr_running) + !se->on_rq;
+
+ update_scan_period(p, new_cpu);
+ }
+@@ -7718,6 +7762,9 @@ static int task_hot(struct task_struct *p, struct lb_env *env)
+ if (sysctl_sched_migration_cost == 0)
+ return 0;
+
++ if (p->se.migrated)
++ return 0;
++
+ delta = rq_clock_task(env->src_rq) - p->se.exec_start;
+
+ return delta < (s64)sysctl_sched_migration_cost;
+diff --git a/kernel/sched/features.h b/kernel/sched/features.h
+index 1cf435bbc..cfff560e0 100644
+--- a/kernel/sched/features.h
++++ b/kernel/sched/features.h
+@@ -100,3 +100,5 @@ SCHED_FEAT(LATENCY_WARN, false)
+
+ SCHED_FEAT(ALT_PERIOD, true)
+ SCHED_FEAT(BASE_SLICE, true)
++
++SCHED_FEAT(PLACE_MIGRATE, true)
+\ No newline at end of file
+diff --git a/kernel/sched/wait.c b/kernel/sched/wait.c
+index 9860bb9a8..c7f045873 100644
+--- a/kernel/sched/wait.c
++++ b/kernel/sched/wait.c
+@@ -47,6 +47,17 @@ void add_wait_queue_priority(struct wait_queue_head *wq_head, struct wait_queue_
+ }
+ EXPORT_SYMBOL_GPL(add_wait_queue_priority);
+
++void add_wait_queue_exclusive_lifo(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
++{
++ unsigned long flags;
++
++ wq_entry->flags |= WQ_FLAG_EXCLUSIVE;
++ spin_lock_irqsave(&wq_head->lock, flags);
++ __add_wait_queue(wq_head, wq_entry);
++ spin_unlock_irqrestore(&wq_head->lock, flags);
++}
++EXPORT_SYMBOL(add_wait_queue_exclusive_lifo);
++
+ void remove_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
+ {
+ unsigned long flags;
+@@ -289,6 +300,19 @@ prepare_to_wait_exclusive(struct wait_queue_head *wq_head, struct wait_queue_ent
+ }
+ EXPORT_SYMBOL(prepare_to_wait_exclusive);
+
++void prepare_to_wait_exclusive_lifo(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state)
++{
++ unsigned long flags;
++
++ wq_entry->flags |= WQ_FLAG_EXCLUSIVE;
++ spin_lock_irqsave(&wq_head->lock, flags);
++ if (list_empty(&wq_entry->entry))
++ __add_wait_queue(wq_head, wq_entry);
++ set_current_state(state);
++ spin_unlock_irqrestore(&wq_head->lock, flags);
++}
++EXPORT_SYMBOL(prepare_to_wait_exclusive_lifo);
++
+ void init_wait_entry(struct wait_queue_entry *wq_entry, int flags)
+ {
+ wq_entry->flags = flags;
+diff --git a/kernel/sys_ni.c b/kernel/sys_ni.c
+index a492f1596..dc765f3ef 100644
+--- a/kernel/sys_ni.c
++++ b/kernel/sys_ni.c
+@@ -291,6 +291,7 @@ COND_SYSCALL(mincore);
+ COND_SYSCALL(madvise);
+ COND_SYSCALL(process_madvise);
+ COND_SYSCALL(process_mrelease);
++COND_SYSCALL(pmadv_ksm);
+ COND_SYSCALL(remap_file_pages);
+ COND_SYSCALL(mbind);
+ COND_SYSCALL(get_mempolicy);
+diff --git a/kernel/sysctl.c b/kernel/sysctl.c
+index 830aaf8ca..af4c0806b 100644
+--- a/kernel/sysctl.c
++++ b/kernel/sysctl.c
+@@ -91,6 +91,9 @@
+ #if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_LOCK_STAT)
+ #include <linux/lockdep.h>
+ #endif
++#ifdef CONFIG_USER_NS
++#include <linux/user_namespace.h>
++#endif
+
+ #if defined(CONFIG_SYSCTL)
+
+@@ -1803,6 +1806,15 @@ static struct ctl_table kern_table[] = {
+ .mode = 0644,
+ .proc_handler = proc_dointvec,
+ },
++#ifdef CONFIG_USER_NS
++ {
++ .procname = "unprivileged_userns_clone",
++ .data = &unprivileged_userns_clone,
++ .maxlen = sizeof(int),
++ .mode = 0644,
++ .proc_handler = proc_dointvec,
++ },
++#endif
+ #ifdef CONFIG_PROC_SYSCTL
+ {
+ .procname = "tainted",
+diff --git a/kernel/user_namespace.c b/kernel/user_namespace.c
+index 5481ba44a..423ab2563 100644
+--- a/kernel/user_namespace.c
++++ b/kernel/user_namespace.c
+@@ -21,6 +21,13 @@
+ #include <linux/bsearch.h>
+ #include <linux/sort.h>
+
++/* sysctl */
++#ifdef CONFIG_USER_NS_UNPRIVILEGED
++int unprivileged_userns_clone = 1;
++#else
++int unprivileged_userns_clone;
++#endif
++
+ static struct kmem_cache *user_ns_cachep __read_mostly;
+ static DEFINE_MUTEX(userns_state_mutex);
+
+diff --git a/kernel/watchdog.c b/kernel/watchdog.c
+index 916622045..161da10e2 100644
+--- a/kernel/watchdog.c
++++ b/kernel/watchdog.c
+@@ -41,7 +41,7 @@ unsigned long __read_mostly watchdog_enabled;
+ int __read_mostly watchdog_user_enabled = 1;
+ int __read_mostly nmi_watchdog_user_enabled = NMI_WATCHDOG_DEFAULT;
+ int __read_mostly soft_watchdog_user_enabled = 1;
+-int __read_mostly watchdog_thresh = 10;
++int __read_mostly watchdog_thresh = 40;
+ static int __read_mostly nmi_watchdog_available;
+
+ struct cpumask watchdog_cpumask __read_mostly;
+diff --git a/mm/compaction.c b/mm/compaction.c
+index de42b8e48..6a30159b0 100644
+--- a/mm/compaction.c
++++ b/mm/compaction.c
+@@ -1736,7 +1736,7 @@ typedef enum {
+ * Allow userspace to control policy on scanning the unevictable LRU for
+ * compactable pages.
+ */
+-#ifdef CONFIG_PREEMPT_RT
++#if defined(CONFIG_PREEMPT_RT) || defined(CONFIG_CACHY)
+ int sysctl_compact_unevictable_allowed __read_mostly = 0;
+ #else
+ int sysctl_compact_unevictable_allowed __read_mostly = 1;
+@@ -2741,7 +2741,11 @@ static void compact_nodes(void)
+ * aggressively the kernel should compact memory in the
+ * background. It takes values in the range [0, 100].
+ */
++#ifdef CONFIG_CACHY
++unsigned int __read_mostly sysctl_compaction_proactiveness;
++#else
+ unsigned int __read_mostly sysctl_compaction_proactiveness = 20;
++#endif
+
+ int compaction_proactiveness_sysctl_handler(struct ctl_table *table, int write,
+ void *buffer, size_t *length, loff_t *ppos)
+diff --git a/mm/huge_memory.c b/mm/huge_memory.c
+index b7d0697b1..4bccbe803 100644
+--- a/mm/huge_memory.c
++++ b/mm/huge_memory.c
+@@ -58,7 +58,11 @@ unsigned long transparent_hugepage_flags __read_mostly =
+ #ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
+ (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)|
+ #endif
++#ifdef CONFIG_CACHY
++ (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG)|
++#else
+ (1<<TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG)|
++#endif
+ (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG)|
+ (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
+
+diff --git a/mm/internal.h b/mm/internal.h
+index cf16280ce..b9ad231c8 100644
+--- a/mm/internal.h
++++ b/mm/internal.h
+@@ -256,6 +256,7 @@ extern void prep_compound_page(struct page *page, unsigned int order);
+ extern void post_alloc_hook(struct page *page, unsigned int order,
+ gfp_t gfp_flags);
+ extern int user_min_free_kbytes;
++extern atomic_long_t kswapd_waiters;
+
+ extern void free_unref_page(struct page *page, unsigned int order);
+ extern void free_unref_page_list(struct list_head *list);
+diff --git a/mm/ksm.c b/mm/ksm.c
+index 063a48eeb..dfa3ce8cd 100644
+--- a/mm/ksm.c
++++ b/mm/ksm.c
+@@ -2408,9 +2408,14 @@ static int ksm_scan_thread(void *nothing)
+
+ if (ksmd_should_run()) {
+ sleep_ms = READ_ONCE(ksm_thread_sleep_millisecs);
+- wait_event_interruptible_timeout(ksm_iter_wait,
+- sleep_ms != READ_ONCE(ksm_thread_sleep_millisecs),
+- msecs_to_jiffies(sleep_ms));
++ if (sleep_ms >= 1000)
++ wait_event_interruptible_timeout(ksm_iter_wait,
++ sleep_ms != READ_ONCE(ksm_thread_sleep_millisecs),
++ msecs_to_jiffies(round_jiffies_relative(sleep_ms)));
++ else
++ wait_event_interruptible_timeout(ksm_iter_wait,
++ sleep_ms != READ_ONCE(ksm_thread_sleep_millisecs),
++ msecs_to_jiffies(sleep_ms));
+ } else {
+ wait_event_freezable(ksm_thread_wait,
+ ksmd_should_run() || kthread_should_stop());
+@@ -2419,54 +2424,78 @@ static int ksm_scan_thread(void *nothing)
+ return 0;
+ }
+
+-int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
+- unsigned long end, int advice, unsigned long *vm_flags)
++int ksm_madvise_merge(struct mm_struct *mm, struct vm_area_struct *vma,
++ unsigned long *vm_flags)
+ {
+- struct mm_struct *mm = vma->vm_mm;
+ int err;
+
+- switch (advice) {
+- case MADV_MERGEABLE:
+- /*
+- * Be somewhat over-protective for now!
+- */
+- if (*vm_flags & (VM_MERGEABLE | VM_SHARED | VM_MAYSHARE |
+- VM_PFNMAP | VM_IO | VM_DONTEXPAND |
+- VM_HUGETLB | VM_MIXEDMAP))
+- return 0; /* just ignore the advice */
++ /*
++ * Be somewhat over-protective for now!
++ */
++ if (*vm_flags & (VM_MERGEABLE | VM_SHARED | VM_MAYSHARE |
++ VM_PFNMAP | VM_IO | VM_DONTEXPAND |
++ VM_HUGETLB | VM_MIXEDMAP))
++ return 0; /* just ignore the advice */
+
+- if (vma_is_dax(vma))
+- return 0;
++ if (vma_is_dax(vma))
++ return 0;
+
+ #ifdef VM_SAO
+ if (*vm_flags & VM_SAO)
+ return 0;
+ #endif
+ #ifdef VM_SPARC_ADI
+- if (*vm_flags & VM_SPARC_ADI)
+- return 0;
++ if (*vm_flags & VM_SPARC_ADI)
++ return 0;
+ #endif
+
+- if (!test_bit(MMF_VM_MERGEABLE, &mm->flags)) {
+- err = __ksm_enter(mm);
+- if (err)
+- return err;
+- }
++ if (!test_bit(MMF_VM_MERGEABLE, &mm->flags)) {
++ err = __ksm_enter(mm);
++ if (err)
++ return err;
++ }
+
+- *vm_flags |= VM_MERGEABLE;
+- break;
++ *vm_flags |= VM_MERGEABLE;
+
+- case MADV_UNMERGEABLE:
+- if (!(*vm_flags & VM_MERGEABLE))
+- return 0; /* just ignore the advice */
++ return 0;
++}
+
+- if (vma->anon_vma) {
+- err = unmerge_ksm_pages(vma, start, end);
+- if (err)
+- return err;
+- }
++int ksm_madvise_unmerge(struct vm_area_struct *vma, unsigned long start,
++ unsigned long end, unsigned long *vm_flags)
++{
++ int err;
++
++ if (!(*vm_flags & VM_MERGEABLE))
++ return 0; /* just ignore the advice */
++
++ if (vma->anon_vma) {
++ err = unmerge_ksm_pages(vma, start, end);
++ if (err)
++ return err;
++ }
+
+- *vm_flags &= ~VM_MERGEABLE;
++ *vm_flags &= ~VM_MERGEABLE;
++
++ return 0;
++}
++
++int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
++ unsigned long end, int advice, unsigned long *vm_flags)
++{
++ struct mm_struct *mm = vma->vm_mm;
++ int err;
++
++ switch (advice) {
++ case MADV_MERGEABLE:
++ err = ksm_madvise_merge(mm, vma, vm_flags);
++ if (err)
++ return err;
++ break;
++
++ case MADV_UNMERGEABLE:
++ err = ksm_madvise_unmerge(vma, start, end, vm_flags);
++ if (err)
++ return err;
+ break;
+ }
+
+diff --git a/mm/madvise.c b/mm/madvise.c
+index 4d29a11c1..d9e6140be 100644
+--- a/mm/madvise.c
++++ b/mm/madvise.c
+@@ -1482,3 +1482,114 @@ SYSCALL_DEFINE5(process_madvise, int, pidfd, const struct iovec __user *, vec,
+ out:
+ return ret;
+ }
++
++SYSCALL_DEFINE3(pmadv_ksm, int, pidfd, int, behaviour, unsigned int, flags)
++{
++#ifdef CONFIG_KSM
++ ssize_t ret;
++ struct pid *pid;
++ struct task_struct *task;
++ struct mm_struct *mm;
++ unsigned int f_flags;
++ struct vm_area_struct *vma;
++
++ if (flags != 0) {
++ ret = -EINVAL;
++ goto out;
++ }
++
++ switch (behaviour) {
++ case MADV_MERGEABLE:
++ case MADV_UNMERGEABLE:
++ break;
++ default:
++ ret = -EINVAL;
++ goto out;
++ break;
++ }
++
++ pid = pidfd_get_pid(pidfd, &f_flags);
++ if (IS_ERR(pid)) {
++ ret = PTR_ERR(pid);
++ goto out;
++ }
++
++ task = get_pid_task(pid, PIDTYPE_PID);
++ if (!task) {
++ ret = -ESRCH;
++ goto put_pid;
++ }
++
++ /* Require PTRACE_MODE_READ to avoid leaking ASLR metadata. */
++ mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
++ if (IS_ERR_OR_NULL(mm)) {
++ ret = IS_ERR(mm) ? PTR_ERR(mm) : -ESRCH;
++ goto release_task;
++ }
++
++ /* Require CAP_SYS_NICE for influencing process performance. */
++ if (!capable(CAP_SYS_NICE)) {
++ ret = -EPERM;
++ goto release_mm;
++ }
++
++ if (mmap_write_lock_killable(mm)) {
++ ret = -EINTR;
++ goto release_mm;
++ }
++
++ for (vma = mm->mmap; vma; vma = vma->vm_next) {
++ switch (behaviour) {
++ case MADV_MERGEABLE:
++ ret = ksm_madvise_merge(vma->vm_mm, vma, &vma->vm_flags);
++ break;
++ case MADV_UNMERGEABLE:
++ ret = ksm_madvise_unmerge(vma, vma->vm_start, vma->vm_end, &vma->vm_flags);
++ break;
++ default:
++ /* look, ma, no brain */
++ break;
++ }
++ if (ret)
++ break;
++ }
++
++ mmap_write_unlock(mm);
++
++release_mm:
++ mmput(mm);
++release_task:
++ put_task_struct(task);
++put_pid:
++ put_pid(pid);
++out:
++ return ret;
++#else /* CONFIG_KSM */
++ return -ENOSYS;
++#endif /* CONFIG_KSM */
++}
++
++#ifdef CONFIG_KSM
++static ssize_t ksm_show(struct kobject *kobj, struct kobj_attribute *attr,
++ char *buf)
++{
++ return sprintf(buf, "%u\n", __NR_pmadv_ksm);
++}
++static struct kobj_attribute pmadv_ksm_attr = __ATTR_RO(ksm);
++
++static struct attribute *pmadv_sysfs_attrs[] = {
++ &pmadv_ksm_attr.attr,
++ NULL,
++};
++
++static const struct attribute_group pmadv_sysfs_attr_group = {
++ .attrs = pmadv_sysfs_attrs,
++ .name = "pmadv",
++};
++
++static int __init pmadv_sysfs_init(void)
++{
++ return sysfs_create_group(kernel_kobj, &pmadv_sysfs_attr_group);
++}
++subsys_initcall(pmadv_sysfs_init);
++#endif /* CONFIG_KSM */
+diff --git a/mm/memcontrol.c b/mm/memcontrol.c
+index 598fece89..b7d092880 100644
+--- a/mm/memcontrol.c
++++ b/mm/memcontrol.c
+@@ -627,7 +627,7 @@ static inline void memcg_rstat_updated(struct mem_cgroup *memcg, int val)
+ cgroup_rstat_updated(memcg->css.cgroup, smp_processor_id());
+
+ x = __this_cpu_add_return(stats_updates, abs(val));
+- if (x > MEMCG_CHARGE_BATCH) {
++ if (x > MEMCG_CHARGE_BATCH * 128) {
+ atomic_add(x / MEMCG_CHARGE_BATCH, &stats_flush_threshold);
+ __this_cpu_write(stats_updates, 0);
+ }
+diff --git a/mm/page-writeback.c b/mm/page-writeback.c
+index 7e2da284e..b1ef020a3 100644
+--- a/mm/page-writeback.c
++++ b/mm/page-writeback.c
+@@ -70,7 +70,11 @@ static long ratelimit_pages = 32;
+ /*
+ * Start background writeback (via writeback threads) at this percentage
+ */
++#ifdef CONFIG_CACHY
++int dirty_background_ratio = 5;
++#else
+ int dirty_background_ratio = 10;
++#endif
+
+ /*
+ * dirty_background_bytes starts at 0 (disabled) so that it is a function of
+@@ -98,7 +102,11 @@ unsigned long vm_dirty_bytes;
+ /*
+ * The interval between `kupdate'-style writebacks
+ */
++#ifdef CONFIG_CACHY
++unsigned int dirty_writeback_interval = 10 * 100; /* centiseconds */
++#else
+ unsigned int dirty_writeback_interval = 5 * 100; /* centiseconds */
++#endif
+
+ EXPORT_SYMBOL_GPL(dirty_writeback_interval);
+
+diff --git a/mm/page_alloc.c b/mm/page_alloc.c
+index 5ced6cb26..041c16aa8 100644
+--- a/mm/page_alloc.c
++++ b/mm/page_alloc.c
+@@ -121,6 +121,8 @@ typedef int __bitwise fpi_t;
+ */
+ #define FPI_SKIP_KASAN_POISON ((__force fpi_t)BIT(2))
+
++atomic_long_t kswapd_waiters = ATOMIC_LONG_INIT(0);
++
+ /* prevent >1 _updater_ of zone percpu pageset ->high and ->batch fields */
+ static DEFINE_MUTEX(pcp_batch_high_lock);
+ #define MIN_PERCPU_PAGELIST_HIGH_FRACTION (8)
+@@ -341,7 +343,7 @@ compound_page_dtor * const compound_page_dtors[NR_COMPOUND_DTORS] = {
+
+ int min_free_kbytes = 1024;
+ int user_min_free_kbytes = -1;
+-int watermark_boost_factor __read_mostly = 15000;
++int watermark_boost_factor __read_mostly;
+ int watermark_scale_factor = 10;
+
+ static unsigned long nr_kernel_pages __initdata;
+@@ -3019,15 +3021,16 @@ __rmqueue(struct zone *zone, unsigned int order, int migratetype,
+ }
+
+ /*
+- * Obtain a specified number of elements from the buddy allocator, all under
+- * a single hold of the lock, for efficiency. Add them to the supplied list.
+- * Returns the number of new pages which were placed at *list.
++ * Obtain a specified number of elements from the buddy allocator, and relax the
++ * zone lock when needed. Add them to the supplied list. Returns the number of
++ * new pages which were placed at *list.
+ */
+ static int rmqueue_bulk(struct zone *zone, unsigned int order,
+ unsigned long count, struct list_head *list,
+ int migratetype, unsigned int alloc_flags)
+ {
+- int i, allocated = 0;
++ const bool can_resched = !preempt_count() && !irqs_disabled();
++ int i, allocated = 0, last_mod = 0;
+
+ /*
+ * local_lock_irq held so equivalent to spin_lock_irqsave for
+@@ -3040,6 +3043,18 @@ static int rmqueue_bulk(struct zone *zone, unsigned int order,
+ if (unlikely(page == NULL))
+ break;
+
++ /* Reschedule and ease the contention on the lock if needed */
++ if (i + 1 < count && ((can_resched && need_resched()) ||
++ spin_needbreak(&zone->lock))) {
++ __mod_zone_page_state(zone, NR_FREE_PAGES,
++ -((i + 1 - last_mod) << order));
++ last_mod = i + 1;
++ spin_unlock(&zone->lock);
++ if (can_resched)
++ cond_resched();
++ spin_lock(&zone->lock);
++ }
++
+ if (unlikely(check_pcp_refill(page, order)))
+ continue;
+
+@@ -3066,7 +3081,7 @@ static int rmqueue_bulk(struct zone *zone, unsigned int order,
+ * on i. Do not confuse with 'allocated' which is the number of
+ * pages added to the pcp list.
+ */
+- __mod_zone_page_state(zone, NR_FREE_PAGES, -(i << order));
++ __mod_zone_page_state(zone, NR_FREE_PAGES, -((i - last_mod) << order));
+ spin_unlock(&zone->lock);
+ return allocated;
+ }
+@@ -4897,6 +4912,7 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
+ int no_progress_loops;
+ unsigned int cpuset_mems_cookie;
+ int reserve_flags;
++ bool woke_kswapd = false;
+
+ /*
+ * We also sanity check to catch abuse of atomic reserves being used by
+@@ -4943,8 +4959,13 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
+ goto nopage;
+ }
+
+- if (alloc_flags & ALLOC_KSWAPD)
++ if (alloc_flags & ALLOC_KSWAPD) {
++ if (!woke_kswapd) {
++ atomic_long_inc(&kswapd_waiters);
++ woke_kswapd = true;
++ }
+ wake_all_kswapds(order, gfp_mask, ac);
++ }
+
+ /*
+ * The adjusted alloc_flags might result in immediate success, so try
+@@ -5149,9 +5170,12 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
+ goto retry;
+ }
+ fail:
+- warn_alloc(gfp_mask, ac->nodemask,
+- "page allocation failure: order:%u", order);
+ got_pg:
++ if (woke_kswapd)
++ atomic_long_dec(&kswapd_waiters);
++ if (!page)
++ warn_alloc(gfp_mask, ac->nodemask,
++ "page allocation failure: order:%u", order);
+ return page;
+ }
+
+diff --git a/mm/vmpressure.c b/mm/vmpressure.c
+index b52644771..11a4b0e3b 100644
+--- a/mm/vmpressure.c
++++ b/mm/vmpressure.c
+@@ -43,7 +43,11 @@ static const unsigned long vmpressure_win = SWAP_CLUSTER_MAX * 16;
+ * essence, they are percents: the higher the value, the more number
+ * unsuccessful reclaims there were.
+ */
++#ifdef CONFIG_CACHY
++static const unsigned int vmpressure_level_med = 65;
++#else
+ static const unsigned int vmpressure_level_med = 60;
++#endif
+ static const unsigned int vmpressure_level_critical = 95;
+
+ /*
+diff --git a/mm/vmscan.c b/mm/vmscan.c
+index 1678802e0..217576cba 100644
+--- a/mm/vmscan.c
++++ b/mm/vmscan.c
+@@ -175,7 +175,11 @@ struct scan_control {
+ /*
+ * From 0 .. 200. Higher means more swappy.
+ */
++#ifdef CONFIG_CACHY
++int vm_swappiness = 20;
++#else
+ int vm_swappiness = 60;
++#endif
+
+ static void set_task_reclaim_state(struct task_struct *task,
+ struct reclaim_state *rs)
+@@ -3584,7 +3588,7 @@ static unsigned long do_try_to_free_pages(struct zonelist *zonelist,
+ return 0;
+ }
+
+-static bool allow_direct_reclaim(pg_data_t *pgdat)
++static bool allow_direct_reclaim(pg_data_t *pgdat, bool using_kswapd)
+ {
+ struct zone *zone;
+ unsigned long pfmemalloc_reserve = 0;
+@@ -3613,6 +3617,10 @@ static bool allow_direct_reclaim(pg_data_t *pgdat)
+
+ wmark_ok = free_pages > pfmemalloc_reserve / 2;
+
++ /* The throttled direct reclaimer is now a kswapd waiter */
++ if (unlikely(!using_kswapd && !wmark_ok))
++ atomic_long_inc(&kswapd_waiters);
++
+ /* kswapd must be awake if processes are being throttled */
+ if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) {
+ if (READ_ONCE(pgdat->kswapd_highest_zoneidx) > ZONE_NORMAL)
+@@ -3678,7 +3686,7 @@ static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist,
+
+ /* Throttle based on the first usable node */
+ pgdat = zone->zone_pgdat;
+- if (allow_direct_reclaim(pgdat))
++ if (allow_direct_reclaim(pgdat, gfp_mask & __GFP_KSWAPD_RECLAIM))
+ goto out;
+ break;
+ }
+@@ -3700,11 +3708,14 @@ static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist,
+ */
+ if (!(gfp_mask & __GFP_FS))
+ wait_event_interruptible_timeout(pgdat->pfmemalloc_wait,
+- allow_direct_reclaim(pgdat), HZ);
++ allow_direct_reclaim(pgdat, true), HZ);
+ else
+ /* Throttle until kswapd wakes the process */
+ wait_event_killable(zone->zone_pgdat->pfmemalloc_wait,
+- allow_direct_reclaim(pgdat));
++ allow_direct_reclaim(pgdat, true));
++
++ if (unlikely(!(gfp_mask & __GFP_KSWAPD_RECLAIM)))
++ atomic_long_dec(&kswapd_waiters);
+
+ if (fatal_signal_pending(current))
+ return true;
+@@ -4197,14 +4208,15 @@ static int balance_pgdat(pg_data_t *pgdat, int order, int highest_zoneidx)
+ * able to safely make forward progress. Wake them
+ */
+ if (waitqueue_active(&pgdat->pfmemalloc_wait) &&
+- allow_direct_reclaim(pgdat))
++ allow_direct_reclaim(pgdat, true))
+ wake_up_all(&pgdat->pfmemalloc_wait);
+
+ /* Check if kswapd should be suspending */
+ __fs_reclaim_release(_THIS_IP_);
+ ret = try_to_freeze();
+ __fs_reclaim_acquire(_THIS_IP_);
+- if (ret || kthread_should_stop())
++ if (ret || kthread_should_stop() ||
++ !atomic_long_read(&kswapd_waiters))
+ break;
+
+ /*
+diff --git a/net/ipv4/inet_connection_sock.c b/net/ipv4/inet_connection_sock.c
+index 1e5b53c2b..f1c86ceac 100644
+--- a/net/ipv4/inet_connection_sock.c
++++ b/net/ipv4/inet_connection_sock.c
+@@ -441,7 +441,7 @@ static int inet_csk_wait_for_connect(struct sock *sk, long timeo)
+ * having to remove and re-insert us on the wait queue.
+ */
+ for (;;) {
+- prepare_to_wait_exclusive(sk_sleep(sk), &wait,
++ prepare_to_wait_exclusive_lifo(sk_sleep(sk), &wait,
+ TASK_INTERRUPTIBLE);
+ release_sock(sk);
+ if (reqsk_queue_empty(&icsk->icsk_accept_queue))
+diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
+index e31cf137c..724eb145f 100644
+--- a/net/ipv4/tcp.c
++++ b/net/ipv4/tcp.c
+@@ -4671,8 +4671,8 @@ void __init tcp_init(void)
+ tcp_init_mem();
+ /* Set per-socket limits to no more than 1/128 the pressure threshold */
+ limit = nr_free_buffer_pages() << (PAGE_SHIFT - 7);
+- max_wshare = min(4UL*1024*1024, limit);
+- max_rshare = min(6UL*1024*1024, limit);
++ max_wshare = min(16UL*1024*1024, limit);
++ max_rshare = min(16UL*1024*1024, limit);
+
+ init_net.ipv4.sysctl_tcp_wmem[0] = SK_MEM_QUANTUM;
+ init_net.ipv4.sysctl_tcp_wmem[1] = 16*1024;
+--
+2.37.0.rc0.15.g3b9a5a33c2
+
+
+From ab00d6d9bf711e24cb091e46876c89f56a19aff3 Mon Sep 17 00:00:00 2001
+From: Peter Jung <[email protected]>
+Date: Tue, 28 Jun 2022 10:14:16 +0200
+Subject: [PATCH 2/5] sched/core: Optimize load balance of core scheduling
+
+In function sched_core_update_cookie(), a task will enqueue into the
+core tree only when it enqueued before, that is, if an uncookied task
+is cookied, it will not enqueue into the core tree until it enqueue
+again, which will result in unnecessary force idle.
+
+Here follows the scenario:
+ CPU x and CPU y are a pair of SMT siblings.
+ 1. Start task a running on CPU x without sleeping, and task b and
+ task c running on CPU y without sleeping.
+ 2. We create a cookie and share it to task a and task b, and then
+ we create another cookie and share it to task c.
+ 3. Simpling core_forceidle_sum of task a and b from /proc/PID/sched
+
+And we will find out that core_forceidle_sum of task a takes 30%
+
+If the number of tasks in the runqueue of SMT siblings are equal, we call
+the core balanced, otherwise unbalanced. If the core is balanced, everytime
+we pick next task, we can pick a pair of tasks with the same cookie for
+each SMT sibling, and forceidle will be avoided.
+
+ - Migrate the task if source core and destination core can balance
+ If ck->nr_running of src_cpu is the highest among the source core, and
+ ck->nr_running of dst_cpu is the lowest among the destination core,
+ migrate the task.
+
+ - Select cookie matched idle CPU or idle CPU with the lowest
+ ck->nr_running among the core
+ In the fast path of task wakeup, if ck->nr_running of the cpu is the
+ lowest among the core, we can select this cpu to wake up.
+
+ - Find cookie matched idlest CPU or cookie matched CPU with the lowest
+ ck->nr_running among the core
+ In the slow path of task wakeup, if ck->nr_running of the cpu is the
+ lowest among the core, we can select this cpu to wake up.
+time of the sampling period, which shouldn't happen as task a and b
+have the same cookie.
+
+Then we migrate task a to CPU x', migrate task b and c to CPU y', where
+CPU x' and CPU y' are a pair of SMT siblings, and sampling again, we
+will found out that core_forceidle_sum of task a and b are almost zero.
+
+To solve this problem, we enqueue the task into the core tree if it's
+on rq.
+
+Signed-off-by: Peter Jung <[email protected]>
+---
+ kernel/sched/core.c | 7 ++++
+ kernel/sched/core_sched.c | 18 +++++-----
+ kernel/sched/fair.c | 4 +--
+ kernel/sched/sched.h | 74 +++++++++++++++++++++++++++++++++++----
+ 4 files changed, 86 insertions(+), 17 deletions(-)
+
+diff --git a/kernel/sched/core.c b/kernel/sched/core.c
+index 78ecb72df..2b36964d2 100644
+--- a/kernel/sched/core.c
++++ b/kernel/sched/core.c
+@@ -238,21 +238,28 @@ static inline int rb_sched_core_cmp(const void *key, const struct rb_node *node)
+
+ void sched_core_enqueue(struct rq *rq, struct task_struct *p)
+ {
++ struct sched_core_cookie *ck = (struct sched_core_cookie *)p->core_cookie;
++
+ rq->core->core_task_seq++;
+
+ if (!p->core_cookie)
+ return;
+
+ rb_add(&p->core_node, &rq->core_tree, rb_sched_core_less);
++
++ *per_cpu_ptr(ck->nr_running, rq->cpu) += 1;
+ }
+
+ void sched_core_dequeue(struct rq *rq, struct task_struct *p, int flags)
+ {
++ struct sched_core_cookie *ck = (struct sched_core_cookie *)p->core_cookie;
++
+ rq->core->core_task_seq++;
+
+ if (sched_core_enqueued(p)) {
+ rb_erase(&p->core_node, &rq->core_tree);
+ RB_CLEAR_NODE(&p->core_node);
++ *per_cpu_ptr(ck->nr_running, rq->cpu) -= 1;
+ }
+
+ /*
+diff --git a/kernel/sched/core_sched.c b/kernel/sched/core_sched.c
+index 38a2cec21..65ab9fcbe 100644
+--- a/kernel/sched/core_sched.c
++++ b/kernel/sched/core_sched.c
+@@ -1,20 +1,19 @@
+ // SPDX-License-Identifier: GPL-2.0-only
+
+-/*
+- * A simple wrapper around refcount. An allocated sched_core_cookie's
+- * address is used to compute the cookie of the task.
+- */
+-struct sched_core_cookie {
+- refcount_t refcnt;
+-};
+-
+ static unsigned long sched_core_alloc_cookie(void)
+ {
+ struct sched_core_cookie *ck = kmalloc(sizeof(*ck), GFP_KERNEL);
++ int cpu;
++
+ if (!ck)
+ return 0;
+
+ refcount_set(&ck->refcnt, 1);
++
++ ck->nr_running = alloc_percpu(unsigned int);
++ for_each_possible_cpu(cpu)
++ *per_cpu_ptr(ck->nr_running, cpu) = 0;
++
+ sched_core_get();
+
+ return (unsigned long)ck;
+@@ -25,6 +24,7 @@ static void sched_core_put_cookie(unsigned long cookie)
+ struct sched_core_cookie *ptr = (void *)cookie;
+
+ if (ptr && refcount_dec_and_test(&ptr->refcnt)) {
++ free_percpu(ptr->nr_running);
+ kfree(ptr);
+ sched_core_put();
+ }
+@@ -75,7 +75,7 @@ static unsigned long sched_core_update_cookie(struct task_struct *p,
+ old_cookie = p->core_cookie;
+ p->core_cookie = cookie;
+
+- if (enqueued)
++ if (task_on_rq_queued(p))
+ sched_core_enqueue(rq, p);
+
+ /*
+diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
+index 5be882a6b..1afec60a4 100644
+--- a/kernel/sched/fair.c
++++ b/kernel/sched/fair.c
+@@ -6127,7 +6127,7 @@ find_idlest_group_cpu(struct sched_group *group, struct task_struct *p, int this
+ for_each_cpu_and(i, sched_group_span(group), p->cpus_ptr) {
+ struct rq *rq = cpu_rq(i);
+
+- if (!sched_core_cookie_match(rq, p))
++ if (!sched_core_cookie_match(NULL, rq, p))
+ continue;
+
+ if (sched_idle_cpu(i))
+@@ -7756,7 +7756,7 @@ static int task_hot(struct task_struct *p, struct lb_env *env)
+ * Don't migrate task if the task's cookie does not match
+ * with the destination CPU's core cookie.
+ */
+- if (!sched_core_cookie_match(cpu_rq(env->dst_cpu), p))
++ if (!(sched_core_cookie_match(env->src_rq, env->dst_rq, p)))
+ return 1;
+
+ if (sysctl_sched_migration_cost == 0)
+diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
+index 84bba67c9..8e9b8fc51 100644
+--- a/kernel/sched/sched.h
++++ b/kernel/sched/sched.h
+@@ -1177,6 +1177,65 @@ static inline raw_spinlock_t *__rq_lockp(struct rq *rq)
+
+ bool cfs_prio_less(struct task_struct *a, struct task_struct *b, bool fi);
+
++/*
++ * A simple wrapper around refcount. An allocated sched_core_cookie's
++ * address is used to compute the cookie of the task.
++ */
++struct sched_core_cookie {
++ refcount_t refcnt;
++ unsigned int __percpu *nr_running;
++};
++
++/*
++ * When tasks with the same cookie can make pairs on SMT siblings, forceidle can be
++ * avoided a lot, so when wake up and load balance, we try to make and keep the pairs
++ * with the same cookie on SMT siblings.
++ */
++static inline bool
++sched_core_make_pair_balance(struct rq *src_rq, struct rq *dst_rq, struct task_struct *p)
++{
++ struct sched_core_cookie *ck = (struct sched_core_cookie *)p->core_cookie;
++ unsigned int src_cpu, dst_cpu, t;
++ unsigned int src_nr_running, dst_nr_running;
++
++ if (!ck)
++ return true;
++
++ /*
++ * When load balance, if ck->nr_running on src_cpu is less than that on SMT
++ * siblings, don't migrate the task.
++ */
++ if (src_rq) {
++ if (!sched_core_enabled(src_rq))
++ return true;
++ src_cpu = cpu_of(src_rq);
++ src_nr_running = *per_cpu_ptr(ck->nr_running, src_cpu);
++ for_each_cpu(t, cpu_smt_mask(src_cpu)) {
++ if (t == src_cpu)
++ continue;
++ if (*per_cpu_ptr(ck->nr_running, t) >= src_nr_running)
++ return false;
++ }
++
++ }
++
++ /*
++ * If task p can make pair the cookied task with p->core_cookie on the
++ * dst core, we can wake up task p on dst_rq, or migrate it to dst_rq.
++ */
++ dst_cpu = cpu_of(dst_rq);
++ dst_nr_running = *per_cpu_ptr(ck->nr_running, dst_cpu);
++ for_each_cpu(t, cpu_smt_mask(dst_cpu)) {
++ if (t == dst_cpu)
++ continue;
++ if (*per_cpu_ptr(ck->nr_running, t) <= dst_nr_running)
++ return false;
++ }
++
++ return true;
++}
++
++
+ /*
+ * Helpers to check if the CPU's core cookie matches with the task's cookie
+ * when core scheduling is enabled.
+@@ -1189,19 +1248,21 @@ static inline bool sched_cpu_cookie_match(struct rq *rq, struct task_struct *p)
+ if (!sched_core_enabled(rq))
+ return true;
+
+- return rq->core->core_cookie == p->core_cookie;
++ return rq->core->core_cookie == p->core_cookie ||
++ sched_core_make_pair_balance(NULL, rq, p);
+ }
+
+-static inline bool sched_core_cookie_match(struct rq *rq, struct task_struct *p)
++static inline bool
++sched_core_cookie_match(struct rq *src_rq, struct rq *dst_rq, struct task_struct *p)
+ {
+ bool idle_core = true;
+ int cpu;
+
+ /* Ignore cookie match if core scheduler is not enabled on the CPU. */
+- if (!sched_core_enabled(rq))
++ if (!sched_core_enabled(dst_rq))
+ return true;
+
+- for_each_cpu(cpu, cpu_smt_mask(cpu_of(rq))) {
++ for_each_cpu(cpu, cpu_smt_mask(cpu_of(dst_rq))) {
+ if (!available_idle_cpu(cpu)) {
+ idle_core = false;
+ break;
+@@ -1212,7 +1273,8 @@ static inline bool sched_core_cookie_match(struct rq *rq, struct task_struct *p)
+ * A CPU in an idle core is always the best choice for tasks with
+ * cookies.
+ */
+- return idle_core || rq->core->core_cookie == p->core_cookie;
++ return idle_core || dst_rq->core->core_cookie == p->core_cookie ||
++ sched_core_make_pair_balance(src_rq, dst_rq, p);
+ }
+
+ static inline bool sched_group_cookie_match(struct rq *rq,
+@@ -1226,7 +1288,7 @@ static inline bool sched_group_cookie_match(struct rq *rq,
+ return true;
+
+ for_each_cpu_and(cpu, sched_group_span(group), p->cpus_ptr) {
+- if (sched_core_cookie_match(rq, p))
++ if (sched_core_cookie_match(NULL, rq, p))
+ return true;
+ }
+ return false;
+--
+2.37.0.rc0.15.g3b9a5a33c2
+
+
+From 959ac64a9087670b2543e813344b8f37b822dd3e Mon Sep 17 00:00:00 2001
+From: Peter Jung <[email protected]>
+Date: Tue, 28 Jun 2022 18:38:39 +0200
+Subject: [PATCH 3/5] Introduce SIS_UTIL to search idle CPU based on sum of
+ util_avg
+
+Signed-off-by: Peter Jung <[email protected]>
+---
+ include/linux/sched/topology.h | 1 +
+ kernel/sched/fair.c | 87 ++++++++++++++++++++++++++++++++++
+ kernel/sched/features.h | 3 +-
+ 3 files changed, 90 insertions(+), 1 deletion(-)
+
+diff --git a/include/linux/sched/topology.h b/include/linux/sched/topology.h
+index 56cffe42a..816df6cc4 100644
+--- a/include/linux/sched/topology.h
++++ b/include/linux/sched/topology.h
+@@ -81,6 +81,7 @@ struct sched_domain_shared {
+ atomic_t ref;
+ atomic_t nr_busy_cpus;
+ int has_idle_cores;
++ int nr_idle_scan;
+ };
+
+ struct sched_domain {
+diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
+index 1afec60a4..00da5ac47 100644
+--- a/kernel/sched/fair.c
++++ b/kernel/sched/fair.c
+@@ -6367,6 +6367,7 @@ static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool
+ {
+ struct cpumask *cpus = this_cpu_cpumask_var_ptr(select_idle_mask);
+ int i, cpu, idle_cpu = -1, nr = INT_MAX;
++ struct sched_domain_shared *sd_share;
+ struct rq *this_rq = this_rq();
+ int this = smp_processor_id();
+ struct sched_domain *this_sd;
+@@ -6406,6 +6407,17 @@ static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool
+ time = cpu_clock(this);
+ }
+
++ if (sched_feat(SIS_UTIL)) {
++ sd_share = rcu_dereference(per_cpu(sd_llc_shared, target));
++ if (sd_share) {
++ /* because !--nr is the condition to stop scan */
++ nr = READ_ONCE(sd_share->nr_idle_scan) + 1;
++ /* overloaded LLC is unlikely to have idle cpu/core */
++ if (nr == 1)
++ return -1;
++ }
++ }
++
+ for_each_cpu_wrap(cpu, cpus, target + 1) {
+ if (has_idle_core) {
+ i = select_idle_core(p, cpu, cpus, &idle_cpu);
+@@ -9300,6 +9312,77 @@ find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
+ return idlest;
+ }
+
++static void update_idle_cpu_scan(struct lb_env *env,
++ unsigned long sum_util)
++{
++ struct sched_domain_shared *sd_share;
++ int llc_weight, pct;
++ u64 x, y, tmp;
++ /*
++ * Update the number of CPUs to scan in LLC domain, which could
++ * be used as a hint in select_idle_cpu(). The update of sd_share
++ * could be expensive because it is within a shared cache line.
++ * So the write of this hint only occurs during periodic load
++ * balancing, rather than CPU_NEWLY_IDLE, because the latter
++ * can fire way more frequently than the former.
++ */
++ if (!sched_feat(SIS_UTIL) || env->idle == CPU_NEWLY_IDLE)
++ return;
++
++ llc_weight = per_cpu(sd_llc_size, env->dst_cpu);
++ if (env->sd->span_weight != llc_weight)
++ return;
++
++ sd_share = rcu_dereference(per_cpu(sd_llc_shared, env->dst_cpu));
++ if (!sd_share)
++ return;
++
++ /*
++ * The number of CPUs to search drops as sum_util increases, when
++ * sum_util hits 85% or above, the scan stops.
++ * The reason to choose 85% as the threshold is because this is the
++ * imbalance_pct(117) when a LLC sched group is overloaded.
++ *
++ * let y = SCHED_CAPACITY_SCALE - p * x^2 [1]
++ * and y'= y / SCHED_CAPACITY_SCALE
++ *
++ * x is the ratio of sum_util compared to the CPU capacity:
++ * x = sum_util / (llc_weight * SCHED_CAPACITY_SCALE)
++ * y' is the ratio of CPUs to be scanned in the LLC domain,
++ * and the number of CPUs to scan is calculated by:
++ *
++ * nr_scan = llc_weight * y' [2]
++ *
++ * When x hits the threshold of overloaded, AKA, when
++ * x = 100 / pct, y drops to 0. According to [1],
++ * p should be SCHED_CAPACITY_SCALE * pct^2 / 10000
++ *
++ * Scale x by SCHED_CAPACITY_SCALE:
++ * x' = sum_util / llc_weight; [3]
++ *
++ * and finally [1] becomes:
++ * y = SCHED_CAPACITY_SCALE -
++ * x'^2 * pct^2 / (10000 * SCHED_CAPACITY_SCALE) [4]
++ *
++ */
++ /* equation [3] */
++ x = sum_util;
++ do_div(x, llc_weight);
++
++ /* equation [4] */
++ pct = env->sd->imbalance_pct;
++ tmp = x * x * pct * pct;
++ do_div(tmp, 10000 * SCHED_CAPACITY_SCALE);
++ tmp = min_t(long, tmp, SCHED_CAPACITY_SCALE);
++ y = SCHED_CAPACITY_SCALE - tmp;
++
++ /* equation [2] */
++ y *= llc_weight;
++ do_div(y, SCHED_CAPACITY_SCALE);
++ if ((int)y != sd_share->nr_idle_scan)
++ WRITE_ONCE(sd_share->nr_idle_scan, (int)y);
++}
++
+ /**
+ * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
+ * @env: The load balancing environment.
+@@ -9312,6 +9395,7 @@ static inline void update_sd_lb_stats(struct lb_env *env, struct sd_lb_stats *sd
+ struct sched_group *sg = env->sd->groups;
+ struct sg_lb_stats *local = &sds->local_stat;
+ struct sg_lb_stats tmp_sgs;
++ unsigned long sum_util = 0;
+ int sg_status = 0;
+
+ do {
+@@ -9344,6 +9428,7 @@ static inline void update_sd_lb_stats(struct lb_env *env, struct sd_lb_stats *sd
+ sds->total_load += sgs->group_load;
+ sds->total_capacity += sgs->group_capacity;
+
++ sum_util += sgs->group_util;
+ sg = sg->next;
+ } while (sg != env->sd->groups);
+
+@@ -9369,6 +9454,8 @@ static inline void update_sd_lb_stats(struct lb_env *env, struct sd_lb_stats *sd
+ WRITE_ONCE(rd->overutilized, SG_OVERUTILIZED);
+ trace_sched_overutilized_tp(rd, SG_OVERUTILIZED);
+ }
++
++ update_idle_cpu_scan(env, sum_util);
+ }
+
+ #define NUMA_IMBALANCE_MIN 2
+diff --git a/kernel/sched/features.h b/kernel/sched/features.h
+index cfff560e0..d58baad2c 100644
+--- a/kernel/sched/features.h
++++ b/kernel/sched/features.h
+@@ -60,7 +60,8 @@ SCHED_FEAT(TTWU_QUEUE, true)
+ /*
+ * When doing wakeups, attempt to limit superfluous scans of the LLC domain.
+ */
+-SCHED_FEAT(SIS_PROP, true)
++SCHED_FEAT(SIS_PROP, false)
++SCHED_FEAT(SIS_UTIL, true)
+
+ /*
+ * Issue a WARN when we do multiple update_rq_clock() calls
+--
+2.37.0.rc0.15.g3b9a5a33c2
+
+
+From 26f365c39adda2c2cdcb478c985e15dd0299c303 Mon Sep 17 00:00:00 2001
+From: Peter Jung <[email protected]>
+Date: Tue, 28 Jun 2022 18:53:05 +0200
+Subject: [PATCH 4/5] improve-scan-efficiency-of-SIS
+
+Signed-off-by: Peter Jung <[email protected]>
+---
+ include/linux/sched/topology.h | 62 ++++++++-
+ kernel/sched/fair.c | 233 +++++++++++++++++++++++++++++----
+ kernel/sched/topology.c | 12 +-
+ 3 files changed, 277 insertions(+), 30 deletions(-)
+
+diff --git a/include/linux/sched/topology.h b/include/linux/sched/topology.h
+index 816df6cc4..6c757bdb1 100644
+--- a/include/linux/sched/topology.h
++++ b/include/linux/sched/topology.h
+@@ -77,11 +77,66 @@ extern int sched_domain_level_max;
+
+ struct sched_group;
+
++/*
++ * States of the sched-domain
++ *
++ * - sd_has_icores
++ * This state is only used in LLC domains to indicate worthy
++ * of a full scan in SIS due to idle cores available.
++ *
++ * - sd_has_icpus
++ * This state indicates that unoccupied (sched-idle/idle) cpus
++ * might exist in this domain. For the LLC domains it is the
++ * default state since these cpus are the main targets of SIS
++ * search, and is also used as a fallback state of the other
++ * states.
++ *
++ * - sd_may_idle
++ * This state implies the unstableness of the SIS filter, and
++ * some bits of it may out of date. This state is only used in
++ * SMT domains as an intermediate state between sd_has_icpus
++ * and sd_is_busy.
++ *
++ * - sd_is_busy
++ * This state indicates there are no unoccupied cpus in this
++ * domain. So for LLC domains, it gives the hint on whether
++ * we should put efforts on the SIS search or not.
++ *
++ * For LLC domains, sd_has_icores is set when the last non-idle cpu of
++ * a core becomes idle. After a full SIS scan and if no idle cores found,
++ * sd_has_icores must be cleared and the state will be set to sd_has_icpus
++ * or sd_is_busy depending on whether there is any idle cpu. And during
++ * load balancing on each SMT domain inside the LLC, the state will be
++ * re-evaluated and switch from sd_is_busy to sd_has_icpus if idle cpus
++ * exist.
++ *
++ * For SMT domains, the state is updated during load balancing at SMT
++ * level. Upper levels are ignored due to the long intervals that make
++ * information out-of-date quickly.
++ */
++enum sd_state {
++ sd_has_icores,
++ sd_has_icpus,
++ sd_may_idle,
++ sd_is_busy
++};
++
+ struct sched_domain_shared {
+ atomic_t ref;
+ atomic_t nr_busy_cpus;
+- int has_idle_cores;
++
++ int updating;
++ int state; /* see enum sd_state */
++
++ /*
++ * Record unoccupied cpus for SIS domain search.
++ *
++ * NOTE: this field is variable length. (Allocated dynamically
++ * by attaching extra space to the end of the structure,
++ * depending on how many CPUs the kernel has booted up with)
++ */
+ int nr_idle_scan;
++ unsigned long idle_cpus[];
+ };
+
+ struct sched_domain {
+@@ -167,6 +222,11 @@ static inline struct cpumask *sched_domain_span(struct sched_domain *sd)
+ return to_cpumask(sd->span);
+ }
+
++static inline struct cpumask *sched_domain_icpus(struct sched_domain_shared *sds)
++{
++ return to_cpumask(sds->idle_cpus);
++}
++
+ extern void partition_sched_domains_locked(int ndoms_new,
+ cpumask_var_t doms_new[],
+ struct sched_domain_attr *dattr_new);
+diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
+index 00da5ac47..6293ed90a 100644
+--- a/kernel/sched/fair.c
++++ b/kernel/sched/fair.c
+@@ -1577,11 +1577,11 @@ numa_type numa_classify(unsigned int imbalance_pct,
+
+ #ifdef CONFIG_SCHED_SMT
+ /* Forward declarations of select_idle_sibling helpers */
+-static inline bool test_idle_cores(int cpu, bool def);
++static inline bool test_idle_cores(int cpu);
+ static inline int numa_idle_core(int idle_core, int cpu)
+ {
+ if (!static_branch_likely(&sched_smt_present) ||
+- idle_core >= 0 || !test_idle_cores(cpu, false))
++ idle_core >= 0 || !test_idle_cores(cpu))
+ return idle_core;
+
+ /*
+@@ -5655,11 +5655,15 @@ static inline void update_overutilized_status(struct rq *rq)
+ static inline void update_overutilized_status(struct rq *rq) { }
+ #endif
+
++static int unoccupied_rq(struct rq *rq)
++{
++ return rq->nr_running == rq->cfs.idle_h_nr_running;
++}
++
+ /* Runqueue only has SCHED_IDLE tasks enqueued */
+ static int sched_idle_rq(struct rq *rq)
+ {
+- return unlikely(rq->nr_running == rq->cfs.idle_h_nr_running &&
+- rq->nr_running);
++ return unlikely(rq->nr_running && unoccupied_rq(rq));
+ }
+
+ /*
+@@ -6232,24 +6236,66 @@ static inline int __select_idle_cpu(int cpu, struct task_struct *p)
+ DEFINE_STATIC_KEY_FALSE(sched_smt_present);
+ EXPORT_SYMBOL_GPL(sched_smt_present);
+
+-static inline void set_idle_cores(int cpu, int val)
++static inline void sd_set_state(int cpu, enum sd_state state)
+ {
+ struct sched_domain_shared *sds;
+
+ sds = rcu_dereference(per_cpu(sd_llc_shared, cpu));
+ if (sds)
+- WRITE_ONCE(sds->has_idle_cores, val);
++ WRITE_ONCE(sds->state, state);
+ }
+
+-static inline bool test_idle_cores(int cpu, bool def)
++static inline enum sd_state sd_get_state(int cpu)
+ {
+ struct sched_domain_shared *sds;
+
+ sds = rcu_dereference(per_cpu(sd_llc_shared, cpu));
+ if (sds)
+- return READ_ONCE(sds->has_idle_cores);
++ return READ_ONCE(sds->state);
++
++ return sd_has_icpus;
++}
++
++static inline void set_idle_cores(int cpu, int idle)
++{
++ sd_set_state(cpu, idle ? sd_has_icores : sd_has_icpus);
++}
++
++static inline bool test_idle_cores(int cpu)
++{
++ return sd_get_state(cpu) == sd_has_icores;
++}
+
+- return def;
++static inline void set_idle_cpus(int cpu, int idle)
++{
++ sd_set_state(cpu, idle ? sd_has_icpus : sd_is_busy);
++}
++
++static inline bool test_idle_cpus(int cpu)
++{
++ return sd_get_state(cpu) != sd_is_busy;
++}
++
++static void sd_update_icpus(int core, int icpu)
++{
++ struct sched_domain_shared *sds;
++ struct cpumask *icpus;
++
++ sds = rcu_dereference(per_cpu(sd_llc_shared, core));
++ if (!sds)
++ return;
++
++ icpus = sched_domain_icpus(sds);
++
++ /*
++ * XXX: The update is racy between different cores.
++ * The non-atomic ops here is a tradeoff of accuracy
++ * for easing the cache traffic.
++ */
++ if (icpu == -1)
++ cpumask_andnot(icpus, icpus, cpu_smt_mask(core));
++ else if (!cpumask_test_cpu(icpu, icpus))
++ __cpumask_set_cpu(icpu, icpus);
+ }
+
+ /*
+@@ -6265,7 +6311,7 @@ void __update_idle_core(struct rq *rq)
+ int cpu;
+
+ rcu_read_lock();
+- if (test_idle_cores(core, true))
++ if (test_idle_cores(core))
+ goto unlock;
+
+ for_each_cpu(cpu, cpu_smt_mask(core)) {
+@@ -6291,9 +6337,6 @@ static int select_idle_core(struct task_struct *p, int core, struct cpumask *cpu
+ bool idle = true;
+ int cpu;
+
+- if (!static_branch_likely(&sched_smt_present))
+- return __select_idle_cpu(core, p);
+-
+ for_each_cpu(cpu, cpu_smt_mask(core)) {
+ if (!available_idle_cpu(cpu)) {
+ idle = false;
+@@ -6320,13 +6363,12 @@ static int select_idle_core(struct task_struct *p, int core, struct cpumask *cpu
+ /*
+ * Scan the local SMT mask for idle CPUs.
+ */
+-static int select_idle_smt(struct task_struct *p, struct sched_domain *sd, int target)
++static int select_idle_smt(struct task_struct *p, int target)
+ {
+ int cpu;
+
+- for_each_cpu(cpu, cpu_smt_mask(target)) {
+- if (!cpumask_test_cpu(cpu, p->cpus_ptr) ||
+- !cpumask_test_cpu(cpu, sched_domain_span(sd)))
++ for_each_cpu_and(cpu, cpu_smt_mask(target), p->cpus_ptr) {
++ if (cpu == target)
+ continue;
+ if (available_idle_cpu(cpu) || sched_idle_cpu(cpu))
+ return cpu;
+@@ -6337,13 +6379,26 @@ static int select_idle_smt(struct task_struct *p, struct sched_domain *sd, int t
+
+ #else /* CONFIG_SCHED_SMT */
+
+-static inline void set_idle_cores(int cpu, int val)
++static inline void set_idle_cores(int cpu, int idle)
+ {
+ }
+
+-static inline bool test_idle_cores(int cpu, bool def)
++static inline bool test_idle_cores(int cpu)
++{
++ return false;
++}
++
++static inline void set_idle_cpus(int cpu, int idle)
++{
++}
++
++static inline bool test_idle_cpus(int cpu)
++{
++ return true;
++}
++
++static inline void sd_update_icpus(int core, int icpu)
+ {
+- return def;
+ }
+
+ static inline int select_idle_core(struct task_struct *p, int core, struct cpumask *cpus, int *idle_cpu)
+@@ -6351,7 +6406,7 @@ static inline int select_idle_core(struct task_struct *p, int core, struct cpuma
+ return __select_idle_cpu(core, p);
+ }
+
+-static inline int select_idle_smt(struct task_struct *p, struct sched_domain *sd, int target)
++static inline int select_idle_smt(struct task_struct *p, int target)
+ {
+ return -1;
+ }
+@@ -6377,7 +6432,8 @@ static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool
+ if (!this_sd)
+ return -1;
+
+- cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr);
++ cpumask_and(cpus, has_idle_core ? sched_domain_span(sd) :
++ sched_domain_icpus(sd->shared), p->cpus_ptr);
+
+ if (sched_feat(SIS_PROP) && !has_idle_core) {
+ u64 avg_cost, avg_idle, span_avg;
+@@ -6433,7 +6489,9 @@ static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool
+ }
+ }
+
+- if (has_idle_core)
++ if (idle_cpu == -1)
++ set_idle_cpus(target, false);
++ else if (has_idle_core)
+ set_idle_cores(target, false);
+
+ if (sched_feat(SIS_PROP) && !has_idle_core) {
+@@ -6582,13 +6640,16 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target)
+ return target;
+
+ if (sched_smt_active()) {
+- has_idle_core = test_idle_cores(target, false);
++ has_idle_core = test_idle_cores(target);
+
+ if (!has_idle_core && cpus_share_cache(prev, target)) {
+- i = select_idle_smt(p, sd, prev);
++ i = select_idle_smt(p, prev);
+ if ((unsigned int)i < nr_cpumask_bits)
+ return i;
+ }
++
++ if (!has_idle_core && !test_idle_cpus(target))
++ return target;
+ }
+
+ i = select_idle_cpu(p, sd, has_idle_core, target);
+@@ -8401,6 +8462,9 @@ struct sd_lb_stats {
+ unsigned long avg_load; /* Average load across all groups in sd */
+ unsigned int prefer_sibling; /* tasks should go to sibling first */
+
++ int sd_state;
++ int idle_cpu;
++
+ struct sg_lb_stats busiest_stat;/* Statistics of the busiest group */
+ struct sg_lb_stats local_stat; /* Statistics of the local group */
+ };
+@@ -8419,6 +8483,8 @@ static inline void init_sd_lb_stats(struct sd_lb_stats *sds)
+ .local = NULL,
+ .total_load = 0UL,
+ .total_capacity = 0UL,
++ .sd_state = sd_is_busy,
++ .idle_cpu = -1,
+ .busiest_stat = {
+ .idle_cpus = UINT_MAX,
+ .group_type = group_has_spare,
+@@ -8759,6 +8825,20 @@ sched_asym(struct lb_env *env, struct sd_lb_stats *sds, struct sg_lb_stats *sgs
+ return sched_asym_prefer(env->dst_cpu, group->asym_prefer_cpu);
+ }
+
++static inline void sd_classify(struct sd_lb_stats *sds, struct rq *rq, int cpu)
++{
++ if (sds->sd_state != sd_has_icpus && unoccupied_rq(rq)) {
++ /*
++ * Prefer idle cpus than unoccupied ones. This
++ * is achieved by only allowing the idle ones
++ * unconditionally overwrite the preious record
++ * while the occupied ones can't.
++ */
++ sds->idle_cpu = cpu;
++ sds->sd_state = sd_has_icpus;
++ }
++}
++
+ /**
+ * update_sg_lb_stats - Update sched_group's statistics for load balancing.
+ * @env: The load balancing environment.
+@@ -8773,14 +8853,16 @@ static inline void update_sg_lb_stats(struct lb_env *env,
+ struct sg_lb_stats *sgs,
+ int *sg_status)
+ {
+- int i, nr_running, local_group;
++ int i, nr_running, local_group, update_core;
+
+ memset(sgs, 0, sizeof(*sgs));
+
+ local_group = group == sds->local;
++ update_core = env->sd->flags & SD_SHARE_CPUCAPACITY;
+
+ for_each_cpu_and(i, sched_group_span(group), env->cpus) {
+ struct rq *rq = cpu_rq(i);
++ bool update = update_core && (env->dst_cpu != i);
+
+ sgs->group_load += cpu_load(rq);
+ sgs->group_util += cpu_util_cfs(i);
+@@ -8790,6 +8872,13 @@ static inline void update_sg_lb_stats(struct lb_env *env,
+ nr_running = rq->nr_running;
+ sgs->sum_nr_running += nr_running;
+
++ /*
++ * The dst_cpu is not preferred since it might
++ * be fed with tasks soon.
++ */
++ if (update)
++ sd_classify(sds, rq, i);
++
+ if (nr_running > 1)
+ *sg_status |= SG_OVERLOAD;
+
+@@ -8804,7 +8893,17 @@ static inline void update_sg_lb_stats(struct lb_env *env,
+ * No need to call idle_cpu() if nr_running is not 0
+ */
+ if (!nr_running && idle_cpu(i)) {
++ /*
++ * Prefer the last idle cpu by overwriting
++ * preious one. The first idle cpu in this
++ * domain (if any) can trigger balancing
++ * and fed with tasks, so we'd better choose
++ * a candidate in an opposite way.
++ */
++ if (update)
++ sds->idle_cpu = i;
+ sgs->idle_cpus++;
++
+ /* Idle cpu can't have misfit task */
+ continue;
+ }
+@@ -9383,6 +9482,83 @@ static void update_idle_cpu_scan(struct lb_env *env,
+ WRITE_ONCE(sd_share->nr_idle_scan, (int)y);
+ }
+
++static void sd_update_state(struct lb_env *env, struct sd_lb_stats *sds)
++{
++ struct sched_domain_shared *sd_smt_shared = env->sd->shared;
++ enum sd_state new = sds->sd_state;
++ int icpu = sds->idle_cpu, this = env->dst_cpu;
++
++ /*
++ * Parallel updating can hardly contribute accuracy to
++ * the filter, besides it can be one of the burdens on
++ * cache traffic.
++ */
++ if (cmpxchg(&sd_smt_shared->updating, 0, 1))
++ return;
++
++ /*
++ * The dst_cpu is likely to be fed with tasks soon.
++ * If it is the only unoccupied cpu in this domain,
++ * we still handle it the same way as as_has_icpus
++ * but turn the SMT into the unstable state, rather
++ * than waiting to the end of load balancing since
++ * it's also important that update the filter as
++ * early as possible to keep it fresh.
++ */
++ if (new == sd_is_busy) {
++ if (idle_cpu(this) || sched_idle_cpu(this)) {
++ new = sd_may_idle;
++ icpu = this;
++ }
++ }
++
++ /*
++ * There is at least one unoccupied cpu available, so
++ * propagate it to the filter to avoid false negative
++ * issue which could result in lost tracking of some
++ * idle cpus thus throughupt downgraded.
++ */
++ if (new != sd_is_busy) {
++ /*
++ * The sd_may_idle state is taken into
++ * consideration as well because from
++ * here we couldn't actually know task
++ * migrations would happen or not.
++ */
++ if (!test_idle_cpus(this))
++ set_idle_cpus(this, true);
++ } else {
++ /*
++ * Nothing changes so nothing to update or
++ * propagate.
++ */
++ if (sd_smt_shared->state == sd_is_busy)
++ goto out;
++
++ /*
++ * Allow false positive to exist for some time
++ * to make a tradeoff of accuracy of the filter
++ * for relieving cache traffic.
++ */
++ if (sd_smt_shared->state == sd_has_icpus) {
++ new = sd_may_idle;
++ goto update;
++ }
++
++ /*
++ * If the false positive issue has already been
++ * there for a while, a correction of the filter
++ * is needed.
++ */
++ }
++
++ sd_update_icpus(this, sds->idle_cpu);
++update:
++ sd_smt_shared->state = new;
++out:
++ xchg(&sd_smt_shared->updating, 0);
++}
++
+ /**
+ * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
+ * @env: The load balancing environment.
+@@ -9435,8 +9611,9 @@ static inline void update_sd_lb_stats(struct lb_env *env, struct sd_lb_stats *sd
+ /* Tag domain that child domain prefers tasks go to siblings first */
+ sds->prefer_sibling = child && child->flags & SD_PREFER_SIBLING;
+
+-
+- if (env->sd->flags & SD_NUMA)
++ if (env->sd->flags & SD_SHARE_CPUCAPACITY)
++ sd_update_state(env, sds);
++ else if (env->sd->flags & SD_NUMA)
+ env->fbq_type = fbq_classify_group(&sds->busiest_stat);
+
+ if (!env->sd->parent) {
+diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c
+index 810750e62..482f5da00 100644
+--- a/kernel/sched/topology.c
++++ b/kernel/sched/topology.c
+@@ -1620,6 +1620,16 @@ sd_init(struct sched_domain_topology_level *tl,
+ sd->shared = *per_cpu_ptr(sdd->sds, sd_id);
+ atomic_inc(&sd->shared->ref);
+ atomic_set(&sd->shared->nr_busy_cpus, sd_weight);
++
++ /*
++ * Initialize SMT domains to be busy, so that we don't
++ * need to propagate idle cpus to LLC domains which are
++ * default to fully busy (no cpus set). This will be
++ * updated in the first load balancing on SMT domains
++ * if necessary.
++ */
++ if (sd->flags & SD_SHARE_CPUCAPACITY)
++ WRITE_ONCE(sd->shared->state, sd_is_busy);
+ }
+
+ sd->private = sdd;
+@@ -2085,7 +2095,7 @@ static int __sdt_alloc(const struct cpumask *cpu_map)
+
+ *per_cpu_ptr(sdd->sd, j) = sd;
+
+- sds = kzalloc_node(sizeof(struct sched_domain_shared),
++ sds = kzalloc_node(sizeof(struct sched_domain_shared) + cpumask_size(),
+ GFP_KERNEL, cpu_to_node(j));
+ if (!sds)
+ return -ENOMEM;
+--
+2.37.0.rc0.15.g3b9a5a33c2
+
+
+From 2e7d9aba576a7509a9b2d00131b787c58ab5be85 Mon Sep 17 00:00:00 2001
+From: Piotr Gorski <[email protected]>
+Date: Wed, 29 Jun 2022 18:30:15 +0200
+Subject: [PATCH 5/5] Revert "improve-scan-efficiency-of-SIS"
+
+tt-scheduler requires "has_idle_cores" which this patch removes.
+
+This reverts commit 7d91bc7c1c6cb64532fa6dbc2956e03b2b9987f0.
+
+Signed-off-by: Piotr Gorski <[email protected]>
+---
+ include/linux/sched/topology.h | 62 +--------
+ kernel/sched/fair.c | 233 ++++-----------------------------
+ kernel/sched/topology.c | 12 +-
+ 3 files changed, 30 insertions(+), 277 deletions(-)
+
+diff --git a/include/linux/sched/topology.h b/include/linux/sched/topology.h
+index 6c757bdb1..816df6cc4 100644
+--- a/include/linux/sched/topology.h
++++ b/include/linux/sched/topology.h
+@@ -77,66 +77,11 @@ extern int sched_domain_level_max;
+
+ struct sched_group;
+
+-/*
+- * States of the sched-domain
+- *
+- * - sd_has_icores
+- * This state is only used in LLC domains to indicate worthy
+- * of a full scan in SIS due to idle cores available.
+- *
+- * - sd_has_icpus
+- * This state indicates that unoccupied (sched-idle/idle) cpus
+- * might exist in this domain. For the LLC domains it is the
+- * default state since these cpus are the main targets of SIS
+- * search, and is also used as a fallback state of the other
+- * states.
+- *
+- * - sd_may_idle
+- * This state implies the unstableness of the SIS filter, and
+- * some bits of it may out of date. This state is only used in
+- * SMT domains as an intermediate state between sd_has_icpus
+- * and sd_is_busy.
+- *
+- * - sd_is_busy
+- * This state indicates there are no unoccupied cpus in this
+- * domain. So for LLC domains, it gives the hint on whether
+- * we should put efforts on the SIS search or not.
+- *
+- * For LLC domains, sd_has_icores is set when the last non-idle cpu of
+- * a core becomes idle. After a full SIS scan and if no idle cores found,
+- * sd_has_icores must be cleared and the state will be set to sd_has_icpus
+- * or sd_is_busy depending on whether there is any idle cpu. And during
+- * load balancing on each SMT domain inside the LLC, the state will be
+- * re-evaluated and switch from sd_is_busy to sd_has_icpus if idle cpus
+- * exist.
+- *
+- * For SMT domains, the state is updated during load balancing at SMT
+- * level. Upper levels are ignored due to the long intervals that make
+- * information out-of-date quickly.
+- */
+-enum sd_state {
+- sd_has_icores,
+- sd_has_icpus,
+- sd_may_idle,
+- sd_is_busy
+-};
+-
+ struct sched_domain_shared {
+ atomic_t ref;
+ atomic_t nr_busy_cpus;
+-
+- int updating;
+- int state; /* see enum sd_state */
+-
+- /*
+- * Record unoccupied cpus for SIS domain search.
+- *
+- * NOTE: this field is variable length. (Allocated dynamically
+- * by attaching extra space to the end of the structure,
+- * depending on how many CPUs the kernel has booted up with)
+- */
++ int has_idle_cores;
+ int nr_idle_scan;
+- unsigned long idle_cpus[];
+ };
+
+ struct sched_domain {
+@@ -222,11 +167,6 @@ static inline struct cpumask *sched_domain_span(struct sched_domain *sd)
+ return to_cpumask(sd->span);
+ }
+
+-static inline struct cpumask *sched_domain_icpus(struct sched_domain_shared *sds)
+-{
+- return to_cpumask(sds->idle_cpus);
+-}
+-
+ extern void partition_sched_domains_locked(int ndoms_new,
+ cpumask_var_t doms_new[],
+ struct sched_domain_attr *dattr_new);
+diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
+index 6293ed90a..00da5ac47 100644
+--- a/kernel/sched/fair.c
++++ b/kernel/sched/fair.c
+@@ -1577,11 +1577,11 @@ numa_type numa_classify(unsigned int imbalance_pct,
+
+ #ifdef CONFIG_SCHED_SMT
+ /* Forward declarations of select_idle_sibling helpers */
+-static inline bool test_idle_cores(int cpu);
++static inline bool test_idle_cores(int cpu, bool def);
+ static inline int numa_idle_core(int idle_core, int cpu)
+ {
+ if (!static_branch_likely(&sched_smt_present) ||
+- idle_core >= 0 || !test_idle_cores(cpu))
++ idle_core >= 0 || !test_idle_cores(cpu, false))
+ return idle_core;
+
+ /*
+@@ -5655,15 +5655,11 @@ static inline void update_overutilized_status(struct rq *rq)
+ static inline void update_overutilized_status(struct rq *rq) { }
+ #endif
+
+-static int unoccupied_rq(struct rq *rq)
+-{
+- return rq->nr_running == rq->cfs.idle_h_nr_running;
+-}
+-
+ /* Runqueue only has SCHED_IDLE tasks enqueued */
+ static int sched_idle_rq(struct rq *rq)
+ {
+- return unlikely(rq->nr_running && unoccupied_rq(rq));
++ return unlikely(rq->nr_running == rq->cfs.idle_h_nr_running &&
++ rq->nr_running);
+ }
+
+ /*
+@@ -6236,66 +6232,24 @@ static inline int __select_idle_cpu(int cpu, struct task_struct *p)
+ DEFINE_STATIC_KEY_FALSE(sched_smt_present);
+ EXPORT_SYMBOL_GPL(sched_smt_present);
+
+-static inline void sd_set_state(int cpu, enum sd_state state)
++static inline void set_idle_cores(int cpu, int val)
+ {
+ struct sched_domain_shared *sds;
+
+ sds = rcu_dereference(per_cpu(sd_llc_shared, cpu));
+ if (sds)
+- WRITE_ONCE(sds->state, state);
++ WRITE_ONCE(sds->has_idle_cores, val);
+ }
+
+-static inline enum sd_state sd_get_state(int cpu)
++static inline bool test_idle_cores(int cpu, bool def)
+ {
+ struct sched_domain_shared *sds;
+
+ sds = rcu_dereference(per_cpu(sd_llc_shared, cpu));
+ if (sds)
+- return READ_ONCE(sds->state);
+-
+- return sd_has_icpus;
+-}
+-
+-static inline void set_idle_cores(int cpu, int idle)
+-{
+- sd_set_state(cpu, idle ? sd_has_icores : sd_has_icpus);
+-}
+-
+-static inline bool test_idle_cores(int cpu)
+-{
+- return sd_get_state(cpu) == sd_has_icores;
+-}
++ return READ_ONCE(sds->has_idle_cores);
+
+-static inline void set_idle_cpus(int cpu, int idle)
+-{
+- sd_set_state(cpu, idle ? sd_has_icpus : sd_is_busy);
+-}
+-
+-static inline bool test_idle_cpus(int cpu)
+-{
+- return sd_get_state(cpu) != sd_is_busy;
+-}
+-
+-static void sd_update_icpus(int core, int icpu)
+-{
+- struct sched_domain_shared *sds;
+- struct cpumask *icpus;
+-
+- sds = rcu_dereference(per_cpu(sd_llc_shared, core));
+- if (!sds)
+- return;
+-
+- icpus = sched_domain_icpus(sds);
+-
+- /*
+- * XXX: The update is racy between different cores.
+- * The non-atomic ops here is a tradeoff of accuracy
+- * for easing the cache traffic.
+- */
+- if (icpu == -1)
+- cpumask_andnot(icpus, icpus, cpu_smt_mask(core));
+- else if (!cpumask_test_cpu(icpu, icpus))
+- __cpumask_set_cpu(icpu, icpus);
++ return def;
+ }
+
+ /*
+@@ -6311,7 +6265,7 @@ void __update_idle_core(struct rq *rq)
+ int cpu;
+
+ rcu_read_lock();
+- if (test_idle_cores(core))
++ if (test_idle_cores(core, true))
+ goto unlock;
+
+ for_each_cpu(cpu, cpu_smt_mask(core)) {
+@@ -6337,6 +6291,9 @@ static int select_idle_core(struct task_struct *p, int core, struct cpumask *cpu
+ bool idle = true;
+ int cpu;
+
++ if (!static_branch_likely(&sched_smt_present))
++ return __select_idle_cpu(core, p);
++
+ for_each_cpu(cpu, cpu_smt_mask(core)) {
+ if (!available_idle_cpu(cpu)) {
+ idle = false;
+@@ -6363,12 +6320,13 @@ static int select_idle_core(struct task_struct *p, int core, struct cpumask *cpu
+ /*
+ * Scan the local SMT mask for idle CPUs.
+ */
+-static int select_idle_smt(struct task_struct *p, int target)
++static int select_idle_smt(struct task_struct *p, struct sched_domain *sd, int target)
+ {
+ int cpu;
+
+- for_each_cpu_and(cpu, cpu_smt_mask(target), p->cpus_ptr) {
+- if (cpu == target)
++ for_each_cpu(cpu, cpu_smt_mask(target)) {
++ if (!cpumask_test_cpu(cpu, p->cpus_ptr) ||
++ !cpumask_test_cpu(cpu, sched_domain_span(sd)))
+ continue;
+ if (available_idle_cpu(cpu) || sched_idle_cpu(cpu))
+ return cpu;
+@@ -6379,26 +6337,13 @@ static int select_idle_smt(struct task_struct *p, int target)
+
+ #else /* CONFIG_SCHED_SMT */
+
+-static inline void set_idle_cores(int cpu, int idle)
++static inline void set_idle_cores(int cpu, int val)
+ {
+ }
+
+-static inline bool test_idle_cores(int cpu)
+-{
+- return false;
+-}
+-
+-static inline void set_idle_cpus(int cpu, int idle)
+-{
+-}
+-
+-static inline bool test_idle_cpus(int cpu)
+-{
+- return true;
+-}
+-
+-static inline void sd_update_icpus(int core, int icpu)
++static inline bool test_idle_cores(int cpu, bool def)
+ {
++ return def;
+ }
+
+ static inline int select_idle_core(struct task_struct *p, int core, struct cpumask *cpus, int *idle_cpu)
+@@ -6406,7 +6351,7 @@ static inline int select_idle_core(struct task_struct *p, int core, struct cpuma
+ return __select_idle_cpu(core, p);
+ }
+
+-static inline int select_idle_smt(struct task_struct *p, int target)
++static inline int select_idle_smt(struct task_struct *p, struct sched_domain *sd, int target)
+ {
+ return -1;
+ }
+@@ -6432,8 +6377,7 @@ static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool
+ if (!this_sd)
+ return -1;
+
+- cpumask_and(cpus, has_idle_core ? sched_domain_span(sd) :
+- sched_domain_icpus(sd->shared), p->cpus_ptr);
++ cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr);
+
+ if (sched_feat(SIS_PROP) && !has_idle_core) {
+ u64 avg_cost, avg_idle, span_avg;
+@@ -6489,9 +6433,7 @@ static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool
+ }
+ }
+
+- if (idle_cpu == -1)
+- set_idle_cpus(target, false);
+- else if (has_idle_core)
++ if (has_idle_core)
+ set_idle_cores(target, false);
+
+ if (sched_feat(SIS_PROP) && !has_idle_core) {
+@@ -6640,16 +6582,13 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target)
+ return target;
+
+ if (sched_smt_active()) {
+- has_idle_core = test_idle_cores(target);
++ has_idle_core = test_idle_cores(target, false);
+
+ if (!has_idle_core && cpus_share_cache(prev, target)) {
+- i = select_idle_smt(p, prev);
++ i = select_idle_smt(p, sd, prev);
+ if ((unsigned int)i < nr_cpumask_bits)
+ return i;
+ }
+-
+- if (!has_idle_core && !test_idle_cpus(target))
+- return target;
+ }
+
+ i = select_idle_cpu(p, sd, has_idle_core, target);
+@@ -8462,9 +8401,6 @@ struct sd_lb_stats {
+ unsigned long avg_load; /* Average load across all groups in sd */
+ unsigned int prefer_sibling; /* tasks should go to sibling first */
+
+- int sd_state;
+- int idle_cpu;
+-
+ struct sg_lb_stats busiest_stat;/* Statistics of the busiest group */
+ struct sg_lb_stats local_stat; /* Statistics of the local group */
+ };
+@@ -8483,8 +8419,6 @@ static inline void init_sd_lb_stats(struct sd_lb_stats *sds)
+ .local = NULL,
+ .total_load = 0UL,
+ .total_capacity = 0UL,
+- .sd_state = sd_is_busy,
+- .idle_cpu = -1,
+ .busiest_stat = {
+ .idle_cpus = UINT_MAX,
+ .group_type = group_has_spare,
+@@ -8825,20 +8759,6 @@ sched_asym(struct lb_env *env, struct sd_lb_stats *sds, struct sg_lb_stats *sgs
+ return sched_asym_prefer(env->dst_cpu, group->asym_prefer_cpu);
+ }
+
+-static inline void sd_classify(struct sd_lb_stats *sds, struct rq *rq, int cpu)
+-{
+- if (sds->sd_state != sd_has_icpus && unoccupied_rq(rq)) {
+- /*
+- * Prefer idle cpus than unoccupied ones. This
+- * is achieved by only allowing the idle ones
+- * unconditionally overwrite the preious record
+- * while the occupied ones can't.
+- */
+- sds->idle_cpu = cpu;
+- sds->sd_state = sd_has_icpus;
+- }
+-}
+-
+ /**
+ * update_sg_lb_stats - Update sched_group's statistics for load balancing.
+ * @env: The load balancing environment.
+@@ -8853,16 +8773,14 @@ static inline void update_sg_lb_stats(struct lb_env *env,
+ struct sg_lb_stats *sgs,
+ int *sg_status)
+ {
+- int i, nr_running, local_group, update_core;
++ int i, nr_running, local_group;
+
+ memset(sgs, 0, sizeof(*sgs));
+
+ local_group = group == sds->local;
+- update_core = env->sd->flags & SD_SHARE_CPUCAPACITY;
+
+ for_each_cpu_and(i, sched_group_span(group), env->cpus) {
+ struct rq *rq = cpu_rq(i);
+- bool update = update_core && (env->dst_cpu != i);
+
+ sgs->group_load += cpu_load(rq);
+ sgs->group_util += cpu_util_cfs(i);
+@@ -8872,13 +8790,6 @@ static inline void update_sg_lb_stats(struct lb_env *env,
+ nr_running = rq->nr_running;
+ sgs->sum_nr_running += nr_running;
+
+- /*
+- * The dst_cpu is not preferred since it might
+- * be fed with tasks soon.
+- */
+- if (update)
+- sd_classify(sds, rq, i);
+-
+ if (nr_running > 1)
+ *sg_status |= SG_OVERLOAD;
+
+@@ -8893,17 +8804,7 @@ static inline void update_sg_lb_stats(struct lb_env *env,
+ * No need to call idle_cpu() if nr_running is not 0
+ */
+ if (!nr_running && idle_cpu(i)) {
+- /*
+- * Prefer the last idle cpu by overwriting
+- * preious one. The first idle cpu in this
+- * domain (if any) can trigger balancing
+- * and fed with tasks, so we'd better choose
+- * a candidate in an opposite way.
+- */
+- if (update)
+- sds->idle_cpu = i;
+ sgs->idle_cpus++;
+-
+ /* Idle cpu can't have misfit task */
+ continue;
+ }
+@@ -9482,83 +9383,6 @@ static void update_idle_cpu_scan(struct lb_env *env,
+ WRITE_ONCE(sd_share->nr_idle_scan, (int)y);
+ }
+
+-static void sd_update_state(struct lb_env *env, struct sd_lb_stats *sds)
+-{
+- struct sched_domain_shared *sd_smt_shared = env->sd->shared;
+- enum sd_state new = sds->sd_state;
+- int icpu = sds->idle_cpu, this = env->dst_cpu;
+-
+- /*
+- * Parallel updating can hardly contribute accuracy to
+- * the filter, besides it can be one of the burdens on
+- * cache traffic.
+- */
+- if (cmpxchg(&sd_smt_shared->updating, 0, 1))
+- return;
+-
+- /*
+- * The dst_cpu is likely to be fed with tasks soon.
+- * If it is the only unoccupied cpu in this domain,
+- * we still handle it the same way as as_has_icpus
+- * but turn the SMT into the unstable state, rather
+- * than waiting to the end of load balancing since
+- * it's also important that update the filter as
+- * early as possible to keep it fresh.
+- */
+- if (new == sd_is_busy) {
+- if (idle_cpu(this) || sched_idle_cpu(this)) {
+- new = sd_may_idle;
+- icpu = this;
+- }
+- }
+-
+- /*
+- * There is at least one unoccupied cpu available, so
+- * propagate it to the filter to avoid false negative
+- * issue which could result in lost tracking of some
+- * idle cpus thus throughupt downgraded.
+- */
+- if (new != sd_is_busy) {
+- /*
+- * The sd_may_idle state is taken into
+- * consideration as well because from
+- * here we couldn't actually know task
+- * migrations would happen or not.
+- */
+- if (!test_idle_cpus(this))
+- set_idle_cpus(this, true);
+- } else {
+- /*
+- * Nothing changes so nothing to update or
+- * propagate.
+- */
+- if (sd_smt_shared->state == sd_is_busy)
+- goto out;
+-
+- /*
+- * Allow false positive to exist for some time
+- * to make a tradeoff of accuracy of the filter
+- * for relieving cache traffic.
+- */
+- if (sd_smt_shared->state == sd_has_icpus) {
+- new = sd_may_idle;
+- goto update;
+- }
+-
+- /*
+- * If the false positive issue has already been
+- * there for a while, a correction of the filter
+- * is needed.
+- */
+- }
+-
+- sd_update_icpus(this, sds->idle_cpu);
+-update:
+- sd_smt_shared->state = new;
+-out:
+- xchg(&sd_smt_shared->updating, 0);
+-}
+-
+ /**
+ * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
+ * @env: The load balancing environment.
+@@ -9611,9 +9435,8 @@ static inline void update_sd_lb_stats(struct lb_env *env, struct sd_lb_stats *sd
+ /* Tag domain that child domain prefers tasks go to siblings first */
+ sds->prefer_sibling = child && child->flags & SD_PREFER_SIBLING;
+
+- if (env->sd->flags & SD_SHARE_CPUCAPACITY)
+- sd_update_state(env, sds);
+- else if (env->sd->flags & SD_NUMA)
++
++ if (env->sd->flags & SD_NUMA)
+ env->fbq_type = fbq_classify_group(&sds->busiest_stat);
+
+ if (!env->sd->parent) {
+diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c
+index 482f5da00..810750e62 100644
+--- a/kernel/sched/topology.c
++++ b/kernel/sched/topology.c
+@@ -1620,16 +1620,6 @@ sd_init(struct sched_domain_topology_level *tl,
+ sd->shared = *per_cpu_ptr(sdd->sds, sd_id);
+ atomic_inc(&sd->shared->ref);
+ atomic_set(&sd->shared->nr_busy_cpus, sd_weight);
+-
+- /*
+- * Initialize SMT domains to be busy, so that we don't
+- * need to propagate idle cpus to LLC domains which are
+- * default to fully busy (no cpus set). This will be
+- * updated in the first load balancing on SMT domains
+- * if necessary.
+- */
+- if (sd->flags & SD_SHARE_CPUCAPACITY)
+- WRITE_ONCE(sd->shared->state, sd_is_busy);
+ }
+
+ sd->private = sdd;
+@@ -2095,7 +2085,7 @@ static int __sdt_alloc(const struct cpumask *cpu_map)
+
+ *per_cpu_ptr(sdd->sd, j) = sd;
+
+- sds = kzalloc_node(sizeof(struct sched_domain_shared) + cpumask_size(),
++ sds = kzalloc_node(sizeof(struct sched_domain_shared),
+ GFP_KERNEL, cpu_to_node(j));
+ if (!sds)
+ return -ENOMEM;
+--
+2.37.0.rc0.15.g3b9a5a33c2
+
diff --git a/linux-cachyos-tt/PKGBUILD b/linux-cachyos-tt/PKGBUILD
index c75d6f0d..6d3ea613 100644
--- a/linux-cachyos-tt/PKGBUILD
+++ b/linux-cachyos-tt/PKGBUILD
@@ -140,7 +140,7 @@ _srcname=linux-${_stable}
#_srcname=linux-${_major}
arch=(x86_64 x86_64_v3)
pkgdesc='Linux TT scheduler Kernel by CachyOS with other patches and improvements'
-pkgrel=2
+pkgrel=3
_kernver=$pkgver-$pkgrel
arch=('x86_64' 'x86_64_v3')
url="https://github.com/CachyOS/linux-cachyos"
@@ -161,6 +161,7 @@ source=(
"https://cdn.kernel.org/pub/linux/kernel/v${pkgver%%.*}.x/${_srcname}.tar.xz"
"config"
#"${_patchsource}/0001-cachy.patch"
+ '0001-cachy-tt.patch'
)
if [ -n "$_build_zfs" ]; then
source+=("git+https://github.com/openzfs/zfs.git#commit=6c3c5fcfbe27d9193cd131753cc7e47ee2784621")
@@ -806,6 +807,7 @@ done
sha256sums=('0823eb05dba001cf9e2560a76dfa4d81c854e1dcfbfb25cc73ba64cd7e900a36'
'58a21aaa4dda3d7cd3c617fd655782b35b27ef52c9e55157d5b2092dba61c5ab'
+ '9b2dd4b454d7d57522ed0227df3b3c230d254ba3c3e59dc8da619f5f3372bf24'
'dcf5df8bd041a44d8657effc6cd13477957b100110038875ae7872c515e3d680'
'326d129f9435145add756dc967accd56ffe1d8ff1b6650f84d2578c41bd6dfd6'
'dc2898751118804bc3f36b5a6928a2927d04919ce41c0ce013009f5564d6d232'