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