14418919fSjohnjiang /* SPDX-License-Identifier: BSD-3-Clause
24418919fSjohnjiang * Copyright(c) 2019 Intel Corporation
34418919fSjohnjiang */
44418919fSjohnjiang
54418919fSjohnjiang #ifndef _RTE_STACK_LF_C11_H_
64418919fSjohnjiang #define _RTE_STACK_LF_C11_H_
74418919fSjohnjiang
84418919fSjohnjiang #include <rte_branch_prediction.h>
94418919fSjohnjiang #include <rte_prefetch.h>
104418919fSjohnjiang
114418919fSjohnjiang static __rte_always_inline unsigned int
__rte_stack_lf_count(struct rte_stack * s)124418919fSjohnjiang __rte_stack_lf_count(struct rte_stack *s)
134418919fSjohnjiang {
144418919fSjohnjiang /* stack_lf_push() and stack_lf_pop() do not update the list's contents
154418919fSjohnjiang * and stack_lf->len atomically, which can cause the list to appear
164418919fSjohnjiang * shorter than it actually is if this function is called while other
174418919fSjohnjiang * threads are modifying the list.
184418919fSjohnjiang *
194418919fSjohnjiang * However, given the inherently approximate nature of the get_count
204418919fSjohnjiang * callback -- even if the list and its size were updated atomically,
214418919fSjohnjiang * the size could change between when get_count executes and when the
224418919fSjohnjiang * value is returned to the caller -- this is acceptable.
234418919fSjohnjiang *
244418919fSjohnjiang * The stack_lf->len updates are placed such that the list may appear to
254418919fSjohnjiang * have fewer elements than it does, but will never appear to have more
264418919fSjohnjiang * elements. If the mempool is near-empty to the point that this is a
274418919fSjohnjiang * concern, the user should consider increasing the mempool size.
284418919fSjohnjiang */
294418919fSjohnjiang return (unsigned int)__atomic_load_n(&s->stack_lf.used.len,
304418919fSjohnjiang __ATOMIC_RELAXED);
314418919fSjohnjiang }
324418919fSjohnjiang
334418919fSjohnjiang 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)344418919fSjohnjiang __rte_stack_lf_push_elems(struct rte_stack_lf_list *list,
354418919fSjohnjiang struct rte_stack_lf_elem *first,
364418919fSjohnjiang struct rte_stack_lf_elem *last,
374418919fSjohnjiang unsigned int num)
384418919fSjohnjiang {
394418919fSjohnjiang struct rte_stack_lf_head old_head;
404418919fSjohnjiang int success;
414418919fSjohnjiang
424418919fSjohnjiang old_head = list->head;
434418919fSjohnjiang
444418919fSjohnjiang do {
454418919fSjohnjiang struct rte_stack_lf_head new_head;
464418919fSjohnjiang
474418919fSjohnjiang /* Swing the top pointer to the first element in the list and
484418919fSjohnjiang * make the last element point to the old top.
494418919fSjohnjiang */
504418919fSjohnjiang new_head.top = first;
514418919fSjohnjiang new_head.cnt = old_head.cnt + 1;
524418919fSjohnjiang
534418919fSjohnjiang last->next = old_head.top;
544418919fSjohnjiang
554418919fSjohnjiang /* Use the release memmodel to ensure the writes to the LF LIFO
564418919fSjohnjiang * elements are visible before the head pointer write.
574418919fSjohnjiang */
584418919fSjohnjiang success = rte_atomic128_cmp_exchange(
594418919fSjohnjiang (rte_int128_t *)&list->head,
604418919fSjohnjiang (rte_int128_t *)&old_head,
614418919fSjohnjiang (rte_int128_t *)&new_head,
624418919fSjohnjiang 1, __ATOMIC_RELEASE,
634418919fSjohnjiang __ATOMIC_RELAXED);
644418919fSjohnjiang } while (success == 0);
654418919fSjohnjiang
664418919fSjohnjiang /* Ensure the stack modifications are not reordered with respect
674418919fSjohnjiang * to the LIFO len update.
684418919fSjohnjiang */
694418919fSjohnjiang __atomic_add_fetch(&list->len, num, __ATOMIC_RELEASE);
704418919fSjohnjiang }
714418919fSjohnjiang
724418919fSjohnjiang 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)734418919fSjohnjiang __rte_stack_lf_pop_elems(struct rte_stack_lf_list *list,
744418919fSjohnjiang unsigned int num,
754418919fSjohnjiang void **obj_table,
764418919fSjohnjiang struct rte_stack_lf_elem **last)
774418919fSjohnjiang {
784418919fSjohnjiang struct rte_stack_lf_head old_head;
794418919fSjohnjiang uint64_t len;
804418919fSjohnjiang int success;
814418919fSjohnjiang
824418919fSjohnjiang /* Reserve num elements, if available */
83*2d9fd380Sjfb8856606 len = __atomic_load_n(&list->len, __ATOMIC_RELAXED);
844418919fSjohnjiang
854418919fSjohnjiang while (1) {
864418919fSjohnjiang /* Does the list contain enough elements? */
874418919fSjohnjiang if (unlikely(len < num))
884418919fSjohnjiang return NULL;
894418919fSjohnjiang
904418919fSjohnjiang /* len is updated on failure */
914418919fSjohnjiang if (__atomic_compare_exchange_n(&list->len,
924418919fSjohnjiang &len, len - num,
93*2d9fd380Sjfb8856606 1, __ATOMIC_ACQUIRE,
94*2d9fd380Sjfb8856606 __ATOMIC_RELAXED))
954418919fSjohnjiang break;
964418919fSjohnjiang }
974418919fSjohnjiang
984418919fSjohnjiang /* If a torn read occurs, the CAS will fail and set old_head to the
994418919fSjohnjiang * correct/latest value.
1004418919fSjohnjiang */
1014418919fSjohnjiang old_head = list->head;
1024418919fSjohnjiang
1034418919fSjohnjiang /* Pop num elements */
1044418919fSjohnjiang do {
1054418919fSjohnjiang struct rte_stack_lf_head new_head;
1064418919fSjohnjiang struct rte_stack_lf_elem *tmp;
1074418919fSjohnjiang unsigned int i;
1084418919fSjohnjiang
1094418919fSjohnjiang /* Use the acquire memmodel to ensure the reads to the LF LIFO
1104418919fSjohnjiang * elements are properly ordered with respect to the head
1114418919fSjohnjiang * pointer read.
1124418919fSjohnjiang */
1134418919fSjohnjiang __atomic_thread_fence(__ATOMIC_ACQUIRE);
1144418919fSjohnjiang
1154418919fSjohnjiang rte_prefetch0(old_head.top);
1164418919fSjohnjiang
1174418919fSjohnjiang tmp = old_head.top;
1184418919fSjohnjiang
1194418919fSjohnjiang /* Traverse the list to find the new head. A next pointer will
1204418919fSjohnjiang * either point to another element or NULL; if a thread
1214418919fSjohnjiang * encounters a pointer that has already been popped, the CAS
1224418919fSjohnjiang * will fail.
1234418919fSjohnjiang */
1244418919fSjohnjiang for (i = 0; i < num && tmp != NULL; i++) {
1254418919fSjohnjiang rte_prefetch0(tmp->next);
1264418919fSjohnjiang if (obj_table)
1274418919fSjohnjiang obj_table[i] = tmp->data;
1284418919fSjohnjiang if (last)
1294418919fSjohnjiang *last = tmp;
1304418919fSjohnjiang tmp = tmp->next;
1314418919fSjohnjiang }
1324418919fSjohnjiang
1334418919fSjohnjiang /* If NULL was encountered, the list was modified while
1344418919fSjohnjiang * traversing it. Retry.
1354418919fSjohnjiang */
1360c6bd470Sfengbojiang if (i != num) {
1370c6bd470Sfengbojiang old_head = list->head;
1384418919fSjohnjiang continue;
1390c6bd470Sfengbojiang }
1404418919fSjohnjiang
1414418919fSjohnjiang new_head.top = tmp;
1424418919fSjohnjiang new_head.cnt = old_head.cnt + 1;
1434418919fSjohnjiang
144*2d9fd380Sjfb8856606 /*
145*2d9fd380Sjfb8856606 * The CAS should have release semantics to ensure that
146*2d9fd380Sjfb8856606 * items are read before the head is updated.
147*2d9fd380Sjfb8856606 * But this function is internal, and items are read
148*2d9fd380Sjfb8856606 * only when __rte_stack_lf_pop calls this function to
149*2d9fd380Sjfb8856606 * pop items from used list.
150*2d9fd380Sjfb8856606 * Then, those items are pushed to the free list.
151*2d9fd380Sjfb8856606 * Push uses a CAS store-release on head, which makes
152*2d9fd380Sjfb8856606 * sure that items are read before they are pushed to
153*2d9fd380Sjfb8856606 * the free list, without need for a CAS release here.
154*2d9fd380Sjfb8856606 * This CAS could also be used to ensure that the new
155*2d9fd380Sjfb8856606 * length is visible before the head update, but
156*2d9fd380Sjfb8856606 * acquire semantics on the length update is enough.
157*2d9fd380Sjfb8856606 */
1584418919fSjohnjiang success = rte_atomic128_cmp_exchange(
1594418919fSjohnjiang (rte_int128_t *)&list->head,
1604418919fSjohnjiang (rte_int128_t *)&old_head,
1614418919fSjohnjiang (rte_int128_t *)&new_head,
162*2d9fd380Sjfb8856606 0, __ATOMIC_RELAXED,
1634418919fSjohnjiang __ATOMIC_RELAXED);
1644418919fSjohnjiang } while (success == 0);
1654418919fSjohnjiang
1664418919fSjohnjiang return old_head.top;
1674418919fSjohnjiang }
1684418919fSjohnjiang
1694418919fSjohnjiang #endif /* _RTE_STACK_LF_C11_H_ */
170