gfs2-glocks.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ============================
  3. Glock internal locking rules
  4. ============================
  5. This documents the basic principles of the glock state machine
  6. internals. Each glock (struct gfs2_glock in fs/gfs2/incore.h)
  7. has two main (internal) locks:
  8. 1. A spinlock (gl_lockref.lock) which protects the internal state such
  9. as gl_state, gl_target and the list of holders (gl_holders)
  10. 2. A non-blocking bit lock, GLF_LOCK, which is used to prevent other
  11. threads from making calls to the DLM, etc. at the same time. If a
  12. thread takes this lock, it must then call run_queue (usually via the
  13. workqueue) when it releases it in order to ensure any pending tasks
  14. are completed.
  15. The gl_holders list contains all the queued lock requests (not
  16. just the holders) associated with the glock. If there are any
  17. held locks, then they will be contiguous entries at the head
  18. of the list. Locks are granted in strictly the order that they
  19. are queued, except for those marked LM_FLAG_PRIORITY which are
  20. used only during recovery, and even then only for journal locks.
  21. There are three lock states that users of the glock layer can request,
  22. namely shared (SH), deferred (DF) and exclusive (EX). Those translate
  23. to the following DLM lock modes:
  24. ========== ====== =====================================================
  25. Glock mode DLM lock mode
  26. ========== ====== =====================================================
  27. UN IV/NL Unlocked (no DLM lock associated with glock) or NL
  28. SH PR (Protected read)
  29. DF CW (Concurrent write)
  30. EX EX (Exclusive)
  31. ========== ====== =====================================================
  32. Thus DF is basically a shared mode which is incompatible with the "normal"
  33. shared lock mode, SH. In GFS2 the DF mode is used exclusively for direct I/O
  34. operations. The glocks are basically a lock plus some routines which deal
  35. with cache management. The following rules apply for the cache:
  36. ========== ========== ============== ========== ==============
  37. Glock mode Cache data Cache Metadata Dirty Data Dirty Metadata
  38. ========== ========== ============== ========== ==============
  39. UN No No No No
  40. SH Yes Yes No No
  41. DF No Yes No No
  42. EX Yes Yes Yes Yes
  43. ========== ========== ============== ========== ==============
  44. These rules are implemented using the various glock operations which
  45. are defined for each type of glock. Not all types of glocks use
  46. all the modes. Only inode glocks use the DF mode for example.
  47. Table of glock operations and per type constants:
  48. ============= =============================================================
  49. Field Purpose
  50. ============= =============================================================
  51. go_xmote_th Called before remote state change (e.g. to sync dirty data)
  52. go_xmote_bh Called after remote state change (e.g. to refill cache)
  53. go_inval Called if remote state change requires invalidating the cache
  54. go_demote_ok Returns boolean value of whether its ok to demote a glock
  55. (e.g. checks timeout, and that there is no cached data)
  56. go_lock Called for the first local holder of a lock
  57. go_unlock Called on the final local unlock of a lock
  58. go_dump Called to print content of object for debugfs file, or on
  59. error to dump glock to the log.
  60. go_type The type of the glock, ``LM_TYPE_*``
  61. go_callback Called if the DLM sends a callback to drop this lock
  62. go_flags GLOF_ASPACE is set, if the glock has an address space
  63. associated with it
  64. ============= =============================================================
  65. The minimum hold time for each lock is the time after a remote lock
  66. grant for which we ignore remote demote requests. This is in order to
  67. prevent a situation where locks are being bounced around the cluster
  68. from node to node with none of the nodes making any progress. This
  69. tends to show up most with shared mmaped files which are being written
  70. to by multiple nodes. By delaying the demotion in response to a
  71. remote callback, that gives the userspace program time to make
  72. some progress before the pages are unmapped.
  73. There is a plan to try and remove the go_lock and go_unlock callbacks
  74. if possible, in order to try and speed up the fast path though the locking.
  75. Also, eventually we hope to make the glock "EX" mode locally shared
  76. such that any local locking will be done with the i_mutex as required
  77. rather than via the glock.
  78. Locking rules for glock operations:
  79. ============= ====================== =============================
  80. Operation GLF_LOCK bit lock held gl_lockref.lock spinlock held
  81. ============= ====================== =============================
  82. go_xmote_th Yes No
  83. go_xmote_bh Yes No
  84. go_inval Yes No
  85. go_demote_ok Sometimes Yes
  86. go_lock Yes No
  87. go_unlock Yes No
  88. go_dump Sometimes Yes
  89. go_callback Sometimes (N/A) Yes
  90. ============= ====================== =============================
  91. .. Note::
  92. Operations must not drop either the bit lock or the spinlock
  93. if its held on entry. go_dump and do_demote_ok must never block.
  94. Note that go_dump will only be called if the glock's state
  95. indicates that it is caching uptodate data.
  96. Glock locking order within GFS2:
  97. 1. i_rwsem (if required)
  98. 2. Rename glock (for rename only)
  99. 3. Inode glock(s)
  100. (Parents before children, inodes at "same level" with same parent in
  101. lock number order)
  102. 4. Rgrp glock(s) (for (de)allocation operations)
  103. 5. Transaction glock (via gfs2_trans_begin) for non-read operations
  104. 6. i_rw_mutex (if required)
  105. 7. Page lock (always last, very important!)
  106. There are two glocks per inode. One deals with access to the inode
  107. itself (locking order as above), and the other, known as the iopen
  108. glock is used in conjunction with the i_nlink field in the inode to
  109. determine the lifetime of the inode in question. Locking of inodes
  110. is on a per-inode basis. Locking of rgrps is on a per rgrp basis.
  111. In general we prefer to lock local locks prior to cluster locks.
  112. Glock Statistics
  113. ----------------
  114. The stats are divided into two sets: those relating to the
  115. super block and those relating to an individual glock. The
  116. super block stats are done on a per cpu basis in order to
  117. try and reduce the overhead of gathering them. They are also
  118. further divided by glock type. All timings are in nanoseconds.
  119. In the case of both the super block and glock statistics,
  120. the same information is gathered in each case. The super
  121. block timing statistics are used to provide default values for
  122. the glock timing statistics, so that newly created glocks
  123. should have, as far as possible, a sensible starting point.
  124. The per-glock counters are initialised to zero when the
  125. glock is created. The per-glock statistics are lost when
  126. the glock is ejected from memory.
  127. The statistics are divided into three pairs of mean and
  128. variance, plus two counters. The mean/variance pairs are
  129. smoothed exponential estimates and the algorithm used is
  130. one which will be very familiar to those used to calculation
  131. of round trip times in network code. See "TCP/IP Illustrated,
  132. Volume 1", W. Richard Stevens, sect 21.3, "Round-Trip Time Measurement",
  133. p. 299 and onwards. Also, Volume 2, Sect. 25.10, p. 838 and onwards.
  134. Unlike the TCP/IP Illustrated case, the mean and variance are
  135. not scaled, but are in units of integer nanoseconds.
  136. The three pairs of mean/variance measure the following
  137. things:
  138. 1. DLM lock time (non-blocking requests)
  139. 2. DLM lock time (blocking requests)
  140. 3. Inter-request time (again to the DLM)
  141. A non-blocking request is one which will complete right
  142. away, whatever the state of the DLM lock in question. That
  143. currently means any requests when (a) the current state of
  144. the lock is exclusive, i.e. a lock demotion (b) the requested
  145. state is either null or unlocked (again, a demotion) or (c) the
  146. "try lock" flag is set. A blocking request covers all the other
  147. lock requests.
  148. There are two counters. The first is there primarily to show
  149. how many lock requests have been made, and thus how much data
  150. has gone into the mean/variance calculations. The other counter
  151. is counting queuing of holders at the top layer of the glock
  152. code. Hopefully that number will be a lot larger than the number
  153. of dlm lock requests issued.
  154. So why gather these statistics? There are several reasons
  155. we'd like to get a better idea of these timings:
  156. 1. To be able to better set the glock "min hold time"
  157. 2. To spot performance issues more easily
  158. 3. To improve the algorithm for selecting resource groups for
  159. allocation (to base it on lock wait time, rather than blindly
  160. using a "try lock")
  161. Due to the smoothing action of the updates, a step change in
  162. some input quantity being sampled will only fully be taken
  163. into account after 8 samples (or 4 for the variance) and this
  164. needs to be carefully considered when interpreting the
  165. results.
  166. Knowing both the time it takes a lock request to complete and
  167. the average time between lock requests for a glock means we
  168. can compute the total percentage of the time for which the
  169. node is able to use a glock vs. time that the rest of the
  170. cluster has its share. That will be very useful when setting
  171. the lock min hold time.
  172. Great care has been taken to ensure that we
  173. measure exactly the quantities that we want, as accurately
  174. as possible. There are always inaccuracies in any
  175. measuring system, but I hope this is as accurate as we
  176. can reasonably make it.
  177. Per sb stats can be found here::
  178. /sys/kernel/debug/gfs2/<fsname>/sbstats
  179. Per glock stats can be found here::
  180. /sys/kernel/debug/gfs2/<fsname>/glstats
  181. Assuming that debugfs is mounted on /sys/kernel/debug and also
  182. that <fsname> is replaced with the name of the gfs2 filesystem
  183. in question.
  184. The abbreviations used in the output as are follows:
  185. ========= ================================================================
  186. srtt Smoothed round trip time for non blocking dlm requests
  187. srttvar Variance estimate for srtt
  188. srttb Smoothed round trip time for (potentially) blocking dlm requests
  189. srttvarb Variance estimate for srttb
  190. sirt Smoothed inter request time (for dlm requests)
  191. sirtvar Variance estimate for sirt
  192. dlm Number of dlm requests made (dcnt in glstats file)
  193. queue Number of glock requests queued (qcnt in glstats file)
  194. ========= ================================================================
  195. The sbstats file contains a set of these stats for each glock type (so 8 lines
  196. for each type) and for each cpu (one column per cpu). The glstats file contains
  197. a set of these stats for each glock in a similar format to the glocks file, but
  198. using the format mean/variance for each of the timing stats.
  199. The gfs2_glock_lock_time tracepoint prints out the current values of the stats
  200. for the glock in question, along with some addition information on each dlm
  201. reply that is received:
  202. ====== =======================================
  203. status The status of the dlm request
  204. flags The dlm request flags
  205. tdiff The time taken by this specific request
  206. ====== =======================================
  207. (remaining fields as per above list)