mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Waiman Long <longman@redhat.com>
To: David Laight <David.Laight@ACULAB.COM>, Jann Horn <jannh@google.com>
Cc: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>, Will Deacon <will@kernel.org>,
	Jonathan Corbet <corbet@lwn.net>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"linux-doc@vger.kernel.org" <linux-doc@vger.kernel.org>
Subject: Re: [PATCH] locking: Document that mutex_unlock() is non-atomic
Date: Sat, 2 Dec 2023 17:39:36 -0500	[thread overview]
Message-ID: <c7c62df7-74bb-416f-b363-274c96b88ae0@redhat.com> (raw)
In-Reply-To: <f98b04e05c964b57bc27fb895810aaaf@AcuMS.aculab.com>

On 12/2/23 10:51, David Laight wrote:
> From: Waiman Long
>> Sent: 01 December 2023 19:16
>>
>> On 12/1/23 13:44, David Laight wrote:
>>> Pending waiters aren't the problem.
>>>
>> Pending waiters can still be a problem if code decides to free the lock
>> containing object after a lock/unlock sequence as it may cause
>> use-after-free.
>>> You have to ensure there aren't any, but the mutex() can be held.
>>>
>> Using reference count to track the number of active users is one way to
>> prevent that if you only release the reference count after
>> mutex_unlock() returns but not in the lock critical section.
> I suspect the documentation need to be more explicit than just saying
> it is non-atomic.
> Saying something like:
>
> The mutex structure may be accessed by mutex_unlock() after another
> thread has locked and unlocked the mutex.
>
> So if a reference count is used to ensure a structure remains valid when
> a lock is released (with the item being freed when the count becomes zero)
> the reference count itself cannot be protected by a mutex in the structure.
> So code like:
> 	...
> 	count = --item->refcount;
> 	mutex_unlock(item->mtx);
> 	if (!count)
> 		free(item);
> can lead to a 'use after free' in mutex_unlock().
> However if the refcount is atomic and decremented without the
> mutex held there isn't a problem.
>
> 	David

That is definitely better than saying it is non-atomic which is vague in 
meaning.

Cheers,
Longman


  reply	other threads:[~2023-12-02 22:39 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-30 20:48 Jann Horn
2023-11-30 21:53 ` Waiman Long
2023-11-30 22:24   ` Jann Horn
2023-11-30 23:56     ` Waiman Long
2023-12-01 10:33     ` [PATCH -v2] locking/mutex: " Ingo Molnar
2023-12-02  1:37       ` Bagas Sanjaya
2023-12-01 10:20   ` [PATCH] locking: " Ingo Molnar
2023-12-01  0:33 ` Waiman Long
2023-12-01 15:01   ` Jann Horn
     [not found]     ` <a9e19ad0-9a27-4885-a6ac-bebd3e997b02@redhat.com>
2023-12-01 16:03       ` Jann Horn
2023-12-01 18:12     ` David Laight
2023-12-01 18:18       ` Jann Horn
     [not found]         ` <1bcee696-d751-413c-a2ec-4a8480bae00b@redhat.com>
     [not found]           ` <780e652ff52044d4a213cacbd9276cf8@AcuMS.aculab.com>
2023-12-01 19:15             ` Waiman Long
2023-12-02 15:51               ` David Laight
2023-12-02 22:39                 ` Waiman Long [this message]
2023-12-01  9:10 ` Peter Zijlstra
2023-12-01 15:58   ` Jann Horn
2023-12-01 10:44 ` [tip: locking/core] locking/mutex: " tip-bot2 for Jann Horn
2023-12-01 12:18   ` Peter Zijlstra
2024-01-08  8:45     ` [PATCH] locking/mutex: Clarify that mutex_unlock(), and most other sleeping locks, cannot be used to reference-count objects Ingo Molnar
2024-01-08  9:00       ` [PATCH -v2] locking/mutex: Clarify that mutex_unlock(), and most other sleeping locks, can still use the lock object after it's unlocked Ingo Molnar
2024-01-08 15:28       ` [PATCH] locking/mutex: Clarify that mutex_unlock(), and most other sleeping locks, cannot be used to reference-count objects Jann Horn
2024-01-08  9:01     ` [tip: locking/core] locking/mutex: Clarify that mutex_unlock(), and most other sleeping locks, can still use the lock object after it's unlocked tip-bot2 for Ingo Molnar

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=c7c62df7-74bb-416f-b363-274c96b88ae0@redhat.com \
    --to=longman@redhat.com \
    --cc=David.Laight@ACULAB.COM \
    --cc=corbet@lwn.net \
    --cc=jannh@google.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®