page_counter.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_PAGE_COUNTER_H
  3. #define _LINUX_PAGE_COUNTER_H
  4. #include <linux/atomic.h>
  5. #include <linux/cache.h>
  6. #include <linux/kernel.h>
  7. #include <asm/page.h>
  8. struct page_counter {
  9. /*
  10. * Make sure 'usage' does not share cacheline with any other field. The
  11. * memcg->memory.usage is a hot member of struct mem_cgroup.
  12. */
  13. atomic_long_t usage;
  14. CACHELINE_PADDING(_pad1_);
  15. /* effective memory.min and memory.min usage tracking */
  16. unsigned long emin;
  17. atomic_long_t min_usage;
  18. atomic_long_t children_min_usage;
  19. /* effective memory.low and memory.low usage tracking */
  20. unsigned long elow;
  21. atomic_long_t low_usage;
  22. atomic_long_t children_low_usage;
  23. unsigned long watermark;
  24. unsigned long failcnt;
  25. /* Keep all the read most fields in a separete cacheline. */
  26. CACHELINE_PADDING(_pad2_);
  27. unsigned long min;
  28. unsigned long low;
  29. unsigned long high;
  30. unsigned long max;
  31. struct page_counter *parent;
  32. } ____cacheline_internodealigned_in_smp;
  33. #if BITS_PER_LONG == 32
  34. #define PAGE_COUNTER_MAX LONG_MAX
  35. #else
  36. #define PAGE_COUNTER_MAX (LONG_MAX / PAGE_SIZE)
  37. #endif
  38. static inline void page_counter_init(struct page_counter *counter,
  39. struct page_counter *parent)
  40. {
  41. atomic_long_set(&counter->usage, 0);
  42. counter->max = PAGE_COUNTER_MAX;
  43. counter->parent = parent;
  44. }
  45. static inline unsigned long page_counter_read(struct page_counter *counter)
  46. {
  47. return atomic_long_read(&counter->usage);
  48. }
  49. void page_counter_cancel(struct page_counter *counter, unsigned long nr_pages);
  50. void page_counter_charge(struct page_counter *counter, unsigned long nr_pages);
  51. bool page_counter_try_charge(struct page_counter *counter,
  52. unsigned long nr_pages,
  53. struct page_counter **fail);
  54. void page_counter_uncharge(struct page_counter *counter, unsigned long nr_pages);
  55. void page_counter_set_min(struct page_counter *counter, unsigned long nr_pages);
  56. void page_counter_set_low(struct page_counter *counter, unsigned long nr_pages);
  57. static inline void page_counter_set_high(struct page_counter *counter,
  58. unsigned long nr_pages)
  59. {
  60. WRITE_ONCE(counter->high, nr_pages);
  61. }
  62. int page_counter_set_max(struct page_counter *counter, unsigned long nr_pages);
  63. int page_counter_memparse(const char *buf, const char *max,
  64. unsigned long *nr_pages);
  65. static inline void page_counter_reset_watermark(struct page_counter *counter)
  66. {
  67. counter->watermark = page_counter_read(counter);
  68. }
  69. #endif /* _LINUX_PAGE_COUNTER_H */