Linux-XFS Archive on lore.kernel.org
 help / color / Atom feed
From: Dave Chinner <david@fromorbit.com>
To: linux-xfs@vger.kernel.org
Subject: [PATCH 21/27] libxfs: Add kernel list_lru wrapper
Date: Thu, 15 Oct 2020 18:21:49 +1100
Message-ID: <20201015072155.1631135-22-david@fromorbit.com> (raw)
In-Reply-To: <20201015072155.1631135-1-david@fromorbit.com>

From: Dave Chinner <dchinner@redhat.com>

The buffer cache in the kernel uses the list_lru infrastructure
for cache reclaim, so we need to add some wrappers to provide
the necessary functionality to userspace to use the same buffer
cache and buftarg code as the kernel for managing the global
buffer cache LRU.

Signed-off-by: Dave Chinner <dchinner@redhat.com>
---
 include/Makefile     |  1 +
 include/libxfs.h     |  1 +
 include/list_lru.h   | 69 ++++++++++++++++++++++++++++++++++++++++++++
 libxfs/buftarg.c     |  7 ++++-
 libxfs/libxfs_io.h   |  1 +
 libxfs/libxfs_priv.h |  1 +
 libxfs/xfs_buftarg.h |  1 +
 7 files changed, 80 insertions(+), 1 deletion(-)
 create mode 100644 include/list_lru.h

diff --git a/include/Makefile b/include/Makefile
index ce89d0237c19..0bd529545dfc 100644
--- a/include/Makefile
+++ b/include/Makefile
@@ -16,6 +16,7 @@ LIBHFILES = libxfs.h \
 	hlist.h \
 	kmem.h \
 	list.h \
+	list_lru.h \
 	parent.h \
 	sema.h \
 	spinlock.h \
diff --git a/include/libxfs.h b/include/libxfs.h
index 72c0b525f9db..7dfc4d2fd3ab 100644
--- a/include/libxfs.h
+++ b/include/libxfs.h
@@ -21,6 +21,7 @@
 #include "spinlock.h"
 #include "completion.h"
 #include "sema.h"
+#include "list_lru.h"
 
 #include "xfs_types.h"
 #include "xfs_fs.h"
