Whamcloud - gitweb
Revert "LU-5570 lnet: check router aliveness timestamp"
[fs/lustre-release.git] / lnet / lnet / lib-eq.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lnet/lnet/lib-eq.c
37  *
38  * Library level Event queue management routines
39  */
40
41 #define DEBUG_SUBSYSTEM S_LNET
42 #include <lnet/lib-lnet.h>
43
44 /**
45  * Create an event queue that has room for \a count number of events.
46  *
47  * The event queue is circular and older events will be overwritten by new
48  * ones if they are not removed in time by the user using the functions
49  * LNetEQGet(), LNetEQWait(), or LNetEQPoll(). It is up to the user to
50  * determine the appropriate size of the event queue to prevent this loss
51  * of events. Note that when EQ handler is specified in \a callback, no
52  * event loss can happen, since the handler is run for each event deposited
53  * into the EQ.
54  *
55  * \param count The number of events to be stored in the event queue. It
56  * will be rounded up to the next power of two.
57  * \param callback A handler function that runs when an event is deposited
58  * into the EQ. The constant value LNET_EQ_HANDLER_NONE can be used to
59  * indicate that no event handler is desired.
60  * \param handle On successful return, this location will hold a handle for
61  * the newly created EQ.
62  *
63  * \retval 0       On success.
64  * \retval -EINVAL If an parameter is not valid.
65  * \retval -ENOMEM If memory for the EQ can't be allocated.
66  *
67  * \see lnet_eq_handler_t for the discussion on EQ handler semantics.
68  */
69 int
70 LNetEQAlloc(unsigned int count, lnet_eq_handler_t callback,
71             lnet_handle_eq_t *handle)
72 {
73         lnet_eq_t     *eq;
74
75         LASSERT (the_lnet.ln_init);
76         LASSERT (the_lnet.ln_refcount > 0);
77
78         /* We need count to be a power of 2 so that when eq_{enq,deq}_seq
79          * overflow, they don't skip entries, so the queue has the same
80          * apparent capacity at all times */
81
82         count = cfs_power2_roundup(count);
83
84         if (callback != LNET_EQ_HANDLER_NONE && count != 0) {
85                 CWARN("EQ callback is guaranteed to get every event, "
86                       "do you still want to set eqcount %d for polling "
87                       "event which will have locking overhead? "
88                       "Please contact with developer to confirm\n", count);
89         }
90
91         /* count can be 0 if only need callback, we can eliminate
92          * overhead of enqueue event */
93         if (count == 0 && callback == LNET_EQ_HANDLER_NONE)
94                 return -EINVAL;
95
96         eq = lnet_eq_alloc();
97         if (eq == NULL)
98                 return -ENOMEM;
99
100         if (count != 0) {
101                 LIBCFS_ALLOC(eq->eq_events, count * sizeof(lnet_event_t));
102                 if (eq->eq_events == NULL)
103                         goto failed;
104                 /* NB allocator has set all event sequence numbers to 0,
105                  * so all them should be earlier than eq_deq_seq */
106         }
107
108         eq->eq_deq_seq = 1;
109         eq->eq_enq_seq = 1;
110         eq->eq_size = count;
111         eq->eq_callback = callback;
112
113         eq->eq_refs = cfs_percpt_alloc(lnet_cpt_table(),
114                                        sizeof(*eq->eq_refs[0]));
115         if (eq->eq_refs == NULL)
116                 goto failed;
117
118         /* MUST hold both exclusive lnet_res_lock */
119         lnet_res_lock(LNET_LOCK_EX);
120         /* NB: hold lnet_eq_wait_lock for EQ link/unlink, so we can do
121          * both EQ lookup and poll event with only lnet_eq_wait_lock */
122         lnet_eq_wait_lock();
123
124         lnet_res_lh_initialize(&the_lnet.ln_eq_container, &eq->eq_lh);
125         list_add(&eq->eq_list, &the_lnet.ln_eq_container.rec_active);
126
127         lnet_eq_wait_unlock();
128         lnet_res_unlock(LNET_LOCK_EX);
129
130         lnet_eq2handle(handle, eq);
131         return 0;
132
133 failed:
134         if (eq->eq_events != NULL)
135                 LIBCFS_FREE(eq->eq_events, count * sizeof(lnet_event_t));
136
137         if (eq->eq_refs != NULL)
138                 cfs_percpt_free(eq->eq_refs);
139
140         lnet_eq_free(eq);
141         return -ENOMEM;
142 }
143 EXPORT_SYMBOL(LNetEQAlloc);
144
145 /**
146  * Release the resources associated with an event queue if it's idle;
147  * otherwise do nothing and it's up to the user to try again.
148  *
149  * \param eqh A handle for the event queue to be released.
150  *
151  * \retval 0 If the EQ is not in use and freed.
152  * \retval -ENOENT If \a eqh does not point to a valid EQ.
153  * \retval -EBUSY  If the EQ is still in use by some MDs.
154  */
155 int
156 LNetEQFree(lnet_handle_eq_t eqh)
157 {
158         struct lnet_eq  *eq;
159         lnet_event_t    *events = NULL;
160         int             **refs = NULL;
161         int             *ref;
162         int             rc = 0;
163         int             size = 0;
164         int             i;
165
166         LASSERT(the_lnet.ln_init);
167         LASSERT(the_lnet.ln_refcount > 0);
168
169         lnet_res_lock(LNET_LOCK_EX);
170         /* NB: hold lnet_eq_wait_lock for EQ link/unlink, so we can do
171          * both EQ lookup and poll event with only lnet_eq_wait_lock */
172         lnet_eq_wait_lock();
173
174         eq = lnet_handle2eq(&eqh);
175         if (eq == NULL) {
176                 rc = -ENOENT;
177                 goto out;
178         }
179
180         cfs_percpt_for_each(ref, i, eq->eq_refs) {
181                 LASSERT(*ref >= 0);
182                 if (*ref == 0)
183                         continue;
184
185                 CDEBUG(D_NET, "Event equeue (%d: %d) busy on destroy.\n",
186                        i, *ref);
187                 rc = -EBUSY;
188                 goto out;
189         }
190
191         /* stash for free after lock dropped */
192         events  = eq->eq_events;
193         size    = eq->eq_size;
194         refs    = eq->eq_refs;
195
196         lnet_res_lh_invalidate(&eq->eq_lh);
197         list_del(&eq->eq_list);
198         lnet_eq_free_locked(eq);
199  out:
200         lnet_eq_wait_unlock();
201         lnet_res_unlock(LNET_LOCK_EX);
202
203         if (events != NULL)
204                 LIBCFS_FREE(events, size * sizeof(lnet_event_t));
205         if (refs != NULL)
206                 cfs_percpt_free(refs);
207
208         return rc;
209 }
210 EXPORT_SYMBOL(LNetEQFree);
211
212 void
213 lnet_eq_enqueue_event(lnet_eq_t *eq, lnet_event_t *ev)
214 {
215         /* MUST called with resource lock hold but w/o lnet_eq_wait_lock */
216         int index;
217
218         if (eq->eq_size == 0) {
219                 LASSERT(eq->eq_callback != LNET_EQ_HANDLER_NONE);
220                 eq->eq_callback(ev);
221                 return;
222         }
223
224         lnet_eq_wait_lock();
225         ev->sequence = eq->eq_enq_seq++;
226
227         LASSERT(eq->eq_size == LOWEST_BIT_SET(eq->eq_size));
228         index = ev->sequence & (eq->eq_size - 1);
229
230         eq->eq_events[index] = *ev;
231
232         if (eq->eq_callback != LNET_EQ_HANDLER_NONE)
233                 eq->eq_callback(ev);
234
235 #ifdef __KERNEL__
236         /* Wake anyone waiting in LNetEQPoll() */
237         if (waitqueue_active(&the_lnet.ln_eq_waitq))
238                 wake_up_all(&the_lnet.ln_eq_waitq);
239 #else
240 # ifndef HAVE_LIBPTHREAD
241         /* LNetEQPoll() calls into _the_ LND to wait for action */
242 # else
243         /* Wake anyone waiting in LNetEQPoll() */
244         pthread_cond_broadcast(&the_lnet.ln_eq_cond);
245 # endif
246 #endif
247         lnet_eq_wait_unlock();
248 }
249
250 int
251 lnet_eq_dequeue_event(lnet_eq_t *eq, lnet_event_t *ev)
252 {
253         int             new_index = eq->eq_deq_seq & (eq->eq_size - 1);
254         lnet_event_t    *new_event = &eq->eq_events[new_index];
255         int             rc;
256         ENTRY;
257
258         /* must called with lnet_eq_wait_lock hold */
259         if (LNET_SEQ_GT(eq->eq_deq_seq, new_event->sequence))
260                 RETURN(0);
261
262         /* We've got a new event... */
263         *ev = *new_event;
264
265         CDEBUG(D_INFO, "event: %p, sequence: %lu, eq->size: %u\n",
266                new_event, eq->eq_deq_seq, eq->eq_size);
267
268         /* ...but did it overwrite an event we've not seen yet? */
269         if (eq->eq_deq_seq == new_event->sequence) {
270                 rc = 1;
271         } else {
272                 /* don't complain with CERROR: some EQs are sized small
273                  * anyway; if it's important, the caller should complain */
274                 CDEBUG(D_NET, "Event Queue Overflow: eq seq %lu ev seq %lu\n",
275                        eq->eq_deq_seq, new_event->sequence);
276                 rc = -EOVERFLOW;
277         }
278
279         eq->eq_deq_seq = new_event->sequence + 1;
280         RETURN(rc);
281 }
282
283 /**
284  * A nonblocking function that can be used to get the next event in an EQ.
285  * If an event handler is associated with the EQ, the handler will run before
286  * this function returns successfully. The event is removed from the queue.
287  *
288  * \param eventq A handle for the event queue.
289  * \param event On successful return (1 or -EOVERFLOW), this location will
290  * hold the next event in the EQ.
291  *
292  * \retval 0          No pending event in the EQ.
293  * \retval 1          Indicates success.
294  * \retval -ENOENT    If \a eventq does not point to a valid EQ.
295  * \retval -EOVERFLOW Indicates success (i.e., an event is returned) and that
296  * at least one event between this event and the last event obtained from the
297  * EQ has been dropped due to limited space in the EQ.
298  */
299 int
300 LNetEQGet (lnet_handle_eq_t eventq, lnet_event_t *event)
301 {
302         int which;
303
304         return LNetEQPoll(&eventq, 1, 0,
305                          event, &which);
306 }
307 EXPORT_SYMBOL(LNetEQGet);
308
309 /**
310  * Block the calling process until there is an event in the EQ.
311  * If an event handler is associated with the EQ, the handler will run before
312  * this function returns successfully. This function returns the next event
313  * in the EQ and removes it from the EQ.
314  *
315  * \param eventq A handle for the event queue.
316  * \param event On successful return (1 or -EOVERFLOW), this location will
317  * hold the next event in the EQ.
318  *
319  * \retval 1          Indicates success.
320  * \retval -ENOENT    If \a eventq does not point to a valid EQ.
321  * \retval -EOVERFLOW Indicates success (i.e., an event is returned) and that
322  * at least one event between this event and the last event obtained from the
323  * EQ has been dropped due to limited space in the EQ.
324  */
325 int
326 LNetEQWait (lnet_handle_eq_t eventq, lnet_event_t *event)
327 {
328         int which;
329
330         return LNetEQPoll(&eventq, 1, LNET_TIME_FOREVER,
331                          event, &which);
332 }
333 EXPORT_SYMBOL(LNetEQWait);
334
335 #ifdef __KERNEL__
336
337 static int
338 lnet_eq_wait_locked(int *timeout_ms)
339 __must_hold(&the_lnet.ln_eq_wait_lock)
340 {
341         int             tms = *timeout_ms;
342         int             wait;
343         wait_queue_t  wl;
344         cfs_time_t      now;
345
346         if (tms == 0)
347                 return -1; /* don't want to wait and no new event */
348
349         init_waitqueue_entry_current(&wl);
350         set_current_state(TASK_INTERRUPTIBLE);
351         add_wait_queue(&the_lnet.ln_eq_waitq, &wl);
352
353         lnet_eq_wait_unlock();
354
355         if (tms < 0) {
356                 waitq_wait(&wl, TASK_INTERRUPTIBLE);
357
358         } else {
359                 struct timeval tv;
360
361                 now = cfs_time_current();
362                 waitq_timedwait(&wl, TASK_INTERRUPTIBLE,
363                                     cfs_time_seconds(tms) / 1000);
364                 cfs_duration_usec(cfs_time_sub(cfs_time_current(), now), &tv);
365                 tms -= (int)(tv.tv_sec * 1000 + tv.tv_usec / 1000);
366                 if (tms < 0) /* no more wait but may have new event */
367                         tms = 0;
368         }
369
370         wait = tms != 0; /* might need to call here again */
371         *timeout_ms = tms;
372
373         lnet_eq_wait_lock();
374         remove_wait_queue(&the_lnet.ln_eq_waitq, &wl);
375
376         return wait;
377 }
378
379 #else /* !__KERNEL__ */
380
381 # ifdef HAVE_LIBPTHREAD
382 static void
383 lnet_eq_cond_wait(struct timespec *ts)
384 {
385         if (ts == NULL) {
386                 pthread_cond_wait(&the_lnet.ln_eq_cond,
387                                   &the_lnet.ln_eq_wait_lock);
388         } else {
389                 pthread_cond_timedwait(&the_lnet.ln_eq_cond,
390                                        &the_lnet.ln_eq_wait_lock, ts);
391         }
392 }
393 # endif
394
395 static int
396 lnet_eq_wait_locked(int *timeout_ms)
397 {
398         lnet_ni_t       *eq_waitni = NULL;
399         int             tms = *timeout_ms;
400         int             wait;
401         struct timeval  then;
402         struct timeval  now;
403
404         if (the_lnet.ln_eq_waitni != NULL) {
405                 /* I have a single NI that I have to call into, to get
406                  * events queued, or to block. */
407                 lnet_eq_wait_unlock();
408
409                 lnet_net_lock(0);
410                 eq_waitni = the_lnet.ln_eq_waitni;
411                 if (unlikely(eq_waitni == NULL)) {
412                         lnet_net_unlock(0);
413
414                         lnet_eq_wait_lock();
415                         return -1;
416                 }
417
418                 lnet_ni_addref_locked(eq_waitni, 0);
419                 lnet_net_unlock(0);
420
421                 if (tms <= 0) { /* even for tms == 0 */
422                         (eq_waitni->ni_lnd->lnd_wait)(eq_waitni, tms);
423
424                 } else {
425                         gettimeofday(&then, NULL);
426
427                         (eq_waitni->ni_lnd->lnd_wait)(eq_waitni, tms);
428
429                         gettimeofday(&now, NULL);
430                         tms -= (now.tv_sec - then.tv_sec) * 1000 +
431                                (now.tv_usec - then.tv_usec) / 1000;
432                         if (tms < 0)
433                                 tms = 0;
434                 }
435
436                 lnet_ni_decref(eq_waitni);
437                 lnet_eq_wait_lock();
438         } else { /* w/o eq_waitni */
439 # ifndef HAVE_LIBPTHREAD
440                 /* If I'm single-threaded, LNET fails at startup if it can't
441                  * set the_lnet.ln_eqwaitni correctly.  */
442                 LBUG();
443 # else /* HAVE_LIBPTHREAD */
444                 struct timespec  ts;
445
446                 if (tms == 0) /* don't want to wait and new event */
447                         return -1;
448
449                 if (tms < 0) {
450                         lnet_eq_cond_wait(NULL);
451
452                 } else {
453
454                         gettimeofday(&then, NULL);
455
456                         ts.tv_sec = then.tv_sec + tms / 1000;
457                         ts.tv_nsec = then.tv_usec * 1000 +
458                                      (tms % 1000) * 1000000;
459                         if (ts.tv_nsec >= 1000000000) {
460                                 ts.tv_sec++;
461                                 ts.tv_nsec -= 1000000000;
462                         }
463
464                         lnet_eq_cond_wait(&ts);
465
466                         gettimeofday(&now, NULL);
467                         tms -= (now.tv_sec - then.tv_sec) * 1000 +
468                                (now.tv_usec - then.tv_usec) / 1000;
469                         if (tms < 0)
470                                 tms = 0;
471                 }
472 # endif /* HAVE_LIBPTHREAD */
473         }
474
475         wait = tms != 0;
476         *timeout_ms = tms;
477
478         return wait;
479 }
480
481 #endif /* __KERNEL__ */
482
483
484 /**
485  * Block the calling process until there's an event from a set of EQs or
486  * timeout happens.
487  *
488  * If an event handler is associated with the EQ, the handler will run before
489  * this function returns successfully, in which case the corresponding event
490  * is consumed.
491  *
492  * LNetEQPoll() provides a timeout to allow applications to poll, block for a
493  * fixed period, or block indefinitely.
494  *
495  * \param eventqs,neq An array of EQ handles, and size of the array.
496  * \param timeout_ms Time in milliseconds to wait for an event to occur on
497  * one of the EQs. The constant LNET_TIME_FOREVER can be used to indicate an
498  * infinite timeout.
499  * \param event,which On successful return (1 or -EOVERFLOW), \a event will
500  * hold the next event in the EQs, and \a which will contain the index of the
501  * EQ from which the event was taken.
502  *
503  * \retval 0          No pending event in the EQs after timeout.
504  * \retval 1          Indicates success.
505  * \retval -EOVERFLOW Indicates success (i.e., an event is returned) and that
506  * at least one event between this event and the last event obtained from the
507  * EQ indicated by \a which has been dropped due to limited space in the EQ.
508  * \retval -ENOENT    If there's an invalid handle in \a eventqs.
509  */
510 int
511 LNetEQPoll(lnet_handle_eq_t *eventqs, int neq, int timeout_ms,
512            lnet_event_t *event, int *which)
513 {
514         int     wait = 1;
515         int     rc;
516         int     i;
517         ENTRY;
518
519         LASSERT (the_lnet.ln_init);
520         LASSERT (the_lnet.ln_refcount > 0);
521
522         if (neq < 1)
523                 RETURN(-ENOENT);
524
525         lnet_eq_wait_lock();
526
527         for (;;) {
528 #ifndef __KERNEL__
529                 lnet_eq_wait_unlock();
530
531                 /* Recursion breaker */
532                 if (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING &&
533                     !LNetHandleIsEqual(eventqs[0], the_lnet.ln_rc_eqh))
534                         lnet_router_checker();
535
536                 lnet_eq_wait_lock();
537 #endif
538                 for (i = 0; i < neq; i++) {
539                         lnet_eq_t *eq = lnet_handle2eq(&eventqs[i]);
540
541                         if (eq == NULL) {
542                                 lnet_eq_wait_unlock();
543                                 RETURN(-ENOENT);
544                         }
545
546                         rc = lnet_eq_dequeue_event(eq, event);
547                         if (rc != 0) {
548                                 lnet_eq_wait_unlock();
549                                 *which = i;
550                                 RETURN(rc);
551                         }
552                 }
553
554                 if (wait == 0)
555                         break;
556
557                 /*
558                  * return value of lnet_eq_wait_locked:
559                  * -1 : did nothing and it's sure no new event
560                  *  1 : sleep inside and wait until new event
561                  *  0 : don't want to wait anymore, but might have new event
562                  *      so need to call dequeue again
563                  */
564                 wait = lnet_eq_wait_locked(&timeout_ms);
565                 if (wait < 0) /* no new event */
566                         break;
567         }
568
569         lnet_eq_wait_unlock();
570         RETURN(0);
571 }