~ubuntu-branches/ubuntu/precise/linux-ti-omap4/precise

« back to all changes in this revision

Viewing changes to fs/eventpoll.c

  • Committer: Bazaar Package Importer
  • Author(s): Paolo Pisati
  • Date: 2011-06-29 15:23:51 UTC
  • mfrom: (26.1.1 natty-proposed)
  • Revision ID: james.westby@ubuntu.com-20110629152351-xs96tm303d95rpbk
Tags: 3.0.0-1200.2
* Rebased against 3.0.0-6.7
* BSP from TI based on 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
62
62
 * This mutex is acquired by ep_free() during the epoll file
63
63
 * cleanup path and it is also acquired by eventpoll_release_file()
64
64
 * if a file has been pushed inside an epoll set and it is then
65
 
 * close()d without a previous call toepoll_ctl(EPOLL_CTL_DEL).
 
65
 * close()d without a previous call to epoll_ctl(EPOLL_CTL_DEL).
66
66
 * It is also acquired when inserting an epoll fd onto another epoll
67
67
 * fd. We do this so that we walk the epoll tree and ensure that this
68
68
 * insertion does not create a cycle of epoll file descriptors, which
152
152
 
153
153
/*
154
154
 * This structure is stored inside the "private_data" member of the file
155
 
 * structure and rapresent the main data sructure for the eventpoll
 
155
 * structure and represents the main data structure for the eventpoll
156
156
 * interface.
157
157
 */
158
158
struct eventpoll {
159
 
        /* Protect the this structure access */
 
159
        /* Protect the access to this structure */
160
160
        spinlock_t lock;
161
161
 
162
162
        /*
181
181
 
182
182
        /*
183
183
         * This is a single linked list that chains all the "struct epitem" that
184
 
         * happened while transfering ready events to userspace w/out
 
184
         * happened while transferring ready events to userspace w/out
185
185
         * holding ->lock.
186
186
         */
187
187
        struct epitem *ovflist;
316
316
}
317
317
 
318
318
/**
 
319
 * ep_events_available - Checks if ready events might be available.
 
320
 *
 
321
 * @ep: Pointer to the eventpoll context.
 
322
 *
 
323
 * Returns: Returns a value different than zero if ready events are available,
 
324
 *          or zero otherwise.
 
325
 */
 
326
static inline int ep_events_available(struct eventpoll *ep)
 
327
{
 
328
        return !list_empty(&ep->rdllist) || ep->ovflist != EP_UNACTIVE_PTR;
 
329
}
 
330
 
 
331
/**
319
332
 * ep_call_nested - Perform a bound (possibly) nested call, by checking
320
333
 *                  that the recursion limit is not exceeded, and that
321
334
 *                  the same nested call (by the meaning of same cookie) is
593
606
         * We do not need to hold "ep->mtx" here because the epoll file
594
607
         * is on the way to be removed and no one has references to it
595
608
         * anymore. The only hit might come from eventpoll_release_file() but
596
 
         * holding "epmutex" is sufficent here.
 
609
         * holding "epmutex" is sufficient here.
597
610
         */
598
611
        mutex_lock(&epmutex);
599
612
 
707
720
        /*
708
721
         * We don't want to get "file->f_lock" because it is not
709
722
         * necessary. It is not necessary because we're in the "struct file"
710
 
         * cleanup path, and this means that noone is using this file anymore.
 
723
         * cleanup path, and this means that no one is using this file anymore.
711
724
         * So, for example, epoll_ctl() cannot hit here since if we reach this
712
725
         * point, the file counter already went to zero and fget() would fail.
713
726
         * The only hit might come from ep_free() but by holding the mutex
793
806
 
794
807
/*
795
808
 * This is the callback that is passed to the wait queue wakeup
796
 
 * machanism. It is called by the stored file descriptors when they
 
809
 * mechanism. It is called by the stored file descriptors when they
797
810
 * have events to report.
798
811
 */
799
812
static int ep_poll_callback(wait_queue_t *wait, unsigned mode, int sync, void *key)
824
837
                goto out_unlock;
825
838
 
826
839
        /*
827
 
         * If we are trasfering events to userspace, we can hold no locks
 
840
         * If we are transferring events to userspace, we can hold no locks
828
841
         * (because we're accessing user memory, and because of linux f_op->poll()
829
 
         * semantics). All the events that happens during that period of time are
 
842
         * semantics). All the events that happen during that period of time are
830
843
         * chained in ep->ovflist and requeued later on.
831
844
         */
