]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * This file contains the procedures for the handling of select and poll | |
3 | * | |
4 | * Created for Linux based loosely upon Mathius Lattner's minix | |
5 | * patches by Peter MacDonald. Heavily edited by Linus. | |
6 | * | |
7 | * 4 February 1994 | |
8 | * COFF/ELF binary emulation. If the process has the STICKY_TIMEOUTS | |
9 | * flag set in its personality we do *not* modify the given timeout | |
10 | * parameter to reflect time remaining. | |
11 | * | |
12 | * 24 January 2000 | |
13 | * Changed sys_poll()/do_poll() to use PAGE_SIZE chunk-based allocation | |
14 | * of fds to overcome nfds < 16390 descriptors limit (Tigran Aivazian). | |
15 | */ | |
16 | ||
022a1692 | 17 | #include <linux/kernel.h> |
3f07c014 IM |
18 | #include <linux/sched/signal.h> |
19 | #include <linux/sched/rt.h> | |
1da177e4 | 20 | #include <linux/syscalls.h> |
630d9c47 | 21 | #include <linux/export.h> |
1da177e4 | 22 | #include <linux/slab.h> |
1da177e4 LT |
23 | #include <linux/poll.h> |
24 | #include <linux/personality.h> /* for STICKY_TIMEOUTS */ | |
25 | #include <linux/file.h> | |
9f3acc31 | 26 | #include <linux/fdtable.h> |
1da177e4 | 27 | #include <linux/fs.h> |
b835996f | 28 | #include <linux/rcupdate.h> |
8ff3e8e8 | 29 | #include <linux/hrtimer.h> |
9745cdb3 | 30 | #include <linux/freezer.h> |
076bb0c8 | 31 | #include <net/busy_poll.h> |
2d19309c | 32 | #include <linux/vmalloc.h> |
1da177e4 | 33 | |
7c0f6ba6 | 34 | #include <linux/uaccess.h> |
1da177e4 | 35 | |
90d6e24a AV |
36 | |
37 | /* | |
38 | * Estimate expected accuracy in ns from a timeval. | |
39 | * | |
40 | * After quite a bit of churning around, we've settled on | |
41 | * a simple thing of taking 0.1% of the timeout as the | |
42 | * slack, with a cap of 100 msec. | |
43 | * "nice" tasks get a 0.5% slack instead. | |
44 | * | |
45 | * Consider this comment an open invitation to come up with even | |
46 | * better solutions.. | |
47 | */ | |
48 | ||
5ae87e79 GK |
49 | #define MAX_SLACK (100 * NSEC_PER_MSEC) |
50 | ||
766b9f92 | 51 | static long __estimate_accuracy(struct timespec64 *tv) |
90d6e24a | 52 | { |
96d2ab48 | 53 | long slack; |
90d6e24a AV |
54 | int divfactor = 1000; |
55 | ||
5ae87e79 GK |
56 | if (tv->tv_sec < 0) |
57 | return 0; | |
58 | ||
4ce105d3 | 59 | if (task_nice(current) > 0) |
90d6e24a AV |
60 | divfactor = divfactor / 5; |
61 | ||
5ae87e79 GK |
62 | if (tv->tv_sec > MAX_SLACK / (NSEC_PER_SEC/divfactor)) |
63 | return MAX_SLACK; | |
64 | ||
90d6e24a AV |
65 | slack = tv->tv_nsec / divfactor; |
66 | slack += tv->tv_sec * (NSEC_PER_SEC/divfactor); | |
67 | ||
5ae87e79 GK |
68 | if (slack > MAX_SLACK) |
69 | return MAX_SLACK; | |
96d2ab48 | 70 | |
90d6e24a AV |
71 | return slack; |
72 | } | |
73 | ||
766b9f92 | 74 | u64 select_estimate_accuracy(struct timespec64 *tv) |
90d6e24a | 75 | { |
da8b44d5 | 76 | u64 ret; |
766b9f92 | 77 | struct timespec64 now; |
90d6e24a AV |
78 | |
79 | /* | |
80 | * Realtime tasks get a slack of 0 for obvious reasons. | |
81 | */ | |
82 | ||
4ce105d3 | 83 | if (rt_task(current)) |
90d6e24a AV |
84 | return 0; |
85 | ||
766b9f92 DD |
86 | ktime_get_ts64(&now); |
87 | now = timespec64_sub(*tv, now); | |
90d6e24a AV |
88 | ret = __estimate_accuracy(&now); |
89 | if (ret < current->timer_slack_ns) | |
90 | return current->timer_slack_ns; | |
91 | return ret; | |
92 | } | |
93 | ||
94 | ||
95 | ||
1da177e4 LT |
96 | struct poll_table_page { |
97 | struct poll_table_page * next; | |
98 | struct poll_table_entry * entry; | |
99 | struct poll_table_entry entries[0]; | |
100 | }; | |
101 | ||
102 | #define POLL_TABLE_FULL(table) \ | |
103 | ((unsigned long)((table)->entry+1) > PAGE_SIZE + (unsigned long)(table)) | |
104 | ||
105 | /* | |
106 | * Ok, Peter made a complicated, but straightforward multiple_wait() function. | |
107 | * I have rewritten this, taking some shortcuts: This code may not be easy to | |
108 | * follow, but it should be free of race-conditions, and it's practical. If you | |
109 | * understand what I'm doing here, then you understand how the linux | |
110 | * sleep/wakeup mechanism works. | |
111 | * | |
112 | * Two very simple procedures, poll_wait() and poll_freewait() make all the | |
113 | * work. poll_wait() is an inline-function defined in <linux/poll.h>, | |
114 | * as all select/poll functions have to call it to add an entry to the | |
115 | * poll table. | |
116 | */ | |
75c96f85 AB |
117 | static void __pollwait(struct file *filp, wait_queue_head_t *wait_address, |
118 | poll_table *p); | |
1da177e4 LT |
119 | |
120 | void poll_initwait(struct poll_wqueues *pwq) | |
121 | { | |
122 | init_poll_funcptr(&pwq->pt, __pollwait); | |
5f820f64 | 123 | pwq->polling_task = current; |
b2add73d | 124 | pwq->triggered = 0; |
1da177e4 LT |
125 | pwq->error = 0; |
126 | pwq->table = NULL; | |
70674f95 | 127 | pwq->inline_index = 0; |
1da177e4 | 128 | } |
1da177e4 LT |
129 | EXPORT_SYMBOL(poll_initwait); |
130 | ||
70674f95 AK |
131 | static void free_poll_entry(struct poll_table_entry *entry) |
132 | { | |
ccf6780d | 133 | remove_wait_queue(entry->wait_address, &entry->wait); |
70674f95 AK |
134 | fput(entry->filp); |
135 | } | |
136 | ||
1da177e4 LT |
137 | void poll_freewait(struct poll_wqueues *pwq) |
138 | { | |
139 | struct poll_table_page * p = pwq->table; | |
70674f95 AK |
140 | int i; |
141 | for (i = 0; i < pwq->inline_index; i++) | |
142 | free_poll_entry(pwq->inline_entries + i); | |
1da177e4 LT |
143 | while (p) { |
144 | struct poll_table_entry * entry; | |
145 | struct poll_table_page *old; | |
146 | ||
147 | entry = p->entry; | |
148 | do { | |
149 | entry--; | |
70674f95 | 150 | free_poll_entry(entry); |
1da177e4 LT |
151 | } while (entry > p->entries); |
152 | old = p; | |
153 | p = p->next; | |
154 | free_page((unsigned long) old); | |
155 | } | |
156 | } | |
1da177e4 LT |
157 | EXPORT_SYMBOL(poll_freewait); |
158 | ||
5f820f64 | 159 | static struct poll_table_entry *poll_get_entry(struct poll_wqueues *p) |
1da177e4 | 160 | { |
1da177e4 LT |
161 | struct poll_table_page *table = p->table; |
162 | ||
70674f95 AK |
163 | if (p->inline_index < N_INLINE_POLL_ENTRIES) |
164 | return p->inline_entries + p->inline_index++; | |
165 | ||
1da177e4 LT |
166 | if (!table || POLL_TABLE_FULL(table)) { |
167 | struct poll_table_page *new_table; | |
168 | ||
169 | new_table = (struct poll_table_page *) __get_free_page(GFP_KERNEL); | |
170 | if (!new_table) { | |
171 | p->error = -ENOMEM; | |
70674f95 | 172 | return NULL; |
1da177e4 LT |
173 | } |
174 | new_table->entry = new_table->entries; | |
175 | new_table->next = table; | |
176 | p->table = new_table; | |
177 | table = new_table; | |
178 | } | |
179 | ||
70674f95 AK |
180 | return table->entry++; |
181 | } | |
182 | ||
ac6424b9 | 183 | static int __pollwake(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) |
5f820f64 TH |
184 | { |
185 | struct poll_wqueues *pwq = wait->private; | |
186 | DECLARE_WAITQUEUE(dummy_wait, pwq->polling_task); | |
187 | ||
188 | /* | |
189 | * Although this function is called under waitqueue lock, LOCK | |
190 | * doesn't imply write barrier and the users expect write | |
191 | * barrier semantics on wakeup functions. The following | |
192 | * smp_wmb() is equivalent to smp_wmb() in try_to_wake_up() | |
b92b8b35 | 193 | * and is paired with smp_store_mb() in poll_schedule_timeout. |
5f820f64 TH |
194 | */ |
195 | smp_wmb(); | |
196 | pwq->triggered = 1; | |
197 | ||
198 | /* | |
199 | * Perform the default wake up operation using a dummy | |
200 | * waitqueue. | |
201 | * | |
202 | * TODO: This is hacky but there currently is no interface to | |
203 | * pass in @sync. @sync is scheduled to be removed and once | |
204 | * that happens, wake_up_process() can be used directly. | |
205 | */ | |
206 | return default_wake_function(&dummy_wait, mode, sync, key); | |
207 | } | |
208 | ||
ac6424b9 | 209 | static int pollwake(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) |
4938d7e0 ED |
210 | { |
211 | struct poll_table_entry *entry; | |
212 | ||
213 | entry = container_of(wait, struct poll_table_entry, wait); | |
214 | if (key && !((unsigned long)key & entry->key)) | |
215 | return 0; | |
216 | return __pollwake(wait, mode, sync, key); | |
217 | } | |
218 | ||
70674f95 AK |
219 | /* Add a new entry */ |
220 | static void __pollwait(struct file *filp, wait_queue_head_t *wait_address, | |
221 | poll_table *p) | |
222 | { | |
5f820f64 TH |
223 | struct poll_wqueues *pwq = container_of(p, struct poll_wqueues, pt); |
224 | struct poll_table_entry *entry = poll_get_entry(pwq); | |
70674f95 AK |
225 | if (!entry) |
226 | return; | |
cb0942b8 | 227 | entry->filp = get_file(filp); |
70674f95 | 228 | entry->wait_address = wait_address; |
626cf236 | 229 | entry->key = p->_key; |
5f820f64 TH |
230 | init_waitqueue_func_entry(&entry->wait, pollwake); |
231 | entry->wait.private = pwq; | |
ccf6780d | 232 | add_wait_queue(wait_address, &entry->wait); |
1da177e4 LT |
233 | } |
234 | ||
5f820f64 TH |
235 | int poll_schedule_timeout(struct poll_wqueues *pwq, int state, |
236 | ktime_t *expires, unsigned long slack) | |
237 | { | |
238 | int rc = -EINTR; | |
239 | ||
240 | set_current_state(state); | |
241 | if (!pwq->triggered) | |
59612d18 | 242 | rc = schedule_hrtimeout_range(expires, slack, HRTIMER_MODE_ABS); |
5f820f64 TH |
243 | __set_current_state(TASK_RUNNING); |
244 | ||
245 | /* | |
246 | * Prepare for the next iteration. | |
247 | * | |
b92b8b35 | 248 | * The following smp_store_mb() serves two purposes. First, it's |
5f820f64 TH |
249 | * the counterpart rmb of the wmb in pollwake() such that data |
250 | * written before wake up is always visible after wake up. | |
251 | * Second, the full barrier guarantees that triggered clearing | |
252 | * doesn't pass event check of the next iteration. Note that | |
253 | * this problem doesn't exist for the first iteration as | |
254 | * add_wait_queue() has full barrier semantics. | |
255 | */ | |
b92b8b35 | 256 | smp_store_mb(pwq->triggered, 0); |
5f820f64 TH |
257 | |
258 | return rc; | |
259 | } | |
260 | EXPORT_SYMBOL(poll_schedule_timeout); | |
261 | ||
b773ad40 TG |
262 | /** |
263 | * poll_select_set_timeout - helper function to setup the timeout value | |
766b9f92 | 264 | * @to: pointer to timespec64 variable for the final timeout |
b773ad40 TG |
265 | * @sec: seconds (from user space) |
266 | * @nsec: nanoseconds (from user space) | |
267 | * | |
268 | * Note, we do not use a timespec for the user space value here, That | |
269 | * way we can use the function for timeval and compat interfaces as well. | |
270 | * | |
271 | * Returns -EINVAL if sec/nsec are not normalized. Otherwise 0. | |
272 | */ | |
766b9f92 | 273 | int poll_select_set_timeout(struct timespec64 *to, time64_t sec, long nsec) |
b773ad40 | 274 | { |
766b9f92 | 275 | struct timespec64 ts = {.tv_sec = sec, .tv_nsec = nsec}; |
b773ad40 | 276 | |
766b9f92 | 277 | if (!timespec64_valid(&ts)) |
b773ad40 TG |
278 | return -EINVAL; |
279 | ||
280 | /* Optimize for the zero timeout value here */ | |
281 | if (!sec && !nsec) { | |
282 | to->tv_sec = to->tv_nsec = 0; | |
283 | } else { | |
766b9f92 DD |
284 | ktime_get_ts64(to); |
285 | *to = timespec64_add_safe(*to, ts); | |
b773ad40 TG |
286 | } |
287 | return 0; | |
288 | } | |
289 | ||
766b9f92 DD |
290 | static int poll_select_copy_remaining(struct timespec64 *end_time, |
291 | void __user *p, | |
b773ad40 TG |
292 | int timeval, int ret) |
293 | { | |
36819ad0 | 294 | struct timespec64 rts; |
b773ad40 TG |
295 | struct timeval rtv; |
296 | ||
297 | if (!p) | |
298 | return ret; | |
299 | ||
300 | if (current->personality & STICKY_TIMEOUTS) | |
301 | goto sticky; | |
302 | ||
303 | /* No update for zero timeout */ | |
304 | if (!end_time->tv_sec && !end_time->tv_nsec) | |
305 | return ret; | |
306 | ||
36819ad0 DD |
307 | ktime_get_ts64(&rts); |
308 | rts = timespec64_sub(*end_time, rts); | |
309 | if (rts.tv_sec < 0) | |
310 | rts.tv_sec = rts.tv_nsec = 0; | |
766b9f92 | 311 | |
b773ad40 TG |
312 | |
313 | if (timeval) { | |
65329bf4 VK |
314 | if (sizeof(rtv) > sizeof(rtv.tv_sec) + sizeof(rtv.tv_usec)) |
315 | memset(&rtv, 0, sizeof(rtv)); | |
36819ad0 DD |
316 | rtv.tv_sec = rts.tv_sec; |
317 | rtv.tv_usec = rts.tv_nsec / NSEC_PER_USEC; | |
b773ad40 TG |
318 | |
319 | if (!copy_to_user(p, &rtv, sizeof(rtv))) | |
320 | return ret; | |
321 | ||
36819ad0 | 322 | } else if (!put_timespec64(&rts, p)) |
b773ad40 TG |
323 | return ret; |
324 | ||
325 | /* | |
326 | * If an application puts its timeval in read-only memory, we | |
327 | * don't want the Linux-specific update to the timeval to | |
328 | * cause a fault after the select has completed | |
329 | * successfully. However, because we're not updating the | |
330 | * timeval, we can't restart the system call. | |
331 | */ | |
332 | ||
333 | sticky: | |
334 | if (ret == -ERESTARTNOHAND) | |
335 | ret = -EINTR; | |
336 | return ret; | |
337 | } | |
338 | ||
e99ca56c AV |
339 | /* |
340 | * Scalable version of the fd_set. | |
341 | */ | |
342 | ||
343 | typedef struct { | |
344 | unsigned long *in, *out, *ex; | |
345 | unsigned long *res_in, *res_out, *res_ex; | |
346 | } fd_set_bits; | |
347 | ||
348 | /* | |
349 | * How many longwords for "nr" bits? | |
350 | */ | |
351 | #define FDS_BITPERLONG (8*sizeof(long)) | |
352 | #define FDS_LONGS(nr) (((nr)+FDS_BITPERLONG-1)/FDS_BITPERLONG) | |
353 | #define FDS_BYTES(nr) (FDS_LONGS(nr)*sizeof(long)) | |
354 | ||
355 | /* | |
356 | * We do a VERIFY_WRITE here even though we are only reading this time: | |
357 | * we'll write to it eventually.. | |
358 | * | |
359 | * Use "unsigned long" accesses to let user-mode fd_set's be long-aligned. | |
360 | */ | |
361 | static inline | |
362 | int get_fd_set(unsigned long nr, void __user *ufdset, unsigned long *fdset) | |
363 | { | |
364 | nr = FDS_BYTES(nr); | |
365 | if (ufdset) | |
366 | return copy_from_user(fdset, ufdset, nr) ? -EFAULT : 0; | |
367 | ||
368 | memset(fdset, 0, nr); | |
369 | return 0; | |
370 | } | |
371 | ||
372 | static inline unsigned long __must_check | |
373 | set_fd_set(unsigned long nr, void __user *ufdset, unsigned long *fdset) | |
374 | { | |
375 | if (ufdset) | |
376 | return __copy_to_user(ufdset, fdset, FDS_BYTES(nr)); | |
377 | return 0; | |
378 | } | |
379 | ||
380 | static inline | |
381 | void zero_fd_set(unsigned long nr, unsigned long *fdset) | |
382 | { | |
383 | memset(fdset, 0, FDS_BYTES(nr)); | |
384 | } | |
385 | ||
1da177e4 LT |
386 | #define FDS_IN(fds, n) (fds->in + n) |
387 | #define FDS_OUT(fds, n) (fds->out + n) | |
388 | #define FDS_EX(fds, n) (fds->ex + n) | |
389 | ||
390 | #define BITS(fds, n) (*FDS_IN(fds, n)|*FDS_OUT(fds, n)|*FDS_EX(fds, n)) | |
391 | ||
392 | static int max_select_fd(unsigned long n, fd_set_bits *fds) | |
393 | { | |
394 | unsigned long *open_fds; | |
395 | unsigned long set; | |
396 | int max; | |
badf1662 | 397 | struct fdtable *fdt; |
1da177e4 LT |
398 | |
399 | /* handle last in-complete long-word first */ | |
8ded2bbc JB |
400 | set = ~(~0UL << (n & (BITS_PER_LONG-1))); |
401 | n /= BITS_PER_LONG; | |
badf1662 | 402 | fdt = files_fdtable(current->files); |
1fd36adc | 403 | open_fds = fdt->open_fds + n; |
1da177e4 LT |
404 | max = 0; |
405 | if (set) { | |
406 | set &= BITS(fds, n); | |
407 | if (set) { | |
408 | if (!(set & ~*open_fds)) | |
409 | goto get_max; | |
410 | return -EBADF; | |
411 | } | |
412 | } | |
413 | while (n) { | |
414 | open_fds--; | |
415 | n--; | |
416 | set = BITS(fds, n); | |
417 | if (!set) | |
418 | continue; | |
419 | if (set & ~*open_fds) | |
420 | return -EBADF; | |
421 | if (max) | |
422 | continue; | |
423 | get_max: | |
424 | do { | |
425 | max++; | |
426 | set >>= 1; | |
427 | } while (set); | |
8ded2bbc | 428 | max += n * BITS_PER_LONG; |
1da177e4 LT |
429 | } |
430 | ||
431 | return max; | |
432 | } | |
433 | ||
1da177e4 LT |
434 | #define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR) |
435 | #define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR) | |
436 | #define POLLEX_SET (POLLPRI) | |
437 | ||
4938d7e0 | 438 | static inline void wait_key_set(poll_table *wait, unsigned long in, |
2d48d67f ET |
439 | unsigned long out, unsigned long bit, |
440 | unsigned int ll_flag) | |
4938d7e0 | 441 | { |
2d48d67f | 442 | wait->_key = POLLEX_SET | ll_flag; |
626cf236 HV |
443 | if (in & bit) |
444 | wait->_key |= POLLIN_SET; | |
445 | if (out & bit) | |
446 | wait->_key |= POLLOUT_SET; | |
4938d7e0 ED |
447 | } |
448 | ||
e99ca56c | 449 | static int do_select(int n, fd_set_bits *fds, struct timespec64 *end_time) |
1da177e4 | 450 | { |
8ff3e8e8 | 451 | ktime_t expire, *to = NULL; |
1da177e4 LT |
452 | struct poll_wqueues table; |
453 | poll_table *wait; | |
8ff3e8e8 | 454 | int retval, i, timed_out = 0; |
da8b44d5 | 455 | u64 slack = 0; |
cbf55001 | 456 | unsigned int busy_flag = net_busy_loop_on() ? POLL_BUSY_LOOP : 0; |
37056719 | 457 | unsigned long busy_start = 0; |
1da177e4 | 458 | |
b835996f | 459 | rcu_read_lock(); |
1da177e4 | 460 | retval = max_select_fd(n, fds); |
b835996f | 461 | rcu_read_unlock(); |
1da177e4 LT |
462 | |
463 | if (retval < 0) | |
464 | return retval; | |
465 | n = retval; | |
466 | ||
467 | poll_initwait(&table); | |
468 | wait = &table.pt; | |
8ff3e8e8 | 469 | if (end_time && !end_time->tv_sec && !end_time->tv_nsec) { |
626cf236 | 470 | wait->_qproc = NULL; |
8ff3e8e8 AV |
471 | timed_out = 1; |
472 | } | |
473 | ||
96d2ab48 | 474 | if (end_time && !timed_out) |
231f3d39 | 475 | slack = select_estimate_accuracy(end_time); |
90d6e24a | 476 | |
1da177e4 LT |
477 | retval = 0; |
478 | for (;;) { | |
479 | unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp; | |
cbf55001 | 480 | bool can_busy_loop = false; |
1da177e4 | 481 | |
1da177e4 LT |
482 | inp = fds->in; outp = fds->out; exp = fds->ex; |
483 | rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex; | |
484 | ||
485 | for (i = 0; i < n; ++rinp, ++routp, ++rexp) { | |
486 | unsigned long in, out, ex, all_bits, bit = 1, mask, j; | |
487 | unsigned long res_in = 0, res_out = 0, res_ex = 0; | |
1da177e4 LT |
488 | |
489 | in = *inp++; out = *outp++; ex = *exp++; | |
490 | all_bits = in | out | ex; | |
491 | if (all_bits == 0) { | |
8ded2bbc | 492 | i += BITS_PER_LONG; |
1da177e4 LT |
493 | continue; |
494 | } | |
495 | ||
8ded2bbc | 496 | for (j = 0; j < BITS_PER_LONG; ++j, ++i, bit <<= 1) { |
2903ff01 | 497 | struct fd f; |
1da177e4 LT |
498 | if (i >= n) |
499 | break; | |
500 | if (!(bit & all_bits)) | |
501 | continue; | |
2903ff01 AV |
502 | f = fdget(i); |
503 | if (f.file) { | |
504 | const struct file_operations *f_op; | |
505 | f_op = f.file->f_op; | |
1da177e4 | 506 | mask = DEFAULT_POLLMASK; |
72c2d531 | 507 | if (f_op->poll) { |
2d48d67f | 508 | wait_key_set(wait, in, out, |
cbf55001 | 509 | bit, busy_flag); |
2903ff01 | 510 | mask = (*f_op->poll)(f.file, wait); |
4938d7e0 | 511 | } |
2903ff01 | 512 | fdput(f); |
1da177e4 LT |
513 | if ((mask & POLLIN_SET) && (in & bit)) { |
514 | res_in |= bit; | |
515 | retval++; | |
626cf236 | 516 | wait->_qproc = NULL; |
1da177e4 LT |
517 | } |
518 | if ((mask & POLLOUT_SET) && (out & bit)) { | |
519 | res_out |= bit; | |
520 | retval++; | |
626cf236 | 521 | wait->_qproc = NULL; |
1da177e4 LT |
522 | } |
523 | if ((mask & POLLEX_SET) && (ex & bit)) { | |
524 | res_ex |= bit; | |
525 | retval++; | |
626cf236 | 526 | wait->_qproc = NULL; |
1da177e4 | 527 | } |
2d48d67f | 528 | /* got something, stop busy polling */ |
cbf55001 ET |
529 | if (retval) { |
530 | can_busy_loop = false; | |
531 | busy_flag = 0; | |
532 | ||
533 | /* | |
534 | * only remember a returned | |
535 | * POLL_BUSY_LOOP if we asked for it | |
536 | */ | |
537 | } else if (busy_flag & mask) | |
538 | can_busy_loop = true; | |
539 | ||
1da177e4 | 540 | } |
1da177e4 LT |
541 | } |
542 | if (res_in) | |
543 | *rinp = res_in; | |
544 | if (res_out) | |
545 | *routp = res_out; | |
546 | if (res_ex) | |
547 | *rexp = res_ex; | |
55d85384 | 548 | cond_resched(); |
1da177e4 | 549 | } |
626cf236 | 550 | wait->_qproc = NULL; |
8ff3e8e8 | 551 | if (retval || timed_out || signal_pending(current)) |
1da177e4 | 552 | break; |
f5264481 | 553 | if (table.error) { |
1da177e4 LT |
554 | retval = table.error; |
555 | break; | |
556 | } | |
9f72949f | 557 | |
cbf55001 | 558 | /* only if found POLL_BUSY_LOOP sockets && not out of time */ |
76b1e9b9 | 559 | if (can_busy_loop && !need_resched()) { |
37056719 AD |
560 | if (!busy_start) { |
561 | busy_start = busy_loop_current_time(); | |
76b1e9b9 ET |
562 | continue; |
563 | } | |
37056719 | 564 | if (!busy_loop_timeout(busy_start)) |
76b1e9b9 ET |
565 | continue; |
566 | } | |
567 | busy_flag = 0; | |
2d48d67f | 568 | |
8ff3e8e8 AV |
569 | /* |
570 | * If this is the first loop and we have a timeout | |
571 | * given, then we convert to ktime_t and set the to | |
572 | * pointer to the expiry value. | |
573 | */ | |
574 | if (end_time && !to) { | |
766b9f92 | 575 | expire = timespec64_to_ktime(*end_time); |
8ff3e8e8 | 576 | to = &expire; |
9f72949f | 577 | } |
8ff3e8e8 | 578 | |
5f820f64 TH |
579 | if (!poll_schedule_timeout(&table, TASK_INTERRUPTIBLE, |
580 | to, slack)) | |
8ff3e8e8 | 581 | timed_out = 1; |
1da177e4 | 582 | } |
1da177e4 LT |
583 | |
584 | poll_freewait(&table); | |
585 | ||
1da177e4 LT |
586 | return retval; |
587 | } | |
588 | ||
1da177e4 LT |
589 | /* |
590 | * We can actually return ERESTARTSYS instead of EINTR, but I'd | |
591 | * like to be certain this leads to no problems. So I return | |
592 | * EINTR just for safety. | |
593 | * | |
594 | * Update: ERESTARTSYS breaks at least the xview clock binary, so | |
595 | * I'm trying ERESTARTNOHAND which restart only when you want to. | |
596 | */ | |
a2dcb44c | 597 | int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp, |
766b9f92 | 598 | fd_set __user *exp, struct timespec64 *end_time) |
1da177e4 LT |
599 | { |
600 | fd_set_bits fds; | |
29ff2db5 | 601 | void *bits; |
bbea9f69 | 602 | int ret, max_fds; |
2d19309c | 603 | size_t size, alloc_size; |
badf1662 | 604 | struct fdtable *fdt; |
70674f95 | 605 | /* Allocate small arguments on the stack to save memory and be faster */ |
30c14e40 | 606 | long stack_fds[SELECT_STACK_ALLOC/sizeof(long)]; |
1da177e4 | 607 | |
1da177e4 LT |
608 | ret = -EINVAL; |
609 | if (n < 0) | |
610 | goto out_nofds; | |
611 | ||
bbea9f69 | 612 | /* max_fds can increase, so grab it once to avoid race */ |
b835996f | 613 | rcu_read_lock(); |
badf1662 | 614 | fdt = files_fdtable(current->files); |
bbea9f69 | 615 | max_fds = fdt->max_fds; |
b835996f | 616 | rcu_read_unlock(); |
bbea9f69 VL |
617 | if (n > max_fds) |
618 | n = max_fds; | |
1da177e4 LT |
619 | |
620 | /* | |
621 | * We need 6 bitmaps (in/out/ex for both incoming and outgoing), | |
622 | * since we used fdset we need to allocate memory in units of | |
623 | * long-words. | |
624 | */ | |
1da177e4 | 625 | size = FDS_BYTES(n); |
b04eb6aa MBJ |
626 | bits = stack_fds; |
627 | if (size > sizeof(stack_fds) / 6) { | |
628 | /* Not enough space in on-stack array; must use kmalloc */ | |
629 | ret = -ENOMEM; | |
2d19309c VB |
630 | if (size > (SIZE_MAX / 6)) |
631 | goto out_nofds; | |
632 | ||
633 | alloc_size = 6 * size; | |
752ade68 | 634 | bits = kvmalloc(alloc_size, GFP_KERNEL); |
b04eb6aa MBJ |
635 | if (!bits) |
636 | goto out_nofds; | |
637 | } | |
29ff2db5 AM |
638 | fds.in = bits; |
639 | fds.out = bits + size; | |
640 | fds.ex = bits + 2*size; | |
641 | fds.res_in = bits + 3*size; | |
642 | fds.res_out = bits + 4*size; | |
643 | fds.res_ex = bits + 5*size; | |
1da177e4 LT |
644 | |
645 | if ((ret = get_fd_set(n, inp, fds.in)) || | |
646 | (ret = get_fd_set(n, outp, fds.out)) || | |
647 | (ret = get_fd_set(n, exp, fds.ex))) | |
648 | goto out; | |
649 | zero_fd_set(n, fds.res_in); | |
650 | zero_fd_set(n, fds.res_out); | |
651 | zero_fd_set(n, fds.res_ex); | |
652 | ||
8ff3e8e8 | 653 | ret = do_select(n, &fds, end_time); |
1da177e4 LT |
654 | |
655 | if (ret < 0) | |
656 | goto out; | |
657 | if (!ret) { | |
658 | ret = -ERESTARTNOHAND; | |
659 | if (signal_pending(current)) | |
660 | goto out; | |
661 | ret = 0; | |
662 | } | |
663 | ||
664 | if (set_fd_set(n, inp, fds.res_in) || | |
665 | set_fd_set(n, outp, fds.res_out) || | |
666 | set_fd_set(n, exp, fds.res_ex)) | |
667 | ret = -EFAULT; | |
668 | ||
669 | out: | |
70674f95 | 670 | if (bits != stack_fds) |
2d19309c | 671 | kvfree(bits); |
1da177e4 LT |
672 | out_nofds: |
673 | return ret; | |
674 | } | |
675 | ||
5a8a82b1 HC |
676 | SYSCALL_DEFINE5(select, int, n, fd_set __user *, inp, fd_set __user *, outp, |
677 | fd_set __user *, exp, struct timeval __user *, tvp) | |
9f72949f | 678 | { |
766b9f92 | 679 | struct timespec64 end_time, *to = NULL; |
9f72949f DW |
680 | struct timeval tv; |
681 | int ret; | |
682 | ||
683 | if (tvp) { | |
684 | if (copy_from_user(&tv, tvp, sizeof(tv))) | |
685 | return -EFAULT; | |
686 | ||
8ff3e8e8 | 687 | to = &end_time; |
4d36a9e6 AV |
688 | if (poll_select_set_timeout(to, |
689 | tv.tv_sec + (tv.tv_usec / USEC_PER_SEC), | |
690 | (tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC)) | |
9f72949f | 691 | return -EINVAL; |
9f72949f DW |
692 | } |
693 | ||
8ff3e8e8 AV |
694 | ret = core_sys_select(n, inp, outp, exp, to); |
695 | ret = poll_select_copy_remaining(&end_time, tvp, 1, ret); | |
9f72949f DW |
696 | |
697 | return ret; | |
698 | } | |
699 | ||
c9da9f21 HC |
700 | static long do_pselect(int n, fd_set __user *inp, fd_set __user *outp, |
701 | fd_set __user *exp, struct timespec __user *tsp, | |
702 | const sigset_t __user *sigmask, size_t sigsetsize) | |
9f72949f | 703 | { |
9f72949f | 704 | sigset_t ksigmask, sigsaved; |
36819ad0 | 705 | struct timespec64 ts, end_time, *to = NULL; |
9f72949f DW |
706 | int ret; |
707 | ||
708 | if (tsp) { | |
36819ad0 | 709 | if (get_timespec64(&ts, tsp)) |
9f72949f DW |
710 | return -EFAULT; |
711 | ||
8ff3e8e8 | 712 | to = &end_time; |
36819ad0 | 713 | if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec)) |
9f72949f | 714 | return -EINVAL; |
9f72949f DW |
715 | } |
716 | ||
717 | if (sigmask) { | |
718 | /* XXX: Don't preclude handling different sized sigset_t's. */ | |
719 | if (sigsetsize != sizeof(sigset_t)) | |
720 | return -EINVAL; | |
721 | if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask))) | |
722 | return -EFAULT; | |
723 | ||
724 | sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP)); | |
725 | sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved); | |
726 | } | |
727 | ||
62568510 | 728 | ret = core_sys_select(n, inp, outp, exp, to); |
8ff3e8e8 | 729 | ret = poll_select_copy_remaining(&end_time, tsp, 0, ret); |
9f72949f DW |
730 | |
731 | if (ret == -ERESTARTNOHAND) { | |
732 | /* | |
733 | * Don't restore the signal mask yet. Let do_signal() deliver | |
734 | * the signal on the way back to userspace, before the signal | |
735 | * mask is restored. | |
736 | */ | |
737 | if (sigmask) { | |
738 | memcpy(¤t->saved_sigmask, &sigsaved, | |
739 | sizeof(sigsaved)); | |
4e4c22c7 | 740 | set_restore_sigmask(); |
9f72949f DW |
741 | } |
742 | } else if (sigmask) | |
743 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); | |
744 | ||
745 | return ret; | |
746 | } | |
747 | ||
748 | /* | |
749 | * Most architectures can't handle 7-argument syscalls. So we provide a | |
750 | * 6-argument version where the sixth argument is a pointer to a structure | |
751 | * which has a pointer to the sigset_t itself followed by a size_t containing | |
752 | * the sigset size. | |
753 | */ | |
d4e82042 HC |
754 | SYSCALL_DEFINE6(pselect6, int, n, fd_set __user *, inp, fd_set __user *, outp, |
755 | fd_set __user *, exp, struct timespec __user *, tsp, | |
756 | void __user *, sig) | |
9f72949f DW |
757 | { |
758 | size_t sigsetsize = 0; | |
759 | sigset_t __user *up = NULL; | |
760 | ||
761 | if (sig) { | |
762 | if (!access_ok(VERIFY_READ, sig, sizeof(void *)+sizeof(size_t)) | |
e110ab94 | 763 | || __get_user(up, (sigset_t __user * __user *)sig) |
9f72949f | 764 | || __get_user(sigsetsize, |
e110ab94 | 765 | (size_t __user *)(sig+sizeof(void *)))) |
9f72949f DW |
766 | return -EFAULT; |
767 | } | |
768 | ||
c9da9f21 | 769 | return do_pselect(n, inp, outp, exp, tsp, up, sigsetsize); |
9f72949f | 770 | } |
9f72949f | 771 | |
5d0e5283 CH |
772 | #ifdef __ARCH_WANT_SYS_OLD_SELECT |
773 | struct sel_arg_struct { | |
774 | unsigned long n; | |
775 | fd_set __user *inp, *outp, *exp; | |
776 | struct timeval __user *tvp; | |
777 | }; | |
778 | ||
779 | SYSCALL_DEFINE1(old_select, struct sel_arg_struct __user *, arg) | |
780 | { | |
781 | struct sel_arg_struct a; | |
782 | ||
783 | if (copy_from_user(&a, arg, sizeof(a))) | |
784 | return -EFAULT; | |
785 | return sys_select(a.n, a.inp, a.outp, a.exp, a.tvp); | |
786 | } | |
787 | #endif | |
788 | ||
1da177e4 LT |
789 | struct poll_list { |
790 | struct poll_list *next; | |
791 | int len; | |
792 | struct pollfd entries[0]; | |
793 | }; | |
794 | ||
795 | #define POLLFD_PER_PAGE ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd)) | |
796 | ||
4a4b69f7 VL |
797 | /* |
798 | * Fish for pollable events on the pollfd->fd file descriptor. We're only | |
799 | * interested in events matching the pollfd->events mask, and the result | |
800 | * matching that mask is both recorded in pollfd->revents and returned. The | |
801 | * pwait poll_table will be used by the fd-provided poll handler for waiting, | |
626cf236 | 802 | * if pwait->_qproc is non-NULL. |
4a4b69f7 | 803 | */ |
2d48d67f | 804 | static inline unsigned int do_pollfd(struct pollfd *pollfd, poll_table *pwait, |
cbf55001 ET |
805 | bool *can_busy_poll, |
806 | unsigned int busy_flag) | |
1da177e4 | 807 | { |
4a4b69f7 VL |
808 | unsigned int mask; |
809 | int fd; | |
810 | ||
811 | mask = 0; | |
812 | fd = pollfd->fd; | |
813 | if (fd >= 0) { | |
2903ff01 | 814 | struct fd f = fdget(fd); |
4a4b69f7 | 815 | mask = POLLNVAL; |
2903ff01 | 816 | if (f.file) { |
4a4b69f7 | 817 | mask = DEFAULT_POLLMASK; |
72c2d531 | 818 | if (f.file->f_op->poll) { |
626cf236 | 819 | pwait->_key = pollfd->events|POLLERR|POLLHUP; |
cbf55001 | 820 | pwait->_key |= busy_flag; |
2903ff01 | 821 | mask = f.file->f_op->poll(f.file, pwait); |
cbf55001 ET |
822 | if (mask & busy_flag) |
823 | *can_busy_poll = true; | |
4938d7e0 | 824 | } |
4a4b69f7 VL |
825 | /* Mask out unneeded events. */ |
826 | mask &= pollfd->events | POLLERR | POLLHUP; | |
2903ff01 | 827 | fdput(f); |
1da177e4 | 828 | } |
1da177e4 | 829 | } |
4a4b69f7 VL |
830 | pollfd->revents = mask; |
831 | ||
832 | return mask; | |
1da177e4 LT |
833 | } |
834 | ||
ccec5ee3 | 835 | static int do_poll(struct poll_list *list, struct poll_wqueues *wait, |
766b9f92 | 836 | struct timespec64 *end_time) |
1da177e4 | 837 | { |
1da177e4 | 838 | poll_table* pt = &wait->pt; |
8ff3e8e8 AV |
839 | ktime_t expire, *to = NULL; |
840 | int timed_out = 0, count = 0; | |
da8b44d5 | 841 | u64 slack = 0; |
cbf55001 | 842 | unsigned int busy_flag = net_busy_loop_on() ? POLL_BUSY_LOOP : 0; |
37056719 | 843 | unsigned long busy_start = 0; |
1da177e4 | 844 | |
9f72949f | 845 | /* Optimise the no-wait case */ |
8ff3e8e8 | 846 | if (end_time && !end_time->tv_sec && !end_time->tv_nsec) { |
626cf236 | 847 | pt->_qproc = NULL; |
8ff3e8e8 AV |
848 | timed_out = 1; |
849 | } | |
9bf084f7 | 850 | |
96d2ab48 | 851 | if (end_time && !timed_out) |
231f3d39 | 852 | slack = select_estimate_accuracy(end_time); |
90d6e24a | 853 | |
1da177e4 LT |
854 | for (;;) { |
855 | struct poll_list *walk; | |
cbf55001 | 856 | bool can_busy_loop = false; |
9f72949f | 857 | |
4a4b69f7 VL |
858 | for (walk = list; walk != NULL; walk = walk->next) { |
859 | struct pollfd * pfd, * pfd_end; | |
860 | ||
861 | pfd = walk->entries; | |
862 | pfd_end = pfd + walk->len; | |
863 | for (; pfd != pfd_end; pfd++) { | |
864 | /* | |
865 | * Fish for events. If we found one, record it | |
626cf236 | 866 | * and kill poll_table->_qproc, so we don't |
4a4b69f7 VL |
867 | * needlessly register any other waiters after |
868 | * this. They'll get immediately deregistered | |
869 | * when we break out and return. | |
870 | */ | |
cbf55001 ET |
871 | if (do_pollfd(pfd, pt, &can_busy_loop, |
872 | busy_flag)) { | |
4a4b69f7 | 873 | count++; |
626cf236 | 874 | pt->_qproc = NULL; |
cbf55001 ET |
875 | /* found something, stop busy polling */ |
876 | busy_flag = 0; | |
877 | can_busy_loop = false; | |
4a4b69f7 VL |
878 | } |
879 | } | |
1da177e4 | 880 | } |
4a4b69f7 VL |
881 | /* |
882 | * All waiters have already been registered, so don't provide | |
626cf236 | 883 | * a poll_table->_qproc to them on the next loop iteration. |
4a4b69f7 | 884 | */ |
626cf236 | 885 | pt->_qproc = NULL; |
9bf084f7 ON |
886 | if (!count) { |
887 | count = wait->error; | |
888 | if (signal_pending(current)) | |
889 | count = -EINTR; | |
890 | } | |
8ff3e8e8 | 891 | if (count || timed_out) |
1da177e4 | 892 | break; |
9f72949f | 893 | |
cbf55001 | 894 | /* only if found POLL_BUSY_LOOP sockets && not out of time */ |
76b1e9b9 | 895 | if (can_busy_loop && !need_resched()) { |
37056719 AD |
896 | if (!busy_start) { |
897 | busy_start = busy_loop_current_time(); | |
76b1e9b9 ET |
898 | continue; |
899 | } | |
37056719 | 900 | if (!busy_loop_timeout(busy_start)) |
76b1e9b9 ET |
901 | continue; |
902 | } | |
903 | busy_flag = 0; | |
91e2fd33 | 904 | |
8ff3e8e8 AV |
905 | /* |
906 | * If this is the first loop and we have a timeout | |
907 | * given, then we convert to ktime_t and set the to | |
908 | * pointer to the expiry value. | |
909 | */ | |
910 | if (end_time && !to) { | |
766b9f92 | 911 | expire = timespec64_to_ktime(*end_time); |
8ff3e8e8 | 912 | to = &expire; |
9f72949f DW |
913 | } |
914 | ||
5f820f64 | 915 | if (!poll_schedule_timeout(wait, TASK_INTERRUPTIBLE, to, slack)) |
8ff3e8e8 | 916 | timed_out = 1; |
1da177e4 | 917 | } |
1da177e4 LT |
918 | return count; |
919 | } | |
920 | ||
70674f95 AK |
921 | #define N_STACK_PPS ((sizeof(stack_pps) - sizeof(struct poll_list)) / \ |
922 | sizeof(struct pollfd)) | |
923 | ||
e99ca56c | 924 | static int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds, |
766b9f92 | 925 | struct timespec64 *end_time) |
1da177e4 LT |
926 | { |
927 | struct poll_wqueues table; | |
252e5725 | 928 | int err = -EFAULT, fdcount, len, size; |
30c14e40 JS |
929 | /* Allocate small arguments on the stack to save memory and be |
930 | faster - use long to make sure the buffer is aligned properly | |
931 | on 64 bit archs to avoid unaligned access */ | |
932 | long stack_pps[POLL_STACK_ALLOC/sizeof(long)]; | |
252e5725 ON |
933 | struct poll_list *const head = (struct poll_list *)stack_pps; |
934 | struct poll_list *walk = head; | |
935 | unsigned long todo = nfds; | |
1da177e4 | 936 | |
d554ed89 | 937 | if (nfds > rlimit(RLIMIT_NOFILE)) |
1da177e4 LT |
938 | return -EINVAL; |
939 | ||
252e5725 ON |
940 | len = min_t(unsigned int, nfds, N_STACK_PPS); |
941 | for (;;) { | |
942 | walk->next = NULL; | |
943 | walk->len = len; | |
944 | if (!len) | |
945 | break; | |
1da177e4 | 946 | |
252e5725 ON |
947 | if (copy_from_user(walk->entries, ufds + nfds-todo, |
948 | sizeof(struct pollfd) * walk->len)) | |
949 | goto out_fds; | |
950 | ||
951 | todo -= walk->len; | |
952 | if (!todo) | |
953 | break; | |
1da177e4 | 954 | |
252e5725 ON |
955 | len = min(todo, POLLFD_PER_PAGE); |
956 | size = sizeof(struct poll_list) + sizeof(struct pollfd) * len; | |
957 | walk = walk->next = kmalloc(size, GFP_KERNEL); | |
958 | if (!walk) { | |
959 | err = -ENOMEM; | |
1da177e4 LT |
960 | goto out_fds; |
961 | } | |
1da177e4 | 962 | } |
9f72949f | 963 | |
252e5725 | 964 | poll_initwait(&table); |
ccec5ee3 | 965 | fdcount = do_poll(head, &table, end_time); |
252e5725 | 966 | poll_freewait(&table); |
1da177e4 | 967 | |
252e5725 | 968 | for (walk = head; walk; walk = walk->next) { |
1da177e4 LT |
969 | struct pollfd *fds = walk->entries; |
970 | int j; | |
971 | ||
252e5725 ON |
972 | for (j = 0; j < walk->len; j++, ufds++) |
973 | if (__put_user(fds[j].revents, &ufds->revents)) | |
1da177e4 | 974 | goto out_fds; |
1da177e4 | 975 | } |
252e5725 | 976 | |
1da177e4 | 977 | err = fdcount; |
1da177e4 | 978 | out_fds: |
252e5725 ON |
979 | walk = head->next; |
980 | while (walk) { | |
981 | struct poll_list *pos = walk; | |
982 | walk = walk->next; | |
983 | kfree(pos); | |
1da177e4 | 984 | } |
252e5725 | 985 | |
1da177e4 LT |
986 | return err; |
987 | } | |
9f72949f | 988 | |
3075d9da CW |
989 | static long do_restart_poll(struct restart_block *restart_block) |
990 | { | |
8ff3e8e8 AV |
991 | struct pollfd __user *ufds = restart_block->poll.ufds; |
992 | int nfds = restart_block->poll.nfds; | |
766b9f92 | 993 | struct timespec64 *to = NULL, end_time; |
3075d9da CW |
994 | int ret; |
995 | ||
8ff3e8e8 AV |
996 | if (restart_block->poll.has_timeout) { |
997 | end_time.tv_sec = restart_block->poll.tv_sec; | |
998 | end_time.tv_nsec = restart_block->poll.tv_nsec; | |
999 | to = &end_time; | |
1000 | } | |
1001 | ||
1002 | ret = do_sys_poll(ufds, nfds, to); | |
1003 | ||
3075d9da CW |
1004 | if (ret == -EINTR) { |
1005 | restart_block->fn = do_restart_poll; | |
3075d9da CW |
1006 | ret = -ERESTART_RESTARTBLOCK; |
1007 | } | |
1008 | return ret; | |
1009 | } | |
1010 | ||
5a8a82b1 | 1011 | SYSCALL_DEFINE3(poll, struct pollfd __user *, ufds, unsigned int, nfds, |
faf30900 | 1012 | int, timeout_msecs) |
9f72949f | 1013 | { |
766b9f92 | 1014 | struct timespec64 end_time, *to = NULL; |
3075d9da | 1015 | int ret; |
9f72949f | 1016 | |
8ff3e8e8 AV |
1017 | if (timeout_msecs >= 0) { |
1018 | to = &end_time; | |
1019 | poll_select_set_timeout(to, timeout_msecs / MSEC_PER_SEC, | |
1020 | NSEC_PER_MSEC * (timeout_msecs % MSEC_PER_SEC)); | |
9f72949f DW |
1021 | } |
1022 | ||
8ff3e8e8 AV |
1023 | ret = do_sys_poll(ufds, nfds, to); |
1024 | ||
3075d9da CW |
1025 | if (ret == -EINTR) { |
1026 | struct restart_block *restart_block; | |
8ff3e8e8 | 1027 | |
f56141e3 | 1028 | restart_block = ¤t->restart_block; |
3075d9da | 1029 | restart_block->fn = do_restart_poll; |
8ff3e8e8 AV |
1030 | restart_block->poll.ufds = ufds; |
1031 | restart_block->poll.nfds = nfds; | |
1032 | ||
1033 | if (timeout_msecs >= 0) { | |
1034 | restart_block->poll.tv_sec = end_time.tv_sec; | |
1035 | restart_block->poll.tv_nsec = end_time.tv_nsec; | |
1036 | restart_block->poll.has_timeout = 1; | |
1037 | } else | |
1038 | restart_block->poll.has_timeout = 0; | |
1039 | ||
3075d9da CW |
1040 | ret = -ERESTART_RESTARTBLOCK; |
1041 | } | |
1042 | return ret; | |
9f72949f DW |
1043 | } |
1044 | ||
d4e82042 HC |
1045 | SYSCALL_DEFINE5(ppoll, struct pollfd __user *, ufds, unsigned int, nfds, |
1046 | struct timespec __user *, tsp, const sigset_t __user *, sigmask, | |
1047 | size_t, sigsetsize) | |
9f72949f DW |
1048 | { |
1049 | sigset_t ksigmask, sigsaved; | |
36819ad0 | 1050 | struct timespec64 ts, end_time, *to = NULL; |
9f72949f DW |
1051 | int ret; |
1052 | ||
1053 | if (tsp) { | |
36819ad0 | 1054 | if (get_timespec64(&ts, tsp)) |
9f72949f DW |
1055 | return -EFAULT; |
1056 | ||
8ff3e8e8 AV |
1057 | to = &end_time; |
1058 | if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec)) | |
1059 | return -EINVAL; | |
9f72949f DW |
1060 | } |
1061 | ||
1062 | if (sigmask) { | |
1063 | /* XXX: Don't preclude handling different sized sigset_t's. */ | |
1064 | if (sigsetsize != sizeof(sigset_t)) | |
1065 | return -EINVAL; | |
1066 | if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask))) | |
1067 | return -EFAULT; | |
1068 | ||
1069 | sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP)); | |
1070 | sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved); | |
1071 | } | |
1072 | ||
8ff3e8e8 | 1073 | ret = do_sys_poll(ufds, nfds, to); |
9f72949f DW |
1074 | |
1075 | /* We can restart this syscall, usually */ | |
1076 | if (ret == -EINTR) { | |
1077 | /* | |
1078 | * Don't restore the signal mask yet. Let do_signal() deliver | |
1079 | * the signal on the way back to userspace, before the signal | |
1080 | * mask is restored. | |
1081 | */ | |
1082 | if (sigmask) { | |
1083 | memcpy(¤t->saved_sigmask, &sigsaved, | |
1084 | sizeof(sigsaved)); | |
4e4c22c7 | 1085 | set_restore_sigmask(); |
9f72949f DW |
1086 | } |
1087 | ret = -ERESTARTNOHAND; | |
1088 | } else if (sigmask) | |
1089 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); | |
1090 | ||
8ff3e8e8 | 1091 | ret = poll_select_copy_remaining(&end_time, tsp, 0, ret); |
9f72949f DW |
1092 | |
1093 | return ret; | |
1094 | } | |
e99ca56c AV |
1095 | |
1096 | #ifdef CONFIG_COMPAT | |
1097 | #define __COMPAT_NFDBITS (8 * sizeof(compat_ulong_t)) | |
1098 | ||
1099 | static | |
36819ad0 | 1100 | int compat_poll_select_copy_remaining(struct timespec64 *end_time, void __user *p, |
e99ca56c AV |
1101 | int timeval, int ret) |
1102 | { | |
36819ad0 | 1103 | struct timespec64 ts; |
e99ca56c AV |
1104 | |
1105 | if (!p) | |
1106 | return ret; | |
1107 | ||
1108 | if (current->personality & STICKY_TIMEOUTS) | |
1109 | goto sticky; | |
1110 | ||
1111 | /* No update for zero timeout */ | |
1112 | if (!end_time->tv_sec && !end_time->tv_nsec) | |
1113 | return ret; | |
1114 | ||
36819ad0 DD |
1115 | ktime_get_ts64(&ts); |
1116 | ts = timespec64_sub(*end_time, ts); | |
e99ca56c AV |
1117 | if (ts.tv_sec < 0) |
1118 | ts.tv_sec = ts.tv_nsec = 0; | |
1119 | ||
1120 | if (timeval) { | |
1121 | struct compat_timeval rtv; | |
1122 | ||
1123 | rtv.tv_sec = ts.tv_sec; | |
1124 | rtv.tv_usec = ts.tv_nsec / NSEC_PER_USEC; | |
1125 | ||
1126 | if (!copy_to_user(p, &rtv, sizeof(rtv))) | |
1127 | return ret; | |
1128 | } else { | |
36819ad0 | 1129 | if (!compat_put_timespec64(&ts, p)) |
e99ca56c AV |
1130 | return ret; |
1131 | } | |
1132 | /* | |
1133 | * If an application puts its timeval in read-only memory, we | |
1134 | * don't want the Linux-specific update to the timeval to | |
1135 | * cause a fault after the select has completed | |
1136 | * successfully. However, because we're not updating the | |
1137 | * timeval, we can't restart the system call. | |
1138 | */ | |
1139 | ||
1140 | sticky: | |
1141 | if (ret == -ERESTARTNOHAND) | |
1142 | ret = -EINTR; | |
1143 | return ret; | |
1144 | } | |
1145 | ||
1146 | /* | |
1147 | * Ooo, nasty. We need here to frob 32-bit unsigned longs to | |
1148 | * 64-bit unsigned longs. | |
1149 | */ | |
1150 | static | |
1151 | int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset, | |
1152 | unsigned long *fdset) | |
1153 | { | |
e99ca56c | 1154 | if (ufdset) { |
464d6242 | 1155 | return compat_get_bitmap(fdset, ufdset, nr); |
e99ca56c | 1156 | } else { |
79de3cbe | 1157 | zero_fd_set(nr, fdset); |
464d6242 | 1158 | return 0; |
e99ca56c | 1159 | } |
e99ca56c AV |
1160 | } |
1161 | ||
1162 | static | |
1163 | int compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset, | |
1164 | unsigned long *fdset) | |
1165 | { | |
e99ca56c AV |
1166 | if (!ufdset) |
1167 | return 0; | |
464d6242 | 1168 | return compat_put_bitmap(ufdset, fdset, nr); |
e99ca56c AV |
1169 | } |
1170 | ||
1171 | ||
1172 | /* | |
1173 | * This is a virtual copy of sys_select from fs/select.c and probably | |
1174 | * should be compared to it from time to time | |
1175 | */ | |
1176 | ||
1177 | /* | |
1178 | * We can actually return ERESTARTSYS instead of EINTR, but I'd | |
1179 | * like to be certain this leads to no problems. So I return | |
1180 | * EINTR just for safety. | |
1181 | * | |
1182 | * Update: ERESTARTSYS breaks at least the xview clock binary, so | |
1183 | * I'm trying ERESTARTNOHAND which restart only when you want to. | |
1184 | */ | |
1185 | static int compat_core_sys_select(int n, compat_ulong_t __user *inp, | |
1186 | compat_ulong_t __user *outp, compat_ulong_t __user *exp, | |
36819ad0 | 1187 | struct timespec64 *end_time) |
e99ca56c AV |
1188 | { |
1189 | fd_set_bits fds; | |
1190 | void *bits; | |
1191 | int size, max_fds, ret = -EINVAL; | |
1192 | struct fdtable *fdt; | |
1193 | long stack_fds[SELECT_STACK_ALLOC/sizeof(long)]; | |
1194 | ||
1195 | if (n < 0) | |
1196 | goto out_nofds; | |
1197 | ||
1198 | /* max_fds can increase, so grab it once to avoid race */ | |
1199 | rcu_read_lock(); | |
1200 | fdt = files_fdtable(current->files); | |
1201 | max_fds = fdt->max_fds; | |
1202 | rcu_read_unlock(); | |
1203 | if (n > max_fds) | |
1204 | n = max_fds; | |
1205 | ||
1206 | /* | |
1207 | * We need 6 bitmaps (in/out/ex for both incoming and outgoing), | |
1208 | * since we used fdset we need to allocate memory in units of | |
1209 | * long-words. | |
1210 | */ | |
1211 | size = FDS_BYTES(n); | |
1212 | bits = stack_fds; | |
1213 | if (size > sizeof(stack_fds) / 6) { | |
1214 | bits = kmalloc(6 * size, GFP_KERNEL); | |
1215 | ret = -ENOMEM; | |
1216 | if (!bits) | |
1217 | goto out_nofds; | |
1218 | } | |
1219 | fds.in = (unsigned long *) bits; | |
1220 | fds.out = (unsigned long *) (bits + size); | |
1221 | fds.ex = (unsigned long *) (bits + 2*size); | |
1222 | fds.res_in = (unsigned long *) (bits + 3*size); | |
1223 | fds.res_out = (unsigned long *) (bits + 4*size); | |
1224 | fds.res_ex = (unsigned long *) (bits + 5*size); | |
1225 | ||
1226 | if ((ret = compat_get_fd_set(n, inp, fds.in)) || | |
1227 | (ret = compat_get_fd_set(n, outp, fds.out)) || | |
1228 | (ret = compat_get_fd_set(n, exp, fds.ex))) | |
1229 | goto out; | |
1230 | zero_fd_set(n, fds.res_in); | |
1231 | zero_fd_set(n, fds.res_out); | |
1232 | zero_fd_set(n, fds.res_ex); | |
1233 | ||
1234 | ret = do_select(n, &fds, end_time); | |
1235 | ||
1236 | if (ret < 0) | |
1237 | goto out; | |
1238 | if (!ret) { | |
1239 | ret = -ERESTARTNOHAND; | |
1240 | if (signal_pending(current)) | |
1241 | goto out; | |
1242 | ret = 0; | |
1243 | } | |
1244 | ||
1245 | if (compat_set_fd_set(n, inp, fds.res_in) || | |
1246 | compat_set_fd_set(n, outp, fds.res_out) || | |
1247 | compat_set_fd_set(n, exp, fds.res_ex)) | |
1248 | ret = -EFAULT; | |
1249 | out: | |
1250 | if (bits != stack_fds) | |
1251 | kfree(bits); | |
1252 | out_nofds: | |
1253 | return ret; | |
1254 | } | |
1255 | ||
1256 | COMPAT_SYSCALL_DEFINE5(select, int, n, compat_ulong_t __user *, inp, | |
1257 | compat_ulong_t __user *, outp, compat_ulong_t __user *, exp, | |
1258 | struct compat_timeval __user *, tvp) | |
1259 | { | |
36819ad0 | 1260 | struct timespec64 end_time, *to = NULL; |
e99ca56c AV |
1261 | struct compat_timeval tv; |
1262 | int ret; | |
1263 | ||
1264 | if (tvp) { | |
1265 | if (copy_from_user(&tv, tvp, sizeof(tv))) | |
1266 | return -EFAULT; | |
1267 | ||
1268 | to = &end_time; | |
1269 | if (poll_select_set_timeout(to, | |
1270 | tv.tv_sec + (tv.tv_usec / USEC_PER_SEC), | |
1271 | (tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC)) | |
1272 | return -EINVAL; | |
1273 | } | |
1274 | ||
1275 | ret = compat_core_sys_select(n, inp, outp, exp, to); | |
1276 | ret = compat_poll_select_copy_remaining(&end_time, tvp, 1, ret); | |
1277 | ||
1278 | return ret; | |
1279 | } | |
1280 | ||
1281 | struct compat_sel_arg_struct { | |
1282 | compat_ulong_t n; | |
1283 | compat_uptr_t inp; | |
1284 | compat_uptr_t outp; | |
1285 | compat_uptr_t exp; | |
1286 | compat_uptr_t tvp; | |
1287 | }; | |
1288 | ||
1289 | COMPAT_SYSCALL_DEFINE1(old_select, struct compat_sel_arg_struct __user *, arg) | |
1290 | { | |
1291 | struct compat_sel_arg_struct a; | |
1292 | ||
1293 | if (copy_from_user(&a, arg, sizeof(a))) | |
1294 | return -EFAULT; | |
1295 | return compat_sys_select(a.n, compat_ptr(a.inp), compat_ptr(a.outp), | |
1296 | compat_ptr(a.exp), compat_ptr(a.tvp)); | |
1297 | } | |
1298 | ||
1299 | static long do_compat_pselect(int n, compat_ulong_t __user *inp, | |
1300 | compat_ulong_t __user *outp, compat_ulong_t __user *exp, | |
1301 | struct compat_timespec __user *tsp, compat_sigset_t __user *sigmask, | |
1302 | compat_size_t sigsetsize) | |
1303 | { | |
1304 | compat_sigset_t ss32; | |
1305 | sigset_t ksigmask, sigsaved; | |
36819ad0 | 1306 | struct timespec64 ts, end_time, *to = NULL; |
e99ca56c AV |
1307 | int ret; |
1308 | ||
1309 | if (tsp) { | |
36819ad0 | 1310 | if (compat_get_timespec64(&ts, tsp)) |
e99ca56c AV |
1311 | return -EFAULT; |
1312 | ||
1313 | to = &end_time; | |
1314 | if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec)) | |
1315 | return -EINVAL; | |
1316 | } | |
1317 | ||
1318 | if (sigmask) { | |
1319 | if (sigsetsize != sizeof(compat_sigset_t)) | |
1320 | return -EINVAL; | |
1321 | if (copy_from_user(&ss32, sigmask, sizeof(ss32))) | |
1322 | return -EFAULT; | |
1323 | sigset_from_compat(&ksigmask, &ss32); | |
1324 | ||
1325 | sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP)); | |
1326 | sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved); | |
1327 | } | |
1328 | ||
1329 | ret = compat_core_sys_select(n, inp, outp, exp, to); | |
1330 | ret = compat_poll_select_copy_remaining(&end_time, tsp, 0, ret); | |
1331 | ||
1332 | if (ret == -ERESTARTNOHAND) { | |
1333 | /* | |
1334 | * Don't restore the signal mask yet. Let do_signal() deliver | |
1335 | * the signal on the way back to userspace, before the signal | |
1336 | * mask is restored. | |
1337 | */ | |
1338 | if (sigmask) { | |
1339 | memcpy(¤t->saved_sigmask, &sigsaved, | |
1340 | sizeof(sigsaved)); | |
1341 | set_restore_sigmask(); | |
1342 | } | |
1343 | } else if (sigmask) | |
1344 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); | |
1345 | ||
1346 | return ret; | |
1347 | } | |
1348 | ||
1349 | COMPAT_SYSCALL_DEFINE6(pselect6, int, n, compat_ulong_t __user *, inp, | |
1350 | compat_ulong_t __user *, outp, compat_ulong_t __user *, exp, | |
1351 | struct compat_timespec __user *, tsp, void __user *, sig) | |
1352 | { | |
1353 | compat_size_t sigsetsize = 0; | |
1354 | compat_uptr_t up = 0; | |
1355 | ||
1356 | if (sig) { | |
1357 | if (!access_ok(VERIFY_READ, sig, | |
1358 | sizeof(compat_uptr_t)+sizeof(compat_size_t)) || | |
1359 | __get_user(up, (compat_uptr_t __user *)sig) || | |
1360 | __get_user(sigsetsize, | |
1361 | (compat_size_t __user *)(sig+sizeof(up)))) | |
1362 | return -EFAULT; | |
1363 | } | |
1364 | return do_compat_pselect(n, inp, outp, exp, tsp, compat_ptr(up), | |
1365 | sigsetsize); | |
1366 | } | |
1367 | ||
1368 | COMPAT_SYSCALL_DEFINE5(ppoll, struct pollfd __user *, ufds, | |
1369 | unsigned int, nfds, struct compat_timespec __user *, tsp, | |
1370 | const compat_sigset_t __user *, sigmask, compat_size_t, sigsetsize) | |
1371 | { | |
1372 | compat_sigset_t ss32; | |
1373 | sigset_t ksigmask, sigsaved; | |
36819ad0 | 1374 | struct timespec64 ts, end_time, *to = NULL; |
e99ca56c AV |
1375 | int ret; |
1376 | ||
1377 | if (tsp) { | |
36819ad0 | 1378 | if (compat_get_timespec64(&ts, tsp)) |
e99ca56c AV |
1379 | return -EFAULT; |
1380 | ||
1381 | to = &end_time; | |
1382 | if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec)) | |
1383 | return -EINVAL; | |
1384 | } | |
1385 | ||
1386 | if (sigmask) { | |
1387 | if (sigsetsize != sizeof(compat_sigset_t)) | |
1388 | return -EINVAL; | |
1389 | if (copy_from_user(&ss32, sigmask, sizeof(ss32))) | |
1390 | return -EFAULT; | |
1391 | sigset_from_compat(&ksigmask, &ss32); | |
1392 | ||
1393 | sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP)); | |
1394 | sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved); | |
1395 | } | |
1396 | ||
1397 | ret = do_sys_poll(ufds, nfds, to); | |
1398 | ||
1399 | /* We can restart this syscall, usually */ | |
1400 | if (ret == -EINTR) { | |
1401 | /* | |
1402 | * Don't restore the signal mask yet. Let do_signal() deliver | |
1403 | * the signal on the way back to userspace, before the signal | |
1404 | * mask is restored. | |
1405 | */ | |
1406 | if (sigmask) { | |
1407 | memcpy(¤t->saved_sigmask, &sigsaved, | |
1408 | sizeof(sigsaved)); | |
1409 | set_restore_sigmask(); | |
1410 | } | |
1411 | ret = -ERESTARTNOHAND; | |
1412 | } else if (sigmask) | |
1413 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); | |
1414 | ||
1415 | ret = compat_poll_select_copy_remaining(&end_time, tsp, 0, ret); | |
1416 | ||
1417 | return ret; | |
1418 | } | |
1419 | #endif |