bpf: Enable cpumasks to be queried and used as kptrs
Certain programs may wish to be able to query cpumasks. For example, if
a program that is tracing percpu operations wishes to track which tasks
end up running on which CPUs, it could be useful to associate that with
the tasks' cpumasks. Similarly, programs tracking NUMA allocations, CPU
scheduling domains, etc, could potentially benefit from being able to
see which CPUs a task could be migrated to.
This patch enables these types of use cases by introducing a series of
bpf_cpumask_* kfuncs. Amongst these kfuncs, there are two separate
"classes" of operations:
1. kfuncs which allow the caller to allocate and mutate their own
cpumask kptrs in the form of a struct bpf_cpumask * object. Such
kfuncs include e.g. bpf_cpumask_create() to allocate the cpumask, and
bpf_cpumask_or() to mutate it. "Regular" cpumasks such as p->cpus_ptr
may not be passed to these kfuncs, and the verifier will ensure this
is the case by comparing BTF IDs.
2. Read-only operations which operate on const struct cpumask *
arguments. For example, bpf_cpumask_test_cpu(), which tests whether a
CPU is set in the cpumask. Any trusted struct cpumask * or struct
bpf_cpumask * may be passed to these kfuncs. The verifier allows
struct bpf_cpumask * even though the kfunc is defined with struct
cpumask * because the first element of a struct bpf_cpumask is a
cpumask_t, so it is safe to cast.
A follow-on patch will add selftests which validate these kfuncs, and
another will document them.
Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230125143816.721952-3-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-25 07:38:11 -07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
/* Copyright (c) 2023 Meta, Inc */
|
|
|
|
#include <linux/bpf.h>
|
|
|
|
#include <linux/bpf_mem_alloc.h>
|
|
|
|
#include <linux/btf.h>
|
|
|
|
#include <linux/btf_ids.h>
|
|
|
|
#include <linux/cpumask.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* struct bpf_cpumask - refcounted BPF cpumask wrapper structure
|
|
|
|
* @cpumask: The actual cpumask embedded in the struct.
|
|
|
|
* @usage: Object reference counter. When the refcount goes to 0, the
|
|
|
|
* memory is released back to the BPF allocator, which provides
|
|
|
|
* RCU safety.
|
|
|
|
*
|
|
|
|
* Note that we explicitly embed a cpumask_t rather than a cpumask_var_t. This
|
|
|
|
* is done to avoid confusing the verifier due to the typedef of cpumask_var_t
|
|
|
|
* changing depending on whether CONFIG_CPUMASK_OFFSTACK is defined or not. See
|
|
|
|
* the details in <linux/cpumask.h>. The consequence is that this structure is
|
|
|
|
* likely a bit larger than it needs to be when CONFIG_CPUMASK_OFFSTACK is
|
|
|
|
* defined due to embedding the whole NR_CPUS-size bitmap, but the extra memory
|
|
|
|
* overhead is minimal. For the more typical case of CONFIG_CPUMASK_OFFSTACK
|
|
|
|
* not being defined, the structure is the same size regardless.
|
|
|
|
*/
|
|
|
|
struct bpf_cpumask {
|
|
|
|
cpumask_t cpumask;
|
|
|
|
refcount_t usage;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct bpf_mem_alloc bpf_cpumask_ma;
|
|
|
|
|
|
|
|
static bool cpu_valid(u32 cpu)
|
|
|
|
{
|
|
|
|
return cpu < nr_cpu_ids;
|
|
|
|
}
|
|
|
|
|
2023-10-31 14:56:24 -07:00
|
|
|
__bpf_kfunc_start_defs();
|
bpf: Enable cpumasks to be queried and used as kptrs
Certain programs may wish to be able to query cpumasks. For example, if
a program that is tracing percpu operations wishes to track which tasks
end up running on which CPUs, it could be useful to associate that with
the tasks' cpumasks. Similarly, programs tracking NUMA allocations, CPU
scheduling domains, etc, could potentially benefit from being able to
see which CPUs a task could be migrated to.
This patch enables these types of use cases by introducing a series of
bpf_cpumask_* kfuncs. Amongst these kfuncs, there are two separate
"classes" of operations:
1. kfuncs which allow the caller to allocate and mutate their own
cpumask kptrs in the form of a struct bpf_cpumask * object. Such
kfuncs include e.g. bpf_cpumask_create() to allocate the cpumask, and
bpf_cpumask_or() to mutate it. "Regular" cpumasks such as p->cpus_ptr
may not be passed to these kfuncs, and the verifier will ensure this
is the case by comparing BTF IDs.
2. Read-only operations which operate on const struct cpumask *
arguments. For example, bpf_cpumask_test_cpu(), which tests whether a
CPU is set in the cpumask. Any trusted struct cpumask * or struct
bpf_cpumask * may be passed to these kfuncs. The verifier allows
struct bpf_cpumask * even though the kfunc is defined with struct
cpumask * because the first element of a struct bpf_cpumask is a
cpumask_t, so it is safe to cast.
A follow-on patch will add selftests which validate these kfuncs, and
another will document them.
Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230125143816.721952-3-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-25 07:38:11 -07:00
|
|
|
|
2023-01-25 07:38:14 -07:00
|
|
|
/**
|
|
|
|
* bpf_cpumask_create() - Create a mutable BPF cpumask.
|
|
|
|
*
|
|
|
|
* Allocates a cpumask that can be queried, mutated, acquired, and released by
|
|
|
|
* a BPF program. The cpumask returned by this function must either be embedded
|
|
|
|
* in a map as a kptr, or freed with bpf_cpumask_release().
|
|
|
|
*
|
|
|
|
* bpf_cpumask_create() allocates memory using the BPF memory allocator, and
|
|
|
|
* will not block. It may return NULL if no memory is available.
|
|
|
|
*/
|
2023-02-01 10:30:15 -07:00
|
|
|
__bpf_kfunc struct bpf_cpumask *bpf_cpumask_create(void)
|
bpf: Enable cpumasks to be queried and used as kptrs
Certain programs may wish to be able to query cpumasks. For example, if
a program that is tracing percpu operations wishes to track which tasks
end up running on which CPUs, it could be useful to associate that with
the tasks' cpumasks. Similarly, programs tracking NUMA allocations, CPU
scheduling domains, etc, could potentially benefit from being able to
see which CPUs a task could be migrated to.
This patch enables these types of use cases by introducing a series of
bpf_cpumask_* kfuncs. Amongst these kfuncs, there are two separate
"classes" of operations:
1. kfuncs which allow the caller to allocate and mutate their own
cpumask kptrs in the form of a struct bpf_cpumask * object. Such
kfuncs include e.g. bpf_cpumask_create() to allocate the cpumask, and
bpf_cpumask_or() to mutate it. "Regular" cpumasks such as p->cpus_ptr
may not be passed to these kfuncs, and the verifier will ensure this
is the case by comparing BTF IDs.
2. Read-only operations which operate on const struct cpumask *
arguments. For example, bpf_cpumask_test_cpu(), which tests whether a
CPU is set in the cpumask. Any trusted struct cpumask * or struct
bpf_cpumask * may be passed to these kfuncs. The verifier allows
struct bpf_cpumask * even though the kfunc is defined with struct
cpumask * because the first element of a struct bpf_cpumask is a
cpumask_t, so it is safe to cast.
A follow-on patch will add selftests which validate these kfuncs, and
another will document them.
Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230125143816.721952-3-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-25 07:38:11 -07:00
|
|
|
{
|
|
|
|
struct bpf_cpumask *cpumask;
|
|
|
|
|
2023-01-28 07:15:37 -07:00
|
|
|
/* cpumask must be the first element so struct bpf_cpumask be cast to struct cpumask. */
|
|
|
|
BUILD_BUG_ON(offsetof(struct bpf_cpumask, cpumask) != 0);
|
|
|
|
|
2023-02-15 19:48:21 -07:00
|
|
|
cpumask = bpf_mem_cache_alloc(&bpf_cpumask_ma);
|
bpf: Enable cpumasks to be queried and used as kptrs
Certain programs may wish to be able to query cpumasks. For example, if
a program that is tracing percpu operations wishes to track which tasks
end up running on which CPUs, it could be useful to associate that with
the tasks' cpumasks. Similarly, programs tracking NUMA allocations, CPU
scheduling domains, etc, could potentially benefit from being able to
see which CPUs a task could be migrated to.
This patch enables these types of use cases by introducing a series of
bpf_cpumask_* kfuncs. Amongst these kfuncs, there are two separate
"classes" of operations:
1. kfuncs which allow the caller to allocate and mutate their own
cpumask kptrs in the form of a struct bpf_cpumask * object. Such
kfuncs include e.g. bpf_cpumask_create() to allocate the cpumask, and
bpf_cpumask_or() to mutate it. "Regular" cpumasks such as p->cpus_ptr
may not be passed to these kfuncs, and the verifier will ensure this
is the case by comparing BTF IDs.
2. Read-only operations which operate on const struct cpumask *
arguments. For example, bpf_cpumask_test_cpu(), which tests whether a
CPU is set in the cpumask. Any trusted struct cpumask * or struct
bpf_cpumask * may be passed to these kfuncs. The verifier allows
struct bpf_cpumask * even though the kfunc is defined with struct
cpumask * because the first element of a struct bpf_cpumask is a
cpumask_t, so it is safe to cast.
A follow-on patch will add selftests which validate these kfuncs, and
another will document them.
Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230125143816.721952-3-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-25 07:38:11 -07:00
|
|
|
if (!cpumask)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
memset(cpumask, 0, sizeof(*cpumask));
|
|
|
|
refcount_set(&cpumask->usage, 1);
|
|
|
|
|
|
|
|
return cpumask;
|
|
|
|
}
|
|
|
|
|
2023-01-25 07:38:14 -07:00
|
|
|
/**
|
|
|
|
* bpf_cpumask_acquire() - Acquire a reference to a BPF cpumask.
|
|
|
|
* @cpumask: The BPF cpumask being acquired. The cpumask must be a trusted
|
|
|
|
* pointer.
|
|
|
|
*
|
|
|
|
* Acquires a reference to a BPF cpumask. The cpumask returned by this function
|
|
|
|
* must either be embedded in a map as a kptr, or freed with
|
|
|
|
* bpf_cpumask_release().
|
|
|
|
*/
|
2023-02-01 10:30:15 -07:00
|
|
|
__bpf_kfunc struct bpf_cpumask *bpf_cpumask_acquire(struct bpf_cpumask *cpumask)
|
bpf: Enable cpumasks to be queried and used as kptrs
Certain programs may wish to be able to query cpumasks. For example, if
a program that is tracing percpu operations wishes to track which tasks
end up running on which CPUs, it could be useful to associate that with
the tasks' cpumasks. Similarly, programs tracking NUMA allocations, CPU
scheduling domains, etc, could potentially benefit from being able to
see which CPUs a task could be migrated to.
This patch enables these types of use cases by introducing a series of
bpf_cpumask_* kfuncs. Amongst these kfuncs, there are two separate
"classes" of operations:
1. kfuncs which allow the caller to allocate and mutate their own
cpumask kptrs in the form of a struct bpf_cpumask * object. Such
kfuncs include e.g. bpf_cpumask_create() to allocate the cpumask, and
bpf_cpumask_or() to mutate it. "Regular" cpumasks such as p->cpus_ptr
may not be passed to these kfuncs, and the verifier will ensure this
is the case by comparing BTF IDs.
2. Read-only operations which operate on const struct cpumask *
arguments. For example, bpf_cpumask_test_cpu(), which tests whether a
CPU is set in the cpumask. Any trusted struct cpumask * or struct
bpf_cpumask * may be passed to these kfuncs. The verifier allows
struct bpf_cpumask * even though the kfunc is defined with struct
cpumask * because the first element of a struct bpf_cpumask is a
cpumask_t, so it is safe to cast.
A follow-on patch will add selftests which validate these kfuncs, and
another will document them.
Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230125143816.721952-3-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-25 07:38:11 -07:00
|
|
|
{
|
|
|
|
refcount_inc(&cpumask->usage);
|
|
|
|
return cpumask;
|
|
|
|
}
|
|
|
|
|
2023-01-25 07:38:14 -07:00
|
|
|
/**
|
|
|
|
* bpf_cpumask_release() - Release a previously acquired BPF cpumask.
|
|
|
|
* @cpumask: The cpumask being released.
|
|
|
|
*
|
|
|
|
* Releases a previously acquired reference to a BPF cpumask. When the final
|
|
|
|
* reference of the BPF cpumask has been released, it is subsequently freed in
|
|
|
|
* an RCU callback in the BPF memory allocator.
|
|
|
|
*/
|
2023-02-01 10:30:15 -07:00
|
|
|
__bpf_kfunc void bpf_cpumask_release(struct bpf_cpumask *cpumask)
|
bpf: Enable cpumasks to be queried and used as kptrs
Certain programs may wish to be able to query cpumasks. For example, if
a program that is tracing percpu operations wishes to track which tasks
end up running on which CPUs, it could be useful to associate that with
the tasks' cpumasks. Similarly, programs tracking NUMA allocations, CPU
scheduling domains, etc, could potentially benefit from being able to
see which CPUs a task could be migrated to.
This patch enables these types of use cases by introducing a series of
bpf_cpumask_* kfuncs. Amongst these kfuncs, there are two separate
"classes" of operations:
1. kfuncs which allow the caller to allocate and mutate their own
cpumask kptrs in the form of a struct bpf_cpumask * object. Such
kfuncs include e.g. bpf_cpumask_create() to allocate the cpumask, and
bpf_cpumask_or() to mutate it. "Regular" cpumasks such as p->cpus_ptr
may not be passed to these kfuncs, and the verifier will ensure this
is the case by comparing BTF IDs.
2. Read-only operations which operate on const struct cpumask *
arguments. For example, bpf_cpumask_test_cpu(), which tests whether a
CPU is set in the cpumask. Any trusted struct cpumask * or struct
bpf_cpumask * may be passed to these kfuncs. The verifier allows
struct bpf_cpumask * even though the kfunc is defined with struct
cpumask * because the first element of a struct bpf_cpumask is a
cpumask_t, so it is safe to cast.
A follow-on patch will add selftests which validate these kfuncs, and
another will document them.
Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230125143816.721952-3-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-25 07:38:11 -07:00
|
|
|
{
|
2023-07-05 20:34:46 -07:00
|
|
|
if (!refcount_dec_and_test(&cpumask->usage))
|
|
|
|
return;
|
|
|
|
|
|
|
|
migrate_disable();
|
|
|
|
bpf_mem_cache_free_rcu(&bpf_cpumask_ma, cpumask);
|
|
|
|
migrate_enable();
|
bpf: Enable cpumasks to be queried and used as kptrs
Certain programs may wish to be able to query cpumasks. For example, if
a program that is tracing percpu operations wishes to track which tasks
end up running on which CPUs, it could be useful to associate that with
the tasks' cpumasks. Similarly, programs tracking NUMA allocations, CPU
scheduling domains, etc, could potentially benefit from being able to
see which CPUs a task could be migrated to.
This patch enables these types of use cases by introducing a series of
bpf_cpumask_* kfuncs. Amongst these kfuncs, there are two separate
"classes" of operations:
1. kfuncs which allow the caller to allocate and mutate their own
cpumask kptrs in the form of a struct bpf_cpumask * object. Such
kfuncs include e.g. bpf_cpumask_create() to allocate the cpumask, and
bpf_cpumask_or() to mutate it. "Regular" cpumasks such as p->cpus_ptr
may not be passed to these kfuncs, and the verifier will ensure this
is the case by comparing BTF IDs.
2. Read-only operations which operate on const struct cpumask *
arguments. For example, bpf_cpumask_test_cpu(), which tests whether a
CPU is set in the cpumask. Any trusted struct cpumask * or struct
bpf_cpumask * may be passed to these kfuncs. The verifier allows
struct bpf_cpumask * even though the kfunc is defined with struct
cpumask * because the first element of a struct bpf_cpumask is a
cpumask_t, so it is safe to cast.
A follow-on patch will add selftests which validate these kfuncs, and
another will document them.
Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230125143816.721952-3-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-25 07:38:11 -07:00
|
|
|
}
|
|
|
|
|
2023-12-15 02:12:22 -07:00
|
|
|
__bpf_kfunc void bpf_cpumask_release_dtor(void *cpumask)
|
|
|
|
{
|
|
|
|
bpf_cpumask_release(cpumask);
|
|
|
|
}
|
|
|
|
CFI_NOSEAL(bpf_cpumask_release_dtor);
|
|
|
|
|
2023-01-25 07:38:14 -07:00
|
|
|
/**
|
|
|
|
* bpf_cpumask_first() - Get the index of the first nonzero bit in the cpumask.
|
|
|
|
* @cpumask: The cpumask being queried.
|
|
|
|
*
|
|
|
|
* Find the index of the first nonzero bit of the cpumask. A struct bpf_cpumask
|
|
|
|
* pointer may be safely passed to this function.
|
|
|
|
*/
|
2023-02-01 10:30:15 -07:00
|
|
|
__bpf_kfunc u32 bpf_cpumask_first(const struct cpumask *cpumask)
|
bpf: Enable cpumasks to be queried and used as kptrs
Certain programs may wish to be able to query cpumasks. For example, if
a program that is tracing percpu operations wishes to track which tasks
end up running on which CPUs, it could be useful to associate that with
the tasks' cpumasks. Similarly, programs tracking NUMA allocations, CPU
scheduling domains, etc, could potentially benefit from being able to
see which CPUs a task could be migrated to.
This patch enables these types of use cases by introducing a series of
bpf_cpumask_* kfuncs. Amongst these kfuncs, there are two separate
"classes" of operations:
1. kfuncs which allow the caller to allocate and mutate their own
cpumask kptrs in the form of a struct bpf_cpumask * object. Such
kfuncs include e.g. bpf_cpumask_create() to allocate the cpumask, and
bpf_cpumask_or() to mutate it. "Regular" cpumasks such as p->cpus_ptr
may not be passed to these kfuncs, and the verifier will ensure this
is the case by comparing BTF IDs.
2. Read-only operations which operate on const struct cpumask *
arguments. For example, bpf_cpumask_test_cpu(), which tests whether a
CPU is set in the cpumask. Any trusted struct cpumask * or struct
bpf_cpumask * may be passed to these kfuncs. The verifier allows
struct bpf_cpumask * even though the kfunc is defined with struct
cpumask * because the first element of a struct bpf_cpumask is a
cpumask_t, so it is safe to cast.
A follow-on patch will add selftests which validate these kfuncs, and
another will document them.
Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230125143816.721952-3-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-25 07:38:11 -07:00
|
|
|
{
|
|
|
|
return cpumask_first(cpumask);
|
|
|
|
}
|
|
|
|
|
2023-01-25 07:38:14 -07:00
|
|
|
/**
|
|
|
|
* bpf_cpumask_first_zero() - Get the index of the first unset bit in the
|
|
|
|
* cpumask.
|
|
|
|
* @cpumask: The cpumask being queried.
|
|
|
|
*
|
|
|
|
* Find the index of the first unset bit of the cpumask. A struct bpf_cpumask
|
|
|
|
* pointer may be safely passed to this function.
|
|
|
|
*/
|
2023-02-01 10:30:15 -07:00
|
|
|
__bpf_kfunc u32 bpf_cpumask_first_zero(const struct cpumask *cpumask)
|
bpf: Enable cpumasks to be queried and used as kptrs
Certain programs may wish to be able to query cpumasks. For example, if
a program that is tracing percpu operations wishes to track which tasks
end up running on which CPUs, it could be useful to associate that with
the tasks' cpumasks. Similarly, programs tracking NUMA allocations, CPU
scheduling domains, etc, could potentially benefit from being able to
see which CPUs a task could be migrated to.
This patch enables these types of use cases by introducing a series of
bpf_cpumask_* kfuncs. Amongst these kfuncs, there are two separate
"classes" of operations:
1. kfuncs which allow the caller to allocate and mutate their own
cpumask kptrs in the form of a struct bpf_cpumask * object. Such
kfuncs include e.g. bpf_cpumask_create() to allocate the cpumask, and
bpf_cpumask_or() to mutate it. "Regular" cpumasks such as p->cpus_ptr
may not be passed to these kfuncs, and the verifier will ensure this
is the case by comparing BTF IDs.
2. Read-only operations which operate on const struct cpumask *
arguments. For example, bpf_cpumask_test_cpu(), which tests whether a
CPU is set in the cpumask. Any trusted struct cpumask * or struct
bpf_cpumask * may be passed to these kfuncs. The verifier allows
struct bpf_cpumask * even though the kfunc is defined with struct
cpumask * because the first element of a struct bpf_cpumask is a
cpumask_t, so it is safe to cast.
A follow-on patch will add selftests which validate these kfuncs, and
another will document them.
Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230125143816.721952-3-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-25 07:38:11 -07:00
|
|
|
{
|
|
|
|
return cpumask_first_zero(cpumask);
|
|
|
|
}
|
|
|
|
|
2023-06-09 20:50:49 -07:00
|
|
|
/**
|
|
|
|
* bpf_cpumask_first_and() - Return the index of the first nonzero bit from the
|
|
|
|
* AND of two cpumasks.
|
|
|
|
* @src1: The first cpumask.
|
|
|
|
* @src2: The second cpumask.
|
|
|
|
*
|
|
|
|
* Find the index of the first nonzero bit of the AND of two cpumasks.
|
|
|
|
* struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
|
|
|
|
*/
|
|
|
|
__bpf_kfunc u32 bpf_cpumask_first_and(const struct cpumask *src1,
|
|
|
|
const struct cpumask *src2)
|
|
|
|
{
|
|
|
|
return cpumask_first_and(src1, src2);
|
|
|
|
}
|
|
|
|
|
2023-01-25 07:38:14 -07:00
|
|
|
/**
|
|
|
|
* bpf_cpumask_set_cpu() - Set a bit for a CPU in a BPF cpumask.
|
|
|
|
* @cpu: The CPU to be set in the cpumask.
|
|
|
|
* @cpumask: The BPF cpumask in which a bit is being set.
|
|
|
|
*/
|
2023-02-01 10:30:15 -07:00
|
|
|
__bpf_kfunc void bpf_cpumask_set_cpu(u32 cpu, struct bpf_cpumask *cpumask)
|
bpf: Enable cpumasks to be queried and used as kptrs
Certain programs may wish to be able to query cpumasks. For example, if
a program that is tracing percpu operations wishes to track which tasks
end up running on which CPUs, it could be useful to associate that with
the tasks' cpumasks. Similarly, programs tracking NUMA allocations, CPU
scheduling domains, etc, could potentially benefit from being able to
see which CPUs a task could be migrated to.
This patch enables these types of use cases by introducing a series of
bpf_cpumask_* kfuncs. Amongst these kfuncs, there are two separate
"classes" of operations:
1. kfuncs which allow the caller to allocate and mutate their own
cpumask kptrs in the form of a struct bpf_cpumask * object. Such
kfuncs include e.g. bpf_cpumask_create() to allocate the cpumask, and
bpf_cpumask_or() to mutate it. "Regular" cpumasks such as p->cpus_ptr
may not be passed to these kfuncs, and the verifier will ensure this
is the case by comparing BTF IDs.
2. Read-only operations which operate on const struct cpumask *
arguments. For example, bpf_cpumask_test_cpu(), which tests whether a
CPU is set in the cpumask. Any trusted struct cpumask * or struct
bpf_cpumask * may be passed to these kfuncs. The verifier allows
struct bpf_cpumask * even though the kfunc is defined with struct
cpumask * because the first element of a struct bpf_cpumask is a
cpumask_t, so it is safe to cast.
A follow-on patch will add selftests which validate these kfuncs, and
another will document them.
Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230125143816.721952-3-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-25 07:38:11 -07:00
|
|
|
{
|
|
|
|
if (!cpu_valid(cpu))
|
|
|
|
return;
|
|
|
|
|
|
|
|
cpumask_set_cpu(cpu, (struct cpumask *)cpumask);
|
|
|
|
}
|
|
|
|
|
2023-01-25 07:38:14 -07:00
|
|
|
/**
|
|
|
|
* bpf_cpumask_clear_cpu() - Clear a bit for a CPU in a BPF cpumask.
|
|
|
|
* @cpu: The CPU to be cleared from the cpumask.
|
|
|
|
* @cpumask: The BPF cpumask in which a bit is being cleared.
|
|
|
|
*/
|
2023-02-01 10:30:15 -07:00
|
|
|
__bpf_kfunc void bpf_cpumask_clear_cpu(u32 cpu, struct bpf_cpumask *cpumask)
|
bpf: Enable cpumasks to be queried and used as kptrs
Certain programs may wish to be able to query cpumasks. For example, if
a program that is tracing percpu operations wishes to track which tasks
end up running on which CPUs, it could be useful to associate that with
the tasks' cpumasks. Similarly, programs tracking NUMA allocations, CPU
scheduling domains, etc, could potentially benefit from being able to
see which CPUs a task could be migrated to.
This patch enables these types of use cases by introducing a series of
bpf_cpumask_* kfuncs. Amongst these kfuncs, there are two separate
"classes" of operations:
1. kfuncs which allow the caller to allocate and mutate their own
cpumask kptrs in the form of a struct bpf_cpumask * object. Such
kfuncs include e.g. bpf_cpumask_create() to allocate the cpumask, and
bpf_cpumask_or() to mutate it. "Regular" cpumasks such as p->cpus_ptr
may not be passed to these kfuncs, and the verifier will ensure this
is the case by comparing BTF IDs.
2. Read-only operations which operate on const struct cpumask *
arguments. For example, bpf_cpumask_test_cpu(), which tests whether a
CPU is set in the cpumask. Any trusted struct cpumask * or struct
bpf_cpumask * may be passed to these kfuncs. The verifier allows
struct bpf_cpumask * even though the kfunc is defined with struct
cpumask * because the first element of a struct bpf_cpumask is a
cpumask_t, so it is safe to cast.
A follow-on patch will add selftests which validate these kfuncs, and
another will document them.
Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230125143816.721952-3-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-25 07:38:11 -07:00
|
|
|
{
|
|
|
|
if (!cpu_valid(cpu))
|
|
|
|
return;
|
|
|
|
|
|
|
|
cpumask_clear_cpu(cpu, (struct cpumask *)cpumask);
|
|
|
|
}
|
|
|
|
|
2023-01-25 07:38:14 -07:00
|
|
|
/**
|
|
|
|
* bpf_cpumask_test_cpu() - Test whether a CPU is set in a cpumask.
|
|
|
|
* @cpu: The CPU being queried for.
|
|
|
|
* @cpumask: The cpumask being queried for containing a CPU.
|
|
|
|
*
|
|
|
|
* Return:
|
|
|
|
* * true - @cpu is set in the cpumask
|
|
|
|
* * false - @cpu was not set in the cpumask, or @cpu is an invalid cpu.
|
|
|
|
*/
|
2023-02-01 10:30:15 -07:00
|
|
|
__bpf_kfunc bool bpf_cpumask_test_cpu(u32 cpu, const struct cpumask *cpumask)
|
bpf: Enable cpumasks to be queried and used as kptrs
Certain programs may wish to be able to query cpumasks. For example, if
a program that is tracing percpu operations wishes to track which tasks
end up running on which CPUs, it could be useful to associate that with
the tasks' cpumasks. Similarly, programs tracking NUMA allocations, CPU
scheduling domains, etc, could potentially benefit from being able to
see which CPUs a task could be migrated to.
This patch enables these types of use cases by introducing a series of
bpf_cpumask_* kfuncs. Amongst these kfuncs, there are two separate
"classes" of operations:
1. kfuncs which allow the caller to allocate and mutate their own
cpumask kptrs in the form of a struct bpf_cpumask * object. Such
kfuncs include e.g. bpf_cpumask_create() to allocate the cpumask, and
bpf_cpumask_or() to mutate it. "Regular" cpumasks such as p->cpus_ptr
may not be passed to these kfuncs, and the verifier will ensure this
is the case by comparing BTF IDs.
2. Read-only operations which operate on const struct cpumask *
arguments. For example, bpf_cpumask_test_cpu(), which tests whether a
CPU is set in the cpumask. Any trusted struct cpumask * or struct
bpf_cpumask * may be passed to these kfuncs. The verifier allows
struct bpf_cpumask * even though the kfunc is defined with struct
cpumask * because the first element of a struct bpf_cpumask is a
cpumask_t, so it is safe to cast.
A follow-on patch will add selftests which validate these kfuncs, and
another will document them.
Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230125143816.721952-3-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-25 07:38:11 -07:00
|
|
|
{
|
|
|
|
if (!cpu_valid(cpu))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return cpumask_test_cpu(cpu, (struct cpumask *)cpumask);
|
|
|
|
}
|
|
|
|
|
2023-01-25 07:38:14 -07:00
|
|
|
/**
|
|
|
|
* bpf_cpumask_test_and_set_cpu() - Atomically test and set a CPU in a BPF cpumask.
|
|
|
|
* @cpu: The CPU being set and queried for.
|
|
|
|
* @cpumask: The BPF cpumask being set and queried for containing a CPU.
|
|
|
|
*
|
|
|
|
* Return:
|
|
|
|
* * true - @cpu is set in the cpumask
|
|
|
|
* * false - @cpu was not set in the cpumask, or @cpu is invalid.
|
|
|
|
*/
|
2023-02-01 10:30:15 -07:00
|
|
|
__bpf_kfunc bool bpf_cpumask_test_and_set_cpu(u32 cpu, struct bpf_cpumask *cpumask)
|
bpf: Enable cpumasks to be queried and used as kptrs
Certain programs may wish to be able to query cpumasks. For example, if
a program that is tracing percpu operations wishes to track which tasks
end up running on which CPUs, it could be useful to associate that with
the tasks' cpumasks. Similarly, programs tracking NUMA allocations, CPU
scheduling domains, etc, could potentially benefit from being able to
see which CPUs a task could be migrated to.
This patch enables these types of use cases by introducing a series of
bpf_cpumask_* kfuncs. Amongst these kfuncs, there are two separate
"classes" of operations:
1. kfuncs which allow the caller to allocate and mutate their own
cpumask kptrs in the form of a struct bpf_cpumask * object. Such
kfuncs include e.g. bpf_cpumask_create() to allocate the cpumask, and
bpf_cpumask_or() to mutate it. "Regular" cpumasks such as p->cpus_ptr
may not be passed to these kfuncs, and the verifier will ensure this
is the case by comparing BTF IDs.
2. Read-only operations which operate on const struct cpumask *
arguments. For example, bpf_cpumask_test_cpu(), which tests whether a
CPU is set in the cpumask. Any trusted struct cpumask * or struct
bpf_cpumask * may be passed to these kfuncs. The verifier allows
struct bpf_cpumask * even though the kfunc is defined with struct
cpumask * because the first element of a struct bpf_cpumask is a
cpumask_t, so it is safe to cast.
A follow-on patch will add selftests which validate these kfuncs, and
another will document them.
Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230125143816.721952-3-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-25 07:38:11 -07:00
|
|
|
{
|
|
|
|
if (!cpu_valid(cpu))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return cpumask_test_and_set_cpu(cpu, (struct cpumask *)cpumask);
|
|
|
|
}
|
|
|
|
|
2023-01-25 07:38:14 -07:00
|
|
|
/**
|
|
|
|
* bpf_cpumask_test_and_clear_cpu() - Atomically test and clear a CPU in a BPF
|
|
|
|
* cpumask.
|
|
|
|
* @cpu: The CPU being cleared and queried for.
|
|
|
|
* @cpumask: The BPF cpumask being cleared and queried for containing a CPU.
|
|
|
|
*
|
|
|
|
* Return:
|
|
|
|
* * true - @cpu is set in the cpumask
|
|
|
|
* * false - @cpu was not set in the cpumask, or @cpu is invalid.
|
|
|
|
*/
|
2023-02-01 10:30:15 -07:00
|
|
|
__bpf_kfunc bool bpf_cpumask_test_and_clear_cpu(u32 cpu, struct bpf_cpumask *cpumask)
|
bpf: Enable cpumasks to be queried and used as kptrs
Certain programs may wish to be able to query cpumasks. For example, if
a program that is tracing percpu operations wishes to track which tasks
end up running on which CPUs, it could be useful to associate that with
the tasks' cpumasks. Similarly, programs tracking NUMA allocations, CPU
scheduling domains, etc, could potentially benefit from being able to
see which CPUs a task could be migrated to.
This patch enables these types of use cases by introducing a series of
bpf_cpumask_* kfuncs. Amongst these kfuncs, there are two separate
"classes" of operations:
1. kfuncs which allow the caller to allocate and mutate their own
cpumask kptrs in the form of a struct bpf_cpumask * object. Such
kfuncs include e.g. bpf_cpumask_create() to allocate the cpumask, and
bpf_cpumask_or() to mutate it. "Regular" cpumasks such as p->cpus_ptr
may not be passed to these kfuncs, and the verifier will ensure this
is the case by comparing BTF IDs.
2. Read-only operations which operate on const struct cpumask *
arguments. For example, bpf_cpumask_test_cpu(), which tests whether a
CPU is set in the cpumask. Any trusted struct cpumask * or struct
bpf_cpumask * may be passed to these kfuncs. The verifier allows
struct bpf_cpumask * even though the kfunc is defined with struct
cpumask * because the first element of a struct bpf_cpumask is a
cpumask_t, so it is safe to cast.
A follow-on patch will add selftests which validate these kfuncs, and
another will document them.
Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230125143816.721952-3-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-25 07:38:11 -07:00
|
|
|
{
|
|
|
|
if (!cpu_valid(cpu))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return cpumask_test_and_clear_cpu(cpu, (struct cpumask *)cpumask);
|
|
|
|
}
|
|
|
|
|
2023-01-25 07:38:14 -07:00
|
|
|
/**
|
|
|
|
* bpf_cpumask_setall() - Set all of the bits in a BPF cpumask.
|
|
|
|
* @cpumask: The BPF cpumask having all of its bits set.
|
|
|
|
*/
|
2023-02-01 10:30:15 -07:00
|
|
|
__bpf_kfunc void bpf_cpumask_setall(struct bpf_cpumask *cpumask)
|
bpf: Enable cpumasks to be queried and used as kptrs
Certain programs may wish to be able to query cpumasks. For example, if
a program that is tracing percpu operations wishes to track which tasks
end up running on which CPUs, it could be useful to associate that with
the tasks' cpumasks. Similarly, programs tracking NUMA allocations, CPU
scheduling domains, etc, could potentially benefit from being able to
see which CPUs a task could be migrated to.
This patch enables these types of use cases by introducing a series of
bpf_cpumask_* kfuncs. Amongst these kfuncs, there are two separate
"classes" of operations:
1. kfuncs which allow the caller to allocate and mutate their own
cpumask kptrs in the form of a struct bpf_cpumask * object. Such
kfuncs include e.g. bpf_cpumask_create() to allocate the cpumask, and
bpf_cpumask_or() to mutate it. "Regular" cpumasks such as p->cpus_ptr
may not be passed to these kfuncs, and the verifier will ensure this
is the case by comparing BTF IDs.
2. Read-only operations which operate on const struct cpumask *
arguments. For example, bpf_cpumask_test_cpu(), which tests whether a
CPU is set in the cpumask. Any trusted struct cpumask * or struct
bpf_cpumask * may be passed to these kfuncs. The verifier allows
struct bpf_cpumask * even though the kfunc is defined with struct
cpumask * because the first element of a struct bpf_cpumask is a
cpumask_t, so it is safe to cast.
A follow-on patch will add selftests which validate these kfuncs, and
another will document them.
Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230125143816.721952-3-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-25 07:38:11 -07:00
|
|
|
{
|
|
|
|
cpumask_setall((struct cpumask *)cpumask);
|
|
|
|
}
|
|
|
|
|
2023-01-25 07:38:14 -07:00
|
|
|
/**
|
|
|
|
* bpf_cpumask_clear() - Clear all of the bits in a BPF cpumask.
|
|
|
|
* @cpumask: The BPF cpumask being cleared.
|
|
|
|
*/
|
2023-02-01 10:30:15 -07:00
|
|
|
__bpf_kfunc void bpf_cpumask_clear(struct bpf_cpumask *cpumask)
|
bpf: Enable cpumasks to be queried and used as kptrs
Certain programs may wish to be able to query cpumasks. For example, if
a program that is tracing percpu operations wishes to track which tasks
end up running on which CPUs, it could be useful to associate that with
the tasks' cpumasks. Similarly, programs tracking NUMA allocations, CPU
scheduling domains, etc, could potentially benefit from being able to
see which CPUs a task could be migrated to.
This patch enables these types of use cases by introducing a series of
bpf_cpumask_* kfuncs. Amongst these kfuncs, there are two separate
"classes" of operations:
1. kfuncs which allow the caller to allocate and mutate their own
cpumask kptrs in the form of a struct bpf_cpumask * object. Such
kfuncs include e.g. bpf_cpumask_create() to allocate the cpumask, and
bpf_cpumask_or() to mutate it. "Regular" cpumasks such as p->cpus_ptr
may not be passed to these kfuncs, and the verifier will ensure this
is the case by comparing BTF IDs.
2. Read-only operations which operate on const struct cpumask *
arguments. For example, bpf_cpumask_test_cpu(), which tests whether a
CPU is set in the cpumask. Any trusted struct cpumask * or struct
bpf_cpumask * may be passed to these kfuncs. The verifier allows
struct bpf_cpumask * even though the kfunc is defined with struct
cpumask * because the first element of a struct bpf_cpumask is a
cpumask_t, so it is safe to cast.
A follow-on patch will add selftests which validate these kfuncs, and
another will document them.
Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230125143816.721952-3-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-25 07:38:11 -07:00
|
|
|
{
|
|
|
|
cpumask_clear((struct cpumask *)cpumask);
|
|
|
|
}
|
|
|
|
|
2023-01-25 07:38:14 -07:00
|
|
|
/**
|
|
|
|
* bpf_cpumask_and() - AND two cpumasks and store the result.
|
|
|
|
* @dst: The BPF cpumask where the result is being stored.
|
|
|
|
* @src1: The first input.
|
|
|
|
* @src2: The second input.
|
|
|
|
*
|
|
|
|
* Return:
|
|
|
|
* * true - @dst has at least one bit set following the operation
|
|
|
|
* * false - @dst is empty following the operation
|
|
|
|
*
|
|
|
|
* struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
|
|
|
|
*/
|
2023-02-01 10:30:15 -07:00
|
|
|
__bpf_kfunc bool bpf_cpumask_and(struct bpf_cpumask *dst,
|
|
|
|
const struct cpumask *src1,
|
|
|
|
const struct cpumask *src2)
|
bpf: Enable cpumasks to be queried and used as kptrs
Certain programs may wish to be able to query cpumasks. For example, if
a program that is tracing percpu operations wishes to track which tasks
end up running on which CPUs, it could be useful to associate that with
the tasks' cpumasks. Similarly, programs tracking NUMA allocations, CPU
scheduling domains, etc, could potentially benefit from being able to
see which CPUs a task could be migrated to.
This patch enables these types of use cases by introducing a series of
bpf_cpumask_* kfuncs. Amongst these kfuncs, there are two separate
"classes" of operations:
1. kfuncs which allow the caller to allocate and mutate their own
cpumask kptrs in the form of a struct bpf_cpumask * object. Such
kfuncs include e.g. bpf_cpumask_create() to allocate the cpumask, and
bpf_cpumask_or() to mutate it. "Regular" cpumasks such as p->cpus_ptr
may not be passed to these kfuncs, and the verifier will ensure this
is the case by comparing BTF IDs.
2. Read-only operations which operate on const struct cpumask *
arguments. For example, bpf_cpumask_test_cpu(), which tests whether a
CPU is set in the cpumask. Any trusted struct cpumask * or struct
bpf_cpumask * may be passed to these kfuncs. The verifier allows
struct bpf_cpumask * even though the kfunc is defined with struct
cpumask * because the first element of a struct bpf_cpumask is a
cpumask_t, so it is safe to cast.
A follow-on patch will add selftests which validate these kfuncs, and
another will document them.
Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230125143816.721952-3-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-25 07:38:11 -07:00
|
|
|
{
|
|
|
|
return cpumask_and((struct cpumask *)dst, src1, src2);
|
|
|
|
}
|
|
|
|
|
2023-01-25 07:38:14 -07:00
|
|
|
/**
|
|
|
|
* bpf_cpumask_or() - OR two cpumasks and store the result.
|
|
|
|
* @dst: The BPF cpumask where the result is being stored.
|
|
|
|
* @src1: The first input.
|
|
|
|
* @src2: The second input.
|
|
|
|
*
|
|
|
|
* struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
|
|
|
|
*/
|
2023-02-01 10:30:15 -07:00
|
|
|
__bpf_kfunc void bpf_cpumask_or(struct bpf_cpumask *dst,
|
|
|
|
const struct cpumask *src1,
|
|
|
|
const struct cpumask *src2)
|
bpf: Enable cpumasks to be queried and used as kptrs
Certain programs may wish to be able to query cpumasks. For example, if
a program that is tracing percpu operations wishes to track which tasks
end up running on which CPUs, it could be useful to associate that with
the tasks' cpumasks. Similarly, programs tracking NUMA allocations, CPU
scheduling domains, etc, could potentially benefit from being able to
see which CPUs a task could be migrated to.
This patch enables these types of use cases by introducing a series of
bpf_cpumask_* kfuncs. Amongst these kfuncs, there are two separate
"classes" of operations:
1. kfuncs which allow the caller to allocate and mutate their own
cpumask kptrs in the form of a struct bpf_cpumask * object. Such
kfuncs include e.g. bpf_cpumask_create() to allocate the cpumask, and
bpf_cpumask_or() to mutate it. "Regular" cpumasks such as p->cpus_ptr
may not be passed to these kfuncs, and the verifier will ensure this
is the case by comparing BTF IDs.
2. Read-only operations which operate on const struct cpumask *
arguments. For example, bpf_cpumask_test_cpu(), which tests whether a
CPU is set in the cpumask. Any trusted struct cpumask * or struct
bpf_cpumask * may be passed to these kfuncs. The verifier allows
struct bpf_cpumask * even though the kfunc is defined with struct
cpumask * because the first element of a struct bpf_cpumask is a
cpumask_t, so it is safe to cast.
A follow-on patch will add selftests which validate these kfuncs, and
another will document them.
Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230125143816.721952-3-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-25 07:38:11 -07:00
|
|
|
{
|
|
|
|
cpumask_or((struct cpumask *)dst, src1, src2);
|
|
|
|
}
|
|
|
|
|
2023-01-25 07:38:14 -07:00
|
|
|
/**
|
|
|
|
* bpf_cpumask_xor() - XOR two cpumasks and store the result.
|
|
|
|
* @dst: The BPF cpumask where the result is being stored.
|
|
|
|
* @src1: The first input.
|
|
|
|
* @src2: The second input.
|
|
|
|
*
|
|
|
|
* struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
|
|
|
|
*/
|
2023-02-01 10:30:15 -07:00
|
|
|
__bpf_kfunc void bpf_cpumask_xor(struct bpf_cpumask *dst,
|
|
|
|
const struct cpumask *src1,
|
|
|
|
const struct cpumask *src2)
|
bpf: Enable cpumasks to be queried and used as kptrs
Certain programs may wish to be able to query cpumasks. For example, if
a program that is tracing percpu operations wishes to track which tasks
end up running on which CPUs, it could be useful to associate that with
the tasks' cpumasks. Similarly, programs tracking NUMA allocations, CPU
scheduling domains, etc, could potentially benefit from being able to
see which CPUs a task could be migrated to.
This patch enables these types of use cases by introducing a series of
bpf_cpumask_* kfuncs. Amongst these kfuncs, there are two separate
"classes" of operations:
1. kfuncs which allow the caller to allocate and mutate their own
cpumask kptrs in the form of a struct bpf_cpumask * object. Such
kfuncs include e.g. bpf_cpumask_create() to allocate the cpumask, and
bpf_cpumask_or() to mutate it. "Regular" cpumasks such as p->cpus_ptr
may not be passed to these kfuncs, and the verifier will ensure this
is the case by comparing BTF IDs.
2. Read-only operations which operate on const struct cpumask *
arguments. For example, bpf_cpumask_test_cpu(), which tests whether a
CPU is set in the cpumask. Any trusted struct cpumask * or struct
bpf_cpumask * may be passed to these kfuncs. The verifier allows
struct bpf_cpumask * even though the kfunc is defined with struct
cpumask * because the first element of a struct bpf_cpumask is a
cpumask_t, so it is safe to cast.
A follow-on patch will add selftests which validate these kfuncs, and
another will document them.
Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230125143816.721952-3-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-25 07:38:11 -07:00
|
|
|
{
|
|
|
|
cpumask_xor((struct cpumask *)dst, src1, src2);
|
|
|
|
}
|
|
|
|
|
2023-01-25 07:38:14 -07:00
|
|
|
/**
|
|
|
|
* bpf_cpumask_equal() - Check two cpumasks for equality.
|
|
|
|
* @src1: The first input.
|
|
|
|
* @src2: The second input.
|
|
|
|
*
|
|
|
|
* Return:
|
|
|
|
* * true - @src1 and @src2 have the same bits set.
|
|
|
|
* * false - @src1 and @src2 differ in at least one bit.
|
|
|
|
*
|
|
|
|
* struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
|
|
|
|
*/
|
2023-02-01 10:30:15 -07:00
|
|
|
__bpf_kfunc bool bpf_cpumask_equal(const struct cpumask *src1, const struct cpumask *src2)
|
bpf: Enable cpumasks to be queried and used as kptrs
Certain programs may wish to be able to query cpumasks. For example, if
a program that is tracing percpu operations wishes to track which tasks
end up running on which CPUs, it could be useful to associate that with
the tasks' cpumasks. Similarly, programs tracking NUMA allocations, CPU
scheduling domains, etc, could potentially benefit from being able to
see which CPUs a task could be migrated to.
This patch enables these types of use cases by introducing a series of
bpf_cpumask_* kfuncs. Amongst these kfuncs, there are two separate
"classes" of operations:
1. kfuncs which allow the caller to allocate and mutate their own
cpumask kptrs in the form of a struct bpf_cpumask * object. Such
kfuncs include e.g. bpf_cpumask_create() to allocate the cpumask, and
bpf_cpumask_or() to mutate it. "Regular" cpumasks such as p->cpus_ptr
may not be passed to these kfuncs, and the verifier will ensure this
is the case by comparing BTF IDs.
2. Read-only operations which operate on const struct cpumask *
arguments. For example, bpf_cpumask_test_cpu(), which tests whether a
CPU is set in the cpumask. Any trusted struct cpumask * or struct
bpf_cpumask * may be passed to these kfuncs. The verifier allows
struct bpf_cpumask * even though the kfunc is defined with struct
cpumask * because the first element of a struct bpf_cpumask is a
cpumask_t, so it is safe to cast.
A follow-on patch will add selftests which validate these kfuncs, and
another will document them.
Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230125143816.721952-3-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-25 07:38:11 -07:00
|
|
|
{
|
|
|
|
return cpumask_equal(src1, src2);
|
|
|
|
}
|
|
|
|
|
2023-01-25 07:38:14 -07:00
|
|
|
/**
|
|
|
|
* bpf_cpumask_intersects() - Check two cpumasks for overlap.
|
|
|
|
* @src1: The first input.
|
|
|
|
* @src2: The second input.
|
|
|
|
*
|
|
|
|
* Return:
|
|
|
|
* * true - @src1 and @src2 have at least one of the same bits set.
|
|
|
|
* * false - @src1 and @src2 don't have any of the same bits set.
|
|
|
|
*
|
|
|
|
* struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
|
|
|
|
*/
|
2023-02-01 10:30:15 -07:00
|
|
|
__bpf_kfunc bool bpf_cpumask_intersects(const struct cpumask *src1, const struct cpumask *src2)
|
bpf: Enable cpumasks to be queried and used as kptrs
Certain programs may wish to be able to query cpumasks. For example, if
a program that is tracing percpu operations wishes to track which tasks
end up running on which CPUs, it could be useful to associate that with
the tasks' cpumasks. Similarly, programs tracking NUMA allocations, CPU
scheduling domains, etc, could potentially benefit from being able to
see which CPUs a task could be migrated to.
This patch enables these types of use cases by introducing a series of
bpf_cpumask_* kfuncs. Amongst these kfuncs, there are two separate
"classes" of operations:
1. kfuncs which allow the caller to allocate and mutate their own
cpumask kptrs in the form of a struct bpf_cpumask * object. Such
kfuncs include e.g. bpf_cpumask_create() to allocate the cpumask, and
bpf_cpumask_or() to mutate it. "Regular" cpumasks such as p->cpus_ptr
may not be passed to these kfuncs, and the verifier will ensure this
is the case by comparing BTF IDs.
2. Read-only operations which operate on const struct cpumask *
arguments. For example, bpf_cpumask_test_cpu(), which tests whether a
CPU is set in the cpumask. Any trusted struct cpumask * or struct
bpf_cpumask * may be passed to these kfuncs. The verifier allows
struct bpf_cpumask * even though the kfunc is defined with struct
cpumask * because the first element of a struct bpf_cpumask is a
cpumask_t, so it is safe to cast.
A follow-on patch will add selftests which validate these kfuncs, and
another will document them.
Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230125143816.721952-3-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-25 07:38:11 -07:00
|
|
|
{
|
|
|
|
return cpumask_intersects(src1, src2);
|
|
|
|
}
|
|
|
|
|
2023-01-25 07:38:14 -07:00
|
|
|
/**
|
|
|
|
* bpf_cpumask_subset() - Check if a cpumask is a subset of another.
|
|
|
|
* @src1: The first cpumask being checked as a subset.
|
|
|
|
* @src2: The second cpumask being checked as a superset.
|
|
|
|
*
|
|
|
|
* Return:
|
|
|
|
* * true - All of the bits of @src1 are set in @src2.
|
|
|
|
* * false - At least one bit in @src1 is not set in @src2.
|
|
|
|
*
|
|
|
|
* struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
|
|
|
|
*/
|
2023-02-01 10:30:15 -07:00
|
|
|
__bpf_kfunc bool bpf_cpumask_subset(const struct cpumask *src1, const struct cpumask *src2)
|
bpf: Enable cpumasks to be queried and used as kptrs
Certain programs may wish to be able to query cpumasks. For example, if
a program that is tracing percpu operations wishes to track which tasks
end up running on which CPUs, it could be useful to associate that with
the tasks' cpumasks. Similarly, programs tracking NUMA allocations, CPU
scheduling domains, etc, could potentially benefit from being able to
see which CPUs a task could be migrated to.
This patch enables these types of use cases by introducing a series of
bpf_cpumask_* kfuncs. Amongst these kfuncs, there are two separate
"classes" of operations:
1. kfuncs which allow the caller to allocate and mutate their own
cpumask kptrs in the form of a struct bpf_cpumask * object. Such
kfuncs include e.g. bpf_cpumask_create() to allocate the cpumask, and
bpf_cpumask_or() to mutate it. "Regular" cpumasks such as p->cpus_ptr
may not be passed to these kfuncs, and the verifier will ensure this
is the case by comparing BTF IDs.
2. Read-only operations which operate on const struct cpumask *
arguments. For example, bpf_cpumask_test_cpu(), which tests whether a
CPU is set in the cpumask. Any trusted struct cpumask * or struct
bpf_cpumask * may be passed to these kfuncs. The verifier allows
struct bpf_cpumask * even though the kfunc is defined with struct
cpumask * because the first element of a struct bpf_cpumask is a
cpumask_t, so it is safe to cast.
A follow-on patch will add selftests which validate these kfuncs, and
another will document them.
Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230125143816.721952-3-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-25 07:38:11 -07:00
|
|
|
{
|
|
|
|
return cpumask_subset(src1, src2);
|
|
|
|
}
|
|
|
|
|
2023-01-25 07:38:14 -07:00
|
|
|
/**
|
|
|
|
* bpf_cpumask_empty() - Check if a cpumask is empty.
|
|
|
|
* @cpumask: The cpumask being checked.
|
|
|
|
*
|
|
|
|
* Return:
|
|
|
|
* * true - None of the bits in @cpumask are set.
|
|
|
|
* * false - At least one bit in @cpumask is set.
|
|
|
|
*
|
|
|
|
* A struct bpf_cpumask pointer may be safely passed to @cpumask.
|
|
|
|
*/
|
2023-02-01 10:30:15 -07:00
|
|
|
__bpf_kfunc bool bpf_cpumask_empty(const struct cpumask *cpumask)
|
bpf: Enable cpumasks to be queried and used as kptrs
Certain programs may wish to be able to query cpumasks. For example, if
a program that is tracing percpu operations wishes to track which tasks
end up running on which CPUs, it could be useful to associate that with
the tasks' cpumasks. Similarly, programs tracking NUMA allocations, CPU
scheduling domains, etc, could potentially benefit from being able to
see which CPUs a task could be migrated to.
This patch enables these types of use cases by introducing a series of
bpf_cpumask_* kfuncs. Amongst these kfuncs, there are two separate
"classes" of operations:
1. kfuncs which allow the caller to allocate and mutate their own
cpumask kptrs in the form of a struct bpf_cpumask * object. Such
kfuncs include e.g. bpf_cpumask_create() to allocate the cpumask, and
bpf_cpumask_or() to mutate it. "Regular" cpumasks such as p->cpus_ptr
may not be passed to these kfuncs, and the verifier will ensure this
is the case by comparing BTF IDs.
2. Read-only operations which operate on const struct cpumask *
arguments. For example, bpf_cpumask_test_cpu(), which tests whether a
CPU is set in the cpumask. Any trusted struct cpumask * or struct
bpf_cpumask * may be passed to these kfuncs. The verifier allows
struct bpf_cpumask * even though the kfunc is defined with struct
cpumask * because the first element of a struct bpf_cpumask is a
cpumask_t, so it is safe to cast.
A follow-on patch will add selftests which validate these kfuncs, and
another will document them.
Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230125143816.721952-3-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-25 07:38:11 -07:00
|
|
|
{
|
|
|
|
return cpumask_empty(cpumask);
|
|
|
|
}
|
|
|
|
|
2023-01-25 07:38:14 -07:00
|
|
|
/**
|
|
|
|
* bpf_cpumask_full() - Check if a cpumask has all bits set.
|
|
|
|
* @cpumask: The cpumask being checked.
|
|
|
|
*
|
|
|
|
* Return:
|
|
|
|
* * true - All of the bits in @cpumask are set.
|
|
|
|
* * false - At least one bit in @cpumask is cleared.
|
|
|
|
*
|
|
|
|
* A struct bpf_cpumask pointer may be safely passed to @cpumask.
|
|
|
|
*/
|
2023-02-01 10:30:15 -07:00
|
|
|
__bpf_kfunc bool bpf_cpumask_full(const struct cpumask *cpumask)
|
bpf: Enable cpumasks to be queried and used as kptrs
Certain programs may wish to be able to query cpumasks. For example, if
a program that is tracing percpu operations wishes to track which tasks
end up running on which CPUs, it could be useful to associate that with
the tasks' cpumasks. Similarly, programs tracking NUMA allocations, CPU
scheduling domains, etc, could potentially benefit from being able to
see which CPUs a task could be migrated to.
This patch enables these types of use cases by introducing a series of
bpf_cpumask_* kfuncs. Amongst these kfuncs, there are two separate
"classes" of operations:
1. kfuncs which allow the caller to allocate and mutate their own
cpumask kptrs in the form of a struct bpf_cpumask * object. Such
kfuncs include e.g. bpf_cpumask_create() to allocate the cpumask, and
bpf_cpumask_or() to mutate it. "Regular" cpumasks such as p->cpus_ptr
may not be passed to these kfuncs, and the verifier will ensure this
is the case by comparing BTF IDs.
2. Read-only operations which operate on const struct cpumask *
arguments. For example, bpf_cpumask_test_cpu(), which tests whether a
CPU is set in the cpumask. Any trusted struct cpumask * or struct
bpf_cpumask * may be passed to these kfuncs. The verifier allows
struct bpf_cpumask * even though the kfunc is defined with struct
cpumask * because the first element of a struct bpf_cpumask is a
cpumask_t, so it is safe to cast.
A follow-on patch will add selftests which validate these kfuncs, and
another will document them.
Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230125143816.721952-3-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-25 07:38:11 -07:00
|
|
|
{
|
|
|
|
return cpumask_full(cpumask);
|
|
|
|
}
|
|
|
|
|
2023-01-25 07:38:14 -07:00
|
|
|
/**
|
|
|
|
* bpf_cpumask_copy() - Copy the contents of a cpumask into a BPF cpumask.
|
|
|
|
* @dst: The BPF cpumask being copied into.
|
|
|
|
* @src: The cpumask being copied.
|
|
|
|
*
|
|
|
|
* A struct bpf_cpumask pointer may be safely passed to @src.
|
|
|
|
*/
|
2023-02-01 10:30:15 -07:00
|
|
|
__bpf_kfunc void bpf_cpumask_copy(struct bpf_cpumask *dst, const struct cpumask *src)
|
bpf: Enable cpumasks to be queried and used as kptrs
Certain programs may wish to be able to query cpumasks. For example, if
a program that is tracing percpu operations wishes to track which tasks
end up running on which CPUs, it could be useful to associate that with
the tasks' cpumasks. Similarly, programs tracking NUMA allocations, CPU
scheduling domains, etc, could potentially benefit from being able to
see which CPUs a task could be migrated to.
This patch enables these types of use cases by introducing a series of
bpf_cpumask_* kfuncs. Amongst these kfuncs, there are two separate
"classes" of operations:
1. kfuncs which allow the caller to allocate and mutate their own
cpumask kptrs in the form of a struct bpf_cpumask * object. Such
kfuncs include e.g. bpf_cpumask_create() to allocate the cpumask, and
bpf_cpumask_or() to mutate it. "Regular" cpumasks such as p->cpus_ptr
may not be passed to these kfuncs, and the verifier will ensure this
is the case by comparing BTF IDs.
2. Read-only operations which operate on const struct cpumask *
arguments. For example, bpf_cpumask_test_cpu(), which tests whether a
CPU is set in the cpumask. Any trusted struct cpumask * or struct
bpf_cpumask * may be passed to these kfuncs. The verifier allows
struct bpf_cpumask * even though the kfunc is defined with struct
cpumask * because the first element of a struct bpf_cpumask is a
cpumask_t, so it is safe to cast.
A follow-on patch will add selftests which validate these kfuncs, and
another will document them.
Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230125143816.721952-3-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-25 07:38:11 -07:00
|
|
|
{
|
|
|
|
cpumask_copy((struct cpumask *)dst, src);
|
|
|
|
}
|
|
|
|
|
2023-01-25 07:38:14 -07:00
|
|
|
/**
|
2023-06-09 20:50:51 -07:00
|
|
|
* bpf_cpumask_any_distribute() - Return a random set CPU from a cpumask.
|
2023-01-25 07:38:14 -07:00
|
|
|
* @cpumask: The cpumask being queried.
|
|
|
|
*
|
|
|
|
* Return:
|
|
|
|
* * A random set bit within [0, num_cpus) if at least one bit is set.
|
|
|
|
* * >= num_cpus if no bit is set.
|
|
|
|
*
|
|
|
|
* A struct bpf_cpumask pointer may be safely passed to @src.
|
|
|
|
*/
|
2023-06-09 20:50:51 -07:00
|
|
|
__bpf_kfunc u32 bpf_cpumask_any_distribute(const struct cpumask *cpumask)
|
bpf: Enable cpumasks to be queried and used as kptrs
Certain programs may wish to be able to query cpumasks. For example, if
a program that is tracing percpu operations wishes to track which tasks
end up running on which CPUs, it could be useful to associate that with
the tasks' cpumasks. Similarly, programs tracking NUMA allocations, CPU
scheduling domains, etc, could potentially benefit from being able to
see which CPUs a task could be migrated to.
This patch enables these types of use cases by introducing a series of
bpf_cpumask_* kfuncs. Amongst these kfuncs, there are two separate
"classes" of operations:
1. kfuncs which allow the caller to allocate and mutate their own
cpumask kptrs in the form of a struct bpf_cpumask * object. Such
kfuncs include e.g. bpf_cpumask_create() to allocate the cpumask, and
bpf_cpumask_or() to mutate it. "Regular" cpumasks such as p->cpus_ptr
may not be passed to these kfuncs, and the verifier will ensure this
is the case by comparing BTF IDs.
2. Read-only operations which operate on const struct cpumask *
arguments. For example, bpf_cpumask_test_cpu(), which tests whether a
CPU is set in the cpumask. Any trusted struct cpumask * or struct
bpf_cpumask * may be passed to these kfuncs. The verifier allows
struct bpf_cpumask * even though the kfunc is defined with struct
cpumask * because the first element of a struct bpf_cpumask is a
cpumask_t, so it is safe to cast.
A follow-on patch will add selftests which validate these kfuncs, and
another will document them.
Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230125143816.721952-3-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-25 07:38:11 -07:00
|
|
|
{
|
2023-06-09 20:50:51 -07:00
|
|
|
return cpumask_any_distribute(cpumask);
|
bpf: Enable cpumasks to be queried and used as kptrs
Certain programs may wish to be able to query cpumasks. For example, if
a program that is tracing percpu operations wishes to track which tasks
end up running on which CPUs, it could be useful to associate that with
the tasks' cpumasks. Similarly, programs tracking NUMA allocations, CPU
scheduling domains, etc, could potentially benefit from being able to
see which CPUs a task could be migrated to.
This patch enables these types of use cases by introducing a series of
bpf_cpumask_* kfuncs. Amongst these kfuncs, there are two separate
"classes" of operations:
1. kfuncs which allow the caller to allocate and mutate their own
cpumask kptrs in the form of a struct bpf_cpumask * object. Such
kfuncs include e.g. bpf_cpumask_create() to allocate the cpumask, and
bpf_cpumask_or() to mutate it. "Regular" cpumasks such as p->cpus_ptr
may not be passed to these kfuncs, and the verifier will ensure this
is the case by comparing BTF IDs.
2. Read-only operations which operate on const struct cpumask *
arguments. For example, bpf_cpumask_test_cpu(), which tests whether a
CPU is set in the cpumask. Any trusted struct cpumask * or struct
bpf_cpumask * may be passed to these kfuncs. The verifier allows
struct bpf_cpumask * even though the kfunc is defined with struct
cpumask * because the first element of a struct bpf_cpumask is a
cpumask_t, so it is safe to cast.
A follow-on patch will add selftests which validate these kfuncs, and
another will document them.
Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230125143816.721952-3-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-25 07:38:11 -07:00
|
|
|
}
|
|
|
|
|
2023-01-25 07:38:14 -07:00
|
|
|
/**
|
2023-06-09 20:50:51 -07:00
|
|
|
* bpf_cpumask_any_and_distribute() - Return a random set CPU from the AND of
|
|
|
|
* two cpumasks.
|
2023-01-25 07:38:14 -07:00
|
|
|
* @src1: The first cpumask.
|
|
|
|
* @src2: The second cpumask.
|
|
|
|
*
|
|
|
|
* Return:
|
2023-06-09 20:50:51 -07:00
|
|
|
* * A random set bit within [0, num_cpus) from the AND of two cpumasks, if at
|
|
|
|
* least one bit is set.
|
2023-01-25 07:38:14 -07:00
|
|
|
* * >= num_cpus if no bit is set.
|
|
|
|
*
|
|
|
|
* struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
|
|
|
|
*/
|
2023-06-09 20:50:51 -07:00
|
|
|
__bpf_kfunc u32 bpf_cpumask_any_and_distribute(const struct cpumask *src1,
|
|
|
|
const struct cpumask *src2)
|
bpf: Enable cpumasks to be queried and used as kptrs
Certain programs may wish to be able to query cpumasks. For example, if
a program that is tracing percpu operations wishes to track which tasks
end up running on which CPUs, it could be useful to associate that with
the tasks' cpumasks. Similarly, programs tracking NUMA allocations, CPU
scheduling domains, etc, could potentially benefit from being able to
see which CPUs a task could be migrated to.
This patch enables these types of use cases by introducing a series of
bpf_cpumask_* kfuncs. Amongst these kfuncs, there are two separate
"classes" of operations:
1. kfuncs which allow the caller to allocate and mutate their own
cpumask kptrs in the form of a struct bpf_cpumask * object. Such
kfuncs include e.g. bpf_cpumask_create() to allocate the cpumask, and
bpf_cpumask_or() to mutate it. "Regular" cpumasks such as p->cpus_ptr
may not be passed to these kfuncs, and the verifier will ensure this
is the case by comparing BTF IDs.
2. Read-only operations which operate on const struct cpumask *
arguments. For example, bpf_cpumask_test_cpu(), which tests whether a
CPU is set in the cpumask. Any trusted struct cpumask * or struct
bpf_cpumask * may be passed to these kfuncs. The verifier allows
struct bpf_cpumask * even though the kfunc is defined with struct
cpumask * because the first element of a struct bpf_cpumask is a
cpumask_t, so it is safe to cast.
A follow-on patch will add selftests which validate these kfuncs, and
another will document them.
Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230125143816.721952-3-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-25 07:38:11 -07:00
|
|
|
{
|
2023-06-09 20:50:51 -07:00
|
|
|
return cpumask_any_and_distribute(src1, src2);
|
bpf: Enable cpumasks to be queried and used as kptrs
Certain programs may wish to be able to query cpumasks. For example, if
a program that is tracing percpu operations wishes to track which tasks
end up running on which CPUs, it could be useful to associate that with
the tasks' cpumasks. Similarly, programs tracking NUMA allocations, CPU
scheduling domains, etc, could potentially benefit from being able to
see which CPUs a task could be migrated to.
This patch enables these types of use cases by introducing a series of
bpf_cpumask_* kfuncs. Amongst these kfuncs, there are two separate
"classes" of operations:
1. kfuncs which allow the caller to allocate and mutate their own
cpumask kptrs in the form of a struct bpf_cpumask * object. Such
kfuncs include e.g. bpf_cpumask_create() to allocate the cpumask, and
bpf_cpumask_or() to mutate it. "Regular" cpumasks such as p->cpus_ptr
may not be passed to these kfuncs, and the verifier will ensure this
is the case by comparing BTF IDs.
2. Read-only operations which operate on const struct cpumask *
arguments. For example, bpf_cpumask_test_cpu(), which tests whether a
CPU is set in the cpumask. Any trusted struct cpumask * or struct
bpf_cpumask * may be passed to these kfuncs. The verifier allows
struct bpf_cpumask * even though the kfunc is defined with struct
cpumask * because the first element of a struct bpf_cpumask is a
cpumask_t, so it is safe to cast.
A follow-on patch will add selftests which validate these kfuncs, and
another will document them.
Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230125143816.721952-3-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-25 07:38:11 -07:00
|
|
|
}
|
|
|
|
|
2023-12-07 14:08:42 -07:00
|
|
|
/**
|
|
|
|
* bpf_cpumask_weight() - Return the number of bits in @cpumask.
|
|
|
|
* @cpumask: The cpumask being queried.
|
|
|
|
*
|
|
|
|
* Count the number of set bits in the given cpumask.
|
|
|
|
*/
|
|
|
|
__bpf_kfunc u32 bpf_cpumask_weight(const struct cpumask *cpumask)
|
|
|
|
{
|
|
|
|
return cpumask_weight(cpumask);
|
|
|
|
}
|
|
|
|
|
2023-10-31 14:56:24 -07:00
|
|
|
__bpf_kfunc_end_defs();
|
bpf: Enable cpumasks to be queried and used as kptrs
Certain programs may wish to be able to query cpumasks. For example, if
a program that is tracing percpu operations wishes to track which tasks
end up running on which CPUs, it could be useful to associate that with
the tasks' cpumasks. Similarly, programs tracking NUMA allocations, CPU
scheduling domains, etc, could potentially benefit from being able to
see which CPUs a task could be migrated to.
This patch enables these types of use cases by introducing a series of
bpf_cpumask_* kfuncs. Amongst these kfuncs, there are two separate
"classes" of operations:
1. kfuncs which allow the caller to allocate and mutate their own
cpumask kptrs in the form of a struct bpf_cpumask * object. Such
kfuncs include e.g. bpf_cpumask_create() to allocate the cpumask, and
bpf_cpumask_or() to mutate it. "Regular" cpumasks such as p->cpus_ptr
may not be passed to these kfuncs, and the verifier will ensure this
is the case by comparing BTF IDs.
2. Read-only operations which operate on const struct cpumask *
arguments. For example, bpf_cpumask_test_cpu(), which tests whether a
CPU is set in the cpumask. Any trusted struct cpumask * or struct
bpf_cpumask * may be passed to these kfuncs. The verifier allows
struct bpf_cpumask * even though the kfunc is defined with struct
cpumask * because the first element of a struct bpf_cpumask is a
cpumask_t, so it is safe to cast.
A follow-on patch will add selftests which validate these kfuncs, and
another will document them.
Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230125143816.721952-3-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-25 07:38:11 -07:00
|
|
|
|
2024-01-28 18:24:08 -07:00
|
|
|
BTF_KFUNCS_START(cpumask_kfunc_btf_ids)
|
bpf: Enable cpumasks to be queried and used as kptrs
Certain programs may wish to be able to query cpumasks. For example, if
a program that is tracing percpu operations wishes to track which tasks
end up running on which CPUs, it could be useful to associate that with
the tasks' cpumasks. Similarly, programs tracking NUMA allocations, CPU
scheduling domains, etc, could potentially benefit from being able to
see which CPUs a task could be migrated to.
This patch enables these types of use cases by introducing a series of
bpf_cpumask_* kfuncs. Amongst these kfuncs, there are two separate
"classes" of operations:
1. kfuncs which allow the caller to allocate and mutate their own
cpumask kptrs in the form of a struct bpf_cpumask * object. Such
kfuncs include e.g. bpf_cpumask_create() to allocate the cpumask, and
bpf_cpumask_or() to mutate it. "Regular" cpumasks such as p->cpus_ptr
may not be passed to these kfuncs, and the verifier will ensure this
is the case by comparing BTF IDs.
2. Read-only operations which operate on const struct cpumask *
arguments. For example, bpf_cpumask_test_cpu(), which tests whether a
CPU is set in the cpumask. Any trusted struct cpumask * or struct
bpf_cpumask * may be passed to these kfuncs. The verifier allows
struct bpf_cpumask * even though the kfunc is defined with struct
cpumask * because the first element of a struct bpf_cpumask is a
cpumask_t, so it is safe to cast.
A follow-on patch will add selftests which validate these kfuncs, and
another will document them.
Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230125143816.721952-3-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-25 07:38:11 -07:00
|
|
|
BTF_ID_FLAGS(func, bpf_cpumask_create, KF_ACQUIRE | KF_RET_NULL)
|
bpf: Treat KF_RELEASE kfuncs as KF_TRUSTED_ARGS
KF_RELEASE kfuncs are not currently treated as having KF_TRUSTED_ARGS,
even though they have a superset of the requirements of KF_TRUSTED_ARGS.
Like KF_TRUSTED_ARGS, KF_RELEASE kfuncs require a 0-offset argument, and
don't allow NULL-able arguments. Unlike KF_TRUSTED_ARGS which require
_either_ an argument with ref_obj_id > 0, _or_ (ref->type &
BPF_REG_TRUSTED_MODIFIERS) (and no unsafe modifiers allowed), KF_RELEASE
only allows for ref_obj_id > 0. Because KF_RELEASE today doesn't
automatically imply KF_TRUSTED_ARGS, some of these requirements are
enforced in different ways that can make the behavior of the verifier
feel unpredictable. For example, a KF_RELEASE kfunc with a NULL-able
argument will currently fail in the verifier with a message like, "arg#0
is ptr_or_null_ expected ptr_ or socket" rather than "Possibly NULL
pointer passed to trusted arg0". Our intention is the same, but the
semantics are different due to implemenetation details that kfunc authors
and BPF program writers should not need to care about.
Let's make the behavior of the verifier more consistent and intuitive by
having KF_RELEASE kfuncs imply the presence of KF_TRUSTED_ARGS. Our
eventual goal is to have all kfuncs assume KF_TRUSTED_ARGS by default
anyways, so this takes us a step in that direction.
Note that it does not make sense to assume KF_TRUSTED_ARGS for all
KF_ACQUIRE kfuncs. KF_ACQUIRE kfuncs can have looser semantics than
KF_RELEASE, with e.g. KF_RCU | KF_RET_NULL. We may want to have
KF_ACQUIRE imply KF_TRUSTED_ARGS _unless_ KF_RCU is specified, but that
can be left to another patch set, and there are no such subtleties to
address for KF_RELEASE.
Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230325213144.486885-4-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-03-25 14:31:46 -07:00
|
|
|
BTF_ID_FLAGS(func, bpf_cpumask_release, KF_RELEASE)
|
bpf: Enable cpumasks to be queried and used as kptrs
Certain programs may wish to be able to query cpumasks. For example, if
a program that is tracing percpu operations wishes to track which tasks
end up running on which CPUs, it could be useful to associate that with
the tasks' cpumasks. Similarly, programs tracking NUMA allocations, CPU
scheduling domains, etc, could potentially benefit from being able to
see which CPUs a task could be migrated to.
This patch enables these types of use cases by introducing a series of
bpf_cpumask_* kfuncs. Amongst these kfuncs, there are two separate
"classes" of operations:
1. kfuncs which allow the caller to allocate and mutate their own
cpumask kptrs in the form of a struct bpf_cpumask * object. Such
kfuncs include e.g. bpf_cpumask_create() to allocate the cpumask, and
bpf_cpumask_or() to mutate it. "Regular" cpumasks such as p->cpus_ptr
may not be passed to these kfuncs, and the verifier will ensure this
is the case by comparing BTF IDs.
2. Read-only operations which operate on const struct cpumask *
arguments. For example, bpf_cpumask_test_cpu(), which tests whether a
CPU is set in the cpumask. Any trusted struct cpumask * or struct
bpf_cpumask * may be passed to these kfuncs. The verifier allows
struct bpf_cpumask * even though the kfunc is defined with struct
cpumask * because the first element of a struct bpf_cpumask is a
cpumask_t, so it is safe to cast.
A follow-on patch will add selftests which validate these kfuncs, and
another will document them.
Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230125143816.721952-3-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-25 07:38:11 -07:00
|
|
|
BTF_ID_FLAGS(func, bpf_cpumask_acquire, KF_ACQUIRE | KF_TRUSTED_ARGS)
|
2023-03-02 21:14:46 -07:00
|
|
|
BTF_ID_FLAGS(func, bpf_cpumask_first, KF_RCU)
|
|
|
|
BTF_ID_FLAGS(func, bpf_cpumask_first_zero, KF_RCU)
|
2023-06-09 20:50:49 -07:00
|
|
|
BTF_ID_FLAGS(func, bpf_cpumask_first_and, KF_RCU)
|
2023-03-02 21:14:46 -07:00
|
|
|
BTF_ID_FLAGS(func, bpf_cpumask_set_cpu, KF_RCU)
|
|
|
|
BTF_ID_FLAGS(func, bpf_cpumask_clear_cpu, KF_RCU)
|
|
|
|
BTF_ID_FLAGS(func, bpf_cpumask_test_cpu, KF_RCU)
|
|
|
|
BTF_ID_FLAGS(func, bpf_cpumask_test_and_set_cpu, KF_RCU)
|
|
|
|
BTF_ID_FLAGS(func, bpf_cpumask_test_and_clear_cpu, KF_RCU)
|
|
|
|
BTF_ID_FLAGS(func, bpf_cpumask_setall, KF_RCU)
|
|
|
|
BTF_ID_FLAGS(func, bpf_cpumask_clear, KF_RCU)
|
|
|
|
BTF_ID_FLAGS(func, bpf_cpumask_and, KF_RCU)
|
|
|
|
BTF_ID_FLAGS(func, bpf_cpumask_or, KF_RCU)
|
|
|
|
BTF_ID_FLAGS(func, bpf_cpumask_xor, KF_RCU)
|
|
|
|
BTF_ID_FLAGS(func, bpf_cpumask_equal, KF_RCU)
|
|
|
|
BTF_ID_FLAGS(func, bpf_cpumask_intersects, KF_RCU)
|
|
|
|
BTF_ID_FLAGS(func, bpf_cpumask_subset, KF_RCU)
|
|
|
|
BTF_ID_FLAGS(func, bpf_cpumask_empty, KF_RCU)
|
|
|
|
BTF_ID_FLAGS(func, bpf_cpumask_full, KF_RCU)
|
|
|
|
BTF_ID_FLAGS(func, bpf_cpumask_copy, KF_RCU)
|
2023-06-09 20:50:51 -07:00
|
|
|
BTF_ID_FLAGS(func, bpf_cpumask_any_distribute, KF_RCU)
|
|
|
|
BTF_ID_FLAGS(func, bpf_cpumask_any_and_distribute, KF_RCU)
|
2023-12-07 14:08:42 -07:00
|
|
|
BTF_ID_FLAGS(func, bpf_cpumask_weight, KF_RCU)
|
2024-01-28 18:24:08 -07:00
|
|
|
BTF_KFUNCS_END(cpumask_kfunc_btf_ids)
|
bpf: Enable cpumasks to be queried and used as kptrs
Certain programs may wish to be able to query cpumasks. For example, if
a program that is tracing percpu operations wishes to track which tasks
end up running on which CPUs, it could be useful to associate that with
the tasks' cpumasks. Similarly, programs tracking NUMA allocations, CPU
scheduling domains, etc, could potentially benefit from being able to
see which CPUs a task could be migrated to.
This patch enables these types of use cases by introducing a series of
bpf_cpumask_* kfuncs. Amongst these kfuncs, there are two separate
"classes" of operations:
1. kfuncs which allow the caller to allocate and mutate their own
cpumask kptrs in the form of a struct bpf_cpumask * object. Such
kfuncs include e.g. bpf_cpumask_create() to allocate the cpumask, and
bpf_cpumask_or() to mutate it. "Regular" cpumasks such as p->cpus_ptr
may not be passed to these kfuncs, and the verifier will ensure this
is the case by comparing BTF IDs.
2. Read-only operations which operate on const struct cpumask *
arguments. For example, bpf_cpumask_test_cpu(), which tests whether a
CPU is set in the cpumask. Any trusted struct cpumask * or struct
bpf_cpumask * may be passed to these kfuncs. The verifier allows
struct bpf_cpumask * even though the kfunc is defined with struct
cpumask * because the first element of a struct bpf_cpumask is a
cpumask_t, so it is safe to cast.
A follow-on patch will add selftests which validate these kfuncs, and
another will document them.
Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230125143816.721952-3-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-25 07:38:11 -07:00
|
|
|
|
|
|
|
static const struct btf_kfunc_id_set cpumask_kfunc_set = {
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.set = &cpumask_kfunc_btf_ids,
|
|
|
|
};
|
|
|
|
|
|
|
|
BTF_ID_LIST(cpumask_dtor_ids)
|
|
|
|
BTF_ID(struct, bpf_cpumask)
|
2023-12-15 02:12:22 -07:00
|
|
|
BTF_ID(func, bpf_cpumask_release_dtor)
|
bpf: Enable cpumasks to be queried and used as kptrs
Certain programs may wish to be able to query cpumasks. For example, if
a program that is tracing percpu operations wishes to track which tasks
end up running on which CPUs, it could be useful to associate that with
the tasks' cpumasks. Similarly, programs tracking NUMA allocations, CPU
scheduling domains, etc, could potentially benefit from being able to
see which CPUs a task could be migrated to.
This patch enables these types of use cases by introducing a series of
bpf_cpumask_* kfuncs. Amongst these kfuncs, there are two separate
"classes" of operations:
1. kfuncs which allow the caller to allocate and mutate their own
cpumask kptrs in the form of a struct bpf_cpumask * object. Such
kfuncs include e.g. bpf_cpumask_create() to allocate the cpumask, and
bpf_cpumask_or() to mutate it. "Regular" cpumasks such as p->cpus_ptr
may not be passed to these kfuncs, and the verifier will ensure this
is the case by comparing BTF IDs.
2. Read-only operations which operate on const struct cpumask *
arguments. For example, bpf_cpumask_test_cpu(), which tests whether a
CPU is set in the cpumask. Any trusted struct cpumask * or struct
bpf_cpumask * may be passed to these kfuncs. The verifier allows
struct bpf_cpumask * even though the kfunc is defined with struct
cpumask * because the first element of a struct bpf_cpumask is a
cpumask_t, so it is safe to cast.
A follow-on patch will add selftests which validate these kfuncs, and
another will document them.
Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230125143816.721952-3-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-25 07:38:11 -07:00
|
|
|
|
|
|
|
static int __init cpumask_kfunc_init(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
const struct btf_id_dtor_kfunc cpumask_dtors[] = {
|
|
|
|
{
|
|
|
|
.btf_id = cpumask_dtor_ids[0],
|
|
|
|
.kfunc_btf_id = cpumask_dtor_ids[1]
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-02-15 19:48:21 -07:00
|
|
|
ret = bpf_mem_alloc_init(&bpf_cpumask_ma, sizeof(struct bpf_cpumask), false);
|
bpf: Enable cpumasks to be queried and used as kptrs
Certain programs may wish to be able to query cpumasks. For example, if
a program that is tracing percpu operations wishes to track which tasks
end up running on which CPUs, it could be useful to associate that with
the tasks' cpumasks. Similarly, programs tracking NUMA allocations, CPU
scheduling domains, etc, could potentially benefit from being able to
see which CPUs a task could be migrated to.
This patch enables these types of use cases by introducing a series of
bpf_cpumask_* kfuncs. Amongst these kfuncs, there are two separate
"classes" of operations:
1. kfuncs which allow the caller to allocate and mutate their own
cpumask kptrs in the form of a struct bpf_cpumask * object. Such
kfuncs include e.g. bpf_cpumask_create() to allocate the cpumask, and
bpf_cpumask_or() to mutate it. "Regular" cpumasks such as p->cpus_ptr
may not be passed to these kfuncs, and the verifier will ensure this
is the case by comparing BTF IDs.
2. Read-only operations which operate on const struct cpumask *
arguments. For example, bpf_cpumask_test_cpu(), which tests whether a
CPU is set in the cpumask. Any trusted struct cpumask * or struct
bpf_cpumask * may be passed to these kfuncs. The verifier allows
struct bpf_cpumask * even though the kfunc is defined with struct
cpumask * because the first element of a struct bpf_cpumask is a
cpumask_t, so it is safe to cast.
A follow-on patch will add selftests which validate these kfuncs, and
another will document them.
Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230125143816.721952-3-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-25 07:38:11 -07:00
|
|
|
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &cpumask_kfunc_set);
|
|
|
|
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &cpumask_kfunc_set);
|
|
|
|
return ret ?: register_btf_id_dtor_kfuncs(cpumask_dtors,
|
|
|
|
ARRAY_SIZE(cpumask_dtors),
|
|
|
|
THIS_MODULE);
|
|
|
|
}
|
|
|
|
|
|
|
|
late_initcall(cpumask_kfunc_init);
|