swap_table.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _MM_SWAP_TABLE_H
  3. #define _MM_SWAP_TABLE_H
  4. #include <linux/rcupdate.h>
  5. #include <linux/atomic.h>
  6. #include "swap.h"
  7. /* A typical flat array in each cluster as swap table */
  8. struct swap_table {
  9. atomic_long_t entries[SWAPFILE_CLUSTER];
  10. };
  11. #define SWP_TABLE_USE_PAGE (sizeof(struct swap_table) == PAGE_SIZE)
  12. /*
  13. * A swap table entry represents the status of a swap slot on a swap
  14. * (physical or virtual) device. The swap table in each cluster is a
  15. * 1:1 map of the swap slots in this cluster.
  16. *
  17. * Each swap table entry could be a pointer (folio), a XA_VALUE
  18. * (shadow), or NULL.
  19. */
  20. /*
  21. * Helpers for casting one type of info into a swap table entry.
  22. */
  23. static inline unsigned long null_to_swp_tb(void)
  24. {
  25. BUILD_BUG_ON(sizeof(unsigned long) != sizeof(atomic_long_t));
  26. return 0;
  27. }
  28. static inline unsigned long folio_to_swp_tb(struct folio *folio)
  29. {
  30. BUILD_BUG_ON(sizeof(unsigned long) != sizeof(void *));
  31. return (unsigned long)folio;
  32. }
  33. static inline unsigned long shadow_swp_to_tb(void *shadow)
  34. {
  35. BUILD_BUG_ON((BITS_PER_XA_VALUE + 1) !=
  36. BITS_PER_BYTE * sizeof(unsigned long));
  37. VM_WARN_ON_ONCE(shadow && !xa_is_value(shadow));
  38. return (unsigned long)shadow;
  39. }
  40. /*
  41. * Helpers for swap table entry type checking.
  42. */
  43. static inline bool swp_tb_is_null(unsigned long swp_tb)
  44. {
  45. return !swp_tb;
  46. }
  47. static inline bool swp_tb_is_folio(unsigned long swp_tb)
  48. {
  49. return !xa_is_value((void *)swp_tb) && !swp_tb_is_null(swp_tb);
  50. }
  51. static inline bool swp_tb_is_shadow(unsigned long swp_tb)
  52. {
  53. return xa_is_value((void *)swp_tb);
  54. }
  55. /*
  56. * Helpers for retrieving info from swap table.
  57. */
  58. static inline struct folio *swp_tb_to_folio(unsigned long swp_tb)
  59. {
  60. VM_WARN_ON(!swp_tb_is_folio(swp_tb));
  61. return (void *)swp_tb;
  62. }
  63. static inline void *swp_tb_to_shadow(unsigned long swp_tb)
  64. {
  65. VM_WARN_ON(!swp_tb_is_shadow(swp_tb));
  66. return (void *)swp_tb;
  67. }
  68. /*
  69. * Helpers for accessing or modifying the swap table of a cluster,
  70. * the swap cluster must be locked.
  71. */
  72. static inline void __swap_table_set(struct swap_cluster_info *ci,
  73. unsigned int off, unsigned long swp_tb)
  74. {
  75. atomic_long_t *table = rcu_dereference_protected(ci->table, true);
  76. lockdep_assert_held(&ci->lock);
  77. VM_WARN_ON_ONCE(off >= SWAPFILE_CLUSTER);
  78. atomic_long_set(&table[off], swp_tb);
  79. }
  80. static inline unsigned long __swap_table_xchg(struct swap_cluster_info *ci,
  81. unsigned int off, unsigned long swp_tb)
  82. {
  83. atomic_long_t *table = rcu_dereference_protected(ci->table, true);
  84. lockdep_assert_held(&ci->lock);
  85. VM_WARN_ON_ONCE(off >= SWAPFILE_CLUSTER);
  86. /* Ordering is guaranteed by cluster lock, relax */
  87. return atomic_long_xchg_relaxed(&table[off], swp_tb);
  88. }
  89. static inline unsigned long __swap_table_get(struct swap_cluster_info *ci,
  90. unsigned int off)
  91. {
  92. atomic_long_t *table;
  93. VM_WARN_ON_ONCE(off >= SWAPFILE_CLUSTER);
  94. table = rcu_dereference_check(ci->table, lockdep_is_held(&ci->lock));
  95. return atomic_long_read(&table[off]);
  96. }
  97. static inline unsigned long swap_table_get(struct swap_cluster_info *ci,
  98. unsigned int off)
  99. {
  100. atomic_long_t *table;
  101. unsigned long swp_tb;
  102. rcu_read_lock();
  103. table = rcu_dereference(ci->table);
  104. swp_tb = table ? atomic_long_read(&table[off]) : null_to_swp_tb();
  105. rcu_read_unlock();
  106. return swp_tb;
  107. }
  108. #endif