list_lru.h 4.2 KB

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