199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson * Copyright(c) 2019 Intel Corporation
399a2dd95SBruce Richardson */
499a2dd95SBruce Richardson
599a2dd95SBruce Richardson #ifndef _RTE_STACK_LF_GENERIC_H_
699a2dd95SBruce Richardson #define _RTE_STACK_LF_GENERIC_H_
799a2dd95SBruce Richardson
899a2dd95SBruce Richardson #include <rte_branch_prediction.h>
999a2dd95SBruce Richardson #include <rte_prefetch.h>
1099a2dd95SBruce Richardson
1199a2dd95SBruce Richardson static __rte_always_inline unsigned int
__rte_stack_lf_count(struct rte_stack * s)1299a2dd95SBruce Richardson __rte_stack_lf_count(struct rte_stack *s)
1399a2dd95SBruce Richardson {
1499a2dd95SBruce Richardson /* stack_lf_push() and stack_lf_pop() do not update the list's contents
1599a2dd95SBruce Richardson * and stack_lf->len atomically, which can cause the list to appear
1699a2dd95SBruce Richardson * shorter than it actually is if this function is called while other
1799a2dd95SBruce Richardson * threads are modifying the list.
1899a2dd95SBruce Richardson *
1999a2dd95SBruce Richardson * However, given the inherently approximate nature of the get_count
2099a2dd95SBruce Richardson * callback -- even if the list and its size were updated atomically,
2199a2dd95SBruce Richardson * the size could change between when get_count executes and when the
2299a2dd95SBruce Richardson * value is returned to the caller -- this is acceptable.
2399a2dd95SBruce Richardson *
2499a2dd95SBruce Richardson * The stack_lf->len updates are placed such that the list may appear to
2599a2dd95SBruce Richardson * have fewer elements than it does, but will never appear to have more
2699a2dd95SBruce Richardson * elements. If the mempool is near-empty to the point that this is a
2799a2dd95SBruce Richardson * concern, the user should consider increasing the mempool size.
2899a2dd95SBruce Richardson */
2999a2dd95SBruce Richardson return (unsigned int)rte_atomic64_read((rte_atomic64_t *)
3099a2dd95SBruce Richardson &s->stack_lf.used.len);
3199a2dd95SBruce Richardson }
3299a2dd95SBruce Richardson
3399a2dd95SBruce Richardson static __rte_always_inline void
__rte_stack_lf_push_elems(struct rte_stack_lf_list * list,struct rte_stack_lf_elem * first,struct rte_stack_lf_elem * last,unsigned int num)3499a2dd95SBruce Richardson __rte_stack_lf_push_elems(struct rte_stack_lf_list *list,
3599a2dd95SBruce Richardson struct rte_stack_lf_elem *first,
3699a2dd95SBruce Richardson struct rte_stack_lf_elem *last,
3799a2dd95SBruce Richardson unsigned int num)
3899a2dd95SBruce Richardson {
3999a2dd95SBruce Richardson struct rte_stack_lf_head old_head;
4099a2dd95SBruce Richardson int success;
4199a2dd95SBruce Richardson
4299a2dd95SBruce Richardson old_head = list->head;
4399a2dd95SBruce Richardson
4499a2dd95SBruce Richardson do {
4599a2dd95SBruce Richardson struct rte_stack_lf_head new_head;
4699a2dd95SBruce Richardson
4799a2dd95SBruce Richardson /* An acquire fence (or stronger) is needed for weak memory
4899a2dd95SBruce Richardson * models to establish a synchronized-with relationship between
4999a2dd95SBruce Richardson * the list->head load and store-release operations (as part of
5099a2dd95SBruce Richardson * the rte_atomic128_cmp_exchange()).
5199a2dd95SBruce Richardson */
5299a2dd95SBruce Richardson rte_smp_mb();
5399a2dd95SBruce Richardson
5499a2dd95SBruce Richardson /* Swing the top pointer to the first element in the list and
5599a2dd95SBruce Richardson * make the last element point to the old top.
5699a2dd95SBruce Richardson */
5799a2dd95SBruce Richardson new_head.top = first;
5899a2dd95SBruce Richardson new_head.cnt = old_head.cnt + 1;
5999a2dd95SBruce Richardson
6099a2dd95SBruce Richardson last->next = old_head.top;
6199a2dd95SBruce Richardson
6299a2dd95SBruce Richardson /* old_head is updated on failure */
6399a2dd95SBruce Richardson success = rte_atomic128_cmp_exchange(
6499a2dd95SBruce Richardson (rte_int128_t *)&list->head,
6599a2dd95SBruce Richardson (rte_int128_t *)&old_head,
6699a2dd95SBruce Richardson (rte_int128_t *)&new_head,
6799a2dd95SBruce Richardson 1, __ATOMIC_RELEASE,
6899a2dd95SBruce Richardson __ATOMIC_RELAXED);
6999a2dd95SBruce Richardson } while (success == 0);
7099a2dd95SBruce Richardson
7199a2dd95SBruce Richardson rte_atomic64_add((rte_atomic64_t *)&list->len, num);
7299a2dd95SBruce Richardson }
7399a2dd95SBruce Richardson
7499a2dd95SBruce Richardson static __rte_always_inline struct rte_stack_lf_elem *
__rte_stack_lf_pop_elems(struct rte_stack_lf_list * list,unsigned int num,void ** obj_table,struct rte_stack_lf_elem ** last)7599a2dd95SBruce Richardson __rte_stack_lf_pop_elems(struct rte_stack_lf_list *list,
7699a2dd95SBruce Richardson unsigned int num,
7799a2dd95SBruce Richardson void **obj_table,
7899a2dd95SBruce Richardson struct rte_stack_lf_elem **last)
7999a2dd95SBruce Richardson {
8099a2dd95SBruce Richardson struct rte_stack_lf_head old_head;
8199a2dd95SBruce Richardson int success = 0;
8299a2dd95SBruce Richardson
8399a2dd95SBruce Richardson /* Reserve num elements, if available */
8499a2dd95SBruce Richardson while (1) {
8599a2dd95SBruce Richardson uint64_t len = rte_atomic64_read((rte_atomic64_t *)&list->len);
8699a2dd95SBruce Richardson
8799a2dd95SBruce Richardson /* Does the list contain enough elements? */
8899a2dd95SBruce Richardson if (unlikely(len < num))
8999a2dd95SBruce Richardson return NULL;
9099a2dd95SBruce Richardson
9199a2dd95SBruce Richardson if (rte_atomic64_cmpset((volatile uint64_t *)&list->len,
9299a2dd95SBruce Richardson len, len - num))
9399a2dd95SBruce Richardson break;
9499a2dd95SBruce Richardson }
9599a2dd95SBruce Richardson
9699a2dd95SBruce Richardson old_head = list->head;
9799a2dd95SBruce Richardson
9899a2dd95SBruce Richardson /* Pop num elements */
9999a2dd95SBruce Richardson do {
10099a2dd95SBruce Richardson struct rte_stack_lf_head new_head;
10199a2dd95SBruce Richardson struct rte_stack_lf_elem *tmp;
10299a2dd95SBruce Richardson unsigned int i;
10399a2dd95SBruce Richardson
10499a2dd95SBruce Richardson /* An acquire fence (or stronger) is needed for weak memory
10599a2dd95SBruce Richardson * models to ensure the LF LIFO element reads are properly
10699a2dd95SBruce Richardson * ordered with respect to the head pointer read.
10799a2dd95SBruce Richardson */
10899a2dd95SBruce Richardson rte_smp_mb();
10999a2dd95SBruce Richardson
11099a2dd95SBruce Richardson rte_prefetch0(old_head.top);
11199a2dd95SBruce Richardson
11299a2dd95SBruce Richardson tmp = old_head.top;
11399a2dd95SBruce Richardson
11499a2dd95SBruce Richardson /* Traverse the list to find the new head. A next pointer will
11599a2dd95SBruce Richardson * either point to another element or NULL; if a thread
11699a2dd95SBruce Richardson * encounters a pointer that has already been popped, the CAS
11799a2dd95SBruce Richardson * will fail.
11899a2dd95SBruce Richardson */
11999a2dd95SBruce Richardson for (i = 0; i < num && tmp != NULL; i++) {
12099a2dd95SBruce Richardson rte_prefetch0(tmp->next);
12199a2dd95SBruce Richardson if (obj_table)
12299a2dd95SBruce Richardson obj_table[i] = tmp->data;
12399a2dd95SBruce Richardson if (last)
12499a2dd95SBruce Richardson *last = tmp;
12599a2dd95SBruce Richardson tmp = tmp->next;
12699a2dd95SBruce Richardson }
12799a2dd95SBruce Richardson
12899a2dd95SBruce Richardson /* If NULL was encountered, the list was modified while
12999a2dd95SBruce Richardson * traversing it. Retry.
13099a2dd95SBruce Richardson */
131*6ded44bcSJulien Meunier if (i != num) {
132*6ded44bcSJulien Meunier old_head = list->head;
13399a2dd95SBruce Richardson continue;
134*6ded44bcSJulien Meunier }
13599a2dd95SBruce Richardson
13699a2dd95SBruce Richardson new_head.top = tmp;
13799a2dd95SBruce Richardson new_head.cnt = old_head.cnt + 1;
13899a2dd95SBruce Richardson
13999a2dd95SBruce Richardson /* old_head is updated on failure */
14099a2dd95SBruce Richardson success = rte_atomic128_cmp_exchange(
14199a2dd95SBruce Richardson (rte_int128_t *)&list->head,
14299a2dd95SBruce Richardson (rte_int128_t *)&old_head,
14399a2dd95SBruce Richardson (rte_int128_t *)&new_head,
14499a2dd95SBruce Richardson 1, __ATOMIC_RELEASE,
14599a2dd95SBruce Richardson __ATOMIC_RELAXED);
14699a2dd95SBruce Richardson } while (success == 0);
14799a2dd95SBruce Richardson
14899a2dd95SBruce Richardson return old_head.top;
14999a2dd95SBruce Richardson }
15099a2dd95SBruce Richardson
15199a2dd95SBruce Richardson #endif /* _RTE_STACK_LF_GENERIC_H_ */
152