srcutree.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Sleepable Read-Copy Update mechanism for mutual exclusion,
  4. * tree variant.
  5. *
  6. * Copyright (C) IBM Corporation, 2017
  7. *
  8. * Author: Paul McKenney <[email protected]>
  9. */
  10. #ifndef _LINUX_SRCU_TREE_H
  11. #define _LINUX_SRCU_TREE_H
  12. #include <linux/rcu_node_tree.h>
  13. #include <linux/completion.h>
  14. struct srcu_node;
  15. struct srcu_struct;
  16. /*
  17. * Per-CPU structure feeding into leaf srcu_node, similar in function
  18. * to rcu_node.
  19. */
  20. struct srcu_data {
  21. /* Read-side state. */
  22. unsigned long srcu_lock_count[2]; /* Locks per CPU. */
  23. unsigned long srcu_unlock_count[2]; /* Unlocks per CPU. */
  24. /* Update-side state. */
  25. spinlock_t __private lock ____cacheline_internodealigned_in_smp;
  26. struct rcu_segcblist srcu_cblist; /* List of callbacks.*/
  27. unsigned long srcu_gp_seq_needed; /* Furthest future GP needed. */
  28. unsigned long srcu_gp_seq_needed_exp; /* Furthest future exp GP. */
  29. bool srcu_cblist_invoking; /* Invoking these CBs? */
  30. struct timer_list delay_work; /* Delay for CB invoking */
  31. struct work_struct work; /* Context for CB invoking. */
  32. struct rcu_head srcu_barrier_head; /* For srcu_barrier() use. */
  33. struct srcu_node *mynode; /* Leaf srcu_node. */
  34. unsigned long grpmask; /* Mask for leaf srcu_node */
  35. /* ->srcu_data_have_cbs[]. */
  36. int cpu;
  37. struct srcu_struct *ssp;
  38. };
  39. /*
  40. * Node in SRCU combining tree, similar in function to rcu_data.
  41. */
  42. struct srcu_node {
  43. spinlock_t __private lock;
  44. unsigned long srcu_have_cbs[4]; /* GP seq for children having CBs, but only */
  45. /* if greater than ->srcu_gq_seq. */
  46. unsigned long srcu_data_have_cbs[4]; /* Which srcu_data structs have CBs for given GP? */
  47. unsigned long srcu_gp_seq_needed_exp; /* Furthest future exp GP. */
  48. struct srcu_node *srcu_parent; /* Next up in tree. */
  49. int grplo; /* Least CPU for node. */
  50. int grphi; /* Biggest CPU for node. */
  51. };
  52. /*
  53. * Per-SRCU-domain structure, similar in function to rcu_state.
  54. */
  55. struct srcu_struct {
  56. struct srcu_node *node; /* Combining tree. */
  57. struct srcu_node *level[RCU_NUM_LVLS + 1];
  58. /* First node at each level. */
  59. int srcu_size_state; /* Small-to-big transition state. */
  60. struct mutex srcu_cb_mutex; /* Serialize CB preparation. */
  61. spinlock_t __private lock; /* Protect counters and size state. */
  62. struct mutex srcu_gp_mutex; /* Serialize GP work. */
  63. unsigned int srcu_idx; /* Current rdr array element. */
  64. unsigned long srcu_gp_seq; /* Grace-period seq #. */
  65. unsigned long srcu_gp_seq_needed; /* Latest gp_seq needed. */
  66. unsigned long srcu_gp_seq_needed_exp; /* Furthest future exp GP. */
  67. unsigned long srcu_gp_start; /* Last GP start timestamp (jiffies) */
  68. unsigned long srcu_last_gp_end; /* Last GP end timestamp (ns) */
  69. unsigned long srcu_size_jiffies; /* Current contention-measurement interval. */
  70. unsigned long srcu_n_lock_retries; /* Contention events in current interval. */
  71. unsigned long srcu_n_exp_nodelay; /* # expedited no-delays in current GP phase. */
  72. struct srcu_data __percpu *sda; /* Per-CPU srcu_data array. */
  73. bool sda_is_static; /* May ->sda be passed to free_percpu()? */
  74. unsigned long srcu_barrier_seq; /* srcu_barrier seq #. */
  75. struct mutex srcu_barrier_mutex; /* Serialize barrier ops. */
  76. struct completion srcu_barrier_completion;
  77. /* Awaken barrier rq at end. */
  78. atomic_t srcu_barrier_cpu_cnt; /* # CPUs not yet posting a */
  79. /* callback for the barrier */
  80. /* operation. */
  81. unsigned long reschedule_jiffies;
  82. unsigned long reschedule_count;
  83. struct delayed_work work;
  84. struct lockdep_map dep_map;
  85. };
  86. /* Values for size state variable (->srcu_size_state). */
  87. #define SRCU_SIZE_SMALL 0
  88. #define SRCU_SIZE_ALLOC 1
  89. #define SRCU_SIZE_WAIT_BARRIER 2
  90. #define SRCU_SIZE_WAIT_CALL 3
  91. #define SRCU_SIZE_WAIT_CBS1 4
  92. #define SRCU_SIZE_WAIT_CBS2 5
  93. #define SRCU_SIZE_WAIT_CBS3 6
  94. #define SRCU_SIZE_WAIT_CBS4 7
  95. #define SRCU_SIZE_BIG 8
  96. /* Values for state variable (bottom bits of ->srcu_gp_seq). */
  97. #define SRCU_STATE_IDLE 0
  98. #define SRCU_STATE_SCAN1 1
  99. #define SRCU_STATE_SCAN2 2
  100. #define __SRCU_STRUCT_INIT(name, pcpu_name) \
  101. { \
  102. .sda = &pcpu_name, \
  103. .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
  104. .srcu_gp_seq_needed = -1UL, \
  105. .work = __DELAYED_WORK_INITIALIZER(name.work, NULL, 0), \
  106. __SRCU_DEP_MAP_INIT(name) \
  107. }
  108. /*
  109. * Define and initialize a srcu struct at build time.
  110. * Do -not- call init_srcu_struct() nor cleanup_srcu_struct() on it.
  111. *
  112. * Note that although DEFINE_STATIC_SRCU() hides the name from other
  113. * files, the per-CPU variable rules nevertheless require that the
  114. * chosen name be globally unique. These rules also prohibit use of
  115. * DEFINE_STATIC_SRCU() within a function. If these rules are too
  116. * restrictive, declare the srcu_struct manually. For example, in
  117. * each file:
  118. *
  119. * static struct srcu_struct my_srcu;
  120. *
  121. * Then, before the first use of each my_srcu, manually initialize it:
  122. *
  123. * init_srcu_struct(&my_srcu);
  124. *
  125. * See include/linux/percpu-defs.h for the rules on per-CPU variables.
  126. */
  127. #ifdef MODULE
  128. # define __DEFINE_SRCU(name, is_static) \
  129. is_static struct srcu_struct name; \
  130. extern struct srcu_struct * const __srcu_struct_##name; \
  131. struct srcu_struct * const __srcu_struct_##name \
  132. __section("___srcu_struct_ptrs") = &name
  133. #else
  134. # define __DEFINE_SRCU(name, is_static) \
  135. static DEFINE_PER_CPU(struct srcu_data, name##_srcu_data); \
  136. is_static struct srcu_struct name = \
  137. __SRCU_STRUCT_INIT(name, name##_srcu_data)
  138. #endif
  139. #define DEFINE_SRCU(name) __DEFINE_SRCU(name, /* not static */)
  140. #define DEFINE_STATIC_SRCU(name) __DEFINE_SRCU(name, static)
  141. void synchronize_srcu_expedited(struct srcu_struct *ssp);
  142. void srcu_barrier(struct srcu_struct *ssp);
  143. void srcu_torture_stats_print(struct srcu_struct *ssp, char *tt, char *tf);
  144. #endif