bpf_lru_list.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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 (4)
  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;
  186. unsigned int nshrinked = 0;
  187. unsigned int i = 0;
  188. list_for_each_entry_safe_reverse(node, tmp_node, inactive, list) {
  189. if (bpf_lru_node_is_ref(node)) {
  190. __bpf_lru_node_move(l, node, BPF_LRU_LIST_T_ACTIVE);
  191. } else if (lru->del_from_htab(lru->del_arg, node)) {
  192. __bpf_lru_node_move_to_free(l, node, free_list,
  193. tgt_free_type);
  194. if (++nshrinked == tgt_nshrink)
  195. break;
  196. }
  197. if (++i == lru->nr_scans)
  198. break;
  199. }
  200. return nshrinked;
  201. }
  202. /* 1. Rotate the active list (if needed)
  203. * 2. Always rotate the inactive list
  204. */
  205. static void __bpf_lru_list_rotate(struct bpf_lru *lru, struct bpf_lru_list *l)
  206. {
  207. if (bpf_lru_list_inactive_low(l))
  208. __bpf_lru_list_rotate_active(lru, l);
  209. __bpf_lru_list_rotate_inactive(lru, l);
  210. }
  211. /* Calls __bpf_lru_list_shrink_inactive() to shrink some
  212. * ref-bit-cleared nodes and move them to the designated
  213. * free list.
  214. *
  215. * If it cannot get a free node after calling
  216. * __bpf_lru_list_shrink_inactive(). It will just remove
  217. * one node from either inactive or active list without
  218. * honoring the ref-bit. It prefers inactive list to active
  219. * list in this situation.
  220. */
  221. static unsigned int __bpf_lru_list_shrink(struct bpf_lru *lru,
  222. struct bpf_lru_list *l,
  223. unsigned int tgt_nshrink,
  224. struct list_head *free_list,
  225. enum bpf_lru_list_type tgt_free_type)
  226. {
  227. struct bpf_lru_node *node, *tmp_node;
  228. struct list_head *force_shrink_list;
  229. unsigned int nshrinked;
  230. nshrinked = __bpf_lru_list_shrink_inactive(lru, l, tgt_nshrink,
  231. free_list, tgt_free_type);
  232. if (nshrinked)
  233. return nshrinked;
  234. /* Do a force shrink by ignoring the reference bit */
  235. if (!list_empty(&l->lists[BPF_LRU_LIST_T_INACTIVE]))
  236. force_shrink_list = &l->lists[BPF_LRU_LIST_T_INACTIVE];
  237. else
  238. force_shrink_list = &l->lists[BPF_LRU_LIST_T_ACTIVE];
  239. list_for_each_entry_safe_reverse(node, tmp_node, force_shrink_list,
  240. list) {
  241. if (lru->del_from_htab(lru->del_arg, node)) {
  242. __bpf_lru_node_move_to_free(l, node, free_list,
  243. tgt_free_type);
  244. return 1;
  245. }
  246. }
  247. return 0;
  248. }
  249. /* Flush the nodes from the local pending list to the LRU list */
  250. static void __local_list_flush(struct bpf_lru_list *l,
  251. struct bpf_lru_locallist *loc_l)
  252. {
  253. struct bpf_lru_node *node, *tmp_node;
  254. list_for_each_entry_safe_reverse(node, tmp_node,
  255. local_pending_list(loc_l), list) {
  256. if (bpf_lru_node_is_ref(node))
  257. __bpf_lru_node_move_in(l, node, BPF_LRU_LIST_T_ACTIVE);
  258. else
  259. __bpf_lru_node_move_in(l, node,
  260. BPF_LRU_LIST_T_INACTIVE);
  261. }
  262. }
  263. static void bpf_lru_list_push_free(struct bpf_lru_list *l,
  264. struct bpf_lru_node *node)
  265. {
  266. unsigned long flags;
  267. if (WARN_ON_ONCE(IS_LOCAL_LIST_TYPE(node->type)))
  268. return;
  269. raw_spin_lock_irqsave(&l->lock, flags);
  270. __bpf_lru_node_move(l, node, BPF_LRU_LIST_T_FREE);
  271. raw_spin_unlock_irqrestore(&l->lock, flags);
  272. }
  273. static void bpf_lru_list_pop_free_to_local(struct bpf_lru *lru,
  274. struct bpf_lru_locallist *loc_l)
  275. {
  276. struct bpf_lru_list *l = &lru->common_lru.lru_list;
  277. struct bpf_lru_node *node, *tmp_node;
  278. unsigned int nfree = 0;
  279. raw_spin_lock(&l->lock);
  280. __local_list_flush(l, loc_l);
  281. __bpf_lru_list_rotate(lru, l);
  282. list_for_each_entry_safe(node, tmp_node, &l->lists[BPF_LRU_LIST_T_FREE],
  283. list) {
  284. __bpf_lru_node_move_to_free(l, node, local_free_list(loc_l),
  285. BPF_LRU_LOCAL_LIST_T_FREE);
  286. if (++nfree == LOCAL_FREE_TARGET)
  287. break;
  288. }
  289. if (nfree < LOCAL_FREE_TARGET)
  290. __bpf_lru_list_shrink(lru, l, LOCAL_FREE_TARGET - nfree,
  291. local_free_list(loc_l),
  292. BPF_LRU_LOCAL_LIST_T_FREE);
  293. raw_spin_unlock(&l->lock);
  294. }
  295. static void __local_list_add_pending(struct bpf_lru *lru,
  296. struct bpf_lru_locallist *loc_l,
  297. int cpu,
  298. struct bpf_lru_node *node,
  299. u32 hash)
  300. {
  301. *(u32 *)((void *)node + lru->hash_offset) = hash;
  302. node->cpu = cpu;
  303. node->type = BPF_LRU_LOCAL_LIST_T_PENDING;
  304. node->ref = 0;
  305. list_add(&node->list, local_pending_list(loc_l));
  306. }
  307. static struct bpf_lru_node *
  308. __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. static struct bpf_lru_node *
  319. __local_list_pop_pending(struct bpf_lru *lru, 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. u8 node_type = READ_ONCE(node->type);
  421. unsigned long flags;
  422. if (WARN_ON_ONCE(node_type == BPF_LRU_LIST_T_FREE) ||
  423. WARN_ON_ONCE(node_type == BPF_LRU_LOCAL_LIST_T_FREE))
  424. return;
  425. if (node_type == BPF_LRU_LOCAL_LIST_T_PENDING) {
  426. struct bpf_lru_locallist *loc_l;
  427. loc_l = per_cpu_ptr(lru->common_lru.local_list, node->cpu);
  428. raw_spin_lock_irqsave(&loc_l->lock, flags);
  429. if (unlikely(node->type != BPF_LRU_LOCAL_LIST_T_PENDING)) {
  430. raw_spin_unlock_irqrestore(&loc_l->lock, flags);
  431. goto check_lru_list;
  432. }
  433. node->type = BPF_LRU_LOCAL_LIST_T_FREE;
  434. node->ref = 0;
  435. list_move(&node->list, local_free_list(loc_l));
  436. raw_spin_unlock_irqrestore(&loc_l->lock, flags);
  437. return;
  438. }
  439. check_lru_list:
  440. bpf_lru_list_push_free(&lru->common_lru.lru_list, node);
  441. }
  442. static void bpf_percpu_lru_push_free(struct bpf_lru *lru,
  443. struct bpf_lru_node *node)
  444. {
  445. struct bpf_lru_list *l;
  446. unsigned long flags;
  447. l = per_cpu_ptr(lru->percpu_lru, node->cpu);
  448. raw_spin_lock_irqsave(&l->lock, flags);
  449. __bpf_lru_node_move(l, node, BPF_LRU_LIST_T_FREE);
  450. raw_spin_unlock_irqrestore(&l->lock, flags);
  451. }
  452. void bpf_lru_push_free(struct bpf_lru *lru, struct bpf_lru_node *node)
  453. {
  454. if (lru->percpu)
  455. bpf_percpu_lru_push_free(lru, node);
  456. else
  457. bpf_common_lru_push_free(lru, node);
  458. }
  459. static void bpf_common_lru_populate(struct bpf_lru *lru, void *buf,
  460. u32 node_offset, u32 elem_size,
  461. u32 nr_elems)
  462. {
  463. struct bpf_lru_list *l = &lru->common_lru.lru_list;
  464. u32 i;
  465. for (i = 0; i < nr_elems; i++) {
  466. struct bpf_lru_node *node;
  467. node = (struct bpf_lru_node *)(buf + node_offset);
  468. node->type = BPF_LRU_LIST_T_FREE;
  469. node->ref = 0;
  470. list_add(&node->list, &l->lists[BPF_LRU_LIST_T_FREE]);
  471. buf += elem_size;
  472. }
  473. }
  474. static void bpf_percpu_lru_populate(struct bpf_lru *lru, void *buf,
  475. u32 node_offset, u32 elem_size,
  476. u32 nr_elems)
  477. {
  478. u32 i, pcpu_entries;
  479. int cpu;
  480. struct bpf_lru_list *l;
  481. pcpu_entries = nr_elems / num_possible_cpus();
  482. i = 0;
  483. for_each_possible_cpu(cpu) {
  484. struct bpf_lru_node *node;
  485. l = per_cpu_ptr(lru->percpu_lru, cpu);
  486. again:
  487. node = (struct bpf_lru_node *)(buf + node_offset);
  488. node->cpu = cpu;
  489. node->type = BPF_LRU_LIST_T_FREE;
  490. node->ref = 0;
  491. list_add(&node->list, &l->lists[BPF_LRU_LIST_T_FREE]);
  492. i++;
  493. buf += elem_size;
  494. if (i == nr_elems)
  495. break;
  496. if (i % pcpu_entries)
  497. goto again;
  498. }
  499. }
  500. void bpf_lru_populate(struct bpf_lru *lru, void *buf, u32 node_offset,
  501. u32 elem_size, u32 nr_elems)
  502. {
  503. if (lru->percpu)
  504. bpf_percpu_lru_populate(lru, buf, node_offset, elem_size,
  505. nr_elems);
  506. else
  507. bpf_common_lru_populate(lru, buf, node_offset, elem_size,
  508. nr_elems);
  509. }
  510. static void bpf_lru_locallist_init(struct bpf_lru_locallist *loc_l, int cpu)
  511. {
  512. int i;
  513. for (i = 0; i < NR_BPF_LRU_LOCAL_LIST_T; i++)
  514. INIT_LIST_HEAD(&loc_l->lists[i]);
  515. loc_l->next_steal = cpu;
  516. raw_spin_lock_init(&loc_l->lock);
  517. }
  518. static void bpf_lru_list_init(struct bpf_lru_list *l)
  519. {
  520. int i;
  521. for (i = 0; i < NR_BPF_LRU_LIST_T; i++)
  522. INIT_LIST_HEAD(&l->lists[i]);
  523. for (i = 0; i < NR_BPF_LRU_LIST_COUNT; i++)
  524. l->counts[i] = 0;
  525. l->next_inactive_rotation = &l->lists[BPF_LRU_LIST_T_INACTIVE];
  526. raw_spin_lock_init(&l->lock);
  527. }
  528. int bpf_lru_init(struct bpf_lru *lru, bool percpu, u32 hash_offset,
  529. del_from_htab_func del_from_htab, void *del_arg)
  530. {
  531. int cpu;
  532. if (percpu) {
  533. lru->percpu_lru = alloc_percpu(struct bpf_lru_list);
  534. if (!lru->percpu_lru)
  535. return -ENOMEM;
  536. for_each_possible_cpu(cpu) {
  537. struct bpf_lru_list *l;
  538. l = per_cpu_ptr(lru->percpu_lru, cpu);
  539. bpf_lru_list_init(l);
  540. }
  541. lru->nr_scans = PERCPU_NR_SCANS;
  542. } else {
  543. struct bpf_common_lru *clru = &lru->common_lru;
  544. clru->local_list = alloc_percpu(struct bpf_lru_locallist);
  545. if (!clru->local_list)
  546. return -ENOMEM;
  547. for_each_possible_cpu(cpu) {
  548. struct bpf_lru_locallist *loc_l;
  549. loc_l = per_cpu_ptr(clru->local_list, cpu);
  550. bpf_lru_locallist_init(loc_l, cpu);
  551. }
  552. bpf_lru_list_init(&clru->lru_list);
  553. lru->nr_scans = LOCAL_NR_SCANS;
  554. }
  555. lru->percpu = percpu;
  556. lru->del_from_htab = del_from_htab;
  557. lru->del_arg = del_arg;
  558. lru->hash_offset = hash_offset;
  559. return 0;
  560. }
  561. void bpf_lru_destroy(struct bpf_lru *lru)
  562. {
  563. if (lru->percpu)
  564. free_percpu(lru->percpu_lru);
  565. else
  566. free_percpu(lru->common_lru.local_list);
  567. }