xref: /linux/fs/netfs/rolling_buffer.c (revision cf85f810f911234a06a4ef2439e8694b93b717fc)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Rolling buffer helpers
3  *
4  * Copyright (C) 2024 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7 
8 #include <linux/bitops.h>
9 #include <linux/mempool.h>
10 #include <linux/pagemap.h>
11 #include <linux/rolling_buffer.h>
12 #include <linux/slab.h>
13 #include "internal.h"
14 
15 static atomic_t debug_ids;
16 
17 /**
18  * netfs_folioq_alloc - Allocate a folio_queue struct
19  * @rreq_id: Associated debugging ID for tracing purposes
20  * @gfp: Allocation constraints
21  * @trace: Trace tag to indicate the purpose of the allocation
22  *
23  * Allocate, initialise and account the folio_queue struct and log a trace line
24  * to mark the allocation.
25  */
26 struct folio_queue *netfs_folioq_alloc(unsigned int rreq_id, gfp_t gfp,
27 				       unsigned int /*enum netfs_folioq_trace*/ trace)
28 {
29 	struct folio_queue *fq;
30 
31 	if (gfp == GFP_KERNEL)
32 		fq = netfs_folioq_pool.alloc(gfp, netfs_folioq_pool.pool_data);
33 	else
34 		fq = mempool_alloc(&netfs_folioq_pool, gfp);
35 	if (fq) {
36 		netfs_stat(&netfs_n_folioq);
37 		folioq_init(fq, rreq_id);
38 		fq->debug_id = atomic_inc_return(&debug_ids);
39 		trace_netfs_folioq(fq, trace);
40 	}
41 	return fq;
42 }
43 EXPORT_SYMBOL(netfs_folioq_alloc);
44 
45 /**
46  * netfs_folioq_free - Free a folio_queue struct
47  * @folioq: The object to free
48  * @trace: Trace tag to indicate which free
49  *
50  * Free and unaccount the folio_queue struct.
51  */
52 void netfs_folioq_free(struct folio_queue *folioq,
53 		       unsigned int /*enum netfs_trace_folioq*/ trace)
54 {
55 	trace_netfs_folioq(folioq, trace);
56 	netfs_stat_d(&netfs_n_folioq);
57 	mempool_free(folioq, &netfs_folioq_pool);
58 }
59 EXPORT_SYMBOL(netfs_folioq_free);
60 
61 /*
62  * Initialise a rolling buffer.  We allocate an empty folio queue struct to so
63  * that the pointers can be independently driven by the producer and the
64  * consumer.
65  */
66 int rolling_buffer_init(struct rolling_buffer *roll, unsigned int rreq_id,
67 			unsigned int direction, gfp_t gfp)
68 {
69 	struct folio_queue *fq;
70 
71 	fq = netfs_folioq_alloc(rreq_id, gfp, netfs_trace_folioq_rollbuf_init);
72 	if (!fq)
73 		return -ENOMEM;
74 
75 	roll->head = fq;
76 	roll->tail = fq;
77 	iov_iter_folio_queue(&roll->iter, direction, fq, 0, 0, 0);
78 	return 0;
79 }
80 
81 /*
82  * Add another folio_queue to a rolling buffer if there's no space left.
83  */
84 int rolling_buffer_make_space(struct rolling_buffer *roll, gfp_t gfp)
85 {
86 	struct folio_queue *fq, *head = roll->head;
87 
88 	if (!folioq_full(head))
89 		return 0;
90 
91 	fq = netfs_folioq_alloc(head->rreq_id, gfp, netfs_trace_folioq_make_space);
92 	if (!fq)
93 		return -ENOMEM;
94 	fq->prev = head;
95 
96 	roll->head = fq;
97 	if (folioq_full(head)) {
98 		/* Make sure we don't leave the master iterator pointing to a
99 		 * block that might get immediately consumed.
100 		 */
101 		if (roll->iter.folioq == head &&
102 		    roll->iter.folioq_slot == folioq_nr_slots(head)) {
103 			roll->iter.folioq = fq;
104 			roll->iter.folioq_slot = 0;
105 		}
106 	}
107 
108 	/* Make sure the initialisation is stored before the next pointer.
109 	 *
110 	 * [!] NOTE: After we set head->next, the consumer is at liberty to
111 	 * immediately delete the old head.
112 	 */
113 	smp_store_release(&head->next, fq);
114 	return 0;
115 }
116 
117 /*
118  * Decant the list of folios to read into a rolling buffer.
119  */
120 ssize_t rolling_buffer_load_from_ra(struct rolling_buffer *roll,
121 				    struct readahead_control *ractl,
122 				    struct folio_batch *put_batch)
123 {
124 	struct folio_queue *fq;
125 	struct page **vec;
126 	int nr, ix, to;
127 	ssize_t size = 0;
128 
129 	if (rolling_buffer_make_space(roll, GFP_KERNEL) < 0)
130 		return -ENOMEM;
131 
132 	fq = roll->head;
133 	vec = (struct page **)fq->vec.folios;
134 	nr = __readahead_batch(ractl, vec + folio_batch_count(&fq->vec),
135 			       folio_batch_space(&fq->vec));
136 	ix = fq->vec.nr;
137 	to = ix + nr;
138 	fq->vec.nr = to;
139 	for (; ix < to; ix++) {
140 		struct folio *folio = folioq_folio(fq, ix);
141 		unsigned int order = folio_order(folio);
142 
143 		fq->orders[ix] = order;
144 		size += PAGE_SIZE << order;
145 		trace_netfs_folio(folio, netfs_folio_trace_read);
146 		if (!folio_batch_add(put_batch, folio))
147 			folio_batch_release(put_batch);
148 	}
149 	WRITE_ONCE(roll->iter.count, roll->iter.count + size);
150 
151 	/* Store the counter after setting the slot. */
152 	smp_store_release(&roll->next_head_slot, to);
153 	return size;
154 }
155 
156 /*
157  * Append a folio to the rolling buffer.
158  */
159 ssize_t rolling_buffer_append(struct rolling_buffer *roll, struct folio *folio,
160 			      unsigned int flags, gfp_t gfp)
161 {
162 	ssize_t size = folio_size(folio);
163 	int slot;
164 
165 	if (rolling_buffer_make_space(roll, gfp) < 0)
166 		return -ENOMEM;
167 
168 	slot = folioq_append(roll->head, folio);
169 	if (flags & ROLLBUF_MARK_1)
170 		folioq_mark(roll->head, slot);
171 	if (flags & ROLLBUF_MARK_2)
172 		folioq_mark2(roll->head, slot);
173 
174 	WRITE_ONCE(roll->iter.count, roll->iter.count + size);
175 
176 	/* Store the counter after setting the slot. */
177 	smp_store_release(&roll->next_head_slot, slot);
178 	return size;
179 }
180 
181 /*
182  * Delete a spent buffer from a rolling queue and return the next in line.  We
183  * don't return the last buffer to keep the pointers independent, but return
184  * NULL instead.
185  */
186 struct folio_queue *rolling_buffer_delete_spent(struct rolling_buffer *roll)
187 {
188 	struct folio_queue *spent = roll->tail, *next = READ_ONCE(spent->next);
189 
190 	if (!next)
191 		return NULL;
192 	next->prev = NULL;
193 	netfs_folioq_free(spent, netfs_trace_folioq_delete);
194 	roll->tail = next;
195 	return next;
196 }
197 
198 /*
199  * Clear out a rolling queue.  Folios that have mark 1 set are put.
200  */
201 void rolling_buffer_clear(struct rolling_buffer *roll)
202 {
203 	struct folio_batch fbatch;
204 	struct folio_queue *p;
205 
206 	folio_batch_init(&fbatch);
207 
208 	while ((p = roll->tail)) {
209 		roll->tail = p->next;
210 		for (int slot = 0; slot < folioq_count(p); slot++) {
211 			struct folio *folio = folioq_folio(p, slot);
212 
213 			if (!folio)
214 				continue;
215 			if (folioq_is_marked(p, slot)) {
216 				trace_netfs_folio(folio, netfs_folio_trace_put);
217 				if (!folio_batch_add(&fbatch, folio))
218 					folio_batch_release(&fbatch);
219 			}
220 		}
221 
222 		netfs_folioq_free(p, netfs_trace_folioq_clear);
223 	}
224 
225 	folio_batch_release(&fbatch);
226 }
227