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