~ubuntu-branches/ubuntu/utopic/linux-hammerhead/utopic

« back to all changes in this revision

Viewing changes to fs/eventpoll.c

  • Committer: Package Import Robot
  • Author(s): Ricardo Salveti de Araujo, Upstream Kernel Changes
  • Date: 2014-07-20 04:30:38 UTC
  • Revision ID: package-import@ubuntu.com-20140720043038-6gq8wf7tbas9b20p
Tags: 3.4.0-1.9
[ Upstream Kernel Changes ]

* epoll: Add a flag, EPOLLWAKEUP, to prevent suspend while epoll events
  are ready
* epoll: Fix user space breakage related to EPOLLWAKEUP

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
#include <linux/bitops.h>
34
34
#include <linux/mutex.h>
35
35
#include <linux/anon_inodes.h>
 
36
#include <linux/device.h>
36
37
#include <linux/freezer.h>
37
38
#include <asm/uaccess.h>
38
39
#include <asm/io.h>
88
89
 */
89
90
 
90
91
/* Epoll private bits inside the event mask */
91
 
#define EP_PRIVATE_BITS (EPOLLONESHOT | EPOLLET)
 
92
#define EP_PRIVATE_BITS (EPOLLWAKEUP | EPOLLONESHOT | EPOLLET)
92
93
 
93
94
/* Maximum number of nesting allowed inside epoll sets */
94
95
#define EP_MAX_NESTS 4
155
156
        /* List header used to link this item to the "struct file" items list */
156
157
        struct list_head fllink;
157
158
 
 
159
        /* wakeup_source used when EPOLLWAKEUP is set */
 
160
        struct wakeup_source *ws;
 
161
 
158
162
        /* The structure that describe the interested events and the source fd */
159
163
        struct epoll_event event;
160
164
};
195
199
         */
196
200
        struct epitem *ovflist;
197
201
 
 
202
        /* wakeup_source used when ep_scan_ready_list is running */
 
203
        struct wakeup_source *ws;
 
204
 
198
205
        /* The user that created the eventpoll descriptor */
199
206
        struct user_struct *user;
200
207
 
589
596
                 * queued into ->ovflist but the "txlist" might already
590
597
                 * contain them, and the list_splice() below takes care of them.
591
598
                 */
592
 
                if (!ep_is_linked(&epi->rdllink))
 
599
                if (!ep_is_linked(&epi->rdllink)) {
593
600
                        list_add_tail(&epi->rdllink, &ep->rdllist);
 
601
                        __pm_stay_awake(epi->ws);
 
602
                }
594
603
        }
595
604
        /*
596
605
         * We need to set back ep->ovflist to EP_UNACTIVE_PTR, so that after
603
612
         * Quickly re-inject items left on "txlist".
604
613
         */
605
614
        list_splice(&txlist, &ep->rdllist);
 
615
        __pm_relax(ep->ws);
606
616
 
607
617
        if (!list_empty(&ep->rdllist)) {
608
618
                /*
657
667
                list_del_init(&epi->rdllink);
658
668
        spin_unlock_irqrestore(&ep->lock, flags);
659
669
 
 
670
        wakeup_source_unregister(epi->ws);
 
671
 
660
672
        /* At this point it is safe to free the eventpoll item */
661
673
        kmem_cache_free(epi_cache, epi);
662
674
 
707
719
        mutex_unlock(&epmutex);
708
720
        mutex_destroy(&ep->mtx);
709
721
        free_uid(ep->user);
 
722
        wakeup_source_unregister(ep->ws);
710
723
        kfree(ep);
711
724
}
712
725
 
738
751
                         * callback, but it's not actually ready, as far as
739
752
                         * caller requested events goes. We can remove it here.
740
753
                         */
 
754
                        __pm_relax(epi->ws);
741
755
                        list_del_init(&epi->rdllink);
742
756
                }
743
757
        }
928
942
                if (epi->next == EP_UNACTIVE_PTR) {
929
943
                        epi->next = ep->ovflist;
930
944
                        ep->ovflist = epi;
 
945
                        if (epi->ws) {
 
946
                                /*
 
947
                                 * Activate ep->ws since epi->ws may get
 
948
                                 * deactivated at any time.
 
949
                                 */
 
950
                                __pm_stay_awake(ep->ws);
 
951
                        }
 
952
 
931
953
                }
932
954
                goto out_unlock;
933
955
        }
934
956
 
935
957
        /* If this file is already in the ready list we exit soon */
936
 
        if (!ep_is_linked(&epi->rdllink))
 
958
        if (!ep_is_linked(&epi->rdllink)) {
937
959
                list_add_tail(&epi->rdllink, &ep->rdllist);
 
960
                __pm_stay_awake(epi->ws);
 
961
        }
938
962
 
