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