All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf-next 0/7] Introduce type match support
@ 2022-06-20 23:17 Daniel Müller
  2022-06-20 23:17 ` [PATCH bpf-next 1/7] bpf: Introduce TYPE_MATCH related constants/macros Daniel Müller
                   ` (6 more replies)
  0 siblings, 7 replies; 20+ messages in thread
From: Daniel Müller @ 2022-06-20 23:17 UTC (permalink / raw)
  To: bpf, ast, andrii, daniel, kernel-team

This patch set proposes the addition of a new way for performing type queries to
BPF. It introduces the "type matches" relation, similar to what is already
present with "type exists" (in the form of bpf_core_type_exists).

"type exists" performs fairly superficial checking, mostly concerned with
whether a type exists in the kernel and is of the same kind (enum/struct/...).
Notably, compatibility checks for members of composite types is lacking.

The newly introduced "type matches" (bpf_core_type_matches) fills this gap in
that it performs stricter checks: compatibility of members and existence of
similarly named enum variants is checked as well. E.g., given these definitions:

	struct task_struct___og { int pid; int tgid; };

	struct task_struct___foo { int foo; }

'task_struct___og' would "match" the kernel type 'task_struct', because the
members match up, while 'task_struct___foo' would not match, because the
kernel's 'task_struct' has no member named 'foo'.

More precisely, the "type match" relation is defined as follows (copied from
source):
- modifiers and typedefs are stripped (and, hence, effectively ignored)
- generally speaking types need to be of same kind (struct vs. struct, union
  vs. union, etc.)
  - exceptions are struct/union behind a pointer which could also match a
    forward declaration of a struct or union, respectively, and enum vs.
    enum64 (see below)
Then, depending on type:
- integers:
  - match if size and signedness match
- arrays & pointers:
  - target types are recursively matched
- structs & unions:
  - local members need to exist in target with the same name
  - for each member we recursively check match unless it is already behind a
    pointer, in which case we only check matching names and compatible kind
- enums:
  - local variants have to have a match in target by symbolic name (but not
    numeric value)
  - size has to match (but enum may match enum64 and vice versa)
- function pointers:
  - number and position of arguments in local type has to match target
  - for each argument and the return value we recursively check match

Enabling this feature requires a new relocation to be made known to the
compiler. This is being taken care of for LLVM as part of
https://reviews.llvm.org/D126838.

If applied, among other things, usage of this functionality could have helped
flag issues such as the one discussed here
https://lore.kernel.org/all/93a20759600c05b6d9e4359a1517c88e06b44834.camel@fb.com/
earlier.

Suggested-by: Andrii Nakryiko <andrii@kernel.org>

Daniel Müller (7):
  bpf: Introduce TYPE_MATCH related constants/macros
  bpftool: Honor BPF_CORE_TYPE_MATCHES relocation
  bpf: Add type match support
  libbpf: Add type match support
  selftests/bpf: Add type-match checks to type-based tests
  selftests/bpf: Add test checking more characteristics
  selftests/bpf: Add nested type to type based tests

 include/linux/btf.h                           |   5 +
 include/uapi/linux/bpf.h                      |   1 +
 kernel/bpf/btf.c                              | 267 +++++++++++++++++
 tools/bpf/bpftool/gen.c                       | 106 +++++++
 tools/include/uapi/linux/bpf.h                |   1 +
 tools/lib/bpf/bpf_core_read.h                 |  11 +
 tools/lib/bpf/libbpf.c                        | 274 ++++++++++++++++++
 tools/lib/bpf/relo_core.c                     |  16 +-
 tools/lib/bpf/relo_core.h                     |   2 +
 .../selftests/bpf/prog_tests/core_reloc.c     |  72 ++++-
 .../progs/btf__core_reloc_type_based___diff.c |   3 +
 .../selftests/bpf/progs/core_reloc_types.h    | 107 ++++++-
 .../bpf/progs/test_core_reloc_type_based.c    |  44 ++-
 13 files changed, 891 insertions(+), 18 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_type_based___diff.c

-- 
2.30.2


^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2022-06-27 21:12 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-20 23:17 [PATCH bpf-next 0/7] Introduce type match support Daniel Müller
2022-06-20 23:17 ` [PATCH bpf-next 1/7] bpf: Introduce TYPE_MATCH related constants/macros Daniel Müller
2022-06-20 23:17 ` [PATCH bpf-next 2/7] bpftool: Honor BPF_CORE_TYPE_MATCHES relocation Daniel Müller
2022-06-20 23:17 ` [PATCH bpf-next 3/7] bpf: Add type match support Daniel Müller
2022-06-21 19:41   ` Joanne Koong
2022-06-22 17:22     ` Daniel Müller
2022-06-23 19:19       ` Daniel Müller
2022-06-24 21:09       ` Andrii Nakryiko
2022-06-27 17:34         ` Daniel Müller
2022-06-27 21:03         ` Daniel Müller
2022-06-27 21:12           ` Andrii Nakryiko
2022-06-20 23:17 ` [PATCH bpf-next 4/7] libbpf: " Daniel Müller
2022-06-20 23:59   ` Alexei Starovoitov
2022-06-21 16:45     ` Daniel Müller
2022-06-21 18:44       ` Alexei Starovoitov
2022-06-21 21:10         ` Daniel Müller
2022-06-21 21:14           ` Alexei Starovoitov
2022-06-20 23:17 ` [PATCH bpf-next 5/7] selftests/bpf: Add type-match checks to type-based tests Daniel Müller
2022-06-20 23:17 ` [PATCH bpf-next 6/7] selftests/bpf: Add test checking more characteristics Daniel Müller
2022-06-20 23:17 ` [PATCH bpf-next 7/7] selftests/bpf: Add nested type to type based tests Daniel Müller

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.