Re: [PATCH v4 1/4] PCI/sysfs: Prohibit unaligned access to I/O port

Bjorn Helgaas
2026-02-26T17:00:51Z

On Fri, Jan 16, 2026 at 04:17:18PM +0800, Ziming Du wrote:
> Unaligned access is harmful for non-x86 archs such as arm64. When we
> use pwrite or pread to access the I/O port resources with unaligned
> offset, system will crash as follows:

> @@ -1166,12 +1167,16 @@ static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj,
> *(u8 *)buf = inb(port);
> return 1;
> case 2:
> + if (!IS_ALIGNED(port, count))
> + return -EINVAL;

I assume "IS_ALIGNED(port, 1)" is *always* true, so can we just do
this once before the switch instead of adding it to the "case 2" and
"case 4"?

> if (write)
> outw(*(u16 *)buf, port);
> else
> *(u16 *)buf = inw(port);
> return 2;
> case 4:
> + if (!IS_ALIGNED(port, count))
> + return -EINVAL;
> if (write)
> outl(*(u32 *)buf, port);
> else
> --
> 2.43.0
>