* [PATCH] lib/iomem_copy: fix __iomem casts
@ 2026-06-22 12:48 Ben Dooks
2026-06-23 23:47 ` Al Viro
0 siblings, 1 reply; 3+ messages in thread
From: Ben Dooks @ 2026-06-22 12:48 UTC (permalink / raw)
To: Andrew Morton, linux-kernel; +Cc: Ben Dooks
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
Signed-off-by: Ben Dooks <ben.dooks@codethink.co.uk>
---
lib/iomem_copy.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/lib/iomem_copy.c b/lib/iomem_copy.c
index dec7eaea60e0..a8cd2642e3ee 100644
--- a/lib/iomem_copy.c
+++ b/lib/iomem_copy.c
@@ -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++;
--
2.37.2.352.g3c44437643
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] lib/iomem_copy: fix __iomem casts
2026-06-22 12:48 [PATCH] lib/iomem_copy: fix __iomem casts Ben Dooks
@ 2026-06-23 23:47 ` Al Viro
2026-06-24 0:04 ` Al Viro
0 siblings, 1 reply; 3+ messages in thread
From: Al Viro @ 2026-06-23 23:47 UTC (permalink / raw)
To: Ben Dooks; +Cc: Andrew Morton, linux-kernel
On Mon, Jun 22, 2026 at 01:48:57PM +0100, Ben Dooks wrote:
> 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.
> +#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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] lib/iomem_copy: fix __iomem casts
2026-06-23 23:47 ` Al Viro
@ 2026-06-24 0:04 ` Al Viro
0 siblings, 0 replies; 3+ messages in thread
From: Al Viro @ 2026-06-24 0:04 UTC (permalink / raw)
To: Ben Dooks; +Cc: Andrew Morton, linux-kernel
On Wed, Jun 24, 2026 at 12:47:20AM +0100, Al Viro wrote:
> On Mon, Jun 22, 2026 at 01:48:57PM +0100, Ben Dooks wrote:
> > 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.
>
> > +#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.
FWIW, we have 170+ places where IS_ALIGNED((unsigned long)pointer, _)
or IS_ALIGNED((uintptr_t)pointer, _) is used in the tree...
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-06-24 0:04 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-22 12:48 [PATCH] lib/iomem_copy: fix __iomem casts Ben Dooks
2026-06-23 23:47 ` Al Viro
2026-06-24 0:04 ` Al Viro
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox