From: Boqun Feng <boqun.feng@gmail.com>
To: Benno Lossin <benno.lossin@proton.me>
Cc: "Alice Ryhl" <aliceryhl@google.com>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Wedson Almeida Filho" <wedsonaf@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Andreas Hindborg" <a.hindborg@samsung.com>,
"Peter Zijlstra" <peterz@infradead.org>,
"Ingo Molnar" <mingo@redhat.com>, "Will Deacon" <will@kernel.org>,
"Waiman Long" <longman@redhat.com>,
"Tiago Lam" <tiagolam@gmail.com>,
"Thomas Gleixner" <tglx@linutronix.de>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 3/4] rust: sync: add `CondVar::wait_timeout`
Date: Thu, 21 Dec 2023 08:54:17 -0800 [thread overview]
Message-ID: <ZYRtuRXPgQcN_3cr@boqun-archlinux> (raw)
In-Reply-To: <3fba196a-8cb6-4ce9-9e67-bfb716a2d171@proton.me>
On Wed, Dec 20, 2023 at 11:31:05AM +0000, Benno Lossin wrote:
> On 12/16/23 16:31, Alice Ryhl wrote:
> > @@ -102,7 +105,12 @@ pub fn new(name: &'static CStr, key: &'static LockClassKey) -> impl PinInit<Self
> > })
> > }
> >
> > - fn wait_internal<T: ?Sized, B: Backend>(&self, wait_state: u32, guard: &mut Guard<'_, T, B>) {
> > + fn wait_internal<T: ?Sized, B: Backend>(
> > + &self,
> > + wait_state: u32,
> > + guard: &mut Guard<'_, T, B>,
> > + timeout: c_long,
> > + ) -> c_long {
> > let wait = Opaque::<bindings::wait_queue_entry>::uninit();
> >
> > // SAFETY: `wait` points to valid memory.
> > @@ -113,11 +121,13 @@ fn wait_internal<T: ?Sized, B: Backend>(&self, wait_state: u32, guard: &mut Guar
> > bindings::prepare_to_wait_exclusive(self.wait_list.get(), wait.get(), wait_state as _)
> > };
> >
> > - // SAFETY: No arguments, switches to another thread.
> > - guard.do_unlocked(|| unsafe { bindings::schedule() });
> > + // SAFETY: Switches to another thread. The timeout can be any number.
> > + let ret = guard.do_unlocked(|| unsafe { bindings::schedule_timeout(timeout) });
>
> I am not sure what exactly the safety requirements of `schedule_timeout`
> are. I looked at the function and saw that the timout should not be
> negative. But aside from that only the the context switching should be
> relevant. What things are not allowed to do when calling `schedule`
> (aside from the stuff that klint catches)?
One thing is that you probably don't want to call `schedule` with task
state being TASK_DEAD, if so the `schedule` would be counted as
`ARef<Task>::drop()`, see __schedule() -> context_switch() ->
finish_context_switch(), and the task may be freed after that, which
free the stack of the task, and anything that references a object on the
stack would be a UAF. On the other hand, if the task state is not
TASK_DEAD, `schedule*()` should be a no-op regarding memory safety.
> Because if there are none, then I would put the "switches to another
> thread" part into a normal comment.
>
I think it's possible to make schedule_timeout() a safe function: we can
define setting task state TASK_DEAD as an unsafe operation, whose safety
requirement is something like: "Must ensure that if some code can
reference a memory object that belongs to the task (e.g. a stack
variable) after the task calls a followed `schedule()`, the code must
also hold an additional reference count to the task."
Yes, it might be out of the scope of this patchset though.
Regards,
Boqun
> --
> Cheers,
> Benno
>
> >
> > // SAFETY: Both `wait` and `wait_list` point to valid memory.
> > unsafe { bindings::finish_wait(self.wait_list.get(), wait.get()) };
> > +
> > + ret
> > }
>
next prev parent reply other threads:[~2023-12-21 16:54 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-12-16 15:31 [PATCH v2 0/4] Additional CondVar methods needed by Rust Binder Alice Ryhl
2023-12-16 15:31 ` [PATCH v2 1/4] rust: sync: add `CondVar::notify_sync` Alice Ryhl
2023-12-18 8:31 ` Tiago Lam
2023-12-16 15:31 ` [PATCH v2 2/4] rust: time: add msecs to jiffies conversion Alice Ryhl
2023-12-16 16:30 ` Martin Rodriguez Reboredo
2023-12-18 8:32 ` Tiago Lam
2023-12-18 17:43 ` Benno Lossin
2023-12-18 21:07 ` Boqun Feng
2024-01-04 13:53 ` Alice Ryhl
2023-12-16 15:31 ` [PATCH v2 3/4] rust: sync: add `CondVar::wait_timeout` Alice Ryhl
2023-12-16 16:37 ` Martin Rodriguez Reboredo
2023-12-18 8:32 ` Tiago Lam
2023-12-18 21:15 ` Boqun Feng
2024-01-04 13:48 ` Alice Ryhl
2023-12-20 11:31 ` Benno Lossin
2023-12-21 16:54 ` Boqun Feng [this message]
2024-01-04 13:49 ` Alice Ryhl
2023-12-16 15:31 ` [PATCH v2 4/4] rust: sync: update integer types in CondVar Alice Ryhl
2023-12-16 16:42 ` Martin Rodriguez Reboredo
2024-01-04 13:50 ` Alice Ryhl
2023-12-18 8:33 ` Tiago Lam
2023-12-18 17:45 ` Benno Lossin
2023-12-18 21:18 ` Boqun Feng
2024-01-04 13:54 ` Alice Ryhl
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=ZYRtuRXPgQcN_3cr@boqun-archlinux \
--to=boqun.feng@gmail.com \
--cc=a.hindborg@samsung.com \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=longman@redhat.com \
--cc=mingo@redhat.com \
--cc=ojeda@kernel.org \
--cc=peterz@infradead.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tglx@linutronix.de \
--cc=tiagolam@gmail.com \
--cc=wedsonaf@gmail.com \
--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;
as well as URLs for NNTP newsgroup(s).