early: usb: xhci-dbc: Handle out of bounds xhci-xdbc capability
PATCH v2 1/1 early: usb: xhci-dbc: Handle out of bounds xhci-xdbc capability
Currently, the early xhci-dbc assumes that the entire PCIe memory IO can be entirely mapped within the fixed boot time mappings dictated by NR_FIX_BTMAPS. This patch handles the case where the PCIe memory IO size can be larger than the fixed boot time mappings and query the xhci debug extended capability in xdbc_map_pci_mmio().
This commit ensures that the xHCI debug capability can still be queried when the PCIe memory IO space exceeds the fixmap size. In this scenario, the base address is mapped uptil fixmap size and debug capabilities are queried thereafter. Iterating over the entire PCIe BAR address size is left for future improvement as and when, such a case arises.
Additionally, this brings the need to track the early_ioremap() mapped size separately hence, introduce additional struct member xhci_base_length in struct xdbc_state.
---
Changes in v2:
- Commit message update
- Changed and tested behaviour for mapping only first fixmap_size of PCIe Base address size instead of iteration over entire BAR size
- Updated comments
- Updated XDBC MAPPING SIZE to 64
v1:
- https://lore.kernel.org/all/[email protected]/
--- drivers/usb/early/xhci-dbc.c | 78 ++++++++++++++++++++++++++++++++---- drivers/usb/early/xhci-dbc.h | 1 + 2 files changed, 71 insertions(+), 8 deletions(-)
drivers/usb/early/xhci-dbc.c +70 -8
@@ -35,10 +35,23 @@ static bool early_console_keep;static inline void xdbc_trace (const char *fmt, ...) { }#endif /* XDBC_TRACE */ +/* Size of xHCI debug capability structure as per section 7.6.8 of xHCI spec. */ +#define XDBC_MAPPING_SIZE 64 + +enum xdbc_capability_flags { +XDBC_CAP_FLAG_NONE =0 , +XDBC_CAP_FLAG_LEGACY =1 <<0 , +XDBC_CAP_FLAG_PROTOCOL =1 <<1 , +XDBC_CAP_FLAG_DEBUG =1 <<2 , +}; +static void __iomem *__init xdbc_map_pci_mmio (u32 bus,u32 dev,u32 func) { -u64 val64, sz64, mask64; +u64 val64, sz64, mask64, fixmap_size, mapped_size; +enum xdbc_capability_flags cap_flags =XDBC_CAP_FLAG_NONE ; +bool found_all_caps = false;void __iomem *base; +int offset;u32 val, sz;u8 byte;
@@ -85,7 +98,56 @@ static void __iomem * __init xdbc_map_pci_mmio(u32 bus, u32 dev, u32 func) xdbc.xhci_start = val64; xdbc.xhci_length = sz64; - base =early_ioremap (val64, sz64); + + fixmap_size =NR_FIX_BTMAPS <<PAGE_SHIFT ; +if (sz64 < fixmap_size) { + xdbc.xhci_base_length = sz64; +return early_ioremap (val64, sz64); + } + +/* +* Base address size is greater than fixed size boot time mappings +* hence, map maximum allowed fixmap size from base address and +* determine if the required extended capabilities lies within the +* fixmap. +*/ + base =early_ioremap (val64, fixmap_size); + offset =xhci_find_next_ext_cap (base,0 ,0 ); + mapped_size = fixmap_size; + +while (offset < fixmap_size) { + val =readl (base + offset); +switch (XHCI_EXT_CAPS_ID (val)) { +case XHCI_EXT_CAPS_DEBUG : +if (offset +XDBC_MAPPING_SIZE < fixmap_size) + cap_flags |=XDBC_CAP_FLAG_DEBUG ; +break ; +case XHCI_EXT_CAPS_PROTOCOL : + cap_flags |=XDBC_CAP_FLAG_PROTOCOL ; +break ; +case XHCI_EXT_CAPS_LEGACY : + cap_flags |=XDBC_CAP_FLAG_LEGACY ; +break ; + } + +if ((cap_flags &XDBC_CAP_FLAG_DEBUG ) && + (cap_flags &XDBC_CAP_FLAG_PROTOCOL ) && + (cap_flags &XDBC_CAP_FLAG_LEGACY )) { + found_all_caps = true; +break ; + } + + offset =xhci_find_next_ext_cap (base, offset,0 ); +if (!offset) +break ; + } + +if (found_all_caps) { + xdbc.xhci_base_length = fixmap_size; + }else { + xdbc.xhci_base_length =0 ; + base =NULL ; + }return base; }
@@ -643,9 +705,9 @@ int __init early_xdbc_parse_parameter(char *s, int keep_early) offset =xhci_find_next_ext_cap (xdbc.xhci_base,0 ,XHCI_EXT_CAPS_DEBUG );if (!offset) {pr_notice ("xhci host doesn't support debug capability\n" ); -early_iounmap (xdbc.xhci_base, xdbc.xhci_length); +early_iounmap (xdbc.xhci_base, xdbc.xhci_base_length); xdbc.xhci_base =NULL ; - xdbc.xhci_length =0 ; + xdbc.xhci_base_length =0 ;return -ENODEV ; }
@@ -682,9 +744,9 @@ int __init early_xdbc_setup_hardware(void) xdbc.table_base =NULL ; xdbc.out_buf =NULL ; -early_iounmap (xdbc.xhci_base, xdbc.xhci_length); +early_iounmap (xdbc.xhci_base, xdbc.xhci_base_length); xdbc.xhci_base =NULL ; - xdbc.xhci_length =0 ; + xdbc.xhci_base_length =0 ; }return ret;
@@ -987,7 +1049,7 @@ static int __init xdbc_init(void) }raw_spin_lock_irqsave (&xdbc.lock, flags); -early_iounmap (xdbc.xhci_base, xdbc.xhci_length); +early_iounmap (xdbc.xhci_base, xdbc.xhci_base_length); xdbc.xhci_base = base; offset =xhci_find_next_ext_cap (xdbc.xhci_base,0 ,XHCI_EXT_CAPS_DEBUG ); xdbc.xdbc_reg = (struct xdbc_regs __iomem *)(xdbc.xhci_base + offset);
@@ -1004,7 +1066,7 @@ static int __init xdbc_init(void)memblock_phys_free (xdbc.table_dma,PAGE_SIZE );memblock_phys_free (xdbc.out_dma,PAGE_SIZE );writel (0 , &xdbc.xdbc_reg->control); -early_iounmap (xdbc.xhci_base, xdbc.xhci_length); +early_iounmap (xdbc.xhci_base, xdbc.xhci_base_length);return ret; }
drivers/usb/early/xhci-dbc.h +1 -0
@@ -144,6 +144,7 @@ struct xdbc_state {u32 dev;u32 func;void __iomem *xhci_base; +size_t xhci_base_length;u64 xhci_start;size_t xhci_length;int port_number;
Signature · 1 line
2.55.0
Quoted 16 lines · Umang Jain
Currently, the early xhci-dbc assumes that the entire PCIe memory IO can be entirely mapped within the fixed boot time mappings dictated by
NR_FIX_BTMAPS. This patch handles the case where the PCIe memory IO size can be larger than the fixed boot time mappings and query the xhci debug extended capability inxdbc_map_pci_mmio().This commit ensures that the xHCI debug capability can still be queried when the PCIe memory IO space exceeds the fixmap size. In this scenario, the base address is mapped uptil fixmap size and debug capabilities are queried thereafter. Iterating over the entire PCIe BAR address size is left for future improvement as and when, such a case arises.
Additionally, this brings the need to track the
early_ioremap()mapped size separately hence, introduce additional struct memberxhci_base_lengthin structxdbc_state.
Quoted 8 lines
Signed-off-by Umang Jain <[email protected]>---
Changes in v2:
- Commit message update
- Changed and tested behaviour for mapping only firstfixmap_sizeof PCIe Base address size instead of iteration over entire BAR size
- Updated comments
- Updated XDBC MAPPING SIZE to 64
Thanks, this starts to look good.
Some small fixes and cleanups still needed.
Quoted 20 lines
v1:
- https://lore.kernel.org/all/[email protected]/--- drivers/usb/early/xhci-dbc.c | 78 ++++++++++++++++++++++++++++++++---- drivers/usb/early/xhci-dbc.h | 1 + 2 files changed, 71 insertions(+), 8 deletions(-)drivers/usb/early/xhci-dbc.c +4
-0@@ -35,10 +35,23 @@ static bool early_console_keep;static inline void xdbc_trace (const char *fmt, ...) { }#endif /* XDBC_TRACE */ +/* Size of xHCI debug capability structure as per section 7.6.8 of xHCI spec. */ +#define XDBC_MAPPING_SIZE 64 + +enum xdbc_capability_flags {
drivers/usb/early/xhci-dbc.c static bool early_console_keep; · line 50
Quoted 10 lines
drivers/usb/early/xhci-dbc.c +7
-1+XDBC_CAP_FLAG_NONE =0 , +XDBC_CAP_FLAG_LEGACY =1 <<0 , +XDBC_CAP_FLAG_PROTOCOL =1 <<1 , + XDBC_CAP_FLAG_DEBUG = 1 << 2, +}; +static void __iomem *__init xdbc_map_pci_mmio (u32 bus,u32 dev,u32 func) { -u64 val64, sz64, mask64; +u64 val64, sz64, mask64, fixmap_size, mapped_size;
mapped_size in no longer needed. Can be removed
Quoted 16 lines
drivers/usb/early/xhci-dbc.c +7
-1+enum xdbc_capability_flags cap_flags =XDBC_CAP_FLAG_NONE ; +bool found_all_caps = false;void __iomem *base; +int offset;u32 val, sz;u8 byte;@@ -85,7 +98,56 @@ static void __iomem * __init xdbc_map_pci_mmio(u32 bus, u32 dev, u32 func) xdbc.xhci_start = val64; xdbc.xhci_length = sz64; - base =early_ioremap (val64, sz64); + + fixmap_size =NR_FIX_BTMAPS <<PAGE_SHIFT ; +if (sz64 < fixmap_size) { + xdbc.xhci_base_length = sz64;
drivers/usb/early/xhci-dbc.c static void __iomem * __init xdbc_map_pci_mmio(u32 bus, u32 dev, u32 func) · line 114
Quoted 10 lines
drivers/usb/early/xhci-dbc.c +10
-0+return early_ioremap (val64, sz64); + } + +/* +* Base address size is greater than fixed size boot time mappings +* hence, map maximum allowed fixmap size from base address and +* determine if the required extended capabilities lies within the +* fixmap. +*/ + base =early_ioremap (val64, fixmap_size);
if (!base)
return NULL;
drivers/usb/early/xhci-dbc.c static void __iomem * __init xdbc_map_pci_mmio(u32 bus, u32 dev, u32 func) · line 116
Quoted 2 lines
drivers/usb/early/xhci-dbc.c +2
-0+ offset =xhci_find_next_ext_cap (base,0 ,0 ); + mapped_size = fixmap_size;
mapped_size not used
Quoted 23 lines
drivers/usb/early/xhci-dbc.c +23
-0+ +while (offset < fixmap_size) { + val =readl (base + offset); +switch (XHCI_EXT_CAPS_ID (val)) { +case XHCI_EXT_CAPS_DEBUG : +if (offset +XDBC_MAPPING_SIZE < fixmap_size) + cap_flags |=XDBC_CAP_FLAG_DEBUG ; +break ; +case XHCI_EXT_CAPS_PROTOCOL : + cap_flags |=XDBC_CAP_FLAG_PROTOCOL ; +break ; +case XHCI_EXT_CAPS_LEGACY : + cap_flags |=XDBC_CAP_FLAG_LEGACY ; +break ; + } + +if ((cap_flags &XDBC_CAP_FLAG_DEBUG ) && + (cap_flags &XDBC_CAP_FLAG_PROTOCOL ) && + (cap_flags &XDBC_CAP_FLAG_LEGACY )) { + found_all_caps = true; +break ; + } +
drivers/usb/early/xhci-dbc.c static void __iomem * __init xdbc_map_pci_mmio(u32 bus, u32 dev, u32 func) · line 149
Quoted 10 lines
drivers/usb/early/xhci-dbc.c +10
-0+ offset =xhci_find_next_ext_cap (base, offset,0 ); +if (!offset) +break ; + } + +if (found_all_caps) { + xdbc.xhci_base_length = fixmap_size; + }else { + xdbc.xhci_base_length =0 ; + base =NULL ;
we need to early_iounmap() in this fail path before returning NULL.
drivers/usb/early/xhci-dbc.c static void __iomem * __init xdbc_map_pci_mmio(u32 bus, u32 dev, u32 func) · line 153
Quoted 4 lines
drivers/usb/early/xhci-dbc.c +1
-0+ }return base; }
Thanks
Mathias
Quoted 53 lines · Mathias Nyman
Quoted 16 lines · Umang Jain
Currently, the early xhci-dbc assumes that the entire PCIe memory IO can be entirely mapped within the fixed boot time mappings dictated by
NR_FIX_BTMAPS. This patch handles the case where the PCIe memory IO size can be larger than the fixed boot time mappings and query the xhci debug extended capability inxdbc_map_pci_mmio().This commit ensures that the xHCI debug capability can still be queried when the PCIe memory IO space exceeds the fixmap size. In this scenario, the base address is mapped uptil fixmap size and debug capabilities are queried thereafter. Iterating over the entire PCIe BAR address size is left for future improvement as and when, such a case arises.
Additionally, this brings the need to track the
early_ioremap()mapped size separately hence, introduce additional struct memberxhci_base_lengthin structxdbc_state.Quoted 8 lines
Signed-off-by Umang Jain <[email protected]>---
Changes in v2:
- Commit message update
- Changed and tested behaviour for mapping only firstfixmap_sizeof PCIe Base address size instead of iteration over entire BAR size
- Updated comments
- Updated XDBC MAPPING SIZE to 64Thanks, this starts to look good.
Some small fixes and cleanups still needed.
Quoted 23 lines
v1:
- https://lore.kernel.org/all/[email protected]/--- drivers/usb/early/xhci-dbc.c | 78 ++++++++++++++++++++++++++++++++---- drivers/usb/early/xhci-dbc.h | 1 + 2 files changed, 71 insertions(+), 8 deletions(-)drivers/usb/early/xhci-dbc.c +7
-0@@ -35,10 +35,23 @@ static bool early_console_keep;static inline void xdbc_trace (const char *fmt, ...) { }#endif /* XDBC_TRACE */ +/* Size of xHCI debug capability structure as per section 7.6.8 of xHCI spec. */ +#define XDBC_MAPPING_SIZE 64 + +enum xdbc_capability_flags { +XDBC_CAP_FLAG_NONE =0 , +XDBC_CAP_FLAG_LEGACY =1 <<0 , +XDBC_CAP_FLAG_PROTOCOL =1 <<1 , +XDBC_CAP_FLAG_DEBUG =1 <<2 ,
Quoted 8 lines
drivers/usb/early/xhci-dbc.c static bool early_console_keep; · line 50
Quoted 6 lines
drivers/usb/early/xhci-dbc.c +3
-1+}; +static void __iomem *__init xdbc_map_pci_mmio (u32 bus,u32 dev,u32 func) { -u64 val64, sz64, mask64; +u64 val64, sz64, mask64, fixmap_size, mapped_size;
mapped_sizein no longer needed. Can be removed
ah right, fallout from last version / changing implementation
Quoted 61 lines
Quoted 15 lines
drivers/usb/early/xhci-dbc.c +7
-1+enum xdbc_capability_flags cap_flags =XDBC_CAP_FLAG_NONE ; +bool found_all_caps = false;void __iomem *base; +int offset;u32 val, sz;u8 byte; @@ -85,7 +98,56 @@ static void __iomem * __init xdbc_map_pci_mmio(u32 bus, u32 dev, u32 func) xdbc.xhci_start = val64; xdbc.xhci_length = sz64; - base =early_ioremap (val64, sz64); + + fixmap_size =NR_FIX_BTMAPS <<PAGE_SHIFT ; +if (sz64 < fixmap_size) { + xdbc.xhci_base_length = sz64;drivers/usb/early/xhci-dbc.c static bool early_console_keep;
Quoted 10 lines
drivers/usb/early/xhci-dbc.c +10
-0+return early_ioremap (val64, sz64); + } + +/* +* Base address size is greater than fixed size boot time mappings +* hence, map maximum allowed fixmap size from base address and +* determine if the required extended capabilities lies within the +* fixmap. +*/ + base =early_ioremap (val64, fixmap_size);if (!base)
return NULL;drivers/usb/early/xhci-dbc.c static void __iomem * __init xdbc_map_pci_mmio(u32 bus, u32 dev, u32 func) · line 116
Quoted 2 lines
drivers/usb/early/xhci-dbc.c +2
-0+ offset =xhci_find_next_ext_cap (base,0 ,0 ); + mapped_size = fixmap_size;
mapped_sizenot usedQuoted 27 lines
drivers/usb/early/xhci-dbc.c +27
-0+ +while (offset < fixmap_size) { + val =readl (base + offset); +switch (XHCI_EXT_CAPS_ID (val)) { +case XHCI_EXT_CAPS_DEBUG : +if (offset +XDBC_MAPPING_SIZE < fixmap_size) + cap_flags |=XDBC_CAP_FLAG_DEBUG ; +break ; +case XHCI_EXT_CAPS_PROTOCOL : + cap_flags |=XDBC_CAP_FLAG_PROTOCOL ; +break ; +case XHCI_EXT_CAPS_LEGACY : + cap_flags |=XDBC_CAP_FLAG_LEGACY ; +break ; + } + +if ((cap_flags &XDBC_CAP_FLAG_DEBUG ) && + (cap_flags &XDBC_CAP_FLAG_PROTOCOL ) && + (cap_flags &XDBC_CAP_FLAG_LEGACY )) { + found_all_caps = true; +break ; + } + + offset =xhci_find_next_ext_cap (base, offset,0 ); +if (!offset) +break ; + }
Quoted 8 lines
drivers/usb/early/xhci-dbc.c static void __iomem * __init xdbc_map_pci_mmio(u32 bus, u32 dev, u32 func) · line 149
Quoted 6 lines
drivers/usb/early/xhci-dbc.c +6
-0+ +if (found_all_caps) { + xdbc.xhci_base_length = fixmap_size; + }else { + xdbc.xhci_base_length =0 ; + base =NULL ;we need to
early_iounmap()in this fail path before returning NULL.
good catch ;-)
Thanks for review, I'll address and send out new version soon.
Are we sure we intend to keep this implementation? And leave passing over the entire PCIe BAR range for later ?
Quoted 7 lines
drivers/usb/early/xhci-dbc.c static void __iomem * __init xdbc_map_pci_mmio(u32 bus, u32 dev, u32 func) · line 153
Quoted 4 lines
drivers/usb/early/xhci-dbc.c +1
-0+ }return base; }Thanks
Mathias