srcutree.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Sleepable Read-Copy Update mechanism for mutual exclusion,
  3. * tree variant.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, you can access it online at
  17. * http://www.gnu.org/licenses/gpl-2.0.html.
  18. *
  19. * Copyright (C) IBM Corporation, 2017
  20. *
  21. * Author: Paul McKenney <paulmck@us.ibm.com>
  22. */
  23. #ifndef _LINUX_SRCU_TREE_H
  24. #define _LINUX_SRCU_TREE_H
  25. #include <linux/rcu_node_tree.h>
  26. #include <linux/completion.h>
  27. struct srcu_node;
  28. struct srcu_struct;
  29. /*
  30. * Per-CPU structure feeding into leaf srcu_node, similar in function
  31. * to rcu_node.
  32. */
  33. struct srcu_data {
  34. /* Read-side state. */
  35. unsigned long srcu_lock_count[2]; /* Locks per CPU. */
  36. unsigned long srcu_unlock_count[2]; /* Unlocks per CPU. */
  37. /* Update-side state. */
  38. raw_spinlock_t __private lock ____cacheline_internodealigned_in_smp;
  39. struct rcu_segcblist srcu_cblist; /* List of callbacks.*/
  40. unsigned long srcu_gp_seq_needed; /* Furthest future GP needed. */
  41. unsigned long srcu_gp_seq_needed_exp; /* Furthest future exp GP. */
  42. bool srcu_cblist_invoking; /* Invoking these CBs? */
  43. struct delayed_work work; /* Context for CB invoking. */
  44. struct rcu_head srcu_barrier_head; /* For srcu_barrier() use. */
  45. struct srcu_node *mynode; /* Leaf srcu_node. */
  46. unsigned long grpmask; /* Mask for leaf srcu_node */
  47. /* ->srcu_data_have_cbs[]. */
  48. int cpu;
  49. struct srcu_struct *sp;
  50. };
  51. /*
  52. * Node in SRCU combining tree, similar in function to rcu_data.
  53. */
  54. struct srcu_node {
  55. raw_spinlock_t __private lock;
  56. unsigned long srcu_have_cbs[4]; /* GP seq for children */
  57. /* having CBs, but only */
  58. /* is > ->srcu_gq_seq. */
  59. unsigned long srcu_data_have_cbs[4]; /* Which srcu_data structs */
  60. /* have CBs for given GP? */
  61. unsigned long srcu_gp_seq_needed_exp; /* Furthest future exp GP. */
  62. struct srcu_node *srcu_parent; /* Next up in tree. */
  63. int grplo; /* Least CPU for node. */
  64. int grphi; /* Biggest CPU for node. */
  65. };
  66. /*
  67. * Per-SRCU-domain structure, similar in function to rcu_state.
  68. */
  69. struct srcu_struct {
  70. struct srcu_node node[NUM_RCU_NODES]; /* Combining tree. */
  71. struct srcu_node *level[RCU_NUM_LVLS + 1];
  72. /* First node at each level. */
  73. struct mutex srcu_cb_mutex; /* Serialize CB preparation. */
  74. raw_spinlock_t __private lock; /* Protect counters */
  75. struct mutex srcu_gp_mutex; /* Serialize GP work. */
  76. unsigned int srcu_idx; /* Current rdr array element. */
  77. unsigned long srcu_gp_seq; /* Grace-period seq #. */
  78. unsigned long srcu_gp_seq_needed; /* Latest gp_seq needed. */
  79. unsigned long srcu_gp_seq_needed_exp; /* Furthest future exp GP. */
  80. unsigned long srcu_last_gp_end; /* Last GP end timestamp (ns) */
  81. struct srcu_data __percpu *sda; /* Per-CPU srcu_data array. */
  82. unsigned long srcu_barrier_seq; /* srcu_barrier seq #. */
  83. struct mutex srcu_barrier_mutex; /* Serialize barrier ops. */
  84. struct completion srcu_barrier_completion;
  85. /* Awaken barrier rq at end. */
  86. atomic_t srcu_barrier_cpu_cnt; /* # CPUs not yet posting a */
  87. /* callback for the barrier */
  88. /* operation. */
  89. struct delayed_work work;
  90. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  91. struct lockdep_map dep_map;
  92. #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  93. };
  94. /* Values for state variable (bottom bits of ->srcu_gp_seq). */
  95. #define SRCU_STATE_IDLE 0
  96. #define SRCU_STATE_SCAN1 1
  97. #define SRCU_STATE_SCAN2 2
  98. void process_srcu(struct work_struct *work);
  99. #define __SRCU_STRUCT_INIT(name) \
  100. { \
  101. .sda = &name##_srcu_data, \
  102. .lock = __RAW_SPIN_LOCK_UNLOCKED(name.lock), \
  103. .srcu_gp_seq_needed = 0 - 1, \
  104. __SRCU_DEP_MAP_INIT(name) \
  105. }
  106. /*
  107. * Define and initialize a srcu struct at build time.
  108. * Do -not- call init_srcu_struct() nor cleanup_srcu_struct() on it.
  109. *
  110. * Note that although DEFINE_STATIC_SRCU() hides the name from other
  111. * files, the per-CPU variable rules nevertheless require that the
  112. * chosen name be globally unique. These rules also prohibit use of
  113. * DEFINE_STATIC_SRCU() within a function. If these rules are too
  114. * restrictive, declare the srcu_struct manually. For example, in
  115. * each file:
  116. *
  117. * static struct srcu_struct my_srcu;
  118. *
  119. * Then, before the first use of each my_srcu, manually initialize it:
  120. *
  121. * init_srcu_struct(&my_srcu);
  122. *
  123. * See include/linux/percpu-defs.h for the rules on per-CPU variables.
  124. */
  125. #define __DEFINE_SRCU(name, is_static) \
  126. static DEFINE_PER_CPU(struct srcu_data, name##_srcu_data);\
  127. is_static struct srcu_struct name = __SRCU_STRUCT_INIT(name)
  128. #define DEFINE_SRCU(name) __DEFINE_SRCU(name, /* not static */)
  129. #define DEFINE_STATIC_SRCU(name) __DEFINE_SRCU(name, static)
  130. void synchronize_srcu_expedited(struct srcu_struct *sp);
  131. void srcu_barrier(struct srcu_struct *sp);
  132. #endif