832
845
        if (unlikely(ep->ovflist != EP_UNACTIVE_PTR)) {
1099
1112
                                 * Trigger mode, we need to insert back inside
1100
1113
                                 * the ready list, so that the next call to
1101
1114
                                 * epoll_wait() will check again the events
1102
 
                                 * availability. At this point, noone can insert
 
1115
                                 * availability. At this point, no one can insert
1103
1116
                                 * into ep->rdllist besides us. The epoll_ctl()
1104
1117
                                 * callers are locked out by
1105
1118
                                 * ep_scan_ready_list() holding "mtx" and the
1135
1148
        return timespec_add_safe(now, ts);
1136
1149
}
1137
1150
 
 
1151
/**
 
1152
 * ep_poll - Retrieves ready events, and delivers them to the caller supplied
 
1153
 *           event buffer.
 
1154
 *
 
1155
 * @ep: Pointer to the eventpoll context.
 
1156
 * @events: Pointer to the userspace buffer where the ready events should be
 
1157
 *          stored.
 
1158
 * @maxevents: Size (in terms of number of events) of the caller event buffer.
 
1159
 * @timeout: Maximum timeout for the ready events fetch operation, in
 
1160
 *           milliseconds. If the @timeout is zero, the function will not block,
 
1161
 *           while if the @timeout is less than zero, the function will block
 
1162
 *           until at least one event has been retrieved (or an error
 
1163
 *           occurred).
 
1164
 *
 
1165
 * Returns: Returns the number of ready events which have been fetched, or an
 
1166
 *          error code, in case of error.
 
1167
 */
1138
1168
static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
1139
1169
                   int maxevents, long timeout)
1140
1170
{
1141
 
        int res, eavail, timed_out = 0;
 
1171
        int res = 0, eavail, timed_out = 0;
1142
1172
        unsigned long flags;
1143
 
        long slack;
 
1173
        long slack = 0;
1144
1174
        wait_queue_t wait;
1145
1175
        ktime_t expires, *to = NULL;
1146
1176
 
1151
1181
                to = &expires;
1152
1182
                *to = timespec_to_ktime(end_time);
1153
1183
        } else if (timeout == 0) {
 
1184
                /*
 
1185
                 * Avoid the unnecessary trip to the wait queue loop, if the
 
1186
                 * caller specified a non blocking operation.
 
1187
                 */
1154
1188
                timed_out = 1;
 
1189
                spin_lock_irqsave(&ep->lock, flags);
 
1190
                goto check_events;
1155
1191
        }
1156
1192
 
1157
 
retry:
 
1193
fetch_events:
1158
1194
        spin_lock_irqsave(&ep->lock, flags);
1159
1195
 
1160
 
        res = 0;
1161
 
        if (list_empty(&ep->rdllist)) {
 
1196
        if (!ep_events_available(ep)) {
1162
1197
                /*
1163
1198
                 * We don't have any available event to return to the caller.
1164
1199
                 * We need to sleep here, and we will be wake up by
1174
1209
                         * to TASK_INTERRUPTIBLE before doing the checks.
1175
1210
                         */
1176
1211
                        set_current_state(TASK_INTERRUPTIBLE);
1177
 
                        if (!list_empty(&ep->rdllist) || timed_out)
 
1212
                        if (ep_events_available(ep) || timed_out)
1178
1213
                                break;
1179
1214
                        if (signal_pending(current)) {
1180
1215
                                res = -EINTR;
1191
1226
 
1192
1227
                set_current_state(TASK_RUNNING);
1193
1228
        }
 
1229
check_events:
1194
1230
        /* Is it worth to try to dig for events ? */
1195
 
        eavail = !list_empty(&ep->rdllist) || ep->ovflist != EP_UNACTIVE_PTR;
 
1231
        eavail = ep_events_available(ep);
1196
1232
 
1197
1233
        spin_unlock_irqrestore(&ep->lock, flags);
1198
1234
 
1203
1239
         */
1204
1240
        if (!res && eavail &&
1205
1241
            !(res = ep_send_events(ep, events, maxevents)) && !timed_out)
1206
 
                goto retry;
 
1242
                goto fetch_events;
1207
1243
 
1208
1244
        return res;
1209
1245
}