1*a38e4082SDave Chinner /* 2*a38e4082SDave Chinner * Copyright (c) 2013 Red Hat, Inc. and Parallels Inc. All rights reserved. 3*a38e4082SDave Chinner * Authors: David Chinner and Glauber Costa 4*a38e4082SDave Chinner * 5*a38e4082SDave Chinner * Generic LRU infrastructure 6*a38e4082SDave Chinner */ 7*a38e4082SDave Chinner #ifndef _LRU_LIST_H 8*a38e4082SDave Chinner #define _LRU_LIST_H 9*a38e4082SDave Chinner 10*a38e4082SDave Chinner #include <linux/list.h> 11*a38e4082SDave Chinner 12*a38e4082SDave Chinner /* list_lru_walk_cb has to always return one of those */ 13*a38e4082SDave Chinner enum lru_status { 14*a38e4082SDave Chinner LRU_REMOVED, /* item removed from list */ 15*a38e4082SDave Chinner LRU_ROTATE, /* item referenced, give another pass */ 16*a38e4082SDave Chinner LRU_SKIP, /* item cannot be locked, skip */ 17*a38e4082SDave Chinner LRU_RETRY, /* item not freeable. May drop the lock 18*a38e4082SDave Chinner internally, but has to return locked. */ 19*a38e4082SDave Chinner }; 20*a38e4082SDave Chinner 21*a38e4082SDave Chinner struct list_lru { 22*a38e4082SDave Chinner spinlock_t lock; 23*a38e4082SDave Chinner struct list_head list; 24*a38e4082SDave Chinner /* kept as signed so we can catch imbalance bugs */ 25*a38e4082SDave Chinner long nr_items; 26*a38e4082SDave Chinner }; 27*a38e4082SDave Chinner 28*a38e4082SDave Chinner int list_lru_init(struct list_lru *lru); 29*a38e4082SDave Chinner 30*a38e4082SDave Chinner /** 31*a38e4082SDave Chinner * list_lru_add: add an element to the lru list's tail 32*a38e4082SDave Chinner * @list_lru: the lru pointer 33*a38e4082SDave Chinner * @item: the item to be added. 34*a38e4082SDave Chinner * 35*a38e4082SDave Chinner * If the element is already part of a list, this function returns doing 36*a38e4082SDave Chinner * nothing. Therefore the caller does not need to keep state about whether or 37*a38e4082SDave Chinner * not the element already belongs in the list and is allowed to lazy update 38*a38e4082SDave Chinner * it. Note however that this is valid for *a* list, not *this* list. If 39*a38e4082SDave Chinner * the caller organize itself in a way that elements can be in more than 40*a38e4082SDave Chinner * one type of list, it is up to the caller to fully remove the item from 41*a38e4082SDave Chinner * the previous list (with list_lru_del() for instance) before moving it 42*a38e4082SDave Chinner * to @list_lru 43*a38e4082SDave Chinner * 44*a38e4082SDave Chinner * Return value: true if the list was updated, false otherwise 45*a38e4082SDave Chinner */ 46*a38e4082SDave Chinner bool list_lru_add(struct list_lru *lru, struct list_head *item); 47*a38e4082SDave Chinner 48*a38e4082SDave Chinner /** 49*a38e4082SDave Chinner * list_lru_del: delete an element to the lru list 50*a38e4082SDave Chinner * @list_lru: the lru pointer 51*a38e4082SDave Chinner * @item: the item to be deleted. 52*a38e4082SDave Chinner * 53*a38e4082SDave Chinner * This function works analogously as list_lru_add in terms of list 54*a38e4082SDave Chinner * manipulation. The comments about an element already pertaining to 55*a38e4082SDave Chinner * a list are also valid for list_lru_del. 56*a38e4082SDave Chinner * 57*a38e4082SDave Chinner * Return value: true if the list was updated, false otherwise 58*a38e4082SDave Chinner */ 59*a38e4082SDave Chinner bool list_lru_del(struct list_lru *lru, struct list_head *item); 60*a38e4082SDave Chinner 61*a38e4082SDave Chinner /** 62*a38e4082SDave Chinner * list_lru_count: return the number of objects currently held by @lru 63*a38e4082SDave Chinner * @lru: the lru pointer. 64*a38e4082SDave Chinner * 65*a38e4082SDave Chinner * Always return a non-negative number, 0 for empty lists. There is no 66*a38e4082SDave Chinner * guarantee that the list is not updated while the count is being computed. 67*a38e4082SDave Chinner * Callers that want such a guarantee need to provide an outer lock. 68*a38e4082SDave Chinner */ 69*a38e4082SDave Chinner static inline unsigned long list_lru_count(struct list_lru *lru) 70*a38e4082SDave Chinner { 71*a38e4082SDave Chinner return lru->nr_items; 72*a38e4082SDave Chinner } 73*a38e4082SDave Chinner 74*a38e4082SDave Chinner typedef enum lru_status 75*a38e4082SDave Chinner (*list_lru_walk_cb)(struct list_head *item, spinlock_t *lock, void *cb_arg); 76*a38e4082SDave Chinner /** 77*a38e4082SDave Chinner * list_lru_walk: walk a list_lru, isolating and disposing freeable items. 78*a38e4082SDave Chinner * @lru: the lru pointer. 79*a38e4082SDave Chinner * @isolate: callback function that is resposible for deciding what to do with 80*a38e4082SDave Chinner * the item currently being scanned 81*a38e4082SDave Chinner * @cb_arg: opaque type that will be passed to @isolate 82*a38e4082SDave Chinner * @nr_to_walk: how many items to scan. 83*a38e4082SDave Chinner * 84*a38e4082SDave Chinner * This function will scan all elements in a particular list_lru, calling the 85*a38e4082SDave Chinner * @isolate callback for each of those items, along with the current list 86*a38e4082SDave Chinner * spinlock and a caller-provided opaque. The @isolate callback can choose to 87*a38e4082SDave Chinner * drop the lock internally, but *must* return with the lock held. The callback 88*a38e4082SDave Chinner * will return an enum lru_status telling the list_lru infrastructure what to 89*a38e4082SDave Chinner * do with the object being scanned. 90*a38e4082SDave Chinner * 91*a38e4082SDave Chinner * Please note that nr_to_walk does not mean how many objects will be freed, 92*a38e4082SDave Chinner * just how many objects will be scanned. 93*a38e4082SDave Chinner * 94*a38e4082SDave Chinner * Return value: the number of objects effectively removed from the LRU. 95*a38e4082SDave Chinner */ 96*a38e4082SDave Chinner unsigned long list_lru_walk(struct list_lru *lru, list_lru_walk_cb isolate, 97*a38e4082SDave Chinner void *cb_arg, unsigned long nr_to_walk); 98*a38e4082SDave Chinner 99*a38e4082SDave Chinner typedef void (*list_lru_dispose_cb)(struct list_head *dispose_list); 100*a38e4082SDave Chinner /** 101*a38e4082SDave Chinner * list_lru_dispose_all: forceably flush all elements in an @lru 102*a38e4082SDave Chinner * @lru: the lru pointer 103*a38e4082SDave Chinner * @dispose: callback function to be called for each lru list. 104*a38e4082SDave Chinner * 105*a38e4082SDave Chinner * This function will forceably isolate all elements into the dispose list, and 106*a38e4082SDave Chinner * call the @dispose callback to flush the list. Please note that the callback 107*a38e4082SDave Chinner * should expect items in any state, clean or dirty, and be able to flush all of 108*a38e4082SDave Chinner * them. 109*a38e4082SDave Chinner * 110*a38e4082SDave Chinner * Return value: how many objects were freed. It should be equal to all objects 111*a38e4082SDave Chinner * in the list_lru. 112*a38e4082SDave Chinner */ 113*a38e4082SDave Chinner unsigned long 114*a38e4082SDave Chinner list_lru_dispose_all(struct list_lru *lru, list_lru_dispose_cb dispose); 115*a38e4082SDave Chinner #endif /* _LRU_LIST_H */ 116