[PATCH] fix __user annotations in fs/select.c
[safe/jmp/linux-2.6] / 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                 if (current->personality & STICKY_TIMEOUTS)
402                         goto sticky;
403                 tv.tv_usec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ));
404                 tv.tv_sec = timeout;
405                 if (copy_to_user(tvp, &tv, sizeof(tv))) {
406 sticky:
407                         /*
408                          * If an application puts its timeval in read-only
409                          * memory, we don't want the Linux-specific update to
410                          * the timeval to cause a fault after the select has
411                          * completed successfully. However, because we're not
412                          * updating the timeval, we can't restart the system
413                          * call.
414                          */
415                         if (ret == -ERESTARTNOHAND)
416                                 ret = -EINTR;
417                 }
418         }
419
420         return ret;
421 }
422
423 #ifdef TIF_RESTORE_SIGMASK
424 asmlinkage long sys_pselect7(int n, fd_set __user *inp, fd_set __user *outp,
425                 fd_set __user *exp, struct timespec __user *tsp,
426                 const sigset_t __user *sigmask, size_t sigsetsize)
427 {
428         s64 timeout = MAX_SCHEDULE_TIMEOUT;
429         sigset_t ksigmask, sigsaved;
430         struct timespec ts;
431         int ret;
432
433         if (tsp) {
434                 if (copy_from_user(&ts, tsp, sizeof(ts)))
435                         return -EFAULT;
436
437                 if (ts.tv_sec < 0 || ts.tv_nsec < 0)
438                         return -EINVAL;
439
440                 /* Cast to u64 to make GCC stop complaining */
441                 if ((u64)ts.tv_sec >= (u64)MAX_INT64_SECONDS)
442                         timeout = -1;   /* infinite */
443                 else {
444                         timeout = ROUND_UP(ts.tv_nsec, NSEC_PER_SEC/HZ);
445                         timeout += ts.tv_sec * HZ;
446                 }
447         }
448
449         if (sigmask) {
450                 /* XXX: Don't preclude handling different sized sigset_t's.  */
451                 if (sigsetsize != sizeof(sigset_t))
452                         return -EINVAL;
453                 if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
454                         return -EFAULT;
455
456                 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
457                 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
458         }
459
460         ret = core_sys_select(n, inp, outp, exp, &timeout);
461
462         if (tsp) {
463                 if (current->personality & STICKY_TIMEOUTS)
464                         goto sticky;
465                 ts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) * 1000;
466                 ts.tv_sec = timeout;
467                 if (copy_to_user(tsp, &ts, sizeof(ts))) {
468 sticky:
469                         /*
470                          * If an application puts its timeval in read-only
471                          * memory, we don't want the Linux-specific update to
472                          * the timeval to cause a fault after the select has
473                          * completed successfully. However, because we're not
474                          * updating the timeval, we can't restart the system
475                          * call.
476                          */
477                         if (ret == -ERESTARTNOHAND)
478                                 ret = -EINTR;
479                 }
480         }
481
482         if (ret == -ERESTARTNOHAND) {
483                 /*
484                  * Don't restore the signal mask yet. Let do_signal() deliver
485                  * the signal on the way back to userspace, before the signal
486                  * mask is restored.
487                  */
488                 if (sigmask) {
489                         memcpy(&current->saved_sigmask, &sigsaved,
490                                         sizeof(sigsaved));
491                         set_thread_flag(TIF_RESTORE_SIGMASK);
492                 }
493         } else if (sigmask)
494                 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
495
496         return ret;
497 }
498
499 /*
500  * Most architectures can't handle 7-argument syscalls. So we provide a
501  * 6-argument version where the sixth argument is a pointer to a structure
502  * which has a pointer to the sigset_t itself followed by a size_t containing
503  * the sigset size.
504  */
505 asmlinkage long sys_pselect6(int n, fd_set __user *inp, fd_set __user *outp,
506         fd_set __user *exp, struct timespec __user *tsp, void __user *sig)
507 {
508         size_t sigsetsize = 0;
509         sigset_t __user *up = NULL;
510
511         if (sig) {
512                 if (!access_ok(VERIFY_READ, sig, sizeof(void *)+sizeof(size_t))
513                     || __get_user(up, (sigset_t __user * __user *)sig)
514                     || __get_user(sigsetsize,
515                                 (size_t __user *)(sig+sizeof(void *))))
516                         return -EFAULT;
517         }
518
519         return sys_pselect7(n, inp, outp, exp, tsp, up, sigsetsize);
520 }
521 #endif /* TIF_RESTORE_SIGMASK */
522
523 struct poll_list {
524         struct poll_list *next;
525         int len;
526         struct pollfd entries[0];
527 };
528
529 #define POLLFD_PER_PAGE  ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
530
531 static void do_pollfd(unsigned int num, struct pollfd * fdpage,
532         poll_table ** pwait, int *count)
533 {
534         int i;
535
536         for (i = 0; i < num; i++) {
537                 int fd;
538                 unsigned int mask;
539                 struct pollfd *fdp;
540
541                 mask = 0;
542                 fdp = fdpage+i;
543                 fd = fdp->fd;
544                 if (fd >= 0) {
545                         struct file * file = fget(fd);
546                         mask = POLLNVAL;
547                         if (file != NULL) {
548                                 mask = DEFAULT_POLLMASK;
549                                 if (file->f_op && file->f_op->poll)
550                                         mask = file->f_op->poll(file, *pwait);
551                                 mask &= fdp->events | POLLERR | POLLHUP;
552                                 fput(file);
553                         }
554                         if (mask) {
555                                 *pwait = NULL;
556                                 (*count)++;
557                         }
558                 }
559                 fdp->revents = mask;
560         }
561 }
562
563 static int do_poll(unsigned int nfds,  struct poll_list *list,
564                    struct poll_wqueues *wait, s64 *timeout)
565 {
566         int count = 0;
567         poll_table* pt = &wait->pt;
568
569         /* Optimise the no-wait case */
570         if (!(*timeout))
571                 pt = NULL;
572  
573         for (;;) {
574                 struct poll_list *walk;
575                 long __timeout;
576
577                 set_current_state(TASK_INTERRUPTIBLE);
578                 walk = list;
579                 while(walk != NULL) {
580                         do_pollfd( walk->len, walk->entries, &pt, &count);
581                         walk = walk->next;
582                 }
583                 pt = NULL;
584                 if (count || !*timeout || signal_pending(current))
585                         break;
586                 count = wait->error;
587                 if (count)
588                         break;
589
590                 if (*timeout < 0) {
591                         /* Wait indefinitely */
592                         __timeout = MAX_SCHEDULE_TIMEOUT;
593                 } else if (unlikely(*timeout >= (s64)MAX_SCHEDULE_TIMEOUT-1)) {
594                         /*
595                          * Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in
596                          * a loop
597                          */
598                         __timeout = MAX_SCHEDULE_TIMEOUT - 1;
599                         *timeout -= __timeout;
600                 } else {
601                         __timeout = *timeout;
602                         *timeout = 0;
603                 }
604
605                 __timeout = schedule_timeout(__timeout);
606                 if (*timeout >= 0)
607                         *timeout += __timeout;
608         }
609         __set_current_state(TASK_RUNNING);
610         return count;
611 }
612
613 int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds, s64 *timeout)
614 {
615         struct poll_wqueues table;
616         int fdcount, err;
617         unsigned int i;
618         struct poll_list *head;
619         struct poll_list *walk;
620         struct fdtable *fdt;
621         int max_fdset;
622
623         /* Do a sanity check on nfds ... */
624         rcu_read_lock();
625         fdt = files_fdtable(current->files);
626         max_fdset = fdt->max_fdset;
627         rcu_read_unlock();
628         if (nfds > max_fdset && nfds > OPEN_MAX)
629                 return -EINVAL;
630
631         poll_initwait(&table);
632
633         head = NULL;
634         walk = NULL;
635         i = nfds;
636         err = -ENOMEM;
637         while(i!=0) {
638                 struct poll_list *pp;
639                 pp = kmalloc(sizeof(struct poll_list)+
640                                 sizeof(struct pollfd)*
641                                 (i>POLLFD_PER_PAGE?POLLFD_PER_PAGE:i),
642                                         GFP_KERNEL);
643                 if(pp==NULL)
644                         goto out_fds;
645                 pp->next=NULL;
646                 pp->len = (i>POLLFD_PER_PAGE?POLLFD_PER_PAGE:i);
647                 if (head == NULL)
648                         head = pp;
649                 else
650                         walk->next = pp;
651
652                 walk = pp;
653                 if (copy_from_user(pp->entries, ufds + nfds-i, 
654                                 sizeof(struct pollfd)*pp->len)) {
655                         err = -EFAULT;
656                         goto out_fds;
657                 }
658                 i -= pp->len;
659         }
660
661         fdcount = do_poll(nfds, head, &table, timeout);
662
663         /* OK, now copy the revents fields back to user space. */
664         walk = head;
665         err = -EFAULT;
666         while(walk != NULL) {
667                 struct pollfd *fds = walk->entries;
668                 int j;
669
670                 for (j=0; j < walk->len; j++, ufds++) {
671                         if(__put_user(fds[j].revents, &ufds->revents))
672                                 goto out_fds;
673                 }
674                 walk = walk->next;
675         }
676         err = fdcount;
677         if (!fdcount && signal_pending(current))
678                 err = -EINTR;
679 out_fds:
680         walk = head;
681         while(walk!=NULL) {
682                 struct poll_list *pp = walk->next;
683                 kfree(walk);
684                 walk = pp;
685         }
686         poll_freewait(&table);
687         return err;
688 }
689
690 asmlinkage long sys_poll(struct pollfd __user *ufds, unsigned int nfds,
691                         long timeout_msecs)
692 {
693         s64 timeout_jiffies = 0;
694
695         if (timeout_msecs) {
696 #if HZ > 1000
697                 /* We can only overflow if HZ > 1000 */
698                 if (timeout_msecs / 1000 > (s64)0x7fffffffffffffffULL / (s64)HZ)
699                         timeout_jiffies = -1;
700                 else
701 #endif
702                         timeout_jiffies = msecs_to_jiffies(timeout_msecs);
703         }
704
705         return do_sys_poll(ufds, nfds, &timeout_jiffies);
706 }
707
708 #ifdef TIF_RESTORE_SIGMASK
709 asmlinkage long sys_ppoll(struct pollfd __user *ufds, unsigned int nfds,
710         struct timespec __user *tsp, const sigset_t __user *sigmask,
711         size_t sigsetsize)
712 {
713         sigset_t ksigmask, sigsaved;
714         struct timespec ts;
715         s64 timeout = -1;
716         int ret;
717
718         if (tsp) {
719                 if (copy_from_user(&ts, tsp, sizeof(ts)))
720                         return -EFAULT;
721
722                 /* Cast to u64 to make GCC stop complaining */
723                 if ((u64)ts.tv_sec >= (u64)MAX_INT64_SECONDS)
724                         timeout = -1;   /* infinite */
725                 else {
726                         timeout = ROUND_UP(ts.tv_nsec, NSEC_PER_SEC/HZ);
727                         timeout += ts.tv_sec * HZ;
728                 }
729         }
730
731         if (sigmask) {
732                 /* XXX: Don't preclude handling different sized sigset_t's.  */
733                 if (sigsetsize != sizeof(sigset_t))
734                         return -EINVAL;
735                 if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
736                         return -EFAULT;
737
738                 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
739                 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
740         }
741
742         ret = do_sys_poll(ufds, nfds, &timeout);
743
744         /* We can restart this syscall, usually */
745         if (ret == -EINTR) {
746                 /*
747                  * Don't restore the signal mask yet. Let do_signal() deliver
748                  * the signal on the way back to userspace, before the signal
749                  * mask is restored.
750                  */
751                 if (sigmask) {
752                         memcpy(&current->saved_sigmask, &sigsaved,
753                                         sizeof(sigsaved));
754                         set_thread_flag(TIF_RESTORE_SIGMASK);
755                 }
756                 ret = -ERESTARTNOHAND;
757         } else if (sigmask)
758                 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
759
760         if (tsp && timeout >= 0) {
761                 if (current->personality & STICKY_TIMEOUTS)
762                         goto sticky;
763                 /* Yes, we know it's actually an s64, but it's also positive. */
764                 ts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) * 1000;
765                 ts.tv_sec = timeout;
766                 if (copy_to_user(tsp, &ts, sizeof(ts))) {
767                 sticky:
768                         /*
769                          * If an application puts its timeval in read-only
770                          * memory, we don't want the Linux-specific update to
771                          * the timeval to cause a fault after the select has
772                          * completed successfully. However, because we're not
773                          * updating the timeval, we can't restart the system
774                          * call.
775                          */
776                         if (ret == -ERESTARTNOHAND && timeout >= 0)
777                                 ret = -EINTR;
778                 }
779         }
780
781         return ret;
782 }
783 #endif /* TIF_RESTORE_SIGMASK */