linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Cyrill Gorcunov <gorcunov@openvz.org>
To: Oleg Nesterov <oleg@redhat.com>,
	Vasiliy Kulikov <segoon@openwall.com>,
	Andrew Morton <akpm@linux-foundation.org>
Cc: "Eric W. Biederman" <ebiederm@xmission.com>,
	Pavel Emelyanov <xemul@parallels.com>,
	Andrey Vagin <avagin@openvz.org>,
	KOSAKI Motohiro <kosaki.motohiro@gmail.com>,
	Ingo Molnar <mingo@elte.hu>, "H. Peter Anvin" <hpa@zytor.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	Glauber Costa <glommer@parallels.com>,
	Andi Kleen <andi@firstfloor.org>, Tejun Heo <tj@kernel.org>,
	Matt Helsley <matthltc@us.ibm.com>,
	Pekka Enberg <penberg@kernel.org>,
	Eric Dumazet <eric.dumazet@gmail.com>,
	Alexey Dobriyan <adobriyan@gmail.com>,
	Valdis.Kletnieks@vt.edu, Michal Marek <mmarek@suse.cz>,
	Frederic Weisbecker <fweisbec@gmail.com>,
	linux-kernel@vger.kernel.org
Subject: Re: + syscalls-x86-add-__nr_kcmp-syscall-v8.patch added to -mm tree
Date: Thu, 16 Feb 2012 20:49:01 +0400	[thread overview]
Message-ID: <20120216164901.GD3849@moon> (raw)
In-Reply-To: <20120216151340.GI1905@moon>

On Thu, Feb 16, 2012 at 07:13:40PM +0400, Cyrill Gorcunov wrote:
...
> 
> > You could simply do
> > 	int mutex_double_lock_killable(struct mutex *m1, struct mutex *m2)
> > 	{
> > 		int err;
> > 
> > 		if (m2 > m1)
> > 			swap(m1, m2);
> > 
> > 		err = mutex_lock_killable(m1);
> > 
> > 		if (!err && likely(m1 != m2)) {
> > 			err = mutex_lock_killable_nested(m2);
> > 			if (err)
> > 				mutex_unlock(m1);
> > 		}
> > 
> > 		return err;
> > 	}
> > 
> > but I won't insist.
> 
> Initially I wanted kcmp would be brining minimum impact
> and if mutex is already taken by someone, we would not sleep
> but return immediately with -EBUSY and it would be up to caller
> to deside if to try again or make some delay first. I simply
> not sure what is better here.
> 
This one should do the trick.

	Cyrill
---
From: Cyrill Gorcunov <gorcunov@openvz.org>
Subject: syscalls, x86: Make __NR_kcmp to work with equivalent pids

In case if pid1 is equal to pid2 the kcmp will return -EBUSY,
which makes no sence. Make it able to work with equivalent pids.
Selftest is extended as well.

Repored-by: Oleg Nesterov <oleg@redhat.com>
Signed-off-by: Cyrill Gorcunov <gorcunov@openvz.org>
---
diff -u linux-2.6.git/kernel/kcmp.c linux-2.6.git/kernel/kcmp.c
--- linux-2.6.git/kernel/kcmp.c
+++ linux-2.6.git/kernel/kcmp.c
@@ -58,22 +58,31 @@
 	return file;
 }
 
-static void access_unlock(struct task_struct *task)
+static void kcmp_unlock(struct mutex *m1, struct mutex *m2)
 {
-	mutex_unlock(&task->signal->cred_guard_mutex);
+	if (m2 > m1)
+		swap(m1, m2);
+
+	if (likely(m2 != m1))
+		mutex_unlock(m2);
+	mutex_unlock(m1);
 }
 
