Re: [PATCH 2/5] xfs_db: fix alignment checks in getbitval

Darrick J. Wong
2024-03-26T16:28:22Z

On Mon, Mar 25, 2024 at 10:14:53PM -0700, Christoph Hellwig wrote:
> On Mon, Mar 25, 2024 at 08:21:21PM -0700, Darrick J. Wong wrote:
> > From: Darrick J. Wong <djwong@kernel.org>
> >
> > For some reason, getbitval insists upon collecting a u64 from a pointer
> > bit by bit if it's not aligned to a 16-byte boundary. Modern day
> > systems only seem to require N-byte alignment for an N-byte quantity, so
> > let's do that instead.
>
> Not sure what modern day systems means here. In general in C you can
> do unaligned access, but it might be very inefficient.

Platforms like SPARC where unaligned accesses result in kernel traps
that abort the program. Not that I've used any such platforms in 15
years now.

> If this code does what I think it does, maybe the right thing is to
> simply use the get_unaligned_be{16,32,64} helpers?

Well we could still use the regular ones for aligned access, e.g.

#define PTR_ALIGNED(p, type) (((intptr_t)(p) & (sizeof(type) - 1)) == 0)
switch (nbits) {
case 64:
if (PTR_ALIGNED(p, __u64))
return be64_to_cpu(*(__be64 *)p);

return get_unaligned_be64(p);
...
}

--D