aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan200101 <sentrycraft123@gmail.com>2024-07-18 10:58:24 +0200
committerJan200101 <sentrycraft123@gmail.com>2024-07-18 10:58:24 +0200
commite76966a10a7754f8d716cc17d752e43a474d5c94 (patch)
tree129aa1b4056b6b567f2189c21b839266fb3ca02c
parent82bbf281341ef6fdc89bf3cd4b8f9e49884deccd (diff)
downloadkernel-fsync-e76966a10a7754f8d716cc17d752e43a474d5c94.tar.gz
kernel-fsync-e76966a10a7754f8d716cc17d752e43a474d5c94.zip
kernel 6.9.9 updated patchset
-rw-r--r--SOURCES/0001-add-ally-x-dmi-quirk-for-controller-suspend.patch46
-rw-r--r--SOURCES/0001-amd-hdr.patch208
-rw-r--r--SOURCES/0001-amd-pstate.patch561
-rw-r--r--SOURCES/0001-ntsync.patch30
-rw-r--r--SOURCES/cachy-bbr3.patch3386
-rw-r--r--SOURCES/cachy-fixes.patch573
-rw-r--r--SOURCES/kernel-aarch64-16k-debug-fedora.config1
-rw-r--r--SOURCES/kernel-aarch64-16k-fedora.config1
-rw-r--r--SOURCES/kernel-aarch64-64k-debug-rhel.config1
-rw-r--r--SOURCES/kernel-aarch64-64k-rhel.config1
-rw-r--r--SOURCES/kernel-aarch64-debug-fedora.config1
-rw-r--r--SOURCES/kernel-aarch64-debug-rhel.config1
-rw-r--r--SOURCES/kernel-aarch64-fedora.config1
-rw-r--r--SOURCES/kernel-aarch64-rhel.config1
-rw-r--r--SOURCES/kernel-aarch64-rt-debug-rhel.config1
-rw-r--r--SOURCES/kernel-aarch64-rt-rhel.config1
-rw-r--r--SOURCES/kernel-ppc64le-debug-fedora.config1
-rw-r--r--SOURCES/kernel-ppc64le-debug-rhel.config1
-rw-r--r--SOURCES/kernel-ppc64le-fedora.config1
-rw-r--r--SOURCES/kernel-ppc64le-rhel.config1
-rw-r--r--SOURCES/kernel-s390x-debug-fedora.config1
-rw-r--r--SOURCES/kernel-s390x-debug-rhel.config1
-rw-r--r--SOURCES/kernel-s390x-fedora.config1
-rw-r--r--SOURCES/kernel-s390x-rhel.config1
-rw-r--r--SOURCES/kernel-s390x-zfcpdump-rhel.config1
-rw-r--r--SOURCES/kernel-x86_64-debug-fedora.config1
-rw-r--r--SOURCES/kernel-x86_64-debug-rhel.config1
-rw-r--r--SOURCES/kernel-x86_64-fedora.config1
-rw-r--r--SOURCES/kernel-x86_64-rhel.config1
-rw-r--r--SOURCES/kernel-x86_64-rt-debug-rhel.config1
-rw-r--r--SOURCES/kernel-x86_64-rt-rhel.config1
-rw-r--r--SOURCES/tkg-misc-additions.patch154
-rw-r--r--SPECS/kernel.spec11
-rwxr-xr-xTOOLS/patch_configs.py4
34 files changed, 4358 insertions, 640 deletions
diff --git a/SOURCES/0001-add-ally-x-dmi-quirk-for-controller-suspend.patch b/SOURCES/0001-add-ally-x-dmi-quirk-for-controller-suspend.patch
index e0753ae..bffa8c5 100644
--- a/SOURCES/0001-add-ally-x-dmi-quirk-for-controller-suspend.patch
+++ b/SOURCES/0001-add-ally-x-dmi-quirk-for-controller-suspend.patch
@@ -1,48 +1,28 @@
-From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
-From: Jan200101 <sentrycraft123@gmail.com>
-Date: Mon, 15 Jul 2024 19:31:40 +0200
+From 79d958eccfa4a1cfbb552032e9542f03333005e7 Mon Sep 17 00:00:00 2001
+From: antheas <antheas@users.noreply.github.com>
+Date: Mon, 15 Jul 2024 00:00:45 +0300
Subject: [PATCH] add ally x dmi quirk for controller suspend
-Signed-off-by: Jan200101 <sentrycraft123@gmail.com>
---
- drivers/platform/x86/asus-wmi.c | 3 +--
- include/linux/platform_data/x86/asus-wmi.h | 14 ++++++++++++++
- 2 files changed, 15 insertions(+), 2 deletions(-)
+ drivers/platform/x86/asus-wmi.c | 6 ++++--
+ 1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/drivers/platform/x86/asus-wmi.c b/drivers/platform/x86/asus-wmi.c
-index 4b78c4f4e..a8060e7fb 100644
+index 3f9b6285c9a6..8e362726b703 100644
--- a/drivers/platform/x86/asus-wmi.c
+++ b/drivers/platform/x86/asus-wmi.c
-@@ -4646,8 +4646,7 @@ static int asus_wmi_add(struct platform_device *pdev)
+@@ -4645,8 +4645,10 @@ static int asus_wmi_add(struct platform_device *pdev)
asus->egpu_enable_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_EGPU);
asus->dgpu_disable_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_DGPU);
asus->kbd_rgb_state_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_TUF_RGB_STATE);
- asus->ally_mcu_usb_switch = acpi_has_method(NULL, ASUS_USB0_PWR_EC0_CSEE)
- && dmi_match(DMI_BOARD_NAME, "RC71L");
-+ asus->ally_mcu_usb_switch = acpi_has_method(NULL, ASUS_USB0_PWR_EC0_CSEE) && dmi_check_system(ally_usb_switch_dmi_ids);
++ asus->ally_mcu_usb_switch =
++ acpi_has_method(NULL, ASUS_USB0_PWR_EC0_CSEE) &&
++ (dmi_match(DMI_BOARD_NAME, "RC71L") ||
++ dmi_match(DMI_BOARD_NAME, "RC72LA"));
if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_MINI_LED_MODE))
asus->mini_led_dev_id = ASUS_WMI_DEVID_MINI_LED_MODE;
-diff --git a/include/linux/platform_data/x86/asus-wmi.h b/include/linux/platform_data/x86/asus-wmi.h
-index 870f4bb57..3e8edc07e 100644
---- a/include/linux/platform_data/x86/asus-wmi.h
-+++ b/include/linux/platform_data/x86/asus-wmi.h
-@@ -225,4 +225,18 @@ static const struct dmi_system_id asus_use_hid_led_dmi_ids[] = {
- NULL,
- };
-
-+static const struct dmi_system_id ally_usb_switch_dmi_ids[] = {
-+ {
-+ .matches = {
-+ DMI_MATCH(DMI_BOARD_NAME, "RC71L"),
-+ },
-+ },
-+ {
-+ .matches = {
-+ DMI_MATCH(DMI_BOARD_NAME, "RC72LA"),
-+ },
-+ },
-+ NULL,
-+};
-+
- #endif /* __PLATFORM_DATA_X86_ASUS_WMI_H */
+--
+2.45.2
diff --git a/SOURCES/0001-amd-hdr.patch b/SOURCES/0001-amd-hdr.patch
deleted file mode 100644
index 8116a3c..0000000
--- a/SOURCES/0001-amd-hdr.patch
+++ /dev/null
@@ -1,208 +0,0 @@
-From 2f33df4d9d3e86546d7c67453ae799306d55b7f5 Mon Sep 17 00:00:00 2001
-From: Melissa Wen <mwen@igalia.com>
-Date: Sat, 22 Apr 2023 14:08:47 -0100
-Subject: [PATCH] HACK: add KConfig to enable driver-specific color mgmt props
-
-We are enabling a large set of color calibration features to enhance KMS
-color mgmt but these properties are specific of AMD display HW, and
-cannot be provided by other vendors. Therefore, set a config option to
-enable AMD driver-private properties used on Steam Deck color mgmt
-pipeline. Replace the agreed name `AMD_PRIVATE_COLOR` with
-our downstream version `CONFIG_DRM_AMD_COLOR_STEAMDECK`.
-
-Signed-off-by: Melissa Wen <mwen@igalia.com>
----
- drivers/gpu/drm/amd/display/Kconfig | 7 +++++++
- drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c | 2 +-
- drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_color.c | 2 +-
- drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c | 6 +++---
- drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_plane.c | 6 +++---
- 5 files changed, 15 insertions(+), 8 deletions(-)
-
-diff --git a/drivers/gpu/drm/amd/display/Kconfig b/drivers/gpu/drm/amd/display/Kconfig
-index 901d1961b7392..49523fa82f92a 100644
---- a/drivers/gpu/drm/amd/display/Kconfig
-+++ b/drivers/gpu/drm/amd/display/Kconfig
-@@ -51,4 +51,11 @@ config DRM_AMD_SECURE_DISPLAY
- This option enables the calculation of crc of specific region via
- debugfs. Cooperate with specific DMCU FW.
-
-+config DRM_AMD_COLOR_STEAMDECK
-+ bool "Enable color calibration features for Steam Deck"
-+ depends on DRM_AMD_DC
-+ help
-+ Choose this option if you want to use AMDGPU features for broader
-+ color management support on Steam Deck.
-+
- endmenu
-diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c
-index 5853cf0229176..1718ddfe75083 100644
---- a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c
-+++ b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c
-@@ -4074,7 +4074,7 @@ static int amdgpu_dm_mode_config_init(struct amdgpu_device *adev)
- return r;
- }
-
--#ifdef AMD_PRIVATE_COLOR
-+#ifdef CONFIG_DRM_AMD_COLOR_STEAMDECK
- if (amdgpu_dm_create_color_properties(adev))
- return -ENOMEM;
- #endif
-diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_color.c b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_color.c
-index 21e0efc42a34c..8f8d2a8fb2921 100644
---- a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_color.c
-+++ b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_color.c
-@@ -97,7 +97,7 @@ static inline struct fixed31_32 amdgpu_dm_fixpt_from_s3132(__u64 x)
- return val;
- }
-
--#ifdef AMD_PRIVATE_COLOR
-+#ifdef CONFIG_DRM_AMD_COLOR_STEAMDECK
- /* Pre-defined Transfer Functions (TF)
- *
- * AMD driver supports pre-defined mathematical functions for transferring
-diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c
-index 6e715ef3a5566..ab9992f24ae21 100644
---- a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c
-+++ b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c
-@@ -290,7 +290,7 @@ static int amdgpu_dm_crtc_late_register(struct drm_crtc *crtc)
- }
- #endif
-
--#ifdef AMD_PRIVATE_COLOR
-+#ifdef CONFIG_DRM_AMD_COLOR_STEAMDECK
- /**
- * dm_crtc_additional_color_mgmt - enable additional color properties
- * @crtc: DRM CRTC
-@@ -372,7 +372,7 @@ static const struct drm_crtc_funcs amdgpu_dm_crtc_funcs = {
- #if defined(CONFIG_DEBUG_FS)
- .late_register = amdgpu_dm_crtc_late_register,
- #endif
--#ifdef AMD_PRIVATE_COLOR
-+#ifdef CONFIG_DRM_AMD_COLOR_STEAMDECK
- .atomic_set_property = amdgpu_dm_atomic_crtc_set_property,
- .atomic_get_property = amdgpu_dm_atomic_crtc_get_property,
- #endif
-@@ -551,7 +551,7 @@ int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm,
-
- drm_mode_crtc_set_gamma_size(&acrtc->base, MAX_COLOR_LEGACY_LUT_ENTRIES);
-
--#ifdef AMD_PRIVATE_COLOR
-+#ifdef CONFIG_DRM_AMD_COLOR_STEAMDECK
- dm_crtc_additional_color_mgmt(&acrtc->base);
- #endif
- return 0;
-diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_plane.c b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_plane.c
-index 8a4c40b4c27e4..5d87c24f0461f 100644
---- a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_plane.c
-+++ b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_plane.c
-@@ -1468,7 +1468,7 @@ static void amdgpu_dm_plane_drm_plane_destroy_state(struct drm_plane *plane,
- drm_atomic_helper_plane_destroy_state(plane, state);
- }
-
--#ifdef AMD_PRIVATE_COLOR
-+#ifdef CONFIG_DRM_AMD_COLOR_STEAMDECK
- static void
- dm_atomic_plane_attach_color_mgmt_properties(struct amdgpu_display_manager *dm,
- struct drm_plane *plane)
-@@ -1659,7 +1659,7 @@ static const struct drm_plane_funcs dm_plane_funcs = {
- .atomic_duplicate_state = amdgpu_dm_plane_drm_plane_duplicate_state,
- .atomic_destroy_state = amdgpu_dm_plane_drm_plane_destroy_state,
- .format_mod_supported = amdgpu_dm_plane_format_mod_supported,
--#ifdef AMD_PRIVATE_COLOR
-+#ifdef CONFIG_DRM_AMD_COLOR_STEAMDECK
- .atomic_set_property = dm_atomic_plane_set_property,
- .atomic_get_property = dm_atomic_plane_get_property,
- #endif
-@@ -1742,7 +1742,7 @@ int amdgpu_dm_plane_init(struct amdgpu_display_manager *dm,
-
- drm_plane_helper_add(plane, &dm_plane_helper_funcs);
-
--#ifdef AMD_PRIVATE_COLOR
-+#ifdef CONFIG_DRM_AMD_COLOR_STEAMDECK
- dm_atomic_plane_attach_color_mgmt_properties(dm, plane);
- #endif
- /* Create (reset) the plane state */
---
-GitLab
-
-From b938468f07222b4faab5ae5cf5391eccd9532bb0 Mon Sep 17 00:00:00 2001
-From: Bouke Sybren Haarsma <boukehaarsma23@gmail.com>
-Date: Fri, 15 Dec 2023 11:14:58 +0100
-Subject: [PATCH] Don't create color_mgmt_properties on asics < SIENNA_CICHLID
-
----
- drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_plane.c | 3 ++-
- 1 file changed, 2 insertions(+), 1 deletion(-)
-
-diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_plane.c b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_plane.c
-index 2ed20e6e439bb5..65ee8745e96540 100644
---- a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_plane.c
-+++ b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_plane.c
-@@ -1742,7 +1742,8 @@ int amdgpu_dm_plane_init(struct amdgpu_display_manager *dm,
- drm_plane_helper_add(plane, &dm_plane_helper_funcs);
-
- #ifdef CONFIG_DRM_AMD_COLOR_STEAMDECK
-- dm_atomic_plane_attach_color_mgmt_properties(dm, plane);
-+ if (dm->adev->asic_type >= CHIP_SIENNA_CICHLID)
-+ dm_atomic_plane_attach_color_mgmt_properties(dm, plane);
- #endif
- /* Create (reset) the plane state */
- if (plane->funcs->reset)
---
-2.43.0
-
-From: Joshua Ashton @ 2024-04-10 22:13 UTC (permalink / raw)
- To: amd-gfx; +Cc: Joshua Ashton, Harry Wentland, Xaver Hugl, Melissa Wen, Ethan Lee
-
-The comment here states "no OGAM in DPP since DCN1", yet that is not
-true.
-
-Testing on an RX 7900XTX (dcn32), it actually does exist in hardware and
-works fine.
-My best guess is the comment is confused with OGAM ROM for DPP, rather
-than OGAM RAM.
-
-I did not test dcn35/351 as I do not have that hardware, but I assume
-the same follows there given the seemingly erroneous comment.
-Someone at AMD should check that before merging this commit.
-
-Signed-off-by: Joshua Ashton <joshua at froggi.es>
-
-Cc: Harry Wentland <harry.wentland at amd.com>
-Cc: Xaver Hugl <xaver.hugl at gmail.com>
-Cc: Melissa Wen <mwen at igalia.com>
-Cc: Ethan Lee <flibitijibibo at gmail.com>
----
- drivers/gpu/drm/amd/display/dc/resource/dcn32/dcn32_resource.c | 2 +-
- drivers/gpu/drm/amd/display/dc/resource/dcn35/dcn35_resource.c | 2 +-
- 2 files changed, 2 insertions(+), 2 deletions(-)
-
-diff --git a/drivers/gpu/drm/amd/display/dc/resource/dcn32/dcn32_resource.c b/drivers/gpu/drm/amd/display/dc/resource/dcn32/dcn32_resource.c
-index 9aa39bd25be9..94f5d2b5aadf 100644
---- a/drivers/gpu/drm/amd/display/dc/resource/dcn32/dcn32_resource.c
-+++ b/drivers/gpu/drm/amd/display/dc/resource/dcn32/dcn32_resource.c
-@@ -2098,7 +2098,7 @@
- dc->caps.color.dpp.dgam_rom_for_yuv = 0;
-
- dc->caps.color.dpp.hw_3d_lut = 1;
-- dc->caps.color.dpp.ogam_ram = 0; // no OGAM in DPP since DCN1
-+ dc->caps.color.dpp.ogam_ram = 1;
- // no OGAM ROM on DCN2 and later ASICs
- dc->caps.color.dpp.ogam_rom_caps.srgb = 0;
- dc->caps.color.dpp.ogam_rom_caps.bt2020 = 0;
-diff --git a/drivers/gpu/drm/amd/display/dc/resource/dcn35/dcn35_resource.c b/drivers/gpu/drm/amd/display/dc/resource/dcn35/dcn35_resource.c
-index 25ac450944e7..708d63cc3f7f 100644
---- a/drivers/gpu/drm/amd/display/dc/resource/dcn35/dcn35_resource.c
-+++ b/drivers/gpu/drm/amd/display/dc/resource/dcn35/dcn35_resource.c
-@@ -1858,7 +1858,7 @@
- dc->caps.color.dpp.dgam_rom_for_yuv = 0;
-
- dc->caps.color.dpp.hw_3d_lut = 1;
-- dc->caps.color.dpp.ogam_ram = 0; // no OGAM in DPP since DCN1
-+ dc->caps.color.dpp.ogam_ram = 1;
- // no OGAM ROM on DCN301
- dc->caps.color.dpp.ogam_rom_caps.srgb = 0;
- dc->caps.color.dpp.ogam_rom_caps.bt2020 = 0;
---
-2.44.0
diff --git a/SOURCES/0001-amd-pstate.patch b/SOURCES/0001-amd-pstate.patch
index f654f25..72d5cc6 100644
--- a/SOURCES/0001-amd-pstate.patch
+++ b/SOURCES/0001-amd-pstate.patch
@@ -1,27 +1,26 @@
-From b36ccc153f8a2ee04f046a4e10220dbe308f37ef Mon Sep 17 00:00:00 2001
+From b2368823f6d79235b4e706263855373ff2fed1e5 Mon Sep 17 00:00:00 2001
From: Peter Jung <admin@ptr1337.dev>
-Date: Tue, 25 Jun 2024 13:29:13 +0200
+Date: Fri, 5 Jul 2024 10:31:29 +0200
Subject: [PATCH 02/10] amd-pstate
Signed-off-by: Peter Jung <admin@ptr1337.dev>
---
- Documentation/admin-guide/pm/amd-pstate.rst | 40 +-
+ Documentation/admin-guide/pm/amd-pstate.rst | 18 +-
arch/x86/include/asm/cpufeatures.h | 1 +
arch/x86/include/asm/msr-index.h | 2 +
arch/x86/kernel/cpu/scattered.c | 1 +
- drivers/cpufreq/acpi-cpufreq.c | 2 -
- drivers/cpufreq/amd-pstate-ut.c | 2 +-
- drivers/cpufreq/amd-pstate.c | 463 +++++++++++++++-----
- drivers/cpufreq/amd-pstate.h | 29 +-
- drivers/cpufreq/cpufreq.c | 25 +-
- include/linux/cpufreq.h | 2 +
- 10 files changed, 436 insertions(+), 131 deletions(-)
+ drivers/cpufreq/Kconfig.x86 | 1 +
+ drivers/cpufreq/acpi-cpufreq.c | 3 +-
+ drivers/cpufreq/amd-pstate.c | 356 ++++++++++++++------
+ drivers/cpufreq/amd-pstate.h | 16 +-
+ drivers/cpufreq/cpufreq.c | 14 +-
+ 9 files changed, 292 insertions(+), 120 deletions(-)
diff --git a/Documentation/admin-guide/pm/amd-pstate.rst b/Documentation/admin-guide/pm/amd-pstate.rst
-index 1e0d101b020a0..57995f54f0c88 100644
+index 1e0d101b020a..d0324d44f548 100644
--- a/Documentation/admin-guide/pm/amd-pstate.rst
+++ b/Documentation/admin-guide/pm/amd-pstate.rst
-@@ -281,6 +281,27 @@ integer values defined between 0 to 255 when EPP feature is enabled by platform
+@@ -281,6 +281,22 @@ integer values defined between 0 to 255 when EPP feature is enabled by platform
firmware, if EPP feature is disabled, driver will ignore the written value
This attribute is read-write.
@@ -41,15 +40,10 @@ index 1e0d101b020a0..57995f54f0c88 100644
+boost or `1` to enable it, for the respective CPU using the sysfs path
+`/sys/devices/system/cpu/cpuX/cpufreq/boost`, where `X` represents the CPU number.
+
-+It is important to note that modifying the global variable
-+`/sys/devices/system/cpu/amd_pstate/cpb_boost` will override the individual CPU
-+settings.
-+
-+
Other performance and frequency values can be read back from
``/sys/devices/system/cpu/cpuX/acpi_cppc/``, see :ref:`cppc_sysfs`.
-@@ -406,7 +427,7 @@ control its functionality at the system level. They are located in the
+@@ -406,7 +422,7 @@ control its functionality at the system level. They are located in the
``/sys/devices/system/cpu/amd_pstate/`` directory and affect all CPUs.
``status``
@@ -58,32 +52,8 @@ index 1e0d101b020a0..57995f54f0c88 100644
"active"
The driver is functional and in the ``active mode``
-@@ -440,6 +461,23 @@ control its functionality at the system level. They are located in the
- This attribute is read-only to check the state of preferred core set
- by the kernel parameter.
-
-+``cpb_boost``
-+ Specifies whether core performance boost is requested to be enabled or disabled
-+ If core performance boost is disabled while a core is in a boosted P-state, the
-+ core automatically transitions to the highest performance non-boosted P-state.
-+ AMD Core Performance Boost(CPB) is controlled by this attribute file which allows
-+ user to change all cores frequency boosting state. It supports all amd-pstate modes.
-+
-+ States of the driver "/sys/devices/system/cpu/amd_pstate/cpb_boost"
-+ "disabled" Core Performance Boosting Disabled.
-+ "enabled" Core Performance Boosting Enabled.
-+
-+ To enable core performance boost:
-+ # echo "enabled" > /sys/devices/system/cpu/amd_pstate/cpb_boost
-+
-+ To disable core performance boost:
-+ # echo "disabled" > /sys/devices/system/cpu/amd_pstate/cpb_boost
-+
- ``cpupower`` tool support for ``amd-pstate``
- ===============================================
-
diff --git a/arch/x86/include/asm/cpufeatures.h b/arch/x86/include/asm/cpufeatures.h
-index 3c7434329661c..6c128d463a143 100644
+index 3c7434329661..6c128d463a14 100644
--- a/arch/x86/include/asm/cpufeatures.h
+++ b/arch/x86/include/asm/cpufeatures.h
@@ -470,6 +470,7 @@
@@ -95,7 +65,7 @@ index 3c7434329661c..6c128d463a143 100644
/*
* BUG word(s)
diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h
-index e72c2b8729579..8738a7b3917d8 100644
+index e72c2b872957..8738a7b3917d 100644
--- a/arch/x86/include/asm/msr-index.h
+++ b/arch/x86/include/asm/msr-index.h
@@ -782,6 +782,8 @@
@@ -108,7 +78,7 @@ index e72c2b8729579..8738a7b3917d8 100644
/* K6 MSRs */
#define MSR_K6_WHCR 0xc0000082
diff --git a/arch/x86/kernel/cpu/scattered.c b/arch/x86/kernel/cpu/scattered.c
-index af5aa2c754c22..c84c30188fdf2 100644
+index af5aa2c754c2..c84c30188fdf 100644
--- a/arch/x86/kernel/cpu/scattered.c
+++ b/arch/x86/kernel/cpu/scattered.c
@@ -45,6 +45,7 @@ static const struct cpuid_bit cpuid_bits[] = {
@@ -119,8 +89,20 @@ index af5aa2c754c22..c84c30188fdf2 100644
{ X86_FEATURE_MBA, CPUID_EBX, 6, 0x80000008, 0 },
{ X86_FEATURE_SMBA, CPUID_EBX, 2, 0x80000020, 0 },
{ X86_FEATURE_BMEC, CPUID_EBX, 3, 0x80000020, 0 },
+diff --git a/drivers/cpufreq/Kconfig.x86 b/drivers/cpufreq/Kconfig.x86
+index 438c9e75a04d..97c2d4f15d76 100644
+--- a/drivers/cpufreq/Kconfig.x86
++++ b/drivers/cpufreq/Kconfig.x86
+@@ -71,6 +71,7 @@ config X86_AMD_PSTATE_DEFAULT_MODE
+ config X86_AMD_PSTATE_UT
+ tristate "selftest for AMD Processor P-State driver"
+ depends on X86 && ACPI_PROCESSOR
++ depends on X86_AMD_PSTATE
+ default n
+ help
+ This kernel module is used for testing. It's safe to say M here.
diff --git a/drivers/cpufreq/acpi-cpufreq.c b/drivers/cpufreq/acpi-cpufreq.c
-index 37f1cdf46d291..2fc82831bddd5 100644
+index 37f1cdf46d29..094f1f91a9b3 100644
--- a/drivers/cpufreq/acpi-cpufreq.c
+++ b/drivers/cpufreq/acpi-cpufreq.c
@@ -50,8 +50,6 @@ enum {
@@ -132,21 +114,16 @@ index 37f1cdf46d291..2fc82831bddd5 100644
struct acpi_cpufreq_data {
unsigned int resume;
unsigned int cpu_feature;
-diff --git a/drivers/cpufreq/amd-pstate-ut.c b/drivers/cpufreq/amd-pstate-ut.c
-index fc275d41d51e9..b528f198f4c36 100644
---- a/drivers/cpufreq/amd-pstate-ut.c
-+++ b/drivers/cpufreq/amd-pstate-ut.c
-@@ -227,7 +227,7 @@ static void amd_pstate_ut_check_freq(u32 index)
- goto skip_test;
- }
+@@ -139,6 +137,7 @@ static int set_boost(struct cpufreq_policy *policy, int val)
+ (void *)(long)val, 1);
+ pr_debug("CPU %*pbl: Core Boosting %s.\n",
+ cpumask_pr_args(policy->cpus), str_enabled_disabled(val));
++ policy->boost_enabled = val;
-- if (cpudata->boost_supported) {
-+ if (amd_pstate_global_params.cpb_boost) {
- if ((policy->max == cpudata->max_freq) ||
- (policy->max == cpudata->nominal_freq))
- amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_PASS;
+ return 0;
+ }
diff --git a/drivers/cpufreq/amd-pstate.c b/drivers/cpufreq/amd-pstate.c
-index 6af175e6c08ac..6eeba793bf442 100644
+index 6af175e6c08a..80eaa58f1405 100644
--- a/drivers/cpufreq/amd-pstate.c
+++ b/drivers/cpufreq/amd-pstate.c
@@ -51,6 +51,7 @@
@@ -173,36 +150,7 @@ index 6af175e6c08ac..6eeba793bf442 100644
static struct cpufreq_driver *current_pstate_driver;
static struct cpufreq_driver amd_pstate_driver;
static struct cpufreq_driver amd_pstate_epp_driver;
-@@ -102,6 +94,11 @@ static bool cppc_enabled;
- static bool amd_pstate_prefcore = true;
- static struct quirk_entry *quirks;
-
-+/* export the amd_pstate_global_params for unit test */
-+struct amd_pstate_global_params amd_pstate_global_params;
-+EXPORT_SYMBOL_GPL(amd_pstate_global_params);
-+static int amd_pstate_cpu_boost(int cpu, bool state);
-+
- /*
- * AMD Energy Preference Performance (EPP)
- * The EPP is used in the CCLK DPM controller to drive
-@@ -143,6 +140,16 @@ static unsigned int epp_values[] = {
- [EPP_INDEX_POWERSAVE] = AMD_CPPC_EPP_POWERSAVE,
- };
-
-+enum CPB_STATE_INDEX {
-+ CPB_STATE_DISABLED = 0,
-+ CPB_STATE_ENABLED = 1,
-+};
-+
-+static const char * const cpb_state[] = {
-+ [CPB_STATE_DISABLED] = "disabled",
-+ [CPB_STATE_ENABLED] = "enabled",
-+};
-+
- typedef int (*cppc_mode_transition_fn)(int);
-
- static struct quirk_entry quirk_amd_7k62 = {
-@@ -157,7 +164,7 @@ static int __init dmi_matched_7k62_bios_bug(const struct dmi_system_id *dmi)
+@@ -157,7 +149,7 @@ static int __init dmi_matched_7k62_bios_bug(const struct dmi_system_id *dmi)
* broken BIOS lack of nominal_freq and lowest_freq capabilities
* definition in ACPI tables
*/
@@ -211,7 +159,7 @@ index 6af175e6c08ac..6eeba793bf442 100644
quirks = dmi->driver_data;
pr_info("Overriding nominal and lowest frequencies for %s\n", dmi->ident);
return 1;
-@@ -199,7 +206,7 @@ static s16 amd_pstate_get_epp(struct amd_cpudata *cpudata, u64 cppc_req_cached)
+@@ -199,7 +191,7 @@ static s16 amd_pstate_get_epp(struct amd_cpudata *cpudata, u64 cppc_req_cached)
u64 epp;
int ret;
@@ -220,7 +168,7 @@ index 6af175e6c08ac..6eeba793bf442 100644
if (!cppc_req_cached) {
epp = rdmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ,
&cppc_req_cached);
-@@ -252,7 +259,7 @@ static int amd_pstate_set_epp(struct amd_cpudata *cpudata, u32 epp)
+@@ -252,7 +244,7 @@ static int amd_pstate_set_epp(struct amd_cpudata *cpudata, u32 epp)
int ret;
struct cppc_perf_ctrls perf_ctrls;
@@ -229,7 +177,7 @@ index 6af175e6c08ac..6eeba793bf442 100644
u64 value = READ_ONCE(cpudata->cppc_req_cached);
value &= ~GENMASK_ULL(31, 24);
-@@ -281,10 +288,8 @@ static int amd_pstate_set_energy_pref_index(struct amd_cpudata *cpudata,
+@@ -281,10 +273,8 @@ static int amd_pstate_set_energy_pref_index(struct amd_cpudata *cpudata,
int epp = -EINVAL;
int ret;
@@ -242,7 +190,7 @@ index 6af175e6c08ac..6eeba793bf442 100644
if (epp == -EINVAL)
epp = epp_values[pref_index];
-@@ -521,7 +526,10 @@ static inline bool amd_pstate_sample(struct amd_cpudata *cpudata)
+@@ -521,7 +511,10 @@ static inline bool amd_pstate_sample(struct amd_cpudata *cpudata)
static void amd_pstate_update(struct amd_cpudata *cpudata, u32 min_perf,
u32 des_perf, u32 max_perf, bool fast_switch, int gov_flags)
{
@@ -253,7 +201,7 @@ index 6af175e6c08ac..6eeba793bf442 100644
u64 value = prev;
min_perf = clamp_t(unsigned long, min_perf, cpudata->min_limit_perf,
-@@ -530,6 +538,9 @@ static void amd_pstate_update(struct amd_cpudata *cpudata, u32 min_perf,
+@@ -530,6 +523,9 @@ static void amd_pstate_update(struct amd_cpudata *cpudata, u32 min_perf,
cpudata->max_limit_perf);
des_perf = clamp_t(unsigned long, des_perf, min_perf, max_perf);
@@ -263,18 +211,18 @@ index 6af175e6c08ac..6eeba793bf442 100644
if ((cppc_state == AMD_PSTATE_GUIDED) && (gov_flags & CPUFREQ_GOV_DYNAMIC_SWITCHING)) {
min_perf = des_perf;
des_perf = 0;
-@@ -541,6 +552,10 @@ static void amd_pstate_update(struct amd_cpudata *cpudata, u32 min_perf,
+@@ -541,6 +537,10 @@ static void amd_pstate_update(struct amd_cpudata *cpudata, u32 min_perf,
value &= ~AMD_CPPC_DES_PERF(~0L);
value |= AMD_CPPC_DES_PERF(des_perf);
+ /* limit the max perf when core performance boost feature is disabled */
-+ if (!amd_pstate_global_params.cpb_boost)
++ if (!cpudata->boost_supported)
+ max_perf = min_t(unsigned long, nominal_perf, max_perf);
+
value &= ~AMD_CPPC_MAX_PERF(~0L);
value |= AMD_CPPC_MAX_PERF(max_perf);
-@@ -651,10 +666,9 @@ static void amd_pstate_adjust_perf(unsigned int cpu,
+@@ -651,10 +651,9 @@ static void amd_pstate_adjust_perf(unsigned int cpu,
unsigned long capacity)
{
unsigned long max_perf, min_perf, des_perf,
@@ -286,7 +234,7 @@ index 6af175e6c08ac..6eeba793bf442 100644
if (policy->min != cpudata->min_limit_freq || policy->max != cpudata->max_limit_freq)
amd_pstate_update_min_max_limit(policy);
-@@ -662,7 +676,6 @@ static void amd_pstate_adjust_perf(unsigned int cpu,
+@@ -662,7 +661,6 @@ static void amd_pstate_adjust_perf(unsigned int cpu,
cap_perf = READ_ONCE(cpudata->highest_perf);
lowest_nonlinear_perf = READ_ONCE(cpudata->lowest_nonlinear_perf);
@@ -294,7 +242,7 @@ index 6af175e6c08ac..6eeba793bf442 100644
des_perf = cap_perf;
if (target_perf < capacity)
-@@ -680,34 +693,12 @@ static void amd_pstate_adjust_perf(unsigned int cpu,
+@@ -680,32 +678,57 @@ static void amd_pstate_adjust_perf(unsigned int cpu,
max_perf = min_perf;
des_perf = clamp_t(unsigned long, des_perf, min_perf, max_perf);
@@ -307,66 +255,100 @@ index 6af175e6c08ac..6eeba793bf442 100644
}
-static int amd_get_min_freq(struct amd_cpudata *cpudata)
--{
++static int amd_pstate_cpu_boost_update(struct cpufreq_policy *policy, bool on)
+ {
- return READ_ONCE(cpudata->min_freq);
-}
--
++ struct amd_cpudata *cpudata = policy->driver_data;
++ struct cppc_perf_ctrls perf_ctrls;
++ u32 highest_perf, nominal_perf, nominal_freq, max_freq;
++ int ret;
+
-static int amd_get_max_freq(struct amd_cpudata *cpudata)
-{
- return READ_ONCE(cpudata->max_freq);
-}
--
++ highest_perf = READ_ONCE(cpudata->highest_perf);
++ nominal_perf = READ_ONCE(cpudata->nominal_perf);
++ nominal_freq = READ_ONCE(cpudata->nominal_freq);
++ max_freq = READ_ONCE(cpudata->max_freq);
+
-static int amd_get_nominal_freq(struct amd_cpudata *cpudata)
-{
- return READ_ONCE(cpudata->nominal_freq);
-}
--
++ if (boot_cpu_has(X86_FEATURE_CPPC)) {
++ u64 value = READ_ONCE(cpudata->cppc_req_cached);
+
-static int amd_get_lowest_nonlinear_freq(struct amd_cpudata *cpudata)
-{
- return READ_ONCE(cpudata->lowest_nonlinear_freq);
--}
--
++ value &= ~GENMASK_ULL(7, 0);
++ value |= on ? highest_perf : nominal_perf;
++ WRITE_ONCE(cpudata->cppc_req_cached, value);
++
++ wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, value);
++ } else {
++ perf_ctrls.max_perf = on ? highest_perf : nominal_perf;
++ ret = cppc_set_perf(cpudata->cpu, &perf_ctrls);
++ if (ret) {
++ cpufreq_cpu_release(policy);
++ pr_debug("Failed to set max perf on CPU:%d. ret:%d\n",
++ cpudata->cpu, ret);
++ return ret;
++ }
++ }
++
++ if (on)
++ policy->cpuinfo.max_freq = max_freq;
++ else if (policy->cpuinfo.max_freq > nominal_freq * 1000)
++ policy->cpuinfo.max_freq = nominal_freq * 1000;
++
++ policy->max = policy->cpuinfo.max_freq;
++
++ if (cppc_state == AMD_PSTATE_PASSIVE) {
++ ret = freq_qos_update_request(&cpudata->req[1], policy->cpuinfo.max_freq);
++ if (ret < 0)
++ pr_debug("Failed to update freq constraint: CPU%d\n", cpudata->cpu);
++ }
++
++ return ret < 0 ? ret : 0;
+ }
+
static int amd_pstate_set_boost(struct cpufreq_policy *policy, int state)
- {
- struct amd_cpudata *cpudata = policy->driver_data;
-@@ -715,36 +706,53 @@ static int amd_pstate_set_boost(struct cpufreq_policy *policy, int state)
+@@ -715,36 +738,51 @@ static int amd_pstate_set_boost(struct cpufreq_policy *policy, int state)
if (!cpudata->boost_supported) {
pr_err("Boost mode is not supported by this processor or SBIOS\n");
- return -EINVAL;
-+ return -ENOTSUPP;
++ return -EOPNOTSUPP;
}
+ mutex_lock(&amd_pstate_driver_lock);
-+ ret = amd_pstate_cpu_boost(policy->cpu, state);
++ ret = amd_pstate_cpu_boost_update(policy, state);
++ WRITE_ONCE(cpudata->boost_state, !ret ? state : false);
++ policy->boost_enabled = !ret ? state : false;
++ refresh_frequency_limits(policy);
+ mutex_unlock(&amd_pstate_driver_lock);
- if (state)
- policy->cpuinfo.max_freq = cpudata->max_freq;
- else
- policy->cpuinfo.max_freq = cpudata->nominal_freq * 1000;
--
-- policy->max = policy->cpuinfo.max_freq;
--
-- ret = freq_qos_update_request(&cpudata->req[1],
-- policy->cpuinfo.max_freq);
-- if (ret < 0)
-- return ret;
--
-- return 0;
-+ return ret < 0 ? ret : 0;
- }
++ return ret;
++}
--static void amd_pstate_boost_init(struct amd_cpudata *cpudata)
+- policy->max = policy->cpuinfo.max_freq;
+static int amd_pstate_init_boost_support(struct amd_cpudata *cpudata)
- {
-- u32 highest_perf, nominal_perf;
++{
+ u64 boost_val;
+ int ret = -1;
-- highest_perf = READ_ONCE(cpudata->highest_perf);
-- nominal_perf = READ_ONCE(cpudata->nominal_perf);
+- ret = freq_qos_update_request(&cpudata->req[1],
+- policy->cpuinfo.max_freq);
+- if (ret < 0)
+- return ret;
+ /*
-+ * If platform has no CPB support or disble it, initialize current driver
++ * If platform has no CPB support or disable it, initialize current driver
+ * boost_enabled state to be false, it is not an error for cpufreq core to handle.
+ */
+ if (!cpu_feature_enabled(X86_FEATURE_CPB)) {
@@ -375,37 +357,39 @@ index 6af175e6c08ac..6eeba793bf442 100644
+ goto exit_err;
+ }
-- if (highest_perf <= nominal_perf)
-- return;
+- return 0;
+-}
++ /* at least one CPU supports CPB, even if others fail later on to set up */
++ current_pstate_driver->boost_enabled = true;
+
+-static void amd_pstate_boost_init(struct amd_cpudata *cpudata)
+-{
+- u32 highest_perf, nominal_perf;
+ ret = rdmsrl_on_cpu(cpudata->cpu, MSR_K7_HWCR, &boost_val);
+ if (ret) {
+ pr_err_once("failed to read initial CPU boost state!\n");
+ ret = -EIO;
+ goto exit_err;
+ }
-+
-+ amd_pstate_global_params.cpb_supported = !(boost_val & MSR_K7_HWCR_CPB_DIS);
-+ if (amd_pstate_global_params.cpb_supported) {
-+ current_pstate_driver->boost_enabled = true;
+
+- highest_perf = READ_ONCE(cpudata->highest_perf);
+- nominal_perf = READ_ONCE(cpudata->nominal_perf);
++ if (!(boost_val & MSR_K7_HWCR_CPB_DIS))
+ cpudata->boost_supported = true;
-+ cpudata->boost_state = true;
-+ }
+
+- if (highest_perf <= nominal_perf)
+- return;
++ return 0;
- cpudata->boost_supported = true;
- current_pstate_driver->boost_enabled = true;
-+ amd_pstate_global_params.cpb_boost = amd_pstate_global_params.cpb_supported;
-+ return 0;
-+
+exit_err:
+ cpudata->boost_supported = false;
-+ cpudata->boost_state = false;
-+ current_pstate_driver->boost_enabled = false;
-+ amd_pstate_global_params.cpb_boost = false;
+ return ret;
}
static void amd_perf_ctl_reset(unsigned int cpu)
-@@ -773,7 +781,7 @@ static int amd_pstate_get_highest_perf(int cpu, u32 *highest_perf)
+@@ -773,7 +811,7 @@ static int amd_pstate_get_highest_perf(int cpu, u32 *highest_perf)
{
int ret;
@@ -414,7 +398,7 @@ index 6af175e6c08ac..6eeba793bf442 100644
u64 cap1;
ret = rdmsrl_safe_on_cpu(cpu, MSR_AMD_CPPC_CAP1, &cap1);
-@@ -860,7 +868,41 @@ static void amd_pstate_update_limits(unsigned int cpu)
+@@ -860,7 +898,41 @@ static void amd_pstate_update_limits(unsigned int cpu)
mutex_unlock(&amd_pstate_driver_lock);
}
@@ -457,7 +441,7 @@ index 6af175e6c08ac..6eeba793bf442 100644
* amd_pstate_init_freq: Initialize the max_freq, min_freq,
* nominal_freq and lowest_nonlinear_freq for
* the @cpudata object.
-@@ -881,7 +923,6 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata)
+@@ -881,7 +953,6 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata)
u32 boost_ratio, lowest_nonlinear_ratio;
struct cppc_perf_caps cppc_perf;
@@ -465,7 +449,7 @@ index 6af175e6c08ac..6eeba793bf442 100644
ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);
if (ret)
return ret;
-@@ -912,12 +953,30 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata)
+@@ -912,12 +983,30 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata)
WRITE_ONCE(cpudata->nominal_freq, nominal_freq);
WRITE_ONCE(cpudata->max_freq, max_freq);
@@ -497,7 +481,7 @@ index 6af175e6c08ac..6eeba793bf442 100644
struct device *dev;
struct amd_cpudata *cpudata;
-@@ -946,20 +1005,11 @@ static int amd_pstate_cpu_init(struct cpufreq_policy *policy)
+@@ -946,20 +1035,15 @@ static int amd_pstate_cpu_init(struct cpufreq_policy *policy)
if (ret)
goto free_cpudata1;
@@ -510,19 +494,27 @@ index 6af175e6c08ac..6eeba793bf442 100644
- dev_err(dev, "min_freq(%d) or max_freq(%d) value is incorrect\n",
- min_freq, max_freq);
- ret = -EINVAL;
-- goto free_cpudata1;
++ ret = amd_pstate_init_boost_support(cpudata);
++ if (ret)
+ goto free_cpudata1;
- }
-+ min_freq = READ_ONCE(cpudata->min_freq);
-+ max_freq = READ_ONCE(cpudata->max_freq);
- policy->cpuinfo.transition_latency = AMD_PSTATE_TRANSITION_LATENCY;
- policy->transition_delay_us = AMD_PSTATE_TRANSITION_DELAY;
++ min_freq = READ_ONCE(cpudata->min_freq);
++ max_freq = READ_ONCE(cpudata->max_freq);
++
+ policy->cpuinfo.transition_latency = amd_pstate_get_transition_latency(policy->cpu);
+ policy->transition_delay_us = amd_pstate_get_transition_delay_us(policy->cpu);
policy->min = min_freq;
policy->max = max_freq;
-@@ -970,7 +1020,7 @@ static int amd_pstate_cpu_init(struct cpufreq_policy *policy)
+@@ -967,10 +1051,12 @@ static int amd_pstate_cpu_init(struct cpufreq_policy *policy)
+ policy->cpuinfo.min_freq = min_freq;
+ policy->cpuinfo.max_freq = max_freq;
+
++ policy->boost_enabled = READ_ONCE(cpudata->boost_supported);
++
/* It will be updated by governor */
policy->cur = policy->cpuinfo.min_freq;
@@ -531,7 +523,7 @@ index 6af175e6c08ac..6eeba793bf442 100644
policy->fast_switch_possible = true;
ret = freq_qos_add_request(&policy->constraints, &cpudata->req[0],
-@@ -992,7 +1042,6 @@ static int amd_pstate_cpu_init(struct cpufreq_policy *policy)
+@@ -992,7 +1078,6 @@ static int amd_pstate_cpu_init(struct cpufreq_policy *policy)
policy->driver_data = cpudata;
@@ -539,7 +531,7 @@ index 6af175e6c08ac..6eeba793bf442 100644
if (!current_pstate_driver->adjust_perf)
current_pstate_driver->adjust_perf = amd_pstate_adjust_perf;
-@@ -1052,7 +1101,7 @@ static ssize_t show_amd_pstate_max_freq(struct cpufreq_policy *policy,
+@@ -1052,7 +1137,7 @@ static ssize_t show_amd_pstate_max_freq(struct cpufreq_policy *policy,
int max_freq;
struct amd_cpudata *cpudata = policy->driver_data;
@@ -548,7 +540,7 @@ index 6af175e6c08ac..6eeba793bf442 100644
if (max_freq < 0)
return max_freq;
-@@ -1065,7 +1114,7 @@ static ssize_t show_amd_pstate_lowest_nonlinear_freq(struct cpufreq_policy *poli
+@@ -1065,7 +1150,7 @@ static ssize_t show_amd_pstate_lowest_nonlinear_freq(struct cpufreq_policy *poli
int freq;
struct amd_cpudata *cpudata = policy->driver_data;
@@ -557,7 +549,7 @@ index 6af175e6c08ac..6eeba793bf442 100644
if (freq < 0)
return freq;
-@@ -1203,7 +1252,7 @@ static int amd_pstate_change_mode_without_dvr_change(int mode)
+@@ -1203,7 +1288,7 @@ static int amd_pstate_change_mode_without_dvr_change(int mode)
cppc_state = mode;
@@ -566,160 +558,8 @@ index 6af175e6c08ac..6eeba793bf442 100644
return 0;
for_each_present_cpu(cpu) {
-@@ -1312,6 +1361,118 @@ static ssize_t prefcore_show(struct device *dev,
- return sysfs_emit(buf, "%s\n", str_enabled_disabled(amd_pstate_prefcore));
- }
-
-+static int amd_pstate_cpu_boost_update(struct cpufreq_policy *policy, bool on)
-+{
-+ struct amd_cpudata *cpudata = policy->driver_data;
-+ struct cppc_perf_ctrls perf_ctrls;
-+ u32 highest_perf, nominal_perf, nominal_freq, max_freq;
-+ int ret;
-+
-+ highest_perf = READ_ONCE(cpudata->highest_perf);
-+ nominal_perf = READ_ONCE(cpudata->nominal_perf);
-+ nominal_freq = READ_ONCE(cpudata->nominal_freq);
-+ max_freq = READ_ONCE(cpudata->max_freq);
-+
-+ if (boot_cpu_has(X86_FEATURE_CPPC)) {
-+ u64 value = READ_ONCE(cpudata->cppc_req_cached);
-+
-+ value &= ~GENMASK_ULL(7, 0);
-+ value |= on ? highest_perf : nominal_perf;
-+ WRITE_ONCE(cpudata->cppc_req_cached, value);
-+
-+ wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, value);
-+ } else {
-+ perf_ctrls.max_perf = on ? highest_perf : nominal_perf;
-+ ret = cppc_set_perf(cpudata->cpu, &perf_ctrls);
-+ if (ret) {
-+ cpufreq_cpu_release(policy);
-+ pr_debug("Failed to set max perf on CPU:%d. ret:%d\n",
-+ cpudata->cpu, ret);
-+ return ret;
-+ }
-+ }
-+
-+ if (on)
-+ policy->cpuinfo.max_freq = max_freq;
-+ else
-+ policy->cpuinfo.max_freq = nominal_freq * 1000;
-+
-+ policy->max = policy->cpuinfo.max_freq;
-+
-+ if (cppc_state == AMD_PSTATE_PASSIVE) {
-+ ret = freq_qos_update_request(&cpudata->req[1], policy->cpuinfo.max_freq);
-+ if (ret < 0)
-+ pr_debug("Failed to update freq constraint: CPU%d\n", cpudata->cpu);
-+ }
-+
-+ return ret < 0 ? ret : 0;
-+}
-+
-+static int amd_pstate_cpu_boost(int cpu, bool state)
-+{
-+ int ret;
-+ struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
-+ struct amd_cpudata *cpudata = policy->driver_data;
-+
-+ if (!policy) {
-+ pr_err("policy is NULL\n");
-+ ret = -ENODATA;
-+ goto err_exit;
-+ }
-+
-+ ret = amd_pstate_cpu_boost_update(policy, state);
-+ refresh_frequency_limits(policy);
-+ WRITE_ONCE(cpudata->boost_state, state);
-+ policy->boost_enabled = state;
-+
-+err_exit:
-+ cpufreq_cpu_put(policy);
-+ return ret < 0 ? ret : 0;
-+}
-+
-+static ssize_t cpb_boost_show(struct device *dev,
-+ struct device_attribute *attr, char *buf)
-+{
-+ bool cpb_idx;
-+
-+ cpb_idx = amd_pstate_global_params.cpb_boost;
-+
-+ return sysfs_emit(buf, "%s\n", cpb_state[cpb_idx]);
-+}
-+
-+static ssize_t cpb_boost_store(struct device *dev, struct device_attribute *b,
-+ const char *buf, size_t count)
-+{
-+ bool new_state;
-+ ssize_t ret;
-+ int cpu, cpb_idx;
-+
-+ if (!amd_pstate_global_params.cpb_supported) {
-+ pr_err("Boost mode is not supported by this processor or SBIOS\n");
-+ return -EINVAL;
-+ }
-+
-+ cpb_idx = sysfs_match_string(cpb_state, buf);
-+ if (cpb_idx < 0)
-+ return -EINVAL;
-+
-+ new_state = cpb_idx;
-+
-+ mutex_lock(&amd_pstate_driver_lock);
-+ for_each_present_cpu(cpu) {
-+ ret = amd_pstate_cpu_boost(cpu, new_state);
-+ if (ret < 0) {
-+ pr_warn("failed to update cpu boost for CPU%d (%zd)\n", cpu, ret);
-+ goto err_exit;
-+ }
-+ }
-+ amd_pstate_global_params.cpb_boost = !!new_state;
-+
-+err_exit:
-+ mutex_unlock(&amd_pstate_driver_lock);
-+ return ret < 0 ? ret : count;
-+}
-+
- cpufreq_freq_attr_ro(amd_pstate_max_freq);
- cpufreq_freq_attr_ro(amd_pstate_lowest_nonlinear_freq);
-
-@@ -1322,6 +1483,7 @@ cpufreq_freq_attr_rw(energy_performance_preference);
- cpufreq_freq_attr_ro(energy_performance_available_preferences);
- static DEVICE_ATTR_RW(status);
- static DEVICE_ATTR_RO(prefcore);
-+static DEVICE_ATTR_RW(cpb_boost);
-
- static struct freq_attr *amd_pstate_attr[] = {
- &amd_pstate_max_freq,
-@@ -1346,6 +1508,7 @@ static struct freq_attr *amd_pstate_epp_attr[] = {
- static struct attribute *pstate_global_attributes[] = {
- &dev_attr_status.attr,
- &dev_attr_prefcore.attr,
-+ &dev_attr_cpb_boost.attr,
- NULL
- };
+@@ -1376,7 +1461,7 @@ static bool amd_pstate_acpi_pm_profile_undefined(void)
-@@ -1374,9 +1537,24 @@ static bool amd_pstate_acpi_pm_profile_undefined(void)
- return false;
- }
-
-+static int amd_pstate_init_boost(struct cpufreq_policy *policy)
-+{
-+ struct amd_cpudata *cpudata = policy->driver_data;
-+ int ret;
-+
-+ /* initialize cpu cores boot state */
-+ ret = amd_pstate_init_boost_support(cpudata);
-+ if (ret)
-+ return ret;
-+
-+ policy->boost_enabled = READ_ONCE(cpudata->boost_state);
-+
-+ return 0;
-+}
-+
static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy)
{
- int min_freq, max_freq, nominal_freq, lowest_nonlinear_freq, ret;
@@ -727,7 +567,7 @@ index 6af175e6c08ac..6eeba793bf442 100644
struct amd_cpudata *cpudata;
struct device *dev;
u64 value;
-@@ -1407,16 +1585,8 @@ static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy)
+@@ -1407,16 +1492,12 @@ static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy)
if (ret)
goto free_cpudata1;
@@ -739,14 +579,17 @@ index 6af175e6c08ac..6eeba793bf442 100644
- dev_err(dev, "min_freq(%d) or max_freq(%d) value is incorrect\n",
- min_freq, max_freq);
- ret = -EINVAL;
-- goto free_cpudata1;
++ ret = amd_pstate_init_boost_support(cpudata);
++ if (ret)
+ goto free_cpudata1;
- }
++
+ min_freq = READ_ONCE(cpudata->min_freq);
+ max_freq = READ_ONCE(cpudata->max_freq);
policy->cpuinfo.min_freq = min_freq;
policy->cpuinfo.max_freq = max_freq;
-@@ -1425,7 +1595,7 @@ static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy)
+@@ -1425,11 +1506,13 @@ static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy)
policy->driver_data = cpudata;
@@ -755,7 +598,13 @@ index 6af175e6c08ac..6eeba793bf442 100644
policy->min = policy->cpuinfo.min_freq;
policy->max = policy->cpuinfo.max_freq;
-@@ -1440,7 +1610,7 @@ static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy)
+
++ policy->boost_enabled = READ_ONCE(cpudata->boost_supported);
++
+ /*
+ * Set the policy to provide a valid fallback value in case
+ * the default cpufreq governor is neither powersave nor performance.
+@@ -1440,7 +1523,7 @@ static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy)
else
policy->policy = CPUFREQ_POLICY_POWERSAVE;
@@ -764,7 +613,7 @@ index 6af175e6c08ac..6eeba793bf442 100644
ret = rdmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, &value);
if (ret)
return ret;
-@@ -1451,7 +1621,6 @@ static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy)
+@@ -1451,7 +1534,6 @@ static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy)
return ret;
WRITE_ONCE(cpudata->cppc_cap1_cached, value);
}
@@ -772,7 +621,7 @@ index 6af175e6c08ac..6eeba793bf442 100644
return 0;
-@@ -1530,7 +1699,7 @@ static void amd_pstate_epp_update_limit(struct cpufreq_policy *policy)
+@@ -1530,7 +1612,7 @@ static void amd_pstate_epp_update_limit(struct cpufreq_policy *policy)
epp = 0;
/* Set initial EPP value */
@@ -781,7 +630,7 @@ index 6af175e6c08ac..6eeba793bf442 100644
value &= ~GENMASK_ULL(31, 24);
value |= (u64)epp << 24;
}
-@@ -1553,6 +1722,12 @@ static int amd_pstate_epp_set_policy(struct cpufreq_policy *policy)
+@@ -1553,6 +1635,12 @@ static int amd_pstate_epp_set_policy(struct cpufreq_policy *policy)
amd_pstate_epp_update_limit(policy);
@@ -794,7 +643,7 @@ index 6af175e6c08ac..6eeba793bf442 100644
return 0;
}
-@@ -1569,7 +1744,7 @@ static void amd_pstate_epp_reenable(struct amd_cpudata *cpudata)
+@@ -1569,7 +1657,7 @@ static void amd_pstate_epp_reenable(struct amd_cpudata *cpudata)
value = READ_ONCE(cpudata->cppc_req_cached);
max_perf = READ_ONCE(cpudata->highest_perf);
@@ -803,7 +652,7 @@ index 6af175e6c08ac..6eeba793bf442 100644
wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, value);
} else {
perf_ctrls.max_perf = max_perf;
-@@ -1603,7 +1778,7 @@ static void amd_pstate_epp_offline(struct cpufreq_policy *policy)
+@@ -1603,7 +1691,7 @@ static void amd_pstate_epp_offline(struct cpufreq_policy *policy)
value = READ_ONCE(cpudata->cppc_req_cached);
mutex_lock(&amd_pstate_limits_lock);
@@ -812,24 +661,15 @@ index 6af175e6c08ac..6eeba793bf442 100644
cpudata->epp_policy = CPUFREQ_POLICY_UNKNOWN;
/* Set max perf same as min perf */
-@@ -1690,6 +1865,7 @@ static struct cpufreq_driver amd_pstate_driver = {
- .exit = amd_pstate_cpu_exit,
- .suspend = amd_pstate_cpu_suspend,
- .resume = amd_pstate_cpu_resume,
-+ .init_boost = amd_pstate_init_boost,
- .set_boost = amd_pstate_set_boost,
- .update_limits = amd_pstate_update_limits,
- .name = "amd-pstate",
-@@ -1707,6 +1883,8 @@ static struct cpufreq_driver amd_pstate_epp_driver = {
+@@ -1707,6 +1795,7 @@ static struct cpufreq_driver amd_pstate_epp_driver = {
.suspend = amd_pstate_epp_suspend,
.resume = amd_pstate_epp_resume,
.update_limits = amd_pstate_update_limits,
-+ .init_boost = amd_pstate_init_boost,
+ .set_boost = amd_pstate_set_boost,
.name = "amd-pstate-epp",
.attr = amd_pstate_epp_attr,
};
-@@ -1730,6 +1908,46 @@ static int __init amd_pstate_set_driver(int mode_idx)
+@@ -1730,6 +1819,46 @@ static int __init amd_pstate_set_driver(int mode_idx)
return -EINVAL;
}
@@ -876,7 +716,7 @@ index 6af175e6c08ac..6eeba793bf442 100644
static int __init amd_pstate_init(void)
{
struct device *dev_root;
-@@ -1738,6 +1956,11 @@ static int __init amd_pstate_init(void)
+@@ -1738,6 +1867,11 @@ static int __init amd_pstate_init(void)
if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
return -ENODEV;
@@ -888,7 +728,7 @@ index 6af175e6c08ac..6eeba793bf442 100644
if (!acpi_cpc_valid()) {
pr_warn_once("the _CPC object is not present in SBIOS or ACPI disabled\n");
return -ENODEV;
-@@ -1752,35 +1975,43 @@ static int __init amd_pstate_init(void)
+@@ -1752,35 +1886,43 @@ static int __init amd_pstate_init(void)
/* check if this machine need CPPC quirks */
dmi_check_system(amd_pstate_quirks_table);
@@ -942,7 +782,7 @@ index 6af175e6c08ac..6eeba793bf442 100644
pr_debug("AMD CPPC MSR based functionality is supported\n");
if (cppc_state != AMD_PSTATE_ACTIVE)
current_pstate_driver->adjust_perf = amd_pstate_adjust_perf;
-@@ -1794,13 +2025,15 @@ static int __init amd_pstate_init(void)
+@@ -1794,13 +1936,15 @@ static int __init amd_pstate_init(void)
/* enable amd pstate feature */
ret = amd_pstate_enable(true);
if (ret) {
@@ -953,24 +793,24 @@ index 6af175e6c08ac..6eeba793bf442 100644
ret = cpufreq_register_driver(current_pstate_driver);
- if (ret)
-+ if (ret) {
++ if (ret) {
pr_err("failed to register with return %d\n", ret);
-+ goto disable_driver;
-+ }
++ goto disable_driver;
++ }
dev_root = bus_get_dev_root(&cpu_subsys);
if (dev_root) {
-@@ -1816,6 +2049,8 @@ static int __init amd_pstate_init(void)
+@@ -1816,6 +1960,8 @@ static int __init amd_pstate_init(void)
global_attr_free:
cpufreq_unregister_driver(current_pstate_driver);
+disable_driver:
-+ amd_pstate_enable(false);
++ amd_pstate_enable(false);
return ret;
}
device_initcall(amd_pstate_init);
diff --git a/drivers/cpufreq/amd-pstate.h b/drivers/cpufreq/amd-pstate.h
-index bc341f35908d7..fb240a8702892 100644
+index bc341f35908d..cc8bb2bc325a 100644
--- a/drivers/cpufreq/amd-pstate.h
+++ b/drivers/cpufreq/amd-pstate.h
@@ -42,13 +42,17 @@ struct amd_aperf_mperf {
@@ -996,7 +836,7 @@ index bc341f35908d7..fb240a8702892 100644
* @boost_supported: check whether the Processor or SBIOS supports boost mode
* @hw_prefcore: check whether HW supports preferred core featue.
* Only when hw_prefcore and early prefcore param are true,
-@@ -95,6 +99,21 @@ struct amd_cpudata {
+@@ -95,6 +99,8 @@ struct amd_cpudata {
u32 policy;
u64 cppc_cap1_cached;
bool suspended;
@@ -1004,22 +844,9 @@ index bc341f35908d7..fb240a8702892 100644
+ bool boost_state;
};
-+/**
-+ * struct amd_pstate_global_params - Global parameters, mostly tunable via sysfs.
-+ * @cpb_boost: Whether or not to use boost CPU P-states.
-+ * @cpb_supported: Whether or not CPU boost P-states are available
-+ * based on the MSR_K7_HWCR bit[25] state
-+ */
-+struct amd_pstate_global_params {
-+ bool cpb_boost;
-+ bool cpb_supported;
-+};
-+
-+extern struct amd_pstate_global_params amd_pstate_global_params;
-+
#endif /* _LINUX_AMD_PSTATE_H */
diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
-index fd9c3ed21f49c..35296d8f9cdfa 100644
+index fd9c3ed21f49..bbbeb8b90313 100644
--- a/drivers/cpufreq/cpufreq.c
+++ b/drivers/cpufreq/cpufreq.c
@@ -614,10 +614,9 @@ static ssize_t show_boost(struct kobject *kobj,
@@ -1049,40 +876,16 @@ index fd9c3ed21f49c..35296d8f9cdfa 100644
return -EINVAL;
if (!cpufreq_driver->boost_enabled)
-@@ -1430,8 +1429,18 @@ static int cpufreq_online(unsigned int cpu)
- goto out_free_policy;
+@@ -1431,7 +1430,8 @@ static int cpufreq_online(unsigned int cpu)
}
-- /* Let the per-policy boost flag mirror the cpufreq_driver boost during init */
+ /* Let the per-policy boost flag mirror the cpufreq_driver boost during init */
- policy->boost_enabled = cpufreq_boost_enabled() && policy_has_boost_freq(policy);
-+ /* init boost state to prepare set_boost callback for each CPU */
-+ if (cpufreq_driver->init_boost) {
-+ ret = cpufreq_driver->init_boost(policy);
-+ if (ret) {
-+ pr_debug("%s: %d: boost initialization failed\n", __func__,
-+ __LINE__);
-+ goto out_offline_policy;
-+ }
-+ } else {
-+ /* Let the per-policy boost flag mirror the cpufreq_driver boost during init */
-+ policy->boost_enabled = cpufreq_boost_enabled() && policy_has_boost_freq(policy);
-+ }
++ if (cpufreq_boost_enabled() && policy_has_boost_freq(policy))
++ policy->boost_enabled = true;
/*
* The initialization has succeeded and the policy is online.
-diff --git a/include/linux/cpufreq.h b/include/linux/cpufreq.h
-index 9956afb9acc23..3eb5ce712c587 100644
---- a/include/linux/cpufreq.h
-+++ b/include/linux/cpufreq.h
-@@ -399,6 +399,8 @@ struct cpufreq_driver {
- bool boost_enabled;
- int (*set_boost)(struct cpufreq_policy *policy, int state);
-
-+ /* initialize boost state to be consistent before calling set_boost */
-+ int (*init_boost)(struct cpufreq_policy *policy);
- /*
- * Set by drivers that want to register with the energy model after the
- * policy is properly initialized, but before the governor is started.
--
2.45.2
diff --git a/SOURCES/0001-ntsync.patch b/SOURCES/0001-ntsync.patch
index 4f3d773..00a3538 100644
--- a/SOURCES/0001-ntsync.patch
+++ b/SOURCES/0001-ntsync.patch
@@ -1,6 +1,6 @@
-From 3aa16b2e2770401eaac82fcd8f279ee3d0ab409c Mon Sep 17 00:00:00 2001
+From 0f079f29cb143deb96685a5d7e5c40ee3e709cf8 Mon Sep 17 00:00:00 2001
From: Peter Jung <admin@ptr1337.dev>
-Date: Thu, 27 Jun 2024 16:53:18 +0200
+Date: Fri, 5 Jul 2024 10:32:47 +0200
Subject: [PATCH 08/10] ntsync
Signed-off-by: Peter Jung <admin@ptr1337.dev>
@@ -28,7 +28,7 @@ Signed-off-by: Peter Jung <admin@ptr1337.dev>
create mode 100644 tools/testing/selftests/drivers/ntsync/ntsync.c
diff --git a/Documentation/userspace-api/index.rst b/Documentation/userspace-api/index.rst
-index afecfe3cc4a86..d5745a500fa7b 100644
+index afecfe3cc4a8..d5745a500fa7 100644
--- a/Documentation/userspace-api/index.rst
+++ b/Documentation/userspace-api/index.rst
@@ -62,6 +62,7 @@ Everything else
@@ -40,7 +40,7 @@ index afecfe3cc4a86..d5745a500fa7b 100644
.. only:: subproject and html
diff --git a/Documentation/userspace-api/ioctl/ioctl-number.rst b/Documentation/userspace-api/ioctl/ioctl-number.rst
-index c472423412bf2..a141e8e65c5d3 100644
+index c472423412bf..a141e8e65c5d 100644
--- a/Documentation/userspace-api/ioctl/ioctl-number.rst
+++ b/Documentation/userspace-api/ioctl/ioctl-number.rst
@@ -174,6 +174,8 @@ Code Seq# Include File Comments
@@ -54,7 +54,7 @@ index c472423412bf2..a141e8e65c5d3 100644
'P' 60-6F sound/sscape_ioctl.h conflict!
diff --git a/Documentation/userspace-api/ntsync.rst b/Documentation/userspace-api/ntsync.rst
new file mode 100644
-index 0000000000000..767844637a7df
+index 000000000000..767844637a7d
--- /dev/null
+++ b/Documentation/userspace-api/ntsync.rst
@@ -0,0 +1,398 @@
@@ -457,7 +457,7 @@ index 0000000000000..767844637a7df
+ ``objs`` and in ``alert``. If this is attempted, the function fails
+ with ``EINVAL``.
diff --git a/MAINTAINERS b/MAINTAINERS
-index 3121709d99e3b..baa28e4151aa6 100644
+index 3121709d99e3..baa28e4151aa 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -15720,6 +15720,15 @@ T: git https://github.com/Paragon-Software-Group/linux-ntfs3.git
@@ -477,7 +477,7 @@ index 3121709d99e3b..baa28e4151aa6 100644
M: Finn Thain <fthain@linux-m68k.org>
L: linux-m68k@lists.linux-m68k.org
diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig
-index 4fb291f0bf7c8..801ed229ed7dc 100644
+index 4fb291f0bf7c..801ed229ed7d 100644
--- a/drivers/misc/Kconfig
+++ b/drivers/misc/Kconfig
@@ -506,6 +506,17 @@ config OPEN_DICE
@@ -499,7 +499,7 @@ index 4fb291f0bf7c8..801ed229ed7dc 100644
tristate "Guest vCPU stall detector"
depends on OF && HAS_IOMEM
diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
-index ea6ea5bbbc9c6..153a3f4837e8c 100644
+index ea6ea5bbbc9c..153a3f4837e8 100644
--- a/drivers/misc/Makefile
+++ b/drivers/misc/Makefile
@@ -59,6 +59,7 @@ obj-$(CONFIG_PVPANIC) += pvpanic/
@@ -512,7 +512,7 @@ index ea6ea5bbbc9c6..153a3f4837e8c 100644
obj-$(CONFIG_GP_PCI1XXXX) += mchp_pci1xxxx/
diff --git a/drivers/misc/ntsync.c b/drivers/misc/ntsync.c
new file mode 100644
-index 0000000000000..87a24798a5c7b
+index 000000000000..87a24798a5c7
--- /dev/null
+++ b/drivers/misc/ntsync.c
@@ -0,0 +1,1232 @@
@@ -1750,7 +1750,7 @@ index 0000000000000..87a24798a5c7b
+MODULE_LICENSE("GPL");
diff --git a/include/uapi/linux/ntsync.h b/include/uapi/linux/ntsync.h
new file mode 100644
-index 0000000000000..4a8095a3fc34c
+index 000000000000..4a8095a3fc34
--- /dev/null
+++ b/include/uapi/linux/ntsync.h
@@ -0,0 +1,62 @@
@@ -1817,7 +1817,7 @@ index 0000000000000..4a8095a3fc34c
+
+#endif
diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
-index e1504833654db..6f95206325e1f 100644
+index e1504833654d..6f95206325e1 100644
--- a/tools/testing/selftests/Makefile
+++ b/tools/testing/selftests/Makefile
@@ -16,6 +16,7 @@ TARGETS += damon
@@ -1830,14 +1830,14 @@ index e1504833654db..6f95206325e1f 100644
TARGETS += drivers/net/team
diff --git a/tools/testing/selftests/drivers/ntsync/.gitignore b/tools/testing/selftests/drivers/ntsync/.gitignore
new file mode 100644
-index 0000000000000..848573a3d3eaf
+index 000000000000..848573a3d3ea
--- /dev/null
+++ b/tools/testing/selftests/drivers/ntsync/.gitignore
@@ -0,0 +1 @@
+ntsync
diff --git a/tools/testing/selftests/drivers/ntsync/Makefile b/tools/testing/selftests/drivers/ntsync/Makefile
new file mode 100644
-index 0000000000000..dbf2b055c0b28
+index 000000000000..dbf2b055c0b2
--- /dev/null
+++ b/tools/testing/selftests/drivers/ntsync/Makefile
@@ -0,0 +1,7 @@
@@ -1850,14 +1850,14 @@ index 0000000000000..dbf2b055c0b28
+include ../../lib.mk
diff --git a/tools/testing/selftests/drivers/ntsync/config b/tools/testing/selftests/drivers/ntsync/config
new file mode 100644
-index 0000000000000..60539c826d062
+index 000000000000..60539c826d06
--- /dev/null
+++ b/tools/testing/selftests/drivers/ntsync/config
@@ -0,0 +1 @@
+CONFIG_WINESYNC=y
diff --git a/tools/testing/selftests/drivers/ntsync/ntsync.c b/tools/testing/selftests/drivers/ntsync/ntsync.c
new file mode 100644
-index 0000000000000..5fa2c9a0768c0
+index 000000000000..5fa2c9a0768c
--- /dev/null
+++ b/tools/testing/selftests/drivers/ntsync/ntsync.c
@@ -0,0 +1,1407 @@
diff --git a/SOURCES/cachy-bbr3.patch b/SOURCES/cachy-bbr3.patch
new file mode 100644
index 0000000..c714f9d
--- /dev/null
+++ b/SOURCES/cachy-bbr3.patch
@@ -0,0 +1,3386 @@
+From 3b7e1abbffd80645ae13f4fb033fa0f49a641e27 Mon Sep 17 00:00:00 2001
+From: Peter Jung <admin@ptr1337.dev>
+Date: Fri, 5 Jul 2024 10:31:38 +0200
+Subject: [PATCH 03/10] bbr3
+
+Signed-off-by: Peter Jung <admin@ptr1337.dev>
+---
+ include/linux/tcp.h | 4 +-
+ include/net/inet_connection_sock.h | 4 +-
+ include/net/tcp.h | 72 +-
+ include/uapi/linux/inet_diag.h | 23 +
+ include/uapi/linux/rtnetlink.h | 4 +-
+ include/uapi/linux/tcp.h | 1 +
+ net/ipv4/Kconfig | 21 +-
+ net/ipv4/bpf_tcp_ca.c | 9 +-
+ net/ipv4/tcp.c | 3 +
+ net/ipv4/tcp_bbr.c | 2230 +++++++++++++++++++++-------
+ net/ipv4/tcp_cong.c | 1 +
+ net/ipv4/tcp_input.c | 40 +-
+ net/ipv4/tcp_minisocks.c | 2 +
+ net/ipv4/tcp_output.c | 48 +-
+ net/ipv4/tcp_rate.c | 30 +-
+ net/ipv4/tcp_timer.c | 1 +
+ 16 files changed, 1940 insertions(+), 553 deletions(-)
+
+diff --git a/include/linux/tcp.h b/include/linux/tcp.h
+index 55399ee2a57e..c6193d87a8e6 100644
+--- a/include/linux/tcp.h
++++ b/include/linux/tcp.h
+@@ -368,7 +368,9 @@ struct tcp_sock {
+ u8 compressed_ack;
+ u8 dup_ack_counter:2,
+ tlp_retrans:1, /* TLP is a retransmission */
+- unused:5;
++ fast_ack_mode:2, /* which fast ack mode ? */
++ tlp_orig_data_app_limited:1, /* app-limited before TLP rtx? */
++ unused:2;
+ u8 thin_lto : 1,/* Use linear timeouts for thin streams */
+ recvmsg_inq : 1,/* Indicate # of bytes in queue upon recvmsg */
+ fastopen_connect:1, /* FASTOPEN_CONNECT sockopt */
+diff --git a/include/net/inet_connection_sock.h b/include/net/inet_connection_sock.h
+index 146ece8563ca..0217922aa100 100644
+--- a/include/net/inet_connection_sock.h
++++ b/include/net/inet_connection_sock.h
+@@ -137,8 +137,8 @@ struct inet_connection_sock {
+ u32 icsk_probes_tstamp;
+ u32 icsk_user_timeout;
+
+- u64 icsk_ca_priv[104 / sizeof(u64)];
+-#define ICSK_CA_PRIV_SIZE sizeof_field(struct inet_connection_sock, icsk_ca_priv)
++#define ICSK_CA_PRIV_SIZE (144)
++ u64 icsk_ca_priv[ICSK_CA_PRIV_SIZE / sizeof(u64)];
+ };
+
+ #define ICSK_TIME_RETRANS 1 /* Retransmit timer */
+diff --git a/include/net/tcp.h b/include/net/tcp.h
+index 2bcf30381d75..54e1af0789b8 100644
+--- a/include/net/tcp.h
++++ b/include/net/tcp.h
+@@ -381,6 +381,8 @@ static inline void tcp_dec_quickack_mode(struct sock *sk)
+ #define TCP_ECN_QUEUE_CWR 2
+ #define TCP_ECN_DEMAND_CWR 4
+ #define TCP_ECN_SEEN 8
++#define TCP_ECN_LOW 16
++#define TCP_ECN_ECT_PERMANENT 32
+
+ enum tcp_tw_status {
+ TCP_TW_SUCCESS = 0,
+@@ -782,6 +784,15 @@ static inline void tcp_fast_path_check(struct sock *sk)
+
+ u32 tcp_delack_max(const struct sock *sk);
+
++static inline void tcp_set_ecn_low_from_dst(struct sock *sk,
++ const struct dst_entry *dst)
++{
++ struct tcp_sock *tp = tcp_sk(sk);
++
++ if (dst_feature(dst, RTAX_FEATURE_ECN_LOW))
++ tp->ecn_flags |= TCP_ECN_LOW;
++}
++
+ /* Compute the actual rto_min value */
+ static inline u32 tcp_rto_min(const struct sock *sk)
+ {
+@@ -887,6 +898,11 @@ static inline u32 tcp_stamp_us_delta(u64 t1, u64 t0)
+ return max_t(s64, t1 - t0, 0);
+ }
+
++static inline u32 tcp_stamp32_us_delta(u32 t1, u32 t0)
++{
++ return max_t(s32, t1 - t0, 0);
++}
++
+ /* provide the departure time in us unit */
+ static inline u64 tcp_skb_timestamp_us(const struct sk_buff *skb)
+ {
+@@ -975,9 +991,14 @@ struct tcp_skb_cb {
+ /* pkts S/ACKed so far upon tx of skb, incl retrans: */
+ __u32 delivered;
+ /* start of send pipeline phase */
+- u64 first_tx_mstamp;
++ u32 first_tx_mstamp;
+ /* when we reached the "delivered" count */
+- u64 delivered_mstamp;
++ u32 delivered_mstamp;
++#define TCPCB_IN_FLIGHT_BITS 20
++#define TCPCB_IN_FLIGHT_MAX ((1U << TCPCB_IN_FLIGHT_BITS) - 1)
++ u32 in_flight:20, /* packets in flight at transmit */
++ unused2:12;
++ u32 lost; /* packets lost so far upon tx of skb */
+ } tx; /* only used for outgoing skbs */
+ union {
+ struct inet_skb_parm h4;
+@@ -1081,6 +1102,7 @@ enum tcp_ca_event {
+ CA_EVENT_LOSS, /* loss timeout */
+ CA_EVENT_ECN_NO_CE, /* ECT set, but not CE marked */
+ CA_EVENT_ECN_IS_CE, /* received CE marked IP packet */
++ CA_EVENT_TLP_RECOVERY, /* a lost segment was repaired by TLP probe */
+ };
+
+ /* Information about inbound ACK, passed to cong_ops->in_ack_event() */
+@@ -1103,7 +1125,11 @@ enum tcp_ca_ack_event_flags {
+ #define TCP_CONG_NON_RESTRICTED 0x1
+ /* Requires ECN/ECT set on all packets */
+ #define TCP_CONG_NEEDS_ECN 0x2
+-#define TCP_CONG_MASK (TCP_CONG_NON_RESTRICTED | TCP_CONG_NEEDS_ECN)
++/* Wants notification of CE events (CA_EVENT_ECN_IS_CE, CA_EVENT_ECN_NO_CE). */
++#define TCP_CONG_WANTS_CE_EVENTS 0x4
++#define TCP_CONG_MASK (TCP_CONG_NON_RESTRICTED | \
++ TCP_CONG_NEEDS_ECN | \
++ TCP_CONG_WANTS_CE_EVENTS)
+
+ union tcp_cc_info;
+
+@@ -1123,10 +1149,13 @@ struct ack_sample {
+ */
+ struct rate_sample {
+ u64 prior_mstamp; /* starting timestamp for interval */
++ u32 prior_lost; /* tp->lost at "prior_mstamp" */
+ u32 prior_delivered; /* tp->delivered at "prior_mstamp" */
+ u32 prior_delivered_ce;/* tp->delivered_ce at "prior_mstamp" */
++ u32 tx_in_flight; /* packets in flight at starting timestamp */
++ s32 lost; /* number of packets lost over interval */
+ s32 delivered; /* number of packets delivered over interval */
+- s32 delivered_ce; /* number of packets delivered w/ CE marks*/
++ s32 delivered_ce; /* packets delivered w/ CE mark over interval */
+ long interval_us; /* time for tp->delivered to incr "delivered" */
+ u32 snd_interval_us; /* snd interval for delivered packets */
+ u32 rcv_interval_us; /* rcv interval for delivered packets */
+@@ -1137,7 +1166,9 @@ struct rate_sample {
+ u32 last_end_seq; /* end_seq of most recently ACKed packet */
+ bool is_app_limited; /* is sample from packet with bubble in pipe? */
+ bool is_retrans; /* is sample from retransmission? */
++ bool is_acking_tlp_retrans_seq; /* ACKed a TLP retransmit sequence? */
+ bool is_ack_delayed; /* is this (likely) a delayed ACK? */
++ bool is_ece; /* did this ACK have ECN marked? */
+ };
+
+ struct tcp_congestion_ops {
+@@ -1161,8 +1192,11 @@ struct tcp_congestion_ops {
+ /* hook for packet ack accounting (optional) */
+ void (*pkts_acked)(struct sock *sk, const struct ack_sample *sample);
+
+- /* override sysctl_tcp_min_tso_segs */
+- u32 (*min_tso_segs)(struct sock *sk);
++ /* pick target number of segments per TSO/GSO skb (optional): */
++ u32 (*tso_segs)(struct sock *sk, unsigned int mss_now);
++
++ /* react to a specific lost skb (optional) */
++ void (*skb_marked_lost)(struct sock *sk, const struct sk_buff *skb);
+
+ /* call when packets are delivered to update cwnd and pacing rate,
+ * after all the ca_state processing. (optional)
+@@ -1228,6 +1262,14 @@ static inline char *tcp_ca_get_name_by_key(u32 key, char *buffer)
+ }
+ #endif
+
++static inline bool tcp_ca_wants_ce_events(const struct sock *sk)
++{
++ const struct inet_connection_sock *icsk = inet_csk(sk);
++
++ return icsk->icsk_ca_ops->flags & (TCP_CONG_NEEDS_ECN |
++ TCP_CONG_WANTS_CE_EVENTS);
++}
++
+ static inline bool tcp_ca_needs_ecn(const struct sock *sk)
+ {
+ const struct inet_connection_sock *icsk = inet_csk(sk);
+@@ -1247,6 +1289,7 @@ static inline void tcp_ca_event(struct sock *sk, const enum tcp_ca_event event)
+ void tcp_set_ca_state(struct sock *sk, const u8 ca_state);
+
+ /* From tcp_rate.c */
++void tcp_set_tx_in_flight(struct sock *sk, struct sk_buff *skb);
+ void tcp_rate_skb_sent(struct sock *sk, struct sk_buff *skb);
+ void tcp_rate_skb_delivered(struct sock *sk, struct sk_buff *skb,
+ struct rate_sample *rs);
+@@ -1259,6 +1302,21 @@ static inline bool tcp_skb_sent_after(u64 t1, u64 t2, u32 seq1, u32 seq2)
+ return t1 > t2 || (t1 == t2 && after(seq1, seq2));
+ }
+
++/* If a retransmit failed due to local qdisc congestion or other local issues,
++ * then we may have called tcp_set_skb_tso_segs() to increase the number of
++ * segments in the skb without increasing the tx.in_flight. In all other cases,
++ * the tx.in_flight should be at least as big as the pcount of the sk_buff. We
++ * do not have the state to know whether a retransmit failed due to local qdisc
++ * congestion or other local issues, so to avoid spurious warnings we consider
++ * that any skb marked lost may have suffered that fate.
++ */
++static inline bool tcp_skb_tx_in_flight_is_suspicious(u32 skb_pcount,
++ u32 skb_sacked_flags,
++ u32 tx_in_flight)
++{
++ return (skb_pcount > tx_in_flight) && !(skb_sacked_flags & TCPCB_LOST);
++}
++
+ /* These functions determine how the current flow behaves in respect of SACK
+ * handling. SACK is negotiated with the peer, and therefore it can vary
+ * between different flows.
+@@ -2417,7 +2475,7 @@ struct tcp_plb_state {
+ u8 consec_cong_rounds:5, /* consecutive congested rounds */
+ unused:3;
+ u32 pause_until; /* jiffies32 when PLB can resume rerouting */
+-};
++} __attribute__ ((__packed__));
+
+ static inline void tcp_plb_init(const struct sock *sk,
+ struct tcp_plb_state *plb)
+diff --git a/include/uapi/linux/inet_diag.h b/include/uapi/linux/inet_diag.h
+index 50655de04c9b..82f8bd8f0d16 100644
+--- a/include/uapi/linux/inet_diag.h
++++ b/include/uapi/linux/inet_diag.h
+@@ -229,6 +229,29 @@ struct tcp_bbr_info {
+ __u32 bbr_min_rtt; /* min-filtered RTT in uSec */
+ __u32 bbr_pacing_gain; /* pacing gain shifted left 8 bits */
+ __u32 bbr_cwnd_gain; /* cwnd gain shifted left 8 bits */
++ __u32 bbr_bw_hi_lsb; /* lower 32 bits of bw_hi */
++ __u32 bbr_bw_hi_msb; /* upper 32 bits of bw_hi */
++ __u32 bbr_bw_lo_lsb; /* lower 32 bits of bw_lo */
++ __u32 bbr_bw_lo_msb; /* upper 32 bits of bw_lo */
++ __u8 bbr_mode; /* current bbr_mode in state machine */
++ __u8 bbr_phase; /* current state machine phase */
++ __u8 unused1; /* alignment padding; not used yet */
++ __u8 bbr_version; /* BBR algorithm version */
++ __u32 bbr_inflight_lo; /* lower short-term data volume bound */
++ __u32 bbr_inflight_hi; /* higher long-term data volume bound */
++ __u32 bbr_extra_acked; /* max excess packets ACKed in epoch */
++};
++
++/* TCP BBR congestion control bbr_phase as reported in netlink/ss stats. */
++enum tcp_bbr_phase {
++ BBR_PHASE_INVALID = 0,
++ BBR_PHASE_STARTUP = 1,
++ BBR_PHASE_DRAIN = 2,
++ BBR_PHASE_PROBE_RTT = 3,
++ BBR_PHASE_PROBE_BW_UP = 4,
++ BBR_PHASE_PROBE_BW_DOWN = 5,
++ BBR_PHASE_PROBE_BW_CRUISE = 6,
++ BBR_PHASE_PROBE_BW_REFILL = 7,
+ };
+
+ union tcp_cc_info {
+diff --git a/include/uapi/linux/rtnetlink.h b/include/uapi/linux/rtnetlink.h
+index 3b687d20c9ed..a7c30c243b54 100644
+--- a/include/uapi/linux/rtnetlink.h
++++ b/include/uapi/linux/rtnetlink.h
+@@ -507,12 +507,14 @@ enum {
+ #define RTAX_FEATURE_TIMESTAMP (1 << 2) /* unused */
+ #define RTAX_FEATURE_ALLFRAG (1 << 3) /* unused */
+ #define RTAX_FEATURE_TCP_USEC_TS (1 << 4)
++#define RTAX_FEATURE_ECN_LOW (1 << 5)
+
+ #define RTAX_FEATURE_MASK (RTAX_FEATURE_ECN | \
+ RTAX_FEATURE_SACK | \
+ RTAX_FEATURE_TIMESTAMP | \
+ RTAX_FEATURE_ALLFRAG | \
+- RTAX_FEATURE_TCP_USEC_TS)
++ RTAX_FEATURE_TCP_USEC_TS | \
++ RTAX_FEATURE_ECN_LOW)
+
+ struct rta_session {
+ __u8 proto;
+diff --git a/include/uapi/linux/tcp.h b/include/uapi/linux/tcp.h
+index c07e9f90c084..5c88336ced60 100644
+--- a/include/uapi/linux/tcp.h
++++ b/include/uapi/linux/tcp.h
+@@ -176,6 +176,7 @@ enum tcp_fastopen_client_fail {
+ #define TCPI_OPT_ECN_SEEN 16 /* we received at least one packet with ECT */
+ #define TCPI_OPT_SYN_DATA 32 /* SYN-ACK acked data in SYN sent or rcvd */
+ #define TCPI_OPT_USEC_TS 64 /* usec timestamps */
++#define TCPI_OPT_ECN_LOW 128 /* Low-latency ECN configured at init */
+
+ /*
+ * Sender's congestion state indicating normal or abnormal situations
+diff --git a/net/ipv4/Kconfig b/net/ipv4/Kconfig
+index 8e94ed7c56a0..50dc9970cad2 100644
+--- a/net/ipv4/Kconfig
++++ b/net/ipv4/Kconfig
+@@ -668,15 +668,18 @@ config TCP_CONG_BBR
+ default n
+ help
+
+- BBR (Bottleneck Bandwidth and RTT) TCP congestion control aims to
+- maximize network utilization and minimize queues. It builds an explicit
+- model of the bottleneck delivery rate and path round-trip propagation
+- delay. It tolerates packet loss and delay unrelated to congestion. It
+- can operate over LAN, WAN, cellular, wifi, or cable modem links. It can
+- coexist with flows that use loss-based congestion control, and can
+- operate with shallow buffers, deep buffers, bufferbloat, policers, or
+- AQM schemes that do not provide a delay signal. It requires the fq
+- ("Fair Queue") pacing packet scheduler.
++ BBR (Bottleneck Bandwidth and RTT) TCP congestion control is a
++ model-based congestion control algorithm that aims to maximize
++ network utilization, keep queues and retransmit rates low, and to be
++ able to coexist with Reno/CUBIC in common scenarios. It builds an
++ explicit model of the network path. It tolerates a targeted degree
++ of random packet loss and delay. It can operate over LAN, WAN,
++ cellular, wifi, or cable modem links, and can use shallow-threshold
++ ECN signals. It can coexist to some degree with flows that use
++ loss-based congestion control, and can operate with shallow buffers,
++ deep buffers, bufferbloat, policers, or AQM schemes that do not
++ provide a delay signal. It requires pacing, using either TCP internal
++ pacing or the fq ("Fair Queue") pacing packet scheduler.
+
+ choice
+ prompt "Default TCP congestion control"
+diff --git a/net/ipv4/bpf_tcp_ca.c b/net/ipv4/bpf_tcp_ca.c
+index 7f518ea5f4ac..8f9b677c20e1 100644
+--- a/net/ipv4/bpf_tcp_ca.c
++++ b/net/ipv4/bpf_tcp_ca.c
+@@ -302,11 +302,15 @@ static void bpf_tcp_ca_pkts_acked(struct sock *sk, const struct ack_sample *samp
+ {
+ }
+
+-static u32 bpf_tcp_ca_min_tso_segs(struct sock *sk)
++static u32 bpf_tcp_ca_tso_segs(struct sock *sk, unsigned int mss_now)
+ {
+ return 0;
+ }
+
++static void bpf_tcp_ca_skb_marked_lost(struct sock *sk, const struct sk_buff *skb)
++{
++}
++
+ static void bpf_tcp_ca_cong_control(struct sock *sk, const struct rate_sample *rs)
+ {
+ }
+@@ -336,7 +340,8 @@ static struct tcp_congestion_ops __bpf_ops_tcp_congestion_ops = {
+ .cwnd_event = bpf_tcp_ca_cwnd_event,
+ .in_ack_event = bpf_tcp_ca_in_ack_event,
+ .pkts_acked = bpf_tcp_ca_pkts_acked,
+- .min_tso_segs = bpf_tcp_ca_min_tso_segs,
++ .tso_segs = bpf_tcp_ca_tso_segs,
++ .skb_marked_lost = bpf_tcp_ca_skb_marked_lost,
+ .cong_control = bpf_tcp_ca_cong_control,
+ .undo_cwnd = bpf_tcp_ca_undo_cwnd,
+ .sndbuf_expand = bpf_tcp_ca_sndbuf_expand,
+diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
+index 77ee1eda3fd8..b3d43b2b8617 100644
+--- a/net/ipv4/tcp.c
++++ b/net/ipv4/tcp.c
+@@ -3099,6 +3099,7 @@ int tcp_disconnect(struct sock *sk, int flags)
+ tp->rx_opt.dsack = 0;
+ tp->rx_opt.num_sacks = 0;
+ tp->rcv_ooopack = 0;
++ tp->fast_ack_mode = 0;
+
+
+ /* Clean up fastopen related fields */
+@@ -3825,6 +3826,8 @@ void tcp_get_info(struct sock *sk, struct tcp_info *info)
+ info->tcpi_options |= TCPI_OPT_ECN;
+ if (tp->ecn_flags & TCP_ECN_SEEN)
+ info->tcpi_options |= TCPI_OPT_ECN_SEEN;
++ if (tp->ecn_flags & TCP_ECN_LOW)
++ info->tcpi_options |= TCPI_OPT_ECN_LOW;
+ if (tp->syn_data_acked)
+ info->tcpi_options |= TCPI_OPT_SYN_DATA;
+ if (tp->tcp_usec_ts)
+diff --git a/net/ipv4/tcp_bbr.c b/net/ipv4/tcp_bbr.c
+index 05dc2d05bc7c..61ed3e549350 100644
+--- a/net/ipv4/tcp_bbr.c
++++ b/net/ipv4/tcp_bbr.c
+@@ -1,18 +1,19 @@
+-/* Bottleneck Bandwidth and RTT (BBR) congestion control
++/* BBR (Bottleneck Bandwidth and RTT) congestion control
+ *
+- * BBR congestion control computes the sending rate based on the delivery
+- * rate (throughput) estimated from ACKs. In a nutshell:
++ * BBR is a model-based congestion control algorithm that aims for low queues,
++ * low loss, and (bounded) Reno/CUBIC coexistence. To maintain a model of the
++ * network path, it uses measurements of bandwidth and RTT, as well as (if they
++ * occur) packet loss and/or shallow-threshold ECN signals. Note that although
++ * it can use ECN or loss signals explicitly, it does not require either; it
++ * can bound its in-flight data based on its estimate of the BDP.
+ *
+- * On each ACK, update our model of the network path:
+- * bottleneck_bandwidth = windowed_max(delivered / elapsed, 10 round trips)
+- * min_rtt = windowed_min(rtt, 10 seconds)
+- * pacing_rate = pacing_gain * bottleneck_bandwidth
+- * cwnd = max(cwnd_gain * bottleneck_bandwidth * min_rtt, 4)
+- *
+- * The core algorithm does not react directly to packet losses or delays,
+- * although BBR may adjust the size of next send per ACK when loss is
+- * observed, or adjust the sending rate if it estimates there is a
+- * traffic policer, in order to keep the drop rate reasonable.
++ * The model has both higher and lower bounds for the operating range:
++ * lo: bw_lo, inflight_lo: conservative short-term lower bound
++ * hi: bw_hi, inflight_hi: robust long-term upper bound
++ * The bandwidth-probing time scale is (a) extended dynamically based on
++ * estimated BDP to improve coexistence with Reno/CUBIC; (b) bounded by
++ * an interactive wall-clock time-scale to be more scalable and responsive
++ * than Reno and CUBIC.
+ *
+ * Here is a state transition diagram for BBR:
+ *
+@@ -65,6 +66,13 @@
+ #include <linux/random.h>
+ #include <linux/win_minmax.h>
+
++#include <trace/events/tcp.h>
++#include "tcp_dctcp.h"
++
++#define BBR_VERSION 3
++
++#define bbr_param(sk,name) (bbr_ ## name)
++
+ /* Scale factor for rate in pkt/uSec unit to avoid truncation in bandwidth
+ * estimation. The rate unit ~= (1500 bytes / 1 usec / 2^24) ~= 715 bps.
+ * This handles bandwidths from 0.06pps (715bps) to 256Mpps (3Tbps) in a u32.
+@@ -85,36 +93,41 @@ enum bbr_mode {
+ BBR_PROBE_RTT, /* cut inflight to min to probe min_rtt */
+ };
+
++/* How does the incoming ACK stream relate to our bandwidth probing? */
++enum bbr_ack_phase {
++ BBR_ACKS_INIT, /* not probing; not getting probe feedback */
++ BBR_ACKS_REFILLING, /* sending at est. bw to fill pipe */
++ BBR_ACKS_PROBE_STARTING, /* inflight rising to probe bw */
++ BBR_ACKS_PROBE_FEEDBACK, /* getting feedback from bw probing */
++ BBR_ACKS_PROBE_STOPPING, /* stopped probing; still getting feedback */
++};
++
+ /* BBR congestion control block */
+ struct bbr {
+ u32 min_rtt_us; /* min RTT in min_rtt_win_sec window */
+ u32 min_rtt_stamp; /* timestamp of min_rtt_us */
+ u32 probe_rtt_done_stamp; /* end time for BBR_PROBE_RTT mode */
+- struct minmax bw; /* Max recent delivery rate in pkts/uS << 24 */
+- u32 rtt_cnt; /* count of packet-timed rounds elapsed */
++ u32 probe_rtt_min_us; /* min RTT in probe_rtt_win_ms win */
++ u32 probe_rtt_min_stamp; /* timestamp of probe_rtt_min_us*/
+ u32 next_rtt_delivered; /* scb->tx.delivered at end of round */
+ u64 cycle_mstamp; /* time of this cycle phase start */
+- u32 mode:3, /* current bbr_mode in state machine */
++ u32 mode:2, /* current bbr_mode in state machine */
+ prev_ca_state:3, /* CA state on previous ACK */
+- packet_conservation:1, /* use packet conservation? */
+ round_start:1, /* start of packet-timed tx->ack round? */
++ ce_state:1, /* If most recent data has CE bit set */
++ bw_probe_up_rounds:5, /* cwnd-limited rounds in PROBE_UP */
++ try_fast_path:1, /* can we take fast path? */
+ idle_restart:1, /* restarting after idle? */
+ probe_rtt_round_done:1, /* a BBR_PROBE_RTT round at 4 pkts? */
+- unused:13,
+- lt_is_sampling:1, /* taking long-term ("LT") samples now? */
+- lt_rtt_cnt:7, /* round trips in long-term interval */
+- lt_use_bw:1; /* use lt_bw as our bw estimate? */
+- u32 lt_bw; /* LT est delivery rate in pkts/uS << 24 */
+- u32 lt_last_delivered; /* LT intvl start: tp->delivered */
+- u32 lt_last_stamp; /* LT intvl start: tp->delivered_mstamp */
+- u32 lt_last_lost; /* LT intvl start: tp->lost */
++ init_cwnd:7, /* initial cwnd */
++ unused_1:10;
+ u32 pacing_gain:10, /* current gain for setting pacing rate */
+ cwnd_gain:10, /* current gain for setting cwnd */
+ full_bw_reached:1, /* reached full bw in Startup? */
+ full_bw_cnt:2, /* number of rounds without large bw gains */
+- cycle_idx:3, /* current index in pacing_gain cycle array */
++ cycle_idx:2, /* current index in pacing_gain cycle array */
+ has_seen_rtt:1, /* have we seen an RTT sample yet? */
+- unused_b:5;
++ unused_2:6;
+ u32 prior_cwnd; /* prior cwnd upon entering loss recovery */
+ u32 full_bw; /* recent bw, to estimate if pipe is full */
+
+@@ -124,19 +137,67 @@ struct bbr {
+ u32 ack_epoch_acked:20, /* packets (S)ACKed in sampling epoch */
+ extra_acked_win_rtts:5, /* age of extra_acked, in round trips */
+ extra_acked_win_idx:1, /* current index in extra_acked array */
+- unused_c:6;
++ /* BBR v3 state: */
++ full_bw_now:1, /* recently reached full bw plateau? */
++ startup_ecn_rounds:2, /* consecutive hi ECN STARTUP rounds */
++ loss_in_cycle:1, /* packet loss in this cycle? */
++ ecn_in_cycle:1, /* ECN in this cycle? */
++ unused_3:1;
++ u32 loss_round_delivered; /* scb->tx.delivered ending loss round */
++ u32 undo_bw_lo; /* bw_lo before latest losses */
++ u32 undo_inflight_lo; /* inflight_lo before latest losses */
++ u32 undo_inflight_hi; /* inflight_hi before latest losses */
++ u32 bw_latest; /* max delivered bw in last round trip */
++ u32 bw_lo; /* lower bound on sending bandwidth */
++ u32 bw_hi[2]; /* max recent measured bw sample */
++ u32 inflight_latest; /* max delivered data in last round trip */
++ u32 inflight_lo; /* lower bound of inflight data range */
++ u32 inflight_hi; /* upper bound of inflight data range */
++ u32 bw_probe_up_cnt; /* packets delivered per inflight_hi incr */
++ u32 bw_probe_up_acks; /* packets (S)ACKed since inflight_hi incr */
++ u32 probe_wait_us; /* PROBE_DOWN until next clock-driven probe */
++ u32 prior_rcv_nxt; /* tp->rcv_nxt when CE state last changed */
++ u32 ecn_eligible:1, /* sender can use ECN (RTT, handshake)? */
++ ecn_alpha:9, /* EWMA delivered_ce/delivered; 0..256 */
++ bw_probe_samples:1, /* rate samples reflect bw probing? */
++ prev_probe_too_high:1, /* did last PROBE_UP go too high? */
++ stopped_risky_probe:1, /* last PROBE_UP stopped due to risk? */
++ rounds_since_probe:8, /* packet-timed rounds since probed bw */
++ loss_round_start:1, /* loss_round_delivered round trip? */
++ loss_in_round:1, /* loss marked in this round trip? */
++ ecn_in_round:1, /* ECN marked in this round trip? */
++ ack_phase:3, /* bbr_ack_phase: meaning of ACKs */
++ loss_events_in_round:4,/* losses in STARTUP round */
++ initialized:1; /* has bbr_init() been called? */
++ u32 alpha_last_delivered; /* tp->delivered at alpha update */
++ u32 alpha_last_delivered_ce; /* tp->delivered_ce at alpha update */
++
++ u8 unused_4; /* to preserve alignment */
++ struct tcp_plb_state plb;
+ };
+
+-#define CYCLE_LEN 8 /* number of phases in a pacing gain cycle */
++struct bbr_context {
++ u32 sample_bw;
++};
+
+-/* Window length of bw filter (in rounds): */
+-static const int bbr_bw_rtts = CYCLE_LEN + 2;
+ /* Window length of min_rtt filter (in sec): */
+ static const u32 bbr_min_rtt_win_sec = 10;
+ /* Minimum time (in ms) spent at bbr_cwnd_min_target in BBR_PROBE_RTT mode: */
+ static const u32 bbr_probe_rtt_mode_ms = 200;
+-/* Skip TSO below the following bandwidth (bits/sec): */
+-static const int bbr_min_tso_rate = 1200000;
++/* Window length of probe_rtt_min_us filter (in ms), and consequently the
++ * typical interval between PROBE_RTT mode entries. The default is 5000ms.
++ * Note that bbr_probe_rtt_win_ms must be <= bbr_min_rtt_win_sec * MSEC_PER_SEC
++ */
++static const u32 bbr_probe_rtt_win_ms = 5000;
++/* Proportion of cwnd to estimated BDP in PROBE_RTT, in units of BBR_UNIT: */
++static const u32 bbr_probe_rtt_cwnd_gain = BBR_UNIT * 1 / 2;
++
++/* Use min_rtt to help adapt TSO burst size, with smaller min_rtt resulting
++ * in bigger TSO bursts. We cut the RTT-based allowance in half
++ * for every 2^9 usec (aka 512 us) of RTT, so that the RTT-based allowance
++ * is below 1500 bytes after 6 * ~500 usec = 3ms.
++ */
++static const u32 bbr_tso_rtt_shift = 9;
+
+ /* Pace at ~1% below estimated bw, on average, to reduce queue at bottleneck.
+ * In order to help drive the network toward lower queues and low latency while
+@@ -146,13 +207,15 @@ static const int bbr_min_tso_rate = 1200000;
+ */
+ static const int bbr_pacing_margin_percent = 1;
+
+-/* We use a high_gain value of 2/ln(2) because it's the smallest pacing gain
++/* We use a startup_pacing_gain of 4*ln(2) because it's the smallest value
+ * that will allow a smoothly increasing pacing rate that will double each RTT
+ * and send the same number of packets per RTT that an un-paced, slow-starting
+ * Reno or CUBIC flow would:
+ */
+-static const int bbr_high_gain = BBR_UNIT * 2885 / 1000 + 1;
+-/* The pacing gain of 1/high_gain in BBR_DRAIN is calculated to typically drain
++static const int bbr_startup_pacing_gain = BBR_UNIT * 277 / 100 + 1;
++/* The gain for deriving startup cwnd: */
++static const int bbr_startup_cwnd_gain = BBR_UNIT * 2;
++/* The pacing gain in BBR_DRAIN is calculated to typically drain
+ * the queue created in BBR_STARTUP in a single round:
+ */
+ static const int bbr_drain_gain = BBR_UNIT * 1000 / 2885;
+@@ -160,13 +223,17 @@ static const int bbr_drain_gain = BBR_UNIT * 1000 / 2885;
+ static const int bbr_cwnd_gain = BBR_UNIT * 2;
+ /* The pacing_gain values for the PROBE_BW gain cycle, to discover/share bw: */
+ static const int bbr_pacing_gain[] = {
+- BBR_UNIT * 5 / 4, /* probe for more available bw */
+- BBR_UNIT * 3 / 4, /* drain queue and/or yield bw to other flows */
+- BBR_UNIT, BBR_UNIT, BBR_UNIT, /* cruise at 1.0*bw to utilize pipe, */
+- BBR_UNIT, BBR_UNIT, BBR_UNIT /* without creating excess queue... */
++ BBR_UNIT * 5 / 4, /* UP: probe for more available bw */
++ BBR_UNIT * 91 / 100, /* DOWN: drain queue and/or yield bw */
++ BBR_UNIT, /* CRUISE: try to use pipe w/ some headroom */
++ BBR_UNIT, /* REFILL: refill pipe to estimated 100% */
++};
++enum bbr_pacing_gain_phase {
++ BBR_BW_PROBE_UP = 0, /* push up inflight to probe for bw/vol */
++ BBR_BW_PROBE_DOWN = 1, /* drain excess inflight from the queue */
++ BBR_BW_PROBE_CRUISE = 2, /* use pipe, w/ headroom in queue/pipe */
++ BBR_BW_PROBE_REFILL = 3, /* v2: refill the pipe again to 100% */
+ };
+-/* Randomize the starting gain cycling phase over N phases: */
+-static const u32 bbr_cycle_rand = 7;
+
+ /* Try to keep at least this many packets in flight, if things go smoothly. For
+ * smooth functioning, a sliding window protocol ACKing every other packet
+@@ -174,24 +241,12 @@ static const u32 bbr_cycle_rand = 7;
+ */
+ static const u32 bbr_cwnd_min_target = 4;
+
+-/* To estimate if BBR_STARTUP mode (i.e. high_gain) has filled pipe... */
++/* To estimate if BBR_STARTUP or BBR_BW_PROBE_UP has filled pipe... */
+ /* If bw has increased significantly (1.25x), there may be more bw available: */
+ static const u32 bbr_full_bw_thresh = BBR_UNIT * 5 / 4;
+ /* But after 3 rounds w/o significant bw growth, estimate pipe is full: */
+ static const u32 bbr_full_bw_cnt = 3;
+
+-/* "long-term" ("LT") bandwidth estimator parameters... */
+-/* The minimum number of rounds in an LT bw sampling interval: */
+-static const u32 bbr_lt_intvl_min_rtts = 4;
+-/* If lost/delivered ratio > 20%, interval is "lossy" and we may be policed: */
+-static const u32 bbr_lt_loss_thresh = 50;
+-/* If 2 intervals have a bw ratio <= 1/8, their bw is "consistent": */
+-static const u32 bbr_lt_bw_ratio = BBR_UNIT / 8;
+-/* If 2 intervals have a bw diff <= 4 Kbit/sec their bw is "consistent": */
+-static const u32 bbr_lt_bw_diff = 4000 / 8;
+-/* If we estimate we're policed, use lt_bw for this many round trips: */
+-static const u32 bbr_lt_bw_max_rtts = 48;
+-
+ /* Gain factor for adding extra_acked to target cwnd: */
+ static const int bbr_extra_acked_gain = BBR_UNIT;
+ /* Window length of extra_acked window. */
+@@ -201,8 +256,121 @@ static const u32 bbr_ack_epoch_acked_reset_thresh = 1U << 20;
+ /* Time period for clamping cwnd increment due to ack aggregation */
+ static const u32 bbr_extra_acked_max_us = 100 * 1000;
+
++/* Flags to control BBR ECN-related behavior... */
++
++/* Ensure ACKs only ACK packets with consistent ECN CE status? */
++static const bool bbr_precise_ece_ack = true;
++
++/* Max RTT (in usec) at which to use sender-side ECN logic.
++ * Disabled when 0 (ECN allowed at any RTT).
++ */
++static const u32 bbr_ecn_max_rtt_us = 5000;
++
++/* On losses, scale down inflight and pacing rate by beta scaled by BBR_SCALE.
++ * No loss response when 0.
++ */
++static const u32 bbr_beta = BBR_UNIT * 30 / 100;
++
++/* Gain factor for ECN mark ratio samples, scaled by BBR_SCALE (1/16 = 6.25%) */
++static const u32 bbr_ecn_alpha_gain = BBR_UNIT * 1 / 16;
++
++/* The initial value for ecn_alpha; 1.0 allows a flow to respond quickly
++ * to congestion if the bottleneck is congested when the flow starts up.
++ */
++static const u32 bbr_ecn_alpha_init = BBR_UNIT;
++
++/* On ECN, cut inflight_lo to (1 - ecn_factor * ecn_alpha) scaled by BBR_SCALE.
++ * No ECN based bounding when 0.
++ */
++static const u32 bbr_ecn_factor = BBR_UNIT * 1 / 3; /* 1/3 = 33% */
++
++/* Estimate bw probing has gone too far if CE ratio exceeds this threshold.
++ * Scaled by BBR_SCALE. Disabled when 0.
++ */
++static const u32 bbr_ecn_thresh = BBR_UNIT * 1 / 2; /* 1/2 = 50% */
++
++/* If non-zero, if in a cycle with no losses but some ECN marks, after ECN
++ * clears then make the first round's increment to inflight_hi the following
++ * fraction of inflight_hi.
++ */
++static const u32 bbr_ecn_reprobe_gain = BBR_UNIT * 1 / 2;
++
++/* Estimate bw probing has gone too far if loss rate exceeds this level. */
++static const u32 bbr_loss_thresh = BBR_UNIT * 2 / 100; /* 2% loss */
++
++/* Slow down for a packet loss recovered by TLP? */
++static const bool bbr_loss_probe_recovery = true;
++
++/* Exit STARTUP if number of loss marking events in a Recovery round is >= N,
++ * and loss rate is higher than bbr_loss_thresh.
++ * Disabled if 0.
++ */
++static const u32 bbr_full_loss_cnt = 6;
++
++/* Exit STARTUP if number of round trips with ECN mark rate above ecn_thresh
++ * meets this count.
++ */
++static const u32 bbr_full_ecn_cnt = 2;
++
++/* Fraction of unutilized headroom to try to leave in path upon high loss. */
++static const u32 bbr_inflight_headroom = BBR_UNIT * 15 / 100;
++
++/* How much do we increase cwnd_gain when probing for bandwidth in
++ * BBR_BW_PROBE_UP? This specifies the increment in units of
++ * BBR_UNIT/4. The default is 1, meaning 0.25.
++ * The min value is 0 (meaning 0.0); max is 3 (meaning 0.75).
++ */
++static const u32 bbr_bw_probe_cwnd_gain = 1;
++
++/* Max number of packet-timed rounds to wait before probing for bandwidth. If
++ * we want to tolerate 1% random loss per round, and not have this cut our
++ * inflight too much, we must probe for bw periodically on roughly this scale.
++ * If low, limits Reno/CUBIC coexistence; if high, limits loss tolerance.
++ * We aim to be fair with Reno/CUBIC up to a BDP of at least:
++ * BDP = 25Mbps * .030sec /(1514bytes) = 61.9 packets
++ */
++static const u32 bbr_bw_probe_max_rounds = 63;
++
++/* Max amount of randomness to inject in round counting for Reno-coexistence.
++ */
++static const u32 bbr_bw_probe_rand_rounds = 2;
++
++/* Use BBR-native probe time scale starting at this many usec.
++ * We aim to be fair with Reno/CUBIC up to an inter-loss time epoch of at least:
++ * BDP*RTT = 25Mbps * .030sec /(1514bytes) * 0.030sec = 1.9 secs
++ */
++static const u32 bbr_bw_probe_base_us = 2 * USEC_PER_SEC; /* 2 secs */
++
++/* Use BBR-native probes spread over this many usec: */
++static const u32 bbr_bw_probe_rand_us = 1 * USEC_PER_SEC; /* 1 secs */
++
++/* Use fast path if app-limited, no loss/ECN, and target cwnd was reached? */
++static const bool bbr_fast_path = true;
++
++/* Use fast ack mode? */
++static const bool bbr_fast_ack_mode = true;
++
++static u32 bbr_max_bw(const struct sock *sk);
++static u32 bbr_bw(const struct sock *sk);
++static void bbr_exit_probe_rtt(struct sock *sk);
++static void bbr_reset_congestion_signals(struct sock *sk);
++static void bbr_run_loss_probe_recovery(struct sock *sk);
++
+ static void bbr_check_probe_rtt_done(struct sock *sk);
+
++/* This connection can use ECN if both endpoints have signaled ECN support in
++ * the handshake and the per-route settings indicated this is a
++ * shallow-threshold ECN environment, meaning both:
++ * (a) ECN CE marks indicate low-latency/shallow-threshold congestion, and
++ * (b) TCP endpoints provide precise ACKs that only ACK data segments
++ * with consistent ECN CE status
++ */
++static bool bbr_can_use_ecn(const struct sock *sk)
++{
++ return (tcp_sk(sk)->ecn_flags & TCP_ECN_OK) &&
++ (tcp_sk(sk)->ecn_flags & TCP_ECN_LOW);
++}
++
+ /* Do we estimate that STARTUP filled the pipe? */
+ static bool bbr_full_bw_reached(const struct sock *sk)
+ {
+@@ -214,17 +382,17 @@ static bool bbr_full_bw_reached(const struct sock *sk)
+ /* Return the windowed max recent bandwidth sample, in pkts/uS << BW_SCALE. */
+ static u32 bbr_max_bw(const struct sock *sk)
+ {
+- struct bbr *bbr = inet_csk_ca(sk);
++ const struct bbr *bbr = inet_csk_ca(sk);
+
+- return minmax_get(&bbr->bw);
++ return max(bbr->bw_hi[0], bbr->bw_hi[1]);
+ }
+
+ /* Return the estimated bandwidth of the path, in pkts/uS << BW_SCALE. */
+ static u32 bbr_bw(const struct sock *sk)
+ {
+- struct bbr *bbr = inet_csk_ca(sk);
++ const struct bbr *bbr = inet_csk_ca(sk);
+
+- return bbr->lt_use_bw ? bbr->lt_bw : bbr_max_bw(sk);
++ return min(bbr_max_bw(sk), bbr->bw_lo);
+ }
+
+ /* Return maximum extra acked in past k-2k round trips,
+@@ -241,15 +409,23 @@ static u16 bbr_extra_acked(const struct sock *sk)
+ * The order here is chosen carefully to avoid overflow of u64. This should
+ * work for input rates of up to 2.9Tbit/sec and gain of 2.89x.
+ */
+-static u64 bbr_rate_bytes_per_sec(struct sock *sk, u64 rate, int gain)
++static u64 bbr_rate_bytes_per_sec(struct sock *sk, u64 rate, int gain,
++ int margin)
+ {
+ unsigned int mss = tcp_sk(sk)->mss_cache;
+
+ rate *= mss;
+ rate *= gain;
+ rate >>= BBR_SCALE;
+- rate *= USEC_PER_SEC / 100 * (100 - bbr_pacing_margin_percent);
+- return rate >> BW_SCALE;
++ rate *= USEC_PER_SEC / 100 * (100 - margin);
++ rate >>= BW_SCALE;
++ rate = max(rate, 1ULL);
++ return rate;
++}
++
++static u64 bbr_bw_bytes_per_sec(struct sock *sk, u64 rate)
++{
++ return bbr_rate_bytes_per_sec(sk, rate, BBR_UNIT, 0);
+ }
+
+ /* Convert a BBR bw and gain factor to a pacing rate in bytes per second. */
+@@ -257,12 +433,13 @@ static unsigned long bbr_bw_to_pacing_rate(struct sock *sk, u32 bw, int gain)
+ {
+ u64 rate = bw;
+
+- rate = bbr_rate_bytes_per_sec(sk, rate, gain);
++ rate = bbr_rate_bytes_per_sec(sk, rate, gain,
++ bbr_pacing_margin_percent);
+ rate = min_t(u64, rate, READ_ONCE(sk->sk_max_pacing_rate));
+ return rate;
+ }
+
+-/* Initialize pacing rate to: high_gain * init_cwnd / RTT. */
++/* Initialize pacing rate to: startup_pacing_gain * init_cwnd / RTT. */
+ static void bbr_init_pacing_rate_from_rtt(struct sock *sk)
+ {
+ struct tcp_sock *tp = tcp_sk(sk);
+@@ -279,7 +456,7 @@ static void bbr_init_pacing_rate_from_rtt(struct sock *sk)
+ bw = (u64)tcp_snd_cwnd(tp) * BW_UNIT;
+ do_div(bw, rtt_us);
+ WRITE_ONCE(sk->sk_pacing_rate,
+- bbr_bw_to_pacing_rate(sk, bw, bbr_high_gain));
++ bbr_bw_to_pacing_rate(sk, bw, bbr_param(sk, startup_pacing_gain)));
+ }
+
+ /* Pace using current bw estimate and a gain factor. */
+@@ -295,26 +472,48 @@ static void bbr_set_pacing_rate(struct sock *sk, u32 bw, int gain)
+ WRITE_ONCE(sk->sk_pacing_rate, rate);
+ }
+
+-/* override sysctl_tcp_min_tso_segs */
+-__bpf_kfunc static u32 bbr_min_tso_segs(struct sock *sk)
++/* Return the number of segments BBR would like in a TSO/GSO skb, given a
++ * particular max gso size as a constraint. TODO: make this simpler and more
++ * consistent by switching bbr to just call tcp_tso_autosize().
++ */
++static u32 bbr_tso_segs_generic(struct sock *sk, unsigned int mss_now,
++ u32 gso_max_size)
++{
++ struct bbr *bbr = inet_csk_ca(sk);
++ u32 segs, r;
++ u64 bytes;
++
++ /* Budget a TSO/GSO burst size allowance based on bw (pacing_rate). */
++ bytes = READ_ONCE(sk->sk_pacing_rate) >> READ_ONCE(sk->sk_pacing_shift);
++
++ /* Budget a TSO/GSO burst size allowance based on min_rtt. For every
++ * K = 2^tso_rtt_shift microseconds of min_rtt, halve the burst.
++ * The min_rtt-based burst allowance is: 64 KBytes / 2^(min_rtt/K)
++ */
++ if (bbr_param(sk, tso_rtt_shift)) {
++ r = bbr->min_rtt_us >> bbr_param(sk, tso_rtt_shift);
++ if (r < BITS_PER_TYPE(u32)) /* prevent undefined behavior */
++ bytes += GSO_LEGACY_MAX_SIZE >> r;
++ }
++
++ bytes = min_t(u32, bytes, gso_max_size - 1 - MAX_TCP_HEADER);
++ segs = max_t(u32, bytes / mss_now,
++ sock_net(sk)->ipv4.sysctl_tcp_min_tso_segs);
++ return segs;
++}
++
++/* Custom tcp_tso_autosize() for BBR, used at transmit time to cap skb size. */
++__bpf_kfunc static u32 bbr_tso_segs(struct sock *sk, unsigned int mss_now)
+ {
+- return READ_ONCE(sk->sk_pacing_rate) < (bbr_min_tso_rate >> 3) ? 1 : 2;
++ return bbr_tso_segs_generic(sk, mss_now, sk->sk_gso_max_size);
+ }
+
++/* Like bbr_tso_segs(), using mss_cache, ignoring driver's sk_gso_max_size. */
+ static u32 bbr_tso_segs_goal(struct sock *sk)
+ {
+ struct tcp_sock *tp = tcp_sk(sk);
+- u32 segs, bytes;
+-
+- /* Sort of tcp_tso_autosize() but ignoring
+- * driver provided sk_gso_max_size.
+- */
+- bytes = min_t(unsigned long,
+- READ_ONCE(sk->sk_pacing_rate) >> READ_ONCE(sk->sk_pacing_shift),
+- GSO_LEGACY_MAX_SIZE - 1 - MAX_TCP_HEADER);
+- segs = max_t(u32, bytes / tp->mss_cache, bbr_min_tso_segs(sk));
+
+- return min(segs, 0x7FU);
++ return bbr_tso_segs_generic(sk, tp->mss_cache, GSO_LEGACY_MAX_SIZE);
+ }
+
+ /* Save "last known good" cwnd so we can restore it after losses or PROBE_RTT */
+@@ -334,7 +533,9 @@ __bpf_kfunc static void bbr_cwnd_event(struct sock *sk, enum tcp_ca_event event)
+ struct tcp_sock *tp = tcp_sk(sk);
+ struct bbr *bbr = inet_csk_ca(sk);
+
+- if (event == CA_EVENT_TX_START && tp->app_limited) {
++ if (event == CA_EVENT_TX_START) {
++ if (!tp->app_limited)
++ return;
+ bbr->idle_restart = 1;
+ bbr->ack_epoch_mstamp = tp->tcp_mstamp;
+ bbr->ack_epoch_acked = 0;
+@@ -345,6 +546,16 @@ __bpf_kfunc static void bbr_cwnd_event(struct sock *sk, enum tcp_ca_event event)
+ bbr_set_pacing_rate(sk, bbr_bw(sk), BBR_UNIT);
+ else if (bbr->mode == BBR_PROBE_RTT)
+ bbr_check_probe_rtt_done(sk);
++ } else if ((event == CA_EVENT_ECN_IS_CE ||
++ event == CA_EVENT_ECN_NO_CE) &&
++ bbr_can_use_ecn(sk) &&
++ bbr_param(sk, precise_ece_ack)) {
++ u32 state = bbr->ce_state;
++ dctcp_ece_ack_update(sk, event, &bbr->prior_rcv_nxt, &state);
++ bbr->ce_state = state;
++ } else if (event == CA_EVENT_TLP_RECOVERY &&
++ bbr_param(sk, loss_probe_recovery)) {
++ bbr_run_loss_probe_recovery(sk);
+ }
+ }
+
+@@ -367,10 +578,10 @@ static u32 bbr_bdp(struct sock *sk, u32 bw, int gain)
+ * default. This should only happen when the connection is not using TCP
+ * timestamps and has retransmitted all of the SYN/SYNACK/data packets
+ * ACKed so far. In this case, an RTO can cut cwnd to 1, in which
+- * case we need to slow-start up toward something safe: TCP_INIT_CWND.
++ * case we need to slow-start up toward something safe: initial cwnd.
+ */
+ if (unlikely(bbr->min_rtt_us == ~0U)) /* no valid RTT samples yet? */
+- return TCP_INIT_CWND; /* be safe: cap at default initial cwnd*/
++ return bbr->init_cwnd; /* be safe: cap at initial cwnd */
+
+ w = (u64)bw * bbr->min_rtt_us;
+
+@@ -387,23 +598,23 @@ static u32 bbr_bdp(struct sock *sk, u32 bw, int gain)
+ * - one skb in sending host Qdisc,
+ * - one skb in sending host TSO/GSO engine
+ * - one skb being received by receiver host LRO/GRO/delayed-ACK engine
+- * Don't worry, at low rates (bbr_min_tso_rate) this won't bloat cwnd because
+- * in such cases tso_segs_goal is 1. The minimum cwnd is 4 packets,
++ * Don't worry, at low rates this won't bloat cwnd because
++ * in such cases tso_segs_goal is small. The minimum cwnd is 4 packets,
+ * which allows 2 outstanding 2-packet sequences, to try to keep pipe
+ * full even with ACK-every-other-packet delayed ACKs.
+ */
+ static u32 bbr_quantization_budget(struct sock *sk, u32 cwnd)
+ {
+ struct bbr *bbr = inet_csk_ca(sk);
++ u32 tso_segs_goal;
+
+- /* Allow enough full-sized skbs in flight to utilize end systems. */
+- cwnd += 3 * bbr_tso_segs_goal(sk);
+-
+- /* Reduce delayed ACKs by rounding up cwnd to the next even number. */
+- cwnd = (cwnd + 1) & ~1U;
++ tso_segs_goal = 3 * bbr_tso_segs_goal(sk);
+
++ /* Allow enough full-sized skbs in flight to utilize end systems. */
++ cwnd = max_t(u32, cwnd, tso_segs_goal);
++ cwnd = max_t(u32, cwnd, bbr_param(sk, cwnd_min_target));
+ /* Ensure gain cycling gets inflight above BDP even for small BDPs. */
+- if (bbr->mode == BBR_PROBE_BW && bbr->cycle_idx == 0)
++ if (bbr->mode == BBR_PROBE_BW && bbr->cycle_idx == BBR_BW_PROBE_UP)
+ cwnd += 2;
+
+ return cwnd;
+@@ -458,10 +669,10 @@ static u32 bbr_ack_aggregation_cwnd(struct sock *sk)
+ {
+ u32 max_aggr_cwnd, aggr_cwnd = 0;
+
+- if (bbr_extra_acked_gain && bbr_full_bw_reached(sk)) {
++ if (bbr_param(sk, extra_acked_gain)) {
+ max_aggr_cwnd = ((u64)bbr_bw(sk) * bbr_extra_acked_max_us)
+ / BW_UNIT;
+- aggr_cwnd = (bbr_extra_acked_gain * bbr_extra_acked(sk))
++ aggr_cwnd = (bbr_param(sk, extra_acked_gain) * bbr_extra_acked(sk))
+ >> BBR_SCALE;
+ aggr_cwnd = min(aggr_cwnd, max_aggr_cwnd);
+ }
+@@ -469,66 +680,27 @@ static u32 bbr_ack_aggregation_cwnd(struct sock *sk)
+ return aggr_cwnd;
+ }
+
+-/* An optimization in BBR to reduce losses: On the first round of recovery, we
+- * follow the packet conservation principle: send P packets per P packets acked.
+- * After that, we slow-start and send at most 2*P packets per P packets acked.
+- * After recovery finishes, or upon undo, we restore the cwnd we had when
+- * recovery started (capped by the target cwnd based on estimated BDP).
+- *
+- * TODO(ycheng/ncardwell): implement a rate-based approach.
+- */
+-static bool bbr_set_cwnd_to_recover_or_restore(
+- struct sock *sk, const struct rate_sample *rs, u32 acked, u32 *new_cwnd)
++/* Returns the cwnd for PROBE_RTT mode. */
++static u32 bbr_probe_rtt_cwnd(struct sock *sk)
+ {
+- struct tcp_sock *tp = tcp_sk(sk);
+- struct bbr *bbr = inet_csk_ca(sk);
+- u8 prev_state = bbr->prev_ca_state, state = inet_csk(sk)->icsk_ca_state;
+- u32 cwnd = tcp_snd_cwnd(tp);
+-
+- /* An ACK for P pkts should release at most 2*P packets. We do this
+- * in two steps. First, here we deduct the number of lost packets.
+- * Then, in bbr_set_cwnd() we slow start up toward the target cwnd.
+- */
+- if (rs->losses > 0)
+- cwnd = max_t(s32, cwnd - rs->losses, 1);
+-
+- if (state == TCP_CA_Recovery && prev_state != TCP_CA_Recovery) {
+- /* Starting 1st round of Recovery, so do packet conservation. */
+- bbr->packet_conservation = 1;
+- bbr->next_rtt_delivered = tp->delivered; /* start round now */
+- /* Cut unused cwnd from app behavior, TSQ, or TSO deferral: */
+- cwnd = tcp_packets_in_flight(tp) + acked;
+- } else if (prev_state >= TCP_CA_Recovery && state < TCP_CA_Recovery) {
+- /* Exiting loss recovery; restore cwnd saved before recovery. */
+- cwnd = max(cwnd, bbr->prior_cwnd);
+- bbr->packet_conservation = 0;
+- }
+- bbr->prev_ca_state = state;
+-
+- if (bbr->packet_conservation) {
+- *new_cwnd = max(cwnd, tcp_packets_in_flight(tp) + acked);
+- return true; /* yes, using packet conservation */
+- }
+- *new_cwnd = cwnd;
+- return false;
++ return max_t(u32, bbr_param(sk, cwnd_min_target),
++ bbr_bdp(sk, bbr_bw(sk), bbr_param(sk, probe_rtt_cwnd_gain)));
+ }
+
+ /* Slow-start up toward target cwnd (if bw estimate is growing, or packet loss
+ * has drawn us down below target), or snap down to target if we're above it.
+ */
+ static void bbr_set_cwnd(struct sock *sk, const struct rate_sample *rs,
+- u32 acked, u32 bw, int gain)
++ u32 acked, u32 bw, int gain, u32 cwnd,
++ struct bbr_context *ctx)
+ {
+ struct tcp_sock *tp = tcp_sk(sk);
+ struct bbr *bbr = inet_csk_ca(sk);
+- u32 cwnd = tcp_snd_cwnd(tp), target_cwnd = 0;
++ u32 target_cwnd = 0;
+
+ if (!acked)
+ goto done; /* no packet fully ACKed; just apply caps */
+
+- if (bbr_set_cwnd_to_recover_or_restore(sk, rs, acked, &cwnd))
+- goto done;
+-
+ target_cwnd = bbr_bdp(sk, bw, gain);
+
+ /* Increment the cwnd to account for excess ACKed data that seems
+@@ -537,74 +709,26 @@ static void bbr_set_cwnd(struct sock *sk, const struct rate_sample *rs,
+ target_cwnd += bbr_ack_aggregation_cwnd(sk);
+ target_cwnd = bbr_quantization_budget(sk, target_cwnd);
+
+- /* If we're below target cwnd, slow start cwnd toward target cwnd. */
+- if (bbr_full_bw_reached(sk)) /* only cut cwnd if we filled the pipe */
+- cwnd = min(cwnd + acked, target_cwnd);
+- else if (cwnd < target_cwnd || tp->delivered < TCP_INIT_CWND)
+- cwnd = cwnd + acked;
+- cwnd = max(cwnd, bbr_cwnd_min_target);
++ /* Update cwnd and enable fast path if cwnd reaches target_cwnd. */
++ bbr->try_fast_path = 0;
++ if (bbr_full_bw_reached(sk)) { /* only cut cwnd if we filled the pipe */
++ cwnd += acked;
++ if (cwnd >= target_cwnd) {
++ cwnd = target_cwnd;
++ bbr->try_fast_path = 1;
++ }
++ } else if (cwnd < target_cwnd || cwnd < 2 * bbr->init_cwnd) {
++ cwnd += acked;
++ } else {
++ bbr->try_fast_path = 1;
++ }
+
++ cwnd = max_t(u32, cwnd, bbr_param(sk, cwnd_min_target));
+ done:
+- tcp_snd_cwnd_set(tp, min(cwnd, tp->snd_cwnd_clamp)); /* apply global cap */
++ tcp_snd_cwnd_set(tp, min(cwnd, tp->snd_cwnd_clamp)); /* global cap */
+ if (bbr->mode == BBR_PROBE_RTT) /* drain queue, refresh min_rtt */
+- tcp_snd_cwnd_set(tp, min(tcp_snd_cwnd(tp), bbr_cwnd_min_target));
+-}
+-
+-/* End cycle phase if it's time and/or we hit the phase's in-flight target. */
+-static bool bbr_is_next_cycle_phase(struct sock *sk,
+- const struct rate_sample *rs)
+-{
+- struct tcp_sock *tp = tcp_sk(sk);
+- struct bbr *bbr = inet_csk_ca(sk);
+- bool is_full_length =
+- tcp_stamp_us_delta(tp->delivered_mstamp, bbr->cycle_mstamp) >
+- bbr->min_rtt_us;
+- u32 inflight, bw;
+-
+- /* The pacing_gain of 1.0 paces at the estimated bw to try to fully
+- * use the pipe without increasing the queue.
+- */
+- if (bbr->pacing_gain == BBR_UNIT)
+- return is_full_length; /* just use wall clock time */
+-
+- inflight = bbr_packets_in_net_at_edt(sk, rs->prior_in_flight);
+- bw = bbr_max_bw(sk);
+-
+- /* A pacing_gain > 1.0 probes for bw by trying to raise inflight to at
+- * least pacing_gain*BDP; this may take more than min_rtt if min_rtt is
+- * small (e.g. on a LAN). We do not persist if packets are lost, since
+- * a path with small buffers may not hold that much.
+- */
+- if (bbr->pacing_gain > BBR_UNIT)
+- return is_full_length &&
+- (rs->losses || /* perhaps pacing_gain*BDP won't fit */
+- inflight >= bbr_inflight(sk, bw, bbr->pacing_gain));
+-
+- /* A pacing_gain < 1.0 tries to drain extra queue we added if bw
+- * probing didn't find more bw. If inflight falls to match BDP then we
+- * estimate queue is drained; persisting would underutilize the pipe.
+- */
+- return is_full_length ||
+- inflight <= bbr_inflight(sk, bw, BBR_UNIT);
+-}
+-
+-static void bbr_advance_cycle_phase(struct sock *sk)
+-{
+- struct tcp_sock *tp = tcp_sk(sk);
+- struct bbr *bbr = inet_csk_ca(sk);
+-
+- bbr->cycle_idx = (bbr->cycle_idx + 1) & (CYCLE_LEN - 1);
+- bbr->cycle_mstamp = tp->delivered_mstamp;
+-}
+-
+-/* Gain cycling: cycle pacing gain to converge to fair share of available bw. */
+-static void bbr_update_cycle_phase(struct sock *sk,
+- const struct rate_sample *rs)
+-{
+- struct bbr *bbr = inet_csk_ca(sk);
+-
+- if (bbr->mode == BBR_PROBE_BW && bbr_is_next_cycle_phase(sk, rs))
+- bbr_advance_cycle_phase(sk);
++ tcp_snd_cwnd_set(tp, min_t(u32, tcp_snd_cwnd(tp),
++ bbr_probe_rtt_cwnd(sk)));
+ }
+
+ static void bbr_reset_startup_mode(struct sock *sk)
+@@ -614,191 +738,49 @@ static void bbr_reset_startup_mode(struct sock *sk)
+ bbr->mode = BBR_STARTUP;
+ }
+
+-static void bbr_reset_probe_bw_mode(struct sock *sk)
+-{
+- struct bbr *bbr = inet_csk_ca(sk);
+-
+- bbr->mode = BBR_PROBE_BW;
+- bbr->cycle_idx = CYCLE_LEN - 1 - get_random_u32_below(bbr_cycle_rand);
+- bbr_advance_cycle_phase(sk); /* flip to next phase of gain cycle */
+-}
+-
+-static void bbr_reset_mode(struct sock *sk)
+-{
+- if (!bbr_full_bw_reached(sk))
+- bbr_reset_startup_mode(sk);
+- else
+- bbr_reset_probe_bw_mode(sk);
+-}
+-
+-/* Start a new long-term sampling interval. */
+-static void bbr_reset_lt_bw_sampling_interval(struct sock *sk)
+-{
+- struct tcp_sock *tp = tcp_sk(sk);
+- struct bbr *bbr = inet_csk_ca(sk);
+-
+- bbr->lt_last_stamp = div_u64(tp->delivered_mstamp, USEC_PER_MSEC);
+- bbr->lt_last_delivered = tp->delivered;
+- bbr->lt_last_lost = tp->lost;
+- bbr->lt_rtt_cnt = 0;
+-}
+-
+-/* Completely reset long-term bandwidth sampling. */
+-static void bbr_reset_lt_bw_sampling(struct sock *sk)
+-{
+- struct bbr *bbr = inet_csk_ca(sk);
+-
+- bbr->lt_bw = 0;
+- bbr->lt_use_bw = 0;
+- bbr->lt_is_sampling = false;
+- bbr_reset_lt_bw_sampling_interval(sk);
+-}
+-
+-/* Long-term bw sampling interval is done. Estimate whether we're policed. */
+-static void bbr_lt_bw_interval_done(struct sock *sk, u32 bw)
+-{
+- struct bbr *bbr = inet_csk_ca(sk);
+- u32 diff;
+-
+- if (bbr->lt_bw) { /* do we have bw from a previous interval? */
+- /* Is new bw close to the lt_bw from the previous interval? */
+- diff = abs(bw - bbr->lt_bw);
+- if ((diff * BBR_UNIT <= bbr_lt_bw_ratio * bbr->lt_bw) ||
+- (bbr_rate_bytes_per_sec(sk, diff, BBR_UNIT) <=
+- bbr_lt_bw_diff)) {
+- /* All criteria are met; estimate we're policed. */
+- bbr->lt_bw = (bw + bbr->lt_bw) >> 1; /* avg 2 intvls */
+- bbr->lt_use_bw = 1;
+- bbr->pacing_gain = BBR_UNIT; /* try to avoid drops */
+- bbr->lt_rtt_cnt = 0;
+- return;
+- }
+- }
+- bbr->lt_bw = bw;
+- bbr_reset_lt_bw_sampling_interval(sk);
+-}
+-
+-/* Token-bucket traffic policers are common (see "An Internet-Wide Analysis of
+- * Traffic Policing", SIGCOMM 2016). BBR detects token-bucket policers and
+- * explicitly models their policed rate, to reduce unnecessary losses. We
+- * estimate that we're policed if we see 2 consecutive sampling intervals with
+- * consistent throughput and high packet loss. If we think we're being policed,
+- * set lt_bw to the "long-term" average delivery rate from those 2 intervals.
++/* See if we have reached next round trip. Upon start of the new round,
++ * returns packets delivered since previous round start plus this ACK.
+ */
+-static void bbr_lt_bw_sampling(struct sock *sk, const struct rate_sample *rs)
+-{
+- struct tcp_sock *tp = tcp_sk(sk);
+- struct bbr *bbr = inet_csk_ca(sk);
+- u32 lost, delivered;
+- u64 bw;
+- u32 t;
+-
+- if (bbr->lt_use_bw) { /* already using long-term rate, lt_bw? */
+- if (bbr->mode == BBR_PROBE_BW && bbr->round_start &&
+- ++bbr->lt_rtt_cnt >= bbr_lt_bw_max_rtts) {
+- bbr_reset_lt_bw_sampling(sk); /* stop using lt_bw */
+- bbr_reset_probe_bw_mode(sk); /* restart gain cycling */
+- }
+- return;
+- }
+-
+- /* Wait for the first loss before sampling, to let the policer exhaust
+- * its tokens and estimate the steady-state rate allowed by the policer.
+- * Starting samples earlier includes bursts that over-estimate the bw.
+- */
+- if (!bbr->lt_is_sampling) {
+- if (!rs->losses)
+- return;
+- bbr_reset_lt_bw_sampling_interval(sk);
+- bbr->lt_is_sampling = true;
+- }
+-
+- /* To avoid underestimates, reset sampling if we run out of data. */
+- if (rs->is_app_limited) {
+- bbr_reset_lt_bw_sampling(sk);
+- return;
+- }
+-
+- if (bbr->round_start)
+- bbr->lt_rtt_cnt++; /* count round trips in this interval */
+- if (bbr->lt_rtt_cnt < bbr_lt_intvl_min_rtts)
+- return; /* sampling interval needs to be longer */
+- if (bbr->lt_rtt_cnt > 4 * bbr_lt_intvl_min_rtts) {
+- bbr_reset_lt_bw_sampling(sk); /* interval is too long */
+- return;
+- }
+-
+- /* End sampling interval when a packet is lost, so we estimate the
+- * policer tokens were exhausted. Stopping the sampling before the
+- * tokens are exhausted under-estimates the policed rate.
+- */
+- if (!rs->losses)
+- return;
+-
+- /* Calculate packets lost and delivered in sampling interval. */
+- lost = tp->lost - bbr->lt_last_lost;
+- delivered = tp->delivered - bbr->lt_last_delivered;
+- /* Is loss rate (lost/delivered) >= lt_loss_thresh? If not, wait. */
+- if (!delivered || (lost << BBR_SCALE) < bbr_lt_loss_thresh * delivered)
+- return;
+-
+- /* Find average delivery rate in this sampling interval. */
+- t = div_u64(tp->delivered_mstamp, USEC_PER_MSEC) - bbr->lt_last_stamp;
+- if ((s32)t < 1)
+- return; /* interval is less than one ms, so wait */
+- /* Check if can multiply without overflow */
+- if (t >= ~0U / USEC_PER_MSEC) {
+- bbr_reset_lt_bw_sampling(sk); /* interval too long; reset */
+- return;
+- }
+- t *= USEC_PER_MSEC;
+- bw = (u64)delivered * BW_UNIT;
+- do_div(bw, t);
+- bbr_lt_bw_interval_done(sk, bw);
+-}
+-
+-/* Estimate the bandwidth based on how fast packets are delivered */
+-static void bbr_update_bw(struct sock *sk, const struct rate_sample *rs)
++static u32 bbr_update_round_start(struct sock *sk,
++ const struct rate_sample *rs, struct bbr_context *ctx)
+ {
+ struct tcp_sock *tp = tcp_sk(sk);
+ struct bbr *bbr = inet_csk_ca(sk);
+- u64 bw;
++ u32 round_delivered = 0;
+
+ bbr->round_start = 0;
+- if (rs->delivered < 0 || rs->interval_us <= 0)
+- return; /* Not a valid observation */
+
+ /* See if we've reached the next RTT */
+- if (!before(rs->prior_delivered, bbr->next_rtt_delivered)) {
++ if (rs->interval_us > 0 &&
++ !before(rs->prior_delivered, bbr->next_rtt_delivered)) {
++ round_delivered = tp->delivered - bbr->next_rtt_delivered;
+ bbr->next_rtt_delivered = tp->delivered;
+- bbr->rtt_cnt++;
+ bbr->round_start = 1;
+- bbr->packet_conservation = 0;
+ }
++ return round_delivered;
++}
+
+- bbr_lt_bw_sampling(sk, rs);
++/* Calculate the bandwidth based on how fast packets are delivered */
++static void bbr_calculate_bw_sample(struct sock *sk,
++ const struct rate_sample *rs, struct bbr_context *ctx)
++{
++ u64 bw = 0;
+
+ /* Divide delivered by the interval to find a (lower bound) bottleneck
+ * bandwidth sample. Delivered is in packets and interval_us in uS and
+ * ratio will be <<1 for most connections. So delivered is first scaled.
++ * Round up to allow growth at low rates, even with integer division.
+ */
+- bw = div64_long((u64)rs->delivered * BW_UNIT, rs->interval_us);
+-
+- /* If this sample is application-limited, it is likely to have a very
+- * low delivered count that represents application behavior rather than
+- * the available network rate. Such a sample could drag down estimated
+- * bw, causing needless slow-down. Thus, to continue to send at the
+- * last measured network rate, we filter out app-limited samples unless
+- * they describe the path bw at least as well as our bw model.
+- *
+- * So the goal during app-limited phase is to proceed with the best
+- * network rate no matter how long. We automatically leave this
+- * phase when app writes faster than the network can deliver :)
+- */
+- if (!rs->is_app_limited || bw >= bbr_max_bw(sk)) {
+- /* Incorporate new sample into our max bw filter. */
+- minmax_running_max(&bbr->bw, bbr_bw_rtts, bbr->rtt_cnt, bw);
++ if (rs->interval_us > 0) {
++ if (WARN_ONCE(rs->delivered < 0,
++ "negative delivered: %d interval_us: %ld\n",
++ rs->delivered, rs->interval_us))
++ return;
++
++ bw = DIV_ROUND_UP_ULL((u64)rs->delivered * BW_UNIT, rs->interval_us);
+ }
++
++ ctx->sample_bw = bw;
+ }
+
+ /* Estimates the windowed max degree of ack aggregation.
+@@ -812,7 +794,7 @@ static void bbr_update_bw(struct sock *sk, const struct rate_sample *rs)
+ *
+ * Max extra_acked is clamped by cwnd and bw * bbr_extra_acked_max_us (100 ms).
+ * Max filter is an approximate sliding window of 5-10 (packet timed) round
+- * trips.
++ * trips for non-startup phase, and 1-2 round trips for startup.
+ */
+ static void bbr_update_ack_aggregation(struct sock *sk,
+ const struct rate_sample *rs)
+@@ -820,15 +802,19 @@ static void bbr_update_ack_aggregation(struct sock *sk,
+ u32 epoch_us, expected_acked, extra_acked;
+ struct bbr *bbr = inet_csk_ca(sk);
+ struct tcp_sock *tp = tcp_sk(sk);
++ u32 extra_acked_win_rtts_thresh = bbr_param(sk, extra_acked_win_rtts);
+
+- if (!bbr_extra_acked_gain || rs->acked_sacked <= 0 ||
++ if (!bbr_param(sk, extra_acked_gain) || rs->acked_sacked <= 0 ||
+ rs->delivered < 0 || rs->interval_us <= 0)
+ return;
+
+ if (bbr->round_start) {
+ bbr->extra_acked_win_rtts = min(0x1F,
+ bbr->extra_acked_win_rtts + 1);
+- if (bbr->extra_acked_win_rtts >= bbr_extra_acked_win_rtts) {
++ if (!bbr_full_bw_reached(sk))
++ extra_acked_win_rtts_thresh = 1;
++ if (bbr->extra_acked_win_rtts >=
++ extra_acked_win_rtts_thresh) {
+ bbr->extra_acked_win_rtts = 0;
+ bbr->extra_acked_win_idx = bbr->extra_acked_win_idx ?
+ 0 : 1;
+@@ -862,49 +848,6 @@ static void bbr_update_ack_aggregation(struct sock *sk,
+ bbr->extra_acked[bbr->extra_acked_win_idx] = extra_acked;
+ }
+
+-/* Estimate when the pipe is full, using the change in delivery rate: BBR
+- * estimates that STARTUP filled the pipe if the estimated bw hasn't changed by
+- * at least bbr_full_bw_thresh (25%) after bbr_full_bw_cnt (3) non-app-limited
+- * rounds. Why 3 rounds: 1: rwin autotuning grows the rwin, 2: we fill the
+- * higher rwin, 3: we get higher delivery rate samples. Or transient
+- * cross-traffic or radio noise can go away. CUBIC Hystart shares a similar
+- * design goal, but uses delay and inter-ACK spacing instead of bandwidth.
+- */
+-static void bbr_check_full_bw_reached(struct sock *sk,
+- const struct rate_sample *rs)
+-{
+- struct bbr *bbr = inet_csk_ca(sk);
+- u32 bw_thresh;
+-
+- if (bbr_full_bw_reached(sk) || !bbr->round_start || rs->is_app_limited)
+- return;
+-
+- bw_thresh = (u64)bbr->full_bw * bbr_full_bw_thresh >> BBR_SCALE;
+- if (bbr_max_bw(sk) >= bw_thresh) {
+- bbr->full_bw = bbr_max_bw(sk);
+- bbr->full_bw_cnt = 0;
+- return;
+- }
+- ++bbr->full_bw_cnt;
+- bbr->full_bw_reached = bbr->full_bw_cnt >= bbr_full_bw_cnt;
+-}
+-
+-/* If pipe is probably full, drain the queue and then enter steady-state. */
+-static void bbr_check_drain(struct sock *sk, const struct rate_sample *rs)
+-{
+- struct bbr *bbr = inet_csk_ca(sk);
+-
+- if (bbr->mode == BBR_STARTUP && bbr_full_bw_reached(sk)) {
+- bbr->mode = BBR_DRAIN; /* drain queue we created */
+- tcp_sk(sk)->snd_ssthresh =
+- bbr_inflight(sk, bbr_max_bw(sk), BBR_UNIT);
+- } /* fall through to check if in-flight is already small: */
+- if (bbr->mode == BBR_DRAIN &&
+- bbr_packets_in_net_at_edt(sk, tcp_packets_in_flight(tcp_sk(sk))) <=
+- bbr_inflight(sk, bbr_max_bw(sk), BBR_UNIT))
+- bbr_reset_probe_bw_mode(sk); /* we estimate queue is drained */
+-}
+-
+ static void bbr_check_probe_rtt_done(struct sock *sk)
+ {
+ struct tcp_sock *tp = tcp_sk(sk);
+@@ -914,9 +857,9 @@ static void bbr_check_probe_rtt_done(struct sock *sk)
+ after(tcp_jiffies32, bbr->probe_rtt_done_stamp)))
+ return;
+
+- bbr->min_rtt_stamp = tcp_jiffies32; /* wait a while until PROBE_RTT */
++ bbr->probe_rtt_min_stamp = tcp_jiffies32; /* schedule next PROBE_RTT */
+ tcp_snd_cwnd_set(tp, max(tcp_snd_cwnd(tp), bbr->prior_cwnd));
+- bbr_reset_mode(sk);
++ bbr_exit_probe_rtt(sk);
+ }
+
+ /* The goal of PROBE_RTT mode is to have BBR flows cooperatively and
+@@ -942,23 +885,35 @@ static void bbr_update_min_rtt(struct sock *sk, const struct rate_sample *rs)
+ {
+ struct tcp_sock *tp = tcp_sk(sk);
+ struct bbr *bbr = inet_csk_ca(sk);
+- bool filter_expired;
++ bool probe_rtt_expired, min_rtt_expired;
++ u32 expire;
+
+- /* Track min RTT seen in the min_rtt_win_sec filter window: */
+- filter_expired = after(tcp_jiffies32,
+- bbr->min_rtt_stamp + bbr_min_rtt_win_sec * HZ);
++ /* Track min RTT in probe_rtt_win_ms to time next PROBE_RTT state. */
++ expire = bbr->probe_rtt_min_stamp +
++ msecs_to_jiffies(bbr_param(sk, probe_rtt_win_ms));
++ probe_rtt_expired = after(tcp_jiffies32, expire);
+ if (rs->rtt_us >= 0 &&
+- (rs->rtt_us < bbr->min_rtt_us ||
+- (filter_expired && !rs->is_ack_delayed))) {
+- bbr->min_rtt_us = rs->rtt_us;
+- bbr->min_rtt_stamp = tcp_jiffies32;
++ (rs->rtt_us < bbr->probe_rtt_min_us ||
++ (probe_rtt_expired && !rs->is_ack_delayed))) {
++ bbr->probe_rtt_min_us = rs->rtt_us;
++ bbr->probe_rtt_min_stamp = tcp_jiffies32;
++ }
++ /* Track min RTT seen in the min_rtt_win_sec filter window: */
++ expire = bbr->min_rtt_stamp + bbr_param(sk, min_rtt_win_sec) * HZ;
++ min_rtt_expired = after(tcp_jiffies32, expire);
++ if (bbr->probe_rtt_min_us <= bbr->min_rtt_us ||
++ min_rtt_expired) {
++ bbr->min_rtt_us = bbr->probe_rtt_min_us;
++ bbr->min_rtt_stamp = bbr->probe_rtt_min_stamp;
+ }
+
+- if (bbr_probe_rtt_mode_ms > 0 && filter_expired &&
++ if (bbr_param(sk, probe_rtt_mode_ms) > 0 && probe_rtt_expired &&
+ !bbr->idle_restart && bbr->mode != BBR_PROBE_RTT) {
+ bbr->mode = BBR_PROBE_RTT; /* dip, drain queue */
+ bbr_save_cwnd(sk); /* note cwnd so we can restore it */
+ bbr->probe_rtt_done_stamp = 0;
++ bbr->ack_phase = BBR_ACKS_PROBE_STOPPING;
++ bbr->next_rtt_delivered = tp->delivered;
+ }
+
+ if (bbr->mode == BBR_PROBE_RTT) {
+@@ -967,9 +922,9 @@ static void bbr_update_min_rtt(struct sock *sk, const struct rate_sample *rs)
+ (tp->delivered + tcp_packets_in_flight(tp)) ? : 1;
+ /* Maintain min packets in flight for max(200 ms, 1 round). */
+ if (!bbr->probe_rtt_done_stamp &&
+- tcp_packets_in_flight(tp) <= bbr_cwnd_min_target) {
++ tcp_packets_in_flight(tp) <= bbr_probe_rtt_cwnd(sk)) {
+ bbr->probe_rtt_done_stamp = tcp_jiffies32 +
+- msecs_to_jiffies(bbr_probe_rtt_mode_ms);
++ msecs_to_jiffies(bbr_param(sk, probe_rtt_mode_ms));
+ bbr->probe_rtt_round_done = 0;
+ bbr->next_rtt_delivered = tp->delivered;
+ } else if (bbr->probe_rtt_done_stamp) {
+@@ -990,18 +945,20 @@ static void bbr_update_gains(struct sock *sk)
+
+ switch (bbr->mode) {
+ case BBR_STARTUP:
+- bbr->pacing_gain = bbr_high_gain;
+- bbr->cwnd_gain = bbr_high_gain;
++ bbr->pacing_gain = bbr_param(sk, startup_pacing_gain);
++ bbr->cwnd_gain = bbr_param(sk, startup_cwnd_gain);
+ break;
+ case BBR_DRAIN:
+- bbr->pacing_gain = bbr_drain_gain; /* slow, to drain */
+- bbr->cwnd_gain = bbr_high_gain; /* keep cwnd */
++ bbr->pacing_gain = bbr_param(sk, drain_gain); /* slow, to drain */
++ bbr->cwnd_gain = bbr_param(sk, startup_cwnd_gain); /* keep cwnd */
+ break;
+ case BBR_PROBE_BW:
+- bbr->pacing_gain = (bbr->lt_use_bw ?
+- BBR_UNIT :
+- bbr_pacing_gain[bbr->cycle_idx]);
+- bbr->cwnd_gain = bbr_cwnd_gain;
++ bbr->pacing_gain = bbr_pacing_gain[bbr->cycle_idx];
++ bbr->cwnd_gain = bbr_param(sk, cwnd_gain);
++ if (bbr_param(sk, bw_probe_cwnd_gain) &&
++ bbr->cycle_idx == BBR_BW_PROBE_UP)
++ bbr->cwnd_gain +=
++ BBR_UNIT * bbr_param(sk, bw_probe_cwnd_gain) / 4;
+ break;
+ case BBR_PROBE_RTT:
+ bbr->pacing_gain = BBR_UNIT;
+@@ -1013,144 +970,1387 @@ static void bbr_update_gains(struct sock *sk)
+ }
+ }
+
+-static void bbr_update_model(struct sock *sk, const struct rate_sample *rs)
++__bpf_kfunc static u32 bbr_sndbuf_expand(struct sock *sk)
+ {
+- bbr_update_bw(sk, rs);
+- bbr_update_ack_aggregation(sk, rs);
+- bbr_update_cycle_phase(sk, rs);
+- bbr_check_full_bw_reached(sk, rs);
+- bbr_check_drain(sk, rs);
+- bbr_update_min_rtt(sk, rs);
+- bbr_update_gains(sk);
++ /* Provision 3 * cwnd since BBR may slow-start even during recovery. */
++ return 3;
+ }
+
+-__bpf_kfunc static void bbr_main(struct sock *sk, const struct rate_sample *rs)
++/* Incorporate a new bw sample into the current window of our max filter. */
++static void bbr_take_max_bw_sample(struct sock *sk, u32 bw)
+ {
+ struct bbr *bbr = inet_csk_ca(sk);
+- u32 bw;
+-
+- bbr_update_model(sk, rs);
+
+- bw = bbr_bw(sk);
+- bbr_set_pacing_rate(sk, bw, bbr->pacing_gain);
+- bbr_set_cwnd(sk, rs, rs->acked_sacked, bw, bbr->cwnd_gain);
++ bbr->bw_hi[1] = max(bw, bbr->bw_hi[1]);
+ }
+
+-__bpf_kfunc static void bbr_init(struct sock *sk)
++/* Keep max of last 1-2 cycles. Each PROBE_BW cycle, flip filter window. */
++static void bbr_advance_max_bw_filter(struct sock *sk)
+ {
+- struct tcp_sock *tp = tcp_sk(sk);
+ struct bbr *bbr = inet_csk_ca(sk);
+
+- bbr->prior_cwnd = 0;
+- tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
+- bbr->rtt_cnt = 0;
+- bbr->next_rtt_delivered = tp->delivered;
+- bbr->prev_ca_state = TCP_CA_Open;
+- bbr->packet_conservation = 0;
+-
+- bbr->probe_rtt_done_stamp = 0;
+- bbr->probe_rtt_round_done = 0;
+- bbr->min_rtt_us = tcp_min_rtt(tp);
+- bbr->min_rtt_stamp = tcp_jiffies32;
+-
+- minmax_reset(&bbr->bw, bbr->rtt_cnt, 0); /* init max bw to 0 */
++ if (!bbr->bw_hi[1])
++ return; /* no samples in this window; remember old window */
++ bbr->bw_hi[0] = bbr->bw_hi[1];
++ bbr->bw_hi[1] = 0;
++}
+
+- bbr->has_seen_rtt = 0;
+- bbr_init_pacing_rate_from_rtt(sk);
++/* Reset the estimator for reaching full bandwidth based on bw plateau. */
++static void bbr_reset_full_bw(struct sock *sk)
++{
++ struct bbr *bbr = inet_csk_ca(sk);
+
+- bbr->round_start = 0;
+- bbr->idle_restart = 0;
+- bbr->full_bw_reached = 0;
+ bbr->full_bw = 0;
+ bbr->full_bw_cnt = 0;
+- bbr->cycle_mstamp = 0;
+- bbr->cycle_idx = 0;
+- bbr_reset_lt_bw_sampling(sk);
+- bbr_reset_startup_mode(sk);
++ bbr->full_bw_now = 0;
++}
+
+- bbr->ack_epoch_mstamp = tp->tcp_mstamp;
+- bbr->ack_epoch_acked = 0;
+- bbr->extra_acked_win_rtts = 0;
+- bbr->extra_acked_win_idx = 0;
+- bbr->extra_acked[0] = 0;
+- bbr->extra_acked[1] = 0;
++/* How much do we want in flight? Our BDP, unless congestion cut cwnd. */
++static u32 bbr_target_inflight(struct sock *sk)
++{
++ u32 bdp = bbr_inflight(sk, bbr_bw(sk), BBR_UNIT);
+
+- cmpxchg(&sk->sk_pacing_status, SK_PACING_NONE, SK_PACING_NEEDED);
++ return min(bdp, tcp_sk(sk)->snd_cwnd);
+ }
+
+-__bpf_kfunc static u32 bbr_sndbuf_expand(struct sock *sk)
++static bool bbr_is_probing_bandwidth(struct sock *sk)
+ {
+- /* Provision 3 * cwnd since BBR may slow-start even during recovery. */
+- return 3;
++ struct bbr *bbr = inet_csk_ca(sk);
++
++ return (bbr->mode == BBR_STARTUP) ||
++ (bbr->mode == BBR_PROBE_BW &&
++ (bbr->cycle_idx == BBR_BW_PROBE_REFILL ||
++ bbr->cycle_idx == BBR_BW_PROBE_UP));
++}
++
++/* Has the given amount of time elapsed since we marked the phase start? */
++static bool bbr_has_elapsed_in_phase(const struct sock *sk, u32 interval_us)
++{
++ const struct tcp_sock *tp = tcp_sk(sk);
++ const struct bbr *bbr = inet_csk_ca(sk);
++
++ return tcp_stamp_us_delta(tp->tcp_mstamp,
++ bbr->cycle_mstamp + interval_us) > 0;
++}
++
++static void bbr_handle_queue_too_high_in_startup(struct sock *sk)
++{
++ struct bbr *bbr = inet_csk_ca(sk);
++ u32 bdp; /* estimated BDP in packets, with quantization budget */
++
++ bbr->full_bw_reached = 1;
++
++ bdp = bbr_inflight(sk, bbr_max_bw(sk), BBR_UNIT);
++ bbr->inflight_hi = max(bdp, bbr->inflight_latest);
++}
++
++/* Exit STARTUP upon N consecutive rounds with ECN mark rate > ecn_thresh. */
++static void bbr_check_ecn_too_high_in_startup(struct sock *sk, u32 ce_ratio)
++{
++ struct bbr *bbr = inet_csk_ca(sk);
++
++ if (bbr_full_bw_reached(sk) || !bbr->ecn_eligible ||
++ !bbr_param(sk, full_ecn_cnt) || !bbr_param(sk, ecn_thresh))
++ return;
++
++ if (ce_ratio >= bbr_param(sk, ecn_thresh))
++ bbr->startup_ecn_rounds++;
++ else
++ bbr->startup_ecn_rounds = 0;
++
++ if (bbr->startup_ecn_rounds >= bbr_param(sk, full_ecn_cnt)) {
++ bbr_handle_queue_too_high_in_startup(sk);
++ return;
++ }
++}
++
++/* Updates ecn_alpha and returns ce_ratio. -1 if not available. */
++static int bbr_update_ecn_alpha(struct sock *sk)
++{
++ struct tcp_sock *tp = tcp_sk(sk);
++ struct net *net = sock_net(sk);
++ struct bbr *bbr = inet_csk_ca(sk);
++ s32 delivered, delivered_ce;
++ u64 alpha, ce_ratio;
++ u32 gain;
++ bool want_ecn_alpha;
++
++ /* See if we should use ECN sender logic for this connection. */
++ if (!bbr->ecn_eligible && bbr_can_use_ecn(sk) &&
++ bbr_param(sk, ecn_factor) &&
++ (bbr->min_rtt_us <= bbr_ecn_max_rtt_us ||
++ !bbr_ecn_max_rtt_us))
++ bbr->ecn_eligible = 1;
++
++ /* Skip updating alpha only if not ECN-eligible and PLB is disabled. */
++ want_ecn_alpha = (bbr->ecn_eligible ||
++ (bbr_can_use_ecn(sk) &&
++ READ_ONCE(net->ipv4.sysctl_tcp_plb_enabled)));
++ if (!want_ecn_alpha)
++ return -1;
++
++ delivered = tp->delivered - bbr->alpha_last_delivered;
++ delivered_ce = tp->delivered_ce - bbr->alpha_last_delivered_ce;
++
++ if (delivered == 0 || /* avoid divide by zero */
++ WARN_ON_ONCE(delivered < 0 || delivered_ce < 0)) /* backwards? */
++ return -1;
++
++ BUILD_BUG_ON(BBR_SCALE != TCP_PLB_SCALE);
++ ce_ratio = (u64)delivered_ce << BBR_SCALE;
++ do_div(ce_ratio, delivered);
++
++ gain = bbr_param(sk, ecn_alpha_gain);
++ alpha = ((BBR_UNIT - gain) * bbr->ecn_alpha) >> BBR_SCALE;
++ alpha += (gain * ce_ratio) >> BBR_SCALE;
++ bbr->ecn_alpha = min_t(u32, alpha, BBR_UNIT);
++
++ bbr->alpha_last_delivered = tp->delivered;
++ bbr->alpha_last_delivered_ce = tp->delivered_ce;
++
++ bbr_check_ecn_too_high_in_startup(sk, ce_ratio);
++ return (int)ce_ratio;
+ }
+
+-/* In theory BBR does not need to undo the cwnd since it does not
+- * always reduce cwnd on losses (see bbr_main()). Keep it for now.
++/* Protective Load Balancing (PLB). PLB rehashes outgoing data (to a new IPv6
++ * flow label) if it encounters sustained congestion in the form of ECN marks.
+ */
+-__bpf_kfunc static u32 bbr_undo_cwnd(struct sock *sk)
++static void bbr_plb(struct sock *sk, const struct rate_sample *rs, int ce_ratio)
++{
++ struct bbr *bbr = inet_csk_ca(sk);
++
++ if (bbr->round_start && ce_ratio >= 0)
++ tcp_plb_update_state(sk, &bbr->plb, ce_ratio);
++
++ tcp_plb_check_rehash(sk, &bbr->plb);
++}
++
++/* Each round trip of BBR_BW_PROBE_UP, double volume of probing data. */
++static void bbr_raise_inflight_hi_slope(struct sock *sk)
++{
++ struct tcp_sock *tp = tcp_sk(sk);
++ struct bbr *bbr = inet_csk_ca(sk);
++ u32 growth_this_round, cnt;
++
++ /* Calculate "slope": packets S/Acked per inflight_hi increment. */
++ growth_this_round = 1 << bbr->bw_probe_up_rounds;
++ bbr->bw_probe_up_rounds = min(bbr->bw_probe_up_rounds + 1, 30);
++ cnt = tcp_snd_cwnd(tp) / growth_this_round;
++ cnt = max(cnt, 1U);
++ bbr->bw_probe_up_cnt = cnt;
++}
++
++/* In BBR_BW_PROBE_UP, not seeing high loss/ECN/queue, so raise inflight_hi. */
++static void bbr_probe_inflight_hi_upward(struct sock *sk,
++ const struct rate_sample *rs)
++{
++ struct tcp_sock *tp = tcp_sk(sk);
++ struct bbr *bbr = inet_csk_ca(sk);
++ u32 delta;
++
++ if (!tp->is_cwnd_limited || tcp_snd_cwnd(tp) < bbr->inflight_hi)
++ return; /* not fully using inflight_hi, so don't grow it */
++
++ /* For each bw_probe_up_cnt packets ACKed, increase inflight_hi by 1. */
++ bbr->bw_probe_up_acks += rs->acked_sacked;
++ if (bbr->bw_probe_up_acks >= bbr->bw_probe_up_cnt) {
++ delta = bbr->bw_probe_up_acks / bbr->bw_probe_up_cnt;
++ bbr->bw_probe_up_acks -= delta * bbr->bw_probe_up_cnt;
++ bbr->inflight_hi += delta;
++ bbr->try_fast_path = 0; /* Need to update cwnd */
++ }
++
++ if (bbr->round_start)
++ bbr_raise_inflight_hi_slope(sk);
++}
++
++/* Does loss/ECN rate for this sample say inflight is "too high"?
++ * This is used by both the bbr_check_loss_too_high_in_startup() function,
++ * which can be used in either v1 or v2, and the PROBE_UP phase of v2, which
++ * uses it to notice when loss/ECN rates suggest inflight is too high.
++ */
++static bool bbr_is_inflight_too_high(const struct sock *sk,
++ const struct rate_sample *rs)
++{
++ const struct bbr *bbr = inet_csk_ca(sk);
++ u32 loss_thresh, ecn_thresh;
++
++ if (rs->lost > 0 && rs->tx_in_flight) {
++ loss_thresh = (u64)rs->tx_in_flight * bbr_param(sk, loss_thresh) >>
++ BBR_SCALE;
++ if (rs->lost > loss_thresh) {
++ return true;
++ }
++ }
++
++ if (rs->delivered_ce > 0 && rs->delivered > 0 &&
++ bbr->ecn_eligible && bbr_param(sk, ecn_thresh)) {
++ ecn_thresh = (u64)rs->delivered * bbr_param(sk, ecn_thresh) >>
++ BBR_SCALE;
++ if (rs->delivered_ce > ecn_thresh) {
++ return true;
++ }
++ }
++
++ return false;
++}
++
++/* Calculate the tx_in_flight level that corresponded to excessive loss.
++ * We find "lost_prefix" segs of the skb where loss rate went too high,
++ * by solving for "lost_prefix" in the following equation:
++ * lost / inflight >= loss_thresh
++ * (lost_prev + lost_prefix) / (inflight_prev + lost_prefix) >= loss_thresh
++ * Then we take that equation, convert it to fixed point, and
++ * round up to the nearest packet.
++ */
++static u32 bbr_inflight_hi_from_lost_skb(const struct sock *sk,
++ const struct rate_sample *rs,
++ const struct sk_buff *skb)
++{
++ const struct tcp_sock *tp = tcp_sk(sk);
++ u32 loss_thresh = bbr_param(sk, loss_thresh);
++ u32 pcount, divisor, inflight_hi;
++ s32 inflight_prev, lost_prev;
++ u64 loss_budget, lost_prefix;
++
++ pcount = tcp_skb_pcount(skb);
++
++ /* How much data was in flight before this skb? */
++ inflight_prev = rs->tx_in_flight - pcount;
++ if (inflight_prev < 0) {
++ WARN_ONCE(tcp_skb_tx_in_flight_is_suspicious(
++ pcount,
++ TCP_SKB_CB(skb)->sacked,
++ rs->tx_in_flight),
++ "tx_in_flight: %u pcount: %u reneg: %u",
++ rs->tx_in_flight, pcount, tcp_sk(sk)->is_sack_reneg);
++ return ~0U;
++ }
++
++ /* How much inflight data was marked lost before this skb? */
++ lost_prev = rs->lost - pcount;
++ if (WARN_ONCE(lost_prev < 0,
++ "cwnd: %u ca: %d out: %u lost: %u pif: %u "
++ "tx_in_flight: %u tx.lost: %u tp->lost: %u rs->lost: %d "
++ "lost_prev: %d pcount: %d seq: %u end_seq: %u reneg: %u",
++ tcp_snd_cwnd(tp), inet_csk(sk)->icsk_ca_state,
++ tp->packets_out, tp->lost_out, tcp_packets_in_flight(tp),
++ rs->tx_in_flight, TCP_SKB_CB(skb)->tx.lost, tp->lost,
++ rs->lost, lost_prev, pcount,
++ TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq,
++ tp->is_sack_reneg))
++ return ~0U;
++
++ /* At what prefix of this lost skb did losss rate exceed loss_thresh? */
++ loss_budget = (u64)inflight_prev * loss_thresh + BBR_UNIT - 1;
++ loss_budget >>= BBR_SCALE;
++ if (lost_prev >= loss_budget) {
++ lost_prefix = 0; /* previous losses crossed loss_thresh */
++ } else {
++ lost_prefix = loss_budget - lost_prev;
++ lost_prefix <<= BBR_SCALE;
++ divisor = BBR_UNIT - loss_thresh;
++ if (WARN_ON_ONCE(!divisor)) /* loss_thresh is 8 bits */
++ return ~0U;
++ do_div(lost_prefix, divisor);
++ }
++
++ inflight_hi = inflight_prev + lost_prefix;
++ return inflight_hi;
++}
++
++/* If loss/ECN rates during probing indicated we may have overfilled a
++ * buffer, return an operating point that tries to leave unutilized headroom in
++ * the path for other flows, for fairness convergence and lower RTTs and loss.
++ */
++static u32 bbr_inflight_with_headroom(const struct sock *sk)
++{
++ struct bbr *bbr = inet_csk_ca(sk);
++ u32 headroom, headroom_fraction;
++
++ if (bbr->inflight_hi == ~0U)
++ return ~0U;
++
++ headroom_fraction = bbr_param(sk, inflight_headroom);
++ headroom = ((u64)bbr->inflight_hi * headroom_fraction) >> BBR_SCALE;
++ headroom = max(headroom, 1U);
++ return max_t(s32, bbr->inflight_hi - headroom,
++ bbr_param(sk, cwnd_min_target));
++}
++
++/* Bound cwnd to a sensible level, based on our current probing state
++ * machine phase and model of a good inflight level (inflight_lo, inflight_hi).
++ */
++static void bbr_bound_cwnd_for_inflight_model(struct sock *sk)
++{
++ struct tcp_sock *tp = tcp_sk(sk);
++ struct bbr *bbr = inet_csk_ca(sk);
++ u32 cap;
++
++ /* tcp_rcv_synsent_state_process() currently calls tcp_ack()
++ * and thus cong_control() without first initializing us(!).
++ */
++ if (!bbr->initialized)
++ return;
++
++ cap = ~0U;
++ if (bbr->mode == BBR_PROBE_BW &&
++ bbr->cycle_idx != BBR_BW_PROBE_CRUISE) {
++ /* Probe to see if more packets fit in the path. */
++ cap = bbr->inflight_hi;
++ } else {
++ if (bbr->mode == BBR_PROBE_RTT ||
++ (bbr->mode == BBR_PROBE_BW &&
++ bbr->cycle_idx == BBR_BW_PROBE_CRUISE))
++ cap = bbr_inflight_with_headroom(sk);
++ }
++ /* Adapt to any loss/ECN since our last bw probe. */
++ cap = min(cap, bbr->inflight_lo);
++
++ cap = max_t(u32, cap, bbr_param(sk, cwnd_min_target));
++ tcp_snd_cwnd_set(tp, min(cap, tcp_snd_cwnd(tp)));
++}
++
++/* How should we multiplicatively cut bw or inflight limits based on ECN? */
++static u32 bbr_ecn_cut(struct sock *sk)
++{
++ struct bbr *bbr = inet_csk_ca(sk);
++
++ return BBR_UNIT -
++ ((bbr->ecn_alpha * bbr_param(sk, ecn_factor)) >> BBR_SCALE);
++}
++
++/* Init lower bounds if have not inited yet. */
++static void bbr_init_lower_bounds(struct sock *sk, bool init_bw)
++{
++ struct tcp_sock *tp = tcp_sk(sk);
++ struct bbr *bbr = inet_csk_ca(sk);
++
++ if (init_bw && bbr->bw_lo == ~0U)
++ bbr->bw_lo = bbr_max_bw(sk);
++ if (bbr->inflight_lo == ~0U)
++ bbr->inflight_lo = tcp_snd_cwnd(tp);
++}
++
++/* Reduce bw and inflight to (1 - beta). */
++static void bbr_loss_lower_bounds(struct sock *sk, u32 *bw, u32 *inflight)
++{
++ struct bbr* bbr = inet_csk_ca(sk);
++ u32 loss_cut = BBR_UNIT - bbr_param(sk, beta);
++
++ *bw = max_t(u32, bbr->bw_latest,
++ (u64)bbr->bw_lo * loss_cut >> BBR_SCALE);
++ *inflight = max_t(u32, bbr->inflight_latest,
++ (u64)bbr->inflight_lo * loss_cut >> BBR_SCALE);
++}
++
++/* Reduce inflight to (1 - alpha*ecn_factor). */
++static void bbr_ecn_lower_bounds(struct sock *sk, u32 *inflight)
++{
++ struct bbr *bbr = inet_csk_ca(sk);
++ u32 ecn_cut = bbr_ecn_cut(sk);
++
++ *inflight = (u64)bbr->inflight_lo * ecn_cut >> BBR_SCALE;
++}
++
++/* Estimate a short-term lower bound on the capacity available now, based
++ * on measurements of the current delivery process and recent history. When we
++ * are seeing loss/ECN at times when we are not probing bw, then conservatively
++ * move toward flow balance by multiplicatively cutting our short-term
++ * estimated safe rate and volume of data (bw_lo and inflight_lo). We use a
++ * multiplicative decrease in order to converge to a lower capacity in time
++ * logarithmic in the magnitude of the decrease.
++ *
++ * However, we do not cut our short-term estimates lower than the current rate
++ * and volume of delivered data from this round trip, since from the current
++ * delivery process we can estimate the measured capacity available now.
++ *
++ * Anything faster than that approach would knowingly risk high loss, which can
++ * cause low bw for Reno/CUBIC and high loss recovery latency for
++ * request/response flows using any congestion control.
++ */
++static void bbr_adapt_lower_bounds(struct sock *sk,
++ const struct rate_sample *rs)
++{
++ struct bbr *bbr = inet_csk_ca(sk);
++ u32 ecn_inflight_lo = ~0U;
++
++ /* We only use lower-bound estimates when not probing bw.
++ * When probing we need to push inflight higher to probe bw.
++ */
++ if (bbr_is_probing_bandwidth(sk))
++ return;
++
++ /* ECN response. */
++ if (bbr->ecn_in_round && bbr_param(sk, ecn_factor)) {
++ bbr_init_lower_bounds(sk, false);
++ bbr_ecn_lower_bounds(sk, &ecn_inflight_lo);
++ }
++
++ /* Loss response. */
++ if (bbr->loss_in_round) {
++ bbr_init_lower_bounds(sk, true);
++ bbr_loss_lower_bounds(sk, &bbr->bw_lo, &bbr->inflight_lo);
++ }
++
++ /* Adjust to the lower of the levels implied by loss/ECN. */
++ bbr->inflight_lo = min(bbr->inflight_lo, ecn_inflight_lo);
++ bbr->bw_lo = max(1U, bbr->bw_lo);
++}
++
++/* Reset any short-term lower-bound adaptation to congestion, so that we can
++ * push our inflight up.
++ */
++static void bbr_reset_lower_bounds(struct sock *sk)
++{
++ struct bbr *bbr = inet_csk_ca(sk);
++
++ bbr->bw_lo = ~0U;
++ bbr->inflight_lo = ~0U;
++}
++
++/* After bw probing (STARTUP/PROBE_UP), reset signals before entering a state
++ * machine phase where we adapt our lower bound based on congestion signals.
++ */
++static void bbr_reset_congestion_signals(struct sock *sk)
++{
++ struct bbr *bbr = inet_csk_ca(sk);
++
++ bbr->loss_in_round = 0;
++ bbr->ecn_in_round = 0;
++ bbr->loss_in_cycle = 0;
++ bbr->ecn_in_cycle = 0;
++ bbr->bw_latest = 0;
++ bbr->inflight_latest = 0;
++}
++
++static void bbr_exit_loss_recovery(struct sock *sk)
++{
++ struct tcp_sock *tp = tcp_sk(sk);
++ struct bbr *bbr = inet_csk_ca(sk);
++
++ tcp_snd_cwnd_set(tp, max(tcp_snd_cwnd(tp), bbr->prior_cwnd));
++ bbr->try_fast_path = 0; /* bound cwnd using latest model */
++}
++
++/* Update rate and volume of delivered data from latest round trip. */
++static void bbr_update_latest_delivery_signals(
++ struct sock *sk, const struct rate_sample *rs, struct bbr_context *ctx)
++{
++ struct tcp_sock *tp = tcp_sk(sk);
++ struct bbr *bbr = inet_csk_ca(sk);
++
++ bbr->loss_round_start = 0;
++ if (rs->interval_us <= 0 || !rs->acked_sacked)
++ return; /* Not a valid observation */
++
++ bbr->bw_latest = max_t(u32, bbr->bw_latest, ctx->sample_bw);
++ bbr->inflight_latest = max_t(u32, bbr->inflight_latest, rs->delivered);
++
++ if (!before(rs->prior_delivered, bbr->loss_round_delivered)) {
++ bbr->loss_round_delivered = tp->delivered;
++ bbr->loss_round_start = 1; /* mark start of new round trip */
++ }
++}
++
++/* Once per round, reset filter for latest rate and volume of delivered data. */
++static void bbr_advance_latest_delivery_signals(
++ struct sock *sk, const struct rate_sample *rs, struct bbr_context *ctx)
++{
++ struct bbr *bbr = inet_csk_ca(sk);
++
++ /* If ACK matches a TLP retransmit, persist the filter. If we detect
++ * that a TLP retransmit plugged a tail loss, we'll want to remember
++ * how much data the path delivered before the tail loss.
++ */
++ if (bbr->loss_round_start && !rs->is_acking_tlp_retrans_seq) {
++ bbr->bw_latest = ctx->sample_bw;
++ bbr->inflight_latest = rs->delivered;
++ }
++}
++
++/* Update (most of) our congestion signals: track the recent rate and volume of
++ * delivered data, presence of loss, and EWMA degree of ECN marking.
++ */
++static void bbr_update_congestion_signals(
++ struct sock *sk, const struct rate_sample *rs, struct bbr_context *ctx)
+ {
+ struct bbr *bbr = inet_csk_ca(sk);
++ u64 bw;
++
++ if (rs->interval_us <= 0 || !rs->acked_sacked)
++ return; /* Not a valid observation */
++ bw = ctx->sample_bw;
+
+- bbr->full_bw = 0; /* spurious slow-down; reset full pipe detection */
++ if (!rs->is_app_limited || bw >= bbr_max_bw(sk))
++ bbr_take_max_bw_sample(sk, bw);
++
++ bbr->loss_in_round |= (rs->losses > 0);
++
++ if (!bbr->loss_round_start)
++ return; /* skip the per-round-trip updates */
++ /* Now do per-round-trip updates. */
++ bbr_adapt_lower_bounds(sk, rs);
++
++ bbr->loss_in_round = 0;
++ bbr->ecn_in_round = 0;
++}
++
++/* Bandwidth probing can cause loss. To help coexistence with loss-based
++ * congestion control we spread out our probing in a Reno-conscious way. Due to
++ * the shape of the Reno sawtooth, the time required between loss epochs for an
++ * idealized Reno flow is a number of round trips that is the BDP of that
++ * flow. We count packet-timed round trips directly, since measured RTT can
++ * vary widely, and Reno is driven by packet-timed round trips.
++ */
++static bool bbr_is_reno_coexistence_probe_time(struct sock *sk)
++{
++ struct bbr *bbr = inet_csk_ca(sk);
++ u32 rounds;
++
++ /* Random loss can shave some small percentage off of our inflight
++ * in each round. To survive this, flows need robust periodic probes.
++ */
++ rounds = min_t(u32, bbr_param(sk, bw_probe_max_rounds), bbr_target_inflight(sk));
++ return bbr->rounds_since_probe >= rounds;
++}
++
++/* How long do we want to wait before probing for bandwidth (and risking
++ * loss)? We randomize the wait, for better mixing and fairness convergence.
++ *
++ * We bound the Reno-coexistence inter-bw-probe time to be 62-63 round trips.
++ * This is calculated to allow fairness with a 25Mbps, 30ms Reno flow,
++ * (eg 4K video to a broadband user):
++ * BDP = 25Mbps * .030sec /(1514bytes) = 61.9 packets
++ *
++ * We bound the BBR-native inter-bw-probe wall clock time to be:
++ * (a) higher than 2 sec: to try to avoid causing loss for a long enough time
++ * to allow Reno at 30ms to get 4K video bw, the inter-bw-probe time must
++ * be at least: 25Mbps * .030sec / (1514bytes) * 0.030sec = 1.9secs
++ * (b) lower than 3 sec: to ensure flows can start probing in a reasonable
++ * amount of time to discover unutilized bw on human-scale interactive
++ * time-scales (e.g. perhaps traffic from a web page download that we
++ * were competing with is now complete).
++ */
++static void bbr_pick_probe_wait(struct sock *sk)
++{
++ struct bbr *bbr = inet_csk_ca(sk);
++
++ /* Decide the random round-trip bound for wait until probe: */
++ bbr->rounds_since_probe =
++ get_random_u32_below(bbr_param(sk, bw_probe_rand_rounds));
++ /* Decide the random wall clock bound for wait until probe: */
++ bbr->probe_wait_us = bbr_param(sk, bw_probe_base_us) +
++ get_random_u32_below(bbr_param(sk, bw_probe_rand_us));
++}
++
++static void bbr_set_cycle_idx(struct sock *sk, int cycle_idx)
++{
++ struct bbr *bbr = inet_csk_ca(sk);
++
++ bbr->cycle_idx = cycle_idx;
++ /* New phase, so need to update cwnd and pacing rate. */
++ bbr->try_fast_path = 0;
++}
++
++/* Send at estimated bw to fill the pipe, but not queue. We need this phase
++ * before PROBE_UP, because as soon as we send faster than the available bw
++ * we will start building a queue, and if the buffer is shallow we can cause
++ * loss. If we do not fill the pipe before we cause this loss, our bw_hi and
++ * inflight_hi estimates will underestimate.
++ */
++static void bbr_start_bw_probe_refill(struct sock *sk, u32 bw_probe_up_rounds)
++{
++ struct tcp_sock *tp = tcp_sk(sk);
++ struct bbr *bbr = inet_csk_ca(sk);
++
++ bbr_reset_lower_bounds(sk);
++ bbr->bw_probe_up_rounds = bw_probe_up_rounds;
++ bbr->bw_probe_up_acks = 0;
++ bbr->stopped_risky_probe = 0;
++ bbr->ack_phase = BBR_ACKS_REFILLING;
++ bbr->next_rtt_delivered = tp->delivered;
++ bbr_set_cycle_idx(sk, BBR_BW_PROBE_REFILL);
++}
++
++/* Now probe max deliverable data rate and volume. */
++static void bbr_start_bw_probe_up(struct sock *sk, struct bbr_context *ctx)
++{
++ struct tcp_sock *tp = tcp_sk(sk);
++ struct bbr *bbr = inet_csk_ca(sk);
++
++ bbr->ack_phase = BBR_ACKS_PROBE_STARTING;
++ bbr->next_rtt_delivered = tp->delivered;
++ bbr->cycle_mstamp = tp->tcp_mstamp;
++ bbr_reset_full_bw(sk);
++ bbr->full_bw = ctx->sample_bw;
++ bbr_set_cycle_idx(sk, BBR_BW_PROBE_UP);
++ bbr_raise_inflight_hi_slope(sk);
++}
++
++/* Start a new PROBE_BW probing cycle of some wall clock length. Pick a wall
++ * clock time at which to probe beyond an inflight that we think to be
++ * safe. This will knowingly risk packet loss, so we want to do this rarely, to
++ * keep packet loss rates low. Also start a round-trip counter, to probe faster
++ * if we estimate a Reno flow at our BDP would probe faster.
++ */
++static void bbr_start_bw_probe_down(struct sock *sk)
++{
++ struct tcp_sock *tp = tcp_sk(sk);
++ struct bbr *bbr = inet_csk_ca(sk);
++
++ bbr_reset_congestion_signals(sk);
++ bbr->bw_probe_up_cnt = ~0U; /* not growing inflight_hi any more */
++ bbr_pick_probe_wait(sk);
++ bbr->cycle_mstamp = tp->tcp_mstamp; /* start wall clock */
++ bbr->ack_phase = BBR_ACKS_PROBE_STOPPING;
++ bbr->next_rtt_delivered = tp->delivered;
++ bbr_set_cycle_idx(sk, BBR_BW_PROBE_DOWN);
++}
++
++/* Cruise: maintain what we estimate to be a neutral, conservative
++ * operating point, without attempting to probe up for bandwidth or down for
++ * RTT, and only reducing inflight in response to loss/ECN signals.
++ */
++static void bbr_start_bw_probe_cruise(struct sock *sk)
++{
++ struct bbr *bbr = inet_csk_ca(sk);
++
++ if (bbr->inflight_lo != ~0U)
++ bbr->inflight_lo = min(bbr->inflight_lo, bbr->inflight_hi);
++
++ bbr_set_cycle_idx(sk, BBR_BW_PROBE_CRUISE);
++}
++
++/* Loss and/or ECN rate is too high while probing.
++ * Adapt (once per bw probe) by cutting inflight_hi and then restarting cycle.
++ */
++static void bbr_handle_inflight_too_high(struct sock *sk,
++ const struct rate_sample *rs)
++{
++ struct bbr *bbr = inet_csk_ca(sk);
++ const u32 beta = bbr_param(sk, beta);
++
++ bbr->prev_probe_too_high = 1;
++ bbr->bw_probe_samples = 0; /* only react once per probe */
++ /* If we are app-limited then we are not robustly
++ * probing the max volume of inflight data we think
++ * might be safe (analogous to how app-limited bw
++ * samples are not known to be robustly probing bw).
++ */
++ if (!rs->is_app_limited) {
++ bbr->inflight_hi = max_t(u32, rs->tx_in_flight,
++ (u64)bbr_target_inflight(sk) *
++ (BBR_UNIT - beta) >> BBR_SCALE);
++ }
++ if (bbr->mode == BBR_PROBE_BW && bbr->cycle_idx == BBR_BW_PROBE_UP)
++ bbr_start_bw_probe_down(sk);
++}
++
++/* If we're seeing bw and loss samples reflecting our bw probing, adapt
++ * using the signals we see. If loss or ECN mark rate gets too high, then adapt
++ * inflight_hi downward. If we're able to push inflight higher without such
++ * signals, push higher: adapt inflight_hi upward.
++ */
++static bool bbr_adapt_upper_bounds(struct sock *sk,
++ const struct rate_sample *rs,
++ struct bbr_context *ctx)
++{
++ struct bbr *bbr = inet_csk_ca(sk);
++
++ /* Track when we'll see bw/loss samples resulting from our bw probes. */
++ if (bbr->ack_phase == BBR_ACKS_PROBE_STARTING && bbr->round_start)
++ bbr->ack_phase = BBR_ACKS_PROBE_FEEDBACK;
++ if (bbr->ack_phase == BBR_ACKS_PROBE_STOPPING && bbr->round_start) {
++ /* End of samples from bw probing phase. */
++ bbr->bw_probe_samples = 0;
++ bbr->ack_phase = BBR_ACKS_INIT;
++ /* At this point in the cycle, our current bw sample is also
++ * our best recent chance at finding the highest available bw
++ * for this flow. So now is the best time to forget the bw
++ * samples from the previous cycle, by advancing the window.
++ */
++ if (bbr->mode == BBR_PROBE_BW && !rs->is_app_limited)
++ bbr_advance_max_bw_filter(sk);
++ /* If we had an inflight_hi, then probed and pushed inflight all
++ * the way up to hit that inflight_hi without seeing any
++ * high loss/ECN in all the resulting ACKs from that probing,
++ * then probe up again, this time letting inflight persist at
++ * inflight_hi for a round trip, then accelerating beyond.
++ */
++ if (bbr->mode == BBR_PROBE_BW &&
++ bbr->stopped_risky_probe && !bbr->prev_probe_too_high) {
++ bbr_start_bw_probe_refill(sk, 0);
++ return true; /* yes, decided state transition */
++ }
++ }
++ if (bbr_is_inflight_too_high(sk, rs)) {
++ if (bbr->bw_probe_samples) /* sample is from bw probing? */
++ bbr_handle_inflight_too_high(sk, rs);
++ } else {
++ /* Loss/ECN rate is declared safe. Adjust upper bound upward. */
++
++ if (bbr->inflight_hi == ~0U)
++ return false; /* no excess queue signals yet */
++
++ /* To be resilient to random loss, we must raise bw/inflight_hi
++ * if we observe in any phase that a higher level is safe.
++ */
++ if (rs->tx_in_flight > bbr->inflight_hi) {
++ bbr->inflight_hi = rs->tx_in_flight;
++ }
++
++ if (bbr->mode == BBR_PROBE_BW &&
++ bbr->cycle_idx == BBR_BW_PROBE_UP)
++ bbr_probe_inflight_hi_upward(sk, rs);
++ }
++
++ return false;
++}
++
++/* Check if it's time to probe for bandwidth now, and if so, kick it off. */
++static bool bbr_check_time_to_probe_bw(struct sock *sk,
++ const struct rate_sample *rs)
++{
++ struct bbr *bbr = inet_csk_ca(sk);
++ u32 n;
++
++ /* If we seem to be at an operating point where we are not seeing loss
++ * but we are seeing ECN marks, then when the ECN marks cease we reprobe
++ * quickly (in case cross-traffic has ceased and freed up bw).
++ */
++ if (bbr_param(sk, ecn_reprobe_gain) && bbr->ecn_eligible &&
++ bbr->ecn_in_cycle && !bbr->loss_in_cycle &&
++ inet_csk(sk)->icsk_ca_state == TCP_CA_Open) {
++ /* Calculate n so that when bbr_raise_inflight_hi_slope()
++ * computes growth_this_round as 2^n it will be roughly the
++ * desired volume of data (inflight_hi*ecn_reprobe_gain).
++ */
++ n = ilog2((((u64)bbr->inflight_hi *
++ bbr_param(sk, ecn_reprobe_gain)) >> BBR_SCALE));
++ bbr_start_bw_probe_refill(sk, n);
++ return true;
++ }
++
++ if (bbr_has_elapsed_in_phase(sk, bbr->probe_wait_us) ||
++ bbr_is_reno_coexistence_probe_time(sk)) {
++ bbr_start_bw_probe_refill(sk, 0);
++ return true;
++ }
++ return false;
++}
++
++/* Is it time to transition from PROBE_DOWN to PROBE_CRUISE? */
++static bool bbr_check_time_to_cruise(struct sock *sk, u32 inflight, u32 bw)
++{
++ /* Always need to pull inflight down to leave headroom in queue. */
++ if (inflight > bbr_inflight_with_headroom(sk))
++ return false;
++
++ return inflight <= bbr_inflight(sk, bw, BBR_UNIT);
++}
++
++/* PROBE_BW state machine: cruise, refill, probe for bw, or drain? */
++static void bbr_update_cycle_phase(struct sock *sk,
++ const struct rate_sample *rs,
++ struct bbr_context *ctx)
++{
++ struct tcp_sock *tp = tcp_sk(sk);
++ struct bbr *bbr = inet_csk_ca(sk);
++ bool is_bw_probe_done = false;
++ u32 inflight, bw;
++
++ if (!bbr_full_bw_reached(sk))
++ return;
++
++ /* In DRAIN, PROBE_BW, or PROBE_RTT, adjust upper bounds. */
++ if (bbr_adapt_upper_bounds(sk, rs, ctx))
++ return; /* already decided state transition */
++
++ if (bbr->mode != BBR_PROBE_BW)
++ return;
++
++ inflight = bbr_packets_in_net_at_edt(sk, rs->prior_in_flight);
++ bw = bbr_max_bw(sk);
++
++ switch (bbr->cycle_idx) {
++ /* First we spend most of our time cruising with a pacing_gain of 1.0,
++ * which paces at the estimated bw, to try to fully use the pipe
++ * without building queue. If we encounter loss/ECN marks, we adapt
++ * by slowing down.
++ */
++ case BBR_BW_PROBE_CRUISE:
++ if (bbr_check_time_to_probe_bw(sk, rs))
++ return; /* already decided state transition */
++ break;
++
++ /* After cruising, when it's time to probe, we first "refill": we send
++ * at the estimated bw to fill the pipe, before probing higher and
++ * knowingly risking overflowing the bottleneck buffer (causing loss).
++ */
++ case BBR_BW_PROBE_REFILL:
++ if (bbr->round_start) {
++ /* After one full round trip of sending in REFILL, we
++ * start to see bw samples reflecting our REFILL, which
++ * may be putting too much data in flight.
++ */
++ bbr->bw_probe_samples = 1;
++ bbr_start_bw_probe_up(sk, ctx);
++ }
++ break;
++
++ /* After we refill the pipe, we probe by using a pacing_gain > 1.0, to
++ * probe for bw. If we have not seen loss/ECN, we try to raise inflight
++ * to at least pacing_gain*BDP; note that this may take more than
++ * min_rtt if min_rtt is small (e.g. on a LAN).
++ *
++ * We terminate PROBE_UP bandwidth probing upon any of the following:
++ *
++ * (1) We've pushed inflight up to hit the inflight_hi target set in the
++ * most recent previous bw probe phase. Thus we want to start
++ * draining the queue immediately because it's very likely the most
++ * recently sent packets will fill the queue and cause drops.
++ * (2) If inflight_hi has not limited bandwidth growth recently, and
++ * yet delivered bandwidth has not increased much recently
++ * (bbr->full_bw_now).
++ * (3) Loss filter says loss rate is "too high".
++ * (4) ECN filter says ECN mark rate is "too high".
++ *
++ * (1) (2) checked here, (3) (4) checked in bbr_is_inflight_too_high()
++ */
++ case BBR_BW_PROBE_UP:
++ if (bbr->prev_probe_too_high &&
++ inflight >= bbr->inflight_hi) {
++ bbr->stopped_risky_probe = 1;
++ is_bw_probe_done = true;
++ } else {
++ if (tp->is_cwnd_limited &&
++ tcp_snd_cwnd(tp) >= bbr->inflight_hi) {
++ /* inflight_hi is limiting bw growth */
++ bbr_reset_full_bw(sk);
++ bbr->full_bw = ctx->sample_bw;
++ } else if (bbr->full_bw_now) {
++ /* Plateau in estimated bw. Pipe looks full. */
++ is_bw_probe_done = true;
++ }
++ }
++ if (is_bw_probe_done) {
++ bbr->prev_probe_too_high = 0; /* no loss/ECN (yet) */
++ bbr_start_bw_probe_down(sk); /* restart w/ down */
++ }
++ break;
++
++ /* After probing in PROBE_UP, we have usually accumulated some data in
++ * the bottleneck buffer (if bw probing didn't find more bw). We next
++ * enter PROBE_DOWN to try to drain any excess data from the queue. To
++ * do this, we use a pacing_gain < 1.0. We hold this pacing gain until
++ * our inflight is less then that target cruising point, which is the
++ * minimum of (a) the amount needed to leave headroom, and (b) the
++ * estimated BDP. Once inflight falls to match the target, we estimate
++ * the queue is drained; persisting would underutilize the pipe.
++ */
++ case BBR_BW_PROBE_DOWN:
++ if (bbr_check_time_to_probe_bw(sk, rs))
++ return; /* already decided state transition */
++ if (bbr_check_time_to_cruise(sk, inflight, bw))
++ bbr_start_bw_probe_cruise(sk);
++ break;
++
++ default:
++ WARN_ONCE(1, "BBR invalid cycle index %u\n", bbr->cycle_idx);
++ }
++}
++
++/* Exiting PROBE_RTT, so return to bandwidth probing in STARTUP or PROBE_BW. */
++static void bbr_exit_probe_rtt(struct sock *sk)
++{
++ struct bbr *bbr = inet_csk_ca(sk);
++
++ bbr_reset_lower_bounds(sk);
++ if (bbr_full_bw_reached(sk)) {
++ bbr->mode = BBR_PROBE_BW;
++ /* Raising inflight after PROBE_RTT may cause loss, so reset
++ * the PROBE_BW clock and schedule the next bandwidth probe for
++ * a friendly and randomized future point in time.
++ */
++ bbr_start_bw_probe_down(sk);
++ /* Since we are exiting PROBE_RTT, we know inflight is
++ * below our estimated BDP, so it is reasonable to cruise.
++ */
++ bbr_start_bw_probe_cruise(sk);
++ } else {
++ bbr->mode = BBR_STARTUP;
++ }
++}
++
++/* Exit STARTUP based on loss rate > 1% and loss gaps in round >= N. Wait until
++ * the end of the round in recovery to get a good estimate of how many packets
++ * have been lost, and how many we need to drain with a low pacing rate.
++ */
++static void bbr_check_loss_too_high_in_startup(struct sock *sk,
++ const struct rate_sample *rs)
++{
++ struct bbr *bbr = inet_csk_ca(sk);
++
++ if (bbr_full_bw_reached(sk))
++ return;
++
++ /* For STARTUP exit, check the loss rate at the end of each round trip
++ * of Recovery episodes in STARTUP. We check the loss rate at the end
++ * of the round trip to filter out noisy/low loss and have a better
++ * sense of inflight (extent of loss), so we can drain more accurately.
++ */
++ if (rs->losses && bbr->loss_events_in_round < 0xf)
++ bbr->loss_events_in_round++; /* update saturating counter */
++ if (bbr_param(sk, full_loss_cnt) && bbr->loss_round_start &&
++ inet_csk(sk)->icsk_ca_state == TCP_CA_Recovery &&
++ bbr->loss_events_in_round >= bbr_param(sk, full_loss_cnt) &&
++ bbr_is_inflight_too_high(sk, rs)) {
++ bbr_handle_queue_too_high_in_startup(sk);
++ return;
++ }
++ if (bbr->loss_round_start)
++ bbr->loss_events_in_round = 0;
++}
++
++/* Estimate when the pipe is full, using the change in delivery rate: BBR
++ * estimates bw probing filled the pipe if the estimated bw hasn't changed by
++ * at least bbr_full_bw_thresh (25%) after bbr_full_bw_cnt (3) non-app-limited
++ * rounds. Why 3 rounds: 1: rwin autotuning grows the rwin, 2: we fill the
++ * higher rwin, 3: we get higher delivery rate samples. Or transient
++ * cross-traffic or radio noise can go away. CUBIC Hystart shares a similar
++ * design goal, but uses delay and inter-ACK spacing instead of bandwidth.
++ */
++static void bbr_check_full_bw_reached(struct sock *sk,
++ const struct rate_sample *rs,
++ struct bbr_context *ctx)
++{
++ struct bbr *bbr = inet_csk_ca(sk);
++ u32 bw_thresh, full_cnt, thresh;
++
++ if (bbr->full_bw_now || rs->is_app_limited)
++ return;
++
++ thresh = bbr_param(sk, full_bw_thresh);
++ full_cnt = bbr_param(sk, full_bw_cnt);
++ bw_thresh = (u64)bbr->full_bw * thresh >> BBR_SCALE;
++ if (ctx->sample_bw >= bw_thresh) {
++ bbr_reset_full_bw(sk);
++ bbr->full_bw = ctx->sample_bw;
++ return;
++ }
++ if (!bbr->round_start)
++ return;
++ ++bbr->full_bw_cnt;
++ bbr->full_bw_now = bbr->full_bw_cnt >= full_cnt;
++ bbr->full_bw_reached |= bbr->full_bw_now;
++}
++
++/* If pipe is probably full, drain the queue and then enter steady-state. */
++static void bbr_check_drain(struct sock *sk, const struct rate_sample *rs,
++ struct bbr_context *ctx)
++{
++ struct bbr *bbr = inet_csk_ca(sk);
++
++ if (bbr->mode == BBR_STARTUP && bbr_full_bw_reached(sk)) {
++ bbr->mode = BBR_DRAIN; /* drain queue we created */
++ /* Set ssthresh to export purely for monitoring, to signal
++ * completion of initial STARTUP by setting to a non-
++ * TCP_INFINITE_SSTHRESH value (ssthresh is not used by BBR).
++ */
++ tcp_sk(sk)->snd_ssthresh =
++ bbr_inflight(sk, bbr_max_bw(sk), BBR_UNIT);
++ bbr_reset_congestion_signals(sk);
++ } /* fall through to check if in-flight is already small: */
++ if (bbr->mode == BBR_DRAIN &&
++ bbr_packets_in_net_at_edt(sk, tcp_packets_in_flight(tcp_sk(sk))) <=
++ bbr_inflight(sk, bbr_max_bw(sk), BBR_UNIT)) {
++ bbr->mode = BBR_PROBE_BW;
++ bbr_start_bw_probe_down(sk);
++ }
++}
++
++static void bbr_update_model(struct sock *sk, const struct rate_sample *rs,
++ struct bbr_context *ctx)
++{
++ bbr_update_congestion_signals(sk, rs, ctx);
++ bbr_update_ack_aggregation(sk, rs);
++ bbr_check_loss_too_high_in_startup(sk, rs);
++ bbr_check_full_bw_reached(sk, rs, ctx);
++ bbr_check_drain(sk, rs, ctx);
++ bbr_update_cycle_phase(sk, rs, ctx);
++ bbr_update_min_rtt(sk, rs);
++}
++
++/* Fast path for app-limited case.
++ *
++ * On each ack, we execute bbr state machine, which primarily consists of:
++ * 1) update model based on new rate sample, and
++ * 2) update control based on updated model or state change.
++ *
++ * There are certain workload/scenarios, e.g. app-limited case, where
++ * either we can skip updating model or we can skip update of both model
++ * as well as control. This provides signifcant softirq cpu savings for
++ * processing incoming acks.
++ *
++ * In case of app-limited, if there is no congestion (loss/ecn) and
++ * if observed bw sample is less than current estimated bw, then we can
++ * skip some of the computation in bbr state processing:
++ *
++ * - if there is no rtt/mode/phase change: In this case, since all the
++ * parameters of the network model are constant, we can skip model
++ * as well control update.
++ *
++ * - else we can skip rest of the model update. But we still need to
++ * update the control to account for the new rtt/mode/phase.
++ *
++ * Returns whether we can take fast path or not.
++ */
++static bool bbr_run_fast_path(struct sock *sk, bool *update_model,
++ const struct rate_sample *rs, struct bbr_context *ctx)
++{
++ struct bbr *bbr = inet_csk_ca(sk);
++ u32 prev_min_rtt_us, prev_mode;
++
++ if (bbr_param(sk, fast_path) && bbr->try_fast_path &&
++ rs->is_app_limited && ctx->sample_bw < bbr_max_bw(sk) &&
++ !bbr->loss_in_round && !bbr->ecn_in_round ) {
++ prev_mode = bbr->mode;
++ prev_min_rtt_us = bbr->min_rtt_us;
++ bbr_check_drain(sk, rs, ctx);
++ bbr_update_cycle_phase(sk, rs, ctx);
++ bbr_update_min_rtt(sk, rs);
++
++ if (bbr->mode == prev_mode &&
++ bbr->min_rtt_us == prev_min_rtt_us &&
++ bbr->try_fast_path) {
++ return true;
++ }
++
++ /* Skip model update, but control still needs to be updated */
++ *update_model = false;
++ }
++ return false;
++}
++
++__bpf_kfunc static void bbr_main(struct sock *sk, const struct rate_sample *rs)
++{
++ struct tcp_sock *tp = tcp_sk(sk);
++ struct bbr *bbr = inet_csk_ca(sk);
++ struct bbr_context ctx = { 0 };
++ bool update_model = true;
++ u32 bw, round_delivered;
++ int ce_ratio = -1;
++
++ round_delivered = bbr_update_round_start(sk, rs, &ctx);
++ if (bbr->round_start) {
++ bbr->rounds_since_probe =
++ min_t(s32, bbr->rounds_since_probe + 1, 0xFF);
++ ce_ratio = bbr_update_ecn_alpha(sk);
++ }
++ bbr_plb(sk, rs, ce_ratio);
++
++ bbr->ecn_in_round |= (bbr->ecn_eligible && rs->is_ece);
++ bbr_calculate_bw_sample(sk, rs, &ctx);
++ bbr_update_latest_delivery_signals(sk, rs, &ctx);
++
++ if (bbr_run_fast_path(sk, &update_model, rs, &ctx))
++ goto out;
++
++ if (update_model)
++ bbr_update_model(sk, rs, &ctx);
++
++ bbr_update_gains(sk);
++ bw = bbr_bw(sk);
++ bbr_set_pacing_rate(sk, bw, bbr->pacing_gain);
++ bbr_set_cwnd(sk, rs, rs->acked_sacked, bw, bbr->cwnd_gain,
++ tcp_snd_cwnd(tp), &ctx);
++ bbr_bound_cwnd_for_inflight_model(sk);
++
++out:
++ bbr_advance_latest_delivery_signals(sk, rs, &ctx);
++ bbr->prev_ca_state = inet_csk(sk)->icsk_ca_state;
++ bbr->loss_in_cycle |= rs->lost > 0;
++ bbr->ecn_in_cycle |= rs->delivered_ce > 0;
++}
++
++__bpf_kfunc static void bbr_init(struct sock *sk)
++{
++ struct tcp_sock *tp = tcp_sk(sk);
++ struct bbr *bbr = inet_csk_ca(sk);
++
++ bbr->initialized = 1;
++
++ bbr->init_cwnd = min(0x7FU, tcp_snd_cwnd(tp));
++ bbr->prior_cwnd = tp->prior_cwnd;
++ tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
++ bbr->next_rtt_delivered = tp->delivered;
++ bbr->prev_ca_state = TCP_CA_Open;
++
++ bbr->probe_rtt_done_stamp = 0;
++ bbr->probe_rtt_round_done = 0;
++ bbr->probe_rtt_min_us = tcp_min_rtt(tp);
++ bbr->probe_rtt_min_stamp = tcp_jiffies32;
++ bbr->min_rtt_us = tcp_min_rtt(tp);
++ bbr->min_rtt_stamp = tcp_jiffies32;
++
++ bbr->has_seen_rtt = 0;
++ bbr_init_pacing_rate_from_rtt(sk);
++
++ bbr->round_start = 0;
++ bbr->idle_restart = 0;
++ bbr->full_bw_reached = 0;
++ bbr->full_bw = 0;
+ bbr->full_bw_cnt = 0;
+- bbr_reset_lt_bw_sampling(sk);
+- return tcp_snd_cwnd(tcp_sk(sk));
++ bbr->cycle_mstamp = 0;
++ bbr->cycle_idx = 0;
++
++ bbr_reset_startup_mode(sk);
++
++ bbr->ack_epoch_mstamp = tp->tcp_mstamp;
++ bbr->ack_epoch_acked = 0;
++ bbr->extra_acked_win_rtts = 0;
++ bbr->extra_acked_win_idx = 0;
++ bbr->extra_acked[0] = 0;
++ bbr->extra_acked[1] = 0;
++
++ bbr->ce_state = 0;
++ bbr->prior_rcv_nxt = tp->rcv_nxt;
++ bbr->try_fast_path = 0;
++
++ cmpxchg(&sk->sk_pacing_status, SK_PACING_NONE, SK_PACING_NEEDED);
++
++ /* Start sampling ECN mark rate after first full flight is ACKed: */
++ bbr->loss_round_delivered = tp->delivered + 1;
++ bbr->loss_round_start = 0;
++ bbr->undo_bw_lo = 0;
++ bbr->undo_inflight_lo = 0;
++ bbr->undo_inflight_hi = 0;
++ bbr->loss_events_in_round = 0;
++ bbr->startup_ecn_rounds = 0;
++ bbr_reset_congestion_signals(sk);
++ bbr->bw_lo = ~0U;
++ bbr->bw_hi[0] = 0;
++ bbr->bw_hi[1] = 0;
++ bbr->inflight_lo = ~0U;
++ bbr->inflight_hi = ~0U;
++ bbr_reset_full_bw(sk);
++ bbr->bw_probe_up_cnt = ~0U;
++ bbr->bw_probe_up_acks = 0;
++ bbr->bw_probe_up_rounds = 0;
++ bbr->probe_wait_us = 0;
++ bbr->stopped_risky_probe = 0;
++ bbr->ack_phase = BBR_ACKS_INIT;
++ bbr->rounds_since_probe = 0;
++ bbr->bw_probe_samples = 0;
++ bbr->prev_probe_too_high = 0;
++ bbr->ecn_eligible = 0;
++ bbr->ecn_alpha = bbr_param(sk, ecn_alpha_init);
++ bbr->alpha_last_delivered = 0;
++ bbr->alpha_last_delivered_ce = 0;
++ bbr->plb.pause_until = 0;
++
++ tp->fast_ack_mode = bbr_fast_ack_mode ? 1 : 0;
++
++ if (bbr_can_use_ecn(sk))
++ tp->ecn_flags |= TCP_ECN_ECT_PERMANENT;
++}
++
++/* BBR marks the current round trip as a loss round. */
++static void bbr_note_loss(struct sock *sk)
++{
++ struct tcp_sock *tp = tcp_sk(sk);
++ struct bbr *bbr = inet_csk_ca(sk);
++
++ /* Capture "current" data over the full round trip of loss, to
++ * have a better chance of observing the full capacity of the path.
++ */
++ if (!bbr->loss_in_round) /* first loss in this round trip? */
++ bbr->loss_round_delivered = tp->delivered; /* set round trip */
++ bbr->loss_in_round = 1;
++ bbr->loss_in_cycle = 1;
+ }
+
+-/* Entering loss recovery, so save cwnd for when we exit or undo recovery. */
++/* Core TCP stack informs us that the given skb was just marked lost. */
++__bpf_kfunc static void bbr_skb_marked_lost(struct sock *sk,
++ const struct sk_buff *skb)
++{
++ struct tcp_sock *tp = tcp_sk(sk);
++ struct bbr *bbr = inet_csk_ca(sk);
++ struct tcp_skb_cb *scb = TCP_SKB_CB(skb);
++ struct rate_sample rs = {};
++
++ bbr_note_loss(sk);
++
++ if (!bbr->bw_probe_samples)
++ return; /* not an skb sent while probing for bandwidth */
++ if (unlikely(!scb->tx.delivered_mstamp))
++ return; /* skb was SACKed, reneged, marked lost; ignore it */
++ /* We are probing for bandwidth. Construct a rate sample that
++ * estimates what happened in the flight leading up to this lost skb,
++ * then see if the loss rate went too high, and if so at which packet.
++ */
++ rs.tx_in_flight = scb->tx.in_flight;
++ rs.lost = tp->lost - scb->tx.lost;
++ rs.is_app_limited = scb->tx.is_app_limited;
++ if (bbr_is_inflight_too_high(sk, &rs)) {
++ rs.tx_in_flight = bbr_inflight_hi_from_lost_skb(sk, &rs, skb);
++ bbr_handle_inflight_too_high(sk, &rs);
++ }
++}
++
++static void bbr_run_loss_probe_recovery(struct sock *sk)
++{
++ struct tcp_sock *tp = tcp_sk(sk);
++ struct bbr *bbr = inet_csk_ca(sk);
++ struct rate_sample rs = {0};
++
++ bbr_note_loss(sk);
++
++ if (!bbr->bw_probe_samples)
++ return; /* not sent while probing for bandwidth */
++ /* We are probing for bandwidth. Construct a rate sample that
++ * estimates what happened in the flight leading up to this
++ * loss, then see if the loss rate went too high.
++ */
++ rs.lost = 1; /* TLP probe repaired loss of a single segment */
++ rs.tx_in_flight = bbr->inflight_latest + rs.lost;
++ rs.is_app_limited = tp->tlp_orig_data_app_limited;
++ if (bbr_is_inflight_too_high(sk, &rs))
++ bbr_handle_inflight_too_high(sk, &rs);
++}
++
++/* Revert short-term model if current loss recovery event was spurious. */
++__bpf_kfunc static u32 bbr_undo_cwnd(struct sock *sk)
++{
++ struct bbr *bbr = inet_csk_ca(sk);
++
++ bbr_reset_full_bw(sk); /* spurious slow-down; reset full bw detector */
++ bbr->loss_in_round = 0;
++
++ /* Revert to cwnd and other state saved before loss episode. */
++ bbr->bw_lo = max(bbr->bw_lo, bbr->undo_bw_lo);
++ bbr->inflight_lo = max(bbr->inflight_lo, bbr->undo_inflight_lo);
++ bbr->inflight_hi = max(bbr->inflight_hi, bbr->undo_inflight_hi);
++ bbr->try_fast_path = 0; /* take slow path to set proper cwnd, pacing */
++ return bbr->prior_cwnd;
++}
++
++/* Entering loss recovery, so save state for when we undo recovery. */
+ __bpf_kfunc static u32 bbr_ssthresh(struct sock *sk)
+ {
++ struct bbr *bbr = inet_csk_ca(sk);
++
+ bbr_save_cwnd(sk);
++ /* For undo, save state that adapts based on loss signal. */
++ bbr->undo_bw_lo = bbr->bw_lo;
++ bbr->undo_inflight_lo = bbr->inflight_lo;
++ bbr->undo_inflight_hi = bbr->inflight_hi;
+ return tcp_sk(sk)->snd_ssthresh;
+ }
+
++static enum tcp_bbr_phase bbr_get_phase(struct bbr *bbr)
++{
++ switch (bbr->mode) {
++ case BBR_STARTUP:
++ return BBR_PHASE_STARTUP;
++ case BBR_DRAIN:
++ return BBR_PHASE_DRAIN;
++ case BBR_PROBE_BW:
++ break;
++ case BBR_PROBE_RTT:
++ return BBR_PHASE_PROBE_RTT;
++ default:
++ return BBR_PHASE_INVALID;
++ }
++ switch (bbr->cycle_idx) {
++ case BBR_BW_PROBE_UP:
++ return BBR_PHASE_PROBE_BW_UP;
++ case BBR_BW_PROBE_DOWN:
++ return BBR_PHASE_PROBE_BW_DOWN;
++ case BBR_BW_PROBE_CRUISE:
++ return BBR_PHASE_PROBE_BW_CRUISE;
++ case BBR_BW_PROBE_REFILL:
++ return BBR_PHASE_PROBE_BW_REFILL;
++ default:
++ return BBR_PHASE_INVALID;
++ }
++}
++
+ static size_t bbr_get_info(struct sock *sk, u32 ext, int *attr,
+- union tcp_cc_info *info)
++ union tcp_cc_info *info)
+ {
+ if (ext & (1 << (INET_DIAG_BBRINFO - 1)) ||
+ ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
+- struct tcp_sock *tp = tcp_sk(sk);
+ struct bbr *bbr = inet_csk_ca(sk);
+- u64 bw = bbr_bw(sk);
+-
+- bw = bw * tp->mss_cache * USEC_PER_SEC >> BW_SCALE;
+- memset(&info->bbr, 0, sizeof(info->bbr));
+- info->bbr.bbr_bw_lo = (u32)bw;
+- info->bbr.bbr_bw_hi = (u32)(bw >> 32);
+- info->bbr.bbr_min_rtt = bbr->min_rtt_us;
+- info->bbr.bbr_pacing_gain = bbr->pacing_gain;
+- info->bbr.bbr_cwnd_gain = bbr->cwnd_gain;
++ u64 bw = bbr_bw_bytes_per_sec(sk, bbr_bw(sk));
++ u64 bw_hi = bbr_bw_bytes_per_sec(sk, bbr_max_bw(sk));
++ u64 bw_lo = bbr->bw_lo == ~0U ?
++ ~0ULL : bbr_bw_bytes_per_sec(sk, bbr->bw_lo);
++ struct tcp_bbr_info *bbr_info = &info->bbr;
++
++ memset(bbr_info, 0, sizeof(*bbr_info));
++ bbr_info->bbr_bw_lo = (u32)bw;
++ bbr_info->bbr_bw_hi = (u32)(bw >> 32);
++ bbr_info->bbr_min_rtt = bbr->min_rtt_us;
++ bbr_info->bbr_pacing_gain = bbr->pacing_gain;
++ bbr_info->bbr_cwnd_gain = bbr->cwnd_gain;
++ bbr_info->bbr_bw_hi_lsb = (u32)bw_hi;
++ bbr_info->bbr_bw_hi_msb = (u32)(bw_hi >> 32);
++ bbr_info->bbr_bw_lo_lsb = (u32)bw_lo;
++ bbr_info->bbr_bw_lo_msb = (u32)(bw_lo >> 32);
++ bbr_info->bbr_mode = bbr->mode;
++ bbr_info->bbr_phase = (__u8)bbr_get_phase(bbr);
++ bbr_info->bbr_version = (__u8)BBR_VERSION;
++ bbr_info->bbr_inflight_lo = bbr->inflight_lo;
++ bbr_info->bbr_inflight_hi = bbr->inflight_hi;
++ bbr_info->bbr_extra_acked = bbr_extra_acked(sk);
+ *attr = INET_DIAG_BBRINFO;
+- return sizeof(info->bbr);
++ return sizeof(*bbr_info);
+ }
+ return 0;
+ }
+
+ __bpf_kfunc static void bbr_set_state(struct sock *sk, u8 new_state)
+ {
++ struct tcp_sock *tp = tcp_sk(sk);
+ struct bbr *bbr = inet_csk_ca(sk);
+
+ if (new_state == TCP_CA_Loss) {
+- struct rate_sample rs = { .losses = 1 };
+
+ bbr->prev_ca_state = TCP_CA_Loss;
+- bbr->full_bw = 0;
+- bbr->round_start = 1; /* treat RTO like end of a round */
+- bbr_lt_bw_sampling(sk, &rs);
++ tcp_plb_update_state_upon_rto(sk, &bbr->plb);
++ /* The tcp_write_timeout() call to sk_rethink_txhash() likely
++ * repathed this flow, so re-learn the min network RTT on the
++ * new path:
++ */
++ bbr_reset_full_bw(sk);
++ if (!bbr_is_probing_bandwidth(sk) && bbr->inflight_lo == ~0U) {
++ /* bbr_adapt_lower_bounds() needs cwnd before
++ * we suffered an RTO, to update inflight_lo:
++ */
++ bbr->inflight_lo =
++ max(tcp_snd_cwnd(tp), bbr->prior_cwnd);
++ }
++ } else if (bbr->prev_ca_state == TCP_CA_Loss &&
++ new_state != TCP_CA_Loss) {
++ bbr_exit_loss_recovery(sk);
+ }
+ }
+
++
+ static struct tcp_congestion_ops tcp_bbr_cong_ops __read_mostly = {
+- .flags = TCP_CONG_NON_RESTRICTED,
++ .flags = TCP_CONG_NON_RESTRICTED | TCP_CONG_WANTS_CE_EVENTS,
+ .name = "bbr",
+ .owner = THIS_MODULE,
+ .init = bbr_init,
+ .cong_control = bbr_main,
+ .sndbuf_expand = bbr_sndbuf_expand,
++ .skb_marked_lost = bbr_skb_marked_lost,
+ .undo_cwnd = bbr_undo_cwnd,
+ .cwnd_event = bbr_cwnd_event,
+ .ssthresh = bbr_ssthresh,
+- .min_tso_segs = bbr_min_tso_segs,
++ .tso_segs = bbr_tso_segs,
+ .get_info = bbr_get_info,
+ .set_state = bbr_set_state,
+ };
+@@ -1161,10 +2361,11 @@ BTF_KFUNCS_START(tcp_bbr_check_kfunc_ids)
+ BTF_ID_FLAGS(func, bbr_init)
+ BTF_ID_FLAGS(func, bbr_main)
+ BTF_ID_FLAGS(func, bbr_sndbuf_expand)
++BTF_ID_FLAGS(func, bbr_skb_marked_lost)
+ BTF_ID_FLAGS(func, bbr_undo_cwnd)
+ BTF_ID_FLAGS(func, bbr_cwnd_event)
+ BTF_ID_FLAGS(func, bbr_ssthresh)
+-BTF_ID_FLAGS(func, bbr_min_tso_segs)
++BTF_ID_FLAGS(func, bbr_tso_segs)
+ BTF_ID_FLAGS(func, bbr_set_state)
+ #endif
+ #endif
+@@ -1199,5 +2400,12 @@ MODULE_AUTHOR("Van Jacobson <vanj@google.com>");
+ MODULE_AUTHOR("Neal Cardwell <ncardwell@google.com>");
+ MODULE_AUTHOR("Yuchung Cheng <ycheng@google.com>");
+ MODULE_AUTHOR("Soheil Hassas Yeganeh <soheil@google.com>");
++MODULE_AUTHOR("Priyaranjan Jha <priyarjha@google.com>");
++MODULE_AUTHOR("Yousuk Seung <ysseung@google.com>");
++MODULE_AUTHOR("Kevin Yang <yyd@google.com>");
++MODULE_AUTHOR("Arjun Roy <arjunroy@google.com>");
++MODULE_AUTHOR("David Morley <morleyd@google.com>");
++
+ MODULE_LICENSE("Dual BSD/GPL");
+ MODULE_DESCRIPTION("TCP BBR (Bottleneck Bandwidth and RTT)");
++MODULE_VERSION(__stringify(BBR_VERSION));
+diff --git a/net/ipv4/tcp_cong.c b/net/ipv4/tcp_cong.c
+index 28ffcfbeef14..7b13915ba288 100644
+--- a/net/ipv4/tcp_cong.c
++++ b/net/ipv4/tcp_cong.c
+@@ -237,6 +237,7 @@ void tcp_init_congestion_control(struct sock *sk)
+ struct inet_connection_sock *icsk = inet_csk(sk);
+
+ tcp_sk(sk)->prior_ssthresh = 0;
++ tcp_sk(sk)->fast_ack_mode = 0;
+ if (icsk->icsk_ca_ops->init)
+ icsk->icsk_ca_ops->init(sk);
+ if (tcp_ca_needs_ecn(sk))
+diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
+index 7b692bcb61d4..650db0ec7222 100644
+--- a/net/ipv4/tcp_input.c
++++ b/net/ipv4/tcp_input.c
+@@ -364,7 +364,7 @@ static void __tcp_ecn_check_ce(struct sock *sk, const struct sk_buff *skb)
+ tcp_enter_quickack_mode(sk, 2);
+ break;
+ case INET_ECN_CE:
+- if (tcp_ca_needs_ecn(sk))
++ if (tcp_ca_wants_ce_events(sk))
+ tcp_ca_event(sk, CA_EVENT_ECN_IS_CE);
+
+ if (!(tp->ecn_flags & TCP_ECN_DEMAND_CWR)) {
+@@ -375,7 +375,7 @@ static void __tcp_ecn_check_ce(struct sock *sk, const struct sk_buff *skb)
+ tp->ecn_flags |= TCP_ECN_SEEN;
+ break;
+ default:
+- if (tcp_ca_needs_ecn(sk))
++ if (tcp_ca_wants_ce_events(sk))
+ tcp_ca_event(sk, CA_EVENT_ECN_NO_CE);
+ tp->ecn_flags |= TCP_ECN_SEEN;
+ break;
+@@ -1112,7 +1112,12 @@ static void tcp_verify_retransmit_hint(struct tcp_sock *tp, struct sk_buff *skb)
+ */
+ static void tcp_notify_skb_loss_event(struct tcp_sock *tp, const struct sk_buff *skb)
+ {
++ struct sock *sk = (struct sock *)tp;
++ const struct tcp_congestion_ops *ca_ops = inet_csk(sk)->icsk_ca_ops;
++
+ tp->lost += tcp_skb_pcount(skb);
++ if (ca_ops->skb_marked_lost)
++ ca_ops->skb_marked_lost(sk, skb);
+ }
+
+ void tcp_mark_skb_lost(struct sock *sk, struct sk_buff *skb)
+@@ -1493,6 +1498,17 @@ static bool tcp_shifted_skb(struct sock *sk, struct sk_buff *prev,
+ WARN_ON_ONCE(tcp_skb_pcount(skb) < pcount);
+ tcp_skb_pcount_add(skb, -pcount);
+
++ /* Adjust tx.in_flight as pcount is shifted from skb to prev. */
++ if (WARN_ONCE(TCP_SKB_CB(skb)->tx.in_flight < pcount,
++ "prev in_flight: %u skb in_flight: %u pcount: %u",
++ TCP_SKB_CB(prev)->tx.in_flight,
++ TCP_SKB_CB(skb)->tx.in_flight,
++ pcount))
++ TCP_SKB_CB(skb)->tx.in_flight = 0;
++ else
++ TCP_SKB_CB(skb)->tx.in_flight -= pcount;
++ TCP_SKB_CB(prev)->tx.in_flight += pcount;
++
+ /* When we're adding to gso_segs == 1, gso_size will be zero,
+ * in theory this shouldn't be necessary but as long as DSACK
+ * code can come after this skb later on it's better to keep
+@@ -3778,7 +3794,8 @@ static void tcp_replace_ts_recent(struct tcp_sock *tp, u32 seq)
+ /* This routine deals with acks during a TLP episode and ends an episode by
+ * resetting tlp_high_seq. Ref: TLP algorithm in draft-ietf-tcpm-rack
+ */
+-static void tcp_process_tlp_ack(struct sock *sk, u32 ack, int flag)
++static void tcp_process_tlp_ack(struct sock *sk, u32 ack, int flag,
++ struct rate_sample *rs)
+ {
+ struct tcp_sock *tp = tcp_sk(sk);
+
+@@ -3795,6 +3812,7 @@ static void tcp_process_tlp_ack(struct sock *sk, u32 ack, int flag)
+ /* ACK advances: there was a loss, so reduce cwnd. Reset
+ * tlp_high_seq in tcp_init_cwnd_reduction()
+ */
++ tcp_ca_event(sk, CA_EVENT_TLP_RECOVERY);
+ tcp_init_cwnd_reduction(sk);
+ tcp_set_ca_state(sk, TCP_CA_CWR);
+ tcp_end_cwnd_reduction(sk);
+@@ -3805,6 +3823,11 @@ static void tcp_process_tlp_ack(struct sock *sk, u32 ack, int flag)
+ FLAG_NOT_DUP | FLAG_DATA_SACKED))) {
+ /* Pure dupack: original and TLP probe arrived; no loss */
+ tp->tlp_high_seq = 0;
++ } else {
++ /* This ACK matches a TLP retransmit. We cannot yet tell if
++ * this ACK is for the original or the TLP retransmit.
++ */
++ rs->is_acking_tlp_retrans_seq = 1;
+ }
+ }
+
+@@ -3913,6 +3936,7 @@ static int tcp_ack(struct sock *sk, const struct sk_buff *skb, int flag)
+
+ prior_fack = tcp_is_sack(tp) ? tcp_highest_sack_seq(tp) : tp->snd_una;
+ rs.prior_in_flight = tcp_packets_in_flight(tp);
++ tcp_rate_check_app_limited(sk);
+
+ /* ts_recent update must be made after we are sure that the packet
+ * is in window.
+@@ -3987,7 +4011,7 @@ static int tcp_ack(struct sock *sk, const struct sk_buff *skb, int flag)
+ tcp_rack_update_reo_wnd(sk, &rs);
+
+ if (tp->tlp_high_seq)
+- tcp_process_tlp_ack(sk, ack, flag);
++ tcp_process_tlp_ack(sk, ack, flag, &rs);
+
+ if (tcp_ack_is_dubious(sk, flag)) {
+ if (!(flag & (FLAG_SND_UNA_ADVANCED |
+@@ -4011,6 +4035,7 @@ static int tcp_ack(struct sock *sk, const struct sk_buff *skb, int flag)
+ delivered = tcp_newly_delivered(sk, delivered, flag);
+ lost = tp->lost - lost; /* freshly marked lost */
+ rs.is_ack_delayed = !!(flag & FLAG_ACK_MAYBE_DELAYED);
++ rs.is_ece = !!(flag & FLAG_ECE);
+ tcp_rate_gen(sk, delivered, lost, is_sack_reneg, sack_state.rate);
+ tcp_cong_control(sk, ack, delivered, flag, sack_state.rate);
+ tcp_xmit_recovery(sk, rexmit);
+@@ -4030,7 +4055,7 @@ static int tcp_ack(struct sock *sk, const struct sk_buff *skb, int flag)
+ tcp_ack_probe(sk);
+
+ if (tp->tlp_high_seq)
+- tcp_process_tlp_ack(sk, ack, flag);
++ tcp_process_tlp_ack(sk, ack, flag, &rs);
+ return 1;
+
+ old_ack:
+@@ -5688,13 +5713,14 @@ static void __tcp_ack_snd_check(struct sock *sk, int ofo_possible)
+
+ /* More than one full frame received... */
+ if (((tp->rcv_nxt - tp->rcv_wup) > inet_csk(sk)->icsk_ack.rcv_mss &&
++ (tp->fast_ack_mode == 1 ||
+ /* ... and right edge of window advances far enough.
+ * (tcp_recvmsg() will send ACK otherwise).
+ * If application uses SO_RCVLOWAT, we want send ack now if
+ * we have not received enough bytes to satisfy the condition.
+ */
+- (tp->rcv_nxt - tp->copied_seq < sk->sk_rcvlowat ||
+- __tcp_select_window(sk) >= tp->rcv_wnd)) ||
++ (tp->rcv_nxt - tp->copied_seq < sk->sk_rcvlowat ||
++ __tcp_select_window(sk) >= tp->rcv_wnd))) ||
+ /* We ACK each frame or... */
+ tcp_in_quickack_mode(sk) ||
+ /* Protocol state mandates a one-time immediate ACK */
+diff --git a/net/ipv4/tcp_minisocks.c b/net/ipv4/tcp_minisocks.c
+index f0761f060a83..90a9389499e6 100644
+--- a/net/ipv4/tcp_minisocks.c
++++ b/net/ipv4/tcp_minisocks.c
+@@ -460,6 +460,8 @@ void tcp_ca_openreq_child(struct sock *sk, const struct dst_entry *dst)
+ u32 ca_key = dst_metric(dst, RTAX_CC_ALGO);
+ bool ca_got_dst = false;
+
++ tcp_set_ecn_low_from_dst(sk, dst);
++
+ if (ca_key != TCP_CA_UNSPEC) {
+ const struct tcp_congestion_ops *ca;
+
+diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c
+index 02caeb7bcf63..0e445bc5cc60 100644
+--- a/net/ipv4/tcp_output.c
++++ b/net/ipv4/tcp_output.c
+@@ -332,10 +332,9 @@ static void tcp_ecn_send_syn(struct sock *sk, struct sk_buff *skb)
+ bool bpf_needs_ecn = tcp_bpf_ca_needs_ecn(sk);
+ bool use_ecn = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_ecn) == 1 ||
+ tcp_ca_needs_ecn(sk) || bpf_needs_ecn;
++ const struct dst_entry *dst = __sk_dst_get(sk);
+
+ if (!use_ecn) {
+- const struct dst_entry *dst = __sk_dst_get(sk);
+-
+ if (dst && dst_feature(dst, RTAX_FEATURE_ECN))
+ use_ecn = true;
+ }
+@@ -347,6 +346,9 @@ static void tcp_ecn_send_syn(struct sock *sk, struct sk_buff *skb)
+ tp->ecn_flags = TCP_ECN_OK;
+ if (tcp_ca_needs_ecn(sk) || bpf_needs_ecn)
+ INET_ECN_xmit(sk);
++
++ if (dst)
++ tcp_set_ecn_low_from_dst(sk, dst);
+ }
+ }
+
+@@ -384,7 +386,8 @@ static void tcp_ecn_send(struct sock *sk, struct sk_buff *skb,
+ th->cwr = 1;
+ skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
+ }
+- } else if (!tcp_ca_needs_ecn(sk)) {
++ } else if (!(tp->ecn_flags & TCP_ECN_ECT_PERMANENT) &&
++ !tcp_ca_needs_ecn(sk)) {
+ /* ACK or retransmitted segment: clear ECT|CE */
+ INET_ECN_dontxmit(sk);
+ }
+@@ -1593,7 +1596,7 @@ int tcp_fragment(struct sock *sk, enum tcp_queue tcp_queue,
+ {
+ struct tcp_sock *tp = tcp_sk(sk);
+ struct sk_buff *buff;
+- int old_factor;
++ int old_factor, inflight_prev;
+ long limit;
+ int nlen;
+ u8 flags;
+@@ -1668,6 +1671,30 @@ int tcp_fragment(struct sock *sk, enum tcp_queue tcp_queue,
+
+ if (diff)
+ tcp_adjust_pcount(sk, skb, diff);
++
++ inflight_prev = TCP_SKB_CB(skb)->tx.in_flight - old_factor;
++ if (inflight_prev < 0) {
++ WARN_ONCE(tcp_skb_tx_in_flight_is_suspicious(
++ old_factor,
++ TCP_SKB_CB(skb)->sacked,
++ TCP_SKB_CB(skb)->tx.in_flight),
++ "inconsistent: tx.in_flight: %u "
++ "old_factor: %d mss: %u sacked: %u "
++ "1st pcount: %d 2nd pcount: %d "
++ "1st len: %u 2nd len: %u ",
++ TCP_SKB_CB(skb)->tx.in_flight, old_factor,
++ mss_now, TCP_SKB_CB(skb)->sacked,
++ tcp_skb_pcount(skb), tcp_skb_pcount(buff),
++ skb->len, buff->len);
++ inflight_prev = 0;
++ }
++ /* Set 1st tx.in_flight as if 1st were sent by itself: */
++ TCP_SKB_CB(skb)->tx.in_flight = inflight_prev +
++ tcp_skb_pcount(skb);
++ /* Set 2nd tx.in_flight with new 1st and 2nd pcounts: */
++ TCP_SKB_CB(buff)->tx.in_flight = inflight_prev +
++ tcp_skb_pcount(skb) +
++ tcp_skb_pcount(buff);
+ }
+
+ /* Link BUFF into the send queue. */
+@@ -2025,13 +2052,12 @@ static u32 tcp_tso_autosize(const struct sock *sk, unsigned int mss_now,
+ static u32 tcp_tso_segs(struct sock *sk, unsigned int mss_now)
+ {
+ const struct tcp_congestion_ops *ca_ops = inet_csk(sk)->icsk_ca_ops;
+- u32 min_tso, tso_segs;
+-
+- min_tso = ca_ops->min_tso_segs ?
+- ca_ops->min_tso_segs(sk) :
+- READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_min_tso_segs);
++ u32 tso_segs;
+
+- tso_segs = tcp_tso_autosize(sk, mss_now, min_tso);
++ tso_segs = ca_ops->tso_segs ?
++ ca_ops->tso_segs(sk, mss_now) :
++ tcp_tso_autosize(sk, mss_now,
++ sock_net(sk)->ipv4.sysctl_tcp_min_tso_segs);
+ return min_t(u32, tso_segs, sk->sk_gso_max_segs);
+ }
+
+@@ -2731,6 +2757,7 @@ static bool tcp_write_xmit(struct sock *sk, unsigned int mss_now, int nonagle,
+ skb_set_delivery_time(skb, tp->tcp_wstamp_ns, true);
+ list_move_tail(&skb->tcp_tsorted_anchor, &tp->tsorted_sent_queue);
+ tcp_init_tso_segs(skb, mss_now);
++ tcp_set_tx_in_flight(sk, skb);
+ goto repair; /* Skip network transmission */
+ }
+
+@@ -2944,6 +2971,7 @@ void tcp_send_loss_probe(struct sock *sk)
+ if (WARN_ON(!skb || !tcp_skb_pcount(skb)))
+ goto rearm_timer;
+
++ tp->tlp_orig_data_app_limited = TCP_SKB_CB(skb)->tx.is_app_limited;
+ if (__tcp_retransmit_skb(sk, skb, 1))
+ goto rearm_timer;
+
+diff --git a/net/ipv4/tcp_rate.c b/net/ipv4/tcp_rate.c
+index a8f6d9d06f2e..8737f2134648 100644
+--- a/net/ipv4/tcp_rate.c
++++ b/net/ipv4/tcp_rate.c
+@@ -34,6 +34,24 @@
+ * ready to send in the write queue.
+ */
+
++void tcp_set_tx_in_flight(struct sock *sk, struct sk_buff *skb)
++{
++ struct tcp_sock *tp = tcp_sk(sk);
++ u32 in_flight;
++
++ /* Check, sanitize, and record packets in flight after skb was sent. */
++ in_flight = tcp_packets_in_flight(tp) + tcp_skb_pcount(skb);
++ if (WARN_ONCE(in_flight > TCPCB_IN_FLIGHT_MAX,
++ "insane in_flight %u cc %s mss %u "
++ "cwnd %u pif %u %u %u %u\n",
++ in_flight, inet_csk(sk)->icsk_ca_ops->name,
++ tp->mss_cache, tp->snd_cwnd,
++ tp->packets_out, tp->retrans_out,
++ tp->sacked_out, tp->lost_out))
++ in_flight = TCPCB_IN_FLIGHT_MAX;
++ TCP_SKB_CB(skb)->tx.in_flight = in_flight;
++}
++
+ /* Snapshot the current delivery information in the skb, to generate
+ * a rate sample later when the skb is (s)acked in tcp_rate_skb_delivered().
+ */
+@@ -66,7 +84,9 @@ void tcp_rate_skb_sent(struct sock *sk, struct sk_buff *skb)
+ TCP_SKB_CB(skb)->tx.delivered_mstamp = tp->delivered_mstamp;
+ TCP_SKB_CB(skb)->tx.delivered = tp->delivered;
+ TCP_SKB_CB(skb)->tx.delivered_ce = tp->delivered_ce;
++ TCP_SKB_CB(skb)->tx.lost = tp->lost;
+ TCP_SKB_CB(skb)->tx.is_app_limited = tp->app_limited ? 1 : 0;
++ tcp_set_tx_in_flight(sk, skb);
+ }
+
+ /* When an skb is sacked or acked, we fill in the rate sample with the (prior)
+@@ -91,18 +111,21 @@ void tcp_rate_skb_delivered(struct sock *sk, struct sk_buff *skb,
+ if (!rs->prior_delivered ||
+ tcp_skb_sent_after(tx_tstamp, tp->first_tx_mstamp,
+ scb->end_seq, rs->last_end_seq)) {
++ rs->prior_lost = scb->tx.lost;
+ rs->prior_delivered_ce = scb->tx.delivered_ce;
+ rs->prior_delivered = scb->tx.delivered;
+ rs->prior_mstamp = scb->tx.delivered_mstamp;
+ rs->is_app_limited = scb->tx.is_app_limited;
+ rs->is_retrans = scb->sacked & TCPCB_RETRANS;
++ rs->tx_in_flight = scb->tx.in_flight;
+ rs->last_end_seq = scb->end_seq;
+
+ /* Record send time of most recently ACKed packet: */
+ tp->first_tx_mstamp = tx_tstamp;
+ /* Find the duration of the "send phase" of this window: */
+- rs->interval_us = tcp_stamp_us_delta(tp->first_tx_mstamp,
+- scb->tx.first_tx_mstamp);
++ rs->interval_us = tcp_stamp32_us_delta(
++ tp->first_tx_mstamp,
++ scb->tx.first_tx_mstamp);
+
+ }
+ /* Mark off the skb delivered once it's sacked to avoid being
+@@ -144,6 +167,7 @@ void tcp_rate_gen(struct sock *sk, u32 delivered, u32 lost,
+ return;
+ }
+ rs->delivered = tp->delivered - rs->prior_delivered;
++ rs->lost = tp->lost - rs->prior_lost;
+
+ rs->delivered_ce = tp->delivered_ce - rs->prior_delivered_ce;
+ /* delivered_ce occupies less than 32 bits in the skb control block */
+@@ -155,7 +179,7 @@ void tcp_rate_gen(struct sock *sk, u32 delivered, u32 lost,
+ * longer phase.
+ */
+ snd_us = rs->interval_us; /* send phase */
+- ack_us = tcp_stamp_us_delta(tp->tcp_mstamp,
++ ack_us = tcp_stamp32_us_delta(tp->tcp_mstamp,
+ rs->prior_mstamp); /* ack phase */
+ rs->interval_us = max(snd_us, ack_us);
+
+diff --git a/net/ipv4/tcp_timer.c b/net/ipv4/tcp_timer.c
+index f96f68cf7961..af0ce59fbbc0 100644
+--- a/net/ipv4/tcp_timer.c
++++ b/net/ipv4/tcp_timer.c
+@@ -682,6 +682,7 @@ void tcp_write_timer_handler(struct sock *sk)
+ return;
+ }
+
++ tcp_rate_check_app_limited(sk);
+ tcp_mstamp_refresh(tcp_sk(sk));
+ event = icsk->icsk_pending;
+
+--
+2.45.2
+
diff --git a/SOURCES/cachy-fixes.patch b/SOURCES/cachy-fixes.patch
new file mode 100644
index 0000000..1c632df
--- /dev/null
+++ b/SOURCES/cachy-fixes.patch
@@ -0,0 +1,573 @@
+diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c
+index 371531b2a3d0..ddc064b4fc32 100644
+--- a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c
++++ b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c
+@@ -8327,6 +8327,122 @@ static inline uint32_t get_mem_type(struct drm_framebuffer *fb)
+ return abo->tbo.resource ? abo->tbo.resource->mem_type : 0;
+ }
+
++/* TODO remove duplicate */
++static int amdgpu_dm_plane_get_cursor_position(struct drm_plane *plane, struct drm_crtc *crtc,
++ struct dc_cursor_position *position)
++{
++ struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
++ int x, y;
++ int xorigin = 0, yorigin = 0;
++
++ if (!crtc || !plane->state->fb)
++ return 0;
++
++ if ((plane->state->crtc_w > amdgpu_crtc->max_cursor_width) ||
++ (plane->state->crtc_h > amdgpu_crtc->max_cursor_height)) {
++ DRM_ERROR("%s: bad cursor width or height %d x %d\n",
++ __func__,
++ plane->state->crtc_w,
++ plane->state->crtc_h);
++ return -EINVAL;
++ }
++
++ x = plane->state->crtc_x;
++ y = plane->state->crtc_y;
++
++ if (x <= -amdgpu_crtc->max_cursor_width ||
++ y <= -amdgpu_crtc->max_cursor_height)
++ return 0;
++
++ if (x < 0) {
++ xorigin = min(-x, amdgpu_crtc->max_cursor_width - 1);
++ x = 0;
++ }
++ if (y < 0) {
++ yorigin = min(-y, amdgpu_crtc->max_cursor_height - 1);
++ y = 0;
++ }
++ position->enable = true;
++ position->translate_by_source = true;
++ position->x = x;
++ position->y = y;
++ position->x_hotspot = xorigin;
++ position->y_hotspot = yorigin;
++
++ return 0;
++}
++
++static void amdgpu_dm_update_cursor(struct drm_plane *plane,
++ struct drm_plane_state *old_plane_state,
++ struct dc_stream_update *update)
++{
++ struct amdgpu_device *adev = drm_to_adev(plane->dev);
++ struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(plane->state->fb);
++ struct drm_crtc *crtc = afb ? plane->state->crtc : old_plane_state->crtc;
++ struct dm_crtc_state *crtc_state = crtc ? to_dm_crtc_state(crtc->state) : NULL;
++ struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
++ uint64_t address = afb ? afb->address : 0;
++ struct dc_cursor_position position = {0};
++ struct dc_cursor_attributes attributes;
++ int ret;
++
++ if (!plane->state->fb && !old_plane_state->fb)
++ return;
++
++ drm_dbg_atomic(plane->dev, "crtc_id=%d with size %d to %d\n",
++ amdgpu_crtc->crtc_id, plane->state->crtc_w,
++ plane->state->crtc_h);
++
++ ret = amdgpu_dm_plane_get_cursor_position(plane, crtc, &position);
++ if (ret)
++ return;
++
++ if (!position.enable) {
++ /* turn off cursor */
++ if (crtc_state && crtc_state->stream) {
++ dc_stream_set_cursor_position(crtc_state->stream,
++ &position);
++ update->cursor_position = &crtc_state->stream->cursor_position;
++ }
++ return;
++ }
++
++ amdgpu_crtc->cursor_width = plane->state->crtc_w;
++ amdgpu_crtc->cursor_height = plane->state->crtc_h;
++
++ memset(&attributes, 0, sizeof(attributes));
++ attributes.address.high_part = upper_32_bits(address);
++ attributes.address.low_part = lower_32_bits(address);
++ attributes.width = plane->state->crtc_w;
++ attributes.height = plane->state->crtc_h;
++ attributes.color_format = CURSOR_MODE_COLOR_PRE_MULTIPLIED_ALPHA;
++ attributes.rotation_angle = 0;
++ attributes.attribute_flags.value = 0;
++
++ /* Enable cursor degamma ROM on DCN3+ for implicit sRGB degamma in DRM
++ * legacy gamma setup.
++ */
++ if (crtc_state->cm_is_degamma_srgb &&
++ adev->dm.dc->caps.color.dpp.gamma_corr)
++ attributes.attribute_flags.bits.ENABLE_CURSOR_DEGAMMA = 1;
++
++ attributes.pitch = afb->base.pitches[0] / afb->base.format->cpp[0];
++
++ if (crtc_state->stream) {
++ if (!dc_stream_set_cursor_attributes(crtc_state->stream,
++ &attributes))
++ DRM_ERROR("DC failed to set cursor attributes\n");
++
++ update->cursor_attributes = &crtc_state->stream->cursor_attributes;
++
++ if (!dc_stream_set_cursor_position(crtc_state->stream,
++ &position))
++ DRM_ERROR("DC failed to set cursor position\n");
++
++ update->cursor_position = &crtc_state->stream->cursor_position;
++ }
++}
++
+ static void amdgpu_dm_commit_planes(struct drm_atomic_state *state,
+ struct drm_device *dev,
+ struct amdgpu_display_manager *dm,
+@@ -8350,6 +8466,7 @@ static void amdgpu_dm_commit_planes(struct drm_atomic_state *state,
+ bool cursor_update = false;
+ bool pflip_present = false;
+ bool dirty_rects_changed = false;
++ bool updated_planes_and_streams = false;
+ struct {
+ struct dc_surface_update surface_updates[MAX_SURFACES];
+ struct dc_plane_info plane_infos[MAX_SURFACES];
+@@ -8386,8 +8503,10 @@ static void amdgpu_dm_commit_planes(struct drm_atomic_state *state,
+ /* Cursor plane is handled after stream updates */
+ if (plane->type == DRM_PLANE_TYPE_CURSOR) {
+ if ((fb && crtc == pcrtc) ||
+- (old_plane_state->fb && old_plane_state->crtc == pcrtc))
++ (old_plane_state->fb && old_plane_state->crtc == pcrtc)) {
+ cursor_update = true;
++ amdgpu_dm_update_cursor(plane, old_plane_state, &bundle->stream_update);
++ }
+
+ continue;
+ }
+@@ -8660,6 +8779,7 @@ static void amdgpu_dm_commit_planes(struct drm_atomic_state *state,
+ acrtc_state->stream,
+ &bundle->stream_update,
+ bundle->surface_updates);
++ updated_planes_and_streams = true;
+
+ /**
+ * Enable or disable the interrupts on the backend.
+@@ -8737,7 +8857,7 @@ static void amdgpu_dm_commit_planes(struct drm_atomic_state *state,
+ * This avoids redundant programming in the case where we're going
+ * to be disabling a single plane - those pipes are being disabled.
+ */
+- if (acrtc_state->active_planes)
++ if (acrtc_state->active_planes && !updated_planes_and_streams)
+ amdgpu_dm_commit_cursors(state);
+
+ cleanup:
+@@ -8925,7 +9045,7 @@ static void amdgpu_dm_commit_streams(struct drm_atomic_state *state,
+
+ memset(&position, 0, sizeof(position));
+ mutex_lock(&dm->dc_lock);
+- dc_stream_set_cursor_position(dm_old_crtc_state->stream, &position);
++ dc_stream_program_cursor_position(dm_old_crtc_state->stream, &position);
+ mutex_unlock(&dm->dc_lock);
+ }
+
+diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_plane.c b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_plane.c
+index 779880c64575..8c8d0d209d7a 100644
+--- a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_plane.c
++++ b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_plane.c
+@@ -1254,7 +1254,7 @@ void amdgpu_dm_plane_handle_cursor_update(struct drm_plane *plane,
+ /* turn off cursor */
+ if (crtc_state && crtc_state->stream) {
+ mutex_lock(&adev->dm.dc_lock);
+- dc_stream_set_cursor_position(crtc_state->stream,
++ dc_stream_program_cursor_position(crtc_state->stream,
+ &position);
+ mutex_unlock(&adev->dm.dc_lock);
+ }
+@@ -1284,11 +1284,11 @@ void amdgpu_dm_plane_handle_cursor_update(struct drm_plane *plane,
+
+ if (crtc_state->stream) {
+ mutex_lock(&adev->dm.dc_lock);
+- if (!dc_stream_set_cursor_attributes(crtc_state->stream,
++ if (!dc_stream_program_cursor_attributes(crtc_state->stream,
+ &attributes))
+ DRM_ERROR("DC failed to set cursor attributes\n");
+
+- if (!dc_stream_set_cursor_position(crtc_state->stream,
++ if (!dc_stream_program_cursor_position(crtc_state->stream,
+ &position))
+ DRM_ERROR("DC failed to set cursor position\n");
+ mutex_unlock(&adev->dm.dc_lock);
+diff --git a/drivers/gpu/drm/amd/display/dc/core/dc.c b/drivers/gpu/drm/amd/display/dc/core/dc.c
+index d68c83e40d4d..575290784c61 100644
+--- a/drivers/gpu/drm/amd/display/dc/core/dc.c
++++ b/drivers/gpu/drm/amd/display/dc/core/dc.c
+@@ -3188,6 +3188,83 @@ static bool update_planes_and_stream_state(struct dc *dc,
+
+ }
+
++static void program_cursor_attributes(
++ struct dc *dc,
++ struct dc_stream_state *stream)
++{
++ int i;
++ struct resource_context *res_ctx;
++ struct pipe_ctx *pipe_to_program = NULL;
++
++ if (!stream)
++ return;
++
++ res_ctx = &dc->current_state->res_ctx;
++
++ for (i = 0; i < MAX_PIPES; i++) {
++ struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
++
++ if (pipe_ctx->stream != stream)
++ continue;
++
++ if (!pipe_to_program) {
++ pipe_to_program = pipe_ctx;
++ dc->hwss.cursor_lock(dc, pipe_to_program, true);
++ if (pipe_to_program->next_odm_pipe)
++ dc->hwss.cursor_lock(dc, pipe_to_program->next_odm_pipe, true);
++ }
++
++ dc->hwss.set_cursor_attribute(pipe_ctx);
++ if (dc->ctx->dmub_srv)
++ dc_send_update_cursor_info_to_dmu(pipe_ctx, i);
++ if (dc->hwss.set_cursor_sdr_white_level)
++ dc->hwss.set_cursor_sdr_white_level(pipe_ctx);
++ }
++
++ if (pipe_to_program) {
++ dc->hwss.cursor_lock(dc, pipe_to_program, false);
++ if (pipe_to_program->next_odm_pipe)
++ dc->hwss.cursor_lock(dc, pipe_to_program->next_odm_pipe, false);
++ }
++}
++
++static void program_cursor_position(
++ struct dc *dc,
++ struct dc_stream_state *stream)
++{
++ int i;
++ struct resource_context *res_ctx;
++ struct pipe_ctx *pipe_to_program = NULL;
++
++ if (!stream)
++ return;
++
++ res_ctx = &dc->current_state->res_ctx;
++
++ for (i = 0; i < MAX_PIPES; i++) {
++ struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
++
++ if (pipe_ctx->stream != stream ||
++ (!pipe_ctx->plane_res.mi && !pipe_ctx->plane_res.hubp) ||
++ !pipe_ctx->plane_state ||
++ (!pipe_ctx->plane_res.xfm && !pipe_ctx->plane_res.dpp) ||
++ (!pipe_ctx->plane_res.ipp && !pipe_ctx->plane_res.dpp))
++ continue;
++
++ if (!pipe_to_program) {
++ pipe_to_program = pipe_ctx;
++ dc->hwss.cursor_lock(dc, pipe_to_program, true);
++ }
++
++ dc->hwss.set_cursor_position(pipe_ctx);
++ if (dc->ctx->dmub_srv)
++ dc_send_update_cursor_info_to_dmu(pipe_ctx, i);
++ }
++
++ if (pipe_to_program)
++ dc->hwss.cursor_lock(dc, pipe_to_program, false);
++}
++
+ static void commit_planes_do_stream_update(struct dc *dc,
+ struct dc_stream_state *stream,
+ struct dc_stream_update *stream_update,
+@@ -3248,6 +3325,13 @@ static void commit_planes_do_stream_update(struct dc *dc,
+ }
+ }
+
++ if (stream_update->cursor_attributes) {
++ program_cursor_attributes(dc, stream);
++ }
++
++ if (stream_update->cursor_position) {
++ program_cursor_position(dc, stream);
++ }
+
+ /* Full fe update*/
+ if (update_type == UPDATE_TYPE_FAST)
+diff --git a/drivers/gpu/drm/amd/display/dc/core/dc_stream.c b/drivers/gpu/drm/amd/display/dc/core/dc_stream.c
+index 51a970fcb5d0..5601ee8a8d41 100644
+--- a/drivers/gpu/drm/amd/display/dc/core/dc_stream.c
++++ b/drivers/gpu/drm/amd/display/dc/core/dc_stream.c
+@@ -278,7 +278,6 @@ bool dc_stream_set_cursor_attributes(
+ const struct dc_cursor_attributes *attributes)
+ {
+ struct dc *dc;
+- bool reset_idle_optimizations = false;
+
+ if (NULL == stream) {
+ dm_error("DC: dc_stream is NULL!\n");
+@@ -309,20 +308,36 @@ bool dc_stream_set_cursor_attributes(
+
+ stream->cursor_attributes = *attributes;
+
+- dc_z10_restore(dc);
+- /* disable idle optimizations while updating cursor */
+- if (dc->idle_optimizations_allowed) {
+- dc_allow_idle_optimizations(dc, false);
+- reset_idle_optimizations = true;
+- }
++ return true;
++}
+
+- program_cursor_attributes(dc, stream, attributes);
++bool dc_stream_program_cursor_attributes(
++ struct dc_stream_state *stream,
++ const struct dc_cursor_attributes *attributes)
++{
++ struct dc *dc;
++ bool reset_idle_optimizations = false;
+
+- /* re-enable idle optimizations if necessary */
+- if (reset_idle_optimizations)
+- dc_allow_idle_optimizations(dc, true);
++ dc = stream ? stream->ctx->dc : NULL;
+
+- return true;
++ if (dc_stream_set_cursor_attributes(stream, attributes)) {
++ dc_z10_restore(dc);
++ /* disable idle optimizations while updating cursor */
++ if (dc->idle_optimizations_allowed) {
++ dc_allow_idle_optimizations(dc, false);
++ reset_idle_optimizations = true;
++ }
++
++ program_cursor_attributes(dc, stream, attributes);
++
++ /* re-enable idle optimizations if necessary */
++ if (reset_idle_optimizations)
++ dc_allow_idle_optimizations(dc, true);
++
++ return true;
++ }
++
++ return false;
+ }
+
+ static void program_cursor_position(
+@@ -367,9 +382,6 @@ bool dc_stream_set_cursor_position(
+ struct dc_stream_state *stream,
+ const struct dc_cursor_position *position)
+ {
+- struct dc *dc;
+- bool reset_idle_optimizations = false;
+-
+ if (NULL == stream) {
+ dm_error("DC: dc_stream is NULL!\n");
+ return false;
+@@ -380,24 +392,43 @@ bool dc_stream_set_cursor_position(
+ return false;
+ }
+
++ stream->cursor_position = *position;
++
++ return true;
++}
++
++bool dc_stream_program_cursor_position(
++ struct dc_stream_state *stream,
++ const struct dc_cursor_position *position)
++{
++ struct dc *dc;
++ bool reset_idle_optimizations = false;
++ const struct dc_cursor_position *old_position;
++
++ old_position = stream ? &stream->cursor_position : NULL;
+ dc = stream->ctx->dc;
+- dc_z10_restore(dc);
+
+- /* disable idle optimizations if enabling cursor */
+- if (dc->idle_optimizations_allowed && (!stream->cursor_position.enable || dc->debug.exit_idle_opt_for_cursor_updates)
+- && position->enable) {
+- dc_allow_idle_optimizations(dc, false);
+- reset_idle_optimizations = true;
+- }
++ if (dc_stream_set_cursor_position(stream, position)) {
++ dc_z10_restore(dc);
+
+- stream->cursor_position = *position;
++ /* disable idle optimizations if enabling cursor */
++ if (dc->idle_optimizations_allowed &&
++ (!old_position->enable || dc->debug.exit_idle_opt_for_cursor_updates) &&
++ position->enable) {
++ dc_allow_idle_optimizations(dc, false);
++ reset_idle_optimizations = true;
++ }
+
+- program_cursor_position(dc, stream, position);
+- /* re-enable idle optimizations if necessary */
+- if (reset_idle_optimizations)
+- dc_allow_idle_optimizations(dc, true);
+
+- return true;
++ program_cursor_position(dc, stream, position);
++ /* re-enable idle optimizations if necessary */
++ if (reset_idle_optimizations)
++ dc_allow_idle_optimizations(dc, true);
++
++ return true;
++ }
++
++ return false;
+ }
+
+ bool dc_stream_add_writeback(struct dc *dc,
+diff --git a/drivers/gpu/drm/amd/display/dc/dc_stream.h b/drivers/gpu/drm/amd/display/dc/dc_stream.h
+index ee10941caa59..df86668f560f 100644
+--- a/drivers/gpu/drm/amd/display/dc/dc_stream.h
++++ b/drivers/gpu/drm/amd/display/dc/dc_stream.h
+@@ -327,6 +327,9 @@ struct dc_stream_update {
+
+ struct test_pattern *pending_test_pattern;
+ struct dc_crtc_timing_adjust *crtc_timing_adjust;
++
++ struct dc_cursor_attributes *cursor_attributes;
++ struct dc_cursor_position *cursor_position;
+ };
+
+ bool dc_is_stream_unchanged(
+@@ -478,10 +481,17 @@ bool dc_stream_set_cursor_attributes(
+ struct dc_stream_state *stream,
+ const struct dc_cursor_attributes *attributes);
+
++bool dc_stream_program_cursor_attributes(
++ struct dc_stream_state *stream,
++ const struct dc_cursor_attributes *attributes);
++
+ bool dc_stream_set_cursor_position(
+ struct dc_stream_state *stream,
+ const struct dc_cursor_position *position);
+
++bool dc_stream_program_cursor_position(
++ struct dc_stream_state *stream,
++ const struct dc_cursor_position *position);
+
+ bool dc_stream_adjust_vmin_vmax(struct dc *dc,
+ struct dc_stream_state *stream,
+diff --git a/drivers/gpu/drm/amd/display/dc/hwss/dcn30/dcn30_hwseq.c b/drivers/gpu/drm/amd/display/dc/hwss/dcn30/dcn30_hwseq.c
+index 8bc3d01537bb..f91169c80f7f 100644
+--- a/drivers/gpu/drm/amd/display/dc/hwss/dcn30/dcn30_hwseq.c
++++ b/drivers/gpu/drm/amd/display/dc/hwss/dcn30/dcn30_hwseq.c
+@@ -1042,7 +1042,7 @@ bool dcn30_apply_idle_power_optimizations(struct dc *dc, bool enable)
+
+ /* Use copied cursor, and it's okay to not switch back */
+ cursor_attr.address.quad_part = cmd.mall.cursor_copy_dst.quad_part;
+- dc_stream_set_cursor_attributes(stream, &cursor_attr);
++ dc_stream_program_cursor_attributes(stream, &cursor_attr);
+ }
+
+ /* Enable MALL */
+diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
+index 1e020620748d..082f87834503 100644
+--- a/fs/btrfs/extent_io.c
++++ b/fs/btrfs/extent_io.c
+@@ -4269,6 +4269,13 @@ void set_extent_buffer_uptodate(struct extent_buffer *eb)
+ }
+ }
+
++static void clear_extent_buffer_reading(struct extent_buffer *eb)
++{
++ clear_bit(EXTENT_BUFFER_READING, &eb->bflags);
++ smp_mb__after_atomic();
++ wake_up_bit(&eb->bflags, EXTENT_BUFFER_READING);
++}
++
+ static void end_bbio_meta_read(struct btrfs_bio *bbio)
+ {
+ struct extent_buffer *eb = bbio->private;
+@@ -4277,6 +4284,13 @@ static void end_bbio_meta_read(struct btrfs_bio *bbio)
+ struct folio_iter fi;
+ u32 bio_offset = 0;
+
++ /*
++ * If the extent buffer is marked UPTODATE before the read operation
++ * completes, other calls to read_extent_buffer_pages() will return
++ * early without waiting for the read to finish, causing data races.
++ */
++ WARN_ON(test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags));
++
+ eb->read_mirror = bbio->mirror_num;
+
+ if (uptodate &&
+@@ -4303,9 +4317,7 @@ static void end_bbio_meta_read(struct btrfs_bio *bbio)
+ bio_offset += len;
+ }
+
+- clear_bit(EXTENT_BUFFER_READING, &eb->bflags);
+- smp_mb__after_atomic();
+- wake_up_bit(&eb->bflags, EXTENT_BUFFER_READING);
++ clear_extent_buffer_reading(eb);
+ free_extent_buffer(eb);
+
+ bio_put(&bbio->bio);
+@@ -4339,9 +4351,7 @@ int read_extent_buffer_pages(struct extent_buffer *eb, int wait, int mirror_num,
+ * will now be set, and we shouldn't read it in again.
+ */
+ if (unlikely(test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))) {
+- clear_bit(EXTENT_BUFFER_READING, &eb->bflags);
+- smp_mb__after_atomic();
+- wake_up_bit(&eb->bflags, EXTENT_BUFFER_READING);
++ clear_extent_buffer_reading(eb);
+ return 0;
+ }
+
+diff --git a/include/linux/filter.h b/include/linux/filter.h
+index cf12bfa2a78c..2acf8634008d 100644
+--- a/include/linux/filter.h
++++ b/include/linux/filter.h
+@@ -644,14 +644,16 @@ static __always_inline u32 __bpf_prog_run(const struct bpf_prog *prog,
+ cant_migrate();
+ if (static_branch_unlikely(&bpf_stats_enabled_key)) {
+ struct bpf_prog_stats *stats;
+- u64 start = sched_clock();
++ u64 duration, start = sched_clock();
+ unsigned long flags;
+
+ ret = dfunc(ctx, prog->insnsi, prog->bpf_func);
++
++ duration = sched_clock() - start;
+ stats = this_cpu_ptr(prog->stats);
+ flags = u64_stats_update_begin_irqsave(&stats->syncp);
+ u64_stats_inc(&stats->cnt);
+- u64_stats_add(&stats->nsecs, sched_clock() - start);
++ u64_stats_add(&stats->nsecs, duration);
+ u64_stats_update_end_irqrestore(&stats->syncp, flags);
+ } else {
+ ret = dfunc(ctx, prog->insnsi, prog->bpf_func);
+diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c
+index db7599c59c78..146de96e99b8 100644
+--- a/kernel/bpf/trampoline.c
++++ b/kernel/bpf/trampoline.c
+@@ -883,12 +883,13 @@ static void notrace update_prog_stats(struct bpf_prog *prog,
+ * Hence check that 'start' is valid.
+ */
+ start > NO_START_TIME) {
++ u64 duration = sched_clock() - start;
+ unsigned long flags;
+
+ stats = this_cpu_ptr(prog->stats);
+ flags = u64_stats_update_begin_irqsave(&stats->syncp);
+ u64_stats_inc(&stats->cnt);
+- u64_stats_add(&stats->nsecs, sched_clock() - start);
++ u64_stats_add(&stats->nsecs, duration);
+ u64_stats_update_end_irqrestore(&stats->syncp, flags);
+ }
+ }
+--
+2.45.2
+
diff --git a/SOURCES/kernel-aarch64-16k-debug-fedora.config b/SOURCES/kernel-aarch64-16k-debug-fedora.config
index 4c44c22..8b9bf99 100644
--- a/SOURCES/kernel-aarch64-16k-debug-fedora.config
+++ b/SOURCES/kernel-aarch64-16k-debug-fedora.config
@@ -10104,3 +10104,4 @@ CONFIG_HID_APPLETB_BL=m
CONFIG_HID_APPLETB_KBD=m
CONFIG_HID_APPLE_MAGIC_BACKLIGHT=m
CONFIG_APPLE_BCE=m
+CONFIG_AMD_PRIVATE_COLOR=y
diff --git a/SOURCES/kernel-aarch64-16k-fedora.config b/SOURCES/kernel-aarch64-16k-fedora.config
index 6ef0ca5..c6ccf78 100644
--- a/SOURCES/kernel-aarch64-16k-fedora.config
+++ b/SOURCES/kernel-aarch64-16k-fedora.config
@@ -10075,3 +10075,4 @@ CONFIG_HID_APPLETB_BL=m
CONFIG_HID_APPLETB_KBD=m
CONFIG_HID_APPLE_MAGIC_BACKLIGHT=m
CONFIG_APPLE_BCE=m
+CONFIG_AMD_PRIVATE_COLOR=y
diff --git a/SOURCES/kernel-aarch64-64k-debug-rhel.config b/SOURCES/kernel-aarch64-64k-debug-rhel.config
index 6b426c3..b1ea174 100644
--- a/SOURCES/kernel-aarch64-64k-debug-rhel.config
+++ b/SOURCES/kernel-aarch64-64k-debug-rhel.config
@@ -8140,3 +8140,4 @@ CONFIG_HID_APPLETB_BL=m
CONFIG_HID_APPLETB_KBD=m
CONFIG_HID_APPLE_MAGIC_BACKLIGHT=m
CONFIG_APPLE_BCE=m
+CONFIG_AMD_PRIVATE_COLOR=y
diff --git a/SOURCES/kernel-aarch64-64k-rhel.config b/SOURCES/kernel-aarch64-64k-rhel.config
index a0b10ae..30709e2 100644
--- a/SOURCES/kernel-aarch64-64k-rhel.config
+++ b/SOURCES/kernel-aarch64-64k-rhel.config
@@ -8115,3 +8115,4 @@ CONFIG_HID_APPLETB_BL=m
CONFIG_HID_APPLETB_KBD=m
CONFIG_HID_APPLE_MAGIC_BACKLIGHT=m
CONFIG_APPLE_BCE=m
+CONFIG_AMD_PRIVATE_COLOR=y
diff --git a/SOURCES/kernel-aarch64-debug-fedora.config b/SOURCES/kernel-aarch64-debug-fedora.config
index 6411934..85a7e95 100644
--- a/SOURCES/kernel-aarch64-debug-fedora.config
+++ b/SOURCES/kernel-aarch64-debug-fedora.config
@@ -10103,3 +10103,4 @@ CONFIG_HID_APPLETB_BL=m
CONFIG_HID_APPLETB_KBD=m
CONFIG_HID_APPLE_MAGIC_BACKLIGHT=m
CONFIG_APPLE_BCE=m
+CONFIG_AMD_PRIVATE_COLOR=y
diff --git a/SOURCES/kernel-aarch64-debug-rhel.config b/SOURCES/kernel-aarch64-debug-rhel.config
index a2357fb..58aa680 100644
--- a/SOURCES/kernel-aarch64-debug-rhel.config
+++ b/SOURCES/kernel-aarch64-debug-rhel.config
@@ -8136,3 +8136,4 @@ CONFIG_HID_APPLETB_BL=m
CONFIG_HID_APPLETB_KBD=m
CONFIG_HID_APPLE_MAGIC_BACKLIGHT=m
CONFIG_APPLE_BCE=m
+CONFIG_AMD_PRIVATE_COLOR=y
diff --git a/SOURCES/kernel-aarch64-fedora.config b/SOURCES/kernel-aarch64-fedora.config
index 912cde2..45d7c64 100644
--- a/SOURCES/kernel-aarch64-fedora.config
+++ b/SOURCES/kernel-aarch64-fedora.config
@@ -10074,3 +10074,4 @@ CONFIG_HID_APPLETB_BL=m
CONFIG_HID_APPLETB_KBD=m
CONFIG_HID_APPLE_MAGIC_BACKLIGHT=m
CONFIG_APPLE_BCE=m
+CONFIG_AMD_PRIVATE_COLOR=y
diff --git a/SOURCES/kernel-aarch64-rhel.config b/SOURCES/kernel-aarch64-rhel.config
index 17340d8..8dfe7db 100644
--- a/SOURCES/kernel-aarch64-rhel.config
+++ b/SOURCES/kernel-aarch64-rhel.config
@@ -8111,3 +8111,4 @@ CONFIG_HID_APPLETB_BL=m
CONFIG_HID_APPLETB_KBD=m
CONFIG_HID_APPLE_MAGIC_BACKLIGHT=m
CONFIG_APPLE_BCE=m
+CONFIG_AMD_PRIVATE_COLOR=y
diff --git a/SOURCES/kernel-aarch64-rt-debug-rhel.config b/SOURCES/kernel-aarch64-rt-debug-rhel.config
index 2230ac1..87cb2c1 100644
--- a/SOURCES/kernel-aarch64-rt-debug-rhel.config
+++ b/SOURCES/kernel-aarch64-rt-debug-rhel.config
@@ -8196,3 +8196,4 @@ CONFIG_HID_APPLETB_BL=m
CONFIG_HID_APPLETB_KBD=m
CONFIG_HID_APPLE_MAGIC_BACKLIGHT=m
CONFIG_APPLE_BCE=m
+CONFIG_AMD_PRIVATE_COLOR=y
diff --git a/SOURCES/kernel-aarch64-rt-rhel.config b/SOURCES/kernel-aarch64-rt-rhel.config
index d1b854a..2b1ac51 100644
--- a/SOURCES/kernel-aarch64-rt-rhel.config
+++ b/SOURCES/kernel-aarch64-rt-rhel.config
@@ -8171,3 +8171,4 @@ CONFIG_HID_APPLETB_BL=m
CONFIG_HID_APPLETB_KBD=m
CONFIG_HID_APPLE_MAGIC_BACKLIGHT=m
CONFIG_APPLE_BCE=m
+CONFIG_AMD_PRIVATE_COLOR=y
diff --git a/SOURCES/kernel-ppc64le-debug-fedora.config b/SOURCES/kernel-ppc64le-debug-fedora.config
index da78efe..bacd0fe 100644
--- a/SOURCES/kernel-ppc64le-debug-fedora.config
+++ b/SOURCES/kernel-ppc64le-debug-fedora.config
@@ -8355,3 +8355,4 @@ CONFIG_HID_APPLETB_BL=m
CONFIG_HID_APPLETB_KBD=m
CONFIG_HID_APPLE_MAGIC_BACKLIGHT=m
CONFIG_APPLE_BCE=m
+CONFIG_AMD_PRIVATE_COLOR=y
diff --git a/SOURCES/kernel-ppc64le-debug-rhel.config b/SOURCES/kernel-ppc64le-debug-rhel.config
index 8d613cf..ba2f4e6 100644
--- a/SOURCES/kernel-ppc64le-debug-rhel.config
+++ b/SOURCES/kernel-ppc64le-debug-rhel.config
@@ -7604,3 +7604,4 @@ CONFIG_HID_APPLETB_BL=m
CONFIG_HID_APPLETB_KBD=m
CONFIG_HID_APPLE_MAGIC_BACKLIGHT=m
CONFIG_APPLE_BCE=m
+CONFIG_AMD_PRIVATE_COLOR=y
diff --git a/SOURCES/kernel-ppc64le-fedora.config b/SOURCES/kernel-ppc64le-fedora.config
index ffda948..330cf3c 100644
--- a/SOURCES/kernel-ppc64le-fedora.config
+++ b/SOURCES/kernel-ppc64le-fedora.config
@@ -8324,3 +8324,4 @@ CONFIG_HID_APPLETB_BL=m
CONFIG_HID_APPLETB_KBD=m
CONFIG_HID_APPLE_MAGIC_BACKLIGHT=m
CONFIG_APPLE_BCE=m
+CONFIG_AMD_PRIVATE_COLOR=y
diff --git a/SOURCES/kernel-ppc64le-rhel.config b/SOURCES/kernel-ppc64le-rhel.config
index a809663..5b74759 100644
--- a/SOURCES/kernel-ppc64le-rhel.config
+++ b/SOURCES/kernel-ppc64le-rhel.config
@@ -7581,3 +7581,4 @@ CONFIG_HID_APPLETB_BL=m
CONFIG_HID_APPLETB_KBD=m
CONFIG_HID_APPLE_MAGIC_BACKLIGHT=m
CONFIG_APPLE_BCE=m
+CONFIG_AMD_PRIVATE_COLOR=y
diff --git a/SOURCES/kernel-s390x-debug-fedora.config b/SOURCES/kernel-s390x-debug-fedora.config
index d1b1dce..c3b15ab 100644
--- a/SOURCES/kernel-s390x-debug-fedora.config
+++ b/SOURCES/kernel-s390x-debug-fedora.config
@@ -8290,3 +8290,4 @@ CONFIG_HID_APPLETB_BL=m
CONFIG_HID_APPLETB_KBD=m
CONFIG_HID_APPLE_MAGIC_BACKLIGHT=m
CONFIG_APPLE_BCE=m
+CONFIG_AMD_PRIVATE_COLOR=y
diff --git a/SOURCES/kernel-s390x-debug-rhel.config b/SOURCES/kernel-s390x-debug-rhel.config
index 0330690..d9e180e 100644
--- a/SOURCES/kernel-s390x-debug-rhel.config
+++ b/SOURCES/kernel-s390x-debug-rhel.config
@@ -7586,3 +7586,4 @@ CONFIG_HID_APPLETB_BL=m
CONFIG_HID_APPLETB_KBD=m
CONFIG_HID_APPLE_MAGIC_BACKLIGHT=m
CONFIG_APPLE_BCE=m
+CONFIG_AMD_PRIVATE_COLOR=y
diff --git a/SOURCES/kernel-s390x-fedora.config b/SOURCES/kernel-s390x-fedora.config
index 8153e5e..1d7548b 100644
--- a/SOURCES/kernel-s390x-fedora.config
+++ b/SOURCES/kernel-s390x-fedora.config
@@ -8259,3 +8259,4 @@ CONFIG_HID_APPLETB_BL=m
CONFIG_HID_APPLETB_KBD=m
CONFIG_HID_APPLE_MAGIC_BACKLIGHT=m
CONFIG_APPLE_BCE=m
+CONFIG_AMD_PRIVATE_COLOR=y
diff --git a/SOURCES/kernel-s390x-rhel.config b/SOURCES/kernel-s390x-rhel.config
index 9caba6f..ce38556 100644
--- a/SOURCES/kernel-s390x-rhel.config
+++ b/SOURCES/kernel-s390x-rhel.config
@@ -7563,3 +7563,4 @@ CONFIG_HID_APPLETB_BL=m
CONFIG_HID_APPLETB_KBD=m
CONFIG_HID_APPLE_MAGIC_BACKLIGHT=m
CONFIG_APPLE_BCE=m
+CONFIG_AMD_PRIVATE_COLOR=y
diff --git a/SOURCES/kernel-s390x-zfcpdump-rhel.config b/SOURCES/kernel-s390x-zfcpdump-rhel.config
index f39c27f..f987196 100644
--- a/SOURCES/kernel-s390x-zfcpdump-rhel.config
+++ b/SOURCES/kernel-s390x-zfcpdump-rhel.config
@@ -7586,3 +7586,4 @@ CONFIG_HID_APPLETB_BL=m
CONFIG_HID_APPLETB_KBD=m
CONFIG_HID_APPLE_MAGIC_BACKLIGHT=m
CONFIG_APPLE_BCE=m
+CONFIG_AMD_PRIVATE_COLOR=y
diff --git a/SOURCES/kernel-x86_64-debug-fedora.config b/SOURCES/kernel-x86_64-debug-fedora.config
index d4e1c16..9486803 100644
--- a/SOURCES/kernel-x86_64-debug-fedora.config
+++ b/SOURCES/kernel-x86_64-debug-fedora.config
@@ -9047,3 +9047,4 @@ CONFIG_SND_SOC_SOF_IPC3=y
CONFIG_SND_SOC_SOF_INTEL_IPC4=y
CONFIG_SND_SOC_SOF_AMD_COMMON=m
CONFIG_SND_SOC_TOPOLOGY=y
+CONFIG_AMD_PRIVATE_COLOR=y
diff --git a/SOURCES/kernel-x86_64-debug-rhel.config b/SOURCES/kernel-x86_64-debug-rhel.config
index d814503..8b78a23 100644
--- a/SOURCES/kernel-x86_64-debug-rhel.config
+++ b/SOURCES/kernel-x86_64-debug-rhel.config
@@ -8035,3 +8035,4 @@ CONFIG_SND_SOC_SOF_AMD_ACP63=m
# CONFIG_SND_AMD_ASOC_REMBRANDT is not set
# CONFIG_SND_SOC_AMD_LEGACY_MACH is not set
CONFIG_SND_SOC_TOPOLOGY=y
+CONFIG_AMD_PRIVATE_COLOR=y
diff --git a/SOURCES/kernel-x86_64-fedora.config b/SOURCES/kernel-x86_64-fedora.config
index 0c9353f..a4240f3 100644
--- a/SOURCES/kernel-x86_64-fedora.config
+++ b/SOURCES/kernel-x86_64-fedora.config
@@ -9017,3 +9017,4 @@ CONFIG_SND_SOC_SOF_IPC3=y
CONFIG_SND_SOC_SOF_INTEL_IPC4=y
CONFIG_SND_SOC_SOF_AMD_COMMON=m
CONFIG_SND_SOC_TOPOLOGY=y
+CONFIG_AMD_PRIVATE_COLOR=y
diff --git a/SOURCES/kernel-x86_64-rhel.config b/SOURCES/kernel-x86_64-rhel.config
index 35f7ad5..c7b6ac2 100644
--- a/SOURCES/kernel-x86_64-rhel.config
+++ b/SOURCES/kernel-x86_64-rhel.config
@@ -8011,3 +8011,4 @@ CONFIG_SND_SOC_SOF_AMD_ACP63=m
# CONFIG_SND_AMD_ASOC_REMBRANDT is not set
# CONFIG_SND_SOC_AMD_LEGACY_MACH is not set
CONFIG_SND_SOC_TOPOLOGY=y
+CONFIG_AMD_PRIVATE_COLOR=y
diff --git a/SOURCES/kernel-x86_64-rt-debug-rhel.config b/SOURCES/kernel-x86_64-rt-debug-rhel.config
index bae0b0e..f9a79f1 100644
--- a/SOURCES/kernel-x86_64-rt-debug-rhel.config
+++ b/SOURCES/kernel-x86_64-rt-debug-rhel.config
@@ -8096,3 +8096,4 @@ CONFIG_SND_SOC_SOF_AMD_ACP63=m
# CONFIG_SND_AMD_ASOC_REMBRANDT is not set
# CONFIG_SND_SOC_AMD_LEGACY_MACH is not set
CONFIG_SND_SOC_TOPOLOGY=y
+CONFIG_AMD_PRIVATE_COLOR=y
diff --git a/SOURCES/kernel-x86_64-rt-rhel.config b/SOURCES/kernel-x86_64-rt-rhel.config
index 37a5069..5e66ede 100644
--- a/SOURCES/kernel-x86_64-rt-rhel.config
+++ b/SOURCES/kernel-x86_64-rt-rhel.config
@@ -8072,3 +8072,4 @@ CONFIG_SND_SOC_SOF_AMD_ACP63=m
# CONFIG_SND_AMD_ASOC_REMBRANDT is not set
# CONFIG_SND_SOC_AMD_LEGACY_MACH is not set
CONFIG_SND_SOC_TOPOLOGY=y
+CONFIG_AMD_PRIVATE_COLOR=y
diff --git a/SOURCES/tkg-misc-additions.patch b/SOURCES/tkg-misc-additions.patch
index 31368a7..938ddf3 100644
--- a/SOURCES/tkg-misc-additions.patch
+++ b/SOURCES/tkg-misc-additions.patch
@@ -97,3 +97,157 @@ index f4b210ab061291..837d0dbb28ea08 100644
depends on HAVE_ARCH_MMAP_RND_COMPAT_BITS
help
This value can be used to select the number of bits to use to
+
+diff --git a/drivers/gpu/drm/amd/display/dc/core/dc_state.c b/drivers/gpu/drm/amd/display/dc/core/dc_state.c
+index 180ac47868c2..42bf097b36b7 100644
+--- a/drivers/gpu/drm/amd/display/dc/core/dc_state.c
++++ b/drivers/gpu/drm/amd/display/dc/core/dc_state.c
+@@ -300,7 +300,6 @@ void dc_state_destruct(struct dc_state *state)
+ state->phantom_plane_count = 0;
+
+ state->stream_mask = 0;
+- memset(&state->res_ctx, 0, sizeof(state->res_ctx));
+ memset(&state->pp_display_cfg, 0, sizeof(state->pp_display_cfg));
+ memset(&state->dcn_bw_vars, 0, sizeof(state->dcn_bw_vars));
+ state->clk_mgr = NULL;
+
+From 299b81f3e619aea3ceda77d7c42842a496b34a53 Mon Sep 17 00:00:00 2001
+From: Peter Jung <admin@ptr1337.dev>
+Date: Thu, 21 Mar 2024 19:00:50 +0100
+Subject: [PATCH] cachy: move AMD_PRIVATE_COLOR to Kconfig
+
+Co-authored-by: PedroHLC <root@pedrohlc.com>
+Signed-off-by: Peter Jung <admin@ptr1337.dev>
+---
+ drivers/gpu/drm/amd/display/Kconfig | 6 ++++++
+ drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c | 2 +-
+ drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_color.c | 2 +-
+ drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c | 6 +++---
+ drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_plane.c | 6 +++---
+ 5 files changed, 14 insertions(+), 8 deletions(-)
+
+diff --git a/drivers/gpu/drm/amd/display/Kconfig b/drivers/gpu/drm/amd/display/Kconfig
+index 901d1961b73927..05c49141f5802f 100644
+--- a/drivers/gpu/drm/amd/display/Kconfig
++++ b/drivers/gpu/drm/amd/display/Kconfig
+@@ -51,4 +51,10 @@ config DRM_AMD_SECURE_DISPLAY
+ This option enables the calculation of crc of specific region via
+ debugfs. Cooperate with specific DMCU FW.
+
++config AMD_PRIVATE_COLOR
++ bool "Enable KMS color management by AMD for AMD"
++ default n
++ help
++ This option extends the KMS color management API with AMD driver-specific properties to enhance the color management support on AMD Steam Deck.
++
+ endmenu
+diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c
+index 59d2eee72a3297..0a4e75de95c257 100644
+--- a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c
++++ b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c
+@@ -4078,7 +4078,7 @@ static int amdgpu_dm_mode_config_init(struct amdgpu_device *adev)
+ return r;
+ }
+
+-#ifdef AMD_PRIVATE_COLOR
++#ifdef CONFIG_AMD_PRIVATE_COLOR
+ if (amdgpu_dm_create_color_properties(adev))
+ return -ENOMEM;
+ #endif
+diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_color.c b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_color.c
+index c87b64e464ed5c..6fe07243adc3d5 100644
+--- a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_color.c
++++ b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_color.c
+@@ -97,7 +97,7 @@ static inline struct fixed31_32 amdgpu_dm_fixpt_from_s3132(__u64 x)
+ return val;
+ }
+
+-#ifdef AMD_PRIVATE_COLOR
++#ifdef CONFIG_AMD_PRIVATE_COLOR
+ /* Pre-defined Transfer Functions (TF)
+ *
+ * AMD driver supports pre-defined mathematical functions for transferring
+diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c
+index 6e715ef3a5566e..11c7199ec3b348 100644
+--- a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c
++++ b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c
+@@ -290,7 +290,7 @@ static int amdgpu_dm_crtc_late_register(struct drm_crtc *crtc)
+ }
+ #endif
+
+-#ifdef AMD_PRIVATE_COLOR
++#ifdef CONFIG_AMD_PRIVATE_COLOR
+ /**
+ * dm_crtc_additional_color_mgmt - enable additional color properties
+ * @crtc: DRM CRTC
+@@ -372,7 +372,7 @@ static const struct drm_crtc_funcs amdgpu_dm_crtc_funcs = {
+ #if defined(CONFIG_DEBUG_FS)
+ .late_register = amdgpu_dm_crtc_late_register,
+ #endif
+-#ifdef AMD_PRIVATE_COLOR
++#ifdef CONFIG_AMD_PRIVATE_COLOR
+ .atomic_set_property = amdgpu_dm_atomic_crtc_set_property,
+ .atomic_get_property = amdgpu_dm_atomic_crtc_get_property,
+ #endif
+@@ -551,7 +551,7 @@ int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm,
+
+ drm_mode_crtc_set_gamma_size(&acrtc->base, MAX_COLOR_LEGACY_LUT_ENTRIES);
+
+-#ifdef AMD_PRIVATE_COLOR
++#ifdef CONFIG_AMD_PRIVATE_COLOR
+ dm_crtc_additional_color_mgmt(&acrtc->base);
+ #endif
+ return 0;
+diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_plane.c b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_plane.c
+index 8a4c40b4c27e4f..779880c6457553 100644
+--- a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_plane.c
++++ b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_plane.c
+@@ -1468,7 +1468,7 @@ static void amdgpu_dm_plane_drm_plane_destroy_state(struct drm_plane *plane,
+ drm_atomic_helper_plane_destroy_state(plane, state);
+ }
+
+-#ifdef AMD_PRIVATE_COLOR
++#ifdef CONFIG_AMD_PRIVATE_COLOR
+ static void
+ dm_atomic_plane_attach_color_mgmt_properties(struct amdgpu_display_manager *dm,
+ struct drm_plane *plane)
+@@ -1659,7 +1659,7 @@ static const struct drm_plane_funcs dm_plane_funcs = {
+ .atomic_duplicate_state = amdgpu_dm_plane_drm_plane_duplicate_state,
+ .atomic_destroy_state = amdgpu_dm_plane_drm_plane_destroy_state,
+ .format_mod_supported = amdgpu_dm_plane_format_mod_supported,
+-#ifdef AMD_PRIVATE_COLOR
++#ifdef CONFIG_AMD_PRIVATE_COLOR
+ .atomic_set_property = dm_atomic_plane_set_property,
+ .atomic_get_property = dm_atomic_plane_get_property,
+ #endif
+@@ -1742,7 +1742,7 @@ int amdgpu_dm_plane_init(struct amdgpu_display_manager *dm,
+
+ drm_plane_helper_add(plane, &dm_plane_helper_funcs);
+
+-#ifdef AMD_PRIVATE_COLOR
++#ifdef CONFIG_AMD_PRIVATE_COLOR
+ dm_atomic_plane_attach_color_mgmt_properties(dm, plane);
+ #endif
+ /* Create (reset) the plane state */
+
+From f1807682de0edbff6c1e46b19642a517d2e15c57 Mon Sep 17 00:00:00 2001
+From: Tk-Glitch <ti3nou@gmail.com>
+Date: Sat, 13 Apr 2024 18:25:35 +0530
+Subject: revert "drm/amd/pm: Fetch current power limit from FW"
+
+Fetching FW value prevents power limit modification by the user.
+The "out-of-band ways" are unclear.
+This fixes 7700/7800 XT custom powercap.
+
+diff --git a/drivers/gpu/drm/amd/pm/swsmu/amdgpu_smu.c b/drivers/gpu/drm/amd/pm/swsmu/amdgpu_smu.c
+index a54663f2e2ab9f..7ffad3eb0a0150 100644
+--- a/drivers/gpu/drm/amd/pm/swsmu/amdgpu_smu.c
++++ b/drivers/gpu/drm/amd/pm/swsmu/amdgpu_smu.c
+@@ -2703,7 +2703,6 @@ int smu_get_power_limit(void *handle,
+ case SMU_PPT_LIMIT_CURRENT:
+ switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {
+ case IP_VERSION(13, 0, 2):
+- case IP_VERSION(13, 0, 6):
+ case IP_VERSION(11, 0, 7):
+ case IP_VERSION(11, 0, 11):
+ case IP_VERSION(11, 0, 12):
diff --git a/SPECS/kernel.spec b/SPECS/kernel.spec
index 913814a..94722d8 100644
--- a/SPECS/kernel.spec
+++ b/SPECS/kernel.spec
@@ -169,7 +169,7 @@ Summary: The Linux kernel
# This is needed to do merge window version magic
%define patchlevel 9
# This allows pkg_release to have configurable %%{?dist} tag
-%define specrelease 202%{?buildid}%{?dist}
+%define specrelease 203%{?buildid}%{?dist}
# This defines the kabi tarball version
%define kabiversion 6.9.9
@@ -984,6 +984,8 @@ Patch211: tkg-misc-additions.patch
Patch212: tkg-unprivileged-CLONE_NEWUSER.patch
Patch213: cachy-bore.patch
Patch214: scx-kernel.patch
+Patch215: cachy-bbr3.patch
+Patch216: cachy-fixes.patch
# device specific patches
Patch300: linux-surface.patch
@@ -1006,7 +1008,6 @@ Patch337: 0001-ally-x-audio-quirk.patch
# hdr: https://github.com/CachyOS/kernel-patches
Patch208: 0001-ntsync.patch
Patch325: 0001-amd-pstate.patch
-Patch326: 0001-amd-hdr.patch
Patch327: 0001-add-acpi_call.patch
Patch328: uinput.patch
@@ -1883,6 +1884,8 @@ ApplyOptionalPatch tkg-misc-additions.patch
ApplyOptionalPatch tkg-unprivileged-CLONE_NEWUSER.patch
ApplyOptionalPatch cachy-bore.patch
ApplyOptionalPatch scx-kernel.patch
+ApplyOptionalPatch cachy-bbr3.patch
+ApplyOptionalPatch cachy-fixes.patch
# device specific patches
ApplyOptionalPatch linux-surface.patch
@@ -1905,7 +1908,6 @@ ApplyOptionalPatch 0001-ally-x-audio-quirk.patch
# https://github.com/CachyOS/kernel-patches
ApplyOptionalPatch 0001-ntsync.patch
ApplyOptionalPatch 0001-amd-pstate.patch
-ApplyOptionalPatch 0001-amd-hdr.patch
ApplyOptionalPatch 0001-add-acpi_call.patch
ApplyOptionalPatch uinput.patch
@@ -4123,6 +4125,9 @@ fi\
#
#
%changelog
+* Thu Jul 18 2024 Jan200101 <sentrycraft123@gmail.com> - 6.9.9-203.fsync
+- Update patchsets
+
* Tue Jul 16 2024 Jan200101 <sentrycraft123@gmail.com> - 6.9.9-202.fsync
- kernel-fsync v6.9.9
diff --git a/TOOLS/patch_configs.py b/TOOLS/patch_configs.py
index 6838cd6..1a84d21 100755
--- a/TOOLS/patch_configs.py
+++ b/TOOLS/patch_configs.py
@@ -37,8 +37,8 @@ GENERIC_PATCHES = [
["SENSORS_SURFACE_FAN", None, MODULE],
["SENSORS_SURFACE_TEMP", None, MODULE],
- # Steam Deck / amdgpu HDR Color management
- ["DRM_AMD_COLOR_STEAMDECK", None, ENABLE],
+ # amdgpu HDR Color management
+ ["AMD_PRIVATE_COLOR", None, ENABLE],
# Rog Ally Gyro Fix
["BMI323_I2C", None, MODULE],