bpf_lru_list.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2016 Facebook
  3. */
  4. #include <linux/cpumask.h>
  5. #include <linux/spinlock.h>
  6. #include <linux/percpu.h>
  7. #include "bpf_lru_list.h"
  8. #define LOCAL_FREE_TARGET (128)
  9. #define LOCAL_NR_SCANS LOCAL_FREE_TARGET
  10. #define PERCPU_FREE_TARGET (4)
  11. #define PERCPU_NR_SCANS PERCPU_FREE_TARGET
  12. /* Helpers to get the local list index */
  13. #define LOCAL_LIST_IDX(t) ((t) - BPF_LOCAL_LIST_T_OFFSET)
  14. #define LOCAL_FREE_LIST_IDX LOCAL_LIST_IDX(BPF_LRU_LOCAL_LIST_T_FREE)
  15. #define LOCAL_PENDING_LIST_IDX LOCAL_LIST_IDX(BPF_LRU_LOCAL_LIST_T_PENDING)
  16. #define IS_LOCAL_LIST_TYPE(t) ((t) >= BPF_LOCAL_LIST_T_OFFSET)
  17. static int get_next_cpu(int cpu)
  18. {
  19. cpu = cpumask_next(cpu, cpu_possible_mask);
  20. if (cpu >= nr_cpu_ids)
  21. cpu = cpumask_first(cpu_possible_mask);
  22. return cpu;
  23. }
  24. /* Local list helpers */
  25. static struct list_head *local_free_list(struct bpf_lru_locallist *loc_l)
  26. {
  27. return &loc_l->lists[LOCAL_FREE_LIST_IDX];
  28. }
  29. static struct list_head *local_pending_list(struct bpf_lru_locallist *loc_l)
  30. {
  31. return &loc_l->lists[LOCAL_PENDING_LIST_IDX];
  32. }
  33. /* bpf_lru_node helpers */
  34. static bool bpf_lru_node_is_ref(const struct bpf_lru_node *node)
  35. {
  36. return READ_ONCE(node->ref);
  37. }
  38. static void bpf_lru_node_clear_ref(struct bpf_lru_node *node)
  39. {
  40. WRITE_ONCE(node->ref, 0);
  41. }
  42. static void bpf_lru_list_count_inc(struct bpf_lru_list *l,
  43. enum bpf_lru_list_type type)
  44. {
  45. if (type < NR_BPF_LRU_LIST_COUNT)
  46. l->counts[type]++;
  47. }
  48. static void bpf_lru_list_count_dec(struct bpf_lru_list *l,
  49. enum bpf_lru_list_type type)
  50. {
  51. if (type < NR_BPF_LRU_LIST_COUNT)
  52. l->counts[type]--;
  53. }
  54. static void __bpf_lru_node_move_to_free(struct bpf_lru_list *l,
  55. struct bpf_lru_node *node,
  56. struct list_head *free_list,
  57. enum bpf_lru_list_type tgt_free_type)
  58. {
  59. if (WARN_ON_ONCE(IS_LOCAL_LIST_TYPE(node->type)))
  60. return;
  61. /* If the removing node is the next_inactive_rotation candidate,
  62. * move the next_inactive_rotation pointer also.
  63. */
  64. if (&node->list == l->next_inactive_rotation)
  65. l->next_inactive_rotation = l->next_inactive_rotation->prev;
  66. bpf_lru_list_count_dec(l, node->type);
  67. node->type = tgt_free_type;
  68. list_move(&node->list, free_list);
  69. }
  70. /* Move nodes from local list to the LRU list */
  71. static void __bpf_lru_node_move_in(struct bpf_lru_list *l,
  72. struct bpf_lru_node *node,
  73. enum bpf_lru_list_type tgt_type)
  74. {
  75. if (WARN_ON_ONCE(!IS_LOCAL_LIST_TYPE(node->type)) ||
  76. WARN_ON_ONCE(IS_LOCAL_LIST_TYPE(tgt_type)))
  77. return;
  78. bpf_lru_list_count_inc(l, tgt_type);
  79. node->type = tgt_type;
  80. bpf_lru_node_clear_ref(node);
  81. list_move(&node->list, &l->lists[tgt_type]);
  82. }
  83. /* Move nodes between or within active and inactive list (like
  84. * active to inactive, inactive to active or tail of active back to
  85. * the head of active).
  86. */
  87. static void __bpf_lru_node_move(struct bpf_lru_list *l,
  88. struct bpf_lru_node *node,
  89. enum bpf_lru_list_type tgt_type)
  90. {
  91. if (WARN_ON_ONCE(IS_LOCAL_LIST_TYPE(node->type)) ||
  92. WARN_ON_ONCE(IS_LOCAL_LIST_TYPE(tgt_type)))
  93. return;
  94. if (node->type != tgt_type) {
  95. bpf_lru_list_count_dec(l, node->type);
  96. bpf_lru_list_count_inc(l, tgt_type);
  97. node->type = tgt_type;
  98. }
  99. bpf_lru_node_clear_ref(node);
  100. /* If the moving node is the next_inactive_rotation candidate,
  101. * move the next_inactive_rotation pointer also.
  102. */
  103. if (&node->list == l->next_inactive_rotation)
  104. l->next_inactive_rotation = l->next_inactive_rotation->prev;
  105. list_move(&node->list, &l->lists[tgt_type]);
  106. }
  107. static bool bpf_lru_list_inactive_low(const struct bpf_lru_list *l)
  108. {
  109. return l->counts[BPF_LRU_LIST_T_INACTIVE] <
  110. l->counts[BPF_LRU_LIST_T_ACTIVE];
  111. }
  112. /* Rotate the active list:
  113. * 1. Start from tail
  114. * 2. If the node has the ref bit set, it will be rotated
  115. * back to the head of active list with the ref bit cleared.
  116. * Give this node one more chance to survive in the active list.
  117. * 3. If the ref bit is not set, move it to the head of the
  118. * inactive list.
  119. * 4. It will at most scan nr_scans nodes
  120. */
  121. static void __bpf_lru_list_rotate_active(struct bpf_lru *lru,
  122. struct bpf_lru_list *l)
  123. {
  124. struct list_head *active = &l->lists[BPF_LRU_LIST_T_ACTIVE];
  125. struct bpf_lru_node *node, *tmp_node, *first_node;
  126. unsigned int i = 0;
  127. first_node = list_first_entry(active, struct bpf_lru_node, list);
  128. list_for_each_entry_safe_reverse(node, tmp_node, active, list) {
  129. if (bpf_lru_node_is_ref(node))
  130. __bpf_lru_node_move(l, node, BPF_LRU_LIST_T_ACTIVE);
  131. else
  132. __bpf_lru_node_move(l, node, BPF_LRU_LIST_T_INACTIVE);
  133. if (++i == lru->nr_scans || node == first_node)
  134. break;
  135. }
  136. }
  137. /* Rotate the inactive list. It starts from the next_inactive_rotation
  138. * 1. If the node has ref bit set, it will be moved to the head
  139. * of active list with the ref bit cleared.
  140. * 2. If the node does not have ref bit set, it will leave it
  141. * at its current location (i.e. do nothing) so that it can
  142. * be considered during the next inactive_shrink.
  143. * 3. It will at most scan nr_scans nodes
  144. */
  145. static void __bpf_lru_list_rotate_inactive(struct bpf_lru *lru,
  146. struct bpf_lru_list *l)
  147. {
  148. struct list_head *inactive = &l->lists[BPF_LRU_LIST_T_INACTIVE];
  149. struct list_head *cur, *last, *next = inactive;
  150. struct bpf_lru_node *node;
  151. unsigned int i = 0;
  152. if (list_empty(inactive))
  153. return;
  154. last = l->next_inactive_rotation->next;
  155. if (last == inactive)
  156. last = last->next;
  157. cur = l->next_inactive_rotation;
  158. while (i < lru->nr_scans) {
  159. if (cur == inactive) {
  160. cur = cur->prev;
  161. continue;
  162. }
  163. node = list_entry(cur, struct bpf_lru_node, list);
  164. next = cur->prev;
  165. if (bpf_lru_node_is_ref(node))
  166. __bpf_lru_node_move(l, node, BPF_LRU_LIST_T_ACTIVE);
  167. if (cur == last)
  168. break;
  169. cur = next;
  170. i++;
  171. }
  172. l->next_inactive_rotation = next;
  173. }
  174. /* Shrink the inactive list. It starts from the tail of the
  175. * inactive list and only move the nodes without the ref bit
  176. * set to the designated free list.
  177. */
  178. static unsigned int
  179. __bpf_lru_list_shrink_inactive(struct bpf_lru *lru,
  180. struct bpf_lru_list *l,
  181. unsigned int tgt_nshrink,
  182. struct list_head *free_list,
  183. enum bpf_lru_list_type tgt_free_type)
  184. {
  185. struct list_head *inactive = &l->lists[BPF_LRU_LIST_T_INACTIVE];
  186. struct bpf_lru_node *node, *tmp_node;
  187. unsigned int nshrinked = 0;
  188. unsigned int i = 0;
  189. list_for_each_entry_safe_reverse(node, tmp_node, inactive, list) {
  190. if (bpf_lru_node_is_ref(node)) {
  191. __bpf_lru_node_move(l, node, BPF_LRU_LIST_T_ACTIVE);
  192. } else if (lru->del_from_htab(lru->del_arg, node)) {
  193. __bpf_lru_node_move_to_free(l, node, free_list,
  194. tgt_free_type);
  195. if (++nshrinked == tgt_nshrink)
  196. break;
  197. }
  198. if (++i == lru->nr_scans)
  199. break;
  200. }
  201. return nshrinked;
  202. }
  203. /* 1. Rotate the active list (if needed)
  204. * 2. Always rotate the inactive list
  205. */
  206. static void __bpf_lru_list_rotate(struct bpf_lru *lru, struct bpf_lru_list *l)
  207. {
  208. if (bpf_lru_list_inactive_low(l))
  209. __bpf_lru_list_rotate_active(lru, l);
  210. __bpf_lru_list_rotate_inactive(lru, l);
  211. }
  212. /* Calls __bpf_lru_list_shrink_inactive() to shrink some
  213. * ref-bit-cleared nodes and move them to the designated
  214. * free list.
  215. *
  216. * If it cannot get a free node after calling
  217. * __bpf_lru_list_shrink_inactive(). It will just remove
  218. * one node from either inactive or active list without
  219. * honoring the ref-bit. It prefers inactive list to active
  220. * list in this situation.
  221. */
  222. static unsigned int __bpf_lru_list_shrink(struct bpf_lru *lru,
  223. struct bpf_lru_list *l,
  224. unsigned int tgt_nshrink,
  225. struct list_head *free_list,
  226. enum bpf_lru_list_type tgt_free_type)
  227. {
  228. struct bpf_lru_node *node, *tmp_node;
  229. struct list_head *force_shrink_list;
  230. unsigned int nshrinked;
  231. nshrinked = __bpf_lru_list_shrink_inactive(lru, l, tgt_nshrink,
  232. free_list, tgt_free_type);
  233. if (nshrinked)
  234. return nshrinked;
  235. /* Do a force shrink by ignoring the reference bit */
  236. if (!list_empty(&l->lists[BPF_LRU_LIST_T_INACTIVE]))
  237. force_shrink_list = &l->lists[BPF_LRU_LIST_T_INACTIVE];
  238. else
  239. force_shrink_list = &l->lists[BPF_LRU_LIST_T_ACTIVE];
  240. list_for_each_entry_safe_reverse(node, tmp_node, force_shrink_list,
  241. list) {
  242. if (lru->del_from_htab(lru->del_arg, node)) {
  243. __bpf_lru_node_move_to_free(l, node, free_list,
  244. tgt_free_type);
  245. return 1;
  246. }
  247. }
  248. return 0;
  249. }
  250. /* Flush the nodes from the local pending list to the LRU list */
  251. static void __local_list_flush(struct bpf_lru_list *l,
  252. struct bpf_lru_locallist *loc_l)
  253. {
  254. struct bpf_lru_node *node, *tmp_node;
  255. list_for_each_entry_safe_reverse(node, tmp_node,
  256. local_pending_list(loc_l), list) {
  257. if (bpf_lru_node_is_ref(node))
  258. __bpf_lru_node_move_in(l, node, BPF_LRU_LIST_T_ACTIVE);
  259. else
  260. __bpf_lru_node_move_in(l, node,
  261. BPF_LRU_LIST_T_INACTIVE);
  262. }
  263. }
  264. static void bpf_lru_list_push_free(struct bpf_lru_list *l,
  265. struct bpf_lru_node *node)
  266. {
  267. unsigned long flags;
  268. if (WARN_ON_ONCE(IS_LOCAL_LIST_TYPE(node->type)))
  269. return;
  270. raw_spin_lock_irqsave(&l->lock, flags);
  271. __bpf_lru_node_move(l, node, BPF_LRU_LIST_T_FREE);
  272. raw_spin_unlock_irqrestore(&l->lock, flags);
  273. }
  274. static void bpf_lru_list_pop_free_to_local(struct bpf_lru *lru,
  275. struct bpf_lru_locallist *loc_l)
  276. {
  277. struct bpf_lru_list *l = &lru->common_lru.lru_list;
  278. struct bpf_lru_node *node, *tmp_node;
  279. unsigned int nfree = 0;
  280. raw_spin_lock(&l->lock);
  281. __local_list_flush(l, loc_l);
  282. __bpf_lru_list_rotate(lru, l);
  283. list_for_each_entry_safe(node, tmp_node, &l->lists[BPF_LRU_LIST_T_FREE],
  284. list) {
  285. __bpf_lru_node_move_to_free(l, node, local_free_list(loc_l),
  286. BPF_LRU_LOCAL_LIST_T_FREE);
  287. if (++nfree == lru->target_free)
  288. break;
  289. }
  290. if (nfree < lru->target_free)
  291. __bpf_lru_list_shrink(lru, l, lru->target_free - nfree,
  292. local_free_list(loc_l),
  293. BPF_LRU_LOCAL_LIST_T_FREE);
  294. raw_spin_unlock(&l->lock);
  295. }
  296. static void __local_list_add_pending(struct bpf_lru *lru,
  297. struct bpf_lru_locallist *loc_l,
  298. int cpu,
  299. struct bpf_lru_node *node,
  300. u32 hash)
  301. {
  302. *(u32 *)((void *)node + lru->hash_offset) = hash;
  303. node->cpu = cpu;
  304. node->type = BPF_LRU_LOCAL_LIST_T_PENDING;
  305. bpf_lru_node_clear_ref(node);
  306. list_add(&node->list, local_pending_list(loc_l));
  307. }
  308. static struct bpf_lru_node *
  309. __local_list_pop_free(struct bpf_lru_locallist *loc_l)
  310. {
  311. struct bpf_lru_node *node;
  312. node = list_first_entry_or_null(local_free_list(loc_l),
  313. struct bpf_lru_node,
  314. list);
  315. if (node)
  316. list_del(&node->list);
  317. return node;
  318. }
  319. static struct bpf_lru_node *
  320. __local_list_pop_pending(struct bpf_lru *lru, struct bpf_lru_locallist *loc_l)
  321. {
  322. struct bpf_lru_node *node;
  323. bool force = false;
  324. ignore_ref:
  325. /* Get from the tail (i.e. older element) of the pending list. */
  326. list_for_each_entry_reverse(node, local_pending_list(loc_l),
  327. list) {
  328. if ((!bpf_lru_node_is_ref(node) || force) &&
  329. lru->del_from_htab(lru->del_arg, node)) {
  330. list_del(&node->list);
  331. return node;
  332. }
  333. }
  334. if (!force) {
  335. force = true;
  336. goto ignore_ref;
  337. }
  338. return NULL;
  339. }
  340. static struct bpf_lru_node *bpf_percpu_lru_pop_free(struct bpf_lru *lru,
  341. u32 hash)
  342. {
  343. struct list_head *free_list;
  344. struct bpf_lru_node *node = NULL;
  345. struct bpf_lru_list *l;
  346. unsigned long flags;
  347. int cpu = raw_smp_processor_id();
  348. l = per_cpu_ptr(lru->percpu_lru, cpu);
  349. raw_spin_lock_irqsave(&l->lock, flags);
  350. __bpf_lru_list_rotate(lru, l);
  351. free_list = &l->lists[BPF_LRU_LIST_T_FREE];
  352. if (list_empty(free_list))
  353. __bpf_lru_list_shrink(lru, l, PERCPU_FREE_TARGET, free_list,
  354. BPF_LRU_LIST_T_FREE);
  355. if (!list_empty(free_list)) {
  356. node = list_first_entry(free_list, struct bpf_lru_node, list);
  357. *(u32 *)((void *)node + lru->hash_offset) = hash;
  358. bpf_lru_node_clear_ref(node);
  359. __bpf_lru_node_move(l, node, BPF_LRU_LIST_T_INACTIVE);
  360. }
  361. raw_spin_unlock_irqrestore(&l->lock, flags);
  362. return node;
  363. }
  364. static struct bpf_lru_node *bpf_common_lru_pop_free(struct bpf_lru *lru,
  365. u32 hash)
  366. {
  367. struct bpf_lru_locallist *loc_l, *steal_loc_l;
  368. struct bpf_common_lru *clru = &lru->common_lru;
  369. struct bpf_lru_node *node;
  370. int steal, first_steal;
  371. unsigned long flags;
  372. int cpu = raw_smp_processor_id();
  373. loc_l = per_cpu_ptr(clru->local_list, cpu);
  374. raw_spin_lock_irqsave(&loc_l->lock, flags);
  375. node = __local_list_pop_free(loc_l);
  376. if (!node) {
  377. bpf_lru_list_pop_free_to_local(lru, loc_l);
  378. node = __local_list_pop_free(loc_l);
  379. }
  380. if (node)
  381. __local_list_add_pending(lru, loc_l, cpu, node, hash);
  382. raw_spin_unlock_irqrestore(&loc_l->lock, flags);
  383. if (node)
  384. return node;
  385. /* No free nodes found from the local free list and
  386. * the global LRU list.
  387. *
  388. * Steal from the local free/pending list of the
  389. * current CPU and remote CPU in RR. It starts
  390. * with the loc_l->next_steal CPU.
  391. */
  392. first_steal = loc_l->next_steal;
  393. steal = first_steal;
  394. do {
  395. steal_loc_l = per_cpu_ptr(clru->local_list, steal);
  396. raw_spin_lock_irqsave(&steal_loc_l->lock, flags);
  397. node = __local_list_pop_free(steal_loc_l);
  398. if (!node)
  399. node = __local_list_pop_pending(lru, steal_loc_l);
  400. raw_spin_unlock_irqrestore(&steal_loc_l->lock, flags);
  401. steal = get_next_cpu(steal);
  402. } while (!node && steal != first_steal);
  403. loc_l->next_steal = steal;
  404. if (node) {
  405. raw_spin_lock_irqsave(&loc_l->lock, flags);
  406. __local_list_add_pending(lru, loc_l, cpu, node, hash);
  407. raw_spin_unlock_irqrestore(&loc_l->lock, flags);
  408. }
  409. return node;
  410. }
  411. struct bpf_lru_node *bpf_lru_pop_free(struct bpf_lru *lru, u32 hash)
  412. {
  413. if (lru->percpu)
  414. return bpf_percpu_lru_pop_free(lru, hash);
  415. else
  416. return bpf_common_lru_pop_free(lru, hash);
  417. }
  418. static void bpf_common_lru_push_free(struct bpf_lru *lru,
  419. struct bpf_lru_node *node)
  420. {
  421. u8 node_type = READ_ONCE(node->type);
  422. unsigned long flags;
  423. if (WARN_ON_ONCE(node_type == BPF_LRU_LIST_T_FREE) ||
  424. WARN_ON_ONCE(node_type == BPF_LRU_LOCAL_LIST_T_FREE))
  425. return;
  426. if (node_type == BPF_LRU_LOCAL_LIST_T_PENDING) {
  427. struct bpf_lru_locallist *loc_l;
  428. loc_l = per_cpu_ptr(lru->common_lru.local_list, node->cpu);
  429. raw_spin_lock_irqsave(&loc_l->lock, flags);
  430. if (unlikely(node->type != BPF_LRU_LOCAL_LIST_T_PENDING)) {
  431. raw_spin_unlock_irqrestore(&loc_l->lock, flags);
  432. goto check_lru_list;
  433. }
  434. node->type = BPF_LRU_LOCAL_LIST_T_FREE;
  435. bpf_lru_node_clear_ref(node);
  436. list_move(&node->list, local_free_list(loc_l));
  437. raw_spin_unlock_irqrestore(&loc_l->lock, flags);
  438. return;
  439. }
  440. check_lru_list:
  441. bpf_lru_list_push_free(&lru->common_lru.lru_list, node);
  442. }
  443. static void bpf_percpu_lru_push_free(struct bpf_lru *lru,
  444. struct bpf_lru_node *node)
  445. {
  446. struct bpf_lru_list *l;
  447. unsigned long flags;
  448. l = per_cpu_ptr(lru->percpu_lru, node->cpu);
  449. raw_spin_lock_irqsave(&l->lock, flags);
  450. __bpf_lru_node_move(l, node, BPF_LRU_LIST_T_FREE);
  451. raw_spin_unlock_irqrestore(&l->lock, flags);
  452. }
  453. void bpf_lru_push_free(struct bpf_lru *lru, struct bpf_lru_node *node)
  454. {
  455. if (lru->percpu)
  456. bpf_percpu_lru_push_free(lru, node);
  457. else
  458. bpf_common_lru_push_free(lru, node);
  459. }
  460. static void bpf_common_lru_populate(struct bpf_lru *lru, void *buf,
  461. u32 node_offset, u32 elem_size,
  462. u32 nr_elems)
  463. {
  464. struct bpf_lru_list *l = &lru->common_lru.lru_list;
  465. u32 i;
  466. for (i = 0; i < nr_elems; i++) {
  467. struct bpf_lru_node *node;
  468. node = (struct bpf_lru_node *)(buf + node_offset);
  469. node->type = BPF_LRU_LIST_T_FREE;
  470. bpf_lru_node_clear_ref(node);
  471. list_add(&node->list, &l->lists[BPF_LRU_LIST_T_FREE]);
  472. buf += elem_size;
  473. }
  474. lru->target_free = clamp((nr_elems / num_possible_cpus()) / 2,
  475. 1, LOCAL_FREE_TARGET);
  476. }
  477. static void bpf_percpu_lru_populate(struct bpf_lru *lru, void *buf,
  478. u32 node_offset, u32 elem_size,
  479. u32 nr_elems)
  480. {
  481. u32 i, pcpu_entries;
  482. int cpu;
  483. struct bpf_lru_list *l;
  484. pcpu_entries = nr_elems / num_possible_cpus();
  485. i = 0;
  486. for_each_possible_cpu(cpu) {
  487. struct bpf_lru_node *node;
  488. l = per_cpu_ptr(lru->percpu_lru, cpu);
  489. again:
  490. node = (struct bpf_lru_node *)(buf + node_offset);
  491. node->cpu = cpu;
  492. node->type = BPF_LRU_LIST_T_FREE;
  493. bpf_lru_node_clear_ref(node);
  494. list_add(&node->list, &l->lists[BPF_LRU_LIST_T_FREE]);
  495. i++;
  496. buf += elem_size;
  497. if (i == nr_elems)
  498. break;
  499. if (i % pcpu_entries)
  500. goto again;
  501. }
  502. }
  503. void bpf_lru_populate(struct bpf_lru *lru, void *buf, u32 node_offset,
  504. u32 elem_size, u32 nr_elems)
  505. {
  506. if (lru->percpu)
  507. bpf_percpu_lru_populate(lru, buf, node_offset, elem_size,
  508. nr_elems);
  509. else
  510. bpf_common_lru_populate(lru, buf, node_offset, elem_size,
  511. nr_elems);
  512. }
  513. static void bpf_lru_locallist_init(struct bpf_lru_locallist *loc_l, int cpu)
  514. {
  515. int i;
  516. for (i = 0; i < NR_BPF_LRU_LOCAL_LIST_T; i++)
  517. INIT_LIST_HEAD(&loc_l->lists[i]);
  518. loc_l->next_steal = cpu;
  519. raw_spin_lock_init(&loc_l->lock);
  520. }
  521. static void bpf_lru_list_init(struct bpf_lru_list *l)
  522. {
  523. int i;
  524. for (i = 0; i < NR_BPF_LRU_LIST_T; i++)
  525. INIT_LIST_HEAD(&l->lists[i]);
  526. for (i = 0; i < NR_BPF_LRU_LIST_COUNT; i++)
  527. l->counts[i] = 0;
  528. l->next_inactive_rotation = &l->lists[BPF_LRU_LIST_T_INACTIVE];
  529. raw_spin_lock_init(&l->lock);
  530. }
  531. int bpf_lru_init(struct bpf_lru *lru, bool percpu, u32 hash_offset,
  532. del_from_htab_func del_from_htab, void *del_arg)
  533. {
  534. int cpu;
  535. if (percpu) {
  536. lru->percpu_lru = alloc_percpu(struct bpf_lru_list);
  537. if (!lru->percpu_lru)
  538. return -ENOMEM;
  539. for_each_possible_cpu(cpu) {
  540. struct bpf_lru_list *l;
  541. l = per_cpu_ptr(lru->percpu_lru, cpu);
  542. bpf_lru_list_init(l);
  543. }
  544. lru->nr_scans = PERCPU_NR_SCANS;
  545. } else {
  546. struct bpf_common_lru *clru = &lru->common_lru;
  547. clru->local_list = alloc_percpu(struct bpf_lru_locallist);
  548. if (!clru->local_list)
  549. return -ENOMEM;
  550. for_each_possible_cpu(cpu) {
  551. struct bpf_lru_locallist *loc_l;
  552. loc_l = per_cpu_ptr(clru->local_list, cpu);
  553. bpf_lru_locallist_init(loc_l, cpu);
  554. }
  555. bpf_lru_list_init(&clru->lru_list);
  556. lru->nr_scans = LOCAL_NR_SCANS;
  557. }
  558. lru->percpu = percpu;
  559. lru->del_from_htab = del_from_htab;
  560. lru->del_arg = del_arg;
  561. lru->hash_offset = hash_offset;
  562. return 0;
  563. }
  564. void bpf_lru_destroy(struct bpf_lru *lru)
  565. {
  566. if (lru->percpu)
  567. free_percpu(lru->percpu_lru);
  568. else
  569. free_percpu(lru->common_lru.local_list);
  570. }