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