File Index
Files and directories
Sign in
FILE

cpumask_common.h

File preview with syntax highlighting.

linux-6.19.14/tools/testing/selftests/bpf/progs/cpumask_common.h C
  1/* SPDX-License-Identifier: GPL-2.0 */
  2/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
  3
  4#ifndef _CPUMASK_COMMON_H
  5#define _CPUMASK_COMMON_H
  6
  7#include "errno.h"
  8#include <stdbool.h>
  9
 10/* Should use BTF_FIELDS_MAX, but it is not always available in vmlinux.h,
 11 * so use the hard-coded number as a workaround.
 12 */
 13#define CPUMASK_KPTR_FIELDS_MAX 11
 14
 15int err;
 16
 17#define private(name) SEC(".bss." #name) __attribute__((aligned(8)))
 18private(MASK) static struct bpf_cpumask __kptr * global_mask;
 19
 20struct __cpumask_map_value {
 21	struct bpf_cpumask __kptr * cpumask;
 22};
 23
 24struct array_map {
 25	__uint(type, BPF_MAP_TYPE_ARRAY);
 26	__type(key, int);
 27	__type(value, struct __cpumask_map_value);
 28	__uint(max_entries, 1);
 29} __cpumask_map SEC(".maps");
 30
 31struct bpf_cpumask *bpf_cpumask_create(void) __ksym __weak;
 32void bpf_cpumask_release(struct bpf_cpumask *cpumask) __ksym __weak;
 33struct bpf_cpumask *bpf_cpumask_acquire(struct bpf_cpumask *cpumask) __ksym __weak;
 34u32 bpf_cpumask_first(const struct cpumask *cpumask) __ksym __weak;
 35u32 bpf_cpumask_first_zero(const struct cpumask *cpumask) __ksym __weak;
 36u32 bpf_cpumask_first_and(const struct cpumask *src1,
 37			  const struct cpumask *src2) __ksym __weak;
 38void bpf_cpumask_set_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym __weak;
 39void bpf_cpumask_clear_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym __weak;
 40bool bpf_cpumask_test_cpu(u32 cpu, const struct cpumask *cpumask) __ksym __weak;
 41bool bpf_cpumask_test_and_set_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym __weak;
 42bool bpf_cpumask_test_and_clear_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym __weak;
 43void bpf_cpumask_setall(struct bpf_cpumask *cpumask) __ksym __weak;
 44void bpf_cpumask_clear(struct bpf_cpumask *cpumask) __ksym __weak;
 45bool bpf_cpumask_and(struct bpf_cpumask *cpumask,
 46		     const struct cpumask *src1,
 47		     const struct cpumask *src2) __ksym __weak;
 48void bpf_cpumask_or(struct bpf_cpumask *cpumask,
 49		    const struct cpumask *src1,
 50		    const struct cpumask *src2) __ksym __weak;
 51void bpf_cpumask_xor(struct bpf_cpumask *cpumask,
 52		     const struct cpumask *src1,
 53		     const struct cpumask *src2) __ksym __weak;
 54bool bpf_cpumask_equal(const struct cpumask *src1, const struct cpumask *src2) __ksym __weak;
 55bool bpf_cpumask_intersects(const struct cpumask *src1, const struct cpumask *src2) __ksym __weak;
 56bool bpf_cpumask_subset(const struct cpumask *src1, const struct cpumask *src2) __ksym __weak;
 57bool bpf_cpumask_empty(const struct cpumask *cpumask) __ksym __weak;
 58bool bpf_cpumask_full(const struct cpumask *cpumask) __ksym __weak;
 59void bpf_cpumask_copy(struct bpf_cpumask *dst, const struct cpumask *src) __ksym __weak;
 60u32 bpf_cpumask_any_distribute(const struct cpumask *src) __ksym __weak;
 61u32 bpf_cpumask_any_and_distribute(const struct cpumask *src1,
 62				   const struct cpumask *src2) __ksym __weak;
 63u32 bpf_cpumask_weight(const struct cpumask *cpumask) __ksym __weak;
 64int bpf_cpumask_populate(struct cpumask *cpumask, void *src, size_t src__sz) __ksym __weak;
 65
 66void bpf_rcu_read_lock(void) __ksym __weak;
 67void bpf_rcu_read_unlock(void) __ksym __weak;
 68
 69static inline const struct cpumask *cast(struct bpf_cpumask *cpumask)
 70{
 71	return (const struct cpumask *)cpumask;
 72}
 73
 74static inline struct bpf_cpumask *create_cpumask(void)
 75{
 76	struct bpf_cpumask *cpumask;
 77
 78	cpumask = bpf_cpumask_create();
 79	if (!cpumask) {
 80		err = 1;
 81		return NULL;
 82	}
 83
 84	if (!bpf_cpumask_empty(cast(cpumask))) {
 85		err = 2;
 86		bpf_cpumask_release(cpumask);
 87		return NULL;
 88	}
 89
 90	return cpumask;
 91}
 92
 93static inline struct __cpumask_map_value *cpumask_map_value_lookup(void)
 94{
 95	u32 key = 0;
 96
 97	return bpf_map_lookup_elem(&__cpumask_map, &key);
 98}
 99
100static inline int cpumask_map_insert(struct bpf_cpumask *mask)
101{
102	struct __cpumask_map_value local, *v;
103	long status;
104	struct bpf_cpumask *old;
105	u32 key = 0;
106
107	local.cpumask = NULL;
108	status = bpf_map_update_elem(&__cpumask_map, &key, &local, 0);
109	if (status) {
110		bpf_cpumask_release(mask);
111		return status;
112	}
113
114	v = bpf_map_lookup_elem(&__cpumask_map, &key);
115	if (!v) {
116		bpf_cpumask_release(mask);
117		return -ENOENT;
118	}
119
120	old = bpf_kptr_xchg(&v->cpumask, mask);
121	if (old) {
122		bpf_cpumask_release(old);
123		return -EEXIST;
124	}
125
126	return 0;
127}
128
129#endif /* _CPUMASK_COMMON_H */
~/ Read-only file preview.