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 int err;
11 
12 struct __cpumask_map_value {
13 	struct bpf_cpumask __kptr_ref * cpumask;
14 };
15 
16 struct array_map {
17 	__uint(type, BPF_MAP_TYPE_ARRAY);
18 	__type(key, int);
19 	__type(value, struct __cpumask_map_value);
20 	__uint(max_entries, 1);
21 } __cpumask_map SEC(".maps");
22 
23 struct bpf_cpumask *bpf_cpumask_create(void) __ksym;
24 void bpf_cpumask_release(struct bpf_cpumask *cpumask) __ksym;
25 struct bpf_cpumask *bpf_cpumask_acquire(struct bpf_cpumask *cpumask) __ksym;
26 struct bpf_cpumask *bpf_cpumask_kptr_get(struct bpf_cpumask **cpumask) __ksym;
27 u32 bpf_cpumask_first(const struct cpumask *cpumask) __ksym;
28 u32 bpf_cpumask_first_zero(const struct cpumask *cpumask) __ksym;
29 void bpf_cpumask_set_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym;
30 void bpf_cpumask_clear_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym;
31 bool bpf_cpumask_test_cpu(u32 cpu, const struct cpumask *cpumask) __ksym;
32 bool bpf_cpumask_test_and_set_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym;
33 bool bpf_cpumask_test_and_clear_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym;
34 void bpf_cpumask_setall(struct bpf_cpumask *cpumask) __ksym;
35 void bpf_cpumask_clear(struct bpf_cpumask *cpumask) __ksym;
36 bool bpf_cpumask_and(struct bpf_cpumask *cpumask,
37 		     const struct cpumask *src1,
38 		     const struct cpumask *src2) __ksym;
39 void bpf_cpumask_or(struct bpf_cpumask *cpumask,
40 		    const struct cpumask *src1,
41 		    const struct cpumask *src2) __ksym;
42 void bpf_cpumask_xor(struct bpf_cpumask *cpumask,
43 		     const struct cpumask *src1,
44 		     const struct cpumask *src2) __ksym;
45 bool bpf_cpumask_equal(const struct cpumask *src1, const struct cpumask *src2) __ksym;
46 bool bpf_cpumask_intersects(const struct cpumask *src1, const struct cpumask *src2) __ksym;
47 bool bpf_cpumask_subset(const struct cpumask *src1, const struct cpumask *src2) __ksym;
48 bool bpf_cpumask_empty(const struct cpumask *cpumask) __ksym;
49 bool bpf_cpumask_full(const struct cpumask *cpumask) __ksym;
50 void bpf_cpumask_copy(struct bpf_cpumask *dst, const struct cpumask *src) __ksym;
51 u32 bpf_cpumask_any(const struct cpumask *src) __ksym;
52 u32 bpf_cpumask_any_and(const struct cpumask *src1, const struct cpumask *src2) __ksym;
53 
54 static inline const struct cpumask *cast(struct bpf_cpumask *cpumask)
55 {
56 	return (const struct cpumask *)cpumask;
57 }
58 
59 static inline struct bpf_cpumask *create_cpumask(void)
60 {
61 	struct bpf_cpumask *cpumask;
62 
63 	cpumask = bpf_cpumask_create();
64 	if (!cpumask) {
65 		err = 1;
66 		return NULL;
67 	}
68 
69 	if (!bpf_cpumask_empty(cast(cpumask))) {
70 		err = 2;
71 		bpf_cpumask_release(cpumask);
72 		return NULL;
73 	}
74 
75 	return cpumask;
76 }
77 
78 static inline struct __cpumask_map_value *cpumask_map_value_lookup(void)
79 {
80 	u32 key = 0;
81 
82 	return bpf_map_lookup_elem(&__cpumask_map, &key);
83 }
84 
85 static inline int cpumask_map_insert(struct bpf_cpumask *mask)
86 {
87 	struct __cpumask_map_value local, *v;
88 	long status;
89 	struct bpf_cpumask *old;
90 	u32 key = 0;
91 
92 	local.cpumask = NULL;
93 	status = bpf_map_update_elem(&__cpumask_map, &key, &local, 0);
94 	if (status) {
95 		bpf_cpumask_release(mask);
96 		return status;
97 	}
98 
99 	v = bpf_map_lookup_elem(&__cpumask_map, &key);
100 	if (!v) {
101 		bpf_cpumask_release(mask);
102 		return -ENOENT;
103 	}
104 
105 	old = bpf_kptr_xchg(&v->cpumask, mask);
106 	if (old) {
107 		bpf_cpumask_release(old);
108 		return -EEXIST;
109 	}
110 
111 	return 0;
112 }
113 
114 #endif /* _CPUMASK_COMMON_H */
115