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