dma-mapping: work around clang bug
PATCH v2 1/1 dma-mapping: work around clang bug
Clang has a rather annoying behavior of checking for integer arithmetic problems in code paths that are discarded by gcc before that perfoms the same checks.
For DMA_BIT_MASK(64), this leads to a warning despite the result of the macro being completely sensible:
arch/arm/plat-iop/adma.c:146:24: error: shift count >= width of type [-Werror,-Wshift-count-overflow]
.coherent_dma_mask = DMA_BIT_MASK(64),
The best workaround I could come up with is to shift the value twice, which makes the macro way less readable but always has the same result.
---
v2: fix off-by-one error
--- include/linux/dma-mapping.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
include/linux/dma-mapping.h +2 -1
@@ -138,7 +138,8 @@ struct dma_map_ops {extern const struct dma_map_ops dma_virt_ops;extern const struct dma_map_ops dma_dummy_ops; -#define DMA_BIT_MASK (n) (((n) == 64) ? ~0ULL : ((1ULL<<(n))-1)) +/* double shift to work around https://bugs.llvm.org/show_bug.cgi?id=38789 */ +#define DMA_BIT_MASK (n) (((n) == 64) ? ~0ULL : ((1ULL<<((n)-1))<<1)-1)#define DMA_MASK_NONE 0x0ULL
Signature · 1 line
2.20.0
Robin Murphy
reviewerHi Arnd,
Quoted 27 lines · Arnd Bergmann
Clang has a rather annoying behavior of checking for integer arithmetic problems in code paths that are discarded by gcc before that perfoms the same checks.
For
DMA_BIT_MASK(64), this leads to a warning despite the result of the macro being completely sensible:
arch/arm/plat-iop/adma.c:146:24: error: shift count >= width of type [-Werror,-Wshift-count-overflow].coherent_dma_mask = DMA_BIT_MASK(64),The best workaround I could come up with is to shift the value twice, which makes the macro way less readable but always has the same result.
Signed-off-by Arnd Bergmann <[email protected]>---
v2: fix off-by-one error--- include/linux/dma-mapping.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)include/linux/dma-mapping.h +0
-0
include/linux/dma-mapping.h struct dma_map_ops { · line 142
Quoted 6 lines
include/linux/dma-mapping.h +2
-1@@ -138,7 +138,8 @@ struct dma_map_ops {extern const struct dma_map_ops dma_virt_ops;extern const struct dma_map_ops dma_dummy_ops; -#define DMA_BIT_MASK (n) (((n) == 64) ? ~0ULL : ((1ULL<<(n))-1)) +/* double shift to work around https://bugs.llvm.org/show_bug.cgi?id=38789 */ +#define DMA_BIT_MASK (n) (((n) == 64) ? ~0ULL : ((1ULL<<((n)-1))<<1)-1)
I think that now makes DMA_BIT_MASK(0) undefined - that shouldn't matter in most cases, but it could potentially happen at runtime where callers use a non-constant argument. However, it also means we don't need to special-case 64 any more (since that's there to avoid the same thing anyway), so we could simply flip that to handle 0 instead.
FWIW I'd be very tempted to fold in the second shift as "2ULL<<((n)-1)", but that may not be to everyone's taste.
Robin.
Quoted 4 lines
include/linux/dma-mapping.h +0
-0#define DMA_MASK_NONE 0x0ULL
Quoted 11 lines · Robin Murphy
include/linux/dma-mapping.h struct dma_map_ops { · line 142
Quoted 4 lines · Arnd Bergmann
include/linux/dma-mapping.h +2
-1-#define DMA_BIT_MASK (n) (((n) == 64) ? ~0ULL : ((1ULL<<(n))-1)) +/* double shift to work around https://bugs.llvm.org/show_bug.cgi?id=38789 */ +#define DMA_BIT_MASK (n) (((n) == 64) ? ~0ULL : ((1ULL<<((n)-1))<<1)-1)I think that now makes
DMA_BIT_MASK(0)undefined - that shouldn't matter in most cases, but it could potentially happen at runtime where callers use a non-constant argument. However, it also means we don't need to special-case 64 any more (since that's there to avoid the same thing anyway), so we could simply flip that to handle 0 instead.
Yes, good idea.
Quoted 2 lines
FWIW I'd be very tempted to fold in the second shift as "2ULL<<((n)-1)", but that may not be to everyone's taste.
I like that. So shall we do this?
/*
* Shifting '2' instead of '1' because of
* https://bugs.llvm.org/show_bug.cgi?id=38789
*/
#define DMA_BIT_MASK(n) (((n) == 0) ? 0ULL : ((2ULL<<((n)-1)))-1)
Arnd
Geert Uytterhoeven
Reviewed-byQuoted 18 lines · Arnd Bergmann
Quoted 11 lines · Robin Murphy
include/linux/dma-mapping.h struct dma_map_ops { · line 142
Quoted 4 lines · Arnd Bergmann
include/linux/dma-mapping.h +2
-1-#define DMA_BIT_MASK (n) (((n) == 64) ? ~0ULL : ((1ULL<<(n))-1)) +/* double shift to work around https://bugs.llvm.org/show_bug.cgi?id=38789 */ +#define DMA_BIT_MASK (n) (((n) == 64) ? ~0ULL : ((1ULL<<((n)-1))<<1)-1)I think that now makes
DMA_BIT_MASK(0)undefined - that shouldn't matter in most cases, but it could potentially happen at runtime where callers use a non-constant argument. However, it also means we don't need to special-case 64 any more (since that's there to avoid the same thing anyway), so we could simply flip that to handle 0 instead.Yes, good idea.
Quoted 2 lines
FWIW I'd be very tempted to fold in the second shift as "2ULL<<((n)-1)", but that may not be to everyone's taste.
Quoted 7 lines
I like that. So shall we do this?
/*
* Shifting '2' instead of '1' because of
* https://bugs.llvm.org/show_bug.cgi?id=38789
*/
#defineDMA_BIT_MASK(n)(((n) == 0) ? 0ULL : ((2ULL<<((n)-1)))-1)
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Quoted 18 lines · Arnd Bergmann
Quoted 11 lines · Robin Murphy
include/linux/dma-mapping.h struct dma_map_ops { · line 142
Quoted 4 lines · Arnd Bergmann
include/linux/dma-mapping.h +2
-1-#define DMA_BIT_MASK (n) (((n) == 64) ? ~0ULL : ((1ULL<<(n))-1)) +/* double shift to work around https://bugs.llvm.org/show_bug.cgi?id=38789 */ +#define DMA_BIT_MASK (n) (((n) == 64) ? ~0ULL : ((1ULL<<((n)-1))<<1)-1)I think that now makes
DMA_BIT_MASK(0)undefined - that shouldn't matter in most cases, but it could potentially happen at runtime where callers use a non-constant argument. However, it also means we don't need to special-case 64 any more (since that's there to avoid the same thing anyway), so we could simply flip that to handle 0 instead.Yes, good idea.
Quoted 2 lines
FWIW I'd be very tempted to fold in the second shift as "2ULL<<((n)-1)", but that may not be to everyone's taste.
Quoted 7 lines
I like that. So shall we do this?
/*
* Shifting '2' instead of '1' because of
* https://bugs.llvm.org/show_bug.cgi?id=38789
*/
#defineDMA_BIT_MASK(n)(((n) == 0) ? 0ULL : ((2ULL<<((n)-1)))-1)
Neat - it was too early in the morning for me to think of a succinct way to comment it, but that's great. I suspect there may be a redundant set of parentheses around the shift still, but other than that,
Cheers,
Robin.