srcutiny.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Sleepable Read-Copy Update mechanism for mutual exclusion,
  3. * tiny 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_TINY_H
  24. #define _LINUX_SRCU_TINY_H
  25. #include <linux/swait.h>
  26. struct srcu_struct {
  27. short srcu_lock_nesting[2]; /* srcu_read_lock() nesting depth. */
  28. short srcu_idx; /* Current reader array element. */
  29. u8 srcu_gp_running; /* GP workqueue running? */
  30. u8 srcu_gp_waiting; /* GP waiting for readers? */
  31. struct swait_queue_head srcu_wq;
  32. /* Last srcu_read_unlock() wakes GP. */
  33. struct rcu_head *srcu_cb_head; /* Pending callbacks: Head. */
  34. struct rcu_head **srcu_cb_tail; /* Pending callbacks: Tail. */
  35. struct work_struct srcu_work; /* For driving grace periods. */
  36. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  37. struct lockdep_map dep_map;
  38. #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  39. };
  40. void srcu_drive_gp(struct work_struct *wp);
  41. #define __SRCU_STRUCT_INIT(name, __ignored) \
  42. { \
  43. .srcu_wq = __SWAIT_QUEUE_HEAD_INITIALIZER(name.srcu_wq), \
  44. .srcu_cb_tail = &name.srcu_cb_head, \
  45. .srcu_work = __WORK_INITIALIZER(name.srcu_work, srcu_drive_gp), \
  46. __SRCU_DEP_MAP_INIT(name) \
  47. }
  48. /*
  49. * This odd _STATIC_ arrangement is needed for API compatibility with
  50. * Tree SRCU, which needs some per-CPU data.
  51. */
  52. #define DEFINE_SRCU(name) \
  53. struct srcu_struct name = __SRCU_STRUCT_INIT(name, name)
  54. #define DEFINE_STATIC_SRCU(name) \
  55. static struct srcu_struct name = __SRCU_STRUCT_INIT(name, name)
  56. void synchronize_srcu(struct srcu_struct *sp);
  57. /*
  58. * Counts the new reader in the appropriate per-CPU element of the
  59. * srcu_struct. Can be invoked from irq/bh handlers, but the matching
  60. * __srcu_read_unlock() must be in the same handler instance. Returns an
  61. * index that must be passed to the matching srcu_read_unlock().
  62. */
  63. static inline int __srcu_read_lock(struct srcu_struct *sp)
  64. {
  65. int idx;
  66. idx = READ_ONCE(sp->srcu_idx);
  67. WRITE_ONCE(sp->srcu_lock_nesting[idx], sp->srcu_lock_nesting[idx] + 1);
  68. return idx;
  69. }
  70. static inline void synchronize_srcu_expedited(struct srcu_struct *sp)
  71. {
  72. synchronize_srcu(sp);
  73. }
  74. static inline void srcu_barrier(struct srcu_struct *sp)
  75. {
  76. synchronize_srcu(sp);
  77. }
  78. /* Defined here to avoid size increase for non-torture kernels. */
  79. static inline void srcu_torture_stats_print(struct srcu_struct *sp,
  80. char *tt, char *tf)
  81. {
  82. int idx;
  83. idx = READ_ONCE(sp->srcu_idx) & 0x1;
  84. pr_alert("%s%s Tiny SRCU per-CPU(idx=%d): (%hd,%hd)\n",
  85. tt, tf, idx,
  86. READ_ONCE(sp->srcu_lock_nesting[!idx]),
  87. READ_ONCE(sp->srcu_lock_nesting[idx]));
  88. }
  89. #endif