]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/dpdk/doc/guides/prog_guide/stack_lib.rst
import 15.2.0 Octopus source
[ceph.git] / ceph / src / spdk / dpdk / doc / guides / prog_guide / stack_lib.rst
1 .. SPDX-License-Identifier: BSD-3-Clause
2 Copyright(c) 2019 Intel Corporation.
3
4 Stack Library
5 =============
6
7 DPDK's stack library provides an API for configuration and use of a bounded
8 stack of pointers.
9
10 The stack library provides the following basic operations:
11
12 * Create a uniquely named stack of a user-specified size and using a
13 user-specified socket, with either standard (lock-based) or lock-free
14 behavior.
15
16 * Push and pop a burst of one or more stack objects (pointers). These function
17 are multi-threading safe.
18
19 * Free a previously created stack.
20
21 * Lookup a pointer to a stack by its name.
22
23 * Query a stack's current depth and number of free entries.
24
25 Implementation
26 ~~~~~~~~~~~~~~
27
28 The library supports two types of stacks: standard (lock-based) and lock-free.
29 Both types use the same set of interfaces, but their implementations differ.
30
31 Lock-based Stack
32 ----------------
33
34 The lock-based stack consists of a contiguous array of pointers, a current
35 index, and a spinlock. Accesses to the stack are made multi-thread safe by the
36 spinlock.
37
38 Lock-free Stack
39 ------------------
40
41 The lock-free stack consists of a linked list of elements, each containing a
42 data pointer and a next pointer, and an atomic stack depth counter. The
43 lock-free property means that multiple threads can push and pop simultaneously,
44 and one thread being preempted/delayed in a push or pop operation will not
45 impede the forward progress of any other thread.
46
47 The lock-free push operation enqueues a linked list of pointers by pointing the
48 list's tail to the current stack head, and using a CAS to swing the stack head
49 pointer to the head of the list. The operation retries if it is unsuccessful
50 (i.e. the list changed between reading the head and modifying it), else it
51 adjusts the stack length and returns.
52
53 The lock-free pop operation first reserves one or more list elements by
54 adjusting the stack length, to ensure the dequeue operation will succeed
55 without blocking. It then dequeues pointers by walking the list -- starting
56 from the head -- then swinging the head pointer (using a CAS as well). While
57 walking the list, the data pointers are recorded in an object table.
58
59 The linked list elements themselves are maintained in a lock-free LIFO, and are
60 allocated before stack pushes and freed after stack pops. Since the stack has a
61 fixed maximum depth, these elements do not need to be dynamically created.
62
63 The lock-free behavior is selected by passing the *RTE_STACK_F_LF* flag to
64 rte_stack_create().
65
66 Preventing the ABA Problem
67 ^^^^^^^^^^^^^^^^^^^^^^^^^^
68
69 To prevent the ABA problem, this algorithm stack uses a 128-bit
70 compare-and-swap instruction to atomically update both the stack top pointer
71 and a modification counter. The ABA problem can occur without a modification
72 counter if, for example:
73
74 1. Thread A reads head pointer X and stores the pointed-to list element.
75 2. Other threads modify the list such that the head pointer is once again X,
76 but its pointed-to data is different than what thread A read.
77 3. Thread A changes the head pointer with a compare-and-swap and succeeds.
78
79 In this case thread A would not detect that the list had changed, and would
80 both pop stale data and incorrect change the head pointer. By adding a
81 modification counter that is updated on every push and pop as part of the
82 compare-and-swap, the algorithm can detect when the list changes even if the
83 head pointer remains the same.