public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] staging: android: binder.c: binder_ioctl() cleanup
@ 2014-06-03 19:27 Tair Rzayev
  2014-06-04  6:22 ` Brian Swetland
  0 siblings, 1 reply; 2+ messages in thread
From: Tair Rzayev @ 2014-06-03 19:27 UTC (permalink / raw)
  To: Greg KH; +Cc: swetland, linux-kernel

binder_ioctl() is quite huge and checkpatch dirty - mostly because of 
the amount of code for the BINDER_WRITE_READ and BINDER_SET_CONTEXT_MGR.
Moved that code into the new binder_ioctl_write_read() and
binder_ioctl_set_ctx_mgr()

Signed-off-by: Tair Rzayev <tair.rzayev@gmail.com>
---
 drivers/staging/android/binder.c | 186 ++++++++++++++++++++++-----------------
 1 file changed, 107 insertions(+), 79 deletions(-)

diff --git a/drivers/staging/android/binder.c b/drivers/staging/android/binder.c
index 989f809..ed432e3 100644
--- a/drivers/staging/android/binder.c
+++ b/drivers/staging/android/binder.c
@@ -2594,6 +2594,106 @@ static unsigned int binder_poll(struct file *filp,
 	return 0;
 }
 
+static int binder_ioctl_write_read(struct file *filp,
+				unsigned int cmd, unsigned long arg,
+				struct binder_thread *thread)
+{
+	int ret = 0;
+	struct binder_proc *proc = filp->private_data;
+	unsigned int size = _IOC_SIZE(cmd);
+	void __user *ubuf = (void __user *)arg;
+	struct binder_write_read bwr;
+
+	if (size != sizeof(struct binder_write_read)) {
+		ret = -EINVAL;
+		goto out;
+	}
+	if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
+		ret = -EFAULT;
+		goto out;
+	}
+	binder_debug(BINDER_DEBUG_READ_WRITE,
+		     "%d:%d write %lld at %016llx, read %lld at %016llx\n",
+		     proc->pid, thread->pid,
+		     (u64)bwr.write_size, (u64)bwr.write_buffer,
+		     (u64)bwr.read_size, (u64)bwr.read_buffer);
+
+	if (bwr.write_size > 0) {
+		ret = binder_thread_write(proc, thread,
+					  bwr.write_buffer,
+					  bwr.write_size,
+					  &bwr.write_consumed);
+		trace_binder_write_done(ret);
+		if (ret < 0) {
+			bwr.read_consumed = 0;
+			if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
+				ret = -EFAULT;
+			goto out;
+		}
+	}
+	if (bwr.read_size > 0) {
+		ret = binder_thread_read(proc, thread, bwr.read_buffer,
+					 bwr.read_size,
+					 &bwr.read_consumed,
+					 filp->f_flags & O_NONBLOCK);
+		trace_binder_read_done(ret);
+		if (!list_empty(&proc->todo))
+			wake_up_interruptible(&proc->wait);
+		if (ret < 0) {
+			if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
+				ret = -EFAULT;
+			goto out;
+		}
+	}
+	binder_debug(BINDER_DEBUG_READ_WRITE,
+		     "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
+		     proc->pid, thread->pid,
+		     (u64)bwr.write_consumed, (u64)bwr.write_size,
+		     (u64)bwr.read_consumed, (u64)bwr.read_size);
+	if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
+		ret = -EFAULT;
+		goto out;
+	}
+out:
+	return ret;
+}
+
+static int binder_ioctl_set_ctx_mgr(struct file *filp)
+{
+	int ret = 0;
+	struct binder_proc *proc = filp->private_data;
+	kuid_t curr_euid = current_euid();
+
+	if (binder_context_mgr_node != NULL) {
+		pr_err("BINDER_SET_CONTEXT_MGR already set\n");
+		ret = -EBUSY;
+		goto out;
+	}
+	if (uid_valid(binder_context_mgr_uid)) {
+		if (!uid_eq(binder_context_mgr_uid, curr_euid)) {
+			pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
+			       from_kuid(&init_user_ns, curr_euid),
+			       from_kuid(&init_user_ns,
+					binder_context_mgr_uid));
+			ret = -EPERM;
+			goto out;
+		}
+	} else {
+		binder_context_mgr_uid = curr_euid;
+	}
+	binder_context_mgr_node = binder_new_node(proc, 0, 0);
+	if (binder_context_mgr_node == NULL) {
+		ret = -ENOMEM;
+		goto out;
+	}
+	binder_context_mgr_node->local_weak_refs++;
+	binder_context_mgr_node->local_strong_refs++;
+	binder_context_mgr_node->has_strong_ref = 1;
+	binder_context_mgr_node->has_weak_ref = 1;
+out:
+	return ret;
+}
+
 static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 {
 	int ret;
@@ -2601,9 +2701,9 @@ static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 	struct binder_thread *thread;
 	unsigned int size = _IOC_SIZE(cmd);
 	void __user *ubuf = (void __user *)arg;
-	kuid_t curr_euid = current_euid();
 
-	/*pr_info("binder_ioctl: %d:%d %x %lx\n", proc->pid, current->pid, cmd, arg);*/
+	/*pr_info("binder_ioctl: %d:%d %x %lx\n",
+			proc->pid, current->pid, cmd, arg);*/
 
 	trace_binder_ioctl(cmd, arg);
 
@@ -2619,61 +2719,11 @@ static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 	}
 
 	switch (cmd) {
-	case BINDER_WRITE_READ: {
-		struct binder_write_read bwr;
-
-		if (size != sizeof(struct binder_write_read)) {
-			ret = -EINVAL;
+	case BINDER_WRITE_READ:
+		ret = binder_ioctl_write_read(filp, cmd, arg, thread);
+		if (ret)
 			goto err;
-		}
-		if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
-			ret = -EFAULT;
-			goto err;
-		}
-		binder_debug(BINDER_DEBUG_READ_WRITE,
-			     "%d:%d write %lld at %016llx, read %lld at %016llx\n",
-			     proc->pid, thread->pid,
-			     (u64)bwr.write_size, (u64)bwr.write_buffer,
-			     (u64)bwr.read_size, (u64)bwr.read_buffer);
-
-		if (bwr.write_size > 0) {
-			ret = binder_thread_write(proc, thread,
-						  bwr.write_buffer,
-						  bwr.write_size,
-						  &bwr.write_consumed);
-			trace_binder_write_done(ret);
-			if (ret < 0) {
-				bwr.read_consumed = 0;
-				if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
-					ret = -EFAULT;
-				goto err;
-			}
-		}
-		if (bwr.read_size > 0) {
-			ret = binder_thread_read(proc, thread, bwr.read_buffer,
-						 bwr.read_size,
-						 &bwr.read_consumed,
-						 filp->f_flags & O_NONBLOCK);
-			trace_binder_read_done(ret);
-			if (!list_empty(&proc->todo))
-				wake_up_interruptible(&proc->wait);
-			if (ret < 0) {
-				if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
-					ret = -EFAULT;
-				goto err;
-			}
-		}
-		binder_debug(BINDER_DEBUG_READ_WRITE,
-			     "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
-			     proc->pid, thread->pid,
-			     (u64)bwr.write_consumed, (u64)bwr.write_size,
-			     (u64)bwr.read_consumed, (u64)bwr.read_size);
-		if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
-			ret = -EFAULT;
-			goto err;
-		}
 		break;
-	}
 	case BINDER_SET_MAX_THREADS:
 		if (copy_from_user(&proc->max_threads, ubuf, sizeof(proc->max_threads))) {
 			ret = -EINVAL;
@@ -2681,31 +2731,9 @@ static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 		}
 		break;
 	case BINDER_SET_CONTEXT_MGR:
-		if (binder_context_mgr_node != NULL) {
-			pr_err("BINDER_SET_CONTEXT_MGR already set\n");
-			ret = -EBUSY;
+		ret = binder_ioctl_set_ctx_mgr(filp);
+		if (ret)
 			goto err;
-		}
-		if (uid_valid(binder_context_mgr_uid)) {
-			if (!uid_eq(binder_context_mgr_uid, curr_euid)) {
-				pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
-				       from_kuid(&init_user_ns, curr_euid),
-				       from_kuid(&init_user_ns, binder_context_mgr_uid));
-				ret = -EPERM;
-				goto err;
-			}
-		} else {
-			binder_context_mgr_uid = curr_euid;
-		}
-		binder_context_mgr_node = binder_new_node(proc, 0, 0);
-		if (binder_context_mgr_node == NULL) {
-			ret = -ENOMEM;
-			goto err;
-		}
-		binder_context_mgr_node->local_weak_refs++;
-		binder_context_mgr_node->local_strong_refs++;
-		binder_context_mgr_node->has_strong_ref = 1;
-		binder_context_mgr_node->has_weak_ref = 1;
 		break;
 	case BINDER_THREAD_EXIT:
 		binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
-- 
1.9.1



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] staging: android: binder.c: binder_ioctl() cleanup
  2014-06-03 19:27 [PATCH] staging: android: binder.c: binder_ioctl() cleanup Tair Rzayev
@ 2014-06-04  6:22 ` Brian Swetland
  0 siblings, 0 replies; 2+ messages in thread
