All of lore.kernel.org
 help / color / mirror / Atom feed
From: ebiederm@xmission.com (Eric W. Biederman)
To: Bernd Edlinger <bernd.edlinger@hotmail.de>
Cc: linux-kernel@vger.kernel.org,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>, Will Deacon <will@kernel.org>,
	Jann Horn <jannh@google.com>,
	Vasiliy Kulikov <segoon@openwall.com>,
	Al Viro <viro@zeniv.linux.org.uk>,
	Oleg Nesterov <oleg@redhat.com>,
	Christopher Yeoh <cyeoh@au1.ibm.com>,
	Cyrill Gorcunov <gorcunov@gmail.com>,
	Sargun Dhillon <sargun@sargun.me>,
	Christian Brauner <christian.brauner@ubuntu.com>,
	Arnd Bergmann <arnd@arndb.de>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Waiman Long <longman@redhat.com>,
	Davidlohr Bueso <dave@stgolabs.net>
Subject: Re: [PATCH 3/3] exec: Transform exec_update_mutex into a rw_semaphore
Date: Fri, 04 Dec 2020 11:39:11 -0600	[thread overview]
Message-ID: <87y2id8o8w.fsf@x220.int.ebiederm.org> (raw)
In-Reply-To: <AM6PR03MB5170412C2B0318C40CED55E5E4F10@AM6PR03MB5170.eurprd03.prod.outlook.com> (Bernd Edlinger's message of "Fri, 4 Dec 2020 17:08:01 +0100")

Bernd Edlinger <bernd.edlinger@hotmail.de> writes:

> Hi Eric,
>
> I think I remembered from a previous discussion about this topic,
> that it was unclear if the rw_semaphores are working the same
> in RT-Linux.  Will this fix work in RT as well?

The locks should work close enough to the same that correct code is also
correct code on RT-linux.  If not it is an RT-linux bug.

An rw_semaphore may be less than optimal on RT-linux.  I do remember
that mutexes are prefered.  But this change is more about correctness
than anything else.

> On 12/3/20 9:12 PM, Eric W. Biederman wrote:
>> --- a/kernel/kcmp.c
>> +++ b/kernel/kcmp.c
>> @@ -70,25 +70,25 @@ get_file_raw_ptr(struct task_struct *task, unsigned int idx)
>>  	return file;
>>  }
>>  
>> -static void kcmp_unlock(struct mutex *m1, struct mutex *m2)
>> +static void kcmp_unlock(struct rw_semaphore *l1, struct rw_semaphore *l2)
>>  {
>> -	if (likely(m2 != m1))
>> -		mutex_unlock(m2);
>> -	mutex_unlock(m1);
>> +	if (likely(l2 != l1))
>
> is this still necessary ?

Yes.  Both pids could be threads of the same process or even the same
value so yes this is definitely necessary.  rw_semaphores don't nest on
the same cpu.

>
>> +		up_read(l2);
>> +	up_read(l1);
>>  }
>>  
>> -static int kcmp_lock(struct mutex *m1, struct mutex *m2)
>> +static int kcmp_lock(struct rw_semaphore *l1, struct rw_semaphore *l2)
>>  {
>>  	int err;
>>  
>> -	if (m2 > m1)
>> -		swap(m1, m2);
>> +	if (l2 > l1)
>> +		swap(l1, l2);
>
> and this is probably also no longer necessary?

I think lockdep needs this, so it can be certain the same rw_semaphore
is not nesting on the cpu.   Otherwise we will have inconsitencies about
which is the nested lock.  It won't matter in practice, but I am not
certain lockdep knows enough to tell the difference.

If anything removing the swap is a candidate for a follow up patch
where it can be considered separately from other concerns.  For this
patch keeping the logic unchanged makes it trivial to verify that
the conversion from one lock to another is correct.

>>  
>> -	err = mutex_lock_killable(m1);
>> -	if (!err && likely(m1 != m2)) {
>> -		err = mutex_lock_killable_nested(m2, SINGLE_DEPTH_NESTING);
>> +	err = down_read_killable(l1);
>> +	if (!err && likely(l1 != l2)) {
>
> and this can now be unconditionally, right?

Nope.  The two locks can be the same lock, and they don't nest on a
single cpu.  I tested and verified that lockdep complains bitterly
if down_read_killable_nested is replaced with a simple
down_read_killable.


>> +		err = down_read_killable_nested(l2, SINGLE_DEPTH_NESTING);
>>  		if (err)
>> -			mutex_unlock(m1);
>> +			up_read(l1);
>>  	}
>>  
>>  	return err;
>> @@ -156,8 +156,8 @@ SYSCALL_DEFINE5(kcmp, pid_t, pid1, pid_t, pid2, int, type,
>>  	/*
>>  	 * One should have enough rights to inspect task details.
>>  	 */
>> -	ret = kcmp_lock(&task1->signal->exec_update_mutex,
>> -			&task2->signal->exec_update_mutex);
>> +	ret = kcmp_lock(&task1->signal->exec_update_lock,
>> +			&task2->signal->exec_update_lock);
>>  	if (ret)
>>  		goto err;
>>  	if (!ptrace_may_access(task1, PTRACE_MODE_READ_REALCREDS) ||
>> @@ -212,8 +212,8 @@ SYSCALL_DEFINE5(kcmp, pid_t, pid1, pid_t, pid2, int, type,
>>  	}
>>  
>>  err_unlock:
>> -	kcmp_unlock(&task1->signal->exec_update_mutex,
>> -		    &task2->signal->exec_update_mutex);
>> +	kcmp_unlock(&task1->signal->exec_update_lock,
>> +		    &task2->signal->exec_update_lock);
>>  err:
>>  	put_task_struct(task1);
>>  	put_task_struct(task2);
>
>
> Thanks
> Bernd.

  parent reply	other threads:[~2020-12-04 17:40 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-03 20:09 [PATCH 0/3] exec: Transform exec_update_mutex into a rw_semaphore Eric W. Biederman
2020-12-03 20:10 ` [PATCH 1/3] rwsem: Implement down_read_killable_nested Eric W. Biederman
2020-12-04  1:58   ` Waiman Long
2020-12-09 18:38   ` [tip: locking/core] " tip-bot2 for Eric W. Biederman
2020-12-03 20:11 ` [PATCH 2/3] rwsem: Implement down_read_interruptible Eric W. Biederman
2020-12-04  1:59   ` Waiman Long
2020-12-07  9:02     ` Peter Zijlstra
2020-12-07 15:33       ` Waiman Long
2020-12-07 16:58         ` David Laight
2020-12-07 19:02           ` Waiman Long
2020-12-08  9:12             ` David Laight
2020-12-08 12:32               ` Peter Zijlstra
2020-12-08 13:13                 ` David Laight
2020-12-08 15:34               ` Waiman Long
2020-12-08 16:23                 ` David Laight
2020-12-07 15:56       ` Eric W. Biederman
2020-12-08 14:52         ` Peter Zijlstra
2020-12-08 18:27           ` Eric W. Biederman
2020-12-09 18:36             ` Peter Zijlstra
2020-12-10 19:33               ` Eric W. Biederman
2020-12-11  8:16                 ` Peter Zijlstra
2020-12-09 18:38       ` [tip: locking/core] locking/rwsem: Introduce rwsem_write_trylock() tip-bot2 for Peter Zijlstra
2020-12-09 18:38       ` [tip: locking/core] locking/rwsem: Fold __down_{read,write}*() tip-bot2 for Peter Zijlstra
2020-12-09 18:38       ` [tip: locking/core] locking/rwsem: Better collate rwsem_read_trylock() tip-bot2 for Peter Zijlstra
2020-12-09 18:38   ` [tip: locking/core] rwsem: Implement down_read_interruptible tip-bot2 for Eric W. Biederman
2020-12-03 20:12 ` [PATCH 3/3] exec: Transform exec_update_mutex into a rw_semaphore Eric W. Biederman
2020-12-04 16:08   ` Bernd Edlinger
2020-12-04 17:21     ` Linus Torvalds
2020-12-04 19:34       ` Eric W. Biederman
2020-12-04 20:10         ` Linus Torvalds
2020-12-04 20:30           ` Bernd Edlinger
2020-12-04 20:48             ` Linus Torvalds
2020-12-04 21:48               ` Davidlohr Bueso
2020-12-05 18:05                 ` Eric W. Biederman
2020-12-07  9:15                   ` Peter Zijlstra
2020-12-07  9:09               ` Peter Zijlstra
2020-12-07 18:40                 ` Linus Torvalds
2020-12-08  8:34                   ` [PATCH] perf: Break deadlock involving exec_update_mutex Peter Zijlstra
2020-12-08 18:37                     ` Linus Torvalds
2020-12-10 18:38                     ` Davidlohr Bueso
2020-12-10 19:40                       ` Eric W. Biederman
2020-12-05 17:43           ` [PATCH 3/3] exec: Transform exec_update_mutex into a rw_semaphore Eric W. Biederman
2020-12-04 17:39     ` Eric W. Biederman [this message]
2020-12-03 22:42 ` [PATCH 0/3] " Linus Torvalds
2020-12-04  1:56   ` Waiman Long
2020-12-04  4:54   ` Davidlohr Bueso

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=87y2id8o8w.fsf@x220.int.ebiederm.org \
    --to=ebiederm@xmission.com \
    --cc=acme@kernel.org \
    --cc=arnd@arndb.de \
    --cc=bernd.edlinger@hotmail.de \
    --cc=christian.brauner@ubuntu.com \
    --cc=cyeoh@au1.ibm.com \
    --cc=dave@stgolabs.net \
    --cc=gorcunov@gmail.com \
    --cc=jannh@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=longman@redhat.com \
    --cc=mingo@redhat.com \
    --cc=oleg@redhat.com \
    --cc=peterz@infradead.org \
    --cc=sargun@sargun.me \
    --cc=segoon@openwall.com \
    --cc=torvalds@linux-foundation.org \
    --cc=viro@zeniv.linux.org.uk \
    --cc=will@kernel.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.