diff --git a/include/list_lru.h b/include/list_lru.h
new file mode 100644
index 000000000000..91c3908432e6
--- /dev/null
+++ b/include/list_lru.h
@@ -0,0 +1,69 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2020 RedHat, Inc.
+ * All Rights Reserved.
+ */
+#ifndef __LIBXFS_LIST_LRU_H__
+#define __LIBXFS_LIST_LRU_H__
+
+/*
+ * This implements kernel compatible list_lru semantics that the buffer cache
+ * requires. It is not meant as a hugely scalable lru list like the kernel, but
+ * just what is needed for the buffer cache to function in userspace.
+ */
+struct list_lru {
+	struct list_head	l_lru;
+	spinlock_t		l_lock;
+	uint64_t		l_count;
+};
+
+static inline bool
+list_lru_add(
+	struct list_lru		*lru,
+	struct list_head	*item)
+{
+	spin_lock(&lru->l_lock);
+	if (!list_empty(item)) {
+		spin_unlock(&(lru->l_lock));
+		return false;
+	}
+	list_add_tail(item, &lru->l_lru);
+	lru->l_count++;
+	spin_unlock(&(lru->l_lock));
+	return true;
+}
+
+static inline bool
+list_lru_del(
+	struct list_lru		*lru,
+	struct list_head	*item)
+{
+	spin_lock(&lru->l_lock);
+	if (list_empty(item)) {
+		spin_unlock(&(lru->l_lock));
+		return false;
+	}
+	list_del_init(item);
+	lru->l_count--;
+	spin_unlock(&(lru->l_lock));
+	return true;
+}
+
+static inline bool
+list_lru_init(
+	struct list_lru		*lru)
+{
+	list_head_init(&lru->l_lru);
+	spin_lock_init(&lru->l_lock);
+	lru->l_count = 0;
+	return false;
+}
+
+static inline void
+list_lru_destroy(
+	struct list_lru		*lru)
+{
+	return;
+}
+
+#endif /* __LIBXFS_LIST_LRU_H__ */
diff --git a/libxfs/buftarg.c b/libxfs/buftarg.c
index dbecab833cb2..6dc8e76d26ef 100644
--- a/libxfs/buftarg.c
+++ b/libxfs/buftarg.c
@@ -78,11 +78,16 @@ xfs_buftarg_alloc(
 	if (xfs_buftarg_setsize_early(btp))
 		goto error_free;
 
-	if (percpu_counter_init(&btp->bt_io_count, 0, GFP_KERNEL))
+	if (list_lru_init(&btp->bt_lru))
 		goto error_free;
 
+	if (percpu_counter_init(&btp->bt_io_count, 0, GFP_KERNEL))
+		goto error_lru;
+
 	return btp;
 
+error_lru:
+	list_lru_destroy(&btp->bt_lru);
 error_free:
 	free(btp);
 	return NULL;
diff --git a/libxfs/libxfs_io.h b/libxfs/libxfs_io.h
index 2e7c943d8978..b4022a4e5dd8 100644
--- a/libxfs/libxfs_io.h
+++ b/libxfs/libxfs_io.h
@@ -72,6 +72,7 @@ struct xfs_buf {
 	struct list_head	b_btc_list;
 	unsigned int		b_state;
 	atomic_t		b_lru_ref;
+	struct list_head	b_lru;
 };
 
 bool xfs_verify_magic(struct xfs_buf *bp, __be32 dmagic);
diff --git a/libxfs/libxfs_priv.h b/libxfs/libxfs_priv.h
index 151c030b5876..0e04ab910b8b 100644
--- a/libxfs/libxfs_priv.h
+++ b/libxfs/libxfs_priv.h
@@ -51,6 +51,7 @@
 #include "spinlock.h"
 #include "completion.h"
 #include "sema.h"
+#include "list_lru.h"
 
 #include "xfs_types.h"
 #include "xfs_arch.h"
diff --git a/libxfs/xfs_buftarg.h b/libxfs/xfs_buftarg.h
index 129b43e037ad..98b4996bea53 100644
--- a/libxfs/xfs_buftarg.h
+++ b/libxfs/xfs_buftarg.h
@@ -41,6 +41,7 @@ struct xfs_buftarg {
 
 	uint32_t		bt_io_count;
 	unsigned int		flags;
+	struct list_lru		bt_lru;
 };
 
 /* We purged a dirty buffer and lost a write. */
-- 
2.28.0


  parent reply index

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-15  7:21 [PATCH 00/27] [RFC, WIP] xfsprogs: xfs_buf unification and AIO Dave Chinner
2020-10-15  7:21 ` [PATCH 01/27] xfsprogs: remove unused buffer tracing code Dave Chinner
2020-10-15  7:21 ` [PATCH 02/27] xfsprogs: remove unused IO_DEBUG functionality Dave Chinner
2020-11-16  2:31   ` Eric Sandeen
2020-10-15  7:21 ` [PATCH 03/27] libxfs: get rid of b_bcount from xfs_buf Dave Chinner
2020-11-23 19:53   ` Eric Sandeen
2020-10-15  7:21 ` [PATCH 04/27] libxfs: rename buftarg->dev to btdev Dave Chinner
2020-11-16  2:33   ` Eric Sandeen
2020-10-15  7:21 ` [PATCH 05/27] xfsprogs: get rid of ancient btree tracing fragments Dave Chinner
2020-11-16  2:35   ` Eric Sandeen
2020-10-15  7:21 ` [PATCH 06/27] xfsprogs: remove xfs_buf_t typedef Dave Chinner
2020-10-15 15:22   ` Darrick J. Wong
2020-10-15 20:54     ` Dave Chinner
2020-10-15  7:21 ` [PATCH 07/27] xfsprogs: introduce liburcu support Dave Chinner
2020-10-15  7:21 ` [PATCH 08/27] libxfs: add spinlock_t wrapper Dave Chinner
2020-10-15  7:21 ` [PATCH 09/27] atomic: convert to uatomic Dave Chinner
2020-10-15  7:21 ` [PATCH 10/27] libxfs: add kernel-compatible completion API Dave Chinner
2020-10-15 17:09   ` Darrick J. Wong
2020-10-19 22:21     ` Dave Chinner
2020-10-15  7:21 ` [PATCH 11/27] libxfs: add wrappers for kernel semaphores Dave Chinner
2020-10-15  7:21 ` [PATCH 12/27] xfsprogs: convert use-once buffer reads to uncached IO Dave Chinner
2020-10-15 17:12   ` Darrick J. Wong
2020-10-19 22:36     ` Dave Chinner
2020-10-15  7:21 ` [PATCH 13/27] libxfs: introduce userspace buftarg infrastructure Dave Chinner
2020-10-15  7:21 ` [PATCH 14/27] xfs: rename libxfs_buftarg_init to libxfs_open_devices() Dave Chinner
2020-10-15  7:21 ` [PATCH 15/27] libxfs: introduce userspace buftarg infrastructure Dave Chinner
2020-10-15 17:16   ` Darrick J. Wong
2020-10-15  7:21 ` [PATCH 16/27] libxfs: add a synchronous IO engine to the buftarg Dave Chinner
2020-10-15  7:21 ` [PATCH 17/27] xfsprogs: convert libxfs_readbufr to libxfs_buf_read_uncached Dave Chinner
2020-10-15  7:21 ` [PATCH 18/27] libxfs: convert libxfs_bwrite to buftarg IO Dave Chinner
2020-10-15  7:21 ` [PATCH 19/27] libxfs: add cache infrastructure to buftarg Dave Chinner
2020-10-15  7:21 ` [PATCH 20/27] libxfs: add internal lru to btcache Dave Chinner
2020-10-15  7:21 ` Dave Chinner [this message]
2020-10-15  7:21 ` [PATCH 22/27] libxfs: introduce new buffer cache infrastructure Dave Chinner
2020-10-15 17:46   ` Darrick J. Wong
2020-10-15  7:21 ` [PATCH 23/27] libxfs: use PSI information to detect memory pressure Dave Chinner
2020-10-15 17:56   ` Darrick J. Wong
2020-10-15 21:20     ` Dave Chinner
2020-10-15  7:21 ` [PATCH 24/27] libxfs: add a buftarg cache shrinker implementation Dave Chinner
2020-10-15 18:01   ` Darrick J. Wong
2020-10-15 21:33     ` Dave Chinner
2020-10-15  7:21 ` [PATCH 25/27] libxfs: switch buffer cache implementations Dave Chinner
2020-10-15  7:21 ` [PATCH 26/27] build: set platform_defs.h.in dependency correctly Dave Chinner
2020-10-15  7:21 ` [PATCH 27/27] libxfs: convert sync IO buftarg engine to AIO Dave Chinner
2020-10-15 18:26   ` Darrick J. Wong
2020-10-15 21:42     ` Dave Chinner
2020-10-15  7:29 ` [PATCH 00/27] [RFC, WIP] xfsprogs: xfs_buf unification and AIO Dave Chinner
2020-10-15 18:37 ` Darrick J. Wong
2020-10-15 22:35   ` Dave Chinner

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=20201015072155.1631135-22-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

Linux-XFS Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-xfs/0 linux-xfs/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-xfs linux-xfs/ https://lore.kernel.org/linux-xfs \
		linux-xfs@vger.kernel.org
	public-inbox-index linux-xfs

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-xfs


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git