PCI/MSI: fix msi_mask()
PATCH 1/1 PCI/MSI: fix msi_mask()
The commit bffac3c593eba1f9da3efd0199e49ea6558a40ce does:
- temp = (1 << multi_msi_capable(control)); - temp = ((temp - 1) & ~temp); + temp = msi_mask((control & PCI_MSI_FLAGS_QMASK) >> 1);
and provides msi_mask() to avoid undefined shift by 32.
According to PCI Local Bus Spec 3.0 "6.8.1.3 Message Control for MSI", relations between the encoded bits in control register, the number of vectors and the proper maskbits should be as following table:
control[3::1] | vectors | maskbits
--------------+----------+-----------
000b | 1 | 0x1
001b | 2 | 0x3
010b | 4 | 0xf
011b | 8 | 0xff
100b | 16 | 0xffff
101b | 32 | 0xffffffff
110b | Reserved | n/a
111b | Reserved | n/aThis patch fix the wrong array in the msi_mask().
--- drivers/pci/msi.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
drivers/pci/msi.c +1 -1
@@ -109,7 +109,7 @@ static void msix_set_enable(struct pci_dev *dev, int enable)*/ static inline __attribute_const__ u32 msi_mask(unsigned x) { -static const u32 mask[] = {1 ,2 ,4 ,0xf ,0xff ,0xffff ,0xffffffff }; +static const u32 mask[] = {1 ,3 ,0xf ,0xff ,0xffff ,0xffffffff };return mask[x]; }
Signature · 1 line
1.6.0
Quoted 7 lines · Hidetoshi Seto
The commit
bffac3c593eba1f9da3efd0199e49ea6558a40cedoes:- temp = (1 << multi_msi_capable(control)); - temp = ((temp - 1) & ~temp); + temp = msi_mask((control & PCI_MSI_FLAGS_QMASK) >> 1);and provides
msi_mask()to avoid undefined shift by 32.
[...]
Quoted 1 line
This patch fix the wrong array in the
msi_mask().
Quite correct. While disconnected for a couple of weeks, I also considered this possible fix:
static inline __attribute_const__ u32 msi_mask(unsigned x)
{
/* Don't shift by >= width of type */
if (x >= 5)
return 0xffffffff;
return (1 << (1 << x)) - 1;
}which has the added bonus of not running off the end of the array if some device has a bogus value in its config space.
What do you think?
Signature · 4 lines
Matthew Wilcox Intel Open Source Technology Centre "Bill, look, we understand that you're interested in selling us this operating system, but compare it to ours. We can't possibly take such a retrograde step."
Quoted 14 lines · Matthew Wilcox
Quoted 1 line · Hidetoshi Seto
This patch fix the wrong array in the
msi_mask().Quite correct. While disconnected for a couple of weeks, I also considered this possible fix:
static inline
__attribute_const__u32msi_mask(unsigned x){ /* Don't shift by >= width of type */ if (x >= 5) return 0xffffffff; return (1 << (1 << x)) - 1; }
Quoted 4 lines
which has the added bonus of not running off the end of the array if some device has a bogus value in its config space.
What do you think?
Yeah, I think it's better one.
I'm going to post a revised patch hiring this style.
Thanks,
H.Seto
PATCH 1/1 PCI/MSI: fix msi_mask() (rev. 2)
In reply to Matthew WilcoxThe commit bffac3c593eba1f9da3efd0199e49ea6558a40ce ("PCI MSI: Fix undefined shift by 32") does:
- temp = (1 << multi_msi_capable(control)); - temp = ((temp - 1) & ~temp); + temp = msi_mask((control & PCI_MSI_FLAGS_QMASK) >> 1);
and provides msi_mask() to avoid undefined shift by 32.
According to PCI Local Bus Spec 3.0 "6.8.1.3 Message Control for MSI", relations between the encoded bits in control register, the number of vectors and the proper maskbits should be as the following table:
control[3::1] | vectors | maskbits
--------------+----------+-----------
000b | 1 | 0x1
001b | 2 | 0x3
010b | 4 | 0xf
011b | 8 | 0xff
100b | 16 | 0xffff
101b | 32 | 0xffffffff
110b | Reserved | n/a
111b | Reserved | n/aThis patch removes the wrong array in the msi_mask(), and adds a bonus of not running off the end of the array if some device has a bogus value in its config space.
--- drivers/pci/msi.c | 6 ++++-- 1 files changed, 4 insertions(+), 2 deletions(-)
drivers/pci/msi.c +4 -2
@@ -109,8 +109,10 @@ static void msix_set_enable(struct pci_dev *dev, int enable)*/ static inline __attribute_const__ u32 msi_mask(unsigned x) { -static const u32 mask[] = {1 ,2 ,4 ,0xf ,0xff ,0xffff ,0xffffffff }; -return mask[x]; +/* Don't shift by >= width of type */ +if (x >=5 ) +return 0xffffffff ; +return (1 << (1 << x)) -1 ; }static void msix_flush_writes (struct irq_desc *desc)
Signature · 1 line
1.6.0.3
Re: [PATCH] PCI/MSI: fix msi_mask() (rev. 2)
Quoted 2 lines · Hidetoshi Seto
The commit
bffac3c593eba1f9da3efd0199e49ea6558a40ce("PCI MSI: Fix undefined shift by 32") does:
I think the commit message could be worded somewhat better, and I think I should be credited as the author of the patch. How about this?
----
From: Matthew Wilcox <[email protected]>
Hidetoshi Seto points out that commit bffac3c593eba1f9da3efd0199e49ea6558a40ce has wrong values in the array. Rather than correct the array, we can just use a bounds check and perform the calculation specified in the comment. As a bonus, this will not run off the end of the array if the device specifies an illegal value in the MSI capcbility.
----
drivers/pci/msi.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/pci/msi.c +4 -6
@@ -103,14 +103,12 @@ static void msix_set_enable(struct pci_dev *dev, int enable) } } -/* -* Essentially, this is ((1 << (1 << x)) - 1), but without the -* undefinedness of a << 32. -*/ static inline __attribute_const__ u32 msi_mask(unsigned x) { -static const u32 mask[] = {1 ,2 ,4 ,0xf ,0xff ,0xffff ,0xffffffff }; -return mask[x]; +/* Don't shift by >= width of type */ +if (x >=5 ) +return 0xffffffff ; +return (1 << (1 << x)) -1 ; }static void msix_flush_writes (struct irq_desc *desc)
Signature · 4 lines
Matthew Wilcox Intel Open Source Technology Centre "Bill, look, we understand that you're interested in selling us this operating system, but compare it to ours. We can't possibly take such a retrograde step."
Re: [PATCH] PCI/MSI: fix msi_mask() (rev. 2)
Quoted 9 lines · Matthew Wilcox
Quoted 2 lines · Hidetoshi Seto
The commit
bffac3c593eba1f9da3efd0199e49ea6558a40ce("PCI MSI: Fix undefined shift by 32") does:I think the commit message could be worded somewhat better, and I think I should be credited as the author of the patch. How about this?
----
Quoted 8 lines
From: Matthew Wilcox <[email protected]>
Hidetoshi Seto points out that commit
bffac3c593eba1f9da3efd0199e49ea6558a40cehas wrong values in the array. Rather than correct the array, we can just use a bounds check and perform the calculation specified in the comment. As a bonus, this will not run off the end of the array if the device specifies an illegal value in the MSI capcbility.
^^^^^^^^^^ capability
Quoted 3 lines
Signed-off-by Matthew Wilcox <[email protected]>Signed-off-by Hidetoshi Seto <[email protected]>
It looks good for me other than the typo above.
Thanks,
H.Seto
Jesse Barnes
AppliedRe: [PATCH] PCI/MSI: fix msi_mask() (rev. 2)
In reply to Matthew WilcoxQuoted 18 lines · Matthew Wilcox
Quoted 2 lines · Hidetoshi Seto
The commit
bffac3c593eba1f9da3efd0199e49ea6558a40ce("PCI MSI: Fix undefined shift by 32") does:I think the commit message could be worded somewhat better, and I think I should be credited as the author of the patch. How about this?
----
From: Matthew Wilcox <[email protected]>
Hidetoshi Seto points out that commit
bffac3c593eba1f9da3efd0199e49ea6558a40cehas wrong values in the array. Rather than correct the array, we can just use a bounds check and perform the calculation specified in the comment. As a bonus, this will not run off the end of the array if the device specifies an illegal value in the MSI capcbility.
Quoted 2 lines
Signed-off-by Matthew Wilcox <[email protected]>Signed-off-by Hidetoshi Seto <[email protected]>
Applied to my for-linus branch, thanks guys.
Signature · 1 line
Jesse Barnes, Intel Open Source Technology Center