[dpdk-dev,v5,1/4] vhost: eventfd_link: refactoring EVENTFD_COPY handler

Message ID 1440787880-7079-1-git-send-email-pboldin@mirantis.com (mailing list archive)
State Superseded, archived
Headers

Commit Message

Pavel Boldin Aug. 28, 2015, 6:51 p.m. UTC
  * Move ioctl `EVENTFD_COPY' code to a separate function
* Remove extra #includes
* Introduce function fget_from_files
* Fix ioctl return values

Signed-off-by: Pavel Boldin <pboldin@mirantis.com>
---
 lib/librte_vhost/eventfd_link/eventfd_link.c | 188 +++++++++++++++------------
 1 file changed, 103 insertions(+), 85 deletions(-)
  

Comments

Pavel Boldin Sept. 23, 2015, 8:25 p.m. UTC | #1
Ping.

On Fri, Aug 28, 2015 at 9:51 PM, Pavel Boldin <pboldin@mirantis.com> wrote:

> * Move ioctl `EVENTFD_COPY' code to a separate function
> * Remove extra #includes
> * Introduce function fget_from_files
> * Fix ioctl return values
>
> Signed-off-by: Pavel Boldin <pboldin@mirantis.com>
> ---
>  lib/librte_vhost/eventfd_link/eventfd_link.c | 188
> +++++++++++++++------------
>  1 file changed, 103 insertions(+), 85 deletions(-)
>
> diff --git a/lib/librte_vhost/eventfd_link/eventfd_link.c
> b/lib/librte_vhost/eventfd_link/eventfd_link.c
> index 62c45c8..5ba1068 100644
> --- a/lib/librte_vhost/eventfd_link/eventfd_link.c
> +++ b/lib/librte_vhost/eventfd_link/eventfd_link.c
> @@ -22,18 +22,11 @@
>   *   Intel Corporation
>   */
>
> -#include <linux/eventfd.h>
>  #include <linux/miscdevice.h>
>  #include <linux/module.h>
> -#include <linux/moduleparam.h>
> -#include <linux/rcupdate.h>
>  #include <linux/file.h>
> -#include <linux/slab.h>
> -#include <linux/fs.h>
> -#include <linux/mmu_context.h>
> -#include <linux/sched.h>
> -#include <asm/mmu_context.h>
>  #include <linux/fdtable.h>
> +#include <linux/syscalls.h>
>
>  #include "eventfd_link.h"
>
> @@ -65,9 +58,26 @@ put_files_struct(struct files_struct *files)
>                 BUG();
>  }
>
> +static struct file *
> +fget_from_files(struct files_struct *files, unsigned fd)
> +{
> +       struct file *file;
> +
> +       rcu_read_lock();
> +       file = fcheck_files(files, fd);
> +       if (file)
> +       {
> +               if (file->f_mode & FMODE_PATH
> +                       || !atomic_long_inc_not_zero(&file->f_count))
> +                   file = NULL;
> +       }
> +       rcu_read_unlock();
> +
> +       return file;
> +}
>
>  static long
> -eventfd_link_ioctl(struct file *f, unsigned int ioctl, unsigned long arg)
> +eventfd_link_ioctl_copy(unsigned long arg)
>  {
>         void __user *argp = (void __user *) arg;
>         struct task_struct *task_target = NULL;
> @@ -75,91 +85,99 @@ eventfd_link_ioctl(struct file *f, unsigned int ioctl,
> unsigned long arg)
>         struct files_struct *files;
>         struct fdtable *fdt;
>         struct eventfd_copy eventfd_copy;
> +       long ret = -EFAULT;
>
> -       switch (ioctl) {
> -       case EVENTFD_COPY:
> -               if (copy_from_user(&eventfd_copy, argp,
> -                       sizeof(struct eventfd_copy)))
> -                       return -EFAULT;
> -
> -               /*
> -                * Find the task struct for the target pid
> -                */
> -               task_target =
> -                       pid_task(find_vpid(eventfd_copy.target_pid),
> PIDTYPE_PID);
> -               if (task_target == NULL) {
> -                       pr_debug("Failed to get mem ctx for target pid\n");
> -                       return -EFAULT;
> -               }
> -
> -               files = get_files_struct(current);
> -               if (files == NULL) {
> -                       pr_debug("Failed to get files struct\n");
> -                       return -EFAULT;
> -               }
> -
> -               rcu_read_lock();
> -               file = fcheck_files(files, eventfd_copy.source_fd);
> -               if (file) {
> -                       if (file->f_mode & FMODE_PATH ||
> -                               !atomic_long_inc_not_zero(&file->f_count))
> -                               file = NULL;
> -               }
> -               rcu_read_unlock();
> -               put_files_struct(files);
> +       if (copy_from_user(&eventfd_copy, argp, sizeof(struct
> eventfd_copy)))
> +               goto out;
> +
> +       /*
> +        * Find the task struct for the target pid
> +        */
> +       ret = -ESRCH;
> +
> +       task_target =
> +               get_pid_task(find_vpid(eventfd_copy.target_pid),
> PIDTYPE_PID);
> +       if (task_target == NULL) {
> +               pr_info("Unable to find pid %d\n",
> eventfd_copy.target_pid);
> +               goto out;
> +       }
> +
> +       ret = -ESTALE;
> +       files = get_files_struct(current);
> +       if (files == NULL) {
> +               pr_info("Failed to get current files struct\n");
> +               goto out_task;
> +       }
>
> -               if (file == NULL) {
> -                       pr_debug("Failed to get file from source pid\n");
> -                       return 0;
> -               }
> -
> -               /*
> -                * Release the existing eventfd in the source process
> -                */
> -               spin_lock(&files->file_lock);
> -               fput(file);
> -               filp_close(file, files);
> -               fdt = files_fdtable(files);
> -               fdt->fd[eventfd_copy.source_fd] = NULL;
> -               spin_unlock(&files->file_lock);
> -
> -               /*
> -                * Find the file struct associated with the target fd.
> -                */
> -
> -               files = get_files_struct(task_target);
> -               if (files == NULL) {
> -                       pr_debug("Failed to get files struct\n");
> -                       return -EFAULT;
> -               }
> -
> -               rcu_read_lock();
> -               file = fcheck_files(files, eventfd_copy.target_fd);
> -               if (file) {
> -                       if (file->f_mode & FMODE_PATH ||
> -                               !atomic_long_inc_not_zero(&file->f_count))
> -                                       file = NULL;
> -               }
> -               rcu_read_unlock();
> +       ret = -EBADF;
> +       file = fget_from_files(files, eventfd_copy.source_fd);
> +
> +       if (file == NULL) {
> +               pr_info("Failed to get fd %d from source\n",
> +                       eventfd_copy.source_fd);
>                 put_files_struct(files);
> +               goto out_task;
> +       }
> +
> +       /*
> +        * Release the existing eventfd in the source process
> +        */
> +       spin_lock(&files->file_lock);
> +       fput(file);
> +       filp_close(file, files);
> +       fdt = files_fdtable(files);
> +       fdt->fd[eventfd_copy.source_fd] = NULL;
> +       spin_unlock(&files->file_lock);
> +
> +       put_files_struct(files);
> +
> +       /*
> +        * Find the file struct associated with the target fd.
> +        */
> +
> +       ret = -ESTALE;
> +       files = get_files_struct(task_target);
> +       if (files == NULL) {
> +               pr_info("Failed to get target files struct\n");
> +               goto out_task;
> +       }
> +
> +       ret = -EBADF;
> +       file = fget_from_files(files, eventfd_copy.target_fd);
> +       put_files_struct(files);
> +
> +       if (file == NULL) {
> +               pr_info("Failed to get fd %d from target\n",
> +                       eventfd_copy.target_fd);
> +               goto out_task;
> +       }
>
> -               if (file == NULL) {
> -                       pr_debug("Failed to get file from target pid\n");
> -                       return 0;
> -               }
> +       /*
> +        * Install the file struct from the target process into the
> +        * file desciptor of the source process,
> +        */
>
> -               /*
> -                * Install the file struct from the target process into the
> -                * file desciptor of the source process,
> -                */
> +       fd_install(eventfd_copy.source_fd, file);
> +       ret = 0;
>
> -               fd_install(eventfd_copy.source_fd, file);
> +out_task:
> +       put_task_struct(task_target);
> +out:
> +       return ret;
> +}
>
> -               return 0;
> +static long
> +eventfd_link_ioctl(struct file *f, unsigned int ioctl, unsigned long arg)
> +{
> +       long ret = -ENOIOCTLCMD;
>
> -       default:
> -               return -ENOIOCTLCMD;
> +       switch (ioctl) {
> +       case EVENTFD_COPY:
> +               ret = eventfd_link_ioctl_copy(arg);
> +               break;
>         }
> +
> +       return ret;
>  }
>
>  static const struct file_operations eventfd_link_fops = {
> --
> 1.9.1
>
>
  
Thomas Monjalon Sept. 29, 2015, 7:42 p.m. UTC | #2
Hi Pavel,

2015-09-23 23:25, Pavel Boldin:
> Ping.

Are you requesting a review?
I think you should set Huawei Xie in the recipient list.

Other note: it is not easy to follow the evolution of your patches because
there is no cover letter and the numbering (v5) is not incremented.

These little details may make review happening ;)
  
Pavel Boldin Sept. 29, 2015, 11:29 p.m. UTC | #3
Hi Thomas,

On Tue, Sep 29, 2015 at 10:42 PM, Thomas Monjalon <thomas.monjalon@6wind.com
> wrote:

> Hi Pavel,
>
> 2015-09-23 23:25, Pavel Boldin:
> > Ping.
>
> Are you requesting a review?
>
Yes.


> I think you should set Huawei Xie in the recipient list.
>
Huawei Xie CCed


>
> Other note: it is not easy to follow the evolution of your patches because
> there is no cover letter and the numbering (v5) is not incremented.
>
Should I reupload them then?

Pavel
  
Huawei Xie Oct. 20, 2015, 9:06 a.m. UTC | #4
On 8/29/2015 2:51 AM, Pavel Boldin wrote:
> * Move ioctl `EVENTFD_COPY' code to a separate function
> * Remove extra #includes
> * Introduce function fget_from_files
> * Fix ioctl return values
>
> Signed-off-by: Pavel Boldin <pboldin@mirantis.com>
Basically OK with this patch since we have reviewed before.
Could you split the patch, one change each patch? Thomas will require
you to do this too. :). I see you have also changed when to call
put_files_struct. It is better put it in a separate patch with the reason.
Another thing is code style. Run checkpatch.pl against your patch. There
are some issues.
  
Pavel Boldin Oct. 28, 2015, 6:33 p.m. UTC | #5
The patchset contains an attempt at the refactoring the `eventfd_link`
kernel module that is used to steal an FD in DPDK.

The first patch refactors old EVENTFD_COPY handler fixing the codepath
and errors returned from the kernel space. This patch is retained
for the backward compatibility.

The next one introduces a new more clean implementation of the
EVENTFD_COPY2 ioctl that allocates a new fd for the `struct file'
being stolen.

The last patch uses this new mechanism in the DPDK userspace.

Pavel Boldin (3):
  vhost: eventfd_link: refactoring EVENTFD_COPY handler
  vhost: add EVENTFD_COPY2 ioctl
  vhost: using EVENTFD_COPY2

 lib/librte_vhost/eventfd_link/eventfd_link.c | 249 ++++++++++++++++++---------
 lib/librte_vhost/eventfd_link/eventfd_link.h |  28 ++-
 lib/librte_vhost/vhost_cuse/eventfd_copy.c   |  54 ++++--
 lib/librte_vhost/vhost_cuse/eventfd_copy.h   |   6 +
 lib/librte_vhost/vhost_cuse/vhost-net-cdev.c |   3 +
 5 files changed, 231 insertions(+), 109 deletions(-)
  
Huawei Xie Oct. 29, 2015, 6:33 p.m. UTC | #6
On 10/29/2015 2:34 AM, Pavel Boldin wrote:
> The patchset contains an attempt at the refactoring the `eventfd_link`
> kernel module that is used to steal an FD in DPDK.
>
> The first patch refactors old EVENTFD_COPY handler fixing the codepath
> and errors returned from the kernel space. This patch is retained
> for the backward compatibility.
>
> The next one introduces a new more clean implementation of the
> EVENTFD_COPY2 ioctl that allocates a new fd for the `struct file'
> being stolen.
>
> The last patch uses this new mechanism in the DPDK userspace.
Pavel:
Don't forget signoff and version change message next time.
Or if you have time today, could you send a new version?

Acked-by: Huawei Xie <huawei.xie@intel.com>

> Pavel Boldin (3):
>   vhost: eventfd_link: refactoring EVENTFD_COPY handler
>   vhost: add EVENTFD_COPY2 ioctl
>   vhost: using EVENTFD_COPY2
>
>  lib/librte_vhost/eventfd_link/eventfd_link.c | 249 ++++++++++++++++++---------
>  lib/librte_vhost/eventfd_link/eventfd_link.h |  28 ++-
>  lib/librte_vhost/vhost_cuse/eventfd_copy.c   |  54 ++++--
>  lib/librte_vhost/vhost_cuse/eventfd_copy.h   |   6 +
>  lib/librte_vhost/vhost_cuse/vhost-net-cdev.c |   3 +
>  5 files changed, 231 insertions(+), 109 deletions(-)
>
  
Thomas Monjalon Oct. 30, 2015, 7:10 p.m. UTC | #7
2015-10-29 18:33, Xie, Huawei:
> On 10/29/2015 2:34 AM, Pavel Boldin wrote:
> > The patchset contains an attempt at the refactoring the `eventfd_link`
> > kernel module that is used to steal an FD in DPDK.
> >
> > The first patch refactors old EVENTFD_COPY handler fixing the codepath
> > and errors returned from the kernel space. This patch is retained
> > for the backward compatibility.
> >
> > The next one introduces a new more clean implementation of the
> > EVENTFD_COPY2 ioctl that allocates a new fd for the `struct file'
> > being stolen.
> >
> > The last patch uses this new mechanism in the DPDK userspace.
> Pavel:
> Don't forget signoff and version change message next time.
> Or if you have time today, could you send a new version?
> 
> Acked-by: Huawei Xie <huawei.xie@intel.com>

Applied, thanks
  

Patch

diff --git a/lib/librte_vhost/eventfd_link/eventfd_link.c b/lib/librte_vhost/eventfd_link/eventfd_link.c
index 62c45c8..5ba1068 100644
--- a/lib/librte_vhost/eventfd_link/eventfd_link.c
+++ b/lib/librte_vhost/eventfd_link/eventfd_link.c
@@ -22,18 +22,11 @@ 
  *   Intel Corporation
  */
 
-#include <linux/eventfd.h>
 #include <linux/miscdevice.h>
 #include <linux/module.h>
-#include <linux/moduleparam.h>
-#include <linux/rcupdate.h>
 #include <linux/file.h>
-#include <linux/slab.h>
-#include <linux/fs.h>
-#include <linux/mmu_context.h>
-#include <linux/sched.h>
-#include <asm/mmu_context.h>
 #include <linux/fdtable.h>
+#include <linux/syscalls.h>
 
 #include "eventfd_link.h"
 
@@ -65,9 +58,26 @@  put_files_struct(struct files_struct *files)
 		BUG();
 }
 
