rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Elle Rhumsaa <elle@weathered-steel.dev>
To: "Onur Özkan" <work@onurozkan.dev>
Cc: rust-for-linux@vger.kernel.org, linux-kernel@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, bjorn3_gh@protonmail.com,
	daniel.almeida@collabora.com
Subject: Re: [PATCH v6 7/7] add KUnit coverage on ww_mutex/exec implementation
Date: Fri, 5 Sep 2025 23:12:40 +0000	[thread overview]
Message-ID: <aLtuaHNDoQlBO17q@archiso> (raw)
In-Reply-To: <20250903131313.4365-8-work@onurozkan.dev>

On Wed, Sep 03, 2025 at 04:13:13PM +0300, Onur Özkan wrote:
> Adds coverage for `ww_mutex/exec.rs` implementation.
> 
> Signed-off-by: Onur Özkan <work@onurozkan.dev>
> ---
>  rust/kernel/sync/lock/ww_mutex/exec.rs | 148 +++++++++++++++++++++++++
>  1 file changed, 148 insertions(+)
> 
> diff --git a/rust/kernel/sync/lock/ww_mutex/exec.rs b/rust/kernel/sync/lock/ww_mutex/exec.rs
> index 2f1fc540f0b8..543c5218232a 100644
> --- a/rust/kernel/sync/lock/ww_mutex/exec.rs
> +++ b/rust/kernel/sync/lock/ww_mutex/exec.rs
> @@ -174,3 +174,151 @@ fn cleanup_on_deadlock(&mut self) -> Result {
>          Ok(())
>      }
>  }
> +
> +#[kunit_tests(rust_kernel_ww_exec)]
> +mod tests {
> +    use crate::c_str;
> +    use crate::prelude::*;
> +    use crate::sync::Arc;
> +    use pin_init::stack_pin_init;
> +
> +    use super::*;
> +
> +    #[test]
> +    fn test_exec_context_basic_lock_unlock() -> Result {
> +        stack_pin_init!(let class = WwClass::new_wound_wait(c_str!("exec_ctx_basic")));
> +
> +        let mutex = Arc::pin_init(WwMutex::new(10, &class), GFP_KERNEL)?;
> +        let mut ctx = KBox::pin_init(ExecContext::new(&class)?, GFP_KERNEL)?;
> +
> +        ctx.lock(&mutex)?;
> +        ctx.with_locked(&mutex, |v| {
> +            assert_eq!(*v, 10);
> +        })?;
> +
> +        ctx.release_all_locks();
> +        assert!(!mutex.is_locked());
> +
> +        Ok(())
> +    }
> +
> +    #[test]
> +    fn test_exec_context_with_locked_mutates_data() -> Result {
> +        stack_pin_init!(let class = WwClass::new_wound_wait(c_str!("exec_ctx_with_locked")));
> +
> +        let mutex = Arc::pin_init(WwMutex::new(5, &class), GFP_KERNEL)?;
> +        let mut ctx = KBox::pin_init(ExecContext::new(&class)?, GFP_KERNEL)?;
> +
> +        ctx.lock(&mutex)?;
> +
> +        ctx.with_locked(&mutex, |v| {
> +            assert_eq!(*v, 5);
> +            // Increment the value.
> +            *v += 7;
> +        })?;
> +
> +        ctx.with_locked(&mutex, |v| {
> +            // Check that mutation took effect.
> +            assert_eq!(*v, 12);
> +        })?;
> +
> +        Ok(())
> +    }
> +
> +    #[test]
> +    fn test_lock_all_success() -> Result {
> +        stack_pin_init!(let class = WwClass::new_wound_wait(c_str!("lock_all_ok")));
> +
> +        let mutex1 = Arc::pin_init(WwMutex::new(1, &class), GFP_KERNEL)?;
> +        let mutex2 = Arc::pin_init(WwMutex::new(2, &class), GFP_KERNEL)?;
> +        let mut ctx = KBox::pin_init(ExecContext::new(&class)?, GFP_KERNEL)?;
> +
> +        let res = ctx.lock_all(
> +            |ctx| {
> +                let _ = ctx.lock(&mutex1)?;
> +                let _ = ctx.lock(&mutex2)?;
> +                Ok(())
> +            },
> +            |ctx| {
> +                ctx.with_locked(&mutex1, |v| *v += 10)?;
> +                ctx.with_locked(&mutex2, |v| *v += 20)?;
> +                Ok((
> +                    ctx.with_locked(&mutex1, |v| *v)?,
> +                    ctx.with_locked(&mutex2, |v| *v)?,
> +                ))
> +            },
> +        )?;
> +
> +        assert_eq!(res, (11, 22));
> +        assert!(!mutex1.is_locked());
> +        assert!(!mutex2.is_locked());
> +
> +        Ok(())
> +    }
> +
> +    #[test]
> +    fn test_with_different_input_type() -> Result {
> +        stack_pin_init!(let class = WwClass::new_wound_wait(c_str!("lock_all_ok")));
> +
> +        let mutex1 = Arc::pin_init(WwMutex::new(1, &class), GFP_KERNEL)?;
> +        let mutex2 = Arc::pin_init(WwMutex::new("hello", &class), GFP_KERNEL)?;
> +        let mut ctx = KBox::pin_init(ExecContext::new(&class)?, GFP_KERNEL)?;
> +
> +        ctx.lock_all(
> +            |ctx| {
> +                ctx.lock(&mutex1)?;
> +                ctx.lock(&mutex2)?;
> +                Ok(())
> +            },
> +            |ctx| {
> +                ctx.with_locked(&mutex1, |v| assert_eq!(*v, 1))?;
> +                ctx.with_locked(&mutex2, |v| assert_eq!(*v, "hello"))?;
> +                Ok(())
> +            },
> +        )?;
> +
> +        Ok(())
> +    }
> +
> +    #[test]
> +    fn test_lock_all_retries_on_deadlock() -> Result {
> +        stack_pin_init!(let class = WwClass::new_wound_wait(c_str!("lock_all_retry")));
> +
> +        let mutex = Arc::pin_init(WwMutex::new(99, &class), GFP_KERNEL)?;
> +        let mut ctx = KBox::pin_init(ExecContext::new(&class)?, GFP_KERNEL)?;
> +        let mut first_try = true;
> +
> +        let res = ctx.lock_all(
> +            |ctx| {
> +                if first_try {
> +                    first_try = false;
> +                    // Simulate deadlock on first attempt.
> +                    return Err(EDEADLK);
> +                }
> +                ctx.lock(&mutex)
> +            },
> +            |ctx| {
> +                ctx.with_locked(&mutex, |v| {
> +                    *v += 1;
> +                    *v
> +                })
> +            },
> +        )?;
> +
> +        assert_eq!(res, 100);
> +        Ok(())
> +    }
> +
> +    #[test]
> +    fn test_with_locked_on_unlocked_mutex() -> Result {
> +        stack_pin_init!(let class = WwClass::new_wound_wait(c_str!("with_unlocked_mutex")));
> +
> +        let mutex = Arc::pin_init(WwMutex::new(5, &class), GFP_KERNEL)?;
> +        let mut ctx = KBox::pin_init(ExecContext::new(&class)?, GFP_KERNEL)?;
> +
> +        let ecode = ctx.with_locked(&mutex, |_v| {}).unwrap_err();
> +        assert_eq!(EINVAL, ecode);
> +
> +        Ok(())
> +    }
> +}
> --
> 2.50.0
> 
> 

