mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Howells <dhowells@redhat.com>
To: Steve French <sfrench@samba.org>
Cc: David Howells <dhowells@redhat.com>,
	Paulo Alcantara <pc@manguebit.org>,
	Shyam Prasad N <sprasad@microsoft.com>,
	Tom Talpey <tom@talpey.com>,
	Wang Zhaolong <wangzhaolong@huaweicloud.com>,
	Stefan Metzmacher <metze@samba.org>,
	Mina Almasry <almasrymina@google.com>,
	linux-cifs@vger.kernel.org, linux-kernel@vger.kernel.org,
	netfs@lists.linux.dev, linux-fsdevel@vger.kernel.org
Subject: [RFC PATCH 03/31] netfs: Provide facility to alloc buffer in a bvecq
Date: Wed,  6 Aug 2025 21:36:24 +0100	[thread overview]
Message-ID: <20250806203705.2560493-4-dhowells@redhat.com> (raw)
In-Reply-To: <20250806203705.2560493-1-dhowells@redhat.com>

Provide facility to allocate a series of bvecq structs and to attach
sufficient pages to that series to provide a buffer for the specified
amount of space.  This can be used to do things like creating an encryption
buffer in cifs and it can then be attached to an ITER_BVECQ iterator and
passed to a socket.

Signed-off-by: David Howells <dhowells@redhat.com>
cc: Steve French <sfrench@samba.org>
cc: Paulo Alcantara <pc@manguebit.org>
cc: Shyam Prasad N <sprasad@microsoft.com>
cc: Tom Talpey <tom@talpey.com>
cc: linux-cifs@vger.kernel.org
cc: netfs@lists.linux.dev
cc: linux-fsdevel@vger.kernel.org
---
 fs/netfs/Makefile     |   1 +
 fs/netfs/buffer.c     | 101 ++++++++++++++++++++++++++++++++++++++++++
 include/linux/netfs.h |   4 ++
 3 files changed, 106 insertions(+)
 create mode 100644 fs/netfs/buffer.c

diff --git a/fs/netfs/Makefile b/fs/netfs/Makefile
index b43188d64bd8..afab6603bd98 100644
--- a/fs/netfs/Makefile
+++ b/fs/netfs/Makefile
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0
 
 netfs-y := \
+	buffer.o \
 	buffered_read.o \
 	buffered_write.o \
 	direct_read.o \
