Commit | Line | Data |
---|---|---|
d7e09d03 PT |
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 | |
9fdaf8c0 | 42 | #include "../../include/linux/lnet/lib-lnet.h" |
d7e09d03 PT |
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 | { | |
7e7ab095 | 73 | lnet_eq_t *eq; |
d7e09d03 | 74 | |
e49d63ea MO |
75 | LASSERT(the_lnet.ln_init); |
76 | LASSERT(the_lnet.ln_refcount > 0); | |
d7e09d03 | 77 | |
4420cfd3 JS |
78 | /* |
79 | * We need count to be a power of 2 so that when eq_{enq,deq}_seq | |
d7e09d03 | 80 | * overflow, they don't skip entries, so the queue has the same |
4420cfd3 JS |
81 | * apparent capacity at all times |
82 | */ | |
deaf7e1c OD |
83 | if (count) |
84 | count = roundup_pow_of_two(count); | |
d7e09d03 | 85 | |
499217c9 | 86 | if (callback != LNET_EQ_HANDLER_NONE && count != 0) |
a446b47d | 87 | 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); |
d7e09d03 | 88 | |
4420cfd3 JS |
89 | /* |
90 | * count can be 0 if only need callback, we can eliminate | |
91 | * overhead of enqueue event | |
92 | */ | |
d7e09d03 PT |
93 | if (count == 0 && callback == LNET_EQ_HANDLER_NONE) |
94 | return -EINVAL; | |
95 | ||
96 | eq = lnet_eq_alloc(); | |
06ace26e | 97 | if (!eq) |
d7e09d03 PT |
98 | return -ENOMEM; |
99 | ||
100 | if (count != 0) { | |
101 | LIBCFS_ALLOC(eq->eq_events, count * sizeof(lnet_event_t)); | |
06ace26e | 102 | if (!eq->eq_events) |
d7e09d03 | 103 | goto failed; |
4420cfd3 JS |
104 | /* |
105 | * NB allocator has set all event sequence numbers to 0, | |
106 | * so all them should be earlier than eq_deq_seq | |
107 | */ | |
d7e09d03 PT |
108 | } |
109 | ||
110 | eq->eq_deq_seq = 1; | |
111 | eq->eq_enq_seq = 1; | |
112 | eq->eq_size = count; | |
113 | eq->eq_callback = callback; | |
114 | ||
115 | eq->eq_refs = cfs_percpt_alloc(lnet_cpt_table(), | |
116 | sizeof(*eq->eq_refs[0])); | |
06ace26e | 117 | if (!eq->eq_refs) |
d7e09d03 PT |
118 | goto failed; |
119 | ||
120 | /* MUST hold both exclusive lnet_res_lock */ | |
121 | lnet_res_lock(LNET_LOCK_EX); | |
4420cfd3 JS |
122 | /* |
123 | * NB: hold lnet_eq_wait_lock for EQ link/unlink, so we can do | |
124 | * both EQ lookup and poll event with only lnet_eq_wait_lock | |
125 | */ | |
d7e09d03 PT |
126 | lnet_eq_wait_lock(); |
127 | ||
128 | lnet_res_lh_initialize(&the_lnet.ln_eq_container, &eq->eq_lh); | |
129 | list_add(&eq->eq_list, &the_lnet.ln_eq_container.rec_active); | |
130 | ||
131 | lnet_eq_wait_unlock(); | |
132 | lnet_res_unlock(LNET_LOCK_EX); | |
133 | ||
134 | lnet_eq2handle(handle, eq); | |
135 | return 0; | |
136 | ||
137 | failed: | |
06ace26e | 138 | if (eq->eq_events) |
d7e09d03 PT |
139 | LIBCFS_FREE(eq->eq_events, count * sizeof(lnet_event_t)); |
140 | ||
06ace26e | 141 | if (eq->eq_refs) |
d7e09d03 PT |
142 | cfs_percpt_free(eq->eq_refs); |
143 | ||
144 | lnet_eq_free(eq); | |
145 | return -ENOMEM; | |
146 | } | |
147 | EXPORT_SYMBOL(LNetEQAlloc); | |
148 | ||
149 | /** | |
150 | * Release the resources associated with an event queue if it's idle; | |
151 | * otherwise do nothing and it's up to the user to try again. | |
152 | * | |
153 | * \param eqh A handle for the event queue to be released. | |
154 | * | |
155 | * \retval 0 If the EQ is not in use and freed. | |
156 | * \retval -ENOENT If \a eqh does not point to a valid EQ. | |
157 | * \retval -EBUSY If the EQ is still in use by some MDs. | |
158 | */ | |
159 | int | |
160 | LNetEQFree(lnet_handle_eq_t eqh) | |
161 | { | |
7e7ab095 MS |
162 | struct lnet_eq *eq; |
163 | lnet_event_t *events = NULL; | |
164 | int **refs = NULL; | |
165 | int *ref; | |
166 | int rc = 0; | |
167 | int size = 0; | |
168 | int i; | |
d7e09d03 PT |
169 | |
170 | LASSERT(the_lnet.ln_init); | |
171 | LASSERT(the_lnet.ln_refcount > 0); | |
172 | ||
173 | lnet_res_lock(LNET_LOCK_EX); | |
4420cfd3 JS |
174 | /* |
175 | * NB: hold lnet_eq_wait_lock for EQ link/unlink, so we can do | |
176 | * both EQ lookup and poll event with only lnet_eq_wait_lock | |
177 | */ | |
d7e09d03 PT |
178 | lnet_eq_wait_lock(); |
179 | ||
180 | eq = lnet_handle2eq(&eqh); | |
06ace26e | 181 | if (!eq) { |
d7e09d03 PT |
182 | rc = -ENOENT; |
183 | goto out; | |
184 | } | |
185 | ||
186 | cfs_percpt_for_each(ref, i, eq->eq_refs) { | |
187 | LASSERT(*ref >= 0); | |
188 | if (*ref == 0) | |
189 | continue; | |
190 | ||
191 | CDEBUG(D_NET, "Event equeue (%d: %d) busy on destroy.\n", | |
192 | i, *ref); | |
193 | rc = -EBUSY; | |
194 | goto out; | |
195 | } | |
196 | ||
197 | /* stash for free after lock dropped */ | |
7e7ab095 MS |
198 | events = eq->eq_events; |
199 | size = eq->eq_size; | |
200 | refs = eq->eq_refs; | |
d7e09d03 PT |
201 | |
202 | lnet_res_lh_invalidate(&eq->eq_lh); | |
203 | list_del(&eq->eq_list); | |
d9c90615 | 204 | lnet_eq_free(eq); |
d7e09d03 PT |
205 | out: |
206 | lnet_eq_wait_unlock(); | |
207 | lnet_res_unlock(LNET_LOCK_EX); | |
208 | ||
06ace26e | 209 | if (events) |
d7e09d03 | 210 | LIBCFS_FREE(events, size * sizeof(lnet_event_t)); |
06ace26e | 211 | if (refs) |
d7e09d03 PT |
212 | cfs_percpt_free(refs); |
213 | ||
214 | return rc; | |
215 | } | |
216 | EXPORT_SYMBOL(LNetEQFree); | |
217 | ||
218 | void | |
219 | lnet_eq_enqueue_event(lnet_eq_t *eq, lnet_event_t *ev) | |
220 | { | |
221 | /* MUST called with resource lock hold but w/o lnet_eq_wait_lock */ | |
222 | int index; | |
223 | ||
224 | if (eq->eq_size == 0) { | |
225 | LASSERT(eq->eq_callback != LNET_EQ_HANDLER_NONE); | |
226 | eq->eq_callback(ev); | |
227 | return; | |
228 | } | |
229 | ||
230 | lnet_eq_wait_lock(); | |
231 | ev->sequence = eq->eq_enq_seq++; | |
232 | ||
233 | LASSERT(eq->eq_size == LOWEST_BIT_SET(eq->eq_size)); | |
234 | index = ev->sequence & (eq->eq_size - 1); | |
235 | ||
236 | eq->eq_events[index] = *ev; | |
237 | ||
238 | if (eq->eq_callback != LNET_EQ_HANDLER_NONE) | |
239 | eq->eq_callback(ev); | |
240 | ||
241 | /* Wake anyone waiting in LNetEQPoll() */ | |
242 | if (waitqueue_active(&the_lnet.ln_eq_waitq)) | |
243 | wake_up_all(&the_lnet.ln_eq_waitq); | |
244 | lnet_eq_wait_unlock(); | |
245 | } | |
246 | ||
5f5b7384 | 247 | static int |
d7e09d03 PT |
248 | lnet_eq_dequeue_event(lnet_eq_t *eq, lnet_event_t *ev) |
249 | { | |
7e7ab095 MS |
250 | int new_index = eq->eq_deq_seq & (eq->eq_size - 1); |
251 | lnet_event_t *new_event = &eq->eq_events[new_index]; | |
252 | int rc; | |
d7e09d03 PT |
253 | |
254 | /* must called with lnet_eq_wait_lock hold */ | |
255 | if (LNET_SEQ_GT(eq->eq_deq_seq, new_event->sequence)) | |
0a3bdb00 | 256 | return 0; |
d7e09d03 PT |
257 | |
258 | /* We've got a new event... */ | |
259 | *ev = *new_event; | |
260 | ||
261 | CDEBUG(D_INFO, "event: %p, sequence: %lu, eq->size: %u\n", | |
262 | new_event, eq->eq_deq_seq, eq->eq_size); | |
263 | ||
264 | /* ...but did it overwrite an event we've not seen yet? */ | |
265 | if (eq->eq_deq_seq == new_event->sequence) { | |
266 | rc = 1; | |
267 | } else { | |
4420cfd3 JS |
268 | /* |
269 | * don't complain with CERROR: some EQs are sized small | |
270 | * anyway; if it's important, the caller should complain | |
271 | */ | |
d7e09d03 PT |
272 | CDEBUG(D_NET, "Event Queue Overflow: eq seq %lu ev seq %lu\n", |
273 | eq->eq_deq_seq, new_event->sequence); | |
274 | rc = -EOVERFLOW; | |
275 | } | |
276 | ||
277 | eq->eq_deq_seq = new_event->sequence + 1; | |
0a3bdb00 | 278 | return rc; |
d7e09d03 PT |
279 | } |
280 | ||
281 | /** | |
282 | * A nonblocking function that can be used to get the next event in an EQ. | |
283 | * If an event handler is associated with the EQ, the handler will run before | |
284 | * this function returns successfully. The event is removed from the queue. | |
285 | * | |
286 | * \param eventq A handle for the event queue. | |
287 | * \param event On successful return (1 or -EOVERFLOW), this location will | |
288 | * hold the next event in the EQ. | |
289 | * | |
290 | * \retval 0 No pending event in the EQ. | |
291 | * \retval 1 Indicates success. | |
292 | * \retval -ENOENT If \a eventq does not point to a valid EQ. | |
293 | * \retval -EOVERFLOW Indicates success (i.e., an event is returned) and that | |
294 | * at least one event between this event and the last event obtained from the | |
295 | * EQ has been dropped due to limited space in the EQ. | |
296 | */ | |
d7e09d03 PT |
297 | |
298 | /** | |
299 | * Block the calling process until there is an event in the EQ. | |
300 | * If an event handler is associated with the EQ, the handler will run before | |
301 | * this function returns successfully. This function returns the next event | |
302 | * in the EQ and removes it from the EQ. | |
303 | * | |
304 | * \param eventq A handle for the event queue. | |
305 | * \param event On successful return (1 or -EOVERFLOW), this location will | |
306 | * hold the next event in the EQ. | |
307 | * | |
308 | * \retval 1 Indicates success. | |
309 | * \retval -ENOENT If \a eventq does not point to a valid EQ. | |
310 | * \retval -EOVERFLOW Indicates success (i.e., an event is returned) and that | |
311 | * at least one event between this event and the last event obtained from the | |
312 | * EQ has been dropped due to limited space in the EQ. | |
313 | */ | |
d7e09d03 | 314 | |
d7e09d03 PT |
315 | static int |
316 | lnet_eq_wait_locked(int *timeout_ms) | |
e69ae73b | 317 | __must_hold(&the_lnet.ln_eq_wait_lock) |
d7e09d03 | 318 | { |
7e7ab095 MS |
319 | int tms = *timeout_ms; |
320 | int wait; | |
321 | wait_queue_t wl; | |
322 | unsigned long now; | |
d7e09d03 PT |
323 | |
324 | if (tms == 0) | |
325 | return -1; /* don't want to wait and no new event */ | |
326 | ||
9e795d35 | 327 | init_waitqueue_entry(&wl, current); |
d7e09d03 PT |
328 | set_current_state(TASK_INTERRUPTIBLE); |
329 | add_wait_queue(&the_lnet.ln_eq_waitq, &wl); | |
330 | ||
331 | lnet_eq_wait_unlock(); | |
332 | ||
333 | if (tms < 0) { | |
b3669a7f | 334 | schedule(); |
d7e09d03 | 335 | } else { |
a11ef8ca AB |
336 | now = jiffies; |
337 | schedule_timeout(msecs_to_jiffies(tms)); | |
338 | tms -= jiffies_to_msecs(jiffies - now); | |
d7e09d03 PT |
339 | if (tms < 0) /* no more wait but may have new event */ |
340 | tms = 0; | |
341 | } | |
342 | ||
343 | wait = tms != 0; /* might need to call here again */ | |
344 | *timeout_ms = tms; | |
345 | ||
346 | lnet_eq_wait_lock(); | |
347 | remove_wait_queue(&the_lnet.ln_eq_waitq, &wl); | |
348 | ||
349 | return wait; | |
350 | } | |
351 | ||
d7e09d03 PT |
352 | /** |
353 | * Block the calling process until there's an event from a set of EQs or | |
354 | * timeout happens. | |
355 | * | |
356 | * If an event handler is associated with the EQ, the handler will run before | |
357 | * this function returns successfully, in which case the corresponding event | |
358 | * is consumed. | |
359 | * | |
360 | * LNetEQPoll() provides a timeout to allow applications to poll, block for a | |
361 | * fixed period, or block indefinitely. | |
362 | * | |
363 | * \param eventqs,neq An array of EQ handles, and size of the array. | |
364 | * \param timeout_ms Time in milliseconds to wait for an event to occur on | |
365 | * one of the EQs. The constant LNET_TIME_FOREVER can be used to indicate an | |
366 | * infinite timeout. | |
367 | * \param event,which On successful return (1 or -EOVERFLOW), \a event will | |
368 | * hold the next event in the EQs, and \a which will contain the index of the | |
369 | * EQ from which the event was taken. | |
370 | * | |
371 | * \retval 0 No pending event in the EQs after timeout. | |
372 | * \retval 1 Indicates success. | |
373 | * \retval -EOVERFLOW Indicates success (i.e., an event is returned) and that | |
374 | * at least one event between this event and the last event obtained from the | |
375 | * EQ indicated by \a which has been dropped due to limited space in the EQ. | |
376 | * \retval -ENOENT If there's an invalid handle in \a eventqs. | |
377 | */ | |
378 | int | |
379 | LNetEQPoll(lnet_handle_eq_t *eventqs, int neq, int timeout_ms, | |
380 | lnet_event_t *event, int *which) | |
381 | { | |
7e7ab095 MS |
382 | int wait = 1; |
383 | int rc; | |
384 | int i; | |
d7e09d03 | 385 | |
e49d63ea MO |
386 | LASSERT(the_lnet.ln_init); |
387 | LASSERT(the_lnet.ln_refcount > 0); | |
d7e09d03 PT |
388 | |
389 | if (neq < 1) | |
0a3bdb00 | 390 | return -ENOENT; |
d7e09d03 PT |
391 | |
392 | lnet_eq_wait_lock(); | |
393 | ||
394 | for (;;) { | |
395 | for (i = 0; i < neq; i++) { | |
396 | lnet_eq_t *eq = lnet_handle2eq(&eventqs[i]); | |
397 | ||
06ace26e | 398 | if (!eq) { |
d7e09d03 | 399 | lnet_eq_wait_unlock(); |
0a3bdb00 | 400 | return -ENOENT; |
d7e09d03 PT |
401 | } |
402 | ||
403 | rc = lnet_eq_dequeue_event(eq, event); | |
404 | if (rc != 0) { | |
405 | lnet_eq_wait_unlock(); | |
406 | *which = i; | |
0a3bdb00 | 407 | return rc; |
d7e09d03 PT |
408 | } |
409 | } | |
410 | ||
411 | if (wait == 0) | |
412 | break; | |
413 | ||
414 | /* | |
415 | * return value of lnet_eq_wait_locked: | |
416 | * -1 : did nothing and it's sure no new event | |
417 | * 1 : sleep inside and wait until new event | |
418 | * 0 : don't want to wait anymore, but might have new event | |
419 | * so need to call dequeue again | |
420 | */ | |
421 | wait = lnet_eq_wait_locked(&timeout_ms); | |
422 | if (wait < 0) /* no new event */ | |
423 | break; | |
424 | } | |
425 | ||
426 | lnet_eq_wait_unlock(); | |
0a3bdb00 | 427 | return 0; |
d7e09d03 | 428 | } |