Linux-libre 4.14.68-gnu
[librecmc/linux-libre.git] / drivers / staging / lustre / 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, 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
39 #include <linux/lnet/lib-lnet.h>
40
41 /**
42  * Create an event queue that has room for \a count number of events.
43  *
44  * The event queue is circular and older events will be overwritten by new
45  * ones if they are not removed in time by the user using the functions
46  * LNetEQGet(), LNetEQWait(), or LNetEQPoll(). It is up to the user to
47  * determine the appropriate size of the event queue to prevent this loss
48  * of events. Note that when EQ handler is specified in \a callback, no
49  * event loss can happen, since the handler is run for each event deposited
50  * into the EQ.
51  *
52  * \param count The number of events to be stored in the event queue. It
53  * will be rounded up to the next power of two.
54  * \param callback A handler function that runs when an event is deposited
55  * into the EQ. The constant value LNET_EQ_HANDLER_NONE can be used to
56  * indicate that no event handler is desired.
57  * \param handle On successful return, this location will hold a handle for
58  * the newly created EQ.
59  *
60  * \retval 0       On success.
61  * \retval -EINVAL If an parameter is not valid.
62  * \retval -ENOMEM If memory for the EQ can't be allocated.
63  *
64  * \see lnet_eq_handler_t for the discussion on EQ handler semantics.
65  */
66 int
67 LNetEQAlloc(unsigned int count, lnet_eq_handler_t callback,
68             struct lnet_handle_eq *handle)
69 {
70         struct lnet_eq *eq;
71
72         LASSERT(the_lnet.ln_refcount > 0);
73
74         /*
75          * We need count to be a power of 2 so that when eq_{enq,deq}_seq
76          * overflow, they don't skip entries, so the queue has the same
77          * apparent capacity at all times
78          */
79         if (count)
80                 count = roundup_pow_of_two(count);
81
82         if (callback != LNET_EQ_HANDLER_NONE && count)
83                 CWARN("EQ callback is guaranteed to get every event, do you still want to set eqcount %d for polling event which will have locking overhead? Please contact with developer to confirm\n", count);
84
85         /*
86          * count can be 0 if only need callback, we can eliminate
87          * overhead of enqueue event
88          */
89         if (!count && callback == LNET_EQ_HANDLER_NONE)
90                 return -EINVAL;
91
92         eq = lnet_eq_alloc();
93         if (!eq)
94                 return -ENOMEM;
95
96         if (count) {
97                 LIBCFS_ALLOC(eq->eq_events, count * sizeof(struct lnet_event));
98                 if (!eq->eq_events)
99                         goto failed;
100                 /*
101                  * NB allocator has set all event sequence numbers to 0,
102                  * so all them should be earlier than eq_deq_seq
103                  */
104         }
105
106         eq->eq_deq_seq = 1;
107         eq->eq_enq_seq = 1;
108         eq->eq_size = count;
109         eq->eq_callback = callback;
110
111         eq->eq_refs = cfs_percpt_alloc(lnet_cpt_table(),
112                                        sizeof(*eq->eq_refs[0]));
113         if (!eq->eq_refs)
114                 goto failed;
115
116         /* MUST hold both exclusive lnet_res_lock */
117         lnet_res_lock(LNET_LOCK_EX);
118         /*
119          * NB: hold lnet_eq_wait_lock for EQ link/unlink, so we can do
120          * both EQ lookup and poll event with only lnet_eq_wait_lock
121          */
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)
135                 LIBCFS_FREE(eq->eq_events, count * sizeof(struct lnet_event));
136
137         if (eq->eq_refs)
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(struct lnet_handle_eq eqh)
157 {
158         struct lnet_eq *eq;
159         struct lnet_event *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_refcount > 0);
167
168         lnet_res_lock(LNET_LOCK_EX);
169         /*
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          */
173         lnet_eq_wait_lock();
174
175         eq = lnet_handle2eq(&eqh);
176         if (!eq) {
177                 rc = -ENOENT;
178                 goto out;
179         }
180
181         cfs_percpt_for_each(ref, i, eq->eq_refs) {
182                 LASSERT(*ref >= 0);
183                 if (!*ref)
184                         continue;
185
186                 CDEBUG(D_NET, "Event equeue (%d: %d) busy on destroy.\n",
187                        i, *ref);
188                 rc = -EBUSY;
189                 goto out;
190         }
191
192         /* stash for free after lock dropped */
193         events = eq->eq_events;
194         size = eq->eq_size;
195         refs = eq->eq_refs;
196
197         lnet_res_lh_invalidate(&eq->eq_lh);
198         list_del(&eq->eq_list);
199         lnet_eq_free(eq);
200  out:
201         lnet_eq_wait_unlock();
202         lnet_res_unlock(LNET_LOCK_EX);
203
204         if (events)
205                 LIBCFS_FREE(events, size * sizeof(struct lnet_event));
206         if (refs)
207                 cfs_percpt_free(refs);
208
209         return rc;
210 }
211 EXPORT_SYMBOL(LNetEQFree);
212
213 void
214 lnet_eq_enqueue_event(struct lnet_eq *eq, struct lnet_event *ev)
215 {
216         /* MUST called with resource lock hold but w/o lnet_eq_wait_lock */
217         int index;
218
219         if (!eq->eq_size) {
220                 LASSERT(eq->eq_callback != LNET_EQ_HANDLER_NONE);
221                 eq->eq_callback(ev);
222                 return;
223         }
224
225         lnet_eq_wait_lock();
226         ev->sequence = eq->eq_enq_seq++;
227
228         LASSERT(eq->eq_size == LOWEST_BIT_SET(eq->eq_size));
229         index = ev->sequence & (eq->eq_size - 1);
230
231         eq->eq_events[index] = *ev;
232
233         if (eq->eq_callback != LNET_EQ_HANDLER_NONE)
234                 eq->eq_callback(ev);
235
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         lnet_eq_wait_unlock();
240 }
241
242 static int
243 lnet_eq_dequeue_event(struct lnet_eq *eq, struct lnet_event *ev)
244 {
245         int new_index = eq->eq_deq_seq & (eq->eq_size - 1);
246         struct lnet_event *new_event = &eq->eq_events[new_index];
247         int rc;
248
249         /* must called with lnet_eq_wait_lock hold */
250         if (LNET_SEQ_GT(eq->eq_deq_seq, new_event->sequence))
251                 return 0;
252
253         /* We've got a new event... */
254         *ev = *new_event;
255
256         CDEBUG(D_INFO, "event: %p, sequence: %lu, eq->size: %u\n",
257                new_event, eq->eq_deq_seq, eq->eq_size);
258
259         /* ...but did it overwrite an event we've not seen yet? */
260         if (eq->eq_deq_seq == new_event->sequence) {
261                 rc = 1;
262         } else {
263                 /*
264                  * don't complain with CERROR: some EQs are sized small
265                  * anyway; if it's important, the caller should complain
266                  */
267                 CDEBUG(D_NET, "Event Queue Overflow: eq seq %lu ev seq %lu\n",
268                        eq->eq_deq_seq, new_event->sequence);
269                 rc = -EOVERFLOW;
270         }
271
272         eq->eq_deq_seq = new_event->sequence + 1;
273         return rc;
274 }
275
276 /**
277  * A nonblocking function that can be used to get the next event in an EQ.
278  * If an event handler is associated with the EQ, the handler will run before
279  * this function returns successfully. The event is removed from the queue.
280  *
281  * \param eventq A handle for the event queue.
282  * \param event On successful return (1 or -EOVERFLOW), this location will
283  * hold the next event in the EQ.
284  *
285  * \retval 0      No pending event in the EQ.
286  * \retval 1      Indicates success.
287  * \retval -ENOENT    If \a eventq does not point to a valid EQ.
288  * \retval -EOVERFLOW Indicates success (i.e., an event is returned) and that
289  * at least one event between this event and the last event obtained from the
290  * EQ has been dropped due to limited space in the EQ.
291  */
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
310 static int
311 lnet_eq_wait_locked(int *timeout_ms)
312 __must_hold(&the_lnet.ln_eq_wait_lock)
313 {
314         int tms = *timeout_ms;
315         int wait;
316         wait_queue_entry_t wl;
317         unsigned long now;
318
319         if (!tms)
320                 return -ENXIO; /* don't want to wait and no new event */
321
322         init_waitqueue_entry(&wl, current);
323         set_current_state(TASK_INTERRUPTIBLE);
324         add_wait_queue(&the_lnet.ln_eq_waitq, &wl);
325
326         lnet_eq_wait_unlock();
327
328         if (tms < 0) {
329                 schedule();
330         } else {
331                 now = jiffies;
332                 schedule_timeout(msecs_to_jiffies(tms));
333                 tms -= jiffies_to_msecs(jiffies - now);
334                 if (tms < 0) /* no more wait but may have new event */
335                         tms = 0;
336         }
337
338         wait = tms; /* might need to call here again */
339         *timeout_ms = tms;
340
341         lnet_eq_wait_lock();
342         remove_wait_queue(&the_lnet.ln_eq_waitq, &wl);
343
344         return wait;
345 }
346
347 /**
348  * Block the calling process until there's an event from a set of EQs or
349  * timeout happens.
350  *
351  * If an event handler is associated with the EQ, the handler will run before
352  * this function returns successfully, in which case the corresponding event
353  * is consumed.
354  *
355  * LNetEQPoll() provides a timeout to allow applications to poll, block for a
356  * fixed period, or block indefinitely.
357  *
358  * \param eventqs,neq An array of EQ handles, and size of the array.
359  * \param timeout_ms Time in milliseconds to wait for an event to occur on
360  * one of the EQs. The constant LNET_TIME_FOREVER can be used to indicate an
361  * infinite timeout.
362  * \param event,which On successful return (1 or -EOVERFLOW), \a event will
363  * hold the next event in the EQs, and \a which will contain the index of the
364  * EQ from which the event was taken.
365  *
366  * \retval 0      No pending event in the EQs after timeout.
367  * \retval 1      Indicates success.
368  * \retval -EOVERFLOW Indicates success (i.e., an event is returned) and that
369  * at least one event between this event and the last event obtained from the
370  * EQ indicated by \a which has been dropped due to limited space in the EQ.
371  * \retval -ENOENT    If there's an invalid handle in \a eventqs.
372  */
373 int
374 LNetEQPoll(struct lnet_handle_eq *eventqs, int neq, int timeout_ms,
375            struct lnet_event *event, int *which)
376 {
377         int wait = 1;
378         int rc;
379         int i;
380
381         LASSERT(the_lnet.ln_refcount > 0);
382
383         if (neq < 1)
384                 return -ENOENT;
385
386         lnet_eq_wait_lock();
387
388         for (;;) {
389                 for (i = 0; i < neq; i++) {
390                         struct lnet_eq *eq = lnet_handle2eq(&eventqs[i]);
391
392                         if (!eq) {
393                                 lnet_eq_wait_unlock();
394                                 return -ENOENT;
395                         }
396
397                         rc = lnet_eq_dequeue_event(eq, event);
398                         if (rc) {
399                                 lnet_eq_wait_unlock();
400                                 *which = i;
401                                 return rc;
402                         }
403                 }
404
405                 if (!wait)
406                         break;
407
408                 /*
409                  * return value of lnet_eq_wait_locked:
410                  * -1 : did nothing and it's sure no new event
411                  *  1 : sleep inside and wait until new event
412                  *  0 : don't want to wait anymore, but might have new event
413                  *      so need to call dequeue again
414                  */
415                 wait = lnet_eq_wait_locked(&timeout_ms);
416                 if (wait < 0) /* no new event */
417                         break;
418         }
419
420         lnet_eq_wait_unlock();
421         return 0;
422 }