All of lore.kernel.org
 help / color / mirror / Atom feed
From: Pavel Boldin <pboldin@mirantis.com>
To: dev@dpdk.org
Subject: [PATCH v6 1/3] vhost: eventfd_link: refactoring EVENTFD_COPY handler
Date: Wed, 28 Oct 2015 20:33:47 +0200	[thread overview]
Message-ID: <94749ae3774b54fa17b901f15eaccef6a0751d05.1446056748.git.pboldin@mirantis.com> (raw)
In-Reply-To: <cover.1446055521.git.pboldin@mirantis.com>
In-Reply-To: <1440787880-7079-1-git-send-email-pboldin@mirantis.com>

* 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 | 181 +++++++++++++++------------
 1 file changed, 100 insertions(+), 81 deletions(-)

diff --git a/lib/librte_vhost/eventfd_link/eventfd_link.c b/lib/librte_vhost/eventfd_link/eventfd_link.c
index 62c45c8..7cbebd4 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,27 @@ 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 +86,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;
-		}
+	if (copy_from_user(&eventfd_copy, argp, sizeof(struct eventfd_copy)))
+		goto out;
 
-		files = get_files_struct(current);
-		if (files == NULL) {
-			pr_debug("Failed to get files struct\n");
-			return -EFAULT;
-		}
+	/*
+	 * Find the task struct for the target pid
+	 */
+	ret = -ESRCH;
 
-		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);
+	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;
+	}
 
-		if (file == NULL) {
-			pr_debug("Failed to get file from source pid\n");
-			return 0;
-		}
+	ret = -ESTALE;
+	files = get_files_struct(current);
+	if (files == NULL) {
+		pr_info("Failed to get current files struct\n");
+		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);
-
-		/*
-		 * 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;
-		}
+	ret = -EBADF;
+	file = fget_from_files(files, eventfd_copy.source_fd);
 
-		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();
+	if (file == NULL) {
+		pr_info("Failed to get fd %d from source\n",
+			eventfd_copy.source_fd);
 		put_files_struct(files);
+		goto out_task;
+	}
 
-		if (file == NULL) {
-			pr_debug("Failed to get file from target 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);
+
+	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);
 
-		/*
-		 * Install the file struct from the target process into the
-		 * file desciptor of the source process,
-		 */
+	if (file == NULL) {
+		pr_info("Failed to get fd %d from target\n",
+			eventfd_copy.target_fd);
+		goto out_task;
+	}
 
-		fd_install(eventfd_copy.source_fd, file);
+	/*
+	 * Install the file struct from the target process into the
+	 * file desciptor of the source process,
+	 */
 
-		return 0;
+	fd_install(eventfd_copy.source_fd, file);
+	ret = 0;
 
