]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
netfs: Add functions to build/clean a buffer in a folio_queue
authorDavid Howells <dhowells@redhat.com>
Mon, 16 Dec 2024 20:41:08 +0000 (20:41 +0000)
committerChristian Brauner <brauner@kernel.org>
Fri, 20 Dec 2024 21:34:06 +0000 (22:34 +0100)
Add two netfslib functions to build up or clean up a buffer in a
folio_queue.  The first, netfs_alloc_folioq_buffer() will add folios to a
buffer, extending up at least to the given size.  If it can, it will add
multipage folios.  The folios are optionally have the mapping set and will
have the index set according to the distance from the front of the folio
queue.

The second function will free up a folio queue and put any folios in the
queue that have the first mark set.

The netfs_folio tracepoint is also altered to cope with folios that have a
NULL mapping, and the folios being added/put will have trace lines emitted
and will be accounted in the stats.

Signed-off-by: David Howells <dhowells@redhat.com>
Link: https://lore.kernel.org/r/20241216204124.3752367-19-dhowells@redhat.com
cc: Jeff Layton <jlayton@kernel.org>
cc: Marc Dionne <marc.dionne@auristor.com>
cc: netfs@lists.linux.dev
cc: linux-afs@lists.infradead.org
cc: linux-fsdevel@vger.kernel.org
Signed-off-by: Christian Brauner <brauner@kernel.org>
fs/netfs/misc.c
include/linux/netfs.h
include/trace/events/netfs.h

index 4249715f4171526d2b418ad0d2a1500ef8f7180d..7099aa07737ac07179c179c4b370120d2895e917 100644 (file)
@@ -8,6 +8,102 @@
 #include <linux/swap.h>
 #include "internal.h"
 
+/**
+ * netfs_alloc_folioq_buffer - Allocate buffer space into a folio queue
+ * @mapping: Address space to set on the folio (or NULL).
+ * @_buffer: Pointer to the folio queue to add to (may point to a NULL; updated).
+ * @_cur_size: Current size of the buffer (updated).
+ * @size: Target size of the buffer.
+ * @gfp: The allocation constraints.
+ */
+int netfs_alloc_folioq_buffer(struct address_space *mapping,
+                             struct folio_queue **_buffer,
+                             size_t *_cur_size, ssize_t size, gfp_t gfp)
+{
+       struct folio_queue *tail = *_buffer, *p;
+
+       size = round_up(size, PAGE_SIZE);
+       if (*_cur_size >= size)
+               return 0;
+
+       if (tail)
+               while (tail->next)
+                       tail = tail->next;
+
+       do {
+               struct folio *folio;
+               int order = 0, slot;
+
+               if (!tail || folioq_full(tail)) {
+                       p = netfs_folioq_alloc(0, GFP_NOFS, netfs_trace_folioq_alloc_buffer);
+                       if (!p)
+                               return -ENOMEM;
+                       if (tail) {
+                               tail->next = p;
+                               p->prev = tail;
+                       } else {
+                               *_buffer = p;
+                       }
+                       tail = p;
+               }
+
+               if (size - *_cur_size > PAGE_SIZE)
+                       order = umin(ilog2(size - *_cur_size) - PAGE_SHIFT,
+                                    MAX_PAGECACHE_ORDER);
+
+               folio = folio_alloc(gfp, order);
+               if (!folio && order > 0)
+                       folio = folio_alloc(gfp, 0);
+               if (!folio)
+                       return -ENOMEM;
+
+               folio->mapping = mapping;
+               folio->index = *_cur_size / PAGE_SIZE;
+               trace_netfs_folio(folio, netfs_folio_trace_alloc_buffer);
+               slot = folioq_append_mark(tail, folio);
+               *_cur_size += folioq_folio_size(tail, slot);
+       } while (*_cur_size < size);
+
+       return 0;
+}
+EXPORT_SYMBOL(netfs_alloc_folioq_buffer);
+
+/**
+ * netfs_free_folioq_buffer - Free a folio queue.
+ * @fq: The start of the folio queue to free
+ *
+ * Free up a chain of folio_queues and, if marked, the marked folios they point
+ * to.
+ */
+void netfs_free_folioq_buffer(struct folio_queue *fq)
+{
+       struct folio_queue *next;
+       struct folio_batch fbatch;
+
+       folio_batch_init(&fbatch);
+
+       for (; fq; fq = next) {
+               for (int slot = 0; slot < folioq_count(fq); slot++) {
+                       struct folio *folio = folioq_folio(fq, slot);
+
+                       if (!folio ||
+                           !folioq_is_marked(fq, slot))
+                               continue;
+
+                       trace_netfs_folio(folio, netfs_folio_trace_put);
+                       if (folio_batch_add(&fbatch, folio))
+                               folio_batch_release(&fbatch);
+               }
+
+               netfs_stat_d(&netfs_n_folioq);
+               next = fq->next;
+               kfree(fq);
+       }
+
+       folio_batch_release(&fbatch);
+}
+EXPORT_SYMBOL(netfs_free_folioq_buffer);
+
 /*
  * Reset the subrequest iterator to refer just to the region remaining to be
  * read.  The iterator may or may not have been advanced by socket ops or
index 374e54beacbef818d815a82af4dd2b64cd1d291a..dd737344cff3a2e81d63313b0f9543c822ad4193 100644 (file)
@@ -457,6 +457,12 @@ struct folio_queue *netfs_folioq_alloc(unsigned int rreq_id, gfp_t gfp,
 void netfs_folioq_free(struct folio_queue *folioq,
                       unsigned int trace /*enum netfs_trace_folioq*/);
 
+/* Buffer wrangling helpers API. */
+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);
+
 /**
  * netfs_inode - Get the netfs inode context from the inode
  * @inode: The inode to query
index 02f6e179b7bc3e7e9fd04981642401b510c6f69f..fc237ff23a33e095fec1c5d6d671fc802e89eef6 100644 (file)
        EM(netfs_streaming_filled_page,         "mod-streamw-f") \
        EM(netfs_streaming_cont_filled_page,    "mod-streamw-f+") \
        EM(netfs_folio_trace_abandon,           "abandon")      \
+       EM(netfs_folio_trace_alloc_buffer,      "alloc-buf")    \
        EM(netfs_folio_trace_cancel_copy,       "cancel-copy")  \
        EM(netfs_folio_trace_cancel_store,      "cancel-store") \
        EM(netfs_folio_trace_clear,             "clear")        \
        E_(netfs_trace_donate_to_deferred_next, "defer-next")
 
 #define netfs_folioq_traces                                    \
-       EM(netfs_trace_folioq_alloc_append_folio, "alloc-apf")  \
-       EM(netfs_trace_folioq_alloc_read_prep,  "alloc-r-prep") \
-       EM(netfs_trace_folioq_alloc_read_prime, "alloc-r-prime") \
-       EM(netfs_trace_folioq_alloc_read_sing,  "alloc-r-sing") \
+       EM(netfs_trace_folioq_alloc_buffer,     "alloc-buf")    \
        EM(netfs_trace_folioq_clear,            "clear")        \
        EM(netfs_trace_folioq_delete,           "delete")       \
        EM(netfs_trace_folioq_make_space,       "make-space")   \