lib/iomem_copy: fix __iomem casts
PATCH 1/1 lib/iomem_copy: fix __iomem casts
The iomem_copy.c code discards __iomem address space when using the IS_ALIGNED() macro. It would make more sense to fix this in one place by aing a PTR_ALIGNED_LONG() macro and then doing the necessary casts there before invoking IS_ALIGNED().
As part of this, also force the pointer to an unsigned long as pointers are generally not signed, although there is no warning as yet on treating pointers as signed.
lib/iomem_copy.c:27:26: warning: cast removes address space '__iomem' of expression lib/iomem_copy.c:27:26: warning: cast removes address space '__iomem' of expression lib/iomem_copy.c:64:26: warning: cast removes address space '__iomem' of expression lib/iomem_copy.c:64:26: warning: cast removes address space '__iomem' of expression lib/iomem_copy.c:106:26: warning: cast removes address space '__iomem' of expression lib/iomem_copy.c:106:26: warning: cast removes address space '__iomem' of expression
--- lib/iomem_copy.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
lib/iomem_copy.c +5 -3
@@ -9,6 +9,8 @@#include <linux/types.h> #include <linux/unaligned.h> +#define PTR_ALIGNED_LONG (__ptr) IS_ALIGNED((__force unsigned long)__ptr, sizeof(long)) +#ifndef memset_io/** * memset_io() - Set a range of I/O memory to a constant value
@@ -24,7 +26,7 @@ void memset_io(volatile void __iomem *addr, int val, size_t count) qc *= ~0UL /0xff ; -while (count && !IS_ALIGNED ((long )addr,sizeof (long ))) { +while (count && !PTR_ALIGNED_LONG (addr)) {__raw_writeb (val, addr); addr++; count--;
@@ -61,7 +63,7 @@ EXPORT_SYMBOL(memset_io);*/ void memcpy_fromio (void *dst,const volatile void __iomem *src,size_t count) { -while (count && !IS_ALIGNED ((long )src,sizeof (long ))) { +while (count && !PTR_ALIGNED_LONG (src)) { *(u8 *)dst =__raw_readb (src); src++; dst++;
@@ -103,7 +105,7 @@ EXPORT_SYMBOL(memcpy_fromio);*/ void memcpy_toio (volatile void __iomem *dst,const void *src,size_t count) { -while (count && !IS_ALIGNED ((long )dst,sizeof (long ))) { +while (count && !PTR_ALIGNED_LONG (dst)) {__raw_writeb (*(u8 *)src, dst); src++; dst++;
Signature · 1 line
2.37.2.352.g3c44437643
Quoted 10 lines · Ben Dooks
The
iomem_copy.ccode discards__iomemaddress space when using theIS_ALIGNED()macro. It would make more sense to fix this in one place by aing aPTR_ALIGNED_LONG()macro and then doing the necessary casts there before invokingIS_ALIGNED().As part of this, also force the pointer to an unsigned long as pointers are generally not signed, although there is no warning as yet on treating pointers as signed.
+#define
PTR_ALIGNED_LONG(__ptr)IS_ALIGNED((__force unsigned long)__ptr, sizeof(long))
Casting to unsigned long is fine (indeed, casting a pointer to long had been very odd in the first place), but... why __force? Casts to unsigned long (de facto uintptr_t) do *not* require __force - they are explicitly allowed, unless you pass -Wcast-from-as in sparse arguments. -Wall does not turn those on; -Wsparse-all would, but kbuild doesn't pass that.
; cat >/tmp/a.c <<'EOF'
#define __iomem __attribute__((noderef, address_space(__iomem)))
static int f(void __iomem *p)
{
return (unsigned long)p;
}static int g(void __iomem *p)
{
return (long)p;
}EOF
; sparse /tmp/a.c/tmp/a.c:10:17: warning: cast removes address space '__iomem' of expression ; sparse -Wcast-from-as /tmp/a.c/tmp/a.c:5:17: warning: cast removes address space '__iomem' of expression /tmp/a.c:10:17: warning: cast removes address space '__iomem' of expression ; sparse -Wall /tmp/a.c/tmp/a.c:10:17: warning: cast removes address space '__iomem' of expression ; sparse -Wsparse-all /tmp/a.c/tmp/a.c:5:17: warning: cast removes address space '__iomem' of expression /tmp/a.c:10:17: warning: cast removes address space '__iomem' of expression
Quoted 10 lines · Al Viro
Quoted 8 lines · Ben Dooks
The
iomem_copy.ccode discards__iomemaddress space when using theIS_ALIGNED()macro. It would make more sense to fix this in one place by aing aPTR_ALIGNED_LONG()macro and then doing the necessary casts there before invokingIS_ALIGNED().As part of this, also force the pointer to an unsigned long as pointers are generally not signed, although there is no warning as yet on treating pointers as signed.
Quoted 7 lines
Quoted 1 line
+#define
PTR_ALIGNED_LONG(__ptr)IS_ALIGNED((__force unsigned long)__ptr, sizeof(long))Casting to unsigned long is fine (indeed, casting a pointer to long had been very odd in the first place), but... why
__force? Casts to unsigned long (de factouintptr_t) do *not* require__force- they are explicitly allowed, unless you pass -Wcast-from-as in sparse arguments. -Wall does not turn those on; -Wsparse-all would, but kbuild doesn't pass that.
FWIW, we have 170+ places where IS_ALIGNED((unsigned long)pointer, _) or IS_ALIGNED((uintptr_t)pointer, _) is used in the tree...
Quoted 13 lines · On Wed, 24 Jun 2026 01:04:36 +0100 Al Viro <[email protected]> wrote:
Quoted 12 lines · Al Viro
Quoted 10 lines · Ben Dooks
The
iomem_copy.ccode discards__iomemaddress space when using theIS_ALIGNED()macro. It would make more sense to fix this in one place by aing aPTR_ALIGNED_LONG()macro and then doing the necessary casts there before invokingIS_ALIGNED().As part of this, also force the pointer to an unsigned long as pointers are generally not signed, although there is no warning as yet on treating pointers as signed.
+#define
PTR_ALIGNED_LONG(__ptr)IS_ALIGNED((__force unsigned long)__ptr, sizeof(long))
Quoted 9 lines
Quoted 5 lines
Casting to unsigned long is fine (indeed, casting a pointer to long had been very odd in the first place), but... why
__force? Casts to unsigned long (de factouintptr_t) do *not* require__force- they are explicitly allowed, unless you pass -Wcast-from-as in sparse arguments. -Wall does not turn those on; -Wsparse-all would, but kbuild doesn't pass that.FWIW, we have 170+ places where
IS_ALIGNED((unsigned long)pointer, _)orIS_ALIGNED((uintptr_t)pointer, _)is used in the tree...
Would is be better to add an IS_PTR_UNALIGNED(ptr, type) that returns
(unsigned long)(ptr) & (sizeof(type) - 1)
since I suspect quite a few of those places do fixups and need the offset?
David
Quoted 10 lines · Al Viro
Quoted 8 lines · Ben Dooks
The
iomem_copy.ccode discards__iomemaddress space when using theIS_ALIGNED()macro. It would make more sense to fix this in one place by aing aPTR_ALIGNED_LONG()macro and then doing the necessary casts there before invokingIS_ALIGNED().As part of this, also force the pointer to an unsigned long as pointers are generally not signed, although there is no warning as yet on treating pointers as signed.
Quoted 7 lines
Quoted 1 line
+#define
PTR_ALIGNED_LONG(__ptr)IS_ALIGNED((__force unsigned long)__ptr, sizeof(long))Casting to unsigned long is fine (indeed, casting a pointer to long had been very odd in the first place), but... why
__force? Casts to unsigned long (de factouintptr_t) do *not* require__force- they are explicitly allowed, unless you pass -Wcast-from-as in sparse arguments. -Wall does not turn those on; -Wsparse-all would, but kbuild doesn't pass that.
Trying to get rid of the address space warning, which requires the __force part.
Quoted 24 lines
; cat >/tmp/a.c <<'EOF'
#define__iomem__attribute__((noderef, address_space(__iomem)))static int
f(void __iomem *p){ return (unsigned long)p; }static int
g(void __iomem *p){ return (long)p; }EOF
; sparse/tmp/a.c/tmp/a.c:10:17: warning: cast removes address space '__iomem' of expression ; sparse -Wcast-from-as/tmp/a.c/tmp/a.c:5:17: warning: cast removes address space '__iomem' of expression/tmp/a.c:10:17: warning: cast removes address space '__iomem' of expression ; sparse -Wall/tmp/a.c/tmp/a.c:10:17: warning: cast removes address space '__iomem' of expression ; sparse -Wsparse-all/tmp/a.c/tmp/a.c:5:17: warning: cast removes address space '__iomem' of expression/tmp/a.c:10:17: warning: cast removes address space '__iomem' of expression
Signature · 4 lines
Ben Dooks http://www.codethink.co.uk/ Senior Engineer Codethink - Providing Genius https://www.codethink.co.uk/privacy.html
Quoted 13 lines · Ben Dooks
Quoted 12 lines · Al Viro
Quoted 10 lines · Ben Dooks
The
iomem_copy.ccode discards__iomemaddress space when using theIS_ALIGNED()macro. It would make more sense to fix this in one place by aing aPTR_ALIGNED_LONG()macro and then doing the necessary casts there before invokingIS_ALIGNED().As part of this, also force the pointer to an unsigned long as pointers are generally not signed, although there is no warning as yet on treating pointers as signed.
+#define
PTR_ALIGNED_LONG(__ptr)IS_ALIGNED((__force unsigned long)__ptr, sizeof(long))
Quoted 8 lines
Quoted 5 lines
Casting to unsigned long is fine (indeed, casting a pointer to long had been very odd in the first place), but... why
__force? Casts to unsigned long (de factouintptr_t) do *not* require__force- they are explicitly allowed, unless you pass -Wcast-from-as in sparse arguments. -Wall does not turn those on; -Wsparse-all would, but kbuild doesn't pass that.Trying to get rid of the address space warning, which requires the
__forcepart.
It does not require __force - not with the type we are casting to being unsigned long. As the examples you've quoted demonstrate. See evaluate_cast() in sparse evaluate.c:
if ((ttype == &ulong_ctype || ttype == uintptr_ctype) && !Wcast_from_as)
tas = &bad_address_space;
with
if (!tas && valid_as(sas))
warning(expr->pos, "cast removes address space '%s' of expression", show_as(sas));several lines below.
And yes, it is deliberate.
Quoted 26 lines · Al Viro
Quoted 13 lines · Ben Dooks
Quoted 12 lines · Al Viro
Quoted 10 lines · Ben Dooks
The
iomem_copy.ccode discards__iomemaddress space when using theIS_ALIGNED()macro. It would make more sense to fix this in one place by aing aPTR_ALIGNED_LONG()macro and then doing the necessary casts there before invokingIS_ALIGNED().As part of this, also force the pointer to an unsigned long as pointers are generally not signed, although there is no warning as yet on treating pointers as signed.
+#define
PTR_ALIGNED_LONG(__ptr)IS_ALIGNED((__force unsigned long)__ptr, sizeof(long))Quoted 8 lines
Quoted 5 lines
Casting to unsigned long is fine (indeed, casting a pointer to long had been very odd in the first place), but... why
__force? Casts to unsigned long (de factouintptr_t) do *not* require__force- they are explicitly allowed, unless you pass -Wcast-from-as in sparse arguments. -Wall does not turn those on; -Wsparse-all would, but kbuild doesn't pass that.Trying to get rid of the address space warning, which requires the
__forcepart.It does not require
__force- not with the type we are casting to being unsigned long. As the examples you've quoted demonstrate. Seeevaluate_cast()in sparseevaluate.c:
Quoted 8 lines
if ((ttype == &ulong_ctype || ttype == uintptr_ctype) && !Wcast_from_as) tas = &bad_address_space; with if (!tas && valid_as(sas)) warning(expr->pos, "cast removes address space '%s' of expression", show_as(sas));several lines below.
And yes, it is deliberate.
Ah we should also have checked for intptr or long type there, the original code had long which causes sparse to complain here.
Signature · 4 lines
Ben Dooks http://www.codethink.co.uk/ Senior Engineer Codethink - Providing Genius https://www.codethink.co.uk/privacy.html