Merge tag 'net-next-6.9' of git://git.kernel.org/pub/scm/linux/kernel/git/netdev...
[sfrench/cifs-2.6.git] / fs / eventpoll.c
index 39ac6fdf8bcab38533b3c2f4e5b4bb97ed3432fa..882b89edc52adb24e6f62dc2b778e186001293d4 100644 (file)
@@ -37,6 +37,7 @@
 #include <linux/seq_file.h>
 #include <linux/compat.h>
 #include <linux/rculist.h>
+#include <linux/capability.h>
 #include <net/busy_poll.h>
 
 /*
@@ -227,6 +228,11 @@ struct eventpoll {
 #ifdef CONFIG_NET_RX_BUSY_POLL
        /* used to track busy poll napi_id */
        unsigned int napi_id;
+       /* busy poll timeout */
+       u32 busy_poll_usecs;
+       /* busy poll packet budget */
+       u16 busy_poll_budget;
+       bool prefer_busy_poll;
 #endif
 
 #ifdef CONFIG_DEBUG_LOCK_ALLOC
@@ -387,11 +393,41 @@ static inline int ep_events_available(struct eventpoll *ep)
 }
 
 #ifdef CONFIG_NET_RX_BUSY_POLL
+/**
+ * busy_loop_ep_timeout - check if busy poll has timed out. The timeout value
+ * from the epoll instance ep is preferred, but if it is not set fallback to
+ * the system-wide global via busy_loop_timeout.
+ *
+ * @start_time: The start time used to compute the remaining time until timeout.
+ * @ep: Pointer to the eventpoll context.
+ *
+ * Return: true if the timeout has expired, false otherwise.
+ */
+static bool busy_loop_ep_timeout(unsigned long start_time,
+                                struct eventpoll *ep)
+{
+       unsigned long bp_usec = READ_ONCE(ep->busy_poll_usecs);
+
+       if (bp_usec) {
+               unsigned long end_time = start_time + bp_usec;
+               unsigned long now = busy_loop_current_time();
+
+               return time_after(now, end_time);
+       } else {
+               return busy_loop_timeout(start_time);
+       }
+}
+
+static bool ep_busy_loop_on(struct eventpoll *ep)
+{
+       return !!ep->busy_poll_usecs || net_busy_loop_on();
+}
+
 static bool ep_busy_loop_end(void *p, unsigned long start_time)
 {
        struct eventpoll *ep = p;
 
-       return ep_events_available(ep) || busy_loop_timeout(start_time);
+       return ep_events_available(ep) || busy_loop_ep_timeout(start_time, ep);
 }
 
 /*
@@ -403,10 +439,15 @@ static bool ep_busy_loop_end(void *p, unsigned long start_time)
 static bool ep_busy_loop(struct eventpoll *ep, int nonblock)
 {
        unsigned int napi_id = READ_ONCE(ep->napi_id);
+       u16 budget = READ_ONCE(ep->busy_poll_budget);
+       bool prefer_busy_poll = READ_ONCE(ep->prefer_busy_poll);
+
+       if (!budget)
+               budget = BUSY_POLL_BUDGET;
 
-       if ((napi_id >= MIN_NAPI_ID) && net_busy_loop_on()) {
-               napi_busy_loop(napi_id, nonblock ? NULL : ep_busy_loop_end, ep, false,
-                              BUSY_POLL_BUDGET);
+       if (napi_id >= MIN_NAPI_ID && ep_busy_loop_on(ep)) {
+               napi_busy_loop(napi_id, nonblock ? NULL : ep_busy_loop_end,
+                              ep, prefer_busy_poll, budget);
                if (ep_events_available(ep))
                        return true;
                /*
@@ -425,12 +466,12 @@ static bool ep_busy_loop(struct eventpoll *ep, int nonblock)
  */
 static inline void ep_set_busy_poll_napi_id(struct epitem *epi)
 {
-       struct eventpoll *ep;
+       struct eventpoll *ep = epi->ep;
        unsigned int napi_id;
        struct socket *sock;
        struct sock *sk;
 
-       if (!net_busy_loop_on())
+       if (!ep_busy_loop_on(ep))
                return;
 
        sock = sock_from_file(epi->ffd.file);
@@ -442,7 +483,6 @@ static inline void ep_set_busy_poll_napi_id(struct epitem *epi)
                return;
 
        napi_id = READ_ONCE(sk->sk_napi_id);
-       ep = epi->ep;
 
        /* Non-NAPI IDs can be rejected
         *      or
@@ -455,6 +495,49 @@ static inline void ep_set_busy_poll_napi_id(struct epitem *epi)
        ep->napi_id = napi_id;
 }
 
+static long ep_eventpoll_bp_ioctl(struct file *file, unsigned int cmd,
+                                 unsigned long arg)
+{
+       struct eventpoll *ep = file->private_data;
+       void __user *uarg = (void __user *)arg;
+       struct epoll_params epoll_params;
+
+       switch (cmd) {
+       case EPIOCSPARAMS:
+               if (copy_from_user(&epoll_params, uarg, sizeof(epoll_params)))
+                       return -EFAULT;
+
+               /* pad byte must be zero */
+               if (epoll_params.__pad)
+                       return -EINVAL;
+
+               if (epoll_params.busy_poll_usecs > S32_MAX)
+                       return -EINVAL;
+
+               if (epoll_params.prefer_busy_poll > 1)
+                       return -EINVAL;
+
+               if (epoll_params.busy_poll_budget > NAPI_POLL_WEIGHT &&
+                   !capable(CAP_NET_ADMIN))
+                       return -EPERM;
+
+               WRITE_ONCE(ep->busy_poll_usecs, epoll_params.busy_poll_usecs);
+               WRITE_ONCE(ep->busy_poll_budget, epoll_params.busy_poll_budget);
+               WRITE_ONCE(ep->prefer_busy_poll, epoll_params.prefer_busy_poll);
+               return 0;
+       case EPIOCGPARAMS:
+               memset(&epoll_params, 0, sizeof(epoll_params));
+               epoll_params.busy_poll_usecs = READ_ONCE(ep->busy_poll_usecs);
+               epoll_params.busy_poll_budget = READ_ONCE(ep->busy_poll_budget);
+               epoll_params.prefer_busy_poll = READ_ONCE(ep->prefer_busy_poll);
+               if (copy_to_user(uarg, &epoll_params, sizeof(epoll_params)))
+                       return -EFAULT;
+               return 0;
+       default:
+               return -ENOIOCTLCMD;
+       }
+}
+
 #else
 
 static inline bool ep_busy_loop(struct eventpoll *ep, int nonblock)
