bpf_lru_list.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /* Copyright (c) 2016 Facebook
  3. */
  4. #ifndef __BPF_LRU_LIST_H_
  5. #define __BPF_LRU_LIST_H_
  6. #include <linux/cache.h>
  7. #include <linux/list.h>
  8. #include <linux/spinlock_types.h>
  9. #define NR_BPF_LRU_LIST_T (3)
  10. #define NR_BPF_LRU_LIST_COUNT (2)
  11. #define NR_BPF_LRU_LOCAL_LIST_T (2)
  12. #define BPF_LOCAL_LIST_T_OFFSET NR_BPF_LRU_LIST_T
  13. enum bpf_lru_list_type {
  14. BPF_LRU_LIST_T_ACTIVE,
  15. BPF_LRU_LIST_T_INACTIVE,
  16. BPF_LRU_LIST_T_FREE,
  17. BPF_LRU_LOCAL_LIST_T_FREE,
  18. BPF_LRU_LOCAL_LIST_T_PENDING,
  19. };
  20. struct bpf_lru_node {
  21. struct list_head list;
  22. u16 cpu;
  23. u8 type;
  24. u8 ref;
  25. };
  26. struct bpf_lru_list {
  27. struct list_head lists[NR_BPF_LRU_LIST_T];
  28. unsigned int counts[NR_BPF_LRU_LIST_COUNT];
  29. /* The next inactive list rotation starts from here */
  30. struct list_head *next_inactive_rotation;
  31. raw_spinlock_t lock ____cacheline_aligned_in_smp;
  32. };
  33. struct bpf_lru_locallist {
  34. struct list_head lists[NR_BPF_LRU_LOCAL_LIST_T];
  35. u16 next_steal;
  36. raw_spinlock_t lock;
  37. };
  38. struct bpf_common_lru {
  39. struct bpf_lru_list lru_list;
  40. struct bpf_lru_locallist __percpu *local_list;
  41. };
  42. typedef bool (*del_from_htab_func)(void *arg, struct bpf_lru_node *node);
  43. struct bpf_lru {
  44. union {
  45. struct bpf_common_lru common_lru;
  46. struct bpf_lru_list __percpu *percpu_lru;
  47. };
  48. del_from_htab_func del_from_htab;
  49. void *del_arg;
  50. unsigned int hash_offset;
  51. unsigned int target_free;
  52. unsigned int nr_scans;
  53. bool percpu;
  54. };
  55. static inline void bpf_lru_node_set_ref(struct bpf_lru_node *node)
  56. {
  57. if (!READ_ONCE(node->ref))
  58. WRITE_ONCE(node->ref, 1);
  59. }
  60. int bpf_lru_init(struct bpf_lru *lru, bool percpu, u32 hash_offset,
  61. del_from_htab_func del_from_htab, void *delete_arg);
  62. void bpf_lru_populate(struct bpf_lru *lru, void *buf, u32 node_offset,
  63. u32 elem_size, u32 nr_elems);
  64. void bpf_lru_destroy(struct bpf_lru *lru);
  65. struct bpf_lru_node *bpf_lru_pop_free(struct bpf_lru *lru, u32 hash);
  66. void bpf_lru_push_free(struct bpf_lru *lru, struct bpf_lru_node *node);
  67. #endif