License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[linux-block.git] / include / linux / list_lru.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
a38e4082
DC
2/*
3 * Copyright (c) 2013 Red Hat, Inc. and Parallels Inc. All rights reserved.
4 * Authors: David Chinner and Glauber Costa
5 *
6 * Generic LRU infrastructure
7 */
8#ifndef _LRU_LIST_H
9#define _LRU_LIST_H
10
11#include <linux/list.h>
3b1d58a4 12#include <linux/nodemask.h>
503c358c 13#include <linux/shrinker.h>
a38e4082 14
60d3fd32
VD
15struct mem_cgroup;
16
a38e4082
DC
17/* list_lru_walk_cb has to always return one of those */
18enum lru_status {
19 LRU_REMOVED, /* item removed from list */
449dd698
JW
20 LRU_REMOVED_RETRY, /* item removed, but lock has been
21 dropped and reacquired */
a38e4082
DC
22 LRU_ROTATE, /* item referenced, give another pass */
23 LRU_SKIP, /* item cannot be locked, skip */
24 LRU_RETRY, /* item not freeable. May drop the lock
25 internally, but has to return locked. */
26};
27
60d3fd32 28struct list_lru_one {
a38e4082 29 struct list_head list;
2788cf0c 30 /* may become negative during memcg reparenting */
a38e4082 31 long nr_items;
60d3fd32
VD
32};
33
34struct list_lru_memcg {
35 /* array of per cgroup lists, indexed by memcg_cache_id */
36 struct list_lru_one *lru[0];
37};
38
39struct list_lru_node {
40 /* protects all lists on the node, including per cgroup */
41 spinlock_t lock;
42 /* global list, used for the root cgroup in cgroup aware lrus */
43 struct list_lru_one lru;
127424c8 44#if defined(CONFIG_MEMCG) && !defined(CONFIG_SLOB)
60d3fd32
VD
45 /* for cgroup aware lrus points to per cgroup lists, otherwise NULL */
46 struct list_lru_memcg *memcg_lrus;
47#endif
2c80cd57 48 long nr_items;
3b1d58a4
DC
49} ____cacheline_aligned_in_smp;
50
51struct list_lru {
5ca302c8 52 struct list_lru_node *node;
127424c8 53#if defined(CONFIG_MEMCG) && !defined(CONFIG_SLOB)
c0a5b560
VD
54 struct list_head list;
55#endif
a38e4082
DC
56};
57
5ca302c8 58void list_lru_destroy(struct list_lru *lru);
60d3fd32
VD
59int __list_lru_init(struct list_lru *lru, bool memcg_aware,
60 struct lock_class_key *key);
61
62#define list_lru_init(lru) __list_lru_init((lru), false, NULL)
63#define list_lru_init_key(lru, key) __list_lru_init((lru), false, (key))
64#define list_lru_init_memcg(lru) __list_lru_init((lru), true, NULL)
65
66int memcg_update_all_list_lrus(int num_memcgs);
2788cf0c 67void memcg_drain_all_list_lrus(int src_idx, int dst_idx);
a38e4082
DC
68
69/**
70 * list_lru_add: add an element to the lru list's tail
71 * @list_lru: the lru pointer
72 * @item: the item to be added.
73 *
74 * If the element is already part of a list, this function returns doing
75 * nothing. Therefore the caller does not need to keep state about whether or
76 * not the element already belongs in the list and is allowed to lazy update
77 * it. Note however that this is valid for *a* list, not *this* list. If
78 * the caller organize itself in a way that elements can be in more than
79 * one type of list, it is up to the caller to fully remove the item from
80 * the previous list (with list_lru_del() for instance) before moving it
81 * to @list_lru
82 *
83 * Return value: true if the list was updated, false otherwise
84 */
85bool list_lru_add(struct list_lru *lru, struct list_head *item);
86
87/**
88 * list_lru_del: delete an element to the lru list
89 * @list_lru: the lru pointer
90 * @item: the item to be deleted.
91 *
92 * This function works analogously as list_lru_add in terms of list
93 * manipulation. The comments about an element already pertaining to
94 * a list are also valid for list_lru_del.
95 *
96 * Return value: true if the list was updated, false otherwise
97 */
98bool list_lru_del(struct list_lru *lru, struct list_head *item);
99
100/**
60d3fd32 101 * list_lru_count_one: return the number of objects currently held by @lru
a38e4082 102 * @lru: the lru pointer.
6a4f496f 103 * @nid: the node id to count from.
60d3fd32 104 * @memcg: the cgroup to count from.
a38e4082
DC
105 *
106 * Always return a non-negative number, 0 for empty lists. There is no
107 * guarantee that the list is not updated while the count is being computed.
108 * Callers that want such a guarantee need to provide an outer lock.
109 */
60d3fd32
VD
110unsigned long list_lru_count_one(struct list_lru *lru,
111 int nid, struct mem_cgroup *memcg);
6a4f496f 112unsigned long list_lru_count_node(struct list_lru *lru, int nid);
503c358c
VD
113
114static inline unsigned long list_lru_shrink_count(struct list_lru *lru,
115 struct shrink_control *sc)
116{
60d3fd32 117 return list_lru_count_one(lru, sc->nid, sc->memcg);
503c358c
VD
118}
119
6a4f496f
GC
120static inline unsigned long list_lru_count(struct list_lru *lru)
121{
122 long count = 0;
123 int nid;
124
ff0b67ef 125 for_each_node_state(nid, N_NORMAL_MEMORY)
6a4f496f
GC
126 count += list_lru_count_node(lru, nid);
127
128 return count;
129}
a38e4082 130
3f97b163
VD
131void list_lru_isolate(struct list_lru_one *list, struct list_head *item);
132void list_lru_isolate_move(struct list_lru_one *list, struct list_head *item,
133 struct list_head *head);
134
135typedef enum lru_status (*list_lru_walk_cb)(struct list_head *item,
136 struct list_lru_one *list, spinlock_t *lock, void *cb_arg);
137
a38e4082 138/**
60d3fd32 139 * list_lru_walk_one: walk a list_lru, isolating and disposing freeable items.
a38e4082 140 * @lru: the lru pointer.
6a4f496f 141 * @nid: the node id to scan from.
60d3fd32 142 * @memcg: the cgroup to scan from.
a38e4082
DC
143 * @isolate: callback function that is resposible for deciding what to do with
144 * the item currently being scanned
145 * @cb_arg: opaque type that will be passed to @isolate
146 * @nr_to_walk: how many items to scan.
147 *
148 * This function will scan all elements in a particular list_lru, calling the
149 * @isolate callback for each of those items, along with the current list
150 * spinlock and a caller-provided opaque. The @isolate callback can choose to
151 * drop the lock internally, but *must* return with the lock held. The callback
152 * will return an enum lru_status telling the list_lru infrastructure what to
153 * do with the object being scanned.
154 *
155 * Please note that nr_to_walk does not mean how many objects will be freed,
156 * just how many objects will be scanned.
157 *
158 * Return value: the number of objects effectively removed from the LRU.
159 */
60d3fd32
VD
160unsigned long list_lru_walk_one(struct list_lru *lru,
161 int nid, struct mem_cgroup *memcg,
162 list_lru_walk_cb isolate, void *cb_arg,
163 unsigned long *nr_to_walk);
6a4f496f
GC
164unsigned long list_lru_walk_node(struct list_lru *lru, int nid,
165 list_lru_walk_cb isolate, void *cb_arg,
166 unsigned long *nr_to_walk);
167
503c358c
VD
168static inline unsigned long
169list_lru_shrink_walk(struct list_lru *lru, struct shrink_control *sc,
170 list_lru_walk_cb isolate, void *cb_arg)
171{
60d3fd32
VD
172 return list_lru_walk_one(lru, sc->nid, sc->memcg, isolate, cb_arg,
173 &sc->nr_to_scan);
503c358c
VD
174}
175
6a4f496f
GC
176static inline unsigned long
177list_lru_walk(struct list_lru *lru, list_lru_walk_cb isolate,
178 void *cb_arg, unsigned long nr_to_walk)
179{
180 long isolated = 0;
181 int nid;
182
ff0b67ef 183 for_each_node_state(nid, N_NORMAL_MEMORY) {
6a4f496f
GC
184 isolated += list_lru_walk_node(lru, nid, isolate,
185 cb_arg, &nr_to_walk);
186 if (nr_to_walk <= 0)
187 break;
188 }
189 return isolated;
190}
a38e4082 191#endif /* _LRU_LIST_H */