1 /* Copyright (c) 2016 Facebook
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
7 #include <linux/cpumask.h>
8 #include <linux/spinlock.h>
9 #include <linux/percpu.h>
11 #include "bpf_lru_list.h"
13 #define LOCAL_FREE_TARGET (128)
14 #define LOCAL_NR_SCANS LOCAL_FREE_TARGET
16 #define PERCPU_FREE_TARGET (4)
17 #define PERCPU_NR_SCANS PERCPU_FREE_TARGET
19 /* Helpers to get the local list index */
20 #define LOCAL_LIST_IDX(t) ((t) - BPF_LOCAL_LIST_T_OFFSET)
21 #define LOCAL_FREE_LIST_IDX LOCAL_LIST_IDX(BPF_LRU_LOCAL_LIST_T_FREE)
22 #define LOCAL_PENDING_LIST_IDX LOCAL_LIST_IDX(BPF_LRU_LOCAL_LIST_T_PENDING)
23 #define IS_LOCAL_LIST_TYPE(t) ((t) >= BPF_LOCAL_LIST_T_OFFSET)
25 static int get_next_cpu(int cpu)
27 cpu = cpumask_next(cpu, cpu_possible_mask);
28 if (cpu >= nr_cpu_ids)
29 cpu = cpumask_first(cpu_possible_mask);
33 /* Local list helpers */
34 static struct list_head *local_free_list(struct bpf_lru_locallist *loc_l)
36 return &loc_l->lists[LOCAL_FREE_LIST_IDX];
39 static struct list_head *local_pending_list(struct bpf_lru_locallist *loc_l)
41 return &loc_l->lists[LOCAL_PENDING_LIST_IDX];
44 /* bpf_lru_node helpers */
45 static bool bpf_lru_node_is_ref(const struct bpf_lru_node *node)
50 static void bpf_lru_list_count_inc(struct bpf_lru_list *l,
51 enum bpf_lru_list_type type)
53 if (type < NR_BPF_LRU_LIST_COUNT)
57 static void bpf_lru_list_count_dec(struct bpf_lru_list *l,
58 enum bpf_lru_list_type type)
60 if (type < NR_BPF_LRU_LIST_COUNT)
64 static void __bpf_lru_node_move_to_free(struct bpf_lru_list *l,
65 struct bpf_lru_node *node,
66 struct list_head *free_list,
67 enum bpf_lru_list_type tgt_free_type)
69 if (WARN_ON_ONCE(IS_LOCAL_LIST_TYPE(node->type)))
72 /* If the removing node is the next_inactive_rotation candidate,
73 * move the next_inactive_rotation pointer also.
75 if (&node->list == l->next_inactive_rotation)
76 l->next_inactive_rotation = l->next_inactive_rotation->prev;
78 bpf_lru_list_count_dec(l, node->type);
80 node->type = tgt_free_type;
81 list_move(&node->list, free_list);
84 /* Move nodes from local list to the LRU list */
85 static void __bpf_lru_node_move_in(struct bpf_lru_list *l,
86 struct bpf_lru_node *node,
87 enum bpf_lru_list_type tgt_type)
89 if (WARN_ON_ONCE(!IS_LOCAL_LIST_TYPE(node->type)) ||
90 WARN_ON_ONCE(IS_LOCAL_LIST_TYPE(tgt_type)))
93 bpf_lru_list_count_inc(l, tgt_type);
94 node->type = tgt_type;
96 list_move(&node->list, &l->lists[tgt_type]);
99 /* Move nodes between or within active and inactive list (like
100 * active to inactive, inactive to active or tail of active back to
101 * the head of active).
103 static void __bpf_lru_node_move(struct bpf_lru_list *l,
104 struct bpf_lru_node *node,
105 enum bpf_lru_list_type tgt_type)
107 if (WARN_ON_ONCE(IS_LOCAL_LIST_TYPE(node->type)) ||
108 WARN_ON_ONCE(IS_LOCAL_LIST_TYPE(tgt_type)))
111 if (node->type != tgt_type) {
112 bpf_lru_list_count_dec(l, node->type);
113 bpf_lru_list_count_inc(l, tgt_type);
114 node->type = tgt_type;
118 /* If the moving node is the next_inactive_rotation candidate,
119 * move the next_inactive_rotation pointer also.
121 if (&node->list == l->next_inactive_rotation)
122 l->next_inactive_rotation = l->next_inactive_rotation->prev;
124 list_move(&node->list, &l->lists[tgt_type]);
127 static bool bpf_lru_list_inactive_low(const struct bpf_lru_list *l)
129 return l->counts[BPF_LRU_LIST_T_INACTIVE] <
130 l->counts[BPF_LRU_LIST_T_ACTIVE];
133 /* Rotate the active list:
135 * 2. If the node has the ref bit set, it will be rotated
136 * back to the head of active list with the ref bit cleared.
137 * Give this node one more chance to survive in the active list.
138 * 3. If the ref bit is not set, move it to the head of the
140 * 4. It will at most scan nr_scans nodes
142 static void __bpf_lru_list_rotate_active(struct bpf_lru *lru,
143 struct bpf_lru_list *l)
145 struct list_head *active = &l->lists[BPF_LRU_LIST_T_ACTIVE];
146 struct bpf_lru_node *node, *tmp_node, *first_node;
149 first_node = list_first_entry(active, struct bpf_lru_node, list);
150 list_for_each_entry_safe_reverse(node, tmp_node, active, list) {
151 if (bpf_lru_node_is_ref(node))
152 __bpf_lru_node_move(l, node, BPF_LRU_LIST_T_ACTIVE);
154 __bpf_lru_node_move(l, node, BPF_LRU_LIST_T_INACTIVE);
156 if (++i == lru->nr_scans || node == first_node)
161 /* Rotate the inactive list. It starts from the next_inactive_rotation
162 * 1. If the node has ref bit set, it will be moved to the head
163 * of active list with the ref bit cleared.
164 * 2. If the node does not have ref bit set, it will leave it
165 * at its current location (i.e. do nothing) so that it can
166 * be considered during the next inactive_shrink.
167 * 3. It will at most scan nr_scans nodes
169 static void __bpf_lru_list_rotate_inactive(struct bpf_lru *lru,
170 struct bpf_lru_list *l)
172 struct list_head *inactive = &l->lists[BPF_LRU_LIST_T_INACTIVE];
173 struct list_head *cur, *last, *next = inactive;
174 struct bpf_lru_node *node;
177 if (list_empty(inactive))
180 last = l->next_inactive_rotation->next;
181 if (last == inactive)
184 cur = l->next_inactive_rotation;
185 while (i < lru->nr_scans) {
186 if (cur == inactive) {
191 node = list_entry(cur, struct bpf_lru_node, list);
193 if (bpf_lru_node_is_ref(node))
194 __bpf_lru_node_move(l, node, BPF_LRU_LIST_T_ACTIVE);
201 l->next_inactive_rotation = next;
204 /* Shrink the inactive list. It starts from the tail of the
205 * inactive list and only move the nodes without the ref bit
206 * set to the designated free list.
209 __bpf_lru_list_shrink_inactive(struct bpf_lru *lru,
210 struct bpf_lru_list *l,
211 unsigned int tgt_nshrink,
212 struct list_head *free_list,
213 enum bpf_lru_list_type tgt_free_type)
215 struct list_head *inactive = &l->lists[BPF_LRU_LIST_T_INACTIVE];
216 struct bpf_lru_node *node, *tmp_node;
217 unsigned int nshrinked = 0;
220 list_for_each_entry_safe_reverse(node, tmp_node, inactive, list) {
221 if (bpf_lru_node_is_ref(node)) {
222 __bpf_lru_node_move(l, node, BPF_LRU_LIST_T_ACTIVE);
223 } else if (lru->del_from_htab(lru->del_arg, node)) {
224 __bpf_lru_node_move_to_free(l, node, free_list,
226 if (++nshrinked == tgt_nshrink)
230 if (++i == lru->nr_scans)
237 /* 1. Rotate the active list (if needed)
238 * 2. Always rotate the inactive list
240 static void __bpf_lru_list_rotate(struct bpf_lru *lru, struct bpf_lru_list *l)
242 if (bpf_lru_list_inactive_low(l))
243 __bpf_lru_list_rotate_active(lru, l);
245 __bpf_lru_list_rotate_inactive(lru, l);
248 /* Calls __bpf_lru_list_shrink_inactive() to shrink some
249 * ref-bit-cleared nodes and move them to the designated
252 * If it cannot get a free node after calling
253 * __bpf_lru_list_shrink_inactive(). It will just remove
254 * one node from either inactive or active list without
255 * honoring the ref-bit. It prefers inactive list to active
256 * list in this situation.
258 static unsigned int __bpf_lru_list_shrink(struct bpf_lru *lru,
259 struct bpf_lru_list *l,
260 unsigned int tgt_nshrink,
261 struct list_head *free_list,
262 enum bpf_lru_list_type tgt_free_type)
265 struct bpf_lru_node *node, *tmp_node;
266 struct list_head *force_shrink_list;
267 unsigned int nshrinked;
269 nshrinked = __bpf_lru_list_shrink_inactive(lru, l, tgt_nshrink,
270 free_list, tgt_free_type);
274 /* Do a force shrink by ignoring the reference bit */
275 if (!list_empty(&l->lists[BPF_LRU_LIST_T_INACTIVE]))
276 force_shrink_list = &l->lists[BPF_LRU_LIST_T_INACTIVE];
278 force_shrink_list = &l->lists[BPF_LRU_LIST_T_ACTIVE];
280 list_for_each_entry_safe_reverse(node, tmp_node, force_shrink_list,
282 if (lru->del_from_htab(lru->del_arg, node)) {
283 __bpf_lru_node_move_to_free(l, node, free_list,
292 /* Flush the nodes from the local pending list to the LRU list */
293 static void __local_list_flush(struct bpf_lru_list *l,
294 struct bpf_lru_locallist *loc_l)
296 struct bpf_lru_node *node, *tmp_node;
298 list_for_each_entry_safe_reverse(node, tmp_node,
299 local_pending_list(loc_l), list) {
300 if (bpf_lru_node_is_ref(node))
301 __bpf_lru_node_move_in(l, node, BPF_LRU_LIST_T_ACTIVE);
303 __bpf_lru_node_move_in(l, node,
304 BPF_LRU_LIST_T_INACTIVE);
308 static void bpf_lru_list_push_free(struct bpf_lru_list *l,
309 struct bpf_lru_node *node)
313 if (WARN_ON_ONCE(IS_LOCAL_LIST_TYPE(node->type)))
316 raw_spin_lock_irqsave(&l->lock, flags);
317 __bpf_lru_node_move(l, node, BPF_LRU_LIST_T_FREE);
318 raw_spin_unlock_irqrestore(&l->lock, flags);
321 static void bpf_lru_list_pop_free_to_local(struct bpf_lru *lru,
322 struct bpf_lru_locallist *loc_l)
324 struct bpf_lru_list *l = &lru->common_lru.lru_list;
325 struct bpf_lru_node *node, *tmp_node;
326 unsigned int nfree = 0;
328 raw_spin_lock(&l->lock);
330 __local_list_flush(l, loc_l);
332 __bpf_lru_list_rotate(lru, l);
334 list_for_each_entry_safe(node, tmp_node, &l->lists[BPF_LRU_LIST_T_FREE],
336 __bpf_lru_node_move_to_free(l, node, local_free_list(loc_l),
337 BPF_LRU_LOCAL_LIST_T_FREE);
338 if (++nfree == LOCAL_FREE_TARGET)
342 if (nfree < LOCAL_FREE_TARGET)
343 __bpf_lru_list_shrink(lru, l, LOCAL_FREE_TARGET - nfree,
344 local_free_list(loc_l),
345 BPF_LRU_LOCAL_LIST_T_FREE);
347 raw_spin_unlock(&l->lock);
350 static void __local_list_add_pending(struct bpf_lru *lru,
351 struct bpf_lru_locallist *loc_l,
353 struct bpf_lru_node *node,
356 *(u32 *)((void *)node + lru->hash_offset) = hash;
358 node->type = BPF_LRU_LOCAL_LIST_T_PENDING;
360 list_add(&node->list, local_pending_list(loc_l));
363 static struct bpf_lru_node *
364 __local_list_pop_free(struct bpf_lru_locallist *loc_l)
366 struct bpf_lru_node *node;
368 node = list_first_entry_or_null(local_free_list(loc_l),
372 list_del(&node->list);
377 static struct bpf_lru_node *
378 __local_list_pop_pending(struct bpf_lru *lru, struct bpf_lru_locallist *loc_l)
380 struct bpf_lru_node *node;
384 /* Get from the tail (i.e. older element) of the pending list. */
385 list_for_each_entry_reverse(node, local_pending_list(loc_l),
387 if ((!bpf_lru_node_is_ref(node) || force) &&
388 lru->del_from_htab(lru->del_arg, node)) {
389 list_del(&node->list);
402 static struct bpf_lru_node *bpf_percpu_lru_pop_free(struct bpf_lru *lru,
405 struct list_head *free_list;
406 struct bpf_lru_node *node = NULL;
407 struct bpf_lru_list *l;
409 int cpu = raw_smp_processor_id();
411 l = per_cpu_ptr(lru->percpu_lru, cpu);
413 raw_spin_lock_irqsave(&l->lock, flags);
415 __bpf_lru_list_rotate(lru, l);
417 free_list = &l->lists[BPF_LRU_LIST_T_FREE];
418 if (list_empty(free_list))
419 __bpf_lru_list_shrink(lru, l, PERCPU_FREE_TARGET, free_list,
420 BPF_LRU_LIST_T_FREE);
422 if (!list_empty(free_list)) {
423 node = list_first_entry(free_list, struct bpf_lru_node, list);
424 *(u32 *)((void *)node + lru->hash_offset) = hash;
426 __bpf_lru_node_move(l, node, BPF_LRU_LIST_T_INACTIVE);
429 raw_spin_unlock_irqrestore(&l->lock, flags);
434 static struct bpf_lru_node *bpf_common_lru_pop_free(struct bpf_lru *lru,
437 struct bpf_lru_locallist *loc_l, *steal_loc_l;
438 struct bpf_common_lru *clru = &lru->common_lru;
439 struct bpf_lru_node *node;
440 int steal, first_steal;
442 int cpu = raw_smp_processor_id();
444 loc_l = per_cpu_ptr(clru->local_list, cpu);
446 raw_spin_lock_irqsave(&loc_l->lock, flags);
448 node = __local_list_pop_free(loc_l);
450 bpf_lru_list_pop_free_to_local(lru, loc_l);
451 node = __local_list_pop_free(loc_l);
455 __local_list_add_pending(lru, loc_l, cpu, node, hash);
457 raw_spin_unlock_irqrestore(&loc_l->lock, flags);
462 /* No free nodes found from the local free list and
463 * the global LRU list.
465 * Steal from the local free/pending list of the
466 * current CPU and remote CPU in RR. It starts
467 * with the loc_l->next_steal CPU.
470 first_steal = loc_l->next_steal;
473 steal_loc_l = per_cpu_ptr(clru->local_list, steal);
475 raw_spin_lock_irqsave(&steal_loc_l->lock, flags);
477 node = __local_list_pop_free(steal_loc_l);
479 node = __local_list_pop_pending(lru, steal_loc_l);
481 raw_spin_unlock_irqrestore(&steal_loc_l->lock, flags);
483 steal = get_next_cpu(steal);
484 } while (!node && steal != first_steal);
486 loc_l->next_steal = steal;
489 raw_spin_lock_irqsave(&loc_l->lock, flags);
490 __local_list_add_pending(lru, loc_l, cpu, node, hash);
491 raw_spin_unlock_irqrestore(&loc_l->lock, flags);
497 struct bpf_lru_node *bpf_lru_pop_free(struct bpf_lru *lru, u32 hash)
500 return bpf_percpu_lru_pop_free(lru, hash);
502 return bpf_common_lru_pop_free(lru, hash);
505 static void bpf_common_lru_push_free(struct bpf_lru *lru,
506 struct bpf_lru_node *node)
510 if (WARN_ON_ONCE(node->type == BPF_LRU_LIST_T_FREE) ||
511 WARN_ON_ONCE(node->type == BPF_LRU_LOCAL_LIST_T_FREE))
514 if (node->type == BPF_LRU_LOCAL_LIST_T_PENDING) {
515 struct bpf_lru_locallist *loc_l;
517 loc_l = per_cpu_ptr(lru->common_lru.local_list, node->cpu);
519 raw_spin_lock_irqsave(&loc_l->lock, flags);
521 if (unlikely(node->type != BPF_LRU_LOCAL_LIST_T_PENDING)) {
522 raw_spin_unlock_irqrestore(&loc_l->lock, flags);
526 node->type = BPF_LRU_LOCAL_LIST_T_FREE;
528 list_move(&node->list, local_free_list(loc_l));
530 raw_spin_unlock_irqrestore(&loc_l->lock, flags);
535 bpf_lru_list_push_free(&lru->common_lru.lru_list, node);
538 static void bpf_percpu_lru_push_free(struct bpf_lru *lru,
539 struct bpf_lru_node *node)
541 struct bpf_lru_list *l;
544 l = per_cpu_ptr(lru->percpu_lru, node->cpu);
546 raw_spin_lock_irqsave(&l->lock, flags);
548 __bpf_lru_node_move(l, node, BPF_LRU_LIST_T_FREE);
550 raw_spin_unlock_irqrestore(&l->lock, flags);
553 void bpf_lru_push_free(struct bpf_lru *lru, struct bpf_lru_node *node)
556 bpf_percpu_lru_push_free(lru, node);
558 bpf_common_lru_push_free(lru, node);
561 static void bpf_common_lru_populate(struct bpf_lru *lru, void *buf,
562 u32 node_offset, u32 elem_size,
565 struct bpf_lru_list *l = &lru->common_lru.lru_list;
568 for (i = 0; i < nr_elems; i++) {
569 struct bpf_lru_node *node;
571 node = (struct bpf_lru_node *)(buf + node_offset);
572 node->type = BPF_LRU_LIST_T_FREE;
574 list_add(&node->list, &l->lists[BPF_LRU_LIST_T_FREE]);
579 static void bpf_percpu_lru_populate(struct bpf_lru *lru, void *buf,
580 u32 node_offset, u32 elem_size,
585 struct bpf_lru_list *l;
587 pcpu_entries = nr_elems / num_possible_cpus();
591 for_each_possible_cpu(cpu) {
592 struct bpf_lru_node *node;
594 l = per_cpu_ptr(lru->percpu_lru, cpu);
596 node = (struct bpf_lru_node *)(buf + node_offset);
598 node->type = BPF_LRU_LIST_T_FREE;
600 list_add(&node->list, &l->lists[BPF_LRU_LIST_T_FREE]);
605 if (i % pcpu_entries)
610 void bpf_lru_populate(struct bpf_lru *lru, void *buf, u32 node_offset,
611 u32 elem_size, u32 nr_elems)
614 bpf_percpu_lru_populate(lru, buf, node_offset, elem_size,
617 bpf_common_lru_populate(lru, buf, node_offset, elem_size,
621 static void bpf_lru_locallist_init(struct bpf_lru_locallist *loc_l, int cpu)
625 for (i = 0; i < NR_BPF_LRU_LOCAL_LIST_T; i++)
626 INIT_LIST_HEAD(&loc_l->lists[i]);
628 loc_l->next_steal = cpu;
630 raw_spin_lock_init(&loc_l->lock);
633 static void bpf_lru_list_init(struct bpf_lru_list *l)
637 for (i = 0; i < NR_BPF_LRU_LIST_T; i++)
638 INIT_LIST_HEAD(&l->lists[i]);
640 for (i = 0; i < NR_BPF_LRU_LIST_COUNT; i++)
643 l->next_inactive_rotation = &l->lists[BPF_LRU_LIST_T_INACTIVE];
645 raw_spin_lock_init(&l->lock);
648 int bpf_lru_init(struct bpf_lru *lru, bool percpu, u32 hash_offset,
649 del_from_htab_func del_from_htab, void *del_arg)
654 lru->percpu_lru = alloc_percpu(struct bpf_lru_list);
655 if (!lru->percpu_lru)
658 for_each_possible_cpu(cpu) {
659 struct bpf_lru_list *l;
661 l = per_cpu_ptr(lru->percpu_lru, cpu);
662 bpf_lru_list_init(l);
664 lru->nr_scans = PERCPU_NR_SCANS;
666 struct bpf_common_lru *clru = &lru->common_lru;
668 clru->local_list = alloc_percpu(struct bpf_lru_locallist);
669 if (!clru->local_list)
672 for_each_possible_cpu(cpu) {
673 struct bpf_lru_locallist *loc_l;
675 loc_l = per_cpu_ptr(clru->local_list, cpu);
676 bpf_lru_locallist_init(loc_l, cpu);
679 bpf_lru_list_init(&clru->lru_list);
680 lru->nr_scans = LOCAL_NR_SCANS;
683 lru->percpu = percpu;
684 lru->del_from_htab = del_from_htab;
685 lru->del_arg = del_arg;
686 lru->hash_offset = hash_offset;
691 void bpf_lru_destroy(struct bpf_lru *lru)
694 free_percpu(lru->percpu_lru);
696 free_percpu(lru->common_lru.local_list);