list_lru.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /*
  2. * Copyright (c) 2013 Red Hat, Inc. and Parallels Inc. All rights reserved.
  3. * Authors: David Chinner and Glauber Costa
  4. *
  5. * Generic LRU infrastructure
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/mm.h>
  10. #include <linux/list_lru.h>
  11. #include <linux/slab.h>
  12. #include <linux/mutex.h>
  13. #include <linux/memcontrol.h>
  14. #ifdef CONFIG_MEMCG_KMEM
  15. static LIST_HEAD(list_lrus);
  16. static DEFINE_MUTEX(list_lrus_mutex);
  17. static void list_lru_register(struct list_lru *lru)
  18. {
  19. mutex_lock(&list_lrus_mutex);
  20. list_add(&lru->list, &list_lrus);
  21. mutex_unlock(&list_lrus_mutex);
  22. }
  23. static void list_lru_unregister(struct list_lru *lru)
  24. {
  25. mutex_lock(&list_lrus_mutex);
  26. list_del(&lru->list);
  27. mutex_unlock(&list_lrus_mutex);
  28. }
  29. #else
  30. static void list_lru_register(struct list_lru *lru)
  31. {
  32. }
  33. static void list_lru_unregister(struct list_lru *lru)
  34. {
  35. }
  36. #endif /* CONFIG_MEMCG_KMEM */
  37. #ifdef CONFIG_MEMCG_KMEM
  38. static inline bool list_lru_memcg_aware(struct list_lru *lru)
  39. {
  40. /*
  41. * This needs node 0 to be always present, even
  42. * in the systems supporting sparse numa ids.
  43. */
  44. return !!lru->node[0].memcg_lrus;
  45. }
  46. static inline struct list_lru_one *
  47. list_lru_from_memcg_idx(struct list_lru_node *nlru, int idx)
  48. {
  49. /*
  50. * The lock protects the array of per cgroup lists from relocation
  51. * (see memcg_update_list_lru_node).
  52. */
  53. lockdep_assert_held(&nlru->lock);
  54. if (nlru->memcg_lrus && idx >= 0)
  55. return nlru->memcg_lrus->lru[idx];
  56. return &nlru->lru;
  57. }
  58. static __always_inline struct mem_cgroup *mem_cgroup_from_kmem(void *ptr)
  59. {
  60. struct page *page;
  61. if (!memcg_kmem_enabled())
  62. return NULL;
  63. page = virt_to_head_page(ptr);
  64. return page->mem_cgroup;
  65. }
  66. static inline struct list_lru_one *
  67. list_lru_from_kmem(struct list_lru_node *nlru, void *ptr)
  68. {
  69. struct mem_cgroup *memcg;
  70. if (!nlru->memcg_lrus)
  71. return &nlru->lru;
  72. memcg = mem_cgroup_from_kmem(ptr);
  73. if (!memcg)
  74. return &nlru->lru;
  75. return list_lru_from_memcg_idx(nlru, memcg_cache_id(memcg));
  76. }
  77. #else
  78. static inline bool list_lru_memcg_aware(struct list_lru *lru)
  79. {
  80. return false;
  81. }
  82. static inline struct list_lru_one *
  83. list_lru_from_memcg_idx(struct list_lru_node *nlru, int idx)
  84. {
  85. return &nlru->lru;
  86. }
  87. static inline struct list_lru_one *
  88. list_lru_from_kmem(struct list_lru_node *nlru, void *ptr)
  89. {
  90. return &nlru->lru;
  91. }
  92. #endif /* CONFIG_MEMCG_KMEM */
  93. bool list_lru_add(struct list_lru *lru, struct list_head *item)
  94. {
  95. int nid = page_to_nid(virt_to_page(item));
  96. struct list_lru_node *nlru = &lru->node[nid];
  97. struct list_lru_one *l;
  98. spin_lock(&nlru->lock);
  99. if (list_empty(item)) {
  100. l = list_lru_from_kmem(nlru, item);
  101. list_add_tail(item, &l->list);
  102. l->nr_items++;
  103. nlru->nr_items++;
  104. spin_unlock(&nlru->lock);
  105. return true;
  106. }
  107. spin_unlock(&nlru->lock);
  108. return false;
  109. }
  110. EXPORT_SYMBOL_GPL(list_lru_add);
  111. bool list_lru_del(struct list_lru *lru, struct list_head *item)
  112. {
  113. int nid = page_to_nid(virt_to_page(item));
  114. struct list_lru_node *nlru = &lru->node[nid];
  115. struct list_lru_one *l;
  116. spin_lock(&nlru->lock);
  117. if (!list_empty(item)) {
  118. l = list_lru_from_kmem(nlru, item);
  119. list_del_init(item);
  120. l->nr_items--;
  121. nlru->nr_items--;
  122. spin_unlock(&nlru->lock);
  123. return true;
  124. }
  125. spin_unlock(&nlru->lock);
  126. return false;
  127. }
  128. EXPORT_SYMBOL_GPL(list_lru_del);
  129. void list_lru_isolate(struct list_lru_one *list, struct list_head *item)
  130. {
  131. list_del_init(item);
  132. list->nr_items--;
  133. }
  134. EXPORT_SYMBOL_GPL(list_lru_isolate);
  135. void list_lru_isolate_move(struct list_lru_one *list, struct list_head *item,
  136. struct list_head *head)
  137. {
  138. list_move(item, head);
  139. list->nr_items--;
  140. }
  141. EXPORT_SYMBOL_GPL(list_lru_isolate_move);
  142. static unsigned long __list_lru_count_one(struct list_lru *lru,
  143. int nid, int memcg_idx)
  144. {
  145. struct list_lru_node *nlru = &lru->node[nid];
  146. struct list_lru_one *l;
  147. unsigned long count;
  148. spin_lock(&nlru->lock);
  149. l = list_lru_from_memcg_idx(nlru, memcg_idx);
  150. count = l->nr_items;
  151. spin_unlock(&nlru->lock);
  152. return count;
  153. }
  154. unsigned long list_lru_count_one(struct list_lru *lru,
  155. int nid, struct mem_cgroup *memcg)
  156. {
  157. return __list_lru_count_one(lru, nid, memcg_cache_id(memcg));
  158. }
  159. EXPORT_SYMBOL_GPL(list_lru_count_one);
  160. unsigned long list_lru_count_node(struct list_lru *lru, int nid)
  161. {
  162. struct list_lru_node *nlru;
  163. nlru = &lru->node[nid];
  164. return nlru->nr_items;
  165. }
  166. EXPORT_SYMBOL_GPL(list_lru_count_node);
  167. static unsigned long
  168. __list_lru_walk_one(struct list_lru *lru, int nid, int memcg_idx,
  169. list_lru_walk_cb isolate, void *cb_arg,
  170. unsigned long *nr_to_walk)
  171. {
  172. struct list_lru_node *nlru = &lru->node[nid];
  173. struct list_lru_one *l;
  174. struct list_head *item, *n;
  175. unsigned long isolated = 0;
  176. spin_lock(&nlru->lock);
  177. l = list_lru_from_memcg_idx(nlru, memcg_idx);
  178. restart:
  179. list_for_each_safe(item, n, &l->list) {
  180. enum lru_status ret;
  181. /*
  182. * decrement nr_to_walk first so that we don't livelock if we
  183. * get stuck on large numbesr of LRU_RETRY items
  184. */
  185. if (!*nr_to_walk)
  186. break;
  187. --*nr_to_walk;
  188. ret = isolate(item, l, &nlru->lock, cb_arg);
  189. switch (ret) {
  190. case LRU_REMOVED_RETRY:
  191. assert_spin_locked(&nlru->lock);
  192. case LRU_REMOVED:
  193. isolated++;
  194. nlru->nr_items--;
  195. /*
  196. * If the lru lock has been dropped, our list
  197. * traversal is now invalid and so we have to
  198. * restart from scratch.
  199. */
  200. if (ret == LRU_REMOVED_RETRY)
  201. goto restart;
  202. break;
  203. case LRU_ROTATE:
  204. list_move_tail(item, &l->list);
  205. break;
  206. case LRU_SKIP:
  207. break;
  208. case LRU_RETRY:
  209. /*
  210. * The lru lock has been dropped, our list traversal is
  211. * now invalid and so we have to restart from scratch.
  212. */
  213. assert_spin_locked(&nlru->lock);
  214. goto restart;
  215. default:
  216. BUG();
  217. }
  218. }
  219. spin_unlock(&nlru->lock);
  220. return isolated;
  221. }
  222. unsigned long
  223. list_lru_walk_one(struct list_lru *lru, int nid, struct mem_cgroup *memcg,
  224. list_lru_walk_cb isolate, void *cb_arg,
  225. unsigned long *nr_to_walk)
  226. {
  227. return __list_lru_walk_one(lru, nid, memcg_cache_id(memcg),
  228. isolate, cb_arg, nr_to_walk);
  229. }
  230. EXPORT_SYMBOL_GPL(list_lru_walk_one);
  231. unsigned long list_lru_walk_node(struct list_lru *lru, int nid,
  232. list_lru_walk_cb isolate, void *cb_arg,
  233. unsigned long *nr_to_walk)
  234. {
  235. long isolated = 0;
  236. int memcg_idx;
  237. isolated += __list_lru_walk_one(lru, nid, -1, isolate, cb_arg,
  238. nr_to_walk);
  239. if (*nr_to_walk > 0 && list_lru_memcg_aware(lru)) {
  240. for_each_memcg_cache_index(memcg_idx) {
  241. isolated += __list_lru_walk_one(lru, nid, memcg_idx,
  242. isolate, cb_arg, nr_to_walk);
  243. if (*nr_to_walk <= 0)
  244. break;
  245. }
  246. }
  247. return isolated;
  248. }
  249. EXPORT_SYMBOL_GPL(list_lru_walk_node);
  250. static void init_one_lru(struct list_lru_one *l)
  251. {
  252. INIT_LIST_HEAD(&l->list);
  253. l->nr_items = 0;
  254. }
  255. #ifdef CONFIG_MEMCG_KMEM
  256. static void __memcg_destroy_list_lru_node(struct list_lru_memcg *memcg_lrus,
  257. int begin, int end)
  258. {
  259. int i;
  260. for (i = begin; i < end; i++)
  261. kfree(memcg_lrus->lru[i]);
  262. }
  263. static int __memcg_init_list_lru_node(struct list_lru_memcg *memcg_lrus,
  264. int begin, int end)
  265. {
  266. int i;
  267. for (i = begin; i < end; i++) {
  268. struct list_lru_one *l;
  269. l = kmalloc(sizeof(struct list_lru_one), GFP_KERNEL);
  270. if (!l)
  271. goto fail;
  272. init_one_lru(l);
  273. memcg_lrus->lru[i] = l;
  274. }
  275. return 0;
  276. fail:
  277. __memcg_destroy_list_lru_node(memcg_lrus, begin, i - 1);
  278. return -ENOMEM;
  279. }
  280. static int memcg_init_list_lru_node(struct list_lru_node *nlru)
  281. {
  282. int size = memcg_nr_cache_ids;
  283. nlru->memcg_lrus = kmalloc(size * sizeof(void *), GFP_KERNEL);
  284. if (!nlru->memcg_lrus)
  285. return -ENOMEM;
  286. if (__memcg_init_list_lru_node(nlru->memcg_lrus, 0, size)) {
  287. kfree(nlru->memcg_lrus);
  288. return -ENOMEM;
  289. }
  290. return 0;
  291. }
  292. static void memcg_destroy_list_lru_node(struct list_lru_node *nlru)
  293. {
  294. __memcg_destroy_list_lru_node(nlru->memcg_lrus, 0, memcg_nr_cache_ids);
  295. kfree(nlru->memcg_lrus);
  296. }
  297. static int memcg_update_list_lru_node(struct list_lru_node *nlru,
  298. int old_size, int new_size)
  299. {
  300. struct list_lru_memcg *old, *new;
  301. BUG_ON(old_size > new_size);
  302. old = nlru->memcg_lrus;
  303. new = kmalloc(new_size * sizeof(void *), GFP_KERNEL);
  304. if (!new)
  305. return -ENOMEM;
  306. if (__memcg_init_list_lru_node(new, old_size, new_size)) {
  307. kfree(new);
  308. return -ENOMEM;
  309. }
  310. memcpy(new, old, old_size * sizeof(void *));
  311. /*
  312. * The lock guarantees that we won't race with a reader
  313. * (see list_lru_from_memcg_idx).
  314. *
  315. * Since list_lru_{add,del} may be called under an IRQ-safe lock,
  316. * we have to use IRQ-safe primitives here to avoid deadlock.
  317. */
  318. spin_lock_irq(&nlru->lock);
  319. nlru->memcg_lrus = new;
  320. spin_unlock_irq(&nlru->lock);
  321. kfree(old);
  322. return 0;
  323. }
  324. static void memcg_cancel_update_list_lru_node(struct list_lru_node *nlru,
  325. int old_size, int new_size)
  326. {
  327. /* do not bother shrinking the array back to the old size, because we
  328. * cannot handle allocation failures here */
  329. __memcg_destroy_list_lru_node(nlru->memcg_lrus, old_size, new_size);
  330. }
  331. static int memcg_init_list_lru(struct list_lru *lru, bool memcg_aware)
  332. {
  333. int i;
  334. if (!memcg_aware)
  335. return 0;
  336. for_each_node(i) {
  337. if (memcg_init_list_lru_node(&lru->node[i]))
  338. goto fail;
  339. }
  340. return 0;
  341. fail:
  342. for (i = i - 1; i >= 0; i--) {
  343. if (!lru->node[i].memcg_lrus)
  344. continue;
  345. memcg_destroy_list_lru_node(&lru->node[i]);
  346. }
  347. return -ENOMEM;
  348. }
  349. static void memcg_destroy_list_lru(struct list_lru *lru)
  350. {
  351. int i;
  352. if (!list_lru_memcg_aware(lru))
  353. return;
  354. for_each_node(i)
  355. memcg_destroy_list_lru_node(&lru->node[i]);
  356. }
  357. static int memcg_update_list_lru(struct list_lru *lru,
  358. int old_size, int new_size)
  359. {
  360. int i;
  361. if (!list_lru_memcg_aware(lru))
  362. return 0;
  363. for_each_node(i) {
  364. if (memcg_update_list_lru_node(&lru->node[i],
  365. old_size, new_size))
  366. goto fail;
  367. }
  368. return 0;
  369. fail:
  370. for (i = i - 1; i >= 0; i--) {
  371. if (!lru->node[i].memcg_lrus)
  372. continue;
  373. memcg_cancel_update_list_lru_node(&lru->node[i],
  374. old_size, new_size);
  375. }
  376. return -ENOMEM;
  377. }
  378. static void memcg_cancel_update_list_lru(struct list_lru *lru,
  379. int old_size, int new_size)
  380. {
  381. int i;
  382. if (!list_lru_memcg_aware(lru))
  383. return;
  384. for_each_node(i)
  385. memcg_cancel_update_list_lru_node(&lru->node[i],
  386. old_size, new_size);
  387. }
  388. int memcg_update_all_list_lrus(int new_size)
  389. {
  390. int ret = 0;
  391. struct list_lru *lru;
  392. int old_size = memcg_nr_cache_ids;
  393. mutex_lock(&list_lrus_mutex);
  394. list_for_each_entry(lru, &list_lrus, list) {
  395. ret = memcg_update_list_lru(lru, old_size, new_size);
  396. if (ret)
  397. goto fail;
  398. }
  399. out:
  400. mutex_unlock(&list_lrus_mutex);
  401. return ret;
  402. fail:
  403. list_for_each_entry_continue_reverse(lru, &list_lrus, list)
  404. memcg_cancel_update_list_lru(lru, old_size, new_size);
  405. goto out;
  406. }
  407. static void memcg_drain_list_lru_node(struct list_lru_node *nlru,
  408. int src_idx, int dst_idx)
  409. {
  410. struct list_lru_one *src, *dst;
  411. /*
  412. * Since list_lru_{add,del} may be called under an IRQ-safe lock,
  413. * we have to use IRQ-safe primitives here to avoid deadlock.
  414. */
  415. spin_lock_irq(&nlru->lock);
  416. src = list_lru_from_memcg_idx(nlru, src_idx);
  417. dst = list_lru_from_memcg_idx(nlru, dst_idx);
  418. list_splice_init(&src->list, &dst->list);
  419. dst->nr_items += src->nr_items;
  420. src->nr_items = 0;
  421. spin_unlock_irq(&nlru->lock);
  422. }
  423. static void memcg_drain_list_lru(struct list_lru *lru,
  424. int src_idx, int dst_idx)
  425. {
  426. int i;
  427. if (!list_lru_memcg_aware(lru))
  428. return;
  429. for_each_node(i)
  430. memcg_drain_list_lru_node(&lru->node[i], src_idx, dst_idx);
  431. }
  432. void memcg_drain_all_list_lrus(int src_idx, int dst_idx)
  433. {
  434. struct list_lru *lru;
  435. mutex_lock(&list_lrus_mutex);
  436. list_for_each_entry(lru, &list_lrus, list)
  437. memcg_drain_list_lru(lru, src_idx, dst_idx);
  438. mutex_unlock(&list_lrus_mutex);
  439. }
  440. #else
  441. static int memcg_init_list_lru(struct list_lru *lru, bool memcg_aware)
  442. {
  443. return 0;
  444. }
  445. static void memcg_destroy_list_lru(struct list_lru *lru)
  446. {
  447. }
  448. #endif /* CONFIG_MEMCG_KMEM */
  449. int __list_lru_init(struct list_lru *lru, bool memcg_aware,
  450. struct lock_class_key *key)
  451. {
  452. int i;
  453. size_t size = sizeof(*lru->node) * nr_node_ids;
  454. int err = -ENOMEM;
  455. memcg_get_cache_ids();
  456. lru->node = kzalloc(size, GFP_KERNEL);
  457. if (!lru->node)
  458. goto out;
  459. for_each_node(i) {
  460. spin_lock_init(&lru->node[i].lock);
  461. if (key)
  462. lockdep_set_class(&lru->node[i].lock, key);
  463. init_one_lru(&lru->node[i].lru);
  464. }
  465. err = memcg_init_list_lru(lru, memcg_aware);
  466. if (err) {
  467. kfree(lru->node);
  468. /* Do this so a list_lru_destroy() doesn't crash: */
  469. lru->node = NULL;
  470. goto out;
  471. }
  472. list_lru_register(lru);
  473. out:
  474. memcg_put_cache_ids();
  475. return err;
  476. }
  477. EXPORT_SYMBOL_GPL(__list_lru_init);
  478. void list_lru_destroy(struct list_lru *lru)
  479. {
  480. /* Already destroyed or not yet initialized? */
  481. if (!lru->node)
  482. return;
  483. memcg_get_cache_ids();
  484. list_lru_unregister(lru);
  485. memcg_destroy_list_lru(lru);
  486. kfree(lru->node);
  487. lru->node = NULL;
  488. memcg_put_cache_ids();
  489. }
  490. EXPORT_SYMBOL_GPL(list_lru_destroy);