[PATCH v2 0/3] Switch get/put unaligned to use memcpy

Ian Rogers posted 3 patches 3 months, 3 weeks ago
There is a newer version of this series
include/vdso/unaligned.h        | 48 ++++++++++++++++++++++++++++-----
tools/include/linux/unaligned.h |  4 ---
tools/include/vdso/unaligned.h  | 48 ++++++++++++++++++++++++++++-----
3 files changed, 84 insertions(+), 16 deletions(-)
[PATCH v2 0/3] Switch get/put unaligned to use memcpy
Posted by Ian Rogers 3 months, 3 weeks ago
The existing type punning approach with packed structs requires
 -fno-strict-aliasing to be passed to the compiler for
correctness. This is true in the kernel tree but not in the tools
directory resulting in this suggested patch from Eric Biggers
 <ebiggers@google.com>:
https://lore.kernel.org/lkml/20250614044133.660848-2-ebiggers@kernel.org/

Requiring -fno-strict-aliasing seems unfortunate and so this patch
makes the unaligned code work via memcpy for type punning rather than
the packed attribute.

v2: switch memcpy to __builtin_memcpy to avoid potential/disallowed
    memcpy calls in vdso caused by -fno-builtin. Reported by
    Christophe Leroy <christophe.leroy@csgroup.eu>:
    https://lore.kernel.org/lkml/c57de5bf-d55c-48c5-9dfa-e2fb844dafe9@csgroup.eu/

Ian Rogers (3):
  vdso: Switch get/put unaligned from packed struct to memcpy
  tools headers: Update the linux/unaligned.h copy with the kernel
    sources
  tools headers: Remove unneeded ignoring of warnings in unaligned.h

 include/vdso/unaligned.h        | 48 ++++++++++++++++++++++++++++-----
 tools/include/linux/unaligned.h |  4 ---
 tools/include/vdso/unaligned.h  | 48 ++++++++++++++++++++++++++++-----
 3 files changed, 84 insertions(+), 16 deletions(-)

-- 
2.50.0.rc2.701.gf1e915cc24-goog
Re: [PATCH v2 0/3] Switch get/put unaligned to use memcpy
Posted by Christophe Leroy 3 months, 3 weeks ago

Le 17/06/2025 à 22:53, Ian Rogers a écrit :
> The existing type punning approach with packed structs requires
>   -fno-strict-aliasing to be passed to the compiler for
> correctness. This is true in the kernel tree but not in the tools
> directory resulting in this suggested patch from Eric Biggers
>   <ebiggers@google.com>:
> https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flore.kernel.org%2Flkml%2F20250614044133.660848-2-ebiggers%40kernel.org%2F&data=05%7C02%7Cchristophe.leroy%40csgroup.eu%7Cf05413010ecc40ad1bdf08ddade1316a%7C8b87af7d86474dc78df45f69a2011bb5%7C0%7C0%7C638857904894967529%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=29QUBLnUowncZiTH4z74Ec1olUlX0OTYnUNGDvWxX1o%3D&reserved=0
> 
> Requiring -fno-strict-aliasing seems unfortunate and so this patch
> makes the unaligned code work via memcpy for type punning rather than
> the packed attribute.
> 
> v2: switch memcpy to __builtin_memcpy to avoid potential/disallowed
>      memcpy calls in vdso caused by -fno-builtin. Reported by
>      Christophe Leroy <christophe.leroy@csgroup.eu>:
>      https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flore.kernel.org%2Flkml%2Fc57de5bf-d55c-48c5-9dfa-e2fb844dafe9%40csgroup.eu%2F&data=05%7C02%7Cchristophe.leroy%40csgroup.eu%7Cf05413010ecc40ad1bdf08ddade1316a%7C8b87af7d86474dc78df45f69a2011bb5%7C0%7C0%7C638857904894985987%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=l1LJP3XPKVuhDpiHxxkfWBiNPMYaBT9YXGBFzs6wLpY%3D&reserved=0
> 

Does this new version also fixes the failures reported by the robots:
- arm64-randconfig with clang 21
- i386-randconfig with gcc 12

Christophe

