list_lru.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2013 Red Hat, Inc. and Parallels Inc. All rights reserved.
  4. * Authors: David Chinner and Glauber Costa
  5. *
  6. * Generic LRU infrastructure
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/mm.h>
  11. #include <linux/list_lru.h>
  12. #include <linux/slab.h>
  13. #include <linux/mutex.h>
  14. #include <linux/memcontrol.h>
  15. #include "slab.h"
  16. #include "internal.h"
  17. #ifdef CONFIG_MEMCG
  18. static LIST_HEAD(memcg_list_lrus);
  19. static DEFINE_MUTEX(list_lrus_mutex);
  20. static inline bool list_lru_memcg_aware(struct list_lru *lru)
  21. {
  22. return lru->memcg_aware;
  23. }
  24. static void list_lru_register(struct list_lru *lru)
  25. {
  26. if (!list_lru_memcg_aware(lru))
  27. return;
  28. mutex_lock(&list_lrus_mutex);
  29. list_add(&lru->list, &memcg_list_lrus);
  30. mutex_unlock(&list_lrus_mutex);
  31. }
  32. static void list_lru_unregister(struct list_lru *lru)
  33. {
  34. if (!list_lru_memcg_aware(lru))
  35. return;
  36. mutex_lock(&list_lrus_mutex);
  37. list_del(&lru->list);
  38. mutex_unlock(&list_lrus_mutex);
  39. }
  40. static int lru_shrinker_id(struct list_lru *lru)
  41. {
  42. return lru->shrinker_id;
  43. }
  44. static inline struct list_lru_one *
  45. list_lru_from_memcg_idx(struct list_lru *lru, int nid, int idx)
  46. {
  47. if (list_lru_memcg_aware(lru) && idx >= 0) {
  48. struct list_lru_memcg *mlru = xa_load(&lru->xa, idx);
  49. return mlru ? &mlru->node[nid] : NULL;
  50. }
  51. return &lru->node[nid].lru;
  52. }
  53. #else
  54. static void list_lru_register(struct list_lru *lru)
  55. {
  56. }
  57. static void list_lru_unregister(struct list_lru *lru)
  58. {
  59. }
  60. static int lru_shrinker_id(struct list_lru *lru)
  61. {
  62. return -1;
  63. }
  64. static inline bool list_lru_memcg_aware(struct list_lru *lru)
  65. {
  66. return false;
  67. }
  68. static inline struct list_lru_one *
  69. list_lru_from_memcg_idx(struct list_lru *lru, int nid, int idx)
  70. {
  71. return &lru->node[nid].lru;
  72. }
  73. #endif /* CONFIG_MEMCG */
  74. /* The caller must ensure the memcg lifetime. */
  75. bool list_lru_add(struct list_lru *lru, struct list_head *item, int nid,
  76. struct mem_cgroup *memcg)
  77. {
  78. struct list_lru_node *nlru = &lru->node[nid];
  79. struct list_lru_one *l;
  80. spin_lock(&nlru->lock);
  81. if (list_empty(item)) {
  82. l = list_lru_from_memcg_idx(lru, nid, memcg_kmem_id(memcg));
  83. list_add_tail(item, &l->list);
  84. /* Set shrinker bit if the first element was added */
  85. if (!l->nr_items++)
  86. set_shrinker_bit(memcg, nid, lru_shrinker_id(lru));
  87. nlru->nr_items++;
  88. spin_unlock(&nlru->lock);
  89. return true;
  90. }
  91. spin_unlock(&nlru->lock);
  92. return false;
  93. }
  94. EXPORT_SYMBOL_GPL(list_lru_add);
  95. bool list_lru_add_obj(struct list_lru *lru, struct list_head *item)
  96. {
  97. bool ret;
  98. int nid = page_to_nid(virt_to_page(item));
  99. if (list_lru_memcg_aware(lru)) {
  100. rcu_read_lock();
  101. ret = list_lru_add(lru, item, nid, mem_cgroup_from_slab_obj(item));
  102. rcu_read_unlock();
  103. } else {
  104. ret = list_lru_add(lru, item, nid, NULL);
  105. }
  106. return ret;
  107. }
  108. EXPORT_SYMBOL_GPL(list_lru_add_obj);
  109. /* The caller must ensure the memcg lifetime. */
  110. bool list_lru_del(struct list_lru *lru, struct list_head *item, int nid,
  111. struct mem_cgroup *memcg)
  112. {
  113. struct list_lru_node *nlru = &lru->node[nid];
  114. struct list_lru_one *l;
  115. spin_lock(&nlru->lock);
  116. if (!list_empty(item)) {
  117. l = list_lru_from_memcg_idx(lru, nid, memcg_kmem_id(memcg));
  118. list_del_init(item);
  119. l->nr_items--;
  120. nlru->nr_items--;
  121. spin_unlock(&nlru->lock);
  122. return true;
  123. }
  124. spin_unlock(&nlru->lock);
  125. return false;
  126. }
  127. EXPORT_SYMBOL_GPL(list_lru_del);
  128. bool list_lru_del_obj(struct list_lru *lru, struct list_head *item)
  129. {
  130. bool ret;
  131. int nid = page_to_nid(virt_to_page(item));
  132. if (list_lru_memcg_aware(lru)) {
  133. rcu_read_lock();
  134. ret = list_lru_del(lru, item, nid, mem_cgroup_from_slab_obj(item));
  135. rcu_read_unlock();
  136. } else {
  137. ret = list_lru_del(lru, item, nid, NULL);
  138. }
  139. return ret;
  140. }
  141. EXPORT_SYMBOL_GPL(list_lru_del_obj);
  142. void list_lru_isolate(struct list_lru_one *list, struct list_head *item)
  143. {
  144. list_del_init(item);
  145. list->nr_items--;
  146. }
  147. EXPORT_SYMBOL_GPL(list_lru_isolate);
  148. void list_lru_isolate_move(struct list_lru_one *list, struct list_head *item,
  149. struct list_head *head)
  150. {
  151. list_move(item, head);
  152. list->nr_items--;
  153. }
  154. EXPORT_SYMBOL_GPL(list_lru_isolate_move);
  155. unsigned long list_lru_count_one(struct list_lru *lru,
  156. int nid, struct mem_cgroup *memcg)
  157. {
  158. struct list_lru_one *l;
  159. long count;
  160. rcu_read_lock();
  161. l = list_lru_from_memcg_idx(lru, nid, memcg_kmem_id(memcg));
  162. count = l ? READ_ONCE(l->nr_items) : 0;
  163. rcu_read_unlock();
  164. if (unlikely(count < 0))
  165. count = 0;
  166. return count;
  167. }
  168. EXPORT_SYMBOL_GPL(list_lru_count_one);
  169. unsigned long list_lru_count_node(struct list_lru *lru, int nid)
  170. {
  171. struct list_lru_node *nlru;
  172. nlru = &lru->node[nid];
  173. return nlru->nr_items;
  174. }
  175. EXPORT_SYMBOL_GPL(list_lru_count_node);
  176. static unsigned long
  177. __list_lru_walk_one(struct list_lru *lru, int nid, int memcg_idx,
  178. list_lru_walk_cb isolate, void *cb_arg,
  179. unsigned long *nr_to_walk)
  180. {
  181. struct list_lru_node *nlru = &lru->node[nid];
  182. struct list_lru_one *l;
  183. struct list_head *item, *n;
  184. unsigned long isolated = 0;
  185. restart:
  186. l = list_lru_from_memcg_idx(lru, nid, memcg_idx);
  187. if (!l)
  188. goto out;
  189. list_for_each_safe(item, n, &l->list) {
  190. enum lru_status ret;
  191. /*
  192. * decrement nr_to_walk first so that we don't livelock if we
  193. * get stuck on large numbers of LRU_RETRY items
  194. */
  195. if (!*nr_to_walk)
  196. break;
  197. --*nr_to_walk;
  198. ret = isolate(item, l, &nlru->lock, cb_arg);
  199. switch (ret) {
  200. case LRU_REMOVED_RETRY:
  201. assert_spin_locked(&nlru->lock);
  202. fallthrough;
  203. case LRU_REMOVED:
  204. isolated++;
  205. nlru->nr_items--;
  206. /*
  207. * If the lru lock has been dropped, our list
  208. * traversal is now invalid and so we have to
  209. * restart from scratch.
  210. */
  211. if (ret == LRU_REMOVED_RETRY)
  212. goto restart;
  213. break;
  214. case LRU_ROTATE:
  215. list_move_tail(item, &l->list);
  216. break;
  217. case LRU_SKIP:
  218. break;
  219. case LRU_RETRY:
  220. /*
  221. * The lru lock has been dropped, our list traversal is
  222. * now invalid and so we have to restart from scratch.
  223. */
  224. assert_spin_locked(&nlru->lock);
  225. goto restart;
  226. case LRU_STOP:
  227. assert_spin_locked(&nlru->lock);
  228. goto out;
  229. default:
  230. BUG();
  231. }
  232. }
  233. out:
  234. return isolated;
  235. }
  236. unsigned long
  237. list_lru_walk_one(struct list_lru *lru, int nid, struct mem_cgroup *memcg,
  238. list_lru_walk_cb isolate, void *cb_arg,
  239. unsigned long *nr_to_walk)
  240. {
  241. struct list_lru_node *nlru = &lru->node[nid];
  242. unsigned long ret;
  243. spin_lock(&nlru->lock);
  244. ret = __list_lru_walk_one(lru, nid, memcg_kmem_id(memcg), isolate,
  245. cb_arg, nr_to_walk);
  246. spin_unlock(&nlru->lock);
  247. return ret;
  248. }
  249. EXPORT_SYMBOL_GPL(list_lru_walk_one);
  250. unsigned long
  251. list_lru_walk_one_irq(struct list_lru *lru, int nid, struct mem_cgroup *memcg,
  252. list_lru_walk_cb isolate, void *cb_arg,
  253. unsigned long *nr_to_walk)
  254. {
  255. struct list_lru_node *nlru = &lru->node[nid];
  256. unsigned long ret;
  257. spin_lock_irq(&nlru->lock);
  258. ret = __list_lru_walk_one(lru, nid, memcg_kmem_id(memcg), isolate,
  259. cb_arg, nr_to_walk);
  260. spin_unlock_irq(&nlru->lock);
  261. return ret;
  262. }
  263. unsigned long list_lru_walk_node(struct list_lru *lru, int nid,
  264. list_lru_walk_cb isolate, void *cb_arg,
  265. unsigned long *nr_to_walk)
  266. {
  267. long isolated = 0;
  268. isolated += list_lru_walk_one(lru, nid, NULL, isolate, cb_arg,
  269. nr_to_walk);
  270. #ifdef CONFIG_MEMCG
  271. if (*nr_to_walk > 0 && list_lru_memcg_aware(lru)) {
  272. struct list_lru_memcg *mlru;
  273. unsigned long index;
  274. xa_for_each(&lru->xa, index, mlru) {
  275. struct list_lru_node *nlru = &lru->node[nid];
  276. spin_lock(&nlru->lock);
  277. isolated += __list_lru_walk_one(lru, nid, index,
  278. isolate, cb_arg,
  279. nr_to_walk);
  280. spin_unlock(&nlru->lock);
  281. if (*nr_to_walk <= 0)
  282. break;
  283. }
  284. }
  285. #endif
  286. return isolated;
  287. }
  288. EXPORT_SYMBOL_GPL(list_lru_walk_node);
  289. static void init_one_lru(struct list_lru_one *l)
  290. {
  291. INIT_LIST_HEAD(&l->list);
  292. l->nr_items = 0;
  293. }
  294. #ifdef CONFIG_MEMCG
  295. static struct list_lru_memcg *memcg_init_list_lru_one(gfp_t gfp)
  296. {
  297. int nid;
  298. struct list_lru_memcg *mlru;
  299. mlru = kmalloc(struct_size(mlru, node, nr_node_ids), gfp);
  300. if (!mlru)
  301. return NULL;
  302. for_each_node(nid)
  303. init_one_lru(&mlru->node[nid]);
  304. return mlru;
  305. }
  306. static void memcg_list_lru_free(struct list_lru *lru, int src_idx)
  307. {
  308. struct list_lru_memcg *mlru = xa_erase_irq(&lru->xa, src_idx);
  309. /*
  310. * The __list_lru_walk_one() can walk the list of this node.
  311. * We need kvfree_rcu() here. And the walking of the list
  312. * is under lru->node[nid]->lock, which can serve as a RCU
  313. * read-side critical section.
  314. */
  315. if (mlru)
  316. kvfree_rcu(mlru, rcu);
  317. }
  318. static inline void memcg_init_list_lru(struct list_lru *lru, bool memcg_aware)
  319. {
  320. if (memcg_aware)
  321. xa_init_flags(&lru->xa, XA_FLAGS_LOCK_IRQ);
  322. lru->memcg_aware = memcg_aware;
  323. }
  324. static void memcg_destroy_list_lru(struct list_lru *lru)
  325. {
  326. XA_STATE(xas, &lru->xa, 0);
  327. struct list_lru_memcg *mlru;
  328. if (!list_lru_memcg_aware(lru))
  329. return;
  330. xas_lock_irq(&xas);
  331. xas_for_each(&xas, mlru, ULONG_MAX) {
  332. kfree(mlru);
  333. xas_store(&xas, NULL);
  334. }
  335. xas_unlock_irq(&xas);
  336. }
  337. static void memcg_reparent_list_lru_node(struct list_lru *lru, int nid,
  338. int src_idx, struct mem_cgroup *dst_memcg)
  339. {
  340. struct list_lru_node *nlru = &lru->node[nid];
  341. int dst_idx = dst_memcg->kmemcg_id;
  342. struct list_lru_one *src, *dst;
  343. /*
  344. * Since list_lru_{add,del} may be called under an IRQ-safe lock,
  345. * we have to use IRQ-safe primitives here to avoid deadlock.
  346. */
  347. spin_lock_irq(&nlru->lock);
  348. src = list_lru_from_memcg_idx(lru, nid, src_idx);
  349. if (!src)
  350. goto out;
  351. dst = list_lru_from_memcg_idx(lru, nid, dst_idx);
  352. list_splice_init(&src->list, &dst->list);
  353. if (src->nr_items) {
  354. dst->nr_items += src->nr_items;
  355. set_shrinker_bit(dst_memcg, nid, lru_shrinker_id(lru));
  356. src->nr_items = 0;
  357. }
  358. out:
  359. spin_unlock_irq(&nlru->lock);
  360. }
  361. static void memcg_reparent_list_lru(struct list_lru *lru,
  362. int src_idx, struct mem_cgroup *dst_memcg)
  363. {
  364. int i;
  365. for_each_node(i)
  366. memcg_reparent_list_lru_node(lru, i, src_idx, dst_memcg);
  367. memcg_list_lru_free(lru, src_idx);
  368. }
  369. void memcg_reparent_list_lrus(struct mem_cgroup *memcg, struct mem_cgroup *parent)
  370. {
  371. struct cgroup_subsys_state *css;
  372. struct list_lru *lru;
  373. int src_idx = memcg->kmemcg_id;
  374. /*
  375. * Change kmemcg_id of this cgroup and all its descendants to the
  376. * parent's id, and then move all entries from this cgroup's list_lrus
  377. * to ones of the parent.
  378. *
  379. * After we have finished, all list_lrus corresponding to this cgroup
  380. * are guaranteed to remain empty. So we can safely free this cgroup's
  381. * list lrus in memcg_list_lru_free().
  382. *
  383. * Changing ->kmemcg_id to the parent can prevent memcg_list_lru_alloc()
  384. * from allocating list lrus for this cgroup after memcg_list_lru_free()
  385. * call.
  386. */
  387. rcu_read_lock();
  388. css_for_each_descendant_pre(css, &memcg->css) {
  389. struct mem_cgroup *child;
  390. child = mem_cgroup_from_css(css);
  391. WRITE_ONCE(child->kmemcg_id, parent->kmemcg_id);
  392. }
  393. rcu_read_unlock();
  394. mutex_lock(&list_lrus_mutex);
  395. list_for_each_entry(lru, &memcg_list_lrus, list)
  396. memcg_reparent_list_lru(lru, src_idx, parent);
  397. mutex_unlock(&list_lrus_mutex);
  398. }
  399. static inline bool memcg_list_lru_allocated(struct mem_cgroup *memcg,
  400. struct list_lru *lru)
  401. {
  402. int idx = memcg->kmemcg_id;
  403. return idx < 0 || xa_load(&lru->xa, idx);
  404. }
  405. int memcg_list_lru_alloc(struct mem_cgroup *memcg, struct list_lru *lru,
  406. gfp_t gfp)
  407. {
  408. int i;
  409. unsigned long flags;
  410. struct list_lru_memcg_table {
  411. struct list_lru_memcg *mlru;
  412. struct mem_cgroup *memcg;
  413. } *table;
  414. XA_STATE(xas, &lru->xa, 0);
  415. if (!list_lru_memcg_aware(lru) || memcg_list_lru_allocated(memcg, lru))
  416. return 0;
  417. gfp &= GFP_RECLAIM_MASK;
  418. table = kmalloc_array(memcg->css.cgroup->level, sizeof(*table), gfp);
  419. if (!table)
  420. return -ENOMEM;
  421. /*
  422. * Because the list_lru can be reparented to the parent cgroup's
  423. * list_lru, we should make sure that this cgroup and all its
  424. * ancestors have allocated list_lru_memcg.
  425. */
  426. for (i = 0; memcg; memcg = parent_mem_cgroup(memcg), i++) {
  427. if (memcg_list_lru_allocated(memcg, lru))
  428. break;
  429. table[i].memcg = memcg;
  430. table[i].mlru = memcg_init_list_lru_one(gfp);
  431. if (!table[i].mlru) {
  432. while (i--)
  433. kfree(table[i].mlru);
  434. kfree(table);
  435. return -ENOMEM;
  436. }
  437. }
  438. xas_lock_irqsave(&xas, flags);
  439. while (i--) {
  440. int index = READ_ONCE(table[i].memcg->kmemcg_id);
  441. struct list_lru_memcg *mlru = table[i].mlru;
  442. xas_set(&xas, index);
  443. retry:
  444. if (unlikely(index < 0 || xas_error(&xas) || xas_load(&xas))) {
  445. kfree(mlru);
  446. } else {
  447. xas_store(&xas, mlru);
  448. if (xas_error(&xas) == -ENOMEM) {
  449. xas_unlock_irqrestore(&xas, flags);
  450. if (xas_nomem(&xas, gfp))
  451. xas_set_err(&xas, 0);
  452. xas_lock_irqsave(&xas, flags);
  453. /*
  454. * The xas lock has been released, this memcg
  455. * can be reparented before us. So reload
  456. * memcg id. More details see the comments
  457. * in memcg_reparent_list_lrus().
  458. */
  459. index = READ_ONCE(table[i].memcg->kmemcg_id);
  460. if (index < 0)
  461. xas_set_err(&xas, 0);
  462. else if (!xas_error(&xas) && index != xas.xa_index)
  463. xas_set(&xas, index);
  464. goto retry;
  465. }
  466. }
  467. }
  468. /* xas_nomem() is used to free memory instead of memory allocation. */
  469. if (xas.xa_alloc)
  470. xas_nomem(&xas, gfp);
  471. xas_unlock_irqrestore(&xas, flags);
  472. kfree(table);
  473. return xas_error(&xas);
  474. }
  475. #else
  476. static inline void memcg_init_list_lru(struct list_lru *lru, bool memcg_aware)
  477. {
  478. }
  479. static void memcg_destroy_list_lru(struct list_lru *lru)
  480. {
  481. }
  482. #endif /* CONFIG_MEMCG */
  483. int __list_lru_init(struct list_lru *lru, bool memcg_aware,
  484. struct lock_class_key *key, struct shrinker *shrinker)
  485. {
  486. int i;
  487. #ifdef CONFIG_MEMCG
  488. if (shrinker)
  489. lru->shrinker_id = shrinker->id;
  490. else
  491. lru->shrinker_id = -1;
  492. if (mem_cgroup_kmem_disabled())
  493. memcg_aware = false;
  494. #endif
  495. lru->node = kcalloc(nr_node_ids, sizeof(*lru->node), GFP_KERNEL);
  496. if (!lru->node)
  497. return -ENOMEM;
  498. for_each_node(i) {
  499. spin_lock_init(&lru->node[i].lock);
  500. if (key)
  501. lockdep_set_class(&lru->node[i].lock, key);
  502. init_one_lru(&lru->node[i].lru);
  503. }
  504. memcg_init_list_lru(lru, memcg_aware);
  505. list_lru_register(lru);
  506. return 0;
  507. }
  508. EXPORT_SYMBOL_GPL(__list_lru_init);
  509. void list_lru_destroy(struct list_lru *lru)
  510. {
  511. /* Already destroyed or not yet initialized? */
  512. if (!lru->node)
  513. return;
  514. list_lru_unregister(lru);
  515. memcg_destroy_list_lru(lru);
  516. kfree(lru->node);
  517. lru->node = NULL;
  518. #ifdef CONFIG_MEMCG
  519. lru->shrinker_id = -1;
  520. #endif
  521. }
  522. EXPORT_SYMBOL_GPL(list_lru_destroy);