shrinker.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 extention 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. /*
  14. * How many objects scan_objects should scan and try to reclaim.
  15. * This is reset before every call, so it is safe for callees
  16. * to modify.
  17. */
  18. unsigned long nr_to_scan;
  19. /*
  20. * How many objects did scan_objects process?
  21. * This defaults to nr_to_scan before every call, but the callee
  22. * should track its actual progress.
  23. */
  24. unsigned long nr_scanned;
  25. /* current node being shrunk (for NUMA aware shrinkers) */
  26. int nid;
  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. int seeks; /* seeks to recreate an obj */
  60. long batch; /* reclaim batch size, 0 = default */
  61. unsigned long flags;
  62. /* These are for internal use */
  63. struct list_head list;
  64. #ifdef CONFIG_MEMCG_KMEM
  65. /* ID in shrinker_idr */
  66. int id;
  67. #endif
  68. /* objs pending delete, per node */
  69. atomic_long_t *nr_deferred;
  70. };
  71. #define DEFAULT_SEEKS 2 /* A good number if you don't know better. */
  72. /* Flags */
  73. #define SHRINKER_NUMA_AWARE (1 << 0)
  74. #define SHRINKER_MEMCG_AWARE (1 << 1)
  75. extern int prealloc_shrinker(struct shrinker *shrinker);
  76. extern void register_shrinker_prepared(struct shrinker *shrinker);
  77. extern int register_shrinker(struct shrinker *shrinker);
  78. extern void unregister_shrinker(struct shrinker *shrinker);
  79. extern void free_prealloced_shrinker(struct shrinker *shrinker);
  80. #endif