gendwarfksyms: Add a kABI rule to override byte_size attributes
authorSami Tolvanen <samitolvanen@google.com>
Wed, 7 May 2025 23:14:06 +0000 (23:14 +0000)
committerMasahiro Yamada <masahiroy@kernel.org>
Sun, 25 May 2025 09:12:22 +0000 (18:12 +0900)
commitdb59d74e5da144111fc133fb1bf72e6392bdb04e
tree14232837420059177251337b4b7002a3bec6064e
parentff2c5f5a9e01b9fc3b4959c2b3f40843cc0a5ecb
gendwarfksyms: Add a kABI rule to override byte_size attributes

A data structure can be partially opaque to modules if its
allocation is handled by the core kernel, and modules only need
to access some of its members. In this situation, it's possible
to append new members to the structure without breaking the ABI,
as long as the layout for the original members remains unchanged.
For example, consider the following struct:

  struct s {
          unsigned long a;
          void *p;
  };

gendwarfksyms --stable --dump-dies produces the following type
expansion:

  variable structure_type s {
    member base_type long unsigned int byte_size(8) encoding(7) a
      data_member_location(0) ,
    member pointer_type {
      base_type void
    } byte_size(8) p data_member_location(8)
  } byte_size(16)

To append new members, we can use the KABI_IGNORE() macro to
hide them from gendwarfksyms --stable:

  struct s {
          /* old members with unchanged layout */
          unsigned long a;
          void *p;

          /* new members not accessed by modules */
          KABI_IGNORE(0, unsigned long n);
  };

However, we can't hide the fact that adding new members changes
the struct size, as seen in the updated type string:

  variable structure_type s {
    member base_type long unsigned int byte_size(8) encoding(7) a
      data_member_location(0) ,
    member pointer_type {
      base_type void
    } byte_size(8) p data_member_location(8)
  } byte_size(24)

In order to support this use case, add a kABI rule that makes it
possible to override the byte_size attribute for types:

  /*
   * struct s allocation is handled by the kernel, so
   * appending new members without changing the original
   * layout won't break the ABI.
   */
  KABI_BYTE_SIZE(s, 16);

This results in a type string that's unchanged from the original
and therefore, won't change versions for symbols that reference
the changed structure.

Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
Reviewed-by: Petr Pavlu <petr.pavlu@suse.com>
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
scripts/gendwarfksyms/dwarf.c
scripts/gendwarfksyms/examples/kabi.h
scripts/gendwarfksyms/examples/kabi_ex.c
scripts/gendwarfksyms/examples/kabi_ex.h
scripts/gendwarfksyms/gendwarfksyms.h
scripts/gendwarfksyms/kabi.c