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