Skip to content

Commit 5c40e42

Browse files
arndbwilldeacon
authored andcommitted
arm64/io: add constant-argument check
In some configurations __const_iowrite32_copy() does not get inlined and gcc runs into the BUILD_BUG(): In file included from <command-line>: In function '__const_memcpy_toio_aligned32', inlined from '__const_iowrite32_copy' at arch/arm64/include/asm/io.h:203:3, inlined from '__const_iowrite32_copy' at arch/arm64/include/asm/io.h:199:20: include/linux/compiler_types.h:487:45: error: call to '__compiletime_assert_538' declared with attribute error: BUILD_BUG failed 487 | _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__) | ^ include/linux/compiler_types.h:468:25: note: in definition of macro '__compiletime_assert' 468 | prefix ## suffix(); \ | ^~~~~~ include/linux/compiler_types.h:487:9: note: in expansion of macro '_compiletime_assert' 487 | _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__) | ^~~~~~~~~~~~~~~~~~~ include/linux/build_bug.h:39:37: note: in expansion of macro 'compiletime_assert' 39 | #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg) | ^~~~~~~~~~~~~~~~~~ include/linux/build_bug.h:59:21: note: in expansion of macro 'BUILD_BUG_ON_MSG' 59 | #define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed") | ^~~~~~~~~~~~~~~~ arch/arm64/include/asm/io.h:193:17: note: in expansion of macro 'BUILD_BUG' 193 | BUILD_BUG(); | ^~~~~~~~~ Move the check for constant arguments into the inline function to ensure it is still constant if the compiler decides against inlining it, and mark them as __always_inline to override the logic that sometimes leads to the compiler not producing the simplified output. Note that either the __always_inline annotation or the check for a constant value are sufficient here, but combining the two looks cleaner as it also avoids the macro. With clang-8 and older, the macro was still needed, but all versions of gcc and clang can reliably perform constant folding here. Fixes: ead7911 ("arm64/io: Provide a WC friendly __iowriteXX_copy()") Signed-off-by: Arnd Bergmann <[email protected]> Reviewed-by: Mark Rutland <[email protected]> Reviewed-by: Jason Gunthorpe <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Will Deacon <[email protected]>
1 parent 14951be commit 5c40e42

File tree

1 file changed

+16
-20
lines changed
  • arch/arm64/include/asm

1 file changed

+16
-20
lines changed

arch/arm64/include/asm/io.h

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,9 @@ extern void __memset_io(volatile void __iomem *, int, size_t);
153153
* emit the large TLP from the CPU.
154154
*/
155155

156-
static inline void __const_memcpy_toio_aligned32(volatile u32 __iomem *to,
157-
const u32 *from, size_t count)
156+
static __always_inline void
157+
__const_memcpy_toio_aligned32(volatile u32 __iomem *to, const u32 *from,
158+
size_t count)
158159
{
159160
switch (count) {
160161
case 8:
@@ -196,24 +197,22 @@ static inline void __const_memcpy_toio_aligned32(volatile u32 __iomem *to,
196197

197198
void __iowrite32_copy_full(void __iomem *to, const void *from, size_t count);
198199

199-
static inline void __const_iowrite32_copy(void __iomem *to, const void *from,
200-
size_t count)
200+
static __always_inline void
201+
__iowrite32_copy(void __iomem *to, const void *from, size_t count)
201202
{
202-
if (count == 8 || count == 4 || count == 2 || count == 1) {
203+
if (__builtin_constant_p(count) &&
204+
(count == 8 || count == 4 || count == 2 || count == 1)) {
203205
__const_memcpy_toio_aligned32(to, from, count);
204206
dgh();
205207
} else {
206208
__iowrite32_copy_full(to, from, count);
207209
}
208210
}
211+
#define __iowrite32_copy __iowrite32_copy
209212

210-
#define __iowrite32_copy(to, from, count) \
211-
(__builtin_constant_p(count) ? \
212-
__const_iowrite32_copy(to, from, count) : \
213-
__iowrite32_copy_full(to, from, count))
214-
215-
static inline void __const_memcpy_toio_aligned64(volatile u64 __iomem *to,
216-
const u64 *from, size_t count)
213+
static __always_inline void
214+
__const_memcpy_toio_aligned64(volatile u64 __iomem *to, const u64 *from,
215+
size_t count)
217216
{
218217
switch (count) {
219218
case 8:
@@ -255,21 +254,18 @@ static inline void __const_memcpy_toio_aligned64(volatile u64 __iomem *to,
255254

256255
void __iowrite64_copy_full(void __iomem *to, const void *from, size_t count);
257256

258-
static inline void __const_iowrite64_copy(void __iomem *to, const void *from,
259-
size_t count)
257+
static __always_inline void
258+
__iowrite64_copy(void __iomem *to, const void *from, size_t count)
260259
{
261-
if (count == 8 || count == 4 || count == 2 || count == 1) {
260+
if (__builtin_constant_p(count) &&
261+
(count == 8 || count == 4 || count == 2 || count == 1)) {
262262
__const_memcpy_toio_aligned64(to, from, count);
263263
dgh();
264264
} else {
265265
__iowrite64_copy_full(to, from, count);
266266
}
267267
}
268-
269-
#define __iowrite64_copy(to, from, count) \
270-
(__builtin_constant_p(count) ? \
271-
__const_iowrite64_copy(to, from, count) : \
272-
__iowrite64_copy_full(to, from, count))
268+
#define __iowrite64_copy __iowrite64_copy
273269

274270
/*
275271
* I/O memory mapping functions.

0 commit comments

Comments
 (0)