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> 113b1d58a4SDave Chinner #include <linux/nodemask.h> 12*503c358cSVladimir Davydov #include <linux/shrinker.h> 13a38e4082SDave Chinner 14a38e4082SDave Chinner /* list_lru_walk_cb has to always return one of those */ 15a38e4082SDave Chinner enum lru_status { 16a38e4082SDave Chinner LRU_REMOVED, /* item removed from list */ 17449dd698SJohannes Weiner LRU_REMOVED_RETRY, /* item removed, but lock has been 18449dd698SJohannes Weiner dropped and reacquired */ 19a38e4082SDave Chinner LRU_ROTATE, /* item referenced, give another pass */ 20a38e4082SDave Chinner LRU_SKIP, /* item cannot be locked, skip */ 21a38e4082SDave Chinner LRU_RETRY, /* item not freeable. May drop the lock 22a38e4082SDave Chinner internally, but has to return locked. */ 23a38e4082SDave Chinner }; 24a38e4082SDave Chinner 253b1d58a4SDave Chinner struct list_lru_node { 26a38e4082SDave Chinner spinlock_t lock; 27a38e4082SDave Chinner struct list_head list; 28a38e4082SDave Chinner /* kept as signed so we can catch imbalance bugs */ 29a38e4082SDave Chinner long nr_items; 303b1d58a4SDave Chinner } ____cacheline_aligned_in_smp; 313b1d58a4SDave Chinner 323b1d58a4SDave Chinner struct list_lru { 335ca302c8SGlauber Costa struct list_lru_node *node; 343b1d58a4SDave Chinner nodemask_t active_nodes; 35a38e4082SDave Chinner }; 36a38e4082SDave Chinner 375ca302c8SGlauber Costa void list_lru_destroy(struct list_lru *lru); 38449dd698SJohannes Weiner int list_lru_init_key(struct list_lru *lru, struct lock_class_key *key); 39449dd698SJohannes Weiner static inline int list_lru_init(struct list_lru *lru) 40449dd698SJohannes Weiner { 41449dd698SJohannes Weiner return list_lru_init_key(lru, NULL); 42449dd698SJohannes Weiner } 43a38e4082SDave Chinner 44a38e4082SDave Chinner /** 45a38e4082SDave Chinner * list_lru_add: add an element to the lru list's tail 46a38e4082SDave Chinner * @list_lru: the lru pointer 47a38e4082SDave Chinner * @item: the item to be added. 48a38e4082SDave Chinner * 49a38e4082SDave Chinner * If the element is already part of a list, this function returns doing 50a38e4082SDave Chinner * nothing. Therefore the caller does not need to keep state about whether or 51a38e4082SDave Chinner * not the element already belongs in the list and is allowed to lazy update 52a38e4082SDave Chinner * it. Note however that this is valid for *a* list, not *this* list. If 53a38e4082SDave Chinner * the caller organize itself in a way that elements can be in more than 54a38e4082SDave Chinner * one type of list, it is up to the caller to fully remove the item from 55a38e4082SDave Chinner * the previous list (with list_lru_del() for instance) before moving it 56a38e4082SDave Chinner * to @list_lru 57a38e4082SDave Chinner * 58a38e4082SDave Chinner * Return value: true if the list was updated, false otherwise 59a38e4082SDave Chinner */ 60a38e4082SDave Chinner bool list_lru_add(struct list_lru *lru, struct list_head *item); 61a38e4082SDave Chinner 62a38e4082SDave Chinner /** 63a38e4082SDave Chinner * list_lru_del: delete an element to the lru list 64a38e4082SDave Chinner * @list_lru: the lru pointer 65a38e4082SDave Chinner * @item: the item to be deleted. 66a38e4082SDave Chinner * 67a38e4082SDave Chinner * This function works analogously as list_lru_add in terms of list 68a38e4082SDave Chinner * manipulation. The comments about an element already pertaining to 69a38e4082SDave Chinner * a list are also valid for list_lru_del. 70a38e4082SDave Chinner * 71a38e4082SDave Chinner * Return value: true if the list was updated, false otherwise 72a38e4082SDave Chinner */ 73a38e4082SDave Chinner bool list_lru_del(struct list_lru *lru, struct list_head *item); 74a38e4082SDave Chinner 75a38e4082SDave Chinner /** 766a4f496fSGlauber Costa * list_lru_count_node: return the number of objects currently held by @lru 77a38e4082SDave Chinner * @lru: the lru pointer. 786a4f496fSGlauber Costa * @nid: the node id to count from. 79a38e4082SDave Chinner * 80a38e4082SDave Chinner * Always return a non-negative number, 0 for empty lists. There is no 81a38e4082SDave Chinner * guarantee that the list is not updated while the count is being computed. 82a38e4082SDave Chinner * Callers that want such a guarantee need to provide an outer lock. 83a38e4082SDave Chinner */ 846a4f496fSGlauber Costa unsigned long list_lru_count_node(struct list_lru *lru, int nid); 85*503c358cSVladimir Davydov 86*503c358cSVladimir Davydov static inline unsigned long list_lru_shrink_count(struct list_lru *lru, 87*503c358cSVladimir Davydov struct shrink_control *sc) 88*503c358cSVladimir Davydov { 89*503c358cSVladimir Davydov return list_lru_count_node(lru, sc->nid); 90*503c358cSVladimir Davydov } 91*503c358cSVladimir Davydov 926a4f496fSGlauber Costa static inline unsigned long list_lru_count(struct list_lru *lru) 936a4f496fSGlauber Costa { 946a4f496fSGlauber Costa long count = 0; 956a4f496fSGlauber Costa int nid; 966a4f496fSGlauber Costa 976a4f496fSGlauber Costa for_each_node_mask(nid, lru->active_nodes) 986a4f496fSGlauber Costa count += list_lru_count_node(lru, nid); 996a4f496fSGlauber Costa 1006a4f496fSGlauber Costa return count; 1016a4f496fSGlauber Costa } 102a38e4082SDave Chinner 103a38e4082SDave Chinner typedef enum lru_status 104a38e4082SDave Chinner (*list_lru_walk_cb)(struct list_head *item, spinlock_t *lock, void *cb_arg); 105a38e4082SDave Chinner /** 1066a4f496fSGlauber Costa * list_lru_walk_node: walk a list_lru, isolating and disposing freeable items. 107a38e4082SDave Chinner * @lru: the lru pointer. 1086a4f496fSGlauber Costa * @nid: the node id to scan from. 109a38e4082SDave Chinner * @isolate: callback function that is resposible for deciding what to do with 110a38e4082SDave Chinner * the item currently being scanned 111a38e4082SDave Chinner * @cb_arg: opaque type that will be passed to @isolate 112a38e4082SDave Chinner * @nr_to_walk: how many items to scan. 113a38e4082SDave Chinner * 114a38e4082SDave Chinner * This function will scan all elements in a particular list_lru, calling the 115a38e4082SDave Chinner * @isolate callback for each of those items, along with the current list 116a38e4082SDave Chinner * spinlock and a caller-provided opaque. The @isolate callback can choose to 117a38e4082SDave Chinner * drop the lock internally, but *must* return with the lock held. The callback 118a38e4082SDave Chinner * will return an enum lru_status telling the list_lru infrastructure what to 119a38e4082SDave Chinner * do with the object being scanned. 120a38e4082SDave Chinner * 121a38e4082SDave Chinner * Please note that nr_to_walk does not mean how many objects will be freed, 122a38e4082SDave Chinner * just how many objects will be scanned. 123a38e4082SDave Chinner * 124a38e4082SDave Chinner * Return value: the number of objects effectively removed from the LRU. 125a38e4082SDave Chinner */ 1266a4f496fSGlauber Costa unsigned long list_lru_walk_node(struct list_lru *lru, int nid, 1276a4f496fSGlauber Costa list_lru_walk_cb isolate, void *cb_arg, 1286a4f496fSGlauber Costa unsigned long *nr_to_walk); 1296a4f496fSGlauber Costa 1306a4f496fSGlauber Costa static inline unsigned long 131*503c358cSVladimir Davydov list_lru_shrink_walk(struct list_lru *lru, struct shrink_control *sc, 132*503c358cSVladimir Davydov list_lru_walk_cb isolate, void *cb_arg) 133*503c358cSVladimir Davydov { 134*503c358cSVladimir Davydov return list_lru_walk_node(lru, sc->nid, isolate, cb_arg, 135*503c358cSVladimir Davydov &sc->nr_to_scan); 136*503c358cSVladimir Davydov } 137*503c358cSVladimir Davydov 138*503c358cSVladimir Davydov static inline unsigned long 1396a4f496fSGlauber Costa list_lru_walk(struct list_lru *lru, list_lru_walk_cb isolate, 1406a4f496fSGlauber Costa void *cb_arg, unsigned long nr_to_walk) 1416a4f496fSGlauber Costa { 1426a4f496fSGlauber Costa long isolated = 0; 1436a4f496fSGlauber Costa int nid; 1446a4f496fSGlauber Costa 1456a4f496fSGlauber Costa for_each_node_mask(nid, lru->active_nodes) { 1466a4f496fSGlauber Costa isolated += list_lru_walk_node(lru, nid, isolate, 1476a4f496fSGlauber Costa cb_arg, &nr_to_walk); 1486a4f496fSGlauber Costa if (nr_to_walk <= 0) 1496a4f496fSGlauber Costa break; 1506a4f496fSGlauber Costa } 1516a4f496fSGlauber Costa return isolated; 1526a4f496fSGlauber Costa } 153a38e4082SDave Chinner #endif /* _LRU_LIST_H */ 154