]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - include/linux/list_lru.h
list_lru: get rid of ->active_nodes
[mirror_ubuntu-bionic-kernel.git] / include / linux / list_lru.h
CommitLineData
a38e4082
DC
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#ifndef _LRU_LIST_H
8#define _LRU_LIST_H
9
10#include <linux/list.h>
3b1d58a4 11#include <linux/nodemask.h>
503c358c 12#include <linux/shrinker.h>
a38e4082
DC
13
14/* list_lru_walk_cb has to always return one of those */
15enum lru_status {
16 LRU_REMOVED, /* item removed from list */
449dd698
JW
17 LRU_REMOVED_RETRY, /* item removed, but lock has been
18 dropped and reacquired */
a38e4082
DC
19 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
3b1d58a4 25struct list_lru_node {
a38e4082
DC
26 spinlock_t lock;
27 struct list_head list;
28 /* kept as signed so we can catch imbalance bugs */
29 long nr_items;
3b1d58a4
DC
30} ____cacheline_aligned_in_smp;
31
32struct list_lru {
5ca302c8 33 struct list_lru_node *node;
a38e4082
DC
34};
35
5ca302c8 36void list_lru_destroy(struct list_lru *lru);
449dd698
JW
37int list_lru_init_key(struct list_lru *lru, struct lock_class_key *key);
38static inline int list_lru_init(struct list_lru *lru)
39{
40 return list_lru_init_key(lru, NULL);
41}
a38e4082
DC
42
43/**
44 * list_lru_add: add an element to the lru list's tail
45 * @list_lru: the lru pointer
46 * @item: the item to be added.
47 *
48 * If the element is already part of a list, this function returns doing
49 * nothing. Therefore the caller does not need to keep state about whether or
50 * not the element already belongs in the list and is allowed to lazy update
51 * it. Note however that this is valid for *a* list, not *this* list. If
52 * the caller organize itself in a way that elements can be in more than
53 * one type of list, it is up to the caller to fully remove the item from
54 * the previous list (with list_lru_del() for instance) before moving it
55 * to @list_lru
56 *
57 * Return value: true if the list was updated, false otherwise
58 */
59bool list_lru_add(struct list_lru *lru, struct list_head *item);
60
61/**
62 * list_lru_del: delete an element to the lru list
63 * @list_lru: the lru pointer
64 * @item: the item to be deleted.
65 *
66 * This function works analogously as list_lru_add in terms of list
67 * manipulation. The comments about an element already pertaining to
68 * a list are also valid for list_lru_del.
69 *
70 * Return value: true if the list was updated, false otherwise
71 */
72bool list_lru_del(struct list_lru *lru, struct list_head *item);
73
74/**
6a4f496f 75 * list_lru_count_node: return the number of objects currently held by @lru
a38e4082 76 * @lru: the lru pointer.
6a4f496f 77 * @nid: the node id to count from.
a38e4082
DC
78 *
79 * Always return a non-negative number, 0 for empty lists. There is no
80 * guarantee that the list is not updated while the count is being computed.
81 * Callers that want such a guarantee need to provide an outer lock.
82 */
6a4f496f 83unsigned long list_lru_count_node(struct list_lru *lru, int nid);
503c358c
VD
84
85static inline unsigned long list_lru_shrink_count(struct list_lru *lru,
86 struct shrink_control *sc)
87{
88 return list_lru_count_node(lru, sc->nid);
89}
90
6a4f496f
GC
91static inline unsigned long list_lru_count(struct list_lru *lru)
92{
93 long count = 0;
94 int nid;
95
ff0b67ef 96 for_each_node_state(nid, N_NORMAL_MEMORY)
6a4f496f
GC
97 count += list_lru_count_node(lru, nid);
98
99 return count;
100}
a38e4082
DC
101
102typedef enum lru_status
103(*list_lru_walk_cb)(struct list_head *item, spinlock_t *lock, void *cb_arg);
104/**
6a4f496f 105 * list_lru_walk_node: walk a list_lru, isolating and disposing freeable items.
a38e4082 106 * @lru: the lru pointer.
6a4f496f 107 * @nid: the node id to scan from.
a38e4082
DC
108 * @isolate: callback function that is resposible for deciding what to do with
109 * the item currently being scanned
110 * @cb_arg: opaque type that will be passed to @isolate
111 * @nr_to_walk: how many items to scan.
112 *
113 * This function will scan all elements in a particular list_lru, calling the
114 * @isolate callback for each of those items, along with the current list
115 * spinlock and a caller-provided opaque. The @isolate callback can choose to
116 * drop the lock internally, but *must* return with the lock held. The callback
117 * will return an enum lru_status telling the list_lru infrastructure what to
118 * do with the object being scanned.
119 *
120 * Please note that nr_to_walk does not mean how many objects will be freed,
121 * just how many objects will be scanned.
122 *
123 * Return value: the number of objects effectively removed from the LRU.
124 */
6a4f496f
GC
125unsigned long list_lru_walk_node(struct list_lru *lru, int nid,
126 list_lru_walk_cb isolate, void *cb_arg,
127 unsigned long *nr_to_walk);
128
503c358c
VD
129static inline unsigned long
130list_lru_shrink_walk(struct list_lru *lru, struct shrink_control *sc,
131 list_lru_walk_cb isolate, void *cb_arg)
132{
133 return list_lru_walk_node(lru, sc->nid, isolate, cb_arg,
134 &sc->nr_to_scan);
135}
136
6a4f496f
GC
137static inline unsigned long
138list_lru_walk(struct list_lru *lru, list_lru_walk_cb isolate,
139 void *cb_arg, unsigned long nr_to_walk)
140{
141 long isolated = 0;
142 int nid;
143
ff0b67ef 144 for_each_node_state(nid, N_NORMAL_MEMORY) {
6a4f496f
GC
145 isolated += list_lru_walk_node(lru, nid, isolate,
146 cb_arg, &nr_to_walk);
147 if (nr_to_walk <= 0)
148 break;
149 }
150 return isolated;
151}
a38e4082 152#endif /* _LRU_LIST_H */