shrinker.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_SHRINKER_H
  3. #define _LINUX_SHRINKER_H
  4. /*
  5. * This struct is used to pass information from page reclaim to the shrinkers.
  6. * We consolidate the values for easier extension later.
  7. *
  8. * The 'gfpmask' refers to the allocation we are currently trying to
  9. * fulfil.
  10. */
  11. struct shrink_control {
  12. gfp_t gfp_mask;
  13. /* current node being shrunk (for NUMA aware shrinkers) */
  14. int nid;
  15. /*
  16. * How many objects scan_objects should scan and try to reclaim.
  17. * This is reset before every call, so it is safe for callees
  18. * to modify.
  19. */
  20. unsigned long nr_to_scan;
  21. /*
  22. * How many objects did scan_objects process?
  23. * This defaults to nr_to_scan before every call, but the callee
  24. * should track its actual progress.
  25. */
  26. unsigned long nr_scanned;
  27. /* current memcg being shrunk (for memcg aware shrinkers) */
  28. struct mem_cgroup *memcg;
  29. };
  30. #define SHRINK_STOP (~0UL)
  31. #define SHRINK_EMPTY (~0UL - 1)
  32. /*
  33. * A callback you can register to apply pressure to ageable caches.
  34. *
  35. * @count_objects should return the number of freeable items in the cache. If
  36. * there are no objects to free, it should return SHRINK_EMPTY, while 0 is
  37. * returned in cases of the number of freeable items cannot be determined
  38. * or shrinker should skip this cache for this time (e.g., their number
  39. * is below shrinkable limit). No deadlock checks should be done during the
  40. * count callback - the shrinker relies on aggregating scan counts that couldn't
  41. * be executed due to potential deadlocks to be run at a later call when the
  42. * deadlock condition is no longer pending.
  43. *
  44. * @scan_objects will only be called if @count_objects returned a non-zero
  45. * value for the number of freeable objects. The callout should scan the cache
  46. * and attempt to free items from the cache. It should then return the number
  47. * of objects freed during the scan, or SHRINK_STOP if progress cannot be made
  48. * due to potential deadlocks. If SHRINK_STOP is returned, then no further
  49. * attempts to call the @scan_objects will be made from the current reclaim
  50. * context.
  51. *
  52. * @flags determine the shrinker abilities, like numa awareness
  53. */
  54. struct shrinker {
  55. unsigned long (*count_objects)(struct shrinker *,
  56. struct shrink_control *sc);
  57. unsigned long (*scan_objects)(struct shrinker *,
  58. struct shrink_control *sc);
  59. long batch; /* reclaim batch size, 0 = default */
  60. int seeks; /* seeks to recreate an obj */
  61. unsigned flags;
  62. /* These are for internal use */
  63. struct list_head list;
  64. #ifdef CONFIG_MEMCG
  65. /* ID in shrinker_idr */
  66. int id;
  67. #endif
  68. #ifdef CONFIG_SHRINKER_DEBUG
  69. int debugfs_id;
  70. const char *name;
  71. struct dentry *debugfs_entry;
  72. #endif
  73. /* objs pending delete, per node */
  74. atomic_long_t *nr_deferred;
  75. };
  76. #define DEFAULT_SEEKS 2 /* A good number if you don't know better. */
  77. /* Flags */
  78. #define SHRINKER_REGISTERED (1 << 0)
  79. #define SHRINKER_NUMA_AWARE (1 << 1)
  80. #define SHRINKER_MEMCG_AWARE (1 << 2)
  81. /*
  82. * It just makes sense when the shrinker is also MEMCG_AWARE for now,
  83. * non-MEMCG_AWARE shrinker should not have this flag set.
  84. */
  85. #define SHRINKER_NONSLAB (1 << 3)
  86. extern int __printf(2, 3) prealloc_shrinker(struct shrinker *shrinker,
  87. const char *fmt, ...);
  88. extern void register_shrinker_prepared(struct shrinker *shrinker);
  89. extern int __printf(2, 3) register_shrinker(struct shrinker *shrinker,
  90. const char *fmt, ...);
  91. extern void unregister_shrinker(struct shrinker *shrinker);
  92. extern void free_prealloced_shrinker(struct shrinker *shrinker);
  93. extern void synchronize_shrinkers(void);
  94. #ifdef CONFIG_SHRINKER_DEBUG
  95. extern int shrinker_debugfs_add(struct shrinker *shrinker);
  96. extern struct dentry *shrinker_debugfs_remove(struct shrinker *shrinker);
  97. extern int __printf(2, 3) shrinker_debugfs_rename(struct shrinker *shrinker,
  98. const char *fmt, ...);
  99. #else /* CONFIG_SHRINKER_DEBUG */
  100. static inline int shrinker_debugfs_add(struct shrinker *shrinker)
  101. {
  102. return 0;
  103. }
  104. static inline struct dentry *shrinker_debugfs_remove(struct shrinker *shrinker)
  105. {
  106. return NULL;
  107. }
  108. static inline __printf(2, 3)
  109. int shrinker_debugfs_rename(struct shrinker *shrinker, const char *fmt, ...)
  110. {
  111. return 0;
  112. }
  113. #endif /* CONFIG_SHRINKER_DEBUG */
  114. #endif /* _LINUX_SHRINKER_H */