From: James Gowans <jgowans@amazon.com>
To: <linux-kernel@vger.kernel.org>
Cc: James Gowans <jgowans@amazon.com>,
Sean Christopherson <seanjc@google.com>,
Paolo Bonzini <pbonzini@redhat.com>,
Alexander Viro <viro@zeniv.linux.org.uk>,
Steve Sistare <steven.sistare@oracle.com>,
Christian Brauner <brauner@kernel.org>, Jan Kara <jack@suse.cz>,
"Anthony Yznaga" <anthony.yznaga@oracle.com>,
Mike Rapoport <rppt@kernel.org>,
"Andrew Morton" <akpm@linux-foundation.org>, <linux-mm@kvack.org>,
Jason Gunthorpe <jgg@ziepe.ca>, <linux-fsdevel@vger.kernel.org>,
Usama Arif <usama.arif@bytedance.com>, <kvm@vger.kernel.org>,
Alexander Graf <graf@amazon.com>,
David Woodhouse <dwmw@amazon.co.uk>,
Paul Durrant <pdurrant@amazon.co.uk>,
Nicolas Saenz Julienne <nsaenz@amazon.es>
Subject: [PATCH 03/10] guestmemfs: add persistent data block allocator
Date: Mon, 5 Aug 2024 11:32:38 +0200 [thread overview]
Message-ID: <20240805093245.889357-4-jgowans@amazon.com> (raw)
In-Reply-To: <20240805093245.889357-1-jgowans@amazon.com>
In order to assign backing data memory to files there needs to be the
ability to allocate blocks of data from the large contiguous reserved
memory block of filesystem memory. Here an allocated is added to serve
that purpose. For now it's a simple bitmap allocator: each bit
corresponds to a 2 MiB chunk in the filesystem data block.
On initialisation the bitmap is allocated for a fixed size (TODO: make
this dynamic based on filesystem memory size). Allocating a block
involves finding and setting the next free bit.
Allocations will be done in the next commit which adds support for
truncating files.
It's quite limiting having a fixed size bitmap, and we perhaps want to
look at making this a dynamic and potentially large allocation early in
boot using the memblock allocator. It may also turn out that a simple
bitmap is too limiting and something with more metadata is needed.
Signed-off-by: James Gowans <jgowans@amazon.com>
---
fs/guestmemfs/Makefile | 2 +-
fs/guestmemfs/allocator.c | 40 ++++++++++++++++++++++++++++++++++++++
fs/guestmemfs/guestmemfs.c | 4 ++++
fs/guestmemfs/guestmemfs.h | 3 +++
4 files changed, 48 insertions(+), 1 deletion(-)
create mode 100644 fs/guestmemfs/allocator.c
diff --git a/fs/guestmemfs/Makefile b/fs/guestmemfs/Makefile
index 804997799ce8..b357073a60f3 100644
--- a/fs/guestmemfs/Makefile
+++ b/fs/guestmemfs/Makefile
@@ -3,4 +3,4 @@
# Makefile for persistent kernel filesystem
#
-obj-y += guestmemfs.o inode.o dir.o
+obj-y += guestmemfs.o inode.o dir.o allocator.o
diff --git a/fs/guestmemfs/allocator.c b/fs/guestmemfs/allocator.c
new file mode 100644
index 000000000000..3da14d11b60f
--- /dev/null
+++ b/fs/guestmemfs/allocator.c
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include "guestmemfs.h"
+
+/**
+ * For allocating blocks from the guestmemfs filesystem.
+ */
+
+static void *guestmemfs_allocations_bitmap(struct super_block *sb)
+{
+ return GUESTMEMFS_PSB(sb)->allocator_bitmap;
+}
+
+void guestmemfs_zero_allocations(struct super_block *sb)
+{
+ memset(guestmemfs_allocations_bitmap(sb), 0, (1 << 20));
+}
+
+/*
+ * Allocs one 2 MiB block, and returns the block index.
+ * Index is 2 MiB chunk index.
+ * Negative error code if unable to alloc.
+ */
+long guestmemfs_alloc_block(struct super_block *sb)
+{
+ unsigned long free_bit;
+ void *allocations_mem = guestmemfs_allocations_bitmap(sb);
+
+ free_bit = bitmap_find_next_zero_area(allocations_mem,
+ (1 << 20), /* Size */
+ 0, /* Start */
+ 1, /* Number of zeroed bits to look for */
+ 0); /* Alignment mask - none required. */
+
+ if (free_bit >= PMD_SIZE / 2)
+ return -ENOMEM;
+
+ bitmap_set(allocations_mem, free_bit, 1);
+ return free_bit;
+}
diff --git a/fs/guestmemfs/guestmemfs.c b/fs/guestmemfs/guestmemfs.c
index 21cb3490a2bd..c45c796c497a 100644
--- a/fs/guestmemfs/guestmemfs.c
+++ b/fs/guestmemfs/guestmemfs.c
@@ -37,6 +37,9 @@ static int guestmemfs_fill_super(struct super_block *sb, struct fs_context *fc)
psb->inodes = kzalloc(2 << 20, GFP_KERNEL);
if (!psb->inodes)
return -ENOMEM;
+ psb->allocator_bitmap = kzalloc(1 << 20, GFP_KERNEL);
+ if (!psb->allocator_bitmap)
+ return -ENOMEM;
/*
* Keep a reference to the persistent super block in the
@@ -45,6 +48,7 @@ static int guestmemfs_fill_super(struct super_block *sb, struct fs_context *fc)
sb->s_fs_info = psb;
spin_lock_init(&psb->allocation_lock);
guestmemfs_initialise_inode_store(sb);
+ guestmemfs_zero_allocations(sb);
guestmemfs_get_persisted_inode(sb, 1)->flags = GUESTMEMFS_INODE_FLAG_DIR;
strscpy(guestmemfs_get_persisted_inode(sb, 1)->filename, ".",
GUESTMEMFS_FILENAME_LEN);
diff --git a/fs/guestmemfs/guestmemfs.h b/fs/guestmemfs/guestmemfs.h
index 3a2954d1beec..af9832390be3 100644
--- a/fs/guestmemfs/guestmemfs.h
+++ b/fs/guestmemfs/guestmemfs.h
@@ -13,6 +13,7 @@ struct guestmemfs_sb {
unsigned long next_free_ino;
unsigned long allocated_inodes;
struct guestmemfs_inode *inodes;
+ void *allocator_bitmap;
spinlock_t allocation_lock;
};
@@ -37,6 +38,8 @@ struct guestmemfs_inode {
};
void guestmemfs_initialise_inode_store(struct super_block *sb);
+void guestmemfs_zero_allocations(struct super_block *sb);
+long guestmemfs_alloc_block(struct super_block *sb);
struct inode *guestmemfs_inode_get(struct super_block *sb, unsigned long ino);
struct guestmemfs_inode *guestmemfs_get_persisted_inode(struct super_block *sb, int ino);
--
2.34.1
next prev parent reply other threads:[~2024-08-05 9:34 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-05 9:32 [PATCH 00/10] Introduce guestmemfs: persistent in-memory filesystem James Gowans
2024-08-05 9:32 ` [PATCH 01/10] guestmemfs: Introduce filesystem skeleton James Gowans
2024-08-05 10:20 ` Christian Brauner
2024-08-05 9:32 ` [PATCH 02/10] guestmemfs: add inode store, files and dirs James Gowans
2024-08-05 9:32 ` James Gowans [this message]
2024-08-05 9:32 ` [PATCH 04/10] guestmemfs: support file truncation James Gowans
2024-08-05 9:32 ` [PATCH 05/10] guestmemfs: add file mmap callback James Gowans
2024-10-29 23:05 ` Elliot Berman
2024-10-30 22:18 ` Frank van der Linden
2024-11-01 12:55 ` Gowans, James
2024-10-31 15:30 ` Gowans, James
2024-10-31 16:06 ` Jason Gunthorpe
2024-11-01 13:01 ` Gowans, James
2024-11-01 13:42 ` Jason Gunthorpe
2024-11-02 8:24 ` Gowans, James
2024-11-04 11:11 ` Mike Rapoport
2024-11-04 14:39 ` Jason Gunthorpe
2024-11-04 10:49 ` Mike Rapoport
2024-08-05 9:32 ` [PATCH 06/10] kexec/kho: Add addr flag to not initialise memory James Gowans
2024-08-05 9:32 ` [PATCH 07/10] guestmemfs: Persist filesystem metadata via KHO James Gowans
2024-08-05 9:32 ` [PATCH 08/10] guestmemfs: Block modifications when serialised James Gowans
2024-08-05 9:32 ` [PATCH 09/10] guestmemfs: Add documentation and usage instructions James Gowans
2024-08-05 9:32 ` [PATCH 10/10] MAINTAINERS: Add maintainers for guestmemfs James Gowans
2024-08-05 14:32 ` [PATCH 00/10] Introduce guestmemfs: persistent in-memory filesystem Theodore Ts'o
2024-08-05 14:41 ` Paolo Bonzini
2024-08-05 19:47 ` Gowans, James
2024-08-05 19:53 ` Gowans, James
2024-08-05 20:01 ` Jan Kara
2024-08-05 23:29 ` Jason Gunthorpe
2024-08-06 8:26 ` Gowans, James
2024-08-06 8:12 ` Gowans, James
2024-08-06 13:43 ` David Hildenbrand
2024-10-17 4:53 ` Vishal Annapurve
2024-11-01 12:53 ` Gowans, James
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=20240805093245.889357-4-jgowans@amazon.com \
--to=jgowans@amazon.com \
--cc=akpm@linux-foundation.org \
--cc=anthony.yznaga@oracle.com \
--cc=brauner@kernel.org \
--cc=dwmw@amazon.co.uk \
--cc=graf@amazon.com \
--cc=jack@suse.cz \
--cc=jgg@ziepe.ca \
--cc=kvm@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=nsaenz@amazon.es \
--cc=pbonzini@redhat.com \
--cc=pdurrant@amazon.co.uk \
--cc=rppt@kernel.org \
--cc=seanjc@google.com \
--cc=steven.sistare@oracle.com \
--cc=usama.arif@bytedance.com \
--cc=viro@zeniv.linux.org.uk \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox