rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Exposing semaphores
@ 2021-04-27  0:02 Carlos
  2021-04-27  0:08 ` Alex Gaynor
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Carlos @ 2021-04-27  0:02 UTC (permalink / raw)
  To: rust-for-linux

Hello to all,

I recently began assessing what functionality from the kernel is missing
for implementing a basic scull driver.

Semaphores are the first missing primitive that caught my attention, and
being such an important part of the kernel itself, I will make exposing
this functionality my first priority instead.

It should be pretty straightforward, following the stablished convention of
leveraging the kernel API.

Any comments before I dig in?




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Exposing semaphores
  2021-04-27  0:02 Exposing semaphores Carlos
@ 2021-04-27  0:08 ` Alex Gaynor
  2021-04-27  0:12 ` Wedson Almeida Filho
  2021-04-27 10:18 ` Sven Van Asbroeck
  2 siblings, 0 replies; 7+ messages in thread
From: Alex Gaynor @ 2021-04-27  0:08 UTC (permalink / raw)
  To: Carlos; +Cc: rust-for-linux

It sounds like you already identified it, but yup, that'd be my major
piece of advice, follow the conventions and traits use in the other
sync/ locking types:
https://github.com/Rust-for-Linux/linux/tree/rust/rust/kernel/sync

Glad to hear you're already diving into scull!

Cheers,
Alex


On Mon, Apr 26, 2021 at 8:02 PM Carlos <carloscarral13@gmail.com> wrote:
>
> Hello to all,
>
> I recently began assessing what functionality from the kernel is missing
> for implementing a basic scull driver.
>
> Semaphores are the first missing primitive that caught my attention, and
> being such an important part of the kernel itself, I will make exposing
> this functionality my first priority instead.
>
> It should be pretty straightforward, following the stablished convention of
> leveraging the kernel API.
>
> Any comments before I dig in?
>
>
>


-- 
All that is necessary for evil to succeed is for good people to do nothing.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Exposing semaphores
  2021-04-27  0:02 Exposing semaphores Carlos
  2021-04-27  0:08 ` Alex Gaynor
@ 2021-04-27  0:12 ` Wedson Almeida Filho
  2021-04-27  1:43   ` Samantha Miller
  2021-04-27 11:00   ` Sven Van Asbroeck
  2021-04-27 10:18 ` Sven Van Asbroeck
  2 siblings, 2 replies; 7+ messages in thread
From: Wedson Almeida Filho @ 2021-04-27  0:12 UTC (permalink / raw)
  To: Carlos; +Cc: rust-for-linux

On Mon, Apr 26, 2021 at 07:02:07PM -0500, Carlos wrote:
> Semaphores are the first missing primitive that caught my attention, and
> being such an important part of the kernel itself, I will make exposing
> this functionality my first priority instead.

If the semaphores are used only for mutual exclusion, then you may want to just
use a mutex or spinlock that are already there. But that's only if you're
primarily interested in getting a rust scull to work first.

If you actually want to implement semaphores, go for it!

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Exposing semaphores
  2021-04-27  0:12 ` Wedson Almeida Filho
@ 2021-04-27  1:43   ` Samantha Miller
  2021-04-27 11:00   ` Sven Van Asbroeck
  1 sibling, 0 replies; 7+ messages in thread
From: Samantha Miller @ 2021-04-27  1:43 UTC (permalink / raw)
  Cc: rust-for-linux

I don't think this sent last time, but sorry for the spam if it did

For my research project writing a safe Rust file system in the kernel,
I implemented RwLock as a wrapper around the kernel semaphore.
I'm not sure if it'll be exactly what you want, but feel free to use it as a
reference or more. The RwLock is here:
https://gitlab.cs.washington.edu/sm237/bento/-/blob/master/bento/rust/src/std/sync/rwlock.rs.

You can do a similar thing for a spinlock wrapper around the kernel
spinlock, I believe.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Exposing semaphores
  2021-04-27  0:02 Exposing semaphores Carlos
  2021-04-27  0:08 ` Alex Gaynor
  2021-04-27  0:12 ` Wedson Almeida Filho
@ 2021-04-27 10:18 ` Sven Van Asbroeck
  2021-04-27 10:58   ` Greg KH
  2 siblings, 1 reply; 7+ messages in thread
