summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/progs/cpumask_common.h
blob: b979e91f55f07906736f12746b9c1e58d84b29c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* SPDX-License-Identifier: GPL-2.0 */
/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */

#ifndef _CPUMASK_COMMON_H
#define _CPUMASK_COMMON_H

#include "errno.h"
#include <stdbool.h>

int err;

#define private(name) SEC(".bss." #name) __attribute__((aligned(8)))
private(MASK) static struct bpf_cpumask __kptr * global_mask;

struct __cpumask_map_value {
	struct bpf_cpumask __kptr * cpumask;
};

struct array_map {
	__uint(type, BPF_MAP_TYPE_ARRAY);
	__type(key, int);
	__type(value, struct __cpumask_map_value);
	__uint(max_entries, 1);
} __cpumask_map SEC(".maps");

struct bpf_cpumask *bpf_cpumask_create(void) __ksym __weak;
void bpf_cpumask_release(struct bpf_cpumask *cpumask) __ksym __weak;
struct bpf_cpumask *bpf_cpumask_acquire(struct bpf_cpumask *cpumask) __ksym __weak;
u32 bpf_cpumask_first(const struct cpumask *cpumask) __ksym __weak;
u32 bpf_cpumask_first_zero(const struct cpumask *cpumask) __ksym __weak;
u32 bpf_cpumask_first_and(const struct cpumask *src1,
			  const struct cpumask *src2) __ksym __weak;
void bpf_cpumask_set_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym __weak;
void bpf_cpumask_clear_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym __weak;
bool bpf_cpumask_test_cpu(u32 cpu, const struct cpumask *cpumask) __ksym __weak;
bool bpf_cpumask_test_and_set_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym __weak;
bool bpf_cpumask_test_and_clear_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym __weak;
void bpf_cpumask_setall(struct bpf_cpumask *cpumask) __ksym __weak;
void bpf_cpumask_clear(struct bpf_cpumask *cpumask) __ksym __weak;
bool bpf_cpumask_and(struct bpf_cpumask *cpumask,
		     const struct cpumask *src1,
		     const struct cpumask *src2) __ksym __weak;
void bpf_cpumask_or(struct bpf_cpumask *cpumask,
		    const struct cpumask *src1,
		    const struct cpumask *src2) __ksym __weak;
void bpf_cpumask_xor(struct bpf_cpumask *cpumask,
		     const struct cpumask *src1,
		     const struct cpumask *src2) __ksym __weak;
bool bpf_cpumask_equal(const struct cpumask *src1, const struct cpumask *src2) __ksym __weak;
bool bpf_cpumask_intersects(const struct cpumask *src1, const struct cpumask *src2) __ksym __weak;
bool bpf_cpumask_subset(const struct cpumask *src1, const struct cpumask *src2) __ksym __weak;
bool bpf_cpumask_empty(const struct cpumask *cpumask) __ksym __weak;
bool bpf_cpumask_full(const struct cpumask *cpumask) __ksym __weak;
void bpf_cpumask_copy(struct bpf_cpumask *dst, const struct cpumask *src) __ksym __weak;
u32 bpf_cpumask_any_distribute(const struct cpumask *src) __ksym __weak;
u32 bpf_cpumask_any_and_distribute(const struct cpumask *src1,
				   const struct cpumask *src2) __ksym __weak;
u32 bpf_cpumask_weight(const struct cpumask *cpumask) __ksym __weak;

void bpf_rcu_read_lock(void) __ksym __weak;
void bpf_rcu_read_unlock(void) __ksym __weak;

static inline const struct cpumask *cast(struct bpf_cpumask *cpumask)
{
	return (const struct cpumask *)cpumask;
}

static inline struct bpf_cpumask *create_cpumask(void)
{
	struct bpf_cpumask *cpumask;

	cpumask = bpf_cpumask_create();
	if (!cpumask) {
		err = 1;
		return NULL;
	}

	if (!bpf_cpumask_empty(cast(cpumask))) {
		err = 2;
		bpf_cpumask_release(cpumask);
		return NULL;
	}

	return cpumask;
}

static inline struct __cpumask_map_value *cpumask_map_value_lookup(void)
{
	u32 key = 0;

	return bpf_map_lookup_elem(&__cpumask_map, &key);
}

static inline int cpumask_map_insert(struct bpf_cpumask *mask)
{
	struct __cpumask_map_value local, *v;
	long status;
	struct bpf_cpumask *old;
	u32 key = 0;

	local.cpumask = NULL;
	status = bpf_map_update_elem(&__cpumask_map, &key, &local, 0);
	if (status) {
		bpf_cpumask_release(mask);
		return status;
	}

	v = bpf_map_lookup_elem(&__cpumask_map, &key);
	if (!v) {
		bpf_cpumask_release(mask);
		return -ENOENT;
	}

	old = bpf_kptr_xchg(&v->cpumask, mask);
	if (old) {
		bpf_cpumask_release(old);
		return -EEXIST;
	}

	return 0;
}

#endif /* _CPUMASK_COMMON_H */