hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/fs/eventpoll.c
....@@ -1,14 +1,9 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * fs/eventpoll.c (Efficient event retrieval implementation)
34 * Copyright (C) 2001,...,2009 Davide Libenzi
45 *
5
- * This program is free software; you can redistribute it and/or modify
6
- * it under the terms of the GNU General Public License as published by
7
- * the Free Software Foundation; either version 2 of the License, or
8
- * (at your option) any later version.
9
- *
106 * Davide Libenzi <davidel@xmailserver.org>
11
- *
127 */
138
149 #include <linux/init.h>
....@@ -45,16 +40,18 @@
4540 #include <linux/rculist.h>
4641 #include <net/busy_poll.h>
4742
43
+#include <trace/hooks/fs.h>
44
+
4845 /*
4946 * LOCKING:
5047 * There are three level of locking required by epoll :
5148 *
5249 * 1) epmutex (mutex)
5350 * 2) ep->mtx (mutex)
54
- * 3) ep->wq.lock (spinlock)
51
+ * 3) ep->lock (rwlock)
5552 *
5653 * The acquire order is the one listed above, from 1 to 3.
57
- * We need a spinlock (ep->wq.lock) because we manipulate objects
54
+ * We need a rwlock (ep->lock) because we manipulate objects
5855 * from inside the poll callback, that might be triggered from
5956 * a wake_up() that in turn might be called from IRQ context.
6057 * So we can't sleep inside the poll callback and hence we need
....@@ -86,7 +83,7 @@
8683 * of epoll file descriptors, we use the current recursion depth as
8784 * the lockdep subkey.
8885 * It is possible to drop the "ep->mtx" and to use the global
89
- * mutex "epmutex" (together with "ep->wq.lock") to have it working,
86
+ * mutex "epmutex" (together with "ep->lock") to have it working,
9087 * but having "ep->mtx" will make the interface more scalable.
9188 * Events that require holding "epmutex" are very rare, while for
9289 * normal operations the epoll private "ep->mtx" will guarantee
....@@ -183,8 +180,6 @@
183180 * This structure is stored inside the "private_data" member of the file
184181 * structure and represents the main data structure for the eventpoll
185182 * interface.
186
- *
187
- * Access to it is protected by the lock inside wq.
188183 */
189184 struct eventpoll {
190185 /*
....@@ -204,13 +199,16 @@
204199 /* List of ready file descriptors */
205200 struct list_head rdllist;
206201
202
+ /* Lock which protects rdllist and ovflist */
203
+ rwlock_t lock;
204
+
207205 /* RB tree root used to store monitored fd structs */
208206 struct rb_root_cached rbr;
209207
210208 /*
211209 * This is a single linked list that chains all the "struct epitem" that
212210 * happened while transferring ready events to userspace w/out
213
- * holding ->wq.lock.
211
+ * holding ->lock.
214212 */
215213 struct epitem *ovflist;
216214
....@@ -228,6 +226,11 @@
228226 #ifdef CONFIG_NET_RX_BUSY_POLL
229227 /* used to track busy poll napi_id */
230228 unsigned int napi_id;
229
+#endif
230
+
231
+#ifdef CONFIG_DEBUG_LOCK_ALLOC
232
+ /* tracks wakeup nests for lockdep validation */
233
+ u8 nests;
231234 #endif
232235 };
233236
....@@ -294,7 +297,7 @@
294297
295298 #include <linux/sysctl.h>
296299
297
-static long zero;
300
+static long long_zero;
298301 static long long_max = LONG_MAX;
299302
300303 struct ctl_table epoll_table[] = {
....@@ -304,7 +307,7 @@
304307 .maxlen = sizeof(max_user_watches),
305308 .mode = 0644,
306309 .proc_handler = proc_doulongvec_minmax,
307
- .extra1 = &zero,
310
+ .extra1 = &long_zero,
308311 .extra2 = &long_max,
309312 },
310313 { }
....@@ -357,12 +360,6 @@
357360 return container_of(p, struct ep_pqueue, pt)->epi;
358361 }
359362
360
-/* Tells if the epoll_ctl(2) operation needs an event copy from userspace */
361
-static inline int ep_op_has_event(int op)
362
-{
363
- return op != EPOLL_CTL_DEL;
364
-}
365
-
366363 /* Initialize the poll safe wake up structure */
367364 static void ep_nested_calls_init(struct nested_calls *ncalls)
368365 {
....@@ -380,7 +377,8 @@
380377 */
381378 static inline int ep_events_available(struct eventpoll *ep)
382379 {
383
- return !list_empty(&ep->rdllist) || ep->ovflist != EP_UNACTIVE_PTR;
380
+ return !list_empty_careful(&ep->rdllist) ||
381
+ READ_ONCE(ep->ovflist) != EP_UNACTIVE_PTR;
384382 }
385383
386384 #ifdef CONFIG_NET_RX_BUSY_POLL
....@@ -470,7 +468,6 @@
470468 * no re-entered.
471469 *
472470 * @ncalls: Pointer to the nested_calls structure to be used for this call.
473
- * @max_nests: Maximum number of allowed nesting calls.
474471 * @nproc: Nested call core function pointer.
475472 * @priv: Opaque data to be passed to the @nproc callback.
476473 * @cookie: Cookie to be used to identify this nested call.
....@@ -479,7 +476,7 @@
479476 * Returns: Returns the code returned by the @nproc callback, or -1 if
480477 * the maximum recursion limit has been exceeded.
481478 */
482
-static int ep_call_nested(struct nested_calls *ncalls, int max_nests,
479
+static int ep_call_nested(struct nested_calls *ncalls,
483480 int (*nproc)(void *, void *, int), void *priv,
484481 void *cookie, void *ctx)
485482 {
....@@ -498,7 +495,7 @@
498495 */
499496 list_for_each_entry(tncur, lsthead, llink) {
500497 if (tncur->ctx == ctx &&
501
- (tncur->cookie == cookie || ++call_nests > max_nests)) {
498
+ (tncur->cookie == cookie || ++call_nests > EP_MAX_NESTS)) {
502499 /*
503500 * Ops ... loop detected or maximum nest level reached.
504501 * We abort this wake by breaking the cycle itself.
....@@ -554,35 +551,49 @@
554551 */
555552 #ifdef CONFIG_DEBUG_LOCK_ALLOC
556553
557
-static struct nested_calls poll_safewake_ncalls;
558
-
559
-static int ep_poll_wakeup_proc(void *priv, void *cookie, int call_nests)
554
+static void ep_poll_safewake(struct eventpoll *ep, struct epitem *epi,
555
+ unsigned pollflags)
560556 {
557
+ struct eventpoll *ep_src;
561558 unsigned long flags;
562
- wait_queue_head_t *wqueue = (wait_queue_head_t *)cookie;
559
+ u8 nests = 0;
563560
564
- spin_lock_irqsave_nested(&wqueue->lock, flags, call_nests + 1);
565
- wake_up_locked_poll(wqueue, EPOLLIN);
566
- spin_unlock_irqrestore(&wqueue->lock, flags);
567
-
568
- return 0;
569
-}
570
-
571
-static void ep_poll_safewake(wait_queue_head_t *wq)
572
-{
573
- int this_cpu = get_cpu();
574
-
575
- ep_call_nested(&poll_safewake_ncalls, EP_MAX_NESTS,
576
- ep_poll_wakeup_proc, NULL, wq, (void *) (long) this_cpu);
577
-
578
- put_cpu();
561
+ /*
562
+ * To set the subclass or nesting level for spin_lock_irqsave_nested()
563
+ * it might be natural to create a per-cpu nest count. However, since
564
+ * we can recurse on ep->poll_wait.lock, and a non-raw spinlock can
565
+ * schedule() in the -rt kernel, the per-cpu variable are no longer
566
+ * protected. Thus, we are introducing a per eventpoll nest field.
567
+ * If we are not being call from ep_poll_callback(), epi is NULL and
568
+ * we are at the first level of nesting, 0. Otherwise, we are being
569
+ * called from ep_poll_callback() and if a previous wakeup source is
570
+ * not an epoll file itself, we are at depth 1 since the wakeup source
571
+ * is depth 0. If the wakeup source is a previous epoll file in the
572
+ * wakeup chain then we use its nests value and record ours as
573
+ * nests + 1. The previous epoll file nests value is stable since its
574
+ * already holding its own poll_wait.lock.
575
+ */
576
+ if (epi) {
577
+ if ((is_file_epoll(epi->ffd.file))) {
578
+ ep_src = epi->ffd.file->private_data;
579
+ nests = ep_src->nests;
580
+ } else {
581
+ nests = 1;
582
+ }
583
+ }
584
+ spin_lock_irqsave_nested(&ep->poll_wait.lock, flags, nests);
585
+ ep->nests = nests + 1;
586
+ wake_up_locked_poll(&ep->poll_wait, EPOLLIN | pollflags);
587
+ ep->nests = 0;
588
+ spin_unlock_irqrestore(&ep->poll_wait.lock, flags);
579589 }
580590
581591 #else
582592
583
-static void ep_poll_safewake(wait_queue_head_t *wq)
593
+static void ep_poll_safewake(struct eventpoll *ep, struct epitem *epi,
594
+ unsigned pollflags)
584595 {
585
- wake_up_poll(wq, EPOLLIN);
596
+ wake_up_poll(&ep->poll_wait, EPOLLIN | pollflags);
586597 }
587598
588599 #endif
....@@ -674,7 +685,6 @@
674685 void *priv, int depth, bool ep_locked)
675686 {
676687 __poll_t res;
677
- int pwake = 0;
678688 struct epitem *epi, *nepi;
679689 LIST_HEAD(txlist);
680690
....@@ -696,23 +706,23 @@
696706 * because we want the "sproc" callback to be able to do it
697707 * in a lockless way.
698708 */
699
- spin_lock_irq(&ep->wq.lock);
709
+ write_lock_irq(&ep->lock);
700710 list_splice_init(&ep->rdllist, &txlist);
701
- ep->ovflist = NULL;
702
- spin_unlock_irq(&ep->wq.lock);
711
+ WRITE_ONCE(ep->ovflist, NULL);
712
+ write_unlock_irq(&ep->lock);
703713
704714 /*
705715 * Now call the callback function.
706716 */
707717 res = (*sproc)(ep, &txlist, priv);
708718
709
- spin_lock_irq(&ep->wq.lock);
719
+ write_lock_irq(&ep->lock);
710720 /*
711721 * During the time we spent inside the "sproc" callback, some
712722 * other events might have been queued by the poll callback.
713723 * We re-insert them inside the main ready-list here.
714724 */
715
- for (nepi = ep->ovflist; (epi = nepi) != NULL;
725
+ for (nepi = READ_ONCE(ep->ovflist); (epi = nepi) != NULL;
716726 nepi = epi->next, epi->next = EP_UNACTIVE_PTR) {
717727 /*
718728 * We need to check if the item is already in the list.
....@@ -721,7 +731,11 @@
721731 * contain them, and the list_splice() below takes care of them.
722732 */
723733 if (!ep_is_linked(epi)) {
724
- list_add_tail(&epi->rdllink, &ep->rdllist);
734
+ /*
735
+ * ->ovflist is LIFO, so we have to reverse it in order
736
+ * to keep in FIFO.
737
+ */
738
+ list_add(&epi->rdllink, &ep->rdllist);
725739 ep_pm_stay_awake(epi);
726740 }
727741 }
....@@ -730,7 +744,7 @@
730744 * releasing the lock, events will be queued in the normal way inside
731745 * ep->rdllist.
732746 */
733
- ep->ovflist = EP_UNACTIVE_PTR;
747
+ WRITE_ONCE(ep->ovflist, EP_UNACTIVE_PTR);
734748
735749 /*
736750 * Quickly re-inject items left on "txlist".
....@@ -739,23 +753,14 @@
739753 __pm_relax(ep->ws);
740754
741755 if (!list_empty(&ep->rdllist)) {
742
- /*
743
- * Wake up (if active) both the eventpoll wait list and
744
- * the ->poll() wait list (delayed after we release the lock).
745
- */
746756 if (waitqueue_active(&ep->wq))
747
- wake_up_locked(&ep->wq);
748
- if (waitqueue_active(&ep->poll_wait))
749
- pwake++;
757
+ wake_up(&ep->wq);
750758 }
751
- spin_unlock_irq(&ep->wq.lock);
759
+
760
+ write_unlock_irq(&ep->lock);
752761
753762 if (!ep_locked)
754763 mutex_unlock(&ep->mtx);
755
-
756
- /* We have to call this outside the lock */
757
- if (pwake)
758
- ep_poll_safewake(&ep->poll_wait);
759764
760765 return res;
761766 }
....@@ -788,10 +793,10 @@
788793
789794 rb_erase_cached(&epi->rbn, &ep->rbr);
790795
791
- spin_lock_irq(&ep->wq.lock);
796
+ write_lock_irq(&ep->lock);
792797 if (ep_is_linked(epi))
793798 list_del_init(&epi->rdllink);
794
- spin_unlock_irq(&ep->wq.lock);
799
+ write_unlock_irq(&ep->lock);
795800
796801 wakeup_source_unregister(ep_wakeup_source(epi));
797802 /*
....@@ -815,7 +820,7 @@
815820
816821 /* We need to release all tasks waiting for these file */
817822 if (waitqueue_active(&ep->poll_wait))
818
- ep_poll_safewake(&ep->poll_wait);
823
+ ep_poll_safewake(ep, NULL, 0);
819824
820825 /*
821826 * We need to lock this because we could be hit by
....@@ -841,7 +846,7 @@
841846 * Walks through the whole tree by freeing each "struct epitem". At this
842847 * point we are sure no poll callbacks will be lingering around, and also by
843848 * holding "epmutex" we can be sure that no file cleanup code will hit
844
- * us during this operation. So we can avoid the lock on "ep->wq.lock".
849
+ * us during this operation. So we can avoid the lock on "ep->lock".
845850 * We do not need to lock ep->mtx, either, we only do it to prevent
846851 * a lockdep warning.
847852 */
....@@ -1022,6 +1027,7 @@
10221027 goto free_uid;
10231028
10241029 mutex_init(&ep->mtx);
1030
+ rwlock_init(&ep->lock);
10251031 init_waitqueue_head(&ep->wq);
10261032 init_waitqueue_head(&ep->poll_wait);
10271033 INIT_LIST_HEAD(&ep->rdllist);
....@@ -1067,7 +1073,7 @@
10671073 return epir;
10681074 }
10691075
1070
-#ifdef CONFIG_CHECKPOINT_RESTORE
1076
+#ifdef CONFIG_KCMP
10711077 static struct epitem *ep_find_tfd(struct eventpoll *ep, int tfd, unsigned long toff)
10721078 {
10731079 struct rb_node *rbp;
....@@ -1109,23 +1115,113 @@
11091115
11101116 return file_raw;
11111117 }
1112
-#endif /* CONFIG_CHECKPOINT_RESTORE */
1118
+#endif /* CONFIG_KCMP */
1119
+
1120
+/**
1121
+ * Adds a new entry to the tail of the list in a lockless way, i.e.
1122
+ * multiple CPUs are allowed to call this function concurrently.
1123
+ *
1124
+ * Beware: it is necessary to prevent any other modifications of the
1125
+ * existing list until all changes are completed, in other words
1126
+ * concurrent list_add_tail_lockless() calls should be protected
1127
+ * with a read lock, where write lock acts as a barrier which
1128
+ * makes sure all list_add_tail_lockless() calls are fully
1129
+ * completed.
1130
+ *
1131
+ * Also an element can be locklessly added to the list only in one
1132
+ * direction i.e. either to the tail either to the head, otherwise
1133
+ * concurrent access will corrupt the list.
1134
+ *
1135
+ * Returns %false if element has been already added to the list, %true
1136
+ * otherwise.
1137
+ */
1138
+static inline bool list_add_tail_lockless(struct list_head *new,
1139
+ struct list_head *head)
1140
+{
1141
+ struct list_head *prev;
1142
+
1143
+ /*
1144
+ * This is simple 'new->next = head' operation, but cmpxchg()
1145
+ * is used in order to detect that same element has been just
1146
+ * added to the list from another CPU: the winner observes
1147
+ * new->next == new.
1148
+ */
1149
+ if (cmpxchg(&new->next, new, head) != new)
1150
+ return false;
1151
+
1152
+ /*
1153
+ * Initially ->next of a new element must be updated with the head
1154
+ * (we are inserting to the tail) and only then pointers are atomically
1155
+ * exchanged. XCHG guarantees memory ordering, thus ->next should be
1156
+ * updated before pointers are actually swapped and pointers are
1157
+ * swapped before prev->next is updated.
1158
+ */
1159
+
1160
+ prev = xchg(&head->prev, new);
1161
+
1162
+ /*
1163
+ * It is safe to modify prev->next and new->prev, because a new element
1164
+ * is added only to the tail and new->next is updated before XCHG.
1165
+ */
1166
+
1167
+ prev->next = new;
1168
+ new->prev = prev;
1169
+
1170
+ return true;
1171
+}
1172
+
1173
+/**
1174
+ * Chains a new epi entry to the tail of the ep->ovflist in a lockless way,
1175
+ * i.e. multiple CPUs are allowed to call this function concurrently.
1176
+ *
1177
+ * Returns %false if epi element has been already chained, %true otherwise.
1178
+ */
1179
+static inline bool chain_epi_lockless(struct epitem *epi)
1180
+{
1181
+ struct eventpoll *ep = epi->ep;
1182
+
1183
+ /* Fast preliminary check */
1184
+ if (epi->next != EP_UNACTIVE_PTR)
1185
+ return false;
1186
+
1187
+ /* Check that the same epi has not been just chained from another CPU */
1188
+ if (cmpxchg(&epi->next, EP_UNACTIVE_PTR, NULL) != EP_UNACTIVE_PTR)
1189
+ return false;
1190
+
1191
+ /* Atomically exchange tail */
1192
+ epi->next = xchg(&ep->ovflist, epi);
1193
+
1194
+ return true;
1195
+}
11131196
11141197 /*
11151198 * This is the callback that is passed to the wait queue wakeup
11161199 * mechanism. It is called by the stored file descriptors when they
11171200 * have events to report.
1201
+ *
1202
+ * This callback takes a read lock in order not to content with concurrent
1203
+ * events from another file descriptors, thus all modifications to ->rdllist
1204
+ * or ->ovflist are lockless. Read lock is paired with the write lock from
1205
+ * ep_scan_ready_list(), which stops all list modifications and guarantees
1206
+ * that lists state is seen correctly.
1207
+ *
1208
+ * Another thing worth to mention is that ep_poll_callback() can be called
1209
+ * concurrently for the same @epi from different CPUs if poll table was inited
1210
+ * with several wait queues entries. Plural wakeup from different CPUs of a
1211
+ * single wait queue is serialized by wq.lock, but the case when multiple wait
1212
+ * queues are used should be detected accordingly. This is detected using
1213
+ * cmpxchg() operation.
11181214 */
11191215 static int ep_poll_callback(wait_queue_entry_t *wait, unsigned mode, int sync, void *key)
11201216 {
11211217 int pwake = 0;
1122
- unsigned long flags;
11231218 struct epitem *epi = ep_item_from_wait(wait);
11241219 struct eventpoll *ep = epi->ep;
11251220 __poll_t pollflags = key_to_poll(key);
1221
+ unsigned long flags;
11261222 int ewake = 0;
11271223
1128
- spin_lock_irqsave(&ep->wq.lock, flags);
1224
+ read_lock_irqsave(&ep->lock, flags);
11291225
11301226 ep_set_busy_poll_napi_id(epi);
11311227
....@@ -1153,26 +1249,13 @@
11531249 * semantics). All the events that happen during that period of time are
11541250 * chained in ep->ovflist and requeued later on.
11551251 */
1156
- if (ep->ovflist != EP_UNACTIVE_PTR) {
1157
- if (epi->next == EP_UNACTIVE_PTR) {
1158
- epi->next = ep->ovflist;
1159
- ep->ovflist = epi;
1160
- if (epi->ws) {
1161
- /*
1162
- * Activate ep->ws since epi->ws may get
1163
- * deactivated at any time.
1164
- */
1165
- __pm_stay_awake(ep->ws);
1166
- }
1167
-
1168
- }
1169
- goto out_unlock;
1170
- }
1171
-
1172
- /* If this file is already in the ready list we exit soon */
1173
- if (!ep_is_linked(epi)) {
1174
- list_add_tail(&epi->rdllink, &ep->rdllist);
1175
- ep_pm_stay_awake_rcu(epi);
1252
+ if (READ_ONCE(ep->ovflist) != EP_UNACTIVE_PTR) {
1253
+ if (chain_epi_lockless(epi))
1254
+ ep_pm_stay_awake_rcu(epi);
1255
+ } else if (!ep_is_linked(epi)) {
1256
+ /* In the usual case, add event to ready list. */
1257
+ if (list_add_tail_lockless(&epi->rdllink, &ep->rdllist))
1258
+ ep_pm_stay_awake_rcu(epi);
11761259 }
11771260
11781261 /*
....@@ -1196,17 +1279,17 @@
11961279 break;
11971280 }
11981281 }
1199
- wake_up_locked(&ep->wq);
1282
+ wake_up(&ep->wq);
12001283 }
12011284 if (waitqueue_active(&ep->poll_wait))
12021285 pwake++;
12031286
12041287 out_unlock:
1205
- spin_unlock_irqrestore(&ep->wq.lock, flags);
1288
+ read_unlock_irqrestore(&ep->lock, flags);
12061289
12071290 /* We have to call this outside the lock */
12081291 if (pwake)
1209
- ep_poll_safewake(&ep->poll_wait);
1292
+ ep_poll_safewake(ep, epi, pollflags & EPOLL_URING_WAKE);
12101293
12111294 if (!(epi->event.events & EPOLLEXCLUSIVE))
12121295 ewake = 1;
....@@ -1332,7 +1415,6 @@
13321415 }
13331416 } else {
13341417 error = ep_call_nested(&poll_loop_ncalls,
1335
- EP_MAX_NESTS,
13361418 reverse_path_check_proc,
13371419 child_file, child_file,
13381420 current);
....@@ -1366,7 +1448,7 @@
13661448 /* let's call this for all tfiles */
13671449 list_for_each_entry(current_file, &tfile_check_list, f_tfile_llink) {
13681450 path_count_init();
1369
- error = ep_call_nested(&poll_loop_ncalls, EP_MAX_NESTS,
1451
+ error = ep_call_nested(&poll_loop_ncalls,
13701452 reverse_path_check_proc, current_file,
13711453 current_file, current);
13721454 if (error)
....@@ -1379,15 +1461,20 @@
13791461 {
13801462 struct name_snapshot n;
13811463 struct wakeup_source *ws;
1464
+ char ws_name[64];
13821465
1466
+ strlcpy(ws_name, "eventpoll", sizeof(ws_name));
1467
+ trace_android_vh_ep_create_wakeup_source(ws_name, sizeof(ws_name));
13831468 if (!epi->ep->ws) {
1384
- epi->ep->ws = wakeup_source_register(NULL, "eventpoll");
1469
+ epi->ep->ws = wakeup_source_register(NULL, ws_name);
13851470 if (!epi->ep->ws)
13861471 return -ENOMEM;
13871472 }
13881473
13891474 take_dentry_name_snapshot(&n, epi->ffd.file->f_path.dentry);
1390
- ws = wakeup_source_register(NULL, n.name);
1475
+ strlcpy(ws_name, n.name.name, sizeof(ws_name));
1476
+ trace_android_vh_ep_create_wakeup_source(ws_name, sizeof(ws_name));
1477
+ ws = wakeup_source_register(NULL, ws_name);
13911478 release_dentry_name_snapshot(&n);
13921479
13931480 if (!ws)
....@@ -1489,7 +1576,7 @@
14891576 goto error_unregister;
14901577
14911578 /* We have to drop the new item inside our item list to keep track of it */
1492
- spin_lock_irq(&ep->wq.lock);
1579
+ write_lock_irq(&ep->lock);
14931580
14941581 /* record NAPI ID of new item if present */
14951582 ep_set_busy_poll_napi_id(epi);
....@@ -1501,18 +1588,18 @@
15011588
15021589 /* Notify waiting tasks that events are available */
15031590 if (waitqueue_active(&ep->wq))
1504
- wake_up_locked(&ep->wq);
1591
+ wake_up(&ep->wq);
15051592 if (waitqueue_active(&ep->poll_wait))
15061593 pwake++;
15071594 }
15081595
1509
- spin_unlock_irq(&ep->wq.lock);
1596
+ write_unlock_irq(&ep->lock);
15101597
15111598 atomic_long_inc(&ep->user->epoll_watches);
15121599
15131600 /* We have to call this outside the lock */
15141601 if (pwake)
1515
- ep_poll_safewake(&ep->poll_wait);
1602
+ ep_poll_safewake(ep, NULL, 0);
15161603
15171604 return 0;
15181605
....@@ -1531,10 +1618,10 @@
15311618 * list, since that is used/cleaned only inside a section bound by "mtx".
15321619 * And ep_insert() is called with "mtx" held.
15331620 */
1534
- spin_lock_irq(&ep->wq.lock);
1621
+ write_lock_irq(&ep->lock);
15351622 if (ep_is_linked(epi))
15361623 list_del_init(&epi->rdllink);
1537
- spin_unlock_irq(&ep->wq.lock);
1624
+ write_unlock_irq(&ep->lock);
15381625
15391626 wakeup_source_unregister(ep_wakeup_source(epi));
15401627
....@@ -1578,9 +1665,9 @@
15781665 * 1) Flush epi changes above to other CPUs. This ensures
15791666 * we do not miss events from ep_poll_callback if an
15801667 * event occurs immediately after we call f_op->poll().
1581
- * We need this because we did not take ep->wq.lock while
1668
+ * We need this because we did not take ep->lock while
15821669 * changing epi above (but ep_poll_callback does take
1583
- * ep->wq.lock).
1670
+ * ep->lock).
15841671 *
15851672 * 2) We also need to ensure we do not miss _past_ events
15861673 * when calling f_op->poll(). This barrier also
....@@ -1599,23 +1686,23 @@
15991686 * list, push it inside.
16001687 */
16011688 if (ep_item_poll(epi, &pt, 1)) {
1602
- spin_lock_irq(&ep->wq.lock);
1689
+ write_lock_irq(&ep->lock);
16031690 if (!ep_is_linked(epi)) {
16041691 list_add_tail(&epi->rdllink, &ep->rdllist);
16051692 ep_pm_stay_awake(epi);
16061693
16071694 /* Notify waiting tasks that events are available */
16081695 if (waitqueue_active(&ep->wq))
1609
- wake_up_locked(&ep->wq);
1696
+ wake_up(&ep->wq);
16101697 if (waitqueue_active(&ep->poll_wait))
16111698 pwake++;
16121699 }
1613
- spin_unlock_irq(&ep->wq.lock);
1700
+ write_unlock_irq(&ep->lock);
16141701 }
16151702
16161703 /* We have to call this outside the lock */
16171704 if (pwake)
1618
- ep_poll_safewake(&ep->poll_wait);
1705
+ ep_poll_safewake(ep, NULL, 0);
16191706
16201707 return 0;
16211708 }
....@@ -1625,21 +1712,24 @@
16251712 {
16261713 struct ep_send_events_data *esed = priv;
16271714 __poll_t revents;
1628
- struct epitem *epi;
1629
- struct epoll_event __user *uevent;
1715
+ struct epitem *epi, *tmp;
1716
+ struct epoll_event __user *uevent = esed->events;
16301717 struct wakeup_source *ws;
16311718 poll_table pt;
16321719
16331720 init_poll_funcptr(&pt, NULL);
1721
+ esed->res = 0;
16341722
16351723 /*
16361724 * We can loop without lock because we are passed a task private list.
16371725 * Items cannot vanish during the loop because ep_scan_ready_list() is
16381726 * holding "mtx" during this call.
16391727 */
1640
- for (esed->res = 0, uevent = esed->events;
1641
- !list_empty(head) && esed->res < esed->maxevents;) {
1642
- epi = list_first_entry(head, struct epitem, rdllink);
1728
+ lockdep_assert_held(&ep->mtx);
1729
+
1730
+ list_for_each_entry_safe(epi, tmp, head, rdllink) {
1731
+ if (esed->res >= esed->maxevents)
1732
+ break;
16431733
16441734 /*
16451735 * Activate ep->ws before deactivating epi->ws to prevent
....@@ -1659,42 +1749,42 @@
16591749
16601750 list_del_init(&epi->rdllink);
16611751
1662
- revents = ep_item_poll(epi, &pt, 1);
1663
-
16641752 /*
16651753 * If the event mask intersect the caller-requested one,
16661754 * deliver the event to userspace. Again, ep_scan_ready_list()
1667
- * is holding "mtx", so no operations coming from userspace
1755
+ * is holding ep->mtx, so no operations coming from userspace
16681756 * can change the item.
16691757 */
1670
- if (revents) {
1671
- if (__put_user(revents, &uevent->events) ||
1672
- __put_user(epi->event.data, &uevent->data)) {
1673
- list_add(&epi->rdllink, head);
1674
- ep_pm_stay_awake(epi);
1675
- if (!esed->res)
1676
- esed->res = -EFAULT;
1677
- return 0;
1678
- }
1679
- esed->res++;
1680
- uevent++;
1681
- if (epi->event.events & EPOLLONESHOT)
1682
- epi->event.events &= EP_PRIVATE_BITS;
1683
- else if (!(epi->event.events & EPOLLET)) {
1684
- /*
1685
- * If this file has been added with Level
1686
- * Trigger mode, we need to insert back inside
1687
- * the ready list, so that the next call to
1688
- * epoll_wait() will check again the events
1689
- * availability. At this point, no one can insert
1690
- * into ep->rdllist besides us. The epoll_ctl()
1691
- * callers are locked out by
1692
- * ep_scan_ready_list() holding "mtx" and the
1693
- * poll callback will queue them in ep->ovflist.
1694
- */
1695
- list_add_tail(&epi->rdllink, &ep->rdllist);
1696
- ep_pm_stay_awake(epi);
1697
- }
1758
+ revents = ep_item_poll(epi, &pt, 1);
1759
+ if (!revents)
1760
+ continue;
1761
+
1762
+ if (__put_user(revents, &uevent->events) ||
1763
+ __put_user(epi->event.data, &uevent->data)) {
1764
+ list_add(&epi->rdllink, head);
1765
+ ep_pm_stay_awake(epi);
1766
+ if (!esed->res)
1767
+ esed->res = -EFAULT;
1768
+ return 0;
1769
+ }
1770
+ esed->res++;
1771
+ uevent++;
1772
+ if (epi->event.events & EPOLLONESHOT)
1773
+ epi->event.events &= EP_PRIVATE_BITS;
1774
+ else if (!(epi->event.events & EPOLLET)) {
1775
+ /*
1776
+ * If this file has been added with Level
1777
+ * Trigger mode, we need to insert back inside
1778
+ * the ready list, so that the next call to
1779
+ * epoll_wait() will check again the events
1780
+ * availability. At this point, no one can insert
1781
+ * into ep->rdllist besides us. The epoll_ctl()
1782
+ * callers are locked out by
1783
+ * ep_scan_ready_list() holding "mtx" and the
1784
+ * poll callback will queue them in ep->ovflist.
1785
+ */
1786
+ list_add_tail(&epi->rdllink, &ep->rdllist);
1787
+ ep_pm_stay_awake(epi);
16981788 }
16991789 }
17001790
....@@ -1722,6 +1812,25 @@
17221812
17231813 ktime_get_ts64(&now);
17241814 return timespec64_add_safe(now, ts);
1815
+}
1816
+
1817
+/*
1818
+ * autoremove_wake_function, but remove even on failure to wake up, because we
1819
+ * know that default_wake_function/ttwu will only fail if the thread is already
1820
+ * woken, and in that case the ep_poll loop will remove the entry anyways, not
1821
+ * try to reuse it.
1822
+ */
1823
+static int ep_autoremove_wake_function(struct wait_queue_entry *wq_entry,
1824
+ unsigned int mode, int sync, void *key)
1825
+{
1826
+ int ret = default_wake_function(wq_entry, mode, sync, key);
1827
+
1828
+ /*
1829
+ * Pairs with list_empty_careful in ep_poll, and ensures future loop
1830
+ * iterations see the cause of this wakeup.
1831
+ */
1832
+ list_del_init_careful(&wq_entry->entry);
1833
+ return ret;
17251834 }
17261835
17271836 /**
....@@ -1760,11 +1869,18 @@
17601869 } else if (timeout == 0) {
17611870 /*
17621871 * Avoid the unnecessary trip to the wait queue loop, if the
1763
- * caller specified a non blocking operation.
1872
+ * caller specified a non blocking operation. We still need
1873
+ * lock because we could race and not see an epi being added
1874
+ * to the ready list while in irq callback. Thus incorrectly
1875
+ * returning 0 back to userspace.
17641876 */
17651877 timed_out = 1;
1766
- spin_lock_irq(&ep->wq.lock);
1767
- goto check_events;
1878
+
1879
+ write_lock_irq(&ep->lock);
1880
+ eavail = ep_events_available(ep);
1881
+ write_unlock_irq(&ep->lock);
1882
+
1883
+ goto send_events;
17681884 }
17691885
17701886 fetch_events:
....@@ -1772,65 +1888,100 @@
17721888 if (!ep_events_available(ep))
17731889 ep_busy_loop(ep, timed_out);
17741890
1775
- spin_lock_irq(&ep->wq.lock);
1776
-
1777
- if (!ep_events_available(ep)) {
1778
- /*
1779
- * Busy poll timed out. Drop NAPI ID for now, we can add
1780
- * it back in when we have moved a socket with a valid NAPI
1781
- * ID onto the ready list.
1782
- */
1783
- ep_reset_busy_poll_napi_id(ep);
1784
-
1785
- /*
1786
- * We don't have any available event to return to the caller.
1787
- * We need to sleep here, and we will be wake up by
1788
- * ep_poll_callback() when events will become available.
1789
- */
1790
- init_waitqueue_entry(&wait, current);
1791
- __add_wait_queue_exclusive(&ep->wq, &wait);
1792
-
1793
- for (;;) {
1794
- /*
1795
- * We don't want to sleep if the ep_poll_callback() sends us
1796
- * a wakeup in between. That's why we set the task state
1797
- * to TASK_INTERRUPTIBLE before doing the checks.
1798
- */
1799
- set_current_state(TASK_INTERRUPTIBLE);
1800
- /*
1801
- * Always short-circuit for fatal signals to allow
1802
- * threads to make a timely exit without the chance of
1803
- * finding more events available and fetching
1804
- * repeatedly.
1805
- */
1806
- if (fatal_signal_pending(current)) {
1807
- res = -EINTR;
1808
- break;
1809
- }
1810
- if (ep_events_available(ep) || timed_out)
1811
- break;
1812
- if (signal_pending(current)) {
1813
- res = -EINTR;
1814
- break;
1815
- }
1816
-
1817
- spin_unlock_irq(&ep->wq.lock);
1818
- if (!freezable_schedule_hrtimeout_range(to, slack,
1819
- HRTIMER_MODE_ABS))
1820
- timed_out = 1;
1821
-
1822
- spin_lock_irq(&ep->wq.lock);
1823
- }
1824
-
1825
- __remove_wait_queue(&ep->wq, &wait);
1826
- __set_current_state(TASK_RUNNING);
1827
- }
1828
-check_events:
1829
- /* Is it worth to try to dig for events ? */
18301891 eavail = ep_events_available(ep);
1892
+ if (eavail)
1893
+ goto send_events;
18311894
1832
- spin_unlock_irq(&ep->wq.lock);
1895
+ /*
1896
+ * Busy poll timed out. Drop NAPI ID for now, we can add
1897
+ * it back in when we have moved a socket with a valid NAPI
1898
+ * ID onto the ready list.
1899
+ */
1900
+ ep_reset_busy_poll_napi_id(ep);
18331901
1902
+ do {
1903
+ /*
1904
+ * Internally init_wait() uses autoremove_wake_function(),
1905
+ * thus wait entry is removed from the wait queue on each
1906
+ * wakeup. Why it is important? In case of several waiters
1907
+ * each new wakeup will hit the next waiter, giving it the
1908
+ * chance to harvest new event. Otherwise wakeup can be
1909
+ * lost. This is also good performance-wise, because on
1910
+ * normal wakeup path no need to call __remove_wait_queue()
1911
+ * explicitly, thus ep->lock is not taken, which halts the
1912
+ * event delivery.
1913
+ *
1914
+ * In fact, we now use an even more aggressive function that
1915
+ * unconditionally removes, because we don't reuse the wait
1916
+ * entry between loop iterations. This lets us also avoid the
1917
+ * performance issue if a process is killed, causing all of its
1918
+ * threads to wake up without being removed normally.
1919
+ */
1920
+ init_wait(&wait);
1921
+ wait.func = ep_autoremove_wake_function;
1922
+
1923
+ write_lock_irq(&ep->lock);
1924
+ /*
1925
+ * Barrierless variant, waitqueue_active() is called under
1926
+ * the same lock on wakeup ep_poll_callback() side, so it
1927
+ * is safe to avoid an explicit barrier.
1928
+ */
1929
+ __set_current_state(TASK_INTERRUPTIBLE);
1930
+
1931
+ /*
1932
+ * Do the final check under the lock. ep_scan_ready_list()
1933
+ * plays with two lists (->rdllist and ->ovflist) and there
1934
+ * is always a race when both lists are empty for short
1935
+ * period of time although events are pending, so lock is
1936
+ * important.
1937
+ */
1938
+ eavail = ep_events_available(ep);
1939
+ if (!eavail) {
1940
+ if (signal_pending(current))
1941
+ res = -EINTR;
1942
+ else
1943
+ __add_wait_queue_exclusive(&ep->wq, &wait);
1944
+ }
1945
+ write_unlock_irq(&ep->lock);
1946
+
1947
+ if (!eavail && !res)
1948
+ timed_out = !freezable_schedule_hrtimeout_range(to, slack,
1949
+ HRTIMER_MODE_ABS);
1950
+
1951
+ /*
1952
+ * We were woken up, thus go and try to harvest some events.
1953
+ * If timed out and still on the wait queue, recheck eavail
1954
+ * carefully under lock, below.
1955
+ */
1956
+ eavail = 1;
1957
+ } while (0);
1958
+
1959
+ __set_current_state(TASK_RUNNING);
1960
+
1961
+ if (!list_empty_careful(&wait.entry)) {
1962
+ write_lock_irq(&ep->lock);
1963
+ /*
1964
+ * If the thread timed out and is not on the wait queue, it
1965
+ * means that the thread was woken up after its timeout expired
1966
+ * before it could reacquire the lock. Thus, when wait.entry is
1967
+ * empty, it needs to harvest events.
1968
+ */
1969
+ if (timed_out)
1970
+ eavail = list_empty(&wait.entry);
1971
+ __remove_wait_queue(&ep->wq, &wait);
1972
+ write_unlock_irq(&ep->lock);
1973
+ }
1974
+
1975
+send_events:
1976
+ if (fatal_signal_pending(current)) {
1977
+ /*
1978
+ * Always short-circuit for fatal signals to allow
1979
+ * threads to make a timely exit without the chance of
1980
+ * finding more events available and fetching
1981
+ * repeatedly.
1982
+ */
1983
+ res = -EINTR;
1984
+ }
18341985 /*
18351986 * Try to transfer events to user space. In case we get 0 events and
18361987 * there's still timeout left over, we go trying again in search of
....@@ -1875,7 +2026,7 @@
18752026 ep_tovisit = epi->ffd.file->private_data;
18762027 if (ep_tovisit->gen == loop_check_gen)
18772028 continue;
1878
- error = ep_call_nested(&poll_loop_ncalls, EP_MAX_NESTS,
2029
+ error = ep_call_nested(&poll_loop_ncalls,
18792030 ep_loop_check_proc, epi->ffd.file,
18802031 ep_tovisit, current);
18812032 if (error != 0)
....@@ -1914,7 +2065,7 @@
19142065 */
19152066 static int ep_loop_check(struct eventpoll *ep, struct file *file)
19162067 {
1917
- return ep_call_nested(&poll_loop_ncalls, EP_MAX_NESTS,
2068
+ return ep_call_nested(&poll_loop_ncalls,
19182069 ep_loop_check_proc, file, ep, current);
19192070 }
19202071
....@@ -1991,26 +2142,27 @@
19912142 return do_epoll_create(0);
19922143 }
19932144
1994
-/*
1995
- * The following function implements the controller interface for
1996
- * the eventpoll file that enables the insertion/removal/change of
1997
- * file descriptors inside the interest set.
1998
- */
1999
-SYSCALL_DEFINE4(epoll_ctl, int, epfd, int, op, int, fd,
2000
- struct epoll_event __user *, event)
2145
+static inline int epoll_mutex_lock(struct mutex *mutex, int depth,
2146
+ bool nonblock)
2147
+{
2148
+ if (!nonblock) {
2149
+ mutex_lock_nested(mutex, depth);
2150
+ return 0;
2151
+ }
2152
+ if (mutex_trylock(mutex))
2153
+ return 0;
2154
+ return -EAGAIN;
2155
+}
2156
+
2157
+int do_epoll_ctl(int epfd, int op, int fd, struct epoll_event *epds,
2158
+ bool nonblock)
20012159 {
20022160 int error;
20032161 int full_check = 0;
20042162 struct fd f, tf;
20052163 struct eventpoll *ep;
20062164 struct epitem *epi;
2007
- struct epoll_event epds;
20082165 struct eventpoll *tep = NULL;
2009
-
2010
- error = -EFAULT;
2011
- if (ep_op_has_event(op) &&
2012
- copy_from_user(&epds, event, sizeof(struct epoll_event)))
2013
- goto error_return;
20142166
20152167 error = -EBADF;
20162168 f = fdget(epfd);
....@@ -2029,7 +2181,7 @@
20292181
20302182 /* Check if EPOLLWAKEUP is allowed */
20312183 if (ep_op_has_event(op))
2032
- ep_take_care_of_epollwakeup(&epds);
2184
+ ep_take_care_of_epollwakeup(epds);
20332185
20342186 /*
20352187 * We have to check that the file structure underneath the file descriptor
....@@ -2045,11 +2197,11 @@
20452197 * so EPOLLEXCLUSIVE is not allowed for a EPOLL_CTL_MOD operation.
20462198 * Also, we do not currently supported nested exclusive wakeups.
20472199 */
2048
- if (ep_op_has_event(op) && (epds.events & EPOLLEXCLUSIVE)) {
2200
+ if (ep_op_has_event(op) && (epds->events & EPOLLEXCLUSIVE)) {
20492201 if (op == EPOLL_CTL_MOD)
20502202 goto error_tgt_fput;
20512203 if (op == EPOLL_CTL_ADD && (is_file_epoll(tf.file) ||
2052
- (epds.events & ~EPOLLEXCLUSIVE_OK_BITS)))
2204
+ (epds->events & ~EPOLLEXCLUSIVE_OK_BITS)))
20532205 goto error_tgt_fput;
20542206 }
20552207
....@@ -2074,14 +2226,19 @@
20742226 * deep wakeup paths from forming in parallel through multiple
20752227 * EPOLL_CTL_ADD operations.
20762228 */
2077
- mutex_lock_nested(&ep->mtx, 0);
2229
+ error = epoll_mutex_lock(&ep->mtx, 0, nonblock);
2230
+ if (error)
2231
+ goto error_tgt_fput;
20782232 if (op == EPOLL_CTL_ADD) {
20792233 if (!list_empty(&f.file->f_ep_links) ||
20802234 ep->gen == loop_check_gen ||
20812235 is_file_epoll(tf.file)) {
2082
- full_check = 1;
20832236 mutex_unlock(&ep->mtx);
2084
- mutex_lock(&epmutex);
2237
+ error = epoll_mutex_lock(&epmutex, 0, nonblock);
2238
+ if (error)
2239
+ goto error_tgt_fput;
2240
+ loop_check_gen++;
2241
+ full_check = 1;
20852242 if (is_file_epoll(tf.file)) {
20862243 error = -ELOOP;
20872244 if (ep_loop_check(ep, tf.file) != 0)
....@@ -2091,10 +2248,16 @@
20912248 list_add(&tf.file->f_tfile_llink,
20922249 &tfile_check_list);
20932250 }
2094
- mutex_lock_nested(&ep->mtx, 0);
2251
+ error = epoll_mutex_lock(&ep->mtx, 0, nonblock);
2252
+ if (error)
2253
+ goto error_tgt_fput;
20952254 if (is_file_epoll(tf.file)) {
20962255 tep = tf.file->private_data;
2097
- mutex_lock_nested(&tep->mtx, 1);
2256
+ error = epoll_mutex_lock(&tep->mtx, 1, nonblock);
2257
+ if (error) {
2258
+ mutex_unlock(&ep->mtx);
2259
+ goto error_tgt_fput;
2260
+ }
20982261 }
20992262 }
21002263 }
....@@ -2110,8 +2273,8 @@
21102273 switch (op) {
21112274 case EPOLL_CTL_ADD:
21122275 if (!epi) {
2113
- epds.events |= EPOLLERR | EPOLLHUP;
2114
- error = ep_insert(ep, &epds, tf.file, fd, full_check);
2276
+ epds->events |= EPOLLERR | EPOLLHUP;
2277
+ error = ep_insert(ep, epds, tf.file, fd, full_check);
21152278 } else
21162279 error = -EEXIST;
21172280 break;
....@@ -2124,8 +2287,8 @@
21242287 case EPOLL_CTL_MOD:
21252288 if (epi) {
21262289 if (!(epi->event.events & EPOLLEXCLUSIVE)) {
2127
- epds.events |= EPOLLERR | EPOLLHUP;
2128
- error = ep_modify(ep, epi, &epds);
2290
+ epds->events |= EPOLLERR | EPOLLHUP;
2291
+ error = ep_modify(ep, epi, epds);
21292292 }
21302293 } else
21312294 error = -ENOENT;
....@@ -2151,6 +2314,23 @@
21512314 }
21522315
21532316 /*
2317
+ * The following function implements the controller interface for
2318
+ * the eventpoll file that enables the insertion/removal/change of
2319
+ * file descriptors inside the interest set.
2320
+ */
2321
+SYSCALL_DEFINE4(epoll_ctl, int, epfd, int, op, int, fd,
2322
+ struct epoll_event __user *, event)
2323
+{
2324
+ struct epoll_event epds;
2325
+
2326
+ if (ep_op_has_event(op) &&
2327
+ copy_from_user(&epds, event, sizeof(struct epoll_event)))
2328
+ return -EFAULT;
2329
+
2330
+ return do_epoll_ctl(epfd, op, fd, &epds, false);
2331
+}
2332
+
2333
+/*
21542334 * Implement the event wait interface for the eventpoll file. It is the kernel
21552335 * part of the user space epoll_wait(2).
21562336 */
....@@ -2166,7 +2346,7 @@
21662346 return -EINVAL;
21672347
21682348 /* Verify that the area passed by the user is writeable */
2169
- if (!access_ok(VERIFY_WRITE, events, maxevents * sizeof(struct epoll_event)))
2349
+ if (!access_ok(events, maxevents * sizeof(struct epoll_event)))
21702350 return -EFAULT;
21712351
21722352 /* Get the "struct file *" for the eventpoll file */
....@@ -2211,37 +2391,17 @@
22112391 size_t, sigsetsize)
22122392 {
22132393 int error;
2214
- sigset_t ksigmask, sigsaved;
22152394
22162395 /*
22172396 * If the caller wants a certain signal mask to be set during the wait,
22182397 * we apply it here.
22192398 */
2220
- if (sigmask) {
2221
- if (sigsetsize != sizeof(sigset_t))
2222
- return -EINVAL;
2223
- if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
2224
- return -EFAULT;
2225
- sigsaved = current->blocked;
2226
- set_current_blocked(&ksigmask);
2227
- }
2399
+ error = set_user_sigmask(sigmask, sigsetsize);
2400
+ if (error)
2401
+ return error;
22282402
22292403 error = do_epoll_wait(epfd, events, maxevents, timeout);
2230
-
2231
- /*
2232
- * If we changed the signal mask, we need to restore the original one.
2233
- * In case we've got a signal while waiting, we do not restore the
2234
- * signal mask yet, and we allow do_signal() to deliver the signal on
2235
- * the way back to userspace, before the signal mask is restored.
2236
- */
2237
- if (sigmask) {
2238
- if (error == -EINTR) {
2239
- memcpy(&current->saved_sigmask, &sigsaved,
2240
- sizeof(sigsaved));
2241
- set_restore_sigmask();
2242
- } else
2243
- set_current_blocked(&sigsaved);
2244
- }
2404
+ restore_saved_sigmask_unless(error == -EINTR);
22452405
22462406 return error;
22472407 }
....@@ -2254,37 +2414,17 @@
22542414 compat_size_t, sigsetsize)
22552415 {
22562416 long err;
2257
- sigset_t ksigmask, sigsaved;
22582417
22592418 /*
22602419 * If the caller wants a certain signal mask to be set during the wait,
22612420 * we apply it here.
22622421 */
2263
- if (sigmask) {
2264
- if (sigsetsize != sizeof(compat_sigset_t))
2265
- return -EINVAL;
2266
- if (get_compat_sigset(&ksigmask, sigmask))
2267
- return -EFAULT;
2268
- sigsaved = current->blocked;
2269
- set_current_blocked(&ksigmask);
2270
- }
2422
+ err = set_compat_user_sigmask(sigmask, sigsetsize);
2423
+ if (err)
2424
+ return err;
22712425
22722426 err = do_epoll_wait(epfd, events, maxevents, timeout);
2273
-
2274
- /*
2275
- * If we changed the signal mask, we need to restore the original one.
2276
- * In case we've got a signal while waiting, we do not restore the
2277
- * signal mask yet, and we allow do_signal() to deliver the signal on
2278
- * the way back to userspace, before the signal mask is restored.
2279
- */
2280
- if (sigmask) {
2281
- if (err == -EINTR) {
2282
- memcpy(&current->saved_sigmask, &sigsaved,
2283
- sizeof(sigsaved));
2284
- set_restore_sigmask();
2285
- } else
2286
- set_current_blocked(&sigsaved);
2287
- }
2427
+ restore_saved_sigmask_unless(err == -EINTR);
22882428
22892429 return err;
22902430 }
....@@ -2307,11 +2447,6 @@
23072447 * inclusion loops checks.
23082448 */
23092449 ep_nested_calls_init(&poll_loop_ncalls);
2310
-
2311
-#ifdef CONFIG_DEBUG_LOCK_ALLOC
2312
- /* Initialize the structure used to perform safe poll wait head wake ups */
2313
- ep_nested_calls_init(&poll_safewake_ncalls);
2314
-#endif
23152450
23162451 /*
23172452 * We can have many thousands of epitems, so prevent this from