mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Onur Özkan" <work@onurozkan.dev>
To: Daniel Almeida <daniel.almeida@collabora.com>
Cc: rust-for-linux@vger.kernel.org, lossin@kernel.org,
	lyude@redhat.com, ojeda@kernel.org, alex.gaynor@gmail.com,
	boqun.feng@gmail.com, gary@garyguo.net, a.hindborg@kernel.org,
	aliceryhl@google.com, tmgross@umich.edu, dakr@kernel.org,
	peterz@infradead.org, mingo@redhat.com, will@kernel.org,
	longman@redhat.com, felipe_life@live.com, daniel@sedlak.dev,
	thomas.hellstrom@linux.intel.com, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v10 6/7] rust: ww_mutex: implement LockSet
Date: Sun, 11 Oct 2026 18:26:20 +0300	[thread overview]
Message-ID: <20261011152630.117725-1-work@onurozkan.dev> (raw)
In-Reply-To: <A11C8B1A-00A3-49C9-B9B5-30A55AFDDF2B@collabora.com>

On Wed, 30 Sep 2026 19:10:16 -0300
Daniel Almeida <daniel.almeida@collabora.com> wrote:

> Hi Onur,
> 
> The bot has caught it already, but remember to compile with CLIPPY=1 for
> v11.
> 
> If push() fails, two things drop:
> 
> > +    /// Attempts to lock the given [`Mutex`] and stores a guard for it.
> > +    pub fn lock<T>(&mut self, mutex: &'a Mutex<'a, T>) -> Result {
> > +        let guard = self.acquire_ctx.lock(mutex)?; <--- this guard
> > +
> > +        self.taken.push(
> > +            RawGuard {
> > +                // SAFETY: We just locked it above so it's a valid pointer.
> > +                mutex_ptr: unsafe { NonNull::new_unchecked(guard.mutex.inner.get()) },
> > +                _not_send: NotThreadSafe,
> > +            }, <------- also the raw guard, which drops in push()
> > +            GFP_KERNEL,
> > +        )?;
> > +
> > +        // Avoid unlocking here; `release_all_locks` (also run by `Drop`)
> > +        // performs the unlock for `LockSet`.
> > +        core::mem::forget(guard);
> > +
> > +        Ok(())
> > +    }
> > +
> 
> Both unlock. I suggest that RawGuard consumes Guard on creation, or some
> variation of this.

We can forget the original guard after constructing RawGuard before calling
push(). I think we can keep `RawGuard` as simple as it is.

> 
> 
> When locking, I don't think it makes sense to immediately retry on
> -EDEADLK. This will busy-loop on a lock that will probably still be
> taken. We should release all locks and sleep instead.
> 
> > +    pub fn lock_all<T, Y, Z>(
> > +        &mut self,
> > +        mut locking_algorithm: T,
> > +        mut on_all_locks_taken: Y,
> > +    ) -> Result<Z>
> > +    where
> > +        T: FnMut(&mut LockSet<'a>) -> Result,
> > +        Y: FnMut(&mut LockSet<'a>) -> Result<Z>,
> > +    {
> > +        loop {
> > +            match locking_algorithm(self) {
> > +                Ok(()) => {
> > +                    // All locks in `locking_algorithm` succeeded.
> > +                    // The user can now safely use them in `on_all_locks_taken`.
> > +                    let res = on_all_locks_taken(self);
> > +                    self.release_all_locks();
> > +
> > +                    return res;
> > +                }
> > +                Err(e) if e == EDEADLK => {
> > +                    // Deadlock detected, retry from scratch.
> > +                    self.cleanup_on_deadlock(); 
> > +                    continue; <------ sleep on the contended lock before continuing
> > +                }
> > +                Err(e) => {
> > +                    self.release_all_locks();
> > +                    return Err(e);
> > +                }
> > +            }
> > +        }
> > +    }
> > +
> 
> In order to sleep, we must record the contended lock though, which is
> something that this patch does not do yet. I suggest that we store a
> pointer to it and call from_raw() on that. Also, when acquiring the
> contended lock, we must hold it before retrying, or we will race for it
> again.
> 
> > +    fn cleanup_on_deadlock(&mut self) {
> > +        self.release_all_locks();
> > +
> > +        // SAFETY: We released all the locks just above.
> > +        unsafe { self.acquire_ctx.as_mut().reinit() }; 
> > +    }
> 
> 
> IIUC, calling reinit() will forfeit our ticket and acquire a new one, so
> LockSet breaks the whole premise of wwmutex: when there's contention,
> the older ticket eventually wins. IMHO, remove that line.
> 
> 
> -- Daniel

  reply	other threads:[~2026-10-11 15:26 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-03  7:35 [PATCH v10 0/7] rust: add ww_mutex support Onur Özkan
2026-01-03  7:35 ` [PATCH v10 1/7] rust: add C wrappers for ww_mutex inline functions Onur Özkan
2026-02-03 13:45   ` Daniel Almeida
2026-02-03 15:02     ` Onur Özkan
2026-09-30 20:24   ` Daniel Almeida
2026-01-03  7:35 ` [PATCH v10 2/7] ww_mutex: add ww_class field unconditionally Onur Özkan
2026-09-30 20:26   ` Daniel Almeida
2026-01-03  7:35 ` [PATCH v10 3/7] rust: error: add EDEADLK Onur Özkan
2026-09-30 20:26   ` Daniel Almeida
2026-01-03  7:35 ` [PATCH v10 4/7] rust: implement Class for ww_class support Onur Özkan
2026-09-30 20:27   ` Daniel Almeida
2026-01-03  7:35 ` [PATCH v10 5/7] rust: ww_mutex: add Mutex, AcquireCtx and MutexGuard Onur Özkan
2026-09-30 21:52   ` Daniel Almeida
2026-10-09 13:40     ` Onur Özkan
2026-01-03  7:35 ` [PATCH v10 6/7] rust: ww_mutex: implement LockSet Onur Özkan
2026-01-03 14:28   ` kernel test robot
2026-09-30 22:10   ` Daniel Almeida
2026-10-11 15:26     ` Onur Özkan [this message]
2026-01-03  7:35 ` [PATCH v10 7/7] MAINTAINERS: add Onur Özkan as WW MUTEX maintainer Onur Özkan
2026-09-30 22:11   ` Daniel Almeida
2026-07-10 12:52 ` [PATCH v10 0/7] rust: add ww_mutex support Alice Ryhl
2026-09-30 22:13 ` Daniel Almeida
2026-10-01  7:40   ` Onur Özkan

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=20261011152630.117725-1-work@onurozkan.dev \
    --to=work@onurozkan.dev \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=daniel@sedlak.dev \
    --cc=felipe_life@live.com \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=longman@redhat.com \
    --cc=lossin@kernel.org \
    --cc=lyude@redhat.com \
    --cc=mingo@redhat.com \
    --cc=ojeda@kernel.org \
    --cc=peterz@infradead.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=thomas.hellstrom@linux.intel.com \
    --cc=tmgross@umich.edu \
    --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®