162306a36Sopenharmony_ci================ 262306a36Sopenharmony_ciCircular Buffers 362306a36Sopenharmony_ci================ 462306a36Sopenharmony_ci 562306a36Sopenharmony_ci:Author: David Howells <dhowells@redhat.com> 662306a36Sopenharmony_ci:Author: Paul E. McKenney <paulmck@linux.ibm.com> 762306a36Sopenharmony_ci 862306a36Sopenharmony_ci 962306a36Sopenharmony_ciLinux provides a number of features that can be used to implement circular 1062306a36Sopenharmony_cibuffering. There are two sets of such features: 1162306a36Sopenharmony_ci 1262306a36Sopenharmony_ci (1) Convenience functions for determining information about power-of-2 sized 1362306a36Sopenharmony_ci buffers. 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_ci (2) Memory barriers for when the producer and the consumer of objects in the 1662306a36Sopenharmony_ci buffer don't want to share a lock. 1762306a36Sopenharmony_ci 1862306a36Sopenharmony_ciTo use these facilities, as discussed below, there needs to be just one 1962306a36Sopenharmony_ciproducer and just one consumer. It is possible to handle multiple producers by 2062306a36Sopenharmony_ciserialising them, and to handle multiple consumers by serialising them. 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_ci 2362306a36Sopenharmony_ci.. Contents: 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_ci (*) What is a circular buffer? 2662306a36Sopenharmony_ci 2762306a36Sopenharmony_ci (*) Measuring power-of-2 buffers. 2862306a36Sopenharmony_ci 2962306a36Sopenharmony_ci (*) Using memory barriers with circular buffers. 3062306a36Sopenharmony_ci - The producer. 3162306a36Sopenharmony_ci - The consumer. 3262306a36Sopenharmony_ci 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_ciWhat is a circular buffer? 3662306a36Sopenharmony_ci========================== 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_ciFirst of all, what is a circular buffer? A circular buffer is a buffer of 3962306a36Sopenharmony_cifixed, finite size into which there are two indices: 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_ci (1) A 'head' index - the point at which the producer inserts items into the 4262306a36Sopenharmony_ci buffer. 4362306a36Sopenharmony_ci 4462306a36Sopenharmony_ci (2) A 'tail' index - the point at which the consumer finds the next item in 4562306a36Sopenharmony_ci the buffer. 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_ciTypically when the tail pointer is equal to the head pointer, the buffer is 4862306a36Sopenharmony_ciempty; and the buffer is full when the head pointer is one less than the tail 4962306a36Sopenharmony_cipointer. 5062306a36Sopenharmony_ci 5162306a36Sopenharmony_ciThe head index is incremented when items are added, and the tail index when 5262306a36Sopenharmony_ciitems are removed. The tail index should never jump the head index, and both 5362306a36Sopenharmony_ciindices should be wrapped to 0 when they reach the end of the buffer, thus 5462306a36Sopenharmony_ciallowing an infinite amount of data to flow through the buffer. 5562306a36Sopenharmony_ci 5662306a36Sopenharmony_ciTypically, items will all be of the same unit size, but this isn't strictly 5762306a36Sopenharmony_cirequired to use the techniques below. The indices can be increased by more 5862306a36Sopenharmony_cithan 1 if multiple items or variable-sized items are to be included in the 5962306a36Sopenharmony_cibuffer, provided that neither index overtakes the other. The implementer must 6062306a36Sopenharmony_cibe careful, however, as a region more than one unit in size may wrap the end of 6162306a36Sopenharmony_cithe buffer and be broken into two segments. 6262306a36Sopenharmony_ci 6362306a36Sopenharmony_ciMeasuring power-of-2 buffers 6462306a36Sopenharmony_ci============================ 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ciCalculation of the occupancy or the remaining capacity of an arbitrarily sized 6762306a36Sopenharmony_cicircular buffer would normally be a slow operation, requiring the use of a 6862306a36Sopenharmony_cimodulus (divide) instruction. However, if the buffer is of a power-of-2 size, 6962306a36Sopenharmony_cithen a much quicker bitwise-AND instruction can be used instead. 7062306a36Sopenharmony_ci 7162306a36Sopenharmony_ciLinux provides a set of macros for handling power-of-2 circular buffers. These 7262306a36Sopenharmony_cican be made use of by:: 7362306a36Sopenharmony_ci 7462306a36Sopenharmony_ci #include <linux/circ_buf.h> 7562306a36Sopenharmony_ci 7662306a36Sopenharmony_ciThe macros are: 7762306a36Sopenharmony_ci 7862306a36Sopenharmony_ci (#) Measure the remaining capacity of a buffer:: 7962306a36Sopenharmony_ci 8062306a36Sopenharmony_ci CIRC_SPACE(head_index, tail_index, buffer_size); 8162306a36Sopenharmony_ci 8262306a36Sopenharmony_ci This returns the amount of space left in the buffer[1] into which items 8362306a36Sopenharmony_ci can be inserted. 8462306a36Sopenharmony_ci 8562306a36Sopenharmony_ci 8662306a36Sopenharmony_ci (#) Measure the maximum consecutive immediate space in a buffer:: 8762306a36Sopenharmony_ci 8862306a36Sopenharmony_ci CIRC_SPACE_TO_END(head_index, tail_index, buffer_size); 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ci This returns the amount of consecutive space left in the buffer[1] into 9162306a36Sopenharmony_ci which items can be immediately inserted without having to wrap back to the 9262306a36Sopenharmony_ci beginning of the buffer. 9362306a36Sopenharmony_ci 9462306a36Sopenharmony_ci 9562306a36Sopenharmony_ci (#) Measure the occupancy of a buffer:: 9662306a36Sopenharmony_ci 9762306a36Sopenharmony_ci CIRC_CNT(head_index, tail_index, buffer_size); 9862306a36Sopenharmony_ci 9962306a36Sopenharmony_ci This returns the number of items currently occupying a buffer[2]. 10062306a36Sopenharmony_ci 10162306a36Sopenharmony_ci 10262306a36Sopenharmony_ci (#) Measure the non-wrapping occupancy of a buffer:: 10362306a36Sopenharmony_ci 10462306a36Sopenharmony_ci CIRC_CNT_TO_END(head_index, tail_index, buffer_size); 10562306a36Sopenharmony_ci 10662306a36Sopenharmony_ci This returns the number of consecutive items[2] that can be extracted from 10762306a36Sopenharmony_ci the buffer without having to wrap back to the beginning of the buffer. 10862306a36Sopenharmony_ci 10962306a36Sopenharmony_ci 11062306a36Sopenharmony_ciEach of these macros will nominally return a value between 0 and buffer_size-1, 11162306a36Sopenharmony_cihowever: 11262306a36Sopenharmony_ci 11362306a36Sopenharmony_ci (1) CIRC_SPACE*() are intended to be used in the producer. To the producer 11462306a36Sopenharmony_ci they will return a lower bound as the producer controls the head index, 11562306a36Sopenharmony_ci but the consumer may still be depleting the buffer on another CPU and 11662306a36Sopenharmony_ci moving the tail index. 11762306a36Sopenharmony_ci 11862306a36Sopenharmony_ci To the consumer it will show an upper bound as the producer may be busy 11962306a36Sopenharmony_ci depleting the space. 12062306a36Sopenharmony_ci 12162306a36Sopenharmony_ci (2) CIRC_CNT*() are intended to be used in the consumer. To the consumer they 12262306a36Sopenharmony_ci will return a lower bound as the consumer controls the tail index, but the 12362306a36Sopenharmony_ci producer may still be filling the buffer on another CPU and moving the 12462306a36Sopenharmony_ci head index. 12562306a36Sopenharmony_ci 12662306a36Sopenharmony_ci To the producer it will show an upper bound as the consumer may be busy 12762306a36Sopenharmony_ci emptying the buffer. 12862306a36Sopenharmony_ci 12962306a36Sopenharmony_ci (3) To a third party, the order in which the writes to the indices by the 13062306a36Sopenharmony_ci producer and consumer become visible cannot be guaranteed as they are 13162306a36Sopenharmony_ci independent and may be made on different CPUs - so the result in such a 13262306a36Sopenharmony_ci situation will merely be a guess, and may even be negative. 13362306a36Sopenharmony_ci 13462306a36Sopenharmony_ciUsing memory barriers with circular buffers 13562306a36Sopenharmony_ci=========================================== 13662306a36Sopenharmony_ci 13762306a36Sopenharmony_ciBy using memory barriers in conjunction with circular buffers, you can avoid 13862306a36Sopenharmony_cithe need to: 13962306a36Sopenharmony_ci 14062306a36Sopenharmony_ci (1) use a single lock to govern access to both ends of the buffer, thus 14162306a36Sopenharmony_ci allowing the buffer to be filled and emptied at the same time; and 14262306a36Sopenharmony_ci 14362306a36Sopenharmony_ci (2) use atomic counter operations. 14462306a36Sopenharmony_ci 14562306a36Sopenharmony_ciThere are two sides to this: the producer that fills the buffer, and the 14662306a36Sopenharmony_ciconsumer that empties it. Only one thing should be filling a buffer at any one 14762306a36Sopenharmony_citime, and only one thing should be emptying a buffer at any one time, but the 14862306a36Sopenharmony_citwo sides can operate simultaneously. 14962306a36Sopenharmony_ci 15062306a36Sopenharmony_ci 15162306a36Sopenharmony_ciThe producer 15262306a36Sopenharmony_ci------------ 15362306a36Sopenharmony_ci 15462306a36Sopenharmony_ciThe producer will look something like this:: 15562306a36Sopenharmony_ci 15662306a36Sopenharmony_ci spin_lock(&producer_lock); 15762306a36Sopenharmony_ci 15862306a36Sopenharmony_ci unsigned long head = buffer->head; 15962306a36Sopenharmony_ci /* The spin_unlock() and next spin_lock() provide needed ordering. */ 16062306a36Sopenharmony_ci unsigned long tail = READ_ONCE(buffer->tail); 16162306a36Sopenharmony_ci 16262306a36Sopenharmony_ci if (CIRC_SPACE(head, tail, buffer->size) >= 1) { 16362306a36Sopenharmony_ci /* insert one item into the buffer */ 16462306a36Sopenharmony_ci struct item *item = buffer[head]; 16562306a36Sopenharmony_ci 16662306a36Sopenharmony_ci produce_item(item); 16762306a36Sopenharmony_ci 16862306a36Sopenharmony_ci smp_store_release(buffer->head, 16962306a36Sopenharmony_ci (head + 1) & (buffer->size - 1)); 17062306a36Sopenharmony_ci 17162306a36Sopenharmony_ci /* wake_up() will make sure that the head is committed before 17262306a36Sopenharmony_ci * waking anyone up */ 17362306a36Sopenharmony_ci wake_up(consumer); 17462306a36Sopenharmony_ci } 17562306a36Sopenharmony_ci 17662306a36Sopenharmony_ci spin_unlock(&producer_lock); 17762306a36Sopenharmony_ci 17862306a36Sopenharmony_ciThis will instruct the CPU that the contents of the new item must be written 17962306a36Sopenharmony_cibefore the head index makes it available to the consumer and then instructs the 18062306a36Sopenharmony_ciCPU that the revised head index must be written before the consumer is woken. 18162306a36Sopenharmony_ci 18262306a36Sopenharmony_ciNote that wake_up() does not guarantee any sort of barrier unless something 18362306a36Sopenharmony_ciis actually awakened. We therefore cannot rely on it for ordering. However, 18462306a36Sopenharmony_cithere is always one element of the array left empty. Therefore, the 18562306a36Sopenharmony_ciproducer must produce two elements before it could possibly corrupt the 18662306a36Sopenharmony_cielement currently being read by the consumer. Therefore, the unlock-lock 18762306a36Sopenharmony_cipair between consecutive invocations of the consumer provides the necessary 18862306a36Sopenharmony_ciordering between the read of the index indicating that the consumer has 18962306a36Sopenharmony_civacated a given element and the write by the producer to that same element. 19062306a36Sopenharmony_ci 19162306a36Sopenharmony_ci 19262306a36Sopenharmony_ciThe Consumer 19362306a36Sopenharmony_ci------------ 19462306a36Sopenharmony_ci 19562306a36Sopenharmony_ciThe consumer will look something like this:: 19662306a36Sopenharmony_ci 19762306a36Sopenharmony_ci spin_lock(&consumer_lock); 19862306a36Sopenharmony_ci 19962306a36Sopenharmony_ci /* Read index before reading contents at that index. */ 20062306a36Sopenharmony_ci unsigned long head = smp_load_acquire(buffer->head); 20162306a36Sopenharmony_ci unsigned long tail = buffer->tail; 20262306a36Sopenharmony_ci 20362306a36Sopenharmony_ci if (CIRC_CNT(head, tail, buffer->size) >= 1) { 20462306a36Sopenharmony_ci 20562306a36Sopenharmony_ci /* extract one item from the buffer */ 20662306a36Sopenharmony_ci struct item *item = buffer[tail]; 20762306a36Sopenharmony_ci 20862306a36Sopenharmony_ci consume_item(item); 20962306a36Sopenharmony_ci 21062306a36Sopenharmony_ci /* Finish reading descriptor before incrementing tail. */ 21162306a36Sopenharmony_ci smp_store_release(buffer->tail, 21262306a36Sopenharmony_ci (tail + 1) & (buffer->size - 1)); 21362306a36Sopenharmony_ci } 21462306a36Sopenharmony_ci 21562306a36Sopenharmony_ci spin_unlock(&consumer_lock); 21662306a36Sopenharmony_ci 21762306a36Sopenharmony_ciThis will instruct the CPU to make sure the index is up to date before reading 21862306a36Sopenharmony_cithe new item, and then it shall make sure the CPU has finished reading the item 21962306a36Sopenharmony_cibefore it writes the new tail pointer, which will erase the item. 22062306a36Sopenharmony_ci 22162306a36Sopenharmony_ciNote the use of READ_ONCE() and smp_load_acquire() to read the 22262306a36Sopenharmony_ciopposition index. This prevents the compiler from discarding and 22362306a36Sopenharmony_cireloading its cached value. This isn't strictly needed if you can 22462306a36Sopenharmony_cibe sure that the opposition index will _only_ be used the once. 22562306a36Sopenharmony_ciThe smp_load_acquire() additionally forces the CPU to order against 22662306a36Sopenharmony_cisubsequent memory references. Similarly, smp_store_release() is used 22762306a36Sopenharmony_ciin both algorithms to write the thread's index. This documents the 22862306a36Sopenharmony_cifact that we are writing to something that can be read concurrently, 22962306a36Sopenharmony_ciprevents the compiler from tearing the store, and enforces ordering 23062306a36Sopenharmony_ciagainst previous accesses. 23162306a36Sopenharmony_ci 23262306a36Sopenharmony_ci 23362306a36Sopenharmony_ciFurther reading 23462306a36Sopenharmony_ci=============== 23562306a36Sopenharmony_ci 23662306a36Sopenharmony_ciSee also Documentation/memory-barriers.txt for a description of Linux's memory 23762306a36Sopenharmony_cibarrier facilities. 238