All of lore.kernel.org
 help / color / mirror / Atom feed
From: Dave Chinner <david@fromorbit.com>
To: linux-xfs@vger.kernel.org
Subject: [PATCH 2/5] libxfs: add spinlock_t wrapper
Date: Thu, 16 Sep 2021 11:46:46 +1000	[thread overview]
Message-ID: <20210916014649.1835564-3-david@fromorbit.com> (raw)
In-Reply-To: <20210916014649.1835564-1-david@fromorbit.com>

From: Dave Chinner <dchinner@redhat.com>

These provide the kernel spinlock_t interface, but are *not*
spinlocks. Spinlocks cannot be used by general purpose userspace
processes due to the fact they cannot control task preemption and
scheduling reliability. Hence these are implemented as a
pthread_mutex_t, similar to the way the kernel RT build implements
spinlock_t as a kernel mutex.

Because the current libxfs spinlock "implementation" just makes
spinlocks go away, we have to also add initialisation to spinlocks
that libxfs uses that are missing from the userspace implementation.

Signed-off-by: Dave Chinner <dchinner@redhat.com>
---
 include/Makefile     |  1 +
 include/libxfs.h     |  1 +
 include/spinlock.h   | 25 +++++++++++++++++++++++++
 include/xfs_inode.h  |  1 +
 include/xfs_mount.h  |  2 ++
 include/xfs_trans.h  |  1 +
 libxfs/init.c        |  4 +++-
 libxfs/libxfs_priv.h |  4 +---
 libxfs/rdwr.c        |  2 ++
 9 files changed, 37 insertions(+), 4 deletions(-)
 create mode 100644 include/spinlock.h

diff --git a/include/Makefile b/include/Makefile
index 632b819fcded..f7c40a5ce1a1 100644
--- a/include/Makefile
+++ b/include/Makefile
@@ -16,6 +16,7 @@ LIBHFILES = libxfs.h \
 	kmem.h \
 	list.h \
 	parent.h \
+	spinlock.h \
 	xfs_inode.h \
 	xfs_log_recover.h \
 	xfs_metadump.h \
diff --git a/include/libxfs.h b/include/libxfs.h
index bc07655e64f1..a494a1d4b002 100644
--- a/include/libxfs.h
+++ b/include/libxfs.h
@@ -18,6 +18,7 @@
 #include "kmem.h"
 #include "libfrog/radix-tree.h"
 #include "atomic.h"
+#include "spinlock.h"
 
 #include "xfs_types.h"
 #include "xfs_fs.h"
diff --git a/include/spinlock.h b/include/spinlock.h
new file mode 100644
index 000000000000..8da2325cc8f5
--- /dev/null
+++ b/include/spinlock.h
@@ -0,0 +1,25 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2019-20 RedHat, Inc.
+ * All Rights Reserved.
+ */
+#ifndef __LIBXFS_SPINLOCK_H__
+#define __LIBXFS_SPINLOCK_H__
+
+/*
+ * This implements kernel compatible spinlock exclusion semantics. These,
+ * however, are not spinlocks, as spinlocks cannot be reliably implemented in
+ * userspace without using realtime scheduling task contexts. Hence this
+ * interface is implemented with pthread mutexes and so can block, but this is
+ * no different to the kernel RT build which replaces spinlocks with mutexes.
+ * Hence we know it works.
+ */
+
+typedef pthread_mutex_t	spinlock_t;
+
+#define spin_lock_init(l)	pthread_mutex_init(l, NULL)
+#define spin_lock(l)           pthread_mutex_lock(l)
+#define spin_trylock(l)        (pthread_mutex_trylock(l) != EBUSY)
+#define spin_unlock(l)         pthread_mutex_unlock(l)
+
+#endif /* __LIBXFS_SPINLOCK_H__ */
diff --git a/include/xfs_inode.h b/include/xfs_inode.h
index 0551fe457d0c..08a62d833372 100644
--- a/include/xfs_inode.h
+++ b/include/xfs_inode.h
@@ -43,6 +43,7 @@ struct inode {
 	struct timespec64	i_atime;
 	struct timespec64	i_mtime;
 	struct timespec64	i_ctime;
+	spinlock_t		i_lock;
 };
 
 static inline uint32_t i_uid_read(struct inode *inode)
