[RFC PATCH 1/2] xfs: add lockref and generic helpers for refcounting

From: Jeffin Philip

Date: Fri Sep 11 2026 - 06:34:29 EST


As part of fixing the UAF in xlog_cil_ail_insert() reported by
syzbot, add a generic lockref to xfs_log_item struct and
initialize it in xfs_log_item_init(). In addition, add generic
helpers(get()/put()/get_safe()) as part of the generic refcounting
infrastructure for xfs.

Signed-off-by: Jeffin Philip <jeffinphilip14@xxxxxxxxx>
---
fs/xfs/xfs_log.c | 38 ++++++++++++++++++++++++++++++++++++++
fs/xfs/xfs_trans.h | 7 +++++++
2 files changed, 45 insertions(+)

diff --git a/fs/xfs/xfs_log.c b/fs/xfs/xfs_log.c
index f807f8f4f705..1489f8f20b3e 100644
--- a/fs/xfs/xfs_log.c
+++ b/fs/xfs/xfs_log.c
@@ -1033,12 +1033,50 @@ xfs_log_item_init(
item->li_ops = ops;
item->li_lv = NULL;

+ /*
+ * Refrain from using lockref_init as BLI refcount should be
+ * initialized to 0 and lockref_init initializes refcount to 1
+ */
+ spin_lock_init(&item->li_ref.lock);
+ item->li_ref.count = 0;
INIT_LIST_HEAD(&item->li_ail);
INIT_LIST_HEAD(&item->li_cil);
INIT_LIST_HEAD(&item->li_bio_list);
INIT_LIST_HEAD(&item->li_trans);
}

+/*
+ * Only called when the caller knows the object is alive
+ */
+void
+xfs_log_item_get(
+ struct xfs_log_item *lip)
+{
+ lockref_get(&lip->li_ref);
+}
+
+/*
+ * Drop a log item reference when called. Returns true if last
+ * ref with lock held. Otherwise false.
+ */
+bool
+xfs_log_item_put(
+ struct xfs_log_item *lip)
+{
+ return lockref_put_or_lock(&lip->li_ref);
+}
+
+/*
+ * Used to lookup if item may be dying. Returns true is the object
+ * is not dead, false otherwise.
+ */
+bool
+xfs_log_item_get_safe(
+ struct xfs_log_item *lip)
+{
+ return lockref_get_not_dead(&lip->li_ref);
+}
+
/*
* Wake up processes waiting for log space after we have moved the log tail.
*/
diff --git a/fs/xfs/xfs_trans.h b/fs/xfs/xfs_trans.h
index eb83c5dac032..cd469e2e4e4d 100644
--- a/fs/xfs/xfs_trans.h
+++ b/fs/xfs/xfs_trans.h
@@ -6,6 +6,8 @@
#ifndef __XFS_TRANS_H__
#define __XFS_TRANS_H__

+#include <linux/lockref.h>
+
/* kernel only transaction subsystem defines */

struct xlog;
@@ -46,6 +48,8 @@ struct xfs_log_item {
struct xfs_log_vec *li_lv_shadow; /* standby vector */
xfs_csn_t li_seq; /* CIL commit seq */
uint32_t li_order_id; /* CIL commit order */
+
+ struct lockref li_ref; /* log item reference */
};

/*
@@ -110,6 +114,9 @@ xlog_item_is_intent_done(struct xfs_log_item *lip)

void xfs_log_item_init(struct xfs_mount *mp, struct xfs_log_item *item,
int type, const struct xfs_item_ops *ops);
+void xfs_log_item_get(struct xfs_log_item *lip);
+bool xfs_log_item_put(struct xfs_log_item *lip);
+bool xfs_log_item_get_safe(struct xfs_log_item *lip);

/*
* Return values for the iop_push() routines.
--
2.55.0