From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lb0-f173.google.com (mail-lb0-f173.google.com [209.85.217.173]) by dpdk.org (Postfix) with ESMTP id 08D6FB62 for ; Mon, 23 Mar 2015 16:15:15 +0100 (CET) Received: by lbcmq2 with SMTP id mq2so10512029lbc.0 for ; Mon, 23 Mar 2015 08:15:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mirantis.com; s=google; h=from:to:subject:date:message-id:in-reply-to:references; bh=TXSM3YAISCv9COmNPQGt5Jw3Co6GKi34P5kaWtKZOPo=; b=VeY51FHS9j7DzClJdjVfGgsz02EKHB6yLCnRDSWIfSz/Pi0MeN7Zb1JCFYXsZu2tc/ GfZtilkCSSa4GsiOpfqJVWSKF6e1wT00CN2ey+yTssJ4lCv3tKET8Vryx5OjHq4MIA1X LzsPbrEdlSc4ZQp6h00QHghbORm/JkjKkB/HE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=TXSM3YAISCv9COmNPQGt5Jw3Co6GKi34P5kaWtKZOPo=; b=a5Lb3kC6GkLOzCnIzpqb+P1lIsBkDAvp6lm3Vqcr0T323GJfsoGPJHZjmsZAGK9mp5 ikHy4tgSkRP5xoOId9Su8A6+zFXdIWVWVcknX4Uh7XoPy3qZegyi0BR84HxaH6RUF2Dy s1e/YLjU/8s/N5VT4r30hCtnvN6fo4Yk5YBW6S8brfq+At0+UePmXzTVoGGQBmcOmYox 0UFJnnYZI+j/86NAQ90gLcYB3L/lE5fLGH9U20GC4aBaq2eENQGONivXPdsar082huEd leywhLZb3qPjG+pTs5dxVaAEUKKQXU/YzGQ6SZqwT9tZ33CmqGZjbezxBUdHvC7ZL6rm OLXw== X-Gm-Message-State: ALoCoQmNMIlu04fMrM4enkaBRrZ8dHCtBhyjE+KeEoSYVkAJn1kRsGoDvtUIq0yIrCteMqbaMgWN X-Received: by 10.152.45.101 with SMTP id l5mr72080652lam.95.1427123714769; Mon, 23 Mar 2015 08:15:14 -0700 (PDT) Received: from pboldin-pc.kha.mirantis.net ([194.213.110.67]) by mx.google.com with ESMTPSA id kh8sm234889lbc.45.2015.03.23.08.15.13 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 23 Mar 2015 08:15:14 -0700 (PDT) From: Pavel Boldin To: dev@dpdk.org Date: Mon, 23 Mar 2015 17:15:31 +0200 Message-Id: <1427123731-15654-1-git-send-email-pboldin@mirantis.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1426684571-14782-1-git-send-email-pboldin@mirantis.com> References: <1426684571-14782-1-git-send-email-pboldin@mirantis.com> Subject: [dpdk-dev] [PATCH v3] vhost: Refactor module `eventfd_link' X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: patches and discussions about DPDK List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 23 Mar 2015 15:15:15 -0000 Changes: * Remove unnecessary #include's. * Deindent by moving the code to an inline function. * Fix return codes. Use appropriate return code for each fault cause. * Remove copy-pasted `close_fd', call `sys_close' instead. * Use `get_pid_task' to correctly reference the `task_target'. Signed-off-by: Pavel Boldin --- Changes since last submission: * Using `sys_close' to close fd. * Removing unnecessary #include's. lib/librte_vhost/eventfd_link/eventfd_link.c | 193 ++++++++++++++------------- 1 file changed, 98 insertions(+), 95 deletions(-) diff --git a/lib/librte_vhost/eventfd_link/eventfd_link.c b/lib/librte_vhost/eventfd_link/eventfd_link.c index 7755dd6..d10e25f 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 #include #include -#include -#include #include -#include -#include -#include -#include -#include #include +#include #include "eventfd_link.h" @@ -65,100 +58,110 @@ put_files_struct(struct files_struct *files) BUG(); } +static struct file * +fget_from_files(struct files_struct *files, unsigned fd) +{ + struct file *file; -static long -eventfd_link_ioctl(struct file *f, unsigned int ioctl, unsigned long arg) + 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 inline long +eventfd_link_ioctl_copy(unsigned long arg) { - void __user *argp = (void __user *) arg; + long ret = -EFAULT; struct task_struct *task_target = NULL; - struct file *file; - struct files_struct *files; - struct fdtable *fdt; + struct file *target_file = NULL; + struct files_struct *target_files = NULL; struct eventfd_copy eventfd_copy; + struct pid *pid; + + if (copy_from_user(&eventfd_copy, (void __user*)arg, + sizeof(struct eventfd_copy))) + goto out; + + /* + * Find the task struct for the target pid + */ + ret = -ESRCH; + + pid = find_vpid(eventfd_copy.target_pid); + if (pid == NULL) { + pr_info("Unable to find pid %d\n", eventfd_copy.target_pid); + goto out; + } + + task_target = get_pid_task(pid, PIDTYPE_PID); + if (task_target == NULL) { + pr_info("Failed to get task for pid %d\n", + eventfd_copy.target_pid); + goto out; + } + + ret = sys_close(eventfd_copy.source_fd); + if (ret) + goto out_task; + ret = -ESTALE; - switch (ioctl) { + /* + * Find the file struct associated with the target fd. + */ + + target_files = get_files_struct(task_target); + if (target_files == NULL) { + pr_info("Failed to get target files struct\n"); + goto out_task; + } + + ret = -EBADF; + target_file = fget_from_files(target_files, eventfd_copy.target_fd); + + if (target_file == NULL) { + pr_info("Failed to get file from target pid\n"); + goto out_target_files; + } + + + /* + * Install the file struct from the target process into the + * file desciptor of the source process, + */ + + fd_install(eventfd_copy.source_fd, target_file); + + ret = 0; + +out_target_files: + put_files_struct(target_files); +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: - 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 (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); - 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(); - put_files_struct(files); - - 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, - */ - - fd_install(eventfd_copy.source_fd, file); - - return 0; - - default: - return -ENOIOCTLCMD; + ret = eventfd_link_ioctl_copy(arg); + break; } + + return ret; } static const struct file_operations eventfd_link_fops = { -- 1.9.1