> Ian Rogers (3):
>    vdso: Switch get/put unaligned from packed struct to memcpy
>    tools headers: Update the linux/unaligned.h copy with the kernel
>      sources
>    tools headers: Remove unneeded ignoring of warnings in unaligned.h
> 
>   include/vdso/unaligned.h        | 48 ++++++++++++++++++++++++++++-----
>   tools/include/linux/unaligned.h |  4 ---
>   tools/include/vdso/unaligned.h  | 48 ++++++++++++++++++++++++++++-----
>   3 files changed, 84 insertions(+), 16 deletions(-)
> 

Re: [PATCH v2 0/3] Switch get/put unaligned to use memcpy
Posted by Ian Rogers 3 months, 2 weeks ago
On Wed, Jun 18, 2025 at 4:42 AM Christophe Leroy
<christophe.leroy@csgroup.eu> wrote:
>
>
>
> Le 17/06/2025 à 22:53, Ian Rogers a écrit :
> > The existing type punning approach with packed structs requires
> >   -fno-strict-aliasing to be passed to the compiler for
> > correctness. This is true in the kernel tree but not in the tools
> > directory resulting in this suggested patch from Eric Biggers
> >   <ebiggers@google.com>:
> > https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flore.kernel.org%2Flkml%2F20250614044133.660848-2-ebiggers%40kernel.org%2F&data=05%7C02%7Cchristophe.leroy%40csgroup.eu%7Cf05413010ecc40ad1bdf08ddade1316a%7C8b87af7d86474dc78df45f69a2011bb5%7C0%7C0%7C638857904894967529%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=29QUBLnUowncZiTH4z74Ec1olUlX0OTYnUNGDvWxX1o%3D&reserved=0
> >
> > Requiring -fno-strict-aliasing seems unfortunate and so this patch
> > makes the unaligned code work via memcpy for type punning rather than
> > the packed attribute.
> >
> > v2: switch memcpy to __builtin_memcpy to avoid potential/disallowed
> >      memcpy calls in vdso caused by -fno-builtin. Reported by
> >      Christophe Leroy <christophe.leroy@csgroup.eu>:
> >      https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flore.kernel.org%2Flkml%2Fc57de5bf-d55c-48c5-9dfa-e2fb844dafe9%40csgroup.eu%2F&data=05%7C02%7Cchristophe.leroy%40csgroup.eu%7Cf05413010ecc40ad1bdf08ddade1316a%7C8b87af7d86474dc78df45f69a2011bb5%7C0%7C0%7C638857904894985987%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=l1LJP3XPKVuhDpiHxxkfWBiNPMYaBT9YXGBFzs6wLpY%3D&reserved=0
> >

So I think I can remove the _Generic block in unaligned.h by depending
on the equivalent __unqual_scalar_typeof from
include/linux/compiler_types.h. I think this is preferable and so I'll
post a v3.

> Does this new version also fixes the failures reported by the robots:
> - arm64-randconfig with clang 21

The issue here was:

  llvm-readelf: warning: 'arch/arm64/kernel/vdso/vdso.so.dbg': invalid
PT_DYNAMIC size (0x118)
  llvm-readelf: warning: 'arch/arm64/kernel/vdso/vdso.so.dbg':
PT_DYNAMIC dynamic table is invalid: SHT_DYNAMIC will be used

which the switch to __builtin_memcpy should have resolved.

> - i386-randconfig with gcc 12

The reported error is in the unaligned code from sparse:

   drivers/scsi/megaraid/megaraid_sas_base.c:8438:32: sparse: sparse:
Using plain integer as NULL pointer

I believe this is because of the _Generic block with a pointer type,
and so the use of __unqual_scalar_typeof in the next/v3 patch set
should hopefully resolve it - or else we'll need to special case
pointer types to assign NULL rather than 0, but I'd prefer not to
special case every potential pointer type.

Thanks,
Ian

> Christophe
>
> > Ian Rogers (3):
> >    vdso: Switch get/put unaligned from packed struct to memcpy
> >    tools headers: Update the linux/unaligned.h copy with the kernel
> >      sources
> >    tools headers: Remove unneeded ignoring of warnings in unaligned.h
> >
> >   include/vdso/unaligned.h        | 48 ++++++++++++++++++++++++++++-----
> >   tools/include/linux/unaligned.h |  4 ---
> >   tools/include/vdso/unaligned.h  | 48 ++++++++++++++++++++++++++++-----
> >   3 files changed, 84 insertions(+), 16 deletions(-)
> >
>