[RFC v6 PATCH 04/10] xfs: relog log reservation stealing and accounting

Brian Foster
2020-04-06T12:36:41Z

The transaction that eventually commits relog enabled log items
requires log reservation like any other transaction. It is not safe
to acquire reservation on-demand because relogged items aren't
processed until they are likely at the tail of the log and require
movement in order to free up space in the log. As such, a relog
transaction that blocks on log reservation is a likely deadlock
vector.

To address this problem, implement a model where relog reservation
is contributed by the transaction that enables relogging on a
particular item. Update the relog helper to transfer reservation
from the transaction to the relog pool. The relog pool holds
outstanding reservation such that it can be used to commit the item
in an otherwise empty transaction. The upcoming relog mechanism is
responsible to replenish the relog reservation as items are
relogged. When relog is cancelled on a log item, transfer the
outstanding relog reservation to the current transaction (if
provided) for eventual release or otherwise release it directly to
the grant heads.

Note that this approach has several caveats:

- Log reservation calculations for transactions that relog items
must be increased accordingly.
- Each current transaction reservation includes extra space for
various overheads, such as for the CIL ticket, etc. Since items
can be relogged in arbitrary combinations, this reservation
overhead must be included for each relogged item.
- Relog reservation must be based on the worst case log requirement
for the lifetime of an item. This is not a concern for fixed size
log items, such as most intents. More granular log items like
buffers can have additional ranges dirtied after relogging has
been enabled for the item and the relog subsystem must have
enough reservation to accommodate.

Signed-off-by: Brian Foster <bfoster@redhat.com>
---
fs/xfs/xfs_log.c | 3 +++
fs/xfs/xfs_trans.c | 20 ++++++++++++++++++++
fs/xfs/xfs_trans.h | 17 +++++++++++++++++
fs/xfs/xfs_trans_ail.c | 2 ++
fs/xfs/xfs_trans_priv.h | 14 ++++++++++++++
5 files changed, 56 insertions(+)

