Whamcloud - gitweb
LU-12080 lnet: clean mt_eqh properly
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lnet/lnet/lib-eq.c
33  *
34  * Library level Event queue management routines
35  */
36
37 #define DEBUG_SUBSYSTEM S_LNET
38 #include <lnet/lib-lnet.h>
39
40 /**
41  * Create an event queue that has room for \a count number of events.
42  *
43  * The event queue is circular and older events will be overwritten by new
44  * ones if they are not removed in time by the user using the functions
45  * LNetEQGet(), LNetEQWait(), or LNetEQPoll(). It is up to the user to
46  * determine the appropriate size of the event queue to prevent this loss
47  * of events. Note that when EQ handler is specified in \a callback, no
48  * event loss can happen, since the handler is run for each event deposited
49  * into the EQ.
50  *
51  * \param count The number of events to be stored in the event queue. It
52  * will be rounded up to the next power of two.
53  * \param callback A handler function that runs when an event is deposited
54  * into the EQ. The constant value LNET_EQ_HANDLER_NONE can be used to
55  * indicate that no event handler is desired.
56  * \param handle On successful return, this location will hold a handle for
57  * the newly created EQ.
58  *
59  * \retval 0       On success.
60  * \retval -EINVAL If an parameter is not valid.
61  * \retval -ENOMEM If memory for the EQ can't be allocated.
62  *
63  * \see lnet_eq_handler_t for the discussion on EQ handler semantics.
64  */
65 int
66 LNetEQAlloc(unsigned int count, lnet_eq_handler_t callback,
67             struct lnet_handle_eq *handle)
68 {
69         struct lnet_eq *eq;
70
71         LASSERT(the_lnet.ln_refcount > 0);
72
73         /* We need count to be a power of 2 so that when eq_{enq,deq}_seq
74          * overflow, they don't skip entries, so the queue has the same
75          * apparent capacity at all times */
76
77         if (count)
78                 count = roundup_pow_of_two(count);
79
80         if (callback != LNET_EQ_HANDLER_NONE && count != 0) {
81                 CWARN("EQ callback is guaranteed to get every event, "
82                       "do you still want to set eqcount %d for polling "
83                       "event which will have locking overhead? "
84                       "Please contact with developer to confirm\n", count);
85         }
86
87         /* count can be 0 if only need callback, we can eliminate
88          * overhead of enqueue event */
89         if (count == 0 && callback == LNET_EQ_HANDLER_NONE)
90                 return -EINVAL;
91
92         eq = lnet_eq_alloc();
93         if (eq == NULL)
94                 return -ENOMEM;
95
96         if (count != 0) {
97                 LIBCFS_ALLOC(eq->eq_events, count * sizeof(struct lnet_event));
98                 if (eq->eq_events == NULL)
99                         goto failed;
100                 /* NB allocator has set all event sequence numbers to 0,
101                  * so all them should be earlier than eq_deq_seq */
102         }
103
104         eq->eq_deq_seq = 1;
105         eq->eq_enq_seq = 1;
106         eq->eq_size = count;
107         eq->eq_callback = callback;
108
109         eq->eq_refs = cfs_percpt_alloc(lnet_cpt_table(),
110                                        sizeof(*eq->eq_refs[0]));
111         if (eq->eq_refs == NULL)
112                 goto failed;
113
114         /* MUST hold both exclusive lnet_res_lock */
115         lnet_res_lock(LNET_LOCK_EX);
116         /* NB: hold lnet_eq_wait_lock for EQ link/unlink, so we can do
117          * both EQ lookup and poll event with only lnet_eq_wait_lock */
118         lnet_eq_wait_lock();
119
120         lnet_res_lh_initialize(&the_lnet.ln_eq_container, &eq->eq_lh);
121         list_add(&eq->eq_list, &the_lnet.ln_eq_container.rec_active);
122
123         lnet_eq_wait_unlock();
124         lnet_res_unlock(LNET_LOCK_EX);
125
126         lnet_eq2handle(handle, eq);
127         return 0;
128
129 failed:
130         if (eq->eq_events != NULL)
131                 LIBCFS_FREE(eq->eq_events, count * sizeof(struct lnet_event));
132
133         if (eq->eq_refs != NULL)
134                 cfs_percpt_free(eq->eq_refs);
135
136         lnet_eq_free(eq);
137         return -ENOMEM;
138 }
139 EXPORT_SYMBOL(LNetEQAlloc);
140
141 /**
142  * Release the resources associated with an event queue if it's idle;
143  * otherwise do nothing and it's up to the user to try again.
144  *
145  * \param eqh A handle for the event queue to be released.
146  *
147  * \retval 0 If the EQ is not in use and freed.
148  * \retval -ENOENT If \a eqh does not point to a valid EQ.
149  * \retval -EBUSY  If the EQ is still in use by some MDs.
150  */
151 int
152 LNetEQFree(struct lnet_handle_eq eqh)
153 {
154         struct lnet_eq  *eq;
155         struct lnet_event       *events = NULL;
156         int             **refs = NULL;
157         int             *ref;
158         int             rc = 0;
159         int             size = 0;
160         int             i;
161
162         lnet_res_lock(LNET_LOCK_EX);
163         /* NB: hold lnet_eq_wait_lock for EQ link/unlink, so we can do
164          * both EQ lookup and poll event with only lnet_eq_wait_lock */
165         lnet_eq_wait_lock();
166
167         eq = lnet_handle2eq(&eqh);
168         if (eq == NULL) {
169                 rc = -ENOENT;
170                 goto out;
171         }
172
173         cfs_percpt_for_each(ref, i, eq->eq_refs) {
174                 LASSERT(*ref >= 0);
175                 if (*ref == 0)
176                         continue;
177
178                 CDEBUG(D_NET, "Event equeue (%d: %d) busy on destroy.\n",
179                        i, *ref);
180                 rc = -EBUSY;
181                 goto out;
182         }
183
184         /* stash for free after lock dropped */
185         events  = eq->eq_events;
186         size    = eq->eq_size;
187         refs    = eq->eq_refs;
188
189         lnet_res_lh_invalidate(&eq->eq_lh);
190         list_del(&eq->eq_list);
191         lnet_eq_free(eq);
192  out:
193         lnet_eq_wait_unlock();
194         lnet_res_unlock(LNET_LOCK_EX);
195
196         if (events != NULL)
197                 LIBCFS_FREE(events, size * sizeof(struct lnet_event));
198         if (refs != NULL)
199                 cfs_percpt_free(refs);
200
201         return rc;
202 }
203 EXPORT_SYMBOL(LNetEQFree);
204
205 void
206 lnet_eq_enqueue_event(struct lnet_eq *eq, struct lnet_event *ev)
207 {
208         /* MUST called with resource lock hold but w/o lnet_eq_wait_lock */
209         int index;
210
211         if (eq->eq_size == 0) {
212                 LASSERT(eq->eq_callback != LNET_EQ_HANDLER_NONE);
213                 eq->eq_callback(ev);
214                 return;
215         }
216
217         lnet_eq_wait_lock();
218         ev->sequence = eq->eq_enq_seq++;
219
220         LASSERT(eq->eq_size == LOWEST_BIT_SET(eq->eq_size));
221         index = ev->sequence & (eq->eq_size - 1);
222
223         eq->eq_events[index] = *ev;
224
225         if (eq->eq_callback != LNET_EQ_HANDLER_NONE)
226                 eq->eq_callback(ev);
227
228         /* Wake anyone waiting in LNetEQPoll() */
229         if (waitqueue_active(&the_lnet.ln_eq_waitq))
230                 wake_up_all(&the_lnet.ln_eq_waitq);
231         lnet_eq_wait_unlock();
232 }
233
234 static int
235 lnet_eq_dequeue_event(struct lnet_eq *eq, struct lnet_event *ev)
236 {
237         int             new_index = eq->eq_deq_seq & (eq->eq_size - 1);
238         struct lnet_event       *new_event = &eq->eq_events[new_index];
239         int             rc;
240         ENTRY;
241
242         /* must called with lnet_eq_wait_lock hold */
243         if (LNET_SEQ_GT(eq->eq_deq_seq, new_event->sequence))
244                 RETURN(0);
245
246         /* We've got a new event... */
247         *ev = *new_event;
248
249         CDEBUG(D_INFO, "event: %p, sequence: %lu, eq->size: %u\n",
250                new_event, eq->eq_deq_seq, eq->eq_size);
251
252         /* ...but did it overwrite an event we've not seen yet? */
253         if (eq->eq_deq_seq == new_event->sequence) {
254                 rc = 1;
255         } else {
256                 /* don't complain with CERROR: some EQs are sized small
257                  * anyway; if it's important, the caller should complain */
258                 CDEBUG(D_NET, "Event Queue Overflow: eq seq %lu ev seq %lu\n",
259                        eq->eq_deq_seq, new_event->sequence);
260                 rc = -EOVERFLOW;
261         }
262
263         eq->eq_deq_seq = new_event->sequence + 1;
264         RETURN(rc);
265 }
266
267 /**
268  * A nonblocking function that can be used to get the next event in an EQ.
269  * If an event handler is associated with the EQ, the handler will run before
270  * this function returns successfully. The event is removed from the queue.
271  *
272  * \param eventq A handle for the event queue.
273  * \param event On successful return (1 or -EOVERFLOW), this location will
274  * hold the next event in the EQ.
275  *
276  * \retval 0          No pending event in the EQ.
277  * \retval 1          Indicates success.
278  * \retval -ENOENT    If \a eventq does not point to a valid EQ.
279  * \retval -EOVERFLOW Indicates success (i.e., an event is returned) and that
280  * at least one event between this event and the last event obtained from the
281  * EQ has been dropped due to limited space in the EQ.
282  */
283 int
284 LNetEQGet(struct lnet_handle_eq eventq, struct lnet_event *event)
285 {
286         int which;
287
288         return LNetEQPoll(&eventq, 1, 0,
289                          event, &which);
290 }
291 EXPORT_SYMBOL(LNetEQGet);
292
293 /**
294  * Block the calling process until there is an event in the EQ.
295  * If an event handler is associated with the EQ, the handler will run before
296  * this function returns successfully. This function returns the next event
297  * in the EQ and removes it from the EQ.
298  *
299  * \param eventq A handle for the event queue.
300  * \param event On successful return (1 or -EOVERFLOW), this location will
301  * hold the next event in the EQ.
302  *
303  * \retval 1          Indicates success.
304  * \retval -ENOENT    If \a eventq does not point to a valid EQ.
305  * \retval -EOVERFLOW Indicates success (i.e., an event is returned) and that
306  * at least one event between this event and the last event obtained from the
307  * EQ has been dropped due to limited space in the EQ.
308  */
309 int
310 LNetEQWait(struct lnet_handle_eq eventq, struct lnet_event *event)
311 {
312         int which;
313
314         return LNetEQPoll(&eventq, 1, MAX_SCHEDULE_TIMEOUT,
315                           event, &which);
316 }
317 EXPORT_SYMBOL(LNetEQWait);
318
319 static int
320 lnet_eq_wait_locked(signed long *timeout)
321 __must_hold(&the_lnet.ln_eq_wait_lock)
322 {
323         signed long tms = *timeout;
324         wait_queue_entry_t wl;
325         int wait;
326
327         if (tms == 0)
328                 return -ENXIO; /* don't want to wait and no new event */
329
330         init_waitqueue_entry(&wl, current);
331         add_wait_queue(&the_lnet.ln_eq_waitq, &wl);
332
333         lnet_eq_wait_unlock();
334
335         tms = schedule_timeout_interruptible(tms);
336         wait = tms != 0; /* might need to call here again */
337         *timeout = tms;
338
339         lnet_eq_wait_lock();
340         remove_wait_queue(&the_lnet.ln_eq_waitq, &wl);
341
342         return wait;
343 }
344
345 /**
346  * Block the calling process until there's an event from a set of EQs or
347  * timeout happens.
348  *
349  * If an event handler is associated with the EQ, the handler will run before
350  * this function returns successfully, in which case the corresponding event
351  * is consumed.
352  *
353  * LNetEQPoll() provides a timeout to allow applications to poll, block for a
354  * fixed period, or block indefinitely.
355  *
356  * \param eventqs,neq An array of EQ handles, and size of the array.
357  * \param timeout Time in jiffies to wait for an event to occur on
358  * one of the EQs. The constant MAX_SCHEDULE_TIMEOUT can be used to indicate an
359  * infinite timeout.
360  * \param event,which On successful return (1 or -EOVERFLOW), \a event will
361  * hold the next event in the EQs, and \a which will contain the index of the
362  * EQ from which the event was taken.
363  *
364  * \retval 0          No pending event in the EQs after timeout.
365  * \retval 1          Indicates success.
366  * \retval -EOVERFLOW Indicates success (i.e., an event is returned) and that
367  * at least one event between this event and the last event obtained from the
368  * EQ indicated by \a which has been dropped due to limited space in the EQ.
369  * \retval -ENOENT    If there's an invalid handle in \a eventqs.
370  */
371 int
372 LNetEQPoll(struct lnet_handle_eq *eventqs, int neq, signed long timeout,
373            struct lnet_event *event, int *which)
374 {
375         int     wait = 1;
376         int     rc;
377         int     i;
378         ENTRY;
379
380         LASSERT(the_lnet.ln_refcount > 0);
381
382         if (neq < 1)
383                 RETURN(-ENOENT);
384
385         lnet_eq_wait_lock();
386
387         for (;;) {
388                 for (i = 0; i < neq; i++) {
389                         struct lnet_eq *eq = lnet_handle2eq(&eventqs[i]);
390
391                         if (eq == NULL) {
392                                 lnet_eq_wait_unlock();
393                                 RETURN(-ENOENT);
394                         }
395
396                         rc = lnet_eq_dequeue_event(eq, event);
397                         if (rc != 0) {
398                                 lnet_eq_wait_unlock();
399                                 *which = i;
400                                 RETURN(rc);
401                         }
402                 }
403
404                 if (wait == 0)
405                         break;
406
407                 /*
408                  * return value of lnet_eq_wait_locked:
409                  * -1 : did nothing and it's sure no new event
410                  *  1 : sleep inside and wait until new event
411                  *  0 : don't want to wait anymore, but might have new event
412                  *      so need to call dequeue again
413                  */
414                 wait = lnet_eq_wait_locked(&timeout);
415                 if (wait < 0) /* no new event */
416                         break;
417         }
418
419         lnet_eq_wait_unlock();
420         RETURN(0);
421 }