125763b3cSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
23a08c2fdSMartin KaFai Lau /* Copyright (c) 2016 Facebook
33a08c2fdSMartin KaFai Lau */
43a08c2fdSMartin KaFai Lau #ifndef __BPF_LRU_LIST_H_
53a08c2fdSMartin KaFai Lau #define __BPF_LRU_LIST_H_
63a08c2fdSMartin KaFai Lau
7534aa1dcSPeter Collingbourne #include <linux/cache.h>
83a08c2fdSMartin KaFai Lau #include <linux/list.h>
93a08c2fdSMartin KaFai Lau #include <linux/spinlock_types.h>
103a08c2fdSMartin KaFai Lau
113a08c2fdSMartin KaFai Lau #define NR_BPF_LRU_LIST_T (3)
123a08c2fdSMartin KaFai Lau #define NR_BPF_LRU_LIST_COUNT (2)
133a08c2fdSMartin KaFai Lau #define NR_BPF_LRU_LOCAL_LIST_T (2)
143a08c2fdSMartin KaFai Lau #define BPF_LOCAL_LIST_T_OFFSET NR_BPF_LRU_LIST_T
153a08c2fdSMartin KaFai Lau
163a08c2fdSMartin KaFai Lau enum bpf_lru_list_type {
173a08c2fdSMartin KaFai Lau BPF_LRU_LIST_T_ACTIVE,
183a08c2fdSMartin KaFai Lau BPF_LRU_LIST_T_INACTIVE,
193a08c2fdSMartin KaFai Lau BPF_LRU_LIST_T_FREE,
203a08c2fdSMartin KaFai Lau BPF_LRU_LOCAL_LIST_T_FREE,
213a08c2fdSMartin KaFai Lau BPF_LRU_LOCAL_LIST_T_PENDING,
223a08c2fdSMartin KaFai Lau };
233a08c2fdSMartin KaFai Lau
243a08c2fdSMartin KaFai Lau struct bpf_lru_node {
253a08c2fdSMartin KaFai Lau struct list_head list;
263a08c2fdSMartin KaFai Lau u16 cpu;
273a08c2fdSMartin KaFai Lau u8 type;
283a08c2fdSMartin KaFai Lau u8 ref;
293a08c2fdSMartin KaFai Lau };
303a08c2fdSMartin KaFai Lau
313a08c2fdSMartin KaFai Lau struct bpf_lru_list {
323a08c2fdSMartin KaFai Lau struct list_head lists[NR_BPF_LRU_LIST_T];
333a08c2fdSMartin KaFai Lau unsigned int counts[NR_BPF_LRU_LIST_COUNT];
340ac16296SQiujun Huang /* The next inactive list rotation starts from here */
353a08c2fdSMartin KaFai Lau struct list_head *next_inactive_rotation;
363a08c2fdSMartin KaFai Lau
373a08c2fdSMartin KaFai Lau raw_spinlock_t lock ____cacheline_aligned_in_smp;
383a08c2fdSMartin KaFai Lau };
393a08c2fdSMartin KaFai Lau
403a08c2fdSMartin KaFai Lau struct bpf_lru_locallist {
413a08c2fdSMartin KaFai Lau struct list_head lists[NR_BPF_LRU_LOCAL_LIST_T];
423a08c2fdSMartin KaFai Lau u16 next_steal;
433a08c2fdSMartin KaFai Lau raw_spinlock_t lock;
443a08c2fdSMartin KaFai Lau };
453a08c2fdSMartin KaFai Lau
463a08c2fdSMartin KaFai Lau struct bpf_common_lru {
473a08c2fdSMartin KaFai Lau struct bpf_lru_list lru_list;
483a08c2fdSMartin KaFai Lau struct bpf_lru_locallist __percpu *local_list;
493a08c2fdSMartin KaFai Lau };
503a08c2fdSMartin KaFai Lau
513a08c2fdSMartin KaFai Lau typedef bool (*del_from_htab_func)(void *arg, struct bpf_lru_node *node);
523a08c2fdSMartin KaFai Lau
533a08c2fdSMartin KaFai Lau struct bpf_lru {
54961578b6SMartin KaFai Lau union {
553a08c2fdSMartin KaFai Lau struct bpf_common_lru common_lru;
56961578b6SMartin KaFai Lau struct bpf_lru_list __percpu *percpu_lru;
57961578b6SMartin KaFai Lau };
583a08c2fdSMartin KaFai Lau del_from_htab_func del_from_htab;
593a08c2fdSMartin KaFai Lau void *del_arg;
603a08c2fdSMartin KaFai Lau unsigned int hash_offset;
613a08c2fdSMartin KaFai Lau unsigned int nr_scans;
62961578b6SMartin KaFai Lau bool percpu;
633a08c2fdSMartin KaFai Lau };
643a08c2fdSMartin KaFai Lau
bpf_lru_node_set_ref(struct bpf_lru_node * node)653a08c2fdSMartin KaFai Lau static inline void bpf_lru_node_set_ref(struct bpf_lru_node *node)
663a08c2fdSMartin KaFai Lau {
67*ee9fd0acSMartin KaFai Lau if (!READ_ONCE(node->ref))
68*ee9fd0acSMartin KaFai Lau WRITE_ONCE(node->ref, 1);
693a08c2fdSMartin KaFai Lau }
703a08c2fdSMartin KaFai Lau
71961578b6SMartin KaFai Lau int bpf_lru_init(struct bpf_lru *lru, bool percpu, u32 hash_offset,
723a08c2fdSMartin KaFai Lau del_from_htab_func del_from_htab, void *delete_arg);
733a08c2fdSMartin KaFai Lau void bpf_lru_populate(struct bpf_lru *lru, void *buf, u32 node_offset,
743a08c2fdSMartin KaFai Lau u32 elem_size, u32 nr_elems);
753a08c2fdSMartin KaFai Lau void bpf_lru_destroy(struct bpf_lru *lru);
763a08c2fdSMartin KaFai Lau struct bpf_lru_node *bpf_lru_pop_free(struct bpf_lru *lru, u32 hash);
773a08c2fdSMartin KaFai Lau void bpf_lru_push_free(struct bpf_lru *lru, struct bpf_lru_node *node);
783a08c2fdSMartin KaFai Lau
793a08c2fdSMartin KaFai Lau #endif
80