From: Brian Swetland @ 2014-06-04  6:22 UTC (permalink / raw)
  To: Tair Rzayev, Arve Hjønnevåg; +Cc: Greg KH, linux-kernel

Arve's probably the best person to loop in on binder driver patches
(considering he wrote it).

Brian

On Tue, Jun 3, 2014 at 12:27 PM, Tair Rzayev <tair.rzayev@gmail.com> wrote:
> binder_ioctl() is quite huge and checkpatch dirty - mostly because of
> the amount of code for the BINDER_WRITE_READ and BINDER_SET_CONTEXT_MGR.
> Moved that code into the new binder_ioctl_write_read() and
> binder_ioctl_set_ctx_mgr()
>
> Signed-off-by: Tair Rzayev <tair.rzayev@gmail.com>
> ---
>  drivers/staging/android/binder.c | 186 ++++++++++++++++++++++-----------------
>  1 file changed, 107 insertions(+), 79 deletions(-)
>
> diff --git a/drivers/staging/android/binder.c b/drivers/staging/android/binder.c
> index 989f809..ed432e3 100644
> --- a/drivers/staging/android/binder.c
> +++ b/drivers/staging/android/binder.c
> @@ -2594,6 +2594,106 @@ static unsigned int binder_poll(struct file *filp,
>         return 0;
>  }
>
> +static int binder_ioctl_write_read(struct file *filp,
> +                               unsigned int cmd, unsigned long arg,
> +                               struct binder_thread *thread)
> +{
> +       int ret = 0;
> +       struct binder_proc *proc = filp->private_data;
> +       unsigned int size = _IOC_SIZE(cmd);
> +       void __user *ubuf = (void __user *)arg;
> +       struct binder_write_read bwr;
> +
> +       if (size != sizeof(struct binder_write_read)) {
> +               ret = -EINVAL;
> +               goto out;
> +       }
> +       if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
> +               ret = -EFAULT;
> +               goto out;
> +       }
> +       binder_debug(BINDER_DEBUG_READ_WRITE,
> +                    "%d:%d write %lld at %016llx, read %lld at %016llx\n",
> +                    proc->pid, thread->pid,
> +                    (u64)bwr.write_size, (u64)bwr.write_buffer,
> +                    (u64)bwr.read_size, (u64)bwr.read_buffer);
> +
> +       if (bwr.write_size > 0) {
> +               ret = binder_thread_write(proc, thread,
> +                                         bwr.write_buffer,
> +                                         bwr.write_size,
> +                                         &bwr.write_consumed);
> +               trace_binder_write_done(ret);
> +               if (ret < 0) {
> +                       bwr.read_consumed = 0;
> +                       if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
> +                               ret = -EFAULT;
> +                       goto out;
> +               }
> +       }
> +       if (bwr.read_size > 0) {
> +               ret = binder_thread_read(proc, thread, bwr.read_buffer,
> +                                        bwr.read_size,
> +                                        &bwr.read_consumed,
> +                                        filp->f_flags & O_NONBLOCK);
> +               trace_binder_read_done(ret);
> +               if (!list_empty(&proc->todo))
> +                       wake_up_interruptible(&proc->wait);
> +               if (ret < 0) {
> +                       if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
> +                               ret = -EFAULT;
> +                       goto out;
> +               }
> +       }
> +       binder_debug(BINDER_DEBUG_READ_WRITE,
> +                    "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
> +                    proc->pid, thread->pid,
> +                    (u64)bwr.write_consumed, (u64)bwr.write_size,
> +                    (u64)bwr.read_consumed, (u64)bwr.read_size);
> +       if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
> +               ret = -EFAULT;
> +               goto out;
> +       }
> +out:
> +       return ret;
> +}
> +
> +static int binder_ioctl_set_ctx_mgr(struct file *filp)
> +{
> +       int ret = 0;
> +       struct binder_proc *proc = filp->private_data;
> +       kuid_t curr_euid = current_euid();
> +
> +       if (binder_context_mgr_node != NULL) {
> +               pr_err("BINDER_SET_CONTEXT_MGR already set\n");
> +               ret = -EBUSY;
> +               goto out;
> +       }
> +       if (uid_valid(binder_context_mgr_uid)) {
> +               if (!uid_eq(binder_context_mgr_uid, curr_euid)) {
> +                       pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
> +                              from_kuid(&init_user_ns, curr_euid),
> +                              from_kuid(&init_user_ns,
> +                                       binder_context_mgr_uid));
> +                       ret = -EPERM;
> +                       goto out;
> +               }
> +       } else {
> +               binder_context_mgr_uid = curr_euid;
> +       }
> +       binder_context_mgr_node = binder_new_node(proc, 0, 0);
> +       if (binder_context_mgr_node == NULL) {
> +               ret = -ENOMEM;
> +               goto out;
> +       }
> +       binder_context_mgr_node->local_weak_refs++;
> +       binder_context_mgr_node->local_strong_refs++;
> +       binder_context_mgr_node->has_strong_ref = 1;
> +       binder_context_mgr_node->has_weak_ref = 1;
> +out:
> +       return ret;
> +}
> +
>  static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
>  {
>         int ret;
> @@ -2601,9 +2701,9 @@ static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
>         struct binder_thread *thread;
>         unsigned int size = _IOC_SIZE(cmd);
>         void __user *ubuf = (void __user *)arg;
> -       kuid_t curr_euid = current_euid();
>
> -       /*pr_info("binder_ioctl: %d:%d %x %lx\n", proc->pid, current->pid, cmd, arg);*/
> +       /*pr_info("binder_ioctl: %d:%d %x %lx\n",
> +                       proc->pid, current->pid, cmd, arg);*/
>
>         trace_binder_ioctl(cmd, arg);
>
> @@ -2619,61 +2719,11 @@ static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
>         }
>
>         switch (cmd) {
> -       case BINDER_WRITE_READ: {
> -               struct binder_write_read bwr;
> -
> -               if (size != sizeof(struct binder_write_read)) {
> -                       ret = -EINVAL;
> +       case BINDER_WRITE_READ:
> +               ret = binder_ioctl_write_read(filp, cmd, arg, thread);
> +               if (ret)
>                         goto err;
> -               }
> -               if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
> -                       ret = -EFAULT;
> -                       goto err;
> -               }
> -               binder_debug(BINDER_DEBUG_READ_WRITE,
> -                            "%d:%d write %lld at %016llx, read %lld at %016llx\n",
> -                            proc->pid, thread->pid,
> -                            (u64)bwr.write_size, (u64)bwr.write_buffer,
> -                            (u64)bwr.read_size, (u64)bwr.read_buffer);
> -
> -               if (bwr.write_size > 0) {
> -                       ret = binder_thread_write(proc, thread,
> -                                                 bwr.write_buffer,
> -                                                 bwr.write_size,
> -                                                 &bwr.write_consumed);
> -                       trace_binder_write_done(ret);
> -                       if (ret < 0) {
> -                               bwr.read_consumed = 0;
> -                               if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
> -                                       ret = -EFAULT;
> -                               goto err;
> -                       }
> -               }
> -               if (bwr.read_size > 0) {
> -                       ret = binder_thread_read(proc, thread, bwr.read_buffer,
> -                                                bwr.read_size,
> -                                                &bwr.read_consumed,
> -                                                filp->f_flags & O_NONBLOCK);
> -                       trace_binder_read_done(ret);
> -                       if (!list_empty(&proc->todo))
> -                               wake_up_interruptible(&proc->wait);
> -                       if (ret < 0) {
> -                               if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
> -                                       ret = -EFAULT;
> -                               goto err;
> -                       }
> -               }
> -               binder_debug(BINDER_DEBUG_READ_WRITE,
> -                            "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
> -                            proc->pid, thread->pid,
> -                            (u64)bwr.write_consumed, (u64)bwr.write_size,
> -                            (u64)bwr.read_consumed, (u64)bwr.read_size);
> -               if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
> -                       ret = -EFAULT;
> -                       goto err;
> -               }
>                 break;
> -       }
>         case BINDER_SET_MAX_THREADS:
>                 if (copy_from_user(&proc->max_threads, ubuf, sizeof(proc->max_threads))) {
>                         ret = -EINVAL;
> @@ -2681,31 +2731,9 @@ static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
>                 }
>                 break;
>         case BINDER_SET_CONTEXT_MGR:
> -               if (binder_context_mgr_node != NULL) {
> -                       pr_err("BINDER_SET_CONTEXT_MGR already set\n");
> -                       ret = -EBUSY;
> +               ret = binder_ioctl_set_ctx_mgr(filp);
> +               if (ret)
>                         goto err;
> -               }
> -               if (uid_valid(binder_context_mgr_uid)) {
> -                       if (!uid_eq(binder_context_mgr_uid, curr_euid)) {
> -                               pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
> -                                      from_kuid(&init_user_ns, curr_euid),
> -                                      from_kuid(&init_user_ns, binder_context_mgr_uid));
> -                               ret = -EPERM;
> -                               goto err;
> -                       }
> -               } else {
> -                       binder_context_mgr_uid = curr_euid;
> -               }
> -               binder_context_mgr_node = binder_new_node(proc, 0, 0);
> -               if (binder_context_mgr_node == NULL) {
> -                       ret = -ENOMEM;
> -                       goto err;
> -               }
> -               binder_context_mgr_node->local_weak_refs++;
> -               binder_context_mgr_node->local_strong_refs++;
> -               binder_context_mgr_node->has_strong_ref = 1;
> -               binder_context_mgr_node->has_weak_ref = 1;
>                 break;
>         case BINDER_THREAD_EXIT:
>                 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
> --
> 1.9.1
>
>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2014-06-04  6:22 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-06-03 19:27 [PATCH] staging: android: binder.c: binder_ioctl() cleanup Tair Rzayev
2014-06-04  6:22 ` Brian Swetland

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox