bpf_lru_list.c 18 KB

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