iommu/arm-smmu-v3: Fix L1 stream table index calculation for 32-bit sid size
Subsystems (2)
PATCH v2 1/1 iommu/arm-smmu-v3: Fix L1 stream table index calculation for 32-bit sid size
The commit ce410410f1a7 ("iommu/arm-smmu-v3: Add arm_smmu_strtab_l1/2_idx()") calculated the last index of L1 stream table by 1 << smmu->sid_bits. 1 is 32 bit value.
However some platforms, for example, AmpereOne, have 32-bit stream id size. This resulted in ouf-of-bound shift. The disassembly of shift is:
ldr w2, [x19, 828] //, smmu_7(D)->sid_bits
mov w20, 1
lsl w20, w20, w2According to ARM spec, if the registers are 32 bit, the instruction actually does:
dest = src << (shift % 32)
So it actually shifted by zero bit.
This caused v6.12-rc1 failed to boot on AmpereOne and other platform [1].
UBSAN also reported:
UBSAN: shift-out-of-bounds in drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c:3628:29 shift exponent 32 is too large for 32-bit type 'int' CPU: 70 UID: 0 PID: 1 Comm: swapper/0 Not tainted 6.12.0-rc1 #4 Hardware name: ZOLLNER SUNMOONLAKE/SunMoon Lake, BIOS 00.00. 2024-08-28 18:42:45 08/28/2024 Call trace: dump_backtrace+0xdc/0x140 show_stack+0x20/0x40 dump_stack_lvl+0x60/0x80 dump_stack+0x18/0x28 ubsan_epilogue+0x10/0x48 __ubsan_handle_shift_out_of_bounds+0xd8/0x1a0 arm_smmu_init_structures+0x374/0x3c8 arm_smmu_device_probe+0x208/0x600 platform_probe+0x70/0xe8 really_probe+0xc8/0x3a0 __driver_probe_device+0x84/0x160 driver_probe_device+0x44/0x130 __driver_attach+0xcc/0x208 bus_for_each_dev+0x84/0x100 driver_attach+0x2c/0x40 bus_add_driver+0x158/0x290 driver_register+0x70/0x138 __platform_driver_register+0x2c/0x40 arm_smmu_driver_init+0x28/0x40 do_one_initcall+0x60/0x318 do_initcalls+0x198/0x1e0 kernel_init_freeable+0x18c/0x1e8 kernel_init+0x28/0x160 ret_from_fork+0x10/0x20
Using 64 bit immediate when doing shift can solve the problem. The disassembly after the fix looks like:
ldr w20, [x19, 828] //, smmu_7(D)->sid_bits
mov x0, 1
lsl x0, x0, x20There are a couple of problematic places, extracted the shift into a helper.
[1] https://lore.kernel.org/lkml/[email protected]/
--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 8 +++++--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 5 +++++ 2 files changed, 10 insertions(+), 3 deletions(-)
v2: * Extracted the shift into a helper per Jason Gunthorpe.
* Covered more places per Nicolin Chen and Jason Gunthorpe.
* Used 1ULL instead of 1UL to guarantee 64 bit per Robin Murphy.
* Made the subject more general since this is not AmpereOne specific problem per the report from James Morse.
* Collected t-b tag from James Morse.
* Added Fixes tag in commit log.
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +5 -3
@@ -3624,8 +3624,9 @@ static int arm_smmu_init_strtab_2lvl(struct arm_smmu_device *smmu) {u32 l1size;struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg; +unsigned int max_sid =arm_smmu_strtab_max_sid (smmu);unsigned int last_sid_idx = -arm_smmu_strtab_l1_idx ((1 << smmu->sid_bits) -1 ); +arm_smmu_strtab_l1_idx (max_sid -1 );/* Calculate the L1 size, capped to the SIDSIZE. */ cfg->l2.num_l1_ents =min (last_sid_idx +1 ,STRTAB_MAX_L1_ENTRIES );
@@ -3657,8 +3658,9 @@ static int arm_smmu_init_strtab_linear(struct arm_smmu_device *smmu) {u32 size;struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg; +unsigned int max_sid =arm_smmu_strtab_max_sid (smmu); - size = (1 << smmu->sid_bits) *sizeof (struct arm_smmu_ste ); + size = max_sid *sizeof (struct arm_smmu_ste ); cfg->linear.table =dmam_alloc_coherent (smmu->dev, size, &cfg->linear.ste_dma,GFP_KERNEL );
@@ -3668,7 +3670,7 @@ static int arm_smmu_init_strtab_linear(struct arm_smmu_device *smmu) size);return -ENOMEM ; } - cfg->linear.num_ents =1 << smmu->sid_bits; + cfg->linear.num_ents = max_sid;arm_smmu_init_initial_stes (cfg->linear.table, cfg->linear.num_ents);return 0 ;
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +5 -0
@@ -853,6 +853,11 @@ struct arm_smmu_master_domain {ioasid_t ssid; }; +static inline unsigned int arm_smmu_strtab_max_sid (struct arm_smmu_device *smmu) +{ +return (1ULL << smmu->sid_bits); +} +static inline struct arm_smmu_domain *to_smmu_domain (struct iommu_domain *dom) { return container_of(dom, struct arm_smmu_domain, domain);
Signature · 1 line
2.41.0
Quoted 8 lines · Yang Shi
Using 64 bit immediate when doing shift can solve the problem. The disassembly after the fix looks like:
ldr w20, [x19, 828] //, smmu_7(D)->sid_bits mov x0, 1 lsl x0, x0, x20There are a couple of problematic places, extracted the shift into a helper.
Quoted 8 lines
[1] https://lore.kernel.org/lkml/[email protected]/
Fixes ce410410f1a7 ("iommu/arm-smmu-v3: Add arm_smmu_strtab_l1/2_idx()")Tested-by James Morse <[email protected]>Signed-off-by Yang Shi <[email protected]>--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 8 +++++--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 5 +++++ 2 files changed, 10 insertions(+), 3 deletions(-)
Jason
I think both v1 and v2 are missing [email protected] in CC.
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h struct arm_smmu_master_domain { · line 860
Quoted 5 lines · Yang Shi
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +5
-0+static inline unsigned int arm_smmu_strtab_max_sid (struct arm_smmu_device *smmu) +{ +return (1ULL << smmu->sid_bits); +} +
Hmm, why ULL gets truncated to unsigned int here?
Thanks
Nicolin
Quoted 1 line · Nicolin Chen
I think both v1 and v2 are missing [email protected] in CC.
Will do it for the later revision.
Quoted 8 lines · Yang Shi
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h struct arm_smmu_master_domain { · line 860
Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +5
-0+static inline unsigned int arm_smmu_strtab_max_sid (struct arm_smmu_device *smmu) +{ +return (1ULL << smmu->sid_bits); +} +Hmm, why ULL gets truncated to unsigned int here?
No particular reason, but it should be better to not truncate here. Will fix it.
Quoted 3 lines
Thanks
Nicolin
Quoted 10 lines · Yang Shi
Quoted 7 lines · Yang Shi
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h struct arm_smmu_master_domain { · line 860
Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +5
-0+static inline unsigned int arm_smmu_strtab_max_sid (struct arm_smmu_device *smmu) +{ +return (1ULL << smmu->sid_bits); +} +Hmm, why ULL gets truncated to unsigned int here?
No particular reason, but it should be better to not truncate here. Will fix it.
Yea, and looks like we are going to do with:
static inline u64 arm_smmu_strtab_num_sids(struct arm_smmu_device *smmu);
Then let's be careful at those return-value holders too:
-----------------------------------------------------------
static int arm_smmu_init_strtab_linear(struct arm_smmu_device *smmu)
{
u32 size;
struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
size = (1 << smmu->sid_bits) * sizeof(struct arm_smmu_ste);
^^^^
overflow?
[...]
cfg->linear.num_ents = 1 << smmu->sid_bits;
^^^^^^^^
This is u32-----------------------------------------------------------
Thanks
Nicolin
Quoted 29 lines · Nicolin Chen
Quoted 10 lines · Yang Shi
Quoted 7 lines · Yang Shi
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h struct arm_smmu_master_domain { · line 860
Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +5
-0+static inline unsigned int arm_smmu_strtab_max_sid (struct arm_smmu_device *smmu) +{ +return (1ULL << smmu->sid_bits); +} +Hmm, why ULL gets truncated to unsigned int here?
No particular reason, but it should be better to not truncate here. Will fix it.
Yea, and looks like we are going to do with:
static inline u64arm_smmu_strtab_num_sids(struct arm_smmu_device *smmu);Then let's be careful at those return-value holders too:
-----------------------------------------------------------
static intarm_smmu_init_strtab_linear(struct arm_smmu_device *smmu){ u32 size; struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg; size = (1 << smmu->sid_bits) * sizeof(struct arm_smmu_ste); ^^^^ overflow? [...] cfg->linear.num_ents = 1 << smmu->sid_bits; ^^^^^^^^ This is u32
Quoted 1 line
-----------------------------------------------------------
It would make some sense to have something like:
u64 size = arm_smmu_strtab_max_sid()
/* Would require too much memory */
if (size > SZ_512M)
return -EINVAL;
Just to reject bad configuration rather than truncate the allocation and overflow STE array memory or something. Having drivers be robust to this kind of stuff is a confidential compute topic :\
Jason
Quoted 21 lines · Jason Gunthorpe
Quoted 20 lines · Nicolin Chen
Quoted 9 lines · Yang Shi
Quoted 7 lines · Yang Shi
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h struct arm_smmu_master_domain { · line 860
Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +5
-0+static inline unsigned int arm_smmu_strtab_max_sid (struct arm_smmu_device *smmu) +{ +return (1ULL << smmu->sid_bits); +} +Hmm, why ULL gets truncated to unsigned int here?
No particular reason, but it should be better to not truncate here. Will fix it.
Yea, and looks like we are going to do with:
static inline u64arm_smmu_strtab_num_sids(struct arm_smmu_device *smmu);Then let's be careful at those return-value holders too:
-----------------------------------------------------------
static intarm_smmu_init_strtab_linear(struct arm_smmu_device *smmu){ u32 size; struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
Quoted 8 lines
Quoted 8 lines
size = (1 << smmu->sid_bits) * sizeof(struct arm_smmu_ste); ^^^^ overflow? [...] cfg->linear.num_ents = 1 << smmu->sid_bits; ^^^^^^^^ This is u32-----------------------------------------------------------
Instead of relying in separate emails, I'd prefer reply in one single place.
This is linear stream table code, IIUC nobody actually implements such large sid size with linear table per the earlier discussions.
Quoted 7 lines
It would make some sense to have something like:
u64 size =
arm_smmu_strtab_max_sid()/* Would require too much memory */
if (size >SZ_512M)return -EINVAL;
This extra check is definitely fine to me and makes sense. SZ_512M is 1 << 29, so it means no hardware actually has 29 bit sid size with linear stream table. It makes sense to me. But I'm not smmu expert, so just would like to double check such configuration is not existing.
Quoted 6 lines
Just to reject bad configuration rather than truncate the allocation and overflow STE array memory or something. Having drivers be robust to this kind of stuff is a confidential compute topic :\
Jason
Quoted 29 lines · Jason Gunthorpe
Quoted 27 lines · Nicolin Chen
Quoted 9 lines · Yang Shi
Quoted 7 lines · Yang Shi
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h struct arm_smmu_master_domain { · line 860
Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +5
-0+static inline unsigned int arm_smmu_strtab_max_sid (struct arm_smmu_device *smmu) +{ +return (1ULL << smmu->sid_bits); +} +Hmm, why ULL gets truncated to unsigned int here?
No particular reason, but it should be better to not truncate here. Will fix it.
Yea, and looks like we are going to do with:
static inline u64arm_smmu_strtab_num_sids(struct arm_smmu_device *smmu);Then let's be careful at those return-value holders too:
-----------------------------------------------------------
static intarm_smmu_init_strtab_linear(struct arm_smmu_device *smmu){ u32 size; struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg; size = (1 << smmu->sid_bits) * sizeof(struct arm_smmu_ste); ^^^^ overflow? [...] cfg->linear.num_ents = 1 << smmu->sid_bits; ^^^^^^^^ This is u32Quoted 1 line
-----------------------------------------------------------
Quoted 7 lines
It would make some sense to have something like:
u64 size =
arm_smmu_strtab_max_sid()/* Would require too much memory */
if (size >SZ_512M)return -EINVAL;
Why not just check smmu->sid_bits?
For example,
if (smmu->sid_bits > 28)
return -EINVAL;
The check can happen before the shift.
Quoted 6 lines
Just to reject bad configuration rather than truncate the allocation and overflow STE array memory or something. Having drivers be robust to this kind of stuff is a confidential compute topic :\
Jason
Quoted 8 lines · Yang Shi
Quoted 7 lines
It would make some sense to have something like:
u64 size =
arm_smmu_strtab_max_sid()/* Would require too much memory */
if (size >SZ_512M)return -EINVAL;
Quoted 8 lines
Why not just check
smmu->sid_bits?For example,
if (
smmu->sid_bits> 28)
return-EINVAL;The check can happen before the shift.
Sure, but IMHO it reads a bit better to check the size computed from the helper
MAX_PAGE_ORDER is often 10, so kmalloc will always fail before we reach 28 bits of sid space.
Jason
Quoted 15 lines · Jason Gunthorpe
Quoted 14 lines · Yang Shi
Quoted 7 lines
It would make some sense to have something like:
u64 size = arm_smmu_strtab_max_sid() /* Would require too much memory */ if (size > SZ_512M) return -EINVAL;Why not just check
smmu->sid_bits?For example,
if (
smmu->sid_bits> 28)
return-EINVAL;
Quoted 6 lines
Quoted 1 line
The check can happen before the shift.
Sure, but IMHO it reads a bit better to check the size computed from the helper
MAX_PAGE_ORDERis often 10, so kmalloc will always fail before we reach 28 bits of sid space.
I'm wondering we may just use 31 instead of using some magic number:
if (smmu->sid_bits > 31)
return -EINVAL;
If I understand correctly, the check is mainly used to avoid the u64 -> u32 overflow. This check guarantee no overflow. If some crazy hardware really requests that large memory, the allocation will fail.
Quoted 2 lines
Jason
Quoted 3 lines · Yang Shi
If I understand correctly, the check is mainly used to avoid the u64 -> u32 overflow. This check guarantee no overflow. If some crazy hardware really requests that large memory, the allocation will fail.
Sure, the kalloc will print a warn on anyhow if it is too big
Jason
Quoted 5 lines · Jason Gunthorpe
Quoted 3 lines · Yang Shi
If I understand correctly, the check is mainly used to avoid the u64 -> u32 overflow. This check guarantee no overflow. If some crazy hardware really requests that large memory, the allocation will fail.
Sure, the kalloc will print a warn on anyhow if it is too big
Thank you. Will spin a new revision.
Quoted 1 line
Jason
Quoted 10 lines · Yang Shi
Quoted 5 lines · Jason Gunthorpe
Quoted 3 lines · Yang Shi
If I understand correctly, the check is mainly used to avoid the u64 -> u32 overflow. This check guarantee no overflow. If some crazy hardware really requests that large memory, the allocation will fail.
Sure, the kalloc will print a warn on anyhow if it is too big
Thank you. Will spin a new revision.
Oh wait a sec, it is not so simple, the 31 is too big because the multiply will overflow or truncate to size_t too. This is why I picked something lower.
Jason
Quoted 4 lines · Jason Gunthorpe
Quoted 3 lines · Yang Shi
Quoted 1 line · Jason Gunthorpe
On Thu, Oct 03, 2024 at 08:31:23AM -0700, Yang Shi wrote:
Quoted 8 lines
Quoted 5 lines
Quoted 4 lines
Quoted 3 lines
If I understand correctly, the check is mainly used to avoid the u64 -> u32 overflow. This check guarantee no overflow. If some crazy hardware really requests that large memory, the allocation will fail.
Sure, the kalloc will print a warn on anyhow if it is too big
Thank you. Will spin a new revision.
Oh wait a sec, it is not so simple, the 31 is too big because the multiply will overflow or truncate to
size_ttoo. This is why I picked something lower.
How about define the size as u64?
static int arm_smmu_init_strtab_linear(struct arm_smmu_device *smmu) {
- u32 size;
+ u64 size;
struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg; u32 num_sids;
It won't overflow and the large allocation will fail anyway.
Quoted 2 lines
Jason
Quoted 18 lines · Yang Shi
Quoted 4 lines · Jason Gunthorpe
Quoted 3 lines · Yang Shi
Quoted 1 line · Jason Gunthorpe
On Thu, Oct 03, 2024 at 08:31:23AM -0700, Yang Shi wrote:
Quoted 8 lines
Quoted 5 lines
Quoted 4 lines
Quoted 3 lines
If I understand correctly, the check is mainly used to avoid the u64 -> u32 overflow. This check guarantee no overflow. If some crazy hardware really requests that large memory, the allocation will fail.
Sure, the kalloc will print a warn on anyhow if it is too big
Thank you. Will spin a new revision.
Oh wait a sec, it is not so simple, the 31 is too big because the multiply will overflow or truncate to
size_ttoo. This is why I picked something lower.How about define the size as u64?
Quoted 8 lines
static int
arm_smmu_init_strtab_linear(struct arm_smmu_device *smmu){
- u32 size;
+ u64 size;
structarm_smmu_strtab_cfg*cfg =&smmu->strtab_cfg; u32num_sids;It won't overflow and the large allocation will fail anyway.
The size parameter passed to dmam_alloc_coherent() is size_t type. We may do:
+#define SIZE_4G (4 * 1024 * 1024 * 1024ULL)
+
static int arm_smmu_init_strtab_linear(struct arm_smmu_device *smmu) {
- u32 size;
+ u64 size;
struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg; - u32 num_sids;
+ u64 num_sids;
/* It is not practical to have too large stream id size for linear */ if (smmu->sid_bits > 31)
@@ -3667,6 +3669,9 @@ static int arm_smmu_init_strtab_linear(struct arm_smmu_device *smmu) num_sids = arm_smmu_strtab_num_sids(smmu);
size = num_sids * sizeof(struct arm_smmu_ste); + if (size > SIZE_4G)
+ return -EINVAL;
+
cfg->linear.table = dmam_alloc_coherent(smmu->dev, size, &cfg->linear.ste_dma,
GFP_KERNEL);
Quoted 4 lines
Quoted 3 lines
Jason
Review
Quoted 4 lines · Yang Shi
size = num_sids * sizeof(struct arm_smmu_ste);(quoted diff) +2
-0+ if (size > SIZE_4G) + return -EINVAL;
If you want to do it like that then 'size >= SIZE_MAX' is the right expression
Jason
Quoted 6 lines · Jason Gunthorpe
Review
Quoted 3 lines · Yang Shi
size = num_sids * sizeof(struct arm_smmu_ste);(quoted diff) +2
-0+ if (size > SIZE_4G) + return -EINVAL;If you want to do it like that then 'size >=
SIZE_MAX' is the right expression
Thank you. Will take it.
And with the size check we don't need to check smmu->sid_bits.
Quoted 2 lines
Jason
Quoted 8 lines · Nicolin Chen
Quoted 7 lines · Yang Shi
Quoted 7 lines · Yang Shi
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h struct arm_smmu_master_domain { · line 860
Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +5
-0+static inline unsigned int arm_smmu_strtab_max_sid (struct arm_smmu_device *smmu) +{ +return (1ULL << smmu->sid_bits); +} +Hmm, why ULL gets truncated to unsigned int here?
Quoted 4 lines
Quoted 2 lines
No particular reason, but it should be better to not truncate here. Will fix it.
Yea, and looks like we are going to do with:
static inline u64arm_smmu_strtab_num_sids(struct arm_smmu_device *smmu);
Since this is an inline function, so the truncate should actually happens when it is used. But anyway using the correct type does make the code less confusing.
Quoted 19 lines
Then let's be careful at those return-value holders too:
-----------------------------------------------------------
static intarm_smmu_init_strtab_linear(struct arm_smmu_device *smmu){ u32 size; struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg; size = (1 << smmu->sid_bits) * sizeof(struct arm_smmu_ste); ^^^^ overflow? [...] cfg->linear.num_ents = 1 << smmu->sid_bits; ^^^^^^^^ This is u32-----------------------------------------------------------
Thanks
Nicolin
Robin Murphy
reviewerQuoted 10 lines · Yang Shi
The commit
ce410410f1a7("iommu/arm-smmu-v3: Add arm_smmu_strtab_l1/2_idx()") calculated the last index of L1 stream table by 1 <<smmu->sid_bits. 1 is 32 bit value.
However some platforms, for example, AmpereOne, have 32-bit stream id size. This resulted in ouf-of-bound shift. The disassembly of shift is:ldr w2, [x19, 828] //, smmu_7(D)->sid_bits mov w20, 1 lsl w20, w20, w2
Quoted 7 lines
According to ARM spec, if the registers are 32 bit, the instruction actually does:
dest = src << (shift % 32)So it actually shifted by zero bit.
This caused v6.12-rc1 failed to boot on AmpereOne and other platform [1].
FWIW it's going to be seen on any platform with Arm MMU-700 since that always advertises 32-bit StreamID support (as other SMMU implementations may do too).
Quoted 4 lines
UBSAN also reported:
UBSAN: shift-out-of-bounds in drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c:3628:29shift exponent 32 is too large for 32-bit type 'int'
At best, those two lines of actual UBSAN warning are the only part relevant to the point, the rest of the backtrace below is definitely not, please trim it.
Quoted 93 lines
CPU: 70 UID: 0 PID: 1 Comm: swapper/0 Not tainted 6.12.0-rc1 #4 Hardware name: ZOLLNER SUNMOONLAKE/SunMoon Lake, BIOS 00.00. 2024-08-28 18:42:45 08/28/2024 Call trace: dump_backtrace+0xdc/0x140 show_stack+0x20/0x40 dump_stack_lvl+0x60/0x80 dump_stack+0x18/0x28 ubsan_epilogue+0x10/0x48 __ubsan_handle_shift_out_of_bounds+0xd8/0x1a0 arm_smmu_init_structures+0x374/0x3c8 arm_smmu_device_probe+0x208/0x600 platform_probe+0x70/0xe8 really_probe+0xc8/0x3a0 __driver_probe_device+0x84/0x160 driver_probe_device+0x44/0x130 __driver_attach+0xcc/0x208 bus_for_each_dev+0x84/0x100 driver_attach+0x2c/0x40 bus_add_driver+0x158/0x290 driver_register+0x70/0x138 __platform_driver_register+0x2c/0x40 arm_smmu_driver_init+0x28/0x40 do_one_initcall+0x60/0x318 do_initcalls+0x198/0x1e0 kernel_init_freeable+0x18c/0x1e8 kernel_init+0x28/0x160 ret_from_fork+0x10/0x20Using 64 bit immediate when doing shift can solve the problem. The disassembly after the fix looks like:
ldr w20, [x19, 828] //, smmu_7(D)->sid_bits mov x0, 1 lsl x0, x0, x20There are a couple of problematic places, extracted the shift into a helper.
[1] https://lore.kernel.org/lkml/[email protected]/
Fixes ce410410f1a7 ("iommu/arm-smmu-v3: Add arm_smmu_strtab_l1/2_idx()")Tested-by James Morse <[email protected]>Signed-off-by Yang Shi <[email protected]>--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 8 +++++--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 5 +++++ 2 files changed, 10 insertions(+), 3 deletions(-)v2: * Extracted the shift into a helper per Jason Gunthorpe.
* Covered more places per Nicolin Chen and Jason Gunthorpe.
* Used 1ULL instead of 1UL to guarantee 64 bit per Robin Murphy.
* Made the subject more general since this is not AmpereOne specific problem per the report from James Morse.
* Collected t-b tag from James Morse.
* Added Fixes tag in commit log.drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +5
-3@@ -3624,8 +3624,9 @@ static int arm_smmu_init_strtab_2lvl(struct arm_smmu_device *smmu) {u32 l1size;struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg; +unsigned int max_sid =arm_smmu_strtab_max_sid (smmu);unsigned int last_sid_idx = -arm_smmu_strtab_l1_idx ((1 << smmu->sid_bits) -1 ); +arm_smmu_strtab_l1_idx (max_sid -1 );/* Calculate the L1 size, capped to the SIDSIZE. */ cfg->l2.num_l1_ents =min (last_sid_idx +1 ,STRTAB_MAX_L1_ENTRIES );@@ -3657,8 +3658,9 @@ static int arm_smmu_init_strtab_linear(struct arm_smmu_device *smmu) {u32 size;struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg; +unsigned int max_sid =arm_smmu_strtab_max_sid (smmu); - size = (1 << smmu->sid_bits) *sizeof (struct arm_smmu_ste ); + size = max_sid *sizeof (struct arm_smmu_ste ); cfg->linear.table =dmam_alloc_coherent (smmu->dev, size, &cfg->linear.ste_dma,GFP_KERNEL );@@ -3668,7 +3670,7 @@ static int arm_smmu_init_strtab_linear(struct arm_smmu_device *smmu) size);return -ENOMEM ; } - cfg->linear.num_ents =1 << smmu->sid_bits; + cfg->linear.num_ents = max_sid;arm_smmu_init_initial_stes (cfg->linear.table, cfg->linear.num_ents);return 0 ;drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +0
-0
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h struct arm_smmu_master_domain { · line 856
Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +1
-0@@ -853,6 +853,11 @@ struct arm_smmu_master_domain {ioasid_t ssid; }; +static inline unsigned int arm_smmu_strtab_max_sid (struct arm_smmu_device *smmu)
Nit: "max_sid" implies returning the largest supported StreamID value, so it would be logical to either include the "- 1" in here and adjust the other callers, or instead call this something like "num_sids".
Thanks,
Robin.
Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +4
-0+{ +return (1ULL << smmu->sid_bits); +} +static inline struct arm_smmu_domain *to_smmu_domain (struct iommu_domain *dom) { return container_of(dom, struct arm_smmu_domain, domain);
Quoted 15 lines · Robin Murphy
Quoted 14 lines · Yang Shi
The commit
ce410410f1a7("iommu/arm-smmu-v3: Add arm_smmu_strtab_l1/2_idx()") calculated the last index of L1 stream table by 1 <<smmu->sid_bits. 1 is 32 bit value.
However some platforms, for example, AmpereOne, have 32-bit stream id size. This resulted in ouf-of-bound shift. The disassembly of shift is:ldr w2, [x19, 828] //, smmu_7(D)->sid_bits mov w20, 1
lsl w20, w20, w2According to ARM spec, if the registers are 32 bit, the instruction actually does:
dest = src << (shift % 32)
Quoted 5 lines
Quoted 3 lines
So it actually shifted by zero bit.
This caused v6.12-rc1 failed to boot on AmpereOne and other platform [1].
FWIW it's going to be seen on any platform with Arm MMU-700 since that always advertises 32-bit StreamID support (as other SMMU implementations may do too).
I see. Will add this info to the commit log.
Quoted 7 lines
Quoted 5 lines
UBSAN also reported:
UBSAN: shift-out-of-bounds in drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c:3628:29shift exponent 32 is too large for 32-bit type 'int'
At best, those two lines of actual UBSAN warning are the only part relevant to the point, the rest of the backtrace below is definitely not, please trim it.
OK.
Quoted 91 lines
Quoted 91 lines
CPU: 70 UID: 0 PID: 1 Comm: swapper/0 Not tainted 6.12.0-rc1 #4 Hardware name: ZOLLNER SUNMOONLAKE/SunMoon Lake, BIOS 00.00. 2024-08-28 18:42:45 08/28/2024 Call trace: dump_backtrace+0xdc/0x140 show_stack+0x20/0x40 dump_stack_lvl+0x60/0x80 dump_stack+0x18/0x28 ubsan_epilogue+0x10/0x48 __ubsan_handle_shift_out_of_bounds+0xd8/0x1a0 arm_smmu_init_structures+0x374/0x3c8 arm_smmu_device_probe+0x208/0x600 platform_probe+0x70/0xe8 really_probe+0xc8/0x3a0 __driver_probe_device+0x84/0x160 driver_probe_device+0x44/0x130 __driver_attach+0xcc/0x208 bus_for_each_dev+0x84/0x100 driver_attach+0x2c/0x40 bus_add_driver+0x158/0x290 driver_register+0x70/0x138 __platform_driver_register+0x2c/0x40 arm_smmu_driver_init+0x28/0x40 do_one_initcall+0x60/0x318 do_initcalls+0x198/0x1e0 kernel_init_freeable+0x18c/0x1e8 kernel_init+0x28/0x160 ret_from_fork+0x10/0x20Using 64 bit immediate when doing shift can solve the problem. The disassembly after the fix looks like:
ldr w20, [x19, 828] //, smmu_7(D)->sid_bits mov x0, 1
lsl x0, x0, x20There are a couple of problematic places, extracted the shift into a helper.
[1] https://lore.kernel.org/lkml/[email protected]/
Fixes ce410410f1a7 ("iommu/arm-smmu-v3: Add arm_smmu_strtab_l1/2_idx()")Tested-by James Morse <[email protected]>Signed-off-by Yang Shi <[email protected]>---
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c| 8 +++++---drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h| 5 +++++2 files changed, 10 insertions(+), 3 deletions(-)v2: * Extracted the shift into a helper per Jason Gunthorpe.
* Covered more places per Nicolin Chen and Jason Gunthorpe.
* Used 1ULL instead of 1UL to guarantee 64 bit per Robin Murphy.
* Made the subject more general since this is not AmpereOne specific problem per the report from James Morse.
* Collected t-b tag from James Morse.
* Added Fixes tag in commit log.drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +1
-0@@ -3624,8 +3624,9 @@ static int arm_smmu_init_strtab_2lvl(struct arm_smmu_device *smmu) { u32 l1size; struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg; + unsigned int max_sid = arm_smmu_strtab_max_sid(smmu);unsigned int
last_sid_idx=
-arm_smmu_strtab_l1_idx((1 << smmu->sid_bits) - 1);
+arm_smmu_strtab_l1_idx(max_sid - 1);
/* Calculate the L1 size, capped to the SIDSIZE. */cfg->l2.num_l1_ents=min(last_sid_idx + 1, STRTAB_MAX_L1_ENTRIES);drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2
-0@@ -3657,8 +3658,9 @@ static int arm_smmu_init_strtab_linear(struct arm_smmu_device *smmu) { u32 size; struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg; + unsigned int max_sid = arm_smmu_strtab_max_sid(smmu); - size = (1 << smmu->sid_bits) * sizeof(struct arm_smmu_ste); + size = max_sid * sizeof(struct arm_smmu_ste); cfg->linear.table = dmam_alloc_coherent(smmu->dev, size, &cfg->linear.ste_dma,
GFP_KERNEL);drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +1
-1@@ -3668,7 +3670,7 @@ static int arm_smmu_init_strtab_linear(struct arm_smmu_device *smmu) size); return -ENOMEM; } - cfg->linear.num_ents = 1 << smmu->sid_bits; + cfg->linear.num_ents = max_sid; arm_smmu_init_initial_stes(cfg->linear.table, cfg->linear.num_ents); return 0;drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +0
-0
Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h struct arm_smmu_master_domain { · line 855
Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +0
-0@@ -853,6 +853,11 @@ struct arm_smmu_master_domain { ioasid_t ssid; }; +static inline unsigned int arm_smmu_strtab_max_sid(struct arm_smmu_device *smmu)Nit: "
max_sid" implies returning the largest supported StreamID value, so it would be logical to either include the "- 1" in here and adjust the other callers, or instead call this something like "num_sids".
Will use "num_sids".
Quoted 11 lines
Thanks,
Robin.Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +4
-0+{ + return (1ULL << smmu->sid_bits); +} + static inline struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom) { return container_of(dom, struct arm_smmu_domain, domain);