srcuclassic.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Sleepable Read-Copy Update mechanism for mutual exclusion,
  3. * classic v4.11 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_CLASSIC_H
  24. #define _LINUX_SRCU_CLASSIC_H
  25. struct srcu_array {
  26. unsigned long lock_count[2];
  27. unsigned long unlock_count[2];
  28. };
  29. struct rcu_batch {
  30. struct rcu_head *head, **tail;
  31. };
  32. #define RCU_BATCH_INIT(name) { NULL, &(name.head) }
  33. struct srcu_struct {
  34. unsigned long completed;
  35. struct srcu_array __percpu *per_cpu_ref;
  36. spinlock_t queue_lock; /* protect ->batch_queue, ->running */
  37. bool running;
  38. /* callbacks just queued */
  39. struct rcu_batch batch_queue;
  40. /* callbacks try to do the first check_zero */
  41. struct rcu_batch batch_check0;
  42. /* callbacks done with the first check_zero and the flip */
  43. struct rcu_batch batch_check1;
  44. struct rcu_batch batch_done;
  45. struct delayed_work work;
  46. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  47. struct lockdep_map dep_map;
  48. #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  49. };
  50. void process_srcu(struct work_struct *work);
  51. #define __SRCU_STRUCT_INIT(name) \
  52. { \
  53. .completed = -300, \
  54. .per_cpu_ref = &name##_srcu_array, \
  55. .queue_lock = __SPIN_LOCK_UNLOCKED(name.queue_lock), \
  56. .running = false, \
  57. .batch_queue = RCU_BATCH_INIT(name.batch_queue), \
  58. .batch_check0 = RCU_BATCH_INIT(name.batch_check0), \
  59. .batch_check1 = RCU_BATCH_INIT(name.batch_check1), \
  60. .batch_done = RCU_BATCH_INIT(name.batch_done), \
  61. .work = __DELAYED_WORK_INITIALIZER(name.work, process_srcu, 0),\
  62. __SRCU_DEP_MAP_INIT(name) \
  63. }
  64. /*
  65. * Define and initialize a srcu struct at build time.
  66. * Do -not- call init_srcu_struct() nor cleanup_srcu_struct() on it.
  67. *
  68. * Note that although DEFINE_STATIC_SRCU() hides the name from other
  69. * files, the per-CPU variable rules nevertheless require that the
  70. * chosen name be globally unique. These rules also prohibit use of
  71. * DEFINE_STATIC_SRCU() within a function. If these rules are too
  72. * restrictive, declare the srcu_struct manually. For example, in
  73. * each file:
  74. *
  75. * static struct srcu_struct my_srcu;
  76. *
  77. * Then, before the first use of each my_srcu, manually initialize it:
  78. *
  79. * init_srcu_struct(&my_srcu);
  80. *
  81. * See include/linux/percpu-defs.h for the rules on per-CPU variables.
  82. */
  83. #define __DEFINE_SRCU(name, is_static) \
  84. static DEFINE_PER_CPU(struct srcu_array, name##_srcu_array);\
  85. is_static struct srcu_struct name = __SRCU_STRUCT_INIT(name)
  86. #define DEFINE_SRCU(name) __DEFINE_SRCU(name, /* not static */)
  87. #define DEFINE_STATIC_SRCU(name) __DEFINE_SRCU(name, static)
  88. void synchronize_srcu_expedited(struct srcu_struct *sp);
  89. void srcu_barrier(struct srcu_struct *sp);
  90. unsigned long srcu_batches_completed(struct srcu_struct *sp);
  91. static inline void srcutorture_get_gp_data(enum rcutorture_type test_type,
  92. struct srcu_struct *sp, int *flags,
  93. unsigned long *gpnum,
  94. unsigned long *completed)
  95. {
  96. if (test_type != SRCU_FLAVOR)
  97. return;
  98. *flags = 0;
  99. *completed = sp->completed;
  100. *gpnum = *completed;
  101. if (sp->batch_queue.head || sp->batch_check0.head || sp->batch_check0.head)
  102. (*gpnum)++;
  103. }
  104. #endif