gfs2-glocks.rst 11 KB

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