blob: f1a0db1941735e1fdf3cfe4da7153573f0cad877 [file] [log] [blame]
Dave Chinnera38e4082013-08-28 10:17:58 +10001/*
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 Chinner3b1d58a2013-08-28 10:18:00 +10009#include <linux/mm.h>
Dave Chinnera38e4082013-08-28 10:17:58 +100010#include <linux/list_lru.h>
Glauber Costa5ca302c2013-08-28 10:18:18 +100011#include <linux/slab.h>
Dave Chinnera38e4082013-08-28 10:17:58 +100012
13bool list_lru_add(struct list_lru *lru, struct list_head *item)
14{
Dave Chinner3b1d58a2013-08-28 10:18:00 +100015 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 Chinnera38e4082013-08-28 10:17:58 +100020 if (list_empty(item)) {
Dave Chinner3b1d58a2013-08-28 10:18:00 +100021 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 Chinnera38e4082013-08-28 10:17:58 +100025 return true;
26 }
Dave Chinner3b1d58a2013-08-28 10:18:00 +100027 spin_unlock(&nlru->lock);
Dave Chinnera38e4082013-08-28 10:17:58 +100028 return false;
29}
30EXPORT_SYMBOL_GPL(list_lru_add);
31
32bool list_lru_del(struct list_lru *lru, struct list_head *item)
33{
Dave Chinner3b1d58a2013-08-28 10:18:00 +100034 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 Chinnera38e4082013-08-28 10:17:58 +100038 if (!list_empty(item)) {
39 list_del_init(item);
Dave Chinner3b1d58a2013-08-28 10:18:00 +100040 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 Chinnera38e4082013-08-28 10:17:58 +100044 return true;
45 }
Dave Chinner3b1d58a2013-08-28 10:18:00 +100046 spin_unlock(&nlru->lock);
Dave Chinnera38e4082013-08-28 10:17:58 +100047 return false;
48}
49EXPORT_SYMBOL_GPL(list_lru_del);
50
Glauber Costa6a4f4962013-08-28 10:18:02 +100051unsigned long
52list_lru_count_node(struct list_lru *lru, int nid)
Dave Chinnera38e4082013-08-28 10:17:58 +100053{
Dave Chinner3b1d58a2013-08-28 10:18:00 +100054 unsigned long count = 0;
Glauber Costa6a4f4962013-08-28 10:18:02 +100055 struct list_lru_node *nlru = &lru->node[nid];
Dave Chinner3b1d58a2013-08-28 10:18:00 +100056
Glauber Costa6a4f4962013-08-28 10:18:02 +100057 spin_lock(&nlru->lock);
58 WARN_ON_ONCE(nlru->nr_items < 0);
59 count += nlru->nr_items;
60 spin_unlock(&nlru->lock);
Dave Chinner3b1d58a2013-08-28 10:18:00 +100061
62 return count;
63}
Glauber Costa6a4f4962013-08-28 10:18:02 +100064EXPORT_SYMBOL_GPL(list_lru_count_node);
Dave Chinner3b1d58a2013-08-28 10:18:00 +100065
Glauber Costa6a4f4962013-08-28 10:18:02 +100066unsigned long
Dave Chinner3b1d58a2013-08-28 10:18:00 +100067list_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 Chinnera38e4082013-08-28 10:17:58 +100072 struct list_head *item, *n;
Dave Chinner3b1d58a2013-08-28 10:18:00 +100073 unsigned long isolated = 0;
Dave Chinnera38e4082013-08-28 10:17:58 +100074
Dave Chinner3b1d58a2013-08-28 10:18:00 +100075 spin_lock(&nlru->lock);
Dave Chinnera38e4082013-08-28 10:17:58 +100076restart:
Dave Chinner3b1d58a2013-08-28 10:18:00 +100077 list_for_each_safe(item, n, &nlru->list) {
Dave Chinnera38e4082013-08-28 10:17:58 +100078 enum lru_status ret;
Dave Chinner5cedf7212013-08-28 10:18:01 +100079
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 Kingc56b0972013-10-30 14:16:16 +000084 if (!*nr_to_walk)
Dave Chinner5cedf7212013-08-28 10:18:01 +100085 break;
Russell Kingc56b0972013-10-30 14:16:16 +000086 --*nr_to_walk;
Dave Chinner5cedf7212013-08-28 10:18:01 +100087
Dave Chinner3b1d58a2013-08-28 10:18:00 +100088 ret = isolate(item, &nlru->lock, cb_arg);
Dave Chinnera38e4082013-08-28 10:17:58 +100089 switch (ret) {
Johannes Weiner449dd692014-04-03 14:47:56 -070090 case LRU_REMOVED_RETRY:
91 assert_spin_locked(&nlru->lock);
Dave Chinnera38e4082013-08-28 10:17:58 +100092 case LRU_REMOVED:
Dave Chinner3b1d58a2013-08-28 10:18:00 +100093 if (--nlru->nr_items == 0)
94 node_clear(nid, lru->active_nodes);
95 WARN_ON_ONCE(nlru->nr_items < 0);
96 isolated++;
Johannes Weiner449dd692014-04-03 14:47:56 -070097 /*
98 * If the lru lock has been dropped, our list
99 * traversal is now invalid and so we have to
100 * restart from scratch.
101 */
102 if (ret == LRU_REMOVED_RETRY)
103 goto restart;
Dave Chinnera38e4082013-08-28 10:17:58 +1000104 break;
105 case LRU_ROTATE:
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000106 list_move_tail(item, &nlru->list);
Dave Chinnera38e4082013-08-28 10:17:58 +1000107 break;
108 case LRU_SKIP:
109 break;
110 case LRU_RETRY:
Dave Chinner5cedf7212013-08-28 10:18:01 +1000111 /*
112 * The lru lock has been dropped, our list traversal is
113 * now invalid and so we have to restart from scratch.
114 */
Johannes Weiner449dd692014-04-03 14:47:56 -0700115 assert_spin_locked(&nlru->lock);
Dave Chinnera38e4082013-08-28 10:17:58 +1000116 goto restart;
117 default:
118 BUG();
119 }
Dave Chinnera38e4082013-08-28 10:17:58 +1000120 }
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000121
122 spin_unlock(&nlru->lock);
123 return isolated;
124}
125EXPORT_SYMBOL_GPL(list_lru_walk_node);
126
Johannes Weiner449dd692014-04-03 14:47:56 -0700127int list_lru_init_key(struct list_lru *lru, struct lock_class_key *key)
Dave Chinnera38e4082013-08-28 10:17:58 +1000128{
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000129 int i;
Glauber Costa5ca302c2013-08-28 10:18:18 +1000130 size_t size = sizeof(*lru->node) * nr_node_ids;
131
132 lru->node = kzalloc(size, GFP_KERNEL);
133 if (!lru->node)
134 return -ENOMEM;
Dave Chinnera38e4082013-08-28 10:17:58 +1000135
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000136 nodes_clear(lru->active_nodes);
Glauber Costa5ca302c2013-08-28 10:18:18 +1000137 for (i = 0; i < nr_node_ids; i++) {
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000138 spin_lock_init(&lru->node[i].lock);
Johannes Weiner449dd692014-04-03 14:47:56 -0700139 if (key)
140 lockdep_set_class(&lru->node[i].lock, key);
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000141 INIT_LIST_HEAD(&lru->node[i].list);
142 lru->node[i].nr_items = 0;
143 }
Dave Chinnera38e4082013-08-28 10:17:58 +1000144 return 0;
145}
Johannes Weiner449dd692014-04-03 14:47:56 -0700146EXPORT_SYMBOL_GPL(list_lru_init_key);
Glauber Costa5ca302c2013-08-28 10:18:18 +1000147
148void list_lru_destroy(struct list_lru *lru)
149{
150 kfree(lru->node);
151}
152EXPORT_SYMBOL_GPL(list_lru_destroy);