[PATCH] select: time comparison fixes
[linux-3.10.git] / fs / select.c
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
17 #include <linux/syscalls.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/smp_lock.h>
21 #include <linux/poll.h>
22 #include <linux/personality.h> /* for STICKY_TIMEOUTS */
23 #include <linux/file.h>
24 #include <linux/fs.h>
25 #include <linux/rcupdate.h>
26
27 #include <asm/uaccess.h>
28
29 #define ROUND_UP(x,y) (((x)+(y)-1)/(y))
30 #define DEFAULT_POLLMASK (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)
31
32 struct poll_table_entry {
33         struct file * filp;
34         wait_queue_t wait;
35         wait_queue_head_t * wait_address;
36 };
37
38 struct poll_table_page {
39         struct poll_table_page * next;
40         struct poll_table_entry * entry;
41         struct poll_table_entry entries[0];
42 };
43
44 #define POLL_TABLE_FULL(table) \
45         ((unsigned long)((table)->entry+1) > PAGE_SIZE + (unsigned long)(table))
46
47 /*
48  * Ok, Peter made a complicated, but straightforward multiple_wait() function.
49  * I have rewritten this, taking some shortcuts: This code may not be easy to
50  * follow, but it should be free of race-conditions, and it's practical. If you
51  * understand what I'm doing here, then you understand how the linux
52  * sleep/wakeup mechanism works.
53  *
54  * Two very simple procedures, poll_wait() and poll_freewait() make all the
55  * work.  poll_wait() is an inline-function defined in <linux/poll.h>,
56  * as all select/poll functions have to call it to add an entry to the
57  * poll table.
58  */
59 static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
60                        poll_table *p);
61
62 void poll_initwait(struct poll_wqueues *pwq)
63 {
64         init_poll_funcptr(&pwq->pt, __pollwait);
65         pwq->error = 0;
66         pwq->table = NULL;
67 }
68
69 EXPORT_SYMBOL(poll_initwait);
70
71 void poll_freewait(struct poll_wqueues *pwq)
72 {
73         struct poll_table_page * p = pwq->table;
74         while (p) {
75                 struct poll_table_entry * entry;
76                 struct poll_table_page *old;
77
78                 entry = p->entry;
79                 do {
80                         entry--;
81                         remove_wait_queue(entry->wait_address,&entry->wait);
82                         fput(entry->filp);
83                 } while (entry > p->entries);
84                 old = p;
85                 p = p->next;
86                 free_page((unsigned long) old);
87         }
88 }
89
90 EXPORT_SYMBOL(poll_freewait);
91
92 static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
93                        poll_table *_p)
94 {
95         struct poll_wqueues *p = container_of(_p, struct poll_wqueues, pt);
96         struct poll_table_page *table = p->table;
97
98         if (!table || POLL_TABLE_FULL(table)) {
99                 struct poll_table_page *new_table;
100
101                 new_table = (struct poll_table_page *) __get_free_page(GFP_KERNEL);
102                 if (!new_table) {
103                         p->error = -ENOMEM;
104                         __set_current_state(TASK_RUNNING);
105                         return;
106                 }
107                 new_table->entry = new_table->entries;
108                 new_table->next = table;
109                 p->table = new_table;
110                 table = new_table;
111         }
112
113         /* Add a new entry */
114         {
115                 struct poll_table_entry * entry = table->entry;
116                 table->entry = entry+1;
117                 get_file(filp);
118                 entry->filp = filp;
119                 entry->wait_address = wait_address;
120                 init_waitqueue_entry(&entry->wait, current);
121                 add_wait_queue(wait_address,&entry->wait);
122         }
123 }
124
125 #define FDS_IN(fds, n)          (fds->in + n)
126 #define FDS_OUT(fds, n)         (fds->out + n)
127 #define FDS_EX(fds, n)          (fds->ex + n)
128
129 #define BITS(fds, n)    (*FDS_IN(fds, n)|*FDS_OUT(fds, n)|*FDS_EX(fds, n))
130
131 static int max_select_fd(unsigned long n, fd_set_bits *fds)
132 {
133         unsigned long *open_fds;
134         unsigned long set;
135         int max;
136         struct fdtable *fdt;
137
138         /* handle last in-complete long-word first */
139         set = ~(~0UL << (n & (__NFDBITS-1)));
140         n /= __NFDBITS;
141         fdt = files_fdtable(current->files);
142         open_fds = fdt->open_fds->fds_bits+n;
143         max = 0;
144         if (set) {
145                 set &= BITS(fds, n);
146                 if (set) {
147                         if (!(set & ~*open_fds))
148                                 goto get_max;
149                         return -EBADF;
150                 }
151         }
152         while (n) {
153                 open_fds--;
154                 n--;
155                 set = BITS(fds, n);
156                 if (!set)
157                         continue;
158                 if (set & ~*open_fds)
159                         return -EBADF;
160                 if (max)
161                         continue;
162 get_max:
163                 do {
164                         max++;
165                         set >>= 1;
166                 } while (set);
167                 max += n * __NFDBITS;
168         }
169
170         return max;
171 }
172
173 #define BIT(i)          (1UL << ((i)&(__NFDBITS-1)))
174 #define MEM(i,m)        ((m)+(unsigned)(i)/__NFDBITS)
175 #define ISSET(i,m)      (((i)&*(m)) != 0)
176 #define SET(i,m)        (*(m) |= (i))
177
178 #define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
179 #define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
180 #define POLLEX_SET (POLLPRI)
181
182 int do_select(int n, fd_set_bits *fds, s64 *timeout)
183 {
184         struct poll_wqueues table;
185         poll_table *wait;
186         int retval, i;
187
188         rcu_read_lock();
189         retval = max_select_fd(n, fds);
190         rcu_read_unlock();
191
192         if (retval < 0)
193                 return retval;
194         n = retval;
195
196         poll_initwait(&table);
197         wait = &table.pt;
198         if (!*timeout)
199                 wait = NULL;
200         retval = 0;
201         for (;;) {
202                 unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
203                 long __timeout;
204
205                 set_current_state(TASK_INTERRUPTIBLE);
206
207                 inp = fds->in; outp = fds->out; exp = fds->ex;
208                 rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
209
210                 for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
211                         unsigned long in, out, ex, all_bits, bit = 1, mask, j;
212                         unsigned long res_in = 0, res_out = 0, res_ex = 0;
213                         struct file_operations *f_op = NULL;
214                         struct file *file = NULL;
215
216                         in = *inp++; out = *outp++; ex = *exp++;
217                         all_bits = in | out | ex;
218                         if (all_bits == 0) {
219                                 i += __NFDBITS;
220                                 continue;
221                         }
222
223                         for (j = 0; j < __NFDBITS; ++j, ++i, bit <<= 1) {
224                                 if (i >= n)
225                                         break;
226                                 if (!(bit & all_bits))
227                                         continue;
228                                 file = fget(i);
229                                 if (file) {
230                                         f_op = file->f_op;
231                                         mask = DEFAULT_POLLMASK;
232                                         if (f_op && f_op->poll)
233                                                 mask = (*f_op->poll)(file, retval ? NULL : wait);
234                                         fput(file);
235                                         if ((mask & POLLIN_SET) && (in & bit)) {
236                                                 res_in |= bit;
237                                                 retval++;
238                                         }
239                                         if ((mask & POLLOUT_SET) && (out & bit)) {
240                                                 res_out |= bit;
241                                                 retval++;
242                                         }
243                                         if ((mask & POLLEX_SET) && (ex & bit)) {
244                                                 res_ex |= bit;
245                                                 retval++;
246                                         }
247                                 }
248                                 cond_resched();
249                         }
250                         if (res_in)
251                                 *rinp = res_in;
252                         if (res_out)
253                                 *routp = res_out;
254                         if (res_ex)
255                                 *rexp = res_ex;
256                 }
257                 wait = NULL;
258                 if (retval || !*timeout || signal_pending(current))
259                         break;
260                 if(table.error) {
261                         retval = table.error;
262                         break;
263                 }
264
265                 if (*timeout < 0) {
266                         /* Wait indefinitely */
267                         __timeout = MAX_SCHEDULE_TIMEOUT;
268                 } else if (unlikely(*timeout >= (s64)MAX_SCHEDULE_TIMEOUT - 1)) {
269                         /* Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in a loop */
270                         __timeout = MAX_SCHEDULE_TIMEOUT - 1;
271                         *timeout -= __timeout;
272                 } else {
273                         __timeout = *timeout;
274                         *timeout = 0;
275                 }
276                 __timeout = schedule_timeout(__timeout);
277                 if (*timeout >= 0)
278                         *timeout += __timeout;
279         }
280         __set_current_state(TASK_RUNNING);
281
282         poll_freewait(&table);
283
284         return retval;
285 }
286
287 static void *select_bits_alloc(int size)
288 {
289         return kmalloc(6 * size, GFP_KERNEL);
290 }
291
292 static void select_bits_free(void *bits, int size)
293 {
294         kfree(bits);
295 }
296
297 /*
298  * We can actually return ERESTARTSYS instead of EINTR, but I'd
299  * like to be certain this leads to no problems. So I return
300  * EINTR just for safety.
301  *
302  * Update: ERESTARTSYS breaks at least the xview clock binary, so
303  * I'm trying ERESTARTNOHAND which restart only when you want to.
304  */
305 #define MAX_SELECT_SECONDS \
306         ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
307
308 static int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
309                            fd_set __user *exp, s64 *timeout)
310 {
311         fd_set_bits fds;
312         char *bits;
313         int ret, size, max_fdset;
314         struct fdtable *fdt;
315
316         ret = -EINVAL;
317         if (n < 0)
318                 goto out_nofds;
319
320         /* max_fdset can increase, so grab it once to avoid race */
321         rcu_read_lock();
322         fdt = files_fdtable(current->files);
323         max_fdset = fdt->max_fdset;
324         rcu_read_unlock();
325         if (n > max_fdset)
326                 n = max_fdset;
327
328         /*
329          * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
330          * since we used fdset we need to allocate memory in units of
331          * long-words. 
332          */
333         ret = -ENOMEM;
334         size = FDS_BYTES(n);
335         bits = select_bits_alloc(size);
336         if (!bits)
337                 goto out_nofds;
338         fds.in      = (unsigned long *)  bits;
339         fds.out     = (unsigned long *) (bits +   size);
340         fds.ex      = (unsigned long *) (bits + 2*size);
341         fds.res_in  = (unsigned long *) (bits + 3*size);
342         fds.res_out = (unsigned long *) (bits + 4*size);
343         fds.res_ex  = (unsigned long *) (bits + 5*size);
344
345         if ((ret = get_fd_set(n, inp, fds.in)) ||
346             (ret = get_fd_set(n, outp, fds.out)) ||
347             (ret = get_fd_set(n, exp, fds.ex)))
348                 goto out;
349         zero_fd_set(n, fds.res_in);
350         zero_fd_set(n, fds.res_out);
351         zero_fd_set(n, fds.res_ex);
352
353         ret = do_select(n, &fds, timeout);
354
355         if (ret < 0)
356                 goto out;
357         if (!ret) {
358                 ret = -ERESTARTNOHAND;
359                 if (signal_pending(current))
360                         goto out;
361                 ret = 0;
362         }
363
364         if (set_fd_set(n, inp, fds.res_in) ||
365             set_fd_set(n, outp, fds.res_out) ||
366             set_fd_set(n, exp, fds.res_ex))
367                 ret = -EFAULT;
368
369 out:
370         select_bits_free(bits, size);
371 out_nofds:
372         return ret;
373 }
374
375 asmlinkage long sys_select(int n, fd_set __user *inp, fd_set __user *outp,
376                         fd_set __user *exp, struct timeval __user *tvp)
377 {
378         s64 timeout = -1;
379         struct timeval tv;
380         int ret;
381
382         if (tvp) {
383                 if (copy_from_user(&tv, tvp, sizeof(tv)))
384                         return -EFAULT;
385
386                 if (tv.tv_sec < 0 || tv.tv_usec < 0)
387                         return -EINVAL;
388
389                 /* Cast to u64 to make GCC stop complaining */
390                 if ((u64)tv.tv_sec >= (u64)MAX_INT64_SECONDS)
391                         timeout = -1;   /* infinite */
392                 else {
393                         timeout = ROUND_UP(tv.tv_usec, USEC_PER_SEC/HZ);
394                         timeout += tv.tv_sec * HZ;
395                 }
396         }
397
398         ret = core_sys_select(n, inp, outp, exp, &timeout);
399
400         if (tvp) {
401                 struct timeval rtv;
402
403                 if (current->personality & STICKY_TIMEOUTS)
404                         goto sticky;
405                 rtv.tv_usec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ));
406                 rtv.tv_sec = timeout;
407                 if (timeval_compare(&rtv, &tv) >= 0)
408                         rtv = tv;
409                 if (copy_to_user(tvp, &rtv, sizeof(rtv))) {
410 sticky:
411                         /*
412                          * If an application puts its timeval in read-only
413                          * memory, we don't want the Linux-specific update to
414                          * the timeval to cause a fault after the select has
415                          * completed successfully. However, because we're not
416                          * updating the timeval, we can't restart the system
417                          * call.
418                          */
419                         if (ret == -ERESTARTNOHAND)
420                                 ret = -EINTR;
421                 }
422         }
423
424         return ret;
425 }
426
427 #ifdef TIF_RESTORE_SIGMASK
428 asmlinkage long sys_pselect7(int n, fd_set __user *inp, fd_set __user *outp,
429                 fd_set __user *exp, struct timespec __user *tsp,
430                 const sigset_t __user *sigmask, size_t sigsetsize)
431 {
432         s64 timeout = MAX_SCHEDULE_TIMEOUT;
433         sigset_t ksigmask, sigsaved;
434         struct timespec ts;
435         int ret;
436
437         if (tsp) {
438                 if (copy_from_user(&ts, tsp, sizeof(ts)))
439                         return -EFAULT;
440
441                 if (ts.tv_sec < 0 || ts.tv_nsec < 0)
442                         return -EINVAL;
443
444                 /* Cast to u64 to make GCC stop complaining */
445                 if ((u64)ts.tv_sec >= (u64)MAX_INT64_SECONDS)
446                         timeout = -1;   /* infinite */
447                 else {
448                         timeout = ROUND_UP(ts.tv_nsec, NSEC_PER_SEC/HZ);
449                         timeout += ts.tv_sec * HZ;
450                 }
451         }
452
453         if (sigmask) {
454                 /* XXX: Don't preclude handling different sized sigset_t's.  */
455                 if (sigsetsize != sizeof(sigset_t))
456                         return -EINVAL;
457                 if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
458                         return -EFAULT;
459
460                 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
461                 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
462         }
463
464         ret = core_sys_select(n, inp, outp, exp, &timeout);
465
466         if (tsp) {
467                 struct timespec rts;
468
469                 if (current->personality & STICKY_TIMEOUTS)
470                         goto sticky;
471                 rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
472                                                 1000;
473                 rts.tv_sec = timeout;
474                 if (timespec_compare(&rts, &ts) >= 0)
475                         rts = ts;
476                 if (copy_to_user(tsp, &rts, sizeof(rts))) {
477 sticky:
478                         /*
479                          * If an application puts its timeval in read-only
480                          * memory, we don't want the Linux-specific update to
481                          * the timeval to cause a fault after the select has
482                          * completed successfully. However, because we're not
483                          * updating the timeval, we can't restart the system
484                          * call.
485                          */
486                         if (ret == -ERESTARTNOHAND)
487                                 ret = -EINTR;
488                 }
489         }
490
491         if (ret == -ERESTARTNOHAND) {
492                 /*
493                  * Don't restore the signal mask yet. Let do_signal() deliver
494                  * the signal on the way back to userspace, before the signal
495                  * mask is restored.
496                  */
497                 if (sigmask) {
498                         memcpy(&current->saved_sigmask, &sigsaved,
499                                         sizeof(sigsaved));
500                         set_thread_flag(TIF_RESTORE_SIGMASK);
501                 }
502         } else if (sigmask)
503                 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
504
505         return ret;
506 }
507
508 /*
509  * Most architectures can't handle 7-argument syscalls. So we provide a
510  * 6-argument version where the sixth argument is a pointer to a structure
511  * which has a pointer to the sigset_t itself followed by a size_t containing
512  * the sigset size.
513  */
514 asmlinkage long sys_pselect6(int n, fd_set __user *inp, fd_set __user *outp,
515         fd_set __user *exp, struct timespec __user *tsp, void __user *sig)
516 {
517         size_t sigsetsize = 0;
518         sigset_t __user *up = NULL;
519
520         if (sig) {
521                 if (!access_ok(VERIFY_READ, sig, sizeof(void *)+sizeof(size_t))
522                     || __get_user(up, (sigset_t __user * __user *)sig)
523                     || __get_user(sigsetsize,
524                                 (size_t __user *)(sig+sizeof(void *))))
525                         return -EFAULT;
526         }
527
528         return sys_pselect7(n, inp, outp, exp, tsp, up, sigsetsize);
529 }
530 #endif /* TIF_RESTORE_SIGMASK */
531
532 struct poll_list {
533         struct poll_list *next;
534         int len;
535         struct pollfd entries[0];
536 };
537
538 #define POLLFD_PER_PAGE  ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
539
540 static void do_pollfd(unsigned int num, struct pollfd * fdpage,
541         poll_table ** pwait, int *count)
542 {
543         int i;
544
545         for (i = 0; i < num; i++) {
546                 int fd;
547                 unsigned int mask;
548                 struct pollfd *fdp;
549
550                 mask = 0;
551                 fdp = fdpage+i;
552                 fd = fdp->fd;
553                 if (fd >= 0) {
554                         struct file * file = fget(fd);
555                         mask = POLLNVAL;
556                         if (file != NULL) {
557                                 mask = DEFAULT_POLLMASK;
558                                 if (file->f_op && file->f_op->poll)
559                                         mask = file->f_op->poll(file, *pwait);
560                                 mask &= fdp->events | POLLERR | POLLHUP;
561                                 fput(file);
562                         }
563                         if (mask) {
564                                 *pwait = NULL;
565                                 (*count)++;
566                         }
567                 }
568                 fdp->revents = mask;
569         }
570 }
571
572 static int do_poll(unsigned int nfds,  struct poll_list *list,
573                    struct poll_wqueues *wait, s64 *timeout)
574 {
575         int count = 0;
576         poll_table* pt = &wait->pt;
577
578         /* Optimise the no-wait case */
579         if (!(*timeout))
580                 pt = NULL;
581  
582         for (;;) {
583                 struct poll_list *walk;
584                 long __timeout;
585
586                 set_current_state(TASK_INTERRUPTIBLE);
587                 walk = list;
588                 while(walk != NULL) {
589                         do_pollfd( walk->len, walk->entries, &pt, &count);
590                         walk = walk->next;
591                 }
592                 pt = NULL;
593                 if (count || !*timeout || signal_pending(current))
594                         break;
595                 count = wait->error;
596                 if (count)
597                         break;
598
599                 if (*timeout < 0) {
600                         /* Wait indefinitely */
601                         __timeout = MAX_SCHEDULE_TIMEOUT;
602                 } else if (unlikely(*timeout >= (s64)MAX_SCHEDULE_TIMEOUT-1)) {
603                         /*
604                          * Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in
605                          * a loop
606                          */
607                         __timeout = MAX_SCHEDULE_TIMEOUT - 1;
608                         *timeout -= __timeout;
609                 } else {
610                         __timeout = *timeout;
611                         *timeout = 0;
612                 }
613
614                 __timeout = schedule_timeout(__timeout);
615                 if (*timeout >= 0)
616                         *timeout += __timeout;
617         }
618         __set_current_state(TASK_RUNNING);
619         return count;
620 }
621
622 int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds, s64 *timeout)
623 {
624         struct poll_wqueues table;
625         int fdcount, err;
626         unsigned int i;
627         struct poll_list *head;
628         struct poll_list *walk;
629         struct fdtable *fdt;
630         int max_fdset;
631
632         /* Do a sanity check on nfds ... */
633         rcu_read_lock();
634         fdt = files_fdtable(current->files);
635         max_fdset = fdt->max_fdset;
636         rcu_read_unlock();
637         if (nfds > max_fdset && nfds > OPEN_MAX)
638                 return -EINVAL;
639
640         poll_initwait(&table);
641
642         head = NULL;
643         walk = NULL;
644         i = nfds;
645         err = -ENOMEM;
646         while(i!=0) {
647                 struct poll_list *pp;
648                 pp = kmalloc(sizeof(struct poll_list)+
649                                 sizeof(struct pollfd)*
650                                 (i>POLLFD_PER_PAGE?POLLFD_PER_PAGE:i),
651                                         GFP_KERNEL);
652                 if(pp==NULL)
653                         goto out_fds;
654                 pp->next=NULL;
655                 pp->len = (i>POLLFD_PER_PAGE?POLLFD_PER_PAGE:i);
656                 if (head == NULL)
657                         head = pp;
658                 else
659                         walk->next = pp;
660
661                 walk = pp;
662                 if (copy_from_user(pp->entries, ufds + nfds-i, 
663                                 sizeof(struct pollfd)*pp->len)) {
664                         err = -EFAULT;
665                         goto out_fds;
666                 }
667                 i -= pp->len;
668         }
669
670         fdcount = do_poll(nfds, head, &table, timeout);
671
672         /* OK, now copy the revents fields back to user space. */
673         walk = head;
674         err = -EFAULT;
675         while(walk != NULL) {
676                 struct pollfd *fds = walk->entries;
677                 int j;
678
679                 for (j=0; j < walk->len; j++, ufds++) {
680                         if(__put_user(fds[j].revents, &ufds->revents))
681                                 goto out_fds;
682                 }
683                 walk = walk->next;
684         }
685         err = fdcount;
686         if (!fdcount && signal_pending(current))
687                 err = -EINTR;
688 out_fds:
689         walk = head;
690         while(walk!=NULL) {
691                 struct poll_list *pp = walk->next;
692                 kfree(walk);
693                 walk = pp;
694         }
695         poll_freewait(&table);
696         return err;
697 }
698
699 asmlinkage long sys_poll(struct pollfd __user *ufds, unsigned int nfds,
700                         long timeout_msecs)
701 {
702         s64 timeout_jiffies = 0;
703
704         if (timeout_msecs) {
705 #if HZ > 1000
706                 /* We can only overflow if HZ > 1000 */
707                 if (timeout_msecs / 1000 > (s64)0x7fffffffffffffffULL / (s64)HZ)
708                         timeout_jiffies = -1;
709                 else
710 #endif
711                         timeout_jiffies = msecs_to_jiffies(timeout_msecs);
712         }
713
714         return do_sys_poll(ufds, nfds, &timeout_jiffies);
715 }
716
717 #ifdef TIF_RESTORE_SIGMASK
718 asmlinkage long sys_ppoll(struct pollfd __user *ufds, unsigned int nfds,
719         struct timespec __user *tsp, const sigset_t __user *sigmask,
720         size_t sigsetsize)
721 {
722         sigset_t ksigmask, sigsaved;
723         struct timespec ts;
724         s64 timeout = -1;
725         int ret;
726
727         if (tsp) {
728                 if (copy_from_user(&ts, tsp, sizeof(ts)))
729                         return -EFAULT;
730
731                 /* Cast to u64 to make GCC stop complaining */
732                 if ((u64)ts.tv_sec >= (u64)MAX_INT64_SECONDS)
733                         timeout = -1;   /* infinite */
734                 else {
735                         timeout = ROUND_UP(ts.tv_nsec, NSEC_PER_SEC/HZ);
736                         timeout += ts.tv_sec * HZ;
737                 }
738         }
739
740         if (sigmask) {
741                 /* XXX: Don't preclude handling different sized sigset_t's.  */
742                 if (sigsetsize != sizeof(sigset_t))
743                         return -EINVAL;
744                 if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
745                         return -EFAULT;
746
747                 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
748                 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
749         }
750
751         ret = do_sys_poll(ufds, nfds, &timeout);
752
753         /* We can restart this syscall, usually */
754         if (ret == -EINTR) {
755                 /*
756                  * Don't restore the signal mask yet. Let do_signal() deliver
757                  * the signal on the way back to userspace, before the signal
758                  * mask is restored.
759                  */
760                 if (sigmask) {
761                         memcpy(&current->saved_sigmask, &sigsaved,
762                                         sizeof(sigsaved));
763                         set_thread_flag(TIF_RESTORE_SIGMASK);
764                 }
765                 ret = -ERESTARTNOHAND;
766         } else if (sigmask)
767                 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
768
769         if (tsp && timeout >= 0) {
770                 struct timespec rts;
771
772                 if (current->personality & STICKY_TIMEOUTS)
773                         goto sticky;
774                 /* Yes, we know it's actually an s64, but it's also positive. */
775                 rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
776                                                 1000;
777                 rts.tv_sec = timeout;
778                 if (timespec_compare(&rts, &ts) >= 0)
779                         rts = ts;
780                 if (copy_to_user(tsp, &rts, sizeof(rts))) {
781                 sticky:
782                         /*
783                          * If an application puts its timeval in read-only
784                          * memory, we don't want the Linux-specific update to
785                          * the timeval to cause a fault after the select has
786                          * completed successfully. However, because we're not
787                          * updating the timeval, we can't restart the system
788                          * call.
789                          */
790                         if (ret == -ERESTARTNOHAND && timeout >= 0)
791                                 ret = -EINTR;
792                 }
793         }
794
795         return ret;
796 }
797 #endif /* TIF_RESTORE_SIGMASK */