@@ -466,6 +549,12 @@ static inline void ep_set_busy_poll_napi_id(struct epitem *epi)
 {
 }
 
+static long ep_eventpoll_bp_ioctl(struct file *file, unsigned int cmd,
+                                 unsigned long arg)
+{
+       return -EOPNOTSUPP;
+}
+
 #endif /* CONFIG_NET_RX_BUSY_POLL */
 
 /*
@@ -819,6 +908,27 @@ static void ep_clear_and_put(struct eventpoll *ep)
                ep_free(ep);
 }
 
+static long ep_eventpoll_ioctl(struct file *file, unsigned int cmd,
+                              unsigned long arg)
+{
+       int ret;
+
+       if (!is_file_epoll(file))
+               return -EINVAL;
+
+       switch (cmd) {
+       case EPIOCSPARAMS:
+       case EPIOCGPARAMS:
+               ret = ep_eventpoll_bp_ioctl(file, cmd, arg);
+               break;
+       default:
+               ret = -EINVAL;
+               break;
+       }
+
+       return ret;
+}
+
 static int ep_eventpoll_release(struct inode *inode, struct file *file)
 {
        struct eventpoll *ep = file->private_data;
@@ -925,6 +1035,8 @@ static const struct file_operations eventpoll_fops = {
        .release        = ep_eventpoll_release,
        .poll           = ep_eventpoll_poll,
        .llseek         = noop_llseek,
+       .unlocked_ioctl = ep_eventpoll_ioctl,
+       .compat_ioctl   = compat_ptr_ioctl,
 };
 
 /*
@@ -2052,6 +2164,11 @@ static int do_epoll_create(int flags)
                error = PTR_ERR(file);
                goto out_free_fd;
        }
+#ifdef CONFIG_NET_RX_BUSY_POLL
+       ep->busy_poll_usecs = 0;
+       ep->busy_poll_budget = 0;
+       ep->prefer_busy_poll = false;
+#endif
        ep->file = file;
        fd_install(fd, file);
        return fd;