+++ /dev/null
-From 9d6d165b5286de45fa1623f8947c98dd50f348da Mon Sep 17 00:00:00 2001
-From: Sasha Levin <sashal@kernel.org>
-Date: Fri, 9 Feb 2024 14:37:30 -0800
-Subject: usb: typec: ucsi: Limit read size on v1.2
-
-From: Abhishek Pandit-Subedi <abhishekpandit@chromium.org>
-
-[ Upstream commit b3db266fb031fba88c423d4bb8983a73a3db6527 ]
-
-Between UCSI 1.2 and UCSI 2.0, the size of the MESSAGE_IN region was
-increased from 16 to 256. In order to avoid overflowing reads for older
-systems, add a mechanism to use the read UCSI version to truncate read
-sizes on UCSI v1.2.
-
-Tested-by: Neil Armstrong <neil.armstrong@linaro.org>
-Reviewed-by: Prashant Malani <pmalani@chromium.org>
-Reviewed-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
-Signed-off-by: Abhishek Pandit-Subedi <abhishekpandit@chromium.org>
-Link: https://lore.kernel.org/r/20240209143723.v5.1.Iacf5570a66b82b73ef03daa6557e2fc0db10266a@changeid
-Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
-Signed-off-by: Sasha Levin <sashal@kernel.org>
----
- drivers/usb/typec/ucsi/ucsi.c | 26 ++++++++++++++++++++++++--
- drivers/usb/typec/ucsi/ucsi.h | 11 +++++++++++
- 2 files changed, 35 insertions(+), 2 deletions(-)
-
-diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c
-index 98f335cbbcdea..da457cf3cc457 100644
---- a/drivers/usb/typec/ucsi/ucsi.c
-+++ b/drivers/usb/typec/ucsi/ucsi.c
-@@ -36,6 +36,19 @@
- */
- #define UCSI_SWAP_TIMEOUT_MS 5000
-
-+static int ucsi_read_message_in(struct ucsi *ucsi, void *buf,
-+ size_t buf_size)
-+{
-+ /*
-+ * Below UCSI 2.0, MESSAGE_IN was limited to 16 bytes. Truncate the
-+ * reads here.
-+ */
-+ if (ucsi->version <= UCSI_VERSION_1_2)
-+ buf_size = clamp(buf_size, 0, 16);
-+
-+ return ucsi->ops->read(ucsi, UCSI_MESSAGE_IN, buf, buf_size);
-+}
-+
- static int ucsi_acknowledge_command(struct ucsi *ucsi)
- {
- u64 ctrl;
-@@ -72,7 +85,7 @@ static int ucsi_read_error(struct ucsi *ucsi)
- if (ret < 0)
- return ret;
-
-- ret = ucsi->ops->read(ucsi, UCSI_MESSAGE_IN, &error, sizeof(error));
-+ ret = ucsi_read_message_in(ucsi, &error, sizeof(error));
- if (ret)
- return ret;
-
-@@ -174,7 +187,7 @@ int ucsi_send_command(struct ucsi *ucsi, u64 command,
- length = ret;
-
- if (data) {
-- ret = ucsi->ops->read(ucsi, UCSI_MESSAGE_IN, data, size);
-+ ret = ucsi_read_message_in(ucsi, data, size);
- if (ret)
- goto out;
- }
-@@ -1441,6 +1454,15 @@ int ucsi_register(struct ucsi *ucsi)
- if (!ucsi->version)
- return -ENODEV;
-
-+ /*
-+ * Version format is JJ.M.N (JJ = Major version, M = Minor version,
-+ * N = sub-minor version).
-+ */
-+ dev_dbg(ucsi->dev, "Registered UCSI interface with version %x.%x.%x",
-+ UCSI_BCD_GET_MAJOR(ucsi->version),
-+ UCSI_BCD_GET_MINOR(ucsi->version),
-+ UCSI_BCD_GET_SUBMINOR(ucsi->version));
-+
- queue_delayed_work(system_long_wq, &ucsi->work, 0);
-
- return 0;
-diff --git a/drivers/usb/typec/ucsi/ucsi.h b/drivers/usb/typec/ucsi/ucsi.h
-index dbb10cb310d4c..304939a712bf8 100644
---- a/drivers/usb/typec/ucsi/ucsi.h
-+++ b/drivers/usb/typec/ucsi/ucsi.h
-@@ -22,6 +22,17 @@ struct ucsi_altmode;
- #define UCSI_CONTROL 8
- #define UCSI_MESSAGE_IN 16
- #define UCSI_MESSAGE_OUT 32
-+#define UCSIv2_MESSAGE_OUT 272
-+
-+/* UCSI versions */
-+#define UCSI_VERSION_1_2 0x0120
-+#define UCSI_VERSION_2_0 0x0200
-+#define UCSI_VERSION_2_1 0x0210
-+#define UCSI_VERSION_3_0 0x0300
-+
-+#define UCSI_BCD_GET_MAJOR(_v_) (((_v_) >> 8) & 0xFF)
-+#define UCSI_BCD_GET_MINOR(_v_) (((_v_) >> 4) & 0x0F)
-+#define UCSI_BCD_GET_SUBMINOR(_v_) ((_v_) & 0x0F)
-
- /* Command Status and Connector Change Indication (CCI) bits */
- #define UCSI_CCI_CONNECTOR(_c_) (((_c_) & GENMASK(7, 1)) >> 1)
---
-2.43.0
-
+++ /dev/null
-From 2b7dac8c8d8bff1282823c9c9f8d424f1c610650 Mon Sep 17 00:00:00 2001
-From: Sasha Levin <sashal@kernel.org>
-Date: Tue, 20 Feb 2024 18:43:41 +0100
-Subject: x86/xen: attempt to inflate the memory balloon on PVH
-MIME-Version: 1.0
-Content-Type: text/plain; charset=UTF-8
-Content-Transfer-Encoding: 8bit
-
-From: Roger Pau Monne <roger.pau@citrix.com>
-
-[ Upstream commit 38620fc4e8934f1801c7811ef39a041914ac4c1d ]
-
-When running as PVH or HVM Linux will use holes in the memory map as scratch
-space to map grants, foreign domain pages and possibly miscellaneous other
-stuff. However the usage of such memory map holes for Xen purposes can be
-problematic. The request of holesby Xen happen quite early in the kernel boot
-process (grant table setup already uses scratch map space), and it's possible
-that by then not all devices have reclaimed their MMIO space. It's not
-unlikely for chunks of Xen scratch map space to end up using PCI bridge MMIO
-window memory, which (as expected) causes quite a lot of issues in the system.
-
-At least for PVH dom0 we have the possibility of using regions marked as
-UNUSABLE in the e820 memory map. Either if the region is UNUSABLE in the
-native memory map, or it has been converted into UNUSABLE in order to hide RAM
-regions from dom0, the second stage translation page-tables can populate those
-areas without issues.
-
-PV already has this kind of logic, where the balloon driver is inflated at
-boot. Re-use the current logic in order to also inflate it when running as
-PVH. onvert UNUSABLE regions up to the ratio specified in EXTRA_MEM_RATIO to
-RAM, while reserving them using xen_add_extra_mem() (which is also moved so
-it's no longer tied to CONFIG_PV).
-
-[jgross: fixed build for CONFIG_PVH without CONFIG_XEN_PVH]
-
-Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
-Reviewed-by: Juergen Gross <jgross@suse.com>
-Link: https://lore.kernel.org/r/20240220174341.56131-1-roger.pau@citrix.com
-Signed-off-by: Juergen Gross <jgross@suse.com>
-Signed-off-by: Sasha Levin <sashal@kernel.org>
----
- arch/x86/include/asm/xen/hypervisor.h | 5 ++
- arch/x86/platform/pvh/enlighten.c | 3 ++
- arch/x86/xen/enlighten.c | 32 +++++++++++++
- arch/x86/xen/enlighten_pvh.c | 68 +++++++++++++++++++++++++++
- arch/x86/xen/setup.c | 44 -----------------
- arch/x86/xen/xen-ops.h | 14 ++++++
- drivers/xen/balloon.c | 2 -
- 7 files changed, 122 insertions(+), 46 deletions(-)
-
-diff --git a/arch/x86/include/asm/xen/hypervisor.h b/arch/x86/include/asm/xen/hypervisor.h
-index 16f548a661cf6..b3bfefdb7793a 100644
---- a/arch/x86/include/asm/xen/hypervisor.h
-+++ b/arch/x86/include/asm/xen/hypervisor.h
-@@ -59,6 +59,11 @@ void xen_arch_unregister_cpu(int num);
- #ifdef CONFIG_PVH
- void __init xen_pvh_init(struct boot_params *boot_params);
- void __init mem_map_via_hcall(struct boot_params *boot_params_p);
-+#ifdef CONFIG_XEN_PVH
-+void __init xen_reserve_extra_memory(struct boot_params *bootp);
-+#else
-+static inline void xen_reserve_extra_memory(struct boot_params *bootp) { }
-+#endif
- #endif
-
- #endif /* _ASM_X86_XEN_HYPERVISOR_H */
-diff --git a/arch/x86/platform/pvh/enlighten.c b/arch/x86/platform/pvh/enlighten.c
-index ed0442e354344..f15dc1e3ad7dd 100644
---- a/arch/x86/platform/pvh/enlighten.c
-+++ b/arch/x86/platform/pvh/enlighten.c
-@@ -74,6 +74,9 @@ static void __init init_pvh_bootparams(bool xen_guest)
- } else
- xen_raw_printk("Warning: Can fit ISA range into e820\n");
-
-+ if (xen_guest)
-+ xen_reserve_extra_memory(&pvh_bootparams);
-+
- pvh_bootparams.hdr.cmd_line_ptr =
- pvh_start_info.cmdline_paddr;
-
-diff --git a/arch/x86/xen/enlighten.c b/arch/x86/xen/enlighten.c
-index 3c61bb98c10e2..a01ca255b0c64 100644
---- a/arch/x86/xen/enlighten.c
-+++ b/arch/x86/xen/enlighten.c
-@@ -6,6 +6,7 @@
- #include <linux/console.h>
- #include <linux/cpu.h>
- #include <linux/kexec.h>
-+#include <linux/memblock.h>
- #include <linux/slab.h>
- #include <linux/panic_notifier.h>
-
-@@ -350,3 +351,34 @@ void xen_arch_unregister_cpu(int num)
- }
- EXPORT_SYMBOL(xen_arch_unregister_cpu);
- #endif
-+
-+/* Amount of extra memory space we add to the e820 ranges */
-+struct xen_memory_region xen_extra_mem[XEN_EXTRA_MEM_MAX_REGIONS] __initdata;
-+
-+void __init xen_add_extra_mem(unsigned long start_pfn, unsigned long n_pfns)
-+{
-+ unsigned int i;
-+
-+ /*
-+ * No need to check for zero size, should happen rarely and will only
-+ * write a new entry regarded to be unused due to zero size.
-+ */
-+ for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) {
-+ /* Add new region. */
-+ if (xen_extra_mem[i].n_pfns == 0) {
-+ xen_extra_mem[i].start_pfn = start_pfn;
-+ xen_extra_mem[i].n_pfns = n_pfns;
-+ break;
-+ }
-+ /* Append to existing region. */
-+ if (xen_extra_mem[i].start_pfn + xen_extra_mem[i].n_pfns ==
-+ start_pfn) {
-+ xen_extra_mem[i].n_pfns += n_pfns;
-+ break;
-+ }
-+ }
-+ if (i == XEN_EXTRA_MEM_MAX_REGIONS)
-+ printk(KERN_WARNING "Warning: not enough extra memory regions\n");
-+
-+ memblock_reserve(PFN_PHYS(start_pfn), PFN_PHYS(n_pfns));
-+}
-diff --git a/arch/x86/xen/enlighten_pvh.c b/arch/x86/xen/enlighten_pvh.c
-index ada3868c02c23..c28f073c1df52 100644
---- a/arch/x86/xen/enlighten_pvh.c
-+++ b/arch/x86/xen/enlighten_pvh.c
-@@ -1,6 +1,7 @@
- // SPDX-License-Identifier: GPL-2.0
- #include <linux/acpi.h>
- #include <linux/export.h>
-+#include <linux/mm.h>
-
- #include <xen/hvc-console.h>
-
-@@ -72,3 +73,70 @@ void __init mem_map_via_hcall(struct boot_params *boot_params_p)
- }
- boot_params_p->e820_entries = memmap.nr_entries;
- }
-+
-+/*
-+ * Reserve e820 UNUSABLE regions to inflate the memory balloon.
-+ *
-+ * On PVH dom0 the host memory map is used, RAM regions available to dom0 are
-+ * located as the same place as in the native memory map, but since dom0 gets
-+ * less memory than the total amount of host RAM the ranges that can't be
-+ * populated are converted from RAM -> UNUSABLE. Use such regions (up to the
-+ * ratio signaled in EXTRA_MEM_RATIO) in order to inflate the balloon driver at
-+ * boot. Doing so prevents the guest (even if just temporary) from using holes
-+ * in the memory map in order to map grants or foreign addresses, and
-+ * hopefully limits the risk of a clash with a device MMIO region. Ideally the
-+ * hypervisor should notify us which memory ranges are suitable for creating
-+ * foreign mappings, but that's not yet implemented.
-+ */
-+void __init xen_reserve_extra_memory(struct boot_params *bootp)
-+{
-+ unsigned int i, ram_pages = 0, extra_pages;
-+
-+ for (i = 0; i < bootp->e820_entries; i++) {
-+ struct boot_e820_entry *e = &bootp->e820_table[i];
-+
-+ if (e->type != E820_TYPE_RAM)
-+ continue;
-+ ram_pages += PFN_DOWN(e->addr + e->size) - PFN_UP(e->addr);
-+ }
-+
-+ /* Max amount of extra memory. */
-+ extra_pages = EXTRA_MEM_RATIO * ram_pages;
-+
-+ /*
-+ * Convert UNUSABLE ranges to RAM and reserve them for foreign mapping
-+ * purposes.
-+ */
-+ for (i = 0; i < bootp->e820_entries && extra_pages; i++) {
-+ struct boot_e820_entry *e = &bootp->e820_table[i];
-+ unsigned long pages;
-+
-+ if (e->type != E820_TYPE_UNUSABLE)
-+ continue;
-+
-+ pages = min(extra_pages,
-+ PFN_DOWN(e->addr + e->size) - PFN_UP(e->addr));
-+
-+ if (pages != (PFN_DOWN(e->addr + e->size) - PFN_UP(e->addr))) {
-+ struct boot_e820_entry *next;
-+
-+ if (bootp->e820_entries ==
-+ ARRAY_SIZE(bootp->e820_table))
-+ /* No space left to split - skip region. */
-+ continue;
-+
-+ /* Split entry. */
-+ next = e + 1;
-+ memmove(next, e,
-+ (bootp->e820_entries - i) * sizeof(*e));
-+ bootp->e820_entries++;
-+ next->addr = PAGE_ALIGN(e->addr) + PFN_PHYS(pages);
-+ e->size = next->addr - e->addr;
-+ next->size -= e->size;
-+ }
-+ e->type = E820_TYPE_RAM;
-+ extra_pages -= pages;
-+
-+ xen_add_extra_mem(PFN_UP(e->addr), pages);
-+ }
-+}
-diff --git a/arch/x86/xen/setup.c b/arch/x86/xen/setup.c
-index 8db26f10fb1d9..72ab4a2f029bb 100644
---- a/arch/x86/xen/setup.c
-+++ b/arch/x86/xen/setup.c
-@@ -37,9 +37,6 @@
-
- #define GB(x) ((uint64_t)(x) * 1024 * 1024 * 1024)
-
--/* Amount of extra memory space we add to the e820 ranges */
--struct xen_memory_region xen_extra_mem[XEN_EXTRA_MEM_MAX_REGIONS] __initdata;
--
- /* Number of pages released from the initial allocation. */
- unsigned long xen_released_pages;
-
-@@ -60,18 +57,6 @@ static struct {
- } xen_remap_buf __initdata __aligned(PAGE_SIZE);
- static unsigned long xen_remap_mfn __initdata = INVALID_P2M_ENTRY;
-
--/*
-- * The maximum amount of extra memory compared to the base size. The
-- * main scaling factor is the size of struct page. At extreme ratios
-- * of base:extra, all the base memory can be filled with page
-- * structures for the extra memory, leaving no space for anything
-- * else.
-- *
-- * 10x seems like a reasonable balance between scaling flexibility and
-- * leaving a practically usable system.
-- */
--#define EXTRA_MEM_RATIO (10)
--
- static bool xen_512gb_limit __initdata = IS_ENABLED(CONFIG_XEN_512GB);
-
- static void __init xen_parse_512gb(void)
-@@ -92,35 +77,6 @@ static void __init xen_parse_512gb(void)
- xen_512gb_limit = val;
- }
-
--static void __init xen_add_extra_mem(unsigned long start_pfn,
-- unsigned long n_pfns)
--{
-- int i;
--
-- /*
-- * No need to check for zero size, should happen rarely and will only
-- * write a new entry regarded to be unused due to zero size.
-- */
-- for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) {
-- /* Add new region. */
-- if (xen_extra_mem[i].n_pfns == 0) {
-- xen_extra_mem[i].start_pfn = start_pfn;
-- xen_extra_mem[i].n_pfns = n_pfns;
-- break;
-- }
-- /* Append to existing region. */
-- if (xen_extra_mem[i].start_pfn + xen_extra_mem[i].n_pfns ==
-- start_pfn) {
-- xen_extra_mem[i].n_pfns += n_pfns;
-- break;
-- }
-- }
-- if (i == XEN_EXTRA_MEM_MAX_REGIONS)
-- printk(KERN_WARNING "Warning: not enough extra memory regions\n");
--
-- memblock_reserve(PFN_PHYS(start_pfn), PFN_PHYS(n_pfns));
--}
--
- static void __init xen_del_extra_mem(unsigned long start_pfn,
- unsigned long n_pfns)
- {
-diff --git a/arch/x86/xen/xen-ops.h b/arch/x86/xen/xen-ops.h
-index b2b2f4315b78d..8c715a4f06c02 100644
---- a/arch/x86/xen/xen-ops.h
-+++ b/arch/x86/xen/xen-ops.h
-@@ -162,4 +162,18 @@ void xen_hvm_post_suspend(int suspend_cancelled);
- static inline void xen_hvm_post_suspend(int suspend_cancelled) {}
- #endif
-
-+/*
-+ * The maximum amount of extra memory compared to the base size. The
-+ * main scaling factor is the size of struct page. At extreme ratios
-+ * of base:extra, all the base memory can be filled with page
-+ * structures for the extra memory, leaving no space for anything
-+ * else.
-+ *
-+ * 10x seems like a reasonable balance between scaling flexibility and
-+ * leaving a practically usable system.
-+ */
-+#define EXTRA_MEM_RATIO (10)
-+
-+void xen_add_extra_mem(unsigned long start_pfn, unsigned long n_pfns);
-+
- #endif /* XEN_OPS_H */
-diff --git a/drivers/xen/balloon.c b/drivers/xen/balloon.c
-index 617a7f4f07a80..4e23b398e2882 100644
---- a/drivers/xen/balloon.c
-+++ b/drivers/xen/balloon.c
-@@ -691,7 +691,6 @@ EXPORT_SYMBOL(xen_free_ballooned_pages);
-
- static void __init balloon_add_regions(void)
- {
--#if defined(CONFIG_XEN_PV)
- unsigned long start_pfn, pages;
- unsigned long pfn, extra_pfn_end;
- unsigned int i;
-@@ -715,7 +714,6 @@ static void __init balloon_add_regions(void)
-
- balloon_stats.total_pages += extra_pfn_end - start_pfn;
- }
--#endif
- }
-
- static int __init balloon_init(void)
---
-2.43.0
-