xfs: reduce AGF hold times during fstrim operations

fstrim will hold the AGF lock for as long as it takes to walk and
discard all the free space in the AG that meets the userspace trim
criteria. For AGs with lots of free space extents (e.g. millions)
or the underlying device is really slow at processing discard
requests (e.g. Ceph RBD), this means the AGF hold time is often
measured in minutes to hours, not a few milliseconds as we normal
see with non-discard based operations.

This can result in the entire filesystem hanging whilst the
long-running fstrim is in progress. We can have transactions get
stuck waiting for the AGF lock (data or metadata extent allocation
and freeing), and then more transactions get stuck waiting on the
locks those transactions hold. We can get to the point where fstrim
blocks an extent allocation or free operation long enough that it
ends up pinning the tail of the log and the log then runs out of
space. At this point, every modification in the filesystem gets
blocked. This includes read operations, if atime updates need to be
made.

To fix this problem, we need to be able to discard free space
extents safely without holding the AGF lock. Fortunately, we already
do this with online discard via busy extents. We can mark free space
extents as "busy being discarded" under the AGF lock and then unlock
the AGF, knowing that nobody will be able to allocate that free
space extent until we remove it from the busy tree.

Modify xfs_trim_extents to use the same asynchronous discard
mechanism backed by busy extents as is used with online discard.
This results in the AGF only needing to be held for short periods of
time and it is never held while we issue discards. Hence if discard
submission gets throttled because it is slow and/or there are lots
of them, we aren't preventing other operations from being performed
on AGF while we wait for discards to complete...

Signed-off-by: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Darrick J. Wong <djwong@kernel.org>

authored by Dave Chinner and committed by Dave Chinner 89cfa899 428c4435

