blob: 96def9d15b1b04ddaef055c99729ea08af16f4e2 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Dave Chinnera38e4082013-08-28 10:17:58 +10002/*
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>
Dave Chinner3b1d58a2013-08-28 10:18:00 +100012#include <linux/nodemask.h>
Vladimir Davydov503c3582015-02-12 14:58:47 -080013#include <linux/shrinker.h>
Dave Chinnera38e4082013-08-28 10:17:58 +100014
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080015struct mem_cgroup;
16
Dave Chinnera38e4082013-08-28 10:17:58 +100017/* list_lru_walk_cb has to always return one of those */
18enum lru_status {
19 LRU_REMOVED, /* item removed from list */
Johannes Weiner449dd692014-04-03 14:47:56 -070020 LRU_REMOVED_RETRY, /* item removed, but lock has been
21 dropped and reacquired */
Dave Chinnera38e4082013-08-28 10:17:58 +100022 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
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080028struct list_lru_one {
Dave Chinnera38e4082013-08-28 10:17:58 +100029 struct list_head list;
Vladimir Davydov2788cf02015-02-12 14:59:38 -080030 /* may become negative during memcg reparenting */
Dave Chinnera38e4082013-08-28 10:17:58 +100031 long nr_items;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080032};
33
34struct list_lru_memcg {
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -070035 struct rcu_head rcu;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080036 /* array of per cgroup lists, indexed by memcg_cache_id */
37 struct list_lru_one *lru[0];
38};
39
40struct list_lru_node {
41 /* protects all lists on the node, including per cgroup */
42 spinlock_t lock;
43 /* global list, used for the root cgroup in cgroup aware lrus */
44 struct list_lru_one lru;
Johannes Weiner127424c2016-01-20 15:02:32 -080045#if defined(CONFIG_MEMCG) && !defined(CONFIG_SLOB)
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080046 /* for cgroup aware lrus points to per cgroup lists, otherwise NULL */
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -070047 struct list_lru_memcg __rcu *memcg_lrus;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080048#endif
Sahitya Tummala2c80cd52017-07-10 15:49:57 -070049 long nr_items;
Dave Chinner3b1d58a2013-08-28 10:18:00 +100050} ____cacheline_aligned_in_smp;
51
52struct list_lru {
Glauber Costa5ca302c2013-08-28 10:18:18 +100053 struct list_lru_node *node;
Johannes Weiner127424c2016-01-20 15:02:32 -080054#if defined(CONFIG_MEMCG) && !defined(CONFIG_SLOB)
Vladimir Davydovc0a5b5602015-02-12 14:59:07 -080055 struct list_head list;
56#endif
Dave Chinnera38e4082013-08-28 10:17:58 +100057};
58
Glauber Costa5ca302c2013-08-28 10:18:18 +100059void list_lru_destroy(struct list_lru *lru);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080060int __list_lru_init(struct list_lru *lru, bool memcg_aware,
61 struct lock_class_key *key);
62
63#define list_lru_init(lru) __list_lru_init((lru), false, NULL)
64#define list_lru_init_key(lru, key) __list_lru_init((lru), false, (key))
65#define list_lru_init_memcg(lru) __list_lru_init((lru), true, NULL)
66
67int memcg_update_all_list_lrus(int num_memcgs);
Vladimir Davydov2788cf02015-02-12 14:59:38 -080068void memcg_drain_all_list_lrus(int src_idx, int dst_idx);
Dave Chinnera38e4082013-08-28 10:17:58 +100069
70/**
71 * list_lru_add: add an element to the lru list's tail
72 * @list_lru: the lru pointer
73 * @item: the item to be added.
74 *
75 * If the element is already part of a list, this function returns doing
76 * nothing. Therefore the caller does not need to keep state about whether or
77 * not the element already belongs in the list and is allowed to lazy update
78 * it. Note however that this is valid for *a* list, not *this* list. If
79 * the caller organize itself in a way that elements can be in more than
80 * one type of list, it is up to the caller to fully remove the item from
81 * the previous list (with list_lru_del() for instance) before moving it
82 * to @list_lru
83 *
84 * Return value: true if the list was updated, false otherwise
85 */
86bool list_lru_add(struct list_lru *lru, struct list_head *item);
87
88/**
89 * list_lru_del: delete an element to the lru list
90 * @list_lru: the lru pointer
91 * @item: the item to be deleted.
92 *
93 * This function works analogously as list_lru_add in terms of list
94 * manipulation. The comments about an element already pertaining to
95 * a list are also valid for list_lru_del.
96 *
97 * Return value: true if the list was updated, false otherwise
98 */
99bool list_lru_del(struct list_lru *lru, struct list_head *item);
100
101/**
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800102 * list_lru_count_one: return the number of objects currently held by @lru
Dave Chinnera38e4082013-08-28 10:17:58 +1000103 * @lru: the lru pointer.
Glauber Costa6a4f4962013-08-28 10:18:02 +1000104 * @nid: the node id to count from.
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800105 * @memcg: the cgroup to count from.
Dave Chinnera38e4082013-08-28 10:17:58 +1000106 *
107 * Always return a non-negative number, 0 for empty lists. There is no
108 * guarantee that the list is not updated while the count is being computed.
109 * Callers that want such a guarantee need to provide an outer lock.
110 */
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800111unsigned long list_lru_count_one(struct list_lru *lru,
112 int nid, struct mem_cgroup *memcg);
Glauber Costa6a4f4962013-08-28 10:18:02 +1000113unsigned long list_lru_count_node(struct list_lru *lru, int nid);
Vladimir Davydov503c3582015-02-12 14:58:47 -0800114
115static inline unsigned long list_lru_shrink_count(struct list_lru *lru,
116 struct shrink_control *sc)
117{
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800118 return list_lru_count_one(lru, sc->nid, sc->memcg);
Vladimir Davydov503c3582015-02-12 14:58:47 -0800119}
120
Glauber Costa6a4f4962013-08-28 10:18:02 +1000121static inline unsigned long list_lru_count(struct list_lru *lru)
122{
123 long count = 0;
124 int nid;
125
Vladimir Davydovff0b67e2015-02-12 14:59:04 -0800126 for_each_node_state(nid, N_NORMAL_MEMORY)
Glauber Costa6a4f4962013-08-28 10:18:02 +1000127 count += list_lru_count_node(lru, nid);
128
129 return count;
130}
Dave Chinnera38e4082013-08-28 10:17:58 +1000131
Vladimir Davydov3f97b162015-02-12 14:59:35 -0800132void list_lru_isolate(struct list_lru_one *list, struct list_head *item);
133void list_lru_isolate_move(struct list_lru_one *list, struct list_head *item,
134 struct list_head *head);
135
136typedef enum lru_status (*list_lru_walk_cb)(struct list_head *item,
137 struct list_lru_one *list, spinlock_t *lock, void *cb_arg);
138
Dave Chinnera38e4082013-08-28 10:17:58 +1000139/**
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800140 * list_lru_walk_one: walk a list_lru, isolating and disposing freeable items.
Dave Chinnera38e4082013-08-28 10:17:58 +1000141 * @lru: the lru pointer.
Glauber Costa6a4f4962013-08-28 10:18:02 +1000142 * @nid: the node id to scan from.
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800143 * @memcg: the cgroup to scan from.
Dave Chinnera38e4082013-08-28 10:17:58 +1000144 * @isolate: callback function that is resposible for deciding what to do with
145 * the item currently being scanned
146 * @cb_arg: opaque type that will be passed to @isolate
147 * @nr_to_walk: how many items to scan.
148 *
149 * This function will scan all elements in a particular list_lru, calling the
150 * @isolate callback for each of those items, along with the current list
151 * spinlock and a caller-provided opaque. The @isolate callback can choose to
152 * drop the lock internally, but *must* return with the lock held. The callback
153 * will return an enum lru_status telling the list_lru infrastructure what to
154 * do with the object being scanned.
155 *
156 * Please note that nr_to_walk does not mean how many objects will be freed,
157 * just how many objects will be scanned.
158 *
159 * Return value: the number of objects effectively removed from the LRU.
160 */
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800161unsigned long list_lru_walk_one(struct list_lru *lru,
162 int nid, struct mem_cgroup *memcg,
163 list_lru_walk_cb isolate, void *cb_arg,
164 unsigned long *nr_to_walk);
Glauber Costa6a4f4962013-08-28 10:18:02 +1000165unsigned long list_lru_walk_node(struct list_lru *lru, int nid,
166 list_lru_walk_cb isolate, void *cb_arg,
167 unsigned long *nr_to_walk);
168
169static inline unsigned long
Vladimir Davydov503c3582015-02-12 14:58:47 -0800170list_lru_shrink_walk(struct list_lru *lru, struct shrink_control *sc,
171 list_lru_walk_cb isolate, void *cb_arg)
172{
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800173 return list_lru_walk_one(lru, sc->nid, sc->memcg, isolate, cb_arg,
174 &sc->nr_to_scan);
Vladimir Davydov503c3582015-02-12 14:58:47 -0800175}
176
177static inline unsigned long
Glauber Costa6a4f4962013-08-28 10:18:02 +1000178list_lru_walk(struct list_lru *lru, list_lru_walk_cb isolate,
179 void *cb_arg, unsigned long nr_to_walk)
180{
181 long isolated = 0;
182 int nid;
183
Vladimir Davydovff0b67e2015-02-12 14:59:04 -0800184 for_each_node_state(nid, N_NORMAL_MEMORY) {
Glauber Costa6a4f4962013-08-28 10:18:02 +1000185 isolated += list_lru_walk_node(lru, nid, isolate,
186 cb_arg, &nr_to_walk);
187 if (nr_to_walk <= 0)
188 break;
189 }
190 return isolated;
191}
Dave Chinnera38e4082013-08-28 10:17:58 +1000192#endif /* _LRU_LIST_H */