Skip to content

Commit 027bdec

Browse files
Byte-LabAlexei Starovoitov
authored andcommitted
bpf/docs: Document the nocast aliasing behavior of ___init
When comparing BTF IDs for pointers being passed to kfunc arguments, the verifier will allow pointer types that are equivalent according to the C standard. For example, for: struct bpf_cpumask { cpumask_t cpumask; refcount_t usage; }; The verifier will allow a struct bpf_cpumask * to be passed to a kfunc that takes a const struct cpumask * (cpumask_t is a typedef of struct cpumask). The exception to this rule is if a type is suffixed with ___init, such as: struct nf_conn___init { struct nf_conn ct; }; The verifier will _not_ allow a struct nf_conn___init * to be passed to a kfunc that expects a struct nf_conn *. This patch documents this behavior in the kfuncs documentation page. Signed-off-by: David Vernet <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent d94cbde commit 027bdec

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

Documentation/bpf/kfuncs.rst

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,49 @@ type. An example is shown below::
247247
}
248248
late_initcall(init_subsystem);
249249

250+
2.6 Specifying no-cast aliases with ___init
251+
--------------------------------------------
252+
253+
The verifier will always enforce that the BTF type of a pointer passed to a
254+
kfunc by a BPF program, matches the type of pointer specified in the kfunc
255+
definition. The verifier, does, however, allow types that are equivalent
256+
according to the C standard to be passed to the same kfunc arg, even if their
257+
BTF_IDs differ.
258+
259+
For example, for the following type definition:
260+
261+
.. code-block:: c
262+
263+
struct bpf_cpumask {
264+
cpumask_t cpumask;
265+
refcount_t usage;
266+
};
267+
268+
The verifier would allow a ``struct bpf_cpumask *`` to be passed to a kfunc
269+
taking a ``cpumask_t *`` (which is a typedef of ``struct cpumask *``). For
270+
instance, both ``struct cpumask *`` and ``struct bpf_cpmuask *`` can be passed
271+
to bpf_cpumask_test_cpu().
272+
273+
In some cases, this type-aliasing behavior is not desired. ``struct
274+
nf_conn___init`` is one such example:
275+
276+
.. code-block:: c
277+
278+
struct nf_conn___init {
279+
struct nf_conn ct;
280+
};
281+
282+
The C standard would consider these types to be equivalent, but it would not
283+
always be safe to pass either type to a trusted kfunc. ``struct
284+
nf_conn___init`` represents an allocated ``struct nf_conn`` object that has
285+
*not yet been initialized*, so it would therefore be unsafe to pass a ``struct
286+
nf_conn___init *`` to a kfunc that's expecting a fully initialized ``struct
287+
nf_conn *`` (e.g. ``bpf_ct_change_timeout()``).
288+
289+
In order to accommodate such requirements, the verifier will enforce strict
290+
PTR_TO_BTF_ID type matching if two types have the exact same name, with one
291+
being suffixed with ``___init``.
292+
250293
3. Core kfuncs
251294
==============
252295

0 commit comments

Comments
 (0)