2018-06-06 10:42:14 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2012-04-29 18:39:43 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
|
|
|
|
* Copyright (c) 2010 David Chinner.
|
|
|
|
* Copyright (c) 2011 Christoph Hellwig.
|
|
|
|
* All Rights Reserved.
|
|
|
|
*/
|
|
|
|
#include "xfs.h"
|
|
|
|
#include "xfs_fs.h"
|
2013-10-23 07:36:05 +08:00
|
|
|
#include "xfs_format.h"
|
2013-10-23 07:50:10 +08:00
|
|
|
#include "xfs_log_format.h"
|
2013-10-23 07:36:05 +08:00
|
|
|
#include "xfs_shared.h"
|
2013-10-23 07:50:10 +08:00
|
|
|
#include "xfs_trans_resv.h"
|
2012-04-29 18:39:43 +08:00
|
|
|
#include "xfs_mount.h"
|
|
|
|
#include "xfs_alloc.h"
|
|
|
|
#include "xfs_extent_busy.h"
|
|
|
|
#include "xfs_trace.h"
|
2013-10-23 07:50:10 +08:00
|
|
|
#include "xfs_trans.h"
|
|
|
|
#include "xfs_log.h"
|
2021-06-02 08:48:24 +08:00
|
|
|
#include "xfs_ag.h"
|
2012-04-29 18:39:43 +08:00
|
|
|
|
2023-10-04 06:24:52 +08:00
|
|
|
static void
|
|
|
|
xfs_extent_busy_insert_list(
|
2021-06-02 08:48:24 +08:00
|
|
|
struct xfs_perag *pag,
|
2012-04-29 18:39:43 +08:00
|
|
|
xfs_agblock_t bno,
|
|
|
|
xfs_extlen_t len,
|
2023-10-04 06:24:52 +08:00
|
|
|
unsigned int flags,
|
|
|
|
struct list_head *busy_list)
|
2012-04-29 18:39:43 +08:00
|
|
|
{
|
2012-04-29 18:41:10 +08:00
|
|
|
struct xfs_extent_busy *new;
|
|
|
|
struct xfs_extent_busy *busyp;
|
2012-04-29 18:39:43 +08:00
|
|
|
struct rb_node **rbp;
|
|
|
|
struct rb_node *parent = NULL;
|
|
|
|
|
2019-08-27 03:06:22 +08:00
|
|
|
new = kmem_zalloc(sizeof(struct xfs_extent_busy), 0);
|
2021-06-02 08:48:24 +08:00
|
|
|
new->agno = pag->pag_agno;
|
2012-04-29 18:39:43 +08:00
|
|
|
new->bno = bno;
|
|
|
|
new->length = len;
|
|
|
|
INIT_LIST_HEAD(&new->list);
|
|
|
|
new->flags = flags;
|
|
|
|
|
|
|
|
/* trace before insert to be able to see failed inserts */
|
2023-10-04 06:24:52 +08:00
|
|
|
trace_xfs_extent_busy(pag->pag_mount, pag->pag_agno, bno, len);
|
2012-04-29 18:39:43 +08:00
|
|
|
|
|
|
|
spin_lock(&pag->pagb_lock);
|
|
|
|
rbp = &pag->pagb_tree.rb_node;
|
|
|
|
while (*rbp) {
|
|
|
|
parent = *rbp;
|
2012-04-29 18:41:10 +08:00
|
|
|
busyp = rb_entry(parent, struct xfs_extent_busy, rb_node);
|
2012-04-29 18:39:43 +08:00
|
|
|
|
|
|
|
if (new->bno < busyp->bno) {
|
|
|
|
rbp = &(*rbp)->rb_left;
|
|
|
|
ASSERT(new->bno + new->length <= busyp->bno);
|
|
|
|
} else if (new->bno > busyp->bno) {
|
|
|
|
rbp = &(*rbp)->rb_right;
|
|
|
|
ASSERT(bno >= busyp->bno + busyp->length);
|
|
|
|
} else {
|
|
|
|
ASSERT(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_link_node(&new->rb_node, parent, rbp);
|
|
|
|
rb_insert_color(&new->rb_node, &pag->pagb_tree);
|
|
|
|
|
2023-10-12 03:35:21 +08:00
|
|
|
/* always process discard lists in fifo order */
|
|
|
|
list_add_tail(&new->list, busy_list);
|
2012-04-29 18:39:43 +08:00
|
|
|
spin_unlock(&pag->pagb_lock);
|
|
|
|
}
|
|
|
|
|
2023-10-04 06:24:52 +08:00
|
|
|
void
|
|
|
|
xfs_extent_busy_insert(
|
|
|
|
struct xfs_trans *tp,
|
|
|
|
struct xfs_perag *pag,
|
|
|
|
xfs_agblock_t bno,
|
|
|
|
xfs_extlen_t len,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
xfs_extent_busy_insert_list(pag, bno, len, flags, &tp->t_busy);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
xfs_extent_busy_insert_discard(
|
|
|
|
struct xfs_perag *pag,
|
|
|
|
xfs_agblock_t bno,
|
|
|
|
xfs_extlen_t len,
|
|
|
|
struct list_head *busy_list)
|
|
|
|
{
|
|
|
|
xfs_extent_busy_insert_list(pag, bno, len, XFS_EXTENT_BUSY_DISCARDED,
|
|
|
|
busy_list);
|
|
|
|
}
|
|
|
|
|
2012-04-29 18:39:43 +08:00
|
|
|
/*
|
|
|
|
* Search for a busy extent within the range of the extent we are about to
|
|
|
|
* allocate. You need to be holding the busy extent tree lock when calling
|
2012-04-29 18:41:10 +08:00
|
|
|
* xfs_extent_busy_search(). This function returns 0 for no overlapping busy
|
2012-04-29 18:39:43 +08:00
|
|
|
* extent, -1 for an overlapping but not exact busy extent, and 1 for an exact
|
|
|
|
* match. This is done so that a non-zero return indicates an overlap that
|
|
|
|
* will require a synchronous transaction, but it can still be
|
|
|
|
* used to distinguish between a partial or exact match.
|
|
|
|
*/
|
|
|
|
int
|
2012-04-29 18:41:10 +08:00
|
|
|
xfs_extent_busy_search(
|
2012-04-29 18:39:43 +08:00
|
|
|
struct xfs_mount *mp,
|
2021-06-02 08:48:24 +08:00
|
|
|
struct xfs_perag *pag,
|
2012-04-29 18:39:43 +08:00
|
|
|
xfs_agblock_t bno,
|
|
|
|
xfs_extlen_t len)
|
|
|
|
{
|
|
|
|
struct rb_node *rbp;
|
2012-04-29 18:41:10 +08:00
|
|
|
struct xfs_extent_busy *busyp;
|
2012-04-29 18:39:43 +08:00
|
|
|
int match = 0;
|
|
|
|
|
2021-06-02 08:48:24 +08:00
|
|
|
/* find closest start bno overlap */
|
2012-04-29 18:39:43 +08:00
|
|
|
spin_lock(&pag->pagb_lock);
|
|
|
|
rbp = pag->pagb_tree.rb_node;
|
|
|
|
while (rbp) {
|
2012-04-29 18:41:10 +08:00
|
|
|
busyp = rb_entry(rbp, struct xfs_extent_busy, rb_node);
|
2012-04-29 18:39:43 +08:00
|
|
|
if (bno < busyp->bno) {
|
|
|
|
/* may overlap, but exact start block is lower */
|
|
|
|
if (bno + len > busyp->bno)
|
|
|
|
match = -1;
|
|
|
|
rbp = rbp->rb_left;
|
|
|
|
} else if (bno > busyp->bno) {
|
|
|
|
/* may overlap, but exact start block is higher */
|
|
|
|
if (bno < busyp->bno + busyp->length)
|
|
|
|
match = -1;
|
|
|
|
rbp = rbp->rb_right;
|
|
|
|
} else {
|
|
|
|
/* bno matches busyp, length determines exact match */
|
|
|
|
match = (busyp->length == len) ? 1 : -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
spin_unlock(&pag->pagb_lock);
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The found free extent [fbno, fend] overlaps part or all of the given busy
|
|
|
|
* extent. If the overlap covers the beginning, the end, or all of the busy
|
|
|
|
* extent, the overlapping portion can be made unbusy and used for the
|
|
|
|
* allocation. We can't split a busy extent because we can't modify a
|
2013-08-12 11:14:57 +08:00
|
|
|
* transaction/CIL context busy list, but we can update an entry's block
|
2012-04-29 18:39:43 +08:00
|
|
|
* number or length.
|
|
|
|
*
|
|
|
|
* Returns true if the extent can safely be reused, or false if the search
|
|
|
|
* needs to be restarted.
|
|
|
|
*/
|
|
|
|
STATIC bool
|
2012-04-29 18:41:10 +08:00
|
|
|
xfs_extent_busy_update_extent(
|
2012-04-29 18:39:43 +08:00
|
|
|
struct xfs_mount *mp,
|
|
|
|
struct xfs_perag *pag,
|
2012-04-29 18:41:10 +08:00
|
|
|
struct xfs_extent_busy *busyp,
|
2012-04-29 18:39:43 +08:00
|
|
|
xfs_agblock_t fbno,
|
|
|
|
xfs_extlen_t flen,
|
2013-09-02 18:49:36 +08:00
|
|
|
bool userdata) __releases(&pag->pagb_lock)
|
|
|
|
__acquires(&pag->pagb_lock)
|
2012-04-29 18:39:43 +08:00
|
|
|
{
|
|
|
|
xfs_agblock_t fend = fbno + flen;
|
|
|
|
xfs_agblock_t bbno = busyp->bno;
|
|
|
|
xfs_agblock_t bend = bbno + busyp->length;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This extent is currently being discarded. Give the thread
|
|
|
|
* performing the discard a chance to mark the extent unbusy
|
|
|
|
* and retry.
|
|
|
|
*/
|
2012-04-29 18:41:10 +08:00
|
|
|
if (busyp->flags & XFS_EXTENT_BUSY_DISCARDED) {
|
2012-04-29 18:39:43 +08:00
|
|
|
spin_unlock(&pag->pagb_lock);
|
|
|
|
delay(1);
|
|
|
|
spin_lock(&pag->pagb_lock);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If there is a busy extent overlapping a user allocation, we have
|
|
|
|
* no choice but to force the log and retry the search.
|
|
|
|
*
|
|
|
|
* Fortunately this does not happen during normal operation, but
|
|
|
|
* only if the filesystem is very low on space and has to dip into
|
|
|
|
* the AGFL for normal allocations.
|
|
|
|
*/
|
|
|
|
if (userdata)
|
|
|
|
goto out_force_log;
|
|
|
|
|
|
|
|
if (bbno < fbno && bend > fend) {
|
|
|
|
/*
|
|
|
|
* Case 1:
|
|
|
|
* bbno bend
|
|
|
|
* +BBBBBBBBBBBBBBBBB+
|
|
|
|
* +---------+
|
|
|
|
* fbno fend
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We would have to split the busy extent to be able to track
|
|
|
|
* it correct, which we cannot do because we would have to
|
|
|
|
* modify the list of busy extents attached to the transaction
|
|
|
|
* or CIL context, which is immutable.
|
|
|
|
*
|
|
|
|
* Force out the log to clear the busy extent and retry the
|
|
|
|
* search.
|
|
|
|
*/
|
|
|
|
goto out_force_log;
|
|
|
|
} else if (bbno >= fbno && bend <= fend) {
|
|
|
|
/*
|
|
|
|
* Case 2:
|
|
|
|
* bbno bend
|
|
|
|
* +BBBBBBBBBBBBBBBBB+
|
|
|
|
* +-----------------+
|
|
|
|
* fbno fend
|
|
|
|
*
|
|
|
|
* Case 3:
|
|
|
|
* bbno bend
|
|
|
|
* +BBBBBBBBBBBBBBBBB+
|
|
|
|
* +--------------------------+
|
|
|
|
* fbno fend
|
|
|
|
*
|
|
|
|
* Case 4:
|
|
|
|
* bbno bend
|
|
|
|
* +BBBBBBBBBBBBBBBBB+
|
|
|
|
* +--------------------------+
|
|
|
|
* fbno fend
|
|
|
|
*
|
|
|
|
* Case 5:
|
|
|
|
* bbno bend
|
|
|
|
* +BBBBBBBBBBBBBBBBB+
|
|
|
|
* +-----------------------------------+
|
|
|
|
* fbno fend
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The busy extent is fully covered by the extent we are
|
|
|
|
* allocating, and can simply be removed from the rbtree.
|
|
|
|
* However we cannot remove it from the immutable list
|
|
|
|
* tracking busy extents in the transaction or CIL context,
|
|
|
|
* so set the length to zero to mark it invalid.
|
|
|
|
*
|
|
|
|
* We also need to restart the busy extent search from the
|
|
|
|
* tree root, because erasing the node can rearrange the
|
|
|
|
* tree topology.
|
|
|
|
*/
|
|
|
|
rb_erase(&busyp->rb_node, &pag->pagb_tree);
|
|
|
|
busyp->length = 0;
|
|
|
|
return false;
|
|
|
|
} else if (fend < bend) {
|
|
|
|
/*
|
|
|
|
* Case 6:
|
|
|
|
* bbno bend
|
|
|
|
* +BBBBBBBBBBBBBBBBB+
|
|
|
|
* +---------+
|
|
|
|
* fbno fend
|
|
|
|
*
|
|
|
|
* Case 7:
|
|
|
|
* bbno bend
|
|
|
|
* +BBBBBBBBBBBBBBBBB+
|
|
|
|
* +------------------+
|
|
|
|
* fbno fend
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
busyp->bno = fend;
|
2023-01-05 02:05:43 +08:00
|
|
|
busyp->length = bend - fend;
|
2012-04-29 18:39:43 +08:00
|
|
|
} else if (bbno < fbno) {
|
|
|
|
/*
|
|
|
|
* Case 8:
|
|
|
|
* bbno bend
|
|
|
|
* +BBBBBBBBBBBBBBBBB+
|
|
|
|
* +-------------+
|
|
|
|
* fbno fend
|
|
|
|
*
|
|
|
|
* Case 9:
|
|
|
|
* bbno bend
|
|
|
|
* +BBBBBBBBBBBBBBBBB+
|
|
|
|
* +----------------------+
|
|
|
|
* fbno fend
|
|
|
|
*/
|
|
|
|
busyp->length = fbno - busyp->bno;
|
|
|
|
} else {
|
|
|
|
ASSERT(0);
|
|
|
|
}
|
|
|
|
|
2012-04-29 18:41:10 +08:00
|
|
|
trace_xfs_extent_busy_reuse(mp, pag->pag_agno, fbno, flen);
|
2012-04-29 18:39:43 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
out_force_log:
|
|
|
|
spin_unlock(&pag->pagb_lock);
|
|
|
|
xfs_log_force(mp, XFS_LOG_SYNC);
|
2012-04-29 18:41:10 +08:00
|
|
|
trace_xfs_extent_busy_force(mp, pag->pag_agno, fbno, flen);
|
2012-04-29 18:39:43 +08:00
|
|
|
spin_lock(&pag->pagb_lock);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For a given extent [fbno, flen], make sure we can reuse it safely.
|
|
|
|
*/
|
|
|
|
void
|
2012-04-29 18:41:10 +08:00
|
|
|
xfs_extent_busy_reuse(
|
2012-04-29 18:39:43 +08:00
|
|
|
struct xfs_mount *mp,
|
2021-06-02 08:48:24 +08:00
|
|
|
struct xfs_perag *pag,
|
2012-04-29 18:39:43 +08:00
|
|
|
xfs_agblock_t fbno,
|
|
|
|
xfs_extlen_t flen,
|
|
|
|
bool userdata)
|
|
|
|
{
|
|
|
|
struct rb_node *rbp;
|
|
|
|
|
|
|
|
ASSERT(flen > 0);
|
|
|
|
spin_lock(&pag->pagb_lock);
|
|
|
|
restart:
|
|
|
|
rbp = pag->pagb_tree.rb_node;
|
|
|
|
while (rbp) {
|
2012-04-29 18:41:10 +08:00
|
|
|
struct xfs_extent_busy *busyp =
|
|
|
|
rb_entry(rbp, struct xfs_extent_busy, rb_node);
|
2012-04-29 18:39:43 +08:00
|
|
|
xfs_agblock_t bbno = busyp->bno;
|
|
|
|
xfs_agblock_t bend = bbno + busyp->length;
|
|
|
|
|
|
|
|
if (fbno + flen <= bbno) {
|
|
|
|
rbp = rbp->rb_left;
|
|
|
|
continue;
|
|
|
|
} else if (fbno >= bend) {
|
|
|
|
rbp = rbp->rb_right;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-04-29 18:41:10 +08:00
|
|
|
if (!xfs_extent_busy_update_extent(mp, pag, busyp, fbno, flen,
|
2012-04-29 18:39:43 +08:00
|
|
|
userdata))
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
spin_unlock(&pag->pagb_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For a given extent [fbno, flen], search the busy extent list to find a
|
|
|
|
* subset of the extent that is not busy. If *rlen is smaller than
|
|
|
|
* args->minlen no suitable extent could be found, and the higher level
|
|
|
|
* code needs to force out the log and retry the allocation.
|
2017-02-08 06:06:57 +08:00
|
|
|
*
|
|
|
|
* Return the current busy generation for the AG if the extent is busy. This
|
|
|
|
* value can be used to wait for at least one of the currently busy extents
|
|
|
|
* to be cleared. Note that the busy list is not guaranteed to be empty after
|
|
|
|
* the gen is woken. The state of a specific extent must always be confirmed
|
|
|
|
* with another call to xfs_extent_busy_trim() before it can be used.
|
2012-04-29 18:39:43 +08:00
|
|
|
*/
|
2017-02-08 06:06:57 +08:00
|
|
|
bool
|
2012-04-29 18:41:10 +08:00
|
|
|
xfs_extent_busy_trim(
|
2012-04-29 18:39:43 +08:00
|
|
|
struct xfs_alloc_arg *args,
|
2017-02-08 06:06:57 +08:00
|
|
|
xfs_agblock_t *bno,
|
|
|
|
xfs_extlen_t *len,
|
|
|
|
unsigned *busy_gen)
|
2012-04-29 18:39:43 +08:00
|
|
|
{
|
|
|
|
xfs_agblock_t fbno;
|
|
|
|
xfs_extlen_t flen;
|
|
|
|
struct rb_node *rbp;
|
2017-02-08 06:06:57 +08:00
|
|
|
bool ret = false;
|
2012-04-29 18:39:43 +08:00
|
|
|
|
2017-02-15 10:16:50 +08:00
|
|
|
ASSERT(*len > 0);
|
2012-04-29 18:39:43 +08:00
|
|
|
|
|
|
|
spin_lock(&args->pag->pagb_lock);
|
2017-02-08 06:06:57 +08:00
|
|
|
fbno = *bno;
|
|
|
|
flen = *len;
|
2012-04-29 18:39:43 +08:00
|
|
|
rbp = args->pag->pagb_tree.rb_node;
|
|
|
|
while (rbp && flen >= args->minlen) {
|
2012-04-29 18:41:10 +08:00
|
|
|
struct xfs_extent_busy *busyp =
|
|
|
|
rb_entry(rbp, struct xfs_extent_busy, rb_node);
|
2012-04-29 18:39:43 +08:00
|
|
|
xfs_agblock_t fend = fbno + flen;
|
|
|
|
xfs_agblock_t bbno = busyp->bno;
|
|
|
|
xfs_agblock_t bend = bbno + busyp->length;
|
|
|
|
|
|
|
|
if (fend <= bbno) {
|
|
|
|
rbp = rbp->rb_left;
|
|
|
|
continue;
|
|
|
|
} else if (fbno >= bend) {
|
|
|
|
rbp = rbp->rb_right;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bbno <= fbno) {
|
|
|
|
/* start overlap */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Case 1:
|
|
|
|
* bbno bend
|
|
|
|
* +BBBBBBBBBBBBBBBBB+
|
|
|
|
* +---------+
|
|
|
|
* fbno fend
|
|
|
|
*
|
|
|
|
* Case 2:
|
|
|
|
* bbno bend
|
|
|
|
* +BBBBBBBBBBBBBBBBB+
|
|
|
|
* +-------------+
|
|
|
|
* fbno fend
|
|
|
|
*
|
|
|
|
* Case 3:
|
|
|
|
* bbno bend
|
|
|
|
* +BBBBBBBBBBBBBBBBB+
|
|
|
|
* +-------------+
|
|
|
|
* fbno fend
|
|
|
|
*
|
|
|
|
* Case 4:
|
|
|
|
* bbno bend
|
|
|
|
* +BBBBBBBBBBBBBBBBB+
|
|
|
|
* +-----------------+
|
|
|
|
* fbno fend
|
|
|
|
*
|
|
|
|
* No unbusy region in extent, return failure.
|
|
|
|
*/
|
|
|
|
if (fend <= bend)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Case 5:
|
|
|
|
* bbno bend
|
|
|
|
* +BBBBBBBBBBBBBBBBB+
|
|
|
|
* +----------------------+
|
|
|
|
* fbno fend
|
|
|
|
*
|
|
|
|
* Case 6:
|
|
|
|
* bbno bend
|
|
|
|
* +BBBBBBBBBBBBBBBBB+
|
|
|
|
* +--------------------------+
|
|
|
|
* fbno fend
|
|
|
|
*
|
|
|
|
* Needs to be trimmed to:
|
|
|
|
* +-------+
|
|
|
|
* fbno fend
|
|
|
|
*/
|
|
|
|
fbno = bend;
|
|
|
|
} else if (bend >= fend) {
|
|
|
|
/* end overlap */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Case 7:
|
|
|
|
* bbno bend
|
|
|
|
* +BBBBBBBBBBBBBBBBB+
|
|
|
|
* +------------------+
|
|
|
|
* fbno fend
|
|
|
|
*
|
|
|
|
* Case 8:
|
|
|
|
* bbno bend
|
|
|
|
* +BBBBBBBBBBBBBBBBB+
|
|
|
|
* +--------------------------+
|
|
|
|
* fbno fend
|
|
|
|
*
|
|
|
|
* Needs to be trimmed to:
|
|
|
|
* +-------+
|
|
|
|
* fbno fend
|
|
|
|
*/
|
|
|
|
fend = bbno;
|
|
|
|
} else {
|
|
|
|
/* middle overlap */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Case 9:
|
|
|
|
* bbno bend
|
|
|
|
* +BBBBBBBBBBBBBBBBB+
|
|
|
|
* +-----------------------------------+
|
|
|
|
* fbno fend
|
|
|
|
*
|
|
|
|
* Can be trimmed to:
|
|
|
|
* +-------+ OR +-------+
|
|
|
|
* fbno fend fbno fend
|
|
|
|
*
|
|
|
|
* Backward allocation leads to significant
|
|
|
|
* fragmentation of directories, which degrades
|
|
|
|
* directory performance, therefore we always want to
|
|
|
|
* choose the option that produces forward allocation
|
|
|
|
* patterns.
|
|
|
|
* Preferring the lower bno extent will make the next
|
|
|
|
* request use "fend" as the start of the next
|
|
|
|
* allocation; if the segment is no longer busy at
|
|
|
|
* that point, we'll get a contiguous allocation, but
|
|
|
|
* even if it is still busy, we will get a forward
|
|
|
|
* allocation.
|
|
|
|
* We try to avoid choosing the segment at "bend",
|
|
|
|
* because that can lead to the next allocation
|
|
|
|
* taking the segment at "fbno", which would be a
|
|
|
|
* backward allocation. We only use the segment at
|
|
|
|
* "fbno" if it is much larger than the current
|
|
|
|
* requested size, because in that case there's a
|
|
|
|
* good chance subsequent allocations will be
|
|
|
|
* contiguous.
|
|
|
|
*/
|
|
|
|
if (bbno - fbno >= args->maxlen) {
|
|
|
|
/* left candidate fits perfect */
|
|
|
|
fend = bbno;
|
|
|
|
} else if (fend - bend >= args->maxlen * 4) {
|
|
|
|
/* right candidate has enough free space */
|
|
|
|
fbno = bend;
|
|
|
|
} else if (bbno - fbno >= args->minlen) {
|
|
|
|
/* left candidate fits minimum requirement */
|
|
|
|
fend = bbno;
|
|
|
|
} else {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
flen = fend - fbno;
|
|
|
|
}
|
2017-02-08 06:06:57 +08:00
|
|
|
out:
|
2012-04-29 18:39:43 +08:00
|
|
|
|
2017-02-08 06:06:57 +08:00
|
|
|
if (fbno != *bno || flen != *len) {
|
|
|
|
trace_xfs_extent_busy_trim(args->mp, args->agno, *bno, *len,
|
2012-04-29 18:39:43 +08:00
|
|
|
fbno, flen);
|
2017-02-08 06:06:57 +08:00
|
|
|
*bno = fbno;
|
|
|
|
*len = flen;
|
|
|
|
*busy_gen = args->pag->pagb_gen;
|
|
|
|
ret = true;
|
2012-04-29 18:39:43 +08:00
|
|
|
}
|
2017-02-08 06:06:57 +08:00
|
|
|
spin_unlock(&args->pag->pagb_lock);
|
|
|
|
return ret;
|
2012-04-29 18:39:43 +08:00
|
|
|
fail:
|
|
|
|
/*
|
|
|
|
* Return a zero extent length as failure indications. All callers
|
|
|
|
* re-check if the trimmed extent satisfies the minlen requirement.
|
|
|
|
*/
|
2017-02-08 06:06:57 +08:00
|
|
|
flen = 0;
|
|
|
|
goto out;
|
2012-04-29 18:39:43 +08:00
|
|
|
}
|
|
|
|
|
2012-04-29 18:41:10 +08:00
|
|
|
STATIC void
|
|
|
|
xfs_extent_busy_clear_one(
|
2012-04-29 18:39:43 +08:00
|
|
|
struct xfs_mount *mp,
|
|
|
|
struct xfs_perag *pag,
|
2012-04-29 18:41:10 +08:00
|
|
|
struct xfs_extent_busy *busyp)
|
2012-04-29 18:39:43 +08:00
|
|
|
{
|
|
|
|
if (busyp->length) {
|
2012-04-29 18:41:10 +08:00
|
|
|
trace_xfs_extent_busy_clear(mp, busyp->agno, busyp->bno,
|
2012-04-29 18:39:43 +08:00
|
|
|
busyp->length);
|
|
|
|
rb_erase(&busyp->rb_node, &pag->pagb_tree);
|
|
|
|
}
|
|
|
|
|
|
|
|
list_del_init(&busyp->list);
|
|
|
|
kmem_free(busyp);
|
|
|
|
}
|
|
|
|
|
2017-02-08 06:06:57 +08:00
|
|
|
static void
|
|
|
|
xfs_extent_busy_put_pag(
|
|
|
|
struct xfs_perag *pag,
|
|
|
|
bool wakeup)
|
|
|
|
__releases(pag->pagb_lock)
|
|
|
|
{
|
|
|
|
if (wakeup) {
|
|
|
|
pag->pagb_gen++;
|
|
|
|
wake_up_all(&pag->pagb_wait);
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock(&pag->pagb_lock);
|
|
|
|
xfs_perag_put(pag);
|
|
|
|
}
|
|
|
|
|
2012-04-29 18:39:43 +08:00
|
|
|
/*
|
|
|
|
* Remove all extents on the passed in list from the busy extents tree.
|
|
|
|
* If do_discard is set skip extents that need to be discarded, and mark
|
|
|
|
* these as undergoing a discard operation instead.
|
|
|
|
*/
|
|
|
|
void
|
2012-04-29 18:41:10 +08:00
|
|
|
xfs_extent_busy_clear(
|
2012-04-29 18:39:43 +08:00
|
|
|
struct xfs_mount *mp,
|
|
|
|
struct list_head *list,
|
|
|
|
bool do_discard)
|
|
|
|
{
|
2012-04-29 18:41:10 +08:00
|
|
|
struct xfs_extent_busy *busyp, *n;
|
2012-04-29 18:39:43 +08:00
|
|
|
struct xfs_perag *pag = NULL;
|
|
|
|
xfs_agnumber_t agno = NULLAGNUMBER;
|
2017-02-08 06:06:57 +08:00
|
|
|
bool wakeup = false;
|
2012-04-29 18:39:43 +08:00
|
|
|
|
|
|
|
list_for_each_entry_safe(busyp, n, list, list) {
|
|
|
|
if (busyp->agno != agno) {
|
2017-02-08 06:06:57 +08:00
|
|
|
if (pag)
|
|
|
|
xfs_extent_busy_put_pag(pag, wakeup);
|
2012-04-29 18:39:43 +08:00
|
|
|
agno = busyp->agno;
|
2017-02-08 06:06:57 +08:00
|
|
|
pag = xfs_perag_get(mp, agno);
|
|
|
|
spin_lock(&pag->pagb_lock);
|
|
|
|
wakeup = false;
|
2012-04-29 18:39:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (do_discard && busyp->length &&
|
2017-02-08 06:06:57 +08:00
|
|
|
!(busyp->flags & XFS_EXTENT_BUSY_SKIP_DISCARD)) {
|
2012-04-29 18:41:10 +08:00
|
|
|
busyp->flags = XFS_EXTENT_BUSY_DISCARDED;
|
2017-02-08 06:06:57 +08:00
|
|
|
} else {
|
2012-04-29 18:41:10 +08:00
|
|
|
xfs_extent_busy_clear_one(mp, pag, busyp);
|
2017-02-08 06:06:57 +08:00
|
|
|
wakeup = true;
|
|
|
|
}
|
2012-04-29 18:39:43 +08:00
|
|
|
}
|
|
|
|
|
2017-02-08 06:06:57 +08:00
|
|
|
if (pag)
|
|
|
|
xfs_extent_busy_put_pag(pag, wakeup);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Flush out all busy extents for this AG.
|
xfs: don't block in busy flushing when freeing extents
If the current transaction holds a busy extent and we are trying to
allocate a new extent to fix up the free list, we can deadlock if
the AG is entirely empty except for the busy extent held by the
transaction.
This can occur at runtime processing an XEFI with multiple extents
in this path:
__schedule+0x22f at ffffffff81f75e8f
schedule+0x46 at ffffffff81f76366
xfs_extent_busy_flush+0x69 at ffffffff81477d99
xfs_alloc_ag_vextent_size+0x16a at ffffffff8141711a
xfs_alloc_ag_vextent+0x19b at ffffffff81417edb
xfs_alloc_fix_freelist+0x22f at ffffffff8141896f
xfs_free_extent_fix_freelist+0x6a at ffffffff8141939a
__xfs_free_extent+0x99 at ffffffff81419499
xfs_trans_free_extent+0x3e at ffffffff814a6fee
xfs_extent_free_finish_item+0x24 at ffffffff814a70d4
xfs_defer_finish_noroll+0x1f7 at ffffffff81441407
xfs_defer_finish+0x11 at ffffffff814417e1
xfs_itruncate_extents_flags+0x13d at ffffffff8148b7dd
xfs_inactive_truncate+0xb9 at ffffffff8148bb89
xfs_inactive+0x227 at ffffffff8148c4f7
xfs_fs_destroy_inode+0xb8 at ffffffff81496898
destroy_inode+0x3b at ffffffff8127d2ab
do_unlinkat+0x1d1 at ffffffff81270df1
do_syscall_64+0x40 at ffffffff81f6b5f0
entry_SYSCALL_64_after_hwframe+0x44 at ffffffff8200007c
This can also happen in log recovery when processing an EFI
with multiple extents through this path:
context_switch() kernel/sched/core.c:3881
__schedule() kernel/sched/core.c:5111
schedule() kernel/sched/core.c:5186
xfs_extent_busy_flush() fs/xfs/xfs_extent_busy.c:598
xfs_alloc_ag_vextent_size() fs/xfs/libxfs/xfs_alloc.c:1641
xfs_alloc_ag_vextent() fs/xfs/libxfs/xfs_alloc.c:828
xfs_alloc_fix_freelist() fs/xfs/libxfs/xfs_alloc.c:2362
xfs_free_extent_fix_freelist() fs/xfs/libxfs/xfs_alloc.c:3029
__xfs_free_extent() fs/xfs/libxfs/xfs_alloc.c:3067
xfs_trans_free_extent() fs/xfs/xfs_extfree_item.c:370
xfs_efi_recover() fs/xfs/xfs_extfree_item.c:626
xlog_recover_process_efi() fs/xfs/xfs_log_recover.c:4605
xlog_recover_process_intents() fs/xfs/xfs_log_recover.c:4893
xlog_recover_finish() fs/xfs/xfs_log_recover.c:5824
xfs_log_mount_finish() fs/xfs/xfs_log.c:764
xfs_mountfs() fs/xfs/xfs_mount.c:978
xfs_fs_fill_super() fs/xfs/xfs_super.c:1908
mount_bdev() fs/super.c:1417
xfs_fs_mount() fs/xfs/xfs_super.c:1985
legacy_get_tree() fs/fs_context.c:647
vfs_get_tree() fs/super.c:1547
do_new_mount() fs/namespace.c:2843
do_mount() fs/namespace.c:3163
ksys_mount() fs/namespace.c:3372
__do_sys_mount() fs/namespace.c:3386
__se_sys_mount() fs/namespace.c:3383
__x64_sys_mount() fs/namespace.c:3383
do_syscall_64() arch/x86/entry/common.c:296
entry_SYSCALL_64() arch/x86/entry/entry_64.S:180
To avoid this deadlock, we should not block in
xfs_extent_busy_flush() if we hold a busy extent in the current
transaction.
Now that the EFI processing code can handle requeuing a partially
completed EFI, we can detect this situation in
xfs_extent_busy_flush() and return -EAGAIN rather than going to
sleep forever. The -EAGAIN get propagated back out to the
xfs_trans_free_extent() context, where the EFD is populated and the
transaction is rolled, thereby moving the busy extents into the CIL.
At this point, we can retry the extent free operation again with a
clean transaction. If we hit the same "all free extents are busy"
situation when trying to fix up the free list, we can safely call
xfs_extent_busy_flush() and wait for the busy extents to resolve
and wake us. At this point, the allocation search can make progress
again and we can fix up the free list.
This deadlock was first reported by Chandan in mid-2021, but I
couldn't make myself understood during review, and didn't have time
to fix it myself.
It was reported again in March 2023, and again I have found myself
unable to explain the complexities of the solution needed during
review.
As such, I don't have hours more time to waste trying to get the
fix written the way it needs to be written, so I'm just doing it
myself. This patchset is largely based on Wengang Wang's last patch,
but with all the unnecessary stuff removed, split up into multiple
patches and cleaned up somewhat.
Reported-by: Chandan Babu R <chandanrlinux@gmail.com>
Reported-by: Wengang Wang <wen.gang.wang@oracle.com>
Signed-off-by: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Darrick J. Wong <djwong@kernel.org>
Signed-off-by: Darrick J. Wong <djwong@kernel.org>
2023-06-29 02:04:33 +08:00
|
|
|
*
|
|
|
|
* If the current transaction is holding busy extents, the caller may not want
|
|
|
|
* to wait for committed busy extents to resolve. If we are being told just to
|
|
|
|
* try a flush or progress has been made since we last skipped a busy extent,
|
|
|
|
* return immediately to allow the caller to try again.
|
|
|
|
*
|
|
|
|
* If we are freeing extents, we might actually be holding the only free extents
|
|
|
|
* in the transaction busy list and the log force won't resolve that situation.
|
|
|
|
* In this case, we must return -EAGAIN to avoid a deadlock by informing the
|
|
|
|
* caller it needs to commit the busy extents it holds before retrying the
|
|
|
|
* extent free operation.
|
2017-02-08 06:06:57 +08:00
|
|
|
*/
|
xfs: don't block in busy flushing when freeing extents
If the current transaction holds a busy extent and we are trying to
allocate a new extent to fix up the free list, we can deadlock if
the AG is entirely empty except for the busy extent held by the
transaction.
This can occur at runtime processing an XEFI with multiple extents
in this path:
__schedule+0x22f at ffffffff81f75e8f
schedule+0x46 at ffffffff81f76366
xfs_extent_busy_flush+0x69 at ffffffff81477d99
xfs_alloc_ag_vextent_size+0x16a at ffffffff8141711a
xfs_alloc_ag_vextent+0x19b at ffffffff81417edb
xfs_alloc_fix_freelist+0x22f at ffffffff8141896f
xfs_free_extent_fix_freelist+0x6a at ffffffff8141939a
__xfs_free_extent+0x99 at ffffffff81419499
xfs_trans_free_extent+0x3e at ffffffff814a6fee
xfs_extent_free_finish_item+0x24 at ffffffff814a70d4
xfs_defer_finish_noroll+0x1f7 at ffffffff81441407
xfs_defer_finish+0x11 at ffffffff814417e1
xfs_itruncate_extents_flags+0x13d at ffffffff8148b7dd
xfs_inactive_truncate+0xb9 at ffffffff8148bb89
xfs_inactive+0x227 at ffffffff8148c4f7
xfs_fs_destroy_inode+0xb8 at ffffffff81496898
destroy_inode+0x3b at ffffffff8127d2ab
do_unlinkat+0x1d1 at ffffffff81270df1
do_syscall_64+0x40 at ffffffff81f6b5f0
entry_SYSCALL_64_after_hwframe+0x44 at ffffffff8200007c
This can also happen in log recovery when processing an EFI
with multiple extents through this path:
context_switch() kernel/sched/core.c:3881
__schedule() kernel/sched/core.c:5111
schedule() kernel/sched/core.c:5186
xfs_extent_busy_flush() fs/xfs/xfs_extent_busy.c:598
xfs_alloc_ag_vextent_size() fs/xfs/libxfs/xfs_alloc.c:1641
xfs_alloc_ag_vextent() fs/xfs/libxfs/xfs_alloc.c:828
xfs_alloc_fix_freelist() fs/xfs/libxfs/xfs_alloc.c:2362
xfs_free_extent_fix_freelist() fs/xfs/libxfs/xfs_alloc.c:3029
__xfs_free_extent() fs/xfs/libxfs/xfs_alloc.c:3067
xfs_trans_free_extent() fs/xfs/xfs_extfree_item.c:370
xfs_efi_recover() fs/xfs/xfs_extfree_item.c:626
xlog_recover_process_efi() fs/xfs/xfs_log_recover.c:4605
xlog_recover_process_intents() fs/xfs/xfs_log_recover.c:4893
xlog_recover_finish() fs/xfs/xfs_log_recover.c:5824
xfs_log_mount_finish() fs/xfs/xfs_log.c:764
xfs_mountfs() fs/xfs/xfs_mount.c:978
xfs_fs_fill_super() fs/xfs/xfs_super.c:1908
mount_bdev() fs/super.c:1417
xfs_fs_mount() fs/xfs/xfs_super.c:1985
legacy_get_tree() fs/fs_context.c:647
vfs_get_tree() fs/super.c:1547
do_new_mount() fs/namespace.c:2843
do_mount() fs/namespace.c:3163
ksys_mount() fs/namespace.c:3372
__do_sys_mount() fs/namespace.c:3386
__se_sys_mount() fs/namespace.c:3383
__x64_sys_mount() fs/namespace.c:3383
do_syscall_64() arch/x86/entry/common.c:296
entry_SYSCALL_64() arch/x86/entry/entry_64.S:180
To avoid this deadlock, we should not block in
xfs_extent_busy_flush() if we hold a busy extent in the current
transaction.
Now that the EFI processing code can handle requeuing a partially
completed EFI, we can detect this situation in
xfs_extent_busy_flush() and return -EAGAIN rather than going to
sleep forever. The -EAGAIN get propagated back out to the
xfs_trans_free_extent() context, where the EFD is populated and the
transaction is rolled, thereby moving the busy extents into the CIL.
At this point, we can retry the extent free operation again with a
clean transaction. If we hit the same "all free extents are busy"
situation when trying to fix up the free list, we can safely call
xfs_extent_busy_flush() and wait for the busy extents to resolve
and wake us. At this point, the allocation search can make progress
again and we can fix up the free list.
This deadlock was first reported by Chandan in mid-2021, but I
couldn't make myself understood during review, and didn't have time
to fix it myself.
It was reported again in March 2023, and again I have found myself
unable to explain the complexities of the solution needed during
review.
As such, I don't have hours more time to waste trying to get the
fix written the way it needs to be written, so I'm just doing it
myself. This patchset is largely based on Wengang Wang's last patch,
but with all the unnecessary stuff removed, split up into multiple
patches and cleaned up somewhat.
Reported-by: Chandan Babu R <chandanrlinux@gmail.com>
Reported-by: Wengang Wang <wen.gang.wang@oracle.com>
Signed-off-by: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Darrick J. Wong <djwong@kernel.org>
Signed-off-by: Darrick J. Wong <djwong@kernel.org>
2023-06-29 02:04:33 +08:00
|
|
|
int
|
2017-02-08 06:06:57 +08:00
|
|
|
xfs_extent_busy_flush(
|
xfs: don't block in busy flushing when freeing extents
If the current transaction holds a busy extent and we are trying to
allocate a new extent to fix up the free list, we can deadlock if
the AG is entirely empty except for the busy extent held by the
transaction.
This can occur at runtime processing an XEFI with multiple extents
in this path:
__schedule+0x22f at ffffffff81f75e8f
schedule+0x46 at ffffffff81f76366
xfs_extent_busy_flush+0x69 at ffffffff81477d99
xfs_alloc_ag_vextent_size+0x16a at ffffffff8141711a
xfs_alloc_ag_vextent+0x19b at ffffffff81417edb
xfs_alloc_fix_freelist+0x22f at ffffffff8141896f
xfs_free_extent_fix_freelist+0x6a at ffffffff8141939a
__xfs_free_extent+0x99 at ffffffff81419499
xfs_trans_free_extent+0x3e at ffffffff814a6fee
xfs_extent_free_finish_item+0x24 at ffffffff814a70d4
xfs_defer_finish_noroll+0x1f7 at ffffffff81441407
xfs_defer_finish+0x11 at ffffffff814417e1
xfs_itruncate_extents_flags+0x13d at ffffffff8148b7dd
xfs_inactive_truncate+0xb9 at ffffffff8148bb89
xfs_inactive+0x227 at ffffffff8148c4f7
xfs_fs_destroy_inode+0xb8 at ffffffff81496898
destroy_inode+0x3b at ffffffff8127d2ab
do_unlinkat+0x1d1 at ffffffff81270df1
do_syscall_64+0x40 at ffffffff81f6b5f0
entry_SYSCALL_64_after_hwframe+0x44 at ffffffff8200007c
This can also happen in log recovery when processing an EFI
with multiple extents through this path:
context_switch() kernel/sched/core.c:3881
__schedule() kernel/sched/core.c:5111
schedule() kernel/sched/core.c:5186
xfs_extent_busy_flush() fs/xfs/xfs_extent_busy.c:598
xfs_alloc_ag_vextent_size() fs/xfs/libxfs/xfs_alloc.c:1641
xfs_alloc_ag_vextent() fs/xfs/libxfs/xfs_alloc.c:828
xfs_alloc_fix_freelist() fs/xfs/libxfs/xfs_alloc.c:2362
xfs_free_extent_fix_freelist() fs/xfs/libxfs/xfs_alloc.c:3029
__xfs_free_extent() fs/xfs/libxfs/xfs_alloc.c:3067
xfs_trans_free_extent() fs/xfs/xfs_extfree_item.c:370
xfs_efi_recover() fs/xfs/xfs_extfree_item.c:626
xlog_recover_process_efi() fs/xfs/xfs_log_recover.c:4605
xlog_recover_process_intents() fs/xfs/xfs_log_recover.c:4893
xlog_recover_finish() fs/xfs/xfs_log_recover.c:5824
xfs_log_mount_finish() fs/xfs/xfs_log.c:764
xfs_mountfs() fs/xfs/xfs_mount.c:978
xfs_fs_fill_super() fs/xfs/xfs_super.c:1908
mount_bdev() fs/super.c:1417
xfs_fs_mount() fs/xfs/xfs_super.c:1985
legacy_get_tree() fs/fs_context.c:647
vfs_get_tree() fs/super.c:1547
do_new_mount() fs/namespace.c:2843
do_mount() fs/namespace.c:3163
ksys_mount() fs/namespace.c:3372
__do_sys_mount() fs/namespace.c:3386
__se_sys_mount() fs/namespace.c:3383
__x64_sys_mount() fs/namespace.c:3383
do_syscall_64() arch/x86/entry/common.c:296
entry_SYSCALL_64() arch/x86/entry/entry_64.S:180
To avoid this deadlock, we should not block in
xfs_extent_busy_flush() if we hold a busy extent in the current
transaction.
Now that the EFI processing code can handle requeuing a partially
completed EFI, we can detect this situation in
xfs_extent_busy_flush() and return -EAGAIN rather than going to
sleep forever. The -EAGAIN get propagated back out to the
xfs_trans_free_extent() context, where the EFD is populated and the
transaction is rolled, thereby moving the busy extents into the CIL.
At this point, we can retry the extent free operation again with a
clean transaction. If we hit the same "all free extents are busy"
situation when trying to fix up the free list, we can safely call
xfs_extent_busy_flush() and wait for the busy extents to resolve
and wake us. At this point, the allocation search can make progress
again and we can fix up the free list.
This deadlock was first reported by Chandan in mid-2021, but I
couldn't make myself understood during review, and didn't have time
to fix it myself.
It was reported again in March 2023, and again I have found myself
unable to explain the complexities of the solution needed during
review.
As such, I don't have hours more time to waste trying to get the
fix written the way it needs to be written, so I'm just doing it
myself. This patchset is largely based on Wengang Wang's last patch,
but with all the unnecessary stuff removed, split up into multiple
patches and cleaned up somewhat.
Reported-by: Chandan Babu R <chandanrlinux@gmail.com>
Reported-by: Wengang Wang <wen.gang.wang@oracle.com>
Signed-off-by: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Darrick J. Wong <djwong@kernel.org>
Signed-off-by: Darrick J. Wong <djwong@kernel.org>
2023-06-29 02:04:33 +08:00
|
|
|
struct xfs_trans *tp,
|
2017-02-08 06:06:57 +08:00
|
|
|
struct xfs_perag *pag,
|
2023-06-29 02:04:32 +08:00
|
|
|
unsigned busy_gen,
|
|
|
|
uint32_t alloc_flags)
|
2017-02-08 06:06:57 +08:00
|
|
|
{
|
|
|
|
DEFINE_WAIT (wait);
|
2018-03-14 14:15:27 +08:00
|
|
|
int error;
|
2017-02-08 06:06:57 +08:00
|
|
|
|
xfs: don't block in busy flushing when freeing extents
If the current transaction holds a busy extent and we are trying to
allocate a new extent to fix up the free list, we can deadlock if
the AG is entirely empty except for the busy extent held by the
transaction.
This can occur at runtime processing an XEFI with multiple extents
in this path:
__schedule+0x22f at ffffffff81f75e8f
schedule+0x46 at ffffffff81f76366
xfs_extent_busy_flush+0x69 at ffffffff81477d99
xfs_alloc_ag_vextent_size+0x16a at ffffffff8141711a
xfs_alloc_ag_vextent+0x19b at ffffffff81417edb
xfs_alloc_fix_freelist+0x22f at ffffffff8141896f
xfs_free_extent_fix_freelist+0x6a at ffffffff8141939a
__xfs_free_extent+0x99 at ffffffff81419499
xfs_trans_free_extent+0x3e at ffffffff814a6fee
xfs_extent_free_finish_item+0x24 at ffffffff814a70d4
xfs_defer_finish_noroll+0x1f7 at ffffffff81441407
xfs_defer_finish+0x11 at ffffffff814417e1
xfs_itruncate_extents_flags+0x13d at ffffffff8148b7dd
xfs_inactive_truncate+0xb9 at ffffffff8148bb89
xfs_inactive+0x227 at ffffffff8148c4f7
xfs_fs_destroy_inode+0xb8 at ffffffff81496898
destroy_inode+0x3b at ffffffff8127d2ab
do_unlinkat+0x1d1 at ffffffff81270df1
do_syscall_64+0x40 at ffffffff81f6b5f0
entry_SYSCALL_64_after_hwframe+0x44 at ffffffff8200007c
This can also happen in log recovery when processing an EFI
with multiple extents through this path:
context_switch() kernel/sched/core.c:3881
__schedule() kernel/sched/core.c:5111
schedule() kernel/sched/core.c:5186
xfs_extent_busy_flush() fs/xfs/xfs_extent_busy.c:598
xfs_alloc_ag_vextent_size() fs/xfs/libxfs/xfs_alloc.c:1641
xfs_alloc_ag_vextent() fs/xfs/libxfs/xfs_alloc.c:828
xfs_alloc_fix_freelist() fs/xfs/libxfs/xfs_alloc.c:2362
xfs_free_extent_fix_freelist() fs/xfs/libxfs/xfs_alloc.c:3029
__xfs_free_extent() fs/xfs/libxfs/xfs_alloc.c:3067
xfs_trans_free_extent() fs/xfs/xfs_extfree_item.c:370
xfs_efi_recover() fs/xfs/xfs_extfree_item.c:626
xlog_recover_process_efi() fs/xfs/xfs_log_recover.c:4605
xlog_recover_process_intents() fs/xfs/xfs_log_recover.c:4893
xlog_recover_finish() fs/xfs/xfs_log_recover.c:5824
xfs_log_mount_finish() fs/xfs/xfs_log.c:764
xfs_mountfs() fs/xfs/xfs_mount.c:978
xfs_fs_fill_super() fs/xfs/xfs_super.c:1908
mount_bdev() fs/super.c:1417
xfs_fs_mount() fs/xfs/xfs_super.c:1985
legacy_get_tree() fs/fs_context.c:647
vfs_get_tree() fs/super.c:1547
do_new_mount() fs/namespace.c:2843
do_mount() fs/namespace.c:3163
ksys_mount() fs/namespace.c:3372
__do_sys_mount() fs/namespace.c:3386
__se_sys_mount() fs/namespace.c:3383
__x64_sys_mount() fs/namespace.c:3383
do_syscall_64() arch/x86/entry/common.c:296
entry_SYSCALL_64() arch/x86/entry/entry_64.S:180
To avoid this deadlock, we should not block in
xfs_extent_busy_flush() if we hold a busy extent in the current
transaction.
Now that the EFI processing code can handle requeuing a partially
completed EFI, we can detect this situation in
xfs_extent_busy_flush() and return -EAGAIN rather than going to
sleep forever. The -EAGAIN get propagated back out to the
xfs_trans_free_extent() context, where the EFD is populated and the
transaction is rolled, thereby moving the busy extents into the CIL.
At this point, we can retry the extent free operation again with a
clean transaction. If we hit the same "all free extents are busy"
situation when trying to fix up the free list, we can safely call
xfs_extent_busy_flush() and wait for the busy extents to resolve
and wake us. At this point, the allocation search can make progress
again and we can fix up the free list.
This deadlock was first reported by Chandan in mid-2021, but I
couldn't make myself understood during review, and didn't have time
to fix it myself.
It was reported again in March 2023, and again I have found myself
unable to explain the complexities of the solution needed during
review.
As such, I don't have hours more time to waste trying to get the
fix written the way it needs to be written, so I'm just doing it
myself. This patchset is largely based on Wengang Wang's last patch,
but with all the unnecessary stuff removed, split up into multiple
patches and cleaned up somewhat.
Reported-by: Chandan Babu R <chandanrlinux@gmail.com>
Reported-by: Wengang Wang <wen.gang.wang@oracle.com>
Signed-off-by: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Darrick J. Wong <djwong@kernel.org>
Signed-off-by: Darrick J. Wong <djwong@kernel.org>
2023-06-29 02:04:33 +08:00
|
|
|
error = xfs_log_force(tp->t_mountp, XFS_LOG_SYNC);
|
2017-02-08 06:06:57 +08:00
|
|
|
if (error)
|
xfs: don't block in busy flushing when freeing extents
If the current transaction holds a busy extent and we are trying to
allocate a new extent to fix up the free list, we can deadlock if
the AG is entirely empty except for the busy extent held by the
transaction.
This can occur at runtime processing an XEFI with multiple extents
in this path:
__schedule+0x22f at ffffffff81f75e8f
schedule+0x46 at ffffffff81f76366
xfs_extent_busy_flush+0x69 at ffffffff81477d99
xfs_alloc_ag_vextent_size+0x16a at ffffffff8141711a
xfs_alloc_ag_vextent+0x19b at ffffffff81417edb
xfs_alloc_fix_freelist+0x22f at ffffffff8141896f
xfs_free_extent_fix_freelist+0x6a at ffffffff8141939a
__xfs_free_extent+0x99 at ffffffff81419499
xfs_trans_free_extent+0x3e at ffffffff814a6fee
xfs_extent_free_finish_item+0x24 at ffffffff814a70d4
xfs_defer_finish_noroll+0x1f7 at ffffffff81441407
xfs_defer_finish+0x11 at ffffffff814417e1
xfs_itruncate_extents_flags+0x13d at ffffffff8148b7dd
xfs_inactive_truncate+0xb9 at ffffffff8148bb89
xfs_inactive+0x227 at ffffffff8148c4f7
xfs_fs_destroy_inode+0xb8 at ffffffff81496898
destroy_inode+0x3b at ffffffff8127d2ab
do_unlinkat+0x1d1 at ffffffff81270df1
do_syscall_64+0x40 at ffffffff81f6b5f0
entry_SYSCALL_64_after_hwframe+0x44 at ffffffff8200007c
This can also happen in log recovery when processing an EFI
with multiple extents through this path:
context_switch() kernel/sched/core.c:3881
__schedule() kernel/sched/core.c:5111
schedule() kernel/sched/core.c:5186
xfs_extent_busy_flush() fs/xfs/xfs_extent_busy.c:598
xfs_alloc_ag_vextent_size() fs/xfs/libxfs/xfs_alloc.c:1641
xfs_alloc_ag_vextent() fs/xfs/libxfs/xfs_alloc.c:828
xfs_alloc_fix_freelist() fs/xfs/libxfs/xfs_alloc.c:2362
xfs_free_extent_fix_freelist() fs/xfs/libxfs/xfs_alloc.c:3029
__xfs_free_extent() fs/xfs/libxfs/xfs_alloc.c:3067
xfs_trans_free_extent() fs/xfs/xfs_extfree_item.c:370
xfs_efi_recover() fs/xfs/xfs_extfree_item.c:626
xlog_recover_process_efi() fs/xfs/xfs_log_recover.c:4605
xlog_recover_process_intents() fs/xfs/xfs_log_recover.c:4893
xlog_recover_finish() fs/xfs/xfs_log_recover.c:5824
xfs_log_mount_finish() fs/xfs/xfs_log.c:764
xfs_mountfs() fs/xfs/xfs_mount.c:978
xfs_fs_fill_super() fs/xfs/xfs_super.c:1908
mount_bdev() fs/super.c:1417
xfs_fs_mount() fs/xfs/xfs_super.c:1985
legacy_get_tree() fs/fs_context.c:647
vfs_get_tree() fs/super.c:1547
do_new_mount() fs/namespace.c:2843
do_mount() fs/namespace.c:3163
ksys_mount() fs/namespace.c:3372
__do_sys_mount() fs/namespace.c:3386
__se_sys_mount() fs/namespace.c:3383
__x64_sys_mount() fs/namespace.c:3383
do_syscall_64() arch/x86/entry/common.c:296
entry_SYSCALL_64() arch/x86/entry/entry_64.S:180
To avoid this deadlock, we should not block in
xfs_extent_busy_flush() if we hold a busy extent in the current
transaction.
Now that the EFI processing code can handle requeuing a partially
completed EFI, we can detect this situation in
xfs_extent_busy_flush() and return -EAGAIN rather than going to
sleep forever. The -EAGAIN get propagated back out to the
xfs_trans_free_extent() context, where the EFD is populated and the
transaction is rolled, thereby moving the busy extents into the CIL.
At this point, we can retry the extent free operation again with a
clean transaction. If we hit the same "all free extents are busy"
situation when trying to fix up the free list, we can safely call
xfs_extent_busy_flush() and wait for the busy extents to resolve
and wake us. At this point, the allocation search can make progress
again and we can fix up the free list.
This deadlock was first reported by Chandan in mid-2021, but I
couldn't make myself understood during review, and didn't have time
to fix it myself.
It was reported again in March 2023, and again I have found myself
unable to explain the complexities of the solution needed during
review.
As such, I don't have hours more time to waste trying to get the
fix written the way it needs to be written, so I'm just doing it
myself. This patchset is largely based on Wengang Wang's last patch,
but with all the unnecessary stuff removed, split up into multiple
patches and cleaned up somewhat.
Reported-by: Chandan Babu R <chandanrlinux@gmail.com>
Reported-by: Wengang Wang <wen.gang.wang@oracle.com>
Signed-off-by: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Darrick J. Wong <djwong@kernel.org>
Signed-off-by: Darrick J. Wong <djwong@kernel.org>
2023-06-29 02:04:33 +08:00
|
|
|
return error;
|
|
|
|
|
|
|
|
/* Avoid deadlocks on uncommitted busy extents. */
|
|
|
|
if (!list_empty(&tp->t_busy)) {
|
|
|
|
if (alloc_flags & XFS_ALLOC_FLAG_TRYFLUSH)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (busy_gen != READ_ONCE(pag->pagb_gen))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (alloc_flags & XFS_ALLOC_FLAG_FREEING)
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
2017-02-08 06:06:57 +08:00
|
|
|
|
xfs: don't block in busy flushing when freeing extents
If the current transaction holds a busy extent and we are trying to
allocate a new extent to fix up the free list, we can deadlock if
the AG is entirely empty except for the busy extent held by the
transaction.
This can occur at runtime processing an XEFI with multiple extents
in this path:
__schedule+0x22f at ffffffff81f75e8f
schedule+0x46 at ffffffff81f76366
xfs_extent_busy_flush+0x69 at ffffffff81477d99
xfs_alloc_ag_vextent_size+0x16a at ffffffff8141711a
xfs_alloc_ag_vextent+0x19b at ffffffff81417edb
xfs_alloc_fix_freelist+0x22f at ffffffff8141896f
xfs_free_extent_fix_freelist+0x6a at ffffffff8141939a
__xfs_free_extent+0x99 at ffffffff81419499
xfs_trans_free_extent+0x3e at ffffffff814a6fee
xfs_extent_free_finish_item+0x24 at ffffffff814a70d4
xfs_defer_finish_noroll+0x1f7 at ffffffff81441407
xfs_defer_finish+0x11 at ffffffff814417e1
xfs_itruncate_extents_flags+0x13d at ffffffff8148b7dd
xfs_inactive_truncate+0xb9 at ffffffff8148bb89
xfs_inactive+0x227 at ffffffff8148c4f7
xfs_fs_destroy_inode+0xb8 at ffffffff81496898
destroy_inode+0x3b at ffffffff8127d2ab
do_unlinkat+0x1d1 at ffffffff81270df1
do_syscall_64+0x40 at ffffffff81f6b5f0
entry_SYSCALL_64_after_hwframe+0x44 at ffffffff8200007c
This can also happen in log recovery when processing an EFI
with multiple extents through this path:
context_switch() kernel/sched/core.c:3881
__schedule() kernel/sched/core.c:5111
schedule() kernel/sched/core.c:5186
xfs_extent_busy_flush() fs/xfs/xfs_extent_busy.c:598
xfs_alloc_ag_vextent_size() fs/xfs/libxfs/xfs_alloc.c:1641
xfs_alloc_ag_vextent() fs/xfs/libxfs/xfs_alloc.c:828
xfs_alloc_fix_freelist() fs/xfs/libxfs/xfs_alloc.c:2362
xfs_free_extent_fix_freelist() fs/xfs/libxfs/xfs_alloc.c:3029
__xfs_free_extent() fs/xfs/libxfs/xfs_alloc.c:3067
xfs_trans_free_extent() fs/xfs/xfs_extfree_item.c:370
xfs_efi_recover() fs/xfs/xfs_extfree_item.c:626
xlog_recover_process_efi() fs/xfs/xfs_log_recover.c:4605
xlog_recover_process_intents() fs/xfs/xfs_log_recover.c:4893
xlog_recover_finish() fs/xfs/xfs_log_recover.c:5824
xfs_log_mount_finish() fs/xfs/xfs_log.c:764
xfs_mountfs() fs/xfs/xfs_mount.c:978
xfs_fs_fill_super() fs/xfs/xfs_super.c:1908
mount_bdev() fs/super.c:1417
xfs_fs_mount() fs/xfs/xfs_super.c:1985
legacy_get_tree() fs/fs_context.c:647
vfs_get_tree() fs/super.c:1547
do_new_mount() fs/namespace.c:2843
do_mount() fs/namespace.c:3163
ksys_mount() fs/namespace.c:3372
__do_sys_mount() fs/namespace.c:3386
__se_sys_mount() fs/namespace.c:3383
__x64_sys_mount() fs/namespace.c:3383
do_syscall_64() arch/x86/entry/common.c:296
entry_SYSCALL_64() arch/x86/entry/entry_64.S:180
To avoid this deadlock, we should not block in
xfs_extent_busy_flush() if we hold a busy extent in the current
transaction.
Now that the EFI processing code can handle requeuing a partially
completed EFI, we can detect this situation in
xfs_extent_busy_flush() and return -EAGAIN rather than going to
sleep forever. The -EAGAIN get propagated back out to the
xfs_trans_free_extent() context, where the EFD is populated and the
transaction is rolled, thereby moving the busy extents into the CIL.
At this point, we can retry the extent free operation again with a
clean transaction. If we hit the same "all free extents are busy"
situation when trying to fix up the free list, we can safely call
xfs_extent_busy_flush() and wait for the busy extents to resolve
and wake us. At this point, the allocation search can make progress
again and we can fix up the free list.
This deadlock was first reported by Chandan in mid-2021, but I
couldn't make myself understood during review, and didn't have time
to fix it myself.
It was reported again in March 2023, and again I have found myself
unable to explain the complexities of the solution needed during
review.
As such, I don't have hours more time to waste trying to get the
fix written the way it needs to be written, so I'm just doing it
myself. This patchset is largely based on Wengang Wang's last patch,
but with all the unnecessary stuff removed, split up into multiple
patches and cleaned up somewhat.
Reported-by: Chandan Babu R <chandanrlinux@gmail.com>
Reported-by: Wengang Wang <wen.gang.wang@oracle.com>
Signed-off-by: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Darrick J. Wong <djwong@kernel.org>
Signed-off-by: Darrick J. Wong <djwong@kernel.org>
2023-06-29 02:04:33 +08:00
|
|
|
/* Wait for committed busy extents to resolve. */
|
2017-02-08 06:06:57 +08:00
|
|
|
do {
|
|
|
|
prepare_to_wait(&pag->pagb_wait, &wait, TASK_KILLABLE);
|
|
|
|
if (busy_gen != READ_ONCE(pag->pagb_gen))
|
|
|
|
break;
|
|
|
|
schedule();
|
|
|
|
} while (1);
|
|
|
|
|
|
|
|
finish_wait(&pag->pagb_wait, &wait);
|
xfs: don't block in busy flushing when freeing extents
If the current transaction holds a busy extent and we are trying to
allocate a new extent to fix up the free list, we can deadlock if
the AG is entirely empty except for the busy extent held by the
transaction.
This can occur at runtime processing an XEFI with multiple extents
in this path:
__schedule+0x22f at ffffffff81f75e8f
schedule+0x46 at ffffffff81f76366
xfs_extent_busy_flush+0x69 at ffffffff81477d99
xfs_alloc_ag_vextent_size+0x16a at ffffffff8141711a
xfs_alloc_ag_vextent+0x19b at ffffffff81417edb
xfs_alloc_fix_freelist+0x22f at ffffffff8141896f
xfs_free_extent_fix_freelist+0x6a at ffffffff8141939a
__xfs_free_extent+0x99 at ffffffff81419499
xfs_trans_free_extent+0x3e at ffffffff814a6fee
xfs_extent_free_finish_item+0x24 at ffffffff814a70d4
xfs_defer_finish_noroll+0x1f7 at ffffffff81441407
xfs_defer_finish+0x11 at ffffffff814417e1
xfs_itruncate_extents_flags+0x13d at ffffffff8148b7dd
xfs_inactive_truncate+0xb9 at ffffffff8148bb89
xfs_inactive+0x227 at ffffffff8148c4f7
xfs_fs_destroy_inode+0xb8 at ffffffff81496898
destroy_inode+0x3b at ffffffff8127d2ab
do_unlinkat+0x1d1 at ffffffff81270df1
do_syscall_64+0x40 at ffffffff81f6b5f0
entry_SYSCALL_64_after_hwframe+0x44 at ffffffff8200007c
This can also happen in log recovery when processing an EFI
with multiple extents through this path:
context_switch() kernel/sched/core.c:3881
__schedule() kernel/sched/core.c:5111
schedule() kernel/sched/core.c:5186
xfs_extent_busy_flush() fs/xfs/xfs_extent_busy.c:598
xfs_alloc_ag_vextent_size() fs/xfs/libxfs/xfs_alloc.c:1641
xfs_alloc_ag_vextent() fs/xfs/libxfs/xfs_alloc.c:828
xfs_alloc_fix_freelist() fs/xfs/libxfs/xfs_alloc.c:2362
xfs_free_extent_fix_freelist() fs/xfs/libxfs/xfs_alloc.c:3029
__xfs_free_extent() fs/xfs/libxfs/xfs_alloc.c:3067
xfs_trans_free_extent() fs/xfs/xfs_extfree_item.c:370
xfs_efi_recover() fs/xfs/xfs_extfree_item.c:626
xlog_recover_process_efi() fs/xfs/xfs_log_recover.c:4605
xlog_recover_process_intents() fs/xfs/xfs_log_recover.c:4893
xlog_recover_finish() fs/xfs/xfs_log_recover.c:5824
xfs_log_mount_finish() fs/xfs/xfs_log.c:764
xfs_mountfs() fs/xfs/xfs_mount.c:978
xfs_fs_fill_super() fs/xfs/xfs_super.c:1908
mount_bdev() fs/super.c:1417
xfs_fs_mount() fs/xfs/xfs_super.c:1985
legacy_get_tree() fs/fs_context.c:647
vfs_get_tree() fs/super.c:1547
do_new_mount() fs/namespace.c:2843
do_mount() fs/namespace.c:3163
ksys_mount() fs/namespace.c:3372
__do_sys_mount() fs/namespace.c:3386
__se_sys_mount() fs/namespace.c:3383
__x64_sys_mount() fs/namespace.c:3383
do_syscall_64() arch/x86/entry/common.c:296
entry_SYSCALL_64() arch/x86/entry/entry_64.S:180
To avoid this deadlock, we should not block in
xfs_extent_busy_flush() if we hold a busy extent in the current
transaction.
Now that the EFI processing code can handle requeuing a partially
completed EFI, we can detect this situation in
xfs_extent_busy_flush() and return -EAGAIN rather than going to
sleep forever. The -EAGAIN get propagated back out to the
xfs_trans_free_extent() context, where the EFD is populated and the
transaction is rolled, thereby moving the busy extents into the CIL.
At this point, we can retry the extent free operation again with a
clean transaction. If we hit the same "all free extents are busy"
situation when trying to fix up the free list, we can safely call
xfs_extent_busy_flush() and wait for the busy extents to resolve
and wake us. At this point, the allocation search can make progress
again and we can fix up the free list.
This deadlock was first reported by Chandan in mid-2021, but I
couldn't make myself understood during review, and didn't have time
to fix it myself.
It was reported again in March 2023, and again I have found myself
unable to explain the complexities of the solution needed during
review.
As such, I don't have hours more time to waste trying to get the
fix written the way it needs to be written, so I'm just doing it
myself. This patchset is largely based on Wengang Wang's last patch,
but with all the unnecessary stuff removed, split up into multiple
patches and cleaned up somewhat.
Reported-by: Chandan Babu R <chandanrlinux@gmail.com>
Reported-by: Wengang Wang <wen.gang.wang@oracle.com>
Signed-off-by: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Darrick J. Wong <djwong@kernel.org>
Signed-off-by: Darrick J. Wong <djwong@kernel.org>
2023-06-29 02:04:33 +08:00
|
|
|
return 0;
|
2017-02-08 06:06:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
xfs_extent_busy_wait_all(
|
|
|
|
struct xfs_mount *mp)
|
|
|
|
{
|
2021-06-02 08:48:24 +08:00
|
|
|
struct xfs_perag *pag;
|
2017-02-08 06:06:57 +08:00
|
|
|
DEFINE_WAIT (wait);
|
|
|
|
xfs_agnumber_t agno;
|
|
|
|
|
2021-06-02 08:48:24 +08:00
|
|
|
for_each_perag(mp, agno, pag) {
|
2017-02-08 06:06:57 +08:00
|
|
|
do {
|
|
|
|
prepare_to_wait(&pag->pagb_wait, &wait, TASK_KILLABLE);
|
|
|
|
if (RB_EMPTY_ROOT(&pag->pagb_tree))
|
|
|
|
break;
|
|
|
|
schedule();
|
|
|
|
} while (1);
|
|
|
|
finish_wait(&pag->pagb_wait, &wait);
|
2012-04-29 18:39:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Callback for list_sort to sort busy extents by the AG they reside in.
|
|
|
|
*/
|
|
|
|
int
|
2012-04-29 18:41:10 +08:00
|
|
|
xfs_extent_busy_ag_cmp(
|
2012-04-29 18:39:43 +08:00
|
|
|
void *priv,
|
2021-04-09 02:28:34 +08:00
|
|
|
const struct list_head *l1,
|
|
|
|
const struct list_head *l2)
|
2012-04-29 18:39:43 +08:00
|
|
|
{
|
2017-02-08 06:07:57 +08:00
|
|
|
struct xfs_extent_busy *b1 =
|
|
|
|
container_of(l1, struct xfs_extent_busy, list);
|
|
|
|
struct xfs_extent_busy *b2 =
|
|
|
|
container_of(l2, struct xfs_extent_busy, list);
|
|
|
|
s32 diff;
|
|
|
|
|
|
|
|
diff = b1->agno - b2->agno;
|
|
|
|
if (!diff)
|
|
|
|
diff = b1->bno - b2->bno;
|
|
|
|
return diff;
|
2012-04-29 18:39:43 +08:00
|
|
|
}
|