// SPDX-License-Identifier: GPL-2.0-or-later /* lru_cache.c This file is part of DRBD by Philipp Reisner and Lars Ellenberg. Copyright (C) 2003-2008, LINBIT Information Technologies GmbH. Copyright (C) 2003-2008, Philipp Reisner <[email protected]>. Copyright (C) 2003-2008, Lars Ellenberg <[email protected]>. */ #include <linux/module.h> #include <linux/bitops.h> #include <linux/slab.h> #include <linux/string.h> /* for memset */ #include <linux/seq_file.h> /* for seq_printf */ #include <linux/lru_cache.h> MODULE_AUTHOR(…) …; MODULE_DESCRIPTION(…) …; MODULE_LICENSE(…) …; /* this is developers aid only. * it catches concurrent access (lack of locking on the users part) */ #define PARANOIA_ENTRY() … #define RETURN(x...) … /* BUG() if e is not one of the elements tracked by lc */ #define PARANOIA_LC_ELEMENT(lc, e) … /* We need to atomically * - try to grab the lock (set LC_LOCKED) * - only if there is no pending transaction * (neither LC_DIRTY nor LC_STARVING is set) * Because of PARANOIA_ENTRY() above abusing lc->flags as well, * it is not sufficient to just say * return 0 == cmpxchg(&lc->flags, 0, LC_LOCKED); */ int lc_try_lock(struct lru_cache *lc) { … } /** * lc_create - prepares to track objects in an active set * @name: descriptive name only used in lc_seq_printf_stats and lc_seq_dump_details * @cache: cache root pointer * @max_pending_changes: maximum changes to accumulate until a transaction is required * @e_count: number of elements allowed to be active simultaneously * @e_size: size of the tracked objects * @e_off: offset to the &struct lc_element member in a tracked object * * Returns a pointer to a newly initialized struct lru_cache on success, * or NULL on (allocation) failure. */ struct lru_cache *lc_create(const char *name, struct kmem_cache *cache, unsigned max_pending_changes, unsigned e_count, size_t e_size, size_t e_off) { … } static void lc_free_by_index(struct lru_cache *lc, unsigned i) { … } /** * lc_destroy - frees memory allocated by lc_create() * @lc: the lru cache to destroy */ void lc_destroy(struct lru_cache *lc) { … } /** * lc_reset - does a full reset for @lc and the hash table slots. * @lc: the lru cache to operate on * * It is roughly the equivalent of re-allocating a fresh lru_cache object, * basically a short cut to lc_destroy(lc); lc = lc_create(...); */ void lc_reset(struct lru_cache *lc) { … } /** * lc_seq_printf_stats - print stats about @lc into @seq * @seq: the seq_file to print into * @lc: the lru cache to print statistics of */ void lc_seq_printf_stats(struct seq_file *seq, struct lru_cache *lc) { … } static struct hlist_head *lc_hash_slot(struct lru_cache *lc, unsigned int enr) { … } static struct lc_element *__lc_find(struct lru_cache *lc, unsigned int enr, bool include_changing) { … } /** * lc_find - find element by label, if present in the hash table * @lc: The lru_cache object * @enr: element number * * Returns the pointer to an element, if the element with the requested * "label" or element number is present in the hash table, * or NULL if not found. Does not change the refcnt. * Ignores elements that are "about to be used", i.e. not yet in the active * set, but still pending transaction commit. */ struct lc_element *lc_find(struct lru_cache *lc, unsigned int enr) { … } /** * lc_is_used - find element by label * @lc: The lru_cache object * @enr: element number * * Returns true, if the element with the requested "label" or element number is * present in the hash table, and is used (refcnt > 0). * Also finds elements that are not _currently_ used but only "about to be * used", i.e. on the "to_be_changed" list, pending transaction commit. */ bool lc_is_used(struct lru_cache *lc, unsigned int enr) { … } /** * lc_del - removes an element from the cache * @lc: The lru_cache object * @e: The element to remove * * @e must be unused (refcnt == 0). Moves @e from "lru" to "free" list, * sets @e->enr to %LC_FREE. */ void lc_del(struct lru_cache *lc, struct lc_element *e) { … } static struct lc_element *lc_prepare_for_change(struct lru_cache *lc, unsigned new_number) { … } static int lc_unused_element_available(struct lru_cache *lc) { … } /* used as internal flags to __lc_get */ enum { … }; static struct lc_element *__lc_get(struct lru_cache *lc, unsigned int enr, unsigned int flags) { … } /** * lc_get - get element by label, maybe change the active set * @lc: the lru cache to operate on * @enr: the label to look up * * Finds an element in the cache, increases its usage count, * "touches" and returns it. * * In case the requested number is not present, it needs to be added to the * cache. Therefore it is possible that an other element becomes evicted from * the cache. In either case, the user is notified so he is able to e.g. keep * a persistent log of the cache changes, and therefore the objects in use. * * Return values: * NULL * The cache was marked %LC_STARVING, * or the requested label was not in the active set * and a changing transaction is still pending (@lc was marked %LC_DIRTY). * Or no unused or free element could be recycled (@lc will be marked as * %LC_STARVING, blocking further lc_get() operations). * * pointer to the element with the REQUESTED element number. * In this case, it can be used right away * * pointer to an UNUSED element with some different element number, * where that different number may also be %LC_FREE. * * In this case, the cache is marked %LC_DIRTY, * so lc_try_lock() will no longer succeed. * The returned element pointer is moved to the "to_be_changed" list, * and registered with the new element number on the hash collision chains, * so it is possible to pick it up from lc_is_used(). * Up to "max_pending_changes" (see lc_create()) can be accumulated. * The user now should do whatever housekeeping is necessary, * typically serialize on lc_try_lock_for_transaction(), then call * lc_committed(lc) and lc_unlock(), to finish the change. * * NOTE: The user needs to check the lc_number on EACH use, so he recognizes * any cache set change. */ struct lc_element *lc_get(struct lru_cache *lc, unsigned int enr) { … } /** * lc_get_cumulative - like lc_get; also finds to-be-changed elements * @lc: the lru cache to operate on * @enr: the label to look up * * Unlike lc_get this also returns the element for @enr, if it is belonging to * a pending transaction, so the return values are like for lc_get(), * plus: * * pointer to an element already on the "to_be_changed" list. * In this case, the cache was already marked %LC_DIRTY. * * Caller needs to make sure that the pending transaction is completed, * before proceeding to actually use this element. */ struct lc_element *lc_get_cumulative(struct lru_cache *lc, unsigned int enr) { … } /** * lc_try_get - get element by label, if present; do not change the active set * @lc: the lru cache to operate on * @enr: the label to look up * * Finds an element in the cache, increases its usage count, * "touches" and returns it. * * Return values: * NULL * The cache was marked %LC_STARVING, * or the requested label was not in the active set * * pointer to the element with the REQUESTED element number. * In this case, it can be used right away */ struct lc_element *lc_try_get(struct lru_cache *lc, unsigned int enr) { … } /** * lc_committed - tell @lc that pending changes have been recorded * @lc: the lru cache to operate on * * User is expected to serialize on explicit lc_try_lock_for_transaction() * before the transaction is started, and later needs to lc_unlock() explicitly * as well. */ void lc_committed(struct lru_cache *lc) { … } /** * lc_put - give up refcnt of @e * @lc: the lru cache to operate on * @e: the element to put * * If refcnt reaches zero, the element is moved to the lru list, * and a %LC_STARVING (if set) is cleared. * Returns the new (post-decrement) refcnt. */ unsigned int lc_put(struct lru_cache *lc, struct lc_element *e) { … } /** * lc_element_by_index * @lc: the lru cache to operate on * @i: the index of the element to return */ struct lc_element *lc_element_by_index(struct lru_cache *lc, unsigned i) { … } /** * lc_seq_dump_details - Dump a complete LRU cache to seq in textual form. * @lc: the lru cache to operate on * @seq: the &struct seq_file pointer to seq_printf into * @utext: user supplied additional "heading" or other info * @detail: function pointer the user may provide to dump further details * of the object the lc_element is embedded in. May be NULL. * Note: a leading space ' ' and trailing newline '\n' is implied. */ void lc_seq_dump_details(struct seq_file *seq, struct lru_cache *lc, char *utext, void (*detail) (struct seq_file *, struct lc_element *)) { … } EXPORT_SYMBOL(…); EXPORT_SYMBOL(…); EXPORT_SYMBOL(…); EXPORT_SYMBOL(…); EXPORT_SYMBOL(…); EXPORT_SYMBOL(…); EXPORT_SYMBOL(…); EXPORT_SYMBOL(…); EXPORT_SYMBOL(…); EXPORT_SYMBOL(…); EXPORT_SYMBOL(…); EXPORT_SYMBOL(…); EXPORT_SYMBOL(…); EXPORT_SYMBOL(…); EXPORT_SYMBOL(…);