@@ -1980,6 +1980,22 @@ static int ep_autoremove_wake_function(struct wait_queue_entry *wq_entry,
19801980 return ret ;
19811981}
19821982
1983+ static int ep_try_send_events (struct eventpoll * ep ,
1984+ struct epoll_event __user * events , int maxevents )
1985+ {
1986+ int res ;
1987+
1988+ /*
1989+ * Try to transfer events to user space. In case we get 0 events and
1990+ * there's still timeout left over, we go trying again in search of
1991+ * more luck.
1992+ */
1993+ res = ep_send_events (ep , events , maxevents );
1994+ if (res > 0 )
1995+ ep_suspend_napi_irqs (ep );
1996+ return res ;
1997+ }
1998+
19831999/**
19842000 * ep_poll - Retrieves ready events, and delivers them to the caller-supplied
19852001 * event buffer.
@@ -2031,17 +2047,9 @@ static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
20312047
20322048 while (1 ) {
20332049 if (eavail ) {
2034- /*
2035- * Try to transfer events to user space. In case we get
2036- * 0 events and there's still timeout left over, we go
2037- * trying again in search of more luck.
2038- */
2039- res = ep_send_events (ep , events , maxevents );
2040- if (res ) {
2041- if (res > 0 )
2042- ep_suspend_napi_irqs (ep );
2050+ res = ep_try_send_events (ep , events , maxevents );
2051+ if (res )
20432052 return res ;
2044- }
20452053 }
20462054
20472055 if (timed_out )
@@ -2445,6 +2453,47 @@ SYSCALL_DEFINE4(epoll_ctl, int, epfd, int, op, int, fd,
24452453 return do_epoll_ctl (epfd , op , fd , & epds , false);
24462454}
24472455
2456+ static int ep_check_params (struct file * file , struct epoll_event __user * evs ,
2457+ int maxevents )
2458+ {
2459+ /* The maximum number of event must be greater than zero */
2460+ if (maxevents <= 0 || maxevents > EP_MAX_EVENTS )
2461+ return - EINVAL ;
2462+
2463+ /* Verify that the area passed by the user is writeable */
2464+ if (!access_ok (evs , maxevents * sizeof (struct epoll_event )))
2465+ return - EFAULT ;
2466+
2467+ /*
2468+ * We have to check that the file structure underneath the fd
2469+ * the user passed to us _is_ an eventpoll file.
2470+ */
2471+ if (!is_file_epoll (file ))
2472+ return - EINVAL ;
2473+
2474+ return 0 ;
2475+ }
2476+
2477+ int epoll_sendevents (struct file * file , struct epoll_event __user * events ,
2478+ int maxevents )
2479+ {
2480+ struct eventpoll * ep ;
2481+ int ret ;
2482+
2483+ ret = ep_check_params (file , events , maxevents );
2484+ if (unlikely (ret ))
2485+ return ret ;
2486+
2487+ ep = file -> private_data ;
2488+ /*
2489+ * Racy call, but that's ok - it should get retried based on
2490+ * poll readiness anyway.
2491+ */
2492+ if (ep_events_available (ep ))
2493+ return ep_try_send_events (ep , events , maxevents );
2494+ return 0 ;
2495+ }
2496+
24482497/*
24492498 * Implement the event wait interface for the eventpoll file. It is the kernel
24502499 * part of the user space epoll_wait(2).
@@ -2453,26 +2502,16 @@ static int do_epoll_wait(int epfd, struct epoll_event __user *events,
24532502 int maxevents , struct timespec64 * to )
24542503{
24552504 struct eventpoll * ep ;
2456-
2457- /* The maximum number of event must be greater than zero */
2458- if (maxevents <= 0 || maxevents > EP_MAX_EVENTS )
2459- return - EINVAL ;
2460-
2461- /* Verify that the area passed by the user is writeable */
2462- if (!access_ok (events , maxevents * sizeof (struct epoll_event )))
2463- return - EFAULT ;
2505+ int ret ;
24642506
24652507 /* Get the "struct file *" for the eventpoll file */
24662508 CLASS (fd , f )(epfd );
24672509 if (fd_empty (f ))
24682510 return - EBADF ;
24692511
2470- /*
2471- * We have to check that the file structure underneath the fd
2472- * the user passed to us _is_ an eventpoll file.
2473- */
2474- if (!is_file_epoll (fd_file (f )))
2475- return - EINVAL ;
2512+ ret = ep_check_params (fd_file (f ), events , maxevents );
2513+ if (unlikely (ret ))
2514+ return ret ;
24762515
24772516 /*
24782517 * At this point it is safe to assume that the "private_data" contains
0 commit comments