blob: f500a2e39b13390df5ef505680ab62343f7256f8 [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#ifndef _LRU_LIST_H
8#define _LRU_LIST_H
9
10#include <linux/list.h>
Dave Chinner3b1d58a2013-08-28 10:18:00 +100011#include <linux/nodemask.h>
Vladimir Davydov503c3582015-02-12 14:58:47 -080012#include <linux/shrinker.h>
Dave Chinnera38e4082013-08-28 10:17:58 +100013
14/* list_lru_walk_cb has to always return one of those */
15enum lru_status {
16 LRU_REMOVED, /* item removed from list */
Johannes Weiner449dd692014-04-03 14:47:56 -070017 LRU_REMOVED_RETRY, /* item removed, but lock has been
18 dropped and reacquired */
Dave Chinnera38e4082013-08-28 10:17:58 +100019 LRU_ROTATE, /* item referenced, give another pass */
20 LRU_SKIP, /* item cannot be locked, skip */
21 LRU_RETRY, /* item not freeable. May drop the lock
22 internally, but has to return locked. */
23};
24
Dave Chinner3b1d58a2013-08-28 10:18:00 +100025struct list_lru_node {
Dave Chinnera38e4082013-08-28 10:17:58 +100026 spinlock_t lock;
27 struct list_head list;
28 /* kept as signed so we can catch imbalance bugs */
29 long nr_items;
Dave Chinner3b1d58a2013-08-28 10:18:00 +100030} ____cacheline_aligned_in_smp;
31
32struct list_lru {
Glauber Costa5ca302c2013-08-28 10:18:18 +100033 struct list_lru_node *node;
Dave Chinner3b1d58a2013-08-28 10:18:00 +100034 nodemask_t active_nodes;
Dave Chinnera38e4082013-08-28 10:17:58 +100035};
36
Glauber Costa5ca302c2013-08-28 10:18:18 +100037void list_lru_destroy(struct list_lru *lru);
Johannes Weiner449dd692014-04-03 14:47:56 -070038int list_lru_init_key(struct list_lru *lru, struct lock_class_key *key);
39static inline int list_lru_init(struct list_lru *lru)
40{
41 return list_lru_init_key(lru, NULL);
42}
Dave Chinnera38e4082013-08-28 10:17:58 +100043
44/**
45 * list_lru_add: add an element to the lru list's tail
46 * @list_lru: the lru pointer
47 * @item: the item to be added.
48 *
49 * If the element is already part of a list, this function returns doing
50 * nothing. Therefore the caller does not need to keep state about whether or
51 * not the element already belongs in the list and is allowed to lazy update
52 * it. Note however that this is valid for *a* list, not *this* list. If
53 * the caller organize itself in a way that elements can be in more than
54 * one type of list, it is up to the caller to fully remove the item from
55 * the previous list (with list_lru_del() for instance) before moving it
56 * to @list_lru
57 *
58 * Return value: true if the list was updated, false otherwise
59 */
60bool list_lru_add(struct list_lru *lru, struct list_head *item);
61
62/**
63 * list_lru_del: delete an element to the lru list
64 * @list_lru: the lru pointer
65 * @item: the item to be deleted.
66 *
67 * This function works analogously as list_lru_add in terms of list
68 * manipulation. The comments about an element already pertaining to
69 * a list are also valid for list_lru_del.
70 *
71 * Return value: true if the list was updated, false otherwise
72 */
73bool list_lru_del(struct list_lru *lru, struct list_head *item);
74
75/**
Glauber Costa6a4f4962013-08-28 10:18:02 +100076 * list_lru_count_node: return the number of objects currently held by @lru
Dave Chinnera38e4082013-08-28 10:17:58 +100077 * @lru: the lru pointer.
Glauber Costa6a4f4962013-08-28 10:18:02 +100078 * @nid: the node id to count from.
Dave Chinnera38e4082013-08-28 10:17:58 +100079 *
80 * Always return a non-negative number, 0 for empty lists. There is no
81 * guarantee that the list is not updated while the count is being computed.
82 * Callers that want such a guarantee need to provide an outer lock.
83 */
Glauber Costa6a4f4962013-08-28 10:18:02 +100084unsigned long list_lru_count_node(struct list_lru *lru, int nid);
Vladimir Davydov503c3582015-02-12 14:58:47 -080085
86static inline unsigned long list_lru_shrink_count(struct list_lru *lru,
87 struct shrink_control *sc)
88{
89 return list_lru_count_node(lru, sc->nid);
90}
91
Glauber Costa6a4f4962013-08-28 10:18:02 +100092static inline unsigned long list_lru_count(struct list_lru *lru)
93{
94 long count = 0;
95 int nid;
96
97 for_each_node_mask(nid, lru->active_nodes)
98 count += list_lru_count_node(lru, nid);
99
100 return count;
101}
Dave Chinnera38e4082013-08-28 10:17:58 +1000102
103typedef enum lru_status
104(*list_lru_walk_cb)(struct list_head *item, spinlock_t *lock, void *cb_arg);
105/**
Glauber Costa6a4f4962013-08-28 10:18:02 +1000106 * list_lru_walk_node: walk a list_lru, isolating and disposing freeable items.
Dave Chinnera38e4082013-08-28 10:17:58 +1000107 * @lru: the lru pointer.
Glauber Costa6a4f4962013-08-28 10:18:02 +1000108 * @nid: the node id to scan from.
Dave Chinnera38e4082013-08-28 10:17:58 +1000109 * @isolate: callback function that is resposible for deciding what to do with
110 * the item currently being scanned
111 * @cb_arg: opaque type that will be passed to @isolate
112 * @nr_to_walk: how many items to scan.
113 *
114 * This function will scan all elements in a particular list_lru, calling the
115 * @isolate callback for each of those items, along with the current list
116 * spinlock and a caller-provided opaque. The @isolate callback can choose to
117 * drop the lock internally, but *must* return with the lock held. The callback
118 * will return an enum lru_status telling the list_lru infrastructure what to
119 * do with the object being scanned.
120 *
121 * Please note that nr_to_walk does not mean how many objects will be freed,
122 * just how many objects will be scanned.
123 *
124 * Return value: the number of objects effectively removed from the LRU.
125 */
Glauber Costa6a4f4962013-08-28 10:18:02 +1000126unsigned long list_lru_walk_node(struct list_lru *lru, int nid,
127 list_lru_walk_cb isolate, void *cb_arg,
128 unsigned long *nr_to_walk);
129
130static inline unsigned long
Vladimir Davydov503c3582015-02-12 14:58:47 -0800131list_lru_shrink_walk(struct list_lru *lru, struct shrink_control *sc,
132 list_lru_walk_cb isolate, void *cb_arg)
133{
134 return list_lru_walk_node(lru, sc->nid, isolate, cb_arg,
135 &sc->nr_to_scan);
136}
137
138static inline unsigned long
Glauber Costa6a4f4962013-08-28 10:18:02 +1000139list_lru_walk(struct list_lru *lru, list_lru_walk_cb isolate,
140 void *cb_arg, unsigned long nr_to_walk)
141{
142 long isolated = 0;
143 int nid;
144
145 for_each_node_mask(nid, lru->active_nodes) {
146 isolated += list_lru_walk_node(lru, nid, isolate,
147 cb_arg, &nr_to_walk);
148 if (nr_to_walk <= 0)
149 break;
150 }
151 return isolated;
152}
Dave Chinnera38e4082013-08-28 10:17:58 +1000153#endif /* _LRU_LIST_H */