0001 .. SPDX-License-Identifier: GPL-2.0
0002
0003 ============================
0004 Glock internal locking rules
0005 ============================
0006
0007 This documents the basic principles of the glock state machine
0008 internals. Each glock (struct gfs2_glock in fs/gfs2/incore.h)
0009 has two main (internal) locks:
0010
0011 1. A spinlock (gl_lockref.lock) which protects the internal state such
0012 as gl_state, gl_target and the list of holders (gl_holders)
0013 2. A non-blocking bit lock, GLF_LOCK, which is used to prevent other
0014 threads from making calls to the DLM, etc. at the same time. If a
0015 thread takes this lock, it must then call run_queue (usually via the
0016 workqueue) when it releases it in order to ensure any pending tasks
0017 are completed.
0018
0019 The gl_holders list contains all the queued lock requests (not
0020 just the holders) associated with the glock. If there are any
0021 held locks, then they will be contiguous entries at the head
0022 of the list. Locks are granted in strictly the order that they
0023 are queued, except for those marked LM_FLAG_PRIORITY which are
0024 used only during recovery, and even then only for journal locks.
0025
0026 There are three lock states that users of the glock layer can request,
0027 namely shared (SH), deferred (DF) and exclusive (EX). Those translate
0028 to the following DLM lock modes:
0029
0030 ========== ====== =====================================================
0031 Glock mode DLM lock mode
0032 ========== ====== =====================================================
0033 UN IV/NL Unlocked (no DLM lock associated with glock) or NL
0034 SH PR (Protected read)
0035 DF CW (Concurrent write)
0036 EX EX (Exclusive)
0037 ========== ====== =====================================================
0038
0039 Thus DF is basically a shared mode which is incompatible with the "normal"
0040 shared lock mode, SH. In GFS2 the DF mode is used exclusively for direct I/O
0041 operations. The glocks are basically a lock plus some routines which deal
0042 with cache management. The following rules apply for the cache:
0043
0044 ========== ========== ============== ========== ==============
0045 Glock mode Cache data Cache Metadata Dirty Data Dirty Metadata
0046 ========== ========== ============== ========== ==============
0047 UN No No No No
0048 SH Yes Yes No No
0049 DF No Yes No No
0050 EX Yes Yes Yes Yes
0051 ========== ========== ============== ========== ==============
0052
0053 These rules are implemented using the various glock operations which
0054 are defined for each type of glock. Not all types of glocks use
0055 all the modes. Only inode glocks use the DF mode for example.
0056
0057 Table of glock operations and per type constants:
0058
0059 ============= =============================================================
0060 Field Purpose
0061 ============= =============================================================
0062 go_xmote_th Called before remote state change (e.g. to sync dirty data)
0063 go_xmote_bh Called after remote state change (e.g. to refill cache)
0064 go_inval Called if remote state change requires invalidating the cache
0065 go_demote_ok Returns boolean value of whether its ok to demote a glock
0066 (e.g. checks timeout, and that there is no cached data)
0067 go_lock Called for the first local holder of a lock
0068 go_unlock Called on the final local unlock of a lock
0069 go_dump Called to print content of object for debugfs file, or on
0070 error to dump glock to the log.
0071 go_type The type of the glock, ``LM_TYPE_*``
0072 go_callback Called if the DLM sends a callback to drop this lock
0073 go_flags GLOF_ASPACE is set, if the glock has an address space
0074 associated with it
0075 ============= =============================================================
0076
0077 The minimum hold time for each lock is the time after a remote lock
0078 grant for which we ignore remote demote requests. This is in order to
0079 prevent a situation where locks are being bounced around the cluster
0080 from node to node with none of the nodes making any progress. This
0081 tends to show up most with shared mmaped files which are being written
0082 to by multiple nodes. By delaying the demotion in response to a
0083 remote callback, that gives the userspace program time to make
0084 some progress before the pages are unmapped.
0085
0086 There is a plan to try and remove the go_lock and go_unlock callbacks
0087 if possible, in order to try and speed up the fast path though the locking.
0088 Also, eventually we hope to make the glock "EX" mode locally shared
0089 such that any local locking will be done with the i_mutex as required
0090 rather than via the glock.
0091
0092 Locking rules for glock operations:
0093
0094 ============= ====================== =============================
0095 Operation GLF_LOCK bit lock held gl_lockref.lock spinlock held
0096 ============= ====================== =============================
0097 go_xmote_th Yes No
0098 go_xmote_bh Yes No
0099 go_inval Yes No
0100 go_demote_ok Sometimes Yes
0101 go_lock Yes No
0102 go_unlock Yes No
0103 go_dump Sometimes Yes
0104 go_callback Sometimes (N/A) Yes
0105 ============= ====================== =============================
0106
0107 .. Note::
0108
0109 Operations must not drop either the bit lock or the spinlock
0110 if its held on entry. go_dump and do_demote_ok must never block.
0111 Note that go_dump will only be called if the glock's state
0112 indicates that it is caching uptodate data.
0113
0114 Glock locking order within GFS2:
0115
0116 1. i_rwsem (if required)
0117 2. Rename glock (for rename only)
0118 3. Inode glock(s)
0119 (Parents before children, inodes at "same level" with same parent in
0120 lock number order)
0121 4. Rgrp glock(s) (for (de)allocation operations)
0122 5. Transaction glock (via gfs2_trans_begin) for non-read operations
0123 6. i_rw_mutex (if required)
0124 7. Page lock (always last, very important!)
0125
0126 There are two glocks per inode. One deals with access to the inode
0127 itself (locking order as above), and the other, known as the iopen
0128 glock is used in conjunction with the i_nlink field in the inode to
0129 determine the lifetime of the inode in question. Locking of inodes
0130 is on a per-inode basis. Locking of rgrps is on a per rgrp basis.
0131 In general we prefer to lock local locks prior to cluster locks.
0132
0133 Glock Statistics
0134 ----------------
0135
0136 The stats are divided into two sets: those relating to the
0137 super block and those relating to an individual glock. The
0138 super block stats are done on a per cpu basis in order to
0139 try and reduce the overhead of gathering them. They are also
0140 further divided by glock type. All timings are in nanoseconds.
0141
0142 In the case of both the super block and glock statistics,
0143 the same information is gathered in each case. The super
0144 block timing statistics are used to provide default values for
0145 the glock timing statistics, so that newly created glocks
0146 should have, as far as possible, a sensible starting point.
0147 The per-glock counters are initialised to zero when the
0148 glock is created. The per-glock statistics are lost when
0149 the glock is ejected from memory.
0150
0151 The statistics are divided into three pairs of mean and
0152 variance, plus two counters. The mean/variance pairs are
0153 smoothed exponential estimates and the algorithm used is
0154 one which will be very familiar to those used to calculation
0155 of round trip times in network code. See "TCP/IP Illustrated,
0156 Volume 1", W. Richard Stevens, sect 21.3, "Round-Trip Time Measurement",
0157 p. 299 and onwards. Also, Volume 2, Sect. 25.10, p. 838 and onwards.
0158 Unlike the TCP/IP Illustrated case, the mean and variance are
0159 not scaled, but are in units of integer nanoseconds.
0160
0161 The three pairs of mean/variance measure the following
0162 things:
0163
0164 1. DLM lock time (non-blocking requests)
0165 2. DLM lock time (blocking requests)
0166 3. Inter-request time (again to the DLM)
0167
0168 A non-blocking request is one which will complete right
0169 away, whatever the state of the DLM lock in question. That
0170 currently means any requests when (a) the current state of
0171 the lock is exclusive, i.e. a lock demotion (b) the requested
0172 state is either null or unlocked (again, a demotion) or (c) the
0173 "try lock" flag is set. A blocking request covers all the other
0174 lock requests.
0175
0176 There are two counters. The first is there primarily to show
0177 how many lock requests have been made, and thus how much data
0178 has gone into the mean/variance calculations. The other counter
0179 is counting queuing of holders at the top layer of the glock
0180 code. Hopefully that number will be a lot larger than the number
0181 of dlm lock requests issued.
0182
0183 So why gather these statistics? There are several reasons
0184 we'd like to get a better idea of these timings:
0185
0186 1. To be able to better set the glock "min hold time"
0187 2. To spot performance issues more easily
0188 3. To improve the algorithm for selecting resource groups for
0189 allocation (to base it on lock wait time, rather than blindly
0190 using a "try lock")
0191
0192 Due to the smoothing action of the updates, a step change in
0193 some input quantity being sampled will only fully be taken
0194 into account after 8 samples (or 4 for the variance) and this
0195 needs to be carefully considered when interpreting the
0196 results.
0197
0198 Knowing both the time it takes a lock request to complete and
0199 the average time between lock requests for a glock means we
0200 can compute the total percentage of the time for which the
0201 node is able to use a glock vs. time that the rest of the
0202 cluster has its share. That will be very useful when setting
0203 the lock min hold time.
0204
0205 Great care has been taken to ensure that we
0206 measure exactly the quantities that we want, as accurately
0207 as possible. There are always inaccuracies in any
0208 measuring system, but I hope this is as accurate as we
0209 can reasonably make it.
0210
0211 Per sb stats can be found here::
0212
0213 /sys/kernel/debug/gfs2/<fsname>/sbstats
0214
0215 Per glock stats can be found here::
0216
0217 /sys/kernel/debug/gfs2/<fsname>/glstats
0218
0219 Assuming that debugfs is mounted on /sys/kernel/debug and also
0220 that <fsname> is replaced with the name of the gfs2 filesystem
0221 in question.
0222
0223 The abbreviations used in the output as are follows:
0224
0225 ========= ================================================================
0226 srtt Smoothed round trip time for non blocking dlm requests
0227 srttvar Variance estimate for srtt
0228 srttb Smoothed round trip time for (potentially) blocking dlm requests
0229 srttvarb Variance estimate for srttb
0230 sirt Smoothed inter request time (for dlm requests)
0231 sirtvar Variance estimate for sirt
0232 dlm Number of dlm requests made (dcnt in glstats file)
0233 queue Number of glock requests queued (qcnt in glstats file)
0234 ========= ================================================================
0235
0236 The sbstats file contains a set of these stats for each glock type (so 8 lines
0237 for each type) and for each cpu (one column per cpu). The glstats file contains
0238 a set of these stats for each glock in a similar format to the glocks file, but
0239 using the format mean/variance for each of the timing stats.
0240
0241 The gfs2_glock_lock_time tracepoint prints out the current values of the stats
0242 for the glock in question, along with some addition information on each dlm
0243 reply that is received:
0244
0245 ====== =======================================
0246 status The status of the dlm request
0247 flags The dlm request flags
0248 tdiff The time taken by this specific request
0249 ====== =======================================
0250
0251 (remaining fields as per above list)
0252
0253