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