From patchwork Mon Aug 5 09:32:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Gowans, James" X-Patchwork-Id: 13753329 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 650C1C3DA4A for ; Mon, 5 Aug 2024 09:34:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EFDD26B008C; Mon, 5 Aug 2024 05:34:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EABB46B0092; Mon, 5 Aug 2024 05:34:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D729F6B0093; Mon, 5 Aug 2024 05:34:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id B7D7F6B008C for ; Mon, 5 Aug 2024 05:34:16 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 610DB81D09 for ; Mon, 5 Aug 2024 09:34:16 +0000 (UTC) X-FDA: 82417680912.01.EFEF178 Received: from smtp-fw-80008.amazon.com (smtp-fw-80008.amazon.com [99.78.197.219]) by imf15.hostedemail.com (Postfix) with ESMTP id 4AD3FA0013 for ; Mon, 5 Aug 2024 09:34:14 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=beTU4qxc; spf=pass (imf15.hostedemail.com: domain of "prvs=940e15008=jgowans@amazon.com" designates 99.78.197.219 as permitted sender) smtp.mailfrom="prvs=940e15008=jgowans@amazon.com"; dmarc=pass (policy=quarantine) header.from=amazon.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722850392; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=mf4bGqkjJGcuD7WLWM/6xATCnP8HxmWYZb3Uae5eqbU=; b=Xhpx3Tv0E5ZCkytGpiZmP1oVbwEQtMfU90VOi+Xc0PKRT/TJ4Dg1nPVUdr2yaTIfPQFZYc YDm5bxOquiv5/RGUJfUAVQf73UDhUnU7aMtjYCnRbmR93IFjsGPHUGrt/PBt/Fzfenv5gu so4XaL0xXiVOJvWeTWCgr9GYX0CRqnw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722850392; a=rsa-sha256; cv=none; b=Ioo/1XDhxhpXhQzxdK04w4+TMB3O3XlWeRhVMkRBEnn7NprYYtXIQg3FkzSJ1jK37b+nGD 5DZrq2BmWAfGni2aFFK8MC0NQKz3Lc0IrK8CEpmpKHY2c/BLa2EtvNBchDGAvewSRSXpc7 kL+F3B9aF0enedWUhoRaVJoAxulEui0= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=beTU4qxc; spf=pass (imf15.hostedemail.com: domain of "prvs=940e15008=jgowans@amazon.com" designates 99.78.197.219 as permitted sender) smtp.mailfrom="prvs=940e15008=jgowans@amazon.com"; dmarc=pass (policy=quarantine) header.from=amazon.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1722850454; x=1754386454; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=mf4bGqkjJGcuD7WLWM/6xATCnP8HxmWYZb3Uae5eqbU=; b=beTU4qxcmHHToJLaMKTze7TrqVcBEFY5+QegBM6/tXgl1QraGY6Ced0I LAYjuEap0z0zchyJ080rZ1tx0PCjzBFjviNw7GtWiRh0sUipwFQzwDRC9 gex2rcSMFhIocBWdoUjSfzhz3R5/JnaxoAUarSo7rKBMHEKGpJHvi/rES w=; X-IronPort-AV: E=Sophos;i="6.09,264,1716249600"; d="scan'208";a="112401062" Received: from pdx4-co-svc-p1-lb2-vlan3.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.25.36.214]) by smtp-border-fw-80008.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Aug 2024 09:34:11 +0000 Received: from EX19MTAEUA001.ant.amazon.com [10.0.43.254:30354] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.25.233:2525] with esmtp (Farcaster) id bfd7aaac-eb93-420b-99d8-e287bbc4fa9e; Mon, 5 Aug 2024 09:34:10 +0000 (UTC) X-Farcaster-Flow-ID: bfd7aaac-eb93-420b-99d8-e287bbc4fa9e Received: from EX19D014EUC004.ant.amazon.com (10.252.51.182) by EX19MTAEUA001.ant.amazon.com (10.252.50.192) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Mon, 5 Aug 2024 09:34:08 +0000 Received: from u5d18b891348c5b.ant.amazon.com (10.146.13.113) by EX19D014EUC004.ant.amazon.com (10.252.51.182) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Mon, 5 Aug 2024 09:33:59 +0000 From: James Gowans To: CC: James Gowans , Sean Christopherson , Paolo Bonzini , Alexander Viro , Steve Sistare , Christian Brauner , Jan Kara , "Anthony Yznaga" , Mike Rapoport , "Andrew Morton" , , Jason Gunthorpe , , Usama Arif , , Alexander Graf , David Woodhouse , Paul Durrant , Nicolas Saenz Julienne Subject: [PATCH 03/10] guestmemfs: add persistent data block allocator Date: Mon, 5 Aug 2024 11:32:38 +0200 Message-ID: <20240805093245.889357-4-jgowans@amazon.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240805093245.889357-1-jgowans@amazon.com> References: <20240805093245.889357-1-jgowans@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.146.13.113] X-ClientProxiedBy: EX19D044UWB004.ant.amazon.com (10.13.139.134) To EX19D014EUC004.ant.amazon.com (10.252.51.182) X-Stat-Signature: jfiof9m551q8h6t45fderzmo1tt5d7ph X-Rspamd-Queue-Id: 4AD3FA0013 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1722850454-84536 X-HE-Meta: U2FsdGVkX19btmAHlV9KxRcs5ViW/PIOnIjGp2u9ebr5sfmKQbXdfItM7zORUIBqOTuNTsh+MREf9z4trCGng+swke0awmGoMDEMn9sE/seFbSYuNa25xlVB26zSkTQoAqgwCxra5pQHPLVJ3IkyWV79FvSFXD/fxgytd6IJUVw51l8mKSH3dBg+Lp1Pvi5sjP3t+WGyiqYjTgzMLwlpUpe0UoMVE5pkk6PySnOot9FbBAa7VY/Uzm1MeP2OBpmmfL35hU+TGM7Rqr88Aa/H3exOEoZgJPal3JcFFhlrTqBWwc2wFYj2L8v1V/kBFrwqdosTelVNsFZQcZDJrd3d3FXwLKAgQfwbyIVsKGdHzEvVThk9QLZ02wQAVJlg/UAKSj5/PiYxg0NB8Efuuk+SNpUDALeeu1N4N7pAZdpQnzuI+NG7nBcm6qhVU4KKfhJ73q6uH4OLoc/kK3qxeDQMCK8PdGHrTQCL/ASjgoBcAyVv4QBE98wXkLsgC7yuzGenJLk4zQcTlD/BxYbJS9jpu2cvk76R/QMF892oBknKhpI6yoOarWms7Eywuylp0HNmPQ37G7hKKxTHkpGoDUf/jMO03pbgwjL3OvxRaVRY7SERA8QNz6LwheaEGqNk1yRgZdIMzJzQTycsAj2U8FpU5Ti97UHm08J8g8phMS38HJnM9Iw0aRngWC7nrjuCYXqH6oFgMAMpMR+6eNA+ic+DfhVK4z/P/StyXxvkliOPkhGvAshpd2TASO+4Xe5drIGT3DLlJ8iAoAmOCeS5l4AmLcPuy4rW03/QeCsxXTMwFiGiZwdhJJaly7cSrtJ7Lk+6NoEkQRjWTrj8VtOyXD7F38kriLZLBzoD6AEv0sqs5R9sqLa0Smxijk1+If4qSMnwc4QlLh2TYamFgsgos4A5K1vNU7QJgjnrpgPiqGpgwqphwnocuT9MK4/3NslBbjIZzugBH5NFxB5IVQd0c+Y EbszFsHd /GyaquY9BRvTTgpCfeQpTdGwRcq06rxQTXjeK8/jrSiqXpLtcWahbLozi2ovAuodAIkQc8tRVNvnNDtCFarO4AjMTGpRXkuqRricA63wqxthXDa+q/bkwoJBFC9LwwpS4cHlnA8x2LeGRJGEwzHISnEeRNTz4ZeRii0/iTEHzCIUSOoY1+MkizNvYvq9RXpcqbRSlDvfSd1MMyPxnNwO5Vc1eC8eI6pGCMwITB0km++0d6+PgePA+EN97buxRhY9mzCo0XM1yGGdP59c7Z8PVtvdPZQaRy9gXPmZIw8RHuhHujNp9jkijwxRB8jQJYpiDsNyOt4ZC+XwdpcZ7VYqPDDwLADqgElSd+GODSQtG3PwMkPtk7RPdBBfm2QRJPpLL0LIbf/fpWASHeU3GmH8T1br1XXampeyEsoukI+eoFPgPZeAz/mjgGqmq2WP9+WiroFX78ajGmKBFZUe+rU96efXlQ1uPXC22uIoo88W6OUcjM6quMEci89eSUhoN6LXp+MyWx+KK0i48BV+VUa9hN5QrJ5uZM/2y+6RYYOxCMRrxdqZyo5Xf26gaar3KlUXDDF/VjfpqlYXhxc0= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 --- 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);