blob: fa7fd03cb5f964c4be4b74f435c2fdb81ba58936 [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
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080014struct mem_cgroup;
15
Dave Chinnera38e4082013-08-28 10:17:58 +100016/* list_lru_walk_cb has to always return one of those */
17enum lru_status {
18 LRU_REMOVED, /* item removed from list */
Johannes Weiner449dd692014-04-03 14:47:56 -070019 LRU_REMOVED_RETRY, /* item removed, but lock has been
20 dropped and reacquired */
Dave Chinnera38e4082013-08-28 10:17:58 +100021 LRU_ROTATE, /* item referenced, give another pass */
22 LRU_SKIP, /* item cannot be locked, skip */
23 LRU_RETRY, /* item not freeable. May drop the lock
24 internally, but has to return locked. */
25};
26
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080027struct list_lru_one {
Dave Chinnera38e4082013-08-28 10:17:58 +100028 struct list_head list;
Vladimir Davydov2788cf02015-02-12 14:59:38 -080029 /* may become negative during memcg reparenting */
Dave Chinnera38e4082013-08-28 10:17:58 +100030 long nr_items;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080031};
32
33struct list_lru_memcg {
34 /* array of per cgroup lists, indexed by memcg_cache_id */
35 struct list_lru_one *lru[0];
36};
37
38struct list_lru_node {
39 /* protects all lists on the node, including per cgroup */
40 spinlock_t lock;
41 /* global list, used for the root cgroup in cgroup aware lrus */
42 struct list_lru_one lru;
Johannes Weiner127424c2016-01-20 15:02:32 -080043#if defined(CONFIG_MEMCG) && !defined(CONFIG_SLOB)
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080044 /* for cgroup aware lrus points to per cgroup lists, otherwise NULL */
45 struct list_lru_memcg *memcg_lrus;
46#endif
Sahitya Tummalaa48542e2017-07-10 15:49:57 -070047 long nr_items;
Dave Chinner3b1d58a2013-08-28 10:18:00 +100048} ____cacheline_aligned_in_smp;
49
50struct list_lru {
Glauber Costa5ca302c2013-08-28 10:18:18 +100051 struct list_lru_node *node;
Johannes Weiner127424c2016-01-20 15:02:32 -080052#if defined(CONFIG_MEMCG) && !defined(CONFIG_SLOB)
Vladimir Davydovc0a5b562015-02-12 14:59:07 -080053 struct list_head list;
54#endif
Dave Chinnera38e4082013-08-28 10:17:58 +100055};
56
Glauber Costa5ca302c2013-08-28 10:18:18 +100057void list_lru_destroy(struct list_lru *lru);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080058int __list_lru_init(struct list_lru *lru, bool memcg_aware,
59 struct lock_class_key *key);
60
61#define list_lru_init(lru) __list_lru_init((lru), false, NULL)
62#define list_lru_init_key(lru, key) __list_lru_init((lru), false, (key))
63#define list_lru_init_memcg(lru) __list_lru_init((lru), true, NULL)
64
65int memcg_update_all_list_lrus(int num_memcgs);
Vladimir Davydov2788cf02015-02-12 14:59:38 -080066void memcg_drain_all_list_lrus(int src_idx, int dst_idx);
Dave Chinnera38e4082013-08-28 10:17:58 +100067
68/**
69 * list_lru_add: add an element to the lru list's tail
70 * @list_lru: the lru pointer
71 * @item: the item to be added.
72 *
73 * If the element is already part of a list, this function returns doing
74 * nothing. Therefore the caller does not need to keep state about whether or
75 * not the element already belongs in the list and is allowed to lazy update
76 * it. Note however that this is valid for *a* list, not *this* list. If
77 * the caller organize itself in a way that elements can be in more than
78 * one type of list, it is up to the caller to fully remove the item from
79 * the previous list (with list_lru_del() for instance) before moving it
80 * to @list_lru
81 *
82 * Return value: true if the list was updated, false otherwise
83 */
84bool list_lru_add(struct list_lru *lru, struct list_head *item);
85
86/**
87 * list_lru_del: delete an element to the lru list
88 * @list_lru: the lru pointer
89 * @item: the item to be deleted.
90 *
91 * This function works analogously as list_lru_add in terms of list
92 * manipulation. The comments about an element already pertaining to
93 * a list are also valid for list_lru_del.
94 *
95 * Return value: true if the list was updated, false otherwise
96 */
97bool list_lru_del(struct list_lru *lru, struct list_head *item);
98
99/**
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800100 * list_lru_count_one: return the number of objects currently held by @lru
Dave Chinnera38e4082013-08-28 10:17:58 +1000101 * @lru: the lru pointer.
Glauber Costa6a4f4962013-08-28 10:18:02 +1000102 * @nid: the node id to count from.
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800103 * @memcg: the cgroup to count from.
Dave Chinnera38e4082013-08-28 10:17:58 +1000104 *
105 * Always return a non-negative number, 0 for empty lists. There is no
106 * guarantee that the list is not updated while the count is being computed.
107 * Callers that want such a guarantee need to provide an outer lock.
108 */
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800109unsigned long list_lru_count_one(struct list_lru *lru,
110 int nid, struct mem_cgroup *memcg);
Glauber Costa6a4f4962013-08-28 10:18:02 +1000111unsigned long list_lru_count_node(struct list_lru *lru, int nid);
Vladimir Davydov503c3582015-02-12 14:58:47 -0800112
113static inline unsigned long list_lru_shrink_count(struct list_lru *lru,
114 struct shrink_control *sc)
115{
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800116 return list_lru_count_one(lru, sc->nid, sc->memcg);
Vladimir Davydov503c3582015-02-12 14:58:47 -0800117}
118
Glauber Costa6a4f4962013-08-28 10:18:02 +1000119static inline unsigned long list_lru_count(struct list_lru *lru)
120{
121 long count = 0;
122 int nid;
123
Vladimir Davydovff0b67e2015-02-12 14:59:04 -0800124 for_each_node_state(nid, N_NORMAL_MEMORY)
Glauber Costa6a4f4962013-08-28 10:18:02 +1000125 count += list_lru_count_node(lru, nid);
126
127 return count;
128}
Dave Chinnera38e4082013-08-28 10:17:58 +1000129
Vladimir Davydov3f97b162015-02-12 14:59:35 -0800130void list_lru_isolate(struct list_lru_one *list, struct list_head *item);
131void list_lru_isolate_move(struct list_lru_one *list, struct list_head *item,
132 struct list_head *head);
133
134typedef enum lru_status (*list_lru_walk_cb)(struct list_head *item,
135 struct list_lru_one *list, spinlock_t *lock, void *cb_arg);
136
Dave Chinnera38e4082013-08-28 10:17:58 +1000137/**
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800138 * list_lru_walk_one: walk a list_lru, isolating and disposing freeable items.
Dave Chinnera38e4082013-08-28 10:17:58 +1000139 * @lru: the lru pointer.
Glauber Costa6a4f4962013-08-28 10:18:02 +1000140 * @nid: the node id to scan from.
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800141 * @memcg: the cgroup to scan from.
Dave Chinnera38e4082013-08-28 10:17:58 +1000142 * @isolate: callback function that is resposible for deciding what to do with
143 * the item currently being scanned
144 * @cb_arg: opaque type that will be passed to @isolate
145 * @nr_to_walk: how many items to scan.
146 *
147 * This function will scan all elements in a particular list_lru, calling the
148 * @isolate callback for each of those items, along with the current list
149 * spinlock and a caller-provided opaque. The @isolate callback can choose to
150 * drop the lock internally, but *must* return with the lock held. The callback
151 * will return an enum lru_status telling the list_lru infrastructure what to
152 * do with the object being scanned.
153 *
154 * Please note that nr_to_walk does not mean how many objects will be freed,
155 * just how many objects will be scanned.
156 *
157 * Return value: the number of objects effectively removed from the LRU.
158 */
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800159unsigned long list_lru_walk_one(struct list_lru *lru,
160 int nid, struct mem_cgroup *memcg,
161 list_lru_walk_cb isolate, void *cb_arg,
162 unsigned long *nr_to_walk);
Glauber Costa6a4f4962013-08-28 10:18:02 +1000163unsigned long list_lru_walk_node(struct list_lru *lru, int nid,
164 list_lru_walk_cb isolate, void *cb_arg,
165 unsigned long *nr_to_walk);
166
167static inline unsigned long
Vladimir Davydov503c3582015-02-12 14:58:47 -0800168list_lru_shrink_walk(struct list_lru *lru, struct shrink_control *sc,
169 list_lru_walk_cb isolate, void *cb_arg)
170{
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800171 return list_lru_walk_one(lru, sc->nid, sc->memcg, isolate, cb_arg,
172 &sc->nr_to_scan);
Vladimir Davydov503c3582015-02-12 14:58:47 -0800173}
174
175static inline unsigned long
Glauber Costa6a4f4962013-08-28 10:18:02 +1000176list_lru_walk(struct list_lru *lru, list_lru_walk_cb isolate,
177 void *cb_arg, unsigned long nr_to_walk)
178{
179 long isolated = 0;
180 int nid;
181
Vladimir Davydovff0b67e2015-02-12 14:59:04 -0800182 for_each_node_state(nid, N_NORMAL_MEMORY) {
Glauber Costa6a4f4962013-08-28 10:18:02 +1000183 isolated += list_lru_walk_node(lru, nid, isolate,
184 cb_arg, &nr_to_walk);
185 if (nr_to_walk <= 0)
186 break;
187 }
188 return isolated;
189}
Dave Chinnera38e4082013-08-28 10:17:58 +1000190#endif /* _LRU_LIST_H */