diff --git a/fs/xfs/xfs_log.c b/fs/xfs/xfs_log.c
index b55abde6c142..940e5bb9786c 100644
--- a/fs/xfs/xfs_log.c
+++ b/fs/xfs/xfs_log.c
@@ -983,6 +983,9 @@ xfs_log_item_init(
item->li_type = type;
item->li_ops = ops;
item->li_lv = NULL;
+#ifdef DEBUG
+ atomic64_set(&item->li_relog_res, 0);
+#endif

INIT_LIST_HEAD(&item->li_ail);
INIT_LIST_HEAD(&item->li_cil);
diff --git a/fs/xfs/xfs_trans.c b/fs/xfs/xfs_trans.c
index 1b25980315bd..6fc81c3b8500 100644
--- a/fs/xfs/xfs_trans.c
+++ b/fs/xfs/xfs_trans.c
@@ -20,6 +20,7 @@
#include "xfs_trace.h"
#include "xfs_error.h"
#include "xfs_defer.h"
+#include "xfs_log_priv.h"

kmem_zone_t *xfs_trans_zone;

@@ -776,9 +777,19 @@ xfs_trans_relog_item(
struct xfs_trans *tp,
struct xfs_log_item *lip)
{
+ int nbytes;
+
+ ASSERT(tp->t_flags & XFS_TRANS_RELOG);
+
if (test_and_set_bit(XFS_LI_RELOG, &lip->li_flags))
return;
trace_xfs_relog_item(lip);
+
+ nbytes = xfs_relog_calc_res(lip);
+
+ tp->t_ticket->t_curr_res -= nbytes;
+ xfs_relog_res_account(lip, nbytes);
+ tp->t_flags |= XFS_TRANS_DIRTY;
}

void
@@ -786,9 +797,18 @@ xfs_trans_relog_item_cancel(
struct xfs_trans *tp,
struct xfs_log_item *lip)
{
+ int res;
+
if (!test_and_clear_bit(XFS_LI_RELOG, &lip->li_flags))
return;
trace_xfs_relog_item_cancel(lip);
+
+ res = xfs_relog_calc_res(lip);
+ if (tp)
+ tp->t_ticket->t_curr_res += res;
+ else
+ xfs_log_ungrant_bytes(lip->li_mountp, res);
+ xfs_relog_res_account(lip, -res);
}

/* Detach and unlock all of the items in a transaction */
diff --git a/fs/xfs/xfs_trans.h b/fs/xfs/xfs_trans.h
index 9ea0f415e5ca..b4bb2a6c5251 100644
--- a/fs/xfs/xfs_trans.h
+++ b/fs/xfs/xfs_trans.h
@@ -48,6 +48,9 @@ struct xfs_log_item {
struct xfs_log_vec *li_lv; /* active log vector */
struct xfs_log_vec *li_lv_shadow; /* standby vector */
xfs_lsn_t li_seq; /* CIL commit seq */
+#ifdef DEBUG
+ atomic64_t li_relog_res; /* automatic relog log res */
+#endif
};

/*
@@ -212,6 +215,20 @@ xfs_trans_read_buf(
flags, bpp, ops);
}

+static inline int
+xfs_relog_calc_res(
+ struct xfs_log_item *lip)
+{
+ int niovecs = 0;
+ int nbytes = 0;
+
+ lip->li_ops->iop_size(lip, &niovecs, &nbytes);
+ ASSERT(niovecs == 1);
+ nbytes = xfs_log_calc_unit_res(lip->li_mountp, nbytes);
+
+ return nbytes;
+}
+
struct xfs_buf *xfs_trans_getsb(xfs_trans_t *, struct xfs_mount *);

void xfs_trans_brelse(xfs_trans_t *, struct xfs_buf *);
diff --git a/fs/xfs/xfs_trans_ail.c b/fs/xfs/xfs_trans_ail.c
index 564253550b75..0b4b1c3cc4de 100644
--- a/fs/xfs/xfs_trans_ail.c
+++ b/fs/xfs/xfs_trans_ail.c
@@ -861,6 +861,7 @@ xfs_trans_ail_init(
spin_lock_init(&ailp->ail_lock);
INIT_LIST_HEAD(&ailp->ail_buf_list);
init_waitqueue_head(&ailp->ail_empty);
+ atomic64_set(&ailp->ail_relog_res, 0);

ailp->ail_task = kthread_run(xfsaild, ailp, "xfsaild/%s",
ailp->ail_mount->m_super->s_id);
@@ -881,6 +882,7 @@ xfs_trans_ail_destroy(
{
struct xfs_ail *ailp = mp->m_ail;

+ ASSERT(atomic64_read(&ailp->ail_relog_res) == 0);
kthread_stop(ailp->ail_task);
kmem_free(ailp);
}
diff --git a/fs/xfs/xfs_trans_priv.h b/fs/xfs/xfs_trans_priv.h
index c890794314a6..926bafdf6cd7 100644
--- a/fs/xfs/xfs_trans_priv.h
+++ b/fs/xfs/xfs_trans_priv.h
@@ -63,6 +63,7 @@ struct xfs_ail {
int ail_log_flush;
struct list_head ail_buf_list;
wait_queue_head_t ail_empty;
+ atomic64_t ail_relog_res;
};

/*
@@ -182,4 +183,17 @@ xfs_set_li_failed(
}
}

+static inline int64_t
+xfs_relog_res_account(
+ struct xfs_log_item *lip,
+ int64_t bytes)
+{
+#ifdef DEBUG
+ int64_t res;
+
+ res = atomic64_add_return(bytes, &lip->li_relog_res);
+ ASSERT(res == bytes || (bytes < 0 && res == 0));
+#endif
+ return atomic64_add_return(bytes, &lip->li_ailp->ail_relog_res);
+}
#endif /* __XFS_TRANS_PRIV_H__ */
--
2.21.1