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