]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - mm/list_lru.c
list_lru: organize all list_lrus to list
[mirror_ubuntu-artful-kernel.git] / mm / list_lru.c
CommitLineData
a38e4082
DC
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>
3b1d58a4 9#include <linux/mm.h>
a38e4082 10#include <linux/list_lru.h>
5ca302c8 11#include <linux/slab.h>
c0a5b560
VD
12#include <linux/mutex.h>
13
14#ifdef CONFIG_MEMCG_KMEM
15static LIST_HEAD(list_lrus);
16static DEFINE_MUTEX(list_lrus_mutex);
17
18static void list_lru_register(struct list_lru *lru)
19{
20 mutex_lock(&list_lrus_mutex);
21 list_add(&lru->list, &list_lrus);
22 mutex_unlock(&list_lrus_mutex);
23}
24
25static void list_lru_unregister(struct list_lru *lru)
26{
27 mutex_lock(&list_lrus_mutex);
28 list_del(&lru->list);
29 mutex_unlock(&list_lrus_mutex);
30}
31#else
32static void list_lru_register(struct list_lru *lru)
33{
34}
35
36static void list_lru_unregister(struct list_lru *lru)
37{
38}
39#endif /* CONFIG_MEMCG_KMEM */
a38e4082
DC
40
41bool list_lru_add(struct list_lru *lru, struct list_head *item)
42{
3b1d58a4
DC
43 int nid = page_to_nid(virt_to_page(item));
44 struct list_lru_node *nlru = &lru->node[nid];
45
46 spin_lock(&nlru->lock);
47 WARN_ON_ONCE(nlru->nr_items < 0);
a38e4082 48 if (list_empty(item)) {
3b1d58a4 49 list_add_tail(item, &nlru->list);
ff0b67ef 50 nlru->nr_items++;
3b1d58a4 51 spin_unlock(&nlru->lock);
a38e4082
DC
52 return true;
53 }
3b1d58a4 54 spin_unlock(&nlru->lock);
a38e4082
DC
55 return false;
56}
57EXPORT_SYMBOL_GPL(list_lru_add);
58
59bool list_lru_del(struct list_lru *lru, struct list_head *item)
60{
3b1d58a4
DC
61 int nid = page_to_nid(virt_to_page(item));
62 struct list_lru_node *nlru = &lru->node[nid];
63
64 spin_lock(&nlru->lock);
a38e4082
DC
65 if (!list_empty(item)) {
66 list_del_init(item);
ff0b67ef 67 nlru->nr_items--;
3b1d58a4
DC
68 WARN_ON_ONCE(nlru->nr_items < 0);
69 spin_unlock(&nlru->lock);
a38e4082
DC
70 return true;
71 }
3b1d58a4 72 spin_unlock(&nlru->lock);
a38e4082
DC
73 return false;
74}
75EXPORT_SYMBOL_GPL(list_lru_del);
76
6a4f496f
GC
77unsigned long
78list_lru_count_node(struct list_lru *lru, int nid)
a38e4082 79{
3b1d58a4 80 unsigned long count = 0;
6a4f496f 81 struct list_lru_node *nlru = &lru->node[nid];
3b1d58a4 82
6a4f496f
GC
83 spin_lock(&nlru->lock);
84 WARN_ON_ONCE(nlru->nr_items < 0);
85 count += nlru->nr_items;
86 spin_unlock(&nlru->lock);
3b1d58a4
DC
87
88 return count;
89}
6a4f496f 90EXPORT_SYMBOL_GPL(list_lru_count_node);
3b1d58a4 91
6a4f496f 92unsigned long
3b1d58a4
DC
93list_lru_walk_node(struct list_lru *lru, int nid, list_lru_walk_cb isolate,
94 void *cb_arg, unsigned long *nr_to_walk)
95{
96
97 struct list_lru_node *nlru = &lru->node[nid];
a38e4082 98 struct list_head *item, *n;
3b1d58a4 99 unsigned long isolated = 0;
a38e4082 100
3b1d58a4 101 spin_lock(&nlru->lock);
a38e4082 102restart:
3b1d58a4 103 list_for_each_safe(item, n, &nlru->list) {
a38e4082 104 enum lru_status ret;
5cedf721
DC
105
106 /*
107 * decrement nr_to_walk first so that we don't livelock if we
108 * get stuck on large numbesr of LRU_RETRY items
109 */
c56b097a 110 if (!*nr_to_walk)
5cedf721 111 break;
c56b097a 112 --*nr_to_walk;
5cedf721 113
3b1d58a4 114 ret = isolate(item, &nlru->lock, cb_arg);
a38e4082 115 switch (ret) {
449dd698
JW
116 case LRU_REMOVED_RETRY:
117 assert_spin_locked(&nlru->lock);
a38e4082 118 case LRU_REMOVED:
ff0b67ef 119 nlru->nr_items--;
3b1d58a4
DC
120 WARN_ON_ONCE(nlru->nr_items < 0);
121 isolated++;
449dd698
JW
122 /*
123 * If the lru lock has been dropped, our list
124 * traversal is now invalid and so we have to
125 * restart from scratch.
126 */
127 if (ret == LRU_REMOVED_RETRY)
128 goto restart;
a38e4082
DC
129 break;
130 case LRU_ROTATE:
3b1d58a4 131 list_move_tail(item, &nlru->list);
a38e4082
DC
132 break;
133 case LRU_SKIP:
134 break;
135 case LRU_RETRY:
5cedf721
DC
136 /*
137 * The lru lock has been dropped, our list traversal is
138 * now invalid and so we have to restart from scratch.
139 */
449dd698 140 assert_spin_locked(&nlru->lock);
a38e4082
DC
141 goto restart;
142 default:
143 BUG();
144 }
a38e4082 145 }
3b1d58a4
DC
146
147 spin_unlock(&nlru->lock);
148 return isolated;
149}
150EXPORT_SYMBOL_GPL(list_lru_walk_node);
151
449dd698 152int list_lru_init_key(struct list_lru *lru, struct lock_class_key *key)
a38e4082 153{
3b1d58a4 154 int i;
5ca302c8
GC
155 size_t size = sizeof(*lru->node) * nr_node_ids;
156
157 lru->node = kzalloc(size, GFP_KERNEL);
158 if (!lru->node)
159 return -ENOMEM;
a38e4082 160
5ca302c8 161 for (i = 0; i < nr_node_ids; i++) {
3b1d58a4 162 spin_lock_init(&lru->node[i].lock);
449dd698
JW
163 if (key)
164 lockdep_set_class(&lru->node[i].lock, key);
3b1d58a4
DC
165 INIT_LIST_HEAD(&lru->node[i].list);
166 lru->node[i].nr_items = 0;
167 }
c0a5b560 168 list_lru_register(lru);
a38e4082
DC
169 return 0;
170}
449dd698 171EXPORT_SYMBOL_GPL(list_lru_init_key);
5ca302c8
GC
172
173void list_lru_destroy(struct list_lru *lru)
174{
c0a5b560
VD
175 /* Already destroyed or not yet initialized? */
176 if (!lru->node)
177 return;
178 list_lru_unregister(lru);
5ca302c8 179 kfree(lru->node);
c0a5b560 180 lru->node = NULL;
5ca302c8
GC
181}
182EXPORT_SYMBOL_GPL(list_lru_destroy);