diff --git a/fs/netfs/buffer.c b/fs/netfs/buffer.c
new file mode 100644
index 000000000000..1e4ed2746e95
--- /dev/null
+++ b/fs/netfs/buffer.c
@@ -0,0 +1,101 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Buffering helpers for bvec_queues
+ *
+ * Copyright (C) 2025 Red Hat, Inc. All Rights Reserved.
+ * Written by David Howells (dhowells@redhat.com)
+ */
+#include "internal.h"
+
+void dump_bvecq(const struct bvecq *bq)
+{
+	int b = 0;
+
+	for (; bq; bq = bq->next, b++) {
+		printk("BQ[%u] %u/%u\n", b, bq->nr_segs, bq->max_segs);
+		for (int s = 0; s < bq->nr_segs; s++) {
+			const struct bio_vec *bv = &bq->bv[s];
+			printk("BQ[%u:%u] %10lx %04x %04x %u\n",
+			       b, s,
+			       bv->bv_page ? page_to_pfn(bv->bv_page) : 0,
+			       bv->bv_offset, bv->bv_len,
+			       page_count(bv->bv_page));
+		}
+	}
+}
+
+/**
+ * netfs_alloc_bvecq_buffer - Allocate buffer space into a bvec queue
+ * @size: Target size of the buffer.
+ * @pre_slots: Number of preamble slots to set aside
+ * @gfp: The allocation constraints.
+ */
+struct bvecq *netfs_alloc_bvecq_buffer(size_t size, unsigned int pre_slots, gfp_t gfp)
+{
+	struct bvecq *head = NULL, *tail = NULL, *p = NULL;
+	size_t count = DIV_ROUND_UP(size, PAGE_SIZE);
+	int max_segs = 32;
+
+	_enter("%zx,%zx,%u", size, count, pre_slots);
+
+	do {
+		struct page **pages;
+		int want, got;
+
+		p = kzalloc(struct_size(p, bv, max_segs), gfp);
+		if (!p)
+			goto oom;
+		if (tail) {
+			tail->next = p;
+			p->prev = tail;
+		} else {
+			head = p;
+		}
+		tail = p;
+		pages = (struct page **)&p->bv[max_segs];
+		pages -= max_segs - pre_slots;
+
+		want = umin(count, max_segs - pre_slots);
+		got = alloc_pages_bulk(gfp, want, pages);
+		if (got < want) {
+			for (int i = 0; i < got; i++)
+				__free_page(pages[i]);
+			goto oom;
+		}
+
+		tail->max_segs = max_segs;
+		tail->nr_segs = pre_slots + got;
+		for (int i = 0; i < got; i++) {
+			int j = pre_slots + i;
+			set_page_count(pages[i], 1);
+			bvec_set_page(&tail->bv[j], pages[i], PAGE_SIZE, 0);
+		}
+
+		count -= got;
+		pre_slots = 0;
+	} while (count > 0);
+
+	return head;
+oom:
+	netfs_free_bvecq_buffer(head);
+	return NULL;
+}
+EXPORT_SYMBOL(netfs_alloc_bvecq_buffer);
+
+/**
+ * netfs_free_bvecq_buffer - Free a bvec queue
+ * @bq: The start of the folio queue to free
+ *
+ * Free up a chain of bvecqs and the pages it points to.
+ */
+void netfs_free_bvecq_buffer(struct bvecq *bq)
+{
+	struct bvecq *next;
+
+	for (; bq; bq = next) {
+		for (int seg = 0; seg < bq->nr_segs; seg++)
+			__free_page(bq->bv[seg].bv_page);
+		next = bq->next;
+		kfree(bq);
+	}
+}
+EXPORT_SYMBOL(netfs_free_bvecq_buffer);
diff --git a/include/linux/netfs.h b/include/linux/netfs.h
index f43f075852c0..8756129b7472 100644
--- a/include/linux/netfs.h
+++ b/include/linux/netfs.h
@@ -23,6 +23,7 @@
 enum netfs_sreq_ref_trace;
 typedef struct mempool_s mempool_t;
 struct folio_queue;
