xref: /linux-6.15/lib/lru_cache.c (revision 9a42bfd2)
1c6ae4c04SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2b411b363SPhilipp Reisner /*
3b411b363SPhilipp Reisner    lru_cache.c
4b411b363SPhilipp Reisner 
5b411b363SPhilipp Reisner    This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
6b411b363SPhilipp Reisner 
7b411b363SPhilipp Reisner    Copyright (C) 2003-2008, LINBIT Information Technologies GmbH.
8b411b363SPhilipp Reisner    Copyright (C) 2003-2008, Philipp Reisner <[email protected]>.
9b411b363SPhilipp Reisner    Copyright (C) 2003-2008, Lars Ellenberg <[email protected]>.
10b411b363SPhilipp Reisner 
11b411b363SPhilipp Reisner 
12b411b363SPhilipp Reisner  */
13b411b363SPhilipp Reisner 
14b411b363SPhilipp Reisner #include <linux/module.h>
15b411b363SPhilipp Reisner #include <linux/bitops.h>
16b411b363SPhilipp Reisner #include <linux/slab.h>
17b411b363SPhilipp Reisner #include <linux/string.h> /* for memset */
18b411b363SPhilipp Reisner #include <linux/seq_file.h> /* for seq_printf */
19b411b363SPhilipp Reisner #include <linux/lru_cache.h>
20b411b363SPhilipp Reisner 
21b411b363SPhilipp Reisner MODULE_AUTHOR("Philipp Reisner <[email protected]>, "
22b411b363SPhilipp Reisner 	      "Lars Ellenberg <[email protected]>");
23b411b363SPhilipp Reisner MODULE_DESCRIPTION("lru_cache - Track sets of hot objects");
24b411b363SPhilipp Reisner MODULE_LICENSE("GPL");
25b411b363SPhilipp Reisner 
26b411b363SPhilipp Reisner /* this is developers aid only.
27b411b363SPhilipp Reisner  * it catches concurrent access (lack of locking on the users part) */
28b411b363SPhilipp Reisner #define PARANOIA_ENTRY() do {		\
29b411b363SPhilipp Reisner 	BUG_ON(!lc);			\
30b411b363SPhilipp Reisner 	BUG_ON(!lc->nr_elements);	\
31b411b363SPhilipp Reisner 	BUG_ON(test_and_set_bit(__LC_PARANOIA, &lc->flags)); \
32b411b363SPhilipp Reisner } while (0)
33b411b363SPhilipp Reisner 
34b411b363SPhilipp Reisner #define RETURN(x...)     do { \
354738fa16SLars Ellenberg 	clear_bit_unlock(__LC_PARANOIA, &lc->flags); \
364738fa16SLars Ellenberg 	return x ; } while (0)
37b411b363SPhilipp Reisner 
38b411b363SPhilipp Reisner /* BUG() if e is not one of the elements tracked by lc */
39b411b363SPhilipp Reisner #define PARANOIA_LC_ELEMENT(lc, e) do {	\
40b411b363SPhilipp Reisner 	struct lru_cache *lc_ = (lc);	\
41b411b363SPhilipp Reisner 	struct lc_element *e_ = (e);	\
42b411b363SPhilipp Reisner 	unsigned i = e_->lc_index;	\
43b411b363SPhilipp Reisner 	BUG_ON(i >= lc_->nr_elements);	\
44b411b363SPhilipp Reisner 	BUG_ON(lc_->lc_element[i] != e_); } while (0)
45b411b363SPhilipp Reisner 
4646a15bc3SLars Ellenberg 
4746a15bc3SLars Ellenberg /* We need to atomically
4846a15bc3SLars Ellenberg  *  - try to grab the lock (set LC_LOCKED)
4946a15bc3SLars Ellenberg  *  - only if there is no pending transaction
5046a15bc3SLars Ellenberg  *    (neither LC_DIRTY nor LC_STARVING is set)
5146a15bc3SLars Ellenberg  * Because of PARANOIA_ENTRY() above abusing lc->flags as well,
5246a15bc3SLars Ellenberg  * it is not sufficient to just say
5346a15bc3SLars Ellenberg  *	return 0 == cmpxchg(&lc->flags, 0, LC_LOCKED);
5446a15bc3SLars Ellenberg  */
lc_try_lock(struct lru_cache * lc)5546a15bc3SLars Ellenberg int lc_try_lock(struct lru_cache *lc)
5646a15bc3SLars Ellenberg {
5746a15bc3SLars Ellenberg 	unsigned long val;
5846a15bc3SLars Ellenberg 	do {
5946a15bc3SLars Ellenberg 		val = cmpxchg(&lc->flags, 0, LC_LOCKED);
6046a15bc3SLars Ellenberg 	} while (unlikely (val == LC_PARANOIA));
6146a15bc3SLars Ellenberg 	/* Spin until no-one is inside a PARANOIA_ENTRY()/RETURN() section. */
6246a15bc3SLars Ellenberg 	return 0 == val;
6346a15bc3SLars Ellenberg }
6446a15bc3SLars Ellenberg 
65b411b363SPhilipp Reisner /**
66b411b363SPhilipp Reisner  * lc_create - prepares to track objects in an active set
67b411b363SPhilipp Reisner  * @name: descriptive name only used in lc_seq_printf_stats and lc_seq_dump_details
68c95c2d32SRandy Dunlap  * @cache: cache root pointer
6946a15bc3SLars Ellenberg  * @max_pending_changes: maximum changes to accumulate until a transaction is required
70b411b363SPhilipp Reisner  * @e_count: number of elements allowed to be active simultaneously
71b411b363SPhilipp Reisner  * @e_size: size of the tracked objects
72b411b363SPhilipp Reisner  * @e_off: offset to the &struct lc_element member in a tracked object
73b411b363SPhilipp Reisner  *
74b411b363SPhilipp Reisner  * Returns a pointer to a newly initialized struct lru_cache on success,
75b411b363SPhilipp Reisner  * or NULL on (allocation) failure.
76b411b363SPhilipp Reisner  */
lc_create(const char * name,struct kmem_cache * cache,unsigned max_pending_changes,unsigned e_count,size_t e_size,size_t e_off)77b411b363SPhilipp Reisner struct lru_cache *lc_create(const char *name, struct kmem_cache *cache,
7846a15bc3SLars Ellenberg 		unsigned max_pending_changes,
79b411b363SPhilipp Reisner 		unsigned e_count, size_t e_size, size_t e_off)
80b411b363SPhilipp Reisner {
81b411b363SPhilipp Reisner 	struct hlist_head *slot = NULL;
82b411b363SPhilipp Reisner 	struct lc_element **element = NULL;
83b411b363SPhilipp Reisner 	struct lru_cache *lc;
84b411b363SPhilipp Reisner 	struct lc_element *e;
85b411b363SPhilipp Reisner 	unsigned cache_obj_size = kmem_cache_size(cache);
86b411b363SPhilipp Reisner 	unsigned i;
87b411b363SPhilipp Reisner 
88b411b363SPhilipp Reisner 	WARN_ON(cache_obj_size < e_size);
89b411b363SPhilipp Reisner 	if (cache_obj_size < e_size)
90b411b363SPhilipp Reisner 		return NULL;
91b411b363SPhilipp Reisner 
92b411b363SPhilipp Reisner 	/* e_count too big; would probably fail the allocation below anyways.
93b411b363SPhilipp Reisner 	 * for typical use cases, e_count should be few thousand at most. */
94b411b363SPhilipp Reisner 	if (e_count > LC_MAX_ACTIVE)
95b411b363SPhilipp Reisner 		return NULL;
96b411b363SPhilipp Reisner 
97a08aa355SIlia Mirkin 	slot = kcalloc(e_count, sizeof(struct hlist_head), GFP_KERNEL);
98b411b363SPhilipp Reisner 	if (!slot)
99b411b363SPhilipp Reisner 		goto out_fail;
1006396bb22SKees Cook 	element = kcalloc(e_count, sizeof(struct lc_element *), GFP_KERNEL);
101b411b363SPhilipp Reisner 	if (!element)
102b411b363SPhilipp Reisner 		goto out_fail;
103b411b363SPhilipp Reisner 
104b411b363SPhilipp Reisner 	lc = kzalloc(sizeof(*lc), GFP_KERNEL);
105b411b363SPhilipp Reisner 	if (!lc)
106b411b363SPhilipp Reisner 		goto out_fail;
107b411b363SPhilipp Reisner 
108b411b363SPhilipp Reisner 	INIT_LIST_HEAD(&lc->in_use);
109b411b363SPhilipp Reisner 	INIT_LIST_HEAD(&lc->lru);
110b411b363SPhilipp Reisner 	INIT_LIST_HEAD(&lc->free);
11146a15bc3SLars Ellenberg 	INIT_LIST_HEAD(&lc->to_be_changed);
112b411b363SPhilipp Reisner 
113b411b363SPhilipp Reisner 	lc->name = name;
114b411b363SPhilipp Reisner 	lc->element_size = e_size;
115b411b363SPhilipp Reisner 	lc->element_off = e_off;
116b411b363SPhilipp Reisner 	lc->nr_elements = e_count;
11746a15bc3SLars Ellenberg 	lc->max_pending_changes = max_pending_changes;
118b411b363SPhilipp Reisner 	lc->lc_cache = cache;
119b411b363SPhilipp Reisner 	lc->lc_element = element;
120b411b363SPhilipp Reisner 	lc->lc_slot = slot;
121b411b363SPhilipp Reisner 
122b411b363SPhilipp Reisner 	/* preallocate all objects */
123b411b363SPhilipp Reisner 	for (i = 0; i < e_count; i++) {
124b411b363SPhilipp Reisner 		void *p = kmem_cache_alloc(cache, GFP_KERNEL);
125b411b363SPhilipp Reisner 		if (!p)
126b411b363SPhilipp Reisner 			break;
127b411b363SPhilipp Reisner 		memset(p, 0, lc->element_size);
128b411b363SPhilipp Reisner 		e = p + e_off;
129b411b363SPhilipp Reisner 		e->lc_index = i;
130b411b363SPhilipp Reisner 		e->lc_number = LC_FREE;
13146a15bc3SLars Ellenberg 		e->lc_new_number = LC_FREE;
132b411b363SPhilipp Reisner 		list_add(&e->list, &lc->free);
133b411b363SPhilipp Reisner 		element[i] = e;
134b411b363SPhilipp Reisner 	}
135b411b363SPhilipp Reisner 	if (i == e_count)
136b411b363SPhilipp Reisner 		return lc;
137b411b363SPhilipp Reisner 
138b411b363SPhilipp Reisner 	/* else: could not allocate all elements, give up */
1395a66fce9Swuchi 	while (i) {
1405a66fce9Swuchi 		void *p = element[--i];
141b411b363SPhilipp Reisner 		kmem_cache_free(cache, p - e_off);
142b411b363SPhilipp Reisner 	}
143b411b363SPhilipp Reisner 	kfree(lc);
144b411b363SPhilipp Reisner out_fail:
145b411b363SPhilipp Reisner 	kfree(element);
146b411b363SPhilipp Reisner 	kfree(slot);
147b411b363SPhilipp Reisner 	return NULL;
148b411b363SPhilipp Reisner }
149b411b363SPhilipp Reisner 
lc_free_by_index(struct lru_cache * lc,unsigned i)1508ce953aaSLars Ellenberg static void lc_free_by_index(struct lru_cache *lc, unsigned i)
151b411b363SPhilipp Reisner {
152b411b363SPhilipp Reisner 	void *p = lc->lc_element[i];
153b411b363SPhilipp Reisner 	WARN_ON(!p);
154b411b363SPhilipp Reisner 	if (p) {
155b411b363SPhilipp Reisner 		p -= lc->element_off;
156b411b363SPhilipp Reisner 		kmem_cache_free(lc->lc_cache, p);
157b411b363SPhilipp Reisner 	}
158b411b363SPhilipp Reisner }
159b411b363SPhilipp Reisner 
160b411b363SPhilipp Reisner /**
161b411b363SPhilipp Reisner  * lc_destroy - frees memory allocated by lc_create()
162b411b363SPhilipp Reisner  * @lc: the lru cache to destroy
163b411b363SPhilipp Reisner  */
lc_destroy(struct lru_cache * lc)164b411b363SPhilipp Reisner void lc_destroy(struct lru_cache *lc)
165b411b363SPhilipp Reisner {
166b411b363SPhilipp Reisner 	unsigned i;
167b411b363SPhilipp Reisner 	if (!lc)
168b411b363SPhilipp Reisner 		return;
169b411b363SPhilipp Reisner 	for (i = 0; i < lc->nr_elements; i++)
170b411b363SPhilipp Reisner 		lc_free_by_index(lc, i);
171b411b363SPhilipp Reisner 	kfree(lc->lc_element);
172b411b363SPhilipp Reisner 	kfree(lc->lc_slot);
173b411b363SPhilipp Reisner 	kfree(lc);
174b411b363SPhilipp Reisner }
175b411b363SPhilipp Reisner 
176b411b363SPhilipp Reisner /**
177b411b363SPhilipp Reisner  * lc_reset - does a full reset for @lc and the hash table slots.
178b411b363SPhilipp Reisner  * @lc: the lru cache to operate on
179b411b363SPhilipp Reisner  *
180b411b363SPhilipp Reisner  * It is roughly the equivalent of re-allocating a fresh lru_cache object,
181b411b363SPhilipp Reisner  * basically a short cut to lc_destroy(lc); lc = lc_create(...);
182b411b363SPhilipp Reisner  */
lc_reset(struct lru_cache * lc)183b411b363SPhilipp Reisner void lc_reset(struct lru_cache *lc)
184b411b363SPhilipp Reisner {
185b411b363SPhilipp Reisner 	unsigned i;
186b411b363SPhilipp Reisner 
187b411b363SPhilipp Reisner 	INIT_LIST_HEAD(&lc->in_use);
188b411b363SPhilipp Reisner 	INIT_LIST_HEAD(&lc->lru);
189b411b363SPhilipp Reisner 	INIT_LIST_HEAD(&lc->free);
19046a15bc3SLars Ellenberg 	INIT_LIST_HEAD(&lc->to_be_changed);
191b411b363SPhilipp Reisner 	lc->used = 0;
192b411b363SPhilipp Reisner 	lc->hits = 0;
193b411b363SPhilipp Reisner 	lc->misses = 0;
194b411b363SPhilipp Reisner 	lc->starving = 0;
19546a15bc3SLars Ellenberg 	lc->locked = 0;
196b411b363SPhilipp Reisner 	lc->changed = 0;
19746a15bc3SLars Ellenberg 	lc->pending_changes = 0;
198b411b363SPhilipp Reisner 	lc->flags = 0;
199b411b363SPhilipp Reisner 	memset(lc->lc_slot, 0, sizeof(struct hlist_head) * lc->nr_elements);
200b411b363SPhilipp Reisner 
201b411b363SPhilipp Reisner 	for (i = 0; i < lc->nr_elements; i++) {
202b411b363SPhilipp Reisner 		struct lc_element *e = lc->lc_element[i];
203b411b363SPhilipp Reisner 		void *p = e;
204b411b363SPhilipp Reisner 		p -= lc->element_off;
205b411b363SPhilipp Reisner 		memset(p, 0, lc->element_size);
206b411b363SPhilipp Reisner 		/* re-init it */
207b411b363SPhilipp Reisner 		e->lc_index = i;
208b411b363SPhilipp Reisner 		e->lc_number = LC_FREE;
20946a15bc3SLars Ellenberg 		e->lc_new_number = LC_FREE;
210b411b363SPhilipp Reisner 		list_add(&e->list, &lc->free);
211b411b363SPhilipp Reisner 	}
212b411b363SPhilipp Reisner }
213b411b363SPhilipp Reisner 
214b411b363SPhilipp Reisner /**
215b411b363SPhilipp Reisner  * lc_seq_printf_stats - print stats about @lc into @seq
216b411b363SPhilipp Reisner  * @seq: the seq_file to print into
217b411b363SPhilipp Reisner  * @lc: the lru cache to print statistics of
218b411b363SPhilipp Reisner  */
lc_seq_printf_stats(struct seq_file * seq,struct lru_cache * lc)219bb649b34SRoland Kammerer void lc_seq_printf_stats(struct seq_file *seq, struct lru_cache *lc)
220b411b363SPhilipp Reisner {
221b411b363SPhilipp Reisner 	/* NOTE:
222b411b363SPhilipp Reisner 	 * total calls to lc_get are
223b411b363SPhilipp Reisner 	 * (starving + hits + misses)
22446a15bc3SLars Ellenberg 	 * misses include "locked" count (update from an other thread in
225b411b363SPhilipp Reisner 	 * progress) and "changed", when this in fact lead to an successful
226b411b363SPhilipp Reisner 	 * update of the cache.
227b411b363SPhilipp Reisner 	 */
228d50f8f8dSJoe Perches 	seq_printf(seq, "\t%s: used:%u/%u hits:%lu misses:%lu starving:%lu locked:%lu changed:%lu\n",
229b411b363SPhilipp Reisner 		   lc->name, lc->used, lc->nr_elements,
23046a15bc3SLars Ellenberg 		   lc->hits, lc->misses, lc->starving, lc->locked, lc->changed);
231b411b363SPhilipp Reisner }
232b411b363SPhilipp Reisner 
lc_hash_slot(struct lru_cache * lc,unsigned int enr)233b411b363SPhilipp Reisner static struct hlist_head *lc_hash_slot(struct lru_cache *lc, unsigned int enr)
234b411b363SPhilipp Reisner {
235b411b363SPhilipp Reisner 	return  lc->lc_slot + (enr % lc->nr_elements);
236b411b363SPhilipp Reisner }
237b411b363SPhilipp Reisner 
238b411b363SPhilipp Reisner 
__lc_find(struct lru_cache * lc,unsigned int enr,bool include_changing)23946a15bc3SLars Ellenberg static struct lc_element *__lc_find(struct lru_cache *lc, unsigned int enr,
24046a15bc3SLars Ellenberg 		bool include_changing)
24146a15bc3SLars Ellenberg {
24246a15bc3SLars Ellenberg 	struct lc_element *e;
24346a15bc3SLars Ellenberg 
24446a15bc3SLars Ellenberg 	BUG_ON(!lc);
24546a15bc3SLars Ellenberg 	BUG_ON(!lc->nr_elements);
246*9a42bfd2SDeshan Zhang 	hlist_for_each_entry(e, lc_hash_slot(lc, enr), collision) {
24746a15bc3SLars Ellenberg 		/* "about to be changed" elements, pending transaction commit,
24846a15bc3SLars Ellenberg 		 * are hashed by their "new number". "Normal" elements have
24946a15bc3SLars Ellenberg 		 * lc_number == lc_new_number. */
25046a15bc3SLars Ellenberg 		if (e->lc_new_number != enr)
25146a15bc3SLars Ellenberg 			continue;
25246a15bc3SLars Ellenberg 		if (e->lc_new_number == e->lc_number || include_changing)
25346a15bc3SLars Ellenberg 			return e;
25446a15bc3SLars Ellenberg 		break;
25546a15bc3SLars Ellenberg 	}
25646a15bc3SLars Ellenberg 	return NULL;
25746a15bc3SLars Ellenberg }
25846a15bc3SLars Ellenberg 
259b411b363SPhilipp Reisner /**
260b411b363SPhilipp Reisner  * lc_find - find element by label, if present in the hash table
261b411b363SPhilipp Reisner  * @lc: The lru_cache object
262b411b363SPhilipp Reisner  * @enr: element number
263b411b363SPhilipp Reisner  *
264b411b363SPhilipp Reisner  * Returns the pointer to an element, if the element with the requested
265b411b363SPhilipp Reisner  * "label" or element number is present in the hash table,
266b411b363SPhilipp Reisner  * or NULL if not found. Does not change the refcnt.
26746a15bc3SLars Ellenberg  * Ignores elements that are "about to be used", i.e. not yet in the active
26846a15bc3SLars Ellenberg  * set, but still pending transaction commit.
269b411b363SPhilipp Reisner  */
lc_find(struct lru_cache * lc,unsigned int enr)270b411b363SPhilipp Reisner struct lc_element *lc_find(struct lru_cache *lc, unsigned int enr)
271b411b363SPhilipp Reisner {
27246a15bc3SLars Ellenberg 	return __lc_find(lc, enr, 0);
273b411b363SPhilipp Reisner }
274b411b363SPhilipp Reisner 
27546a15bc3SLars Ellenberg /**
27646a15bc3SLars Ellenberg  * lc_is_used - find element by label
27746a15bc3SLars Ellenberg  * @lc: The lru_cache object
27846a15bc3SLars Ellenberg  * @enr: element number
27946a15bc3SLars Ellenberg  *
28046a15bc3SLars Ellenberg  * Returns true, if the element with the requested "label" or element number is
28146a15bc3SLars Ellenberg  * present in the hash table, and is used (refcnt > 0).
28246a15bc3SLars Ellenberg  * Also finds elements that are not _currently_ used but only "about to be
28346a15bc3SLars Ellenberg  * used", i.e. on the "to_be_changed" list, pending transaction commit.
28446a15bc3SLars Ellenberg  */
lc_is_used(struct lru_cache * lc,unsigned int enr)28546a15bc3SLars Ellenberg bool lc_is_used(struct lru_cache *lc, unsigned int enr)
286b411b363SPhilipp Reisner {
28746a15bc3SLars Ellenberg 	struct lc_element *e = __lc_find(lc, enr, 1);
28846a15bc3SLars Ellenberg 	return e && e->refcnt;
289b411b363SPhilipp Reisner }
290b411b363SPhilipp Reisner 
291b411b363SPhilipp Reisner /**
292b411b363SPhilipp Reisner  * lc_del - removes an element from the cache
293b411b363SPhilipp Reisner  * @lc: The lru_cache object
294b411b363SPhilipp Reisner  * @e: The element to remove
295b411b363SPhilipp Reisner  *
296b411b363SPhilipp Reisner  * @e must be unused (refcnt == 0). Moves @e from "lru" to "free" list,
297b411b363SPhilipp Reisner  * sets @e->enr to %LC_FREE.
298b411b363SPhilipp Reisner  */
lc_del(struct lru_cache * lc,struct lc_element * e)299b411b363SPhilipp Reisner void lc_del(struct lru_cache *lc, struct lc_element *e)
300b411b363SPhilipp Reisner {
301b411b363SPhilipp Reisner 	PARANOIA_ENTRY();
302b411b363SPhilipp Reisner 	PARANOIA_LC_ELEMENT(lc, e);
303b411b363SPhilipp Reisner 	BUG_ON(e->refcnt);
304b411b363SPhilipp Reisner 
30546a15bc3SLars Ellenberg 	e->lc_number = e->lc_new_number = LC_FREE;
306*9a42bfd2SDeshan Zhang 	hlist_del_init(&e->collision);
307b411b363SPhilipp Reisner 	list_move(&e->list, &lc->free);
308b411b363SPhilipp Reisner 	RETURN();
309b411b363SPhilipp Reisner }
310b411b363SPhilipp Reisner 
lc_prepare_for_change(struct lru_cache * lc,unsigned new_number)31146a15bc3SLars Ellenberg static struct lc_element *lc_prepare_for_change(struct lru_cache *lc, unsigned new_number)
312b411b363SPhilipp Reisner {
313b411b363SPhilipp Reisner 	struct list_head *n;
31446a15bc3SLars Ellenberg 	struct lc_element *e;
315b411b363SPhilipp Reisner 
31646a15bc3SLars Ellenberg 	if (!list_empty(&lc->free))
317b411b363SPhilipp Reisner 		n = lc->free.next;
31846a15bc3SLars Ellenberg 	else if (!list_empty(&lc->lru))
31946a15bc3SLars Ellenberg 		n = lc->lru.prev;
32046a15bc3SLars Ellenberg 	else
32146a15bc3SLars Ellenberg 		return NULL;
32246a15bc3SLars Ellenberg 
32346a15bc3SLars Ellenberg 	e = list_entry(n, struct lc_element, list);
32446a15bc3SLars Ellenberg 	PARANOIA_LC_ELEMENT(lc, e);
32546a15bc3SLars Ellenberg 
32646a15bc3SLars Ellenberg 	e->lc_new_number = new_number;
327*9a42bfd2SDeshan Zhang 	if (!hlist_unhashed(&e->collision))
328*9a42bfd2SDeshan Zhang 		__hlist_del(&e->collision);
329*9a42bfd2SDeshan Zhang 	hlist_add_head(&e->collision, lc_hash_slot(lc, new_number));
33046a15bc3SLars Ellenberg 	list_move(&e->list, &lc->to_be_changed);
33146a15bc3SLars Ellenberg 
33246a15bc3SLars Ellenberg 	return e;
333b411b363SPhilipp Reisner }
334b411b363SPhilipp Reisner 
lc_unused_element_available(struct lru_cache * lc)335b411b363SPhilipp Reisner static int lc_unused_element_available(struct lru_cache *lc)
336b411b363SPhilipp Reisner {
337b411b363SPhilipp Reisner 	if (!list_empty(&lc->free))
338b411b363SPhilipp Reisner 		return 1; /* something on the free list */
339b411b363SPhilipp Reisner 	if (!list_empty(&lc->lru))
340b411b363SPhilipp Reisner 		return 1;  /* something to evict */
341b411b363SPhilipp Reisner 
342b411b363SPhilipp Reisner 	return 0;
343b411b363SPhilipp Reisner }
344b411b363SPhilipp Reisner 
345cbe5e610SLars Ellenberg /* used as internal flags to __lc_get */
346cbe5e610SLars Ellenberg enum {
347cbe5e610SLars Ellenberg 	LC_GET_MAY_CHANGE = 1,
348cbe5e610SLars Ellenberg 	LC_GET_MAY_USE_UNCOMMITTED = 2,
349cbe5e610SLars Ellenberg };
350cbe5e610SLars Ellenberg 
__lc_get(struct lru_cache * lc,unsigned int enr,unsigned int flags)351cbe5e610SLars Ellenberg static struct lc_element *__lc_get(struct lru_cache *lc, unsigned int enr, unsigned int flags)
352a9efc748SLars Ellenberg {
353a9efc748SLars Ellenberg 	struct lc_element *e;
354a9efc748SLars Ellenberg 
355a9efc748SLars Ellenberg 	PARANOIA_ENTRY();
356f2d03d89SLars Ellenberg 	if (test_bit(__LC_STARVING, &lc->flags)) {
357a9efc748SLars Ellenberg 		++lc->starving;
358a9efc748SLars Ellenberg 		RETURN(NULL);
359a9efc748SLars Ellenberg 	}
360a9efc748SLars Ellenberg 
36146a15bc3SLars Ellenberg 	e = __lc_find(lc, enr, 1);
36246a15bc3SLars Ellenberg 	/* if lc_new_number != lc_number,
36346a15bc3SLars Ellenberg 	 * this enr is currently being pulled in already,
36446a15bc3SLars Ellenberg 	 * and will be available once the pending transaction
36546a15bc3SLars Ellenberg 	 * has been committed. */
366cbe5e610SLars Ellenberg 	if (e) {
367cbe5e610SLars Ellenberg 		if (e->lc_new_number != e->lc_number) {
368cbe5e610SLars Ellenberg 			/* It has been found above, but on the "to_be_changed"
369cbe5e610SLars Ellenberg 			 * list, not yet committed.  Don't pull it in twice,
370cbe5e610SLars Ellenberg 			 * wait for the transaction, then try again...
371cbe5e610SLars Ellenberg 			 */
372cbe5e610SLars Ellenberg 			if (!(flags & LC_GET_MAY_USE_UNCOMMITTED))
373cbe5e610SLars Ellenberg 				RETURN(NULL);
374cbe5e610SLars Ellenberg 			/* ... unless the caller is aware of the implications,
375cbe5e610SLars Ellenberg 			 * probably preparing a cumulative transaction. */
376cbe5e610SLars Ellenberg 			++e->refcnt;
377cbe5e610SLars Ellenberg 			++lc->hits;
378cbe5e610SLars Ellenberg 			RETURN(e);
379cbe5e610SLars Ellenberg 		}
380cbe5e610SLars Ellenberg 		/* else: lc_new_number == lc_number; a real hit. */
381a9efc748SLars Ellenberg 		++lc->hits;
382a9efc748SLars Ellenberg 		if (e->refcnt++ == 0)
383a9efc748SLars Ellenberg 			lc->used++;
384a9efc748SLars Ellenberg 		list_move(&e->list, &lc->in_use); /* Not evictable... */
385a9efc748SLars Ellenberg 		RETURN(e);
386a9efc748SLars Ellenberg 	}
387cbe5e610SLars Ellenberg 	/* e == NULL */
388a9efc748SLars Ellenberg 
389a9efc748SLars Ellenberg 	++lc->misses;
390cbe5e610SLars Ellenberg 	if (!(flags & LC_GET_MAY_CHANGE))
39146a15bc3SLars Ellenberg 		RETURN(NULL);
39246a15bc3SLars Ellenberg 
39346a15bc3SLars Ellenberg 	/* To avoid races with lc_try_lock(), first, mark us dirty
39446a15bc3SLars Ellenberg 	 * (using test_and_set_bit, as it implies memory barriers), ... */
39546a15bc3SLars Ellenberg 	test_and_set_bit(__LC_DIRTY, &lc->flags);
39646a15bc3SLars Ellenberg 
39746a15bc3SLars Ellenberg 	/* ... only then check if it is locked anyways. If lc_unlock clears
39846a15bc3SLars Ellenberg 	 * the dirty bit again, that's not a problem, we will come here again.
39946a15bc3SLars Ellenberg 	 */
40046a15bc3SLars Ellenberg 	if (test_bit(__LC_LOCKED, &lc->flags)) {
40146a15bc3SLars Ellenberg 		++lc->locked;
40246a15bc3SLars Ellenberg 		RETURN(NULL);
40346a15bc3SLars Ellenberg 	}
40446a15bc3SLars Ellenberg 
405a9efc748SLars Ellenberg 	/* In case there is nothing available and we can not kick out
406a9efc748SLars Ellenberg 	 * the LRU element, we have to wait ...
407a9efc748SLars Ellenberg 	 */
408a9efc748SLars Ellenberg 	if (!lc_unused_element_available(lc)) {
409f2d03d89SLars Ellenberg 		set_bit(__LC_STARVING, &lc->flags);
410a9efc748SLars Ellenberg 		RETURN(NULL);
411a9efc748SLars Ellenberg 	}
412a9efc748SLars Ellenberg 
41346a15bc3SLars Ellenberg 	/* It was not present in the active set.  We are going to recycle an
41446a15bc3SLars Ellenberg 	 * unused (or even "free") element, but we won't accumulate more than
41546a15bc3SLars Ellenberg 	 * max_pending_changes changes.  */
41646a15bc3SLars Ellenberg 	if (lc->pending_changes >= lc->max_pending_changes)
417a9efc748SLars Ellenberg 		RETURN(NULL);
418a9efc748SLars Ellenberg 
41946a15bc3SLars Ellenberg 	e = lc_prepare_for_change(lc, enr);
420a9efc748SLars Ellenberg 	BUG_ON(!e);
421a9efc748SLars Ellenberg 
422a9efc748SLars Ellenberg 	clear_bit(__LC_STARVING, &lc->flags);
423a9efc748SLars Ellenberg 	BUG_ON(++e->refcnt != 1);
424a9efc748SLars Ellenberg 	lc->used++;
42546a15bc3SLars Ellenberg 	lc->pending_changes++;
426a9efc748SLars Ellenberg 
427a9efc748SLars Ellenberg 	RETURN(e);
428a9efc748SLars Ellenberg }
429b411b363SPhilipp Reisner 
430b411b363SPhilipp Reisner /**
431b411b363SPhilipp Reisner  * lc_get - get element by label, maybe change the active set
432b411b363SPhilipp Reisner  * @lc: the lru cache to operate on
433b411b363SPhilipp Reisner  * @enr: the label to look up
434b411b363SPhilipp Reisner  *
435b411b363SPhilipp Reisner  * Finds an element in the cache, increases its usage count,
436b411b363SPhilipp Reisner  * "touches" and returns it.
437b411b363SPhilipp Reisner  *
438b411b363SPhilipp Reisner  * In case the requested number is not present, it needs to be added to the
439b411b363SPhilipp Reisner  * cache. Therefore it is possible that an other element becomes evicted from
440b411b363SPhilipp Reisner  * the cache. In either case, the user is notified so he is able to e.g. keep
441b411b363SPhilipp Reisner  * a persistent log of the cache changes, and therefore the objects in use.
442b411b363SPhilipp Reisner  *
443b411b363SPhilipp Reisner  * Return values:
444b411b363SPhilipp Reisner  *  NULL
445b411b363SPhilipp Reisner  *     The cache was marked %LC_STARVING,
446b411b363SPhilipp Reisner  *     or the requested label was not in the active set
447b411b363SPhilipp Reisner  *     and a changing transaction is still pending (@lc was marked %LC_DIRTY).
448b411b363SPhilipp Reisner  *     Or no unused or free element could be recycled (@lc will be marked as
449b411b363SPhilipp Reisner  *     %LC_STARVING, blocking further lc_get() operations).
450b411b363SPhilipp Reisner  *
451b411b363SPhilipp Reisner  *  pointer to the element with the REQUESTED element number.
452b411b363SPhilipp Reisner  *     In this case, it can be used right away
453b411b363SPhilipp Reisner  *
454b411b363SPhilipp Reisner  *  pointer to an UNUSED element with some different element number,
455b411b363SPhilipp Reisner  *          where that different number may also be %LC_FREE.
456b411b363SPhilipp Reisner  *
45746a15bc3SLars Ellenberg  *          In this case, the cache is marked %LC_DIRTY,
45846a15bc3SLars Ellenberg  *          so lc_try_lock() will no longer succeed.
45946a15bc3SLars Ellenberg  *          The returned element pointer is moved to the "to_be_changed" list,
46046a15bc3SLars Ellenberg  *          and registered with the new element number on the hash collision chains,
46146a15bc3SLars Ellenberg  *          so it is possible to pick it up from lc_is_used().
46246a15bc3SLars Ellenberg  *          Up to "max_pending_changes" (see lc_create()) can be accumulated.
46346a15bc3SLars Ellenberg  *          The user now should do whatever housekeeping is necessary,
46446a15bc3SLars Ellenberg  *          typically serialize on lc_try_lock_for_transaction(), then call
46546a15bc3SLars Ellenberg  *          lc_committed(lc) and lc_unlock(), to finish the change.
466b411b363SPhilipp Reisner  *
467b411b363SPhilipp Reisner  * NOTE: The user needs to check the lc_number on EACH use, so he recognizes
468b411b363SPhilipp Reisner  *       any cache set change.
469b411b363SPhilipp Reisner  */
lc_get(struct lru_cache * lc,unsigned int enr)470b411b363SPhilipp Reisner struct lc_element *lc_get(struct lru_cache *lc, unsigned int enr)
471b411b363SPhilipp Reisner {
472cbe5e610SLars Ellenberg 	return __lc_get(lc, enr, LC_GET_MAY_CHANGE);
473cbe5e610SLars Ellenberg }
474cbe5e610SLars Ellenberg 
475cbe5e610SLars Ellenberg /**
476cbe5e610SLars Ellenberg  * lc_get_cumulative - like lc_get; also finds to-be-changed elements
477cbe5e610SLars Ellenberg  * @lc: the lru cache to operate on
478cbe5e610SLars Ellenberg  * @enr: the label to look up
479cbe5e610SLars Ellenberg  *
480cbe5e610SLars Ellenberg  * Unlike lc_get this also returns the element for @enr, if it is belonging to
481cbe5e610SLars Ellenberg  * a pending transaction, so the return values are like for lc_get(),
482cbe5e610SLars Ellenberg  * plus:
483cbe5e610SLars Ellenberg  *
484cbe5e610SLars Ellenberg  * pointer to an element already on the "to_be_changed" list.
485cbe5e610SLars Ellenberg  * 	In this case, the cache was already marked %LC_DIRTY.
486cbe5e610SLars Ellenberg  *
487cbe5e610SLars Ellenberg  * Caller needs to make sure that the pending transaction is completed,
488cbe5e610SLars Ellenberg  * before proceeding to actually use this element.
489cbe5e610SLars Ellenberg  */
lc_get_cumulative(struct lru_cache * lc,unsigned int enr)490cbe5e610SLars Ellenberg struct lc_element *lc_get_cumulative(struct lru_cache *lc, unsigned int enr)
491cbe5e610SLars Ellenberg {
492cbe5e610SLars Ellenberg 	return __lc_get(lc, enr, LC_GET_MAY_CHANGE|LC_GET_MAY_USE_UNCOMMITTED);
493b411b363SPhilipp Reisner }
494b411b363SPhilipp Reisner 
495a9efc748SLars Ellenberg /**
496a9efc748SLars Ellenberg  * lc_try_get - get element by label, if present; do not change the active set
497a9efc748SLars Ellenberg  * @lc: the lru cache to operate on
498a9efc748SLars Ellenberg  * @enr: the label to look up
499a9efc748SLars Ellenberg  *
500a9efc748SLars Ellenberg  * Finds an element in the cache, increases its usage count,
501a9efc748SLars Ellenberg  * "touches" and returns it.
502a9efc748SLars Ellenberg  *
503a9efc748SLars Ellenberg  * Return values:
504a9efc748SLars Ellenberg  *  NULL
505a9efc748SLars Ellenberg  *     The cache was marked %LC_STARVING,
506a9efc748SLars Ellenberg  *     or the requested label was not in the active set
507a9efc748SLars Ellenberg  *
508a9efc748SLars Ellenberg  *  pointer to the element with the REQUESTED element number.
509a9efc748SLars Ellenberg  *     In this case, it can be used right away
510b411b363SPhilipp Reisner  */
lc_try_get(struct lru_cache * lc,unsigned int enr)511b411b363SPhilipp Reisner struct lc_element *lc_try_get(struct lru_cache *lc, unsigned int enr)
512b411b363SPhilipp Reisner {
513a9efc748SLars Ellenberg 	return __lc_get(lc, enr, 0);
514b411b363SPhilipp Reisner }
515b411b363SPhilipp Reisner 
516b411b363SPhilipp Reisner /**
51746a15bc3SLars Ellenberg  * lc_committed - tell @lc that pending changes have been recorded
518b411b363SPhilipp Reisner  * @lc: the lru cache to operate on
51946a15bc3SLars Ellenberg  *
52046a15bc3SLars Ellenberg  * User is expected to serialize on explicit lc_try_lock_for_transaction()
52146a15bc3SLars Ellenberg  * before the transaction is started, and later needs to lc_unlock() explicitly
52246a15bc3SLars Ellenberg  * as well.
523b411b363SPhilipp Reisner  */
lc_committed(struct lru_cache * lc)52446a15bc3SLars Ellenberg void lc_committed(struct lru_cache *lc)
525b411b363SPhilipp Reisner {
52646a15bc3SLars Ellenberg 	struct lc_element *e, *tmp;
52746a15bc3SLars Ellenberg 
528b411b363SPhilipp Reisner 	PARANOIA_ENTRY();
52946a15bc3SLars Ellenberg 	list_for_each_entry_safe(e, tmp, &lc->to_be_changed, list) {
53046a15bc3SLars Ellenberg 		/* count number of changes, not number of transactions */
531b411b363SPhilipp Reisner 		++lc->changed;
53246a15bc3SLars Ellenberg 		e->lc_number = e->lc_new_number;
53346a15bc3SLars Ellenberg 		list_move(&e->list, &lc->in_use);
53446a15bc3SLars Ellenberg 	}
53546a15bc3SLars Ellenberg 	lc->pending_changes = 0;
536b411b363SPhilipp Reisner 	RETURN();
537b411b363SPhilipp Reisner }
538b411b363SPhilipp Reisner 
539b411b363SPhilipp Reisner 
540b411b363SPhilipp Reisner /**
541b411b363SPhilipp Reisner  * lc_put - give up refcnt of @e
542b411b363SPhilipp Reisner  * @lc: the lru cache to operate on
543b411b363SPhilipp Reisner  * @e: the element to put
544b411b363SPhilipp Reisner  *
545b411b363SPhilipp Reisner  * If refcnt reaches zero, the element is moved to the lru list,
546b411b363SPhilipp Reisner  * and a %LC_STARVING (if set) is cleared.
547b411b363SPhilipp Reisner  * Returns the new (post-decrement) refcnt.
548b411b363SPhilipp Reisner  */
lc_put(struct lru_cache * lc,struct lc_element * e)549b411b363SPhilipp Reisner unsigned int lc_put(struct lru_cache *lc, struct lc_element *e)
550b411b363SPhilipp Reisner {
551b411b363SPhilipp Reisner 	PARANOIA_ENTRY();
552b411b363SPhilipp Reisner 	PARANOIA_LC_ELEMENT(lc, e);
553b411b363SPhilipp Reisner 	BUG_ON(e->refcnt == 0);
55446a15bc3SLars Ellenberg 	BUG_ON(e->lc_number != e->lc_new_number);
555b411b363SPhilipp Reisner 	if (--e->refcnt == 0) {
556b411b363SPhilipp Reisner 		/* move it to the front of LRU. */
557b411b363SPhilipp Reisner 		list_move(&e->list, &lc->lru);
558b411b363SPhilipp Reisner 		lc->used--;
5594738fa16SLars Ellenberg 		clear_bit_unlock(__LC_STARVING, &lc->flags);
560b411b363SPhilipp Reisner 	}
561b411b363SPhilipp Reisner 	RETURN(e->refcnt);
562b411b363SPhilipp Reisner }
563b411b363SPhilipp Reisner 
564b411b363SPhilipp Reisner /**
565b411b363SPhilipp Reisner  * lc_element_by_index
566b411b363SPhilipp Reisner  * @lc: the lru cache to operate on
567b411b363SPhilipp Reisner  * @i: the index of the element to return
568b411b363SPhilipp Reisner  */
lc_element_by_index(struct lru_cache * lc,unsigned i)569b411b363SPhilipp Reisner struct lc_element *lc_element_by_index(struct lru_cache *lc, unsigned i)
570b411b363SPhilipp Reisner {
571b411b363SPhilipp Reisner 	BUG_ON(i >= lc->nr_elements);
572b411b363SPhilipp Reisner 	BUG_ON(lc->lc_element[i] == NULL);
573b411b363SPhilipp Reisner 	BUG_ON(lc->lc_element[i]->lc_index != i);
574b411b363SPhilipp Reisner 	return lc->lc_element[i];
575b411b363SPhilipp Reisner }
576b411b363SPhilipp Reisner 
577b411b363SPhilipp Reisner /**
578c95c2d32SRandy Dunlap  * lc_seq_dump_details - Dump a complete LRU cache to seq in textual form.
579b411b363SPhilipp Reisner  * @lc: the lru cache to operate on
580b411b363SPhilipp Reisner  * @seq: the &struct seq_file pointer to seq_printf into
58154e6fc38SLars Ellenberg  * @utext: user supplied additional "heading" or other info
582b411b363SPhilipp Reisner  * @detail: function pointer the user may provide to dump further details
58354e6fc38SLars Ellenberg  * of the object the lc_element is embedded in. May be NULL.
58454e6fc38SLars Ellenberg  * Note: a leading space ' ' and trailing newline '\n' is implied.
585b411b363SPhilipp Reisner  */
lc_seq_dump_details(struct seq_file * seq,struct lru_cache * lc,char * utext,void (* detail)(struct seq_file *,struct lc_element *))586b411b363SPhilipp Reisner void lc_seq_dump_details(struct seq_file *seq, struct lru_cache *lc, char *utext,
587b411b363SPhilipp Reisner 	     void (*detail) (struct seq_file *, struct lc_element *))
588b411b363SPhilipp Reisner {
589b411b363SPhilipp Reisner 	unsigned int nr_elements = lc->nr_elements;
590b411b363SPhilipp Reisner 	struct lc_element *e;
591b411b363SPhilipp Reisner 	int i;
592b411b363SPhilipp Reisner 
59354e6fc38SLars Ellenberg 	seq_printf(seq, "\tnn: lc_number (new nr) refcnt %s\n ", utext);
594b411b363SPhilipp Reisner 	for (i = 0; i < nr_elements; i++) {
595b411b363SPhilipp Reisner 		e = lc_element_by_index(lc, i);
59654e6fc38SLars Ellenberg 		if (e->lc_number != e->lc_new_number)
59754e6fc38SLars Ellenberg 			seq_printf(seq, "\t%5d: %6d %8d %6d ",
59854e6fc38SLars Ellenberg 				i, e->lc_number, e->lc_new_number, e->refcnt);
59954e6fc38SLars Ellenberg 		else
60054e6fc38SLars Ellenberg 			seq_printf(seq, "\t%5d: %6d %-8s %6d ",
60154e6fc38SLars Ellenberg 				i, e->lc_number, "-\"-", e->refcnt);
60254e6fc38SLars Ellenberg 		if (detail)
603b411b363SPhilipp Reisner 			detail(seq, e);
60454e6fc38SLars Ellenberg 		seq_putc(seq, '\n');
605b411b363SPhilipp Reisner 	}
606b411b363SPhilipp Reisner }
607b411b363SPhilipp Reisner 
608b411b363SPhilipp Reisner EXPORT_SYMBOL(lc_create);
609b411b363SPhilipp Reisner EXPORT_SYMBOL(lc_reset);
610b411b363SPhilipp Reisner EXPORT_SYMBOL(lc_destroy);
611b411b363SPhilipp Reisner EXPORT_SYMBOL(lc_del);
612b411b363SPhilipp Reisner EXPORT_SYMBOL(lc_try_get);
613b411b363SPhilipp Reisner EXPORT_SYMBOL(lc_find);
614b411b363SPhilipp Reisner EXPORT_SYMBOL(lc_get);
615b411b363SPhilipp Reisner EXPORT_SYMBOL(lc_put);
61646a15bc3SLars Ellenberg EXPORT_SYMBOL(lc_committed);
617b411b363SPhilipp Reisner EXPORT_SYMBOL(lc_element_by_index);
618b411b363SPhilipp Reisner EXPORT_SYMBOL(lc_seq_printf_stats);
619b411b363SPhilipp Reisner EXPORT_SYMBOL(lc_seq_dump_details);
62046a15bc3SLars Ellenberg EXPORT_SYMBOL(lc_try_lock);
62146a15bc3SLars Ellenberg EXPORT_SYMBOL(lc_is_used);
622cbe5e610SLars Ellenberg EXPORT_SYMBOL(lc_get_cumulative);
623