-	default:
-		return -ENOIOCTLCMD;
+out_task:
+	put_task_struct(task_target);
+out:
+	return ret;
+}
+
+static long
+eventfd_link_ioctl(struct file *f, unsigned int ioctl, unsigned long arg)
+{
+	long ret = -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

  parent reply	other threads:[~2015-10-28 18:34 UTC|newest]

Thread overview: 59+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-16 16:40 [PATCH] Fix `eventfd_link' module leakages and races Pavel Boldin
     [not found] ` <1426524059-30886-1-git-send-email-pboldin+dpdk-nYU0QVwCCFFWk0Htik3J/w@public.gmane.org>
2015-03-16 17:55   ` Neil Horman
2015-03-17  1:29   ` Ouyang, Changchun
     [not found]     ` <F52918179C57134FAEC9EA62FA2F962511A5E173-E2R4CRU6q/6iAffOGbnezLfspsVTdybXVpNB7YpNyf8@public.gmane.org>
2015-03-20 15:57       ` Pavel Boldin
2015-03-18 13:16   ` [PATCH v2] " Pavel Boldin
     [not found]     ` <1426684571-14782-1-git-send-email-pboldin-nYU0QVwCCFFWk0Htik3J/w@public.gmane.org>
2015-03-23 11:15       ` Thomas Monjalon
2015-03-23 11:36         ` Pavel Boldin
     [not found]           ` <CACf4_B8buRSAPCFO+fWJjQ8it5n0cgN789jSrsB6Bc-cJGOyHQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2015-03-23 11:44             ` Thomas Monjalon
2015-03-23 15:15       ` [PATCH v3] vhost: Refactor module `eventfd_link' Pavel Boldin
     [not found]         ` <1427123731-15654-1-git-send-email-pboldin-nYU0QVwCCFFWk0Htik3J/w@public.gmane.org>
2015-04-02 17:01           ` [PATCH v4 0/5] " Pavel Boldin
     [not found]             ` <1427994080-10163-1-git-send-email-pboldin-nYU0QVwCCFFWk0Htik3J/w@public.gmane.org>
2015-04-02 17:01               ` [PATCH v4 1/5] vhost: eventfd_link: moving ioctl to a function Pavel Boldin
2015-05-07  7:57                 ` Xie, Huawei
     [not found]                   ` <C37D651A908B024F974696C65296B57B0F476887-0J0gbvR4kThpB2pF5aRoyrfspsVTdybXVpNB7YpNyf8@public.gmane.org>
2015-05-07 13:16                     ` Pavel Boldin
2015-05-18  6:06                       ` Xie, Huawei
2015-05-18  9:16                         ` Pavel Boldin
2015-06-18  3:08                       ` Xie, Huawei
2015-04-02 17:01               ` [PATCH v4 2/5] vhost: eventfd_link: add function fget_from_files Pavel Boldin
2015-04-02 17:01               ` [PATCH v4 3/5] vhost: eventfd_link: fix ioctl return values Pavel Boldin
2015-04-02 17:01               ` [PATCH v4 4/5] vhost: eventfd_link: replace copy-pasted sys_close Pavel Boldin
2015-04-02 17:01               ` [PATCH v4 5/5] vhost: eventfd_link: removing extra #includes Pavel Boldin
2015-04-16 11:48               ` [PATCH v5 0/5] Refactor module `eventfd_link' Pavel Boldin
     [not found]                 ` <1429184910-30186-1-git-send-email-pboldin-nYU0QVwCCFFWk0Htik3J/w@public.gmane.org>
2015-04-16 11:48                   ` [PATCH v5 1/5] vhost: eventfd_link: moving ioctl to a function Pavel Boldin
2015-08-28 18:51                     ` [PATCH v5 1/4] vhost: eventfd_link: refactoring EVENTFD_COPY handler Pavel Boldin
2015-09-23 20:25                       ` Pavel Boldin
2015-09-29 19:42                         ` Thomas Monjalon
2015-09-29 23:29                           ` Pavel Boldin
2015-10-20  9:06                       ` Xie, Huawei
2015-10-28 18:33                       ` [PATCH v6 0/3] vhost: eventfd_link refactoring Pavel Boldin
2015-10-29 18:33                         ` Xie, Huawei
2015-10-30 19:10                           ` Thomas Monjalon
2015-10-28 18:33                       ` Pavel Boldin [this message]
2015-10-28 18:33                       ` [PATCH v6 2/3] vhost: add EVENTFD_COPY2 ioctl Pavel Boldin
2015-10-28 18:33                       ` [PATCH v6 3/3] vhost: using EVENTFD_COPY2 Pavel Boldin
2015-08-28 18:51                     ` [PATCH v5 2/4] vhost: add EVENTFD_COPY2 ioctl Pavel Boldin
2015-10-20  9:43                       ` Xie, Huawei
2015-08-28 18:51                     ` [PATCH v5 3/4] vhost: using EVENTFD_COPY2 Pavel Boldin
2015-10-20  9:52                       ` Xie, Huawei
2015-10-21 12:16                         ` Pavel Boldin
2015-10-26  1:45                           ` Xie, Huawei
2015-10-28 18:35                             ` Pavel Boldin
2015-08-28 18:51                     ` [PATCH v5 4/4] DO NOT MERGE: Tests for new eventfd_link module Pavel Boldin
2015-04-16 11:48                   ` [PATCH v5 2/5] vhost: eventfd_link: add function fget_from_files Pavel Boldin
2015-04-16 11:48                   ` [PATCH v5 3/5] vhost: eventfd_link: fix ioctl return values Pavel Boldin
2015-04-16 11:48                   ` [PATCH v5 4/5] vhost: eventfd_link: replace copy-pasted sys_close Pavel Boldin
2015-05-07  6:54                     ` Xie, Huawei
2015-06-17 15:24                       ` Thomas Monjalon
2015-07-09  0:59                         ` Thomas Monjalon
2015-07-10 14:27                         ` Xie, Huawei
2015-07-10 14:50                           ` Pavel Boldin
2015-07-10 15:32                             ` Xie, Huawei
2015-07-10 15:42                             ` Xie, Huawei
2015-07-10 15:49                               ` Thomas Monjalon
2015-07-10 16:06                                 ` Xie, Huawei
2015-07-11 15:08                               ` Pavel Boldin
2015-07-13  1:59                                 ` Xie, Huawei
2015-07-19 12:39                                   ` Pavel Boldin
2015-04-16 11:48                   ` [PATCH v5 5/5] vhost: eventfd_link: removing extra #includes Pavel Boldin
2015-04-28 14:35                   ` [PATCH v5 0/5] Refactor module `eventfd_link' Thomas Monjalon
2015-05-04  5:29                     ` Xie, Huawei

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=94749ae3774b54fa17b901f15eaccef6a0751d05.1446056748.git.pboldin@mirantis.com \
    --to=pboldin@mirantis.com \
    --cc=dev@dpdk.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.