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

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>
---
Changelog:
v1 -> v2:
- deduplicated and moved core algorithm into relo_core.c
- adjusted bpf_core_names_match to get btf_type passed in
- removed some length equality checks before strncmp usage
- correctly use kflag from targ_t instead of local_t
- added comment for meaning of kflag w/ FWD kind
- __u32 -> u32
- handle BTF_KIND_FWD properly in bpftool marking logic
- rebased

Daniel Müller (9):
  bpf: Introduce TYPE_MATCH related constants/macros
  bpftool: Honor BPF_CORE_TYPE_MATCHES relocation
  bpf: Introduce btf_int_bits() function
  libbpf: Add type match support
  bpf: Add type match support
  libbpf: Honor TYPE_MATCH relocation
  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                              |   9 +
 tools/bpf/bpftool/gen.c                       | 107 +++++++
 tools/include/uapi/linux/bpf.h                |   1 +
 tools/lib/bpf/bpf_core_read.h                 |  11 +
 tools/lib/bpf/libbpf.c                        |   6 +
 tools/lib/bpf/relo_core.c                     | 292 +++++++++++++++++-
 tools/lib/bpf/relo_core.h                     |   4 +
 .../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, 644 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] 19+ messages in thread

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

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-23 21:21 [PATCH bpf-next v2 0/9] Introduce type match support Daniel Müller
2022-06-23 21:21 ` [PATCH bpf-next v2 1/9] bpf: Introduce TYPE_MATCH related constants/macros Daniel Müller
2022-06-23 21:21 ` [PATCH bpf-next v2 2/9] bpftool: Honor BPF_CORE_TYPE_MATCHES relocation Daniel Müller
2022-06-24 11:37   ` Quentin Monnet
2022-06-27 16:43     ` Daniel Müller
2022-06-24 21:25   ` Andrii Nakryiko
2022-06-27 16:50     ` Daniel Müller
2022-06-23 21:21 ` [PATCH bpf-next v2 3/9] bpf: Introduce btf_int_bits() function Daniel Müller
2022-06-23 21:22 ` [PATCH bpf-next v2 4/9] libbpf: Add type match support Daniel Müller
2022-06-24 21:39   ` Andrii Nakryiko
2022-06-27 21:28     ` Daniel Müller
2022-06-23 21:22 ` [PATCH bpf-next v2 5/9] bpf: " Daniel Müller
2022-06-23 21:22 ` [PATCH bpf-next v2 6/9] libbpf: Honor TYPE_MATCH relocation Daniel Müller
2022-06-24 21:41   ` Andrii Nakryiko
2022-06-23 21:22 ` [PATCH bpf-next v2 7/9] selftests/bpf: Add type-match checks to type-based tests Daniel Müller
2022-06-23 21:22 ` [PATCH bpf-next v2 8/9] selftests/bpf: Add test checking more characteristics Daniel Müller
2022-06-23 21:22 ` [PATCH bpf-next v2 9/9] selftests/bpf: Add nested type to type based tests Daniel Müller
2022-06-24 21:45   ` Andrii Nakryiko
2022-06-27 23:06     ` 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.