250 lines · plain
1.. SPDX-License-Identifier: GPL-2.02 3============================4Glock internal locking rules5============================6 7This documents the basic principles of the glock state machine8internals. Each glock (struct gfs2_glock in fs/gfs2/incore.h)9has two main (internal) locks:10 11 1. A spinlock (gl_lockref.lock) which protects the internal state such12 as gl_state, gl_target and the list of holders (gl_holders)13 2. A non-blocking bit lock, GLF_LOCK, which is used to prevent other14 threads from making calls to the DLM, etc. at the same time. If a15 thread takes this lock, it must then call run_queue (usually via the16 workqueue) when it releases it in order to ensure any pending tasks17 are completed.18 19The gl_holders list contains all the queued lock requests (not20just the holders) associated with the glock. If there are any21held locks, then they will be contiguous entries at the head22of the list. Locks are granted in strictly the order that they23are queued.24 25There are three lock states that users of the glock layer can request,26namely shared (SH), deferred (DF) and exclusive (EX). Those translate27to the following DLM lock modes:28 29========== ====== =====================================================30Glock mode DLM lock mode31========== ====== =====================================================32 UN IV/NL Unlocked (no DLM lock associated with glock) or NL33 SH PR (Protected read)34 DF CW (Concurrent write)35 EX EX (Exclusive)36========== ====== =====================================================37 38Thus DF is basically a shared mode which is incompatible with the "normal"39shared lock mode, SH. In GFS2 the DF mode is used exclusively for direct I/O40operations. The glocks are basically a lock plus some routines which deal41with cache management. The following rules apply for the cache:42 43========== ============== ========== ========== ==============44Glock mode Cache Metadata Cache data Dirty Data Dirty Metadata45========== ============== ========== ========== ==============46 UN No No No No47 DF Yes No No No48 SH Yes Yes No No49 EX Yes Yes Yes Yes50========== ============== ========== ========== ==============51 52These rules are implemented using the various glock operations which53are defined for each type of glock. Not all types of glocks use54all the modes. Only inode glocks use the DF mode for example.55 56Table of glock operations and per type constants:57 58============== =============================================================59Field Purpose60============== =============================================================61go_sync Called before remote state change (e.g. to sync dirty data)62go_xmote_bh Called after remote state change (e.g. to refill cache)63go_inval Called if remote state change requires invalidating the cache64go_instantiate Called when a glock has been acquired65go_held Called every time a glock holder is acquired66go_dump Called to print content of object for debugfs file, or on67 error to dump glock to the log.68go_callback Called if the DLM sends a callback to drop this lock69go_unlocked Called when a glock is unlocked (dlm_unlock())70go_type The type of the glock, ``LM_TYPE_*``71go_flags GLOF_ASPACE is set, if the glock has an address space72 associated with it73============== =============================================================74 75The minimum hold time for each lock is the time after a remote lock76grant for which we ignore remote demote requests. This is in order to77prevent a situation where locks are being bounced around the cluster78from node to node with none of the nodes making any progress. This79tends to show up most with shared mmapped files which are being written80to by multiple nodes. By delaying the demotion in response to a81remote callback, that gives the userspace program time to make82some progress before the pages are unmapped.83 84Eventually, we hope to make the glock "EX" mode locally shared such that any85local locking will be done with the i_mutex as required rather than via the86glock.87 88Locking rules for glock operations:89 90============== ====================== =============================91Operation GLF_LOCK bit lock held gl_lockref.lock spinlock held92============== ====================== =============================93go_sync Yes No94go_xmote_bh Yes No95go_inval Yes No96go_instantiate No No97go_held No No98go_dump Sometimes Yes99go_callback Sometimes (N/A) Yes100go_unlocked Yes No101============== ====================== =============================102 103.. Note::104 105 Operations must not drop either the bit lock or the spinlock106 if its held on entry. go_dump and do_demote_ok must never block.107 Note that go_dump will only be called if the glock's state108 indicates that it is caching uptodate data.109 110Glock locking order within GFS2:111 112 1. i_rwsem (if required)113 2. Rename glock (for rename only)114 3. Inode glock(s)115 (Parents before children, inodes at "same level" with same parent in116 lock number order)117 4. Rgrp glock(s) (for (de)allocation operations)118 5. Transaction glock (via gfs2_trans_begin) for non-read operations119 6. i_rw_mutex (if required)120 7. Page lock (always last, very important!)121 122There are two glocks per inode. One deals with access to the inode123itself (locking order as above), and the other, known as the iopen124glock is used in conjunction with the i_nlink field in the inode to125determine the lifetime of the inode in question. Locking of inodes126is on a per-inode basis. Locking of rgrps is on a per rgrp basis.127In general we prefer to lock local locks prior to cluster locks.128 129Glock Statistics130----------------131 132The stats are divided into two sets: those relating to the133super block and those relating to an individual glock. The134super block stats are done on a per cpu basis in order to135try and reduce the overhead of gathering them. They are also136further divided by glock type. All timings are in nanoseconds.137 138In the case of both the super block and glock statistics,139the same information is gathered in each case. The super140block timing statistics are used to provide default values for141the glock timing statistics, so that newly created glocks142should have, as far as possible, a sensible starting point.143The per-glock counters are initialised to zero when the144glock is created. The per-glock statistics are lost when145the glock is ejected from memory.146 147The statistics are divided into three pairs of mean and148variance, plus two counters. The mean/variance pairs are149smoothed exponential estimates and the algorithm used is150one which will be very familiar to those used to calculation151of round trip times in network code. See "TCP/IP Illustrated,152Volume 1", W. Richard Stevens, sect 21.3, "Round-Trip Time Measurement",153p. 299 and onwards. Also, Volume 2, Sect. 25.10, p. 838 and onwards.154Unlike the TCP/IP Illustrated case, the mean and variance are155not scaled, but are in units of integer nanoseconds.156 157The three pairs of mean/variance measure the following158things:159 160 1. DLM lock time (non-blocking requests)161 2. DLM lock time (blocking requests)162 3. Inter-request time (again to the DLM)163 164A non-blocking request is one which will complete right165away, whatever the state of the DLM lock in question. That166currently means any requests when (a) the current state of167the lock is exclusive, i.e. a lock demotion (b) the requested168state is either null or unlocked (again, a demotion) or (c) the169"try lock" flag is set. A blocking request covers all the other170lock requests.171 172There are two counters. The first is there primarily to show173how many lock requests have been made, and thus how much data174has gone into the mean/variance calculations. The other counter175is counting queuing of holders at the top layer of the glock176code. Hopefully that number will be a lot larger than the number177of dlm lock requests issued.178 179So why gather these statistics? There are several reasons180we'd like to get a better idea of these timings:181 1821. To be able to better set the glock "min hold time"1832. To spot performance issues more easily1843. To improve the algorithm for selecting resource groups for185 allocation (to base it on lock wait time, rather than blindly186 using a "try lock")187 188Due to the smoothing action of the updates, a step change in189some input quantity being sampled will only fully be taken190into account after 8 samples (or 4 for the variance) and this191needs to be carefully considered when interpreting the192results.193 194Knowing both the time it takes a lock request to complete and195the average time between lock requests for a glock means we196can compute the total percentage of the time for which the197node is able to use a glock vs. time that the rest of the198cluster has its share. That will be very useful when setting199the lock min hold time.200 201Great care has been taken to ensure that we202measure exactly the quantities that we want, as accurately203as possible. There are always inaccuracies in any204measuring system, but I hope this is as accurate as we205can reasonably make it.206 207Per sb stats can be found here::208 209 /sys/kernel/debug/gfs2/<fsname>/sbstats210 211Per glock stats can be found here::212 213 /sys/kernel/debug/gfs2/<fsname>/glstats214 215Assuming that debugfs is mounted on /sys/kernel/debug and also216that <fsname> is replaced with the name of the gfs2 filesystem217in question.218 219The abbreviations used in the output as are follows:220 221========= ================================================================222srtt Smoothed round trip time for non blocking dlm requests223srttvar Variance estimate for srtt224srttb Smoothed round trip time for (potentially) blocking dlm requests225srttvarb Variance estimate for srttb226sirt Smoothed inter request time (for dlm requests)227sirtvar Variance estimate for sirt228dlm Number of dlm requests made (dcnt in glstats file)229queue Number of glock requests queued (qcnt in glstats file)230========= ================================================================231 232The sbstats file contains a set of these stats for each glock type (so 8 lines233for each type) and for each cpu (one column per cpu). The glstats file contains234a set of these stats for each glock in a similar format to the glocks file, but235using the format mean/variance for each of the timing stats.236 237The gfs2_glock_lock_time tracepoint prints out the current values of the stats238for the glock in question, along with some addition information on each dlm239reply that is received:240 241====== =======================================242status The status of the dlm request243flags The dlm request flags244tdiff The time taken by this specific request245====== =======================================246 247(remaining fields as per above list)248 249 250