939
963
        /*
940
964
         * Wake up ( if active ) both the eventpoll wait list and the ->poll()
1092
1116
        return error;
1093
1117
}
1094
1118
 
 
1119
static int ep_create_wakeup_source(struct epitem *epi)
 
1120
{
 
1121
        const char *name;
 
1122
 
 
1123
        if (!epi->ep->ws) {
 
1124
                epi->ep->ws = wakeup_source_register("eventpoll");
 
1125
                if (!epi->ep->ws)
 
1126
                        return -ENOMEM;
 
1127
        }
 
1128
 
 
1129
        name = epi->ffd.file->f_path.dentry->d_name.name;
 
1130
        epi->ws = wakeup_source_register(name);
 
1131
        if (!epi->ws)
 
1132
                return -ENOMEM;
 
1133
 
 
1134
        return 0;
 
1135
}
 
1136
 
 
1137
static void ep_destroy_wakeup_source(struct epitem *epi)
 
1138
{
 
1139
        wakeup_source_unregister(epi->ws);
 
1140
        epi->ws = NULL;
 
1141
}
 
1142
 
1095
1143
/*
1096
1144
 * Must be called with "mtx" held.
1097
1145
 */
1119
1167
        epi->event = *event;
1120
1168
        epi->nwait = 0;
1121
1169
        epi->next = EP_UNACTIVE_PTR;
 
1170
        if (epi->event.events & EPOLLWAKEUP) {
 
1171
                error = ep_create_wakeup_source(epi);
 
1172
                if (error)
 
1173
                        goto error_create_wakeup_source;
 
1174
        } else {
 
1175
                epi->ws = NULL;
 
1176
        }
1122
1177
 
1123
1178
        /* Initialize the poll table using the queue callback */
1124
1179
        epq.epi = epi;
1165
1220
        /* If the file is already "ready" we drop it inside the ready list */
1166
1221
        if ((revents & event->events) && !ep_is_linked(&epi->rdllink)) {
1167
1222
                list_add_tail(&epi->rdllink, &ep->rdllist);
 
1223
                __pm_stay_awake(epi->ws);
1168
1224
 
1169
1225
                /* Notify waiting tasks that events are available */
1170
1226
                if (waitqueue_active(&ep->wq))
1205
1261
                list_del_init(&epi->rdllink);
1206
1262
        spin_unlock_irqrestore(&ep->lock, flags);
1207
1263
 
 
1264
        wakeup_source_unregister(epi->ws);
 
1265
 
 
1266
error_create_wakeup_source:
1208
1267
        kmem_cache_free(epi_cache, epi);
1209
1268
 
1210
1269
        return error;
1230
1289
        epi->event.events = event->events;
1231
1290
        pt._key = event->events;
1232
1291
        epi->event.data = event->data; /* protected by mtx */
 
1292
        if (epi->event.events & EPOLLWAKEUP) {
 
1293
                if (!epi->ws)
 
1294
                        ep_create_wakeup_source(epi);
 
1295
        } else if (epi->ws) {
 
1296
                ep_destroy_wakeup_source(epi);
 
1297
        }
1233
1298
 
1234
1299
        /*
1235
1300
         * Get current event bits. We can safely use the file* here because
1245
1310
                spin_lock_irq(&ep->lock);
1246
1311
                if (!ep_is_linked(&epi->rdllink)) {
1247
1312
                        list_add_tail(&epi->rdllink, &ep->rdllist);
 
1313
                        __pm_stay_awake(epi->ws);
1248
1314
 
1249
1315
                        /* Notify waiting tasks that events are available */
1250
1316
                        if (waitqueue_active(&ep->wq))
1283
1349
             !list_empty(head) && eventcnt < esed->maxevents;) {
1284
1350
                epi = list_first_entry(head, struct epitem, rdllink);
1285
1351
 
 
1352
                /*
 
1353
                 * Activate ep->ws before deactivating epi->ws to prevent
 
1354
                 * triggering auto-suspend here (in case we reactive epi->ws
 
1355
                 * below).
 
1356
                 *
 
1357
                 * This could be rearranged to delay the deactivation of epi->ws
 
1358
                 * instead, but then epi->ws would temporarily be out of sync
 
1359
                 * with ep_is_linked().
 
1360
                 */
 
1361
                if (epi->ws && epi->ws->active)
 
1362
                        __pm_stay_awake(ep->ws);
 
1363
                __pm_relax(epi->ws);
1286
1364
                list_del_init(&epi->rdllink);
1287
1365
 
1288
1366
                pt._key = epi->event.events;
1299
1377
                        if (__put_user(revents, &uevent->events) ||
1300
1378
                            __put_user(epi->event.data, &uevent->data)) {
1301
1379
                                list_add(&epi->rdllink, head);
 
1380
                                __pm_stay_awake(epi->ws);
1302
1381
                                return eventcnt ? eventcnt : -EFAULT;
1303
1382
                        }
1304
1383
                        eventcnt++;
1318
1397
                                 * poll callback will queue them in ep->ovflist.
1319
1398
                                 */
1320
1399
                                list_add_tail(&epi->rdllink, &ep->rdllist);
 
1400
                                __pm_stay_awake(epi->ws);
1321
1401
                        }
1322
1402
                }
1323
1403
        }
1631
1711
        if (!tfile->f_op || !tfile->f_op->poll)
1632
1712
                goto error_tgt_fput;
1633
1713
 
 
1714
        /* Check if EPOLLWAKEUP is allowed */
 
1715
        if ((epds.events & EPOLLWAKEUP) && !capable(CAP_EPOLLWAKEUP))
 
1716
                epds.events &= ~EPOLLWAKEUP;
 
1717
 
1634
1718
        /*
1635
1719
         * We have to check that the file structure underneath the file descriptor
1636
1720
         * the user passed to us _is_ an eventpoll file. And also we do not permit