1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /* Copyright (c) 2016 Facebook
4 #ifndef __BPF_LRU_LIST_H_
5 #define __BPF_LRU_LIST_H_
7 #include <linux/list.h>
8 #include <linux/spinlock_types.h>
10 #define NR_BPF_LRU_LIST_T (3)
11 #define NR_BPF_LRU_LIST_COUNT (2)
12 #define NR_BPF_LRU_LOCAL_LIST_T (2)
13 #define BPF_LOCAL_LIST_T_OFFSET NR_BPF_LRU_LIST_T
15 enum bpf_lru_list_type
{
16 BPF_LRU_LIST_T_ACTIVE
,
17 BPF_LRU_LIST_T_INACTIVE
,
19 BPF_LRU_LOCAL_LIST_T_FREE
,
20 BPF_LRU_LOCAL_LIST_T_PENDING
,
24 struct list_head list
;
31 struct list_head lists
[NR_BPF_LRU_LIST_T
];
32 unsigned int counts
[NR_BPF_LRU_LIST_COUNT
];
33 /* The next inactive list rotation starts from here */
34 struct list_head
*next_inactive_rotation
;
36 raw_spinlock_t lock ____cacheline_aligned_in_smp
;
39 struct bpf_lru_locallist
{
40 struct list_head lists
[NR_BPF_LRU_LOCAL_LIST_T
];
45 struct bpf_common_lru
{
46 struct bpf_lru_list lru_list
;
47 struct bpf_lru_locallist __percpu
*local_list
;
50 typedef bool (*del_from_htab_func
)(void *arg
, struct bpf_lru_node
*node
);
54 struct bpf_common_lru common_lru
;
55 struct bpf_lru_list __percpu
*percpu_lru
;
57 del_from_htab_func del_from_htab
;
59 unsigned int hash_offset
;
60 unsigned int nr_scans
;
64 static inline void bpf_lru_node_set_ref(struct bpf_lru_node
*node
)
66 /* ref is an approximation on access frequency. It does not
67 * have to be very accurate. Hence, no protection is used.
73 int bpf_lru_init(struct bpf_lru
*lru
, bool percpu
, u32 hash_offset
,
74 del_from_htab_func del_from_htab
, void *delete_arg
);
75 void bpf_lru_populate(struct bpf_lru
*lru
, void *buf
, u32 node_offset
,
76 u32 elem_size
, u32 nr_elems
);
77 void bpf_lru_destroy(struct bpf_lru
*lru
);
78 struct bpf_lru_node
*bpf_lru_pop_free(struct bpf_lru
*lru
, u32 hash
);
79 void bpf_lru_push_free(struct bpf_lru
*lru
, struct bpf_lru_node
*node
);
80 void bpf_lru_promote(struct bpf_lru
*lru
, struct bpf_lru_node
*node
);