mem: fix undefined behavior in NUMA-aware mapping
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_alarm.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 #include <stdio.h>
5 #include <stdint.h>
6 #include <signal.h>
7 #include <errno.h>
8 #include <string.h>
9 #include <sys/queue.h>
10 #include <sys/time.h>
11 #include <sys/timerfd.h>
12
13 #include <rte_memory.h>
14 #include <rte_interrupts.h>
15 #include <rte_alarm.h>
16 #include <rte_common.h>
17 #include <rte_per_lcore.h>
18 #include <rte_eal.h>
19 #include <rte_launch.h>
20 #include <rte_lcore.h>
21 #include <rte_errno.h>
22 #include <rte_spinlock.h>
23 #include <eal_private.h>
24
25 #ifndef TFD_NONBLOCK
26 #include <fcntl.h>
27 #define TFD_NONBLOCK    O_NONBLOCK
28 #endif
29
30 #define NS_PER_US 1000
31 #define US_PER_MS 1000
32 #define MS_PER_S 1000
33 #define US_PER_S (US_PER_MS * MS_PER_S)
34
35 #ifdef CLOCK_MONOTONIC_RAW /* Defined in glibc bits/time.h */
36 #define CLOCK_TYPE_ID CLOCK_MONOTONIC_RAW
37 #else
38 #define CLOCK_TYPE_ID CLOCK_MONOTONIC
39 #endif
40
41 struct alarm_entry {
42         LIST_ENTRY(alarm_entry) next;
43         struct timeval time;
44         rte_eal_alarm_callback cb_fn;
45         void *cb_arg;
46         volatile uint8_t executing;
47         volatile pthread_t executing_id;
48 };
49
50 static LIST_HEAD(alarm_list, alarm_entry) alarm_list = LIST_HEAD_INITIALIZER();
51 static rte_spinlock_t alarm_list_lk = RTE_SPINLOCK_INITIALIZER;
52
53 static struct rte_intr_handle intr_handle = {.fd = -1 };
54 static int handler_registered = 0;
55 static void eal_alarm_callback(void *arg);
56
57 int
58 rte_eal_alarm_init(void)
59 {
60         intr_handle.type = RTE_INTR_HANDLE_ALARM;
61         /* create a timerfd file descriptor */
62         intr_handle.fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
63         if (intr_handle.fd == -1)
64                 goto error;
65
66         return 0;
67
68 error:
69         rte_errno = errno;
70         return -1;
71 }
72
73 static void
74 eal_alarm_callback(void *arg __rte_unused)
75 {
76         struct timespec now;
77         struct alarm_entry *ap;
78
79         rte_spinlock_lock(&alarm_list_lk);
80         while ((ap = LIST_FIRST(&alarm_list)) !=NULL &&
81                         clock_gettime(CLOCK_TYPE_ID, &now) == 0 &&
82                         (ap->time.tv_sec < now.tv_sec || (ap->time.tv_sec == now.tv_sec &&
83                                                 (ap->time.tv_usec * NS_PER_US) <= now.tv_nsec))) {
84                 ap->executing = 1;
85                 ap->executing_id = pthread_self();
86                 rte_spinlock_unlock(&alarm_list_lk);
87
88                 ap->cb_fn(ap->cb_arg);
89
90                 rte_spinlock_lock(&alarm_list_lk);
91
92                 LIST_REMOVE(ap, next);
93                 free(ap);
94         }
95
96         if (!LIST_EMPTY(&alarm_list)) {
97                 struct itimerspec atime = { .it_interval = { 0, 0 } };
98
99                 ap = LIST_FIRST(&alarm_list);
100                 atime.it_value.tv_sec = ap->time.tv_sec;
101                 atime.it_value.tv_nsec = ap->time.tv_usec * NS_PER_US;
102                 /* perform borrow for subtraction if necessary */
103                 if (now.tv_nsec > (ap->time.tv_usec * NS_PER_US))
104                         atime.it_value.tv_sec--, atime.it_value.tv_nsec += US_PER_S * NS_PER_US;
105
106                 atime.it_value.tv_sec -= now.tv_sec;
107                 atime.it_value.tv_nsec -= now.tv_nsec;
108                 timerfd_settime(intr_handle.fd, 0, &atime, NULL);
109         }
110         rte_spinlock_unlock(&alarm_list_lk);
111 }
112
113 int
114 rte_eal_alarm_set(uint64_t us, rte_eal_alarm_callback cb_fn, void *cb_arg)
115 {
116         struct timespec now;
117         int ret = 0;
118         struct alarm_entry *ap, *new_alarm;
119
120         /* Check parameters, including that us won't cause a uint64_t overflow */
121         if (us < 1 || us > (UINT64_MAX - US_PER_S) || cb_fn == NULL)
122                 return -EINVAL;
123
124         new_alarm = calloc(1, sizeof(*new_alarm));
125         if (new_alarm == NULL)
126                 return -ENOMEM;
127
128         /* use current time to calculate absolute time of alarm */
129         clock_gettime(CLOCK_TYPE_ID, &now);
130
131         new_alarm->cb_fn = cb_fn;
132         new_alarm->cb_arg = cb_arg;
133         new_alarm->time.tv_usec = ((now.tv_nsec / NS_PER_US) + us) % US_PER_S;
134         new_alarm->time.tv_sec = now.tv_sec + (((now.tv_nsec / NS_PER_US) + us) / US_PER_S);
135
136         rte_spinlock_lock(&alarm_list_lk);
137         if (!handler_registered) {
138                 ret |= rte_intr_callback_register(&intr_handle,
139                                 eal_alarm_callback, NULL);
140                 handler_registered = (ret == 0) ? 1 : 0;
141         }
142
143         if (LIST_EMPTY(&alarm_list))
144                 LIST_INSERT_HEAD(&alarm_list, new_alarm, next);
145         else {
146                 LIST_FOREACH(ap, &alarm_list, next) {
147                         if (ap->time.tv_sec > new_alarm->time.tv_sec ||
148                                         (ap->time.tv_sec == new_alarm->time.tv_sec &&
149                                                         ap->time.tv_usec > new_alarm->time.tv_usec)){
150                                 LIST_INSERT_BEFORE(ap, new_alarm, next);
151                                 break;
152                         }
153                         if (LIST_NEXT(ap, next) == NULL) {
154                                 LIST_INSERT_AFTER(ap, new_alarm, next);
155                                 break;
156                         }
157                 }
158         }
159
160         if (LIST_FIRST(&alarm_list) == new_alarm) {
161                 struct itimerspec alarm_time = {
162                         .it_interval = {0, 0},
163                         .it_value = {
164                                 .tv_sec = us / US_PER_S,
165                                 .tv_nsec = (us % US_PER_S) * NS_PER_US,
166                         },
167                 };
168                 ret |= timerfd_settime(intr_handle.fd, 0, &alarm_time, NULL);
169         }
170         rte_spinlock_unlock(&alarm_list_lk);
171
172         return ret;
173 }
174
175 int
176 rte_eal_alarm_cancel(rte_eal_alarm_callback cb_fn, void *cb_arg)
177 {
178         struct alarm_entry *ap, *ap_prev;
179         int count = 0;
180         int err = 0;
181         int executing;
182
183         if (!cb_fn) {
184                 rte_errno = EINVAL;
185                 return -1;
186         }
187
188         do {
189                 executing = 0;
190                 rte_spinlock_lock(&alarm_list_lk);
191                 /* remove any matches at the start of the list */
192                 while ((ap = LIST_FIRST(&alarm_list)) != NULL &&
193                                 cb_fn == ap->cb_fn &&
194                                 (cb_arg == (void *)-1 || cb_arg == ap->cb_arg)) {
195
196                         if (ap->executing == 0) {
197                                 LIST_REMOVE(ap, next);
198                                 free(ap);
199                                 count++;
200                         } else {
201                                 /* If calling from other context, mark that alarm is executing
202                                  * so loop can spin till it finish. Otherwise we are trying to
203                                  * cancel our self - mark it by EINPROGRESS */
204                                 if (pthread_equal(ap->executing_id, pthread_self()) == 0)
205                                         executing++;
206                                 else
207                                         err = EINPROGRESS;
208
209                                 break;
210                         }
211                 }
212                 ap_prev = ap;
213
214                 /* now go through list, removing entries not at start */
215                 LIST_FOREACH(ap, &alarm_list, next) {
216                         /* this won't be true first time through */
217                         if (cb_fn == ap->cb_fn &&
218                                         (cb_arg == (void *)-1 || cb_arg == ap->cb_arg)) {
219
220                                 if (ap->executing == 0) {
221                                         LIST_REMOVE(ap, next);
222                                         free(ap);
223                                         count++;
224                                         ap = ap_prev;
225                                 } else if (pthread_equal(ap->executing_id, pthread_self()) == 0)
226                                         executing++;
227                                 else
228                                         err = EINPROGRESS;
229                         }
230                         ap_prev = ap;
231                 }
232                 rte_spinlock_unlock(&alarm_list_lk);
233         } while (executing != 0);
234
235         if (count == 0 && err == 0)
236                 rte_errno = ENOENT;
237         else if (err)
238                 rte_errno = err;
239
240         return count;
241 }