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> |
| 11 | |
| 12 | bool list_lru_add(struct list_lru *lru, struct list_head *item) |
| 13 | { |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 14 | int nid = page_to_nid(virt_to_page(item)); |
| 15 | struct list_lru_node *nlru = &lru->node[nid]; |
| 16 | |
| 17 | spin_lock(&nlru->lock); |
| 18 | WARN_ON_ONCE(nlru->nr_items < 0); |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 19 | if (list_empty(item)) { |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 20 | list_add_tail(item, &nlru->list); |
| 21 | if (nlru->nr_items++ == 0) |
| 22 | node_set(nid, lru->active_nodes); |
| 23 | spin_unlock(&nlru->lock); |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 24 | return true; |
| 25 | } |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 26 | spin_unlock(&nlru->lock); |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 27 | return false; |
| 28 | } |
| 29 | EXPORT_SYMBOL_GPL(list_lru_add); |
| 30 | |
| 31 | bool list_lru_del(struct list_lru *lru, struct list_head *item) |
| 32 | { |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 33 | int nid = page_to_nid(virt_to_page(item)); |
| 34 | struct list_lru_node *nlru = &lru->node[nid]; |
| 35 | |
| 36 | spin_lock(&nlru->lock); |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 37 | if (!list_empty(item)) { |
| 38 | list_del_init(item); |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 39 | if (--nlru->nr_items == 0) |
| 40 | node_clear(nid, lru->active_nodes); |
| 41 | WARN_ON_ONCE(nlru->nr_items < 0); |
| 42 | spin_unlock(&nlru->lock); |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 43 | return true; |
| 44 | } |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 45 | spin_unlock(&nlru->lock); |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 46 | return false; |
| 47 | } |
| 48 | EXPORT_SYMBOL_GPL(list_lru_del); |
| 49 | |
Glauber Costa | 6a4f496 | 2013-08-28 10:18:02 +1000 | [diff] [blame^] | 50 | unsigned long |
| 51 | list_lru_count_node(struct list_lru *lru, int nid) |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 52 | { |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 53 | unsigned long count = 0; |
Glauber Costa | 6a4f496 | 2013-08-28 10:18:02 +1000 | [diff] [blame^] | 54 | struct list_lru_node *nlru = &lru->node[nid]; |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 55 | |
Glauber Costa | 6a4f496 | 2013-08-28 10:18:02 +1000 | [diff] [blame^] | 56 | spin_lock(&nlru->lock); |
| 57 | WARN_ON_ONCE(nlru->nr_items < 0); |
| 58 | count += nlru->nr_items; |
| 59 | spin_unlock(&nlru->lock); |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 60 | |
| 61 | return count; |
| 62 | } |
Glauber Costa | 6a4f496 | 2013-08-28 10:18:02 +1000 | [diff] [blame^] | 63 | EXPORT_SYMBOL_GPL(list_lru_count_node); |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 64 | |
Glauber Costa | 6a4f496 | 2013-08-28 10:18:02 +1000 | [diff] [blame^] | 65 | unsigned long |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 66 | list_lru_walk_node(struct list_lru *lru, int nid, list_lru_walk_cb isolate, |
| 67 | void *cb_arg, unsigned long *nr_to_walk) |
| 68 | { |
| 69 | |
| 70 | struct list_lru_node *nlru = &lru->node[nid]; |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 71 | struct list_head *item, *n; |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 72 | unsigned long isolated = 0; |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 73 | |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 74 | spin_lock(&nlru->lock); |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 75 | restart: |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 76 | list_for_each_safe(item, n, &nlru->list) { |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 77 | enum lru_status ret; |
Dave Chinner | 5cedf721 | 2013-08-28 10:18:01 +1000 | [diff] [blame] | 78 | |
| 79 | /* |
| 80 | * decrement nr_to_walk first so that we don't livelock if we |
| 81 | * get stuck on large numbesr of LRU_RETRY items |
| 82 | */ |
| 83 | if (--(*nr_to_walk) == 0) |
| 84 | break; |
| 85 | |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 86 | ret = isolate(item, &nlru->lock, cb_arg); |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 87 | switch (ret) { |
| 88 | case LRU_REMOVED: |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 89 | if (--nlru->nr_items == 0) |
| 90 | node_clear(nid, lru->active_nodes); |
| 91 | WARN_ON_ONCE(nlru->nr_items < 0); |
| 92 | isolated++; |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 93 | break; |
| 94 | case LRU_ROTATE: |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 95 | list_move_tail(item, &nlru->list); |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 96 | break; |
| 97 | case LRU_SKIP: |
| 98 | break; |
| 99 | case LRU_RETRY: |
Dave Chinner | 5cedf721 | 2013-08-28 10:18:01 +1000 | [diff] [blame] | 100 | /* |
| 101 | * The lru lock has been dropped, our list traversal is |
| 102 | * now invalid and so we have to restart from scratch. |
| 103 | */ |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 104 | goto restart; |
| 105 | default: |
| 106 | BUG(); |
| 107 | } |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 108 | } |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 109 | |
| 110 | spin_unlock(&nlru->lock); |
| 111 | return isolated; |
| 112 | } |
| 113 | EXPORT_SYMBOL_GPL(list_lru_walk_node); |
| 114 | |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 115 | static unsigned long list_lru_dispose_all_node(struct list_lru *lru, int nid, |
| 116 | list_lru_dispose_cb dispose) |
| 117 | { |
| 118 | struct list_lru_node *nlru = &lru->node[nid]; |
| 119 | LIST_HEAD(dispose_list); |
| 120 | unsigned long disposed = 0; |
| 121 | |
| 122 | spin_lock(&nlru->lock); |
| 123 | while (!list_empty(&nlru->list)) { |
| 124 | list_splice_init(&nlru->list, &dispose_list); |
| 125 | disposed += nlru->nr_items; |
| 126 | nlru->nr_items = 0; |
| 127 | node_clear(nid, lru->active_nodes); |
| 128 | spin_unlock(&nlru->lock); |
| 129 | |
| 130 | dispose(&dispose_list); |
| 131 | |
| 132 | spin_lock(&nlru->lock); |
| 133 | } |
| 134 | spin_unlock(&nlru->lock); |
| 135 | return disposed; |
| 136 | } |
| 137 | |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 138 | unsigned long list_lru_dispose_all(struct list_lru *lru, |
| 139 | list_lru_dispose_cb dispose) |
| 140 | { |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 141 | unsigned long disposed; |
| 142 | unsigned long total = 0; |
| 143 | int nid; |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 144 | |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 145 | do { |
| 146 | disposed = 0; |
| 147 | for_each_node_mask(nid, lru->active_nodes) { |
| 148 | disposed += list_lru_dispose_all_node(lru, nid, |
| 149 | dispose); |
| 150 | } |
| 151 | total += disposed; |
| 152 | } while (disposed != 0); |
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 | return total; |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | int list_lru_init(struct list_lru *lru) |
| 158 | { |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 159 | int i; |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 160 | |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 161 | nodes_clear(lru->active_nodes); |
| 162 | for (i = 0; i < MAX_NUMNODES; i++) { |
| 163 | spin_lock_init(&lru->node[i].lock); |
| 164 | INIT_LIST_HEAD(&lru->node[i].list); |
| 165 | lru->node[i].nr_items = 0; |
| 166 | } |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 167 | return 0; |
| 168 | } |
| 169 | EXPORT_SYMBOL_GPL(list_lru_init); |