Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 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> |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 9 | #include <linux/mm.h> |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 10 | #include <linux/list_lru.h> |
Glauber Costa | 5ca302c | 2013-08-28 10:18:18 +1000 | [diff] [blame] | 11 | #include <linux/slab.h> |
Vladimir Davydov | c0a5b56 | 2015-02-12 14:59:07 -0800 | [diff] [blame^] | 12 | #include <linux/mutex.h> |
| 13 | |
| 14 | #ifdef CONFIG_MEMCG_KMEM |
| 15 | static LIST_HEAD(list_lrus); |
| 16 | static DEFINE_MUTEX(list_lrus_mutex); |
| 17 | |
| 18 | static 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 | |
| 25 | static 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 |
| 32 | static void list_lru_register(struct list_lru *lru) |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | static void list_lru_unregister(struct list_lru *lru) |
| 37 | { |
| 38 | } |
| 39 | #endif /* CONFIG_MEMCG_KMEM */ |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 40 | |
| 41 | bool list_lru_add(struct list_lru *lru, struct list_head *item) |
| 42 | { |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 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); |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 48 | if (list_empty(item)) { |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 49 | list_add_tail(item, &nlru->list); |
Vladimir Davydov | ff0b67e | 2015-02-12 14:59:04 -0800 | [diff] [blame] | 50 | nlru->nr_items++; |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 51 | spin_unlock(&nlru->lock); |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 52 | return true; |
| 53 | } |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 54 | spin_unlock(&nlru->lock); |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 55 | return false; |
| 56 | } |
| 57 | EXPORT_SYMBOL_GPL(list_lru_add); |
| 58 | |
| 59 | bool list_lru_del(struct list_lru *lru, struct list_head *item) |
| 60 | { |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 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); |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 65 | if (!list_empty(item)) { |
| 66 | list_del_init(item); |
Vladimir Davydov | ff0b67e | 2015-02-12 14:59:04 -0800 | [diff] [blame] | 67 | nlru->nr_items--; |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 68 | WARN_ON_ONCE(nlru->nr_items < 0); |
| 69 | spin_unlock(&nlru->lock); |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 70 | return true; |
| 71 | } |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 72 | spin_unlock(&nlru->lock); |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 73 | return false; |
| 74 | } |
| 75 | EXPORT_SYMBOL_GPL(list_lru_del); |
| 76 | |
Glauber Costa | 6a4f496 | 2013-08-28 10:18:02 +1000 | [diff] [blame] | 77 | unsigned long |
| 78 | list_lru_count_node(struct list_lru *lru, int nid) |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 79 | { |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 80 | unsigned long count = 0; |
Glauber Costa | 6a4f496 | 2013-08-28 10:18:02 +1000 | [diff] [blame] | 81 | struct list_lru_node *nlru = &lru->node[nid]; |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 82 | |
Glauber Costa | 6a4f496 | 2013-08-28 10:18:02 +1000 | [diff] [blame] | 83 | spin_lock(&nlru->lock); |
| 84 | WARN_ON_ONCE(nlru->nr_items < 0); |
| 85 | count += nlru->nr_items; |
| 86 | spin_unlock(&nlru->lock); |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 87 | |
| 88 | return count; |
| 89 | } |
Glauber Costa | 6a4f496 | 2013-08-28 10:18:02 +1000 | [diff] [blame] | 90 | EXPORT_SYMBOL_GPL(list_lru_count_node); |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 91 | |
Glauber Costa | 6a4f496 | 2013-08-28 10:18:02 +1000 | [diff] [blame] | 92 | unsigned long |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 93 | list_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]; |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 98 | struct list_head *item, *n; |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 99 | unsigned long isolated = 0; |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 100 | |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 101 | spin_lock(&nlru->lock); |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 102 | restart: |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 103 | list_for_each_safe(item, n, &nlru->list) { |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 104 | enum lru_status ret; |
Dave Chinner | 5cedf721 | 2013-08-28 10:18:01 +1000 | [diff] [blame] | 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 | */ |
Russell King | c56b097 | 2013-10-30 14:16:16 +0000 | [diff] [blame] | 110 | if (!*nr_to_walk) |
Dave Chinner | 5cedf721 | 2013-08-28 10:18:01 +1000 | [diff] [blame] | 111 | break; |
Russell King | c56b097 | 2013-10-30 14:16:16 +0000 | [diff] [blame] | 112 | --*nr_to_walk; |
Dave Chinner | 5cedf721 | 2013-08-28 10:18:01 +1000 | [diff] [blame] | 113 | |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 114 | ret = isolate(item, &nlru->lock, cb_arg); |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 115 | switch (ret) { |
Johannes Weiner | 449dd69 | 2014-04-03 14:47:56 -0700 | [diff] [blame] | 116 | case LRU_REMOVED_RETRY: |
| 117 | assert_spin_locked(&nlru->lock); |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 118 | case LRU_REMOVED: |
Vladimir Davydov | ff0b67e | 2015-02-12 14:59:04 -0800 | [diff] [blame] | 119 | nlru->nr_items--; |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 120 | WARN_ON_ONCE(nlru->nr_items < 0); |
| 121 | isolated++; |
Johannes Weiner | 449dd69 | 2014-04-03 14:47:56 -0700 | [diff] [blame] | 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; |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 129 | break; |
| 130 | case LRU_ROTATE: |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 131 | list_move_tail(item, &nlru->list); |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 132 | break; |
| 133 | case LRU_SKIP: |
| 134 | break; |
| 135 | case LRU_RETRY: |
Dave Chinner | 5cedf721 | 2013-08-28 10:18:01 +1000 | [diff] [blame] | 136 | /* |
| 137 | * The lru lock has been dropped, our list traversal is |
| 138 | * now invalid and so we have to restart from scratch. |
| 139 | */ |
Johannes Weiner | 449dd69 | 2014-04-03 14:47:56 -0700 | [diff] [blame] | 140 | assert_spin_locked(&nlru->lock); |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 141 | goto restart; |
| 142 | default: |
| 143 | BUG(); |
| 144 | } |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 145 | } |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 146 | |
| 147 | spin_unlock(&nlru->lock); |
| 148 | return isolated; |
| 149 | } |
| 150 | EXPORT_SYMBOL_GPL(list_lru_walk_node); |
| 151 | |
Johannes Weiner | 449dd69 | 2014-04-03 14:47:56 -0700 | [diff] [blame] | 152 | int list_lru_init_key(struct list_lru *lru, struct lock_class_key *key) |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 153 | { |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 154 | int i; |
Glauber Costa | 5ca302c | 2013-08-28 10:18:18 +1000 | [diff] [blame] | 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; |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 160 | |
Glauber Costa | 5ca302c | 2013-08-28 10:18:18 +1000 | [diff] [blame] | 161 | for (i = 0; i < nr_node_ids; i++) { |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 162 | spin_lock_init(&lru->node[i].lock); |
Johannes Weiner | 449dd69 | 2014-04-03 14:47:56 -0700 | [diff] [blame] | 163 | if (key) |
| 164 | lockdep_set_class(&lru->node[i].lock, key); |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 165 | INIT_LIST_HEAD(&lru->node[i].list); |
| 166 | lru->node[i].nr_items = 0; |
| 167 | } |
Vladimir Davydov | c0a5b56 | 2015-02-12 14:59:07 -0800 | [diff] [blame^] | 168 | list_lru_register(lru); |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 169 | return 0; |
| 170 | } |
Johannes Weiner | 449dd69 | 2014-04-03 14:47:56 -0700 | [diff] [blame] | 171 | EXPORT_SYMBOL_GPL(list_lru_init_key); |
Glauber Costa | 5ca302c | 2013-08-28 10:18:18 +1000 | [diff] [blame] | 172 | |
| 173 | void list_lru_destroy(struct list_lru *lru) |
| 174 | { |
Vladimir Davydov | c0a5b56 | 2015-02-12 14:59:07 -0800 | [diff] [blame^] | 175 | /* Already destroyed or not yet initialized? */ |
| 176 | if (!lru->node) |
| 177 | return; |
| 178 | list_lru_unregister(lru); |
Glauber Costa | 5ca302c | 2013-08-28 10:18:18 +1000 | [diff] [blame] | 179 | kfree(lru->node); |
Vladimir Davydov | c0a5b56 | 2015-02-12 14:59:07 -0800 | [diff] [blame^] | 180 | lru->node = NULL; |
Glauber Costa | 5ca302c | 2013-08-28 10:18:18 +1000 | [diff] [blame] | 181 | } |
| 182 | EXPORT_SYMBOL_GPL(list_lru_destroy); |