Thanks for adding these tests!

Reviewed-by: Elle Rhumsaa <elle@weathered-steel.dev>

  reply	other threads:[~2025-09-05 23:12 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-03 13:13 [PATCH v6 0/7] rust: add `ww_mutex` support Onur Özkan
2025-09-03 13:13 ` [PATCH v6 1/7] rust: add C wrappers for ww_mutex inline functions Onur Özkan
2025-09-03 13:46   ` Daniel Almeida
2025-09-03 13:13 ` [PATCH v6 2/7] rust: implement `WwClass` for ww_mutex support Onur Özkan
2025-09-03 16:06   ` Boqun Feng
2025-09-04  8:23     ` Onur Özkan
2025-09-03 13:13 ` [PATCH v6 3/7] rust: implement `WwMutex`, `WwAcquireCtx` and `WwMutexGuard` Onur Özkan
2025-09-05 18:49   ` Daniel Almeida
2025-09-05 19:03     ` Daniel Almeida
2025-09-06 11:38       ` Onur
2025-10-22 10:47       ` Onur Özkan
2025-09-06 11:35     ` Onur
2025-09-03 13:13 ` [PATCH v6 4/7] add KUnit coverage on Rust ww_mutex implementation Onur Özkan
2025-09-05 19:04   ` Daniel Almeida
2025-09-03 13:13 ` [PATCH v6 5/7] rust: ww_mutex: add context-free locking functions Onur Özkan
2025-09-05 19:14   ` Daniel Almeida
2025-09-06 11:20     ` Onur
2025-10-21 13:31       ` Daniel Almeida
2025-10-21 13:20     ` Onur Özkan
2025-09-03 13:13 ` [PATCH v6 6/7] rust: ww_mutex/exec: add high-level API Onur Özkan
2025-09-05 19:42   ` Daniel Almeida
2025-09-06 11:13     ` Onur
2025-09-06 15:04       ` Daniel Almeida
2025-09-07  8:20         ` Onur
2025-09-07  8:38           ` Onur
2025-10-21 19:36             ` Onur Özkan
2025-10-21 13:24     ` Onur Özkan
2025-10-21 14:04     ` Onur Özkan
2025-09-05 23:11   ` Elle Rhumsaa
2025-09-06 11:47     ` Onur Özkan
2025-09-03 13:13 ` [PATCH v6 7/7] add KUnit coverage on ww_mutex/exec implementation Onur Özkan
2025-09-05 23:12   ` Elle Rhumsaa [this message]
2025-10-16 19:47 ` [PATCH v6 0/7] rust: add `ww_mutex` support Lyude Paul
2025-10-17  5:03   ` 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=aLtuaHNDoQlBO17q@archiso \
    --to=elle@weathered-steel.dev \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.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=tmgross@umich.edu \
    --cc=will@kernel.org \
    --cc=work@onurozkan.dev \
    /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).