From: Sven Van Asbroeck @ 2021-04-27 10:18 UTC (permalink / raw)
  To: Carlos; +Cc: rust-for-linux

Hi Carlos,

On Mon, Apr 26, 2021 at 8:02 PM Carlos <carloscarral13@gmail.com> wrote:
>
> I recently began assessing what functionality from the kernel is missing
> for implementing a basic scull driver.
>
> Semaphores are the first missing primitive that caught my attention, and
> being such an important part of the kernel itself, I will make exposing
> this functionality my first priority instead.

Isn't scull's semaphore used exclusively in mutex mode, i.e.
initialized using init_MUTEX?

If so, prefer "struct mutex" instead. At the time scull was written
(over 15 years ago) it did not yet exist.
A Rust-for-Linux abstraction is already available:
https://github.com/Rust-for-Linux/linux/blob/rust/rust/kernel/sync/mutex.rs

This is before my time, but I believe that the use of "bare" struct
semaphores in drivers used to be a common anti-pattern, often
resulting in buggy code and/or performance issues - most semaphores
have been replaced by conceptually safer, higher-level primitives, and
today one needs a valid excuse to use them in new or even refactored
driver code. But I do not know the full story.

Sven

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Exposing semaphores
  2021-04-27 10:18 ` Sven Van Asbroeck
@ 2021-04-27 10:58   ` Greg KH
  0 siblings, 0 replies; 7+ messages in thread
From: Greg KH @ 2021-04-27 10:58 UTC (permalink / raw)
  To: Sven Van Asbroeck; +Cc: Carlos, rust-for-linux

On Tue, Apr 27, 2021 at 06:18:52AM -0400, Sven Van Asbroeck wrote:
> Hi Carlos,
> 
> On Mon, Apr 26, 2021 at 8:02 PM Carlos <carloscarral13@gmail.com> wrote:
> >
> > I recently began assessing what functionality from the kernel is missing
> > for implementing a basic scull driver.
> >
> > Semaphores are the first missing primitive that caught my attention, and
> > being such an important part of the kernel itself, I will make exposing
> > this functionality my first priority instead.
> 
> Isn't scull's semaphore used exclusively in mutex mode, i.e.
> initialized using init_MUTEX?
> 
> If so, prefer "struct mutex" instead. At the time scull was written
> (over 15 years ago) it did not yet exist.
> A Rust-for-Linux abstraction is already available:
> https://github.com/Rust-for-Linux/linux/blob/rust/rust/kernel/sync/mutex.rs
> 
> This is before my time, but I believe that the use of "bare" struct
> semaphores in drivers used to be a common anti-pattern, often
> resulting in buggy code and/or performance issues - most semaphores
> have been replaced by conceptually safer, higher-level primitives, and
> today one needs a valid excuse to use them in new or even refactored
> driver code. But I do not know the full story.

I agree, never try to ab-use a semaphore in the way we did it in the
book, that is not a good "pattern" to follow for the majority of kernel
drivers.

A simple mutex should be all that is necessary.

But also, while the CUSE driver has a lot of history, it really was only
created as an example to show how things could work, it isn't all that
useful in the real world :)

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Exposing semaphores
  2021-04-27  0:12 ` Wedson Almeida Filho
  2021-04-27  1:43   ` Samantha Miller
@ 2021-04-27 11:00   ` Sven Van Asbroeck
  1 sibling, 0 replies; 7+ messages in thread
From: Sven Van Asbroeck @ 2021-04-27 11:00 UTC (permalink / raw)
  To: Wedson Almeida Filho; +Cc: Carlos, rust-for-linux

On Mon, Apr 26, 2021 at 8:13 PM Wedson Almeida Filho
<wedsonaf@google.com> wrote:
>
> If the semaphores are used only for mutual exclusion, then you may want to just
> use a mutex or spinlock that are already there. But that's only if you're
> primarily interested in getting a rust scull to work first.

(My apologies for not reading Wedson's message before hitting Reply)

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2021-04-27 11:00 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-27  0:02 Exposing semaphores Carlos
2021-04-27  0:08 ` Alex Gaynor
2021-04-27  0:12 ` Wedson Almeida Filho
2021-04-27  1:43   ` Samantha Miller
2021-04-27 11:00   ` Sven Van Asbroeck
2021-04-27 10:18 ` Sven Van Asbroeck
2021-04-27 10:58   ` Greg KH

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).