Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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