-static int access_trylock(struct task_struct *task)
+static int kcmp_lock(struct mutex *m1, struct mutex *m2)
 {
-	if (!mutex_trylock(&task->signal->cred_guard_mutex))
-		return -EBUSY;
+	int err;
+
+	if (m2 > m1)
+		swap(m1, m2);
 
-	if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
-		mutex_unlock(&task->signal->cred_guard_mutex);
-		return -EPERM;
+	err = mutex_lock_killable(m1);
+	if (!err && likely(m1 != m2)) {
+		err = mutex_lock_killable_nested(m2, SINGLE_DEPTH_NESTING);
+		if (err)
+			mutex_unlock(m1);
 	}
 
-	return 0;
+	return err;
 }
 
 SYSCALL_DEFINE5(kcmp, pid_t, pid1, pid_t, pid2, int, type,
@@ -100,12 +109,15 @@
 	/*
 	 * One should have enough rights to inspect task details.
 	 */
-	ret = access_trylock(task1);
+	ret = kcmp_lock(&task1->signal->cred_guard_mutex,
+			&task2->signal->cred_guard_mutex);
 	if (ret)
 		goto err;
-	ret = access_trylock(task2);
-	if (ret)
+	if (!ptrace_may_access(task1, PTRACE_MODE_READ) ||
+	    !ptrace_may_access(task2, PTRACE_MODE_READ)) {
+		ret = -EPERM;
 		goto err_unlock;
+	}
 
 	switch (type) {
 	case KCMP_FILE: {
@@ -149,9 +161,9 @@
 		break;
 	}
 
-	access_unlock(task2);
 err_unlock:
-	access_unlock(task1);
+	kcmp_unlock(&task1->signal->cred_guard_mutex,
+		    &task2->signal->cred_guard_mutex);
 err:
 	put_task_struct(task1);
 	put_task_struct(task2);
diff -u linux-2.6.git/tools/testing/selftests/kcmp/kcmp_test.c linux-2.6.git/tools/testing/selftests/kcmp/kcmp_test.c
--- linux-2.6.git/tools/testing/selftests/kcmp/kcmp_test.c
+++ linux-2.6.git/tools/testing/selftests/kcmp/kcmp_test.c
@@ -74,6 +74,15 @@
 			ret = -1;
 		} else
 			printf("PASS: 0 returned as expected\n");
+
+		/* Compare with self */
+		ret = sys_kcmp(pid1, pid1, KCMP_VM, 0, 0);
+		if (ret) {
+			printf("FAIL: 0 expected but %li returned\n", ret);
+			ret = -1;
+		} else
+			printf("PASS: 0 returned as expected\n");
+
 		exit(ret);
 	}
 

  reply	other threads:[~2012-02-16 16:49 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-15 14:36 + syscalls-x86-add-__nr_kcmp-syscall-v8.patch added to -mm tree Oleg Nesterov
2012-02-15 15:10 ` Cyrill Gorcunov
2012-02-15 15:38   ` Oleg Nesterov
2012-02-15 16:13     ` Cyrill Gorcunov
2012-02-15 16:22       ` Oleg Nesterov
2012-02-15 17:53         ` Cyrill Gorcunov
2012-02-15 18:43           ` Oleg Nesterov
2012-02-15 19:56             ` Cyrill Gorcunov
2012-02-15 19:57               ` Vasiliy Kulikov
2012-02-15 20:05                 ` Cyrill Gorcunov
2012-02-15 20:25                   ` Cyrill Gorcunov
2012-02-15 21:09                     ` Cyrill Gorcunov
2012-02-15 21:58                       ` Cyrill Gorcunov
2012-02-16 14:49                         ` Oleg Nesterov
2012-02-16 15:13                           ` Cyrill Gorcunov
2012-02-16 16:49                             ` Cyrill Gorcunov [this message]
2012-02-16 17:40                               ` Oleg Nesterov
2012-02-16 17:58                                 ` Cyrill Gorcunov
2012-02-16 19:03                                   ` Oleg Nesterov
2012-02-16 19:20                                     ` H. Peter Anvin
2012-02-16 19:29                                     ` Cyrill Gorcunov
2012-02-16 19:52                                       ` Andrew Morton
2012-02-16 20:01                                         ` Cyrill Gorcunov
2012-02-16 18:21                               ` Vasiliy Kulikov
2012-02-16 18:34                                 ` Cyrill Gorcunov
2012-02-16 18:33                                   ` Vasiliy Kulikov
2012-02-16 18:49                                 ` Oleg Nesterov
2012-02-15 18:32   ` Cyrill Gorcunov
2012-02-15 19:06     ` Oleg Nesterov
2012-02-15 19:18       ` Cyrill Gorcunov
2012-02-15 16:06 ` Oleg Nesterov
2012-02-15 16:27   ` Cyrill Gorcunov
2012-04-09 22:10     ` Andrew Morton
2012-04-09 22:24       ` Cyrill Gorcunov
2012-04-09 23:22         ` H. Peter Anvin
2012-04-10 22:37           ` Cyrill Gorcunov
2012-04-10 22:39             ` H. Peter Anvin
2012-04-10 22:48               ` Cyrill Gorcunov
2012-04-10 23:08             ` Oleg Nesterov
2012-04-10 23:32               ` H. Peter Anvin
2012-04-10 23:42                 ` Oleg Nesterov
2012-04-11  6:39                   ` Cyrill Gorcunov
2012-04-11 18:31                     ` Oleg Nesterov
2012-04-11  0:02           ` Valdis.Kletnieks
2012-04-10  3:25       ` Eric W. Biederman
2012-04-10 22:54         ` Cyrill Gorcunov
2012-04-10 23:58       ` Valdis.Kletnieks
2012-04-11  0:06         ` H. Peter Anvin

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=20120216164901.GD3849@moon \
    --to=gorcunov@openvz.org \
    --cc=Valdis.Kletnieks@vt.edu \
    --cc=adobriyan@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=andi@firstfloor.org \
    --cc=avagin@openvz.org \
    --cc=ebiederm@xmission.com \
    --cc=eric.dumazet@gmail.com \
    --cc=fweisbec@gmail.com \
    --cc=glommer@parallels.com \
    --cc=hpa@zytor.com \
    --cc=kosaki.motohiro@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=matthltc@us.ibm.com \
    --cc=mingo@elte.hu \
    --cc=mmarek@suse.cz \
    --cc=oleg@redhat.com \
    --cc=penberg@kernel.org \
    --cc=segoon@openwall.com \
    --cc=tglx@linutronix.de \
    --cc=tj@kernel.org \
    --cc=xemul@parallels.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).