+static struct file *
+fget_from_files(struct files_struct *files, unsigned fd)
+{
+	struct file *file;
+
+	rcu_read_lock();
+	file = fcheck_files(files, fd);
+	if (file)
+	{
+		if (file->f_mode & FMODE_PATH
+			|| !atomic_long_inc_not_zero(&file->f_count))
+		    file = NULL;
+	}
+	rcu_read_unlock();
+
+	return file;
+}
 
 static long
-eventfd_link_ioctl(struct file *f, unsigned int ioctl, unsigned long arg)
+eventfd_link_ioctl_copy(unsigned long arg)
 {
 	void __user *argp = (void __user *) arg;
 	struct task_struct *task_target = NULL;
@@ -75,91 +85,99 @@  eventfd_link_ioctl(struct file *f, unsigned int ioctl, unsigned long arg)
 	struct files_struct *files;
 	struct fdtable *fdt;
 	struct eventfd_copy eventfd_copy;
+	long ret = -EFAULT;
 
-	switch (ioctl) {
-	case EVENTFD_COPY:
-		if (copy_from_user(&eventfd_copy, argp,
-			sizeof(struct eventfd_copy)))
-			return -EFAULT;
-
-		/*
-		 * Find the task struct for the target pid
-		 */
-		task_target =
-			pid_task(find_vpid(eventfd_copy.target_pid), PIDTYPE_PID);
-		if (task_target == NULL) {
-			pr_debug("Failed to get mem ctx for target pid\n");
-			return -EFAULT;
-		}
-
-		files = get_files_struct(current);
-		if (files == NULL) {
-			pr_debug("Failed to get files struct\n");
-			return -EFAULT;
-		}
-
-		rcu_read_lock();
-		file = fcheck_files(files, eventfd_copy.source_fd);
-		if (file) {
-			if (file->f_mode & FMODE_PATH ||
-				!atomic_long_inc_not_zero(&file->f_count))
-				file = NULL;
-		}
-		rcu_read_unlock();
-		put_files_struct(files);
+	if (copy_from_user(&eventfd_copy, argp, sizeof(struct eventfd_copy)))
+		goto out;
+
+	/*
+	 * Find the task struct for the target pid
+	 */
+	ret = -ESRCH;
+
+	task_target =
+		get_pid_task(find_vpid(eventfd_copy.target_pid), PIDTYPE_PID);
+	if (task_target == NULL) {
+		pr_info("Unable to find pid %d\n", eventfd_copy.target_pid);
+		goto out;
+	}
+
+	ret = -ESTALE;
+	files = get_files_struct(current);
+	if (files == NULL) {
+		pr_info("Failed to get current files struct\n");
+		goto out_task;
+	}
 
-		if (file == NULL) {
-			pr_debug("Failed to get file from source pid\n");
-			return 0;
-		}
-
-		/*
-		 * Release the existing eventfd in the source process
-		 */
-		spin_lock(&files->file_lock);
-		fput(file);
-		filp_close(file, files);
-		fdt = files_fdtable(files);
-		fdt->fd[eventfd_copy.source_fd] = NULL;
-		spin_unlock(&files->file_lock);
-
-		/*
-		 * Find the file struct associated with the target fd.
-		 */
-
-		files = get_files_struct(task_target);
-		if (files == NULL) {
-			pr_debug("Failed to get files struct\n");
-			return -EFAULT;
-		}
-
-		rcu_read_lock();
-		file = fcheck_files(files, eventfd_copy.target_fd);
-		if (file) {
-			if (file->f_mode & FMODE_PATH ||
-				!atomic_long_inc_not_zero(&file->f_count))
-					file = NULL;
-		}
-		rcu_read_unlock();
+	ret = -EBADF;
+	file = fget_from_files(files, eventfd_copy.source_fd);
+
+	if (file == NULL) {
+		pr_info("Failed to get fd %d from source\n",
+			eventfd_copy.source_fd);
 		put_files_struct(files);
+		goto out_task;
+	}
+
+	/*
+	 * Release the existing eventfd in the source process
+	 */
+	spin_lock(&files->file_lock);
+	fput(file);
+	filp_close(file, files);
+	fdt = files_fdtable(files);
+	fdt->fd[eventfd_copy.source_fd] = NULL;
+	spin_unlock(&files->file_lock);
+
+	put_files_struct(files);
+
+	/*
+	 * Find the file struct associated with the target fd.
+	 */
+
+	ret = -ESTALE;
+	files = get_files_struct(task_target);
+	if (files == NULL) {
+		pr_info("Failed to get target files struct\n");
+		goto out_task;
+	}
+
+	ret = -EBADF;
+	file = fget_from_files(files, eventfd_copy.target_fd);
+	put_files_struct(files);
+
+	if (file == NULL) {
+		pr_info("Failed to get fd %d from target\n",
+			eventfd_copy.target_fd);
+		goto out_task;
+	}
 
-		if (file == NULL) {
-			pr_debug("Failed to get file from target pid\n");
-			return 0;
-		}
+	/*
+	 * Install the file struct from the target process into the
+	 * file desciptor of the source process,
+	 */
 
-		/*
-		 * Install the file struct from the target process into the
-		 * file desciptor of the source process,
-		 */
+	fd_install(eventfd_copy.source_fd, file);
+	ret = 0;
 
-		fd_install(eventfd_copy.source_fd, file);
+out_task:
+	put_task_struct(task_target);
+out:
+	return ret;
+}
 
-		return 0;
+static long
+eventfd_link_ioctl(struct file *f, unsigned int ioctl, unsigned long arg)
+{
+	long ret = -ENOIOCTLCMD;
 
-	default:
-		return -ENOIOCTLCMD;
+	switch (ioctl) {
+	case EVENTFD_COPY:
+		ret = eventfd_link_ioctl_copy(arg);
+		break;
 	}
+
+	return ret;
 }
 
 static const struct file_operations eventfd_link_fops = {