+struct bvecq;
 
 /**
  * folio_start_private_2 - Start an fscache write on a folio.  [DEPRECATED]
@@ -462,6 +463,9 @@ int netfs_alloc_folioq_buffer(struct address_space *mapping,
 			      struct folio_queue **_buffer,
 			      size_t *_cur_size, ssize_t size, gfp_t gfp);
 void netfs_free_folioq_buffer(struct folio_queue *fq);
+void dump_bvecq(const struct bvecq *bq);
+struct bvecq *netfs_alloc_bvecq_buffer(size_t size, unsigned int pre_slots, gfp_t gfp);
+void netfs_free_bvecq_buffer(struct bvecq *bq);
 
 /**
  * netfs_inode - Get the netfs inode context from the inode


  parent reply	other threads:[~2025-08-06 20:37 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-06 20:36 [RFC PATCH 00/31] netfs: [WIP] Allow the use of MSG_SPLICE_PAGES and use netmem allocator David Howells
2025-08-06 20:36 ` [RFC PATCH 01/31] iov_iter: Move ITER_DISCARD and ITER_XARRAY iteration out-of-line David Howells
2025-08-06 20:36 ` [RFC PATCH 02/31] iov_iter: Add a segmented queue of bio_vec[] David Howells
2025-08-06 20:36 ` David Howells [this message]
2025-08-06 20:36 ` [RFC PATCH 04/31] cifs, nls: Provide unicode size determination func David Howells
2025-08-06 20:36 ` [RFC PATCH 05/31] cifs: Introduce an ALIGN8() macro David Howells
2025-08-06 20:36 ` [RFC PATCH 06/31] cifs: Move the SMB1 transport code out of transport.c David Howells
2025-08-06 20:36 ` [RFC PATCH 07/31] cifs: Rename mid_q_entry to smb_message David Howells
2025-08-06 20:36 ` [RFC PATCH 08/31] cifs: Keep the CPU-endian command ID around David Howells
2025-08-06 20:36 ` [RFC PATCH 09/31] cifs: Rename SMB2_xxxx_HE to SMB2_xxxx David Howells
2025-08-06 20:36 ` [RFC PATCH 10/31] cifs: Make smb1's SendReceive() wrap cifs_send_recv() David Howells
2025-08-06 20:36 ` [RFC PATCH 11/31] cifs: Fix SMB1 to not require separate kvec for the rfc1002 header David Howells
2025-08-06 20:36 ` [RFC PATCH 12/31] cifs: Replace SendReceiveBlockingLock() with SendReceive() plus flags David Howells
2025-08-06 20:36 ` [RFC PATCH 13/31] cifs: Institute message managing struct David Howells
2025-08-06 20:36 ` [RFC PATCH 14/31] cifs: Split crypt_message() into encrypt and decrypt variants David Howells
2025-08-06 20:36 ` [RFC PATCH 15/31] cifs: Use netfs_alloc/free_folioq_buffer() David Howells
2025-08-06 20:36 ` [RFC PATCH 16/31] cifs: Rewrite base TCP transmission David Howells
2025-08-07  5:40   ` Stefan Metzmacher
2025-08-07 10:12   ` David Howells
2025-08-07 10:14   ` David Howells
2025-08-06 20:36 ` [RFC PATCH 17/31] cifs: Rework smb2 decryption David Howells
2025-08-06 20:36 ` [RFC PATCH 18/31] cifs: Pass smb_message structs down into the transport layer David Howells
2025-08-08 14:21   ` Enzo Matsumiya
2025-08-06 20:36 ` [RFC PATCH 19/31] cifs: Clean up mid->callback_data and kill off mid->creator David Howells
2025-08-06 20:36 ` [RFC PATCH 20/31] cifs: Don't need state locking in smb2_get_mid_entry() David Howells
2025-08-06 20:36 ` [RFC PATCH 21/31] cifs: [DEBUG] smb_message refcounting David Howells
2025-08-06 20:36 ` [RFC PATCH 22/31] cifs: Add netmem allocation functions David Howells
2025-08-06 20:36 ` [RFC PATCH 23/31] cifs: Add more pieces to smb_message David Howells
2025-08-06 20:36 ` [RFC PATCH 24/31] cifs: Convert SMB2 Negotiate Protocol request David Howells
2025-08-08 14:44   ` Enzo Matsumiya
2025-08-08 15:10   ` David Howells
2025-08-06 20:36 ` [RFC PATCH 25/31] cifs: Convert SMB2 Session Setup request David Howells
2025-08-06 20:36 ` [RFC PATCH 26/31] cifs: Convert SMB2 Logoff request David Howells
2025-08-06 20:36 ` [RFC PATCH 27/31] cifs: Convert SMB2 Tree Connect request David Howells
2025-08-06 20:36 ` [RFC PATCH 28/31] cifs: Convert SMB2 Tree Disconnect request David Howells
2025-08-06 20:36 ` [RFC PATCH 29/31] cifs: Rearrange Create request subfuncs David Howells
2025-08-06 20:36 ` [RFC PATCH 30/31] cifs: Convert SMB2 Posix Mkdir request David Howells
2025-08-06 20:36 ` [RFC PATCH 31/31] cifs: Convert SMB2 Open request David Howells
2025-08-07  5:23 ` [RFC PATCH 00/31] netfs: [WIP] Allow the use of MSG_SPLICE_PAGES and use netmem allocator Stefan Metzmacher
2025-08-07  6:24 ` David Howells
2025-08-07  6:54   ` Stefan Metzmacher
2025-08-07  7:12   ` David Howells
2025-08-08 14:15 ` Enzo Matsumiya
2025-08-08 17:25 ` David Howells
2025-08-08 19:58   ` Enzo Matsumiya
2025-08-08 20:33   ` David Howells
2025-08-10 23:29   ` David Howells
2025-08-11 12:25     ` Enzo Matsumiya

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=20250806203705.2560493-4-dhowells@redhat.com \
    --to=dhowells@redhat.com \
    --cc=almasrymina@google.com \
    --cc=linux-cifs@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=metze@samba.org \
    --cc=netfs@lists.linux.dev \
    --cc=pc@manguebit.org \
    --cc=sfrench@samba.org \
    --cc=sprasad@microsoft.com \
    --cc=tom@talpey.com \
    --cc=wangzhaolong@huaweicloud.com \
    /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

all inboxes | Powered by JetHome®