diff --git a/include/xfs_mount.h b/include/xfs_mount.h
index 12019c4b4dbd..2f3208802575 100644
--- a/include/xfs_mount.h
+++ b/include/xfs_mount.h
@@ -22,6 +22,7 @@ typedef struct xfs_mount {
 #define m_icount	m_sb.sb_icount
 #define m_ifree		m_sb.sb_ifree
 #define m_fdblocks	m_sb.sb_fdblocks
+	spinlock_t		m_sb_lock;
 
 	/*
 	 * Bitsets of per-fs metadata that have been checked and/or are sick.
@@ -32,6 +33,7 @@ typedef struct xfs_mount {
 
 	char			*m_fsname;	/* filesystem name */
 	int			m_bsize;	/* fs logical block size */
+	spinlock_t		m_agirotor_lock;
 	xfs_agnumber_t		m_agfrotor;	/* last ag where space found */
 	xfs_agnumber_t		m_agirotor;	/* last ag dir inode alloced */
 	xfs_agnumber_t		m_maxagi;	/* highest inode alloc group */
diff --git a/include/xfs_trans.h b/include/xfs_trans.h
index ad76ecfddebc..2c55bb857369 100644
--- a/include/xfs_trans.h
+++ b/include/xfs_trans.h
@@ -35,6 +35,7 @@ struct xfs_inode_log_item {
 	unsigned int		ili_last_fields;	/* fields when flushed*/
 	unsigned int		ili_fields;		/* fields to be logged */
 	unsigned int		ili_fsync_fields;	/* ignored by userspace */
+	spinlock_t		ili_lock;
 };
 
 typedef struct xfs_buf_log_item {
diff --git a/libxfs/init.c b/libxfs/init.c
index b06faf8acdde..2c54546bcdda 100644
--- a/libxfs/init.c
+++ b/libxfs/init.c
@@ -743,7 +743,9 @@ libxfs_mount(
 	mp->m_flags = (LIBXFS_MOUNT_32BITINODES|LIBXFS_MOUNT_32BITINOOPT);
 	mp->m_sb = *sb;
 	INIT_RADIX_TREE(&mp->m_perag_tree, GFP_KERNEL);
-	sbp = &(mp->m_sb);
+	sbp = &mp->m_sb;
+	spin_lock_init(&mp->m_sb_lock);
+	spin_lock_init(&mp->m_agirotor_lock);
 
 	xfs_sb_mount_common(mp, sb);
 
diff --git a/libxfs/libxfs_priv.h b/libxfs/libxfs_priv.h
index db90e173f36e..e1e90268c0b7 100644
--- a/libxfs/libxfs_priv.h
+++ b/libxfs/libxfs_priv.h
@@ -48,6 +48,7 @@
 #include "kmem.h"
 #include "libfrog/radix-tree.h"
 #include "atomic.h"
+#include "spinlock.h"
 
 #include "xfs_types.h"
 #include "xfs_arch.h"
@@ -205,9 +206,6 @@ enum ce { CE_DEBUG, CE_CONT, CE_NOTE, CE_WARN, CE_ALERT, CE_PANIC };
 #endif
 
 /* miscellaneous kernel routines not in user space */
-#define spin_lock_init(a)	((void) 0)
-#define spin_lock(a)		((void) 0)
-#define spin_unlock(a)		((void) 0)
 #define likely(x)		(x)
 #define unlikely(x)		(x)
 
diff --git a/libxfs/rdwr.c b/libxfs/rdwr.c
index 713ef9afc8c6..a5fd0596687e 100644
--- a/libxfs/rdwr.c
+++ b/libxfs/rdwr.c
@@ -1070,6 +1070,8 @@ libxfs_iget(
 	VFS_I(ip)->i_count = 1;
 	ip->i_ino = ino;
 	ip->i_mount = mp;
+	spin_lock_init(&VFS_I(ip)->i_lock);
+
 	error = xfs_imap(mp, tp, ip->i_ino, &ip->i_imap, 0);
 	if (error)
 		goto out_destroy;
-- 
2.33.0


  parent reply	other threads:[~2021-09-16  1:46 UTC|newest]

Thread overview: 90+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-15 23:06 [PATCHSET 00/61] xfs: sync libxfs with 5.14 Darrick J. Wong
2021-09-15 23:06 ` [PATCH 01/61] mkfs: move mkfs/proto.c declarations to mkfs/proto.h Darrick J. Wong
2021-09-16  7:24   ` Christoph Hellwig
2021-09-15 23:06 ` [PATCH 02/61] libfrog: move topology.[ch] to libxfs Darrick J. Wong
2021-09-16  7:26   ` Christoph Hellwig
2021-09-15 23:06 ` [PATCH 03/61] libfrog: create header file for mocked-up kernel data structures Darrick J. Wong
2021-09-16  0:46   ` Dave Chinner
2021-09-16  0:58     ` Darrick J. Wong
2021-09-16  1:29       ` Dave Chinner
2021-09-16  1:37         ` Dave Chinner
2021-09-16  1:46           ` [PATCH 0/5] xfsprogs: generic serialisation primitives Dave Chinner
2021-09-16  1:46             ` [PATCH 1/5] xfsprogs: introduce liburcu support Dave Chinner
2021-09-24  0:41               ` Eric Sandeen
2021-09-24  3:02                 ` Chandan Babu R
2021-09-16  1:46             ` Dave Chinner [this message]
2021-09-16  1:46             ` [PATCH 3/5] atomic: convert to uatomic Dave Chinner
2021-09-16  1:46             ` [PATCH 4/5] libxfs: add kernel-compatible completion API Dave Chinner
2021-09-16  1:46             ` [PATCH 5/5] libxfs: add wrappers for kernel semaphores Dave Chinner
2021-09-22 22:08             ` [PATCH 0/5] xfsprogs: generic serialisation primitives Eric Sandeen
2021-09-23  8:47             ` [External] : " Chandan Babu R
2021-09-16 16:23     ` [PATCH 03/61] libfrog: create header file for mocked-up kernel data structures Eric Sandeen
2021-09-15 23:06 ` [PATCH 04/61] libxfs: port xfs_set_inode_alloc from the kernel Darrick J. Wong
2021-10-01 17:54   ` Eric Sandeen
2021-09-15 23:07 ` [PATCH 05/61] libxfs: fix whitespace inconsistencies with kernel Darrick J. Wong
2021-10-01 19:06   ` Eric Sandeen
2021-09-15 23:07 ` [PATCH 06/61] xfs: Fix fall-through warnings for Clang Darrick J. Wong
2021-10-01 19:57   ` Eric Sandeen
2021-09-15 23:07 ` [PATCH 07/61] misc: convert utilities to use "fallthrough;" Darrick J. Wong
2021-10-01 19:10   ` Eric Sandeen
2021-09-15 23:07 ` [PATCH 08/61] xfs: use xfs_buf_alloc_pages for uncached buffers Darrick J. Wong
2021-09-15 23:07 ` [PATCH 09/61] xfs: Reverse apply 72b97ea40d Darrick J. Wong
2021-09-15 23:07 ` [PATCH 10/61] xfs: Add xfs_attr_node_remove_name Darrick J. Wong
2021-09-15 23:07 ` [PATCH 11/61] xfs: Refactor xfs_attr_set_shortform Darrick J. Wong
2021-09-15 23:07 ` [PATCH 12/61] xfs: Separate xfs_attr_node_addname and xfs_attr_node_addname_clear_incomplete Darrick J. Wong
2021-09-15 23:07 ` [PATCH 13/61] xfs: Add helper xfs_attr_node_addname_find_attr Darrick J. Wong
2021-09-15 23:07 ` [PATCH 14/61] xfs: Hoist xfs_attr_node_addname Darrick J. Wong
2021-09-15 23:07 ` [PATCH 15/61] xfs: Hoist xfs_attr_leaf_addname Darrick J. Wong
2021-09-15 23:08 ` [PATCH 16/61] xfs: Hoist node transaction handling Darrick J. Wong
2021-09-15 23:08 ` [PATCH 17/61] xfs: Add delay ready attr remove routines Darrick J. Wong
2021-09-15 23:08 ` [PATCH 18/61] xfs: Add delay ready attr set routines Darrick J. Wong
2021-09-15 23:08 ` [PATCH 19/61] xfs: Remove xfs_attr_rmtval_set Darrick J. Wong
2021-09-15 23:08 ` [PATCH 20/61] xfs: Clean up xfs_attr_node_addname_clear_incomplete Darrick J. Wong
2021-09-15 23:08 ` [PATCH 21/61] xfs: clean up open-coded fs block unit conversions Darrick J. Wong
2021-09-15 23:08 ` [PATCH 22/61] xfs: move xfs_perag_get/put to xfs_ag.[ch] Darrick J. Wong
2021-09-15 23:08 ` [PATCH 23/61] xfs: move perag structure and setup to libxfs/xfs_ag.[ch] Darrick J. Wong
2021-09-15 23:08 ` [PATCH 24/61] xfs: make for_each_perag... a first class citizen Darrick J. Wong
2021-09-15 23:08 ` [PATCH 25/61] xfs: convert raw ag walks to use for_each_perag Darrick J. Wong
2021-09-15 23:08 ` [PATCH 26/61] xfs: convert xfs_iwalk to use perag references Darrick J. Wong
2021-09-15 23:09 ` [PATCH 27/61] xfs: convert secondary superblock walk to use perags Darrick J. Wong
2021-09-15 23:09 ` [PATCH 28/61] xfs: pass perags through to the busy extent code Darrick J. Wong
2021-09-15 23:09 ` [PATCH 29/61] xfs: push perags through the ag reservation callouts Darrick J. Wong
2021-09-15 23:09 ` [PATCH 30/61] xfs: pass perags around in fsmap data dev functions Darrick J. Wong
2021-09-15 23:09 ` [PATCH 31/61] xfs: add a perag to the btree cursor Darrick J. Wong
2021-09-15 23:09 ` [PATCH 32/61] xfs: convert rmap btree cursor to using a perag Darrick J. Wong
2021-09-15 23:09 ` [PATCH 33/61] xfs: convert refcount btree cursor to use perags Darrick J. Wong
2021-09-15 23:09 ` [PATCH 34/61] xfs: convert allocbt cursors " Darrick J. Wong
2021-09-15 23:09 ` [PATCH 35/61] xfs: use perag for ialloc btree cursors Darrick J. Wong
2021-09-15 23:09 ` [PATCH 36/61] xfs: remove agno from btree cursor Darrick J. Wong
2021-09-15 23:09 ` [PATCH 37/61] xfs: simplify xfs_dialloc_select_ag() return values Darrick J. Wong
2021-09-15 23:10 ` [PATCH 38/61] xfs: collapse AG selection for inode allocation Darrick J. Wong
2021-09-15 23:10 ` [PATCH 39/61] xfs: get rid of xfs_dir_ialloc() Darrick J. Wong
2021-09-15 23:10 ` [PATCH 40/61] xfs: inode allocation can use a single perag instance Darrick J. Wong
2021-09-15 23:10 ` [PATCH 41/61] xfs: clean up and simplify xfs_dialloc() Darrick J. Wong
2021-09-15 23:10 ` [PATCH 42/61] xfs: use perag through unlink processing Darrick J. Wong
2021-09-15 23:10 ` [PATCH 43/61] xfs: remove xfs_perag_t Darrick J. Wong
2021-09-15 23:10 ` [PATCH 44/61] xfs: sort variable alphabetically to avoid repeated declaration Darrick J. Wong
2021-09-15 23:10 ` [PATCH 45/61] xfs: Remove redundant assignment to busy Darrick J. Wong
2021-09-15 23:10 ` [PATCH 46/61] xfs: mark xfs_bmap_set_attrforkoff static Darrick J. Wong
2021-09-15 23:10 ` [PATCH 47/61] xfs: fix radix tree tag signs Darrick J. Wong
2021-09-15 23:10 ` [PATCH 48/61] xfs: drop the AGI being passed to xfs_check_agi_freecount Darrick J. Wong
2021-09-15 23:11 ` [PATCH 49/61] xfs: Fix default ASSERT in xfs_attr_set_iter Darrick J. Wong
2021-09-15 23:11 ` [PATCH 50/61] xfs: Make attr name schemes consistent Darrick J. Wong
2021-09-15 23:11 ` [PATCH 51/61] xfs: perag may be null in xfs_imap() Darrick J. Wong
2021-09-15 23:11 ` [PATCH 52/61] xfs: log stripe roundoff is a property of the log Darrick J. Wong
2021-09-15 23:11 ` [PATCH 53/61] xfs: xfs_log_force_lsn isn't passed a LSN Darrick J. Wong
2021-09-15 23:11 ` [PATCH 54/61] xfs: fix endianness issue in xfs_ag_shrink_space Darrick J. Wong
2021-09-15 23:11 ` [PATCH 55/61] xfs: Initialize error in xfs_attr_remove_iter Darrick J. Wong
2021-09-15 23:11 ` [PATCH 56/61] xfs: Fix multiple fall-through warnings for Clang Darrick J. Wong
2021-09-15 23:11 ` [PATCH 57/61] xfs: check for sparse inode clusters that cross new EOAG when shrinking Darrick J. Wong
2021-09-15 23:11 ` [PATCH 58/61] xfs: correct the narrative around misaligned rtinherit/extszinherit dirs Darrick J. Wong
2021-09-15 23:11 ` [PATCH 59/61] xfs: logging the on disk inode LSN can make it go backwards Darrick J. Wong
2021-09-15 23:12 ` [PATCH 60/61] xfs_db: convert the agresv command to use for_each_perag Darrick J. Wong
2021-09-16  7:20   ` Christoph Hellwig
2021-09-15 23:12 ` [PATCH 61/61] mkfs: warn about V4 deprecation when creating new V4 filesystems Darrick J. Wong
2021-09-16  7:18   ` Christoph Hellwig
2021-09-16 15:10     ` Darrick J. Wong
2021-09-16 15:15       ` Christoph Hellwig
2021-11-04  2:25   ` Darrick J. Wong
2021-11-04  2:30     ` Eric Sandeen
2021-09-15 23:36 ` [PATCHSET 00/61] xfs: sync libxfs with 5.14 Darrick J. Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210916014649.1835564-3-david@fromorbit.com \
    --to=david@fromorbit.com \
    --cc=linux-xfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.