1 /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
2 /* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
4 #include "bpf_arena_common.h"
7 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
10 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
14 #define NR_CPUS (sizeof(struct cpumask) * 8)
16 static void __arena
* __arena page_frag_cur_page
[NR_CPUS
];
17 static int __arena page_frag_cur_offset
[NR_CPUS
];
19 /* Simple page_frag allocator */
20 static inline void __arena
* bpf_alloc(unsigned int size
)
22 __u64 __arena
*obj_cnt
;
23 __u32 cpu
= bpf_get_smp_processor_id();
24 void __arena
*page
= page_frag_cur_page
[cpu
];
25 int __arena
*cur_offset
= &page_frag_cur_offset
[cpu
];
28 size
= round_up(size
, 8);
29 if (size
>= PAGE_SIZE
- 8)
33 page
= bpf_arena_alloc_pages(&arena
, NULL
, 1, NUMA_NO_NODE
, 0);
37 page_frag_cur_page
[cpu
] = page
;
38 *cur_offset
= PAGE_SIZE
- 8;
39 obj_cnt
= page
+ PAGE_SIZE
- 8;
43 obj_cnt
= page
+ PAGE_SIZE
- 8;
46 offset
= *cur_offset
- size
;
55 static inline void bpf_free(void __arena
*addr
)
57 __u64 __arena
*obj_cnt
;
59 addr
= (void __arena
*)(((long)addr
) & ~(PAGE_SIZE
- 1));
60 obj_cnt
= addr
+ PAGE_SIZE
- 8;
61 if (--(*obj_cnt
) == 0)
62 bpf_arena_free_pages(&arena
, addr
, 1);
65 static inline void __arena
* bpf_alloc(unsigned int size
) { return NULL
; }
66 static inline void bpf_free(void __arena
*addr
) {}