+188 -24
+156 -18
fs/xfs/xfs_discard.c
··· 19 #include "xfs_log.h" 20 #include "xfs_ag.h" 21 22 struct workqueue_struct *xfs_discard_wq; 23 24 static void ··· 144 } 145 146 147 - STATIC int 148 - xfs_trim_extents( 149 struct xfs_perag *pag, 150 xfs_daddr_t start, 151 xfs_daddr_t end, 152 xfs_daddr_t minlen, 153 uint64_t *blocks_trimmed) 154 { 155 struct xfs_mount *mp = pag->pag_mount; 156 - struct block_device *bdev = mp->m_ddev_targp->bt_bdev; 157 struct xfs_btree_cur *cur; 158 struct xfs_buf *agbp; 159 - struct xfs_agf *agf; 160 int error; 161 int i; 162 163 /* 164 * Force out the log. This means any transactions that might have freed ··· 171 error = xfs_alloc_read_agf(pag, NULL, 0, &agbp); 172 if (error) 173 return error; 174 - agf = agbp->b_addr; 175 176 cur = xfs_allocbt_init_cursor(mp, NULL, agbp, pag, XFS_BTNUM_CNT); 177 178 /* 179 - * Look up the longest btree in the AGF and start with it. 180 */ 181 - error = xfs_alloc_lookup_ge(cur, 0, be32_to_cpu(agf->agf_longest), &i); 182 if (error) 183 goto out_del_cursor; 184 185 /* 186 * Loop until we are done with all extents that are large 187 - * enough to be worth discarding. 188 */ 189 while (i) { 190 xfs_agblock_t fbno; ··· 207 error = -EFSCORRUPTED; 208 break; 209 } 210 - ASSERT(flen <= be32_to_cpu(agf->agf_longest)); 211 212 /* 213 * use daddr format for all range/len calculations as that is ··· 231 */ 232 if (dlen < minlen) { 233 trace_xfs_discard_toosmall(mp, pag->pag_agno, fbno, flen); 234 break; 235 } 236 ··· 254 goto next_extent; 255 } 256 257 - trace_xfs_discard_extent(mp, pag->pag_agno, fbno, flen); 258 - error = blkdev_issue_discard(bdev, dbno, dlen, GFP_NOFS); 259 - if (error) 260 - break; 261 *blocks_trimmed += flen; 262 - 263 next_extent: 264 error = xfs_btree_decrement(cur, 0, &i); 265 if (error) 266 break; 267 ··· 340 error = -ERESTARTSYS; 341 break; 342 } 343 - } 344 345 - out_del_cursor: 346 - xfs_btree_del_cursor(cur, error); 347 - xfs_buf_relse(agbp); 348 return error; 349 } 350 351 /*
··· 19 #include "xfs_log.h" 20 #include "xfs_ag.h" 21 22 + /* 23 + * Notes on an efficient, low latency fstrim algorithm 24 + * 25 + * We need to walk the filesystem free space and issue discards on the free 26 + * space that meet the search criteria (size and location). We cannot issue 27 + * discards on extents that might be in use, or are so recently in use they are 28 + * still marked as busy. To serialise against extent state changes whilst we are 29 + * gathering extents to trim, we must hold the AGF lock to lock out other 30 + * allocations and extent free operations that might change extent state. 31 + * 32 + * However, we cannot just hold the AGF for the entire AG free space walk whilst 33 + * we issue discards on each free space that is found. Storage devices can have 34 + * extremely slow discard implementations (e.g. ceph RBD) and so walking a 35 + * couple of million free extents and issuing synchronous discards on each 36 + * extent can take a *long* time. Whilst we are doing this walk, nothing else 37 + * can access the AGF, and we can stall transactions and hence the log whilst 38 + * modifications wait for the AGF lock to be released. This can lead hung tasks 39 + * kicking the hung task timer and rebooting the system. This is bad. 40 + * 41 + * Hence we need to take a leaf from the bulkstat playbook. It takes the AGI 42 + * lock, gathers a range of inode cluster buffers that are allocated, drops the 43 + * AGI lock and then reads all the inode cluster buffers and processes them. It 44 + * loops doing this, using a cursor to keep track of where it is up to in the AG 45 + * for each iteration to restart the INOBT lookup from. 46 + * 47 + * We can't do this exactly with free space - once we drop the AGF lock, the 48 + * state of the free extent is out of our control and we cannot run a discard 49 + * safely on it in this situation. Unless, of course, we've marked the free 50 + * extent as busy and undergoing a discard operation whilst we held the AGF 51 + * locked. 52 + * 53 + * This is exactly how online discard works - free extents are marked busy when 54 + * they are freed, and once the extent free has been committed to the journal, 55 + * the busy extent record is marked as "undergoing discard" and the discard is 56 + * then issued on the free extent. Once the discard completes, the busy extent 57 + * record is removed and the extent is able to be allocated again. 58 + * 59 + * In the context of fstrim, if we find a free extent we need to discard, we 60 + * don't have to discard it immediately. All we need to do it record that free 61 + * extent as being busy and under discard, and all the allocation routines will 62 + * now avoid trying to allocate it. Hence if we mark the extent as busy under 63 + * the AGF lock, we can safely discard it without holding the AGF lock because 64 + * nothing will attempt to allocate that free space until the discard completes. 65 + * 66 + * This also allows us to issue discards asynchronously like we do with online 67 + * discard, and so for fast devices fstrim will run much faster as we can have 68 + * multiple discard operations in flight at once, as well as pipeline the free 69 + * extent search so that it overlaps in flight discard IO. 70 + */ 71 + 72 struct workqueue_struct *xfs_discard_wq; 73 74 static void ··· 94 } 95 96 97 + static int 98 + xfs_trim_gather_extents( 99 struct xfs_perag *pag, 100 xfs_daddr_t start, 101 xfs_daddr_t end, 102 xfs_daddr_t minlen, 103 + struct xfs_alloc_rec_incore *tcur, 104 + struct xfs_busy_extents *extents, 105 uint64_t *blocks_trimmed) 106 { 107 struct xfs_mount *mp = pag->pag_mount; 108 struct xfs_btree_cur *cur; 109 struct xfs_buf *agbp; 110 int error; 111 int i; 112 + int batch = 100; 113 114 /* 115 * Force out the log. This means any transactions that might have freed ··· 120 error = xfs_alloc_read_agf(pag, NULL, 0, &agbp); 121 if (error) 122 return error; 123 124 cur = xfs_allocbt_init_cursor(mp, NULL, agbp, pag, XFS_BTNUM_CNT); 125 126 /* 127 + * Look up the extent length requested in the AGF and start with it. 128 */ 129 + if (tcur->ar_startblock == NULLAGBLOCK) 130 + error = xfs_alloc_lookup_ge(cur, 0, tcur->ar_blockcount, &i); 131 + else 132 + error = xfs_alloc_lookup_le(cur, tcur->ar_startblock, 133 + tcur->ar_blockcount, &i); 134 if (error) 135 goto out_del_cursor; 136 + if (i == 0) { 137 + /* nothing of that length left in the AG, we are done */ 138 + tcur->ar_blockcount = 0; 139 + goto out_del_cursor; 140 + } 141 142 /* 143 * Loop until we are done with all extents that are large 144 + * enough to be worth discarding or we hit batch limits. 145 */ 146 while (i) { 147 xfs_agblock_t fbno; ··· 148 error = -EFSCORRUPTED; 149 break; 150 } 151 + 152 + if (--batch <= 0) { 153 + /* 154 + * Update the cursor to point at this extent so we 155 + * restart the next batch from this extent. 156 + */ 157 + tcur->ar_startblock = fbno; 158 + tcur->ar_blockcount = flen; 159 + break; 160 + } 161 162 /* 163 * use daddr format for all range/len calculations as that is ··· 163 */ 164 if (dlen < minlen) { 165 trace_xfs_discard_toosmall(mp, pag->pag_agno, fbno, flen); 166 + tcur->ar_blockcount = 0; 167 break; 168 } 169 ··· 185 goto next_extent; 186 } 187 188 + xfs_extent_busy_insert_discard(pag, fbno, flen, 189 + &extents->extent_list); 190 *blocks_trimmed += flen; 191 next_extent: 192 error = xfs_btree_decrement(cur, 0, &i); 193 + if (error) 194 + break; 195 + 196 + /* 197 + * If there's no more records in the tree, we are done. Set the 198 + * cursor block count to 0 to indicate to the caller that there 199 + * is no more extents to search. 200 + */ 201 + if (i == 0) 202 + tcur->ar_blockcount = 0; 203 + } 204 + 205 + /* 206 + * If there was an error, release all the gathered busy extents because 207 + * we aren't going to issue a discard on them any more. 208 + */ 209 + if (error) 210 + xfs_extent_busy_clear(mp, &extents->extent_list, false); 211 + out_del_cursor: 212 + xfs_btree_del_cursor(cur, error); 213 + xfs_buf_relse(agbp); 214 + return error; 215 + } 216 + 217 + /* 218 + * Iterate the free list gathering extents and discarding them. We need a cursor 219 + * for the repeated iteration of gather/discard loop, so use the longest extent 220 + * we found in the last batch as the key to start the next. 221 + */ 222 + static int 223 + xfs_trim_extents( 224 + struct xfs_perag *pag, 225 + xfs_daddr_t start, 226 + xfs_daddr_t end, 227 + xfs_daddr_t minlen, 228 + uint64_t *blocks_trimmed) 229 + { 230 + struct xfs_alloc_rec_incore tcur = { 231 + .ar_blockcount = pag->pagf_longest, 232 + .ar_startblock = NULLAGBLOCK, 233 + }; 234 + int error = 0; 235 + 236 + do { 237 + struct xfs_busy_extents *extents; 238 + 239 + extents = kzalloc(sizeof(*extents), GFP_KERNEL); 240 + if (!extents) { 241 + error = -ENOMEM; 242 + break; 243 + } 244 + 245 + extents->mount = pag->pag_mount; 246 + extents->owner = extents; 247 + INIT_LIST_HEAD(&extents->extent_list); 248 + 249 + error = xfs_trim_gather_extents(pag, start, end, minlen, 250 + &tcur, extents, blocks_trimmed); 251 + if (error) { 252 + kfree(extents); 253 + break; 254 + } 255 + 256 + /* 257 + * We hand the extent list to the discard function here so the 258 + * discarded extents can be removed from the busy extent list. 259 + * This allows the discards to run asynchronously with gathering 260 + * the next round of extents to discard. 261 + * 262 + * However, we must ensure that we do not reference the extent 263 + * list after this function call, as it may have been freed by 264 + * the time control returns to us. 265 + */ 266 + error = xfs_discard_extents(pag->pag_mount, extents); 267 if (error) 268 break; 269 ··· 200 error = -ERESTARTSYS; 201 break; 202 } 203 + } while (tcur.ar_blockcount != 0); 204 205 return error; 206 + 207 } 208 209 /*
+28 -6
fs/xfs/xfs_extent_busy.c
··· 19 #include "xfs_log.h" 20 #include "xfs_ag.h" 21 22 - void 23 - xfs_extent_busy_insert( 24 - struct xfs_trans *tp, 25 struct xfs_perag *pag, 26 xfs_agblock_t bno, 27 xfs_extlen_t len, 28 - unsigned int flags) 29 { 30 struct xfs_extent_busy *new; 31 struct xfs_extent_busy *busyp; ··· 40 new->flags = flags; 41 42 /* trace before insert to be able to see failed inserts */ 43 - trace_xfs_extent_busy(tp->t_mountp, pag->pag_agno, bno, len); 44 45 spin_lock(&pag->pagb_lock); 46 rbp = &pag->pagb_tree.rb_node; ··· 62 rb_link_node(&new->rb_node, parent, rbp); 63 rb_insert_color(&new->rb_node, &pag->pagb_tree); 64 65 - list_add(&new->list, &tp->t_busy); 66 spin_unlock(&pag->pagb_lock); 67 } 68 69 /*
··· 19 #include "xfs_log.h" 20 #include "xfs_ag.h" 21 22 + static void 23 + xfs_extent_busy_insert_list( 24 struct xfs_perag *pag, 25 xfs_agblock_t bno, 26 xfs_extlen_t len, 27 + unsigned int flags, 28 + struct list_head *busy_list) 29 { 30 struct xfs_extent_busy *new; 31 struct xfs_extent_busy *busyp; ··· 40 new->flags = flags; 41 42 /* trace before insert to be able to see failed inserts */ 43 + trace_xfs_extent_busy(pag->pag_mount, pag->pag_agno, bno, len); 44 45 spin_lock(&pag->pagb_lock); 46 rbp = &pag->pagb_tree.rb_node; ··· 62 rb_link_node(&new->rb_node, parent, rbp); 63 rb_insert_color(&new->rb_node, &pag->pagb_tree); 64 65 + list_add(&new->list, busy_list); 66 spin_unlock(&pag->pagb_lock); 67 + } 68 + 69 + void 70 + xfs_extent_busy_insert( 71 + struct xfs_trans *tp, 72 + struct xfs_perag *pag, 73 + xfs_agblock_t bno, 74 + xfs_extlen_t len, 75 + unsigned int flags) 76 + { 77 + xfs_extent_busy_insert_list(pag, bno, len, flags, &tp->t_busy); 78 + } 79 + 80 + void 81 + xfs_extent_busy_insert_discard( 82 + struct xfs_perag *pag, 83 + xfs_agblock_t bno, 84 + xfs_extlen_t len, 85 + struct list_head *busy_list) 86 + { 87 + xfs_extent_busy_insert_list(pag, bno, len, XFS_EXTENT_BUSY_DISCARDED, 88 + busy_list); 89 } 90 91 /*
+4
fs/xfs/xfs_extent_busy.h
··· 50 xfs_agblock_t bno, xfs_extlen_t len, unsigned int flags); 51 52 void 53 xfs_extent_busy_clear(struct xfs_mount *mp, struct list_head *list, 54 bool do_discard); 55
··· 50 xfs_agblock_t bno, xfs_extlen_t len, unsigned int flags); 51 52 void 53 + xfs_extent_busy_insert_discard(struct xfs_perag *pag, xfs_agblock_t bno, 54 + xfs_extlen_t len, struct list_head *busy_list); 55 + 56 + void 57 xfs_extent_busy_clear(struct xfs_mount *mp, struct list_head *list, 58 bool do_discard); 59