xref: /openbmc/linux/Documentation/core-api/circular-buffers.rst (revision 0898782247ae533d1f4e47a06bc5d4870931b284)
1d8a121e3SMauro Carvalho Chehab================
2d8a121e3SMauro Carvalho ChehabCircular Buffers
3d8a121e3SMauro Carvalho Chehab================
4d8a121e3SMauro Carvalho Chehab
5d8a121e3SMauro Carvalho Chehab:Author: David Howells <dhowells@redhat.com>
6*714b6904SPaul E. McKenney:Author: Paul E. McKenney <paulmck@linux.ibm.com>
7d8a121e3SMauro Carvalho Chehab
8d8a121e3SMauro Carvalho Chehab
9d8a121e3SMauro Carvalho ChehabLinux provides a number of features that can be used to implement circular
10d8a121e3SMauro Carvalho Chehabbuffering.  There are two sets of such features:
11d8a121e3SMauro Carvalho Chehab
12d8a121e3SMauro Carvalho Chehab (1) Convenience functions for determining information about power-of-2 sized
13d8a121e3SMauro Carvalho Chehab     buffers.
14d8a121e3SMauro Carvalho Chehab
15d8a121e3SMauro Carvalho Chehab (2) Memory barriers for when the producer and the consumer of objects in the
16d8a121e3SMauro Carvalho Chehab     buffer don't want to share a lock.
17d8a121e3SMauro Carvalho Chehab
18d8a121e3SMauro Carvalho ChehabTo use these facilities, as discussed below, there needs to be just one
19d8a121e3SMauro Carvalho Chehabproducer and just one consumer.  It is possible to handle multiple producers by
20d8a121e3SMauro Carvalho Chehabserialising them, and to handle multiple consumers by serialising them.
21d8a121e3SMauro Carvalho Chehab
22d8a121e3SMauro Carvalho Chehab
23d8a121e3SMauro Carvalho Chehab.. Contents:
24d8a121e3SMauro Carvalho Chehab
25d8a121e3SMauro Carvalho Chehab (*) What is a circular buffer?
26d8a121e3SMauro Carvalho Chehab
27d8a121e3SMauro Carvalho Chehab (*) Measuring power-of-2 buffers.
28d8a121e3SMauro Carvalho Chehab
29d8a121e3SMauro Carvalho Chehab (*) Using memory barriers with circular buffers.
30d8a121e3SMauro Carvalho Chehab     - The producer.
31d8a121e3SMauro Carvalho Chehab     - The consumer.
32d8a121e3SMauro Carvalho Chehab
33d8a121e3SMauro Carvalho Chehab
34d8a121e3SMauro Carvalho Chehab
35d8a121e3SMauro Carvalho ChehabWhat is a circular buffer?
36d8a121e3SMauro Carvalho Chehab==========================
37d8a121e3SMauro Carvalho Chehab
38d8a121e3SMauro Carvalho ChehabFirst of all, what is a circular buffer?  A circular buffer is a buffer of
39d8a121e3SMauro Carvalho Chehabfixed, finite size into which there are two indices:
40d8a121e3SMauro Carvalho Chehab
41d8a121e3SMauro Carvalho Chehab (1) A 'head' index - the point at which the producer inserts items into the
42d8a121e3SMauro Carvalho Chehab     buffer.
43d8a121e3SMauro Carvalho Chehab
44d8a121e3SMauro Carvalho Chehab (2) A 'tail' index - the point at which the consumer finds the next item in
45d8a121e3SMauro Carvalho Chehab     the buffer.
46d8a121e3SMauro Carvalho Chehab
47d8a121e3SMauro Carvalho ChehabTypically when the tail pointer is equal to the head pointer, the buffer is
48d8a121e3SMauro Carvalho Chehabempty; and the buffer is full when the head pointer is one less than the tail
49d8a121e3SMauro Carvalho Chehabpointer.
50d8a121e3SMauro Carvalho Chehab
51d8a121e3SMauro Carvalho ChehabThe head index is incremented when items are added, and the tail index when
52d8a121e3SMauro Carvalho Chehabitems are removed.  The tail index should never jump the head index, and both
53d8a121e3SMauro Carvalho Chehabindices should be wrapped to 0 when they reach the end of the buffer, thus
54d8a121e3SMauro Carvalho Chehaballowing an infinite amount of data to flow through the buffer.
55d8a121e3SMauro Carvalho Chehab
56d8a121e3SMauro Carvalho ChehabTypically, items will all be of the same unit size, but this isn't strictly
57d8a121e3SMauro Carvalho Chehabrequired to use the techniques below.  The indices can be increased by more
58d8a121e3SMauro Carvalho Chehabthan 1 if multiple items or variable-sized items are to be included in the
59d8a121e3SMauro Carvalho Chehabbuffer, provided that neither index overtakes the other.  The implementer must
60d8a121e3SMauro Carvalho Chehabbe careful, however, as a region more than one unit in size may wrap the end of
61d8a121e3SMauro Carvalho Chehabthe buffer and be broken into two segments.
62d8a121e3SMauro Carvalho Chehab
63d8a121e3SMauro Carvalho ChehabMeasuring power-of-2 buffers
64d8a121e3SMauro Carvalho Chehab============================
65d8a121e3SMauro Carvalho Chehab
66d8a121e3SMauro Carvalho ChehabCalculation of the occupancy or the remaining capacity of an arbitrarily sized
67d8a121e3SMauro Carvalho Chehabcircular buffer would normally be a slow operation, requiring the use of a
68d8a121e3SMauro Carvalho Chehabmodulus (divide) instruction.  However, if the buffer is of a power-of-2 size,
69d8a121e3SMauro Carvalho Chehabthen a much quicker bitwise-AND instruction can be used instead.
70d8a121e3SMauro Carvalho Chehab
71d8a121e3SMauro Carvalho ChehabLinux provides a set of macros for handling power-of-2 circular buffers.  These
72d8a121e3SMauro Carvalho Chehabcan be made use of by::
73d8a121e3SMauro Carvalho Chehab
74d8a121e3SMauro Carvalho Chehab	#include <linux/circ_buf.h>
75d8a121e3SMauro Carvalho Chehab
76d8a121e3SMauro Carvalho ChehabThe macros are:
77d8a121e3SMauro Carvalho Chehab
78d8a121e3SMauro Carvalho Chehab (#) Measure the remaining capacity of a buffer::
79d8a121e3SMauro Carvalho Chehab
80d8a121e3SMauro Carvalho Chehab	CIRC_SPACE(head_index, tail_index, buffer_size);
81d8a121e3SMauro Carvalho Chehab
82d8a121e3SMauro Carvalho Chehab     This returns the amount of space left in the buffer[1] into which items
83d8a121e3SMauro Carvalho Chehab     can be inserted.
84d8a121e3SMauro Carvalho Chehab
85d8a121e3SMauro Carvalho Chehab
86d8a121e3SMauro Carvalho Chehab (#) Measure the maximum consecutive immediate space in a buffer::
87d8a121e3SMauro Carvalho Chehab
88d8a121e3SMauro Carvalho Chehab	CIRC_SPACE_TO_END(head_index, tail_index, buffer_size);
89d8a121e3SMauro Carvalho Chehab
90d8a121e3SMauro Carvalho Chehab     This returns the amount of consecutive space left in the buffer[1] into
91d8a121e3SMauro Carvalho Chehab     which items can be immediately inserted without having to wrap back to the
92d8a121e3SMauro Carvalho Chehab     beginning of the buffer.
93d8a121e3SMauro Carvalho Chehab
94d8a121e3SMauro Carvalho Chehab
95d8a121e3SMauro Carvalho Chehab (#) Measure the occupancy of a buffer::
96d8a121e3SMauro Carvalho Chehab
97d8a121e3SMauro Carvalho Chehab	CIRC_CNT(head_index, tail_index, buffer_size);
98d8a121e3SMauro Carvalho Chehab
99d8a121e3SMauro Carvalho Chehab     This returns the number of items currently occupying a buffer[2].
100d8a121e3SMauro Carvalho Chehab
101d8a121e3SMauro Carvalho Chehab
102d8a121e3SMauro Carvalho Chehab (#) Measure the non-wrapping occupancy of a buffer::
103d8a121e3SMauro Carvalho Chehab
104d8a121e3SMauro Carvalho Chehab	CIRC_CNT_TO_END(head_index, tail_index, buffer_size);
105d8a121e3SMauro Carvalho Chehab
106d8a121e3SMauro Carvalho Chehab     This returns the number of consecutive items[2] that can be extracted from
107d8a121e3SMauro Carvalho Chehab     the buffer without having to wrap back to the beginning of the buffer.
108d8a121e3SMauro Carvalho Chehab
109d8a121e3SMauro Carvalho Chehab
110d8a121e3SMauro Carvalho ChehabEach of these macros will nominally return a value between 0 and buffer_size-1,
111d8a121e3SMauro Carvalho Chehabhowever:
112d8a121e3SMauro Carvalho Chehab
113d8a121e3SMauro Carvalho Chehab (1) CIRC_SPACE*() are intended to be used in the producer.  To the producer
114d8a121e3SMauro Carvalho Chehab     they will return a lower bound as the producer controls the head index,
115d8a121e3SMauro Carvalho Chehab     but the consumer may still be depleting the buffer on another CPU and
116d8a121e3SMauro Carvalho Chehab     moving the tail index.
117d8a121e3SMauro Carvalho Chehab
118d8a121e3SMauro Carvalho Chehab     To the consumer it will show an upper bound as the producer may be busy
119d8a121e3SMauro Carvalho Chehab     depleting the space.
120d8a121e3SMauro Carvalho Chehab
121d8a121e3SMauro Carvalho Chehab (2) CIRC_CNT*() are intended to be used in the consumer.  To the consumer they
122d8a121e3SMauro Carvalho Chehab     will return a lower bound as the consumer controls the tail index, but the
123d8a121e3SMauro Carvalho Chehab     producer may still be filling the buffer on another CPU and moving the
124d8a121e3SMauro Carvalho Chehab     head index.
125d8a121e3SMauro Carvalho Chehab
126d8a121e3SMauro Carvalho Chehab     To the producer it will show an upper bound as the consumer may be busy
127d8a121e3SMauro Carvalho Chehab     emptying the buffer.
128d8a121e3SMauro Carvalho Chehab
129d8a121e3SMauro Carvalho Chehab (3) To a third party, the order in which the writes to the indices by the
130d8a121e3SMauro Carvalho Chehab     producer and consumer become visible cannot be guaranteed as they are
131d8a121e3SMauro Carvalho Chehab     independent and may be made on different CPUs - so the result in such a
132d8a121e3SMauro Carvalho Chehab     situation will merely be a guess, and may even be negative.
133d8a121e3SMauro Carvalho Chehab
134d8a121e3SMauro Carvalho ChehabUsing memory barriers with circular buffers
135d8a121e3SMauro Carvalho Chehab===========================================
136d8a121e3SMauro Carvalho Chehab
137d8a121e3SMauro Carvalho ChehabBy using memory barriers in conjunction with circular buffers, you can avoid
138d8a121e3SMauro Carvalho Chehabthe need to:
139d8a121e3SMauro Carvalho Chehab
140d8a121e3SMauro Carvalho Chehab (1) use a single lock to govern access to both ends of the buffer, thus
141d8a121e3SMauro Carvalho Chehab     allowing the buffer to be filled and emptied at the same time; and
142d8a121e3SMauro Carvalho Chehab
143d8a121e3SMauro Carvalho Chehab (2) use atomic counter operations.
144d8a121e3SMauro Carvalho Chehab
145d8a121e3SMauro Carvalho ChehabThere are two sides to this: the producer that fills the buffer, and the
146d8a121e3SMauro Carvalho Chehabconsumer that empties it.  Only one thing should be filling a buffer at any one
147d8a121e3SMauro Carvalho Chehabtime, and only one thing should be emptying a buffer at any one time, but the
148d8a121e3SMauro Carvalho Chehabtwo sides can operate simultaneously.
149d8a121e3SMauro Carvalho Chehab
150d8a121e3SMauro Carvalho Chehab
151d8a121e3SMauro Carvalho ChehabThe producer
152d8a121e3SMauro Carvalho Chehab------------
153d8a121e3SMauro Carvalho Chehab
154d8a121e3SMauro Carvalho ChehabThe producer will look something like this::
155d8a121e3SMauro Carvalho Chehab
156d8a121e3SMauro Carvalho Chehab	spin_lock(&producer_lock);
157d8a121e3SMauro Carvalho Chehab
158d8a121e3SMauro Carvalho Chehab	unsigned long head = buffer->head;
159d8a121e3SMauro Carvalho Chehab	/* The spin_unlock() and next spin_lock() provide needed ordering. */
160d8a121e3SMauro Carvalho Chehab	unsigned long tail = READ_ONCE(buffer->tail);
161d8a121e3SMauro Carvalho Chehab
162d8a121e3SMauro Carvalho Chehab	if (CIRC_SPACE(head, tail, buffer->size) >= 1) {
163d8a121e3SMauro Carvalho Chehab		/* insert one item into the buffer */
164d8a121e3SMauro Carvalho Chehab		struct item *item = buffer[head];
165d8a121e3SMauro Carvalho Chehab
166d8a121e3SMauro Carvalho Chehab		produce_item(item);
167d8a121e3SMauro Carvalho Chehab
168d8a121e3SMauro Carvalho Chehab		smp_store_release(buffer->head,
169d8a121e3SMauro Carvalho Chehab				  (head + 1) & (buffer->size - 1));
170d8a121e3SMauro Carvalho Chehab
171d8a121e3SMauro Carvalho Chehab		/* wake_up() will make sure that the head is committed before
172d8a121e3SMauro Carvalho Chehab		 * waking anyone up */
173d8a121e3SMauro Carvalho Chehab		wake_up(consumer);
174d8a121e3SMauro Carvalho Chehab	}
175d8a121e3SMauro Carvalho Chehab
176d8a121e3SMauro Carvalho Chehab	spin_unlock(&producer_lock);
177d8a121e3SMauro Carvalho Chehab
178d8a121e3SMauro Carvalho ChehabThis will instruct the CPU that the contents of the new item must be written
179d8a121e3SMauro Carvalho Chehabbefore the head index makes it available to the consumer and then instructs the
180d8a121e3SMauro Carvalho ChehabCPU that the revised head index must be written before the consumer is woken.
181d8a121e3SMauro Carvalho Chehab
182d8a121e3SMauro Carvalho ChehabNote that wake_up() does not guarantee any sort of barrier unless something
183d8a121e3SMauro Carvalho Chehabis actually awakened.  We therefore cannot rely on it for ordering.  However,
184d8a121e3SMauro Carvalho Chehabthere is always one element of the array left empty.  Therefore, the
185d8a121e3SMauro Carvalho Chehabproducer must produce two elements before it could possibly corrupt the
186d8a121e3SMauro Carvalho Chehabelement currently being read by the consumer.  Therefore, the unlock-lock
187d8a121e3SMauro Carvalho Chehabpair between consecutive invocations of the consumer provides the necessary
188d8a121e3SMauro Carvalho Chehabordering between the read of the index indicating that the consumer has
189d8a121e3SMauro Carvalho Chehabvacated a given element and the write by the producer to that same element.
190d8a121e3SMauro Carvalho Chehab
191d8a121e3SMauro Carvalho Chehab
192d8a121e3SMauro Carvalho ChehabThe Consumer
193d8a121e3SMauro Carvalho Chehab------------
194d8a121e3SMauro Carvalho Chehab
195d8a121e3SMauro Carvalho ChehabThe consumer will look something like this::
196d8a121e3SMauro Carvalho Chehab
197d8a121e3SMauro Carvalho Chehab	spin_lock(&consumer_lock);
198d8a121e3SMauro Carvalho Chehab
199d8a121e3SMauro Carvalho Chehab	/* Read index before reading contents at that index. */
200d8a121e3SMauro Carvalho Chehab	unsigned long head = smp_load_acquire(buffer->head);
201d8a121e3SMauro Carvalho Chehab	unsigned long tail = buffer->tail;
202d8a121e3SMauro Carvalho Chehab
203d8a121e3SMauro Carvalho Chehab	if (CIRC_CNT(head, tail, buffer->size) >= 1) {
204d8a121e3SMauro Carvalho Chehab
205d8a121e3SMauro Carvalho Chehab		/* extract one item from the buffer */
206d8a121e3SMauro Carvalho Chehab		struct item *item = buffer[tail];
207d8a121e3SMauro Carvalho Chehab
208d8a121e3SMauro Carvalho Chehab		consume_item(item);
209d8a121e3SMauro Carvalho Chehab
210d8a121e3SMauro Carvalho Chehab		/* Finish reading descriptor before incrementing tail. */
211d8a121e3SMauro Carvalho Chehab		smp_store_release(buffer->tail,
212d8a121e3SMauro Carvalho Chehab				  (tail + 1) & (buffer->size - 1));
213d8a121e3SMauro Carvalho Chehab	}
214d8a121e3SMauro Carvalho Chehab
215d8a121e3SMauro Carvalho Chehab	spin_unlock(&consumer_lock);
216d8a121e3SMauro Carvalho Chehab
217d8a121e3SMauro Carvalho ChehabThis will instruct the CPU to make sure the index is up to date before reading
218d8a121e3SMauro Carvalho Chehabthe new item, and then it shall make sure the CPU has finished reading the item
219d8a121e3SMauro Carvalho Chehabbefore it writes the new tail pointer, which will erase the item.
220d8a121e3SMauro Carvalho Chehab
221d8a121e3SMauro Carvalho ChehabNote the use of READ_ONCE() and smp_load_acquire() to read the
222d8a121e3SMauro Carvalho Chehabopposition index.  This prevents the compiler from discarding and
223d8a121e3SMauro Carvalho Chehabreloading its cached value.  This isn't strictly needed if you can
224d8a121e3SMauro Carvalho Chehabbe sure that the opposition index will _only_ be used the once.
225d8a121e3SMauro Carvalho ChehabThe smp_load_acquire() additionally forces the CPU to order against
226d8a121e3SMauro Carvalho Chehabsubsequent memory references.  Similarly, smp_store_release() is used
227d8a121e3SMauro Carvalho Chehabin both algorithms to write the thread's index.  This documents the
228d8a121e3SMauro Carvalho Chehabfact that we are writing to something that can be read concurrently,
229d8a121e3SMauro Carvalho Chehabprevents the compiler from tearing the store, and enforces ordering
230d8a121e3SMauro Carvalho Chehabagainst previous accesses.
231d8a121e3SMauro Carvalho Chehab
232d8a121e3SMauro Carvalho Chehab
233d8a121e3SMauro Carvalho ChehabFurther reading
234d8a121e3SMauro Carvalho Chehab===============
235d8a121e3SMauro Carvalho Chehab
236d8a121e3SMauro Carvalho ChehabSee also Documentation/memory-barriers.txt for a description of Linux's memory
237d8a121e3SMauro Carvalho Chehabbarrier facilities.
238